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 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780 8781 8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792 8793 8794 8795 8796 8797 8798 8799 8800 8801 8802 8803 8804 8805 8806 8807 8808 8809 8810 8811 8812 8813 8814 8815 8816 8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836 8837 8838 8839 8840 8841 8842 8843 8844 8845 8846 8847 8848 8849 8850 8851 8852 8853 8854 8855 8856 8857 8858 8859 8860 8861 8862 8863 8864 8865 8866 8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877 8878 8879 8880 8881 8882 8883 8884 8885 8886 8887 8888 8889 8890 8891 8892 8893 8894 8895 8896 8897 8898 8899 8900 8901 8902 8903 8904 8905 8906 8907 8908 8909 8910 8911 8912 8913 8914 8915 8916 8917 8918 8919 8920 8921 8922 8923 8924 8925 8926 8927 8928 8929 8930 8931 8932 8933 8934 8935 8936 8937 8938 8939 8940 8941 8942 8943 8944 8945 8946 8947 8948 8949 8950 8951 8952 8953 8954 8955 8956 8957 8958 8959 8960 8961 8962 8963 8964 8965 8966 8967 8968 8969 8970 8971 8972 8973 8974 8975 8976 8977 8978 8979 8980 8981 8982 8983 8984 8985 8986 8987 8988 8989 8990 8991 8992 8993 8994 8995 8996 8997 8998 8999 9000 9001 9002 9003 9004 9005 9006 9007 9008 9009 9010 9011 9012 9013 9014 9015 9016 9017 9018 9019 9020 9021 9022 9023 9024 9025 9026 9027 9028 9029 9030 9031 9032 9033 9034 9035 9036 9037 9038 9039 9040 9041 9042 9043 9044 9045 9046 9047 9048 9049 9050 9051 9052 9053 9054 9055 9056 9057 9058 9059 9060 9061 9062 9063 9064 9065 9066 9067 9068 9069 9070 9071 9072 9073 9074 9075 9076 9077 9078 9079 9080 9081 9082 9083 9084 9085 9086 9087 9088 9089 9090 9091 9092 9093 9094 9095 9096 9097 9098 9099 9100 9101 9102 9103 9104 9105 9106 9107 9108 9109 9110 9111 9112 9113 9114 9115 9116 9117 9118 9119 9120 9121 9122 9123 9124 9125 9126 9127 9128 9129 9130 9131 9132 9133 9134 9135 9136 9137 9138 9139 9140 9141 9142 9143 9144 9145 9146 9147 9148 9149 9150 9151 9152 9153 9154 9155 9156 9157 9158 9159 9160 9161 9162 9163 9164 9165 9166 9167 9168 9169 9170 9171 9172 9173 9174 9175 9176 9177 9178 9179 9180 9181 9182 9183 9184 9185 9186 9187 9188 9189 9190 9191 9192 9193 9194 9195 9196 9197 9198 9199 9200 9201 9202 9203 9204 9205 9206 9207 9208 9209 9210 9211 9212 9213 9214 9215 9216 9217 9218 9219 9220 9221 9222 9223 9224 9225 9226 9227 9228 9229 9230 9231 9232 9233 9234 9235 9236 9237 9238 9239 9240 9241 9242 9243 9244 9245 9246 9247 9248 9249 9250 9251 9252 9253 9254 9255 9256 9257 9258 9259 9260 9261 9262 9263 9264 9265 9266 9267 9268 9269 9270 9271 9272 9273 9274 9275 9276 9277 9278 9279 9280 9281 9282 9283 9284 9285 9286 9287 9288 9289 9290 9291 9292 9293 9294 9295 9296 9297 9298 9299 9300 9301 9302 9303 9304 9305 9306 9307 9308 9309 9310 9311 9312 9313 9314 9315 9316 9317 9318 9319 9320 9321 9322 9323 9324 9325 9326 9327 9328 9329 9330 9331 9332 9333 9334 9335 9336 9337 9338 9339 9340 9341 9342 9343 9344 9345 9346 9347 9348 9349 9350 9351 9352 9353 9354 9355 9356 9357 9358 9359 9360 9361 9362 9363 9364 9365 9366 9367 9368 9369 9370 9371 9372 9373 9374 9375 9376 9377 9378 9379 9380 9381 9382 9383 9384 9385 9386 9387 9388 9389 9390 9391 9392 9393 9394 9395 9396 9397 9398 9399 9400 9401 9402 9403 9404 9405 9406 9407 9408 9409 9410 9411 9412 9413 9414 9415 9416 9417 9418 9419 9420 9421 9422 9423 9424 9425 9426 9427 9428 9429 9430 9431 9432 9433 9434 9435 9436 9437 9438 9439 9440 9441 9442 9443 9444 9445 9446 9447 9448 9449 9450 9451 9452 9453 9454 9455 9456 9457 9458 9459 9460 9461 9462 9463 9464 9465 9466 9467 9468 9469 9470 9471 9472 9473 9474 9475 9476 9477 9478 9479 9480 9481 9482 9483 9484 9485 9486 9487 9488 9489 9490 9491 9492 9493 9494 9495 9496 9497 9498 9499 9500 9501 9502 9503 9504 9505 9506 9507 9508 9509 9510 9511 9512 9513 9514 9515 9516 9517 9518 9519 9520 9521 9522 9523 9524 9525 9526 9527 9528 9529 9530 9531 9532 9533 9534 9535 9536 9537 9538 9539 9540 9541 9542 9543 9544 9545 9546 9547 9548 9549 9550 9551 9552 9553 9554 9555 9556 9557 9558 9559 9560 9561 9562 9563 9564 9565 9566 9567 9568 9569 9570 9571 9572 9573 9574 9575 9576 9577 9578 9579 9580 9581 9582 9583 9584 9585 9586 9587 9588 9589 9590 9591 9592 9593 9594 9595 9596 9597 9598 9599 9600 9601 9602 9603 9604 9605 9606 9607 9608 9609 9610 9611 9612 9613 9614 9615 9616 9617 9618 9619 9620 9621 9622 9623 9624 9625 9626 9627 9628 9629 9630 9631 9632 9633 9634 9635 9636 9637 9638 9639 9640 9641 9642 9643 9644 9645 9646 9647 9648 9649 9650 9651 9652 9653 9654 9655 9656 9657 9658 9659 9660 9661 9662 9663 9664 9665 9666 9667 9668 9669 9670 9671 9672 9673 9674 9675 9676 9677 9678 9679 9680 9681 9682 9683 9684 9685 9686 9687 9688 9689 9690 9691 9692 9693 9694 9695 9696 9697 9698 9699 9700 9701 9702 9703 9704 9705 9706 9707 9708 9709 9710 9711 9712 9713 9714 9715 9716 9717 9718 9719 9720 9721 9722 9723 9724 9725 9726 9727 9728 9729 9730 9731 9732 9733 9734 9735 9736 9737 9738 9739 9740 9741 9742 9743 9744 9745 9746 9747 9748 9749 9750 9751 9752 9753 9754 9755 9756 9757 9758 9759 9760 9761 9762 9763 9764 9765 9766 9767 9768 9769 9770 9771 9772 9773 9774 9775 9776 9777 9778 9779 9780 9781 9782 9783 9784 9785 9786 9787 9788 9789 9790 9791 9792 9793 9794 9795 9796 9797 9798 9799 9800 9801 9802 9803 | /* * Copyright (c) 2000-2021 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_fault.c * Author: Avadis Tevanian, Jr., Michael Wayne Young * * Page fault handling module. */ #include <libkern/OSAtomic.h> #include <mach/mach_types.h> #include <mach/kern_return.h> #include <mach/message.h> /* for error codes */ #include <mach/vm_param.h> #include <mach/vm_behavior.h> #include <mach/memory_object.h> /* For memory_object_data_{request,unlock} */ #include <mach/sdt.h> #include <kern/kern_types.h> #include <kern/host_statistics.h> #include <kern/backtrace.h> #include <kern/counter.h> #include <kern/task.h> #include <kern/thread.h> #include <kern/telemetry.h> #include <kern/sched_prim.h> #include <kern/host.h> #include <kern/mach_param.h> #include <kern/macro_help.h> #include <kern/zalloc_internal.h> #include <kern/misc_protos.h> #include <kern/policy_internal.h> #include <kern/exc_guard.h> #include <kern/telemetry.h> #include <vm/vm_compressor_internal.h> #include <vm/vm_compressor_pager_internal.h> #include <vm/vm_dyld_pager_internal.h> #include <vm/vm_entry_lock_internal.h> #include <vm/vm_fault_internal.h> #include <vm/vm_map_internal.h> #include <vm/vm_object_internal.h> #include <vm/vm_page_internal.h> #include <vm/vm_kern_internal.h> #include <vm/pmap.h> #include <vm/vm_pageout_internal.h> #include <vm/vm_protos_internal.h> #include <vm/vm_external.h> #include <vm/memory_object.h> #include <vm/vm_purgeable_internal.h> /* Needed by some vm_page.h macros */ #include <vm/vm_shared_region.h> #include <vm/vm_page_internal.h> #include <vm/vm_map_lock_internal.h> #include <vm/vm_lock_contention.h> #include <vm/vm_log.h> #if HAS_MTE #include <vm/vm_mteinfo_internal.h> #include <vm/vm_memtag.h> #endif /* HAS_MTE */ #include <sys/codesign.h> #include <sys/code_signing.h> #include <sys/kdebug.h> #include <sys/kdebug_triage.h> #include <sys/reason.h> #include <sys/signalvar.h> #include <san/kasan.h> #include <libkern/coreanalytics/coreanalytics.h> #define VM_FAULT_CLASSIFY 0 #define TRACEFAULTPAGE 0 /* (TEST/DEBUG) */ int vm_protect_privileged_from_untrusted = 1; /* * Enforce a maximum number of concurrent PageIns per vm-object to prevent * high-I/O-volume tasks from saturating storage and starving the rest of the * system. * * TODO: This throttling mechanism may be more naturally done by the pager, * filesystem, or storage layers, which will have better information about how * much concurrency the backing store can reasonably support. */ TUNABLE(uint16_t, vm_object_pagein_throttle, "vm_object_pagein_throttle", 16); /* * Various debugging counters for lock contention scenarios that are expected * to be rare. */ #if DEBUG || DEVELOPMENT SCALABLE_COUNTER_DEFINE(vm_fault_busy_trylock_count); SCALABLE_COUNTER_DEFINE(vm_fault_excl_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_count); SCALABLE_COUNTER_DEFINE(vm_fault_copy_busy_trylock_count); #endif /* DEBUG || DEVELOPMENT */ SCALABLE_COUNTER_DEFINE(vm_fault_busy_retry_count); SCALABLE_COUNTER_DEFINE(vm_fault_excl_busy_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_busy_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_clean_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_busy_copy_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_blocked_obj_count); SCALABLE_COUNTER_DEFINE(vm_fault_page_excl_pager_not_ready_count); SCALABLE_COUNTER_DEFINE(vm_fault_copy_busy_retry_count); /* * We apply a hard throttle to the demand zero rate of tasks that we believe are running out of control which * kicks in when swap space runs out. 64-bit programs have massive address spaces and can leak enormous amounts * of memory if they're buggy and can run the system completely out of swap space. If this happens, we * impose a hard throttle on them to prevent them from taking the last bit of memory left. This helps * keep the UI active so that the user has a chance to kill the offending task before the system * completely hangs. * * The hard throttle is only applied when the system is nearly completely out of swap space and is only applied * to tasks that appear to be bloated. When swap runs out, any task using more than vm_hard_throttle_threshold * will be throttled. The throttling is done by giving the thread that's trying to demand zero a page a * delay of HARD_THROTTLE_DELAY microseconds before being allowed to try the page fault again. */ extern void throttle_lowpri_io(int); extern struct vnode *vnode_pager_lookup_vnode(memory_object_t); extern uint64_t get_current_unique_pid(void); uint64_t vm_hard_throttle_threshold; #if DEBUG || DEVELOPMENT static bool vmtc_panic_instead = false; int panic_object_not_alive = 1; #endif /* DEBUG || DEVELOPMENT */ OS_ALWAYS_INLINE boolean_t NEED_TO_HARD_THROTTLE_THIS_TASK(void) { return vm_wants_task_throttled(current_task()) || ((vm_page_free_count < vm_page_throttle_limit || HARD_THROTTLE_LIMIT_REACHED()) && proc_get_effective_thread_policy(current_thread(), TASK_POLICY_IO) >= THROTTLE_LEVEL_THROTTLED); } /* * XXX: For now, vm faults cannot be recursively disabled. If the need for * nested code that disables faults arises, the implementation can be modified * to track a disabled-count. */ OS_ALWAYS_INLINE void vm_fault_disable(void) { thread_t t = current_thread(); assert(!t->th_vm_faults_disabled); t->th_vm_faults_disabled = true; act_set_debug_assert(); } OS_ALWAYS_INLINE void vm_fault_enable(void) { thread_t t = current_thread(); assert(t->th_vm_faults_disabled); t->th_vm_faults_disabled = false; } OS_ALWAYS_INLINE bool vm_fault_get_disabled(void) { thread_t t = current_thread(); return t->th_vm_faults_disabled; } #define HARD_THROTTLE_DELAY 10000 /* 10000 us == 10 ms */ #define SOFT_THROTTLE_DELAY 200 /* 200 us == .2 ms */ #define VM_PAGE_CREATION_THROTTLE_PERIOD_SECS 6 #define VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC 20000 #define VM_STAT_DECOMPRESSIONS() \ MACRO_BEGIN \ counter_inc(&vm_statistics_decompressions); \ current_thread()->decompressions++; \ MACRO_END boolean_t current_thread_aborted(void); /* Forward declarations of internal routines. */ static kern_return_t vm_fault_wire_fast( vm_map_t map, vm_map_offset_t va, vm_prot_t prot, vm_tag_t wire_tag, vm_map_entry_t entry, pmap_t pmap, vm_map_offset_t pmap_addr, ppnum_t *physpage_p); static kern_return_t vm_fault_internal( vm_map_t map, vm_map_offset_t vaddr, vm_prot_t caller_prot, vm_tag_t wire_tag, pmap_t pmap, vm_map_offset_t pmap_addr, ppnum_t *physpage_p, vm_object_fault_info_t fault_info, vm_map_lock_ctx_t vml_ctx_for_vaddr); static void vm_fault_copy_cleanup( vm_page_t page, vm_page_t top_page); static void vm_fault_copy_dst_cleanup( vm_page_t page); #if VM_FAULT_CLASSIFY extern void vm_fault_classify(vm_object_t object, vm_object_offset_t offset, vm_prot_t fault_type); extern void vm_fault_classify_init(void); #endif unsigned long vm_pmap_enter_blocked = 0; unsigned long vm_pmap_enter_retried = 0; unsigned long vm_cs_validates = 0; unsigned long vm_cs_revalidates = 0; unsigned long vm_cs_query_modified = 0; unsigned long vm_cs_validated_dirtied = 0; unsigned long vm_cs_bitmap_validated = 0; #if CODE_SIGNING_MONITOR uint64_t vm_cs_defer_to_csm = 0; uint64_t vm_cs_defer_to_csm_not = 0; #endif /* CODE_SIGNING_MONITOR */ extern char *kdp_compressor_decompressed_page; extern addr64_t kdp_compressor_decompressed_page_paddr; extern ppnum_t kdp_compressor_decompressed_page_ppnum; struct vmrtfr { int vmrtfr_maxi; int vmrtfr_curi; int64_t vmrtf_total; vm_rtfault_record_t *vm_rtf_records; } vmrtfrs; #define VMRTF_DEFAULT_BUFSIZE (4096) #define VMRTF_NUM_RECORDS_DEFAULT (VMRTF_DEFAULT_BUFSIZE / sizeof(vm_rtfault_record_t)) TUNABLE(int, vmrtf_num_records, "vm_rtfault_records", VMRTF_NUM_RECORDS_DEFAULT); static void vm_rtfrecord_lock(void); static void vm_rtfrecord_unlock(void); static void vm_record_rtfault(thread_t, uint64_t, vm_map_offset_t, int); extern lck_grp_t vm_page_lck_grp_bucket; extern lck_attr_t vm_page_lck_attr; LCK_SPIN_DECLARE_ATTR(vm_rtfr_slock, &vm_page_lck_grp_bucket, &vm_page_lck_attr); #if DEVELOPMENT || DEBUG extern int madvise_free_debug; extern int madvise_free_debug_sometimes; #endif /* DEVELOPMENT || DEBUG */ extern int vm_pageout_protect_realtime; /* * Routine: vm_fault_init * Purpose: * Initialize our private data structures. */ __startup_func void vm_fault_init(void) { /* * Choose a value for the hard throttle threshold based on the amount of ram. The threshold is * computed as a percentage of available memory, and the percentage used is scaled inversely with * the amount of memory. The percentage runs between 10% and 35%. We use 35% for small memory systems * and reduce the value down to 10% for very large memory configurations. This helps give us a * definition of a memory hog that makes more sense relative to the amount of ram in the machine. * The formula here simply uses the number of gigabytes of ram to adjust the percentage. */ vm_hard_throttle_threshold = sane_size * (35 - MIN((int)(sane_size / (1024 * 1024 * 1024)), 25)) / 100; PE_parse_boot_argn("vm_protect_privileged_from_untrusted", &vm_protect_privileged_from_untrusted, sizeof(vm_protect_privileged_from_untrusted)); #if DEBUG || DEVELOPMENT (void)PE_parse_boot_argn("text_corruption_panic", &vmtc_panic_instead, sizeof(vmtc_panic_instead)); if (kern_feature_override(KF_MADVISE_FREE_DEBUG_OVRD)) { madvise_free_debug = 0; madvise_free_debug_sometimes = 0; } PE_parse_boot_argn("panic_object_not_alive", &panic_object_not_alive, sizeof(panic_object_not_alive)); #endif /* DEBUG || DEVELOPMENT */ } __startup_func static void vm_rtfault_record_init(void) { size_t size; vmrtf_num_records = MAX(vmrtf_num_records, 1); size = vmrtf_num_records * sizeof(vm_rtfault_record_t); vmrtfrs.vm_rtf_records = zalloc_permanent_tag(size, ZALIGN(vm_rtfault_record_t), VM_KERN_MEMORY_DIAG); vmrtfrs.vmrtfr_maxi = vmrtf_num_records - 1; } STARTUP(ZALLOC, STARTUP_RANK_MIDDLE, vm_rtfault_record_init); /* * Routine: vm_fault_cleanup * Purpose: * Clean up the result of vm_fault_page. * Results: * The paging reference for "object" is released. * "object" is unlocked. * If "top_page" is not null, "top_page" is * freed and the paging reference for the object * containing it is released. * * In/out conditions: * "object" must be locked. */ void vm_fault_cleanup( vm_object_t object, vm_page_t top_page) { thread_pri_floor_t token = { .thread = THREAD_NULL }; if (top_page != VM_PAGE_NULL && top_page->vmp_busy) { /* * We busied the top page. Apply a priority floor before dropping the * current object (and therefore the rw-lock boost) to avoid * inversions due to another thread sleeping on the top-level page. * * TODO: Register a page-worker token when busying the top-level page instead * (rdar://154313767) */ token = thread_priority_floor_start(); } vm_object_paging_end(object); vm_object_unlock(object); if (top_page != VM_PAGE_NULL) { object = VM_PAGE_OBJECT(top_page); vm_object_lock(object); VM_PAGE_FREE(top_page); vm_object_paging_end(object); vm_object_unlock(object); } if (token.thread != THREAD_NULL) { thread_priority_floor_end(&token); } } #define ALIGNED(x) (((x) & (PAGE_SIZE_64 - 1)) == 0) TUNABLE(bool, vm_page_deactivate_behind, "vm_deactivate_behind", true); TUNABLE(uint32_t, vm_page_deactivate_behind_min_resident_ratio, "vm_deactivate_behind_min_resident_ratio", 3); /* * default sizes given VM_BEHAVIOR_DEFAULT reference behavior */ #define VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW 128 #define VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER 16 /* don't make this too big... */ /* we use it to size an array on the stack */ int vm_default_behind = VM_DEFAULT_DEACTIVATE_BEHIND_WINDOW; #define MAX_SEQUENTIAL_RUN (1024 * 1024 * 1024) /* * vm_page_is_sequential * * Determine if sequential access is in progress * in accordance with the behavior specified. * Update state to indicate current access pattern. * * object must have at least the shared lock held */ static void vm_fault_is_sequential( vm_object_t object, vm_object_offset_t offset, vm_behavior_t behavior) { vm_object_offset_t last_alloc; int sequential; int orig_sequential; last_alloc = object->last_alloc; sequential = object->sequential; orig_sequential = sequential; offset = vm_object_trunc_page(offset); if (offset == last_alloc && behavior != VM_BEHAVIOR_RANDOM) { /* re-faulting in the same page: no change in behavior */ return; } switch (behavior) { case VM_BEHAVIOR_RANDOM: /* * reset indicator of sequential behavior */ sequential = 0; break; case VM_BEHAVIOR_SEQUENTIAL: if (offset && last_alloc == offset - PAGE_SIZE_64) { /* * advance indicator of sequential behavior */ if (sequential < MAX_SEQUENTIAL_RUN) { sequential += PAGE_SIZE; } } else { /* * reset indicator of sequential behavior */ sequential = 0; } break; case VM_BEHAVIOR_RSEQNTL: if (last_alloc && last_alloc == offset + PAGE_SIZE_64) { /* * advance indicator of sequential behavior */ if (sequential > -MAX_SEQUENTIAL_RUN) { sequential -= PAGE_SIZE; } } else { /* * reset indicator of sequential behavior */ sequential = 0; } break; case VM_BEHAVIOR_DEFAULT: default: if (offset && last_alloc == (offset - PAGE_SIZE_64)) { /* * advance indicator of sequential behavior */ if (sequential < 0) { sequential = 0; } if (sequential < MAX_SEQUENTIAL_RUN) { sequential += PAGE_SIZE; } } else if (last_alloc && last_alloc == (offset + PAGE_SIZE_64)) { /* * advance indicator of sequential behavior */ if (sequential > 0) { sequential = 0; } if (sequential > -MAX_SEQUENTIAL_RUN) { sequential -= PAGE_SIZE; } } else { /* * reset indicator of sequential behavior */ sequential = 0; } break; } if (sequential != orig_sequential) { if (!OSCompareAndSwap(orig_sequential, sequential, (UInt32 *)&object->sequential)) { /* * if someone else has already updated object->sequential * don't bother trying to update it or object->last_alloc */ return; } } /* * I'd like to do this with a OSCompareAndSwap64, but that * doesn't exist for PPC... however, it shouldn't matter * that much... last_alloc is maintained so that we can determine * if a sequential access pattern is taking place... if only * one thread is banging on this object, no problem with the unprotected * update... if 2 or more threads are banging away, we run the risk of * someone seeing a mangled update... however, in the face of multiple * accesses, no sequential access pattern can develop anyway, so we * haven't lost any real info. */ object->last_alloc = offset; } #if DEVELOPMENT || DEBUG SCALABLE_COUNTER_DEFINE(vm_page_deactivate_behind_count); #endif /* DEVELOPMENT || DEBUG */ /* * Policy function to identify cases where vm_fault_deactivate_behind should * avoid deactivating pages even when sequential behavior has been detected. */ static bool vm_fault_allow_deactivate_behind( vm_object_t object, vm_object_offset_t offset, vm_object_fault_info_t fault_info) { if (vm_page_deactivate_behind == FALSE) { /* We've disabled the deactivate behind mechanism. */ return false; } if (is_kernel_object(object)) { /* * Do not deactivate pages from the kernel object; they are not * intended to become pageable. */ return false; } if (vm_object_trunc_page(offset) != offset) { /* * We are dealing with an offset that is not aligned to the * system's PAGE_SIZE. In that case we will handle the * deactivation on the aligned offset and, thus, the full * PAGE_SIZE page once. This helps us avoid the redundant * deactivates and the extra faults. */ return false; } if (object->resident_page_count - object->wired_page_count < (vm_page_active_count / vm_page_deactivate_behind_min_resident_ratio)) { /* * Objects need only participate in backwards deactivation if * they are exceedingly large (i.e. their resident pages are * liable to comprise a substantially large portion of the * active queue and push out the rest of the system's working * set). */ return false; } if (fault_info->fi_change_wiring) { /* * If we're faulting as a part of a wire operation, we will * fault the pages sequentially, but we want the pages to * remain resident as they must be resident when the wire * completes. Therefore, deactivating the pages is never * desired even though the access is sequential. * * As far as we know, despite the name, this field is only used * currently for wiring pages, not unwiring them. * fi_change_wiring is not set by vm_fault_unwire when calling * vm_fault_page. 3P kexts might unwire pages by passing * change_wiring=TRUE to vm_fault, but all 1P callers of that * function today pass change_wiring=FALSE. */ return false; } return true; } /* * @func vm_fault_deactivate_behind * * @description * Determine if sequential access is in progress * in accordance with the behavior specified. If * so, compute a potential page to deactivate and * deactivate it. * * object must be locked. * * @returns the number of deactivated pages */ static uint32_t vm_fault_deactivate_behind( vm_object_t object, vm_object_offset_t offset, vm_object_fault_info_t fault_info) { uint32_t pages_in_run = 0; uint32_t max_pages_in_run = 0; int32_t sequential_run; vm_behavior_t sequential_behavior = VM_BEHAVIOR_SEQUENTIAL; vm_object_offset_t run_offset = 0; vm_object_offset_t pg_offset = 0; vm_page_t m; vm_page_t page_run[VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER]; #if TRACEFAULTPAGE dbgTrace(0xBEEF0018, (unsigned int) object, (unsigned int) vm_fault_deactivate_behind); /* (TEST/DEBUG) */ #endif if (!vm_fault_allow_deactivate_behind(object, offset, fault_info)) { return 0; } KDBG_FILTERED(VMDBG_CODE(DBG_VM_FAULT_DEACTIVATE_BEHIND) | DBG_FUNC_START, VM_KERNEL_ADDRHIDE(object), offset, fault_info->behavior); 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 (fault_info->behavior) { case VM_BEHAVIOR_RANDOM: break; case VM_BEHAVIOR_SEQUENTIAL: if (sequential_run >= (int)PAGE_SIZE) { run_offset = 0 - PAGE_SIZE_64; max_pages_in_run = 1; } break; case VM_BEHAVIOR_RSEQNTL: if (sequential_run >= (int)PAGE_SIZE) { run_offset = PAGE_SIZE_64; max_pages_in_run = 1; } break; case VM_BEHAVIOR_DEFAULT: default: { vm_object_offset_t behind = vm_default_behind * PAGE_SIZE_64; /* * determine if the run of sequential accesss has been * long enough on an object with default access behavior * to consider it for deactivation */ if ((uint64_t)sequential_run >= behind && (sequential_run % (VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER * PAGE_SIZE)) == 0) { /* * the comparisons between offset and behind are done * in this kind of odd fashion in order to prevent wrap around * at the end points */ if (sequential_behavior == VM_BEHAVIOR_SEQUENTIAL) { if (offset >= behind) { run_offset = 0 - behind; pg_offset = PAGE_SIZE_64; max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER; } } else { if (offset < -behind) { run_offset = behind; pg_offset = 0 - PAGE_SIZE_64; max_pages_in_run = VM_DEFAULT_DEACTIVATE_BEHIND_CLUSTER; } } } break;} } for (unsigned n = 0; n < max_pages_in_run; n++) { m = vm_page_lookup(object, offset + run_offset + (n * pg_offset)); if (m && !m->vmp_laundry && !m->vmp_busy && !m->vmp_no_cache && (m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && !vm_page_is_fictitious(m) && !m->vmp_absent) { page_run[pages_in_run++] = m; /* * by not passing in a pmap_flush_context we will forgo any TLB flushing, local or otherwise... * * a TLB flush isn't really needed here since at worst we'll miss the reference bit being * updated in the PTE if a remote processor still has this mapping cached in its TLB when the * new reference happens. If no futher references happen on the page after that remote TLB flushes * we'll see a clean, non-referenced page when it eventually gets pulled out of the inactive queue * by pageout_scan, which is just fine since the last reference would have happened quite far * in the past (TLB caches don't hang around for very long), and of course could just as easily * have happened before we did the deactivate_behind. */ pmap_clear_refmod_options(VM_PAGE_GET_PHYS_PAGE(m), VM_MEM_REFERENCED, PMAP_OPTIONS_NOFLUSH, (void *)NULL); } } if (pages_in_run) { vm_page_lockspin_queues(); for (unsigned n = 0; n < pages_in_run; n++) { m = page_run[n]; vm_page_deactivate_internal(m, FALSE); #if DEVELOPMENT || DEBUG counter_inc(&vm_page_deactivate_behind_count); #endif /* DEVELOPMENT || DEBUG */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0019, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ #endif } vm_page_unlock_queues(); } KDBG_FILTERED(VMDBG_CODE(DBG_VM_FAULT_DEACTIVATE_BEHIND) | DBG_FUNC_END, pages_in_run); return pages_in_run; } #if (DEVELOPMENT || DEBUG) uint32_t vm_page_creation_throttled_hard = 0; uint32_t vm_page_creation_throttled_soft = 0; uint64_t vm_page_creation_throttle_avoided = 0; #endif /* DEVELOPMENT || DEBUG */ static int vm_page_throttled(boolean_t page_kept) { clock_sec_t elapsed_sec; clock_sec_t tv_sec; clock_usec_t tv_usec; task_t curtask = current_task_early(); thread_t thread = current_thread(); if (thread->options & TH_OPT_VMPRIV) { return 0; } if (curtask && !curtask->active) { return 0; } if (thread->t_page_creation_throttled) { thread->t_page_creation_throttled = 0; if (page_kept == FALSE) { goto no_throttle; } } if (NEED_TO_HARD_THROTTLE_THIS_TASK()) { #if (DEVELOPMENT || DEBUG) thread->t_page_creation_throttled_hard++; OSAddAtomic(1, &vm_page_creation_throttled_hard); #endif /* DEVELOPMENT || DEBUG */ return HARD_THROTTLE_DELAY; } if ((vm_page_free_count < vm_page_throttle_limit || (VM_CONFIG_COMPRESSOR_IS_PRESENT && SWAPPER_NEEDS_TO_UNTHROTTLE())) && thread->t_page_creation_count > (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS * VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC)) { if (vm_page_free_wanted == 0 && vm_page_free_wanted_privileged == 0) { #if (DEVELOPMENT || DEBUG) OSAddAtomic64(1, &vm_page_creation_throttle_avoided); #endif goto no_throttle; } clock_get_system_microtime(&tv_sec, &tv_usec); elapsed_sec = tv_sec - thread->t_page_creation_time; if (elapsed_sec <= VM_PAGE_CREATION_THROTTLE_PERIOD_SECS || (thread->t_page_creation_count / elapsed_sec) >= VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC) { if (elapsed_sec >= (3 * VM_PAGE_CREATION_THROTTLE_PERIOD_SECS)) { /* * we'll reset our stats to give a well behaved app * that was unlucky enough to accumulate a bunch of pages * over a long period of time a chance to get out of * the throttled state... we reset the counter and timestamp * so that if it stays under the rate limit for the next second * it will be back in our good graces... if it exceeds it, it * will remain in the throttled state */ thread->t_page_creation_time = tv_sec; thread->t_page_creation_count = VM_PAGE_CREATION_THROTTLE_RATE_PER_SEC * (VM_PAGE_CREATION_THROTTLE_PERIOD_SECS - 1); } VM_PAGEOUT_DEBUG(vm_page_throttle_count, 1); thread->t_page_creation_throttled = 1; if (VM_CONFIG_COMPRESSOR_IS_PRESENT && HARD_THROTTLE_LIMIT_REACHED()) { #if (DEVELOPMENT || DEBUG) thread->t_page_creation_throttled_hard++; OSAddAtomic(1, &vm_page_creation_throttled_hard); #endif /* DEVELOPMENT || DEBUG */ return HARD_THROTTLE_DELAY; } else { #if (DEVELOPMENT || DEBUG) thread->t_page_creation_throttled_soft++; OSAddAtomic(1, &vm_page_creation_throttled_soft); #endif /* DEVELOPMENT || DEBUG */ return SOFT_THROTTLE_DELAY; } } thread->t_page_creation_time = tv_sec; thread->t_page_creation_count = 0; } no_throttle: thread->t_page_creation_count++; return 0; } extern boolean_t vm_pageout_running; static __attribute__((noinline, not_tail_called)) void __VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__( int throttle_delay) { /* make sure vm_pageout_scan() gets to work while we're throttled */ if (!vm_pageout_running) { thread_wakeup((event_t)&vm_page_free_wanted); } delay(throttle_delay); } /* * check for various conditions that would * prevent us from creating a ZF page... * cleanup is based on being called from vm_fault_page * * object must be locked * object == m->vmp_object */ static vm_fault_return_t vm_fault_check(vm_object_t object, vm_page_t m, vm_page_t first_m, wait_interrupt_t interruptible_state, boolean_t page_throttle) { int throttle_delay; if (object->shadow_severed || VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) { /* * Either: * 1. the shadow chain was severed, * 2. the purgeable object is volatile or empty and is marked * to fault on access while volatile. * Just have to return an error at this point */ if (m != VM_PAGE_NULL) { VM_PAGE_FREE(m); } vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); if (VM_OBJECT_PURGEABLE_FAULT_ERROR(object)) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */); } if (object->shadow_severed) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_SHADOW_SEVERED), 0 /* arg */); } return VM_FAULT_MEMORY_ERROR; } if (page_throttle == TRUE) { if ((throttle_delay = vm_page_throttled(FALSE))) { /* * we're throttling zero-fills... * treat this as if we couldn't grab a page */ if (m != VM_PAGE_NULL) { VM_PAGE_FREE(m); } vm_fault_cleanup(object, first_m); VM_DEBUG_EVENT(vmf_check_zfdelay, DBG_VM_FAULT_CHECK_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); __VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay); if (current_thread_aborted()) { thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */); return VM_FAULT_INTERRUPTED; } thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } } return VM_FAULT_SUCCESS; } /* * Clear the code signing bits on the given page_t */ static void vm_fault_cs_clear(vm_page_t m) { m->vmp_cs_validated = VMP_CS_ALL_FALSE; m->vmp_cs_tainted = VMP_CS_ALL_FALSE; m->vmp_cs_nx = VMP_CS_ALL_FALSE; } /* * Enqueues the given page on the throttled queue. */ static void vm_fault_enqueue_throttled(vm_object_t object, vm_page_t m) { if (!VM_DYNAMIC_PAGING_ENABLED() && (object->purgable == VM_PURGABLE_DENY || object->purgable == VM_PURGABLE_NONVOLATILE || object->purgable == VM_PURGABLE_VOLATILE)) { vm_page_lockspin_queues(); if (!VM_DYNAMIC_PAGING_ENABLED()) { assert(!VM_PAGE_WIRED(m)); /* * can't be on the pageout queue since we don't * have a pager to try and clean to */ vm_page_queues_remove(m, TRUE); vm_page_check_pageable_safe(m); vm_page_queue_enter(&vm_page_queue_throttled, m, vmp_pageq); m->vmp_q_state = VM_PAGE_ON_THROTTLED_Q; vm_page_throttled_count++; } vm_page_unlock_queues(); } } /* * do the work required for a zero fill faulted page, * injecting it into the correct paging queue * * object must be locked * page queue lock must NOT be held */ static void vm_fault_enqueue_zf_fault(vm_object_t object, vm_page_t m, boolean_t no_zero_fill) { /* * This is is a zero-fill page fault... * * Checking the page lock is a waste of * time; this page was absent, so * it can't be page locked by a pager. * * we also consider it undefined * with respect to instruction * execution. i.e. it is the responsibility * of higher layers to call for an instruction * sync after changing the contents and before * sending a program into this area. We * choose this approach for performance */ vm_fault_cs_clear(m); m->vmp_pmapped = TRUE; if (!no_zero_fill || !m->vmp_absent || !m->vmp_busy) { assert(!m->vmp_laundry); assert(!is_kernel_object(object)); vm_fault_enqueue_throttled(object, m); } } /* * Recovery actions for vm_fault_page */ __attribute__((always_inline)) static void vm_fault_page_release_page( vm_page_t m, /* Page to release */ bool *clear_absent_on_error /* IN/OUT */) { vm_page_wakeup_done(VM_PAGE_OBJECT(m), m); if (!VM_PAGE_PAGEABLE(m)) { vm_page_lockspin_queues(); if (*clear_absent_on_error && m->vmp_absent) { vm_page_zero_fill(m); m->vmp_absent = false; } if (!VM_PAGE_PAGEABLE(m)) { if (VM_CONFIG_COMPRESSOR_IS_ACTIVE) { vm_page_deactivate(m); } else { vm_page_activate(m); } } vm_page_unlock_queues(); } *clear_absent_on_error = false; } /* * Routine: vm_fault_page * Purpose: * Find the resident page for the virtual memory * specified by the given virtual memory object * and offset. * Additional arguments: * The required permissions for the page is given * in "fault_type". Desired permissions are included * in "protection". * fault_info is passed along to determine pagein cluster * limits... it contains the expected reference pattern, * cluster size if available, etc... * * If the desired page is known to be resident (for * example, because it was previously wired down), asserting * the "unwiring" parameter will speed the search. * * If the operation can be interrupted (by thread_abort * or thread_terminate), then the "interruptible" * parameter should be asserted. * * Results: * The page containing the proper data is returned * in "result_page". * * In/out conditions: * The source object must be locked and referenced, * and must donate one paging reference. The reference * is not affected. The paging reference and lock are * consumed. * * If the call succeeds, the object in which "result_page" * resides is left locked and holding a paging reference. * If this is not the original object, a busy page in the * original object is returned in "top_page", to prevent other * callers from pursuing this same data, along with a paging * reference for the original object. The "top_page" should * be destroyed when this guarantee is no longer required. * The "result_page" is also left busy. It is not removed * from the pageout queues. * Special Case: * A return value of VM_FAULT_SUCCESS_NO_PAGE means that the * fault succeeded but there's no VM page (i.e. the VM object * does not actually hold VM pages, but device memory or * large pages). The object is still locked and we still hold a * paging_in_progress reference. */ unsigned int vm_fault_page_blocked_access = 0; unsigned int vm_fault_page_forced_retry = 0; vm_fault_return_t vm_fault_page( /* Arguments: */ vm_object_t first_object, /* Object to begin search */ vm_object_offset_t first_offset, /* Offset into object */ vm_prot_t fault_type, /* What access is requested */ boolean_t must_be_resident,/* Must page be resident? */ boolean_t caller_lookup, /* caller looked up page */ /* Modifies in place: */ vm_prot_t *protection, /* Protection for mapping */ vm_page_t *result_page, /* Page found, if successful */ /* Returns: */ vm_page_t *top_page, /* Page in top object, if * not result_page. */ int *type_of_fault, /* if non-null, fill in with type of fault * COW, zero-fill, etc... returned in trace point */ /* More arguments: */ kern_return_t *error_code, /* code if page is in error */ boolean_t no_zero_fill, /* don't zero fill absent pages */ vm_object_fault_info_t fault_info, vm_map_lock_ctx_t vml_ctx_for_vaddr) { vm_page_t m; vm_object_t object; vm_object_offset_t offset; vm_page_t first_m; vm_object_t next_object; vm_object_t copy_object; boolean_t look_for_page; boolean_t force_fault_retry = FALSE; vm_prot_t access_required = fault_type; vm_prot_t wants_copy_flag; kern_return_t wait_result; wait_interrupt_t interruptible_state; boolean_t data_already_requested = FALSE; vm_behavior_t orig_behavior; vm_size_t orig_cluster_size; vm_fault_return_t error; int my_fault; uint32_t try_failed_count; wait_interrupt_t interruptible; /* how may fault be interrupted? */ int external_state = VM_EXTERNAL_STATE_UNKNOWN; memory_object_t pager; vm_fault_return_t retval; vm_grab_options_t grab_options; bool clear_absent_on_error = false; /* * MUST_ASK_PAGER() evaluates to TRUE if the page specified by object/offset is * marked as paged out in the compressor pager or the pager doesn't exist. * Note also that if the pager for an internal object * has not been created, the pager is not invoked regardless of the value * of MUST_ASK_PAGER(). * * PAGED_OUT() evaluates to TRUE if the page specified by the object/offset * is marked as paged out in the compressor pager. * PAGED_OUT() is used to determine if a page has already been pushed * into a copy object in order to avoid a redundant page out operation. */ #define MUST_ASK_PAGER(o, f, s) \ ((s = vm_object_compressor_pager_state_get((o), (f))) != VM_EXTERNAL_STATE_ABSENT) #define PAGED_OUT(o, f) \ (vm_object_compressor_pager_state_get((o), (f)) == VM_EXTERNAL_STATE_EXISTS) #if TRACEFAULTPAGE dbgTrace(0xBEEF0002, (unsigned int) first_object, (unsigned int) first_offset); /* (TEST/DEBUG) */ #endif interruptible = fault_info->interruptible; interruptible_state = thread_interrupt_level(interruptible); /* * INVARIANTS (through entire routine): * * 1) At all times, we must either have the object * lock or a busy page in some object to prevent * some other thread from trying to bring in * the same page. * * Note that we cannot hold any locks during the * pager access or when waiting for memory, so * we use a busy page then. * * 2) To prevent another thread from racing us down the * shadow chain and entering a new page in the top * object before we do, we must keep a busy page in * the top object while following the shadow chain. * * 3) We must increment paging_in_progress on any object * for which we have a busy page before dropping * the object lock * * 4) We leave busy pages on the pageout queues. * If the pageout daemon comes across a busy page, * it will remove the page from the pageout queues. */ object = first_object; offset = first_offset; first_m = VM_PAGE_NULL; access_required = fault_type; vm_lock_contention_event_with_excl_ctx_dev(vml_ctx_for_vaddr, &vm_fault_page_excl_count, VMLP_EVENT_LC_NONE); /* * default type of fault */ my_fault = DBG_CACHE_HIT_FAULT; thread_pri_floor_t token; bool drop_floor = false; while (TRUE) { #if TRACEFAULTPAGE dbgTrace(0xBEEF0003, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */ #endif grab_options = vm_page_grab_options_for_object(object); #if HAS_MTE if (!(grab_options & VM_PAGE_GRAB_MTE) && mteinfo_vm_tag_can_use_tag_storage((vm_tag_t)fault_info->user_tag)) { grab_options |= VM_PAGE_GRAB_ALLOW_TAG_STORAGE; } #endif /* HAS_MTE */ if (!object->alive) { /* * object is no longer valid * clean up and return error */ #if DEVELOPMENT || DEBUG printf("FBDP rdar://93769854 %s:%d object %p internal %d pager %p (%s) copy %p shadow %p alive %d terminating %d named %d ref %d shadow_severed %d\n", __FUNCTION__, __LINE__, object, object->internal, object->pager, object->pager ? object->pager->mo_pager_ops->memory_object_pager_name : "?", object->vo_copy, object->shadow, object->alive, object->terminating, object->named, os_ref_get_count_raw(&object->ref_count), object->shadow_severed); if (panic_object_not_alive) { panic("FBDP rdar://93769854 %s:%d object %p internal %d pager %p (%s) copy %p shadow %p alive %d terminating %d named %d ref %d shadow_severed %d\n", __FUNCTION__, __LINE__, object, object->internal, object->pager, object->pager ? object->pager->mo_pager_ops->memory_object_pager_name : "?", object->vo_copy, object->shadow, object->alive, object->terminating, object->named, os_ref_get_count_raw(&object->ref_count), object->shadow_severed); } #endif /* DEVELOPMENT || DEBUG */ vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_NOT_ALIVE), 0 /* arg */); return VM_FAULT_MEMORY_ERROR; } if (!object->pager_created && object->phys_contiguous) { /* * A physically-contiguous object without a pager: * must be a "large page" object. We do not deal * with VM pages for this object. */ caller_lookup = FALSE; m = VM_PAGE_NULL; goto phys_contig_object; } if (object->blocked_access) { /* * Access to this VM object has been blocked. * Replace our "paging_in_progress" reference with * a "activity_in_progress" reference and wait for * access to be unblocked. */ caller_lookup = FALSE; /* no longer valid after sleep */ vm_object_activity_begin(object); vm_object_paging_end(object); vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_page_excl_blocked_obj_count, VMLP_EVENT_LC_VM_FAULT_PAGE_EXCL_BLOCKED_OBJ); while (object->blocked_access) { vm_object_sleep(object, VM_OBJECT_EVENT_UNBLOCKED, THREAD_UNINT, LCK_SLEEP_EXCLUSIVE); } vm_fault_page_blocked_access++; vm_object_paging_begin(object); vm_object_activity_end(object); } /* * See whether the page at 'offset' is resident */ if (caller_lookup == TRUE) { /* * The caller has already looked up the page * and gave us the result in "result_page". * We can use this for the first lookup but * it loses its validity as soon as we unlock * the object. */ m = *result_page; caller_lookup = FALSE; /* no longer valid after that */ } else { m = vm_page_lookup(object, vm_object_trunc_page(offset)); } #if TRACEFAULTPAGE dbgTrace(0xBEEF0004, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ #endif if (m != VM_PAGE_NULL) { if (m->vmp_busy) { /* * The page is being brought in, * wait for it and then retry. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0005, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ #endif if (fault_info->fi_no_sleep) { /* Caller has requested not to sleep on busy pages */ vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_BUSY; } vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_page_excl_busy_count, VMLP_EVENT_LC_VM_FAULT_PAGE_EXCL_BUSY); wait_result = vm_page_sleep(object, m, interruptible, LCK_SLEEP_DEFAULT); if (wait_result != THREAD_AWAKENED) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); if (wait_result == THREAD_RESTART) { return VM_FAULT_RETRY; } else { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_BUSYPAGE_WAIT_INTERRUPTED), 0 /* arg */); return VM_FAULT_INTERRUPTED; } } continue; } if (m->vmp_laundry) { m->vmp_free_when_done = FALSE; if (!m->vmp_cleaning) { vm_pageout_steal_laundry(m, FALSE); } } vm_object_lock_assert_exclusive(VM_PAGE_OBJECT(m)); if (vm_page_is_guard(m)) { /* * Guard page: off limits ! */ if (fault_type == VM_PROT_NONE) { /* * The fault is not requesting any * access to the guard page, so it must * be just to wire or unwire it. * Let's pretend it succeeded... */ m->vmp_busy = TRUE; *result_page = m; assert(first_m == VM_PAGE_NULL); *top_page = first_m; if (type_of_fault) { *type_of_fault = DBG_GUARD_FAULT; } thread_interrupt_level(interruptible_state); return VM_FAULT_SUCCESS; } else { /* * The fault requests access to the * guard page: let's deny that ! */ vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_GUARDPAGE_FAULT), 0 /* arg */); return VM_FAULT_MEMORY_ERROR; } } if (m->vmp_error) { /* * The page is in error, give up now. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0006, (unsigned int) m, (unsigned int) error_code); /* (TEST/DEBUG) */ #endif if (error_code) { *error_code = KERN_MEMORY_ERROR; } VM_PAGE_FREE(m); vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PAGE_HAS_ERROR), 0 /* arg */); return VM_FAULT_MEMORY_ERROR; } if (m->vmp_restart) { /* * The pager wants us to restart * at the top of the chain, * typically because it has moved the * page to another pager, then do so. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0007, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ #endif VM_PAGE_FREE(m); vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PAGE_HAS_RESTART), 0 /* arg */); return VM_FAULT_RETRY; } if (m->vmp_absent) { /* * The page isn't busy, but is absent, * therefore it's deemed "unavailable". * * Remove the non-existent page (unless it's * in the top object) and move on down to the * next object (if there is one). */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0008, (unsigned int) m, (unsigned int) object->shadow); /* (TEST/DEBUG) */ #endif next_object = object->shadow; if (next_object == VM_OBJECT_NULL) { /* * Absent page at bottom of shadow * chain; zero fill the page we left * busy in the first object, and free * the absent page. */ assert(!must_be_resident); /* * check for any conditions that prevent * us from creating a new zero-fill page * vm_fault_check will do all of the * fault cleanup in the case of an error condition * including resetting the thread_interrupt_level */ error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE); if (error != VM_FAULT_SUCCESS) { return error; } if (object != first_object) { /* * free the absent page we just found */ VM_PAGE_FREE(m); /* * drop reference and lock on current object */ vm_object_paging_end(object); vm_object_unlock(object); /* * grab the original page we * 'soldered' in place and * retake lock on 'first_object' */ m = first_m; first_m = VM_PAGE_NULL; object = first_object; offset = first_offset; vm_object_lock(object); } else { /* * we're going to use the absent page we just found * so convert it to a 'busy' page */ m->vmp_absent = FALSE; m->vmp_busy = TRUE; } if (fault_info->mark_zf_absent && no_zero_fill == TRUE) { m->vmp_absent = TRUE; clear_absent_on_error = true; } /* * zero-fill the page and put it on * the correct paging queue */ if (no_zero_fill) { my_fault = DBG_NZF_PAGE_FAULT; } else { vm_page_zero_fill(m); my_fault = DBG_ZERO_FILL_FAULT; } vm_fault_enqueue_zf_fault(object, m, no_zero_fill); break; } else { if (must_be_resident) { vm_object_paging_end(object); } else if (object != first_object) { vm_object_paging_end(object); VM_PAGE_FREE(m); } else { first_m = m; m->vmp_absent = FALSE; m->vmp_busy = TRUE; vm_page_lockspin_queues(); vm_page_queues_remove(m, FALSE); vm_page_unlock_queues(); } offset += object->vo_shadow_offset; fault_info->lo_offset += object->vo_shadow_offset; fault_info->hi_offset += object->vo_shadow_offset; access_required = VM_PROT_READ; vm_object_lock(next_object); vm_object_unlock(object); object = next_object; vm_object_paging_begin(object); /* * reset to default type of fault */ my_fault = DBG_CACHE_HIT_FAULT; continue; } } if ((m->vmp_cleaning) && ((object != first_object) || (object->vo_copy != VM_OBJECT_NULL)) && (fault_type & VM_PROT_WRITE)) { /* * This is a copy-on-write fault that will * cause us to revoke access to this page, but * this page is in the process of being cleaned * in a clustered pageout. We must wait until * the cleaning operation completes before * revoking access to the original page, * otherwise we might attempt to remove a * wired mapping. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0009, (unsigned int) m, (unsigned int) offset); /* (TEST/DEBUG) */ #endif /* * take an extra ref so that object won't die */ vm_object_reference_locked(object); vm_fault_cleanup(object, first_m); vm_object_lock(object); assert(os_ref_get_count_raw(&object->ref_count) > 0); m = vm_page_lookup(object, vm_object_trunc_page(offset)); if (m != VM_PAGE_NULL && m->vmp_cleaning) { vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_page_excl_clean_count, VMLP_EVENT_LC_VM_FAULT_PAGE_EXCL_CLEAN); wait_result = vm_page_sleep(object, m, interruptible, LCK_SLEEP_UNLOCK); vm_object_deallocate(object); goto backoff; } else { vm_object_unlock(object); vm_object_deallocate(object); thread_interrupt_level(interruptible_state); return VM_FAULT_RETRY; } } if (type_of_fault == NULL && (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) && !(fault_info != NULL && fault_info->stealth)) { /* * If we were passed a non-NULL pointer for * "type_of_fault", than we came from * vm_fault... we'll let it deal with * this condition, since it * needs to see m->vmp_speculative to correctly * account the pageins, otherwise... * take it off the speculative queue, we'll * let the caller of vm_fault_page deal * with getting it onto the correct queue * * If the caller specified in fault_info that * it wants a "stealth" fault, we also leave * the page in the speculative queue. */ vm_page_lockspin_queues(); if (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) { vm_page_queues_remove(m, FALSE); } vm_page_unlock_queues(); } assert(object == VM_PAGE_OBJECT(m)); if (object->code_signed) { /* * CODE SIGNING: * We just paged in a page from a signed * memory object but we don't need to * validate it now. We'll validate it if * when it gets mapped into a user address * space for the first time or when the page * gets copied to another object as a result * of a copy-on-write. */ } /* * We mark the page busy and leave it on * the pageout queues. If the pageout * deamon comes across it, then it will * remove the page from the queue, but not the object */ #if TRACEFAULTPAGE dbgTrace(0xBEEF000B, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ #endif assert(!m->vmp_busy); assert(!m->vmp_absent); m->vmp_busy = TRUE; break; } /* * we get here when there is no page present in the object at * the offset we're interested in... we'll allocate a page * at this point if the pager associated with * this object can provide the data or we're the top object... * object is locked; m == NULL */ if (must_be_resident) { if (fault_type == VM_PROT_NONE && is_kernel_object(object)) { /* * We've been called from vm_fault_unwire() * while removing a map entry that was allocated * with KMA_KOBJECT and KMA_VAONLY. This page * is not present and there's nothing more to * do here (nothing to unwire). */ vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_ERROR; } goto dont_look_for_page; } /* Don't expect to fault pages into the kernel object. */ assert(!is_kernel_object(object)); look_for_page = (object->pager_created && (MUST_ASK_PAGER(object, offset, external_state) == TRUE)); #if TRACEFAULTPAGE dbgTrace(0xBEEF000C, (unsigned int) look_for_page, (unsigned int) object); /* (TEST/DEBUG) */ #endif if (!look_for_page && object == first_object && !object->phys_contiguous) { vmpi_flags_t flags = VMPI_NONE; /* * Allocate a new page for this object/offset pair as a placeholder */ m = vm_page_grab_options(grab_options); #if TRACEFAULTPAGE dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ #endif if (m == VM_PAGE_NULL) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } if (fault_info && fault_info->batch_pmap_op == TRUE) { flags |= VMPI_BATCH_PMAP_OP; } vm_page_insert_internal(m, object, vm_object_trunc_page(offset), VM_KERN_MEMORY_NONE, flags, NULL); } if (look_for_page) { kern_return_t rc; int my_fault_type; /* * If the memory manager is not ready, we * cannot make requests. */ if (!object->pager_ready) { #if TRACEFAULTPAGE dbgTrace(0xBEEF000E, (unsigned int) 0, (unsigned int) 0); /* (TEST/DEBUG) */ #endif if (m != VM_PAGE_NULL) { VM_PAGE_FREE(m); } /* * take an extra ref so object won't die */ vm_object_reference_locked(object); vm_fault_cleanup(object, first_m); vm_object_lock(object); assert(os_ref_get_count_raw(&object->ref_count) > 0); if (!object->pager_ready) { vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_page_excl_pager_not_ready_count, VMLP_EVENT_LC_VM_FAULT_PAGE_EXCL_PAGER_NOT_READY); wait_result = vm_object_sleep(object, VM_OBJECT_EVENT_PAGER_READY, interruptible, LCK_SLEEP_UNLOCK); vm_object_deallocate(object); goto backoff; } else { vm_object_unlock(object); vm_object_deallocate(object); thread_interrupt_level(interruptible_state); return VM_FAULT_RETRY; } } if (!object->internal && !object->phys_contiguous && object->paging_in_progress > vm_object_pagein_throttle) { /* * If there are too many outstanding page * requests pending on this external object, we * wait for them to be resolved now. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0010, (unsigned int) m, (unsigned int) 0); /* (TEST/DEBUG) */ #endif if (m != VM_PAGE_NULL) { VM_PAGE_FREE(m); } /* * take an extra ref so object won't die */ vm_object_reference_locked(object); vm_fault_cleanup(object, first_m); vm_object_lock(object); assert(os_ref_get_count_raw(&object->ref_count) > 0); if (object->paging_in_progress >= vm_object_pagein_throttle) { wait_result = vm_object_paging_throttle_wait(object, interruptible); vm_object_unlock(object); vm_object_deallocate(object); goto backoff; } else { vm_object_unlock(object); vm_object_deallocate(object); thread_interrupt_level(interruptible_state); return VM_FAULT_RETRY; } } if (object->internal) { int compressed_count_delta; vm_compressor_options_t c_flags = 0; vmpi_flags_t flags = VMPI_NONE; assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); if (m == VM_PAGE_NULL) { /* * Allocate a new page for this object/offset pair as a placeholder */ m = vm_page_grab_options(grab_options); #if TRACEFAULTPAGE dbgTrace(0xBEEF000D, (unsigned int) m, (unsigned int) object); /* (TEST/DEBUG) */ #endif if (m == VM_PAGE_NULL) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } if (fault_info && fault_info->batch_pmap_op == TRUE) { flags |= VMPI_BATCH_PMAP_OP; } m->vmp_absent = TRUE; vm_page_insert_internal(m, object, vm_object_trunc_page(offset), VM_KERN_MEMORY_NONE, flags, NULL); } assert(m->vmp_busy); m->vmp_absent = TRUE; pager = object->pager; assert(object->paging_in_progress > 0); page_worker_token_t pw_token; #if PAGE_SLEEP_WITH_INHERITOR page_worker_register_worker((event_t)m, &pw_token); #endif /* PAGE_SLEEP_WITH_INHERITOR */ vm_object_unlock(object); #if HAS_MTE if (vm_object_is_mte_mappable(object)) { c_flags |= C_MTE; } #endif /* HAS_MTE */ rc = vm_compressor_pager_get( pager, offset + object->paging_offset, VM_PAGE_GET_PHYS_PAGE(m), &my_fault_type, c_flags, &compressed_count_delta); if (type_of_fault == NULL) { int throttle_delay; /* * we weren't called from vm_fault, so we * need to apply page creation throttling * do it before we re-acquire any locks */ if (my_fault_type == DBG_COMPRESSOR_FAULT) { if ((throttle_delay = vm_page_throttled(TRUE))) { VM_DEBUG_EVENT(vmf_compressordelay, DBG_VM_FAULT_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 1, 0); __VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay); } } } vm_object_lock(object); assert(object->paging_in_progress > 0); vm_compressor_pager_count( pager, compressed_count_delta, FALSE, /* shared_lock */ object); switch (rc) { case KERN_SUCCESS: m->vmp_absent = FALSE; m->vmp_dirty = TRUE; if (!HAS_DEFAULT_CACHEABILITY(object->wimg_bits & VM_WIMG_MASK)) { /* * If the page is not cacheable, * we can't let its contents * linger in the data cache * after the decompression. */ pmap_sync_page_attributes_phys( VM_PAGE_GET_PHYS_PAGE(m)); } else { m->vmp_written_by_kernel = TRUE; } #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES if ((fault_type & VM_PROT_WRITE) == 0) { vm_object_lock_assert_exclusive(object); vm_page_lockspin_queues(); m->vmp_unmodified_ro = true; vm_page_unlock_queues(); os_atomic_inc(&compressor_ro_uncompressed, relaxed); *protection &= ~VM_PROT_WRITE; } #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */ /* * If the object is purgeable, its * owner's purgeable ledgers have been * updated in vm_page_insert() but the * page was also accounted for in a * "compressed purgeable" ledger, so * update that now. */ if (((object->purgable != VM_PURGABLE_DENY) || object->vo_ledger_tag) && (object->vo_owner != NULL)) { /* * One less compressed * purgeable/tagged page. */ if (compressed_count_delta) { vm_object_owner_compressed_update( object, -1); } } break; case KERN_MEMORY_FAILURE: m->vmp_unusual = TRUE; m->vmp_error = TRUE; m->vmp_absent = FALSE; break; case KERN_MEMORY_ERROR: assert(m->vmp_absent); break; default: panic("vm_fault_page(): unexpected " "error %d from " "vm_compressor_pager_get()\n", rc); } vm_page_wakeup_done_with_inheritor(object, m, &pw_token); rc = KERN_SUCCESS; goto data_requested; } my_fault_type = DBG_PAGEIN_FAULT; if (m != VM_PAGE_NULL) { VM_PAGE_FREE(m); m = VM_PAGE_NULL; } #if TRACEFAULTPAGE dbgTrace(0xBEEF0012, (unsigned int) object, (unsigned int) 0); /* (TEST/DEBUG) */ #endif /* * It's possible someone called vm_object_destroy while we weren't * holding the object lock. If that has happened, then bail out * here. */ pager = object->pager; if (pager == MEMORY_OBJECT_NULL) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); static const enum vm_subsys_error_codes object_destroy_errors[VM_OBJECT_DESTROY_MAX + 1] = { [VM_OBJECT_DESTROY_UNKNOWN_REASON] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER, [VM_OBJECT_DESTROY_UNMOUNT] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER_UNMOUNT, [VM_OBJECT_DESTROY_FORCED_UNMOUNT] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER_FORCED_UNMOUNT, [VM_OBJECT_DESTROY_UNGRAFT] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER_UNGRAFT, [VM_OBJECT_DESTROY_PAGER] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER_DEALLOC_PAGER, [VM_OBJECT_DESTROY_RECLAIM] = KDBG_TRIAGE_VM_OBJECT_NO_PAGER_RECLAIM, }; enum vm_subsys_error_codes kdbg_code = object_destroy_errors[(vm_object_destroy_reason_t)object->no_pager_reason]; ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, kdbg_code), 0 /* arg */); return VM_FAULT_MEMORY_ERROR; } /* * We have an absent page in place for the faulting offset, * so we can release the object lock. */ if (object->object_is_shared_cache || pager->mo_pager_ops == &dyld_pager_ops) { token = thread_priority_floor_start(); /* * A non-native shared cache object might * be getting set up in parallel with this * fault and so we can't assume that this * check will be valid after we drop the * object lock below. * * FIXME: This should utilize @c page_worker_register_worker() * (rdar://153586539) */ drop_floor = true; } vm_object_unlock(object); /* * If this object uses a copy_call strategy, * and we are interested in a copy of this object * (having gotten here only by following a * shadow chain), then tell the memory manager * via a flag added to the desired_access * parameter, so that it can detect a race * between our walking down the shadow chain * and its pushing pages up into a copy of * the object that it manages. */ if (object->copy_strategy == MEMORY_OBJECT_COPY_CALL && object != first_object) { wants_copy_flag = VM_PROT_WANTS_COPY; } else { wants_copy_flag = VM_PROT_NONE; } if (object->vo_copy == first_object) { /* * if we issue the memory_object_data_request in * this state, we are subject to a deadlock with * the underlying filesystem if it is trying to * shrink the file resulting in a push of pages * into the copy object... that push will stall * on the placeholder page, and if the pushing thread * is holding a lock that is required on the pagein * path (such as a truncate lock), we'll deadlock... * to avoid this potential deadlock, we throw away * our placeholder page before calling memory_object_data_request * and force this thread to retry the vm_fault_page after * we have issued the I/O. the second time through this path * we will find the page already in the cache (presumably still * busy waiting for the I/O to complete) and then complete * the fault w/o having to go through memory_object_data_request again */ assert(first_m != VM_PAGE_NULL); assert(VM_PAGE_OBJECT(first_m) == first_object); vm_object_lock(first_object); VM_PAGE_FREE(first_m); vm_object_paging_end(first_object); vm_object_unlock(first_object); first_m = VM_PAGE_NULL; force_fault_retry = TRUE; vm_fault_page_forced_retry++; } if (data_already_requested == TRUE) { orig_behavior = fault_info->behavior; orig_cluster_size = fault_info->cluster_size; fault_info->behavior = VM_BEHAVIOR_RANDOM; fault_info->cluster_size = PAGE_SIZE; } /* * Call the memory manager to retrieve the data. */ rc = memory_object_data_request( pager, vm_object_trunc_page(offset) + object->paging_offset, PAGE_SIZE, access_required | wants_copy_flag, (memory_object_fault_info_t)fault_info); if (data_already_requested == TRUE) { fault_info->behavior = orig_behavior; fault_info->cluster_size = orig_cluster_size; } else { data_already_requested = TRUE; } DTRACE_VM2(maj_fault, int, 1, (uint64_t *), NULL); #if TRACEFAULTPAGE dbgTrace(0xBEEF0013, (unsigned int) object, (unsigned int) rc); /* (TEST/DEBUG) */ #endif vm_object_lock(object); if (drop_floor) { thread_priority_floor_end(&token); drop_floor = false; } data_requested: if (rc != ERR_SUCCESS) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_NO_DATA), 0 /* arg */); if (rc == MACH_SEND_INTERRUPTED) { return VM_FAULT_INTERRUPTED; } else if (rc == KERN_ALREADY_WAITING) { return VM_FAULT_BUSY; } else { return VM_FAULT_MEMORY_ERROR; } } else { clock_sec_t tv_sec; clock_usec_t tv_usec; if (my_fault_type == DBG_PAGEIN_FAULT) { clock_get_system_microtime(&tv_sec, &tv_usec); current_thread()->t_page_creation_time = tv_sec; current_thread()->t_page_creation_count = 0; } } if ((interruptible != THREAD_UNINT) && (current_thread()->sched_flags & TH_SFLAG_ABORT)) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */); return VM_FAULT_INTERRUPTED; } if (force_fault_retry == TRUE) { vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_RETRY; } if (m == VM_PAGE_NULL && object->phys_contiguous) { /* * No page here means that the object we * initially looked up was "physically * contiguous" (i.e. device memory). However, * with Virtual VRAM, the object might not * be backed by that device memory anymore, * so we're done here only if the object is * still "phys_contiguous". * Otherwise, if the object is no longer * "phys_contiguous", we need to retry the * page fault against the object's new backing * store (different memory object). */ phys_contig_object: assert(object->copy_strategy == MEMORY_OBJECT_COPY_NONE); assert(object == first_object); goto done; } /* * potentially a pagein fault * if we make it through the state checks * above, than we'll count it as such */ my_fault = my_fault_type; /* * Retry with same object/offset, since new data may * be in a different page (i.e., m is meaningless at * this point). */ continue; } dont_look_for_page: /* * We get here if the object has no pager, or an existence map * exists and indicates the page isn't present on the pager * or we're unwiring a page. If a pager exists, but there * is no existence map, then the m->vmp_absent case above handles * the ZF case when the pager can't provide the page */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0014, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ #endif if (object == first_object) { first_m = m; } else { assert(m == VM_PAGE_NULL); } next_object = object->shadow; if (next_object == VM_OBJECT_NULL) { /* * we've hit the bottom of the shadown chain, * fill the page in the top object with zeros. */ assert(!must_be_resident); if (object != first_object) { vm_object_paging_end(object); vm_object_unlock(object); object = first_object; offset = first_offset; vm_object_lock(object); } m = first_m; assert(VM_PAGE_OBJECT(m) == object); first_m = VM_PAGE_NULL; /* * check for any conditions that prevent * us from creating a new zero-fill page * vm_fault_check will do all of the * fault cleanup in the case of an error condition * including resetting the thread_interrupt_level */ error = vm_fault_check(object, m, first_m, interruptible_state, (type_of_fault == NULL) ? TRUE : FALSE); if (error != VM_FAULT_SUCCESS) { return error; } if (m == VM_PAGE_NULL) { m = vm_page_grab_options(grab_options | (no_zero_fill ? VM_PAGE_GRAB_OPTIONS_NONE : VM_PAGE_GRAB_ZERO_FILL)); if (m == VM_PAGE_NULL) { vm_fault_cleanup(object, VM_PAGE_NULL); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } vm_page_insert(m, object, vm_object_trunc_page(offset)); } else if (!no_zero_fill) { vm_page_zero_fill(m); } if (fault_info->mark_zf_absent && no_zero_fill == TRUE) { m->vmp_absent = TRUE; clear_absent_on_error = true; } vm_fault_enqueue_zf_fault(object, m, no_zero_fill); if (no_zero_fill) { my_fault = DBG_NZF_PAGE_FAULT; } else { my_fault = DBG_ZERO_FILL_FAULT; } break; } else { /* * Move on to the next object. Lock the next * object before unlocking the current one. */ if ((object != first_object) || must_be_resident) { vm_object_paging_end(object); } offset += object->vo_shadow_offset; fault_info->lo_offset += object->vo_shadow_offset; fault_info->hi_offset += object->vo_shadow_offset; access_required = VM_PROT_READ; vm_object_lock(next_object); vm_object_unlock(object); object = next_object; vm_object_paging_begin(object); } } /* * PAGE HAS BEEN FOUND. * * This page (m) is: * busy, so that we can play with it; * not absent, so that nobody else will fill it; * possibly eligible for pageout; * * The top-level page (first_m) is: * VM_PAGE_NULL if the page was found in the * top-level object; * busy, not absent, and ineligible for pageout. * * The current object (object) is locked. A paging * reference is held for the current and top-level * objects. */ #if TRACEFAULTPAGE dbgTrace(0xBEEF0015, (unsigned int) object, (unsigned int) m); /* (TEST/DEBUG) */ #endif #if EXTRA_ASSERTIONS assert(m->vmp_busy && !m->vmp_absent); assert((first_m == VM_PAGE_NULL) || (first_m->vmp_busy && !first_m->vmp_absent && !first_m->vmp_active && !first_m->vmp_inactive && !first_m->vmp_secluded)); #endif /* EXTRA_ASSERTIONS */ /* * If the page is being written, but isn't * already owned by the top-level object, * we have to copy it into a new page owned * by the top-level object. */ if (object != first_object) { #if TRACEFAULTPAGE dbgTrace(0xBEEF0016, (unsigned int) object, (unsigned int) fault_type); /* (TEST/DEBUG) */ #endif if (fault_type & VM_PROT_WRITE) { vm_page_t copy_m; /* * We only really need to copy if we * want to write it. */ assert(!must_be_resident); /* * If we try to collapse first_object at this * point, we may deadlock when we try to get * the lock on an intermediate object (since we * have the bottom object locked). We can't * unlock the bottom object, because the page * we found may move (by collapse) if we do. * * Instead, we first copy the page. Then, when * we have no more use for the bottom object, * we unlock it and try to collapse. * * Note that we copy the page even if we didn't * need to... that's the breaks. */ /* * Allocate a page for the copy */ copy_m = vm_page_grab_options(grab_options); if (copy_m == VM_PAGE_NULL) { vm_fault_page_release_page(m, &clear_absent_on_error); vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } vm_page_copy(m, copy_m); /* * If another map is truly sharing this * page with us, we have to flush all * uses of the original page, since we * can't distinguish those which want the * original from those which need the * new copy. * * XXXO If we know that only one map has * access to this page, then we could * avoid the pmap_disconnect() call. */ if (m->vmp_pmapped) { pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); } if (m->vmp_clustered) { VM_PAGE_COUNT_AS_PAGEIN(m); VM_PAGE_CONSUME_CLUSTERED(m); } assert(!m->vmp_cleaning); /* * We no longer need the old page or object. */ vm_fault_page_release_page(m, &clear_absent_on_error); /* * This check helps with marking the object as having a sequential pattern * Normally we'll miss doing this below because this fault is about COW to * the first_object i.e. bring page in from disk, push to object above but * don't update the file object's sequential pattern. */ if (object->internal == FALSE) { vm_fault_is_sequential(object, offset, fault_info->behavior); } vm_object_paging_end(object); vm_object_unlock(object); my_fault = DBG_COW_FAULT; counter_inc(&vm_statistics_cow_faults); DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL); counter_inc(¤t_task()->cow_faults); object = first_object; offset = first_offset; vm_object_lock(object); /* * get rid of the place holder * page that we soldered in earlier */ VM_PAGE_FREE(first_m); first_m = VM_PAGE_NULL; /* * and replace it with the * page we just copied into */ assert(copy_m->vmp_busy); vm_page_insert(copy_m, object, vm_object_trunc_page(offset)); SET_PAGE_DIRTY(copy_m, TRUE); m = copy_m; /* * Now that we've gotten the copy out of the * way, let's try to collapse the top object. * But we have to play ugly games with * paging_in_progress to do that... */ vm_object_paging_end(object); vm_object_collapse(object, vm_object_trunc_page(offset), TRUE); vm_object_paging_begin(object); } else { *protection &= (~VM_PROT_WRITE); } } /* * Now check whether the page needs to be pushed into the * copy object. The use of asymmetric copy on write for * shared temporary objects means that we may do two copies to * satisfy the fault; one above to get the page from a * shadowed object, and one here to push it into the copy. */ try_failed_count = 0; while ((copy_object = first_object->vo_copy) != VM_OBJECT_NULL) { vm_object_offset_t copy_offset; vm_page_t copy_m; #if TRACEFAULTPAGE dbgTrace(0xBEEF0017, (unsigned int) copy_object, (unsigned int) fault_type); /* (TEST/DEBUG) */ #endif /* * If the page is being written, but hasn't been * copied to the copy-object, we have to copy it there. */ if ((fault_type & VM_PROT_WRITE) == 0) { *protection &= ~VM_PROT_WRITE; break; } /* * If the page was guaranteed to be resident, * we must have already performed the copy. */ if (must_be_resident) { break; } /* * Try to get the lock on the copy_object. */ if (!vm_object_lock_try(copy_object)) { vm_object_unlock(object); try_failed_count++; mutex_pause(try_failed_count); /* wait a bit */ vm_object_lock(object); continue; } try_failed_count = 0; /* * Make another reference to the copy-object, * to keep it from disappearing during the * copy. */ vm_object_reference_locked(copy_object); /* * Does the page exist in the copy? */ copy_offset = first_offset - copy_object->vo_shadow_offset; copy_offset = vm_object_trunc_page(copy_offset); if (copy_object->vo_size <= copy_offset) { /* * Copy object doesn't cover this page -- do nothing. */ ; } else if ((copy_m = vm_page_lookup(copy_object, copy_offset)) != VM_PAGE_NULL) { /* * Page currently exists in the copy object */ if (copy_m->vmp_busy) { /* * If the page is being brought * in, wait for it and then retry. */ vm_fault_page_release_page(m, &clear_absent_on_error); /* * take an extra ref so object won't die */ vm_object_reference_locked(copy_object); vm_object_unlock(copy_object); vm_fault_cleanup(object, first_m); vm_object_lock(copy_object); vm_object_lock_assert_exclusive(copy_object); os_ref_release_live_locked_raw(©_object->ref_count, &vm_object_refgrp); copy_m = vm_page_lookup(copy_object, copy_offset); if (copy_m != VM_PAGE_NULL && copy_m->vmp_busy) { vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_page_excl_busy_copy_count, VMLP_EVENT_LC_VM_FAULT_PAGE_EXCL_BUSY_COPY); wait_result = vm_page_sleep(copy_object, copy_m, interruptible, LCK_SLEEP_UNLOCK); vm_object_deallocate(copy_object); goto backoff; } else { vm_object_unlock(copy_object); vm_object_deallocate(copy_object); thread_interrupt_level(interruptible_state); return VM_FAULT_RETRY; } } } else if (!PAGED_OUT(copy_object, copy_offset)) { /* * If PAGED_OUT is TRUE, then the page used to exist * in the copy-object, and has already been paged out. * We don't need to repeat this. If PAGED_OUT is * FALSE, then either we don't know (!pager_created, * for example) or it hasn't been paged out. * (VM_EXTERNAL_STATE_UNKNOWN||VM_EXTERNAL_STATE_ABSENT) * We must copy the page to the copy object. * * Allocate a page for the copy */ copy_m = vm_page_grab_options(grab_options); if (copy_m == VM_PAGE_NULL) { vm_fault_page_release_page(m, &clear_absent_on_error); vm_object_lock_assert_exclusive(copy_object); os_ref_release_live_locked_raw(©_object->ref_count, &vm_object_refgrp); vm_object_unlock(copy_object); vm_fault_cleanup(object, first_m); thread_interrupt_level(interruptible_state); return VM_FAULT_MEMORY_SHORTAGE; } /* * Must copy page into copy-object. */ vm_page_insert(copy_m, copy_object, copy_offset); vm_page_copy(m, copy_m); /* * If the old page was in use by any users * of the copy-object, it must be removed * from all pmaps. (We can't know which * pmaps use it.) */ if (m->vmp_pmapped) { pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); } if (m->vmp_clustered) { VM_PAGE_COUNT_AS_PAGEIN(m); VM_PAGE_CONSUME_CLUSTERED(m); } /* * If there's a pager, then immediately * page out this page, using the "initialize" * option. Else, we use the copy. */ if ((!copy_object->pager_ready) || vm_object_compressor_pager_state_get(copy_object, copy_offset) == VM_EXTERNAL_STATE_ABSENT ) { vm_page_lockspin_queues(); assert(!m->vmp_cleaning); vm_page_activate(copy_m); vm_page_unlock_queues(); SET_PAGE_DIRTY(copy_m, TRUE); vm_page_wakeup_done(copy_object, copy_m); } else { assert(copy_m->vmp_busy == TRUE); assert(!m->vmp_cleaning); /* * dirty is protected by the object lock */ SET_PAGE_DIRTY(copy_m, TRUE); /* * The page is already ready for pageout: * not on pageout queues and busy. * Unlock everything except the * copy_object itself. */ vm_object_unlock(object); /* * Write the page to the copy-object, * flushing it from the kernel. */ vm_pageout_initialize_page(copy_m); /* * Since the pageout may have * temporarily dropped the * copy_object's lock, we * check whether we'll have * to deallocate the hard way. */ if ((copy_object->shadow != object) || (os_ref_get_count_raw(©_object->ref_count) == 1)) { vm_object_unlock(copy_object); vm_object_deallocate(copy_object); vm_object_lock(object); continue; } /* * Pick back up the old object's * lock. [It is safe to do so, * since it must be deeper in the * object tree.] */ vm_object_lock(object); } /* * Because we're pushing a page upward * in the object tree, we must restart * any faults that are waiting here. * [Note that this is an expansion of * vm_page_wakeup() that uses the THREAD_RESTART * wait result]. Can't turn off the page's * busy bit because we're not done with it. */ if (m->vmp_wanted) { m->vmp_wanted = FALSE; thread_wakeup_with_result((event_t) m, THREAD_RESTART); } } /* * The reference count on copy_object must be * at least 2: one for our extra reference, * and at least one from the outside world * (we checked that when we last locked * copy_object). */ vm_object_lock_assert_exclusive(copy_object); os_ref_release_live_locked_raw(©_object->ref_count, &vm_object_refgrp); vm_object_unlock(copy_object); break; } done: *result_page = m; *top_page = first_m; if (m != VM_PAGE_NULL) { assert(VM_PAGE_OBJECT(m) == object); retval = VM_FAULT_SUCCESS; if (my_fault == DBG_PAGEIN_FAULT) { VM_PAGE_COUNT_AS_PAGEIN(m); if (object->internal) { my_fault = DBG_PAGEIND_FAULT; } else { my_fault = DBG_PAGEINV_FAULT; } /* * evaluate access pattern and update state * vm_fault_deactivate_behind depends on the * state being up to date */ vm_fault_is_sequential(object, offset, fault_info->behavior); vm_fault_deactivate_behind(object, offset, fault_info); } else if (type_of_fault == NULL && my_fault == DBG_CACHE_HIT_FAULT) { /* * we weren't called from vm_fault, so handle the * accounting here for hits in the cache */ if (m->vmp_clustered) { VM_PAGE_COUNT_AS_PAGEIN(m); VM_PAGE_CONSUME_CLUSTERED(m); } vm_fault_is_sequential(object, offset, fault_info->behavior); vm_fault_deactivate_behind(object, offset, fault_info); } else if (my_fault == DBG_COMPRESSOR_FAULT || my_fault == DBG_COMPRESSOR_SWAPIN_FAULT) { VM_STAT_DECOMPRESSIONS(); } if (type_of_fault) { *type_of_fault = my_fault; } } else { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_SUCCESS_NO_PAGE), 0 /* arg */); retval = VM_FAULT_SUCCESS_NO_VM_PAGE; assert(first_m == VM_PAGE_NULL); assert(object == first_object); } thread_interrupt_level(interruptible_state); #if TRACEFAULTPAGE dbgTrace(0xBEEF001A, (unsigned int) VM_FAULT_SUCCESS, 0); /* (TEST/DEBUG) */ #endif return retval; backoff: thread_interrupt_level(interruptible_state); if (wait_result == THREAD_INTERRUPTED) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */); return VM_FAULT_INTERRUPTED; } return VM_FAULT_RETRY; } #if MACH_ASSERT && (XNU_PLATFORM_WatchOS || __x86_64__) #define PANIC_ON_CS_KILLED_DEFAULT true #else #define PANIC_ON_CS_KILLED_DEFAULT false #endif static TUNABLE(bool, panic_on_cs_killed, "panic_on_cs_killed", PANIC_ON_CS_KILLED_DEFAULT); extern int proc_selfpid(void); extern char *proc_name_address(struct proc *p); extern const char *proc_best_name(struct proc *); unsigned long cs_enter_tainted_rejected = 0; unsigned long cs_enter_tainted_accepted = 0; /* * CODE SIGNING: * When soft faulting a page, we have to validate the page if: * 1. the page is being mapped in user space * 2. the page hasn't already been found to be "tainted" * 3. the page belongs to a code-signed object * 4. the page has not been validated yet or has been mapped for write. */ static bool vm_fault_cs_need_validation( pmap_t pmap, vm_page_t page, vm_object_t page_obj, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset) { if (pmap == kernel_pmap) { /* 1 - not user space */ return false; } if (!page_obj->code_signed) { /* 3 - page does not belong to a code-signed object */ return false; } if (fault_page_size == PAGE_SIZE) { /* looking at the whole page */ assertf(fault_phys_offset == 0, "fault_page_size 0x%llx fault_phys_offset 0x%llx\n", (uint64_t)fault_page_size, (uint64_t)fault_phys_offset); if (page->vmp_cs_tainted == VMP_CS_ALL_TRUE) { /* 2 - page is all tainted */ return false; } if (page->vmp_cs_validated == VMP_CS_ALL_TRUE && !page->vmp_wpmapped) { /* 4 - already fully validated and never mapped writable */ return false; } } else { /* looking at a specific sub-page */ if (VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) { /* 2 - sub-page was already marked as tainted */ return false; } if (VMP_CS_VALIDATED(page, fault_page_size, fault_phys_offset) && !page->vmp_wpmapped) { /* 4 - already validated and never mapped writable */ return false; } } /* page needs to be validated */ return true; } static bool vm_fault_cs_page_immutable( vm_page_t m, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_prot_t prot __unused) { if (VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) /*&& ((prot) & VM_PROT_EXECUTE)*/) { return true; } return false; } static bool vm_fault_cs_page_nx( vm_page_t m, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset) { return VMP_CS_NX(m, fault_page_size, fault_phys_offset); } /* * Check if the page being entered into the pmap violates code signing. */ static kern_return_t vm_fault_cs_check_violation( bool cs_bypass, vm_object_t object, vm_page_t m, pmap_t pmap, vm_prot_t prot, vm_prot_t caller_prot, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_object_fault_info_t fault_info, bool map_is_switched, bool map_is_switch_protected, bool *cs_violation) { #if !CODE_SIGNING_MONITOR #pragma unused(caller_prot) #pragma unused(fault_info) #endif /* !CODE_SIGNING_MONITOR */ int cs_enforcement_enabled; if (!cs_bypass && vm_fault_cs_need_validation(pmap, m, object, fault_page_size, fault_phys_offset)) { vm_object_lock_assert_exclusive(object); if (VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset)) { vm_cs_revalidates++; } /* VM map is locked, so 1 ref will remain on VM object - * so no harm if vm_page_validate_cs drops the object lock */ #if CODE_SIGNING_MONITOR if (fault_info->csm_associated && csm_enabled() && !VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) && !VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset) && !VMP_CS_NX(m, fault_page_size, fault_phys_offset) && (prot & VM_PROT_EXECUTE) && (caller_prot & VM_PROT_EXECUTE)) { /* * When we have a code signing monitor, the monitor will evaluate the code signature * for any executable page mapping. No need for the VM to also validate the page. * In the code signing monitor we trust :) */ vm_cs_defer_to_csm++; } else { vm_cs_defer_to_csm_not++; vm_page_validate_cs(m, fault_page_size, fault_phys_offset); } #else /* CODE_SIGNING_MONITOR */ vm_page_validate_cs(m, fault_page_size, fault_phys_offset); #endif /* CODE_SIGNING_MONITOR */ } /* If the map is switched, and is switch-protected, we must protect * some pages from being write-faulted: immutable pages because by * definition they may not be written, and executable pages because that * would provide a way to inject unsigned code. * If the page is immutable, we can simply return. However, we can't * immediately determine whether a page is executable anywhere. But, * we can disconnect it everywhere and remove the executable protection * from the current map. We do that below right before we do the * PMAP_ENTER. */ if (pmap == kernel_pmap) { /* kernel fault: cs_enforcement does not apply */ cs_enforcement_enabled = 0; } else { cs_enforcement_enabled = pmap_get_vm_map_cs_enforced(pmap); } if (cs_enforcement_enabled && map_is_switched && map_is_switch_protected && vm_fault_cs_page_immutable(m, fault_page_size, fault_phys_offset, prot) && (prot & VM_PROT_WRITE)) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAILED_IMMUTABLE_PAGE_WRITE), 0 /* arg */); return KERN_CODESIGN_ERROR; } if (cs_enforcement_enabled && vm_fault_cs_page_nx(m, fault_page_size, fault_phys_offset) && (prot & VM_PROT_EXECUTE)) { if (cs_debug) { printf("page marked to be NX, not letting it be mapped EXEC\n"); } ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAILED_NX_PAGE_EXEC_MAPPING), 0 /* arg */); return KERN_CODESIGN_ERROR; } /* A page could be tainted, or pose a risk of being tainted later. * Check whether the receiving process wants it, and make it feel * the consequences (that hapens in cs_invalid_page()). * For CS Enforcement, two other conditions will * cause that page to be tainted as well: * - pmapping an unsigned page executable - this means unsigned code; * - writeable mapping of a validated page - the content of that page * can be changed without the kernel noticing, therefore unsigned * code can be created */ if (cs_bypass) { /* code-signing is bypassed */ *cs_violation = FALSE; } else if (VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset)) { /* tainted page */ *cs_violation = TRUE; } else if (!cs_enforcement_enabled) { /* no further code-signing enforcement */ *cs_violation = FALSE; } else if (vm_fault_cs_page_immutable(m, fault_page_size, fault_phys_offset, prot) && ((prot & VM_PROT_WRITE) || m->vmp_wpmapped)) { /* * The page should be immutable, but is in danger of being * modified. * This is the case where we want policy from the code * directory - is the page immutable or not? For now we have * to assume that code pages will be immutable, data pages not. * We'll assume a page is a code page if it has a code directory * and we fault for execution. * That is good enough since if we faulted the code page for * writing in another map before, it is wpmapped; if we fault * it for writing in this map later it will also be faulted for * executing at the same time; and if we fault for writing in * another map later, we will disconnect it from this pmap so * we'll notice the change. */ *cs_violation = TRUE; } else if (!VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) && (prot & VM_PROT_EXECUTE) #if CODE_SIGNING_MONITOR /* * Executable pages will be validated by the code signing monitor. If the * code signing monitor is turned off, then this is a code-signing violation. */ && !csm_enabled() #endif /* CODE_SIGNING_MONITOR */ ) { *cs_violation = TRUE; } else { *cs_violation = FALSE; } return KERN_SUCCESS; } /* * Handles a code signing violation by either rejecting the page or forcing a disconnect. * @param must_disconnect This value will be set to true if the caller must disconnect * this page. * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault. */ static kern_return_t vm_fault_cs_handle_violation( vm_object_t object, vm_page_t m, pmap_t pmap, vm_prot_t prot, vm_map_offset_t vaddr, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, bool map_is_switched, bool map_is_switch_protected, bool *must_disconnect) { #if !MACH_ASSERT #pragma unused(pmap) #pragma unused(map_is_switch_protected) #endif /* !MACH_ASSERT */ /* * We will have a tainted page. Have to handle the special case * of a switched map now. If the map is not switched, standard * procedure applies - call cs_invalid_page(). * If the map is switched, the real owner is invalid already. * There is no point in invalidating the switching process since * it will not be executing from the map. So we don't call * cs_invalid_page() in that case. */ boolean_t reject_page, cs_killed; kern_return_t kr; if (map_is_switched) { assert(pmap == vm_map_pmap(current_thread()->map)); assert(!(prot & VM_PROT_WRITE) || (map_is_switch_protected == FALSE)); reject_page = FALSE; } else { if (cs_debug > 5) { printf("vm_fault: signed: %s validate: %s tainted: %s wpmapped: %s prot: 0x%x\n", object->code_signed ? "yes" : "no", VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) ? "yes" : "no", VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset) ? "yes" : "no", m->vmp_wpmapped ? "yes" : "no", (int)prot); } reject_page = cs_invalid_page((addr64_t) vaddr, &cs_killed); } if (reject_page) { /* reject the invalid page: abort the page fault */ int pid; const char *procname; task_t task; vm_object_t file_object, shadow; vm_object_offset_t file_offset; char *pathname, *filename; vm_size_t pathname_len, filename_len; boolean_t truncated_path; #define __PATH_MAX 1024 struct timespec mtime, cs_mtime; int shadow_depth; os_reason_t codesigning_exit_reason = OS_REASON_NULL; kr = KERN_CODESIGN_ERROR; cs_enter_tainted_rejected++; /* get process name and pid */ procname = "?"; task = current_task(); pid = proc_selfpid(); if (get_bsdtask_info(task) != NULL) { procname = proc_name_address(get_bsdtask_info(task)); } /* get file's VM object */ file_object = object; file_offset = m->vmp_offset; for (shadow = file_object->shadow, shadow_depth = 0; shadow != VM_OBJECT_NULL; shadow = file_object->shadow, shadow_depth++) { vm_object_lock_shared(shadow); if (file_object != object) { vm_object_unlock(file_object); } file_offset += file_object->vo_shadow_offset; file_object = shadow; } mtime.tv_sec = 0; mtime.tv_nsec = 0; cs_mtime.tv_sec = 0; cs_mtime.tv_nsec = 0; /* get file's pathname and/or filename */ pathname = NULL; filename = NULL; pathname_len = 0; filename_len = 0; truncated_path = FALSE; /* no pager -> no file -> no pathname, use "<nil>" in that case */ if (file_object->pager != NULL) { pathname = kalloc_data(__PATH_MAX * 2, Z_WAITOK); if (pathname) { pathname[0] = '\0'; pathname_len = __PATH_MAX; filename = pathname + pathname_len; filename_len = __PATH_MAX; if (vnode_pager_get_object_name(file_object->pager, pathname, pathname_len, filename, filename_len, &truncated_path) == KERN_SUCCESS) { /* safety first... */ pathname[__PATH_MAX - 1] = '\0'; filename[__PATH_MAX - 1] = '\0'; vnode_pager_get_object_mtime(file_object->pager, &mtime, &cs_mtime); } else { kfree_data(pathname, __PATH_MAX * 2); pathname = NULL; filename = NULL; pathname_len = 0; filename_len = 0; truncated_path = FALSE; } } } printf("CODE SIGNING: process %d[%s]: " "rejecting invalid page at address 0x%llx " "from offset 0x%llx in file \"%s%s%s\" " "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) " "(signed:%d validated:%d tainted:%d nx:%d " "wpmapped:%d dirty:%d depth:%d)\n", pid, procname, (addr64_t) vaddr, file_offset, (pathname ? pathname : "<nil>"), (truncated_path ? "/.../" : ""), (truncated_path ? filename : ""), cs_mtime.tv_sec, cs_mtime.tv_nsec, ((cs_mtime.tv_sec == mtime.tv_sec && cs_mtime.tv_nsec == mtime.tv_nsec) ? "==" : "!="), mtime.tv_sec, mtime.tv_nsec, object->code_signed, VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset), VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset), VMP_CS_NX(m, fault_page_size, fault_phys_offset), m->vmp_wpmapped, m->vmp_dirty, shadow_depth); /* * We currently only generate an exit reason if cs_invalid_page directly killed a process. If cs_invalid_page * did not kill the process (more the case on desktop), vm_fault_enter will not satisfy the fault and whether the * process dies is dependent on whether there is a signal handler registered for SIGSEGV and how that handler * will deal with the segmentation fault. */ if (cs_killed) { KDBG(BSDDBG_CODE(DBG_BSD_PROC, BSD_PROC_EXITREASON_CREATE) | DBG_FUNC_NONE, pid, OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE); codesigning_exit_reason = os_reason_create(OS_REASON_CODESIGNING, CODESIGNING_EXIT_REASON_INVALID_PAGE); if (codesigning_exit_reason == NULL) { printf("vm_fault_enter: failed to allocate codesigning exit reason\n"); } else { mach_vm_address_t data_addr = 0; struct codesigning_exit_reason_info *ceri = NULL; uint32_t reason_buffer_size_estimate = kcdata_estimate_required_buffer_size(1, sizeof(*ceri)); if (os_reason_alloc_buffer_noblock(codesigning_exit_reason, reason_buffer_size_estimate)) { printf("vm_fault_enter: failed to allocate buffer for codesigning exit reason\n"); } else { if (KERN_SUCCESS == kcdata_get_memory_addr(&codesigning_exit_reason->osr_kcd_descriptor, EXIT_REASON_CODESIGNING_INFO, sizeof(*ceri), &data_addr)) { ceri = (struct codesigning_exit_reason_info *)data_addr; static_assert(__PATH_MAX == sizeof(ceri->ceri_pathname)); ceri->ceri_virt_addr = vaddr; ceri->ceri_file_offset = file_offset; if (pathname) { strncpy((char *)&ceri->ceri_pathname, pathname, sizeof(ceri->ceri_pathname)); } else { ceri->ceri_pathname[0] = '\0'; } if (filename) { strncpy((char *)&ceri->ceri_filename, filename, sizeof(ceri->ceri_filename)); } else { ceri->ceri_filename[0] = '\0'; } ceri->ceri_path_truncated = (truncated_path ? 1 : 0); ceri->ceri_codesig_modtime_secs = cs_mtime.tv_sec; ceri->ceri_codesig_modtime_nsecs = cs_mtime.tv_nsec; ceri->ceri_page_modtime_secs = mtime.tv_sec; ceri->ceri_page_modtime_nsecs = mtime.tv_nsec; ceri->ceri_object_codesigned = (object->code_signed); ceri->ceri_page_codesig_validated = VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset); ceri->ceri_page_codesig_tainted = VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset); ceri->ceri_page_codesig_nx = VMP_CS_NX(m, fault_page_size, fault_phys_offset); ceri->ceri_page_wpmapped = (m->vmp_wpmapped); ceri->ceri_page_slid = 0; ceri->ceri_page_dirty = (m->vmp_dirty); ceri->ceri_page_shadow_depth = shadow_depth; } else { #if DEBUG || DEVELOPMENT panic("vm_fault_enter: failed to allocate kcdata for codesigning exit reason"); #else printf("vm_fault_enter: failed to allocate kcdata for codesigning exit reason\n"); #endif /* DEBUG || DEVELOPMENT */ /* Free the buffer */ os_reason_alloc_buffer_noblock(codesigning_exit_reason, 0); } } } set_thread_exit_reason(current_thread(), codesigning_exit_reason, FALSE); } if (panic_on_cs_killed && object->object_is_shared_cache) { char *tainted_contents; vm_map_offset_t src_vaddr; src_vaddr = (vm_map_offset_t) phystokv((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m) << PAGE_SHIFT); tainted_contents = kalloc_data(PAGE_SIZE, Z_WAITOK); bcopy((const char *)src_vaddr, tainted_contents, PAGE_SIZE); printf("CODE SIGNING: tainted page %p phys 0x%x phystokv 0x%llx copied to %p\n", m, VM_PAGE_GET_PHYS_PAGE(m), (uint64_t)src_vaddr, tainted_contents); panic("CODE SIGNING: process %d[%s]: " "rejecting invalid page (phys#0x%x) at address 0x%llx " "from offset 0x%llx in file \"%s%s%s\" " "(cs_mtime:%lu.%ld %s mtime:%lu.%ld) " "(signed:%d validated:%d tainted:%d nx:%d" "wpmapped:%d dirty:%d depth:%d)\n", pid, procname, VM_PAGE_GET_PHYS_PAGE(m), (addr64_t) vaddr, file_offset, (pathname ? pathname : "<nil>"), (truncated_path ? "/.../" : ""), (truncated_path ? filename : ""), cs_mtime.tv_sec, cs_mtime.tv_nsec, ((cs_mtime.tv_sec == mtime.tv_sec && cs_mtime.tv_nsec == mtime.tv_nsec) ? "==" : "!="), mtime.tv_sec, mtime.tv_nsec, object->code_signed, VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset), VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset), VMP_CS_NX(m, fault_page_size, fault_phys_offset), m->vmp_wpmapped, m->vmp_dirty, shadow_depth); } if (file_object != object) { vm_object_unlock(file_object); } if (pathname_len != 0) { kfree_data(pathname, __PATH_MAX * 2); pathname = NULL; filename = NULL; } } else { /* proceed with the invalid page */ kr = KERN_SUCCESS; if (!VMP_CS_VALIDATED(m, fault_page_size, fault_phys_offset) && !object->code_signed) { /* * This page has not been (fully) validated but * does not belong to a code-signed object * so it should not be forcefully considered * as tainted. * We're just concerned about it here because * we've been asked to "execute" it but that * does not mean that it should cause other * accesses to fail. * This happens when a debugger sets a * breakpoint and we then execute code in * that page. Marking the page as "tainted" * would cause any inspection tool ("leaks", * "vmmap", "CrashReporter", ...) to get killed * due to code-signing violation on that page, * even though they're just reading it and not * executing from it. */ } else { /* * Page might have been tainted before or not; * now it definitively is. If the page wasn't * tainted, we must disconnect it from all * pmaps later, to force existing mappings * through that code path for re-consideration * of the validity of that page. */ if (!VMP_CS_TAINTED(m, fault_page_size, fault_phys_offset)) { *must_disconnect = TRUE; VMP_CS_SET_TAINTED(m, fault_page_size, fault_phys_offset, TRUE); } } cs_enter_tainted_accepted++; } if (kr != KERN_SUCCESS) { if (cs_debug) { printf("CODESIGNING: vm_fault_enter(0x%llx): " "*** INVALID PAGE ***\n", (long long)vaddr); } #if !SECURE_KERNEL if (cs_enforcement_panic) { panic("CODESIGNING: panicking on invalid page"); } #endif } return kr; } /* * Check that the code signature is valid for the given page being inserted into * the pmap. * * @param must_disconnect This value will be set to true if the caller must disconnect * this page. * @return If this function does not return KERN_SUCCESS, the caller must abort the page fault. */ static kern_return_t vm_fault_validate_cs( bool cs_bypass, vm_object_t object, vm_page_t m, pmap_t pmap, vm_map_offset_t vaddr, vm_prot_t prot, vm_prot_t caller_prot, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_object_fault_info_t fault_info, bool *must_disconnect) { bool map_is_switched, map_is_switch_protected, cs_violation; kern_return_t kr; /* Validate code signature if necessary. */ map_is_switched = ((pmap != vm_map_pmap(current_task()->map)) && (pmap == vm_map_pmap(current_thread()->map))); map_is_switch_protected = current_thread()->map->switch_protect; kr = vm_fault_cs_check_violation(cs_bypass, object, m, pmap, prot, caller_prot, fault_page_size, fault_phys_offset, fault_info, map_is_switched, map_is_switch_protected, &cs_violation); if (kr != KERN_SUCCESS) { return kr; } if (cs_violation) { kr = vm_fault_cs_handle_violation(object, m, pmap, prot, vaddr, fault_page_size, fault_phys_offset, map_is_switched, map_is_switch_protected, must_disconnect); } return kr; } static inline int vm_fault_type_for_tracing(bool need_copy_on_read, int type_of_fault) { if (need_copy_on_read && type_of_fault == DBG_COW_FAULT) { return DBG_COR_FAULT; } return type_of_fault; } static inline uint32_t vm_fault_trace_eventid(vm_object_t obj) { uint32_t code = 0; if (obj->internal) { code = VM_REAL_FAULT_ADDR_INTERNAL; } else if (obj->object_is_shared_cache) { code = VM_REAL_FAULT_ADDR_SHAREDCACHE; } else { code = VM_REAL_FAULT_ADDR_EXTERNAL; } return MACHDBG_CODE(DBG_MACH_WORKINGSET, code); } static void vm_fault_trace( uint64_t va, uint64_t trace_va, vm_page_t page, vm_object_fault_info_t fault_info, vm_object_t object, vm_prot_t caller_prot, int type_of_fault) { uint32_t eventid = vm_fault_trace_eventid(object); KDBG_RELEASE( eventid, trace_va, (fault_info->user_tag << 16) | (caller_prot << 8) | type_of_fault, page->vmp_offset, get_current_unique_pid()); DTRACE_VM6(real_fault, vm_map_offset_t, va, vm_map_offset_t, page->vmp_offset, int, eventid, int, caller_prot, int, type_of_fault, int, fault_info->user_tag); telemetry_vm_fault(va, type_of_fault, TM_VMF_BASE); } /* * Enqueue the page on the appropriate paging queue. */ static void vm_fault_enqueue_page( vm_object_t object, vm_page_t m, bool wired, bool change_wiring, vm_tag_t wire_tag, bool no_cache, int *type_of_fault, kern_return_t kr) { assert((m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) || object != compressor_object); boolean_t page_queues_locked = FALSE; boolean_t previously_pmapped = m->vmp_pmapped; #define __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED() \ MACRO_BEGIN \ if (! page_queues_locked) { \ page_queues_locked = TRUE; \ vm_page_lockspin_queues(); \ } \ MACRO_END #define __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED() \ MACRO_BEGIN \ if (page_queues_locked) { \ page_queues_locked = FALSE; \ vm_page_unlock_queues(); \ } \ MACRO_END vm_page_update_special_state(m); if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { /* * Compressor pages are neither wired * nor pageable and should never change. */ assert(object == compressor_object); } else if (change_wiring) { __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); if (wired) { if (kr == KERN_SUCCESS) { vm_page_wire(m, wire_tag, TRUE); } } else { vm_page_unwire(m, TRUE); } /* we keep the page queues lock, if we need it later */ } else { if (object->internal == TRUE) { /* * don't allow anonymous pages on * the speculative queues */ no_cache = FALSE; } if (kr != KERN_SUCCESS) { __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); vm_page_deactivate(m); /* we keep the page queues lock, if we need it later */ } else if (((m->vmp_q_state == VM_PAGE_NOT_ON_Q) || (m->vmp_q_state == VM_PAGE_ON_SPECULATIVE_Q) || (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) || ((m->vmp_q_state != VM_PAGE_ON_THROTTLED_Q) && no_cache)) && !VM_PAGE_WIRED(m)) { if (vm_page_local_q && (*type_of_fault == DBG_COW_FAULT || *type_of_fault == DBG_ZERO_FILL_FAULT)) { struct vpl *lq; uint32_t lid; assert(m->vmp_q_state == VM_PAGE_NOT_ON_Q); __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED(); vm_object_lock_assert_exclusive(object); /* * we got a local queue to stuff this * new page on... * its safe to manipulate local and * local_id at this point since we're * behind an exclusive object lock and * the page is not on any global queue. * * we'll use the current cpu number to * select the queue note that we don't * need to disable preemption... we're * going to be behind the local queue's * lock to do the real work */ lid = cpu_number(); lq = zpercpu_get_cpu(vm_page_local_q, lid); VPL_LOCK(&lq->vpl_lock); vm_page_check_pageable_safe(m); vm_page_queue_enter(&lq->vpl_queue, m, vmp_pageq); m->vmp_q_state = VM_PAGE_ON_ACTIVE_LOCAL_Q; m->vmp_local_id = (uint16_t)lid; lq->vpl_count++; if (object->internal) { lq->vpl_internal_count++; } else { lq->vpl_external_count++; } VPL_UNLOCK(&lq->vpl_lock); if (lq->vpl_count > vm_page_local_q_soft_limit) { /* * we're beyond the soft limit * for the local queue * vm_page_reactivate_local will * 'try' to take the global page * queue lock... if it can't * that's ok... we'll let the * queue continue to grow up * to the hard limit... at that * point we'll wait for the * lock... once we've got the * lock, we'll transfer all of * the pages from the local * queue to the global active * queue */ vm_page_reactivate_local(lid, FALSE, FALSE); } } else { __VM_PAGE_LOCKSPIN_QUEUES_IF_NEEDED(); /* * test again now that we hold the * page queue lock */ if (!VM_PAGE_WIRED(m)) { if (m->vmp_q_state == VM_PAGE_ON_INACTIVE_CLEANED_Q) { vm_page_queues_remove(m, FALSE); VM_PAGEOUT_DEBUG(vm_pageout_cleaned_reactivated, 1); VM_PAGEOUT_DEBUG(vm_pageout_cleaned_fault_reactivated, 1); } if (!VM_PAGE_ACTIVE_OR_INACTIVE(m) || no_cache) { /* * If this is a no_cache mapping * and the page has never been * mapped before or was * previously a no_cache page, * then we want to leave pages * in the speculative state so * that they can be readily * recycled if free memory runs * low. Otherwise the page is * activated as normal. */ if (no_cache && (!previously_pmapped || m->vmp_no_cache)) { m->vmp_no_cache = TRUE; if (m->vmp_q_state != VM_PAGE_ON_SPECULATIVE_Q) { vm_page_speculate(m, FALSE); } } else if (!VM_PAGE_ACTIVE_OR_INACTIVE(m)) { vm_page_activate(m); } } } /* we keep the page queues lock, if we need it later */ } } } /* we're done with the page queues lock, if we ever took it */ __VM_PAGE_UNLOCK_QUEUES_IF_NEEDED(); } /* * Sets the pmmpped, xpmapped, and wpmapped bits on the vm_page_t and updates accounting. * @return true if the page needs to be sync'ed via pmap_sync-page_data_physo * before being inserted into the pmap. */ static bool vm_fault_enter_set_mapped( vm_object_t object, vm_page_t m, vm_prot_t prot, vm_prot_t fault_type) { bool page_needs_sync = false; /* * NOTE: we may only hold the vm_object lock SHARED * at this point, so we need the phys_page lock to * properly serialize updating the pmapped and * xpmapped bits */ if ((prot & VM_PROT_EXECUTE) && !m->vmp_xpmapped) { ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m); pmap_lock_phys_page(phys_page); m->vmp_pmapped = TRUE; if (!m->vmp_xpmapped) { m->vmp_xpmapped = TRUE; pmap_unlock_phys_page(phys_page); if (!object->internal) { OSAddAtomic(1, &vm_page_xpmapped_external_count); } #if defined(__arm64__) page_needs_sync = true; #else if (object->internal && object->pager != NULL) { /* * This page could have been * uncompressed by the * compressor pager and its * contents might be only in * the data cache. * Since it's being mapped for * "execute" for the fist time, * make sure the icache is in * sync. */ assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); page_needs_sync = true; } #endif } else { pmap_unlock_phys_page(phys_page); } } else { if (m->vmp_pmapped == FALSE) { ppnum_t phys_page = VM_PAGE_GET_PHYS_PAGE(m); pmap_lock_phys_page(phys_page); m->vmp_pmapped = TRUE; pmap_unlock_phys_page(phys_page); } } if (fault_type & VM_PROT_WRITE) { if (m->vmp_wpmapped == FALSE) { vm_object_lock_assert_exclusive(object); if (!object->internal && object->pager) { task_update_logical_writes(current_task(), PAGE_SIZE, TASK_WRITE_DEFERRED, vnode_pager_lookup_vnode(object->pager)); } m->vmp_wpmapped = TRUE; } } return page_needs_sync; } #if HAS_MTE static bool vm_should_override_mte_cacheattr( pmap_t pmap, vm_object_t obj, __unused vm_map_address_t va, pmap_paddr_t pa) { /* * We need to ask whether _any_ tagged mapping exists for this frame, * rather than asking whether the object we're holding _now_ is tagged. * This is how we ensure that if an MTE mapping escapes into a non-MTE * context, shuffles around a bit, then comes back around to the originating * context, we'll enter it as MTE. */ if (obj != VM_OBJECT_NULL && pmap_is_tagged_page((ppnum_t)atop(pa)) && pmap->associated_vm_map_serial_id != obj->vmo_provenance) { return true; } return false; } #endif static inline kern_return_t vm_fault_pmap_validate_page( pmap_t pmap __unused, vm_page_t m __unused, vm_map_offset_t vaddr __unused, vm_prot_t prot __unused, vm_object_fault_info_t fault_info __unused, bool *page_sleep_needed) { assert(page_sleep_needed != NULL); *page_sleep_needed = false; #if CONFIG_SPTM /* * Reject the executable or debug mapping if the page is already wired for I/O. The SPTM's security * model doesn't allow us to reliably use executable pages for I/O due to both CS integrity * protections and the possibility that the pages may be dynamically retyped while wired for I/O. * This check is required to happen under the VM object lock in order to synchronize with the * complementary check on the I/O wiring path in vm_page_do_delayed_work(). */ if (__improbable((m->vmp_cleaning || VM_PAGE_IOPL_WIRED(m)) && pmap_will_retype(pmap, vaddr, VM_PAGE_GET_PHYS_PAGE(m), prot, fault_info->pmap_options | ((fault_info->fi_xnu_user_debug && !VM_PAGE_OBJECT(m)->code_signed) ? PMAP_OPTIONS_XNU_USER_DEBUG : 0), PMAP_MAPPING_TYPE_INFER))) { if (__improbable(VM_PAGE_IOPL_WIRED(m))) { vm_map_guard_exception(current_map(), vaddr, kGUARD_EXC_SEC_EXEC_ON_IOPL_PAGE); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_EXEC_ON_IOPL_PAGE), (uintptr_t)vaddr); return KERN_PROTECTION_FAILURE; } *page_sleep_needed = m->vmp_cleaning; } #endif /* CONFIG_SPTM */ return KERN_SUCCESS; } /* * wrappers for pmap_enter_options() */ kern_return_t pmap_enter_object_options_check( pmap_t pmap, vm_map_address_t virtual_address, vm_map_offset_t fault_phys_offset, vm_object_t obj, ppnum_t pn, vm_prot_t protection, vm_prot_t fault_type, boolean_t wired, unsigned int options) { unsigned int flags = 0; unsigned int extra_options = 0; if (obj->internal) { extra_options |= PMAP_OPTIONS_INTERNAL; } pmap_paddr_t physical_address = (pmap_paddr_t)ptoa(pn) + fault_phys_offset; #if HAS_MTE /* * By policy we sometimes decide to enter an MTE-capable object * as non-MTE in a particular map. * * Most notably, we enact a general policy that MTE memory which escapes its * original context will be aliased in other maps as non-MTE (aliasing back * into the originating map will result in an MTE-enabled mapping.) * * Using VM_WIMG_DEFAULT for this pmap_enter only sets the PTE values * correctly *for this mapping only* without changing the MTE-ness * of the underlying page. */ if (vm_should_override_mte_cacheattr(pmap, obj, virtual_address, physical_address)) { /* * Certain first-party actors (such as WCP and BlastDoor) are modeled untrustworthy, and should never * be allowed to receive untagged aliases to tagged memory from other actors. * If we make it this far on a pmap that should never receive untagged aliases, throw a fatal guard. */ if (pmap->restrict_receiving_aliases_to_tagged_memory) { /* Immediately send a fatal guard */ uint64_t address_to_report = 0; #if DEBUG || DEVELOPMENT /* On internal variants, report the PA we tried to alias */ address_to_report = physical_address; #endif /* DEBUG || DEVELOPMENT */ mach_exception_code_t code = 0; EXC_GUARD_ENCODE_TYPE(code, GUARD_TYPE_VIRT_MEMORY); EXC_GUARD_ENCODE_FLAVOR(code, kGUARD_EXC_SEC_SHARING_DENIED); thread_guard_violation( current_thread(), code, address_to_report, /* Sticky */ true); /* And indicate that something went wrong */ return VM_FAULT_MEMORY_ERROR; } else { assert(!(flags & VM_WIMG_MASK)); flags |= VM_WIMG_USE_DEFAULT; } } #endif /* HAS_MTE */ return pmap_enter_options_addr(pmap, virtual_address, physical_address, protection, fault_type, flags, wired, options | extra_options, NULL, PMAP_MAPPING_TYPE_INFER); } kern_return_t pmap_enter_options_check( pmap_t pmap, vm_map_address_t virtual_address, vm_map_offset_t fault_phys_offset, vm_page_t page, vm_prot_t protection, vm_prot_t fault_type, boolean_t wired, unsigned int options) { if (page->vmp_error) { return KERN_MEMORY_FAILURE; } vm_object_t obj = VM_PAGE_OBJECT(page); if (page->vmp_reusable || obj->all_reusable) { options |= PMAP_OPTIONS_REUSABLE; } assert(page->vmp_pmapped); if (fault_type & VM_PROT_WRITE) { if (pmap == kernel_pmap) { /* * The kernel sometimes needs to map a page to provide its * initial contents but that does not mean that the page is * actually dirty/modified, so let's not assert that it's been * "wpmapped". */ } else { assert(page->vmp_wpmapped); } } return pmap_enter_object_options_check( pmap, virtual_address, fault_phys_offset, obj, VM_PAGE_GET_PHYS_PAGE(page), protection, fault_type, wired, options); } kern_return_t pmap_enter_check( pmap_t pmap, vm_map_address_t virtual_address, vm_page_t page, vm_prot_t protection, vm_prot_t fault_type, boolean_t wired) { return pmap_enter_options_check(pmap, virtual_address, 0 /* fault_phys_offset */, page, protection, fault_type, wired, 0 /* options */); } /* * Try to enter the given page into the pmap. * Will retry without execute permission if the code signing monitor is enabled and * we encounter a codesigning failure on a non-execute fault. */ __mockable __static_testable kern_return_t vm_fault_attempt_pmap_enter( pmap_t pmap, vm_map_offset_t vaddr, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_page_t m, vm_prot_t *prot, vm_prot_t caller_prot, vm_prot_t fault_type, bool wired, int pmap_options) { #if !CODE_SIGNING_MONITOR #pragma unused(caller_prot) #endif /* !CODE_SIGNING_MONITOR */ kern_return_t kr; if (fault_page_size != PAGE_SIZE) { DEBUG4K_FAULT("pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x fault_type 0x%x\n", pmap, (uint64_t)vaddr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, *prot, fault_type); assertf((!(fault_phys_offset & FOURK_PAGE_MASK) && fault_phys_offset < PAGE_SIZE), "0x%llx\n", (uint64_t)fault_phys_offset); } else { assertf(fault_phys_offset == 0, "0x%llx\n", (uint64_t)fault_phys_offset); } kr = pmap_enter_options_check(pmap, vaddr, fault_phys_offset, m, *prot, fault_type, wired, pmap_options); #if CODE_SIGNING_MONITOR /* * Retry without execute permission if we encountered a codesigning * failure on a non-execute fault. This allows applications which * don't actually need to execute code to still map it for read access. */ if (kr == KERN_CODESIGN_ERROR && csm_enabled() && (*prot & VM_PROT_EXECUTE) && !(caller_prot & VM_PROT_EXECUTE)) { *prot &= ~VM_PROT_EXECUTE; kr = pmap_enter_options_check(pmap, vaddr, fault_phys_offset, m, *prot, fault_type, wired, pmap_options); } #endif /* CODE_SIGNING_MONITOR */ return kr; } /* * Enter the given page into the pmap. * The vm map must be locked shared. * The vm object must be locked exclusive, unless this is a soft fault. * For a soft fault, the object must be locked shared or exclusive. * * @param need_retry if not null, avoid making a (potentially) blocking call into * the pmap layer. When such a call would be necessary, return true in this boolean instead. */ static kern_return_t vm_fault_pmap_enter_with_object_lock( vm_object_t object, pmap_t pmap, vm_map_offset_t vaddr, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_page_t m, vm_prot_t *prot, vm_prot_t caller_prot, vm_prot_t fault_type, bool wired, int pmap_options, bool *need_retry, uint8_t *object_lock_type) { kern_return_t kr; assert(need_retry != NULL); *need_retry = false; /* * Prevent a deadlock by not * holding the object lock if we need to wait for a page in * pmap_enter() - <rdar://problem/7138958> */ kr = vm_fault_attempt_pmap_enter(pmap, vaddr, fault_page_size, fault_phys_offset, m, prot, caller_prot, fault_type, wired, pmap_options | PMAP_OPTIONS_NOWAIT); #if __x86_64__ if (kr == KERN_INVALID_ARGUMENT && pmap == PMAP_NULL && wired) { /* * Wiring a page in a pmap-less VM map: * VMware's "vmmon" kernel extension does this * to grab pages. * Let it proceed even though the PMAP_ENTER() failed. */ kr = KERN_SUCCESS; } #endif /* __x86_64__ */ if (kr == KERN_RESOURCE_SHORTAGE) { /* * We can't drop the object lock(s) here to retry the pmap_enter() * in a blocking way so that it can expand the page table as needed. * That would allow vm_object_copy_delayed() to create a new copy object * and change the copy-on-write obligations. * Our only recourse is to deal with it at a higher level where we can * drop both locks, expand the page table and retry the fault. */ *need_retry = true; vm_pmap_enter_retried++; goto done; } #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES if ((*prot & VM_PROT_WRITE) && m->vmp_unmodified_ro) { if (*object_lock_type == OBJECT_LOCK_SHARED) { boolean_t was_busy = m->vmp_busy; m->vmp_busy = TRUE; *object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { vm_object_lock(object); } if (!was_busy) { vm_page_wakeup_done(object, m); } } vm_object_lock_assert_exclusive(object); vm_page_lockspin_queues(); m->vmp_unmodified_ro = false; vm_page_unlock_queues(); os_atomic_dec(&compressor_ro_uncompressed, relaxed); vm_object_compressor_pager_state_clr(VM_PAGE_OBJECT(m), m->vmp_offset); } #else /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */ #pragma unused(object) #pragma unused(object_lock_type) #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */ done: return kr; } /* * Prepare to enter a page into the pmap by checking CS, protection bits, * and setting mapped bits on the page_t. * Does not modify the page's paging queue. * * page queue lock must NOT be held * m->vmp_object must be locked * * NOTE: m->vmp_object could be locked "shared" only if we are called * from vm_fault() as part of a soft fault. */ __static_testable kern_return_t vm_fault_enter_prepare( vm_page_t m, pmap_t pmap, vm_map_offset_t vaddr, vm_prot_t *prot, vm_prot_t caller_prot, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_prot_t fault_type, vm_object_fault_info_t fault_info, int *type_of_fault, bool *page_needs_data_sync, bool *page_needs_sleep) { kern_return_t kr; bool is_tainted = false; vm_object_t object; boolean_t cs_bypass = fault_info->cs_bypass; object = VM_PAGE_OBJECT(m); vm_object_lock_assert_held(object); #if KASAN if (pmap == kernel_pmap) { kasan_notify_address(vaddr, PAGE_SIZE); } #endif #if CODE_SIGNING_MONITOR if (csm_address_space_exempt(pmap) == KERN_SUCCESS) { cs_bypass = TRUE; } #endif LCK_MTX_ASSERT(&vm_page_queue_lock, LCK_MTX_ASSERT_NOTOWNED); if (*type_of_fault == DBG_ZERO_FILL_FAULT) { vm_object_lock_assert_exclusive(object); } else if ((fault_type & VM_PROT_WRITE) == 0 && !fault_info->fi_change_wiring && (!m->vmp_wpmapped #if VM_OBJECT_ACCESS_TRACKING || object->access_tracking #endif /* VM_OBJECT_ACCESS_TRACKING */ )) { /* * This is not a "write" fault, so we * might not have taken the object lock * exclusively and we might not be able * to update the "wpmapped" bit in * vm_fault_enter(). * Let's just grant read access to * the page for now and we'll * soft-fault again if we need write * access later... */ /* This had better not be a JIT page. */ if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, *prot)) { /* * This pmap enforces extra constraints for this set of * protections, so we can't modify them. */ if (!cs_bypass) { panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x !cs_bypass", __FUNCTION__, pmap, (uint64_t)vaddr, *prot, fault_info->pmap_options); } } else { *prot &= ~VM_PROT_WRITE; } } if (m->vmp_pmapped == FALSE) { if (m->vmp_clustered) { if (*type_of_fault == DBG_CACHE_HIT_FAULT) { /* * found it in the cache, but this * is the first fault-in of the page (m->vmp_pmapped == FALSE) * so it must have come in as part of * a cluster... account 1 pagein against it */ if (object->internal) { *type_of_fault = DBG_PAGEIND_FAULT; } else { *type_of_fault = DBG_PAGEINV_FAULT; telemetry_pagein_emit(object, m->vmp_offset); } VM_PAGE_COUNT_AS_PAGEIN(m); } else if (*type_of_fault == DBG_PAGEINV_FAULT) { /* * Fresh page-in of a clustered page (not found in cache). * This can happen when clustering is disabled or cluster_size=1. * We must emit telemetry here, otherwise single-page reads * marked as clustered would never be recorded. */ telemetry_pagein_emit(object, m->vmp_offset); } VM_PAGE_CONSUME_CLUSTERED(m); } else if (*type_of_fault == DBG_PAGEINV_FAULT) { telemetry_pagein_emit(object, m->vmp_offset); } } if (*type_of_fault != DBG_COW_FAULT) { DTRACE_VM2(as_fault, int, 1, (uint64_t *), NULL); if (pmap == kernel_pmap) { DTRACE_VM2(kernel_asflt, int, 1, (uint64_t *), NULL); } } kr = vm_fault_pmap_validate_page(pmap, m, vaddr, *prot, fault_info, page_needs_sleep); if (__improbable((kr != KERN_SUCCESS) || *page_needs_sleep)) { return kr; } kr = vm_fault_validate_cs(cs_bypass, object, m, pmap, vaddr, *prot, caller_prot, fault_page_size, fault_phys_offset, fault_info, &is_tainted); if (kr == KERN_SUCCESS) { /* * We either have a good page, or a tainted page that has been accepted by the process. * In both cases the page will be entered into the pmap. */ *page_needs_data_sync = vm_fault_enter_set_mapped(object, m, *prot, fault_type); if ((fault_type & VM_PROT_WRITE) && is_tainted) { /* * This page is tainted but we're inserting it anyways. * Since it's writeable, we need to disconnect it from other pmaps * now so those processes can take note. */ /* * We can only get here * because of the CSE logic */ assert(pmap_get_vm_map_cs_enforced(pmap)); pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(m)); /* * If we are faulting for a write, we can clear * the execute bit - that will ensure the page is * checked again before being executable, which * protects against a map switch. * This only happens the first time the page * gets tainted, so we won't get stuck here * to make an already writeable page executable. */ if (!cs_bypass) { if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, *prot)) { /* * This pmap enforces extra constraints * for this set of protections, so we * can't change the protections. */ panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x", __FUNCTION__, pmap, (uint64_t)vaddr, *prot, fault_info->pmap_options); } *prot &= ~VM_PROT_EXECUTE; } } assert(VM_PAGE_OBJECT(m) == object); #if VM_OBJECT_ACCESS_TRACKING if (object->access_tracking) { DTRACE_VM2(access_tracking, vm_map_offset_t, vaddr, int, fault_type); if (fault_type & VM_PROT_WRITE) { object->access_tracking_writes++; vm_object_access_tracking_writes++; } else { object->access_tracking_reads++; vm_object_access_tracking_reads++; } } #endif /* VM_OBJECT_ACCESS_TRACKING */ } return kr; } /* * page queue lock must NOT be held * m->vmp_object must be locked * * NOTE: m->vmp_object could be locked "shared" only if we are called * from vm_fault() as part of a soft fault. If so, we must be * careful not to modify the VM object in any way that is not * legal under a shared lock... */ kern_return_t vm_fault_enter( vm_page_t m, pmap_t pmap, vm_map_offset_t vaddr, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, vm_prot_t prot, vm_prot_t caller_prot, boolean_t wired, vm_tag_t wire_tag, vm_object_fault_info_t fault_info, bool *need_retry, int *type_of_fault, uint8_t *object_lock_type, bool *page_needs_sleep) { kern_return_t kr; vm_object_t object; bool page_needs_data_sync; vm_prot_t fault_type; int pmap_options = fault_info->pmap_options; assert(need_retry != NULL); if (vm_page_is_guard(m)) { return KERN_SUCCESS; } fault_type = fault_info->fi_change_wiring ? VM_PROT_NONE : caller_prot; assertf(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL, "m=%p", m); kr = vm_fault_enter_prepare(m, pmap, vaddr, &prot, caller_prot, fault_page_size, fault_phys_offset, fault_type, fault_info, type_of_fault, &page_needs_data_sync, page_needs_sleep); object = VM_PAGE_OBJECT(m); vm_fault_enqueue_page(object, m, wired, fault_info->fi_change_wiring, wire_tag, fault_info->no_cache, type_of_fault, kr); if (__probable((kr == KERN_SUCCESS) && !(*page_needs_sleep))) { if (page_needs_data_sync) { pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m)); } if (fault_info->fi_xnu_user_debug && !object->code_signed) { pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG; } kr = vm_fault_pmap_enter_with_object_lock(object, pmap, vaddr, fault_page_size, fault_phys_offset, m, &prot, caller_prot, fault_type, wired, pmap_options, need_retry, object_lock_type); } return kr; } kern_return_t vm_pre_fault_with_info( vm_map_t map, vm_map_offset_t vaddr, vm_prot_t prot, vm_object_fault_info_t fault_info) { assert(fault_info != NULL); if (pmap_find_phys(map->pmap, vaddr) == 0) { return vm_fault_internal(map, vaddr, /* vaddr */ prot, /* fault_type */ VM_KERN_MEMORY_NONE, /* tag - not wiring */ NULL, /* caller_pmap */ 0, /* caller_pmap_addr */ NULL, fault_info, NULL /* vml_ctx_for_addr */); } return KERN_SUCCESS; } /* * Fault on the given vaddr iff the page is not already entered in the pmap. */ kern_return_t vm_pre_fault(vm_map_offset_t vaddr, vm_prot_t prot) { struct vm_object_fault_info fault_info = { .interruptible = THREAD_UNINT, }; return vm_pre_fault_with_info(current_map(), vaddr, prot, &fault_info); } /* * Routine: vm_fault * Purpose: * Handle page faults, including pseudo-faults * used to change the wiring status of pages. * Returns: * Explicit continuations have been removed. * Implementation: * vm_fault and vm_fault_page save mucho state * in the moral equivalent of a closure. The state * structure is allocated when first entering vm_fault * and deallocated when leaving vm_fault. */ unsigned long vm_fault_collapse_total = 0; unsigned long vm_fault_collapse_skipped = 0; kern_return_t vm_fault_external( vm_map_t map, vm_map_offset_t vaddr, vm_prot_t fault_type, boolean_t change_wiring, int interruptible, pmap_t caller_pmap, vm_map_offset_t caller_pmap_addr) { struct vm_object_fault_info fault_info = { .interruptible = interruptible, .fi_change_wiring = change_wiring, }; return vm_fault_internal(map, vaddr, fault_type, change_wiring ? vm_tag_bt() : VM_KERN_MEMORY_NONE, caller_pmap, caller_pmap_addr, NULL, &fault_info, NULL); } kern_return_t vm_fault( vm_map_t map, vm_map_offset_t vaddr, vm_prot_t fault_type, boolean_t change_wiring, vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */ int interruptible, pmap_t caller_pmap, vm_map_offset_t caller_pmap_addr) { struct vm_object_fault_info fault_info = { .interruptible = interruptible, .fi_change_wiring = change_wiring, }; return vm_fault_internal(map, vaddr, fault_type, wire_tag, caller_pmap, caller_pmap_addr, NULL, &fault_info, NULL); } static boolean_t current_proc_is_privileged(void) { return csproc_get_platform_binary(current_proc()); } uint64_t vm_copied_on_read = 0; uint64_t vm_copied_on_read_kernel_map = 0; uint64_t vm_copied_on_read_platform_map = 0; /* * Unlock the context unless vm_fault_internal was already called with a lock * context held. */ static void vm_fault_unlock_ctx( vm_map_lock_ctx_t ctx, vm_map_lock_ctx_t vml_ctx_for_vaddr) { if (vml_ctx_for_vaddr == NULL) { vm_map_range_sh_unlock(ctx, NULL); } } /* * Cleanup after a vm_fault_enter. * At this point, the fault should either have failed (kr != KERN_SUCCESS) * or the page should be in the pmap and on the correct paging queue. * * Precondition: * ctx must be locked shared. * m_object must be locked. * If top_object != VM_OBJECT_NULL, it must be locked. * * Postcondition: * map will be unlocked * m_object will be unlocked * top_object will be unlocked * ctx will be unlocked */ static void vm_fault_complete( vm_object_t object, vm_object_t m_object, vm_page_t m, vm_map_offset_t offset, vm_map_offset_t trace_real_vaddr, vm_object_fault_info_t fault_info, vm_prot_t caller_prot, #if CONFIG_DTRACE vm_map_offset_t real_vaddr, #else __unused vm_map_offset_t real_vaddr, #endif /* CONFIG_DTRACE */ int type_of_fault, bool need_retry, kern_return_t kr, ppnum_t *physpage_p, vm_prot_t prot, vm_object_t top_object, boolean_t need_collapse, vm_map_offset_t cur_offset, vm_prot_t fault_type, vm_object_t *written_on_object, memory_object_t *written_on_pager, vm_object_offset_t *written_on_offset, vm_map_lock_ctx_t ctx, vm_map_lock_ctx_t vml_ctx_for_vaddr) { vm_object_lock_assert_held(m_object); if (top_object != VM_OBJECT_NULL) { vm_object_lock_assert_held(top_object); } vm_fault_trace( real_vaddr, trace_real_vaddr, m, fault_info, m_object, caller_prot, type_of_fault); if (kr == KERN_SUCCESS && physpage_p != NULL) { /* for vm_map_wire_and_extract() */ *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); if (prot & VM_PROT_WRITE) { vm_object_lock_assert_exclusive(m_object); m->vmp_dirty = TRUE; } } if (top_object != VM_OBJECT_NULL) { /* * It's safe to drop the top object * now that we've done our * vm_fault_enter(). Any other fault * in progress for that virtual * address will either find our page * and translation or put in a new page * and translation. */ vm_object_unlock(top_object); top_object = VM_OBJECT_NULL; } if (need_collapse == TRUE) { vm_object_collapse(object, vm_object_trunc_page(offset), TRUE); } if (!need_retry && (type_of_fault == DBG_PAGEIND_FAULT || type_of_fault == DBG_PAGEINV_FAULT || type_of_fault == DBG_CACHE_HIT_FAULT)) { /* * evaluate access pattern and update state * vm_fault_deactivate_behind depends on the * state being up to date */ vm_fault_is_sequential(m_object, cur_offset, fault_info->behavior); vm_fault_deactivate_behind(m_object, cur_offset, fault_info); } /* * If the hash insertion was delayed, do it now */ if (!m->vmp_hashed) { #if MACH_ASSERT assert(object->delayed_page_insert); object->delayed_page_insert = false; #endif /* MACH_ASSERT */ vm_page_hash_insert(m, object, m->vmp_offset); } /* * That's it, clean up and return. */ if (m->vmp_busy) { vm_object_lock_assert_exclusive(m_object); vm_page_wakeup_done(m_object, m); } if (!need_retry && !m_object->internal && (fault_type & VM_PROT_WRITE)) { vm_object_paging_begin(m_object); assert3p(*written_on_object, ==, VM_OBJECT_NULL); *written_on_object = m_object; *written_on_pager = m_object->pager; *written_on_offset = m_object->paging_offset + m->vmp_offset; } vm_object_unlock(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); } uint64_t vm_fault_resilient_media_initiate = 0; uint64_t vm_fault_resilient_media_retry = 0; uint64_t vm_fault_resilient_media_proceed = 0; uint64_t vm_fault_resilient_media_release = 0; uint64_t vm_fault_resilient_media_abort1 = 0; uint64_t vm_fault_resilient_media_abort2 = 0; #if MACH_ASSERT int vm_fault_resilient_media_inject_error1_rate = 0; int vm_fault_resilient_media_inject_error1 = 0; int vm_fault_resilient_media_inject_error2_rate = 0; int vm_fault_resilient_media_inject_error2 = 0; int vm_fault_resilient_media_inject_error3_rate = 0; int vm_fault_resilient_media_inject_error3 = 0; #endif /* MACH_ASSERT */ /* * vm_map_lookup_object_and_lock_entry: * * Finds the VM object, offset, and * protection for a given virtual address in the * specified map, assuming a page fault of the * type specified. * * Returns the (object, offset, protection) for * this address, whether it is wired down, and whether * this map has the only reference to the data in question. * * The entry will be locked by this function in shared mode. * * If a lookup is requested with "write protection" * specified, the map may be changed to perform virtual * copying operations, although the data referenced will * remain the same. * * On input, var_map is the map to search for vaddr in. * On output, var_map is the map containing the entry we currently have locked * real_map is the map containing the pmap we want to use * * vml_ctx_for_vaddr is either NULL or a valid ctx: * It can either be NULL, in which case ctx is filled and used as the context * If it is not NULL, it must be a context that already has the vaddr * we care about locked. It must be a valid context for fault, e.g. * have already done object allocation/stablization if needed. * * If fault_info is provided, then the information is * initialized according to the properties of the map entry * NB: only properties of the entry are initialized, * namely: * - user_tag * - pmap_options * - iokit_acct * - behavior * - lo_offset * - hi_offset * - no_cache * - cs_bypass * - csm_associated * - resilient_media * - vme_xnu_user_debug * - vme_no_copy_on_read * - used_for_tpro */ kern_return_t vm_map_lookup_object_and_lock_entry( vm_map_t *var_map, /* IN/OUT */ vm_map_offset_t vaddr, vm_prot_t fault_type, vm_object_t *object, /* OUT */ vm_map_entry_t *entry, /* OUT */ vm_object_offset_t *offset, /* OUT */ vm_prot_t *out_prot, /* OUT */ boolean_t *wired, /* OUT */ vm_object_fault_info_t fault_info, /* OUT */ vm_map_t *real_map, /* OUT */ vm_map_lock_ctx_t ctx, vm_map_lock_ctx_t vml_ctx_for_vaddr, bool try_lock_entry) { vm_map_t original_map = *var_map; vm_prot_t prot; boolean_t mask_protections; boolean_t force_copy; vm_map_size_t fault_page_mask; kern_return_t kr; /* * VM_PROT_MASK means that the caller wants us to use "fault_type" * as a mask against the mapping's actual protections, not as an * absolute value. */ mask_protections = (fault_type & VM_PROT_IS_MASK) ? TRUE : FALSE; force_copy = (fault_type & VM_PROT_COPY) ? TRUE : FALSE; fault_type &= VM_PROT_ALL; *real_map = original_map; fault_page_mask = MIN(VM_MAP_PAGE_MASK(original_map), PAGE_MASK); vaddr = VM_MAP_TRUNC_PAGE(vaddr, fault_page_mask); bool would_resolve_cow = force_copy || (fault_type & VM_PROT_WRITE); vmrl_sh_flags_t flags = VMRL_SH_STREAM_NO_HOLES | VMRL_SH_DESCEND_INTO_CONSTANT | VMRL_SH_NO_MIN_MAX_CHECK; if (would_resolve_cow) { flags |= VMRL_SH_RESOLVE_COW_AND_OBJ; } else { flags |= VMRL_SH_VMO_ALLOCATE; } if (try_lock_entry) { flags |= VMRL_SH_TRY_LOCK_ENTRY; } if (vml_ctx_for_vaddr != NULL) { /* * We already have a lock for this address. Just take that entry * from the lock context directly. */ *entry = vml_ctx_for_vaddr->vmlc_vme; assert((*entry)->vme_start <= vaddr && (*entry)->vme_end > vaddr); vmrl_sh_flags_t minimum_flags = flags; /* Check that the parent lock context has the flags we would require */ minimum_flags &= ~(VMRL_SH_NO_MIN_MAX_CHECK | VMRL_SH_DESCEND_INTO_CONSTANT | VMRL_SH_STREAM | VMRL_SH_TRY_LOCK_ENTRY); if (vml_ctx_for_vaddr->__vmlc_flags & VMRL_RESOLVE_COW_AND_OBJ) { minimum_flags &= ~(VMRL_VMO_ALLOCATE); } assert(!(vml_ctx_for_vaddr->__vmlc_flags & VMRL_NO_DESCEND_TRANSPARENT)); assert((vml_ctx_for_vaddr->__vmlc_flags & minimum_flags) == minimum_flags); /* * If an entry is passed, it needs to already be stabilized. * We don't want to have missed applying flags fault would need */ if ((*entry)->is_sub_map || (*entry)->needs_copy) { panic("Entry(%p) passed to vm_map_lookup_object_and_lock_entry is not fully stable.", *entry); } ctx = vml_ctx_for_vaddr; } else { vm_map_t tmp_map = original_map; kr = vm_map_range_sh_lock(ctx, &tmp_map, vaddr, vaddr + VM_MAP_PAGE_SIZE(tmp_map), flags); if (kr != KERN_SUCCESS) { return kr; } *entry = vm_map_range_stream_next_with_error(ctx, &kr); if (kr != KERN_SUCCESS) { vm_map_range_sh_unlock(ctx, &tmp_map); if (kr == VMRL_ERR_LOCK_ALREADY_HELD) { /* our trylock failed */ return kr; } /* * There was no entry at vaddr, * or some step of resolution failed. */ return KERN_INVALID_ADDRESS; } } /* * If we're in a submap, update vaddr */ vaddr = vm_map_lock_ctx_from_parent_address(ctx, vaddr); /* * Check whether this task is allowed to have * this page. */ prot = (*entry)->protection; if (override_nx(original_map, VME_ALIAS(*entry)) && prot) { /* * HACK -- * This is a historical feature allowing execution from the stack * or data section originally from PPC that is still allowed * for x86 apps in some cases (32 bit maps or if enabled via sysctl) */ prot |= VM_PROT_EXECUTE; } #if __arm64e__ /* * If the entry we're dealing with is TPRO and we have a write * fault, inject VM_PROT_WRITE into protections. This allows us * to maintain RO permissions when not marked as TPRO. */ if ((*entry)->used_for_tpro && (fault_type & VM_PROT_WRITE)) { prot |= VM_PROT_WRITE; } #endif /* __arm64e__ */ if (mask_protections) { fault_type &= prot; if (fault_type == VM_PROT_NONE) { goto protection_failure; } } if (((fault_type & prot) != fault_type) #if __arm64__ /* prefetch abort in execute-only page */ && !(prot == VM_PROT_EXECUTE && fault_type == (VM_PROT_READ | VM_PROT_EXECUTE)) #elif defined(__x86_64__) /* Consider the UEXEC bit when handling an EXECUTE fault */ && !((fault_type & VM_PROT_EXECUTE) && !(prot & VM_PROT_EXECUTE) && (prot & VM_PROT_UEXEC)) #endif ) { protection_failure: if ((fault_type & VM_PROT_EXECUTE) && prot) { log_stack_execution_failure((addr64_t)vaddr, prot); } DTRACE_VM2(prot_fault, int, 1, (uint64_t *), NULL); DTRACE_VM3(prot_fault_detailed, vm_prot_t, fault_type, vm_prot_t, prot, void *, vaddr); /* * Noisy (esp. internally) and can be inferred from CrashReports. So OFF for now. * * ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PROTECTION_FAILURE), 0); */ vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); return KERN_PROTECTION_FAILURE; } assert(VME_OBJECT(*entry) != VM_OBJECT_NULL); /* * If this page is not pageable, we have to get * it for all possible accesses. */ *wired = ((*entry)->wired_count != 0); if (*wired) { fault_type = prot; } if ((*entry)->needs_copy) { /* * The entry is copy-on-write. needs_copy should already have * been resolved for faults needing write permissions * (write faults, faults on wired entries, or force_copy) */ assert(!(fault_type & VM_PROT_WRITE)); assert(!*wired); assert(!force_copy); /* * We're attempting to read a copy-on-write * page -- don't allow writes. */ prot &= (~VM_PROT_WRITE); } if (vm_map_lock_ctx_is_in_needs_copy_submap(ctx) && (prot & VM_PROT_WRITE)) { /* * The entry in the top level map is a needs_copy submap, * and we didn't try to unnest * * Avoid granting write permission to the bottom level entry in * the submap, because that would bypass the submap's "needs_copy" */ assert(!(fault_type & VM_PROT_WRITE)); assert(!*wired); assert(!force_copy); prot &= ~VM_PROT_WRITE; } /* * Return the object/offset from this entry. If the entry * was copy-on-write or empty, it has been fixed up. Also * return the protection. */ *offset = vm_map_lock_ctx_offset_for_address(ctx, vaddr); *object = VME_OBJECT(*entry); *out_prot = prot; KDBG_FILTERED(MACHDBG_CODE(DBG_MACH_WORKINGSET, VM_MAP_LOOKUP_OBJECT), VM_KERNEL_UNSLIDE_OR_PERM(*object), (unsigned long) VME_ALIAS(*entry), 0, 0); /* * real_map is the map containing the pmap we want to use. If we're * in a submap with a nested pmap, we want to use that one. * Otherwise, we want to use the pmap of the original map. */ if (vm_map_lock_ctx_is_in_pmap_nested_submap(ctx)) { *real_map = ctx->vmlc_map; } else { *real_map = original_map; } /* var_map is the map containing the entry we currently have locked */ *var_map = ctx->vmlc_map; if (fault_info) { /* * Initialize fault information according to the entry being faulted * from. */ fault_info->user_tag = VME_ALIAS(*entry); fault_info->pmap_options = 0; if ((*entry)->iokit_acct || (!(*entry)->is_sub_map && !(*entry)->use_pmap)) { fault_info->pmap_options |= PMAP_OPTIONS_ALT_ACCT; } if (fault_info->behavior == VM_BEHAVIOR_DEFAULT) { fault_info->behavior = (*entry)->behavior; } fault_info->lo_offset = VME_OFFSET(*entry); fault_info->hi_offset = ((*entry)->vme_end - (*entry)->vme_start) + VME_OFFSET(*entry); fault_info->no_cache = (*entry)->no_cache; fault_info->io_sync = FALSE; fault_info->cs_bypass = ((*entry)->used_for_jit || #if CODE_SIGNING_MONITOR (csm_address_space_exempt((*var_map)->pmap) == KERN_SUCCESS) || #endif (*entry)->vme_resilient_codesign); fault_info->mark_zf_absent = FALSE; fault_info->batch_pmap_op = FALSE; /* * The pmap layer will validate this page * before allowing it to be executed from. */ #if CODE_SIGNING_MONITOR fault_info->csm_associated = (*entry)->csm_associated; #else fault_info->csm_associated = FALSE; #endif fault_info->resilient_media = (*entry)->vme_resilient_media; fault_info->fi_xnu_user_debug = (*entry)->vme_xnu_user_debug; fault_info->no_copy_on_read = (*entry)->vme_no_copy_on_read; #if __arm64e__ fault_info->fi_used_for_tpro = (*entry)->used_for_tpro; #else /* __arm64e__ */ fault_info->fi_used_for_tpro = FALSE; #endif if ((*entry)->translated_allow_execute) { fault_info->pmap_options |= PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE; } } return KERN_SUCCESS; } kern_return_t vm_fault_internal( vm_map_t map, vm_map_offset_t vaddr, vm_prot_t caller_prot, vm_tag_t wire_tag, /* if wiring must pass tag != VM_KERN_MEMORY_NONE */ pmap_t caller_pmap, vm_map_offset_t caller_pmap_addr, ppnum_t *physpage_p, vm_object_fault_info_t fault_info, vm_map_lock_ctx_t vml_ctx_for_vaddr) { boolean_t wired; /* Should mapping be wired down? */ vm_object_t object; /* Top-level object */ vm_object_offset_t offset; /* Top-level offset */ vm_prot_t prot; /* Protection for mapping */ vm_object_t old_copy_object; /* Saved copy object */ uint64_t old_copy_version; vm_page_t result_page; /* Result of vm_fault_page */ vm_page_t top_page; /* Placeholder page */ kern_return_t kr; vm_page_t m; /* Fast access to result_page */ kern_return_t error_code; vm_object_t cur_object; vm_object_t m_object = NULL; vm_object_offset_t cur_offset; vm_page_t cur_m; vm_object_t new_object; int type_of_fault; pmap_t pmap; wait_interrupt_t interruptible_state; vm_map_t real_map = map; vm_map_t original_map = map; vm_prot_t fault_type; vm_prot_t original_fault_type; bool need_collapse = FALSE; bool need_retry = false; uint8_t object_lock_type = 0; uint8_t cur_object_lock_type; vm_object_t top_object = VM_OBJECT_NULL; vm_object_t written_on_object = VM_OBJECT_NULL; memory_object_t written_on_pager = NULL; vm_object_offset_t written_on_offset = 0; int throttle_delay; int compressed_count_delta; vm_grab_options_t grab_options; bool need_copy; bool need_copy_on_read; vm_map_offset_t trace_vaddr; vm_map_offset_t trace_real_vaddr; vm_map_size_t fault_page_size; vm_map_size_t fault_page_mask; int fault_page_shift; vm_map_offset_t fault_phys_offset; vm_map_offset_t real_vaddr; bool resilient_media_retry = false; bool resilient_media_ref_transfer = false; vm_object_t resilient_media_object = VM_OBJECT_NULL; vm_object_offset_t resilient_media_offset = (vm_object_offset_t)-1; bool page_needs_data_sync = false; vm_map_entry_t entry; VM_MAP_LOCK_CTX_DECLARE(ctx); vmlp_api_start(VM_FAULT_INTERNAL); vm_lock_contention_event_with_excl_ctx_dev(vml_ctx_for_vaddr, &vm_fault_excl_count, VMLP_EVENT_LC_NONE); #if HAS_MTE || HAS_MTE_EMULATION_SHIMS /* * We may be faulting on a tagged address. Canonicalize it here so we have * a chance to find it in the VM map. */ if (current_task_has_sec_enabled()) { vaddr = vm_memtag_canonicalize(map, vaddr); } #endif /* HAS_MTE || HAS_MTE_EMULATION_SHIMS */ real_vaddr = vaddr; trace_real_vaddr = vaddr; /* * Some (kernel) submaps are marked with "should never fault", so that * guard pages in such submaps do not need to use fictitious * placeholders at all, while not causing ZFOD pages to be made * (which is the default behavior otherwise). * * We also want capture the fault address easily so that the zone * allocator might present an enhanced panic log. */ if (map->never_faults) { assert(map->pmap == kernel_pmap); vmlp_api_end(VM_FAULT_INTERNAL, KERN_INVALID_ADDRESS); return KERN_INVALID_ADDRESS; } if (VM_MAP_PAGE_SIZE(original_map) < PAGE_SIZE) { fault_phys_offset = (vm_map_offset_t)-1; fault_page_size = VM_MAP_PAGE_SIZE(original_map); fault_page_mask = VM_MAP_PAGE_MASK(original_map); fault_page_shift = VM_MAP_PAGE_SHIFT(original_map); if (fault_page_size < PAGE_SIZE) { DEBUG4K_FAULT("map %p vaddr 0x%llx caller_prot 0x%x\n", map, (uint64_t)trace_real_vaddr, caller_prot); vaddr = vm_map_trunc_page(vaddr, fault_page_mask); } } else { fault_phys_offset = 0; fault_page_size = PAGE_SIZE; fault_page_mask = PAGE_MASK; fault_page_shift = PAGE_SHIFT; vaddr = vm_map_trunc_page(vaddr, PAGE_MASK); } if (map == kernel_map) { trace_vaddr = VM_KERNEL_ADDRHIDE(vaddr); trace_real_vaddr = VM_KERNEL_ADDRHIDE(trace_real_vaddr); } else { trace_vaddr = vaddr; } KDBG_RELEASE( (VMDBG_CODE(DBG_VM_FAULT_INTERNAL)) | DBG_FUNC_START, ((uint64_t)trace_vaddr >> 32), trace_vaddr, (map == kernel_map)); if (get_preemption_level() != 0) { KDBG_RELEASE( (VMDBG_CODE(DBG_VM_FAULT_INTERNAL)) | DBG_FUNC_END, ((uint64_t)trace_vaddr >> 32), trace_vaddr, KERN_FAILURE); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_NONZERO_PREEMPTION_LEVEL), 0 /* arg */); vmlp_api_end(VM_FAULT_INTERNAL, KERN_FAILURE); return KERN_FAILURE; } thread_t cthread = current_thread(); if (cthread->th_vm_faults_disabled) { KDBG_RELEASE( (MACHDBG_CODE(DBG_MACH_VM, 2)) | DBG_FUNC_END, ((uint64_t)trace_vaddr >> 32), trace_vaddr, KERN_FAILURE); ktriage_record(thread_tid(cthread), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULTS_DISABLED), 0 /* arg */); vmlp_api_end(VM_FAULT_INTERNAL, KERN_FAILURE); return KERN_FAILURE; } bool rtfault = (cthread->sched_mode == TH_MODE_REALTIME); bool page_sleep_needed = false; uint64_t fstart = 0; if (rtfault) { fstart = mach_continuous_time(); } assert(fault_info != NULL); interruptible_state = thread_interrupt_level(fault_info->interruptible); fault_type = (fault_info->fi_change_wiring ? VM_PROT_NONE : caller_prot); counter_inc(&vm_statistics_faults); counter_inc(¤t_task()->faults); original_fault_type = fault_type; need_copy = FALSE; if (fault_type & VM_PROT_WRITE) { need_copy = TRUE; } if (need_copy || fault_info->fi_change_wiring) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; } else { object_lock_type = OBJECT_LOCK_SHARED; } cur_object_lock_type = OBJECT_LOCK_SHARED; if ((map == kernel_map) && (caller_prot & VM_PROT_WRITE)) { if (compressor_map) { if ((vaddr >= vm_map_min(compressor_map)) && (vaddr < vm_map_max(compressor_map))) { panic("Write fault on compressor map, va: %p type: %u bounds: %p->%p", (void *) vaddr, caller_prot, (void *) vm_map_min(compressor_map), (void *) vm_map_max(compressor_map)); } } } RetryFault: assert3p(written_on_object, ==, VM_OBJECT_NULL); /* * assume we will hit a page in the cache * otherwise, explicitly override with * the real fault type once we determine it */ type_of_fault = DBG_CACHE_HIT_FAULT; /* * Find the backing store object and offset into * it to begin the search. */ fault_type = original_fault_type; map = original_map; if (resilient_media_retry) { /* * If we have to insert a fake zero-filled page to hide * a media failure to provide the real page, we need to * resolve any pending copy-on-write on this mapping. * VM_PROT_COPY tells vm_map_lookup_object_and_lock_entry() to deal * with that even if this is not a "write" fault. */ need_copy = TRUE; /* * If the top object is COPY_DELAYED and has a "copy" object, * we would have to push our zero-filled page to this copy * object before allowing it to be modified, so let's consider * this as a read-only fault for now. If this was a write * fault, we'll fault again on the read-only zero-filled page * and fulfill our copy-on-write obligations then. */ fault_type = VM_PROT_READ; /* * We need the object's exclusive lock to insert the * zero-filled page. */ object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_fault_resilient_media_retry++; } vm_map_lock_ctx_init(ctx); kr = vm_map_lookup_object_and_lock_entry(&map, vaddr, (fault_type | (need_copy ? VM_PROT_COPY : 0)), &object, &entry, &offset, &prot, &wired, fault_info, &real_map, ctx, vml_ctx_for_vaddr, false /* try_lock_entry */); /* * best effort to prime the free page queues including a pre-zerofilled * page. The placement of this function is extremely subtle and moving * it can incur non trivial performance costs. * * vm_map_lookup_object_and_lock_entry() above will do this sequence: * - lock_shared(map); * - lock_shared(entry); * - unlock_shared(map); * * The unlock_shared(map) has a release barrier that would force a flush * of the zerofill that vm_page_grab_prime() might perform. So we must * prime after it happens even if it means we're priming under the * shared lock of a VM entry. * * Sequencing the code this way allows to tend to have a primed * pre-zerofilled page before twe take the object lock exclusively * in the case of a zerofill fault which significantly improves * scalability. * * It is very important that no release barrier is added between this * function and the pmap_enter() that will eventually happen, as each * release barrier can cause a stall due to in flight stores for the * zerofill operation. */ vm_page_grab_prime(); if (kr != KERN_SUCCESS) { /* * This can be seen in a crash report if indeed the * thread is crashing due to an invalid access in a non-existent * range. * Turning this OFF for now because it is noisy and not always fatal * eg prefaulting. * * if (kr == KERN_INVALID_ADDRESS) { * ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_ADDRESS_NOT_FOUND), 0); * } */ goto done; } pmap = real_map->pmap; fault_info->io_sync = FALSE; fault_info->mark_zf_absent = FALSE; fault_info->batch_pmap_op = FALSE; /* * If the page is wired, we must fault for the current protection * value, to avoid further faults. */ if (wired) { fault_type = prot | VM_PROT_WRITE; } if (wired || need_copy) { /* * since we're treating this fault as a 'write' * we must hold the top object lock exclusively */ object_lock_type = OBJECT_LOCK_EXCLUSIVE; } #if defined(__arm64__) /* * Fail if reading an execute-only page in a * pmap that enforces execute-only protection. */ if (fault_type == VM_PROT_READ && (prot & VM_PROT_EXECUTE) && !(prot & VM_PROT_READ) && pmap_enforces_execute_only(pmap)) { vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); kr = KERN_PROTECTION_FAILURE; goto done; } #endif if (resilient_media_retry) { /* * We're retrying this fault after having detected a media * failure from a "resilient_media" mapping. * Check that the mapping is still pointing at the object * that just failed to provide a page. */ assert(resilient_media_object != VM_OBJECT_NULL); assert(resilient_media_offset != (vm_object_offset_t)-1); if ((object != VM_OBJECT_NULL && object == resilient_media_object && offset == resilient_media_offset && fault_info->resilient_media) #if MACH_ASSERT && (vm_fault_resilient_media_inject_error1_rate == 0 || (++vm_fault_resilient_media_inject_error1 % vm_fault_resilient_media_inject_error1_rate) != 0) #endif /* MACH_ASSERT */ ) { /* * This mapping still points at the same object * and is still "resilient_media": proceed in * "recovery-from-media-failure" mode, where we'll * insert a zero-filled page in the top object. */ // printf("RESILIENT_MEDIA %s:%d recovering for object %p offset 0x%llx\n", __FUNCTION__, __LINE__, object, offset); vm_fault_resilient_media_proceed++; } else { /* not recovering: reset state and retry fault */ // printf("RESILIENT_MEDIA %s:%d no recovery resilient %d object %p/%p offset 0x%llx/0x%llx\n", __FUNCTION__, __LINE__, fault_info->resilient_media, object, resilient_media_object, offset, resilient_media_offset); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); /* release our extra reference on failed object */ // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); vm_object_deallocate(resilient_media_object); resilient_media_object = VM_OBJECT_NULL; resilient_media_offset = (vm_object_offset_t)-1; resilient_media_retry = false; vm_fault_resilient_media_abort1++; goto RetryFault; } } else { assert(resilient_media_object == VM_OBJECT_NULL); resilient_media_offset = (vm_object_offset_t)-1; } if (object_lock_type == OBJECT_LOCK_EXCLUSIVE || (object->shadow == VM_OBJECT_NULL && lck_rw_has_exclusive_spinners(&object->Lock))) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_object_lock(object); } else { vm_object_lock_shared(object); } #if VM_FAULT_CLASSIFY /* * Temporary data gathering code */ vm_fault_classify(object, offset, fault_type); #endif /* * Fast fault code. The basic idea is to do as much as * possible while holding the map lock and object locks. * Busy pages are not used until the object lock has to * be dropped to do something (copy, zero fill, pmap enter). * Similarly, paging references aren't acquired until that * point, and object references aren't used. * * If we can figure out what to do * (zero fill, copy on write, pmap enter) while holding * the locks, then it gets done. Otherwise, we give up, * and use the original fault path (which doesn't hold * the map lock, and relies on busy pages). * The give up cases include: * - Have to talk to pager. * - Page is busy, absent or in error. * - Pager has locked out desired access. * - Fault needs to be restarted. * - Have to push page into copy object. * * The code is an infinite loop that moves one level down * the shadow chain each time. cur_object and cur_offset * refer to the current object being examined. object and offset * are the original object from the map. The loop is at the * top level if and only if object and cur_object are the same. * * Invariants: Map lock is held throughout. Lock is held on * original object and cur_object (if different) when * continuing or exiting loop. * */ fault_phys_offset = (vm_map_offset_t)offset - vm_map_trunc_page((vm_map_offset_t)offset, PAGE_MASK); /* * If this page is to be inserted in a copy delay object * for writing, and if the object has a copy, then the * copy delay strategy is implemented in the slow fault page. */ if ((object->copy_strategy == MEMORY_OBJECT_COPY_DELAY || object->copy_strategy == MEMORY_OBJECT_COPY_DELAY_FORK) && object->vo_copy != VM_OBJECT_NULL && (fault_type & VM_PROT_WRITE)) { assert(!resilient_media_retry); /* should be read-only fault */ goto handle_copy_delay; } cur_object = object; cur_offset = offset; grab_options = vm_page_grab_options_for_object(object); #if HAS_MTE if (!(grab_options & VM_PAGE_GRAB_MTE) && mteinfo_vm_tag_can_use_tag_storage((vm_tag_t)fault_info->user_tag)) { grab_options |= VM_PAGE_GRAB_ALLOW_TAG_STORAGE; } #endif /* HAS_MTE */ while (TRUE) { if (!cur_object->pager_created && cur_object->phys_contiguous) { /* superpage */ break; } if (cur_object->blocked_access) { /* * Access to this VM object has been blocked. * Let the slow path handle it. */ break; } m = vm_page_lookup(cur_object, vm_object_trunc_page(cur_offset)); m_object = NULL; if (m != VM_PAGE_NULL) { m_object = cur_object; if (__improbable(page_sleep_needed)) { /* * If a prior iteration of the loop requested vm_page_sleep(), re-validate the page * to see if it's still needed. */ kr = vm_fault_pmap_validate_page(pmap, m, vaddr, prot, fault_info, &page_sleep_needed); if (__improbable(kr != KERN_SUCCESS)) { vm_object_unlock(object); if (object != cur_object) { vm_object_unlock(cur_object); } vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto done; } } if (m->vmp_busy || page_sleep_needed) { page_sleep_needed = false; wait_result_t result; /* * in order to vm_page_sleep(), we must * have object that 'm' belongs to locked exclusively */ if (object != cur_object) { if (cur_object_lock_type == OBJECT_LOCK_SHARED) { cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(cur_object) == FALSE) { /* * couldn't upgrade so go do a full retry * immediately since we can no longer be * certain about cur_object (since we * don't hold a reference on it)... * first drop the top object lock */ vm_object_unlock(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } } } else if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade, so explictly take the lock * exclusively and go relookup the page since we * will have dropped the object lock and * a different thread could have inserted * a page at this offset * no need for a full retry since we're * at the top level of the object chain */ vm_object_lock(object); continue; } } if ((m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) && m_object->internal) { /* * m->vmp_busy == TRUE and the object is locked exclusively * if m->pageout_queue == TRUE after we acquire the * queues lock, we are guaranteed that it is stable on * the pageout queue and therefore reclaimable * * NOTE: this is only true for the internal pageout queue * in the compressor world */ assert(VM_CONFIG_COMPRESSOR_IS_PRESENT); vm_page_lock_queues(); if (m->vmp_q_state == VM_PAGE_ON_PAGEOUT_Q) { vm_pageout_throttle_up(m); vm_page_unlock_queues(); vm_page_wakeup_done(m_object, m); goto reclaimed_from_pageout; } vm_page_unlock_queues(); } if (object != cur_object) { vm_object_unlock(object); } vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); vm_lock_contention_event_with_excl_ctx(vml_ctx_for_vaddr, &vm_fault_excl_busy_count, VMLP_EVENT_LC_VM_FAULT_EXCL_BUSY); result = vm_page_sleep(cur_object, m, fault_info->interruptible, LCK_SLEEP_UNLOCK); if (result == THREAD_AWAKENED || result == THREAD_RESTART) { goto RetryFault; } ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_BUSYPAGE_WAIT_INTERRUPTED), 0 /* arg */); kr = KERN_ABORTED; goto done; } reclaimed_from_pageout: if (m->vmp_laundry) { if (object != cur_object) { if (cur_object_lock_type == OBJECT_LOCK_SHARED) { cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_object_unlock(object); vm_object_unlock(cur_object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } } else if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade, so explictly take the lock * exclusively and go relookup the page since we * will have dropped the object lock and * a different thread could have inserted * a page at this offset * no need for a full retry since we're * at the top level of the object chain */ vm_object_lock(object); continue; } } vm_object_lock_assert_exclusive(VM_PAGE_OBJECT(m)); vm_pageout_steal_laundry(m, FALSE); } if (vm_page_is_guard(m)) { /* * Guard page: let the slow path deal with it */ break; } if (m->vmp_unusual && (m->vmp_error || m->vmp_restart || vm_page_is_private(m) || m->vmp_absent)) { /* * Unusual case... let the slow path deal with it */ break; } if (VM_OBJECT_PURGEABLE_FAULT_ERROR(m_object)) { if (object != cur_object) { vm_object_unlock(object); } vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); vm_object_unlock(cur_object); ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */); kr = KERN_MEMORY_ERROR; goto done; } assert(m_object == VM_PAGE_OBJECT(m)); if (vm_fault_cs_need_validation(map->pmap, m, m_object, PAGE_SIZE, 0) || (physpage_p != NULL && (prot & VM_PROT_WRITE))) { upgrade_lock_and_retry: /* * We might need to validate this page * against its code signature, so we * want to hold the VM object exclusively. */ if (object != cur_object) { if (cur_object_lock_type == OBJECT_LOCK_SHARED) { vm_object_unlock(object); vm_object_unlock(cur_object); cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } } else if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade, so explictly take the lock * exclusively and go relookup the page since we * will have dropped the object lock and * a different thread could have inserted * a page at this offset * no need for a full retry since we're * at the top level of the object chain */ vm_object_lock(object); continue; } } } /* * Two cases of map in faults: * - At top level w/o copy object. * - Read fault anywhere. * --> must disallow write. */ if (object == cur_object && object->vo_copy == VM_OBJECT_NULL) { #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES if ((fault_type & VM_PROT_WRITE) && m->vmp_unmodified_ro) { assert(cur_object == VM_PAGE_OBJECT(m)); assert(cur_object->internal); vm_object_lock_assert_exclusive(cur_object); vm_page_lockspin_queues(); m->vmp_unmodified_ro = false; vm_page_unlock_queues(); os_atomic_dec(&compressor_ro_uncompressed, relaxed); vm_object_compressor_pager_state_clr(cur_object, m->vmp_offset); } #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */ goto FastPmapEnter; } if (!need_copy && !fault_info->no_copy_on_read && cur_object != object && !cur_object->internal && !cur_object->pager_trusted && !cur_object->code_signed && vm_protect_privileged_from_untrusted && (current_proc_is_privileged() || vm_kernel_map_is_kernel(map) || vm_map_is_platform_binary(map))) { /* * We're faulting on a page in "object" and * went down the shadow chain to "cur_object" * to find out that "cur_object"'s pager * is not "trusted", i.e. we can not trust it * to always return the same contents. * Since the target is a "privileged" process, * let's treat this as a copy-on-read fault, as * if it was a copy-on-write fault. * Once "object" gets a copy of this page, it * won't have to rely on "cur_object" to * provide the contents again. * * This is done by setting "need_copy" and * retrying the fault from the top with the * appropriate locking. * * Special case: if the mapping is executable * and the untrusted object is code-signed and * the process is "cs_enforced", we do not * copy-on-read because that would break * code-signing enforcement expectations (an * executable page must belong to a code-signed * object) and we can rely on code-signing * to re-validate the page if it gets evicted * and paged back in. */ // printf("COPY-ON-READ %s:%d map %p va 0x%llx page %p object %p offset 0x%llx UNTRUSTED: need copy-on-read!\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, m, VM_PAGE_OBJECT(m), m->vmp_offset); vm_copied_on_read++; if (!current_proc_is_privileged()) { /* not a privileged proc but still copy-on-read... */ if (vm_kernel_map_is_kernel(map)) { /* ... because target map is a kernel map */ vm_copied_on_read_kernel_map++; } else { /* ... because target map is "platform" */ vm_copied_on_read_platform_map++; } } need_copy = TRUE; vm_object_unlock(object); vm_object_unlock(cur_object); object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } if (!(fault_type & VM_PROT_WRITE) && !need_copy) { if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) { /* * For a protection that the pmap cares * about, we must hand over the full * set of protections (so that the pmap * layer can apply any desired policy). * This means that cs_bypass must be * set, as this can force us to pass * RWX. */ if (!fault_info->cs_bypass) { panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x", __FUNCTION__, pmap, (uint64_t)vaddr, prot, fault_info->pmap_options); } } else { prot &= ~VM_PROT_WRITE; } if (object != cur_object) { /* * We still need to hold the top object * lock here to prevent a race between * a read fault (taking only "shared" * locks) and a write fault (taking * an "exclusive" lock on the top * object. * Otherwise, as soon as we release the * top lock, the write fault could * proceed and actually complete before * the read fault, and the copied page's * translation could then be overwritten * by the read fault's translation for * the original page. * * Let's just record what the top object * is and we'll release it later. */ top_object = object; /* * switch to the object that has the new page */ object = cur_object; object_lock_type = cur_object_lock_type; } FastPmapEnter: assert(m_object == VM_PAGE_OBJECT(m)); if (resilient_media_retry && (prot & VM_PROT_WRITE)) { /* * We might have bypassed some copy-on-write * mechanism to get here (theoretically inserting * a zero-filled page in the top object to avoid * raising an exception on an unavailable page at * the bottom of the shadow chain. * So let's not grant write access to this page yet. * If write access is needed, the next fault should * handle any copy-on-write obligations. */ if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) { /* * For a protection that the pmap cares * about, we must hand over the full * set of protections (so that the pmap * layer can apply any desired policy). * This means that cs_bypass must be * set, as this can force us to pass * RWX. */ if (!fault_info->cs_bypass) { panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x", __FUNCTION__, pmap, (uint64_t)vaddr, prot, fault_info->pmap_options); } } else { prot &= ~VM_PROT_WRITE; } } /* * prepare for the pmap_enter... * object and map are both locked * m contains valid data * object == m->vmp_object * cur_object == NULL or it's been unlocked * no paging references on either object or cur_object */ if (fault_page_size < PAGE_SIZE) { DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx caller pmap %p va 0x%llx pa 0x%llx (0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map, original_map, pmap, (uint64_t)vaddr, caller_pmap, (uint64_t)caller_pmap_addr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, prot, caller_prot); assertf((!(fault_phys_offset & FOURK_PAGE_MASK) && fault_phys_offset < PAGE_SIZE), "0x%llx\n", (uint64_t)fault_phys_offset); } else { assertf(fault_phys_offset == 0, "0x%llx\n", (uint64_t)fault_phys_offset); } if (__improbable(rtfault && !m->vmp_realtime && vm_pageout_protect_realtime)) { vm_page_lock_queues(); if (!m->vmp_realtime) { m->vmp_realtime = true; VM_COUNTER_INC(&vm_page_realtime_count); } vm_page_unlock_queues(); } assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p object=%p", m, m_object, object); assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL); need_retry = false; if (caller_pmap) { kr = vm_fault_enter(m, caller_pmap, caller_pmap_addr, fault_page_size, fault_phys_offset, prot, caller_prot, wired, wire_tag, fault_info, &need_retry, &type_of_fault, &object_lock_type, &page_sleep_needed); } else { kr = vm_fault_enter(m, pmap, vaddr, fault_page_size, fault_phys_offset, prot, caller_prot, wired, wire_tag, fault_info, &need_retry, &type_of_fault, &object_lock_type, &page_sleep_needed); } vm_fault_complete( object, m_object, m, offset, trace_real_vaddr, fault_info, caller_prot, real_vaddr, vm_fault_type_for_tracing(need_copy_on_read, type_of_fault), need_retry || page_sleep_needed, kr, physpage_p, prot, top_object, need_collapse, cur_offset, fault_type, &written_on_object, &written_on_pager, &written_on_offset, ctx, vml_ctx_for_vaddr); top_object = VM_OBJECT_NULL; if (need_retry) { /* * vm_fault_enter couldn't complete the PMAP_ENTER... * at this point we don't hold any locks so it's safe * to ask the pmap layer to expand the page table to * accommodate this mapping... once expanded, we'll * re-drive the fault which should result in vm_fault_enter * being able to successfully enter the mapping this time around */ (void)pmap_enter_options( pmap, vaddr, 0, 0, 0, 0, 0, PMAP_OPTIONS_NOENTER, NULL, PMAP_MAPPING_TYPE_INFER); need_retry = false; goto RetryFault; } if (page_sleep_needed) { goto RetryFault; } goto done; } /* * COPY ON WRITE FAULT */ assert(object_lock_type == OBJECT_LOCK_EXCLUSIVE); /* * If objects match, then * object->vo_copy must not be NULL (else control * would be in previous code block), and we * have a potential push into the copy object * with which we can't cope with here. */ if (cur_object == object) { /* * must take the slow path to * deal with the copy push */ break; } /* * This is now a shadow based copy on write * fault -- it requires a copy up the shadow * chain. */ assert(m_object == VM_PAGE_OBJECT(m)); if ((cur_object_lock_type == OBJECT_LOCK_SHARED) && vm_fault_cs_need_validation(NULL, m, m_object, PAGE_SIZE, 0)) { goto upgrade_lock_and_retry; } #if MACH_ASSERT if (resilient_media_retry && vm_fault_resilient_media_inject_error2_rate != 0 && (++vm_fault_resilient_media_inject_error2 % vm_fault_resilient_media_inject_error2_rate) == 0) { /* inject an error */ cur_m = m; m = VM_PAGE_NULL; m_object = VM_OBJECT_NULL; break; } #endif /* MACH_ASSERT */ /* * Allocate a page in the original top level * object. Give up if allocate fails. Also * need to remember current page, as it's the * source of the copy. * * at this point we hold locks on both * object and cur_object... no need to take * paging refs or mark pages BUSY since * we don't drop either object lock until * the page has been copied and inserted */ cur_m = m; m = vm_page_grab_options(grab_options); m_object = NULL; if (m == VM_PAGE_NULL) { /* * no free page currently available... * must take the slow path */ break; } /* * Now do the copy. Mark the source page busy... * * NOTE: This code holds the map lock across * the page copy. */ vm_page_copy(cur_m, m); vm_page_insert(m, object, vm_object_trunc_page(offset)); if (VM_MAP_PAGE_MASK(map) != PAGE_MASK) { DEBUG4K_FAULT("map %p vaddr 0x%llx page %p [%p 0x%llx] copied to %p [%p 0x%llx]\n", map, (uint64_t)vaddr, cur_m, VM_PAGE_OBJECT(cur_m), cur_m->vmp_offset, m, VM_PAGE_OBJECT(m), m->vmp_offset); } m_object = object; SET_PAGE_DIRTY(m, FALSE); /* * Now cope with the source page and object */ if (os_ref_get_count_raw(&object->ref_count) > 1 && cur_m->vmp_pmapped) { pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m)); } else if (VM_MAP_PAGE_SIZE(map) < PAGE_SIZE) { /* * We've copied the full 16K page but we're * about to call vm_fault_enter() only for * the 4K chunk we're faulting on. The other * three 4K chunks in that page could still * be pmapped in this pmap. * Since the VM object layer thinks that the * entire page has been dealt with and the * original page might no longer be needed, * it might collapse/bypass the original VM * object and free its pages, which would be * bad (and would trigger pmap_verify_free() * assertions) if the other 4K chunks are still * pmapped. */ /* * XXX FBDP TODO4K: to be revisisted * Technically, we need to pmap_disconnect() * only the target pmap's mappings for the 4K * chunks of this 16K VM page. If other pmaps * have PTEs on these chunks, that means that * the associated VM map must have a reference * on the VM object, so no need to worry about * those. * pmap_protect() for each 4K chunk would be * better but we'd have to check which chunks * are actually mapped before and after this * one. * A full-blown pmap_disconnect() is easier * for now but not efficient. */ DEBUG4K_FAULT("pmap_disconnect() page %p object %p offset 0x%llx phys 0x%x\n", cur_m, VM_PAGE_OBJECT(cur_m), cur_m->vmp_offset, VM_PAGE_GET_PHYS_PAGE(cur_m)); pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(cur_m)); } if (cur_m->vmp_clustered) { VM_PAGE_COUNT_AS_PAGEIN(cur_m); VM_PAGE_CONSUME_CLUSTERED(cur_m); vm_fault_is_sequential(cur_object, cur_offset, fault_info->behavior); } need_collapse = TRUE; if (!cur_object->internal && cur_object->copy_strategy == MEMORY_OBJECT_COPY_DELAY) { /* * The object from which we've just * copied a page is most probably backed * by a vnode. We don't want to waste too * much time trying to collapse the VM objects * and create a bottleneck when several tasks * map the same file. */ if (cur_object->vo_copy == object) { /* * Shared mapping or no COW yet. * We can never collapse a copy * object into its backing object. */ need_collapse = FALSE; } else if (cur_object->vo_copy == object->shadow && object->shadow->resident_page_count == 0) { /* * Shared mapping after a COW occurred. */ need_collapse = FALSE; } } vm_object_unlock(cur_object); if (need_collapse == FALSE) { vm_fault_collapse_skipped++; } vm_fault_collapse_total++; type_of_fault = DBG_COW_FAULT; counter_inc(&vm_statistics_cow_faults); DTRACE_VM2(cow_fault, int, 1, (uint64_t *), NULL); counter_inc(¤t_task()->cow_faults); goto FastPmapEnter; } else { /* * No page at cur_object, cur_offset... m == NULL */ if (cur_object->pager_created) { vm_external_state_t compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN; if (MUST_ASK_PAGER(cur_object, cur_offset, compressor_external_state) == TRUE) { int my_fault_type; vm_compressor_options_t c_flags = C_DONT_BLOCK; bool insert_cur_object = FALSE; /* * May have to talk to a pager... * if so, take the slow path by * doing a 'break' from the while (TRUE) loop * * external_state will only be set to VM_EXTERNAL_STATE_EXISTS * if the compressor is active and the page exists there */ if (compressor_external_state != VM_EXTERNAL_STATE_EXISTS) { break; } if (map == kernel_map || real_map == kernel_map) { /* * can't call into the compressor with the kernel_map * lock held, since the compressor may try to operate * on the kernel map in order to return an empty c_segment */ break; } if (object != cur_object) { if (fault_type & VM_PROT_WRITE) { c_flags |= C_KEEP; } else { insert_cur_object = TRUE; } } if (insert_cur_object == TRUE) { if (cur_object_lock_type == OBJECT_LOCK_SHARED) { cur_object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(cur_object) == FALSE) { /* * couldn't upgrade so go do a full retry * immediately since we can no longer be * certain about cur_object (since we * don't hold a reference on it)... * first drop the top object lock */ vm_object_unlock(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } } } else if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (object != cur_object) { /* * we can't go for the upgrade on the top * lock since the upgrade may block waiting * for readers to drain... since we hold * cur_object locked at this point, waiting * for the readers to drain would represent * a lock order inversion since the lock order * for objects is the reference order in the * shadown chain */ vm_object_unlock(object); vm_object_unlock(cur_object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade, so explictly take the lock * exclusively and go relookup the page since we * will have dropped the object lock and * a different thread could have inserted * a page at this offset * no need for a full retry since we're * at the top level of the object chain */ vm_object_lock(object); continue; } } #if HAS_MTE if (vm_object_is_mte_mappable(object)) { c_flags |= C_MTE; } #endif /* HAS_MTE */ m = vm_page_grab_options(grab_options); m_object = NULL; if (m == VM_PAGE_NULL) { /* * no free page currently available... * must take the slow path */ break; } /* * The object is and remains locked * so no need to take a * "paging_in_progress" reference. */ bool shared_lock; if ((object == cur_object && object_lock_type == OBJECT_LOCK_EXCLUSIVE) || (object != cur_object && cur_object_lock_type == OBJECT_LOCK_EXCLUSIVE)) { shared_lock = FALSE; } else { shared_lock = TRUE; } kr = vm_compressor_pager_get( cur_object->pager, (vm_object_trunc_page(cur_offset) + cur_object->paging_offset), VM_PAGE_GET_PHYS_PAGE(m), &my_fault_type, c_flags, &compressed_count_delta); vm_compressor_pager_count( cur_object->pager, compressed_count_delta, shared_lock, cur_object); if (kr != KERN_SUCCESS) { vm_page_release(m, VMP_RELEASE_NONE); m = VM_PAGE_NULL; } /* * If vm_compressor_pager_get() returns * KERN_MEMORY_FAILURE, then the * compressed data is permanently lost, * so return this error immediately. */ if (kr == KERN_MEMORY_FAILURE) { if (object != cur_object) { vm_object_unlock(cur_object); } vm_object_unlock(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto done; } else if (kr != KERN_SUCCESS) { break; } m->vmp_dirty = TRUE; #if CONFIG_TRACK_UNMODIFIED_ANON_PAGES if ((fault_type & VM_PROT_WRITE) == 0) { prot &= ~VM_PROT_WRITE; /* * The page, m, has yet to be inserted * into an object. So we are fine with * the object/cur_object lock being held * shared. */ vm_page_lockspin_queues(); m->vmp_unmodified_ro = true; vm_page_unlock_queues(); os_atomic_inc(&compressor_ro_uncompressed, relaxed); } #endif /* CONFIG_TRACK_UNMODIFIED_ANON_PAGES */ /* * If the object is purgeable, its * owner's purgeable ledgers will be * updated in vm_page_insert() but the * page was also accounted for in a * "compressed purgeable" ledger, so * update that now. */ if (object != cur_object && !insert_cur_object) { /* * We're not going to insert * the decompressed page into * the object it came from. * * We're dealing with a * copy-on-write fault on * "object". * We're going to decompress * the page directly into the * target "object" while * keepin the compressed * page for "cur_object", so * no ledger update in that * case. */ } else if (((cur_object->purgable == VM_PURGABLE_DENY) && (!cur_object->vo_ledger_tag)) || (cur_object->vo_owner == NULL)) { /* * "cur_object" is not purgeable * and is not ledger-taged, or * there's no owner for it, * so no owner's ledgers to * update. */ } else { /* * One less compressed * purgeable/tagged page for * cur_object's owner. */ if (compressed_count_delta) { vm_object_owner_compressed_update( cur_object, -1); } } if (insert_cur_object) { vm_page_insert(m, cur_object, vm_object_trunc_page(cur_offset)); m_object = cur_object; } else { vm_page_insert(m, object, vm_object_trunc_page(offset)); m_object = object; } if (!HAS_DEFAULT_CACHEABILITY(m_object->wimg_bits & VM_WIMG_MASK)) { /* * If the page is not cacheable, * we can't let its contents * linger in the data cache * after the decompression. */ pmap_sync_page_attributes_phys(VM_PAGE_GET_PHYS_PAGE(m)); } type_of_fault = my_fault_type; VM_STAT_DECOMPRESSIONS(); if (cur_object != object) { if (insert_cur_object) { top_object = object; /* * switch to the object that has the new page */ object = cur_object; object_lock_type = cur_object_lock_type; } else { vm_object_unlock(cur_object); cur_object = object; } } goto FastPmapEnter; } /* * existence map present and indicates * that the pager doesn't have this page */ } if (cur_object->shadow == VM_OBJECT_NULL || resilient_media_retry) { /* * Zero fill fault. Page gets * inserted into the original object. */ if (cur_object->shadow_severed || VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object) || cur_object == compressor_object || is_kernel_object(cur_object)) { if (object != cur_object) { vm_object_unlock(cur_object); } vm_object_unlock(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); if (VM_OBJECT_PURGEABLE_FAULT_ERROR(cur_object)) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_PURGEABLE_FAULT_ERROR), 0 /* arg */); } if (cur_object->shadow_severed) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_OBJECT_SHADOW_SEVERED), 0 /* arg */); } kr = KERN_MEMORY_ERROR; goto done; } if (cur_object != object) { vm_object_unlock(cur_object); cur_object = object; } if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade so do a full retry on the fault * since we dropped the object lock which * could allow another thread to insert * a page at this offset */ vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } } if (!object->internal) { panic("%s:%d should not zero-fill page at offset 0x%llx in external object %p", __FUNCTION__, __LINE__, (uint64_t)offset, object); } #if MACH_ASSERT if (resilient_media_retry && vm_fault_resilient_media_inject_error3_rate != 0 && (++vm_fault_resilient_media_inject_error3 % vm_fault_resilient_media_inject_error3_rate) == 0) { /* inject an error */ m_object = NULL; break; } #endif /* MACH_ASSERT */ /* * This allocation is done with the map and * object lock held, which really isn't great * for scalability. * * we called vm_page_grab_prime() above, so * hopefully we hit a fast path most of the * time. */ m = vm_page_grab_options(grab_options | (map->no_zero_fill ? VM_PAGE_GRAB_OPTIONS_NONE : VM_PAGE_GRAB_ZERO_FILL)); m_object = NULL; if (m == VM_PAGE_NULL) { /* * no free page currently available... * must take the slow path */ break; } /* * Passing VMPI_DELAY_HASH here is crucial for * performance: if we inserted in the page hash * right away, this would take/drop the page * bucket hash lock, which has a release barrier * in the unlock path, and would cause flushing * of any in flight vm_page_zero_fill() that * precedes this call. * * This is a 10-20% performance cost in zerofill * paths if this release barrier existed here. * * Instead, vm_fault_complete() will perform the * insert into the table much later, but before * we release the lock on "object". */ m_object = object; vm_page_insert_internal(m, m_object, vm_object_trunc_page(offset), VM_KERN_MEMORY_NONE, VMPI_DELAY_HASH, NULL); if ((prot & VM_PROT_WRITE) && !(fault_type & VM_PROT_WRITE) && object->vo_copy != VM_OBJECT_NULL) { /* * This is not a write fault and * we might have a copy-on-write * obligation to honor (copy object or * "needs_copy" map entry), so do not * give write access yet. * We'll need to catch the first write * to resolve the copy-on-write by * pushing this page to a copy object * or making a shadow object. */ if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) { /* * This pmap enforces extra * constraints for this set of * protections, so we can't * change the protections. * We would expect code-signing * to be bypassed in this case. */ if (!fault_info->cs_bypass) { panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x", __FUNCTION__, pmap, (uint64_t)vaddr, prot, fault_info->pmap_options); } } else { prot &= ~VM_PROT_WRITE; } } if (resilient_media_retry) { /* * Not a real write, so no reason to assert. * We've just allocated a new page for this * <object,offset> so we know nobody has any * PTE pointing at any previous version of this * page and no copy-on-write is involved here. * We're just inserting a page of zeroes at this * stage of the shadow chain because the pager * for the lowest object in the shadow chain * said it could not provide that page and we * want to avoid failing the fault and causing * a crash on this "resilient_media" mapping. */ } else { assertf(!((fault_type & VM_PROT_WRITE) && object->vo_copy), "map %p va 0x%llx wrong path for write fault (fault_type 0x%x) on object %p with copy %p\n", map, (uint64_t)vaddr, fault_type, object, object->vo_copy); } vm_object_t saved_copy_object; uint64_t saved_copy_version; saved_copy_object = object->vo_copy; saved_copy_version = object->vo_copy_version; /* * Zeroing the page and entering into it into the pmap * represents a significant amount of the zero fill fault handler's work. * * To improve fault scalability, we'll drop the object lock, if it appears contended, * now that we've inserted the page into the vm object. * Before dropping the lock, we need to check protection bits and set the * mapped bits on the page. Then we can mark the page busy, drop the lock, * zero it, and do the pmap enter. We'll need to reacquire the lock * to clear the busy bit and wake up any waiters. */ vm_fault_cs_clear(m); m->vmp_pmapped = TRUE; if (map->no_zero_fill) { type_of_fault = DBG_NZF_PAGE_FAULT; } else { type_of_fault = DBG_ZERO_FILL_FAULT; } { pmap_t destination_pmap; vm_map_offset_t destination_pmap_vaddr; vm_prot_t enter_fault_type; if (caller_pmap) { destination_pmap = caller_pmap; destination_pmap_vaddr = caller_pmap_addr; } else { destination_pmap = pmap; destination_pmap_vaddr = vaddr; } if (fault_info->fi_change_wiring) { enter_fault_type = VM_PROT_NONE; } else { enter_fault_type = caller_prot; } assertf(VM_PAGE_OBJECT(m) == object, "m=%p object=%p", m, object); kr = vm_fault_enter_prepare(m, destination_pmap, destination_pmap_vaddr, &prot, caller_prot, fault_page_size, fault_phys_offset, enter_fault_type, fault_info, &type_of_fault, &page_needs_data_sync, &page_sleep_needed); assert(!page_sleep_needed); if (kr != KERN_SUCCESS) { goto zero_fill_cleanup; } if (type_of_fault == DBG_ZERO_FILL_FAULT) { assert(!map->no_zero_fill); } if (page_needs_data_sync) { pmap_sync_page_data_phys(VM_PAGE_GET_PHYS_PAGE(m)); } if (fault_info->fi_xnu_user_debug && !object->code_signed) { fault_info->pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG; } kr = vm_fault_pmap_enter_with_object_lock(object, destination_pmap, destination_pmap_vaddr, fault_page_size, fault_phys_offset, m, &prot, caller_prot, enter_fault_type, wired, fault_info->pmap_options, &need_retry, &object_lock_type); } zero_fill_cleanup: vm_fault_enqueue_throttled(object, m); vm_fault_enqueue_page(object, m, wired, fault_info->fi_change_wiring, wire_tag, fault_info->no_cache, &type_of_fault, kr); if (__improbable(rtfault && !m->vmp_realtime && vm_pageout_protect_realtime)) { vm_page_lock_queues(); if (!m->vmp_realtime) { m->vmp_realtime = true; VM_COUNTER_INC(&vm_page_realtime_count); } vm_page_unlock_queues(); } vm_fault_complete( object, m_object, m, offset, trace_real_vaddr, fault_info, caller_prot, real_vaddr, type_of_fault, need_retry, kr, physpage_p, prot, top_object, need_collapse, cur_offset, fault_type, &written_on_object, &written_on_pager, &written_on_offset, ctx, vml_ctx_for_vaddr); top_object = VM_OBJECT_NULL; if (need_retry) { /* * vm_fault_enter couldn't complete the PMAP_ENTER... * at this point we don't hold any locks so it's safe * to ask the pmap layer to expand the page table to * accommodate this mapping... once expanded, we'll * re-drive the fault which should result in vm_fault_enter * being able to successfully enter the mapping this time around */ (void)pmap_enter_options( pmap, vaddr, 0, 0, 0, 0, 0, PMAP_OPTIONS_NOENTER, NULL, PMAP_MAPPING_TYPE_INFER); need_retry = FALSE; goto RetryFault; } goto done; } /* * On to the next level in the shadow chain */ cur_offset += cur_object->vo_shadow_offset; new_object = cur_object->shadow; fault_phys_offset = cur_offset - vm_object_trunc_page(cur_offset); /* * take the new_object's lock with the indicated state */ if (cur_object_lock_type == OBJECT_LOCK_SHARED) { vm_object_lock_shared(new_object); } else { vm_object_lock(new_object); } if (cur_object != object) { vm_object_unlock(cur_object); } cur_object = new_object; continue; } } /* * Cleanup from fast fault failure. Drop any object * lock other than original and drop map lock. */ if (object != cur_object) { vm_object_unlock(cur_object); } /* * must own the object lock exclusively at this point */ if (object_lock_type == OBJECT_LOCK_SHARED) { object_lock_type = OBJECT_LOCK_EXCLUSIVE; if (vm_object_lock_upgrade(object) == FALSE) { /* * couldn't upgrade, so explictly * take the lock exclusively * no need to retry the fault at this * point since "vm_fault_page" will * completely re-evaluate the state */ vm_object_lock(object); } } handle_copy_delay: vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); entry = VM_MAP_ENTRY_NULL; if (__improbable(object == compressor_object || is_kernel_object(object))) { /* * These objects are explicitly managed and populated by the * kernel. The virtual ranges backed by these objects should * either have wired pages or "holes" that are not supposed to * be accessed at all until they get explicitly populated. * We should never have to resolve a fault on a mapping backed * by one of these VM objects and providing a zero-filled page * would be wrong here, so let's fail the fault and let the * caller crash or recover. */ vm_object_unlock(object); kr = KERN_MEMORY_ERROR; goto done; } resilient_media_ref_transfer = false; if (resilient_media_retry) { /* * We could get here if we failed to get a free page * to zero-fill and had to take the slow path again. * Reset our "recovery-from-failed-media" state. */ assert(resilient_media_object != VM_OBJECT_NULL); assert(resilient_media_offset != (vm_object_offset_t)-1); /* release our extra reference on failed object */ // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); if (object == resilient_media_object) { /* * We're holding "object"'s lock, so we can't release * our extra reference at this point. * We need an extra reference on "object" anyway * (see below), so let's just transfer this reference. */ resilient_media_ref_transfer = true; } else { vm_object_deallocate(resilient_media_object); } resilient_media_object = VM_OBJECT_NULL; resilient_media_offset = (vm_object_offset_t)-1; resilient_media_retry = false; vm_fault_resilient_media_abort2++; } /* * Make a reference to this object to * prevent its disposal while we are messing with * it. Once we have the reference, the map is free * to be diddled. Since objects reference their * shadows (and copies), they will stay around as well. */ if (resilient_media_ref_transfer) { /* we already have an extra reference on this object */ resilient_media_ref_transfer = false; } else { vm_object_reference_locked(object); } vm_object_paging_begin(object); set_thread_pagein_error(cthread, 0); error_code = 0; result_page = VM_PAGE_NULL; vm_fault_return_t err = vm_fault_page(object, offset, fault_type, (fault_info->fi_change_wiring && !wired), FALSE, /* page not looked up */ &prot, &result_page, &top_page, &type_of_fault, &error_code, map->no_zero_fill, fault_info, vml_ctx_for_vaddr); /* * if kr != VM_FAULT_SUCCESS, then the paging reference * has been dropped and the object unlocked... the ref_count * is still held * * if kr == VM_FAULT_SUCCESS, then the paging reference * is still held along with the ref_count on the original object * * the object is returned locked with a paging reference * * if top_page != NULL, then it's BUSY and the * object it belongs to has a paging reference * but is returned unlocked */ if (err != VM_FAULT_SUCCESS && err != VM_FAULT_SUCCESS_NO_VM_PAGE) { if (err == VM_FAULT_MEMORY_ERROR && fault_info->resilient_media) { assertf(object->internal, "object %p", object); /* * This fault failed but the mapping was * "media resilient", so we'll retry the fault in * recovery mode to get a zero-filled page in the * top object. * Keep the reference on the failing object so * that we can check that the mapping is still * pointing to it when we retry the fault. */ // printf("RESILIENT_MEDIA %s:%d: object %p offset 0x%llx recover from media error 0x%x kr 0x%x top_page %p result_page %p\n", __FUNCTION__, __LINE__, object, offset, error_code, kr, top_page, result_page); assert(!resilient_media_retry); /* no double retry */ assert(resilient_media_object == VM_OBJECT_NULL); assert(resilient_media_offset == (vm_object_offset_t)-1); resilient_media_retry = true; resilient_media_object = object; resilient_media_offset = offset; // printf("FBDP %s:%d resilient_media_object %p offset 0x%llx kept reference\n", __FUNCTION__, __LINE__, resilient_media_object, resilient_mmedia_offset); vm_fault_resilient_media_initiate++; goto RetryFault; } else { /* * we didn't succeed, lose the object reference * immediately. */ vm_object_deallocate(object); object = VM_OBJECT_NULL; /* no longer valid */ } /* * See why we failed, and take corrective action. */ switch (err) { case VM_FAULT_SUCCESS: case VM_FAULT_SUCCESS_NO_VM_PAGE: /* These aren't possible but needed to make the switch exhaustive */ break; case VM_FAULT_MEMORY_SHORTAGE: if (vm_page_wait((fault_info->fi_change_wiring) ? THREAD_UNINT : THREAD_ABORTSAFE)) { goto RetryFault; } ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_MEMORY_SHORTAGE), 0 /* arg */); OS_FALLTHROUGH; case VM_FAULT_INTERRUPTED: ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_INTERRUPTED), 0 /* arg */); kr = KERN_ABORTED; goto done; case VM_FAULT_RETRY: goto RetryFault; case VM_FAULT_MEMORY_ERROR: if (error_code) { kr = error_code; } else { kr = KERN_MEMORY_ERROR; } goto done; case VM_FAULT_BUSY: kr = KERN_ALREADY_WAITING; goto done; } } m = result_page; m_object = NULL; if (m != VM_PAGE_NULL) { m_object = VM_PAGE_OBJECT(m); assert((fault_info->fi_change_wiring && !wired) ? (top_page == VM_PAGE_NULL) : ((top_page == VM_PAGE_NULL) == (m_object == object))); } /* * What to do with the resulting page from vm_fault_page * if it doesn't get entered into the physical map: */ #define RELEASE_PAGE(m) \ MACRO_BEGIN \ vm_page_wakeup_done(VM_PAGE_OBJECT(m), m); \ if ( !VM_PAGE_PAGEABLE(m)) { \ vm_page_lockspin_queues(); \ if ( !VM_PAGE_PAGEABLE(m)) \ vm_page_activate(m); \ vm_page_unlock_queues(); \ } \ MACRO_END if (m != VM_PAGE_NULL) { old_copy_object = m_object->vo_copy; old_copy_version = m_object->vo_copy_version; } else { old_copy_object = VM_OBJECT_NULL; old_copy_version = 0; } /* * Drop the object locks, then go and retry with a new lookup and lock */ if (m != VM_PAGE_NULL) { vm_object_unlock(m_object); } else { vm_object_unlock(object); } /* * no object locks are held at this point */ vm_object_t retry_object; vm_object_offset_t retry_offset; vm_prot_t retry_prot; map = original_map; vm_map_lock_ctx_init(ctx); vm_lock_contention_event_dev(map, &vm_fault_busy_trylock_count, VMLP_EVENT_LC_NONE, vaddr, vaddr); /* * Just do a try lock, s.t. we don't try to take an entry lock * while we hold the busy bit. This is because wire takes the busy bit * while it holds the entry lock, so doing so in the reverse order here * could cause a deadlock. */ kr = vm_map_lookup_object_and_lock_entry(&map, vaddr, fault_type, &retry_object, &entry, &retry_offset, &retry_prot, &wired, fault_info, &real_map, ctx, vml_ctx_for_vaddr, true /* try_lock_entry */); pmap = real_map->pmap; if (kr != KERN_SUCCESS) { if (m != VM_PAGE_NULL) { assert(VM_PAGE_OBJECT(m) == m_object); /* * retake the lock so that * we can drop the paging reference * in vm_fault_cleanup and do the * vm_page_wakeup_done() in RELEASE_PAGE */ vm_object_lock(m_object); RELEASE_PAGE(m); vm_fault_cleanup(m_object, top_page); } else { /* * retake the lock so that * we can drop the paging reference * in vm_fault_cleanup */ vm_object_lock(object); vm_fault_cleanup(object, top_page); } vm_object_deallocate(object); if (kr == VMRL_ERR_LOCK_ALREADY_HELD) { /* * Our try_lock failed, just retry the fault from the top */ vm_lock_contention_event(map, &vm_fault_busy_retry_count, VMLP_EVENT_LC_VM_FAULT_BUSY_RETRY, vaddr, vaddr); goto RetryFault; } if (kr == KERN_INVALID_ADDRESS) { ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_ADDRESS_NOT_FOUND), 0 /* arg */); } goto done; } if ((retry_object != object) || (retry_offset != offset)) { if (m != VM_PAGE_NULL) { assert(VM_PAGE_OBJECT(m) == m_object); /* * retake the lock so that * we can drop the paging reference * in vm_fault_cleanup and do the * vm_page_wakeup_done() in RELEASE_PAGE */ vm_object_lock(m_object); RELEASE_PAGE(m); vm_fault_cleanup(m_object, top_page); } else { /* * retake the lock so that * we can drop the paging reference * in vm_fault_cleanup */ vm_object_lock(object); vm_fault_cleanup(object, top_page); } vm_object_deallocate(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } /* * Check whether the protection has changed or the object * has been copied while we left the map unlocked. */ if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, retry_prot)) { /* If the pmap layer cares, pass the full set. */ prot = retry_prot; } else { prot &= retry_prot; } if (m != VM_PAGE_NULL) { assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p", m, m_object); assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL); if (vm_object_is_contended_for_kdbg(m_object)) { KDBG(VMDBG_CODE(DBG_VM_FAULT_SLOW_OBJECT_CONTENTION) | DBG_FUNC_NONE, trace_real_vaddr, m_object->internal, m_object->copy_strategy, fault_type); } vm_object_lock(m_object); } else { if (vm_object_is_contended_for_kdbg(object)) { KDBG(VMDBG_CODE(DBG_VM_FAULT_SLOW_OBJECT_CONTENTION) | DBG_FUNC_NONE, trace_real_vaddr, object->internal, object->copy_strategy, fault_type); } vm_object_lock(object); } if ((prot & VM_PROT_WRITE) && m != VM_PAGE_NULL && (m_object->vo_copy != old_copy_object || m_object->vo_copy_version != old_copy_version)) { /* * The copy object changed while the top-level object * was unlocked, so take away write permission. */ if (pmap_has_prot_policy(pmap, fault_info->pmap_options & PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE, prot)) { /* * This pmap enforces extra constraints for this set * of protections, so we can't change the protections. * This mapping should have been setup to avoid * copy-on-write since that requires removing write * access. */ panic("%s: pmap %p vaddr 0x%llx prot 0x%x options 0x%x m%p obj %p copyobj %p", __FUNCTION__, pmap, (uint64_t)vaddr, prot, fault_info->pmap_options, m, m_object, m_object->vo_copy); } prot &= ~VM_PROT_WRITE; } if (!need_copy && !fault_info->no_copy_on_read && m != VM_PAGE_NULL && VM_PAGE_OBJECT(m) != object && !VM_PAGE_OBJECT(m)->pager_trusted && vm_protect_privileged_from_untrusted && !VM_PAGE_OBJECT(m)->code_signed && current_proc_is_privileged()) { /* * We found the page we want in an "untrusted" VM object * down the shadow chain. Since the target is "privileged" * we want to perform a copy-on-read of that page, so that the * mapped object gets a stable copy and does not have to * rely on the "untrusted" object to provide the same * contents if the page gets reclaimed and has to be paged * in again later on. * * Special case: if the mapping is executable and the untrusted * object is code-signed and the process is "cs_enforced", we * do not copy-on-read because that would break code-signing * enforcement expectations (an executable page must belong * to a code-signed object) and we can rely on code-signing * to re-validate the page if it gets evicted and paged back in. */ // printf("COPY-ON-READ %s:%d map %p vaddr 0x%llx obj %p offset 0x%llx found page %p (obj %p offset 0x%llx) UNTRUSTED -> need copy-on-read\n", __FUNCTION__, __LINE__, map, (uint64_t)vaddr, object, offset, m, VM_PAGE_OBJECT(m), m->vmp_offset); vm_copied_on_read++; need_copy_on_read = TRUE; need_copy = TRUE; } else { need_copy_on_read = FALSE; } /* * If we want to wire down this page, but no longer have * adequate permissions, we must start all over. * If we decided to copy-on-read, we must also start all over. */ if ((wired && (fault_type != (prot | VM_PROT_WRITE))) || need_copy_on_read) { if (m != VM_PAGE_NULL) { assert(VM_PAGE_OBJECT(m) == m_object); RELEASE_PAGE(m); vm_fault_cleanup(m_object, top_page); } else { vm_fault_cleanup(object, top_page); } vm_object_deallocate(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); goto RetryFault; } if (m != VM_PAGE_NULL) { /* * Put this page into the physical map. * We had to do the unlock above because pmap_enter * may cause other faults. The page may be on * the pageout queues. If the pageout daemon comes * across the page, it will remove it from the queues. */ if (fault_page_size < PAGE_SIZE) { DEBUG4K_FAULT("map %p original %p pmap %p va 0x%llx pa 0x%llx(0x%llx+0x%llx) prot 0x%x caller_prot 0x%x\n", map, original_map, pmap, (uint64_t)vaddr, (uint64_t)((((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT) + fault_phys_offset), (uint64_t)(((pmap_paddr_t)VM_PAGE_GET_PHYS_PAGE(m)) << PAGE_SHIFT), (uint64_t)fault_phys_offset, prot, caller_prot); assertf((!(fault_phys_offset & FOURK_PAGE_MASK) && fault_phys_offset < PAGE_SIZE), "0x%llx\n", (uint64_t)fault_phys_offset); } else { assertf(fault_phys_offset == 0, "0x%llx\n", (uint64_t)fault_phys_offset); } assertf(VM_PAGE_OBJECT(m) == m_object, "m=%p m_object=%p", m, m_object); assert(VM_PAGE_OBJECT(m) != VM_OBJECT_NULL); need_retry = false; if (caller_pmap) { kr = vm_fault_enter(m, caller_pmap, caller_pmap_addr, fault_page_size, fault_phys_offset, prot, caller_prot, wired, wire_tag, fault_info, &need_retry, &type_of_fault, &object_lock_type, &page_sleep_needed); } else { kr = vm_fault_enter(m, pmap, vaddr, fault_page_size, fault_phys_offset, prot, caller_prot, wired, wire_tag, fault_info, &need_retry, &type_of_fault, &object_lock_type, &page_sleep_needed); } assert(VM_PAGE_OBJECT(m) == m_object); vm_fault_trace( real_vaddr, trace_real_vaddr, m, fault_info, m_object, caller_prot, vm_fault_type_for_tracing(need_copy_on_read, type_of_fault)); if ((kr != KERN_SUCCESS) || page_sleep_needed || need_retry) { /* abort this page fault */ vm_page_wakeup_done(m_object, m); vm_fault_cleanup(m_object, top_page); vm_object_deallocate(object); if (need_retry) { /* * We could not expand the page table while holding an * object lock. * Expand it now and retry the fault. */ assert3u(kr, ==, KERN_RESOURCE_SHORTAGE); if (caller_pmap) { (void)pmap_enter_options( caller_pmap, caller_pmap_addr, 0, 0, 0, 0, 0, PMAP_OPTIONS_NOENTER, NULL, PMAP_MAPPING_TYPE_INFER); } else { (void)pmap_enter_options( pmap, vaddr, 0, 0, 0, 0, 0, PMAP_OPTIONS_NOENTER, NULL, PMAP_MAPPING_TYPE_INFER); } need_retry = FALSE; kr = KERN_SUCCESS; /* retry fault instead of failing below */ } vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); if (kr != KERN_SUCCESS) { goto done; } goto RetryFault; } if (physpage_p != NULL) { /* for vm_map_wire_and_extract() */ *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); if (prot & VM_PROT_WRITE) { vm_object_lock_assert_exclusive(m_object); m->vmp_dirty = TRUE; } } } else { vm_map_offset_t laddr, ldelta, hdelta; uint16_t superpage; pmap_t block_map_pmap; addr64_t block_map_va; pmap_paddr_t block_map_pa; int block_map_wimg; /* * do a pmap block mapping from the physical address * in the object * * hdelta/ldelta are initially set as the largest supported * ~(PAGE_MASK) on the system. */ laddr = vm_map_lock_ctx_from_parent_address(ctx, vaddr); hdelta = ldelta = (vm_map_offset_t)0xFFFFFFFFFFFFF000ULL; if (ldelta > (laddr - entry->vme_start)) { ldelta = laddr - entry->vme_start; } if (hdelta > (entry->vme_end - laddr)) { hdelta = entry->vme_end - laddr; } assert(object != VM_OBJECT_NULL); assert(VME_OBJECT(entry) == object); if (!object->pager_created && object->phys_contiguous && VME_OFFSET(entry) == 0 && (entry->vme_end - entry->vme_start == object->vo_size) && VM_MAP_PAGE_ALIGNED(entry->vme_start, (object->vo_size - 1))) { superpage = VM_MEM_SUPERPAGE; } else { superpage = 0; } if (superpage && physpage_p) { /* for vm_map_wire_and_extract() */ *physpage_p = (ppnum_t) ((((vm_map_offset_t) object->vo_shadow_offset) + VME_OFFSET(entry) + (laddr - entry->vme_start)) >> PAGE_SHIFT); } /* * Set up a block mapped area */ assert((uint32_t)((ldelta + hdelta) >> fault_page_shift) == ((ldelta + hdelta) >> fault_page_shift)); block_map_pa = (pmap_paddr_t)(((vm_map_offset_t)(object->vo_shadow_offset)) + VME_OFFSET(entry) + (laddr - entry->vme_start) - ldelta); block_map_wimg = VM_WIMG_MASK & (int)object->wimg_bits; if (caller_pmap) { block_map_pmap = caller_pmap; block_map_va = (addr64_t)(caller_pmap_addr - ldelta); } else { block_map_pmap = real_map->pmap; block_map_va = (addr64_t)(vaddr - ldelta); } #if HAS_MTE /* * We hit this path if we return SUCCESS from vm_fault_page but don't * return a page. This happens if we're trying to fault in a * phys_contiguous object (used by device pagers and superpages), or * if the page is non-VM managed. Both of these cases are not * expected to occur with MTE. */ assert(!vm_should_override_mte_cacheattr(block_map_pmap, object, block_map_va, block_map_pa)); #endif /* HAS_MTE */ kr = pmap_map_block_addr(block_map_pmap, block_map_va, block_map_pa, (uint32_t)((ldelta + hdelta) >> fault_page_shift), prot, block_map_wimg | superpage, 0); if (kr != KERN_SUCCESS) { goto cleanup; } } /* * Success */ kr = KERN_SUCCESS; /* * TODO: could most of the done cases just use cleanup? */ cleanup: /* * Unlock everything, and return */ if (m != VM_PAGE_NULL) { if (__improbable(rtfault && !m->vmp_realtime && vm_pageout_protect_realtime)) { vm_page_lock_queues(); if (!m->vmp_realtime) { m->vmp_realtime = true; VM_COUNTER_INC(&vm_page_realtime_count); } vm_page_unlock_queues(); } assert(VM_PAGE_OBJECT(m) == m_object); if (!m_object->internal && (fault_type & VM_PROT_WRITE)) { vm_object_paging_begin(m_object); assert3p(written_on_object, ==, VM_OBJECT_NULL); written_on_object = m_object; written_on_pager = m_object->pager; written_on_offset = m_object->paging_offset + m->vmp_offset; } vm_page_wakeup_done(m_object, m); vm_fault_cleanup(m_object, top_page); } else { vm_fault_cleanup(object, top_page); } vm_object_deallocate(object); vm_fault_unlock_ctx(ctx, vml_ctx_for_vaddr); #undef RELEASE_PAGE done: thread_interrupt_level(interruptible_state); if (resilient_media_object != VM_OBJECT_NULL) { assert(resilient_media_retry); assert(resilient_media_offset != (vm_object_offset_t)-1); /* release extra reference on failed object */ // printf("FBDP %s:%d resilient_media_object %p deallocate\n", __FUNCTION__, __LINE__, resilient_media_object); vm_object_deallocate(resilient_media_object); resilient_media_object = VM_OBJECT_NULL; resilient_media_offset = (vm_object_offset_t)-1; resilient_media_retry = false; vm_fault_resilient_media_release++; } assert(!resilient_media_retry); /* * Only I/O throttle on faults which cause a pagein/swapin. */ if ((type_of_fault == DBG_PAGEIND_FAULT) || (type_of_fault == DBG_PAGEINV_FAULT) || (type_of_fault == DBG_COMPRESSOR_SWAPIN_FAULT)) { throttle_lowpri_io(1); } else { if (kr == KERN_SUCCESS && type_of_fault != DBG_CACHE_HIT_FAULT && type_of_fault != DBG_GUARD_FAULT) { if ((throttle_delay = vm_page_throttled(TRUE))) { if (vm_debug_events) { if (type_of_fault == DBG_COMPRESSOR_FAULT) { VM_DEBUG_EVENT(vmf_compressordelay, DBG_VM_FAULT_COMPRESSORDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); } else if (type_of_fault == DBG_COW_FAULT) { VM_DEBUG_EVENT(vmf_cowdelay, DBG_VM_FAULT_COWDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); } else { VM_DEBUG_EVENT(vmf_zfdelay, DBG_VM_FAULT_ZFDELAY, DBG_FUNC_NONE, throttle_delay, 0, 0, 0); } } __VM_FAULT_THROTTLE_FOR_PAGEOUT_SCAN__(throttle_delay); } } } if (written_on_object) { vnode_pager_dirtied(written_on_pager, written_on_offset, written_on_offset + PAGE_SIZE_64); vm_object_lock(written_on_object); vm_object_paging_end(written_on_object); vm_object_unlock(written_on_object); written_on_object = VM_OBJECT_NULL; } if (rtfault) { vm_record_rtfault(cthread, fstart, trace_vaddr, type_of_fault); } KDBG_RELEASE( (VMDBG_CODE(DBG_VM_FAULT_INTERNAL)) | DBG_FUNC_END, ((uint64_t)trace_vaddr >> 32), trace_vaddr, kr, vm_fault_type_for_tracing(need_copy_on_read, type_of_fault)); if (fault_page_size < PAGE_SIZE && kr != KERN_SUCCESS) { DEBUG4K_FAULT("map %p original %p vaddr 0x%llx -> 0x%x\n", map, original_map, (uint64_t)trace_real_vaddr, kr); } vmlp_api_end(VM_FAULT_INTERNAL, kr); return kr; } /* * vm_fault_wire_resident_pages: * * Inform the pmap that a range of memory is wired and fault it down * The pages in the range must already be wired. */ kern_return_t vm_fault_wire_resident_pages( vm_map_t map, vm_map_entry_t entry, vm_prot_t prot, vm_tag_t wire_tag, ppnum_t *physpage_p, vm_map_lock_ctx_t vml_ctx) { vm_map_offset_t va; vm_map_offset_t end_addr = entry->vme_end; kern_return_t rc; vm_map_size_t effective_page_size; pmap_t pmap = map->pmap; vm_map_offset_t pmap_addr = entry->vme_start; if (entry->is_sub_map) { panic("submap entries should never be directly wired. entry: %p", entry); } if (VME_OBJECT(entry) == VM_OBJECT_NULL) { panic("Any objects should already have been resolved at wiring time. entry: %p", entry); } if (VME_OBJECT(entry)->phys_contiguous) { return KERN_SUCCESS; } /* * Inform the physical mapping system that the * range of addresses may not fault, so that * page tables and such can be locked down as well. */ pmap_pageable(pmap, pmap_addr, pmap_addr + (end_addr - entry->vme_start), FALSE); /* * We simulate a fault to get the page and enter it * in the physical map. */ effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE); for (va = entry->vme_start; va < end_addr; va += effective_page_size) { rc = vm_fault_wire_fast(map, va, prot, wire_tag, entry, pmap, pmap_addr + (va - entry->vme_start), physpage_p); if (rc != KERN_SUCCESS) { struct vm_object_fault_info fault_info = { .interruptible = (pmap == kernel_pmap) ? THREAD_UNINT : THREAD_ABORTSAFE, .behavior = VM_BEHAVIOR_SEQUENTIAL, .fi_change_wiring = true, }; if (os_sub_overflow(end_addr, va, &fault_info.cluster_size)) { fault_info.cluster_size = UPL_SIZE_MAX; } rc = vm_fault_internal(map, va, prot, wire_tag, pmap, (pmap_addr + (va - entry->vme_start)), physpage_p, &fault_info, vml_ctx); if (rc == KERN_SUCCESS) { /* * We already wired this at a page level, but * vm_fault_internal added another wiring. * Remove that extra wiring. */ vm_fault_unwire_object_pages(map, VME_OBJECT(entry), VME_OFFSET(entry) + (va - entry->vme_start), effective_page_size); } DTRACE_VM2(softlock, int, 1, (uint64_t *), NULL); } if (rc != KERN_SUCCESS) { vm_object_offset_t current_offset = va - entry->vme_start; /* * fault_unwire the pages we've fault_wired. * regularly unwire the pages we haven't. */ vm_fault_unwire(map, entry, false, pmap, pmap_addr, va); vm_fault_unwire_object_pages(map, VME_OBJECT(entry), VME_OFFSET(entry) + current_offset, (entry->vme_end - entry->vme_start) - current_offset); return rc; } } return KERN_SUCCESS; } /* * For this to happen, the object likely was shrunk during the call to vm_wire */ __abortlike static void __vm_fault_wire_object_offset_oob_panic( vm_map_t map, vm_object_t object, vm_object_offset_t offset, vm_map_size_t wire_size, vm_map_size_t object_size) { panic("vm_fault_wire_object_pages(%p,%p): offset(0x%llx) out of bounds with wire_size = 0x%llx and object size = 0x%llx", map, object, (uint64_t)offset, (uint64_t)wire_size, (uint64_t)object_size); } /*! * @abstract * Wire all the pages in an object for a given range * * @discussion * Wire every page of a given range in a specified object. * Guard pages are ignored and not actually wired by this. * * @param map the map the wiring will be entered into * @param object the object * @param offset the starting offset into the object at which pages should * be wired (must be in the object) * @param wire_size the size of the range that should be wired * @param tag the tag the wiring should be billed to * @param interruptible the interruptibility of the wiring */ kern_return_t vm_fault_wire_object_pages( vm_map_t map, vm_object_t object, vm_object_offset_t offset, vm_map_size_t wire_size, vm_tag_t tag, wait_interrupt_t interruptible) { vm_page_t page; vm_page_t top_page; vm_prot_t prot; vm_map_size_t amount_left = wire_size; kern_return_t error = 0; vm_fault_return_t result; vm_object_offset_t start_offset = offset; struct vm_object_fault_info fault_info = {}; vm_map_size_t effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE); /* * In order not to confuse the clustered pageins, align * the different offsets on a page boundary. */ fault_info.interruptible = interruptible; fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL; fault_info.lo_offset = vm_object_trunc_page(offset); fault_info.hi_offset = fault_info.lo_offset + wire_size; fault_info.stealth = TRUE; fault_info.fi_change_wiring = TRUE; do { /* while (amount_left > 0) */ prot = VM_PROT_WRITE | VM_PROT_READ; /* cap cluster size at maximum UPL size */ upl_size_t cluster_size; if (os_convert_overflow(amount_left, &cluster_size)) { cluster_size = UPL_SIZE_MAX; } fault_info.cluster_size = cluster_size; vm_page_grab_prime(); vm_object_lock(object); if (object->internal && start_offset + wire_size > object->vo_size) { __vm_fault_wire_object_offset_oob_panic(map, object, offset, wire_size, object->vo_size); } vm_object_paging_begin(object); page = VM_PAGE_NULL; result = vm_fault_page(object, offset, VM_PROT_WRITE | VM_PROT_READ, FALSE, FALSE, /* page not looked up */ &prot, &page, &top_page, (int *)0, &error, map->no_zero_fill, &fault_info, NULL); switch (result) { case VM_FAULT_SUCCESS: break; case VM_FAULT_RETRY: continue; case VM_FAULT_MEMORY_SHORTAGE: if (vm_page_wait(interruptible)) { continue; } ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_COPY_MEMORY_SHORTAGE), 0 /* arg */); OS_FALLTHROUGH; case VM_FAULT_INTERRUPTED: vm_fault_unwire_object_pages(map, object, start_offset, wire_size - amount_left); return MACH_SEND_INTERRUPTED; case VM_FAULT_SUCCESS_NO_VM_PAGE: /* success but no VM page: fail */ vm_object_paging_end(object); if (object->phys_contiguous) { /* * Physically contiguous_object: no page is expected. * There is no page to wire here, move onto the * next offset. */ amount_left -= effective_page_size; offset += effective_page_size; vm_object_unlock(object); continue; } vm_object_unlock(object); OS_FALLTHROUGH; case VM_FAULT_MEMORY_ERROR: /* * We may have gotten this due to a guard page. We allow * "wiring" of guard pages under the assumption they will * never actually be entered in a pmap. */ vm_object_lock(object); page = vm_page_lookup(object, offset); if (page == VM_PAGE_NULL) { /* * No page found at this address. One way this * could happen is if vm_fault_page found a page * with vmp_error = true. */ } else if (vm_page_is_guard(page)) { vm_object_unlock(object); amount_left -= effective_page_size; offset += effective_page_size; continue; } /* * Fail the wiring and unwire what we've wired so far. */ vm_object_unlock(object); vm_fault_unwire_object_pages(map, object, start_offset, wire_size - amount_left); if (error) { return error; } else { return KERN_MEMORY_ERROR; } default: panic("vm_fault_copy: unexpected error 0x%x from " "vm_fault_page()\n", result); } assert((prot & VM_PROT_WRITE) != VM_PROT_NONE); assert(object == VM_PAGE_OBJECT(page)); vm_page_lockspin_queues(); vm_page_wire(page, tag, TRUE); vm_page_unlock_queues(); vm_page_wakeup_done(object, page); if (top_page != VM_PAGE_NULL) { VM_PAGE_FREE(top_page); } vm_object_paging_end(object); vm_object_unlock(object); amount_left -= effective_page_size; offset += effective_page_size; } while (amount_left > 0); return KERN_SUCCESS; } /*! * @abstract * Find a page to later be unwired * a vm_page_wakeup_done(object, page) and a vm_fault_cleanup(object, top_page) * should be done by the caller after unwiring the page. * * @param top_page Out parameter to later be cleaned up with vm_fault_cleanup * * @returns * - NULL no more work needs to be done, we are already in the state we want * - a page the page to be unwired */ static vm_page_t vm_fault_unwire_find_page( vm_map_t map, vm_object_t object, vm_object_offset_t fault_offset, vm_object_fault_info_t fault_info, vm_page_t * top_page, bool __assert_only deallocate) { vm_prot_t prot; vm_page_t result_page; vm_fault_return_t result; assert(!object->phys_contiguous); /* Physically contiguous pages are not wired */ do { prot = VM_PROT_NONE; vm_object_lock(object); vm_object_paging_begin(object); result_page = VM_PAGE_NULL; result = vm_fault_page( object, fault_offset, VM_PROT_NONE, TRUE, FALSE, /* page not looked up */ &prot, &result_page, top_page, (int *)0, NULL, map->no_zero_fill, fault_info, NULL); } while (result == VM_FAULT_RETRY); /* * If this was a mapping to a file on a device that has been forcibly * unmounted, then we won't get a page back from vm_fault_page(). Just * move on to the next one in case the remaining pages are mapped from * different objects. During a forced unmount, the object is terminated * so the alive flag will be false if this happens. A forced unmount will * will occur when an external disk is unplugged before the user does an * eject, so we don't want to panic in that situation. */ if (result == VM_FAULT_MEMORY_ERROR) { if (!object->alive) { return NULL; } if (!object->internal && object->pager == NULL) { return NULL; } } if (result == VM_FAULT_MEMORY_ERROR && is_kernel_object(object)) { /* * This must have been allocated with * KMA_KOBJECT and KMA_VAONLY and there's * no physical page at this offset. * We're done (no page to free). */ assert(deallocate); return NULL; } if (result != VM_FAULT_SUCCESS) { panic("vm_fault_unwire_find_page: failure"); } return result_page; } void vm_fault_unwire_object_pages( vm_map_t map, vm_object_t object, vm_object_offset_t start_offset, vm_map_size_t unwire_size) { /* * If it's marked phys_contiguous, then vm_fault_wire() didn't actually * do anything since such memory is wired by default. We don't have * anything to undo here. */ if (object->phys_contiguous) { return; } vm_map_size_t effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE); struct vm_object_fault_info fault_info = {}; /* * In order not to confuse the clustered pageins, align * the different offsets on a page boundary. */ fault_info.interruptible = THREAD_UNINT; fault_info.behavior = VM_BEHAVIOR_SEQUENTIAL; fault_info.lo_offset = vm_object_trunc_page(start_offset); fault_info.hi_offset = fault_info.lo_offset + unwire_size; fault_info.stealth = TRUE; for (vm_object_offset_t offset = start_offset; offset < start_offset + unwire_size; offset += effective_page_size) { vm_page_t top_page; vm_object_t result_object; vm_page_t result_page; /* cap cluster size at maximum UPL size */ upl_size_t cluster_size; if (os_convert_overflow(unwire_size - (offset - start_offset), &cluster_size)) { cluster_size = UPL_SIZE_MAX; } fault_info.cluster_size = cluster_size; result_page = vm_fault_unwire_find_page(map, object, offset, &fault_info, &top_page, false); if (result_page == VM_PAGE_NULL) { /* Nothing for us to do */ continue; } result_object = VM_PAGE_OBJECT(result_page); if (VM_PAGE_WIRED(result_page)) { vm_page_lockspin_queues(); vm_page_unwire(result_page, TRUE); vm_page_unlock_queues(); } vm_page_wakeup_done(result_object, result_page); vm_fault_cleanup(result_object, top_page); } } /* * vm_fault_unwire: * * Unwire a range of virtual addresses in a map. */ __mockable void vm_fault_unwire( vm_map_t map, vm_map_entry_t entry, bool deallocate, pmap_t pmap, vm_map_offset_t pmap_addr, vm_map_offset_t end_addr) { vm_map_offset_t va; vm_object_t object; struct vm_object_fault_info fault_info = { .interruptible = THREAD_UNINT, }; unsigned int unwired_pages; vm_map_size_t effective_page_size; if (entry->is_sub_map) { panic("We never wire submap entries, so we should not need to unwire them. entry: %p", entry); } if (VME_OBJECT(entry) == VM_OBJECT_NULL) { panic("Any objects should already have been resolved at wiring time. entry: %p", entry); } object = VME_OBJECT(entry); /* * If it's marked phys_contiguous, then vm_fault_wire() didn't actually * do anything since such memory is wired by default. We don't have * anything to undo here. */ if (object->phys_contiguous) { return; } fault_info.interruptible = THREAD_UNINT; fault_info.behavior = entry->behavior; fault_info.user_tag = VME_ALIAS(entry); if (entry->iokit_acct || (!entry->is_sub_map && !entry->use_pmap)) { fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT; } fault_info.lo_offset = VME_OFFSET(entry); fault_info.hi_offset = (end_addr - entry->vme_start) + VME_OFFSET(entry); fault_info.no_cache = entry->no_cache; fault_info.stealth = TRUE; if (entry->vme_xnu_user_debug) { /* * Modified code-signed executable region: wired pages must * have been copied, so they should be XNU_USER_DEBUG rather * than XNU_USER_EXEC. */ fault_info.pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG; } unwired_pages = 0; /* * Since the pages are wired down, we must be able to * get their mappings from the physical map system. */ effective_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE); for (va = entry->vme_start; va < end_addr; va += effective_page_size) { vm_page_t result_page; vm_object_t result_object; vm_page_t top_page = VM_PAGE_NULL; /* cap cluster size at maximum UPL size */ upl_size_t cluster_size; if (os_sub_overflow(end_addr, va, &cluster_size)) { cluster_size = UPL_SIZE_MAX; } fault_info.cluster_size = cluster_size; result_page = vm_fault_unwire_find_page(map, object, VME_OFFSET(entry) + (va - entry->vme_start), &fault_info, &top_page, deallocate); if (result_page == VM_PAGE_NULL) { /* Nothing for us to do */ continue; } result_object = VM_PAGE_OBJECT(result_page); if (deallocate) { assert(VM_PAGE_GET_PHYS_PAGE(result_page) != vm_page_fictitious_addr); pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(result_page)); if (VM_PAGE_WIRED(result_page)) { unwired_pages++; } VM_PAGE_FREE(result_page); } else { if (pmap && !vm_page_is_guard(result_page)) { pmap_change_wiring(pmap, pmap_addr + (va - entry->vme_start), FALSE); } if (VM_PAGE_WIRED(result_page)) { vm_page_lockspin_queues(); vm_page_unwire(result_page, TRUE); vm_page_unlock_queues(); unwired_pages++; } if (entry->zero_wired_pages && (entry->protection & VM_PROT_WRITE) && #if __arm64e__ !entry->used_for_tpro && #endif /* __arm64e__ */ !entry->used_for_jit) { pmap_zero_page(VM_PAGE_GET_PHYS_PAGE(result_page)); } vm_page_wakeup_done(result_object, result_page); } vm_fault_cleanup(result_object, top_page); } /* * Inform the physical mapping system that the range * of addresses may fault, so that page tables and * such may be unwired themselves. */ pmap_pageable(pmap, pmap_addr, pmap_addr + (end_addr - entry->vme_start), TRUE); if (is_kernel_object(object)) { /* * Would like to make user_tag in vm_object_fault_info * vm_tag_t (unsigned short) but user_tag derives its value from * VME_ALIAS(entry) at a few places and VME_ALIAS, in turn, casts * to an _unsigned int_ which is used by non-fault_info paths throughout the * code at many places. * * So, for now, an explicit truncation to unsigned short (vm_tag_t). */ assertf((fault_info.user_tag & VME_ALIAS_MASK) == fault_info.user_tag, "VM Tag truncated from 0x%x to 0x%x\n", fault_info.user_tag, (fault_info.user_tag & VME_ALIAS_MASK)); vm_tag_update_size((vm_tag_t) fault_info.user_tag, -ptoa_64(unwired_pages), NULL); } } /* * vm_fault_wire_fast: * * Handle common case of a wire down page fault at the given address. * If successful, the page is inserted into the associated physical map. * The map entry is passed in to avoid the overhead of a map lookup. * The page must already be wired. * * NOTE: the given address should be truncated to the * proper page address. * * KERN_SUCCESS is returned if the page fault is handled; otherwise, * a standard error specifying why the fault is fatal is returned. * * The map in question must be referenced, and remains so. * Caller has a lock on the entry. * * This is a stripped version of vm_fault() for wiring pages. Anything * other than the common case will return KERN_FAILURE, and the caller * is expected to call vm_fault(). */ static kern_return_t vm_fault_wire_fast( __unused vm_map_t map, vm_map_offset_t va, __unused vm_prot_t caller_prot, vm_tag_t wire_tag, vm_map_entry_t entry, pmap_t pmap, vm_map_offset_t pmap_addr, ppnum_t *physpage_p) { vm_object_t object; vm_object_offset_t offset; vm_page_t m; vm_prot_t prot; thread_t thread = current_thread(); int type_of_fault; kern_return_t kr; vm_map_size_t fault_page_size; vm_map_offset_t fault_phys_offset; struct vm_object_fault_info fault_info = { .interruptible = THREAD_UNINT, }; uint8_t object_lock_type = 0; counter_inc(&vm_statistics_faults); if (thread != THREAD_NULL) { counter_inc(&get_threadtask(thread)->faults); } /* * If this entry is not directly to a vm_object, bail out. */ if (entry->is_sub_map) { assert(physpage_p == NULL); return KERN_FAILURE; } /* * Find the backing store object and offset into it. */ object = VME_OBJECT(entry); offset = (va - entry->vme_start) + VME_OFFSET(entry); prot = entry->protection; /* * Make a reference to this object to prevent its * disposal while we are messing with it. */ object_lock_type = OBJECT_LOCK_EXCLUSIVE; vm_object_lock(object); vm_object_reference_locked(object); vm_object_paging_begin(object); /* * INVARIANTS (through entire routine): * * 1) At all times, we must either have the object * lock or a busy page in some object to prevent * some other thread from trying to bring in * the same page. * * 2) Once we have a busy page, we must remove it from * the pageout queues, so that the pageout daemon * will not grab it away. * */ if (entry->needs_copy) { panic("attempting to wire needs_copy memory"); } /* * Since we don't have the machinary to resolve CoW obligations on the fast * path, if we might have to push pages to a copy, just give up. */ if (object->vo_copy != VM_OBJECT_NULL) { kr = KERN_FAILURE; goto unlock_and_deallocate; } /* * Look for page in top-level object. If it's not there or * there's something going on, give up. */ m = vm_page_lookup(object, vm_object_trunc_page(offset)); if ((m == VM_PAGE_NULL) || (m->vmp_busy) || (m->vmp_unusual && (m->vmp_error || m->vmp_restart || m->vmp_absent))) { kr = KERN_FAILURE; goto unlock_and_deallocate; } if (vm_page_is_guard(m)) { /* * Guard pages are fictitious pages and are never * entered into a pmap, so let's say it's been wired... */ kr = KERN_SUCCESS; goto done; } /* * The page must already be wired */ assert(m->vmp_wire_count > 0); /* * Mark page busy for other threads. */ assert(!m->vmp_busy); m->vmp_busy = TRUE; assert(!m->vmp_absent); fault_info.user_tag = VME_ALIAS(entry); fault_info.pmap_options = 0; if (entry->iokit_acct || (!entry->is_sub_map && !entry->use_pmap)) { fault_info.pmap_options |= PMAP_OPTIONS_ALT_ACCT; } if (entry->vme_xnu_user_debug) { /* * Modified code-signed executable region: wiring will * copy the pages, so they should be XNU_USER_DEBUG rather * than XNU_USER_EXEC. */ fault_info.pmap_options |= PMAP_OPTIONS_XNU_USER_DEBUG; } if (entry->translated_allow_execute) { fault_info.pmap_options |= PMAP_OPTIONS_TRANSLATED_ALLOW_EXECUTE; } fault_page_size = MIN(VM_MAP_PAGE_SIZE(map), PAGE_SIZE); fault_phys_offset = offset - vm_object_trunc_page(offset); /* * Put this page into the physical map. */ type_of_fault = DBG_CACHE_HIT_FAULT; assert3p(VM_PAGE_OBJECT(m), ==, object); bool page_sleep_needed = false; bool need_retry = false; kr = vm_fault_enter(m, pmap, pmap_addr, fault_page_size, fault_phys_offset, prot, prot, TRUE, /* wired */ wire_tag, &fault_info, &need_retry, &type_of_fault, &object_lock_type, /* Exclusive lock mode. Will remain unchanged.*/ &page_sleep_needed); if ((kr != KERN_SUCCESS) || page_sleep_needed || need_retry) { kr = KERN_FAILURE; goto wakeup_unlock_and_deallocate; } done: /* * Unlock everything, and return */ if (physpage_p) { /* for vm_map_wire_and_extract() */ if (kr == KERN_SUCCESS) { assert3p(object, ==, VM_PAGE_OBJECT(m)); *physpage_p = VM_PAGE_GET_PHYS_PAGE(m); if (prot & VM_PROT_WRITE) { vm_object_lock_assert_exclusive(object); m->vmp_dirty = TRUE; } } else { *physpage_p = 0; } } wakeup_unlock_and_deallocate: if (m->vmp_busy) { vm_page_wakeup_done(object, m); } unlock_and_deallocate: vm_object_paging_end(object); vm_object_unlock(object); vm_object_deallocate(object); return kr; } /* * Routine: vm_fault_copy_cleanup * Purpose: * Release a page used by vm_fault_copy. */ static void vm_fault_copy_cleanup( vm_page_t page, vm_page_t top_page) { vm_object_t object = VM_PAGE_OBJECT(page); vm_object_lock(object); vm_page_wakeup_done(object, page); if (!VM_PAGE_PAGEABLE(page)) { vm_page_lockspin_queues(); if (!VM_PAGE_PAGEABLE(page)) { vm_page_activate(page); } vm_page_unlock_queues(); } vm_fault_cleanup(object, top_page); } static void vm_fault_copy_dst_cleanup( vm_page_t page) { vm_object_t object; if (page != VM_PAGE_NULL) { object = VM_PAGE_OBJECT(page); vm_object_lock(object); vm_page_lockspin_queues(); vm_page_unwire(page, TRUE); vm_page_unlock_queues(); vm_object_paging_end(object); vm_object_unlock(object); } } /* * Routine: vm_fault_copy * * Purpose: * Copy pages from one vm_object_t to another -- * neither the source nor destination pages need be resident. * * Before actually copying a page, various checks including the map * lock context's preflight and and those in * @c vm_map_copy_overwrite_can_page_copy will be run. * * In/out conditions: * The caller must hold a reference, but not a lock, to * each of the source and destination objects. * On entry, the context is streaming, with the cursor locked at *dst_entry_p * The entry lock may be dropped and re-acquired multiple times * On exit with KERN_SUCCESS, the context is again streaming locked * at *dst_entry_p (which may have changed). * On error, *dst_entry is NULL, and the context has no entry, but * still needs to be unlocked. * * Return values: * - KERN_SUCCESS No errors were encountered during the copy * - KERN_INTERRUPTED The operation was interrupted (only possible * if the "interruptible" argument is asserted). * - Other Some permanent error happened. * * The actual amount of data copied will be returned in the * "copy_size" argument. In the event that the destination map * changed sufficiently during this call, this amount may be less * than the amount requested. */ kern_return_t vm_fault_copy( vm_map_lock_ctx_t ctx, vm_object_t src_object, vm_object_offset_t src_offset, vm_map_size_t *const copy_size, /* INOUT */ vm_map_entry_t *const dst_entry_p, /* IN/OUT */ vm_object_t dst_object, vm_object_offset_t dst_offset, int interruptible) { vm_page_t result_page; vm_page_t src_page; vm_page_t src_top_page; vm_prot_t src_prot; vm_page_t dst_page; vm_page_t dst_top_page; vm_prot_t dst_prot; vm_map_size_t amount_left; vm_object_t old_copy_object; uint64_t old_copy_version; vm_object_t result_page_object = NULL; kern_return_t vm_fault_page_kr = 0, kr = KERN_SUCCESS; vm_fault_return_t vm_fault_page_return; vm_map_size_t part_size; struct vm_object_fault_info fault_info_src = {}; struct vm_object_fault_info fault_info_dst = {}; vmlp_api_start(VM_FAULT_COPY); /* * In order not to confuse the clustered pageins, align * the different offsets on a page boundary. */ amount_left = *copy_size; fault_info_src.interruptible = interruptible; fault_info_src.behavior = VM_BEHAVIOR_SEQUENTIAL; fault_info_src.lo_offset = vm_object_trunc_page(src_offset); fault_info_src.hi_offset = fault_info_src.lo_offset + amount_left; fault_info_src.stealth = TRUE; fault_info_dst.interruptible = interruptible; fault_info_dst.behavior = VM_BEHAVIOR_SEQUENTIAL; fault_info_dst.lo_offset = vm_object_trunc_page(dst_offset); fault_info_dst.hi_offset = fault_info_dst.lo_offset + amount_left; fault_info_dst.stealth = TRUE; do { /* while (amount_left > 0) */ vm_map_entry_t new_entry; vm_map_t dst_map = ctx->vmlc_map; /* * There may be a deadlock if both source and destination * pages are the same. To avoid this deadlock, the copy must * start by getting the destination page in order to apply * COW semantics if any. */ vm_map_range_stream_drop_without_advance(ctx); RetryDestinationFault:; dst_prot = VM_PROT_WRITE | VM_PROT_READ; vm_page_grab_prime(); vm_object_lock(dst_object); vm_object_paging_begin(dst_object); /* cap cluster size at maximum UPL size */ upl_size_t cluster_size; if (os_convert_overflow(amount_left, &cluster_size)) { cluster_size = 0 - (upl_size_t)PAGE_SIZE; } fault_info_dst.cluster_size = cluster_size; dst_page = VM_PAGE_NULL; vm_fault_page_return = vm_fault_page(dst_object, vm_object_trunc_page(dst_offset), VM_PROT_WRITE | VM_PROT_READ, FALSE, FALSE, /* page not looked up */ &dst_prot, &dst_page, &dst_top_page, (int *)0, &vm_fault_page_kr, dst_map->no_zero_fill, &fault_info_dst, ctx); switch (vm_fault_page_return) { case VM_FAULT_SUCCESS: break; case VM_FAULT_RETRY: goto RetryDestinationFault; case VM_FAULT_MEMORY_SHORTAGE: if (vm_page_wait(interruptible)) { goto RetryDestinationFault; } ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_FAULT_COPY_MEMORY_SHORTAGE), 0 /* arg */); OS_FALLTHROUGH; case VM_FAULT_INTERRUPTED: kr = MACH_SEND_INTERRUPTED; goto done; case VM_FAULT_SUCCESS_NO_VM_PAGE: /* success but no VM page: fail the copy */ vm_object_paging_end(dst_object); vm_object_unlock(dst_object); OS_FALLTHROUGH; case VM_FAULT_MEMORY_ERROR: if (vm_fault_page_kr) { kr = vm_fault_page_kr; } else { kr = KERN_MEMORY_ERROR; } goto done; default: panic("vm_fault_copy: unexpected error 0x%x from " "vm_fault_page()\n", vm_fault_page_return); } assert((dst_prot & VM_PROT_WRITE) != VM_PROT_NONE); assert(dst_object == VM_PAGE_OBJECT(dst_page)); old_copy_object = dst_object->vo_copy; old_copy_version = dst_object->vo_copy_version; /* * There exists the possiblity that the source and * destination page are the same. But we can't * easily determine that now. If they are the * same, the call to vm_fault_page() for the * destination page will deadlock. To prevent this we * wire the page so we can drop busy without having * the page daemon steal the page. We clean up the * top page but keep the paging reference on the object * holding the dest page so it doesn't go away. */ vm_page_lockspin_queues(); vm_page_wire(dst_page, VM_KERN_MEMORY_OSFMK, TRUE); vm_page_unlock_queues(); vm_page_wakeup_done(dst_object, dst_page); vm_object_unlock(dst_object); if (dst_top_page != VM_PAGE_NULL) { vm_object_lock(dst_object); VM_PAGE_FREE(dst_top_page); vm_object_paging_end(dst_object); vm_object_unlock(dst_object); } RetrySourceFault:; if (src_object == VM_OBJECT_NULL) { /* * No source object. We will just * zero-fill the page in dst_object. */ src_page = VM_PAGE_NULL; result_page = VM_PAGE_NULL; } else { vm_page_grab_prime(); vm_object_lock(src_object); src_page = vm_page_lookup(src_object, vm_object_trunc_page(src_offset)); if (src_page == dst_page) { src_prot = dst_prot; result_page = VM_PAGE_NULL; } else { src_prot = VM_PROT_READ; vm_object_paging_begin(src_object); /* cap cluster size at maximum UPL size */ if (os_convert_overflow(amount_left, &cluster_size)) { cluster_size = 0 - (upl_size_t)PAGE_SIZE; } fault_info_src.cluster_size = cluster_size; result_page = VM_PAGE_NULL; vm_fault_page_return = vm_fault_page( src_object, vm_object_trunc_page(src_offset), VM_PROT_READ, FALSE, FALSE, /* page not looked up */ &src_prot, &result_page, &src_top_page, (int *)0, &vm_fault_page_kr, FALSE, &fault_info_src, ctx); switch (vm_fault_page_return) { case VM_FAULT_SUCCESS: break; case VM_FAULT_RETRY: goto RetrySourceFault; case VM_FAULT_MEMORY_SHORTAGE: if (vm_page_wait(interruptible)) { goto RetrySourceFault; } OS_FALLTHROUGH; case VM_FAULT_INTERRUPTED: vm_fault_copy_dst_cleanup(dst_page); kr = MACH_SEND_INTERRUPTED; goto done; case VM_FAULT_SUCCESS_NO_VM_PAGE: /* success but no VM page: fail */ vm_object_paging_end(src_object); vm_object_unlock(src_object); OS_FALLTHROUGH; case VM_FAULT_MEMORY_ERROR: vm_fault_copy_dst_cleanup(dst_page); if (vm_fault_page_kr) { kr = vm_fault_page_kr; } else { kr = KERN_MEMORY_ERROR; } goto done; default: panic("vm_fault_copy(2): unexpected " "error 0x%x from " "vm_fault_page()\n", vm_fault_page_return); } result_page_object = VM_PAGE_OBJECT(result_page); assert((src_top_page == VM_PAGE_NULL) == (result_page_object == src_object)); } assert((src_prot & VM_PROT_READ) != VM_PROT_NONE); vm_object_unlock(result_page_object); } assert(dst_object == VM_PAGE_OBJECT(dst_page)); /* * If there's no valid entry, return out early. * Do a try lock to avoid the lock ordering of holding the busy * bit when taking an entry lock. */ vm_map_range_ex_lock_add_flags(ctx, VMRL_EX_TRY_LOCK_ENTRY); vm_lock_contention_event_dev(dst_map, &vm_fault_copy_busy_trylock_count, VMLP_EVENT_LC_NONE, ctx->vmlc_req_start, ctx->vmlc_req_end); new_entry = vm_map_range_stream_next_with_error(ctx, &kr); vm_map_range_ex_lock_remove_flags(ctx, VMRL_EX_TRY_LOCK_ENTRY); *dst_entry_p = new_entry; if (new_entry == VM_MAP_ENTRY_NULL) { if (result_page != VM_PAGE_NULL && src_page != dst_page) { vm_fault_copy_cleanup(result_page, src_top_page); } vm_fault_copy_dst_cleanup(dst_page); /* kr is error */ assert(kr != KERN_SUCCESS); if (kr == VMRL_ERR_LOCK_ALREADY_HELD) { vm_lock_contention_event(dst_map, &vm_fault_copy_busy_retry_count, VMLP_EVENT_LC_VM_FAULT_COPY_BUSY_RETRY, ctx->vmlc_req_start, ctx->vmlc_req_end); /* * Now that we've released the busy bit, actually * lock the relevant entry. * Let the caller of this function retry if * there is more to be copied. */ new_entry = vm_map_range_stream_next_with_error(ctx, &kr); *dst_entry_p = new_entry; } goto done; } assert(kr == KERN_SUCCESS); /* * Check to make sure we can actually do the page copy. * If we can't return out early. */ if (!vm_map_copy_overwrite_can_page_copy(dst_map, new_entry, dst_offset, dst_page, old_copy_object, old_copy_version)) { if (result_page != VM_PAGE_NULL && src_page != dst_page) { vm_fault_copy_cleanup(result_page, src_top_page); } vm_fault_copy_dst_cleanup(dst_page); goto done; } /* vm_map_copy_overwrite_can_page_copy() should have returned with the object locked. */ vm_object_lock_assert_exclusive(dst_object); /** * Avoid overwriting a page that has become busy while dst_object's lock was dropped. * Re-run the loop at the same position; if necessary, vm_fault_page() will wait * for the destination page to be unbusied. */ if (__improbable(dst_page->vmp_busy)) { vm_object_unlock(dst_object); if (result_page != VM_PAGE_NULL && src_page != dst_page) { vm_fault_copy_cleanup(result_page, src_top_page); } vm_fault_copy_dst_cleanup(dst_page); continue; } #if CONFIG_SPTM if (__improbable(PMAP_PAGE_IS_USER_EXECUTABLE(dst_page))) { /** * We've found a page with an executable frame type, which likely means its physical aperture * mapping is write-protected, so we won't be able to do the copy below. We'll need to remove * all extant mappings and retype the page, but first we need to make sure we can safely retype. */ if (__improbable(dst_page->vmp_cleaning || VM_PAGE_IOPL_WIRED(dst_page))) { /** * Clean up our locking state and source page/object references so that we can safely * sleep on the destination page. */ vm_object_unlock(dst_object); if (result_page != VM_PAGE_NULL && src_page != dst_page) { vm_fault_copy_cleanup(result_page, src_top_page); } vm_object_lock(dst_object); assert3p(dst_object, ==, VM_PAGE_OBJECT(dst_page)); if (VM_PAGE_IOPL_WIRED(dst_page)) { /** * If the page is wired for I/O, we can't safely retype and we can't reasonably * wait for the I/O to finish. */ vm_object_unlock(dst_object); vm_fault_copy_dst_cleanup(dst_page); kr = KERN_MEMORY_ERROR; goto done; } else if (dst_page->vmp_cleaning) { /** * We can wait for an in-place clean to finish. * NOTE: The page is still wired and we still hold a paging reference on the object * at this point, both of which will be undone by vm_fault_copy_dst_cleanup(). * Is it really safe to sleep on the page in that state? */ wait_result_t wres = vm_page_sleep(dst_object, dst_page, interruptible, LCK_SLEEP_UNLOCK); vm_fault_copy_dst_cleanup(dst_page); if (wres == THREAD_AWAKENED || wres == THREAD_RESTART) { continue; } else { kr = KERN_ABORTED; goto done; } } else { /** * The cleaning or I/O state we initially observed went away while the object * lock was dropped. Since we've torn down much of our state already, we need * to rerun the copy loop at the same position. */ vm_object_unlock(dst_object); vm_fault_copy_dst_cleanup(dst_page); continue; } } /** * Remove all existing mappings and retype the page. Consumers of the page will be forced to * re-fault it and, if necessary, re-validate it for codesigning. */ pmap_disconnect_options(VM_PAGE_GET_PHYS_PAGE(dst_page), PMAP_OPTIONS_RETYPE, NULL); } #endif /* CONFIG_SPTM */ /** * Copy the page, and note that it is dirty immediately. * NOTE: if we're concerned about lock contention due to holding the object lock across the copy, * we could instead consider marking dst_page busy and dropping the lock, but only if we have some * other means of preventing a CoW bypass on this path. */ vm_object_offset_t src_po, dst_po; src_po = src_offset - vm_object_trunc_page(src_offset); dst_po = dst_offset - vm_object_trunc_page(dst_offset); if (dst_po > src_po) { part_size = PAGE_SIZE - dst_po; } else { part_size = PAGE_SIZE - src_po; } if (part_size > (amount_left)) { part_size = amount_left; } assert((vm_size_t) part_size == part_size); assert((vm_offset_t) dst_po == dst_po); /** * For the case in which we're copying a full page, we don't want to use vm_page_copy() here * because that will do CS validation (unnecessarily in this case) which requires the source * object lock to be held, which in turn would complicate our locking requirements since we * already hold the destination object lock. Instead we treat the full-page case as simply * a zero-offset/PAGE_SIZE variant of the partial-page case, which keeps the code simpler * anyway. */ if (result_page == VM_PAGE_NULL) { vm_page_part_zero_fill(dst_page, (vm_offset_t) dst_po, (vm_size_t) part_size); } else { assert((vm_offset_t) src_po == src_po); vm_page_part_copy(result_page, (vm_offset_t) src_po, dst_page, (vm_offset_t) dst_po, (vm_size_t)part_size); if (!dst_page->vmp_dirty) { SET_PAGE_DIRTY(dst_page, TRUE); } } vm_object_unlock(dst_object); /* * Cleanup and return */ if (result_page != VM_PAGE_NULL && src_page != dst_page) { vm_fault_copy_cleanup(result_page, src_top_page); } vm_fault_copy_dst_cleanup(dst_page); amount_left -= part_size; src_offset += part_size; dst_offset += part_size; } while (amount_left > 0); done: *copy_size -= amount_left; if (kr != KERN_SUCCESS) { *dst_entry_p = VM_MAP_ENTRY_NULL; } vmlp_api_end(VM_FAULT_COPY, kr); return kr; } #if VM_FAULT_CLASSIFY /* * Temporary statistics gathering support. */ /* * Statistics arrays: */ #define VM_FAULT_TYPES_MAX 5 #define VM_FAULT_LEVEL_MAX 8 int vm_fault_stats[VM_FAULT_TYPES_MAX][VM_FAULT_LEVEL_MAX]; #define VM_FAULT_TYPE_ZERO_FILL 0 #define VM_FAULT_TYPE_MAP_IN 1 #define VM_FAULT_TYPE_PAGER 2 #define VM_FAULT_TYPE_COPY 3 #define VM_FAULT_TYPE_OTHER 4 void vm_fault_classify(vm_object_t object, vm_object_offset_t offset, vm_prot_t fault_type) { int type, level = 0; vm_page_t m; while (TRUE) { m = vm_page_lookup(object, offset); if (m != VM_PAGE_NULL) { if (m->vmp_busy || m->vmp_error || m->vmp_restart || m->vmp_absent) { type = VM_FAULT_TYPE_OTHER; break; } if (((fault_type & VM_PROT_WRITE) == 0) || ((level == 0) && object->vo_copy == VM_OBJECT_NULL)) { type = VM_FAULT_TYPE_MAP_IN; break; } type = VM_FAULT_TYPE_COPY; break; } else { if (object->pager_created) { type = VM_FAULT_TYPE_PAGER; break; } if (object->shadow == VM_OBJECT_NULL) { type = VM_FAULT_TYPE_ZERO_FILL; break; } offset += object->vo_shadow_offset; object = object->shadow; level++; continue; } } if (level > VM_FAULT_LEVEL_MAX) { level = VM_FAULT_LEVEL_MAX; } vm_fault_stats[type][level] += 1; return; } /* cleanup routine to call from debugger */ void vm_fault_classify_init(void) { int type, level; for (type = 0; type < VM_FAULT_TYPES_MAX; type++) { for (level = 0; level < VM_FAULT_LEVEL_MAX; level++) { vm_fault_stats[type][level] = 0; } } return; } #endif /* VM_FAULT_CLASSIFY */ static inline bool object_supports_coredump(const vm_object_t object) { switch (object->wimg_bits & VM_WIMG_MASK) { case VM_WIMG_DEFAULT: return true; #if HAS_MTE case VM_WIMG_MTE: return true; #endif /* HAS_MTE */ default: return false; } } vm_offset_t kdp_lightweight_fault(vm_map_t map, vm_offset_t cur_target_addr, bool multi_cpu) { vm_map_entry_t entry; vm_object_t object; vm_offset_t object_offset; vm_page_t m; int compressor_external_state, compressed_count_delta; vm_compressor_options_t compressor_flags = (C_DONT_BLOCK | C_KEEP | C_KDP); int my_fault_type = VM_PROT_READ; kern_return_t kr; int effective_page_mask, effective_page_size; int my_cpu_no = cpu_number(); ppnum_t decomp_ppnum; addr64_t decomp_paddr; vmlp_api_start(KDP_LIGHTWEIGHT_FAULT); if (multi_cpu) { compressor_flags |= C_KDP_MULTICPU; } if (VM_MAP_PAGE_SHIFT(map) < PAGE_SHIFT) { effective_page_mask = VM_MAP_PAGE_MASK(map); effective_page_size = VM_MAP_PAGE_SIZE(map); } else { effective_page_mask = PAGE_MASK; effective_page_size = PAGE_SIZE; } if (not_in_kdp) { panic("kdp_lightweight_fault called from outside of debugger context"); } assert(map != VM_MAP_NULL); assert((cur_target_addr & effective_page_mask) == 0); if ((cur_target_addr & effective_page_mask) != 0) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } if (kdp_vm_map_is_acquired_exclusive(map)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } entry = vm_map_store_lookup_entry_kdp(map, cur_target_addr); if (entry == VM_MAP_ENTRY_NULL) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } vmlp_range_event_entry(map, entry); if (kdp_vm_entry_lock_is_acquired_exclusive(entry)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } if (entry->is_sub_map) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } object = VME_OBJECT(entry); if (object == VM_OBJECT_NULL) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } object_offset = cur_target_addr - entry->vme_start + VME_OFFSET(entry); while (TRUE) { if (kdp_lck_rw_lock_is_acquired_exclusive(&object->Lock)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } if (object->pager_created && (object->paging_in_progress || object->activity_in_progress)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } m = kdp_vm_page_lookup(object, vm_object_trunc_page(object_offset)); if (m != VM_PAGE_NULL) { if (!object_supports_coredump(object)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } if (m->vmp_laundry || m->vmp_busy || m->vmp_free_when_done || m->vmp_absent || VMP_ERROR_GET(m) || m->vmp_cleaning || m->vmp_overwriting || m->vmp_restart || m->vmp_unusual) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } assert(!vm_page_is_private(m)); if (vm_page_is_private(m)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } assert(!vm_page_is_fictitious(m)); if (vm_page_is_fictitious(m)) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } assert(m->vmp_q_state != VM_PAGE_USED_BY_COMPRESSOR); if (m->vmp_q_state == VM_PAGE_USED_BY_COMPRESSOR) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, 0); return ptoa(VM_PAGE_GET_PHYS_PAGE(m)); } compressor_external_state = VM_EXTERNAL_STATE_UNKNOWN; if (multi_cpu) { assert(vm_compressor_kdp_state.kc_decompressed_pages_ppnum != NULL); assert(vm_compressor_kdp_state.kc_decompressed_pages_paddr != NULL); decomp_ppnum = vm_compressor_kdp_state.kc_decompressed_pages_ppnum[my_cpu_no]; decomp_paddr = vm_compressor_kdp_state.kc_decompressed_pages_paddr[my_cpu_no]; } else { decomp_ppnum = vm_compressor_kdp_state.kc_panic_decompressed_page_ppnum; decomp_paddr = vm_compressor_kdp_state.kc_panic_decompressed_page_paddr; } if (object->pager_created && MUST_ASK_PAGER(object, object_offset, compressor_external_state)) { if (compressor_external_state == VM_EXTERNAL_STATE_EXISTS) { #if HAS_MTE if (vm_object_is_mte_mappable(object)) { compressor_flags |= C_MTE | C_MTE_DROP_TAGS; } #endif /* HAS_MTE */ kr = vm_compressor_pager_get(object->pager, vm_object_trunc_page(object_offset + object->paging_offset), decomp_ppnum, &my_fault_type, compressor_flags, &compressed_count_delta); if (kr == KERN_SUCCESS) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, 0); return decomp_paddr; } else { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } } } if (object->shadow == VM_OBJECT_NULL) { vmlp_api_end(KDP_LIGHTWEIGHT_FAULT, -1); return 0; } object_offset += object->vo_shadow_offset; object = object->shadow; } } /* * vm_page_validate_cs_fast(): * Performs a few quick checks to determine if the page's code signature * really needs to be fully validated. It could: * 1. have been modified (i.e. automatically tainted), * 2. have already been validated, * 3. have already been found to be tainted, * 4. no longer have a backing store. * Returns FALSE if the page needs to be fully validated. */ static boolean_t vm_page_validate_cs_fast( vm_page_t page, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset) { vm_object_t object; object = VM_PAGE_OBJECT(page); vm_object_lock_assert_held(object); if (page->vmp_wpmapped && !VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) { /* * This page was mapped for "write" access sometime in the * past and could still be modifiable in the future. * Consider it tainted. * [ If the page was already found to be "tainted", no * need to re-validate. ] */ vm_object_lock_assert_exclusive(object); VMP_CS_SET_VALIDATED(page, fault_page_size, fault_phys_offset, TRUE); VMP_CS_SET_TAINTED(page, fault_page_size, fault_phys_offset, TRUE); if (cs_debug) { printf("CODESIGNING: %s: " "page %p obj %p off 0x%llx " "was modified\n", __FUNCTION__, page, object, page->vmp_offset); } vm_cs_validated_dirtied++; } if (VMP_CS_VALIDATED(page, fault_page_size, fault_phys_offset) || VMP_CS_TAINTED(page, fault_page_size, fault_phys_offset)) { return TRUE; } vm_object_lock_assert_exclusive(object); #if CHECK_CS_VALIDATION_BITMAP kern_return_t kr; kr = vnode_pager_cs_check_validation_bitmap( object->pager, page->vmp_offset + object->paging_offset, CS_BITMAP_CHECK); if (kr == KERN_SUCCESS) { page->vmp_cs_validated = VMP_CS_ALL_TRUE; page->vmp_cs_tainted = VMP_CS_ALL_FALSE; vm_cs_bitmap_validated++; return TRUE; } #endif /* CHECK_CS_VALIDATION_BITMAP */ if (!object->alive || object->terminating || object->pager == NULL) { /* * The object is terminating and we don't have its pager * so we can't validate the data... */ return TRUE; } /* we need to really validate this page */ vm_object_lock_assert_exclusive(object); return FALSE; } void vm_page_validate_cs_mapped_slow( vm_page_t page, const void *kaddr) { vm_object_t object; memory_object_offset_t mo_offset; memory_object_t pager; struct vnode *vnode; int validated, tainted, nx; assert(page->vmp_busy); object = VM_PAGE_OBJECT(page); vm_object_lock_assert_exclusive(object); vm_cs_validates++; /* * Since we get here to validate a page that was brought in by * the pager, we know that this pager is all setup and ready * by now. */ assert(object->code_signed); assert(!object->internal); assert(object->pager != NULL); assert(object->pager_ready); pager = object->pager; assert(object->paging_in_progress); vnode = vnode_pager_lookup_vnode(pager); mo_offset = page->vmp_offset + object->paging_offset; /* verify the SHA1 hash for this page */ validated = 0; tainted = 0; nx = 0; cs_validate_page(vnode, pager, mo_offset, (const void *)((const char *)kaddr), &validated, &tainted, &nx); page->vmp_cs_validated |= validated; page->vmp_cs_tainted |= tainted; page->vmp_cs_nx |= nx; #if CHECK_CS_VALIDATION_BITMAP if (page->vmp_cs_validated == VMP_CS_ALL_TRUE && page->vmp_cs_tainted == VMP_CS_ALL_FALSE) { vnode_pager_cs_check_validation_bitmap(object->pager, mo_offset, CS_BITMAP_SET); } #endif /* CHECK_CS_VALIDATION_BITMAP */ } void vm_page_validate_cs_mapped( vm_page_t page, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset, const void *kaddr) { if (!vm_page_validate_cs_fast(page, fault_page_size, fault_phys_offset)) { vm_page_validate_cs_mapped_slow(page, kaddr); } } static void vm_page_map_and_validate_cs( vm_object_t object, vm_page_t page) { vm_object_offset_t offset; vm_map_offset_t koffset; vm_map_size_t ksize; vm_offset_t kaddr; kern_return_t kr; boolean_t busy_page; boolean_t need_unmap; vm_object_lock_assert_exclusive(object); assert(object->code_signed); offset = page->vmp_offset; busy_page = page->vmp_busy; if (!busy_page) { /* keep page busy while we map (and unlock) the VM object */ page->vmp_busy = TRUE; } /* * Take a paging reference on the VM object * to protect it from collapse or bypass, * and keep it from disappearing too. */ vm_object_paging_begin(object); /* map the page in the kernel address space */ ksize = PAGE_SIZE_64; koffset = 0; need_unmap = FALSE; kr = vm_paging_map_object(page, object, offset, VM_PROT_READ, FALSE, /* can't unlock object ! */ &ksize, &koffset, &need_unmap); if (kr != KERN_SUCCESS) { panic("%s: could not map page: 0x%x", __FUNCTION__, kr); } kaddr = CAST_DOWN(vm_offset_t, koffset); /* validate the mapped page */ vm_page_validate_cs_mapped_slow(page, (const void *) kaddr); assert(page->vmp_busy); assert(object == VM_PAGE_OBJECT(page)); vm_object_lock_assert_exclusive(object); if (!busy_page) { vm_page_wakeup_done(object, page); } if (need_unmap) { /* unmap the map from the kernel address space */ vm_paging_unmap_object(object, koffset, koffset + ksize); koffset = 0; ksize = 0; kaddr = 0; } vm_object_paging_end(object); } void vm_page_validate_cs( vm_page_t page, vm_map_size_t fault_page_size, vm_map_offset_t fault_phys_offset) { vm_object_t object; object = VM_PAGE_OBJECT(page); vm_object_lock_assert_held(object); if (vm_page_validate_cs_fast(page, fault_page_size, fault_phys_offset)) { return; } vm_page_map_and_validate_cs(object, page); } void vm_page_validate_cs_mapped_chunk( vm_page_t page, const void *kaddr, vm_offset_t chunk_offset, vm_size_t chunk_size, boolean_t *validated_p, unsigned *tainted_p) { vm_object_t object; vm_object_offset_t offset, offset_in_page; memory_object_t pager; struct vnode *vnode; boolean_t validated; unsigned tainted; *validated_p = FALSE; *tainted_p = 0; assert(page->vmp_busy); object = VM_PAGE_OBJECT(page); vm_object_lock_assert_exclusive(object); assert(object->code_signed); offset = page->vmp_offset; if (!object->alive || object->terminating || object->pager == NULL) { /* * The object is terminating and we don't have its pager * so we can't validate the data... */ return; } /* * Since we get here to validate a page that was brought in by * the pager, we know that this pager is all setup and ready * by now. */ assert(!object->internal); assert(object->pager != NULL); assert(object->pager_ready); pager = object->pager; assert(object->paging_in_progress); vnode = vnode_pager_lookup_vnode(pager); /* verify the signature for this chunk */ offset_in_page = chunk_offset; assert(offset_in_page < PAGE_SIZE); tainted = 0; validated = cs_validate_range(vnode, pager, (object->paging_offset + offset + offset_in_page), (const void *)((const char *)kaddr + offset_in_page), chunk_size, &tainted); if (validated) { *validated_p = TRUE; } if (tainted) { *tainted_p = tainted; } } static void vm_rtfrecord_lock(void) { lck_spin_lock(&vm_rtfr_slock); } static void vm_rtfrecord_unlock(void) { lck_spin_unlock(&vm_rtfr_slock); } unsigned int vmrtfaultinfo_bufsz(void) { return vmrtf_num_records * sizeof(vm_rtfault_record_t); } __attribute__((noinline)) static void vm_record_rtfault(thread_t cthread, uint64_t fstart, vm_map_offset_t fault_vaddr, int type_of_fault) { uint64_t fend = mach_continuous_time(); uint64_t cfpc = 0; uint64_t ctid = cthread->thread_id; uint64_t cupid = get_current_unique_pid(); uintptr_t bpc = 0; errno_t btr = 0; /* * Capture a single-frame backtrace. This extracts just the program * counter at the point of the fault, and should not use copyin to get * Rosetta save state. */ struct backtrace_control ctl = { .btc_user_thread = cthread, .btc_user_copy = backtrace_user_copy_error, }; unsigned int bfrs = backtrace_user(&bpc, 1U, &ctl, NULL); if ((btr == 0) && (bfrs > 0)) { cfpc = bpc; } assert((fstart != 0) && fend >= fstart); vm_rtfrecord_lock(); assert(vmrtfrs.vmrtfr_curi <= vmrtfrs.vmrtfr_maxi); vmrtfrs.vmrtf_total++; vm_rtfault_record_t *cvmr = &vmrtfrs.vm_rtf_records[vmrtfrs.vmrtfr_curi++]; cvmr->rtfabstime = fstart; cvmr->rtfduration = fend - fstart; cvmr->rtfaddr = fault_vaddr; cvmr->rtfpc = cfpc; cvmr->rtftype = type_of_fault; cvmr->rtfupid = cupid; cvmr->rtftid = ctid; if (vmrtfrs.vmrtfr_curi > vmrtfrs.vmrtfr_maxi) { vmrtfrs.vmrtfr_curi = 0; } vm_rtfrecord_unlock(); } int vmrtf_extract(uint64_t cupid, __unused boolean_t isroot, unsigned long vrecordsz, void *vrecords, unsigned long *vmrtfrv) { vm_rtfault_record_t *cvmrd = vrecords; size_t residue = vrecordsz; size_t numextracted = 0; boolean_t early_exit = FALSE; vm_rtfrecord_lock(); for (int vmfi = 0; vmfi <= vmrtfrs.vmrtfr_maxi; vmfi++) { if (residue < sizeof(vm_rtfault_record_t)) { early_exit = TRUE; break; } if (vmrtfrs.vm_rtf_records[vmfi].rtfupid != cupid) { #if DEVELOPMENT || DEBUG if (isroot == FALSE) { continue; } #else continue; #endif /* DEVDEBUG */ } *cvmrd = vmrtfrs.vm_rtf_records[vmfi]; cvmrd++; residue -= sizeof(vm_rtfault_record_t); numextracted++; } vm_rtfrecord_unlock(); *vmrtfrv = numextracted; return early_exit; } /* * Only allow one diagnosis to be in flight at a time, to avoid * creating too much additional memory usage. */ static volatile uint_t vmtc_diagnosing; unsigned int vmtc_total = 0; /* * Type used to update telemetry for the diagnosis counts. */ CA_EVENT(vmtc_telemetry, CA_INT, vmtc_num_byte, /* number of corrupt bytes found */ CA_BOOL, vmtc_undiagnosed, /* undiagnosed because more than 1 at a time */ CA_BOOL, vmtc_not_eligible, /* the page didn't qualify */ CA_BOOL, vmtc_copyin_fail, /* unable to copy in the page */ CA_BOOL, vmtc_not_found, /* no corruption found even though CS failed */ CA_BOOL, vmtc_one_bit_flip, /* single bit flip */ CA_BOOL, vmtc_testing); /* caused on purpose by testing */ #if DEVELOPMENT || DEBUG /* * Buffers used to compare before/after page contents. * Stashed to aid when debugging crashes. */ static size_t vmtc_last_buffer_size = 0; static uint64_t *vmtc_last_before_buffer = NULL; static uint64_t *vmtc_last_after_buffer = NULL; /* * Needed to record corruptions due to testing. */ static uintptr_t corruption_test_va = 0; #endif /* DEVELOPMENT || DEBUG */ /* * Stash a copy of data from a possibly corrupt page. */ static uint64_t * vmtc_get_page_data( vm_map_offset_t code_addr, vm_page_t page) { uint64_t *buffer = NULL; addr64_t buffer_paddr; addr64_t page_paddr; extern void bcopy_phys(addr64_t from, addr64_t to, vm_size_t bytes); uint_t size = MIN(vm_map_page_size(current_map()), PAGE_SIZE); /* * Need an aligned buffer to do a physical copy. */ if (kernel_memory_allocate(kernel_map, (vm_offset_t *)&buffer, size, size - 1, KMA_KOBJECT, VM_KERN_MEMORY_DIAG) != KERN_SUCCESS) { return NULL; } buffer_paddr = kvtophys((vm_offset_t)buffer); page_paddr = ptoa(VM_PAGE_GET_PHYS_PAGE(page)); /* adjust the page start address if we need only 4K of a 16K page */ if (size < PAGE_SIZE) { uint_t subpage_start = ((code_addr & (PAGE_SIZE - 1)) & ~(size - 1)); page_paddr += subpage_start; } bcopy_phys(page_paddr, buffer_paddr, size); return buffer; } /* * Set things up so we can diagnose a potential text page corruption. */ static uint64_t * vmtc_text_page_diagnose_setup( vm_map_offset_t code_addr, vm_page_t page, CA_EVENT_TYPE(vmtc_telemetry) *event) { uint64_t *buffer = NULL; /* * If another is being diagnosed, skip this one. */ if (!OSCompareAndSwap(0, 1, &vmtc_diagnosing)) { event->vmtc_undiagnosed = true; return NULL; } /* * Get the contents of the corrupt page. */ buffer = vmtc_get_page_data(code_addr, page); if (buffer == NULL) { event->vmtc_copyin_fail = true; if (!OSCompareAndSwap(1, 0, &vmtc_diagnosing)) { panic("Bad compare and swap in setup!"); } return NULL; } return buffer; } /* * Diagnose the text page by comparing its contents with * the one we've previously saved. */ static void vmtc_text_page_diagnose( vm_map_offset_t code_addr, uint64_t *old_code_buffer, CA_EVENT_TYPE(vmtc_telemetry) *event) { uint64_t *new_code_buffer; size_t size = MIN(vm_map_page_size(current_map()), PAGE_SIZE); uint_t count = (uint_t)size / sizeof(uint64_t); uint_t diff_count = 0; bool bit_flip = false; uint_t b; uint64_t *new; uint64_t *old; new_code_buffer = kalloc_data(size, Z_WAITOK); assert(new_code_buffer != NULL); if (copyin((user_addr_t)vm_map_trunc_page(code_addr, size - 1), new_code_buffer, size) != 0) { /* copyin error, so undo things */ event->vmtc_copyin_fail = true; goto done; } new = new_code_buffer; old = old_code_buffer; for (; count-- > 0; ++new, ++old) { if (*new == *old) { continue; } /* * On first diff, check for a single bit flip */ if (diff_count == 0) { uint64_t x = (*new ^ *old); assert(x != 0); if ((x & (x - 1)) == 0) { bit_flip = true; ++diff_count; continue; } } /* * count up the number of different bytes. */ for (b = 0; b < sizeof(uint64_t); ++b) { char *n = (char *)new; char *o = (char *)old; if (n[b] != o[b]) { ++diff_count; } } } if (diff_count > 1) { bit_flip = false; } if (diff_count == 0) { event->vmtc_not_found = true; } else { event->vmtc_num_byte = diff_count; } if (bit_flip) { event->vmtc_one_bit_flip = true; } done: /* * Free up the code copy buffers, but save the last * set on development / debug kernels in case they * can provide evidence for debugging memory stomps. */ #if DEVELOPMENT || DEBUG if (vmtc_last_before_buffer != NULL) { kmem_free(kernel_map, (vm_offset_t)vmtc_last_before_buffer, vmtc_last_buffer_size); } if (vmtc_last_after_buffer != NULL) { kfree_data(vmtc_last_after_buffer, vmtc_last_buffer_size); } vmtc_last_before_buffer = old_code_buffer; vmtc_last_after_buffer = new_code_buffer; vmtc_last_buffer_size = size; #else /* DEVELOPMENT || DEBUG */ kfree_data(new_code_buffer, size); kmem_free(kernel_map, (vm_offset_t)old_code_buffer, size); #endif /* DEVELOPMENT || DEBUG */ /* * We're finished, so clear the diagnosing flag. */ if (!OSCompareAndSwap(1, 0, &vmtc_diagnosing)) { panic("Bad compare and swap in diagnose!"); } } /* * For the given map, virt address, find the object, offset, and page. * This has to lookup the map entry, verify protections, walk any shadow chains. * If found, returns with the object locked. */ static kern_return_t vmtc_revalidate_lookup( vm_map_t map, vm_map_lock_ctx_t ctx, vm_map_offset_t vaddr, vm_object_t *ret_object, vm_object_offset_t *ret_offset, vm_page_t *ret_page, vm_prot_t *ret_prot) { vm_object_t object; vm_object_offset_t offset; vm_page_t page; kern_return_t kr = KERN_SUCCESS; boolean_t wired; struct vm_object_fault_info fault_info = { .interruptible = THREAD_UNINT }; vm_map_t var_map = NULL; vm_map_t real_map = NULL; vm_prot_t prot; vm_object_t shadow; vm_map_entry_t entry; vmlp_api_start(VMTC_REVALIDATE_LOOKUP); /* * Find the object/offset for the given location/map. * Note this returns with the object locked. */ restart: /* in case we come around the restart path */ vm_map_lock_ctx_init(ctx); object = VM_OBJECT_NULL; var_map = map; kr = vm_map_lookup_object_and_lock_entry(&var_map, vaddr, VM_PROT_READ, &object, &entry, &offset, &prot, &wired, &fault_info, &real_map, ctx, NULL, false); /* * If there's no page here, fail. */ if (kr != KERN_SUCCESS) { kr = KERN_FAILURE; goto done; } vm_object_lock(object); vm_map_range_sh_unlock(ctx, NULL); /* * Chase down any shadow chains to find the actual page. */ for (;;) { /* * See if the page is on the current object. */ page = vm_page_lookup(object, vm_object_trunc_page(offset)); if (page != NULL) { /* restart the lookup */ if (page->vmp_restart) { vm_object_unlock(object); goto restart; } /* * If this page is busy, we need to wait for it. */ if (page->vmp_busy) { vm_page_sleep(object, page, THREAD_INTERRUPTIBLE, LCK_SLEEP_UNLOCK); goto restart; } break; } /* * If the object doesn't have the page and * has no shadow, then we can quit. */ shadow = object->shadow; if (shadow == NULL) { kr = KERN_FAILURE; goto done; } /* * Move to the next object */ offset += object->vo_shadow_offset; vm_object_lock(shadow); vm_object_unlock(object); object = shadow; shadow = VM_OBJECT_NULL; } *ret_object = object; *ret_offset = vm_object_trunc_page(offset); *ret_page = page; *ret_prot = prot; done: if (kr != KERN_SUCCESS && object != NULL) { vm_object_unlock(object); } vmlp_api_end(VMTC_REVALIDATE_LOOKUP, kr); return kr; } /* * Check if a page is wired, needs extra locking. */ static bool is_page_wired(vm_page_t page) { bool result; vm_page_lock_queues(); result = VM_PAGE_WIRED(page); vm_page_unlock_queues(); return result; } /* * A fatal process error has occurred in the given task. * Recheck the code signing of the text page at the given * address to check for a text page corruption. * * Returns KERN_FAILURE if a page was found to be corrupt * by failing to match its code signature. KERN_SUCCESS * means the page is either valid or we don't have the * information to say it's corrupt. */ kern_return_t revalidate_text_page(task_t task, vm_map_offset_t code_addr) { kern_return_t kr; vm_map_t map; vm_object_t object = NULL; vm_object_offset_t offset; vm_page_t page = NULL; struct vnode *vnode; uint64_t *diagnose_buffer = NULL; CA_EVENT_TYPE(vmtc_telemetry) * event = NULL; ca_event_t ca_event = NULL; vm_prot_t prot; VM_MAP_LOCK_CTX_DECLARE(ctx); map = task->map; if (task->map == NULL) { return KERN_SUCCESS; } kr = vmtc_revalidate_lookup(map, ctx, code_addr, &object, &offset, &page, &prot); if (kr != KERN_SUCCESS) { goto err; } /* * The page must be executable. */ if (!(prot & VM_PROT_EXECUTE)) { goto done; } /* * The object needs to have a pager. */ if (object->pager == NULL) { goto done; } /* * Needs to be a vnode backed page to have a signature. */ vnode = vnode_pager_lookup_vnode(object->pager); if (vnode == NULL) { goto done; } /* * Object checks to see if we should proceed. */ if (!object->code_signed || /* no code signature to check */ object->internal || /* internal objects aren't signed */ object->terminating || /* the object and its pages are already going away */ !object->pager_ready) { /* this should happen, but check shouldn't hurt */ goto done; } /* * Check the code signature of the page in question. */ vm_page_map_and_validate_cs(object, page); /* * At this point: * vmp_cs_validated |= validated (set if a code signature exists) * vmp_cs_tainted |= tainted (set if code signature violation) * vmp_cs_nx |= nx; ?? * * if vmp_pmapped then have to pmap_disconnect.. * other flags to check on object or page? */ if (page->vmp_cs_tainted != VMP_CS_ALL_FALSE) { #if DEBUG || DEVELOPMENT /* * On development builds, a boot-arg can be used to cause * a panic, instead of a quiet repair. */ if (vmtc_panic_instead) { panic("Text page corruption detected: vm_page_t 0x%llx", (long long)(uintptr_t)page); } #endif /* DEBUG || DEVELOPMENT */ /* * We're going to invalidate this page. Grab a copy of it for comparison. */ ca_event = CA_EVENT_ALLOCATE(vmtc_telemetry); event = ca_event->data; diagnose_buffer = vmtc_text_page_diagnose_setup(code_addr, page, event); /* * Invalidate, i.e. toss, the corrupted page. */ if (!page->vmp_cleaning && !page->vmp_laundry && !vm_page_is_fictitious(page) && !page->vmp_precious && !page->vmp_absent && !VMP_ERROR_GET(page) && !page->vmp_dirty && !is_page_wired(page)) { if (page->vmp_pmapped) { int refmod = pmap_disconnect(VM_PAGE_GET_PHYS_PAGE(page)); if (refmod & VM_MEM_MODIFIED) { SET_PAGE_DIRTY(page, FALSE); } if (refmod & VM_MEM_REFERENCED) { page->vmp_reference = TRUE; } } /* If the page seems intentionally modified, don't trash it. */ if (!page->vmp_dirty) { VM_PAGE_FREE(page); } else { event->vmtc_not_eligible = true; } } else { event->vmtc_not_eligible = true; } vm_object_unlock(object); object = VM_OBJECT_NULL; /* * Now try to diagnose the type of failure by faulting * in a new copy and diff'ing it with what we saved. */ if (diagnose_buffer != NULL) { vmtc_text_page_diagnose(code_addr, diagnose_buffer, event); } #if DEBUG || DEVELOPMENT if (corruption_test_va != 0) { corruption_test_va = 0; event->vmtc_testing = true; } #endif /* DEBUG || DEVELOPMENT */ ktriage_record(thread_tid(current_thread()), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_VM, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_VM_TEXT_CORRUPTION), 0 /* arg */); CA_EVENT_SEND(ca_event); printf("Text page corruption detected for pid %d\n", proc_selfpid()); ++vmtc_total; return KERN_FAILURE; /* failure means we definitely found a corrupt page */ } done: if (object != NULL) { vm_object_unlock(object); } err: return KERN_SUCCESS; } #if DEBUG || DEVELOPMENT /* * For implementing unit tests - ask the pmap to corrupt a text page. * We have to find the page, to get the physical address, then invoke * the pmap. */ extern kern_return_t vm_corrupt_text_addr(uintptr_t); kern_return_t vm_corrupt_text_addr(uintptr_t va) { task_t task = current_task(); vm_map_t map; kern_return_t kr = KERN_SUCCESS; vm_object_t object = VM_OBJECT_NULL; vm_object_offset_t offset; vm_page_t page = NULL; pmap_paddr_t pa; vm_prot_t prot; VM_MAP_LOCK_CTX_DECLARE(ctx); map = task->map; if (task->map == NULL) { printf("corrupt_text_addr: no map\n"); return KERN_FAILURE; } kr = vmtc_revalidate_lookup(map, ctx, (vm_map_offset_t)va, &object, &offset, &page, &prot); if (kr != KERN_SUCCESS) { printf("corrupt_text_addr: page lookup failed\n"); return kr; } if (!(prot & VM_PROT_EXECUTE)) { if (object != VM_OBJECT_NULL) { vm_object_unlock(object); } printf("corrupt_text_addr: page not executable\n"); return KERN_FAILURE; } /* get the physical address to use */ pa = ptoa(VM_PAGE_GET_PHYS_PAGE(page)) + (va - vm_object_trunc_page(va)); /* * Check we have something we can work with. * Due to racing with pageout as we enter the sysctl, * it's theoretically possible to have the page disappear, just * before the lookup. * * That's highly likely to happen often. I've filed a radar 72857482 * to bubble up the error here to the sysctl result and have the * test not FAIL in that case. */ if (page->vmp_busy) { printf("corrupt_text_addr: vmp_busy\n"); kr = KERN_FAILURE; } if (page->vmp_cleaning) { printf("corrupt_text_addr: vmp_cleaning\n"); kr = KERN_FAILURE; } if (page->vmp_laundry) { printf("corrupt_text_addr: vmp_cleaning\n"); kr = KERN_FAILURE; } if (vm_page_is_fictitious(page)) { printf("corrupt_text_addr: vmp_fictitious\n"); kr = KERN_FAILURE; } if (page->vmp_precious) { printf("corrupt_text_addr: vmp_precious\n"); kr = KERN_FAILURE; } if (page->vmp_absent) { printf("corrupt_text_addr: vmp_absent\n"); kr = KERN_FAILURE; } if (VMP_ERROR_GET(page)) { printf("corrupt_text_addr: vmp_error\n"); kr = KERN_FAILURE; } if (page->vmp_dirty) { printf("corrupt_text_addr: vmp_dirty\n"); kr = KERN_FAILURE; } if (is_page_wired(page)) { printf("corrupt_text_addr: wired\n"); kr = KERN_FAILURE; } if (!page->vmp_pmapped) { printf("corrupt_text_addr: !vmp_pmapped\n"); kr = KERN_FAILURE; } if (kr == KERN_SUCCESS) { printf("corrupt_text_addr: using physaddr 0x%llx\n", (long long)pa); kr = pmap_test_text_corruption(pa); if (kr != KERN_SUCCESS) { printf("corrupt_text_addr: pmap error %d\n", kr); } else { corruption_test_va = va; } } else { printf("corrupt_text_addr: object %p\n", object); printf("corrupt_text_addr: offset 0x%llx\n", (uint64_t)offset); printf("corrupt_text_addr: va 0x%llx\n", (uint64_t)va); printf("corrupt_text_addr: vm_object_trunc_page(va) 0x%llx\n", (uint64_t)vm_object_trunc_page(va)); printf("corrupt_text_addr: vm_page_t %p\n", page); printf("corrupt_text_addr: ptoa(PHYS_PAGE) 0x%llx\n", (uint64_t)ptoa(VM_PAGE_GET_PHYS_PAGE(page))); printf("corrupt_text_addr: using physaddr 0x%llx\n", (uint64_t)pa); } if (object != VM_OBJECT_NULL) { vm_object_unlock(object); } return kr; } #endif /* DEBUG || DEVELOPMENT */ |