summaryrefslogtreecommitdiff
path: root/src/VBox/Debugger/DBGCRemoteKd.cpp
blob: e2b5031b984358bd3e5b931856979fb6f6731081 (plain)
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
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
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
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
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
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
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
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
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
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
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
2921
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
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
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
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
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
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
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
3215
3216
3217
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
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
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
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
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
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
/* $Id$ */
/** @file
 * DBGC - Debugger Console, Windows Kd Remote Stub.
 */

/*
 * Copyright (C) 2020-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#include <VBox/dbg.h>
#include <VBox/vmm/dbgf.h>
#include <VBox/vmm/vmapi.h> /* VMR3GetVM() */
#include <VBox/vmm/hm.h>    /* HMR3IsEnabled */
#include <VBox/vmm/nem.h>   /* NEMR3IsEnabled */
#include <iprt/assertcompile.h>
#include <iprt/cdefs.h>
#include <iprt/err.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/sg.h>
#include <iprt/string.h>
#include <iprt/time.h>
#include <iprt/x86.h>
#include <iprt/formats/pecoff.h>
#include <iprt/formats/mz.h>

#include <stdlib.h>

#include "DBGCInternal.h"


/*********************************************************************************************************************************
*   Defined Constants And Macros                                                                                                 *
*********************************************************************************************************************************/

/** Number of milliseconds we wait for new data to arrive when a new packet was detected. */
#define DBGC_KD_RECV_TIMEOUT_MS                     UINT32_C(1000)

/** NT status code - Success. */
#define NTSTATUS_SUCCESS                            0
/** NT status code - buffer overflow. */
#define NTSTATUS_BUFFER_OVERFLOW                    UINT32_C(0x80000005)
/** NT status code - operation unsuccesful. */
#define NTSTATUS_UNSUCCESSFUL                       UINT32_C(0xc0000001)
/** NT status code - operation not implemented. */
#define NTSTATUS_NOT_IMPLEMENTED                    UINT32_C(0xc0000002)
/** NT status code - Object not found. */
#define NTSTATUS_NOT_FOUND                          UINT32_C(0xc0000225)

/** Offset where the KD version block pointer is stored in the KPCR.
 * From: https://www.geoffchappell.com/studies/windows/km/ntoskrnl/structs/kprcb/amd64.htm */
#define KD_KPCR_VERSION_BLOCK_ADDR_OFF              0x34


/*********************************************************************************************************************************
*   Structures and Typedefs                                                                                                      *
*********************************************************************************************************************************/

/**
 * KD packet header as sent over the wire.
 */
typedef struct KDPACKETHDR
{
    /** Packet signature (leader) - defines the type of packet. */
    uint32_t                    u32Signature;
    /** Packet (sub) type. */
    uint16_t                    u16SubType;
    /** Size of the packet body in bytes.*/
    uint16_t                    cbBody;
    /** Packet ID. */
    uint32_t                    idPacket;
    /** Checksum of the packet body. */
    uint32_t                    u32ChkSum;
} KDPACKETHDR;
AssertCompileSize(KDPACKETHDR, 16);
/** Pointer to a packet header. */
typedef KDPACKETHDR *PKDPACKETHDR;
/** Pointer to a const packet header. */
typedef const KDPACKETHDR *PCKDPACKETHDR;

/** Signature for a data packet. */
#define KD_PACKET_HDR_SIGNATURE_DATA                UINT32_C(0x30303030)
/** First byte for a data packet header. */
#define KD_PACKET_HDR_SIGNATURE_DATA_BYTE           0x30
/** Signature for a control packet. */
#define KD_PACKET_HDR_SIGNATURE_CONTROL             UINT32_C(0x69696969)
/** First byte for a control packet header. */
#define KD_PACKET_HDR_SIGNATURE_CONTROL_BYTE        0x69
/** Signature for a breakin packet. */
#define KD_PACKET_HDR_SIGNATURE_BREAKIN             UINT32_C(0x62626262)
/** First byte for a breakin packet header. */
#define KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE        0x62

/** @name Packet sub types.
 * @{ */
#define KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE32       UINT16_C(1)
#define KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE     UINT16_C(2)
#define KD_PACKET_HDR_SUB_TYPE_DEBUG_IO             UINT16_C(3)
#define KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE          UINT16_C(4)
#define KD_PACKET_HDR_SUB_TYPE_RESEND               UINT16_C(5)
#define KD_PACKET_HDR_SUB_TYPE_RESET                UINT16_C(6)
#define KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64       UINT16_C(7)
#define KD_PACKET_HDR_SUB_TYPE_POLL_BREAKIN         UINT16_C(8)
#define KD_PACKET_HDR_SUB_TYPE_TRACE_IO             UINT16_C(9)
#define KD_PACKET_HDR_SUB_TYPE_CONTROL_REQUEST      UINT16_C(10)
#define KD_PACKET_HDR_SUB_TYPE_FILE_IO              UINT16_C(11)
#define KD_PACKET_HDR_SUB_TYPE_MAX                  UINT16_C(12)
/** @} */

/** Initial packet ID value. */
#define KD_PACKET_HDR_ID_INITIAL                    UINT32_C(0x80800800)
/** Packet ID value after a resync. */
#define KD_PACKET_HDR_ID_RESET                      UINT32_C(0x80800000)

/** Trailing byte of a packet. */
#define KD_PACKET_TRAILING_BYTE                     0xaa


/** Maximum number of parameters in the exception record. */
#define KDPACKETEXCP_PARMS_MAX                      15

/**
 * 64bit exception record.
 */
typedef struct KDPACKETEXCP64
{
    /** The exception code identifying the excpetion. */
    uint32_t                    u32ExcpCode;
    /** Flags associated with the exception. */
    uint32_t                    u32ExcpFlags;
    /** Pointer to a chained exception record. */
    uint64_t                    u64PtrExcpRecNested;
    /** Address where the exception occurred. */
    uint64_t                    u64PtrExcpAddr;
    /** Number of parameters in the exception information array. */
    uint32_t                    cExcpParms;
    /** Alignment. */
    uint32_t                    u32Alignment;
    /** Exception parameters array. */
    uint64_t                    au64ExcpParms[KDPACKETEXCP_PARMS_MAX];
} KDPACKETEXCP64;
AssertCompileSize(KDPACKETEXCP64, 152);
/** Pointer to an exception record. */
typedef KDPACKETEXCP64 *PKDPACKETEXCP64;
/** Pointer to a const exception record. */
typedef const KDPACKETEXCP64 *PCKDPACKETEXCP64;


/**
 * amd64 NT context structure.
 */
typedef struct NTCONTEXT64
{
    /** The P[1-6]Home members. */
    uint64_t                    au64PHome[6];
    /** Context flags indicating the valid bits, see NTCONTEXT_F_XXX. */
    uint32_t                    fContext;
    /** MXCSR register. */
    uint32_t                    u32RegMxCsr;
    /** CS selector. */
    uint16_t                    u16SegCs;
    /** DS selector. */
    uint16_t                    u16SegDs;
    /** ES selector. */
    uint16_t                    u16SegEs;
    /** FS selector. */
    uint16_t                    u16SegFs;
    /** GS selector. */
    uint16_t                    u16SegGs;
    /** SS selector. */
    uint16_t                    u16SegSs;
    /** EFlags register. */
    uint32_t                    u32RegEflags;
    /** DR0 register. */
    uint64_t                    u64RegDr0;
    /** DR1 register. */
    uint64_t                    u64RegDr1;
    /** DR2 register. */
    uint64_t                    u64RegDr2;
    /** DR3 register. */
    uint64_t                    u64RegDr3;
    /** DR6 register. */
    uint64_t                    u64RegDr6;
    /** DR7 register. */
    uint64_t                    u64RegDr7;
    /** RAX register. */
    uint64_t                    u64RegRax;
    /** RCX register. */
    uint64_t                    u64RegRcx;
    /** RDX register. */
    uint64_t                    u64RegRdx;
    /** RBX register. */
    uint64_t                    u64RegRbx;
    /** RSP register. */
    uint64_t                    u64RegRsp;
    /** RBP register. */
    uint64_t                    u64RegRbp;
    /** RSI register. */
    uint64_t                    u64RegRsi;
    /** RDI register. */
    uint64_t                    u64RegRdi;
    /** R8 register. */
    uint64_t                    u64RegR8;
    /** R9 register. */
    uint64_t                    u64RegR9;
    /** R10 register. */
    uint64_t                    u64RegR10;
    /** R11 register. */
    uint64_t                    u64RegR11;
    /** R12 register. */
    uint64_t                    u64RegR12;
    /** R13 register. */
    uint64_t                    u64RegR13;
    /** R14 register. */
    uint64_t                    u64RegR14;
    /** R15 register. */
    uint64_t                    u64RegR15;
    /** RIP register. */
    uint64_t                    u64RegRip;
    /** Extended floating point save area. */
    X86FXSTATE                  FxSave;
    /** AVX(?) vector registers. */
    RTUINT128U                  aRegsVec[26];
    /** Vector control register. */
    uint64_t                    u64RegVecCtrl;
    /** Debug control. */
    uint64_t                    u64DbgCtrl;
    /** @todo lbr */
    uint64_t                    u64LastBrToRip;
    uint64_t                    u64LastBrFromRip;
    uint64_t                    u64LastExcpToRip;
    uint64_t                    u64LastExcpFromRip;
} NTCONTEXT64;
AssertCompileSize(NTCONTEXT64, 1232);
AssertCompileMemberOffset(NTCONTEXT64, FxSave, 0x100);
AssertCompileMemberOffset(NTCONTEXT64, aRegsVec, 0x300);
/** Pointer to an amd64 NT context. */
typedef NTCONTEXT64 *PNTCONTEXT64;
/** Pointer to a const amd64 NT context. */
typedef const NTCONTEXT64 *PCNTCONTEXT64;


/**
 * 64bit [GI]DT descriptor.
 */
typedef struct NTKCONTEXTDESC64
{
    /** Alignment. */
    uint16_t                    au16Alignment[3];
    /** Limit. */
    uint16_t                    u16Limit;
    /** Base address. */
    uint64_t                    u64PtrBase;
} NTKCONTEXTDESC64;
AssertCompileSize(NTKCONTEXTDESC64, 2 * 8);
/** Pointer to a 64bit [GI]DT descriptor. */
typedef NTKCONTEXTDESC64 *PNTKCONTEXTDESC64;
/** Pointer to a const 64bit [GI]DT descriptor. */
typedef const NTKCONTEXTDESC64 *PCNTKCONTEXTDESC64;


/**
 * Kernel context as queried by KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE
 */
typedef struct NTKCONTEXT64
{
    /** CR0 register. */
    uint64_t                    u64RegCr0;
    /** CR2 register. */
    uint64_t                    u64RegCr2;
    /** CR3 register. */
    uint64_t                    u64RegCr3;
    /** CR4 register. */
    uint64_t                    u64RegCr4;
    /** DR0 register. */
    uint64_t                    u64RegDr0;
    /** DR1 register. */
    uint64_t                    u64RegDr1;
    /** DR2 register. */
    uint64_t                    u64RegDr2;
    /** DR3 register. */
    uint64_t                    u64RegDr3;
    /** DR6 register. */
    uint64_t                    u64RegDr6;
    /** DR7 register. */
    uint64_t                    u64RegDr7;
    /** GDTR. */
    NTKCONTEXTDESC64            Gdtr;
    /** IDTR. */
    NTKCONTEXTDESC64            Idtr;
    /** TR register. */
    uint16_t                    u16RegTr;
    /** LDTR register. */
    uint16_t                    u16RegLdtr;
    /** MXCSR register. */
    uint32_t                    u32RegMxCsr;
    /** Debug control. */
    uint64_t                    u64DbgCtrl;
    /** @todo lbr */
    uint64_t                    u64LastBrToRip;
    uint64_t                    u64LastBrFromRip;
    uint64_t                    u64LastExcpToRip;
    uint64_t                    u64LastExcpFromRip;
    /** CR8 register. */
    uint64_t                    u64RegCr8;
    /** GS base MSR register. */
    uint64_t                    u64MsrGsBase;
    /** Kernel GS base MSR register. */
    uint64_t                    u64MsrKernelGsBase;
    /** STAR MSR register. */
    uint64_t                    u64MsrStar;
    /** LSTAR MSR register. */
    uint64_t                    u64MsrLstar;
    /** CSTAR MSR register. */
    uint64_t                    u64MsrCstar;
    /** SFMASK MSR register. */
    uint64_t                    u64MsrSfMask;
    /** XCR0 register. */
    uint64_t                    u64RegXcr0;
    /** Standard context. */
    NTCONTEXT64                 Ctx;
} NTKCONTEXT64;
AssertCompileMemberOffset(NTKCONTEXT64, Ctx, 224);
/** Pointer to an amd64 NT context. */
typedef NTKCONTEXT64 *PNTKCONTEXT64;
/** Pointer to a const amd64 NT context. */
typedef const NTKCONTEXT64 *PCNTKCONTEXT64;


/**
 * 32bit context FPU save area.
 */
typedef struct NTCONTEXT32_FPU_SAVE_AREA
{
    uint32_t                    u32CtrlWord;
    uint32_t                    u32StatusWord;
    uint32_t                    u32TagWord;
    uint32_t                    u32ErrorOff;
    uint32_t                    u32ErrorSel;
    uint32_t                    u32DataOff;
    uint32_t                    u32DataSel;
    X86FPUMMX                   aFpuRegs[8];
    uint32_t                    u32Cr0Npx;
} NTCONTEXT32_FPU_SAVE_AREA;
/** Pointer to an 32bit context FPU save area. */
typedef NTCONTEXT32_FPU_SAVE_AREA *PNTCONTEXT32_FPU_SAVE_AREA;
/** Pointer to a const 32bit context FPU save area. */
typedef const NTCONTEXT32_FPU_SAVE_AREA *PCNTCONTEXT32_FPU_SAVE_AREA;


/**
 * i386 NT context structure.
 */
typedef struct NTCONTEXT32
{
    /** Context flags indicating the valid bits, see NTCONTEXT_F_XXX. */
    uint32_t                    fContext;
    /** DR0 register. */
    uint32_t                    u32RegDr0;
    /** DR1 register. */
    uint32_t                    u32RegDr1;
    /** DR2 register. */
    uint32_t                    u32RegDr2;
    /** DR3 register. */
    uint32_t                    u32RegDr3;
    /** DR6 register. */
    uint32_t                    u32RegDr6;
    /** DR7 register. */
    uint32_t                    u32RegDr7;
    /** Floating point save area. */
    NTCONTEXT32_FPU_SAVE_AREA   FloatSave;
    /** GS segment. */
    uint32_t                    u32SegGs;
    /** FS segment. */
    uint32_t                    u32SegFs;
    /** ES segment. */
    uint32_t                    u32SegEs;
    /** DS segment. */
    uint32_t                    u32SegDs;
    /** EDI register. */
    uint32_t                    u32RegEdi;
    /** ESI register. */
    uint32_t                    u32RegEsi;
    /** EBX register. */
    uint32_t                    u32RegEbx;
    /** EDX register. */
    uint32_t                    u32RegEdx;
    /** ECX register. */
    uint32_t                    u32RegEcx;
    /** EAX register. */
    uint32_t                    u32RegEax;
    /** EBP register. */
    uint32_t                    u32RegEbp;
    /** EIP register. */
    uint32_t                    u32RegEip;
    /** CS segment. */
    uint32_t                    u32SegCs;
    /** EFLAGS register. */
    uint32_t                    u32RegEflags;
    /** ESP register. */
    uint32_t                    u32RegEsp;
    /** SS segment. */
    uint32_t                    u32SegSs;
    /** @todo Extended registers */
    uint8_t                     abRegsExtended[512];
} NTCONTEXT32;
AssertCompileSize(NTCONTEXT32, 716);
/** Pointer to an i386 NT context. */
typedef NTCONTEXT32 *PNTCONTEXT32;
/** Pointer to a const i386 NT context. */
typedef const NTCONTEXT32 *PCNTCONTEXT32;


/**
 * 32bit [GI]DT descriptor.
 */
typedef struct NTKCONTEXTDESC32
{
    /** Alignment. */
    uint16_t                    u16Alignment;
    /** Limit. */
    uint16_t                    u16Limit;
    /** Base address. */
    uint32_t                    u32PtrBase;
} NTKCONTEXTDESC32;
AssertCompileSize(NTKCONTEXTDESC32, 2 * 4);
/** Pointer to an 32bit [GI]DT descriptor. */
typedef NTKCONTEXTDESC32 *PNTKCONTEXTDESC32;
/** Pointer to a const 32bit [GI]DT descriptor. */
typedef const NTKCONTEXTDESC32 *PCNTKCONTEXTDESC32;


/**
 * 32bit Kernel context as queried by KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE
 */
typedef struct NTKCONTEXT32
{
    /** CR0 register. */
    uint32_t                    u32RegCr0;
    /** CR2 register. */
    uint32_t                    u32RegCr2;
    /** CR3 register. */
    uint32_t                    u32RegCr3;
    /** CR4 register. */
    uint32_t                    u32RegCr4;
    /** DR0 register. */
    uint32_t                    u32RegDr0;
    /** DR1 register. */
    uint32_t                    u32RegDr1;
    /** DR2 register. */
    uint32_t                    u32RegDr2;
    /** DR3 register. */
    uint32_t                    u32RegDr3;
    /** DR6 register. */
    uint32_t                    u32RegDr6;
    /** DR7 register. */
    uint32_t                    u32RegDr7;
    /** GDTR. */
    NTKCONTEXTDESC32            Gdtr;
    /** IDTR. */
    NTKCONTEXTDESC32            Idtr;
    /** TR register. */
    uint16_t                    u16RegTr;
    /** LDTR register. */
    uint16_t                    u16RegLdtr;
    /** Padding. */
    uint8_t                     abPad[24];
} NTKCONTEXT32;
AssertCompileSize(NTKCONTEXT32, 84);
/** Pointer to an i386 NT context. */
typedef NTKCONTEXT32 *PNTKCONTEXT32;
/** Pointer to a const i386 NT context. */
typedef const NTKCONTEXT32 *PCNTKCONTEXT32;


/** x86 context. */
#define NTCONTEXT_F_X86                             UINT32_C(0x00010000)
/** AMD64 context. */
#define NTCONTEXT_F_AMD64                           UINT32_C(0x00100000)
/** Control registers valid (CS, (R)SP, (R)IP, FLAGS and BP). */
#define NTCONTEXT_F_CONTROL                         RT_BIT_32(0)
/** Integer registers valid. */
#define NTCONTEXT_F_INTEGER                         RT_BIT_32(1)
/** Segment registers valid. */
#define NTCONTEXT_F_SEGMENTS                        RT_BIT_32(2)
/** Floating point registers valid. */
#define NTCONTEXT_F_FLOATING_POINT                  RT_BIT_32(3)
/** Debug registers valid. */
#define NTCONTEXT_F_DEBUG                           RT_BIT_32(4)
/** Extended registers valid (x86 only). */
#define NTCONTEXT_F_EXTENDED                        RT_BIT_32(5)
/** Full x86 context valid. */
#define NTCONTEXT32_F_FULL (NTCONTEXT_F_X86 | NTCONTEXT_F_CONTROL | NTCONTEXT_F_INTEGER | NTCONTEXT_F_SEGMENTS)
/** Full amd64 context valid. */
#define NTCONTEXT64_F_FULL (NTCONTEXT_F_AMD64 | NTCONTEXT_F_CONTROL | NTCONTEXT_F_INTEGER | NTCONTEXT_F_SEGMENTS)


/**
 * 32bit exception record.
 */
typedef struct KDPACKETEXCP32
{
    /** The exception code identifying the excpetion. */
    uint32_t                    u32ExcpCode;
    /** Flags associated with the exception. */
    uint32_t                    u32ExcpFlags;
    /** Pointer to a chained exception record. */
    uint32_t                    u32PtrExcpRecNested;
    /** Address where the exception occurred. */
    uint32_t                    u32PtrExcpAddr;
    /** Number of parameters in the exception information array. */
    uint32_t                    cExcpParms;
    /** Exception parameters array. */
    uint32_t                    au32ExcpParms[KDPACKETEXCP_PARMS_MAX];
} KDPACKETEXCP32;
AssertCompileSize(KDPACKETEXCP32, 80);
/** Pointer to an exception record. */
typedef KDPACKETEXCP32 *PKDPACKETEXCP32;
/** Pointer to a const exception record. */
typedef const KDPACKETEXCP32 *PCKDPACKETEXCP32;


/** @name Exception codes.
 * @{ */
/** A breakpoint was hit. */
#define KD_PACKET_EXCP_CODE_BKPT                    UINT32_C(0x80000003)
/** An instruction was single stepped. */
#define KD_PACKET_EXCP_CODE_SINGLE_STEP             UINT32_C(0x80000004)
/** @} */


/** Maximum number of bytes in the instruction stream. */
#define KD_PACKET_CTRL_REPORT_INSN_STREAM_MAX       16

/**
 * 64bit control report record.
 */
