summaryrefslogtreecommitdiff
path: root/sys/uvm/uvm_fault.c
blob: 72513cc9dacf08cea4f9f8838037e09bf6a8a1f7 (plain)
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
/*	$NetBSD: uvm_fault.c,v 1.232 2023/04/09 09:00:56 riastradh Exp $	*/

/*
 * Copyright (c) 1997 Charles D. Cranor and Washington University.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
 */

/*
 * uvm_fault.c: fault handler
 */

#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.232 2023/04/09 09:00:56 riastradh Exp $");

#include "opt_uvmhist.h"

#include <sys/param.h>
#include <sys/systm.h>
#include <sys/atomic.h>
#include <sys/kernel.h>
#include <sys/mman.h>

#include <uvm/uvm.h>
#include <uvm/uvm_pdpolicy.h>

/*
 *
 * a word on page faults:
 *
 * types of page faults we handle:
 *
 * CASE 1: upper layer faults                   CASE 2: lower layer faults
 *
 *    CASE 1A         CASE 1B                  CASE 2A        CASE 2B
 *    read/write1     write>1                  read/write   +-cow_write/zero
 *         |             |                         |        |
 *      +--|--+       +--|--+     +-----+       +  |  +     | +-----+
 * amap |  V  |       |  ---------> new |          |        | |  ^  |
 *      +-----+       +-----+     +-----+       +  |  +     | +--|--+
 *                                                 |        |    |
 *      +-----+       +-----+                   +--|--+     | +--|--+
 * uobj | d/c |       | d/c |                   |  V  |     +----+  |
 *      +-----+       +-----+                   +-----+       +-----+
 *
 * d/c = don't care
 *
 *   case [0]: layerless fault
 *	no amap or uobj is present.   this is an error.
 *
 *   case [1]: upper layer fault [anon active]
 *     1A: [read] or [write with anon->an_ref == 1]
 *		I/O takes place in upper level anon and uobj is not touched.
 *     1B: [write with anon->an_ref > 1]
 *		new anon is alloc'd and data is copied off ["COW"]
 *
 *   case [2]: lower layer fault [uobj]
 *     2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
 *		I/O takes place directly in object.
 *     2B: [write to copy_on_write] or [read on NULL uobj]
 *		data is "promoted" from uobj to a new anon.
 *		if uobj is null, then we zero fill.
 *
 * we follow the standard UVM locking protocol ordering:
 *
 * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
 * we hold a PG_BUSY page if we unlock for I/O
 *
 *
 * the code is structured as follows:
 *
 *     - init the "IN" params in the ufi structure
 *   ReFault: (ERESTART returned to the loop in uvm_fault_internal)
 *     - do lookups [locks maps], check protection, handle needs_copy
 *     - check for case 0 fault (error)
 *     - establish "range" of fault
 *     - if we have an amap lock it and extract the anons
 *     - if sequential advice deactivate pages behind us
 *     - at the same time check pmap for unmapped areas and anon for pages
 *	 that we could map in (and do map it if found)
 *     - check object for resident pages that we could map in
 *     - if (case 2) goto Case2
 *     - >>> handle case 1
 *           - ensure source anon is resident in RAM
 *           - if case 1B alloc new anon and copy from source
 *           - map the correct page in
 *   Case2:
 *     - >>> handle case 2
 *           - ensure source page is resident (if uobj)
 *           - if case 2B alloc new anon and copy from source (could be zero
 *		fill if uobj == NULL)
 *           - map the correct page in
 *     - done!
 *
 * note on paging:
 *   if we have to do I/O we place a PG_BUSY page in the correct object,
 * unlock everything, and do the I/O.   when I/O is done we must reverify
 * the state of the world before assuming that our data structures are
 * valid.   [because mappings could change while the map is unlocked]
 *
 *  alternative 1: unbusy the page in question and restart the page fault
 *    from the top (ReFault).   this is easy but does not take advantage
 *    of the information that we already have from our previous lookup,
 *    although it is possible that the "hints" in the vm_map will help here.
 *
 * alternative 2: the system already keeps track of a "version" number of
 *    a map.   [i.e. every time you write-lock a map (e.g. to change a
 *    mapping) you bump the version number up by one...]   so, we can save
 *    the version number of the map before we release the lock and start I/O.
 *    then when I/O is done we can relock and check the version numbers
 *    to see if anything changed.    this might save us some over 1 because
 *    we don't have to unbusy the page and may be less compares(?).
 *
 * alternative 3: put in backpointers or a way to "hold" part of a map
 *    in place while I/O is in progress.   this could be complex to
 *    implement (especially with structures like amap that can be referenced
 *    by multiple map entries, and figuring out what should wait could be
 *    complex as well...).
 *
 * we use alternative 2.  given that we are multi-threaded now we may want
 * to reconsider the choice.
 */

/*
 * local data structures
 */

struct uvm_advice {
	int advice;
	int nback;
	int nforw;
};

/*
 * page range array:
 * note: index in array must match "advice" value
 * XXX: borrowed numbers from freebsd.   do they work well for us?
 */

static const struct uvm_advice uvmadvice[] = {
	{ UVM_ADV_NORMAL, 3, 4 },
	{ UVM_ADV_RANDOM, 0, 0 },
	{ UVM_ADV_SEQUENTIAL, 8, 7},
};

#define UVM_MAXRANGE 16	/* must be MAX() of nback+nforw+1 */

/*
 * private prototypes
 */

/*
 * inline functions
 */

/*
 * uvmfault_anonflush: try and deactivate pages in specified anons
 *
 * => does not have to deactivate page if it is busy
 */

static inline void
uvmfault_anonflush(struct vm_anon **anons, int n)
{
	int lcv;
	struct vm_page *pg;

	for (lcv = 0; lcv < n; lcv++) {
		if (anons[lcv] == NULL)
			continue;
		KASSERT(rw_lock_held(anons[lcv]->an_lock));
		pg = anons[lcv]->an_page;
		if (pg && (pg->flags & PG_BUSY) == 0) {
			uvm_pagelock(pg);
			uvm_pagedeactivate(pg);
			uvm_pageunlock(pg);
		}
	}
}

/*
 * normal functions
 */

/*
 * uvmfault_amapcopy: clear "needs_copy" in a map.
 *
 * => called with VM data structures unlocked (usually, see below)
 * => we get a write lock on the maps and clear needs_copy for a VA
 * => if we are out of RAM we sleep (waiting for more)
 */

static void
uvmfault_amapcopy(struct uvm_faultinfo *ufi)
{
	for (;;) {

		/*
		 * no mapping?  give up.
		 */

		if (uvmfault_lookup(ufi, true) == false)
			return;

		/*
		 * copy if needed.
		 */

		if (UVM_ET_ISNEEDSCOPY(ufi->entry))
			amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
				ufi->orig_rvaddr, ufi->orig_rvaddr + 1);

		/*
		 * didn't work?  must be out of RAM.   unlock and sleep.
		 */

		if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
			uvmfault_unlockmaps(ufi, true);
			uvm_wait("fltamapcopy");
			continue;
		}

		/*
		 * got it!   unlock and return.
		 */

		uvmfault_unlockmaps(ufi, true);
		return;
	}
	/*NOTREACHED*/
}

/*
 * uvmfault_anonget: get data in an anon into a non-busy, non-released
 * page in that anon.
 *
 * => Map, amap and thus anon should be locked by caller.
 * => If we fail, we unlock everything and error is returned.
 * => If we are successful, return with everything still locked.
 * => We do not move the page on the queues [gets moved later].  If we
 *    allocate a new page [we_own], it gets put on the queues.  Either way,
 *    the result is that the page is on the queues at return time
 * => For pages which are on loan from a uvm_object (and thus are not owned
 *    by the anon): if successful, return with the owning object locked.
 *    The caller must unlock this object when it unlocks everything else.
 */

int
uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
    struct vm_anon *anon)
{
	struct vm_page *pg;
	krw_t lock_type;
	int error;

	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
	KASSERT(rw_lock_held(anon->an_lock));
	KASSERT(anon->an_lock == amap->am_lock);

	/* Increment the counters.*/
	cpu_count(CPU_COUNT_FLTANGET, 1);
	if (anon->an_page) {
		curlwp->l_ru.ru_minflt++;
	} else {
		curlwp->l_ru.ru_majflt++;
	}
	error = 0;

	/*
	 * Loop until we get the anon data, or fail.
	 */

