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
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
/*
 * Copyright (c) 2006-2019 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 <kern/sched_prim.h>
#include <kern/kalloc.h>
#include <kern/assert.h>
#include <kern/debug.h>
#include <kern/locks.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <kern/host.h>
#include <kern/policy_internal.h>
#include <kern/thread_group.h>
#include <kern/zalloc.h>

#include <corpses/task_corpse.h>
#include <libkern/libkern.h>
#include <mach/mach_time.h>
#include <mach/task.h>
#include <mach/host_priv.h>
#include <mach/mach_host.h>
#include <pexpert/pexpert.h>
#include <sys/coalition.h>
#include <sys/code_signing.h>
#include <sys/kern_event.h>
#include <sys/proc.h>
#include <sys/proc_info.h>
#include <sys/reason.h>
#include <sys/signal.h>
#include <sys/signalvar.h>
#include <sys/sysctl.h>
#include <sys/sysproto.h>
#include <sys/spawn_internal.h>
#include <sys/wait.h>
#include <sys/tree.h>
#include <sys/priv.h>
#include <vm/pmap.h>
#include <vm/vm_reclaim_xnu.h>
#include <vm/vm_pageout_xnu.h>
#include <vm/vm_protos.h>
#include <vm/vm_purgeable_xnu.h>
#include <vm/vm_page.h>
#include <vm/vm_compressor_xnu.h>
#include <mach/machine/sdt.h>
#include <libkern/section_keywords.h>
#include <os/atomic_private.h>
#include <os/overflow.h>
#include <mach/mach_time.h>

#include <IOKit/IOBSD.h>

#if CONFIG_MACF
#include <security/mac_framework.h>
#endif

#if CONFIG_FREEZE
#include <vm/vm_map.h>
#endif /* CONFIG_FREEZE */

#include <kern/kern_memorystatus_internal.h>
#include <sys/kern_memorystatus.h>
#include <sys/kern_memorystatus_xnu.h>
#include <sys/kern_memorystatus_freeze.h>
#include <sys/kern_memorystatus_notify.h>
#include <sys/kdebug_triage.h>
#include <sys/file_internal.h>
#include <net/necp.h>

errno_t mach_to_bsd_errno(kern_return_t mach_err);
extern uint32_t vm_compressor_pool_size(void);
extern uint32_t vm_compressor_fragmentation_level(void);

extern boolean_t memorystatus_freeze_daemons_all_allowed;

int block_corpses = 0; /* counter to block new corpses if jetsam purges them */

/* For logging clarity */
static const char *memstat_kill_cause_name[] = {
	"",                                             /* kMemorystatusInvalid							*/
	"jettisoned",                                   /* kMemorystatusKilled							*/
	"highwater",                                    /* kMemorystatusKilledHiwat						*/
	"vnode-limit",                                  /* kMemorystatusKilledVnodes					*/
	"vm-pageshortage",                              /* kMemorystatusKilledVMPageShortage			*/
	"proc-thrashing",                               /* kMemorystatusKilledProcThrashing				*/
	"fc-thrashing",                                 /* kMemorystatusKilledFCThrashing				*/
	"per-process-limit",                            /* kMemorystatusKilledPerProcessLimit			*/
	"disk-space-shortage",                          /* kMemorystatusKilledDiskSpaceShortage			*/
	"idle-exit",                                    /* kMemorystatusKilledIdleExit					*/
	"zone-map-exhaustion",                          /* kMemorystatusKilledZoneMapExhaustion			*/
	"vm-compressor-thrashing",                      /* kMemorystatusKilledVMCompressorThrashing		*/
	"vm-compressor-space-shortage",                 /* kMemorystatusKilledVMCompressorSpaceShortage	*/
	"low-swap",                                     /* kMemorystatusKilledLowSwap                   */
	"sustained-memory-pressure",                    /* kMemorystatusKilledSustainedPressure         */
	"vm-pageout-starvation",                        /* kMemorystatusKilledVMPageoutStarvation       */
	"conclave-limit",                               /* kMemorystatusKilledConclaveLimit             */
	"long-idle-exit",                               /* kMemorystatusKilledLongIdleExit				*/
};

static const char *
memorystatus_priority_band_name(int32_t priority)
{
	switch (priority) {
	case JETSAM_PRIORITY_FOREGROUND:
		return "FOREGROUND";
	case JETSAM_PRIORITY_AUDIO_AND_ACCESSORY:
		return "AUDIO_AND_ACCESSORY";
	case JETSAM_PRIORITY_CONDUCTOR:
		return "CONDUCTOR";
	case JETSAM_PRIORITY_DRIVER_APPLE:
		return "DRIVER_APPLE";
	case JETSAM_PRIORITY_HOME:
		return "HOME";
	case JETSAM_PRIORITY_EXECUTIVE:
		return "EXECUTIVE";
	case JETSAM_PRIORITY_IMPORTANT:
		return "IMPORTANT";
	case JETSAM_PRIORITY_CRITICAL:
		return "CRITICAL";
	}

	return "?";
}

bool
is_reason_thrashing(unsigned cause)
{
	switch (cause) {
	case kMemorystatusKilledFCThrashing:
	case kMemorystatusKilledVMCompressorThrashing:
	case kMemorystatusKilledVMCompressorSpaceShortage:
		return true;
	default:
		return false;
	}
}

bool
is_reason_zone_map_exhaustion(unsigned cause)
{
	return cause == kMemorystatusKilledZoneMapExhaustion;
}

/*
 * Returns the current zone map size and capacity to include in the jetsam snapshot.
 * Defined in zalloc.c
 */
extern void get_zone_map_size(uint64_t *current_size, uint64_t *capacity);

/*
 * Returns the name of the largest zone and its size to include in the jetsam snapshot.
 * Defined in zalloc.c
 */
extern void get_largest_zone_info(char *zone_name, size_t zone_name_len, uint64_t *zone_size);

static int memstat_set_memlimits_locked(proc_t p, int32_t active_limit,
    int32_t inactive_limit, memlimit_options_t options);
static bool memstat_proc_is_active_locked(proc_t);

static int memorystatus_highwater_enabled = 1;  /* Update the cached memlimit data. */

/*
 * Cache this proc's active limit as its current limit before writing it to
 * the ledger. Returns whether the new limit should be written to the ledger.
 */
static inline bool
memstat_update_memlimit_locked(proc_t p, bool use_active)
{
	bool ledger_needed = false;
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	/* Cache limit value */
	if (use_active && p->p_memstat_memlimit != p->p_memstat_memlimit_active) {
		p->p_memstat_memlimit = p->p_memstat_memlimit_active;
		ledger_needed = true;
	} else if (!use_active &&
	    p->p_memstat_memlimit != p->p_memstat_memlimit_inactive) {
		p->p_memstat_memlimit = p->p_memstat_memlimit_inactive;
		ledger_needed = true;
	}

	/* Cache limit fatality */
	if (_memstat_proc_memlimit_is_fatal(p, use_active) &&
	    !_memstat_proc_cached_memlimit_is_fatal(p)) {
		p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;
		ledger_needed = true;
	} else if (!_memstat_proc_memlimit_is_fatal(p, use_active) &&
	    _memstat_proc_cached_memlimit_is_fatal(p)) {
		p->p_memstat_state &= ~P_MEMSTAT_FATAL_MEMLIMIT;
		ledger_needed = true;
	}

	return ledger_needed;
}

/*
 * Write the process' current memlimit to the ledger for enforcement.
 *
 * Holding the proc_list_lock while writing to the ledgers (where the task
 * lock is taken) can be problematic.  The proc list lock may optionally be
 * dropped and re-taken while writing limits to the ledger. (rdar://21394491)
 */
static int
_memstat_write_memlimit_to_ledger_locked(proc_t p, bool is_active, bool drop_lock)
{
	kern_return_t kr;
	bool is_fatal = _memstat_proc_cached_memlimit_is_fatal(p);

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

#if MACH_ASSERT
	if (memorystatus_highwater_enabled) {
		if (is_active) {
			assert3u(is_fatal, ==, _memstat_proc_active_memlimit_is_fatal(p));
			assert3u(p->p_memstat_memlimit, ==, p->p_memstat_memlimit_active);
		} else {
			assert3u(is_fatal, ==, _memstat_proc_inactive_memlimit_is_fatal(p));
			assert3u(p->p_memstat_memlimit, ==, p->p_memstat_memlimit_inactive);
		}
	}
#endif /* MACH_ASSERT */

	if (drop_lock) {
		if (proc_ref(p, true) != p) {
			memorystatus_log_error("Unable to take a reference on proc %s [%d]. "
			    "Cannot update memlimit", proc_best_name(p), proc_getpid(p));
			return ESRCH;
		}
		proc_list_unlock();
	}

	memorystatus_log_debug("memorystatus: new limit on pid %d (%dMB %s)\n",
	    proc_getpid(p), (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1),
	    (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"));

	kr = task_set_phys_footprint_limit_internal(proc_task(p),
	    (p->p_memstat_memlimit > 0) ? p->p_memstat_memlimit : -1,
	    NULL, is_active, is_fatal);

	if (drop_lock) {
		proc_list_lock();
		proc_rele(p);
	}

	if (kr != KERN_SUCCESS) {
		memorystatus_log_fault("memorystatus: error (%d) setting memlimit in "
		    "ledger for %s [%d]\n", kr, proc_best_name(p), proc_pid(p));
		return mach_to_bsd_errno(kr);
	}
	return 0;
}

#pragma mark General Tunables

#define MEMORYSTATUS_SMALL_MEMORY_THRESHOLD (3UL * (1UL << 30))
#define MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD (6UL * (1UL << 30))

#define MEMORYSTATUS_CLEAR_THE_DECKS_OFFSET_PERCENTAGE 5UL
#define MEMORYSTATUS_BALLAST_OFFSET_PERCENTAGE 5UL
#define MEMORYSTATUS_AGGR_SYSPROC_AGING_PERCENTAGE 7UL
#define MEMORYSTATUS_DELTA_PERCENTAGE_LARGE 4UL
#define MEMORYSTATUS_DELTA_PERCENTAGE_SMALL 5UL

/*
 * Fall back to these percentages/ratios if a mb value is not provided via EDT
 *  DRAM (GB) | critical | idle | pressure | reaper | freeze
 *  (0,3]     | 5%       | 10%  | 15%      | 20%    | 50%
 *  (3,6]     | 4%       | 9%   | 15%      | 18%    | 50%
 *  (6,∞)     | 4%       | 8%   | 12%      | 16%    | 50%
 */

#define MEMORYSTATUS_CRITICAL_PERCENTAGE_SMALL 5UL
#define MEMORYSTATUS_CRITICAL_PERCENTAGE_LARGE 4UL

#define MEMORYSTATUS_IDLE_RATIO_NUM 2UL
#define MEMORYSTATUS_IDLE_RATIO_DENOM 1UL
#define MEMORYSTATUS_PRESSURE_RATIO_NUM 3UL
#define MEMORYSTATUS_PRESSURE_RATIO_DENOM 1UL
#define MEMORYSTATUS_REAPER_RATIO_NUM 4UL
#define MEMORYSTATUS_REAPER_RATIO_DENOM 1UL

#if (XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR) || XNU_TARGET_OS_WATCH
#define MEMORYSTATUS_REAPER_ENABLED_DEFAULT TRUE
#else
#define MEMORYSTATUS_REAPER_ENABLED_DEFAULT FALSE
#endif /* (XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR) || XNU_TARGET_OS_WATCH */
#define MEMORYSTATUS_REAPER_MIN_AGE_SECS_DEFAULT 300
#define MEMORYSTATUS_REAPER_MIN_AGE_APPS_SECS_DEFAULT 300
#define MEMORYSTATUS_REAPER_MAX_PRIORITY_DEFAULT JETSAM_PRIORITY_IDLE
#define MEMORYSTATUS_REAPER_RESCAN_SECS_DEFAULT 30
#define MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT -1

#define MEMORYSTATUS_REAPER_REAP_RELAUNCH_MASK_UNKNOWN (P_MEMSTAT_RELAUNCH_HIGH << 1)
#define MEMORYSTATUS_REAPER_REAP_RELAUNCH_MASK_DEFAULT (P_MEMSTAT_RELAUNCH_LOW | MEMORYSTATUS_REAPER_REAP_RELAUNCH_MASK_UNKNOWN)

/*
 * For historical reasons, devices with "medium"-sized memory configs have a critical:idle:pressure ratio of
 * 4:9:15. This ratio is preserved for these devices when a fixed-mb base value has not been provided by EDT/boot-arg;
 * all other devices use a 1:2:3 ratio.
 */
#define MEMORYSTATUS_IDLE_RATIO_NUM_MEDIUM 9UL
#define MEMORYSTATUS_IDLE_RATIO_DENOM_MEDIUM 4UL
#define MEMORYSTATUS_PRESSURE_RATIO_NUM_MEDIUM  15UL
#define MEMORYSTATUS_PRESSURE_RATIO_DENOM_MEDIUM  4UL
#define MEMORYSTATUS_REAPER_RATIO_NUM_MEDIUM 20UL
#define MEMORYSTATUS_REAPER_RATIO_DENOM_MEDIUM 4UL
#define MEMORYSTATUS_REAPER_MIN_AGE_SECS_DEFAULT_MEDIUM 240
#define MEMORYSTATUS_REAPER_MIN_AGE_APPS_SECS_DEFAULT_MEDIUM 240

/*
 * For Large config device, set the reaper threhsold to be 19% of the Memsize
 */
#define MEMORYSTATUS_REAPER_RATIO_NUM_LARGE 19UL
#define MEMORYSTATUS_REAPER_RATIO_DENOM_LARGE 4UL

static int32_t memorystatus_get_default_task_active_limit(proc_t p);
static int32_t memorystatus_get_default_task_inactive_limit(proc_t p);

/*
 * default jetsam snapshot support
 */
memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot;

#if CONFIG_FREEZE
memorystatus_jetsam_snapshot_t *memorystatus_jetsam_snapshot_freezer;
/*
 * The size of the freezer snapshot is given by memorystatus_jetsam_snapshot_max / JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR
 * The freezer snapshot can be much smaller than the default snapshot
 * because it only includes apps that have been killed and dasd consumes it every 30 minutes.
 * Since the snapshots are always wired we don't want to overallocate too much.
 */
#define JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR 20
unsigned int memorystatus_jetsam_snapshot_freezer_max;
unsigned int memorystatus_jetsam_snapshot_freezer_size;
TUNABLE(bool, memorystatus_jetsam_use_freezer_snapshot, "kern.jetsam_user_freezer_snapshot", true);

#define MEMORYSTATUS_FREEZE_THRESHOLD_PERCENTAGE 50UL
TUNABLE_DT(uint32_t, memorystatus_freeze_threshold_mb, "/defaults", "kern.memstat_freeze_mb",
    "memorystatus_freeze_threshold_mb", 0, TUNABLE_DT_NONE);
#endif /* CONFIG_FREEZE */

unsigned int memorystatus_jetsam_snapshot_count = 0;
unsigned int memorystatus_jetsam_snapshot_max = 0;
unsigned int memorystatus_jetsam_snapshot_size = 0;
uint64_t memorystatus_jetsam_snapshot_last_timestamp = 0;
uint64_t memorystatus_jetsam_snapshot_timeout = 0;

#define JETSAM_SNAPSHOT_TIMEOUT_SECS 30

#if DEVELOPMENT || DEBUG
/*
 * On development and debug kernels, we allow one pid to take ownership
 * of some memorystatus data structures for testing purposes (via memorystatus_control).
 * If there's an owner, then only they may consume the jetsam snapshot & set freezer probabilities.
 * This is used when testing these interface to avoid racing with other
 * processes on the system that typically use them (namely OSAnalytics & dasd).
 */
static pid_t memorystatus_testing_pid = 0;
SYSCTL_INT(_kern, OID_AUTO, memorystatus_testing_pid, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_testing_pid, 0, "");
#endif /* DEVELOPMENT || DEBUG */

/*
 * jetsam zprint snapshot data
 */
#if JETSAM_ZPRINT_SNAPSHOT
static unsigned int        jzs_trigger_band = JETSAM_PRIORITY_FOREGROUND;
static mach_zone_name_t    *jzs_names = NULL;
static mach_zone_info_t    *jzs_info = NULL;
static int                *jzs_coalesce = NULL;
static unsigned int        jzs_zone_cnt = 0;
static mach_memory_info_t *jzs_meminfo = NULL;
static unsigned int        jzs_meminfo_cnt = 0;
static uint64_t            jzs_gencount = (uint64_t) -1ll;

#if DEVELOPMENT || DEBUG
SYSCTL_UINT(_kern, OID_AUTO, jzs_trigger_band, CTLFLAG_RW | CTLFLAG_LOCKED, &jzs_trigger_band, 0, "Priority band threshold for taking jetsam zprint snapshot");
#endif /* DEVELOPMENT || DEBUG */
#endif /* JETSAM_ZPRINT_SNAPSHOT */


static void memorystatus_init_jetsam_snapshot_header(memorystatus_jetsam_snapshot_t *snapshot);

/* General memorystatus stuff */

/*
 * Daemons: The actual idle deferred time for the daemon is based on
 * the relaunch behavior of the daemon. The relaunch behavior determines
 * the scaling factor applied to memorystatus_sysprocs_idle_delay_time. See
 * kJetsamSysProcsIdleDelayTime* ratios defined in kern_memorystatus.c
 *
 * Apps: The apps are aged for memorystatus_apps_idle_delay_time factored
 * by kJetsamAppsIdleDelayTimeRatio.
 */
TUNABLE(uint64_t, memstat_idle_deferral_time_s, "memorystatus_idle_deferral_time_s", 10);
TUNABLE(uint64_t, memstat_aging_stuck_time_s, "memorystatus_aging_stuck_time_s", 30);
uint64_t memorystatus_sysprocs_idle_delay_time = 0;
uint64_t memorystatus_apps_idle_delay_time = 0;
uint64_t memorystatus_aging_stuck_delay_time = 0;
/* 2GB devices support an entitlement for a higher app memory limit of "almost 2GB". */
static int32_t memorystatus_ios13extended_footprint_limit_mb = 1800;

#define CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT XNU_TARGET_OS_XR

/* Some devices give entitled apps a higher memory limit */
TUNABLE_DT_WRITEABLE(int32_t, memorystatus_entitled_max_task_footprint_mb,
    "/defaults", "kern.entitled_max_task_pmem",
    "entitled_max_task_pmem", 0, TUNABLE_DT_NONE);
TUNABLE_DT_WRITEABLE(int32_t, memorystatus_entitled_dev_max_task_footprint_mb,
    "/defaults", "kern.entitled_dev_max_task_pmem",
    "entitled_dev_max_task_pmem", 0, TUNABLE_DT_NONE);
#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
/* On visionOS, we want a separate high memory limit for bincompat (iOS) apps. */
TUNABLE_DT_WRITEABLE(int32_t, memorystatus_entitled_bincompat_max_task_footprint_mb,
    "/defaults", "kern.entitled_bc_max_task_pmem",
    "entitled_bincompat_max_task_pmem", 0, TUNABLE_DT_NONE);
#endif // CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
#if DEBUG || DEVELOPMENT
TUNABLE(bool, memstat_ignore_task_limit_increase, "memstat_no_task_limit_increase", false);
#endif /* DEBUG || DEVELOPMENT */

#if __arm64__
#if DEVELOPMENT || DEBUG
SYSCTL_INT(_kern, OID_AUTO, ios13extended_footprint_limit_mb,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &memorystatus_ios13extended_footprint_limit_mb, 0, "");
SYSCTL_INT(_kern, OID_AUTO, entitled_max_task_pmem,
    CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
    &memorystatus_entitled_max_task_footprint_mb, 0, "");
SYSCTL_INT(_kern, OID_AUTO, entitled_dev_max_task_pmem,
    CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
    &memorystatus_entitled_dev_max_task_footprint_mb, 0, "");
#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
SYSCTL_INT(_kern, OID_AUTO, entitled_bincompat_max_task_pmem,
    CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_KERN,
    &memorystatus_entitled_bincompat_max_task_footprint_mb, 0, "");
#endif /* CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT */
#else /* !(DEVELOPMENT || DEBUG) */
SYSCTL_INT(_kern, OID_AUTO, entitled_max_task_pmem,
    CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN,
    &memorystatus_entitled_max_task_footprint_mb, 0, "");
#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
SYSCTL_INT(_kern, OID_AUTO, entitled_bincompat_max_task_pmem,
    CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_MASKED | CTLFLAG_KERN,
    &memorystatus_entitled_bincompat_max_task_footprint_mb, 0, "");
#endif /* CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT */
#endif /* DEVELOPMENT || DEBUG */
#endif /* __arm64__ */

#pragma mark Logging

os_log_t memorystatus_log_handle;

TUNABLE_WRITEABLE(memorystatus_log_level_t, memorystatus_log_level, "memorystatus_log_level", MEMORYSTATUS_LOG_LEVEL_DEFAULT);

#if DEBUG || DEVELOPMENT
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_log_level, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_log_level, MEMORYSTATUS_LOG_LEVEL_DEFAULT, "");
#endif

#pragma mark Locks

static LCK_GRP_DECLARE(memorystatus_lock_group, "memorystatus");

/* Synchronizes jetsam pressure broadcasts */
LCK_MTX_DECLARE(memorystatus_jetsam_broadcast_lock, &memorystatus_lock_group);

#if DEVELOPMENT || DEBUG
static LCK_MTX_DECLARE(disconnect_page_mappings_mutex, &memorystatus_lock_group);
#endif /* DEVELOPMENT || DEBUG */

/* Idle guard handling */

static void memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2);
static void _memstat_invalidate_idle_demotion_locked(proc_t p);
static void _memstat_schedule_idle_demotion_locked(proc_t p);
static void _memstat_reschedule_idle_demotion_locked(void);
int memorystatus_update_priority_for_appnap(proc_t p);
vm_pressure_level_t convert_internal_pressure_level_to_dispatch_level(vm_pressure_level_t);
boolean_t is_knote_registered_modify_task_pressure_bits(struct knote*, int, task_t, vm_pressure_level_t, vm_pressure_level_t);
void memorystatus_klist_reset_all_for_level(vm_pressure_level_t pressure_level_to_clear);
void memorystatus_send_low_swap_note(void);
boolean_t memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count,
    uint32_t *errors, uint64_t *memory_reclaimed);
static bool memorystatus_kill_proc(proc_t p, uint32_t cause,
    os_reason_t jetsam_reason, bool *killed, uint64_t *footprint_of_killed_proc);
/* Synchronously kill a process in priority order */
static bool memstat_kill_top_process(uint32_t cause, os_reason_t jetsam_reason,
    int32_t max_priority, memstat_kill_options_t options,
    int32_t *priority, uint32_t *errors, uint64_t *memory_reclaimed);

uint64_t memorystatus_available_memory_internal(proc_t p);
void memorystatus_thread_wake(void);
static bool _memstat_consider_waking_jetsam_thread(void);
#if CONFIG_JETSAM
static void memorystatus_thread_pool_default(void);
static void memorystatus_thread_pool_max(void);
#endif /* CONFIG_JETSAM */

unsigned int memorystatus_level = 0;
static int memorystatus_list_count = 0;
memstat_bucket_t memstat_bucket[MEMSTAT_BUCKET_COUNT];
static thread_call_t memorystatus_idle_demotion_call;
uint64_t memstat_idle_demotion_deadline = 0;
#if CONFIG_FREEZE
unsigned int memorystatus_suspended_count = 0;
#endif /* CONFIG_FREEZE */

#if XNU_TARGET_OS_OSX
/*
 * Effectively disable the system process and application demotion
 * logic on macOS. This means system processes and apps won't get the
 * 10 second protection before landing in the IDLE band after moving
 * out of their active band. Reasons:-
 * - daemons + extensions + apps on macOS don't behave the way they
 *   do on iOS and so they are confusing the demotion logic. For example,
 *   not all apps go from FG to IDLE. Some sit in higher bands instead. This
 *   is causing multiple asserts to fire internally.
 * - we use the aging bands to protect processes from jetsam. But on macOS,
 *   we have a very limited jetsam that is only invoked under extreme conditions
 *   where we have no more swap / compressor space OR are under critical pressure.
 */
int system_procs_aging_band = 0;
int system_procs_aging_band_stuck = 0;
int applications_aging_band = 0;
#else /* XNU_TARGET_OS_OSX */
int system_procs_aging_band = JETSAM_PRIORITY_AGING_BAND1;
int system_procs_aging_band_stuck = JETSAM_PRIORITY_AGING_BAND1_STUCK;
int applications_aging_band = JETSAM_PRIORITY_AGING_BAND2;
#endif /* XNU_TARGET_OS_OSX */

/* the jetsam band which will contain P_MEMSTAT_FROZEN processes */
#if CONFIG_FREEZE
int memorystatus_freeze_jetsam_band = JETSAM_PRIORITY_FREEZER;
#else /* CONFIG_FREEZE */
int memorystatus_freeze_jetsam_band = 0;
#endif /* CONFIG_FREEZE */

_Atomic bool memorystatus_zone_map_is_exhausted = false;
_Atomic bool memorystatus_compressor_space_shortage = false;
_Atomic bool memorystatus_pageout_starved = false;
#if CONFIG_PHANTOM_CACHE
_Atomic bool memorystatus_phantom_cache_pressure = false;
#endif /* CONFIG_PHANTOM_CACHE */

bool memorystatus_should_issue_fg_band_notify = true;

extern void coalition_mark_swappable(coalition_t coal);
extern bool coalition_is_swappable(coalition_t coal);
boolean_t memorystatus_allowed_vm_map_fork(task_t, bool *);
#if DEVELOPMENT || DEBUG
void memorystatus_abort_vm_map_fork(task_t);
#endif

SYSCTL_NODE(_kern, OID_AUTO, memorystatus,
    CTLFLAG_RW | CTLFLAG_LOCKED, 0, "memorystatus subsystem");

/*
 * Idle delay timeout factors for daemons based on relaunch behavior. Only used in
 * kJetsamAgingPolicySysProcsReclaimedFirst aging policy.
 */
#define kJetsamSysProcsIdleDelayTimeLowRatio    (5)
#define kJetsamSysProcsIdleDelayTimeMedRatio    (2)
#define kJetsamSysProcsIdleDelayTimeHighRatio   (1)

/*
 * For the kJetsamAgingPolicySysProcsReclaimedFirst aging policy, treat apps as well
 * behaved daemons for aging purposes.
 */
#define kJetsamAppsIdleDelayTimeRatio   (kJetsamSysProcsIdleDelayTimeLowRatio)

static uint64_t
memorystatus_sysprocs_idle_time(proc_t p)
{
	uint64_t idle_delay_time = 0;

	/*
	 * If the proc is brand-new (i.e. has not yet marked itself dirty),
	 * always provide the full idle delay time
	 */
	if (p->p_memstat_dirty & P_DIRTY_IM_NEW_HERE) {
		return memorystatus_sysprocs_idle_delay_time;
	}

	/*
	 * For system processes, base the idle delay time on the
	 * jetsam relaunch behavior specified by launchd. The idea
	 * is to provide extra protection to the daemons which would
	 * relaunch immediately after jetsam.
	 */
	switch (p->p_memstat_relaunch_flags) {
	case P_MEMSTAT_RELAUNCH_UNKNOWN:
	case P_MEMSTAT_RELAUNCH_LOW:
		idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeLowRatio;
		break;
	case P_MEMSTAT_RELAUNCH_MED:
		idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeMedRatio;
		break;
	case P_MEMSTAT_RELAUNCH_HIGH:
		idle_delay_time = memorystatus_sysprocs_idle_delay_time / kJetsamSysProcsIdleDelayTimeHighRatio;
		break;
	default:
		panic("Unknown relaunch flags on process!");
		break;
	}
	return idle_delay_time;
}

static uint64_t
memorystatus_apps_idle_time(__unused proc_t p)
{
	return memorystatus_apps_idle_delay_time / kJetsamAppsIdleDelayTimeRatio;
}

static uint64_t
_memstat_sysprocs_aging_stuck_delay_time(__unused proc_t p)
{
	return memorystatus_aging_stuck_delay_time;
}


static int
sysctl_jetsam_set_sysprocs_idle_delay_time SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)

	int error = 0, val = 0, old_time_in_secs = 0;
	uint64_t old_time_in_ns = 0;

	absolutetime_to_nanoseconds(memorystatus_sysprocs_idle_delay_time, &old_time_in_ns);
	old_time_in_secs = (int) (old_time_in_ns / NSEC_PER_SEC);

	error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL);
	if (error || !req->newptr) {
		return error;
	}

	if ((val < 0) || (val > INT32_MAX)) {
		memorystatus_log_error("jetsam: new idle delay interval has invalid value.\n");
		return EINVAL;
	}

	nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time);

	return 0;
}

SYSCTL_PROC(_kern_memorystatus, OID_AUTO, sysprocs_idle_delay_time_ns, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED,
    0, 0, sysctl_jetsam_set_sysprocs_idle_delay_time, "I", "Aging window for system processes");


static int
sysctl_jetsam_set_apps_idle_delay_time SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg1, arg2)

	int error = 0, val = 0, old_time_in_secs = 0;
	uint64_t old_time_in_ns = 0;

	absolutetime_to_nanoseconds(memorystatus_apps_idle_delay_time, &old_time_in_ns);
	old_time_in_secs = (int) (old_time_in_ns / NSEC_PER_SEC);

	error = sysctl_io_number(req, old_time_in_secs, sizeof(int), &val, NULL);
	if (error || !req->newptr) {
		return error;
	}

	if ((val < 0) || (val > INT32_MAX)) {
		memorystatus_log_error("jetsam: new idle delay interval has invalid value.\n");
		return EINVAL;
	}

	nanoseconds_to_absolutetime((uint64_t)val * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time);

	return 0;
}

SYSCTL_PROC(_kern_memorystatus, OID_AUTO, apps_idle_delay_time_ns, CTLTYPE_INT | CTLFLAG_RW,
    0, 0, sysctl_jetsam_set_apps_idle_delay_time, "I", "Aging window for applications");

SYSCTL_INT(_kern, OID_AUTO, max_task_pmem, CTLFLAG_RD | CTLFLAG_LOCKED | CTLFLAG_KERN, &max_task_footprint_mb, 0, "");

#if __arm64__
int legacy_footprint_bonus_mb = 50; /* This value was chosen after looking at the top 30 apps
                                     * that needed the additional room in their footprint when
                                     * the 'correct' accounting methods were applied to them.
                                     */

#if DEVELOPMENT || DEBUG
SYSCTL_INT(_kern, OID_AUTO, legacy_footprint_bonus_mb, CTLFLAG_RW | CTLFLAG_LOCKED, &legacy_footprint_bonus_mb, 0, "");
#endif /* DEVELOPMENT || DEBUG */
/*
 * Raise the inactive and active memory limits to new values.
 * Will only raise the limits and will do nothing if either of the current
 * limits are 0.
 * Caller must hold the proc_list_lock
 */
static void
memorystatus_raise_memlimit_locked(proc_t p,
    int new_memlimit_active,
    int new_memlimit_inactive)
{
	int memlimit_mb_active = 0, memlimit_mb_inactive = 0;
	memlimit_options_t memlimit_options = MEMLIMIT_OPTIONS_NONE;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	if (p->p_memstat_memlimit_active > 0) {
		memlimit_mb_active = p->p_memstat_memlimit_active;
	} else if (p->p_memstat_memlimit_active == -1) {
		memlimit_mb_active = max_task_footprint_mb;
	} else {
		/*
		 * Nothing to do for '0' which is
		 * a special value only used internally
		 * to test 'no limits'.
		 */
		return;
	}

	if (p->p_memstat_memlimit_inactive > 0) {
		memlimit_mb_inactive = p->p_memstat_memlimit_inactive;
	} else if (p->p_memstat_memlimit_inactive == -1) {
		memlimit_mb_inactive = max_task_footprint_mb;
	} else {
		/*
		 * Nothing to do for '0' which is
		 * a special value only used internally
		 * to test 'no limits'.
		 */
		return;
	}

	memlimit_mb_active = MAX(new_memlimit_active, memlimit_mb_active);
	memlimit_mb_inactive = MAX(new_memlimit_inactive, memlimit_mb_inactive);

	/* Maintain pre-existing limit fatality */
	if (_memstat_proc_active_memlimit_is_fatal(p)) {
		memlimit_options |= MEMLIMIT_ACTIVE_FATAL;
	}
	if (_memstat_proc_inactive_memlimit_is_fatal(p)) {
		memlimit_options |= MEMLIMIT_INACTIVE_FATAL;
	}

	memstat_set_memlimits_locked(p, memlimit_mb_active,
	    memlimit_mb_inactive, memlimit_options);
}

void
memorystatus_act_on_legacy_footprint_entitlement(proc_t p, boolean_t footprint_increase)
{
	int memlimit_mb_active = 0, memlimit_mb_inactive = 0;

	if (p == NULL) {
		return;
	}

	proc_list_lock();

	if (p->p_memstat_memlimit_active > 0) {
		memlimit_mb_active = p->p_memstat_memlimit_active;
	} else if (p->p_memstat_memlimit_active == -1) {
		memlimit_mb_active = max_task_footprint_mb;
	} else {
		/*
		 * Nothing to do for '0' which is
		 * a special value only used internally
		 * to test 'no limits'.
		 */
		proc_list_unlock();
		return;
	}

	if (p->p_memstat_memlimit_inactive > 0) {
		memlimit_mb_inactive = p->p_memstat_memlimit_inactive;
	} else if (p->p_memstat_memlimit_inactive == -1) {
		memlimit_mb_inactive = max_task_footprint_mb;
	} else {
		/*
		 * Nothing to do for '0' which is
		 * a special value only used internally
		 * to test 'no limits'.
		 */
		proc_list_unlock();
		return;
	}

	if (footprint_increase) {
		memlimit_mb_active += legacy_footprint_bonus_mb;
		memlimit_mb_inactive += legacy_footprint_bonus_mb;
	} else {
		memlimit_mb_active -= legacy_footprint_bonus_mb;
		if (memlimit_mb_active == max_task_footprint_mb) {
			memlimit_mb_active = -1; /* reverting back to default system limit */
		}

		memlimit_mb_inactive -= legacy_footprint_bonus_mb;
		if (memlimit_mb_inactive == max_task_footprint_mb) {
			memlimit_mb_inactive = -1; /* reverting back to default system limit */
		}
	}
	memorystatus_raise_memlimit_locked(p, memlimit_mb_active, memlimit_mb_inactive);

	proc_list_unlock();
}

void
memorystatus_act_on_ios13extended_footprint_entitlement(proc_t p)
{
	proc_list_lock();
	memorystatus_raise_memlimit_locked(p,
	    memorystatus_ios13extended_footprint_limit_mb,
	    memorystatus_ios13extended_footprint_limit_mb);
	proc_list_unlock();
}

void
memorystatus_act_on_entitled_task_limit(proc_t p)
{
	int memlimit;
	if (memorystatus_entitled_max_task_footprint_mb == 0) {
		// Entitlement is not supported on this device.
		return;
	}
	proc_list_lock();
#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
	// We want a separate memory limit for bincompat (iPad) apps on visionOS.
	switch (proc_platform(p)) {
	case PLATFORM_XROS:
	case PLATFORM_XROSSIMULATOR:
		memlimit = memorystatus_entitled_max_task_footprint_mb;
		break;
	default:
		if (memorystatus_entitled_bincompat_max_task_footprint_mb != 0) {
			memlimit = memorystatus_entitled_bincompat_max_task_footprint_mb;
		} else {
			memlimit = memorystatus_entitled_max_task_footprint_mb;
		}
		break;
	}
#else // CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
	memlimit = memorystatus_entitled_max_task_footprint_mb;
#endif // !CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
	memorystatus_raise_memlimit_locked(p, memlimit, memlimit);
	proc_list_unlock();
}

void
memorystatus_act_on_entitled_developer_task_limit(proc_t p)
{
	if (memorystatus_entitled_dev_max_task_footprint_mb == 0) {
		// Entitlement not supported on this device
		return;
	}
	memorystatus_log("memorystatus: WARNING %s [%d] is receiving an entitled "
	    "debugging memory limit. This is intended only for debugging and "
	    "can result in unstable device behavior.",
	    proc_best_name(p), proc_getpid(p));
	proc_list_lock();
	memorystatus_raise_memlimit_locked(p,
	    memorystatus_entitled_dev_max_task_footprint_mb,
	    memorystatus_entitled_dev_max_task_footprint_mb);
	proc_list_unlock();
}

#endif /* __arm64__ */

SYSCTL_INT(_kern, OID_AUTO, memorystatus_level, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_level, 0, "");

int
memorystatus_get_level(__unused struct proc *p, struct memorystatus_get_level_args *args, __unused int *ret)
{
	user_addr_t     level = 0;

	level = args->level;

	if (copyout(&memorystatus_level, level, sizeof(memorystatus_level)) != 0) {
		return EFAULT;
	}

	return 0;
}

static void memorystatus_thread(void *param __unused, wait_result_t wr __unused);

/* Memory Limits */

static bool memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason);
static bool memstat_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason);


static int memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);

#if DEBUG || DEVELOPMENT
static int memorystatus_cmd_set_diag_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);
static int memorystatus_cmd_get_diag_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);
static int memorystatus_set_diag_memlimit_properties_internal(proc_t p, memorystatus_diag_memlimit_properties_t *p_entry);
static int memorystatus_get_diag_memlimit_properties_internal(proc_t p, memorystatus_diag_memlimit_properties_t *p_entry);
#endif  // DEBUG || DEVELOPMENT
static int memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry);

static int memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);

static int memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval);

static void memorystatus_get_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t *p_entry);

int proc_get_memstat_priority(proc_t, boolean_t);

static boolean_t memorystatus_idle_snapshot = 0;

unsigned int memorystatus_delta = 0;

/* Jetsam Loop Detection */
boolean_t memorystatus_jld_enabled = FALSE;              /* Enable jetsam loop detection */
uint32_t memorystatus_jld_eval_period_msecs = 0;         /* Init pass sets this based on device memory size */
int      memorystatus_jld_max_kill_loops = 2;            /* How many times should we try and kill up to the target band */

/*
 * A FG app can request that the aggressive jetsam mechanism display some leniency in the FG band. This 'lenient' mode is described as:
 * --- if aggressive jetsam kills an app in the FG band and gets back >=AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD memory, it will stop the aggressive march further into and up the jetsam bands.
 *
 * RESTRICTIONS:
 * - Such a request is respected/acknowledged only once while that 'requesting' app is in the FG band i.e. if aggressive jetsam was
 * needed and the 'lenient' mode was deployed then that's it for this special mode while the app is in the FG band.
 *
 * - If the app is still in the FG band and aggressive jetsam is needed again, there will be no stop-and-check the next time around.
 *
 * - Also, the transition of the 'requesting' app away from the FG band will void this special behavior.
 */

#define AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD        25
boolean_t       memorystatus_aggressive_jetsam_lenient_allowed = FALSE;
boolean_t       memorystatus_aggressive_jetsam_lenient = FALSE;

#if DEVELOPMENT || DEBUG
/*
 * Jetsam Loop Detection tunables.
 */

SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_eval_period_msecs, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_eval_period_msecs, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_jld_max_kill_loops, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_jld_max_kill_loops, 0, "");
#endif /* DEVELOPMENT || DEBUG */

/*
 * snapshot support for memstats collected at boot.
 */
static memorystatus_jetsam_snapshot_t memorystatus_at_boot_snapshot;

static void memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count);
static boolean_t memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount);
static void memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime);

static void memorystatus_clear_errors(void);

static void memorystatus_get_task_phys_footprint_page_counts(task_t task,
    uint64_t *internal_pages, uint64_t *internal_compressed_pages,
    uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages,
    uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages,
    uint64_t *iokit_mapped_pages, uint64_t *page_table_pages, uint64_t *frozen_to_swap_pages,
    uint64_t *neural_nofootprint_total_pages);

static void memorystatus_get_task_memory_region_count(task_t task, uint64_t *count);

static memorystatus_proc_state_t _memstat_build_state(proc_t p);
//static boolean_t memorystatus_issue_pressure_kevent(boolean_t pressured);

static bool memorystatus_kill_processes_aggressive(uint32_t cause, int aggr_count, int32_t priority_max, int32_t max_kills, uint32_t *errors, uint64_t *memory_reclaimed);
static boolean_t memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged, uint64_t *memory_reclaimed);
static bool _memstat_proc_is_reapable(proc_t p);
static void _memstat_refresh_oldest_reapable_proc_info(void);
static bool _memstat_proc_is_application(proc_t p);

#if CONFIG_JETSAM
static void _memstat_reaper_check_oldest_reapable_proc_info_timeout(void);
static void _memstat_reaper_start_sweep(void);
static void _memstat_reaper_end_sweep(void);
static void _memstat_reaper_record_kill(uint64_t bytes_freed);
#endif /* CONFIG_JETSAM */
static const char* _memstat_relaunch_flags_description(uint32_t flags);
static const char* _memstat_proc_type_description(proc_t p);


/* Priority Band Sorting Routines */
static int  memstat_sort_bucket(unsigned int bucket_index, memorystatus_jetsam_sort_order_t sort_order);
static void memstat_sort_coals_locked(unsigned int bucket_index, memorystatus_jetsam_sort_order_t sort_order);
static void memstat_sort_by_footprint_locked(unsigned int bucket_index);

#define JETSAM_SORT_IDLE_DEFAULT JETSAM_SORT_FOOTPRINT_NOCOAL
#if XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR
#define JETSAM_SORT_FG_DEFAULT JETSAM_SORT_LRU
#else /* XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR */
#define JETSAM_SORT_FG_DEFAULT JETSAM_SORT_FOOTPRINT
#endif /* !(XNU_TARGET_OS_IOS && !XNU_TARGET_OS_XR) */

TUNABLE_DT(memorystatus_jetsam_sort_order_t, memstat_jetsam_fg_sort_order, "/defaults",
    "kern.memstat_fg_sort_order", "memstat_fg_sort_order", JETSAM_SORT_FG_DEFAULT, TUNABLE_DT_NONE);

/* qsort routines */
typedef int (*cmpfunc_t)(const void *a, const void *b);
extern void qsort(void *a, size_t n, size_t es, cmpfunc_t cmp);

/* VM pressure */

#if CONFIG_SECLUDED_MEMORY
extern unsigned int     vm_page_secluded_count;
extern unsigned int     vm_page_secluded_count_over_target;
#endif /* CONFIG_SECLUDED_MEMORY */

/* Aggressive jetsam pages threshold for sysproc aging policy */
unsigned int memorystatus_sysproc_aging_aggr_pages = 0;

uint32_t memorystatus_available_pages = UINT32_MAX;

__options_closed_decl(memorystatus_policy_t, uint8_t, {
	kPolicyDefault        = 0x00,
	kPolicyClearTheDecks  = 0x01,
	kPolicyBallastDrain   = 0x02,
});

static memorystatus_policy_t memstat_policy_config = kPolicyDefault;

#define MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX ((uint32_t)(atop_64(max_mem) / 2))

/*
 * Jetsam Page Shortage Thresholds (PSTs):
 *  - critical: jetsam above the idle band
 *  - idle: jetsam in the idle band
 *  - pressure: jetsam soft memory limit violators
 *  - reaper: jetsam long-idle processes
 *  - ballast: offset applied to non-critical thresholds upon request
 *    from userspace
 *  - ctd (clear-the-decks): offset applied to non-critical thresholds upon request
 *    from userspace
 */
uint32_t memstat_critical_threshold = 0;
uint32_t memstat_idle_threshold = 0;
uint32_t memstat_soft_threshold = 0;
uint32_t memstat_reaper_threshold = 0;
uint32_t memstat_ballast_offset = 0;
uint32_t memstat_ctd_offset = 0;

int32_t  memstat_reaper_min_age_secs = MEMORYSTATUS_REAPER_MIN_AGE_SECS_DEFAULT;
int32_t  memstat_reaper_min_age_apps_secs = MEMORYSTATUS_REAPER_MIN_AGE_APPS_SECS_DEFAULT;
boolean_t memstat_reaper_enabled = MEMORYSTATUS_REAPER_ENABLED_DEFAULT;
uint32_t memstat_reaper_max_priority = MEMORYSTATUS_REAPER_MAX_PRIORITY_DEFAULT;
int32_t  memstat_reaper_rescan_secs = MEMORYSTATUS_REAPER_RESCAN_SECS_DEFAULT;
bool     memstat_reaper_is_currently_sweeping = false;
uint64_t memstat_reaper_can_run_after_ts_matu = 0;
uint64_t memstat_reaper_start_ts_matu = 0;

uint32_t memstat_reaper_reap_relaunch_mask = MEMORYSTATUS_REAPER_REAP_RELAUNCH_MASK_DEFAULT;

#define MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_UNKNOWN UINT64_MAX
#define MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_NONE    (UINT64_MAX-1)
uint64_t memstat_oldest_reapable_proc_prio_start = MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_UNKNOWN;
uint64_t memstat_oldest_reapable_proc_info_expiration_ts_matu = 0;

uint64_t memstat_oldest_reapable_proc_will_be_reapable_at_ts_matu = 0;

typedef struct memstat_reaper_stats {
	uint32_t sweep_count;
	uint32_t kill_count;
	uint64_t memory_freed_bytes;
} memstat_reaper_stats_t;

memstat_reaper_stats_t memstat_reaper_current_sweep_stats;
memstat_reaper_stats_t memstat_reaper_cumulative_stats;
uint32_t memstat_reaper_cumulative_memory_freed_mb = 0;
/*
 * NB: These MiB thresholds are only read at boot and may become out of sync
 * with the PSTs above.
 */
TUNABLE_DT(uint32_t, memorystatus_critical_threshold_mb, "/defaults",
    "kern.memstat_critical_mb", "memorystatus_critical_threshold_mb", 0, TUNABLE_DT_NONE);
TUNABLE_DT(uint32_t, memorystatus_idle_threshold_mb, "/defaults",
    "kern.memstat_idle_mb", "memorystatus_idle_threshold_mb", 0, TUNABLE_DT_NONE);
TUNABLE_DT(uint32_t, memorystatus_pressure_threshold_mb, "/defaults",
    "kern.memstat_pressure_mb", "memorystatus_pressure_threshold_mb", 0, TUNABLE_DT_NONE);
TUNABLE_DT(uint32_t, memorystatus_reaper_threshold_mb, "/defaults",
    "kern.memstat_reaper_mb", "memorystatus_reaper_threshold_mb", 0, TUNABLE_DT_NONE);
TUNABLE_DT(uint32_t, memstat_ballast_offset_mb, "/defaults",
    "kern.memstat_ballast_mb", "memstat_ballast_offset_mb", 0, TUNABLE_DT_NONE);
TUNABLE(uint32_t, memstat_ctd_offset_mb, "memstat_ballast_offset_mb", 0);

/*
 * Kill count tracking
 *
 * Since idle exit is only applicable to processes in the idle band, track it
 * separately to save space. We also don't care about kMemorysatusInvalid.
 */
uint32_t _Atomic memorystatus_kill_counts[JETSAM_PRIORITY_MAX + 1][JETSAM_REASON_MEMORYSTATUS_MAX - 1];
uint32_t _Atomic memorystatus_idle_exit_kill_count = 0;

TUNABLE_DT(int32_t, memorystatus_reaper_minimum_age_seconds, "/defaults",
    "kern.memstat_reaper_minage_secs", "memorystatus_reaper_minimum_age_seconds", MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT, TUNABLE_DT_NONE);
TUNABLE_DT(int32_t, memorystatus_reaper_minimum_age_apps_seconds, "/defaults",
    "kern.memstat_reaper_minapp_secs", "memorystatus_reaper_minimum_age_apps_seconds", MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT, TUNABLE_DT_NONE);
TUNABLE_DT(uint32_t, memorystatus_reaper_rescan_delay_seconds, "/defaults",
    "kern.memstat_reaper_rescan_secs", "memorystatus_reaper_rescan_delay_seconds", MEMORYSTATUS_REAPER_RESCAN_SECS_DEFAULT, TUNABLE_DT_NONE);
TUNABLE_DT(boolean_t, memorystatus_reaper_enabled, "/defaults",
    "kern.memstat_reaper_enabled", "memorystatus_reaper_enabled", MEMORYSTATUS_REAPER_ENABLED_DEFAULT, TUNABLE_DT_NONE);


#if CONFIG_JETSAM
TUNABLE_DT_WRITEABLE(unsigned int, memorystatus_swap_all_apps, "/defaults", "kern.swap_all_apps", "kern.swap_all_apps", false, TUNABLE_DT_NONE);
/* Will compact the early swapin queue if there are >= this many csegs on it. */
static unsigned int memorystatus_swapin_trigger_segments = 10;
unsigned int memorystatus_swapin_trigger_pages = 0;

#if DEVELOPMENT || DEBUG
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, "");
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_swapin_trigger_pages, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_swapin_trigger_pages, 0, "");
#else
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_available_pages, CTLFLAG_RD | CTLFLAG_MASKED | CTLFLAG_LOCKED, &memorystatus_available_pages, 0, "");
#endif /* DEVELOPMENT || DEBUG */
SYSCTL_UINT(_kern, OID_AUTO, memorystatus_swap_all_apps, CTLFLAG_RD | CTLFLAG_LOCKED, &memorystatus_swap_all_apps, 0, "");

static int memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit);

proc_name_t memorystatus_jetsam_proc_name_panic; /* Panic when we are about to jetsam this process. */
uint32_t    memorystatus_jetsam_proc_cause_panic = 0; /* If specified, panic only when we are about to jetsam the process above for this cause. */
uint32_t    memorystatus_jetsam_proc_size_panic = 0; /* If specified, panic only when we are about to jetsam the process above and its footprint is more than this in MB. */

/* If set, kill swappable processes when we're low on swap space. Currently off until we can allocate more swap space (rdar://87800902) */
TUNABLE(bool, jetsam_kill_on_low_swap, "jetsam_kill_on_low_swap", false);

/*
 * Global switch for enabling fast jetsam. Fast jetsam is
 * hooked up via the system_override() system call. When
 * enabled, the following features can be toggled:
 * - clear-the-decks jetsam
 * - ballast-drain jetsam
 */
TUNABLE_WRITEABLE(bool, fast_jetsam_enabled, "fast_jetsam_enabled", true);

#else /* !CONFIG_JETSAM */

/*
 * On compressor/swap exhaustion, kill the largest process regardless of
 * its chosen process policy.
 */
#if DEVELOPMENT || DEBUG
TUNABLE(bool, kill_on_no_paging_space, "-kill_on_no_paging_space", false);
#else /* !(DEVELOPMENT || DEBUG) */
bool kill_on_no_paging_space = false;
#endif /* DEVELOPMENT || DEBUG */

/* The timestamp (MAS) of the last no paging space action */
_Atomic uint64_t last_no_space_action_ts = 0;
/* The minimum duration between no paging space actions */
TUNABLE(uint64_t, no_paging_space_action_throttle_delay_ns,
    "no_paging_space_throttle_delay_ns", 5 * NSEC_PER_SEC);

#endif /* CONFIG_JETSAM */

static inline uint32_t
roundToNearestMB(uint32_t in)
{
	return (in + ((1 << 20) - 1)) >> 20;
}

#if DEVELOPMENT || DEBUG
static int memorystatus_cmd_increase_jetsam_task_limit(pid_t pid, uint32_t byte_increase);
#endif

#if __arm64__
extern int legacy_footprint_entitlement_mode;
#endif /* __arm64__ */

/* Debug */

extern struct knote *vm_find_knote_from_pid(pid_t, struct klist *);

#if DEVELOPMENT || DEBUG

static unsigned int memorystatus_debug_dump_this_bucket = 0;

static void
memorystatus_debug_dump_bucket_locked(unsigned int bucket_index)
{
	proc_t p = NULL;
	uint64_t bytes = 0;
	int ledger_limit = 0;
	unsigned int b = bucket_index;
	boolean_t traverse_all_buckets = FALSE;

	if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
		traverse_all_buckets = TRUE;
		b = 0;
	} else {
		traverse_all_buckets = FALSE;
		b = bucket_index;
	}

	/*
	 * footprint reported in [pages / MB ]
	 * limits reported as:
	 *      L-limit  proc's Ledger limit
	 *      C-limit  proc's Cached limit, should match Ledger
	 *      A-limit  proc's Active limit
	 *     IA-limit  proc's Inactive limit
	 *	F==Fatal,  NF==NonFatal
	 */

	memorystatus_log_debug("memorystatus_debug_dump ***START*(PAGE_SIZE_64=%llu)**\n", PAGE_SIZE_64);
	memorystatus_log_debug("bucket [pid]       [pages / MB]     [state]      [EP / RP / AP]   dirty     deadline [L-limit / C-limit / A-limit / IA-limit] name\n");
	p = memorystatus_get_first_proc_locked(&b, traverse_all_buckets);
	while (p) {
		bytes = get_task_phys_footprint(proc_task(p));
		task_get_phys_footprint_limit(proc_task(p), &ledger_limit);
		memorystatus_log_debug("%2d     [%5d]     [%5lld /%3lldMB]   0x%-8x   [%2d / %2d / %2d]   0x%-3x   %10lld    [%3d / %3d%s / %3d%s / %3d%s]   %s\n",
		    b, proc_getpid(p),
		    (bytes / PAGE_SIZE_64),             /* task's footprint converted from bytes to pages     */
		    (bytes / (1024ULL * 1024ULL)),      /* task's footprint converted from bytes to MB */
		    p->p_memstat_state, p->p_memstat_effectivepriority, p->p_memstat_requestedpriority, p->p_memstat_assertionpriority,
		    p->p_memstat_dirty, p->p_memstat_idledeadline,
		    ledger_limit,
		    p->p_memstat_memlimit,
		    (p->p_memstat_state & P_MEMSTAT_FATAL_MEMLIMIT ? "F " : "NF"),
		    p->p_memstat_memlimit_active,
		    (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL ? "F " : "NF"),
		    p->p_memstat_memlimit_inactive,
		    (p->p_memstat_state & P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL ? "F " : "NF"),
		    (*p->p_name ? p->p_name : "unknown"));
		p = memorystatus_get_next_proc_locked(&b, p, traverse_all_buckets);
	}
	memorystatus_log_debug("memorystatus_debug_dump ***END***\n");
}

static int
sysctl_memorystatus_debug_dump_bucket SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg2)
	int bucket_index = 0;
	int error;
	error = SYSCTL_OUT(req, arg1, sizeof(int));
	if (error || !req->newptr) {
		return error;
	}
	error = SYSCTL_IN(req, &bucket_index, sizeof(int));
	if (error || !req->newptr) {
		return error;
	}
	if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
		/*
		 * All jetsam buckets will be dumped.
		 */
	} else {
		/*
		 * Only a single bucket will be dumped.
		 */
	}

	proc_list_lock();
	memorystatus_debug_dump_bucket_locked(bucket_index);
	proc_list_unlock();
	memorystatus_debug_dump_this_bucket = bucket_index;
	return error;
}

/*
 * Debug aid to look at jetsam buckets and proc jetsam fields.
 *	Use this sysctl to act on a particular jetsam bucket.
 *	Writing the sysctl triggers the dump.
 *      Usage: sysctl kern.memorystatus_debug_dump_this_bucket=<bucket_index>
 */

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_debug_dump_this_bucket, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_debug_dump_this_bucket, 0, sysctl_memorystatus_debug_dump_bucket, "I", "");


/* Debug aid to aid determination of limit */

static int
sysctl_memorystatus_highwater_enable SYSCTL_HANDLER_ARGS
{
#pragma unused(oidp, arg2)
	proc_t p;
	unsigned int b = 0;
	int error, enable = 0;
	bool use_active;   /* use the active limit and active limit attributes */

	error = SYSCTL_OUT(req, arg1, sizeof(int));
	if (error || !req->newptr) {
		return error;
	}

	error = SYSCTL_IN(req, &enable, sizeof(int));
	if (error || !req->newptr) {
		return error;
	}

	if (!(enable == 0 || enable == 1)) {
		return EINVAL;
	}

	proc_list_lock();

	memorystatus_highwater_enabled = enable;

	p = memorystatus_get_first_proc_locked(&b, TRUE);
	while (p) {
		use_active = memstat_proc_is_active_locked(p);

		if (enable) {
			(void)memstat_update_memlimit_locked(p, use_active);
		} else {
			/*
			 * Disabling limits does not touch the stored variants.
			 * Set the cached limit fields to system_wide defaults.
			 */
			p->p_memstat_memlimit = -1;
			p->p_memstat_state |= P_MEMSTAT_FATAL_MEMLIMIT;
		}

		/*
		 * Enforce the cached limit by writing to the ledger.
		 */
		_memstat_write_memlimit_to_ledger_locked(p, use_active, false);

		p = memorystatus_get_next_proc_locked(&b, p, TRUE);
	}


	proc_list_unlock();

	return 0;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_highwater_enabled, CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_highwater_enabled, 0, sysctl_memorystatus_highwater_enable, "I", "");

SYSCTL_INT(_kern, OID_AUTO, memorystatus_idle_snapshot, CTLFLAG_RW | CTLFLAG_LOCKED, &memorystatus_idle_snapshot, 0, "");

#endif /* DEVELOPMENT || DEBUG */

#if CONFIG_JETSAM
#if DEVELOPMENT || DEBUG
static int
memstat_page_shortage_threshold_sysctl_handler SYSCTL_HANDLER_ARGS
{
	uint32_t threshold;
	if (arg1 == &memstat_idle_threshold) {
		threshold = memorystatus_get_idle_exit_page_shortage_threshold();
	} else if (arg1 == &memstat_soft_threshold) {
		threshold = memorystatus_get_soft_memlimit_page_shortage_threshold();
	} else if (arg1 == &memstat_critical_threshold) {
		threshold = memorystatus_get_critical_page_shortage_threshold();
	} else if (arg1 == &memstat_reaper_threshold) {
		threshold = memorystatus_get_reaper_page_shortage_threshold();
	} else {
		return EINVAL;
	}
	return sysctl_handle_int(oidp, NULL, threshold, req);
}

SYSCTL_PROC(_kern_memorystatus, OID_AUTO, available_pages_critical,
    CTLFLAG_RD | CTLFLAG_LOCKED, &memstat_critical_threshold, 0,
    memstat_page_shortage_threshold_sysctl_handler, "IU",
    "");
SYSCTL_PROC(_kern_memorystatus, OID_AUTO, available_pages_idle,
    CTLFLAG_RD | CTLFLAG_LOCKED, &memstat_idle_threshold, 0,
    memstat_page_shortage_threshold_sysctl_handler, "IU",
    "");
SYSCTL_PROC(_kern_memorystatus, OID_AUTO, available_pages_soft,
    CTLFLAG_RD | CTLFLAG_LOCKED, &memstat_soft_threshold, 0,
    memstat_page_shortage_threshold_sysctl_handler, "IU",
    "");
SYSCTL_PROC(_kern_memorystatus, OID_AUTO, available_pages_reaper,
    CTLFLAG_RD | CTLFLAG_LOCKED, &memstat_reaper_threshold, 0,
    memstat_page_shortage_threshold_sysctl_handler, "IU",
    "");

SYSCTL_INT(_kern_memorystatus, OID_AUTO, ballast_offset_pages,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &memstat_ballast_offset, 0, "");
SYSCTL_INT(_kern_memorystatus, OID_AUTO, ctd_offset_pages,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &memstat_ctd_offset, 0, "");
#endif /* DEBUG || DEVELOPMENT */

EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_enabled, &memstat_reaper_enabled, FALSE, TRUE, "");
EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_min_age_secs, &memstat_reaper_min_age_secs, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_min_age_apps_secs, &memstat_reaper_min_age_apps_secs, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_max_priority, &memstat_reaper_max_priority, 0, JETSAM_PRIORITY_MAX, "");
EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_reap_relaunch_mask, &memstat_reaper_reap_relaunch_mask, 0, UINT32_MAX, "");
EXPERIMENT_FACTOR_LEGACY_UINT(_kern_memorystatus, reaper_rescan_secs, &memstat_reaper_rescan_secs, 0, UINT32_MAX, "");

SYSCTL_INT(_kern_memorystatus, OID_AUTO, reaper_stats_sweep_count,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &(memstat_reaper_cumulative_stats.sweep_count), 0, "");
SYSCTL_INT(_kern_memorystatus, OID_AUTO, reaper_stats_total_kills,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &(memstat_reaper_cumulative_stats.kill_count), 0, "");
SYSCTL_INT(_kern_memorystatus, OID_AUTO, reaper_stats_total_freed_mb,
    CTLFLAG_RD | CTLFLAG_LOCKED,
    &memstat_reaper_cumulative_memory_freed_mb, 0, "");

static int
memstat_page_shortage_threshold_experiment_handler SYSCTL_HANDLER_ARGS
{
	uint32_t threshold_mb;
	int error;

	assert3p(arg1, !=, NULL);
	threshold_mb = ptoa_32(os_atomic_load((uint32_t *)arg1, relaxed)) >> 20;

	error = sysctl_handle_int(oidp, &threshold_mb, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	if (threshold_mb > UINT32_MAX >> 20) {
		/* Converting to bytes would overflow */
		return EINVAL;
	}

	uint32_t new_threshold_pages = atop_32(threshold_mb << 20);
	/*
	 * Page shortage thresholds may not exceed 1/2 max_mem
	 */
	if (new_threshold_pages > MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX) {
		return EINVAL;
	}
	if ((arg1 == &memstat_soft_threshold ||
	    arg1 == &memstat_idle_threshold ||
	    arg1 == &memstat_critical_threshold ||
	    arg1 == &memstat_reaper_threshold) &&
	    new_threshold_pages == 0) {
		return EINVAL;
	}

	if (arg1 == &memstat_soft_threshold) {
		memorystatus_log("memorystatus: setting soft memory limit "
		    "page shortage threshold to %u MiB\n", threshold_mb);
	} else if (arg1 == &memstat_idle_threshold) {
		memorystatus_log("memorystatus: setting idle exit page "
		    "shortage threshold to %u MiB\n", threshold_mb);
	} else if (arg1 == &memstat_critical_threshold) {
		memorystatus_log("memorystatus: setting critical page shortage"
		    " threshold to %u MiB\n", threshold_mb);
	} else if (arg1 == &memstat_reaper_threshold) {
		memorystatus_log("memorystatus: setting reaper page shortage"
		    " threshold to %u MiB\n", threshold_mb);
	} else if (arg1 == &memstat_ctd_offset) {
		memorystatus_log("memorystatus: setting clear-the-decks page shortage"
		    " offset to %u MiB\n", threshold_mb);
	} else if (arg1 == &memstat_ballast_offset) {
		memorystatus_log("memorystatus: setting ballast page shortage"
		    " offset to %u MiB\n", threshold_mb);
	} else {
		return EINVAL;
	}
	os_atomic_store((uint32_t *)arg1, new_threshold_pages, relaxed);

	return 0;
}

#if DEVELOPMENT || DEBUG
#define MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS (CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED)
#else /* RELEASE */
#define MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS (CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_ANYBODY | CTLFLAG_LOCKED | CTLFLAG_MASKED)
#endif /* DEVELOPMENT || DEBUG */

EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, soft_threshold_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_soft_threshold, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "The minimum amount of available memory to maintain before killing "
    "processes which have violated there soft memory limit");

EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, idle_threshold_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_idle_threshold, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "The minimum amount of available memory to maintain before exiting idle "
    "processes");
EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, critical_threshold_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_critical_threshold, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "The minimum amount of available memory to maintain before killing non-idle "
    "processes");
EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, reaper_threshold_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_reaper_threshold, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "The minimum amount of available memory to maintain before killing long-idle "
    "processes");
EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, ballast_offset_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_ballast_offset, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "An offset to apply to all non-critical page shortage thresholds when "
    "ballast is filling");
EXPERIMENT_FACTOR_LEGACY_PROC(_kern_memorystatus, clear_the_decks_offset_mb,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    &memstat_ctd_offset, 0, memstat_page_shortage_threshold_experiment_handler,
    "IU",
    "An offset to apply to all non-critical page shortage thresholds when "
    "clear-the-decks is engaged");

int
memorystatus_ballast_control(bool drain)
{
	if (!fast_jetsam_enabled) {
		memorystatus_log_error("memorystatus: fast-jetsam "
		    "has been disabled on this system. denying request to %s ballast\n",
		    drain ? "drain" : "flood");
		return ENOTSUP;
	}
	if (memstat_ballast_offset == 0) {
		/* nothing to do */
		return 0;
	}
	if (drain) {
		/*
		 * Drain the ballast tanks, providing additional buoyancy by requiring that
		 * they only be used to store "available" memory.
		 */
		memorystatus_policy_t orig_policy = os_atomic_or_orig(
			&memstat_policy_config,
			(memorystatus_policy_t)kPolicyBallastDrain, relaxed);
		if (orig_policy & kPolicyBallastDrain) {
			return 0;
		}
		memorystatus_log("memorystatus: draining ballast "
		    "-- will add %u MiB to non-critical page shortage "
		    "thresholds\n", ptoa_32(memstat_ballast_offset) >> 20);
		memorystatus_thread_pool_max();
		_memstat_consider_waking_jetsam_thread();
	} else {
		/*
		 * Flood the ballast tanks, removing the extra buoyancy by allowing them to be
		 * filled with "unavailable" memory.
		 */
		memorystatus_policy_t orig_policy = os_atomic_andnot_orig(
			&memstat_policy_config,
			(memorystatus_policy_t)kPolicyBallastDrain, relaxed);
		if (!(orig_policy & kPolicyBallastDrain)) {
			/* already disabled */
			return 0;
		}
		assertf(fast_jetsam_enabled, "ballast was drained while fast-jetsam was disabled");
		memorystatus_log("memorystatus: flooding ballast "
		    "-- will subtract %u MiB from non-critical page shortage "
		    "thresholds\n", ptoa_32(memstat_ballast_offset) >> 20);
		memorystatus_thread_pool_default();
		_memstat_consider_waking_jetsam_thread();
	}
	return 0;
}

static int
sysctl_kern_memorystatus_ballast_drain SYSCTL_HANDLER_ARGS
{
	int error = 0;

	boolean_t drained = os_atomic_load(&memstat_policy_config, relaxed) & kPolicyBallastDrain ? TRUE : FALSE;

	error = sysctl_handle_int(oidp, &drained, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	/*
	 * Writers must be root or have the com.apple.private.kernel.jetsam entitlement
	 */
	error = priv_check_cred(kauth_cred_get(), PRIV_VM_JETSAM, 0);
	if (error) {
		return error;
	}

	return memorystatus_ballast_control(drained);
}

SYSCTL_PROC(_kern_memorystatus, OID_AUTO, ballast_drained,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS, 0, 0,
    sysctl_kern_memorystatus_ballast_drain, "IU",
    "If true, apply an offset (kern.memorystatus.ballast_offset_mb) to "
    "all non-critical page shortage thresholds");

#if DEVELOPMENT || DEBUG
/*
 * In preparation for a storm, sailors may "clear the decks" of non-essential
 * cargo to increase the seaworthiness of a vessel. In our analogy, the
 * non-essential cargo is idle processes or processes which have exceeded
 * their memory limit. The storm may be any foreseeable user activity that will
 * require significant memory demand.
 *
 * Mechanically, clearing the decks involves adding a configurable offset to
 * the idle and soft available page shortage thresholds.
 *
 * Readers may note that the clear-the-decks policy is mechanically identical
 * the ballast-draining policy. Their difference lies in intended use.
 * Clear-the-decks is intended to address imminent memory demand and may be
 * configured with an offset that wouldn't be sustainable for long-term system
 * use. The interface is generally intended to allow clients to hint to the
 * system that they will need a significant amount of memory in the near future,
 * and the system should proactively try to free unneeded reserves to satisfy
 * to be able to better satisfy the demand.
 *
 * This policy is currently only exposed on development kernels for prototyping
 * until a productized use case emerges
 *
 * TODO: If adopted on production systems, this mechanism should use a
 * dedicated system-call / memorystatus-command
 */
static int
memstat_clear_the_decks(bool clear)
{
	if (!fast_jetsam_enabled) {
		memorystatus_log_error("memorystatus: fast-jetsam "
		    "has been disabled on this system\n");
		return ENOTSUP;
	}
	if (clear) {
		/*
		 * Clear the decks of non-essential cargo.
		 */
		memorystatus_policy_t orig_policy = os_atomic_or_orig(
			&memstat_policy_config,
			(memorystatus_policy_t)kPolicyClearTheDecks, relaxed);
		if (orig_policy & kPolicyClearTheDecks) {
			return EALREADY;
		}
		memorystatus_log("memorystatus: clear-the-decks engaged "
		    "-- will add %u MiB to non-critical page shortage "
		    "thresholds\n", ptoa_32(memstat_ctd_offset) >> 20);
		memorystatus_thread_pool_max();
		_memstat_consider_waking_jetsam_thread();
	} else {
		/*
		 * Allow the decks to be reloaded with non-essential cargo.
		 */
		memorystatus_policy_t orig_policy = os_atomic_andnot_orig(
			&memstat_policy_config,
			(memorystatus_policy_t)kPolicyClearTheDecks, relaxed);
		if (!(orig_policy & kPolicyClearTheDecks)) {
			return EALREADY;
		}
		assertf(fast_jetsam_enabled, "clear the decks was set while fast-jetsam was disabled");
		memorystatus_log("memorystatus: clear-the-decks disengaged "
		    "-- will subtract %u MiB from non-critical page shortage "
		    "thresholds\n", ptoa_32(memstat_ctd_offset) >> 20);
		memorystatus_thread_pool_default();
		_memstat_consider_waking_jetsam_thread();
	}
	return 0;
}

static int
sysctl_kern_memorystatus_decks_cleared SYSCTL_HANDLER_ARGS
{
	int error = 0;

	boolean_t cleared = os_atomic_load(&memstat_policy_config, relaxed) & kPolicyClearTheDecks ? TRUE : FALSE;

	error = sysctl_handle_int(oidp, &cleared, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	/*
	 * Writers must be root or have the com.apple.private.kernel.jetsam entitlement
	 */
	error = priv_check_cred(kauth_cred_get(), PRIV_VM_JETSAM, 0);
	if (error) {
		return error;
	}

	return memstat_clear_the_decks(cleared);
}

SYSCTL_PROC(_kern_memorystatus, OID_AUTO, decks_cleared,
    MEMSTAT_PAGE_SHORTAGE_EXPERIMENT_FLAGS,
    0, 0, sysctl_kern_memorystatus_decks_cleared, "I",
    "If true, apply an offset (kern.memorystatus_ctd_offset_mb) to "
    "all non-critical page shortage thresholds");
#endif /* DEVELOPMENT || DEBUG */
#endif /* CONFIG_JETSAM */

extern kern_return_t kernel_thread_start_priority(thread_continue_t continuation,
    void *parameter,
    integer_t priority,
    thread_t *new_thread);

#if DEVELOPMENT || DEBUG

static int
sysctl_memorystatus_disconnect_page_mappings SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	int     error = 0, pid = 0;
	proc_t  p;

	error = sysctl_handle_int(oidp, &pid, 0, req);
	if (error || !req->newptr) {
		return error;
	}

	lck_mtx_lock(&disconnect_page_mappings_mutex);

	if (pid == -1) {
		vm_pageout_disconnect_all_pages();
	} else {
		p = proc_find(pid);

		if (p != NULL) {
			error = task_disconnect_page_mappings(proc_task(p));

			proc_rele(p);

			if (error) {
				error = EIO;
			}
		} else {
			error = EINVAL;
		}
	}
	lck_mtx_unlock(&disconnect_page_mappings_mutex);

	return error;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_disconnect_page_mappings, CTLTYPE_INT | CTLFLAG_WR | CTLFLAG_LOCKED | CTLFLAG_MASKED,
    0, 0, &sysctl_memorystatus_disconnect_page_mappings, "I", "");

#endif /* DEVELOPMENT || DEBUG */

/*
 * Sorts the given bucket.
 *
 * Input:
 *	bucket_index - jetsam priority band to be sorted.
 *	sort_order - JETSAM_SORT_xxx from kern_memorystatus.h
 *
 * proc_list_lock must be held by the caller.
 */
static void
memstat_sort_bucket_locked(
	unsigned int bucket_index,
	memorystatus_jetsam_sort_order_t sort_order)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(bucket_index < MEMSTAT_BUCKET_COUNT);
	if (memstat_bucket[bucket_index].count == 0) {
		return;
	}

	switch (sort_order) {
	case JETSAM_SORT_NONE:
		break;
	case JETSAM_SORT_LRU:
	case JETSAM_SORT_FOOTPRINT:
		memstat_sort_coals_locked(bucket_index, sort_order);
		break;
	case JETSAM_SORT_FOOTPRINT_NOCOAL:
		memstat_sort_by_footprint_locked(bucket_index);
	}
}

/*
 * Picks the sorting routine for a given jetsam priority band.
 *
 * Input:
 *	bucket_index - jetsam priority band to be sorted.
 *	sort_order - sort order to use
 *
 * Return:
 *	0     on success
 *      non-0 on failure
 */
static int
memstat_sort_bucket(
	unsigned int bucket_index,
	memorystatus_jetsam_sort_order_t sort_order)
{
	assert(bucket_index < MEMSTAT_BUCKET_COUNT);

	proc_list_lock();
	memstat_sort_bucket_locked(bucket_index, sort_order);
	proc_list_unlock();

	return 0;
}

/*
 * Sort processes by size for a single jetsam bucket.
 */

static void
memstat_sort_by_footprint_locked(unsigned int bucket_index)
{
	proc_t p = NULL, insert_after_proc = NULL, max_proc = NULL;
	proc_t next_p = NULL, prev_max_proc = NULL;
	uint32_t pages = 0, max_pages = 0;
	memstat_bucket_t *current_bucket;

	if (bucket_index >= MEMSTAT_BUCKET_COUNT) {
		return;
	}

	current_bucket = &memstat_bucket[bucket_index];

	p = TAILQ_FIRST(&current_bucket->list);

	while (p) {
		memorystatus_get_task_page_counts(proc_task(p), &pages, NULL, NULL);
		max_pages = pages;
		max_proc = p;
		prev_max_proc = p;

		while ((next_p = TAILQ_NEXT(p, p_memstat_list)) != NULL) {
			/* traversing list until we find next largest process */
			p = next_p;
			memorystatus_get_task_page_counts(proc_task(p), &pages, NULL, NULL);
			if (pages > max_pages) {
				max_pages = pages;
				max_proc = p;
			}
		}

		if (prev_max_proc != max_proc) {
			/* found a larger process, place it in the list */
			TAILQ_REMOVE(&current_bucket->list, max_proc, p_memstat_list);
			if (insert_after_proc == NULL) {
				TAILQ_INSERT_HEAD(&current_bucket->list, max_proc, p_memstat_list);
			} else {
				TAILQ_INSERT_AFTER(&current_bucket->list, insert_after_proc, max_proc, p_memstat_list);
			}
			prev_max_proc = max_proc;
		}

		insert_after_proc = max_proc;

		p = TAILQ_NEXT(max_proc, p_memstat_list);
	}
}

proc_t
memorystatus_get_first_proc_locked(unsigned int *bucket_index, boolean_t search)
{
	memstat_bucket_t *current_bucket;
	proc_t next_p;

	if ((*bucket_index) >= MEMSTAT_BUCKET_COUNT) {
		return NULL;
	}

	current_bucket = &memstat_bucket[*bucket_index];
	next_p = TAILQ_FIRST(&current_bucket->list);
	if (!next_p && search) {
		while (!next_p && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) {
			current_bucket = &memstat_bucket[*bucket_index];
			next_p = TAILQ_FIRST(&current_bucket->list);
		}
	}

	return next_p;
}

proc_t
memorystatus_get_next_proc_locked(unsigned int *bucket_index, proc_t p, boolean_t search)
{
	memstat_bucket_t *current_bucket;
	proc_t next_p;

	if (!p || ((*bucket_index) >= MEMSTAT_BUCKET_COUNT)) {
		return NULL;
	}

	next_p = TAILQ_NEXT(p, p_memstat_list);
	while (!next_p && search && (++(*bucket_index) < MEMSTAT_BUCKET_COUNT)) {
		current_bucket = &memstat_bucket[*bucket_index];
		next_p = TAILQ_FIRST(&current_bucket->list);
	}

	return next_p;
}

jetsam_state_t jetsam_threads;

/* Maximum number of jetsam threads allowed */
#define JETSAM_THREADS_LIMIT   3

/* Number of active jetsam threads */
_Atomic unsigned int active_jetsam_threads = 1;
/* Number of maximum jetsam threads configured */
unsigned int max_jetsam_threads = 1;

static jetsam_state_t
jetsam_current_thread()
{
	for (int thr_id = 0; thr_id < max_jetsam_threads; thr_id++) {
		if (jetsam_threads[thr_id].thread == current_thread()) {
			return &(jetsam_threads[thr_id]);
		}
	}
	return NULL;
}

#if CONFIG_JETSAM
static void
initialize_entitled_max_task_limit()
{
#if !XNU_TARGET_OS_XR
	/**
	 * We've already stored the potential boot-arg "entitled_max_task_pmem" in
	 * memorystatus_entitled_max_task_footprint_mb as a TUNABLE_DT.  We provide
	 * argptr=NULL and max_len=0 here to check only for existence of the boot-arg.
	 *
	 * The boot-arg takes precedence over memorystatus_swap_all_apps.
	 */
	if (!PE_parse_boot_argn("entitled_max_task_pmem", NULL, 0) && memorystatus_swap_all_apps) {
		/*
		 * When we have swap, we let entitled apps go up to the dram config
		 * regardless of what's set in EDT,
		 * This can still be overriden with the entitled_max_task_pmem boot-arg.
		 *
		 * We do not want to do this on visionOS, since we can have an effectively
		 * infinite number of apps open at a time, and cannot swap our way to safety.
		 */
		memorystatus_entitled_max_task_footprint_mb =
		    (int32_t)(max_mem_actual / (1ULL << 20));
		memorystatus_entitled_dev_max_task_footprint_mb =
		    memorystatus_entitled_max_task_footprint_mb;
	}
#endif

	if (memorystatus_entitled_max_task_footprint_mb < 0) {
		memorystatus_log_error("Invalid value (%d) for entitled_max_task_pmem. "
		    "Setting to 0\n", memorystatus_entitled_max_task_footprint_mb);
		memorystatus_entitled_max_task_footprint_mb = 0;
	}

#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
	if (memorystatus_entitled_bincompat_max_task_footprint_mb < 0) {
		memorystatus_log_error("Invalid value (%d) for entitled_bincompat_max_task_pmem. "
		    "Setting to 0\n", memorystatus_entitled_bincompat_max_task_footprint_mb);
		memorystatus_entitled_bincompat_max_task_footprint_mb = 0;
	}
#endif /* CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT */

	if (memorystatus_entitled_dev_max_task_footprint_mb < -1) {
		memorystatus_log_error("Invalid value (%d) for entitled_max_developer_task_pmem. "
		    "Setting to 0\n", memorystatus_entitled_dev_max_task_footprint_mb);
		memorystatus_entitled_dev_max_task_footprint_mb = 0;
	} else if (memorystatus_entitled_dev_max_task_footprint_mb == -1) {
		memorystatus_entitled_dev_max_task_footprint_mb = (int32_t)
		    (max_mem_actual >> 20);
	}

	if (memorystatus_entitled_dev_max_task_footprint_mb &&
	    memorystatus_entitled_dev_max_task_footprint_mb <
	    memorystatus_entitled_max_task_footprint_mb) {
		memorystatus_log_error("memorystatus: Entitled developer limit (%d MB) "
		    "must be ≥ entitled task limit (%d MB)\n",
		    memorystatus_entitled_dev_max_task_footprint_mb,
		    memorystatus_entitled_max_task_footprint_mb);
		memorystatus_entitled_dev_max_task_footprint_mb =
		    memorystatus_entitled_max_task_footprint_mb;
	}
}

#endif /* CONFIG_JETSAM */


__private_extern__ void
memorystatus_init(void)
{
	kern_return_t result;
	int i;

#if CONFIG_FREEZE
	memorystatus_frozen_processes_max = FREEZE_PROCESSES_MAX_DEFAULT;
	memorystatus_frozen_shared_mb_max = ((MAX_FROZEN_SHARED_MB_PERCENT * max_task_footprint_mb) / 100); /* 10% of the system wide task limit */
	memorystatus_freeze_shared_mb_per_process_max = ((memorystatus_frozen_shared_mb_max * MAX_FROZEN_SHARED_MB_PER_PROCESS_PERCENT) / 100);
	memorystatus_freeze_pages_min = FREEZE_PAGES_MIN_DEFAULT;
	memorystatus_freeze_pages_max = FREEZE_PAGES_MAX_DEFAULT;
	memorystatus_max_frozen_demotions_daily = MAX_FROZEN_PROCESS_DEMOTIONS_DEFAULT;
	memorystatus_thaw_count_demotion_threshold = MIN_THAW_DEMOTION_THRESHOLD_DEFAULT;
	memorystatus_min_thaw_refreeze_threshold = MIN_THAW_REFREEZE_THRESHOLD_DEFAULT;
#endif /* CONFIG_FREEZE */

	// Note: no-op pending rdar://27006343 (Custom kernel log handles)
	memorystatus_log_handle = os_log_create("com.apple.xnu", "memorystatus");

	/* Init buckets */
	for (i = 0; i < MEMSTAT_BUCKET_COUNT; i++) {
		TAILQ_INIT(&memstat_bucket[i].list);
		memstat_bucket[i].count = 0;
		memstat_bucket[i].relaunch_high_count = 0;
	}
	memorystatus_idle_demotion_call = thread_call_allocate((thread_call_func_t)memorystatus_perform_idle_demotion, NULL);

	nanoseconds_to_absolutetime(memstat_idle_deferral_time_s * NSEC_PER_SEC, &memorystatus_sysprocs_idle_delay_time);
	nanoseconds_to_absolutetime(memstat_idle_deferral_time_s * NSEC_PER_SEC, &memorystatus_apps_idle_delay_time);
	nanoseconds_to_absolutetime(memstat_aging_stuck_time_s * NSEC_PER_SEC, &memorystatus_aging_stuck_delay_time);
	assert3u(memstat_idle_deferral_time_s, >=, kJetsamSysProcsIdleDelayTimeLowRatio);

#if CONFIG_JETSAM
	bzero(memorystatus_jetsam_proc_name_panic, sizeof(memorystatus_jetsam_proc_name_panic));
	if (PE_parse_boot_argn("jetsam_proc_name_panic", &memorystatus_jetsam_proc_name_panic, sizeof(memorystatus_jetsam_proc_name_panic))) {
		/*
		 * No bounds check to see if this is a valid cause.
		 * This is a debugging aid. The callers should know precisely which cause they wish to track.
		 */
		PE_parse_boot_argn("jetsam_proc_cause_panic", &memorystatus_jetsam_proc_cause_panic, sizeof(memorystatus_jetsam_proc_cause_panic));
		PE_parse_boot_argn("jetsam_proc_size_panic", &memorystatus_jetsam_proc_size_panic, sizeof(memorystatus_jetsam_proc_size_panic));
	}

	if (memorystatus_swap_all_apps && vm_page_donate_mode == VM_PAGE_DONATE_DISABLED) {
		panic("kern.swap_all_apps is not supported on this platform");
	}

	/*
	 * The aging bands cannot overlap with the JETSAM_PRIORITY_ELEVATED_INACTIVE
	 * band and must be below it in priority. This is so that we don't have to make
	 * our 'aging' code worry about a mix of processes, some of which need to age
	 * and some others that need to stay elevated in the jetsam bands.
	 */
	assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > system_procs_aging_band);
	assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > system_procs_aging_band_stuck);
	assert(JETSAM_PRIORITY_ELEVATED_INACTIVE > applications_aging_band);

	/* Take snapshots for idle-exit kills by default? First check the boot-arg... */
	if (!PE_parse_boot_argn("jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot))) {
		/* ...no boot-arg, so check the device tree */
		PE_get_default("kern.jetsam_idle_snapshot", &memorystatus_idle_snapshot, sizeof(memorystatus_idle_snapshot));
	}

	memorystatus_sysproc_aging_aggr_pages = (unsigned int)MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_AGGR_SYSPROC_AGING_PERCENTAGE);

	if (max_mem <= MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) {
		memorystatus_delta = (unsigned int)MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_DELTA_PERCENTAGE_SMALL);
	} else {
		memorystatus_delta = (unsigned int)MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_DELTA_PERCENTAGE_LARGE);
	}

	if (memorystatus_critical_threshold_mb != 0) {
		memstat_critical_threshold = atop_32(memorystatus_critical_threshold_mb << 20);
	} else {
		if (max_mem <= MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) {
			memstat_critical_threshold = MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_CRITICAL_PERCENTAGE_SMALL);
		} else {
			memstat_critical_threshold = MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_CRITICAL_PERCENTAGE_LARGE);
		}
	}
	assert3u(memstat_critical_threshold, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	if (memorystatus_idle_threshold_mb != 0) {
		memstat_idle_threshold = atop_32(memorystatus_idle_threshold_mb << 20);
	} else {
		/*
		 * For historical reasons, devices with "medium"-sized memory configs have a different critical:idle:pressure ratio
		 */
		if ((max_mem > MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) &&
		    (max_mem <= MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD)) {
			memstat_idle_threshold = (MEMORYSTATUS_IDLE_RATIO_NUM_MEDIUM * memstat_critical_threshold) /
			    MEMORYSTATUS_IDLE_RATIO_DENOM_MEDIUM;
		} else {
			memstat_idle_threshold = (MEMORYSTATUS_IDLE_RATIO_NUM * memstat_critical_threshold) /
			    MEMORYSTATUS_IDLE_RATIO_DENOM;
		}
	}
	assert3u(memstat_idle_threshold, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	if (memorystatus_pressure_threshold_mb != 0) {
		memstat_soft_threshold = atop_32(memorystatus_pressure_threshold_mb << 20);
	} else {
		if ((max_mem > MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) &&
		    (max_mem <= MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD)) {
			memstat_soft_threshold = (MEMORYSTATUS_PRESSURE_RATIO_NUM_MEDIUM * memstat_critical_threshold) /
			    MEMORYSTATUS_PRESSURE_RATIO_DENOM_MEDIUM;
		} else {
			memstat_soft_threshold = (MEMORYSTATUS_PRESSURE_RATIO_NUM * memstat_critical_threshold) /
			    MEMORYSTATUS_PRESSURE_RATIO_DENOM;
		}
	}
	assert3u(memstat_soft_threshold, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	memstat_reaper_max_priority = MEMORYSTATUS_REAPER_MAX_PRIORITY_DEFAULT;

	if (memorystatus_reaper_threshold_mb != 0) {
		memstat_reaper_threshold = atop_32(memorystatus_reaper_threshold_mb << 20);
	} else {
		if ((max_mem > MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) &&
		    (max_mem <= MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD)) {
			memstat_reaper_threshold = (MEMORYSTATUS_REAPER_RATIO_NUM_MEDIUM * memstat_critical_threshold) /
			    MEMORYSTATUS_REAPER_RATIO_DENOM_MEDIUM;
		} else if (max_mem > MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD) {
			memstat_reaper_threshold = (MEMORYSTATUS_REAPER_RATIO_NUM_LARGE * memstat_critical_threshold) /
			    MEMORYSTATUS_REAPER_RATIO_DENOM_LARGE;
		} else {
			memstat_reaper_threshold = (MEMORYSTATUS_REAPER_RATIO_NUM * memstat_critical_threshold) /
			    MEMORYSTATUS_REAPER_RATIO_DENOM;
		}
	}
	assert3u(memstat_reaper_threshold, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	if (memorystatus_reaper_minimum_age_seconds != MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT) {
		memstat_reaper_min_age_secs = memorystatus_reaper_minimum_age_seconds;
	} else {
		if ((max_mem > MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) &&
		    (max_mem <= MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD)) {
			memstat_reaper_min_age_secs = MEMORYSTATUS_REAPER_MIN_AGE_SECS_DEFAULT_MEDIUM;
		} else {
			memstat_reaper_min_age_secs = MEMORYSTATUS_REAPER_MIN_AGE_SECS_DEFAULT;
		}
	}

	if (memorystatus_reaper_minimum_age_apps_seconds != MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT) {
		memstat_reaper_min_age_apps_secs = memorystatus_reaper_minimum_age_apps_seconds;
	} else {
		if ((max_mem > MEMORYSTATUS_SMALL_MEMORY_THRESHOLD) &&
		    (max_mem <= MEMORYSTATUS_MEDIUM_MEMORY_THRESHOLD)) {
			memstat_reaper_min_age_apps_secs = MEMORYSTATUS_REAPER_MIN_AGE_APPS_SECS_DEFAULT_MEDIUM;
		} else {
			memstat_reaper_min_age_apps_secs = MEMORYSTATUS_REAPER_MIN_AGE_APPS_SECS_DEFAULT;
		}
	}

	if (memorystatus_reaper_rescan_delay_seconds != MEMORYSTATUS_REAPER_SENTINAL_VALUE_MEANING_USE_DEFAULT) {
		memstat_reaper_rescan_secs = memorystatus_reaper_rescan_delay_seconds;
	} else {
		memstat_reaper_rescan_secs = MEMORYSTATUS_REAPER_RESCAN_SECS_DEFAULT;
	}

	memstat_reaper_enabled = memorystatus_reaper_enabled;

	if (memstat_ballast_offset_mb != 0) {
		memstat_ballast_offset = atop_32(memstat_ballast_offset_mb << 20);
	}
	assert3u(memstat_ballast_offset, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	if (memstat_ctd_offset_mb != 0) {
		memstat_ctd_offset = atop_32(memstat_ctd_offset_mb << 20);
	}
	assert3u(memstat_ctd_offset, <, MEMSTAT_PAGE_SHORTAGE_THRESHOLD_MAX);

	/* Set the swapin trigger in pages based on the maximum size allocated for each c_seg */
	memorystatus_swapin_trigger_pages = (unsigned int) atop_64(memorystatus_swapin_trigger_segments * c_seg_allocsize);

	/* Jetsam Loop Detection */
	if (max_mem <= (512 * 1024 * 1024)) {
		/* 512 MB devices */
		memorystatus_jld_eval_period_msecs = 8000;      /* 8000 msecs == 8 second window */
	} else {
		/* 1GB and larger devices */
		memorystatus_jld_eval_period_msecs = 6000;      /* 6000 msecs == 6 second window */
	}

	memorystatus_jld_enabled = TRUE;

	initialize_entitled_max_task_limit();
#endif /* CONFIG_JETSAM */

	memorystatus_jetsam_snapshot_max = maxproc;

	memorystatus_jetsam_snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
	    (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_max);

	memorystatus_jetsam_snapshot = kalloc_data(memorystatus_jetsam_snapshot_size, Z_WAITOK | Z_ZERO);
	if (!memorystatus_jetsam_snapshot) {
		panic("Could not allocate memorystatus_jetsam_snapshot");
	}

#if CONFIG_FREEZE
	memorystatus_jetsam_snapshot_freezer_max = memorystatus_jetsam_snapshot_max / JETSAM_SNAPSHOT_FREEZER_MAX_FACTOR;
	memorystatus_jetsam_snapshot_freezer_size = sizeof(memorystatus_jetsam_snapshot_t) +
	    (sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_freezer_max);

	memorystatus_jetsam_snapshot_freezer =
	    zalloc_permanent(memorystatus_jetsam_snapshot_freezer_size, ZALIGN_PTR);
#endif /* CONFIG_FREEZE */

	nanoseconds_to_absolutetime((uint64_t)JETSAM_SNAPSHOT_TIMEOUT_SECS * NSEC_PER_SEC, &memorystatus_jetsam_snapshot_timeout);

	memset(&memorystatus_at_boot_snapshot, 0, sizeof(memorystatus_jetsam_snapshot_t));

#if CONFIG_FREEZE
	if (memorystatus_freeze_threshold_mb != 0) {
		memorystatus_freeze_threshold = (unsigned int)atop_64((uint64_t)memorystatus_freeze_threshold_mb << 20);
	} else {
		memorystatus_freeze_threshold = (unsigned int)MEMSTAT_PERCENT_TOTAL_PAGES(MEMORYSTATUS_FREEZE_THRESHOLD_PERCENTAGE);
	}
	assert(memorystatus_freeze_threshold < (unsigned int)atop_64(max_mem));

	if (memorystatus_swap_all_apps) {
		/*
		 * Swap is enabled, so we expect a larger working set & larger apps.
		 * Adjust thresholds accordingly.
		 */
		memorystatus_freeze_configure_for_swap();
	}
#endif

	/* Check the boot-arg to configure the maximum number of jetsam threads */
	if (!PE_parse_boot_argn("max_jetsam_threads", &max_jetsam_threads, sizeof(max_jetsam_threads))) {
		max_jetsam_threads = JETSAM_THREADS_LIMIT;
	}

	/* Restrict the maximum number of jetsam threads to JETSAM_THREADS_LIMIT */
	if (max_jetsam_threads > JETSAM_THREADS_LIMIT) {
		max_jetsam_threads = JETSAM_THREADS_LIMIT;
	}

#if CONFIG_JETSAM
	/* For low CPU systems disable fast jetsam mechanism */
	if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) {
		max_jetsam_threads = 1;
	}
#endif /* CONFIG_JETSAM */

#if DEVELOPMENT || DEBUG
	if (PE_parse_boot_argn("-memorystatus-skip-fg-notify", &i, sizeof(i))) {
		memorystatus_should_issue_fg_band_notify = false;
	}

	if (PE_parse_boot_argn("memorystatus_kill_on_sustained_pressure", &i, sizeof(i))) {
		if (i) {
			memstat_pressure_config |= MEMSTAT_WARNING_KILL_SUSTAINED;
		} else {
			memstat_pressure_config &= ~MEMSTAT_WARNING_KILL_SUSTAINED;
		}
	}
#endif /* DEVELOPMENT || DEBUG */

	/* Initialize the jetsam_threads state array */
#if __x86_64__
	(void)zalloc_permanent(sizeof(struct jetsam_state_s) *
	    max_jetsam_threads, ZALIGN(struct jetsam_state_s));
#endif /* __x86_64__ */
	jetsam_threads = zalloc_permanent(sizeof(struct jetsam_state_s) *
	    max_jetsam_threads, ZALIGN(struct jetsam_state_s));
#if __x86_64__
	(void)zalloc_permanent(sizeof(struct jetsam_state_s) *
	    max_jetsam_threads, ZALIGN(struct jetsam_state_s));
#endif /* __x86_64__ */

	/* Initialize all the jetsam threads */
	for (i = 0; i < max_jetsam_threads; i++) {
		jetsam_threads[i].inited = false;
		jetsam_threads[i].index = i;
		result = kernel_thread_start_priority(memorystatus_thread, NULL, 95 /* MAXPRI_KERNEL */, &jetsam_threads[i].thread);
		if (result != KERN_SUCCESS) {
			panic("Could not create memorystatus_thread %d", i);
		}
		thread_deallocate(jetsam_threads[i].thread);
	}

#if VM_PRESSURE_EVENTS
	memorystatus_notify_init();
#endif /* VM_PRESSURE_EVENTS */

#if JETSAM_ZPRINT_SNAPSHOT
	size_t jzs_names_size, jzs_info_size, jzs_meminfo_size;

	jzs_zone_cnt = zone_max_zones();
	jzs_names_size = jzs_zone_cnt * sizeof(mach_zone_name_t);
	jzs_names = zalloc_permanent(jzs_names_size, ZALIGN(mach_zone_name_t));

	jzs_info_size = jzs_zone_cnt * sizeof(mach_zone_info_t);
	jzs_info = zalloc_permanent(jzs_info_size, ZALIGN(mach_zone_info_t));

	jzs_coalesce = zalloc_permanent(jzs_zone_cnt * sizeof(int), ZALIGN(int));

	jzs_meminfo_cnt = vm_page_diagnose_estimate();
	jzs_meminfo_size = jzs_meminfo_cnt * sizeof(mach_memory_info_t);
	jzs_meminfo = kalloc_data_tag(jzs_meminfo_size, Z_WAITOK, VM_KERN_MEMORY_DIAG);
#endif /* JETSAM_ZPRINT_SNAPSHOT */

	bzero(memorystatus_kill_counts, sizeof(memorystatus_kill_counts));
}

#if CONFIG_JETSAM
bool
memorystatus_disable_swap(void)
{
#if DEVELOPMENT || DEBUG
	int boot_arg_val = 0;
	if (PE_parse_boot_argn("kern.swap_all_apps", &boot_arg_val, sizeof(boot_arg_val))) {
		if (boot_arg_val) {
			/* Can't disable app swap if it was set via a boot-arg */
			return false;
		}
	}
#endif /* DEVELOPMENT || DEBUG */
	memorystatus_swap_all_apps = false;
#if CONFIG_FREEZE
	/* Go back to the smaller freezer thresholds */
	memorystatus_freeze_disable_swap();
#endif /* CONFIG_FREEZE */
	initialize_entitled_max_task_limit();
	return true;
}
#endif /* CONFIG_JETSAM */

static void
_memstat_record_kill(int32_t priority, memorystatus_kill_cause_t cause)
{
	uint32_t _Atomic *count;
	uint32_t orig;

	/* Check validity of reason / cause */
	if ((priority < JETSAM_PRIORITY_IDLE) ||
	    (priority > JETSAM_PRIORITY_MAX) ||
	    (cause <= kMemorystatusInvalid) ||
	    (cause > JETSAM_REASON_MEMORYSTATUS_MAX)) {
		memorystatus_log_error("memorystatus: not tracking kill with invalid priority %d / cause %d\n",
		    priority, cause);
		return;
	}

	if ((priority == JETSAM_PRIORITY_IDLE) && (cause == kMemorystatusKilledIdleExit)) {
		/* rdar://141462516 */
		count = &memorystatus_idle_exit_kill_count;
	} else {
		if (cause == kMemorystatusKilledIdleExit) {
			memorystatus_log_error("memorystatus: not tracking idle exit kill for priority %d\n", priority);
			return;
		}

		/* kMemorystatusKilledIdleExit and kMemorystatusInvalid are not in the array */
		if (cause < kMemorystatusKilledIdleExit) {
			count = &memorystatus_kill_counts[priority][cause - 1];
		} else {
			count = &memorystatus_kill_counts[priority][cause - 2];
		}
	}

	orig = os_atomic_inc_orig(count, relaxed);
	if (orig == UINT32_MAX) {
		os_atomic_dec(count, relaxed);
		memorystatus_log_error("memorystatus: overflowed kill count for priority %d + cause %d\n", priority, cause);
	}
}

/*
 * The jetsam no frills kill call
 *      Return: 0 on success
 *		error code on failure (EINVAL...)
 */
static int
jetsam_do_kill(proc_t p, int jetsam_flags, os_reason_t jetsam_reason)
{
	int error = 0;
	error = exit_with_reason(p, W_EXITCODE(0, SIGKILL), (int *)NULL, FALSE, FALSE, jetsam_flags, jetsam_reason);
	return error;
}

/*
 * Wrapper for processes exiting with memorystatus details
 */
static bool
memorystatus_do_kill(proc_t p, uint32_t cause, os_reason_t jetsam_reason, uint64_t *footprint_out)
{
	int error = 0;
	__unused pid_t victim_pid = proc_getpid(p);
	uint64_t footprint = get_task_phys_footprint(proc_task(p));
#if (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD)
	int32_t memstat_effectivepriority = p->p_memstat_effectivepriority;
#endif /* (KDEBUG_LEVEL >= KDEBUG_LEVEL_STANDARD) */

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_DO_KILL) | DBG_FUNC_START,
	    victim_pid, cause, vm_page_free_count, footprint);
	DTRACE_MEMORYSTATUS4(memorystatus_do_kill, proc_t, p, os_reason_t, jetsam_reason, uint32_t, cause, uint64_t, footprint);

#if CONFIG_JETSAM
	if (*p->p_name && !strncmp(memorystatus_jetsam_proc_name_panic, p->p_name, sizeof(p->p_name))) { /* name */
		if ((!memorystatus_jetsam_proc_cause_panic || cause == memorystatus_jetsam_proc_cause_panic) && /* cause */
		    (!memorystatus_jetsam_proc_size_panic || (footprint >> 20) >= memorystatus_jetsam_proc_size_panic)) { /* footprint */
			panic("memorystatus_do_kill(): requested panic on jetsam of %s (cause: %d and footprint: %llu mb)",
			    memorystatus_jetsam_proc_name_panic, cause, footprint >> 20);
		}
	}
#else /* CONFIG_JETSAM */
#pragma unused(cause)
#endif /* CONFIG_JETSAM */

	if (p->p_memstat_effectivepriority >= JETSAM_PRIORITY_FOREGROUND) {
		memorystatus_log(
			"memorystatus: killing process %d [%s] in high band %s (%d) - memorystatus_available_pages: %llu\n",
			proc_getpid(p), (*p->p_name ? p->p_name : "unknown"),
			memorystatus_priority_band_name(p->p_memstat_effectivepriority), p->p_memstat_effectivepriority,
			(uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES);
	}

	_memstat_record_kill(p->p_memstat_effectivepriority, cause);

	/*
	 * The jetsam_reason (os_reason_t) has enough information about the kill cause.
	 * We don't really need jetsam_flags anymore, so it's okay that not all possible kill causes have been mapped.
	 */
	int jetsam_flags = P_LTERM_JETSAM;
	switch (cause) {
	case kMemorystatusKilledHiwat:                                          jetsam_flags |= P_JETSAM_HIWAT; break;
	case kMemorystatusKilledVnodes:                                         jetsam_flags |= P_JETSAM_VNODE; break;
	case kMemorystatusKilledVMPageShortage:                         jetsam_flags |= P_JETSAM_VMPAGESHORTAGE; break;
	case kMemorystatusKilledVMCompressorThrashing:
	case kMemorystatusKilledVMCompressorSpaceShortage:      jetsam_flags |= P_JETSAM_VMTHRASHING; break;
	case kMemorystatusKilledFCThrashing:                            jetsam_flags |= P_JETSAM_FCTHRASHING; break;
	case kMemorystatusKilledPerProcessLimit:                        jetsam_flags |= P_JETSAM_PID; break;
	case kMemorystatusKilledIdleExit:                                       jetsam_flags |= P_JETSAM_IDLEEXIT; break;
	case kMemorystatusKilledConclaveLimit:                        jetsam_flags |= P_JETSAM_PID; break;
	}
	/* jetsam_do_kill drops a reference. */
	os_reason_ref(jetsam_reason);
	error = jetsam_do_kill(p, jetsam_flags, jetsam_reason);
	if (footprint_out) {
		*footprint_out = ((error == 0) ? footprint : 0);
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_DO_KILL) | DBG_FUNC_END,
	    victim_pid, memstat_effectivepriority, vm_page_free_count, error);

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_COMPACTOR_RUN) | DBG_FUNC_START,
	    victim_pid, cause, vm_page_free_count, footprint);

	if (jetsam_reason->osr_code == JETSAM_REASON_VNODE) {
		/*
		 * vnode jetsams are syncronous and not caused by memory pressure.
		 * Running the compactor on this thread adds significant latency to the filesystem operation
		 * that triggered this jetsam.
		 * Kick of compactor thread asyncronously instead.
		 */
		vm_wake_compactor_swapper();
	} else {
		/* compact now, except for idle reaper kills.
		 * idle reaper kills are done in batches, so we defer compaction until the end of the batch.
		 */
		if (jetsam_reason->osr_code != JETSAM_REASON_MEMORY_LONGIDLE_EXIT) {
			vm_run_compactor();
		}
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_COMPACTOR_RUN) | DBG_FUNC_END,
	    victim_pid, cause, vm_page_free_count);

	os_reason_free(jetsam_reason);
	return error == 0;
}

static int
memstat_update_inactive_priority(proc_t  p, boolean_t enable, int jetsam_prio, boolean_t effective_now)
{
	if (_memstat_proc_is_internal(p)) {
		return EINVAL;
	}

	if ((enable && _memstat_proc_is_elevated(p)) ||
	    (!enable && !_memstat_proc_is_elevated(p))) {
		/*
		 * No change in state.
		 */
	} else {
		proc_list_lock();

		if (enable) {
			p->p_memstat_state |= P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND;

			if (effective_now) {
				if (p->p_memstat_effectivepriority < jetsam_prio) {
					memstat_update_priority_locked(p, jetsam_prio, MEMSTAT_PRIORITY_OPTIONS_NONE);
				}
			} else {
				if (_memstat_proc_is_aging(p)) {
					memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE, MEMSTAT_PRIORITY_NO_AGING);
				}
			}
		} else {
			p->p_memstat_state &= ~P_MEMSTAT_USE_ELEVATED_INACTIVE_BAND;

			if (effective_now) {
				if (p->p_memstat_effectivepriority == jetsam_prio) {
					memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE, MEMSTAT_PRIORITY_NO_AGING);
				}
			} else {
				if (_memstat_proc_is_aging(p)) {
					memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE, MEMSTAT_PRIORITY_NO_AGING);
				}
			}
		}
		proc_list_unlock();
	}
	return 0;
}

/*
 * Pin a process to a particular jetsam band when it is in the background i.e. not doing active work.
 * For an application: that means no longer in the FG band
 * For a daemon: that means no longer in its 'requested' jetsam priority band
 */

int
memorystatus_update_inactive_jetsam_priority_band(pid_t pid, uint32_t op_flags, int jetsam_prio, boolean_t effective_now)
{
	int error = 0;
	boolean_t enable = FALSE;
	proc_t  p = NULL;

	/* Validate inputs */
	if (pid == 0) {
		return EINVAL;
	}

	if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE) {
		enable = TRUE;
	} else if (op_flags == MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE) {
		enable = FALSE;
	} else {
		return EINVAL;
	}

	p = proc_find(pid);
	if (p != NULL) {
		error = memstat_update_inactive_priority(p, enable, jetsam_prio, effective_now);
		proc_rele(p);
	} else {
		error = ESRCH;
	}
	return error;
}

static bool
_memstat_proc_has_importance_assertion(proc_t p)
{
	return (p->p_memstat_state & P_MEMSTAT_TEST_IMP_ASSERTION) || task_has_assertions(proc_task(p));
}

static void
_memstat_perform_idle_demotion_for_band(unsigned int demote_prio_band)
{
	proc_t p;
	uint64_t current_time = 0, idle_delay_time = 0;
	memstat_bucket_t *demotion_bucket;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	current_time = mach_absolute_time();

	demotion_bucket = &memstat_bucket[demote_prio_band];
	p = memorystatus_get_first_proc_locked(&demote_prio_band, FALSE);

	while (p) {
		memorystatus_log_debug("memorystatus_perform_idle_demotion() found %s [%d]\n", proc_best_name(p), proc_getpid(p));

		assert(p->p_memstat_idledeadline);

		assert(_memstat_proc_is_aging(p));

		if (current_time >= p->p_memstat_idledeadline) {
			proc_t next_proc = NULL;

			next_proc = memorystatus_get_next_proc_locked(&demote_prio_band, p, FALSE);

			if ((isSysProc(p) && _memstat_proc_is_dirty(p)) || /* system proc marked dirty*/
			    _memstat_proc_has_importance_assertion(p)) {    /* has outstanding assertions which might indicate outstanding work too */
				idle_delay_time = (isSysProc(p)) ? memorystatus_sysprocs_idle_time(p) : memorystatus_apps_idle_time(p);

				if (isSysProc(p) && _memstat_proc_has_importance_assertion(p)) {
					if (demote_prio_band != system_procs_aging_band_stuck) {
						memorystatus_log_debug("memorystatus_perform_idle_demotion() found stuck process %d [%s], moving to JETSAM_PRIORITY_AGING_BAND1_STUCK\n",
						    proc_getpid(p), (*p->p_name ? p->p_name : "(unknown)"));
						memstat_update_priority_locked(p, JETSAM_PRIORITY_AGING_BAND1_STUCK, MEMSTAT_PRIORITY_NO_AGING);
						idle_delay_time = _memstat_sysprocs_aging_stuck_delay_time(p);
						KDBG(MEMSTAT_CODE(BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_NONE,
						    proc_pid(p), JETSAM_PRIORITY_AGING_BAND1_STUCK, p->p_memstat_idledeadline + idle_delay_time);
					} else {
						memorystatus_log("memorystatus_perform_idle_demotion() timed out stuck process %d [%s], moving to idle band\n",
						    proc_getpid(p), proc_best_name(p));
						memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE, MEMSTAT_PRIORITY_NO_AGING);
						KDBG(MEMSTAT_CODE(BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_NONE,
						    proc_pid(p), JETSAM_PRIORITY_IDLE, p->p_memstat_idledeadline);
						idle_delay_time = 0;
					}
				}

				p->p_memstat_idledeadline += idle_delay_time;
			} else {
				memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE, MEMSTAT_PRIORITY_NO_AGING);
				KDBG(MEMSTAT_CODE(BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_NONE,
				    proc_pid(p), JETSAM_PRIORITY_IDLE, p->p_memstat_idledeadline);
			}
			p = next_proc;
		} else {
			// No further candidates
			break;
		}
	}
}

static void
memorystatus_perform_idle_demotion(__unused void *spare1, __unused void *spare2)
{
	memorystatus_log_debug("memorystatus_perform_idle_demotion()\n");

	if (!system_procs_aging_band && !system_procs_aging_band_stuck && !applications_aging_band) {
		return;
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_START);

	proc_list_lock();

	_memstat_perform_idle_demotion_for_band(system_procs_aging_band);
	_memstat_perform_idle_demotion_for_band(system_procs_aging_band_stuck);
	_memstat_perform_idle_demotion_for_band(applications_aging_band);

	_memstat_reschedule_idle_demotion_locked();

	proc_list_unlock();

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_IDLE_DEMOTE) | DBG_FUNC_END);
}

/*
 * Schedule a process for idle demotion. Updates the process' idle deadline
 * and marks it as aging. The caller is responsible for rescheduling the idle
 * demotion thread
 */
static void
_memstat_schedule_idle_demotion_locked(proc_t p)
{
	uint64_t  idle_delay_time = 0;
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(system_procs_aging_band || applications_aging_band);
	assert(!_memstat_proc_is_aging(p));

	memorystatus_log_debug(
		"%s: scheduling demotion to idle band for pid %d (dirty:0x%x).\n",
		__func__, proc_getpid(p), p->p_memstat_dirty);

	idle_delay_time = isSysProc(p) ? memorystatus_sysprocs_idle_time(p) :
	    memorystatus_apps_idle_time(p);
	p->p_memstat_dirty |= P_DIRTY_AGING_IN_PROGRESS;
	p->p_memstat_idledeadline = mach_absolute_time() + idle_delay_time;
}

/*
 * Cancel a process' idle demotion. The caller must also reschedule the idle
 * demotion thread.
 */
static void
_memstat_invalidate_idle_demotion_locked(proc_t p)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(system_procs_aging_band || applications_aging_band);
	assert(_memstat_proc_is_aging(p));

	memorystatus_log_debug(
		"%s: invalidating demotion to idle band for %s [%d]\n",
		__func__, proc_best_name(p), proc_getpid(p));

	p->p_memstat_idledeadline = 0;
	p->p_memstat_dirty &= ~P_DIRTY_AGING_IN_PROGRESS;
}

/*
 * Return the earliest idle deadline of all aging procs. Returns 0 if there are
 * no aging procs.
 */
static uint64_t
_memstat_find_earliest_idle_deadline(void)
{
	memstat_bucket_t *demotion_bucket;
	proc_t oldest_proc = PROC_NULL;
	uint32_t aging_app_count = 0, aging_sysproc_count = 0, aging_sysproc_count_stuck = 0;
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(system_procs_aging_band || system_procs_aging_band_stuck || applications_aging_band);

	if (system_procs_aging_band) {
		aging_sysproc_count = memstat_bucket[system_procs_aging_band].count;
	}
	if (system_procs_aging_band_stuck) {
		aging_sysproc_count_stuck = memstat_bucket[system_procs_aging_band_stuck].count;
	}
	if (applications_aging_band) {
		aging_app_count = memstat_bucket[applications_aging_band].count;
	}

	if ((aging_app_count + aging_sysproc_count + aging_sysproc_count_stuck) == 0) {
		return 0;
	}

	if (system_procs_aging_band && aging_sysproc_count > 0) {
		demotion_bucket = &memstat_bucket[system_procs_aging_band];
		oldest_proc = TAILQ_FIRST(&demotion_bucket->list);
	}

	if (system_procs_aging_band_stuck && aging_sysproc_count_stuck > 0) {
		proc_t oldest_sysproc_stuck;
		demotion_bucket = &memstat_bucket[system_procs_aging_band_stuck];
		oldest_sysproc_stuck = TAILQ_FIRST(&demotion_bucket->list);

		if (oldest_proc) {
			if (oldest_sysproc_stuck->p_memstat_idledeadline <
			    oldest_proc->p_memstat_idledeadline) {
				oldest_proc = oldest_sysproc_stuck;
			}
		} else {
			oldest_proc = oldest_sysproc_stuck;
		}
	}

	if (applications_aging_band && aging_app_count > 0) {
		proc_t oldest_app;
		demotion_bucket = &memstat_bucket[applications_aging_band];
		oldest_app = TAILQ_FIRST(&demotion_bucket->list);

		if (!oldest_proc ||
		    (oldest_app->p_memstat_idledeadline <
		    oldest_proc->p_memstat_idledeadline)) {
			oldest_proc = oldest_app;
		}
	}

	assert(oldest_proc);
	assert(oldest_proc->p_memstat_idledeadline);
	assert(_memstat_proc_is_aging(oldest_proc));

	return oldest_proc->p_memstat_idledeadline;
}

/*
 * Reschedule or cancel a pending wakeup of the idle_demotion thread. If called
 * in response to a process transitioning in/out of the aging band, then
 * rescheduling must occur *after* the new priority is updated.
 */
static void
_memstat_reschedule_idle_demotion_locked(void)
{
	uint64_t idle_deadline;
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	if (!system_procs_aging_band && !applications_aging_band) {
		return;
	}
	idle_deadline = _memstat_find_earliest_idle_deadline();
	if (idle_deadline == 0) {
		/* No aging processes, cancel call to demotion thread */
		thread_call_cancel(memorystatus_idle_demotion_call);
	} else if (memstat_idle_demotion_deadline != idle_deadline) {
		thread_call_enter_delayed(memorystatus_idle_demotion_call, idle_deadline);
	}
	memstat_idle_demotion_deadline = idle_deadline;
}

/*
 * List manipulation
 */

int
memorystatus_add(proc_t p, boolean_t locked)
{
	memstat_bucket_t *bucket;
	bool reschedule_demotion = false;

	memorystatus_log_debug("memorystatus_list_add(): adding pid %d with priority %d.\n",
	    proc_getpid(p), p->p_memstat_effectivepriority);

	if (!locked) {
		proc_list_lock();
	}

	DTRACE_MEMORYSTATUS2(memorystatus_add, proc_t, p, int32_t, p->p_memstat_effectivepriority);

	/* Processes marked internal do not have priority tracked */
	if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
		goto exit;
	}

#if CONFIG_FREEZE
	/*
	 * Opt out system processes from being frozen by default.
	 * For coalition-based freezing, we only want to freeze sysprocs that have specifically opted in.
	 */
	if (isSysProc(p) && !memorystatus_freeze_daemons_all_allowed) {
		p->p_memstat_state |= P_MEMSTAT_FREEZE_DISABLED;
	}
	memorystatus_freeze_init_proc(p);
#endif

	bucket = &memstat_bucket[p->p_memstat_effectivepriority];

	if ((system_procs_aging_band &&
	    p->p_memstat_effectivepriority == system_procs_aging_band) ||
	    (applications_aging_band &&
	    p->p_memstat_effectivepriority == applications_aging_band)) {
		_memstat_schedule_idle_demotion_locked(p);
		reschedule_demotion = true;
	}

	p->p_memstat_prio_start = mach_absolute_time();

	TAILQ_INSERT_TAIL(&bucket->list, p, p_memstat_list);
	bucket->count++;
	if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) {
		bucket->relaunch_high_count++;
	}

	memorystatus_list_count++;

	if (reschedule_demotion) {
		_memstat_reschedule_idle_demotion_locked();
	}

	task_t t = proc_task(p);
	if (t && task_is_app_suspended(t)) {
		_memstat_proc_set_suspended(p);
	}

	_memstat_consider_waking_jetsam_thread();

exit:
	if (!locked) {
		proc_list_unlock();
	}

	return 0;
}

void
memorystatus_set_proc_entitlement_flags(proc_t p)
{
	static const char kInternalJetsamRangeEntitlement[] = "com.apple.private.internal-jetsam-range";
	task_t t = proc_task(p);
	assert(t != TASK_NULL);
	if (IOTaskHasEntitlement(t, kInternalJetsamRangeEntitlement)) {
		p->p_memstat_state |= P_MEMSTAT_INTERNAL_RANGE;
	}
}

/*
 * Record timestamps if process p is transitioning in/out of the IDLE band.
 */
static void
_memstat_record_prio_transition(proc_t p, int new_priority)
{
	uint64_t now;

	if (p->p_memstat_effectivepriority == new_priority) {
		/* no change in priority */
		return;
	}

	now = mach_absolute_time();

	if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) {
		/*
		 * Transitioning out of the idle priority bucket.
		 * Record idle delta.
		 */
		assert(p->p_memstat_prio_start != 0);
		if (now < p->p_memstat_prio_start) {
			// rdar://139660508
			memorystatus_log_error("memorystatus: prio_start > mach_absolute_time "
			    "for %s(%d)? Using delta of 0.\n",
			    proc_best_name(p), proc_getpid(p));
			p->p_memstat_prio_start = now;
		}
		p->p_memstat_idle_delta = now - p->p_memstat_prio_start;

		/*
		 * About to become active and so memory footprint could change.
		 * So mark it eligible for freeze-considerations next time around.
		 */
		p->p_memstat_state &= ~P_MEMSTAT_FREEZE_IGNORE;

		_memstat_consider_waking_jetsam_thread();
	}
	p->p_memstat_prio_start = now;
}

/*
 * Everything between the idle band and the application agining band
 * are reserved for internal use. We allow some entitled user space programs
 * to use this range for experimentation.
 */
static bool
_memstat_proc_can_use_entitled_range(proc_t proc)
{
	if (proc == PROC_NULL) {
		return false;
	}
	return proc->p_memstat_state & P_MEMSTAT_INTERNAL_RANGE;
}

/*
 * Description:
 *	Moves a process from one jetsam bucket to another.
 *	which changes the LRU position of the process.
 *
 *	Monitors transition between buckets and if necessary
 *	will update cached memory limits accordingly.
 *
 */
void
memstat_update_priority_locked(proc_t p,
    int priority,
    memstat_priority_options_t options)
{
	memstat_bucket_t *old_bucket, *new_bucket;
	bool reschedule_demotion = false;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	assert(priority < MEMSTAT_BUCKET_COUNT);
	/* Not allowed */
	assert(!_memstat_proc_is_internal(p));

	/* Ensure that exit isn't underway, leaving the proc retained but removed from its bucket */
	if (proc_list_exited(p)) {
		return;
	}

	memorystatus_log_debug("memorystatus: setting %s(%d) to priority %d, inserting at %s\n",
	    (*p->p_name ? p->p_name : "unknown"), proc_getpid(p), priority,
	    (options & MEMSTAT_PRIORITY_INSERT_HEAD) ? "head" : "tail");

	DTRACE_MEMORYSTATUS3(memorystatus_update_priority, proc_t, p, int32_t, p->p_memstat_effectivepriority, int, priority);

	old_bucket = &memstat_bucket[p->p_memstat_effectivepriority];

	if (priority == JETSAM_PRIORITY_IDLE &&
	    !(_memstat_proc_can_idle_exit(p) && !_memstat_proc_is_dirty(p)) &&
	    !(_memstat_proc_is_managed(p) && !_memstat_proc_has_priority_assertion(p)) &&
	    !_memstat_proc_can_use_entitled_range(p)) {
		priority = JETSAM_PRIORITY_BACKGROUND;
		memorystatus_log_error("memorystatus: %s [%d] is neither "
		    "clean (0x%x) nor assertion-less (0x%x) and cannot "
		    "therefore be idle - overriding to pri %d\n",
		    proc_best_name(p), proc_getpid(p), p->p_memstat_dirty,
		    p->p_memstat_state, priority);
	}

	if (!(options & MEMSTAT_PRIORITY_NO_AGING)) {
		if (_memstat_proc_is_elevated(p)) {
			/*
			 * 2 types of processes can use the non-standard elevated inactive band:
			 * - Frozen processes that always land in memorystatus_freeze_jetsam_band
			 * OR
			 * - processes that specifically opt-in to the elevated inactive support e.g. docked processes.
			 */
			if (_memstat_proc_is_frozen(p) &&
			    priority <= memorystatus_freeze_jetsam_band) {
				priority = memorystatus_freeze_jetsam_band;
			} else if (priority <= JETSAM_PRIORITY_ELEVATED_INACTIVE) {
				priority = JETSAM_PRIORITY_ELEVATED_INACTIVE;
			}
		}
		if (_memstat_proc_is_tracked(p)) {
			if (system_procs_aging_band && priority <= system_procs_aging_band) {
				if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) {
					/* process has already aged */
					priority = JETSAM_PRIORITY_IDLE;
				} else {
					priority = system_procs_aging_band;
				}
			} else if (system_procs_aging_band_stuck && priority <= system_procs_aging_band_stuck) {
				if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) {
					/* process has already aged */
					priority = JETSAM_PRIORITY_IDLE;
				} else {
					/* don't let anyone move anything between sysproc and sysproc stuck inclusive */
					priority = system_procs_aging_band;
				}
			}
		} else if (_memstat_proc_is_managed(p)) {
			if (applications_aging_band && priority <= applications_aging_band) {
				if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) {
					/* process has already aged */
					priority = JETSAM_PRIORITY_IDLE;
				} else {
					priority = applications_aging_band;
				}
			}
		}
	}

	TAILQ_REMOVE(&old_bucket->list, p, p_memstat_list);
	old_bucket->count--;
	if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) {
		old_bucket->relaunch_high_count--;
	}

	new_bucket = &memstat_bucket[priority];
	if (options & MEMSTAT_PRIORITY_INSERT_HEAD) {
		TAILQ_INSERT_HEAD(&new_bucket->list, p, p_memstat_list);
	} else {
		TAILQ_INSERT_TAIL(&new_bucket->list, p, p_memstat_list);
	}
	new_bucket->count++;
	if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) {
		new_bucket->relaunch_high_count++;
	}

	if (p->p_memstat_effectivepriority != priority) {
		/*
		 * This process is transitioning between
		 * jetsam priority buckets.
		 */
		_memstat_record_prio_transition(p, priority);

		if ((system_procs_aging_band &&
		    p->p_memstat_effectivepriority == system_procs_aging_band) ||
		    (system_procs_aging_band_stuck &&
		    p->p_memstat_effectivepriority == system_procs_aging_band_stuck) ||
		    (applications_aging_band &&
		    p->p_memstat_effectivepriority == applications_aging_band)) {
			/* removing this process from an aging band */
			_memstat_invalidate_idle_demotion_locked(p);
			reschedule_demotion = true;
		}

		if ((system_procs_aging_band &&
		    priority == system_procs_aging_band) ||
		    (system_procs_aging_band_stuck &&
		    priority == system_procs_aging_band_stuck) ||
		    (applications_aging_band &&
		    priority == applications_aging_band)) {
			/* placing this process into an aging band */
			_memstat_schedule_idle_demotion_locked(p);
			reschedule_demotion = true;
		}

		if (reschedule_demotion) {
			_memstat_reschedule_idle_demotion_locked();
		}

		KDBG(MEMSTAT_CODE(BSD_MEMSTAT_CHANGE_PRIORITY) | DBG_FUNC_NONE,
		    proc_getpid(p), priority, p->p_memstat_effectivepriority);
		p->p_memstat_effectivepriority = priority;
	}

	if (memorystatus_highwater_enabled) {
		const bool use_active = memstat_proc_is_active_locked(p);
		if (memstat_update_memlimit_locked(p, use_active)) {
			_memstat_write_memlimit_to_ledger_locked(p, use_active, false);
		}
	}

#if CONFIG_SECLUDED_MEMORY
	if (secluded_for_apps &&
	    task_could_use_secluded_mem(proc_task(p))) {
		task_set_can_use_secluded_mem(
			proc_task(p),
			(priority >= JETSAM_PRIORITY_FOREGROUND));
	}
#endif /* CONFIG_SECLUDED_MEMORY */

	_memstat_consider_waking_jetsam_thread();
}

int
memorystatus_relaunch_flags_update(proc_t p, int relaunch_flags)
{
	p->p_memstat_relaunch_flags = relaunch_flags;
	KDBG(BSDDBG_CODE(DBG_BSD_MEMSTAT, BSD_MEMSTAT_RELAUNCH_FLAGS), proc_getpid(p), relaunch_flags);
	return 0;
}

#if DEVELOPMENT || DEBUG
static int sysctl_memorystatus_relaunch_flags SYSCTL_HANDLER_ARGS {
#pragma unused(oidp, arg1, arg2)
	proc_t p;
	int relaunch_flags = 0;

	p = current_proc();
	relaunch_flags = p->p_memstat_relaunch_flags;
	switch (relaunch_flags) {
	case P_MEMSTAT_RELAUNCH_LOW:
		relaunch_flags = POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_LOW;
		break;
	case P_MEMSTAT_RELAUNCH_MED:
		relaunch_flags = POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_MED;
		break;
	case P_MEMSTAT_RELAUNCH_HIGH:
		relaunch_flags = POSIX_SPAWN_JETSAM_RELAUNCH_BEHAVIOR_HIGH;
		break;
	}

	return SYSCTL_OUT(req, &relaunch_flags, sizeof(relaunch_flags));
}
SYSCTL_PROC(_kern, OID_AUTO, memorystatus_relaunch_flags, CTLTYPE_INT | CTLFLAG_RD |
    CTLFLAG_LOCKED | CTLFLAG_MASKED, 0, 0, sysctl_memorystatus_relaunch_flags, "I", "get relaunch flags for current process");
#endif /* DEVELOPMENT || DEBUG */

/*
 * Set a process' requested priority band. This is the entry point used during
 * spawn and by memorystatus_control.
 */
int
memorystatus_set_priority(proc_t p, int priority, uint64_t user_data,
    memstat_priority_options_t options)
{
	int ret;

	memorystatus_log_debug("memorystatus: changing (%s) pid %d: priority %d, user_data 0x%llx\n",
	    (*p->p_name ? p->p_name : "unknown"), proc_getpid(p), priority, user_data);

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_UPDATE) | DBG_FUNC_START, proc_getpid(p), priority, user_data, options);

	if (priority == -1) {
		/* Use as shorthand for default priority */
		priority = JETSAM_PRIORITY_DEFAULT;
	} else if (priority > JETSAM_PRIORITY_IDLE && priority <= JETSAM_PRIORITY_AGING_BAND2) {
		/*
		 * Everything between idle and the aging bands are reserved for internal use.
		 * if requested, adjust to JETSAM_PRIORITY_IDLE.
		 * Entitled processes (just munch) can use a subset of this range for testing.
		 */
		if (priority > JETSAM_PRIORITY_ENTITLED_MAX ||
		    !_memstat_proc_can_use_entitled_range(current_proc())) {
			priority = JETSAM_PRIORITY_IDLE;
			options |= MEMSTAT_PRIORITY_NO_AGING;
		}
	} else if (priority == JETSAM_PRIORITY_IDLE_HEAD) {
		/* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle queue */
		priority = JETSAM_PRIORITY_IDLE;
		options |= (MEMSTAT_PRIORITY_INSERT_HEAD | MEMSTAT_PRIORITY_NO_AGING);
	} else if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) {
		/* Sanity check */
		ret = EINVAL;
		goto out;
	}

	proc_list_lock();

	assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));

	if ((options & MEMSTAT_PRIORITY_IS_EFFECTIVE) &&
	    (p->p_memstat_state & P_MEMSTAT_PRIORITYUPDATED)) {
		ret = EALREADY;
		proc_list_unlock();
		memorystatus_log_error("memorystatus_update: effective change specified for pid %d, but change already occurred.\n",
		    proc_getpid(p));
		goto out;
	}

	if ((p->p_memstat_state & (P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP)) || proc_list_exited(p)) {
		/*
		 * This could happen when a process calling posix_spawn() is exiting on the jetsam thread.
		 */
		ret = EBUSY;
		proc_list_unlock();
		goto out;
	}

	p->p_memstat_state |= P_MEMSTAT_PRIORITYUPDATED;
	p->p_memstat_userdata = user_data;

	if ((options & MEMSTAT_PRIORITY_IS_ASSERTION)) {
		if (priority != JETSAM_PRIORITY_IDLE) {
			/*
			 * Process is now being managed by assertions,
			 */
			p->p_memstat_state |= P_MEMSTAT_PRIORITY_ASSERTION;
			p->p_memstat_assertionpriority = priority;
		} else if (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION) {
			/*
			 * Assertions relinquish control when the process is heading to IDLE.
			 */
			p->p_memstat_state &= ~P_MEMSTAT_PRIORITY_ASSERTION;
		}

		if (_memstat_proc_is_tracked(p) &&
		    (_memstat_proc_is_dirty(p) || !_memstat_proc_can_idle_exit(p))) {
			priority = MAX(p->p_memstat_assertionpriority,
			    p->p_memstat_requestedpriority);
		}
	} else {
		p->p_memstat_requestedpriority = priority;
	}

	memstat_update_priority_locked(p, priority, options);

	proc_list_unlock();
	ret = 0;

out:
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_UPDATE) | DBG_FUNC_END, ret);

	return ret;
}

#if DEVELOPMENT || DEBUG
static int32_t
memstat_increased_limit(int32_t limit, int32_t increase)
{
	int32_t offset_limit;
	if (limit <= 0) {
		return 0;
	}
	if (os_add_overflow(limit, increase, &offset_limit)) {
		return INT32_MAX;
	}
	return offset_limit;
}
#endif /* DEVELOPMENT || DEBUG */

static int
memstat_set_memlimits_locked(proc_t p, int32_t active_limit,
    int32_t inactive_limit, memlimit_options_t options)
{
	/*
	 * Posix_spawn'd processes and managed processes come through this path to
	 * instantiate ledger limits. Forked processes do not come through this
	 * path and will always receive the default task limit.
	 */

	int err = 0;
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	int32_t default_active_limit = memorystatus_get_default_task_active_limit(p);
	int32_t default_inactive_limit = memorystatus_get_default_task_inactive_limit(p);

	/*
	 * The special value of -1 specifies that this proc wants the default
	 * memory limit
	 */
	if (active_limit <= 0) {
		active_limit = default_active_limit;
	}
	if (inactive_limit <= 0) {
		inactive_limit = default_inactive_limit;
	}

#if DEVELOPMENT || DEBUG
	if (p->p_memlimit_increase) {
		/* Apply memlimit increase (for testing with overlay roots) */
		int32_t memlimit_increase = roundToNearestMB(p->p_memlimit_increase);
		active_limit = memstat_increased_limit(active_limit, memlimit_increase);
		inactive_limit = memstat_increased_limit(inactive_limit, memlimit_increase);
	}
#endif /* DEVELOPMENT || DEBUG */

	/*
	 * Work around a bug in JetsamProperties whereby processes may mistakenly receive
	 * ActiveSoftMemoryLimit := -1 by forcing the default task limit to be fatal.
	 */
	if (default_active_limit && active_limit == default_active_limit) {
		options |= MEMLIMIT_ACTIVE_FATAL;
	}

	if (default_inactive_limit && inactive_limit == default_inactive_limit) {
		options |= MEMLIMIT_INACTIVE_FATAL;
	}

	memorystatus_log_debug(
		"memorystatus: setting memlimit for %s [%d], "
		"Active(%dMB %s), Inactive(%dMB, %s)\n",
		proc_best_name(p), proc_getpid(p),
		active_limit, ((options & MEMLIMIT_ACTIVE_FATAL) ? "F" : "NF"),
		inactive_limit, ((options & MEMLIMIT_INACTIVE_FATAL) ? "F" : "NF"));

	p->p_memstat_memlimit_active = active_limit;
	p->p_memstat_memlimit_inactive = inactive_limit;
	if (options & MEMLIMIT_INACTIVE_FATAL) {
		p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL;
	} else {
		p->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_INACTIVE_FATAL;
	}
	if (options & MEMLIMIT_ACTIVE_FATAL) {
		p->p_memstat_state |= P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL;
	} else {
		p->p_memstat_state &= ~P_MEMSTAT_MEMLIMIT_ACTIVE_FATAL;
	}

	/*
	 * Initialize the cached limits for target process.
	 * When the target process is dirty tracked, it's typically
	 * in a clean state.  Non dirty tracked processes are
	 * typically active (Foreground or above).
	 * But just in case, we don't make assumptions...
	 */
	const bool use_active = memstat_proc_is_active_locked(p);
	if (memorystatus_highwater_enabled &&
	    memstat_update_memlimit_locked(p, use_active)) {
		err = _memstat_write_memlimit_to_ledger_locked(p, use_active, false);
	}

	return err;
}

int
memorystatus_set_memlimits(proc_t p, int32_t active_limit,
    int32_t inactive_limit, memlimit_options_t options)
{
	int err;
	proc_list_lock();
	err = memstat_set_memlimits_locked(p, active_limit, inactive_limit,
	    options);
	proc_list_unlock();
	return err;
}

int
memorystatus_remove(proc_t p)
{
	int ret;
	memstat_bucket_t *bucket;
	bool reschedule = false;

	memorystatus_log_debug("memorystatus_list_remove: removing pid %d\n", proc_getpid(p));

	/* Processes marked internal do not have priority tracked */
	if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
		return 0;
	}

	/*
	 * Check if this proc is locked (because we're performing a freeze).
	 * If so, we fail and instruct the caller to try again later.
	 */
	if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
		return EAGAIN;
	}

	assert(!(p->p_memstat_state & P_MEMSTAT_INTERNAL));

	bucket = &memstat_bucket[p->p_memstat_effectivepriority];

	if ((system_procs_aging_band &&
	    p->p_memstat_effectivepriority == system_procs_aging_band) ||
	    (system_procs_aging_band_stuck &&
	    p->p_memstat_effectivepriority == system_procs_aging_band_stuck) ||
	    (applications_aging_band &&
	    p->p_memstat_effectivepriority == applications_aging_band)) {
		_memstat_invalidate_idle_demotion_locked(p);
		reschedule = true;
	}

	/*
	 * Record idle delta
	 */

	if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_IDLE) {
		uint64_t now = mach_absolute_time();
		if (now > p->p_memstat_prio_start) {
			p->p_memstat_idle_delta = now - p->p_memstat_prio_start;
		}
	}

	TAILQ_REMOVE(&bucket->list, p, p_memstat_list);
	bucket->count--;
	if (p->p_memstat_relaunch_flags & (P_MEMSTAT_RELAUNCH_HIGH)) {
		bucket->relaunch_high_count--;
	}

	memorystatus_list_count--;

	/* If awaiting demotion to the idle band, clean up */
	if (reschedule) {
		_memstat_reschedule_idle_demotion_locked();
	}

#if CONFIG_FREEZE
	if (_memstat_proc_is_frozen(p)) {
		if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) {
			p->p_memstat_state &= ~P_MEMSTAT_REFREEZE_ELIGIBLE;
			assert(memorystatus_refreeze_eligible_count > 0);
			memorystatus_refreeze_eligible_count--;
		}

		assert(memorystatus_frozen_count > 0);
		memorystatus_frozen_count--;
		if (p->p_memstat_state & P_MEMSTAT_FROZEN_XPC_SERVICE) {
			assert(memorystatus_frozen_count_xpc_service > 0);
			memorystatus_frozen_count_xpc_service--;
		}
		if (strcmp(p->p_name, "com.apple.WebKit.WebContent") == 0) {
			assert(memorystatus_frozen_count_webcontent > 0);
			memorystatus_frozen_count_webcontent--;
		}
		memorystatus_frozen_shared_mb -= p->p_memstat_freeze_sharedanon_pages;
		p->p_memstat_freeze_sharedanon_pages = 0;
	}
#endif /* CONFIG_FREEZE */

	_memstat_proc_set_resumed(p);

#if DEVELOPMENT || DEBUG
	if (proc_getpid(p) == memorystatus_testing_pid) {
		memorystatus_testing_pid = 0;
	}
#endif /* DEVELOPMENT || DEBUG */

	if (p) {
		ret = 0;
	} else {
		ret = ESRCH;
	}

	return ret;
}

/*
 * Validate dirty tracking flags with process state.
 *
 * Return:
 *	0     on success
 *      non-0 on failure
 *
 * The proc_list_lock is held by the caller.
 */

static int
memorystatus_validate_track_flags(struct proc *target_p, uint32_t pcontrol)
{
	/* See that the process isn't marked for termination */
	if (_memstat_proc_is_terminating(target_p)) {
		return EBUSY;
	}

	/* Idle exit requires that process be tracked */
	if ((pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) &&
	    !(pcontrol & PROC_DIRTY_TRACK)) {
		return EINVAL;
	}

	/* 'Launch in progress' tracking requires that process have enabled dirty tracking too. */
	if ((pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) &&
	    !(pcontrol & PROC_DIRTY_TRACK)) {
		return EINVAL;
	}

	/* Only one type of DEFER behavior is allowed.*/
	if ((pcontrol & PROC_DIRTY_DEFER) &&
	    (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) {
		return EINVAL;
	}

	/* Deferral is only relevant if idle exit is specified */
	if (((pcontrol & PROC_DIRTY_DEFER) ||
	    (pcontrol & PROC_DIRTY_DEFER_ALWAYS)) &&
	    !(pcontrol & PROC_DIRTY_ALLOWS_IDLE_EXIT)) {
		return EINVAL;
	}

	return 0;
}

/*
 * Processes can opt to have their state tracked by the kernel, indicating  when they are busy (dirty) or idle
 * (clean). They may also indicate that they support termination when idle, with the result that they are promoted
 * to their desired, higher, jetsam priority when dirty (and are therefore killed later), and demoted to the low
 * priority idle band when clean (and killed earlier, protecting higher priority procesess).
 *
 * If the deferral flag is set, then newly tracked processes will be protected for an initial period (as determined by
 * memorystatus_sysprocs_idle_delay_time); if they go clean during this time, then they will be moved to a deferred-idle band
 * with a slightly higher priority, guarding against immediate termination under memory pressure and being unable to
 * make forward progress. Finally, when the guard expires, they will be moved to the standard, lowest-priority, idle
 * band. The deferral can be cleared early by clearing the appropriate flag.
 *
 * The deferral timer is active only for the duration that the process is marked as guarded and clean; if the process
 * is marked dirty, the timer will be cancelled. Upon being subsequently marked clean, the deferment will either be
 * re-enabled or the guard state cleared, depending on whether the guard deadline has passed.
 */

int
memorystatus_dirty_track(proc_t p, uint32_t pcontrol)
{
	unsigned int old_dirty;
	boolean_t defer_now = FALSE;
	int ret = 0;
	int priority;
	bool kill = false;
	memstat_priority_options_t priority_options =
	    MEMSTAT_PRIORITY_OPTIONS_NONE;

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_DIRTY_TRACK),
	    proc_getpid(p), p->p_memstat_dirty, pcontrol);

	proc_list_lock();

	if (proc_list_exited(p)) {
		/*
		 * Process is on its way out.
		 */
		ret = EBUSY;
		goto exit;
	}

	if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
		ret = EPERM;
		goto exit;
	}

	if ((ret = memorystatus_validate_track_flags(p, pcontrol)) != 0) {
		/* error  */
		goto exit;
	}

	old_dirty = p->p_memstat_dirty;

	/* These bits are cumulative, as per <rdar://problem/11159924> */
	if (pcontrol & PROC_DIRTY_TRACK) {
		/* Request to turn ON Dirty tracking... */
		if (p->p_memstat_state & P_MEMSTAT_MANAGED) {
			/* on a process managed by RunningBoard or its equivalent...*/
			if (!_memstat_proc_cached_memlimit_is_fatal(p)) {
				/* but this might be an app because there's no fatal limits
				 * NB: This _big_ assumption is not universal. What we really
				 * need is a way to say this is an _APP_ and we can't have dirty
				 * tracking turned ON for it. Lacking that functionality we clump
				 * together some checks and try to do the best detection we can.
				 * Reason we can't allow addition of these flags is because, per the
				 * kernel checks, they change the role of a process from app to daemon. And the
				 * AGING_IN_PROGRESS bits might still be set i.e. it needs to be demoted
				 * correctly from the right aging band (app or sysproc). We can't simply try
				 * to invalidate the demotion here because, owing to assertion priorities, we
				 * might not be in the aging bands.
				 */
				memorystatus_log(
					"memorystatus: Denying dirty-tracking opt-in for managed %s [%d]\n",
					proc_best_name(p), proc_getpid(p));
				/* fail silently to avoid an XPC assertion... */
				ret = 0;
				goto exit;
			}
		}

		p->p_memstat_dirty |= P_DIRTY_TRACK;
		if (!(old_dirty & P_DIRTY_TRACK)) {
			p->p_memstat_dirty |= P_DIRTY_IM_NEW_HERE;
		}
	}

	if (pcontrol & PROC_DIRTY_ALLOW_IDLE_EXIT) {
		p->p_memstat_dirty |= P_DIRTY_ALLOW_IDLE_EXIT;
	}

	if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
		p->p_memstat_dirty |= P_DIRTY_LAUNCH_IN_PROGRESS;
	}

	/*
	 * NB: All processes are now automatically enrolled in idle aging
	 * regardless of whether they request to be deferred.
	 */
	if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) {
		if ((pcontrol & (PROC_DIRTY_DEFER)) &&
		    !(old_dirty & P_DIRTY_DEFER)) {
			p->p_memstat_dirty |= P_DIRTY_DEFER;
		}

		if ((pcontrol & (PROC_DIRTY_DEFER_ALWAYS)) &&
		    !(old_dirty & P_DIRTY_DEFER_ALWAYS)) {
			p->p_memstat_dirty |= P_DIRTY_DEFER_ALWAYS;
		}

		defer_now = TRUE;
	}

	if (pcontrol & PROC_DIRTY_SHUTDOWN_ON_CLEAN) {
		p->p_memstat_dirty |= P_DIRTY_SHUTDOWN_ON_CLEAN;

		if (_memstat_proc_is_tracked(p) && !_memstat_proc_is_dirty(p)) {
			kill = true;
		}
	}

	memorystatus_log_info(
		"%s [%d] enrolled in ActivityTracking tracked %d / idle-exit %d / defer %d / dirty %d",
		proc_best_name(p), proc_getpid(p),
		_memstat_proc_is_tracked(p), _memstat_proc_can_idle_exit(p), defer_now,
		_memstat_proc_is_dirty(p));

	if (!_memstat_proc_is_dirty(p) && _memstat_proc_is_tracked(p) &&
	    _memstat_proc_can_idle_exit(p)) {
		priority = JETSAM_PRIORITY_IDLE;
		if (!defer_now && _memstat_proc_is_aging(p)) {
			/*
			 * Historically, some processes have tried to use this to opt out
			 * of the 'aging' facility.
			 */
			priority_options |= MEMSTAT_PRIORITY_NO_AGING;
		}
	} else {
		priority = p->p_memstat_requestedpriority;
	}

	if (_memstat_proc_has_priority_assertion(p)) {
		priority = MAX(priority, p->p_memstat_assertionpriority);
	}

	memstat_update_priority_locked(p, priority, priority_options);

exit:
	if (kill && proc_ref(p, true) == p) {
		proc_list_unlock();
		psignal(p, SIGKILL);
		proc_rele(p);
	} else {
		proc_list_unlock();
	}

	return ret;
}

int
memorystatus_dirty_set(proc_t p, boolean_t self, uint32_t pcontrol)
{
	int ret = 0;
	bool kill = false;
	bool was_dirty;
	bool now_dirty = false;
	int priority;
	task_t t = proc_task(p);

	memorystatus_log_debug("memorystatus_dirty_set(): %d %d 0x%x 0x%x\n", self, proc_getpid(p), pcontrol, p->p_memstat_dirty);
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_DIRTY_SET), proc_getpid(p), self, pcontrol);

	proc_list_lock();

	if (proc_list_exited(p)) {
		/*
		 * Process is on its way out.
		 */
		ret = EBUSY;
		goto exit;
	}

	if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
		ret = EPERM;
		goto exit;
	}

	was_dirty = _memstat_proc_is_dirty(p);

	if (!_memstat_proc_is_tracked(p)) {
		/* Dirty tracking not enabled */
		ret = EINVAL;
		goto exit;
	} else if (pcontrol && _memstat_proc_is_terminating(p)) {
		/*
		 * Process is set to be terminated and we're attempting to mark it dirty.
		 * Set for termination and marking as clean is OK - see <rdar://problem/10594349>.
		 */
		ret = EBUSY;
		goto exit;
	}

	int flag = (self == TRUE) ? P_DIRTY : P_DIRTY_SHUTDOWN;
	if (pcontrol && !(p->p_memstat_dirty & flag)) {
		/* Mark the process as having been dirtied at some point */
		p->p_memstat_dirty |= (flag | P_DIRTY_MARKED);
	} else if ((pcontrol == 0) && (p->p_memstat_dirty & flag)) {
		if ((flag == P_DIRTY_SHUTDOWN) && (!(p->p_memstat_dirty & P_DIRTY))) {
			/* Clearing the dirty shutdown flag, and the process is otherwise clean - kill */
			p->p_memstat_dirty |= P_DIRTY_TERMINATED;
			kill = true;
		} else if ((flag == P_DIRTY) && _memstat_proc_is_terminating(p)) {
			/* Kill previously terminated processes if set clean */
			kill = true;
		}
		p->p_memstat_dirty &= ~flag;
	} else {
		/* Already set */
		ret = EALREADY;
		goto exit;
	}

	now_dirty = _memstat_proc_is_dirty(p);

	if (was_dirty && !now_dirty) {
		if (_memstat_proc_can_idle_exit(p)) {
			/*
			 * Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band OR it might be heading back
			 * there once it's clean again. For the legacy case, this only applies if it has some protection window left.
			 * P_DIRTY_DEFER: one-time protection window given at launch
			 * P_DIRTY_DEFER_ALWAYS: protection window given for every dirty->clean transition. Like non-legacy mode.
			 *
			 * Non-Legacy mode: P_DIRTY_AGING_IN_PROGRESS means the process is in the aging band. It will always stop over
			 * in that band on it's way to IDLE.
			 */
			assert(!_memstat_proc_is_aging(p));
			priority = JETSAM_PRIORITY_IDLE;
		} else {
			priority = p->p_memstat_requestedpriority;
		}
		task_ledger_settle_dirty_time(t);
		task_set_dirty_start(t, 0);
		if (_memstat_proc_shutdown_on_clean(p)) {
			kill = true;
		}
	} else if (!was_dirty && now_dirty) {
		p->p_memstat_dirty &= ~P_DIRTY_IM_NEW_HERE;
		priority = p->p_memstat_requestedpriority;
		task_set_dirty_start(t, mach_absolute_time());
	}

	if (_memstat_proc_has_priority_assertion(p)) {
		priority = MAX(priority, p->p_memstat_assertionpriority);
	}

	memstat_update_priority_locked(p, priority, MEMSTAT_PRIORITY_OPTIONS_NONE);

exit:
	if (kill && proc_ref(p, true) == p) {
		proc_list_unlock();
		psignal(p, SIGKILL);
		proc_rele(p);
	} else {
		proc_list_unlock();
	}

	return ret;
}

int
memorystatus_dirty_clear(proc_t p, uint32_t pcontrol)
{
	int ret = 0;

	memorystatus_log_debug("memorystatus_dirty_clear(): %d 0x%x 0x%x\n", proc_getpid(p), pcontrol, p->p_memstat_dirty);
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_DIRTY_CLEAR), proc_getpid(p), pcontrol);

	proc_list_lock();

	if (proc_list_exited(p)) {
		/*
		 * Process is on its way out.
		 */
		ret = EBUSY;
		goto exit;
	}

	if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
		ret = EPERM;
		goto exit;
	}

	if (!_memstat_proc_is_tracked(p)) {
		/* Dirty tracking not enabled */
		ret = EINVAL;
		goto exit;
	}

	if (!pcontrol || (pcontrol & (PROC_DIRTY_LAUNCH_IN_PROGRESS | PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) == 0) {
		ret = EINVAL;
		goto exit;
	}

	if (pcontrol & PROC_DIRTY_LAUNCH_IN_PROGRESS) {
		p->p_memstat_dirty &= ~P_DIRTY_LAUNCH_IN_PROGRESS;
	}

	/* This can be set and cleared exactly once. */
	if (pcontrol & (PROC_DIRTY_DEFER | PROC_DIRTY_DEFER_ALWAYS)) {
		if (p->p_memstat_dirty & P_DIRTY_DEFER) {
			p->p_memstat_dirty &= ~(P_DIRTY_DEFER);
		}

		if (p->p_memstat_dirty & P_DIRTY_DEFER_ALWAYS) {
			p->p_memstat_dirty &= ~(P_DIRTY_DEFER_ALWAYS);
		}

		if (_memstat_proc_is_aging(p)) {
			memstat_update_priority_locked(p, JETSAM_PRIORITY_IDLE,
			    MEMSTAT_PRIORITY_NO_AGING);
		}
	}

	ret = 0;
exit:
	proc_list_unlock();

	return ret;
}

int
memorystatus_dirty_get(proc_t p, boolean_t locked)
{
	int ret = 0;

	if (!locked) {
		proc_list_lock();
	}

	if (_memstat_proc_is_tracked(p)) {
		ret |= PROC_DIRTY_TRACKED;
		if (_memstat_proc_can_idle_exit(p)) {
			ret |= PROC_DIRTY_ALLOWS_IDLE_EXIT;
		}
		if (p->p_memstat_dirty & P_DIRTY) {
			ret |= PROC_DIRTY_IS_DIRTY;
		}
		if (p->p_memstat_dirty & P_DIRTY_LAUNCH_IN_PROGRESS) {
			ret |= PROC_DIRTY_LAUNCH_IS_IN_PROGRESS;
		}
	}

	if (!locked) {
		proc_list_unlock();
	}

	return ret;
}

int
memorystatus_on_terminate(proc_t p)
{
	int sig;

	proc_list_lock();

	p->p_memstat_dirty |= P_DIRTY_TERMINATED;

	if ((_memstat_proc_is_tracked(p) && !_memstat_proc_is_dirty(p)) ||
	    (_memstat_proc_is_suspended(p))) {
		/*
		 * Mark as terminated and issue SIGKILL if:-
		 * - process is clean, or,
		 * - if process is dirty but suspended. This case is likely
		 * an extension because apps don't opt into dirty-tracking
		 * and daemons aren't suspended.
		 */
#if DEVELOPMENT || DEBUG
		if (_memstat_proc_is_suspended(p)) {
			memorystatus_log(
				"memorystatus: sending suspended process %s (pid %d) SIGKILL\n",
				(*p->p_name ? p->p_name : "unknown"), proc_getpid(p));
		}
#endif /* DEVELOPMENT || DEBUG */
		sig = SIGKILL;
	} else {
		/* Dirty, terminated, or state tracking is unsupported; issue SIGTERM to allow cleanup */
		sig = SIGTERM;
	}

	proc_list_unlock();

	return sig;
}

void
memorystatus_on_suspend(proc_t p)
{
#if CONFIG_FREEZE
	uint32_t pages;
	memorystatus_get_task_page_counts(proc_task(p), &pages, NULL, NULL);
#endif
	proc_list_lock();

	_memstat_proc_set_suspended(p);

	/* Check if proc is marked for termination */
	bool kill_process = _memstat_proc_is_terminating(p);
	proc_list_unlock();

	if (kill_process) {
		psignal(p, SIGKILL);
	}
}

extern uint64_t memorystatus_thaw_count_since_boot;

void
memorystatus_on_resume(proc_t p)
{
#if CONFIG_FREEZE
	pid_t pid;
#endif

	proc_list_lock();

#if CONFIG_FREEZE
	const bool frozen = _memstat_proc_is_frozen(p);
	if (frozen) {
		/*
		 * Now that we don't _thaw_ a process completely,
		 * resuming it (and having some on-demand swapins)
		 * shouldn't preclude it from being counted as frozen.
		 *
		 * memorystatus_frozen_count--;
		 *
		 * We preserve the P_MEMSTAT_FROZEN state since the process
		 * could have state on disk AND so will deserve some protection
		 * in the jetsam bands.
		 */
		if ((p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) == 0) {
			p->p_memstat_state |= P_MEMSTAT_REFREEZE_ELIGIBLE;
			memorystatus_refreeze_eligible_count++;
		}
		if (p->p_memstat_thaw_count == 0 || p->p_memstat_last_thaw_interval < memorystatus_freeze_current_interval) {
			os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_thawed), relaxed);
			if (strcmp(p->p_name, "com.apple.WebKit.WebContent") == 0) {
				os_atomic_inc(&(memorystatus_freezer_stats.mfs_processes_thawed_webcontent), relaxed);
			}
		}
		p->p_memstat_last_thaw_interval = memorystatus_freeze_current_interval;
		p->p_memstat_thaw_count++;

		memorystatus_log("memorystatus: resuming/thawing pid %d [%s]\n", p->p_pid, proc_best_name(p));
		memorystatus_freeze_record_process_thawed(p);

		memorystatus_thaw_count++;
		memorystatus_thaw_count_since_boot++;
	}

	pid = proc_getpid(p);
#endif

	/*
	 * P_MEMSTAT_FROZEN will remain unchanged. This used to be:
	 * p->p_memstat_state &= ~(P_MEMSTAT_SUSPENDED | P_MEMSTAT_FROZEN);
	 */
	_memstat_proc_set_resumed(p);

	proc_list_unlock();

#if CONFIG_FREEZE
	if (frozen) {
		memorystatus_freeze_entry_t data = { pid, FALSE, 0 };
		memorystatus_send_note(kMemorystatusFreezeNote, &data, sizeof(data));
	}
#endif
}

void
memorystatus_on_inactivity(proc_t p)
{
#pragma unused(p)
#if CONFIG_FREEZE
	/* Wake the freeze thread */
	thread_wakeup((event_t)&memorystatus_freeze_wakeup);
#endif
}

/*
 * The proc_list_lock is held by the caller.
 */
static memorystatus_proc_state_t
_memstat_build_state(proc_t p)
{
	uint32_t snapshot_state = 0;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	/* General */
	if (_memstat_proc_is_suspended(p)) {
		snapshot_state |= kMemorystatusSuspended;
	}
	if (_memstat_proc_is_frozen(p)) {
		snapshot_state |= kMemorystatusFrozen;
	}
	if (p->p_memstat_state & P_MEMSTAT_REFREEZE_ELIGIBLE) {
		snapshot_state |= kMemorystatusWasThawed;
	}
	if (p->p_memstat_state & P_MEMSTAT_PRIORITY_ASSERTION) {
		snapshot_state |= kMemorystatusAssertion;
	}

	/* Tracking */
	if (_memstat_proc_is_tracked(p)) {
		snapshot_state |= kMemorystatusTracked;
	}
	if (_memstat_proc_can_idle_exit(p)) {
		snapshot_state |= kMemorystatusSupportsIdleExit;
	}
	if (_memstat_proc_is_dirty(p)) {
		snapshot_state |= kMemorystatusDirty;
	}
	if (memstat_proc_is_active_locked(p)) {
		snapshot_state |= kMemorystatusActive;
	}

	/* Probable relaunch behavior */
	if (p->p_memstat_relaunch_flags & P_MEMSTAT_RELAUNCH_LOW) {
		snapshot_state |= kMemorystatusRelaunchLow;
	}
	if (p->p_memstat_relaunch_flags & P_MEMSTAT_RELAUNCH_MED) {
		snapshot_state |= kMemorystatusRelaunchMed;
	}
	if (p->p_memstat_relaunch_flags & P_MEMSTAT_RELAUNCH_HIGH) {
		snapshot_state |= kMemorystatusRelaunchHigh;
	}

	return snapshot_state;
}

bool
memstat_kill_idle_process(memorystatus_kill_cause_t cause,
    uint64_t *footprint_out)
{
	proc_t p = PROC_NULL;
	uint64_t current_time;
	bool killed = FALSE;
	unsigned int i = 0;
	os_reason_t jetsam_reason = OS_REASON_NULL;

	/* Pick next idle exit victim. */
	current_time = mach_absolute_time();

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, (jetsam_reason_t)cause);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus: failed to allocate jetsam reason\n");
	}

	proc_list_lock();

	p = memorystatus_get_first_proc_locked(&i, FALSE);
	while (p) {
		/* No need to look beyond the idle band */
		if (p->p_memstat_effectivepriority != JETSAM_PRIORITY_IDLE) {
			break;
		}

		if ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT | P_DIRTY_IS_DIRTY | P_DIRTY_TERMINATED)) == (P_DIRTY_ALLOW_IDLE_EXIT)) {
			if (current_time >= p->p_memstat_idledeadline) {
				p->p_memstat_dirty |= P_DIRTY_TERMINATED;
				p = proc_ref(p, true);
				break;
			}
		}

		p = memorystatus_get_next_proc_locked(&i, p, FALSE);
	}

	proc_list_unlock();

	if (p) {
		memorystatus_log(
			"memorystatus: killing (idle) %s [%d] due to %s (%u)\n",
			proc_best_name(p), proc_getpid(p), memstat_kill_cause_name[cause], cause);
		memorystatus_kill_proc(p, cause, jetsam_reason, &killed, footprint_out);
		proc_rele(p);
	} else {
		os_reason_free(jetsam_reason);
	}

	return killed;
}

/*
 * Consider waking the jetsam thread. Returns true if the thread was awoken.
 */
static bool
_memstat_consider_waking_jetsam_thread(void)
{
#if CONFIG_JETSAM
	if (memstat_evaluate_page_shortage(NULL, NULL, NULL, NULL)) {
		memorystatus_thread_wake();
		return true;
	}
#endif /* CONFIG_JETSAM */
	return false;
}

void
memorystatus_thread_wake()
{
	int thr_id = 0;
	int active_thr = atomic_load(&active_jetsam_threads);

	/* Wakeup all the jetsam threads */
	for (thr_id = 0; thr_id < active_thr; thr_id++) {
		jetsam_state_t jetsam_thread = &jetsam_threads[thr_id];
		sched_cond_signal(&(jetsam_thread->jt_wakeup_cond), jetsam_thread->thread);
	}
}

void
memorystatus_respond_to_compressor_exhaustion(void)
{
#if CONFIG_JETSAM
	memorystatus_thread_wake();
#else /* !CONFIG_JETSAM */
	if (kill_on_no_paging_space) {
		memorystatus_thread_wake();
	} else {
		/*
		 * Throttle how often the jetsam thread is woken due to
		 * compressor/swap exhaustion
		 */
		uint64_t now = mach_absolute_time();
		uint64_t delta_since_last_no_space_ns;
		uint64_t last_action_ts = os_atomic_load(&last_no_space_action_ts, relaxed);
		if (now < last_action_ts) {
			/* Raced with a concurrent no-paging-space action */
			return;
		}
		absolutetime_to_nanoseconds(now - last_action_ts, &delta_since_last_no_space_ns);
		if (delta_since_last_no_space_ns > no_paging_space_action_throttle_delay_ns) {
			memorystatus_thread_wake();
		}
	}
#endif /* CONFIG_JETSAM */
}

void
memorystatus_respond_to_swap_exhaustion(void)
{
#if CONFIG_JETSAM
	/*
	 * On systems with both swap and jetsam,
	 * just wake up the jetsam thread and have it handle the low swap condition
	 * by killing apps.
	 */
	if (jetsam_kill_on_low_swap) {
		memorystatus_thread_wake();
	}
#else /* !CONFIG_JETSAM */
	memorystatus_respond_to_compressor_exhaustion();
#endif /* CONFIG_JETSAM */
}

#if CONFIG_JETSAM
static void
memorystatus_thread_pool_max()
{
	/* Increase the jetsam thread pool to max_jetsam_threads */
	int max_threads = max_jetsam_threads;
	memorystatus_log_info("Expanding memorystatus pool to %d\n", max_threads);
	os_atomic_store(&active_jetsam_threads, max_threads, relaxed);
}

static void
memorystatus_thread_pool_default()
{
	/* Restore the jetsam thread pool to a single thread */
	memorystatus_log_info("Reverting memorystatus pool back to 1\n");
	os_atomic_store(&active_jetsam_threads, 1, relaxed);
}
#endif /* CONFIG_JETSAM */

/*
 * An offset applied to non-critical page shortage thresholds.
 */
static uint32_t
_memstat_page_shortage_offset(void)
{
	uint32_t offset = 0;
	if (os_atomic_load(&memstat_policy_config, acquire) & kPolicyClearTheDecks) {
		offset += memstat_ctd_offset;
	}
	if (os_atomic_load(&memstat_policy_config, acquire) & kPolicyBallastDrain) {
		offset += memstat_ballast_offset;
	}
	return offset;
}

uint32_t
memorystatus_get_critical_page_shortage_threshold(void)
{
	return memstat_critical_threshold;
}

uint32_t
memorystatus_get_idle_exit_page_shortage_threshold(void)
{
	uint32_t offset = _memstat_page_shortage_offset();
	return memstat_idle_threshold + offset;
}

uint32_t
memorystatus_get_soft_memlimit_page_shortage_threshold(void)
{
	uint32_t offset = _memstat_page_shortage_offset();
	return memstat_soft_threshold + offset;
}

uint32_t
memorystatus_get_reaper_page_shortage_threshold(void)
{
	uint32_t offset = _memstat_page_shortage_offset();
	return memstat_reaper_threshold + offset;
}

#if CONFIG_JETSAM
void
_memstat_reaper_check_oldest_reapable_proc_info_timeout(void)
{
	if (memstat_oldest_reapable_proc_prio_start != MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_UNKNOWN) {
		uint64_t curr_ts_matu = mach_absolute_time();
		if (curr_ts_matu > memstat_oldest_reapable_proc_info_expiration_ts_matu) {
			memstat_oldest_reapable_proc_prio_start = MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_UNKNOWN;
			memstat_oldest_reapable_proc_will_be_reapable_at_ts_matu = 0;
		}
	}
}

void
_memstat_reaper_start_sweep(void)
{
	if (!memstat_reaper_is_currently_sweeping) {
		memorystatus_log("memorystatus: _memstat_reaper_start_sweep: reaper sweep starting\n");
		memstat_reaper_is_currently_sweeping = true;
		memstat_reaper_start_ts_matu = mach_absolute_time();
		memstat_reaper_current_sweep_stats.kill_count = 0;
		memstat_reaper_current_sweep_stats.memory_freed_bytes = 0;
	}
}

void
_memstat_reaper_end_sweep(void)
{
	uint64_t rescan_delta_matu;
	if (memstat_reaper_is_currently_sweeping) {
		uint64_t delta_time_nsec;

		/* For idle reaper kills, we skip the normal compaction after each kill,
		 * and do one compaction here at the end of the sweep.
		 */
		vm_run_compactor();

		absolutetime_to_nanoseconds(mach_absolute_time() - memstat_reaper_start_ts_matu, &delta_time_nsec);
		memstat_reaper_cumulative_stats.sweep_count++;
		memstat_reaper_cumulative_memory_freed_mb = (uint32_t)(memstat_reaper_cumulative_stats.memory_freed_bytes >> 20);
		memorystatus_log("memorystatus: _memstat_reaper_end_sweep: reaper sweep ended, %d processes killed, %lluMB freed, %llums elapsed, %lluus/process\n",
		    memstat_reaper_current_sweep_stats.kill_count,
		    memstat_reaper_current_sweep_stats.memory_freed_bytes >> 20,
		    (delta_time_nsec / NSEC_PER_MSEC),
		    memstat_reaper_current_sweep_stats.kill_count ? ((delta_time_nsec / NSEC_PER_USEC) / memstat_reaper_current_sweep_stats.kill_count) : 0);
		memorystatus_log("memorystatus: _memstat_reaper_end_sweep: reaper totals: %d sweeps, %d processes killed, %dMB freed\n",
		    memstat_reaper_cumulative_stats.sweep_count,
		    memstat_reaper_cumulative_stats.kill_count,
		    memstat_reaper_cumulative_memory_freed_mb);
		memstat_reaper_is_currently_sweeping = false;
		nanoseconds_to_absolutetime((memstat_reaper_rescan_secs * NSEC_PER_SEC), &rescan_delta_matu);
		memstat_reaper_can_run_after_ts_matu = mach_absolute_time() + rescan_delta_matu;
	}
}

void
_memstat_reaper_record_kill(uint64_t bytes_freed)
{
	memstat_reaper_current_sweep_stats.kill_count++;
	memstat_reaper_current_sweep_stats.memory_freed_bytes += bytes_freed;
	memstat_reaper_cumulative_stats.kill_count++;
	memstat_reaper_cumulative_stats.memory_freed_bytes += bytes_freed;
}
#endif /* CONFIG_JETSAM */

const char*
_memstat_relaunch_flags_description(uint32_t flags)
{
	switch (flags) {
	case P_MEMSTAT_RELAUNCH_UNKNOWN:
		return "-";
	case P_MEMSTAT_RELAUNCH_LOW:
		return "low";
	case P_MEMSTAT_RELAUNCH_MED:
		return "med";
	case P_MEMSTAT_RELAUNCH_HIGH:
		return "high";
	default:
		return "??";
	}
}

const char*
_memstat_proc_type_description(proc_t p)
{
	if (_memstat_proc_is_application(p)) {
		return "app";
	} else {
		return "daemon";
	}
}

bool
memstat_evaluate_page_shortage(
	bool *should_enforce_memlimits,
	bool *should_idle_exit,
	bool *should_jetsam,
	bool *should_reap)
{
	bool requires_action = false;
	if (should_enforce_memlimits) {
		*should_enforce_memlimits = false;
	}
	if (should_idle_exit) {
		*should_idle_exit = false;
	}
	if (should_jetsam) {
		*should_jetsam = false;
	}
	if (should_reap) {
		*should_reap = false;
	}
#if CONFIG_JETSAM
	uint32_t available_page_count = os_atomic_load(&memorystatus_available_pages, relaxed);
#if VM_PRESSURE_EVENTS
	if (available_page_count <
	    memorystatus_get_soft_memlimit_page_shortage_threshold()) {
		/*
		 * Only wake the jetsam thread if there are hwm violators to
		 * kill
		 */
		bool hwm_candidates = os_atomic_load(&memorystatus_hwm_candidates, acquire);
		requires_action = requires_action || hwm_candidates;
		if (should_enforce_memlimits) {
			*should_enforce_memlimits = true;
		}
	}
#endif /* VM_PRESSURE_EVENTS */

	if (memstat_reaper_enabled) {
		/*
		 * Only wake the jetsam thread to do reaper kills if the reaper is currently alreay running a sweep
		 * OR if other conditions suggest that we should start a sweep
		 */

		// if we are already in the middle of a reaper sweep already, continue it
		if (memstat_reaper_is_currently_sweeping) {
			requires_action = true;
			if (should_reap) {
				*should_reap = true;
			}
		} else {
			uint64_t curr_ts_matu = mach_absolute_time();
			// if we are not already in the middle of a reaper sweep, do very quick tests to see if we should possibly start one:
			// - the minimum rescan time has passed since the end of the last sweep
			// - we are below the page threshold
			// - the oldest reapable process is old enough to be a reaper candidate now

			if ((curr_ts_matu > memstat_reaper_can_run_after_ts_matu)
			    && (available_page_count < memorystatus_get_reaper_page_shortage_threshold())) {
				_memstat_reaper_check_oldest_reapable_proc_info_timeout();

				if (memstat_oldest_reapable_proc_prio_start == MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_NONE) {
					memorystatus_log_debug("memorystatus: memstat_evaluate_page_shortage: no known-reapable processes\n");
				} else {
					if (curr_ts_matu >= memstat_oldest_reapable_proc_will_be_reapable_at_ts_matu) {
						requires_action = true;
						if (should_reap) {
							*should_reap = true;
							memorystatus_log_debug("memorystatus: memstat_evaluate_page_shortage: should start reaping long-idle processes\n");
						}
						_memstat_reaper_start_sweep();
					}
				}
			}
		}
	}
	if (available_page_count < memorystatus_get_idle_exit_page_shortage_threshold()) {
		/*
		 * Only wake the jetsam thread if there are idle processes that
		 * could exit.
		 */
		uint32_t idle_proc_count = os_atomic_load(
			&memstat_bucket[JETSAM_PRIORITY_IDLE].count, relaxed);
		requires_action = requires_action || (idle_proc_count > 0);
		if (should_idle_exit) {
			*should_idle_exit = true;
		}
	}
	if (available_page_count < memorystatus_get_critical_page_shortage_threshold()) {
		if (should_jetsam) {
			*should_jetsam = true;
		}
		requires_action = true;
	}
#endif /* CONFIG_JETSAM */
	return requires_action;
}

#if CONFIG_JETSAM
static uint64_t
memorystatus_swap_trigger_pages(void)
{
	/*
	 * The swapout trigger varies based on the current memorystatus_level.
	 * When available memory is somewhat high (at memorystatus_available_pages_pressure)
	 * we keep more swappable compressor segments in memory.
	 * However, as available memory drops to our idle and eventually critical kill
	 * thresholds we start swapping more aggressively.
	 */
	static uint32_t available_pages_factor[] = {0, 1, 1, 1, 2, 2, 3, 5, 7, 8, 10, 13, 15, 17, 20};
	size_t index = MIN(memorystatus_level, sizeof(available_pages_factor) / sizeof(uint32_t) - 1);
	return available_pages_factor[index] * memorystatus_available_pages / 10;
}

static int
sysctl_memorystatus_swap_trigger_pages SYSCTL_HANDLER_ARGS
{
#pragma unused(arg1, arg2)
	uint64_t trigger_pages = memorystatus_swap_trigger_pages();
	return SYSCTL_OUT(req, &trigger_pages, sizeof(trigger_pages));
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_swap_trigger_pages, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED,
    0, 0, &sysctl_memorystatus_swap_trigger_pages, "I", "");

/*
 * Check if the number of full swappable csegments is over the trigger
 * threshold to start swapping.
 * The adjustment_factor is applied to the trigger to raise or lower
 * it. For example an adjustement factor of 110 will raise the threshold by 10%.
 */
bool
memorystatus_swap_over_trigger(uint64_t adjustment_factor)
{
	if (!memorystatus_swap_all_apps) {
		return false;
	}
	uint64_t trigger_pages = memorystatus_swap_trigger_pages();
	trigger_pages = trigger_pages * adjustment_factor / 100;
	return atop_64(c_late_swapout_count * c_seg_allocsize) > trigger_pages;
}

/*
 * Check if the number of segments on the early swapin queue
 * is over the trigger to start compacting it.
 */
bool
memorystatus_swapin_over_trigger(void)
{
	return atop_64(c_late_swappedin_count * c_seg_allocsize) > memorystatus_swapin_trigger_pages;
}
#endif /* CONFIG_JETSAM */

#if DEVELOPMENT || DEBUG
SYSCTL_UINT(_vm, OID_AUTO, c_late_swapout_count, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, &c_late_swapout_count, 0, "");
SYSCTL_UINT(_vm, OID_AUTO, c_seg_allocsize, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, &c_seg_allocsize, 0, "");
#if CONFIG_FREEZE
extern int32_t c_segment_pages_compressed_incore_late_swapout;
SYSCTL_INT(_vm, OID_AUTO, c_segment_pages_compressed_incore_late_swapout, CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_LOCKED, &c_segment_pages_compressed_incore_late_swapout, 0, "");
#endif /* CONFIG_FREEZE */
#endif /* DEVELOPMENT || DEBUG */

static boolean_t
memorystatus_should_post_snapshot(int32_t priority, uint32_t cause)
{
	boolean_t is_idle_priority;

	is_idle_priority = (priority == JETSAM_PRIORITY_IDLE || priority == JETSAM_PRIORITY_IDLE_DEFERRED);
#if CONFIG_JETSAM
#pragma unused(cause)
	/*
	 * Don't generate logs for steady-state idle-exit kills,
	 * unless it is overridden for debug or by the device
	 * tree.
	 */

	return !is_idle_priority || memorystatus_idle_snapshot;

#else /* CONFIG_JETSAM */
	/*
	 * Don't generate logs for steady-state idle-exit kills,
	 * unless
	 * - it is overridden for debug or by the device
	 * tree.
	 * OR
	 * - the kill causes are important i.e. not kMemorystatusKilledIdleExit
	 */

	boolean_t snapshot_eligible_kill_cause = (is_reason_thrashing(cause) || is_reason_zone_map_exhaustion(cause));
	return !is_idle_priority || memorystatus_idle_snapshot || snapshot_eligible_kill_cause;
#endif /* CONFIG_JETSAM */
}


static boolean_t
memorystatus_act_on_hiwat_processes(uint32_t *errors, uint32_t *hwm_kill, bool *post_snapshot, uint64_t *memory_reclaimed)
{
	boolean_t purged = FALSE, killed = FALSE;

	*memory_reclaimed = 0;
	killed = memorystatus_kill_hiwat_proc(errors, &purged, memory_reclaimed);

	if (killed) {
		*hwm_kill = *hwm_kill + 1;
		*post_snapshot = TRUE;
		return TRUE;
	} else {
		if (purged == FALSE) {
			/* couldn't purge and couldn't kill */
			os_atomic_store(&memorystatus_hwm_candidates, false, relaxed);
		}
	}

	return killed;
}

/*
 * Purge kernel memory caches
 */
static void
memstat_purge_caches(jetsam_state_t state)
{
	memorystatus_log("memorystatus: purging kernel memory caches\n");

	uint64_t pmap_released = pmap_release_pages_fast();
	memorystatus_log("memorystatus: recovered %llu pages from pmap\n",
	    pmap_released);

	/*
	 * Only purge corpses once per jetsam event. No new corpses can be created
	 * after the initial purge (block_corpses)
	 */
	if (!state->corpse_list_purged) {
		memorystatus_log("memorystatus: purging all corpses\n");
		os_atomic_inc(&block_corpses, relaxed);
		assert(block_corpses > 0);
		if (total_corpses_count() > 0) {
			task_purge_all_corpses();
		} else {
			memorystatus_log("memorystatus: no corpses to purge\n");
		}
		state->corpse_list_purged = true;
	}

#if CONFIG_DEFERRED_RECLAIM
	memorystatus_log("memorystatus: reclaiming all deferred user memory\n");
	size_t vmdr_bytes_reclaimed;
	vm_deferred_reclamation_gc(RECLAIM_GC_DRAIN, &vmdr_bytes_reclaimed,
	    RECLAIM_NO_FAULT | RECLAIM_NO_WAIT);
	memorystatus_log("memorystatus: purged %zu KiB of deferred user memory\n",
	    vmdr_bytes_reclaimed >> 10);
#endif /* CONFIG_DEFERRED_RECLAIM */

	/* TODO: estimate wired memory recovered from zone_gc */
	memorystatus_log("memorystatus: trimming kernel zone allocator\n");
	zone_gc_trim();
}

static void
memstat_no_victim(jetsam_state_t state,
    memorystatus_kill_cause_t cause)
{
	/*
	 * We tried to kill a process, but failed to find anyone to kill. It's
	 * possible we chose not to because we reclaimed some purgeable memory or
	 * hit this thread's priority limit.
	 */
	assert3u(state->memory_reclaimed, ==, 0);
	if (state->limit_to_low_bands) {
		/*
		 * This thread isn't allowed to reach the high bands -- no need to overreact.
		 */
		return;
	}
	/*
	 * We should have found someone to kill. Either we failed because of a transient
	 * error or we've run out of candidates and the issue is caused by the kernel.
	 */
	memorystatus_log("memorystatus: failed to find a %s victim!\n", memstat_kill_cause_name[cause]);
	if (state->errors && !state->errors_cleared) {
		/*
		 * It's possible that all of the kill candidates had the error bit set
		 * (e.g. because we caught them in exec()). Clear all the error bits and
		 * try to kill them one more time in the hopes that they are now killable.
		 */
		memorystatus_log("memorystatus: clearing kill errors and retrying\n");
		memorystatus_clear_errors();
		state->errors_cleared = true;
	} else {
		/* The memory may be held by a corpse or zalloc. */
		memstat_purge_caches(state);
		struct memorystatus_system_health_s health_status;
		bool is_system_healthy = memstat_check_system_health(&health_status);
		if (!is_system_healthy) {
			memorystatus_log("memorystatus: system still unhealthy after cache purge!\n");
			/*
			 * We trimmed the zones above but it's possible there is a bug with
			 * working set estimation and we needed a full drain.
			 */
			memorystatus_log_fault("memorystatus: fully draining kernel zone allocator\n");
			zone_gc_drain();
			is_system_healthy = memstat_check_system_health(&health_status);
			if (!is_system_healthy) {
				/*
				 * We've killed everything and purged all xnu caches. There is nothing
				 * left to do but panic.
				 */
				panic("memorystatus: all %s victims exhausted", memstat_kill_cause_name[cause]);
			}
		}
	}
}

/*
 * Called before jetsamming in the foreground band in the hope that we'll
 * avoid a jetsam.
 */
static void
memstat_approaching_fg_band(jetsam_state_t state)
{
	memorystatus_log("memorystatus: jetsam is approaching JETSAM_PRIORITY_FOREGROUND\n");
	if (memorystatus_should_issue_fg_band_notify) {
		memorystatus_broadcast_jetsam_pressure(kVMPressureForegroundJetsam);
	}
	memstat_purge_caches(state);
}

unsigned int jld_eval_aggressive_count = 0;
uint64_t  jld_timestamp_msecs = 0;
int       jld_idle_kill_candidates = 0;

/*
 * Progressively raise the maximum priority to aggressively kill to
 * when a jetsam loop is detected. Background work often happens at
 * @c JETSAM_PRIORITY_MAIL. Start there and elevate as needed if
 * the jetsam loop re-occurs in a short time window.
 */
int jld_max_priority_arr[] = {
	JETSAM_PRIORITY_MAIL,
	JETSAM_PRIORITY_MAIL,
	JETSAM_PRIORITY_UI_SUPPORT,
	JETSAM_PRIORITY_UI_SUPPORT,
	JETSAM_PRIORITY_DRIVER_APPLE,
};
#define JLD_MAX_PRIORITY_ARR_COUNT (sizeof(jld_max_priority_arr) / sizeof(jld_max_priority_arr[0]))

static bool
memorystatus_act_aggressive(jetsam_state_t state, uint32_t cause, os_reason_t jetsam_reason)
{
	boolean_t killed;
	uint32_t errors = 0;
	uint64_t footprint_of_killed_proc = 0;
	int elevated_bucket_count = 0, maximum_kills = 0, band = 0;
	state->memory_reclaimed = 0;

	unsigned int iteration_no = jld_eval_aggressive_count++;
	int max_kill_pri = jld_max_priority_arr[MIN(iteration_no, JLD_MAX_PRIORITY_ARR_COUNT - 1)];
	assert3u(max_kill_pri, <=, MEMSTAT_BUCKET_COUNT);

	if (max_kill_pri >= JETSAM_PRIORITY_FOREGROUND) {
		memstat_approaching_fg_band(state);
	}

	proc_list_lock();
	elevated_bucket_count = memstat_bucket[JETSAM_PRIORITY_ELEVATED_INACTIVE].count;
	proc_list_unlock();

	/* Visit elevated processes first */
	while (elevated_bucket_count) {
		elevated_bucket_count--;

		/*
		 * memorystatus_kill_elevated_process() drops a reference,
		 * so take another one so we can continue to use this exit reason
		 * even after it returns.
		 */

		os_reason_ref(jetsam_reason);
		killed = memorystatus_kill_elevated_process(
			cause,
			jetsam_reason,
			JETSAM_PRIORITY_ELEVATED_INACTIVE,
			jld_eval_aggressive_count,
			&errors, &footprint_of_killed_proc);
		if (killed) {
			state->post_snapshot = true;
			state->memory_reclaimed += footprint_of_killed_proc;
			if (!memstat_evaluate_page_shortage(NULL, NULL, NULL, NULL)) {
				/*
				 * System is no longer under pressure --
				 * bail early because the pressure was
				 * coming from an inactive process
				 */
				return true;
			}
		} else {
			/*
			 * No pinned processes left to kill.
			 * Abandon elevated band.
			 */
			break;
		}
	}

	proc_list_lock();
	for (band = JETSAM_PRIORITY_IDLE; band < max_kill_pri; band++) {
		maximum_kills += memstat_bucket[band].count;
	}
	proc_list_unlock();
	maximum_kills *= memorystatus_jld_max_kill_loops;
	/*
	 * memorystatus_kill_processes_aggressive() allocates its own
	 * jetsam_reason so the kMemorystatusKilledProcThrashing cause
	 * is consistent throughout the aggressive march.
	 */
	killed = memorystatus_kill_processes_aggressive(
		kMemorystatusKilledProcThrashing,
		jld_eval_aggressive_count,
		max_kill_pri,
		maximum_kills,
		&errors, &footprint_of_killed_proc);

	if (killed) {
		/* Always generate logs after aggressive kill */
		state->post_snapshot = true;
		state->memory_reclaimed += footprint_of_killed_proc;
		state->jld_idle_kills = 0;
	}

	return killed;
}

/*
 * Sets up a new jetsam thread.
 */
static void
memorystatus_thread_init(jetsam_state_t jetsam_thread)
{
	char name[32];
	thread_wire_internal(host_priv_self(), current_thread(), TRUE, NULL);
	snprintf(name, 32, "VM_memorystatus_%d", jetsam_thread->index + 1);

	/* Limit all but one thread to the lower jetsam bands, as that's where most of the victims are. */
	if (jetsam_thread->index == 0) {
		if (vm_pageout_state.vm_restricted_to_single_processor == TRUE) {
			thread_vm_bind_group_add();
		}
		jetsam_thread->limit_to_low_bands = false;
	} else {
		jetsam_thread->limit_to_low_bands = true;
	}
#if CONFIG_THREAD_GROUPS
	thread_group_vm_add();
#endif
	thread_set_thread_name(current_thread(), name);
	sched_cond_init(&(jetsam_thread->jt_wakeup_cond));
	jetsam_thread->inited = true;
}

/*
 * Create a new jetsam reason from the given kill cause.
 */
static os_reason_t
create_jetsam_reason(memorystatus_kill_cause_t cause)
{
	os_reason_t jetsam_reason = OS_REASON_NULL;

	jetsam_reason_t reason_code = (jetsam_reason_t)cause;
	assert3u(reason_code, <=, JETSAM_REASON_MEMORYSTATUS_MAX);

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, reason_code);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus: failed to allocate jetsam reason for cause %u\n", cause);
	}
	return jetsam_reason;
}

/*
 * Do one kill as we're marching up the priority bands.
 * This is a wrapper around memstat_kill_top_process that also
 * sets post_snapshot, tracks jld_idle_kills, and notifies if we're appraoching the fg band.
 */
static bool
memstat_do_priority_kill(jetsam_state_t state,
    uint32_t kill_cause, int32_t max_priority, memstat_kill_options_t options)
{
	os_reason_t jetsam_reason = OS_REASON_NULL;
	bool killed = false;
	int priority;

	jetsam_reason = create_jetsam_reason(kill_cause);
	/*
	 * memstat_kill_top_process() drops a reference,
	 * so take another one so we can continue to use this exit reason
	 * even after it returns
	 */
	os_reason_ref(jetsam_reason);

	if (state->sort_flag) {
		options |= MEMSTAT_SORT_BUCKET;
	}
	/* LRU */
	killed = memstat_kill_top_process(kill_cause, jetsam_reason, max_priority,
	    options, &priority, &state->errors, &state->memory_reclaimed);
	state->sort_flag = false;

	if (killed) {
		if (memorystatus_should_post_snapshot(priority, kill_cause) == TRUE) {
			state->post_snapshot = true;
		}

		/* Jetsam Loop Detection */
		if (memorystatus_jld_enabled == TRUE) {
			if (priority <= applications_aging_band) {
				state->jld_idle_kills++;
			} else {
				/*
				 * We've reached into bands beyond idle deferred.
				 * We make no attempt to monitor them
				 */
			}
		}

		if (priority >= JETSAM_PRIORITY_FREEZER && !state->fg_approached) {
			state->fg_approached = true;
			memstat_approaching_fg_band(state);
		}
		if (priority >= JETSAM_PRIORITY_BACKGROUND && !state->bg_approached) {
			state->bg_approached = true;
			memorystatus_broadcast_jetsam_pressure(kVMPressureBackgroundJetsam);
		}
	}
	os_reason_free(jetsam_reason);

	return killed;
}

static bool
memstat_perform_no_paging_space_action(memorystatus_kill_cause_t cause)
{
#if !CONFIG_JETSAM
	uint64_t now = mach_absolute_time();
	os_atomic_store(&last_no_space_action_ts, now, relaxed);

	bool should_notify = no_paging_space_action(cause);
	if (should_notify) {
		/*
		 * Put up the "Out of Application Memory" dialogue. The user will be
		 * prompted to select applications to Force Quit.
		 */
		memorystatus_log("memorystatus: sending out-of-application memory knote\n");
		memorystatus_send_low_swap_note();
		return false;
	}
	return true;
#else /* CONFIG_JETSAM */
	(void)cause;
	panic("No-Paging-Space Action unsupported on this platform");
#endif /* !CONFIG_JETSAM */
}

static bool
memorystatus_do_action(jetsam_state_t state, memorystatus_action_t action, memorystatus_kill_cause_t kill_cause)
{
	bool killed = false;
	os_reason_t jetsam_reason = OS_REASON_NULL;

	switch (action) {
	case MEMORYSTATUS_KILL_HIWATER:
		killed = memorystatus_act_on_hiwat_processes(&state->errors, &state->hwm_kills,
		    &state->post_snapshot, &state->memory_reclaimed);
		break;
	case MEMORYSTATUS_KILL_AGGRESSIVE:
		jetsam_reason = create_jetsam_reason(kill_cause);
		killed = memorystatus_act_aggressive(state, kill_cause, jetsam_reason);
		os_reason_free(jetsam_reason);
		break;
	case MEMORYSTATUS_KILL_TOP_PROCESS:
		killed = memstat_do_priority_kill(state, kill_cause, JETSAM_PRIORITY_MAX, 0);
		break;
	case MEMORYSTATUS_WAKE_SWAPPER:
		memorystatus_log_info(
			"memorystatus_do_action: Waking up swap thread. memorystatus_available_pages: %llu\n",
			(uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES);
		vm_swapout_wakeup();
		break;
	case MEMORYSTATUS_PROCESS_SWAPIN_QUEUE:
		memorystatus_log_info(
			"memorystatus_do_action: Processing swapin queue of length: %u memorystatus_available_pages: %llu\n",
			c_late_swappedin_count, (uint64_t) MEMORYSTATUS_LOG_AVAILABLE_PAGES);
		vm_compressor_process_special_swapped_in_segments();
		break;
	case MEMORYSTATUS_KILL_SUSPENDED_SWAPPABLE:
		killed = memstat_do_priority_kill(state, kill_cause, JETSAM_PRIORITY_BACKGROUND - 1, MEMSTAT_ONLY_SWAPPABBLE);
		break;
	case MEMORYSTATUS_KILL_SWAPPABLE:
		killed = memstat_do_priority_kill(state, kill_cause, JETSAM_PRIORITY_MAX, MEMSTAT_ONLY_SWAPPABBLE);
		break;
	case MEMORYSTATUS_KILL_IDLE:
		killed = memstat_do_priority_kill(state, kill_cause, JETSAM_PRIORITY_IDLE, 0);
		break;
	case MEMORYSTATUS_KILL_LONG_IDLE:
		killed = memstat_do_priority_kill(state, kill_cause, JETSAM_PRIORITY_IDLE, MEMSTAT_ONLY_LONG_IDLE);
		break;
	case MEMORYSTATUS_NO_PAGING_SPACE:
		killed = memstat_perform_no_paging_space_action(kill_cause);
		break;
	case MEMORYSTATUS_PURGE_CACHES:
		memstat_purge_caches(state);
		killed = true;
		break;
	case MEMORYSTATUS_KILL_NONE:
		panic("memorystatus_do_action: Impossible! memorystatus_do_action called with action = NONE\n");
	}
	return killed;
}

void
memorystatus_post_snapshot()
{
	proc_list_lock();
	size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
	    sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count);
	uint64_t timestamp_now = mach_absolute_time();
	memorystatus_jetsam_snapshot->notification_time = timestamp_now;
	memorystatus_jetsam_snapshot->js_gencount++;
	if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 ||
	    timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) {
		proc_list_unlock();
		int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
		if (!ret) {
			proc_list_lock();
			memorystatus_jetsam_snapshot_last_timestamp = timestamp_now; proc_list_unlock();
		}
	} else {
		proc_list_unlock();
	}
}

#if JETSAM_ZPRINT_SNAPSHOT

/*
 *  Called by memorystatus_update_jetsam_snapshot_entry_locked to take a zprint snapshot.
 */
static void
memorystatus_collect_jetsam_snapshot_zprint(void)
{
	unsigned int new_meminfo_cnt;

	jzs_zone_cnt = zone_max_zones();

	new_meminfo_cnt = vm_page_diagnose_estimate();
	if (new_meminfo_cnt > jzs_meminfo_cnt) {
		jzs_meminfo = krealloc_data_tag(jzs_meminfo,
		    jzs_meminfo_cnt * sizeof(mach_memory_info_t),
		    new_meminfo_cnt * sizeof(mach_memory_info_t),
		    Z_WAITOK,
		    VM_KERN_MEMORY_DIAG);

		jzs_meminfo_cnt = new_meminfo_cnt;
	}

	mach_memory_info_sample(jzs_names, jzs_info, jzs_coalesce, &jzs_zone_cnt, jzs_meminfo, jzs_meminfo_cnt, true);
}

#endif /* JETSAM_ZPRINT_SNAPSHOT */

/*
 * Main entrypoint for the memorystatus thread.
 * This thread is woken up when we're low on one of the following resources:
 * - available pages (free + filebacked)
 * - zone memory
 * - compressor space
 *
 * Or when thrashing is detected in the compressor or file cache.
 */
static void
memorystatus_thread_internal(jetsam_state_t state)
{
	uint64_t total_memory_reclaimed = 0;
	bool highwater_remaining = true;
	bool swappable_apps_remaining = false;
	bool suspended_swappable_apps_remaining = false;

#if CONFIG_JETSAM
	swappable_apps_remaining = memorystatus_swap_all_apps;
	suspended_swappable_apps_remaining = memorystatus_swap_all_apps;
#endif /* CONFIG_JETSAM */

	assert(state != NULL);
	state->jld_idle_kills = 0;
	state->errors = 0;
	state->errors_cleared = false;
	state->hwm_kills = 0;
	state->sort_flag = true;
	state->corpse_list_purged = false;
	state->bg_approached = false;
	state->fg_approached = false;
	state->post_snapshot = false;
	state->memory_reclaimed = 0;

	if (state->inited == FALSE) {
		/*
		 * It's the first time the thread has run, so just mark the thread as privileged and block.
		 */
		memorystatus_thread_init(state);
		sched_cond_wait(&state->jt_wakeup_cond, THREAD_UNINT, memorystatus_thread);
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_SCAN) | DBG_FUNC_START,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, memorystatus_jld_enabled, memorystatus_jld_eval_period_msecs, jld_eval_aggressive_count);

	extern uint32_t c_segment_count;
	extern mach_timespec_t major_compact_ts;
	clock_sec_t now;
	clock_nsec_t nsec;
	clock_get_system_nanotime(&now, &nsec);
	mach_timespec_t major_compact_diff = {.tv_sec = (int)now, .tv_nsec = nsec};
	SUB_MACH_TIMESPEC(&major_compact_diff, &major_compact_ts);
	memorystatus_log_info(
		"memorystatus: c_segment_count=%u major compaction occurred %u seconds ago\n",
		c_segment_count, major_compact_diff.tv_sec);

	/*
	 * Jetsam aware version.
	 *
	 * The VM pressure notification thread is working its way through clients in parallel.
	 *
	 * So, while the pressure notification thread is targeting processes in order of
	 * increasing jetsam priority, we can hopefully reduce / stop its work by killing
	 * any processes that have exceeded their highwater mark.
	 *
	 * If we run out of HWM processes and our available pages drops below the critical threshold, then,
	 * we target the least recently used process in order of increasing jetsam priority (exception: the FG band).
	 */
	while (true) {
		bool killed;
		state->memory_reclaimed = 0;
		uint32_t cause = 0;

		memorystatus_action_t action = memorystatus_pick_action(state, &cause,
		    highwater_remaining, suspended_swappable_apps_remaining, swappable_apps_remaining,
		    &state->jld_idle_kills);
		if (action == MEMORYSTATUS_KILL_NONE) {
			break;
		}

		if (cause == kMemorystatusKilledVMCompressorThrashing || cause == kMemorystatusKilledVMCompressorSpaceShortage) {
			memorystatus_log("memorystatus: killing due to \"%s\" - compression_ratio=%u\n", memstat_kill_cause_name[cause], vm_compression_ratio());
		}

		killed = memorystatus_do_action(state, action, cause);
		total_memory_reclaimed += state->memory_reclaimed;

		if (!killed && !state->memory_reclaimed) {
			switch (action) {
			case MEMORYSTATUS_KILL_HIWATER:
				highwater_remaining = false;
				break;
			case MEMORYSTATUS_KILL_SWAPPABLE:
				swappable_apps_remaining = false;
				suspended_swappable_apps_remaining = false;
				break;
			case MEMORYSTATUS_KILL_SUSPENDED_SWAPPABLE:
				suspended_swappable_apps_remaining = false;
				break;
			case MEMORYSTATUS_KILL_TOP_PROCESS:
				memstat_no_victim(state, cause);
				break;
			default:
				memorystatus_log("memorystatus: no victim found (action: %d)\n", action);
				break;
			}
		} else {
			/* We successfully killed a process */
			if (cause == kMemorystatusKilledVMCompressorThrashing || cause == kMemorystatusKilledVMCompressorSpaceShortage) {
				memorystatus_log("memorystatus: post-jetsam compressor fragmentation_level=%u\n", vm_compressor_fragmentation_level());
			}
			/* Always re-check for highwater and swappable kills after doing a kill. */
			highwater_remaining = true;
			swappable_apps_remaining = true;
			suspended_swappable_apps_remaining = true;
		}


		/*
		 * If we did a kill on behalf of another subsystem (compressor or zalloc)
		 * notify them.
		 */
		if (killed && is_reason_thrashing(cause)) {
			os_atomic_store(&memorystatus_compressor_space_shortage, false, release);
#if CONFIG_PHANTOM_CACHE
			os_atomic_store(&memorystatus_phantom_cache_pressure, false, release);
#endif /* CONFIG_PHANTOM_CACHE */
#if CONFIG_JETSAM
			vm_thrashing_jetsam_done();
#endif /* CONFIG_JETSAM */
		} else if (killed && is_reason_zone_map_exhaustion(cause)) {
			os_atomic_store(&memorystatus_zone_map_is_exhausted, false, release);
		} else if (killed && cause == kMemorystatusKilledVMPageoutStarvation) {
			os_atomic_store(&memorystatus_pageout_starved, false, release);
		}
	}

	if (state->errors) {
		memorystatus_clear_errors();
	}

	if (state->post_snapshot) {
		memorystatus_post_snapshot();
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_SCAN) | DBG_FUNC_END,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, total_memory_reclaimed);

	if (state->corpse_list_purged) {
		os_atomic_dec(&block_corpses, relaxed);
		assert(block_corpses >= 0);
	}
}

OS_NORETURN
static void
memorystatus_thread(void *param __unused, wait_result_t wr __unused)
{
	jetsam_state_t jetsam_thread = jetsam_current_thread();
	sched_cond_ack(&(jetsam_thread->jt_wakeup_cond));
	while (1) {
		memorystatus_thread_internal(jetsam_thread);
		sched_cond_wait(&(jetsam_thread->jt_wakeup_cond), THREAD_UNINT, memorystatus_thread);
	}
}

/*
 * Callback invoked when allowable physical memory footprint exceeded
 * (dirty pages + IOKit mappings)
 *
 * This is invoked for both advisory, non-fatal per-task high watermarks,
 * as well as the fatal task memory limits.
 */
void
memorystatus_on_ledger_footprint_exceeded(boolean_t warning, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal)
{
	os_reason_t jetsam_reason = OS_REASON_NULL;

	proc_t p = current_proc();

#if VM_PRESSURE_EVENTS
	if (warning == TRUE) {
		/*
		 * This is a warning path which implies that the current process is close, but has
		 * not yet exceeded its per-process memory limit.
		 */
		if (memorystatus_warn_process(p, memlimit_is_active, memlimit_is_fatal, FALSE /* not exceeded */) != TRUE) {
			/* Print warning, since it's possible that task has not registered for pressure notifications */
			memorystatus_log_debug(
				"memorystatus_on_ledger_footprint_exceeded: failed to warn %s [%d] (exiting, or no handler registered?).\n",
				proc_best_name(p), proc_getpid(p));
		}
		return;
	}
#endif /* VM_PRESSURE_EVENTS */

	if (memlimit_is_fatal) {
		/*
		 * If this process has no high watermark or has a fatal task limit, then we have been invoked because the task
		 * has violated either the system-wide per-task memory limit OR its own task limit.
		 */
		jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_PERPROCESSLIMIT);
		if (jetsam_reason == NULL) {
			memorystatus_log_error("task_exceeded footprint: failed to allocate jetsam reason\n");
		} else if (corpse_for_fatal_memkill && proc_send_synchronous_EXC_RESOURCE(p) == FALSE) {
			/* Set OS_REASON_FLAG_GENERATE_CRASH_REPORT to generate corpse */
			jetsam_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
		}

		if (memstat_kill_process_sync(proc_getpid(p), kMemorystatusKilledPerProcessLimit, jetsam_reason) != TRUE) {
			memorystatus_log_error("task_exceeded_footprint: failed to kill the current task (exiting?).\n");
		}
	} else {
		/*
		 * HWM offender exists. Done without locks or synchronization.
		 * See comment near its declaration for more details.
		 */
		os_atomic_store(&memorystatus_hwm_candidates, true, release);
		_memstat_consider_waking_jetsam_thread();

#if VM_PRESSURE_EVENTS
		/*
		 * The current process is not in the warning path.
		 * This path implies the current process has exceeded a non-fatal (soft) memory limit.
		 * Failure to send note is ignored here.
		 */
		(void)memorystatus_warn_process(p, memlimit_is_active, memlimit_is_fatal, TRUE /* exceeded */);

#endif /* VM_PRESSURE_EVENTS */
	}
}

void
memorystatus_log_exception(const int max_footprint_mb, boolean_t memlimit_is_active, boolean_t memlimit_is_fatal)
{
	proc_t p = current_proc();

	/*
	 * The limit violation is logged here, but only once per process per limit.
	 * Soft memory limit is a non-fatal high-water-mark
	 * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit.
	 */

	memorystatus_log("memorystatus: %s [%d] exceeded mem limit: %s%s %d MB (%s)\n",
	    ((p && *p->p_name) ? p->p_name : "unknown"), (p ? proc_getpid(p) : -1), (memlimit_is_active ? "Active" : "Inactive"),
	    (memlimit_is_fatal  ? "Hard" : "Soft"), max_footprint_mb,
	    (memlimit_is_fatal  ? "fatal" : "non-fatal"));
}

void
memorystatus_log_diag_threshold_exception(const int diag_threshold_value)
{
	proc_t p = current_proc();

	/*
	 * The limit violation is logged here, but only once per process per limit.
	 * Soft memory limit is a non-fatal high-water-mark
	 * Hard memory limit is a fatal custom-task-limit or system-wide per-task memory limit.
	 */

	memorystatus_log("memorystatus: %s [%d] exceeded diag threshold limit: %d MB \n",
	    ((p && *p->p_name) ? p->p_name : "unknown"), (p ? proc_getpid(p) : -1), diag_threshold_value);
}

void
memorystatus_on_conclave_limit_exceeded(const int max_footprint_mb)
{
	os_reason_t jetsam_reason = OS_REASON_NULL;
	proc_t p = current_proc();

	/*
	 * The limit violation is logged here; it's always fatal.
	 */
	memorystatus_log("memorystatus: %s [%d] exceeded conclave limit: %d MB \n",
	    ((p && *p->p_name) ? p->p_name : "unknown"), (p ? proc_getpid(p) : -1), max_footprint_mb);

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_CONCLAVELIMIT);
	if (jetsam_reason == NULL) {
		memorystatus_log_error("task_exceeded_conclave: failed to allocate jetsam reason\n");
	} else if (corpse_for_fatal_memkill && proc_send_synchronous_EXC_RESOURCE(p) == FALSE) {
		/* Set OS_REASON_FLAG_GENERATE_CRASH_REPORT to generate corpse */
		jetsam_reason->osr_flags |= OS_REASON_FLAG_GENERATE_CRASH_REPORT;
	}

	if (memstat_kill_process_sync(proc_getpid(p), kMemorystatusKilledConclaveLimit, jetsam_reason) != TRUE) {
		memorystatus_log_error("task_exceeded_conclave: failed to kill the current task (exiting?).\n");
	}
}

/*
 * Description:
 *	Evaluates process state to determine which limit
 *	should be applied (active vs. inactive limit).
 *
 *	Return: TRUE if active
 *		False if inactive
 */
static bool
memstat_proc_is_active_locked(proc_t p)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	if (_memstat_proc_is_elevated(p) &&
	    (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE ||
	    p->p_memstat_effectivepriority == JETSAM_PRIORITY_FREEZER)) {
		/* This process is sitting in an elevated inactive band. */
		if (p->p_memstat_effectivepriority == JETSAM_PRIORITY_ELEVATED_INACTIVE) {
			/*
			 * This process is in an elevated band and may be doing background
			 * work.
			 */
			return true;
		} else {
			/* This process is frozen. */
			return false;
		}
	} else if (_memstat_proc_is_tracked(p)) {
		/*
		 * Process has enrolled in ActivityTracking. Its limit will be
		 * determined based on whether it is clean or dirty.
		 */
		if (_memstat_proc_is_dirty(p)) {
			/* Dirty processes are always active */
			return true;
		} else if (_memstat_proc_can_idle_exit(p) &&
		    p->p_memstat_effectivepriority > JETSAM_PRIORITY_IDLE) {
			/*
			 * This process is clean and supports idle exit, but has not made
			 * its way to the idle band. It is either aging in the deferred
			 * idle band or has a RunningBoard assertion that is keeping it
			 * from going idle.
			 */
			return true;
		} else {
			/*
			 * This process is clean and either:
			 *   - does not support idle exit
			 *   or
			 *   - does support idle exit and is now idle
			 */
			return false;
		}
	} else if (_memstat_proc_is_managed(p)) {
		/*
		 * RunningBoard-managed processes are active if they have any
		 * outstanding assertions
		 */
		return _memstat_proc_has_priority_assertion(p);
	} else {
		/*
		 * Unmanaged and untracked processes receive an active limit unless
		 * they are completely idle.
		 */
		return p->p_memstat_effectivepriority > JETSAM_PRIORITY_IDLE;
	}
}

static bool
memstat_kill_process_sync(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason)
{
	bool killed;

	uint32_t errors = 0;
	uint64_t memory_reclaimed = 0;

	/* Validate inputs */
	if (victim_pid == 0) {
		return false;
	}

	if (victim_pid == -1) {
		uint32_t max_priority;
#if CONFIG_JETSAM
		max_priority = JETSAM_PRIORITY_MAX;
#else /* !CONFIG_JETSAM */
		if (kill_on_no_paging_space ||
		    cause == kMemorystatusKilledZoneMapExhaustion) {
			max_priority = JETSAM_PRIORITY_MAX;
		} else if (cause == kMemorystatusKilledSustainedPressure) {
			max_priority = memstat_sustained_pressure_max_pri;
		} else {
			max_priority = JETSAM_PRIORITY_IDLE;
		}
#endif /* CONFIG_JETSAM */
		/* No pid, so kill first process */
		killed = memstat_kill_top_process(cause, jetsam_reason,
		    max_priority, MEMSTAT_SORT_BUCKET, NULL, &errors, &memory_reclaimed);
	} else {
		killed = memorystatus_kill_specific_process(victim_pid, cause, jetsam_reason);
	}

	if (errors) {
		memorystatus_clear_errors();
	}

	if (killed) {
		/* Fire off snapshot notification */
		proc_list_lock();
		size_t snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) +
		    sizeof(memorystatus_jetsam_snapshot_entry_t) * memorystatus_jetsam_snapshot_count;
		uint64_t timestamp_now = mach_absolute_time();
		memorystatus_jetsam_snapshot->notification_time = timestamp_now;
		if (memorystatus_jetsam_snapshot_count > 0 && (memorystatus_jetsam_snapshot_last_timestamp == 0 ||
		    timestamp_now > memorystatus_jetsam_snapshot_last_timestamp + memorystatus_jetsam_snapshot_timeout)) {
			proc_list_unlock();
			int ret = memorystatus_send_note(kMemorystatusSnapshotNote, &snapshot_size, sizeof(snapshot_size));
			if (!ret) {
				proc_list_lock();
				memorystatus_jetsam_snapshot_last_timestamp = timestamp_now;
				proc_list_unlock();
			}
		} else {
			proc_list_unlock();
		}
	}

	return killed;
}

/*
 * Jetsam a specific process.
 */
static bool
memorystatus_kill_specific_process(pid_t victim_pid, uint32_t cause, os_reason_t jetsam_reason)
{
	bool killed;
	proc_t p;
	uint64_t killtime = 0;
	uint64_t time_in_priority_band_secs = 0;
	uint64_t footprint_of_killed_proc;
	clock_sec_t     tv_sec;
	clock_usec_t    tv_usec;
	uint32_t        tv_msec;

	/* TODO - add a victim queue and push this into the main jetsam thread */

	p = proc_find(victim_pid);
	if (!p) {
		os_reason_free(jetsam_reason);
		return false;
	}

	proc_list_lock();

	if (_memstat_proc_was_killed(p)) {
		/*
		 * Someone beat us to this kill.
		 * Nothing to do here.
		 */
		proc_list_unlock();
		os_reason_free(jetsam_reason);
		proc_rele(p);
		return false;
	}
	p->p_memstat_state |= P_MEMSTAT_TERMINATED;

	if (memorystatus_jetsam_snapshot_count == 0) {
		memorystatus_init_jetsam_snapshot_locked(NULL, 0);
	}

	killtime = mach_absolute_time();
	absolutetime_to_microtime(killtime, &tv_sec, &tv_usec);
	tv_msec = tv_usec / 1000;

	memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime);

	proc_list_unlock();

	absolutetime_to_nanoseconds(killtime - p->p_memstat_prio_start, &time_in_priority_band_secs);
	time_in_priority_band_secs /= NSEC_PER_SEC;

	killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc);

	memorystatus_log("%lu.%03d memorystatus: killing_specific_process pid %d [%s] (%s %d %llus rf:%s type:%s) %lluKB - memorystatus_available_pages: %llu\n",
	    (unsigned long)tv_sec, tv_msec, victim_pid, ((p && *p->p_name) ? p->p_name : "unknown"),
	    memstat_kill_cause_name[cause], (p ? p->p_memstat_effectivepriority: -1), time_in_priority_band_secs,
	    (p ? _memstat_relaunch_flags_description(p->p_memstat_relaunch_flags) : 0), _memstat_proc_type_description(p),
	    footprint_of_killed_proc >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES);

	if (!killed) {
		proc_list_lock();
		p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
		proc_list_unlock();
	}

	proc_rele(p);

	return killed;
}


/*
 * Toggle the P_MEMSTAT_SKIP bit.
 * Takes the proc_list_lock.
 */
void
proc_memstat_skip(proc_t p, boolean_t set)
{
#if DEVELOPMENT || DEBUG
	if (p) {
		proc_list_lock();
		if (set == TRUE) {
			p->p_memstat_state |= P_MEMSTAT_SKIP;
		} else {
			p->p_memstat_state &= ~P_MEMSTAT_SKIP;
		}
		proc_list_unlock();
	}
#else
#pragma unused(p, set)
	/*
	 * do nothing
	 */
#endif /* DEVELOPMENT || DEBUG */
	return;
}


#if CONFIG_JETSAM
/*
 * This is invoked when cpulimits have been exceeded while in fatal mode.
 * The jetsam_flags do not apply as those are for memory related kills.
 * We call this routine so that the offending process is killed with
 * a non-zero exit status.
 */
void
jetsam_on_ledger_cpulimit_exceeded(void)
{
	int retval = 0;
	int jetsam_flags = 0;  /* make it obvious */
	proc_t p = current_proc();
	os_reason_t jetsam_reason = OS_REASON_NULL;

	memorystatus_log("memorystatus: killing %s [%d] due to cpulimit "
	    "violation\n", proc_best_name(p), proc_getpid(p));

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_CPULIMIT);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus: unable to allocate memory for jetsam reason\n");
	}

	retval = jetsam_do_kill(p, jetsam_flags, jetsam_reason);

	if (retval) {
		memorystatus_log_error("memorystatus: failed to kill current task (exiting?).\n");
	}
}

#endif /* CONFIG_JETSAM */

static void
memorystatus_get_task_memory_region_count(task_t task, uint64_t *count)
{
	assert(task);
	assert(count);

	*count = get_task_memory_region_count(task);
}


#define MEMORYSTATUS_VM_MAP_FORK_ALLOWED     0x100000000
#define MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED 0x200000000

#if DEVELOPMENT || DEBUG

/*
 * Sysctl only used to test memorystatus_allowed_vm_map_fork() path.
 *   set a new pidwatch value
 *	or
 *   get the current pidwatch value
 *
 * The pidwatch_val starts out with a PID to watch for in the map_fork path.
 * Its value is:
 * - OR'd with MEMORYSTATUS_VM_MAP_FORK_ALLOWED if we allow the map_fork.
 * - OR'd with MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED if we disallow the map_fork.
 * - set to -1ull if the map_fork() is aborted for other reasons.
 */

uint64_t memorystatus_vm_map_fork_pidwatch_val = 0;

static int sysctl_memorystatus_vm_map_fork_pidwatch SYSCTL_HANDLER_ARGS {
#pragma unused(oidp, arg1, arg2)

	uint64_t new_value = 0;
	uint64_t old_value = 0;
	int error = 0;

	/*
	 * The pid is held in the low 32 bits.
	 * The 'allowed' flags are in the upper 32 bits.
	 */
	old_value = memorystatus_vm_map_fork_pidwatch_val;

	error = sysctl_io_number(req, old_value, sizeof(old_value), &new_value, NULL);

	if (error || !req->newptr) {
		/*
		 * No new value passed in.
		 */
		return error;
	}

	/*
	 * A new pid was passed in via req->newptr.
	 * Ignore any attempt to set the higher order bits.
	 */
	memorystatus_vm_map_fork_pidwatch_val = new_value & 0xFFFFFFFF;
	memorystatus_log_debug("memorystatus: pidwatch old_value = 0x%llx, new_value = 0x%llx\n", old_value, new_value);

	return error;
}

SYSCTL_PROC(_kern, OID_AUTO, memorystatus_vm_map_fork_pidwatch, CTLTYPE_QUAD | CTLFLAG_RW | CTLFLAG_LOCKED | CTLFLAG_MASKED,
    0, 0, sysctl_memorystatus_vm_map_fork_pidwatch, "Q", "get/set pid watched for in vm_map_fork");


/*
 * Record if a watched process fails to qualify for a vm_map_fork().
 */
void
memorystatus_abort_vm_map_fork(task_t task)
{
	if (memorystatus_vm_map_fork_pidwatch_val != 0) {
		proc_t p = get_bsdtask_info(task);
		if (p != NULL && memorystatus_vm_map_fork_pidwatch_val == (uint64_t)proc_getpid(p)) {
			memorystatus_vm_map_fork_pidwatch_val = -1ull;
		}
	}
}

static void
set_vm_map_fork_pidwatch(task_t task, uint64_t x)
{
	if (memorystatus_vm_map_fork_pidwatch_val != 0) {
		proc_t p = get_bsdtask_info(task);
		if (p && (memorystatus_vm_map_fork_pidwatch_val == (uint64_t)proc_getpid(p))) {
			memorystatus_vm_map_fork_pidwatch_val |= x;
		}
	}
}

#else /* DEVELOPMENT || DEBUG */


static void
set_vm_map_fork_pidwatch(task_t task, uint64_t x)
{
#pragma unused(task)
#pragma unused(x)
}

#endif /* DEVELOPMENT || DEBUG */

/*
 * Called during EXC_RESOURCE handling when a process exceeds a soft
 * memory limit.  This is the corpse fork path and here we decide if
 * vm_map_fork will be allowed when creating the corpse.
 * The task being considered is suspended.
 *
 * By default, a vm_map_fork is allowed to proceed.
 *
 * A few simple policy assumptions:
 *	If the device has a zero system-wide task limit,
 *	then the vm_map_fork is allowed. macOS always has a zero
 *	system wide task limit (unless overriden by a boot-arg).
 *
 *	And if a process's memory footprint calculates less
 *	than or equal to quarter of the system-wide task limit,
 *	then the vm_map_fork is allowed.  This calculation
 *	is based on the assumption that a process can
 *	munch memory up to the system-wide task limit.
 *
 *      For watchOS, which has a low task limit, we use a
 *      different value. Current task limit has been reduced
 *      to 300MB and it's been decided the limit should be 200MB.
 */
int large_corpse_count = 0;
boolean_t
memorystatus_allowed_vm_map_fork(task_t task, bool *is_large)
{
	boolean_t is_allowed = TRUE;   /* default */
	uint64_t footprint_in_bytes;
	uint64_t max_allowed_bytes;
	thread_t self = current_thread();

	*is_large = false;

	/* Jetsam in high bands blocks any new corpse */
	if (os_atomic_load(&block_corpses, relaxed) != 0) {
		memorystatus_log("memorystatus_allowed_vm_map_fork: corpse for pid %d blocked by jetsam).\n", task_pid(task));
		ktriage_record(thread_tid(self), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_CORPSE, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_CORPSE_BLOCKED_JETSAM), 0 /* arg */);
		return FALSE;
	}

	if (max_task_footprint_mb == 0) {
		set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED);
		return is_allowed;
	}

	footprint_in_bytes = get_task_phys_footprint(task);

	/*
	 * Maximum is 1/4 of the system-wide task limit by default.
	 */
	max_allowed_bytes = ((uint64_t)max_task_footprint_mb * 1024 * 1024) >> 2;

#if XNU_TARGET_OS_WATCH
	/*
	 * For watches with > 1G, use a limit of 200MB and allow
	 * one corpse at a time of up to 300MB.
	 */
#define LARGE_CORPSE_LIMIT 1
	if (sane_size > 1 * 1024 * 1024 * 1024) {
		int cnt = large_corpse_count;
		if (footprint_in_bytes > 200 * 1024 * 1024 &&
		    footprint_in_bytes <= 300 * 1024 * 1024 &&
		    cnt < LARGE_CORPSE_LIMIT &&
		    OSCompareAndSwap(cnt, cnt + 1, &large_corpse_count)) {
			*is_large = true;
			max_allowed_bytes = MAX(max_allowed_bytes, 300 * 1024 * 1024);
		} else {
			max_allowed_bytes = MAX(max_allowed_bytes, 200 * 1024 * 1024);
		}
	}
#endif /* XNU_TARGET_OS_WATCH */

#if DEBUG || DEVELOPMENT
	if (corpse_threshold_system_limit) {
		max_allowed_bytes = (uint64_t)max_task_footprint_mb * (1UL << 20);
	}
#endif /* DEBUG || DEVELOPMENT */

	if (footprint_in_bytes > max_allowed_bytes) {
		memorystatus_log("memorystatus disallowed vm_map_fork %lld  %lld\n", footprint_in_bytes, max_allowed_bytes);
		set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_NOT_ALLOWED);
		ktriage_record(thread_tid(self), KDBG_TRIAGE_EVENTID(KDBG_TRIAGE_SUBSYS_CORPSE, KDBG_TRIAGE_RESERVED, KDBG_TRIAGE_CORPSE_PROC_TOO_BIG), 0 /* arg */);
		return !is_allowed;
	}

	set_vm_map_fork_pidwatch(task, MEMORYSTATUS_VM_MAP_FORK_ALLOWED);
	return is_allowed;
}

void
memorystatus_get_task_page_counts(task_t task, uint32_t *footprint, uint32_t *max_footprint_lifetime, uint32_t *purgeable_pages)
{
	assert(task);
	assert(footprint);

	uint64_t pages;

	pages = (get_task_phys_footprint(task) / PAGE_SIZE_64);
	assert(((uint32_t)pages) == pages);
	*footprint = (uint32_t)pages;

	if (max_footprint_lifetime) {
		pages = (get_task_phys_footprint_lifetime_max(task) / PAGE_SIZE_64);
		assert(((uint32_t)pages) == pages);
		*max_footprint_lifetime = (uint32_t)pages;
	}
	if (purgeable_pages) {
		pages = (get_task_purgeable_size(task) / PAGE_SIZE_64);
		assert(((uint32_t)pages) == pages);
		*purgeable_pages = (uint32_t)pages;
	}
}

static void
memorystatus_get_task_phys_footprint_page_counts(task_t task,
    uint64_t *internal_pages, uint64_t *internal_compressed_pages,
    uint64_t *purgeable_nonvolatile_pages, uint64_t *purgeable_nonvolatile_compressed_pages,
    uint64_t *alternate_accounting_pages, uint64_t *alternate_accounting_compressed_pages,
    uint64_t *iokit_mapped_pages, uint64_t *page_table_pages, uint64_t *frozen_to_swap_pages,
    uint64_t *neural_nofootprint_total_pages)
{
	assert(task);

	if (internal_pages) {
		*internal_pages = (get_task_internal(task) / PAGE_SIZE_64);
	}

	if (internal_compressed_pages) {
		*internal_compressed_pages = (get_task_internal_compressed(task) / PAGE_SIZE_64);
	}

	if (purgeable_nonvolatile_pages) {
		*purgeable_nonvolatile_pages = (get_task_purgeable_nonvolatile(task) / PAGE_SIZE_64);
	}

	if (purgeable_nonvolatile_compressed_pages) {
		*purgeable_nonvolatile_compressed_pages = (get_task_purgeable_nonvolatile_compressed(task) / PAGE_SIZE_64);
	}

	if (alternate_accounting_pages) {
		*alternate_accounting_pages = (get_task_alternate_accounting(task) / PAGE_SIZE_64);
	}

	if (alternate_accounting_compressed_pages) {
		*alternate_accounting_compressed_pages = (get_task_alternate_accounting_compressed(task) / PAGE_SIZE_64);
	}

	if (iokit_mapped_pages) {
		*iokit_mapped_pages = (get_task_iokit_mapped(task) / PAGE_SIZE_64);
	}

	if (page_table_pages) {
		*page_table_pages = (get_task_page_table(task) / PAGE_SIZE_64);
	}

	if (neural_nofootprint_total_pages) {
		*neural_nofootprint_total_pages = (get_task_neural_nofootprint_total(task) / PAGE_SIZE_64);
	}

#if CONFIG_FREEZE
	if (frozen_to_swap_pages) {
		*frozen_to_swap_pages = (get_task_frozen_to_swap(task) / PAGE_SIZE_64);
	}
#else /* CONFIG_FREEZE */
#pragma unused(frozen_to_swap_pages)
#endif /* CONFIG_FREEZE */
}

#if CONFIG_FREEZE
/*
 * Copies the source entry into the destination snapshot.
 * Returns true on success. Fails if the destination snapshot is full.
 * Caller must hold the proc list lock.
 */
static bool
memorystatus_jetsam_snapshot_copy_entry_locked(memorystatus_jetsam_snapshot_t *dst_snapshot, unsigned int dst_snapshot_size, const memorystatus_jetsam_snapshot_entry_t *src_entry)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	assert(dst_snapshot);

	if (dst_snapshot->entry_count == dst_snapshot_size) {
		/* Destination snapshot is full. Can not be updated until it is consumed. */
		return false;
	}
	if (dst_snapshot->entry_count == 0) {
		memorystatus_init_jetsam_snapshot_header(dst_snapshot);
	}
	memorystatus_jetsam_snapshot_entry_t *dst_entry = &dst_snapshot->entries[dst_snapshot->entry_count++];
	memcpy(dst_entry, src_entry, sizeof(memorystatus_jetsam_snapshot_entry_t));
	return true;
}
#endif /* CONFIG_FREEZE */

static bool
memorystatus_init_jetsam_snapshot_entry_with_kill_locked(memorystatus_jetsam_snapshot_t *snapshot, proc_t p, uint32_t kill_cause, uint64_t killtime, memorystatus_jetsam_snapshot_entry_t **entry)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	memorystatus_jetsam_snapshot_entry_t *snapshot_list = snapshot->entries;
	size_t i = snapshot->entry_count;

	if (memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], (snapshot->js_gencount)) == TRUE) {
		*entry = &snapshot_list[i];
		(*entry)->killed       = kill_cause;
		(*entry)->jse_killtime = killtime;

		snapshot->entry_count = i + 1;
		return true;
	}
	return false;
}

/*
 * This routine only acts on the global jetsam event snapshot.
 * Updating the process's entry can race when the memorystatus_thread
 * has chosen to kill a process that is racing to exit on another core.
 */
static void
memorystatus_update_jetsam_snapshot_entry_locked(proc_t p, uint32_t kill_cause, uint64_t killtime)
{
	memorystatus_jetsam_snapshot_entry_t *entry = NULL;
	memorystatus_jetsam_snapshot_t *snapshot    = NULL;
	memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL;

	unsigned int i;
#if CONFIG_FREEZE
	bool copied_to_freezer_snapshot = false;
#endif /* CONFIG_FREEZE */

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	if (memorystatus_jetsam_snapshot_count == 0) {
		/*
		 * No active snapshot.
		 * Nothing to do.
		 */
		goto exit;
	}

	/*
	 * Sanity check as this routine should only be called
	 * from a jetsam kill path.
	 */
	assert(kill_cause != 0 && killtime != 0);

	snapshot       = memorystatus_jetsam_snapshot;
	snapshot_list  = memorystatus_jetsam_snapshot->entries;

#if JETSAM_ZPRINT_SNAPSHOT
	/*
	 * Collect the snapshot zprint info if we've reached the right priority
	 */
	if (p->p_memstat_effectivepriority >= (int)jzs_trigger_band &&
	    jzs_gencount != snapshot->js_gencount) {
		memorystatus_collect_jetsam_snapshot_zprint();
		jzs_gencount = snapshot->js_gencount;
	}
#endif

	for (i = 0; i < memorystatus_jetsam_snapshot_count; i++) {
		if (snapshot_list[i].pid == proc_getpid(p)) {
			entry = &snapshot_list[i];

			if (entry->killed || entry->jse_killtime) {
				/*
				 * We apparently raced on the exit path
				 * for this process, as it's snapshot entry
				 * has already recorded a kill.
				 */
				assert(entry->killed && entry->jse_killtime);
				break;
			}

			/*
			 * Update the entry we just found in the snapshot.
			 */

			entry->killed       = kill_cause;
			entry->jse_killtime = killtime;
			entry->jse_gencount = snapshot->js_gencount;
			entry->jse_idle_delta = p->p_memstat_idle_delta;
			entry->jse_prio_start = p->p_memstat_prio_start;
#if CONFIG_FREEZE
			entry->jse_thaw_count = p->p_memstat_thaw_count;
			entry->jse_freeze_skip_reason = p->p_memstat_freeze_skip_reason;
#else /* CONFIG_FREEZE */
			entry->jse_thaw_count = 0;
			entry->jse_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
#endif /* CONFIG_FREEZE */

			/*
			 * If a process has moved between bands since snapshot was
			 * initialized, then likely these fields changed too.
			 */
			if (entry->priority != p->p_memstat_effectivepriority) {
				strlcpy(entry->name, p->p_name, sizeof(entry->name));
				entry->priority  = p->p_memstat_effectivepriority;
				entry->state     = _memstat_build_state(p);
				entry->user_data = p->p_memstat_userdata;
				entry->fds       = p->p_fd.fd_nfiles;
			}

			/*
			 * Always update the page counts on a kill.
			 */

			uint32_t pages              = 0;
			uint32_t max_pages_lifetime = 0;
			uint32_t purgeable_pages    = 0;

			memorystatus_get_task_page_counts(proc_task(p), &pages, &max_pages_lifetime, &purgeable_pages);
			entry->pages              = (uint64_t)pages;
			entry->max_pages_lifetime = (uint64_t)max_pages_lifetime;
			entry->purgeable_pages    = (uint64_t)purgeable_pages;

			uint64_t internal_pages                        = 0;
			uint64_t internal_compressed_pages             = 0;
			uint64_t purgeable_nonvolatile_pages           = 0;
			uint64_t purgeable_nonvolatile_compressed_pages = 0;
			uint64_t alternate_accounting_pages            = 0;
			uint64_t alternate_accounting_compressed_pages = 0;
			uint64_t iokit_mapped_pages                    = 0;
			uint64_t page_table_pages                      = 0;
			uint64_t frozen_to_swap_pages                  = 0;
			uint64_t neural_nofootprint_total_pages        = 0;

			memorystatus_get_task_phys_footprint_page_counts(proc_task(p), &internal_pages, &internal_compressed_pages,
			    &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages,
			    &alternate_accounting_pages, &alternate_accounting_compressed_pages,
			    &iokit_mapped_pages, &page_table_pages, &frozen_to_swap_pages, &neural_nofootprint_total_pages);

			entry->jse_internal_pages = internal_pages;
			entry->jse_internal_compressed_pages = internal_compressed_pages;
			entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages;
			entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages;
			entry->jse_alternate_accounting_pages = alternate_accounting_pages;
			entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages;
			entry->jse_iokit_mapped_pages = iokit_mapped_pages;
			entry->jse_page_table_pages = page_table_pages;
			entry->jse_frozen_to_swap_pages = frozen_to_swap_pages;
			entry->jse_neural_nofootprint_total_pages = neural_nofootprint_total_pages;

			uint64_t region_count = 0;
			memorystatus_get_task_memory_region_count(proc_task(p), &region_count);
			entry->jse_memory_region_count = region_count;
			entry->csflags = proc_getcsflags(p);
			goto exit;
		}
	}

	if (entry == NULL) {
		/*
		 * The entry was not found in the snapshot, so the process must have
		 * launched after the snapshot was initialized.
		 * Let's try to append the new entry.
		 */
		if (memorystatus_jetsam_snapshot_count < memorystatus_jetsam_snapshot_max) {
			/*
			 * A populated snapshot buffer exists
			 * and there is room to init a new entry.
			 */
			assert(memorystatus_jetsam_snapshot_count == snapshot->entry_count);

			if (memorystatus_init_jetsam_snapshot_entry_with_kill_locked(snapshot, p, kill_cause, killtime, &entry)) {
				memorystatus_jetsam_snapshot_count++;

				if (memorystatus_jetsam_snapshot_count >= memorystatus_jetsam_snapshot_max) {
					/*
					 * We just used the last slot in the snapshot buffer.
					 * We only want to log it once... so we do it here
					 * when we notice we've hit the max.
					 */
					memorystatus_log_error("memorystatus: WARNING snapshot buffer is full, count %d\n", memorystatus_jetsam_snapshot_count);
				}
			}
		}
	}

exit:
	if (entry) {
#if CONFIG_FREEZE
		if (memorystatus_jetsam_use_freezer_snapshot && isApp(p)) {
			/* This is an app kill. Record it in the freezer snapshot so dasd can incorporate this in its recommendations. */
			copied_to_freezer_snapshot = memorystatus_jetsam_snapshot_copy_entry_locked(memorystatus_jetsam_snapshot_freezer, memorystatus_jetsam_snapshot_freezer_max, entry);
			if (copied_to_freezer_snapshot && memorystatus_jetsam_snapshot_freezer->entry_count == memorystatus_jetsam_snapshot_freezer_max) {
				/*
				 * We just used the last slot in the freezer snapshot buffer.
				 * We only want to log it once... so we do it here
				 * when we notice we've hit the max.
				 */
				memorystatus_log_error("memorystatus: WARNING freezer snapshot buffer is full, count %zu\n",
				    memorystatus_jetsam_snapshot_freezer->entry_count);
			}
		}
#endif /* CONFIG_FREEZE */
	} else {
		/*
		 * If we reach here, the snapshot buffer could not be updated.
		 * Most likely, the buffer is full, in which case we would have
		 * logged a warning in the previous call.
		 *
		 * For now, we will stop appending snapshot entries.
		 * When the buffer is consumed, the snapshot state will reset.
		 */

		memorystatus_log_error(
			"memorystatus_update_jetsam_snapshot_entry_locked: failed to update pid %d, priority %d, count %d\n",
			proc_getpid(p), p->p_memstat_effectivepriority, memorystatus_jetsam_snapshot_count);

#if CONFIG_FREEZE
		/* We still attempt to record this in the freezer snapshot */
		if (memorystatus_jetsam_use_freezer_snapshot && isApp(p)) {
			snapshot = memorystatus_jetsam_snapshot_freezer;
			if (snapshot->entry_count < memorystatus_jetsam_snapshot_freezer_max) {
				copied_to_freezer_snapshot = memorystatus_init_jetsam_snapshot_entry_with_kill_locked(snapshot, p, kill_cause, killtime, &entry);
				if (copied_to_freezer_snapshot && memorystatus_jetsam_snapshot_freezer->entry_count == memorystatus_jetsam_snapshot_freezer_max) {
					/*
					 * We just used the last slot in the freezer snapshot buffer.
					 * We only want to log it once... so we do it here
					 * when we notice we've hit the max.
					 */
					memorystatus_log_error("memorystatus: WARNING freezer snapshot buffer is full, count %zu\n",
					    memorystatus_jetsam_snapshot_freezer->entry_count);
				}
			}
		}
#endif /* CONFIG_FREEZE */
	}

	return;
}

uint32_t
memorystatus_get_available_page_count(void)
{
	return os_atomic_load(&memorystatus_available_pages, relaxed);
}

void
memorystatus_update_available_page_count(uint32_t available_page_count)
{
	os_atomic_store(&memorystatus_available_pages, available_page_count,
	    relaxed);
#if VM_PRESSURE_EVENTS
	/*
	 * Since memorystatus_available_pages changes, we should
	 * re-evaluate the pressure levels on the system and
	 * check if we need to wake the pressure thread.
	 * We also update memorystatus_level in that routine.
	 */
	vm_pressure_response();
#endif /* VM_PRESSURE_EVENTS */
#if CONFIG_FREEZE
	/*
	 * We can't grab the freezer_mutex here even though that synchronization would be correct to inspect
	 * the # of frozen processes and wakeup the freezer thread. Reason being that we come here into this
	 * code with (possibly) the page-queue locks held and preemption disabled. So trying to grab a mutex here
	 * will result in the "mutex with preemption disabled" panic.
	 */

	if (memorystatus_freeze_thread_should_run()) {
		/*
		 * The freezer thread is usually woken up by some user-space call i.e. pid_hibernate(any process).
		 * That trigger isn't invoked often enough and so we are enabling this explicit wakeup here.
		 */
		if (VM_CONFIG_FREEZER_SWAP_IS_ACTIVE) {
			thread_wakeup((event_t)&memorystatus_freeze_wakeup);
		}
	}
#endif /* CONFIG_FREEZE */
	_memstat_consider_waking_jetsam_thread();
}

static boolean_t
memorystatus_init_jetsam_snapshot_entry_locked(proc_t p, memorystatus_jetsam_snapshot_entry_t *entry, uint64_t gencount)
{
	clock_sec_t                     tv_sec;
	clock_usec_t                    tv_usec;
	uint32_t pages = 0;
	uint32_t max_pages_lifetime = 0;
	uint32_t purgeable_pages = 0;
	uint64_t internal_pages                         = 0;
	uint64_t internal_compressed_pages              = 0;
	uint64_t purgeable_nonvolatile_pages            = 0;
	uint64_t purgeable_nonvolatile_compressed_pages = 0;
	uint64_t alternate_accounting_pages             = 0;
	uint64_t alternate_accounting_compressed_pages  = 0;
	uint64_t iokit_mapped_pages                     = 0;
	uint64_t page_table_pages                       = 0;
	uint64_t frozen_to_swap_pages                   = 0;
	uint64_t neural_nofootprint_total_pages         = 0;
	uint64_t region_count                           = 0;
	uint64_t cids[COALITION_NUM_TYPES];
	uint32_t trust                                  = 0;
	kern_return_t ret                               = 0;
	memset(entry, 0, sizeof(memorystatus_jetsam_snapshot_entry_t));

	entry->pid = proc_getpid(p);
	strlcpy(&entry->name[0], p->p_name, sizeof(entry->name));
	entry->priority = p->p_memstat_effectivepriority;

	memorystatus_get_task_page_counts(proc_task(p), &pages, &max_pages_lifetime, &purgeable_pages);
	entry->pages              = (uint64_t)pages;
	entry->max_pages_lifetime = (uint64_t)max_pages_lifetime;
	entry->purgeable_pages    = (uint64_t)purgeable_pages;

	memorystatus_get_task_phys_footprint_page_counts(proc_task(p), &internal_pages, &internal_compressed_pages,
	    &purgeable_nonvolatile_pages, &purgeable_nonvolatile_compressed_pages,
	    &alternate_accounting_pages, &alternate_accounting_compressed_pages,
	    &iokit_mapped_pages, &page_table_pages, &frozen_to_swap_pages, &neural_nofootprint_total_pages);

	entry->jse_internal_pages = internal_pages;
	entry->jse_internal_compressed_pages = internal_compressed_pages;
	entry->jse_purgeable_nonvolatile_pages = purgeable_nonvolatile_pages;
	entry->jse_purgeable_nonvolatile_compressed_pages = purgeable_nonvolatile_compressed_pages;
	entry->jse_alternate_accounting_pages = alternate_accounting_pages;
	entry->jse_alternate_accounting_compressed_pages = alternate_accounting_compressed_pages;
	entry->jse_iokit_mapped_pages = iokit_mapped_pages;
	entry->jse_page_table_pages = page_table_pages;
	entry->jse_frozen_to_swap_pages = frozen_to_swap_pages;
	entry->jse_neural_nofootprint_total_pages = neural_nofootprint_total_pages;

	memorystatus_get_task_memory_region_count(proc_task(p), &region_count);
	entry->jse_memory_region_count = region_count;

	entry->state     = _memstat_build_state(p);
	entry->user_data = p->p_memstat_userdata;
	proc_getexecutableuuid(p, &entry->uuid[0], sizeof(entry->uuid));
	entry->fds       = p->p_fd.fd_nfiles;

	absolutetime_to_microtime(get_task_cpu_time(proc_task(p)), &tv_sec, &tv_usec);
	entry->cpu_time.tv_sec = (int64_t)tv_sec;
	entry->cpu_time.tv_usec = (int64_t)tv_usec;

	assert(p->p_stats != NULL);
	entry->jse_starttime =  p->p_stats->ps_start;   /* abstime process started */
	entry->jse_killtime = 0;                        /* abstime jetsam chose to kill process */
	entry->killed       = 0;                        /* the jetsam kill cause */
	entry->jse_gencount = gencount;                 /* indicates a pass through jetsam thread, when process was targeted to be killed */

	entry->jse_idle_delta = p->p_memstat_idle_delta; /* Most recent timespan spent in idle-band */
	entry->jse_prio_start = p->p_memstat_prio_start; /* Time moved to current band */

#if CONFIG_FREEZE
	entry->jse_freeze_skip_reason = p->p_memstat_freeze_skip_reason;
	entry->jse_thaw_count = p->p_memstat_thaw_count;
#else /* CONFIG_FREEZE */
	entry->jse_thaw_count = 0;
	entry->jse_freeze_skip_reason = kMemorystatusFreezeSkipReasonNone;
#endif /* CONFIG_FREEZE */

	proc_coalitionids(p, cids);
	entry->jse_coalition_jetsam_id = cids[COALITION_TYPE_JETSAM];
	entry->csflags = proc_getcsflags(p);
	ret = get_trust_level_kdp(get_task_pmap(proc_task(p)), &trust);
	if (ret != KERN_SUCCESS) {
		trust = KCDATA_INVALID_CS_TRUST_LEVEL;
	}
	entry->cs_trust_level = trust;
	return TRUE;
}

static void
memorystatus_init_snapshot_vmstats(memorystatus_jetsam_snapshot_t *snapshot)
{
	kern_return_t kr = KERN_SUCCESS;
	mach_msg_type_number_t  count = HOST_VM_INFO64_COUNT;
	vm_statistics64_data_t  vm_stat;

	if ((kr = host_statistics64(host_self(), HOST_VM_INFO64, (host_info64_t)&vm_stat, &count)) != KERN_SUCCESS) {
		memorystatus_log_error("memorystatus_init_jetsam_snapshot_stats: host_statistics64 failed with %d\n", kr);
		memset(&snapshot->stats, 0, sizeof(snapshot->stats));
	} else {
		snapshot->stats.free_pages      = vm_stat.free_count;
		snapshot->stats.active_pages    = vm_stat.active_count;
		snapshot->stats.inactive_pages  = vm_stat.inactive_count;
		snapshot->stats.throttled_pages = vm_stat.throttled_count;
		snapshot->stats.purgeable_pages = vm_stat.purgeable_count;
		snapshot->stats.wired_pages     = vm_stat.wire_count;

		snapshot->stats.speculative_pages = vm_stat.speculative_count;
		snapshot->stats.filebacked_pages  = vm_stat.external_page_count;
		snapshot->stats.anonymous_pages   = vm_stat.internal_page_count;
		snapshot->stats.compressions      = vm_stat.compressions;
		snapshot->stats.decompressions    = vm_stat.decompressions;
		snapshot->stats.compressor_pages  = vm_stat.compressor_page_count;
		snapshot->stats.total_uncompressed_pages_in_compressor = vm_stat.total_uncompressed_pages_in_compressor;
	}

	get_zone_map_size(&snapshot->stats.zone_map_size, &snapshot->stats.zone_map_capacity);

	bzero(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name));
	get_largest_zone_info(snapshot->stats.largest_zone_name, sizeof(snapshot->stats.largest_zone_name),
	    &snapshot->stats.largest_zone_size);
}

/*
 * Collect vm statistics at boot.
 * Called only once (see kern_exec.c)
 * Data can be consumed at any time.
 */
void
memorystatus_init_at_boot_snapshot()
{
	memorystatus_init_snapshot_vmstats(&memorystatus_at_boot_snapshot);
	memorystatus_at_boot_snapshot.entry_count = 0;
	memorystatus_at_boot_snapshot.notification_time = 0;   /* updated when consumed */
	memorystatus_at_boot_snapshot.snapshot_time = mach_absolute_time();
}

static void
memorystatus_init_jetsam_snapshot_header(memorystatus_jetsam_snapshot_t *snapshot)
{
	memorystatus_init_snapshot_vmstats(snapshot);
	snapshot->snapshot_time = mach_absolute_time();
	snapshot->notification_time = 0;
	snapshot->js_gencount = 0;
}

static void
memorystatus_init_jetsam_snapshot_locked(memorystatus_jetsam_snapshot_t *od_snapshot, uint32_t ods_list_count )
{
	proc_t p, next_p;
	unsigned int b = 0, i = 0;

	memorystatus_jetsam_snapshot_t *snapshot = NULL;
	memorystatus_jetsam_snapshot_entry_t *snapshot_list = NULL;
	unsigned int snapshot_max = 0;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	if (od_snapshot) {
		/*
		 * This is an on_demand snapshot
		 */
		snapshot      = od_snapshot;
		snapshot_list = od_snapshot->entries;
		snapshot_max  = ods_list_count;
	} else {
		/*
		 * This is a jetsam event snapshot
		 */
		snapshot      = memorystatus_jetsam_snapshot;
		snapshot_list = memorystatus_jetsam_snapshot->entries;
		snapshot_max  = memorystatus_jetsam_snapshot_max;
	}

	memorystatus_init_jetsam_snapshot_header(snapshot);

	next_p = memorystatus_get_first_proc_locked(&b, TRUE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&b, p, TRUE);

		if (FALSE == memorystatus_init_jetsam_snapshot_entry_locked(p, &snapshot_list[i], snapshot->js_gencount)) {
			continue;
		}

		if (++i == snapshot_max) {
			break;
		}
	}

	/* Log launchd and kernel_task as well to see more context, even though jetsam doesn't apply to them. */
	if (i < snapshot_max) {
		memorystatus_init_jetsam_snapshot_entry_locked(initproc, &snapshot_list[i], snapshot->js_gencount);
		i++;
	}

	if (i < snapshot_max) {
		memorystatus_init_jetsam_snapshot_entry_locked(kernproc, &snapshot_list[i], snapshot->js_gencount);
		i++;
	}

	snapshot->entry_count = i;

	if (!od_snapshot) {
		/* update the system buffer count */
		memorystatus_jetsam_snapshot_count = i;
	}
}

/* number of entries added to the end of the jetsam snapshot (for launchd and kernel) */
static const int memorystatus_artificial_snapshot_entry_count = 2;

#if DEVELOPMENT || DEBUG

/*
 * Fills an array with the given pids in the order they are seen in a
 * jetsam band.
 */
static int
memorystatus_get_sort_order(
	unsigned int bucket_index,
	pid_t *pids,
	pid_t *order,
	size_t num_pids)
{
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);

	proc_t p = NULL;
	size_t i, out_idx = 0;

	/*
	 * Read out the order of all the pids into the order array.
	 */
	p = memorystatus_get_first_proc_locked(&bucket_index, FALSE);
	while (p) {
		for (i = 0; i < num_pids; i++) {
			if (pids[i] == proc_getpid(p)) {
				if (out_idx >= num_pids) {
					/* Did we somehow see something twice? */
					return EINVAL;
				}
				order[out_idx] = pids[i];
				out_idx++;
			}
		}
		p = memorystatus_get_next_proc_locked(&bucket_index, p, FALSE);
	}
	return 0;
}

/*
 * Triggers a sort_order on a specified jetsam priority band.
 * This is for testing only, used to force a path through the sort
 * function.
 */
static int
memorystatus_cmd_test_jetsam_sort(int priority,
    int sort_order,
    user_addr_t expected_order_user,
    size_t expected_order_user_len)
{
	pid_t *expected_order, *actual_order;
	int error = 0;
	size_t num_pids = expected_order_user_len / sizeof(pid_t);

	if (num_pids > 512) { /* Just so we don't allocate some huge buffer */
		return EINVAL;
	}

	if ((priority < 0) || (priority >= MEMSTAT_BUCKET_COUNT)) {
		return EINVAL;
	}

	expected_order = kalloc_data_tag(num_pids * sizeof(pid_t), Z_WAITOK, VM_KERN_MEMORY_DIAG);
	actual_order = kalloc_data_tag(num_pids * sizeof(pid_t), Z_WAITOK, VM_KERN_MEMORY_DIAG);

	error = copyin(expected_order_user, expected_order, expected_order_user_len);
	if (error != 0) {
		goto err;
	}

	/*
	 * Acquire lock before sorting so we can check the sort order
	 * while still holding the lock.
	 */
	proc_list_lock();

	memstat_sort_bucket_locked(priority, sort_order);

	if (expected_order_user != CAST_USER_ADDR_T(NULL) && expected_order_user_len > 0) {
		bzero(actual_order, num_pids * sizeof(pid_t));
		error = memorystatus_get_sort_order(priority, expected_order, actual_order, num_pids);
		/* Even if we get an error, we still want to copyout what we had */
		copyout(actual_order, expected_order_user, num_pids * sizeof(pid_t));
	}

	proc_list_unlock();

err:
	kfree_data(expected_order, num_pids * sizeof(pid_t));
	kfree_data(actual_order, num_pids * sizeof(pid_t));
	return error;
}

#endif /* DEVELOPMENT || DEBUG */

/*
 * Prepare the process to be killed (set state, update snapshot) and kill it.
 */
static uint64_t memorystatus_purge_before_jetsam_success = 0;

#if SOCKETS
static int
networking_memstatus_callout(proc_t p, uint32_t status)
{
	struct fileproc *fp;

	/*
	 * proc list lock NOT held
	 * proc lock NOT held
	 * a reference on the proc has been held / shall be dropped by the caller.
	 */
	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_NOTOWNED);
	LCK_MTX_ASSERT(&p->p_mlock, LCK_MTX_ASSERT_NOTOWNED);

	proc_fdlock(p);

	fdt_foreach(fp, p) {
		switch (FILEGLOB_DTYPE(fp->fp_glob)) {
#if NECP
		case DTYPE_NETPOLICY:
			necp_fd_memstatus(p, status,
			    (struct necp_fd_data *)fp_get_data(fp));
			break;
#endif /* NECP */
#if SKYWALK
		case DTYPE_CHANNEL:
			kern_channel_memstatus(p, status,
			    (struct kern_channel *)fp_get_data(fp));
			break;
#endif /* SKYWALK */
		default:
			break;
		}
	}
	proc_fdunlock(p);

	return 1;
}
#endif /* SOCKETS */

static bool
memorystatus_kill_proc(proc_t p, uint32_t cause, os_reason_t jetsam_reason, bool *killed, uint64_t *footprint_out)
{
	pid_t aPid = 0;
	uint32_t aPid_ep = 0;

	uint64_t        killtime = 0;
	uint64_t        time_in_priority_band_secs = 0;
	clock_sec_t     tv_sec;
	clock_usec_t    tv_usec;
	uint32_t        tv_msec;
	bool retval = false;

	aPid = proc_getpid(p);
	aPid_ep = p->p_memstat_effectivepriority;

	if (cause != kMemorystatusKilledVnodes && cause != kMemorystatusKilledZoneMapExhaustion) {
		/*
		 * Genuine memory pressure and not other (vnode/zone) resource exhaustion.
		 */
		boolean_t success = FALSE;
		uint64_t num_pages_purged;
		uint64_t num_pages_reclaimed = 0;
		uint64_t num_pages_unsecluded = 0;

		networking_memstatus_callout(p, cause);
		num_pages_purged = vm_purgeable_purge_task_owned(proc_task(p));
		num_pages_reclaimed += num_pages_purged;
#if CONFIG_SECLUDED_MEMORY
		if (cause == kMemorystatusKilledVMPageShortage &&
		    vm_page_secluded_count > 0 &&
		    task_can_use_secluded_mem(proc_task(p), FALSE)) {
			/*
			 * We're about to kill a process that has access
			 * to the secluded pool.  Drain that pool into the
			 * free or active queues to make these pages re-appear
			 * as "available", which might make us no longer need
			 * to kill that process.
			 * Since the secluded pool does not get refilled while
			 * a process has access to it, it should remain
			 * drained.
			 */
			num_pages_unsecluded = vm_page_secluded_drain();
			num_pages_reclaimed += num_pages_unsecluded;
		}
#endif /* CONFIG_SECLUDED_MEMORY */

		if (num_pages_reclaimed) {
			/*
			 * We actually reclaimed something and so let's
			 * check if we need to continue with the kill.
			 */
			if (cause == kMemorystatusKilledHiwat) {
				uint64_t footprint_in_bytes = get_task_phys_footprint(proc_task(p));
				uint64_t memlimit_in_bytes  = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL);  /* convert MB to bytes */
				success = (footprint_in_bytes <= memlimit_in_bytes);
			} else {
				success = !(memorystatus_get_available_page_count() < memorystatus_get_soft_memlimit_page_shortage_threshold());
#if CONFIG_SECLUDED_MEMORY
				if (!success && num_pages_unsecluded) {
					/*
					 * We just drained the secluded pool
					 * because we're about to kill a
					 * process that has access to it.
					 * This is an important process and
					 * we'd rather not kill it unless
					 * absolutely necessary, so declare
					 * success even if draining the pool
					 * did not quite get us out of the
					 * "pressure" level but still got
					 * us out of the "critical" level.
					 */
					success = !(
						memorystatus_get_available_page_count() <
						memorystatus_get_critical_page_shortage_threshold());
				}
#endif /* CONFIG_SECLUDED_MEMORY */
			}

			if (success) {
				memorystatus_purge_before_jetsam_success++;

				memorystatus_log_info("memorystatus: reclaimed %llu pages (%llu purged, %llu unsecluded) from pid %d [%s] and avoided %s\n",
				    num_pages_reclaimed, num_pages_purged, num_pages_unsecluded, aPid, ((p && *p->p_name) ? p->p_name : "unknown"), memstat_kill_cause_name[cause]);

				*killed = false;
				*footprint_out = num_pages_reclaimed + num_pages_purged + num_pages_unsecluded;

				return true;
			}
		}
	}

	killtime = mach_absolute_time();
	absolutetime_to_microtime(killtime, &tv_sec, &tv_usec);
	tv_msec = tv_usec / 1000;

	proc_list_lock();
	memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime);
	proc_list_unlock();

	char kill_reason_string[128];

	if (cause == kMemorystatusKilledHiwat) {
		strlcpy(kill_reason_string, "killing_highwater_process", 128);
	} else {
		if (aPid_ep == JETSAM_PRIORITY_IDLE) {
			strlcpy(kill_reason_string, "killing_idle_process", 128);
		} else {
			strlcpy(kill_reason_string, "killing_top_process", 128);
		}
	}

	/*
	 * memorystatus_do_kill drops a reference, so take another one so we can
	 * continue to use this exit reason even after memorystatus_do_kill()
	 * returns
	 */
	os_reason_ref(jetsam_reason);

	retval = memorystatus_do_kill(p, cause, jetsam_reason, footprint_out);
	*killed = retval;

	absolutetime_to_nanoseconds(killtime - p->p_memstat_prio_start, &time_in_priority_band_secs);
	time_in_priority_band_secs /= NSEC_PER_SEC;

	memorystatus_log("memorystatus: %s pid %d [%s] (%s %d %llus rf:%s type:%s) %lluKB - memorystatus_available_pages: %llu compressor_size:%u\n",
	    kill_reason_string,
	    aPid, proc_best_name(p),
	    memstat_kill_cause_name[cause], aPid_ep, time_in_priority_band_secs,
	    _memstat_relaunch_flags_description(p->p_memstat_relaunch_flags), _memstat_proc_type_description(p),
	    (*footprint_out) >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES, vm_compressor_pool_size());

	return retval;
}

/*
 * Jetsam the first process in the queue.
 */
static bool
memstat_kill_top_process(uint32_t cause, os_reason_t jetsam_reason,
    int32_t max_priority, memstat_kill_options_t options,
    int32_t *priority_out, uint32_t *errors_out, uint64_t *memory_reclaimed_out)
{
	pid_t aPid;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	bool new_snapshot = false, force_new_snapshot = false, killed = false, freed_mem = false;
	unsigned int i = 0;
	uint32_t aPid_ep;
	uint64_t footprint_of_killed_proc = 0;

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES);

	bool only_long_idle = options & MEMSTAT_ONLY_LONG_IDLE;
	bool only_swappable = options & MEMSTAT_ONLY_SWAPPABBLE;
	bool sort_bucket = options & MEMSTAT_SORT_BUCKET;

#if CONFIG_JETSAM
	if (sort_bucket) {
		(void)memstat_sort_bucket(JETSAM_PRIORITY_FOREGROUND, memstat_jetsam_fg_sort_order);
	}

	if (memory_reclaimed_out) {
		*memory_reclaimed_out = 0;
	}

	force_new_snapshot = false;

#else /* CONFIG_JETSAM */
	if (sort_bucket) {
		(void)memstat_sort_bucket(JETSAM_PRIORITY_IDLE, JETSAM_SORT_IDLE_DEFAULT);
	}

	/*
	 * And, because we are here under extreme circumstances, we force a snapshot even for
	 * IDLE kills.
	 */
	force_new_snapshot = true;

#endif /* CONFIG_JETSAM */

	if (cause != kMemorystatusKilledZoneMapExhaustion &&
	    jetsam_current_thread() != NULL &&
	    jetsam_current_thread()->limit_to_low_bands &&
	    max_priority > JETSAM_PRIORITY_MAIL) {
		max_priority = JETSAM_PRIORITY_MAIL;
	}

	_memstat_refresh_oldest_reapable_proc_info();

	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&i, TRUE);
	while (next_p && (next_p->p_memstat_effectivepriority <= max_priority)) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);


		aPid = proc_getpid(p);
		aPid_ep = p->p_memstat_effectivepriority;

		if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP)) {
			continue;   /* with lock held */
		}

		if (cause == kMemorystatusKilledVnodes) {
			/*
			 * If the system runs out of vnodes, we systematically jetsam
			 * processes in hopes of stumbling onto a vnode gain that helps
			 * the system recover.  The process that happens to trigger
			 * this path has no known relationship to the vnode shortage.
			 * Deadlock avoidance: attempt to safeguard the caller.
			 */

			if (p == current_proc()) {
				/* do not jetsam the current process */
				continue;
			}
		}

		if (only_swappable && !task_donates_own_pages(proc_task(p))) {
			continue;
		}

		if (only_long_idle) {
			if (!_memstat_proc_is_reapable(p)) {
				memorystatus_log_debug("memorystatus: memstat_kill_top_process: skipping non-reapable process %s [%d]\n",
				    proc_best_name(p), p->p_pid);
				continue;
			}
			memorystatus_log_debug("memorystatus: memstat_kill_top_process: found reapable long-idle process %s [%d]\n",
			    proc_best_name(p), p->p_pid);
		}

#if !CONFIG_JETSAM
		if (max_priority == JETSAM_PRIORITY_IDLE &&
		    ((p->p_memstat_dirty & (P_DIRTY_ALLOW_IDLE_EXIT | P_DIRTY_IS_DIRTY | P_DIRTY_TERMINATED)) != (P_DIRTY_ALLOW_IDLE_EXIT))) {
			/*
			 * This process is in the idle band but is not clean+idle-exitable or
			 * managed+assertion-less. Skip it.
			 */
			memorystatus_log_error("memorystatus: skipping idle but not idle-exitable process "
			    "%s [%d] (0x%x)\n", proc_best_name(p), proc_getpid(p), p->p_memstat_state);
			continue;
		}
#endif /* !CONFIG_JETSAM */
#if CONFIG_FREEZE
		if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
			continue;
		}
#endif
		if (proc_ref(p, true) == p) {
			/*
			 * Mark as terminated so that if exit1() indicates success, but the process (for example)
			 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
			 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
			 * acquisition of the proc lock.
			 */
			p->p_memstat_state |= P_MEMSTAT_TERMINATED;
		} else {
			/*
			 * We need to restart the search again because
			 * proc_ref _can_ drop the proc_list lock
			 * and we could have lost our stored next_p via
			 * an exit() on another core.
			 */
			i = 0;
			next_p = memorystatus_get_first_proc_locked(&i, TRUE);
			continue;
		}

		/*
		 * Capture a snapshot if none exists and:
		 * - we are forcing a new snapshot creation, either because:
		 *      - on a particular platform we need these snapshots every time, OR
		 *	- a boot-arg/embedded device tree property has been set.
		 * - priority was not requested (this is something other than an ambient kill)
		 * - the priority was requested *and* the targeted process is not at idle priority
		 */
		if ((memorystatus_jetsam_snapshot_count == 0) &&
		    (force_new_snapshot || memorystatus_idle_snapshot || ((!priority_out) || (priority_out && (aPid_ep != JETSAM_PRIORITY_IDLE))))) {
			memorystatus_init_jetsam_snapshot_locked(NULL, 0);
			new_snapshot = true;
		}

		proc_list_unlock();

		freed_mem = memorystatus_kill_proc(p, cause, jetsam_reason, &killed, &footprint_of_killed_proc); /* purged and/or killed 'p' */
		/* Success? */
		if (freed_mem) {
			if (memory_reclaimed_out) {
				*memory_reclaimed_out = footprint_of_killed_proc;
			}
			if (killed) {
				if (priority_out) {
					*priority_out = aPid_ep;
				}
			} else {
				/* purged */
				proc_list_lock();
				p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
				proc_list_unlock();
			}
			proc_rele(p);
			goto exit;
		}

		/*
		 * Failure - first unwind the state,
		 * then fall through to restart the search.
		 */
		proc_list_lock();
		proc_rele(p);
		p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
		p->p_memstat_state |= P_MEMSTAT_ERROR;
		if (errors_out) {
			*errors_out += 1;
		}

		i = 0;
		next_p = memorystatus_get_first_proc_locked(&i, TRUE);
	}

	proc_list_unlock();

exit:
	os_reason_free(jetsam_reason);

	if (!killed) {
		/* Clear snapshot if freshly captured and no target was found */
		if (new_snapshot) {
			proc_list_lock();
			memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
			proc_list_unlock();
		}
#if CONFIG_JETSAM
		if (only_long_idle) {
			_memstat_reaper_end_sweep();
		}
#endif
	}

#if CONFIG_JETSAM
	if (killed && only_long_idle) {
		_memstat_reaper_record_kill(footprint_of_killed_proc);
	}
#endif

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, killed, footprint_of_killed_proc);

	return killed;
}

static
void
_memstat_refresh_oldest_reapable_proc_info()
{
	uint64_t oldest_prio_start = MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_NONE;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	unsigned int i = 0;
	uint64_t mat = mach_absolute_time();
	uint64_t rescan_timeout_duration_matu;


	/* If we're still within 'memstat_reaper_rescan_secs' of the last process-list walk,
	 * don't do another walk yet, and just use the existing information.
	 */
	if (mat < memstat_oldest_reapable_proc_info_expiration_ts_matu) {
		memorystatus_log_debug("memorystatus: _memstat_refresh_oldest_reapable_proc_info: re-using existing data\n");
		return;
	} else {
		memorystatus_log_debug("memorystatus: _memstat_refresh_oldest_reapable_proc_info: rescanning proc list\n");
	}

	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&i, TRUE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);

		/* Since the process list is sorted in priority order, once we find the first
		 * process that is out of the reaper's acceptible range, we can skip the rest
		 */
		if (p->p_memstat_effectivepriority > memstat_reaper_max_priority) {
			break;
		}

		if (_memstat_proc_is_reapable(p)) {
			uint64_t proc_prio_start = p->p_memstat_prio_start;
			if (proc_prio_start < oldest_prio_start) {
				oldest_prio_start = proc_prio_start;
				/* Since the process list is sorted in age order within priority bands,
				 * the first process will be the oldest one, and we can bail out and skip the rest
				 */
				break;
			}
		}
	}

	proc_list_unlock();

	memstat_oldest_reapable_proc_prio_start = oldest_prio_start;

	if (memstat_oldest_reapable_proc_prio_start != MEMSTAT_OLDEST_REAPABLE_PROC_PRIO_START_NONE) {
		uint64_t min_age_matu;
		nanoseconds_to_absolutetime((memstat_reaper_min_age_secs * NSEC_PER_SEC), &min_age_matu);
		memstat_oldest_reapable_proc_will_be_reapable_at_ts_matu = memstat_oldest_reapable_proc_prio_start + min_age_matu;
	}

	nanoseconds_to_absolutetime((memstat_reaper_rescan_secs * NSEC_PER_SEC), &rescan_timeout_duration_matu);
	memstat_oldest_reapable_proc_info_expiration_ts_matu = mat + rescan_timeout_duration_matu;
}

static bool
_memstat_proc_is_reapable(proc_t proc)
{
	uint32_t priority_band;
	uint64_t time_in_priority_band_secs;
	uint32_t relaunch_probability_acceptable_mask;

	/*
	 *  To be potentially reapable, the process
	 *  - must be in or below the max reapable priority and
	 *  - must not have a relaunch probability of High or Medium (per memstat_reaper_reap_relaunch_mask)
	 *  - must have been in that priority band longer than the reaper minimum age threshold
	 *  - must have been in that priority band longer than the reaper minimum age threshold for applications, if process is an application
	 */
	priority_band = proc->p_memstat_effectivepriority;
	if (priority_band > memstat_reaper_max_priority) {
		memorystatus_log_debug("_memstat_proc_is_reapable: %s [%d] not reapable because priority (%d) is above threshold (%d)\n",
		    proc_best_name(proc), proc->p_pid, priority_band, memstat_reaper_max_priority);
		return false;
	}

	uint32_t relaunch_flags = proc->p_memstat_relaunch_flags;
	// There's no explicit flag for "unknown" relaunch probability, and we need one for our control bitmask.
	// So if none of the Low Medium or High bits are set, we set the next higher bit as the "unknown relaunch probability" bit
	// and then test all the bits at once, below, with a bitwise-and.
	if ((relaunch_flags & (P_MEMSTAT_RELAUNCH_LOW | P_MEMSTAT_RELAUNCH_MED | P_MEMSTAT_RELAUNCH_HIGH)) == 0) {
		relaunch_flags |= MEMORYSTATUS_REAPER_REAP_RELAUNCH_MASK_UNKNOWN; // The bit for 'unknown' is the one just to the left (above) of High, e.g. 0x08
	}
	relaunch_probability_acceptable_mask = relaunch_flags & memstat_reaper_reap_relaunch_mask;

	if (relaunch_probability_acceptable_mask == 0) {
		memorystatus_log_debug("_memstat_proc_is_reapable: %s [%d] not reapable because relaunch probability bitmask (0x%02X) does not match with the memstat_reaper_reap_relaunch_mask (0x%02X).\n",
		    proc_best_name(proc), proc->p_pid, relaunch_flags, memstat_reaper_reap_relaunch_mask);
		return false;
	}

	absolutetime_to_nanoseconds(mach_absolute_time() - proc->p_memstat_prio_start, &time_in_priority_band_secs);
	time_in_priority_band_secs /= NSEC_PER_SEC;

	if (_memstat_proc_is_application(proc)) {
		if ((time_in_priority_band_secs < memstat_reaper_min_age_apps_secs)) {
			memorystatus_log_debug("_memstat_proc_is_reapable: %s [%d] not reapable because it is an application and age (%llu) is below min age for apps (%d)\n",
			    proc_best_name(proc), proc->p_pid, time_in_priority_band_secs, memstat_reaper_min_age_apps_secs);
			return false;
		}
	} else {
		if (time_in_priority_band_secs < memstat_reaper_min_age_secs) {
			memorystatus_log_debug("_memstat_proc_is_reapable: %s [%d] not reapable because age (%llu) is below min age (%d)\n",
			    proc_best_name(proc), proc->p_pid, time_in_priority_band_secs, memstat_reaper_min_age_secs);
			return false;
		}
	}

	memorystatus_log_debug("_memstat_proc_is_reapable: %s [%d] is reapable; priority=%d, age=%d, relaunch_probability_acceptable_mask=0x%02X, type=%s\n",
	    proc_best_name(proc), proc->p_pid, priority_band, (uint32_t)(time_in_priority_band_secs), relaunch_probability_acceptable_mask,
	    _memstat_proc_type_description(proc));
	return true;
}

static bool
_memstat_proc_is_application(proc_t proc)
{
	bool isApp = false;

	task_t task = proc_task(proc);
	if (task != NULL) {
		isApp = task_is_app( task);
	}

	return isApp;
}

/*
 * Jetsam aggressively
 */
static bool
memorystatus_kill_processes_aggressive(uint32_t cause, int aggr_count,
    int32_t priority_max, int max_kills, uint32_t *errors, uint64_t *memory_reclaimed)
{
	pid_t aPid;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	boolean_t new_snapshot = FALSE, killed = FALSE;
	int kill_count = 0;
	unsigned int priority_band = JETSAM_PRIORITY_IDLE;
	int32_t aPid_ep = 0;
	unsigned int memorystatus_level_snapshot = 0;
	uint64_t killtime = 0;
	uint64_t time_in_priority_band_secs = 0;
	clock_sec_t     tv_sec;
	clock_usec_t    tv_usec;
	uint32_t        tv_msec;
	os_reason_t jetsam_reason = OS_REASON_NULL;
	uint64_t footprint_of_killed_proc = 0;

	*memory_reclaimed = 0;

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, priority_max);

	if (priority_max >= JETSAM_PRIORITY_FOREGROUND) {
		/*
		 * Check if aggressive jetsam has been asked to kill upto or beyond the
		 * JETSAM_PRIORITY_FOREGROUND bucket. If yes, sort the FG band based on
		 * coalition footprint.
		 */
		memstat_sort_bucket(JETSAM_PRIORITY_FOREGROUND, memstat_jetsam_fg_sort_order);
	}

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, cause);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus_kill_processes_aggressive: failed to allocate exit reason\n");
	}
	memorystatus_log("memorystatus: aggressively killing up to %d processes below band %d.\n", max_kills, priority_max + 1);
	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&priority_band, TRUE);
	while (next_p) {
		if (proc_list_exited(next_p) ||
		    ((unsigned int)(next_p->p_memstat_effectivepriority) != priority_band)) {
			/*
			 * We have raced with next_p running on another core.
			 * It may be exiting or it may have moved to a different
			 * jetsam priority band.  This means we have lost our
			 * place in line while traversing the jetsam list.  We
			 * attempt to recover by rewinding to the beginning of the band
			 * we were already traversing.  By doing this, we do not guarantee
			 * that no process escapes this aggressive march, but we can make
			 * skipping an entire range of processes less likely. (PR-21069019)
			 */

			memorystatus_log_debug(
				"memorystatus: aggressive%d: rewinding band %d, %s(%d) moved or exiting.\n",
				aggr_count, priority_band, (*next_p->p_name ? next_p->p_name : "unknown"), proc_getpid(next_p));

			next_p = memorystatus_get_first_proc_locked(&priority_band, TRUE);
			continue;
		}

		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&priority_band, p, TRUE);

		if (p->p_memstat_effectivepriority > priority_max) {
			/*
			 * Bail out of this killing spree if we have
			 * reached beyond the priority_max jetsam band.
			 * That is, we kill up to and through the
			 * priority_max jetsam band.
			 */
			proc_list_unlock();
			goto exit;
		}

		aPid = proc_getpid(p);
		aPid_ep = p->p_memstat_effectivepriority;

		if (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP)) {
			continue;
		}

		/*
		 * Capture a snapshot if none exists.
		 */
		if (memorystatus_jetsam_snapshot_count == 0) {
			memorystatus_init_jetsam_snapshot_locked(NULL, 0);
			new_snapshot = TRUE;
		}

		/*
		 * Mark as terminated so that if exit1() indicates success, but the process (for example)
		 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
		 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
		 * acquisition of the proc lock.
		 */
		p->p_memstat_state |= P_MEMSTAT_TERMINATED;

		killtime = mach_absolute_time();
		absolutetime_to_microtime(killtime, &tv_sec, &tv_usec);
		tv_msec = tv_usec / 1000;

		/* Shift queue, update stats */
		memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime);

		/*
		 * In order to kill the target process, we will drop the proc_list_lock.
		 * To guaranteee that p and next_p don't disappear out from under the lock,
		 * we must take a ref on both.
		 * If we cannot get a reference, then it's likely we've raced with
		 * that process exiting on another core.
		 */
		if (proc_ref(p, true) == p) {
			if (next_p) {
				while (next_p && (proc_ref(next_p, true) != next_p)) {
					proc_t temp_p;

					/*
					 * We must have raced with next_p exiting on another core.
					 * Recover by getting the next eligible process in the band.
					 */

					memorystatus_log_debug(
						"memorystatus: aggressive%d: skipping %d [%s] (exiting?)\n",
						aggr_count, proc_getpid(next_p), (*next_p->p_name ? next_p->p_name : "(unknown)"));

					temp_p = next_p;
					next_p = memorystatus_get_next_proc_locked(&priority_band, temp_p, TRUE);
				}
			}
			proc_list_unlock();

			if (aPid_ep <= system_procs_aging_band &&
			    (p->p_memstat_relaunch_flags & P_MEMSTAT_RELAUNCH_HIGH)) {
				memorystatus_log("memorystatus: killing %s [%d] in band %d "
				    "with high relaunch probability\n",
				    proc_best_name(p), aPid, aPid_ep);
			}
			absolutetime_to_nanoseconds(killtime - p->p_memstat_prio_start, &time_in_priority_band_secs);
			time_in_priority_band_secs /= NSEC_PER_SEC;
			memorystatus_log(
				"memorystatus: %s%d pid %d [%s] (%s %d %llus rf:%s type:%s) - memorystatus_available_pages: %llu\n",
				((aPid_ep == JETSAM_PRIORITY_IDLE) ? "killing_idle_process_aggressive" : "killing_top_process_aggressive"),
				aggr_count, aPid, proc_best_name(p),
				memstat_kill_cause_name[cause], aPid_ep,
				time_in_priority_band_secs, _memstat_relaunch_flags_description(p->p_memstat_relaunch_flags), _memstat_proc_type_description(p),
				(uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES);

			memorystatus_level_snapshot = memorystatus_level;

			/*
			 * memorystatus_do_kill() drops a reference, so take another one so we can
			 * continue to use this exit reason even after memorystatus_do_kill()
			 * returns.
			 */
			os_reason_ref(jetsam_reason);
			killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc);

			/* Success? */
			if (killed) {
				*memory_reclaimed += footprint_of_killed_proc;
				proc_rele(p);
				kill_count++;
				p = NULL;
				killed = FALSE;

				/*
				 * Continue the killing spree.
				 */
				proc_list_lock();
				if (next_p) {
					proc_rele(next_p);
				}

				if (kill_count == max_kills) {
					memorystatus_log_info(
						"memorystatus: giving up aggressive kill after killing "
						"%d processes below band %d.\n",
						max_kills, priority_max + 1);
					break;
				}

				if (aPid_ep == JETSAM_PRIORITY_FOREGROUND && memorystatus_aggressive_jetsam_lenient == TRUE) {
					if (memorystatus_level > memorystatus_level_snapshot && ((memorystatus_level - memorystatus_level_snapshot) >= AGGRESSIVE_JETSAM_LENIENT_MODE_THRESHOLD)) {
#if DEVELOPMENT || DEBUG
						memorystatus_log_info("Disabling Lenient mode after one-time deployment.\n");
#endif /* DEVELOPMENT || DEBUG */
						memorystatus_aggressive_jetsam_lenient = FALSE;
						break;
					}
				}

				continue;
			}

			/*
			 * Failure - first unwind the state,
			 * then fall through to restart the search.
			 */
			proc_list_lock();
			proc_rele(p);
			if (next_p) {
				proc_rele(next_p);
			}
			p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
			p->p_memstat_state |= P_MEMSTAT_ERROR;
			*errors += 1;
			p = NULL;
		}

		/*
		 * Failure - restart the search at the beginning of
		 * the band we were already traversing.
		 *
		 * We might have raced with "p" exiting on another core, resulting in no
		 * ref on "p".  Or, we may have failed to kill "p".
		 *
		 * Either way, we fall thru to here, leaving the proc in the
		 * P_MEMSTAT_TERMINATED or P_MEMSTAT_ERROR state.
		 *
		 * And, we hold the the proc_list_lock at this point.
		 */

		next_p = memorystatus_get_first_proc_locked(&priority_band, TRUE);
	}

	proc_list_unlock();

exit:
	os_reason_free(jetsam_reason);

	/* Clear snapshot if freshly captured and no target was found */
	if (new_snapshot && (kill_count == 0)) {
		proc_list_lock();
		memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
		proc_list_unlock();
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, 0, kill_count, *memory_reclaimed);

	return kill_count > 0;
}

static boolean_t
memorystatus_kill_hiwat_proc(uint32_t *errors, boolean_t *purged, uint64_t *memory_reclaimed)
{
	pid_t aPid = 0;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	bool new_snapshot = false, killed = false, freed_mem = false;
	unsigned int i = 0;
	uint32_t aPid_ep;
	os_reason_t jetsam_reason = OS_REASON_NULL;
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_START,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES);

	jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_HIGHWATER);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus_kill_hiwat_proc: failed to allocate exit reason\n");
	}

	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&i, TRUE);
	while (next_p) {
		uint64_t footprint_in_bytes = 0;
		uint64_t memlimit_in_bytes  = 0;
		boolean_t skip = 0;

		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&i, p, TRUE);

		aPid = proc_getpid(p);
		aPid_ep = p->p_memstat_effectivepriority;

		if (p->p_memstat_state  & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP)) {
			continue;
		}

		/* skip if no limit set */
		if (p->p_memstat_memlimit <= 0) {
			continue;
		}

		footprint_in_bytes = get_task_phys_footprint(proc_task(p));
		memlimit_in_bytes  = (((uint64_t)p->p_memstat_memlimit) * 1024ULL * 1024ULL);   /* convert MB to bytes */
		skip = (footprint_in_bytes <= memlimit_in_bytes);

#if CONFIG_FREEZE
		if (!skip) {
			if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
				skip = TRUE;
			} else {
				skip = FALSE;
			}
		}
#endif

		if (skip) {
			continue;
		} else {
			if (memorystatus_jetsam_snapshot_count == 0) {
				memorystatus_init_jetsam_snapshot_locked(NULL, 0);
				new_snapshot = true;
			}

			if (proc_ref(p, true) == p) {
				/*
				 * Mark as terminated so that if exit1() indicates success, but the process (for example)
				 * is blocked in task_exception_notify(), it'll be skipped if encountered again - see
				 * <rdar://problem/13553476>. This is cheaper than examining P_LEXIT, which requires the
				 * acquisition of the proc lock.
				 */
				p->p_memstat_state |= P_MEMSTAT_TERMINATED;

				proc_list_unlock();
			} else {
				/*
				 * We need to restart the search again because
				 * proc_ref _can_ drop the proc_list lock
				 * and we could have lost our stored next_p via
				 * an exit() on another core.
				 */
				i = 0;
				next_p = memorystatus_get_first_proc_locked(&i, TRUE);
				continue;
			}

			footprint_in_bytes = 0;
			freed_mem = memorystatus_kill_proc(p, kMemorystatusKilledHiwat, jetsam_reason, &killed, &footprint_in_bytes); /* purged and/or killed 'p' */

			/* Success? */
			if (freed_mem) {
				if (!killed) {
					/* purged 'p'..don't reset HWM candidate count */
					*purged = TRUE;

					proc_list_lock();
					p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
					proc_list_unlock();
				} else {
					*memory_reclaimed = footprint_in_bytes;
				}
				proc_rele(p);
				goto exit;
			}
			/*
			 * Failure - first unwind the state,
			 * then fall through to restart the search.
			 */
			proc_list_lock();
			proc_rele(p);
			p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
			p->p_memstat_state |= P_MEMSTAT_ERROR;
			*errors += 1;

			i = 0;
			next_p = memorystatus_get_first_proc_locked(&i, TRUE);
		}
	}

	proc_list_unlock();

exit:
	os_reason_free(jetsam_reason);

	if (!killed) {
		*memory_reclaimed = 0;

		/* Clear snapshot if freshly captured and no target was found */
		if (new_snapshot) {
			proc_list_lock();
			memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
			proc_list_unlock();
		}
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM_HIWAT) | DBG_FUNC_END,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, killed, *memory_reclaimed, 0);

	return killed;
}

/*
 * Jetsam a process pinned in the elevated band.
 *
 * Return:  true -- a pinned process was jetsammed
 *	    false -- no pinned process was jetsammed
 */
boolean_t
memorystatus_kill_elevated_process(uint32_t cause, os_reason_t jetsam_reason, unsigned int band, int aggr_count, uint32_t *errors, uint64_t *memory_reclaimed)
{
	pid_t aPid = 0;
	proc_t p = PROC_NULL, next_p = PROC_NULL;
	boolean_t new_snapshot = FALSE, killed = FALSE;
	int kill_count = 0;
	uint32_t aPid_ep;
	uint64_t killtime = 0;
	uint64_t time_in_priority_band_secs = 0;
	clock_sec_t     tv_sec;
	clock_usec_t    tv_usec;
	uint32_t        tv_msec;
	uint64_t footprint_of_killed_proc = 0;


	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_START,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES);

#if CONFIG_FREEZE
	boolean_t consider_frozen_only = FALSE;

	if (band == (unsigned int) memorystatus_freeze_jetsam_band) {
		consider_frozen_only = TRUE;
	}
#endif /* CONFIG_FREEZE */

	proc_list_lock();

	next_p = memorystatus_get_first_proc_locked(&band, FALSE);
	while (next_p) {
		p = next_p;
		next_p = memorystatus_get_next_proc_locked(&band, p, FALSE);

		aPid = proc_getpid(p);
		aPid_ep = p->p_memstat_effectivepriority;

		/*
		 * Only pick a process pinned in this elevated band
		 */
		if (!_memstat_proc_is_elevated(p)) {
			continue;
		}

		if (p->p_memstat_state  & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP)) {
			continue;
		}

#if CONFIG_FREEZE
		if (consider_frozen_only && !_memstat_proc_is_frozen(p)) {
			continue;
		}

		if (p->p_memstat_state & P_MEMSTAT_LOCKED) {
			continue;
		}
#endif /* CONFIG_FREEZE */

#if DEVELOPMENT || DEBUG
		memorystatus_log_info(
			"jetsam: elevated%d process pid %d [%s] - memorystatus_available_pages: %d\n",
			aggr_count, aPid, (*p->p_name ? p->p_name : "unknown"), MEMORYSTATUS_LOG_AVAILABLE_PAGES);
#endif /* DEVELOPMENT || DEBUG */

		if (memorystatus_jetsam_snapshot_count == 0) {
			memorystatus_init_jetsam_snapshot_locked(NULL, 0);
			new_snapshot = TRUE;
		}

		p->p_memstat_state |= P_MEMSTAT_TERMINATED;

		killtime = mach_absolute_time();
		absolutetime_to_microtime(killtime, &tv_sec, &tv_usec);
		tv_msec = tv_usec / 1000;

		memorystatus_update_jetsam_snapshot_entry_locked(p, cause, killtime);

		if (proc_ref(p, true) == p) {
			proc_list_unlock();

			/*
			 * memorystatus_do_kill drops a reference, so take another one so we can
			 * continue to use this exit reason even after memorystatus_do_kill()
			 * returns
			 */
			os_reason_ref(jetsam_reason);
			killed = memorystatus_do_kill(p, cause, jetsam_reason, &footprint_of_killed_proc);

			absolutetime_to_nanoseconds(killtime - p->p_memstat_prio_start, &time_in_priority_band_secs);
			time_in_priority_band_secs /= NSEC_PER_SEC;
			memorystatus_log("%lu.%03d memorystatus: killing_top_process_elevated%d pid %d [%s] (%s %d %llus rf:%s type:%s) %lluKB - memorystatus_available_pages: %llu\n",
			    (unsigned long)tv_sec, tv_msec,
			    aggr_count,
			    aPid, ((p && *p->p_name) ? p->p_name : "unknown"),
			    memstat_kill_cause_name[cause], aPid_ep,
			    time_in_priority_band_secs, _memstat_relaunch_flags_description(p->p_memstat_relaunch_flags),
			    _memstat_proc_type_description(p),
			    footprint_of_killed_proc >> 10, (uint64_t)MEMORYSTATUS_LOG_AVAILABLE_PAGES);

			/* Success? */
			if (killed) {
				*memory_reclaimed = footprint_of_killed_proc;
				proc_rele(p);
				kill_count++;
				goto exit;
			}

			/*
			 * Failure - first unwind the state,
			 * then fall through to restart the search.
			 */
			proc_list_lock();
			proc_rele(p);
			p->p_memstat_state &= ~P_MEMSTAT_TERMINATED;
			p->p_memstat_state |= P_MEMSTAT_ERROR;
			*errors += 1;
		}

		/*
		 * Failure - restart the search.
		 *
		 * We might have raced with "p" exiting on another core, resulting in no
		 * ref on "p".  Or, we may have failed to kill "p".
		 *
		 * Either way, we fall thru to here, leaving the proc in the
		 * P_MEMSTAT_TERMINATED state or P_MEMSTAT_ERROR state.
		 *
		 * And, we hold the the proc_list_lock at this point.
		 */

		next_p = memorystatus_get_first_proc_locked(&band, FALSE);
	}

	proc_list_unlock();

exit:
	os_reason_free(jetsam_reason);

	if (kill_count == 0) {
		*memory_reclaimed = 0;

		/* Clear snapshot if freshly captured and no target was found */
		if (new_snapshot) {
			proc_list_lock();
			memorystatus_jetsam_snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
			proc_list_unlock();
		}
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_JETSAM) | DBG_FUNC_END,
	    MEMORYSTATUS_LOG_AVAILABLE_PAGES, killed ? aPid : 0, kill_count, *memory_reclaimed);

	return killed;
}

bool
memorystatus_kill_on_VM_compressor_space_shortage(bool async)
{
	if (async) {
		os_atomic_store(&memorystatus_compressor_space_shortage, true, release);
		memorystatus_thread_wake();
		return true;
	} else {
		os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_VMCOMPRESSOR_SPACE_SHORTAGE);
		if (jetsam_reason == OS_REASON_NULL) {
			memorystatus_log_error("memorystatus_kill_on_VM_compressor_space_shortage -- sync: failed to allocate jetsam reason\n");
		}

		return memstat_kill_process_sync(-1, kMemorystatusKilledVMCompressorSpaceShortage, jetsam_reason);
	}
}

#if CONFIG_JETSAM

void
memorystatus_kill_on_vps_starvation(void)
{
	os_atomic_store(&memorystatus_pageout_starved, true, release);
	memorystatus_thread_wake();
}

bool
memorystatus_kill_on_vnode_exhaustion(void)
{
	os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_VNODE);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("memorystatus_kill_on_vnode_limit: failed to allocate jetsam reason\n");
	}

	return memstat_kill_process_sync(-1, kMemorystatusKilledVnodes, jetsam_reason);
}

#endif /* CONFIG_JETSAM */

bool
memorystatus_kill_on_sustained_pressure()
{
	os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_MEMORY_SUSTAINED_PRESSURE);
	if (jetsam_reason == OS_REASON_NULL) {
		memorystatus_log_error("%s() failed to allocate jetsam reason\n", __func__);
	}

	return memstat_kill_process_sync(-1, kMemorystatusKilledSustainedPressure, jetsam_reason);
}

bool
memstat_kill_with_jetsam_reason_sync(pid_t pid, os_reason_t jetsam_reason)
{
	uint32_t kill_cause = jetsam_reason->osr_code <= JETSAM_REASON_MEMORYSTATUS_MAX ?
	    (uint32_t) jetsam_reason->osr_code : JETSAM_REASON_INVALID;
	return memstat_kill_process_sync(pid, kill_cause, jetsam_reason);
}

bool
memorystatus_kill_on_zone_map_exhaustion(pid_t pid)
{
	if (pid == -1) {
		os_atomic_store(&memorystatus_zone_map_is_exhausted, true, release);
		memorystatus_thread_wake();
		return true;
	} else {
		os_reason_t jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_ZONE_MAP_EXHAUSTION);
		if (jetsam_reason == OS_REASON_NULL) {
			memorystatus_log_error("memorystatus_kill_on_zone_map_exhaustion: failed to allocate jetsam reason\n");
		}
		return memstat_kill_process_sync(pid, kMemorystatusKilledZoneMapExhaustion, jetsam_reason);
	}
}

void
memorystatus_on_pageout_scan_end(void)
{
	/* No-op */
}

static size_t
memorystatus_priority_list_size(pid_t pid, size_t entry_size)
{
	assert(
		(entry_size == sizeof(memorystatus_priority_entry_t)) ||
		(entry_size == sizeof(memorystatus_priority_entry_v2_t)));
	uint32_t list_count = (pid == 0) ? memorystatus_list_count : 1;
	return entry_size * list_count;
}

/* Return both allocated and actual size, since there's a race between allocation and list compilation */
static int
memorystatus_get_priority_list(memorystatus_priority_entry_v2_t **list_ptr, size_t *buffer_size, size_t *list_size, size_t entry_size)
{
	memorystatus_priority_entry_v2_t *entry;
	proc_t p;
	uint32_t i = 0;

	*list_size = memorystatus_priority_list_size(0, entry_size);

	/* Otherwise, validate the size of the buffer */
	if (*buffer_size < *list_size) {
		return EINVAL;
	}

	*list_ptr = kalloc_data(*list_size, Z_WAITOK | Z_ZERO);
	if (!*list_ptr) {
		return ENOMEM;
	}

	*buffer_size = *list_size;
	*list_size = 0;

	entry = *list_ptr;

	proc_list_lock();

	p = memorystatus_get_first_proc_locked(&i, TRUE);
	while (p && (*list_size < *buffer_size)) {
		entry->pid = proc_getpid(p);
		entry->priority = p->p_memstat_effectivepriority;
		entry->user_data = p->p_memstat_userdata;

		if (p->p_memstat_memlimit <= 0) {
			task_get_phys_footprint_limit(proc_task(p), &entry->limit);
		} else {
			entry->limit = p->p_memstat_memlimit;
		}

		entry->state = _memstat_build_state(p);

		if (entry_size == sizeof(memorystatus_priority_entry_t)) {
			entry = (memorystatus_priority_entry_v2_t *) (((memorystatus_priority_entry_t *)entry) + 1);
		} else {
			/* Only add v2 entries if we're not using the legacy version of this call */
			entry->priority_start_mtime = p->p_memstat_prio_start;

			entry++;
		}

		*list_size += entry_size;
		p = memorystatus_get_next_proc_locked(&i, p, TRUE);
	}

	proc_list_unlock();

	memorystatus_log_debug("memorystatus_get_priority_list: returning %lu for size\n", (unsigned long)*list_size);

	return 0;
}

static int
memorystatus_get_priority_pid(pid_t pid, user_addr_t buffer, size_t buffer_size)
{
	int error = 0;
	bool zombref = false;
	memorystatus_priority_entry_v2_t mp_entry;
	kern_return_t ret;
	boolean_t size_valid =
	    (buffer_size == sizeof(memorystatus_priority_entry_v2_t)) ||
	    (buffer_size == sizeof(memorystatus_priority_entry_t));

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || !size_valid) {
		return EINVAL;
	}

	proc_list_lock();
	proc_t p = proc_find_locked(pid);
	if (!p) {
		zombref = true;
		p = proc_find_zombref_locked(pid);
		if (!p) {
			proc_list_unlock();
			return ESRCH;
		}
	}

	memset(&mp_entry, 0, sizeof(memorystatus_priority_entry_v2_t));

	mp_entry.pid = proc_getpid(p);
	mp_entry.priority = p->p_memstat_effectivepriority;
	mp_entry.user_data = p->p_memstat_userdata;
	if (p->p_memstat_memlimit <= 0 && !zombref) {
		task_t task = proc_task(p);
		assert(task);
		ret = task_get_phys_footprint_limit(task, &mp_entry.limit);
		if (ret != KERN_SUCCESS) {
			error = mach_to_bsd_errno(ret);
			proc_list_unlock();
			goto done;
		}
	} else {
		mp_entry.limit = p->p_memstat_memlimit;
	}

	mp_entry.state = _memstat_build_state(p);
	mp_entry.priority_start_mtime = p->p_memstat_prio_start;
	proc_list_unlock();

	error = copyout(&mp_entry, buffer, buffer_size);

done:
	if (zombref) {
		proc_drop_zombref(p);
	} else {
		proc_rele(p);
	}

	return error;
}

static int
memorystatus_cmd_get_priority_list(pid_t pid, user_addr_t buffer, size_t buffer_size, int32_t *retval, size_t entry_size)
{
	int error = 0;
	boolean_t size_only;
	size_t list_size;

	static_assert(sizeof(memorystatus_priority_entry_v2_t) == 128);
	assert(
		(entry_size == sizeof(memorystatus_priority_entry_t)) ||
		(entry_size == sizeof(memorystatus_priority_entry_v2_t)));

	/*
	 * When a non-zero pid is provided, the 'list' has only one entry.
	 */

	size_only = ((buffer == USER_ADDR_NULL) ? TRUE: FALSE);

	if (pid != 0) {
		/* One PID */
		list_size = entry_size;
		if (!size_only) {
			error = memorystatus_get_priority_pid(pid, buffer, buffer_size);
		}
	} else if (size_only) {
		/* List size query */
		list_size = memorystatus_priority_list_size(0, entry_size);
	} else {
		/* List */
		memorystatus_priority_entry_v2_t *list = NULL;
		error = memorystatus_get_priority_list(&list, &buffer_size, &list_size, entry_size);
		if (error == 0) {
			error = copyout(list, buffer, list_size);
			kfree_data(list, buffer_size);
		}
	}

	if (error == 0) {
		assert(list_size <= INT32_MAX);
		*retval = (int32_t) list_size;
	}

	return error;
}

static void
memorystatus_clear_errors(void)
{
	proc_t p;
	unsigned int i = 0;

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_START);

	proc_list_lock();

	p = memorystatus_get_first_proc_locked(&i, TRUE);
	while (p) {
		if (p->p_memstat_state & P_MEMSTAT_ERROR) {
			p->p_memstat_state &= ~P_MEMSTAT_ERROR;
		}
		p = memorystatus_get_next_proc_locked(&i, p, TRUE);
	}

	proc_list_unlock();

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_CLEAR_ERRORS) | DBG_FUNC_END);
}

void
memorystatus_fast_jetsam_override(bool enable_override)
{
#if CONFIG_JETSAM
	fast_jetsam_enabled = !enable_override;
	if (!fast_jetsam_enabled) {
		/* Disable any pre-configured policies */
		os_atomic_store(&memstat_policy_config, kPolicyDefault, relaxed);
		memorystatus_thread_pool_default();
		_memstat_consider_waking_jetsam_thread();
	}
#else /* CONFIG_JETSAM */
	(void)enable_override;
#endif /* CONFIG_JETSAM */
}

/*
 * Get the at_boot snapshot
 */
static int
memorystatus_get_at_boot_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only)
{
	size_t input_size = *snapshot_size;

	/*
	 * The at_boot snapshot has no entry list.
	 */
	*snapshot_size = sizeof(memorystatus_jetsam_snapshot_t);

	if (size_only) {
		return 0;
	}

	/*
	 * Validate the size of the snapshot buffer
	 */
	if (input_size < *snapshot_size) {
		return EINVAL;
	}

	/*
	 * Update the notification_time only
	 */
	memorystatus_at_boot_snapshot.notification_time = mach_absolute_time();
	*snapshot = &memorystatus_at_boot_snapshot;

	memorystatus_log_debug(
		"memorystatus_get_at_boot_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%d)\n",
		(long)input_size, (long)*snapshot_size, 0);
	return 0;
}

#if CONFIG_FREEZE
static int
memorystatus_get_jetsam_snapshot_freezer(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only)
{
	size_t input_size = *snapshot_size;

	if (memorystatus_jetsam_snapshot_freezer->entry_count > 0) {
		*snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_freezer->entry_count));
	} else {
		*snapshot_size = 0;
	}
	assert(*snapshot_size <= memorystatus_jetsam_snapshot_freezer_size);

	if (size_only) {
		return 0;
	}

	if (input_size < *snapshot_size) {
		return EINVAL;
	}

	*snapshot = memorystatus_jetsam_snapshot_freezer;

	memorystatus_log_debug(
		"memorystatus_get_jetsam_snapshot_freezer: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
		(long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_freezer->entry_count);

	return 0;
}
#endif /* CONFIG_FREEZE */

static int
memorystatus_get_on_demand_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only)
{
	size_t input_size = *snapshot_size;
	uint32_t ods_list_count = memorystatus_list_count + memorystatus_artificial_snapshot_entry_count;
	memorystatus_jetsam_snapshot_t *ods = NULL;     /* The on_demand snapshot buffer */

	*snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (ods_list_count));

	if (size_only) {
		return 0;
	}

	/*
	 * Validate the size of the snapshot buffer.
	 * This is inherently racey. May want to revisit
	 * this error condition and trim the output when
	 * it doesn't fit.
	 */
	if (input_size < *snapshot_size) {
		return EINVAL;
	}

	/*
	 * Allocate and initialize a snapshot buffer.
	 */
	ods = kalloc_data(*snapshot_size, Z_WAITOK | Z_ZERO);
	if (!ods) {
		return ENOMEM;
	}

	proc_list_lock();
	memorystatus_init_jetsam_snapshot_locked(ods, ods_list_count);
	proc_list_unlock();

	/*
	 * Return the kernel allocated, on_demand buffer.
	 * The caller of this routine will copy the data out
	 * to user space and then free the kernel allocated
	 * buffer.
	 */
	*snapshot = ods;

	memorystatus_log_debug(
		"memorystatus_get_on_demand_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
		(long)input_size, (long)*snapshot_size, (long)ods_list_count);

	return 0;
}

static int
memorystatus_get_jetsam_snapshot(memorystatus_jetsam_snapshot_t **snapshot, size_t *snapshot_size, boolean_t size_only)
{
	size_t input_size = *snapshot_size;

	if (memorystatus_jetsam_snapshot_count > 0) {
		*snapshot_size = sizeof(memorystatus_jetsam_snapshot_t) + (sizeof(memorystatus_jetsam_snapshot_entry_t) * (memorystatus_jetsam_snapshot_count));
	} else {
		*snapshot_size = 0;
	}

	if (size_only) {
		return 0;
	}

	if (input_size < *snapshot_size) {
		return EINVAL;
	}

	*snapshot = memorystatus_jetsam_snapshot;

	memorystatus_log_debug(
		"memorystatus_get_jetsam_snapshot: returned inputsize (%ld), snapshot_size(%ld), listcount(%ld)\n",
		(long)input_size, (long)*snapshot_size, (long)memorystatus_jetsam_snapshot_count);

	return 0;
}

#if JETSAM_ZPRINT_SNAPSHOT
/*
 * Utility function to handle copyout of jetsam zprint snapshot data
 */
static int
memorystatus_cmd_get_data_buffer(
	user_addr_t  buffer,
	size_t       buffer_size,
	int32_t      *retval,
	size_t       data_size,
	void         *data)
{
	boolean_t size_only = (buffer == USER_ADDR_NULL);
	int error;

	/* Nothing to return if there's no data yet, instruct the caller to try again later. */
	if (data == NULL) {
		*retval = -1;
		return EAGAIN;
	}

	/* Handle just a size request */
	if (size_only) {
		*retval = (int32_t)data_size;
		return 0;
	}

	/* buffer needs to be large enough */
	if (buffer_size < data_size) {
		*retval = -1;
		return EINVAL;
	}

	error = copyout(data, buffer, data_size);
	if (error == 0) {
		*retval = (int32_t)data_size;
	} else {
		*retval = -1;
	}

	return error;
}
#endif

static int
memorystatus_cmd_get_jetsam_snapshot(int32_t flags, user_addr_t buffer, size_t buffer_size, int32_t *retval)
{
	int error = EINVAL;
	boolean_t size_only;
	boolean_t is_default_snapshot = FALSE;
	boolean_t is_on_demand_snapshot = FALSE;
	boolean_t is_at_boot_snapshot = FALSE;
#if CONFIG_FREEZE
	bool is_freezer_snapshot = false;
#endif /* CONFIG_FREEZE */
	memorystatus_jetsam_snapshot_t *snapshot;

	size_only = ((buffer == USER_ADDR_NULL) ? TRUE : FALSE);

	if (flags == 0) {
		/* Default */
		is_default_snapshot = TRUE;
		error = memorystatus_get_jetsam_snapshot(&snapshot, &buffer_size, size_only);
	} else {
		if (flags & ~(MEMORYSTATUS_SNAPSHOT_ON_DEMAND | MEMORYSTATUS_SNAPSHOT_AT_BOOT | MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER)) {
			/*
			 * Unsupported bit set in flag.
			 */
			return EINVAL;
		}

		if (flags & (flags - 0x1)) {
			/*
			 * Can't have multiple flags set at the same time.
			 */
			return EINVAL;
		}

		if (flags & MEMORYSTATUS_SNAPSHOT_ON_DEMAND) {
			is_on_demand_snapshot = TRUE;
			/*
			 * When not requesting the size only, the following call will allocate
			 * an on_demand snapshot buffer, which is freed below.
			 */
			error = memorystatus_get_on_demand_snapshot(&snapshot, &buffer_size, size_only);
		} else if (flags & MEMORYSTATUS_SNAPSHOT_AT_BOOT) {
			is_at_boot_snapshot = TRUE;
			error = memorystatus_get_at_boot_snapshot(&snapshot, &buffer_size, size_only);
#if CONFIG_FREEZE
		} else if (flags & MEMORYSTATUS_FLAGS_SNAPSHOT_FREEZER) {
			is_freezer_snapshot = true;
			error = memorystatus_get_jetsam_snapshot_freezer(&snapshot, &buffer_size, size_only);
#endif /* CONFIG_FREEZE */
		} else {
			/*
			 * Invalid flag setting.
			 */
			return EINVAL;
		}
	}

	if (error) {
		goto out;
	}

	/*
	 * Copy the data out to user space and clear the snapshot buffer.
	 * If working with the jetsam snapshot,
	 *	clearing the buffer means, reset the count.
	 * If working with an on_demand snapshot
	 *	clearing the buffer means, free it.
	 * If working with the at_boot snapshot
	 *	there is nothing to clear or update.
	 * If working with a copy of the snapshot
	 *	there is nothing to clear or update.
	 * If working with the freezer snapshot
	 *	clearing the buffer means, reset the count.
	 */
	if (!size_only) {
		if ((error = copyout(snapshot, buffer, buffer_size)) == 0) {
#if CONFIG_FREEZE
			if (is_default_snapshot || is_freezer_snapshot) {
#else
			if (is_default_snapshot) {
#endif /* CONFIG_FREEZE */
				/*
				 * The jetsam snapshot is never freed, its count is simply reset.
				 * However, we make a copy for any parties that might be interested
				 * in the previous fully populated snapshot.
				 */
				proc_list_lock();
#if DEVELOPMENT || DEBUG
				if (memorystatus_testing_pid != 0 && memorystatus_testing_pid != proc_getpid(current_proc())) {
					/* Snapshot is currently owned by someone else. Don't consume it. */
					proc_list_unlock();
					goto out;
				}
#endif /* (DEVELOPMENT || DEBUG)*/
				if (is_default_snapshot) {
					snapshot->entry_count = memorystatus_jetsam_snapshot_count = 0;
				}
#if CONFIG_FREEZE
				else if (is_freezer_snapshot) {
					memorystatus_jetsam_snapshot_freezer->entry_count = 0;
				}
#endif /* CONFIG_FREEZE */
				proc_list_unlock();
			}
		}

		if (is_on_demand_snapshot) {
			/*
			 * The on_demand snapshot is always freed,
			 * even if the copyout failed.
			 */
			kfree_data(snapshot, buffer_size);
		}
	}

out:
	if (error == 0) {
		assert(buffer_size <= INT32_MAX);
		*retval = (int32_t) buffer_size;
	}
	return error;
}

#if DEVELOPMENT || DEBUG
static int
memorystatus_cmd_set_testing_pid(int32_t flags)
{
	int error = EINVAL;
	proc_t caller = current_proc();
	assert(caller != kernproc);
	proc_list_lock();
	if (flags & MEMORYSTATUS_FLAGS_SET_TESTING_PID) {
		if (memorystatus_testing_pid == 0) {
			memorystatus_testing_pid = proc_getpid(caller);
			error = 0;
		} else if (memorystatus_testing_pid == proc_getpid(caller)) {
			error = 0;
		} else {
			/* We don't allow ownership to be taken from another proc. */
			error = EBUSY;
		}
	} else if (flags & MEMORYSTATUS_FLAGS_UNSET_TESTING_PID) {
		if (memorystatus_testing_pid == proc_getpid(caller)) {
			memorystatus_testing_pid = 0;
			error = 0;
		} else if (memorystatus_testing_pid != 0) {
			/* We don't allow ownership to be taken from another proc. */
			error = EPERM;
		}
	} else if (flags & MEMORYSTATUS_FLAGS_SET_IMP_TESTING_PID) {
		caller->p_memstat_state |= P_MEMSTAT_TEST_IMP_ASSERTION;
		error = 0;
	}
	proc_list_unlock();

	return error;
}
#endif /* DEVELOPMENT || DEBUG */

/*
 *      Routine:	memorystatus_cmd_grp_set_priorities
 *	Purpose:	Update priorities for a group of processes.
 *
 *	[priority]
 *		Move each process out of its effective priority
 *		band and into a new priority band.
 *		Maintains relative order from lowest to highest priority.
 *		In single band, maintains relative order from head to tail.
 *
 *		eg: before	[effectivepriority | pid]
 *				[18 | p101              ]
 *				[17 | p55, p67, p19     ]
 *				[12 | p103 p10          ]
 *				[ 7 | p25               ]
 *			        [ 0 | p71, p82,         ]
 *
 *		after	[ new band | pid]
 *			[ xxx | p71, p82, p25, p103, p10, p55, p67, p19, p101]
 *
 *	Returns:  0 on success, else non-zero.
 *
 *	Caveat:   We know there is a race window regarding recycled pids.
 *		  A process could be killed before the kernel can act on it here.
 *		  If a pid cannot be found in any of the jetsam priority bands,
 *		  then we simply ignore it.  No harm.
 *		  But, if the pid has been recycled then it could be an issue.
 *		  In that scenario, we might move an unsuspecting process to the new
 *		  priority band. It's not clear how the kernel can safeguard
 *		  against this, but it would be an extremely rare case anyway.
 *		  The caller of this api might avoid such race conditions by
 *		  ensuring that the processes passed in the pid list are suspended.
 */


static int
memorystatus_cmd_grp_set_priorities(user_addr_t buffer, size_t buffer_size)
{
	/*
	 * We only handle setting priority
	 * per process
	 */
	int error = 0;
	memorystatus_properties_entry_v1_t *entries = NULL;
	size_t entry_count = 0;

	/* This will be the ordered proc list */
	typedef struct memorystatus_internal_properties {
		proc_t proc;
		int32_t priority;
	} memorystatus_internal_properties_t;

	memorystatus_internal_properties_t *table = NULL;
	uint32_t table_count = 0;

	size_t i = 0;
	uint32_t bucket_index = 0;
	int32_t new_priority;

	proc_t p;

	/* Verify inputs */
	if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) {
		error = EINVAL;
		goto out;
	}

	entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t));
	if (entry_count == 0) {
		/* buffer size was not large enough for a single entry */
		error = EINVAL;
		goto out;
	}

	if ((entries = kalloc_data(buffer_size, Z_WAITOK)) == NULL) {
		error = ENOMEM;
		goto out;
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY, entry_count);

	if ((error = copyin(buffer, entries, buffer_size)) != 0) {
		goto out;
	}

	/* Verify sanity of input priorities */
	if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) {
		if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) {
			error = EINVAL;
			goto out;
		}
	} else {
		error = EINVAL;
		goto out;
	}

	for (i = 0; i < entry_count; i++) {
		if (entries[i].priority == -1) {
			/* Use as shorthand for default priority */
			entries[i].priority = JETSAM_PRIORITY_DEFAULT;
		} else if (entries[i].priority > JETSAM_PRIORITY_IDLE && entries[i].priority <= applications_aging_band) {
			/*
			 * Everything between idle and the aging bands are reserved for internal use.
			 * if requested, adjust to JETSAM_PRIORITY_IDLE.
			 * Entitled processes (just munch) can use a subset of this range for testing.
			 */
			if (entries[i].priority > JETSAM_PRIORITY_ENTITLED_MAX ||
			    !_memstat_proc_can_use_entitled_range(current_proc())) {
				entries[i].priority = JETSAM_PRIORITY_IDLE;
			}
		} else if (entries[i].priority == JETSAM_PRIORITY_IDLE_HEAD) {
			/* JETSAM_PRIORITY_IDLE_HEAD inserts at the head of the idle
			 * queue */
			/* Deal with this later */
		} else if ((entries[i].priority < 0) || (entries[i].priority >= MEMSTAT_BUCKET_COUNT)) {
			/* Sanity check */
			error = EINVAL;
			goto out;
		}
	}

	table = kalloc_type(memorystatus_internal_properties_t, entry_count,
	    Z_WAITOK | Z_ZERO);
	if (table == NULL) {
		error = ENOMEM;
		goto out;
	}


	/*
	 * For each jetsam bucket entry, spin through the input property list.
	 * When a matching pid is found, populate an adjacent table with the
	 * appropriate proc pointer and new property values.
	 * This traversal automatically preserves order from lowest
	 * to highest priority.
	 */

	bucket_index = 0;

	proc_list_lock();

	/* Create the ordered table */
	p = memorystatus_get_first_proc_locked(&bucket_index, TRUE);
	while (p && (table_count < entry_count)) {
		for (i = 0; i < entry_count; i++) {
			if (proc_getpid(p) == entries[i].pid) {
				/* Build the table data  */
				table[table_count].proc = p;
				table[table_count].priority = entries[i].priority;
				table_count++;
				break;
			}
		}
		p = memorystatus_get_next_proc_locked(&bucket_index, p, TRUE);
	}

	/* We now have ordered list of procs ready to move */
	for (i = 0; i < table_count; i++) {
		p = table[i].proc;
		assert(p != NULL);
		memstat_priority_options_t priority_options = MEMSTAT_PRIORITY_OPTIONS_NONE;

		/* Allow head inserts -- but relative order is now  */
		if (table[i].priority == JETSAM_PRIORITY_IDLE_HEAD) {
			new_priority = JETSAM_PRIORITY_IDLE;
			priority_options |= (MEMSTAT_PRIORITY_INSERT_HEAD | MEMSTAT_PRIORITY_NO_AGING);
		} else {
			new_priority = table[i].priority;
		}

		/* Not allowed */
		if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
			continue;
		}

		memstat_update_priority_locked(p, new_priority, priority_options);
	}

	proc_list_unlock();

	/*
	 * if (table_count != entry_count)
	 * then some pids were not found in a jetsam band.
	 * harmless but interesting...
	 */
out:
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY, entry_count, table_count);

	kfree_data(entries, buffer_size);
	kfree_type(memorystatus_internal_properties_t, entry_count, table);

	return error;
}

memorystatus_internal_probabilities_t *memorystatus_global_probabilities_table = NULL;
size_t memorystatus_global_probabilities_size = 0;

static int
memorystatus_cmd_grp_set_probabilities(user_addr_t buffer, size_t buffer_size)
{
	int error = 0;
	memorystatus_properties_entry_v1_t *entries = NULL;
	size_t entry_count = 0, i = 0;
	memorystatus_internal_probabilities_t *tmp_table_new = NULL, *tmp_table_old = NULL;
	size_t tmp_table_new_size = 0, tmp_table_old_size = 0;
#if DEVELOPMENT || DEBUG
	if (memorystatus_testing_pid != 0 && memorystatus_testing_pid != proc_getpid(current_proc())) {
		/* probabilites are currently owned by someone else. Don't change them. */
		error = EPERM;
		goto out;
	}
#endif /* (DEVELOPMENT || DEBUG)*/

	/* Verify inputs */
	if ((buffer == USER_ADDR_NULL) || (buffer_size == 0)) {
		error = EINVAL;
		goto out;
	}

	entry_count = (buffer_size / sizeof(memorystatus_properties_entry_v1_t));
	if (entry_count == 0) {
		error = EINVAL;
		goto out;
	}

	if ((entries = kalloc_data(buffer_size, Z_WAITOK)) == NULL) {
		error = ENOMEM;
		goto out;
	}

	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_START, MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY, entry_count);

	if ((error = copyin(buffer, entries, buffer_size)) != 0) {
		goto out;
	}

	if (entries[0].version == MEMORYSTATUS_MPE_VERSION_1) {
		if ((buffer_size % MEMORYSTATUS_MPE_VERSION_1_SIZE) != 0) {
			error = EINVAL;
			goto out;
		}
	} else {
		error = EINVAL;
		goto out;
	}

	/* Verify sanity of input priorities */
	for (i = 0; i < entry_count; i++) {
		/*
		 * 0 - low probability of use.
		 * 1 - high probability of use.
		 *
		 * Keeping this field an int (& not a bool) to allow
		 * us to experiment with different values/approaches
		 * later on.
		 */
		if (entries[i].use_probability > 1) {
			error = EINVAL;
			goto out;
		}
	}

	tmp_table_new_size = sizeof(memorystatus_internal_probabilities_t) * entry_count;

	if ((tmp_table_new = kalloc_data(tmp_table_new_size, Z_WAITOK | Z_ZERO)) == NULL) {
		error = ENOMEM;
		goto out;
	}

	proc_list_lock();

	if (memorystatus_global_probabilities_table) {
		tmp_table_old = memorystatus_global_probabilities_table;
		tmp_table_old_size = memorystatus_global_probabilities_size;
	}

	memorystatus_global_probabilities_table = tmp_table_new;
	memorystatus_global_probabilities_size = tmp_table_new_size;
	tmp_table_new = NULL;

	for (i = 0; i < entry_count; i++) {
		/* Build the table data  */
		strlcpy(memorystatus_global_probabilities_table[i].proc_name, entries[i].proc_name, MAXCOMLEN + 1);
		memorystatus_global_probabilities_table[i].use_probability = entries[i].use_probability;
	}

	proc_list_unlock();

out:
	KDBG(MEMSTAT_CODE(BSD_MEMSTAT_GRP_SET_PROP) | DBG_FUNC_END, MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY, entry_count, tmp_table_new_size);

	kfree_data(entries, buffer_size);
	kfree_data(tmp_table_old, tmp_table_old_size);

	return error;
}

static int
memorystatus_cmd_grp_set_properties(int32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;

	if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_PRIORITY) {
		error = memorystatus_cmd_grp_set_priorities(buffer, buffer_size);
	} else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) == MEMORYSTATUS_FLAGS_GRP_SET_PROBABILITY) {
		error = memorystatus_cmd_grp_set_probabilities(buffer, buffer_size);
#if CONFIG_FREEZE
	} else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_FREEZE_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_FREEZE_PRIORITY) {
		error = memorystatus_cmd_grp_set_freeze_list(buffer, buffer_size);
	} else if ((flags & MEMORYSTATUS_FLAGS_GRP_SET_DEMOTE_PRIORITY) == MEMORYSTATUS_FLAGS_GRP_SET_DEMOTE_PRIORITY) {
		error = memorystatus_cmd_grp_set_demote_list(buffer, buffer_size);
#endif /* CONFIG_FREEZE */
	} else {
		error = EINVAL;
	}

	return error;
}

/*
 * This routine is used to update a process's jetsam priority position and stored user_data.
 * It is not used for the setting of memory limits.
 *
 * Flags passed into this call are used to distinguish the motivation behind a jetsam priority
 * transition.  By default, the kernel updates the process's original requested priority when
 * no flag is passed.  But when the MEMORYSTATUS_SET_PRIORITY_ASSERTION flag is used, the kernel
 * updates the process's assertion driven priority.
 *
 * The assertion flag was introduced for use by the device's assertion mediator (eg: runningboardd).
 * When an assertion is controlling a process's jetsam priority, it may conflict with that process's
 * dirty/clean (active/inactive) jetsam state.  The kernel attempts to resolve a priority transition
 * conflict by reviewing the process state and then choosing the maximum jetsam band at play,
 * eg: requested priority versus assertion priority.
 */

static int
memorystatus_cmd_set_priority_properties(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;
	memorystatus_priority_properties_t mpp_entry;

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_priority_properties_t))) {
		return EINVAL;
	}

	/* Validate flags */
	if (flags & ~(MEMORYSTATUS_SET_PRIORITY_ASSERTION)) {
		/*
		 * Unsupported bit set in flag.
		 */
		return EINVAL;
	}

	error = copyin(buffer, &mpp_entry, buffer_size);

	if (error == 0) {
		proc_t p;

		p = proc_find(pid);
		if (!p) {
			return ESRCH;
		}

		if (p->p_memstat_state & P_MEMSTAT_INTERNAL) {
			proc_rele(p);
			return EPERM;
		}

		if ((flags & MEMORYSTATUS_SET_PRIORITY_ASSERTION) &&
		    !(p->p_memstat_state & P_MEMSTAT_MANAGED)) {
			/*
			 * Assertion-
			 * processes.
			 */
			proc_rele(p);
			return EPERM;
		}

		memstat_priority_options_t options = MEMSTAT_PRIORITY_OPTIONS_NONE;
		if (flags & MEMORYSTATUS_SET_PRIORITY_ASSERTION) {
			options |= MEMSTAT_PRIORITY_IS_ASSERTION;
		}
		error = memorystatus_set_priority(p, mpp_entry.priority, mpp_entry.user_data,
		    options);
		proc_rele(p);
	}

	return error;
}

static int
memorystatus_cmd_set_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;
	memorystatus_memlimit_properties_t mmp_entry;

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_memlimit_properties_t))) {
		return EINVAL;
	}

	error = copyin(buffer, &mmp_entry, buffer_size);

	if (error == 0) {
		error = memorystatus_set_memlimit_properties(pid, &mmp_entry);
	}

	return error;
}

#if DEBUG || DEVELOPMENT
static int
memorystatus_cmd_set_diag_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;
	memorystatus_diag_memlimit_properties_t mmp_entry;
	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_diag_memlimit_properties_t))) {
		proc_rele(p);
		return EINVAL;
	}

	error = copyin(buffer, &mmp_entry, buffer_size);

	if (error == 0) {
		proc_list_lock();
		error = memorystatus_set_diag_memlimit_properties_internal(p, &mmp_entry);
		proc_list_unlock();
	}
	proc_rele(p);
	return error;
}

static int
memorystatus_cmd_get_diag_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;
	memorystatus_diag_memlimit_properties_t mmp_entry;
	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(memorystatus_diag_memlimit_properties_t))) {
		proc_rele(p);
		return EINVAL;
	}
	proc_list_lock();
	error = memorystatus_get_diag_memlimit_properties_internal(p, &mmp_entry);
	proc_list_unlock();
	proc_rele(p);
	if (error == 0) {
		error = copyout(&mmp_entry, buffer, buffer_size);
	}


	return error;
}
#endif //DEBUG || DEVELOPMENT

static int
_memstat_get_process_conclave_mem_limit(pid_t pid, int32_t *retval)
{
	kern_return_t error;
	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	uint64_t conclave_limit;
	error = task_get_conclave_mem_limit(proc_task(p), &conclave_limit);

	if (error == KERN_SUCCESS) {
		*retval = roundToNearestMB((uint32_t)conclave_limit);
	}

	proc_rele(p);
	return mach_to_bsd_errno(error);
}

static void
memorystatus_get_memlimit_properties_internal(proc_t p, memorystatus_memlimit_properties_t* p_entry)
{
	memset(p_entry, 0, sizeof(memorystatus_memlimit_properties_t));

	if (p->p_memstat_memlimit_active > 0) {
		p_entry->memlimit_active = p->p_memstat_memlimit_active;
	} else {
		task_convert_phys_footprint_limit(-1, &p_entry->memlimit_active);
	}

	if (_memstat_proc_active_memlimit_is_fatal(p)) {
		p_entry->memlimit_active_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
	}

	/*
	 * Get the inactive limit and attributes
	 */
	if (p->p_memstat_memlimit_inactive <= 0) {
		task_convert_phys_footprint_limit(-1, &p_entry->memlimit_inactive);
	} else {
		p_entry->memlimit_inactive = p->p_memstat_memlimit_inactive;
	}
	if (_memstat_proc_inactive_memlimit_is_fatal(p)) {
		p_entry->memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
	}
}

/*
 * When getting the memlimit settings, we can't simply call task_get_phys_footprint_limit().
 * That gets the proc's cached memlimit and there is no guarantee that the active/inactive
 * limits will be the same in the no-limit case.  Instead we convert limits <= 0 using
 * task_convert_phys_footprint_limit(). It computes the same limit value that would be written
 * to the task's ledgers via task_set_phys_footprint_limit().
 */
static int
memorystatus_cmd_get_memlimit_properties(pid_t pid, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	memorystatus_memlimit_properties2_t mmp_entry;

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) ||
	    ((buffer_size != sizeof(memorystatus_memlimit_properties_t)) &&
	    (buffer_size != sizeof(memorystatus_memlimit_properties2_t)))) {
		return EINVAL;
	}

	memset(&mmp_entry, 0, sizeof(memorystatus_memlimit_properties2_t));

	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/*
	 * Get the active limit and attributes.
	 * No locks taken since we hold a reference to the proc.
	 */

	memorystatus_get_memlimit_properties_internal(p, &mmp_entry.v1);

#if CONFIG_JETSAM
#if DEVELOPMENT || DEBUG
	/*
	 * Get the limit increased via SPI
	 */
	mmp_entry.memlimit_increase = roundToNearestMB(p->p_memlimit_increase);
	mmp_entry.memlimit_increase_bytes = p->p_memlimit_increase;
#endif /* DEVELOPMENT || DEBUG */
#endif /* CONFIG_JETSAM */

	proc_rele(p);

	int error = copyout(&mmp_entry, buffer, buffer_size);

	return error;
}


/*
 * SPI for kbd - pr24956468
 * This is a very simple snapshot that calculates how much a
 * process's phys_footprint exceeds a specific memory limit.
 * Only the inactive memory limit is supported for now.
 * The delta is returned as bytes in excess or zero.
 */
static int
memorystatus_cmd_get_memlimit_excess_np(pid_t pid, uint32_t flags, user_addr_t buffer, size_t buffer_size, __unused int32_t *retval)
{
	int error = 0;
	uint64_t footprint_in_bytes = 0;
	uint64_t delta_in_bytes = 0;
	int32_t  memlimit_mb = 0;
	uint64_t memlimit_bytes = 0;

	/* Validate inputs */
	if ((pid == 0) || (buffer == USER_ADDR_NULL) || (buffer_size != sizeof(uint64_t)) || (flags != 0)) {
		return EINVAL;
	}

	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/*
	 * Get the inactive limit.
	 * No locks taken since we hold a reference to the proc.
	 */

	if (p->p_memstat_memlimit_inactive <= 0) {
		task_convert_phys_footprint_limit(-1, &memlimit_mb);
	} else {
		memlimit_mb = p->p_memstat_memlimit_inactive;
	}

	footprint_in_bytes = get_task_phys_footprint(proc_task(p));

	proc_rele(p);

	memlimit_bytes = memlimit_mb * 1024 * 1024;     /* MB to bytes */

	/*
	 * Computed delta always returns >= 0 bytes
	 */
	if (footprint_in_bytes > memlimit_bytes) {
		delta_in_bytes = footprint_in_bytes - memlimit_bytes;
	}

	error = copyout(&delta_in_bytes, buffer, sizeof(delta_in_bytes));

	return error;
}


static int
memorystatus_cmd_get_pressure_status(int32_t *retval)
{
	int error;

	/* Need privilege for check */
	error = priv_check_cred(kauth_cred_get(), PRIV_VM_PRESSURE, 0);
	if (error) {
		return error;
	}

	/* Inherently racy, so it's not worth taking a lock here */
	*retval = (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0;

	return error;
}

int
memorystatus_get_pressure_status_kdp()
{
	return (kVMPressureNormal != memorystatus_vm_pressure_level) ? 1 : 0;
}

/*
 * Every process, including a P_MEMSTAT_INTERNAL process (currently only pid 1), is allowed to set a HWM.
 *
 * This call is inflexible -- it does not distinguish between active/inactive, fatal/non-fatal
 * So, with 2-level HWM preserving previous behavior will map as follows.
 *      - treat the limit passed in as both an active and inactive limit.
 *      - treat the is_fatal_limit flag as though it applies to both active and inactive limits.
 *
 * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK
 *      - the is_fatal_limit is FALSE, meaning the active and inactive limits are non-fatal/soft
 *      - so mapping is (active/non-fatal, inactive/non-fatal)
 *
 * When invoked via MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT
 *      - the is_fatal_limit is TRUE, meaning the process's active and inactive limits are fatal/hard
 *      - so mapping is (active/fatal, inactive/fatal)
 */

#if CONFIG_JETSAM
static int
memorystatus_cmd_set_jetsam_memory_limit(pid_t pid, int32_t high_water_mark, __unused int32_t *retval, boolean_t is_fatal_limit)
{
	int error = 0;
	memorystatus_memlimit_properties_t entry;

	entry.memlimit_active = high_water_mark;
	entry.memlimit_active_attr = 0;
	entry.memlimit_inactive = high_water_mark;
	entry.memlimit_inactive_attr = 0;

	if (is_fatal_limit == TRUE) {
		entry.memlimit_active_attr   |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
		entry.memlimit_inactive_attr |= MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
	}

	error = memorystatus_set_memlimit_properties(pid, &entry);
	return error;
}

static int
memorystatus_cmd_mark_process_coalition_swappable(pid_t pid, __unused int32_t *retval)
{
	int error = 0;
	proc_t p = PROC_NULL;
	coalition_t coal = COALITION_NULL;

	if (!memorystatus_swap_all_apps) {
		/* Swap is not supported on this device. */
		return ENOTSUP;
	}
	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}
	coal = task_get_coalition((task_t) proc_task(p), COALITION_TYPE_JETSAM);
	if (coal && coalition_is_leader((task_t) proc_task(p), coal)) {
		coalition_mark_swappable(coal);
	} else {
		/* This SPI is only supported on coalition leaders. */
		error = EINVAL;
	}

	proc_rele(p);
	return error;
}

static int
memorystatus_cmd_get_process_coalition_is_swappable(pid_t pid, int32_t *retval)
{
	int error = 0;
	proc_t p = PROC_NULL;
	coalition_t coal = COALITION_NULL;

	if (!memorystatus_swap_all_apps) {
		/* Swap is not supported on this device. */
		return ENOTSUP;
	}
	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}
	coal = task_get_coalition((task_t) proc_task(p), COALITION_TYPE_JETSAM);
	if (coal) {
		*retval = coalition_is_swappable(coal);
	} else {
		error = EINVAL;
	}

	proc_rele(p);
	return error;
}

static int
memorystatus_cmd_convert_memlimit_mb(pid_t pid, int32_t limit, int32_t *retval)
{
	int error = 0;
	proc_t p;
	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}
	if (limit <= 0) {
		/*
		 * A limit of <= 0 implies that the task gets its default limit.
		 */
		limit = memorystatus_get_default_task_active_limit(p);
		if (limit <= 0) {
			/* Task uses system wide default limit */
			limit = max_task_footprint_mb ? max_task_footprint_mb : INT32_MAX;
		}
		*retval = limit;
	} else {
#if DEVELOPMENT || DEBUG
		/* add the current increase to it, for roots */
		limit += roundToNearestMB(p->p_memlimit_increase);
#endif /* DEVELOPMENT || DEBUG */
		*retval = limit;
	}

	proc_rele(p);
	return error;
}

static int
_memstat_rearm_proc_memlimit(proc_t proc, void* flagsptr)
{
	task_t task = proc_task(proc);
	uint32_t flags = *((uint32_t *) flagsptr);

	if (flags & MEMORYSTATUS_FLAGS_REARM_ACTIVE) {
		task_reset_triggered_exc_resource(task, true);
	}
	if (flags & MEMORYSTATUS_FLAGS_REARM_INACTIVE) {
		task_reset_triggered_exc_resource(task, false);
	}

	return 0;
}

static int
memorystatus_cmd_rearm_memlimit(pid_t pid, uint32_t flags, __unused int32_t *retval)
{
	if (pid == -1) {
		/* Re-arm all pids */
		proc_iterate(
			PROC_ALLPROCLIST,
			_memstat_rearm_proc_memlimit,
			&flags,
			NULL,
			NULL);
	} else {
		/* Re-arm one pid */
		proc_t p = (pid == proc_selfpid()) ? proc_self() : proc_find(pid);
		if (!p) {
			return ESRCH;
		}
		_memstat_rearm_proc_memlimit(p, &flags);
		proc_rele(p);
	}

	return 0;
}
#endif /* CONFIG_JETSAM */

#if DEBUG || DEVELOPMENT
static int
memorystatus_set_diag_memlimit_properties_internal(proc_t p, memorystatus_diag_memlimit_properties_t *p_entry)
{
	int error = 0;
	uint64_t old_limit = 0;

	LCK_MTX_ASSERT(&proc_list_mlock, LCK_MTX_ASSERT_OWNED);
	/* Enforce the limit by writing to the ledgers */
	error = (task_set_diag_footprint_limit_internal(proc_task(p), p_entry->memlimit, &old_limit) == KERN_SUCCESS) ? KERN_SUCCESS : EINVAL;

	memorystatus_log_debug( "memorystatus_set_diag_memlimit_properties: new limit on pid %d (%lluMB old %lluMB)\n",
	    proc_getpid(p), (p_entry->memlimit > 0 ? p_entry->memlimit : -1), (old_limit)
	    );
	DTRACE_MEMORYSTATUS2(memorystatus_diag_memlimit_properties_t, proc_t, p, int32_t, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1));
	return error;
}

static int
memorystatus_get_diag_memlimit_properties_internal(proc_t p, memorystatus_diag_memlimit_properties_t *p_entry)
{
	int error = 0;
	/* Enforce the limit by writing to the ledgers */
	error = (task_get_diag_footprint_limit_internal(proc_task(p), &p_entry->memlimit, &p_entry->threshold_enabled) == KERN_SUCCESS) ? KERN_SUCCESS : EINVAL;

	DTRACE_MEMORYSTATUS2(memorystatus_diag_memlimit_properties_t, proc_t, p, int32_t, (p->p_memstat_memlimit > 0 ? p->p_memstat_memlimit : -1));
	return error;
}
#endif // DEBUG || DEVELOPMENT

bool
memorystatus_task_has_increased_memory_limit_entitlement(task_t task)
{
	if (memorystatus_entitled_max_task_footprint_mb == 0) {
		// Entitlement is not supported on this device.
		return false;
	}
	return IOTaskHasEntitlement(task,
	           "com.apple.developer.kernel.increased-memory-limit");
}

bool
memorystatus_task_has_increased_debugging_memory_limit_entitlement(task_t task)
{
	if (memorystatus_entitled_dev_max_task_footprint_mb == 0) {
		// Entitlement is not supported on this device.
		return false;
	}
	return IOTaskHasEntitlement(task,
	           "com.apple.developer.kernel.increased-debugging-memory-limit");
}

bool
memorystatus_task_has_legacy_footprint_entitlement(task_t task)
{
	return IOTaskHasEntitlement(task,
	           "com.apple.private.memory.legacy_footprint");
}

bool
memorystatus_task_has_ios13extended_footprint_limit(task_t task)
{
	if (max_mem < 1500ULL * 1024 * 1024 ||
	    max_mem > 2ULL * 1024 * 1024 * 1024) {
		/* ios13extended_footprint is only for 2GB devices */
		return false;
	}
	return IOTaskHasEntitlement(task,
	           "com.apple.developer.memory.ios13extended_footprint");
}

static int32_t
memorystatus_get_default_task_active_limit(proc_t p)
{
	int32_t limit = (int32_t)max_task_footprint_mb;
	task_t task = proc_task(p);

	/*
	 * Check for the various entitlement footprint hacks
	 * and try to apply each one. Note that if multiple entitlements are present
	 * whichever results in the largest limit applies.
	 */
	if (memorystatus_task_has_increased_debugging_memory_limit_entitlement(task)) {
		limit = MAX(limit, memorystatus_entitled_dev_max_task_footprint_mb);
	}
	if (memorystatus_task_has_increased_memory_limit_entitlement(task)) {
#if CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT
		/* On visionOS, we want a separate memory limit for iOS (bincompat) apps. */
		if ((proc_platform(p) == PLATFORM_IOS) &&
		    (memorystatus_entitled_bincompat_max_task_footprint_mb != 0)) {
			limit = MAX(limit, memorystatus_entitled_bincompat_max_task_footprint_mb);
		} else {
			limit = MAX(limit, memorystatus_entitled_max_task_footprint_mb);
		}
#else /* CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT */
		limit = MAX(limit, memorystatus_entitled_max_task_footprint_mb);
#endif /* !CONFIG_SEPARATE_BINCOMPAT_ENTITLED_MEMLIMIT */
	}
#if __arm64__
	if (legacy_footprint_entitlement_mode == LEGACY_FOOTPRINT_ENTITLEMENT_LIMIT_INCREASE &&
	    memorystatus_task_has_legacy_footprint_entitlement(task)) {
		limit = MAX(limit, max_task_footprint_mb + legacy_footprint_bonus_mb);
	}
#endif /* __arm64__ */
	if (memorystatus_task_has_ios13extended_footprint_limit(task)) {
		limit = MAX(limit, memorystatus_ios13extended_footprint_limit_mb);
	}

	return limit;
}

static int32_t
memorystatus_get_default_task_inactive_limit(proc_t p)
{
	// Currently the default active and inactive limits are always the same.
	return memorystatus_get_default_task_active_limit(p);
}

static int
memorystatus_set_memlimit_properties(pid_t pid, memorystatus_memlimit_properties_t *entry)
{
	int32_t memlimit_active, memlimit_inactive;
	memlimit_options_t memlimit_options = MEMLIMIT_OPTIONS_NONE;

	proc_t p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	/*
	 * Check for valid attribute flags.
	 */
	const uint32_t valid_attrs = MEMORYSTATUS_MEMLIMIT_ATTR_FATAL;
	if ((entry->memlimit_active_attr & (~valid_attrs)) != 0) {
		proc_rele(p);
		return EINVAL;
	}
	if ((entry->memlimit_inactive_attr & (~valid_attrs)) != 0) {
		proc_rele(p);
		return EINVAL;
	}

	/*
	 * Setup the active memlimit properties
	 */
	memlimit_active = entry->memlimit_active;
	if (entry->memlimit_active_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) {
		memlimit_options |= MEMLIMIT_ACTIVE_FATAL;
	}

	/*
	 * Setup the inactive memlimit properties
	 */
	memlimit_inactive = entry->memlimit_inactive;
	if (entry->memlimit_inactive_attr & MEMORYSTATUS_MEMLIMIT_ATTR_FATAL) {
		memlimit_options |= MEMLIMIT_INACTIVE_FATAL;
	}

	int error = memorystatus_set_memlimits(p, memlimit_active,
	    memlimit_inactive, memlimit_options);
	proc_rele(p);
	return error;
}

/*
 * Returns the jetsam priority (effective or requested) of the process
 * associated with this task.
 */
int
proc_get_memstat_priority(proc_t p, boolean_t effective_priority)
{
	if (p) {
		if (effective_priority) {
			return p->p_memstat_effectivepriority;
		} else {
			return p->p_memstat_requestedpriority;
		}
	}
	return 0;
}

static int
memorystatus_get_process_is_managed(pid_t pid, int *is_managed)
{
	proc_t p = NULL;

	/* Validate inputs */
	if (pid == 0) {
		return EINVAL;
	}

	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	*is_managed = memorystatus_get_proc_is_managed(p) ? 1 : 0;

	proc_rele(p);

	return 0;
}

bool
memorystatus_get_proc_is_managed(proc_t proc)
{
	proc_list_lock();
	bool is_managed = _memstat_proc_is_managed(proc);
	proc_list_unlock();
	return is_managed;
}


static int
memorystatus_set_process_is_managed(pid_t pid, boolean_t set_managed)
{
	proc_t p = NULL;

	/* Validate inputs */
	if (pid == 0) {
		return EINVAL;
	}

	p = proc_find(pid);
	if (!p) {
		return ESRCH;
	}

	proc_list_lock();

	if (set_managed == TRUE) {
		p->p_memstat_state |= P_MEMSTAT_MANAGED;
		/*
		 * The P_MEMSTAT_MANAGED bit is set by Runningboard for Apps.
		 * Also opt them in to being frozen (they might have started
		 * off with the P_MEMSTAT_FREEZE_DISABLED bit set.)
		 */
		p->p_memstat_state &= ~P_MEMSTAT_FREEZE_DISABLED;
	} else {
		p->p_memstat_state &= ~P_MEMSTAT_MANAGED;
	}

	if (_memstat_proc_is_tracked(p)) {
		memorystatus_log_error("memorystatus: process %s [%d] opted in to both "
		    "Management and ActivityTracking\n", proc_best_name(p),
		    proc_pid(p));
	}

	proc_list_unlock();

	proc_rele(p);

	return 0;
}

static int
_memstat_get_kill_count(int priority, memorystatus_kill_cause_t cause, bool clear)
{
	uint32_t _Atomic *ptr;

	assert(priority >= JETSAM_PRIORITY_IDLE);
	assert(priority <= JETSAM_PRIORITY_MAX);

	/* rdar://141462516 */
	if (cause == kMemorystatusInvalid) {
		return 0;
	} else if (cause == kMemorystatusKilledIdleExit) {
		if (priority == JETSAM_PRIORITY_IDLE) {
			ptr = &memorystatus_idle_exit_kill_count;
		} else {
			return 0; /* This never happens */
		}
	} else {
		if (cause < kMemorystatusKilledIdleExit) {
			ptr = &memorystatus_kill_counts[priority][cause - 1];
		} else {
			ptr = &memorystatus_kill_counts[priority][cause - 2];
		}
	}

	if (clear) {
		return os_atomic_xchg(ptr, 0, relaxed);
	} else {
		return os_atomic_load(ptr, relaxed);
	}
}

static int
memorystatus_cmd_get_kill_counts(int priority, user_addr_t buffer, size_t buffer_size, int flags)
{
	memorystatus_kill_cause_t cause;
	uint32_t outbuf[JETSAM_REASON_MEMORYSTATUS_MAX + 1];
	bool clear = flags & MEMORYSTATUS_GET_KILL_COUNTS_CLEAR;

	if (((buffer_size % sizeof(uint32_t)) != 0) ||
	    (priority < JETSAM_PRIORITY_IDLE) ||
	    (priority > JETSAM_PRIORITY_MAX)) {
		return EINVAL;
	}

	for (cause = kMemorystatusInvalid; cause <= JETSAM_REASON_MEMORYSTATUS_MAX; cause++) {
		outbuf[cause] = _memstat_get_kill_count(priority, cause, clear);
	}

	return copyout(outbuf, buffer, MIN(buffer_size, sizeof(outbuf)));
}

int
memorystatus_control(struct proc *p, struct memorystatus_control_args *args, int *ret)
{
	int error = EINVAL;
	boolean_t skip_auth_check = FALSE;
	os_reason_t jetsam_reason = OS_REASON_NULL;

#if !CONFIG_JETSAM
    #pragma unused(ret)
    #pragma unused(jetsam_reason)
#endif

	/* We don't need entitlements if we're setting / querying the freeze preference or frozen status for a process. */
	if (args->command == MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE ||
	    args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE ||
	    args->command == MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN) {
		skip_auth_check = TRUE;
	}

	/*
	 * On development kernel, we don't need entitlements if we're adjusting the limit.
	 * This required for limit adjustment by dyld when roots are detected, see rdar://99669958
	 */
#if DEVELOPMENT || DEBUG
	if (args->command == MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT && proc_getpid(p) == args->pid) {
		skip_auth_check = TRUE;
	}
#endif /* DEVELOPMENT || DEBUG */

#if DEVELOPMENT || DEBUG
	/*
	 * On development kernels, processes should be able to re-arm themselves
	 * without entitlement for testing.
	 */
	if (args->command == MEMORYSTATUS_CMD_REARM_MEMLIMIT && proc_getpid(p) == args->pid) {
		skip_auth_check = TRUE;
	}
#endif

	/* Need to be root or have entitlement. */
	if (!kauth_cred_issuser(kauth_cred_get()) && !IOCurrentTaskHasEntitlement(MEMORYSTATUS_ENTITLEMENT) && !skip_auth_check) {
		error = EPERM;
		goto out;
	}

	/*
	 * Sanity check.
	 * Do not enforce it for snapshots or v2 priority list.
	 * (the latter always allocates an appropriately-sized buffer.)
	 */
	if (args->command != MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT &&
	    args->command != MEMORYSTATUS_CMD_GET_PRIORITY_LIST_V2 &&
	    args->command != MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_NAMES &&
	    args->command != MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_INFO &&
	    args->command != MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_MEMINFO) {
		if (args->buffersize > MEMORYSTATUS_BUFFERSIZE_MAX) {
			error = EINVAL;
			goto out;
		}
	}

#if CONFIG_MACF
	error = mac_proc_check_memorystatus_control(p, args->command, args->pid);
	if (error) {
		goto out;
	}
#endif /* MAC */

	switch (args->command) {
	case MEMORYSTATUS_CMD_GET_PRIORITY_LIST:
		error = memorystatus_cmd_get_priority_list(
			args->pid,
			args->buffer,
			args->buffersize,
			ret,
			sizeof(memorystatus_priority_entry_t));
		break;
	case MEMORYSTATUS_CMD_GET_PRIORITY_LIST_V2:
		error = memorystatus_cmd_get_priority_list(
			args->pid,
			args->buffer,
			args->buffersize,
			ret,
			sizeof(memorystatus_priority_entry_v2_t));
		break;
	case MEMORYSTATUS_CMD_SET_PRIORITY_PROPERTIES:
		error = memorystatus_cmd_set_priority_properties(args->pid, args->flags, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_SET_MEMLIMIT_PROPERTIES:
		error = memorystatus_cmd_set_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_GET_MEMLIMIT_PROPERTIES:
		error = memorystatus_cmd_get_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_GET_MEMLIMIT_EXCESS:
		error = memorystatus_cmd_get_memlimit_excess_np(args->pid, args->flags, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_GRP_SET_PROPERTIES:
		error = memorystatus_cmd_grp_set_properties((int32_t)args->flags, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_GET_JETSAM_SNAPSHOT:
		error = memorystatus_cmd_get_jetsam_snapshot((int32_t)args->flags, args->buffer, args->buffersize, ret);
		break;
#if JETSAM_ZPRINT_SNAPSHOT
	case MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_NAMES:
		error = memorystatus_cmd_get_data_buffer(args->buffer, args->buffersize, ret,
		    jzs_zone_cnt * sizeof(mach_zone_name_t), jzs_names);
		break;
	case MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_INFO:
		error = memorystatus_cmd_get_data_buffer(args->buffer, args->buffersize, ret,
		    jzs_zone_cnt * sizeof(mach_zone_info_t), jzs_info);
		break;
	case MEMORYSTATUS_CMD_GET_JETSAM_ZPRINT_MEMINFO:
		error = memorystatus_cmd_get_data_buffer(args->buffer, args->buffersize, ret,
		    jzs_meminfo_cnt * sizeof(mach_memory_info_t), jzs_meminfo);
		break;
#endif
#if DEVELOPMENT || DEBUG
	case MEMORYSTATUS_CMD_SET_TESTING_PID:
		error = memorystatus_cmd_set_testing_pid((int32_t) args->flags);
		break;
#endif
	case MEMORYSTATUS_CMD_GET_PRESSURE_STATUS:
		error = memorystatus_cmd_get_pressure_status(ret);
		break;
#if CONFIG_JETSAM
	case MEMORYSTATUS_CMD_SET_JETSAM_HIGH_WATER_MARK:
		/*
		 * This call does not distinguish between active and inactive limits.
		 * Default behavior in 2-level HWM world is to set both.
		 * Non-fatal limit is also assumed for both.
		 */
		error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, FALSE);
		break;
	case MEMORYSTATUS_CMD_SET_JETSAM_TASK_LIMIT:
		/*
		 * This call does not distinguish between active and inactive limits.
		 * Default behavior in 2-level HWM world is to set both.
		 * Fatal limit is also assumed for both.
		 */
		error = memorystatus_cmd_set_jetsam_memory_limit(args->pid, (int32_t)args->flags, ret, TRUE);
		break;
	case MEMORYSTATUS_CMD_MARK_PROCESS_COALITION_SWAPPABLE:
		error = memorystatus_cmd_mark_process_coalition_swappable(args->pid, ret);
		break;

	case MEMORYSTATUS_CMD_GET_PROCESS_COALITION_IS_SWAPPABLE:
		error = memorystatus_cmd_get_process_coalition_is_swappable(args->pid, ret);
		break;

	case MEMORYSTATUS_CMD_CONVERT_MEMLIMIT_MB:
		error = memorystatus_cmd_convert_memlimit_mb(args->pid, (int32_t) args->flags, ret);
		break;

	case MEMORYSTATUS_CMD_REARM_MEMLIMIT:
		error = memorystatus_cmd_rearm_memlimit(args->pid, args->flags, ret);
		break;
#endif /* CONFIG_JETSAM */
		/* Test commands */
#if DEVELOPMENT || DEBUG
	case MEMORYSTATUS_CMD_TEST_JETSAM:
		jetsam_reason = os_reason_create(OS_REASON_JETSAM, JETSAM_REASON_GENERIC);
		if (jetsam_reason == OS_REASON_NULL) {
			memorystatus_log_error("memorystatus_control: failed to allocate jetsam reason\n");
		}

		error = memstat_kill_process_sync(args->pid, kMemorystatusKilled, jetsam_reason) ? 0 : EINVAL;
		break;
	case MEMORYSTATUS_CMD_TEST_JETSAM_SORT:
		error = memorystatus_cmd_test_jetsam_sort(args->pid, (int32_t)args->flags, args->buffer, args->buffersize);
		break;
#else /* DEVELOPMENT || DEBUG */
	#pragma unused(jetsam_reason)
#endif /* DEVELOPMENT || DEBUG */
	case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_ENABLE:
		if (memorystatus_aggressive_jetsam_lenient_allowed == FALSE) {
#if DEVELOPMENT || DEBUG
			memorystatus_log_info("Enabling Lenient Mode\n");
#endif /* DEVELOPMENT || DEBUG */

			memorystatus_aggressive_jetsam_lenient_allowed = TRUE;
			memorystatus_aggressive_jetsam_lenient = TRUE;
			error = 0;
		}
		break;
	case MEMORYSTATUS_CMD_AGGRESSIVE_JETSAM_LENIENT_MODE_DISABLE:
#if DEVELOPMENT || DEBUG
		memorystatus_log_info("Disabling Lenient mode\n");
#endif /* DEVELOPMENT || DEBUG */
		memorystatus_aggressive_jetsam_lenient_allowed = FALSE;
		memorystatus_aggressive_jetsam_lenient = FALSE;
		error = 0;
		break;
	case MEMORYSTATUS_CMD_GET_AGGRESSIVE_JETSAM_LENIENT_MODE:
		*ret = (memorystatus_aggressive_jetsam_lenient ? 1 : 0);
		error = 0;
		break;
	case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_ENABLE:
	case MEMORYSTATUS_CMD_PRIVILEGED_LISTENER_DISABLE:
		error = memorystatus_low_mem_privileged_listener(args->command);
		break;

	case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_ENABLE:
	case MEMORYSTATUS_CMD_ELEVATED_INACTIVEJETSAMPRIORITY_DISABLE:
		error = memorystatus_update_inactive_jetsam_priority_band(args->pid, args->command, JETSAM_PRIORITY_ELEVATED_INACTIVE, args->flags ? TRUE : FALSE);
		break;
	case MEMORYSTATUS_CMD_SET_PROCESS_IS_MANAGED:
		error = memorystatus_set_process_is_managed(args->pid, args->flags);
		break;

	case MEMORYSTATUS_CMD_GET_PROCESS_IS_MANAGED:
		error = memorystatus_get_process_is_managed(args->pid, ret);
		break;

#if CONFIG_FREEZE
	case MEMORYSTATUS_CMD_SET_PROCESS_IS_FREEZABLE:
		error = memorystatus_set_process_is_freezable(args->pid, args->flags ? TRUE : FALSE);
		break;

	case MEMORYSTATUS_CMD_GET_PROCESS_IS_FREEZABLE:
		error = memorystatus_get_process_is_freezable(args->pid, ret);
		break;
	case MEMORYSTATUS_CMD_GET_PROCESS_IS_FROZEN:
		error = memorystatus_get_process_is_frozen(args->pid, ret);
		break;

	case MEMORYSTATUS_CMD_FREEZER_CONTROL:
		error = memorystatus_freezer_control(args->flags, args->buffer, args->buffersize, ret);
		break;
#endif /* CONFIG_FREEZE */

#if DEVELOPMENT || DEBUG
	case MEMORYSTATUS_CMD_INCREASE_JETSAM_TASK_LIMIT:
		error = memorystatus_cmd_increase_jetsam_task_limit(args->pid, args->flags);
		break;
	case MEMORYSTATUS_CMD_SET_DIAG_LIMIT:
		error = memorystatus_cmd_set_diag_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
		break;
	case MEMORYSTATUS_CMD_GET_DIAG_LIMIT:
		error = memorystatus_cmd_get_diag_memlimit_properties(args->pid, args->buffer, args->buffersize, ret);
		break;
#endif /* DEVELOPMENT || DEBUG */

	case MEMORYSTATUS_CMD_GET_KILL_COUNTS:
		error = memorystatus_cmd_get_kill_counts(args->pid, args->buffer, args->buffersize, args->flags);
		break;

	case MEMORYSTATUS_CMD_GET_CONCLAVE_LIMIT:
		error = _memstat_get_process_conclave_mem_limit(args->pid, ret);
		break;

	default:
		error = EINVAL;
		break;
	}

out:
	return error;
}

/* Coalition support */

/*
 * Inserts a list of pids before the given proc in the bucket. If any of the
 * pids in the given list are not already in the bucket, they will be ignored.
 */
static void
memstat_insert_list_locked(
	proc_t before,
	unsigned int bucket_idx,
	pid_t *pid_list,
	int list_sz)
{
	int i;
	proc_t p;
	memstat_bucket_t *bucket;

	assert(bucket_idx < MEMSTAT_BUCKET_COUNT);

	bucket = &memstat_bucket[bucket_idx];

	if ((pid_list == NULL) || (list_sz <= 0)) {
		return;
	}

	for (i = list_sz - 1; i >= 0; i--) {
		p = proc_find_locked(pid_list[i]);

		if (p == NULL) {
			continue;
		}

		if ((p == before) || (p->p_memstat_effectivepriority != bucket_idx)) {
			/*
			 * We can encounter p == before when we try to sort a coalition with an in-
			 * progress exec of the leader, such that the leader and the exec-ing
			 * member have the same PID. Just skip over it for now, since this member
			 * will soon be removed from the proc list anyway.
			 */
			proc_rele(p);
			continue;
		}

		TAILQ_REMOVE(&bucket->list, p, p_memstat_list);
		TAILQ_INSERT_BEFORE(before, p, p_memstat_list);
		proc_rele(p);
	}
}
/*
 * Return the number of pids rearranged during this sort.
 */
static void
memstat_sort_coals_locked(unsigned int bucket_index, memorystatus_jetsam_sort_order_t sort_order)
{
#define MAX_SORT_PIDS           80

	int ntasks = 0;
	proc_t p = NULL;
	coalition_t coal = COALITION_NULL;
	pid_t pid_list[MAX_SORT_PIDS];
	memstat_bucket_t *bucket;

	assert((sort_order == JETSAM_SORT_LRU) || (sort_order == JETSAM_SORT_FOOTPRINT));
	assert(bucket_index < MEMSTAT_BUCKET_COUNT);

	switch (sort_order) {
	case JETSAM_SORT_LRU:
		/* Nothing to do, buckets are already LRU */
		break;
	case JETSAM_SORT_FOOTPRINT:
		/* Sort bucket by footprint first */
		memstat_sort_by_footprint_locked(bucket_index);
		break;
	default:
		panic("Invalid sort order %d passed to memstat_sort_coals", sort_order);
	}

	/*
	 * During coalition sorting, processes in a priority band are rearranged
	 * by being re-inserted at the head of the queue.  So, when handling a
	 * list, the first process that gets moved to the head of the queue,
	 * ultimately gets pushed toward the queue tail, and hence, jetsams last.
	 *
	 * So, for example, the coalition leader is expected to jetsam last,
	 * after its coalition members.  Therefore, the coalition leader is
	 * inserted at the head of the queue first.
	 *
	 * After processing a coalition, the jetsam order is as follows:
	 *   undefs(jetsam first), extensions, xpc services, leader(jetsam last)
	 */

	/*
	 * Coalition members are rearranged in the priority bucket here,
	 * based on their coalition role.
	 */

	bucket = &memstat_bucket[bucket_index];
	p = TAILQ_FIRST(&bucket->list);
	while (p) {
		coal = task_get_coalition(proc_task(p), COALITION_TYPE_JETSAM);
		if (!coalition_is_leader(proc_task(p), coal)) {
			p = TAILQ_NEXT(p, p_memstat_list);
			continue;
		}

		/* undefined coalition members should be the first to jetsam */
		ntasks = coalition_get_pid_list(coal, COALITION_ROLEMASK_UNDEF,
		    COALITION_SORT_DEFAULT, pid_list, MAX_SORT_PIDS);

		if (ntasks > 0) {
			memstat_insert_list_locked(p, bucket_index, pid_list,
			    (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
		}

		/* extensions should jetsam after unmarked processes */
		ntasks = coalition_get_pid_list(coal, COALITION_ROLEMASK_EXT,
		    COALITION_SORT_DEFAULT, pid_list, MAX_SORT_PIDS);

		if (ntasks > 0) {
			memstat_insert_list_locked(p, bucket_index, pid_list,
			    (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
		}

		/* xpc services should jetsam after extensions */
		ntasks = coalition_get_pid_list(coal, COALITION_ROLEMASK_XPC,
		    COALITION_SORT_DEFAULT, pid_list, MAX_SORT_PIDS);

		if (ntasks > 0) {
			memstat_insert_list_locked(p, bucket_index, pid_list,
			    (ntasks <= MAX_SORT_PIDS ? ntasks : MAX_SORT_PIDS));
		}

		/*
		 * And then, the leader will jetsam last since we inserted everyone else
		 * before it in the bucket
		 */

		p = TAILQ_NEXT(p, p_memstat_list);
	} /* end for */
}



uint32_t
memstat_get_idle_proccnt(void)
{
	uint32_t count = 0;
	uint32_t bucket = JETSAM_PRIORITY_IDLE;

	proc_list_lock();
	for (proc_t p = memorystatus_get_first_proc_locked(&bucket, FALSE);
	    p != PROC_NULL;
	    p = memorystatus_get_next_proc_locked(&bucket, p, FALSE)) {
#if CONFIG_JETSAM
		if (!_memstat_proc_has_error(p)) {
			/*
			 * If termination hit an error on this jetsam iteration, don't consider the
			 * process idle. We'll clear the error bit before going to sleep and have
			 * another chance at killing it when we get re-awoken.
			 */
			count++;
		}
#else /* !CONFIG_JETSAM */
		/*
		 * On macOS, we can only exit clean daemons. In the future, we
		 * should include assertion-less managed daemons. Apps may make
		 * their way into this band as well, and we cannot jetsam those.
		 */
		if (_memstat_proc_can_idle_exit(p) &&
		    !_memstat_proc_is_dirty(p) &&
		    !_memstat_proc_is_terminating(p)) {
			count++;
		}
#endif /* CONFIG_JETSAM */
	}
	proc_list_unlock();

	return count;
}

uint32_t
memstat_get_long_idle_proccnt(void)
{
	uint32_t count = 0;
	uint32_t bucket = JETSAM_PRIORITY_IDLE;

	proc_list_lock();
	for (proc_t p = memorystatus_get_first_proc_locked(&bucket, FALSE);
	    p != PROC_NULL;
	    p = memorystatus_get_next_proc_locked(&bucket, p, FALSE)) {
		if (!_memstat_proc_is_dirty(p) && _memstat_proc_can_idle_exit(p) &&
		    !_memstat_proc_is_terminating(p) && _memstat_proc_is_reapable(p)) {
			count++;
		}
	}
	proc_list_unlock();

	return count;
}

uint32_t
memstat_get_proccnt_upto_priority(uint32_t max_bucket_index)
{
	int32_t i = JETSAM_PRIORITY_IDLE;
	int count = 0;

	assert3u(max_bucket_index, <=, MEMSTAT_BUCKET_COUNT);

	while (i <= max_bucket_index) {
		/*
		 * NB: We don't hold the proc-list lock here; that's ok b/c this is just an
		 * estimate.
		 */
		count += os_atomic_load(&memstat_bucket[i++].count, relaxed);
	}

	return count;
}

int
memorystatus_update_priority_for_appnap(proc_t p)
{
#if !CONFIG_JETSAM
	if (!p || (!isApp(p)) || (p->p_memstat_state & (P_MEMSTAT_INTERNAL | P_MEMSTAT_MANAGED))) {
		/*
		 * Ineligible processes OR system processes e.g. launchd.
		 */
		return -1;
	}

	int32_t priority = 0;

	proc_list_lock();

	if (proc_list_exited(p) ||
	    (p->p_memstat_state & (P_MEMSTAT_ERROR | P_MEMSTAT_TERMINATED | P_MEMSTAT_SKIP))) {
		/*
		 * If the process is on its way out OR
		 * jetsam has alread tried and failed to kill this process,
		 * let's skip the whole jetsam band transition.
		 */
		proc_list_unlock();
		return 0;
	}

	/*
	 * Update priority. We don't want the aging logic because that's only applicable on
	 * configs with CONFIG_JETSAM.
	 */
	priority = proc_get_effective_task_policy(proc_task(p), TASK_POLICY_SUP_ACTIVE) ?
	    JETSAM_PRIORITY_BACKGROUND :
	    p->p_memstat_requestedpriority;
	if (_memstat_proc_has_priority_assertion(p)) {
		priority = MAX(priority, p->p_memstat_assertionpriority);
	}
	memstat_update_priority_locked(p, priority, MEMSTAT_PRIORITY_NO_AGING);

	proc_list_unlock();

	return 0;

#else /* !CONFIG_JETSAM */
	#pragma unused(p)
	return -1;
#endif /* !CONFIG_JETSAM */
}

uint64_t
memorystatus_available_memory_internal(struct proc *p)
{
#ifdef XNU_TARGET_OS_OSX
	if (p->p_memstat_memlimit <= 0) {
		return 0;
	}
#endif /* XNU_TARGET_OS_OSX */
	const uint64_t footprint_in_bytes = get_task_phys_footprint(proc_task(p));
	int32_t memlimit_mb;
	int64_t memlimit_bytes;
	int64_t rc;

	if (isApp(p) == FALSE) {
		return 0;
	}

	if (p->p_memstat_memlimit > 0) {
		memlimit_mb = p->p_memstat_memlimit;
	} else if (task_convert_phys_footprint_limit(-1, &memlimit_mb) != KERN_SUCCESS) {
		return 0;
	}

	if (memlimit_mb <= 0) {
		memlimit_bytes = INT_MAX & ~((1 << 20) - 1);
	} else {
		memlimit_bytes = ((int64_t) memlimit_mb) << 20;
	}

	rc = memlimit_bytes - footprint_in_bytes;

	return (rc >= 0) ? rc : 0;
}

int
memorystatus_available_memory(struct proc *p, __unused struct memorystatus_available_memory_args *args, uint64_t *ret)
{
	*ret = memorystatus_available_memory_internal(p);

	return 0;
}

#if DEVELOPMENT || DEBUG
static int
memorystatus_cmd_increase_jetsam_task_limit(pid_t pid, uint32_t byte_increase)
{
	int32_t memlimit_active, memlimit_inactive;

	/* Validate inputs */
	if ((pid == 0) || (byte_increase == 0)) {
		return EINVAL;
	}

	if (memstat_ignore_task_limit_increase) {
		/* If the bootarg is set, lie and say we did it */
		return 0;
	}

	proc_t p = proc_find(pid);

	if (!p) {
		return ESRCH;
	}

	const uint32_t current_memlimit_increase = roundToNearestMB(p->p_memlimit_increase);
	/* round to page */
	const int32_t page_aligned_increase = (int32_t) MIN(round_page(p->p_memlimit_increase + byte_increase), INT32_MAX);

	proc_list_lock();

	memlimit_active = p->p_memstat_memlimit_active;
	if (memlimit_active > 0) {
		memlimit_active -= current_memlimit_increase;
		memlimit_active += roundToNearestMB(page_aligned_increase);
	}

	memlimit_inactive = p->p_memstat_memlimit_inactive;
	if (memlimit_inactive > 0) {
		memlimit_inactive -= current_memlimit_increase;
		memlimit_inactive += roundToNearestMB(page_aligned_increase);
	}

	/*
	 * Store the updated delta limit in the proc.
	 */
	p->p_memlimit_increase = page_aligned_increase;

	memlimit_options_t memlimit_options = MEMLIMIT_OPTIONS_NONE;
	if (_memstat_proc_inactive_memlimit_is_fatal(p)) {
		memlimit_options |= MEMLIMIT_INACTIVE_FATAL;
	}
	if (_memstat_proc_active_memlimit_is_fatal(p)) {
		memlimit_options |= MEMLIMIT_ACTIVE_FATAL;
	}

	int error = memstat_set_memlimits_locked(p,
	    memlimit_active, memlimit_inactive,
	    memlimit_options);

	proc_list_unlock();
	proc_rele(p);

	return error;
}
#endif /* DEVELOPMENT */