typedef struct KDPACKETCTRLREPORT64
{
    /** Value of DR6. */
    uint64_t                    u64RegDr6;
    /** Value of DR7. */
    uint64_t                    u64RegDr7;
    /** EFLAGS. */
    uint32_t                    u32RegEflags;
    /** Number of instruction bytes in the instruction stream. */
    uint16_t                    cbInsnStream;
    /** Report flags. */
    uint16_t                    fFlags;
    /** The instruction stream. */
    uint8_t                     abInsn[KD_PACKET_CTRL_REPORT_INSN_STREAM_MAX];
    /** CS selector. */
    uint16_t                    u16SegCs;
    /** DS selector. */
    uint16_t                    u16SegDs;
    /** ES selector. */
    uint16_t                    u16SegEs;
    /** FS selector. */
    uint16_t                    u16SegFs;
} KDPACKETCTRLREPORT64;
AssertCompileSize(KDPACKETCTRLREPORT64, 2 * 8 + 4 + 2 * 2 + 16 + 4 * 2);
/** Pointer to a control report record. */
typedef KDPACKETCTRLREPORT64 *PKDPACKETCTRLREPORT64;
/** Pointer to a const control report record. */
typedef const KDPACKETCTRLREPORT64 *PCKDPACKETCTRLREPORT64;


/**
 * 64bit state change packet body.
 */
typedef struct KDPACKETSTATECHANGE64
{
    /** The new state. */
    uint32_t                    u32StateNew;
    /** The processor level. */
    uint16_t                    u16CpuLvl;
    /** The processor ID generating the state change. */
    uint16_t                    idCpu;
    /** Number of processors in the system. */
    uint32_t                    cCpus;
    /** Alignment. */
    uint32_t                    u32Alignment;
    /** The thread ID currently executing when the state change occurred. */
    uint64_t                    idThread;
    /** Program counter of the thread. */
    uint64_t                    u64RipThread;
    /** Data based on the state. */
    union
    {
        /** Exception occurred data. */
        struct
        {
            /** The exception record. */
            KDPACKETEXCP64      ExcpRec;
            /** First chance(?). */
            uint32_t            u32FirstChance;
        } Exception;
    } u;
    /** The control report */
    union
    {
        /** AMD64 control report. */
        KDPACKETCTRLREPORT64    Amd64;
    } uCtrlReport;
} KDPACKETSTATECHANGE64;
//AssertCompileSize(KDPACKETSTATECHANGE64, 4 + 2 * 2 + 2 * 4 + 2 * 8 + sizeof(KDPACKETEXCP64) + 4 + sizeof(KDPACKETCTRLREPORT64));
/** Pointer to a 64bit state change packet body. */
typedef KDPACKETSTATECHANGE64 *PKDPACKETSTATECHANGE64;
/** Pointer to a const 64bit state change packet body. */
typedef const KDPACKETSTATECHANGE64 *PCKDPACKETSTATECHANGE64;


/** @name State change state types.
 * @{ */
/** Minimum state change type. */
#define KD_PACKET_STATE_CHANGE_MIN                  UINT32_C(0x00003030)
/** An exception occured. */
#define KD_PACKET_STATE_CHANGE_EXCEPTION            KD_PACKET_STATE_CHANGE_MIN
/** Symbols were loaded(?). */
#define KD_PACKET_STATE_CHANGE_LOAD_SYMBOLS         UINT32_C(0x00003031)
/** Command string (custom command was executed?). */
#define KD_PACKET_STATE_CHANGE_CMD_STRING           UINT32_C(0x00003032)
/** Maximum state change type (exclusive). */
#define KD_PACKET_STATE_CHANGE_MAX                  UINT32_C(0x00003033)
/** @} */


/**
 * Debug I/O payload.
 */
typedef struct KDPACKETDEBUGIO
{
    /** Debug I/O payload type (KD_PACKET_DEBUG_IO_STRING). */
    uint32_t                    u32Type;
    /** The processor level. */
    uint16_t                    u16CpuLvl;
    /** The processor ID generating this packet. */
    uint16_t                    idCpu;
    /** Type dependent data. */
    union
    {
        /** Debug string sent. */
        struct
        {
            /** Length of the string following in bytes. */
            uint32_t            cbStr;
            /** Some padding it looks like. */
            uint32_t            u32Pad;
        } Str;
        /** Debug prompt. */
        struct
        {
            /** Length of prompt. */
            uint32_t            cbPrompt;
            /** Size of the string returned on success. */
            uint32_t            cbReturn;
        } Prompt;
    } u;
} KDPACKETDEBUGIO;
AssertCompileSize(KDPACKETDEBUGIO, 16);
/** Pointer to a Debug I/O payload. */
typedef KDPACKETDEBUGIO *PKDPACKETDEBUGIO;
/** Pointer to a const Debug I/O payload. */
typedef const KDPACKETDEBUGIO *PCKDPACKETDEBUGIO;


/** @name Debug I/O types.
 * @{ */
/** Debug string output (usually DbgPrint() and friends). */
#define KD_PACKET_DEBUG_IO_STRING                   UINT32_C(0x00003230)
/** Get debug string (DbgPrompt()). */
#define KD_PACKET_DEBUG_IO_GET_STRING               UINT32_C(0x00003231)
/** @} */


/**
 * 64bit get version manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_GETVERSION64
{
    /** Major version. */
    uint16_t                    u16VersMaj;
    /** Minor version. */
    uint16_t                    u16VersMin;
    /** Protocol version. */
    uint8_t                     u8VersProtocol;
    /** KD secondary version. */
    uint8_t                     u8VersKdSecondary;
    /** Flags. */
    uint16_t                    fFlags;
    /** Machine type. */
    uint16_t                    u16MachineType;
    /** Maximum packet type. */
    uint8_t                     u8MaxPktType;
    /** Maximum state change */
    uint8_t                     u8MaxStateChange;
    /** Maximum manipulate request ID. */
    uint8_t                     u8MaxManipulate;
    /** Some simulation flag. */
    uint8_t                     u8Simulation;
    /** Padding. */
    uint16_t                    u16Padding;
    /** Kernel base. */
    uint64_t                    u64PtrKernBase;
    /** Pointer of the loaded module list head. */
    uint64_t                    u64PtrPsLoadedModuleList;
    /** Pointer of the debugger data list. */
    uint64_t                    u64PtrDebuggerDataList;
} KDPACKETMANIPULATE_GETVERSION64;
AssertCompileSize(KDPACKETMANIPULATE_GETVERSION64, 40);
/** Pointer to a 64bit get version manipulate payload. */
typedef KDPACKETMANIPULATE_GETVERSION64 *PKDPACKETMANIPULATE_GETVERSION64;
/** Pointer to a const 64bit get version manipulate payload. */
typedef const KDPACKETMANIPULATE_GETVERSION64 *PCKDPACKETMANIPULATE_GETVERSION64;


/** @name Get version flags.
 * @{ */
/** Flag whether this is a multi processor kernel. */
#define KD_PACKET_MANIPULATE64_GET_VERSION_F_MP     RT_BIT_32(0)
/** Flag whether the pointer is 64bit. */
#define KD_PACKET_MANIPULATE64_GET_VERSION_F_PTR64  RT_BIT_32(2)
/** @} */


/**
 * 64bit memory transfer manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_XFERMEM64
{
    /** Target base address. */
    uint64_t                    u64PtrTarget;
    /** Requested number of bytes to transfer*/
    uint32_t                    cbXferReq;
    /** Number of bytes actually transferred (response). */
    uint32_t                    cbXfered;
    /** Some padding?. */
    uint64_t                    au64Pad[3];
} KDPACKETMANIPULATE_XFERMEM64;
AssertCompileSize(KDPACKETMANIPULATE_XFERMEM64, 40);
/** Pointer to a 64bit memory transfer manipulate payload. */
typedef KDPACKETMANIPULATE_XFERMEM64 *PKDPACKETMANIPULATE_XFERMEM64;
/** Pointer to a const 64bit memory transfer manipulate payload. */
typedef const KDPACKETMANIPULATE_XFERMEM64 *PCKDPACKETMANIPULATE_XFERMEM64;


/**
 * 64bit control space transfer manipulate payload.
 *
 * @note Same layout as the memory transfer but the pointer has a different meaning so
 *       we moved it into a separate request structure.
 */
typedef struct KDPACKETMANIPULATE_XFERCTRLSPACE64
{
    /** Identifier of the item to transfer in the control space. */
    uint64_t                    u64IdXfer;
    /** Requested number of bytes to transfer*/
    uint32_t                    cbXferReq;
    /** Number of bytes actually transferred (response). */
    uint32_t                    cbXfered;
    /** Some padding?. */
    uint64_t                    au64Pad[3];
} KDPACKETMANIPULATE_XFERCTRLSPACE64;
AssertCompileSize(KDPACKETMANIPULATE_XFERCTRLSPACE64, 40);
/** Pointer to a 64bit memory transfer manipulate payload. */
typedef KDPACKETMANIPULATE_XFERCTRLSPACE64 *PKDPACKETMANIPULATE_XFERCTRLSPACE64;
/** Pointer to a const 64bit memory transfer manipulate payload. */
typedef const KDPACKETMANIPULATE_XFERCTRLSPACE64 *PCKDPACKETMANIPULATE_XFERCTRLSPACE64;


/** @name Known control space identifiers.
 * @{ */
/** Read/Write KPCR address. */
#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCR   UINT64_C(0)
/** Read/Write KPCRB address. */
#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCRB  UINT64_C(1)
/** Read/Write Kernel context. */
#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KCTX   UINT64_C(2)
/** Read/Write current kernel thread. */
#define KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KTHRD  UINT64_C(3)
/** @} */


/**
 * 64bit restore breakpoint manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_RESTOREBKPT64
{
    /** The breakpoint handle to restore. */
    uint32_t                    u32HndBkpt;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[36];
} KDPACKETMANIPULATE_RESTOREBKPT64;
AssertCompileSize(KDPACKETMANIPULATE_RESTOREBKPT64, 40);
/** Pointer to a 64bit restore breakpoint manipulate payload. */
typedef KDPACKETMANIPULATE_RESTOREBKPT64 *PKDPACKETMANIPULATE_RESTOREBKPT64;
/** Pointer to a const 64bit restore breakpoint manipulate payload. */
typedef const KDPACKETMANIPULATE_RESTOREBKPT64 *PCKDPACKETMANIPULATE_RESTOREBKPT64;


/**
 * 64bit write breakpoint manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_WRITEBKPT64
{
    /** Where to write the breakpoint. */
    uint64_t                    u64PtrBkpt;
    /** The breakpoint handle returned in the response. */
    uint32_t                    u32HndBkpt;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[28];
} KDPACKETMANIPULATE_WRITEBKPT64;
AssertCompileSize(KDPACKETMANIPULATE_WRITEBKPT64, 40);
/** Pointer to a 64bit write breakpoint manipulate payload. */
typedef KDPACKETMANIPULATE_WRITEBKPT64 *PKDPACKETMANIPULATE_WRITEBKPT64;
/** Pointer to a const 64bit write breakpoint manipulate payload. */
typedef const KDPACKETMANIPULATE_WRITEBKPT64 *PCKDPACKETMANIPULATE_WRITEBKPT64;


/**
 * Context extended manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_CONTEXTEX
{
    /** Where to start copying the context. */
    uint32_t                    offStart;
    /** Number of bytes to transfer. */
    uint32_t                    cbXfer;
    /** Number of bytes actually transfered. */
    uint32_t                    cbXfered;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[28];
} KDPACKETMANIPULATE_CONTEXTEX;
AssertCompileSize(KDPACKETMANIPULATE_CONTEXTEX, 40);
/** Pointer to a context extended manipulate payload. */
typedef KDPACKETMANIPULATE_CONTEXTEX *PKDPACKETMANIPULATE_CONTEXTEX;
/** Pointer to a const context extended manipulate payload. */
typedef const KDPACKETMANIPULATE_CONTEXTEX *PCKDPACKETMANIPULATE_CONTEXTEX;


/**
 * Continue manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_CONTINUE
{
    /** Continue (status?). */
    uint32_t                    u32NtContSts;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[36];
} KDPACKETMANIPULATE_CONTINUE;
AssertCompileSize(KDPACKETMANIPULATE_CONTINUE, 40);
/** Pointer to a continue manipulate payload. */
typedef KDPACKETMANIPULATE_CONTINUE *PKDPACKETMANIPULATE_CONTINUE;
/** Pointer to a const continue manipulate payload. */
typedef const KDPACKETMANIPULATE_CONTINUE *PCKDPACKETMANIPULATE_CONTINUE;


/**
 * Continue 2 manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_CONTINUE2
{
    /** Continue (status?). */
    uint32_t                    u32NtContSts;
    /** Trace flag. */
    uint32_t                    fTrace;
    /** Bitsize dependent data. */
    union
    {
        /** 32bit. */
        struct
        {
            /** DR7 value to continue with. */
            uint32_t            u32RegDr7;
            /** @todo (?) */
            uint32_t            u32SymCurStart;
            uint32_t            u32SymCurEnd;
        } x86;
        /** 64bit. */
        struct
        {
            /** DR7 value to continue with. */
            uint64_t            u64RegDr7;
            /** @todo (?) */
            uint64_t            u64SymCurStart;
            uint64_t            u64SymCurEnd;
        } amd64;
    } u;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[8];
} KDPACKETMANIPULATE_CONTINUE2;
AssertCompileSize(KDPACKETMANIPULATE_CONTINUE2, 40);
/** Pointer to a continue 2 manipulate payload. */
typedef KDPACKETMANIPULATE_CONTINUE2 *PKDPACKETMANIPULATE_CONTINUE2;
/** Pointer to a const continue 2 manipulate payload. */
typedef const KDPACKETMANIPULATE_CONTINUE2 *PCKDPACKETMANIPULATE_CONTINUE2;


/**
 * Set context manipulate payload.
 */
typedef struct KDPACKETMANIPULATE_SETCONTEXT
{
    /** Continue (status?). */
    uint32_t                    u32CtxFlags;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[36];
} KDPACKETMANIPULATE_SETCONTEXT;
AssertCompileSize(KDPACKETMANIPULATE_SETCONTEXT, 40);
/** Pointer to a set context manipulate payload. */
typedef KDPACKETMANIPULATE_SETCONTEXT *PKDPACKETMANIPULATE_SETCONTEXT;
/** Pointer to a const set context manipulate payload. */
typedef const KDPACKETMANIPULATE_SETCONTEXT *PCKDPACKETMANIPULATE_SETCONTEXT;


/**
 * Query memory properties payload.
 */
typedef struct KDPACKETMANIPULATE_QUERYMEMORY
{
    /** The address to query the properties for. */
    uint64_t                    u64GCPtr;
    /** Reserved. */
    uint64_t                    u64Rsvd;
    /** Address space type on return. */
    uint32_t                    u32AddrSpace;
    /** Protection flags. */
    uint32_t                    u32Flags;
    /** Blows up the request to the required size. */
    uint8_t                     abPad[16];
} KDPACKETMANIPULATE_QUERYMEMORY;
AssertCompileSize(KDPACKETMANIPULATE_QUERYMEMORY, 40);
/** Pointer to a query memory properties payload. */
typedef KDPACKETMANIPULATE_QUERYMEMORY *PKDPACKETMANIPULATE_QUERYMEMORY;
/** Pointer to a const query memory properties payload. */
typedef const KDPACKETMANIPULATE_QUERYMEMORY *PCKDPACKETMANIPULATE_QUERYMEMORY;


/** @name Query memory address space identifiers.
 * @{ */
/** Process memory space. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_SPACE_PROCESS  UINT32_C(0)
/** Session memory space. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_SPACE_SESSION  UINT32_C(1)
/** Kernel memory space. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_SPACE_KERNEL UINT32_C(2)
/** @} */


/** @name Query memory address protection flags.
 * @{ */
/** Readable. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_READ         RT_BIT_32(0)
/** Writable. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_WRITE        RT_BIT_32(1)
/** Executable. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_EXEC         RT_BIT_32(2)
/** Fixed address. */
#define KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_FIXED        RT_BIT_32(3)
/** @} */


/**
 * Search memory payload.
 */
typedef struct KDPACKETMANIPULATE_SEARCHMEMORY
{
    /** The address to start searching at on input, found address on output. */
    uint64_t                    u64GCPtr;
    /** Number of bytes to search. */
    uint64_t                    cbSearch;
    /** Length of the pattern to search for following the payload. */
    uint32_t                    cbPattern;
    /** Padding to the required size. */
    uint32_t                    au32Pad[5];
} KDPACKETMANIPULATE_SEARCHMEMORY;
AssertCompileSize(KDPACKETMANIPULATE_SEARCHMEMORY, 40);
/** Pointer to a search memory properties payload. */
typedef KDPACKETMANIPULATE_SEARCHMEMORY *PKDPACKETMANIPULATE_SEARCHMEMORY;
/** Pointer to a const search memory properties payload. */
typedef const KDPACKETMANIPULATE_SEARCHMEMORY *PCKDPACKETMANIPULATE_SEARCHMEMORY;


/**
 * Manipulate request packet header (Same for 32bit and 64bit).
 */
typedef struct KDPACKETMANIPULATEHDR
{
    /** The request to execute. */
    uint32_t                    idReq;
    /** The processor level to execute the request on. */
    uint16_t                    u16CpuLvl;
    /** The processor ID to execute the request on. */
    uint16_t                    idCpu;
    /** Return status code. */
    uint32_t                    u32NtStatus;
    /** Alignment. */
    uint32_t                    u32Alignment;
} KDPACKETMANIPULATEHDR;
AssertCompileSize(KDPACKETMANIPULATEHDR, 3 * 4 + 2 * 2);
/** Pointer to a manipulate request packet header. */
typedef KDPACKETMANIPULATEHDR *PKDPACKETMANIPULATEHDR;
/** Pointer to a const manipulate request packet header. */
typedef const KDPACKETMANIPULATEHDR *PCPKDPACKETMANIPULATEHDR;


/**
 * 64bit manipulate state request packet.
 */
typedef struct KDPACKETMANIPULATE64
{
    /** Header. */
    KDPACKETMANIPULATEHDR                  Hdr;
    /** Request payloads. */
    union
    {
        /** Get Version. */
        KDPACKETMANIPULATE_GETVERSION64    GetVersion;
        /** Read/Write memory. */
        KDPACKETMANIPULATE_XFERMEM64       XferMem;
        /** Continue. */
        KDPACKETMANIPULATE_CONTINUE        Continue;
        /** Continue2. */
        KDPACKETMANIPULATE_CONTINUE2       Continue2;
        /** Set context. */
        KDPACKETMANIPULATE_SETCONTEXT      SetContext;
        /** Read/Write control space. */
        KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace;
        /** Restore breakpoint. */
        KDPACKETMANIPULATE_RESTOREBKPT64   RestoreBkpt;
        /** Write breakpoint. */
        KDPACKETMANIPULATE_WRITEBKPT64     WriteBkpt;
        /** Context extended. */
        KDPACKETMANIPULATE_CONTEXTEX       ContextEx;
        /** Query memory. */
        KDPACKETMANIPULATE_QUERYMEMORY     QueryMemory;
        /** Search memory. */
        KDPACKETMANIPULATE_SEARCHMEMORY    SearchMemory;
    } u;
} KDPACKETMANIPULATE64;
AssertCompileSize(KDPACKETMANIPULATE64, 16 + 40);
/** Pointer to a 64bit manipulate state request packet. */
typedef KDPACKETMANIPULATE64 *PKDPACKETMANIPULATE64;
/** Pointer to a const 64bit manipulate state request packet. */
typedef const KDPACKETMANIPULATE64 *PCKDPACKETMANIPULATE64;

/** @name Manipulate requests.
 * @{ */