	for (;;) {
		bool we_own, locked;
		/*
		 * Note: 'we_own' will become true if we set PG_BUSY on a page.
		 */
		we_own = false;
		pg = anon->an_page;

		/*
		 * If there is a resident page and it is loaned, then anon
		 * may not own it.  Call out to uvm_anon_lockloanpg() to
		 * identify and lock the real owner of the page.
		 */

		if (pg && pg->loan_count)
			pg = uvm_anon_lockloanpg(anon);

		/*
		 * Is page resident?  Make sure it is not busy/released.
		 */

		lock_type = rw_lock_op(anon->an_lock);
		if (pg) {

			/*
			 * at this point, if the page has a uobject [meaning
			 * we have it on loan], then that uobject is locked
			 * by us!   if the page is busy, we drop all the
			 * locks (including uobject) and try again.
			 */

			if ((pg->flags & PG_BUSY) == 0) {
				UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
				return 0;
			}
			cpu_count(CPU_COUNT_FLTPGWAIT, 1);

			/*
			 * The last unlock must be an atomic unlock and wait
			 * on the owner of page.
			 */

			if (pg->uobject) {
				/* Owner of page is UVM object. */
				uvmfault_unlockall(ufi, amap, NULL);
				UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
				    0,0,0);
				uvm_pagewait(pg, pg->uobject->vmobjlock, "anonget1");
			} else {
				/* Owner of page is anon. */
				uvmfault_unlockall(ufi, NULL, NULL);
				UVMHIST_LOG(maphist, " unlock+wait on anon",0,
				    0,0,0);
				uvm_pagewait(pg, anon->an_lock, "anonget2");
			}
		} else {
#if defined(VMSWAP)
			/*
			 * No page, therefore allocate one.  A write lock is
			 * required for this.  If the caller didn't supply
			 * one, fail now and have them retry.
			 */

			if (lock_type == RW_READER) {
				return ENOLCK;
			}
			pg = uvm_pagealloc(NULL,
			    ufi != NULL ? ufi->orig_rvaddr : 0,
			    anon, ufi != NULL ? UVM_FLAG_COLORMATCH : 0);
			if (pg == NULL) {
				/* Out of memory.  Wait a little. */
				uvmfault_unlockall(ufi, amap, NULL);
				cpu_count(CPU_COUNT_FLTNORAM, 1);
				UVMHIST_LOG(maphist, "  noram -- UVM_WAIT",0,
				    0,0,0);
				if (!uvm_reclaimable()) {
					return ENOMEM;
				}
				uvm_wait("flt_noram1");
			} else {
				/* PG_BUSY bit is set. */
				we_own = true;
				uvmfault_unlockall(ufi, amap, NULL);

				/*
				 * Pass a PG_BUSY+PG_FAKE clean page into
				 * the uvm_swap_get() function with all data
				 * structures unlocked.  Note that it is OK
				 * to read an_swslot here, because we hold
				 * PG_BUSY on the page.
				 */
				cpu_count(CPU_COUNT_PAGEINS, 1);
				error = uvm_swap_get(pg, anon->an_swslot,
				    PGO_SYNCIO);

				/*
				 * We clean up after the I/O below in the
				 * 'we_own' case.
				 */
			}
#else
			panic("%s: no page", __func__);
#endif /* defined(VMSWAP) */
		}

		/*
		 * Re-lock the map and anon.
		 */

		locked = uvmfault_relock(ufi);
		if (locked || we_own) {
			rw_enter(anon->an_lock, lock_type);
		}

		/*
		 * If we own the page (i.e. we set PG_BUSY), then we need
		 * to clean up after the I/O.  There are three cases to
		 * consider:
		 *
		 * 1) Page was released during I/O: free anon and ReFault.
		 * 2) I/O not OK.  Free the page and cause the fault to fail.
		 * 3) I/O OK!  Activate the page and sync with the non-we_own
		 *    case (i.e. drop anon lock if not locked).
		 */

		if (we_own) {
			KASSERT(lock_type == RW_WRITER);
#if defined(VMSWAP)
			if (error) {

				/*
				 * Remove the swap slot from the anon and
				 * mark the anon as having no real slot.
				 * Do not free the swap slot, thus preventing
				 * it from being used again.
				 */

				if (anon->an_swslot > 0) {
					uvm_swap_markbad(anon->an_swslot, 1);
				}
				anon->an_swslot = SWSLOT_BAD;

				if ((pg->flags & PG_RELEASED) != 0) {
					goto released;
				}

				/*
				 * Note: page was never !PG_BUSY, so it
				 * cannot be mapped and thus no need to
				 * pmap_page_protect() it.
				 */

				uvm_pagefree(pg);

				if (locked) {
					uvmfault_unlockall(ufi, NULL, NULL);
				}
				rw_exit(anon->an_lock);
				UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
				return error;
			}

			if ((pg->flags & PG_RELEASED) != 0) {
released:
				KASSERT(anon->an_ref == 0);

				/*
				 * Released while we had unlocked amap.
				 */

				if (locked) {
					uvmfault_unlockall(ufi, NULL, NULL);
				}
				uvm_anon_release(anon);

				if (error) {
					UVMHIST_LOG(maphist,
					    "<- ERROR/RELEASED", 0,0,0,0);
					return error;
				}

				UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
				return ERESTART;
			}

			/*
			 * We have successfully read the page, activate it.
			 */

			uvm_pagelock(pg);
			uvm_pageactivate(pg);
			uvm_pagewakeup(pg);
			uvm_pageunlock(pg);
			pg->flags &= ~(PG_BUSY|PG_FAKE);
			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
			UVM_PAGE_OWN(pg, NULL);
#else
			panic("%s: we_own", __func__);
#endif /* defined(VMSWAP) */
		}

		/*
		 * We were not able to re-lock the map - restart the fault.
		 */

		if (!locked) {
			if (we_own) {
				rw_exit(anon->an_lock);
			}
			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
			return ERESTART;
		}

		/*
		 * Verify that no one has touched the amap and moved
		 * the anon on us.
		 */

		if (ufi != NULL && amap_lookup(&ufi->entry->aref,
		    ufi->orig_rvaddr - ufi->entry->start) != anon) {

			uvmfault_unlockall(ufi, amap, NULL);
			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
			return ERESTART;
		}

		/*
		 * Retry..
		 */

		cpu_count(CPU_COUNT_FLTANRETRY, 1);
		continue;
	}
	/*NOTREACHED*/
}

/*
 * uvmfault_promote: promote data to a new anon.  used for 1B and 2B.
 *
 *	1. allocate an anon and a page.
 *	2. fill its contents.
 *	3. put it into amap.
 *
 * => if we fail (result != 0) we unlock everything.
 * => on success, return a new locked anon via 'nanon'.
 *    (*nanon)->an_page will be a resident, locked, dirty page.
 * => it's caller's responsibility to put the promoted nanon->an_page to the
 *    page queue.
 */

static int
uvmfault_promote(struct uvm_faultinfo *ufi,
    struct vm_anon *oanon,
    struct vm_page *uobjpage,
    struct vm_anon **nanon, /* OUT: allocated anon */
    struct vm_anon **spare)
{
	struct vm_amap *amap = ufi->entry->aref.ar_amap;
	struct uvm_object *uobj;
	struct vm_anon *anon;
	struct vm_page *pg;
	struct vm_page *opg;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	if (oanon) {
		/* anon COW */
		opg = oanon->an_page;
		KASSERT(opg != NULL);
		KASSERT(opg->uobject == NULL || opg->loan_count > 0);
	} else if (uobjpage != PGO_DONTCARE) {
		/* object-backed COW */
		opg = uobjpage;
		KASSERT(rw_lock_held(opg->uobject->vmobjlock));
	} else {
		/* ZFOD */
		opg = NULL;
	}
	if (opg != NULL) {
		uobj = opg->uobject;
	} else {
		uobj = NULL;
	}

	KASSERT(amap != NULL);
	KASSERT(uobjpage != NULL);
	KASSERT(rw_write_held(amap->am_lock));
	KASSERT(oanon == NULL || amap->am_lock == oanon->an_lock);
	KASSERT(uobj == NULL || rw_lock_held(uobj->vmobjlock));

	if (*spare != NULL) {
		anon = *spare;
		*spare = NULL;
	} else {
		anon = uvm_analloc();
	}
	if (anon) {

		/*
		 * The new anon is locked.
		 *
		 * if opg == NULL, we want a zero'd, dirty page,
		 * so have uvm_pagealloc() do that for us.
		 */

		KASSERT(anon->an_lock == NULL);
		anon->an_lock = amap->am_lock;
		pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon,
		    UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0));
		if (pg == NULL) {
			anon->an_lock = NULL;
		}
	} else {
		pg = NULL;
	}

	/*
	 * out of memory resources?
	 */

	if (pg == NULL) {
		/* save anon for the next try. */
		if (anon != NULL) {
			*spare = anon;
		}

		/* unlock and fail ... */
		uvmfault_unlockall(ufi, amap, uobj);
		if (!uvm_reclaimable()) {
			UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
			cpu_count(CPU_COUNT_FLTNOANON, 1);
			error = ENOMEM;
			goto done;
		}

		UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
		cpu_count(CPU_COUNT_FLTNORAM, 1);
		uvm_wait("flt_noram5");
		error = ERESTART;
		goto done;
	}

	/* copy page [pg now dirty] */
	if (opg) {
		uvm_pagecopy(opg, pg);
	}
	KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);

	amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon,
	    oanon != NULL);

	/*
	 * from this point on am_lock won't be dropped until the page is
	 * entered, so it's safe to unbusy the page up front.
	 *
	 * uvm_fault_{upper,lower}_done will activate or enqueue the page.
	 */

	pg = anon->an_page;
	pg->flags &= ~(PG_BUSY|PG_FAKE);
	UVM_PAGE_OWN(pg, NULL);

	*nanon = anon;
	error = 0;
done:
	return error;
}

/*
 * Update statistics after fault resolution.
 * - maxrss
 */
void
uvmfault_update_stats(struct uvm_faultinfo *ufi)
{
	struct vm_map		*map;
	struct vmspace 		*vm;
	struct proc		*p;
	vsize_t			 res;

	map = ufi->orig_map;

	p = curproc;
	KASSERT(p != NULL);
	vm = p->p_vmspace;

	if (&vm->vm_map != map)
		return;

	res = pmap_resident_count(map->pmap);
	if (vm->vm_rssmax < res)
		vm->vm_rssmax = res;
}

