1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
|
/* $Id: PGMAllPool.cpp $ */
/** @file
* PGM Shadow Page Pool.
*/
/*
* Copyright (C) 2006-2020 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* you can redistribute it and/or modify it under the terms of the GNU
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_PGM_POOL
#include <VBox/vmm/pgm.h>
#include <VBox/vmm/mm.h>
#include <VBox/vmm/em.h>
#include <VBox/vmm/cpum.h>
#include "PGMInternal.h"
#include <VBox/vmm/vmcc.h>
#include "PGMInline.h"
#include <VBox/disopcode.h>
#include <VBox/vmm/hm_vmx.h>
#include <VBox/log.h>
#include <VBox/err.h>
#include <iprt/asm.h>
#include <iprt/asm-amd64-x86.h>
#include <iprt/string.h>
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
RT_C_DECLS_BEGIN
#if 0 /* unused */
DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind);
DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind);
#endif /* unused */
static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable);
static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage);
#if defined(LOG_ENABLED) || defined(VBOX_STRICT)
static const char *pgmPoolPoolKindToStr(uint8_t enmKind);
#endif
#if 0 /*defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT)*/
static void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PTPAE pGstPT);
#endif
int pgmPoolTrackFlushGCPhysPTsSlow(PVMCC pVM, PPGMPAGE pPhysPage);
PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt);
void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt);
void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt);
RT_C_DECLS_END
#if 0 /* unused */
/**
* Checks if the specified page pool kind is for a 4MB or 2MB guest page.
*
* @returns true if it's the shadow of a 4MB or 2MB guest page, otherwise false.
* @param enmKind The page kind.
*/
DECLINLINE(bool) pgmPoolIsBigPage(PGMPOOLKIND enmKind)
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
return true;
default:
return false;
}
}
#endif /* unused */
/**
* Flushes a chain of pages sharing the same access monitor.
*
* @param pPool The pool.
* @param pPage A page in the chain.
*/
void pgmPoolMonitorChainFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
LogFlow(("pgmPoolMonitorChainFlush: Flush page %RGp type=%d\n", pPage->GCPhys, pPage->enmKind));
/*
* Find the list head.
*/
uint16_t idx = pPage->idx;
if (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
{
while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
{
idx = pPage->iMonitoredPrev;
Assert(idx != pPage->idx);
pPage = &pPool->aPages[idx];
}
}
/*
* Iterate the list flushing each shadow page.
*/
for (;;)
{
idx = pPage->iMonitoredNext;
Assert(idx != pPage->idx);
if (pPage->idx >= PGMPOOL_IDX_FIRST)
{
int rc2 = pgmPoolFlushPage(pPool, pPage);
AssertRC(rc2);
}
/* next */
if (idx == NIL_PGMPOOL_IDX)
break;
pPage = &pPool->aPages[idx];
}
}
/**
* Wrapper for getting the current context pointer to the entry being modified.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The cross context VM structure.
* @param pvDst Destination address
* @param pvSrc Pointer to the mapping of @a GCPhysSrc or NULL depending
* on the context (e.g. \#PF in R0 & RC).
* @param GCPhysSrc The source guest physical address.
* @param cb Size of data to read
*/
DECLINLINE(int) pgmPoolPhysSimpleReadGCPhys(PVMCC pVM, void *pvDst, void const *pvSrc, RTGCPHYS GCPhysSrc, size_t cb)
{
#if defined(IN_RING3)
NOREF(pVM); NOREF(GCPhysSrc);
memcpy(pvDst, (RTHCPTR)((uintptr_t)pvSrc & ~(RTHCUINTPTR)(cb - 1)), cb);
return VINF_SUCCESS;
#else
/** @todo in RC we could attempt to use the virtual address, although this can cause many faults (PAE Windows XP guest). */
NOREF(pvSrc);
return PGMPhysSimpleReadGCPhys(pVM, pvDst, GCPhysSrc & ~(RTGCPHYS)(cb - 1), cb);
#endif
}
/**
* Process shadow entries before they are changed by the guest.
*
* For PT entries we will clear them. For PD entries, we'll simply check
* for mapping conflicts and set the SyncCR3 FF if found.
*
* @param pVCpu The cross context virtual CPU structure.
* @param pPool The pool.
* @param pPage The head page.
* @param GCPhysFault The guest physical fault address.
* @param pvAddress Pointer to the mapping of @a GCPhysFault or NULL
* depending on the context (e.g. \#PF in R0 & RC).
* @param cbWrite Write size; might be zero if the caller knows we're not crossing entry boundaries
*/
static void pgmPoolMonitorChainChanging(PVMCPU pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhysFault,
void const *pvAddress, unsigned cbWrite)
{
AssertMsg(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX, ("%u (idx=%u)\n", pPage->iMonitoredPrev, pPage->idx));
const unsigned off = GCPhysFault & PAGE_OFFSET_MASK;
PVMCC pVM = pPool->CTX_SUFF(pVM);
NOREF(pVCpu);
LogFlow(("pgmPoolMonitorChainChanging: %RGv phys=%RGp cbWrite=%d\n",
(RTGCPTR)(CTXTYPE(RTGCPTR, uintptr_t, RTGCPTR))(uintptr_t)pvAddress, GCPhysFault, cbWrite));
for (;;)
{
union
{
void *pv;
PX86PT pPT;
PPGMSHWPTPAE pPTPae;
PX86PD pPD;
PX86PDPAE pPDPae;
PX86PDPT pPDPT;
PX86PML4 pPML4;
} uShw;
LogFlow(("pgmPoolMonitorChainChanging: page idx=%d phys=%RGp (next=%d) kind=%s write=%#x\n",
pPage->idx, pPage->GCPhys, pPage->iMonitoredNext, pgmPoolPoolKindToStr(pPage->enmKind), cbWrite));
uShw.pv = NULL;
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PTE);
LogFlow(("PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT iShw=%x\n", iShw));
if (uShw.pPT->a[iShw].n.u1Present)
{
X86PTE GstPte;
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
AssertRC(rc);
Log4(("pgmPoolMonitorChainChanging 32_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage,
uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK,
GstPte.u & X86_PTE_PG_MASK,
iShw);
ASMAtomicWriteU32(&uShw.pPT->a[iShw].u, 0);
}
break;
}
/* page/2 sized */
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
if (!((off ^ pPage->GCPhys) & (PAGE_SIZE / 2)))
{
const unsigned iShw = (off / sizeof(X86PTE)) & (X86_PG_PAE_ENTRIES - 1);
LogFlow(("PGMPOOLKIND_PAE_PT_FOR_32BIT_PT iShw=%x\n", iShw));
if (PGMSHWPTEPAE_IS_P(uShw.pPTPae->a[iShw]))
{
X86PTE GstPte;
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
AssertRC(rc);
Log4(("pgmPoolMonitorChainChanging pae_32: deref %016RX64 GCPhys %08RX32\n", uShw.pPT->a[iShw].u & X86_PTE_PAE_PG_MASK, GstPte.u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage,
PGMSHWPTEPAE_GET_HCPHYS(uShw.pPTPae->a[iShw]),
GstPte.u & X86_PTE_PG_MASK,
iShw);
PGMSHWPTEPAE_ATOMIC_SET(uShw.pPTPae->a[iShw], 0);
}
}
break;
}
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
{
unsigned iGst = off / sizeof(X86PDE);
unsigned iShwPdpt = iGst / 256;
unsigned iShw = (iGst % 256) * 2;
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
LogFlow(("pgmPoolMonitorChainChanging PAE for 32 bits: iGst=%x iShw=%x idx = %d page idx=%d\n", iGst, iShw, iShwPdpt, pPage->enmKind - PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD));
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
if (iShwPdpt == pPage->enmKind - (unsigned)PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD)
{
for (unsigned i = 0; i < 2; i++)
{
if (uShw.pPDPae->a[iShw+i].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw+i, uShw.pPDPae->a[iShw+i].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw+i].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw + i);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw+i].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 3)
&& (off & 3) + cbWrite > 4)
{
const unsigned iShw2 = iShw + 2 + i;
if (iShw2 < RT_ELEMENTS(uShw.pPDPae->a))
{
if (uShw.pPDPae->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw2);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw2].u, 0);
}
}
}
}
}
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
{
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PTEPAE);
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPT));
if (PGMSHWPTEPAE_IS_P(uShw.pPTPae->a[iShw]))
{
X86PTEPAE GstPte;
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvAddress, GCPhysFault, sizeof(GstPte));
AssertRC(rc);
Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", PGMSHWPTEPAE_GET_HCPHYS(uShw.pPTPae->a[iShw]), GstPte.u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage,
PGMSHWPTEPAE_GET_HCPHYS(uShw.pPTPae->a[iShw]),
GstPte.u & X86_PTE_PAE_PG_MASK,
iShw);
PGMSHWPTEPAE_ATOMIC_SET(uShw.pPTPae->a[iShw], 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 7)
&& (off & 7) + cbWrite > sizeof(X86PTEPAE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTEPAE);
AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPTPae->a));
if (PGMSHWPTEPAE_IS_P(uShw.pPTPae->a[iShw2]))
{
X86PTEPAE GstPte;
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte,
pvAddress ? (uint8_t const *)pvAddress + sizeof(GstPte) : NULL,
GCPhysFault + sizeof(GstPte), sizeof(GstPte));
AssertRC(rc);
Log4(("pgmPoolMonitorChainChanging pae: deref %016RX64 GCPhys %016RX64\n", PGMSHWPTEPAE_GET_HCPHYS(uShw.pPTPae->a[iShw2]), GstPte.u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage,
PGMSHWPTEPAE_GET_HCPHYS(uShw.pPTPae->a[iShw2]),
GstPte.u & X86_PTE_PAE_PG_MASK,
iShw2);
PGMSHWPTEPAE_ATOMIC_SET(uShw.pPTPae->a[iShw2], 0);
}
}
break;
}
case PGMPOOLKIND_32BIT_PD:
{
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PTE); // ASSUMING 32-bit guest paging!
LogFlow(("pgmPoolMonitorChainChanging: PGMPOOLKIND_32BIT_PD %x\n", iShw));
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
if (uShw.pPD->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
pgmPoolFree(pVM,
uShw.pPD->a[iShw].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw);
ASMAtomicWriteU32(&uShw.pPD->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 3)
&& (off & 3) + cbWrite > sizeof(X86PTE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PTE);
if ( iShw2 != iShw
&& iShw2 < RT_ELEMENTS(uShw.pPD->a))
{
if (uShw.pPD->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: 32 bit pd iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPD->a[iShw2].u));
pgmPoolFree(pVM,
uShw.pPD->a[iShw2].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw2);
ASMAtomicWriteU32(&uShw.pPD->a[iShw2].u, 0);
}
}
}
#if 0 /* useful when running PGMAssertCR3(), a bit too troublesome for general use (TLBs). - not working any longer... */
if ( uShw.pPD->a[iShw].n.u1Present
&& !VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3))
{
LogFlow(("pgmPoolMonitorChainChanging: iShw=%#x: %RX32 -> freeing it!\n", iShw, uShw.pPD->a[iShw].u));
pgmPoolFree(pVM, uShw.pPD->a[iShw].u & X86_PDE_PG_MASK, pPage->idx, iShw);
ASMAtomicWriteU32(&uShw.pPD->a[iShw].u, 0);
}
#endif
break;
}
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
{
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PDEPAE);
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
/*
* Causes trouble when the guest uses a PDE to refer to the whole page table level
* structure. (Invalidate here; faults later on when it tries to change the page
* table entries -> recheck; probably only applies to the RC case.)
*/
if (uShw.pPDPae->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 7)
&& (off & 7) + cbWrite > sizeof(X86PDEPAE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
if (uShw.pPDPae->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw2);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw2].u, 0);
}
}
break;
}
case PGMPOOLKIND_PAE_PDPT:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
/*
* Hopefully this doesn't happen very often:
* - touching unused parts of the page
* - messing with the bits of pd pointers without changing the physical address
*/
/* PDPT roots are not page aligned; 32 byte only! */
const unsigned offPdpt = GCPhysFault - pPage->GCPhys;
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = offPdpt / sizeof(X86PDPE);
if (iShw < X86_PG_PAE_PDPE_ENTRIES) /* don't use RT_ELEMENTS(uShw.pPDPT->a), because that's for long mode only */
{
if (uShw.pPDPT->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
pgmPoolFree(pVM,
uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK,
pPage->idx,
iShw);
ASMAtomicWriteU64(&uShw.pPDPT->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (offPdpt & 7)
&& (offPdpt & 7) + cbWrite > sizeof(X86PDPE))
{
const unsigned iShw2 = (offPdpt + cbWrite - 1) / sizeof(X86PDPE);
if ( iShw2 != iShw
&& iShw2 < X86_PG_PAE_PDPE_ENTRIES)
{
if (uShw.pPDPT->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
pgmPoolFree(pVM,
uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK,
pPage->idx,
iShw2);
ASMAtomicWriteU64(&uShw.pPDPT->a[iShw2].u, 0);
}
}
}
}
break;
}
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPD));
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PDEPAE);
Assert(!(uShw.pPDPae->a[iShw].u & PGM_PDFLAGS_MAPPING));
if (uShw.pPDPae->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPae->a[iShw].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 7)
&& (off & 7) + cbWrite > sizeof(X86PDEPAE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDEPAE);
AssertBreak(iShw2 < RT_ELEMENTS(uShw.pPDPae->a));
Assert(!(uShw.pPDPae->a[iShw2].u & PGM_PDFLAGS_MAPPING));
if (uShw.pPDPae->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pae pd iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPae->a[iShw2].u));
pgmPoolFree(pVM,
uShw.pPDPae->a[iShw2].u & X86_PDE_PAE_PG_MASK,
pPage->idx,
iShw2);
ASMAtomicWriteU64(&uShw.pPDPae->a[iShw2].u, 0);
}
}
break;
}
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPDPT));
/*
* Hopefully this doesn't happen very often:
* - messing with the bits of pd pointers without changing the physical address
*/
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PDPE);
if (uShw.pPDPT->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPDPT->a[iShw].u));
pgmPoolFree(pVM, uShw.pPDPT->a[iShw].u & X86_PDPE_PG_MASK, pPage->idx, iShw);
ASMAtomicWriteU64(&uShw.pPDPT->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 7)
&& (off & 7) + cbWrite > sizeof(X86PDPE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PDPE);
if (uShw.pPDPT->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pdpt iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPDPT->a[iShw2].u));
pgmPoolFree(pVM, uShw.pPDPT->a[iShw2].u & X86_PDPE_PG_MASK, pPage->idx, iShw2);
ASMAtomicWriteU64(&uShw.pPDPT->a[iShw2].u, 0);
}
}
break;
}
case PGMPOOLKIND_64BIT_PML4:
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitor,FaultPML4));
/*
* Hopefully this doesn't happen very often:
* - messing with the bits of pd pointers without changing the physical address
*/
uShw.pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
const unsigned iShw = off / sizeof(X86PDPE);
if (uShw.pPML4->a[iShw].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw=%#x: %RX64 -> freeing it!\n", iShw, uShw.pPML4->a[iShw].u));
pgmPoolFree(pVM, uShw.pPML4->a[iShw].u & X86_PML4E_PG_MASK, pPage->idx, iShw);
ASMAtomicWriteU64(&uShw.pPML4->a[iShw].u, 0);
}
/* paranoia / a bit assumptive. */
if ( (off & 7)
&& (off & 7) + cbWrite > sizeof(X86PDPE))
{
const unsigned iShw2 = (off + cbWrite - 1) / sizeof(X86PML4E);
if (uShw.pPML4->a[iShw2].n.u1Present)
{
LogFlow(("pgmPoolMonitorChainChanging: pml4 iShw2=%#x: %RX64 -> freeing it!\n", iShw2, uShw.pPML4->a[iShw2].u));
pgmPoolFree(pVM, uShw.pPML4->a[iShw2].u & X86_PML4E_PG_MASK, pPage->idx, iShw2);
ASMAtomicWriteU64(&uShw.pPML4->a[iShw2].u, 0);
}
}
break;
}
default:
AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, uShw.pv);
/* next */
if (pPage->iMonitoredNext == NIL_PGMPOOL_IDX)
return;
pPage = &pPool->aPages[pPage->iMonitoredNext];
}
}
#ifndef IN_RING3
/**
* Checks if a access could be a fork operation in progress.
*
* Meaning, that the guest is setting up the parent process for Copy-On-Write.
*
* @returns true if it's likely that we're forking, otherwise false.
* @param pPool The pool.
* @param pDis The disassembled instruction.
* @param offFault The access offset.
*/
DECLINLINE(bool) pgmRZPoolMonitorIsForking(PPGMPOOL pPool, PDISCPUSTATE pDis, unsigned offFault)
{
/*
* i386 linux is using btr to clear X86_PTE_RW.
* The functions involved are (2.6.16 source inspection):
* clear_bit
* ptep_set_wrprotect
* copy_one_pte
* copy_pte_range
* copy_pmd_range
* copy_pud_range
* copy_page_range
* dup_mmap
* dup_mm
* copy_mm
* copy_process
* do_fork
*/
if ( pDis->pCurInstr->uOpcode == OP_BTR
&& !(offFault & 4)
/** @todo Validate that the bit index is X86_PTE_RW. */
)
{
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitorPf,Fork)); RT_NOREF_PV(pPool);
return true;
}
return false;
}
/**
* Determine whether the page is likely to have been reused.
*
* @returns true if we consider the page as being reused for a different purpose.
* @returns false if we consider it to still be a paging page.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pRegFrame Trap register frame.
* @param pDis The disassembly info for the faulting instruction.
* @param pvFault The fault address.
* @param pPage The pool page being accessed.
*
* @remark The REP prefix check is left to the caller because of STOSD/W.
*/
DECLINLINE(bool) pgmRZPoolMonitorIsReused(PVMCC pVM, PVMCPUCC pVCpu, PCPUMCTXCORE pRegFrame, PDISCPUSTATE pDis, RTGCPTR pvFault,
PPGMPOOLPAGE pPage)
{
/* Locked (CR3, PDPTR*4) should not be reusable. Considering them as
such may cause loops booting tst-ubuntu-15_10-64-efi, ++. */
if (pPage->cLocked)
{
Log2(("pgmRZPoolMonitorIsReused: %RGv (%p) can't have been resued, because it's locked!\n", pvFault, pPage));
return false;
}
/** @todo could make this general, faulting close to rsp should be a safe reuse heuristic. */
if ( HMHasPendingIrq(pVM)
&& pRegFrame->rsp - pvFault < 32)
{
/* Fault caused by stack writes while trying to inject an interrupt event. */
Log(("pgmRZPoolMonitorIsReused: reused %RGv for interrupt stack (rsp=%RGv).\n", pvFault, pRegFrame->rsp));
return true;
}
LogFlow(("Reused instr %RGv %d at %RGv param1.fUse=%llx param1.reg=%d\n", pRegFrame->rip, pDis->pCurInstr->uOpcode, pvFault, pDis->Param1.fUse, pDis->Param1.Base.idxGenReg));
/* Non-supervisor mode write means it's used for something else. */
if (CPUMGetGuestCPL(pVCpu) == 3)
return true;
switch (pDis->pCurInstr->uOpcode)
{
/* call implies the actual push of the return address faulted */
case OP_CALL:
Log4(("pgmRZPoolMonitorIsReused: CALL\n"));
return true;
case OP_PUSH:
Log4(("pgmRZPoolMonitorIsReused: PUSH\n"));
return true;
case OP_PUSHF:
Log4(("pgmRZPoolMonitorIsReused: PUSHF\n"));
return true;
case OP_PUSHA:
Log4(("pgmRZPoolMonitorIsReused: PUSHA\n"));
return true;
case OP_FXSAVE:
Log4(("pgmRZPoolMonitorIsReused: FXSAVE\n"));
return true;
case OP_MOVNTI: /* solaris - block_zero_no_xmm */
Log4(("pgmRZPoolMonitorIsReused: MOVNTI\n"));
return true;
case OP_MOVNTDQ: /* solaris - hwblkclr & hwblkpagecopy */
Log4(("pgmRZPoolMonitorIsReused: MOVNTDQ\n"));
return true;
case OP_MOVSWD:
case OP_STOSWD:
if ( pDis->fPrefix == (DISPREFIX_REP|DISPREFIX_REX)
&& pRegFrame->rcx >= 0x40
)
{
Assert(pDis->uCpuMode == DISCPUMODE_64BIT);
Log(("pgmRZPoolMonitorIsReused: OP_STOSQ\n"));
return true;
}
break;
default:
/*
* Anything having ESP on the left side means stack writes.
*/
if ( ( (pDis->Param1.fUse & DISUSE_REG_GEN32)
|| (pDis->Param1.fUse & DISUSE_REG_GEN64))
&& (pDis->Param1.Base.idxGenReg == DISGREG_ESP))
{
Log4(("pgmRZPoolMonitorIsReused: ESP\n"));
return true;
}
break;
}
/*
* Page table updates are very very unlikely to be crossing page boundraries,
* and we don't want to deal with that in pgmPoolMonitorChainChanging and such.
*/
uint32_t const cbWrite = DISGetParamSize(pDis, &pDis->Param1);
if ( (((uintptr_t)pvFault + cbWrite) >> X86_PAGE_SHIFT) != ((uintptr_t)pvFault >> X86_PAGE_SHIFT) )
{
Log4(("pgmRZPoolMonitorIsReused: cross page write\n"));
return true;
}
/*
* Nobody does an unaligned 8 byte write to a page table, right.
*/
if (cbWrite >= 8 && ((uintptr_t)pvFault & 7) != 0)
{
Log4(("pgmRZPoolMonitorIsReused: Unaligned 8+ byte write\n"));
return true;
}
return false;
}
/**
* Flushes the page being accessed.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pDis The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
* @todo VBOXSTRICTRC
*/
static int pgmRZPoolAccessPfHandlerFlush(PVMCC pVM, PVMCPUCC pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
{
NOREF(pVM); NOREF(GCPhysFault);
/*
* First, do the flushing.
*/
pgmPoolMonitorChainFlush(pPool, pPage);
/*
* Emulate the instruction (xp/w2k problem, requires pc/cr2/sp detection).
* Must do this in raw mode (!); XP boot will fail otherwise.
*/
int rc = VINF_SUCCESS;
VBOXSTRICTRC rc2 = EMInterpretInstructionDisasState(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_ALL);
if (rc2 == VINF_SUCCESS)
{ /* do nothing */ }
else if (rc2 == VINF_EM_RESCHEDULE)
{
rc = VBOXSTRICTRC_VAL(rc2);
# ifndef IN_RING3
VMCPU_FF_SET(pVCpu, VMCPU_FF_TO_R3);
# endif
}
else if (rc2 == VERR_EM_INTERPRETER)
{
rc = VINF_EM_RAW_EMULATE_INSTR;
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitorPf,EmulateInstr));
}
else if (RT_FAILURE_NP(rc2))
rc = VBOXSTRICTRC_VAL(rc2);
else
AssertMsgFailed(("%Rrc\n", VBOXSTRICTRC_VAL(rc2))); /* ASSUMES no complicated stuff here. */
LogFlow(("pgmRZPoolAccessPfHandlerFlush: returns %Rrc (flushed)\n", rc));
return rc;
}
/**
* Handles the STOSD write accesses.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The cross context VM structure.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pDis The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
*/
DECLINLINE(int) pgmRZPoolAccessPfHandlerSTOSD(PVMCC pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault)
{
unsigned uIncrement = pDis->Param1.cb;
NOREF(pVM);
Assert(pDis->uCpuMode == DISCPUMODE_32BIT || pDis->uCpuMode == DISCPUMODE_64BIT);
Assert(pRegFrame->rcx <= 0x20);
# ifdef VBOX_STRICT
if (pDis->uOpMode == DISCPUMODE_32BIT)
Assert(uIncrement == 4);
else
Assert(uIncrement == 8);
# endif
Log3(("pgmRZPoolAccessPfHandlerSTOSD\n"));
/*
* Increment the modification counter and insert it into the list
* of modified pages the first time.
*/
if (!pPage->cModifications++)
pgmPoolMonitorModifiedInsert(pPool, pPage);
/*
* Execute REP STOSD.
*
* This ASSUMES that we're not invoked by Trap0e on in a out-of-sync
* write situation, meaning that it's safe to write here.
*/
PVMCPUCC pVCpu = VMMGetCpu(pPool->CTX_SUFF(pVM));
RTGCUINTPTR pu32 = (RTGCUINTPTR)pvFault;
while (pRegFrame->rcx)
{
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
uint32_t iPrevSubset = PGMRZDynMapPushAutoSubset(pVCpu);
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, NULL, uIncrement);
PGMRZDynMapPopAutoSubset(pVCpu, iPrevSubset);
# else
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, NULL, uIncrement);
# endif
PGMPhysSimpleWriteGCPhys(pVM, GCPhysFault, &pRegFrame->rax, uIncrement);
pu32 += uIncrement;
GCPhysFault += uIncrement;
pRegFrame->rdi += uIncrement;
pRegFrame->rcx--;
}
pRegFrame->rip += pDis->cbInstr;
LogFlow(("pgmRZPoolAccessPfHandlerSTOSD: returns\n"));
return VINF_SUCCESS;
}
/**
* Handles the simple write accesses.
*
* @returns VBox status code suitable for scheduling.
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
* @param pPool The pool.
* @param pPage The pool page (head).
* @param pDis The disassembly of the write instruction.
* @param pRegFrame The trap register frame.
* @param GCPhysFault The fault address as guest physical address.
* @param pvFault The fault address.
* @param pfReused Reused state (in/out)
*/
DECLINLINE(int) pgmRZPoolAccessPfHandlerSimple(PVMCC pVM, PVMCPUCC pVCpu, PPGMPOOL pPool, PPGMPOOLPAGE pPage, PDISCPUSTATE pDis,
PCPUMCTXCORE pRegFrame, RTGCPHYS GCPhysFault, RTGCPTR pvFault, bool *pfReused)
{
Log3(("pgmRZPoolAccessPfHandlerSimple\n"));
NOREF(pVM);
NOREF(pfReused); /* initialized by caller */
/*
* Increment the modification counter and insert it into the list
* of modified pages the first time.
*/
if (!pPage->cModifications++)
pgmPoolMonitorModifiedInsert(pPool, pPage);
/*
* Clear all the pages. ASSUMES that pvFault is readable.
*/
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
uint32_t iPrevSubset = PGMRZDynMapPushAutoSubset(pVCpu);
# endif
uint32_t cbWrite = DISGetParamSize(pDis, &pDis->Param1);
if (cbWrite <= 8)
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, NULL, cbWrite);
else if (cbWrite <= 16)
{
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault, NULL, 8);
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault + 8, NULL, cbWrite - 8);
}
else
{
Assert(cbWrite <= 32);
for (uint32_t off = 0; off < cbWrite; off += 8)
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhysFault + off, NULL, RT_MIN(8, cbWrite - off));
}
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
PGMRZDynMapPopAutoSubset(pVCpu, iPrevSubset);
# endif
/*
* Interpret the instruction.
*/
VBOXSTRICTRC rc = EMInterpretInstructionDisasState(pVCpu, pDis, pRegFrame, pvFault, EMCODETYPE_ALL);
if (RT_SUCCESS(rc))
AssertMsg(rc == VINF_SUCCESS, ("%Rrc\n", VBOXSTRICTRC_VAL(rc))); /* ASSUMES no complicated stuff here. */
else if (rc == VERR_EM_INTERPRETER)
{
LogFlow(("pgmRZPoolAccessPfHandlerSimple: Interpretation failed for %04x:%RGv - opcode=%d\n",
pRegFrame->cs.Sel, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->uOpcode));
rc = VINF_EM_RAW_EMULATE_INSTR;
STAM_COUNTER_INC(&pPool->CTX_MID_Z(StatMonitorPf,EmulateInstr));
}
# if 0 /* experimental code */
if (rc == VINF_SUCCESS)
{
switch (pPage->enmKind)
{
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
{
X86PTEPAE GstPte;
int rc = pgmPoolPhysSimpleReadGCPhys(pVM, &GstPte, pvFault, GCPhysFault, sizeof(GstPte));
AssertRC(rc);
/* Check the new value written by the guest. If present and with a bogus physical address, then
* it's fairly safe to assume the guest is reusing the PT.
*/
if (GstPte.n.u1Present)
{
RTHCPHYS HCPhys = -1;
int rc = PGMPhysGCPhys2HCPhys(pVM, GstPte.u & X86_PTE_PAE_PG_MASK, &HCPhys);
if (rc != VINF_SUCCESS)
{
*pfReused = true;
STAM_COUNTER_INC(&pPool->StatForceFlushReused);
}
}
break;
}
}
}
# endif
LogFlow(("pgmRZPoolAccessPfHandlerSimple: returns %Rrc\n", VBOXSTRICTRC_VAL(rc)));
return VBOXSTRICTRC_VAL(rc);
}
/**
* @callback_method_impl{FNPGMRZPHYSPFHANDLER,
* \#PF access handler callback for page table pages.}
*
* @remarks The @a pvUser argument points to the PGMPOOLPAGE.
*/
DECLEXPORT(VBOXSTRICTRC) pgmRZPoolAccessPfHandler(PVMCC pVM, PVMCPUCC pVCpu, RTGCUINT uErrorCode, PCPUMCTXCORE pRegFrame,
RTGCPTR pvFault, RTGCPHYS GCPhysFault, void *pvUser)
{
STAM_PROFILE_START(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorRZ, a);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
unsigned cMaxModifications;
bool fForcedFlush = false;
NOREF(uErrorCode);
LogFlow(("pgmRZPoolAccessPfHandler: pvFault=%RGv pPage=%p:{.idx=%d} GCPhysFault=%RGp\n", pvFault, pPage, pPage->idx, GCPhysFault));
pgmLock(pVM);
if (PHYS_PAGE_ADDRESS(GCPhysFault) != PHYS_PAGE_ADDRESS(pPage->GCPhys))
{
/* Pool page changed while we were waiting for the lock; ignore. */
Log(("CPU%d: pgmRZPoolAccessPfHandler pgm pool page for %RGp changed (to %RGp) while waiting!\n", pVCpu->idCpu, PHYS_PAGE_ADDRESS(GCPhysFault), PHYS_PAGE_ADDRESS(pPage->GCPhys)));
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorPfRZ, &pPool->StatMonitorPfRZHandled, a);
pgmUnlock(pVM);
return VINF_SUCCESS;
}
# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
{
Assert(VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_TLB_FLUSH));
pgmUnlock(pVM);
return VINF_SUCCESS; /* SMP guest case where we were blocking on the pgm lock while the same page was being marked dirty. */
}
# endif
# if 0 /* test code defined(VBOX_STRICT) && defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) */
if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
{
void *pvShw = PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pPage);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pPool->CTX_SUFF(pVM), pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackCheckPTPaePae(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PTPAE)pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvShw);
}
# endif
/*
* Disassemble the faulting instruction.
*/
PDISCPUSTATE pDis = &pVCpu->pgm.s.DisState;
int rc = EMInterpretDisasCurrent(pVM, pVCpu, pDis, NULL);
if (RT_UNLIKELY(rc != VINF_SUCCESS))
{
AssertMsg(rc == VERR_PAGE_NOT_PRESENT || rc == VERR_PAGE_TABLE_NOT_PRESENT, ("Unexpected rc %d\n", rc));
pgmUnlock(pVM);
return rc;
}
Assert(pPage->enmKind != PGMPOOLKIND_FREE);
/*
* We should ALWAYS have the list head as user parameter. This
* is because we use that page to record the changes.
*/
Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
# ifdef IN_RING0
/* Maximum nr of modifications depends on the page type. */
if ( pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT
|| pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_32BIT_PT)
cMaxModifications = 4;
else
cMaxModifications = 24;
# else
cMaxModifications = 48;
# endif
/*
* Incremental page table updates should weigh more than random ones.
* (Only applies when started from offset 0)
*/
pVCpu->pgm.s.cPoolAccessHandler++;
if ( pPage->GCPtrLastAccessHandlerRip >= pRegFrame->rip - 0x40 /* observed loops in Windows 7 x64 */
&& pPage->GCPtrLastAccessHandlerRip < pRegFrame->rip + 0x40
&& pvFault == (pPage->GCPtrLastAccessHandlerFault + pDis->Param1.cb)
&& pVCpu->pgm.s.cPoolAccessHandler == pPage->cLastAccessHandler + 1)
{
Log(("Possible page reuse cMods=%d -> %d (locked=%d type=%s)\n", pPage->cModifications, pPage->cModifications * 2, pgmPoolIsPageLocked(pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
Assert(pPage->cModifications < 32000);
pPage->cModifications = pPage->cModifications * 2;
pPage->GCPtrLastAccessHandlerFault = pvFault;
pPage->cLastAccessHandler = pVCpu->pgm.s.cPoolAccessHandler;
if (pPage->cModifications >= cMaxModifications)
{
STAM_COUNTER_INC(&pPool->StatMonitorPfRZFlushReinit);
fForcedFlush = true;
}
}
if (pPage->cModifications >= cMaxModifications)
Log(("Mod overflow %RGv cMods=%d (locked=%d type=%s)\n", pvFault, pPage->cModifications, pgmPoolIsPageLocked(pPage), pgmPoolPoolKindToStr(pPage->enmKind)));
/*
* Check if it's worth dealing with.
*/
bool fReused = false;
bool fNotReusedNotForking = false;
if ( ( pPage->cModifications < cMaxModifications /** @todo \#define */ /** @todo need to check that it's not mapping EIP. */ /** @todo adjust this! */
|| pgmPoolIsPageLocked(pPage)
)
&& !(fReused = pgmRZPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault, pPage))
&& !pgmRZPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
{
/*
* Simple instructions, no REP prefix.
*/
if (!(pDis->fPrefix & (DISPREFIX_REP | DISPREFIX_REPNE)))
{
rc = pgmRZPoolAccessPfHandlerSimple(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault, &fReused);
if (fReused)
goto flushPage;
/* A mov instruction to change the first page table entry will be remembered so we can detect
* full page table changes early on. This will reduce the amount of unnecessary traps we'll take.
*/
if ( rc == VINF_SUCCESS
&& !pPage->cLocked /* only applies to unlocked pages as we can't free locked ones (e.g. cr3 root). */
&& pDis->pCurInstr->uOpcode == OP_MOV
&& (pvFault & PAGE_OFFSET_MASK) == 0)
{
pPage->GCPtrLastAccessHandlerFault = pvFault;
pPage->cLastAccessHandler = pVCpu->pgm.s.cPoolAccessHandler;
pPage->GCPtrLastAccessHandlerRip = pRegFrame->rip;
/* Make sure we don't kick out a page too quickly. */
if (pPage->cModifications > 8)
pPage->cModifications = 2;
}
else if (pPage->GCPtrLastAccessHandlerFault == pvFault)
{
/* ignore the 2nd write to this page table entry. */
pPage->cLastAccessHandler = pVCpu->pgm.s.cPoolAccessHandler;
}
else
{
pPage->GCPtrLastAccessHandlerFault = NIL_RTGCPTR;
pPage->GCPtrLastAccessHandlerRip = 0;
}
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorPfRZ, &pPool->StatMonitorPfRZHandled, a);
pgmUnlock(pVM);
return rc;
}
/*
* Windows is frequently doing small memset() operations (netio test 4k+).
* We have to deal with these or we'll kill the cache and performance.
*/
if ( pDis->pCurInstr->uOpcode == OP_STOSWD
&& !pRegFrame->eflags.Bits.u1DF
&& pDis->uOpMode == pDis->uCpuMode
&& pDis->uAddrMode == pDis->uCpuMode)
{
bool fValidStosd = false;
if ( pDis->uCpuMode == DISCPUMODE_32BIT
&& pDis->fPrefix == DISPREFIX_REP
&& pRegFrame->ecx <= 0x20
&& pRegFrame->ecx * 4 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
&& !((uintptr_t)pvFault & 3)
&& (pRegFrame->eax == 0 || pRegFrame->eax == 0x80) /* the two values observed. */
)
{
fValidStosd = true;
pRegFrame->rcx &= 0xffffffff; /* paranoia */
}
else
if ( pDis->uCpuMode == DISCPUMODE_64BIT
&& pDis->fPrefix == (DISPREFIX_REP | DISPREFIX_REX)
&& pRegFrame->rcx <= 0x20
&& pRegFrame->rcx * 8 <= PAGE_SIZE - ((uintptr_t)pvFault & PAGE_OFFSET_MASK)
&& !((uintptr_t)pvFault & 7)
&& (pRegFrame->rax == 0 || pRegFrame->rax == 0x80) /* the two values observed. */
)
{
fValidStosd = true;
}
if (fValidStosd)
{
rc = pgmRZPoolAccessPfHandlerSTOSD(pVM, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorPfRZ, &pPool->StatMonitorPfRZRepStosd, a);
pgmUnlock(pVM);
return rc;
}
}
/* REP prefix, don't bother. */
STAM_COUNTER_INC(&pPool->StatMonitorPfRZRepPrefix);
Log4(("pgmRZPoolAccessPfHandler: eax=%#x ecx=%#x edi=%#x esi=%#x rip=%RGv opcode=%d prefix=%#x\n",
pRegFrame->eax, pRegFrame->ecx, pRegFrame->edi, pRegFrame->esi, (RTGCPTR)pRegFrame->rip, pDis->pCurInstr->uOpcode, pDis->fPrefix));
fNotReusedNotForking = true;
}
# if defined(PGMPOOL_WITH_OPTIMIZED_DIRTY_PT) && defined(IN_RING0)
/* E.g. Windows 7 x64 initializes page tables and touches some pages in the table during the process. This
* leads to pgm pool trashing and an excessive amount of write faults due to page monitoring.
*/
if ( pPage->cModifications >= cMaxModifications
&& !fForcedFlush
&& (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT || pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_32BIT_PT)
&& ( fNotReusedNotForking
|| ( !pgmRZPoolMonitorIsReused(pVM, pVCpu, pRegFrame, pDis, pvFault, pPage)
&& !pgmRZPoolMonitorIsForking(pPool, pDis, GCPhysFault & PAGE_OFFSET_MASK))
)
)
{
Assert(!pgmPoolIsPageLocked(pPage));
Assert(pPage->fDirty == false);
/* Flush any monitored duplicates as we will disable write protection. */
if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
|| pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pPageHead = pPage;
/* Find the monitor head. */
while (pPageHead->iMonitoredPrev != NIL_PGMPOOL_IDX)
pPageHead = &pPool->aPages[pPageHead->iMonitoredPrev];
while (pPageHead)
{
unsigned idxNext = pPageHead->iMonitoredNext;
if (pPageHead != pPage)
{
STAM_COUNTER_INC(&pPool->StatDirtyPageDupFlush);
Log(("Flush duplicate page idx=%d GCPhys=%RGp type=%s\n", pPageHead->idx, pPageHead->GCPhys, pgmPoolPoolKindToStr(pPageHead->enmKind)));
int rc2 = pgmPoolFlushPage(pPool, pPageHead);
AssertRC(rc2);
}
if (idxNext == NIL_PGMPOOL_IDX)
break;
pPageHead = &pPool->aPages[idxNext];
}
}
/* The flushing above might fail for locked pages, so double check. */
if ( pPage->iMonitoredNext == NIL_PGMPOOL_IDX
&& pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
{
pgmPoolAddDirtyPage(pVM, pPool, pPage);
/* Temporarily allow write access to the page table again. */
rc = PGMHandlerPhysicalPageTempOff(pVM, pPage->GCPhys & PAGE_BASE_GC_MASK, pPage->GCPhys & PAGE_BASE_GC_MASK);
if (rc == VINF_SUCCESS)
{
rc = PGMShwMakePageWritable(pVCpu, pvFault, PGM_MK_PG_IS_WRITE_FAULT);
AssertMsg(rc == VINF_SUCCESS
/* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
|| rc == VERR_PAGE_TABLE_NOT_PRESENT
|| rc == VERR_PAGE_NOT_PRESENT,
("PGMShwModifyPage -> GCPtr=%RGv rc=%d\n", pvFault, rc));
# ifdef VBOX_STRICT
pPage->GCPtrDirtyFault = pvFault;
# endif
STAM_PROFILE_STOP(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorPfRZ, a);
pgmUnlock(pVM);
return rc;
}
}
}
# endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT && IN_RING0 */
STAM_COUNTER_INC(&pPool->StatMonitorPfRZFlushModOverflow);
flushPage:
/*
* Not worth it, so flush it.
*
* If we considered it to be reused, don't go back to ring-3
* to emulate failed instructions since we usually cannot
* interpret then. This may be a bit risky, in which case
* the reuse detection must be fixed.
*/
rc = pgmRZPoolAccessPfHandlerFlush(pVM, pVCpu, pPool, pPage, pDis, pRegFrame, GCPhysFault, pvFault);
if ( rc == VINF_EM_RAW_EMULATE_INSTR
&& fReused)
{
/* Make sure that the current instruction still has shadow page backing, otherwise we'll end up in a loop. */
if (PGMShwGetPage(pVCpu, pRegFrame->rip, NULL, NULL) == VINF_SUCCESS)
rc = VINF_SUCCESS; /* safe to restart the instruction. */
}
STAM_PROFILE_STOP_EX(&pVM->pgm.s.CTX_SUFF(pPool)->StatMonitorPfRZ, &pPool->StatMonitorPfRZFlushPage, a);
pgmUnlock(pVM);
return rc;
}
#endif /* !IN_RING3 */
/**
* @callback_method_impl{FNPGMPHYSHANDLER,
* Access handler for shadowed page table pages.}
*
* @remarks Only uses the VINF_PGM_HANDLER_DO_DEFAULT status.
*/
PGM_ALL_CB2_DECL(VBOXSTRICTRC)
pgmPoolAccessHandler(PVMCC pVM, PVMCPUCC pVCpu, RTGCPHYS GCPhys, void *pvPhys, void *pvBuf, size_t cbBuf,
PGMACCESSTYPE enmAccessType, PGMACCESSORIGIN enmOrigin, void *pvUser)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
STAM_PROFILE_START(&pPool->CTX_SUFF_Z(StatMonitor), a);
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)pvUser;
LogFlow(("PGM_ALL_CB_DECL: GCPhys=%RGp %p:{.Core=%RHp, .idx=%d, .GCPhys=%RGp, .enmType=%d}\n",
GCPhys, pPage, pPage->Core.Key, pPage->idx, pPage->GCPhys, pPage->enmKind));
NOREF(pvPhys); NOREF(pvBuf); NOREF(enmAccessType);
pgmLock(pVM);
#ifdef VBOX_WITH_STATISTICS
/*
* Collect stats on the access.
*/
AssertCompile(RT_ELEMENTS(pPool->CTX_MID_Z(aStatMonitor,Sizes)) == 19);
if (cbBuf <= 16 && cbBuf > 0)
STAM_COUNTER_INC(&pPool->CTX_MID_Z(aStatMonitor,Sizes)[cbBuf - 1]);
else if (cbBuf >= 17 && cbBuf < 32)
STAM_COUNTER_INC(&pPool->CTX_MID_Z(aStatMonitor,Sizes)[16]);
else if (cbBuf >= 32 && cbBuf < 64)
STAM_COUNTER_INC(&pPool->CTX_MID_Z(aStatMonitor,Sizes)[17]);
else if (cbBuf >= 64)
STAM_COUNTER_INC(&pPool->CTX_MID_Z(aStatMonitor,Sizes)[18]);
uint8_t cbAlign;
switch (pPage->enmKind)
{
default:
cbAlign = 7;
break;
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_32BIT_PD_PHYS:
cbAlign = 3;
break;
}
AssertCompile(RT_ELEMENTS(pPool->CTX_MID_Z(aStatMonitor,Misaligned)) == 7);
if ((uint8_t)GCPhys & cbAlign)
STAM_COUNTER_INC(&pPool->CTX_MID_Z(aStatMonitor,Misaligned)[((uint8_t)GCPhys & cbAlign) - 1]);
#endif
/*
* Make sure the pool page wasn't modified by a different CPU.
*/
if (PHYS_PAGE_ADDRESS(GCPhys) == PHYS_PAGE_ADDRESS(pPage->GCPhys))
{
Assert(pPage->enmKind != PGMPOOLKIND_FREE);
/* The max modification count before flushing depends on the context and page type. */
#ifdef IN_RING3
uint16_t const cMaxModifications = 96; /* it's cheaper here, right? */
#else
uint16_t cMaxModifications;
if ( pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT
|| pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_32BIT_PT)
cMaxModifications = 4;
else
cMaxModifications = 24;
#endif
/*
* We don't have to be very sophisticated about this since there are relativly few calls here.
* However, we must try our best to detect any non-cpu accesses (disk / networking).
*/
if ( ( pPage->cModifications < cMaxModifications
|| pgmPoolIsPageLocked(pPage) )
&& enmOrigin != PGMACCESSORIGIN_DEVICE
&& cbBuf <= 16)
{
/* Clear the shadow entry. */
if (!pPage->cModifications++)
pgmPoolMonitorModifiedInsert(pPool, pPage);
if (cbBuf <= 8)
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys, pvBuf, (uint32_t)cbBuf);
else
{
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys, pvBuf, 8);
pgmPoolMonitorChainChanging(pVCpu, pPool, pPage, GCPhys + 8, (uint8_t *)pvBuf + 8, (uint32_t)cbBuf - 8);
}
}
else
pgmPoolMonitorChainFlush(pPool, pPage);
STAM_PROFILE_STOP_EX(&pPool->CTX_SUFF_Z(StatMonitor), &pPool->CTX_MID_Z(StatMonitor,FlushPage), a);
}
else
Log(("CPU%d: PGM_ALL_CB_DECL pgm pool page for %RGp changed (to %RGp) while waiting!\n", pVCpu->idCpu, PHYS_PAGE_ADDRESS(GCPhys), PHYS_PAGE_ADDRESS(pPage->GCPhys)));
pgmUnlock(pVM);
return VINF_PGM_HANDLER_DO_DEFAULT;
}
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
# if defined(VBOX_STRICT) && !defined(IN_RING3)
/**
* Check references to guest physical memory in a PAE / PAE page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
static void pgmPoolTrackCheckPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PTPAE pGstPT)
{
unsigned cErrors = 0;
int LastRc = -1; /* initialized to shut up gcc */
unsigned LastPTE = ~0U; /* initialized to shut up gcc */
RTHCPHYS LastHCPhys = NIL_RTHCPHYS; /* initialized to shut up gcc */
PVMCC pVM = pPool->CTX_SUFF(pVM);
# ifdef VBOX_STRICT
for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
AssertMsg(!PGMSHWPTEPAE_IS_P(pShwPT->a[i]), ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), pPage->iFirstPresent));
# endif
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
RTHCPHYS HCPhys = NIL_RTHCPHYS;
int rc = PGMPhysGCPhys2HCPhys(pVM, pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
if ( rc != VINF_SUCCESS
|| PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]) != HCPhys)
{
Log(("rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", rc, i, pGstPT->a[i].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), HCPhys));
LastPTE = i;
LastRc = rc;
LastHCPhys = HCPhys;
cErrors++;
RTHCPHYS HCPhysPT = NIL_RTHCPHYS;
rc = PGMPhysGCPhys2HCPhys(pVM, pPage->GCPhys, &HCPhysPT);
AssertRC(rc);
for (unsigned iPage = 0; iPage < pPool->cCurPages; iPage++)
{
PPGMPOOLPAGE pTempPage = &pPool->aPages[iPage];
if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
{
PPGMSHWPTPAE pShwPT2 = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pVM, pTempPage);
for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
{
if ( PGMSHWPTEPAE_IS_P_RW(pShwPT2->a[j])
&& PGMSHWPTEPAE_GET_HCPHYS(pShwPT2->a[j]) == HCPhysPT)
{
Log(("GCPhys=%RGp idx=%d %RX64 vs %RX64\n", pTempPage->GCPhys, j, PGMSHWPTEPAE_GET_LOG(pShwPT->a[j]), PGMSHWPTEPAE_GET_LOG(pShwPT2->a[j])));
}
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pShwPT2);
}
}
}
}
}
AssertMsg(!cErrors, ("cErrors=%d: last rc=%d idx=%d guest %RX64 shw=%RX64 vs %RHp\n", cErrors, LastRc, LastPTE, pGstPT->a[LastPTE].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[LastPTE]), LastHCPhys));
}
/**
* Check references to guest physical memory in a PAE / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
static void pgmPoolTrackCheckPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PT pGstPT)
{
unsigned cErrors = 0;
int LastRc = -1; /* initialized to shut up gcc */
unsigned LastPTE = ~0U; /* initialized to shut up gcc */
RTHCPHYS LastHCPhys = NIL_RTHCPHYS; /* initialized to shut up gcc */
PVMCC pVM = pPool->CTX_SUFF(pVM);
# ifdef VBOX_STRICT
for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
AssertMsg(!PGMSHWPTEPAE_IS_P(pShwPT->a[i]), ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), pPage->iFirstPresent));
# endif
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
RTHCPHYS HCPhys = NIL_RTHCPHYS;
int rc = PGMPhysGCPhys2HCPhys(pVM, pGstPT->a[i].u & X86_PTE_PG_MASK, &HCPhys);
if ( rc != VINF_SUCCESS
|| PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]) != HCPhys)
{
Log(("rc=%d idx=%d guest %x shw=%RX64 vs %RHp\n", rc, i, pGstPT->a[i].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), HCPhys));
LastPTE = i;
LastRc = rc;
LastHCPhys = HCPhys;
cErrors++;
RTHCPHYS HCPhysPT = NIL_RTHCPHYS;
rc = PGMPhysGCPhys2HCPhys(pVM, pPage->GCPhys, &HCPhysPT);
AssertRC(rc);
for (unsigned iPage = 0; iPage < pPool->cCurPages; iPage++)
{
PPGMPOOLPAGE pTempPage = &pPool->aPages[iPage];
if (pTempPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_32BIT_PT)
{
PPGMSHWPTPAE pShwPT2 = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pVM, pTempPage);
for (unsigned j = 0; j < RT_ELEMENTS(pShwPT->a); j++)
{
if ( PGMSHWPTEPAE_IS_P_RW(pShwPT2->a[j])
&& PGMSHWPTEPAE_GET_HCPHYS(pShwPT2->a[j]) == HCPhysPT)
{
Log(("GCPhys=%RGp idx=%d %RX64 vs %RX64\n", pTempPage->GCPhys, j, PGMSHWPTEPAE_GET_LOG(pShwPT->a[j]), PGMSHWPTEPAE_GET_LOG(pShwPT2->a[j])));
}
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pShwPT2);
}
}
}
}
}
AssertMsg(!cErrors, ("cErrors=%d: last rc=%d idx=%d guest %x shw=%RX64 vs %RHp\n", cErrors, LastRc, LastPTE, pGstPT->a[LastPTE].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[LastPTE]), LastHCPhys));
}
# endif /* VBOX_STRICT && !IN_RING3 */
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @returns nr of changed PTEs
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
* @param pOldGstPT The old cached guest page table.
* @param fAllowRemoval Bail out as soon as we encounter an invalid PTE
* @param pfFlush Flush reused page table (out)
*/
DECLINLINE(unsigned) pgmPoolTrackFlushPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PTPAE pGstPT,
PCX86PTPAE pOldGstPT, bool fAllowRemoval, bool *pfFlush)
{
unsigned cChanged = 0;
# ifdef VBOX_STRICT
for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
AssertMsg(!PGMSHWPTEPAE_IS_P(pShwPT->a[i]), ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), pPage->iFirstPresent));
# endif
*pfFlush = false;
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
/* Check the new value written by the guest. If present and with a bogus physical address, then
* it's fairly safe to assume the guest is reusing the PT.
*/
if ( fAllowRemoval
&& pGstPT->a[i].n.u1Present)
{
if (!PGMPhysIsGCPhysValid(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
{
*pfFlush = true;
return ++cChanged;
}
}
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
/* If the old cached PTE is identical, then there's no need to flush the shadow copy. */
if ((pGstPT->a[i].u & X86_PTE_PAE_PG_MASK) == (pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK))
{
# ifdef VBOX_STRICT
RTHCPHYS HCPhys = NIL_RTGCPHYS;
int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK, &HCPhys);
AssertMsg(rc == VINF_SUCCESS && PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]) == HCPhys, ("rc=%d guest %RX64 old %RX64 shw=%RX64 vs %RHp\n", rc, pGstPT->a[i].u, pOldGstPT->a[i].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), HCPhys));
# endif
uint64_t uHostAttr = PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
bool fHostRW = !!(PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & X86_PTE_RW);
uint64_t uGuestAttr = pGstPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G | X86_PTE_PAE_NX);
bool fGuestRW = !!(pGstPT->a[i].u & X86_PTE_RW);
if ( uHostAttr == uGuestAttr
&& fHostRW <= fGuestRW)
continue;
}
cChanged++;
/* Something was changed, so flush it. */
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX64 hint=%RX64\n",
i, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pOldGstPT->a[i].u & X86_PTE_PAE_PG_MASK, i);
PGMSHWPTEPAE_ATOMIC_SET(pShwPT->a[i], 0);
}
}
return cChanged;
}
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @returns nr of changed PTEs
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
* @param pOldGstPT The old cached guest page table.
* @param fAllowRemoval Bail out as soon as we encounter an invalid PTE
* @param pfFlush Flush reused page table (out)
*/
DECLINLINE(unsigned) pgmPoolTrackFlushPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PT pGstPT,
PCX86PT pOldGstPT, bool fAllowRemoval, bool *pfFlush)
{
unsigned cChanged = 0;
# ifdef VBOX_STRICT
for (unsigned i = 0; i < RT_MIN(RT_ELEMENTS(pShwPT->a), pPage->iFirstPresent); i++)
AssertMsg(!PGMSHWPTEPAE_IS_P(pShwPT->a[i]), ("Unexpected PTE: idx=%d %RX64 (first=%d)\n", i, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), pPage->iFirstPresent));
# endif
*pfFlush = false;
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
/* Check the new value written by the guest. If present and with a bogus physical address, then
* it's fairly safe to assume the guest is reusing the PT.
*/
if ( fAllowRemoval
&& pGstPT->a[i].n.u1Present)
{
if (!PGMPhysIsGCPhysValid(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PG_MASK))
{
*pfFlush = true;
return ++cChanged;
}
}
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
/* If the old cached PTE is identical, then there's no need to flush the shadow copy. */
if ((pGstPT->a[i].u & X86_PTE_PG_MASK) == (pOldGstPT->a[i].u & X86_PTE_PG_MASK))
{
# ifdef VBOX_STRICT
RTHCPHYS HCPhys = NIL_RTGCPHYS;
int rc = PGMPhysGCPhys2HCPhys(pPool->CTX_SUFF(pVM), pGstPT->a[i].u & X86_PTE_PG_MASK, &HCPhys);
AssertMsg(rc == VINF_SUCCESS && PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]) == HCPhys, ("rc=%d guest %x old %x shw=%RX64 vs %RHp\n", rc, pGstPT->a[i].u, pOldGstPT->a[i].u, PGMSHWPTEPAE_GET_LOG(pShwPT->a[i]), HCPhys));
# endif
uint64_t uHostAttr = PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G);
bool fHostRW = !!(PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & X86_PTE_RW);
uint64_t uGuestAttr = pGstPT->a[i].u & (X86_PTE_P | X86_PTE_US | X86_PTE_A | X86_PTE_D | X86_PTE_G);
bool fGuestRW = !!(pGstPT->a[i].u & X86_PTE_RW);
if ( uHostAttr == uGuestAttr
&& fHostRW <= fGuestRW)
continue;
}
cChanged++;
/* Something was changed, so flush it. */
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX64 hint=%x\n",
i, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pOldGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pOldGstPT->a[i].u & X86_PTE_PG_MASK, i);
PGMSHWPTEPAE_ATOMIC_SET(pShwPT->a[i], 0);
}
}
return cChanged;
}
/**
* Flush a dirty page
*
* @param pVM The cross context VM structure.
* @param pPool The pool.
* @param idxSlot Dirty array slot index
* @param fAllowRemoval Allow a reused page table to be removed
*/
static void pgmPoolFlushDirtyPage(PVMCC pVM, PPGMPOOL pPool, unsigned idxSlot, bool fAllowRemoval = false)
{
AssertCompile(RT_ELEMENTS(pPool->aidxDirtyPages) == RT_ELEMENTS(pPool->aDirtyPages));
Assert(idxSlot < RT_ELEMENTS(pPool->aDirtyPages));
unsigned idxPage = pPool->aidxDirtyPages[idxSlot];
if (idxPage == NIL_PGMPOOL_IDX)
return;
PPGMPOOLPAGE pPage = &pPool->aPages[idxPage];
Assert(pPage->idx == idxPage);
Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
AssertMsg(pPage->fDirty, ("Page %RGp (slot=%d) not marked dirty!", pPage->GCPhys, idxSlot));
Log(("Flush dirty page %RGp cMods=%d\n", pPage->GCPhys, pPage->cModifications));
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
PVMCPU pVCpu = VMMGetCpu(pVM);
uint32_t iPrevSubset = PGMRZDynMapPushAutoSubset(pVCpu);
# endif
/* First write protect the page again to catch all write accesses. (before checking for changes -> SMP) */
int rc = PGMHandlerPhysicalReset(pVM, pPage->GCPhys & PAGE_BASE_GC_MASK);
Assert(rc == VINF_SUCCESS);
pPage->fDirty = false;
# ifdef VBOX_STRICT
uint64_t fFlags = 0;
RTHCPHYS HCPhys;
rc = PGMShwGetPage(VMMGetCpu(pVM), pPage->GCPtrDirtyFault, &fFlags, &HCPhys);
AssertMsg( ( rc == VINF_SUCCESS
&& (!(fFlags & X86_PTE_RW) || HCPhys != pPage->Core.Key))
/* In the SMP case the page table might be removed while we wait for the PGM lock in the trap handler. */
|| rc == VERR_PAGE_TABLE_NOT_PRESENT
|| rc == VERR_PAGE_NOT_PRESENT,
("PGMShwGetPage -> GCPtr=%RGv rc=%d flags=%RX64\n", pPage->GCPtrDirtyFault, rc, fFlags));
# endif
/* Flush those PTEs that have changed. */
STAM_PROFILE_START(&pPool->StatTrackDeref,a);
void *pvShw = PGMPOOL_PAGE_2_PTR(pVM, pPage);
void *pvGst;
rc = PGM_GCPHYS_2_PTR_EX(pVM, pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
bool fFlush;
unsigned cChanges;
if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
cChanges = pgmPoolTrackFlushPTPaePae(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PTPAE)pvGst,
(PCX86PTPAE)&pPool->aDirtyPages[idxSlot].aPage[0], fAllowRemoval, &fFlush);
else
cChanges = pgmPoolTrackFlushPTPae32Bit(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PT)pvGst,
(PCX86PT)&pPool->aDirtyPages[idxSlot].aPage[0], fAllowRemoval, &fFlush);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
/* Note: we might want to consider keeping the dirty page active in case there were many changes. */
/* This page is likely to be modified again, so reduce the nr of modifications just a bit here. */
Assert(pPage->cModifications);
if (cChanges < 4)
pPage->cModifications = 1; /* must use > 0 here */
else
pPage->cModifications = RT_MAX(1, pPage->cModifications / 2);
STAM_COUNTER_INC(&pPool->StatResetDirtyPages);
if (pPool->cDirtyPages == RT_ELEMENTS(pPool->aDirtyPages))
pPool->idxFreeDirtyPage = idxSlot;
pPool->cDirtyPages--;
pPool->aidxDirtyPages[idxSlot] = NIL_PGMPOOL_IDX;
Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aDirtyPages));
if (fFlush)
{
Assert(fAllowRemoval);
Log(("Flush reused page table!\n"));
pgmPoolFlushPage(pPool, pPage);
STAM_COUNTER_INC(&pPool->StatForceFlushReused);
}
else
Log(("Removed dirty page %RGp cMods=%d cChanges=%d\n", pPage->GCPhys, pPage->cModifications, cChanges));
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
PGMRZDynMapPopAutoSubset(pVCpu, iPrevSubset);
# endif
}
# ifndef IN_RING3
/**
* Add a new dirty page
*
* @param pVM The cross context VM structure.
* @param pPool The pool.
* @param pPage The page.
*/
void pgmPoolAddDirtyPage(PVMCC pVM, PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
PGM_LOCK_ASSERT_OWNER(pVM);
AssertCompile(RT_ELEMENTS(pPool->aDirtyPages) == 8 || RT_ELEMENTS(pPool->aDirtyPages) == 16);
Assert(!pPage->fDirty);
unsigned idxFree = pPool->idxFreeDirtyPage;
Assert(idxFree < RT_ELEMENTS(pPool->aDirtyPages));
Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX && pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
if (pPool->cDirtyPages >= RT_ELEMENTS(pPool->aDirtyPages))
{
STAM_COUNTER_INC(&pPool->StatDirtyPageOverFlowFlush);
pgmPoolFlushDirtyPage(pVM, pPool, idxFree, true /* allow removal of reused page tables*/);
}
Assert(pPool->cDirtyPages < RT_ELEMENTS(pPool->aDirtyPages));
AssertMsg(pPool->aidxDirtyPages[idxFree] == NIL_PGMPOOL_IDX, ("idxFree=%d cDirtyPages=%d\n", idxFree, pPool->cDirtyPages));
Log(("Add dirty page %RGp (slot=%d)\n", pPage->GCPhys, idxFree));
/*
* Make a copy of the guest page table as we require valid GCPhys addresses
* when removing references to physical pages.
* (The HCPhys linear lookup is *extremely* expensive!)
*/
void *pvGst;
int rc = PGM_GCPHYS_2_PTR_EX(pVM, pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
memcpy(&pPool->aDirtyPages[idxFree].aPage[0], pvGst, (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT) ? PAGE_SIZE : PAGE_SIZE/2);
# ifdef VBOX_STRICT
void *pvShw = PGMPOOL_PAGE_2_PTR(pVM, pPage);
if (pPage->enmKind == PGMPOOLKIND_PAE_PT_FOR_PAE_PT)
pgmPoolTrackCheckPTPaePae(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PTPAE)pvGst);
else
pgmPoolTrackCheckPTPae32Bit(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PT)pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvShw);
# endif
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
STAM_COUNTER_INC(&pPool->StatDirtyPage);
pPage->fDirty = true;
pPage->idxDirtyEntry = (uint8_t)idxFree; Assert(pPage->idxDirtyEntry == idxFree);
pPool->aidxDirtyPages[idxFree] = pPage->idx;
pPool->cDirtyPages++;
pPool->idxFreeDirtyPage = (pPool->idxFreeDirtyPage + 1) & (RT_ELEMENTS(pPool->aDirtyPages) - 1);
if ( pPool->cDirtyPages < RT_ELEMENTS(pPool->aDirtyPages)
&& pPool->aidxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
{
unsigned i;
for (i = 1; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
idxFree = (pPool->idxFreeDirtyPage + i) & (RT_ELEMENTS(pPool->aDirtyPages) - 1);
if (pPool->aidxDirtyPages[idxFree] == NIL_PGMPOOL_IDX)
{
pPool->idxFreeDirtyPage = idxFree;
break;
}
}
Assert(i != RT_ELEMENTS(pPool->aDirtyPages));
}
Assert(pPool->cDirtyPages == RT_ELEMENTS(pPool->aDirtyPages) || pPool->aidxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX);
/*
* Clear all references to this shadow table. See @bugref{7298}.
*/
pgmPoolTrackClearPageUsers(pPool, pPage);
}
# endif /* !IN_RING3 */
/**
* Check if the specified page is dirty (not write monitored)
*
* @return dirty or not
* @param pVM The cross context VM structure.
* @param GCPhys Guest physical address
*/
bool pgmPoolIsDirtyPageSlow(PVM pVM, RTGCPHYS GCPhys)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PGM_LOCK_ASSERT_OWNER(pVM);
if (!pPool->cDirtyPages)
return false;
GCPhys = GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
unsigned idxPage = pPool->aidxDirtyPages[i];
if (idxPage != NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pPage = &pPool->aPages[idxPage];
if (pPage->GCPhys == GCPhys)
return true;
}
}
return false;
}
/**
* Reset all dirty pages by reinstating page monitoring.
*
* @param pVM The cross context VM structure.
*/
void pgmPoolResetDirtyPages(PVMCC pVM)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PGM_LOCK_ASSERT_OWNER(pVM);
Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aDirtyPages));
if (!pPool->cDirtyPages)
return;
Log(("pgmPoolResetDirtyPages\n"));
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
pgmPoolFlushDirtyPage(pVM, pPool, i, true /* allow removal of reused page tables*/);
pPool->idxFreeDirtyPage = 0;
if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aDirtyPages)
&& pPool->aidxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
{
unsigned i;
for (i = 1; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
if (pPool->aidxDirtyPages[i] == NIL_PGMPOOL_IDX)
{
pPool->idxFreeDirtyPage = i;
break;
}
}
AssertMsg(i != RT_ELEMENTS(pPool->aDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
}
Assert(pPool->aidxDirtyPages[pPool->idxFreeDirtyPage] == NIL_PGMPOOL_IDX || pPool->cDirtyPages == RT_ELEMENTS(pPool->aDirtyPages));
return;
}
/**
* Invalidate the PT entry for the specified page
*
* @param pVM The cross context VM structure.
* @param GCPtrPage Guest page to invalidate
*/
void pgmPoolResetDirtyPage(PVM pVM, RTGCPTR GCPtrPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PGM_LOCK_ASSERT_OWNER(pVM);
Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aDirtyPages));
if (!pPool->cDirtyPages)
return;
Log(("pgmPoolResetDirtyPage %RGv\n", GCPtrPage)); RT_NOREF_PV(GCPtrPage);
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
/** @todo What was intended here??? This looks incomplete... */
}
}
/**
* Reset all dirty pages by reinstating page monitoring.
*
* @param pVM The cross context VM structure.
* @param GCPhysPT Physical address of the page table
*/
void pgmPoolInvalidateDirtyPage(PVMCC pVM, RTGCPHYS GCPhysPT)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PGM_LOCK_ASSERT_OWNER(pVM);
Assert(pPool->cDirtyPages <= RT_ELEMENTS(pPool->aDirtyPages));
unsigned idxDirtyPage = RT_ELEMENTS(pPool->aDirtyPages);
if (!pPool->cDirtyPages)
return;
GCPhysPT = GCPhysPT & ~(RTGCPHYS)PAGE_OFFSET_MASK;
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
unsigned idxPage = pPool->aidxDirtyPages[i];
if (idxPage != NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pPage = &pPool->aPages[idxPage];
if (pPage->GCPhys == GCPhysPT)
{
idxDirtyPage = i;
break;
}
}
}
if (idxDirtyPage != RT_ELEMENTS(pPool->aDirtyPages))
{
pgmPoolFlushDirtyPage(pVM, pPool, idxDirtyPage, true /* allow removal of reused page tables*/);
if ( pPool->cDirtyPages != RT_ELEMENTS(pPool->aDirtyPages)
&& pPool->aidxDirtyPages[pPool->idxFreeDirtyPage] != NIL_PGMPOOL_IDX)
{
unsigned i;
for (i = 0; i < RT_ELEMENTS(pPool->aDirtyPages); i++)
{
if (pPool->aidxDirtyPages[i] == NIL_PGMPOOL_IDX)
{
pPool->idxFreeDirtyPage = i;
break;
}
}
AssertMsg(i != RT_ELEMENTS(pPool->aDirtyPages), ("cDirtyPages %d", pPool->cDirtyPages));
}
}
}
#endif /* PGMPOOL_WITH_OPTIMIZED_DIRTY_PT */
/**
* Inserts a page into the GCPhys hash table.
*
* @param pPool The pool.
* @param pPage The page.
*/
DECLINLINE(void) pgmPoolHashInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolHashInsert: %RGp\n", pPage->GCPhys));
Assert(pPage->GCPhys != NIL_RTGCPHYS); Assert(pPage->iNext == NIL_PGMPOOL_IDX);
uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
pPage->iNext = pPool->aiHash[iHash];
pPool->aiHash[iHash] = pPage->idx;
}
/**
* Removes a page from the GCPhys hash table.
*
* @param pPool The pool.
* @param pPage The page.
*/
DECLINLINE(void) pgmPoolHashRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolHashRemove: %RGp\n", pPage->GCPhys));
uint16_t iHash = PGMPOOL_HASH(pPage->GCPhys);
if (pPool->aiHash[iHash] == pPage->idx)
pPool->aiHash[iHash] = pPage->iNext;
else
{
uint16_t iPrev = pPool->aiHash[iHash];
for (;;)
{
const int16_t i = pPool->aPages[iPrev].iNext;
if (i == pPage->idx)
{
pPool->aPages[iPrev].iNext = pPage->iNext;
break;
}
if (i == NIL_PGMPOOL_IDX)
{
AssertReleaseMsgFailed(("GCPhys=%RGp idx=%d\n", pPage->GCPhys, pPage->idx));
break;
}
iPrev = i;
}
}
pPage->iNext = NIL_PGMPOOL_IDX;
}
/**
* Frees up one cache page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @param pPool The pool.
* @param iUser The user index.
*/
static int pgmPoolCacheFreeOne(PPGMPOOL pPool, uint16_t iUser)
{
const PVMCC pVM = pPool->CTX_SUFF(pVM);
Assert(pPool->iAgeHead != pPool->iAgeTail); /* We shouldn't be here if there < 2 cached entries! */
STAM_COUNTER_INC(&pPool->StatCacheFreeUpOne);
/*
* Select one page from the tail of the age list.
*/
PPGMPOOLPAGE pPage;
for (unsigned iLoop = 0; ; iLoop++)
{
uint16_t iToFree = pPool->iAgeTail;
if (iToFree == iUser && iUser != NIL_PGMPOOL_IDX)
iToFree = pPool->aPages[iToFree].iAgePrev;
/* This is the alternative to the SyncCR3 pgmPoolCacheUsed calls.
if (pPool->aPages[iToFree].iUserHead != NIL_PGMPOOL_USER_INDEX)
{
uint16_t i = pPool->aPages[iToFree].iAgePrev;
for (unsigned j = 0; j < 10 && i != NIL_PGMPOOL_USER_INDEX; j++, i = pPool->aPages[i].iAgePrev)
{
if (pPool->aPages[iToFree].iUserHead == NIL_PGMPOOL_USER_INDEX)
continue;
iToFree = i;
break;
}
}
*/
Assert(iToFree != iUser);
AssertReleaseMsg(iToFree != NIL_PGMPOOL_IDX,
("iToFree=%#x (iAgeTail=%#x) iUser=%#x iLoop=%u - pPool=%p LB %#zx\n",
iToFree, pPool->iAgeTail, iUser, iLoop, pPool,
RT_UOFFSETOF_DYN(PGMPOOL, aPages[pPool->cMaxPages])
+ pPool->cMaxUsers * sizeof(PGMPOOLUSER)
+ pPool->cMaxPhysExts * sizeof(PGMPOOLPHYSEXT) ));
pPage = &pPool->aPages[iToFree];
/*
* Reject any attempts at flushing the currently active shadow CR3 mapping.
* Call pgmPoolCacheUsed to move the page to the head of the age list.
*/
if ( !pgmPoolIsPageLocked(pPage)
&& pPage->idx >= PGMPOOL_IDX_FIRST /* paranoia (#6349) */)
break;
LogFlow(("pgmPoolCacheFreeOne: refuse CR3 mapping\n"));
pgmPoolCacheUsed(pPool, pPage);
AssertLogRelReturn(iLoop < 8192, VERR_PGM_POOL_TOO_MANY_LOOPS);
}
/*
* Found a usable page, flush it and return.
*/
int rc = pgmPoolFlushPage(pPool, pPage);
/* This flush was initiated by us and not the guest, so explicitly flush the TLB. */
/** @todo find out why this is necessary; pgmPoolFlushPage should trigger a flush if one is really needed. */
if (rc == VINF_SUCCESS)
PGM_INVL_ALL_VCPU_TLBS(pVM);
return rc;
}
/**
* Checks if a kind mismatch is really a page being reused
* or if it's just normal remappings.
*
* @returns true if reused and the cached page (enmKind1) should be flushed
* @returns false if not reused.
* @param enmKind1 The kind of the cached page.
* @param enmKind2 The kind of the requested page.
*/
static bool pgmPoolCacheReusedByKind(PGMPOOLKIND enmKind1, PGMPOOLKIND enmKind2)
{
switch (enmKind1)
{
/*
* Never reuse them. There is no remapping in non-paging mode.
*/
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT: /* never reuse them for other types */
return false;
/*
* It's perfectly fine to reuse these, except for PAE and non-paging stuff.
*/
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
switch (enmKind2)
{
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
return true;
default:
return false;
}
/*
* It's perfectly fine to reuse these, except for PAE and non-paging stuff.
*/
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
switch (enmKind2)
{
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
return true;
default:
return false;
}
/*
* These cannot be flushed, and it's common to reuse the PDs as PTs.
*/
case PGMPOOLKIND_ROOT_NESTED:
return false;
default:
AssertFatalMsgFailed(("enmKind1=%d\n", enmKind1));
}
}
/**
* Attempts to satisfy a pgmPoolAlloc request from the cache.
*
* @returns VBox status code.
* @retval VINF_PGM_CACHED_PAGE on success.
* @retval VERR_FILE_NOT_FOUND if not found.
* @param pPool The pool.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* @param enmKind The kind of mapping.
* @param enmAccess Access type for the mapping (only relevant for big pages)
* @param fA20Enabled Whether the CPU has the A20 gate enabled.
* @param iUser The shadow page pool index of the user table. This is
* NIL_PGMPOOL_IDX for root pages.
* @param iUserTable The index into the user table (shadowed). Ignored if
* root page
* @param ppPage Where to store the pointer to the page.
*/
static int pgmPoolCacheAlloc(PPGMPOOL pPool, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, bool fA20Enabled,
uint16_t iUser, uint32_t iUserTable, PPPGMPOOLPAGE ppPage)
{
/*
* Look up the GCPhys in the hash.
*/
unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
Log3(("pgmPoolCacheAlloc: %RGp kind %s iUser=%d iUserTable=%x SLOT=%d\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable, i));
if (i != NIL_PGMPOOL_IDX)
{
do
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
Log4(("pgmPoolCacheAlloc: slot %d found page %RGp\n", i, pPage->GCPhys));
if (pPage->GCPhys == GCPhys)
{
if ( (PGMPOOLKIND)pPage->enmKind == enmKind
&& (PGMPOOLACCESS)pPage->enmAccess == enmAccess
&& pPage->fA20Enabled == fA20Enabled)
{
/* Put it at the start of the use list to make sure pgmPoolTrackAddUser
* doesn't flush it in case there are no more free use records.
*/
pgmPoolCacheUsed(pPool, pPage);
int rc = VINF_SUCCESS;
if (iUser != NIL_PGMPOOL_IDX)
rc = pgmPoolTrackAddUser(pPool, pPage, iUser, iUserTable);
if (RT_SUCCESS(rc))
{
Assert((PGMPOOLKIND)pPage->enmKind == enmKind);
*ppPage = pPage;
if (pPage->cModifications)
pPage->cModifications = 1; /* reset counter (can't use 0, or else it will be reinserted in the modified list) */
STAM_COUNTER_INC(&pPool->StatCacheHits);
return VINF_PGM_CACHED_PAGE;
}
return rc;
}
if ((PGMPOOLKIND)pPage->enmKind != enmKind)
{
/*
* The kind is different. In some cases we should now flush the page
* as it has been reused, but in most cases this is normal remapping
* of PDs as PT or big pages using the GCPhys field in a slightly
* different way than the other kinds.
*/
if (pgmPoolCacheReusedByKind((PGMPOOLKIND)pPage->enmKind, enmKind))
{
STAM_COUNTER_INC(&pPool->StatCacheKindMismatches);
pgmPoolFlushPage(pPool, pPage);
break;
}
}
}
/* next */
i = pPage->iNext;
} while (i != NIL_PGMPOOL_IDX);
}
Log3(("pgmPoolCacheAlloc: Missed GCPhys=%RGp enmKind=%s\n", GCPhys, pgmPoolPoolKindToStr(enmKind)));
STAM_COUNTER_INC(&pPool->StatCacheMisses);
return VERR_FILE_NOT_FOUND;
}
/**
* Inserts a page into the cache.
*
* @param pPool The pool.
* @param pPage The cached page.
* @param fCanBeCached Set if the page is fit for caching from the caller's point of view.
*/
static void pgmPoolCacheInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fCanBeCached)
{
/*
* Insert into the GCPhys hash if the page is fit for that.
*/
Assert(!pPage->fCached);
if (fCanBeCached)
{
pPage->fCached = true;
pgmPoolHashInsert(pPool, pPage);
Log3(("pgmPoolCacheInsert: Caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
STAM_COUNTER_INC(&pPool->StatCacheCacheable);
}
else
{
Log3(("pgmPoolCacheInsert: Not caching %p:{.Core=%RHp, .idx=%d, .enmKind=%s, GCPhys=%RGp}\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
STAM_COUNTER_INC(&pPool->StatCacheUncacheable);
}
/*
* Insert at the head of the age list.
*/
pPage->iAgePrev = NIL_PGMPOOL_IDX;
pPage->iAgeNext = pPool->iAgeHead;
if (pPool->iAgeHead != NIL_PGMPOOL_IDX)
pPool->aPages[pPool->iAgeHead].iAgePrev = pPage->idx;
else
pPool->iAgeTail = pPage->idx;
pPool->iAgeHead = pPage->idx;
}
/**
* Flushes a cached page.
*
* @param pPool The pool.
* @param pPage The cached page.
*/
static void pgmPoolCacheFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolCacheFlushPage: %RGp\n", pPage->GCPhys));
/*
* Remove the page from the hash.
*/
if (pPage->fCached)
{
pPage->fCached = false;
pgmPoolHashRemove(pPool, pPage);
}
else
Assert(pPage->iNext == NIL_PGMPOOL_IDX);
/*
* Remove it from the age list.
*/
if (pPage->iAgeNext != NIL_PGMPOOL_IDX)
pPool->aPages[pPage->iAgeNext].iAgePrev = pPage->iAgePrev;
else
pPool->iAgeTail = pPage->iAgePrev;
if (pPage->iAgePrev != NIL_PGMPOOL_IDX)
pPool->aPages[pPage->iAgePrev].iAgeNext = pPage->iAgeNext;
else
pPool->iAgeHead = pPage->iAgeNext;
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
}
/**
* Looks for pages sharing the monitor.
*
* @returns Pointer to the head page.
* @returns NULL if not found.
* @param pPool The Pool
* @param pNewPage The page which is going to be monitored.
*/
static PPGMPOOLPAGE pgmPoolMonitorGetPageByGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pNewPage)
{
/*
* Look up the GCPhys in the hash.
*/
RTGCPHYS GCPhys = pNewPage->GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
if (i == NIL_PGMPOOL_IDX)
return NULL;
do
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
if ( pPage->GCPhys - GCPhys < PAGE_SIZE
&& pPage != pNewPage)
{
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
{
/* find the head */
while (pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
{
Assert(pPage->iMonitoredPrev != pPage->idx);
pPage = &pPool->aPages[pPage->iMonitoredPrev];
}
return pPage;
}
/* ignore, no monitoring. */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
break;
default:
AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
}
}
/* next */
i = pPage->iNext;
} while (i != NIL_PGMPOOL_IDX);
return NULL;
}
/**
* Enabled write monitoring of a guest page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @param pPool The pool.
* @param pPage The cached page.
*/
static int pgmPoolMonitorInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
LogFlow(("pgmPoolMonitorInsert %RGp\n", pPage->GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK));
/*
* Filter out the relevant kinds.
*/
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
break;
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
/* Nothing to monitor here. */
return VINF_SUCCESS;
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
/* Nothing to monitor here. */
return VINF_SUCCESS;
default:
AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
}
/*
* Install handler.
*/
int rc;
PPGMPOOLPAGE pPageHead = pgmPoolMonitorGetPageByGCPhys(pPool, pPage);
if (pPageHead)
{
Assert(pPageHead != pPage); Assert(pPageHead->iMonitoredNext != pPage->idx);
Assert(pPageHead->iMonitoredPrev != pPage->idx);
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPageHead->fDirty)
pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPageHead->idxDirtyEntry, false /* do not remove */);
#endif
pPage->iMonitoredPrev = pPageHead->idx;
pPage->iMonitoredNext = pPageHead->iMonitoredNext;
if (pPageHead->iMonitoredNext != NIL_PGMPOOL_IDX)
pPool->aPages[pPageHead->iMonitoredNext].iMonitoredPrev = pPage->idx;
pPageHead->iMonitoredNext = pPage->idx;
rc = VINF_SUCCESS;
}
else
{
Assert(pPage->iMonitoredNext == NIL_PGMPOOL_IDX); Assert(pPage->iMonitoredPrev == NIL_PGMPOOL_IDX);
PVMCC pVM = pPool->CTX_SUFF(pVM);
const RTGCPHYS GCPhysPage = pPage->GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
rc = PGMHandlerPhysicalRegister(pVM, GCPhysPage, GCPhysPage + PAGE_OFFSET_MASK, pPool->hAccessHandlerType,
MMHyperCCToR3(pVM, pPage), MMHyperCCToR0(pVM, pPage), MMHyperCCToRC(pVM, pPage),
NIL_RTR3PTR /*pszDesc*/);
/** @todo we should probably deal with out-of-memory conditions here, but for now increasing
* the heap size should suffice. */
AssertFatalMsgRC(rc, ("PGMHandlerPhysicalRegisterEx %RGp failed with %Rrc\n", GCPhysPage, rc));
PVMCPU pVCpu = VMMGetCpu(pVM);
AssertFatalMsg(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3), ("fSyncFlags=%x syncff=%d\n", pVCpu->pgm.s.fSyncFlags, VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3)));
}
pPage->fMonitored = true;
return rc;
}
/**
* Disables write monitoring of a guest page.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @param pPool The pool.
* @param pPage The cached page.
*/
static int pgmPoolMonitorFlush(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Filter out the relevant kinds.
*/
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
break;
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
/* Nothing to monitor here. */
Assert(!pPage->fMonitored);
return VINF_SUCCESS;
default:
AssertFatalMsgFailed(("This can't happen! enmKind=%d\n", pPage->enmKind));
}
Assert(pPage->fMonitored);
/*
* Remove the page from the monitored list or uninstall it if last.
*/
const PVMCC pVM = pPool->CTX_SUFF(pVM);
int rc;
if ( pPage->iMonitoredNext != NIL_PGMPOOL_IDX
|| pPage->iMonitoredPrev != NIL_PGMPOOL_IDX)
{
if (pPage->iMonitoredPrev == NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pNewHead = &pPool->aPages[pPage->iMonitoredNext];
pNewHead->iMonitoredPrev = NIL_PGMPOOL_IDX;
rc = PGMHandlerPhysicalChangeUserArgs(pVM, pPage->GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK,
MMHyperCCToR3(pVM, pNewHead), MMHyperCCToR0(pVM, pNewHead));
AssertFatalRCSuccess(rc);
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
}
else
{
pPool->aPages[pPage->iMonitoredPrev].iMonitoredNext = pPage->iMonitoredNext;
if (pPage->iMonitoredNext != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iMonitoredNext].iMonitoredPrev = pPage->iMonitoredPrev;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
}
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
rc = VINF_SUCCESS;
}
}
else
{
rc = PGMHandlerPhysicalDeregister(pVM, pPage->GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK);
AssertFatalRC(rc);
PVMCPU pVCpu = VMMGetCpu(pVM);
AssertFatalMsg(!(pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL) || VMCPU_FF_IS_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3),
("%#x %#x\n", pVCpu->pgm.s.fSyncFlags, pVM->fGlobalForcedActions));
}
pPage->fMonitored = false;
/*
* Remove it from the list of modified pages (if in it).
*/
pgmPoolMonitorModifiedRemove(pPool, pPage);
return rc;
}
/**
* Inserts the page into the list of modified pages.
*
* @param pPool The pool.
* @param pPage The page.
*/
void pgmPoolMonitorModifiedInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolMonitorModifiedInsert: idx=%d\n", pPage->idx));
AssertMsg( pPage->iModifiedNext == NIL_PGMPOOL_IDX
&& pPage->iModifiedPrev == NIL_PGMPOOL_IDX
&& pPool->iModifiedHead != pPage->idx,
("Next=%d Prev=%d idx=%d cModifications=%d Head=%d cModifiedPages=%d\n",
pPage->iModifiedNext, pPage->iModifiedPrev, pPage->idx, pPage->cModifications,
pPool->iModifiedHead, pPool->cModifiedPages));
pPage->iModifiedNext = pPool->iModifiedHead;
if (pPool->iModifiedHead != NIL_PGMPOOL_IDX)
pPool->aPages[pPool->iModifiedHead].iModifiedPrev = pPage->idx;
pPool->iModifiedHead = pPage->idx;
pPool->cModifiedPages++;
#ifdef VBOX_WITH_STATISTICS
if (pPool->cModifiedPages > pPool->cModifiedPagesHigh)
pPool->cModifiedPagesHigh = pPool->cModifiedPages;
#endif
}
/**
* Removes the page from the list of modified pages and resets the
* modification counter.
*
* @param pPool The pool.
* @param pPage The page which is believed to be in the list of modified pages.
*/
static void pgmPoolMonitorModifiedRemove(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
Log3(("pgmPoolMonitorModifiedRemove: idx=%d cModifications=%d\n", pPage->idx, pPage->cModifications));
if (pPool->iModifiedHead == pPage->idx)
{
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
pPool->iModifiedHead = pPage->iModifiedNext;
if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedNext].iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
}
pPool->cModifiedPages--;
}
else if (pPage->iModifiedPrev != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedPrev].iModifiedNext = pPage->iModifiedNext;
if (pPage->iModifiedNext != NIL_PGMPOOL_IDX)
{
pPool->aPages[pPage->iModifiedNext].iModifiedPrev = pPage->iModifiedPrev;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
}
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPool->cModifiedPages--;
}
else
Assert(pPage->iModifiedPrev == NIL_PGMPOOL_IDX);
pPage->cModifications = 0;
}
/**
* Zaps the list of modified pages, resetting their modification counters in the process.
*
* @param pVM The cross context VM structure.
*/
static void pgmPoolMonitorModifiedClearAll(PVMCC pVM)
{
pgmLock(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
LogFlow(("pgmPoolMonitorModifiedClearAll: cModifiedPages=%d\n", pPool->cModifiedPages));
unsigned cPages = 0; NOREF(cPages);
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
pgmPoolResetDirtyPages(pVM);
#endif
uint16_t idx = pPool->iModifiedHead;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
while (idx != NIL_PGMPOOL_IDX)
{
PPGMPOOLPAGE pPage = &pPool->aPages[idx];
idx = pPage->iModifiedNext;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cModifications = 0;
Assert(++cPages);
}
AssertMsg(cPages == pPool->cModifiedPages, ("%d != %d\n", cPages, pPool->cModifiedPages));
pPool->cModifiedPages = 0;
pgmUnlock(pVM);
}
/**
* Handle SyncCR3 pool tasks
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
* @retval VINF_PGM_SYNC_CR3 is it needs to be deferred to ring 3 (GC only)
* @param pVCpu The cross context virtual CPU structure.
* @remark Should only be used when monitoring is available, thus placed in
* the PGMPOOL_WITH_MONITORING \#ifdef.
*/
int pgmPoolSyncCR3(PVMCPUCC pVCpu)
{
PVMCC pVM = pVCpu->CTX_SUFF(pVM);
LogFlow(("pgmPoolSyncCR3 fSyncFlags=%x\n", pVCpu->pgm.s.fSyncFlags));
/*
* When monitoring shadowed pages, we reset the modification counters on CR3 sync.
* Occasionally we will have to clear all the shadow page tables because we wanted
* to monitor a page which was mapped by too many shadowed page tables. This operation
* sometimes referred to as a 'lightweight flush'.
*/
# ifdef IN_RING3 /* Don't flush in ring-0 or raw mode, it's taking too long. */
if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
pgmR3PoolClearAll(pVM, false /*fFlushRemTlb*/);
# else /* !IN_RING3 */
if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
{
Log(("SyncCR3: PGM_SYNC_CLEAR_PGM_POOL is set -> VINF_PGM_SYNC_CR3\n"));
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3); /** @todo no need to do global sync, right? */
/* Make sure all other VCPUs return to ring 3. */
if (pVM->cCpus > 1)
{
VM_FF_SET(pVM, VM_FF_PGM_POOL_FLUSH_PENDING);
PGM_INVL_ALL_VCPU_TLBS(pVM);
}
return VINF_PGM_SYNC_CR3;
}
# endif /* !IN_RING3 */
else
{
pgmPoolMonitorModifiedClearAll(pVM);
/* pgmPoolMonitorModifiedClearAll can cause a pgm pool flush (dirty page clearing), so make sure we handle this! */
if (pVCpu->pgm.s.fSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)
{
Log(("pgmPoolMonitorModifiedClearAll caused a pgm flush -> call pgmPoolSyncCR3 again!\n"));
return pgmPoolSyncCR3(pVCpu);
}
}
return VINF_SUCCESS;
}
/**
* Frees up at least one user entry.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
*
* @param pPool The pool.
* @param iUser The user index.
*/
static int pgmPoolTrackFreeOneUser(PPGMPOOL pPool, uint16_t iUser)
{
STAM_COUNTER_INC(&pPool->StatTrackFreeUpOneUser);
/*
* Just free cached pages in a braindead fashion.
*/
/** @todo walk the age list backwards and free the first with usage. */
int rc = VINF_SUCCESS;
do
{
int rc2 = pgmPoolCacheFreeOne(pPool, iUser);
if (RT_FAILURE(rc2) && rc == VINF_SUCCESS)
rc = rc2;
} while (pPool->iUserFreeHead == NIL_PGMPOOL_USER_INDEX);
return rc;
}
/**
* Inserts a page into the cache.
*
* This will create user node for the page, insert it into the GCPhys
* hash, and insert it into the age list.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
*
* @param pPool The pool.
* @param pPage The cached page.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* @param iUser The user index.
* @param iUserTable The user table index.
*/
DECLINLINE(int) pgmPoolTrackInsert(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTGCPHYS GCPhys, uint16_t iUser, uint32_t iUserTable)
{
int rc = VINF_SUCCESS;
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
LogFlow(("pgmPoolTrackInsert GCPhys=%RGp iUser=%d iUserTable=%x\n", GCPhys, iUser, iUserTable)); RT_NOREF_PV(GCPhys);
if (iUser != NIL_PGMPOOL_IDX)
{
#ifdef VBOX_STRICT
/*
* Check that the entry doesn't already exists.
*/
if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
{
uint16_t i = pPage->iUserHead;
do
{
Assert(i < pPool->cMaxUsers);
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
i = paUsers[i].iNext;
} while (i != NIL_PGMPOOL_USER_INDEX);
}
#endif
/*
* Find free a user node.
*/
uint16_t i = pPool->iUserFreeHead;
if (i == NIL_PGMPOOL_USER_INDEX)
{
rc = pgmPoolTrackFreeOneUser(pPool, iUser);
if (RT_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
/*
* Unlink the user node from the free list,
* initialize and insert it into the user list.
*/
pPool->iUserFreeHead = paUsers[i].iNext;
paUsers[i].iNext = NIL_PGMPOOL_USER_INDEX;
paUsers[i].iUser = iUser;
paUsers[i].iUserTable = iUserTable;
pPage->iUserHead = i;
}
else
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
/*
* Insert into cache and enable monitoring of the guest page if enabled.
*
* Until we implement caching of all levels, including the CR3 one, we'll
* have to make sure we don't try monitor & cache any recursive reuse of
* a monitored CR3 page. Because all windows versions are doing this we'll
* have to be able to do combined access monitoring, CR3 + PT and
* PD + PT (guest PAE).
*
* Update:
* We're now cooperating with the CR3 monitor if an uncachable page is found.
*/
const bool fCanBeMonitored = true;
pgmPoolCacheInsert(pPool, pPage, fCanBeMonitored); /* This can be expanded. */
if (fCanBeMonitored)
{
rc = pgmPoolMonitorInsert(pPool, pPage);
AssertRC(rc);
}
return rc;
}
/**
* Adds a user reference to a page.
*
* This will move the page to the head of the
*
* @returns VBox status code.
* @retval VINF_SUCCESS if successfully added.
*
* @param pPool The pool.
* @param pPage The cached page.
* @param iUser The user index.
* @param iUserTable The user table.
*/
static int pgmPoolTrackAddUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
Log3(("pgmPoolTrackAddUser: GCPhys=%RGp iUser=%x iUserTable=%x\n", pPage->GCPhys, iUser, iUserTable));
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
Assert(iUser != NIL_PGMPOOL_IDX);
# ifdef VBOX_STRICT
/*
* Check that the entry doesn't already exists. We only allow multiple
* users of top-level paging structures (SHW_POOL_ROOT_IDX).
*/
if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
{
uint16_t i = pPage->iUserHead;
do
{
Assert(i < pPool->cMaxUsers);
/** @todo this assertion looks odd... Shouldn't it be && here? */
AssertMsg(paUsers[i].iUser != iUser || paUsers[i].iUserTable != iUserTable, ("%x %x vs new %x %x\n", paUsers[i].iUser, paUsers[i].iUserTable, iUser, iUserTable));
i = paUsers[i].iNext;
} while (i != NIL_PGMPOOL_USER_INDEX);
}
# endif
/*
* Allocate a user node.
*/
uint16_t i = pPool->iUserFreeHead;
if (i == NIL_PGMPOOL_USER_INDEX)
{
int rc = pgmPoolTrackFreeOneUser(pPool, iUser);
if (RT_FAILURE(rc))
return rc;
i = pPool->iUserFreeHead;
}
pPool->iUserFreeHead = paUsers[i].iNext;
/*
* Initialize the user node and insert it.
*/
paUsers[i].iNext = pPage->iUserHead;
paUsers[i].iUser = iUser;
paUsers[i].iUserTable = iUserTable;
pPage->iUserHead = i;
# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
pgmPoolFlushDirtyPage(pPool->CTX_SUFF(pVM), pPool, pPage->idxDirtyEntry, false /* do not remove */);
# endif
/*
* Tell the cache to update its replacement stats for this page.
*/
pgmPoolCacheUsed(pPool, pPage);
return VINF_SUCCESS;
}
/**
* Frees a user record associated with a page.
*
* This does not clear the entry in the user table, it simply replaces the
* user record to the chain of free records.
*
* @param pPool The pool.
* @param pPage The shadow page.
* @param iUser The shadow page pool index of the user table.
* @param iUserTable The index into the user table (shadowed).
*
* @remarks Don't call this for root pages.
*/
static void pgmPoolTrackFreeUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
Log3(("pgmPoolTrackFreeUser %RGp %x %x\n", pPage->GCPhys, iUser, iUserTable));
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
Assert(iUser != NIL_PGMPOOL_IDX);
/*
* Unlink and free the specified user entry.
*/
/* Special: For PAE and 32-bit paging, there is usually no more than one user. */
uint16_t i = pPage->iUserHead;
if ( i != NIL_PGMPOOL_USER_INDEX
&& paUsers[i].iUser == iUser
&& paUsers[i].iUserTable == iUserTable)
{
pPage->iUserHead = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
return;
}
/* General: Linear search. */
uint16_t iPrev = NIL_PGMPOOL_USER_INDEX;
while (i != NIL_PGMPOOL_USER_INDEX)
{
if ( paUsers[i].iUser == iUser
&& paUsers[i].iUserTable == iUserTable)
{
if (iPrev != NIL_PGMPOOL_USER_INDEX)
paUsers[iPrev].iNext = paUsers[i].iNext;
else
pPage->iUserHead = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
return;
}
iPrev = i;
i = paUsers[i].iNext;
}
/* Fatal: didn't find it */
AssertFatalMsgFailed(("Didn't find the user entry! iUser=%d iUserTable=%#x GCPhys=%RGp\n",
iUser, iUserTable, pPage->GCPhys));
}
#if 0 /* unused */
/**
* Gets the entry size of a shadow table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
DECLINLINE(unsigned) pgmPoolTrackGetShadowEntrySize(PGMPOOLKIND enmKind)
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_32BIT_PD_PHYS:
return 4;
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
return 8;
default:
AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
}
}
#endif /* unused */
#if 0 /* unused */
/**
* Gets the entry size of a guest table.
*
* @param enmKind The kind of page.
*
* @returns The size of the entry in bytes. That is, 0, 4 or 8.
* @returns If the kind is not for a table, an assertion is raised and 0 is
* returned.
*/
DECLINLINE(unsigned) pgmPoolTrackGetGuestEntrySize(PGMPOOLKIND enmKind)
{
switch (enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
return 4;
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_PAE_PDPT:
return 8;
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
/** @todo can we return 0? (nobody is calling this...) */
AssertFailed();
return 0;
default:
AssertFatalMsgFailed(("enmKind=%d\n", enmKind));
}
}
#endif /* unused */
/**
* Checks one shadow page table entry for a mapping of a physical page.
*
* @returns true / false indicating removal of all relevant PTEs
*
* @param pVM The cross context VM structure.
* @param pPhysPage The guest page in question.
* @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
* @param iShw The shadow page table.
* @param iPte Page table entry or NIL_PGMPOOL_PHYSEXT_IDX_PTE if unknown
*/
static bool pgmPoolTrackFlushGCPhysPTInt(PVM pVM, PCPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw, uint16_t iPte)
{
LogFlow(("pgmPoolTrackFlushGCPhysPTInt: pPhysPage=%RHp iShw=%d iPte=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw, iPte));
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
bool fRet = false;
/*
* Assert sanity.
*/
Assert(iPte != NIL_PGMPOOL_PHYSEXT_IDX_PTE);
AssertFatalMsg(iShw < pPool->cCurPages && iShw != NIL_PGMPOOL_IDX, ("iShw=%d\n", iShw));
PPGMPOOLPAGE pPage = &pPool->aPages[iShw];
/*
* Then, clear the actual mappings to the page in the shadow PT.
*/
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
{
const uint32_t u32 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
uint32_t u32AndMask = 0;
uint32_t u32OrMask = 0;
if (!fFlushPTEs)
{
switch (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage))
{
case PGM_PAGE_HNDL_PHYS_STATE_NONE: /* No handler installed. */
case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /* Monitoring is temporarily disabled. */
u32OrMask = X86_PTE_RW;
u32AndMask = UINT32_MAX;
fRet = true;
STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
break;
case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /* Write access is monitored. */
u32OrMask = 0;
u32AndMask = ~X86_PTE_RW;
fRet = true;
STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
break;
default:
/* (shouldn't be here, will assert below) */
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
break;
}
}
else
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
/* Update the counter if we're removing references. */
if (!u32AndMask)
{
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
}
if ((pPT->a[iPte].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
X86PTE Pte;
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX32\n", iPte, pPT->a[iPte]));
Pte.u = (pPT->a[iPte].u & u32AndMask) | u32OrMask;
if (Pte.u & PGM_PTFLAGS_TRACK_DIRTY)
Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
ASMAtomicWriteU32(&pPT->a[iPte].u, Pte.u);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);
return fRet;
}
#ifdef LOG_ENABLED
Log(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
for (unsigned i = 0, cFound = 0; i < RT_ELEMENTS(pPT->a); i++)
if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
Log(("i=%d cFound=%d\n", i, ++cFound));
}
#endif
AssertFatalMsgFailed(("iFirstPresent=%d cPresent=%d u32=%RX32 poolkind=%x\n", pPage->iFirstPresent, pPage->cPresent, u32, pPage->enmKind));
/*PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);*/
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS: /* physical mask the same as PAE; RW bit as well; be careful! */
{
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P;
PPGMSHWPTPAE pPT = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
uint64_t u64OrMask = 0;
uint64_t u64AndMask = 0;
if (!fFlushPTEs)
{
switch (PGM_PAGE_GET_HNDL_PHYS_STATE(pPhysPage))
{
case PGM_PAGE_HNDL_PHYS_STATE_NONE: /* No handler installed. */
case PGM_PAGE_HNDL_PHYS_STATE_DISABLED: /* Monitoring is temporarily disabled. */
u64OrMask = X86_PTE_RW;
u64AndMask = UINT64_MAX;
fRet = true;
STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
break;
case PGM_PAGE_HNDL_PHYS_STATE_WRITE: /* Write access is monitored. */
u64OrMask = 0;
u64AndMask = ~(uint64_t)X86_PTE_RW;
fRet = true;
STAM_COUNTER_INC(&pPool->StatTrackFlushEntryKeep);
break;
default:
/* (shouldn't be here, will assert below) */
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
break;
}
}
else
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
/* Update the counter if we're removing references. */
if (!u64AndMask)
{
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
}
if ((PGMSHWPTEPAE_GET_U(pPT->a[iPte]) & (X86_PTE_PAE_PG_MASK | X86_PTE_P | X86_PTE_PAE_MBZ_MASK_NX)) == u64)
{
X86PTEPAE Pte;
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pte=%RX64\n", iPte, PGMSHWPTEPAE_GET_LOG(pPT->a[iPte])));
Pte.u = (PGMSHWPTEPAE_GET_U(pPT->a[iPte]) & u64AndMask) | u64OrMask;
if (Pte.u & PGM_PTFLAGS_TRACK_DIRTY)
Pte.n.u1Write = 0; /* need to disallow writes when dirty bit tracking is still active. */
PGMSHWPTEPAE_ATOMIC_SET(pPT->a[iPte], Pte.u);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);
return fRet;
}
#ifdef LOG_ENABLED
Log(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
Log(("Found %RX64 expected %RX64\n", PGMSHWPTEPAE_GET_U(pPT->a[iPte]) & (X86_PTE_PAE_PG_MASK | X86_PTE_P | X86_PTE_PAE_MBZ_MASK_NX), u64));
for (unsigned i = 0, cFound = 0; i < RT_ELEMENTS(pPT->a); i++)
if ((PGMSHWPTEPAE_GET_U(pPT->a[i]) & (X86_PTE_PAE_PG_MASK | X86_PTE_P | X86_PTE_PAE_MBZ_MASK_NX)) == u64)
Log(("i=%d cFound=%d\n", i, ++cFound));
#endif
AssertFatalMsgFailed(("iFirstPresent=%d cPresent=%d u64=%RX64 poolkind=%x iPte=%d PT=%RX64\n", pPage->iFirstPresent, pPage->cPresent, u64, pPage->enmKind, iPte, PGMSHWPTEPAE_GET_LOG(pPT->a[iPte])));
/*PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);*/
break;
}
#ifdef PGM_WITH_LARGE_PAGES
/* Large page case only. */
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
{
Assert(pVM->pgm.s.fNestedPaging);
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PDE4M_P | X86_PDE4M_PS;
PEPTPD pPD = (PEPTPD)PGMPOOL_PAGE_2_PTR(pVM, pPage);
if ((pPD->a[iPte].u & (EPT_PDE2M_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
{
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pde=%RX64\n", iPte, pPD->a[iPte]));
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
pPD->a[iPte].u = 0;
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPD);
/* Update the counter as we're removing references. */
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
return fRet;
}
# ifdef LOG_ENABLED
Log(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
for (unsigned i = 0, cFound = 0; i < RT_ELEMENTS(pPD->a); i++)
if ((pPD->a[i].u & (EPT_PDE2M_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
Log(("i=%d cFound=%d\n", i, ++cFound));
# endif
AssertFatalMsgFailed(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
/*PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPD);*/
break;
}
/* AMD-V nested paging */ /** @todo merge with EPT as we only check the parts that are identical. */
case PGMPOOLKIND_PAE_PD_PHYS:
{
Assert(pVM->pgm.s.fNestedPaging);
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PDE4M_P | X86_PDE4M_PS;
PX86PD pPD = (PX86PD)PGMPOOL_PAGE_2_PTR(pVM, pPage);
if ((pPD->a[iPte].u & (X86_PDE2M_PAE_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
{
Log4(("pgmPoolTrackFlushGCPhysPTs: i=%d pde=%RX64\n", iPte, pPD->a[iPte]));
STAM_COUNTER_INC(&pPool->StatTrackFlushEntry);
pPD->a[iPte].u = 0;
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPD);
/* Update the counter as we're removing references. */
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
return fRet;
}
# ifdef LOG_ENABLED
Log(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
for (unsigned i = 0, cFound = 0; i < RT_ELEMENTS(pPD->a); i++)
if ((pPD->a[i].u & (X86_PDE2M_PAE_PG_MASK | X86_PDE4M_P | X86_PDE4M_PS)) == u64)
Log(("i=%d cFound=%d\n", i, ++cFound));
# endif
AssertFatalMsgFailed(("iFirstPresent=%d cPresent=%d\n", pPage->iFirstPresent, pPage->cPresent));
/*PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPD);*/
break;
}
#endif /* PGM_WITH_LARGE_PAGES */
default:
AssertFatalMsgFailed(("enmKind=%d iShw=%d\n", pPage->enmKind, iShw));
}
/* not reached. */
#ifndef _MSC_VER
return fRet;
#endif
}
/**
* Scans one shadow page table for mappings of a physical page.
*
* @param pVM The cross context VM structure.
* @param pPhysPage The guest page in question.
* @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
* @param iShw The shadow page table.
*/
static void pgmPoolTrackFlushGCPhysPT(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iShw)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool); NOREF(pPool);
/* We should only come here with when there's only one reference to this physical page. */
Assert(PGMPOOL_TD_GET_CREFS(PGM_PAGE_GET_TRACKING(pPhysPage)) == 1);
Log2(("pgmPoolTrackFlushGCPhysPT: pPhysPage=%RHp iShw=%d\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iShw));
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPT, f);
bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, iShw, PGM_PAGE_GET_PTE_INDEX(pPhysPage));
if (!fKeptPTEs)
PGM_PAGE_SET_TRACKING(pVM, pPhysPage, 0);
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPT, f);
}
/**
* Flushes a list of shadow page tables mapping the same physical page.
*
* @param pVM The cross context VM structure.
* @param pPhysPage The guest page in question.
* @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
* @param iPhysExt The physical cross reference extent list to flush.
*/
static void pgmPoolTrackFlushGCPhysPTs(PVM pVM, PPGMPAGE pPhysPage, bool fFlushPTEs, uint16_t iPhysExt)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
bool fKeepList = false;
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTs, f);
Log2(("pgmPoolTrackFlushGCPhysPTs: pPhysPage=%RHp iPhysExt=%u\n", PGM_PAGE_GET_HCPHYS(pPhysPage), iPhysExt));
const uint16_t iPhysExtStart = iPhysExt;
PPGMPOOLPHYSEXT pPhysExt;
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
{
if (pPhysExt->aidx[i] != NIL_PGMPOOL_IDX)
{
bool fKeptPTEs = pgmPoolTrackFlushGCPhysPTInt(pVM, pPhysPage, fFlushPTEs, pPhysExt->aidx[i], pPhysExt->apte[i]);
if (!fKeptPTEs)
{
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
pPhysExt->apte[i] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
}
else
fKeepList = true;
}
}
/* next */
iPhysExt = pPhysExt->iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
if (!fKeepList)
{
/* insert the list into the free list and clear the ram range entry. */
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExtStart;
/* Invalidate the tracking data. */
PGM_PAGE_SET_TRACKING(pVM, pPhysPage, 0);
}
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTs, f);
}
/**
* Flushes all shadow page table mappings of the given guest page.
*
* This is typically called when the host page backing the guest one has been
* replaced or when the page protection was changed due to a guest access
* caught by the monitoring.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if all references has been successfully cleared.
* @retval VINF_PGM_SYNC_CR3 if we're better off with a CR3 sync and a page
* pool cleaning. FF and sync flags are set.
*
* @param pVM The cross context VM structure.
* @param GCPhysPage GC physical address of the page in question
* @param pPhysPage The guest page in question.
* @param fFlushPTEs Flush PTEs or allow them to be updated (e.g. in case of an RW bit change)
* @param pfFlushTLBs This is set to @a true if the shadow TLBs should be
* flushed, it is NOT touched if this isn't necessary.
* The caller MUST initialized this to @a false.
*/
int pgmPoolTrackUpdateGCPhys(PVMCC pVM, RTGCPHYS GCPhysPage, PPGMPAGE pPhysPage, bool fFlushPTEs, bool *pfFlushTLBs)
{
PVMCPUCC pVCpu = VMMGetCpu(pVM);
pgmLock(pVM);
int rc = VINF_SUCCESS;
#ifdef PGM_WITH_LARGE_PAGES
/* Is this page part of a large page? */
if (PGM_PAGE_GET_PDE_TYPE(pPhysPage) == PGM_PAGE_PDE_TYPE_PDE)
{
RTGCPHYS GCPhysBase = GCPhysPage & X86_PDE2M_PAE_PG_MASK;
GCPhysPage &= X86_PDE_PAE_PG_MASK;
/* Fetch the large page base. */
PPGMPAGE pLargePage;
if (GCPhysBase != GCPhysPage)
{
pLargePage = pgmPhysGetPage(pVM, GCPhysBase);
AssertFatal(pLargePage);
}
else
pLargePage = pPhysPage;
Log(("pgmPoolTrackUpdateGCPhys: update large page PDE for %RGp (%RGp)\n", GCPhysBase, GCPhysPage));
if (PGM_PAGE_GET_PDE_TYPE(pLargePage) == PGM_PAGE_PDE_TYPE_PDE)
{
/* Mark the large page as disabled as we need to break it up to change a single page in the 2 MB range. */
PGM_PAGE_SET_PDE_TYPE(pVM, pLargePage, PGM_PAGE_PDE_TYPE_PDE_DISABLED);
pVM->pgm.s.cLargePagesDisabled++;
/* Update the base as that *only* that one has a reference and there's only one PDE to clear. */
rc = pgmPoolTrackUpdateGCPhys(pVM, GCPhysBase, pLargePage, fFlushPTEs, pfFlushTLBs);
*pfFlushTLBs = true;
pgmUnlock(pVM);
return rc;
}
}
#else
NOREF(GCPhysPage);
#endif /* PGM_WITH_LARGE_PAGES */
const uint16_t u16 = PGM_PAGE_GET_TRACKING(pPhysPage);
if (u16)
{
/*
* The zero page is currently screwing up the tracking and we'll
* have to flush the whole shebang. Unless VBOX_WITH_NEW_LAZY_PAGE_ALLOC
* is defined, zero pages won't normally be mapped. Some kind of solution
* will be needed for this problem of course, but it will have to wait...
*/
if ( PGM_PAGE_IS_ZERO(pPhysPage)
|| PGM_PAGE_IS_BALLOONED(pPhysPage))
rc = VINF_PGM_GCPHYS_ALIASED;
else
{
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0 /** @todo we can drop this now. */
/* Start a subset here because pgmPoolTrackFlushGCPhysPTsSlow and
pgmPoolTrackFlushGCPhysPTs will/may kill the pool otherwise. */
uint32_t iPrevSubset = PGMRZDynMapPushAutoSubset(pVCpu);
# endif
if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
{
Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
pgmPoolTrackFlushGCPhysPT(pVM,
pPhysPage,
fFlushPTEs,
PGMPOOL_TD_GET_IDX(u16));
}
else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
pgmPoolTrackFlushGCPhysPTs(pVM, pPhysPage, fFlushPTEs, PGMPOOL_TD_GET_IDX(u16));
else
rc = pgmPoolTrackFlushGCPhysPTsSlow(pVM, pPhysPage);
*pfFlushTLBs = true;
# ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
PGMRZDynMapPopAutoSubset(pVCpu, iPrevSubset);
# endif
}
}
if (rc == VINF_PGM_GCPHYS_ALIASED)
{
pVCpu->pgm.s.fSyncFlags |= PGM_SYNC_CLEAR_PGM_POOL;
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
rc = VINF_PGM_SYNC_CR3;
}
pgmUnlock(pVM);
return rc;
}
/**
* Scans all shadow page tables for mappings of a physical page.
*
* This may be slow, but it's most likely more efficient than cleaning
* out the entire page pool / cache.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if all references has been successfully cleared.
* @retval VINF_PGM_GCPHYS_ALIASED if we're better off with a CR3 sync and
* a page pool cleaning.
*
* @param pVM The cross context VM structure.
* @param pPhysPage The guest page in question.
*/
int pgmPoolTrackFlushGCPhysPTsSlow(PVMCC pVM, PPGMPAGE pPhysPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
STAM_PROFILE_START(&pPool->StatTrackFlushGCPhysPTsSlow, s);
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: cUsedPages=%d cPresent=%d pPhysPage=%R[pgmpage]\n",
pPool->cUsedPages, pPool->cPresent, pPhysPage));
/*
* There is a limit to what makes sense.
*/
if ( pPool->cPresent > 1024
&& pVM->cCpus == 1)
{
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
return VINF_PGM_GCPHYS_ALIASED;
}
/*
* Iterate all the pages until we've encountered all that in use.
* This is simple but not quite optimal solution.
*/
const uint64_t u64 = PGM_PAGE_GET_HCPHYS(pPhysPage) | X86_PTE_P; /** @todo drop X86_PTE_P here as we always test if present separately, anyway. */
const uint32_t u32 = u64; /** @todo move into the 32BIT_PT_xx case */
unsigned cLeft = pPool->cUsedPages;
unsigned iPage = pPool->cCurPages;
while (--iPage >= PGMPOOL_IDX_FIRST)
{
PPGMPOOLPAGE pPage = &pPool->aPages[iPage];
if ( pPage->GCPhys != NIL_RTGCPHYS
&& pPage->cPresent)
{
switch (pPage->enmKind)
{
/*
* We only care about shadow page tables.
*/
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
{
unsigned cPresent = pPage->cPresent;
PX86PT pPT = (PX86PT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
if (pPT->a[i].n.u1Present)
{
if ((pPT->a[i].u & (X86_PTE_PG_MASK | X86_PTE_P)) == u32)
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX32\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
/* Update the counter as we're removing references. */
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
}
if (!--cPresent)
break;
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
{
unsigned cPresent = pPage->cPresent;
PPGMSHWPTPAE pPT = (PPGMSHWPTPAE)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
if (PGMSHWPTEPAE_IS_P(pPT->a[i]))
{
if ((PGMSHWPTEPAE_GET_U(pPT->a[i]) & (X86_PTE_PAE_PG_MASK | X86_PTE_P)) == u64)
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
PGMSHWPTEPAE_SET(pPT->a[i], 0); /// @todo why not atomic?
/* Update the counter as we're removing references. */
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
}
if (!--cPresent)
break;
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);
break;
}
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
{
unsigned cPresent = pPage->cPresent;
PEPTPT pPT = (PEPTPT)PGMPOOL_PAGE_2_PTR(pVM, pPage);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pPT->a); i++)
if (pPT->a[i].n.u1Present)
{
if ((pPT->a[i].u & (EPT_PTE_PG_MASK | X86_PTE_P)) == u64)
{
//Log4(("pgmPoolTrackFlushGCPhysPTsSlow: idx=%d i=%d pte=%RX64\n", iPage, i, pPT->a[i]));
pPT->a[i].u = 0;
/* Update the counter as we're removing references. */
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
}
if (!--cPresent)
break;
}
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pPT);
break;
}
}
if (!--cLeft)
break;
}
}
PGM_PAGE_SET_TRACKING(pVM, pPhysPage, 0);
STAM_PROFILE_STOP(&pPool->StatTrackFlushGCPhysPTsSlow, s);
/*
* There is a limit to what makes sense. The above search is very expensive, so force a pgm pool flush.
*/
if (pPool->cPresent > 1024)
{
LogFlow(("pgmPoolTrackFlushGCPhysPTsSlow: giving up... (cPresent=%d)\n", pPool->cPresent));
return VINF_PGM_GCPHYS_ALIASED;
}
return VINF_SUCCESS;
}
/**
* Clears the user entry in a user table.
*
* This is used to remove all references to a page when flushing it.
*/
static void pgmPoolTrackClearPageUser(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PCPGMPOOLUSER pUser)
{
Assert(pUser->iUser != NIL_PGMPOOL_IDX);
Assert(pUser->iUser < pPool->cCurPages);
uint32_t iUserTable = pUser->iUserTable;
/*
* Map the user page. Ignore references made by fictitious pages.
*/
PPGMPOOLPAGE pUserPage = &pPool->aPages[pUser->iUser];
LogFlow(("pgmPoolTrackClearPageUser: clear %x in %s (%RGp) (flushing %s)\n", iUserTable, pgmPoolPoolKindToStr(pUserPage->enmKind), pUserPage->Core.Key, pgmPoolPoolKindToStr(pPage->enmKind)));
union
{
uint64_t *pau64;
uint32_t *pau32;
} u;
if (pUserPage->idx < PGMPOOL_IDX_FIRST)
{
Assert(!pUserPage->pvPageR3);
return;
}
u.pau64 = (uint64_t *)PGMPOOL_PAGE_2_PTR(pPool->CTX_SUFF(pVM), pUserPage);
/* Safety precaution in case we change the paging for other modes too in the future. */
Assert(!pgmPoolIsPageLocked(pPage)); RT_NOREF_PV(pPage);
#ifdef VBOX_STRICT
/*
* Some sanity checks.
*/
switch (pUserPage->enmKind)
{
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_32BIT_PD_PHYS:
Assert(iUserTable < X86_PG_ENTRIES);
break;
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
case PGMPOOLKIND_PAE_PDPT_PHYS:
Assert(iUserTable < 4);
Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
break;
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PD_PHYS:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
break;
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
Assert(!(u.pau64[iUserTable] & PGM_PDFLAGS_MAPPING));
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
break;
case PGMPOOLKIND_64BIT_PML4:
Assert(!(u.pau64[iUserTable] & PGM_PLXFLAGS_PERMANENT));
/* GCPhys >> PAGE_SHIFT is the index here */
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
break;
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
break;
case PGMPOOLKIND_ROOT_NESTED:
Assert(iUserTable < X86_PG_PAE_ENTRIES);
break;
default:
AssertMsgFailed(("enmKind=%d\n", pUserPage->enmKind));
break;
}
#endif /* VBOX_STRICT */
/*
* Clear the entry in the user page.
*/
switch (pUserPage->enmKind)
{
/* 32-bit entries */
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_32BIT_PD_PHYS:
ASMAtomicWriteU32(&u.pau32[iUserTable], 0);
break;
/* 64-bit entries */
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
ASMAtomicWriteU64(&u.pau64[iUserTable], 0);
break;
default:
AssertFatalMsgFailed(("enmKind=%d iUser=%d iUserTable=%#x\n", pUserPage->enmKind, pUser->iUser, pUser->iUserTable));
}
PGM_DYNMAP_UNUSED_HINT_VM(pPool->CTX_SUFF(pVM), u.pau64);
}
/**
* Clears all users of a page.
*/
static void pgmPoolTrackClearPageUsers(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Free all the user records.
*/
LogFlow(("pgmPoolTrackClearPageUsers %RGp\n", pPage->GCPhys));
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
uint16_t i = pPage->iUserHead;
while (i != NIL_PGMPOOL_USER_INDEX)
{
/* Clear enter in user table. */
pgmPoolTrackClearPageUser(pPool, pPage, &paUsers[i]);
/* Free it. */
const uint16_t iNext = paUsers[i].iNext;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iNext = pPool->iUserFreeHead;
pPool->iUserFreeHead = i;
/* Next. */
i = iNext;
}
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
}
/**
* Allocates a new physical cross reference extent.
*
* @returns Pointer to the allocated extent on success. NULL if we're out of them.
* @param pVM The cross context VM structure.
* @param piPhysExt Where to store the phys ext index.
*/
PPGMPOOLPHYSEXT pgmPoolTrackPhysExtAlloc(PVM pVM, uint16_t *piPhysExt)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
uint16_t iPhysExt = pPool->iPhysExtFreeHead;
if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
{
STAM_COUNTER_INC(&pPool->StamTrackPhysExtAllocFailures);
return NULL;
}
PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
pPool->iPhysExtFreeHead = pPhysExt->iNext;
pPhysExt->iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
*piPhysExt = iPhysExt;
return pPhysExt;
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The cross context VM structure.
* @param iPhysExt The extent to free.
*/
void pgmPoolTrackPhysExtFree(PVM pVM, uint16_t iPhysExt)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
Assert(iPhysExt < pPool->cMaxPhysExts);
PPGMPOOLPHYSEXT pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
{
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
pPhysExt->apte[i] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
}
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExt;
}
/**
* Frees a physical cross reference extent.
*
* @param pVM The cross context VM structure.
* @param iPhysExt The extent to free.
*/
void pgmPoolTrackPhysExtFreeList(PVM pVM, uint16_t iPhysExt)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
const uint16_t iPhysExtStart = iPhysExt;
PPGMPOOLPHYSEXT pPhysExt;
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
pPhysExt = &pPool->CTX_SUFF(paPhysExts)[iPhysExt];
for (unsigned i = 0; i < RT_ELEMENTS(pPhysExt->aidx); i++)
{
pPhysExt->aidx[i] = NIL_PGMPOOL_IDX;
pPhysExt->apte[i] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
}
/* next */
iPhysExt = pPhysExt->iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
pPhysExt->iNext = pPool->iPhysExtFreeHead;
pPool->iPhysExtFreeHead = iPhysExtStart;
}
/**
* Insert a reference into a list of physical cross reference extents.
*
* @returns The new tracking data for PGMPAGE.
*
* @param pVM The cross context VM structure.
* @param iPhysExt The physical extent index of the list head.
* @param iShwPT The shadow page table index.
* @param iPte Page table entry
*
*/
static uint16_t pgmPoolTrackPhysExtInsert(PVM pVM, uint16_t iPhysExt, uint16_t iShwPT, uint16_t iPte)
{
PGM_LOCK_ASSERT_OWNER(pVM);
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
/*
* Special common cases.
*/
if (paPhysExts[iPhysExt].aidx[1] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[1] = iShwPT;
paPhysExts[iPhysExt].apte[1] = iPte;
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtInsert: %d:{,%d pte %d,}\n", iPhysExt, iShwPT, iPte));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
if (paPhysExts[iPhysExt].aidx[2] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[2] = iShwPT;
paPhysExts[iPhysExt].apte[2] = iPte;
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtInsert: %d:{,,%d pte %d}\n", iPhysExt, iShwPT, iPte));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
AssertCompile(RT_ELEMENTS(paPhysExts[iPhysExt].aidx) == 3);
/*
* General treatment.
*/
const uint16_t iPhysExtStart = iPhysExt;
unsigned cMax = 15;
for (;;)
{
Assert(iPhysExt < pPool->cMaxPhysExts);
for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] == NIL_PGMPOOL_IDX)
{
paPhysExts[iPhysExt].aidx[i] = iShwPT;
paPhysExts[iPhysExt].apte[i] = iPte;
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackAliasedMany);
LogFlow(("pgmPoolTrackPhysExtInsert: %d:{%d pte %d} i=%d cMax=%d\n", iPhysExt, iShwPT, iPte, i, cMax));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtStart);
}
if (!--cMax)
{
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackOverflows);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
LogFlow(("pgmPoolTrackPhysExtInsert: overflow (1) iShwPT=%d\n", iShwPT));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
/* advance */
iPhysExt = paPhysExts[iPhysExt].iNext;
if (iPhysExt == NIL_PGMPOOL_PHYSEXT_INDEX)
break;
}
/*
* Add another extent to the list.
*/
PPGMPOOLPHYSEXT pNew = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (!pNew)
{
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackNoExtentsLeft);
pgmPoolTrackPhysExtFreeList(pVM, iPhysExtStart);
LogFlow(("pgmPoolTrackPhysExtInsert: pgmPoolTrackPhysExtAlloc failed iShwPT=%d\n", iShwPT));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
pNew->iNext = iPhysExtStart;
pNew->aidx[0] = iShwPT;
pNew->apte[0] = iPte;
LogFlow(("pgmPoolTrackPhysExtInsert: added new extent %d:{%d pte %d}->%d\n", iPhysExt, iShwPT, iPte, iPhysExtStart));
return PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
/**
* Add a reference to guest physical page where extents are in use.
*
* @returns The new tracking data for PGMPAGE.
*
* @param pVM The cross context VM structure.
* @param pPhysPage Pointer to the aPages entry in the ram range.
* @param u16 The ram range flags (top 16-bits).
* @param iShwPT The shadow page table index.
* @param iPte Page table entry
*/
uint16_t pgmPoolTrackPhysExtAddref(PVMCC pVM, PPGMPAGE pPhysPage, uint16_t u16, uint16_t iShwPT, uint16_t iPte)
{
pgmLock(pVM);
if (PGMPOOL_TD_GET_CREFS(u16) != PGMPOOL_TD_CREFS_PHYSEXT)
{
/*
* Convert to extent list.
*/
Assert(PGMPOOL_TD_GET_CREFS(u16) == 1);
uint16_t iPhysExt;
PPGMPOOLPHYSEXT pPhysExt = pgmPoolTrackPhysExtAlloc(pVM, &iPhysExt);
if (pPhysExt)
{
LogFlow(("pgmPoolTrackPhysExtAddref: new extent: %d:{%d, %d}\n", iPhysExt, PGMPOOL_TD_GET_IDX(u16), iShwPT));
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackAliased);
pPhysExt->aidx[0] = PGMPOOL_TD_GET_IDX(u16);
pPhysExt->apte[0] = PGM_PAGE_GET_PTE_INDEX(pPhysPage);
pPhysExt->aidx[1] = iShwPT;
pPhysExt->apte[1] = iPte;
u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExt);
}
else
u16 = PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED);
}
else if (u16 != PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, PGMPOOL_TD_IDX_OVERFLOWED))
{
/*
* Insert into the extent list.
*/
u16 = pgmPoolTrackPhysExtInsert(pVM, PGMPOOL_TD_GET_IDX(u16), iShwPT, iPte);
}
else
STAM_COUNTER_INC(&pVM->pgm.s.CTX_SUFF(pStats)->StatTrackAliasedLots);
pgmUnlock(pVM);
return u16;
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pPhysPage Pointer to the aPages entry in the ram range.
* @param iPte Shadow PTE index
*/
void pgmPoolTrackPhysExtDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMPAGE pPhysPage, uint16_t iPte)
{
PVMCC pVM = pPool->CTX_SUFF(pVM);
const unsigned cRefs = PGM_PAGE_GET_TD_CREFS(pPhysPage);
AssertFatalMsg(cRefs == PGMPOOL_TD_CREFS_PHYSEXT, ("cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
uint16_t iPhysExt = PGM_PAGE_GET_TD_IDX(pPhysPage);
if (iPhysExt != PGMPOOL_TD_IDX_OVERFLOWED)
{
pgmLock(pVM);
uint16_t iPhysExtPrev = NIL_PGMPOOL_PHYSEXT_INDEX;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
do
{
Assert(iPhysExt < pPool->cMaxPhysExts);
/*
* Look for the shadow page and check if it's all freed.
*/
for (unsigned i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
{
if ( paPhysExts[iPhysExt].aidx[i] == pPage->idx
&& paPhysExts[iPhysExt].apte[i] == iPte)
{
paPhysExts[iPhysExt].aidx[i] = NIL_PGMPOOL_IDX;
paPhysExts[iPhysExt].apte[i] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
for (i = 0; i < RT_ELEMENTS(paPhysExts[iPhysExt].aidx); i++)
if (paPhysExts[iPhysExt].aidx[i] != NIL_PGMPOOL_IDX)
{
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d\n", pPhysPage, pPage->idx));
pgmUnlock(pVM);
return;
}
/* we can free the node. */
const uint16_t iPhysExtNext = paPhysExts[iPhysExt].iNext;
if ( iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX
&& iPhysExtNext == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* lonely node */
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d lonely\n", pPhysPage, pPage->idx));
PGM_PAGE_SET_TRACKING(pVM, pPhysPage, 0);
}
else if (iPhysExtPrev == NIL_PGMPOOL_PHYSEXT_INDEX)
{
/* head */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d head\n", pPhysPage, pPage->idx));
PGM_PAGE_SET_TRACKING(pVM, pPhysPage, PGMPOOL_TD_MAKE(PGMPOOL_TD_CREFS_PHYSEXT, iPhysExtNext));
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
else
{
/* in list */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage] idx=%d in list\n", pPhysPage, pPage->idx));
paPhysExts[iPhysExtPrev].iNext = iPhysExtNext;
pgmPoolTrackPhysExtFree(pVM, iPhysExt);
}
iPhysExt = iPhysExtNext;
pgmUnlock(pVM);
return;
}
}
/* next */
iPhysExtPrev = iPhysExt;
iPhysExt = paPhysExts[iPhysExt].iNext;
} while (iPhysExt != NIL_PGMPOOL_PHYSEXT_INDEX);
pgmUnlock(pVM);
AssertFatalMsgFailed(("not-found! cRefs=%d pPhysPage=%R[pgmpage] pPage=%p:{.idx=%d}\n", cRefs, pPhysPage, pPage, pPage->idx));
}
else /* nothing to do */
Log2(("pgmPoolTrackPhysExtDerefGCPhys: pPhysPage=%R[pgmpage]\n", pPhysPage));
}
/**
* Clear references to guest physical memory.
*
* This is the same as pgmPoolTracDerefGCPhysHint except that the guest
* physical address is assumed to be correct, so the linear search can be
* skipped and we can assert at an earlier point.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhys The guest physical address corresponding to HCPhys.
* @param iPte Shadow PTE index
*/
static void pgmPoolTracDerefGCPhys(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhys, uint16_t iPte)
{
/*
* Lookup the page and check if it checks out before derefing it.
*/
PVMCC pVM = pPool->CTX_SUFF(pVM);
PPGMPAGE pPhysPage = pgmPhysGetPage(pVM, GCPhys);
if (pPhysPage)
{
Assert(PGM_PAGE_GET_HCPHYS(pPhysPage));
#ifdef LOG_ENABLED
RTHCPHYS HCPhysPage = PGM_PAGE_GET_HCPHYS(pPhysPage);
Log2(("pgmPoolTracDerefGCPhys %RHp vs %RHp\n", HCPhysPage, HCPhys));
#endif
if (PGM_PAGE_GET_HCPHYS(pPhysPage) == HCPhys)
{
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
pgmTrackDerefGCPhys(pPool, pPage, pPhysPage, iPte);
return;
}
AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp; found page has HCPhys=%RHp\n",
HCPhys, GCPhys, PGM_PAGE_GET_HCPHYS(pPhysPage)));
}
AssertFatalMsgFailed(("HCPhys=%RHp GCPhys=%RGp\n", HCPhys, GCPhys));
}
/**
* Clear references to guest physical memory.
*
* @param pPool The pool.
* @param pPage The page.
* @param HCPhys The host physical address corresponding to the guest page.
* @param GCPhysHint The guest physical address which may corresponding to HCPhys.
* @param iPte Shadow pte index
*/
void pgmPoolTracDerefGCPhysHint(PPGMPOOL pPool, PPGMPOOLPAGE pPage, RTHCPHYS HCPhys, RTGCPHYS GCPhysHint, uint16_t iPte)
{
Log4(("pgmPoolTracDerefGCPhysHint %RHp %RGp\n", HCPhys, GCPhysHint));
/*
* Try the hint first.
*/
RTHCPHYS HCPhysHinted;
PVMCC pVM = pPool->CTX_SUFF(pVM);
PPGMPAGE pPhysPage = pgmPhysGetPage(pVM, GCPhysHint);
if (pPhysPage)
{
HCPhysHinted = PGM_PAGE_GET_HCPHYS(pPhysPage);
Assert(HCPhysHinted);
if (HCPhysHinted == HCPhys)
{
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
pgmTrackDerefGCPhys(pPool, pPage, pPhysPage, iPte);
return;
}
}
else
HCPhysHinted = UINT64_C(0xdeadbeefdeadbeef);
/*
* Damn, the hint didn't work. We'll have to do an expensive linear search.
*/
STAM_COUNTER_INC(&pPool->StatTrackLinearRamSearches);
PPGMRAMRANGE pRam = pPool->CTX_SUFF(pVM)->pgm.s.CTX_SUFF(pRamRangesX);
while (pRam)
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
{
if (PGM_PAGE_GET_HCPHYS(&pRam->aPages[iPage]) == HCPhys)
{
Log4(("pgmPoolTracDerefGCPhysHint: Linear HCPhys=%RHp GCPhysHint=%RGp GCPhysReal=%RGp\n",
HCPhys, GCPhysHint, pRam->GCPhys + (iPage << PAGE_SHIFT)));
Assert(pPage->cPresent);
Assert(pPool->cPresent);
pPage->cPresent--;
pPool->cPresent--;
pgmTrackDerefGCPhys(pPool, pPage, &pRam->aPages[iPage], iPte);
return;
}
}
pRam = pRam->CTX_SUFF(pNext);
}
AssertFatalMsgFailed(("HCPhys=%RHp GCPhysHint=%RGp (Hinted page has HCPhys = %RHp)\n", HCPhys, GCPhysHint, HCPhysHinted));
}
/**
* Clear references to guest physical memory in a 32-bit / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT, PCX86PT pGstPT)
{
RTGCPHYS32 const fPgMask = pPage->fA20Enabled ? X86_PTE_PG_MASK : X86_PTE_PG_MASK & ~RT_BIT_32(20);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
Assert(!(pShwPT->a[i].u & RT_BIT_32(10)));
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit32Bit: i=%d pte=%RX32 hint=%RX32\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, pGstPT->a[i].u & fPgMask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to guest physical memory in a PAE / 32-bit page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table (just a half one).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPae32Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PT pGstPT)
{
RTGCPHYS32 const fPgMask = pPage->fA20Enabled ? X86_PTE_PG_MASK : X86_PTE_PG_MASK & ~RT_BIT_32(20);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
Assert( (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == 0
|| (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == UINT64_C(0x7ff0000000000000));
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
Log4(("pgmPoolTrackDerefPTPae32Bit: i=%d pte=%RX64 hint=%RX32\n",
i, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pGstPT->a[i].u & X86_PTE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pGstPT->a[i].u & fPgMask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to guest physical memory in a PAE / PAE page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
* @param pGstPT The guest page table.
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaePae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT, PCX86PTPAE pGstPT)
{
RTGCPHYS const fPgMask = pPage->fA20Enabled ? X86_PTE_PAE_PG_MASK : X86_PTE_PAE_PG_MASK & ~RT_BIT_64(20);
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++)
{
Assert( (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == 0
|| (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == UINT64_C(0x7ff0000000000000));
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
Log4(("pgmPoolTrackDerefPTPaePae: i=%d pte=%RX32 hint=%RX32\n",
i, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pGstPT->a[i].u & X86_PTE_PAE_PG_MASK));
pgmPoolTracDerefGCPhysHint(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), pGstPT->a[i].u & fPgMask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to guest physical memory in a 32-bit / 4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPT32Bit4MB(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PT pShwPT)
{
RTGCPHYS const GCPhysA20Mask = pPage->fA20Enabled ? UINT64_MAX : ~RT_BIT_64(20);
RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
{
Assert(!(pShwPT->a[i].u & RT_BIT_32(10)));
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPT32Bit4MB: i=%d pte=%RX32 GCPhys=%RGp\n",
i, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & X86_PTE_PG_MASK, GCPhys & GCPhysA20Mask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to guest physical memory in a PAE / 2/4MB page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPTPaeBig(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PPGMSHWPTPAE pShwPT)
{
RTGCPHYS const GCPhysA20Mask = pPage->fA20Enabled ? UINT64_MAX : ~RT_BIT_64(20);
RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
{
Assert( (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == 0
|| (PGMSHWPTEPAE_GET_U(pShwPT->a[i]) & UINT64_C(0x7ff0000000000400)) == UINT64_C(0x7ff0000000000000));
if (PGMSHWPTEPAE_IS_P(pShwPT->a[i]))
{
Log4(("pgmPoolTrackDerefPTPaeBig: i=%d pte=%RX64 hint=%RGp\n",
i, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, PGMSHWPTEPAE_GET_HCPHYS(pShwPT->a[i]), GCPhys & GCPhysA20Mask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to shadowed pages in an EPT page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPT The shadow page directory pointer table (mapping of the
* page).
*/
DECLINLINE(void) pgmPoolTrackDerefPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPT pShwPT)
{
RTGCPHYS const GCPhysA20Mask = pPage->fA20Enabled ? UINT64_MAX : ~RT_BIT_64(20);
RTGCPHYS GCPhys = pPage->GCPhys + PAGE_SIZE * pPage->iFirstPresent;
for (unsigned i = pPage->iFirstPresent; i < RT_ELEMENTS(pShwPT->a); i++, GCPhys += PAGE_SIZE)
{
Assert((pShwPT->a[i].u & UINT64_C(0xfff0000000000f80)) == 0);
if (pShwPT->a[i].n.u1Present)
{
Log4(("pgmPoolTrackDerefPTEPT: i=%d pte=%RX64 GCPhys=%RX64\n",
i, pShwPT->a[i].u & EPT_PTE_PG_MASK, pPage->GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPT->a[i].u & EPT_PTE_PG_MASK, GCPhys & GCPhysA20Mask, i);
if (!pPage->cPresent)
break;
}
}
}
/**
* Clear references to shadowed pages in a 32 bits page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPD(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PD pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if ( pShwPD->a[i].n.u1Present
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%x\n", pShwPD->a[i].u & X86_PDE_PG_MASK));
}
}
}
/**
* Clear references to shadowed pages in a PAE (legacy or 64 bits) page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPAE pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
if ( pShwPD->a[i].n.u1Present
&& !(pShwPD->a[i].u & PGM_PDFLAGS_MAPPING))
{
#ifdef PGM_WITH_LARGE_PAGES
if (pShwPD->a[i].b.u1Size)
{
Log4(("pgmPoolTrackDerefPDPae: i=%d pde=%RX64 GCPhys=%RX64\n",
i, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK,
pPage->GCPhys + i * 2 * _1M /* pPage->GCPhys = base address of the memory described by the PD */,
i);
}
else
#endif
{
Assert((pShwPD->a[i].u & (X86_PDE_PAE_MBZ_MASK_NX | UINT64_C(0x7ff0000000000000))) == 0);
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & X86_PDE_PAE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & X86_PDE_PAE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
}
/**
* Clear references to shadowed pages in a PAE page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPTPae(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
{
for (unsigned i = 0; i < X86_PG_PAE_PDPE_ENTRIES; i++)
{
Assert((pShwPDPT->a[i].u & (X86_PDPE_PAE_MBZ_MASK | UINT64_C(0x7ff0000000000200))) == 0);
if ( pShwPDPT->a[i].n.u1Present
&& !(pShwPDPT->a[i].u & PGM_PLXFLAGS_MAPPING)
)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
}
}
}
/**
* Clear references to shadowed pages in a 64-bit page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPT64Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PDPT pShwPDPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
Assert((pShwPDPT->a[i].u & (X86_PDPE_LM_MBZ_MASK_NX | UINT64_C(0x7ff0000000000200))) == 0);
if (pShwPDPT->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & X86_PDPE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in a 64-bit level 4 page table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPML4 The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPML464Bit(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PX86PML4 pShwPML4)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPML4->a); i++)
{
Assert((pShwPML4->a[i].u & (X86_PML4E_MBZ_MASK_NX | UINT64_C(0x7ff0000000000200))) == 0);
if (pShwPML4->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPML4->a[i].u & X86_PDPE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPML4->a[i].u & X86_PML4E_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page directory.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPD The shadow page directory (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPD pShwPD)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPD->a); i++)
{
Assert((pShwPD->a[i].u & UINT64_C(0xfff0000000000f80)) == 0);
if (pShwPD->a[i].n.u1Present)
{
#ifdef PGM_WITH_LARGE_PAGES
if (pShwPD->a[i].b.u1Size)
{
Log4(("pgmPoolTrackDerefPDEPT: i=%d pde=%RX64 GCPhys=%RX64\n",
i, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK, pPage->GCPhys));
pgmPoolTracDerefGCPhys(pPool, pPage, pShwPD->a[i].u & X86_PDE2M_PAE_PG_MASK,
pPage->GCPhys + i * 2 * _1M /* pPage->GCPhys = base address of the memory described by the PD */,
i);
}
else
#endif
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPD->a[i].u & EPT_PDE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPD->a[i].u & EPT_PDE_PG_MASK));
}
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clear references to shadowed pages in an EPT page directory pointer table.
*
* @param pPool The pool.
* @param pPage The page.
* @param pShwPDPT The shadow page directory pointer table (mapping of the page).
*/
DECLINLINE(void) pgmPoolTrackDerefPDPTEPT(PPGMPOOL pPool, PPGMPOOLPAGE pPage, PEPTPDPT pShwPDPT)
{
for (unsigned i = 0; i < RT_ELEMENTS(pShwPDPT->a); i++)
{
Assert((pShwPDPT->a[i].u & UINT64_C(0xfff0000000000f80)) == 0);
if (pShwPDPT->a[i].n.u1Present)
{
PPGMPOOLPAGE pSubPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK);
if (pSubPage)
pgmPoolTrackFreeUser(pPool, pSubPage, pPage->idx, i);
else
AssertFatalMsgFailed(("%RX64\n", pShwPDPT->a[i].u & EPT_PDPTE_PG_MASK));
/** @todo 64-bit guests: have to ensure that we're not exhausting the dynamic mappings! */
}
}
}
/**
* Clears all references made by this page.
*
* This includes other shadow pages and GC physical addresses.
*
* @param pPool The pool.
* @param pPage The page.
*/
static void pgmPoolTrackDeref(PPGMPOOL pPool, PPGMPOOLPAGE pPage)
{
/*
* Map the shadow page and take action according to the page kind.
*/
PVMCC pVM = pPool->CTX_SUFF(pVM);
void *pvShw = PGMPOOL_PAGE_2_PTR(pVM, pPage);
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pVM, pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPT32Bit32Bit(pPool, pPage, (PX86PT)pvShw, (PCX86PT)pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR_EX(pVM, pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPae32Bit(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PT)pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
void *pvGst;
int rc = PGM_GCPHYS_2_PTR(pVM, pPage->GCPhys, &pvGst); AssertReleaseRC(rc);
pgmPoolTrackDerefPTPaePae(pPool, pPage, (PPGMSHWPTPAE)pvShw, (PCX86PTPAE)pvGst);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvGst);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_32BIT_PT_FOR_PHYS: /* treat it like a 4 MB page */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPT32Bit4MB(pPool, pPage, (PX86PT)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PT_FOR_PHYS: /* treat it like a 2 MB page */
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
{
STAM_PROFILE_START(&pPool->StatTrackDerefGCPhys, g);
pgmPoolTrackDerefPTPaeBig(pPool, pPage, (PPGMSHWPTPAE)pvShw);
STAM_PROFILE_STOP(&pPool->StatTrackDerefGCPhys, g);
break;
}
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
pgmPoolTrackDerefPDPae(pPool, pPage, (PX86PDPAE)pvShw);
break;
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_32BIT_PD:
pgmPoolTrackDerefPD(pPool, pPage, (PX86PD)pvShw);
break;
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
case PGMPOOLKIND_PAE_PDPT:
case PGMPOOLKIND_PAE_PDPT_PHYS:
pgmPoolTrackDerefPDPTPae(pPool, pPage, (PX86PDPT)pvShw);
break;
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
pgmPoolTrackDerefPDPT64Bit(pPool, pPage, (PX86PDPT)pvShw);
break;
case PGMPOOLKIND_64BIT_PML4:
pgmPoolTrackDerefPML464Bit(pPool, pPage, (PX86PML4)pvShw);
break;
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
pgmPoolTrackDerefPTEPT(pPool, pPage, (PEPTPT)pvShw);
break;
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
pgmPoolTrackDerefPDEPT(pPool, pPage, (PEPTPD)pvShw);
break;
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
pgmPoolTrackDerefPDPTEPT(pPool, pPage, (PEPTPDPT)pvShw);
break;
default:
AssertFatalMsgFailed(("enmKind=%d\n", pPage->enmKind));
}
/* paranoia, clear the shadow page. Remove this laser (i.e. let Alloc and ClearAll do it). */
STAM_PROFILE_START(&pPool->StatZeroPage, z);
ASMMemZeroPage(pvShw);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
pPage->fZeroed = true;
Assert(!pPage->cPresent);
PGM_DYNMAP_UNUSED_HINT_VM(pVM, pvShw);
}
/**
* Flushes a pool page.
*
* This moves the page to the free list after removing all user references to it.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
* @param pPool The pool.
* @param pPage The shadow page.
* @param fFlush Flush the TLBS when required (should only be false in very specific use cases!!)
*/
int pgmPoolFlushPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, bool fFlush)
{
PVMCC pVM = pPool->CTX_SUFF(pVM);
bool fFlushRequired = false;
int rc = VINF_SUCCESS;
STAM_PROFILE_START(&pPool->StatFlushPage, f);
LogFlow(("pgmPoolFlushPage: pPage=%p:{.Key=%RHp, .idx=%d, .enmKind=%s, .GCPhys=%RGp}\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), pPage->GCPhys));
/*
* Reject any attempts at flushing any of the special root pages (shall
* not happen).
*/
AssertMsgReturn(pPage->idx >= PGMPOOL_IDX_FIRST,
("pgmPoolFlushPage: special root page, rejected. enmKind=%s idx=%d\n",
pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx),
VINF_SUCCESS);
pgmLock(pVM);
/*
* Quietly reject any attempts at flushing the currently active shadow CR3 mapping
*/
if (pgmPoolIsPageLocked(pPage))
{
AssertMsg( pPage->enmKind == PGMPOOLKIND_64BIT_PML4
|| pPage->enmKind == PGMPOOLKIND_PAE_PDPT
|| pPage->enmKind == PGMPOOLKIND_PAE_PDPT_FOR_32BIT
|| pPage->enmKind == PGMPOOLKIND_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD_FOR_PAE_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD
|| pPage->enmKind == PGMPOOLKIND_ROOT_NESTED,
("Can't free the shadow CR3! (%RHp vs %RHp kind=%d\n", PGMGetHyperCR3(VMMGetCpu(pVM)), pPage->Core.Key, pPage->enmKind));
Log(("pgmPoolFlushPage: current active shadow CR3, rejected. enmKind=%s idx=%d\n", pgmPoolPoolKindToStr(pPage->enmKind), pPage->idx));
pgmUnlock(pVM);
return VINF_SUCCESS;
}
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Start a subset so we won't run out of mapping space. */
PVMCPU pVCpu = VMMGetCpu(pVM);
uint32_t iPrevSubset = PGMRZDynMapPushAutoSubset(pVCpu);
#endif
/*
* Mark the page as being in need of an ASMMemZeroPage().
*/
pPage->fZeroed = false;
#ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
pgmPoolFlushDirtyPage(pVM, pPool, pPage->idxDirtyEntry, false /* do not remove */);
#endif
/* If there are any users of this table, then we *must* issue a tlb flush on all VCPUs. */
if (pPage->iUserHead != NIL_PGMPOOL_USER_INDEX)
fFlushRequired = true;
/*
* Clear the page.
*/
pgmPoolTrackClearPageUsers(pPool, pPage);
STAM_PROFILE_START(&pPool->StatTrackDeref,a);
pgmPoolTrackDeref(pPool, pPage);
STAM_PROFILE_STOP(&pPool->StatTrackDeref,a);
/*
* Flush it from the cache.
*/
pgmPoolCacheFlushPage(pPool, pPage);
#ifdef VBOX_WITH_2X_4GB_ADDR_SPACE_IN_R0
/* Heavy stuff done. */
PGMRZDynMapPopAutoSubset(pVCpu, iPrevSubset);
#endif
/*
* Deregistering the monitoring.
*/
if (pPage->fMonitored)
rc = pgmPoolMonitorFlush(pPool, pPage);
/*
* Free the page.
*/
Assert(pPage->iNext == NIL_PGMPOOL_IDX);
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->fReusedFlushPending = false;
pPool->cUsedPages--;
/* Flush the TLBs of all VCPUs if required. */
if ( fFlushRequired
&& fFlush)
{
PGM_INVL_ALL_VCPU_TLBS(pVM);
}
pgmUnlock(pVM);
STAM_PROFILE_STOP(&pPool->StatFlushPage, f);
return rc;
}
/**
* Frees a usage of a pool page.
*
* The caller is responsible to updating the user table so that it no longer
* references the shadow page.
*
* @param pPool The pool.
* @param pPage The shadow page.
* @param iUser The shadow page pool index of the user table.
* NIL_PGMPOOL_IDX for root pages.
* @param iUserTable The index into the user table (shadowed). Ignored if
* root page.
*/
void pgmPoolFreeByPage(PPGMPOOL pPool, PPGMPOOLPAGE pPage, uint16_t iUser, uint32_t iUserTable)
{
PVMCC pVM = pPool->CTX_SUFF(pVM);
STAM_PROFILE_START(&pPool->StatFree, a);
LogFlow(("pgmPoolFreeByPage: pPage=%p:{.Key=%RHp, .idx=%d, enmKind=%s} iUser=%d iUserTable=%#x\n",
pPage, pPage->Core.Key, pPage->idx, pgmPoolPoolKindToStr(pPage->enmKind), iUser, iUserTable));
AssertReturnVoid(pPage->idx >= PGMPOOL_IDX_FIRST); /* paranoia (#6349) */
pgmLock(pVM);
if (iUser != NIL_PGMPOOL_IDX)
pgmPoolTrackFreeUser(pPool, pPage, iUser, iUserTable);
if (!pPage->fCached)
pgmPoolFlushPage(pPool, pPage);
pgmUnlock(pVM);
STAM_PROFILE_STOP(&pPool->StatFree, a);
}
/**
* Makes one or more free page free.
*
* @returns VBox status code.
* @retval VINF_SUCCESS on success.
*
* @param pPool The pool.
* @param enmKind Page table kind
* @param iUser The user of the page.
*/
static int pgmPoolMakeMoreFreePages(PPGMPOOL pPool, PGMPOOLKIND enmKind, uint16_t iUser)
{
PVMCC pVM = pPool->CTX_SUFF(pVM);
LogFlow(("pgmPoolMakeMoreFreePages: enmKind=%d iUser=%d\n", enmKind, iUser));
NOREF(enmKind);
/*
* If the pool isn't full grown yet, expand it.
*/
if (pPool->cCurPages < pPool->cMaxPages)
{
STAM_PROFILE_ADV_SUSPEND(&pPool->StatAlloc, a);
#ifdef IN_RING3
int rc = PGMR3PoolGrow(pVM, VMMGetCpu(pVM));
#else
int rc = VMMRZCallRing3NoCpu(pVM, VMMCALLRING3_PGM_POOL_GROW, 0);
#endif
if (RT_FAILURE(rc))
return rc;
STAM_PROFILE_ADV_RESUME(&pPool->StatAlloc, a);
if (pPool->iFreeHead != NIL_PGMPOOL_IDX)
return VINF_SUCCESS;
}
/*
* Free one cached page.
*/
return pgmPoolCacheFreeOne(pPool, iUser);
}
/**
* Allocates a page from the pool.
*
* This page may actually be a cached page and not in need of any processing
* on the callers part.
*
* @returns VBox status code.
* @retval VINF_SUCCESS if a NEW page was allocated.
* @retval VINF_PGM_CACHED_PAGE if a CACHED page was returned.
*
* @param pVM The cross context VM structure.
* @param GCPhys The GC physical address of the page we're gonna shadow.
* For 4MB and 2MB PD entries, it's the first address the
* shadow PT is covering.
* @param enmKind The kind of mapping.
* @param enmAccess Access type for the mapping (only relevant for big pages)
* @param fA20Enabled Whether the A20 gate is enabled or not.
* @param iUser The shadow page pool index of the user table. Root
* pages should pass NIL_PGMPOOL_IDX.
* @param iUserTable The index into the user table (shadowed). Ignored for
* root pages (iUser == NIL_PGMPOOL_IDX).
* @param fLockPage Lock the page
* @param ppPage Where to store the pointer to the page. NULL is stored here on failure.
*/
int pgmPoolAlloc(PVMCC pVM, RTGCPHYS GCPhys, PGMPOOLKIND enmKind, PGMPOOLACCESS enmAccess, bool fA20Enabled,
uint16_t iUser, uint32_t iUserTable, bool fLockPage, PPPGMPOOLPAGE ppPage)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
STAM_PROFILE_ADV_START(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: GCPhys=%RGp enmKind=%s iUser=%d iUserTable=%#x\n", GCPhys, pgmPoolPoolKindToStr(enmKind), iUser, iUserTable));
*ppPage = NULL;
/** @todo CSAM/PGMPrefetchPage messes up here during CSAMR3CheckGates
* (TRPMR3SyncIDT) because of FF priority. Try fix that?
* Assert(!(pVM->pgm.s.fGlobalSyncFlags & PGM_SYNC_CLEAR_PGM_POOL)); */
pgmLock(pVM);
if (pPool->fCacheEnabled)
{
int rc2 = pgmPoolCacheAlloc(pPool, GCPhys, enmKind, enmAccess, fA20Enabled, iUser, iUserTable, ppPage);
if (RT_SUCCESS(rc2))
{
if (fLockPage)
pgmPoolLockPage(pPool, *ppPage);
pgmUnlock(pVM);
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
LogFlow(("pgmPoolAlloc: cached returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d}\n", rc2, *ppPage, (*ppPage)->Core.Key, (*ppPage)->idx));
return rc2;
}
}
/*
* Allocate a new one.
*/
int rc = VINF_SUCCESS;
uint16_t iNew = pPool->iFreeHead;
if (iNew == NIL_PGMPOOL_IDX)
{
rc = pgmPoolMakeMoreFreePages(pPool, enmKind, iUser);
if (RT_FAILURE(rc))
{
pgmUnlock(pVM);
Log(("pgmPoolAlloc: returns %Rrc (Free)\n", rc));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
iNew = pPool->iFreeHead;
AssertReleaseMsgReturn(iNew != NIL_PGMPOOL_IDX, ("iNew=%#x\n", iNew), VERR_PGM_POOL_IPE);
}
/* unlink the free head */
PPGMPOOLPAGE pPage = &pPool->aPages[iNew];
pPool->iFreeHead = pPage->iNext;
pPage->iNext = NIL_PGMPOOL_IDX;
/*
* Initialize it.
*/
pPool->cUsedPages++; /* physical handler registration / pgmPoolTrackFlushGCPhysPTsSlow requirement. */
pPage->enmKind = enmKind;
pPage->enmAccess = enmAccess;
pPage->GCPhys = GCPhys;
pPage->fA20Enabled = fA20Enabled;
pPage->fSeenNonGlobal = false; /* Set this to 'true' to disable this feature. */
pPage->fMonitored = false;
pPage->fCached = false;
pPage->fDirty = false;
pPage->fReusedFlushPending = false;
pPage->cModifications = 0;
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->cPresent = 0;
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
pPage->idxDirtyEntry = 0;
pPage->GCPtrLastAccessHandlerFault = NIL_RTGCPTR;
pPage->GCPtrLastAccessHandlerRip = NIL_RTGCPTR;
pPage->cLastAccessHandler = 0;
pPage->cLocked = 0;
# ifdef VBOX_STRICT
pPage->GCPtrDirtyFault = NIL_RTGCPTR;
# endif
/*
* Insert into the tracking and cache. If this fails, free the page.
*/
int rc3 = pgmPoolTrackInsert(pPool, pPage, GCPhys, iUser, iUserTable);
if (RT_FAILURE(rc3))
{
pPool->cUsedPages--;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->iNext = pPool->iFreeHead;
pPool->iFreeHead = pPage->idx;
pgmUnlock(pVM);
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
Log(("pgmPoolAlloc: returns %Rrc (Insert)\n", rc3));
return rc3;
}
/*
* Commit the allocation, clear the page and return.
*/
#ifdef VBOX_WITH_STATISTICS
if (pPool->cUsedPages > pPool->cUsedPagesHigh)
pPool->cUsedPagesHigh = pPool->cUsedPages;
#endif
if (!pPage->fZeroed)
{
STAM_PROFILE_START(&pPool->StatZeroPage, z);
void *pv = PGMPOOL_PAGE_2_PTR(pVM, pPage);
ASMMemZeroPage(pv);
STAM_PROFILE_STOP(&pPool->StatZeroPage, z);
}
*ppPage = pPage;
if (fLockPage)
pgmPoolLockPage(pPool, pPage);
pgmUnlock(pVM);
LogFlow(("pgmPoolAlloc: returns %Rrc *ppPage=%p:{.Key=%RHp, .idx=%d, .fCached=%RTbool, .fMonitored=%RTbool}\n",
rc, pPage, pPage->Core.Key, pPage->idx, pPage->fCached, pPage->fMonitored));
STAM_PROFILE_ADV_STOP(&pPool->StatAlloc, a);
return rc;
}
/**
* Frees a usage of a pool page.
*
* @param pVM The cross context VM structure.
* @param HCPhys The HC physical address of the shadow page.
* @param iUser The shadow page pool index of the user table.
* NIL_PGMPOOL_IDX if root page.
* @param iUserTable The index into the user table (shadowed). Ignored if
* root page.
*/
void pgmPoolFree(PVM pVM, RTHCPHYS HCPhys, uint16_t iUser, uint32_t iUserTable)
{
LogFlow(("pgmPoolFree: HCPhys=%RHp iUser=%d iUserTable=%#x\n", HCPhys, iUser, iUserTable));
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
pgmPoolFreeByPage(pPool, pgmPoolGetPage(pPool, HCPhys), iUser, iUserTable);
}
/**
* Internal worker for finding a 'in-use' shadow page give by it's physical address.
*
* @returns Pointer to the shadow page structure.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
PPGMPOOLPAGE pgmPoolGetPage(PPGMPOOL pPool, RTHCPHYS HCPhys)
{
PGM_LOCK_ASSERT_OWNER(pPool->CTX_SUFF(pVM));
/*
* Look up the page.
*/
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
AssertFatalMsg(pPage && pPage->enmKind != PGMPOOLKIND_FREE, ("HCPhys=%RHp pPage=%p idx=%d\n", HCPhys, pPage, (pPage) ? pPage->idx : 0));
return pPage;
}
/**
* Internal worker for finding a page for debugging purposes, no assertions.
*
* @returns Pointer to the shadow page structure. NULL on if not found.
* @param pPool The pool.
* @param HCPhys The HC physical address of the shadow page.
*/
PPGMPOOLPAGE pgmPoolQueryPageForDbg(PPGMPOOL pPool, RTHCPHYS HCPhys)
{
PGM_LOCK_ASSERT_OWNER(pPool->CTX_SUFF(pVM));
return (PPGMPOOLPAGE)RTAvloHCPhysGet(&pPool->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
}
/**
* Internal worker for PGM_HCPHYS_2_PTR.
*
* @returns VBox status code.
* @param pVM The cross context VM structure.
* @param HCPhys The HC physical address of the shadow page.
* @param ppv Where to return the address.
*/
int pgmPoolHCPhys2Ptr(PVM pVM, RTHCPHYS HCPhys, void **ppv)
{
PPGMPOOLPAGE pPage = (PPGMPOOLPAGE)RTAvloHCPhysGet(&pVM->pgm.s.CTX_SUFF(pPool)->HCPhysTree, HCPhys & X86_PTE_PAE_PG_MASK);
AssertMsgReturn(pPage && pPage->enmKind != PGMPOOLKIND_FREE,
("HCPhys=%RHp pPage=%p idx=%d\n", HCPhys, pPage, (pPage) ? pPage->idx : 0),
VERR_PGM_POOL_GET_PAGE_FAILED);
*ppv = (uint8_t *)pPage->CTX_SUFF(pvPage) + (HCPhys & PAGE_OFFSET_MASK);
return VINF_SUCCESS;
}
#ifdef IN_RING3 /* currently only used in ring 3; save some space in the R0 & GC modules (left it here as we might need it elsewhere later on) */
/**
* Flush the specified page if present
*
* @param pVM The cross context VM structure.
* @param GCPhys Guest physical address of the page to flush
*/
void pgmPoolFlushPageByGCPhys(PVM pVM, RTGCPHYS GCPhys)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
VM_ASSERT_EMT(pVM);
/*
* Look up the GCPhys in the hash.
*/
GCPhys = GCPhys & ~(RTGCPHYS)PAGE_OFFSET_MASK;
unsigned i = pPool->aiHash[PGMPOOL_HASH(GCPhys)];
if (i == NIL_PGMPOOL_IDX)
return;
do
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
if (pPage->GCPhys - GCPhys < PAGE_SIZE)
{
switch (pPage->enmKind)
{
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
case PGMPOOLKIND_64BIT_PML4:
case PGMPOOLKIND_32BIT_PD:
case PGMPOOLKIND_PAE_PDPT:
{
Log(("PGMPoolFlushPage: found pgm pool pages for %RGp\n", GCPhys));
# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
if (pPage->fDirty)
STAM_COUNTER_INC(&pPool->StatForceFlushDirtyPage);
else
# endif
STAM_COUNTER_INC(&pPool->StatForceFlushPage);
Assert(!pgmPoolIsPageLocked(pPage));
pgmPoolMonitorChainFlush(pPool, pPage);
return;
}
/* ignore, no monitoring. */
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
case PGMPOOLKIND_ROOT_NESTED:
case PGMPOOLKIND_PAE_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_PHYS:
case PGMPOOLKIND_32BIT_PD_PHYS:
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
break;
default:
AssertFatalMsgFailed(("enmKind=%d idx=%d\n", pPage->enmKind, pPage->idx));
}
}
/* next */
i = pPage->iNext;
} while (i != NIL_PGMPOOL_IDX);
return;
}
/**
* Reset CPU on hot plugging.
*
* @param pVM The cross context VM structure.
* @param pVCpu The cross context virtual CPU structure.
*/
void pgmR3PoolResetUnpluggedCpu(PVM pVM, PVMCPU pVCpu)
{
pgmR3ExitShadowModeBeforePoolFlush(pVCpu);
pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
}
/**
* Flushes the entire cache.
*
* It will assert a global CR3 flush (FF) and assumes the caller is aware of
* this and execute this CR3 flush.
*
* @param pVM The cross context VM structure.
*/
void pgmR3PoolReset(PVM pVM)
{
PPGMPOOL pPool = pVM->pgm.s.CTX_SUFF(pPool);
PGM_LOCK_ASSERT_OWNER(pVM);
STAM_PROFILE_START(&pPool->StatR3Reset, a);
LogFlow(("pgmR3PoolReset:\n"));
/*
* If there are no pages in the pool, there is nothing to do.
*/
if (pPool->cCurPages <= PGMPOOL_IDX_FIRST)
{
STAM_PROFILE_STOP(&pPool->StatR3Reset, a);
return;
}
/*
* Exit the shadow mode since we're going to clear everything,
* including the root page.
*/
VMCC_FOR_EACH_VMCPU(pVM)
pgmR3ExitShadowModeBeforePoolFlush(pVCpu);
VMCC_FOR_EACH_VMCPU_END(pVM);
/*
* Nuke the free list and reinsert all pages into it.
*/
for (unsigned i = pPool->cCurPages - 1; i >= PGMPOOL_IDX_FIRST; i--)
{
PPGMPOOLPAGE pPage = &pPool->aPages[i];
if (pPage->fMonitored)
pgmPoolMonitorFlush(pPool, pPage);
pPage->iModifiedNext = NIL_PGMPOOL_IDX;
pPage->iModifiedPrev = NIL_PGMPOOL_IDX;
pPage->iMonitoredNext = NIL_PGMPOOL_IDX;
pPage->iMonitoredPrev = NIL_PGMPOOL_IDX;
pPage->GCPhys = NIL_RTGCPHYS;
pPage->enmKind = PGMPOOLKIND_FREE;
pPage->enmAccess = PGMPOOLACCESS_DONTCARE;
Assert(pPage->idx == i);
pPage->iNext = i + 1;
pPage->fA20Enabled = true;
pPage->fZeroed = false; /* This could probably be optimized, but better safe than sorry. */
pPage->fSeenNonGlobal = false;
pPage->fMonitored = false;
pPage->fDirty = false;
pPage->fCached = false;
pPage->fReusedFlushPending = false;
pPage->iUserHead = NIL_PGMPOOL_USER_INDEX;
pPage->cPresent = 0;
pPage->iFirstPresent = NIL_PGMPOOL_PRESENT_INDEX;
pPage->cModifications = 0;
pPage->iAgeNext = NIL_PGMPOOL_IDX;
pPage->iAgePrev = NIL_PGMPOOL_IDX;
pPage->idxDirtyEntry = 0;
pPage->GCPtrLastAccessHandlerRip = NIL_RTGCPTR;
pPage->GCPtrLastAccessHandlerFault = NIL_RTGCPTR;
pPage->cLastAccessHandler = 0;
pPage->cLocked = 0;
# ifdef VBOX_STRICT
pPage->GCPtrDirtyFault = NIL_RTGCPTR;
# endif
}
pPool->aPages[pPool->cCurPages - 1].iNext = NIL_PGMPOOL_IDX;
pPool->iFreeHead = PGMPOOL_IDX_FIRST;
pPool->cUsedPages = 0;
/*
* Zap and reinitialize the user records.
*/
pPool->cPresent = 0;
pPool->iUserFreeHead = 0;
PPGMPOOLUSER paUsers = pPool->CTX_SUFF(paUsers);
const unsigned cMaxUsers = pPool->cMaxUsers;
for (unsigned i = 0; i < cMaxUsers; i++)
{
paUsers[i].iNext = i + 1;
paUsers[i].iUser = NIL_PGMPOOL_IDX;
paUsers[i].iUserTable = 0xfffffffe;
}
paUsers[cMaxUsers - 1].iNext = NIL_PGMPOOL_USER_INDEX;
/*
* Clear all the GCPhys links and rebuild the phys ext free list.
*/
for (PPGMRAMRANGE pRam = pVM->pgm.s.CTX_SUFF(pRamRangesX);
pRam;
pRam = pRam->CTX_SUFF(pNext))
{
unsigned iPage = pRam->cb >> PAGE_SHIFT;
while (iPage-- > 0)
PGM_PAGE_SET_TRACKING(pVM, &pRam->aPages[iPage], 0);
}
pPool->iPhysExtFreeHead = 0;
PPGMPOOLPHYSEXT paPhysExts = pPool->CTX_SUFF(paPhysExts);
const unsigned cMaxPhysExts = pPool->cMaxPhysExts;
for (unsigned i = 0; i < cMaxPhysExts; i++)
{
paPhysExts[i].iNext = i + 1;
paPhysExts[i].aidx[0] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[0] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
paPhysExts[i].aidx[1] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[1] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
paPhysExts[i].aidx[2] = NIL_PGMPOOL_IDX;
paPhysExts[i].apte[2] = NIL_PGMPOOL_PHYSEXT_IDX_PTE;
}
paPhysExts[cMaxPhysExts - 1].iNext = NIL_PGMPOOL_PHYSEXT_INDEX;
/*
* Just zap the modified list.
*/
pPool->cModifiedPages = 0;
pPool->iModifiedHead = NIL_PGMPOOL_IDX;
/*
* Clear the GCPhys hash and the age list.
*/
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aiHash); i++)
pPool->aiHash[i] = NIL_PGMPOOL_IDX;
pPool->iAgeHead = NIL_PGMPOOL_IDX;
pPool->iAgeTail = NIL_PGMPOOL_IDX;
# ifdef PGMPOOL_WITH_OPTIMIZED_DIRTY_PT
/* Clear all dirty pages. */
pPool->idxFreeDirtyPage = 0;
pPool->cDirtyPages = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pPool->aidxDirtyPages); i++)
pPool->aidxDirtyPages[i] = NIL_PGMPOOL_IDX;
# endif
/*
* Reinsert active pages into the hash and ensure monitoring chains are correct.
*/
VMCC_FOR_EACH_VMCPU(pVM)
{
/*
* Re-enter the shadowing mode and assert Sync CR3 FF.
*/
pgmR3ReEnterShadowModeAfterPoolFlush(pVM, pVCpu);
VMCPU_FF_SET(pVCpu, VMCPU_FF_PGM_SYNC_CR3);
VMCPU_FF_SET(pVCpu, VMCPU_FF_TLB_FLUSH);
}
VMCC_FOR_EACH_VMCPU_END(pVM);
STAM_PROFILE_STOP(&pPool->StatR3Reset, a);
}
#endif /* IN_RING3 */
#if defined(LOG_ENABLED) || defined(VBOX_STRICT)
/**
* Stringifies a PGMPOOLKIND value.
*/
static const char *pgmPoolPoolKindToStr(uint8_t enmKind)
{
switch ((PGMPOOLKIND)enmKind)
{
case PGMPOOLKIND_INVALID:
return "PGMPOOLKIND_INVALID";
case PGMPOOLKIND_FREE:
return "PGMPOOLKIND_FREE";
case PGMPOOLKIND_32BIT_PT_FOR_PHYS:
return "PGMPOOLKIND_32BIT_PT_FOR_PHYS";
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT:
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_PT";
case PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB:
return "PGMPOOLKIND_32BIT_PT_FOR_32BIT_4MB";
case PGMPOOLKIND_PAE_PT_FOR_PHYS:
return "PGMPOOLKIND_PAE_PT_FOR_PHYS";
case PGMPOOLKIND_PAE_PT_FOR_32BIT_PT:
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_PT";
case PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB:
return "PGMPOOLKIND_PAE_PT_FOR_32BIT_4MB";
case PGMPOOLKIND_PAE_PT_FOR_PAE_PT:
return "PGMPOOLKIND_PAE_PT_FOR_PAE_PT";
case PGMPOOLKIND_PAE_PT_FOR_PAE_2MB:
return "PGMPOOLKIND_PAE_PT_FOR_PAE_2MB";
case PGMPOOLKIND_32BIT_PD:
return "PGMPOOLKIND_32BIT_PD";
case PGMPOOLKIND_32BIT_PD_PHYS:
return "PGMPOOLKIND_32BIT_PD_PHYS";
case PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD0_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD1_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD2_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD:
return "PGMPOOLKIND_PAE_PD3_FOR_32BIT_PD";
case PGMPOOLKIND_PAE_PD_FOR_PAE_PD:
return "PGMPOOLKIND_PAE_PD_FOR_PAE_PD";
case PGMPOOLKIND_PAE_PD_PHYS:
return "PGMPOOLKIND_PAE_PD_PHYS";
case PGMPOOLKIND_PAE_PDPT_FOR_32BIT:
return "PGMPOOLKIND_PAE_PDPT_FOR_32BIT";
case PGMPOOLKIND_PAE_PDPT:
return "PGMPOOLKIND_PAE_PDPT";
case PGMPOOLKIND_PAE_PDPT_PHYS:
return "PGMPOOLKIND_PAE_PDPT_PHYS";
case PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT:
return "PGMPOOLKIND_64BIT_PDPT_FOR_64BIT_PDPT";
case PGMPOOLKIND_64BIT_PDPT_FOR_PHYS:
return "PGMPOOLKIND_64BIT_PDPT_FOR_PHYS";
case PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD:
return "PGMPOOLKIND_64BIT_PD_FOR_64BIT_PD";
case PGMPOOLKIND_64BIT_PD_FOR_PHYS:
return "PGMPOOLKIND_64BIT_PD_FOR_PHYS";
case PGMPOOLKIND_64BIT_PML4:
return "PGMPOOLKIND_64BIT_PML4";
case PGMPOOLKIND_EPT_PDPT_FOR_PHYS:
return "PGMPOOLKIND_EPT_PDPT_FOR_PHYS";
case PGMPOOLKIND_EPT_PD_FOR_PHYS:
return "PGMPOOLKIND_EPT_PD_FOR_PHYS";
case PGMPOOLKIND_EPT_PT_FOR_PHYS:
return "PGMPOOLKIND_EPT_PT_FOR_PHYS";
case PGMPOOLKIND_ROOT_NESTED:
return "PGMPOOLKIND_ROOT_NESTED";
}
return "Unknown kind!";
}
#endif /* LOG_ENABLED || VBOX_STRICT */
|