/** Minimum available request. */
#define KD_PACKET_MANIPULATE_REQ_MIN                        UINT32_C(0x00003130)
/** Read virtual memory request. */
#define KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM              KD_PACKET_MANIPULATE_REQ_MIN
/** Write virtual memory request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM             UINT32_C(0x00003131)
/** Get context request. */
#define KD_PACKET_MANIPULATE_REQ_GET_CONTEXT                UINT32_C(0x00003132)
/** Set context request. */
#define KD_PACKET_MANIPULATE_REQ_SET_CONTEXT                UINT32_C(0x00003133)
/** Write breakpoint request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_BKPT                 UINT32_C(0x00003134)
/** Restore breakpoint request. */
#define KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT               UINT32_C(0x00003135)
/** Continue request. */
#define KD_PACKET_MANIPULATE_REQ_CONTINUE                   UINT32_C(0x00003136)
/** Read control space request. */
#define KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE            UINT32_C(0x00003137)
/** Write control space request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE           UINT32_C(0x00003138)
/** Read I/O space request. */
#define KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE              UINT32_C(0x00003139)
/** Write I/O space request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE             UINT32_C(0x0000313a)
/** Reboot request. */
#define KD_PACKET_MANIPULATE_REQ_REBOOT                     UINT32_C(0x0000313b)
/** continue 2nd version request. */
#define KD_PACKET_MANIPULATE_REQ_CONTINUE2                  UINT32_C(0x0000313c)
/** Read physical memory request. */
#define KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM              UINT32_C(0x0000313d)
/** Write physical memory request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM             UINT32_C(0x0000313e)
/** Query special calls request. */
#define KD_PACKET_MANIPULATE_REQ_QUERY_SPEC_CALLS           UINT32_C(0x0000313f)
/** Set special calls request. */
#define KD_PACKET_MANIPULATE_REQ_SET_SPEC_CALLS             UINT32_C(0x00003140)
/** Clear special calls request. */
#define KD_PACKET_MANIPULATE_REQ_CLEAR_SPEC_CALLS           UINT32_C(0x00003141)
/** Set internal breakpoint request. */
#define KD_PACKET_MANIPULATE_REQ_SET_INTERNAL_BKPT          UINT32_C(0x00003142)
/** Get internal breakpoint request. */
#define KD_PACKET_MANIPULATE_REQ_GET_INTERNAL_BKPT          UINT32_C(0x00003143)
/** Read I/O space extended request. */
#define KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE_EX           UINT32_C(0x00003144)
/** Write I/O space extended request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE_EX          UINT32_C(0x00003145)
/** Get version request. */
#define KD_PACKET_MANIPULATE_REQ_GET_VERSION                UINT32_C(0x00003146)
/** Write breakpoint extended request. */
#define KD_PACKET_MANIPULATE_REQ_WRITE_BKPT_EX              UINT32_C(0x00003147)
/** Restore breakpoint extended request. */
#define KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT_EX            UINT32_C(0x00003148)
/** Cause a bugcheck request. */
#define KD_PACKET_MANIPULATE_REQ_CAUSE_BUGCHECK             UINT32_C(0x00003149)
/** Cause a bugcheck request. */
#define KD_PACKET_MANIPULATE_REQ_SWITCH_PROCESSOR           UINT32_C(0x00003150)
/** @todo 0x3151-0x3155 */
/** Search memory for a pattern request. */
#define KD_PACKET_MANIPULATE_REQ_SEARCH_MEMORY              UINT32_C(0x00003156)
/** @todo 0x3157-0x3159 */
/** Clear all internal breakpoints request. */
#define KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT    UINT32_C(0x0000315a)
/** Fill memory. */
#define KD_PACKET_MANIPULATE_REQ_FILL_MEMORY                UINT32_C(0x0000315b)
/** Query memory properties. */
#define KD_PACKET_MANIPULATE_REQ_QUERY_MEMORY               UINT32_C(0x0000315c)
/** @todo 0x315d, 0x315e */
/** Get context extended request. */
#define KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX             UINT32_C(0x0000315f)
/** @todo 0x3160 */
/** Maximum available request (exclusive). */
#define KD_PACKET_MANIPULATE_REQ_MAX                        UINT32_C(0x00003161)
/** @} */

/**
 * KD stub receive state.
 */
typedef enum KDRECVSTATE
{
    /** Invalid state. */
    KDRECVSTATE_INVALID = 0,
    /** Receiving the first byte of the packet header. */
    KDRECVSTATE_PACKET_HDR_FIRST_BYTE,
    /** Receiving the second byte of the packet header. */
    KDRECVSTATE_PACKET_HDR_SECOND_BYTE,
    /** Receiving the header. */
    KDRECVSTATE_PACKET_HDR,
    /** Receiving the packet body. */
    KDRECVSTATE_PACKET_BODY,
    /** Receiving the trailing byte. */
    KDRECVSTATE_PACKET_TRAILER,
    /** Blow up the enum to 32bits for easier alignment of members in structs. */
    KDRECVSTATE_32BIT_HACK = 0x7fffffff
} KDRECVSTATE;


/**
 * KD emulated hardware breakpoint.
 */
typedef struct KDCTXHWBP
{
    /** The DBGF breakpoint handle if active, NIL_DBGFBP if not active. */
    DBGFBP                      hDbgfBp;
    /** The linear address of the breakpoint if active. */
    RTGCPTR                     GCPtrBp;
    /** Access type of the breakpoint, see X86_DR7_RW_*. */
    uint8_t                     fAcc;
    /** Length flags of the breakpoint. */
    uint8_t                     fLen;
    /** Flag whether it is a local breakpoint. */
    bool                        fLocal;
    /** Flag whether it is a global breakpoint. */
    bool                        fGlobal;
    /** Flag whether the breakpoint has triggered since the last time of the reset. */
    bool                        fTriggered;
} KDCTXHWBP;
/** Pointer to an emulated hardware breakpoint. */
typedef KDCTXHWBP *PKDCTXHWBP;
/** Pointer to a const emulated hardware breakpoint. */
typedef const KDCTXHWBP *PCKDCTXHWBP;


/**
 * KD context data.
 */
typedef struct KDCTX
{
    /** Internal debugger console data. */
    DBGC                        Dbgc;
    /** Number of bytes received left for the current state. */
    size_t                      cbRecvLeft;
    /** Pointer where to write the next received data. */
    uint8_t                     *pbRecv;
    /** The current state when receiving a new packet. */
    KDRECVSTATE                 enmState;
    /** The timeout waiting for new data. */
    RTMSINTERVAL                msRecvTimeout;
    /** Timestamp when we last received data from the remote end. */
    uint64_t                    tsRecvLast;
    /** Packet header being received. */
    union
    {
        KDPACKETHDR             Fields;
        uint8_t                 ab[16];
    } PktHdr;
    /** The next packet ID to send. */
    uint32_t                    idPktNext;
    /** Offset into the body receive buffer. */
    size_t                      offBodyRecv;
    /** Body data. */
    uint8_t                     abBody[_4K];
    /** The trailer byte storage. */
    uint8_t                     bTrailer;
    /** Flag whether a breakin packet was received since the last time it was reset. */
    bool                        fBreakinRecv;
    /** Flag whether we entered the native VBox hypervisor through a bugcheck request. */
    bool                        fInVBoxDbg;

    /** Emulated hardware breakpoint handling. */
    KDCTXHWBP                   aHwBp[4];
    /** Flag whether a single step completed since last time this was cleared. */
    bool                        fSingleStepped;

    /** Pointer to the OS digger WinNt interface if a matching guest was detected. */
    PDBGFOSIWINNT               pIfWinNt;
    /** Flag whether the detected guest is 32bit (false if 64bit). */
    bool                        f32Bit;
} KDCTX;
/** Pointer to the KD context data. */
typedef KDCTX *PKDCTX;
/** Pointer to const KD context data. */
typedef const KDCTX *PCKDCTX;
/** Pointer to a KD context data pointer. */
typedef PKDCTX *PPKDCTX;


/**
 * Register mapping descriptor.
 */
typedef struct KDREGDESC
{
    /** The DBGF register enum. */
    DBGFREG                     enmReg;
    /** Register width. */
    DBGFREGVALTYPE              enmValType;
    /** The offset into the context structure where the value ends up. */
    uintptr_t                   offReg;
} KDREGDESC;
/** Pointer to a register mapping structure. */
typedef KDREGDESC *PKDREGDESC;
/** Pointer to a const register mapping structure. */
typedef const KDREGDESC *PCKDREGDESC;


/** Creates a possibly sign extended guest context pointer which is required for 32bit targets. */
#define KD_PTR_CREATE(a_pThis, a_GCPtr) ((a_pThis)->f32Bit && ((a_GCPtr) & RT_BIT_32(31)) ? (a_GCPtr) | UINT64_C(0xffffffff00000000) : (a_GCPtr))
/** Returns the value of a possibly sign extended guest context pointer received for 32bit targets. */
#define KD_PTR_GET(a_pThis, a_GCPtr) ((a_pThis)->f32Bit ? (a_GCPtr) & ~UINT64_C(0xffffffff00000000) : (a_GCPtr))


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/

/** 64bit control register set. */
static const KDREGDESC g_aRegsCtrl64[] =
{
    { DBGFREG_CS,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegCs)             },
    { DBGFREG_SS,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegSs)             },
    { DBGFREG_RIP,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRip)            },
    { DBGFREG_RSP,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRsp)            },
    { DBGFREG_RBP,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRbp)            },
    { DBGFREG_EFLAGS,       DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT64, u32RegEflags)         }
};


/** 64bit integer register set. */
static const KDREGDESC g_aRegsInt64[] =
{
    { DBGFREG_RAX,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRax)            },
    { DBGFREG_RCX,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRcx)            },
    { DBGFREG_RDX,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRdx)            },
    { DBGFREG_RBX,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRbx)            },
    { DBGFREG_RSI,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRsi)            },
    { DBGFREG_RDI,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegRdi)            },
    { DBGFREG_R8,           DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR8)             },
    { DBGFREG_R9,           DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR9)             },
    { DBGFREG_R10,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR10)            },
    { DBGFREG_R11,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR11)            },
    { DBGFREG_R12,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR12)            },
    { DBGFREG_R13,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR13)            },
    { DBGFREG_R14,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR14)            },
    { DBGFREG_R15,          DBGFREGVALTYPE_U64,     RT_UOFFSETOF(NTCONTEXT64, u64RegR15)            }
};


/** 64bit segments register set. */
static const KDREGDESC g_aRegsSegs64[] =
{
    { DBGFREG_DS,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegDs)             },
    { DBGFREG_ES,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegEs)             },
    { DBGFREG_FS,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegFs)             },
    { DBGFREG_GS,           DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, u16SegGs)             }
};


/** 64bit floating point register set. */
static const KDREGDESC g_aRegsFx64[] =
{
    { DBGFREG_FCW,          DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FCW)           },
    { DBGFREG_FSW,          DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FSW)           },
    { DBGFREG_FTW,          DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FTW)           },
    { DBGFREG_FOP,          DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FOP)           },
    { DBGFREG_FPUIP,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FPUIP)         },
    /// @todo Fails on Solaris { DBGFREG_FPUCS,        DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.CS)            },
    { DBGFREG_FPUDP,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT64, FxSave.FPUDP)         },
    /// @todo Fails on Solaris { DBGFREG_FPUDS,        DBGFREGVALTYPE_U16,     RT_UOFFSETOF(NTCONTEXT64, FxSave.DS)            },
    { DBGFREG_MXCSR,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT64, FxSave.MXCSR)         },
    { DBGFREG_MXCSR_MASK,   DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT64, FxSave.MXCSR_MASK)    },
    { DBGFREG_ST0,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[0])      },
    { DBGFREG_ST1,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[1])      },
    { DBGFREG_ST2,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[2])      },
    { DBGFREG_ST3,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[3])      },
    { DBGFREG_ST4,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[4])      },
    { DBGFREG_ST5,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[5])      },
    { DBGFREG_ST6,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[6])      },
    { DBGFREG_ST7,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT64, FxSave.aRegs[7])      },
    { DBGFREG_XMM0,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[0])       },
    { DBGFREG_XMM1,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[1])       },
    { DBGFREG_XMM2,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[2])       },
    { DBGFREG_XMM3,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[3])       },
    { DBGFREG_XMM4,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[4])       },
    { DBGFREG_XMM5,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[5])       },
    { DBGFREG_XMM6,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[6])       },
    { DBGFREG_XMM7,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[7])       },
    { DBGFREG_XMM8,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[8])       },
    { DBGFREG_XMM9,         DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[9])       },
    { DBGFREG_XMM10,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[10])      },
    { DBGFREG_XMM11,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[11])      },
    { DBGFREG_XMM12,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[12])      },
    { DBGFREG_XMM13,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[13])      },
    { DBGFREG_XMM14,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[14])      },
    { DBGFREG_XMM15,        DBGFREGVALTYPE_U128,    RT_UOFFSETOF(NTCONTEXT64, FxSave.aXMM[15])      }
};


/** 32bit control register set. */
static const KDREGDESC g_aRegsCtrl32[] =
{
    { DBGFREG_CS,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegCs)             },
    { DBGFREG_SS,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegSs)             },
    { DBGFREG_EIP,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEip)            },
    { DBGFREG_ESP,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEsp)            },
    { DBGFREG_EBP,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEbp)            },
    { DBGFREG_EFLAGS,       DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEflags)         }
};


/** 32bit integer register set. */
static const KDREGDESC g_aRegsInt32[] =
{
    { DBGFREG_EAX,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEax)            },
    { DBGFREG_ECX,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEcx)            },
    { DBGFREG_EDX,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEdx)            },
    { DBGFREG_EBX,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEbx)            },
    { DBGFREG_ESI,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEsi)            },
    { DBGFREG_EDI,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegEdi)            }
};


/** 32bit segments register set. */
static const KDREGDESC g_aRegsSegs32[] =
{
    { DBGFREG_DS,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegDs)             },
    { DBGFREG_ES,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegEs)             },
    { DBGFREG_FS,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegFs)             },
    { DBGFREG_GS,           DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32SegGs)             }
};


/** 32bit debug register set. */
static const KDREGDESC g_aRegsDbg32[] =
{
    { DBGFREG_DR0,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr0)            },
    { DBGFREG_DR1,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr1)            },
    { DBGFREG_DR2,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr2)            },
    { DBGFREG_DR3,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr3)            },
    { DBGFREG_DR6,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr6)            },
    { DBGFREG_DR7,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, u32RegDr7)            }
};


/** 32bit floating point register set. */
static const KDREGDESC g_aRegsFx32[] =
{
    { DBGFREG_FCW,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32CtrlWord)  },
    { DBGFREG_FSW,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32StatusWord)},
    { DBGFREG_FTW,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32TagWord)   },
    { DBGFREG_FCW,          DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32CtrlWord)  },
    { DBGFREG_FPUIP,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32ErrorOff)  },
    { DBGFREG_FPUCS,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32ErrorSel)  },
    { DBGFREG_FPUDS,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32DataOff)   },
    { DBGFREG_FPUDS,        DBGFREGVALTYPE_U32,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.u32DataSel)   },
    { DBGFREG_ST0,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[0])  },
    { DBGFREG_ST1,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[1])  },
    { DBGFREG_ST2,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[2])  },
    { DBGFREG_ST3,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[3])  },
    { DBGFREG_ST4,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[4])  },
    { DBGFREG_ST5,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[5])  },
    { DBGFREG_ST6,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[6])  },
    { DBGFREG_ST7,          DBGFREGVALTYPE_R80,     RT_UOFFSETOF(NTCONTEXT32, FloatSave.aFpuRegs[7])  }
};


/*********************************************************************************************************************************
*   Internal Functions                                                                                                           *
*********************************************************************************************************************************/
static void dbgcKdCtxMsgSend(PKDCTX pThis, bool fWarning, const char *pszMsg);


#ifdef LOG_ENABLED
/**
 * Returns a human readable string of the given packet sub type.
 *
 * @returns Pointer to sub type string.
 * @param   u16SubType          The sub type to convert to a string.
 */
static const char *dbgcKdPktDumpSubTypeToStr(uint16_t u16SubType)
{
    switch (u16SubType)
    {
        case KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE32:     return "StateChange32";
        case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE:   return "Manipulate";
        case KD_PACKET_HDR_SUB_TYPE_DEBUG_IO:           return "DebugIo";
        case KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE:        return "Ack";
        case KD_PACKET_HDR_SUB_TYPE_RESEND:             return "Resend";
        case KD_PACKET_HDR_SUB_TYPE_RESET:              return "Reset";
        case KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64:     return "StateChange64";
        case KD_PACKET_HDR_SUB_TYPE_POLL_BREAKIN:       return "PollBreakin";
        case KD_PACKET_HDR_SUB_TYPE_TRACE_IO:           return "TraceIo";
        case KD_PACKET_HDR_SUB_TYPE_CONTROL_REQUEST:    return "ControlRequest";
        case KD_PACKET_HDR_SUB_TYPE_FILE_IO:            return "FileIo";
        default:                                        break;
    }

    return "<UNKNOWN>";
}


/**
 * Returns a human readable string of the given manipulate request ID.
 *
 * @returns Human readable string (read only).
 * @param   idReq               Request ID (API number in KD speak).
 */
static const char *dbgcKdPktDumpManipulateReqToStr(uint32_t idReq)
{
    switch (idReq)
    {
        case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM:            return "ReadVirtMem";
        case KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM:           return "WriteVirtMem";
        case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT:              return "GetContext";
        case KD_PACKET_MANIPULATE_REQ_SET_CONTEXT:              return "SetContext";
        case KD_PACKET_MANIPULATE_REQ_WRITE_BKPT:               return "WriteBkpt";
        case KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT:             return "RestoreBkpt";
        case KD_PACKET_MANIPULATE_REQ_CONTINUE:                 return "Continue";
        case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE:          return "ReadCtrlSpace";
        case KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE:         return "WriteCtrlSpace";
        case KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE:            return "ReadIoSpace";
        case KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE:           return "WriteIoSpace";
        case KD_PACKET_MANIPULATE_REQ_REBOOT:                   return "Reboot";
        case KD_PACKET_MANIPULATE_REQ_CONTINUE2:                return "Continue2";
        case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM:            return "ReadPhysMem";
        case KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM:           return "WritePhysMem";
        case KD_PACKET_MANIPULATE_REQ_QUERY_SPEC_CALLS:         return "QuerySpecCalls";
        case KD_PACKET_MANIPULATE_REQ_SET_SPEC_CALLS:           return "SetSpecCalls";
        case KD_PACKET_MANIPULATE_REQ_CLEAR_SPEC_CALLS:         return "ClrSpecCalls";
        case KD_PACKET_MANIPULATE_REQ_SET_INTERNAL_BKPT:        return "SetIntBkpt";
        case KD_PACKET_MANIPULATE_REQ_GET_INTERNAL_BKPT:        return "GetIntBkpt";
        case KD_PACKET_MANIPULATE_REQ_READ_IO_SPACE_EX:         return "ReadIoSpaceEx";
        case KD_PACKET_MANIPULATE_REQ_WRITE_IO_SPACE_EX:        return "WriteIoSpaceEx";
        case KD_PACKET_MANIPULATE_REQ_GET_VERSION:              return "GetVersion";
        case KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT:  return "ClrAllIntBkpt";
        case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX:           return "GetContextEx";
        case KD_PACKET_MANIPULATE_REQ_QUERY_MEMORY:             return "QueryMemory";
        case KD_PACKET_MANIPULATE_REQ_CAUSE_BUGCHECK:           return "CauseBugCheck";
        case KD_PACKET_MANIPULATE_REQ_SWITCH_PROCESSOR:         return "SwitchProcessor";
        case KD_PACKET_MANIPULATE_REQ_SEARCH_MEMORY:            return "SearchMemory";
        default:                                                break;
    }

    return "<UNKNOWN>";
}


/**
 * Dumps the content of a manipulate packet.
 *
 * @param   pSgBuf              S/G buffer containing the manipulate packet payload.
 */
static void dbgcKdPktDumpManipulate(PRTSGBUF pSgBuf)
{
    KDPACKETMANIPULATEHDR Hdr;
    size_t cbCopied = RTSgBufCopyToBuf(pSgBuf, &Hdr, sizeof(Hdr));

    if (cbCopied == sizeof(Hdr))
    {
        const char *pszReq = dbgcKdPktDumpManipulateReqToStr(Hdr.idReq);

        Log3(("    MANIPULATE(%#x (%s), %#x, %u, %#x)\n",
              Hdr.idReq, pszReq, Hdr.u16CpuLvl, Hdr.idCpu, Hdr.u32NtStatus));

        switch (Hdr.idReq)
        {
            case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM:
            case KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM:
            case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM:
            case KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM:
            {
                KDPACKETMANIPULATE_XFERMEM64 XferMem64;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &XferMem64, sizeof(XferMem64));
                if (cbCopied == sizeof(XferMem64))
                {
                    Log3(("        u64PtrTarget: %RX64\n"
                          "        cbXferReq:    %RX32\n"
                          "        cbXfered:     %RX32\n",
                          XferMem64.u64PtrTarget, XferMem64.cbXferReq, XferMem64.cbXfered));
                }
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(XferMem64), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT:
            {
                KDPACKETMANIPULATE_RESTOREBKPT64 RestoreBkpt64;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &RestoreBkpt64, sizeof(RestoreBkpt64));
                if (cbCopied == sizeof(RestoreBkpt64))
                    Log3(("        u32HndBkpt:   %RX32\n", RestoreBkpt64.u32HndBkpt));
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(RestoreBkpt64), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_WRITE_BKPT:
            {
                KDPACKETMANIPULATE_WRITEBKPT64 WriteBkpt64;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &WriteBkpt64, sizeof(WriteBkpt64));
                if (cbCopied == sizeof(WriteBkpt64))
                    Log3(("        u64PtrBkpt:   %RX64\n"
                          "        u32HndBkpt:   %RX32\n",
                          WriteBkpt64.u64PtrBkpt, WriteBkpt64.u32HndBkpt));
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(WriteBkpt64), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_CONTINUE:
            {
                KDPACKETMANIPULATE_CONTINUE Continue;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &Continue, sizeof(Continue));
                if (cbCopied == sizeof(Continue))
                    Log3(("        u32NtContSts: %RX32\n", Continue.u32NtContSts));
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(Continue), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_CONTINUE2:
            {
                KDPACKETMANIPULATE_CONTINUE2 Continue;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &Continue, sizeof(Continue));
                if (cbCopied == sizeof(Continue))
                    Log3(("        u32NtContSts: %RX32\n"
                          "        fTrace:       %RX32\n",
                          Continue.u32NtContSts, Continue.fTrace));
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(Continue), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE:
            case KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE:
            {
                KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace64;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &XferCtrlSpace64, sizeof(XferCtrlSpace64));
                if (cbCopied == sizeof(XferCtrlSpace64))
                {
                    Log3(("        u64IdXfer:    %RX64\n"
                          "        cbXferReq:    %RX32\n"
                          "        cbXfered:     %RX32\n",
                          XferCtrlSpace64.u64IdXfer, XferCtrlSpace64.cbXferReq, XferCtrlSpace64.cbXfered));
                }
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(XferCtrlSpace64), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX:
            {
                KDPACKETMANIPULATE_CONTEXTEX GetContextEx;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &GetContextEx, sizeof(GetContextEx));
                if (cbCopied == sizeof(GetContextEx))
                {
                    Log3(("        offStart:     %RX32\n"
                          "        cbXferReq:    %RX32\n"
                          "        cbXfered:     %RX32\n",
                          GetContextEx.offStart, GetContextEx.cbXfer, GetContextEx.cbXfered));
                }
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(GetContextEx), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_QUERY_MEMORY:
            {
                KDPACKETMANIPULATE_QUERYMEMORY QueryMemory;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &QueryMemory, sizeof(QueryMemory));
                if (cbCopied == sizeof(QueryMemory))
                {
                    Log3(("        u64GCPtr:     %RX64\n"
                          "        u32AddrSpace: %RX32\n"
                          "        u32Flags:     %RX32\n",
                          QueryMemory.u64GCPtr, QueryMemory.u32AddrSpace, QueryMemory.u32Flags));
                }
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(QueryMemory), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_SEARCH_MEMORY:
            {
                KDPACKETMANIPULATE_SEARCHMEMORY SearchMemory;
                cbCopied = RTSgBufCopyToBuf(pSgBuf, &SearchMemory, sizeof(SearchMemory));
                if (cbCopied == sizeof(SearchMemory))
                {
                    Log3(("        u64GCPtr:     %RX64\n"
                          "        cbSearch:     %RX64\n"
                          "        cbPattern:    %RX32\n",
                          SearchMemory.u64GCPtr, SearchMemory.cbSearch, SearchMemory.cbPattern));
                }
                else
                    Log3(("        Payload to small, expected %u, got %zu\n", sizeof(SearchMemory), cbCopied));
                break;
            }
            case KD_PACKET_MANIPULATE_REQ_SWITCH_PROCESSOR:
            default:
                break;
        }
    }
    else
        Log3(("    MANIPULATE(Header too small, expected %u, got %zu)\n", sizeof(Hdr), cbCopied));
}