/*
 *   F A U L T   -   m a i n   e n t r y   p o i n t
 */

/*
 * uvm_fault: page fault handler
 *
 * => called from MD code to resolve a page fault
 * => VM data structures usually should be unlocked.   however, it is
 *	possible to call here with the main map locked if the caller
 *	gets a write lock, sets it recursive, and then calls us (c.f.
 *	uvm_map_pageable).   this should be avoided because it keeps
 *	the map locked off during I/O.
 * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
 */

#define MASK(entry)     (UVM_ET_ISCOPYONWRITE(entry) ? \
			 ~VM_PROT_WRITE : VM_PROT_ALL)

/* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
#define UVM_FAULT_WIRE		(1 << 0)
#define UVM_FAULT_MAXPROT	(1 << 1)

struct uvm_faultctx {

	/*
	 * the following members are set up by uvm_fault_check() and
	 * read-only after that.
	 *
	 * note that narrow is used by uvm_fault_check() to change
	 * the behaviour after ERESTART.
	 *
	 * most of them might change after RESTART if the underlying
	 * map entry has been changed behind us.  an exception is
	 * wire_paging, which does never change.
	 */
	vm_prot_t access_type;
	vaddr_t startva;
	int npages;
	int centeridx;
	bool narrow;		/* work on a single requested page only */
	bool wire_mapping;	/* request a PMAP_WIRED mapping
				   (UVM_FAULT_WIRE or VM_MAPENT_ISWIRED) */
	bool wire_paging;	/* request uvm_pagewire
				   (true for UVM_FAULT_WIRE) */
	bool cow_now;		/* VM_PROT_WRITE is actually requested
				   (ie. should break COW and page loaning) */

	/*
	 * enter_prot is set up by uvm_fault_check() and clamped
	 * (ie. drop the VM_PROT_WRITE bit) in various places in case
	 * of !cow_now.
	 */
	vm_prot_t enter_prot;	/* prot at which we want to enter pages in */

	/*
	 * the following member is for uvmfault_promote() and ERESTART.
	 */
	struct vm_anon *anon_spare;

	/*
	 * the following is actually a uvm_fault_lower() internal.
	 * it's here merely for debugging.
	 * (or due to the mechanical separation of the function?)
	 */
	bool promote;

	/*
	 * type of lock to acquire on objects in both layers.
	 */
	krw_t lower_lock_type;
	krw_t upper_lock_type;
};

static inline int	uvm_fault_check(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct vm_anon ***, bool);

static int		uvm_fault_upper(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct vm_anon **);
static inline int	uvm_fault_upper_lookup(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct vm_anon **, struct vm_page **);
static inline void	uvm_fault_upper_neighbor(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    vaddr_t, struct vm_page *, bool);
static inline int	uvm_fault_upper_loan(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct vm_anon *, struct uvm_object **);
static inline int	uvm_fault_upper_promote(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object *, struct vm_anon *);
static inline int	uvm_fault_upper_direct(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object *, struct vm_anon *);
static int		uvm_fault_upper_enter(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct uvm_object *, struct vm_anon *,
			    struct vm_page *, struct vm_anon *);
static inline void	uvm_fault_upper_done(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct vm_anon *, struct vm_page *);

static int		uvm_fault_lower(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct vm_page **);
static inline void	uvm_fault_lower_lookup(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct vm_page **);
static inline void	uvm_fault_lower_neighbor(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    vaddr_t, struct vm_page *);
static inline int	uvm_fault_lower_io(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object **, struct vm_page **);
static inline int	uvm_fault_lower_direct(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object *, struct vm_page *);
static inline int	uvm_fault_lower_direct_loan(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object *, struct vm_page **,
			    struct vm_page **);
static inline int	uvm_fault_lower_promote(
			    struct uvm_faultinfo *, struct uvm_faultctx *,
			    struct uvm_object *, struct vm_page *);
static int		uvm_fault_lower_enter(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct uvm_object *,
			    struct vm_anon *, struct vm_page *);
static inline void	uvm_fault_lower_done(
			    struct uvm_faultinfo *, const struct uvm_faultctx *,
			    struct uvm_object *, struct vm_page *);

int
uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
    vm_prot_t access_type, int fault_flag)
{
	struct uvm_faultinfo ufi;
	struct uvm_faultctx flt = {
		.access_type = access_type,

		/* don't look for neighborhood * pages on "wire" fault */
		.narrow = (fault_flag & UVM_FAULT_WIRE) != 0,

		/* "wire" fault causes wiring of both mapping and paging */
		.wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0,
		.wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0,

		/*
		 * default lock type to acquire on upper & lower layer
		 * objects: reader.  this can be upgraded at any point
		 * during the fault from read -> write and uvm_faultctx
		 * changed to match, but is never downgraded write -> read.
		 */
#ifdef __HAVE_UNLOCKED_PMAP /* XXX temporary */
		.upper_lock_type = RW_WRITER,
		.lower_lock_type = RW_WRITER,
#else
		.upper_lock_type = RW_READER,
		.lower_lock_type = RW_READER,
#endif
	};
	const bool maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0;
	struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
	struct vm_page *pages_store[UVM_MAXRANGE], **pages;
	int error;

	UVMHIST_FUNC(__func__);
	UVMHIST_CALLARGS(maphist, "(map=%#jx, vaddr=%#jx, at=%jd, ff=%jd)",
	      (uintptr_t)orig_map, vaddr, access_type, fault_flag);

	/* Don't count anything until user interaction is possible */
	kpreempt_disable();
	if (__predict_true(start_init_exec)) {
		struct cpu_info *ci = curcpu();
		CPU_COUNT(CPU_COUNT_NFAULT, 1);
		/* Don't flood RNG subsystem with samples. */
		if (++(ci->ci_faultrng) == 503) {
			ci->ci_faultrng = 0;
			rnd_add_uint32(&curcpu()->ci_data.cpu_uvm->rs,
			    sizeof(vaddr_t) == sizeof(uint32_t) ?
			    (uint32_t)vaddr : sizeof(vaddr_t) ==
			    sizeof(uint64_t) ?
			    (uint32_t)vaddr :
			    (uint32_t)ci->ci_counts[CPU_COUNT_NFAULT]);
		}
	}
	kpreempt_enable();

	/*
	 * init the IN parameters in the ufi
	 */

	ufi.orig_map = orig_map;
	ufi.orig_rvaddr = trunc_page(vaddr);
	ufi.orig_size = PAGE_SIZE;	/* can't get any smaller than this */

	error = ERESTART;
	while (error == ERESTART) { /* ReFault: */
		anons = anons_store;
		pages = pages_store;

		error = uvm_fault_check(&ufi, &flt, &anons, maxprot);
		if (error != 0)
			continue;

		error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
		if (error != 0)
			continue;

		if (pages[flt.centeridx] == PGO_DONTCARE)
			error = uvm_fault_upper(&ufi, &flt, anons);
		else {
			struct uvm_object * const uobj =
			    ufi.entry->object.uvm_obj;

			if (uobj && uobj->pgops->pgo_fault != NULL) {
				/*
				 * invoke "special" fault routine.
				 */
				rw_enter(uobj->vmobjlock, RW_WRITER);
				/* locked: maps(read), amap(if there), uobj */
				error = uobj->pgops->pgo_fault(&ufi,
				    flt.startva, pages, flt.npages,
				    flt.centeridx, flt.access_type,
				    PGO_LOCKED|PGO_SYNCIO);

				/*
				 * locked: nothing, pgo_fault has unlocked
				 * everything
				 */

				/*
				 * object fault routine responsible for
				 * pmap_update().
				 */

				/*
				 * Wake up the pagedaemon if the fault method
				 * failed for lack of memory but some can be
				 * reclaimed.
				 */
				if (error == ENOMEM && uvm_reclaimable()) {
					uvm_wait("pgo_fault");
					error = ERESTART;
				}
			} else {
				error = uvm_fault_lower(&ufi, &flt, pages);
			}
		}
	}

	if (flt.anon_spare != NULL) {
		flt.anon_spare->an_ref--;
		KASSERT(flt.anon_spare->an_ref == 0);
		KASSERT(flt.anon_spare->an_lock == NULL);
		uvm_anfree(flt.anon_spare);
	}
	return error;
}

/*
 * uvm_fault_check: check prot, handle needs-copy, etc.
 *
 *	1. lookup entry.
 *	2. check protection.
 *	3. adjust fault condition (mainly for simulated fault).
 *	4. handle needs-copy (lazy amap copy).
 *	5. establish range of interest for neighbor fault (aka pre-fault).
 *	6. look up anons (if amap exists).
 *	7. flush pages (if MADV_SEQUENTIAL)
 *
 * => called with nothing locked.
 * => if we fail (result != 0) we unlock everything.
 * => initialize/adjust many members of flt.
 */

