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 | /* * Copyright (c) 2012-2020 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@ */ #include <mach/host_priv.h> #include <mach/host_special_ports.h> #include <mach/mach_types.h> #include <mach/telemetry_notification_server.h> #include <kern/assert.h> #include <kern/clock.h> #include <kern/coalition.h> #include <kern/counter.h> #include <kern/debug.h> #include <kern/host.h> #include <kern/kalloc.h> #include <kern/kern_types.h> #include <kern/locks.h> #include <kern/misc_protos.h> #include <kern/sched.h> #include <kern/sched_prim.h> #include <kern/thread.h> #include <kern/telemetry.h> #include <kern/timer_call.h> #include <kern/policy_internal.h> #include <kern/kcdata.h> #include <kern/percpu.h> #include <kern/mpsc_ring.h> #include <kern/kern_stackshot.h> #include <vm/vm_shared_region_xnu.h> #include <pexpert/pexpert.h> #include <vm/vm_kern_xnu.h> #include <vm/vm_shared_region.h> #include <vm/vm_protos_internal.h> #include <kperf/callstack.h> #include <kern/backtrace.h> #include <kern/monotonic.h> #include <sys/codesign.h> #include <security/mac_mach_internal.h> #include <sys/errno.h> #include <sys/kdebug.h> #include <uuid/uuid.h> #include <kdp/kdp_dyld.h> #include <os/base.h> #include <string.h> #include <libkern/OSAtomic.h> #include <libkern/coreanalytics/coreanalytics.h> #include <kern/thread_call.h> struct proc; extern int proc_pid(struct proc *); extern char *proc_name_address(void *p); extern char *proc_longname_address(void *p); extern uint64_t proc_uniqueid(void *p); extern uint64_t proc_was_throttled(void *p); extern uint64_t proc_did_throttle(void *p); extern boolean_t task_did_exec(task_t task); extern boolean_t task_is_exec_copy(task_t task); extern void proc_getexecutableuuid(proc_t p, unsigned char *uuidbuf, unsigned long size); extern task_t proc_get_task_raw(proc_t proc); #if CONFIG_CPU_COUNTERS #define HAS_PMI_MICROSTACKSHOTS 1 #endif /* CONFIG_CPU_COUNTERS */ struct micro_snapshot_buffer { vm_offset_t buffer; uint32_t size; uint32_t current_position; uint32_t end_point; }; static const size_t _telemetry_sample_size_static = sizeof(struct micro_snapshot) + sizeof(struct task_snapshot) + sizeof(struct thread_snapshot); static void telemetry_instrumentation_begin( struct micro_snapshot_buffer *buffer, enum micro_snapshot_flags flags); static void telemetry_instrumentation_end(struct micro_snapshot_buffer *buffer); static void telemetry_take_sample(thread_t thread, enum micro_snapshot_flags flags); #if HAS_PMI_MICROSTACKSHOTS static void _telemetry_take_sample_kernel(thread_t thread, enum micro_snapshot_flags flags); static void _telemetry_set_ast_pmi(bool interrupted_user); #endif /* HAS_PMI_MICROSTACKSHOTS */ #if CONFIG_MACF static void telemetry_macf_take_sample(thread_t thread, enum micro_snapshot_flags flags); #endif struct telemetry_target { thread_t thread; uintptr_t *frames; size_t frames_count; bool user64_regs; uint16_t async_start_index; enum micro_snapshot_flags microsnapshot_flags; bool include_metadata; struct micro_snapshot_buffer *buffer; lck_mtx_t *buffer_mtx; }; static int telemetry_process_sample( const struct telemetry_target *target, bool release_buffer_lock, uint32_t *out_current_record_start); static int telemetry_buffer_gather( user_addr_t buffer, uint32_t *length, bool mark, struct micro_snapshot_buffer *current_buffer); // The default size of the buffer used for microstackshots (16KiB). #define TELEMETRY_DEFAULT_BUFFER_SIZE (16 << 10) // The maximum size of the buffer used for microstackshots (10MiB). #define TELEMETRY_MAX_BUFFER_SIZE (10 << 20) // User space gets some leeway to collect data after notification (4KiB). #define TELEMETRY_DEFAULT_NOTIFY_LEEWAY (4 << 10) // Maximum non-shared cache UUIDs to include for symbolication. #define TELEMETRY_MAX_UUID_COUNT (128) uint64_t microstackshot_pmi_period = 0; cpc_slot_t microstackshot_pmi_counter = 0; bool telemetry_sample_pmis = false; /** * @abstract the trigger of microstackshot samples */ __enum_closed_decl(telemetry_source_t, uint8_t, { TMSRC_NONE = 0, TMSRC_UNKNOWN, TMSRC_TIME, TMSRC_INSTRUCTIONS, TMSRC_CYCLES, TMSRC_VM_FAULTS, TMSRC_PAGE_GRABS, }); struct telemetry_metadata { /* * The current generation of microstackshot-based telemetry. * Incremented whenever the settings change. */ uint32_t tm_generation; /* * The total number of samples recorded. */ uint64_t tm_samples_recorded; /* * The total number of samples that were skipped. */ uint64_t tm_samples_skipped; /* * What's triggering the microstackshot samples. */ telemetry_source_t tm_source; /* * The interval used for periodic sampling. */ uint64_t tm_period; }; /* * The telemetry_buffer is responsible * for timer samples and interrupt samples that are driven by * compute_averages(). It will notify its client (if one * exists) when it has enough data to be worth flushing. */ struct micro_snapshot_buffer telemetry_buffer = { .buffer = 0, .size = 0, .current_position = 0, .end_point = 0 }; #if CONFIG_MACF #define TELEMETRY_MACF_DEFAULT_BUFFER_SIZE (16*1024) /* * The MAC framework uses its own telemetry buffer for the purposes of auditing * security-related work being done by userland threads. */ struct micro_snapshot_buffer telemetry_macf_buffer = { .buffer = 0, .size = 0, .current_position = 0, .end_point = 0 }; #endif /* CONFIG_MACF */ int telemetry_bytes_since_last_mark = -1; // How much data since buf was last marked? int telemetry_buffer_notify_at = 0; LCK_GRP_DECLARE(telemetry_lck_grp, "telemetry group"); LCK_MTX_DECLARE(telemetry_mtx, &telemetry_lck_grp); LCK_MTX_DECLARE(telemetry_pmi_mtx, &telemetry_lck_grp); LCK_MTX_DECLARE(telemetry_macf_mtx, &telemetry_lck_grp); LCK_SPIN_DECLARE(telemetry_metadata_lck, &telemetry_lck_grp); #define TELEMETRY_LOCK() do { lck_mtx_lock(&telemetry_mtx); } while (0) #define TELEMETRY_TRY_SPIN_LOCK() lck_mtx_try_lock_spin(&telemetry_mtx) #define TELEMETRY_UNLOCK() do { lck_mtx_unlock(&telemetry_mtx); } while (0) #define TELEMETRY_PMI_LOCK() do { lck_mtx_lock(&telemetry_pmi_mtx); } while (0) #define TELEMETRY_PMI_UNLOCK() do { lck_mtx_unlock(&telemetry_pmi_mtx); } while (0) #define TELEMETRY_MACF_LOCK() do { lck_mtx_lock(&telemetry_macf_mtx); } while (0) #define TELEMETRY_MACF_UNLOCK() do { lck_mtx_unlock(&telemetry_macf_mtx); } while (0) /* * Protected by the telemetry_metadata_lck spinlock. */ struct telemetry_metadata telemetry_metadata = { 0 }; #if HAS_PMI_MICROSTACKSHOTS #define TELEMETRY_MIN_PMI_PERIOD (10 * 1000 * 1000) static __security_const_late thread_call_t _telemetry_kernel_notify_thread; _Atomic bool _telemetry_kernel_notified = false; static struct mpsc_ring _telemetry_kernel_ring; static void _telemetry_kernel_notify(void *, void *); #endif /* HAS_PMI_MICROSTACKSHOTS */ TUNABLE(uint32_t, telemetry_buffer_size, "telemetry_buffer_size", TELEMETRY_DEFAULT_BUFFER_SIZE); TUNABLE(uint8_t, telemetry_kernel_buffer_size_pow_2, "telemetry_kernel_buffer_size_pow_2", 16); TUNABLE(uint32_t, telemetry_notification_leeway, "telemetry_notification_leeway", TELEMETRY_DEFAULT_NOTIFY_LEEWAY); __startup_func static void _telemetry_init(void) { telemetry_buffer.size = MIN(telemetry_buffer_size, TELEMETRY_MAX_BUFFER_SIZE); kern_return_t ret = kmem_alloc(kernel_map, &telemetry_buffer.buffer, telemetry_buffer.size, KMA_DATA | KMA_ZERO | KMA_PERMANENT, VM_KERN_MEMORY_DIAG); if (ret != KERN_SUCCESS) { printf("telemetry: allocation failed: %d\n", ret); return; } if (telemetry_notification_leeway >= telemetry_buffer.size) { printf("telemetry: nonsensical telemetry_notification_leeway boot-arg %d changed to %d\n", telemetry_notification_leeway, TELEMETRY_DEFAULT_NOTIFY_LEEWAY); telemetry_notification_leeway = TELEMETRY_DEFAULT_NOTIFY_LEEWAY; } telemetry_buffer_notify_at = telemetry_buffer.size - telemetry_notification_leeway; #if HAS_PMI_MICROSTACKSHOTS #if __arm__ || __arm64__ unsigned int cpu_count = ml_get_cpu_count(); #else // __arm__ || __arm64__ unsigned int cpu_count = ml_early_cpu_max_number() + 1; #endif // !__arm__ && !__arm64__ mpsc_ring_init(&_telemetry_kernel_ring, telemetry_kernel_buffer_size_pow_2, (uint8_t)cpu_count); _telemetry_kernel_notify_thread = thread_call_allocate_with_options( _telemetry_kernel_notify, NULL, THREAD_CALL_PRIORITY_USER, THREAD_CALL_OPTIONS_ONCE); if (!_telemetry_kernel_notify_thread) { panic("telemetry_init: failed to allocate kernel notification thread call"); } #endif /* !HAS_PMI_MICROSTACKSHOTS */ } STARTUP(MACH_IPC, STARTUP_RANK_FIRST, _telemetry_init); /* * If userland has registered a port for telemetry notifications, send one now. */ static void _telemetry_notify_user(telemetry_notice_t flags) { mach_port_t user_port = MACH_PORT_NULL; kern_return_t kr = host_get_telemetry_port(host_priv_self(), &user_port); if ((kr != KERN_SUCCESS) || !IPC_PORT_VALID(user_port)) { return; } telemetry_notification(user_port, flags); ipc_port_release_send(user_port); } #if CONFIG_CPU_COUNTERS cpc_cyclic_t microstackshot_cpc_cyclic = NULL; static void _telemetry_cyclic_handler( struct cpc_cyclic_info * __unused info, uint64_t __unused count, uint64_t __unused extra_count, uintptr_t __unused pc, cpc_call_source_t source, cpc_call_flags_t __unused flags) { thread_t thread = current_thread(); if (get_threadtask(thread) == kernel_task) { _telemetry_take_sample_kernel(thread, kPMIRecord); } else { _telemetry_set_ast_pmi(source == CPC_CS_USER); } } #endif /* HAS_PMI_MICROSTACKSHOTS */ int telemetry_pmi_setup(telemetry_pmi_t pmi_ctr, uint64_t period) { #if HAS_PMI_MICROSTACKSHOTS telemetry_source_t source = TMSRC_NONE; int error = 0; const char *name = "?"; if (!cpc_cpmu_supported) { return ENOTSUP; } if (pmi_ctr != TELEMETRY_PMI_NONE && (period < TELEMETRY_MIN_PMI_PERIOD || period > cpc_hw_max_period(CPC_HW_CPMU))) { return EINVAL; } TELEMETRY_PMI_LOCK(); switch (pmi_ctr) { case TELEMETRY_PMI_NONE: if (!telemetry_sample_pmis) { error = 1; goto out; } telemetry_sample_pmis = false; printf("telemetry: disabling ustackshot on PMI\n"); cpc_cyclic_cancel(microstackshot_cpc_cyclic); microstackshot_pmi_counter = 0; microstackshot_pmi_period = 0; cpc_cyclic_destroy(microstackshot_cpc_cyclic); microstackshot_cpc_cyclic = NULL; int intrs_en = ml_set_interrupts_enabled(FALSE); lck_spin_lock(&telemetry_metadata_lck); telemetry_metadata.tm_period = 0; telemetry_metadata.tm_source = TMSRC_NONE; lck_spin_unlock(&telemetry_metadata_lck); ml_set_interrupts_enabled(intrs_en); goto out; case TELEMETRY_PMI_INSTRS: microstackshot_pmi_counter = cpc_fixed_event_slot(CPC_HW_CPMU, CPC_FEVT_INSTRUCTIONS); name = "instructions"; source = TMSRC_INSTRUCTIONS; break; case TELEMETRY_PMI_CYCLES: microstackshot_pmi_counter = cpc_fixed_event_slot(CPC_HW_CPMU, CPC_FEVT_CYCLES); name = "cycles"; source = TMSRC_CYCLES; break; default: error = 1; goto out; } if (microstackshot_cpc_cyclic) { cpc_cyclic_cancel(microstackshot_cpc_cyclic); cpc_cyclic_destroy(microstackshot_cpc_cyclic); microstackshot_cpc_cyclic = NULL; } telemetry_sample_pmis = true; microstackshot_pmi_period = period; struct cpc_cyclic_info info = { .cci_func = _telemetry_cyclic_handler, .cci_slot = microstackshot_pmi_counter, .cci_period = microstackshot_pmi_period, }; microstackshot_cpc_cyclic = cpc_cyclic_alloc(CPC_HW_CPMU, &info); if (!microstackshot_cpc_cyclic) { panic("telemetry: failed to allocate microstackshot PMI cyclic"); } cpc_cyclic_activate(microstackshot_cpc_cyclic); printf("telemetry: microstackshot every %llu %s\n", microstackshot_pmi_period, name); int intrs_en = ml_set_interrupts_enabled(FALSE); lck_spin_lock(&telemetry_metadata_lck); telemetry_metadata.tm_period = period; telemetry_metadata.tm_source = source; telemetry_metadata.tm_generation += 1; lck_spin_unlock(&telemetry_metadata_lck); ml_set_interrupts_enabled(intrs_en); out: TELEMETRY_PMI_UNLOCK(); return error; #else /* HAS_PMI_MICROSTACKSHOTS */ #pragma unused(pmi_ctr, period) return 1; #endif /* !HAS_PMI_MICROSTACKSHOTS */ } #if HAS_PMI_MICROSTACKSHOTS /* * Mark the current thread for an interrupt-based * telemetry record, to be sampled at the next AST boundary. */ static void _telemetry_set_ast_pmi(bool interrupted_userspace) { telemetry_ast_t reason = TELEMETRY_AST_PMI; thread_t thread = current_thread(); /* * PMI handler was called but microstackshot expected sampling to be * disabled; log it for telemetry and ignore the sample. */ if (!telemetry_sample_pmis) { os_atomic_inc(&telemetry_metadata.tm_samples_skipped, relaxed); return; } reason |= (interrupted_userspace ? TELEMETRY_AST_USER : TELEMETRY_AST_KERNEL); act_set_telemetry_ast(thread, reason); } static void _telemetry_kernel_notify(void * __unused p1, void * __unused p2) { _telemetry_notify_user(TELEMETRY_NOTICE_KERNEL_MICROSTACKSHOT); } #endif /* HAS_PMI_MICROSTACKSHOTS */ void telemetry_handle_ast(thread_t thread, telemetry_ast_t reasons) { assert(reasons != 0); uint32_t record_type = 0; if (reasons & TELEMETRY_AST_IO) { record_type |= kIORecord; } if (reasons & TELEMETRY_AST_VM_FAULT) { record_type |= kVMFaultRecord; } if (reasons & TELEMETRY_AST_PAGE_GRAB) { record_type |= kPageGrabRecord; } if (reasons & (TELEMETRY_AST_USER | TELEMETRY_AST_KERNEL)) { record_type |= (reasons & TELEMETRY_AST_PMI) ? kPMIRecord : kInterruptRecord; } if ((reasons & TELEMETRY_AST_MACF) != 0) { record_type |= kMACFRecord; } enum micro_snapshot_flags user_telemetry = (reasons & TELEMETRY_AST_USER) ? kUserMode : 0; enum micro_snapshot_flags microsnapshot_flags = record_type | user_telemetry; if ((reasons & TELEMETRY_AST_MACF) != 0) { telemetry_macf_take_sample(thread, microsnapshot_flags); } if ((reasons & (TELEMETRY_AST_PMI | TELEMETRY_AST_IO | TELEMETRY_AST_VM_FAULT | TELEMETRY_AST_PAGE_GRAB)) != 0) { telemetry_take_sample(thread, microsnapshot_flags); } } static bool _telemetry_task_can_sample(task_t task) { return (task != TASK_NULL) && !task_did_exec(task) && !task_is_exec_copy(task); } /* * Kernel Thread Microstackshot Support */ #define TELEMETRY_KERNEL_FRAMES_MAX (128) #if HAS_PMI_MICROSTACKSHOTS static const uint32_t TKS_MAGIC = 0x83a83f29; /* * The bare minimum needed to record a sample from interrupt context, stored in * a ringbuffer for later collection. */ struct _telemetry_kernel_sample { clock_sec_t tks_time_secs; uint64_t tks_serial_number; uint64_t tks_telemetry_skipped; uint64_t tks_telemetry_period; uint64_t tks_system_time_in_terminated_threads; uint64_t tks_pagein_count; uint64_t tks_fault_count; uint64_t tks_cow_fault_count; uint64_t tks_thread_id; uint64_t tks_system_time; clock_usec_t tks_time_usecs; uint32_t tks_magic; uint32_t tks_thread_state; uint32_t tks_sched_pri; uint32_t tks_base_pri; uint32_t tks_sched_flags; uint32_t tks_call_stack_size; uint32_t tks_telemetry_source; uint32_t tks_telemetry_generation; uint8_t tks_cpu; uint8_t tks_io_tier; char tks_thread_name[MAXTHREADNAMESIZE]; }; /* * Only collect call stacks up to this maximum length. */ #define TELEMETRY_KERNEL_FRAMES_MAX (128) /* * A scratch buffer that mirrors the format of data stored in the ringbuffer so * it can be written contiguously in a single update. */ struct _telemetry_scratch { struct _telemetry_kernel_sample ts_sample; uintptr_t ts_call_stack[TELEMETRY_KERNEL_FRAMES_MAX]; }; /* * Each writer in interrupt context needs a place off the stack to store these * scratch buffers. */ static struct _telemetry_scratch PERCPU_DATA(_telemetry_pcpu); /* * Collect a sample for the current kernel thread. Must be called in interrupt * context. */ static void _telemetry_take_sample_kernel(thread_t thread, enum micro_snapshot_flags __unused flags) { assert(ml_at_interrupt_context()); uint8_t cpu = (uint8_t)cpu_number(); struct _telemetry_scratch *scratch = PERCPU_GET(_telemetry_pcpu); /* * Collect the call stack in a packed representation to fit more of these * samples into the ringbuffer. */ struct backtrace_control ctl = { .btc_flags = BTF_KERN_INTERRUPTED, }; backtrace_info_t info = BTI_NONE; unsigned int call_stack_count = backtrace(scratch->ts_call_stack, TELEMETRY_KERNEL_FRAMES_MAX, &ctl, &info); unsigned int call_stack_size = call_stack_count * sizeof(scratch->ts_call_stack[0]); /* * Relaxed here, which allows the samples to be non-monotonically * increasing, but avoids any further synchronization with writers. */ uint64_t serial_number = os_atomic_inc(&telemetry_metadata.tm_samples_recorded, relaxed); struct recount_times_mach term_times = recount_task_terminated_times(kernel_task); struct recount_times_mach thread_times = recount_current_thread_times(); clock_sec_t secs = 0; clock_usec_t usecs = 0; clock_get_calendar_microtime(&secs, &usecs); scratch->ts_sample = (struct _telemetry_kernel_sample){ .tks_magic = TKS_MAGIC, .tks_serial_number = serial_number, .tks_telemetry_skipped = os_atomic_load(&telemetry_metadata.tm_samples_skipped, relaxed), .tks_telemetry_period = telemetry_metadata.tm_period, .tks_telemetry_source = telemetry_metadata.tm_source, .tks_telemetry_generation = telemetry_metadata.tm_generation, .tks_cpu = cpu, .tks_time_secs = secs, .tks_time_usecs = usecs, .tks_thread_id = thread_tid(thread), .tks_pagein_count = counter_load(&kernel_task->pageins), .tks_fault_count = counter_load(&kernel_task->faults), .tks_cow_fault_count = counter_load(&kernel_task->cow_faults), .tks_system_time_in_terminated_threads = term_times.rtm_system, .tks_system_time = thread_times.rtm_system, .tks_thread_state = thread->state, .tks_sched_pri = thread->sched_pri, .tks_base_pri = thread->base_pri, .tks_io_tier = (uint8_t)proc_get_effective_thread_policy(thread, TASK_POLICY_IO), .tks_call_stack_size = call_stack_size, }; thread_get_thread_name(thread, scratch->ts_sample.tks_thread_name); /* * Write just the amount needed to store the sample information and call * stack. */ uint32_t size_needed = sizeof(struct _telemetry_kernel_sample) + call_stack_size; uint32_t available = mpsc_ring_write(&_telemetry_kernel_ring, cpu, scratch, size_needed); /* * Check that there was enough space to store the sample. */ bool skipped = available < size_needed; /* * Incrementing samples-recorded in the metadata will cover indicating this * sample is missing to user space. */ if (skipped || available - size_needed <= telemetry_notification_leeway) { if (os_atomic_cmpxchg(&_telemetry_kernel_notified, false, true, relaxed)) { thread_call_enter(_telemetry_kernel_notify_thread); } } } /* * The format of sample data that user space can parse, with no UUIDs present, * as is the case for kernel samples. */ struct _telemetry_kernel_snapshots { struct micro_snapshot tkse_micro_snap; struct microstackshot_task tkse_task_snap; struct microstackshot_thread tkse_thread_snap; }; /* * Convert a kernel sample into the trio of snapshots that user space can parse. */ static void _telemetry_kernel_snapshot( struct _telemetry_kernel_snapshots *snaps, struct _telemetry_kernel_sample *sample) { snaps->tkse_micro_snap = (struct micro_snapshot){ .snapshot_magic = STACKSHOT_MICRO_SNAPSHOT_MAGIC, .ms_flags = (uint8_t)(kPMIRecord | kKernelThread), .ms_cpu = sample->tks_cpu, .ms_time = sample->tks_time_secs, .ms_time_microsecs = sample->tks_time_usecs, }; snaps->tkse_task_snap = (struct microstackshot_task){ .mst_magic = STACKSHOT_TASK_SNAPSHOT_MAGIC, .mst_stackshot_flags_trunc = kKernel64_p, .mst_stackshot_flags = kKernel64_p, .mst_pid = 0, .mst_task_uniqueid = 0, .mst_system_term_mach_time = sample->tks_system_time_in_terminated_threads, /* * This cannot be queried in the ledger while running in interrupt context. */ .mst_page_count = get_task_phys_footprint(kernel_task) / PAGE_SIZE, .mst_fault_count = sample->tks_fault_count, .mst_pagein_count = sample->tks_pagein_count, .mst_cow_fault_count = sample->tks_cow_fault_count, .mst_proc_comm_name = "kernel_task", .mst_was_throttled = 0, .mst_did_throttle = 0, .mst_resource_coal_id = coalition_id(kernel_task->coalition[COALITION_TYPE_RESOURCE]), .mst_on_behalf_origin_pid = -1, .mst_on_behalf_proximate_pid = -1, .mst_latency_qos = LATENCY_QOS_TIER_UNSPECIFIED, .mst_metadata = { .mstm_telemetry_source = sample->tks_telemetry_source, .mstm_telemetry_generation = sample->tks_telemetry_generation, .mstm_telemetry_period = sample->tks_telemetry_period, .mstm_serial_number = sample->tks_serial_number, .mstm_telemetry_skipped = sample->tks_telemetry_skipped, }, }; snaps->tkse_thread_snap = (struct microstackshot_thread){ .msth_magic = STACKSHOT_THREAD_SNAPSHOT_MAGIC, .msth_stackshot_flags = kKernel64_p, .msth_kern_frame_count = sample->tks_call_stack_size / sizeof(uintptr_t), .msth_wait_event = 0, .msth_continuation = 0, .msth_thread_id = sample->tks_thread_id, .msth_system_mach_time = sample->tks_system_time, .msth_state = sample->tks_thread_state, .msth_base_priority = sample->tks_base_pri, .msth_sched_priority = sample->tks_sched_pri, .msth_io_tier = sample->tks_io_tier, }; memset(snaps->tkse_thread_snap.msth_name, 0, sizeof(snaps->tkse_thread_snap.msth_name)); strlcpy(snaps->tkse_thread_snap.msth_name, sample->tks_thread_name, sizeof(snaps->tkse_thread_snap.msth_name)); } #endif /* HAS_PMI_MICROSTACKSHOTS */ int telemetry_kernel_gather(user_addr_t user_buffer, uint32_t *user_length) { #if HAS_PMI_MICROSTACKSHOTS int result = 0; /* * Track how much data has been copied out to the user buffer. */ uint32_t copied = 0; uint32_t copy_length = *user_length; *user_length = 0; /* * Get a cursor to read from the ringbuffer. */ mpsc_ring_cursor_t cursor = mpsc_ring_read_start(&_telemetry_kernel_ring); while (copied < copy_length) { /* * This function is called directly off a syscall, so it can afford to * use some stack space. */ struct _telemetry_kernel_snapshots snaps = { 0 }; /* * Check that the user buffer still has enough space for at least the * snapshot structures. */ if (sizeof(snaps) > copy_length - copied) { break; } /* * Read the sample from the ringbuffer. */ struct _telemetry_kernel_sample sample = { 0 }; bool advanced = mpsc_ring_cursor_advance( &_telemetry_kernel_ring, &cursor, &sample, sizeof(sample)); /* * If there's no more data, return to user space. */ if (!advanced) { break; } if (sample.tks_magic != TKS_MAGIC) { panic("microstackshot: kernel sample magic is invalid"); } /* * Compute the size needed for the snapshots and call stack and bail * out if there's not enough room in the user's buffer. */ assert3u(sample.tks_call_stack_size, <, sizeof(uintptr_t) * TELEMETRY_KERNEL_FRAMES_MAX); uint32_t size_needed = sizeof(snaps) + sample.tks_call_stack_size; if (size_needed > copy_length - copied) { break; } /* * Convert the sample into snapshots suitable for user space and copy * them out. */ _telemetry_kernel_snapshot(&snaps, &sample); result = copyout(&snaps, user_buffer + copied, sizeof(snaps)); if (result != 0) { break; } copied += sizeof(snaps); /* * Copy the call stack out of the ringbuffer. */ uintptr_t call_stack[TELEMETRY_KERNEL_FRAMES_MAX] = { 0 }; assert3u(sizeof(call_stack), >=, sample.tks_call_stack_size); advanced = mpsc_ring_cursor_advance( &_telemetry_kernel_ring, &cursor, &call_stack, sample.tks_call_stack_size); /* * There must be a call stack after the sample, otherwise something got * corrupted and there's no more framing information for the reader. */ assert(advanced); uint32_t call_stack_count = sample.tks_call_stack_size / sizeof(uintptr_t); for (uint32_t i = 0; i < call_stack_count; i++) { /* * The last frame of the call stack can sometimes be 0, ignore it. */ if (call_stack[i] != 0) { call_stack[i] = VM_KERNEL_UNSLIDE(call_stack[i]); } } /* * Copy the unpacked call stack out to user space. */ result = copyout(&call_stack, user_buffer + copied, sample.tks_call_stack_size); if (result != 0) { break; } copied += sample.tks_call_stack_size; mpsc_ring_cursor_commit(&_telemetry_kernel_ring, &cursor); } /* * On success, store the number of bytes copied. * * Some partial data may have been copied out, but user space shouldn't * try to inspect it. */ if (result == 0) { /* * Complete the read operation and sync any progress back to the ringbuffer. */ mpsc_ring_read_finish(&_telemetry_kernel_ring, cursor); os_atomic_store(&_telemetry_kernel_notified, false, relaxed); *user_length = copied; } else { mpsc_ring_read_cancel(&_telemetry_kernel_ring, cursor); } return result; #else /* HAS_PMI_MICROSTACKSHOTS */ #pragma unused(user_buffer, user_length) return ENOTSUP; #endif /* !HAS_PMI_MICROSTACKSHOTS */ } void telemetry_instrumentation_begin( __unused struct micro_snapshot_buffer *buffer, __unused enum micro_snapshot_flags flags) { /* telemetry_XXX accessed outside of lock for instrumentation only */ KDBG(MACHDBG_CODE(DBG_MACH_STACKSHOT, MICROSTACKSHOT_RECORD) | DBG_FUNC_START, flags, telemetry_bytes_since_last_mark, 0, (&telemetry_buffer != buffer)); } void telemetry_instrumentation_end(__unused struct micro_snapshot_buffer *buffer) { /* telemetry_XXX accessed outside of lock for instrumentation only */ KDBG(MACHDBG_CODE(DBG_MACH_STACKSHOT, MICROSTACKSHOT_RECORD) | DBG_FUNC_END, (&telemetry_buffer == buffer), telemetry_bytes_since_last_mark, buffer->current_position, buffer->end_point); } static void _telemetry_take_sample_user(thread_t thread, enum micro_snapshot_flags flags) { uintptr_t frames[128]; size_t frames_len = sizeof(frames) / sizeof(frames[0]); uint32_t btcount; struct backtrace_user_info btinfo = BTUINFO_INIT; uint16_t async_start_index = UINT16_MAX; /* Collect backtrace from user thread. */ btcount = backtrace_user(frames, frames_len, NULL, &btinfo); if (btinfo.btui_error != 0) { return; } if (btinfo.btui_async_frame_addr != 0 && btinfo.btui_async_start_index != 0) { /* * Put the async callstack inline after the frame pointer walk call * stack. */ async_start_index = (uint16_t)btinfo.btui_async_start_index; uintptr_t frame_addr = btinfo.btui_async_frame_addr; unsigned int frames_left = frames_len - async_start_index; struct backtrace_control ctl = { .btc_frame_addr = frame_addr, }; btinfo = BTUINFO_INIT; unsigned int async_filled = backtrace_user(frames + async_start_index, frames_left, &ctl, &btinfo); if (btinfo.btui_error == 0) { btcount = MIN(async_start_index + async_filled, frames_len); } } /* * Capture any other metadata and write it to the telemetry buffer. */ struct telemetry_target target = { .thread = thread, .frames = frames, .frames_count = btcount, .user64_regs = (btinfo.btui_info & BTI_64_BIT) != 0, .microsnapshot_flags = flags, .include_metadata = flags & (kPMIRecord | kVMFaultRecord | kPageGrabRecord), .buffer = &telemetry_buffer, .buffer_mtx = &telemetry_mtx, .async_start_index = async_start_index, }; telemetry_process_sample(&target, true, NULL); } void telemetry_take_sample(thread_t thread, enum micro_snapshot_flags flags) { if (thread == THREAD_NULL) { return; } /* Ensure task is ready for taking a sample. */ task_t task = get_threadtask(thread); if (!_telemetry_task_can_sample(task)) { os_atomic_inc(&telemetry_metadata.tm_samples_skipped, relaxed); return; } telemetry_instrumentation_begin(&telemetry_buffer, flags); _telemetry_take_sample_user(thread, flags); telemetry_instrumentation_end(&telemetry_buffer); } #if CONFIG_MACF void telemetry_macf_take_sample(thread_t thread, enum micro_snapshot_flags flags) { task_t task; uintptr_t frames_stack[128]; vm_size_t btcapacity = ARRAY_COUNT(frames_stack); uint32_t btcount = 0; typedef uintptr_t telemetry_user_frame_t __kernel_data_semantics; telemetry_user_frame_t *frames = frames_stack; bool alloced_frames = false; struct backtrace_user_info btinfo = BTUINFO_INIT; struct backtrace_control btctl = BTCTL_INIT; uint32_t retry_count = 0; const uint32_t max_retries = 10; bool initialized = false; struct micro_snapshot_buffer *telbuf = &telemetry_macf_buffer; uint32_t record_start = 0; bool did_process = false; int rv = 0; if (thread == THREAD_NULL) { return; } telemetry_instrumentation_begin(telbuf, flags); /* Ensure task is ready for taking a sample. */ task = get_threadtask(thread); if (!_telemetry_task_can_sample(task) || task == kernel_task) { rv = EBUSY; goto out; } /* Ensure MACF telemetry buffer was initialized. */ TELEMETRY_MACF_LOCK(); initialized = (telbuf->size > 0); TELEMETRY_MACF_UNLOCK(); if (!initialized) { rv = ENOMEM; goto out; } /* Collect backtrace from user thread. */ while (retry_count < max_retries) { btcount += backtrace_user(frames + btcount, btcapacity - btcount, &btctl, &btinfo); if ((btinfo.btui_info & BTI_TRUNCATED) != 0 && btinfo.btui_next_frame_addr != 0) { /* * Fast path uses stack memory to avoid an allocation. We must * pivot to heap memory in the case where we cannot write the * complete backtrace to this buffer. */ if (frames == frames_stack) { btcapacity += 128; frames = kalloc_data(btcapacity * sizeof(*frames), Z_WAITOK); if (frames == NULL) { break; } alloced_frames = true; assert(btcapacity > sizeof(frames_stack) / sizeof(frames_stack[0])); memcpy(frames, frames_stack, sizeof(frames_stack)); } else { assert(alloced_frames); frames = krealloc_data(frames, btcapacity * sizeof(*frames), (btcapacity + 128) * sizeof(*frames), Z_WAITOK); if (frames == NULL) { break; } btcapacity += 128; } btctl.btc_frame_addr = btinfo.btui_next_frame_addr; ++retry_count; } else { break; } } if (frames == NULL) { rv = ENOMEM; goto out; } else if (btinfo.btui_error != 0) { rv = btinfo.btui_error; goto out; } /* Process the backtrace. */ struct telemetry_target target = { .thread = thread, .frames = frames, .frames_count = btcount, .user64_regs = (btinfo.btui_info & BTI_64_BIT) != 0, .microsnapshot_flags = flags, .include_metadata = false, .buffer = telbuf, .buffer_mtx = &telemetry_macf_mtx }; rv = telemetry_process_sample(&target, false, &record_start); did_process = true; out: /* Immediately deliver the collected sample to MAC clients. */ if (rv == 0) { assert(telbuf->current_position >= record_start); mac_thread_telemetry(thread, 0, (void *)(telbuf->buffer + record_start), telbuf->current_position - record_start); } else { mac_thread_telemetry(thread, rv, NULL, 0); } /* * The lock was taken by telemetry_process_sample, and we asked it not to * unlock upon completion, so we must release the lock here. */ if (did_process) { TELEMETRY_MACF_UNLOCK(); } if (alloced_frames && frames != NULL) { kfree_data(frames, btcapacity * sizeof(*frames)); } telemetry_instrumentation_end(telbuf); } #endif /* CONFIG_MACF */ static void _write_task_snapshot( struct microstackshot_task *tsnap, const struct telemetry_target *target) { struct task *task = get_threadtask(target->thread); struct proc *p = get_bsdtask_info(task); tsnap->mst_magic = STACKSHOT_TASK_SNAPSHOT_MAGIC; tsnap->mst_pid = proc_pid(p); tsnap->mst_task_uniqueid = proc_uniqueid(p); struct recount_times_mach times = recount_task_terminated_times(task); tsnap->mst_user_term_mach_time = times.rtm_user; tsnap->mst_system_term_mach_time = times.rtm_system; tsnap->mst_suspend_count = task->suspend_count; tsnap->mst_page_count = (typeof(tsnap->mst_page_count))(get_task_phys_footprint(task) / PAGE_SIZE); tsnap->mst_fault_count = counter_load(&task->faults); tsnap->mst_pagein_count = counter_load(&task->pageins); tsnap->mst_cow_fault_count = counter_load(&task->cow_faults); /* * The throttling counters are maintained as 64-bit counters in the proc * structure. However, we reserve 32-bits (each) for them in * `struct microstackshot_task` struct to save space and since we do not * expect them to overflow 32-bits. */ tsnap->mst_was_throttled = (uint32_t)proc_was_throttled(p); tsnap->mst_did_throttle = (uint32_t)proc_did_throttle(p); #if CONFIG_COALITIONS coalition_t rsrc_coal = task->coalition[COALITION_TYPE_RESOURCE]; tsnap->mst_resource_coal_id = rsrc_coal ? coalition_id(rsrc_coal) : 0; pid_t origin_pid = -1, proximate_pid = -1; (void)thread_get_voucher_origin_proximate_pid(target->thread, &origin_pid, &proximate_pid); tsnap->mst_on_behalf_proximate_pid = proximate_pid; tsnap->mst_on_behalf_origin_pid = origin_pid; #endif /* CONFIG_COALITIONS */ uint64_t ss_flags = kcdata_get_task_ss_flags(task, false); tsnap->mst_stackshot_flags = ss_flags; tsnap->mst_stackshot_flags_trunc = (uint32_t)ss_flags; tsnap->mst_energy_nj = recount_task_energy_nj(task); int64_t pages_grabbed = 0; ledger_get_balance(task->ledger, task_ledgers.pages_grabbed, 0, &pages_grabbed); tsnap->mst_page_grab_count = pages_grabbed; int64_t pages_grabbed_iopl = 0; ledger_get_balance(task->ledger, task_ledgers.pages_grabbed_iopl, 0, &pages_grabbed_iopl); int64_t pages_grabbed_upl = 0; ledger_get_balance(task->ledger, task_ledgers.pages_grabbed_upl, 0, &pages_grabbed_upl); tsnap->mst_iopl_upl_page_grab_count = pages_grabbed_iopl + pages_grabbed_upl; tsnap->mst_latency_qos = task_grab_latency_qos(task); strlcpy(tsnap->mst_proc_comm_name, proc_name_address(p), sizeof(tsnap->mst_proc_comm_name)); const char *longname = proc_longname_address(p); if (longname[0] != '\0') { strlcpy((char *)tsnap->mst_proc_name_extra, &longname[16], sizeof(tsnap->mst_proc_name_extra)); } if (target->include_metadata) { enum micro_snapshot_flags mss_flags = target->microsnapshot_flags; telemetry_source_t source = TMSRC_UNKNOWN; if (mss_flags & kPMIRecord) { source = telemetry_metadata.tm_source; #if CONFIG_MEMORY_MICROSTACKSHOT } else if (mss_flags & kVMFaultRecord) { source = TMSRC_VM_FAULTS; } else if (mss_flags & kPageGrabRecord) { source = TMSRC_PAGE_GRABS; #endif /* CONFIG_MEMORY_MICROSTACKSHOT */ } uint64_t period = 0; switch (source) { #if CONFIG_MEMORY_MICROSTACKSHOT case TMSRC_VM_FAULTS: period = telemetry_vm_fault_period; break; case TMSRC_PAGE_GRABS: period = telemetry_page_grab_period; break; #endif /* CONFIG_MEMORY_MICROSTACKSHOT */ default: period = telemetry_metadata.tm_period; break; } tsnap->mst_metadata.mstm_telemetry_source = source; tsnap->mst_metadata.mstm_telemetry_generation = telemetry_metadata.tm_generation; tsnap->mst_metadata.mstm_telemetry_period = period; tsnap->mst_metadata.mstm_serial_number = os_atomic_inc(&telemetry_metadata.tm_samples_recorded, relaxed); tsnap->mst_metadata.mstm_telemetry_skipped = telemetry_metadata.tm_samples_skipped; } if (task->task_shared_region_slide != -1) { tsnap->mst_shared_cache_slide = task->task_shared_region_slide; bcopy(task->task_shared_region_uuid, tsnap->mst_shared_cache_identifier, sizeof(task->task_shared_region_uuid)); } } static void _write_thread_snapshot(struct microstackshot_thread *thsnap, const struct telemetry_target *target) { struct thread *thread = target->thread; thsnap->msth_magic = STACKSHOT_THREAD_SNAPSHOT_MAGIC; thsnap->msth_thread_id = thread_tid(thread); thsnap->msth_state = thread->state; thsnap->msth_base_priority = thread->base_pri; thsnap->msth_sched_priority = thread->sched_pri; thsnap->msth_sched_flags = thread->sched_flags; thsnap->msth_effective_qos = thread->effective_policy.thep_qos; thsnap->msth_requested_qos = thread->requested_policy.thrp_qos; thsnap->msth_requested_qos_override = MAX(thread->requested_policy.thrp_qos_override, thread->requested_policy.thrp_qos_workq_override); thsnap->msth_user_frame_count = target->frames_count; static_assert(MAXTHREADNAMESIZE <= STACKSHOT_MAX_THREAD_NAME_SIZE); thread_get_thread_name(thread, thsnap->msth_name); thsnap->msth_async_index = target->async_start_index; #if CONFIG_MEMORY_MICROSTACKSHOT if (target->microsnapshot_flags & kVMFaultRecord) { thsnap->msth_fault_va = thread->t_vm_fault_info.tvfi_va; thsnap->msth_fault_type = thread->t_vm_fault_info.tvfi_type; thsnap->msth_fault_flags = thread->t_vm_fault_info.tvfi_flags; memset(&thread->t_vm_fault_info, 0, sizeof(thread->t_vm_fault_info)); } if (target->microsnapshot_flags & kPageGrabRecord) { thsnap->msth_grab_vm_tag = thread->t_page_grab_info.tpgi_tag; thsnap->msth_grab_iopl_count = thread->t_page_grab_info.tpgi_iopl_count; thsnap->msth_grab_upl_count = thread->t_page_grab_info.tpgi_upl_count; memset(&thread->t_page_grab_info, 0, sizeof(thread->t_page_grab_info)); } #endif /* CONFIG_MEMORY_MICROSTACKSHOT */ thsnap->msth_stackshot_flags |= kStacksPCOnly; if (proc_get_effective_thread_policy(thread, TASK_POLICY_DARWIN_BG)) { thsnap->msth_stackshot_flags |= kThreadDarwinBG; } if (target->user64_regs) { thsnap->msth_stackshot_flags |= kUser64_p; } boolean_t interrupt_state = ml_set_interrupts_enabled(FALSE); struct recount_times_mach times = recount_current_thread_times(); ml_set_interrupts_enabled(interrupt_state); thsnap->msth_user_mach_time = times.rtm_user; thsnap->msth_system_mach_time = times.rtm_system; } struct _telemetry_uuids { errno_t error; void *uuid_info; uint32_t uuid_info_count; uint32_t uuid_info_size; }; /* * Retrieve the array of UUIDs for binaries used by this task. */ static struct _telemetry_uuids _telemetry_sample_uuids(task_t task, unsigned int extra_elements) { bool const user64_va = task_has_64Bit_addr(task); uint32_t uuid_info_count_unsafe = 0; mach_vm_address_t uuid_info_addr = 0; uint32_t uuid_info_size = 0; if (user64_va) { uuid_info_size = sizeof(struct user64_dyld_uuid_info); struct user64_dyld_all_image_infos task_image_infos; if (copyin(task->all_image_info_addr, &task_image_infos, sizeof(task_image_infos)) == 0) { uuid_info_count_unsafe = (uint32_t)task_image_infos.uuidArrayCount; uuid_info_addr = task_image_infos.uuidArray; } } else { uuid_info_size = sizeof(struct user32_dyld_uuid_info); struct user32_dyld_all_image_infos task_image_infos; if (copyin(task->all_image_info_addr, &task_image_infos, sizeof(task_image_infos)) == 0) { uuid_info_count_unsafe = task_image_infos.uuidArrayCount; uuid_info_addr = task_image_infos.uuidArray; } } /* * If dyld is updating the data structure (indicated by a NULL uuidArray field), * do not provide any UUIDs with the sample. */ if (uuid_info_addr == USER_ADDR_NULL) { return (struct _telemetry_uuids){}; } /* * The main binary and interesting non-shared-cache libraries should be in the first few images. */ unsigned int uuid_info_count = MIN(uuid_info_count_unsafe, TELEMETRY_MAX_UUID_COUNT); if (uuid_info_count == 0) { return (struct _telemetry_uuids){}; } uint32_t copyin_size = uuid_info_count * uuid_info_size; uint32_t alloc_size = (uuid_info_count + extra_elements) * uuid_info_size; char *uuid_info_array = kalloc_data(alloc_size, Z_WAITOK); if (uuid_info_array == NULL) { return (struct _telemetry_uuids){ .error = ENOMEM, }; } /* * Copy in the UUID info array. */ if (copyin(uuid_info_addr, uuid_info_array, copyin_size) != 0) { /* * Don't report this as an error with the sample to avoid transient * dyld operations from impacting sample collection. */ kfree_data(uuid_info_array, alloc_size); return (struct _telemetry_uuids){}; } return (struct _telemetry_uuids){ .uuid_info = uuid_info_array, .uuid_info_count = uuid_info_count, .uuid_info_size = alloc_size, }; } static bool _telemetry_sample_dispatch_serialno(task_t task, thread_t thread, uint64_t *serialno_out) { uint64_t const dqkeyaddr = thread_dispatchqaddr(thread); if (dqkeyaddr != 0) { uint64_t dqaddr = 0; size_t const user_ptr_size = task_has_64Bit_addr(task) ? 8 : 4; uint64_t const dq_serialno_offset = get_task_dispatchqueue_serialno_offset(task); if ((copyin(dqkeyaddr, (char *)&dqaddr, user_ptr_size) == 0) && (dqaddr != 0) && (dq_serialno_offset != 0)) { uint64_t dqserialnumaddr = dqaddr + dq_serialno_offset; if (copyin(dqserialnumaddr, serialno_out, user_ptr_size) == 0) { return true; } } } return false; } static void * _telemetry_buffer_alloc(struct micro_snapshot_buffer *buf, size_t size) { void *alloc = (void *)(uintptr_t)(buf->buffer + buf->current_position); buf->current_position += size; assert3u(buf->current_position, <=, buf->size); memset(alloc, 0, size); return alloc; } int telemetry_process_sample(const struct telemetry_target *target, bool release_buffer_lock, uint32_t *out_current_record_start) { thread_t const thread = target->thread; size_t const btcount = target->frames_count; bool const user64_regs = target->user64_regs; struct micro_snapshot_buffer * const current_buffer = target->buffer; lck_mtx_t * const buffer_mtx = target->buffer_mtx; clock_sec_t secs; clock_usec_t usecs; bool notify = false; int rv = 0; if (thread == THREAD_NULL) { return EINVAL; } task_t const task = get_threadtask(thread); struct _telemetry_uuids uuids = _telemetry_sample_uuids(task, 0); /* * Look for a dispatch queue serial number, and copy it in from userland if present. */ uint64_t dqserial = 0; bool dqserial_valid = _telemetry_sample_dispatch_serialno(task, thread, &dqserial); size_t const frames_size = btcount * (user64_regs ? 8 : 4); size_t const sample_size = _telemetry_sample_size_static + uuids.uuid_info_size + (dqserial_valid ? sizeof(dqserial) : 0) + frames_size; clock_get_calendar_microtime(&secs, &usecs); /* * We do the bulk of the operation under the telemetry lock, on assumption that * any page faults during execution will not cause another AST_TELEMETRY * to deadlock; they will just block until we finish. This makes it easier * to copy into the buffer directly. As soon as we unlock, userspace can copy * out of our buffer. */ lck_mtx_lock(buffer_mtx); /* * If the buffer has been deallocated, there's no way to take a sample. */ if (!current_buffer->buffer) { rv = EINVAL; } /* * If the sample would be larger than the entire buffer, ignore it. */ if (rv == 0 && current_buffer->size < sample_size) { rv = ERANGE; } if (rv == 0) { if ((current_buffer->size - current_buffer->current_position) < sample_size) { /* * We can't fit a record in the space available, so wrap around to the beginning. * Save the current position as the known end point of valid data. */ current_buffer->end_point = current_buffer->current_position; current_buffer->current_position = 0; } uint32_t current_record_start = current_buffer->current_position; /* * Write the snapshots and variable-length arrays into the telemetry buffer. */ struct micro_snapshot *msnap = _telemetry_buffer_alloc(current_buffer, sizeof(*msnap)); *msnap = (struct micro_snapshot){ .snapshot_magic = STACKSHOT_MICRO_SNAPSHOT_MAGIC, .ms_flags = (uint8_t)target->microsnapshot_flags, .ms_cpu = cpu_number(), .ms_time = secs, .ms_time_microsecs = usecs, }; struct microstackshot_task *tsnap = _telemetry_buffer_alloc(current_buffer, sizeof(*tsnap)); _write_task_snapshot(tsnap, target); if (uuids.uuid_info_size > 0) { void *uuid_info_buf = _telemetry_buffer_alloc(current_buffer, uuids.uuid_info_size); memcpy(uuid_info_buf, uuids.uuid_info, uuids.uuid_info_size); tsnap->mst_loadinfo_count = uuids.uuid_info_count; } struct microstackshot_thread *thsnap = _telemetry_buffer_alloc(current_buffer, sizeof(*thsnap)); _write_thread_snapshot(thsnap, target); if (dqserial_valid) { thsnap->msth_stackshot_flags |= kHasDispatchSerial; uint64_t *dqserial_buf = _telemetry_buffer_alloc(current_buffer, sizeof(*dqserial_buf)); memcpy(dqserial_buf, &dqserial, sizeof(dqserial)); } void *frames_buf = _telemetry_buffer_alloc(current_buffer, frames_size); if (user64_regs) { memcpy(frames_buf, target->frames, frames_size); } else { uint32_t *frames_32 = frames_buf; for (int i = 0; i < btcount; i++) { frames_32[i] = (uint32_t)target->frames[i]; } } if (current_buffer->end_point < current_buffer->current_position) { /* * Each time the cursor wraps around to the beginning, we leave a * differing amount of unused space at the end of the buffer. Make * sure the cursor pushes the end point in case we're making use of * more of the buffer than we did the last time we wrapped. */ current_buffer->end_point = current_buffer->current_position; } /* * Now THIS is a hack. */ if (current_buffer == &telemetry_buffer) { telemetry_bytes_since_last_mark += (current_buffer->current_position - current_record_start); if (telemetry_bytes_since_last_mark > telemetry_buffer_notify_at) { notify = true; } } if (out_current_record_start != NULL) { *out_current_record_start = current_record_start; } } if (release_buffer_lock) { lck_mtx_unlock(buffer_mtx); } if (notify) { _telemetry_notify_user(TELEMETRY_NOTICE_BASE); } if (uuids.uuid_info != NULL) { kfree_data(uuids.uuid_info, uuids.uuid_info_size); } return rv; } int telemetry_gather(user_addr_t buffer, uint32_t *length, bool mark) { return telemetry_buffer_gather(buffer, length, mark, &telemetry_buffer); } int telemetry_buffer_gather(user_addr_t buffer, uint32_t *length, bool mark, struct micro_snapshot_buffer * current_buffer) { int result = 0; uint32_t oldest_record_offset; KDBG(MACHDBG_CODE(DBG_MACH_STACKSHOT, MICROSTACKSHOT_GATHER) | DBG_FUNC_START, mark, telemetry_bytes_since_last_mark, 0, (&telemetry_buffer != current_buffer)); TELEMETRY_LOCK(); if (current_buffer->buffer == 0) { *length = 0; goto out; } if (*length < current_buffer->size) { result = KERN_NO_SPACE; goto out; } /* * Copy the ring buffer out to userland in order sorted by time: least recent to most recent. * First, we need to search forward from the cursor to find the oldest record in our buffer. */ oldest_record_offset = current_buffer->current_position; do { if (((oldest_record_offset + sizeof(uint32_t)) > current_buffer->size) || ((oldest_record_offset + sizeof(uint32_t)) > current_buffer->end_point)) { if (*(uint32_t *)(uintptr_t)(current_buffer->buffer) == 0) { /* * There is no magic number at the start of the buffer, which means * it's empty; nothing to see here yet. */ *length = 0; goto out; } /* * We've looked through the end of the active buffer without finding a valid * record; that means all valid records are in a single chunk, beginning at * the very start of the buffer. */ oldest_record_offset = 0; assert(*(uint32_t *)(uintptr_t)(current_buffer->buffer) == STACKSHOT_MICRO_SNAPSHOT_MAGIC); break; } if (*(uint32_t *)(uintptr_t)(current_buffer->buffer + oldest_record_offset) == STACKSHOT_MICRO_SNAPSHOT_MAGIC) { break; } /* * There are no alignment guarantees for micro-stackshot records, so we must search at each * byte offset. */ oldest_record_offset++; } while (oldest_record_offset != current_buffer->current_position); /* * If needed, copyout in two chunks: from the oldest record to the end of the buffer, and then * from the beginning of the buffer up to the current position. */ if (oldest_record_offset != 0) { if ((result = copyout((void *)(current_buffer->buffer + oldest_record_offset), buffer, current_buffer->end_point - oldest_record_offset)) != 0) { *length = 0; goto out; } *length = current_buffer->end_point - oldest_record_offset; } else { *length = 0; } if ((result = copyout((void *)current_buffer->buffer, buffer + *length, current_buffer->current_position)) != 0) { *length = 0; goto out; } *length += (uint32_t)current_buffer->current_position; out: if (mark && (*length > 0)) { telemetry_bytes_since_last_mark = 0; } TELEMETRY_UNLOCK(); KDBG(MACHDBG_CODE(DBG_MACH_STACKSHOT, MICROSTACKSHOT_GATHER) | DBG_FUNC_END, current_buffer->current_position, *length, current_buffer->end_point, (&telemetry_buffer != current_buffer)); return result; } #if CONFIG_MACF static int telemetry_macf_init_locked(size_t buffer_size) { kern_return_t kr; if (buffer_size > TELEMETRY_MAX_BUFFER_SIZE) { buffer_size = TELEMETRY_MAX_BUFFER_SIZE; } telemetry_macf_buffer.size = buffer_size; kr = kmem_alloc(kernel_map, &telemetry_macf_buffer.buffer, telemetry_macf_buffer.size, KMA_DATA | KMA_ZERO | KMA_PERMANENT, VM_KERN_MEMORY_SECURITY); if (kr != KERN_SUCCESS) { kprintf("Telemetry (MACF): Allocation failed: %d\n", kr); return ENOMEM; } return 0; } int telemetry_macf_mark_curthread(void) { thread_t thread = current_thread(); task_t task = get_threadtask(thread); int rv = 0; if (task == kernel_task) { /* Kernel threads never return to an AST boundary, and are ineligible */ return EINVAL; } /* Initialize the MACF telemetry buffer if needed. */ TELEMETRY_MACF_LOCK(); if (__improbable(telemetry_macf_buffer.size == 0)) { rv = telemetry_macf_init_locked(TELEMETRY_MACF_DEFAULT_BUFFER_SIZE); if (rv != 0) { return rv; } } TELEMETRY_MACF_UNLOCK(); act_set_telemetry_ast(thread, TELEMETRY_AST_MACF); return 0; } #endif /* CONFIG_MACF */ #pragma mark - Page-in Telemetry #if CONFIG_THREAD_GROUPS /* * Global data used by the page-in telemetry subsystem. */ struct _telemetry_pagein_globals { /* * The size of the buffer to create to hold page-in telemetry. * * Clamped to `TELEMETRY_PAGEIN_BUFFER_SIZE_MAX`. */ size_t tp_buffer_size; /* * Any flags that affect collection. */ telemetry_pagein_flags_t tp_flags; /* * The ID of the thread group that is currently collecting page-in * telemetry. * * Any threads with their home thread group as this ID are eligible. */ uint64_t tp_thread_group_id; /* * Once page-in telemetry has started, the buffer that holds the page-in * data. */ struct telemetry_pagein *tp_buffer; /* * The allocated buffer size. */ size_t tp_buffer_allocated_size; /* * The number of page-in telemetry elements requested to be counted. * * This can be larger than the capacity. */ unsigned int _Atomic tp_count; /* * The number of entries the buffer can hold. */ unsigned int tp_capacity; /* * The `mach_continuous_time()` when page-in telemetry started. */ uint64_t tp_start_mct; }; /* * A mutex to protect access to page-in telemetry globals. */ LCK_MTX_DECLARE(_telemetry_pagein_mtx, &telemetry_lck_grp); /* * The globals protected by the `_telemetry_pagein_mtx` lock. */ struct _telemetry_pagein_globals _pagein_globals = { 0 }; #define TELEMETRY_PAGEIN_BUFFER_MAX_SIZE (5 << 20) int telemetry_pagein_setup( uint64_t buffer_size, telemetry_pagein_flags_t flags) { if (buffer_size > TELEMETRY_PAGEIN_BUFFER_MAX_SIZE) { buffer_size = TELEMETRY_PAGEIN_BUFFER_MAX_SIZE; } if (buffer_size > 0 && buffer_size < sizeof(struct telemetry_pagein)) { return EINVAL; } lck_mtx_lock(&_telemetry_pagein_mtx); /* * Consider making this a once-only configuration on release kernels? */ _pagein_globals.tp_buffer_size = buffer_size; _pagein_globals.tp_flags = flags; lck_mtx_unlock(&_telemetry_pagein_mtx); return 0; } void telemetry_pagein_start(void *coal) { if (coal == COALITION_NULL) { return; } struct thread_group *tg = coalition_get_thread_group(coal); if (!tg) { return; } uint64_t tgid = thread_group_get_id(tg); lck_mtx_lock(&_telemetry_pagein_mtx); uint64_t buffer_size = _pagein_globals.tp_buffer_size; lck_mtx_unlock(&_telemetry_pagein_mtx); if (buffer_size == 0) { return; } struct telemetry_pagein *buf = kalloc_data_tag( buffer_size, Z_WAITOK | Z_ZERO, VM_KERN_MEMORY_DIAG); if (!buf) { printf("telemetry: failed to allocate %lld byte buffer for page-ins", buffer_size); return; } bool assigned = false; lck_mtx_lock(&_telemetry_pagein_mtx); if (_pagein_globals.tp_buffer == NULL) { os_atomic_store(&_pagein_globals.tp_count, 0, relaxed); _pagein_globals.tp_buffer = buf; assigned = true; _pagein_globals.tp_buffer_allocated_size = buffer_size; _pagein_globals.tp_capacity = _pagein_globals.tp_buffer_size / sizeof(struct telemetry_pagein); _pagein_globals.tp_start_mct = mach_continuous_time(); os_atomic_store(&_pagein_globals.tp_thread_group_id, tgid, release); } lck_mtx_unlock(&_telemetry_pagein_mtx); if (!assigned) { kfree_data(buf, buffer_size); } } static bool _pagein_telemetry_active(uint64_t *trace_tgid_out) { uint64_t tgid = _pagein_globals.tp_thread_group_id; if (__probable(tgid == 0)) { return false; } thread_t curth = current_thread(); struct thread_group *curtg = thread_group_get_home_group(curth); *trace_tgid_out = tgid; return curtg && thread_group_get_id(curtg) == tgid; } static bool _pagein_get_emit_index(unsigned int *index_out, uint64_t *trace_tgid_out) { if (__probable(!_pagein_telemetry_active(trace_tgid_out))) { return false; } unsigned int reserved_index = os_atomic_inc_orig(&_pagein_globals.tp_count, relaxed); /* * Best effort to avoid expense, this is double-checked under the lock later. */ if (reserved_index >= _pagein_globals.tp_capacity) { return false; } *index_out = reserved_index; return true; } static void _pagein_emit_internal( unsigned int index, uint64_t tgid, struct vnode *vp, off_t file_offset) { extern int vnode_get_ids(struct vnode *vp, uint64_t *fsid_out, uint64_t *fsobj_id_out); uint64_t fsid, fsobj_id; int error = vnode_get_ids(vp, &fsid, &fsobj_id); if (error != 0) { return; } lck_mtx_lock(&_telemetry_pagein_mtx); bool const correct_tgid = tgid == _pagein_globals.tp_thread_group_id; bool const has_space = _pagein_globals.tp_capacity > index; if (correct_tgid && has_space) { _pagein_globals.tp_buffer[index] = (struct telemetry_pagein){ .tp_fsid = fsid, .tp_fsobj_id = fsobj_id, .tp_file_offset = file_offset, }; } lck_mtx_unlock(&_telemetry_pagein_mtx); } void telemetry_pagein_emit( void *objectv, mach_vm_offset_t mem_offset) { vm_object_t const object = objectv; memory_object_t pager = object->pager; if (memory_object_is_vnode_pager(pager)) { struct vnode *vp = vnode_pager_lookup_vnode(pager); if (vp != NULL) { unsigned int emit_index = 0; uint64_t tgid = 0; if (_pagein_get_emit_index(&emit_index, &tgid)) { off_t file_offset = object->paging_offset + mem_offset; _pagein_emit_internal(emit_index, tgid, vp, file_offset); } } } } int telemetry_pagein_read( unsigned int *size_out, user_addr_t destination, size_t destination_size) { if (destination == USER_ADDR_NULL || destination_size == 0) { /* * Handle user space requesting the size to allocate; more events * could come in by the time this is called again. */ uint64_t count = os_atomic_load(&_pagein_globals.tp_count, relaxed); *size_out = sizeof(struct telemetry_pagein_header) + sizeof(struct telemetry_pagein) * count; return 0; } struct telemetry_pagein_header hdr = { .tph_magic = TELEMETRY_PAGEIN_MAGIC, .tph_version = 1, }; if (destination_size < sizeof(hdr)) { return ENOSPC; } struct telemetry_pagein *buf = NULL; uint64_t count = 0; uint64_t capacity = 0; size_t buffer_size = 0; lck_mtx_lock(&_telemetry_pagein_mtx); _pagein_globals.tp_thread_group_id = 0; buf = _pagein_globals.tp_buffer; _pagein_globals.tp_buffer = NULL; count = os_atomic_load(&_pagein_globals.tp_count, relaxed); os_atomic_store(&_pagein_globals.tp_count, 0, relaxed); capacity = _pagein_globals.tp_capacity; _pagein_globals.tp_capacity = 0; buffer_size = _pagein_globals.tp_buffer_allocated_size; _pagein_globals.tp_buffer_allocated_size = 0; hdr.tph_duration_mct = mach_continuous_time() - _pagein_globals.tp_start_mct; _pagein_globals.tp_start_mct = 0; hdr.tph_pagein_count = count; hdr.tph_flags = _pagein_globals.tp_flags; lck_mtx_unlock(&_telemetry_pagein_mtx); int error = copyout(&hdr, destination, sizeof(hdr)); if (error) { goto out; } destination += sizeof(hdr); destination_size -= sizeof(hdr); unsigned int copyout_count = MIN(count, capacity); size_t pageins_size = MIN(destination_size, copyout_count * sizeof(struct telemetry_pagein)); *size_out = sizeof(hdr) + pageins_size; if (pageins_size > 0) { error = copyout(buf, destination, pageins_size); } out: kfree_data(buf, buffer_size); return error; } #else /* CONFIG_THREAD_GROUPS */ int telemetry_pagein_setup( __unused uint64_t buffer_size, __unused telemetry_pagein_flags_t flags) { return ENOTSUP; } void telemetry_pagein_start(__unused void *coal) { } void telemetry_pagein_emit( __unused void *vp, __unused mach_vm_offset_t file_offset) { } int telemetry_pagein_read( __unused unsigned int *count_out, __unused user_addr_t destination, __unused size_t destination_size) { return ENOTSUP; } #endif /* !CONFIG_THREAD_GROUPS */ #if CONFIG_MEMORY_MICROSTACKSHOT struct telemetry_memory_usage_cpu PERCPU_DATA(telemetry_memory_usage_percpu); TUNABLE_WRITEABLE(uint64_t, telemetry_vm_fault_period, "telemetry_mss_fault_period", 0); TUNABLE_WRITEABLE(uint64_t, telemetry_page_grab_period, "telemetry_mss_grab_period", 0); int telemetry_memory_usage_setup(uint64_t page_grab_period, uint64_t vm_fault_period) { telemetry_page_grab_period = page_grab_period; telemetry_vm_fault_period = vm_fault_period; return 0; } #else /* CONFIG_MEMORY_MICROSTACKSHOT */ int telemetry_memory_usage_setup(uint64_t __unused page_grab_period, uint64_t __unused vm_fault_period) { return ENOTSUP; } #endif /* !CONFIG_MEMORY_MICROSTACKSHOT */ #pragma mark - Trap Telemetry Utilities static int telemetry_backtrace_add_kernel( char *buf, size_t buflen) { int rc = 0; #if defined(__arm__) || defined(__arm64__) extern vm_offset_t segTEXTEXECB; extern unsigned long segSizeTEXTEXEC; vm_address_t unslid = segTEXTEXECB - vm_kernel_stext; rc += scnprintf(buf, buflen, "%s@%lx:%lx\n", kernel_uuid_string, unslid, unslid + segSizeTEXTEXEC - 1); #elif defined(__x86_64__) rc += scnprintf(buf, buflen, "%s@0:%lx\n", kernel_uuid_string, vm_kernel_etext - vm_kernel_stext); #else #pragma unused(buf, buflen) #endif return rc; } /** * Generate a backtrace string which can be symbolicated off system * * All addresses are relative to the vm_kernel_stext which means that all * offsets will be typically <= 50M which uses 7 hex digits. * * We allow up to TOT entries from FRAMES. The result will be formatted into BUF * (up to BUFLEN-1 characters) with the following format: * * <OFFSET1>\n * <OFFSET2>\n * ... * <UUID_a>@<TEXT_EXEC_BASE_OFFSET>:<TEXT_EXEC_END_OFFSET>\n * <UUID_b>@<TEXT_EXEC_BASE_OFFSET>:<TEXT_EXEC_END_OFFSET>\n * ... * * In general this backtrace takes 8 bytes per "frame", with an extra 52 bytes * per unique UUID referenced. As a rule of thumb, with a 256 byte long output * buffer, at least five entries from four unique UUIDs will generally fit. */ void telemetry_backtrace_to_string( char *buf, size_t buflen, uint32_t tot, uintptr_t *frames) { size_t l = 0; for (uint32_t i = 0; i < tot; i++) { l += scnprintf(buf + l, buflen - l, "%lx\n", frames[i] - vm_kernel_stext); } l += telemetry_backtrace_add_kernel(buf + l, buflen - l); telemetry_backtrace_add_kexts(buf + l, buflen - l, frames, tot); } CA_EVENT(os_fault_with_ustack, CA_STATIC_STRING(CA_PROCNAME_LEN), proc_name, CA_STATIC_STRING(CA_SIGNINGID_MAX_LEN), signing_id, CA_INT, namespace, CA_INT, reason_code, CA_STATIC_STRING(CA_UUID_LEN), shared_cache_uuid, CA_STATIC_STRING(CA_UUID_LEN), main_exec_uuid, CA_STATIC_STRING(CA_UUID_LEN), frame1UUID, CA_INT, frame1offset, CA_STATIC_STRING(CA_UUID_LEN), frame2UUID, CA_INT, frame2offset, CA_STATIC_STRING(CA_UUID_LEN), frame3UUID, CA_INT, frame3offset, CA_STATIC_STRING(CA_UUID_LEN), frame4UUID, CA_INT, frame4offset); static int compare_uuids32(const void *a, const void *b) { uint32_t addr_a = ((const struct user32_dyld_uuid_info*)a)->imageLoadAddress; uint32_t addr_b = ((const struct user32_dyld_uuid_info*)b)->imageLoadAddress; if (addr_a < addr_b) { return -1; } else if (addr_a == addr_b) { return 0; } else { return 1; } } static int compare_uuids64(const void *a, const void *b) { uint64_t addr_a = ((const struct user64_dyld_uuid_info*)a)->imageLoadAddress; uint64_t addr_b = ((const struct user64_dyld_uuid_info*)b)->imageLoadAddress; if (addr_a < addr_b) { return -1; } else if (addr_a == addr_b) { return 0; } else { return 1; } } #define OS_FAULT_NUMFRAMES (4) static uint64_t sr_virt_base(struct vm_shared_region *sr) { return sr == NULL ? 0 : sr->sr_base_address + sr->sr_first_mapping; } static size_t _normalize_offset32(struct _telemetry_uuids uuids, uintptr_t addr, uuid_string_t uuid_out, struct vm_shared_region *sr) { struct user32_dyld_uuid_info * elms = (struct user32_dyld_uuid_info*)uuids.uuid_info; struct user32_dyld_uuid_info *image = &elms[0]; for (int img_i = 1; img_i < uuids.uuid_info_count; ++img_i) { if (elms[img_i].imageLoadAddress > addr) { break; } image = &elms[img_i]; } if (addr < image->imageLoadAddress) { /* basic check if addr is not in the image * it also covers cases when PC addr == 0 */ return 0; } size_t res = addr - image->imageLoadAddress; if (sr && sr_virt_base(sr) == image->imageLoadAddress && res > sr->sr_size) { /* if shared cache and outside of range - return 0 */ res = 0; } else { /* if shared cache - must be within range */ uuid_unparse_upper(image->imageUUID, uuid_out); } return res; } static size_t _normalize_offset64(struct _telemetry_uuids uuids, uintptr_t addr, uuid_string_t uuid_out, struct vm_shared_region *sr) { struct user64_dyld_uuid_info * elms = (struct user64_dyld_uuid_info*)uuids.uuid_info; struct user64_dyld_uuid_info *image = &elms[0]; for (int img_i = 1; img_i < uuids.uuid_info_count; ++img_i) { if (elms[img_i].imageLoadAddress > addr) { break; } image = &elms[img_i]; } if (addr < image->imageLoadAddress) { /* basic check if addr is not in the image * it also covers cases when PC addr == 0 */ return 0; } size_t res = addr - image->imageLoadAddress; if (sr && sr_virt_base(sr) == image->imageLoadAddress && res > sr->sr_size) { /* if shared cache and outside of range - return 0 */ res = 0; } else { /* if shared cache - must be within range */ uuid_unparse_upper(image->imageUUID, uuid_out); } return res; } static inline void _extend_uuids_with_shared_cache( struct _telemetry_uuids *uuids, bool is_64bit, struct vm_shared_region *shared_region) { if (!shared_region) { return; } /* create an entry for shared cache in the images array, bc dyld doesn't keep it there */ if (is_64bit) { struct user64_dyld_uuid_info * elms = (struct user64_dyld_uuid_info*)uuids->uuid_info; elms[uuids->uuid_info_count].imageLoadAddress = sr_virt_base(shared_region); memcpy(elms[uuids->uuid_info_count].imageUUID, shared_region->sr_uuid, sizeof(uuid_t)); } else { struct user32_dyld_uuid_info * elms = (struct user32_dyld_uuid_info*)uuids->uuid_info; elms[uuids->uuid_info_count].imageLoadAddress = (uint32_t)sr_virt_base(shared_region); memcpy(elms[uuids->uuid_info_count].imageUUID, shared_region->sr_uuid, sizeof(uuid_t)); } ++uuids->uuid_info_count; } static struct _telemetry_uuids _alloc_uuid_only_for_sc(task_t task) { bool const user64_va = task_has_64Bit_addr(task); uint32_t uuid_info_size = 0; if (user64_va) { uuid_info_size = sizeof(struct user64_dyld_uuid_info); } else { uuid_info_size = sizeof(struct user32_dyld_uuid_info); } char *uuid_info_array = kalloc_data(uuid_info_size, Z_WAITOK); if (uuid_info_array == NULL) { return (struct _telemetry_uuids){ .error = ENOMEM, }; } /* allocate space for shared cache but do not fill it up yet * hence uuid_info_count == 0 */ return (struct _telemetry_uuids){ .uuid_info = uuid_info_array, .uuid_info_count = 0, .uuid_info_size = uuid_info_size, }; } extern const char *cs_identity_get(proc_t); void os_user_fault_send_ca_event(uint32_t reason_namespace, uint64_t reason_code) { proc_t proc = current_proc(); task_t task = current_task(); bool platform_binary = csproc_get_platform_binary(current_proc()) != 0; ca_event_t ca_event = CA_EVENT_ALLOCATE_FLAGS(os_fault_with_ustack, Z_WAITOK | Z_ZERO); if (ca_event == NULL) { goto out; } CA_EVENT_TYPE(os_fault_with_ustack) * event = ca_event->data; struct _telemetry_uuids uuids = { }; uuid_t uuid_parsed; struct vm_shared_region *shared_region = NULL; if (task->shared_region != NULL) { shared_region = task->shared_region; uuid_unparse_upper(shared_region->sr_uuid, event->shared_cache_uuid); } proc_getexecutableuuid(proc, uuid_parsed, sizeof(uuid_parsed)); uuid_unparse_upper(uuid_parsed, event->main_exec_uuid); const char* signing_id = platform_binary? cs_identity_get(current_proc()) : NULL; if (signing_id != NULL) { strlcpy(event->signing_id, signing_id, sizeof(event->signing_id)); } event->namespace = reason_namespace; event->reason_code = reason_code; const char* proc_name = proc_name_address(proc); strlcpy(event->proc_name, proc_name, sizeof(event->proc_name)); if (platform_binary) { /* sample uuids via dyld image info only for platform binaries */ uuids = _telemetry_sample_uuids(task, 1); } if (uuids.uuid_info == NULL || uuids.uuid_info_count == 0) { uuids = _alloc_uuid_only_for_sc(task); } if (uuids.error != 0) { /* ignore telemetry */ return; } assert(uuids.uuid_info != NULL); assert(uuids.uuid_info_size > 0); _extend_uuids_with_shared_cache(&uuids, task_has_64Bit_addr(task), shared_region); /* need to sort before we can start searching */ size_t elm_size = task_has_64Bit_addr(task) ? sizeof(struct user64_dyld_uuid_info) : sizeof(struct user32_dyld_uuid_info); cmpfunc_t comparator = task_has_64Bit_addr(task) ? compare_uuids64 : compare_uuids32; qsort(uuids.uuid_info, uuids.uuid_info_count, elm_size, comparator); /* walk top 4 frames in the backtrace and find corresponding UUID of the image */ uintptr_t user_frames[OS_FAULT_NUMFRAMES] = {}; struct backtrace_user_info btinfo = BTUINFO_INIT; unsigned int frame_count = backtrace_user(user_frames, OS_FAULT_NUMFRAMES, NULL, &btinfo); assert(frame_count <= OS_FAULT_NUMFRAMES); if (btinfo.btui_error != 0) { goto out; } if (frame_count > 0) { event->frame1offset = task_has_64Bit_addr(task) ? _normalize_offset64(uuids, user_frames[0], event->frame1UUID, shared_region): _normalize_offset32(uuids, user_frames[0], event->frame1UUID, shared_region); } else { #if __arm64__ /* [arm64 only] fault path: if we failed to collect the backtrace - try to symbolicate PC at least */ struct arm_saved_state *sstate = find_user_regs(current_thread()); if (is_saved_state64(sstate)) { struct arm_saved_state64 *state = saved_state64(sstate); assert(task_has_64Bit_addr(task)); event->frame1offset = _normalize_offset64(uuids, state->pc, event->frame1UUID, shared_region); } else { struct arm_saved_state32 *state = saved_state32(sstate); event->frame1offset = _normalize_offset32(uuids, state->pc, event->frame1UUID, shared_region); } #endif /* #if __arm64__ */ } if (frame_count > 1) { event->frame2offset = task_has_64Bit_addr(task) ? _normalize_offset64(uuids, user_frames[1], event->frame2UUID, shared_region): _normalize_offset32(uuids, user_frames[1], event->frame2UUID, shared_region); } else { #if __arm64__ /* [arm64 only] fault path: if we failed to collect the backtrace - try to symbolicate LR at least */ struct arm_saved_state *sstate = find_user_regs(current_thread()); if (is_saved_state64(sstate)) { struct arm_saved_state64 *state = saved_state64(sstate); assert(task_has_64Bit_addr(task)); event->frame2offset = _normalize_offset64(uuids, state->lr, event->frame2UUID, shared_region); } else { struct arm_saved_state32 *state = saved_state32(sstate); event->frame2offset = _normalize_offset32(uuids, state->lr, event->frame2UUID, shared_region); } #endif /* #if __arm64__ */ } if (frame_count > 2) { event->frame3offset = task_has_64Bit_addr(task) ? _normalize_offset64(uuids, user_frames[2], event->frame3UUID, shared_region): _normalize_offset32(uuids, user_frames[2], event->frame3UUID, shared_region); } if (frame_count > 3) { event->frame4offset = task_has_64Bit_addr(task) ? _normalize_offset64(uuids, user_frames[3], event->frame4UUID, shared_region): _normalize_offset32(uuids, user_frames[3], event->frame4UUID, shared_region); } if (ca_event) { CA_EVENT_SEND(ca_event); ca_event = NULL; /* is consumed by send above */ } out: if (uuids.uuid_info != NULL) { kfree_data(uuids.uuid_info, uuids.uuid_info_size); } if (ca_event != NULL) { CA_EVENT_DEALLOCATE(ca_event); } } |