/**
 * Dumps the received packet to the debug log.
 *
 * @returns VBox status code.
 * @param   pPktHdr             The packet header to dump.
 * @param   fRx                 Flag whether the packet was received (false indicates an outgoing packet).
 */
static void dbgcKdPktDump(PCKDPACKETHDR pPktHdr, PCRTSGSEG paSegs, uint32_t cSegs, bool fRx)
{
    RTSGBUF SgBuf;

    RTSgBufInit(&SgBuf, paSegs, cSegs);

    Log3(("%s KDPKTHDR(%#x, %#x (%s), %u, %#x, %#x)\n",
          fRx ? "=>" : "<=",
          pPktHdr->u32Signature, pPktHdr->u16SubType, dbgcKdPktDumpSubTypeToStr(pPktHdr->u16SubType),
          pPktHdr->cbBody, pPktHdr->idPacket, pPktHdr->u32ChkSum));
    switch (pPktHdr->u16SubType)
    {
        case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE:
            dbgcKdPktDumpManipulate(&SgBuf);
            break;
        default:
            break;
    }
}
#endif


/**
 * Resets the emulated hardware breakpoint state to a state similar after a reboot.
 *
 * @param   pThis               The KD context.
 */
static void dbgcKdCtxHwBpReset(PKDCTX pThis)
{
    pThis->fSingleStepped = false;

    for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aHwBp); i++)
    {
        PKDCTXHWBP pBp = &pThis->aHwBp[i];

        if (pBp->hDbgfBp != NIL_DBGFBP)
        {
            int rc = DBGFR3BpClear(pThis->Dbgc.pUVM, pBp->hDbgfBp);
            AssertRC(rc);
        }

        pBp->hDbgfBp    = NIL_DBGFBP;
        pBp->GCPtrBp    = 0;
        pBp->fAcc       = 0;
        pBp->fLen       = 0;
        pBp->fLocal     = false;
        pBp->fGlobal    = false;
        pBp->fTriggered = false;
    }
}


/**
 * Updates the given breakpoint with the given properties.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pBp                 The breakpoint to update.
 * @param   fAcc                Access mode.
 * @param   fLen                Access length.
 * @param   fGlobal             Global breakpoint.
 * @param   fLocal              Local breakpoint.
 * @param   GCPtrBp             Linear address of the breakpoint.
 */
static int dbgcKdCtxHwBpUpdate(PKDCTX pThis, PKDCTXHWBP pBp, uint8_t fAcc, uint8_t fLen,
                               bool fGlobal, bool fLocal, RTGCPTR GCPtrBp)
{
    int rc = VINF_SUCCESS;

    /* Did anything actually change?. */
    if (   pBp->fAcc != fAcc
        || pBp->fLen != fLen
        || pBp->fGlobal != fGlobal
        || pBp->fLocal != fLocal
        || pBp->GCPtrBp != GCPtrBp)
    {
        /* Clear the old breakpoint. */
        if (pBp->hDbgfBp != NIL_DBGFBP)
        {
            rc = DBGFR3BpClear(pThis->Dbgc.pUVM, pBp->hDbgfBp);
            AssertRC(rc);
            pBp->hDbgfBp = NIL_DBGFBP;
        }

        pBp->fAcc    = fAcc;
        pBp->fLen    = fLen;
        pBp->fGlobal = fGlobal;
        pBp->fLocal  = fLocal;
        pBp->GCPtrBp = GCPtrBp;
        if (pBp->fGlobal || pBp->fLocal)
        {
            DBGFADDRESS AddrBp;
            DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrBp, GCPtrBp);

            uint8_t cb = 0;
            switch (pBp->fLen)
            {
                case X86_DR7_LEN_BYTE:
                    cb = 1;
                    break;
                case X86_DR7_LEN_WORD:
                    cb = 2;
                    break;
                case X86_DR7_LEN_DWORD:
                    cb = 4;
                    break;
                case X86_DR7_LEN_QWORD:
                    cb = 8;
                    break;
                default:
                    AssertFailed();
                    return VERR_NET_PROTOCOL_ERROR;
            }

            rc = DBGFR3BpSetReg(pThis->Dbgc.pUVM, &AddrBp, 0 /*iHitTrigger*/, UINT64_MAX /*iHitDisable*/,
                                pBp->fAcc, cb, &pBp->hDbgfBp);
        }
    }

    return rc;
}


/**
 * Updates emulated hardware breakpoints based on the written DR7 value.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   uDr7                The DR7 value which is written.
 */
static int dbgcKdCtxHwBpDr7Update(PKDCTX pThis, uint32_t uDr7)
{
    int rc = VINF_SUCCESS;

    for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aHwBp); i++)
    {
        PKDCTXHWBP pBp = &pThis->aHwBp[i];
        uint8_t fAcc = X86_DR7_GET_RW(uDr7, i);
        uint8_t fLen = X86_DR7_GET_LEN(uDr7, i);
        bool fGlobal = (uDr7 & RT_BIT_32(1 + i * 2)) ? true : false;
        bool fLocal = (uDr7 & RT_BIT_32(i * 2)) ? true : false;

        int rc2 = dbgcKdCtxHwBpUpdate(pThis, pBp, fAcc, fLen, fGlobal, fLocal, pThis->aHwBp[i].GCPtrBp);
        if (   RT_FAILURE(rc2)
            && RT_SUCCESS(rc))
            rc = rc2;
    }

    return rc;
}


/**
 * Updates the linear guest pointer for the given hardware breakpoint.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pBp                 The breakpoint to update.
 * @param   GCPtrBp             The linear breakpoint address.
 */
DECLINLINE(int) dbgcKdCtxHwBpGCPtrUpdate(PKDCTX pThis, PKDCTXHWBP pBp, RTGCPTR GCPtrBp)
{
    return dbgcKdCtxHwBpUpdate(pThis, pBp, pBp->fAcc, pBp->fLen, pBp->fGlobal, pBp->fLocal, GCPtrBp);
}


/**
 * Calculates the DR7 value based on the emulated hardware breakpoint state and returns it.
 *
 * @returns The emulated DR7 value.
 * @param   pThis               The KD context.
 */
static uint32_t dbgcKdCtxHwBpDr7Get(PKDCTX pThis)
{
    uint32_t uDr7 = 0;

    uDr7 |= X86_DR7_RW(0, pThis->aHwBp[0].fAcc);
    uDr7 |= X86_DR7_RW(1, pThis->aHwBp[1].fAcc);
    uDr7 |= X86_DR7_RW(2, pThis->aHwBp[2].fAcc);
    uDr7 |= X86_DR7_RW(3, pThis->aHwBp[3].fAcc);

    uDr7 |= X86_DR7_LEN(0, pThis->aHwBp[0].fLen);
    uDr7 |= X86_DR7_LEN(1, pThis->aHwBp[1].fLen);
    uDr7 |= X86_DR7_LEN(2, pThis->aHwBp[2].fLen);
    uDr7 |= X86_DR7_LEN(3, pThis->aHwBp[3].fLen);

    uDr7 |= pThis->aHwBp[0].fGlobal ? X86_DR7_G(0) : 0;
    uDr7 |= pThis->aHwBp[1].fGlobal ? X86_DR7_G(1) : 0;
    uDr7 |= pThis->aHwBp[2].fGlobal ? X86_DR7_G(2) : 0;
    uDr7 |= pThis->aHwBp[3].fGlobal ? X86_DR7_G(3) : 0;

    uDr7 |= pThis->aHwBp[0].fLocal ? X86_DR7_L(0) : 0;
    uDr7 |= pThis->aHwBp[1].fLocal ? X86_DR7_L(1) : 0;
    uDr7 |= pThis->aHwBp[2].fLocal ? X86_DR7_L(2) : 0;
    uDr7 |= pThis->aHwBp[3].fLocal ? X86_DR7_L(3) : 0;

    return uDr7;
}


/**
 * Updates emulated hardware breakpoints based on the written DR6 value.
 *
 * @param   pThis               The KD context.
 * @param   uDr6                The DR7 value which is written.
 */
static void dbgcKdCtxHwBpDr6Update(PKDCTX pThis, uint32_t uDr6)
{
    pThis->aHwBp[0].fTriggered = (uDr6 & X86_DR6_B0) ? true : false;
    pThis->aHwBp[1].fTriggered = (uDr6 & X86_DR6_B1) ? true : false;
    pThis->aHwBp[2].fTriggered = (uDr6 & X86_DR6_B2) ? true : false;
    pThis->aHwBp[3].fTriggered = (uDr6 & X86_DR6_B3) ? true : false;
    pThis->fSingleStepped = (uDr6 & X86_DR6_BS) ? true : false;
}


/**
 * Calculates the DR6 value based on the emulated hardware breakpoint state and returns it.
 *
 * @returns The emulated DR6 value.
 * @param   pThis               The KD context.
 */
static uint32_t dbgcKdCtxHwBpDr6Get(PKDCTX pThis)
{
    uint32_t uDr6 = 0;

    if (pThis->aHwBp[0].fTriggered)
        uDr6 |= X86_DR6_B0;
    if (pThis->aHwBp[1].fTriggered)
        uDr6 |= X86_DR6_B1;
    if (pThis->aHwBp[2].fTriggered)
        uDr6 |= X86_DR6_B2;
    if (pThis->aHwBp[3].fTriggered)
        uDr6 |= X86_DR6_B3;
    if (pThis->fSingleStepped)
        uDr6 |= X86_DR6_BS;

    return uDr6;
}


/**
 * Wrapper for the I/O interface write callback.
 *
 * @returns Status code.
 * @param   pThis               The KD context.
 * @param   pvPkt               The packet data to send.
 * @param   cbPkt               Size of the packet in bytes.
 */
DECLINLINE(int) dbgcKdCtxWrite(PKDCTX pThis, const void *pvPkt, size_t cbPkt)
{
    return pThis->Dbgc.pIo->pfnWrite(pThis->Dbgc.pIo, pvPkt, cbPkt, NULL /*pcbWritten*/);
}


/**
 * Queries a given register set and stores it into the given context buffer.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   paRegs              The register set to query.
 * @param   cRegs               Number of entries in the register set.
 * @param   pvCtx               The context buffer to store the data into.
 */
static int dbgcKdCtxQueryRegs(PKDCTX pThis, VMCPUID idCpu, PCKDREGDESC paRegs, uint32_t cRegs, void *pvCtx)
{
    int rc = VINF_SUCCESS;

    for (uint32_t i = 0; i < cRegs && rc == VINF_SUCCESS; i++)
    {
        void *pvStart = (uint8_t *)pvCtx + paRegs[i].offReg;

        switch (paRegs[i].enmValType)
        {
            case DBGFREGVALTYPE_U16:  rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, paRegs[i].enmReg, (uint16_t *)pvStart);    break;
            case DBGFREGVALTYPE_U32:  rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, paRegs[i].enmReg, (uint32_t *)pvStart);    break;
            case DBGFREGVALTYPE_U64:  rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, paRegs[i].enmReg, (uint64_t *)pvStart);    break;
            //case DBGFREGVALTYPE_R80:  rc = DBGFR3RegCpuQueryR80(pThis->Dbgc.pUVM, idCpu, paRegs[i].enmReg, (RTFLOAT80U *)pvStart); break;
            //case DBGFREGVALTYPE_U128: rc = DBGFR3RegCpuQueryU128(pThis->Dbgc.pUVM, idCpu, paRegs[i].enmReg, (PRTUINT128U)pvStart);  break;
            default: AssertMsgFailedBreakStmt(("Register type %u not implemented\n", paRegs[i].enmValType), rc = VERR_NOT_IMPLEMENTED);
        }

        if (   rc == VINF_DBGF_ZERO_EXTENDED_REGISTER
            || (   rc == VINF_DBGF_TRUNCATED_REGISTER
                && paRegs[i].enmReg == DBGFREG_RFLAGS)) /* KD protocol specifies 32bit but is really 64bit. */
            rc = VINF_SUCCESS;
    }

    if (   RT_SUCCESS(rc)
        && rc != VINF_SUCCESS)
        rc = VERR_DBGF_UNSUPPORTED_CAST;

    return rc;
}


/**
 * Fills in the given 64bit NT context structure with the requested values.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pNtCtx              The NT context structure to fill in.
 * @param   fCtxFlags           Combination of NTCONTEXT_F_XXX determining what to fill in.
 */
static int dbgcKdCtxQueryNtCtx64(PKDCTX pThis, VMCPUID idCpu, PNTCONTEXT64 pNtCtx, uint32_t fCtxFlags)
{
    RT_BZERO(pNtCtx, sizeof(*pNtCtx));

    pNtCtx->fContext = NTCONTEXT_F_AMD64;
    int rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_MXCSR, &pNtCtx->u32RegMxCsr);

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_CONTROL)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsCtrl64[0], RT_ELEMENTS(g_aRegsCtrl64), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_CONTROL;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_INTEGER)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsInt64[0], RT_ELEMENTS(g_aRegsInt64), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_INTEGER;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_SEGMENTS)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsSegs64[0], RT_ELEMENTS(g_aRegsSegs64), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_SEGMENTS;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_FLOATING_POINT)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsFx64[0], RT_ELEMENTS(g_aRegsFx64), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_FLOATING_POINT;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_DEBUG)
    {
        /** @todo NTCONTEXT_F_DEBUG */
    }

    return rc;
}


/**
 * Fills in the given 32bit NT context structure with the requested values.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pNtCtx              The NT context structure to fill in.
 * @param   fCtxFlags           Combination of NTCONTEXT_F_XXX determining what to fill in.
 */
static int dbgcKdCtxQueryNtCtx32(PKDCTX pThis, VMCPUID idCpu, PNTCONTEXT32 pNtCtx, uint32_t fCtxFlags)
{
    RT_BZERO(pNtCtx, sizeof(*pNtCtx));

    pNtCtx->fContext = NTCONTEXT_F_X86;

    int rc = VINF_SUCCESS;
    if (fCtxFlags & NTCONTEXT_F_CONTROL)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsCtrl32[0], RT_ELEMENTS(g_aRegsCtrl32), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_CONTROL;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_INTEGER)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsInt32[0], RT_ELEMENTS(g_aRegsInt32), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_INTEGER;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_SEGMENTS)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsSegs32[0], RT_ELEMENTS(g_aRegsSegs32), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_SEGMENTS;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_FLOATING_POINT)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsFx32[0], RT_ELEMENTS(g_aRegsFx32), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_FLOATING_POINT;
    }

    if (   RT_SUCCESS(rc)
        && fCtxFlags & NTCONTEXT_F_DEBUG)
    {
        rc = dbgcKdCtxQueryRegs(pThis, idCpu, &g_aRegsDbg32[0], RT_ELEMENTS(g_aRegsDbg32), pNtCtx);
        if (RT_SUCCESS(rc))
            pNtCtx->fContext |= NTCONTEXT_F_DEBUG;
    }

    return rc;
}


#define KD_REG_INIT(a_pszName, a_enmType, a_ValMember, a_Val) \
    do \
    { \
        aRegsSet[idxReg].pszName = a_pszName; \
        aRegsSet[idxReg].enmType = a_enmType; \
        aRegsSet[idxReg].Val.a_ValMember = a_Val; \
        idxReg++; \
    } while (0)
#define KD_REG_INIT_DTR(a_pszName, a_Base, a_Limit) \
    do \
    { \
        aRegsSet[idxReg].pszName = a_pszName; \
        aRegsSet[idxReg].enmType = DBGFREGVALTYPE_DTR; \
        aRegsSet[idxReg].Val.dtr.u64Base = a_Base; \
        aRegsSet[idxReg].Val.dtr.u32Limit = a_Limit; \
        idxReg++; \
    } while (0)
#define KD_REG_INIT_U16(a_pszName, a_Val) KD_REG_INIT(a_pszName, DBGFREGVALTYPE_U16, u16, a_Val)
#define KD_REG_INIT_U32(a_pszName, a_Val) KD_REG_INIT(a_pszName, DBGFREGVALTYPE_U32, u32, a_Val)
#define KD_REG_INIT_U64(a_pszName, a_Val) KD_REG_INIT(a_pszName, DBGFREGVALTYPE_U64, u64, a_Val)


/**
 * Writes the indicated values from the given context structure to the guests register set.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pNtCtx              The NT context structure to set.
 * @param   fCtxFlags           Combination of NTCONTEXT_F_XXX determining what to set.
 */
static int dbgcKdCtxSetNtCtx64(PKDCTX pThis, VMCPUID idCpu, PCNTCONTEXT64 pNtCtx, uint32_t fCtxFlags)
{
    uint32_t idxReg = 0;
    DBGFREGENTRYNM aRegsSet[64]; /** @todo Verify that this is enough when fully implemented. */

    KD_REG_INIT_U32("mxcsr", pNtCtx->u32RegMxCsr);

    if (fCtxFlags & NTCONTEXT_F_CONTROL)
    {
#if 0 /** @todo CPUM returns VERR_NOT_IMPLEMENTED */
        KD_REG_INIT_U16("cs", pNtCtx->u16SegCs);
        KD_REG_INIT_U16("ss", pNtCtx->u16SegSs);
#endif
        KD_REG_INIT_U64("rip", pNtCtx->u64RegRip);
        KD_REG_INIT_U64("rsp", pNtCtx->u64RegRsp);
        KD_REG_INIT_U64("rbp", pNtCtx->u64RegRbp);
        KD_REG_INIT_U32("rflags", pNtCtx->u32RegEflags);
    }

    if (fCtxFlags & NTCONTEXT_F_INTEGER)
    {
        KD_REG_INIT_U64("rax", pNtCtx->u64RegRax);
        KD_REG_INIT_U64("rcx", pNtCtx->u64RegRcx);
        KD_REG_INIT_U64("rdx", pNtCtx->u64RegRdx);
        KD_REG_INIT_U64("rbx", pNtCtx->u64RegRbx);
        KD_REG_INIT_U64("rsi", pNtCtx->u64RegRsi);
        KD_REG_INIT_U64("rdi", pNtCtx->u64RegRdi);
        KD_REG_INIT_U64("r8", pNtCtx->u64RegR8);
        KD_REG_INIT_U64("r9", pNtCtx->u64RegR9);
        KD_REG_INIT_U64("r10", pNtCtx->u64RegR10);
        KD_REG_INIT_U64("r11", pNtCtx->u64RegR11);
        KD_REG_INIT_U64("r12", pNtCtx->u64RegR12);
        KD_REG_INIT_U64("r13", pNtCtx->u64RegR13);
        KD_REG_INIT_U64("r14", pNtCtx->u64RegR14);
        KD_REG_INIT_U64("r15", pNtCtx->u64RegR15);
    }

    if (fCtxFlags & NTCONTEXT_F_SEGMENTS)
    {
#if 0 /** @todo CPUM returns VERR_NOT_IMPLEMENTED */
        KD_REG_INIT_U16("ds", pNtCtx->u16SegDs);
        KD_REG_INIT_U16("es", pNtCtx->u16SegEs);
        KD_REG_INIT_U16("fs", pNtCtx->u16SegFs);
        KD_REG_INIT_U16("gs", pNtCtx->u16SegGs);
#endif
    }

    if (fCtxFlags & NTCONTEXT_F_FLOATING_POINT)
    {
        /** @todo NTCONTEXT_F_FLOATING_POINT. */
    }

    if (fCtxFlags & NTCONTEXT_F_DEBUG)
        dbgcKdCtxMsgSend(pThis, true /*fWarning*/, "Setting local DR registers does not work!");

    return DBGFR3RegNmSetBatch(pThis->Dbgc.pUVM, idCpu, &aRegsSet[0], idxReg);
}


