1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
57
62
67
73
79
85
90
96
102
108
114
120
126
131
136
142
143
144
145
146
147
148
149
153
154
158
159
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
187
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
216
217
218
219
220
221
227
228
229
230
231
232
233
234
235
236
237
239
240
241
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
282
283
288
289
290
291
292
294
295
296
297
298
299
300
301
302
303
304
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
343
344
345
352
353
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
378
379
380
381
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
409
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
435
436
437
438
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
494
495
516
517
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
571
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
636
637
638
639
640
647
648
649
650
651
654
655
656
657
661
662
663
664
665
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
696
697
698
699
700
701
702
703
704
705
706
707
708
709
711
712
713
714
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
744
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
775
776
777
778
779
780
781
782
783
785
790
792
794
797
798
799
800
801
802
803
804
805
806
807
810
811
812
813
814
815
816
817
818
821
822
826
827
828
829
830
831
832
833
834
835
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
875
876
877
884
885
886
887
888
889
890
891
892
893
894
895
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
932
939
940
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
963
964
967
968
969
970
971
972
975
976
977
978
979
980
981
982
983
995
996
997
1000
1001
1002
1003
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1050
1051
1056
1057
1058
1059
1060
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1162
1163
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1197
1198
1202
1203
1204
1205
1206
1207
1208
1209
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1236
1237
1238
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1315
1316
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1345
1346
1347
1348
1349
1350
1351
1352
1353
1358
1359
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1457
1458
1463
1464
1465
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1492
1493
1495
1496
1497
1498
1499
1504
1505
1506
1507
1508
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1525
1526
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1612
1613
1614
1615
1616
1620
1621
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1801
1802
1806
1811
1812
1813
1814
1815
1816
1817
1818
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1856
1857
1858
1859
1860
1861
1862
1864
1865
1866
1867
1868
1870
1875
1876
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1945
1950
1956
1957
1958
1959
1960
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2034
2035
2036
2037
2038
2039
2043
2044
2045
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2072
2073
2074
2075
2076
2077
2078
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2155
2156
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2182
2183
2184
2185
2186
2187
2188
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2209
2210
2211
2212
2213
2214
2215
2226
2227
2228
2229
2230
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2266
2267
2268
2269
2270
2271
2272
2273
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2297
2298
2299
2300
2301
2302
2303
2304
2305
2309
2310
2311
2312
2313
2317
2321
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2390
2391
2392
2393
2394
2395
2396
2397
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2416
2417
2418
2419
2420
2421
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2458
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2493
2494
2498
2499
2500
2501
2505
2506
2507
2508
2509
2510
2511
2512
2516
2517
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2549
2550
2558
2559
2560
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2619
2620
2621
2622
2623
2624
2625
2626
2627
2633
2634
2635
2640
2641
2642
2643
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2747
2748
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2789
2790
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2832
2833
2834
2835
2836
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2875
2876
2877
2878
2879
2880
2886
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2973
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3099
3100
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3135
3136
3137
3138
3139
3140
3141
3142
3143
3152
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3184
3185
3186
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3311
3312
3313
3314
3317
3320
3321
3324
3325
3326
3327
3328
3329
3330
3331
3332
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
/* ... */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "jtag/interface.h"
#include "breakpoints.h"
#include "cortex_m.h"
#include "target_request.h"
#include "target_type.h"
#include "arm_adi_v5.h"
#include "arm_disassembler.h"
#include "register.h"
#include "arm_opcodes.h"
#include "arm_semihosting.h"
#include "smp.h"
#include <helper/nvp.h>
#include <helper/time_support.h>
#include <rtt/rtt.h>
14 includes
/* ... */
#define DHCSR_S_REGRDY_TIMEOUT (500)
static const struct cortex_m_part_info cortex_m_parts[] = {
{
.impl_part = CORTEX_M0_PARTNO,
.name = "Cortex-M0",
.arch = ARM_ARCH_V6M,
...},
{
.impl_part = CORTEX_M0P_PARTNO,
.name = "Cortex-M0+",
.arch = ARM_ARCH_V6M,
...},
{
.impl_part = CORTEX_M1_PARTNO,
.name = "Cortex-M1",
.arch = ARM_ARCH_V6M,
...},
{
.impl_part = CORTEX_M3_PARTNO,
.name = "Cortex-M3",
.arch = ARM_ARCH_V7M,
.flags = CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K,
...},
{
.impl_part = CORTEX_M4_PARTNO,
.name = "Cortex-M4",
.arch = ARM_ARCH_V7M,
.flags = CORTEX_M_F_HAS_FPV4 | CORTEX_M_F_TAR_AUTOINCR_BLOCK_4K,
...},
{
.impl_part = CORTEX_M7_PARTNO,
.name = "Cortex-M7",
.arch = ARM_ARCH_V7M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = CORTEX_M23_PARTNO,
.name = "Cortex-M23",
.arch = ARM_ARCH_V8M,
...},
{
.impl_part = CORTEX_M33_PARTNO,
.name = "Cortex-M33",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = CORTEX_M35P_PARTNO,
.name = "Cortex-M35P",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = CORTEX_M52_PARTNO,
.name = "Cortex-M52",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = CORTEX_M55_PARTNO,
.name = "Cortex-M55",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = CORTEX_M85_PARTNO,
.name = "Cortex-M85",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = STAR_MC1_PARTNO,
.name = "STAR-MC1",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
{
.impl_part = INFINEON_SLX2_PARTNO,
.name = "Infineon-SLx2",
.arch = ARM_ARCH_V8M,
...},
{
.impl_part = REALTEK_M200_PARTNO,
.name = "Real-M200 (KM0)",
.arch = ARM_ARCH_V8M,
...},
{
.impl_part = REALTEK_M300_PARTNO,
.name = "Real-M300 (KM4)",
.arch = ARM_ARCH_V8M,
.flags = CORTEX_M_F_HAS_FPV5,
...},
...};
static int cortex_m_store_core_reg_u32(struct target *target,
uint32_t num, uint32_t value);
static void cortex_m_dwt_free(struct target *target);
/* ... */
static inline void cortex_m_cumulate_dhcsr_sticky(struct cortex_m_common *cortex_m,
uint32_t dhcsr)
{
cortex_m->dcb_dhcsr_cumulated_sticky |= dhcsr;
}{ ... }
/* ... */
static int cortex_m_read_dhcsr_atomic_sticky(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = target_to_armv7m(target);
int retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DHCSR,
&cortex_m->dcb_dhcsr);
if (retval != ERROR_OK)
return retval;
cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
return ERROR_OK;
}{ ... }
static int cortex_m_load_core_reg_u32(struct target *target,
uint32_t regsel, uint32_t *value)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = target_to_armv7m(target);
int retval;
uint32_t dcrdr, tmp_value;
int64_t then;
/* ... */
if (target->dbg_msg_enabled) {
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
if (retval != ERROR_OK)
return retval;
}if (target->dbg_msg_enabled) { ... }
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
if (retval != ERROR_OK)
return retval;
then = timeval_ms();
while (1) {
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR,
&cortex_m->dcb_dhcsr);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DCRDR,
&tmp_value);
if (retval != ERROR_OK)
return retval;
cortex_m_cumulate_dhcsr_sticky(cortex_m, cortex_m->dcb_dhcsr);
if (cortex_m->dcb_dhcsr & S_REGRDY)
break;
cortex_m->slow_register_read = true;
if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
return ERROR_TIMEOUT_REACHED;
}if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) { ... }
keep_alive();
}while (1) { ... }
*value = tmp_value;
if (target->dbg_msg_enabled) {
/* ... */
if (retval == ERROR_OK)
retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
}if (target->dbg_msg_enabled) { ... }
return retval;
}{ ... }
static int cortex_m_slow_read_all_regs(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = target_to_armv7m(target);
const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
/* ... */
cortex_m->slow_register_read = false;
for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) {
struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
if (r->exist) {
int retval = armv7m->arm.read_core_reg(target, r, reg_id, ARM_MODE_ANY);
if (retval != ERROR_OK)
return retval;
}if (r->exist) { ... }
}for (unsigned int reg_id = 0; reg_id < num_regs; reg_id++) { ... }
if (!cortex_m->slow_register_read)
LOG_TARGET_DEBUG(target, "Switching back to fast register reads");
return ERROR_OK;
}{ ... }
static int cortex_m_queue_reg_read(struct target *target, uint32_t regsel,
uint32_t *reg_value, uint32_t *dhcsr)
{
struct armv7m_common *armv7m = target_to_armv7m(target);
int retval;
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DHCSR, dhcsr);
if (retval != ERROR_OK)
return retval;
return mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, reg_value);
}{ ... }
static int cortex_m_fast_read_all_regs(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = target_to_armv7m(target);
int retval;
uint32_t dcrdr;
/* ... */
bool dbg_msg_enabled = target->dbg_msg_enabled;
if (dbg_msg_enabled) {
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
if (retval != ERROR_OK)
return retval;
}if (dbg_msg_enabled) { ... }
const unsigned int num_regs = armv7m->arm.core_cache->num_regs;
const unsigned int n_r32 = ARMV7M_LAST_REG - ARMV7M_CORE_FIRST_REG + 1
+ ARMV7M_FPU_LAST_REG - ARMV7M_FPU_FIRST_REG + 1;
/* ... */
uint32_t r_vals[n_r32];
uint32_t dhcsr[n_r32];
unsigned int wi = 0;
unsigned int reg_id;
for (reg_id = 0; reg_id < num_regs; reg_id++) {
struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
if (!r->exist)
continue;
if (r->size <= 8) {
/* ... */
continue;
}if (r->size <= 8) { ... }
uint32_t regsel = armv7m_map_id_to_regsel(reg_id);
retval = cortex_m_queue_reg_read(target, regsel, &r_vals[wi],
&dhcsr[wi]);
if (retval != ERROR_OK)
return retval;
wi++;
assert(r->size == 32 || r->size == 64);
if (r->size == 32)
continue;
assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
retval = cortex_m_queue_reg_read(target, regsel + 1, &r_vals[wi],
&dhcsr[wi]);
if (retval != ERROR_OK)
return retval;
wi++;
}for (reg_id = 0; reg_id < num_regs; reg_id++) { ... }
assert(wi <= n_r32);
retval = dap_run(armv7m->debug_ap->dap);
if (retval != ERROR_OK)
return retval;
if (dbg_msg_enabled) {
/* ... */
retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
if (retval != ERROR_OK)
return retval;
}if (dbg_msg_enabled) { ... }
bool not_ready = false;
for (unsigned int i = 0; i < wi; i++) {
if ((dhcsr[i] & S_REGRDY) == 0) {
not_ready = true;
LOG_TARGET_DEBUG(target, "Register %u was not ready during fast read", i);
}if ((dhcsr[i] & S_REGRDY) == 0) { ... }
cortex_m_cumulate_dhcsr_sticky(cortex_m, dhcsr[i]);
}for (unsigned int i = 0; i < wi; i++) { ... }
if (not_ready) {
/* ... */
return ERROR_TIMEOUT_REACHED;
}if (not_ready) { ... }
LOG_TARGET_DEBUG(target, "read %u 32-bit registers", wi);
unsigned int ri = 0;
for (reg_id = 0; reg_id < num_regs; reg_id++) {
struct reg *r = &armv7m->arm.core_cache->reg_list[reg_id];
if (!r->exist)
continue;
r->dirty = false;
unsigned int reg32_id;
uint32_t offset;
if (armv7m_map_reg_packing(reg_id, ®32_id, &offset)) {
struct reg *r32 = &armv7m->arm.core_cache->reg_list[reg32_id];
/* ... */
assert(r32->valid);
buf_cpy(r32->value + offset, r->value, r->size);
}if (armv7m_map_reg_packing(reg_id, ®32_id, &offset)) { ... } else {
assert(r->size == 32 || r->size == 64);
buf_set_u32(r->value, 0, 32, r_vals[ri++]);
if (r->size == 64) {
assert(reg_id >= ARMV7M_FPU_FIRST_REG && reg_id <= ARMV7M_FPU_LAST_REG);
buf_set_u32(r->value + 4, 0, 32, r_vals[ri++]);
}if (r->size == 64) { ... }
}else { ... }
r->valid = true;
}for (reg_id = 0; reg_id < num_regs; reg_id++) { ... }
assert(ri == wi);
return retval;
}{ ... }
static int cortex_m_store_core_reg_u32(struct target *target,
uint32_t regsel, uint32_t value)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = target_to_armv7m(target);
int retval;
uint32_t dcrdr;
int64_t then;
/* ... */
if (target->dbg_msg_enabled) {
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DCRDR, &dcrdr);
if (retval != ERROR_OK)
return retval;
}if (target->dbg_msg_enabled) { ... }
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, value);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRSR, regsel | DCRSR_WNR);
if (retval != ERROR_OK)
return retval;
then = timeval_ms();
while (1) {
retval = cortex_m_read_dhcsr_atomic_sticky(target);
if (retval != ERROR_OK)
return retval;
if (cortex_m->dcb_dhcsr & S_REGRDY)
break;
if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) {
LOG_TARGET_ERROR(target, "Timeout waiting for DCRDR transfer ready");
return ERROR_TIMEOUT_REACHED;
}if (timeval_ms() > then + DHCSR_S_REGRDY_TIMEOUT) { ... }
keep_alive();
}while (1) { ... }
if (target->dbg_msg_enabled) {
/* ... */
if (retval == ERROR_OK)
retval = mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DCRDR, dcrdr);
}if (target->dbg_msg_enabled) { ... }
return retval;
}{ ... }
static int cortex_m_write_debug_halt_mask(struct target *target,
uint32_t mask_on, uint32_t mask_off)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
cortex_m->dcb_dhcsr &= ~((0xFFFFul << 16) | mask_off);
cortex_m->dcb_dhcsr |= DBGKEY | C_DEBUGEN | mask_on;
return mem_ap_write_atomic_u32(armv7m->debug_ap, DCB_DHCSR, cortex_m->dcb_dhcsr);
}{ ... }
static int cortex_m_set_maskints(struct target *target, bool mask)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
if (!!(cortex_m->dcb_dhcsr & C_MASKINTS) != mask)
return cortex_m_write_debug_halt_mask(target, mask ? C_MASKINTS : 0, mask ? 0 : C_MASKINTS);
else
return ERROR_OK;
}{ ... }
static int cortex_m_set_maskints_for_halt(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
switch (cortex_m->isrmasking_mode) {
case CORTEX_M_ISRMASK_AUTO:
return cortex_m_set_maskints(target, false);
case CORTEX_M_ISRMASK_AUTO:
case CORTEX_M_ISRMASK_OFF:
return cortex_m_set_maskints(target, false);
case CORTEX_M_ISRMASK_OFF:
case CORTEX_M_ISRMASK_ON:
return cortex_m_set_maskints(target, true);
case CORTEX_M_ISRMASK_ON:
case CORTEX_M_ISRMASK_STEPONLY:
/* ... */
return cortex_m_set_maskints(target, cortex_m->maskints_erratum);case CORTEX_M_ISRMASK_STEPONLY:
}switch (cortex_m->isrmasking_mode) { ... }
return ERROR_OK;
}{ ... }
static int cortex_m_set_maskints_for_run(struct target *target)
{
switch (target_to_cm(target)->isrmasking_mode) {
case CORTEX_M_ISRMASK_AUTO:
return cortex_m_set_maskints(target, false);
case CORTEX_M_ISRMASK_AUTO:
case CORTEX_M_ISRMASK_OFF:
return cortex_m_set_maskints(target, false);
case CORTEX_M_ISRMASK_OFF:
case CORTEX_M_ISRMASK_ON:
return cortex_m_set_maskints(target, true);
case CORTEX_M_ISRMASK_ON:
case CORTEX_M_ISRMASK_STEPONLY:
return cortex_m_set_maskints(target, false);case CORTEX_M_ISRMASK_STEPONLY:
}switch (target_to_cm(target)->isrmasking_mode) { ... }
return ERROR_OK;
}{ ... }
static int cortex_m_set_maskints_for_step(struct target *target)
{
switch (target_to_cm(target)->isrmasking_mode) {
case CORTEX_M_ISRMASK_AUTO:
return cortex_m_set_maskints(target, true);
case CORTEX_M_ISRMASK_AUTO:
case CORTEX_M_ISRMASK_OFF:
return cortex_m_set_maskints(target, false);
case CORTEX_M_ISRMASK_OFF:
case CORTEX_M_ISRMASK_ON:
return cortex_m_set_maskints(target, true);
case CORTEX_M_ISRMASK_ON:
case CORTEX_M_ISRMASK_STEPONLY:
return cortex_m_set_maskints(target, true);case CORTEX_M_ISRMASK_STEPONLY:
}switch (target_to_cm(target)->isrmasking_mode) { ... }
return ERROR_OK;
}{ ... }
static int cortex_m_clear_halt(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
int retval;
cortex_m_write_debug_halt_mask(target, C_HALT, C_STEP);
retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_DFSR, &cortex_m->nvic_dfsr);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_write_atomic_u32(armv7m->debug_ap, NVIC_DFSR, cortex_m->nvic_dfsr);
if (retval != ERROR_OK)
return retval;
LOG_TARGET_DEBUG(target, "NVIC_DFSR 0x%" PRIx32 "", cortex_m->nvic_dfsr);
return ERROR_OK;
}{ ... }
static int cortex_m_single_step_core(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
int retval;
/* ... */
if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) {
retval = cortex_m_write_debug_halt_mask(target, C_MASKINTS, 0);
if (retval != ERROR_OK)
return retval;
}if (!(cortex_m->dcb_dhcsr & C_MASKINTS)) { ... }
retval = cortex_m_write_debug_halt_mask(target, C_STEP, C_HALT);
if (retval != ERROR_OK)
return retval;
LOG_TARGET_DEBUG(target, "single step");
cortex_m_clear_halt(target);
return ERROR_OK;
}{ ... }
static int cortex_m_enable_fpb(struct target *target)
{
int retval = target_write_u32(target, FP_CTRL, 3);
if (retval != ERROR_OK)
return retval;
uint32_t fpctrl;
retval = target_read_u32(target, FP_CTRL, &fpctrl);
if (retval != ERROR_OK)
return retval;
if (fpctrl & 1)
return ERROR_OK;
return ERROR_FAIL;
}{ ... }
static int cortex_m_endreset_event(struct target *target)
{
int retval;
uint32_t dcb_demcr;
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
struct adiv5_dap *swjdp = cortex_m->armv7m.arm.dap;
struct cortex_m_fp_comparator *fp_list = cortex_m->fp_comparator_list;
struct cortex_m_dwt_comparator *dwt_list = cortex_m->dwt_comparator_list;
retval = mem_ap_read_atomic_u32(armv7m->debug_ap, DCB_DEMCR, &dcb_demcr);
if (retval != ERROR_OK)
return retval;
LOG_TARGET_DEBUG(target, "DCB_DEMCR = 0x%8.8" PRIx32 "", dcb_demcr);
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DCRDR, 0);
if (retval != ERROR_OK)
return retval;
retval = cortex_m_read_dhcsr_atomic_sticky(target);
if (retval != ERROR_OK)
return retval;
if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) {
retval = cortex_m_write_debug_halt_mask(target, 0, C_HALT | C_STEP | C_MASKINTS);
if (retval != ERROR_OK)
return retval;
}if (!(cortex_m->dcb_dhcsr & C_DEBUGEN)) { ... }
cortex_m_set_maskints_for_run(target);
/* ... */
retval = mem_ap_write_u32(armv7m->debug_ap, DCB_DEMCR, TRCENA | armv7m->demcr);
if (retval != ERROR_OK)
return retval;
/* ... */
retval = cortex_m_enable_fpb(target);
if (retval != ERROR_OK) {
LOG_TARGET_ERROR(target, "Failed to enable the FPB");
return retval;
}if (retval != ERROR_OK) { ... }
cortex_m->fpb_enabled = true;
for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) {
retval = target_write_u32(target, fp_list[i].fpcr_address, fp_list[i].fpcr_value);
if (retval != ERROR_OK)
return retval;
}for (unsigned int i = 0; i < cortex_m->fp_num_code + cortex_m->fp_num_lit; i++) { ... }
for (unsigned int i = 0; i < cortex_m->dwt_num_comp; i++) {
retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 0,
dwt_list[i].comp);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 4,
dwt_list[i].mask);
if (retval != ERROR_OK)
return retval;
retval = target_write_u32(target, dwt_list[i].dwt_comparator_address + 8,
dwt_list[i].function);
if (retval != ERROR_OK)
return retval;
}for (unsigned int i = 0; i < cortex_m->dwt_num_comp; i++) { ... }
retval = dap_run(swjdp);
if (retval != ERROR_OK)
return retval;
register_cache_invalidate(armv7m->arm.core_cache);
/* ... */
retval = cortex_m_read_dhcsr_atomic_sticky(target);
if (retval != ERROR_OK)
return retval;
return retval;
}{ ... }
static int cortex_m_examine_debug_reason(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
/* ... */
if ((target->debug_reason != DBG_REASON_DBGRQ)
&& (target->debug_reason != DBG_REASON_SINGLESTEP)) {
if (cortex_m->nvic_dfsr & DFSR_BKPT) {
target->debug_reason = DBG_REASON_BREAKPOINT;
if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
target->debug_reason = DBG_REASON_WPTANDBKPT;
}if (cortex_m->nvic_dfsr & DFSR_BKPT) { ... } else if (cortex_m->nvic_dfsr & DFSR_DWTTRAP)
target->debug_reason = DBG_REASON_WATCHPOINT;
else if (cortex_m->nvic_dfsr & DFSR_VCATCH)
target->debug_reason = DBG_REASON_BREAKPOINT;
else if (cortex_m->nvic_dfsr & DFSR_EXTERNAL)
target->debug_reason = DBG_REASON_DBGRQ;
else
target->debug_reason = DBG_REASON_UNDEFINED;
}if ((target->debug_reason != DBG_REASON_DBGRQ) && (target->debug_reason != DBG_REASON_SINGLESTEP)) { ... }
return ERROR_OK;
}{ ... }
static int cortex_m_examine_exception_reason(struct target *target)
{
uint32_t shcsr = 0, except_sr = 0, cfsr = -1, except_ar = -1;
struct armv7m_common *armv7m = target_to_armv7m(target);
struct adiv5_dap *swjdp = armv7m->arm.dap;
int retval;
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SHCSR, &shcsr);
if (retval != ERROR_OK)
return retval;
switch (armv7m->exception_number) {
case 2:
break;case 2:
case 3:
retval = mem_ap_read_atomic_u32(armv7m->debug_ap, NVIC_HFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
if (except_sr & 0x40000000) {
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &cfsr);
if (retval != ERROR_OK)
return retval;
}if (except_sr & 0x40000000) { ... }
break;case 3:
case 4:
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_MMFAR, &except_ar);
if (retval != ERROR_OK)
return retval;
break;case 4:
case 5:
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_BFAR, &except_ar);
if (retval != ERROR_OK)
return retval;
break;case 5:
case 6:
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_CFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
break;case 6:
case 7:
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_SFAR, &except_ar);
if (retval != ERROR_OK)
return retval;
break;case 7:
case 11:
break;case 11:
case 12:
retval = mem_ap_read_u32(armv7m->debug_ap, NVIC_DFSR, &except_sr);
if (retval != ERROR_OK)
return retval;
break;case 12:
case 14:
break;case 14:
case 15:
break;case 15:
default:
except_sr = 0;
break;default
}switch (armv7m->exception_number) { ... }
retval = dap_run(swjdp);
if (retval == ERROR_OK)
LOG_TARGET_DEBUG(target, "%s SHCSR 0x%" PRIx32 ", SR 0x%" PRIx32
", CFSR 0x%" PRIx32 ", AR 0x%" PRIx32,
armv7m_exception_string(armv7m->exception_number),
shcsr, except_sr, cfsr, except_ar);
return retval;
}{ ... }
/* ... */
static int cortex_m_erratum_check_breakpoint(struct target *target)
{
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
struct arm *arm = &armv7m->arm;
uint32_t pc = buf_get_u32(arm->pc->value, 0, 32);
/* ... */
struct breakpoint *bkpt = breakpoint_find(target, pc);
if (bkpt) {
LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint confirmed");
return ERROR_OK;
}if (bkpt) { ... }
if (pc >= 0xe0000000u)
return ERROR_OK;
uint16_t insn;
int retval = target_read_u16(target, pc, &insn);
if (retval != ERROR_OK)
return ERROR_OK;
if ((insn & 0xff00) == (ARMV5_T_BKPT(0) & 0xff00)) {
LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint embedded in code confirmed");
return ERROR_OK;
}if ((insn & 0xff00) == (ARMV5_T_BKPT(0) & 0xff00)) { ... }
LOG_TARGET_DEBUG(target, "Erratum 3092511: breakpoint not found, proceed with resume");
return ERROR_TARGET_HALTED_DO_RESUME;
}{ ... }
static int cortex_m_debug_entry(struct target *target)
{
uint32_t xpsr;
int retval;
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
struct arm *arm = &armv7m->arm;
struct reg *r;
LOG_TARGET_DEBUG(target, " ");
/* ... */
cortex_m_set_maskints_for_halt(target);
cortex_m_clear_halt(target);
retval = cortex_m_read_dhcsr_atomic_sticky(target);
if (retval != ERROR_OK)
return retval;
retval = armv7m->examine_debug_reason(target);
if (retval != ERROR_OK)
return retval;
uint32_t dscsr = 0;
if (armv7m->arm.arch == ARM_ARCH_V8M) {
retval = mem_ap_read_u32(armv7m->debug_ap, DCB_DSCSR, &dscsr);
if (retval != ERROR_OK)
return retval;
}if (armv7m->arm.arch == ARM_ARCH_V8M) { ... }
if (!cortex_m->slow_register_read) {
retval = cortex_m_fast_read_all_regs(target);
if (retval == ERROR_TIMEOUT_REACHED) {
cortex_m->slow_register_read = true;
LOG_TARGET_DEBUG(target, "Switched to slow register read");
}if (retval == ERROR_TIMEOUT_REACHED) { ... }
}if (!cortex_m->slow_register_read) { ... }
if (cortex_m->slow_register_read)
retval = cortex_m_slow_read_all_regs(target);
if (retval != ERROR_OK)
return retval;
r = arm->cpsr;
xpsr = buf_get_u32(r->value, 0, 32);
if (xpsr & 0x1FF) {
armv7m->exception_number = (xpsr & 0x1FF);
arm->core_mode = ARM_MODE_HANDLER;
arm->map = armv7m_msp_reg_map;
}if (xpsr & 0x1FF) { ... } else {
unsigned control = buf_get_u32(arm->core_cache
->reg_list[ARMV7M_CONTROL].value, 0, 3);
arm->core_mode = control & 1
? ARM_MODE_USER_THREAD
: ARM_MODE_THREAD;
if (control & 2)
arm->map = armv7m_psp_reg_map;
else
arm->map = armv7m_msp_reg_map;
armv7m->exception_number = 0;
}else { ... }
if (armv7m->exception_number)
cortex_m_examine_exception_reason(target);
bool secure_state = (dscsr & DSCSR_CDS) == DSCSR_CDS;
LOG_TARGET_DEBUG(target, "entered debug state in core mode: %s at PC 0x%" PRIx32
", cpu in %s state, target->state: %s",
arm_mode_name(arm->core_mode),
buf_get_u32(arm->pc->value, 0, 32),
secure_state ? "Secure" : "Non-Secure",
target_state_name(target));
/* ... */
if (cortex_m->incorrect_halt_erratum
&& armv7m->exception_number
&& cortex_m->nvic_dfsr == (DFSR_BKPT | DFSR_HALTED)) {
retval = cortex_m_erratum_check_breakpoint(target);
if (retval != ERROR_OK)
return retval;
}if (cortex_m->incorrect_halt_erratum && armv7m->exception_number && cortex_m->nvic_dfsr == (DFSR_BKPT | DFSR_HALTED)) { ... }
if (armv7m->post_debug_entry) {
retval = armv7m->post_debug_entry(target);
if (retval != ERROR_OK)
return retval;
}if (armv7m->post_debug_entry) { ... }
return ERROR_OK;
}{ ... }
static int cortex_m_poll_one(struct target *target)
{
int detected_failure = ERROR_OK;
int retval = ERROR_OK;
enum target_state prev_target_state = target->state;
struct cortex_m_common *cortex_m = target_to_cm(target);
struct armv7m_common *armv7m = &cortex_m->armv7m;
retval = cortex_m_read_dhcsr_atomic_sticky(target);
if (retval != ERROR_OK) {
target->state = TARGET_UNKNOWN;
return retval;
}if (retval != ERROR_OK) { ... }
/* ... */
if (cortex_m->dcb_dhcsr & S_LOCKUP) {
LOG_TARGET_ERROR(target, "clearing lockup after double fault")