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
|
/* $Id: NEMAllNativeTemplate-win.cpp.h $ */
/** @file
* NEM - Native execution manager, Windows code template ring-0/3.
*/
/*
* Copyright (C) 2018-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
*/
#ifndef IN_RING3
# error "This is ring-3 only now"
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** Copy back a segment from hyper-V. */
#define NEM_WIN_COPY_BACK_SEG(a_Dst, a_Src) \
do { \
(a_Dst).u64Base = (a_Src).Base; \
(a_Dst).u32Limit = (a_Src).Limit; \
(a_Dst).ValidSel = (a_Dst).Sel = (a_Src).Selector; \
(a_Dst).Attr.u = (a_Src).Attributes; \
(a_Dst).fFlags = CPUMSELREG_FLAGS_VALID; \
} while (0)
/** @def NEMWIN_ASSERT_MSG_REG_VAL
* Asserts the correctness of a register value in a message/context.
*/
#if 0
# define NEMWIN_NEED_GET_REGISTER
# define NEMWIN_ASSERT_MSG_REG_VAL(a_pVCpu, a_enmReg, a_Expr, a_Msg) \
do { \
WHV_REGISTER_VALUE TmpVal; \
nemR3WinGetRegister(a_pVCpu, a_enmReg, &TmpVal); \
AssertMsg(a_Expr, a_Msg); \
} while (0)
#else
# define NEMWIN_ASSERT_MSG_REG_VAL(a_pVCpu, a_enmReg, a_Expr, a_Msg) do { } while (0)
#endif
/** @def NEMWIN_ASSERT_MSG_REG_VAL
* Asserts the correctness of a 64-bit register value in a message/context.
*/
#define NEMWIN_ASSERT_MSG_REG_VAL64(a_pVCpu, a_enmReg, a_u64Val) \
NEMWIN_ASSERT_MSG_REG_VAL(a_pVCpu, a_enmReg, (a_u64Val) == TmpVal.Reg64, \
(#a_u64Val "=%#RX64, expected %#RX64\n", (a_u64Val), TmpVal.Reg64))
/** @def NEMWIN_ASSERT_MSG_REG_VAL
* Asserts the correctness of a segment register value in a message/context.
*/
#define NEMWIN_ASSERT_MSG_REG_SEG(a_pVCpu, a_enmReg, a_SReg) \
NEMWIN_ASSERT_MSG_REG_VAL(a_pVCpu, a_enmReg, \
(a_SReg).Base == TmpVal.Segment.Base \
&& (a_SReg).Limit == TmpVal.Segment.Limit \
&& (a_SReg).Selector == TmpVal.Segment.Selector \
&& (a_SReg).Attributes == TmpVal.Segment.Attributes, \
( #a_SReg "=%#RX16 {%#RX64 LB %#RX32,%#RX16} expected %#RX16 {%#RX64 LB %#RX32,%#RX16}\n", \
(a_SReg).Selector, (a_SReg).Base, (a_SReg).Limit, (a_SReg).Attributes, \
TmpVal.Segment.Selector, TmpVal.Segment.Base, TmpVal.Segment.Limit, TmpVal.Segment.Attributes))
#ifndef NTDDI_WIN10_19H1
# define NTDDI_WIN10_19H1 0x0a000007
#endif
/** WHvRegisterPendingEvent0 was renamed to WHvRegisterPendingEvent between
* SDK 17134 and 18362. */
#if WDK_NTDDI_VERSION < NTDDI_WIN10_19H1
# define WHvRegisterPendingEvent WHvRegisterPendingEvent0
#endif
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** NEM_WIN_PAGE_STATE_XXX names. */
NEM_TMPL_STATIC const char * const g_apszPageStates[4] = { "not-set", "unmapped", "readable", "writable" };
/** HV_INTERCEPT_ACCESS_TYPE names. */
static const char * const g_apszHvInterceptAccessTypes[4] = { "read", "write", "exec", "!undefined!" };
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
NEM_TMPL_STATIC int nemHCNativeSetPhysPage(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
uint32_t fPageProt, uint8_t *pu2State, bool fBackingChanged);
NEM_TMPL_STATIC int nemHCWinCopyStateToHyperV(PVMCC pVM, PVMCPUCC pVCpu)
{
/*
* The following is very similar to what nemR0WinExportState() does.
*/
WHV_REGISTER_NAME aenmNames[128];
WHV_REGISTER_VALUE aValues[128];
uint64_t const fWhat = ~pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK);
if ( !fWhat
&& pVCpu->nem.s.fCurrentInterruptWindows == pVCpu->nem.s.fDesiredInterruptWindows)
return VINF_SUCCESS;
uintptr_t iReg = 0;
#define ADD_REG64(a_enmName, a_uValue) do { \
aenmNames[iReg] = (a_enmName); \
aValues[iReg].Reg128.High64 = 0; \
aValues[iReg].Reg64 = (a_uValue); \
iReg++; \
} while (0)
#define ADD_REG128(a_enmName, a_uValueLo, a_uValueHi) do { \
aenmNames[iReg] = (a_enmName); \
aValues[iReg].Reg128.Low64 = (a_uValueLo); \
aValues[iReg].Reg128.High64 = (a_uValueHi); \
iReg++; \
} while (0)
/* GPRs */
if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
{
if (fWhat & CPUMCTX_EXTRN_RAX)
ADD_REG64(WHvX64RegisterRax, pVCpu->cpum.GstCtx.rax);
if (fWhat & CPUMCTX_EXTRN_RCX)
ADD_REG64(WHvX64RegisterRcx, pVCpu->cpum.GstCtx.rcx);
if (fWhat & CPUMCTX_EXTRN_RDX)
ADD_REG64(WHvX64RegisterRdx, pVCpu->cpum.GstCtx.rdx);
if (fWhat & CPUMCTX_EXTRN_RBX)
ADD_REG64(WHvX64RegisterRbx, pVCpu->cpum.GstCtx.rbx);
if (fWhat & CPUMCTX_EXTRN_RSP)
ADD_REG64(WHvX64RegisterRsp, pVCpu->cpum.GstCtx.rsp);
if (fWhat & CPUMCTX_EXTRN_RBP)
ADD_REG64(WHvX64RegisterRbp, pVCpu->cpum.GstCtx.rbp);
if (fWhat & CPUMCTX_EXTRN_RSI)
ADD_REG64(WHvX64RegisterRsi, pVCpu->cpum.GstCtx.rsi);
if (fWhat & CPUMCTX_EXTRN_RDI)
ADD_REG64(WHvX64RegisterRdi, pVCpu->cpum.GstCtx.rdi);
if (fWhat & CPUMCTX_EXTRN_R8_R15)
{
ADD_REG64(WHvX64RegisterR8, pVCpu->cpum.GstCtx.r8);
ADD_REG64(WHvX64RegisterR9, pVCpu->cpum.GstCtx.r9);
ADD_REG64(WHvX64RegisterR10, pVCpu->cpum.GstCtx.r10);
ADD_REG64(WHvX64RegisterR11, pVCpu->cpum.GstCtx.r11);
ADD_REG64(WHvX64RegisterR12, pVCpu->cpum.GstCtx.r12);
ADD_REG64(WHvX64RegisterR13, pVCpu->cpum.GstCtx.r13);
ADD_REG64(WHvX64RegisterR14, pVCpu->cpum.GstCtx.r14);
ADD_REG64(WHvX64RegisterR15, pVCpu->cpum.GstCtx.r15);
}
}
/* RIP & Flags */
if (fWhat & CPUMCTX_EXTRN_RIP)
ADD_REG64(WHvX64RegisterRip, pVCpu->cpum.GstCtx.rip);
if (fWhat & CPUMCTX_EXTRN_RFLAGS)
ADD_REG64(WHvX64RegisterRflags, pVCpu->cpum.GstCtx.rflags.u);
/* Segments */
#define ADD_SEG(a_enmName, a_SReg) \
do { \
aenmNames[iReg] = a_enmName; \
aValues[iReg].Segment.Base = (a_SReg).u64Base; \
aValues[iReg].Segment.Limit = (a_SReg).u32Limit; \
aValues[iReg].Segment.Selector = (a_SReg).Sel; \
aValues[iReg].Segment.Attributes = (a_SReg).Attr.u; \
iReg++; \
} while (0)
if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
{
if (fWhat & CPUMCTX_EXTRN_ES)
ADD_SEG(WHvX64RegisterEs, pVCpu->cpum.GstCtx.es);
if (fWhat & CPUMCTX_EXTRN_CS)
ADD_SEG(WHvX64RegisterCs, pVCpu->cpum.GstCtx.cs);
if (fWhat & CPUMCTX_EXTRN_SS)
ADD_SEG(WHvX64RegisterSs, pVCpu->cpum.GstCtx.ss);
if (fWhat & CPUMCTX_EXTRN_DS)
ADD_SEG(WHvX64RegisterDs, pVCpu->cpum.GstCtx.ds);
if (fWhat & CPUMCTX_EXTRN_FS)
ADD_SEG(WHvX64RegisterFs, pVCpu->cpum.GstCtx.fs);
if (fWhat & CPUMCTX_EXTRN_GS)
ADD_SEG(WHvX64RegisterGs, pVCpu->cpum.GstCtx.gs);
}
/* Descriptor tables & task segment. */
if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
{
if (fWhat & CPUMCTX_EXTRN_LDTR)
ADD_SEG(WHvX64RegisterLdtr, pVCpu->cpum.GstCtx.ldtr);
if (fWhat & CPUMCTX_EXTRN_TR)
ADD_SEG(WHvX64RegisterTr, pVCpu->cpum.GstCtx.tr);
if (fWhat & CPUMCTX_EXTRN_IDTR)
{
aenmNames[iReg] = WHvX64RegisterIdtr;
aValues[iReg].Table.Limit = pVCpu->cpum.GstCtx.idtr.cbIdt;
aValues[iReg].Table.Base = pVCpu->cpum.GstCtx.idtr.pIdt;
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_GDTR)
{
aenmNames[iReg] = WHvX64RegisterGdtr;
aValues[iReg].Table.Limit = pVCpu->cpum.GstCtx.gdtr.cbGdt;
aValues[iReg].Table.Base = pVCpu->cpum.GstCtx.gdtr.pGdt;
iReg++;
}
}
/* Control registers. */
if (fWhat & CPUMCTX_EXTRN_CR_MASK)
{
if (fWhat & CPUMCTX_EXTRN_CR0)
ADD_REG64(WHvX64RegisterCr0, pVCpu->cpum.GstCtx.cr0);
if (fWhat & CPUMCTX_EXTRN_CR2)
ADD_REG64(WHvX64RegisterCr2, pVCpu->cpum.GstCtx.cr2);
if (fWhat & CPUMCTX_EXTRN_CR3)
ADD_REG64(WHvX64RegisterCr3, pVCpu->cpum.GstCtx.cr3);
if (fWhat & CPUMCTX_EXTRN_CR4)
ADD_REG64(WHvX64RegisterCr4, pVCpu->cpum.GstCtx.cr4);
}
if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
ADD_REG64(WHvX64RegisterCr8, CPUMGetGuestCR8(pVCpu));
/* Debug registers. */
/** @todo fixme. Figure out what the hyper-v version of KVM_SET_GUEST_DEBUG would be. */
if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
{
ADD_REG64(WHvX64RegisterDr0, pVCpu->cpum.GstCtx.dr[0]); // CPUMGetHyperDR0(pVCpu));
ADD_REG64(WHvX64RegisterDr1, pVCpu->cpum.GstCtx.dr[1]); // CPUMGetHyperDR1(pVCpu));
ADD_REG64(WHvX64RegisterDr2, pVCpu->cpum.GstCtx.dr[2]); // CPUMGetHyperDR2(pVCpu));
ADD_REG64(WHvX64RegisterDr3, pVCpu->cpum.GstCtx.dr[3]); // CPUMGetHyperDR3(pVCpu));
}
if (fWhat & CPUMCTX_EXTRN_DR6)
ADD_REG64(WHvX64RegisterDr6, pVCpu->cpum.GstCtx.dr[6]); // CPUMGetHyperDR6(pVCpu));
if (fWhat & CPUMCTX_EXTRN_DR7)
ADD_REG64(WHvX64RegisterDr7, pVCpu->cpum.GstCtx.dr[7]); // CPUMGetHyperDR7(pVCpu));
/* Floating point state. */
if (fWhat & CPUMCTX_EXTRN_X87)
{
ADD_REG128(WHvX64RegisterFpMmx0, pVCpu->cpum.GstCtx.XState.x87.aRegs[0].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[0].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx1, pVCpu->cpum.GstCtx.XState.x87.aRegs[1].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[1].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx2, pVCpu->cpum.GstCtx.XState.x87.aRegs[2].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[2].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx3, pVCpu->cpum.GstCtx.XState.x87.aRegs[3].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[3].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx4, pVCpu->cpum.GstCtx.XState.x87.aRegs[4].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[4].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx5, pVCpu->cpum.GstCtx.XState.x87.aRegs[5].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[5].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx6, pVCpu->cpum.GstCtx.XState.x87.aRegs[6].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[6].au64[1]);
ADD_REG128(WHvX64RegisterFpMmx7, pVCpu->cpum.GstCtx.XState.x87.aRegs[7].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[7].au64[1]);
aenmNames[iReg] = WHvX64RegisterFpControlStatus;
aValues[iReg].FpControlStatus.FpControl = pVCpu->cpum.GstCtx.XState.x87.FCW;
aValues[iReg].FpControlStatus.FpStatus = pVCpu->cpum.GstCtx.XState.x87.FSW;
aValues[iReg].FpControlStatus.FpTag = pVCpu->cpum.GstCtx.XState.x87.FTW;
aValues[iReg].FpControlStatus.Reserved = pVCpu->cpum.GstCtx.XState.x87.FTW >> 8;
aValues[iReg].FpControlStatus.LastFpOp = pVCpu->cpum.GstCtx.XState.x87.FOP;
aValues[iReg].FpControlStatus.LastFpRip = (pVCpu->cpum.GstCtx.XState.x87.FPUIP)
| ((uint64_t)pVCpu->cpum.GstCtx.XState.x87.CS << 32)
| ((uint64_t)pVCpu->cpum.GstCtx.XState.x87.Rsrvd1 << 48);
iReg++;
aenmNames[iReg] = WHvX64RegisterXmmControlStatus;
aValues[iReg].XmmControlStatus.LastFpRdp = (pVCpu->cpum.GstCtx.XState.x87.FPUDP)
| ((uint64_t)pVCpu->cpum.GstCtx.XState.x87.DS << 32)
| ((uint64_t)pVCpu->cpum.GstCtx.XState.x87.Rsrvd2 << 48);
aValues[iReg].XmmControlStatus.XmmStatusControl = pVCpu->cpum.GstCtx.XState.x87.MXCSR;
aValues[iReg].XmmControlStatus.XmmStatusControlMask = pVCpu->cpum.GstCtx.XState.x87.MXCSR_MASK; /** @todo ??? (Isn't this an output field?) */
iReg++;
}
/* Vector state. */
if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
{
ADD_REG128(WHvX64RegisterXmm0, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 0].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 0].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm1, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 1].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 1].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm2, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 2].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 2].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm3, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 3].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 3].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm4, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 4].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 4].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm5, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 5].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 5].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm6, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 6].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 6].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm7, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 7].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 7].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm8, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 8].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 8].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm9, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 9].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 9].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm10, pVCpu->cpum.GstCtx.XState.x87.aXMM[10].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[10].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm11, pVCpu->cpum.GstCtx.XState.x87.aXMM[11].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[11].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm12, pVCpu->cpum.GstCtx.XState.x87.aXMM[12].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[12].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm13, pVCpu->cpum.GstCtx.XState.x87.aXMM[13].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[13].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm14, pVCpu->cpum.GstCtx.XState.x87.aXMM[14].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[14].uXmm.s.Hi);
ADD_REG128(WHvX64RegisterXmm15, pVCpu->cpum.GstCtx.XState.x87.aXMM[15].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[15].uXmm.s.Hi);
}
/* MSRs */
// WHvX64RegisterTsc - don't touch
if (fWhat & CPUMCTX_EXTRN_EFER)
ADD_REG64(WHvX64RegisterEfer, pVCpu->cpum.GstCtx.msrEFER);
if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
ADD_REG64(WHvX64RegisterKernelGsBase, pVCpu->cpum.GstCtx.msrKERNELGSBASE);
if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
{
ADD_REG64(WHvX64RegisterSysenterCs, pVCpu->cpum.GstCtx.SysEnter.cs);
ADD_REG64(WHvX64RegisterSysenterEip, pVCpu->cpum.GstCtx.SysEnter.eip);
ADD_REG64(WHvX64RegisterSysenterEsp, pVCpu->cpum.GstCtx.SysEnter.esp);
}
if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
{
ADD_REG64(WHvX64RegisterStar, pVCpu->cpum.GstCtx.msrSTAR);
ADD_REG64(WHvX64RegisterLstar, pVCpu->cpum.GstCtx.msrLSTAR);
ADD_REG64(WHvX64RegisterCstar, pVCpu->cpum.GstCtx.msrCSTAR);
ADD_REG64(WHvX64RegisterSfmask, pVCpu->cpum.GstCtx.msrSFMASK);
}
if (fWhat & (CPUMCTX_EXTRN_TSC_AUX | CPUMCTX_EXTRN_OTHER_MSRS))
{
PCPUMCTXMSRS const pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pVCpu);
if (fWhat & CPUMCTX_EXTRN_TSC_AUX)
ADD_REG64(WHvX64RegisterTscAux, pCtxMsrs->msr.TscAux);
if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
{
ADD_REG64(WHvX64RegisterApicBase, APICGetBaseMsrNoCheck(pVCpu));
ADD_REG64(WHvX64RegisterPat, pVCpu->cpum.GstCtx.msrPAT);
#if 0 /** @todo check if WHvX64RegisterMsrMtrrCap works here... */
ADD_REG64(WHvX64RegisterMsrMtrrCap, CPUMGetGuestIa32MtrrCap(pVCpu));
#endif
ADD_REG64(WHvX64RegisterMsrMtrrDefType, pCtxMsrs->msr.MtrrDefType);
ADD_REG64(WHvX64RegisterMsrMtrrFix64k00000, pCtxMsrs->msr.MtrrFix64K_00000);
ADD_REG64(WHvX64RegisterMsrMtrrFix16k80000, pCtxMsrs->msr.MtrrFix16K_80000);
ADD_REG64(WHvX64RegisterMsrMtrrFix16kA0000, pCtxMsrs->msr.MtrrFix16K_A0000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kC0000, pCtxMsrs->msr.MtrrFix4K_C0000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kC8000, pCtxMsrs->msr.MtrrFix4K_C8000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kD0000, pCtxMsrs->msr.MtrrFix4K_D0000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kD8000, pCtxMsrs->msr.MtrrFix4K_D8000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kE0000, pCtxMsrs->msr.MtrrFix4K_E0000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kE8000, pCtxMsrs->msr.MtrrFix4K_E8000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kF0000, pCtxMsrs->msr.MtrrFix4K_F0000);
ADD_REG64(WHvX64RegisterMsrMtrrFix4kF8000, pCtxMsrs->msr.MtrrFix4K_F8000);
#if 0 /** @todo these registers aren't available? Might explain something.. .*/
const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pVM);
if (enmCpuVendor != CPUMCPUVENDOR_AMD)
{
ADD_REG64(HvX64RegisterIa32MiscEnable, pCtxMsrs->msr.MiscEnable);
ADD_REG64(HvX64RegisterIa32FeatureControl, CPUMGetGuestIa32FeatureControl(pVCpu));
}
#endif
}
}
/* event injection (clear it). */
if (fWhat & CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT)
ADD_REG64(WHvRegisterPendingInterruption, 0);
/* Interruptibility state. This can get a little complicated since we get
half of the state via HV_X64_VP_EXECUTION_STATE. */
if ( (fWhat & (CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI))
== (CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI) )
{
ADD_REG64(WHvRegisterInterruptState, 0);
if (CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx))
aValues[iReg - 1].InterruptState.InterruptShadow = 1;
aValues[iReg - 1].InterruptState.NmiMasked = CPUMAreInterruptsInhibitedByNmi(&pVCpu->cpum.GstCtx);
}
else if (fWhat & CPUMCTX_EXTRN_INHIBIT_INT)
{
if ( pVCpu->nem.s.fLastInterruptShadow
|| CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx))
{
ADD_REG64(WHvRegisterInterruptState, 0);
if (CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx))
aValues[iReg - 1].InterruptState.InterruptShadow = 1;
/** @todo Retrieve NMI state, currently assuming it's zero. (yes this may happen on I/O) */
//if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_BLOCK_NMIS))
// aValues[iReg - 1].InterruptState.NmiMasked = 1;
}
}
else
Assert(!(fWhat & CPUMCTX_EXTRN_INHIBIT_NMI));
/* Interrupt windows. Always set if active as Hyper-V seems to be forgetful. */
uint8_t const fDesiredIntWin = pVCpu->nem.s.fDesiredInterruptWindows;
if ( fDesiredIntWin
|| pVCpu->nem.s.fCurrentInterruptWindows != fDesiredIntWin)
{
pVCpu->nem.s.fCurrentInterruptWindows = pVCpu->nem.s.fDesiredInterruptWindows;
Log8(("Setting WHvX64RegisterDeliverabilityNotifications, fDesiredIntWin=%X\n", fDesiredIntWin));
ADD_REG64(WHvX64RegisterDeliverabilityNotifications, fDesiredIntWin);
Assert(aValues[iReg - 1].DeliverabilityNotifications.NmiNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_NMI));
Assert(aValues[iReg - 1].DeliverabilityNotifications.InterruptNotification == RT_BOOL(fDesiredIntWin & NEM_WIN_INTW_F_REGULAR));
Assert(aValues[iReg - 1].DeliverabilityNotifications.InterruptPriority == (unsigned)((fDesiredIntWin & NEM_WIN_INTW_F_PRIO_MASK) >> NEM_WIN_INTW_F_PRIO_SHIFT));
}
/// @todo WHvRegisterPendingEvent
#undef ADD_REG64
#undef ADD_REG128
#undef ADD_SEG
/*
* Set the registers.
*/
Assert(iReg < RT_ELEMENTS(aValues));
Assert(iReg < RT_ELEMENTS(aenmNames));
#ifdef NEM_WIN_INTERCEPT_NT_IO_CTLS
Log12(("Calling WHvSetVirtualProcessorRegisters(%p, %u, %p, %u, %p)\n",
pVM->nem.s.hPartition, pVCpu->idCpu, aenmNames, iReg, aValues));
#endif
HRESULT hrc = WHvSetVirtualProcessorRegisters(pVM->nem.s.hPartition, pVCpu->idCpu, aenmNames, iReg, aValues);
if (SUCCEEDED(hrc))
{
pVCpu->cpum.GstCtx.fExtrn |= CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK | CPUMCTX_EXTRN_KEEPER_NEM;
return VINF_SUCCESS;
}
AssertLogRelMsgFailed(("WHvSetVirtualProcessorRegisters(%p, %u,,%u,) -> %Rhrc (Last=%#x/%u)\n",
pVM->nem.s.hPartition, pVCpu->idCpu, iReg,
hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_INTERNAL_ERROR;
}
NEM_TMPL_STATIC int nemHCWinCopyStateFromHyperV(PVMCC pVM, PVMCPUCC pVCpu, uint64_t fWhat)
{
WHV_REGISTER_NAME aenmNames[128];
fWhat &= pVCpu->cpum.GstCtx.fExtrn;
uintptr_t iReg = 0;
/* GPRs */
if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
{
if (fWhat & CPUMCTX_EXTRN_RAX)
aenmNames[iReg++] = WHvX64RegisterRax;
if (fWhat & CPUMCTX_EXTRN_RCX)
aenmNames[iReg++] = WHvX64RegisterRcx;
if (fWhat & CPUMCTX_EXTRN_RDX)
aenmNames[iReg++] = WHvX64RegisterRdx;
if (fWhat & CPUMCTX_EXTRN_RBX)
aenmNames[iReg++] = WHvX64RegisterRbx;
if (fWhat & CPUMCTX_EXTRN_RSP)
aenmNames[iReg++] = WHvX64RegisterRsp;
if (fWhat & CPUMCTX_EXTRN_RBP)
aenmNames[iReg++] = WHvX64RegisterRbp;
if (fWhat & CPUMCTX_EXTRN_RSI)
aenmNames[iReg++] = WHvX64RegisterRsi;
if (fWhat & CPUMCTX_EXTRN_RDI)
aenmNames[iReg++] = WHvX64RegisterRdi;
if (fWhat & CPUMCTX_EXTRN_R8_R15)
{
aenmNames[iReg++] = WHvX64RegisterR8;
aenmNames[iReg++] = WHvX64RegisterR9;
aenmNames[iReg++] = WHvX64RegisterR10;
aenmNames[iReg++] = WHvX64RegisterR11;
aenmNames[iReg++] = WHvX64RegisterR12;
aenmNames[iReg++] = WHvX64RegisterR13;
aenmNames[iReg++] = WHvX64RegisterR14;
aenmNames[iReg++] = WHvX64RegisterR15;
}
}
/* RIP & Flags */
if (fWhat & CPUMCTX_EXTRN_RIP)
aenmNames[iReg++] = WHvX64RegisterRip;
if (fWhat & CPUMCTX_EXTRN_RFLAGS)
aenmNames[iReg++] = WHvX64RegisterRflags;
/* Segments */
if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
{
if (fWhat & CPUMCTX_EXTRN_ES)
aenmNames[iReg++] = WHvX64RegisterEs;
if (fWhat & CPUMCTX_EXTRN_CS)
aenmNames[iReg++] = WHvX64RegisterCs;
if (fWhat & CPUMCTX_EXTRN_SS)
aenmNames[iReg++] = WHvX64RegisterSs;
if (fWhat & CPUMCTX_EXTRN_DS)
aenmNames[iReg++] = WHvX64RegisterDs;
if (fWhat & CPUMCTX_EXTRN_FS)
aenmNames[iReg++] = WHvX64RegisterFs;
if (fWhat & CPUMCTX_EXTRN_GS)
aenmNames[iReg++] = WHvX64RegisterGs;
}
/* Descriptor tables. */
if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
{
if (fWhat & CPUMCTX_EXTRN_LDTR)
aenmNames[iReg++] = WHvX64RegisterLdtr;
if (fWhat & CPUMCTX_EXTRN_TR)
aenmNames[iReg++] = WHvX64RegisterTr;
if (fWhat & CPUMCTX_EXTRN_IDTR)
aenmNames[iReg++] = WHvX64RegisterIdtr;
if (fWhat & CPUMCTX_EXTRN_GDTR)
aenmNames[iReg++] = WHvX64RegisterGdtr;
}
/* Control registers. */
if (fWhat & CPUMCTX_EXTRN_CR_MASK)
{
if (fWhat & CPUMCTX_EXTRN_CR0)
aenmNames[iReg++] = WHvX64RegisterCr0;
if (fWhat & CPUMCTX_EXTRN_CR2)
aenmNames[iReg++] = WHvX64RegisterCr2;
if (fWhat & CPUMCTX_EXTRN_CR3)
aenmNames[iReg++] = WHvX64RegisterCr3;
if (fWhat & CPUMCTX_EXTRN_CR4)
aenmNames[iReg++] = WHvX64RegisterCr4;
}
if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
aenmNames[iReg++] = WHvX64RegisterCr8;
/* Debug registers. */
if (fWhat & CPUMCTX_EXTRN_DR7)
aenmNames[iReg++] = WHvX64RegisterDr7;
if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
{
if (!(fWhat & CPUMCTX_EXTRN_DR7) && (pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_DR7))
{
fWhat |= CPUMCTX_EXTRN_DR7;
aenmNames[iReg++] = WHvX64RegisterDr7;
}
aenmNames[iReg++] = WHvX64RegisterDr0;
aenmNames[iReg++] = WHvX64RegisterDr1;
aenmNames[iReg++] = WHvX64RegisterDr2;
aenmNames[iReg++] = WHvX64RegisterDr3;
}
if (fWhat & CPUMCTX_EXTRN_DR6)
aenmNames[iReg++] = WHvX64RegisterDr6;
/* Floating point state. */
if (fWhat & CPUMCTX_EXTRN_X87)
{
aenmNames[iReg++] = WHvX64RegisterFpMmx0;
aenmNames[iReg++] = WHvX64RegisterFpMmx1;
aenmNames[iReg++] = WHvX64RegisterFpMmx2;
aenmNames[iReg++] = WHvX64RegisterFpMmx3;
aenmNames[iReg++] = WHvX64RegisterFpMmx4;
aenmNames[iReg++] = WHvX64RegisterFpMmx5;
aenmNames[iReg++] = WHvX64RegisterFpMmx6;
aenmNames[iReg++] = WHvX64RegisterFpMmx7;
aenmNames[iReg++] = WHvX64RegisterFpControlStatus;
}
if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
aenmNames[iReg++] = WHvX64RegisterXmmControlStatus;
/* Vector state. */
if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
{
aenmNames[iReg++] = WHvX64RegisterXmm0;
aenmNames[iReg++] = WHvX64RegisterXmm1;
aenmNames[iReg++] = WHvX64RegisterXmm2;
aenmNames[iReg++] = WHvX64RegisterXmm3;
aenmNames[iReg++] = WHvX64RegisterXmm4;
aenmNames[iReg++] = WHvX64RegisterXmm5;
aenmNames[iReg++] = WHvX64RegisterXmm6;
aenmNames[iReg++] = WHvX64RegisterXmm7;
aenmNames[iReg++] = WHvX64RegisterXmm8;
aenmNames[iReg++] = WHvX64RegisterXmm9;
aenmNames[iReg++] = WHvX64RegisterXmm10;
aenmNames[iReg++] = WHvX64RegisterXmm11;
aenmNames[iReg++] = WHvX64RegisterXmm12;
aenmNames[iReg++] = WHvX64RegisterXmm13;
aenmNames[iReg++] = WHvX64RegisterXmm14;
aenmNames[iReg++] = WHvX64RegisterXmm15;
}
/* MSRs */
// WHvX64RegisterTsc - don't touch
if (fWhat & CPUMCTX_EXTRN_EFER)
aenmNames[iReg++] = WHvX64RegisterEfer;
if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
aenmNames[iReg++] = WHvX64RegisterKernelGsBase;
if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
{
aenmNames[iReg++] = WHvX64RegisterSysenterCs;
aenmNames[iReg++] = WHvX64RegisterSysenterEip;
aenmNames[iReg++] = WHvX64RegisterSysenterEsp;
}
if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
{
aenmNames[iReg++] = WHvX64RegisterStar;
aenmNames[iReg++] = WHvX64RegisterLstar;
aenmNames[iReg++] = WHvX64RegisterCstar;
aenmNames[iReg++] = WHvX64RegisterSfmask;
}
//#ifdef LOG_ENABLED
// const CPUMCPUVENDOR enmCpuVendor = CPUMGetHostCpuVendor(pVM);
//#endif
if (fWhat & CPUMCTX_EXTRN_TSC_AUX)
aenmNames[iReg++] = WHvX64RegisterTscAux;
if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
{
aenmNames[iReg++] = WHvX64RegisterApicBase; /// @todo APIC BASE
aenmNames[iReg++] = WHvX64RegisterPat;
#if 0 /*def LOG_ENABLED*/ /** @todo Check if WHvX64RegisterMsrMtrrCap works... */
aenmNames[iReg++] = WHvX64RegisterMsrMtrrCap;
#endif
aenmNames[iReg++] = WHvX64RegisterMsrMtrrDefType;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix64k00000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix16k80000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix16kA0000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kC0000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kC8000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kD0000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kD8000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kE0000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kE8000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kF0000;
aenmNames[iReg++] = WHvX64RegisterMsrMtrrFix4kF8000;
/** @todo look for HvX64RegisterIa32MiscEnable and HvX64RegisterIa32FeatureControl? */
//#ifdef LOG_ENABLED
// if (enmCpuVendor != CPUMCPUVENDOR_AMD)
// aenmNames[iReg++] = HvX64RegisterIa32FeatureControl;
//#endif
}
/* Interruptibility. */
if (fWhat & (CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI))
{
aenmNames[iReg++] = WHvRegisterInterruptState;
aenmNames[iReg++] = WHvX64RegisterRip;
}
/* event injection */
aenmNames[iReg++] = WHvRegisterPendingInterruption;
aenmNames[iReg++] = WHvRegisterPendingEvent;
size_t const cRegs = iReg;
Assert(cRegs < RT_ELEMENTS(aenmNames));
/*
* Get the registers.
*/
WHV_REGISTER_VALUE aValues[128];
RT_ZERO(aValues);
Assert(RT_ELEMENTS(aValues) >= cRegs);
Assert(RT_ELEMENTS(aenmNames) >= cRegs);
#ifdef NEM_WIN_INTERCEPT_NT_IO_CTLS
Log12(("Calling WHvGetVirtualProcessorRegisters(%p, %u, %p, %u, %p)\n",
pVM->nem.s.hPartition, pVCpu->idCpu, aenmNames, cRegs, aValues));
#endif
HRESULT hrc = WHvGetVirtualProcessorRegisters(pVM->nem.s.hPartition, pVCpu->idCpu, aenmNames, (uint32_t)cRegs, aValues);
AssertLogRelMsgReturn(SUCCEEDED(hrc),
("WHvGetVirtualProcessorRegisters(%p, %u,,%u,) -> %Rhrc (Last=%#x/%u)\n",
pVM->nem.s.hPartition, pVCpu->idCpu, cRegs, hrc, RTNtLastStatusValue(), RTNtLastErrorValue())
, VERR_NEM_GET_REGISTERS_FAILED);
iReg = 0;
#define GET_REG64(a_DstVar, a_enmName) do { \
Assert(aenmNames[iReg] == (a_enmName)); \
(a_DstVar) = aValues[iReg].Reg64; \
iReg++; \
} while (0)
#define GET_REG64_LOG7(a_DstVar, a_enmName, a_szLogName) do { \
Assert(aenmNames[iReg] == (a_enmName)); \
if ((a_DstVar) != aValues[iReg].Reg64) \
Log7(("NEM/%u: " a_szLogName " changed %RX64 -> %RX64\n", pVCpu->idCpu, (a_DstVar), aValues[iReg].Reg64)); \
(a_DstVar) = aValues[iReg].Reg64; \
iReg++; \
} while (0)
#define GET_REG128(a_DstVarLo, a_DstVarHi, a_enmName) do { \
Assert(aenmNames[iReg] == a_enmName); \
(a_DstVarLo) = aValues[iReg].Reg128.Low64; \
(a_DstVarHi) = aValues[iReg].Reg128.High64; \
iReg++; \
} while (0)
#define GET_SEG(a_SReg, a_enmName) do { \
Assert(aenmNames[iReg] == (a_enmName)); \
NEM_WIN_COPY_BACK_SEG(a_SReg, aValues[iReg].Segment); \
iReg++; \
} while (0)
/* GPRs */
if (fWhat & CPUMCTX_EXTRN_GPRS_MASK)
{
if (fWhat & CPUMCTX_EXTRN_RAX)
GET_REG64(pVCpu->cpum.GstCtx.rax, WHvX64RegisterRax);
if (fWhat & CPUMCTX_EXTRN_RCX)
GET_REG64(pVCpu->cpum.GstCtx.rcx, WHvX64RegisterRcx);
if (fWhat & CPUMCTX_EXTRN_RDX)
GET_REG64(pVCpu->cpum.GstCtx.rdx, WHvX64RegisterRdx);
if (fWhat & CPUMCTX_EXTRN_RBX)
GET_REG64(pVCpu->cpum.GstCtx.rbx, WHvX64RegisterRbx);
if (fWhat & CPUMCTX_EXTRN_RSP)
GET_REG64(pVCpu->cpum.GstCtx.rsp, WHvX64RegisterRsp);
if (fWhat & CPUMCTX_EXTRN_RBP)
GET_REG64(pVCpu->cpum.GstCtx.rbp, WHvX64RegisterRbp);
if (fWhat & CPUMCTX_EXTRN_RSI)
GET_REG64(pVCpu->cpum.GstCtx.rsi, WHvX64RegisterRsi);
if (fWhat & CPUMCTX_EXTRN_RDI)
GET_REG64(pVCpu->cpum.GstCtx.rdi, WHvX64RegisterRdi);
if (fWhat & CPUMCTX_EXTRN_R8_R15)
{
GET_REG64(pVCpu->cpum.GstCtx.r8, WHvX64RegisterR8);
GET_REG64(pVCpu->cpum.GstCtx.r9, WHvX64RegisterR9);
GET_REG64(pVCpu->cpum.GstCtx.r10, WHvX64RegisterR10);
GET_REG64(pVCpu->cpum.GstCtx.r11, WHvX64RegisterR11);
GET_REG64(pVCpu->cpum.GstCtx.r12, WHvX64RegisterR12);
GET_REG64(pVCpu->cpum.GstCtx.r13, WHvX64RegisterR13);
GET_REG64(pVCpu->cpum.GstCtx.r14, WHvX64RegisterR14);
GET_REG64(pVCpu->cpum.GstCtx.r15, WHvX64RegisterR15);
}
}
/* RIP & Flags */
if (fWhat & CPUMCTX_EXTRN_RIP)
GET_REG64(pVCpu->cpum.GstCtx.rip, WHvX64RegisterRip);
if (fWhat & CPUMCTX_EXTRN_RFLAGS)
GET_REG64(pVCpu->cpum.GstCtx.rflags.u, WHvX64RegisterRflags);
/* Segments */
if (fWhat & CPUMCTX_EXTRN_SREG_MASK)
{
if (fWhat & CPUMCTX_EXTRN_ES)
GET_SEG(pVCpu->cpum.GstCtx.es, WHvX64RegisterEs);
if (fWhat & CPUMCTX_EXTRN_CS)
GET_SEG(pVCpu->cpum.GstCtx.cs, WHvX64RegisterCs);
if (fWhat & CPUMCTX_EXTRN_SS)
GET_SEG(pVCpu->cpum.GstCtx.ss, WHvX64RegisterSs);
if (fWhat & CPUMCTX_EXTRN_DS)
GET_SEG(pVCpu->cpum.GstCtx.ds, WHvX64RegisterDs);
if (fWhat & CPUMCTX_EXTRN_FS)
GET_SEG(pVCpu->cpum.GstCtx.fs, WHvX64RegisterFs);
if (fWhat & CPUMCTX_EXTRN_GS)
GET_SEG(pVCpu->cpum.GstCtx.gs, WHvX64RegisterGs);
}
/* Descriptor tables and the task segment. */
if (fWhat & CPUMCTX_EXTRN_TABLE_MASK)
{
if (fWhat & CPUMCTX_EXTRN_LDTR)
GET_SEG(pVCpu->cpum.GstCtx.ldtr, WHvX64RegisterLdtr);
if (fWhat & CPUMCTX_EXTRN_TR)
{
/* AMD-V likes loading TR with in AVAIL state, whereas intel insists on BUSY. So,
avoid to trigger sanity assertions around the code, always fix this. */
GET_SEG(pVCpu->cpum.GstCtx.tr, WHvX64RegisterTr);
switch (pVCpu->cpum.GstCtx.tr.Attr.n.u4Type)
{
case X86_SEL_TYPE_SYS_386_TSS_BUSY:
case X86_SEL_TYPE_SYS_286_TSS_BUSY:
break;
case X86_SEL_TYPE_SYS_386_TSS_AVAIL:
pVCpu->cpum.GstCtx.tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_386_TSS_BUSY;
break;
case X86_SEL_TYPE_SYS_286_TSS_AVAIL:
pVCpu->cpum.GstCtx.tr.Attr.n.u4Type = X86_SEL_TYPE_SYS_286_TSS_BUSY;
break;
}
}
if (fWhat & CPUMCTX_EXTRN_IDTR)
{
Assert(aenmNames[iReg] == WHvX64RegisterIdtr);
pVCpu->cpum.GstCtx.idtr.cbIdt = aValues[iReg].Table.Limit;
pVCpu->cpum.GstCtx.idtr.pIdt = aValues[iReg].Table.Base;
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_GDTR)
{
Assert(aenmNames[iReg] == WHvX64RegisterGdtr);
pVCpu->cpum.GstCtx.gdtr.cbGdt = aValues[iReg].Table.Limit;
pVCpu->cpum.GstCtx.gdtr.pGdt = aValues[iReg].Table.Base;
iReg++;
}
}
/* Control registers. */
bool fMaybeChangedMode = false;
bool fUpdateCr3 = false;
if (fWhat & CPUMCTX_EXTRN_CR_MASK)
{
if (fWhat & CPUMCTX_EXTRN_CR0)
{
Assert(aenmNames[iReg] == WHvX64RegisterCr0);
if (pVCpu->cpum.GstCtx.cr0 != aValues[iReg].Reg64)
{
CPUMSetGuestCR0(pVCpu, aValues[iReg].Reg64);
fMaybeChangedMode = true;
}
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_CR2)
GET_REG64(pVCpu->cpum.GstCtx.cr2, WHvX64RegisterCr2);
if (fWhat & CPUMCTX_EXTRN_CR3)
{
if (pVCpu->cpum.GstCtx.cr3 != aValues[iReg].Reg64)
{
CPUMSetGuestCR3(pVCpu, aValues[iReg].Reg64);
fUpdateCr3 = true;
}
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_CR4)
{
if (pVCpu->cpum.GstCtx.cr4 != aValues[iReg].Reg64)
{
CPUMSetGuestCR4(pVCpu, aValues[iReg].Reg64);
fMaybeChangedMode = true;
}
iReg++;
}
}
if (fWhat & CPUMCTX_EXTRN_APIC_TPR)
{
Assert(aenmNames[iReg] == WHvX64RegisterCr8);
APICSetTpr(pVCpu, (uint8_t)aValues[iReg].Reg64 << 4);
iReg++;
}
/* Debug registers. */
if (fWhat & CPUMCTX_EXTRN_DR7)
{
Assert(aenmNames[iReg] == WHvX64RegisterDr7);
if (pVCpu->cpum.GstCtx.dr[7] != aValues[iReg].Reg64)
CPUMSetGuestDR7(pVCpu, aValues[iReg].Reg64);
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_DR7; /* Hack alert! Avoids asserting when processing CPUMCTX_EXTRN_DR0_DR3. */
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_DR0_DR3)
{
Assert(aenmNames[iReg] == WHvX64RegisterDr0);
Assert(aenmNames[iReg+3] == WHvX64RegisterDr3);
if (pVCpu->cpum.GstCtx.dr[0] != aValues[iReg].Reg64)
CPUMSetGuestDR0(pVCpu, aValues[iReg].Reg64);
iReg++;
if (pVCpu->cpum.GstCtx.dr[1] != aValues[iReg].Reg64)
CPUMSetGuestDR1(pVCpu, aValues[iReg].Reg64);
iReg++;
if (pVCpu->cpum.GstCtx.dr[2] != aValues[iReg].Reg64)
CPUMSetGuestDR2(pVCpu, aValues[iReg].Reg64);
iReg++;
if (pVCpu->cpum.GstCtx.dr[3] != aValues[iReg].Reg64)
CPUMSetGuestDR3(pVCpu, aValues[iReg].Reg64);
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_DR6)
{
Assert(aenmNames[iReg] == WHvX64RegisterDr6);
if (pVCpu->cpum.GstCtx.dr[6] != aValues[iReg].Reg64)
CPUMSetGuestDR6(pVCpu, aValues[iReg].Reg64);
iReg++;
}
/* Floating point state. */
if (fWhat & CPUMCTX_EXTRN_X87)
{
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[0].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[0].au64[1], WHvX64RegisterFpMmx0);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[1].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[1].au64[1], WHvX64RegisterFpMmx1);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[2].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[2].au64[1], WHvX64RegisterFpMmx2);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[3].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[3].au64[1], WHvX64RegisterFpMmx3);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[4].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[4].au64[1], WHvX64RegisterFpMmx4);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[5].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[5].au64[1], WHvX64RegisterFpMmx5);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[6].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[6].au64[1], WHvX64RegisterFpMmx6);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aRegs[7].au64[0], pVCpu->cpum.GstCtx.XState.x87.aRegs[7].au64[1], WHvX64RegisterFpMmx7);
Assert(aenmNames[iReg] == WHvX64RegisterFpControlStatus);
pVCpu->cpum.GstCtx.XState.x87.FCW = aValues[iReg].FpControlStatus.FpControl;
pVCpu->cpum.GstCtx.XState.x87.FSW = aValues[iReg].FpControlStatus.FpStatus;
pVCpu->cpum.GstCtx.XState.x87.FTW = aValues[iReg].FpControlStatus.FpTag
/*| (aValues[iReg].FpControlStatus.Reserved << 8)*/;
pVCpu->cpum.GstCtx.XState.x87.FOP = aValues[iReg].FpControlStatus.LastFpOp;
pVCpu->cpum.GstCtx.XState.x87.FPUIP = (uint32_t)aValues[iReg].FpControlStatus.LastFpRip;
pVCpu->cpum.GstCtx.XState.x87.CS = (uint16_t)(aValues[iReg].FpControlStatus.LastFpRip >> 32);
pVCpu->cpum.GstCtx.XState.x87.Rsrvd1 = (uint16_t)(aValues[iReg].FpControlStatus.LastFpRip >> 48);
iReg++;
}
if (fWhat & (CPUMCTX_EXTRN_X87 | CPUMCTX_EXTRN_SSE_AVX))
{
Assert(aenmNames[iReg] == WHvX64RegisterXmmControlStatus);
if (fWhat & CPUMCTX_EXTRN_X87)
{
pVCpu->cpum.GstCtx.XState.x87.FPUDP = (uint32_t)aValues[iReg].XmmControlStatus.LastFpRdp;
pVCpu->cpum.GstCtx.XState.x87.DS = (uint16_t)(aValues[iReg].XmmControlStatus.LastFpRdp >> 32);
pVCpu->cpum.GstCtx.XState.x87.Rsrvd2 = (uint16_t)(aValues[iReg].XmmControlStatus.LastFpRdp >> 48);
}
pVCpu->cpum.GstCtx.XState.x87.MXCSR = aValues[iReg].XmmControlStatus.XmmStatusControl;
pVCpu->cpum.GstCtx.XState.x87.MXCSR_MASK = aValues[iReg].XmmControlStatus.XmmStatusControlMask; /** @todo ??? (Isn't this an output field?) */
iReg++;
}
/* Vector state. */
if (fWhat & CPUMCTX_EXTRN_SSE_AVX)
{
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 0].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 0].uXmm.s.Hi, WHvX64RegisterXmm0);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 1].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 1].uXmm.s.Hi, WHvX64RegisterXmm1);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 2].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 2].uXmm.s.Hi, WHvX64RegisterXmm2);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 3].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 3].uXmm.s.Hi, WHvX64RegisterXmm3);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 4].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 4].uXmm.s.Hi, WHvX64RegisterXmm4);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 5].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 5].uXmm.s.Hi, WHvX64RegisterXmm5);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 6].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 6].uXmm.s.Hi, WHvX64RegisterXmm6);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 7].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 7].uXmm.s.Hi, WHvX64RegisterXmm7);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 8].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 8].uXmm.s.Hi, WHvX64RegisterXmm8);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[ 9].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[ 9].uXmm.s.Hi, WHvX64RegisterXmm9);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[10].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[10].uXmm.s.Hi, WHvX64RegisterXmm10);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[11].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[11].uXmm.s.Hi, WHvX64RegisterXmm11);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[12].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[12].uXmm.s.Hi, WHvX64RegisterXmm12);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[13].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[13].uXmm.s.Hi, WHvX64RegisterXmm13);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[14].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[14].uXmm.s.Hi, WHvX64RegisterXmm14);
GET_REG128(pVCpu->cpum.GstCtx.XState.x87.aXMM[15].uXmm.s.Lo, pVCpu->cpum.GstCtx.XState.x87.aXMM[15].uXmm.s.Hi, WHvX64RegisterXmm15);
}
/* MSRs */
// WHvX64RegisterTsc - don't touch
if (fWhat & CPUMCTX_EXTRN_EFER)
{
Assert(aenmNames[iReg] == WHvX64RegisterEfer);
if (aValues[iReg].Reg64 != pVCpu->cpum.GstCtx.msrEFER)
{
Log7(("NEM/%u: MSR EFER changed %RX64 -> %RX64\n", pVCpu->idCpu, pVCpu->cpum.GstCtx.msrEFER, aValues[iReg].Reg64));
if ((aValues[iReg].Reg64 ^ pVCpu->cpum.GstCtx.msrEFER) & MSR_K6_EFER_NXE)
PGMNotifyNxeChanged(pVCpu, RT_BOOL(aValues[iReg].Reg64 & MSR_K6_EFER_NXE));
pVCpu->cpum.GstCtx.msrEFER = aValues[iReg].Reg64;
fMaybeChangedMode = true;
}
iReg++;
}
if (fWhat & CPUMCTX_EXTRN_KERNEL_GS_BASE)
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrKERNELGSBASE, WHvX64RegisterKernelGsBase, "MSR KERNEL_GS_BASE");
if (fWhat & CPUMCTX_EXTRN_SYSENTER_MSRS)
{
GET_REG64_LOG7(pVCpu->cpum.GstCtx.SysEnter.cs, WHvX64RegisterSysenterCs, "MSR SYSENTER.CS");
GET_REG64_LOG7(pVCpu->cpum.GstCtx.SysEnter.eip, WHvX64RegisterSysenterEip, "MSR SYSENTER.EIP");
GET_REG64_LOG7(pVCpu->cpum.GstCtx.SysEnter.esp, WHvX64RegisterSysenterEsp, "MSR SYSENTER.ESP");
}
if (fWhat & CPUMCTX_EXTRN_SYSCALL_MSRS)
{
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrSTAR, WHvX64RegisterStar, "MSR STAR");
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrLSTAR, WHvX64RegisterLstar, "MSR LSTAR");
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrCSTAR, WHvX64RegisterCstar, "MSR CSTAR");
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrSFMASK, WHvX64RegisterSfmask, "MSR SFMASK");
}
if (fWhat & (CPUMCTX_EXTRN_TSC_AUX | CPUMCTX_EXTRN_OTHER_MSRS))
{
PCPUMCTXMSRS const pCtxMsrs = CPUMQueryGuestCtxMsrsPtr(pVCpu);
if (fWhat & CPUMCTX_EXTRN_TSC_AUX)
GET_REG64_LOG7(pCtxMsrs->msr.TscAux, WHvX64RegisterTscAux, "MSR TSC_AUX");
if (fWhat & CPUMCTX_EXTRN_OTHER_MSRS)
{
Assert(aenmNames[iReg] == WHvX64RegisterApicBase);
const uint64_t uOldBase = APICGetBaseMsrNoCheck(pVCpu);
if (aValues[iReg].Reg64 != uOldBase)
{
Log7(("NEM/%u: MSR APICBase changed %RX64 -> %RX64 (%RX64)\n",
pVCpu->idCpu, uOldBase, aValues[iReg].Reg64, aValues[iReg].Reg64 ^ uOldBase));
int rc2 = APICSetBaseMsr(pVCpu, aValues[iReg].Reg64);
AssertLogRelMsg(rc2 == VINF_SUCCESS, ("%Rrc %RX64\n", rc2, aValues[iReg].Reg64));
}
iReg++;
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrPAT, WHvX64RegisterPat, "MSR PAT");
#if 0 /*def LOG_ENABLED*/ /** @todo something's wrong with HvX64RegisterMtrrCap? (AMD) */
GET_REG64_LOG7(pVCpu->cpum.GstCtx.msrPAT, WHvX64RegisterMsrMtrrCap);
#endif
GET_REG64_LOG7(pCtxMsrs->msr.MtrrDefType, WHvX64RegisterMsrMtrrDefType, "MSR MTRR_DEF_TYPE");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix64K_00000, WHvX64RegisterMsrMtrrFix64k00000, "MSR MTRR_FIX_64K_00000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix16K_80000, WHvX64RegisterMsrMtrrFix16k80000, "MSR MTRR_FIX_16K_80000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix16K_A0000, WHvX64RegisterMsrMtrrFix16kA0000, "MSR MTRR_FIX_16K_A0000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_C0000, WHvX64RegisterMsrMtrrFix4kC0000, "MSR MTRR_FIX_4K_C0000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_C8000, WHvX64RegisterMsrMtrrFix4kC8000, "MSR MTRR_FIX_4K_C8000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_D0000, WHvX64RegisterMsrMtrrFix4kD0000, "MSR MTRR_FIX_4K_D0000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_D8000, WHvX64RegisterMsrMtrrFix4kD8000, "MSR MTRR_FIX_4K_D8000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_E0000, WHvX64RegisterMsrMtrrFix4kE0000, "MSR MTRR_FIX_4K_E0000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_E8000, WHvX64RegisterMsrMtrrFix4kE8000, "MSR MTRR_FIX_4K_E8000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_F0000, WHvX64RegisterMsrMtrrFix4kF0000, "MSR MTRR_FIX_4K_F0000");
GET_REG64_LOG7(pCtxMsrs->msr.MtrrFix4K_F8000, WHvX64RegisterMsrMtrrFix4kF8000, "MSR MTRR_FIX_4K_F8000");
/** @todo look for HvX64RegisterIa32MiscEnable and HvX64RegisterIa32FeatureControl? */
}
}
/* Interruptibility. */
if (fWhat & (CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI))
{
Assert(aenmNames[iReg] == WHvRegisterInterruptState);
Assert(aenmNames[iReg + 1] == WHvX64RegisterRip);
if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_INHIBIT_INT))
pVCpu->nem.s.fLastInterruptShadow = CPUMUpdateInterruptShadowEx(&pVCpu->cpum.GstCtx,
aValues[iReg].InterruptState.InterruptShadow,
aValues[iReg + 1].Reg64);
if (!(pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_INHIBIT_NMI))
CPUMUpdateInterruptInhibitingByNmi(&pVCpu->cpum.GstCtx, aValues[iReg].InterruptState.NmiMasked);
fWhat |= CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI;
iReg += 2;
}
/* Event injection. */
/// @todo WHvRegisterPendingInterruption
Assert(aenmNames[iReg] == WHvRegisterPendingInterruption);
if (aValues[iReg].PendingInterruption.InterruptionPending)
{
Log7(("PendingInterruption: type=%u vector=%#x errcd=%RTbool/%#x instr-len=%u nested=%u\n",
aValues[iReg].PendingInterruption.InterruptionType, aValues[iReg].PendingInterruption.InterruptionVector,
aValues[iReg].PendingInterruption.DeliverErrorCode, aValues[iReg].PendingInterruption.ErrorCode,
aValues[iReg].PendingInterruption.InstructionLength, aValues[iReg].PendingInterruption.NestedEvent));
AssertMsg((aValues[iReg].PendingInterruption.AsUINT64 & UINT64_C(0xfc00)) == 0,
("%#RX64\n", aValues[iReg].PendingInterruption.AsUINT64));
}
/// @todo WHvRegisterPendingEvent
/* Almost done, just update extrn flags and maybe change PGM mode. */
pVCpu->cpum.GstCtx.fExtrn &= ~fWhat;
if (!(pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL | (CPUMCTX_EXTRN_NEM_WIN_MASK & ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT))))
pVCpu->cpum.GstCtx.fExtrn = 0;
/* Typical. */
if (!fMaybeChangedMode && !fUpdateCr3)
return VINF_SUCCESS;
/*
* Slow.
*/
if (fMaybeChangedMode)
{
int rc = PGMChangeMode(pVCpu, pVCpu->cpum.GstCtx.cr0, pVCpu->cpum.GstCtx.cr4, pVCpu->cpum.GstCtx.msrEFER,
false /* fForce */);
AssertMsgReturn(rc == VINF_SUCCESS, ("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_NEM_IPE_1);
}
if (fUpdateCr3)
{
int rc = PGMUpdateCR3(pVCpu, pVCpu->cpum.GstCtx.cr3);
if (rc == VINF_SUCCESS)
{ /* likely */ }
else
AssertMsgFailedReturn(("rc=%Rrc\n", rc), RT_FAILURE_NP(rc) ? rc : VERR_NEM_IPE_2);
}
return VINF_SUCCESS;
}
/**
* Interface for importing state on demand (used by IEM).
*
* @returns VBox status code.
* @param pVCpu The cross context CPU structure.
* @param fWhat What to import, CPUMCTX_EXTRN_XXX.
*/
VMM_INT_DECL(int) NEMImportStateOnDemand(PVMCPUCC pVCpu, uint64_t fWhat)
{
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnDemand);
return nemHCWinCopyStateFromHyperV(pVCpu->pVMR3, pVCpu, fWhat);
}
/**
* Query the CPU tick counter and optionally the TSC_AUX MSR value.
*
* @returns VBox status code.
* @param pVCpu The cross context CPU structure.
* @param pcTicks Where to return the CPU tick count.
* @param puAux Where to return the TSC_AUX register value.
*/
VMM_INT_DECL(int) NEMHCQueryCpuTick(PVMCPUCC pVCpu, uint64_t *pcTicks, uint32_t *puAux)
{
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatQueryCpuTick);
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_VM_THREAD_NOT_EMT);
AssertReturn(VM_IS_NEM_ENABLED(pVM), VERR_NEM_IPE_9);
/* Call the offical API. */
WHV_REGISTER_NAME aenmNames[2] = { WHvX64RegisterTsc, WHvX64RegisterTscAux };
WHV_REGISTER_VALUE aValues[2] = { { {0, 0} }, { {0, 0} } };
Assert(RT_ELEMENTS(aenmNames) == RT_ELEMENTS(aValues));
HRESULT hrc = WHvGetVirtualProcessorRegisters(pVM->nem.s.hPartition, pVCpu->idCpu, aenmNames, 2, aValues);
AssertLogRelMsgReturn(SUCCEEDED(hrc),
("WHvGetVirtualProcessorRegisters(%p, %u,{tsc,tsc_aux},2,) -> %Rhrc (Last=%#x/%u)\n",
pVM->nem.s.hPartition, pVCpu->idCpu, hrc, RTNtLastStatusValue(), RTNtLastErrorValue())
, VERR_NEM_GET_REGISTERS_FAILED);
*pcTicks = aValues[0].Reg64;
if (puAux)
*puAux = pVCpu->cpum.GstCtx.fExtrn & CPUMCTX_EXTRN_TSC_AUX ? aValues[1].Reg64 : CPUMGetGuestTscAux(pVCpu);
return VINF_SUCCESS;
}
/**
* Resumes CPU clock (TSC) on all virtual CPUs.
*
* This is called by TM when the VM is started, restored, resumed or similar.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context CPU structure of the calling EMT.
* @param uPausedTscValue The TSC value at the time of pausing.
*/
VMM_INT_DECL(int) NEMHCResumeCpuTickOnAll(PVMCC pVM, PVMCPUCC pVCpu, uint64_t uPausedTscValue)
{
VMCPU_ASSERT_EMT_RETURN(pVCpu, VERR_VM_THREAD_NOT_EMT);
AssertReturn(VM_IS_NEM_ENABLED(pVM), VERR_NEM_IPE_9);
/*
* Call the offical API to do the job.
*/
if (pVM->cCpus > 1)
RTThreadYield(); /* Try decrease the chance that we get rescheduled in the middle. */
/* Start with the first CPU. */
WHV_REGISTER_NAME enmName = WHvX64RegisterTsc;
WHV_REGISTER_VALUE Value = { {0, 0} };
Value.Reg64 = uPausedTscValue;
uint64_t const uFirstTsc = ASMReadTSC();
HRESULT hrc = WHvSetVirtualProcessorRegisters(pVM->nem.s.hPartition, 0 /*iCpu*/, &enmName, 1, &Value);
AssertLogRelMsgReturn(SUCCEEDED(hrc),
("WHvSetVirtualProcessorRegisters(%p, 0,{tsc},2,%#RX64) -> %Rhrc (Last=%#x/%u)\n",
pVM->nem.s.hPartition, uPausedTscValue, hrc, RTNtLastStatusValue(), RTNtLastErrorValue())
, VERR_NEM_SET_TSC);
/* Do the other CPUs, adjusting for elapsed TSC and keeping finger crossed
that we don't introduce too much drift here. */
for (VMCPUID iCpu = 1; iCpu < pVM->cCpus; iCpu++)
{
Assert(enmName == WHvX64RegisterTsc);
const uint64_t offDelta = (ASMReadTSC() - uFirstTsc);
Value.Reg64 = uPausedTscValue + offDelta;
hrc = WHvSetVirtualProcessorRegisters(pVM->nem.s.hPartition, iCpu, &enmName, 1, &Value);
AssertLogRelMsgReturn(SUCCEEDED(hrc),
("WHvSetVirtualProcessorRegisters(%p, 0,{tsc},2,%#RX64 + %#RX64) -> %Rhrc (Last=%#x/%u)\n",
pVM->nem.s.hPartition, iCpu, uPausedTscValue, offDelta, hrc, RTNtLastStatusValue(), RTNtLastErrorValue())
, VERR_NEM_SET_TSC);
}
return VINF_SUCCESS;
}
#ifdef LOG_ENABLED
/**
* Get the virtual processor running status.
*/
DECLINLINE(VID_PROCESSOR_STATUS) nemHCWinCpuGetRunningStatus(PVMCPUCC pVCpu)
{
RTERRVARS Saved;
RTErrVarsSave(&Saved);
/*
* This API is disabled in release builds, it seems. On build 17101 it requires
* the following patch to be enabled (windbg): eb vid+12180 0f 84 98 00 00 00
*/
VID_PROCESSOR_STATUS enmCpuStatus = VidProcessorStatusUndefined;
NTSTATUS rcNt = g_pfnVidGetVirtualProcessorRunningStatus(pVCpu->pVMR3->nem.s.hPartitionDevice, pVCpu->idCpu, &enmCpuStatus);
AssertRC(rcNt);
RTErrVarsRestore(&Saved);
return enmCpuStatus;
}
/**
* Logs the current CPU state.
*/
NEM_TMPL_STATIC void nemHCWinLogState(PVMCC pVM, PVMCPUCC pVCpu)
{
if (LogIs3Enabled())
{
# if 0 // def IN_RING3 - causes lazy state import assertions all over CPUM.
char szRegs[4096];
DBGFR3RegPrintf(pVM->pUVM, pVCpu->idCpu, &szRegs[0], sizeof(szRegs),
"rax=%016VR{rax} rbx=%016VR{rbx} rcx=%016VR{rcx} rdx=%016VR{rdx}\n"
"rsi=%016VR{rsi} rdi=%016VR{rdi} r8 =%016VR{r8} r9 =%016VR{r9}\n"
"r10=%016VR{r10} r11=%016VR{r11} r12=%016VR{r12} r13=%016VR{r13}\n"
"r14=%016VR{r14} r15=%016VR{r15} %VRF{rflags}\n"
"rip=%016VR{rip} rsp=%016VR{rsp} rbp=%016VR{rbp}\n"
"cs={%04VR{cs} base=%016VR{cs_base} limit=%08VR{cs_lim} flags=%04VR{cs_attr}} cr0=%016VR{cr0}\n"
"ds={%04VR{ds} base=%016VR{ds_base} limit=%08VR{ds_lim} flags=%04VR{ds_attr}} cr2=%016VR{cr2}\n"
"es={%04VR{es} base=%016VR{es_base} limit=%08VR{es_lim} flags=%04VR{es_attr}} cr3=%016VR{cr3}\n"
"fs={%04VR{fs} base=%016VR{fs_base} limit=%08VR{fs_lim} flags=%04VR{fs_attr}} cr4=%016VR{cr4}\n"
"gs={%04VR{gs} base=%016VR{gs_base} limit=%08VR{gs_lim} flags=%04VR{gs_attr}} cr8=%016VR{cr8}\n"
"ss={%04VR{ss} base=%016VR{ss_base} limit=%08VR{ss_lim} flags=%04VR{ss_attr}}\n"
"dr0=%016VR{dr0} dr1=%016VR{dr1} dr2=%016VR{dr2} dr3=%016VR{dr3}\n"
"dr6=%016VR{dr6} dr7=%016VR{dr7}\n"
"gdtr=%016VR{gdtr_base}:%04VR{gdtr_lim} idtr=%016VR{idtr_base}:%04VR{idtr_lim} rflags=%08VR{rflags}\n"
"ldtr={%04VR{ldtr} base=%016VR{ldtr_base} limit=%08VR{ldtr_lim} flags=%08VR{ldtr_attr}}\n"
"tr ={%04VR{tr} base=%016VR{tr_base} limit=%08VR{tr_lim} flags=%08VR{tr_attr}}\n"
" sysenter={cs=%04VR{sysenter_cs} eip=%08VR{sysenter_eip} esp=%08VR{sysenter_esp}}\n"
" efer=%016VR{efer}\n"
" pat=%016VR{pat}\n"
" sf_mask=%016VR{sf_mask}\n"
"krnl_gs_base=%016VR{krnl_gs_base}\n"
" lstar=%016VR{lstar}\n"
" star=%016VR{star} cstar=%016VR{cstar}\n"
"fcw=%04VR{fcw} fsw=%04VR{fsw} ftw=%04VR{ftw} mxcsr=%04VR{mxcsr} mxcsr_mask=%04VR{mxcsr_mask}\n"
);
char szInstr[256];
DBGFR3DisasInstrEx(pVM->pUVM, pVCpu->idCpu, 0, 0,
DBGF_DISAS_FLAGS_CURRENT_GUEST | DBGF_DISAS_FLAGS_DEFAULT_MODE,
szInstr, sizeof(szInstr), NULL);
Log3(("%s%s\n", szRegs, szInstr));
# else
/** @todo stat logging in ring-0 */
RT_NOREF(pVM, pVCpu);
# endif
}
}
#endif /* LOG_ENABLED */
/**
* Translates the execution stat bitfield into a short log string, WinHv version.
*
* @returns Read-only log string.
* @param pExitCtx The exit context which state to summarize.
*/
static const char *nemR3WinExecStateToLogStr(WHV_VP_EXIT_CONTEXT const *pExitCtx)
{
unsigned u = (unsigned)pExitCtx->ExecutionState.InterruptionPending
| ((unsigned)pExitCtx->ExecutionState.DebugActive << 1)
| ((unsigned)pExitCtx->ExecutionState.InterruptShadow << 2);
#define SWITCH_IT(a_szPrefix) \
do \
switch (u)\
{ \
case 0x00: return a_szPrefix ""; \
case 0x01: return a_szPrefix ",Pnd"; \
case 0x02: return a_szPrefix ",Dbg"; \
case 0x03: return a_szPrefix ",Pnd,Dbg"; \
case 0x04: return a_szPrefix ",Shw"; \
case 0x05: return a_szPrefix ",Pnd,Shw"; \
case 0x06: return a_szPrefix ",Shw,Dbg"; \
case 0x07: return a_szPrefix ",Pnd,Shw,Dbg"; \
default: AssertFailedReturn("WTF?"); \
} \
while (0)
if (pExitCtx->ExecutionState.EferLma)
SWITCH_IT("LM");
else if (pExitCtx->ExecutionState.Cr0Pe)
SWITCH_IT("PM");
else
SWITCH_IT("RM");
#undef SWITCH_IT
}
/**
* Advances the guest RIP and clear EFLAGS.RF, WinHv version.
*
* This may clear VMCPU_FF_INHIBIT_INTERRUPTS.
*
* @param pVCpu The cross context virtual CPU structure.
* @param pExitCtx The exit context.
* @param cbMinInstr The minimum instruction length, or 1 if not unknown.
*/
DECLINLINE(void) nemR3WinAdvanceGuestRipAndClearRF(PVMCPUCC pVCpu, WHV_VP_EXIT_CONTEXT const *pExitCtx, uint8_t cbMinInstr)
{
Assert(!(pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS)));
/* Advance the RIP. */
Assert(pExitCtx->InstructionLength >= cbMinInstr); RT_NOREF_PV(cbMinInstr);
pVCpu->cpum.GstCtx.rip += pExitCtx->InstructionLength;
pVCpu->cpum.GstCtx.rflags.Bits.u1RF = 0;
CPUMClearInterruptShadow(&pVCpu->cpum.GstCtx);
}
/**
* State to pass between nemHCWinHandleMemoryAccess / nemR3WinWHvHandleMemoryAccess
* and nemHCWinHandleMemoryAccessPageCheckerCallback.
*/
typedef struct NEMHCWINHMACPCCSTATE
{
/** Input: Write access. */
bool fWriteAccess;
/** Output: Set if we did something. */
bool fDidSomething;
/** Output: Set it we should resume. */
bool fCanResume;
} NEMHCWINHMACPCCSTATE;
/**
* @callback_method_impl{FNPGMPHYSNEMCHECKPAGE,
* Worker for nemR3WinHandleMemoryAccess; pvUser points to a
* NEMHCWINHMACPCCSTATE structure. }
*/
NEM_TMPL_STATIC DECLCALLBACK(int)
nemHCWinHandleMemoryAccessPageCheckerCallback(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhys, PPGMPHYSNEMPAGEINFO pInfo, void *pvUser)
{
NEMHCWINHMACPCCSTATE *pState = (NEMHCWINHMACPCCSTATE *)pvUser;
pState->fDidSomething = false;
pState->fCanResume = false;
/* If A20 is disabled, we may need to make another query on the masked
page to get the correct protection information. */
uint8_t u2State = pInfo->u2NemState;
RTGCPHYS GCPhysSrc;
#ifdef NEM_WIN_WITH_A20
if ( pVM->nem.s.fA20Enabled
|| !NEM_WIN_IS_SUBJECT_TO_A20(GCPhys))
#endif
GCPhysSrc = GCPhys;
#ifdef NEM_WIN_WITH_A20
else
{
GCPhysSrc = GCPhys & ~(RTGCPHYS)RT_BIT_32(20);
PGMPHYSNEMPAGEINFO Info2;
int rc = PGMPhysNemPageInfoChecker(pVM, pVCpu, GCPhysSrc, pState->fWriteAccess, &Info2, NULL, NULL);
AssertRCReturn(rc, rc);
*pInfo = Info2;
pInfo->u2NemState = u2State;
}
#endif
/*
* Consolidate current page state with actual page protection and access type.
* We don't really consider downgrades here, as they shouldn't happen.
*/
/** @todo Someone at microsoft please explain:
* I'm not sure WTF was going on, but I ended up in a loop if I remapped a
* readonly page as writable (unmap, then map again). Specifically, this was an
* issue with the big VRAM mapping at 0xe0000000 when booing DSL 4.4.1. So, in
* a hope to work around that we no longer pre-map anything, just unmap stuff
* and do it lazily here. And here we will first unmap, restart, and then remap
* with new protection or backing.
*/
int rc;
switch (u2State)
{
case NEM_WIN_PAGE_STATE_UNMAPPED:
case NEM_WIN_PAGE_STATE_NOT_SET:
if (pInfo->fNemProt == NEM_PAGE_PROT_NONE)
{
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - #1\n", GCPhys));
return VINF_SUCCESS;
}
/* Don't bother remapping it if it's a write request to a non-writable page. */
if ( pState->fWriteAccess
&& !(pInfo->fNemProt & NEM_PAGE_PROT_WRITE))
{
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - #1w\n", GCPhys));
return VINF_SUCCESS;
}
/* Map the page. */
rc = nemHCNativeSetPhysPage(pVM,
pVCpu,
GCPhysSrc & ~(RTGCPHYS)X86_PAGE_OFFSET_MASK,
GCPhys & ~(RTGCPHYS)X86_PAGE_OFFSET_MASK,
pInfo->fNemProt,
&u2State,
true /*fBackingState*/);
pInfo->u2NemState = u2State;
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - synced => %s + %Rrc\n",
GCPhys, g_apszPageStates[u2State], rc));
pState->fDidSomething = true;
pState->fCanResume = true;
return rc;
case NEM_WIN_PAGE_STATE_READABLE:
if ( !(pInfo->fNemProt & NEM_PAGE_PROT_WRITE)
&& (pInfo->fNemProt & (NEM_PAGE_PROT_READ | NEM_PAGE_PROT_EXECUTE)))
{
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - #2\n", GCPhys));
return VINF_SUCCESS;
}
break;
case NEM_WIN_PAGE_STATE_WRITABLE:
if (pInfo->fNemProt & NEM_PAGE_PROT_WRITE)
{
if (pInfo->u2OldNemState == NEM_WIN_PAGE_STATE_WRITABLE)
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - #3a\n", GCPhys));
else
{
pState->fCanResume = true;
Log4(("nemHCWinHandleMemoryAccessPageCheckerCallback: %RGp - #3b (%s -> %s)\n",
GCPhys, g_apszPageStates[pInfo->u2OldNemState], g_apszPageStates[u2State]));
}
return VINF_SUCCESS;
}
break;
default:
AssertLogRelMsgFailedReturn(("u2State=%#x\n", u2State), VERR_NEM_IPE_4);
}
/*
* Unmap and restart the instruction.
* If this fails, which it does every so often, just unmap everything for now.
*/
/** @todo figure out whether we mess up the state or if it's WHv. */
STAM_REL_PROFILE_START(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
HRESULT hrc = WHvUnmapGpaRange(pVM->nem.s.hPartition, GCPhys, X86_PAGE_SIZE);
STAM_REL_PROFILE_STOP(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
if (SUCCEEDED(hrc))
{
pState->fDidSomething = true;
pState->fCanResume = true;
pInfo->u2NemState = NEM_WIN_PAGE_STATE_UNMAPPED;
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPage);
uint32_t cMappedPages = ASMAtomicDecU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
Log5(("NEM GPA unmapped/exit: %RGp (was %s, cMappedPages=%u)\n", GCPhys, g_apszPageStates[u2State], cMappedPages));
return VINF_SUCCESS;
}
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPageFailed);
LogRel(("nemHCWinHandleMemoryAccessPageCheckerCallback/unmap: GCPhysDst=%RGp %s hrc=%Rhrc (%#x)\n",
GCPhys, g_apszPageStates[u2State], hrc, hrc));
return VERR_NEM_UNMAP_PAGES_FAILED;
}
/**
* Wrapper around nemHCWinCopyStateFromHyperV.
*
* Unlike the wrapped APIs, this checks whether it's necessary.
*
* @returns VBox strict status code.
* @param pVCpu The cross context per CPU structure.
* @param fWhat What to import.
* @param pszCaller Who is doing the importing.
*/
DECLINLINE(VBOXSTRICTRC) nemHCWinImportStateIfNeededStrict(PVMCPUCC pVCpu, uint64_t fWhat, const char *pszCaller)
{
if (pVCpu->cpum.GstCtx.fExtrn & fWhat)
{
RT_NOREF(pszCaller);
int rc = nemHCWinCopyStateFromHyperV(pVCpu->pVMR3, pVCpu, fWhat);
AssertRCReturn(rc, rc);
}
return VINF_SUCCESS;
}
/**
* Copies register state from the (common) exit context.
*
* ASSUMES no state copied yet.
*
* @param pVCpu The cross context per CPU structure.
* @param pExitCtx The common exit context.
* @sa nemHCWinCopyStateFromX64Header
*/
DECLINLINE(void) nemR3WinCopyStateFromX64Header(PVMCPUCC pVCpu, WHV_VP_EXIT_CONTEXT const *pExitCtx)
{
Assert( (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_INHIBIT_INT))
== (CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_INHIBIT_INT));
NEM_WIN_COPY_BACK_SEG(pVCpu->cpum.GstCtx.cs, pExitCtx->Cs);
pVCpu->cpum.GstCtx.rip = pExitCtx->Rip;
pVCpu->cpum.GstCtx.rflags.u = pExitCtx->Rflags;
pVCpu->nem.s.fLastInterruptShadow = CPUMUpdateInterruptShadowEx(&pVCpu->cpum.GstCtx,
pExitCtx->ExecutionState.InterruptShadow,
pExitCtx->Rip);
APICSetTpr(pVCpu, pExitCtx->Cr8 << 4);
pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_APIC_TPR);
}
/**
* Deals with memory access exits (WHvRunVpExitReasonMemoryAccess).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageMemory
*/
NEM_TMPL_STATIC VBOXSTRICTRC
nemR3WinHandleExitMemory(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
uint64_t const uHostTsc = ASMReadTSC();
Assert(pExit->MemoryAccess.AccessInfo.AccessType != 3);
/*
* Whatever we do, we must clear pending event injection upon resume.
*/
if (pExit->VpContext.ExecutionState.InterruptionPending)
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT;
/*
* Ask PGM for information about the given GCPhys. We need to check if we're
* out of sync first.
*/
NEMHCWINHMACPCCSTATE State = { pExit->MemoryAccess.AccessInfo.AccessType == WHvMemoryAccessWrite, false, false };
PGMPHYSNEMPAGEINFO Info;
int rc = PGMPhysNemPageInfoChecker(pVM, pVCpu, pExit->MemoryAccess.Gpa, State.fWriteAccess, &Info,
nemHCWinHandleMemoryAccessPageCheckerCallback, &State);
if (RT_SUCCESS(rc))
{
if (Info.fNemProt & ( pExit->MemoryAccess.AccessInfo.AccessType == WHvMemoryAccessWrite
? NEM_PAGE_PROT_WRITE : NEM_PAGE_PROT_READ))
{
if (State.fCanResume)
{
Log4(("MemExit/%u: %04x:%08RX64/%s: %RGp (=>%RHp) %s fProt=%u%s%s%s; restarting (%s)\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MemoryAccess.Gpa, Info.HCPhys, g_apszPageStates[Info.u2NemState], Info.fNemProt,
Info.fHasHandlers ? " handlers" : "", Info.fZeroPage ? " zero-pg" : "",
State.fDidSomething ? "" : " no-change", g_apszHvInterceptAccessTypes[pExit->MemoryAccess.AccessInfo.AccessType]));
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_MEMORY_ACCESS),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, uHostTsc);
return VINF_SUCCESS;
}
}
Log4(("MemExit/%u: %04x:%08RX64/%s: %RGp (=>%RHp) %s fProt=%u%s%s%s; emulating (%s)\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MemoryAccess.Gpa, Info.HCPhys, g_apszPageStates[Info.u2NemState], Info.fNemProt,
Info.fHasHandlers ? " handlers" : "", Info.fZeroPage ? " zero-pg" : "",
State.fDidSomething ? "" : " no-change", g_apszHvInterceptAccessTypes[pExit->MemoryAccess.AccessInfo.AccessType]));
}
else
Log4(("MemExit/%u: %04x:%08RX64/%s: %RGp rc=%Rrc%s; emulating (%s)\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MemoryAccess.Gpa, rc, State.fDidSomething ? " modified-backing" : "",
g_apszHvInterceptAccessTypes[pExit->MemoryAccess.AccessInfo.AccessType]));
/*
* Emulate the memory access, either access handler or special memory.
*/
PCEMEXITREC pExitRec = EMHistoryAddExit(pVCpu,
pExit->MemoryAccess.AccessInfo.AccessType == WHvMemoryAccessWrite
? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_WRITE)
: EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MMIO_READ),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, uHostTsc);
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
rc = nemHCWinCopyStateFromHyperV(pVM, pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM | CPUMCTX_EXTRN_DS | CPUMCTX_EXTRN_ES);
AssertRCReturn(rc, rc);
if (pExit->VpContext.ExecutionState.Reserved0 || pExit->VpContext.ExecutionState.Reserved1)
Log(("MemExit/Hdr/State: Reserved0=%#x Reserved1=%#x\n", pExit->VpContext.ExecutionState.Reserved0, pExit->VpContext.ExecutionState.Reserved1));
VBOXSTRICTRC rcStrict;
if (!pExitRec)
{
//if (pMsg->InstructionByteCount > 0)
// Log4(("InstructionByteCount=%#x %.16Rhxs\n", pMsg->InstructionByteCount, pMsg->InstructionBytes));
if (pExit->MemoryAccess.InstructionByteCount > 0)
rcStrict = IEMExecOneWithPrefetchedByPC(pVCpu, pExit->VpContext.Rip, pExit->MemoryAccess.InstructionBytes, pExit->MemoryAccess.InstructionByteCount);
else
rcStrict = IEMExecOne(pVCpu);
/** @todo do we need to do anything wrt debugging here? */
}
else
{
/* Frequent access or probing. */
rcStrict = EMHistoryExec(pVCpu, pExitRec, 0);
Log4(("MemExit/%u: %04x:%08RX64/%s: EMHistoryExec -> %Rrc + %04x:%08RX64\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
VBOXSTRICTRC_VAL(rcStrict), pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip));
}
return rcStrict;
}
/**
* Deals with I/O port access exits (WHvRunVpExitReasonX64IoPortAccess).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageIoPort
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExitIoPort(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
Assert( pExit->IoPortAccess.AccessInfo.AccessSize == 1
|| pExit->IoPortAccess.AccessInfo.AccessSize == 2
|| pExit->IoPortAccess.AccessInfo.AccessSize == 4);
/*
* Whatever we do, we must clear pending event injection upon resume.
*/
if (pExit->VpContext.ExecutionState.InterruptionPending)
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT;
/*
* Add history first to avoid two paths doing EMHistoryExec calls.
*/
PCEMEXITREC pExitRec = EMHistoryAddExit(pVCpu,
!pExit->IoPortAccess.AccessInfo.StringOp
? ( pExit->MemoryAccess.AccessInfo.AccessType == WHvMemoryAccessWrite
? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_WRITE)
: EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_READ))
: ( pExit->MemoryAccess.AccessInfo.AccessType == WHvMemoryAccessWrite
? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_STR_WRITE)
: EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_IO_PORT_STR_READ)),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
if (!pExitRec)
{
VBOXSTRICTRC rcStrict;
if (!pExit->IoPortAccess.AccessInfo.StringOp)
{
/*
* Simple port I/O.
*/
static uint32_t const s_fAndMask[8] =
{ UINT32_MAX, UINT32_C(0xff), UINT32_C(0xffff), UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX, UINT32_MAX };
uint32_t const fAndMask = s_fAndMask[pExit->IoPortAccess.AccessInfo.AccessSize];
if (pExit->IoPortAccess.AccessInfo.IsWrite)
{
rcStrict = IOMIOPortWrite(pVM, pVCpu, pExit->IoPortAccess.PortNumber,
(uint32_t)pExit->IoPortAccess.Rax & fAndMask,
pExit->IoPortAccess.AccessInfo.AccessSize);
Log4(("IOExit/%u: %04x:%08RX64/%s: OUT %#x, %#x LB %u rcStrict=%Rrc\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->IoPortAccess.PortNumber, (uint32_t)pExit->IoPortAccess.Rax & fAndMask,
pExit->IoPortAccess.AccessInfo.AccessSize, VBOXSTRICTRC_VAL(rcStrict) ));
if (IOM_SUCCESS(rcStrict))
{
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
nemR3WinAdvanceGuestRipAndClearRF(pVCpu, &pExit->VpContext, 1);
}
}
else
{
uint32_t uValue = 0;
rcStrict = IOMIOPortRead(pVM, pVCpu, pExit->IoPortAccess.PortNumber, &uValue,
pExit->IoPortAccess.AccessInfo.AccessSize);
Log4(("IOExit/%u: %04x:%08RX64/%s: IN %#x LB %u -> %#x, rcStrict=%Rrc\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->IoPortAccess.PortNumber, pExit->IoPortAccess.AccessInfo.AccessSize, uValue, VBOXSTRICTRC_VAL(rcStrict) ));
if (IOM_SUCCESS(rcStrict))
{
if (pExit->IoPortAccess.AccessInfo.AccessSize != 4)
pVCpu->cpum.GstCtx.rax = (pExit->IoPortAccess.Rax & ~(uint64_t)fAndMask) | (uValue & fAndMask);
else
pVCpu->cpum.GstCtx.rax = uValue;
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_RAX;
Log4(("IOExit/%u: RAX %#RX64 -> %#RX64\n", pVCpu->idCpu, pExit->IoPortAccess.Rax, pVCpu->cpum.GstCtx.rax));
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
nemR3WinAdvanceGuestRipAndClearRF(pVCpu, &pExit->VpContext, 1);
}
}
}
else
{
/*
* String port I/O.
*/
/** @todo Someone at Microsoft please explain how we can get the address mode
* from the IoPortAccess.VpContext. CS.Attributes is only sufficient for
* getting the default mode, it can always be overridden by a prefix. This
* forces us to interpret the instruction from opcodes, which is suboptimal.
* Both AMD-V and VT-x includes the address size in the exit info, at least on
* CPUs that are reasonably new.
*
* Of course, it's possible this is an undocumented and we just need to do some
* experiments to figure out how it's communicated. Alternatively, we can scan
* the opcode bytes for possible evil prefixes.
*/
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
pVCpu->cpum.GstCtx.fExtrn &= ~( CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDI | CPUMCTX_EXTRN_RSI
| CPUMCTX_EXTRN_DS | CPUMCTX_EXTRN_ES);
NEM_WIN_COPY_BACK_SEG(pVCpu->cpum.GstCtx.ds, pExit->IoPortAccess.Ds);
NEM_WIN_COPY_BACK_SEG(pVCpu->cpum.GstCtx.es, pExit->IoPortAccess.Es);
pVCpu->cpum.GstCtx.rax = pExit->IoPortAccess.Rax;
pVCpu->cpum.GstCtx.rcx = pExit->IoPortAccess.Rcx;
pVCpu->cpum.GstCtx.rdi = pExit->IoPortAccess.Rdi;
pVCpu->cpum.GstCtx.rsi = pExit->IoPortAccess.Rsi;
int rc = nemHCWinCopyStateFromHyperV(pVM, pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM);
AssertRCReturn(rc, rc);
Log4(("IOExit/%u: %04x:%08RX64/%s: %s%s %#x LB %u (emulating)\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->IoPortAccess.AccessInfo.RepPrefix ? "REP " : "",
pExit->IoPortAccess.AccessInfo.IsWrite ? "OUTS" : "INS",
pExit->IoPortAccess.PortNumber, pExit->IoPortAccess.AccessInfo.AccessSize ));
rcStrict = IEMExecOne(pVCpu);
}
if (IOM_SUCCESS(rcStrict))
{
/*
* Do debug checks.
*/
if ( pExit->VpContext.ExecutionState.DebugActive /** @todo Microsoft: Does DebugActive this only reflect DR7? */
|| (pExit->VpContext.Rflags & X86_EFL_TF)
|| DBGFBpIsHwIoArmed(pVM) )
{
/** @todo Debugging. */
}
}
return rcStrict;
}
/*
* Frequent exit or something needing probing.
* Get state and call EMHistoryExec.
*/
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
if (!pExit->IoPortAccess.AccessInfo.StringOp)
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_RAX;
else
{
pVCpu->cpum.GstCtx.fExtrn &= ~( CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDI | CPUMCTX_EXTRN_RSI
| CPUMCTX_EXTRN_DS | CPUMCTX_EXTRN_ES);
NEM_WIN_COPY_BACK_SEG(pVCpu->cpum.GstCtx.ds, pExit->IoPortAccess.Ds);
NEM_WIN_COPY_BACK_SEG(pVCpu->cpum.GstCtx.es, pExit->IoPortAccess.Es);
pVCpu->cpum.GstCtx.rcx = pExit->IoPortAccess.Rcx;
pVCpu->cpum.GstCtx.rdi = pExit->IoPortAccess.Rdi;
pVCpu->cpum.GstCtx.rsi = pExit->IoPortAccess.Rsi;
}
pVCpu->cpum.GstCtx.rax = pExit->IoPortAccess.Rax;
int rc = nemHCWinCopyStateFromHyperV(pVM, pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM);
AssertRCReturn(rc, rc);
Log4(("IOExit/%u: %04x:%08RX64/%s: %s%s%s %#x LB %u -> EMHistoryExec\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->IoPortAccess.AccessInfo.RepPrefix ? "REP " : "",
pExit->IoPortAccess.AccessInfo.IsWrite ? "OUT" : "IN",
pExit->IoPortAccess.AccessInfo.StringOp ? "S" : "",
pExit->IoPortAccess.PortNumber, pExit->IoPortAccess.AccessInfo.AccessSize));
VBOXSTRICTRC rcStrict = EMHistoryExec(pVCpu, pExitRec, 0);
Log4(("IOExit/%u: %04x:%08RX64/%s: EMHistoryExec -> %Rrc + %04x:%08RX64\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
VBOXSTRICTRC_VAL(rcStrict), pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip));
return rcStrict;
}
/**
* Deals with interrupt window exits (WHvRunVpExitReasonX64InterruptWindow).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageInterruptWindow
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExitInterruptWindow(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
/*
* Assert message sanity.
*/
AssertMsg( pExit->InterruptWindow.DeliverableType == WHvX64PendingInterrupt
|| pExit->InterruptWindow.DeliverableType == WHvX64PendingNmi,
("%#x\n", pExit->InterruptWindow.DeliverableType));
/*
* Just copy the state we've got and handle it in the loop for now.
*/
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_INTTERRUPT_WINDOW),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
Log4(("IntWinExit/%u: %04x:%08RX64/%s: %u IF=%d InterruptShadow=%d CR8=%#x\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->InterruptWindow.DeliverableType, RT_BOOL(pExit->VpContext.Rflags & X86_EFL_IF),
pExit->VpContext.ExecutionState.InterruptShadow, pExit->VpContext.Cr8));
/** @todo call nemHCWinHandleInterruptFF */
RT_NOREF(pVM);
return VINF_SUCCESS;
}
/**
* Deals with CPUID exits (WHvRunVpExitReasonX64Cpuid).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageCpuId
*/
NEM_TMPL_STATIC VBOXSTRICTRC
nemR3WinHandleExitCpuId(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
PCEMEXITREC pExitRec = EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_CPUID),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
if (!pExitRec)
{
/*
* Soak up state and execute the instruction.
*/
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu,
IEM_CPUMCTX_EXTRN_EXEC_DECODED_NO_MEM_MASK
| CPUMCTX_EXTRN_CR3, /* May call PGMChangeMode() requiring cr3 (due to cr0 being imported). */
"CPUID");
if (rcStrict == VINF_SUCCESS)
{
/* Copy in the low register values (top is always cleared). */
pVCpu->cpum.GstCtx.rax = (uint32_t)pExit->CpuidAccess.Rax;
pVCpu->cpum.GstCtx.rcx = (uint32_t)pExit->CpuidAccess.Rcx;
pVCpu->cpum.GstCtx.rdx = (uint32_t)pExit->CpuidAccess.Rdx;
pVCpu->cpum.GstCtx.rbx = (uint32_t)pExit->CpuidAccess.Rbx;
pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
/* Execute the decoded instruction. */
rcStrict = IEMExecDecodedCpuid(pVCpu, pExit->VpContext.InstructionLength);
Log4(("CpuIdExit/%u: %04x:%08RX64/%s: rax=%08RX64 / rcx=%08RX64 / rdx=%08RX64 / rbx=%08RX64 -> %08RX32 / %08RX32 / %08RX32 / %08RX32 (hv: %08RX64 / %08RX64 / %08RX64 / %08RX64)\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->CpuidAccess.Rax, pExit->CpuidAccess.Rcx, pExit->CpuidAccess.Rdx, pExit->CpuidAccess.Rbx,
pVCpu->cpum.GstCtx.eax, pVCpu->cpum.GstCtx.ecx, pVCpu->cpum.GstCtx.edx, pVCpu->cpum.GstCtx.ebx,
pExit->CpuidAccess.DefaultResultRax, pExit->CpuidAccess.DefaultResultRcx, pExit->CpuidAccess.DefaultResultRdx, pExit->CpuidAccess.DefaultResultRbx));
}
RT_NOREF_PV(pVM);
return rcStrict;
}
/*
* Frequent exit or something needing probing.
* Get state and call EMHistoryExec.
*/
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
pVCpu->cpum.GstCtx.rax = pExit->CpuidAccess.Rax;
pVCpu->cpum.GstCtx.rcx = pExit->CpuidAccess.Rcx;
pVCpu->cpum.GstCtx.rdx = pExit->CpuidAccess.Rdx;
pVCpu->cpum.GstCtx.rbx = pExit->CpuidAccess.Rbx;
pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RCX | CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RBX);
Log4(("CpuIdExit/%u: %04x:%08RX64/%s: rax=%08RX64 / rcx=%08RX64 / rdx=%08RX64 / rbx=%08RX64 (hv: %08RX64 / %08RX64 / %08RX64 / %08RX64) ==> EMHistoryExec\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->CpuidAccess.Rax, pExit->CpuidAccess.Rcx, pExit->CpuidAccess.Rdx, pExit->CpuidAccess.Rbx,
pExit->CpuidAccess.DefaultResultRax, pExit->CpuidAccess.DefaultResultRcx, pExit->CpuidAccess.DefaultResultRdx, pExit->CpuidAccess.DefaultResultRbx));
int rc = nemHCWinCopyStateFromHyperV(pVM, pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM);
AssertRCReturn(rc, rc);
VBOXSTRICTRC rcStrict = EMHistoryExec(pVCpu, pExitRec, 0);
Log4(("CpuIdExit/%u: %04x:%08RX64/%s: EMHistoryExec -> %Rrc + %04x:%08RX64\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
VBOXSTRICTRC_VAL(rcStrict), pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip));
return rcStrict;
}
/**
* Deals with MSR access exits (WHvRunVpExitReasonX64MsrAccess).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageMsr
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExitMsr(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
/*
* Check CPL as that's common to both RDMSR and WRMSR.
*/
VBOXSTRICTRC rcStrict;
if (pExit->VpContext.ExecutionState.Cpl == 0)
{
/*
* Get all the MSR state. Since we're getting EFER, we also need to
* get CR0, CR4 and CR3.
*/
PCEMEXITREC pExitRec = EMHistoryAddExit(pVCpu,
pExit->MsrAccess.AccessInfo.IsWrite
? EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_WRITE)
: EMEXIT_MAKE_FT(EMEXIT_F_KIND_EM, EMEXITTYPE_MSR_READ),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu,
(!pExitRec ? 0 : IEM_CPUMCTX_EXTRN_MUST_MASK)
| CPUMCTX_EXTRN_ALL_MSRS | CPUMCTX_EXTRN_CR0
| CPUMCTX_EXTRN_CR3 | CPUMCTX_EXTRN_CR4,
"MSRs");
if (rcStrict == VINF_SUCCESS)
{
if (!pExitRec)
{
/*
* Handle writes.
*/
if (pExit->MsrAccess.AccessInfo.IsWrite)
{
rcStrict = CPUMSetGuestMsr(pVCpu, pExit->MsrAccess.MsrNumber,
RT_MAKE_U64((uint32_t)pExit->MsrAccess.Rax, (uint32_t)pExit->MsrAccess.Rdx));
Log4(("MsrExit/%u: %04x:%08RX64/%s: WRMSR %08x, %08x:%08x -> %Rrc\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->MsrAccess.MsrNumber,
(uint32_t)pExit->MsrAccess.Rax, (uint32_t)pExit->MsrAccess.Rdx, VBOXSTRICTRC_VAL(rcStrict) ));
if (rcStrict == VINF_SUCCESS)
{
nemR3WinAdvanceGuestRipAndClearRF(pVCpu, &pExit->VpContext, 2);
return VINF_SUCCESS;
}
LogRel(("MsrExit/%u: %04x:%08RX64/%s: WRMSR %08x, %08x:%08x -> %Rrc!\n", pVCpu->idCpu,
pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MsrAccess.MsrNumber, (uint32_t)pExit->MsrAccess.Rax, (uint32_t)pExit->MsrAccess.Rdx,
VBOXSTRICTRC_VAL(rcStrict) ));
}
/*
* Handle reads.
*/
else
{
uint64_t uValue = 0;
rcStrict = CPUMQueryGuestMsr(pVCpu, pExit->MsrAccess.MsrNumber, &uValue);
Log4(("MsrExit/%u: %04x:%08RX64/%s: RDMSR %08x -> %08RX64 / %Rrc\n", pVCpu->idCpu,
pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MsrAccess.MsrNumber, uValue, VBOXSTRICTRC_VAL(rcStrict) ));
if (rcStrict == VINF_SUCCESS)
{
pVCpu->cpum.GstCtx.rax = (uint32_t)uValue;
pVCpu->cpum.GstCtx.rdx = uValue >> 32;
pVCpu->cpum.GstCtx.fExtrn &= ~(CPUMCTX_EXTRN_RAX | CPUMCTX_EXTRN_RDX);
nemR3WinAdvanceGuestRipAndClearRF(pVCpu, &pExit->VpContext, 2);
return VINF_SUCCESS;
}
LogRel(("MsrExit/%u: %04x:%08RX64/%s: RDMSR %08x -> %08RX64 / %Rrc\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->MsrAccess.MsrNumber,
uValue, VBOXSTRICTRC_VAL(rcStrict) ));
}
}
else
{
/*
* Handle frequent exit or something needing probing.
*/
Log4(("MsrExit/%u: %04x:%08RX64/%s: %sMSR %#08x\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MsrAccess.AccessInfo.IsWrite ? "WR" : "RD", pExit->MsrAccess.MsrNumber));
rcStrict = EMHistoryExec(pVCpu, pExitRec, 0);
Log4(("MsrExit/%u: %04x:%08RX64/%s: EMHistoryExec -> %Rrc + %04x:%08RX64\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
VBOXSTRICTRC_VAL(rcStrict), pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip));
return rcStrict;
}
}
else
{
LogRel(("MsrExit/%u: %04x:%08RX64/%s: %sMSR %08x -> %Rrc - msr state import\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->MsrAccess.AccessInfo.IsWrite ? "WR" : "RD", pExit->MsrAccess.MsrNumber, VBOXSTRICTRC_VAL(rcStrict) ));
return rcStrict;
}
}
else if (pExit->MsrAccess.AccessInfo.IsWrite)
Log4(("MsrExit/%u: %04x:%08RX64/%s: CPL %u -> #GP(0); WRMSR %08x, %08x:%08x\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.ExecutionState.Cpl,
pExit->MsrAccess.MsrNumber, (uint32_t)pExit->MsrAccess.Rax, (uint32_t)pExit->MsrAccess.Rdx ));
else
Log4(("MsrExit/%u: %04x:%08RX64/%s: CPL %u -> #GP(0); RDMSR %08x\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.ExecutionState.Cpl,
pExit->MsrAccess.MsrNumber));
/*
* If we get down here, we're supposed to #GP(0).
*/
rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM | CPUMCTX_EXTRN_ALL_MSRS, "MSR");
if (rcStrict == VINF_SUCCESS)
{
rcStrict = IEMInjectTrap(pVCpu, X86_XCPT_GP, TRPM_TRAP, 0, 0, 0);
if (rcStrict == VINF_IEM_RAISED_XCPT)
rcStrict = VINF_SUCCESS;
else if (rcStrict != VINF_SUCCESS)
Log4(("MsrExit/%u: Injecting #GP(0) failed: %Rrc\n", VBOXSTRICTRC_VAL(rcStrict) ));
}
RT_NOREF_PV(pVM);
return rcStrict;
}
/**
* Worker for nemHCWinHandleMessageException & nemR3WinHandleExitException that
* checks if the given opcodes are of interest at all.
*
* @returns true if interesting, false if not.
* @param cbOpcodes Number of opcode bytes available.
* @param pbOpcodes The opcode bytes.
* @param f64BitMode Whether we're in 64-bit mode.
*/
DECLINLINE(bool) nemHcWinIsInterestingUndefinedOpcode(uint8_t cbOpcodes, uint8_t const *pbOpcodes, bool f64BitMode)
{
/*
* Currently only interested in VMCALL and VMMCALL.
*/
while (cbOpcodes >= 3)
{
switch (pbOpcodes[0])
{
case 0x0f:
switch (pbOpcodes[1])
{
case 0x01:
switch (pbOpcodes[2])
{
case 0xc1: /* 0f 01 c1 VMCALL */
return true;
case 0xd9: /* 0f 01 d9 VMMCALL */
return true;
default:
break;
}
break;
}
break;
default:
return false;
/* prefixes */
case 0x40: case 0x41: case 0x42: case 0x43: case 0x44: case 0x45: case 0x46: case 0x47:
case 0x48: case 0x49: case 0x4a: case 0x4b: case 0x4c: case 0x4d: case 0x4e: case 0x4f:
if (!f64BitMode)
return false;
RT_FALL_THRU();
case X86_OP_PRF_CS:
case X86_OP_PRF_SS:
case X86_OP_PRF_DS:
case X86_OP_PRF_ES:
case X86_OP_PRF_FS:
case X86_OP_PRF_GS:
case X86_OP_PRF_SIZE_OP:
case X86_OP_PRF_SIZE_ADDR:
case X86_OP_PRF_LOCK:
case X86_OP_PRF_REPZ:
case X86_OP_PRF_REPNZ:
cbOpcodes--;
pbOpcodes++;
continue;
}
break;
}
return false;
}
/**
* Copies state included in a exception intercept exit.
*
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information.
* @param fClearXcpt Clear pending exception.
*/
DECLINLINE(void) nemR3WinCopyStateFromExceptionMessage(PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit, bool fClearXcpt)
{
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
if (fClearXcpt)
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT;
}
/**
* Advances the guest RIP by the number of bytes specified in @a cb.
*
* @param pVCpu The cross context virtual CPU structure.
* @param cb RIP increment value in bytes.
*/
DECLINLINE(void) nemHcWinAdvanceRip(PVMCPUCC pVCpu, uint32_t cb)
{
PCPUMCTX pCtx = &pVCpu->cpum.GstCtx;
pCtx->rip += cb;
/** @todo Why not clear RF too? */
CPUMClearInterruptShadow(&pVCpu->cpum.GstCtx);
}
/**
* Hacks its way around the lovely mesa driver's backdoor accesses.
*
* @sa hmR0VmxHandleMesaDrvGp
* @sa hmR0SvmHandleMesaDrvGp
*/
static int nemHcWinHandleMesaDrvGp(PVMCPUCC pVCpu, PCPUMCTX pCtx)
{
Assert(!(pCtx->fExtrn & (CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_CS | CPUMCTX_EXTRN_RFLAGS | CPUMCTX_EXTRN_GPRS_MASK)));
RT_NOREF(pCtx);
/* For now we'll just skip the instruction. */
nemHcWinAdvanceRip(pVCpu, 1);
return VINF_SUCCESS;
}
/**
* Checks if the \#GP'ing instruction is the mesa driver doing it's lovely
* backdoor logging w/o checking what it is running inside.
*
* This recognizes an "IN EAX,DX" instruction executed in flat ring-3, with the
* backdoor port and magic numbers loaded in registers.
*
* @returns true if it is, false if it isn't.
* @sa hmR0VmxIsMesaDrvGp
* @sa hmR0SvmIsMesaDrvGp
*/
DECLINLINE(bool) nemHcWinIsMesaDrvGp(PVMCPUCC pVCpu, PCPUMCTX pCtx, const uint8_t *pbInsn, uint32_t cbInsn)
{
/* #GP(0) is already checked by caller. */
/* Check magic and port. */
Assert(!(pCtx->fExtrn & (CPUMCTX_EXTRN_RDX | CPUMCTX_EXTRN_RAX)));
if (pCtx->dx != UINT32_C(0x5658))
return false;
if (pCtx->rax != UINT32_C(0x564d5868))
return false;
/* Flat ring-3 CS. */
if (CPUMGetGuestCPL(pVCpu) != 3)
return false;
if (pCtx->cs.u64Base != 0)
return false;
/* 0xed: IN eAX,dx */
if (cbInsn < 1) /* Play safe (shouldn't happen). */
{
uint8_t abInstr[1];
int rc = PGMPhysSimpleReadGCPtr(pVCpu, abInstr, pCtx->rip, sizeof(abInstr));
if (RT_FAILURE(rc))
return false;
if (abInstr[0] != 0xed)
return false;
}
else
{
if (pbInsn[0] != 0xed)
return false;
}
return true;
}
/**
* Deals with MSR access exits (WHvRunVpExitReasonException).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemR3WinHandleExitException
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExitException(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
/*
* Get most of the register state since we'll end up making IEM inject the
* event. The exception isn't normally flaged as a pending event, so duh.
*
* Note! We can optimize this later with event injection.
*/
Log4(("XcptExit/%u: %04x:%08RX64/%s: %x errcd=%#x parm=%RX64\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpException.ExceptionType,
pExit->VpException.ErrorCode, pExit->VpException.ExceptionParameter ));
nemR3WinCopyStateFromExceptionMessage(pVCpu, pExit, true /*fClearXcpt*/);
uint64_t fWhat = NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM;
if (pExit->VpException.ExceptionType == X86_XCPT_DB)
fWhat |= CPUMCTX_EXTRN_DR0_DR3 | CPUMCTX_EXTRN_DR7 | CPUMCTX_EXTRN_DR6;
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, fWhat, "Xcpt");
if (rcStrict != VINF_SUCCESS)
return rcStrict;
/*
* Handle the intercept.
*/
TRPMEVENT enmEvtType = TRPM_TRAP;
switch (pExit->VpException.ExceptionType)
{
/*
* We get undefined opcodes on VMMCALL(AMD) & VMCALL(Intel) instructions
* and need to turn them over to GIM.
*
* Note! We do not check fGIMTrapXcptUD here ASSUMING that GIM only wants
* #UD for handling non-native hypercall instructions. (IEM will
* decode both and let the GIM provider decide whether to accept it.)
*/
case X86_XCPT_UD:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionUd);
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_XCPT_UD),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
if (nemHcWinIsInterestingUndefinedOpcode(pExit->VpException.InstructionByteCount, pExit->VpException.InstructionBytes,
pExit->VpContext.ExecutionState.EferLma && pExit->VpContext.Cs.Long ))
{
rcStrict = IEMExecOneWithPrefetchedByPC(pVCpu, pExit->VpContext.Rip,
pExit->VpException.InstructionBytes,
pExit->VpException.InstructionByteCount);
Log4(("XcptExit/%u: %04x:%08RX64/%s: #UD -> emulated -> %Rrc\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip,
nemR3WinExecStateToLogStr(&pExit->VpContext), VBOXSTRICTRC_VAL(rcStrict) ));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionUdHandled);
return rcStrict;
}
Log4(("XcptExit/%u: %04x:%08RX64/%s: #UD [%.*Rhxs] -> re-injected\n", pVCpu->idCpu,
pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext),
pExit->VpException.InstructionByteCount, pExit->VpException.InstructionBytes ));
break;
/*
* Workaround the lovely mesa driver assuming that vmsvga means vmware
* hypervisor and tries to log stuff to the host.
*/
case X86_XCPT_GP:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionGp);
/** @todo r=bird: Need workaround in IEM for this, right?
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_XCPT_GP),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC()); */
if ( !pVCpu->nem.s.fTrapXcptGpForLovelyMesaDrv
|| !nemHcWinIsMesaDrvGp(pVCpu, &pVCpu->cpum.GstCtx, pExit->VpException.InstructionBytes,
pExit->VpException.InstructionByteCount))
{
#if 1 /** @todo Need to emulate instruction or we get a triple fault when trying to inject the \#GP... */
rcStrict = IEMExecOneWithPrefetchedByPC(pVCpu, pExit->VpContext.Rip,
pExit->VpException.InstructionBytes,
pExit->VpException.InstructionByteCount);
Log4(("XcptExit/%u: %04x:%08RX64/%s: #GP -> emulated -> %Rrc\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip,
nemR3WinExecStateToLogStr(&pExit->VpContext), VBOXSTRICTRC_VAL(rcStrict) ));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionUdHandled);
return rcStrict;
#else
break;
#endif
}
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionGpMesa);
return nemHcWinHandleMesaDrvGp(pVCpu, &pVCpu->cpum.GstCtx);
/*
* Filter debug exceptions.
*/
case X86_XCPT_DB:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionDb);
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_XCPT_DB),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
Log4(("XcptExit/%u: %04x:%08RX64/%s: #DB - TODO\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext) ));
break;
case X86_XCPT_BP:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitExceptionBp);
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_XCPT_BP),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
Log4(("XcptExit/%u: %04x:%08RX64/%s: #BP - TODO - %u\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.InstructionLength));
enmEvtType = TRPM_SOFTWARE_INT; /* We're at the INT3 instruction, not after it. */
break;
/* This shouldn't happen. */
default:
AssertLogRelMsgFailedReturn(("ExceptionType=%#x\n", pExit->VpException.ExceptionType), VERR_IEM_IPE_6);
}
/*
* Inject it.
*/
rcStrict = IEMInjectTrap(pVCpu, pExit->VpException.ExceptionType, enmEvtType, pExit->VpException.ErrorCode,
pExit->VpException.ExceptionParameter /*??*/, pExit->VpContext.InstructionLength);
Log4(("XcptExit/%u: %04x:%08RX64/%s: %#u -> injected -> %Rrc\n",
pVCpu->idCpu, pExit->VpContext.Cs.Selector, pExit->VpContext.Rip,
nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpException.ExceptionType, VBOXSTRICTRC_VAL(rcStrict) ));
RT_NOREF_PV(pVM);
return rcStrict;
}
/**
* Deals with MSR access exits (WHvRunVpExitReasonUnrecoverableException).
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessageUnrecoverableException
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExitUnrecoverableException(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
#if 0
/*
* Just copy the state we've got and handle it in the loop for now.
*/
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
Log(("TripleExit/%u: %04x:%08RX64/%s: RFL=%#RX64 -> VINF_EM_TRIPLE_FAULT\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.Rflags));
RT_NOREF_PV(pVM);
return VINF_EM_TRIPLE_FAULT;
#else
/*
* Let IEM decide whether this is really it.
*/
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_UNRECOVERABLE_EXCEPTION),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
nemR3WinCopyStateFromX64Header(pVCpu, &pExit->VpContext);
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM | CPUMCTX_EXTRN_ALL, "TripleExit");
if (rcStrict == VINF_SUCCESS)
{
rcStrict = IEMExecOne(pVCpu);
if (rcStrict == VINF_SUCCESS)
{
Log(("UnrecovExit/%u: %04x:%08RX64/%s: RFL=%#RX64 -> VINF_SUCCESS\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.Rflags));
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT; /* Make sure to reset pending #DB(0). */
return VINF_SUCCESS;
}
if (rcStrict == VINF_EM_TRIPLE_FAULT)
Log(("UnrecovExit/%u: %04x:%08RX64/%s: RFL=%#RX64 -> VINF_EM_TRIPLE_FAULT!\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.Rflags, VBOXSTRICTRC_VAL(rcStrict) ));
else
Log(("UnrecovExit/%u: %04x:%08RX64/%s: RFL=%#RX64 -> %Rrc (IEMExecOne)\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.Rflags, VBOXSTRICTRC_VAL(rcStrict) ));
}
else
Log(("UnrecovExit/%u: %04x:%08RX64/%s: RFL=%#RX64 -> %Rrc (state import)\n", pVCpu->idCpu, pExit->VpContext.Cs.Selector,
pExit->VpContext.Rip, nemR3WinExecStateToLogStr(&pExit->VpContext), pExit->VpContext.Rflags, VBOXSTRICTRC_VAL(rcStrict) ));
RT_NOREF_PV(pVM);
return rcStrict;
#endif
}
/**
* Handles VM exits.
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pExit The VM exit information to handle.
* @sa nemHCWinHandleMessage
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemR3WinHandleExit(PVMCC pVM, PVMCPUCC pVCpu, WHV_RUN_VP_EXIT_CONTEXT const *pExit)
{
switch (pExit->ExitReason)
{
case WHvRunVpExitReasonMemoryAccess:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitMemUnmapped);
return nemR3WinHandleExitMemory(pVM, pVCpu, pExit);
case WHvRunVpExitReasonX64IoPortAccess:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitPortIo);
return nemR3WinHandleExitIoPort(pVM, pVCpu, pExit);
case WHvRunVpExitReasonX64Halt:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitHalt);
EMHistoryAddExit(pVCpu, EMEXIT_MAKE_FT(EMEXIT_F_KIND_NEM, NEMEXITTYPE_HALT),
pExit->VpContext.Rip + pExit->VpContext.Cs.Base, ASMReadTSC());
Log4(("HaltExit/%u\n", pVCpu->idCpu));
return VINF_EM_HALT;
case WHvRunVpExitReasonCanceled:
Log4(("CanceledExit/%u\n", pVCpu->idCpu));
return VINF_SUCCESS;
case WHvRunVpExitReasonX64InterruptWindow:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitInterruptWindow);
return nemR3WinHandleExitInterruptWindow(pVM, pVCpu, pExit);
case WHvRunVpExitReasonX64Cpuid:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitCpuId);
return nemR3WinHandleExitCpuId(pVM, pVCpu, pExit);
case WHvRunVpExitReasonX64MsrAccess:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitMsr);
return nemR3WinHandleExitMsr(pVM, pVCpu, pExit);
case WHvRunVpExitReasonException:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitException);
return nemR3WinHandleExitException(pVM, pVCpu, pExit);
case WHvRunVpExitReasonUnrecoverableException:
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatExitUnrecoverable);
return nemR3WinHandleExitUnrecoverableException(pVM, pVCpu, pExit);
case WHvRunVpExitReasonUnsupportedFeature:
case WHvRunVpExitReasonInvalidVpRegisterValue:
LogRel(("Unimplemented exit:\n%.*Rhxd\n", (int)sizeof(*pExit), pExit));
AssertLogRelMsgFailedReturn(("Unexpected exit on CPU #%u: %#x\n%.32Rhxd\n",
pVCpu->idCpu, pExit->ExitReason, pExit), VERR_NEM_IPE_3);
/* Undesired exits: */
case WHvRunVpExitReasonNone:
default:
LogRel(("Unknown exit:\n%.*Rhxd\n", (int)sizeof(*pExit), pExit));
AssertLogRelMsgFailedReturn(("Unknown exit on CPU #%u: %#x!\n", pVCpu->idCpu, pExit->ExitReason), VERR_NEM_IPE_3);
}
}
/**
* Deals with pending interrupt related force flags, may inject interrupt.
*
* @returns VBox strict status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
* @param pfInterruptWindows Where to return interrupt window flags.
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemHCWinHandleInterruptFF(PVMCC pVM, PVMCPUCC pVCpu, uint8_t *pfInterruptWindows)
{
Assert(!TRPMHasTrap(pVCpu));
RT_NOREF_PV(pVM);
/*
* First update APIC. We ASSUME this won't need TPR/CR8.
*/
if (VMCPU_FF_TEST_AND_CLEAR(pVCpu, VMCPU_FF_UPDATE_APIC))
{
APICUpdatePendingInterrupts(pVCpu);
if (!VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC
| VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI))
return VINF_SUCCESS;
}
/*
* We don't currently implement SMIs.
*/
AssertReturn(!VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_SMI), VERR_NEM_IPE_0);
/*
* Check if we've got the minimum of state required for deciding whether we
* can inject interrupts and NMIs. If we don't have it, get all we might require
* for injection via IEM.
*/
bool const fPendingNmi = VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_NMI);
uint64_t fNeedExtrn = CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_RIP | CPUMCTX_EXTRN_RFLAGS
| (fPendingNmi ? CPUMCTX_EXTRN_INHIBIT_NMI : 0);
if (pVCpu->cpum.GstCtx.fExtrn & fNeedExtrn)
{
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM_XCPT, "IntFF");
if (rcStrict != VINF_SUCCESS)
return rcStrict;
}
/*
* NMI? Try deliver it first.
*/
if (fPendingNmi)
{
if ( !CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx)
&& !CPUMAreInterruptsInhibitedByNmi(&pVCpu->cpum.GstCtx))
{
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM_XCPT, "NMI");
if (rcStrict == VINF_SUCCESS)
{
VMCPU_FF_CLEAR(pVCpu, VMCPU_FF_INTERRUPT_NMI);
rcStrict = IEMInjectTrap(pVCpu, X86_XCPT_NMI, TRPM_HARDWARE_INT, 0, 0, 0);
Log8(("Injected NMI on %u (%d)\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
}
return rcStrict;
}
*pfInterruptWindows |= NEM_WIN_INTW_F_NMI;
Log8(("NMI window pending on %u\n", pVCpu->idCpu));
}
/*
* APIC or PIC interrupt?
*/
if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_INTERRUPT_PIC))
{
/** @todo check NMI inhibiting here too! */
if ( !CPUMIsInInterruptShadow(&pVCpu->cpum.GstCtx)
&& pVCpu->cpum.GstCtx.rflags.Bits.u1IF)
{
AssertCompile(NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM_XCPT & CPUMCTX_EXTRN_APIC_TPR);
VBOXSTRICTRC rcStrict = nemHCWinImportStateIfNeededStrict(pVCpu, NEM_WIN_CPUMCTX_EXTRN_MASK_FOR_IEM_XCPT, "NMI");
if (rcStrict == VINF_SUCCESS)
{
uint8_t bInterrupt;
int rc = PDMGetInterrupt(pVCpu, &bInterrupt);
if (RT_SUCCESS(rc))
{
Log8(("Injecting interrupt %#x on %u: %04x:%08RX64 efl=%#x\n", bInterrupt, pVCpu->idCpu, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.eflags.u));
rcStrict = IEMInjectTrap(pVCpu, bInterrupt, TRPM_HARDWARE_INT, 0, 0, 0);
Log8(("Injected interrupt %#x on %u (%d)\n", bInterrupt, pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
}
else if (rc == VERR_APIC_INTR_MASKED_BY_TPR)
{
*pfInterruptWindows |= ((bInterrupt >> 4) << NEM_WIN_INTW_F_PRIO_SHIFT) | NEM_WIN_INTW_F_REGULAR;
Log8(("VERR_APIC_INTR_MASKED_BY_TPR: *pfInterruptWindows=%#x\n", *pfInterruptWindows));
}
else
Log8(("PDMGetInterrupt failed -> %Rrc\n", rc));
}
return rcStrict;
}
if (VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC) && !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC))
{
/* If only an APIC interrupt is pending, we need to know its priority. Otherwise we'll
* likely get pointless deliverability notifications with IF=1 but TPR still too high.
*/
bool fPendingIntr = false;
uint8_t bTpr = 0;
uint8_t bPendingIntr = 0;
int rc = APICGetTpr(pVCpu, &bTpr, &fPendingIntr, &bPendingIntr);
AssertRC(rc);
*pfInterruptWindows |= ((bPendingIntr >> 4) << NEM_WIN_INTW_F_PRIO_SHIFT) | NEM_WIN_INTW_F_REGULAR;
Log8(("Interrupt window pending on %u: %#x (bTpr=%#x fPendingIntr=%d bPendingIntr=%#x)\n",
pVCpu->idCpu, *pfInterruptWindows, bTpr, fPendingIntr, bPendingIntr));
}
else
{
*pfInterruptWindows |= NEM_WIN_INTW_F_REGULAR;
Log8(("Interrupt window pending on %u: %#x\n", pVCpu->idCpu, *pfInterruptWindows));
}
}
return VINF_SUCCESS;
}
/**
* Inner NEM runloop for windows.
*
* @returns Strict VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context per CPU structure.
*/
NEM_TMPL_STATIC VBOXSTRICTRC nemHCWinRunGC(PVMCC pVM, PVMCPUCC pVCpu)
{
LogFlow(("NEM/%u: %04x:%08RX64 efl=%#08RX64 <=\n", pVCpu->idCpu, pVCpu->cpum.GstCtx.cs.Sel, pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u));
#ifdef LOG_ENABLED
if (LogIs3Enabled())
nemHCWinLogState(pVM, pVCpu);
#endif
/*
* Try switch to NEM runloop state.
*/
if (VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED))
{ /* likely */ }
else
{
VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
LogFlow(("NEM/%u: returning immediately because canceled\n", pVCpu->idCpu));
return VINF_SUCCESS;
}
/*
* The run loop.
*
* Current approach to state updating to use the sledgehammer and sync
* everything every time. This will be optimized later.
*/
const bool fSingleStepping = DBGFIsStepping(pVCpu);
// const uint32_t fCheckVmFFs = !fSingleStepping ? VM_FF_HP_R0_PRE_HM_MASK
// : VM_FF_HP_R0_PRE_HM_STEP_MASK;
// const uint32_t fCheckCpuFFs = !fSingleStepping ? VMCPU_FF_HP_R0_PRE_HM_MASK : VMCPU_FF_HP_R0_PRE_HM_STEP_MASK;
VBOXSTRICTRC rcStrict = VINF_SUCCESS;
for (unsigned iLoop = 0;; iLoop++)
{
/*
* Pending interrupts or such? Need to check and deal with this prior
* to the state syncing.
*/
pVCpu->nem.s.fDesiredInterruptWindows = 0;
if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_APIC | VMCPU_FF_UPDATE_APIC | VMCPU_FF_INTERRUPT_PIC
| VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI))
{
/* Try inject interrupt. */
rcStrict = nemHCWinHandleInterruptFF(pVM, pVCpu, &pVCpu->nem.s.fDesiredInterruptWindows);
if (rcStrict == VINF_SUCCESS)
{ /* likely */ }
else
{
LogFlow(("NEM/%u: breaking: nemHCWinHandleInterruptFF -> %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnStatus);
break;
}
}
#ifndef NEM_WIN_WITH_A20
/*
* Do not execute in hyper-V if the A20 isn't enabled.
*/
if (PGMPhysIsA20Enabled(pVCpu))
{ /* likely */ }
else
{
rcStrict = VINF_EM_RESCHEDULE_REM;
LogFlow(("NEM/%u: breaking: A20 disabled\n", pVCpu->idCpu));
break;
}
#endif
/*
* Ensure that hyper-V has the whole state.
* (We always update the interrupt windows settings when active as hyper-V seems
* to forget about it after an exit.)
*/
if ( (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK))
!= (CPUMCTX_EXTRN_ALL | CPUMCTX_EXTRN_NEM_WIN_MASK)
|| ( ( pVCpu->nem.s.fDesiredInterruptWindows
|| pVCpu->nem.s.fCurrentInterruptWindows != pVCpu->nem.s.fDesiredInterruptWindows) ) )
{
int rc2 = nemHCWinCopyStateToHyperV(pVM, pVCpu);
AssertRCReturn(rc2, rc2);
}
/*
* Poll timers and run for a bit.
*
* With the VID approach (ring-0 or ring-3) we can specify a timeout here,
* so we take the time of the next timer event and uses that as a deadline.
* The rounding heuristics are "tuned" so that rhel5 (1K timer) will boot fine.
*/
/** @todo See if we cannot optimize this TMTimerPollGIP by only redoing
* the whole polling job when timers have changed... */
uint64_t offDeltaIgnored;
uint64_t const nsNextTimerEvt = TMTimerPollGIP(pVM, pVCpu, &offDeltaIgnored); NOREF(nsNextTimerEvt);
if ( !VM_FF_IS_ANY_SET(pVM, VM_FF_EMT_RENDEZVOUS | VM_FF_TM_VIRTUAL_SYNC)
&& !VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_HM_TO_R3_MASK))
{
if (VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM_WAIT, VMCPUSTATE_STARTED_EXEC_NEM))
{
#ifdef LOG_ENABLED
if (LogIsFlowEnabled())
{
static const WHV_REGISTER_NAME s_aNames[6] = { WHvX64RegisterCs, WHvX64RegisterRip, WHvX64RegisterRflags,
WHvX64RegisterSs, WHvX64RegisterRsp, WHvX64RegisterCr0 };
WHV_REGISTER_VALUE aRegs[RT_ELEMENTS(s_aNames)] = { {{0, 0} } };
WHvGetVirtualProcessorRegisters(pVM->nem.s.hPartition, pVCpu->idCpu, s_aNames, RT_ELEMENTS(s_aNames), aRegs);
LogFlow(("NEM/%u: Entry @ %04x:%08RX64 IF=%d EFL=%#RX64 SS:RSP=%04x:%08RX64 cr0=%RX64\n",
pVCpu->idCpu, aRegs[0].Segment.Selector, aRegs[1].Reg64, RT_BOOL(aRegs[2].Reg64 & X86_EFL_IF),
aRegs[2].Reg64, aRegs[3].Segment.Selector, aRegs[4].Reg64, aRegs[5].Reg64));
}
#endif
WHV_RUN_VP_EXIT_CONTEXT ExitReason = {0};
TMNotifyStartOfExecution(pVM, pVCpu);
HRESULT hrc = WHvRunVirtualProcessor(pVM->nem.s.hPartition, pVCpu->idCpu, &ExitReason, sizeof(ExitReason));
VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED_EXEC_NEM, VMCPUSTATE_STARTED_EXEC_NEM_WAIT);
TMNotifyEndOfExecution(pVM, pVCpu, ASMReadTSC());
#ifdef LOG_ENABLED
LogFlow(("NEM/%u: Exit @ %04X:%08RX64 IF=%d CR8=%#x Reason=%#x\n", pVCpu->idCpu, ExitReason.VpContext.Cs.Selector,
ExitReason.VpContext.Rip, RT_BOOL(ExitReason.VpContext.Rflags & X86_EFL_IF), ExitReason.VpContext.Cr8,
ExitReason.ExitReason));
#endif
if (SUCCEEDED(hrc))
{
/*
* Deal with the message.
*/
rcStrict = nemR3WinHandleExit(pVM, pVCpu, &ExitReason);
if (rcStrict == VINF_SUCCESS)
{ /* hopefully likely */ }
else
{
LogFlow(("NEM/%u: breaking: nemHCWinHandleMessage -> %Rrc\n", pVCpu->idCpu, VBOXSTRICTRC_VAL(rcStrict) ));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnStatus);
break;
}
}
else
AssertLogRelMsgFailedReturn(("WHvRunVirtualProcessor failed for CPU #%u: %#x (%u)\n",
pVCpu->idCpu, hrc, GetLastError()),
VERR_NEM_IPE_0);
/*
* If no relevant FFs are pending, loop.
*/
if ( !VM_FF_IS_ANY_SET( pVM, !fSingleStepping ? VM_FF_HP_R0_PRE_HM_MASK : VM_FF_HP_R0_PRE_HM_STEP_MASK)
&& !VMCPU_FF_IS_ANY_SET(pVCpu, !fSingleStepping ? VMCPU_FF_HP_R0_PRE_HM_MASK : VMCPU_FF_HP_R0_PRE_HM_STEP_MASK) )
continue;
/** @todo Try handle pending flags, not just return to EM loops. Take care
* not to set important RCs here unless we've handled a message. */
LogFlow(("NEM/%u: breaking: pending FF (%#x / %#RX64)\n",
pVCpu->idCpu, pVM->fGlobalForcedActions, (uint64_t)pVCpu->fLocalForcedActions));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnFFPost);
}
else
{
LogFlow(("NEM/%u: breaking: canceled %d (pre exec)\n", pVCpu->idCpu, VMCPU_GET_STATE(pVCpu) ));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnCancel);
}
}
else
{
LogFlow(("NEM/%u: breaking: pending FF (pre exec)\n", pVCpu->idCpu));
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatBreakOnFFPre);
}
break;
} /* the run loop */
/*
* If the CPU is running, make sure to stop it before we try sync back the
* state and return to EM. We don't sync back the whole state if we can help it.
*/
if (!VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM))
VMCPU_CMPXCHG_STATE(pVCpu, VMCPUSTATE_STARTED, VMCPUSTATE_STARTED_EXEC_NEM_CANCELED);
if (pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL | (CPUMCTX_EXTRN_NEM_WIN_MASK & ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT)))
{
/* Try anticipate what we might need. */
uint64_t fImport = IEM_CPUMCTX_EXTRN_MUST_MASK | CPUMCTX_EXTRN_INHIBIT_INT | CPUMCTX_EXTRN_INHIBIT_NMI;
if ( (rcStrict >= VINF_EM_FIRST && rcStrict <= VINF_EM_LAST)
|| RT_FAILURE(rcStrict))
fImport = CPUMCTX_EXTRN_ALL | (CPUMCTX_EXTRN_NEM_WIN_MASK & ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT);
else if (VMCPU_FF_IS_ANY_SET(pVCpu, VMCPU_FF_INTERRUPT_PIC | VMCPU_FF_INTERRUPT_APIC
| VMCPU_FF_INTERRUPT_NMI | VMCPU_FF_INTERRUPT_SMI))
fImport |= IEM_CPUMCTX_EXTRN_XCPT_MASK;
if (pVCpu->cpum.GstCtx.fExtrn & fImport)
{
int rc2 = nemHCWinCopyStateFromHyperV(pVM, pVCpu, fImport | CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT);
if (RT_SUCCESS(rc2))
pVCpu->cpum.GstCtx.fExtrn &= ~fImport;
else if (RT_SUCCESS(rcStrict))
rcStrict = rc2;
if (!(pVCpu->cpum.GstCtx.fExtrn & (CPUMCTX_EXTRN_ALL | (CPUMCTX_EXTRN_NEM_WIN_MASK & ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT))))
pVCpu->cpum.GstCtx.fExtrn = 0;
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturn);
}
else
{
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
pVCpu->cpum.GstCtx.fExtrn &= ~CPUMCTX_EXTRN_NEM_WIN_EVENT_INJECT;
}
}
else
{
STAM_REL_COUNTER_INC(&pVCpu->nem.s.StatImportOnReturnSkipped);
pVCpu->cpum.GstCtx.fExtrn = 0;
}
LogFlow(("NEM/%u: %04x:%08RX64 efl=%#08RX64 => %Rrc\n", pVCpu->idCpu, pVCpu->cpum.GstCtx.cs.Sel,
pVCpu->cpum.GstCtx.rip, pVCpu->cpum.GstCtx.rflags.u, VBOXSTRICTRC_VAL(rcStrict) ));
return rcStrict;
}
/**
* @callback_method_impl{FNPGMPHYSNEMCHECKPAGE}
*/
NEM_TMPL_STATIC DECLCALLBACK(int) nemHCWinUnsetForA20CheckerCallback(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhys,
PPGMPHYSNEMPAGEINFO pInfo, void *pvUser)
{
/* We'll just unmap the memory. */
if (pInfo->u2NemState > NEM_WIN_PAGE_STATE_UNMAPPED)
{
HRESULT hrc = WHvUnmapGpaRange(pVM->nem.s.hPartition, GCPhys, X86_PAGE_SIZE);
if (SUCCEEDED(hrc))
{
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPage);
uint32_t cMappedPages = ASMAtomicDecU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
Log5(("NEM GPA unmapped/A20: %RGp (was %s, cMappedPages=%u)\n", GCPhys, g_apszPageStates[pInfo->u2NemState], cMappedPages));
pInfo->u2NemState = NEM_WIN_PAGE_STATE_UNMAPPED;
}
else
{
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPageFailed);
LogRel(("nemHCWinUnsetForA20CheckerCallback/unmap: GCPhys=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
GCPhys, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_NEM_IPE_2;
}
}
RT_NOREF(pVCpu, pvUser);
return VINF_SUCCESS;
}
/**
* Unmaps a page from Hyper-V for the purpose of emulating A20 gate behavior.
*
* @returns The PGMPhysNemQueryPageInfo result.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param GCPhys The page to unmap.
*/
NEM_TMPL_STATIC int nemHCWinUnmapPageForA20Gate(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhys)
{
PGMPHYSNEMPAGEINFO Info;
return PGMPhysNemPageInfoChecker(pVM, pVCpu, GCPhys, false /*fMakeWritable*/, &Info,
nemHCWinUnsetForA20CheckerCallback, NULL);
}
void nemHCNativeNotifyHandlerPhysicalRegister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb)
{
Log5(("nemHCNativeNotifyHandlerPhysicalRegister: %RGp LB %RGp enmKind=%d\n", GCPhys, cb, enmKind));
NOREF(pVM); NOREF(enmKind); NOREF(GCPhys); NOREF(cb);
}
VMM_INT_DECL(void) NEMHCNotifyHandlerPhysicalDeregister(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhys, RTGCPHYS cb,
RTR3PTR pvMemR3, uint8_t *pu2State)
{
Log5(("NEMHCNotifyHandlerPhysicalDeregister: %RGp LB %RGp enmKind=%d pvMemR3=%p pu2State=%p (%d)\n",
GCPhys, cb, enmKind, pvMemR3, pu2State, *pu2State));
*pu2State = UINT8_MAX;
if (pvMemR3)
{
STAM_REL_PROFILE_START(&pVM->nem.s.StatProfMapGpaRange, a);
HRESULT hrc = WHvMapGpaRange(pVM->nem.s.hPartition, pvMemR3, GCPhys, cb,
WHvMapGpaRangeFlagRead | WHvMapGpaRangeFlagExecute | WHvMapGpaRangeFlagWrite);
STAM_REL_PROFILE_STOP(&pVM->nem.s.StatProfMapGpaRange, a);
if (SUCCEEDED(hrc))
*pu2State = NEM_WIN_PAGE_STATE_WRITABLE;
else
AssertLogRelMsgFailed(("NEMHCNotifyHandlerPhysicalDeregister: WHvMapGpaRange(,%p,%RGp,%RGp,) -> %Rhrc\n",
pvMemR3, GCPhys, cb, hrc));
}
RT_NOREF(enmKind);
}
void nemHCNativeNotifyHandlerPhysicalModify(PVMCC pVM, PGMPHYSHANDLERKIND enmKind, RTGCPHYS GCPhysOld,
RTGCPHYS GCPhysNew, RTGCPHYS cb, bool fRestoreAsRAM)
{
Log5(("nemHCNativeNotifyHandlerPhysicalModify: %RGp LB %RGp -> %RGp enmKind=%d fRestoreAsRAM=%d\n",
GCPhysOld, cb, GCPhysNew, enmKind, fRestoreAsRAM));
NOREF(pVM); NOREF(enmKind); NOREF(GCPhysOld); NOREF(GCPhysNew); NOREF(cb); NOREF(fRestoreAsRAM);
}
/**
* Worker that maps pages into Hyper-V.
*
* This is used by the PGM physical page notifications as well as the memory
* access VMEXIT handlers.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure of the
* calling EMT.
* @param GCPhysSrc The source page address.
* @param GCPhysDst The hyper-V destination page. This may differ from
* GCPhysSrc when A20 is disabled.
* @param fPageProt NEM_PAGE_PROT_XXX.
* @param pu2State Our page state (input/output).
* @param fBackingChanged Set if the page backing is being changed.
* @thread EMT(pVCpu)
*/
NEM_TMPL_STATIC int nemHCNativeSetPhysPage(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhysSrc, RTGCPHYS GCPhysDst,
uint32_t fPageProt, uint8_t *pu2State, bool fBackingChanged)
{
/*
* Looks like we need to unmap a page before we can change the backing
* or even modify the protection. This is going to be *REALLY* efficient.
* PGM lends us two bits to keep track of the state here.
*/
RT_NOREF(pVCpu);
uint8_t const u2OldState = *pu2State;
uint8_t const u2NewState = fPageProt & NEM_PAGE_PROT_WRITE ? NEM_WIN_PAGE_STATE_WRITABLE
: fPageProt & NEM_PAGE_PROT_READ ? NEM_WIN_PAGE_STATE_READABLE : NEM_WIN_PAGE_STATE_UNMAPPED;
if ( fBackingChanged
|| u2NewState != u2OldState)
{
if (u2OldState > NEM_WIN_PAGE_STATE_UNMAPPED)
{
STAM_REL_PROFILE_START(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
HRESULT hrc = WHvUnmapGpaRange(pVM->nem.s.hPartition, GCPhysDst, X86_PAGE_SIZE);
STAM_REL_PROFILE_STOP(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
if (SUCCEEDED(hrc))
{
*pu2State = NEM_WIN_PAGE_STATE_UNMAPPED;
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPage);
uint32_t cMappedPages = ASMAtomicDecU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
if (u2NewState == NEM_WIN_PAGE_STATE_UNMAPPED)
{
Log5(("NEM GPA unmapped/set: %RGp (was %s, cMappedPages=%u)\n",
GCPhysDst, g_apszPageStates[u2OldState], cMappedPages));
return VINF_SUCCESS;
}
}
else
{
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPageFailed);
LogRel(("nemHCNativeSetPhysPage/unmap: GCPhysDst=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
GCPhysDst, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_NEM_INIT_FAILED;
}
}
}
/*
* Writeable mapping?
*/
if (fPageProt & NEM_PAGE_PROT_WRITE)
{
void *pvPage;
int rc = nemR3NativeGCPhys2R3PtrWriteable(pVM, GCPhysSrc, &pvPage);
if (RT_SUCCESS(rc))
{
HRESULT hrc = WHvMapGpaRange(pVM->nem.s.hPartition, pvPage, GCPhysDst, X86_PAGE_SIZE,
WHvMapGpaRangeFlagRead | WHvMapGpaRangeFlagExecute | WHvMapGpaRangeFlagWrite);
if (SUCCEEDED(hrc))
{
*pu2State = NEM_WIN_PAGE_STATE_WRITABLE;
STAM_REL_COUNTER_INC(&pVM->nem.s.StatMapPage);
uint32_t cMappedPages = ASMAtomicIncU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
Log5(("NEM GPA mapped/set: %RGp %s (was %s, cMappedPages=%u)\n",
GCPhysDst, g_apszPageStates[u2NewState], g_apszPageStates[u2OldState], cMappedPages));
return VINF_SUCCESS;
}
STAM_REL_COUNTER_INC(&pVM->nem.s.StatMapPageFailed);
LogRel(("nemHCNativeSetPhysPage/writable: GCPhysDst=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
GCPhysDst, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_NEM_INIT_FAILED;
}
LogRel(("nemHCNativeSetPhysPage/writable: GCPhysSrc=%RGp rc=%Rrc\n", GCPhysSrc, rc));
return rc;
}
if (fPageProt & NEM_PAGE_PROT_READ)
{
const void *pvPage;
int rc = nemR3NativeGCPhys2R3PtrReadOnly(pVM, GCPhysSrc, &pvPage);
if (RT_SUCCESS(rc))
{
STAM_REL_PROFILE_START(&pVM->nem.s.StatProfMapGpaRangePage, a);
HRESULT hrc = WHvMapGpaRange(pVM->nem.s.hPartition, (void *)pvPage, GCPhysDst, X86_PAGE_SIZE,
WHvMapGpaRangeFlagRead | WHvMapGpaRangeFlagExecute);
STAM_REL_PROFILE_STOP(&pVM->nem.s.StatProfMapGpaRangePage, a);
if (SUCCEEDED(hrc))
{
*pu2State = NEM_WIN_PAGE_STATE_READABLE;
STAM_REL_COUNTER_INC(&pVM->nem.s.StatMapPage);
uint32_t cMappedPages = ASMAtomicIncU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
Log5(("NEM GPA mapped/set: %RGp %s (was %s, cMappedPages=%u)\n",
GCPhysDst, g_apszPageStates[u2NewState], g_apszPageStates[u2OldState], cMappedPages));
return VINF_SUCCESS;
}
STAM_REL_COUNTER_INC(&pVM->nem.s.StatMapPageFailed);
LogRel(("nemHCNativeSetPhysPage/readonly: GCPhysDst=%RGp hrc=%Rhrc (%#x) Last=%#x/%u\n",
GCPhysDst, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_NEM_INIT_FAILED;
}
LogRel(("nemHCNativeSetPhysPage/readonly: GCPhysSrc=%RGp rc=%Rrc\n", GCPhysSrc, rc));
return rc;
}
/* We already unmapped it above. */
*pu2State = NEM_WIN_PAGE_STATE_UNMAPPED;
return VINF_SUCCESS;
}
NEM_TMPL_STATIC int nemHCJustUnmapPageFromHyperV(PVMCC pVM, RTGCPHYS GCPhysDst, uint8_t *pu2State)
{
if (*pu2State <= NEM_WIN_PAGE_STATE_UNMAPPED)
{
Log5(("nemHCJustUnmapPageFromHyperV: %RGp == unmapped\n", GCPhysDst));
*pu2State = NEM_WIN_PAGE_STATE_UNMAPPED;
return VINF_SUCCESS;
}
STAM_REL_PROFILE_START(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
HRESULT hrc = WHvUnmapGpaRange(pVM->nem.s.hPartition, GCPhysDst & ~(RTGCPHYS)X86_PAGE_OFFSET_MASK, X86_PAGE_SIZE);
STAM_REL_PROFILE_STOP(&pVM->nem.s.StatProfUnmapGpaRangePage, a);
if (SUCCEEDED(hrc))
{
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPage);
uint32_t cMappedPages = ASMAtomicDecU32(&pVM->nem.s.cMappedPages); NOREF(cMappedPages);
*pu2State = NEM_WIN_PAGE_STATE_UNMAPPED;
Log5(("nemHCJustUnmapPageFromHyperV: %RGp => unmapped (total %u)\n", GCPhysDst, cMappedPages));
return VINF_SUCCESS;
}
STAM_REL_COUNTER_INC(&pVM->nem.s.StatUnmapPageFailed);
LogRel(("nemHCJustUnmapPageFromHyperV(%RGp): failed! hrc=%Rhrc (%#x) Last=%#x/%u\n",
GCPhysDst, hrc, hrc, RTNtLastStatusValue(), RTNtLastErrorValue()));
return VERR_NEM_IPE_6;
}
int nemHCNativeNotifyPhysPageAllocated(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, uint32_t fPageProt,
PGMPAGETYPE enmType, uint8_t *pu2State)
{
Log5(("nemHCNativeNotifyPhysPageAllocated: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
GCPhys, HCPhys, fPageProt, enmType, *pu2State));
RT_NOREF_PV(HCPhys); RT_NOREF_PV(enmType);
int rc;
RT_NOREF_PV(fPageProt);
#ifdef NEM_WIN_WITH_A20
if ( pVM->nem.s.fA20Enabled
|| !NEM_WIN_IS_RELEVANT_TO_A20(GCPhys))
#endif
rc = nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
#ifdef NEM_WIN_WITH_A20
else if (!NEM_WIN_IS_SUBJECT_TO_A20(GCPhys))
rc = nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
else
rc = VINF_SUCCESS; /* ignore since we've got the alias page at this address. */
#endif
return rc;
}
VMM_INT_DECL(void) NEMHCNotifyPhysPageProtChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhys, RTR3PTR pvR3, uint32_t fPageProt,
PGMPAGETYPE enmType, uint8_t *pu2State)
{
Log5(("NEMHCNotifyPhysPageProtChanged: %RGp HCPhys=%RHp fPageProt=%#x enmType=%d *pu2State=%d\n",
GCPhys, HCPhys, fPageProt, enmType, *pu2State));
Assert(VM_IS_NEM_ENABLED(pVM));
RT_NOREF(HCPhys, enmType, pvR3);
RT_NOREF_PV(fPageProt);
#ifdef NEM_WIN_WITH_A20
if ( pVM->nem.s.fA20Enabled
|| !NEM_WIN_IS_RELEVANT_TO_A20(GCPhys))
#endif
nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
#ifdef NEM_WIN_WITH_A20
else if (!NEM_WIN_IS_SUBJECT_TO_A20(GCPhys))
nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
/* else: ignore since we've got the alias page at this address. */
#endif
}
VMM_INT_DECL(void) NEMHCNotifyPhysPageChanged(PVMCC pVM, RTGCPHYS GCPhys, RTHCPHYS HCPhysPrev, RTHCPHYS HCPhysNew,
RTR3PTR pvNewR3, uint32_t fPageProt, PGMPAGETYPE enmType, uint8_t *pu2State)
{
Log5(("nemHCNativeNotifyPhysPageChanged: %RGp HCPhys=%RHp->%RHp pvNewR3=%p fPageProt=%#x enmType=%d *pu2State=%d\n",
GCPhys, HCPhysPrev, HCPhysNew, pvNewR3, fPageProt, enmType, *pu2State));
Assert(VM_IS_NEM_ENABLED(pVM));
RT_NOREF(HCPhysPrev, HCPhysNew, pvNewR3, enmType);
RT_NOREF_PV(fPageProt);
#ifdef NEM_WIN_WITH_A20
if ( pVM->nem.s.fA20Enabled
|| !NEM_WIN_IS_RELEVANT_TO_A20(GCPhys))
#endif
nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
#ifdef NEM_WIN_WITH_A20
else if (!NEM_WIN_IS_SUBJECT_TO_A20(GCPhys))
nemHCJustUnmapPageFromHyperV(pVM, GCPhys, pu2State);
/* else: ignore since we've got the alias page at this address. */
#endif
}
/**
* Returns features supported by the NEM backend.
*
* @returns Flags of features supported by the native NEM backend.
* @param pVM The cross context VM structure.
*/
VMM_INT_DECL(uint32_t) NEMHCGetFeatures(PVMCC pVM)
{
RT_NOREF(pVM);
/** @todo Make use of the WHvGetVirtualProcessorXsaveState/WHvSetVirtualProcessorXsaveState
* interface added in 2019 to enable passthrough of xsave/xrstor (and depending) features to the guest. */
/** @todo Is NEM_FEAT_F_FULL_GST_EXEC always true? */
return NEM_FEAT_F_NESTED_PAGING | NEM_FEAT_F_FULL_GST_EXEC;
}
|