/**
 * Fills in the given 64bit NT kernel context structure with the requested values.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pKNtCtx             The NT context structure to fill in.
 * @param   fCtxFlags           Combination of NTCONTEXT_F_XXX determining what to fill in.
 */
static int dbgcKdCtxQueryNtKCtx64(PKDCTX pThis, VMCPUID idCpu, PNTKCONTEXT64 pKNtCtx, uint32_t fCtxFlags)
{
    RT_BZERO(pKNtCtx, sizeof(*pKNtCtx));

    int rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR0, &pKNtCtx->u64RegCr0);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR2, &pKNtCtx->u64RegCr2);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR3, &pKNtCtx->u64RegCr3);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR4, &pKNtCtx->u64RegCr4);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR8, &pKNtCtx->u64RegCr8);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_LIMIT, &pKNtCtx->Gdtr.u16Limit);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_BASE, &pKNtCtx->Gdtr.u64PtrBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_LIMIT, &pKNtCtx->Idtr.u16Limit);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_BASE, &pKNtCtx->Idtr.u64PtrBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_TR, &pKNtCtx->u16RegTr);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_LDTR, &pKNtCtx->u16RegLdtr);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_MXCSR, &pKNtCtx->u32RegMxCsr);

    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_GS_BASE, &pKNtCtx->u64MsrGsBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_KERNEL_GS_BASE, &pKNtCtx->u64MsrKernelGsBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K6_STAR, &pKNtCtx->u64MsrStar);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_LSTAR, &pKNtCtx->u64MsrLstar);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_CSTAR, &pKNtCtx->u64MsrCstar);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, idCpu, DBGFREG_MSR_K8_SF_MASK, &pKNtCtx->u64MsrSfMask);
    /** @todo XCR0 */

    /* Get the emulated DR register state. */
    pKNtCtx->u64RegDr0 = pThis->aHwBp[0].GCPtrBp;
    pKNtCtx->u64RegDr1 = pThis->aHwBp[1].GCPtrBp;
    pKNtCtx->u64RegDr2 = pThis->aHwBp[2].GCPtrBp;
    pKNtCtx->u64RegDr3 = pThis->aHwBp[3].GCPtrBp;
    pKNtCtx->u64RegDr6 = dbgcKdCtxHwBpDr6Get(pThis);
    pKNtCtx->u64RegDr7 = dbgcKdCtxHwBpDr7Get(pThis);

    if (RT_SUCCESS(rc))
        rc = dbgcKdCtxQueryNtCtx64(pThis, idCpu, &pKNtCtx->Ctx, fCtxFlags);

    return rc;
}


/**
 * Fills in the given 32bit NT kernel context structure with the requested values.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pKNtCtx             The NT context structure to fill in.
 */
static int dbgcKdCtxQueryNtKCtx32(PKDCTX pThis, VMCPUID idCpu, PNTKCONTEXT32 pKNtCtx)
{
    RT_BZERO(pKNtCtx, sizeof(*pKNtCtx));

    int rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR0, &pKNtCtx->u32RegCr0);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR2, &pKNtCtx->u32RegCr2);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR3, &pKNtCtx->u32RegCr3);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_CR4, &pKNtCtx->u32RegCr4);

    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_LIMIT, &pKNtCtx->Gdtr.u16Limit);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_GDTR_BASE, &pKNtCtx->Gdtr.u32PtrBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_LIMIT, &pKNtCtx->Idtr.u16Limit);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, idCpu, DBGFREG_IDTR_BASE, &pKNtCtx->Idtr.u32PtrBase);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_TR, &pKNtCtx->u16RegTr);
    if (RT_SUCCESS(rc))
        rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, idCpu, DBGFREG_LDTR, &pKNtCtx->u16RegLdtr);

    /* Get the emulated DR register state. */
    pKNtCtx->u32RegDr0 = (uint32_t)pThis->aHwBp[0].GCPtrBp;
    pKNtCtx->u32RegDr1 = (uint32_t)pThis->aHwBp[1].GCPtrBp;
    pKNtCtx->u32RegDr2 = (uint32_t)pThis->aHwBp[2].GCPtrBp;
    pKNtCtx->u32RegDr3 = (uint32_t)pThis->aHwBp[3].GCPtrBp;
    pKNtCtx->u32RegDr6 = dbgcKdCtxHwBpDr6Get(pThis);
    pKNtCtx->u32RegDr7 = dbgcKdCtxHwBpDr7Get(pThis);

    return rc;
}


/**
 * Fills in the given 64bit NT kernel context structure with the requested values.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   idCpu               The CPU to query the context for.
 * @param   pKNtCtx             The NT context structure to fill in.
 * @param   cbSet               How many bytes of the context are valid.
 */
static int dbgcKdCtxSetNtKCtx64(PKDCTX pThis, VMCPUID idCpu, PCNTKCONTEXT64 pKNtCtx, size_t cbSet)
{
    AssertReturn(cbSet >= RT_UOFFSETOF(NTKCONTEXT64, Ctx), VERR_INVALID_PARAMETER);

    uint32_t idxReg = 0;
    DBGFREGENTRYNM aRegsSet[64]; /** @todo Verify that this is enough when fully implemented. */

    KD_REG_INIT_U64("cr0", pKNtCtx->u64RegCr0);
    KD_REG_INIT_U64("cr2", pKNtCtx->u64RegCr2);
    KD_REG_INIT_U64("cr3", pKNtCtx->u64RegCr3);
    KD_REG_INIT_U64("cr4", pKNtCtx->u64RegCr4);
    KD_REG_INIT_U64("cr8", pKNtCtx->u64RegCr8);

    KD_REG_INIT_DTR("gdtr", pKNtCtx->Gdtr.u64PtrBase, pKNtCtx->Gdtr.u16Limit);
    KD_REG_INIT_DTR("idtr", pKNtCtx->Idtr.u64PtrBase, pKNtCtx->Idtr.u16Limit);

#if 0 /** @todo CPUM returns VERR_NOT_IMPLEMENTED */
    KD_REG_INIT_U16("tr", pKNtCtx->u16RegTr);
    KD_REG_INIT_U16("ldtr", pKNtCtx->u16RegLdtr);
#endif
    KD_REG_INIT_U32("mxcsr", pKNtCtx->u32RegMxCsr);

    KD_REG_INIT_U64("msr_gs_base", pKNtCtx->u64MsrGsBase);
    KD_REG_INIT_U64("krnl_gs_base", pKNtCtx->u64MsrKernelGsBase);
    KD_REG_INIT_U64("star", pKNtCtx->u64MsrStar);
    KD_REG_INIT_U64("lstar", pKNtCtx->u64MsrLstar);
    KD_REG_INIT_U64("cstar", pKNtCtx->u64MsrCstar);
    KD_REG_INIT_U64("sf_mask", pKNtCtx->u64MsrSfMask);

    int rc = DBGFR3RegNmSetBatch(pThis->Dbgc.pUVM, idCpu, &aRegsSet[0], idxReg);
    if (   RT_SUCCESS(rc)
        && cbSet > RT_UOFFSETOF(NTKCONTEXT64, Ctx)) /** @todo Probably wrong. */
        rc = dbgcKdCtxSetNtCtx64(pThis, idCpu, &pKNtCtx->Ctx, pKNtCtx->Ctx.fContext);

    if (RT_SUCCESS(rc))
    {
        /* Update emulated hardware breakpoint state. */
        dbgcKdCtxHwBpDr6Update(pThis, (uint32_t)pKNtCtx->u64RegDr6);
        rc = dbgcKdCtxHwBpDr7Update(pThis, (uint32_t)pKNtCtx->u64RegDr7);
        if (RT_SUCCESS(rc))
            rc = dbgcKdCtxHwBpGCPtrUpdate(pThis, &pThis->aHwBp[0], pKNtCtx->u64RegDr0);
        if (RT_SUCCESS(rc))
            rc = dbgcKdCtxHwBpGCPtrUpdate(pThis, &pThis->aHwBp[1], pKNtCtx->u64RegDr1);
        if (RT_SUCCESS(rc))
            rc = dbgcKdCtxHwBpGCPtrUpdate(pThis, &pThis->aHwBp[2], pKNtCtx->u64RegDr2);
        if (RT_SUCCESS(rc))
            rc = dbgcKdCtxHwBpGCPtrUpdate(pThis, &pThis->aHwBp[3], pKNtCtx->u64RegDr3);
    }

    return rc;
}

#undef KD_REG_INIT_64
#undef KD_REG_INIT_32
#undef KD_REG_INIT_16
#undef KD_REG_INIT_DTR
#undef KD_REG_INIT


/**
 * Validates the given KD packet header.
 *
 * @returns Flag whether the packet header is valid, false if invalid.
 * @param   pPktHdr             The packet header to validate.
 */
static bool dbgcKdPktHdrValidate(PCKDPACKETHDR pPktHdr)
{
    if (   pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_DATA
        && pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_CONTROL
        && pPktHdr->u32Signature != KD_PACKET_HDR_SIGNATURE_BREAKIN)
        return false;

    if (pPktHdr->u16SubType >= KD_PACKET_HDR_SUB_TYPE_MAX)
        return false;

    uint32_t idPacket = pPktHdr->idPacket & UINT32_C(0xfffffffe);
    if (   idPacket != KD_PACKET_HDR_ID_INITIAL
        && idPacket != KD_PACKET_HDR_ID_RESET
        && idPacket != 0 /* Happens on the very first packet */)
        return false;

    return true;
}


/**
 * Generates a checksum from the given buffer.
 *
 * @returns Generated checksum.
 * @param   pv                  The data to generate a checksum from.
 * @param   cb                  Number of bytes to checksum.
 */
static uint32_t dbgcKdPktChkSumGen(const void *pv, size_t cb)
{
    const uint8_t *pb = (const uint8_t *)pv;
    uint32_t u32ChkSum = 0;

    while (cb--)
        u32ChkSum += *pb++;

    return u32ChkSum;
}


/**
 * Generates a checksum from the given segments.
 *
 * @returns Generated checksum.
 * @param   paSegs              Pointer to the array of segments containing the data.
 * @param   cSegs               Number of segments.
 * @param   pcbChkSum           Where to store the number of bytes checksummed, optional.
 */
static uint32_t dbgcKdPktChkSumGenSg(PCRTSGSEG paSegs, uint32_t cSegs, size_t *pcbChkSum)
{
    size_t cbChkSum = 0;
    uint32_t u32ChkSum = 0;

    for (uint32_t i = 0; i < cSegs; i++)
    {
        u32ChkSum += dbgcKdPktChkSumGen(paSegs[i].pvSeg, paSegs[i].cbSeg);
        cbChkSum += paSegs[i].cbSeg;
    }

    if (pcbChkSum)
        *pcbChkSum = cbChkSum;

    return u32ChkSum;
}


/**
 * Waits for an acknowledgment.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   msWait              Maximum number of milliseconds to wait for an acknowledge.
 * @param   pfResend            Where to store the resend requested flag on success.
 */
static int dbgcKdCtxPktWaitForAck(PKDCTX pThis, RTMSINTERVAL msWait, bool *pfResend)
{
    KDPACKETHDR PktAck;
    uint8_t *pbCur = (uint8_t *)&PktAck;
    size_t cbLeft = sizeof(PktAck);
    uint64_t tsStartMs = RTTimeMilliTS();
    int rc = VINF_SUCCESS;

    LogFlowFunc(("pThis=%p msWait=%u pfResend=%p\n", pThis, msWait, pfResend));

    RT_ZERO(PktAck);

    /* There might be breakin packets in the queue, read until we get something else. */
    while (   msWait
           && RT_SUCCESS(rc))
    {
        if (pThis->Dbgc.pIo->pfnInput(pThis->Dbgc.pIo, msWait))
        {
            size_t cbRead = 0;
            rc = pThis->Dbgc.pIo->pfnRead(pThis->Dbgc.pIo, pbCur, 1, &cbRead);
            if (   RT_SUCCESS(rc)
                && cbRead == 1)
            {
                uint64_t tsSpanMs = RTTimeMilliTS() - tsStartMs;
                msWait -= RT_MIN(msWait, tsSpanMs);
                tsStartMs = RTTimeMilliTS();

                if (*pbCur == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
                    pThis->fBreakinRecv = true;
                else
                {
                    pbCur++;
                    cbLeft--;
                    break;
                }
            }
        }
        else
            rc = VERR_TIMEOUT;
    }

    if (   RT_SUCCESS(rc)
        && !msWait)
        rc = VERR_TIMEOUT;

    if (RT_SUCCESS(rc))
    {
        while (   msWait
               && RT_SUCCESS(rc)
               && cbLeft)
        {
            if (pThis->Dbgc.pIo->pfnInput(pThis->Dbgc.pIo, msWait))
            {
                size_t cbRead = 0;
                rc = pThis->Dbgc.pIo->pfnRead(pThis->Dbgc.pIo, pbCur, cbLeft, &cbRead);
                if (RT_SUCCESS(rc))
                {
                    uint64_t tsSpanMs = RTTimeMilliTS() - tsStartMs;
                    msWait -= RT_MIN(msWait, tsSpanMs);
                    tsStartMs = RTTimeMilliTS();

                    cbLeft -= cbRead;
                    pbCur  += cbRead;
                }
            }
            else
                rc = VERR_TIMEOUT;
        }

        if (RT_SUCCESS(rc))
        {
            if (PktAck.u32Signature == KD_PACKET_HDR_SIGNATURE_CONTROL)
            {
                if (PktAck.u16SubType == KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE)
                    rc = VINF_SUCCESS;
                else if (PktAck.u16SubType == KD_PACKET_HDR_SUB_TYPE_RESEND)
                {
                    *pfResend = true;
                    rc = VINF_SUCCESS;
                }
                else
                    rc = VERR_NET_PROTOCOL_ERROR;
            }
            else
                rc = VERR_NET_PROTOCOL_ERROR;
        }
    }

    LogFlowFunc(("returns rc=%Rrc *pfResend=%RTbool\n", rc, *pfResend));
    return rc;
}


/**
 * Sends the given packet header and optional segmented body (the trailing byte is sent automatically).
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   u32Signature        The signature to send.
 * @param   u16SubType          The sub type to send.
 * @param   paSegs              Pointer to the array of segments to send in the body, optional.
 * @param   cSegs               Number of segments.
 * @param   fAck                Flag whether to wait for an acknowledge.
 */
static int dbgcKdCtxPktSendSg(PKDCTX pThis, uint32_t u32Signature, uint16_t u16SubType,
                              PCRTSGSEG paSegs, uint32_t cSegs, bool fAck)
{
    int rc = VINF_SUCCESS;
    uint32_t cRetriesLeft = 3;
    uint8_t bTrailer = KD_PACKET_TRAILING_BYTE;
    KDPACKETHDR Hdr;

    size_t cbChkSum = 0;
    uint32_t u32ChkSum = dbgcKdPktChkSumGenSg(paSegs, cSegs, &cbChkSum);

    Hdr.u32Signature = u32Signature;
    Hdr.u16SubType   = u16SubType;
    Hdr.cbBody       = (uint16_t)cbChkSum;
    Hdr.idPacket     = pThis->idPktNext;
    Hdr.u32ChkSum    = u32ChkSum;

#ifdef LOG_ENABLED
    dbgcKdPktDump(&Hdr, paSegs, cSegs, false /*fRx*/);
#endif

    while (cRetriesLeft--)
    {
        bool fResend = false;

        if (pThis->Dbgc.pIo->pfnPktBegin)
        {
            rc = pThis->Dbgc.pIo->pfnPktBegin(pThis->Dbgc.pIo, 0 /*cbPktHint*/);
            AssertRC(rc);
        }

        rc = dbgcKdCtxWrite(pThis, &Hdr, sizeof(Hdr));
        if (   RT_SUCCESS(rc)
            && paSegs
            && cSegs)
        {
            for (uint32_t i = 0; i < cSegs && RT_SUCCESS(rc); i++)
                rc = dbgcKdCtxWrite(pThis, paSegs[i].pvSeg, paSegs[i].cbSeg);

            if (RT_SUCCESS(rc))
                rc = dbgcKdCtxWrite(pThis, &bTrailer, sizeof(bTrailer));
        }

        if (   RT_SUCCESS(rc)
            && pThis->Dbgc.pIo->pfnPktEnd)
            rc = pThis->Dbgc.pIo->pfnPktEnd(pThis->Dbgc.pIo);

        if (RT_SUCCESS(rc))
        {
            if (fAck)
                rc = dbgcKdCtxPktWaitForAck(pThis, 10 * 1000, &fResend);

            if (   RT_SUCCESS(rc)
                && !fResend)
                break;
        }
    }

    return rc;
}


/**
 * Sends the given packet header and optional body (the trailing byte is sent automatically).
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   u32Signature        The signature to send.
 * @param   u16SubType          The sub type to send.
 * @param   pvBody              The body to send, optional.
 * @param   cbBody              Body size in bytes.
 * @param   fAck                Flag whether to wait for an acknowledge.
 */
DECLINLINE(int) dbgcKdCtxPktSend(PKDCTX pThis, uint32_t u32Signature, uint16_t u16SubType,
                                 const void *pvBody, size_t cbBody,
                                 bool fAck)
{
    RTSGSEG Seg;

    Seg.pvSeg = (void *)pvBody;
    Seg.cbSeg = cbBody;
    return dbgcKdCtxPktSendSg(pThis, u32Signature, u16SubType, cbBody ? &Seg : NULL, cbBody ? 1 : 0, fAck);
}


/**
 * Sends a resend packet answer.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
DECLINLINE(int) dbgcKdCtxPktSendResend(PKDCTX pThis)
{
    return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_RESEND,
                            NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
}


/**
 * Sends a resend packet answer.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
DECLINLINE(int) dbgcKdCtxPktSendReset(PKDCTX pThis)
{
    pThis->idPktNext = KD_PACKET_HDR_ID_INITIAL;
    return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_RESET,
                            NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
}


/**
 * Sends an acknowledge packet answer.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
DECLINLINE(int) dbgcKdCtxPktSendAck(PKDCTX pThis)
{
    return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_CONTROL, KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE,
                            NULL /*pvBody*/, 0 /*cbBody*/, false /*fAck*/);
}


/**
 * Resets the packet receive state machine.
 *
 * @param   pThis               The KD context.
 */
static void dbgcKdCtxPktRecvReset(PKDCTX pThis)
{
    pThis->enmState       = KDRECVSTATE_PACKET_HDR_FIRST_BYTE;
    pThis->pbRecv         = &pThis->PktHdr.ab[0];
    pThis->cbRecvLeft     = sizeof(pThis->PktHdr.ab[0]);
    pThis->msRecvTimeout  = RT_INDEFINITE_WAIT;
    pThis->tsRecvLast     = RTTimeMilliTS();
}


/**
 * Sends a Debug I/O string packet.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context data.
 * @param   idCpu               The CPU ID generating this packet.
 * @param   pachChars           The characters to send (ASCII).
 * @param   cbChars             Number of characters to send.
 */
static int dbgcKdCtxDebugIoStrSend(PKDCTX pThis, VMCPUID idCpu, const char *pachChars, size_t cbChars)
{
    KDPACKETDEBUGIO DebugIo;
    RT_ZERO(DebugIo);

    /* Fix your damn log strings if this exceeds 4GB... */
    if (cbChars != (uint32_t)cbChars)
        return VERR_BUFFER_OVERFLOW;

    DebugIo.u32Type     = KD_PACKET_DEBUG_IO_STRING;
    DebugIo.u16CpuLvl   = 0x6;
    DebugIo.idCpu       = (uint16_t)idCpu;
    DebugIo.u.Str.cbStr = (uint32_t)cbChars;

    RTSGSEG aRespSegs[2];

    aRespSegs[0].pvSeg = &DebugIo;
    aRespSegs[0].cbSeg = sizeof(DebugIo);
    aRespSegs[1].pvSeg = (void *)pachChars;
    aRespSegs[1].cbSeg = cbChars;

    int rc = dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_DEBUG_IO,
                                &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
    if (RT_SUCCESS(rc))
        pThis->idPktNext ^= 0x1;

    return rc;
}


/**
 * Sends a message to the remotes end.
 *
 * @param   pThis               The KD context data.
 * @param   fWarning            Flag whether this is a warning or an informational message.
 * @param   pszMsg              The message to send.
 */
