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
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 1992, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/*
* Copyright (c) 2010, Intel Corporation.
* All rights reserved.
* Copyright 2019, Joyent, Inc.
*/
/* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Portions of this source code were derived from Berkeley 4.3 BSD
* under license from the Regents of the University of California.
*/
/*
* UNIX machine dependent virtual memory support.
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/user.h>
#include <sys/proc.h>
#include <sys/kmem.h>
#include <sys/vmem.h>
#include <sys/buf.h>
#include <sys/cpuvar.h>
#include <sys/lgrp.h>
#include <sys/disp.h>
#include <sys/vm.h>
#include <sys/mman.h>
#include <sys/vnode.h>
#include <sys/cred.h>
#include <sys/exec.h>
#include <sys/exechdr.h>
#include <sys/debug.h>
#include <sys/vmsystm.h>
#include <sys/swap.h>
#include <sys/dumphdr.h>
#include <sys/random.h>
#include <vm/hat.h>
#include <vm/as.h>
#include <vm/seg.h>
#include <vm/seg_kp.h>
#include <vm/seg_vn.h>
#include <vm/page.h>
#include <vm/seg_kmem.h>
#include <vm/seg_kpm.h>
#include <vm/vm_dep.h>
#include <sys/cpu.h>
#include <sys/vm_machparam.h>
#include <sys/memlist.h>
#include <sys/bootconf.h> /* XXX the memlist stuff belongs in memlist_plat.h */
#include <vm/hat_i86.h>
#include <sys/x86_archext.h>
#include <sys/elf_386.h>
#include <sys/cmn_err.h>
#include <sys/archsystm.h>
#include <sys/machsystm.h>
#include <sys/secflags.h>
#include <sys/vtrace.h>
#include <sys/ddidmareq.h>
#include <sys/promif.h>
#include <sys/memnode.h>
#include <sys/stack.h>
#include <util/qsort.h>
#include <sys/taskq.h>
#ifdef __xpv
#include <sys/hypervisor.h>
#include <sys/xen_mmu.h>
#include <sys/balloon_impl.h>
/*
* domain 0 pages usable for DMA are kept pre-allocated and kept in
* distinct lists, ordered by increasing mfn.
*/
static kmutex_t io_pool_lock;
static kmutex_t contig_list_lock;
static page_t *io_pool_4g; /* pool for 32 bit dma limited devices */
static page_t *io_pool_16m; /* pool for 24 bit dma limited legacy devices */
static long io_pool_cnt;
static long io_pool_cnt_max = 0;
#define DEFAULT_IO_POOL_MIN 128
static long io_pool_cnt_min = DEFAULT_IO_POOL_MIN;
static long io_pool_cnt_lowater = 0;
static long io_pool_shrink_attempts; /* how many times did we try to shrink */
static long io_pool_shrinks; /* how many times did we really shrink */
static long io_pool_grows; /* how many times did we grow */
static mfn_t start_mfn = 1;
static caddr_t io_pool_kva; /* use to alloc pages when needed */
static int create_contig_pfnlist(uint_t);
/*
* percentage of phys mem to hold in the i/o pool
*/
#define DEFAULT_IO_POOL_PCT 2
static long io_pool_physmem_pct = DEFAULT_IO_POOL_PCT;
static void page_io_pool_sub(page_t **, page_t *, page_t *);
int ioalloc_dbg = 0;
#endif /* __xpv */
uint_t vac_colors = 1;
int largepagesupport = 0;
extern uint_t page_create_new;
extern uint_t page_create_exists;
extern uint_t page_create_putbacks;
/*
* Allow users to disable the kernel's use of SSE.
*/
extern int use_sse_pagecopy, use_sse_pagezero;
/*
* combined memory ranges from mnode and memranges[] to manage single
* mnode/mtype dimension in the page lists.
*/
typedef struct {
pfn_t mnr_pfnlo;
pfn_t mnr_pfnhi;
int mnr_mnode;
int mnr_memrange; /* index into memranges[] */
int mnr_next; /* next lower PA mnoderange */
int mnr_exists;
/* maintain page list stats */
pgcnt_t mnr_mt_clpgcnt; /* cache list cnt */
pgcnt_t mnr_mt_flpgcnt[MMU_PAGE_SIZES]; /* free list cnt per szc */
pgcnt_t mnr_mt_totcnt; /* sum of cache and free lists */
#ifdef DEBUG
struct mnr_mts { /* mnode/mtype szc stats */
pgcnt_t mnr_mts_pgcnt;
int mnr_mts_colors;
pgcnt_t *mnr_mtsc_pgcnt;
} *mnr_mts;
#endif
} mnoderange_t;
#define MEMRANGEHI(mtype) \
((mtype > 0) ? memranges[mtype - 1] - 1: physmax)
#define MEMRANGELO(mtype) (memranges[mtype])
#define MTYPE_FREEMEM(mt) (mnoderanges[mt].mnr_mt_totcnt)
/*
* As the PC architecture evolved memory up was clumped into several
* ranges for various historical I/O devices to do DMA.
* < 16Meg - ISA bus
* < 2Gig - ???
* < 4Gig - PCI bus or drivers that don't understand PAE mode
*
* These are listed in reverse order, so that we can skip over unused
* ranges on machines with small memories.
*
* For now under the Hypervisor, we'll only ever have one memrange.
*/
#define PFN_4GIG 0x100000
#define PFN_16MEG 0x1000
/* Indices into the memory range (arch_memranges) array. */
#define MRI_4G 0
#define MRI_2G 1
#define MRI_16M 2
#define MRI_0 3
static pfn_t arch_memranges[NUM_MEM_RANGES] = {
PFN_4GIG, /* pfn range for 4G and above */
0x80000, /* pfn range for 2G-4G */
PFN_16MEG, /* pfn range for 16M-2G */
0x00000, /* pfn range for 0-16M */
};
pfn_t *memranges = &arch_memranges[0];
int nranges = NUM_MEM_RANGES;
/*
* This combines mem_node_config and memranges into one data
* structure to be used for page list management.
*/
static mnoderange_t *mnoderanges;
static int mnoderangecnt;
static int mtype4g;
static int mtype16m;
static int mtypetop;
/*
* 4g memory management variables for systems with more than 4g of memory:
*
* physical memory below 4g is required for 32bit dma devices and, currently,
* for kmem memory. On systems with more than 4g of memory, the pool of memory
* below 4g can be depleted without any paging activity given that there is
* likely to be sufficient memory above 4g.
*
* physmax4g is set true if the largest pfn is over 4g. The rest of the
* 4g memory management code is enabled only when physmax4g is true.
*
* maxmem4g is the count of the maximum number of pages on the page lists
* with physical addresses below 4g. It can be a lot less then 4g given that
* BIOS may reserve large chunks of space below 4g for hot plug pci devices,
* agp aperture etc.
*
* freemem4g maintains the count of the number of available pages on the
* page lists with physical addresses below 4g.
*
* DESFREE4G specifies the desired amount of below 4g memory. It defaults to
* 6% (desfree4gshift = 4) of maxmem4g.
*
* RESTRICT4G_ALLOC returns true if freemem4g falls below DESFREE4G
* and the amount of physical memory above 4g is greater than freemem4g.
* In this case, page_get_* routines will restrict below 4g allocations
* for requests that don't specifically require it.
*/
#define DESFREE4G (maxmem4g >> desfree4gshift)
#define RESTRICT4G_ALLOC \
(physmax4g && (freemem4g < DESFREE4G) && ((freemem4g << 1) < freemem))
static pgcnt_t maxmem4g;
static pgcnt_t freemem4g;
static int physmax4g;
static int desfree4gshift = 4; /* maxmem4g shift to derive DESFREE4G */
/*
* 16m memory management:
*
* reserve some amount of physical memory below 16m for legacy devices.
*
* RESTRICT16M_ALLOC returns true if an there are sufficient free pages above
* 16m or if the 16m pool drops below DESFREE16M.
*
* In this case, general page allocations via page_get_{free,cache}list
* routines will be restricted from allocating from the 16m pool. Allocations
* that require specific pfn ranges (page_get_anylist) and PG_PANIC allocations
* are not restricted.
*/
#define FREEMEM16M MTYPE_FREEMEM(mtype16m)
#define DESFREE16M desfree16m
#define RESTRICT16M_ALLOC(freemem, pgcnt, flags) \
(mtype16m != -1 && (freemem != 0) && ((flags & PG_PANIC) == 0) && \
((freemem >= (FREEMEM16M)) || \
(FREEMEM16M < (DESFREE16M + pgcnt))))
static pgcnt_t desfree16m = 0x380;
/*
* This can be patched via /etc/system to allow old non-PAE aware device
* drivers to use kmem_alloc'd memory on 32 bit systems with > 4Gig RAM.
*/
int restricted_kmemalloc = 0;
#ifdef VM_STATS
struct {
ulong_t pga_alloc;
ulong_t pga_notfullrange;
ulong_t pga_nulldmaattr;
ulong_t pga_allocok;
ulong_t pga_allocfailed;
ulong_t pgma_alloc;
ulong_t pgma_allocok;
ulong_t pgma_allocfailed;
ulong_t pgma_allocempty;
} pga_vmstats;
#endif
uint_t mmu_page_sizes;
/* How many page sizes the users can see */
uint_t mmu_exported_page_sizes;
/* page sizes that legacy applications can see */
uint_t mmu_legacy_page_sizes;
/*
* Number of pages in 1 GB. Don't enable automatic large pages if we have
* fewer than this many pages.
*/
pgcnt_t shm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
pgcnt_t privm_lpg_min_physmem = 1 << (30 - MMU_PAGESHIFT);
/*
* Maximum and default segment size tunables for user private
* and shared anon memory, and user text and initialized data.
* These can be patched via /etc/system to allow large pages
* to be used for mapping application private and shared anon memory.
*/
size_t mcntl0_lpsize = MMU_PAGESIZE;
size_t max_uheap_lpsize = MMU_PAGESIZE;
size_t default_uheap_lpsize = MMU_PAGESIZE;
size_t max_ustack_lpsize = MMU_PAGESIZE;
size_t default_ustack_lpsize = MMU_PAGESIZE;
size_t max_privmap_lpsize = MMU_PAGESIZE;
size_t max_uidata_lpsize = MMU_PAGESIZE;
size_t max_utext_lpsize = MMU_PAGESIZE;
size_t max_shm_lpsize = MMU_PAGESIZE;
/*
* initialized by page_coloring_init().
*/
uint_t page_colors;
uint_t page_colors_mask;
uint_t page_coloring_shift;
int cpu_page_colors;
static uint_t l2_colors;
/*
* Page freelists and cachelists are dynamically allocated once mnoderangecnt
* and page_colors are calculated from the l2 cache n-way set size. Within a
* mnode range, the page freelist and cachelist are hashed into bins based on
* color. This makes it easier to search for a page within a specific memory
* range.
*/
#define PAGE_COLORS_MIN 16
page_t ****page_freelists;
page_t ***page_cachelists;
/*
* Used by page layer to know about page sizes
*/
hw_pagesize_t hw_page_array[MAX_NUM_LEVEL + 1];
kmutex_t *fpc_mutex[NPC_MUTEX];
kmutex_t *cpc_mutex[NPC_MUTEX];
/* Lock to protect mnoderanges array for memory DR operations. */
static kmutex_t mnoderange_lock;
/*
* Only let one thread at a time try to coalesce large pages, to
* prevent them from working against each other.
*/
static kmutex_t contig_lock;
#define CONTIG_LOCK() mutex_enter(&contig_lock);
#define CONTIG_UNLOCK() mutex_exit(&contig_lock);
#define PFN_16M (mmu_btop((uint64_t)0x1000000))
caddr_t
i86devmap(pfn_t pf, pgcnt_t pgcnt, uint_t prot)
{
caddr_t addr;
caddr_t addr1;
page_t *pp;
addr1 = addr = vmem_alloc(heap_arena, mmu_ptob(pgcnt), VM_SLEEP);
for (; pgcnt != 0; addr += MMU_PAGESIZE, ++pf, --pgcnt) {
pp = page_numtopp_nolock(pf);
if (pp == NULL) {
hat_devload(kas.a_hat, addr, MMU_PAGESIZE, pf,
prot | HAT_NOSYNC, HAT_LOAD_LOCK);
} else {
hat_memload(kas.a_hat, addr, pp,
prot | HAT_NOSYNC, HAT_LOAD_LOCK);
}
}
return (addr1);
}
/*
* This routine is like page_numtopp, but accepts only free pages, which
* it allocates (unfrees) and returns with the exclusive lock held.
* It is used by machdep.c/dma_init() to find contiguous free pages.
*/
page_t *
page_numtopp_alloc(pfn_t pfnum)
{
page_t *pp;
retry:
pp = page_numtopp_nolock(pfnum);
if (pp == NULL) {
return (NULL);
}
if (!page_trylock(pp, SE_EXCL)) {
return (NULL);
}
if (page_pptonum(pp) != pfnum) {
page_unlock(pp);
goto retry;
}
if (!PP_ISFREE(pp)) {
page_unlock(pp);
return (NULL);
}
if (pp->p_szc) {
page_demote_free_pages(pp);
page_unlock(pp);
goto retry;
}
/* If associated with a vnode, destroy mappings */
if (pp->p_vnode) {
page_destroy_free(pp);
if (!page_lock(pp, SE_EXCL, (kmutex_t *)NULL, P_NO_RECLAIM)) {
return (NULL);
}
if (page_pptonum(pp) != pfnum) {
page_unlock(pp);
goto retry;
}
}
if (!PP_ISFREE(pp)) {
page_unlock(pp);
return (NULL);
}
if (!page_reclaim(pp, (kmutex_t *)NULL))
return (NULL);
return (pp);
}
/*
* Return the optimum page size for a given mapping
*/
/*ARGSUSED*/
size_t
map_pgsz(int maptype, struct proc *p, caddr_t addr, size_t len, int memcntl)
{
level_t l = 0;
size_t pgsz = MMU_PAGESIZE;
size_t max_lpsize;
uint_t mszc;
ASSERT(maptype != MAPPGSZ_VA);
if (maptype != MAPPGSZ_ISM && physmem < privm_lpg_min_physmem) {
return (MMU_PAGESIZE);
}
switch (maptype) {
case MAPPGSZ_HEAP:
case MAPPGSZ_STK:
max_lpsize = memcntl ? mcntl0_lpsize : (maptype ==
MAPPGSZ_HEAP ? max_uheap_lpsize : max_ustack_lpsize);
if (max_lpsize == MMU_PAGESIZE) {
return (MMU_PAGESIZE);
}
if (len == 0) {
len = (maptype == MAPPGSZ_HEAP) ? p->p_brkbase +
p->p_brksize - p->p_bssbase : p->p_stksize;
}
len = (maptype == MAPPGSZ_HEAP) ? MAX(len,
default_uheap_lpsize) : MAX(len, default_ustack_lpsize);
/*
* use the pages size that best fits len
*/
for (l = mmu.umax_page_level; l > 0; --l) {
if (LEVEL_SIZE(l) > max_lpsize || len < LEVEL_SIZE(l)) {
continue;
} else {
pgsz = LEVEL_SIZE(l);
}
break;
}
mszc = (maptype == MAPPGSZ_HEAP ? p->p_brkpageszc :
p->p_stkpageszc);
if (addr == 0 && (pgsz < hw_page_array[mszc].hp_size)) {
pgsz = hw_page_array[mszc].hp_size;
}
return (pgsz);
case MAPPGSZ_ISM:
for (l = mmu.umax_page_level; l > 0; --l) {
if (len >= LEVEL_SIZE(l))
return (LEVEL_SIZE(l));
}
return (LEVEL_SIZE(0));
}
return (pgsz);
}
static uint_t
map_szcvec(caddr_t addr, size_t size, uintptr_t off, size_t max_lpsize,
size_t min_physmem)
{
caddr_t eaddr = addr + size;
uint_t szcvec = 0;
caddr_t raddr;
caddr_t readdr;
size_t pgsz;
int i;
if (physmem < min_physmem || max_lpsize <= MMU_PAGESIZE) {
return (0);
}
for (i = mmu_exported_page_sizes - 1; i > 0; i--) {
pgsz = page_get_pagesize(i);
if (pgsz > max_lpsize) {
continue;
}
raddr = (caddr_t)P2ROUNDUP((uintptr_t)addr, pgsz);
readdr = (caddr_t)P2ALIGN((uintptr_t)eaddr, pgsz);
if (raddr < addr || raddr >= readdr) {
continue;
}
if (P2PHASE((uintptr_t)addr ^ off, pgsz)) {
continue;
}
/*
* Set szcvec to the remaining page sizes.
*/
szcvec = ((1 << (i + 1)) - 1) & ~1;
break;
}
return (szcvec);
}
/*
* Return a bit vector of large page size codes that
* can be used to map [addr, addr + len) region.
*/
/*ARGSUSED*/
uint_t
map_pgszcvec(caddr_t addr, size_t size, uintptr_t off, int flags, int type,
int memcntl)
{
size_t max_lpsize = mcntl0_lpsize;
if (mmu.max_page_level == 0)
return (0);
if (flags & MAP_TEXT) {
if (!memcntl)
max_lpsize = max_utext_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
shm_lpg_min_physmem));
} else if (flags & MAP_INITDATA) {
if (!memcntl)
max_lpsize = max_uidata_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
privm_lpg_min_physmem));
} else if (type == MAPPGSZC_SHM) {
if (!memcntl)
max_lpsize = max_shm_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
shm_lpg_min_physmem));
} else if (type == MAPPGSZC_HEAP) {
if (!memcntl)
max_lpsize = max_uheap_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
privm_lpg_min_physmem));
} else if (type == MAPPGSZC_STACK) {
if (!memcntl)
max_lpsize = max_ustack_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
privm_lpg_min_physmem));
} else {
if (!memcntl)
max_lpsize = max_privmap_lpsize;
return (map_szcvec(addr, size, off, max_lpsize,
privm_lpg_min_physmem));
}
}
/*
* Handle a pagefault.
*/
faultcode_t
pagefault(
caddr_t addr,
enum fault_type type,
enum seg_rw rw,
int iskernel)
{
struct as *as;
struct hat *hat;
struct proc *p;
kthread_t *t;
faultcode_t res;
caddr_t base;
size_t len;
int err;
int mapped_red;
uintptr_t ea;
ASSERT_STACK_ALIGNED();
if (INVALID_VADDR(addr))
return (FC_NOMAP);
mapped_red = segkp_map_red();
if (iskernel) {
as = &kas;
hat = as->a_hat;
} else {
t = curthread;
p = ttoproc(t);
as = p->p_as;
hat = as->a_hat;
}
/*
* Dispatch pagefault.
*/
res = as_fault(hat, as, addr, 1, type, rw);
/*
* If this isn't a potential unmapped hole in the user's
* UNIX data or stack segments, just return status info.
*/
if (res != FC_NOMAP || iskernel)
goto out;
/*
* Check to see if we happened to faulted on a currently unmapped
* part of the UNIX data or stack segments. If so, create a zfod
* mapping there and then try calling the fault routine again.
*/
base = p->p_brkbase;
len = p->p_brksize;
if (addr < base || addr >= base + len) { /* data seg? */
base = (caddr_t)p->p_usrstack - p->p_stksize;
len = p->p_stksize;
if (addr < base || addr >= p->p_usrstack) { /* stack seg? */
/* not in either UNIX data or stack segments */
res = FC_NOMAP;
goto out;
}
}
/*
* the rest of this function implements a 3.X 4.X 5.X compatibility
* This code is probably not needed anymore
*/
if (p->p_model == DATAMODEL_ILP32) {
/* expand the gap to the page boundaries on each side */
ea = P2ROUNDUP((uintptr_t)base + len, MMU_PAGESIZE);
base = (caddr_t)P2ALIGN((uintptr_t)base, MMU_PAGESIZE);
len = ea - (uintptr_t)base;
as_rangelock(as);
if (as_gap(as, MMU_PAGESIZE, &base, &len, AH_CONTAIN, addr) ==
0) {
err = as_map(as, base, len, segvn_create, zfod_argsp);
as_rangeunlock(as);
if (err) {
res = FC_MAKE_ERR(err);
goto out;
}
} else {
/*
* This page is already mapped by another thread after
* we returned from as_fault() above. We just fall
* through as_fault() below.
*/
as_rangeunlock(as);
}
res = as_fault(hat, as, addr, 1, F_INVAL, rw);
}
out:
if (mapped_red)
segkp_unmap_red();
return (res);
}
void
map_addr(caddr_t *addrp, size_t len, offset_t off, int vacalign, uint_t flags)
{
struct proc *p = curproc;
map_addr_proc(addrp, len, off, vacalign,
map_userlimit(p, p->p_as, flags), curproc, flags);
}
/*ARGSUSED*/
int
map_addr_vacalign_check(caddr_t addr, u_offset_t off)
{
return (0);
}
/*
* The maximum amount a randomized mapping will be slewed. We should perhaps
* arrange things so these tunables can be separate for mmap, mmapobj, and
* ld.so
*/
size_t aslr_max_map_skew = 256 * 1024 * 1024; /* 256MB */
/*
* map_addr_proc() is the routine called when the system is to
* choose an address for the user. We will pick an address
* range which is the highest available below userlimit.
*
* Every mapping will have a redzone of a single page on either side of
* the request. This is done to leave one page unmapped between segments.
* This is not required, but it's useful for the user because if their
* program strays across a segment boundary, it will catch a fault
* immediately making debugging a little easier. Currently the redzone
* is mandatory.
*
* addrp is a value/result parameter.
* On input it is a hint from the user to be used in a completely
* machine dependent fashion. We decide to completely ignore this hint.
* If MAP_ALIGN was specified, addrp contains the minimal alignment, which
* must be some "power of two" multiple of pagesize.
*
* On output it is NULL if no address can be found in the current
* processes address space or else an address that is currently
* not mapped for len bytes with a page of red zone on either side.
*
* vacalign is not needed on x86 (it's for viturally addressed caches)
*/
/*ARGSUSED*/
void
map_addr_proc(
caddr_t *addrp,
size_t len,
offset_t off,
int vacalign,
caddr_t userlimit,
struct proc *p,
uint_t flags)
{
struct as *as = p->p_as;
caddr_t addr;
caddr_t base;
size_t slen;
size_t align_amount;
ASSERT32(userlimit == as->a_userlimit);
base = p->p_brkbase;
#if defined(__amd64)
if (p->p_model == DATAMODEL_NATIVE) {
if (userlimit < as->a_userlimit) {
/*
* This happens when a program wants to map
* something in a range that's accessible to a
* program in a smaller address space. For example,
* a 64-bit program calling mmap32(2) to guarantee
* that the returned address is below 4Gbytes.
*/
ASSERT((uintptr_t)userlimit < ADDRESS_C(0xffffffff));
if (userlimit > base)
slen = userlimit - base;
else {
*addrp = NULL;
return;
}
} else {
/*
* With the stack positioned at a higher address than
* the heap for 64-bit processes, it is necessary to be
* mindful of its location and potential size.
*
* Unallocated space above the top of the stack (that
* is, at a lower address) but still within the bounds
* of the stack limit should be considered unavailable.
*
* As the 64-bit stack guard is mapped in immediately
* adjacent to the stack limit boundary, this prevents
* new mappings from having accidentally dangerous
* proximity to the stack.
*/
slen = p->p_usrstack - base -
((p->p_stk_ctl + PAGEOFFSET) & PAGEMASK);
}
} else
#endif /* defined(__amd64) */
slen = userlimit - base;
/* Make len be a multiple of PAGESIZE */
len = (len + PAGEOFFSET) & PAGEMASK;
/*
* figure out what the alignment should be
*
* XX64 -- is there an ELF_AMD64_MAXPGSZ or is it the same????
*/
if (len <= ELF_386_MAXPGSZ) {
/*
* Align virtual addresses to ensure that ELF shared libraries
* are mapped with the appropriate alignment constraints by
* the run-time linker.
*/
align_amount = ELF_386_MAXPGSZ;
} else {
/*
* For 32-bit processes, only those which have specified
* MAP_ALIGN and an addr will be aligned on a larger page size.
* Not doing so can potentially waste up to 1G of process
* address space.
*/
int lvl = (p->p_model == DATAMODEL_ILP32) ? 1 :
mmu.umax_page_level;
while (lvl && len < LEVEL_SIZE(lvl))
--lvl;
align_amount = LEVEL_SIZE(lvl);
}
if ((flags & MAP_ALIGN) && ((uintptr_t)*addrp > align_amount))
align_amount = (uintptr_t)*addrp;
ASSERT(ISP2(align_amount));
ASSERT(align_amount == 0 || align_amount >= PAGESIZE);
off = off & (align_amount - 1);
/*
* Look for a large enough hole starting below userlimit.
* After finding it, use the upper part.
*/
if (as_gap_aligned(as, len, &base, &slen, AH_HI, NULL, align_amount,
PAGESIZE, off) == 0) {
caddr_t as_addr;
/*
* addr is the highest possible address to use since we have
* a PAGESIZE redzone at the beginning and end.
*/
addr = base + slen - (PAGESIZE + len);
as_addr = addr;
/*
* Round address DOWN to the alignment amount and
* add the offset in.
* If addr is greater than as_addr, len would not be large
* enough to include the redzone, so we must adjust down
* by the alignment amount.
*/
addr = (caddr_t)((uintptr_t)addr & (~(align_amount - 1)));
addr += (uintptr_t)off;
if (addr > as_addr) {
addr -= align_amount;
}
/*
* If randomization is requested, slew the allocation
* backwards, within the same gap, by a random amount.
*/
if (flags & _MAP_RANDOMIZE) {
uint32_t slew;
(void) random_get_pseudo_bytes((uint8_t *)&slew,
sizeof (slew));
slew = slew % MIN(aslr_max_map_skew, (addr - base));
addr -= P2ALIGN(slew, align_amount);
}
ASSERT(addr > base);
ASSERT(addr + len < base + slen);
ASSERT(((uintptr_t)addr & (align_amount - 1)) ==
((uintptr_t)(off)));
*addrp = addr;
} else {
*addrp = NULL; /* no more virtual space */
}
}
int valid_va_range_aligned_wraparound;
/*
* Determine whether [*basep, *basep + *lenp) contains a mappable range of
* addresses at least "minlen" long, where the base of the range is at "off"
* phase from an "align" boundary and there is space for a "redzone"-sized
* redzone on either side of the range. On success, 1 is returned and *basep
* and *lenp are adjusted to describe the acceptable range (including
* the redzone). On failure, 0 is returned.
*/
/*ARGSUSED3*/
int
valid_va_range_aligned(caddr_t *basep, size_t *lenp, size_t minlen, int dir,
size_t align, size_t redzone, size_t off)
{
uintptr_t hi, lo;
size_t tot_len;
ASSERT(align == 0 ? off == 0 : off < align);
ASSERT(ISP2(align));
ASSERT(align == 0 || align >= PAGESIZE);
lo = (uintptr_t)*basep;
hi = lo + *lenp;
tot_len = minlen + 2 * redzone; /* need at least this much space */
/*
* If hi rolled over the top, try cutting back.
*/
if (hi < lo) {
*lenp = 0UL - lo - 1UL;
/* See if this really happens. If so, then we figure out why */
valid_va_range_aligned_wraparound++;
hi = lo + *lenp;
}
if (*lenp < tot_len) {
return (0);
}
#if defined(__amd64)
/*
* Deal with a possible hole in the address range between
* hole_start and hole_end that should never be mapped.
*/
if (lo < hole_start) {
if (hi > hole_start) {
if (hi < hole_end) {
hi = hole_start;
} else {
/* lo < hole_start && hi >= hole_end */
if (dir == AH_LO) {
/*
* prefer lowest range
*/
if (hole_start - lo >= tot_len)
hi = hole_start;
else if (hi - hole_end >= tot_len)
lo = hole_end;
else
return (0);
} else {
/*
* prefer highest range
*/
if (hi - hole_end >= tot_len)
lo = hole_end;
else if (hole_start - lo >= tot_len)
hi = hole_start;
else
return (0);
}
}
}
} else {
/* lo >= hole_start */
if (hi < hole_end)
return (0);
if (lo < hole_end)
lo = hole_end;
}
#endif
if (hi - lo < tot_len)
return (0);
if (align > 1) {
uintptr_t tlo = lo + redzone;
uintptr_t thi = hi - redzone;
tlo = (uintptr_t)P2PHASEUP(tlo, align, off);
if (tlo < lo + redzone) {
return (0);
}
if (thi < tlo || thi - tlo < minlen) {
return (0);
}
}
*basep = (caddr_t)lo;
*lenp = hi - lo;
return (1);
}
/*
* Determine whether [*basep, *basep + *lenp) contains a mappable range of
* addresses at least "minlen" long. On success, 1 is returned and *basep
* and *lenp are adjusted to describe the acceptable range. On failure, 0
* is returned.
*/
int
valid_va_range(caddr_t *basep, size_t *lenp, size_t minlen, int dir)
{
return (valid_va_range_aligned(basep, lenp, minlen, dir, 0, 0, 0));
}
/*
* Default to forbidding the first 64k of address space. This protects most
* reasonably sized structures from dereferences through NULL:
* ((foo_t *)0)->bar
*/
uintptr_t forbidden_null_mapping_sz = 0x10000;
/*
* Determine whether [addr, addr+len] are valid user addresses.
*/
/*ARGSUSED*/
int
valid_usr_range(caddr_t addr, size_t len, uint_t prot, struct as *as,
caddr_t userlimit)
{
caddr_t eaddr = addr + len;
if (eaddr <= addr || addr >= userlimit || eaddr > userlimit)
return (RANGE_BADADDR);
if ((addr <= (caddr_t)forbidden_null_mapping_sz) &&
as->a_proc != NULL &&
secflag_enabled(as->a_proc, PROC_SEC_FORBIDNULLMAP))
return (RANGE_BADADDR);
#if defined(__amd64)
/*
* Check for the VA hole
*/
if (eaddr > (caddr_t)hole_start && addr < (caddr_t)hole_end)
return (RANGE_BADADDR);
#endif
return (RANGE_OKAY);
}
/*
* Return 1 if the page frame is onboard memory, else 0.
*/
int
pf_is_memory(pfn_t pf)
{
if (pfn_is_foreign(pf))
return (0);
return (address_in_memlist(phys_install, pfn_to_pa(pf), 1));
}
/*
* return the memrange containing pfn
*/
int
memrange_num(pfn_t pfn)
{
int n;
for (n = 0; n < nranges - 1; ++n) {
if (pfn >= memranges[n])
break;
}
return (n);
}
/*
* return the mnoderange containing pfn
*/
/*ARGSUSED*/
int
pfn_2_mtype(pfn_t pfn)
{
#if defined(__xpv)
return (0);
#else
int n;
/* Always start from highest pfn and work our way down */
for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
if (pfn >= mnoderanges[n].mnr_pfnlo) {
break;
}
}
return (n);
#endif
}
#if !defined(__xpv)
/*
* is_contigpage_free:
* returns a page list of contiguous pages. It minimally has to return
* minctg pages. Caller determines minctg based on the scatter-gather
* list length.
*
* pfnp is set to the next page frame to search on return.
*/
static page_t *
is_contigpage_free(
pfn_t *pfnp,
pgcnt_t *pgcnt,
pgcnt_t minctg,
uint64_t pfnseg,
int iolock)
{
int i = 0;
pfn_t pfn = *pfnp;
page_t *pp;
page_t *plist = NULL;
/*
* fail if pfn + minctg crosses a segment boundary.
* Adjust for next starting pfn to begin at segment boundary.
*/
if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg)) {
*pfnp = roundup(*pfnp, pfnseg + 1);
return (NULL);
}
do {
retry:
pp = page_numtopp_nolock(pfn + i);
if ((pp == NULL) || IS_DUMP_PAGE(pp) ||
(page_trylock(pp, SE_EXCL) == 0)) {
(*pfnp)++;
break;
}
if (page_pptonum(pp) != pfn + i) {
page_unlock(pp);
goto retry;
}
if (!(PP_ISFREE(pp))) {
page_unlock(pp);
(*pfnp)++;
break;
}
if (!PP_ISAGED(pp)) {
page_list_sub(pp, PG_CACHE_LIST);
page_hashout(pp, (kmutex_t *)NULL);
} else {
page_list_sub(pp, PG_FREE_LIST);
}
if (iolock)
page_io_lock(pp);
page_list_concat(&plist, &pp);
/*
* exit loop when pgcnt satisfied or segment boundary reached.
*/
} while ((++i < *pgcnt) && ((pfn + i) & pfnseg));
*pfnp += i; /* set to next pfn to search */
if (i >= minctg) {
*pgcnt -= i;
return (plist);
}
/*
* failure: minctg not satisfied.
*
* if next request crosses segment boundary, set next pfn
* to search from the segment boundary.
*/
if (((*pfnp + minctg - 1) & pfnseg) < (*pfnp & pfnseg))
*pfnp = roundup(*pfnp, pfnseg + 1);
/* clean up any pages already allocated */
while (plist) {
pp = plist;
page_sub(&plist, pp);
page_list_add(pp, PG_FREE_LIST | PG_LIST_TAIL);
if (iolock)
page_io_unlock(pp);
page_unlock(pp);
}
return (NULL);
}
#endif /* !__xpv */
/*
* verify that pages being returned from allocator have correct DMA attribute
*/
#ifndef DEBUG
#define check_dma(a, b, c) (void)(0)
#else
static void
check_dma(ddi_dma_attr_t *dma_attr, page_t *pp, int cnt)
{
if (dma_attr == NULL)
return;
while (cnt-- > 0) {
if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) <
dma_attr->dma_attr_addr_lo)
panic("PFN (pp=%p) below dma_attr_addr_lo", (void *)pp);
if (pa_to_ma(pfn_to_pa(pp->p_pagenum)) >=
dma_attr->dma_attr_addr_hi)
panic("PFN (pp=%p) above dma_attr_addr_hi", (void *)pp);
pp = pp->p_next;
}
}
#endif
#if !defined(__xpv)
static page_t *
page_get_contigpage(pgcnt_t *pgcnt, ddi_dma_attr_t *mattr, int iolock)
{
pfn_t pfn;
int sgllen;
uint64_t pfnseg;
pgcnt_t minctg;
page_t *pplist = NULL, *plist;
uint64_t lo, hi;
pgcnt_t pfnalign = 0;
static pfn_t startpfn;
static pgcnt_t lastctgcnt;
uintptr_t align;
CONTIG_LOCK();
if (mattr) {
lo = mmu_btop((mattr->dma_attr_addr_lo + MMU_PAGEOFFSET));
hi = mmu_btop(mattr->dma_attr_addr_hi);
if (hi >= physmax)
hi = physmax - 1;
sgllen = mattr->dma_attr_sgllen;
pfnseg = mmu_btop(mattr->dma_attr_seg);
align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
if (align > MMU_PAGESIZE)
pfnalign = mmu_btop(align);
/*
* in order to satisfy the request, must minimally
* acquire minctg contiguous pages
*/
minctg = howmany(*pgcnt, sgllen);
ASSERT(hi >= lo);
/*
* start from where last searched if the minctg >= lastctgcnt
*/
if (minctg < lastctgcnt || startpfn < lo || startpfn > hi)
startpfn = lo;
} else {
hi = physmax - 1;
lo = 0;
sgllen = 1;
pfnseg = mmu.highest_pfn;
minctg = *pgcnt;
if (minctg < lastctgcnt)
startpfn = lo;
}
lastctgcnt = minctg;
ASSERT(pfnseg + 1 >= (uint64_t)minctg);
/* conserve 16m memory - start search above 16m when possible */
if (hi > PFN_16M && startpfn < PFN_16M)
startpfn = PFN_16M;
pfn = startpfn;
if (pfnalign)
pfn = P2ROUNDUP(pfn, pfnalign);
while (pfn + minctg - 1 <= hi) {
plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
if (plist) {
page_list_concat(&pplist, &plist);
sgllen--;
/*
* return when contig pages no longer needed
*/
if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
startpfn = pfn;
CONTIG_UNLOCK();
check_dma(mattr, pplist, *pgcnt);
return (pplist);
}
minctg = howmany(*pgcnt, sgllen);
}
if (pfnalign)
pfn = P2ROUNDUP(pfn, pfnalign);
}
/* cannot find contig pages in specified range */
if (startpfn == lo) {
CONTIG_UNLOCK();
return (NULL);
}
/* did not start with lo previously */
pfn = lo;
if (pfnalign)
pfn = P2ROUNDUP(pfn, pfnalign);
/* allow search to go above startpfn */
while (pfn < startpfn) {
plist = is_contigpage_free(&pfn, pgcnt, minctg, pfnseg, iolock);
if (plist != NULL) {
page_list_concat(&pplist, &plist);
sgllen--;
/*
* return when contig pages no longer needed
*/
if (!*pgcnt || ((*pgcnt <= sgllen) && !pfnalign)) {
startpfn = pfn;
CONTIG_UNLOCK();
check_dma(mattr, pplist, *pgcnt);
return (pplist);
}
minctg = howmany(*pgcnt, sgllen);
}
if (pfnalign)
pfn = P2ROUNDUP(pfn, pfnalign);
}
CONTIG_UNLOCK();
return (NULL);
}
#endif /* !__xpv */
/*
* mnode_range_cnt() calculates the number of memory ranges for mnode and
* memranges[]. Used to determine the size of page lists and mnoderanges.
*/
int
mnode_range_cnt(int mnode)
{
#if defined(__xpv)
ASSERT(mnode == 0);
return (1);
#else /* __xpv */
int mri;
int mnrcnt = 0;
if (mem_node_config[mnode].exists != 0) {
mri = nranges - 1;
/* find the memranges index below contained in mnode range */
while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
mri--;
/*
* increment mnode range counter when memranges or mnode
* boundary is reached.
*/
while (mri >= 0 &&
mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
mnrcnt++;
if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
mri--;
else
break;
}
}
ASSERT(mnrcnt <= MAX_MNODE_MRANGES);
return (mnrcnt);
#endif /* __xpv */
}
static int
mnoderange_cmp(const void *v1, const void *v2)
{
const mnoderange_t *m1 = v1;
const mnoderange_t *m2 = v2;
if (m1->mnr_pfnlo < m2->mnr_pfnlo)
return (-1);
return (m1->mnr_pfnlo > m2->mnr_pfnlo);
}
void
mnode_range_setup(mnoderange_t *mnoderanges)
{
mnoderange_t *mp;
size_t nr_ranges;
size_t mnode;
for (mnode = 0, nr_ranges = 0, mp = mnoderanges;
mnode < max_mem_nodes; mnode++) {
size_t mri = nranges - 1;
if (mem_node_config[mnode].exists == 0)
continue;
while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
mri--;
while (mri >= 0 && mem_node_config[mnode].physmax >=
MEMRANGELO(mri)) {
mp->mnr_pfnlo = MAX(MEMRANGELO(mri),
mem_node_config[mnode].physbase);
mp->mnr_pfnhi = MIN(MEMRANGEHI(mri),
mem_node_config[mnode].physmax);
mp->mnr_mnode = mnode;
mp->mnr_memrange = mri;
mp->mnr_next = -1;
mp->mnr_exists = 1;
mp++;
nr_ranges++;
if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
mri--;
else
break;
}
}
/*
* mnoderangecnt can be larger than nr_ranges when memory DR is
* supposedly supported.
*/
VERIFY3U(nr_ranges, <=, mnoderangecnt);
qsort(mnoderanges, nr_ranges, sizeof (mnoderange_t), mnoderange_cmp);
/*
* If some intrepid soul takes the axe to the memory DR code, we can
* remove ->mnr_next altogether, as we just sorted by ->mnr_pfnlo order.
*
* The VERIFY3U() above can be "==" then too.
*/
for (size_t i = 1; i < nr_ranges; i++)
mnoderanges[i].mnr_next = i - 1;
mtypetop = nr_ranges - 1;
mtype16m = pfn_2_mtype(PFN_16MEG - 1); /* Can be -1 ... */
if (physmax4g)
mtype4g = pfn_2_mtype(0xfffff);
}
#ifndef __xpv
/*
* Update mnoderanges for memory hot-add DR operations.
*/
static void
mnode_range_add(int mnode)
{
int *prev;
int n, mri;
pfn_t start, end;
extern void membar_sync(void);
ASSERT(0 <= mnode && mnode < max_mem_nodes);
ASSERT(mem_node_config[mnode].exists);
start = mem_node_config[mnode].physbase;
end = mem_node_config[mnode].physmax;
ASSERT(start <= end);
mutex_enter(&mnoderange_lock);
#ifdef DEBUG
/* Check whether it interleaves with other memory nodes. */
for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
ASSERT(mnoderanges[n].mnr_exists);
if (mnoderanges[n].mnr_mnode == mnode)
continue;
ASSERT(start > mnoderanges[n].mnr_pfnhi ||
end < mnoderanges[n].mnr_pfnlo);
}
#endif /* DEBUG */
mri = nranges - 1;
while (MEMRANGEHI(mri) < mem_node_config[mnode].physbase)
mri--;
while (mri >= 0 && mem_node_config[mnode].physmax >= MEMRANGELO(mri)) {
/* Check whether mtype already exists. */
for (n = mtypetop; n != -1; n = mnoderanges[n].mnr_next) {
if (mnoderanges[n].mnr_mnode == mnode &&
mnoderanges[n].mnr_memrange == mri) {
mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri),
start);
mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri),
end);
break;
}
}
/* Add a new entry if it doesn't exist yet. */
if (n == -1) {
/* Try to find an unused entry in mnoderanges array. */
for (n = 0; n < mnoderangecnt; n++) {
if (mnoderanges[n].mnr_exists == 0)
break;
}
ASSERT(n < mnoderangecnt);
mnoderanges[n].mnr_pfnlo = MAX(MEMRANGELO(mri), start);
mnoderanges[n].mnr_pfnhi = MIN(MEMRANGEHI(mri), end);
mnoderanges[n].mnr_mnode = mnode;
mnoderanges[n].mnr_memrange = mri;
mnoderanges[n].mnr_exists = 1;
/* Page 0 should always be present. */
for (prev = &mtypetop;
mnoderanges[*prev].mnr_pfnlo > start;
prev = &mnoderanges[*prev].mnr_next) {
ASSERT(mnoderanges[*prev].mnr_next >= 0);
ASSERT(mnoderanges[*prev].mnr_pfnlo > end);
}
mnoderanges[n].mnr_next = *prev;
membar_sync();
*prev = n;
}
if (mem_node_config[mnode].physmax > MEMRANGEHI(mri))
mri--;
else
break;
}
mutex_exit(&mnoderange_lock);
}
/*
* Update mnoderanges for memory hot-removal DR operations.
*/
static void
mnode_range_del(int mnode)
{
_NOTE(ARGUNUSED(mnode));
ASSERT(0 <= mnode && mnode < max_mem_nodes);
/* TODO: support deletion operation. */
ASSERT(0);
}
void
plat_slice_add(pfn_t start, pfn_t end)
{
mem_node_add_slice(start, end);
if (plat_dr_enabled()) {
mnode_range_add(PFN_2_MEM_NODE(start));
}
}
void
plat_slice_del(pfn_t start, pfn_t end)
{
ASSERT(PFN_2_MEM_NODE(start) == PFN_2_MEM_NODE(end));
ASSERT(plat_dr_enabled());
mnode_range_del(PFN_2_MEM_NODE(start));
mem_node_del_slice(start, end);
}
#endif /* __xpv */
/*ARGSUSED*/
int
mtype_init(vnode_t *vp, caddr_t vaddr, uint_t *flags, size_t pgsz)
{
int mtype = mtypetop;
#if !defined(__xpv)
#if defined(__i386)
/*
* set the mtype range
* - kmem requests need to be below 4g if restricted_kmemalloc is set.
* - for non kmem requests, set range to above 4g if memory below 4g
* runs low.
*/
if (restricted_kmemalloc && VN_ISKAS(vp) &&
(caddr_t)(vaddr) >= kernelheap &&
(caddr_t)(vaddr) < ekernelheap) {
ASSERT(physmax4g);
mtype = mtype4g;
if (RESTRICT16M_ALLOC(freemem4g - btop(pgsz),
btop(pgsz), *flags)) {
*flags |= PGI_MT_RANGE16M;
} else {
VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
VM_STAT_COND_ADD((*flags & PG_PANIC),
vmm_vmstats.pgpanicalloc);
*flags |= PGI_MT_RANGE0;
}
return (mtype);
}
#endif /* __i386 */
if (RESTRICT4G_ALLOC) {
VM_STAT_ADD(vmm_vmstats.restrict4gcnt);
/* here only for > 4g systems */
*flags |= PGI_MT_RANGE4G;
} else if (RESTRICT16M_ALLOC(freemem, btop(pgsz), *flags)) {
*flags |= PGI_MT_RANGE16M;
} else {
VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
VM_STAT_COND_ADD((*flags & PG_PANIC), vmm_vmstats.pgpanicalloc);
*flags |= PGI_MT_RANGE0;
}
#endif /* !__xpv */
return (mtype);
}
/* mtype init for page_get_replacement_page */
/*ARGSUSED*/
int
mtype_pgr_init(int *flags, page_t *pp, pgcnt_t pgcnt)
{
int mtype = mtypetop;
#if !defined(__xpv)
if (RESTRICT16M_ALLOC(freemem, pgcnt, *flags)) {
*flags |= PGI_MT_RANGE16M;
} else {
VM_STAT_ADD(vmm_vmstats.unrestrict16mcnt);
*flags |= PGI_MT_RANGE0;
}
#endif
return (mtype);
}
/*
* Determine if the mnode range specified in mtype contains memory belonging
* to memory node mnode. If flags & PGI_MT_RANGE is set then mtype contains
* the range from high pfn to 0, 16m or 4g.
*
* Return first mnode range type index found otherwise return -1 if none found.
*/
int
mtype_func(int mnode, int mtype, uint_t flags)
{
if (flags & PGI_MT_RANGE) {
int mnr_lim = MRI_0;
if (flags & PGI_MT_NEXT) {
mtype = mnoderanges[mtype].mnr_next;
}
if (flags & PGI_MT_RANGE4G)
mnr_lim = MRI_4G; /* exclude 0-4g range */
else if (flags & PGI_MT_RANGE16M)
mnr_lim = MRI_16M; /* exclude 0-16m range */
while (mtype != -1 &&
mnoderanges[mtype].mnr_memrange <= mnr_lim) {
if (mnoderanges[mtype].mnr_mnode == mnode)
return (mtype);
mtype = mnoderanges[mtype].mnr_next;
}
} else if (mnoderanges[mtype].mnr_mnode == mnode) {
return (mtype);
}
return (-1);
}
/*
* Update the page list max counts with the pfn range specified by the
* input parameters.
*/
void
mtype_modify_max(pfn_t startpfn, long cnt)
{
int mtype;
pgcnt_t inc;
spgcnt_t scnt = (spgcnt_t)(cnt);
pgcnt_t acnt = ABS(scnt);
pfn_t endpfn = startpfn + acnt;
pfn_t pfn, lo;
if (!physmax4g)
return;
mtype = mtypetop;
for (pfn = endpfn; pfn > startpfn; ) {
ASSERT(mtype != -1);
lo = mnoderanges[mtype].mnr_pfnlo;
if (pfn > lo) {
if (startpfn >= lo) {
inc = pfn - startpfn;
} else {
inc = pfn - lo;
}
if (mnoderanges[mtype].mnr_memrange != MRI_4G) {
if (scnt > 0)
maxmem4g += inc;
else
maxmem4g -= inc;
}
pfn -= inc;
}
mtype = mnoderanges[mtype].mnr_next;
}
}
int
mtype_2_mrange(int mtype)
{
return (mnoderanges[mtype].mnr_memrange);
}
void
mnodetype_2_pfn(int mnode, int mtype, pfn_t *pfnlo, pfn_t *pfnhi)
{
_NOTE(ARGUNUSED(mnode));
ASSERT(mnoderanges[mtype].mnr_mnode == mnode);
*pfnlo = mnoderanges[mtype].mnr_pfnlo;
*pfnhi = mnoderanges[mtype].mnr_pfnhi;
}
size_t
plcnt_sz(size_t ctrs_sz)
{
#ifdef DEBUG
int szc, colors;
ctrs_sz += mnoderangecnt * sizeof (struct mnr_mts) * mmu_page_sizes;
for (szc = 0; szc < mmu_page_sizes; szc++) {
colors = page_get_pagecolors(szc);
ctrs_sz += mnoderangecnt * sizeof (pgcnt_t) * colors;
}
#endif
return (ctrs_sz);
}
caddr_t
plcnt_init(caddr_t addr)
{
#ifdef DEBUG
int mt, szc, colors;
for (mt = 0; mt < mnoderangecnt; mt++) {
mnoderanges[mt].mnr_mts = (struct mnr_mts *)addr;
addr += (sizeof (struct mnr_mts) * mmu_page_sizes);
for (szc = 0; szc < mmu_page_sizes; szc++) {
colors = page_get_pagecolors(szc);
mnoderanges[mt].mnr_mts[szc].mnr_mts_colors = colors;
mnoderanges[mt].mnr_mts[szc].mnr_mtsc_pgcnt =
(pgcnt_t *)addr;
addr += (sizeof (pgcnt_t) * colors);
}
}
#endif
return (addr);
}
void
plcnt_inc_dec(page_t *pp, int mtype, int szc, long cnt, int flags)
{
_NOTE(ARGUNUSED(pp));
#ifdef DEBUG
int bin = PP_2_BIN(pp);
atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mts_pgcnt, cnt);
atomic_add_long(&mnoderanges[mtype].mnr_mts[szc].mnr_mtsc_pgcnt[bin],
cnt);
#endif
ASSERT(mtype == PP_2_MTYPE(pp));
if (physmax4g && mnoderanges[mtype].mnr_memrange != MRI_4G)
atomic_add_long(&freemem4g, cnt);
if (flags & PG_CACHE_LIST)
atomic_add_long(&mnoderanges[mtype].mnr_mt_clpgcnt, cnt);
else
atomic_add_long(&mnoderanges[mtype].mnr_mt_flpgcnt[szc], cnt);
atomic_add_long(&mnoderanges[mtype].mnr_mt_totcnt, cnt);
}
/*
* Returns the free page count for mnode
*/
int
mnode_pgcnt(int mnode)
{
int mtype = mtypetop;
int flags = PGI_MT_RANGE0;
pgcnt_t pgcnt = 0;
mtype = mtype_func(mnode, mtype, flags);
while (mtype != -1) {
pgcnt += MTYPE_FREEMEM(mtype);
mtype = mtype_func(mnode, mtype, flags | PGI_MT_NEXT);
}
return (pgcnt);
}
/*
* Initialize page coloring variables based on the l2 cache parameters.
* Calculate and return memory needed for page coloring data structures.
*/
size_t
page_coloring_init(uint_t l2_sz, int l2_linesz, int l2_assoc)
{
_NOTE(ARGUNUSED(l2_linesz));
size_t colorsz = 0;
int i;
int colors;
#if defined(__xpv)
/*
* Hypervisor domains currently don't have any concept of NUMA.
* Hence we'll act like there is only 1 memrange.
*/
i = memrange_num(1);
#else /* !__xpv */
/*
* Reduce the memory ranges lists if we don't have large amounts
* of memory. This avoids searching known empty free lists.
* To support memory DR operations, we need to keep memory ranges
* for possible memory hot-add operations.
*/
if (plat_dr_physmax > physmax)
i = memrange_num(plat_dr_physmax);
else
i = memrange_num(physmax);
#if defined(__i386)
if (i > MRI_4G)
restricted_kmemalloc = 0;
#endif
/* physmax greater than 4g */
if (i == MRI_4G)
physmax4g = 1;
#endif /* !__xpv */
memranges += i;
nranges -= i;
ASSERT(mmu_page_sizes <= MMU_PAGE_SIZES);
ASSERT(ISP2(l2_linesz));
ASSERT(l2_sz > MMU_PAGESIZE);
/* l2_assoc is 0 for fully associative l2 cache */
if (l2_assoc)
l2_colors = MAX(1, l2_sz / (l2_assoc * MMU_PAGESIZE));
else
l2_colors = 1;
ASSERT(ISP2(l2_colors));
/* for scalability, configure at least PAGE_COLORS_MIN color bins */
page_colors = MAX(l2_colors, PAGE_COLORS_MIN);
/*
* cpu_page_colors is non-zero when a page color may be spread across
* multiple bins.
*/
if (l2_colors < page_colors)
cpu_page_colors = l2_colors;
ASSERT(ISP2(page_colors));
page_colors_mask = page_colors - 1;
ASSERT(ISP2(CPUSETSIZE()));
page_coloring_shift = lowbit(CPUSETSIZE());
/* initialize number of colors per page size */
for (i = 0; i <= mmu.max_page_level; i++) {
hw_page_array[i].hp_size = LEVEL_SIZE(i);
hw_page_array[i].hp_shift = LEVEL_SHIFT(i);
hw_page_array[i].hp_pgcnt = LEVEL_SIZE(i) >> LEVEL_SHIFT(0);
hw_page_array[i].hp_colors = (page_colors_mask >>
(hw_page_array[i].hp_shift - hw_page_array[0].hp_shift))
+ 1;
colorequivszc[i] = 0;
}
/*
* The value of cpu_page_colors determines if additional color bins
* need to be checked for a particular color in the page_get routines.
*/
if (cpu_page_colors != 0) {
int a = lowbit(page_colors) - lowbit(cpu_page_colors);
ASSERT(a > 0);
ASSERT(a < 16);
for (i = 0; i <= mmu.max_page_level; i++) {
if ((colors = hw_page_array[i].hp_colors) <= 1) {
colorequivszc[i] = 0;
continue;
}
while ((colors >> a) == 0)
a--;
ASSERT(a >= 0);
/* higher 4 bits encodes color equiv mask */
colorequivszc[i] = (a << 4);
}
}
/* factor in colorequiv to check additional 'equivalent' bins. */
if (colorequiv > 1) {
int a = lowbit(colorequiv) - 1;
if (a > 15)
a = 15;
for (i = 0; i <= mmu.max_page_level; i++) {
if ((colors = hw_page_array[i].hp_colors) <= 1) {
continue;
}
while ((colors >> a) == 0)
a--;
if ((a << 4) > colorequivszc[i]) {
colorequivszc[i] = (a << 4);
}
}
}
/* size for mnoderanges */
for (mnoderangecnt = 0, i = 0; i < max_mem_nodes; i++)
mnoderangecnt += mnode_range_cnt(i);
if (plat_dr_support_memory()) {
/*
* Reserve enough space for memory DR operations.
* Two extra mnoderanges for possbile fragmentations,
* one for the 2G boundary and the other for the 4G boundary.
* We don't expect a memory board crossing the 16M boundary
* for memory hot-add operations on x86 platforms.
*/
mnoderangecnt += 2 + max_mem_nodes - lgrp_plat_node_cnt;
}
colorsz = mnoderangecnt * sizeof (mnoderange_t);
/* size for fpc_mutex and cpc_mutex */
colorsz += (2 * max_mem_nodes * sizeof (kmutex_t) * NPC_MUTEX);
/* size of page_freelists */
colorsz += mnoderangecnt * sizeof (page_t ***);
colorsz += mnoderangecnt * mmu_page_sizes * sizeof (page_t **);
for (i = 0; i < mmu_page_sizes; i++) {
colors = page_get_pagecolors(i);
colorsz += mnoderangecnt * colors * sizeof (page_t *);
}
/* size of page_cachelists */
colorsz += mnoderangecnt * sizeof (page_t **);
colorsz += mnoderangecnt * page_colors * sizeof (page_t *);
return (colorsz);
}
/*
* Called once at startup to configure page_coloring data structures and
* does the 1st page_free()/page_freelist_add().
*/
void
page_coloring_setup(caddr_t pcmemaddr)
{
int i;
int j;
int k;
caddr_t addr;
int colors;
/*
* do page coloring setup
*/
addr = pcmemaddr;
mnoderanges = (mnoderange_t *)addr;
addr += (mnoderangecnt * sizeof (mnoderange_t));
mnode_range_setup(mnoderanges);
for (k = 0; k < NPC_MUTEX; k++) {
fpc_mutex[k] = (kmutex_t *)addr;
addr += (max_mem_nodes * sizeof (kmutex_t));
}
for (k = 0; k < NPC_MUTEX; k++) {
cpc_mutex[k] = (kmutex_t *)addr;
addr += (max_mem_nodes * sizeof (kmutex_t));
}
page_freelists = (page_t ****)addr;
addr += (mnoderangecnt * sizeof (page_t ***));
page_cachelists = (page_t ***)addr;
addr += (mnoderangecnt * sizeof (page_t **));
for (i = 0; i < mnoderangecnt; i++) {
page_freelists[i] = (page_t ***)addr;
addr += (mmu_page_sizes * sizeof (page_t **));
for (j = 0; j < mmu_page_sizes; j++) {
colors = page_get_pagecolors(j);
page_freelists[i][j] = (page_t **)addr;
addr += (colors * sizeof (page_t *));
}
page_cachelists[i] = (page_t **)addr;
addr += (page_colors * sizeof (page_t *));
}
}
#if defined(__xpv)
/*
* Give back 10% of the io_pool pages to the free list.
* Don't shrink the pool below some absolute minimum.
*/
static void
page_io_pool_shrink()
{
int retcnt;
page_t *pp, *pp_first, *pp_last, **curpool;
mfn_t mfn;
int bothpools = 0;
mutex_enter(&io_pool_lock);
io_pool_shrink_attempts++; /* should be a kstat? */
retcnt = io_pool_cnt / 10;
if (io_pool_cnt - retcnt < io_pool_cnt_min)
retcnt = io_pool_cnt - io_pool_cnt_min;
if (retcnt <= 0)
goto done;
io_pool_shrinks++; /* should be a kstat? */
curpool = &io_pool_4g;
domore:
/*
* Loop through taking pages from the end of the list
* (highest mfns) till amount to return reached.
*/
for (pp = *curpool; pp && retcnt > 0; ) {
pp_first = pp_last = pp->p_prev;
if (pp_first == *curpool)
break;
retcnt--;
io_pool_cnt--;
page_io_pool_sub(curpool, pp_first, pp_last);
if ((mfn = pfn_to_mfn(pp->p_pagenum)) < start_mfn)
start_mfn = mfn;
page_free(pp_first, 1);
pp = *curpool;
}
if (retcnt != 0 && !bothpools) {
/*
* If not enough found in less constrained pool try the
* more constrained one.
*/
curpool = &io_pool_16m;
bothpools = 1;
goto domore;
}
done:
mutex_exit(&io_pool_lock);
}
#endif /* __xpv */
uint_t
page_create_update_flags_x86(uint_t flags)
{
#if defined(__xpv)
/*
* Check this is an urgent allocation and free pages are depleted.
*/
if (!(flags & PG_WAIT) && freemem < desfree)
page_io_pool_shrink();
#else /* !__xpv */
/*
* page_create_get_something may call this because 4g memory may be
* depleted. Set flags to allow for relocation of base page below
* 4g if necessary.
*/
if (physmax4g)
flags |= (PGI_PGCPSZC0 | PGI_PGCPHIPRI);
#endif /* __xpv */
return (flags);
}
/*ARGSUSED*/
int
bp_color(struct buf *bp)
{
return (0);
}
#if defined(__xpv)
/*
* Take pages out of an io_pool
*/
static void
page_io_pool_sub(page_t **poolp, page_t *pp_first, page_t *pp_last)
{
if (*poolp == pp_first) {
*poolp = pp_last->p_next;
if (*poolp == pp_first)
*poolp = NULL;
}
pp_first->p_prev->p_next = pp_last->p_next;
pp_last->p_next->p_prev = pp_first->p_prev;
pp_first->p_prev = pp_last;
pp_last->p_next = pp_first;
}
/*
* Put a page on the io_pool list. The list is ordered by increasing MFN.
*/
static void
page_io_pool_add(page_t **poolp, page_t *pp)
{
page_t *look;
mfn_t mfn = mfn_list[pp->p_pagenum];
if (*poolp == NULL) {
*poolp = pp;
pp->p_next = pp;
pp->p_prev = pp;
return;
}
/*
* Since we try to take pages from the high end of the pool
* chances are good that the pages to be put on the list will
* go at or near the end of the list. so start at the end and
* work backwards.
*/
look = (*poolp)->p_prev;
while (mfn < mfn_list[look->p_pagenum]) {
look = look->p_prev;
if (look == (*poolp)->p_prev)
break; /* backed all the way to front of list */
}
/* insert after look */
pp->p_prev = look;
pp->p_next = look->p_next;
pp->p_next->p_prev = pp;
look->p_next = pp;
if (mfn < mfn_list[(*poolp)->p_pagenum]) {
/*
* we inserted a new first list element
* adjust pool pointer to newly inserted element
*/
*poolp = pp;
}
}
/*
* Add a page to the io_pool. Setting the force flag will force the page
* into the io_pool no matter what.
*/
static void
add_page_to_pool(page_t *pp, int force)
{
page_t *highest;
page_t *freep = NULL;
mutex_enter(&io_pool_lock);
/*
* Always keep the scarce low memory pages
*/
if (mfn_list[pp->p_pagenum] < PFN_16MEG) {
++io_pool_cnt;
page_io_pool_add(&io_pool_16m, pp);
goto done;
}
if (io_pool_cnt < io_pool_cnt_max || force || io_pool_4g == NULL) {
++io_pool_cnt;
page_io_pool_add(&io_pool_4g, pp);
} else {
highest = io_pool_4g->p_prev;
if (mfn_list[pp->p_pagenum] < mfn_list[highest->p_pagenum]) {
page_io_pool_sub(&io_pool_4g, highest, highest);
page_io_pool_add(&io_pool_4g, pp);
freep = highest;
} else {
freep = pp;
}
}
done:
mutex_exit(&io_pool_lock);
if (freep)
page_free(freep, 1);
}
int contig_pfn_cnt; /* no of pfns in the contig pfn list */
int contig_pfn_max; /* capacity of the contig pfn list */
int next_alloc_pfn; /* next position in list to start a contig search */
int contig_pfnlist_updates; /* pfn list update count */
int contig_pfnlist_builds; /* how many times have we (re)built list */
int contig_pfnlist_buildfailed; /* how many times has list build failed */
int create_contig_pending; /* nonzero means taskq creating contig list */
pfn_t *contig_pfn_list = NULL; /* list of contig pfns in ascending mfn order */
/*
* Function to use in sorting a list of pfns by their underlying mfns.
*/
static int
mfn_compare(const void *pfnp1, const void *pfnp2)
{
mfn_t mfn1 = mfn_list[*(pfn_t *)pfnp1];
mfn_t mfn2 = mfn_list[*(pfn_t *)pfnp2];
if (mfn1 > mfn2)
return (1);
if (mfn1 < mfn2)
return (-1);
return (0);
}
/*
* Compact the contig_pfn_list by tossing all the non-contiguous
* elements from the list.
*/
static void
compact_contig_pfn_list(void)
{
pfn_t pfn, lapfn, prev_lapfn;
mfn_t mfn;
int i, newcnt = 0;
prev_lapfn = 0;
for (i = 0; i < contig_pfn_cnt - 1; i++) {
pfn = contig_pfn_list[i];
lapfn = contig_pfn_list[i + 1];
mfn = mfn_list[pfn];
/*
* See if next pfn is for a contig mfn
*/
if (mfn_list[lapfn] != mfn + 1)
continue;
/*
* pfn and lookahead are both put in list
* unless pfn is the previous lookahead.
*/
if (pfn != prev_lapfn)
contig_pfn_list[newcnt++] = pfn;
contig_pfn_list[newcnt++] = lapfn;
prev_lapfn = lapfn;
}
for (i = newcnt; i < contig_pfn_cnt; i++)
contig_pfn_list[i] = 0;
contig_pfn_cnt = newcnt;
}
/*ARGSUSED*/
static void
call_create_contiglist(void *arg)
{
(void) create_contig_pfnlist(PG_WAIT);
}
/*
* Create list of freelist pfns that have underlying
* contiguous mfns. The list is kept in ascending mfn order.
* returns 1 if list created else 0.
*/
static int
create_contig_pfnlist(uint_t flags)
{
pfn_t pfn;
page_t *pp;
int ret = 1;
mutex_enter(&contig_list_lock);
if (contig_pfn_list != NULL)
goto out;
contig_pfn_max = freemem + (freemem / 10);
contig_pfn_list = kmem_zalloc(contig_pfn_max * sizeof (pfn_t),
(flags & PG_WAIT) ? KM_SLEEP : KM_NOSLEEP);
if (contig_pfn_list == NULL) {
/*
* If we could not create the contig list (because
* we could not sleep for memory). Dispatch a taskq that can
* sleep to get the memory.
*/
if (!create_contig_pending) {
if (taskq_dispatch(system_taskq, call_create_contiglist,
NULL, TQ_NOSLEEP) != TASKQID_INVALID)
create_contig_pending = 1;
}
contig_pfnlist_buildfailed++; /* count list build failures */
ret = 0;
goto out;
}
create_contig_pending = 0;
ASSERT(contig_pfn_cnt == 0);
for (pfn = 0; pfn < mfn_count; pfn++) {
pp = page_numtopp_nolock(pfn);
if (pp == NULL || !PP_ISFREE(pp))
continue;
contig_pfn_list[contig_pfn_cnt] = pfn;
if (++contig_pfn_cnt == contig_pfn_max)
break;
}
/*
* Sanity check the new list.
*/
if (contig_pfn_cnt < 2) { /* no contig pfns */
contig_pfn_cnt = 0;
contig_pfnlist_buildfailed++;
kmem_free(contig_pfn_list, contig_pfn_max * sizeof (pfn_t));
contig_pfn_list = NULL;
contig_pfn_max = 0;
ret = 0;
goto out;
}
qsort(contig_pfn_list, contig_pfn_cnt, sizeof (pfn_t), mfn_compare);
compact_contig_pfn_list();
/*
* Make sure next search of the newly created contiguous pfn
* list starts at the beginning of the list.
*/
next_alloc_pfn = 0;
contig_pfnlist_builds++; /* count list builds */
out:
mutex_exit(&contig_list_lock);
return (ret);
}
/*
* Toss the current contig pfnlist. Someone is about to do a massive
* update to pfn<->mfn mappings. So we have them destroy the list and lock
* it till they are done with their update.
*/
void
clear_and_lock_contig_pfnlist()
{
pfn_t *listp = NULL;
size_t listsize;
mutex_enter(&contig_list_lock);
if (contig_pfn_list != NULL) {
listp = contig_pfn_list;
listsize = contig_pfn_max * sizeof (pfn_t);
contig_pfn_list = NULL;
contig_pfn_max = contig_pfn_cnt = 0;
}
if (listp != NULL)
kmem_free(listp, listsize);
}
/*
* Unlock the contig_pfn_list. The next attempted use of it will cause
* it to be re-created.
*/
void
unlock_contig_pfnlist()
{
mutex_exit(&contig_list_lock);
}
/*
* Update the contiguous pfn list in response to a pfn <-> mfn reassignment
*/
void
update_contig_pfnlist(pfn_t pfn, mfn_t oldmfn, mfn_t newmfn)
{
int probe_hi, probe_lo, probe_pos, insert_after, insert_point;
pfn_t probe_pfn;
mfn_t probe_mfn;
int drop_lock = 0;
if (mutex_owner(&contig_list_lock) != curthread) {
drop_lock = 1;
mutex_enter(&contig_list_lock);
}
if (contig_pfn_list == NULL)
goto done;
contig_pfnlist_updates++;
/*
* Find the pfn in the current list. Use a binary chop to locate it.
*/
probe_hi = contig_pfn_cnt - 1;
probe_lo = 0;
probe_pos = (probe_hi + probe_lo) / 2;
while ((probe_pfn = contig_pfn_list[probe_pos]) != pfn) {
if (probe_pos == probe_lo) { /* pfn not in list */
probe_pos = -1;
break;
}
if (pfn_to_mfn(probe_pfn) <= oldmfn)
probe_lo = probe_pos;
else
probe_hi = probe_pos;
probe_pos = (probe_hi + probe_lo) / 2;
}
if (probe_pos >= 0) {
/*
* Remove pfn from list and ensure next alloc
* position stays in bounds.
*/
if (--contig_pfn_cnt <= next_alloc_pfn)
next_alloc_pfn = 0;
if (contig_pfn_cnt < 2) { /* no contig pfns */
contig_pfn_cnt = 0;
kmem_free(contig_pfn_list,
contig_pfn_max * sizeof (pfn_t));
contig_pfn_list = NULL;
contig_pfn_max = 0;
goto done;
}
ovbcopy(&contig_pfn_list[probe_pos + 1],
&contig_pfn_list[probe_pos],
(contig_pfn_cnt - probe_pos) * sizeof (pfn_t));
}
if (newmfn == MFN_INVALID)
goto done;
/*
* Check if new mfn has adjacent mfns in the list
*/
probe_hi = contig_pfn_cnt - 1;
probe_lo = 0;
insert_after = -2;
do {
probe_pos = (probe_hi + probe_lo) / 2;
probe_mfn = pfn_to_mfn(contig_pfn_list[probe_pos]);
if (newmfn == probe_mfn + 1)
insert_after = probe_pos;
else if (newmfn == probe_mfn - 1)
insert_after = probe_pos - 1;
if (probe_pos == probe_lo)
break;
if (probe_mfn <= newmfn)
probe_lo = probe_pos;
else
probe_hi = probe_pos;
} while (insert_after == -2);
/*
* If there is space in the list and there are adjacent mfns
* insert the pfn in to its proper place in the list.
*/
if (insert_after != -2 && contig_pfn_cnt + 1 <= contig_pfn_max) {
insert_point = insert_after + 1;
ovbcopy(&contig_pfn_list[insert_point],
&contig_pfn_list[insert_point + 1],
(contig_pfn_cnt - insert_point) * sizeof (pfn_t));
contig_pfn_list[insert_point] = pfn;
contig_pfn_cnt++;
}
done:
if (drop_lock)
mutex_exit(&contig_list_lock);
}
/*
* Called to (re-)populate the io_pool from the free page lists.
*/
long
populate_io_pool(void)
{
pfn_t pfn;
mfn_t mfn, max_mfn;
page_t *pp;
/*
* Figure out the bounds of the pool on first invocation.
* We use a percentage of memory for the io pool size.
* we allow that to shrink, but not to less than a fixed minimum
*/
if (io_pool_cnt_max == 0) {
io_pool_cnt_max = physmem / (100 / io_pool_physmem_pct);
io_pool_cnt_lowater = io_pool_cnt_max;
/*
* This is the first time in populate_io_pool, grab a va to use
* when we need to allocate pages.
*/
io_pool_kva = vmem_alloc(heap_arena, PAGESIZE, VM_SLEEP);
}
/*
* If we are out of pages in the pool, then grow the size of the pool
*/
if (io_pool_cnt == 0) {
/*
* Grow the max size of the io pool by 5%, but never more than
* 25% of physical memory.
*/
if (io_pool_cnt_max < physmem / 4)
io_pool_cnt_max += io_pool_cnt_max / 20;
}
io_pool_grows++; /* should be a kstat? */
/*
* Get highest mfn on this platform, but limit to the 32 bit DMA max.
*/
(void) mfn_to_pfn(start_mfn);
max_mfn = MIN(cached_max_mfn, PFN_4GIG);
for (mfn = start_mfn; mfn < max_mfn; start_mfn = ++mfn) {
pfn = mfn_to_pfn(mfn);
if (pfn & PFN_IS_FOREIGN_MFN)
continue;
/*
* try to allocate it from free pages
*/
pp = page_numtopp_alloc(pfn);
if (pp == NULL)
continue;
PP_CLRFREE(pp);
add_page_to_pool(pp, 1);
if (io_pool_cnt >= io_pool_cnt_max)
break;
}
return (io_pool_cnt);
}
/*
* Destroy a page that was being used for DMA I/O. It may or
* may not actually go back to the io_pool.
*/
void
page_destroy_io(page_t *pp)
{
mfn_t mfn = mfn_list[pp->p_pagenum];
/*
* When the page was alloc'd a reservation was made, release it now
*/
page_unresv(1);
/*
* Unload translations, if any, then hash out the
* page to erase its identity.
*/
(void) hat_pageunload(pp, HAT_FORCE_PGUNLOAD);
page_hashout(pp, NULL);
/*
* If the page came from the free lists, just put it back to them.
* DomU pages always go on the free lists as well.
*/
if (!DOMAIN_IS_INITDOMAIN(xen_info) || mfn >= PFN_4GIG) {
page_free(pp, 1);
return;
}
add_page_to_pool(pp, 0);
}
long contig_searches; /* count of times contig pages requested */
long contig_search_restarts; /* count of contig ranges tried */
long contig_search_failed; /* count of contig alloc failures */
/*
* Free partial page list
*/
static void
free_partial_list(page_t **pplist)
{
page_t *pp;
while (*pplist != NULL) {
pp = *pplist;
page_io_pool_sub(pplist, pp, pp);
page_free(pp, 1);
}
}
/*
* Look thru the contiguous pfns that are not part of the io_pool for
* contiguous free pages. Return a list of the found pages or NULL.
*/
page_t *
find_contig_free(uint_t npages, uint_t flags, uint64_t pfnseg,
pgcnt_t pfnalign)
{
page_t *pp, *plist = NULL;
mfn_t mfn, prev_mfn, start_mfn;
pfn_t pfn;
int pages_needed, pages_requested;
int search_start;
/*
* create the contig pfn list if not already done
*/
retry:
mutex_enter(&contig_list_lock);
if (contig_pfn_list == NULL) {
mutex_exit(&contig_list_lock);
if (!create_contig_pfnlist(flags)) {
return (NULL);
}
goto retry;
}
contig_searches++;
/*
* Search contiguous pfn list for physically contiguous pages not in
* the io_pool. Start the search where the last search left off.
*/
pages_requested = pages_needed = npages;
search_start = next_alloc_pfn;
start_mfn = prev_mfn = 0;
while (pages_needed) {
pfn = contig_pfn_list[next_alloc_pfn];
mfn = pfn_to_mfn(pfn);
/*
* Check if mfn is first one or contig to previous one and
* if page corresponding to mfn is free and that mfn
* range is not crossing a segment boundary.
*/
if ((prev_mfn == 0 || mfn == prev_mfn + 1) &&
(pp = page_numtopp_alloc(pfn)) != NULL &&
!((mfn & pfnseg) < (start_mfn & pfnseg))) {
PP_CLRFREE(pp);
page_io_pool_add(&plist, pp);
pages_needed--;
if (prev_mfn == 0) {
if (pfnalign &&
mfn != P2ROUNDUP(mfn, pfnalign)) {
/*
* not properly aligned
*/
contig_search_restarts++;
free_partial_list(&plist);
pages_needed = pages_requested;
start_mfn = prev_mfn = 0;
goto skip;
}
start_mfn = mfn;
}
prev_mfn = mfn;
} else {
contig_search_restarts++;
free_partial_list(&plist);
pages_needed = pages_requested;
start_mfn = prev_mfn = 0;
}
skip:
if (++next_alloc_pfn == contig_pfn_cnt)
next_alloc_pfn = 0;
if (next_alloc_pfn == search_start)
break; /* all pfns searched */
}
mutex_exit(&contig_list_lock);
if (pages_needed) {
contig_search_failed++;
/*
* Failed to find enough contig pages.
* free partial page list
*/
free_partial_list(&plist);
}
return (plist);
}
/*
* Search the reserved io pool pages for a page range with the
* desired characteristics.
*/
page_t *
page_io_pool_alloc(ddi_dma_attr_t *mattr, int contig, pgcnt_t minctg)
{
page_t *pp_first, *pp_last;
page_t *pp, **poolp;
pgcnt_t nwanted, pfnalign;
uint64_t pfnseg;
mfn_t mfn, tmfn, hi_mfn, lo_mfn;
int align, attempt = 0;
if (minctg == 1)
contig = 0;
lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
pfnseg = mmu_btop(mattr->dma_attr_seg);
align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
if (align > MMU_PAGESIZE)
pfnalign = mmu_btop(align);
else
pfnalign = 0;
try_again:
/*
* See if we want pages for a legacy device
*/
if (hi_mfn < PFN_16MEG)
poolp = &io_pool_16m;
else
poolp = &io_pool_4g;
try_smaller:
/*
* Take pages from I/O pool. We'll use pages from the highest
* MFN range possible.
*/
pp_first = pp_last = NULL;
mutex_enter(&io_pool_lock);
nwanted = minctg;
for (pp = *poolp; pp && nwanted > 0; ) {
pp = pp->p_prev;
/*
* skip pages above allowable range
*/
mfn = mfn_list[pp->p_pagenum];
if (hi_mfn < mfn)
goto skip;
/*
* stop at pages below allowable range
*/
if (lo_mfn > mfn)
break;
restart:
if (pp_last == NULL) {
/*
* Check alignment
*/
tmfn = mfn - (minctg - 1);
if (pfnalign && tmfn != P2ROUNDUP(tmfn, pfnalign))
goto skip; /* not properly aligned */
/*
* Check segment
*/
if ((mfn & pfnseg) < (tmfn & pfnseg))
goto skip; /* crosses seg boundary */
/*
* Start building page list
*/
pp_first = pp_last = pp;
nwanted--;
} else {
/*
* check physical contiguity if required
*/
if (contig &&
mfn_list[pp_first->p_pagenum] != mfn + 1) {
/*
* not a contiguous page, restart list.
*/
pp_last = NULL;
nwanted = minctg;
goto restart;
} else { /* add page to list */
pp_first = pp;
nwanted--;
}
}
skip:
if (pp == *poolp)
break;
}
/*
* If we didn't find memory. Try the more constrained pool, then
* sweep free pages into the DMA pool and try again.
*/
if (nwanted != 0) {
mutex_exit(&io_pool_lock);
/*
* If we were looking in the less constrained pool and
* didn't find pages, try the more constrained pool.
*/
if (poolp == &io_pool_4g) {
poolp = &io_pool_16m;
goto try_smaller;
}
kmem_reap();
if (++attempt < 4) {
/*
* Grab some more io_pool pages
*/
(void) populate_io_pool();
goto try_again; /* go around and retry */
}
return (NULL);
}
/*
* Found the pages, now snip them from the list
*/
page_io_pool_sub(poolp, pp_first, pp_last);
io_pool_cnt -= minctg;
/*
* reset low water mark
*/
if (io_pool_cnt < io_pool_cnt_lowater)
io_pool_cnt_lowater = io_pool_cnt;
mutex_exit(&io_pool_lock);
return (pp_first);
}
page_t *
page_swap_with_hypervisor(struct vnode *vp, u_offset_t off, caddr_t vaddr,
ddi_dma_attr_t *mattr, uint_t flags, pgcnt_t minctg)
{
uint_t kflags;
int order, extra, extpages, i, contig, nbits, extents;
page_t *pp, *expp, *pp_first, **pplist = NULL;
mfn_t *mfnlist = NULL;
extra = 0;
contig = flags & PG_PHYSCONTIG;
if (minctg == 1)
contig = 0;
flags &= ~PG_PHYSCONTIG;
kflags = flags & PG_WAIT ? KM_SLEEP : KM_NOSLEEP;
/*
* Hypervisor will allocate extents, if we want contig
* pages extent must be >= minctg
*/
if (contig) {
order = highbit(minctg) - 1;
if (minctg & ((1 << order) - 1))
order++;
extpages = 1 << order;
} else {
order = 0;
extpages = minctg;
}
if (extpages > minctg) {
extra = extpages - minctg;
if (!page_resv(extra, kflags))
return (NULL);
}
pp_first = NULL;
pplist = kmem_alloc(extpages * sizeof (page_t *), kflags);
if (pplist == NULL)
goto balloon_fail;
mfnlist = kmem_alloc(extpages * sizeof (mfn_t), kflags);
if (mfnlist == NULL)
goto balloon_fail;
pp = page_create_va(vp, off, minctg * PAGESIZE, flags, &kvseg, vaddr);
if (pp == NULL)
goto balloon_fail;
pp_first = pp;
if (extpages > minctg) {
/*
* fill out the rest of extent pages to swap
* with the hypervisor
*/
for (i = 0; i < extra; i++) {
expp = page_create_va(vp,
(u_offset_t)(uintptr_t)io_pool_kva,
PAGESIZE, flags, &kvseg, io_pool_kva);
if (expp == NULL)
goto balloon_fail;
(void) hat_pageunload(expp, HAT_FORCE_PGUNLOAD);
page_io_unlock(expp);
page_hashout(expp, NULL);
page_io_lock(expp);
/*
* add page to end of list
*/
expp->p_prev = pp_first->p_prev;
expp->p_next = pp_first;
expp->p_prev->p_next = expp;
pp_first->p_prev = expp;
}
}
for (i = 0; i < extpages; i++) {
pplist[i] = pp;
pp = pp->p_next;
}
nbits = highbit(mattr->dma_attr_addr_hi);
extents = contig ? 1 : minctg;
if (balloon_replace_pages(extents, pplist, nbits, order,
mfnlist) != extents) {
if (ioalloc_dbg)
cmn_err(CE_NOTE, "request to hypervisor"
" for %d pages, maxaddr %" PRIx64 " failed",
extpages, mattr->dma_attr_addr_hi);
goto balloon_fail;
}
kmem_free(pplist, extpages * sizeof (page_t *));
kmem_free(mfnlist, extpages * sizeof (mfn_t));
/*
* Return any excess pages to free list
*/
if (extpages > minctg) {
for (i = 0; i < extra; i++) {
pp = pp_first->p_prev;
page_sub(&pp_first, pp);
page_io_unlock(pp);
page_unresv(1);
page_free(pp, 1);
}
}
return (pp_first);
balloon_fail:
/*
* Return pages to free list and return failure
*/
while (pp_first != NULL) {
pp = pp_first;
page_sub(&pp_first, pp);
page_io_unlock(pp);
if (pp->p_vnode != NULL)
page_hashout(pp, NULL);
page_free(pp, 1);
}
if (pplist)
kmem_free(pplist, extpages * sizeof (page_t *));
if (mfnlist)
kmem_free(mfnlist, extpages * sizeof (mfn_t));
page_unresv(extpages - minctg);
return (NULL);
}
static void
return_partial_alloc(page_t *plist)
{
page_t *pp;
while (plist != NULL) {
pp = plist;
page_sub(&plist, pp);
page_io_unlock(pp);
page_destroy_io(pp);
}
}
static page_t *
page_get_contigpages(
struct vnode *vp,
u_offset_t off,
int *npagesp,
uint_t flags,
caddr_t vaddr,
ddi_dma_attr_t *mattr)
{
mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
page_t *plist; /* list to return */
page_t *pp, *mcpl;
int contig, anyaddr, npages, getone = 0;
mfn_t lo_mfn;
mfn_t hi_mfn;
pgcnt_t pfnalign = 0;
int align, sgllen;
uint64_t pfnseg;
pgcnt_t minctg;
npages = *npagesp;
ASSERT(mattr != NULL);
lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
sgllen = mattr->dma_attr_sgllen;
pfnseg = mmu_btop(mattr->dma_attr_seg);
align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
if (align > MMU_PAGESIZE)
pfnalign = mmu_btop(align);
contig = flags & PG_PHYSCONTIG;
if (npages == -1) {
npages = 1;
pfnalign = 0;
}
/*
* Clear the contig flag if only one page is needed.
*/
if (npages == 1) {
getone = 1;
contig = 0;
}
/*
* Check if any page in the system is fine.
*/
anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn;
if (!contig && anyaddr && !pfnalign) {
flags &= ~PG_PHYSCONTIG;
plist = page_create_va(vp, off, npages * MMU_PAGESIZE,
flags, &kvseg, vaddr);
if (plist != NULL) {
*npagesp = 0;
return (plist);
}
}
plist = NULL;
minctg = howmany(npages, sgllen);
while (npages > sgllen || getone) {
if (minctg > npages)
minctg = npages;
mcpl = NULL;
/*
* We could want contig pages with no address range limits.
*/
if (anyaddr && contig) {
/*
* Look for free contig pages to satisfy the request.
*/
mcpl = find_contig_free(minctg, flags, pfnseg,
pfnalign);
}
/*
* Try the reserved io pools next
*/
if (mcpl == NULL)
mcpl = page_io_pool_alloc(mattr, contig, minctg);
if (mcpl != NULL) {
pp = mcpl;
do {
if (!page_hashin(pp, vp, off, NULL)) {
panic("page_get_contigpages:"
" hashin failed"
" pp %p, vp %p, off %llx",
(void *)pp, (void *)vp, off);
}
off += MMU_PAGESIZE;
PP_CLRFREE(pp);
PP_CLRAGED(pp);
page_set_props(pp, P_REF);
page_io_lock(pp);
pp = pp->p_next;
} while (pp != mcpl);
} else {
/*
* Hypervisor exchange doesn't handle segment or
* alignment constraints
*/
if (mattr->dma_attr_seg < mattr->dma_attr_addr_hi ||
pfnalign)
goto fail;
/*
* Try exchanging pages with the hypervisor
*/
mcpl = page_swap_with_hypervisor(vp, off, vaddr, mattr,
flags, minctg);
if (mcpl == NULL)
goto fail;
off += minctg * MMU_PAGESIZE;
}
check_dma(mattr, mcpl, minctg);
/*
* Here with a minctg run of contiguous pages, add them to the
* list we will return for this request.
*/
page_list_concat(&plist, &mcpl);
npages -= minctg;
*npagesp = npages;
sgllen--;
if (getone)
break;
}
return (plist);
fail:
return_partial_alloc(plist);
return (NULL);
}
/*
* Allocator for domain 0 I/O pages. We match the required
* DMA attributes and contiguity constraints.
*/
/*ARGSUSED*/
page_t *
page_create_io(
struct vnode *vp,
u_offset_t off,
uint_t bytes,
uint_t flags,
struct as *as,
caddr_t vaddr,
ddi_dma_attr_t *mattr)
{
page_t *plist = NULL, *pp;
int npages = 0, contig, anyaddr, pages_req;
mfn_t lo_mfn;
mfn_t hi_mfn;
pgcnt_t pfnalign = 0;
int align;
int is_domu = 0;
int dummy, bytes_got;
mfn_t max_mfn = HYPERVISOR_memory_op(XENMEM_maximum_ram_page, NULL);
ASSERT(mattr != NULL);
lo_mfn = mmu_btop(mattr->dma_attr_addr_lo);
hi_mfn = mmu_btop(mattr->dma_attr_addr_hi);
align = maxbit(mattr->dma_attr_align, mattr->dma_attr_minxfer);
if (align > MMU_PAGESIZE)
pfnalign = mmu_btop(align);
/*
* Clear the contig flag if only one page is needed or the scatter
* gather list length is >= npages.
*/
pages_req = npages = mmu_btopr(bytes);
contig = (flags & PG_PHYSCONTIG);
bytes = P2ROUNDUP(bytes, MMU_PAGESIZE);
if (bytes == MMU_PAGESIZE || mattr->dma_attr_sgllen >= npages)
contig = 0;
/*
* Check if any old page in the system is fine.
* DomU should always go down this path.
*/
is_domu = !DOMAIN_IS_INITDOMAIN(xen_info);
anyaddr = lo_mfn == 0 && hi_mfn >= max_mfn && !pfnalign;
if ((!contig && anyaddr) || is_domu) {
flags &= ~PG_PHYSCONTIG;
plist = page_create_va(vp, off, bytes, flags, &kvseg, vaddr);
if (plist != NULL)
return (plist);
else if (is_domu)
return (NULL); /* no memory available */
}
/*
* DomU should never reach here
*/
if (contig) {
plist = page_get_contigpages(vp, off, &npages, flags, vaddr,
mattr);
if (plist == NULL)
goto fail;
bytes_got = (pages_req - npages) << MMU_PAGESHIFT;
vaddr += bytes_got;
off += bytes_got;
/*
* We now have all the contiguous pages we need, but
* we may still need additional non-contiguous pages.
*/
}
/*
* now loop collecting the requested number of pages, these do
* not have to be contiguous pages but we will use the contig
* page alloc code to get the pages since it will honor any
* other constraints the pages may have.
*/
while (npages--) {
dummy = -1;
pp = page_get_contigpages(vp, off, &dummy, flags, vaddr, mattr);
if (pp == NULL)
goto fail;
page_add(&plist, pp);
vaddr += MMU_PAGESIZE;
off += MMU_PAGESIZE;
}
return (plist);
fail:
/*
* Failed to get enough pages, return ones we did get
*/
return_partial_alloc(plist);
return (NULL);
}
/*
* Lock and return the page with the highest mfn that we can find. last_mfn
* holds the last one found, so the next search can start from there. We
* also keep a counter so that we don't loop forever if the machine has no
* free pages.
*
* This is called from the balloon thread to find pages to give away. new_high
* is used when new mfn's have been added to the system - we will reset our
* search if the new mfn's are higher than our current search position.
*/
page_t *
page_get_high_mfn(mfn_t new_high)
{
static mfn_t last_mfn = 0;
pfn_t pfn;
page_t *pp;
ulong_t loop_count = 0;
if (new_high > last_mfn)
last_mfn = new_high;
for (; loop_count < mfn_count; loop_count++, last_mfn--) {
if (last_mfn == 0) {
last_mfn = cached_max_mfn;
}
pfn = mfn_to_pfn(last_mfn);
if (pfn & PFN_IS_FOREIGN_MFN)
continue;
/* See if the page is free. If so, lock it. */
pp = page_numtopp_alloc(pfn);
if (pp == NULL)
continue;
PP_CLRFREE(pp);
ASSERT(PAGE_EXCL(pp));
ASSERT(pp->p_vnode == NULL);
ASSERT(!hat_page_is_mapped(pp));
last_mfn--;
return (pp);
}
return (NULL);
}
#else /* !__xpv */
/*
* get a page from any list with the given mnode
*/
static page_t *
page_get_mnode_anylist(ulong_t origbin, uchar_t szc, uint_t flags,
int mnode, int mtype, ddi_dma_attr_t *dma_attr)
{
kmutex_t *pcm;
int i;
page_t *pp;
page_t *first_pp;
uint64_t pgaddr;
ulong_t bin;
int mtypestart;
int plw_initialized;
page_list_walker_t plw;
VM_STAT_ADD(pga_vmstats.pgma_alloc);
ASSERT((flags & PG_MATCH_COLOR) == 0);
ASSERT(szc == 0);
ASSERT(dma_attr != NULL);
MTYPE_START(mnode, mtype, flags);
if (mtype < 0) {
VM_STAT_ADD(pga_vmstats.pgma_allocempty);
return (NULL);
}
mtypestart = mtype;
bin = origbin;
/*
* check up to page_colors + 1 bins - origbin may be checked twice
* because of BIN_STEP skip
*/
do {
plw_initialized = 0;
for (plw.plw_count = 0;
plw.plw_count < page_colors; plw.plw_count++) {
if (PAGE_FREELISTS(mnode, szc, bin, mtype) == NULL)
goto nextfreebin;
pcm = PC_BIN_MUTEX(mnode, bin, PG_FREE_LIST);
mutex_enter(pcm);
pp = PAGE_FREELISTS(mnode, szc, bin, mtype);
first_pp = pp;
while (pp != NULL) {
if (IS_DUMP_PAGE(pp) || page_trylock(pp,
SE_EXCL) == 0) {
pp = pp->p_next;
if (pp == first_pp) {
pp = NULL;
}
continue;
}
ASSERT(PP_ISFREE(pp));
ASSERT(PP_ISAGED(pp));
ASSERT(pp->p_vnode == NULL);
ASSERT(pp->p_hash == NULL);
ASSERT(pp->p_offset == (u_offset_t)-1);
ASSERT(pp->p_szc == szc);
ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
/* check if page within DMA attributes */
pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
(pgaddr + MMU_PAGESIZE - 1 <=
dma_attr->dma_attr_addr_hi)) {
break;
}
/* continue looking */
page_unlock(pp);
pp = pp->p_next;
if (pp == first_pp)
pp = NULL;
}
if (pp != NULL) {
ASSERT(mtype == PP_2_MTYPE(pp));
ASSERT(pp->p_szc == 0);
/* found a page with specified DMA attributes */
page_sub(&PAGE_FREELISTS(mnode, szc, bin,
mtype), pp);
page_ctr_sub(mnode, mtype, pp, PG_FREE_LIST);
if ((PP_ISFREE(pp) == 0) ||
(PP_ISAGED(pp) == 0)) {
cmn_err(CE_PANIC, "page %p is not free",
(void *)pp);
}
mutex_exit(pcm);
check_dma(dma_attr, pp, 1);
VM_STAT_ADD(pga_vmstats.pgma_allocok);
return (pp);
}
mutex_exit(pcm);
nextfreebin:
if (plw_initialized == 0) {
page_list_walk_init(szc, 0, bin, 1, 0, &plw);
ASSERT(plw.plw_ceq_dif == page_colors);
plw_initialized = 1;
}
if (plw.plw_do_split) {
pp = page_freelist_split(szc, bin, mnode,
mtype,
mmu_btop(dma_attr->dma_attr_addr_lo),
mmu_btop(dma_attr->dma_attr_addr_hi + 1),
&plw);
if (pp != NULL) {
check_dma(dma_attr, pp, 1);
return (pp);
}
}
bin = page_list_walk_next_bin(szc, bin, &plw);
}
MTYPE_NEXT(mnode, mtype, flags);
} while (mtype >= 0);
/* failed to find a page in the freelist; try it in the cachelist */
/* reset mtype start for cachelist search */
mtype = mtypestart;
ASSERT(mtype >= 0);
/* start with the bin of matching color */
bin = origbin;
do {
for (i = 0; i <= page_colors; i++) {
if (PAGE_CACHELISTS(mnode, bin, mtype) == NULL)
goto nextcachebin;
pcm = PC_BIN_MUTEX(mnode, bin, PG_CACHE_LIST);
mutex_enter(pcm);
pp = PAGE_CACHELISTS(mnode, bin, mtype);
first_pp = pp;
while (pp != NULL) {
if (IS_DUMP_PAGE(pp) || page_trylock(pp,
SE_EXCL) == 0) {
pp = pp->p_next;
if (pp == first_pp)
pp = NULL;
continue;
}
ASSERT(pp->p_vnode);
ASSERT(PP_ISAGED(pp) == 0);
ASSERT(pp->p_szc == 0);
ASSERT(PFN_2_MEM_NODE(pp->p_pagenum) == mnode);
/* check if page within DMA attributes */
pgaddr = pa_to_ma(pfn_to_pa(pp->p_pagenum));
if ((pgaddr >= dma_attr->dma_attr_addr_lo) &&
(pgaddr + MMU_PAGESIZE - 1 <=
dma_attr->dma_attr_addr_hi)) {
break;
}
/* continue looking */
page_unlock(pp);
pp = pp->p_next;
if (pp == first_pp)
pp = NULL;
}
if (pp != NULL) {
ASSERT(mtype == PP_2_MTYPE(pp));
ASSERT(pp->p_szc == 0);
/* found a page with specified DMA attributes */
page_sub(&PAGE_CACHELISTS(mnode, bin,
mtype), pp);
page_ctr_sub(mnode, mtype, pp, PG_CACHE_LIST);
mutex_exit(pcm);
ASSERT(pp->p_vnode);
ASSERT(PP_ISAGED(pp) == 0);
check_dma(dma_attr, pp, 1);
VM_STAT_ADD(pga_vmstats.pgma_allocok);
return (pp);
}
mutex_exit(pcm);
nextcachebin:
bin += (i == 0) ? BIN_STEP : 1;
bin &= page_colors_mask;
}
MTYPE_NEXT(mnode, mtype, flags);
} while (mtype >= 0);
VM_STAT_ADD(pga_vmstats.pgma_allocfailed);
return (NULL);
}
/*
* This function is similar to page_get_freelist()/page_get_cachelist()
* but it searches both the lists to find a page with the specified
* color (or no color) and DMA attributes. The search is done in the
* freelist first and then in the cache list within the highest memory
* range (based on DMA attributes) before searching in the lower
* memory ranges.
*
* Note: This function is called only by page_create_io().
*/
/*ARGSUSED*/
static page_t *
page_get_anylist(struct vnode *vp, u_offset_t off, struct as *as, caddr_t vaddr,
size_t size, uint_t flags, ddi_dma_attr_t *dma_attr, lgrp_t *lgrp)
{
uint_t bin;
int mtype;
page_t *pp;
int n;
int m;
int szc;
int fullrange;
int mnode;
int local_failed_stat = 0;
lgrp_mnode_cookie_t lgrp_cookie;
VM_STAT_ADD(pga_vmstats.pga_alloc);
/* only base pagesize currently supported */
if (size != MMU_PAGESIZE)
return (NULL);
/*
* If we're passed a specific lgroup, we use it. Otherwise,
* assume first-touch placement is desired.
*/
if (!LGRP_EXISTS(lgrp))
lgrp = lgrp_home_lgrp();
/* LINTED */
AS_2_BIN(as, seg, vp, vaddr, bin, 0);
/*
* Only hold one freelist or cachelist lock at a time, that way we
* can start anywhere and not have to worry about lock
* ordering.
*/
if (dma_attr == NULL) {
n = mtype16m;
m = mtypetop;
fullrange = 1;
VM_STAT_ADD(pga_vmstats.pga_nulldmaattr);
} else {
pfn_t pfnlo = mmu_btop(dma_attr->dma_attr_addr_lo);
pfn_t pfnhi = mmu_btop(dma_attr->dma_attr_addr_hi);
/*
* We can guarantee alignment only for page boundary.
*/
if (dma_attr->dma_attr_align > MMU_PAGESIZE)
return (NULL);
/* Sanity check the dma_attr */
if (pfnlo > pfnhi)
return (NULL);
n = pfn_2_mtype(pfnlo);
m = pfn_2_mtype(pfnhi);
fullrange = ((pfnlo == mnoderanges[n].mnr_pfnlo) &&
(pfnhi >= mnoderanges[m].mnr_pfnhi));
}
VM_STAT_COND_ADD(fullrange == 0, pga_vmstats.pga_notfullrange);
szc = 0;
/* cylcing thru mtype handled by RANGE0 if n == mtype16m */
if (n == mtype16m) {
flags |= PGI_MT_RANGE0;
n = m;
}
/*
* Try local memory node first, but try remote if we can't
* get a page of the right color.
*/
LGRP_MNODE_COOKIE_INIT(lgrp_cookie, lgrp, LGRP_SRCH_HIER);
while ((mnode = lgrp_memnode_choose(&lgrp_cookie)) >= 0) {
/*
* allocate pages from high pfn to low.
*/
mtype = m;
do {
if (fullrange != 0) {
pp = page_get_mnode_freelist(mnode,
bin, mtype, szc, flags);
if (pp == NULL) {
pp = page_get_mnode_cachelist(
bin, flags, mnode, mtype);
}
} else {
pp = page_get_mnode_anylist(bin, szc,
flags, mnode, mtype, dma_attr);
}
if (pp != NULL) {
VM_STAT_ADD(pga_vmstats.pga_allocok);
check_dma(dma_attr, pp, 1);
return (pp);
}
} while (mtype != n &&
(mtype = mnoderanges[mtype].mnr_next) != -1);
if (!local_failed_stat) {
lgrp_stat_add(lgrp->lgrp_id, LGRP_NUM_ALLOC_FAIL, 1);
local_failed_stat = 1;
}
}
VM_STAT_ADD(pga_vmstats.pga_allocfailed);
return (NULL);
}
/*
* page_create_io()
*
* This function is a copy of page_create_va() with an additional
* argument 'mattr' that specifies DMA memory requirements to
* the page list functions. This function is used by the segkmem
* allocator so it is only to create new pages (i.e PG_EXCL is
* set).
*
* Note: This interface is currently used by x86 PSM only and is
* not fully specified so the commitment level is only for
* private interface specific to x86. This interface uses PSM
* specific page_get_anylist() interface.
*/
#define PAGE_HASH_SEARCH(index, pp, vp, off) { \
for ((pp) = page_hash[(index)]; (pp); (pp) = (pp)->p_hash) { \
if ((pp)->p_vnode == (vp) && (pp)->p_offset == (off)) \
break; \
} \
}
page_t *
page_create_io(
struct vnode *vp,
u_offset_t off,
uint_t bytes,
uint_t flags,
struct as *as,
caddr_t vaddr,
ddi_dma_attr_t *mattr) /* DMA memory attributes if any */
{
page_t *plist = NULL;
uint_t plist_len = 0;
pgcnt_t npages;
page_t *npp = NULL;
uint_t pages_req;
page_t *pp;
kmutex_t *phm = NULL;
uint_t index;
TRACE_4(TR_FAC_VM, TR_PAGE_CREATE_START,
"page_create_start:vp %p off %llx bytes %u flags %x",
vp, off, bytes, flags);
ASSERT((flags & ~(PG_EXCL | PG_WAIT | PG_PHYSCONTIG)) == 0);
pages_req = npages = mmu_btopr(bytes);
/*
* Do the freemem and pcf accounting.
*/
if (!page_create_wait(npages, flags)) {
return (NULL);
}
TRACE_2(TR_FAC_VM, TR_PAGE_CREATE_SUCCESS,
"page_create_success:vp %p off %llx", vp, off);
/*
* If satisfying this request has left us with too little
* memory, start the wheels turning to get some back. The
* first clause of the test prevents waking up the pageout
* daemon in situations where it would decide that there's
* nothing to do.
*/
if (nscan < desscan && freemem < minfree) {
TRACE_1(TR_FAC_VM, TR_PAGEOUT_CV_SIGNAL,
"pageout_cv_signal:freemem %ld", freemem);
WAKE_PAGEOUT_SCANNER();
}
if (flags & PG_PHYSCONTIG) {
plist = page_get_contigpage(&npages, mattr, 1);
if (plist == NULL) {
page_create_putback(npages);
return (NULL);
}
pp = plist;
do {
if (!page_hashin(pp, vp, off, NULL)) {
panic("pg_creat_io: hashin failed %p %p %llx",
(void *)pp, (void *)vp, off);
}
VM_STAT_ADD(page_create_new);
off += MMU_PAGESIZE;
PP_CLRFREE(pp);
PP_CLRAGED(pp);
page_set_props(pp, P_REF);
pp = pp->p_next;
} while (pp != plist);
if (!npages) {
check_dma(mattr, plist, pages_req);
return (plist);
} else {
vaddr += (pages_req - npages) << MMU_PAGESHIFT;
}
/*
* fall-thru:
*
* page_get_contigpage returns when npages <= sgllen.
* Grab the rest of the non-contig pages below from anylist.
*/
}
/*
* Loop around collecting the requested number of pages.
* Most of the time, we have to `create' a new page. With
* this in mind, pull the page off the free list before
* getting the hash lock. This will minimize the hash
* lock hold time, nesting, and the like. If it turns
* out we don't need the page, we put it back at the end.
*/
while (npages--) {
phm = NULL;
index = PAGE_HASH_FUNC(vp, off);
top:
ASSERT(phm == NULL);
ASSERT(index == PAGE_HASH_FUNC(vp, off));
ASSERT(MUTEX_NOT_HELD(page_vnode_mutex(vp)));
if (npp == NULL) {
/*
* Try to get the page of any color either from
* the freelist or from the cache list.
*/
npp = page_get_anylist(vp, off, as, vaddr, MMU_PAGESIZE,
flags & ~PG_MATCH_COLOR, mattr, NULL);
if (npp == NULL) {
if (mattr == NULL) {
/*
* Not looking for a special page;
* panic!
*/
panic("no page found %d", (int)npages);
}
/*
* No page found! This can happen
* if we are looking for a page
* within a specific memory range
* for DMA purposes. If PG_WAIT is
* specified then we wait for a
* while and then try again. The
* wait could be forever if we
* don't get the page(s) we need.
*
* Note: XXX We really need a mechanism
* to wait for pages in the desired
* range. For now, we wait for any
* pages and see if we can use it.
*/
if ((mattr != NULL) && (flags & PG_WAIT)) {
delay(10);
goto top;
}
goto fail; /* undo accounting stuff */
}
if (PP_ISAGED(npp) == 0) {
/*
* Since this page came from the
* cachelist, we must destroy the
* old vnode association.
*/
page_hashout(npp, (kmutex_t *)NULL);
}
}
/*
* We own this page!
*/
ASSERT(PAGE_EXCL(npp));
ASSERT(npp->p_vnode == NULL);
ASSERT(!hat_page_is_mapped(npp));
PP_CLRFREE(npp);
PP_CLRAGED(npp);
/*
* Here we have a page in our hot little mits and are
* just waiting to stuff it on the appropriate lists.
* Get the mutex and check to see if it really does
* not exist.
*/
phm = PAGE_HASH_MUTEX(index);
mutex_enter(phm);
PAGE_HASH_SEARCH(index, pp, vp, off);
if (pp == NULL) {
VM_STAT_ADD(page_create_new);
pp = npp;
npp = NULL;
if (!page_hashin(pp, vp, off, phm)) {
/*
* Since we hold the page hash mutex and
* just searched for this page, page_hashin
* had better not fail. If it does, that
* means somethread did not follow the
* page hash mutex rules. Panic now and
* get it over with. As usual, go down
* holding all the locks.
*/
ASSERT(MUTEX_HELD(phm));
panic("page_create: hashin fail %p %p %llx %p",
(void *)pp, (void *)vp, off, (void *)phm);
}
ASSERT(MUTEX_HELD(phm));
mutex_exit(phm);
phm = NULL;
/*
* Hat layer locking need not be done to set
* the following bits since the page is not hashed
* and was on the free list (i.e., had no mappings).
*
* Set the reference bit to protect
* against immediate pageout
*
* XXXmh modify freelist code to set reference
* bit so we don't have to do it here.
*/
page_set_props(pp, P_REF);
} else {
ASSERT(MUTEX_HELD(phm));
mutex_exit(phm);
phm = NULL;
/*
* NOTE: This should not happen for pages associated
* with kernel vnode 'kvp'.
*/
/* XX64 - to debug why this happens! */
ASSERT(!VN_ISKAS(vp));
if (VN_ISKAS(vp))
cmn_err(CE_NOTE,
"page_create: page not expected "
"in hash list for kernel vnode - pp 0x%p",
(void *)pp);
VM_STAT_ADD(page_create_exists);
goto fail;
}
/*
* Got a page! It is locked. Acquire the i/o
* lock since we are going to use the p_next and
* p_prev fields to link the requested pages together.
*/
page_io_lock(pp);
page_add(&plist, pp);
plist = plist->p_next;
off += MMU_PAGESIZE;
vaddr += MMU_PAGESIZE;
}
check_dma(mattr, plist, pages_req);
return (plist);
fail:
if (npp != NULL) {
/*
* Did not need this page after all.
* Put it back on the free list.
*/
VM_STAT_ADD(page_create_putbacks);
PP_SETFREE(npp);
PP_SETAGED(npp);
npp->p_offset = (u_offset_t)-1;
page_list_add(npp, PG_FREE_LIST | PG_LIST_TAIL);
page_unlock(npp);
}
/*
* Give up the pages we already got.
*/
while (plist != NULL) {
pp = plist;
page_sub(&plist, pp);
page_io_unlock(pp);
plist_len++;
/*LINTED: constant in conditional ctx*/
VN_DISPOSE(pp, B_INVAL, 0, kcred);
}
/*
* VN_DISPOSE does freemem accounting for the pages in plist
* by calling page_free. So, we need to undo the pcf accounting
* for only the remaining pages.
*/
VM_STAT_ADD(page_create_putbacks);
page_create_putback(pages_req - plist_len);
return (NULL);
}
#endif /* !__xpv */
/*
* Copy the data from the physical page represented by "frompp" to
* that represented by "topp". ppcopy uses CPU->cpu_caddr1 and
* CPU->cpu_caddr2. It assumes that no one uses either map at interrupt
* level and no one sleeps with an active mapping there.
*
* Note that the ref/mod bits in the page_t's are not affected by
* this operation, hence it is up to the caller to update them appropriately.
*/
int
ppcopy(page_t *frompp, page_t *topp)
{
caddr_t pp_addr1;
caddr_t pp_addr2;
hat_mempte_t pte1;
hat_mempte_t pte2;
kmutex_t *ppaddr_mutex;
label_t ljb;
int ret = 1;
ASSERT_STACK_ALIGNED();
ASSERT(PAGE_LOCKED(frompp));
ASSERT(PAGE_LOCKED(topp));
if (kpm_enable) {
pp_addr1 = hat_kpm_page2va(frompp, 0);
pp_addr2 = hat_kpm_page2va(topp, 0);
kpreempt_disable();
} else {
/*
* disable pre-emption so that CPU can't change
*/
kpreempt_disable();
pp_addr1 = CPU->cpu_caddr1;
pp_addr2 = CPU->cpu_caddr2;
pte1 = CPU->cpu_caddr1pte;
pte2 = CPU->cpu_caddr2pte;
ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
mutex_enter(ppaddr_mutex);
hat_mempte_remap(page_pptonum(frompp), pp_addr1, pte1,
PROT_READ | HAT_STORECACHING_OK, HAT_LOAD_NOCONSIST);
hat_mempte_remap(page_pptonum(topp), pp_addr2, pte2,
PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
HAT_LOAD_NOCONSIST);
}
if (on_fault(&ljb)) {
ret = 0;
goto faulted;
}
if (use_sse_pagecopy)
#ifdef __xpv
page_copy_no_xmm(pp_addr2, pp_addr1);
#else
hwblkpagecopy(pp_addr1, pp_addr2);
#endif
else
bcopy(pp_addr1, pp_addr2, PAGESIZE);
no_fault();
faulted:
if (!kpm_enable) {
#ifdef __xpv
/*
* We can't leave unused mappings laying about under the
* hypervisor, so blow them away.
*/
if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr1, 0,
UVMF_INVLPG | UVMF_LOCAL) < 0)
panic("HYPERVISOR_update_va_mapping() failed");
if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
UVMF_INVLPG | UVMF_LOCAL) < 0)
panic("HYPERVISOR_update_va_mapping() failed");
#endif
mutex_exit(ppaddr_mutex);
}
kpreempt_enable();
return (ret);
}
void
pagezero(page_t *pp, uint_t off, uint_t len)
{
ASSERT(PAGE_LOCKED(pp));
pfnzero(page_pptonum(pp), off, len);
}
/*
* Zero the physical page from off to off + len given by pfn
* without changing the reference and modified bits of page.
*
* We use this using CPU private page address #2, see ppcopy() for more info.
* pfnzero() must not be called at interrupt level.
*/
void
pfnzero(pfn_t pfn, uint_t off, uint_t len)
{
caddr_t pp_addr2;
hat_mempte_t pte2;
kmutex_t *ppaddr_mutex = NULL;
ASSERT_STACK_ALIGNED();
ASSERT(len <= MMU_PAGESIZE);
ASSERT(off <= MMU_PAGESIZE);
ASSERT(off + len <= MMU_PAGESIZE);
if (kpm_enable && !pfn_is_foreign(pfn)) {
pp_addr2 = hat_kpm_pfn2va(pfn);
kpreempt_disable();
} else {
kpreempt_disable();
pp_addr2 = CPU->cpu_caddr2;
pte2 = CPU->cpu_caddr2pte;
ppaddr_mutex = &CPU->cpu_ppaddr_mutex;
mutex_enter(ppaddr_mutex);
hat_mempte_remap(pfn, pp_addr2, pte2,
PROT_READ | PROT_WRITE | HAT_STORECACHING_OK,
HAT_LOAD_NOCONSIST);
}
if (use_sse_pagezero) {
#ifdef __xpv
uint_t rem;
/*
* zero a byte at a time until properly aligned for
* block_zero_no_xmm().
*/
while (!P2NPHASE(off, ((uint_t)BLOCKZEROALIGN)) && len-- > 0)
pp_addr2[off++] = 0;
/*
* Now use faster block_zero_no_xmm() for any range
* that is properly aligned and sized.
*/
rem = P2PHASE(len, ((uint_t)BLOCKZEROALIGN));
len -= rem;
if (len != 0) {
block_zero_no_xmm(pp_addr2 + off, len);
off += len;
}
/*
* zero remainder with byte stores.
*/
while (rem-- > 0)
pp_addr2[off++] = 0;
#else
hwblkclr(pp_addr2 + off, len);
#endif
} else {
bzero(pp_addr2 + off, len);
}
if (!kpm_enable || pfn_is_foreign(pfn)) {
#ifdef __xpv
/*
* On the hypervisor this page might get used for a page
* table before any intervening change to this mapping,
* so blow it away.
*/
if (HYPERVISOR_update_va_mapping((uintptr_t)pp_addr2, 0,
UVMF_INVLPG) < 0)
panic("HYPERVISOR_update_va_mapping() failed");
#endif
mutex_exit(ppaddr_mutex);
}
kpreempt_enable();
}
/*
* Platform-dependent page scrub call.
*/
void
pagescrub(page_t *pp, uint_t off, uint_t len)
{
/*
* For now, we rely on the fact that pagezero() will
* always clear UEs.
*/
pagezero(pp, off, len);
}
/*
* set up two private addresses for use on a given CPU for use in ppcopy()
*/
void
setup_vaddr_for_ppcopy(struct cpu *cpup)
{
void *addr;
hat_mempte_t pte_pa;
addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
pte_pa = hat_mempte_setup(addr);
cpup->cpu_caddr1 = addr;
cpup->cpu_caddr1pte = pte_pa;
addr = vmem_alloc(heap_arena, mmu_ptob(1), VM_SLEEP);
pte_pa = hat_mempte_setup(addr);
cpup->cpu_caddr2 = addr;
cpup->cpu_caddr2pte = pte_pa;
mutex_init(&cpup->cpu_ppaddr_mutex, NULL, MUTEX_DEFAULT, NULL);
}
/*
* Undo setup_vaddr_for_ppcopy
*/
void
teardown_vaddr_for_ppcopy(struct cpu *cpup)
{
mutex_destroy(&cpup->cpu_ppaddr_mutex);
hat_mempte_release(cpup->cpu_caddr2, cpup->cpu_caddr2pte);
cpup->cpu_caddr2pte = 0;
vmem_free(heap_arena, cpup->cpu_caddr2, mmu_ptob(1));
cpup->cpu_caddr2 = 0;
hat_mempte_release(cpup->cpu_caddr1, cpup->cpu_caddr1pte);
cpup->cpu_caddr1pte = 0;
vmem_free(heap_arena, cpup->cpu_caddr1, mmu_ptob(1));
cpup->cpu_caddr1 = 0;
}
/*
* Function for flushing D-cache when performing module relocations
* to an alternate mapping. Unnecessary on Intel / AMD platforms.
*/
void
dcache_flushall()
{}
/*
* Allocate a memory page. The argument 'seed' can be any pseudo-random
* number to vary where the pages come from. This is quite a hacked up
* method -- it works for now, but really needs to be fixed up a bit.
*
* We currently use page_create_va() on the kvp with fake offsets,
* segments and virt address. This is pretty bogus, but was copied from the
* old hat_i86.c code. A better approach would be to specify either mnode
* random or mnode local and takes a page from whatever color has the MOST
* available - this would have a minimal impact on page coloring.
*/
page_t *
page_get_physical(uintptr_t seed)
{
page_t *pp;
u_offset_t offset;
static struct seg tmpseg;
static uintptr_t ctr = 0;
/*
* This code is gross, we really need a simpler page allocator.
*
* We need to assign an offset for the page to call page_create_va()
* To avoid conflicts with other pages, we get creative with the offset.
* For 32 bits, we need an offset > 4Gig
* For 64 bits, need an offset somewhere in the VA hole.
*/
offset = seed;
if (offset > kernelbase)
offset -= kernelbase;
offset <<= MMU_PAGESHIFT;
#if defined(__amd64)
offset += mmu.hole_start; /* something in VA hole */
#else
offset += 1ULL << 40; /* something > 4 Gig */
#endif
if (page_resv(1, KM_NOSLEEP) == 0)
return (NULL);
#ifdef DEBUG
pp = page_exists(&kvp, offset);
if (pp != NULL)
panic("page already exists %p", (void *)pp);
#endif
pp = page_create_va(&kvp, offset, MMU_PAGESIZE, PG_EXCL,
&tmpseg, (caddr_t)(ctr += MMU_PAGESIZE)); /* changing VA usage */
if (pp != NULL) {
page_io_unlock(pp);
page_downgrade(pp);
}
return (pp);
}
|