static int
uvm_fault_check(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct vm_anon ***ranons, bool maxprot)
{
	struct vm_amap *amap;
	struct uvm_object *uobj;
	vm_prot_t check_prot;
	int nback, nforw;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/*
	 * lookup and lock the maps
	 */

	if (uvmfault_lookup(ufi, false) == false) {
		UVMHIST_LOG(maphist, "<- no mapping @ %#jx", ufi->orig_rvaddr,
		    0,0,0);
		return EFAULT;
	}
	/* locked: maps(read) */

#ifdef DIAGNOSTIC
	if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
		printf("Page fault on non-pageable map:\n");
		printf("ufi->map = %p\n", ufi->map);
		printf("ufi->orig_map = %p\n", ufi->orig_map);
		printf("ufi->orig_rvaddr = %#lx\n", (u_long) ufi->orig_rvaddr);
		panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
	}
#endif

	/*
	 * check protection
	 */

	check_prot = maxprot ?
	    ufi->entry->max_protection : ufi->entry->protection;
	if ((check_prot & flt->access_type) != flt->access_type) {
		UVMHIST_LOG(maphist,
		    "<- protection failure (prot=%#jx, access=%#jx)",
		    ufi->entry->protection, flt->access_type, 0, 0);
		uvmfault_unlockmaps(ufi, false);
		return EFAULT;
	}

	/*
	 * "enter_prot" is the protection we want to enter the page in at.
	 * for certain pages (e.g. copy-on-write pages) this protection can
	 * be more strict than ufi->entry->protection.  "wired" means either
	 * the entry is wired or we are fault-wiring the pg.
	 */

	flt->enter_prot = ufi->entry->protection;
	if (VM_MAPENT_ISWIRED(ufi->entry)) {
		flt->wire_mapping = true;
		flt->wire_paging = true;
		flt->narrow = true;
	}

	if (flt->wire_mapping) {
		flt->access_type = flt->enter_prot; /* full access for wired */
		flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
	} else {
		flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
	}

	if (flt->wire_paging) {
		/* wiring pages requires a write lock. */
		flt->upper_lock_type = RW_WRITER;
		flt->lower_lock_type = RW_WRITER;
	}

	flt->promote = false;

	/*
	 * handle "needs_copy" case.   if we need to copy the amap we will
	 * have to drop our readlock and relock it with a write lock.  (we
	 * need a write lock to change anything in a map entry [e.g.
	 * needs_copy]).
	 */

	if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
		if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
			KASSERT(!maxprot);
			/* need to clear */
			UVMHIST_LOG(maphist,
			    "  need to clear needs_copy and refault",0,0,0,0);
			uvmfault_unlockmaps(ufi, false);
			uvmfault_amapcopy(ufi);
			cpu_count(CPU_COUNT_FLTAMCOPY, 1);
			return ERESTART;

		} else {

			/*
			 * ensure that we pmap_enter page R/O since
			 * needs_copy is still true
			 */

			flt->enter_prot &= ~VM_PROT_WRITE;
		}
	}

	/*
	 * identify the players
	 */

	amap = ufi->entry->aref.ar_amap;	/* upper layer */
	uobj = ufi->entry->object.uvm_obj;	/* lower layer */

	/*
	 * check for a case 0 fault.  if nothing backing the entry then
	 * error now.
	 */

	if (amap == NULL && uobj == NULL) {
		uvmfault_unlockmaps(ufi, false);
		UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
		return EFAULT;
	}

	/*
	 * for a case 2B fault waste no time on adjacent pages because
	 * they are likely already entered.
	 */

	if (uobj != NULL && amap != NULL &&
	    (flt->access_type & VM_PROT_WRITE) != 0) {
		/* wide fault (!narrow) */
		flt->narrow = true;
	}

	/*
	 * establish range of interest based on advice from mapper
	 * and then clip to fit map entry.   note that we only want
	 * to do this the first time through the fault.   if we
	 * ReFault we will disable this by setting "narrow" to true.
	 */

	if (flt->narrow == false) {

		/* wide fault (!narrow) */
		KASSERT(uvmadvice[ufi->entry->advice].advice ==
			 ufi->entry->advice);
		nback = MIN(uvmadvice[ufi->entry->advice].nback,
		    (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
		flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
		/*
		 * note: "-1" because we don't want to count the
		 * faulting page as forw
		 */
		nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
			    ((ufi->entry->end - ufi->orig_rvaddr) >>
			     PAGE_SHIFT) - 1);
		flt->npages = nback + nforw + 1;
		flt->centeridx = nback;

		flt->narrow = true;	/* ensure only once per-fault */

	} else {

		/* narrow fault! */
		nback = nforw = 0;
		flt->startva = ufi->orig_rvaddr;
		flt->npages = 1;
		flt->centeridx = 0;

	}
	/* offset from entry's start to pgs' start */
	const voff_t eoff = flt->startva - ufi->entry->start;

	/* locked: maps(read) */
	UVMHIST_LOG(maphist, "  narrow=%jd, back=%jd, forw=%jd, startva=%#jx",
		    flt->narrow, nback, nforw, flt->startva);
	UVMHIST_LOG(maphist, "  entry=%#jx, amap=%#jx, obj=%#jx",
	    (uintptr_t)ufi->entry, (uintptr_t)amap, (uintptr_t)uobj, 0);

	/*
	 * guess at the most suitable lock types to acquire.
	 * if we've got an amap then lock it and extract current anons.
	 */

	if (amap) {
		if ((amap_flags(amap) & AMAP_SHARED) == 0) {
			/*
			 * the amap isn't shared.  get a writer lock to
			 * avoid the cost of upgrading the lock later if
			 * needed.
			 *
			 * XXX nice for PostgreSQL, but consider threads.
			 */
			flt->upper_lock_type = RW_WRITER;
		} else if ((flt->access_type & VM_PROT_WRITE) != 0) {
			/*
			 * assume we're about to COW.
			 */
			flt->upper_lock_type = RW_WRITER;
		}
		amap_lock(amap, flt->upper_lock_type);
		amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
	} else {
		if ((flt->access_type & VM_PROT_WRITE) != 0) {
			/*
			 * we are about to dirty the object and that
			 * requires a write lock.
			 */
			flt->lower_lock_type = RW_WRITER;
		}
		*ranons = NULL;	/* to be safe */
	}

	/* locked: maps(read), amap(if there) */
	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);

	/*
	 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
	 * now and then forget about them (for the rest of the fault).
	 */

	if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {

		UVMHIST_LOG(maphist, "  MADV_SEQUENTIAL: flushing backpages",
		    0,0,0,0);
		/* flush back-page anons? */
		if (amap)
			uvmfault_anonflush(*ranons, nback);

		/*
		 * flush object?  change lock type to RW_WRITER, to avoid
		 * excessive competition between read/write locks if many
		 * threads doing "sequential access".
		 */
		if (uobj) {
			voff_t uoff;

			flt->lower_lock_type = RW_WRITER;
			uoff = ufi->entry->offset + eoff;
			rw_enter(uobj->vmobjlock, RW_WRITER);
			(void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
				    (nback << PAGE_SHIFT), PGO_DEACTIVATE);
		}

		/* now forget about the backpages */
		if (amap)
			*ranons += nback;
		flt->startva += (nback << PAGE_SHIFT);
		flt->npages -= nback;
		flt->centeridx = 0;
	}
	/*
	 * => startva is fixed
	 * => npages is fixed
	 */
	KASSERT(flt->startva <= ufi->orig_rvaddr);
	KASSERT(ufi->orig_rvaddr + ufi->orig_size <=
	    flt->startva + (flt->npages << PAGE_SHIFT));
	return 0;
}

/*
 * uvm_fault_upper_upgrade: upgrade upper lock, reader -> writer
 */

static inline int
uvm_fault_upper_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
    struct vm_amap *amap, struct uvm_object *uobj)
{
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	KASSERT(amap != NULL);
	KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));

	/*
	 * fast path.
	 */

	if (__predict_true(flt->upper_lock_type == RW_WRITER)) {
		return 0;
	}

	/*
	 * otherwise try for the upgrade.  if we don't get it, unlock
	 * everything, restart the fault and next time around get a writer
	 * lock.
	 */

	flt->upper_lock_type = RW_WRITER;
	if (__predict_false(!rw_tryupgrade(amap->am_lock))) {
		uvmfault_unlockall(ufi, amap, uobj);
		cpu_count(CPU_COUNT_FLTNOUP, 1);
		UVMHIST_LOG(maphist, "  !upgrade upper", 0, 0,0,0);
		return ERESTART;
	}
	cpu_count(CPU_COUNT_FLTUP, 1);
	KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
	return 0;
}

/*
 * uvm_fault_upper_lookup: look up existing h/w mapping and amap.
 *
 * iterate range of interest:
 *	1. check if h/w mapping exists.  if yes, we don't care
 *	2. check if anon exists.  if not, page is lower.
 *	3. if anon exists, enter h/w mapping for neighbors.
 *
 * => called with amap locked (if exists).
 */