static void dbgcKdCtxMsgSend(PKDCTX pThis, bool fWarning, const char *pszMsg)
{
    size_t cchMsg = strlen(pszMsg);

    KDPACKETDEBUGIO DebugIo;
    RT_ZERO(DebugIo);

    DebugIo.u32Type     = KD_PACKET_DEBUG_IO_STRING;
    DebugIo.u16CpuLvl   = 0x6;
    DebugIo.idCpu       = 0;

    RTSGSEG aRespSegs[5];

    aRespSegs[0].pvSeg = &DebugIo;
    aRespSegs[0].cbSeg = sizeof(DebugIo);
    aRespSegs[1].pvSeg = (void *)"VBoxDbg ";
    aRespSegs[1].cbSeg = sizeof("VBoxDbg ") - 1;
    if (fWarning)
    {
        aRespSegs[2].pvSeg = (void *)"WARNING ";
        aRespSegs[2].cbSeg = sizeof("WARNING ") - 1;
    }
    else
    {
        aRespSegs[2].pvSeg = (void *)"INFO ";
        aRespSegs[2].cbSeg = sizeof("INFO ") - 1;
    }
    aRespSegs[3].pvSeg = (void *)pszMsg;
    aRespSegs[3].cbSeg = cchMsg;
    aRespSegs[4].pvSeg = (void *)"\r\n";
    aRespSegs[4].cbSeg = 2;

    DebugIo.u.Str.cbStr = (uint32_t)(  aRespSegs[1].cbSeg
                                     + aRespSegs[2].cbSeg
                                     + aRespSegs[3].cbSeg
                                     + aRespSegs[4].cbSeg);

    int rc = dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_DEBUG_IO,
                                &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
    if (RT_SUCCESS(rc))
        pThis->idPktNext ^= 0x1;
}


/**
 * Queries some user input from the remotes end.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context data.
 * @param   idCpu               The CPU ID generating this packet.
 * @param   pachPrompt          The prompt to send (ASCII).
 * @param   cbPrompt            Number of characters to send for the prompt.
 * @param   cbResponseMax       Maximum size for the response.
 */
static int dbgcKdCtxDebugIoGetStrSend(PKDCTX pThis, VMCPUID idCpu, const char *pachPrompt, size_t cbPrompt,
                                      size_t cbResponseMax)
{
    KDPACKETDEBUGIO DebugIo;
    RT_ZERO(DebugIo);

    /* Fix your damn log strings if this exceeds 4GB... */
    if (   cbPrompt != (uint32_t)cbPrompt
        || cbResponseMax != (uint32_t)cbResponseMax)
        return VERR_BUFFER_OVERFLOW;

    DebugIo.u32Type           = KD_PACKET_DEBUG_IO_GET_STRING;
    DebugIo.u16CpuLvl         = 0x6;
    DebugIo.idCpu             = (uint16_t)idCpu;
    DebugIo.u.Prompt.cbPrompt = (uint32_t)cbPrompt;
    DebugIo.u.Prompt.cbReturn = (uint32_t)cbResponseMax;

    RTSGSEG aRespSegs[2];

    aRespSegs[0].pvSeg = &DebugIo;
    aRespSegs[0].cbSeg = sizeof(DebugIo);
    aRespSegs[1].pvSeg = (void *)pachPrompt;
    aRespSegs[1].cbSeg = cbPrompt;

    int rc = dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_DEBUG_IO,
                                &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
    if (RT_SUCCESS(rc))
        pThis->idPktNext ^= 0x1;

    return rc;
}


/**
 * Sends a state change event packet.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context data.
 * @param   enmType             The event type.
 */
static int dbgcKdCtxStateChangeSend(PKDCTX pThis, DBGFEVENTTYPE enmType)
{
    LogFlowFunc(("pThis=%p enmType=%u\n", pThis, enmType));

    /* Select the record to send based on the CPU mode. */
    int rc = VINF_SUCCESS;
    KDPACKETSTATECHANGE64 StateChange64;
    RT_ZERO(StateChange64);

    StateChange64.u32StateNew = KD_PACKET_STATE_CHANGE_EXCEPTION;
    StateChange64.u16CpuLvl   = 0x6; /** @todo Figure this one out. */
    StateChange64.idCpu       = pThis->Dbgc.idCpu;
    StateChange64.cCpus       = (uint16_t)DBGFR3CpuGetCount(pThis->Dbgc.pUVM);
    rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_RIP, &StateChange64.u64RipThread);
    if (RT_SUCCESS(rc))
    {
        DBGFADDRESS AddrRip;
        DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrRip, StateChange64.u64RipThread);

        StateChange64.u64RipThread = KD_PTR_CREATE(pThis, StateChange64.u64RipThread);

        /** @todo Properly fill in the exception record. */
        switch (enmType)
        {
            case DBGFEVENT_HALT_DONE:
            case DBGFEVENT_BREAKPOINT:
            case DBGFEVENT_BREAKPOINT_IO:
            case DBGFEVENT_BREAKPOINT_MMIO:
            case DBGFEVENT_BREAKPOINT_HYPER:
                StateChange64.u.Exception.ExcpRec.u32ExcpCode = KD_PACKET_EXCP_CODE_BKPT;
                break;
            case DBGFEVENT_STEPPED:
            case DBGFEVENT_STEPPED_HYPER:
                pThis->fSingleStepped = true; /* For emulation of DR6. */
                StateChange64.u.Exception.ExcpRec.u32ExcpCode = KD_PACKET_EXCP_CODE_SINGLE_STEP;
                break;
            default:
                AssertMsgFailed(("Invalid DBGF event type for state change %d!\n", enmType));
        }

        StateChange64.u.Exception.ExcpRec.cExcpParms = 3;
        StateChange64.u.Exception.u32FirstChance     = 0x1;

        /** @todo Properly fill in the control report. */
        rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DR6, &StateChange64.uCtrlReport.Amd64.u64RegDr6);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU64(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DR7, &StateChange64.uCtrlReport.Amd64.u64RegDr7);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU32(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_RFLAGS, &StateChange64.uCtrlReport.Amd64.u32RegEflags);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_CS, &StateChange64.uCtrlReport.Amd64.u16SegCs);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_DS, &StateChange64.uCtrlReport.Amd64.u16SegDs);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_ES, &StateChange64.uCtrlReport.Amd64.u16SegEs);
        if (RT_SUCCESS(rc))
            rc = DBGFR3RegCpuQueryU16(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGFREG_FS, &StateChange64.uCtrlReport.Amd64.u16SegFs);

        /* Read instruction bytes. */
        StateChange64.uCtrlReport.Amd64.cbInsnStream = sizeof(StateChange64.uCtrlReport.Amd64.abInsn);
        rc = DBGFR3MemRead(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &AddrRip,
                           &StateChange64.uCtrlReport.Amd64.abInsn[0], StateChange64.uCtrlReport.Amd64.cbInsnStream);
        if (RT_SUCCESS(rc))
        {
            pThis->idPktNext = KD_PACKET_HDR_ID_INITIAL;
            rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_CHANGE64,
                                  &StateChange64, sizeof(StateChange64), false /*fAck*/);
        }
    }

    LogFlowFunc(("returns %Rrc\n", rc));
    return rc;
}


/**
 * Processes a get version 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64GetVersion(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATE64 Resp;
    RT_ZERO(Resp);

    /* Fill in the generic part. */
    Resp.Hdr.idReq       = KD_PACKET_MANIPULATE_REQ_GET_VERSION;
    Resp.Hdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    Resp.Hdr.idCpu       = pPktManip->Hdr.idCpu;
    Resp.Hdr.u32NtStatus = NTSTATUS_SUCCESS;

    /* Build our own response in case there is no Windows interface available. */
    uint32_t NtBuildNumber = 0x0f2800; /* Used when there is no NT interface available, which probably breaks symbol loading. */
    bool f32Bit = false;
    if (pThis->pIfWinNt)
    {
        int rc = pThis->pIfWinNt->pfnQueryVersion(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(),
                                                  NULL /*puVersMajor*/, NULL /*puVersMinor*/,
                                                  &NtBuildNumber, &f32Bit);
        if (RT_SUCCESS(rc))
            rc = pThis->pIfWinNt->pfnQueryKernelPtrs(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(),
                                                     &Resp.u.GetVersion.u64PtrKernBase,
                                                     &Resp.u.GetVersion.u64PtrPsLoadedModuleList);
    }

    /* Fill in the request specific part. */
    Resp.u.GetVersion.u16VersMaj             = NtBuildNumber >> 16;
    Resp.u.GetVersion.u16VersMin             = NtBuildNumber & UINT32_C(0xffff);
    Resp.u.GetVersion.u8VersProtocol         = 0x6; /* From a Windows 10 guest. */
    Resp.u.GetVersion.u8VersKdSecondary      = pThis->f32Bit ? 0 : 0x2; /* amd64 has a versioned context (0 and 1 are obsolete). */
    Resp.u.GetVersion.fFlags                 = KD_PACKET_MANIPULATE64_GET_VERSION_F_MP;
    Resp.u.GetVersion.u8MaxPktType           = KD_PACKET_HDR_SUB_TYPE_MAX;
    Resp.u.GetVersion.u8MaxStateChange       = KD_PACKET_STATE_CHANGE_MAX - KD_PACKET_STATE_CHANGE_MIN;
    Resp.u.GetVersion.u8MaxManipulate        = KD_PACKET_MANIPULATE_REQ_MAX - KD_PACKET_MANIPULATE_REQ_MIN;
    Resp.u.GetVersion.u64PtrDebuggerDataList = 0;

    if (f32Bit)
    {
        Resp.u.GetVersion.u16MachineType           = IMAGE_FILE_MACHINE_I386;
        Resp.u.GetVersion.u64PtrKernBase           = KD_PTR_CREATE(pThis, Resp.u.GetVersion.u64PtrKernBase);
        Resp.u.GetVersion.u64PtrPsLoadedModuleList = KD_PTR_CREATE(pThis, Resp.u.GetVersion.u64PtrPsLoadedModuleList);
    }
    else
    {
        Resp.u.GetVersion.u16MachineType = IMAGE_FILE_MACHINE_AMD64;
        Resp.u.GetVersion.fFlags |= KD_PACKET_MANIPULATE64_GET_VERSION_F_PTR64;
    }

    return dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                            &Resp, sizeof(Resp), true /*fAck*/);
}


/**
 * Processes a read memory 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64ReadMem(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_XFERMEM64 XferMem64;
    uint8_t abMem[_4K];
    RT_ZERO(RespHdr); RT_ZERO(XferMem64);

    DBGFADDRESS AddrRead;
    uint32_t cbRead = RT_MIN(sizeof(abMem), pPktManip->u.XferMem.cbXferReq);
    if (pPktManip->Hdr.idReq == KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM)
        DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrRead, KD_PTR_GET(pThis, pPktManip->u.XferMem.u64PtrTarget));
    else
        DBGFR3AddrFromPhys(pThis->Dbgc.pUVM, &AddrRead, KD_PTR_GET(pThis, pPktManip->u.XferMem.u64PtrTarget));

    RTSGSEG aRespSegs[3];
    uint32_t cSegs = 2; /* Gets incremented when read is successful. */
    RespHdr.idReq       = pPktManip->Hdr.idReq;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    XferMem64.u64PtrTarget = pPktManip->u.XferMem.u64PtrTarget;
    XferMem64.cbXferReq    = pPktManip->u.XferMem.cbXferReq;
    XferMem64.cbXfered     = (uint32_t)cbRead;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &XferMem64;
    aRespSegs[1].cbSeg = sizeof(XferMem64);

    int rc = DBGFR3MemRead(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &AddrRead, &abMem[0], cbRead);
    if (RT_SUCCESS(rc))
    {
        cSegs++;
        aRespSegs[2].pvSeg = &abMem[0];
        aRespSegs[2].cbSeg = cbRead;
    }
    else
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], cSegs, true /*fAck*/);
}


/**
 * Processes a write memory 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64WriteMem(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_XFERMEM64 XferMem64;
    RT_ZERO(RespHdr); RT_ZERO(XferMem64);

    DBGFADDRESS AddrWrite;
    const void *pv = &pThis->abBody[sizeof(*pPktManip)]; /* Data comes directly after the manipulate state body. */
    uint32_t cbWrite = RT_MIN(sizeof(pThis->abBody) - sizeof(*pPktManip), pPktManip->u.XferMem.cbXferReq);
    if (pPktManip->Hdr.idReq == KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM)
        DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &AddrWrite, KD_PTR_GET(pThis, pPktManip->u.XferMem.u64PtrTarget));
    else
        DBGFR3AddrFromPhys(pThis->Dbgc.pUVM, &AddrWrite, KD_PTR_GET(pThis, pPktManip->u.XferMem.u64PtrTarget));

    RTSGSEG aRespSegs[2];
    uint32_t cSegs = 2;
    RespHdr.idReq       = pPktManip->Hdr.idReq;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    XferMem64.u64PtrTarget = pPktManip->u.XferMem.u64PtrTarget;
    XferMem64.cbXferReq    = pPktManip->u.XferMem.cbXferReq;
    XferMem64.cbXfered     = (uint32_t)cbWrite;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &XferMem64;
    aRespSegs[1].cbSeg = sizeof(XferMem64);

    int rc = DBGFR3MemWrite(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &AddrWrite, pv, cbWrite);
    if (RT_FAILURE(rc))
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], cSegs, true /*fAck*/);
}


/**
 * Processes a continue request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64Continue(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    RT_NOREF(pPktManip);
    int rc = VINF_SUCCESS;

    /* No response, just resume. */
    if (DBGFR3IsHalted(pThis->Dbgc.pUVM, VMCPUID_ALL))
        rc = DBGFR3Resume(pThis->Dbgc.pUVM, VMCPUID_ALL);

    return rc;
}


/**
 * Processes a continue request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64Continue2(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    int rc = VINF_SUCCESS;

    /* Update DR7. */
    if (pThis->f32Bit)
        rc = dbgcKdCtxHwBpDr7Update(pThis, pPktManip->u.Continue2.u.x86.u32RegDr7);
    else
        rc = dbgcKdCtxHwBpDr7Update(pThis, (uint32_t)pPktManip->u.Continue2.u.amd64.u64RegDr7);

    /* Resume if not single stepping, the single step will get a state change when the VM stepped. */
    if (pPktManip->u.Continue2.fTrace)
    {
        PDBGFADDRESS pStackPop  = NULL;
        RTGCPTR      cbStackPop = 0;
        rc = DBGFR3StepEx(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, DBGF_STEP_F_INTO, NULL,
                          pStackPop, cbStackPop, 1 /*cMaxSteps*/);
    }
    else if (DBGFR3IsHalted(pThis->Dbgc.pUVM, VMCPUID_ALL))
        rc = DBGFR3Resume(pThis->Dbgc.pUVM, VMCPUID_ALL);

    return rc;
}


/**
 * Processes a set context request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64SetContext(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_SETCONTEXT SetContext;
    RT_ZERO(RespHdr); RT_ZERO(SetContext);

    PCNTCONTEXT64 pNtCtx = (PCNTCONTEXT64)&pThis->abBody[sizeof(*pPktManip)]; /* Data comes directly after the manipulate state body. */

    RTSGSEG aRespSegs[2];
    uint32_t cSegs = 2;
    RespHdr.idReq       = pPktManip->Hdr.idReq;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    /** @todo What do these flags mean? Can't be the context state to set because the valid one is
     * in NTCONTEXT64::fContext (observed with WinDbg). */
    SetContext.u32CtxFlags = pPktManip->u.SetContext.u32CtxFlags;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &SetContext;
    aRespSegs[1].cbSeg = sizeof(SetContext);

    int rc = dbgcKdCtxSetNtCtx64(pThis, pPktManip->Hdr.idCpu, pNtCtx, pNtCtx->fContext);
    if (RT_FAILURE(rc))
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], cSegs, true /*fAck*/);
}


/**
 * Processes a read control space 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64ReadCtrlSpace(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace64;
    uint8_t abResp[sizeof(NTKCONTEXT64)];
    uint32_t cbData = 0;
    RT_ZERO(RespHdr); RT_ZERO(XferCtrlSpace64);
    RT_ZERO(abResp);

    RTSGSEG aRespSegs[3];
    uint32_t cSegs = 2; /* Gets incremented when read is successful. */
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    XferCtrlSpace64.u64IdXfer = pPktManip->u.XferCtrlSpace.u64IdXfer;
    XferCtrlSpace64.cbXferReq = pPktManip->u.XferCtrlSpace.cbXferReq;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &XferCtrlSpace64;
    aRespSegs[1].cbSeg = sizeof(XferCtrlSpace64);

    int rc = VINF_SUCCESS;
    if (pThis->f32Bit)
    {
        if (pPktManip->u.XferCtrlSpace.u64IdXfer == sizeof(NTCONTEXT32))
        {
            /* Queries the kernel context. */
            rc = dbgcKdCtxQueryNtKCtx32(pThis, RespHdr.idCpu, (PNTKCONTEXT32)&abResp[0]);
            if (RT_SUCCESS(rc))
                cbData = sizeof(NTKCONTEXT32);
        }
    }
    else
    {
        switch (pPktManip->u.XferCtrlSpace.u64IdXfer)
        {
            case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCR:
            {
                if (pThis->pIfWinNt)
                {
                    RTGCUINTPTR GCPtrKpcr = 0;

                    rc = pThis->pIfWinNt->pfnQueryKpcrForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(), RespHdr.idCpu,
                                                              &GCPtrKpcr, NULL /*pKpcrb*/);
                    if (RT_SUCCESS(rc))
                        memcpy(&abResp[0], &GCPtrKpcr, sizeof(GCPtrKpcr));
                }

                cbData = sizeof(RTGCUINTPTR);
                break;
            }
            case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCRB:
            {
                if (pThis->pIfWinNt)
                {
                    RTGCUINTPTR GCPtrKpcrb = 0;

                    rc = pThis->pIfWinNt->pfnQueryKpcrForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(), RespHdr.idCpu,
                                                              NULL /*pKpcr*/, &GCPtrKpcrb);
                    if (RT_SUCCESS(rc))
                        memcpy(&abResp[0], &GCPtrKpcrb, sizeof(GCPtrKpcrb));
                }

                cbData = sizeof(RTGCUINTPTR);
                break;
            }
            case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KCTX:
            {
                rc = dbgcKdCtxQueryNtKCtx64(pThis, RespHdr.idCpu, (PNTKCONTEXT64)&abResp[0], NTCONTEXT64_F_FULL);
                if (RT_SUCCESS(rc))
                    cbData = sizeof(NTKCONTEXT64);
                break;
            }
            case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KTHRD:
            {
                if (pThis->pIfWinNt)
                {
                    RTGCUINTPTR GCPtrCurThrd = 0;

                    rc = pThis->pIfWinNt->pfnQueryCurThrdForVCpu(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(),
                                                                 RespHdr.idCpu, &GCPtrCurThrd);
                    if (RT_SUCCESS(rc))
                        memcpy(&abResp[0], &GCPtrCurThrd, sizeof(GCPtrCurThrd));
                }

                cbData = sizeof(RTGCUINTPTR);
                break;
            }
            default:
                rc = VERR_NOT_SUPPORTED;
                break;
        }
    }

    if (   RT_SUCCESS(rc)
        && cbData)
    {
        XferCtrlSpace64.cbXfered = RT_MIN(cbData, XferCtrlSpace64.cbXferReq);

        cSegs++;
        aRespSegs[2].pvSeg = &abResp[0];
        aRespSegs[2].cbSeg = cbData;
    }
    else if (RT_FAILURE(rc))
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], cSegs, true /*fAck*/);
}


/**
 * Processes a write control space 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64WriteCtrlSpace(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_XFERCTRLSPACE64 XferCtrlSpace64;
    uint32_t cbData = 0;
    RT_ZERO(RespHdr); RT_ZERO(XferCtrlSpace64);

    RTSGSEG aRespSegs[2];
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    XferCtrlSpace64.u64IdXfer = pPktManip->u.XferCtrlSpace.u64IdXfer;
    XferCtrlSpace64.cbXferReq = pPktManip->u.XferCtrlSpace.cbXferReq;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &XferCtrlSpace64;
    aRespSegs[1].cbSeg = sizeof(XferCtrlSpace64);

    int rc = VINF_SUCCESS;
    switch (pPktManip->u.XferCtrlSpace.u64IdXfer)
    {
        case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KCTX:
        {
            PCNTKCONTEXT64 pNtKCtx = (PCNTKCONTEXT64)&pThis->abBody[sizeof(*pPktManip)]; /* Data comes directly after the manipulate state body. */
            rc = dbgcKdCtxSetNtKCtx64(pThis, RespHdr.idCpu, pNtKCtx, XferCtrlSpace64.cbXferReq);
            if (RT_SUCCESS(rc))
                cbData = RT_MIN(XferCtrlSpace64.cbXferReq, sizeof(NTKCONTEXT64));
            break;
        }
        case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCR:
        case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KPCRB:
        case KD_PACKET_MANIPULATE64_CTRL_SPACE_ID_KTHRD:
        default:
            rc = VERR_NOT_SUPPORTED;
            break;
    }

    if (RT_FAILURE(rc))
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Convert to an appropriate NT status code. */
    else
        XferCtrlSpace64.cbXfered = cbData;

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
}


