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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
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
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
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
568
569
570
571
572
573
574
575
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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
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
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
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
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
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
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
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
1410
1411
1412
1413
1414
1415
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
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
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
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
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
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
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
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
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
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
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
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
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
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
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
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
|
/** @file
Commond Debug Agent library implementition. It mainly includes
the first C function called by exception/interrupt handlers,
read/write debug packet to communication with HOST based on transfer
protocol.
Copyright (c) 2010 - 2014, Intel Corporation. All rights reserved.<BR>
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php.
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/
#include "DebugAgent.h"
#include "Ia32/DebugException.h"
CHAR8 mErrorMsgVersionAlert[] = "\rThe SourceLevelDebugPkg you are using requires a newer version of the Intel(R) UDK Debugger Tool.\r\n";
CHAR8 mErrorMsgSendInitPacket[] = "\rSend INIT break packet and try to connect the HOST (Intel(R) UDK Debugger Tool v1.4) ...\r\n";
CHAR8 mErrorMsgConnectOK[] = "HOST connection is successful!\r\n";
CHAR8 mErrorMsgConnectFail[] = "HOST connection is failed!\r\n";
CHAR8 mWarningMsgIngoreBreakpoint[] = "Ignore break point in SMM for SMI issued during DXE debugging!\r\n";
//
// Vector Handoff Info list used by Debug Agent for persist
//
EFI_VECTOR_HANDOFF_INFO mVectorHandoffInfoDebugAgent[] = {
{
DEBUG_EXCEPT_DIVIDE_ERROR, // Vector 0
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_DEBUG, // Vector 1
EFI_VECTOR_HANDOFF_DO_NOT_HOOK,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_NMI, // Vector 2
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_BREAKPOINT, // Vector 3
EFI_VECTOR_HANDOFF_DO_NOT_HOOK,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_OVERFLOW, // Vector 4
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_BOUND, // Vector 5
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_INVALID_OPCODE, // Vector 6
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_DOUBLE_FAULT, // Vector 8
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_INVALID_TSS, // Vector 10
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_SEG_NOT_PRESENT, // Vector 11
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_STACK_FAULT, // Vector 12
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_GP_FAULT, // Vector 13
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_PAGE_FAULT, // Vector 14
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_FP_ERROR, // Vector 16
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_ALIGNMENT_CHECK, // Vector 17
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_MACHINE_CHECK, // Vector 18
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_EXCEPT_SIMD, // Vector 19
EFI_VECTOR_HANDOFF_HOOK_BEFORE,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_TIMER_VECTOR, // Vector 32
EFI_VECTOR_HANDOFF_DO_NOT_HOOK,
EFI_DEBUG_AGENT_GUID
},
{
DEBUG_MAILBOX_VECTOR, // Vector 33
EFI_VECTOR_HANDOFF_DO_NOT_HOOK,
EFI_DEBUG_AGENT_GUID
},
{
0,
EFI_VECTOR_HANDOFF_LAST_ENTRY,
{ 0 }
}
};
UINTN mVectorHandoffInfoCount = sizeof (mVectorHandoffInfoDebugAgent) / sizeof (EFI_VECTOR_HANDOFF_INFO);
/**
Calculate CRC16 for target data.
@param[in] Data The target data.
@param[in] DataSize The target data size.
@param[in] Crc Initial CRC.
@return UINT16 The CRC16 value.
**/
UINT16
CalculateCrc16 (
IN UINT8 *Data,
IN UINTN DataSize,
IN UINT16 Crc
)
{
UINTN Index;
UINTN BitIndex;
for (Index = 0; Index < DataSize; Index++) {
Crc ^= Data[Index];
for (BitIndex = 0; BitIndex < 8; BitIndex++) {
if ((Crc & 0x8000) != 0) {
Crc <<= 1;
Crc ^= 0x1021;
} else {
Crc <<= 1;
}
}
}
return Crc;
}
/**
Read IDT entry to check if IDT entries are setup by Debug Agent.
@retval TRUE IDT entries were setup by Debug Agent.
@retval FALSE IDT entries were not setup by Debug Agent.
**/
BOOLEAN
IsDebugAgentInitialzed (
VOID
)
{
UINTN InterruptHandler;
InterruptHandler = (UINTN) GetExceptionHandlerInIdtEntry (0);
if (InterruptHandler >= 4 && *(UINT32 *)(InterruptHandler - 4) == AGENT_HANDLER_SIGNATURE) {
return TRUE;
} else {
return FALSE;
}
}
/**
Find and report module image info to HOST.
@param[in] AlignSize Image aligned size.
**/
VOID
FindAndReportModuleImageInfo (
IN UINTN AlignSize
)
{
UINTN Pe32Data;
EFI_IMAGE_DOS_HEADER *DosHdr;
EFI_IMAGE_OPTIONAL_HEADER_PTR_UNION Hdr;
PE_COFF_LOADER_IMAGE_CONTEXT ImageContext;
//
// Find Image Base
//
Pe32Data = ((UINTN)mErrorMsgVersionAlert) & ~(AlignSize - 1);
while (Pe32Data != 0) {
DosHdr = (EFI_IMAGE_DOS_HEADER *) Pe32Data;
if (DosHdr->e_magic == EFI_IMAGE_DOS_SIGNATURE) {
//
// DOS image header is present, so read the PE header after the DOS image header.
// Check if address overflow firstly.
//
if ((MAX_ADDRESS - (UINTN)DosHdr->e_lfanew) > Pe32Data) {
Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)(Pe32Data + (UINTN)(DosHdr->e_lfanew));
if (Hdr.Pe32->Signature == EFI_IMAGE_NT_SIGNATURE) {
//
// It's PE image.
//
break;
}
}
} else {
//
// DOS image header is not present, TE header is at the image base.
//
Hdr.Pe32 = (EFI_IMAGE_NT_HEADERS32 *)Pe32Data;
if ((Hdr.Te->Signature == EFI_TE_IMAGE_HEADER_SIGNATURE) &&
((Hdr.Te->Machine == IMAGE_FILE_MACHINE_I386) || Hdr.Te->Machine == IMAGE_FILE_MACHINE_X64)) {
//
// It's TE image, it TE header and Machine type match
//
break;
}
}
//
// Not found the image base, check the previous aligned address
//
Pe32Data -= AlignSize;
}
ImageContext.ImageAddress = Pe32Data;
ImageContext.PdbPointer = PeCoffLoaderGetPdbPointer ((VOID*) (UINTN) ImageContext.ImageAddress);
PeCoffLoaderRelocateImageExtraAction (&ImageContext);
}
/**
Trigger one software interrupt to debug agent to handle it.
@param[in] Signature Software interrupt signature.
**/
VOID
TriggerSoftInterrupt (
IN UINT32 Signature
)
{
UINTN Dr0;
UINTN Dr1;
//
// Save Debug Register State
//
Dr0 = AsmReadDr0 ();
Dr1 = AsmReadDr1 ();
//
// DR0 = Signature
//
AsmWriteDr0 (SOFT_INTERRUPT_SIGNATURE);
AsmWriteDr1 (Signature);
//
// Do INT3 to communicate with HOST side
//
CpuBreakpoint ();
//
// Restore Debug Register State only when Host didn't change it inside exception handler.
// Dr registers can only be changed by setting the HW breakpoint.
//
AsmWriteDr0 (Dr0);
AsmWriteDr1 (Dr1);
}
/**
Caculate Mailbox checksum and update the checksum field.
@param[in] Mailbox Debug Agent Mailbox pointer.
**/
VOID
UpdateMailboxChecksum (
IN DEBUG_AGENT_MAILBOX *Mailbox
)
{
Mailbox->CheckSum = CalculateCheckSum8 ((UINT8 *)Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2);
}
/**
Verify Mailbox checksum.
If checksum error, print debug message and run init dead loop.
@param[in] Mailbox Debug Agent Mailbox pointer.
**/
VOID
VerifyMailboxChecksum (
IN DEBUG_AGENT_MAILBOX *Mailbox
)
{
UINT8 CheckSum;
CheckSum = CalculateCheckSum8 ((UINT8 *) Mailbox, sizeof (DEBUG_AGENT_MAILBOX) - 2);
//
// The checksum updating process may be disturbed by hardware SMI, we need to check CheckSum field
// and ToBeCheckSum field to validate the mail box.
//
if (CheckSum != Mailbox->CheckSum && CheckSum != Mailbox->ToBeCheckSum) {
DEBUG ((EFI_D_ERROR, "DebugAgent: Mailbox checksum error, stack or heap crashed!\n"));
DEBUG ((EFI_D_ERROR, "DebugAgent: CheckSum = %x, Mailbox->CheckSum = %x, Mailbox->ToBeCheckSum = %x\n", CheckSum, Mailbox->CheckSum, Mailbox->ToBeCheckSum));
CpuDeadLoop ();
}
}
/**
Update Mailbox content by index.
@param[in] Mailbox Debug Agent Mailbox pointer.
@param[in] Index Mailbox content index.
@param[in] Value Value to be set into Mailbox.
**/
VOID
UpdateMailboxContent (
IN DEBUG_AGENT_MAILBOX *Mailbox,
IN UINTN Index,
IN UINT64 Value
)
{
AcquireMpSpinLock (&mDebugMpContext.MailboxSpinLock);
switch (Index) {
case DEBUG_MAILBOX_DEBUG_FLAG_INDEX:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugFlag.Uint64, sizeof(UINT64))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINT64));
Mailbox->DebugFlag.Uint64 = Value;
break;
case DEBUG_MAILBOX_DEBUG_PORT_HANDLE_INDEX:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->DebugPortHandle, sizeof(UINTN))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));
Mailbox->DebugPortHandle = (UINTN) Value;
break;
case DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->ExceptionBufferPointer, sizeof(UINTN))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINTN));
Mailbox->ExceptionBufferPointer = (UINTN) Value;
break;
case DEBUG_MAILBOX_LAST_ACK:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->LastAck, sizeof(UINT8))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
Mailbox->LastAck = (UINT8) Value;
break;
case DEBUG_MAILBOX_SEQUENCE_NO_INDEX:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->SequenceNo, sizeof(UINT8))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
Mailbox->SequenceNo = (UINT8) Value;
break;
case DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX:
Mailbox->ToBeCheckSum = Mailbox->CheckSum + CalculateSum8 ((UINT8 *)&Mailbox->HostSequenceNo, sizeof(UINT8))
- CalculateSum8 ((UINT8 *)&Value, sizeof(UINT8));
Mailbox->HostSequenceNo = (UINT8) Value;
break;
}
UpdateMailboxChecksum (Mailbox);
ReleaseMpSpinLock (&mDebugMpContext.MailboxSpinLock);
}
/**
Set debug flag in mailbox.
@param[in] FlagMask Debug flag mask value.
@param[in] FlagValue Debug flag value.
**/
VOID
SetDebugFlag (
IN UINT64 FlagMask,
IN UINT32 FlagValue
)
{
DEBUG_AGENT_MAILBOX *Mailbox;
UINT64 Data64;
Mailbox = GetMailboxPointer ();
Data64 = (Mailbox->DebugFlag.Uint64 & ~FlagMask) |
(LShiftU64 ((UINT64)FlagValue, LowBitSet64 (FlagMask)) & FlagMask);
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_DEBUG_FLAG_INDEX, Data64);
}
/**
Get debug flag in mailbox.
@param[in] FlagMask Debug flag mask value.
@return Debug flag value.
**/
UINT32
GetDebugFlag (
IN UINT64 FlagMask
)
{
DEBUG_AGENT_MAILBOX *Mailbox;
UINT32 DebugFlag;
Mailbox = GetMailboxPointer ();
DebugFlag = (UINT32) RShiftU64 (Mailbox->DebugFlag.Uint64 & FlagMask, LowBitSet64 (FlagMask));
return DebugFlag;
}
/**
Send a debug message packet to the debug port.
@param[in] Buffer The debug message.
@param[in] Length The length of debug message.
**/
VOID
SendDebugMsgPacket (
IN CHAR8 *Buffer,
IN UINTN Length
)
{
DEBUG_PACKET_HEADER DebugHeader;
DEBUG_PORT_HANDLE Handle;
Handle = GetDebugPortHandle();
DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
DebugHeader.Command = DEBUG_COMMAND_PRINT_MESSAGE;
DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER) + (UINT8) Length;
DebugHeader.SequenceNo = 0xEE;
DebugHeader.Crc = 0;
DebugHeader.Crc = CalculateCrc16 (
(UINT8 *)Buffer, Length,
CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0)
);
DebugPortWriteBuffer (Handle, (UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER));
DebugPortWriteBuffer (Handle, (UINT8 *)Buffer, Length);
}
/**
Prints a debug message to the debug port if the specified error level is enabled.
If any bit in ErrorLevel is also set in Mainbox, then print the message specified
by Format and the associated variable argument list to the debug port.
@param[in] ErrorLevel The error level of the debug message.
@param[in] Format Format string for the debug message to print.
@param[in] ... Variable argument list whose contents are accessed
based on the format string specified by Format.
**/
VOID
EFIAPI
DebugAgentMsgPrint (
IN UINT8 ErrorLevel,
IN CHAR8 *Format,
...
)
{
CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];
VA_LIST Marker;
//
// Check driver debug mask value and global mask
//
if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {
return;
}
//
// Convert the DEBUG() message to an ASCII String
//
VA_START (Marker, Format);
AsciiVSPrint (Buffer, sizeof (Buffer), Format, Marker);
VA_END (Marker);
SendDebugMsgPacket (Buffer, AsciiStrLen (Buffer));
}
/**
Prints a debug message to the debug output device if the specified error level is enabled.
If any bit in ErrorLevel is also set in DebugPrintErrorLevelLib function
GetDebugPrintErrorLevel (), then print the message specified by Format and the
associated variable argument list to the debug output device.
If Format is NULL, then ASSERT().
@param[in] ErrorLevel The error level of the debug message.
@param[in] IsSend Flag of debug message to declare that the data is being sent or being received.
@param[in] Data Variable argument list whose contents are accessed
@param[in] Length based on the format string specified by Format.
**/
VOID
EFIAPI
DebugAgentDataMsgPrint (
IN UINT8 ErrorLevel,
IN BOOLEAN IsSend,
IN UINT8 *Data,
IN UINT8 Length
)
{
CHAR8 Buffer[DEBUG_DATA_MAXIMUM_REAL_DATA];
CHAR8 *DestBuffer;
UINTN Index;
//
// Check driver debug mask value and global mask
//
if ((ErrorLevel & GetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL)) == 0) {
return;
}
DestBuffer = Buffer;
if (IsSend) {
DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Sent data [ ");
} else {
DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA, "Received data [ ");
}
Index = 0;
while (TRUE) {
if (DestBuffer - Buffer > DEBUG_DATA_MAXIMUM_REAL_DATA - 6) {
//
// If there was no enough space in buffer, send out the debug message,
// reserving 6 bytes is for the last data and end characters "]\n".
//
SendDebugMsgPacket (Buffer, DestBuffer - Buffer);
DestBuffer = Buffer;
}
DestBuffer += AsciiSPrint (DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "%02x ", Data[Index]);
Index ++;
if (Index >= Length) {
//
// The last character of debug message has been foramtted in buffer
//
DestBuffer += AsciiSPrint(DestBuffer, DEBUG_DATA_MAXIMUM_REAL_DATA - (DestBuffer - Buffer), "]\n");
SendDebugMsgPacket (Buffer, DestBuffer - Buffer);
break;
}
}
}
/**
Read remaing debug packet except for the start symbol
@param[in] Handle Pointer to Debug Port handle.
@param[in, out] DebugHeader Debug header buffer including start symbol.
@retval EFI_SUCCESS Read the symbol in BreakSymbol.
@retval EFI_CRC_ERROR CRC check fail.
@retval EFI_TIMEOUT Timeout occurs when reading debug packet.
@retval EFI_DEVICE_ERROR Receive the old or responsed packet.
**/
EFI_STATUS
ReadRemainingBreakPacket (
IN DEBUG_PORT_HANDLE Handle,
IN OUT DEBUG_PACKET_HEADER *DebugHeader
)
{
UINT16 Crc;
DEBUG_AGENT_MAILBOX *Mailbox;
//
// Has received start symbol, try to read the rest part
//
if (DebugPortReadBuffer (Handle, &DebugHeader->Command, sizeof (DEBUG_PACKET_HEADER) - 1, READ_PACKET_TIMEOUT) == 0) {
//
// Timeout occur, exit
//
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Timeout in Debug Timer interrupt\n");
return EFI_TIMEOUT;
}
Crc = DebugHeader->Crc;
DebugHeader->Crc = 0;
if (CalculateCrc16 ((UINT8 *)DebugHeader, DebugHeader->Length, 0) != Crc) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Debug Timer CRC (%x) against (%x)\n", Crc, CalculateCrc16 ((UINT8 *) &DebugHeader, DebugHeader->Length, 0));
DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *)DebugHeader, DebugHeader->Length);
return EFI_CRC_ERROR;
}
Mailbox = GetMailboxPointer();
if (((DebugHeader->Command & DEBUG_COMMAND_RESPONSE) == 0) &&
(DebugHeader->SequenceNo == (UINT8) (Mailbox->HostSequenceNo + 1))) {
//
// Only updagte HostSequenceNo for new command packet
//
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, DebugHeader->SequenceNo);
return EFI_SUCCESS;
} else {
//
// If one old command or response packet received, skip it
//
return EFI_DEVICE_ERROR;
}
}
/**
Check if HOST is attached based on Mailbox.
@retval TRUE HOST is attached.
@retval FALSE HOST is not attached.
**/
BOOLEAN
IsHostAttached (
VOID
)
{
return (BOOLEAN) (GetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED) == 1);
}
/**
Set HOST connect flag in Mailbox.
@param[in] Attached Attach status.
**/
VOID
SetHostAttached (
IN BOOLEAN Attached
)
{
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Attach status is %d\n", Attached);
SetDebugFlag (DEBUG_AGENT_FLAG_HOST_ATTACHED, (UINT32)Attached);
}
/**
Set debug setting of Debug Agent in Mailbox.
@param DebugSetting Pointer to Debug Setting defined by transfer protocol.
@retval RETURN_SUCCESS The setting is set successfully.
@retval RETURN_UNSUPPORTED The Key value is not supported.
**/
RETURN_STATUS
SetDebugSetting (
IN DEBUG_DATA_SET_DEBUG_SETTING *DebugSetting
)
{
RETURN_STATUS Status;
Status = RETURN_SUCCESS;
switch (DebugSetting->Key) {
case DEBUG_AGENT_SETTING_SMM_ENTRY_BREAK:
SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_ON_NEXT_SMI, DebugSetting->Value);
break;
case DEBUG_AGENT_SETTING_PRINT_ERROR_LEVEL:
SetDebugFlag (DEBUG_AGENT_FLAG_PRINT_ERROR_LEVEL, DebugSetting->Value);
break;
case DEBUG_AGENT_SETTING_BOOT_SCRIPT_ENTRY_BREAK:
SetDebugFlag (DEBUG_AGENT_FLAG_BREAK_BOOT_SCRIPT, DebugSetting->Value);
break;
default:
Status = RETURN_UNSUPPORTED;
}
return Status;
}
/**
Exectue GO command.
@param[in] CpuContext Pointer to saved CPU context.
**/
VOID
CommandGo (
IN DEBUG_CPU_CONTEXT *CpuContext
)
{
IA32_EFLAGS32 *Eflags;
Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;
Eflags->Bits.TF = 0;
Eflags->Bits.RF = 1;
}
/**
Exectue Stepping command.
@param[in] CpuContext Pointer to saved CPU context.
**/
VOID
CommandStepping (
IN DEBUG_CPU_CONTEXT *CpuContext
)
{
IA32_EFLAGS32 *Eflags;
Eflags = (IA32_EFLAGS32 *) &CpuContext->Eflags;
Eflags->Bits.TF = 1;
Eflags->Bits.RF = 1;
}
/**
Set debug register for hardware breakpoint.
@param[in] CpuContext Pointer to saved CPU context.
@param[in] SetHwBreakpoint Hardware breakpoint to be set.
**/
VOID
SetDebugRegister (
IN DEBUG_CPU_CONTEXT *CpuContext,
IN DEBUG_DATA_SET_HW_BREAKPOINT *SetHwBreakpoint
)
{
UINT8 RegisterIndex;
UINTN Dr7Value;
RegisterIndex = SetHwBreakpoint->Type.Index;
//
// Set debug address
//
* ((UINTN *) &CpuContext->Dr0 + RegisterIndex) = (UINTN) SetHwBreakpoint->Address;
Dr7Value = CpuContext->Dr7;
//
// Enable Gx, Lx
//
Dr7Value |= 0x3 << (RegisterIndex * 2);
//
// Set RWx and Lenx
//
Dr7Value &= ~(0xf << (16 + RegisterIndex * 4));
Dr7Value |= (UINTN) ((SetHwBreakpoint->Type.Length << 2) | SetHwBreakpoint->Type.Access) << (16 + RegisterIndex * 4);
//
// Enable GE, LE
//
Dr7Value |= 0x300;
CpuContext->Dr7 = Dr7Value;
}
/**
Clear debug register for hardware breakpoint.
@param[in] CpuContext Pointer to saved CPU context.
@param[in] ClearHwBreakpoint Hardware breakpoint to be cleared.
**/
VOID
ClearDebugRegister (
IN DEBUG_CPU_CONTEXT *CpuContext,
IN DEBUG_DATA_CLEAR_HW_BREAKPOINT *ClearHwBreakpoint
)
{
if ((ClearHwBreakpoint->IndexMask & BIT0) != 0) {
CpuContext->Dr0 = 0;
CpuContext->Dr7 &= ~(0x3 << 0);
}
if ((ClearHwBreakpoint->IndexMask & BIT1) != 0) {
CpuContext->Dr1 = 0;
CpuContext->Dr7 &= ~(0x3 << 2);
}
if ((ClearHwBreakpoint->IndexMask & BIT2) != 0) {
CpuContext->Dr2 = 0;
CpuContext->Dr7 &= ~(0x3 << 4);
}
if ((ClearHwBreakpoint->IndexMask & BIT3) != 0) {
CpuContext->Dr3 = 0;
CpuContext->Dr7 &= ~(0x3 << 6);
}
}
/**
Return the offset of FP / MMX / XMM registers in the FPU saved state by register index.
@param[in] Index Register index.
@param[out] Width Register width returned.
@return Offset in the FPU Save State.
**/
UINT16
ArchReadFxStatOffset (
IN UINT8 Index,
OUT UINT8 *Width
)
{
if (Index < SOFT_DEBUGGER_REGISTER_ST0) {
switch (Index) {
case SOFT_DEBUGGER_REGISTER_FP_FCW:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fcw);
case SOFT_DEBUGGER_REGISTER_FP_FSW:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Fsw);
case SOFT_DEBUGGER_REGISTER_FP_FTW:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ftw);
case SOFT_DEBUGGER_REGISTER_FP_OPCODE:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Opcode);
case SOFT_DEBUGGER_REGISTER_FP_EIP:
*Width = (UINT8) sizeof (UINT32);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Eip);
case SOFT_DEBUGGER_REGISTER_FP_CS:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Cs);
case SOFT_DEBUGGER_REGISTER_FP_DATAOFFSET:
*Width = (UINT8) sizeof (UINT32);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, DataOffset);
case SOFT_DEBUGGER_REGISTER_FP_DS:
*Width = (UINT8) sizeof (UINT16);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Ds);
case SOFT_DEBUGGER_REGISTER_FP_MXCSR:
*Width = (UINT8) sizeof (UINT32);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr);
case SOFT_DEBUGGER_REGISTER_FP_MXCSR_MASK:
*Width = (UINT8) sizeof (UINT32);
return OFFSET_OF(DEBUG_DATA_FX_SAVE_STATE, Mxcsr_Mask);
}
}
if (Index <= SOFT_DEBUGGER_REGISTER_ST7) {
*Width = 10;
} else if (Index <= SOFT_DEBUGGER_REGISTER_XMM15) {
*Width = 16;
} else {
//
// MMX register
//
*Width = 8;
Index -= SOFT_DEBUGGER_REGISTER_MM0 - SOFT_DEBUGGER_REGISTER_ST0;
}
return OFFSET_OF (DEBUG_DATA_FX_SAVE_STATE, St0Mm0) + (Index - SOFT_DEBUGGER_REGISTER_ST0) * 16;
}
/**
Return the pointer of the register value in the CPU saved context.
@param[in] CpuContext Pointer to saved CPU context.
@param[in] Index Register index value.
@param[out] Width Data width to read.
@return The pointer in the CPU saved context.
**/
UINT8 *
ArchReadRegisterBuffer (
IN DEBUG_CPU_CONTEXT *CpuContext,
IN UINT8 Index,
OUT UINT8 *Width
)
{
UINT8 *Buffer;
if (Index < SOFT_DEBUGGER_REGISTER_FP_BASE) {
Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, Dr0) + Index * sizeof (UINTN);
*Width = (UINT8) sizeof (UINTN);
} else {
//
// FPU/MMX/XMM registers
//
Buffer = (UINT8 *) CpuContext + OFFSET_OF (DEBUG_CPU_CONTEXT, FxSaveState) + ArchReadFxStatOffset (Index, Width);
}
return Buffer;
}
/**
Send the packet without data to HOST.
@param[in] CommandType Type of Command.
@param[in] SequenceNo Sequence number.
**/
VOID
SendPacketWithoutData (
IN UINT8 CommandType,
IN UINT8 SequenceNo
)
{
DEBUG_PACKET_HEADER DebugHeader;
DEBUG_PORT_HANDLE Handle;
Handle = GetDebugPortHandle();
DebugHeader.StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
DebugHeader.Command = CommandType;
DebugHeader.Length = sizeof (DEBUG_PACKET_HEADER);
DebugHeader.SequenceNo = SequenceNo;
DebugHeader.Crc = 0;
DebugHeader.Crc = CalculateCrc16 ((UINT8 *)&DebugHeader, sizeof (DEBUG_PACKET_HEADER), 0);
DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) &DebugHeader, DebugHeader.Length);
DebugPortWriteBuffer (Handle, (UINT8 *) &DebugHeader, DebugHeader.Length);
}
/**
Send acknowledge packet to HOST.
@param[in] AckCommand Type of Acknowledge packet.
**/
VOID
SendAckPacket (
IN UINT8 AckCommand
)
{
UINT8 SequenceNo;
DEBUG_AGENT_MAILBOX *Mailbox;
if (AckCommand != DEBUG_COMMAND_OK) {
//
// This is not ACK OK packet
//
DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Send ACK(%d)\n", AckCommand);
}
Mailbox = GetMailboxPointer();
SequenceNo = Mailbox->HostSequenceNo;
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "SendAckPacket: SequenceNo = %x\n", SequenceNo);
SendPacketWithoutData (AckCommand, SequenceNo);
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_LAST_ACK, AckCommand);
}
/**
Receive valid packet from HOST.
@param[out] InputPacket Buffer to receive packet.
@param[out] BreakReceived TRUE means break-in symbol received.
FALSE means break-in symbol not received.
@param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
TRUE: Compatible packet received.
FALSE: Incompatible packet received.
@param[in] Timeout Time out value to wait for acknowlege from HOST.
The unit is microsecond.
@param[in] SkipStartSymbol TRUE: Skip time out when reading start symbol.
FALSE: Does not Skip time out when reading start symbol.
@retval RETURN_SUCCESS A valid package was reveived in InputPacket.
@retval RETURN_TIMEOUT Timeout occurs.
**/
RETURN_STATUS
ReceivePacket (
OUT UINT8 *InputPacket,
OUT BOOLEAN *BreakReceived,
OUT BOOLEAN *IncompatibilityFlag, OPTIONAL
IN UINTN Timeout,
IN BOOLEAN SkipStartSymbol
)
{
DEBUG_PACKET_HEADER *DebugHeader;
UINTN Received;
DEBUG_PORT_HANDLE Handle;
UINT16 Crc;
UINTN TimeoutForStartSymbol;
Handle = GetDebugPortHandle();
if (SkipStartSymbol) {
TimeoutForStartSymbol = 0;
} else {
TimeoutForStartSymbol = Timeout;
}
DebugHeader = (DEBUG_PACKET_HEADER *) InputPacket;
while (TRUE) {
//
// Find the valid start symbol
//
Received = DebugPortReadBuffer (Handle, &DebugHeader->StartSymbol, sizeof (DebugHeader->StartSymbol), TimeoutForStartSymbol);
if (Received < sizeof (DebugHeader->StartSymbol)) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "DebugPortReadBuffer(StartSymbol) timeout\n");
return RETURN_TIMEOUT;
}
if (DebugHeader->StartSymbol != DEBUG_STARTING_SYMBOL_NORMAL) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Invalid start symbol received [%02x]\n", DebugHeader->StartSymbol);
continue;
}
//
// Read Package header till field Length
//
Received = DebugPortReadBuffer (
Handle,
&DebugHeader->Command,
OFFSET_OF (DEBUG_PACKET_HEADER, Length) + sizeof (DebugHeader->Length) - sizeof (DebugHeader->StartSymbol),
Timeout
);
if (Received == 0) {
DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(Command) timeout\n");
return RETURN_TIMEOUT;
}
if (DebugHeader->Length < sizeof (DEBUG_PACKET_HEADER)) {
if (IncompatibilityFlag != NULL) {
//
// This is one old version debug packet format, set Incompatibility flag
//
*IncompatibilityFlag = TRUE;
} else {
//
// Skip the bad small packet
//
continue;
}
} else {
//
// Read the payload data include the CRC field
//
Received = DebugPortReadBuffer (Handle, &DebugHeader->SequenceNo, (UINT8) (DebugHeader->Length - OFFSET_OF (DEBUG_PACKET_HEADER, SequenceNo)), Timeout);
if (Received == 0) {
DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "DebugPortReadBuffer(SequenceNo) timeout\n");
return RETURN_TIMEOUT;
}
//
// Calculate the CRC of Debug Packet
//
Crc = DebugHeader->Crc;
DebugHeader->Crc = 0;
if (Crc == CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0)) {
break;
}
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "CRC Error (received CRC is %x)\n", Crc);
DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);
}
}
DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, FALSE, (UINT8 *) DebugHeader, DebugHeader->Length);
return RETURN_SUCCESS;
}
/**
Receive acknowledge packet OK from HOST in specified time.
@param[in] Command The command type issued by TARGET.
@param[in] Timeout Time out value to wait for acknowlege from HOST.
The unit is microsecond.
@param[out] BreakReceived If BreakReceived is not NULL,
TRUE is retured if break-in symbol received.
FALSE is retured if break-in symbol not received.
@param[out] IncompatibilityFlag If IncompatibilityFlag is not NULL, return
TRUE: Compatible packet received.
FALSE: Incompatible packet received.
@retval RETRUEN_SUCCESS Succeed to receive acknowlege packet from HOST,
the type of acknowlege packet saved in Ack.
@retval RETURN_TIMEOUT Specified timeout value was up.
**/
RETURN_STATUS
SendCommandAndWaitForAckOK (
IN UINT8 Command,
IN UINTN Timeout,
OUT BOOLEAN *BreakReceived, OPTIONAL
OUT BOOLEAN *IncompatibilityFlag OPTIONAL
)
{
RETURN_STATUS Status;
UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
DEBUG_PACKET_HEADER *DebugHeader;
UINT8 SequenceNo;
UINT8 HostSequenceNo;
UINT8 RetryCount;
RetryCount = 3;
DebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;
Status = RETURN_TIMEOUT;
while (RetryCount > 0) {
SequenceNo = GetMailboxPointer()->SequenceNo;
HostSequenceNo = GetMailboxPointer()->HostSequenceNo;
SendPacketWithoutData (Command, SequenceNo);
Status = ReceivePacket ((UINT8 *) DebugHeader, BreakReceived, IncompatibilityFlag, Timeout, FALSE);
if (Status == RETURN_TIMEOUT) {
if (Command == DEBUG_COMMAND_INIT_BREAK) {
RetryCount--;
} else {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout when waiting for ACK packet.\n");
}
continue;
}
ASSERT_EFI_ERROR (Status);
//
// Status == RETURN_SUCCESS
//
if (DebugHeader->Command == DEBUG_COMMAND_OK && DebugHeader->SequenceNo == SequenceNo) {
//
// Received Ack OK
//
UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_SEQUENCE_NO_INDEX, ++SequenceNo);
return Status;
}
if (DebugHeader->Command == DEBUG_COMMAND_GO && (DebugHeader->SequenceNo == HostSequenceNo || Command == DEBUG_COMMAND_INIT_BREAK)) {
//
// Received Old GO
//
if (Command == DEBUG_COMMAND_INIT_BREAK) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive GO() in last boot\n");
}
SendPacketWithoutData (DEBUG_COMMAND_OK, DebugHeader->SequenceNo);
}
}
ASSERT (Command == DEBUG_COMMAND_INIT_BREAK);
return Status;
}
/**
Get current break cause.
@param[in] Vector Vector value of exception or interrupt.
@param[in] CpuContext Pointer to save CPU context.
@return The type of break cause defined by XXXX
**/
UINT8
GetBreakCause (
IN UINTN Vector,
IN DEBUG_CPU_CONTEXT *CpuContext
)
{
UINT8 Cause;
Cause = DEBUG_DATA_BREAK_CAUSE_UNKNOWN;
switch (Vector) {
case DEBUG_INT1_VECTOR:
case DEBUG_INT3_VECTOR:
if (Vector == DEBUG_INT1_VECTOR) {
//
// INT 1
//
if ((CpuContext->Dr6 & BIT14) != 0) {
Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;
//
// If it's single step, no need to check DR0, to ensure single step work in PeCoffExtraActionLib
// (right after triggering a breakpoint to report image load/unload).
//
return Cause;
} else {
Cause = DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT;
}
} else {
//
// INT 3
//
Cause = DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT;
}
switch (CpuContext->Dr0) {
case IMAGE_LOAD_SIGNATURE:
case IMAGE_UNLOAD_SIGNATURE:
if (CpuContext->Dr3 == IO_PORT_BREAKPOINT_ADDRESS) {
Cause = (UINT8) ((CpuContext->Dr0 == IMAGE_LOAD_SIGNATURE) ?
DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD : DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD);
}
break;
case SOFT_INTERRUPT_SIGNATURE:
if (CpuContext->Dr1 == MEMORY_READY_SIGNATURE) {
Cause = DEBUG_DATA_BREAK_CAUSE_MEMORY_READY;
CpuContext->Dr0 = 0;
} else if (CpuContext->Dr1 == SYSTEM_RESET_SIGNATURE) {
Cause = DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET;
CpuContext->Dr0 = 0;
}
break;
default:
break;
}
break;
case DEBUG_TIMER_VECTOR:
Cause = DEBUG_DATA_BREAK_CAUSE_USER_HALT;
break;
default:
if (Vector < 20) {
if (GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) == 1) {
//
// If stepping command is executing
//
Cause = DEBUG_DATA_BREAK_CAUSE_STEPPING;
} else {
Cause = DEBUG_DATA_BREAK_CAUSE_EXCEPTION;
}
}
break;
}
return Cause;
}
/**
Copy memory from source to destination with specified width.
@param[out] Dest A pointer to the destination buffer of the memory copy.
@param[in] Src A pointer to the source buffer of the memory copy.
@param[in] Count The number of data with specified width to copy from source to destination.
@param[in] Width Data width in byte.
**/
VOID
CopyMemByWidth (
OUT UINT8 *Dest,
IN UINT8 *Src,
IN UINT16 Count,
IN UINT8 Width
)
{
UINT8 *Destination;
UINT8 *Source;
INT8 Step;
if (Src > Dest) {
Destination = Dest;
Source = Src;
Step = Width;
} else {
//
// Copy memory from tail to avoid memory overlap
//
Destination = Dest + (Count - 1) * Width;
Source = Src + (Count - 1) * Width;
Step = -Width;
}
while (Count-- != 0) {
switch (Width) {
case 1:
*(UINT8 *) Destination = MmioRead8 ((UINTN) Source);
break;
case 2:
*(UINT16 *) Destination = MmioRead16 ((UINTN) Source);
break;
case 4:
*(UINT32 *) Destination = MmioRead32 ((UINTN) Source);
break;
case 8:
*(UINT64 *) Destination = MmioRead64 ((UINTN) Source);
break;
default:
ASSERT (FALSE);
}
Source += Step;
Destination += Step;
}
}
/**
Read memory with speicifed width and send packet with response data to HOST.
@param[in] Data Pointer to response data buffer.
@param[in] Count The number of data with specified Width.
@param[in] Width Data width in byte.
@retval RETURN_SUCCESS Response data was sent successfully.
**/
RETURN_STATUS
ReadMemoryAndSendResponsePacket (
IN UINT8 *Data,
IN UINT16 Count,
IN UINT8 Width
)
{
RETURN_STATUS Status;
DEBUG_PACKET_HEADER *DebugHeader;
BOOLEAN LastPacket;
DEBUG_PACKET_HEADER *AckDebugHeader;
UINT8 DebugPacket[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];
UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT];
DEBUG_PORT_HANDLE Handle;
UINT8 SequenceNo;
UINTN RemainingDataSize;
UINTN CurrentDataSize;
Handle = GetDebugPortHandle();
//
// Data is appended end of Debug Packet header, make sure data address
// in Debug Packet 8-byte alignment always
//
DebugHeader = (DEBUG_PACKET_HEADER *) (ALIGN_VALUE ((UINTN)&DebugPacket + sizeof (DEBUG_PACKET_HEADER), sizeof (UINT64))
- sizeof (DEBUG_PACKET_HEADER));
DebugHeader->StartSymbol = DEBUG_STARTING_SYMBOL_NORMAL;
RemainingDataSize = Count * Width;
while (TRUE) {
SequenceNo = GetMailboxPointer()->HostSequenceNo;
if (RemainingDataSize <= DEBUG_DATA_MAXIMUM_REAL_DATA) {
//
// If the remaining data is less one real packet size, this is the last data packet
//
CurrentDataSize = RemainingDataSize;
LastPacket = TRUE;
DebugHeader->Command = DEBUG_COMMAND_OK;
} else {
//
// Data is too larger to be sent in one packet, calculate the actual data size could
// be sent in one Maximum data packet
//
CurrentDataSize = (DEBUG_DATA_MAXIMUM_REAL_DATA / Width) * Width;
LastPacket = FALSE;
DebugHeader->Command = DEBUG_COMMAND_IN_PROGRESS;
}
//
// Construct the rest Debug header
//
DebugHeader->Length = (UINT8)(CurrentDataSize + sizeof (DEBUG_PACKET_HEADER));
DebugHeader->SequenceNo = SequenceNo;
DebugHeader->Crc = 0;
CopyMemByWidth ((UINT8 *)(DebugHeader + 1), Data, (UINT16) CurrentDataSize / Width, Width);
//
// Calculate and fill the checksum, DebugHeader->Crc should be 0 before invoking CalculateCrc16 ()
//
DebugHeader->Crc = CalculateCrc16 ((UINT8 *) DebugHeader, DebugHeader->Length, 0);
DebugAgentDataMsgPrint (DEBUG_AGENT_VERBOSE, TRUE, (UINT8 *) DebugHeader, DebugHeader->Length);
DebugPortWriteBuffer (Handle, (UINT8 *) DebugHeader, DebugHeader->Length);
while (TRUE) {
Status = ReceivePacket (InputPacketBuffer, NULL, NULL, READ_PACKET_TIMEOUT, FALSE);
if (Status == RETURN_TIMEOUT) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Timeout in SendDataResponsePacket()\n");
break;
}
AckDebugHeader = (DEBUG_PACKET_HEADER *) InputPacketBuffer;
SequenceNo = AckDebugHeader->SequenceNo;
if (AckDebugHeader->Command == DEBUG_COMMAND_OK &&
SequenceNo == DebugHeader->SequenceNo &&
LastPacket) {
//
// If this is the last packet, return RETURN_SUCCESS.
//
return RETURN_SUCCESS;
}
if ((SequenceNo == (UINT8) (DebugHeader->SequenceNo + 1)) && (AckDebugHeader->Command == DEBUG_COMMAND_CONTINUE)) {
//
// Calculate the rest data size
//
Data += CurrentDataSize;
RemainingDataSize -= CurrentDataSize;
UpdateMailboxContent (GetMailboxPointer(), DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) SequenceNo);
break;
}
if (SequenceNo >= DebugHeader->SequenceNo) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Received one old or new command(SequenceNo is %x, last SequenceNo is %x)\n", SequenceNo, DebugHeader->SequenceNo);
break;
}
}
}
}
/**
Send packet with response data to HOST.
@param[in] Data Pointer to response data buffer.
@param[in] DataSize Size of response data in byte.
@retval RETURN_SUCCESS Response data was sent successfully.
**/
RETURN_STATUS
SendDataResponsePacket (
IN UINT8 *Data,
IN UINT16 DataSize
)
{
return ReadMemoryAndSendResponsePacket (Data, DataSize, 1);
}
/**
Send break cause packet to HOST.
@param[in] Vector Vector value of exception or interrutp.
@param[in] CpuContext Pointer to save CPU context.
@retval RETURN_SUCCESS Response data was sent successfully.
@retval RETURN_DEVICE_ERROR Cannot receive DEBUG_COMMAND_OK from HOST.
**/
RETURN_STATUS
SendBreakCausePacket (
IN UINTN Vector,
IN DEBUG_CPU_CONTEXT *CpuContext
)
{
DEBUG_DATA_RESPONSE_BREAK_CAUSE DebugDataBreakCause;
DebugDataBreakCause.StopAddress = CpuContext->Eip;
DebugDataBreakCause.Cause = GetBreakCause (Vector, CpuContext);
return SendDataResponsePacket ((UINT8 *) &DebugDataBreakCause, (UINT16) sizeof (DEBUG_DATA_RESPONSE_BREAK_CAUSE));
}
/**
Try to attach the HOST.
Send init break packet to HOST:
If no acknowlege received in specified Timeout, return RETURN_TIMEOUT.
If received acknowlege, check the revision of HOST.
Set Attach Flag if attach successfully.
@param[in] BreakCause Break cause of this break event.
@param[in] Timeout Time out value to wait for acknowlege from HOST.
The unit is microsecond.
@param[out] BreakReceived If BreakReceived is not NULL,
TRUE is retured if break-in symbol received.
FALSE is retured if break-in symbol not received.
**/
RETURN_STATUS
AttachHost (
IN UINT8 BreakCause,
IN UINTN Timeout,
OUT BOOLEAN *BreakReceived
)
{
RETURN_STATUS Status;
DEBUG_PORT_HANDLE Handle;
BOOLEAN IncompatibilityFlag;
IncompatibilityFlag = FALSE;
Handle = GetDebugPortHandle();
//
// Send init break and wait ack in Timeout
//
DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgSendInitPacket, AsciiStrLen (mErrorMsgSendInitPacket));
if (BreakCause == DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET) {
Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_INIT_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
} else {
Status = SendCommandAndWaitForAckOK (DEBUG_COMMAND_ATTACH_BREAK, Timeout, BreakReceived, &IncompatibilityFlag);
}
if (IncompatibilityFlag) {
//
// If the incompatible Debug Packet received, the HOST should be running transfer protocol before DEBUG_AGENT_REVISION.
// It could be UDK Debugger for Windows v1.1/v1.2 or for Linux v0.8/v1.2.
//
DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgVersionAlert, AsciiStrLen (mErrorMsgVersionAlert));
CpuDeadLoop ();
}
if (RETURN_ERROR (Status)) {
DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectFail, AsciiStrLen (mErrorMsgConnectFail));
} else {
DebugPortWriteBuffer (Handle, (UINT8 *) mErrorMsgConnectOK, AsciiStrLen (mErrorMsgConnectOK));
//
// Set Attach flag
//
SetHostAttached (TRUE);
}
return Status;
}
/**
Send Break point packet to HOST.
Only the first breaking processor could sent BREAK_POINT packet.
@param[in] BreakCause Break cause of this break event.
@param[in] ProcessorIndex Processor index value.
@param[out] BreakReceived If BreakReceived is not NULL,
TRUE is retured if break-in symbol received.
FALSE is retured if break-in symbol not received.
**/
VOID
SendBreakPacketToHost (
IN UINT8 BreakCause,
IN UINT32 ProcessorIndex,
OUT BOOLEAN *BreakReceived
)
{
UINT8 InputCharacter;
DEBUG_PORT_HANDLE Handle;
Handle = GetDebugPortHandle();
if (IsHostAttached ()) {
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Send Break Packet to HOST.\n", ProcessorIndex);
SendCommandAndWaitForAckOK (DEBUG_COMMAND_BREAK_POINT, READ_PACKET_TIMEOUT, BreakReceived, NULL);
} else {
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "processor[%x]:Try to attach HOST.\n", ProcessorIndex);
//
// If HOST is not attached, try to attach it firstly.
//
//
// Poll Attach symbols from HOST and ack OK
//
do {
DebugPortReadBuffer (Handle, &InputCharacter, 1, 0);
} while (InputCharacter != DEBUG_STARTING_SYMBOL_ATTACH);
SendAckPacket (DEBUG_COMMAND_OK);
//
// Try to attach HOST
//
while (AttachHost (BreakCause, 0, NULL) != RETURN_SUCCESS);
}
}
/**
The main function to process communication with HOST.
It received the command packet from HOST, and sent response data packet to HOST.
@param[in] Vector Vector value of exception or interrutp.
@param[in, out] CpuContext Pointer to saved CPU context.
@param[in] BreakReceived TRUE means break-in symbol received.
FALSE means break-in symbol not received.
**/
VOID
CommandCommunication (
IN UINTN Vector,
IN OUT DEBUG_CPU_CONTEXT *CpuContext,
IN BOOLEAN BreakReceived
)
{
RETURN_STATUS Status;
UINT8 InputPacketBuffer[DEBUG_DATA_UPPER_LIMIT + sizeof (UINT64) - 1];
DEBUG_PACKET_HEADER *DebugHeader;
UINT8 Width;
UINT8 Data8;
UINT32 Data32;
UINT64 Data64;
DEBUG_DATA_READ_MEMORY *MemoryRead;
DEBUG_DATA_WRITE_MEMORY *MemoryWrite;
DEBUG_DATA_READ_IO *IoRead;
DEBUG_DATA_WRITE_IO *IoWrite;
DEBUG_DATA_READ_REGISTER *RegisterRead;
DEBUG_DATA_WRITE_REGISTER *RegisterWrite;
UINT8 *RegisterBuffer;
DEBUG_DATA_READ_MSR *MsrRegisterRead;
DEBUG_DATA_WRITE_MSR *MsrRegisterWrite;
DEBUG_DATA_CPUID *Cpuid;
DEBUG_DATA_RESPONSE_CPUID CpuidResponse;
DEBUG_DATA_SEARCH_SIGNATURE *SearchSignature;
DEBUG_DATA_RESPONSE_GET_EXCEPTION Exception;
DEBUG_DATA_RESPONSE_GET_REVISION DebugAgentRevision;
DEBUG_DATA_SET_VIEWPOINT *SetViewPoint;
BOOLEAN HaltDeferred;
UINT32 ProcessorIndex;
DEBUG_PORT_HANDLE Handle;
DEBUG_AGENT_EXCEPTION_BUFFER AgentExceptionBuffer;
UINT32 IssuedViewPoint;
DEBUG_AGENT_MAILBOX *Mailbox;
UINT8 *AlignedDataPtr;
ProcessorIndex = 0;
IssuedViewPoint = 0;
HaltDeferred = BreakReceived;
if (MultiProcessorDebugSupport()) {
ProcessorIndex = GetProcessorIndex ();
SetCpuStopFlagByIndex (ProcessorIndex, TRUE);
if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
//
// Only the current view processor could set AgentInProgress Flag.
//
IssuedViewPoint = ProcessorIndex;
}
}
if (IssuedViewPoint == ProcessorIndex) {
//
// Set AgentInProgress Flag.
//
SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 1);
}
Handle = GetDebugPortHandle();
while (TRUE) {
if (MultiProcessorDebugSupport()) {
//
// Check if the current processor is HOST view point
//
if (mDebugMpContext.ViewPointIndex != ProcessorIndex) {
if (mDebugMpContext.RunCommandSet) {
//
// If HOST view point sets RUN flag, run GO command to leave
//
SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
CommandGo (CpuContext);
break;
} else {
//
// Run into loop again
//
CpuPause ();
continue;
}
}
}
AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
DebugHeader =(DEBUG_PACKET_HEADER *) InputPacketBuffer;
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "TARGET: Try to get command from HOST...\n");
Status = ReceivePacket ((UINT8 *)DebugHeader, &BreakReceived, NULL, READ_PACKET_TIMEOUT, TRUE);
if (Status != RETURN_SUCCESS || (DebugHeader->Command & DEBUG_COMMAND_RESPONSE) != 0) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command[%x] sequenceno[%x] returned status is [%x] \n", DebugHeader->Command, DebugHeader->SequenceNo, Status);
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Get command failed or it's response packet not expected! \n");
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
continue;
}
Mailbox = GetMailboxPointer ();
if (DebugHeader->SequenceNo == Mailbox->HostSequenceNo) {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "TARGET: Receive one old command[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
SendAckPacket (Mailbox->LastAck);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
continue;
} else if (DebugHeader->SequenceNo == (UINT8) (Mailbox->HostSequenceNo + 1)) {
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, (UINT8) DebugHeader->SequenceNo);
} else {
DebugAgentMsgPrint (DEBUG_AGENT_WARNING, "Receive one invalid comamnd[%x] agaist command[%x]\n", DebugHeader->SequenceNo, Mailbox->HostSequenceNo);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
continue;
}
//
// Save CPU content before executing HOST commond
//
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_EXCEPTION_BUFFER_POINTER_INDEX, (UINT64)(UINTN) &AgentExceptionBuffer.JumpBuffer);
if (SetJump (&AgentExceptionBuffer.JumpBuffer) != 0) {
//
// If HOST command failed, continue to wait for HOST's next command
// If needed, agent could send exception info to HOST.
//
SendAckPacket (DEBUG_COMMAND_ABORT);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
continue;
}
DebugAgentMsgPrint (DEBUG_AGENT_INFO, "Processor[%x]:Received one command(%x)\n", mDebugMpContext.ViewPointIndex, DebugHeader->Command);
switch (DebugHeader->Command) {
case DEBUG_COMMAND_HALT:
SendAckPacket (DEBUG_COMMAND_HALT_DEFERRED);
HaltDeferred = TRUE;
BreakReceived = FALSE;
Status = RETURN_SUCCESS;
break;
case DEBUG_COMMAND_RESET:
SendAckPacket (DEBUG_COMMAND_OK);
SendAckPacket (DEBUG_COMMAND_OK);
SendAckPacket (DEBUG_COMMAND_OK);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
ResetCold ();
//
// Assume system resets in 2 seconds, otherwise send TIMEOUT packet.
// PCD can be used if 2 seconds isn't long enough for some platforms.
//
MicroSecondDelay (2000000);
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, Mailbox->HostSequenceNo + 1);
SendAckPacket (DEBUG_COMMAND_TIMEOUT);
SendAckPacket (DEBUG_COMMAND_TIMEOUT);
SendAckPacket (DEBUG_COMMAND_TIMEOUT);
break;
case DEBUG_COMMAND_GO:
CommandGo (CpuContext);
//
// Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
// If HOST changed Dr0 before GO, we will not change Dr0 here
//
Data8 = GetBreakCause (Vector, CpuContext);
if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
CpuContext->Dr0 = 0;
}
//
// Clear Stepping Flag
//
SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 0);
if (!HaltDeferred) {
//
// If no HALT command received when being in-active mode
//
if (MultiProcessorDebugSupport()) {
Data32 = FindNextPendingBreakCpu ();
if (Data32 != -1) {
//
// If there are still others processors being in break state,
// send OK packet to HOST to finish this go command
//
SendAckPacket (DEBUG_COMMAND_OK);
CpuPause ();
//
// Set current view to the next breaking processor
//
mDebugMpContext.ViewPointIndex = Data32;
mDebugMpContext.BreakAtCpuIndex = mDebugMpContext.ViewPointIndex;
SetCpuBreakFlagByIndex (mDebugMpContext.ViewPointIndex, FALSE);
//
// Send break packet to HOST to let HOST break again
//
SendBreakPacketToHost (DEBUG_DATA_BREAK_CAUSE_UNKNOWN, mDebugMpContext.BreakAtCpuIndex, &BreakReceived);
//
// Continue to run into loop to read command packet from HOST
//
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
break;
}
//
// If no else processor break, set stop bitmask,
// and set Running flag for all processors.
//
SetCpuStopFlagByIndex (ProcessorIndex, FALSE);
SetCpuRunningFlag (TRUE);
CpuPause ();
//
// Wait for all processors are in running state
//
while (TRUE) {
if (IsAllCpuRunning ()) {
break;
}
}
//
// Set BSP to be current view point.
//
SetDebugViewPoint (mDebugMpContext.BspIndex);
CpuPause ();
//
// Clear breaking processor index and running flag
//
mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
SetCpuRunningFlag (FALSE);
}
//
// Send OK packet to HOST to finish this go command
//
SendAckPacket (DEBUG_COMMAND_OK);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
if (!IsHostAttached()) {
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_SEQUENCE_NO_INDEX, 0);
UpdateMailboxContent (Mailbox, DEBUG_MAILBOX_HOST_SEQUENCE_NO_INDEX, 0);
}
return;
} else {
//
// If reveived HALT command, need to defer the GO command
//
SendAckPacket (DEBUG_COMMAND_HALT_PROCESSED);
HaltDeferred = FALSE;
Vector = DEBUG_TIMER_VECTOR;
}
break;
case DEBUG_COMMAND_BREAK_CAUSE:
if (MultiProcessorDebugSupport() && ProcessorIndex != mDebugMpContext.BreakAtCpuIndex) {
Status = SendBreakCausePacket (DEBUG_TIMER_VECTOR, CpuContext);
} else {
Status = SendBreakCausePacket (Vector, CpuContext);
}
break;
case DEBUG_COMMAND_SET_HW_BREAKPOINT:
SetDebugRegister (CpuContext, (DEBUG_DATA_SET_HW_BREAKPOINT *) (DebugHeader + 1));
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_CLEAR_HW_BREAKPOINT:
ClearDebugRegister (CpuContext, (DEBUG_DATA_CLEAR_HW_BREAKPOINT *) (DebugHeader + 1));
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_SINGLE_STEPPING:
CommandStepping (CpuContext);
//
// Clear Dr0 to avoid to be recognized as IMAGE_LOAD/_UNLOAD again when hitting a breakpoint after GO
// If HOST changed Dr0 before GO, we will not change Dr0 here
//
Data8 = GetBreakCause (Vector, CpuContext);
if (Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD || Data8 == DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD) {
CpuContext->Dr0 = 0;
}
mDebugMpContext.BreakAtCpuIndex = (UINT32) (-1);
//
// Set Stepping Flag
//
SetDebugFlag (DEBUG_AGENT_FLAG_STEPPING, 1);
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
//
// Executing stepping command directly without sending ACK packet,
// ACK packet will be sent after stepping done.
//
return;
case DEBUG_COMMAND_SET_SW_BREAKPOINT:
Data64 = (UINTN) (((DEBUG_DATA_SET_SW_BREAKPOINT *) (DebugHeader + 1))->Address);
Data8 = *(UINT8 *) (UINTN) Data64;
*(UINT8 *) (UINTN) Data64 = DEBUG_SW_BREAKPOINT_SYMBOL;
Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
break;
case DEBUG_COMMAND_READ_MEMORY:
MemoryRead = (DEBUG_DATA_READ_MEMORY *) (DebugHeader + 1);
Status = ReadMemoryAndSendResponsePacket ((UINT8 *) (UINTN) MemoryRead->Address, MemoryRead->Count, MemoryRead->Width);
break;
case DEBUG_COMMAND_WRITE_MEMORY:
MemoryWrite = (DEBUG_DATA_WRITE_MEMORY *) (DebugHeader + 1);
//
// Copy data into one memory with 8-byte alignment address
//
AlignedDataPtr = ALIGN_POINTER ((UINT8 *) &MemoryWrite->Data, sizeof (UINT64));
if (AlignedDataPtr != (UINT8 *) &MemoryWrite->Data) {
CopyMem (AlignedDataPtr, (UINT8 *) &MemoryWrite->Data, MemoryWrite->Count * MemoryWrite->Width);
}
CopyMemByWidth ((UINT8 *) (UINTN) MemoryWrite->Address, AlignedDataPtr, MemoryWrite->Count, MemoryWrite->Width);
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_READ_IO:
IoRead = (DEBUG_DATA_READ_IO *) (DebugHeader + 1);
switch (IoRead->Width) {
case 1:
Data64 = IoRead8 ((UINTN) IoRead->Port);
break;
case 2:
Data64 = IoRead16 ((UINTN) IoRead->Port);
break;
case 4:
Data64 = IoRead32 ((UINTN) IoRead->Port);
break;
case 8:
Data64 = IoRead64 ((UINTN) IoRead->Port);
break;
default:
Data64 = (UINT64) -1;
}
Status = SendDataResponsePacket ((UINT8 *) &Data64, IoRead->Width);
break;
case DEBUG_COMMAND_WRITE_IO:
IoWrite = (DEBUG_DATA_WRITE_IO *) (DebugHeader + 1);
switch (IoWrite->Width) {
case 1:
Data64 = IoWrite8 ((UINTN) IoWrite->Port, *(UINT8 *) &IoWrite->Data);
break;
case 2:
Data64 = IoWrite16 ((UINTN) IoWrite->Port, *(UINT16 *) &IoWrite->Data);
break;
case 4:
Data64 = IoWrite32 ((UINTN) IoWrite->Port, *(UINT32 *) &IoWrite->Data);
break;
case 8:
Data64 = IoWrite64 ((UINTN) IoWrite->Port, *(UINT64 *) &IoWrite->Data);
break;
default:
Data64 = (UINT64) -1;
}
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_READ_ALL_REGISTERS:
Status = SendDataResponsePacket ((UINT8 *) CpuContext, sizeof (*CpuContext));
break;
case DEBUG_COMMAND_READ_REGISTER:
RegisterRead = (DEBUG_DATA_READ_REGISTER *) (DebugHeader + 1);
if (RegisterRead->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterRead->Index, &Width);
Status = SendDataResponsePacket (RegisterBuffer, Width);
} else {
Status = RETURN_UNSUPPORTED;
}
break;
case DEBUG_COMMAND_WRITE_REGISTER:
RegisterWrite = (DEBUG_DATA_WRITE_REGISTER *) (DebugHeader + 1);
if (RegisterWrite->Index <= SOFT_DEBUGGER_REGISTER_MAX) {
RegisterBuffer = ArchReadRegisterBuffer (CpuContext, RegisterWrite->Index, &Width);
ASSERT (Width == RegisterWrite->Length);
CopyMem (RegisterBuffer, RegisterWrite->Data, Width);
SendAckPacket (DEBUG_COMMAND_OK);
} else {
Status = RETURN_UNSUPPORTED;
}
break;
case DEBUG_COMMAND_ARCH_MODE:
Data8 = DEBUG_ARCH_SYMBOL;
Status = SendDataResponsePacket ((UINT8 *) &Data8, (UINT16) sizeof (UINT8));
break;
case DEBUG_COMMAND_READ_MSR:
MsrRegisterRead = (DEBUG_DATA_READ_MSR *) (DebugHeader + 1);
Data64 = AsmReadMsr64 (MsrRegisterRead->Index);
Status = SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (UINT64));
break;
case DEBUG_COMMAND_WRITE_MSR:
MsrRegisterWrite = (DEBUG_DATA_WRITE_MSR *) (DebugHeader + 1);
AsmWriteMsr64 (MsrRegisterWrite->Index, MsrRegisterWrite->Value);
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_SET_DEBUG_SETTING:
Status = SetDebugSetting ((DEBUG_DATA_SET_DEBUG_SETTING *)(DebugHeader + 1));
if (Status == RETURN_SUCCESS) {
SendAckPacket (DEBUG_COMMAND_OK);
}
break;
case DEBUG_COMMAND_GET_REVISION:
DebugAgentRevision.Revision = DEBUG_AGENT_REVISION;
DebugAgentRevision.Capabilities = DEBUG_AGENT_CAPABILITIES;
Status = SendDataResponsePacket ((UINT8 *) &DebugAgentRevision, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_REVISION));
break;
case DEBUG_COMMAND_GET_EXCEPTION:
Exception.ExceptionNum = (UINT8) Vector;
Exception.ExceptionData = (UINT32) CpuContext->ExceptionData;
Status = SendDataResponsePacket ((UINT8 *) &Exception, (UINT16) sizeof (DEBUG_DATA_RESPONSE_GET_EXCEPTION));
break;
case DEBUG_COMMAND_SET_VIEWPOINT:
SetViewPoint = (DEBUG_DATA_SET_VIEWPOINT *) (DebugHeader + 1);
if (MultiProcessorDebugSupport()) {
if (IsCpuStopped (SetViewPoint->ViewPoint)) {
SetDebugViewPoint (SetViewPoint->ViewPoint);
SendAckPacket (DEBUG_COMMAND_OK);
} else {
//
// If CPU is not halted
//
SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
}
} else if (SetViewPoint->ViewPoint == 0) {
SendAckPacket (DEBUG_COMMAND_OK);
} else {
SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
}
break;
case DEBUG_COMMAND_GET_VIEWPOINT:
Data32 = mDebugMpContext.ViewPointIndex;
SendDataResponsePacket((UINT8 *) &Data32, (UINT16) sizeof (UINT32));
break;
case DEBUG_COMMAND_MEMORY_READY:
Data8 = (UINT8) GetDebugFlag (DEBUG_AGENT_FLAG_MEMORY_READY);
SendDataResponsePacket (&Data8, (UINT16) sizeof (UINT8));
break;
case DEBUG_COMMAND_DETACH:
SetHostAttached (FALSE);
SendAckPacket (DEBUG_COMMAND_OK);
break;
case DEBUG_COMMAND_CPUID:
Cpuid = (DEBUG_DATA_CPUID *) (DebugHeader + 1);
AsmCpuidEx (
Cpuid->Eax, Cpuid->Ecx,
&CpuidResponse.Eax, &CpuidResponse.Ebx,
&CpuidResponse.Ecx, &CpuidResponse.Edx
);
SendDataResponsePacket ((UINT8 *) &CpuidResponse, (UINT16) sizeof (CpuidResponse));
break;
case DEBUG_COMMAND_SEARCH_SIGNATURE:
SearchSignature = (DEBUG_DATA_SEARCH_SIGNATURE *) (DebugHeader + 1);
if ((SearchSignature->Alignment != 0) &&
(SearchSignature->Alignment == GetPowerOfTwo32 (SearchSignature->Alignment))
) {
if (SearchSignature->Positive) {
for (
Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start, SearchSignature->Alignment);
Data64 <= SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength;
Data64 += SearchSignature->Alignment
) {
if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
break;
}
}
if (Data64 > SearchSignature->Start + SearchSignature->Count - SearchSignature->DataLength) {
Data64 = (UINT64) -1;
}
} else {
for (
Data64 = ALIGN_VALUE ((UINTN) SearchSignature->Start - SearchSignature->Alignment, SearchSignature->Alignment);
Data64 >= SearchSignature->Start - SearchSignature->Count;
Data64 -= SearchSignature->Alignment
) {
if (CompareMem ((VOID *) (UINTN) Data64, &SearchSignature->Data, SearchSignature->DataLength) == 0) {
break;
}
}
if (Data64 < SearchSignature->Start - SearchSignature->Count) {
Data64 = (UINT64) -1;
}
}
SendDataResponsePacket ((UINT8 *) &Data64, (UINT16) sizeof (Data64));
} else {
Status = RETURN_UNSUPPORTED;
}
break;
default:
SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
break;
}
if (Status == RETURN_UNSUPPORTED) {
SendAckPacket (DEBUG_COMMAND_NOT_SUPPORTED);
} else if (Status != RETURN_SUCCESS) {
SendAckPacket (DEBUG_COMMAND_ABORT);
}
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
CpuPause ();
}
}
/**
C function called in interrupt handler.
@param[in] Vector Vector value of exception or interrutp.
@param[in] CpuContext Pointer to save CPU context.
**/
VOID
EFIAPI
InterruptProcess (
IN UINT32 Vector,
IN DEBUG_CPU_CONTEXT *CpuContext
)
{
UINT8 InputCharacter;
UINT8 BreakCause;
UINTN SavedEip;
BOOLEAN BreakReceived;
UINT32 ProcessorIndex;
UINT32 CurrentDebugTimerInitCount;
DEBUG_PORT_HANDLE Handle;
UINT8 Data8;
UINT8 *Al;
UINT32 IssuedViewPoint;
DEBUG_AGENT_EXCEPTION_BUFFER *ExceptionBuffer;
ProcessorIndex = 0;
IssuedViewPoint = 0;
BreakReceived = FALSE;
if (mSkipBreakpoint) {
//
// If Skip Breakpoint flag is set, means communication is disturbed by hardware SMI, we need to ignore the break points in SMM
//
if ((Vector == DEBUG_INT1_VECTOR) || (Vector == DEBUG_INT3_VECTOR)) {
DebugPortWriteBuffer (GetDebugPortHandle(), (UINT8 *) mWarningMsgIngoreBreakpoint, AsciiStrLen (mWarningMsgIngoreBreakpoint));
return;
}
}
if (MultiProcessorDebugSupport()) {
ProcessorIndex = GetProcessorIndex ();
//
// If this processor has alreay halted before, need to check it later
//
if (IsCpuStopped (ProcessorIndex)) {
IssuedViewPoint = ProcessorIndex;
}
}
if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
//
// Check if this exception is issued by Debug Agent itself
// If yes, fill the debug agent exception buffer and LongJump() back to
// the saved CPU content in CommandCommunication()
//
if (GetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS) == 1) {
DebugAgentMsgPrint (DEBUG_AGENT_ERROR, "Debug agent meet one Exception, ExceptionNum is %d, EIP = 0x%x.\n", Vector, (UINTN)CpuContext->Eip);
ExceptionBuffer = (DEBUG_AGENT_EXCEPTION_BUFFER *) (UINTN) GetMailboxPointer()->ExceptionBufferPointer;
ExceptionBuffer->ExceptionContent.ExceptionNum = (UINT8) Vector;
ExceptionBuffer->ExceptionContent.ExceptionData = (UINT32) CpuContext->ExceptionData;
LongJump ((BASE_LIBRARY_JUMP_BUFFER *)(UINTN)(ExceptionBuffer), 1);
}
}
if (MultiProcessorDebugSupport()) {
//
// If RUN commmand is executing, wait for it done.
//
while (mDebugMpContext.RunCommandSet) {
CpuPause ();
}
}
Handle = GetDebugPortHandle();
BreakCause = GetBreakCause (Vector, CpuContext);
switch (Vector) {
case DEBUG_INT1_VECTOR:
case DEBUG_INT3_VECTOR:
switch (BreakCause) {
case DEBUG_DATA_BREAK_CAUSE_SYSTEM_RESET:
if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
//
// Try to connect HOST, return if fails
//
break;
}
CommandCommunication (Vector, CpuContext, BreakReceived);
break;
case DEBUG_DATA_BREAK_CAUSE_STEPPING:
//
// Stepping is finished, send Ack package.
//
if (MultiProcessorDebugSupport()) {
mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
}
SendAckPacket (DEBUG_COMMAND_OK);
CommandCommunication (Vector, CpuContext, BreakReceived);
break;
case DEBUG_DATA_BREAK_CAUSE_MEMORY_READY:
//
// Memory is ready
//
SendCommandAndWaitForAckOK (DEBUG_COMMAND_MEMORY_READY, READ_PACKET_TIMEOUT, &BreakReceived, NULL);
CommandCommunication (Vector, CpuContext, BreakReceived);
break;
case DEBUG_DATA_BREAK_CAUSE_IMAGE_LOAD:
case DEBUG_DATA_BREAK_CAUSE_IMAGE_UNLOAD:
//
// Set AL to DEBUG_AGENT_IMAGE_CONTINUE
//
Al = ArchReadRegisterBuffer (CpuContext, SOFT_DEBUGGER_REGISTER_AX, &Data8);
*Al = DEBUG_AGENT_IMAGE_CONTINUE;
if (!IsHostAttached ()) {
//
// If HOST is not connected for image load/unload, return
//
break;
}
//
// Continue to run the following common code
//
case DEBUG_DATA_BREAK_CAUSE_HW_BREAKPOINT:
case DEBUG_DATA_BREAK_CAUSE_SW_BREAKPOINT:
default:
//
// Send Break packet to HOST
//
AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
//
// Only the first breaking processor could send BREAK_POINT to HOST
//
if (IsFirstBreakProcessor (ProcessorIndex)) {
SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
}
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
if (Vector == DEBUG_INT3_VECTOR) {
//
// go back address located "0xCC"
//
CpuContext->Eip--;
SavedEip = CpuContext->Eip;
CommandCommunication (Vector, CpuContext, BreakReceived);
if ((SavedEip == CpuContext->Eip) &&
(*(UINT8 *) (UINTN) CpuContext->Eip == DEBUG_SW_BREAKPOINT_SYMBOL)) {
//
// If this is not a software breakpoint set by HOST,
// restore EIP
//
CpuContext->Eip++;
}
} else {
CommandCommunication (Vector, CpuContext, BreakReceived);
}
break;
}
break;
case DEBUG_TIMER_VECTOR:
AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
if (MultiProcessorDebugSupport()) {
if (IsBsp (ProcessorIndex)) {
//
// If current processor is BSP, check Apic timer's init count if changed,
// it may be re-written when switching BSP.
// If it changed, re-initialize debug timer
//
CurrentDebugTimerInitCount = GetApicTimerInitCount ();
if (mDebugMpContext.DebugTimerInitCount != CurrentDebugTimerInitCount) {
InitializeDebugTimer ();
}
}
if (!IsBsp (ProcessorIndex) || mDebugMpContext.IpiSentByAp) {
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
//
// If current processor is not BSP or this is one IPI sent by AP
//
if (mDebugMpContext.BreakAtCpuIndex != (UINT32) (-1)) {
CommandCommunication (Vector, CpuContext, FALSE);
}
//
// Clear EOI before exiting interrupt process routine.
//
SendApicEoi ();
break;
}
}
//
// Only BSP could run here
//
while (TRUE) {
//
// If there is data in debug port, will check whether it is break(attach/break-in) symbol,
// If yes, go into communication mode with HOST.
// If no, exit interrupt process.
//
if (DebugReadBreakSymbol (Handle, &InputCharacter) == EFI_NOT_FOUND) {
break;
}
if ((!IsHostAttached () && (InputCharacter == DEBUG_STARTING_SYMBOL_ATTACH)) ||
(IsHostAttached () && (InputCharacter == DEBUG_COMMAND_HALT)) ||
(IsHostAttached () && (InputCharacter == DEBUG_COMMAND_GO))
) {
DebugAgentMsgPrint (DEBUG_AGENT_VERBOSE, "Received data [%02x]\n", InputCharacter);
//
// Ack OK for break-in symbol
//
SendAckPacket (DEBUG_COMMAND_OK);
//
// If receive GO command in Debug Timer, means HOST may lost ACK packet before.
//
if (InputCharacter == DEBUG_COMMAND_GO) {
break;
}
if (!IsHostAttached ()) {
//
// Try to attach HOST, if no ack received after 200ms, return
//
if (AttachHost (BreakCause, READ_PACKET_TIMEOUT, &BreakReceived) != RETURN_SUCCESS) {
break;
}
}
if (MultiProcessorDebugSupport()) {
if(FindNextPendingBreakCpu () != -1) {
SetCpuBreakFlagByIndex (ProcessorIndex, TRUE);
} else {
HaltOtherProcessors (ProcessorIndex);
}
}
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
CommandCommunication (Vector, CpuContext, BreakReceived);
AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
break;
}
}
//
// Clear EOI before exiting interrupt process routine.
//
SendApicEoi ();
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
break;
default:
if (Vector <= DEBUG_EXCEPT_SIMD) {
if (BreakCause == DEBUG_DATA_BREAK_CAUSE_STEPPING) {
//
// Stepping is finished, send Ack package.
//
if (MultiProcessorDebugSupport()) {
mDebugMpContext.BreakAtCpuIndex = ProcessorIndex;
}
SendAckPacket (DEBUG_COMMAND_OK);
} else {
//
// Exception occurs, send Break packet to HOST
//
AcquireMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
//
// Only the first breaking processor could send BREAK_POINT to HOST
//
if (IsFirstBreakProcessor (ProcessorIndex)) {
SendBreakPacketToHost (BreakCause, ProcessorIndex, &BreakReceived);
}
ReleaseMpSpinLock (&mDebugMpContext.DebugPortSpinLock);
}
CommandCommunication (Vector, CpuContext, BreakReceived);
}
break;
}
if (MultiProcessorDebugSupport()) {
//
// Clear flag and wait for all processors run here
//
SetIpiSentByApFlag (FALSE);
while (mDebugMpContext.RunCommandSet) {
CpuPause ();
}
//
// Only current (view) processor could clean up AgentInProgress flag.
//
if (mDebugMpContext.ViewPointIndex == ProcessorIndex) {
IssuedViewPoint = mDebugMpContext.ViewPointIndex;
}
}
if (IssuedViewPoint == ProcessorIndex && GetDebugFlag (DEBUG_AGENT_FLAG_STEPPING) != 1) {
//
// If the command is not stepping, clean up AgentInProgress flag
//
SetDebugFlag (DEBUG_AGENT_FLAG_AGENT_IN_PROGRESS, 0);
}
return;
}
|