static int
uvm_fault_upper_lookup(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct vm_anon **anons, struct vm_page **pages)
{
	struct vm_amap *amap = ufi->entry->aref.ar_amap;
	int lcv;
	vaddr_t currva;
	bool shadowed __unused;
	bool entered;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* locked: maps(read), amap(if there) */
	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);

	/*
	 * map in the backpages and frontpages we found in the amap in hopes
	 * of preventing future faults.    we also init the pages[] array as
	 * we go.
	 */

	currva = flt->startva;
	shadowed = false;
	entered = false;
	for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
		/*
		 * unmapped or center page.   check if any anon at this level.
		 */
		if (amap == NULL || anons[lcv] == NULL) {
			pages[lcv] = NULL;
			continue;
		}

		/*
		 * check for present page and map if possible.
		 */

		pages[lcv] = PGO_DONTCARE;
		if (lcv == flt->centeridx) {	/* save center for later! */
			shadowed = true;
			continue;
		}

		struct vm_anon *anon = anons[lcv];
		struct vm_page *pg = anon->an_page;

		KASSERT(anon->an_lock == amap->am_lock);

		/*
		 * ignore loaned and busy pages.
		 * don't play with VAs that are already mapped.
		 */

		if (pg && pg->loan_count == 0 && (pg->flags & PG_BUSY) == 0 &&
		    !pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
			uvm_fault_upper_neighbor(ufi, flt, currva,
			    pg, anon->an_ref > 1);
			entered = true;
		}
	}
	if (entered) {
		pmap_update(ufi->orig_map->pmap);
	}

	/* locked: maps(read), amap(if there) */
	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	/* (shadowed == true) if there is an anon at the faulting address */
	UVMHIST_LOG(maphist, "  shadowed=%jd, will_get=%jd", shadowed,
	    (ufi->entry->object.uvm_obj && shadowed != false),0,0);

	return 0;
}

/*
 * uvm_fault_upper_neighbor: enter single upper neighbor page.
 *
 * => called with amap and anon locked.
 */

static void
uvm_fault_upper_neighbor(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	vaddr_t currva, struct vm_page *pg, bool readonly)
{
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* locked: amap, anon */

	KASSERT(pg->uobject == NULL);
	KASSERT(pg->uanon != NULL);
	KASSERT(rw_lock_op(pg->uanon->an_lock) == flt->upper_lock_type);
	KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);

	/*
	 * there wasn't a direct fault on the page, so avoid the cost of
	 * activating it.
	 */

	if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
		uvm_pagelock(pg);
		uvm_pageenqueue(pg);
		uvm_pageunlock(pg);
	}

	UVMHIST_LOG(maphist,
	    "  MAPPING: n anon: pm=%#jx, va=%#jx, pg=%#jx",
	    (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
	cpu_count(CPU_COUNT_FLTNAMAP, 1);

	/*
	 * Since this page isn't the page that's actually faulting,
	 * ignore pmap_enter() failures; it's not critical that we
	 * enter these right now.
	 */

	(void) pmap_enter(ufi->orig_map->pmap, currva,
	    VM_PAGE_TO_PHYS(pg),
	    readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
	    flt->enter_prot,
	    PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
}

/*
 * uvm_fault_upper: handle upper fault.
 *
 *	1. acquire anon lock.
 *	2. get anon.  let uvmfault_anonget do the dirty work.
 *	3. handle loan.
 *	4. dispatch direct or promote handlers.
 */

static int
uvm_fault_upper(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct vm_anon **anons)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	struct vm_anon * const anon = anons[flt->centeridx];
	struct uvm_object *uobj;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* locked: maps(read), amap, anon */
	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(anon->an_lock == amap->am_lock);

	/*
	 * handle case 1: fault on an anon in our amap
	 */

	UVMHIST_LOG(maphist, "  case 1 fault: anon=%#jx",
	    (uintptr_t)anon, 0, 0, 0);

	/*
	 * no matter if we have case 1A or case 1B we are going to need to
	 * have the anon's memory resident.   ensure that now.
	 */

	/*
	 * let uvmfault_anonget do the dirty work.
	 * if it fails (!OK) it will unlock everything for us.
	 * if it succeeds, locks are still valid and locked.
	 * also, if it is OK, then the anon's page is on the queues.
	 * if the page is on loan from a uvm_object, then anonget will
	 * lock that object for us if it does not fail.
	 */
 retry:
	error = uvmfault_anonget(ufi, amap, anon);
	switch (error) {
	case 0:
		break;

	case ERESTART:
		return ERESTART;

	case EAGAIN:
		kpause("fltagain1", false, hz/2, NULL);
		return ERESTART;

	case ENOLCK:
		/* it needs a write lock: retry */
		error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
		if (error != 0) {
			return error;
		}
		KASSERT(rw_write_held(amap->am_lock));
		goto retry;

	default:
		return error;
	}

	/*
	 * uobj is non null if the page is on loan from an object (i.e. uobj)
	 */

	uobj = anon->an_page->uobject;	/* locked by anonget if !NULL */

	/* locked: maps(read), amap, anon, uobj(if one) */
	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(anon->an_lock == amap->am_lock);
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/*
	 * special handling for loaned pages
	 */

	if (anon->an_page->loan_count) {
		error = uvm_fault_upper_loan(ufi, flt, anon, &uobj);
		if (error != 0)
			return error;
	}

	/*
	 * if we are case 1B then we will need to allocate a new blank
	 * anon to transfer the data into.   note that we have a lock
	 * on anon, so no one can busy or release the page until we are done.
	 * also note that the ref count can't drop to zero here because
	 * it is > 1 and we are only dropping one ref.
	 *
	 * in the (hopefully very rare) case that we are out of RAM we
	 * will unlock, wait for more RAM, and refault.
	 *
	 * if we are out of anon VM we kill the process (XXX: could wait?).
	 */

	if (flt->cow_now && anon->an_ref > 1) {
		flt->promote = true;
		error = uvm_fault_upper_promote(ufi, flt, uobj, anon);
	} else {
		error = uvm_fault_upper_direct(ufi, flt, uobj, anon);
	}
	return error;
}

/*
 * uvm_fault_upper_loan: handle loaned upper page.
 *
 *	1. if not cow'ing now, simply adjust flt->enter_prot.
 *	2. if cow'ing now, and if ref count is 1, break loan.
 */

static int
uvm_fault_upper_loan(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct vm_anon *anon, struct uvm_object **ruobj)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	int error = 0;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	if (!flt->cow_now) {

		/*
		 * for read faults on loaned pages we just cap the
		 * protection at read-only.
		 */

		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;

	} else {
		/*
		 * note that we can't allow writes into a loaned page!
		 *
		 * if we have a write fault on a loaned page in an
		 * anon then we need to look at the anon's ref count.
		 * if it is greater than one then we are going to do
		 * a normal copy-on-write fault into a new anon (this
		 * is not a problem).  however, if the reference count
		 * is one (a case where we would normally allow a
		 * write directly to the page) then we need to kill
		 * the loan before we continue.
		 */

		/* >1 case is already ok */
		if (anon->an_ref == 1) {
			/* breaking loan requires a write lock. */
			error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
			if (error != 0) {
				return error;
			}
			KASSERT(rw_write_held(amap->am_lock));

			error = uvm_loanbreak_anon(anon, *ruobj);
			if (error != 0) {
				uvmfault_unlockall(ufi, amap, *ruobj);
				uvm_wait("flt_noram2");
				return ERESTART;
			}
			/* if we were a loan receiver uobj is gone */
			if (*ruobj)
				*ruobj = NULL;
		}
	}
	return error;
}

/*
 * uvm_fault_upper_promote: promote upper page.
 *
 *	1. call uvmfault_promote.
 *	2. enqueue page.
 *	3. deref.
 *	4. pass page to uvm_fault_upper_enter.
 */

static int
uvm_fault_upper_promote(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_anon *anon)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	struct vm_anon * const oanon = anon;
	struct vm_page *pg;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	UVMHIST_LOG(maphist, "  case 1B: COW fault",0,0,0,0);
	cpu_count(CPU_COUNT_FLT_ACOW, 1);

	/* promoting requires a write lock. */
	error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
	if (error != 0) {
		return error;
	}
	KASSERT(rw_write_held(amap->am_lock));

	error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon,
	    &flt->anon_spare);
	switch (error) {
	case 0:
		break;
	case ERESTART:
		return ERESTART;
	default:
		return error;
	}
	pg = anon->an_page;

	KASSERT(anon->an_lock == oanon->an_lock);
	KASSERT((pg->flags & (PG_BUSY | PG_FAKE)) == 0);

	/* deref: can not drop to zero here by defn! */
	KASSERT(oanon->an_ref > 1);
	oanon->an_ref--;

	/*
	 * note: oanon is still locked, as is the new anon.  we
	 * need to check for this later when we unlock oanon; if
	 * oanon != anon, we'll have to unlock anon, too.
	 */

	return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
}

/*
 * uvm_fault_upper_direct: handle direct fault.
 */

static int
uvm_fault_upper_direct(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_anon *anon)
{
	struct vm_anon * const oanon = anon;
	struct vm_page *pg;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	cpu_count(CPU_COUNT_FLT_ANON, 1);
	pg = anon->an_page;
	if (anon->an_ref > 1)     /* disallow writes to ref > 1 anons */
		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;

	return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
}

/*
 * uvm_fault_upper_enter: enter h/w mapping of upper page.
 */