/**
 * Processes a restore breakpoint 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64RestoreBkpt(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_RESTOREBKPT64 RestoreBkpt64;
    RT_ZERO(RespHdr); RT_ZERO(RestoreBkpt64);

    RTSGSEG aRespSegs[2];
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    RestoreBkpt64.u32HndBkpt = pPktManip->u.RestoreBkpt.u32HndBkpt;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &RestoreBkpt64;
    aRespSegs[1].cbSeg = sizeof(RestoreBkpt64);

    int rc = DBGFR3BpClear(pThis->Dbgc.pUVM, pPktManip->u.RestoreBkpt.u32HndBkpt);
    if (RT_SUCCESS(rc))
    {
        rc = dbgcBpDelete(&pThis->Dbgc, pPktManip->u.RestoreBkpt.u32HndBkpt);
        AssertRC(rc);
    }
    else if (rc != VERR_DBGF_BP_NOT_FOUND)
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL;

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
}


/**
 * Processes a write breakpoint 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64WriteBkpt(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_WRITEBKPT64 WriteBkpt64;
    RT_ZERO(RespHdr); RT_ZERO(WriteBkpt64);

    RTSGSEG aRespSegs[2];
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_WRITE_BKPT;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &WriteBkpt64;
    aRespSegs[1].cbSeg = sizeof(WriteBkpt64);

    WriteBkpt64.u64PtrBkpt = pPktManip->u.WriteBkpt.u64PtrBkpt;

    DBGFADDRESS BpAddr;
    DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &BpAddr, KD_PTR_GET(pThis, pPktManip->u.WriteBkpt.u64PtrBkpt));
    int rc = DBGFR3BpSetInt3(pThis->Dbgc.pUVM, pThis->Dbgc.idCpu, &BpAddr,
                             1 /*iHitTrigger*/, UINT64_MAX /*iHitDisable*/, &WriteBkpt64.u32HndBkpt);
    if (RT_SUCCESS(rc))
    {
        rc = dbgcBpAdd(&pThis->Dbgc, WriteBkpt64.u32HndBkpt, NULL /*pszCmd*/);
        if (RT_FAILURE(rc))
            DBGFR3BpClear(pThis->Dbgc.pUVM, WriteBkpt64.u32HndBkpt);
    }
    else
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL;

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
}


/**
 * Processes a get context extended 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64GetContextEx(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_CONTEXTEX ContextEx;
    union
    {
        NTCONTEXT64 v64;
        NTCONTEXT32 v32;
    } NtCtx;
    RT_ZERO(RespHdr); RT_ZERO(ContextEx); RT_ZERO(NtCtx);

    RTSGSEG aRespSegs[3];
    uint32_t cSegs = 2;
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL;

    ContextEx.offStart = pPktManip->u.ContextEx.offStart;
    ContextEx.cbXfer   = pPktManip->u.ContextEx.cbXfer;
    ContextEx.cbXfered = 0;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &ContextEx;
    aRespSegs[1].cbSeg = sizeof(ContextEx);

    int rc = VINF_SUCCESS;
    uint32_t cbCtx = pThis->f32Bit ? sizeof(NtCtx.v32) : sizeof(NtCtx.v64);
    if (pThis->f32Bit)
        dbgcKdCtxQueryNtCtx32(pThis, pPktManip->Hdr.idCpu, &NtCtx.v32, NTCONTEXT32_F_FULL);
    else
        dbgcKdCtxQueryNtCtx64(pThis, pPktManip->Hdr.idCpu, &NtCtx.v64, NTCONTEXT64_F_FULL);
    if (   RT_SUCCESS(rc)
        && pPktManip->u.ContextEx.offStart < cbCtx)
    {
        RespHdr.u32NtStatus = NTSTATUS_SUCCESS;
        ContextEx.cbXfered = RT_MIN(cbCtx - ContextEx.offStart, ContextEx.cbXfer);

        aRespSegs[2].pvSeg = (uint8_t *)&NtCtx + ContextEx.offStart;
        aRespSegs[2].cbSeg = ContextEx.cbXfered;
        cSegs++;
    }

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], cSegs, true /*fAck*/);
}


/**
 * Processes a query memory 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64QueryMemory(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_QUERYMEMORY QueryMemory;
    RT_ZERO(RespHdr); RT_ZERO(QueryMemory);

    RTSGSEG aRespSegs[2];
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_QUERY_MEMORY;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    /** @todo Need DBGF API to query protection and privilege level from guest page tables. */
    QueryMemory.u64GCPtr     = pPktManip->u.QueryMemory.u64GCPtr;
    QueryMemory.u32AddrSpace = KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_SPACE_KERNEL;
    QueryMemory.u32Flags     =   KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_READ
                               | KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_WRITE
                               | KD_PACKET_MANIPULATE64_QUERY_MEMORY_ADDR_F_EXEC;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &QueryMemory;
    aRespSegs[1].cbSeg = sizeof(QueryMemory);

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
}


/**
 * Processes a search memory 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64SearchMemory(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    KDPACKETMANIPULATEHDR RespHdr;
    KDPACKETMANIPULATE_SEARCHMEMORY SearchMemory;
    RT_ZERO(RespHdr); RT_ZERO(SearchMemory);

    RTSGSEG aRespSegs[2];
    RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_SEARCH_MEMORY;
    RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
    RespHdr.idCpu       = pPktManip->Hdr.idCpu;
    RespHdr.u32NtStatus = NTSTATUS_SUCCESS;

    SearchMemory.u64GCPtr  = pPktManip->u.SearchMemory.u64GCPtr;
    SearchMemory.cbSearch  = pPktManip->u.SearchMemory.cbSearch;
    SearchMemory.cbPattern = pPktManip->u.SearchMemory.cbPattern;

    /* Validate the pattern length and start searching. */
    if (pPktManip->u.SearchMemory.cbPattern < sizeof(pThis->abBody) - sizeof(*pPktManip))
    {
        DBGFADDRESS StartAddress;
        DBGFADDRESS HitAddress;
        VMCPUID idCpu = pPktManip->Hdr.idCpu;
        DBGFR3AddrFromFlat(pThis->Dbgc.pUVM, &StartAddress, pPktManip->u.SearchMemory.u64GCPtr);

        /** @todo WindDbg sends CPU ID 32 sometimes, maybe that means continue search on last used CPU?. */
        if (idCpu >= DBGFR3CpuGetCount(pThis->Dbgc.pUVM))
            idCpu = pThis->Dbgc.idCpu;

        int rc = DBGFR3MemScan(pThis->Dbgc.pUVM, idCpu, &StartAddress, pPktManip->u.SearchMemory.cbSearch, 1,
                               &pThis->abBody[sizeof(*pPktManip)], pPktManip->u.SearchMemory.cbPattern, &HitAddress);
        if (RT_SUCCESS(rc))
            SearchMemory.u64GCPtr = HitAddress.FlatPtr;
        else if (rc == VERR_DBGF_MEM_NOT_FOUND)
            RespHdr.u32NtStatus = NTSTATUS_NOT_FOUND;
        else
            RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL;
    }
    else
        RespHdr.u32NtStatus = NTSTATUS_BUFFER_OVERFLOW;

    aRespSegs[0].pvSeg = &RespHdr;
    aRespSegs[0].cbSeg = sizeof(RespHdr);
    aRespSegs[1].pvSeg = &SearchMemory;
    aRespSegs[1].cbSeg = sizeof(SearchMemory);

    return dbgcKdCtxPktSendSg(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &aRespSegs[0], RT_ELEMENTS(aRespSegs), true /*fAck*/);
}


/**
 * Processes a cause bugcheck 64 request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 *
 * @note We abuse this request to initiate a native VBox debugger command prompt from the remote end
  *      (There is monitor/Rcmd equivalent like with GDB unfortunately).
 */
static int dbgcKdCtxPktManipulate64CauseBugCheck(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    RT_NOREF(pPktManip);
    pThis->fInVBoxDbg = true;
    return dbgcKdCtxDebugIoGetStrSend(pThis, pThis->Dbgc.idCpu, "VBoxDbg>", sizeof("VBoxDbg>") - 1,
                                      512 /*cbResponseMax*/);
}


/**
 * Processes a switch processor request.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 * @param   pPktManip           The manipulate packet request.
 */
static int dbgcKdCtxPktManipulate64SwitchProcessor(PKDCTX pThis, PCKDPACKETMANIPULATE64 pPktManip)
{
    int rc = VINF_SUCCESS;

    if (RT_UNLIKELY(pPktManip->Hdr.idCpu >= DBGFR3CpuGetCount(pThis->Dbgc.pUVM)))
    {
        KDPACKETMANIPULATEHDR RespHdr;
        RT_ZERO(RespHdr);

        RespHdr.idReq       = KD_PACKET_MANIPULATE_REQ_SWITCH_PROCESSOR;
        RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
        RespHdr.idCpu       = pPktManip->Hdr.idCpu;
        RespHdr.u32NtStatus = NTSTATUS_UNSUCCESSFUL; /** @todo Test this path. */
        rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                              &RespHdr, sizeof(RespHdr), true /*fAck*/);
    }
    else
    {
        pThis->Dbgc.idCpu = pPktManip->Hdr.idCpu;
        rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
    }

    return rc;
}


/**
 * Processes a manipulate packet.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
static int dbgcKdCtxPktManipulate64Process(PKDCTX pThis)
{
    int rc = VINF_SUCCESS;
    PCKDPACKETMANIPULATE64 pPktManip = (PCKDPACKETMANIPULATE64)&pThis->abBody[0];

    switch (pPktManip->Hdr.idReq)
    {
        case KD_PACKET_MANIPULATE_REQ_GET_VERSION:
        {
            rc = dbgcKdCtxPktManipulate64GetVersion(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_READ_VIRT_MEM:
        case KD_PACKET_MANIPULATE_REQ_READ_PHYS_MEM:
        {
            rc = dbgcKdCtxPktManipulate64ReadMem(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_WRITE_VIRT_MEM:
        case KD_PACKET_MANIPULATE_REQ_WRITE_PHYS_MEM:
        {
            rc = dbgcKdCtxPktManipulate64WriteMem(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_CONTINUE:
        {
            rc = dbgcKdCtxPktManipulate64Continue(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_CONTINUE2:
        {
            rc = dbgcKdCtxPktManipulate64Continue2(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_SET_CONTEXT:
        {
            rc = dbgcKdCtxPktManipulate64SetContext(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_READ_CTRL_SPACE:
        {
            rc = dbgcKdCtxPktManipulate64ReadCtrlSpace(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_WRITE_CTRL_SPACE:
        {
            rc = dbgcKdCtxPktManipulate64WriteCtrlSpace(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_RESTORE_BKPT:
        {
            rc = dbgcKdCtxPktManipulate64RestoreBkpt(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_WRITE_BKPT:
        {
            rc = dbgcKdCtxPktManipulate64WriteBkpt(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_CLEAR_ALL_INTERNAL_BKPT:
            /* WinDbg doesn't seem to expect an answer apart from the ACK here. */
            break;
        case KD_PACKET_MANIPULATE_REQ_GET_CONTEXT_EX:
        {
            rc = dbgcKdCtxPktManipulate64GetContextEx(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_QUERY_MEMORY:
        {
            rc = dbgcKdCtxPktManipulate64QueryMemory(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_SEARCH_MEMORY:
        {
            rc = dbgcKdCtxPktManipulate64SearchMemory(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_CAUSE_BUGCHECK:
        {
            rc = dbgcKdCtxPktManipulate64CauseBugCheck(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_SWITCH_PROCESSOR:
        {
            rc = dbgcKdCtxPktManipulate64SwitchProcessor(pThis, pPktManip);
            break;
        }
        case KD_PACKET_MANIPULATE_REQ_REBOOT:
        {
            rc = VMR3Reset(pThis->Dbgc.pUVM); /* Doesn't expect an answer here. */
            if (   RT_SUCCESS(rc)
                && DBGFR3IsHalted(pThis->Dbgc.pUVM, VMCPUID_ALL))
                rc = DBGFR3Resume(pThis->Dbgc.pUVM, VMCPUID_ALL);
            break;
        }
        default:
            KDPACKETMANIPULATEHDR RespHdr;
            RT_ZERO(RespHdr);

            RespHdr.idReq       = pPktManip->Hdr.idReq;
            RespHdr.u16CpuLvl   = pPktManip->Hdr.u16CpuLvl;
            RespHdr.idCpu       = pPktManip->Hdr.idCpu;
            RespHdr.u32NtStatus = NTSTATUS_NOT_IMPLEMENTED;
            rc = dbgcKdCtxPktSend(pThis, KD_PACKET_HDR_SIGNATURE_DATA, KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE,
                                  &RespHdr, sizeof(RespHdr), true /*fAck*/);
            break;
    }

    return rc;
}


/**
 * Tries to detect the guest OS running in the VM looking specifically for the Windows NT kind.
 *
 * @param   pThis               The KD context.
 */
static void dbgcKdCtxDetectGstOs(PKDCTX pThis)
{
    pThis->pIfWinNt = NULL;

    /* Try detecting a Windows NT guest. */
    char szName[64];
    int rc = DBGFR3OSDetect(pThis->Dbgc.pUVM, szName, sizeof(szName));
    if (RT_SUCCESS(rc))
    {
        pThis->pIfWinNt = (PDBGFOSIWINNT)DBGFR3OSQueryInterface(pThis->Dbgc.pUVM, DBGFOSINTERFACE_WINNT);
        if (pThis->pIfWinNt)
            LogRel(("DBGC/Kd: Detected Windows NT guest OS (%s)\n", &szName[0]));
        else
            LogRel(("DBGC/Kd: Detected guest OS is not of the Windows NT kind (%s)\n", &szName[0]));
    }
    else
    {
        LogRel(("DBGC/Kd: Unable to detect any guest operating system type, rc=%Rrc\n", rc));
        rc = VINF_SUCCESS; /* Try to continue nevertheless. */
    }

    if (pThis->pIfWinNt)
    {
        rc = pThis->pIfWinNt->pfnQueryVersion(pThis->pIfWinNt, pThis->Dbgc.pUVM, VMMR3GetVTable(),
                                              NULL /*puVersMajor*/, NULL /*puVersMinor*/,
                                              NULL /*puBuildNumber*/, &pThis->f32Bit);
        AssertRC(rc);
    }
    else
    {
        /*
         * Try to detect bitness based on the current CPU mode which might fool us (32bit process running
         * inside of 64bit host).
         */
        CPUMMODE enmMode = DBGCCmdHlpGetCpuMode(&pThis->Dbgc.CmdHlp);
        if (enmMode == CPUMMODE_PROTECTED)
            pThis->f32Bit = true;
        else if (enmMode == CPUMMODE_LONG)
            pThis->f32Bit = false;
        else
            LogRel(("DBGC/Kd: Heh, trying to debug real mode code with WinDbg are we? Good luck with that...\n"));
    }
}


/**
 * Processes a fully received packet.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
static int dbgcKdCtxPktProcess(PKDCTX pThis)
{
    int rc = VINF_SUCCESS;

    pThis->fBreakinRecv = false;

    /* Verify checksum. */
    if (dbgcKdPktChkSumGen(&pThis->abBody[0], pThis->PktHdr.Fields.cbBody) == pThis->PktHdr.Fields.u32ChkSum)
    {
        /** @todo Check packet id. */
        if (pThis->PktHdr.Fields.u16SubType != KD_PACKET_HDR_SUB_TYPE_RESET)
        {
            pThis->idPktNext = pThis->PktHdr.Fields.idPacket;
            rc = dbgcKdCtxPktSendAck(pThis);
        }
        if (RT_SUCCESS(rc))
        {
#ifdef LOG_ENABLED
            RTSGSEG Seg;
            Seg.pvSeg = &pThis->abBody[0];
            Seg.cbSeg = pThis->PktHdr.Fields.cbBody;
            dbgcKdPktDump(&pThis->PktHdr.Fields, &Seg, 1 /*cSegs*/, true /*fRx*/);
#endif

            switch (pThis->PktHdr.Fields.u16SubType)
            {
                case KD_PACKET_HDR_SUB_TYPE_RESET:
                {
                    dbgcKdCtxDetectGstOs(pThis);

                    pThis->idPktNext = 0;
                    rc = dbgcKdCtxPktSendReset(pThis);
                    if (RT_SUCCESS(rc))
                    {
                        rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
                        if (rc == VWRN_DBGF_ALREADY_HALTED)
                            rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
                    }
                    pThis->idPktNext = KD_PACKET_HDR_ID_RESET;
                    break;
                }
                case KD_PACKET_HDR_SUB_TYPE_STATE_MANIPULATE:
                {
                    pThis->idPktNext = pThis->PktHdr.Fields.idPacket ^ 0x1;
                    rc = dbgcKdCtxPktManipulate64Process(pThis);
                    break;
                }
                case KD_PACKET_HDR_SUB_TYPE_ACKNOWLEDGE:
                case KD_PACKET_HDR_SUB_TYPE_RESEND:
                {
                    /* Don't do anything. */
                    rc = VINF_SUCCESS;
                    break;
                }
                case KD_PACKET_HDR_SUB_TYPE_DEBUG_IO:
                {
                    if (pThis->fInVBoxDbg)
                    {
                        pThis->idPktNext = pThis->PktHdr.Fields.idPacket ^ 0x1;
                        /* Get the string and execute it. */
                        PCKDPACKETDEBUGIO pPktDbgIo = (PCKDPACKETDEBUGIO)&pThis->abBody[0];
                        if (   pPktDbgIo->u32Type == KD_PACKET_DEBUG_IO_GET_STRING
                            && pPktDbgIo->u.Prompt.cbReturn <= sizeof(pThis->abBody) - sizeof(*pPktDbgIo) - 1)
                        {
                            if (pPktDbgIo->u.Prompt.cbReturn)
                            {
                                /* Terminate return value. */
                                pThis->abBody[sizeof(*pPktDbgIo) + pPktDbgIo->u.Prompt.cbReturn] = '\0';

                                const char *pszCmd = (const char *)&pThis->abBody[sizeof(*pPktDbgIo)];
                                /* Filter out 'exit' which is handled here directly and exits the debug loop. */
                                if (!strcmp(pszCmd, "exit"))
                                    pThis->fInVBoxDbg = false;
                                else
                                {
                                    rc = pThis->Dbgc.CmdHlp.pfnExec(&pThis->Dbgc.CmdHlp, pszCmd);
                                    if (RT_SUCCESS(rc))
                                        rc = dbgcKdCtxDebugIoGetStrSend(pThis, pThis->Dbgc.idCpu, "VBoxDbg>", sizeof("VBoxDbg>") - 1,
                                                                        512 /*cbResponseMax*/);
                                    else
                                        LogRel(("DBGC/Kd: Executing command \"%s\" failed with rc=%Rrc\n", pszCmd, rc));
                                }
                            }
                            else
                                rc = dbgcKdCtxDebugIoGetStrSend(pThis, pThis->Dbgc.idCpu, "VBoxDbg>", sizeof("VBoxDbg>") - 1,
                                                                512 /*cbResponseMax*/);
                        }
                        else
                            LogRel(("DBGC/Kd: Received invalid DEBUG_IO packet from remote end, ignoring\n"));
                    }
                    else
                        LogRel(("DBGC/Kd: Received out of band DEBUG_IO packet from remote end, ignoring\n"));
                    break;
                }
                default:
                    rc = VERR_NOT_IMPLEMENTED;
            }
        }
    }
    else
    {
        pThis->idPktNext = pThis->PktHdr.Fields.idPacket;
        rc = dbgcKdCtxPktSendResend(pThis);
    }

    if (pThis->fBreakinRecv)
    {
        pThis->fBreakinRecv = false;
        rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
        if (rc == VWRN_DBGF_ALREADY_HALTED)
            rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
    }

    /* Next packet. */
    dbgcKdCtxPktRecvReset(pThis);
    return rc;
}


/**
 * Processes the received data based on the current state.
 *
 * @returns VBox status code.
 * @param   pThis               The KD context.
 */
static int dbgcKdCtxRecvDataProcess(PKDCTX pThis)
{
    int rc = VINF_SUCCESS;

    switch (pThis->enmState)
    {
        case KDRECVSTATE_PACKET_HDR_FIRST_BYTE:
        {
            /* Does it look like a valid packet start?. */
            if (   pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_DATA_BYTE
                || pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_CONTROL_BYTE)
            {
                pThis->pbRecv        = &pThis->PktHdr.ab[1];
                pThis->cbRecvLeft    = sizeof(pThis->PktHdr.ab[1]);
                pThis->enmState      = KDRECVSTATE_PACKET_HDR_SECOND_BYTE;
                pThis->msRecvTimeout = DBGC_KD_RECV_TIMEOUT_MS;
            }
            else if (pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
            {
                rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
                if (rc == VWRN_DBGF_ALREADY_HALTED)
                    rc = dbgcKdCtxStateChangeSend(pThis, DBGFEVENT_HALT_DONE);
                dbgcKdCtxPktRecvReset(pThis);
            }
            else
                dbgcKdCtxPktRecvReset(pThis); /* Reset and continue. */
            break;
        }
        case KDRECVSTATE_PACKET_HDR_SECOND_BYTE:
        {
            /*
             * If the first and second byte differ there might be a single breakin
             * packet byte received and this is actually the start of a new packet.
             */
            if (pThis->PktHdr.ab[0] != pThis->PktHdr.ab[1])
            {
                if (pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
                {
                    /* Halt the VM and rearrange the packet receiving state machine. */
                    LogFlow(("DbgKd: Halting VM!\n"));

                    rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
                    pThis->PktHdr.ab[0] = pThis->PktHdr.ab[1]; /* Overwrite the first byte with the new start. */
                    pThis->pbRecv       = &pThis->PktHdr.ab[1];
                    pThis->cbRecvLeft   = sizeof(pThis->PktHdr.ab[1]);
                }
                else
                    rc = VERR_NET_PROTOCOL_ERROR; /* Refuse talking to the remote end any further. */
            }
            else
            {
                /* Normal packet receive continues with the rest of the header. */
                pThis->pbRecv     = &pThis->PktHdr.ab[2];
                pThis->cbRecvLeft = sizeof(pThis->PktHdr.Fields) - 2;
                pThis->enmState   = KDRECVSTATE_PACKET_HDR;
            }
            break;
        }
        case KDRECVSTATE_PACKET_HDR:
        {
            if (   dbgcKdPktHdrValidate(&pThis->PktHdr.Fields)
                && pThis->PktHdr.Fields.cbBody <= sizeof(pThis->abBody))
            {
                /* Start receiving the body. */
                if (pThis->PktHdr.Fields.cbBody)
                {
                    pThis->pbRecv     = &pThis->abBody[0];
                    pThis->cbRecvLeft = pThis->PktHdr.Fields.cbBody;
                    pThis->enmState   = KDRECVSTATE_PACKET_BODY;
                }
                else /* No body means no trailer byte it looks like. */
                    rc = dbgcKdCtxPktProcess(pThis);
            }
            else
                rc = VERR_NET_PROTOCOL_ERROR;
            break;
        }
        case KDRECVSTATE_PACKET_BODY:
        {
            pThis->enmState   = KDRECVSTATE_PACKET_TRAILER;
            pThis->bTrailer   = 0;
            pThis->pbRecv     = &pThis->bTrailer;
            pThis->cbRecvLeft = sizeof(pThis->bTrailer);
            break;
        }
        case KDRECVSTATE_PACKET_TRAILER:
        {
            if (pThis->bTrailer == KD_PACKET_TRAILING_BYTE)
                rc = dbgcKdCtxPktProcess(pThis);
            else
                rc = VERR_NET_PROTOCOL_ERROR;
            break;
        }
        default:
            AssertMsgFailed(("Invalid receive state %d\n", pThis->enmState));
    }

    return rc;
}


/**
 * Receive data and processes complete packets.
 *
 * @returns Status code.
 * @param   pThis               The KD context.
 */
static int dbgcKdCtxRecv(PKDCTX pThis)
{
    int rc = VINF_SUCCESS;

    LogFlowFunc(("pThis=%p{.cbRecvLeft=%zu}\n", pThis, pThis->cbRecvLeft));

    if (pThis->cbRecvLeft)
    {
        size_t cbRead = 0;
        rc = pThis->Dbgc.pIo->pfnRead(pThis->Dbgc.pIo, pThis->pbRecv, pThis->cbRecvLeft, &cbRead);
        if (RT_SUCCESS(rc))
        {
            pThis->tsRecvLast  = RTTimeMilliTS();
            pThis->cbRecvLeft -= cbRead;
            pThis->pbRecv     += cbRead;
            if (!pThis->cbRecvLeft)
                rc = dbgcKdCtxRecvDataProcess(pThis);
        }
    }

    LogFlowFunc(("returns rc=%Rrc\n", rc));
    return rc;
}


/**
 * Processes debugger events.
 *
 * @returns VBox status code.
 * @param   pThis   The KD context data.
 * @param   pEvent  Pointer to event data.
 */
static int dbgcKdCtxProcessEvent(PKDCTX pThis, PCDBGFEVENT pEvent)
{
    /*
     * Process the event.
     */
    PDBGC pDbgc = &pThis->Dbgc;
    pThis->Dbgc.pszScratch = &pThis->Dbgc.achInput[0];
    pThis->Dbgc.iArg       = 0;
    int rc = VINF_SUCCESS;
    VMCPUID idCpuOld = pDbgc->idCpu;
    pDbgc->idCpu = pEvent->idCpu;
    switch (pEvent->enmType)
    {
        /*
         * The first part is events we have initiated with commands.
         */
        case DBGFEVENT_HALT_DONE:
        {
            rc = dbgcKdCtxStateChangeSend(pThis, pEvent->enmType);
            break;
        }

        /*
         * The second part is events which can occur at any time.
         */
        case DBGFEVENT_FATAL_ERROR:
        {
            rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbf event: Fatal error! (%s)\n",
                                         dbgcGetEventCtx(pEvent->enmCtx));
            if (RT_SUCCESS(rc))
                rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
            break;
        }

        case DBGFEVENT_BREAKPOINT:
        case DBGFEVENT_BREAKPOINT_IO:
        case DBGFEVENT_BREAKPOINT_MMIO:
        case DBGFEVENT_BREAKPOINT_HYPER:
        {
            rc = dbgcBpExec(pDbgc, pEvent->u.Bp.hBp);
            switch (rc)
            {
                case VERR_DBGC_BP_NOT_FOUND:
                    rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Unknown breakpoint %u! (%s)\n",
                                                 pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
                    break;

                case VINF_DBGC_BP_NO_COMMAND:
                    rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! (%s)\n",
                                                 pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
                    break;

                case VINF_BUFFER_OVERFLOW:
                    rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: Breakpoint %u! Command too long to execute! (%s)\n",
                                                 pEvent->u.Bp.hBp, dbgcGetEventCtx(pEvent->enmCtx));
                    break;

                default:
                    break;
            }
            if (RT_SUCCESS(rc) && DBGFR3IsHalted(pDbgc->pUVM, VMCPUID_ALL))
            {
                rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");

                /* Set the resume flag to ignore the breakpoint when resuming execution. */
                if (   RT_SUCCESS(rc)
                    && pEvent->enmType == DBGFEVENT_BREAKPOINT)
                    rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r eflags.rf = 1");
            }

            /* Figure out the breakpoint and set the triggered flag for emulation of DR6. */
            for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aHwBp); i++)
            {
                if (pThis->aHwBp[i].hDbgfBp == pEvent->u.Bp.hBp)
                {
                    pThis->aHwBp[i].fTriggered = true;
                    break;
                }
            }

            rc = dbgcKdCtxStateChangeSend(pThis, pEvent->enmType);
            break;
        }

        case DBGFEVENT_STEPPED:
        case DBGFEVENT_STEPPED_HYPER:
        {
            pThis->fSingleStepped = true; /* For emulation of DR6. */
            rc = dbgcKdCtxStateChangeSend(pThis, pEvent->enmType);
            break;
        }

        case DBGFEVENT_ASSERTION_HYPER:
        {
            rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
                                         "\ndbgf event: Hypervisor Assertion! (%s)\n"
                                         "%s"
                                         "%s"
                                         "\n",
                                         dbgcGetEventCtx(pEvent->enmCtx),
                                         pEvent->u.Assert.pszMsg1,
                                         pEvent->u.Assert.pszMsg2);
            if (RT_SUCCESS(rc))
                rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
            break;
        }

        case DBGFEVENT_DEV_STOP:
        {
            rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
                                         "\n"
                                         "dbgf event: DBGFSTOP (%s)\n"
                                         "File:     %s\n"
                                         "Line:     %d\n"
                                         "Function: %s\n",
                                         dbgcGetEventCtx(pEvent->enmCtx),
                                         pEvent->u.Src.pszFile,
                                         pEvent->u.Src.uLine,
                                         pEvent->u.Src.pszFunction);
            if (RT_SUCCESS(rc) && pEvent->u.Src.pszMessage && *pEvent->u.Src.pszMessage)
                rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL,
                                             "Message:  %s\n",
                                             pEvent->u.Src.pszMessage);
            if (RT_SUCCESS(rc))
                rc = pDbgc->CmdHlp.pfnExec(&pDbgc->CmdHlp, "r");
            break;
        }


        case DBGFEVENT_INVALID_COMMAND:
        {
            rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Invalid command event!\n");
            break;
        }

        case DBGFEVENT_POWERING_OFF:
        {
            pThis->Dbgc.fReady = false;
            pThis->Dbgc.pIo->pfnSetReady(pThis->Dbgc.pIo, false);
            rc = VERR_GENERAL_FAILURE;
            break;
        }

        default:
        {
            /*
             * Probably a generic event. Look it up to find its name.
             */
            PCDBGCSXEVT pEvtDesc = dbgcEventLookup(pEvent->enmType);
            if (pEvtDesc)
            {
                if (pEvtDesc->enmKind == kDbgcSxEventKind_Interrupt)
                {
                    Assert(pEvtDesc->pszDesc);
                    Assert(pEvent->u.Generic.cArgs == 1);
                    rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s no %#llx! (%s)\n",
                                                 pEvtDesc->pszDesc, pEvent->u.Generic.auArgs[0], pEvtDesc->pszName);
                }
                else if (pEvtDesc->fFlags & DBGCSXEVT_F_BUGCHECK)
                {
                    Assert(pEvent->u.Generic.cArgs >= 5);
                    char szDetails[512];
                    DBGFR3FormatBugCheck(pDbgc->pUVM, szDetails, sizeof(szDetails), pEvent->u.Generic.auArgs[0],
                                         pEvent->u.Generic.auArgs[1], pEvent->u.Generic.auArgs[2],
                                         pEvent->u.Generic.auArgs[3], pEvent->u.Generic.auArgs[4]);
                    rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s %s%s!\n%s", pEvtDesc->pszName,
                                                 pEvtDesc->pszDesc ? "- " : "", pEvtDesc->pszDesc ? pEvtDesc->pszDesc : "",
                                                 szDetails);
                }
                else if (   (pEvtDesc->fFlags & DBGCSXEVT_F_TAKE_ARG)
                         || pEvent->u.Generic.cArgs > 1
                         || (   pEvent->u.Generic.cArgs == 1
                             && pEvent->u.Generic.auArgs[0] != 0))
                {
                    if (pEvtDesc->pszDesc)
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!",
                                                     pEvtDesc->pszName, pEvtDesc->pszDesc);
                    else
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!", pEvtDesc->pszName);
                    if (pEvent->u.Generic.cArgs <= 1)
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " arg=%#llx\n", pEvent->u.Generic.auArgs[0]);
                    else
                    {
                        for (uint32_t i = 0; i < pEvent->u.Generic.cArgs; i++)
                            rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, " args[%u]=%#llx", i, pEvent->u.Generic.auArgs[i]);
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\n");
                    }
                }
                else
                {
                    if (pEvtDesc->pszDesc)
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s - %s!\n",
                                                     pEvtDesc->pszName, pEvtDesc->pszDesc);
                    else
                        rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf event: %s!\n", pEvtDesc->pszName);
                }
            }
            else
                rc = pDbgc->CmdHlp.pfnPrintf(&pDbgc->CmdHlp, NULL, "\ndbgf/dbgc error: Unknown event %d!\n", pEvent->enmType);
            break;
        }
    }

    pDbgc->idCpu = idCpuOld;
    return rc;
}


