Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 | /* * Copyright (c) 2000-2007 Apple Inc. All rights reserved. * * @APPLE_OSREFERENCE_LICENSE_HEADER_START@ * * This file contains Original Code and/or Modifications of Original Code * as defined in and that are subject to the Apple Public Source License * Version 2.0 (the 'License'). You may not use this file except in * compliance with the License. The rights granted to you under the License * may not be used to create, or enable the creation or redistribution of, * unlawful or unlicensed copies of an Apple operating system, or to * circumvent, violate, or enable the circumvention or violation of, any * terms of an Apple operating system software license agreement. * * Please obtain a copy of the License at * http://www.opensource.apple.com/apsl/ and read it before using this file. * * The Original Code and all software distributed under the License are * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. * Please see the License for the specific language governing rights and * limitations under the License. * * @APPLE_OSREFERENCE_LICENSE_HEADER_END@ */ /* * @OSF_COPYRIGHT@ */ /* * Mach Operating System * Copyright (c) 1991,1990,1989,1988,1987 Carnegie Mellon University * All Rights Reserved. * * Permission to use, copy, modify and distribute this software and its * documentation is hereby granted, provided that both the copyright * notice and this permission notice appear in all copies of the * software, derivative works or modified versions, and any portions * thereof, and that both notices appear in supporting documentation. * * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND FOR * ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. * * Carnegie Mellon requests users of this software to return to * * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU * School of Computer Science * Carnegie Mellon University * Pittsburgh PA 15213-3890 * * any improvements or extensions that they make and grant Carnegie Mellon * the rights to redistribute these changes. */ /* */ /* * File: vm/vm_object.c * Author: Avadis Tevanian, Jr., Michael Wayne Young * * Virtual memory object module. */ #include <debug.h> #include <mach_pagemap.h> #include <task_swapper.h> #include <mach/mach_types.h> #include <mach/memory_object.h> #include <mach/memory_object_default.h> #include <mach/memory_object_control_server.h> #include <mach/vm_param.h> #include <ipc/ipc_types.h> #include <ipc/ipc_port.h> #include <kern/kern_types.h> #include <kern/assert.h> #include <kern/lock.h> #include <kern/queue.h> #include <kern/xpr.h> #include <kern/zalloc.h> #include <kern/host.h> #include <kern/host_statistics.h> #include <kern/processor.h> #include <kern/misc_protos.h> #include <vm/memory_object.h> #include <vm/vm_fault.h> #include <vm/vm_map.h> #include <vm/vm_object.h> #include <vm/vm_page.h> #include <vm/vm_pageout.h> #include <vm/vm_protos.h> #include <vm/vm_purgeable_internal.h> #if CONFIG_EMBEDDED #include <sys/kern_memorystatus.h> #endif /* * Virtual memory objects maintain the actual data * associated with allocated virtual memory. A given * page of memory exists within exactly one object. * * An object is only deallocated when all "references" * are given up. * * Associated with each object is a list of all resident * memory pages belonging to that object; this list is * maintained by the "vm_page" module, but locked by the object's * lock. * * Each object also records the memory object reference * that is used by the kernel to request and write * back data (the memory object, field "pager"), etc... * * Virtual memory objects are allocated to provide * zero-filled memory (vm_allocate) or map a user-defined * memory object into a virtual address space (vm_map). * * Virtual memory objects that refer to a user-defined * memory object are called "permanent", because all changes * made in virtual memory are reflected back to the * memory manager, which may then store it permanently. * Other virtual memory objects are called "temporary", * meaning that changes need be written back only when * necessary to reclaim pages, and that storage associated * with the object can be discarded once it is no longer * mapped. * * A permanent memory object may be mapped into more * than one virtual address space. Moreover, two threads * may attempt to make the first mapping of a memory * object concurrently. Only one thread is allowed to * complete this mapping; all others wait for the * "pager_initialized" field is asserted, indicating * that the first thread has initialized all of the * necessary fields in the virtual memory object structure. * * The kernel relies on a *default memory manager* to * provide backing storage for the zero-filled virtual * memory objects. The pager memory objects associated * with these temporary virtual memory objects are only * requested from the default memory manager when it * becomes necessary. Virtual memory objects * that depend on the default memory manager are called * "internal". The "pager_created" field is provided to * indicate whether these ports have ever been allocated. * * The kernel may also create virtual memory objects to * hold changed pages after a copy-on-write operation. * In this case, the virtual memory object (and its * backing storage -- its memory object) only contain * those pages that have been changed. The "shadow" * field refers to the virtual memory object that contains * the remainder of the contents. The "shadow_offset" * field indicates where in the "shadow" these contents begin. * The "copy" field refers to a virtual memory object * to which changed pages must be copied before changing * this object, in order to implement another form * of copy-on-write optimization. * * The virtual memory object structure also records * the attributes associated with its memory object. * The "pager_ready", "can_persist" and "copy_strategy" * fields represent those attributes. The "cached_list" * field is used in the implementation of the persistence * attribute. * * ZZZ Continue this comment. */ /* Forward declarations for internal functions. */ static kern_return_t vm_object_terminate( vm_object_t object); extern void vm_object_remove( vm_object_t object); static kern_return_t vm_object_copy_call( vm_object_t src_object, vm_object_offset_t src_offset, vm_object_size_t size, vm_object_t *_result_object); static void vm_object_do_collapse( vm_object_t object, vm_object_t backing_object); static void vm_object_do_bypass( vm_object_t object, vm_object_t backing_object); static void vm_object_release_pager( memory_object_t pager, boolean_t hashed); static zone_t vm_object_zone; /* vm backing store zone */ /* * All wired-down kernel memory belongs to a single virtual * memory object (kernel_object) to avoid wasting data structures. */ static struct vm_object kernel_object_store; vm_object_t kernel_object; /* * The submap object is used as a placeholder for vm_map_submap * operations. The object is declared in vm_map.c because it * is exported by the vm_map module. The storage is declared * here because it must be initialized here. */ static struct vm_object vm_submap_object_store; /* * Virtual memory objects are initialized from * a template (see vm_object_allocate). * * When adding a new field to the virtual memory * object structure, be sure to add initialization * (see _vm_object_allocate()). */ static struct vm_object vm_object_template; unsigned int vm_page_purged_wired = 0; unsigned int vm_page_purged_busy = 0; unsigned int vm_page_purged_others = 0; #if VM_OBJECT_CACHE /* * Virtual memory objects that are not referenced by * any address maps, but that are allowed to persist * (an attribute specified by the associated memory manager), * are kept in a queue (vm_object_cached_list). * * When an object from this queue is referenced again, * for example to make another address space mapping, * it must be removed from the queue. That is, the * queue contains *only* objects with zero references. * * The kernel may choose to terminate objects from this * queue in order to reclaim storage. The current policy * is to permit a fixed maximum number of unreferenced * objects (vm_object_cached_max). * * A spin lock (accessed by routines * vm_object_cache_{lock,lock_try,unlock}) governs the * object cache. It must be held when objects are * added to or removed from the cache (in vm_object_terminate). * The routines that acquire a reference to a virtual * memory object based on one of the memory object ports * must also lock the cache. * * Ideally, the object cache should be more isolated * from the reference mechanism, so that the lock need * not be held to make simple references. */ static vm_object_t vm_object_cache_trim( boolean_t called_from_vm_object_deallocate); static queue_head_t vm_object_cached_list; static int vm_object_cached_count=0; static int vm_object_cached_high; /* highest # cached objects */ static int vm_object_cached_max = 512; /* may be patched*/ static lck_mtx_t vm_object_cached_lock_data; static lck_mtx_ext_t vm_object_cached_lock_data_ext; #define vm_object_cache_lock() \ lck_mtx_lock(&vm_object_cached_lock_data) #define vm_object_cache_lock_try() \ lck_mtx_try_lock(&vm_object_cached_lock_data) #define vm_object_cache_lock_spin() \ lck_mtx_lock_spin(&vm_object_cached_lock_data) #define vm_object_cache_unlock() \ lck_mtx_unlock(&vm_object_cached_lock_data) #endif /* VM_OBJECT_CACHE */ static void vm_object_deactivate_all_pages( vm_object_t object); #define VM_OBJECT_HASH_COUNT 1024 #define VM_OBJECT_HASH_LOCK_COUNT 512 static lck_mtx_t vm_object_hashed_lock_data[VM_OBJECT_HASH_LOCK_COUNT]; static lck_mtx_ext_t vm_object_hashed_lock_data_ext[VM_OBJECT_HASH_LOCK_COUNT]; static queue_head_t vm_object_hashtable[VM_OBJECT_HASH_COUNT]; static struct zone *vm_object_hash_zone; struct vm_object_hash_entry { queue_chain_t hash_link; /* hash chain link */ memory_object_t pager; /* pager we represent */ vm_object_t object; /* corresponding object */ boolean_t waiting; /* someone waiting for * termination */ }; typedef struct vm_object_hash_entry *vm_object_hash_entry_t; #define VM_OBJECT_HASH_ENTRY_NULL ((vm_object_hash_entry_t) 0) #define VM_OBJECT_HASH_SHIFT 5 #define vm_object_hash(pager) \ ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_COUNT)) #define vm_object_lock_hash(pager) \ ((int)((((uintptr_t)pager) >> VM_OBJECT_HASH_SHIFT) % VM_OBJECT_HASH_LOCK_COUNT)) void vm_object_hash_entry_free( vm_object_hash_entry_t entry); static void vm_object_reap(vm_object_t object); static void vm_object_reap_async(vm_object_t object); static void vm_object_reaper_thread(void); static lck_mtx_t vm_object_reaper_lock_data; static lck_mtx_ext_t vm_object_reaper_lock_data_ext; static queue_head_t vm_object_reaper_queue; /* protected by vm_object_reaper_lock() */ unsigned int vm_object_reap_count = 0; unsigned int vm_object_reap_count_async = 0; #define vm_object_reaper_lock() \ lck_mtx_lock(&vm_object_reaper_lock_data) #define vm_object_reaper_lock_spin() \ lck_mtx_lock_spin(&vm_object_reaper_lock_data) #define vm_object_reaper_unlock() \ lck_mtx_unlock(&vm_object_reaper_lock_data) static lck_mtx_t * vm_object_hash_lock_spin( memory_object_t pager) { int index; index = vm_object_lock_hash(pager); lck_mtx_lock_spin(&vm_object_hashed_lock_data[index]); return (&vm_object_hashed_lock_data[index]); } static void vm_object_hash_unlock(lck_mtx_t *lck) { lck_mtx_unlock(lck); } /* * vm_object_hash_lookup looks up a pager in the hashtable * and returns the corresponding entry, with optional removal. */ static vm_object_hash_entry_t vm_object_hash_lookup( memory_object_t pager, boolean_t remove_entry) { queue_t bucket; vm_object_hash_entry_t entry; bucket = &vm_object_hashtable[vm_object_hash(pager)]; entry = (vm_object_hash_entry_t)queue_first(bucket); while (!queue_end(bucket, (queue_entry_t)entry)) { if (entry->pager == pager) { if (remove_entry) { queue_remove(bucket, entry, vm_object_hash_entry_t, hash_link); } return(entry); } entry = (vm_object_hash_entry_t)queue_next(&entry->hash_link); } return(VM_OBJECT_HASH_ENTRY_NULL); } /* * vm_object_hash_enter enters the specified * pager / cache object association in the hashtable. */ static void vm_object_hash_insert( vm_object_hash_entry_t entry, vm_object_t object) { queue_t bucket; bucket = &vm_object_hashtable[vm_object_hash(entry->pager)]; queue_enter(bucket, entry, vm_object_hash_entry_t, hash_link); entry->object = object; object->hashed = TRUE; } static vm_object_hash_entry_t vm_object_hash_entry_alloc( memory_object_t pager) { vm_object_hash_entry_t entry; entry = (vm_object_hash_entry_t)zalloc(vm_object_hash_zone); entry->pager = pager; entry->object = VM_OBJECT_NULL; entry->waiting = FALSE; return(entry); } void vm_object_hash_entry_free( vm_object_hash_entry_t entry) { zfree(vm_object_hash_zone, entry); } /* * vm_object_allocate: * * Returns a new object with the given size. */ __private_extern__ void _vm_object_allocate( vm_object_size_t size, vm_object_t object) { XPR(XPR_VM_OBJECT, "vm_object_allocate, object 0x%X size 0x%X\n", object, size, 0,0,0); *object = vm_object_template; queue_init(&object->memq); queue_init(&object->msr_q); #if UPL_DEBUG queue_init(&object->uplq); #endif /* UPL_DEBUG */ vm_object_lock_init(object); object->size = size; } __private_extern__ vm_object_t vm_object_allocate( vm_object_size_t size) { register vm_object_t object; object = (vm_object_t) zalloc(vm_object_zone); // dbgLog(object, size, 0, 2); /* (TEST/DEBUG) */ if (object != VM_OBJECT_NULL) _vm_object_allocate(size, object); return object; } lck_grp_t vm_object_lck_grp; lck_grp_attr_t vm_object_lck_grp_attr; lck_attr_t vm_object_lck_attr; lck_attr_t kernel_object_lck_attr; /* * vm_object_bootstrap: * * Initialize the VM objects module. */ __private_extern__ void vm_object_bootstrap(void) { register int i; vm_object_zone = zinit((vm_size_t) sizeof(struct vm_object), round_page(512*1024), round_page(12*1024), "vm objects"); vm_object_init_lck_grp(); #if VM_OBJECT_CACHE queue_init(&vm_object_cached_list); lck_mtx_init_ext(&vm_object_cached_lock_data, &vm_object_cached_lock_data_ext, &vm_object_lck_grp, &vm_object_lck_attr); #endif queue_init(&vm_object_reaper_queue); for (i = 0; i < VM_OBJECT_HASH_LOCK_COUNT; i++) { lck_mtx_init_ext(&vm_object_hashed_lock_data[i], &vm_object_hashed_lock_data_ext[i], &vm_object_lck_grp, &vm_object_lck_attr); } lck_mtx_init_ext(&vm_object_reaper_lock_data, &vm_object_reaper_lock_data_ext, &vm_object_lck_grp, &vm_object_lck_attr); vm_object_hash_zone = zinit((vm_size_t) sizeof (struct vm_object_hash_entry), round_page(512*1024), round_page(12*1024), "vm object hash entries"); for (i = 0; i < VM_OBJECT_HASH_COUNT; i++) queue_init(&vm_object_hashtable[i]); /* * Fill in a template object, for quick initialization */ /* memq; Lock; init after allocation */ vm_object_template.memq.prev = NULL; vm_object_template.memq.next = NULL; #if 0 /* * We can't call vm_object_lock_init() here because that will * allocate some memory and VM is not fully initialized yet. * The lock will be initialized for each allocated object in * _vm_object_allocate(), so we don't need to initialize it in * the vm_object_template. */ vm_object_lock_init(&vm_object_template); #endif vm_object_template.size = 0; vm_object_template.memq_hint = VM_PAGE_NULL; vm_object_template.ref_count = 1; #if TASK_SWAPPER vm_object_template.res_count = 1; #endif /* TASK_SWAPPER */ vm_object_template.resident_page_count = 0; vm_object_template.wired_page_count = 0; vm_object_template.reusable_page_count = 0; vm_object_template.copy = VM_OBJECT_NULL; vm_object_template.shadow = VM_OBJECT_NULL; vm_object_template.shadow_offset = (vm_object_offset_t) 0; vm_object_template.pager = MEMORY_OBJECT_NULL; vm_object_template.paging_offset = 0; vm_object_template.pager_control = MEMORY_OBJECT_CONTROL_NULL; vm_object_template.copy_strategy = MEMORY_OBJECT_COPY_SYMMETRIC; vm_object_template.paging_in_progress = 0; vm_object_template.activity_in_progress = 0; /* Begin bitfields */ vm_object_template.all_wanted = 0; /* all bits FALSE */ vm_object_template.pager_created = FALSE; vm_object_template.pager_initialized = FALSE; vm_object_template.pager_ready = FALSE; vm_object_template.pager_trusted = FALSE; vm_object_template.can_persist = FALSE; vm_object_template.internal = TRUE; vm_object_template.temporary = TRUE; vm_object_template.private = FALSE; vm_object_template.pageout = FALSE; vm_object_template.alive = TRUE; vm_object_template.purgable = VM_PURGABLE_DENY; vm_object_template.shadowed = FALSE; vm_object_template.silent_overwrite = FALSE; vm_object_template.advisory_pageout = FALSE; vm_object_template.true_share = FALSE; vm_object_template.terminating = FALSE; vm_object_template.named = FALSE; vm_object_template.shadow_severed = FALSE; vm_object_template.phys_contiguous = FALSE; vm_object_template.nophyscache = FALSE; /* End bitfields */ vm_object_template.cached_list.prev = NULL; vm_object_template.cached_list.next = NULL; vm_object_template.msr_q.prev = NULL; vm_object_template.msr_q.next = NULL; vm_object_template.last_alloc = (vm_object_offset_t) 0; vm_object_template.sequential = (vm_object_offset_t) 0; vm_object_template.pages_created = 0; vm_object_template.pages_used = 0; #if MACH_PAGEMAP vm_object_template.existence_map = VM_EXTERNAL_NULL; #endif /* MACH_PAGEMAP */ vm_object_template.cow_hint = ~(vm_offset_t)0; #if MACH_ASSERT vm_object_template.paging_object = VM_OBJECT_NULL; #endif /* MACH_ASSERT */ /* cache bitfields */ vm_object_template.wimg_bits = VM_WIMG_DEFAULT; vm_object_template.code_signed = FALSE; vm_object_template.hashed = FALSE; vm_object_template.transposed = FALSE; vm_object_template.mapping_in_progress = FALSE; vm_object_template.volatile_empty = FALSE; vm_object_template.volatile_fault = FALSE; vm_object_template.all_reusable = FALSE; vm_object_template.blocked_access = FALSE; vm_object_template.__object2_unused_bits = 0; #if UPL_DEBUG vm_object_template.uplq.prev = NULL; vm_object_template.uplq.next = NULL; #endif /* UPL_DEBUG */ #ifdef VM_PIP_DEBUG bzero(&vm_object_template.pip_holders, sizeof (vm_object_template.pip_holders)); #endif /* VM_PIP_DEBUG */ vm_object_template.objq.next=NULL; vm_object_template.objq.prev=NULL; /* * Initialize the "kernel object" */ kernel_object = &kernel_object_store; /* * Note that in the following size specifications, we need to add 1 because * VM_MAX_KERNEL_ADDRESS (vm_last_addr) is a maximum address, not a size. */ #ifdef ppc _vm_object_allocate(vm_last_addr + 1, kernel_object); #else _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1, kernel_object); #endif kernel_object->copy_strategy = MEMORY_OBJECT_COPY_NONE; /* * Initialize the "submap object". Make it as large as the * kernel object so that no limit is imposed on submap sizes. */ vm_submap_object = &vm_submap_object_store; #ifdef ppc _vm_object_allocate(vm_last_addr + 1, vm_submap_object); #else _vm_object_allocate(VM_MAX_KERNEL_ADDRESS + 1, vm_submap_object); #endif vm_submap_object->copy_strategy = MEMORY_OBJECT_COPY_NONE; /* * Create an "extra" reference to this object so that we never * try to deallocate it; zfree doesn't like to be called with * non-zone memory. */ vm_object_reference(vm_submap_object); #if MACH_PAGEMAP vm_external_module_initialize(); #endif /* MACH_PAGEMAP */ } void vm_object_reaper_init(void) { kern_return_t kr; thread_t thread; kr = kernel_thread_start_priority( (thread_continue_t) vm_object_reaper_thread, NULL, BASEPRI_PREEMPT - 1, &thread); if (kr != KERN_SUCCESS) { panic("failed to launch vm_object_reaper_thread kr=0x%x", kr); } thread_deallocate(thread); } __private_extern__ void vm_object_init(void) { /* * Finish initializing the kernel object. */ } __private_extern__ void vm_object_init_lck_grp(void) { /* * initialze the vm_object lock world */ lck_grp_attr_setdefault(&vm_object_lck_grp_attr); lck_grp_init(&vm_object_lck_grp, "vm_object", &vm_object_lck_grp_attr); lck_attr_setdefault(&vm_object_lck_attr); lck_attr_setdefault(&kernel_object_lck_attr); lck_attr_cleardebug(&kernel_object_lck_attr); } #if VM_OBJECT_CACHE #define MIGHT_NOT_CACHE_SHADOWS 1 #if MIGHT_NOT_CACHE_SHADOWS static int cache_shadows = TRUE; #endif /* MIGHT_NOT_CACHE_SHADOWS */ #endif /* * vm_object_deallocate: * * Release a reference to the specified object, * gained either through a vm_object_allocate * or a vm_object_reference call. When all references * are gone, storage associated with this object * may be relinquished. * * No object may be locked. */ unsigned long vm_object_deallocate_shared_successes = 0; unsigned long vm_object_deallocate_shared_failures = 0; unsigned long vm_object_deallocate_shared_swap_failures = 0; __private_extern__ void vm_object_deallocate( register vm_object_t object) { #if VM_OBJECT_CACHE boolean_t retry_cache_trim = FALSE; uint32_t try_failed_count = 0; #endif vm_object_t shadow = VM_OBJECT_NULL; // if(object)dbgLog(object, object->ref_count, object->can_persist, 3); /* (TEST/DEBUG) */ // else dbgLog(object, 0, 0, 3); /* (TEST/DEBUG) */ if (object == VM_OBJECT_NULL) return; if (object == kernel_object) { vm_object_lock_shared(object); OSAddAtomic(-1, &object->ref_count); if (object->ref_count == 0) { panic("vm_object_deallocate: losing kernel_object\n"); } vm_object_unlock(object); return; } if (object->ref_count > 2 || (!object->named && object->ref_count > 1)) { UInt32 original_ref_count; volatile UInt32 *ref_count_p; Boolean atomic_swap; /* * The object currently looks like it is not being * kept alive solely by the reference we're about to release. * Let's try and release our reference without taking * all the locks we would need if we had to terminate the * object (cache lock + exclusive object lock). * Lock the object "shared" to make sure we don't race with * anyone holding it "exclusive". */ vm_object_lock_shared(object); ref_count_p = (volatile UInt32 *) &object->ref_count; original_ref_count = object->ref_count; /* * Test again as "ref_count" could have changed. * "named" shouldn't change. */ if (original_ref_count > 2 || (!object->named && original_ref_count > 1)) { atomic_swap = OSCompareAndSwap( original_ref_count, original_ref_count - 1, (UInt32 *) &object->ref_count); if (atomic_swap == FALSE) { vm_object_deallocate_shared_swap_failures++; } } else { atomic_swap = FALSE; } vm_object_unlock(object); if (atomic_swap) { /* * ref_count was updated atomically ! */ vm_object_deallocate_shared_successes++; return; } /* * Someone else updated the ref_count at the same * time and we lost the race. Fall back to the usual * slow but safe path... */ vm_object_deallocate_shared_failures++; } while (object != VM_OBJECT_NULL) { vm_object_lock(object); assert(object->ref_count > 0); /* * If the object has a named reference, and only * that reference would remain, inform the pager * about the last "mapping" reference going away. */ if ((object->ref_count == 2) && (object->named)) { memory_object_t pager = object->pager; /* Notify the Pager that there are no */ /* more mappers for this object */ if (pager != MEMORY_OBJECT_NULL) { vm_object_mapping_wait(object, THREAD_UNINT); vm_object_mapping_begin(object); vm_object_unlock(object); memory_object_last_unmap(pager); vm_object_lock(object); vm_object_mapping_end(object); } /* * recheck the ref_count since we dropped the object lock * to call 'memory_object_last_unmap'... it's possible * additional references got taken and we only want * to deactivate the pages if this 'named' object will only * referenced by the backing pager once we drop our reference * below */ if (!object->terminating && object->ref_count == 2) vm_object_deactivate_all_pages(object); assert(object->ref_count > 0); } /* * Lose the reference. If other references * remain, then we are done, unless we need * to retry a cache trim. * If it is the last reference, then keep it * until any pending initialization is completed. */ /* if the object is terminating, it cannot go into */ /* the cache and we obviously should not call */ /* terminate again. */ if ((object->ref_count > 1) || object->terminating) { vm_object_lock_assert_exclusive(object); object->ref_count--; vm_object_res_deallocate(object); if (object->ref_count == 1 && object->shadow != VM_OBJECT_NULL) { /* * There's only one reference left on this * VM object. We can't tell if it's a valid * one (from a mapping for example) or if this * object is just part of a possibly stale and * useless shadow chain. * We would like to try and collapse it into * its parent, but we don't have any pointers * back to this parent object. * But we can try and collapse this object with * its own shadows, in case these are useless * too... * We can't bypass this object though, since we * don't know if this last reference on it is * meaningful or not. */ vm_object_collapse(object, 0, FALSE); } vm_object_unlock(object); #if VM_OBJECT_CACHE if (retry_cache_trim && ((object = vm_object_cache_trim(TRUE)) != VM_OBJECT_NULL)) { continue; } #endif return; } /* * We have to wait for initialization * before destroying or caching the object. */ if (object->pager_created && ! object->pager_initialized) { assert(! object->can_persist); vm_object_assert_wait(object, VM_OBJECT_EVENT_INITIALIZED, THREAD_UNINT); vm_object_unlock(object); thread_block(THREAD_CONTINUE_NULL); continue; } #if VM_OBJECT_CACHE /* * If this object can persist, then enter it in * the cache. Otherwise, terminate it. * * NOTE: Only permanent objects are cached, and * permanent objects cannot have shadows. This * affects the residence counting logic in a minor * way (can do it in-line, mostly). */ if ((object->can_persist) && (object->alive)) { /* * Now it is safe to decrement reference count, * and to return if reference count is > 0. */ vm_object_lock_assert_exclusive(object); if (--object->ref_count > 0) { vm_object_res_deallocate(object); vm_object_unlock(object); if (retry_cache_trim && ((object = vm_object_cache_trim(TRUE)) != VM_OBJECT_NULL)) { continue; } return; } #if MIGHT_NOT_CACHE_SHADOWS /* * Remove shadow now if we don't * want to cache shadows. */ if (! cache_shadows) { shadow = object->shadow; object->shadow = VM_OBJECT_NULL; } #endif /* MIGHT_NOT_CACHE_SHADOWS */ /* * Enter the object onto the queue of * cached objects, and deactivate * all of its pages. */ assert(object->shadow == VM_OBJECT_NULL); VM_OBJ_RES_DECR(object); XPR(XPR_VM_OBJECT, "vm_o_deallocate: adding %x to cache, queue = (%x, %x)\n", object, vm_object_cached_list.next, vm_object_cached_list.prev,0,0); vm_object_unlock(object); try_failed_count = 0; for (;;) { vm_object_cache_lock(); /* * if we try to take a regular lock here * we risk deadlocking against someone * holding a lock on this object while * trying to vm_object_deallocate a different * object */ if (vm_object_lock_try(object)) break; vm_object_cache_unlock(); try_failed_count++; mutex_pause(try_failed_count); /* wait a bit */ } vm_object_cached_count++; if (vm_object_cached_count > vm_object_cached_high) vm_object_cached_high = vm_object_cached_count; queue_enter(&vm_object_cached_list, object, vm_object_t, cached_list); vm_object_cache_unlock(); vm_object_deactivate_all_pages(object); vm_object_unlock(object); #if MIGHT_NOT_CACHE_SHADOWS /* * If we have a shadow that we need * to deallocate, do so now, remembering * to trim the cache later. */ if (! cache_shadows && shadow != VM_OBJECT_NULL) { object = shadow; retry_cache_trim = TRUE; continue; } #endif /* MIGHT_NOT_CACHE_SHADOWS */ /* * Trim the cache. If the cache trim * returns with a shadow for us to deallocate, * then remember to retry the cache trim * when we are done deallocating the shadow. * Otherwise, we are done. */ object = vm_object_cache_trim(TRUE); if (object == VM_OBJECT_NULL) { return; } retry_cache_trim = TRUE; } else #endif /* VM_OBJECT_CACHE */ { /* * This object is not cachable; terminate it. */ XPR(XPR_VM_OBJECT, "vm_o_deallocate: !cacheable 0x%X res %d paging_ops %d thread 0x%p ref %d\n", object, object->resident_page_count, object->paging_in_progress, (void *)current_thread(),object->ref_count); VM_OBJ_RES_DECR(object); /* XXX ? */ /* * Terminate this object. If it had a shadow, * then deallocate it; otherwise, if we need * to retry a cache trim, do so now; otherwise, * we are done. "pageout" objects have a shadow, * but maintain a "paging reference" rather than * a normal reference. */ shadow = object->pageout?VM_OBJECT_NULL:object->shadow; if (vm_object_terminate(object) != KERN_SUCCESS) { return; } if (shadow != VM_OBJECT_NULL) { object = shadow; continue; } #if VM_OBJECT_CACHE if (retry_cache_trim && ((object = vm_object_cache_trim(TRUE)) != VM_OBJECT_NULL)) { continue; } #endif return; } } #if VM_OBJECT_CACHE assert(! retry_cache_trim); #endif } #if VM_OBJECT_CACHE /* * Check to see whether we really need to trim * down the cache. If so, remove an object from * the cache, terminate it, and repeat. * * Called with, and returns with, cache lock unlocked. */ vm_object_t vm_object_cache_trim( boolean_t called_from_vm_object_deallocate) { register vm_object_t object = VM_OBJECT_NULL; vm_object_t shadow; for (;;) { /* * If we no longer need to trim the cache, * then we are done. */ if (vm_object_cached_count <= vm_object_cached_max) return VM_OBJECT_NULL; vm_object_cache_lock(); if (vm_object_cached_count <= vm_object_cached_max) { vm_object_cache_unlock(); return VM_OBJECT_NULL; } /* * We must trim down the cache, so remove * the first object in the cache. */ XPR(XPR_VM_OBJECT, "vm_object_cache_trim: removing from front of cache (%x, %x)\n", vm_object_cached_list.next, vm_object_cached_list.prev, 0, 0, 0); object = (vm_object_t) queue_first(&vm_object_cached_list); if(object == (vm_object_t) &vm_object_cached_list) { /* something's wrong with the calling parameter or */ /* the value of vm_object_cached_count, just fix */ /* and return */ if(vm_object_cached_max < 0) vm_object_cached_max = 0; vm_object_cached_count = 0; vm_object_cache_unlock(); return VM_OBJECT_NULL; } vm_object_lock(object); queue_remove(&vm_object_cached_list, object, vm_object_t, cached_list); vm_object_cached_count--; vm_object_cache_unlock(); /* * Since this object is in the cache, we know * that it is initialized and has no references. * Take a reference to avoid recursive deallocations. */ assert(object->pager_initialized); assert(object->ref_count == 0); vm_object_lock_assert_exclusive(object); object->ref_count++; /* * Terminate the object. * If the object had a shadow, we let vm_object_deallocate * deallocate it. "pageout" objects have a shadow, but * maintain a "paging reference" rather than a normal * reference. * (We are careful here to limit recursion.) */ shadow = object->pageout?VM_OBJECT_NULL:object->shadow; if(vm_object_terminate(object) != KERN_SUCCESS) continue; if (shadow != VM_OBJECT_NULL) { if (called_from_vm_object_deallocate) { return shadow; } else { vm_object_deallocate(shadow); } } } } #endif /* * Routine: vm_object_terminate * Purpose: * Free all resources associated with a vm_object. * In/out conditions: * Upon entry, the object must be locked, * and the object must have exactly one reference. * * The shadow object reference is left alone. * * The object must be unlocked if its found that pages * must be flushed to a backing object. If someone * manages to map the object while it is being flushed * the object is returned unlocked and unchanged. Otherwise, * upon exit, the cache will be unlocked, and the * object will cease to exist. */ static kern_return_t vm_object_terminate( vm_object_t object) { vm_object_t shadow_object; XPR(XPR_VM_OBJECT, "vm_object_terminate, object 0x%X ref %d\n", object, object->ref_count, 0, 0, 0); if (!object->pageout && (!object->temporary || object->can_persist) && (object->pager != NULL || object->shadow_severed)) { /* * Clear pager_trusted bit so that the pages get yanked * out of the object instead of cleaned in place. This * prevents a deadlock in XMM and makes more sense anyway. */ object->pager_trusted = FALSE; vm_object_reap_pages(object, REAP_TERMINATE); } /* * Make sure the object isn't already being terminated */ if (object->terminating) { vm_object_lock_assert_exclusive(object); object->ref_count--; assert(object->ref_count > 0); vm_object_unlock(object); return KERN_FAILURE; } /* * Did somebody get a reference to the object while we were * cleaning it? */ if (object->ref_count != 1) { vm_object_lock_assert_exclusive(object); object->ref_count--; assert(object->ref_count > 0); vm_object_res_deallocate(object); vm_object_unlock(object); return KERN_FAILURE; } /* * Make sure no one can look us up now. */ object->terminating = TRUE; object->alive = FALSE; if (object->hashed) { lck_mtx_t *lck; lck = vm_object_hash_lock_spin(object->pager); vm_object_remove(object); vm_object_hash_unlock(lck); } /* * Detach the object from its shadow if we are the shadow's * copy. The reference we hold on the shadow must be dropped * by our caller. */ if (((shadow_object = object->shadow) != VM_OBJECT_NULL) && !(object->pageout)) { vm_object_lock(shadow_object); if (shadow_object->copy == object) shadow_object->copy = VM_OBJECT_NULL; vm_object_unlock(shadow_object); } if (object->paging_in_progress != 0 || object->activity_in_progress != 0) { /* * There are still some paging_in_progress references * on this object, meaning that there are some paging * or other I/O operations in progress for this VM object. * Such operations take some paging_in_progress references * up front to ensure that the object doesn't go away, but * they may also need to acquire a reference on the VM object, * to map it in kernel space, for example. That means that * they may end up releasing the last reference on the VM * object, triggering its termination, while still holding * paging_in_progress references. Waiting for these * pending paging_in_progress references to go away here would * deadlock. * * To avoid deadlocking, we'll let the vm_object_reaper_thread * complete the VM object termination if it still holds * paging_in_progress references at this point. * * No new paging_in_progress should appear now that the * VM object is "terminating" and not "alive". */ vm_object_reap_async(object); vm_object_unlock(object); /* * Return KERN_FAILURE to let the caller know that we * haven't completed the termination and it can't drop this * object's reference on its shadow object yet. * The reaper thread will take care of that once it has * completed this object's termination. */ return KERN_FAILURE; } /* * complete the VM object termination */ vm_object_reap(object); object = VM_OBJECT_NULL; /* * the object lock was released by vm_object_reap() * * KERN_SUCCESS means that this object has been terminated * and no longer needs its shadow object but still holds a * reference on it. * The caller is responsible for dropping that reference. * We can't call vm_object_deallocate() here because that * would create a recursion. */ return KERN_SUCCESS; } /* * vm_object_reap(): * * Complete the termination of a VM object after it's been marked * as "terminating" and "!alive" by vm_object_terminate(). * * The VM object must be locked by caller. * The lock will be released on return and the VM object is no longer valid. */ void vm_object_reap( vm_object_t object) { memory_object_t pager; vm_object_lock_assert_exclusive(object); assert(object->paging_in_progress == 0); assert(object->activity_in_progress == 0); vm_object_reap_count++; pager = object->pager; object->pager = MEMORY_OBJECT_NULL; if (pager != MEMORY_OBJECT_NULL) memory_object_control_disable(object->pager_control); object->ref_count--; #if TASK_SWAPPER assert(object->res_count == 0); #endif /* TASK_SWAPPER */ assert (object->ref_count == 0); /* * remove from purgeable queue if it's on */ if (object->objq.next || object->objq.prev) { purgeable_q_t queue = vm_purgeable_object_remove(object); assert(queue); /* Must take page lock for this - using it to protect token queue */ vm_page_lock_queues(); vm_purgeable_token_delete_first(queue); assert(queue->debug_count_objects>=0); vm_page_unlock_queues(); } /* * Clean or free the pages, as appropriate. * It is possible for us to find busy/absent pages, * if some faults on this object were aborted. */ if (object->pageout) { assert(object->shadow != VM_OBJECT_NULL); vm_pageout_object_terminate(object); } else if (((object->temporary && !object->can_persist) || (pager == MEMORY_OBJECT_NULL))) { vm_object_reap_pages(object, REAP_REAP); } assert(queue_empty(&object->memq)); assert(object->paging_in_progress == 0); assert(object->activity_in_progress == 0); assert(object->ref_count == 0); /* * If the pager has not already been released by * vm_object_destroy, we need to terminate it and * release our reference to it here. */ if (pager != MEMORY_OBJECT_NULL) { vm_object_unlock(object); vm_object_release_pager(pager, object->hashed); vm_object_lock(object); } /* kick off anyone waiting on terminating */ object->terminating = FALSE; vm_object_paging_begin(object); vm_object_paging_end(object); vm_object_unlock(object); #if MACH_PAGEMAP vm_external_destroy(object->existence_map, object->size); #endif /* MACH_PAGEMAP */ object->shadow = VM_OBJECT_NULL; vm_object_lock_destroy(object); /* * Free the space for the object. */ zfree(vm_object_zone, object); object = VM_OBJECT_NULL; } #define V_O_R_MAX_BATCH 128 #define VM_OBJ_REAP_FREELIST(_local_free_q, do_disconnect) \ MACRO_BEGIN \ if (_local_free_q) { \ if (do_disconnect) { \ vm_page_t m; \ for (m = _local_free_q; \ m != VM_PAGE_NULL; \ m = (vm_page_t) m->pageq.next) { \ if (m->pmapped) { \ pmap_disconnect(m->phys_page); \ } \ } \ } \ vm_page_free_list(_local_free_q, TRUE); \ _local_free_q = VM_PAGE_NULL; \ } \ MACRO_END void vm_object_reap_pages( vm_object_t object, int reap_type) { vm_page_t p; vm_page_t next; vm_page_t local_free_q = VM_PAGE_NULL; int loop_count; boolean_t disconnect_on_release; if (reap_type == REAP_DATA_FLUSH) { /* * We need to disconnect pages from all pmaps before * releasing them to the free list */ disconnect_on_release = TRUE; } else { /* * Either the caller has already disconnected the pages * from all pmaps, or we disconnect them here as we add * them to out local list of pages to be released. * No need to re-disconnect them when we release the pages * to the free list. */ disconnect_on_release = FALSE; } restart_after_sleep: if (queue_empty(&object->memq)) return; loop_count = V_O_R_MAX_BATCH + 1; vm_page_lockspin_queues(); next = (vm_page_t)queue_first(&object->memq); while (!queue_end(&object->memq, (queue_entry_t)next)) { p = next; next = (vm_page_t)queue_next(&next->listq); if (--loop_count == 0) { vm_page_unlock_queues(); if (local_free_q) { /* * Free the pages we reclaimed so far * and take a little break to avoid * hogging the page queue lock too long */ VM_OBJ_REAP_FREELIST(local_free_q, disconnect_on_release); } else mutex_pause(0); loop_count = V_O_R_MAX_BATCH + 1; vm_page_lockspin_queues(); } if (reap_type == REAP_DATA_FLUSH || reap_type == REAP_TERMINATE) { if (reap_type == REAP_DATA_FLUSH && (p->pageout == TRUE && p->list_req_pending == TRUE)) { p->list_req_pending = FALSE; p->cleaning = FALSE; p->pageout = FALSE; /* * need to drop the laundry count... * we may also need to remove it * from the I/O paging queue... * vm_pageout_throttle_up handles both cases * * the laundry and pageout_queue flags are cleared... */ #if CONFIG_EMBEDDED if (p->laundry) vm_pageout_throttle_up(p); #else vm_pageout_throttle_up(p); #endif /* * toss the wire count we picked up * when we intially set this page up * to be cleaned... */ vm_page_unwire(p); PAGE_WAKEUP(p); } else if (p->busy || p->cleaning) { vm_page_unlock_queues(); /* * free the pages reclaimed so far */ VM_OBJ_REAP_FREELIST(local_free_q, disconnect_on_release); PAGE_SLEEP(object, p, THREAD_UNINT); goto restart_after_sleep; } } switch (reap_type) { case REAP_DATA_FLUSH: if (VM_PAGE_WIRED(p)) { /* * this is an odd case... perhaps we should * zero-fill this page since we're conceptually * tossing its data at this point, but leaving * it on the object to honor the 'wire' contract */ continue; } break; case REAP_PURGEABLE: if (VM_PAGE_WIRED(p)) { /* can't purge a wired page */ vm_page_purged_wired++; continue; } if (p->busy) { /* * We can't reclaim a busy page but we can * make it pageable (it's not wired) to make * sure that it gets considered by * vm_pageout_scan() later. */ vm_page_deactivate(p); vm_page_purged_busy++; continue; } if (p->cleaning || p->laundry || p->list_req_pending) { /* * page is being acted upon, * so don't mess with it */ vm_page_purged_others++; continue; } assert(p->object != kernel_object); /* * we can discard this page... */ if (p->pmapped == TRUE) { int refmod_state; /* * unmap the page */ refmod_state = pmap_disconnect(p->phys_page); if (refmod_state & VM_MEM_MODIFIED) { p->dirty = TRUE; } } if (p->dirty || p->precious) { /* * we saved the cost of cleaning this page ! */ vm_page_purged_count++; } break; case REAP_TERMINATE: if (p->absent || p->private) { /* * For private pages, VM_PAGE_FREE just * leaves the page structure around for * its owner to clean up. For absent * pages, the structure is returned to * the appropriate pool. */ break; } if (p->fictitious) { assert (p->phys_page == vm_page_guard_addr); break; } if (!p->dirty && p->wpmapped) p->dirty = pmap_is_modified(p->phys_page); if ((p->dirty || p->precious) && !p->error && object->alive) { p->busy = TRUE; VM_PAGE_QUEUES_REMOVE(p); vm_page_unlock_queues(); /* * free the pages reclaimed so far */ VM_OBJ_REAP_FREELIST(local_free_q, disconnect_on_release); /* * flush page... page will be freed * upon completion of I/O */ vm_pageout_cluster(p); vm_object_paging_wait(object, THREAD_UNINT); goto restart_after_sleep; } break; case REAP_REAP: break; } vm_page_free_prepare_queues(p); assert(p->pageq.next == NULL && p->pageq.prev == NULL); /* * Add this page to our list of reclaimed pages, * to be freed later. */ p->pageq.next = (queue_entry_t) local_free_q; local_free_q = p; } vm_page_unlock_queues(); /* * Free the remaining reclaimed pages */ VM_OBJ_REAP_FREELIST(local_free_q, disconnect_on_release); } void vm_object_reap_async( vm_object_t object) { vm_object_lock_assert_exclusive(object); vm_object_reaper_lock_spin(); vm_object_reap_count_async++; /* enqueue the VM object... */ queue_enter(&vm_object_reaper_queue, object, vm_object_t, cached_list); vm_object_reaper_unlock(); /* ... and wake up the reaper thread */ thread_wakeup((event_t) &vm_object_reaper_queue); } void vm_object_reaper_thread(void) { vm_object_t object, shadow_object; vm_object_reaper_lock_spin(); while (!queue_empty(&vm_object_reaper_queue)) { queue_remove_first(&vm_object_reaper_queue, object, vm_object_t, cached_list); vm_object_reaper_unlock(); vm_object_lock(object); assert(object->terminating); assert(!object->alive); /* * The pageout daemon might be playing with our pages. * Now that the object is dead, it won't touch any more * pages, but some pages might already be on their way out. * Hence, we wait until the active paging activities have * ceased before we break the association with the pager * itself. */ while (object->paging_in_progress != 0 || object->activity_in_progress != 0) { vm_object_wait(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS, THREAD_UNINT); vm_object_lock(object); } shadow_object = object->pageout ? VM_OBJECT_NULL : object->shadow; vm_object_reap(object); /* cache is unlocked and object is no longer valid */ object = VM_OBJECT_NULL; if (shadow_object != VM_OBJECT_NULL) { /* * Drop the reference "object" was holding on * its shadow object. */ vm_object_deallocate(shadow_object); shadow_object = VM_OBJECT_NULL; } vm_object_reaper_lock_spin(); } /* wait for more work... */ assert_wait((event_t) &vm_object_reaper_queue, THREAD_UNINT); vm_object_reaper_unlock(); thread_block((thread_continue_t) vm_object_reaper_thread); /*NOTREACHED*/ } /* * Routine: vm_object_pager_wakeup * Purpose: Wake up anyone waiting for termination of a pager. */ static void vm_object_pager_wakeup( memory_object_t pager) { vm_object_hash_entry_t entry; boolean_t waiting = FALSE; lck_mtx_t *lck; /* * If anyone was waiting for the memory_object_terminate * to be queued, wake them up now. */ lck = vm_object_hash_lock_spin(pager); entry = vm_object_hash_lookup(pager, TRUE); if (entry != VM_OBJECT_HASH_ENTRY_NULL) waiting = entry->waiting; vm_object_hash_unlock(lck); if (entry != VM_OBJECT_HASH_ENTRY_NULL) { if (waiting) thread_wakeup((event_t) pager); vm_object_hash_entry_free(entry); } } /* * Routine: vm_object_release_pager * Purpose: Terminate the pager and, upon completion, * release our last reference to it. * just like memory_object_terminate, except * that we wake up anyone blocked in vm_object_enter * waiting for termination message to be queued * before calling memory_object_init. */ static void vm_object_release_pager( memory_object_t pager, boolean_t hashed) { /* * Terminate the pager. */ (void) memory_object_terminate(pager); if (hashed == TRUE) { /* * Wakeup anyone waiting for this terminate * and remove the entry from the hash */ vm_object_pager_wakeup(pager); } /* * Release reference to pager. */ memory_object_deallocate(pager); } /* * Routine: vm_object_destroy * Purpose: * Shut down a VM object, despite the * presence of address map (or other) references * to the vm_object. */ kern_return_t vm_object_destroy( vm_object_t object, __unused kern_return_t reason) { memory_object_t old_pager; if (object == VM_OBJECT_NULL) return(KERN_SUCCESS); /* * Remove the pager association immediately. * * This will prevent the memory manager from further * meddling. [If it wanted to flush data or make * other changes, it should have done so before performing * the destroy call.] */ vm_object_lock(object); object->can_persist = FALSE; object->named = FALSE; object->alive = FALSE; if (object->hashed) { lck_mtx_t *lck; /* * Rip out the pager from the vm_object now... */ lck = vm_object_hash_lock_spin(object->pager); vm_object_remove(object); vm_object_hash_unlock(lck); } old_pager = object->pager; object->pager = MEMORY_OBJECT_NULL; if (old_pager != MEMORY_OBJECT_NULL) memory_object_control_disable(object->pager_control); /* * Wait for the existing paging activity (that got * through before we nulled out the pager) to subside. */ vm_object_paging_wait(object, THREAD_UNINT); vm_object_unlock(object); /* * Terminate the object now. */ if (old_pager != MEMORY_OBJECT_NULL) { vm_object_release_pager(old_pager, object->hashed); /* * JMM - Release the caller's reference. This assumes the * caller had a reference to release, which is a big (but * currently valid) assumption if this is driven from the * vnode pager (it is holding a named reference when making * this call).. */ vm_object_deallocate(object); } return(KERN_SUCCESS); } #define VM_OBJ_DEACT_ALL_STATS DEBUG #if VM_OBJ_DEACT_ALL_STATS uint32_t vm_object_deactivate_all_pages_batches = 0; uint32_t vm_object_deactivate_all_pages_pages = 0; #endif /* VM_OBJ_DEACT_ALL_STATS */ /* * vm_object_deactivate_all_pages * * Deactivate all pages in the specified object. (Keep its pages * in memory even though it is no longer referenced.) * * The object must be locked. */ static void vm_object_deactivate_all_pages( register vm_object_t object) { register vm_page_t p; int loop_count; #if VM_OBJ_DEACT_ALL_STATS int pages_count; #endif /* VM_OBJ_DEACT_ALL_STATS */ #define V_O_D_A_P_MAX_BATCH 256 loop_count = V_O_D_A_P_MAX_BATCH; #if VM_OBJ_DEACT_ALL_STATS pages_count = 0; #endif /* VM_OBJ_DEACT_ALL_STATS */ vm_page_lock_queues(); queue_iterate(&object->memq, p, vm_page_t, listq) { if (--loop_count == 0) { #if VM_OBJ_DEACT_ALL_STATS hw_atomic_add(&vm_object_deactivate_all_pages_batches, 1); hw_atomic_add(&vm_object_deactivate_all_pages_pages, pages_count); pages_count = 0; #endif /* VM_OBJ_DEACT_ALL_STATS */ lck_mtx_yield(&vm_page_queue_lock); loop_count = V_O_D_A_P_MAX_BATCH; } if (!p->busy && !p->throttled) { #if VM_OBJ_DEACT_ALL_STATS pages_count++; #endif /* VM_OBJ_DEACT_ALL_STATS */ vm_page_deactivate(p); } } #if VM_OBJ_DEACT_ALL_STATS if (pages_count) { hw_atomic_add(&vm_object_deactivate_all_pages_batches, 1); hw_atomic_add(&vm_object_deactivate_all_pages_pages, pages_count); pages_count = 0; } #endif /* VM_OBJ_DEACT_ALL_STATS */ vm_page_unlock_queues(); } /* * when deallocating pages it is necessary to hold * the vm_page_queue_lock (a hot global lock) for certain operations * on the page... however, the majority of the work can be done * while merely holding the object lock... to mitigate the time spent behind the * global lock, go to a 2 pass algorithm... collect pages up to DELAYED_WORK_LIMIT * while doing all of the work that doesn't require the vm_page_queue_lock... * them call dw_do_work to acquire the vm_page_queue_lock and do the * necessary work for each page... we will grab the busy bit on the page * so that dw_do_work can drop the object lock if it can't immediately take the * vm_page_queue_lock in order to compete for the locks in the same order that * vm_pageout_scan takes them. */ #define DELAYED_WORK_LIMIT 32 #define DW_clear_reference 0x01 #define DW_move_page 0x02 #define DW_clear_busy 0x04 #define DW_PAGE_WAKEUP 0x08 struct dw { vm_page_t dw_m; int dw_mask; }; static void dw_do_work(vm_object_t object, struct dw *dwp, int dw_count); static void dw_do_work( vm_object_t object, struct dw *dwp, int dw_count) { vm_page_t m; int j; /* * pageout_scan takes the vm_page_lock_queues first * then tries for the object lock... to avoid what * is effectively a lock inversion, we'll go to the * trouble of taking them in that same order... otherwise * if this object contains the majority of the pages resident * in the UBC (or a small set of large objects actively being * worked on contain the majority of the pages), we could * cause the pageout_scan thread to 'starve' in its attempt * to find pages to move to the free queue, since it has to * successfully acquire the object lock of any candidate page * before it can steal/clean it. */ if (!vm_page_trylockspin_queues()) { vm_object_unlock(object); vm_page_lockspin_queues(); for (j = 0; ; j++) { if (!vm_object_lock_avoid(object) && _vm_object_lock_try(object)) break; vm_page_unlock_queues(); mutex_pause(j); vm_page_lockspin_queues(); } } for (j = 0; j < dw_count; j++, dwp++) { m = dwp->dw_m; if (dwp->dw_mask & DW_clear_reference) m->reference = FALSE; if (dwp->dw_mask & DW_move_page) { VM_PAGE_QUEUES_REMOVE(m); assert(!m->laundry); assert(m->object != kernel_object); assert(m->pageq.next == NULL && m->pageq.prev == NULL); if (m->zero_fill) { queue_enter_first(&vm_page_queue_zf, m, vm_page_t, pageq); vm_zf_queue_count++; } else { queue_enter_first(&vm_page_queue_inactive, m, vm_page_t, pageq); } m->inactive = TRUE; if (!m->fictitious) { vm_page_inactive_count++; token_new_pagecount++; } else { assert(m->phys_page == vm_page_fictitious_addr); } } if (dwp->dw_mask & DW_clear_busy) dwp->dw_m->busy = FALSE; if (dwp->dw_mask & DW_PAGE_WAKEUP) PAGE_WAKEUP(dwp->dw_m); } vm_page_unlock_queues(); #if CONFIG_EMBEDDED { int percent_avail; /* * Decide if we need to send a memory status notification. */ percent_avail = (vm_page_active_count + vm_page_inactive_count + vm_page_speculative_count + vm_page_free_count + (IP_VALID(memory_manager_default)?0:vm_page_purgeable_count) ) * 100 / atop_64(max_mem); if (percent_avail >= (kern_memorystatus_level + 5) || percent_avail <= (kern_memorystatus_level - 5)) { kern_memorystatus_level = percent_avail; thread_wakeup((event_t)&kern_memorystatus_wakeup); } } #endif } /* * The "chunk" macros are used by routines below when looking for pages to deactivate. These * exist because of the need to handle shadow chains. When deactivating pages, we only * want to deactive the ones at the top most level in the object chain. In order to do * this efficiently, the specified address range is divided up into "chunks" and we use * a bit map to keep track of which pages have already been processed as we descend down * the shadow chain. These chunk macros hide the details of the bit map implementation * as much as we can. * * For convenience, we use a 64-bit data type as the bit map, and therefore a chunk is * set to 64 pages. The bit map is indexed from the low-order end, so that the lowest * order bit represents page 0 in the current range and highest order bit represents * page 63. * * For further convenience, we also use negative logic for the page state in the bit map. * The bit is set to 1 to indicate it has not yet been seen, and to 0 to indicate it has * been processed. This way we can simply test the 64-bit long word to see if it's zero * to easily tell if the whole range has been processed. Therefore, the bit map starts * out with all the bits set. The macros below hide all these details from the caller. */ #define PAGES_IN_A_CHUNK 64 /* The number of pages in the chunk must */ /* be the same as the number of bits in */ /* the chunk_state_t type. We use 64 */ /* just for convenience. */ #define CHUNK_SIZE (PAGES_IN_A_CHUNK * PAGE_SIZE_64) /* Size of a chunk in bytes */ typedef uint64_t chunk_state_t; /* * The bit map uses negative logic, so we start out with all 64 bits set to indicate * that no pages have been processed yet. Also, if len is less than the full CHUNK_SIZE, * then we mark pages beyond the len as having been "processed" so that we don't waste time * looking at pages in that range. This can save us from unnecessarily chasing down the * shadow chain. */ #define CHUNK_INIT(c, len) \ MACRO_BEGIN \ uint64_t p; \ \ (c) = 0xffffffffffffffffLL; \ \ for (p = (len) / PAGE_SIZE_64; p < PAGES_IN_A_CHUNK; p++) \ MARK_PAGE_HANDLED(c, p); \ MACRO_END /* * Return true if all pages in the chunk have not yet been processed. */ #define CHUNK_NOT_COMPLETE(c) ((c) != 0) /* * Return true if the page at offset 'p' in the bit map has already been handled * while processing a higher level object in the shadow chain. */ #define PAGE_ALREADY_HANDLED(c, p) (((c) & (1LL << (p))) == 0) /* * Mark the page at offset 'p' in the bit map as having been processed. */ #define MARK_PAGE_HANDLED(c, p) \ MACRO_BEGIN \ (c) = (c) & ~(1LL << (p)); \ MACRO_END /* * Return true if the page at the given offset has been paged out. Object is * locked upon entry and returned locked. */ static boolean_t page_is_paged_out( vm_object_t object, vm_object_offset_t offset) { kern_return_t kr; memory_object_t pager; /* * Check the existence map for the page if we have one, otherwise * ask the pager about this page. */ #if MACH_PAGEMAP if (object->existence_map) { if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_EXISTS) { /* * We found the page */ return TRUE; } } else #endif if (object->internal && object->alive && !object->terminating && object->pager_ready) { /* * We're already holding a "paging in progress" reference * so the object can't disappear when we release the lock. */ assert(object->paging_in_progress); pager = object->pager; vm_object_unlock(object); kr = memory_object_data_request( pager, offset + object->paging_offset, 0, /* just poke the pager */ VM_PROT_READ, NULL); vm_object_lock(object); if (kr == KERN_SUCCESS) { /* * We found the page */ return TRUE; } } return FALSE; } /* * Deactivate the pages in the specified object and range. If kill_page is set, also discard any * page modified state from the pmap. Update the chunk_state as we go along. The caller must specify * a size that is less than or equal to the CHUNK_SIZE. */ static void deactivate_pages_in_object( vm_object_t object, vm_object_offset_t offset, vm_object_size_t size, boolean_t kill_page, boolean_t reusable_page, #if !MACH_ASSERT __unused #endif boolean_t all_reusable, chunk_state_t *chunk_state) { vm_page_t m; int p; struct dw dw_array[DELAYED_WORK_LIMIT]; struct dw *dwp; int dw_count; unsigned int reusable = 0; /* * Examine each page in the chunk. The variable 'p' is the page number relative to the start of the * chunk. Since this routine is called once for each level in the shadow chain, the chunk_state may * have pages marked as having been processed already. We stop the loop early if we find we've handled * all the pages in the chunk. */ dwp = &dw_array[0]; dw_count = 0; for(p = 0; size && CHUNK_NOT_COMPLETE(*chunk_state); p++, size -= PAGE_SIZE_64, offset += PAGE_SIZE_64) { /* * If this offset has already been found and handled in a higher level object, then don't * do anything with it in the current shadow object. */ if (PAGE_ALREADY_HANDLED(*chunk_state, p)) continue; /* * See if the page at this offset is around. First check to see if the page is resident, * then if not, check the existence map or with the pager. */ if ((m = vm_page_lookup(object, offset)) != VM_PAGE_NULL) { /* * We found a page we were looking for. Mark it as "handled" now in the chunk_state * so that we won't bother looking for a page at this offset again if there are more * shadow objects. Then deactivate the page. */ MARK_PAGE_HANDLED(*chunk_state, p); if (( !VM_PAGE_WIRED(m)) && (!m->private) && (!m->gobbled) && (!m->busy)) { int clear_refmod; assert(!m->laundry); clear_refmod = VM_MEM_REFERENCED; dwp->dw_mask = DW_clear_reference; if ((kill_page) && (object->internal)) { m->precious = FALSE; m->dirty = FALSE; clear_refmod |= VM_MEM_MODIFIED; if (m->throttled) { /* * This page is now clean and * reclaimable. Move it out * of the throttled queue, so * that vm_pageout_scan() can * find it. */ dwp->dw_mask |= DW_move_page; } #if MACH_PAGEMAP vm_external_state_clr(object->existence_map, offset); #endif /* MACH_PAGEMAP */ if (reusable_page && !m->reusable) { assert(!all_reusable); assert(!object->all_reusable); m->reusable = TRUE; object->reusable_page_count++; assert(object->resident_page_count >= object->reusable_page_count); reusable++; #if CONFIG_EMBEDDED } else { if (m->reusable) { m->reusable = FALSE; object->reusable_page_count--; } #endif } } pmap_clear_refmod(m->phys_page, clear_refmod); if (!m->throttled && !(reusable_page || all_reusable)) dwp->dw_mask |= DW_move_page; /* * dw_do_work may need to drop the object lock * if it does, we need the pages its looking at to * be held stable via the busy bit. */ m->busy = TRUE; dwp->dw_mask |= (DW_clear_busy | DW_PAGE_WAKEUP); dwp->dw_m = m; dwp++; dw_count++; if (dw_count >= DELAYED_WORK_LIMIT) { if (reusable) { OSAddAtomic(reusable, &vm_page_stats_reusable.reusable_count); vm_page_stats_reusable.reusable += reusable; reusable = 0; } dw_do_work(object, &dw_array[0], dw_count); dwp = &dw_array[0]; dw_count = 0; } } } else { /* * The page at this offset isn't memory resident, check to see if it's * been paged out. If so, mark it as handled so we don't bother looking * for it in the shadow chain. */ if (page_is_paged_out(object, offset)) { MARK_PAGE_HANDLED(*chunk_state, p); /* * If we're killing a non-resident page, then clear the page in the existence * map so we don't bother paging it back in if it's touched again in the future. */ if ((kill_page) && (object->internal)) { #if MACH_PAGEMAP vm_external_state_clr(object->existence_map, offset); #endif /* MACH_PAGEMAP */ } } } } if (reusable) { OSAddAtomic(reusable, &vm_page_stats_reusable.reusable_count); vm_page_stats_reusable.reusable += reusable; reusable = 0; } if (dw_count) dw_do_work(object, &dw_array[0], dw_count); } /* * Deactive a "chunk" of the given range of the object starting at offset. A "chunk" * will always be less than or equal to the given size. The total range is divided up * into chunks for efficiency and performance related to the locks and handling the shadow * chain. This routine returns how much of the given "size" it actually processed. It's * up to the caler to loop and keep calling this routine until the entire range they want * to process has been done. */ static vm_object_size_t deactivate_a_chunk( vm_object_t orig_object, vm_object_offset_t offset, vm_object_size_t size, boolean_t kill_page, boolean_t reusable_page, boolean_t all_reusable) { vm_object_t object; vm_object_t tmp_object; vm_object_size_t length; chunk_state_t chunk_state; /* * Get set to do a chunk. We'll do up to CHUNK_SIZE, but no more than the * remaining size the caller asked for. */ length = MIN(size, CHUNK_SIZE); /* * The chunk_state keeps track of which pages we've already processed if there's * a shadow chain on this object. At this point, we haven't done anything with this * range of pages yet, so initialize the state to indicate no pages processed yet. */ CHUNK_INIT(chunk_state, length); object = orig_object; /* * Start at the top level object and iterate around the loop once for each object * in the shadow chain. We stop processing early if we've already found all the pages * in the range. Otherwise we stop when we run out of shadow objects. */ while (object && CHUNK_NOT_COMPLETE(chunk_state)) { vm_object_paging_begin(object); deactivate_pages_in_object(object, offset, length, kill_page, reusable_page, all_reusable, &chunk_state); vm_object_paging_end(object); /* * We've finished with this object, see if there's a shadow object. If * there is, update the offset and lock the new object. We also turn off * kill_page at this point since we only kill pages in the top most object. */ tmp_object = object->shadow; if (tmp_object) { kill_page = FALSE; reusable_page = FALSE; all_reusable = FALSE; offset += object->shadow_offset; vm_object_lock(tmp_object); } if (object != orig_object) vm_object_unlock(object); object = tmp_object; } if (object && object != orig_object) vm_object_unlock(object); return length; } /* * Move any resident pages in the specified range to the inactive queue. If kill_page is set, * we also clear the modified status of the page and "forget" any changes that have been made * to the page. */ __private_extern__ void vm_object_deactivate_pages( vm_object_t object, vm_object_offset_t offset, vm_object_size_t size, boolean_t kill_page, boolean_t reusable_page) { vm_object_size_t length; boolean_t all_reusable; /* * We break the range up into chunks and do one chunk at a time. This is for * efficiency and performance while handling the shadow chains and the locks. * The deactivate_a_chunk() function returns how much of the range it processed. * We keep calling this routine until the given size is exhausted. */ all_reusable = FALSE; if (reusable_page && object->size != 0 && object->size == size && object->reusable_page_count == 0) { all_reusable = TRUE; reusable_page = FALSE; } #if CONFIG_EMBEDDED if ((reusable_page || all_reusable) && object->all_reusable) { /* This means MADV_FREE_REUSABLE has been called twice, which * is probably illegal. */ return; } #endif while (size) { length = deactivate_a_chunk(object, offset, size, kill_page, reusable_page, all_reusable); size -= length; offset += length; } if (all_reusable) { if (!object->all_reusable) { unsigned int reusable; object->all_reusable = TRUE; assert(object->reusable_page_count == 0); /* update global stats */ reusable = object->resident_page_count; OSAddAtomic(reusable, &vm_page_stats_reusable.reusable_count); vm_page_stats_reusable.reusable += reusable; vm_page_stats_reusable.all_reusable_calls++; } } else if (reusable_page) { vm_page_stats_reusable.partial_reusable_calls++; } } void vm_object_reuse_pages( vm_object_t object, vm_object_offset_t start_offset, vm_object_offset_t end_offset, boolean_t allow_partial_reuse) { vm_object_offset_t cur_offset; vm_page_t m; unsigned int reused, reusable; #define VM_OBJECT_REUSE_PAGE(object, m, reused) \ MACRO_BEGIN \ if ((m) != VM_PAGE_NULL && \ (m)->reusable) { \ assert((object)->reusable_page_count <= \ (object)->resident_page_count); \ assert((object)->reusable_page_count > 0); \ (object)->reusable_page_count--; \ (m)->reusable = FALSE; \ (reused)++; \ } \ MACRO_END reused = 0; reusable = 0; vm_object_lock_assert_exclusive(object); if (object->all_reusable) { assert(object->reusable_page_count == 0); object->all_reusable = FALSE; if (end_offset - start_offset == object->size || !allow_partial_reuse) { vm_page_stats_reusable.all_reuse_calls++; reused = object->resident_page_count; } else { vm_page_stats_reusable.partial_reuse_calls++; queue_iterate(&object->memq, m, vm_page_t, listq) { if (m->offset < start_offset || m->offset >= end_offset) { m->reusable = TRUE; object->reusable_page_count++; assert(object->resident_page_count >= object->reusable_page_count); continue; } else { assert(!m->reusable); reused++; } } } } else if (object->resident_page_count > ((end_offset - start_offset) >> PAGE_SHIFT)) { vm_page_stats_reusable.partial_reuse_calls++; for (cur_offset = start_offset; cur_offset < end_offset; cur_offset += PAGE_SIZE_64) { if (object->reusable_page_count == 0) { break; } m = vm_page_lookup(object, cur_offset); VM_OBJECT_REUSE_PAGE(object, m, reused); } } else { vm_page_stats_reusable.partial_reuse_calls++; queue_iterate(&object->memq, m, vm_page_t, listq) { if (object->reusable_page_count == 0) { break; } if (m->offset < start_offset || m->offset >= end_offset) { continue; } VM_OBJECT_REUSE_PAGE(object, m, reused); } } /* update global stats */ OSAddAtomic(reusable-reused, &vm_page_stats_reusable.reusable_count); vm_page_stats_reusable.reused += reused; vm_page_stats_reusable.reusable += reusable; } /* * Routine: vm_object_pmap_protect * * Purpose: * Reduces the permission for all physical * pages in the specified object range. * * If removing write permission only, it is * sufficient to protect only the pages in * the top-level object; only those pages may * have write permission. * * If removing all access, we must follow the * shadow chain from the top-level object to * remove access to all pages in shadowed objects. * * The object must *not* be locked. The object must * be temporary/internal. * * If pmap is not NULL, this routine assumes that * the only mappings for the pages are in that * pmap. */ __private_extern__ void vm_object_pmap_protect( register vm_object_t object, register vm_object_offset_t offset, vm_object_size_t size, pmap_t pmap, vm_map_offset_t pmap_start, vm_prot_t prot) { if (object == VM_OBJECT_NULL) return; size = vm_object_round_page(size); offset = vm_object_trunc_page(offset); vm_object_lock(object); if (object->phys_contiguous) { if (pmap != NULL) { vm_object_unlock(object); pmap_protect(pmap, pmap_start, pmap_start + size, prot); } else { vm_object_offset_t phys_start, phys_end, phys_addr; phys_start = object->shadow_offset + offset; phys_end = phys_start + size; assert(phys_start <= phys_end); assert(phys_end <= object->shadow_offset + object->size); vm_object_unlock(object); for (phys_addr = phys_start; phys_addr < phys_end; phys_addr += PAGE_SIZE_64) { pmap_page_protect((ppnum_t) (phys_addr >> PAGE_SHIFT), prot); } } return; } assert(object->internal); while (TRUE) { if (ptoa_64(object->resident_page_count) > size/2 && pmap != PMAP_NULL) { vm_object_unlock(object); pmap_protect(pmap, pmap_start, pmap_start + size, prot); return; } /* if we are doing large ranges with respect to resident */ /* page count then we should interate over pages otherwise */ /* inverse page look-up will be faster */ if (ptoa_64(object->resident_page_count / 4) < size) { vm_page_t p; vm_object_offset_t end; end = offset + size; if (pmap != PMAP_NULL) { queue_iterate(&object->memq, p, vm_page_t, listq) { if (!p->fictitious && (offset <= p->offset) && (p->offset < end)) { vm_map_offset_t start; start = pmap_start + p->offset - offset; pmap_protect(pmap, start, start + PAGE_SIZE_64, prot); } } } else { queue_iterate(&object->memq, p, vm_page_t, listq) { if (!p->fictitious && (offset <= p->offset) && (p->offset < end)) { pmap_page_protect(p->phys_page, prot); } } } } else { vm_page_t p; vm_object_offset_t end; vm_object_offset_t target_off; end = offset + size; if (pmap != PMAP_NULL) { for(target_off = offset; target_off < end; target_off += PAGE_SIZE) { p = vm_page_lookup(object, target_off); if (p != VM_PAGE_NULL) { vm_object_offset_t start; start = pmap_start + (p->offset - offset); pmap_protect(pmap, start, start + PAGE_SIZE, prot); } } } else { for(target_off = offset; target_off < end; target_off += PAGE_SIZE) { p = vm_page_lookup(object, target_off); if (p != VM_PAGE_NULL) { pmap_page_protect(p->phys_page, prot); } } } } if (prot == VM_PROT_NONE) { /* * Must follow shadow chain to remove access * to pages in shadowed objects. */ register vm_object_t next_object; next_object = object->shadow; if (next_object != VM_OBJECT_NULL) { offset += object->shadow_offset; vm_object_lock(next_object); vm_object_unlock(object); object = next_object; } else { /* * End of chain - we are done. */ break; } } else { /* * Pages in shadowed objects may never have * write permission - we may stop here. */ break; } } vm_object_unlock(object); } /* * Routine: vm_object_copy_slowly * * Description: * Copy the specified range of the source * virtual memory object without using * protection-based optimizations (such * as copy-on-write). The pages in the * region are actually copied. * * In/out conditions: * The caller must hold a reference and a lock * for the source virtual memory object. The source * object will be returned *unlocked*. * * Results: * If the copy is completed successfully, KERN_SUCCESS is * returned. If the caller asserted the interruptible * argument, and an interruption occurred while waiting * for a user-generated event, MACH_SEND_INTERRUPTED is * returned. Other values may be returned to indicate * hard errors during the copy operation. * * A new virtual memory object is returned in a * parameter (_result_object). The contents of this * new object, starting at a zero offset, are a copy * of the source memory region. In the event of * an error, this parameter will contain the value * VM_OBJECT_NULL. */ __private_extern__ kern_return_t vm_object_copy_slowly( register vm_object_t src_object, vm_object_offset_t src_offset, vm_object_size_t size, boolean_t interruptible, vm_object_t *_result_object) /* OUT */ { vm_object_t new_object; vm_object_offset_t new_offset; struct vm_object_fault_info fault_info; XPR(XPR_VM_OBJECT, "v_o_c_slowly obj 0x%x off 0x%x size 0x%x\n", src_object, src_offset, size, 0, 0); if (size == 0) { vm_object_unlock(src_object); *_result_object = VM_OBJECT_NULL; return(KERN_INVALID_ARGUMENT); } /* * Prevent destruction of the source object while we copy. */ vm_object_reference_locked(src_object); vm_object_unlock(src_object); /* * Create a new object to hold the copied pages. * A few notes: * We fill the new object starting at offset 0, * regardless of the input offset. * We don't bother to lock the new object within * this routine, since we have the only reference. */ new_object = vm_object_allocate(size); new_offset = 0; assert(size == trunc_page_64(size)); /* Will the loop terminate? */ fault_info.interruptible = interruptible; fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL; fault_info.user_tag = 0; fault_info.lo_offset = src_offset; fault_info.hi_offset = src_offset + size; fault_info.no_cache = FALSE; fault_info.stealth = TRUE; for ( ; size != 0 ; src_offset += PAGE_SIZE_64, new_offset += PAGE_SIZE_64, size -= PAGE_SIZE_64 ) { vm_page_t new_page; vm_fault_return_t result; vm_object_lock(new_object); while ((new_page = vm_page_alloc(new_object, new_offset)) == VM_PAGE_NULL) { vm_object_unlock(new_object); if (!vm_page_wait(interruptible)) { vm_object_deallocate(new_object); vm_object_deallocate(src_object); *_result_object = VM_OBJECT_NULL; return(MACH_SEND_INTERRUPTED); } vm_object_lock(new_object); } vm_object_unlock(new_object); do { vm_prot_t prot = VM_PROT_READ; vm_page_t _result_page; vm_page_t top_page; register vm_page_t result_page; kern_return_t error_code; vm_object_lock(src_object); vm_object_paging_begin(src_object); if (size > (vm_size_t) -1) { /* 32-bit overflow */ fault_info.cluster_size = (vm_size_t) (0 - PAGE_SIZE); } else { fault_info.cluster_size = (vm_size_t) size; assert(fault_info.cluster_size == size); } XPR(XPR_VM_FAULT,"vm_object_copy_slowly -> vm_fault_page",0,0,0,0,0); result = vm_fault_page(src_object, src_offset, VM_PROT_READ, FALSE, &prot, &_result_page, &top_page, (int *)0, &error_code, FALSE, FALSE, &fault_info); switch(result) { case VM_FAULT_SUCCESS: result_page = _result_page; /* * We don't need to hold the object * lock -- the busy page will be enough. * [We don't care about picking up any * new modifications.] * * Copy the page to the new object. * * POLICY DECISION: * If result_page is clean, * we could steal it instead * of copying. */ vm_object_unlock(result_page->object); vm_page_copy(result_page, new_page); /* * Let go of both pages (make them * not busy, perform wakeup, activate). */ vm_object_lock(new_object); new_page->dirty = TRUE; PAGE_WAKEUP_DONE(new_page); vm_object_unlock(new_object); vm_object_lock(result_page->object); PAGE_WAKEUP_DONE(result_page); vm_page_lockspin_queues(); if (!result_page->active && !result_page->inactive && !result_page->throttled) vm_page_activate(result_page); vm_page_activate(new_page); vm_page_unlock_queues(); /* * Release paging references and * top-level placeholder page, if any. */ vm_fault_cleanup(result_page->object, top_page); break; case VM_FAULT_RETRY: break; case VM_FAULT_FICTITIOUS_SHORTAGE: vm_page_more_fictitious(); break; case VM_FAULT_MEMORY_SHORTAGE: if (vm_page_wait(interruptible)) break; /* fall thru */ case VM_FAULT_INTERRUPTED: vm_object_lock(new_object); VM_PAGE_FREE(new_page); vm_object_unlock(new_object); vm_object_deallocate(new_object); vm_object_deallocate(src_object); *_result_object = VM_OBJECT_NULL; return(MACH_SEND_INTERRUPTED); case VM_FAULT_SUCCESS_NO_VM_PAGE: /* success but no VM page: fail */ vm_object_paging_end(src_object); vm_object_unlock(src_object); /*FALLTHROUGH*/ case VM_FAULT_MEMORY_ERROR: /* * A policy choice: * (a) ignore pages that we can't * copy * (b) return the null object if * any page fails [chosen] */ vm_object_lock(new_object); VM_PAGE_FREE(new_page); vm_object_unlock(new_object); vm_object_deallocate(new_object); vm_object_deallocate(src_object); *_result_object = VM_OBJECT_NULL; return(error_code ? error_code: KERN_MEMORY_ERROR); default: panic("vm_object_copy_slowly: unexpected error" " 0x%x from vm_fault_page()\n", result); } } while (result != VM_FAULT_SUCCESS); } /* * Lose the extra reference, and return our object. */ vm_object_deallocate(src_object); *_result_object = new_object; return(KERN_SUCCESS); } /* * Routine: vm_object_copy_quickly * * Purpose: * Copy the specified range of the source virtual * memory object, if it can be done without waiting * for user-generated events. * * Results: * If the copy is successful, the copy is returned in * the arguments; otherwise, the arguments are not * affected. * * In/out conditions: * The object should be unlocked on entry and exit. */ /*ARGSUSED*/ __private_extern__ boolean_t vm_object_copy_quickly( vm_object_t *_object, /* INOUT */ __unused vm_object_offset_t offset, /* IN */ __unused vm_object_size_t size, /* IN */ boolean_t *_src_needs_copy, /* OUT */ boolean_t *_dst_needs_copy) /* OUT */ { vm_object_t object = *_object; memory_object_copy_strategy_t copy_strategy; XPR(XPR_VM_OBJECT, "v_o_c_quickly obj 0x%x off 0x%x size 0x%x\n", *_object, offset, size, 0, 0); if (object == VM_OBJECT_NULL) { *_src_needs_copy = FALSE; *_dst_needs_copy = FALSE; return(TRUE); } vm_object_lock(object); copy_strategy = object->copy_strategy; switch (copy_strategy) { case MEMORY_OBJECT_COPY_SYMMETRIC: /* * Symmetric copy strategy. * Make another reference to the object. * Leave object/offset unchanged. */ vm_object_reference_locked(object); object->shadowed = TRUE; vm_object_unlock(object); /* * Both source and destination must make * shadows, and the source must be made * read-only if not already. */ *_src_needs_copy = TRUE; *_dst_needs_copy = TRUE; break; case MEMORY_OBJECT_COPY_DELAY: vm_object_unlock(object); return(FALSE); default: vm_object_unlock(object); return(FALSE); } return(TRUE); } static int copy_call_count = 0; static int copy_call_sleep_count = 0; static int copy_call_restart_count = 0; /* * Routine: vm_object_copy_call [internal] * * Description: * Copy the source object (src_object), using the * user-managed copy algorithm. * * In/out conditions: * The source object must be locked on entry. It * will be *unlocked* on exit. * * Results: * If the copy is successful, KERN_SUCCESS is returned. * A new object that represents the copied virtual * memory is returned in a parameter (*_result_object). * If the return value indicates an error, this parameter * is not valid. */ static kern_return_t vm_object_copy_call( vm_object_t src_object, vm_object_offset_t src_offset, vm_object_size_t size, vm_object_t *_result_object) /* OUT */ { kern_return_t kr; vm_object_t copy; boolean_t check_ready = FALSE; uint32_t try_failed_count = 0; /* * If a copy is already in progress, wait and retry. * * XXX * Consider making this call interruptable, as Mike * intended it to be. * * XXXO * Need a counter or version or something to allow * us to use the copy that the currently requesting * thread is obtaining -- is it worth adding to the * vm object structure? Depends how common this case it. */ copy_call_count++; while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) { vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL, THREAD_UNINT); copy_call_restart_count++; } /* * Indicate (for the benefit of memory_object_create_copy) * that we want a copy for src_object. (Note that we cannot * do a real assert_wait before calling memory_object_copy, * so we simply set the flag.) */ vm_object_set_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL); vm_object_unlock(src_object); /* * Ask the memory manager to give us a memory object * which represents a copy of the src object. * The memory manager may give us a memory object * which we already have, or it may give us a * new memory object. This memory object will arrive * via memory_object_create_copy. */ kr = KERN_FAILURE; /* XXX need to change memory_object.defs */ if (kr != KERN_SUCCESS) { return kr; } /* * Wait for the copy to arrive. */ vm_object_lock(src_object); while (vm_object_wanted(src_object, VM_OBJECT_EVENT_COPY_CALL)) { vm_object_sleep(src_object, VM_OBJECT_EVENT_COPY_CALL, THREAD_UNINT); copy_call_sleep_count++; } Retry: assert(src_object->copy != VM_OBJECT_NULL); copy = src_object->copy; if (!vm_object_lock_try(copy)) { vm_object_unlock(src_object); try_failed_count++; mutex_pause(try_failed_count); /* wait a bit */ vm_object_lock(src_object); goto Retry; } if (copy->size < src_offset+size) copy->size = src_offset+size; if (!copy->pager_ready) check_ready = TRUE; /* * Return the copy. */ *_result_object = copy; vm_object_unlock(copy); vm_object_unlock(src_object); /* Wait for the copy to be ready. */ if (check_ready == TRUE) { vm_object_lock(copy); while (!copy->pager_ready) { vm_object_sleep(copy, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT); } vm_object_unlock(copy); } return KERN_SUCCESS; } static int copy_delayed_lock_collisions = 0; static int copy_delayed_max_collisions = 0; static int copy_delayed_lock_contention = 0; static int copy_delayed_protect_iterate = 0; /* * Routine: vm_object_copy_delayed [internal] * * Description: * Copy the specified virtual memory object, using * the asymmetric copy-on-write algorithm. * * In/out conditions: * The src_object must be locked on entry. It will be unlocked * on exit - so the caller must also hold a reference to it. * * This routine will not block waiting for user-generated * events. It is not interruptible. */ __private_extern__ vm_object_t vm_object_copy_delayed( vm_object_t src_object, vm_object_offset_t src_offset, vm_object_size_t size, boolean_t src_object_shared) { vm_object_t new_copy = VM_OBJECT_NULL; vm_object_t old_copy; vm_page_t p; vm_object_size_t copy_size = src_offset + size; int collisions = 0; /* * The user-level memory manager wants to see all of the changes * to this object, but it has promised not to make any changes on * its own. * * Perform an asymmetric copy-on-write, as follows: * Create a new object, called a "copy object" to hold * pages modified by the new mapping (i.e., the copy, * not the original mapping). * Record the original object as the backing object for * the copy object. If the original mapping does not * change a page, it may be used read-only by the copy. * Record the copy object in the original object. * When the original mapping causes a page to be modified, * it must be copied to a new page that is "pushed" to * the copy object. * Mark the new mapping (the copy object) copy-on-write. * This makes the copy object itself read-only, allowing * it to be reused if the original mapping makes no * changes, and simplifying the synchronization required * in the "push" operation described above. * * The copy-on-write is said to be assymetric because the original * object is *not* marked copy-on-write. A copied page is pushed * to the copy object, regardless which party attempted to modify * the page. * * Repeated asymmetric copy operations may be done. If the * original object has not been changed since the last copy, its * copy object can be reused. Otherwise, a new copy object can be * inserted between the original object and its previous copy * object. Since any copy object is read-only, this cannot affect * affect the contents of the previous copy object. * * Note that a copy object is higher in the object tree than the * original object; therefore, use of the copy object recorded in * the original object must be done carefully, to avoid deadlock. */ Retry: /* * Wait for paging in progress. */ if (!src_object->true_share && (src_object->paging_in_progress != 0 || src_object->activity_in_progress != 0)) { if (src_object_shared == TRUE) { vm_object_unlock(src_object); vm_object_lock(src_object); src_object_shared = FALSE; goto Retry; } vm_object_paging_wait(src_object, THREAD_UNINT); } /* * See whether we can reuse the result of a previous * copy operation. */ old_copy = src_object->copy; if (old_copy != VM_OBJECT_NULL) { int lock_granted; /* * Try to get the locks (out of order) */ if (src_object_shared == TRUE) lock_granted = vm_object_lock_try_shared(old_copy); else lock_granted = vm_object_lock_try(old_copy); if (!lock_granted) { vm_object_unlock(src_object); if (collisions++ == 0) copy_delayed_lock_contention++; mutex_pause(collisions); /* Heisenberg Rules */ copy_delayed_lock_collisions++; if (collisions > copy_delayed_max_collisions) copy_delayed_max_collisions = collisions; if (src_object_shared == TRUE) vm_object_lock_shared(src_object); else vm_object_lock(src_object); goto Retry; } /* * Determine whether the old copy object has * been modified. */ if (old_copy->resident_page_count == 0 && !old_copy->pager_created) { /* * It has not been modified. * * Return another reference to * the existing copy-object if * we can safely grow it (if * needed). */ if (old_copy->size < copy_size) { if (src_object_shared == TRUE) { vm_object_unlock(old_copy); vm_object_unlock(src_object); vm_object_lock(src_object); src_object_shared = FALSE; goto Retry; } /* * We can't perform a delayed copy if any of the * pages in the extended range are wired (because * we can't safely take write permission away from * wired pages). If the pages aren't wired, then * go ahead and protect them. */ copy_delayed_protect_iterate++; queue_iterate(&src_object->memq, p, vm_page_t, listq) { if (!p->fictitious && p->offset >= old_copy->size && p->offset < copy_size) { if (VM_PAGE_WIRED(p)) { vm_object_unlock(old_copy); vm_object_unlock(src_object); if (new_copy != VM_OBJECT_NULL) { vm_object_unlock(new_copy); vm_object_deallocate(new_copy); } return VM_OBJECT_NULL; } else { pmap_page_protect(p->phys_page, (VM_PROT_ALL & ~VM_PROT_WRITE)); } } } old_copy->size = copy_size; } if (src_object_shared == TRUE) vm_object_reference_shared(old_copy); else vm_object_reference_locked(old_copy); vm_object_unlock(old_copy); vm_object_unlock(src_object); if (new_copy != VM_OBJECT_NULL) { vm_object_unlock(new_copy); vm_object_deallocate(new_copy); } return(old_copy); } /* * Adjust the size argument so that the newly-created * copy object will be large enough to back either the * old copy object or the new mapping. */ if (old_copy->size > copy_size) copy_size = old_copy->size; if (new_copy == VM_OBJECT_NULL) { vm_object_unlock(old_copy); vm_object_unlock(src_object); new_copy = vm_object_allocate(copy_size); vm_object_lock(src_object); vm_object_lock(new_copy); src_object_shared = FALSE; goto Retry; } new_copy->size = copy_size; /* * The copy-object is always made large enough to * completely shadow the original object, since * it may have several users who want to shadow * the original object at different points. */ assert((old_copy->shadow == src_object) && (old_copy->shadow_offset == (vm_object_offset_t) 0)); } else if (new_copy == VM_OBJECT_NULL) { vm_object_unlock(src_object); new_copy = vm_object_allocate(copy_size); vm_object_lock(src_object); vm_object_lock(new_copy); src_object_shared = FALSE; goto Retry; } /* * We now have the src object locked, and the new copy object * allocated and locked (and potentially the old copy locked). * Before we go any further, make sure we can still perform * a delayed copy, as the situation may have changed. * * Specifically, we can't perform a delayed copy if any of the * pages in the range are wired (because we can't safely take * write permission away from wired pages). If the pages aren't * wired, then go ahead and protect them. */ copy_delayed_protect_iterate++; queue_iterate(&src_object->memq, p, vm_page_t, listq) { if (!p->fictitious && p->offset < copy_size) { if (VM_PAGE_WIRED(p)) { if (old_copy) vm_object_unlock(old_copy); vm_object_unlock(src_object); vm_object_unlock(new_copy); vm_object_deallocate(new_copy); return VM_OBJECT_NULL; } else { pmap_page_protect(p->phys_page, (VM_PROT_ALL & ~VM_PROT_WRITE)); } } } if (old_copy != VM_OBJECT_NULL) { /* * Make the old copy-object shadow the new one. * It will receive no more pages from the original * object. */ /* remove ref. from old_copy */ vm_object_lock_assert_exclusive(src_object); src_object->ref_count--; assert(src_object->ref_count > 0); vm_object_lock_assert_exclusive(old_copy); old_copy->shadow = new_copy; vm_object_lock_assert_exclusive(new_copy); assert(new_copy->ref_count > 0); new_copy->ref_count++; /* for old_copy->shadow ref. */ #if TASK_SWAPPER if (old_copy->res_count) { VM_OBJ_RES_INCR(new_copy); VM_OBJ_RES_DECR(src_object); } #endif vm_object_unlock(old_copy); /* done with old_copy */ } /* * Point the new copy at the existing object. */ vm_object_lock_assert_exclusive(new_copy); new_copy->shadow = src_object; new_copy->shadow_offset = 0; new_copy->shadowed = TRUE; /* caller must set needs_copy */ vm_object_lock_assert_exclusive(src_object); vm_object_reference_locked(src_object); src_object->copy = new_copy; vm_object_unlock(src_object); vm_object_unlock(new_copy); XPR(XPR_VM_OBJECT, "vm_object_copy_delayed: used copy object %X for source %X\n", new_copy, src_object, 0, 0, 0); return new_copy; } /* * Routine: vm_object_copy_strategically * * Purpose: * Perform a copy according to the source object's * declared strategy. This operation may block, * and may be interrupted. */ __private_extern__ kern_return_t vm_object_copy_strategically( register vm_object_t src_object, vm_object_offset_t src_offset, vm_object_size_t size, vm_object_t *dst_object, /* OUT */ vm_object_offset_t *dst_offset, /* OUT */ boolean_t *dst_needs_copy) /* OUT */ { boolean_t result; boolean_t interruptible = THREAD_ABORTSAFE; /* XXX */ boolean_t object_lock_shared = FALSE; memory_object_copy_strategy_t copy_strategy; assert(src_object != VM_OBJECT_NULL); copy_strategy = src_object->copy_strategy; if (copy_strategy == MEMORY_OBJECT_COPY_DELAY) { vm_object_lock_shared(src_object); object_lock_shared = TRUE; } else vm_object_lock(src_object); /* * The copy strategy is only valid if the memory manager * is "ready". Internal objects are always ready. */ while (!src_object->internal && !src_object->pager_ready) { wait_result_t wait_result; if (object_lock_shared == TRUE) { vm_object_unlock(src_object); vm_object_lock(src_object); object_lock_shared = FALSE; continue; } wait_result = vm_object_sleep( src_object, VM_OBJECT_EVENT_PAGER_READY, interruptible); if (wait_result != THREAD_AWAKENED) { vm_object_unlock(src_object); *dst_object = VM_OBJECT_NULL; *dst_offset = 0; *dst_needs_copy = FALSE; return(MACH_SEND_INTERRUPTED); } } /* * Use the appropriate copy strategy. */ switch (copy_strategy) { case MEMORY_OBJECT_COPY_DELAY: *dst_object = vm_object_copy_delayed(src_object, src_offset, size, object_lock_shared); if (*dst_object != VM_OBJECT_NULL) { *dst_offset = src_offset; *dst_needs_copy = TRUE; result = KERN_SUCCESS; break; } vm_object_lock(src_object); /* fall thru when delayed copy not allowed */ case MEMORY_OBJECT_COPY_NONE: result = vm_object_copy_slowly(src_object, src_offset, size, interruptible, dst_object); if (result == KERN_SUCCESS) { *dst_offset = 0; *dst_needs_copy = FALSE; } break; case MEMORY_OBJECT_COPY_CALL: result = vm_object_copy_call(src_object, src_offset, size, dst_object); if (result == KERN_SUCCESS) { *dst_offset = src_offset; *dst_needs_copy = TRUE; } break; case MEMORY_OBJECT_COPY_SYMMETRIC: XPR(XPR_VM_OBJECT, "v_o_c_strategically obj 0x%x off 0x%x size 0x%x\n", src_object, src_offset, size, 0, 0); vm_object_unlock(src_object); result = KERN_MEMORY_RESTART_COPY; break; default: panic("copy_strategically: bad strategy"); result = KERN_INVALID_ARGUMENT; } return(result); } /* * vm_object_shadow: * * Create a new object which is backed by the * specified existing object range. The source * object reference is deallocated. * * The new object and offset into that object * are returned in the source parameters. */ boolean_t vm_object_shadow_check = FALSE; __private_extern__ boolean_t vm_object_shadow( vm_object_t *object, /* IN/OUT */ vm_object_offset_t *offset, /* IN/OUT */ vm_object_size_t length) { register vm_object_t source; register vm_object_t result; source = *object; #if 0 /* * XXX FBDP * This assertion is valid but it gets triggered by Rosetta for example * due to a combination of vm_remap() that changes a VM object's * copy_strategy from SYMMETRIC to DELAY and vm_protect(VM_PROT_COPY) * that then sets "needs_copy" on its map entry. This creates a * mapping situation that VM should never see and doesn't know how to * handle. * It's not clear if this can create any real problem but we should * look into fixing this, probably by having vm_protect(VM_PROT_COPY) * do more than just set "needs_copy" to handle the copy-on-write... * In the meantime, let's disable the assertion. */ assert(source->copy_strategy == MEMORY_OBJECT_COPY_SYMMETRIC); #endif /* * Determine if we really need a shadow. */ if (vm_object_shadow_check && source->ref_count == 1 && (source->shadow == VM_OBJECT_NULL || source->shadow->copy == VM_OBJECT_NULL)) { source->shadowed = FALSE; return FALSE; } /* * Allocate a new object with the given length */ if ((result = vm_object_allocate(length)) == VM_OBJECT_NULL) panic("vm_object_shadow: no object for shadowing"); /* * The new object shadows the source object, adding * a reference to it. Our caller changes his reference * to point to the new object, removing a reference to * the source object. Net result: no change of reference * count. */ result->shadow = source; /* * Store the offset into the source object, * and fix up the offset into the new object. */ result->shadow_offset = *offset; /* * Return the new things */ *offset = 0; *object = result; return TRUE; } /* * The relationship between vm_object structures and * the memory_object requires careful synchronization. * * All associations are created by memory_object_create_named * for external pagers and vm_object_pager_create for internal * objects as follows: * * pager: the memory_object itself, supplied by * the user requesting a mapping (or the kernel, * when initializing internal objects); the * kernel simulates holding send rights by keeping * a port reference; * * pager_request: * the memory object control port, * created by the kernel; the kernel holds * receive (and ownership) rights to this * port, but no other references. * * When initialization is complete, the "initialized" field * is asserted. Other mappings using a particular memory object, * and any references to the vm_object gained through the * port association must wait for this initialization to occur. * * In order to allow the memory manager to set attributes before * requests (notably virtual copy operations, but also data or * unlock requests) are made, a "ready" attribute is made available. * Only the memory manager may affect the value of this attribute. * Its value does not affect critical kernel functions, such as * internal object initialization or destruction. [Furthermore, * memory objects created by the kernel are assumed to be ready * immediately; the default memory manager need not explicitly * set the "ready" attribute.] * * [Both the "initialized" and "ready" attribute wait conditions * use the "pager" field as the wait event.] * * The port associations can be broken down by any of the * following routines: * vm_object_terminate: * No references to the vm_object remain, and * the object cannot (or will not) be cached. * This is the normal case, and is done even * though one of the other cases has already been * done. * memory_object_destroy: * The memory manager has requested that the * kernel relinquish references to the memory * object. [The memory manager may not want to * destroy the memory object, but may wish to * refuse or tear down existing memory mappings.] * * Each routine that breaks an association must break all of * them at once. At some later time, that routine must clear * the pager field and release the memory object references. * [Furthermore, each routine must cope with the simultaneous * or previous operations of the others.] * * In addition to the lock on the object, the vm_object_hash_lock * governs the associations. References gained through the * association require use of the hash lock. * * Because the pager field may be cleared spontaneously, it * cannot be used to determine whether a memory object has * ever been associated with a particular vm_object. [This * knowledge is important to the shadow object mechanism.] * For this reason, an additional "created" attribute is * provided. * * During various paging operations, the pager reference found in the * vm_object must be valid. To prevent this from being released, * (other than being removed, i.e., made null), routines may use * the vm_object_paging_begin/end routines [actually, macros]. * The implementation uses the "paging_in_progress" and "wanted" fields. * [Operations that alter the validity of the pager values include the * termination routines and vm_object_collapse.] */ /* * Routine: vm_object_enter * Purpose: * Find a VM object corresponding to the given * pager; if no such object exists, create one, * and initialize the pager. */ vm_object_t vm_object_enter( memory_object_t pager, vm_object_size_t size, boolean_t internal, boolean_t init, boolean_t named) { register vm_object_t object; vm_object_t new_object; boolean_t must_init; vm_object_hash_entry_t entry, new_entry; uint32_t try_failed_count = 0; lck_mtx_t *lck; if (pager == MEMORY_OBJECT_NULL) return(vm_object_allocate(size)); new_object = VM_OBJECT_NULL; new_entry = VM_OBJECT_HASH_ENTRY_NULL; must_init = init; /* * Look for an object associated with this port. */ Retry: lck = vm_object_hash_lock_spin(pager); do { entry = vm_object_hash_lookup(pager, FALSE); if (entry == VM_OBJECT_HASH_ENTRY_NULL) { if (new_object == VM_OBJECT_NULL) { /* * We must unlock to create a new object; * if we do so, we must try the lookup again. */ vm_object_hash_unlock(lck); assert(new_entry == VM_OBJECT_HASH_ENTRY_NULL); new_entry = vm_object_hash_entry_alloc(pager); new_object = vm_object_allocate(size); lck = vm_object_hash_lock_spin(pager); } else { /* * Lookup failed twice, and we have something * to insert; set the object. */ vm_object_hash_insert(new_entry, new_object); entry = new_entry; new_entry = VM_OBJECT_HASH_ENTRY_NULL; new_object = VM_OBJECT_NULL; must_init = TRUE; } } else if (entry->object == VM_OBJECT_NULL) { /* * If a previous object is being terminated, * we must wait for the termination message * to be queued (and lookup the entry again). */ entry->waiting = TRUE; entry = VM_OBJECT_HASH_ENTRY_NULL; assert_wait((event_t) pager, THREAD_UNINT); vm_object_hash_unlock(lck); thread_block(THREAD_CONTINUE_NULL); lck = vm_object_hash_lock_spin(pager); } } while (entry == VM_OBJECT_HASH_ENTRY_NULL); object = entry->object; assert(object != VM_OBJECT_NULL); if (!must_init) { if ( !vm_object_lock_try(object)) { vm_object_hash_unlock(lck); try_failed_count++; mutex_pause(try_failed_count); /* wait a bit */ goto Retry; } assert(!internal || object->internal); #if VM_OBJECT_CACHE if (object->ref_count == 0) { if ( !vm_object_cache_lock_try()) { vm_object_hash_unlock(lck); vm_object_unlock(object); try_failed_count++; mutex_pause(try_failed_count); /* wait a bit */ goto Retry; } XPR(XPR_VM_OBJECT_CACHE, "vm_object_enter: removing %x from cache, head (%x, %x)\n", object, vm_object_cached_list.next, vm_object_cached_list.prev, 0,0); queue_remove(&vm_object_cached_list, object, vm_object_t, cached_list); vm_object_cached_count--; vm_object_cache_unlock(); } #endif if (named) { assert(!object->named); object->named = TRUE; } vm_object_lock_assert_exclusive(object); object->ref_count++; vm_object_res_reference(object); vm_object_hash_unlock(lck); vm_object_unlock(object); VM_STAT_INCR(hits); } else vm_object_hash_unlock(lck); assert(object->ref_count > 0); VM_STAT_INCR(lookups); XPR(XPR_VM_OBJECT, "vm_o_enter: pager 0x%x obj 0x%x must_init %d\n", pager, object, must_init, 0, 0); /* * If we raced to create a vm_object but lost, let's * throw away ours. */ if (new_object != VM_OBJECT_NULL) vm_object_deallocate(new_object); if (new_entry != VM_OBJECT_HASH_ENTRY_NULL) vm_object_hash_entry_free(new_entry); if (must_init) { memory_object_control_t control; /* * Allocate request port. */ control = memory_object_control_allocate(object); assert (control != MEMORY_OBJECT_CONTROL_NULL); vm_object_lock(object); assert(object != kernel_object); /* * Copy the reference we were given. */ memory_object_reference(pager); object->pager_created = TRUE; object->pager = pager; object->internal = internal; object->pager_trusted = internal; if (!internal) { /* copy strategy invalid until set by memory manager */ object->copy_strategy = MEMORY_OBJECT_COPY_INVALID; } object->pager_control = control; object->pager_ready = FALSE; vm_object_unlock(object); /* * Let the pager know we're using it. */ (void) memory_object_init(pager, object->pager_control, PAGE_SIZE); vm_object_lock(object); if (named) object->named = TRUE; if (internal) { object->pager_ready = TRUE; vm_object_wakeup(object, VM_OBJECT_EVENT_PAGER_READY); } object->pager_initialized = TRUE; vm_object_wakeup(object, VM_OBJECT_EVENT_INITIALIZED); } else { vm_object_lock(object); } /* * [At this point, the object must be locked] */ /* * Wait for the work above to be done by the first * thread to map this object. */ while (!object->pager_initialized) { vm_object_sleep(object, VM_OBJECT_EVENT_INITIALIZED, THREAD_UNINT); } vm_object_unlock(object); XPR(XPR_VM_OBJECT, "vm_object_enter: vm_object %x, memory_object %x, internal %d\n", object, object->pager, internal, 0,0); return(object); } /* * Routine: vm_object_pager_create * Purpose: * Create a memory object for an internal object. * In/out conditions: * The object is locked on entry and exit; * it may be unlocked within this call. * Limitations: * Only one thread may be performing a * vm_object_pager_create on an object at * a time. Presumably, only the pageout * daemon will be using this routine. */ void vm_object_pager_create( register vm_object_t object) { memory_object_t pager; vm_object_hash_entry_t entry; lck_mtx_t *lck; #if MACH_PAGEMAP vm_object_size_t size; vm_external_map_t map; #endif /* MACH_PAGEMAP */ XPR(XPR_VM_OBJECT, "vm_object_pager_create, object 0x%X\n", object, 0,0,0,0); assert(object != kernel_object); if (memory_manager_default_check() != KERN_SUCCESS) return; /* * Prevent collapse or termination by holding a paging reference */ vm_object_paging_begin(object); if (object->pager_created) { /* * Someone else got to it first... * wait for them to finish initializing the ports */ while (!object->pager_initialized) { vm_object_sleep(object, VM_OBJECT_EVENT_INITIALIZED, THREAD_UNINT); } vm_object_paging_end(object); return; } /* * Indicate that a memory object has been assigned * before dropping the lock, to prevent a race. */ object->pager_created = TRUE; object->paging_offset = 0; #if MACH_PAGEMAP size = object->size; #endif /* MACH_PAGEMAP */ vm_object_unlock(object); #if MACH_PAGEMAP map = vm_external_create(size); vm_object_lock(object); assert(object->size == size); object->existence_map = map; vm_object_unlock(object); #endif /* MACH_PAGEMAP */ if ((uint32_t) object->size != object->size) { panic("vm_object_pager_create(): object size 0x%llx >= 4GB\n", (uint64_t) object->size); } /* * Create the [internal] pager, and associate it with this object. * * We make the association here so that vm_object_enter() * can look up the object to complete initializing it. No * user will ever map this object. */ { memory_object_default_t dmm; /* acquire a reference for the default memory manager */ dmm = memory_manager_default_reference(); assert(object->temporary); /* create our new memory object */ assert((vm_size_t) object->size == object->size); (void) memory_object_create(dmm, (vm_size_t) object->size, &pager); memory_object_default_deallocate(dmm); } entry = vm_object_hash_entry_alloc(pager); lck = vm_object_hash_lock_spin(pager); vm_object_hash_insert(entry, object); vm_object_hash_unlock(lck); /* * A reference was returned by * memory_object_create(), and it is * copied by vm_object_enter(). */ if (vm_object_enter(pager, object->size, TRUE, TRUE, FALSE) != object) panic("vm_object_pager_create: mismatch"); /* * Drop the reference we were passed. */ memory_object_deallocate(pager); vm_object_lock(object); /* * Release the paging reference */ vm_object_paging_end(object); } /* * Routine: vm_object_remove * Purpose: * Eliminate the pager/object association * for this pager. * Conditions: * The object cache must be locked. */ __private_extern__ void vm_object_remove( vm_object_t object) { memory_object_t pager; if ((pager = object->pager) != MEMORY_OBJECT_NULL) { vm_object_hash_entry_t entry; entry = vm_object_hash_lookup(pager, FALSE); if (entry != VM_OBJECT_HASH_ENTRY_NULL) entry->object = VM_OBJECT_NULL; } } /* * Global variables for vm_object_collapse(): * * Counts for normal collapses and bypasses. * Debugging variables, to watch or disable collapse. */ static long object_collapses = 0; static long object_bypasses = 0; static boolean_t vm_object_collapse_allowed = TRUE; static boolean_t vm_object_bypass_allowed = TRUE; #if MACH_PAGEMAP static int vm_external_discarded; static int vm_external_collapsed; #endif unsigned long vm_object_collapse_encrypted = 0; /* * Routine: vm_object_do_collapse * Purpose: * Collapse an object with the object backing it. * Pages in the backing object are moved into the * parent, and the backing object is deallocated. * Conditions: * Both objects and the cache are locked; the page * queues are unlocked. * */ static void vm_object_do_collapse( vm_object_t object, vm_object_t backing_object) { vm_page_t p, pp; vm_object_offset_t new_offset, backing_offset; vm_object_size_t size; vm_object_lock_assert_exclusive(object); vm_object_lock_assert_exclusive(backing_object); backing_offset = object->shadow_offset; size = object->size; /* * Move all in-memory pages from backing_object * to the parent. Pages that have been paged out * will be overwritten by any of the parent's * pages that shadow them. */ while (!queue_empty(&backing_object->memq)) { p = (vm_page_t) queue_first(&backing_object->memq); new_offset = (p->offset - backing_offset); assert(!p->busy || p->absent); /* * If the parent has a page here, or if * this page falls outside the parent, * dispose of it. * * Otherwise, move it as planned. */ if (p->offset < backing_offset || new_offset >= size) { VM_PAGE_FREE(p); } else { /* * ENCRYPTED SWAP: * The encryption key includes the "pager" and the * "paging_offset". These will not change during the * object collapse, so we can just move an encrypted * page from one object to the other in this case. * We can't decrypt the page here, since we can't drop * the object lock. */ if (p->encrypted) { vm_object_collapse_encrypted++; } pp = vm_page_lookup(object, new_offset); if (pp == VM_PAGE_NULL) { /* * Parent now has no page. * Move the backing object's page up. */ vm_page_rename(p, object, new_offset, TRUE); #if MACH_PAGEMAP } else if (pp->absent) { /* * Parent has an absent page... * it's not being paged in, so * it must really be missing from * the parent. * * Throw out the absent page... * any faults looking for that * page will restart with the new * one. */ VM_PAGE_FREE(pp); vm_page_rename(p, object, new_offset, TRUE); #endif /* MACH_PAGEMAP */ } else { assert(! pp->absent); /* * Parent object has a real page. * Throw away the backing object's * page. */ VM_PAGE_FREE(p); } } } #if !MACH_PAGEMAP assert((!object->pager_created && (object->pager == MEMORY_OBJECT_NULL)) || (!backing_object->pager_created && (backing_object->pager == MEMORY_OBJECT_NULL))); #else assert(!object->pager_created && object->pager == MEMORY_OBJECT_NULL); #endif /* !MACH_PAGEMAP */ if (backing_object->pager != MEMORY_OBJECT_NULL) { vm_object_hash_entry_t entry; /* * Move the pager from backing_object to object. * * XXX We're only using part of the paging space * for keeps now... we ought to discard the * unused portion. */ assert(!object->paging_in_progress); assert(!object->activity_in_progress); object->pager = backing_object->pager; if (backing_object->hashed) { lck_mtx_t *lck; lck = vm_object_hash_lock_spin(backing_object->pager); entry = vm_object_hash_lookup(object->pager, FALSE); assert(entry != VM_OBJECT_HASH_ENTRY_NULL); entry->object = object; vm_object_hash_unlock(lck); object->hashed = TRUE; } object->pager_created = backing_object->pager_created; object->pager_control = backing_object->pager_control; object->pager_ready = backing_object->pager_ready; object->pager_initialized = backing_object->pager_initialized; object->paging_offset = backing_object->paging_offset + backing_offset; if (object->pager_control != MEMORY_OBJECT_CONTROL_NULL) { memory_object_control_collapse(object->pager_control, object); } } #if MACH_PAGEMAP /* * If the shadow offset is 0, the use the existence map from * the backing object if there is one. If the shadow offset is * not zero, toss it. * * XXX - If the shadow offset is not 0 then a bit copy is needed * if the map is to be salvaged. For now, we just just toss the * old map, giving the collapsed object no map. This means that * the pager is invoked for zero fill pages. If analysis shows * that this happens frequently and is a performance hit, then * this code should be fixed to salvage the map. */ assert(object->existence_map == VM_EXTERNAL_NULL); if (backing_offset || (size != backing_object->size)) { vm_external_discarded++; vm_external_destroy(backing_object->existence_map, backing_object->size); } else { vm_external_collapsed++; object->existence_map = backing_object->existence_map; } backing_object->existence_map = VM_EXTERNAL_NULL; #endif /* MACH_PAGEMAP */ /* * Object now shadows whatever backing_object did. * Note that the reference to backing_object->shadow * moves from within backing_object to within object. */ assert(!object->phys_contiguous); assert(!backing_object->phys_contiguous); object->shadow = backing_object->shadow; if (object->shadow) { object->shadow_offset += backing_object->shadow_offset; } else { /* no shadow, therefore no shadow offset... */ object->shadow_offset = 0; } assert((object->shadow == VM_OBJECT_NULL) || (object->shadow->copy != backing_object)); /* * Discard backing_object. * * Since the backing object has no pages, no * pager left, and no object references within it, * all that is necessary is to dispose of it. */ assert((backing_object->ref_count == 1) && (backing_object->resident_page_count == 0) && (backing_object->paging_in_progress == 0) && (backing_object->activity_in_progress == 0)); backing_object->alive = FALSE; vm_object_unlock(backing_object); XPR(XPR_VM_OBJECT, "vm_object_collapse, collapsed 0x%X\n", backing_object, 0,0,0,0); vm_object_lock_destroy(backing_object); zfree(vm_object_zone, backing_object); object_collapses++; } static void vm_object_do_bypass( vm_object_t object, vm_object_t backing_object) { /* * Make the parent shadow the next object * in the chain. */ vm_object_lock_assert_exclusive(object); vm_object_lock_assert_exclusive(backing_object); #if TASK_SWAPPER /* * Do object reference in-line to * conditionally increment shadow's * residence count. If object is not * resident, leave residence count * on shadow alone. */ if (backing_object->shadow != VM_OBJECT_NULL) { vm_object_lock(backing_object->shadow); vm_object_lock_assert_exclusive(backing_object->shadow); backing_object->shadow->ref_count++; if (object->res_count != 0) vm_object_res_reference(backing_object->shadow); vm_object_unlock(backing_object->shadow); } #else /* TASK_SWAPPER */ vm_object_reference(backing_object->shadow); #endif /* TASK_SWAPPER */ assert(!object->phys_contiguous); assert(!backing_object->phys_contiguous); object->shadow = backing_object->shadow; if (object->shadow) { object->shadow_offset += backing_object->shadow_offset; } else { /* no shadow, therefore no shadow offset... */ object->shadow_offset = 0; } /* * Backing object might have had a copy pointer * to us. If it did, clear it. */ if (backing_object->copy == object) { backing_object->copy = VM_OBJECT_NULL; } /* * Drop the reference count on backing_object. #if TASK_SWAPPER * Since its ref_count was at least 2, it * will not vanish; so we don't need to call * vm_object_deallocate. * [with a caveat for "named" objects] * * The res_count on the backing object is * conditionally decremented. It's possible * (via vm_pageout_scan) to get here with * a "swapped" object, which has a 0 res_count, * in which case, the backing object res_count * is already down by one. #else * Don't call vm_object_deallocate unless * ref_count drops to zero. * * The ref_count can drop to zero here if the * backing object could be bypassed but not * collapsed, such as when the backing object * is temporary and cachable. #endif */ if (backing_object->ref_count > 2 || (!backing_object->named && backing_object->ref_count > 1)) { vm_object_lock_assert_exclusive(backing_object); backing_object->ref_count--; #if TASK_SWAPPER if (object->res_count != 0) vm_object_res_deallocate(backing_object); assert(backing_object->ref_count > 0); #endif /* TASK_SWAPPER */ vm_object_unlock(backing_object); } else { /* * Drop locks so that we can deallocate * the backing object. */ #if TASK_SWAPPER if (object->res_count == 0) { /* XXX get a reference for the deallocate below */ vm_object_res_reference(backing_object); } #endif /* TASK_SWAPPER */ vm_object_unlock(object); vm_object_unlock(backing_object); vm_object_deallocate(backing_object); /* * Relock object. We don't have to reverify * its state since vm_object_collapse will * do that for us as it starts at the * top of its loop. */ vm_object_lock(object); } object_bypasses++; } /* * vm_object_collapse: * * Perform an object collapse or an object bypass if appropriate. * The real work of collapsing and bypassing is performed in * the routines vm_object_do_collapse and vm_object_do_bypass. * * Requires that the object be locked and the page queues be unlocked. * */ static unsigned long vm_object_collapse_calls = 0; static unsigned long vm_object_collapse_objects = 0; static unsigned long vm_object_collapse_do_collapse = 0; static unsigned long vm_object_collapse_do_bypass = 0; static unsigned long vm_object_collapse_delays = 0; __private_extern__ void vm_object_collapse( register vm_object_t object, register vm_object_offset_t hint_offset, boolean_t can_bypass) { register vm_object_t backing_object; register unsigned int rcount; register unsigned int size; vm_object_t original_object; int object_lock_type; int backing_object_lock_type; vm_object_collapse_calls++; if (! vm_object_collapse_allowed && ! (can_bypass && vm_object_bypass_allowed)) { return; } XPR(XPR_VM_OBJECT, "vm_object_collapse, obj 0x%X\n", object, 0,0,0,0); if (object == VM_OBJECT_NULL) return; original_object = object; /* * The top object was locked "exclusive" by the caller. * In the first pass, to determine if we can collapse the shadow chain, * take a "shared" lock on the shadow objects. If we can collapse, * we'll have to go down the chain again with exclusive locks. */ object_lock_type = OBJECT_LOCK_EXCLUSIVE; backing_object_lock_type = OBJECT_LOCK_SHARED; retry: object = original_object; vm_object_lock_assert_exclusive(object); while (TRUE) { vm_object_collapse_objects++; /* * Verify that the conditions are right for either * collapse or bypass: */ /* * There is a backing object, and */ backing_object = object->shadow; if (backing_object == VM_OBJECT_NULL) { if (object != original_object) { vm_object_unlock(object); } return; } if (backing_object_lock_type == OBJECT_LOCK_SHARED) { vm_object_lock_shared(backing_object); } else { vm_object_lock(backing_object); } /* * No pages in the object are currently * being paged out, and */ if (object->paging_in_progress != 0 || object->activity_in_progress != 0) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * ... * The backing object is not read_only, * and no pages in the backing object are * currently being paged out. * The backing object is internal. * */ if (!backing_object->internal || backing_object->paging_in_progress != 0 || backing_object->activity_in_progress != 0) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * The backing object can't be a copy-object: * the shadow_offset for the copy-object must stay * as 0. Furthermore (for the 'we have all the * pages' case), if we bypass backing_object and * just shadow the next object in the chain, old * pages from that object would then have to be copied * BOTH into the (former) backing_object and into the * parent object. */ if (backing_object->shadow != VM_OBJECT_NULL && backing_object->shadow->copy == backing_object) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * We can now try to either collapse the backing * object (if the parent is the only reference to * it) or (perhaps) remove the parent's reference * to it. * * If there is exactly one reference to the backing * object, we may be able to collapse it into the * parent. * * If MACH_PAGEMAP is defined: * The parent must not have a pager created for it, * since collapsing a backing_object dumps new pages * into the parent that its pager doesn't know about * (and the collapse code can't merge the existence * maps). * Otherwise: * As long as one of the objects is still not known * to the pager, we can collapse them. */ if (backing_object->ref_count == 1 && (!object->pager_created #if !MACH_PAGEMAP || !backing_object->pager_created #endif /*!MACH_PAGEMAP */ ) && vm_object_collapse_allowed) { /* * We need the exclusive lock on the VM objects. */ if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) { /* * We have an object and its shadow locked * "shared". We can't just upgrade the locks * to "exclusive", as some other thread might * also have these objects locked "shared" and * attempt to upgrade one or the other to * "exclusive". The upgrades would block * forever waiting for the other "shared" locks * to get released. * So we have to release the locks and go * down the shadow chain again (since it could * have changed) with "exclusive" locking. */ vm_object_unlock(backing_object); if (object != original_object) vm_object_unlock(object); object_lock_type = OBJECT_LOCK_EXCLUSIVE; backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE; goto retry; } XPR(XPR_VM_OBJECT, "vm_object_collapse: %x to %x, pager %x, pager_control %x\n", backing_object, object, backing_object->pager, backing_object->pager_control, 0); /* * Collapse the object with its backing * object, and try again with the object's * new backing object. */ vm_object_do_collapse(object, backing_object); vm_object_collapse_do_collapse++; continue; } /* * Collapsing the backing object was not possible * or permitted, so let's try bypassing it. */ if (! (can_bypass && vm_object_bypass_allowed)) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * If the object doesn't have all its pages present, * we have to make sure no pages in the backing object * "show through" before bypassing it. */ size = atop(object->size); rcount = object->resident_page_count; if (rcount != size) { vm_object_offset_t offset; vm_object_offset_t backing_offset; unsigned int backing_rcount; unsigned int lookups = 0; /* * If the backing object has a pager but no pagemap, * then we cannot bypass it, because we don't know * what pages it has. */ if (backing_object->pager_created #if MACH_PAGEMAP && (backing_object->existence_map == VM_EXTERNAL_NULL) #endif /* MACH_PAGEMAP */ ) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * If the object has a pager but no pagemap, * then we cannot bypass it, because we don't know * what pages it has. */ if (object->pager_created #if MACH_PAGEMAP && (object->existence_map == VM_EXTERNAL_NULL) #endif /* MACH_PAGEMAP */ ) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * If all of the pages in the backing object are * shadowed by the parent object, the parent * object no longer has to shadow the backing * object; it can shadow the next one in the * chain. * * If the backing object has existence info, * we must check examine its existence info * as well. * */ backing_offset = object->shadow_offset; backing_rcount = backing_object->resident_page_count; #if MACH_PAGEMAP #define EXISTS_IN_OBJECT(obj, off, rc) \ (vm_external_state_get((obj)->existence_map, \ (vm_offset_t)(off)) == VM_EXTERNAL_STATE_EXISTS || \ ((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--)) #else #define EXISTS_IN_OBJECT(obj, off, rc) \ (((rc) && ++lookups && vm_page_lookup((obj), (off)) != VM_PAGE_NULL && (rc)--)) #endif /* MACH_PAGEMAP */ /* * Check the hint location first * (since it is often the quickest way out of here). */ if (object->cow_hint != ~(vm_offset_t)0) hint_offset = (vm_object_offset_t)object->cow_hint; else hint_offset = (hint_offset > 8 * PAGE_SIZE_64) ? (hint_offset - 8 * PAGE_SIZE_64) : 0; if (EXISTS_IN_OBJECT(backing_object, hint_offset + backing_offset, backing_rcount) && !EXISTS_IN_OBJECT(object, hint_offset, rcount)) { /* dependency right at the hint */ object->cow_hint = (vm_offset_t) hint_offset; /* atomic */ /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } /* * If the object's window onto the backing_object * is large compared to the number of resident * pages in the backing object, it makes sense to * walk the backing_object's resident pages first. * * NOTE: Pages may be in both the existence map and * resident. So, we can't permanently decrement * the rcount here because the second loop may * find the same pages in the backing object' * existence map that we found here and we would * double-decrement the rcount. We also may or * may not have found the */ if (backing_rcount && #if MACH_PAGEMAP size > ((backing_object->existence_map) ? backing_rcount : (backing_rcount >> 1)) #else size > (backing_rcount >> 1) #endif /* MACH_PAGEMAP */ ) { unsigned int rc = rcount; vm_page_t p; backing_rcount = backing_object->resident_page_count; p = (vm_page_t)queue_first(&backing_object->memq); do { /* Until we get more than one lookup lock */ if (lookups > 256) { vm_object_collapse_delays++; lookups = 0; mutex_pause(0); } offset = (p->offset - backing_offset); if (offset < object->size && offset != hint_offset && !EXISTS_IN_OBJECT(object, offset, rc)) { /* found a dependency */ object->cow_hint = (vm_offset_t) offset; /* atomic */ break; } p = (vm_page_t) queue_next(&p->listq); } while (--backing_rcount); if (backing_rcount != 0 ) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } } /* * Walk through the offsets looking for pages in the * backing object that show through to the object. */ if (backing_rcount #if MACH_PAGEMAP || backing_object->existence_map #endif /* MACH_PAGEMAP */ ) { offset = hint_offset; while((offset = (offset + PAGE_SIZE_64 < object->size) ? (offset + PAGE_SIZE_64) : 0) != hint_offset) { /* Until we get more than one lookup lock */ if (lookups > 256) { vm_object_collapse_delays++; lookups = 0; mutex_pause(0); } if (EXISTS_IN_OBJECT(backing_object, offset + backing_offset, backing_rcount) && !EXISTS_IN_OBJECT(object, offset, rcount)) { /* found a dependency */ object->cow_hint = (vm_offset_t) offset; /* atomic */ break; } } if (offset != hint_offset) { /* try and collapse the rest of the shadow chain */ if (object != original_object) { vm_object_unlock(object); } object = backing_object; object_lock_type = backing_object_lock_type; continue; } } } /* * We need "exclusive" locks on the 2 VM objects. */ if (backing_object_lock_type != OBJECT_LOCK_EXCLUSIVE) { vm_object_unlock(backing_object); if (object != original_object) vm_object_unlock(object); object_lock_type = OBJECT_LOCK_EXCLUSIVE; backing_object_lock_type = OBJECT_LOCK_EXCLUSIVE; goto retry; } /* reset the offset hint for any objects deeper in the chain */ object->cow_hint = (vm_offset_t)0; /* * All interesting pages in the backing object * already live in the parent or its pager. * Thus we can bypass the backing object. */ vm_object_do_bypass(object, backing_object); vm_object_collapse_do_bypass++; /* * Try again with this object's new backing object. */ continue; } if (object != original_object) { vm_object_unlock(object); } } /* * Routine: vm_object_page_remove: [internal] * Purpose: * Removes all physical pages in the specified * object range from the object's list of pages. * * In/out conditions: * The object must be locked. * The object must not have paging_in_progress, usually * guaranteed by not having a pager. */ unsigned int vm_object_page_remove_lookup = 0; unsigned int vm_object_page_remove_iterate = 0; __private_extern__ void vm_object_page_remove( register vm_object_t object, register vm_object_offset_t start, register vm_object_offset_t end) { register vm_page_t p, next; /* * One and two page removals are most popular. * The factor of 16 here is somewhat arbitrary. * It balances vm_object_lookup vs iteration. */ if (atop_64(end - start) < (unsigned)object->resident_page_count/16) { vm_object_page_remove_lookup++; for (; start < end; start += PAGE_SIZE_64) { p = vm_page_lookup(object, start); if (p != VM_PAGE_NULL) { assert(!p->cleaning && !p->pageout); if (!p->fictitious && p->pmapped) pmap_disconnect(p->phys_page); VM_PAGE_FREE(p); } } } else { vm_object_page_remove_iterate++; p = (vm_page_t) queue_first(&object->memq); while (!queue_end(&object->memq, (queue_entry_t) p)) { next = (vm_page_t) queue_next(&p->listq); if ((start <= p->offset) && (p->offset < end)) { assert(!p->cleaning && !p->pageout); if (!p->fictitious && p->pmapped) pmap_disconnect(p->phys_page); VM_PAGE_FREE(p); } p = next; } } } /* * Routine: vm_object_coalesce * Function: Coalesces two objects backing up adjoining * regions of memory into a single object. * * returns TRUE if objects were combined. * * NOTE: Only works at the moment if the second object is NULL - * if it's not, which object do we lock first? * * Parameters: * prev_object First object to coalesce * prev_offset Offset into prev_object * next_object Second object into coalesce * next_offset Offset into next_object * * prev_size Size of reference to prev_object * next_size Size of reference to next_object * * Conditions: * The object(s) must *not* be locked. The map must be locked * to preserve the reference to the object(s). */ static int vm_object_coalesce_count = 0; __private_extern__ boolean_t vm_object_coalesce( register vm_object_t prev_object, vm_object_t next_object, vm_object_offset_t prev_offset, __unused vm_object_offset_t next_offset, vm_object_size_t prev_size, vm_object_size_t next_size) { vm_object_size_t newsize; #ifdef lint next_offset++; #endif /* lint */ if (next_object != VM_OBJECT_NULL) { return(FALSE); } if (prev_object == VM_OBJECT_NULL) { return(TRUE); } XPR(XPR_VM_OBJECT, "vm_object_coalesce: 0x%X prev_off 0x%X prev_size 0x%X next_size 0x%X\n", prev_object, prev_offset, prev_size, next_size, 0); vm_object_lock(prev_object); /* * Try to collapse the object first */ vm_object_collapse(prev_object, prev_offset, TRUE); /* * Can't coalesce if pages not mapped to * prev_entry may be in use any way: * . more than one reference * . paged out * . shadows another object * . has a copy elsewhere * . is purgeable * . paging references (pages might be in page-list) */ if ((prev_object->ref_count > 1) || prev_object->pager_created || (prev_object->shadow != VM_OBJECT_NULL) || (prev_object->copy != VM_OBJECT_NULL) || (prev_object->true_share != FALSE) || (prev_object->purgable != VM_PURGABLE_DENY) || (prev_object->paging_in_progress != 0) || (prev_object->activity_in_progress != 0)) { vm_object_unlock(prev_object); return(FALSE); } vm_object_coalesce_count++; /* * Remove any pages that may still be in the object from * a previous deallocation. */ vm_object_page_remove(prev_object, prev_offset + prev_size, prev_offset + prev_size + next_size); /* * Extend the object if necessary. */ newsize = prev_offset + prev_size + next_size; if (newsize > prev_object->size) { #if MACH_PAGEMAP /* * We cannot extend an object that has existence info, * since the existence info might then fail to cover * the entire object. * * This assertion must be true because the object * has no pager, and we only create existence info * for objects with pagers. */ assert(prev_object->existence_map == VM_EXTERNAL_NULL); #endif /* MACH_PAGEMAP */ prev_object->size = newsize; } vm_object_unlock(prev_object); return(TRUE); } /* * Attach a set of physical pages to an object, so that they can * be mapped by mapping the object. Typically used to map IO memory. * * The mapping function and its private data are used to obtain the * physical addresses for each page to be mapped. */ void vm_object_page_map( vm_object_t object, vm_object_offset_t offset, vm_object_size_t size, vm_object_offset_t (*map_fn)(void *map_fn_data, vm_object_offset_t offset), void *map_fn_data) /* private to map_fn */ { int64_t num_pages; int i; vm_page_t m; vm_page_t old_page; vm_object_offset_t addr; num_pages = atop_64(size); for (i = 0; i < num_pages; i++, offset += PAGE_SIZE_64) { addr = (*map_fn)(map_fn_data, offset); while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL) vm_page_more_fictitious(); vm_object_lock(object); if ((old_page = vm_page_lookup(object, offset)) != VM_PAGE_NULL) { VM_PAGE_FREE(old_page); } assert((ppnum_t) addr == addr); vm_page_init(m, (ppnum_t) addr); /* * private normally requires lock_queues but since we * are initializing the page, its not necessary here */ m->private = TRUE; /* don`t free page */ m->wire_count = 1; vm_page_insert(m, object, offset); PAGE_WAKEUP_DONE(m); vm_object_unlock(object); } } #include <mach_kdb.h> #if MACH_KDB #include <ddb/db_output.h> #include <vm/vm_print.h> #define printf kdbprintf extern boolean_t vm_object_cached( vm_object_t object); extern void print_bitstring( char byte); boolean_t vm_object_print_pages = FALSE; void print_bitstring( char byte) { printf("%c%c%c%c%c%c%c%c", ((byte & (1 << 0)) ? '1' : '0'), ((byte & (1 << 1)) ? '1' : '0'), ((byte & (1 << 2)) ? '1' : '0'), ((byte & (1 << 3)) ? '1' : '0'), ((byte & (1 << 4)) ? '1' : '0'), ((byte & (1 << 5)) ? '1' : '0'), ((byte & (1 << 6)) ? '1' : '0'), ((byte & (1 << 7)) ? '1' : '0')); } boolean_t vm_object_cached( __unused register vm_object_t object) { #if VM_OBJECT_CACHE register vm_object_t o; queue_iterate(&vm_object_cached_list, o, vm_object_t, cached_list) { if (object == o) { return TRUE; } } #endif return FALSE; } #if MACH_PAGEMAP /* * vm_external_print: [ debug ] */ void vm_external_print( vm_external_map_t emap, vm_object_size_t size) { if (emap == VM_EXTERNAL_NULL) { printf("0 "); } else { vm_object_size_t existence_size = stob(size); printf("{ size=%lld, map=[", (uint64_t) existence_size); if (existence_size > 0) { print_bitstring(emap[0]); } if (existence_size > 1) { print_bitstring(emap[1]); } if (existence_size > 2) { printf("..."); print_bitstring(emap[existence_size-1]); } printf("] }\n"); } return; } #endif /* MACH_PAGEMAP */ int vm_follow_object( vm_object_t object) { int count = 0; int orig_db_indent = db_indent; while (TRUE) { if (object == VM_OBJECT_NULL) { db_indent = orig_db_indent; return count; } count += 1; iprintf("object 0x%x", object); printf(", shadow=0x%x", object->shadow); printf(", copy=0x%x", object->copy); printf(", pager=0x%x", object->pager); printf(", ref=%d\n", object->ref_count); db_indent += 2; object = object->shadow; } } /* * vm_object_print: [ debug ] */ void vm_object_print(db_expr_t db_addr, __unused boolean_t have_addr, __unused db_expr_t arg_count, __unused char *modif) { vm_object_t object; register vm_page_t p; const char *s; register int count; object = (vm_object_t) (long) db_addr; if (object == VM_OBJECT_NULL) return; iprintf("object 0x%x\n", object); db_indent += 2; iprintf("size=0x%x", object->size); printf(", memq_hint=%p", object->memq_hint); printf(", ref_count=%d\n", object->ref_count); iprintf(""); #if TASK_SWAPPER printf("res_count=%d, ", object->res_count); #endif /* TASK_SWAPPER */ printf("resident_page_count=%d\n", object->resident_page_count); iprintf("shadow=0x%x", object->shadow); if (object->shadow) { register int i = 0; vm_object_t shadow = object; while((shadow = shadow->shadow)) i++; printf(" (depth %d)", i); } printf(", copy=0x%x", object->copy); printf(", shadow_offset=0x%x", object->shadow_offset); printf(", last_alloc=0x%x\n", object->last_alloc); iprintf("pager=0x%x", object->pager); printf(", paging_offset=0x%x", object->paging_offset); printf(", pager_control=0x%x\n", object->pager_control); iprintf("copy_strategy=%d[", object->copy_strategy); switch (object->copy_strategy) { case MEMORY_OBJECT_COPY_NONE: printf("copy_none"); break; case MEMORY_OBJECT_COPY_CALL: printf("copy_call"); break; case MEMORY_OBJECT_COPY_DELAY: printf("copy_delay"); break; case MEMORY_OBJECT_COPY_SYMMETRIC: printf("copy_symmetric"); break; case MEMORY_OBJECT_COPY_INVALID: printf("copy_invalid"); break; default: printf("?"); } printf("]"); iprintf("all_wanted=0x%x<", object->all_wanted); s = ""; if (vm_object_wanted(object, VM_OBJECT_EVENT_INITIALIZED)) { printf("%sinit", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGER_READY)) { printf("%sready", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS)) { printf("%spaging", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_LOCK_IN_PROGRESS)) { printf("%slock", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_UNCACHING)) { printf("%suncaching", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_COPY_CALL)) { printf("%scopy_call", s); s = ","; } if (vm_object_wanted(object, VM_OBJECT_EVENT_CACHING)) { printf("%scaching", s); s = ","; } printf(">"); printf(", paging_in_progress=%d\n", object->paging_in_progress); printf(", activity_in_progress=%d\n", object->activity_in_progress); iprintf("%screated, %sinit, %sready, %spersist, %strusted, %spageout, %s, %s\n", (object->pager_created ? "" : "!"), (object->pager_initialized ? "" : "!"), (object->pager_ready ? "" : "!"), (object->can_persist ? "" : "!"), (object->pager_trusted ? "" : "!"), (object->pageout ? "" : "!"), (object->internal ? "internal" : "external"), (object->temporary ? "temporary" : "permanent")); iprintf("%salive, %spurgeable, %spurgeable_volatile, %spurgeable_empty, %sshadowed, %scached, %sprivate\n", (object->alive ? "" : "!"), ((object->purgable != VM_PURGABLE_DENY) ? "" : "!"), ((object->purgable == VM_PURGABLE_VOLATILE) ? "" : "!"), ((object->purgable == VM_PURGABLE_EMPTY) ? "" : "!"), (object->shadowed ? "" : "!"), (vm_object_cached(object) ? "" : "!"), (object->private ? "" : "!")); iprintf("%sadvisory_pageout, %ssilent_overwrite\n", (object->advisory_pageout ? "" : "!"), (object->silent_overwrite ? "" : "!")); #if MACH_PAGEMAP iprintf("existence_map="); vm_external_print(object->existence_map, object->size); #endif /* MACH_PAGEMAP */ #if MACH_ASSERT iprintf("paging_object=0x%x\n", object->paging_object); #endif /* MACH_ASSERT */ if (vm_object_print_pages) { count = 0; p = (vm_page_t) queue_first(&object->memq); while (!queue_end(&object->memq, (queue_entry_t) p)) { if (count == 0) { iprintf("memory:="); } else if (count == 2) { printf("\n"); iprintf(" ..."); count = 0; } else { printf(","); } count++; printf("(off=0x%llX,page=%p)", p->offset, p); p = (vm_page_t) queue_next(&p->listq); } if (count != 0) { printf("\n"); } } db_indent -= 2; } /* * vm_object_find [ debug ] * * Find all tasks which reference the given vm_object. */ boolean_t vm_object_find(vm_object_t object); boolean_t vm_object_print_verbose = FALSE; boolean_t vm_object_find( vm_object_t object) { task_t task; vm_map_t map; vm_map_entry_t entry; boolean_t found = FALSE; queue_iterate(&tasks, task, task_t, tasks) { map = task->map; for (entry = vm_map_first_entry(map); entry && entry != vm_map_to_entry(map); entry = entry->vme_next) { vm_object_t obj; /* * For the time being skip submaps, * only the kernel can have submaps, * and unless we are interested in * kernel objects, we can simply skip * submaps. See sb/dejan/nmk18b7/src/mach_kernel/vm * for a full solution. */ if (entry->is_sub_map) continue; if (entry) obj = entry->object.vm_object; else continue; while (obj != VM_OBJECT_NULL) { if (obj == object) { if (!found) { printf("TASK\t\tMAP\t\tENTRY\n"); found = TRUE; } printf("0x%x\t0x%x\t0x%x\n", task, map, entry); } obj = obj->shadow; } } } return(found); } #endif /* MACH_KDB */ kern_return_t vm_object_populate_with_private( vm_object_t object, vm_object_offset_t offset, ppnum_t phys_page, vm_size_t size) { ppnum_t base_page; vm_object_offset_t base_offset; if(!object->private) return KERN_FAILURE; base_page = phys_page; vm_object_lock(object); if(!object->phys_contiguous) { vm_page_t m; if((base_offset = trunc_page_64(offset)) != offset) { vm_object_unlock(object); return KERN_FAILURE; } base_offset += object->paging_offset; while(size) { m = vm_page_lookup(object, base_offset); if(m != VM_PAGE_NULL) { if(m->fictitious) { if (m->phys_page != vm_page_guard_addr) { vm_page_lockspin_queues(); m->private = TRUE; vm_page_unlock_queues(); m->fictitious = FALSE; m->phys_page = base_page; if(!m->busy) { m->busy = TRUE; } if(!m->absent) { m->absent = TRUE; } m->list_req_pending = TRUE; } } else if (m->phys_page != base_page) { if (m->pmapped) { /* * pmap call to clear old mapping */ pmap_disconnect(m->phys_page); } m->phys_page = base_page; } /* * ENCRYPTED SWAP: * We're not pointing to the same * physical page any longer and the * contents of the new one are not * supposed to be encrypted. * XXX What happens to the original * physical page. Is it lost ? */ m->encrypted = FALSE; } else { while ((m = vm_page_grab_fictitious()) == VM_PAGE_NULL) vm_page_more_fictitious(); /* * private normally requires lock_queues but since we * are initializing the page, its not necessary here */ m->private = TRUE; m->fictitious = FALSE; m->phys_page = base_page; m->list_req_pending = TRUE; m->absent = TRUE; m->unusual = TRUE; vm_page_insert(m, object, base_offset); } base_page++; /* Go to the next physical page */ base_offset += PAGE_SIZE; size -= PAGE_SIZE; } } else { /* NOTE: we should check the original settings here */ /* if we have a size > zero a pmap call should be made */ /* to disable the range */ /* pmap_? */ /* shadows on contiguous memory are not allowed */ /* we therefore can use the offset field */ object->shadow_offset = (vm_object_offset_t)phys_page << PAGE_SHIFT; object->size = size; } vm_object_unlock(object); return KERN_SUCCESS; } /* * memory_object_free_from_cache: * * Walk the vm_object cache list, removing and freeing vm_objects * which are backed by the pager identified by the caller, (pager_ops). * Remove up to "count" objects, if there are that may available * in the cache. * * Walk the list at most once, return the number of vm_objects * actually freed. */ __private_extern__ kern_return_t memory_object_free_from_cache( __unused host_t host, __unused memory_object_pager_ops_t pager_ops, int *count) { #if VM_OBJECT_CACHE int object_released = 0; register vm_object_t object = VM_OBJECT_NULL; vm_object_t shadow; /* if(host == HOST_NULL) return(KERN_INVALID_ARGUMENT); */ try_again: vm_object_cache_lock(); queue_iterate(&vm_object_cached_list, object, vm_object_t, cached_list) { if (object->pager && (pager_ops == object->pager->mo_pager_ops)) { vm_object_lock(object); queue_remove(&vm_object_cached_list, object, vm_object_t, cached_list); vm_object_cached_count--; vm_object_cache_unlock(); /* * Since this object is in the cache, we know * that it is initialized and has only a pager's * (implicit) reference. Take a reference to avoid * recursive deallocations. */ assert(object->pager_initialized); assert(object->ref_count == 0); vm_object_lock_assert_exclusive(object); object->ref_count++; /* * Terminate the object. * If the object had a shadow, we let * vm_object_deallocate deallocate it. * "pageout" objects have a shadow, but * maintain a "paging reference" rather * than a normal reference. * (We are careful here to limit recursion.) */ shadow = object->pageout?VM_OBJECT_NULL:object->shadow; if ((vm_object_terminate(object) == KERN_SUCCESS) && (shadow != VM_OBJECT_NULL)) { vm_object_deallocate(shadow); } if(object_released++ == *count) return KERN_SUCCESS; goto try_again; } } vm_object_cache_unlock(); *count = object_released; #else *count = 0; #endif return KERN_SUCCESS; } kern_return_t memory_object_create_named( memory_object_t pager, memory_object_offset_t size, memory_object_control_t *control) { vm_object_t object; vm_object_hash_entry_t entry; lck_mtx_t *lck; *control = MEMORY_OBJECT_CONTROL_NULL; if (pager == MEMORY_OBJECT_NULL) return KERN_INVALID_ARGUMENT; lck = vm_object_hash_lock_spin(pager); entry = vm_object_hash_lookup(pager, FALSE); if ((entry != VM_OBJECT_HASH_ENTRY_NULL) && (entry->object != VM_OBJECT_NULL)) { if (entry->object->named == TRUE) panic("memory_object_create_named: caller already holds the right"); } vm_object_hash_unlock(lck); if ((object = vm_object_enter(pager, size, FALSE, FALSE, TRUE)) == VM_OBJECT_NULL) { return(KERN_INVALID_OBJECT); } /* wait for object (if any) to be ready */ if (object != VM_OBJECT_NULL) { vm_object_lock(object); object->named = TRUE; while (!object->pager_ready) { vm_object_sleep(object, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT); } *control = object->pager_control; vm_object_unlock(object); } return (KERN_SUCCESS); } /* * Routine: memory_object_recover_named [user interface] * Purpose: * Attempt to recover a named reference for a VM object. * VM will verify that the object has not already started * down the termination path, and if it has, will optionally * wait for that to finish. * Returns: * KERN_SUCCESS - we recovered a named reference on the object * KERN_FAILURE - we could not recover a reference (object dead) * KERN_INVALID_ARGUMENT - bad memory object control */ kern_return_t memory_object_recover_named( memory_object_control_t control, boolean_t wait_on_terminating) { vm_object_t object; object = memory_object_control_to_vm_object(control); if (object == VM_OBJECT_NULL) { return (KERN_INVALID_ARGUMENT); } restart: vm_object_lock(object); if (object->terminating && wait_on_terminating) { vm_object_wait(object, VM_OBJECT_EVENT_PAGING_IN_PROGRESS, THREAD_UNINT); goto restart; } if (!object->alive) { vm_object_unlock(object); return KERN_FAILURE; } if (object->named == TRUE) { vm_object_unlock(object); return KERN_SUCCESS; } #if VM_OBJECT_CACHE if ((object->ref_count == 0) && (!object->terminating)) { if (!vm_object_cache_lock_try()) { vm_object_unlock(object); goto restart; } queue_remove(&vm_object_cached_list, object, vm_object_t, cached_list); vm_object_cached_count--; XPR(XPR_VM_OBJECT_CACHE, "memory_object_recover_named: removing %X, head (%X, %X)\n", object, vm_object_cached_list.next, vm_object_cached_list.prev, 0,0); vm_object_cache_unlock(); } #endif object->named = TRUE; vm_object_lock_assert_exclusive(object); object->ref_count++; vm_object_res_reference(object); while (!object->pager_ready) { vm_object_sleep(object, VM_OBJECT_EVENT_PAGER_READY, THREAD_UNINT); } vm_object_unlock(object); return (KERN_SUCCESS); } /* * vm_object_release_name: * * Enforces name semantic on memory_object reference count decrement * This routine should not be called unless the caller holds a name * reference gained through the memory_object_create_named. * * If the TERMINATE_IDLE flag is set, the call will return if the * reference count is not 1. i.e. idle with the only remaining reference * being the name. * If the decision is made to proceed the name field flag is set to * false and the reference count is decremented. If the RESPECT_CACHE * flag is set and the reference count has gone to zero, the * memory_object is checked to see if it is cacheable otherwise when * the reference count is zero, it is simply terminated. */ __private_extern__ kern_return_t vm_object_release_name( vm_object_t object, int flags) { vm_object_t shadow; boolean_t original_object = TRUE; while (object != VM_OBJECT_NULL) { vm_object_lock(object); assert(object->alive); if (original_object) assert(object->named); assert(object->ref_count > 0); /* * We have to wait for initialization before * destroying or caching the object. */ if (object->pager_created && !object->pager_initialized) { assert(!object->can_persist); vm_object_assert_wait(object, VM_OBJECT_EVENT_INITIALIZED, THREAD_UNINT); vm_object_unlock(object); thread_block(THREAD_CONTINUE_NULL); continue; } if (((object->ref_count > 1) && (flags & MEMORY_OBJECT_TERMINATE_IDLE)) || (object->terminating)) { vm_object_unlock(object); return KERN_FAILURE; } else { if (flags & MEMORY_OBJECT_RELEASE_NO_OP) { vm_object_unlock(object); return KERN_SUCCESS; } } if ((flags & MEMORY_OBJECT_RESPECT_CACHE) && (object->ref_count == 1)) { if (original_object) object->named = FALSE; vm_object_unlock(object); /* let vm_object_deallocate push this thing into */ /* the cache, if that it is where it is bound */ vm_object_deallocate(object); return KERN_SUCCESS; } VM_OBJ_RES_DECR(object); shadow = object->pageout?VM_OBJECT_NULL:object->shadow; if (object->ref_count == 1) { if (vm_object_terminate(object) != KERN_SUCCESS) { if (original_object) { return KERN_FAILURE; } else { return KERN_SUCCESS; } } if (shadow != VM_OBJECT_NULL) { original_object = FALSE; object = shadow; continue; } return KERN_SUCCESS; } else { vm_object_lock_assert_exclusive(object); object->ref_count--; assert(object->ref_count > 0); if(original_object) object->named = FALSE; vm_object_unlock(object); return KERN_SUCCESS; } } /*NOTREACHED*/ assert(0); return KERN_FAILURE; } __private_extern__ kern_return_t vm_object_lock_request( vm_object_t object, vm_object_offset_t offset, vm_object_size_t size, memory_object_return_t should_return, int flags, vm_prot_t prot) { __unused boolean_t should_flush; should_flush = flags & MEMORY_OBJECT_DATA_FLUSH; XPR(XPR_MEMORY_OBJECT, "vm_o_lock_request, obj 0x%X off 0x%X size 0x%X flags %X prot %X\n", object, offset, size, (((should_return&1)<<1)|should_flush), prot); /* * Check for bogus arguments. */ if (object == VM_OBJECT_NULL) return (KERN_INVALID_ARGUMENT); if ((prot & ~VM_PROT_ALL) != 0 && prot != VM_PROT_NO_CHANGE) return (KERN_INVALID_ARGUMENT); size = round_page_64(size); /* * Lock the object, and acquire a paging reference to * prevent the memory_object reference from being released. */ vm_object_lock(object); vm_object_paging_begin(object); (void)vm_object_update(object, offset, size, NULL, NULL, should_return, flags, prot); vm_object_paging_end(object); vm_object_unlock(object); return (KERN_SUCCESS); } /* * Empty a purgeable object by grabbing the physical pages assigned to it and * putting them on the free queue without writing them to backing store, etc. * When the pages are next touched they will be demand zero-fill pages. We * skip pages which are busy, being paged in/out, wired, etc. We do _not_ * skip referenced/dirty pages, pages on the active queue, etc. We're more * than happy to grab these since this is a purgeable object. We mark the * object as "empty" after reaping its pages. * * On entry the object must be locked and it must be * purgeable with no delayed copies pending. */ void vm_object_purge(vm_object_t object) { vm_object_lock_assert_exclusive(object); if (object->purgable == VM_PURGABLE_DENY) return; assert(object->copy == VM_OBJECT_NULL); assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE); if(object->purgable == VM_PURGABLE_VOLATILE) { unsigned int delta; assert(object->resident_page_count >= object->wired_page_count); delta = (object->resident_page_count - object->wired_page_count); if (delta != 0) { assert(vm_page_purgeable_count >= delta); OSAddAtomic(-delta, (SInt32 *)&vm_page_purgeable_count); } if (object->wired_page_count != 0) { assert(vm_page_purgeable_wired_count >= object->wired_page_count); OSAddAtomic(-object->wired_page_count, (SInt32 *)&vm_page_purgeable_wired_count); } } object->purgable = VM_PURGABLE_EMPTY; vm_object_reap_pages(object, REAP_PURGEABLE); } /* * vm_object_purgeable_control() allows the caller to control and investigate the * state of a purgeable object. A purgeable object is created via a call to * vm_allocate() with VM_FLAGS_PURGABLE specified. A purgeable object will * never be coalesced with any other object -- even other purgeable objects -- * and will thus always remain a distinct object. A purgeable object has * special semantics when its reference count is exactly 1. If its reference * count is greater than 1, then a purgeable object will behave like a normal * object and attempts to use this interface will result in an error return * of KERN_INVALID_ARGUMENT. * * A purgeable object may be put into a "volatile" state which will make the * object's pages elligable for being reclaimed without paging to backing * store if the system runs low on memory. If the pages in a volatile * purgeable object are reclaimed, the purgeable object is said to have been * "emptied." When a purgeable object is emptied the system will reclaim as * many pages from the object as it can in a convenient manner (pages already * en route to backing store or busy for other reasons are left as is). When * a purgeable object is made volatile, its pages will generally be reclaimed * before other pages in the application's working set. This semantic is * generally used by applications which can recreate the data in the object * faster than it can be paged in. One such example might be media assets * which can be reread from a much faster RAID volume. * * A purgeable object may be designated as "non-volatile" which means it will * behave like all other objects in the system with pages being written to and * read from backing store as needed to satisfy system memory needs. If the * object was emptied before the object was made non-volatile, that fact will * be returned as the old state of the purgeable object (see * VM_PURGABLE_SET_STATE below). In this case, any pages of the object which * were reclaimed as part of emptying the object will be refaulted in as * zero-fill on demand. It is up to the application to note that an object * was emptied and recreate the objects contents if necessary. When a * purgeable object is made non-volatile, its pages will generally not be paged * out to backing store in the immediate future. A purgeable object may also * be manually emptied. * * Finally, the current state (non-volatile, volatile, volatile & empty) of a * volatile purgeable object may be queried at any time. This information may * be used as a control input to let the application know when the system is * experiencing memory pressure and is reclaiming memory. * * The specified address may be any address within the purgeable object. If * the specified address does not represent any object in the target task's * virtual address space, then KERN_INVALID_ADDRESS will be returned. If the * object containing the specified address is not a purgeable object, then * KERN_INVALID_ARGUMENT will be returned. Otherwise, KERN_SUCCESS will be * returned. * * The control parameter may be any one of VM_PURGABLE_SET_STATE or * VM_PURGABLE_GET_STATE. For VM_PURGABLE_SET_STATE, the in/out parameter * state is used to set the new state of the purgeable object and return its * old state. For VM_PURGABLE_GET_STATE, the current state of the purgeable * object is returned in the parameter state. * * The in/out parameter state may be one of VM_PURGABLE_NONVOLATILE, * VM_PURGABLE_VOLATILE or VM_PURGABLE_EMPTY. These, respectively, represent * the non-volatile, volatile and volatile/empty states described above. * Setting the state of a purgeable object to VM_PURGABLE_EMPTY will * immediately reclaim as many pages in the object as can be conveniently * collected (some may have already been written to backing store or be * otherwise busy). * * The process of making a purgeable object non-volatile and determining its * previous state is atomic. Thus, if a purgeable object is made * VM_PURGABLE_NONVOLATILE and the old state is returned as * VM_PURGABLE_VOLATILE, then the purgeable object's previous contents are * completely intact and will remain so until the object is made volatile * again. If the old state is returned as VM_PURGABLE_EMPTY then the object * was reclaimed while it was in a volatile state and its previous contents * have been lost. */ /* * The object must be locked. */ kern_return_t vm_object_purgable_control( vm_object_t object, vm_purgable_t control, int *state) { int old_state; int new_state; if (object == VM_OBJECT_NULL) { /* * Object must already be present or it can't be purgeable. */ return KERN_INVALID_ARGUMENT; } /* * Get current state of the purgeable object. */ old_state = object->purgable; if (old_state == VM_PURGABLE_DENY) return KERN_INVALID_ARGUMENT; /* purgeable cant have delayed copies - now or in the future */ assert(object->copy == VM_OBJECT_NULL); assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE); /* * Execute the desired operation. */ if (control == VM_PURGABLE_GET_STATE) { *state = old_state; return KERN_SUCCESS; } if ((*state) & VM_PURGABLE_DEBUG_EMPTY) { object->volatile_empty = TRUE; } if ((*state) & VM_PURGABLE_DEBUG_FAULT) { object->volatile_fault = TRUE; } new_state = *state & VM_PURGABLE_STATE_MASK; if (new_state == VM_PURGABLE_VOLATILE && object->volatile_empty) { new_state = VM_PURGABLE_EMPTY; } switch (new_state) { case VM_PURGABLE_DENY: case VM_PURGABLE_NONVOLATILE: object->purgable = new_state; if (old_state == VM_PURGABLE_VOLATILE) { unsigned int delta; assert(object->resident_page_count >= object->wired_page_count); delta = (object->resident_page_count - object->wired_page_count); assert(vm_page_purgeable_count >= delta); if (delta != 0) { OSAddAtomic(-delta, (SInt32 *)&vm_page_purgeable_count); } if (object->wired_page_count != 0) { assert(vm_page_purgeable_wired_count >= object->wired_page_count); OSAddAtomic(-object->wired_page_count, (SInt32 *)&vm_page_purgeable_wired_count); } vm_page_lock_queues(); assert(object->objq.next != NULL && object->objq.prev != NULL); /* object should be on a queue */ purgeable_q_t queue = vm_purgeable_object_remove(object); assert(queue); vm_purgeable_token_delete_first(queue); assert(queue->debug_count_objects>=0); vm_page_unlock_queues(); } break; case VM_PURGABLE_VOLATILE: if (object->volatile_fault) { vm_page_t p; int refmod; queue_iterate(&object->memq, p, vm_page_t, listq) { if (p->busy || VM_PAGE_WIRED(p) || p->fictitious) { continue; } refmod = pmap_disconnect(p->phys_page); if ((refmod & VM_MEM_MODIFIED) && !p->dirty) { p->dirty = TRUE; } } } if (old_state == VM_PURGABLE_EMPTY && object->resident_page_count == 0) break; purgeable_q_t queue; /* find the correct queue */ if ((*state&VM_PURGABLE_ORDERING_MASK) == VM_PURGABLE_ORDERING_OBSOLETE) queue = &purgeable_queues[PURGEABLE_Q_TYPE_OBSOLETE]; else { if ((*state&VM_PURGABLE_BEHAVIOR_MASK) == VM_PURGABLE_BEHAVIOR_FIFO) queue = &purgeable_queues[PURGEABLE_Q_TYPE_FIFO]; else queue = &purgeable_queues[PURGEABLE_Q_TYPE_LIFO]; } if (old_state == VM_PURGABLE_NONVOLATILE || old_state == VM_PURGABLE_EMPTY) { unsigned int delta; /* try to add token... this can fail */ vm_page_lock_queues(); kern_return_t result = vm_purgeable_token_add(queue); if (result != KERN_SUCCESS) { vm_page_unlock_queues(); return result; } vm_page_unlock_queues(); assert(object->resident_page_count >= object->wired_page_count); delta = (object->resident_page_count - object->wired_page_count); if (delta != 0) { OSAddAtomic(delta, &vm_page_purgeable_count); } if (object->wired_page_count != 0) { OSAddAtomic(object->wired_page_count, &vm_page_purgeable_wired_count); } object->purgable = new_state; /* object should not be on a queue */ assert(object->objq.next == NULL && object->objq.prev == NULL); } else if (old_state == VM_PURGABLE_VOLATILE) { /* * if reassigning priorities / purgeable groups, we don't change the * token queue. So moving priorities will not make pages stay around longer. * Reasoning is that the algorithm gives most priority to the most important * object. If a new token is added, the most important object' priority is boosted. * This biases the system already for purgeable queues that move a lot. * It doesn't seem more biasing is neccessary in this case, where no new object is added. */ assert(object->objq.next != NULL && object->objq.prev != NULL); /* object should be on a queue */ purgeable_q_t old_queue=vm_purgeable_object_remove(object); assert(old_queue); if (old_queue != queue) { kern_return_t result; /* Changing queue. Have to move token. */ vm_page_lock_queues(); vm_purgeable_token_delete_first(old_queue); result = vm_purgeable_token_add(queue); vm_page_unlock_queues(); assert(result==KERN_SUCCESS); /* this should never fail since we just freed a token */ } }; vm_purgeable_object_add(object, queue, (*state&VM_VOLATILE_GROUP_MASK)>>VM_VOLATILE_GROUP_SHIFT ); assert(queue->debug_count_objects>=0); break; case VM_PURGABLE_EMPTY: if (object->volatile_fault) { vm_page_t p; int refmod; queue_iterate(&object->memq, p, vm_page_t, listq) { if (p->busy || VM_PAGE_WIRED(p) || p->fictitious) { continue; } refmod = pmap_disconnect(p->phys_page); if ((refmod & VM_MEM_MODIFIED) && !p->dirty) { p->dirty = TRUE; } } } if (old_state != new_state) { assert(old_state == VM_PURGABLE_NONVOLATILE || old_state == VM_PURGABLE_VOLATILE); if (old_state == VM_PURGABLE_VOLATILE) { purgeable_q_t old_queue; /* object should be on a queue */ assert(object->objq.next != NULL && object->objq.prev != NULL); old_queue = vm_purgeable_object_remove(object); assert(old_queue); vm_page_lock_queues(); vm_purgeable_token_delete_first(old_queue); vm_page_unlock_queues(); } (void) vm_object_purge(object); } break; } *state = old_state; return KERN_SUCCESS; } #if TASK_SWAPPER /* * vm_object_res_deallocate * * (recursively) decrement residence counts on vm objects and their shadows. * Called from vm_object_deallocate and when swapping out an object. * * The object is locked, and remains locked throughout the function, * even as we iterate down the shadow chain. Locks on intermediate objects * will be dropped, but not the original object. * * NOTE: this function used to use recursion, rather than iteration. */ __private_extern__ void vm_object_res_deallocate( vm_object_t object) { vm_object_t orig_object = object; /* * Object is locked so it can be called directly * from vm_object_deallocate. Original object is never * unlocked. */ assert(object->res_count > 0); while (--object->res_count == 0) { assert(object->ref_count >= object->res_count); vm_object_deactivate_all_pages(object); /* iterate on shadow, if present */ if (object->shadow != VM_OBJECT_NULL) { vm_object_t tmp_object = object->shadow; vm_object_lock(tmp_object); if (object != orig_object) vm_object_unlock(object); object = tmp_object; assert(object->res_count > 0); } else break; } if (object != orig_object) vm_object_unlock(object); } /* * vm_object_res_reference * * Internal function to increment residence count on a vm object * and its shadows. It is called only from vm_object_reference, and * when swapping in a vm object, via vm_map_swap. * * The object is locked, and remains locked throughout the function, * even as we iterate down the shadow chain. Locks on intermediate objects * will be dropped, but not the original object. * * NOTE: this function used to use recursion, rather than iteration. */ __private_extern__ void vm_object_res_reference( vm_object_t object) { vm_object_t orig_object = object; /* * Object is locked, so this can be called directly * from vm_object_reference. This lock is never released. */ while ((++object->res_count == 1) && (object->shadow != VM_OBJECT_NULL)) { vm_object_t tmp_object = object->shadow; assert(object->ref_count >= object->res_count); vm_object_lock(tmp_object); if (object != orig_object) vm_object_unlock(object); object = tmp_object; } if (object != orig_object) vm_object_unlock(object); assert(orig_object->ref_count >= orig_object->res_count); } #endif /* TASK_SWAPPER */ /* * vm_object_reference: * * Gets another reference to the given object. */ #ifdef vm_object_reference #undef vm_object_reference #endif __private_extern__ void vm_object_reference( register vm_object_t object) { if (object == VM_OBJECT_NULL) return; vm_object_lock(object); assert(object->ref_count > 0); vm_object_reference_locked(object); vm_object_unlock(object); } #ifdef MACH_BSD /* * Scale the vm_object_cache * This is required to make sure that the vm_object_cache is big * enough to effectively cache the mapped file. * This is really important with UBC as all the regular file vnodes * have memory object associated with them. Havving this cache too * small results in rapid reclaim of vnodes and hurts performance a LOT! * * This is also needed as number of vnodes can be dynamically scaled. */ kern_return_t adjust_vm_object_cache( __unused vm_size_t oval, __unused vm_size_t nval) { #if VM_OBJECT_CACHE vm_object_cached_max = nval; vm_object_cache_trim(FALSE); #endif return (KERN_SUCCESS); } #endif /* MACH_BSD */ /* * vm_object_transpose * * This routine takes two VM objects of the same size and exchanges * their backing store. * The objects should be "quiesced" via a UPL operation with UPL_SET_IO_WIRE * and UPL_BLOCK_ACCESS if they are referenced anywhere. * * The VM objects must not be locked by caller. */ unsigned int vm_object_transpose_count = 0; kern_return_t vm_object_transpose( vm_object_t object1, vm_object_t object2, vm_object_size_t transpose_size) { vm_object_t tmp_object; kern_return_t retval; boolean_t object1_locked, object2_locked; vm_page_t page; vm_object_offset_t page_offset; lck_mtx_t *hash_lck; vm_object_hash_entry_t hash_entry; tmp_object = VM_OBJECT_NULL; object1_locked = FALSE; object2_locked = FALSE; if (object1 == object2 || object1 == VM_OBJECT_NULL || object2 == VM_OBJECT_NULL) { /* * If the 2 VM objects are the same, there's * no point in exchanging their backing store. */ retval = KERN_INVALID_VALUE; goto done; } /* * Since we need to lock both objects at the same time, * make sure we always lock them in the same order to * avoid deadlocks. */ if (object1 > object2) { tmp_object = object1; object1 = object2; object2 = tmp_object; } /* * Allocate a temporary VM object to hold object1's contents * while we copy object2 to object1. */ tmp_object = vm_object_allocate(transpose_size); vm_object_lock(tmp_object); tmp_object->can_persist = FALSE; /* * Grab control of the 1st VM object. */ vm_object_lock(object1); object1_locked = TRUE; if (!object1->alive || object1->terminating || object1->copy || object1->shadow || object1->shadowed || object1->purgable != VM_PURGABLE_DENY) { /* * We don't deal with copy or shadow objects (yet). */ retval = KERN_INVALID_VALUE; goto done; } /* * We're about to mess with the object's backing store and * taking a "paging_in_progress" reference wouldn't be enough * to prevent any paging activity on this object, so the caller should * have "quiesced" the objects beforehand, via a UPL operation with * UPL_SET_IO_WIRE (to make sure all the pages are there and wired) * and UPL_BLOCK_ACCESS (to mark the pages "busy"). * * Wait for any paging operation to complete (but only paging, not * other kind of activities not linked to the pager). After we're * statisfied that there's no more paging in progress, we keep the * object locked, to guarantee that no one tries to access its pager. */ vm_object_paging_only_wait(object1, THREAD_UNINT); /* * Same as above for the 2nd object... */ vm_object_lock(object2); object2_locked = TRUE; if (! object2->alive || object2->terminating || object2->copy || object2->shadow || object2->shadowed || object2->purgable != VM_PURGABLE_DENY) { retval = KERN_INVALID_VALUE; goto done; } vm_object_paging_only_wait(object2, THREAD_UNINT); if (object1->size != object2->size || object1->size != transpose_size) { /* * If the 2 objects don't have the same size, we can't * exchange their backing stores or one would overflow. * If their size doesn't match the caller's * "transpose_size", we can't do it either because the * transpose operation will affect the entire span of * the objects. */ retval = KERN_INVALID_VALUE; goto done; } /* * Transpose the lists of resident pages. * This also updates the resident_page_count and the memq_hint. */ if (object1->phys_contiguous || queue_empty(&object1->memq)) { /* * No pages in object1, just transfer pages * from object2 to object1. No need to go through * an intermediate object. */ while (!queue_empty(&object2->memq)) { page = (vm_page_t) queue_first(&object2->memq); vm_page_rename(page, object1, page->offset, FALSE); } assert(queue_empty(&object2->memq)); } else if (object2->phys_contiguous || queue_empty(&object2->memq)) { /* * No pages in object2, just transfer pages * from object1 to object2. No need to go through * an intermediate object. */ while (!queue_empty(&object1->memq)) { page = (vm_page_t) queue_first(&object1->memq); vm_page_rename(page, object2, page->offset, FALSE); } assert(queue_empty(&object1->memq)); } else { /* transfer object1's pages to tmp_object */ while (!queue_empty(&object1->memq)) { page = (vm_page_t) queue_first(&object1->memq); page_offset = page->offset; vm_page_remove(page, TRUE); page->offset = page_offset; queue_enter(&tmp_object->memq, page, vm_page_t, listq); } assert(queue_empty(&object1->memq)); /* transfer object2's pages to object1 */ while (!queue_empty(&object2->memq)) { page = (vm_page_t) queue_first(&object2->memq); vm_page_rename(page, object1, page->offset, FALSE); } assert(queue_empty(&object2->memq)); /* transfer tmp_object's pages to object1 */ while (!queue_empty(&tmp_object->memq)) { page = (vm_page_t) queue_first(&tmp_object->memq); queue_remove(&tmp_object->memq, page, vm_page_t, listq); vm_page_insert(page, object2, page->offset); } assert(queue_empty(&tmp_object->memq)); } #define __TRANSPOSE_FIELD(field) \ MACRO_BEGIN \ tmp_object->field = object1->field; \ object1->field = object2->field; \ object2->field = tmp_object->field; \ MACRO_END /* "Lock" refers to the object not its contents */ /* "size" should be identical */ assert(object1->size == object2->size); /* "memq_hint" was updated above when transposing pages */ /* "ref_count" refers to the object not its contents */ #if TASK_SWAPPER /* "res_count" refers to the object not its contents */ #endif /* "resident_page_count" was updated above when transposing pages */ /* "wired_page_count" was updated above when transposing pages */ /* "reusable_page_count" was updated above when transposing pages */ /* there should be no "copy" */ assert(!object1->copy); assert(!object2->copy); /* there should be no "shadow" */ assert(!object1->shadow); assert(!object2->shadow); __TRANSPOSE_FIELD(shadow_offset); /* used by phys_contiguous objects */ __TRANSPOSE_FIELD(pager); __TRANSPOSE_FIELD(paging_offset); __TRANSPOSE_FIELD(pager_control); /* update the memory_objects' pointers back to the VM objects */ if (object1->pager_control != MEMORY_OBJECT_CONTROL_NULL) { memory_object_control_collapse(object1->pager_control, object1); } if (object2->pager_control != MEMORY_OBJECT_CONTROL_NULL) { memory_object_control_collapse(object2->pager_control, object2); } __TRANSPOSE_FIELD(copy_strategy); /* "paging_in_progress" refers to the object not its contents */ assert(!object1->paging_in_progress); assert(!object2->paging_in_progress); assert(object1->activity_in_progress); assert(object2->activity_in_progress); /* "all_wanted" refers to the object not its contents */ __TRANSPOSE_FIELD(pager_created); __TRANSPOSE_FIELD(pager_initialized); __TRANSPOSE_FIELD(pager_ready); __TRANSPOSE_FIELD(pager_trusted); __TRANSPOSE_FIELD(can_persist); __TRANSPOSE_FIELD(internal); __TRANSPOSE_FIELD(temporary); __TRANSPOSE_FIELD(private); __TRANSPOSE_FIELD(pageout); /* "alive" should be set */ assert(object1->alive); assert(object2->alive); /* "purgeable" should be non-purgeable */ assert(object1->purgable == VM_PURGABLE_DENY); assert(object2->purgable == VM_PURGABLE_DENY); /* "shadowed" refers to the the object not its contents */ __TRANSPOSE_FIELD(silent_overwrite); __TRANSPOSE_FIELD(advisory_pageout); __TRANSPOSE_FIELD(true_share); /* "terminating" should not be set */ assert(!object1->terminating); assert(!object2->terminating); __TRANSPOSE_FIELD(named); /* "shadow_severed" refers to the object not its contents */ __TRANSPOSE_FIELD(phys_contiguous); __TRANSPOSE_FIELD(nophyscache); /* "cached_list.next" points to transposed object */ object1->cached_list.next = (queue_entry_t) object2; object2->cached_list.next = (queue_entry_t) object1; /* "cached_list.prev" should be NULL */ assert(object1->cached_list.prev == NULL); assert(object2->cached_list.prev == NULL); /* "msr_q" is linked to the object not its contents */ assert(queue_empty(&object1->msr_q)); assert(queue_empty(&object2->msr_q)); __TRANSPOSE_FIELD(last_alloc); __TRANSPOSE_FIELD(sequential); __TRANSPOSE_FIELD(pages_created); __TRANSPOSE_FIELD(pages_used); #if MACH_PAGEMAP __TRANSPOSE_FIELD(existence_map); #endif __TRANSPOSE_FIELD(cow_hint); #if MACH_ASSERT __TRANSPOSE_FIELD(paging_object); #endif __TRANSPOSE_FIELD(wimg_bits); __TRANSPOSE_FIELD(code_signed); if (object1->hashed) { hash_lck = vm_object_hash_lock_spin(object2->pager); hash_entry = vm_object_hash_lookup(object2->pager, FALSE); assert(hash_entry != VM_OBJECT_HASH_ENTRY_NULL); hash_entry->object = object2; vm_object_hash_unlock(hash_lck); } if (object2->hashed) { hash_lck = vm_object_hash_lock_spin(object1->pager); hash_entry = vm_object_hash_lookup(object1->pager, FALSE); assert(hash_entry != VM_OBJECT_HASH_ENTRY_NULL); hash_entry->object = object1; vm_object_hash_unlock(hash_lck); } __TRANSPOSE_FIELD(hashed); object1->transposed = TRUE; object2->transposed = TRUE; __TRANSPOSE_FIELD(mapping_in_progress); __TRANSPOSE_FIELD(volatile_empty); __TRANSPOSE_FIELD(volatile_fault); __TRANSPOSE_FIELD(all_reusable); assert(object1->blocked_access); assert(object2->blocked_access); assert(object1->__object2_unused_bits == 0); assert(object2->__object2_unused_bits == 0); #if UPL_DEBUG /* "uplq" refers to the object not its contents (see upl_transpose()) */ #endif assert(object1->objq.next == NULL); assert(object1->objq.prev == NULL); assert(object2->objq.next == NULL); assert(object2->objq.prev == NULL); #undef __TRANSPOSE_FIELD retval = KERN_SUCCESS; done: /* * Cleanup. */ if (tmp_object != VM_OBJECT_NULL) { vm_object_unlock(tmp_object); /* * Re-initialize the temporary object to avoid * deallocating a real pager. */ _vm_object_allocate(transpose_size, tmp_object); vm_object_deallocate(tmp_object); tmp_object = VM_OBJECT_NULL; } if (object1_locked) { vm_object_unlock(object1); object1_locked = FALSE; } if (object2_locked) { vm_object_unlock(object2); object2_locked = FALSE; } vm_object_transpose_count++; return retval; } /* * vm_object_cluster_size * * Determine how big a cluster we should issue an I/O for... * * Inputs: *start == offset of page needed * *length == maximum cluster pager can handle * Outputs: *start == beginning offset of cluster * *length == length of cluster to try * * The original *start will be encompassed by the cluster * */ extern int speculative_reads_disabled; #if CONFIG_EMBEDDED unsigned int preheat_pages_max = MAX_UPL_TRANSFER; unsigned int preheat_pages_min = 8; unsigned int preheat_pages_mult = 4; #else unsigned int preheat_pages_max = MAX_UPL_TRANSFER; unsigned int preheat_pages_min = 8; unsigned int preheat_pages_mult = 4; #endif uint32_t pre_heat_scaling[MAX_UPL_TRANSFER + 1]; uint32_t pre_heat_cluster[MAX_UPL_TRANSFER + 1]; __private_extern__ void vm_object_cluster_size(vm_object_t object, vm_object_offset_t *start, vm_size_t *length, vm_object_fault_info_t fault_info, uint32_t *io_streaming) { vm_size_t pre_heat_size; vm_size_t tail_size; vm_size_t head_size; vm_size_t max_length; vm_size_t cluster_size; vm_object_offset_t object_size; vm_object_offset_t orig_start; vm_object_offset_t target_start; vm_object_offset_t offset; vm_behavior_t behavior; boolean_t look_behind = TRUE; boolean_t look_ahead = TRUE; uint32_t throttle_limit; int sequential_run; int sequential_behavior = VM_BEHAVIOR_SEQUENTIAL; unsigned int max_ph_size; unsigned int min_ph_size; unsigned int ph_mult; assert( !(*length & PAGE_MASK)); assert( !(*start & PAGE_MASK_64)); if ( (ph_mult = preheat_pages_mult) < 1 ) ph_mult = 1; if ( (min_ph_size = preheat_pages_min) < 1 ) min_ph_size = 1; if ( (max_ph_size = preheat_pages_max) > MAX_UPL_TRANSFER ) max_ph_size = MAX_UPL_TRANSFER; if ( (max_length = *length) > (max_ph_size * PAGE_SIZE) ) max_length = (max_ph_size * PAGE_SIZE); /* * we'll always return a cluster size of at least * 1 page, since the original fault must always * be processed */ *length = PAGE_SIZE; *io_streaming = 0; if (speculative_reads_disabled || fault_info == NULL || max_length == 0) { /* * no cluster... just fault the page in */ return; } orig_start = *start; target_start = orig_start; cluster_size = round_page(fault_info->cluster_size); behavior = fault_info->behavior; vm_object_lock(object); if (object->internal) object_size = object->size; else if (object->pager != MEMORY_OBJECT_NULL) vnode_pager_get_object_size(object->pager, &object_size); else goto out; /* pager is gone for this object, nothing more to do */ object_size = round_page_64(object_size); if (orig_start >= object_size) { /* * fault occurred beyond the EOF... * we need to punt w/o changing the * starting offset */ goto out; } if (object->pages_used > object->pages_created) { /* * must have wrapped our 32 bit counters * so reset */ object->pages_used = object->pages_created = 0; } if ((sequential_run = object->sequential)) { if (sequential_run < 0) { sequential_behavior = VM_BEHAVIOR_RSEQNTL; sequential_run = 0 - sequential_run; } else { sequential_behavior = VM_BEHAVIOR_SEQUENTIAL; } } switch(behavior) { default: behavior = VM_BEHAVIOR_DEFAULT; case VM_BEHAVIOR_DEFAULT: if (object->internal && fault_info->user_tag == VM_MEMORY_STACK) goto out; if (sequential_run >= (3 * PAGE_SIZE)) { pre_heat_size = sequential_run + PAGE_SIZE; if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) look_behind = FALSE; else look_ahead = FALSE; *io_streaming = 1; } else { if (object->pages_created < 32 * ph_mult) { /* * prime the pump */ pre_heat_size = PAGE_SIZE * 8 * ph_mult; break; } /* * Linear growth in PH size: The maximum size is max_length... * this cacluation will result in a size that is neither a * power of 2 nor a multiple of PAGE_SIZE... so round * it up to the nearest PAGE_SIZE boundary */ pre_heat_size = (ph_mult * (max_length * object->pages_used) / object->pages_created); if (pre_heat_size < PAGE_SIZE * min_ph_size) pre_heat_size = PAGE_SIZE * min_ph_size; else pre_heat_size = round_page(pre_heat_size); } break; case VM_BEHAVIOR_RANDOM: if ((pre_heat_size = cluster_size) <= PAGE_SIZE) goto out; break; case VM_BEHAVIOR_SEQUENTIAL: if ((pre_heat_size = cluster_size) == 0) pre_heat_size = sequential_run + PAGE_SIZE; look_behind = FALSE; *io_streaming = 1; break; case VM_BEHAVIOR_RSEQNTL: if ((pre_heat_size = cluster_size) == 0) pre_heat_size = sequential_run + PAGE_SIZE; look_ahead = FALSE; *io_streaming = 1; break; } throttle_limit = (uint32_t) max_length; assert(throttle_limit == max_length); if (vnode_pager_check_hard_throttle(object->pager, &throttle_limit, *io_streaming) == KERN_SUCCESS) { if (max_length > throttle_limit) max_length = throttle_limit; } if (pre_heat_size > max_length) pre_heat_size = max_length; if (behavior == VM_BEHAVIOR_DEFAULT) { if (vm_page_free_count < vm_page_throttle_limit) pre_heat_size = trunc_page(pre_heat_size / 8); else if (vm_page_free_count < vm_page_free_target) pre_heat_size = trunc_page(pre_heat_size / 2); if (pre_heat_size <= PAGE_SIZE) goto out; } if (look_ahead == TRUE) { if (look_behind == TRUE) { /* * if we get here its due to a random access... * so we want to center the original fault address * within the cluster we will issue... make sure * to calculate 'head_size' as a multiple of PAGE_SIZE... * 'pre_heat_size' is a multiple of PAGE_SIZE but not * necessarily an even number of pages so we need to truncate * the result to a PAGE_SIZE boundary */ head_size = trunc_page(pre_heat_size / 2); if (target_start > head_size) target_start -= head_size; else target_start = 0; /* * 'target_start' at this point represents the beginning offset * of the cluster we are considering... 'orig_start' will be in * the center of this cluster if we didn't have to clip the start * due to running into the start of the file */ } if ((target_start + pre_heat_size) > object_size) pre_heat_size = (vm_size_t)(round_page_64(object_size - target_start)); /* * at this point caclulate the number of pages beyond the original fault * address that we want to consider... this is guaranteed not to extend beyond * the current EOF... */ assert((vm_size_t)(orig_start - target_start) == (orig_start - target_start)); tail_size = pre_heat_size - (vm_size_t)(orig_start - target_start) - PAGE_SIZE; } else { if (pre_heat_size > target_start) pre_heat_size = (vm_size_t) target_start; /* XXX: 32-bit vs 64-bit ? Joe ? */ tail_size = 0; } assert( !(target_start & PAGE_MASK_64)); assert( !(pre_heat_size & PAGE_MASK)); pre_heat_scaling[pre_heat_size / PAGE_SIZE]++; if (pre_heat_size <= PAGE_SIZE) goto out; if (look_behind == TRUE) { /* * take a look at the pages before the original * faulting offset... recalculate this in case * we had to clip 'pre_heat_size' above to keep * from running past the EOF. */ head_size = pre_heat_size - tail_size - PAGE_SIZE; for (offset = orig_start - PAGE_SIZE_64; head_size; offset -= PAGE_SIZE_64, head_size -= PAGE_SIZE) { /* * don't poke below the lowest offset */ if (offset < fault_info->lo_offset) break; /* * for external objects and internal objects w/o an existence map * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN */ #if MACH_PAGEMAP if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_ABSENT) { /* * we know for a fact that the pager can't provide the page * so don't include it or any pages beyond it in this cluster */ break; } #endif if (vm_page_lookup(object, offset) != VM_PAGE_NULL) { /* * don't bridge resident pages */ break; } *start = offset; *length += PAGE_SIZE; } } if (look_ahead == TRUE) { for (offset = orig_start + PAGE_SIZE_64; tail_size; offset += PAGE_SIZE_64, tail_size -= PAGE_SIZE) { /* * don't poke above the highest offset */ if (offset >= fault_info->hi_offset) break; assert(offset < object_size); /* * for external objects and internal objects w/o an existence map * vm_externl_state_get will return VM_EXTERNAL_STATE_UNKNOWN */ #if MACH_PAGEMAP if (vm_external_state_get(object->existence_map, offset) == VM_EXTERNAL_STATE_ABSENT) { /* * we know for a fact that the pager can't provide the page * so don't include it or any pages beyond it in this cluster */ break; } #endif if (vm_page_lookup(object, offset) != VM_PAGE_NULL) { /* * don't bridge resident pages */ break; } *length += PAGE_SIZE; } } out: if (*length > max_length) *length = max_length; pre_heat_cluster[*length / PAGE_SIZE]++; vm_object_unlock(object); } /* * Allow manipulation of individual page state. This is actually part of * the UPL regimen but takes place on the VM object rather than on a UPL */ kern_return_t vm_object_page_op( vm_object_t object, vm_object_offset_t offset, int ops, ppnum_t *phys_entry, int *flags) { vm_page_t dst_page; vm_object_lock(object); if(ops & UPL_POP_PHYSICAL) { if(object->phys_contiguous) { if (phys_entry) { *phys_entry = (ppnum_t) (object->shadow_offset >> PAGE_SHIFT); } vm_object_unlock(object); return KERN_SUCCESS; } else { vm_object_unlock(object); return KERN_INVALID_OBJECT; } } if(object->phys_contiguous) { vm_object_unlock(object); return KERN_INVALID_OBJECT; } while(TRUE) { if((dst_page = vm_page_lookup(object,offset)) == VM_PAGE_NULL) { vm_object_unlock(object); return KERN_FAILURE; } /* Sync up on getting the busy bit */ if((dst_page->busy || dst_page->cleaning) && (((ops & UPL_POP_SET) && (ops & UPL_POP_BUSY)) || (ops & UPL_POP_DUMP))) { /* someone else is playing with the page, we will */ /* have to wait */ PAGE_SLEEP(object, dst_page, THREAD_UNINT); continue; } if (ops & UPL_POP_DUMP) { if (dst_page->pmapped == TRUE) pmap_disconnect(dst_page->phys_page); VM_PAGE_FREE(dst_page); break; } if (flags) { *flags = 0; /* Get the condition of flags before requested ops */ /* are undertaken */ if(dst_page->dirty) *flags |= UPL_POP_DIRTY; if(dst_page->pageout) *flags |= UPL_POP_PAGEOUT; if(dst_page->precious) *flags |= UPL_POP_PRECIOUS; if(dst_page->absent) *flags |= UPL_POP_ABSENT; if(dst_page->busy) *flags |= UPL_POP_BUSY; } /* The caller should have made a call either contingent with */ /* or prior to this call to set UPL_POP_BUSY */ if(ops & UPL_POP_SET) { /* The protection granted with this assert will */ /* not be complete. If the caller violates the */ /* convention and attempts to change page state */ /* without first setting busy we may not see it */ /* because the page may already be busy. However */ /* if such violations occur we will assert sooner */ /* or later. */ assert(dst_page->busy || (ops & UPL_POP_BUSY)); if (ops & UPL_POP_DIRTY) dst_page->dirty = TRUE; if (ops & UPL_POP_PAGEOUT) dst_page->pageout = TRUE; if (ops & UPL_POP_PRECIOUS) dst_page->precious = TRUE; if (ops & UPL_POP_ABSENT) dst_page->absent = TRUE; if (ops & UPL_POP_BUSY) dst_page->busy = TRUE; } if(ops & UPL_POP_CLR) { assert(dst_page->busy); if (ops & UPL_POP_DIRTY) dst_page->dirty = FALSE; if (ops & UPL_POP_PAGEOUT) dst_page->pageout = FALSE; if (ops & UPL_POP_PRECIOUS) dst_page->precious = FALSE; if (ops & UPL_POP_ABSENT) dst_page->absent = FALSE; if (ops & UPL_POP_BUSY) { dst_page->busy = FALSE; PAGE_WAKEUP(dst_page); } } if (dst_page->encrypted) { /* * ENCRYPTED SWAP: * We need to decrypt this encrypted page before the * caller can access its contents. * But if the caller really wants to access the page's * contents, they have to keep the page "busy". * Otherwise, the page could get recycled or re-encrypted * at any time. */ if ((ops & UPL_POP_SET) && (ops & UPL_POP_BUSY) && dst_page->busy) { /* * The page is stable enough to be accessed by * the caller, so make sure its contents are * not encrypted. */ vm_page_decrypt(dst_page, 0); } else { /* * The page is not busy, so don't bother * decrypting it, since anything could * happen to it between now and when the * caller wants to access it. * We should not give the caller access * to this page. */ assert(!phys_entry); } } if (phys_entry) { /* * The physical page number will remain valid * only if the page is kept busy. * ENCRYPTED SWAP: make sure we don't let the * caller access an encrypted page. */ assert(dst_page->busy); assert(!dst_page->encrypted); *phys_entry = dst_page->phys_page; } break; } vm_object_unlock(object); return KERN_SUCCESS; } /* * vm_object_range_op offers performance enhancement over * vm_object_page_op for page_op functions which do not require page * level state to be returned from the call. Page_op was created to provide * a low-cost alternative to page manipulation via UPLs when only a single * page was involved. The range_op call establishes the ability in the _op * family of functions to work on multiple pages where the lack of page level * state handling allows the caller to avoid the overhead of the upl structures. */ kern_return_t vm_object_range_op( vm_object_t object, vm_object_offset_t offset_beg, vm_object_offset_t offset_end, int ops, uint32_t *range) { vm_object_offset_t offset; vm_page_t dst_page; if (offset_end - offset_beg > (uint32_t) -1) { /* range is too big and would overflow "*range" */ return KERN_INVALID_ARGUMENT; } if (object->resident_page_count == 0) { if (range) { if (ops & UPL_ROP_PRESENT) { *range = 0; } else { *range = (uint32_t) (offset_end - offset_beg); assert(*range == (offset_end - offset_beg)); } } return KERN_SUCCESS; } vm_object_lock(object); if (object->phys_contiguous) { vm_object_unlock(object); return KERN_INVALID_OBJECT; } offset = offset_beg & ~PAGE_MASK_64; while (offset < offset_end) { dst_page = vm_page_lookup(object, offset); if (dst_page != VM_PAGE_NULL) { if (ops & UPL_ROP_DUMP) { if (dst_page->busy || dst_page->cleaning) { /* * someone else is playing with the * page, we will have to wait */ PAGE_SLEEP(object, dst_page, THREAD_UNINT); /* * need to relook the page up since it's * state may have changed while we slept * it might even belong to a different object * at this point */ continue; } if (dst_page->pmapped == TRUE) pmap_disconnect(dst_page->phys_page); VM_PAGE_FREE(dst_page); } else if ((ops & UPL_ROP_ABSENT) && !dst_page->absent) break; } else if (ops & UPL_ROP_PRESENT) break; offset += PAGE_SIZE; } vm_object_unlock(object); if (range) { if (offset > offset_end) offset = offset_end; if(offset > offset_beg) { *range = (uint32_t) (offset - offset_beg); assert(*range == (offset - offset_beg)); } else { *range = 0; } } return KERN_SUCCESS; } uint32_t scan_object_collision = 0; void vm_object_lock(vm_object_t object) { if (object == vm_pageout_scan_wants_object) { scan_object_collision++; mutex_pause(2); } lck_rw_lock_exclusive(&object->Lock); } boolean_t vm_object_lock_avoid(vm_object_t object) { if (object == vm_pageout_scan_wants_object) { scan_object_collision++; return TRUE; } return FALSE; } boolean_t _vm_object_lock_try(vm_object_t object) { return (lck_rw_try_lock_exclusive(&object->Lock)); } boolean_t vm_object_lock_try(vm_object_t object) { if (vm_object_lock_avoid(object)) { mutex_pause(2); } return _vm_object_lock_try(object); } void vm_object_lock_shared(vm_object_t object) { if (vm_object_lock_avoid(object)) { mutex_pause(2); } lck_rw_lock_shared(&object->Lock); } boolean_t vm_object_lock_try_shared(vm_object_t object) { if (vm_object_lock_avoid(object)) { mutex_pause(2); } return (lck_rw_try_lock_shared(&object->Lock)); } |