static int
uvm_fault_upper_enter(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
	struct vm_anon *oanon)
{
	struct pmap *pmap = ufi->orig_map->pmap;
	vaddr_t va = ufi->orig_rvaddr;
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* locked: maps(read), amap, oanon, anon(if different from oanon) */
	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(anon->an_lock == amap->am_lock);
	KASSERT(oanon->an_lock == amap->am_lock);
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
	KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);

	/*
	 * now map the page in.
	 */

	UVMHIST_LOG(maphist,
	    "  MAPPING: anon: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
	    (uintptr_t)pmap, va, (uintptr_t)pg, flt->promote);
	if (pmap_enter(pmap, va, VM_PAGE_TO_PHYS(pg),
	    flt->enter_prot, flt->access_type | PMAP_CANFAIL |
	    (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {

		/*
		 * If pmap_enter() fails, it must not leave behind an existing
		 * pmap entry.  In particular, a now-stale entry for a different
		 * page would leave the pmap inconsistent with the vm_map.
		 * This is not to imply that pmap_enter() should remove an
		 * existing mapping in such a situation (since that could create
		 * different problems, eg. if the existing mapping is wired),
		 * but rather that the pmap should be designed such that it
		 * never needs to fail when the new mapping is replacing an
		 * existing mapping and the new page has no existing mappings.
		 *
		 * XXX This can't be asserted safely any more because many
		 * LWPs and/or many processes could simultaneously fault on
		 * the same VA and some might succeed.
		 */

		/* KASSERT(!pmap_extract(pmap, va, NULL)); */

		/*
		 * ensure that the page is queued in the case that
		 * we just promoted.
		 */

		uvm_pagelock(pg);
		uvm_pageenqueue(pg);
		uvm_pageunlock(pg);

		/*
		 * No need to undo what we did; we can simply think of
		 * this as the pmap throwing away the mapping information.
		 *
		 * We do, however, have to go through the ReFault path,
		 * as the map may change while we're asleep.
		 */

		uvmfault_unlockall(ufi, amap, uobj);
		if (!uvm_reclaimable()) {
			UVMHIST_LOG(maphist,
			    "<- failed.  out of VM",0,0,0,0);
			/* XXX instrumentation */
			return ENOMEM;
		}
		/* XXX instrumentation */
		uvm_wait("flt_pmfail1");
		return ERESTART;
	}

	uvm_fault_upper_done(ufi, flt, anon, pg);

	/*
	 * done case 1!  finish up by unlocking everything and returning success
	 */

	pmap_update(pmap);
	uvmfault_unlockall(ufi, amap, uobj);
	return 0;
}

/*
 * uvm_fault_upper_done: queue upper center page.
 */

static void
uvm_fault_upper_done(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct vm_anon *anon, struct vm_page *pg)
{
	const bool wire_paging = flt->wire_paging;

	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/*
	 * ... update the page queues.
	 */

	if (wire_paging) {
		uvm_pagelock(pg);
		uvm_pagewire(pg);
		uvm_pageunlock(pg);

		/*
		 * since the now-wired page cannot be paged out,
		 * release its swap resources for others to use.
		 * and since an anon with no swap cannot be clean,
		 * mark it dirty now.
		 */

		uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
		uvm_anon_dropswap(anon);
	} else if (uvmpdpol_pageactivate_p(pg)) {
		/*
		 * avoid re-activating the page unless needed,
		 * to avoid false sharing on multiprocessor.
		 */

		uvm_pagelock(pg);
		uvm_pageactivate(pg);
		uvm_pageunlock(pg);
	}
}

/*
 * uvm_fault_lower_upgrade: upgrade lower lock, reader -> writer
 */

static inline int
uvm_fault_lower_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
    struct vm_amap *amap, struct uvm_object *uobj, struct vm_page *uobjpage)
{

	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	KASSERT(uobj != NULL);
	KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));

	/*
	 * fast path.
	 */

	if (__predict_true(flt->lower_lock_type == RW_WRITER)) {
		return 0;
	}

	/*
	 * otherwise try for the upgrade.  if we don't get it, unlock
	 * everything, restart the fault and next time around get a writer
	 * lock.
	 */

	flt->lower_lock_type = RW_WRITER;
	if (__predict_false(!rw_tryupgrade(uobj->vmobjlock))) {
		uvmfault_unlockall(ufi, amap, uobj);
		cpu_count(CPU_COUNT_FLTNOUP, 1);
		UVMHIST_LOG(maphist, "  !upgrade lower", 0, 0,0,0);
		return ERESTART;
	}
	cpu_count(CPU_COUNT_FLTUP, 1);
	KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
	return 0;
}

/*
 * uvm_fault_lower: handle lower fault.
 *
 *	1. check uobj
 *	1.1. if null, ZFOD.
 *	1.2. if not null, look up unnmapped neighbor pages.
 *	2. for center page, check if promote.
 *	2.1. ZFOD always needs promotion.
 *	2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
 *	3. if uobj is not ZFOD and page is not found, do i/o.
 *	4. dispatch either direct / promote fault.
 */

static int
uvm_fault_lower(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct vm_page **pages)
{
	struct vm_amap *amap __diagused = ufi->entry->aref.ar_amap;
	struct uvm_object *uobj = ufi->entry->object.uvm_obj;
	struct vm_page *uobjpage;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/*
	 * now, if the desired page is not shadowed by the amap and we have
	 * a backing object that does not have a special fault routine, then
	 * we ask (with pgo_get) the object for resident pages that we care
	 * about and attempt to map them in.  we do not let pgo_get block
	 * (PGO_LOCKED).
	 */

	if (uobj == NULL) {
		/* zero fill; don't care neighbor pages */
		uobjpage = NULL;
	} else {
		uvm_fault_lower_lookup(ufi, flt, pages);
		uobjpage = pages[flt->centeridx];
	}

	/*
	 * note that at this point we are done with any front or back pages.
	 * we are now going to focus on the center page (i.e. the one we've
	 * faulted on).  if we have faulted on the upper (anon) layer
	 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
	 * not touched it yet).  if we have faulted on the bottom (uobj)
	 * layer [i.e. case 2] and the page was both present and available,
	 * then we've got a pointer to it as "uobjpage" and we've already
	 * made it BUSY.
	 */

	/*
	 * locked:
	 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
	 */
	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/*
	 * note that uobjpage can not be PGO_DONTCARE at this point.  we now
	 * set uobjpage to PGO_DONTCARE if we are doing a zero fill.  if we
	 * have a backing object, check and see if we are going to promote
	 * the data up to an anon during the fault.
	 */

	if (uobj == NULL) {
		uobjpage = PGO_DONTCARE;
		flt->promote = true;		/* always need anon here */
	} else {
		KASSERT(uobjpage != PGO_DONTCARE);
		flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
	}
	UVMHIST_LOG(maphist, "  case 2 fault: promote=%jd, zfill=%jd",
	    flt->promote, (uobj == NULL), 0,0);

	/*
	 * if uobjpage is not null then we do not need to do I/O to get the
	 * uobjpage.
	 *
	 * if uobjpage is null, then we need to unlock and ask the pager to
	 * get the data for us.   once we have the data, we need to reverify
	 * the state the world.   we are currently not holding any resources.
	 */

	if (uobjpage) {
		/* update rusage counters */
		curlwp->l_ru.ru_minflt++;
	} else {
		error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
		if (error != 0)
			return error;
	}

	/*
	 * locked:
	 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
	 */
	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/*
	 * notes:
	 *  - at this point uobjpage can not be NULL
	 *  - at this point uobjpage can not be PG_RELEASED (since we checked
	 *  for it above)
	 *  - at this point uobjpage could be waited on (handle later)
	 *  - uobjpage can be from a different object if tmpfs (vnode vs UAO)
	 */

	KASSERT(uobjpage != NULL);
	KASSERT(uobj == NULL ||
	    uobjpage->uobject->vmobjlock == uobj->vmobjlock);
	KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
	    uvm_pagegetdirty(uobjpage) == UVM_PAGE_STATUS_CLEAN);

	if (!flt->promote) {
		error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
	} else {
		error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
	}
	return error;
}

/*
 * uvm_fault_lower_lookup: look up on-memory uobj pages.
 *
 *	1. get on-memory pages.
 *	2. if failed, give up (get only center page later).
 *	3. if succeeded, enter h/w mapping of neighbor pages.
 */

static void
uvm_fault_lower_lookup(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct vm_page **pages)
{
	struct uvm_object *uobj = ufi->entry->object.uvm_obj;
	int lcv, gotpages;
	vaddr_t currva;
	bool entered;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	rw_enter(uobj->vmobjlock, flt->lower_lock_type);

	/*
	 * Locked: maps(read), amap(if there), uobj
	 */

	cpu_count(CPU_COUNT_FLTLGET, 1);
	gotpages = flt->npages;
	(void) uobj->pgops->pgo_get(uobj,
	    ufi->entry->offset + flt->startva - ufi->entry->start,
	    pages, &gotpages, flt->centeridx,
	    flt->access_type & MASK(ufi->entry), ufi->entry->advice,
	    PGO_LOCKED);

	KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/*
	 * check for pages to map, if we got any
	 */

	if (gotpages == 0) {
		pages[flt->centeridx] = NULL;
		return;
	}

	entered = false;
	currva = flt->startva;
	for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
		struct vm_page *curpg;

		curpg = pages[lcv];
		if (curpg == NULL || curpg == PGO_DONTCARE) {
			continue;
		}

		/*
		 * in the case of tmpfs, the pages might be from a different
		 * uvm_object.  just make sure that they have the same lock.
		 */

		KASSERT(curpg->uobject->vmobjlock == uobj->vmobjlock);
		KASSERT((curpg->flags & PG_BUSY) == 0);

		/*
		 * leave the centre page for later.  don't screw with
		 * existing mappings (needless & expensive).
		 */

		if (lcv == flt->centeridx) {
			UVMHIST_LOG(maphist, "  got uobjpage (%#jx) "
			    "with locked get", (uintptr_t)curpg, 0, 0, 0);
		} else if (!pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
			uvm_fault_lower_neighbor(ufi, flt, currva, curpg);
			entered = true;
		}
	}
	if (entered) {
		pmap_update(ufi->orig_map->pmap);
	}
}