/**
 * Handle a receive timeout.
 *
 * @returns VBox status code.
 * @param   pThis   Pointer to the KD context.
 */
static int dbgcKdCtxRecvTimeout(PKDCTX pThis)
{
    int rc = VINF_SUCCESS;

    LogFlowFunc(("pThis=%p\n", pThis));

    /*
     * If a single breakin packet byte was received but the header is otherwise incomplete
     * the VM is halted and a state change will be sent in the event processing loop.
     */
    if (   pThis->enmState == KDRECVSTATE_PACKET_HDR_SECOND_BYTE
        && pThis->PktHdr.ab[0] == KD_PACKET_HDR_SIGNATURE_BREAKIN_BYTE)
    {
        LogFlow(("DbgKd: Halting VM!\n"));
        rc = DBGFR3Halt(pThis->Dbgc.pUVM, VMCPUID_ALL);
    }
    else /* Send a reset packet */ /** @todo Figure out the semantics in this case exactly. */
        rc = dbgcKdCtxPktSendReset(pThis);

    dbgcKdCtxPktRecvReset(pThis);

    LogFlowFunc(("rc=%Rrc\n", rc));
    return rc;
}


/**
 * @copydoc DBGC::pfnOutput
 */
static DECLCALLBACK(int) dbgcKdOutput(void *pvUser, const char *pachChars, size_t cbChars)
{
    PKDCTX pThis = (PKDCTX)pvUser;

    return dbgcKdCtxDebugIoStrSend(pThis, pThis->Dbgc.idCpu, pachChars, cbChars);
}


/**
 * Run the debugger console.
 *
 * @returns VBox status code.
 * @param   pThis   Pointer to the KD context.
 */
int dbgcKdRun(PKDCTX pThis)
{
    /*
     * We're ready for commands now.
     */
    pThis->Dbgc.fReady = true;
    pThis->Dbgc.pIo->pfnSetReady(pThis->Dbgc.pIo, true);

    /*
     * Main Debugger Loop.
     *
     * This loop will either block on waiting for input or on waiting on
     * debug events. If we're forwarding the log we cannot wait for long
     * before we must flush the log.
     */
    int rc;
    for (;;)
    {
        rc = VERR_SEM_OUT_OF_TURN;
        if (pThis->Dbgc.pUVM)
            rc = DBGFR3QueryWaitable(pThis->Dbgc.pUVM);

        if (RT_SUCCESS(rc))
        {
            /*
             * Wait for a debug event.
             */
            DBGFEVENT Evt;
            rc = DBGFR3EventWait(pThis->Dbgc.pUVM, 32, &Evt);
            if (RT_SUCCESS(rc))
            {
                rc = dbgcKdCtxProcessEvent(pThis, &Evt);
                if (RT_FAILURE(rc))
                    break;
            }
            else if (rc != VERR_TIMEOUT)
                break;

            /*
             * Check for input.
             */
            if (pThis->Dbgc.pIo->pfnInput(pThis->Dbgc.pIo, 0))
            {
                rc = dbgcKdCtxRecv(pThis);
                if (RT_FAILURE(rc))
                    break;
            }
        }
        else if (rc == VERR_SEM_OUT_OF_TURN)
        {
            /*
             * Wait for input.
             */
            if (pThis->Dbgc.pIo->pfnInput(pThis->Dbgc.pIo, 1000))
            {
                rc = dbgcKdCtxRecv(pThis);
                if (RT_FAILURE(rc))
                    break;
            }
            else if (   pThis->msRecvTimeout != RT_INDEFINITE_WAIT
                     && (RTTimeMilliTS() - pThis->tsRecvLast >= pThis->msRecvTimeout))
                rc = dbgcKdCtxRecvTimeout(pThis);
        }
        else
            break;
    }

    return rc;
}


/**
 * Creates a KD context instance with the given backend.
 *
 * @returns VBox status code.
 * @param   ppKdCtx                 Where to store the pointer to the KD stub context instance on success.
 * @param   pIo                     Pointer to the I/O callback table.
 * @param   fFlags                  Flags controlling the behavior.
 */
static int dbgcKdCtxCreate(PPKDCTX ppKdCtx, PCDBGCIO pIo, unsigned fFlags)
{
    /*
     * Validate input.
     */
    AssertPtrReturn(pIo, VERR_INVALID_POINTER);
    AssertMsgReturn(!fFlags, ("%#x", fFlags), VERR_INVALID_PARAMETER);

    /*
     * Allocate and initialize.
     */
    PKDCTX pThis = (PKDCTX)RTMemAllocZ(sizeof(*pThis));
    if (!pThis)
        return VERR_NO_MEMORY;

    dbgcInitCmdHlp(&pThis->Dbgc);
    /*
     * This is compied from the native debug console (will be used for monitor commands)
     * in DBGCConsole.cpp. Try to keep both functions in sync.
     */
    pThis->Dbgc.pIo              = pIo;
    pThis->Dbgc.pfnOutput        = dbgcKdOutput;
    pThis->Dbgc.pvOutputUser     = pThis;
    pThis->Dbgc.pVM              = NULL;
    pThis->Dbgc.pUVM             = NULL;
    pThis->Dbgc.idCpu            = 0;
    pThis->Dbgc.hDbgAs           = DBGF_AS_GLOBAL;
    pThis->Dbgc.pszEmulation     = "CodeView/WinDbg";
    pThis->Dbgc.paEmulationCmds  = &g_aCmdsCodeView[0];
    pThis->Dbgc.cEmulationCmds   = g_cCmdsCodeView;
    pThis->Dbgc.paEmulationFuncs = &g_aFuncsCodeView[0];
    pThis->Dbgc.cEmulationFuncs  = g_cFuncsCodeView;
    //pThis->Dbgc.fLog             = false;
    pThis->Dbgc.fRegTerse        = true;
    pThis->Dbgc.fStepTraceRegs   = true;
    //pThis->Dbgc.cPagingHierarchyDumps = 0;
    //pThis->Dbgc.DisasmPos        = {0};
    //pThis->Dbgc.SourcePos        = {0};
    //pThis->Dbgc.DumpPos          = {0};
    pThis->Dbgc.pLastPos          = &pThis->Dbgc.DisasmPos;
    //pThis->Dbgc.cbDumpElement    = 0;
    //pThis->Dbgc.cVars            = 0;
    //pThis->Dbgc.paVars           = NULL;
    //pThis->Dbgc.pPlugInHead      = NULL;
    //pThis->Dbgc.pFirstBp         = NULL;
    //pThis->Dbgc.abSearch         = {0};
    //pThis->Dbgc.cbSearch         = 0;
    pThis->Dbgc.cbSearchUnit       = 1;
    pThis->Dbgc.cMaxSearchHits     = 1;
    //pThis->Dbgc.SearchAddr       = {0};
    //pThis->Dbgc.cbSearchRange    = 0;

    //pThis->Dbgc.uInputZero       = 0;
    //pThis->Dbgc.iRead            = 0;
    //pThis->Dbgc.iWrite           = 0;
    //pThis->Dbgc.cInputLines      = 0;
    //pThis->Dbgc.fInputOverflow   = false;
    pThis->Dbgc.fReady           = true;
    pThis->Dbgc.pszScratch       = &pThis->Dbgc.achScratch[0];
    //pThis->Dbgc.iArg             = 0;
    //pThis->Dbgc.rcOutput         = 0;
    //pThis->Dbgc.rcCmd            = 0;

    //pThis->Dbgc.pszHistoryFile       = NULL;
    //pThis->Dbgc.pszGlobalInitScript  = NULL;
    //pThis->Dbgc.pszLocalInitScript   = NULL;

    dbgcEvalInit();

    pThis->fBreakinRecv = false;
    pThis->fInVBoxDbg   = false;
    pThis->idPktNext    = KD_PACKET_HDR_ID_INITIAL;
    pThis->pIfWinNt     = NULL;
    pThis->f32Bit       = false;
    dbgcKdCtxPktRecvReset(pThis);

    for (uint32_t i = 0; i < RT_ELEMENTS(pThis->aHwBp); i++)
    {
        PKDCTXHWBP pBp = &pThis->aHwBp[i];
        pBp->hDbgfBp = NIL_DBGFBP;
    }

    dbgcKdCtxHwBpReset(pThis);

    *ppKdCtx = pThis;
    return VINF_SUCCESS;
}


/**
 * Destroys the given KD context.
 *
 * @param   pThis                   The KD context to destroy.
 */
static void dbgcKdCtxDestroy(PKDCTX pThis)
{
    AssertPtr(pThis);

    pThis->pIfWinNt = NULL;

    /* Detach from the VM. */
    if (pThis->Dbgc.pUVM)
        DBGFR3Detach(pThis->Dbgc.pUVM);

    /* Free config strings. */
    RTStrFree(pThis->Dbgc.pszGlobalInitScript);
    pThis->Dbgc.pszGlobalInitScript = NULL;
    RTStrFree(pThis->Dbgc.pszLocalInitScript);
    pThis->Dbgc.pszLocalInitScript = NULL;
    RTStrFree(pThis->Dbgc.pszHistoryFile);
    pThis->Dbgc.pszHistoryFile = NULL;

    /* Finally, free the instance memory. */
    RTMemFree(pThis);
}


DECL_HIDDEN_CALLBACK(int) dbgcKdStubRunloop(PUVM pUVM, PCDBGCIO pIo, unsigned fFlags)
{
    /*
     * Validate input.
     */
    AssertPtrNullReturn(pUVM, VERR_INVALID_VM_HANDLE);
    PVM pVM = NULL;
    if (pUVM)
    {
        pVM = VMR3GetVM(pUVM);
        AssertPtrReturn(pVM, VERR_INVALID_VM_HANDLE);
    }

    /*
     * Allocate and initialize instance data
     */
    PKDCTX pThis;
    int rc = dbgcKdCtxCreate(&pThis, pIo, fFlags);
    if (RT_FAILURE(rc))
        return rc;
    if (!HMR3IsEnabled(pUVM) && !NEMR3IsEnabled(pUVM))
        pThis->Dbgc.hDbgAs = DBGF_AS_RC_AND_GC_GLOBAL;

    /*
     * Attach to the specified VM.
     */
    if (RT_SUCCESS(rc) && pUVM)
    {
        rc = DBGFR3Attach(pUVM);
        if (RT_SUCCESS(rc))
        {
            pThis->Dbgc.pVM   = pVM;
            pThis->Dbgc.pUVM  = pUVM;
            pThis->Dbgc.idCpu = 0;
        }
        else
            rc = pThis->Dbgc.CmdHlp.pfnVBoxError(&pThis->Dbgc.CmdHlp, rc, "When trying to attach to VM %p\n", pThis->Dbgc.pVM);
    }

    /*
     * Load plugins.
     */
    if (RT_SUCCESS(rc))
    {
        if (pVM)
            DBGFR3PlugInLoadAll(pThis->Dbgc.pUVM);
        dbgcEventInit(&pThis->Dbgc);

        /*
         * Run the debugger main loop.
         */
        rc = dbgcKdRun(pThis);
        dbgcEventTerm(&pThis->Dbgc);
    }

    /*
     * Cleanup console debugger session.
     */
    dbgcKdCtxDestroy(pThis);
    return rc == VERR_DBGC_QUIT ? VINF_SUCCESS : rc;
}