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
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
|
/* $Id: GMMR0.cpp $ */
/** @file
* GMM - Global Memory Manager.
*/
/*
* Copyright (C) 2007-2023 Oracle and/or its affiliates.
*
* This file is part of VirtualBox base platform packages, as
* available from https://www.virtualbox.org.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation, in version 3 of the
* License.
*
* This program is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <https://www.gnu.org/licenses>.
*
* SPDX-License-Identifier: GPL-3.0-only
*/
/** @page pg_gmm GMM - The Global Memory Manager
*
* As the name indicates, this component is responsible for global memory
* management. Currently only guest RAM is allocated from the GMM, but this
* may change to include shadow page tables and other bits later.
*
* Guest RAM is managed as individual pages, but allocated from the host OS
* in chunks for reasons of portability / efficiency. To minimize the memory
* footprint all tracking structure must be as small as possible without
* unnecessary performance penalties.
*
* The allocation chunks has fixed sized, the size defined at compile time
* by the #GMM_CHUNK_SIZE \#define.
*
* Each chunk is given an unique ID. Each page also has a unique ID. The
* relationship between the two IDs is:
* @code
* GMM_CHUNK_SHIFT = log2(GMM_CHUNK_SIZE / GUEST_PAGE_SIZE);
* idPage = (idChunk << GMM_CHUNK_SHIFT) | iPage;
* @endcode
* Where iPage is the index of the page within the chunk. This ID scheme
* permits for efficient chunk and page lookup, but it relies on the chunk size
* to be set at compile time. The chunks are organized in an AVL tree with their
* IDs being the keys.
*
* The physical address of each page in an allocation chunk is maintained by
* the #RTR0MEMOBJ and obtained using #RTR0MemObjGetPagePhysAddr. There is no
* need to duplicate this information (it'll cost 8-bytes per page if we did).
*
* So what do we need to track per page? Most importantly we need to know
* which state the page is in:
* - Private - Allocated for (eventually) backing one particular VM page.
* - Shared - Readonly page that is used by one or more VMs and treated
* as COW by PGM.
* - Free - Not used by anyone.
*
* For the page replacement operations (sharing, defragmenting and freeing)
* to be somewhat efficient, private pages needs to be associated with a
* particular page in a particular VM.
*
* Tracking the usage of shared pages is impractical and expensive, so we'll
* settle for a reference counting system instead.
*
* Free pages will be chained on LIFOs
*
* On 64-bit systems we will use a 64-bit bitfield per page, while on 32-bit
* systems a 32-bit bitfield will have to suffice because of address space
* limitations. The #GMMPAGE structure shows the details.
*
*
* @section sec_gmm_alloc_strat Page Allocation Strategy
*
* The strategy for allocating pages has to take fragmentation and shared
* pages into account, or we may end up with with 2000 chunks with only
* a few pages in each. Shared pages cannot easily be reallocated because
* of the inaccurate usage accounting (see above). Private pages can be
* reallocated by a defragmentation thread in the same manner that sharing
* is done.
*
* The first approach is to manage the free pages in two sets depending on
* whether they are mainly for the allocation of shared or private pages.
* In the initial implementation there will be almost no possibility for
* mixing shared and private pages in the same chunk (only if we're really
* stressed on memory), but when we implement forking of VMs and have to
* deal with lots of COW pages it'll start getting kind of interesting.
*
* The sets are lists of chunks with approximately the same number of
* free pages. Say the chunk size is 1MB, meaning 256 pages, and a set
* consists of 16 lists. So, the first list will contain the chunks with
* 1-7 free pages, the second covers 8-15, and so on. The chunks will be
* moved between the lists as pages are freed up or allocated.
*
*
* @section sec_gmm_costs Costs
*
* The per page cost in kernel space is 32-bit plus whatever RTR0MEMOBJ
* entails. In addition there is the chunk cost of approximately
* (sizeof(RT0MEMOBJ) + sizeof(CHUNK)) / 2^CHUNK_SHIFT bytes per page.
*
* On Windows the per page #RTR0MEMOBJ cost is 32-bit on 32-bit windows
* and 64-bit on 64-bit windows (a PFN_NUMBER in the MDL). So, 64-bit per page.
* The cost on Linux is identical, but here it's because of sizeof(struct page *).
*
*
* @section sec_gmm_legacy Legacy Mode for Non-Tier-1 Platforms
*
* In legacy mode the page source is locked user pages and not
* #RTR0MemObjAllocPhysNC, this means that a page can only be allocated
* by the VM that locked it. We will make no attempt at implementing
* page sharing on these systems, just do enough to make it all work.
*
* @note With 6.1 really dropping 32-bit support, the legacy mode is obsoleted
* under the assumption that there is sufficient kernel virtual address
* space to map all of the guest memory allocations. So, we'll be using
* #RTR0MemObjAllocPage on some platforms as an alternative to
* #RTR0MemObjAllocPhysNC.
*
*
* @subsection sub_gmm_locking Serializing
*
* One simple fast mutex will be employed in the initial implementation, not
* two as mentioned in @ref sec_pgmPhys_Serializing.
*
* @see @ref sec_pgmPhys_Serializing
*
*
* @section sec_gmm_overcommit Memory Over-Commitment Management
*
* The GVM will have to do the system wide memory over-commitment
* management. My current ideas are:
* - Per VM oc policy that indicates how much to initially commit
* to it and what to do in a out-of-memory situation.
* - Prevent overtaxing the host.
*
* There are some challenges here, the main ones are configurability and
* security. Should we for instance permit anyone to request 100% memory
* commitment? Who should be allowed to do runtime adjustments of the
* config. And how to prevent these settings from being lost when the last
* VM process exits? The solution is probably to have an optional root
* daemon the will keep VMMR0.r0 in memory and enable the security measures.
*
*
*
* @section sec_gmm_numa NUMA
*
* NUMA considerations will be designed and implemented a bit later.
*
* The preliminary guesses is that we will have to try allocate memory as
* close as possible to the CPUs the VM is executed on (EMT and additional CPU
* threads). Which means it's mostly about allocation and sharing policies.
* Both the scheduler and allocator interface will to supply some NUMA info
* and we'll need to have a way to calc access costs.
*
*/
/*********************************************************************************************************************************
* Header Files *
*********************************************************************************************************************************/
#define LOG_GROUP LOG_GROUP_GMM
#include <VBox/rawpci.h>
#include <VBox/vmm/gmm.h>
#include "GMMR0Internal.h"
#include <VBox/vmm/vmcc.h>
#include <VBox/vmm/pgm.h>
#include <VBox/log.h>
#include <VBox/param.h>
#include <VBox/err.h>
#include <VBox/VMMDev.h>
#include <iprt/asm.h>
#include <iprt/avl.h>
#ifdef VBOX_STRICT
# include <iprt/crc.h>
#endif
#include <iprt/critsect.h>
#include <iprt/list.h>
#include <iprt/mem.h>
#include <iprt/memobj.h>
#include <iprt/mp.h>
#include <iprt/semaphore.h>
#include <iprt/spinlock.h>
#include <iprt/string.h>
#include <iprt/time.h>
/* This is 64-bit only code now. */
#if HC_ARCH_BITS != 64 || ARCH_BITS != 64
# error "This is 64-bit only code"
#endif
/*********************************************************************************************************************************
* Defined Constants And Macros *
*********************************************************************************************************************************/
/** @def VBOX_USE_CRIT_SECT_FOR_GIANT
* Use a critical section instead of a fast mutex for the giant GMM lock.
*
* @remarks This is primarily a way of avoiding the deadlock checks in the
* windows driver verifier. */
#if defined(RT_OS_WINDOWS) || defined(RT_OS_DARWIN) || defined(DOXYGEN_RUNNING)
# define VBOX_USE_CRIT_SECT_FOR_GIANT
#endif
/*********************************************************************************************************************************
* Structures and Typedefs *
*********************************************************************************************************************************/
/** Pointer to set of free chunks. */
typedef struct GMMCHUNKFREESET *PGMMCHUNKFREESET;
/**
* The per-page tracking structure employed by the GMM.
*
* Because of the different layout on 32-bit and 64-bit hosts in earlier
* versions of the code, macros are used to get and set some of the data.
*/
typedef union GMMPAGE
{
/** Unsigned integer view. */
uint64_t u;
/** The common view. */
struct GMMPAGECOMMON
{
uint32_t uStuff1 : 32;
uint32_t uStuff2 : 30;
/** The page state. */
uint32_t u2State : 2;
} Common;
/** The view of a private page. */
struct GMMPAGEPRIVATE
{
/** The guest page frame number. (Max addressable: 2 ^ 44 - 16) */
uint32_t pfn;
/** The GVM handle. (64K VMs) */
uint32_t hGVM : 16;
/** Reserved. */
uint32_t u16Reserved : 14;
/** The page state. */
uint32_t u2State : 2;
} Private;
/** The view of a shared page. */
struct GMMPAGESHARED
{
/** The host page frame number. (Max addressable: 2 ^ 44 - 16) */
uint32_t pfn;
/** The reference count (64K VMs). */
uint32_t cRefs : 16;
/** Used for debug checksumming. */
uint32_t u14Checksum : 14;
/** The page state. */
uint32_t u2State : 2;
} Shared;
/** The view of a free page. */
struct GMMPAGEFREE
{
/** The index of the next page in the free list. UINT16_MAX is NIL. */
uint16_t iNext;
/** Reserved. Checksum or something? */
uint16_t u16Reserved0;
/** Reserved. Checksum or something? */
uint32_t u30Reserved1 : 29;
/** Set if the page was zeroed. */
uint32_t fZeroed : 1;
/** The page state. */
uint32_t u2State : 2;
} Free;
} GMMPAGE;
AssertCompileSize(GMMPAGE, sizeof(RTHCUINTPTR));
/** Pointer to a GMMPAGE. */
typedef GMMPAGE *PGMMPAGE;
/** @name The Page States.
* @{ */
/** A private page. */
#define GMM_PAGE_STATE_PRIVATE 0
/** A shared page. */
#define GMM_PAGE_STATE_SHARED 2
/** A free page. */
#define GMM_PAGE_STATE_FREE 3
/** @} */
/** @def GMM_PAGE_IS_PRIVATE
*
* @returns true if private, false if not.
* @param pPage The GMM page.
*/
#define GMM_PAGE_IS_PRIVATE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_PRIVATE )
/** @def GMM_PAGE_IS_SHARED
*
* @returns true if shared, false if not.
* @param pPage The GMM page.
*/
#define GMM_PAGE_IS_SHARED(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_SHARED )
/** @def GMM_PAGE_IS_FREE
*
* @returns true if free, false if not.
* @param pPage The GMM page.
*/
#define GMM_PAGE_IS_FREE(pPage) ( (pPage)->Common.u2State == GMM_PAGE_STATE_FREE )
/** @def GMM_PAGE_PFN_LAST
* The last valid guest pfn range.
* @remark Some of the values outside the range has special meaning,
* see GMM_PAGE_PFN_UNSHAREABLE.
*/
#define GMM_PAGE_PFN_LAST UINT32_C(0xfffffff0)
AssertCompile(GMM_PAGE_PFN_LAST == (GMM_GCPHYS_LAST >> GUEST_PAGE_SHIFT));
/** @def GMM_PAGE_PFN_UNSHAREABLE
* Indicates that this page isn't used for normal guest memory and thus isn't shareable.
*/
#define GMM_PAGE_PFN_UNSHAREABLE UINT32_C(0xfffffff1)
AssertCompile(GMM_PAGE_PFN_UNSHAREABLE == (GMM_GCPHYS_UNSHAREABLE >> GUEST_PAGE_SHIFT));
/**
* A GMM allocation chunk ring-3 mapping record.
*
* This should really be associated with a session and not a VM, but
* it's simpler to associated with a VM and cleanup with the VM object
* is destroyed.
*/
typedef struct GMMCHUNKMAP
{
/** The mapping object. */
RTR0MEMOBJ hMapObj;
/** The VM owning the mapping. */
PGVM pGVM;
} GMMCHUNKMAP;
/** Pointer to a GMM allocation chunk mapping. */
typedef struct GMMCHUNKMAP *PGMMCHUNKMAP;
/**
* A GMM allocation chunk.
*/
typedef struct GMMCHUNK
{
/** The AVL node core.
* The Key is the chunk ID. (Giant mtx.) */
AVLU32NODECORE Core;
/** The memory object.
* Either from RTR0MemObjAllocPhysNC or RTR0MemObjLockUser depending on
* what the host can dish up with. (Chunk mtx protects mapping accesses
* and related frees.) */
RTR0MEMOBJ hMemObj;
#ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
/** Pointer to the kernel mapping. */
uint8_t *pbMapping;
#endif
/** Pointer to the next chunk in the free list. (Giant mtx.) */
PGMMCHUNK pFreeNext;
/** Pointer to the previous chunk in the free list. (Giant mtx.) */
PGMMCHUNK pFreePrev;
/** Pointer to the free set this chunk belongs to. NULL for
* chunks with no free pages. (Giant mtx.) */
PGMMCHUNKFREESET pSet;
/** List node in the chunk list (GMM::ChunkList). (Giant mtx.) */
RTLISTNODE ListNode;
/** Pointer to an array of mappings. (Chunk mtx.) */
PGMMCHUNKMAP paMappingsX;
/** The number of mappings. (Chunk mtx.) */
uint16_t cMappingsX;
/** The mapping lock this chunk is using using. UINT8_MAX if nobody is mapping
* or freeing anything. (Giant mtx.) */
uint8_t volatile iChunkMtx;
/** GMM_CHUNK_FLAGS_XXX. (Giant mtx.) */
uint8_t fFlags;
/** The head of the list of free pages. UINT16_MAX is the NIL value.
* (Giant mtx.) */
uint16_t iFreeHead;
/** The number of free pages. (Giant mtx.) */
uint16_t cFree;
/** The GVM handle of the VM that first allocated pages from this chunk, this
* is used as a preference when there are several chunks to choose from.
* When in bound memory mode this isn't a preference any longer. (Giant
* mtx.) */
uint16_t hGVM;
/** The ID of the NUMA node the memory mostly resides on. (Reserved for
* future use.) (Giant mtx.) */
uint16_t idNumaNode;
/** The number of private pages. (Giant mtx.) */
uint16_t cPrivate;
/** The number of shared pages. (Giant mtx.) */
uint16_t cShared;
/** The UID this chunk is associated with. */
RTUID uidOwner;
uint32_t u32Padding;
/** The pages. (Giant mtx.) */
GMMPAGE aPages[GMM_CHUNK_NUM_PAGES];
} GMMCHUNK;
/** Indicates that the NUMA properies of the memory is unknown. */
#define GMM_CHUNK_NUMA_ID_UNKNOWN UINT16_C(0xfffe)
/** @name GMM_CHUNK_FLAGS_XXX - chunk flags.
* @{ */
/** Indicates that the chunk is a large page (2MB). */
#define GMM_CHUNK_FLAGS_LARGE_PAGE UINT16_C(0x0001)
/** @} */
/**
* An allocation chunk TLB entry.
*/
typedef struct GMMCHUNKTLBE
{
/** The chunk id. */
uint32_t idChunk;
/** Pointer to the chunk. */
PGMMCHUNK pChunk;
} GMMCHUNKTLBE;
/** Pointer to an allocation chunk TLB entry. */
typedef GMMCHUNKTLBE *PGMMCHUNKTLBE;
/** The number of entries in the allocation chunk TLB. */
#define GMM_CHUNKTLB_ENTRIES 32
/** Gets the TLB entry index for the given Chunk ID. */
#define GMM_CHUNKTLB_IDX(idChunk) ( (idChunk) & (GMM_CHUNKTLB_ENTRIES - 1) )
/**
* An allocation chunk TLB.
*/
typedef struct GMMCHUNKTLB
{
/** The TLB entries. */
GMMCHUNKTLBE aEntries[GMM_CHUNKTLB_ENTRIES];
} GMMCHUNKTLB;
/** Pointer to an allocation chunk TLB. */
typedef GMMCHUNKTLB *PGMMCHUNKTLB;
/**
* The GMM instance data.
*/
typedef struct GMM
{
/** Magic / eye catcher. GMM_MAGIC */
uint32_t u32Magic;
/** The number of threads waiting on the mutex. */
uint32_t cMtxContenders;
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
/** The critical section protecting the GMM.
* More fine grained locking can be implemented later if necessary. */
RTCRITSECT GiantCritSect;
#else
/** The fast mutex protecting the GMM.
* More fine grained locking can be implemented later if necessary. */
RTSEMFASTMUTEX hMtx;
#endif
#ifdef VBOX_STRICT
/** The current mutex owner. */
RTNATIVETHREAD hMtxOwner;
#endif
/** Spinlock protecting the AVL tree.
* @todo Make this a read-write spinlock as we should allow concurrent
* lookups. */
RTSPINLOCK hSpinLockTree;
/** The chunk tree.
* Protected by hSpinLockTree. */
PAVLU32NODECORE pChunks;
/** Chunk freeing generation - incremented whenever a chunk is freed. Used
* for validating the per-VM chunk TLB entries. Valid range is 1 to 2^62
* (exclusive), though higher numbers may temporarily occure while
* invalidating the individual TLBs during wrap-around processing. */
uint64_t volatile idFreeGeneration;
/** The chunk TLB.
* Protected by hSpinLockTree. */
GMMCHUNKTLB ChunkTLB;
/** The private free set. */
GMMCHUNKFREESET PrivateX;
/** The shared free set. */
GMMCHUNKFREESET Shared;
/** Shared module tree (global).
* @todo separate trees for distinctly different guest OSes. */
PAVLLU32NODECORE pGlobalSharedModuleTree;
/** Sharable modules (count of nodes in pGlobalSharedModuleTree). */
uint32_t cShareableModules;
/** The chunk list. For simplifying the cleanup process and avoid tree
* traversal. */
RTLISTANCHOR ChunkList;
/** The maximum number of pages we're allowed to allocate.
* @gcfgm{GMM/MaxPages,64-bit, Direct.}
* @gcfgm{GMM/PctPages,32-bit, Relative to the number of host pages.} */
uint64_t cMaxPages;
/** The number of pages that has been reserved.
* The deal is that cReservedPages - cOverCommittedPages <= cMaxPages. */
uint64_t cReservedPages;
/** The number of pages that we have over-committed in reservations. */
uint64_t cOverCommittedPages;
/** The number of actually allocated (committed if you like) pages. */
uint64_t cAllocatedPages;
/** The number of pages that are shared. A subset of cAllocatedPages. */
uint64_t cSharedPages;
/** The number of pages that are actually shared between VMs. */
uint64_t cDuplicatePages;
/** The number of pages that are shared that has been left behind by
* VMs not doing proper cleanups. */
uint64_t cLeftBehindSharedPages;
/** The number of allocation chunks.
* (The number of pages we've allocated from the host can be derived from this.) */
uint32_t cChunks;
/** The number of current ballooned pages. */
uint64_t cBalloonedPages;
#ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
/** Whether #RTR0MemObjAllocPhysNC works. */
bool fHasWorkingAllocPhysNC;
#else
bool fPadding;
#endif
/** The bound memory mode indicator.
* When set, the memory will be bound to a specific VM and never
* shared. This is always set if fLegacyAllocationMode is set.
* (Also determined at initialization time.) */
bool fBoundMemoryMode;
/** The number of registered VMs. */
uint16_t cRegisteredVMs;
/** The index of the next mutex to use. */
uint32_t iNextChunkMtx;
/** Chunk locks for reducing lock contention without having to allocate
* one lock per chunk. */
struct
{
/** The mutex */
RTSEMFASTMUTEX hMtx;
/** The number of threads currently using this mutex. */
uint32_t volatile cUsers;
} aChunkMtx[64];
/** The number of freed chunks ever. This is used as list generation to
* avoid restarting the cleanup scanning when the list wasn't modified. */
uint32_t volatile cFreedChunks;
/** The previous allocated Chunk ID.
* Used as a hint to avoid scanning the whole bitmap. */
uint32_t idChunkPrev;
/** Spinlock protecting idChunkPrev & bmChunkId. */
RTSPINLOCK hSpinLockChunkId;
/** Chunk ID allocation bitmap.
* Bits of allocated IDs are set, free ones are clear.
* The NIL id (0) is marked allocated. */
uint32_t bmChunkId[(GMM_CHUNKID_LAST + 1 + 31) / 32];
} GMM;
/** Pointer to the GMM instance. */
typedef GMM *PGMM;
/** The value of GMM::u32Magic (Katsuhiro Otomo). */
#define GMM_MAGIC UINT32_C(0x19540414)
/**
* GMM chunk mutex state.
*
* This is returned by gmmR0ChunkMutexAcquire and is used by the other
* gmmR0ChunkMutex* methods.
*/
typedef struct GMMR0CHUNKMTXSTATE
{
PGMM pGMM;
/** The index of the chunk mutex. */
uint8_t iChunkMtx;
/** The relevant flags (GMMR0CHUNK_MTX_XXX). */
uint8_t fFlags;
} GMMR0CHUNKMTXSTATE;
/** Pointer to a chunk mutex state. */
typedef GMMR0CHUNKMTXSTATE *PGMMR0CHUNKMTXSTATE;
/** @name GMMR0CHUNK_MTX_XXX
* @{ */
#define GMMR0CHUNK_MTX_INVALID UINT32_C(0)
#define GMMR0CHUNK_MTX_KEEP_GIANT UINT32_C(1)
#define GMMR0CHUNK_MTX_RETAKE_GIANT UINT32_C(2)
#define GMMR0CHUNK_MTX_DROP_GIANT UINT32_C(3)
#define GMMR0CHUNK_MTX_END UINT32_C(4)
/** @} */
/** The maximum number of shared modules per-vm. */
#define GMM_MAX_SHARED_PER_VM_MODULES 2048
/** The maximum number of shared modules GMM is allowed to track. */
#define GMM_MAX_SHARED_GLOBAL_MODULES 16834
/**
* Argument packet for gmmR0SharedModuleCleanup.
*/
typedef struct GMMR0SHMODPERVMDTORARGS
{
PGVM pGVM;
PGMM pGMM;
} GMMR0SHMODPERVMDTORARGS;
/**
* Argument packet for gmmR0CheckSharedModule.
*/
typedef struct GMMCHECKSHAREDMODULEINFO
{
PGVM pGVM;
VMCPUID idCpu;
} GMMCHECKSHAREDMODULEINFO;
/*********************************************************************************************************************************
* Global Variables *
*********************************************************************************************************************************/
/** Pointer to the GMM instance data. */
static PGMM g_pGMM = NULL;
/** Macro for obtaining and validating the g_pGMM pointer.
*
* On failure it will return from the invoking function with the specified
* return value.
*
* @param pGMM The name of the pGMM variable.
* @param rc The return value on failure. Use VERR_GMM_INSTANCE for VBox
* status codes.
*/
#define GMM_GET_VALID_INSTANCE(pGMM, rc) \
do { \
(pGMM) = g_pGMM; \
AssertPtrReturn((pGMM), (rc)); \
AssertMsgReturn((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic), (rc)); \
} while (0)
/** Macro for obtaining and validating the g_pGMM pointer, void function
* variant.
*
* On failure it will return from the invoking function.
*
* @param pGMM The name of the pGMM variable.
*/
#define GMM_GET_VALID_INSTANCE_VOID(pGMM) \
do { \
(pGMM) = g_pGMM; \
AssertPtrReturnVoid((pGMM)); \
AssertMsgReturnVoid((pGMM)->u32Magic == GMM_MAGIC, ("%p - %#x\n", (pGMM), (pGMM)->u32Magic)); \
} while (0)
/** @def GMM_CHECK_SANITY_UPON_ENTERING
* Checks the sanity of the GMM instance data before making changes.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && defined(GMMR0_WITH_SANITY_CHECK) && 0
# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (RT_LIKELY(gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0))
#else
# define GMM_CHECK_SANITY_UPON_ENTERING(pGMM) (true)
#endif
/** @def GMM_CHECK_SANITY_UPON_LEAVING
* Checks the sanity of the GMM instance data after making changes.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && defined(GMMR0_WITH_SANITY_CHECK) && 0
# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
#else
# define GMM_CHECK_SANITY_UPON_LEAVING(pGMM) (true)
#endif
/** @def GMM_CHECK_SANITY_IN_LOOPS
* Checks the sanity of the GMM instance in the allocation loops.
*
* This is macro is a stub by default and must be enabled manually in the code.
*
* @returns true if sane, false if not.
* @param pGMM The name of the pGMM variable.
*/
#if defined(VBOX_STRICT) && defined(GMMR0_WITH_SANITY_CHECK) && 0
# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (gmmR0SanityCheck((pGMM), __PRETTY_FUNCTION__, __LINE__) == 0)
#else
# define GMM_CHECK_SANITY_IN_LOOPS(pGMM) (true)
#endif
/*********************************************************************************************************************************
* Internal Functions *
*********************************************************************************************************************************/
static DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM);
static bool gmmR0CleanupVMScanChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
DECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk);
DECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet);
DECLINLINE(void) gmmR0SelectSetAndLinkChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
#ifdef GMMR0_WITH_SANITY_CHECK
static uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo);
#endif
static bool gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, bool fRelaxedSem);
DECLINLINE(void) gmmR0FreePrivatePage(PGMM pGMM, PGVM pGVM, uint32_t idPage, PGMMPAGE pPage);
DECLINLINE(void) gmmR0FreeSharedPage(PGMM pGMM, PGVM pGVM, uint32_t idPage, PGMMPAGE pPage);
static int gmmR0UnmapChunkLocked(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk);
#ifdef VBOX_WITH_PAGE_SHARING
static void gmmR0SharedModuleCleanup(PGMM pGMM, PGVM pGVM);
# ifdef VBOX_STRICT
static uint32_t gmmR0StrictPageChecksum(PGMM pGMM, PGVM pGVM, uint32_t idPage);
# endif
#endif
/**
* Initializes the GMM component.
*
* This is called when the VMMR0.r0 module is loaded and protected by the
* loader semaphore.
*
* @returns VBox status code.
*/
GMMR0DECL(int) GMMR0Init(void)
{
LogFlow(("GMMInit:\n"));
/* Currently assuming same host and guest page size here. Can change it to
dish out guest pages with different size from the host page later if
needed, though a restriction would be the host page size must be larger
than the guest page size. */
AssertCompile(GUEST_PAGE_SIZE == HOST_PAGE_SIZE);
AssertCompile(GUEST_PAGE_SIZE <= HOST_PAGE_SIZE);
/*
* Allocate the instance data and the locks.
*/
PGMM pGMM = (PGMM)RTMemAllocZ(sizeof(*pGMM));
if (!pGMM)
return VERR_NO_MEMORY;
pGMM->u32Magic = GMM_MAGIC;
for (unsigned i = 0; i < RT_ELEMENTS(pGMM->ChunkTLB.aEntries); i++)
pGMM->ChunkTLB.aEntries[i].idChunk = NIL_GMM_CHUNKID;
RTListInit(&pGMM->ChunkList);
ASMBitSet(&pGMM->bmChunkId[0], NIL_GMM_CHUNKID);
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
int rc = RTCritSectInit(&pGMM->GiantCritSect);
#else
int rc = RTSemFastMutexCreate(&pGMM->hMtx);
#endif
if (RT_SUCCESS(rc))
{
unsigned iMtx;
for (iMtx = 0; iMtx < RT_ELEMENTS(pGMM->aChunkMtx); iMtx++)
{
rc = RTSemFastMutexCreate(&pGMM->aChunkMtx[iMtx].hMtx);
if (RT_FAILURE(rc))
break;
}
pGMM->hSpinLockTree = NIL_RTSPINLOCK;
if (RT_SUCCESS(rc))
rc = RTSpinlockCreate(&pGMM->hSpinLockTree, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "gmm-chunk-tree");
pGMM->hSpinLockChunkId = NIL_RTSPINLOCK;
if (RT_SUCCESS(rc))
rc = RTSpinlockCreate(&pGMM->hSpinLockChunkId, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "gmm-chunk-id");
if (RT_SUCCESS(rc))
{
/*
* Figure out how we're going to allocate stuff (only applicable to
* host with linear physical memory mappings).
*/
pGMM->fBoundMemoryMode = false;
#ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
pGMM->fHasWorkingAllocPhysNC = false;
RTR0MEMOBJ hMemObj;
rc = RTR0MemObjAllocPhysNC(&hMemObj, GMM_CHUNK_SIZE, NIL_RTHCPHYS);
if (RT_SUCCESS(rc))
{
rc = RTR0MemObjFree(hMemObj, true);
AssertRC(rc);
pGMM->fHasWorkingAllocPhysNC = true;
}
else if (rc != VERR_NOT_SUPPORTED)
SUPR0Printf("GMMR0Init: Warning! RTR0MemObjAllocPhysNC(, %u, NIL_RTHCPHYS) -> %d!\n", GMM_CHUNK_SIZE, rc);
# endif
/*
* Query system page count and guess a reasonable cMaxPages value.
*/
pGMM->cMaxPages = UINT32_MAX; /** @todo IPRT function for query ram size and such. */
/*
* The idFreeGeneration value should be set so we actually trigger the
* wrap-around invalidation handling during a typical test run.
*/
pGMM->idFreeGeneration = UINT64_MAX / 4 - 128;
g_pGMM = pGMM;
#ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
LogFlow(("GMMInit: pGMM=%p fBoundMemoryMode=%RTbool fHasWorkingAllocPhysNC=%RTbool\n", pGMM, pGMM->fBoundMemoryMode, pGMM->fHasWorkingAllocPhysNC));
#else
LogFlow(("GMMInit: pGMM=%p fBoundMemoryMode=%RTbool\n", pGMM, pGMM->fBoundMemoryMode));
#endif
return VINF_SUCCESS;
}
/*
* Bail out.
*/
RTSpinlockDestroy(pGMM->hSpinLockChunkId);
RTSpinlockDestroy(pGMM->hSpinLockTree);
while (iMtx-- > 0)
RTSemFastMutexDestroy(pGMM->aChunkMtx[iMtx].hMtx);
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
RTCritSectDelete(&pGMM->GiantCritSect);
#else
RTSemFastMutexDestroy(pGMM->hMtx);
#endif
}
pGMM->u32Magic = 0;
RTMemFree(pGMM);
SUPR0Printf("GMMR0Init: failed! rc=%d\n", rc);
return rc;
}
/**
* Terminates the GMM component.
*/
GMMR0DECL(void) GMMR0Term(void)
{
LogFlow(("GMMTerm:\n"));
/*
* Take care / be paranoid...
*/
PGMM pGMM = g_pGMM;
if (!RT_VALID_PTR(pGMM))
return;
if (pGMM->u32Magic != GMM_MAGIC)
{
SUPR0Printf("GMMR0Term: u32Magic=%#x\n", pGMM->u32Magic);
return;
}
/*
* Undo what init did and free all the resources we've acquired.
*/
/* Destroy the fundamentals. */
g_pGMM = NULL;
pGMM->u32Magic = ~GMM_MAGIC;
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
RTCritSectDelete(&pGMM->GiantCritSect);
#else
RTSemFastMutexDestroy(pGMM->hMtx);
pGMM->hMtx = NIL_RTSEMFASTMUTEX;
#endif
RTSpinlockDestroy(pGMM->hSpinLockTree);
pGMM->hSpinLockTree = NIL_RTSPINLOCK;
RTSpinlockDestroy(pGMM->hSpinLockChunkId);
pGMM->hSpinLockChunkId = NIL_RTSPINLOCK;
/* Free any chunks still hanging around. */
RTAvlU32Destroy(&pGMM->pChunks, gmmR0TermDestroyChunk, pGMM);
/* Destroy the chunk locks. */
for (unsigned iMtx = 0; iMtx < RT_ELEMENTS(pGMM->aChunkMtx); iMtx++)
{
Assert(pGMM->aChunkMtx[iMtx].cUsers == 0);
RTSemFastMutexDestroy(pGMM->aChunkMtx[iMtx].hMtx);
pGMM->aChunkMtx[iMtx].hMtx = NIL_RTSEMFASTMUTEX;
}
/* Finally the instance data itself. */
RTMemFree(pGMM);
LogFlow(("GMMTerm: done\n"));
}
/**
* RTAvlU32Destroy callback.
*
* @returns 0
* @param pNode The node to destroy.
* @param pvGMM The GMM handle.
*/
static DECLCALLBACK(int) gmmR0TermDestroyChunk(PAVLU32NODECORE pNode, void *pvGMM)
{
PGMMCHUNK pChunk = (PGMMCHUNK)pNode;
if (pChunk->cFree != GMM_CHUNK_NUM_PAGES)
SUPR0Printf("GMMR0Term: %RKv/%#x: cFree=%d cPrivate=%d cShared=%d cMappings=%d\n", pChunk,
pChunk->Core.Key, pChunk->cFree, pChunk->cPrivate, pChunk->cShared, pChunk->cMappingsX);
int rc = RTR0MemObjFree(pChunk->hMemObj, true /* fFreeMappings */);
if (RT_FAILURE(rc))
{
SUPR0Printf("GMMR0Term: %RKv/%#x: RTRMemObjFree(%RKv,true) -> %d (cMappings=%d)\n", pChunk,
pChunk->Core.Key, pChunk->hMemObj, rc, pChunk->cMappingsX);
AssertRC(rc);
}
pChunk->hMemObj = NIL_RTR0MEMOBJ;
RTMemFree(pChunk->paMappingsX);
pChunk->paMappingsX = NULL;
RTMemFree(pChunk);
NOREF(pvGMM);
return 0;
}
/**
* Initializes the per-VM data for the GMM.
*
* This is called from within the GVMM lock (from GVMMR0CreateVM)
* and should only initialize the data members so GMMR0CleanupVM
* can deal with them. We reserve no memory or anything here,
* that's done later in GMMR0InitVM.
*
* @param pGVM Pointer to the Global VM structure.
*/
GMMR0DECL(int) GMMR0InitPerVMData(PGVM pGVM)
{
AssertCompile(RT_SIZEOFMEMB(GVM,gmm.s) <= RT_SIZEOFMEMB(GVM,gmm.padding));
pGVM->gmm.s.Stats.enmPolicy = GMMOCPOLICY_INVALID;
pGVM->gmm.s.Stats.enmPriority = GMMPRIORITY_INVALID;
pGVM->gmm.s.Stats.fMayAllocate = false;
pGVM->gmm.s.hChunkTlbSpinLock = NIL_RTSPINLOCK;
int rc = RTSpinlockCreate(&pGVM->gmm.s.hChunkTlbSpinLock, RTSPINLOCK_FLAGS_INTERRUPT_SAFE, "per-vm-chunk-tlb");
AssertRCReturn(rc, rc);
return VINF_SUCCESS;
}
/**
* Acquires the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pGMM Pointer to the GMM instance.
*/
static int gmmR0MutexAcquire(PGMM pGMM)
{
ASMAtomicIncU32(&pGMM->cMtxContenders);
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
int rc = RTCritSectEnter(&pGMM->GiantCritSect);
#else
int rc = RTSemFastMutexRequest(pGMM->hMtx);
#endif
ASMAtomicDecU32(&pGMM->cMtxContenders);
AssertRC(rc);
#ifdef VBOX_STRICT
pGMM->hMtxOwner = RTThreadNativeSelf();
#endif
return rc;
}
/**
* Releases the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pGMM Pointer to the GMM instance.
*/
static int gmmR0MutexRelease(PGMM pGMM)
{
#ifdef VBOX_STRICT
pGMM->hMtxOwner = NIL_RTNATIVETHREAD;
#endif
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
int rc = RTCritSectLeave(&pGMM->GiantCritSect);
#else
int rc = RTSemFastMutexRelease(pGMM->hMtx);
AssertRC(rc);
#endif
return rc;
}
/**
* Yields the GMM giant lock if there is contention and a certain minimum time
* has elapsed since we took it.
*
* @returns @c true if the mutex was yielded, @c false if not.
* @param pGMM Pointer to the GMM instance.
* @param puLockNanoTS Where the lock acquisition time stamp is kept
* (in/out).
*/
static bool gmmR0MutexYield(PGMM pGMM, uint64_t *puLockNanoTS)
{
/*
* If nobody is contending the mutex, don't bother checking the time.
*/
if (ASMAtomicReadU32(&pGMM->cMtxContenders) == 0)
return false;
/*
* Don't yield if we haven't executed for at least 2 milliseconds.
*/
uint64_t uNanoNow = RTTimeSystemNanoTS();
if (uNanoNow - *puLockNanoTS < UINT32_C(2000000))
return false;
/*
* Yield the mutex.
*/
#ifdef VBOX_STRICT
pGMM->hMtxOwner = NIL_RTNATIVETHREAD;
#endif
ASMAtomicIncU32(&pGMM->cMtxContenders);
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
int rc1 = RTCritSectLeave(&pGMM->GiantCritSect); AssertRC(rc1);
#else
int rc1 = RTSemFastMutexRelease(pGMM->hMtx); AssertRC(rc1);
#endif
RTThreadYield();
#ifdef VBOX_USE_CRIT_SECT_FOR_GIANT
int rc2 = RTCritSectEnter(&pGMM->GiantCritSect); AssertRC(rc2);
#else
int rc2 = RTSemFastMutexRequest(pGMM->hMtx); AssertRC(rc2);
#endif
*puLockNanoTS = RTTimeSystemNanoTS();
ASMAtomicDecU32(&pGMM->cMtxContenders);
#ifdef VBOX_STRICT
pGMM->hMtxOwner = RTThreadNativeSelf();
#endif
return true;
}
/**
* Acquires a chunk lock.
*
* The caller must own the giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pMtxState The chunk mutex state info. (Avoids
* passing the same flags and stuff around
* for subsequent release and drop-giant
* calls.)
* @param pGMM Pointer to the GMM instance.
* @param pChunk Pointer to the chunk.
* @param fFlags Flags regarding the giant lock, GMMR0CHUNK_MTX_XXX.
*/
static int gmmR0ChunkMutexAcquire(PGMMR0CHUNKMTXSTATE pMtxState, PGMM pGMM, PGMMCHUNK pChunk, uint32_t fFlags)
{
Assert(fFlags > GMMR0CHUNK_MTX_INVALID && fFlags < GMMR0CHUNK_MTX_END);
Assert(pGMM->hMtxOwner == RTThreadNativeSelf());
pMtxState->pGMM = pGMM;
pMtxState->fFlags = (uint8_t)fFlags;
/*
* Get the lock index and reference the lock.
*/
Assert(pGMM->hMtxOwner == RTThreadNativeSelf());
uint32_t iChunkMtx = pChunk->iChunkMtx;
if (iChunkMtx == UINT8_MAX)
{
iChunkMtx = pGMM->iNextChunkMtx++;
iChunkMtx %= RT_ELEMENTS(pGMM->aChunkMtx);
/* Try get an unused one... */
if (pGMM->aChunkMtx[iChunkMtx].cUsers)
{
iChunkMtx = pGMM->iNextChunkMtx++;
iChunkMtx %= RT_ELEMENTS(pGMM->aChunkMtx);
if (pGMM->aChunkMtx[iChunkMtx].cUsers)
{
iChunkMtx = pGMM->iNextChunkMtx++;
iChunkMtx %= RT_ELEMENTS(pGMM->aChunkMtx);
if (pGMM->aChunkMtx[iChunkMtx].cUsers)
{
iChunkMtx = pGMM->iNextChunkMtx++;
iChunkMtx %= RT_ELEMENTS(pGMM->aChunkMtx);
}
}
}
pChunk->iChunkMtx = iChunkMtx;
}
AssertCompile(RT_ELEMENTS(pGMM->aChunkMtx) < UINT8_MAX);
pMtxState->iChunkMtx = (uint8_t)iChunkMtx;
ASMAtomicIncU32(&pGMM->aChunkMtx[iChunkMtx].cUsers);
/*
* Drop the giant?
*/
if (fFlags != GMMR0CHUNK_MTX_KEEP_GIANT)
{
/** @todo GMM life cycle cleanup (we may race someone
* destroying and cleaning up GMM)? */
gmmR0MutexRelease(pGMM);
}
/*
* Take the chunk mutex.
*/
int rc = RTSemFastMutexRequest(pGMM->aChunkMtx[iChunkMtx].hMtx);
AssertRC(rc);
return rc;
}
/**
* Releases the GMM giant lock.
*
* @returns Assert status code from RTSemFastMutexRequest.
* @param pMtxState Pointer to the chunk mutex state.
* @param pChunk Pointer to the chunk if it's still
* alive, NULL if it isn't. This is used to deassociate
* the chunk from the mutex on the way out so a new one
* can be selected next time, thus avoiding contented
* mutexes.
*/
static int gmmR0ChunkMutexRelease(PGMMR0CHUNKMTXSTATE pMtxState, PGMMCHUNK pChunk)
{
PGMM pGMM = pMtxState->pGMM;
/*
* Release the chunk mutex and reacquire the giant if requested.
*/
int rc = RTSemFastMutexRelease(pGMM->aChunkMtx[pMtxState->iChunkMtx].hMtx);
AssertRC(rc);
if (pMtxState->fFlags == GMMR0CHUNK_MTX_RETAKE_GIANT)
rc = gmmR0MutexAcquire(pGMM);
else
Assert((pMtxState->fFlags != GMMR0CHUNK_MTX_DROP_GIANT) == (pGMM->hMtxOwner == RTThreadNativeSelf()));
/*
* Drop the chunk mutex user reference and deassociate it from the chunk
* when possible.
*/
if ( ASMAtomicDecU32(&pGMM->aChunkMtx[pMtxState->iChunkMtx].cUsers) == 0
&& pChunk
&& RT_SUCCESS(rc) )
{
if (pMtxState->fFlags != GMMR0CHUNK_MTX_DROP_GIANT)
pChunk->iChunkMtx = UINT8_MAX;
else
{
rc = gmmR0MutexAcquire(pGMM);
if (RT_SUCCESS(rc))
{
if (pGMM->aChunkMtx[pMtxState->iChunkMtx].cUsers == 0)
pChunk->iChunkMtx = UINT8_MAX;
rc = gmmR0MutexRelease(pGMM);
}
}
}
pMtxState->pGMM = NULL;
return rc;
}
/**
* Drops the giant GMM lock we kept in gmmR0ChunkMutexAcquire while keeping the
* chunk locked.
*
* This only works if gmmR0ChunkMutexAcquire was called with
* GMMR0CHUNK_MTX_KEEP_GIANT. gmmR0ChunkMutexRelease will retake the giant
* mutex, i.e. behave as if GMMR0CHUNK_MTX_RETAKE_GIANT was used.
*
* @returns VBox status code (assuming success is ok).
* @param pMtxState Pointer to the chunk mutex state.
*/
static int gmmR0ChunkMutexDropGiant(PGMMR0CHUNKMTXSTATE pMtxState)
{
AssertReturn(pMtxState->fFlags == GMMR0CHUNK_MTX_KEEP_GIANT, VERR_GMM_MTX_FLAGS);
Assert(pMtxState->pGMM->hMtxOwner == RTThreadNativeSelf());
pMtxState->fFlags = GMMR0CHUNK_MTX_RETAKE_GIANT;
/** @todo GMM life cycle cleanup (we may race someone
* destroying and cleaning up GMM)? */
return gmmR0MutexRelease(pMtxState->pGMM);
}
/**
* For experimenting with NUMA affinity and such.
*
* @returns The current NUMA Node ID.
*/
static uint16_t gmmR0GetCurrentNumaNodeId(void)
{
#if 1
return GMM_CHUNK_NUMA_ID_UNKNOWN;
#else
return RTMpCpuId() / 16;
#endif
}
/**
* Cleans up when a VM is terminating.
*
* @param pGVM Pointer to the Global VM structure.
*/
GMMR0DECL(void) GMMR0CleanupVM(PGVM pGVM)
{
LogFlow(("GMMR0CleanupVM: pGVM=%p:{.hSelf=%#x}\n", pGVM, pGVM->hSelf));
PGMM pGMM;
GMM_GET_VALID_INSTANCE_VOID(pGMM);
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Clean up all registered shared modules first.
*/
gmmR0SharedModuleCleanup(pGMM, pGVM);
#endif
gmmR0MutexAcquire(pGMM);
uint64_t uLockNanoTS = RTTimeSystemNanoTS();
GMM_CHECK_SANITY_UPON_ENTERING(pGMM);
/*
* The policy is 'INVALID' until the initial reservation
* request has been serviced.
*/
if ( pGVM->gmm.s.Stats.enmPolicy > GMMOCPOLICY_INVALID
&& pGVM->gmm.s.Stats.enmPolicy < GMMOCPOLICY_END)
{
/*
* If it's the last VM around, we can skip walking all the chunk looking
* for the pages owned by this VM and instead flush the whole shebang.
*
* This takes care of the eventuality that a VM has left shared page
* references behind (shouldn't happen of course, but you never know).
*/
Assert(pGMM->cRegisteredVMs);
pGMM->cRegisteredVMs--;
/*
* Walk the entire pool looking for pages that belong to this VM
* and leftover mappings. (This'll only catch private pages,
* shared pages will be 'left behind'.)
*/
/** @todo r=bird: This scanning+freeing could be optimized in bound mode! */
uint64_t cPrivatePages = pGVM->gmm.s.Stats.cPrivatePages; /* save */
unsigned iCountDown = 64;
bool fRedoFromStart;
PGMMCHUNK pChunk;
do
{
fRedoFromStart = false;
RTListForEachReverse(&pGMM->ChunkList, pChunk, GMMCHUNK, ListNode)
{
uint32_t const cFreeChunksOld = pGMM->cFreedChunks;
if ( ( !pGMM->fBoundMemoryMode
|| pChunk->hGVM == pGVM->hSelf)
&& gmmR0CleanupVMScanChunk(pGMM, pGVM, pChunk))
{
/* We left the giant mutex, so reset the yield counters. */
uLockNanoTS = RTTimeSystemNanoTS();
iCountDown = 64;
}
else
{
/* Didn't leave it, so do normal yielding. */
if (!iCountDown)
gmmR0MutexYield(pGMM, &uLockNanoTS);
else
iCountDown--;
}
if (pGMM->cFreedChunks != cFreeChunksOld)
{
fRedoFromStart = true;
break;
}
}
} while (fRedoFromStart);
if (pGVM->gmm.s.Stats.cPrivatePages)
SUPR0Printf("GMMR0CleanupVM: hGVM=%#x has %#x private pages that cannot be found!\n", pGVM->hSelf, pGVM->gmm.s.Stats.cPrivatePages);
pGMM->cAllocatedPages -= cPrivatePages;
/*
* Free empty chunks.
*/
PGMMCHUNKFREESET pPrivateSet = pGMM->fBoundMemoryMode ? &pGVM->gmm.s.Private : &pGMM->PrivateX;
do
{
fRedoFromStart = false;
iCountDown = 10240;
pChunk = pPrivateSet->apLists[GMM_CHUNK_FREE_SET_UNUSED_LIST];
while (pChunk)
{
PGMMCHUNK pNext = pChunk->pFreeNext;
Assert(pChunk->cFree == GMM_CHUNK_NUM_PAGES);
if ( !pGMM->fBoundMemoryMode
|| pChunk->hGVM == pGVM->hSelf)
{
uint64_t const idGenerationOld = pPrivateSet->idGeneration;
if (gmmR0FreeChunk(pGMM, pGVM, pChunk, true /*fRelaxedSem*/))
{
/* We've left the giant mutex, restart? (+1 for our unlink) */
fRedoFromStart = pPrivateSet->idGeneration != idGenerationOld + 1;
if (fRedoFromStart)
break;
uLockNanoTS = RTTimeSystemNanoTS();
iCountDown = 10240;
}
}
/* Advance and maybe yield the lock. */
pChunk = pNext;
if (--iCountDown == 0)
{
uint64_t const idGenerationOld = pPrivateSet->idGeneration;
fRedoFromStart = gmmR0MutexYield(pGMM, &uLockNanoTS)
&& pPrivateSet->idGeneration != idGenerationOld;
if (fRedoFromStart)
break;
iCountDown = 10240;
}
}
} while (fRedoFromStart);
/*
* Account for shared pages that weren't freed.
*/
if (pGVM->gmm.s.Stats.cSharedPages)
{
Assert(pGMM->cSharedPages >= pGVM->gmm.s.Stats.cSharedPages);
SUPR0Printf("GMMR0CleanupVM: hGVM=%#x left %#x shared pages behind!\n", pGVM->hSelf, pGVM->gmm.s.Stats.cSharedPages);
pGMM->cLeftBehindSharedPages += pGVM->gmm.s.Stats.cSharedPages;
}
/*
* Clean up balloon statistics in case the VM process crashed.
*/
Assert(pGMM->cBalloonedPages >= pGVM->gmm.s.Stats.cBalloonedPages);
pGMM->cBalloonedPages -= pGVM->gmm.s.Stats.cBalloonedPages;
/*
* Update the over-commitment management statistics.
*/
pGMM->cReservedPages -= pGVM->gmm.s.Stats.Reserved.cBasePages
+ pGVM->gmm.s.Stats.Reserved.cFixedPages
+ pGVM->gmm.s.Stats.Reserved.cShadowPages;
switch (pGVM->gmm.s.Stats.enmPolicy)
{
case GMMOCPOLICY_NO_OC:
break;
default:
/** @todo Update GMM->cOverCommittedPages */
break;
}
}
/* zap the GVM data. */
pGVM->gmm.s.Stats.enmPolicy = GMMOCPOLICY_INVALID;
pGVM->gmm.s.Stats.enmPriority = GMMPRIORITY_INVALID;
pGVM->gmm.s.Stats.fMayAllocate = false;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
gmmR0MutexRelease(pGMM);
/*
* Destroy the spinlock.
*/
RTSPINLOCK hSpinlock = NIL_RTSPINLOCK;
ASMAtomicXchgHandle(&pGVM->gmm.s.hChunkTlbSpinLock, NIL_RTSPINLOCK, &hSpinlock);
RTSpinlockDestroy(hSpinlock);
LogFlow(("GMMR0CleanupVM: returns\n"));
}
/**
* Scan one chunk for private pages belonging to the specified VM.
*
* @note This function may drop the giant mutex!
*
* @returns @c true if we've temporarily dropped the giant mutex, @c false if
* we didn't.
* @param pGMM Pointer to the GMM instance.
* @param pGVM The global VM handle.
* @param pChunk The chunk to scan.
*/
static bool gmmR0CleanupVMScanChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
{
Assert(!pGMM->fBoundMemoryMode || pChunk->hGVM == pGVM->hSelf);
/*
* Look for pages belonging to the VM.
* (Perform some internal checks while we're scanning.)
*/
#ifndef VBOX_STRICT
if (pChunk->cFree != GMM_CHUNK_NUM_PAGES)
#endif
{
unsigned cPrivate = 0;
unsigned cShared = 0;
unsigned cFree = 0;
gmmR0UnlinkChunk(pChunk); /* avoiding cFreePages updates. */
uint16_t hGVM = pGVM->hSelf;
unsigned iPage = (GMM_CHUNK_SIZE >> GUEST_PAGE_SHIFT);
while (iPage-- > 0)
if (GMM_PAGE_IS_PRIVATE(&pChunk->aPages[iPage]))
{
if (pChunk->aPages[iPage].Private.hGVM == hGVM)
{
/*
* Free the page.
*
* The reason for not using gmmR0FreePrivatePage here is that we
* must *not* cause the chunk to be freed from under us - we're in
* an AVL tree walk here.
*/
pChunk->aPages[iPage].u = 0;
pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
pChunk->aPages[iPage].Free.fZeroed = false;
pChunk->aPages[iPage].Free.iNext = pChunk->iFreeHead;
pChunk->iFreeHead = iPage;
pChunk->cPrivate--;
pChunk->cFree++;
pGVM->gmm.s.Stats.cPrivatePages--;
cFree++;
}
else
cPrivate++;
}
else if (GMM_PAGE_IS_FREE(&pChunk->aPages[iPage]))
cFree++;
else
cShared++;
gmmR0SelectSetAndLinkChunk(pGMM, pGVM, pChunk);
/*
* Did it add up?
*/
if (RT_UNLIKELY( pChunk->cFree != cFree
|| pChunk->cPrivate != cPrivate
|| pChunk->cShared != cShared))
{
SUPR0Printf("gmmR0CleanupVMScanChunk: Chunk %RKv/%#x has bogus stats - free=%d/%d private=%d/%d shared=%d/%d\n",
pChunk, pChunk->Core.Key, pChunk->cFree, cFree, pChunk->cPrivate, cPrivate, pChunk->cShared, cShared);
pChunk->cFree = cFree;
pChunk->cPrivate = cPrivate;
pChunk->cShared = cShared;
}
}
/*
* If not in bound memory mode, we should reset the hGVM field
* if it has our handle in it.
*/
if (pChunk->hGVM == pGVM->hSelf)
{
if (!g_pGMM->fBoundMemoryMode)
pChunk->hGVM = NIL_GVM_HANDLE;
else if (pChunk->cFree != GMM_CHUNK_NUM_PAGES)
{
SUPR0Printf("gmmR0CleanupVMScanChunk: %RKv/%#x: cFree=%#x - it should be 0 in bound mode!\n",
pChunk, pChunk->Core.Key, pChunk->cFree);
AssertMsgFailed(("%p/%#x: cFree=%#x - it should be 0 in bound mode!\n", pChunk, pChunk->Core.Key, pChunk->cFree));
gmmR0UnlinkChunk(pChunk);
pChunk->cFree = GMM_CHUNK_NUM_PAGES;
gmmR0SelectSetAndLinkChunk(pGMM, pGVM, pChunk);
}
}
/*
* Look for a mapping belonging to the terminating VM.
*/
GMMR0CHUNKMTXSTATE MtxState;
gmmR0ChunkMutexAcquire(&MtxState, pGMM, pChunk, GMMR0CHUNK_MTX_KEEP_GIANT);
unsigned cMappings = pChunk->cMappingsX;
for (unsigned i = 0; i < cMappings; i++)
if (pChunk->paMappingsX[i].pGVM == pGVM)
{
gmmR0ChunkMutexDropGiant(&MtxState);
RTR0MEMOBJ hMemObj = pChunk->paMappingsX[i].hMapObj;
cMappings--;
if (i < cMappings)
pChunk->paMappingsX[i] = pChunk->paMappingsX[cMappings];
pChunk->paMappingsX[cMappings].pGVM = NULL;
pChunk->paMappingsX[cMappings].hMapObj = NIL_RTR0MEMOBJ;
Assert(pChunk->cMappingsX - 1U == cMappings);
pChunk->cMappingsX = cMappings;
int rc = RTR0MemObjFree(hMemObj, false /* fFreeMappings (NA) */);
if (RT_FAILURE(rc))
{
SUPR0Printf("gmmR0CleanupVMScanChunk: %RKv/%#x: mapping #%x: RTRMemObjFree(%RKv,false) -> %d \n",
pChunk, pChunk->Core.Key, i, hMemObj, rc);
AssertRC(rc);
}
gmmR0ChunkMutexRelease(&MtxState, pChunk);
return true;
}
gmmR0ChunkMutexRelease(&MtxState, pChunk);
return false;
}
/**
* The initial resource reservations.
*
* This will make memory reservations according to policy and priority. If there aren't
* sufficient resources available to sustain the VM this function will fail and all
* future allocations requests will fail as well.
*
* These are just the initial reservations made very very early during the VM creation
* process and will be adjusted later in the GMMR0UpdateReservation call after the
* ring-3 init has completed.
*
* @returns VBox status code.
* @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
* @retval VERR_GMM_
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id - must be zero.
* @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
* This does not include MMIO2 and similar.
* @param cShadowPages The number of pages that may be allocated for shadow paging structures.
* @param cFixedPages The number of pages that may be allocated for fixed objects like the
* hyper heap, MMIO2 and similar.
* @param enmPolicy The OC policy to use on this VM.
* @param enmPriority The priority in an out-of-memory situation.
*
* @thread The creator thread / EMT(0).
*/
GMMR0DECL(int) GMMR0InitialReservation(PGVM pGVM, VMCPUID idCpu, uint64_t cBasePages, uint32_t cShadowPages,
uint32_t cFixedPages, GMMOCPOLICY enmPolicy, GMMPRIORITY enmPriority)
{
LogFlow(("GMMR0InitialReservation: pGVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x enmPolicy=%d enmPriority=%d\n",
pGVM, cBasePages, cShadowPages, cFixedPages, enmPolicy, enmPriority));
/*
* Validate, get basics and take the semaphore.
*/
AssertReturn(idCpu == 0, VERR_INVALID_CPU_ID);
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
AssertReturn(enmPolicy > GMMOCPOLICY_INVALID && enmPolicy < GMMOCPOLICY_END, VERR_INVALID_PARAMETER);
AssertReturn(enmPriority > GMMPRIORITY_INVALID && enmPriority < GMMPRIORITY_END, VERR_INVALID_PARAMETER);
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
if ( !pGVM->gmm.s.Stats.Reserved.cBasePages
&& !pGVM->gmm.s.Stats.Reserved.cFixedPages
&& !pGVM->gmm.s.Stats.Reserved.cShadowPages)
{
/*
* Check if we can accommodate this.
*/
/* ... later ... */
if (RT_SUCCESS(rc))
{
/*
* Update the records.
*/
pGVM->gmm.s.Stats.Reserved.cBasePages = cBasePages;
pGVM->gmm.s.Stats.Reserved.cFixedPages = cFixedPages;
pGVM->gmm.s.Stats.Reserved.cShadowPages = cShadowPages;
pGVM->gmm.s.Stats.enmPolicy = enmPolicy;
pGVM->gmm.s.Stats.enmPriority = enmPriority;
pGVM->gmm.s.Stats.fMayAllocate = true;
pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
pGMM->cRegisteredVMs++;
}
}
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0InitialReservation: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0InitialReservation.
*
* @returns see GMMR0InitialReservation.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0InitialReservationReq(PGVM pGVM, VMCPUID idCpu, PGMMINITIALRESERVATIONREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pGVM, VERR_INVALID_POINTER);
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0InitialReservation(pGVM, idCpu, pReq->cBasePages, pReq->cShadowPages,
pReq->cFixedPages, pReq->enmPolicy, pReq->enmPriority);
}
/**
* This updates the memory reservation with the additional MMIO2 and ROM pages.
*
* @returns VBox status code.
* @retval VERR_GMM_MEMORY_RESERVATION_DECLINED
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param cBasePages The number of pages that may be allocated for the base RAM and ROMs.
* This does not include MMIO2 and similar.
* @param cShadowPages The number of pages that may be allocated for shadow paging structures.
* @param cFixedPages The number of pages that may be allocated for fixed objects like the
* hyper heap, MMIO2 and similar.
*
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0UpdateReservation(PGVM pGVM, VMCPUID idCpu, uint64_t cBasePages,
uint32_t cShadowPages, uint32_t cFixedPages)
{
LogFlow(("GMMR0UpdateReservation: pGVM=%p cBasePages=%#llx cShadowPages=%#x cFixedPages=%#x\n",
pGVM, cBasePages, cShadowPages, cFixedPages));
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertReturn(cBasePages, VERR_INVALID_PARAMETER);
AssertReturn(cShadowPages, VERR_INVALID_PARAMETER);
AssertReturn(cFixedPages, VERR_INVALID_PARAMETER);
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
if ( pGVM->gmm.s.Stats.Reserved.cBasePages
&& pGVM->gmm.s.Stats.Reserved.cFixedPages
&& pGVM->gmm.s.Stats.Reserved.cShadowPages)
{
/*
* Check if we can accommodate this.
*/
/* ... later ... */
if (RT_SUCCESS(rc))
{
/*
* Update the records.
*/
pGMM->cReservedPages -= pGVM->gmm.s.Stats.Reserved.cBasePages
+ pGVM->gmm.s.Stats.Reserved.cFixedPages
+ pGVM->gmm.s.Stats.Reserved.cShadowPages;
pGMM->cReservedPages += cBasePages + cFixedPages + cShadowPages;
pGVM->gmm.s.Stats.Reserved.cBasePages = cBasePages;
pGVM->gmm.s.Stats.Reserved.cFixedPages = cFixedPages;
pGVM->gmm.s.Stats.Reserved.cShadowPages = cShadowPages;
}
}
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0UpdateReservation: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0UpdateReservation.
*
* @returns see GMMR0UpdateReservation.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0UpdateReservationReq(PGVM pGVM, VMCPUID idCpu, PGMMUPDATERESERVATIONREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0UpdateReservation(pGVM, idCpu, pReq->cBasePages, pReq->cShadowPages, pReq->cFixedPages);
}
#ifdef GMMR0_WITH_SANITY_CHECK
/**
* Performs sanity checks on a free set.
*
* @returns Error count.
*
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param pszSetName The set name.
* @param pszFunction The function from which it was called.
* @param uLine The line number.
*/
static uint32_t gmmR0SanityCheckSet(PGMM pGMM, PGMMCHUNKFREESET pSet, const char *pszSetName,
const char *pszFunction, unsigned uLineNo)
{
uint32_t cErrors = 0;
/*
* Count the free pages in all the chunks and match it against pSet->cFreePages.
*/
uint32_t cPages = 0;
for (unsigned i = 0; i < RT_ELEMENTS(pSet->apLists); i++)
{
for (PGMMCHUNK pCur = pSet->apLists[i]; pCur; pCur = pCur->pFreeNext)
{
/** @todo check that the chunk is hash into the right set. */
cPages += pCur->cFree;
}
}
if (RT_UNLIKELY(cPages != pSet->cFreePages))
{
SUPR0Printf("GMM insanity: found %#x pages in the %s set, expected %#x. (%s, line %u)\n",
cPages, pszSetName, pSet->cFreePages, pszFunction, uLineNo);
cErrors++;
}
return cErrors;
}
/**
* Performs some sanity checks on the GMM while owning lock.
*
* @returns Error count.
*
* @param pGMM Pointer to the GMM instance.
* @param pszFunction The function from which it is called.
* @param uLineNo The line number.
*/
static uint32_t gmmR0SanityCheck(PGMM pGMM, const char *pszFunction, unsigned uLineNo)
{
uint32_t cErrors = 0;
cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->PrivateX, "private", pszFunction, uLineNo);
cErrors += gmmR0SanityCheckSet(pGMM, &pGMM->Shared, "shared", pszFunction, uLineNo);
/** @todo add more sanity checks. */
return cErrors;
}
#endif /* GMMR0_WITH_SANITY_CHECK */
/**
* Looks up a chunk in the tree and fill in the TLB entry for it.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
* @param pTlbe Pointer to the TLB entry.
*
* @note Caller owns spinlock.
*/
static PGMMCHUNK gmmR0GetChunkSlow(PGMM pGMM, uint32_t idChunk, PGMMCHUNKTLBE pTlbe)
{
PGMMCHUNK pChunk = (PGMMCHUNK)RTAvlU32Get(&pGMM->pChunks, idChunk);
AssertMsgReturn(pChunk, ("Chunk %#x not found!\n", idChunk), NULL);
pTlbe->idChunk = idChunk;
pTlbe->pChunk = pChunk;
return pChunk;
}
/**
* Finds a allocation chunk, spin-locked.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
*/
DECLINLINE(PGMMCHUNK) gmmR0GetChunkLocked(PGMM pGMM, uint32_t idChunk)
{
/*
* Do a TLB lookup, branch if not in the TLB.
*/
PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(idChunk)];
PGMMCHUNK pChunk = pTlbe->pChunk;
if ( pChunk == NULL
|| pTlbe->idChunk != idChunk)
pChunk = gmmR0GetChunkSlow(pGMM, idChunk, pTlbe);
return pChunk;
}
/**
* Finds a allocation chunk.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the allocation chunk, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idChunk The ID of the chunk to find.
*/
DECLINLINE(PGMMCHUNK) gmmR0GetChunk(PGMM pGMM, uint32_t idChunk)
{
RTSpinlockAcquire(pGMM->hSpinLockTree);
PGMMCHUNK pChunk = gmmR0GetChunkLocked(pGMM, idChunk);
RTSpinlockRelease(pGMM->hSpinLockTree);
return pChunk;
}
/**
* Finds a page.
*
* This is not expected to fail and will bitch if it does.
*
* @returns Pointer to the page, NULL if not found.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
DECLINLINE(PGMMPAGE) gmmR0GetPage(PGMM pGMM, uint32_t idPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
if (RT_LIKELY(pChunk))
return &pChunk->aPages[idPage & GMM_PAGEID_IDX_MASK];
return NULL;
}
#if 0 /* unused */
/**
* Gets the host physical address for a page given by it's ID.
*
* @returns The host physical address or NIL_RTHCPHYS.
* @param pGMM Pointer to the GMM instance.
* @param idPage The ID of the page to find.
*/
DECLINLINE(RTHCPHYS) gmmR0GetPageHCPhys(PGMM pGMM, uint32_t idPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
if (RT_LIKELY(pChunk))
return RTR0MemObjGetPagePhysAddr(pChunk->hMemObj, idPage & GMM_PAGEID_IDX_MASK);
return NIL_RTHCPHYS;
}
#endif /* unused */
/**
* Selects the appropriate free list given the number of free pages.
*
* @returns Free list index.
* @param cFree The number of free pages in the chunk.
*/
DECLINLINE(unsigned) gmmR0SelectFreeSetList(unsigned cFree)
{
unsigned iList = cFree >> GMM_CHUNK_FREE_SET_SHIFT;
AssertMsg(iList < RT_SIZEOFMEMB(GMMCHUNKFREESET, apLists) / RT_SIZEOFMEMB(GMMCHUNKFREESET, apLists[0]),
("%d (%u)\n", iList, cFree));
return iList;
}
/**
* Unlinks the chunk from the free list it's currently on (if any).
*
* @param pChunk The allocation chunk.
*/
DECLINLINE(void) gmmR0UnlinkChunk(PGMMCHUNK pChunk)
{
PGMMCHUNKFREESET pSet = pChunk->pSet;
if (RT_LIKELY(pSet))
{
pSet->cFreePages -= pChunk->cFree;
pSet->idGeneration++;
PGMMCHUNK pPrev = pChunk->pFreePrev;
PGMMCHUNK pNext = pChunk->pFreeNext;
if (pPrev)
pPrev->pFreeNext = pNext;
else
pSet->apLists[gmmR0SelectFreeSetList(pChunk->cFree)] = pNext;
if (pNext)
pNext->pFreePrev = pPrev;
pChunk->pSet = NULL;
pChunk->pFreeNext = NULL;
pChunk->pFreePrev = NULL;
}
else
{
Assert(!pChunk->pFreeNext);
Assert(!pChunk->pFreePrev);
Assert(!pChunk->cFree);
}
}
/**
* Links the chunk onto the appropriate free list in the specified free set.
*
* If no free entries, it's not linked into any list.
*
* @param pChunk The allocation chunk.
* @param pSet The free set.
*/
DECLINLINE(void) gmmR0LinkChunk(PGMMCHUNK pChunk, PGMMCHUNKFREESET pSet)
{
Assert(!pChunk->pSet);
Assert(!pChunk->pFreeNext);
Assert(!pChunk->pFreePrev);
if (pChunk->cFree > 0)
{
pChunk->pSet = pSet;
pChunk->pFreePrev = NULL;
unsigned const iList = gmmR0SelectFreeSetList(pChunk->cFree);
pChunk->pFreeNext = pSet->apLists[iList];
if (pChunk->pFreeNext)
pChunk->pFreeNext->pFreePrev = pChunk;
pSet->apLists[iList] = pChunk;
pSet->cFreePages += pChunk->cFree;
pSet->idGeneration++;
}
}
/**
* Links the chunk onto the appropriate free list in the specified free set.
*
* If no free entries, it's not linked into any list.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the kernel-only VM instace data.
* @param pChunk The allocation chunk.
*/
DECLINLINE(void) gmmR0SelectSetAndLinkChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
{
PGMMCHUNKFREESET pSet;
if (pGMM->fBoundMemoryMode)
pSet = &pGVM->gmm.s.Private;
else if (pChunk->cShared)
pSet = &pGMM->Shared;
else
pSet = &pGMM->PrivateX;
gmmR0LinkChunk(pChunk, pSet);
}
/**
* Frees a Chunk ID.
*
* @param pGMM Pointer to the GMM instance.
* @param idChunk The Chunk ID to free.
*/
static void gmmR0FreeChunkId(PGMM pGMM, uint32_t idChunk)
{
AssertReturnVoid(idChunk != NIL_GMM_CHUNKID);
RTSpinlockAcquire(pGMM->hSpinLockChunkId); /* We could probably skip the locking here, I think. */
AssertMsg(ASMBitTest(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk));
ASMAtomicBitClear(&pGMM->bmChunkId[0], idChunk);
RTSpinlockRelease(pGMM->hSpinLockChunkId);
}
/**
* Allocates a new Chunk ID.
*
* @returns The Chunk ID.
* @param pGMM Pointer to the GMM instance.
*/
static uint32_t gmmR0AllocateChunkId(PGMM pGMM)
{
AssertCompile(!((GMM_CHUNKID_LAST + 1) & 31)); /* must be a multiple of 32 */
AssertCompile(NIL_GMM_CHUNKID == 0);
RTSpinlockAcquire(pGMM->hSpinLockChunkId);
/*
* Try the next sequential one.
*/
int32_t idChunk = ++pGMM->idChunkPrev;
if ( (uint32_t)idChunk <= GMM_CHUNKID_LAST
&& idChunk > NIL_GMM_CHUNKID)
{
if (!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk))
{
RTSpinlockRelease(pGMM->hSpinLockChunkId);
return idChunk;
}
/*
* Scan sequentially from the last one.
*/
if ((uint32_t)idChunk < GMM_CHUNKID_LAST)
{
idChunk = ASMBitNextClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1, idChunk);
if ( idChunk > NIL_GMM_CHUNKID
&& (uint32_t)idChunk <= GMM_CHUNKID_LAST)
{
AssertMsgReturnStmt(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk),
RTSpinlockRelease(pGMM->hSpinLockChunkId), NIL_GMM_CHUNKID);
pGMM->idChunkPrev = idChunk;
RTSpinlockRelease(pGMM->hSpinLockChunkId);
return idChunk;
}
}
}
/*
* Ok, scan from the start.
* We're not racing anyone, so there is no need to expect failures or have restart loops.
*/
idChunk = ASMBitFirstClear(&pGMM->bmChunkId[0], GMM_CHUNKID_LAST + 1);
AssertMsgReturnStmt(idChunk > NIL_GMM_CHUNKID && (uint32_t)idChunk <= GMM_CHUNKID_LAST, ("%#x\n", idChunk),
RTSpinlockRelease(pGMM->hSpinLockChunkId), NIL_GVM_HANDLE);
AssertMsgReturnStmt(!ASMAtomicBitTestAndSet(&pGMM->bmChunkId[0], idChunk), ("%#x\n", idChunk),
RTSpinlockRelease(pGMM->hSpinLockChunkId), NIL_GMM_CHUNKID);
pGMM->idChunkPrev = idChunk;
RTSpinlockRelease(pGMM->hSpinLockChunkId);
return idChunk;
}
/**
* Allocates one private page.
*
* Worker for gmmR0AllocatePages.
*
* @param pChunk The chunk to allocate it from.
* @param hGVM The GVM handle of the VM requesting memory.
* @param pPageDesc The page descriptor.
*/
static void gmmR0AllocatePage(PGMMCHUNK pChunk, uint32_t hGVM, PGMMPAGEDESC pPageDesc)
{
/* update the chunk stats. */
if (pChunk->hGVM == NIL_GVM_HANDLE)
pChunk->hGVM = hGVM;
Assert(pChunk->cFree);
pChunk->cFree--;
pChunk->cPrivate++;
/* unlink the first free page. */
const uint32_t iPage = pChunk->iFreeHead;
AssertReleaseMsg(iPage < RT_ELEMENTS(pChunk->aPages), ("%d\n", iPage));
PGMMPAGE pPage = &pChunk->aPages[iPage];
Assert(GMM_PAGE_IS_FREE(pPage));
pChunk->iFreeHead = pPage->Free.iNext;
Log3(("A pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x iNext=%#x\n",
pPage, iPage, (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage,
pPage->Common.u2State, pChunk->iFreeHead, pPage->Free.iNext));
bool const fZeroed = pPage->Free.fZeroed;
/* make the page private. */
pPage->u = 0;
AssertCompile(GMM_PAGE_STATE_PRIVATE == 0);
pPage->Private.hGVM = hGVM;
AssertCompile(NIL_RTHCPHYS >= GMM_GCPHYS_LAST);
AssertCompile(GMM_GCPHYS_UNSHAREABLE >= GMM_GCPHYS_LAST);
if (pPageDesc->HCPhysGCPhys <= GMM_GCPHYS_LAST)
pPage->Private.pfn = pPageDesc->HCPhysGCPhys >> GUEST_PAGE_SHIFT;
else
pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE; /* unshareable / unassigned - same thing. */
/* update the page descriptor. */
pPageDesc->idSharedPage = NIL_GMM_PAGEID;
pPageDesc->idPage = (pChunk->Core.Key << GMM_CHUNKID_SHIFT) | iPage;
RTHCPHYS const HCPhys = RTR0MemObjGetPagePhysAddr(pChunk->hMemObj, iPage);
Assert(HCPhys != NIL_RTHCPHYS); Assert(HCPhys < NIL_GMMPAGEDESC_PHYS);
pPageDesc->HCPhysGCPhys = HCPhys;
pPageDesc->fZeroed = fZeroed;
}
/**
* Picks the free pages from a chunk.
*
* @returns The new page descriptor table index.
* @param pChunk The chunk.
* @param hGVM The affinity of the chunk. NIL_GVM_HANDLE for no
* affinity.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesFromChunk(PGMMCHUNK pChunk, uint16_t const hGVM, uint32_t iPage, uint32_t cPages,
PGMMPAGEDESC paPages)
{
PGMMCHUNKFREESET pSet = pChunk->pSet; Assert(pSet);
gmmR0UnlinkChunk(pChunk);
for (; pChunk->cFree && iPage < cPages; iPage++)
gmmR0AllocatePage(pChunk, hGVM, &paPages[iPage]);
gmmR0LinkChunk(pChunk, pSet);
return iPage;
}
/**
* Registers a new chunk of memory.
*
* This is called by gmmR0AllocateOneChunk and GMMR0AllocateLargePage.
*
* In the GMMR0AllocateLargePage case the GMM_CHUNK_FLAGS_LARGE_PAGE flag is
* set and the chunk will be registered as fully allocated to save time.
*
* @returns VBox status code. On success, the giant GMM lock will be held, the
* caller must release it (ugly).
* @param pGMM Pointer to the GMM instance.
* @param pSet Pointer to the set.
* @param hMemObj The memory object for the chunk.
* @param hGVM The affinity of the chunk. NIL_GVM_HANDLE for no
* affinity.
* @param pSession Same as @a hGVM.
* @param fChunkFlags The chunk flags, GMM_CHUNK_FLAGS_XXX.
* @param cPages The number of pages requested. Zero for large pages.
* @param paPages The page descriptor table (input + output). NULL for
* large pages.
* @param piPage The pointer to the page descriptor table index variable.
* This will be updated. NULL for large pages.
* @param ppChunk Chunk address (out).
*
* @remarks The caller must not own the giant GMM mutex.
* The giant GMM mutex will be acquired and returned acquired in
* the success path. On failure, no locks will be held.
*/
static int gmmR0RegisterChunk(PGMM pGMM, PGMMCHUNKFREESET pSet, RTR0MEMOBJ hMemObj, uint16_t hGVM, PSUPDRVSESSION pSession,
uint16_t fChunkFlags, uint32_t cPages, PGMMPAGEDESC paPages, uint32_t *piPage, PGMMCHUNK *ppChunk)
{
/*
* Validate input & state.
*/
Assert(pGMM->hMtxOwner != RTThreadNativeSelf());
Assert(hGVM != NIL_GVM_HANDLE || pGMM->fBoundMemoryMode);
Assert(fChunkFlags == 0 || fChunkFlags == GMM_CHUNK_FLAGS_LARGE_PAGE);
if (!(fChunkFlags &= GMM_CHUNK_FLAGS_LARGE_PAGE))
{
AssertPtr(paPages);
AssertPtr(piPage);
Assert(cPages > 0);
Assert(cPages > *piPage);
}
else
{
Assert(cPages == 0);
Assert(!paPages);
Assert(!piPage);
}
#ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
/*
* Get a ring-0 mapping of the object.
*/
uint8_t *pbMapping = (uint8_t *)RTR0MemObjAddress(hMemObj);
if (!pbMapping)
{
RTR0MEMOBJ hMapObj;
int rc = RTR0MemObjMapKernel(&hMapObj, hMemObj, (void *)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE);
if (RT_SUCCESS(rc))
pbMapping = (uint8_t *)RTR0MemObjAddress(hMapObj);
else
return rc;
AssertPtr(pbMapping);
}
#endif
/*
* Allocate a chunk and an ID for it.
*/
int rc;
PGMMCHUNK pChunk = (PGMMCHUNK)RTMemAllocZ(sizeof(*pChunk));
if (pChunk)
{
pChunk->Core.Key = gmmR0AllocateChunkId(pGMM);
if ( pChunk->Core.Key != NIL_GMM_CHUNKID
&& pChunk->Core.Key <= GMM_CHUNKID_LAST)
{
/*
* Initialize it.
*/
pChunk->hMemObj = hMemObj;
#ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
pChunk->pbMapping = pbMapping;
#endif
pChunk->hGVM = hGVM;
pChunk->idNumaNode = gmmR0GetCurrentNumaNodeId();
pChunk->iChunkMtx = UINT8_MAX;
pChunk->fFlags = fChunkFlags;
pChunk->uidOwner = pSession ? SUPR0GetSessionUid(pSession) : NIL_RTUID;
/*pChunk->cShared = 0; */
uint32_t const iDstPageFirst = piPage ? *piPage : cPages;
if (!(fChunkFlags & GMM_CHUNK_FLAGS_LARGE_PAGE))
{
/*
* Allocate the requested number of pages from the start of the chunk,
* queue the rest (if any) on the free list.
*/
uint32_t const cPagesAlloc = RT_MIN(cPages - iDstPageFirst, GMM_CHUNK_NUM_PAGES);
pChunk->cPrivate = cPagesAlloc;
pChunk->cFree = GMM_CHUNK_NUM_PAGES - cPagesAlloc;
pChunk->iFreeHead = GMM_CHUNK_NUM_PAGES > cPagesAlloc ? cPagesAlloc : UINT16_MAX;
/* Alloc pages: */
uint32_t const idPageChunk = pChunk->Core.Key << GMM_CHUNKID_SHIFT;
uint32_t iDstPage = iDstPageFirst;
uint32_t iPage;
for (iPage = 0; iPage < cPagesAlloc; iPage++, iDstPage++)
{
if (paPages[iDstPage].HCPhysGCPhys <= GMM_GCPHYS_LAST)
pChunk->aPages[iPage].Private.pfn = paPages[iDstPage].HCPhysGCPhys >> GUEST_PAGE_SHIFT;
else
pChunk->aPages[iPage].Private.pfn = GMM_PAGE_PFN_UNSHAREABLE; /* unshareable / unassigned - same thing. */
pChunk->aPages[iPage].Private.hGVM = hGVM;
pChunk->aPages[iPage].Private.u2State = GMM_PAGE_STATE_PRIVATE;
paPages[iDstPage].HCPhysGCPhys = RTR0MemObjGetPagePhysAddr(hMemObj, iPage);
paPages[iDstPage].fZeroed = true;
paPages[iDstPage].idPage = idPageChunk | iPage;
paPages[iDstPage].idSharedPage = NIL_GMM_PAGEID;
}
*piPage = iDstPage;
/* Build free list: */
if (iPage < RT_ELEMENTS(pChunk->aPages))
{
Assert(pChunk->iFreeHead == iPage);
for (; iPage < RT_ELEMENTS(pChunk->aPages) - 1; iPage++)
{
pChunk->aPages[iPage].Free.u2State = GMM_PAGE_STATE_FREE;
pChunk->aPages[iPage].Free.fZeroed = true;
pChunk->aPages[iPage].Free.iNext = iPage + 1;
}
pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.u2State = GMM_PAGE_STATE_FREE;
pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.fZeroed = true;
pChunk->aPages[RT_ELEMENTS(pChunk->aPages) - 1].Free.iNext = UINT16_MAX;
}
else
Assert(pChunk->iFreeHead == UINT16_MAX);
}
else
{
/*
* Large page: Mark all pages as privately allocated (watered down gmmR0AllocatePage).
*/
pChunk->cFree = 0;
pChunk->cPrivate = GMM_CHUNK_NUM_PAGES;
pChunk->iFreeHead = UINT16_MAX;
for (unsigned iPage = 0; iPage < RT_ELEMENTS(pChunk->aPages); iPage++)
{
pChunk->aPages[iPage].Private.pfn = GMM_PAGE_PFN_UNSHAREABLE;
pChunk->aPages[iPage].Private.hGVM = hGVM;
pChunk->aPages[iPage].Private.u2State = GMM_PAGE_STATE_PRIVATE;
}
}
/*
* Zero the memory if it wasn't zeroed by the host already.
* This simplifies keeping secret kernel bits from userland and brings
* everyone to the same level wrt allocation zeroing.
*/
rc = VINF_SUCCESS;
if (!RTR0MemObjWasZeroInitialized(hMemObj))
{
#ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
if (!(fChunkFlags & GMM_CHUNK_FLAGS_LARGE_PAGE))
{
for (uint32_t iPage = 0; iPage < GMM_CHUNK_SIZE / HOST_PAGE_SIZE; iPage++)
{
void *pvPage = NULL;
rc = SUPR0HCPhysToVirt(RTR0MemObjGetPagePhysAddr(hMemObj, iPage), &pvPage);
AssertRCBreak(rc);
RT_BZERO(pvPage, HOST_PAGE_SIZE);
}
}
else
{
/* Can do the whole large page in one go. */
void *pvPage = NULL;
rc = SUPR0HCPhysToVirt(RTR0MemObjGetPagePhysAddr(hMemObj, 0), &pvPage);
AssertRC(rc);
if (RT_SUCCESS(rc))
RT_BZERO(pvPage, GMM_CHUNK_SIZE);
}
#else
RT_BZERO(pbMapping, GMM_CHUNK_SIZE);
#endif
}
if (RT_SUCCESS(rc))
{
*ppChunk = pChunk;
/*
* Allocate a Chunk ID and insert it into the tree.
* This has to be done behind the mutex of course.
*/
rc = gmmR0MutexAcquire(pGMM);
if (RT_SUCCESS(rc))
{
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
RTSpinlockAcquire(pGMM->hSpinLockTree);
if (RTAvlU32Insert(&pGMM->pChunks, &pChunk->Core))
{
pGMM->cChunks++;
RTListAppend(&pGMM->ChunkList, &pChunk->ListNode);
RTSpinlockRelease(pGMM->hSpinLockTree);
gmmR0LinkChunk(pChunk, pSet);
LogFlow(("gmmR0RegisterChunk: pChunk=%p id=%#x cChunks=%d\n", pChunk, pChunk->Core.Key, pGMM->cChunks));
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
return VINF_SUCCESS;
}
/*
* Bail out.
*/
RTSpinlockRelease(pGMM->hSpinLockTree);
rc = VERR_GMM_CHUNK_INSERT;
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
}
*ppChunk = NULL;
}
/* Undo any page allocations. */
if (!(fChunkFlags & GMM_CHUNK_FLAGS_LARGE_PAGE))
{
uint32_t const cToFree = pChunk->cPrivate;
Assert(*piPage - iDstPageFirst == cToFree);
for (uint32_t iDstPage = iDstPageFirst, iPage = 0; iPage < cToFree; iPage++, iDstPage++)
{
paPages[iDstPageFirst].fZeroed = false;
if (pChunk->aPages[iPage].Private.pfn == GMM_PAGE_PFN_UNSHAREABLE)
paPages[iDstPageFirst].HCPhysGCPhys = NIL_GMMPAGEDESC_PHYS;
else
paPages[iDstPageFirst].HCPhysGCPhys = (RTHCPHYS)pChunk->aPages[iPage].Private.pfn << GUEST_PAGE_SHIFT;
paPages[iDstPageFirst].idPage = NIL_GMM_PAGEID;
paPages[iDstPageFirst].idSharedPage = NIL_GMM_PAGEID;
}
*piPage = iDstPageFirst;
}
gmmR0FreeChunkId(pGMM, pChunk->Core.Key);
}
else
rc = VERR_GMM_CHUNK_INSERT;
RTMemFree(pChunk);
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Allocate a new chunk, immediately pick the requested pages from it, and adds
* what's remaining to the specified free set.
*
* @note This will leave the giant mutex while allocating the new chunk!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the kernel-only VM instace data.
* @param pSet Pointer to the free set.
* @param cPages The number of pages requested.
* @param paPages The page descriptor table (input + output).
* @param piPage The pointer to the page descriptor table index variable.
* This will be updated.
*/
static int gmmR0AllocateChunkNew(PGMM pGMM, PGVM pGVM, PGMMCHUNKFREESET pSet, uint32_t cPages,
PGMMPAGEDESC paPages, uint32_t *piPage)
{
gmmR0MutexRelease(pGMM);
RTR0MEMOBJ hMemObj;
int rc;
#ifdef VBOX_WITH_LINEAR_HOST_PHYS_MEM
if (pGMM->fHasWorkingAllocPhysNC)
rc = RTR0MemObjAllocPhysNC(&hMemObj, GMM_CHUNK_SIZE, NIL_RTHCPHYS);
else
#endif
rc = RTR0MemObjAllocPage(&hMemObj, GMM_CHUNK_SIZE, false /*fExecutable*/);
if (RT_SUCCESS(rc))
{
PGMMCHUNK pIgnored;
rc = gmmR0RegisterChunk(pGMM, pSet, hMemObj, pGVM->hSelf, pGVM->pSession, 0 /*fChunkFlags*/,
cPages, paPages, piPage, &pIgnored);
if (RT_SUCCESS(rc))
return VINF_SUCCESS;
/* bail out */
RTR0MemObjFree(hMemObj, true /* fFreeMappings */);
}
int rc2 = gmmR0MutexAcquire(pGMM);
AssertRCReturn(rc2, RT_FAILURE(rc) ? rc : rc2);
return rc;
}
/**
* As a last restort we'll pick any page we can get.
*
* @returns The new page descriptor table index.
* @param pSet The set to pick from.
* @param pGVM Pointer to the global VM structure.
* @param uidSelf The UID of the caller.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesIndiscriminately(PGMMCHUNKFREESET pSet, PGVM pGVM, RTUID uidSelf,
uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
unsigned iList = RT_ELEMENTS(pSet->apLists);
while (iList-- > 0)
{
PGMMCHUNK pChunk = pSet->apLists[iList];
while (pChunk)
{
PGMMCHUNK pNext = pChunk->pFreeNext;
if ( pChunk->uidOwner == uidSelf
|| ( pChunk->cMappingsX == 0
&& pChunk->cFree == (GMM_CHUNK_SIZE >> GUEST_PAGE_SHIFT)))
{
iPage = gmmR0AllocatePagesFromChunk(pChunk, pGVM->hSelf, iPage, cPages, paPages);
if (iPage >= cPages)
return iPage;
}
pChunk = pNext;
}
}
return iPage;
}
/**
* Pick pages from empty chunks on the same NUMA node.
*
* @returns The new page descriptor table index.
* @param pSet The set to pick from.
* @param pGVM Pointer to the global VM structure.
* @param uidSelf The UID of the caller.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesFromEmptyChunksOnSameNode(PGMMCHUNKFREESET pSet, PGVM pGVM, RTUID uidSelf,
uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
PGMMCHUNK pChunk = pSet->apLists[GMM_CHUNK_FREE_SET_UNUSED_LIST];
if (pChunk)
{
uint16_t const idNumaNode = gmmR0GetCurrentNumaNodeId();
while (pChunk)
{
PGMMCHUNK pNext = pChunk->pFreeNext;
if ( pChunk->idNumaNode == idNumaNode
&& ( pChunk->uidOwner == uidSelf
|| pChunk->cMappingsX == 0))
{
pChunk->hGVM = pGVM->hSelf;
pChunk->uidOwner = uidSelf;
iPage = gmmR0AllocatePagesFromChunk(pChunk, pGVM->hSelf, iPage, cPages, paPages);
if (iPage >= cPages)
{
pGVM->gmm.s.idLastChunkHint = pChunk->cFree ? pChunk->Core.Key : NIL_GMM_CHUNKID;
return iPage;
}
}
pChunk = pNext;
}
}
return iPage;
}
/**
* Pick pages from non-empty chunks on the same NUMA node.
*
* @returns The new page descriptor table index.
* @param pSet The set to pick from.
* @param pGVM Pointer to the global VM structure.
* @param uidSelf The UID of the caller.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesFromSameNode(PGMMCHUNKFREESET pSet, PGVM pGVM, RTUID const uidSelf,
uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
/** @todo start by picking from chunks with about the right size first? */
uint16_t const idNumaNode = gmmR0GetCurrentNumaNodeId();
unsigned iList = GMM_CHUNK_FREE_SET_UNUSED_LIST;
while (iList-- > 0)
{
PGMMCHUNK pChunk = pSet->apLists[iList];
while (pChunk)
{
PGMMCHUNK pNext = pChunk->pFreeNext;
if ( pChunk->idNumaNode == idNumaNode
&& pChunk->uidOwner == uidSelf)
{
iPage = gmmR0AllocatePagesFromChunk(pChunk, pGVM->hSelf, iPage, cPages, paPages);
if (iPage >= cPages)
{
pGVM->gmm.s.idLastChunkHint = pChunk->cFree ? pChunk->Core.Key : NIL_GMM_CHUNKID;
return iPage;
}
}
pChunk = pNext;
}
}
return iPage;
}
/**
* Pick pages that are in chunks already associated with the VM.
*
* @returns The new page descriptor table index.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the global VM structure.
* @param pSet The set to pick from.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesAssociatedWithVM(PGMM pGMM, PGVM pGVM, PGMMCHUNKFREESET pSet,
uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
uint16_t const hGVM = pGVM->hSelf;
/* Hint. */
if (pGVM->gmm.s.idLastChunkHint != NIL_GMM_CHUNKID)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, pGVM->gmm.s.idLastChunkHint);
if (pChunk && pChunk->cFree)
{
iPage = gmmR0AllocatePagesFromChunk(pChunk, hGVM, iPage, cPages, paPages);
if (iPage >= cPages)
return iPage;
}
}
/* Scan. */
for (unsigned iList = 0; iList < RT_ELEMENTS(pSet->apLists); iList++)
{
PGMMCHUNK pChunk = pSet->apLists[iList];
while (pChunk)
{
PGMMCHUNK pNext = pChunk->pFreeNext;
if (pChunk->hGVM == hGVM)
{
iPage = gmmR0AllocatePagesFromChunk(pChunk, hGVM, iPage, cPages, paPages);
if (iPage >= cPages)
{
pGVM->gmm.s.idLastChunkHint = pChunk->cFree ? pChunk->Core.Key : NIL_GMM_CHUNKID;
return iPage;
}
}
pChunk = pNext;
}
}
return iPage;
}
/**
* Pick pages in bound memory mode.
*
* @returns The new page descriptor table index.
* @param pGVM Pointer to the global VM structure.
* @param iPage The current page descriptor table index.
* @param cPages The total number of pages to allocate.
* @param paPages The page descriptor table (input + ouput).
*/
static uint32_t gmmR0AllocatePagesInBoundMode(PGVM pGVM, uint32_t iPage, uint32_t cPages, PGMMPAGEDESC paPages)
{
for (unsigned iList = 0; iList < RT_ELEMENTS(pGVM->gmm.s.Private.apLists); iList++)
{
PGMMCHUNK pChunk = pGVM->gmm.s.Private.apLists[iList];
while (pChunk)
{
Assert(pChunk->hGVM == pGVM->hSelf);
PGMMCHUNK pNext = pChunk->pFreeNext;
iPage = gmmR0AllocatePagesFromChunk(pChunk, pGVM->hSelf, iPage, cPages, paPages);
if (iPage >= cPages)
return iPage;
pChunk = pNext;
}
}
return iPage;
}
/**
* Checks if we should start picking pages from chunks of other VMs because
* we're getting close to the system memory or reserved limit.
*
* @returns @c true if we should, @c false if we should first try allocate more
* chunks.
*/
static bool gmmR0ShouldAllocatePagesInOtherChunksBecauseOfLimits(PGVM pGVM)
{
/*
* Don't allocate a new chunk if we're
*/
uint64_t cPgReserved = pGVM->gmm.s.Stats.Reserved.cBasePages
+ pGVM->gmm.s.Stats.Reserved.cFixedPages
- pGVM->gmm.s.Stats.cBalloonedPages
/** @todo what about shared pages? */;
uint64_t cPgAllocated = pGVM->gmm.s.Stats.Allocated.cBasePages
+ pGVM->gmm.s.Stats.Allocated.cFixedPages;
uint64_t cPgDelta = cPgReserved - cPgAllocated;
if (cPgDelta < GMM_CHUNK_NUM_PAGES * 4)
return true;
/** @todo make the threshold configurable, also test the code to see if
* this ever kicks in (we might be reserving too much or smth). */
/*
* Check how close we're to the max memory limit and how many fragments
* there are?...
*/
/** @todo */
return false;
}
/**
* Checks if we should start picking pages from chunks of other VMs because
* there is a lot of free pages around.
*
* @returns @c true if we should, @c false if we should first try allocate more
* chunks.
*/
static bool gmmR0ShouldAllocatePagesInOtherChunksBecauseOfLotsFree(PGMM pGMM)
{
/*
* Setting the limit at 16 chunks (32 MB) at the moment.
*/
if (pGMM->PrivateX.cFreePages >= GMM_CHUNK_NUM_PAGES * 16)
return true;
return false;
}
/**
* Common worker for GMMR0AllocateHandyPages and GMMR0AllocatePages.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the VM.
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors. See GMMPAGEDESC for
* details on what is expected on input.
* @param enmAccount The account to charge.
*
* @remarks Caller owns the giant GMM lock.
*/
static int gmmR0AllocatePagesNew(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
Assert(pGMM->hMtxOwner == RTThreadNativeSelf());
/*
* Check allocation limits.
*/
if (RT_LIKELY(pGMM->cAllocatedPages + cPages <= pGMM->cMaxPages))
{ /* likely */ }
else
return VERR_GMM_HIT_GLOBAL_LIMIT;
switch (enmAccount)
{
case GMMACCOUNT_BASE:
if (RT_LIKELY( pGVM->gmm.s.Stats.Allocated.cBasePages + pGVM->gmm.s.Stats.cBalloonedPages + cPages
<= pGVM->gmm.s.Stats.Reserved.cBasePages))
{ /* likely */ }
else
{
Log(("gmmR0AllocatePages:Base: Reserved=%#llx Allocated+Ballooned+Requested=%#llx+%#llx+%#x!\n",
pGVM->gmm.s.Stats.Reserved.cBasePages, pGVM->gmm.s.Stats.Allocated.cBasePages,
pGVM->gmm.s.Stats.cBalloonedPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_SHADOW:
if (RT_LIKELY(pGVM->gmm.s.Stats.Allocated.cShadowPages + cPages <= pGVM->gmm.s.Stats.Reserved.cShadowPages))
{ /* likely */ }
else
{
Log(("gmmR0AllocatePages:Shadow: Reserved=%#x Allocated+Requested=%#x+%#x!\n",
pGVM->gmm.s.Stats.Reserved.cShadowPages, pGVM->gmm.s.Stats.Allocated.cShadowPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
case GMMACCOUNT_FIXED:
if (RT_LIKELY(pGVM->gmm.s.Stats.Allocated.cFixedPages + cPages <= pGVM->gmm.s.Stats.Reserved.cFixedPages))
{ /* likely */ }
else
{
Log(("gmmR0AllocatePages:Fixed: Reserved=%#x Allocated+Requested=%#x+%#x!\n",
pGVM->gmm.s.Stats.Reserved.cFixedPages, pGVM->gmm.s.Stats.Allocated.cFixedPages, cPages));
return VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
/*
* Update the accounts before we proceed because we might be leaving the
* protection of the global mutex and thus run the risk of permitting
* too much memory to be allocated.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE: pGVM->gmm.s.Stats.Allocated.cBasePages += cPages; break;
case GMMACCOUNT_SHADOW: pGVM->gmm.s.Stats.Allocated.cShadowPages += cPages; break;
case GMMACCOUNT_FIXED: pGVM->gmm.s.Stats.Allocated.cFixedPages += cPages; break;
default: AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
pGVM->gmm.s.Stats.cPrivatePages += cPages;
pGMM->cAllocatedPages += cPages;
/*
* Bound mode is also relatively straightforward.
*/
uint32_t iPage = 0;
int rc = VINF_SUCCESS;
if (pGMM->fBoundMemoryMode)
{
iPage = gmmR0AllocatePagesInBoundMode(pGVM, iPage, cPages, paPages);
if (iPage < cPages)
do
rc = gmmR0AllocateChunkNew(pGMM, pGVM, &pGVM->gmm.s.Private, cPages, paPages, &iPage);
while (iPage < cPages && RT_SUCCESS(rc));
}
/*
* Shared mode is trickier as we should try archive the same locality as
* in bound mode, but smartly make use of non-full chunks allocated by
* other VMs if we're low on memory.
*/
else
{
RTUID const uidSelf = SUPR0GetSessionUid(pGVM->pSession);
/* Pick the most optimal pages first. */
iPage = gmmR0AllocatePagesAssociatedWithVM(pGMM, pGVM, &pGMM->PrivateX, iPage, cPages, paPages);
if (iPage < cPages)
{
/* Maybe we should try getting pages from chunks "belonging" to
other VMs before allocating more chunks? */
bool fTriedOnSameAlready = false;
if (gmmR0ShouldAllocatePagesInOtherChunksBecauseOfLimits(pGVM))
{
iPage = gmmR0AllocatePagesFromSameNode(&pGMM->PrivateX, pGVM, uidSelf, iPage, cPages, paPages);
fTriedOnSameAlready = true;
}
/* Allocate memory from empty chunks. */
if (iPage < cPages)
iPage = gmmR0AllocatePagesFromEmptyChunksOnSameNode(&pGMM->PrivateX, pGVM, uidSelf, iPage, cPages, paPages);
/* Grab empty shared chunks. */
if (iPage < cPages)
iPage = gmmR0AllocatePagesFromEmptyChunksOnSameNode(&pGMM->Shared, pGVM, uidSelf, iPage, cPages, paPages);
/* If there is a lof of free pages spread around, try not waste
system memory on more chunks. (Should trigger defragmentation.) */
if ( !fTriedOnSameAlready
&& gmmR0ShouldAllocatePagesInOtherChunksBecauseOfLotsFree(pGMM))
{
iPage = gmmR0AllocatePagesFromSameNode(&pGMM->PrivateX, pGVM, uidSelf, iPage, cPages, paPages);
if (iPage < cPages)
iPage = gmmR0AllocatePagesIndiscriminately(&pGMM->PrivateX, pGVM, uidSelf, iPage, cPages, paPages);
}
/*
* Ok, try allocate new chunks.
*/
if (iPage < cPages)
{
do
rc = gmmR0AllocateChunkNew(pGMM, pGVM, &pGMM->PrivateX, cPages, paPages, &iPage);
while (iPage < cPages && RT_SUCCESS(rc));
#if 0 /* We cannot mix chunks with different UIDs. */
/* If the host is out of memory, take whatever we can get. */
if ( (rc == VERR_NO_MEMORY || rc == VERR_NO_PHYS_MEMORY)
&& pGMM->PrivateX.cFreePages + pGMM->Shared.cFreePages >= cPages - iPage)
{
iPage = gmmR0AllocatePagesIndiscriminately(&pGMM->PrivateX, pGVM, iPage, cPages, paPages);
if (iPage < cPages)
iPage = gmmR0AllocatePagesIndiscriminately(&pGMM->Shared, pGVM, iPage, cPages, paPages);
AssertRelease(iPage == cPages);
rc = VINF_SUCCESS;
}
#endif
}
}
}
/*
* Clean up on failure. Since this is bound to be a low-memory condition
* we will give back any empty chunks that might be hanging around.
*/
if (RT_SUCCESS(rc))
{ /* likely */ }
else
{
/* Update the statistics. */
pGVM->gmm.s.Stats.cPrivatePages -= cPages;
pGMM->cAllocatedPages -= cPages - iPage;
switch (enmAccount)
{
case GMMACCOUNT_BASE: pGVM->gmm.s.Stats.Allocated.cBasePages -= cPages; break;
case GMMACCOUNT_SHADOW: pGVM->gmm.s.Stats.Allocated.cShadowPages -= cPages; break;
case GMMACCOUNT_FIXED: pGVM->gmm.s.Stats.Allocated.cFixedPages -= cPages; break;
default: AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
/* Release the pages. */
while (iPage-- > 0)
{
uint32_t idPage = paPages[iPage].idPage;
PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
if (RT_LIKELY(pPage))
{
Assert(GMM_PAGE_IS_PRIVATE(pPage));
Assert(pPage->Private.hGVM == pGVM->hSelf);
gmmR0FreePrivatePage(pGMM, pGVM, idPage, pPage);
}
else
AssertMsgFailed(("idPage=%#x\n", idPage));
paPages[iPage].idPage = NIL_GMM_PAGEID;
paPages[iPage].idSharedPage = NIL_GMM_PAGEID;
paPages[iPage].HCPhysGCPhys = NIL_GMMPAGEDESC_PHYS;
paPages[iPage].fZeroed = false;
}
/* Free empty chunks. */
/** @todo */
/* return the fail status on failure */
return rc;
}
return VINF_SUCCESS;
}
/**
* Updates the previous allocations and allocates more pages.
*
* The handy pages are always taken from the 'base' memory account.
* The allocated pages are not cleared and will contains random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_PAGE_NOT_FOUND if one of the pages to update wasn't found.
* @retval VERR_GMM_PAGE_NOT_PRIVATE if one of the pages to update wasn't a
* private page.
* @retval VERR_GMM_PAGE_NOT_SHARED if one of the pages to update wasn't a
* shared page.
* @retval VERR_GMM_NOT_PAGE_OWNER if one of the pages to be updated wasn't
* owned by the VM.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param cPagesToUpdate The number of pages to update (starting from the head).
* @param cPagesToAlloc The number of pages to allocate (starting from the head).
* @param paPages The array of page descriptors.
* See GMMPAGEDESC for details on what is expected on input.
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0AllocateHandyPages(PGVM pGVM, VMCPUID idCpu, uint32_t cPagesToUpdate,
uint32_t cPagesToAlloc, PGMMPAGEDESC paPages)
{
LogFlow(("GMMR0AllocateHandyPages: pGVM=%p cPagesToUpdate=%#x cPagesToAlloc=%#x paPages=%p\n",
pGVM, cPagesToUpdate, cPagesToAlloc, paPages));
/*
* Validate & get basics.
* (This is a relatively busy path, so make predictions where possible.)
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn( (cPagesToUpdate && cPagesToUpdate < 1024)
|| (cPagesToAlloc && cPagesToAlloc < 1024),
("cPagesToUpdate=%#x cPagesToAlloc=%#x\n", cPagesToUpdate, cPagesToAlloc),
VERR_INVALID_PARAMETER);
unsigned iPage = 0;
for (; iPage < cPagesToUpdate; iPage++)
{
AssertMsgReturn( ( paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
&& !(paPages[iPage].HCPhysGCPhys & GUEST_PAGE_OFFSET_MASK))
|| paPages[iPage].HCPhysGCPhys == NIL_GMMPAGEDESC_PHYS
|| paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE,
("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys),
VERR_INVALID_PARAMETER);
/* ignore fZeroed here */
AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn( paPages[iPage].idSharedPage == NIL_GMM_PAGEID
|| paPages[iPage].idSharedPage <= GMM_PAGEID_LAST,
("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
for (; iPage < cPagesToAlloc; iPage++)
{
AssertMsgReturn(paPages[iPage].HCPhysGCPhys == NIL_GMMPAGEDESC_PHYS, ("#%#x: %RHp\n", iPage, paPages[iPage].HCPhysGCPhys), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].fZeroed == false, ("#%#x: %#x\n", iPage, paPages[iPage].fZeroed), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
/*
* Take the semaphore
*/
VMMR0EMTBLOCKCTX Ctx;
PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, "GMMR0AllocateHandyPages", pGMM, &Ctx);
AssertRCReturn(rc, rc);
rc = gmmR0MutexAcquire(pGMM);
if ( RT_SUCCESS(rc)
&& GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/* No allocations before the initial reservation has been made! */
if (RT_LIKELY( pGVM->gmm.s.Stats.Reserved.cBasePages
&& pGVM->gmm.s.Stats.Reserved.cFixedPages
&& pGVM->gmm.s.Stats.Reserved.cShadowPages))
{
/*
* Perform the updates.
* Stop on the first error.
*/
for (iPage = 0; iPage < cPagesToUpdate; iPage++)
{
if (paPages[iPage].idPage != NIL_GMM_PAGEID)
{
PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
{
if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
{
AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
if (RT_LIKELY(paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST))
pPage->Private.pfn = paPages[iPage].HCPhysGCPhys >> GUEST_PAGE_SHIFT;
else if (paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE)
pPage->Private.pfn = GMM_PAGE_PFN_UNSHAREABLE;
/* else: NIL_RTHCPHYS nothing */
paPages[iPage].idPage = NIL_GMM_PAGEID;
paPages[iPage].HCPhysGCPhys = NIL_GMMPAGEDESC_PHYS;
paPages[iPage].fZeroed = false;
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not owner! hGVM=%#x hSelf=%#x\n",
iPage, paPages[iPage].idPage, pPage->Private.hGVM, pGVM->hSelf));
rc = VERR_GMM_NOT_PAGE_OWNER;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not private! %.*Rhxs (type %d)\n", iPage, paPages[iPage].idPage, sizeof(*pPage), pPage, pPage->Common.u2State));
rc = VERR_GMM_PAGE_NOT_PRIVATE;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (private)\n", iPage, paPages[iPage].idPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
}
if (paPages[iPage].idSharedPage == NIL_GMM_PAGEID)
{ /* likely */ }
else
{
PGMMPAGE pPage = gmmR0GetPage(pGMM, paPages[iPage].idSharedPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
{
AssertCompile(NIL_RTHCPHYS > GMM_GCPHYS_LAST && GMM_GCPHYS_UNSHAREABLE > GMM_GCPHYS_LAST);
Assert(pPage->Shared.cRefs);
Assert(pGVM->gmm.s.Stats.cSharedPages);
Assert(pGVM->gmm.s.Stats.Allocated.cBasePages);
Log(("GMMR0AllocateHandyPages: free shared page %x cRefs=%d\n", paPages[iPage].idSharedPage, pPage->Shared.cRefs));
pGVM->gmm.s.Stats.cSharedPages--;
pGVM->gmm.s.Stats.Allocated.cBasePages--;
if (!--pPage->Shared.cRefs)
gmmR0FreeSharedPage(pGMM, pGVM, paPages[iPage].idSharedPage, pPage);
else
{
Assert(pGMM->cDuplicatePages);
pGMM->cDuplicatePages--;
}
paPages[iPage].idSharedPage = NIL_GMM_PAGEID;
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not shared!\n", iPage, paPages[iPage].idSharedPage));
rc = VERR_GMM_PAGE_NOT_SHARED;
break;
}
}
else
{
Log(("GMMR0AllocateHandyPages: #%#x/%#x: Not found! (shared)\n", iPage, paPages[iPage].idSharedPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
}
} /* for each page to update */
if (RT_SUCCESS(rc) && cPagesToAlloc > 0)
{
#ifdef VBOX_STRICT
for (iPage = 0; iPage < cPagesToAlloc; iPage++)
{
Assert(paPages[iPage].HCPhysGCPhys == NIL_GMMPAGEDESC_PHYS);
Assert(paPages[iPage].fZeroed == false);
Assert(paPages[iPage].idPage == NIL_GMM_PAGEID);
Assert(paPages[iPage].idSharedPage == NIL_GMM_PAGEID);
}
#endif
/*
* Join paths with GMMR0AllocatePages for the allocation.
* Note! gmmR0AllocateMoreChunks may leave the protection of the mutex!
*/
rc = gmmR0AllocatePagesNew(pGMM, pGVM, cPagesToAlloc, paPages, GMMACCOUNT_BASE);
}
}
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
gmmR0MutexRelease(pGMM);
}
else if (RT_SUCCESS(rc))
{
gmmR0MutexRelease(pGMM);
rc = VERR_GMM_IS_NOT_SANE;
}
VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
LogFlow(("GMMR0AllocateHandyPages: returns %Rrc\n", rc));
return rc;
}
/**
* Allocate one or more pages.
*
* This is typically used for ROMs and MMIO2 (VRAM) during VM creation.
* The allocated pages are not cleared and will contain random garbage.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors.
* See GMMPAGEDESC for details on what is expected on
* input.
* @param enmAccount The account to charge.
*
* @thread EMT.
*/
GMMR0DECL(int) GMMR0AllocatePages(PGVM pGVM, VMCPUID idCpu, uint32_t cPages, PGMMPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0AllocatePages: pGVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pGVM, cPages, paPages, enmAccount));
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - GUEST_PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
for (unsigned iPage = 0; iPage < cPages; iPage++)
{
AssertMsgReturn( paPages[iPage].HCPhysGCPhys == NIL_GMMPAGEDESC_PHYS
|| paPages[iPage].HCPhysGCPhys == GMM_GCPHYS_UNSHAREABLE
|| ( enmAccount == GMMACCOUNT_BASE
&& paPages[iPage].HCPhysGCPhys <= GMM_GCPHYS_LAST
&& !(paPages[iPage].HCPhysGCPhys & GUEST_PAGE_OFFSET_MASK)),
("#%#x: %RHp enmAccount=%d\n", iPage, paPages[iPage].HCPhysGCPhys, enmAccount),
VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].fZeroed == false, ("#%#x: %#x\n", iPage, paPages[iPage].fZeroed), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
AssertMsgReturn(paPages[iPage].idSharedPage == NIL_GMM_PAGEID, ("#%#x: %#x\n", iPage, paPages[iPage].idSharedPage), VERR_INVALID_PARAMETER);
}
/*
* Grab the giant mutex and get working.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/* No allocations before the initial reservation has been made! */
if (RT_LIKELY( pGVM->gmm.s.Stats.Reserved.cBasePages
&& pGVM->gmm.s.Stats.Reserved.cFixedPages
&& pGVM->gmm.s.Stats.Reserved.cShadowPages))
rc = gmmR0AllocatePagesNew(pGMM, pGVM, cPages, paPages, enmAccount);
else
rc = VERR_WRONG_ORDER;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0AllocatePages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0AllocatePages.
*
* @returns see GMMR0AllocatePages.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0AllocatePagesReq(PGVM pGVM, VMCPUID idCpu, PGMMALLOCATEPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0]),
("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMALLOCATEPAGESREQ, aPages[0])),
VERR_INVALID_PARAMETER);
AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF_DYN(GMMALLOCATEPAGESREQ, aPages[pReq->cPages]),
("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF_DYN(GMMALLOCATEPAGESREQ, aPages[pReq->cPages])),
VERR_INVALID_PARAMETER);
return GMMR0AllocatePages(pGVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
}
/**
* Allocate a large page to represent guest RAM
*
* The allocated pages are zeroed upon return.
*
* @returns VBox status code:
* @retval VINF_SUCCESS on success.
* @retval VERR_NOT_OWNER if the caller is not an EMT.
* @retval VERR_GMM_HIT_GLOBAL_LIMIT if we've exhausted the available pages.
* @retval VERR_GMM_HIT_VM_ACCOUNT_LIMIT if we've hit the VM account limit,
* that is we're trying to allocate more than we've reserved.
* @retval VERR_TRY_AGAIN if the host is temporarily out of large pages.
* @returns see GMMR0AllocatePages.
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param cbPage Large page size.
* @param pIdPage Where to return the GMM page ID of the page.
* @param pHCPhys Where to return the host physical address of the page.
*/
GMMR0DECL(int) GMMR0AllocateLargePage(PGVM pGVM, VMCPUID idCpu, uint32_t cbPage, uint32_t *pIdPage, RTHCPHYS *pHCPhys)
{
LogFlow(("GMMR0AllocateLargePage: pGVM=%p cbPage=%x\n", pGVM, cbPage));
AssertPtrReturn(pIdPage, VERR_INVALID_PARAMETER);
*pIdPage = NIL_GMM_PAGEID;
AssertPtrReturn(pHCPhys, VERR_INVALID_PARAMETER);
*pHCPhys = NIL_RTHCPHYS;
AssertReturn(cbPage == GMM_CHUNK_SIZE, VERR_INVALID_PARAMETER);
/*
* Validate GVM + idCpu, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
AssertRCReturn(rc, rc);
VMMR0EMTBLOCKCTX Ctx;
PGVMCPU pGVCpu = &pGVM->aCpus[idCpu];
rc = VMMR0EmtPrepareToBlock(pGVCpu, VINF_SUCCESS, "GMMR0AllocateLargePage", pGMM, &Ctx);
AssertRCReturn(rc, rc);
rc = gmmR0MutexAcquire(pGMM);
if (RT_SUCCESS(rc))
{
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/*
* Check the quota.
*/
/** @todo r=bird: Quota checking could be done w/o the giant mutex but using
* a VM specific mutex... */
if (RT_LIKELY( pGVM->gmm.s.Stats.Allocated.cBasePages + pGVM->gmm.s.Stats.cBalloonedPages + GMM_CHUNK_NUM_PAGES
<= pGVM->gmm.s.Stats.Reserved.cBasePages))
{
/*
* Allocate a new large page chunk.
*
* Note! We leave the giant GMM lock temporarily as the allocation might
* take a long time. gmmR0RegisterChunk will retake it (ugly).
*/
AssertCompile(GMM_CHUNK_SIZE == _2M);
gmmR0MutexRelease(pGMM);
RTR0MEMOBJ hMemObj;
rc = RTR0MemObjAllocLarge(&hMemObj, GMM_CHUNK_SIZE, GMM_CHUNK_SIZE, RTMEMOBJ_ALLOC_LARGE_F_FAST);
if (RT_SUCCESS(rc))
{
*pHCPhys = RTR0MemObjGetPagePhysAddr(hMemObj, 0);
/*
* Register the chunk as fully allocated.
* Note! As mentioned above, this will return owning the mutex on success.
*/
PGMMCHUNK pChunk = NULL;
PGMMCHUNKFREESET const pSet = pGMM->fBoundMemoryMode ? &pGVM->gmm.s.Private : &pGMM->PrivateX;
rc = gmmR0RegisterChunk(pGMM, pSet, hMemObj, pGVM->hSelf, pGVM->pSession, GMM_CHUNK_FLAGS_LARGE_PAGE,
0 /*cPages*/, NULL /*paPages*/, NULL /*piPage*/, &pChunk);
if (RT_SUCCESS(rc))
{
/*
* The gmmR0RegisterChunk call already marked all pages allocated,
* so we just have to fill in the return values and update stats now.
*/
*pIdPage = pChunk->Core.Key << GMM_CHUNKID_SHIFT;
/* Update accounting. */
pGVM->gmm.s.Stats.Allocated.cBasePages += GMM_CHUNK_NUM_PAGES;
pGVM->gmm.s.Stats.cPrivatePages += GMM_CHUNK_NUM_PAGES;
pGMM->cAllocatedPages += GMM_CHUNK_NUM_PAGES;
gmmR0LinkChunk(pChunk, pSet);
gmmR0MutexRelease(pGMM);
VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
LogFlow(("GMMR0AllocateLargePage: returns VINF_SUCCESS\n"));
return VINF_SUCCESS;
}
/*
* Bail out.
*/
RTR0MemObjFree(hMemObj, true /* fFreeMappings */);
*pHCPhys = NIL_RTHCPHYS;
}
/** @todo r=bird: Turn VERR_NO_MEMORY etc into VERR_TRY_AGAIN? Docs say we
* return it, but I am sure IPRT doesn't... */
}
else
{
Log(("GMMR0AllocateLargePage: Reserved=%#llx Allocated+Requested=%#llx+%#x!\n",
pGVM->gmm.s.Stats.Reserved.cBasePages, pGVM->gmm.s.Stats.Allocated.cBasePages, GMM_CHUNK_NUM_PAGES));
gmmR0MutexRelease(pGMM);
rc = VERR_GMM_HIT_VM_ACCOUNT_LIMIT;
}
}
else
{
gmmR0MutexRelease(pGMM);
rc = VERR_GMM_IS_NOT_SANE;
}
}
VMMR0EmtResumeAfterBlocking(pGVCpu, &Ctx);
LogFlow(("GMMR0AllocateLargePage: returns %Rrc\n", rc));
return rc;
}
/**
* Free a large page.
*
* @returns VBox status code:
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param idPage The large page id.
*/
GMMR0DECL(int) GMMR0FreeLargePage(PGVM pGVM, VMCPUID idCpu, uint32_t idPage)
{
LogFlow(("GMMR0FreeLargePage: pGVM=%p idPage=%x\n", pGVM, idPage));
/*
* Validate, get basics and take the semaphore.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
const unsigned cPages = GMM_CHUNK_NUM_PAGES;
if (RT_UNLIKELY(pGVM->gmm.s.Stats.Allocated.cBasePages < cPages))
{
Log(("GMMR0FreeLargePage: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Stats.Allocated.cBasePages, cPages));
gmmR0MutexRelease(pGMM);
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
if (RT_LIKELY( pPage
&& GMM_PAGE_IS_PRIVATE(pPage)))
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cPrivate > 0);
/* Release the memory immediately. */
gmmR0FreeChunk(pGMM, NULL, pChunk, false /*fRelaxedSem*/); /** @todo this can be relaxed too! */
/* Update accounting. */
pGVM->gmm.s.Stats.Allocated.cBasePages -= cPages;
pGVM->gmm.s.Stats.cPrivatePages -= cPages;
pGMM->cAllocatedPages -= cPages;
}
else
rc = VERR_GMM_PAGE_NOT_FOUND;
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0FreeLargePage: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreeLargePage.
*
* @returns see GMMR0FreeLargePage.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0FreeLargePageReq(PGVM pGVM, VMCPUID idCpu, PGMMFREELARGEPAGEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMFREEPAGESREQ),
("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(GMMFREEPAGESREQ)),
VERR_INVALID_PARAMETER);
return GMMR0FreeLargePage(pGVM, idCpu, pReq->idPage);
}
/**
* @callback_method_impl{FNGVMMR0ENUMCALLBACK,
* Used by gmmR0FreeChunkFlushPerVmTlbs().}
*/
static DECLCALLBACK(int) gmmR0InvalidatePerVmChunkTlbCallback(PGVM pGVM, void *pvUser)
{
RT_NOREF(pvUser);
if (pGVM->gmm.s.hChunkTlbSpinLock != NIL_RTSPINLOCK)
{
RTSpinlockAcquire(pGVM->gmm.s.hChunkTlbSpinLock);
uintptr_t i = RT_ELEMENTS(pGVM->gmm.s.aChunkTlbEntries);
while (i-- > 0)
{
pGVM->gmm.s.aChunkTlbEntries[i].idGeneration = UINT64_MAX;
pGVM->gmm.s.aChunkTlbEntries[i].pChunk = NULL;
}
RTSpinlockRelease(pGVM->gmm.s.hChunkTlbSpinLock);
}
return VINF_SUCCESS;
}
/**
* Called by gmmR0FreeChunk when we reach the threshold for wrapping around the
* free generation ID value.
*
* This is done at 2^62 - 1, which allows us to drop all locks and as it will
* take a while before 12 exa (2 305 843 009 213 693 952) calls to
* gmmR0FreeChunk can be made and causes a real wrap-around. We do two
* invalidation passes and resets the generation ID between then. This will
* make sure there are no false positives.
*
* @param pGMM Pointer to the GMM instance.
*/
static void gmmR0FreeChunkFlushPerVmTlbs(PGMM pGMM)
{
/*
* First invalidation pass.
*/
int rc = GVMMR0EnumVMs(gmmR0InvalidatePerVmChunkTlbCallback, NULL);
AssertRCSuccess(rc);
/*
* Reset the generation number.
*/
RTSpinlockAcquire(pGMM->hSpinLockTree);
ASMAtomicWriteU64(&pGMM->idFreeGeneration, 1);
RTSpinlockRelease(pGMM->hSpinLockTree);
/*
* Second invalidation pass.
*/
rc = GVMMR0EnumVMs(gmmR0InvalidatePerVmChunkTlbCallback, NULL);
AssertRCSuccess(rc);
}
/**
* Frees a chunk, giving it back to the host OS.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM This is set when called from GMMR0CleanupVM so we can
* unmap and free the chunk in one go.
* @param pChunk The chunk to free.
* @param fRelaxedSem Whether we can release the semaphore while doing the
* freeing (@c true) or not.
*/
static bool gmmR0FreeChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, bool fRelaxedSem)
{
Assert(pChunk->Core.Key != NIL_GMM_CHUNKID);
GMMR0CHUNKMTXSTATE MtxState;
gmmR0ChunkMutexAcquire(&MtxState, pGMM, pChunk, GMMR0CHUNK_MTX_KEEP_GIANT);
/*
* Cleanup hack! Unmap the chunk from the callers address space.
* This shouldn't happen, so screw lock contention...
*/
if (pChunk->cMappingsX && pGVM)
gmmR0UnmapChunkLocked(pGMM, pGVM, pChunk);
/*
* If there are current mappings of the chunk, then request the
* VMs to unmap them. Reposition the chunk in the free list so
* it won't be a likely candidate for allocations.
*/
if (pChunk->cMappingsX)
{
/** @todo R0 -> VM request */
/* The chunk can be mapped by more than one VM if fBoundMemoryMode is false! */
Log(("gmmR0FreeChunk: chunk still has %d mappings; don't free!\n", pChunk->cMappingsX));
gmmR0ChunkMutexRelease(&MtxState, pChunk);
return false;
}
/*
* Save and trash the handle.
*/
RTR0MEMOBJ const hMemObj = pChunk->hMemObj;
pChunk->hMemObj = NIL_RTR0MEMOBJ;
/*
* Unlink it from everywhere.
*/
gmmR0UnlinkChunk(pChunk);
RTSpinlockAcquire(pGMM->hSpinLockTree);
RTListNodeRemove(&pChunk->ListNode);
PAVLU32NODECORE pCore = RTAvlU32Remove(&pGMM->pChunks, pChunk->Core.Key);
Assert(pCore == &pChunk->Core); NOREF(pCore);
PGMMCHUNKTLBE pTlbe = &pGMM->ChunkTLB.aEntries[GMM_CHUNKTLB_IDX(pChunk->Core.Key)];
if (pTlbe->pChunk == pChunk)
{
pTlbe->idChunk = NIL_GMM_CHUNKID;
pTlbe->pChunk = NULL;
}
Assert(pGMM->cChunks > 0);
pGMM->cChunks--;
uint64_t const idFreeGeneration = ASMAtomicIncU64(&pGMM->idFreeGeneration);
RTSpinlockRelease(pGMM->hSpinLockTree);
pGMM->cFreedChunks++;
/* Drop the lock. */
gmmR0ChunkMutexRelease(&MtxState, NULL);
if (fRelaxedSem)
gmmR0MutexRelease(pGMM);
/*
* Flush per VM chunk TLBs if we're getting remotely close to a generation wraparound.
*/
if (idFreeGeneration == UINT64_MAX / 4)
gmmR0FreeChunkFlushPerVmTlbs(pGMM);
/*
* Free the Chunk ID and all memory associated with the chunk.
*/
gmmR0FreeChunkId(pGMM, pChunk->Core.Key);
pChunk->Core.Key = NIL_GMM_CHUNKID;
RTMemFree(pChunk->paMappingsX);
pChunk->paMappingsX = NULL;
RTMemFree(pChunk);
#ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
int rc = RTR0MemObjFree(hMemObj, true /* fFreeMappings */);
#else
int rc = RTR0MemObjFree(hMemObj, false /* fFreeMappings */);
#endif
AssertLogRelRC(rc);
if (fRelaxedSem)
gmmR0MutexAcquire(pGMM);
return fRelaxedSem;
}
/**
* Free page worker.
*
* The caller does all the statistic decrementing, we do all the incrementing.
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the GVM instance.
* @param pChunk Pointer to the chunk this page belongs to.
* @param idPage The Page ID.
* @param pPage Pointer to the page.
*/
static void gmmR0FreePageWorker(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, uint32_t idPage, PGMMPAGE pPage)
{
Log3(("F pPage=%p iPage=%#x/%#x u2State=%d iFreeHead=%#x\n",
pPage, pPage - &pChunk->aPages[0], idPage, pPage->Common.u2State, pChunk->iFreeHead)); NOREF(idPage);
/*
* Put the page on the free list.
*/
pPage->u = 0;
pPage->Free.u2State = GMM_PAGE_STATE_FREE;
pPage->Free.fZeroed = false;
Assert(pChunk->iFreeHead < RT_ELEMENTS(pChunk->aPages) || pChunk->iFreeHead == UINT16_MAX);
pPage->Free.iNext = pChunk->iFreeHead;
pChunk->iFreeHead = pPage - &pChunk->aPages[0];
/*
* Update statistics (the cShared/cPrivate stats are up to date already),
* and relink the chunk if necessary.
*/
unsigned const cFree = pChunk->cFree;
if ( !cFree
|| gmmR0SelectFreeSetList(cFree) != gmmR0SelectFreeSetList(cFree + 1))
{
gmmR0UnlinkChunk(pChunk);
pChunk->cFree++;
gmmR0SelectSetAndLinkChunk(pGMM, pGVM, pChunk);
}
else
{
pChunk->cFree = cFree + 1;
pChunk->pSet->cFreePages++;
}
/*
* If the chunk becomes empty, consider giving memory back to the host OS.
*
* The current strategy is to try give it back if there are other chunks
* in this free list, meaning if there are at least 240 free pages in this
* category. Note that since there are probably mappings of the chunk,
* it won't be freed up instantly, which probably screws up this logic
* a bit...
*/
/** @todo Do this on the way out. */
if (RT_LIKELY( pChunk->cFree != GMM_CHUNK_NUM_PAGES
|| pChunk->pFreeNext == NULL
|| pChunk->pFreePrev == NULL /** @todo this is probably misfiring, see reset... */))
{ /* likely */ }
else
gmmR0FreeChunk(pGMM, NULL, pChunk, false);
}
/**
* Frees a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param idPage The page id.
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0FreeSharedPage(PGMM pGMM, PGVM pGVM, uint32_t idPage, PGMMPAGE pPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cShared > 0);
Assert(pGMM->cSharedPages > 0);
Assert(pGMM->cAllocatedPages > 0);
Assert(!pPage->Shared.cRefs);
pChunk->cShared--;
pGMM->cAllocatedPages--;
pGMM->cSharedPages--;
gmmR0FreePageWorker(pGMM, pGVM, pChunk, idPage, pPage);
}
/**
* Frees a private page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param idPage The page id.
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0FreePrivatePage(PGMM pGMM, PGVM pGVM, uint32_t idPage, PGMMPAGE pPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(pChunk->cPrivate > 0);
Assert(pGMM->cAllocatedPages > 0);
pChunk->cPrivate--;
pGMM->cAllocatedPages--;
gmmR0FreePageWorker(pGMM, pGVM, pChunk, idPage, pPage);
}
/**
* Common worker for GMMR0FreePages and GMMR0BalloonedPages.
*
* @returns VBox status code:
* @retval xxx
*
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the VM.
* @param cPages The number of pages to free.
* @param paPages Pointer to the page descriptors.
* @param enmAccount The account this relates to.
*/
static int gmmR0FreePages(PGMM pGMM, PGVM pGVM, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
/*
* Check that the request isn't impossible wrt to the account status.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE:
if (RT_UNLIKELY(pGVM->gmm.s.Stats.Allocated.cBasePages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Stats.Allocated.cBasePages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
case GMMACCOUNT_SHADOW:
if (RT_UNLIKELY(pGVM->gmm.s.Stats.Allocated.cShadowPages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Stats.Allocated.cShadowPages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
case GMMACCOUNT_FIXED:
if (RT_UNLIKELY(pGVM->gmm.s.Stats.Allocated.cFixedPages < cPages))
{
Log(("gmmR0FreePages: allocated=%#llx cPages=%#x!\n", pGVM->gmm.s.Stats.Allocated.cFixedPages, cPages));
return VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
/*
* Walk the descriptors and free the pages.
*
* Statistics (except the account) are being updated as we go along,
* unlike the alloc code. Also, stop on the first error.
*/
int rc = VINF_SUCCESS;
uint32_t iPage;
for (iPage = 0; iPage < cPages; iPage++)
{
uint32_t idPage = paPages[iPage].idPage;
PGMMPAGE pPage = gmmR0GetPage(pGMM, idPage);
if (RT_LIKELY(pPage))
{
if (RT_LIKELY(GMM_PAGE_IS_PRIVATE(pPage)))
{
if (RT_LIKELY(pPage->Private.hGVM == pGVM->hSelf))
{
Assert(pGVM->gmm.s.Stats.cPrivatePages);
pGVM->gmm.s.Stats.cPrivatePages--;
gmmR0FreePrivatePage(pGMM, pGVM, idPage, pPage);
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: not owner! hGVM=%#x hSelf=%#x\n", iPage, idPage,
pPage->Private.hGVM, pGVM->hSelf));
rc = VERR_GMM_NOT_PAGE_OWNER;
break;
}
}
else if (RT_LIKELY(GMM_PAGE_IS_SHARED(pPage)))
{
Assert(pGVM->gmm.s.Stats.cSharedPages);
Assert(pPage->Shared.cRefs);
#if defined(VBOX_WITH_PAGE_SHARING) && defined(VBOX_STRICT)
if (pPage->Shared.u14Checksum)
{
uint32_t uChecksum = gmmR0StrictPageChecksum(pGMM, pGVM, idPage);
uChecksum &= UINT32_C(0x00003fff);
AssertMsg(!uChecksum || uChecksum == pPage->Shared.u14Checksum,
("%#x vs %#x - idPage=%#x\n", uChecksum, pPage->Shared.u14Checksum, idPage));
}
#endif
pGVM->gmm.s.Stats.cSharedPages--;
if (!--pPage->Shared.cRefs)
gmmR0FreeSharedPage(pGMM, pGVM, idPage, pPage);
else
{
Assert(pGMM->cDuplicatePages);
pGMM->cDuplicatePages--;
}
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: already free!\n", iPage, idPage));
rc = VERR_GMM_PAGE_ALREADY_FREE;
break;
}
}
else
{
Log(("gmmR0AllocatePages: #%#x/%#x: not found!\n", iPage, idPage));
rc = VERR_GMM_PAGE_NOT_FOUND;
break;
}
paPages[iPage].idPage = NIL_GMM_PAGEID;
}
/*
* Update the account.
*/
switch (enmAccount)
{
case GMMACCOUNT_BASE: pGVM->gmm.s.Stats.Allocated.cBasePages -= iPage; break;
case GMMACCOUNT_SHADOW: pGVM->gmm.s.Stats.Allocated.cShadowPages -= iPage; break;
case GMMACCOUNT_FIXED: pGVM->gmm.s.Stats.Allocated.cFixedPages -= iPage; break;
default:
AssertMsgFailedReturn(("enmAccount=%d\n", enmAccount), VERR_IPE_NOT_REACHED_DEFAULT_CASE);
}
/*
* Any threshold stuff to be done here?
*/
return rc;
}
/**
* Free one or more pages.
*
* This is typically used at reset time or power off.
*
* @returns VBox status code:
* @retval xxx
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param cPages The number of pages to allocate.
* @param paPages Pointer to the page descriptors containing the page IDs
* for each page.
* @param enmAccount The account this relates to.
* @thread EMT.
*/
GMMR0DECL(int) GMMR0FreePages(PGVM pGVM, VMCPUID idCpu, uint32_t cPages, PGMMFREEPAGEDESC paPages, GMMACCOUNT enmAccount)
{
LogFlow(("GMMR0FreePages: pGVM=%p cPages=%#x paPages=%p enmAccount=%d\n", pGVM, cPages, paPages, enmAccount));
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(paPages, VERR_INVALID_PARAMETER);
AssertMsgReturn(enmAccount > GMMACCOUNT_INVALID && enmAccount < GMMACCOUNT_END, ("%d\n", enmAccount), VERR_INVALID_PARAMETER);
AssertMsgReturn(cPages > 0 && cPages < RT_BIT(32 - GUEST_PAGE_SHIFT), ("%#x\n", cPages), VERR_INVALID_PARAMETER);
for (unsigned iPage = 0; iPage < cPages; iPage++)
AssertMsgReturn( paPages[iPage].idPage <= GMM_PAGEID_LAST
/*|| paPages[iPage].idPage == NIL_GMM_PAGEID*/,
("#%#x: %#x\n", iPage, paPages[iPage].idPage), VERR_INVALID_PARAMETER);
/*
* Take the semaphore and call the worker function.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
rc = gmmR0FreePages(pGMM, pGVM, cPages, paPages, enmAccount);
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0FreePages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0FreePages.
*
* @returns see GMMR0FreePages.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0FreePagesReq(PGVM pGVM, VMCPUID idCpu, PGMMFREEPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq >= RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0]),
("%#x < %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF(GMMFREEPAGESREQ, aPages[0])),
VERR_INVALID_PARAMETER);
AssertMsgReturn(pReq->Hdr.cbReq == RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[pReq->cPages]),
("%#x != %#x\n", pReq->Hdr.cbReq, RT_UOFFSETOF_DYN(GMMFREEPAGESREQ, aPages[pReq->cPages])),
VERR_INVALID_PARAMETER);
return GMMR0FreePages(pGVM, idCpu, pReq->cPages, &pReq->aPages[0], pReq->enmAccount);
}
/**
* Report back on a memory ballooning request.
*
* The request may or may not have been initiated by the GMM. If it was initiated
* by the GMM it is important that this function is called even if no pages were
* ballooned.
*
* @returns VBox status code:
* @retval VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH
* @retval VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH
* @retval VERR_GMM_OVERCOMMITTED_TRY_AGAIN_IN_A_BIT - reset condition
* indicating that we won't necessarily have sufficient RAM to boot
* the VM again and that it should pause until this changes (we'll try
* balloon some other VM). (For standard deflate we have little choice
* but to hope the VM won't use the memory that was returned to it.)
*
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param enmAction Inflate/deflate/reset.
* @param cBalloonedPages The number of pages that was ballooned.
*
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0BalloonedPages(PGVM pGVM, VMCPUID idCpu, GMMBALLOONACTION enmAction, uint32_t cBalloonedPages)
{
LogFlow(("GMMR0BalloonedPages: pGVM=%p enmAction=%d cBalloonedPages=%#x\n",
pGVM, enmAction, cBalloonedPages));
AssertMsgReturn(cBalloonedPages < RT_BIT(32 - GUEST_PAGE_SHIFT), ("%#x\n", cBalloonedPages), VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
switch (enmAction)
{
case GMMBALLOONACTION_INFLATE:
{
if (RT_LIKELY(pGVM->gmm.s.Stats.Allocated.cBasePages + pGVM->gmm.s.Stats.cBalloonedPages + cBalloonedPages
<= pGVM->gmm.s.Stats.Reserved.cBasePages))
{
/*
* Record the ballooned memory.
*/
pGMM->cBalloonedPages += cBalloonedPages;
if (pGVM->gmm.s.Stats.cReqBalloonedPages)
{
/* Codepath never taken. Might be interesting in the future to request ballooned memory from guests in low memory conditions.. */
AssertFailed();
pGVM->gmm.s.Stats.cBalloonedPages += cBalloonedPages;
pGVM->gmm.s.Stats.cReqActuallyBalloonedPages += cBalloonedPages;
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx Req=%#llx Actual=%#llx (pending)\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.Stats.cBalloonedPages,
pGVM->gmm.s.Stats.cReqBalloonedPages, pGVM->gmm.s.Stats.cReqActuallyBalloonedPages));
}
else
{
pGVM->gmm.s.Stats.cBalloonedPages += cBalloonedPages;
Log(("GMMR0BalloonedPages: +%#x - Global=%#llx / VM: Total=%#llx (user)\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.Stats.cBalloonedPages));
}
}
else
{
Log(("GMMR0BalloonedPages: cBasePages=%#llx Total=%#llx cBalloonedPages=%#llx Reserved=%#llx\n",
pGVM->gmm.s.Stats.Allocated.cBasePages, pGVM->gmm.s.Stats.cBalloonedPages, cBalloonedPages,
pGVM->gmm.s.Stats.Reserved.cBasePages));
rc = VERR_GMM_ATTEMPT_TO_FREE_TOO_MUCH;
}
break;
}
case GMMBALLOONACTION_DEFLATE:
{
/* Deflate. */
if (pGVM->gmm.s.Stats.cBalloonedPages >= cBalloonedPages)
{
/*
* Record the ballooned memory.
*/
Assert(pGMM->cBalloonedPages >= cBalloonedPages);
pGMM->cBalloonedPages -= cBalloonedPages;
pGVM->gmm.s.Stats.cBalloonedPages -= cBalloonedPages;
if (pGVM->gmm.s.Stats.cReqDeflatePages)
{
AssertFailed(); /* This is path is for later. */
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx Req=%#llx\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.Stats.cBalloonedPages, pGVM->gmm.s.Stats.cReqDeflatePages));
/*
* Anything we need to do here now when the request has been completed?
*/
pGVM->gmm.s.Stats.cReqDeflatePages = 0;
}
else
Log(("GMMR0BalloonedPages: -%#x - Global=%#llx / VM: Total=%#llx (user)\n",
cBalloonedPages, pGMM->cBalloonedPages, pGVM->gmm.s.Stats.cBalloonedPages));
}
else
{
Log(("GMMR0BalloonedPages: Total=%#llx cBalloonedPages=%#llx\n", pGVM->gmm.s.Stats.cBalloonedPages, cBalloonedPages));
rc = VERR_GMM_ATTEMPT_TO_DEFLATE_TOO_MUCH;
}
break;
}
case GMMBALLOONACTION_RESET:
{
/* Reset to an empty balloon. */
Assert(pGMM->cBalloonedPages >= pGVM->gmm.s.Stats.cBalloonedPages);
pGMM->cBalloonedPages -= pGVM->gmm.s.Stats.cBalloonedPages;
pGVM->gmm.s.Stats.cBalloonedPages = 0;
break;
}
default:
rc = VERR_INVALID_PARAMETER;
break;
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0BalloonedPages: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0BalloonedPages.
*
* @returns see GMMR0BalloonedPages.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0BalloonedPagesReq(PGVM pGVM, VMCPUID idCpu, PGMMBALLOONEDPAGESREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMBALLOONEDPAGESREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMBALLOONEDPAGESREQ)),
VERR_INVALID_PARAMETER);
return GMMR0BalloonedPages(pGVM, idCpu, pReq->enmAction, pReq->cBalloonedPages);
}
/**
* Return memory statistics for the hypervisor
*
* @returns VBox status code.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0QueryHypervisorMemoryStatsReq(PGMMMEMSTATSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMMEMSTATSREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMMEMSTATSREQ)),
VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
pReq->cAllocPages = pGMM->cAllocatedPages;
pReq->cFreePages = (pGMM->cChunks << (GMM_CHUNK_SHIFT - GUEST_PAGE_SHIFT)) - pGMM->cAllocatedPages;
pReq->cBalloonedPages = pGMM->cBalloonedPages;
pReq->cMaxPages = pGMM->cMaxPages;
pReq->cSharedPages = pGMM->cDuplicatePages;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
return VINF_SUCCESS;
}
/**
* Return memory statistics for the VM
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu Cpu id.
* @param pReq Pointer to the request packet.
*
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0QueryMemoryStatsReq(PGVM pGVM, VMCPUID idCpu, PGMMMEMSTATSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(GMMMEMSTATSREQ),
("%#x < %#x\n", pReq->Hdr.cbReq, sizeof(GMMMEMSTATSREQ)),
VERR_INVALID_PARAMETER);
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
pReq->cAllocPages = pGVM->gmm.s.Stats.Allocated.cBasePages;
pReq->cBalloonedPages = pGVM->gmm.s.Stats.cBalloonedPages;
pReq->cMaxPages = pGVM->gmm.s.Stats.Reserved.cBasePages;
pReq->cFreePages = pReq->cMaxPages - pReq->cAllocPages;
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR3QueryVMMemoryStats: returns %Rrc\n", rc));
return rc;
}
/**
* Worker for gmmR0UnmapChunk and gmmr0FreeChunk.
*
* Don't call this in legacy allocation mode!
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be unmapped.
*/
static int gmmR0UnmapChunkLocked(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk)
{
RT_NOREF_PV(pGMM);
/*
* Find the mapping and try unmapping it.
*/
uint32_t cMappings = pChunk->cMappingsX;
for (uint32_t i = 0; i < cMappings; i++)
{
Assert(pChunk->paMappingsX[i].pGVM && pChunk->paMappingsX[i].hMapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappingsX[i].pGVM == pGVM)
{
/* unmap */
int rc = RTR0MemObjFree(pChunk->paMappingsX[i].hMapObj, false /* fFreeMappings (NA) */);
if (RT_SUCCESS(rc))
{
/* update the record. */
cMappings--;
if (i < cMappings)
pChunk->paMappingsX[i] = pChunk->paMappingsX[cMappings];
pChunk->paMappingsX[cMappings].hMapObj = NIL_RTR0MEMOBJ;
pChunk->paMappingsX[cMappings].pGVM = NULL;
Assert(pChunk->cMappingsX - 1U == cMappings);
pChunk->cMappingsX = cMappings;
}
return rc;
}
}
Log(("gmmR0UnmapChunk: Chunk %#x is not mapped into pGVM=%p/%#x\n", pChunk->Core.Key, pGVM, pGVM->hSelf));
return VERR_GMM_CHUNK_NOT_MAPPED;
}
/**
* Unmaps a chunk previously mapped into the address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be unmapped.
* @param fRelaxedSem Whether we can release the semaphore while doing the
* mapping (@c true) or not.
*/
static int gmmR0UnmapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, bool fRelaxedSem)
{
/*
* Lock the chunk and if possible leave the giant GMM lock.
*/
GMMR0CHUNKMTXSTATE MtxState;
int rc = gmmR0ChunkMutexAcquire(&MtxState, pGMM, pChunk,
fRelaxedSem ? GMMR0CHUNK_MTX_RETAKE_GIANT : GMMR0CHUNK_MTX_KEEP_GIANT);
if (RT_SUCCESS(rc))
{
rc = gmmR0UnmapChunkLocked(pGMM, pGVM, pChunk);
gmmR0ChunkMutexRelease(&MtxState, pChunk);
}
return rc;
}
/**
* Worker for gmmR0MapChunk.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
* In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
* contain the address of the existing mapping.
*/
static int gmmR0MapChunkLocked(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, PRTR3PTR ppvR3)
{
RT_NOREF(pGMM);
/*
* Check to see if the chunk is already mapped.
*/
for (uint32_t i = 0; i < pChunk->cMappingsX; i++)
{
Assert(pChunk->paMappingsX[i].pGVM && pChunk->paMappingsX[i].hMapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappingsX[i].pGVM == pGVM)
{
*ppvR3 = RTR0MemObjAddressR3(pChunk->paMappingsX[i].hMapObj);
Log(("gmmR0MapChunk: chunk %#x is already mapped at %p!\n", pChunk->Core.Key, *ppvR3));
#ifdef VBOX_WITH_PAGE_SHARING
/* The ring-3 chunk cache can be out of sync; don't fail. */
return VINF_SUCCESS;
#else
return VERR_GMM_CHUNK_ALREADY_MAPPED;
#endif
}
}
/*
* Do the mapping.
*/
RTR0MEMOBJ hMapObj;
int rc = RTR0MemObjMapUser(&hMapObj, pChunk->hMemObj, (RTR3PTR)-1, 0, RTMEM_PROT_READ | RTMEM_PROT_WRITE, NIL_RTR0PROCESS);
if (RT_SUCCESS(rc))
{
/* reallocate the array? assumes few users per chunk (usually one). */
unsigned iMapping = pChunk->cMappingsX;
if ( iMapping <= 3
|| (iMapping & 3) == 0)
{
unsigned cNewSize = iMapping <= 3
? iMapping + 1
: iMapping + 4;
Assert(cNewSize < 4 || RT_ALIGN_32(cNewSize, 4) == cNewSize);
if (RT_UNLIKELY(cNewSize > UINT16_MAX))
{
rc = RTR0MemObjFree(hMapObj, false /* fFreeMappings (NA) */); AssertRC(rc);
return VERR_GMM_TOO_MANY_CHUNK_MAPPINGS;
}
void *pvMappings = RTMemRealloc(pChunk->paMappingsX, cNewSize * sizeof(pChunk->paMappingsX[0]));
if (RT_UNLIKELY(!pvMappings))
{
rc = RTR0MemObjFree(hMapObj, false /* fFreeMappings (NA) */); AssertRC(rc);
return VERR_NO_MEMORY;
}
pChunk->paMappingsX = (PGMMCHUNKMAP)pvMappings;
}
/* insert new entry */
pChunk->paMappingsX[iMapping].hMapObj = hMapObj;
pChunk->paMappingsX[iMapping].pGVM = pGVM;
Assert(pChunk->cMappingsX == iMapping);
pChunk->cMappingsX = iMapping + 1;
*ppvR3 = RTR0MemObjAddressR3(hMapObj);
}
return rc;
}
/**
* Maps a chunk into the user address space of the current process.
*
* @returns VBox status code.
* @param pGMM Pointer to the GMM instance data.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param fRelaxedSem Whether we can release the semaphore while doing the
* mapping (@c true) or not.
* @param ppvR3 Where to store the ring-3 address of the mapping.
* In the VERR_GMM_CHUNK_ALREADY_MAPPED case, this will be
* contain the address of the existing mapping.
*/
static int gmmR0MapChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, bool fRelaxedSem, PRTR3PTR ppvR3)
{
/*
* Take the chunk lock and leave the giant GMM lock when possible, then
* call the worker function.
*/
GMMR0CHUNKMTXSTATE MtxState;
int rc = gmmR0ChunkMutexAcquire(&MtxState, pGMM, pChunk,
fRelaxedSem ? GMMR0CHUNK_MTX_RETAKE_GIANT : GMMR0CHUNK_MTX_KEEP_GIANT);
if (RT_SUCCESS(rc))
{
rc = gmmR0MapChunkLocked(pGMM, pGVM, pChunk, ppvR3);
gmmR0ChunkMutexRelease(&MtxState, pChunk);
}
return rc;
}
#if defined(VBOX_WITH_PAGE_SHARING) || defined(VBOX_STRICT)
/**
* Check if a chunk is mapped into the specified VM
*
* @returns mapped yes/no
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the Global VM structure.
* @param pChunk Pointer to the chunk to be mapped.
* @param ppvR3 Where to store the ring-3 address of the mapping.
*/
static bool gmmR0IsChunkMapped(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, PRTR3PTR ppvR3)
{
GMMR0CHUNKMTXSTATE MtxState;
gmmR0ChunkMutexAcquire(&MtxState, pGMM, pChunk, GMMR0CHUNK_MTX_KEEP_GIANT);
for (uint32_t i = 0; i < pChunk->cMappingsX; i++)
{
Assert(pChunk->paMappingsX[i].pGVM && pChunk->paMappingsX[i].hMapObj != NIL_RTR0MEMOBJ);
if (pChunk->paMappingsX[i].pGVM == pGVM)
{
*ppvR3 = RTR0MemObjAddressR3(pChunk->paMappingsX[i].hMapObj);
gmmR0ChunkMutexRelease(&MtxState, pChunk);
return true;
}
}
*ppvR3 = NULL;
gmmR0ChunkMutexRelease(&MtxState, pChunk);
return false;
}
#endif /* VBOX_WITH_PAGE_SHARING || VBOX_STRICT */
/**
* Map a chunk and/or unmap another chunk.
*
* The mapping and unmapping applies to the current process.
*
* This API does two things because it saves a kernel call per mapping when
* when the ring-3 mapping cache is full.
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idChunkMap The chunk to map. NIL_GMM_CHUNKID if nothing to map.
* @param idChunkUnmap The chunk to unmap. NIL_GMM_CHUNKID if nothing to unmap.
* @param ppvR3 Where to store the address of the mapped chunk. NULL is ok if nothing to map.
* @thread EMT ???
*/
GMMR0DECL(int) GMMR0MapUnmapChunk(PGVM pGVM, uint32_t idChunkMap, uint32_t idChunkUnmap, PRTR3PTR ppvR3)
{
LogFlow(("GMMR0MapUnmapChunk: pGVM=%p idChunkMap=%#x idChunkUnmap=%#x ppvR3=%p\n",
pGVM, idChunkMap, idChunkUnmap, ppvR3));
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVM(pGVM);
if (RT_FAILURE(rc))
return rc;
AssertCompile(NIL_GMM_CHUNKID == 0);
AssertMsgReturn(idChunkMap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkMap), VERR_INVALID_PARAMETER);
AssertMsgReturn(idChunkUnmap <= GMM_CHUNKID_LAST, ("%#x\n", idChunkUnmap), VERR_INVALID_PARAMETER);
if ( idChunkMap == NIL_GMM_CHUNKID
&& idChunkUnmap == NIL_GMM_CHUNKID)
return VERR_INVALID_PARAMETER;
if (idChunkMap != NIL_GMM_CHUNKID)
{
AssertPtrReturn(ppvR3, VERR_INVALID_POINTER);
*ppvR3 = NIL_RTR3PTR;
}
/*
* Take the semaphore and do the work.
*
* The unmapping is done last since it's easier to undo a mapping than
* undoing an unmapping. The ring-3 mapping cache cannot not be so big
* that it pushes the user virtual address space to within a chunk of
* it it's limits, so, no problem here.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
PGMMCHUNK pMap = NULL;
if (idChunkMap != NIL_GVM_HANDLE)
{
pMap = gmmR0GetChunk(pGMM, idChunkMap);
if (RT_LIKELY(pMap))
rc = gmmR0MapChunk(pGMM, pGVM, pMap, true /*fRelaxedSem*/, ppvR3);
else
{
Log(("GMMR0MapUnmapChunk: idChunkMap=%#x\n", idChunkMap));
rc = VERR_GMM_CHUNK_NOT_FOUND;
}
}
/** @todo split this operation, the bail out might (theoretcially) not be
* entirely safe. */
if ( idChunkUnmap != NIL_GMM_CHUNKID
&& RT_SUCCESS(rc))
{
PGMMCHUNK pUnmap = gmmR0GetChunk(pGMM, idChunkUnmap);
if (RT_LIKELY(pUnmap))
rc = gmmR0UnmapChunk(pGMM, pGVM, pUnmap, true /*fRelaxedSem*/);
else
{
Log(("GMMR0MapUnmapChunk: idChunkUnmap=%#x\n", idChunkUnmap));
rc = VERR_GMM_CHUNK_NOT_FOUND;
}
if (RT_FAILURE(rc) && pMap)
gmmR0UnmapChunk(pGMM, pGVM, pMap, false /*fRelaxedSem*/);
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
LogFlow(("GMMR0MapUnmapChunk: returns %Rrc\n", rc));
return rc;
}
/**
* VMMR0 request wrapper for GMMR0MapUnmapChunk.
*
* @returns see GMMR0MapUnmapChunk.
* @param pGVM The global (ring-0) VM structure.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0MapUnmapChunkReq(PGVM pGVM, PGMMMAPUNMAPCHUNKREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0MapUnmapChunk(pGVM, pReq->idChunkMap, pReq->idChunkUnmap, &pReq->pvR3);
}
#ifndef VBOX_WITH_LINEAR_HOST_PHYS_MEM
/**
* Gets the ring-0 virtual address for the given page.
*
* This is used by PGM when IEM and such wants to access guest RAM from ring-0.
* One of the ASSUMPTIONS here is that the @a idPage is used by the VM and the
* corresponding chunk will remain valid beyond the call (at least till the EMT
* returns to ring-3).
*
* @returns VBox status code.
* @param pGVM Pointer to the kernel-only VM instace data.
* @param idPage The page ID.
* @param ppv Where to store the address.
* @thread EMT
*/
GMMR0DECL(int) GMMR0PageIdToVirt(PGVM pGVM, uint32_t idPage, void **ppv)
{
*ppv = NULL;
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
uint32_t const idChunk = idPage >> GMM_CHUNKID_SHIFT;
/*
* Start with the per-VM TLB.
*/
RTSpinlockAcquire(pGVM->gmm.s.hChunkTlbSpinLock);
PGMMPERVMCHUNKTLBE pTlbe = &pGVM->gmm.s.aChunkTlbEntries[GMMPERVM_CHUNKTLB_IDX(idChunk)];
PGMMCHUNK pChunk = pTlbe->pChunk;
if ( pChunk != NULL
&& pTlbe->idGeneration == ASMAtomicUoReadU64(&pGMM->idFreeGeneration)
&& pChunk->Core.Key == idChunk)
pGVM->R0Stats.gmm.cChunkTlbHits++; /* hopefully this is a likely outcome */
else
{
pGVM->R0Stats.gmm.cChunkTlbMisses++;
/*
* Look it up in the chunk tree.
*/
RTSpinlockAcquire(pGMM->hSpinLockTree);
pChunk = gmmR0GetChunkLocked(pGMM, idChunk);
if (RT_LIKELY(pChunk))
{
pTlbe->idGeneration = pGMM->idFreeGeneration;
RTSpinlockRelease(pGMM->hSpinLockTree);
pTlbe->pChunk = pChunk;
}
else
{
RTSpinlockRelease(pGMM->hSpinLockTree);
RTSpinlockRelease(pGVM->gmm.s.hChunkTlbSpinLock);
AssertMsgFailed(("idPage=%#x\n", idPage));
return VERR_GMM_PAGE_NOT_FOUND;
}
}
RTSpinlockRelease(pGVM->gmm.s.hChunkTlbSpinLock);
/*
* Got a chunk, now validate the page ownership and calcuate it's address.
*/
const GMMPAGE * const pPage = &pChunk->aPages[idPage & GMM_PAGEID_IDX_MASK];
if (RT_LIKELY( ( GMM_PAGE_IS_PRIVATE(pPage)
&& pPage->Private.hGVM == pGVM->hSelf)
|| GMM_PAGE_IS_SHARED(pPage)))
{
AssertPtr(pChunk->pbMapping);
*ppv = &pChunk->pbMapping[(idPage & GMM_PAGEID_IDX_MASK) << GUEST_PAGE_SHIFT];
return VINF_SUCCESS;
}
AssertMsgFailed(("idPage=%#x is-private=%RTbool Private.hGVM=%u pGVM->hGVM=%u\n",
idPage, GMM_PAGE_IS_PRIVATE(pPage), pPage->Private.hGVM, pGVM->hSelf));
return VERR_GMM_NOT_PAGE_OWNER;
}
#endif /* !VBOX_WITH_LINEAR_HOST_PHYS_MEM */
#ifdef VBOX_WITH_PAGE_SHARING
# ifdef VBOX_STRICT
/**
* For checksumming shared pages in strict builds.
*
* The purpose is making sure that a page doesn't change.
*
* @returns Checksum, 0 on failure.
* @param pGMM The GMM instance data.
* @param pGVM Pointer to the kernel-only VM instace data.
* @param idPage The page ID.
*/
static uint32_t gmmR0StrictPageChecksum(PGMM pGMM, PGVM pGVM, uint32_t idPage)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
AssertMsgReturn(pChunk, ("idPage=%#x\n", idPage), 0);
uint8_t *pbChunk;
if (!gmmR0IsChunkMapped(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk))
return 0;
uint8_t const *pbPage = pbChunk + ((idPage & GMM_PAGEID_IDX_MASK) << GUEST_PAGE_SHIFT);
return RTCrc32(pbPage, GUEST_PAGE_SIZE);
}
# endif /* VBOX_STRICT */
/**
* Calculates the module hash value.
*
* @returns Hash value.
* @param pszModuleName The module name.
* @param pszVersion The module version string.
*/
static uint32_t gmmR0ShModCalcHash(const char *pszModuleName, const char *pszVersion)
{
return RTStrHash1ExN(3, pszModuleName, RTSTR_MAX, "::", (size_t)2, pszVersion, RTSTR_MAX);
}
/**
* Finds a global module.
*
* @returns Pointer to the global module on success, NULL if not found.
* @param pGMM The GMM instance data.
* @param uHash The hash as calculated by gmmR0ShModCalcHash.
* @param cbModule The module size.
* @param enmGuestOS The guest OS type.
* @param cRegions The number of regions.
* @param pszModuleName The module name.
* @param pszVersion The module version.
* @param paRegions The region descriptions.
*/
static PGMMSHAREDMODULE gmmR0ShModFindGlobal(PGMM pGMM, uint32_t uHash, uint32_t cbModule, VBOXOSFAMILY enmGuestOS,
uint32_t cRegions, const char *pszModuleName, const char *pszVersion,
struct VMMDEVSHAREDREGIONDESC const *paRegions)
{
for (PGMMSHAREDMODULE pGblMod = (PGMMSHAREDMODULE)RTAvllU32Get(&pGMM->pGlobalSharedModuleTree, uHash);
pGblMod;
pGblMod = (PGMMSHAREDMODULE)pGblMod->Core.pList)
{
if (pGblMod->cbModule != cbModule)
continue;
if (pGblMod->enmGuestOS != enmGuestOS)
continue;
if (pGblMod->cRegions != cRegions)
continue;
if (strcmp(pGblMod->szName, pszModuleName))
continue;
if (strcmp(pGblMod->szVersion, pszVersion))
continue;
uint32_t i;
for (i = 0; i < cRegions; i++)
{
uint32_t off = paRegions[i].GCRegionAddr & GUEST_PAGE_OFFSET_MASK;
if (pGblMod->aRegions[i].off != off)
break;
uint32_t cb = RT_ALIGN_32(paRegions[i].cbRegion + off, GUEST_PAGE_SIZE);
if (pGblMod->aRegions[i].cb != cb)
break;
}
if (i == cRegions)
return pGblMod;
}
return NULL;
}
/**
* Creates a new global module.
*
* @returns VBox status code.
* @param pGMM The GMM instance data.
* @param uHash The hash as calculated by gmmR0ShModCalcHash.
* @param cbModule The module size.
* @param enmGuestOS The guest OS type.
* @param cRegions The number of regions.
* @param pszModuleName The module name.
* @param pszVersion The module version.
* @param paRegions The region descriptions.
* @param ppGblMod Where to return the new module on success.
*/
static int gmmR0ShModNewGlobal(PGMM pGMM, uint32_t uHash, uint32_t cbModule, VBOXOSFAMILY enmGuestOS,
uint32_t cRegions, const char *pszModuleName, const char *pszVersion,
struct VMMDEVSHAREDREGIONDESC const *paRegions, PGMMSHAREDMODULE *ppGblMod)
{
Log(("gmmR0ShModNewGlobal: %s %s size %#x os %u rgn %u\n", pszModuleName, pszVersion, cbModule, enmGuestOS, cRegions));
if (pGMM->cShareableModules >= GMM_MAX_SHARED_GLOBAL_MODULES)
{
Log(("gmmR0ShModNewGlobal: Too many modules\n"));
return VERR_GMM_TOO_MANY_GLOBAL_MODULES;
}
PGMMSHAREDMODULE pGblMod = (PGMMSHAREDMODULE)RTMemAllocZ(RT_UOFFSETOF_DYN(GMMSHAREDMODULE, aRegions[cRegions]));
if (!pGblMod)
{
Log(("gmmR0ShModNewGlobal: No memory\n"));
return VERR_NO_MEMORY;
}
pGblMod->Core.Key = uHash;
pGblMod->cbModule = cbModule;
pGblMod->cRegions = cRegions;
pGblMod->cUsers = 1;
pGblMod->enmGuestOS = enmGuestOS;
strcpy(pGblMod->szName, pszModuleName);
strcpy(pGblMod->szVersion, pszVersion);
for (uint32_t i = 0; i < cRegions; i++)
{
Log(("gmmR0ShModNewGlobal: rgn[%u]=%RGvLB%#x\n", i, paRegions[i].GCRegionAddr, paRegions[i].cbRegion));
pGblMod->aRegions[i].off = paRegions[i].GCRegionAddr & GUEST_PAGE_OFFSET_MASK;
pGblMod->aRegions[i].cb = paRegions[i].cbRegion + pGblMod->aRegions[i].off;
pGblMod->aRegions[i].cb = RT_ALIGN_32(pGblMod->aRegions[i].cb, GUEST_PAGE_SIZE);
pGblMod->aRegions[i].paidPages = NULL; /* allocated when needed. */
}
bool fInsert = RTAvllU32Insert(&pGMM->pGlobalSharedModuleTree, &pGblMod->Core);
Assert(fInsert); NOREF(fInsert);
pGMM->cShareableModules++;
*ppGblMod = pGblMod;
return VINF_SUCCESS;
}
/**
* Deletes a global module which is no longer referenced by anyone.
*
* @param pGMM The GMM instance data.
* @param pGblMod The module to delete.
*/
static void gmmR0ShModDeleteGlobal(PGMM pGMM, PGMMSHAREDMODULE pGblMod)
{
Assert(pGblMod->cUsers == 0);
Assert(pGMM->cShareableModules > 0 && pGMM->cShareableModules <= GMM_MAX_SHARED_GLOBAL_MODULES);
void *pvTest = RTAvllU32RemoveNode(&pGMM->pGlobalSharedModuleTree, &pGblMod->Core);
Assert(pvTest == pGblMod); NOREF(pvTest);
pGMM->cShareableModules--;
uint32_t i = pGblMod->cRegions;
while (i-- > 0)
{
if (pGblMod->aRegions[i].paidPages)
{
/* We don't doing anything to the pages as they are handled by the
copy-on-write mechanism in PGM. */
RTMemFree(pGblMod->aRegions[i].paidPages);
pGblMod->aRegions[i].paidPages = NULL;
}
}
RTMemFree(pGblMod);
}
static int gmmR0ShModNewPerVM(PGVM pGVM, RTGCPTR GCBaseAddr, uint32_t cRegions, const VMMDEVSHAREDREGIONDESC *paRegions,
PGMMSHAREDMODULEPERVM *ppRecVM)
{
if (pGVM->gmm.s.Stats.cShareableModules >= GMM_MAX_SHARED_PER_VM_MODULES)
return VERR_GMM_TOO_MANY_PER_VM_MODULES;
PGMMSHAREDMODULEPERVM pRecVM;
pRecVM = (PGMMSHAREDMODULEPERVM)RTMemAllocZ(RT_UOFFSETOF_DYN(GMMSHAREDMODULEPERVM, aRegionsGCPtrs[cRegions]));
if (!pRecVM)
return VERR_NO_MEMORY;
pRecVM->Core.Key = GCBaseAddr;
for (uint32_t i = 0; i < cRegions; i++)
pRecVM->aRegionsGCPtrs[i] = paRegions[i].GCRegionAddr;
bool fInsert = RTAvlGCPtrInsert(&pGVM->gmm.s.pSharedModuleTree, &pRecVM->Core);
Assert(fInsert); NOREF(fInsert);
pGVM->gmm.s.Stats.cShareableModules++;
*ppRecVM = pRecVM;
return VINF_SUCCESS;
}
static void gmmR0ShModDeletePerVM(PGMM pGMM, PGVM pGVM, PGMMSHAREDMODULEPERVM pRecVM, bool fRemove)
{
/*
* Free the per-VM module.
*/
PGMMSHAREDMODULE pGblMod = pRecVM->pGlobalModule;
pRecVM->pGlobalModule = NULL;
if (fRemove)
{
void *pvTest = RTAvlGCPtrRemove(&pGVM->gmm.s.pSharedModuleTree, pRecVM->Core.Key);
Assert(pvTest == &pRecVM->Core); NOREF(pvTest);
}
RTMemFree(pRecVM);
/*
* Release the global module.
* (In the registration bailout case, it might not be.)
*/
if (pGblMod)
{
Assert(pGblMod->cUsers > 0);
pGblMod->cUsers--;
if (pGblMod->cUsers == 0)
gmmR0ShModDeleteGlobal(pGMM, pGblMod);
}
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Registers a new shared module for the VM.
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param enmGuestOS The guest OS type.
* @param pszModuleName The module name.
* @param pszVersion The module version.
* @param GCPtrModBase The module base address.
* @param cbModule The module size.
* @param cRegions The mumber of shared region descriptors.
* @param paRegions Pointer to an array of shared region(s).
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0RegisterSharedModule(PGVM pGVM, VMCPUID idCpu, VBOXOSFAMILY enmGuestOS, char *pszModuleName,
char *pszVersion, RTGCPTR GCPtrModBase, uint32_t cbModule,
uint32_t cRegions, struct VMMDEVSHAREDREGIONDESC const *paRegions)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*
* Note! Turns out the module size does necessarily match the size of the
* regions. (iTunes on XP)
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
if (RT_UNLIKELY(cRegions > VMMDEVSHAREDREGIONDESC_MAX))
return VERR_GMM_TOO_MANY_REGIONS;
if (RT_UNLIKELY(cbModule == 0 || cbModule > _1G))
return VERR_GMM_BAD_SHARED_MODULE_SIZE;
uint32_t cbTotal = 0;
for (uint32_t i = 0; i < cRegions; i++)
{
if (RT_UNLIKELY(paRegions[i].cbRegion == 0 || paRegions[i].cbRegion > _1G))
return VERR_GMM_SHARED_MODULE_BAD_REGIONS_SIZE;
cbTotal += paRegions[i].cbRegion;
if (RT_UNLIKELY(cbTotal > _1G))
return VERR_GMM_SHARED_MODULE_BAD_REGIONS_SIZE;
}
AssertPtrReturn(pszModuleName, VERR_INVALID_POINTER);
if (RT_UNLIKELY(!memchr(pszModuleName, '\0', GMM_SHARED_MODULE_MAX_NAME_STRING)))
return VERR_GMM_MODULE_NAME_TOO_LONG;
AssertPtrReturn(pszVersion, VERR_INVALID_POINTER);
if (RT_UNLIKELY(!memchr(pszVersion, '\0', GMM_SHARED_MODULE_MAX_VERSION_STRING)))
return VERR_GMM_MODULE_NAME_TOO_LONG;
uint32_t const uHash = gmmR0ShModCalcHash(pszModuleName, pszVersion);
Log(("GMMR0RegisterSharedModule %s %s base %RGv size %x hash %x\n", pszModuleName, pszVersion, GCPtrModBase, cbModule, uHash));
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/*
* Check if this module is already locally registered and register
* it if it isn't. The base address is a unique module identifier
* locally.
*/
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCPtrModBase);
bool fNewModule = pRecVM == NULL;
if (fNewModule)
{
rc = gmmR0ShModNewPerVM(pGVM, GCPtrModBase, cRegions, paRegions, &pRecVM);
if (RT_SUCCESS(rc))
{
/*
* Find a matching global module, register a new one if needed.
*/
PGMMSHAREDMODULE pGblMod = gmmR0ShModFindGlobal(pGMM, uHash, cbModule, enmGuestOS, cRegions,
pszModuleName, pszVersion, paRegions);
if (!pGblMod)
{
Assert(fNewModule);
rc = gmmR0ShModNewGlobal(pGMM, uHash, cbModule, enmGuestOS, cRegions,
pszModuleName, pszVersion, paRegions, &pGblMod);
if (RT_SUCCESS(rc))
{
pRecVM->pGlobalModule = pGblMod; /* (One referenced returned by gmmR0ShModNewGlobal.) */
Log(("GMMR0RegisterSharedModule: new module %s %s\n", pszModuleName, pszVersion));
}
else
gmmR0ShModDeletePerVM(pGMM, pGVM, pRecVM, true /*fRemove*/);
}
else
{
Assert(pGblMod->cUsers > 0 && pGblMod->cUsers < UINT32_MAX / 2);
pGblMod->cUsers++;
pRecVM->pGlobalModule = pGblMod;
Log(("GMMR0RegisterSharedModule: new per vm module %s %s, gbl users %d\n", pszModuleName, pszVersion, pGblMod->cUsers));
}
}
}
else
{
/*
* Attempt to re-register an existing module.
*/
PGMMSHAREDMODULE pGblMod = gmmR0ShModFindGlobal(pGMM, uHash, cbModule, enmGuestOS, cRegions,
pszModuleName, pszVersion, paRegions);
if (pRecVM->pGlobalModule == pGblMod)
{
Log(("GMMR0RegisterSharedModule: already registered %s %s, gbl users %d\n", pszModuleName, pszVersion, pGblMod->cUsers));
rc = VINF_GMM_SHARED_MODULE_ALREADY_REGISTERED;
}
else
{
/** @todo may have to unregister+register when this happens in case it's caused
* by VBoxService crashing and being restarted... */
Log(("GMMR0RegisterSharedModule: Address clash!\n"
" incoming at %RGvLB%#x %s %s rgns %u\n"
" existing at %RGvLB%#x %s %s rgns %u\n",
GCPtrModBase, cbModule, pszModuleName, pszVersion, cRegions,
pRecVM->Core.Key, pRecVM->pGlobalModule->cbModule, pRecVM->pGlobalModule->szName,
pRecVM->pGlobalModule->szVersion, pRecVM->pGlobalModule->cRegions));
rc = VERR_GMM_SHARED_MODULE_ADDRESS_CLASH;
}
}
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
return rc;
#else
NOREF(pGVM); NOREF(idCpu); NOREF(enmGuestOS); NOREF(pszModuleName); NOREF(pszVersion);
NOREF(GCPtrModBase); NOREF(cbModule); NOREF(cRegions); NOREF(paRegions);
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0RegisterSharedModule.
*
* @returns see GMMR0RegisterSharedModule.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0RegisterSharedModuleReq(PGVM pGVM, VMCPUID idCpu, PGMMREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn( pReq->Hdr.cbReq >= sizeof(*pReq)
&& pReq->Hdr.cbReq == RT_UOFFSETOF_DYN(GMMREGISTERSHAREDMODULEREQ, aRegions[pReq->cRegions]),
("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
/* Pass back return code in the request packet to preserve informational codes. (VMMR3CallR0 chokes on them) */
pReq->rc = GMMR0RegisterSharedModule(pGVM, idCpu, pReq->enmGuestOS, pReq->szName, pReq->szVersion,
pReq->GCBaseAddr, pReq->cbModule, pReq->cRegions, pReq->aRegions);
return VINF_SUCCESS;
}
/**
* Unregisters a shared module for the VM
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pszModuleName The module name.
* @param pszVersion The module version.
* @param GCPtrModBase The module base address.
* @param cbModule The module size.
*/
GMMR0DECL(int) GMMR0UnregisterSharedModule(PGVM pGVM, VMCPUID idCpu, char *pszModuleName, char *pszVersion,
RTGCPTR GCPtrModBase, uint32_t cbModule)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
AssertPtrReturn(pszModuleName, VERR_INVALID_POINTER);
AssertPtrReturn(pszVersion, VERR_INVALID_POINTER);
if (RT_UNLIKELY(!memchr(pszModuleName, '\0', GMM_SHARED_MODULE_MAX_NAME_STRING)))
return VERR_GMM_MODULE_NAME_TOO_LONG;
if (RT_UNLIKELY(!memchr(pszVersion, '\0', GMM_SHARED_MODULE_MAX_VERSION_STRING)))
return VERR_GMM_MODULE_NAME_TOO_LONG;
Log(("GMMR0UnregisterSharedModule %s %s base=%RGv size %x\n", pszModuleName, pszVersion, GCPtrModBase, cbModule));
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/*
* Locate and remove the specified module.
*/
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)RTAvlGCPtrGet(&pGVM->gmm.s.pSharedModuleTree, GCPtrModBase);
if (pRecVM)
{
/** @todo Do we need to do more validations here, like that the
* name + version + cbModule matches? */
NOREF(cbModule);
Assert(pRecVM->pGlobalModule);
gmmR0ShModDeletePerVM(pGMM, pGVM, pRecVM, true /*fRemove*/);
}
else
rc = VERR_GMM_SHARED_MODULE_NOT_FOUND;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
return rc;
#else
NOREF(pGVM); NOREF(idCpu); NOREF(pszModuleName); NOREF(pszVersion); NOREF(GCPtrModBase); NOREF(cbModule);
return VERR_NOT_IMPLEMENTED;
#endif
}
/**
* VMMR0 request wrapper for GMMR0UnregisterSharedModule.
*
* @returns see GMMR0UnregisterSharedModule.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0UnregisterSharedModuleReq(PGVM pGVM, VMCPUID idCpu, PGMMUNREGISTERSHAREDMODULEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0UnregisterSharedModule(pGVM, idCpu, pReq->szName, pReq->szVersion, pReq->GCBaseAddr, pReq->cbModule);
}
#ifdef VBOX_WITH_PAGE_SHARING
/**
* Increase the use count of a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param pPage The page structure.
*/
DECLINLINE(void) gmmR0UseSharedPage(PGMM pGMM, PGVM pGVM, PGMMPAGE pPage)
{
Assert(pGMM->cSharedPages > 0);
Assert(pGMM->cAllocatedPages > 0);
pGMM->cDuplicatePages++;
pPage->Shared.cRefs++;
pGVM->gmm.s.Stats.cSharedPages++;
pGVM->gmm.s.Stats.Allocated.cBasePages++;
}
/**
* Converts a private page to a shared page, the page is known to exist and be valid and such.
*
* @param pGMM Pointer to the GMM instance.
* @param pGVM Pointer to the GVM instance.
* @param HCPhys Host physical address
* @param idPage The Page ID
* @param pPage The page structure.
* @param pPageDesc Shared page descriptor
*/
DECLINLINE(void) gmmR0ConvertToSharedPage(PGMM pGMM, PGVM pGVM, RTHCPHYS HCPhys, uint32_t idPage, PGMMPAGE pPage,
PGMMSHAREDPAGEDESC pPageDesc)
{
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, idPage >> GMM_CHUNKID_SHIFT);
Assert(pChunk);
Assert(pChunk->cFree < GMM_CHUNK_NUM_PAGES);
Assert(GMM_PAGE_IS_PRIVATE(pPage));
pChunk->cPrivate--;
pChunk->cShared++;
pGMM->cSharedPages++;
pGVM->gmm.s.Stats.cSharedPages++;
pGVM->gmm.s.Stats.cPrivatePages--;
/* Modify the page structure. */
pPage->Shared.pfn = (uint32_t)(uint64_t)(HCPhys >> GUEST_PAGE_SHIFT);
pPage->Shared.cRefs = 1;
#ifdef VBOX_STRICT
pPageDesc->u32StrictChecksum = gmmR0StrictPageChecksum(pGMM, pGVM, idPage);
pPage->Shared.u14Checksum = pPageDesc->u32StrictChecksum;
#else
NOREF(pPageDesc);
pPage->Shared.u14Checksum = 0;
#endif
pPage->Shared.u2State = GMM_PAGE_STATE_SHARED;
}
static int gmmR0SharedModuleCheckPageFirstTime(PGMM pGMM, PGVM pGVM, PGMMSHAREDMODULE pModule,
unsigned idxRegion, unsigned idxPage,
PGMMSHAREDPAGEDESC pPageDesc, PGMMSHAREDREGIONDESC pGlobalRegion)
{
NOREF(pModule);
/* Easy case: just change the internal page type. */
PGMMPAGE pPage = gmmR0GetPage(pGMM, pPageDesc->idPage);
AssertMsgReturn(pPage, ("idPage=%#x (GCPhys=%RGp HCPhys=%RHp idxRegion=%#x idxPage=%#x) #1\n",
pPageDesc->idPage, pPageDesc->GCPhys, pPageDesc->HCPhys, idxRegion, idxPage),
VERR_PGM_PHYS_INVALID_PAGE_ID);
NOREF(idxRegion);
AssertMsg(pPageDesc->GCPhys == (pPage->Private.pfn << 12), ("desc %RGp gmm %RGp\n", pPageDesc->HCPhys, (pPage->Private.pfn << 12)));
gmmR0ConvertToSharedPage(pGMM, pGVM, pPageDesc->HCPhys, pPageDesc->idPage, pPage, pPageDesc);
/* Keep track of these references. */
pGlobalRegion->paidPages[idxPage] = pPageDesc->idPage;
return VINF_SUCCESS;
}
/**
* Checks specified shared module range for changes
*
* Performs the following tasks:
* - If a shared page is new, then it changes the GMM page type to shared and
* returns it in the pPageDesc descriptor.
* - If a shared page already exists, then it checks if the VM page is
* identical and if so frees the VM page and returns the shared page in
* pPageDesc descriptor.
*
* @remarks ASSUMES the caller has acquired the GMM semaphore!!
*
* @returns VBox status code.
* @param pGVM Pointer to the GVM instance data.
* @param pModule Module description
* @param idxRegion Region index
* @param idxPage Page index
* @param pPageDesc Page descriptor
*/
GMMR0DECL(int) GMMR0SharedModuleCheckPage(PGVM pGVM, PGMMSHAREDMODULE pModule, uint32_t idxRegion, uint32_t idxPage,
PGMMSHAREDPAGEDESC pPageDesc)
{
int rc;
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
pPageDesc->u32StrictChecksum = 0;
AssertMsgReturn(idxRegion < pModule->cRegions,
("idxRegion=%#x cRegions=%#x %s %s\n", idxRegion, pModule->cRegions, pModule->szName, pModule->szVersion),
VERR_INVALID_PARAMETER);
uint32_t const cPages = pModule->aRegions[idxRegion].cb >> GUEST_PAGE_SHIFT;
AssertMsgReturn(idxPage < cPages,
("idxRegion=%#x cRegions=%#x %s %s\n", idxRegion, pModule->cRegions, pModule->szName, pModule->szVersion),
VERR_INVALID_PARAMETER);
LogFlow(("GMMR0SharedModuleCheckRange %s base %RGv region %d idxPage %d\n", pModule->szName, pModule->Core.Key, idxRegion, idxPage));
/*
* First time; create a page descriptor array.
*/
PGMMSHAREDREGIONDESC pGlobalRegion = &pModule->aRegions[idxRegion];
if (!pGlobalRegion->paidPages)
{
Log(("Allocate page descriptor array for %d pages\n", cPages));
pGlobalRegion->paidPages = (uint32_t *)RTMemAlloc(cPages * sizeof(pGlobalRegion->paidPages[0]));
AssertReturn(pGlobalRegion->paidPages, VERR_NO_MEMORY);
/* Invalidate all descriptors. */
uint32_t i = cPages;
while (i-- > 0)
pGlobalRegion->paidPages[i] = NIL_GMM_PAGEID;
}
/*
* We've seen this shared page for the first time?
*/
if (pGlobalRegion->paidPages[idxPage] == NIL_GMM_PAGEID)
{
Log(("New shared page guest %RGp host %RHp\n", pPageDesc->GCPhys, pPageDesc->HCPhys));
return gmmR0SharedModuleCheckPageFirstTime(pGMM, pGVM, pModule, idxRegion, idxPage, pPageDesc, pGlobalRegion);
}
/*
* We've seen it before...
*/
Log(("Replace existing page guest %RGp host %RHp id %#x -> id %#x\n",
pPageDesc->GCPhys, pPageDesc->HCPhys, pPageDesc->idPage, pGlobalRegion->paidPages[idxPage]));
Assert(pPageDesc->idPage != pGlobalRegion->paidPages[idxPage]);
/*
* Get the shared page source.
*/
PGMMPAGE pPage = gmmR0GetPage(pGMM, pGlobalRegion->paidPages[idxPage]);
AssertMsgReturn(pPage, ("idPage=%#x (idxRegion=%#x idxPage=%#x) #2\n", pPageDesc->idPage, idxRegion, idxPage),
VERR_PGM_PHYS_INVALID_PAGE_ID);
if (pPage->Common.u2State != GMM_PAGE_STATE_SHARED)
{
/*
* Page was freed at some point; invalidate this entry.
*/
/** @todo this isn't really bullet proof. */
Log(("Old shared page was freed -> create a new one\n"));
pGlobalRegion->paidPages[idxPage] = NIL_GMM_PAGEID;
return gmmR0SharedModuleCheckPageFirstTime(pGMM, pGVM, pModule, idxRegion, idxPage, pPageDesc, pGlobalRegion);
}
Log(("Replace existing page guest host %RHp -> %RHp\n", pPageDesc->HCPhys, ((uint64_t)pPage->Shared.pfn) << GUEST_PAGE_SHIFT));
/*
* Calculate the virtual address of the local page.
*/
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, pPageDesc->idPage >> GMM_CHUNKID_SHIFT);
AssertMsgReturn(pChunk, ("idPage=%#x (idxRegion=%#x idxPage=%#x) #4\n", pPageDesc->idPage, idxRegion, idxPage),
VERR_PGM_PHYS_INVALID_PAGE_ID);
uint8_t *pbChunk;
AssertMsgReturn(gmmR0IsChunkMapped(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk),
("idPage=%#x (idxRegion=%#x idxPage=%#x) #3\n", pPageDesc->idPage, idxRegion, idxPage),
VERR_PGM_PHYS_INVALID_PAGE_ID);
uint8_t *pbLocalPage = pbChunk + ((pPageDesc->idPage & GMM_PAGEID_IDX_MASK) << GUEST_PAGE_SHIFT);
/*
* Calculate the virtual address of the shared page.
*/
pChunk = gmmR0GetChunk(pGMM, pGlobalRegion->paidPages[idxPage] >> GMM_CHUNKID_SHIFT);
Assert(pChunk); /* can't fail as gmmR0GetPage succeeded. */
/*
* Get the virtual address of the physical page; map the chunk into the VM
* process if not already done.
*/
if (!gmmR0IsChunkMapped(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
Log(("Map chunk into process!\n"));
rc = gmmR0MapChunk(pGMM, pGVM, pChunk, false /*fRelaxedSem*/, (PRTR3PTR)&pbChunk);
AssertRCReturn(rc, rc);
}
uint8_t *pbSharedPage = pbChunk + ((pGlobalRegion->paidPages[idxPage] & GMM_PAGEID_IDX_MASK) << GUEST_PAGE_SHIFT);
#ifdef VBOX_STRICT
pPageDesc->u32StrictChecksum = RTCrc32(pbSharedPage, GUEST_PAGE_SIZE);
uint32_t uChecksum = pPageDesc->u32StrictChecksum & UINT32_C(0x00003fff);
AssertMsg(!uChecksum || uChecksum == pPage->Shared.u14Checksum || !pPage->Shared.u14Checksum,
("%#x vs %#x - idPage=%#x - %s %s\n", uChecksum, pPage->Shared.u14Checksum,
pGlobalRegion->paidPages[idxPage], pModule->szName, pModule->szVersion));
#endif
if (memcmp(pbSharedPage, pbLocalPage, GUEST_PAGE_SIZE))
{
Log(("Unexpected differences found between local and shared page; skip\n"));
/* Signal to the caller that this one hasn't changed. */
pPageDesc->idPage = NIL_GMM_PAGEID;
return VINF_SUCCESS;
}
/*
* Free the old local page.
*/
GMMFREEPAGEDESC PageDesc;
PageDesc.idPage = pPageDesc->idPage;
rc = gmmR0FreePages(pGMM, pGVM, 1, &PageDesc, GMMACCOUNT_BASE);
AssertRCReturn(rc, rc);
gmmR0UseSharedPage(pGMM, pGVM, pPage);
/*
* Pass along the new physical address & page id.
*/
pPageDesc->HCPhys = ((uint64_t)pPage->Shared.pfn) << GUEST_PAGE_SHIFT;
pPageDesc->idPage = pGlobalRegion->paidPages[idxPage];
return VINF_SUCCESS;
}
/**
* RTAvlGCPtrDestroy callback.
*
* @returns 0 or VERR_GMM_INSTANCE.
* @param pNode The node to destroy.
* @param pvArgs Pointer to an argument packet.
*/
static DECLCALLBACK(int) gmmR0CleanupSharedModule(PAVLGCPTRNODECORE pNode, void *pvArgs)
{
gmmR0ShModDeletePerVM(((GMMR0SHMODPERVMDTORARGS *)pvArgs)->pGMM,
((GMMR0SHMODPERVMDTORARGS *)pvArgs)->pGVM,
(PGMMSHAREDMODULEPERVM)pNode,
false /*fRemove*/);
return VINF_SUCCESS;
}
/**
* Used by GMMR0CleanupVM to clean up shared modules.
*
* This is called without taking the GMM lock so that it can be yielded as
* needed here.
*
* @param pGMM The GMM handle.
* @param pGVM The global VM handle.
*/
static void gmmR0SharedModuleCleanup(PGMM pGMM, PGVM pGVM)
{
gmmR0MutexAcquire(pGMM);
GMM_CHECK_SANITY_UPON_ENTERING(pGMM);
GMMR0SHMODPERVMDTORARGS Args;
Args.pGVM = pGVM;
Args.pGMM = pGMM;
RTAvlGCPtrDestroy(&pGVM->gmm.s.pSharedModuleTree, gmmR0CleanupSharedModule, &Args);
AssertMsg(pGVM->gmm.s.Stats.cShareableModules == 0, ("%d\n", pGVM->gmm.s.Stats.cShareableModules));
pGVM->gmm.s.Stats.cShareableModules = 0;
gmmR0MutexRelease(pGMM);
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Removes all shared modules for the specified VM
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The VCPU id.
*/
GMMR0DECL(int) GMMR0ResetSharedModules(PGVM pGVM, VMCPUID idCpu)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
Log(("GMMR0ResetSharedModules\n"));
GMMR0SHMODPERVMDTORARGS Args;
Args.pGVM = pGVM;
Args.pGMM = pGMM;
RTAvlGCPtrDestroy(&pGVM->gmm.s.pSharedModuleTree, gmmR0CleanupSharedModule, &Args);
pGVM->gmm.s.Stats.cShareableModules = 0;
rc = VINF_SUCCESS;
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
return rc;
#else
RT_NOREF(pGVM, idCpu);
return VERR_NOT_IMPLEMENTED;
#endif
}
#ifdef VBOX_WITH_PAGE_SHARING
/**
* Tree enumeration callback for checking a shared module.
*/
static DECLCALLBACK(int) gmmR0CheckSharedModule(PAVLGCPTRNODECORE pNode, void *pvUser)
{
GMMCHECKSHAREDMODULEINFO *pArgs = (GMMCHECKSHAREDMODULEINFO*)pvUser;
PGMMSHAREDMODULEPERVM pRecVM = (PGMMSHAREDMODULEPERVM)pNode;
PGMMSHAREDMODULE pGblMod = pRecVM->pGlobalModule;
Log(("gmmR0CheckSharedModule: check %s %s base=%RGv size=%x\n",
pGblMod->szName, pGblMod->szVersion, pGblMod->Core.Key, pGblMod->cbModule));
int rc = PGMR0SharedModuleCheck(pArgs->pGVM, pArgs->pGVM, pArgs->idCpu, pGblMod, pRecVM->aRegionsGCPtrs);
if (RT_FAILURE(rc))
return rc;
return VINF_SUCCESS;
}
#endif /* VBOX_WITH_PAGE_SHARING */
/**
* Check all shared modules for the specified VM.
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param idCpu The calling EMT number.
* @thread EMT(idCpu)
*/
GMMR0DECL(int) GMMR0CheckSharedModules(PGVM pGVM, VMCPUID idCpu)
{
#ifdef VBOX_WITH_PAGE_SHARING
/*
* Validate input and get the basics.
*/
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVMandEMT(pGVM, idCpu);
if (RT_FAILURE(rc))
return rc;
# ifndef DEBUG_sandervl
/*
* Take the semaphore and do some more validations.
*/
gmmR0MutexAcquire(pGMM);
# endif
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
/*
* Walk the tree, checking each module.
*/
Log(("GMMR0CheckSharedModules\n"));
GMMCHECKSHAREDMODULEINFO Args;
Args.pGVM = pGVM;
Args.idCpu = idCpu;
rc = RTAvlGCPtrDoWithAll(&pGVM->gmm.s.pSharedModuleTree, true /* fFromLeft */, gmmR0CheckSharedModule, &Args);
Log(("GMMR0CheckSharedModules done (rc=%Rrc)!\n", rc));
GMM_CHECK_SANITY_UPON_LEAVING(pGMM);
}
else
rc = VERR_GMM_IS_NOT_SANE;
# ifndef DEBUG_sandervl
gmmR0MutexRelease(pGMM);
# endif
return rc;
#else
RT_NOREF(pGVM, idCpu);
return VERR_NOT_IMPLEMENTED;
#endif
}
#ifdef VBOX_STRICT
/**
* Worker for GMMR0FindDuplicatePageReq.
*
* @returns true if duplicate, false if not.
*/
static bool gmmR0FindDupPageInChunk(PGMM pGMM, PGVM pGVM, PGMMCHUNK pChunk, uint8_t const *pbSourcePage)
{
bool fFoundDuplicate = false;
/* Only take chunks not mapped into this VM process; not entirely correct. */
uint8_t *pbChunk;
if (!gmmR0IsChunkMapped(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
int rc = gmmR0MapChunk(pGMM, pGVM, pChunk, false /*fRelaxedSem*/, (PRTR3PTR)&pbChunk);
if (RT_SUCCESS(rc))
{
/*
* Look for duplicate pages
*/
uintptr_t iPage = GMM_CHUNK_NUM_PAGES;
while (iPage-- > 0)
{
if (GMM_PAGE_IS_PRIVATE(&pChunk->aPages[iPage]))
{
uint8_t *pbDestPage = pbChunk + (iPage << GUEST_PAGE_SHIFT);
if (!memcmp(pbSourcePage, pbDestPage, GUEST_PAGE_SIZE))
{
fFoundDuplicate = true;
break;
}
}
}
gmmR0UnmapChunk(pGMM, pGVM, pChunk, false /*fRelaxedSem*/);
}
}
return fFoundDuplicate;
}
/**
* Find a duplicate of the specified page in other active VMs
*
* @returns VBox status code.
* @param pGVM The global (ring-0) VM structure.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0FindDuplicatePageReq(PGVM pGVM, PGMMFINDDUPLICATEPAGEREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
int rc = GVMMR0ValidateGVM(pGVM);
if (RT_FAILURE(rc))
return rc;
/*
* Take the semaphore and do some more validations.
*/
rc = gmmR0MutexAcquire(pGMM);
if (GMM_CHECK_SANITY_UPON_ENTERING(pGMM))
{
uint8_t *pbChunk;
PGMMCHUNK pChunk = gmmR0GetChunk(pGMM, pReq->idPage >> GMM_CHUNKID_SHIFT);
if (pChunk)
{
if (gmmR0IsChunkMapped(pGMM, pGVM, pChunk, (PRTR3PTR)&pbChunk))
{
uint8_t *pbSourcePage = pbChunk + ((pReq->idPage & GMM_PAGEID_IDX_MASK) << GUEST_PAGE_SHIFT);
PGMMPAGE pPage = gmmR0GetPage(pGMM, pReq->idPage);
if (pPage)
{
/*
* Walk the chunks
*/
pReq->fDuplicate = false;
RTListForEach(&pGMM->ChunkList, pChunk, GMMCHUNK, ListNode)
{
if (gmmR0FindDupPageInChunk(pGMM, pGVM, pChunk, pbSourcePage))
{
pReq->fDuplicate = true;
break;
}
}
}
else
{
AssertFailed();
rc = VERR_PGM_PHYS_INVALID_PAGE_ID;
}
}
else
AssertFailed();
}
else
AssertFailed();
}
else
rc = VERR_GMM_IS_NOT_SANE;
gmmR0MutexRelease(pGMM);
return rc;
}
#endif /* VBOX_STRICT */
/**
* Retrieves the GMM statistics visible to the caller.
*
* @returns VBox status code.
*
* @param pStats Where to put the statistics.
* @param pSession The current session.
* @param pGVM The GVM to obtain statistics for. Optional.
*/
GMMR0DECL(int) GMMR0QueryStatistics(PGMMSTATS pStats, PSUPDRVSESSION pSession, PGVM pGVM)
{
LogFlow(("GVMMR0QueryStatistics: pStats=%p pSession=%p pGVM=%p\n", pStats, pSession, pGVM));
/*
* Validate input.
*/
AssertPtrReturn(pSession, VERR_INVALID_POINTER);
AssertPtrReturn(pStats, VERR_INVALID_POINTER);
pStats->cMaxPages = 0; /* (crash before taking the mutex...) */
PGMM pGMM;
GMM_GET_VALID_INSTANCE(pGMM, VERR_GMM_INSTANCE);
/*
* Validate the VM handle, if not NULL, and lock the GMM.
*/
int rc;
if (pGVM)
{
rc = GVMMR0ValidateGVM(pGVM);
if (RT_FAILURE(rc))
return rc;
}
rc = gmmR0MutexAcquire(pGMM);
if (RT_FAILURE(rc))
return rc;
/*
* Copy out the GMM statistics.
*/
pStats->cMaxPages = pGMM->cMaxPages;
pStats->cReservedPages = pGMM->cReservedPages;
pStats->cOverCommittedPages = pGMM->cOverCommittedPages;
pStats->cAllocatedPages = pGMM->cAllocatedPages;
pStats->cSharedPages = pGMM->cSharedPages;
pStats->cDuplicatePages = pGMM->cDuplicatePages;
pStats->cLeftBehindSharedPages = pGMM->cLeftBehindSharedPages;
pStats->cBalloonedPages = pGMM->cBalloonedPages;
pStats->cChunks = pGMM->cChunks;
pStats->cFreedChunks = pGMM->cFreedChunks;
pStats->cShareableModules = pGMM->cShareableModules;
pStats->idFreeGeneration = pGMM->idFreeGeneration;
RT_ZERO(pStats->au64Reserved);
/*
* Copy out the VM statistics.
*/
if (pGVM)
pStats->VMStats = pGVM->gmm.s.Stats;
else
RT_ZERO(pStats->VMStats);
gmmR0MutexRelease(pGMM);
return rc;
}
/**
* VMMR0 request wrapper for GMMR0QueryStatistics.
*
* @returns see GMMR0QueryStatistics.
* @param pGVM The global (ring-0) VM structure. Optional.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0QueryStatisticsReq(PGVM pGVM, PGMMQUERYSTATISTICSSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0QueryStatistics(&pReq->Stats, pReq->pSession, pGVM);
}
/**
* Resets the specified GMM statistics.
*
* @returns VBox status code.
*
* @param pStats Which statistics to reset, that is, non-zero fields
* indicates which to reset.
* @param pSession The current session.
* @param pGVM The GVM to reset statistics for. Optional.
*/
GMMR0DECL(int) GMMR0ResetStatistics(PCGMMSTATS pStats, PSUPDRVSESSION pSession, PGVM pGVM)
{
NOREF(pStats); NOREF(pSession); NOREF(pGVM);
/* Currently nothing we can reset at the moment. */
return VINF_SUCCESS;
}
/**
* VMMR0 request wrapper for GMMR0ResetStatistics.
*
* @returns see GMMR0ResetStatistics.
* @param pGVM The global (ring-0) VM structure. Optional.
* @param pReq Pointer to the request packet.
*/
GMMR0DECL(int) GMMR0ResetStatisticsReq(PGVM pGVM, PGMMRESETSTATISTICSSREQ pReq)
{
/*
* Validate input and pass it on.
*/
AssertPtrReturn(pReq, VERR_INVALID_POINTER);
AssertMsgReturn(pReq->Hdr.cbReq == sizeof(*pReq), ("%#x != %#x\n", pReq->Hdr.cbReq, sizeof(*pReq)), VERR_INVALID_PARAMETER);
return GMMR0ResetStatistics(&pReq->Stats, pReq->pSession, pGVM);
}
|