/*
 * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
 */

static void
uvm_fault_lower_neighbor(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	vaddr_t currva, struct vm_page *pg)
{
	const bool readonly = uvm_pagereadonly_p(pg) || pg->loan_count > 0;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* locked: maps(read), amap(if there), uobj */

	/*
	 * calling pgo_get with PGO_LOCKED returns us pages which
	 * are neither busy nor released, so we don't need to check
	 * for this.  we can just directly enter the pages.
	 *
	 * there wasn't a direct fault on the page, so avoid the cost of
	 * activating it.
	 */

	if (!uvmpdpol_pageisqueued_p(pg) && pg->wire_count == 0) {
		uvm_pagelock(pg);
		uvm_pageenqueue(pg);
		uvm_pageunlock(pg);
	}

	UVMHIST_LOG(maphist,
	    "  MAPPING: n obj: pm=%#jx, va=%#jx, pg=%#jx",
	    (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
	cpu_count(CPU_COUNT_FLTNOMAP, 1);

	/*
	 * Since this page isn't the page that's actually faulting,
	 * ignore pmap_enter() failures; it's not critical that we
	 * enter these right now.
	 * NOTE: page can't be waited on or PG_RELEASED because we've
	 * held the lock the whole time we've had the handle.
	 */
	KASSERT((pg->flags & PG_PAGEOUT) == 0);
	KASSERT((pg->flags & PG_RELEASED) == 0);
	KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
	    uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
	KASSERT((pg->flags & PG_BUSY) == 0);
	KASSERT(rw_lock_op(pg->uobject->vmobjlock) == flt->lower_lock_type);

	const vm_prot_t mapprot =
	    readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
	    flt->enter_prot & MASK(ufi->entry);
	const u_int mapflags =
	    PMAP_CANFAIL | (flt->wire_mapping ? (mapprot | PMAP_WIRED) : 0);
	(void) pmap_enter(ufi->orig_map->pmap, currva,
	    VM_PAGE_TO_PHYS(pg), mapprot, mapflags);
}

/*
 * uvm_fault_lower_io: get lower page from backing store.
 *
 *	1. unlock everything, because i/o will block.
 *	2. call pgo_get.
 *	3. if failed, recover.
 *	4. if succeeded, relock everything and verify things.
 */

static int
uvm_fault_lower_io(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object **ruobj, struct vm_page **ruobjpage)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	struct uvm_object *uobj = *ruobj;
	struct vm_page *pg;
	bool locked;
	int gotpages;
	int error;
	voff_t uoff;
	vm_prot_t access_type;
	int advice;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/* update rusage counters */
	curlwp->l_ru.ru_majflt++;

	/* grab everything we need from the entry before we unlock */
	uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
	access_type = flt->access_type & MASK(ufi->entry);
	advice = ufi->entry->advice;

	/* Locked: maps(read), amap(if there), uobj */
	KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/* Upgrade to a write lock if needed. */
	error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, NULL);
	if (error != 0) {
		return error;
	}
	uvmfault_unlockall(ufi, amap, NULL);

	/* Locked: uobj(write) */
	KASSERT(rw_write_held(uobj->vmobjlock));

	cpu_count(CPU_COUNT_FLTGET, 1);
	gotpages = 1;
	pg = NULL;
	error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
	    0, access_type, advice, PGO_SYNCIO);
	/* locked: pg(if no error) */

	/*
	 * recover from I/O
	 */

	if (error) {
		if (error == EAGAIN) {
			UVMHIST_LOG(maphist,
			    "  pgo_get says TRY AGAIN!",0,0,0,0);
			kpause("fltagain2", false, hz/2, NULL);
			return ERESTART;
		}

#if 0
		KASSERT(error != ERESTART);
#else
		/* XXXUEBS don't re-fault? */
		if (error == ERESTART)
			error = EIO;
#endif

		UVMHIST_LOG(maphist, "<- pgo_get failed (code %jd)",
		    error, 0,0,0);
		return error;
	}

	/*
	 * re-verify the state of the world by first trying to relock
	 * the maps.  always relock the object.
	 */

	locked = uvmfault_relock(ufi);
	if (locked && amap)
		amap_lock(amap, flt->upper_lock_type);

	/* might be changed */
	uobj = pg->uobject;

	rw_enter(uobj->vmobjlock, flt->lower_lock_type);
	KASSERT((pg->flags & PG_BUSY) != 0);
	KASSERT(flt->lower_lock_type == RW_WRITER);

	uvm_pagelock(pg);
	uvm_pageactivate(pg);
	uvm_pageunlock(pg);

	/* locked(locked): maps(read), amap(if !null), uobj, pg */
	/* locked(!locked): uobj, pg */

	/*
	 * verify that the page has not be released and re-verify
	 * that amap slot is still free.   if there is a problem,
	 * we unlock and clean up.
	 */

	if ((pg->flags & PG_RELEASED) != 0 ||
	    (locked && amap && amap_lookup(&ufi->entry->aref,
	      ufi->orig_rvaddr - ufi->entry->start))) {
		if (locked)
			uvmfault_unlockall(ufi, amap, NULL);
		locked = false;
	}

	/*
	 * unbusy/release the page.
	 */

	if ((pg->flags & PG_RELEASED) == 0) {
		pg->flags &= ~PG_BUSY;
		uvm_pagelock(pg);
		uvm_pagewakeup(pg);
		uvm_pageunlock(pg);
		UVM_PAGE_OWN(pg, NULL);
	} else {
		cpu_count(CPU_COUNT_FLTPGRELE, 1);
		uvm_pagefree(pg);
	}

	/*
	 * didn't get the lock?   retry.
	 */

	if (locked == false) {
		UVMHIST_LOG(maphist,
		    "  wasn't able to relock after fault: retry",
		    0,0,0,0);
		rw_exit(uobj->vmobjlock);
		return ERESTART;
	}

	/*
	 * we have the data in pg.  we are holding object lock (so the page
	 * can't be released on us).
	 */

	/* locked: maps(read), amap(if !null), uobj */

	*ruobj = uobj;
	*ruobjpage = pg;
	return 0;
}

/*
 * uvm_fault_lower_direct: fault lower center page
 *
 *	1. adjust flt->enter_prot.
 *	2. if page is loaned, resolve.
 */

int
uvm_fault_lower_direct(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_page *uobjpage)
{
	struct vm_page *pg;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/*
	 * we are not promoting.   if the mapping is COW ensure that we
	 * don't give more access than we should (e.g. when doing a read
	 * fault on a COPYONWRITE mapping we want to map the COW page in
	 * R/O even though the entry protection could be R/W).
	 *
	 * set "pg" to the page we want to map in (uobjpage, usually)
	 */

	cpu_count(CPU_COUNT_FLT_OBJ, 1);
	if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
	    UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
		flt->enter_prot &= ~VM_PROT_WRITE;
	pg = uobjpage;		/* map in the actual object */

	KASSERT(uobjpage != PGO_DONTCARE);

	/*
	 * we are faulting directly on the page.   be careful
	 * about writing to loaned pages...
	 */

	if (uobjpage->loan_count) {
		uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
	}
	KASSERT(pg == uobjpage);
	KASSERT((pg->flags & PG_BUSY) == 0);
	return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg);
}

/*
 * uvm_fault_lower_direct_loan: resolve loaned page.
 *
 *	1. if not cow'ing, adjust flt->enter_prot.
 *	2. if cow'ing, break loan.
 */

static int
uvm_fault_lower_direct_loan(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_page **rpg,
	struct vm_page **ruobjpage)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	struct vm_page *pg;
	struct vm_page *uobjpage = *ruobjpage;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	if (!flt->cow_now) {
		/* read fault: cap the protection at readonly */
		/* cap! */
		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
	} else {
		/*
		 * write fault: must break the loan here.  to do this
		 * we need a write lock on the object.
		 */

		error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, uobjpage);
		if (error != 0) {
			return error;
		}
		KASSERT(rw_write_held(uobj->vmobjlock));

		pg = uvm_loanbreak(uobjpage);
		if (pg == NULL) {

			uvmfault_unlockall(ufi, amap, uobj);
			UVMHIST_LOG(maphist,
			  "  out of RAM breaking loan, waiting",
			  0,0,0,0);
			cpu_count(CPU_COUNT_FLTNORAM, 1);
			uvm_wait("flt_noram4");
			return ERESTART;
		}
		*rpg = pg;
		*ruobjpage = pg;

		/*
		 * drop ownership of page while still holding object lock,
		 * which won't be dropped until the page is entered.
		 */

		uvm_pagelock(pg);
		uvm_pagewakeup(pg);
		uvm_pageunlock(pg);
		pg->flags &= ~PG_BUSY;
		UVM_PAGE_OWN(pg, NULL);
	}
	return 0;
}

/*
 * uvm_fault_lower_promote: promote lower page.
 *
 *	1. call uvmfault_promote.
 *	2. fill in data.
 *	3. if not ZFOD, dispose old page.
 */

int
uvm_fault_lower_promote(
	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_page *uobjpage)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	struct vm_anon *anon;
	struct vm_page *pg;
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	KASSERT(amap != NULL);

	/* promoting requires a write lock. */
	error = uvm_fault_upper_upgrade(ufi, flt, amap, uobj);
	if (error != 0) {
		return error;
	}
	KASSERT(rw_write_held(amap->am_lock));
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);

	/*
	 * If we are going to promote the data to an anon we
	 * allocate a blank anon here and plug it into our amap.
	 */
	error = uvmfault_promote(ufi, NULL, uobjpage, &anon, &flt->anon_spare);
	switch (error) {
	case 0:
		break;
	case ERESTART:
		return ERESTART;
	default:
		return error;
	}

	pg = anon->an_page;

	/*
	 * Fill in the data.
	 */

	if (uobjpage != PGO_DONTCARE) {
		cpu_count(CPU_COUNT_FLT_PRCOPY, 1);

		/*
		 * promote to shared amap?  make sure all sharing
		 * procs see it
		 */

		if ((amap_flags(amap) & AMAP_SHARED) != 0) {
			pmap_page_protect(uobjpage, VM_PROT_NONE);
			/*
			 * XXX: PAGE MIGHT BE WIRED!
			 */
		}

		UVMHIST_LOG(maphist,
		    "  promote uobjpage %#jx to anon/page %#jx/%#jx",
		    (uintptr_t)uobjpage, (uintptr_t)anon, (uintptr_t)pg, 0);

	} else {
		cpu_count(CPU_COUNT_FLT_PRZERO, 1);

		/*
		 * Page is zero'd and marked dirty by
		 * uvmfault_promote().
		 */

		UVMHIST_LOG(maphist,"  zero fill anon/page %#jx/%#jx",
		    (uintptr_t)anon, (uintptr_t)pg, 0, 0);
	}

	return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg);
}

/*
 * uvm_fault_lower_enter: enter h/w mapping of lower page or anon page promoted
 * from the lower page.
 */

int
uvm_fault_lower_enter(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct uvm_object *uobj,
	struct vm_anon *anon, struct vm_page *pg)
{
	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
	const bool readonly = uvm_pagereadonly_p(pg);
	int error;
	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	/*
	 * Locked:
	 *
	 *	maps(read), amap(if !null), uobj(if !null),
	 *	anon(if !null), pg(if anon), unlock_uobj(if !null)
	 *
	 * anon must be write locked (promotion).  uobj can be either.
	 *
	 * Note: pg is either the uobjpage or the new page in the new anon.
	 */

	KASSERT(amap == NULL ||
	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
	KASSERT(uobj == NULL ||
	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
	KASSERT(anon == NULL || anon->an_lock == amap->am_lock);

	/*
	 * note that pg can't be PG_RELEASED or PG_BUSY since we did
	 * not drop the object lock since the last time we checked.
	 */

	KASSERT((pg->flags & PG_RELEASED) == 0);
	KASSERT((pg->flags & PG_BUSY) == 0);

	/*
	 * all resources are present.   we can now map it in and free our
	 * resources.
	 */

	UVMHIST_LOG(maphist,
	    "  MAPPING: case2: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
	    (uintptr_t)ufi->orig_map->pmap, ufi->orig_rvaddr,
	    (uintptr_t)pg, flt->promote);
	KASSERTMSG((flt->access_type & VM_PROT_WRITE) == 0 || !readonly,
	    "promote=%u cow_now=%u access_type=%x enter_prot=%x cow=%u "
	    "entry=%p map=%p orig_rvaddr=%p pg=%p",
	    flt->promote, flt->cow_now, flt->access_type, flt->enter_prot,
	    UVM_ET_ISCOPYONWRITE(ufi->entry), ufi->entry, ufi->orig_map,
	    (void *)ufi->orig_rvaddr, pg);
	KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || !readonly);
	if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr,
	    VM_PAGE_TO_PHYS(pg),
	    readonly ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
	    flt->access_type | PMAP_CANFAIL |
	    (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {

		/*
		 * No need to undo what we did; we can simply think of
		 * this as the pmap throwing away the mapping information.
		 *
		 * We do, however, have to go through the ReFault path,
		 * as the map may change while we're asleep.
		 */

		/*
		 * ensure that the page is queued in the case that
		 * we just promoted the page.
		 */

		if (anon != NULL) {
			uvm_pagelock(pg);
			uvm_pageenqueue(pg);
			uvm_pagewakeup(pg);
			uvm_pageunlock(pg);
		}

		uvmfault_unlockall(ufi, amap, uobj);
		if (!uvm_reclaimable()) {
			UVMHIST_LOG(maphist,
			    "<- failed.  out of VM",0,0,0,0);
			/* XXX instrumentation */
			error = ENOMEM;
			return error;
		}
		/* XXX instrumentation */
		uvm_wait("flt_pmfail2");
		return ERESTART;
	}

	uvm_fault_lower_done(ufi, flt, uobj, pg);
	pmap_update(ufi->orig_map->pmap);
	uvmfault_unlockall(ufi, amap, uobj);

	UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
	return 0;
}

/*
 * uvm_fault_lower_done: queue lower center page.
 */

void
uvm_fault_lower_done(
	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
	struct uvm_object *uobj, struct vm_page *pg)
{

	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);

	if (flt->wire_paging) {
		uvm_pagelock(pg);
		uvm_pagewire(pg);
		uvm_pageunlock(pg);
		if (pg->flags & PG_AOBJ) {

			/*
			 * since the now-wired page cannot be paged out,
			 * release its swap resources for others to use.
			 * since an aobj page with no swap cannot be clean,
			 * mark it dirty now.
			 *
			 * use pg->uobject here.  if the page is from a
			 * tmpfs vnode, the pages are backed by its UAO and
			 * not the vnode.
			 */

			KASSERT(uobj != NULL);
			KASSERT(uobj->vmobjlock == pg->uobject->vmobjlock);
			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
			uao_dropswap(pg->uobject, pg->offset >> PAGE_SHIFT);
		}
	} else if (uvmpdpol_pageactivate_p(pg)) {
		/*
		 * avoid re-activating the page unless needed,
		 * to avoid false sharing on multiprocessor.
		 */

		uvm_pagelock(pg);
		uvm_pageactivate(pg);
		uvm_pageunlock(pg);
	}
}


/*
 * uvm_fault_wire: wire down a range of virtual addresses in a map.
 *
 * => map may be read-locked by caller, but MUST NOT be write-locked.
 * => if map is read-locked, any operations which may cause map to
 *	be write-locked in uvm_fault() must be taken care of by
 *	the caller.  See uvm_map_pageable().
 */

int
uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
    vm_prot_t access_type, int maxprot)
{
	vaddr_t va;
	int error;

	/*
	 * now fault it in a page at a time.   if the fault fails then we have
	 * to undo what we have done.   note that in uvm_fault VM_PROT_NONE
	 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
	 */

	/*
	 * XXX work around overflowing a vaddr_t.  this prevents us from
	 * wiring the last page in the address space, though.
	 */
	if (start > end) {
		return EFAULT;
	}

	for (va = start; va < end; va += PAGE_SIZE) {
		error = uvm_fault_internal(map, va, access_type,
		    (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
		if (error) {
			if (va != start) {
				uvm_fault_unwire(map, start, va);
			}
			return error;
		}
	}
	return 0;
}

/*
 * uvm_fault_unwire(): unwire range of virtual space.
 */

void
uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
{
	vm_map_lock_read(map);
	uvm_fault_unwire_locked(map, start, end);
	vm_map_unlock_read(map);
}

/*
 * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
 *
 * => map must be at least read-locked.
 */

void
uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
{
	struct vm_map_entry *entry, *oentry;
	pmap_t pmap = vm_map_pmap(map);
	vaddr_t va;
	paddr_t pa;
	struct vm_page *pg;

	/*
	 * we assume that the area we are unwiring has actually been wired
	 * in the first place.   this means that we should be able to extract
	 * the PAs from the pmap.   we also lock out the page daemon so that
	 * we can call uvm_pageunwire.
	 */

	/*
	 * find the beginning map entry for the region.
	 */

	KASSERT(start >= vm_map_min(map));
	KASSERT(end <= vm_map_max(map));
	if (uvm_map_lookup_entry(map, start, &entry) == false)
		panic("uvm_fault_unwire_locked: address not in map");

	oentry = NULL;
	for (va = start; va < end; va += PAGE_SIZE) {

		/*
		 * find the map entry for the current address.
		 */

		KASSERT(va >= entry->start);
		while (va >= entry->end) {
			KASSERT(entry->next != &map->header);
			KASSERT(entry->next->start <= entry->end);
			entry = entry->next;
		}

		/*
		 * lock it.
		 */

		if (entry != oentry) {
			if (oentry != NULL) {
				uvm_map_unlock_entry(oentry);
			}
			uvm_map_lock_entry(entry, RW_WRITER);
			oentry = entry;
		}

		/*
		 * if the entry is no longer wired, tell the pmap.
		 */

		if (!pmap_extract(pmap, va, &pa))
			continue;

		if (VM_MAPENT_ISWIRED(entry) == 0)
			pmap_unwire(pmap, va);

		pg = PHYS_TO_VM_PAGE(pa);
		if (pg) {
			uvm_pagelock(pg);
			uvm_pageunwire(pg);
			uvm_pageunlock(pg);
		}
	}

	if (oentry != NULL) {
		uvm_map_unlock_entry(entry);
	}
}