Home | History | Annotate | Line # | Download | only in uvm
uvm_fault.c revision 1.222
      1 /*	$NetBSD: uvm_fault.c,v 1.222 2020/03/22 18:32:42 ad Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Charles D. Cranor and Washington University.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  *
     16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26  *
     27  * from: Id: uvm_fault.c,v 1.1.2.23 1998/02/06 05:29:05 chs Exp
     28  */
     29 
     30 /*
     31  * uvm_fault.c: fault handler
     32  */
     33 
     34 #include <sys/cdefs.h>
     35 __KERNEL_RCSID(0, "$NetBSD: uvm_fault.c,v 1.222 2020/03/22 18:32:42 ad Exp $");
     36 
     37 #include "opt_uvmhist.h"
     38 
     39 #include <sys/param.h>
     40 #include <sys/systm.h>
     41 #include <sys/atomic.h>
     42 #include <sys/kernel.h>
     43 #include <sys/mman.h>
     44 
     45 #include <uvm/uvm.h>
     46 
     47 /*
     48  *
     49  * a word on page faults:
     50  *
     51  * types of page faults we handle:
     52  *
     53  * CASE 1: upper layer faults                   CASE 2: lower layer faults
     54  *
     55  *    CASE 1A         CASE 1B                  CASE 2A        CASE 2B
     56  *    read/write1     write>1                  read/write   +-cow_write/zero
     57  *         |             |                         |        |
     58  *      +--|--+       +--|--+     +-----+       +  |  +     | +-----+
     59  * amap |  V  |       |  ---------> new |          |        | |  ^  |
     60  *      +-----+       +-----+     +-----+       +  |  +     | +--|--+
     61  *                                                 |        |    |
     62  *      +-----+       +-----+                   +--|--+     | +--|--+
     63  * uobj | d/c |       | d/c |                   |  V  |     +----+  |
     64  *      +-----+       +-----+                   +-----+       +-----+
     65  *
     66  * d/c = don't care
     67  *
     68  *   case [0]: layerless fault
     69  *	no amap or uobj is present.   this is an error.
     70  *
     71  *   case [1]: upper layer fault [anon active]
     72  *     1A: [read] or [write with anon->an_ref == 1]
     73  *		I/O takes place in upper level anon and uobj is not touched.
     74  *     1B: [write with anon->an_ref > 1]
     75  *		new anon is alloc'd and data is copied off ["COW"]
     76  *
     77  *   case [2]: lower layer fault [uobj]
     78  *     2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
     79  *		I/O takes place directly in object.
     80  *     2B: [write to copy_on_write] or [read on NULL uobj]
     81  *		data is "promoted" from uobj to a new anon.
     82  *		if uobj is null, then we zero fill.
     83  *
     84  * we follow the standard UVM locking protocol ordering:
     85  *
     86  * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
     87  * we hold a PG_BUSY page if we unlock for I/O
     88  *
     89  *
     90  * the code is structured as follows:
     91  *
     92  *     - init the "IN" params in the ufi structure
     93  *   ReFault: (ERESTART returned to the loop in uvm_fault_internal)
     94  *     - do lookups [locks maps], check protection, handle needs_copy
     95  *     - check for case 0 fault (error)
     96  *     - establish "range" of fault
     97  *     - if we have an amap lock it and extract the anons
     98  *     - if sequential advice deactivate pages behind us
     99  *     - at the same time check pmap for unmapped areas and anon for pages
    100  *	 that we could map in (and do map it if found)
    101  *     - check object for resident pages that we could map in
    102  *     - if (case 2) goto Case2
    103  *     - >>> handle case 1
    104  *           - ensure source anon is resident in RAM
    105  *           - if case 1B alloc new anon and copy from source
    106  *           - map the correct page in
    107  *   Case2:
    108  *     - >>> handle case 2
    109  *           - ensure source page is resident (if uobj)
    110  *           - if case 2B alloc new anon and copy from source (could be zero
    111  *		fill if uobj == NULL)
    112  *           - map the correct page in
    113  *     - done!
    114  *
    115  * note on paging:
    116  *   if we have to do I/O we place a PG_BUSY page in the correct object,
    117  * unlock everything, and do the I/O.   when I/O is done we must reverify
    118  * the state of the world before assuming that our data structures are
    119  * valid.   [because mappings could change while the map is unlocked]
    120  *
    121  *  alternative 1: unbusy the page in question and restart the page fault
    122  *    from the top (ReFault).   this is easy but does not take advantage
    123  *    of the information that we already have from our previous lookup,
    124  *    although it is possible that the "hints" in the vm_map will help here.
    125  *
    126  * alternative 2: the system already keeps track of a "version" number of
    127  *    a map.   [i.e. every time you write-lock a map (e.g. to change a
    128  *    mapping) you bump the version number up by one...]   so, we can save
    129  *    the version number of the map before we release the lock and start I/O.
    130  *    then when I/O is done we can relock and check the version numbers
    131  *    to see if anything changed.    this might save us some over 1 because
    132  *    we don't have to unbusy the page and may be less compares(?).
    133  *
    134  * alternative 3: put in backpointers or a way to "hold" part of a map
    135  *    in place while I/O is in progress.   this could be complex to
    136  *    implement (especially with structures like amap that can be referenced
    137  *    by multiple map entries, and figuring out what should wait could be
    138  *    complex as well...).
    139  *
    140  * we use alternative 2.  given that we are multi-threaded now we may want
    141  * to reconsider the choice.
    142  */
    143 
    144 /*
    145  * local data structures
    146  */
    147 
    148 struct uvm_advice {
    149 	int advice;
    150 	int nback;
    151 	int nforw;
    152 };
    153 
    154 /*
    155  * page range array:
    156  * note: index in array must match "advice" value
    157  * XXX: borrowed numbers from freebsd.   do they work well for us?
    158  */
    159 
    160 static const struct uvm_advice uvmadvice[] = {
    161 	{ UVM_ADV_NORMAL, 3, 4 },
    162 	{ UVM_ADV_RANDOM, 0, 0 },
    163 	{ UVM_ADV_SEQUENTIAL, 8, 7},
    164 };
    165 
    166 #define UVM_MAXRANGE 16	/* must be MAX() of nback+nforw+1 */
    167 
    168 /*
    169  * private prototypes
    170  */
    171 
    172 /*
    173  * externs from other modules
    174  */
    175 
    176 extern int start_init_exec;	/* Is init_main() done / init running? */
    177 
    178 /*
    179  * inline functions
    180  */
    181 
    182 /*
    183  * uvmfault_anonflush: try and deactivate pages in specified anons
    184  *
    185  * => does not have to deactivate page if it is busy
    186  */
    187 
    188 static inline void
    189 uvmfault_anonflush(struct vm_anon **anons, int n)
    190 {
    191 	int lcv;
    192 	struct vm_page *pg;
    193 
    194 	for (lcv = 0; lcv < n; lcv++) {
    195 		if (anons[lcv] == NULL)
    196 			continue;
    197 		KASSERT(rw_lock_held(anons[lcv]->an_lock));
    198 		pg = anons[lcv]->an_page;
    199 		if (pg && (pg->flags & PG_BUSY) == 0) {
    200 			uvm_pagelock(pg);
    201 			uvm_pagedeactivate(pg);
    202 			uvm_pageunlock(pg);
    203 		}
    204 	}
    205 }
    206 
    207 /*
    208  * normal functions
    209  */
    210 
    211 /*
    212  * uvmfault_amapcopy: clear "needs_copy" in a map.
    213  *
    214  * => called with VM data structures unlocked (usually, see below)
    215  * => we get a write lock on the maps and clear needs_copy for a VA
    216  * => if we are out of RAM we sleep (waiting for more)
    217  */
    218 
    219 static void
    220 uvmfault_amapcopy(struct uvm_faultinfo *ufi)
    221 {
    222 	for (;;) {
    223 
    224 		/*
    225 		 * no mapping?  give up.
    226 		 */
    227 
    228 		if (uvmfault_lookup(ufi, true) == false)
    229 			return;
    230 
    231 		/*
    232 		 * copy if needed.
    233 		 */
    234 
    235 		if (UVM_ET_ISNEEDSCOPY(ufi->entry))
    236 			amap_copy(ufi->map, ufi->entry, AMAP_COPY_NOWAIT,
    237 				ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
    238 
    239 		/*
    240 		 * didn't work?  must be out of RAM.   unlock and sleep.
    241 		 */
    242 
    243 		if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
    244 			uvmfault_unlockmaps(ufi, true);
    245 			uvm_wait("fltamapcopy");
    246 			continue;
    247 		}
    248 
    249 		/*
    250 		 * got it!   unlock and return.
    251 		 */
    252 
    253 		uvmfault_unlockmaps(ufi, true);
    254 		return;
    255 	}
    256 	/*NOTREACHED*/
    257 }
    258 
    259 /*
    260  * uvmfault_anonget: get data in an anon into a non-busy, non-released
    261  * page in that anon.
    262  *
    263  * => Map, amap and thus anon should be locked by caller.
    264  * => If we fail, we unlock everything and error is returned.
    265  * => If we are successful, return with everything still locked.
    266  * => We do not move the page on the queues [gets moved later].  If we
    267  *    allocate a new page [we_own], it gets put on the queues.  Either way,
    268  *    the result is that the page is on the queues at return time
    269  * => For pages which are on loan from a uvm_object (and thus are not owned
    270  *    by the anon): if successful, return with the owning object locked.
    271  *    The caller must unlock this object when it unlocks everything else.
    272  */
    273 
    274 int
    275 uvmfault_anonget(struct uvm_faultinfo *ufi, struct vm_amap *amap,
    276     struct vm_anon *anon)
    277 {
    278 	struct vm_page *pg;
    279 	krw_t lock_type;
    280 	int error;
    281 
    282 	UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
    283 	KASSERT(rw_lock_held(anon->an_lock));
    284 	KASSERT(anon->an_lock == amap->am_lock);
    285 
    286 	/* Increment the counters.*/
    287 	cpu_count(CPU_COUNT_FLTANGET, 1);
    288 	if (anon->an_page) {
    289 		curlwp->l_ru.ru_minflt++;
    290 	} else {
    291 		curlwp->l_ru.ru_majflt++;
    292 	}
    293 	error = 0;
    294 
    295 	/*
    296 	 * Loop until we get the anon data, or fail.
    297 	 */
    298 
    299 	for (;;) {
    300 		bool we_own, locked;
    301 		/*
    302 		 * Note: 'we_own' will become true if we set PG_BUSY on a page.
    303 		 */
    304 		we_own = false;
    305 		pg = anon->an_page;
    306 
    307 		/*
    308 		 * If there is a resident page and it is loaned, then anon
    309 		 * may not own it.  Call out to uvm_anon_lockloanpg() to
    310 		 * identify and lock the real owner of the page.
    311 		 */
    312 
    313 		if (pg && pg->loan_count)
    314 			pg = uvm_anon_lockloanpg(anon);
    315 
    316 		/*
    317 		 * Is page resident?  Make sure it is not busy/released.
    318 		 */
    319 
    320 		lock_type = rw_lock_op(anon->an_lock);
    321 		if (pg) {
    322 
    323 			/*
    324 			 * at this point, if the page has a uobject [meaning
    325 			 * we have it on loan], then that uobject is locked
    326 			 * by us!   if the page is busy, we drop all the
    327 			 * locks (including uobject) and try again.
    328 			 */
    329 
    330 			if ((pg->flags & PG_BUSY) == 0) {
    331 				UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
    332 				return 0;
    333 			}
    334 			cpu_count(CPU_COUNT_FLTPGWAIT, 1);
    335 
    336 			/*
    337 			 * The last unlock must be an atomic unlock and wait
    338 			 * on the owner of page.
    339 			 */
    340 
    341 			if (pg->uobject) {
    342 				/* Owner of page is UVM object. */
    343 				uvmfault_unlockall(ufi, amap, NULL);
    344 				UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
    345 				    0,0,0);
    346 				uvm_pagewait(pg, pg->uobject->vmobjlock, "anonget1");
    347 			} else {
    348 				/* Owner of page is anon. */
    349 				uvmfault_unlockall(ufi, NULL, NULL);
    350 				UVMHIST_LOG(maphist, " unlock+wait on anon",0,
    351 				    0,0,0);
    352 				uvm_pagewait(pg, anon->an_lock, "anonget2");
    353 			}
    354 		} else {
    355 #if defined(VMSWAP)
    356 			/*
    357 			 * No page, therefore allocate one.  A write lock is
    358 			 * required for this.  If the caller didn't supply
    359 			 * one, fail now and have them retry.
    360 			 */
    361 
    362 			if (lock_type == RW_READER) {
    363 				return ENOLCK;
    364 			}
    365 			pg = uvm_pagealloc(NULL,
    366 			    ufi != NULL ? ufi->orig_rvaddr : 0,
    367 			    anon, ufi != NULL ? UVM_FLAG_COLORMATCH : 0);
    368 			if (pg == NULL) {
    369 				/* Out of memory.  Wait a little. */
    370 				uvmfault_unlockall(ufi, amap, NULL);
    371 				cpu_count(CPU_COUNT_FLTNORAM, 1);
    372 				UVMHIST_LOG(maphist, "  noram -- UVM_WAIT",0,
    373 				    0,0,0);
    374 				if (!uvm_reclaimable()) {
    375 					return ENOMEM;
    376 				}
    377 				uvm_wait("flt_noram1");
    378 			} else {
    379 				/* PG_BUSY bit is set. */
    380 				we_own = true;
    381 				uvmfault_unlockall(ufi, amap, NULL);
    382 
    383 				/*
    384 				 * Pass a PG_BUSY+PG_FAKE clean page into
    385 				 * the uvm_swap_get() function with all data
    386 				 * structures unlocked.  Note that it is OK
    387 				 * to read an_swslot here, because we hold
    388 				 * PG_BUSY on the page.
    389 				 */
    390 				cpu_count(CPU_COUNT_PAGEINS, 1);
    391 				error = uvm_swap_get(pg, anon->an_swslot,
    392 				    PGO_SYNCIO);
    393 
    394 				/*
    395 				 * We clean up after the I/O below in the
    396 				 * 'we_own' case.
    397 				 */
    398 			}
    399 #else
    400 			panic("%s: no page", __func__);
    401 #endif /* defined(VMSWAP) */
    402 		}
    403 
    404 		/*
    405 		 * Re-lock the map and anon.
    406 		 */
    407 
    408 		locked = uvmfault_relock(ufi);
    409 		if (locked || we_own) {
    410 			rw_enter(anon->an_lock, lock_type);
    411 		}
    412 
    413 		/*
    414 		 * If we own the page (i.e. we set PG_BUSY), then we need
    415 		 * to clean up after the I/O.  There are three cases to
    416 		 * consider:
    417 		 *
    418 		 * 1) Page was released during I/O: free anon and ReFault.
    419 		 * 2) I/O not OK.  Free the page and cause the fault to fail.
    420 		 * 3) I/O OK!  Activate the page and sync with the non-we_own
    421 		 *    case (i.e. drop anon lock if not locked).
    422 		 */
    423 
    424 		if (we_own) {
    425 			KASSERT(lock_type == RW_WRITER);
    426 #if defined(VMSWAP)
    427 			if (error) {
    428 
    429 				/*
    430 				 * Remove the swap slot from the anon and
    431 				 * mark the anon as having no real slot.
    432 				 * Do not free the swap slot, thus preventing
    433 				 * it from being used again.
    434 				 */
    435 
    436 				if (anon->an_swslot > 0) {
    437 					uvm_swap_markbad(anon->an_swslot, 1);
    438 				}
    439 				anon->an_swslot = SWSLOT_BAD;
    440 
    441 				if ((pg->flags & PG_RELEASED) != 0) {
    442 					goto released;
    443 				}
    444 
    445 				/*
    446 				 * Note: page was never !PG_BUSY, so it
    447 				 * cannot be mapped and thus no need to
    448 				 * pmap_page_protect() it.
    449 				 */
    450 
    451 				uvm_pagefree(pg);
    452 
    453 				if (locked) {
    454 					uvmfault_unlockall(ufi, NULL, NULL);
    455 				}
    456 				rw_exit(anon->an_lock);
    457 				UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
    458 				return error;
    459 			}
    460 
    461 			if ((pg->flags & PG_RELEASED) != 0) {
    462 released:
    463 				KASSERT(anon->an_ref == 0);
    464 
    465 				/*
    466 				 * Released while we had unlocked amap.
    467 				 */
    468 
    469 				if (locked) {
    470 					uvmfault_unlockall(ufi, NULL, NULL);
    471 				}
    472 				uvm_anon_release(anon);
    473 
    474 				if (error) {
    475 					UVMHIST_LOG(maphist,
    476 					    "<- ERROR/RELEASED", 0,0,0,0);
    477 					return error;
    478 				}
    479 
    480 				UVMHIST_LOG(maphist, "<- RELEASED", 0,0,0,0);
    481 				return ERESTART;
    482 			}
    483 
    484 			/*
    485 			 * We have successfully read the page, activate it.
    486 			 */
    487 
    488 			uvm_pagelock(pg);
    489 			uvm_pageactivate(pg);
    490 			uvm_pagewakeup(pg);
    491 			uvm_pageunlock(pg);
    492 			pg->flags &= ~(PG_BUSY|PG_FAKE);
    493 			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_UNKNOWN);
    494 			UVM_PAGE_OWN(pg, NULL);
    495 #else
    496 			panic("%s: we_own", __func__);
    497 #endif /* defined(VMSWAP) */
    498 		}
    499 
    500 		/*
    501 		 * We were not able to re-lock the map - restart the fault.
    502 		 */
    503 
    504 		if (!locked) {
    505 			if (we_own) {
    506 				rw_exit(anon->an_lock);
    507 			}
    508 			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    509 			return ERESTART;
    510 		}
    511 
    512 		/*
    513 		 * Verify that no one has touched the amap and moved
    514 		 * the anon on us.
    515 		 */
    516 
    517 		if (ufi != NULL && amap_lookup(&ufi->entry->aref,
    518 		    ufi->orig_rvaddr - ufi->entry->start) != anon) {
    519 
    520 			uvmfault_unlockall(ufi, amap, NULL);
    521 			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    522 			return ERESTART;
    523 		}
    524 
    525 		/*
    526 		 * Retry..
    527 		 */
    528 
    529 		cpu_count(CPU_COUNT_FLTANRETRY, 1);
    530 		continue;
    531 	}
    532 	/*NOTREACHED*/
    533 }
    534 
    535 /*
    536  * uvmfault_promote: promote data to a new anon.  used for 1B and 2B.
    537  *
    538  *	1. allocate an anon and a page.
    539  *	2. fill its contents.
    540  *	3. put it into amap.
    541  *
    542  * => if we fail (result != 0) we unlock everything.
    543  * => on success, return a new locked anon via 'nanon'.
    544  *    (*nanon)->an_page will be a resident, locked, dirty page.
    545  * => it's caller's responsibility to put the promoted nanon->an_page to the
    546  *    page queue.
    547  */
    548 
    549 static int
    550 uvmfault_promote(struct uvm_faultinfo *ufi,
    551     struct vm_anon *oanon,
    552     struct vm_page *uobjpage,
    553     struct vm_anon **nanon, /* OUT: allocated anon */
    554     struct vm_anon **spare)
    555 {
    556 	struct vm_amap *amap = ufi->entry->aref.ar_amap;
    557 	struct uvm_object *uobj;
    558 	struct vm_anon *anon;
    559 	struct vm_page *pg;
    560 	struct vm_page *opg;
    561 	int error;
    562 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
    563 
    564 	if (oanon) {
    565 		/* anon COW */
    566 		opg = oanon->an_page;
    567 		KASSERT(opg != NULL);
    568 		KASSERT(opg->uobject == NULL || opg->loan_count > 0);
    569 	} else if (uobjpage != PGO_DONTCARE) {
    570 		/* object-backed COW */
    571 		opg = uobjpage;
    572 		if ((uobjpage->flags & PG_BUSY) != 0) {
    573 			KASSERT(rw_write_held(opg->uobject->vmobjlock));
    574 		} else {
    575 			KASSERT(rw_read_held(opg->uobject->vmobjlock));
    576 		}
    577 	} else {
    578 		/* ZFOD */
    579 		opg = NULL;
    580 	}
    581 	if (opg != NULL) {
    582 		uobj = opg->uobject;
    583 	} else {
    584 		uobj = NULL;
    585 	}
    586 
    587 	KASSERT(amap != NULL);
    588 	KASSERT(uobjpage != NULL);
    589 	KASSERT(rw_write_held(amap->am_lock));
    590 	KASSERT(oanon == NULL || amap->am_lock == oanon->an_lock);
    591 	KASSERT(uobj == NULL || rw_lock_held(uobj->vmobjlock));
    592 
    593 	if (*spare != NULL) {
    594 		anon = *spare;
    595 		*spare = NULL;
    596 	} else {
    597 		anon = uvm_analloc();
    598 	}
    599 	if (anon) {
    600 
    601 		/*
    602 		 * The new anon is locked.
    603 		 *
    604 		 * if opg == NULL, we want a zero'd, dirty page,
    605 		 * so have uvm_pagealloc() do that for us.
    606 		 */
    607 
    608 		KASSERT(anon->an_lock == NULL);
    609 		anon->an_lock = amap->am_lock;
    610 		pg = uvm_pagealloc(NULL, ufi->orig_rvaddr, anon,
    611 		    UVM_FLAG_COLORMATCH | (opg == NULL ? UVM_PGA_ZERO : 0));
    612 		if (pg == NULL) {
    613 			anon->an_lock = NULL;
    614 		}
    615 	} else {
    616 		pg = NULL;
    617 	}
    618 
    619 	/*
    620 	 * out of memory resources?
    621 	 */
    622 
    623 	if (pg == NULL) {
    624 		/* save anon for the next try. */
    625 		if (anon != NULL) {
    626 			*spare = anon;
    627 		}
    628 
    629 		/* unlock and fail ... */
    630 		if (uobjpage != PGO_DONTCARE &&
    631 		    (uobjpage->flags & PG_BUSY) != 0) {
    632 			uvm_page_unbusy(&uobjpage, 1);
    633 		}
    634 		uvmfault_unlockall(ufi, amap, uobj);
    635 		if (!uvm_reclaimable()) {
    636 			UVMHIST_LOG(maphist, "out of VM", 0,0,0,0);
    637 			cpu_count(CPU_COUNT_FLTNOANON, 1);
    638 			error = ENOMEM;
    639 			goto done;
    640 		}
    641 
    642 		UVMHIST_LOG(maphist, "out of RAM, waiting for more", 0,0,0,0);
    643 		cpu_count(CPU_COUNT_FLTNORAM, 1);
    644 		uvm_wait("flt_noram5");
    645 		error = ERESTART;
    646 		goto done;
    647 	}
    648 
    649 	/* copy page [pg now dirty] */
    650 	if (opg) {
    651 		uvm_pagecopy(opg, pg);
    652 	}
    653 	KASSERT(uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_DIRTY);
    654 
    655 	amap_add(&ufi->entry->aref, ufi->orig_rvaddr - ufi->entry->start, anon,
    656 	    oanon != NULL);
    657 
    658 	*nanon = anon;
    659 	error = 0;
    660 done:
    661 	return error;
    662 }
    663 
    664 /*
    665  * Update statistics after fault resolution.
    666  * - maxrss
    667  */
    668 void
    669 uvmfault_update_stats(struct uvm_faultinfo *ufi)
    670 {
    671 	struct vm_map		*map;
    672 	struct vmspace 		*vm;
    673 	struct proc		*p;
    674 	vsize_t			 res;
    675 
    676 	map = ufi->orig_map;
    677 
    678 	p = curproc;
    679 	KASSERT(p != NULL);
    680 	vm = p->p_vmspace;
    681 
    682 	if (&vm->vm_map != map)
    683 		return;
    684 
    685 	res = pmap_resident_count(map->pmap);
    686 	if (vm->vm_rssmax < res)
    687 		vm->vm_rssmax = res;
    688 }
    689 
    690 /*
    691  *   F A U L T   -   m a i n   e n t r y   p o i n t
    692  */
    693 
    694 /*
    695  * uvm_fault: page fault handler
    696  *
    697  * => called from MD code to resolve a page fault
    698  * => VM data structures usually should be unlocked.   however, it is
    699  *	possible to call here with the main map locked if the caller
    700  *	gets a write lock, sets it recusive, and then calls us (c.f.
    701  *	uvm_map_pageable).   this should be avoided because it keeps
    702  *	the map locked off during I/O.
    703  * => MUST NEVER BE CALLED IN INTERRUPT CONTEXT
    704  */
    705 
    706 #define MASK(entry)     (UVM_ET_ISCOPYONWRITE(entry) ? \
    707 			 ~VM_PROT_WRITE : VM_PROT_ALL)
    708 
    709 /* fault_flag values passed from uvm_fault_wire to uvm_fault_internal */
    710 #define UVM_FAULT_WIRE		(1 << 0)
    711 #define UVM_FAULT_MAXPROT	(1 << 1)
    712 
    713 struct uvm_faultctx {
    714 
    715 	/*
    716 	 * the following members are set up by uvm_fault_check() and
    717 	 * read-only after that.
    718 	 *
    719 	 * note that narrow is used by uvm_fault_check() to change
    720 	 * the behaviour after ERESTART.
    721 	 *
    722 	 * most of them might change after RESTART if the underlying
    723 	 * map entry has been changed behind us.  an exception is
    724 	 * wire_paging, which does never change.
    725 	 */
    726 	vm_prot_t access_type;
    727 	vaddr_t startva;
    728 	int npages;
    729 	int centeridx;
    730 	bool narrow;		/* work on a single requested page only */
    731 	bool wire_mapping;	/* request a PMAP_WIRED mapping
    732 				   (UVM_FAULT_WIRE or VM_MAPENT_ISWIRED) */
    733 	bool wire_paging;	/* request uvm_pagewire
    734 				   (true for UVM_FAULT_WIRE) */
    735 	bool cow_now;		/* VM_PROT_WRITE is actually requested
    736 				   (ie. should break COW and page loaning) */
    737 
    738 	/*
    739 	 * enter_prot is set up by uvm_fault_check() and clamped
    740 	 * (ie. drop the VM_PROT_WRITE bit) in various places in case
    741 	 * of !cow_now.
    742 	 */
    743 	vm_prot_t enter_prot;	/* prot at which we want to enter pages in */
    744 
    745 	/*
    746 	 * the following member is for uvmfault_promote() and ERESTART.
    747 	 */
    748 	struct vm_anon *anon_spare;
    749 
    750 	/*
    751 	 * the folloing is actually a uvm_fault_lower() internal.
    752 	 * it's here merely for debugging.
    753 	 * (or due to the mechanical separation of the function?)
    754 	 */
    755 	bool promote;
    756 
    757 	/*
    758 	 * type of lock to acquire on objects in both layers.
    759 	 */
    760 	krw_t lower_lock_type;
    761 	krw_t upper_lock_type;
    762 };
    763 
    764 static inline int	uvm_fault_check(
    765 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    766 			    struct vm_anon ***, bool);
    767 
    768 static int		uvm_fault_upper(
    769 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    770 			    struct vm_anon **);
    771 static inline int	uvm_fault_upper_lookup(
    772 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    773 			    struct vm_anon **, struct vm_page **);
    774 static inline void	uvm_fault_upper_neighbor(
    775 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    776 			    vaddr_t, struct vm_page *, bool);
    777 static inline int	uvm_fault_upper_loan(
    778 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    779 			    struct vm_anon *, struct uvm_object **);
    780 static inline int	uvm_fault_upper_promote(
    781 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    782 			    struct uvm_object *, struct vm_anon *);
    783 static inline int	uvm_fault_upper_direct(
    784 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    785 			    struct uvm_object *, struct vm_anon *);
    786 static int		uvm_fault_upper_enter(
    787 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    788 			    struct uvm_object *, struct vm_anon *,
    789 			    struct vm_page *, struct vm_anon *);
    790 static inline void	uvm_fault_upper_done(
    791 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    792 			    struct vm_anon *, struct vm_page *);
    793 
    794 static int		uvm_fault_lower(
    795 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    796 			    struct vm_page **);
    797 static inline void	uvm_fault_lower_lookup(
    798 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    799 			    struct vm_page **);
    800 static inline void	uvm_fault_lower_neighbor(
    801 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    802 			    vaddr_t, struct vm_page *);
    803 static inline int	uvm_fault_lower_io(
    804 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    805 			    struct uvm_object **, struct vm_page **);
    806 static inline int	uvm_fault_lower_direct(
    807 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    808 			    struct uvm_object *, struct vm_page *);
    809 static inline int	uvm_fault_lower_direct_loan(
    810 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    811 			    struct uvm_object *, struct vm_page **,
    812 			    struct vm_page **);
    813 static inline int	uvm_fault_lower_promote(
    814 			    struct uvm_faultinfo *, struct uvm_faultctx *,
    815 			    struct uvm_object *, struct vm_page *);
    816 static int		uvm_fault_lower_enter(
    817 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    818 			    struct uvm_object *,
    819 			    struct vm_anon *, struct vm_page *);
    820 static inline void	uvm_fault_lower_done(
    821 			    struct uvm_faultinfo *, const struct uvm_faultctx *,
    822 			    struct uvm_object *, struct vm_page *);
    823 
    824 int
    825 uvm_fault_internal(struct vm_map *orig_map, vaddr_t vaddr,
    826     vm_prot_t access_type, int fault_flag)
    827 {
    828 	struct uvm_faultinfo ufi;
    829 	struct uvm_faultctx flt = {
    830 		.access_type = access_type,
    831 
    832 		/* don't look for neighborhood * pages on "wire" fault */
    833 		.narrow = (fault_flag & UVM_FAULT_WIRE) != 0,
    834 
    835 		/* "wire" fault causes wiring of both mapping and paging */
    836 		.wire_mapping = (fault_flag & UVM_FAULT_WIRE) != 0,
    837 		.wire_paging = (fault_flag & UVM_FAULT_WIRE) != 0,
    838 
    839 		/*
    840 		 * default lock type to acquire on upper & lower layer
    841 		 * objects: reader.  this can be upgraded at any point
    842 		 * during the fault from read -> write and uvm_faultctx
    843 		 * changed to match, but is never downgraded write -> read.
    844 		 */
    845 #ifdef __HAVE_UNLOCKED_PMAP /* XXX temporary */
    846 		.upper_lock_type = RW_WRITER,
    847 		.lower_lock_type = RW_WRITER,
    848 #else
    849 		.upper_lock_type = RW_READER,
    850 		.lower_lock_type = RW_READER,
    851 #endif
    852 	};
    853 	const bool maxprot = (fault_flag & UVM_FAULT_MAXPROT) != 0;
    854 	struct vm_anon *anons_store[UVM_MAXRANGE], **anons;
    855 	struct vm_page *pages_store[UVM_MAXRANGE], **pages;
    856 	int error;
    857 
    858 	UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
    859 
    860 	UVMHIST_LOG(maphist, "(map=%#jx, vaddr=%#jx, at=%jd, ff=%jd)",
    861 	      (uintptr_t)orig_map, vaddr, access_type, fault_flag);
    862 
    863 	/* Don't count anything until user interaction is possible */
    864 	kpreempt_disable();
    865 	if (__predict_true(start_init_exec)) {
    866 		struct cpu_info *ci = curcpu();
    867 		CPU_COUNT(CPU_COUNT_NFAULT, 1);
    868 		/* Don't flood RNG subsystem with samples. */
    869 		if (++(ci->ci_faultrng) == 503) {
    870 			ci->ci_faultrng = 0;
    871 			rnd_add_uint32(&curcpu()->ci_data.cpu_uvm->rs,
    872 			    sizeof(vaddr_t) == sizeof(uint32_t) ?
    873 			    (uint32_t)vaddr : sizeof(vaddr_t) ==
    874 			    sizeof(uint64_t) ?
    875 			    (uint32_t)vaddr :
    876 			    (uint32_t)ci->ci_counts[CPU_COUNT_NFAULT]);
    877 		}
    878 	}
    879 	kpreempt_enable();
    880 
    881 	/*
    882 	 * init the IN parameters in the ufi
    883 	 */
    884 
    885 	ufi.orig_map = orig_map;
    886 	ufi.orig_rvaddr = trunc_page(vaddr);
    887 	ufi.orig_size = PAGE_SIZE;	/* can't get any smaller than this */
    888 
    889 	error = ERESTART;
    890 	while (error == ERESTART) { /* ReFault: */
    891 		anons = anons_store;
    892 		pages = pages_store;
    893 
    894 		error = uvm_fault_check(&ufi, &flt, &anons, maxprot);
    895 		if (error != 0)
    896 			continue;
    897 
    898 		error = uvm_fault_upper_lookup(&ufi, &flt, anons, pages);
    899 		if (error != 0)
    900 			continue;
    901 
    902 		if (pages[flt.centeridx] == PGO_DONTCARE)
    903 			error = uvm_fault_upper(&ufi, &flt, anons);
    904 		else {
    905 			struct uvm_object * const uobj =
    906 			    ufi.entry->object.uvm_obj;
    907 
    908 			if (uobj && uobj->pgops->pgo_fault != NULL) {
    909 				/*
    910 				 * invoke "special" fault routine.
    911 				 */
    912 				rw_enter(uobj->vmobjlock, RW_WRITER);
    913 				/* locked: maps(read), amap(if there), uobj */
    914 				error = uobj->pgops->pgo_fault(&ufi,
    915 				    flt.startva, pages, flt.npages,
    916 				    flt.centeridx, flt.access_type,
    917 				    PGO_LOCKED|PGO_SYNCIO);
    918 
    919 				/*
    920 				 * locked: nothing, pgo_fault has unlocked
    921 				 * everything
    922 				 */
    923 
    924 				/*
    925 				 * object fault routine responsible for
    926 				 * pmap_update().
    927 				 */
    928 
    929 				/*
    930 				 * Wake up the pagedaemon if the fault method
    931 				 * failed for lack of memory but some can be
    932 				 * reclaimed.
    933 				 */
    934 				if (error == ENOMEM && uvm_reclaimable()) {
    935 					uvm_wait("pgo_fault");
    936 					error = ERESTART;
    937 				}
    938 			} else {
    939 				error = uvm_fault_lower(&ufi, &flt, pages);
    940 			}
    941 		}
    942 	}
    943 
    944 	if (flt.anon_spare != NULL) {
    945 		flt.anon_spare->an_ref--;
    946 		KASSERT(flt.anon_spare->an_ref == 0);
    947 		KASSERT(flt.anon_spare->an_lock == NULL);
    948 		uvm_anfree(flt.anon_spare);
    949 	}
    950 	return error;
    951 }
    952 
    953 /*
    954  * uvm_fault_check: check prot, handle needs-copy, etc.
    955  *
    956  *	1. lookup entry.
    957  *	2. check protection.
    958  *	3. adjust fault condition (mainly for simulated fault).
    959  *	4. handle needs-copy (lazy amap copy).
    960  *	5. establish range of interest for neighbor fault (aka pre-fault).
    961  *	6. look up anons (if amap exists).
    962  *	7. flush pages (if MADV_SEQUENTIAL)
    963  *
    964  * => called with nothing locked.
    965  * => if we fail (result != 0) we unlock everything.
    966  * => initialize/adjust many members of flt.
    967  */
    968 
    969 static int
    970 uvm_fault_check(
    971 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
    972 	struct vm_anon ***ranons, bool maxprot)
    973 {
    974 	struct vm_amap *amap;
    975 	struct uvm_object *uobj;
    976 	vm_prot_t check_prot;
    977 	int nback, nforw;
    978 	UVMHIST_FUNC("uvm_fault_check"); UVMHIST_CALLED(maphist);
    979 
    980 	/*
    981 	 * lookup and lock the maps
    982 	 */
    983 
    984 	if (uvmfault_lookup(ufi, false) == false) {
    985 		UVMHIST_LOG(maphist, "<- no mapping @ %#jx", ufi->orig_rvaddr,
    986 		    0,0,0);
    987 		return EFAULT;
    988 	}
    989 	/* locked: maps(read) */
    990 
    991 #ifdef DIAGNOSTIC
    992 	if ((ufi->map->flags & VM_MAP_PAGEABLE) == 0) {
    993 		printf("Page fault on non-pageable map:\n");
    994 		printf("ufi->map = %p\n", ufi->map);
    995 		printf("ufi->orig_map = %p\n", ufi->orig_map);
    996 		printf("ufi->orig_rvaddr = %#lx\n", (u_long) ufi->orig_rvaddr);
    997 		panic("uvm_fault: (ufi->map->flags & VM_MAP_PAGEABLE) == 0");
    998 	}
    999 #endif
   1000 
   1001 	/*
   1002 	 * check protection
   1003 	 */
   1004 
   1005 	check_prot = maxprot ?
   1006 	    ufi->entry->max_protection : ufi->entry->protection;
   1007 	if ((check_prot & flt->access_type) != flt->access_type) {
   1008 		UVMHIST_LOG(maphist,
   1009 		    "<- protection failure (prot=%#jx, access=%#jx)",
   1010 		    ufi->entry->protection, flt->access_type, 0, 0);
   1011 		uvmfault_unlockmaps(ufi, false);
   1012 		return EFAULT;
   1013 	}
   1014 
   1015 	/*
   1016 	 * "enter_prot" is the protection we want to enter the page in at.
   1017 	 * for certain pages (e.g. copy-on-write pages) this protection can
   1018 	 * be more strict than ufi->entry->protection.  "wired" means either
   1019 	 * the entry is wired or we are fault-wiring the pg.
   1020 	 */
   1021 
   1022 	flt->enter_prot = ufi->entry->protection;
   1023 	if (VM_MAPENT_ISWIRED(ufi->entry)) {
   1024 		flt->wire_mapping = true;
   1025 		flt->wire_paging = true;
   1026 		flt->narrow = true;
   1027 	}
   1028 
   1029 	if (flt->wire_mapping) {
   1030 		flt->access_type = flt->enter_prot; /* full access for wired */
   1031 		flt->cow_now = (check_prot & VM_PROT_WRITE) != 0;
   1032 	} else {
   1033 		flt->cow_now = (flt->access_type & VM_PROT_WRITE) != 0;
   1034 	}
   1035 
   1036 	if (flt->wire_paging) {
   1037 		/* wiring pages requires a write lock. */
   1038 		flt->upper_lock_type = RW_WRITER;
   1039 		flt->lower_lock_type = RW_WRITER;
   1040 	}
   1041 
   1042 	flt->promote = false;
   1043 
   1044 	/*
   1045 	 * handle "needs_copy" case.   if we need to copy the amap we will
   1046 	 * have to drop our readlock and relock it with a write lock.  (we
   1047 	 * need a write lock to change anything in a map entry [e.g.
   1048 	 * needs_copy]).
   1049 	 */
   1050 
   1051 	if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
   1052 		if (flt->cow_now || (ufi->entry->object.uvm_obj == NULL)) {
   1053 			KASSERT(!maxprot);
   1054 			/* need to clear */
   1055 			UVMHIST_LOG(maphist,
   1056 			    "  need to clear needs_copy and refault",0,0,0,0);
   1057 			uvmfault_unlockmaps(ufi, false);
   1058 			uvmfault_amapcopy(ufi);
   1059 			cpu_count(CPU_COUNT_FLTAMCOPY, 1);
   1060 			return ERESTART;
   1061 
   1062 		} else {
   1063 
   1064 			/*
   1065 			 * ensure that we pmap_enter page R/O since
   1066 			 * needs_copy is still true
   1067 			 */
   1068 
   1069 			flt->enter_prot &= ~VM_PROT_WRITE;
   1070 		}
   1071 	}
   1072 
   1073 	/*
   1074 	 * identify the players
   1075 	 */
   1076 
   1077 	amap = ufi->entry->aref.ar_amap;	/* upper layer */
   1078 	uobj = ufi->entry->object.uvm_obj;	/* lower layer */
   1079 
   1080 	/*
   1081 	 * check for a case 0 fault.  if nothing backing the entry then
   1082 	 * error now.
   1083 	 */
   1084 
   1085 	if (amap == NULL && uobj == NULL) {
   1086 		uvmfault_unlockmaps(ufi, false);
   1087 		UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
   1088 		return EFAULT;
   1089 	}
   1090 
   1091 	/*
   1092 	 * establish range of interest based on advice from mapper
   1093 	 * and then clip to fit map entry.   note that we only want
   1094 	 * to do this the first time through the fault.   if we
   1095 	 * ReFault we will disable this by setting "narrow" to true.
   1096 	 */
   1097 
   1098 	if (flt->narrow == false) {
   1099 
   1100 		/* wide fault (!narrow) */
   1101 		KASSERT(uvmadvice[ufi->entry->advice].advice ==
   1102 			 ufi->entry->advice);
   1103 		nback = MIN(uvmadvice[ufi->entry->advice].nback,
   1104 		    (ufi->orig_rvaddr - ufi->entry->start) >> PAGE_SHIFT);
   1105 		flt->startva = ufi->orig_rvaddr - (nback << PAGE_SHIFT);
   1106 		/*
   1107 		 * note: "-1" because we don't want to count the
   1108 		 * faulting page as forw
   1109 		 */
   1110 		nforw = MIN(uvmadvice[ufi->entry->advice].nforw,
   1111 			    ((ufi->entry->end - ufi->orig_rvaddr) >>
   1112 			     PAGE_SHIFT) - 1);
   1113 		flt->npages = nback + nforw + 1;
   1114 		flt->centeridx = nback;
   1115 
   1116 		flt->narrow = true;	/* ensure only once per-fault */
   1117 
   1118 	} else {
   1119 
   1120 		/* narrow fault! */
   1121 		nback = nforw = 0;
   1122 		flt->startva = ufi->orig_rvaddr;
   1123 		flt->npages = 1;
   1124 		flt->centeridx = 0;
   1125 
   1126 	}
   1127 	/* offset from entry's start to pgs' start */
   1128 	const voff_t eoff = flt->startva - ufi->entry->start;
   1129 
   1130 	/* locked: maps(read) */
   1131 	UVMHIST_LOG(maphist, "  narrow=%jd, back=%jd, forw=%jd, startva=%#jx",
   1132 		    flt->narrow, nback, nforw, flt->startva);
   1133 	UVMHIST_LOG(maphist, "  entry=%#jx, amap=%#jx, obj=%#jx",
   1134 	    (uintptr_t)ufi->entry, (uintptr_t)amap, (uintptr_t)uobj, 0);
   1135 
   1136 	/*
   1137 	 * guess at the most suitable lock types to acquire.
   1138 	 * if we've got an amap then lock it and extract current anons.
   1139 	 */
   1140 
   1141 	if (amap) {
   1142 		if ((amap_flags(amap) & AMAP_SHARED) == 0) {
   1143 			/*
   1144 			 * the amap isn't shared.  get a writer lock to
   1145 			 * avoid the cost of upgrading the lock later if
   1146 			 * needed.
   1147 			 *
   1148 			 * XXX nice for PostgreSQL, but consider threads.
   1149 			 */
   1150 			flt->upper_lock_type = RW_WRITER;
   1151 		} else if ((flt->access_type & VM_PROT_WRITE) != 0) {
   1152 			/*
   1153 			 * assume we're about to COW.
   1154 			 */
   1155 			flt->upper_lock_type = RW_WRITER;
   1156 		}
   1157 		amap_lock(amap, flt->upper_lock_type);
   1158 		amap_lookups(&ufi->entry->aref, eoff, *ranons, flt->npages);
   1159 	} else {
   1160 		if ((flt->access_type & VM_PROT_WRITE) != 0) {
   1161 			/*
   1162 			 * we are about to dirty the object and that
   1163 			 * requires a write lock.
   1164 			 */
   1165 			flt->lower_lock_type = RW_WRITER;
   1166 		}
   1167 		*ranons = NULL;	/* to be safe */
   1168 	}
   1169 
   1170 	/* locked: maps(read), amap(if there) */
   1171 	KASSERT(amap == NULL ||
   1172 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1173 
   1174 	/*
   1175 	 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
   1176 	 * now and then forget about them (for the rest of the fault).
   1177 	 */
   1178 
   1179 	if (ufi->entry->advice == MADV_SEQUENTIAL && nback != 0) {
   1180 
   1181 		UVMHIST_LOG(maphist, "  MADV_SEQUENTIAL: flushing backpages",
   1182 		    0,0,0,0);
   1183 		/* flush back-page anons? */
   1184 		if (amap)
   1185 			uvmfault_anonflush(*ranons, nback);
   1186 
   1187 		/* flush object? */
   1188 		if (uobj) {
   1189 			voff_t uoff;
   1190 
   1191 			uoff = ufi->entry->offset + eoff;
   1192 			rw_enter(uobj->vmobjlock, RW_WRITER);
   1193 			(void) (uobj->pgops->pgo_put)(uobj, uoff, uoff +
   1194 				    (nback << PAGE_SHIFT), PGO_DEACTIVATE);
   1195 		}
   1196 
   1197 		/* now forget about the backpages */
   1198 		if (amap)
   1199 			*ranons += nback;
   1200 		flt->startva += (nback << PAGE_SHIFT);
   1201 		flt->npages -= nback;
   1202 		flt->centeridx = 0;
   1203 	}
   1204 	/*
   1205 	 * => startva is fixed
   1206 	 * => npages is fixed
   1207 	 */
   1208 	KASSERT(flt->startva <= ufi->orig_rvaddr);
   1209 	KASSERT(ufi->orig_rvaddr + ufi->orig_size <=
   1210 	    flt->startva + (flt->npages << PAGE_SHIFT));
   1211 	return 0;
   1212 }
   1213 
   1214 /*
   1215  * uvm_fault_upper_upgrade: upgrade upper lock, reader -> writer
   1216  */
   1217 
   1218 static inline int
   1219 uvm_fault_upper_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1220     struct vm_amap *amap, struct uvm_object *uobj)
   1221 {
   1222 
   1223 	KASSERT(amap != NULL);
   1224 	KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
   1225 
   1226 	/*
   1227 	 * fast path.
   1228 	 */
   1229 
   1230 	if (__predict_true(flt->upper_lock_type == RW_WRITER)) {
   1231 		return 0;
   1232 	}
   1233 
   1234 	/*
   1235 	 * otherwise try for the upgrade.  if we don't get it, unlock
   1236 	 * everything, restart the fault and next time around get a writer
   1237 	 * lock.
   1238 	 */
   1239 
   1240 	flt->upper_lock_type = RW_WRITER;
   1241 	if (__predict_false(!rw_tryupgrade(amap->am_lock))) {
   1242 		uvmfault_unlockall(ufi, amap, uobj);
   1243 		cpu_count(CPU_COUNT_FLTNOUP, 1);
   1244 		UVMHIST_LOG(maphist, "  !upgrade upper", 0, 0,0,0);
   1245 		return ERESTART;
   1246 	}
   1247 	cpu_count(CPU_COUNT_FLTUP, 1);
   1248 	KASSERT(flt->upper_lock_type == rw_lock_op(amap->am_lock));
   1249 	return 0;
   1250 }
   1251 
   1252 /*
   1253  * uvm_fault_upper_lookup: look up existing h/w mapping and amap.
   1254  *
   1255  * iterate range of interest:
   1256  *	1. check if h/w mapping exists.  if yes, we don't care
   1257  *	2. check if anon exists.  if not, page is lower.
   1258  *	3. if anon exists, enter h/w mapping for neighbors.
   1259  *
   1260  * => called with amap locked (if exists).
   1261  */
   1262 
   1263 static int
   1264 uvm_fault_upper_lookup(
   1265 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   1266 	struct vm_anon **anons, struct vm_page **pages)
   1267 {
   1268 	struct vm_amap *amap = ufi->entry->aref.ar_amap;
   1269 	int lcv;
   1270 	vaddr_t currva;
   1271 	bool shadowed __unused;
   1272 	bool entered;
   1273 	UVMHIST_FUNC("uvm_fault_upper_lookup"); UVMHIST_CALLED(maphist);
   1274 
   1275 	/* locked: maps(read), amap(if there) */
   1276 	KASSERT(amap == NULL ||
   1277 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1278 
   1279 	/*
   1280 	 * map in the backpages and frontpages we found in the amap in hopes
   1281 	 * of preventing future faults.    we also init the pages[] array as
   1282 	 * we go.
   1283 	 */
   1284 
   1285 	currva = flt->startva;
   1286 	shadowed = false;
   1287 	entered = false;
   1288 	for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
   1289 		/*
   1290 		 * unmapped or center page.   check if any anon at this level.
   1291 		 */
   1292 		if (amap == NULL || anons[lcv] == NULL) {
   1293 			pages[lcv] = NULL;
   1294 			continue;
   1295 		}
   1296 
   1297 		/*
   1298 		 * check for present page and map if possible.
   1299 		 */
   1300 
   1301 		pages[lcv] = PGO_DONTCARE;
   1302 		if (lcv == flt->centeridx) {	/* save center for later! */
   1303 			shadowed = true;
   1304 			continue;
   1305 		}
   1306 
   1307 		struct vm_anon *anon = anons[lcv];
   1308 		struct vm_page *pg = anon->an_page;
   1309 
   1310 		KASSERT(anon->an_lock == amap->am_lock);
   1311 
   1312 		/*
   1313 		 * ignore loaned and busy pages.
   1314 		 * don't play with VAs that are already mapped.
   1315 		 */
   1316 
   1317 		if (pg && pg->loan_count == 0 && (pg->flags & PG_BUSY) == 0 &&
   1318 		    !pmap_extract(ufi->orig_map->pmap, currva, NULL)) {
   1319 			uvm_fault_upper_neighbor(ufi, flt, currva,
   1320 			    pg, anon->an_ref > 1);
   1321 			entered = true;
   1322 		}
   1323 	}
   1324 	if (entered) {
   1325 		pmap_update(ufi->orig_map->pmap);
   1326 	}
   1327 
   1328 	/* locked: maps(read), amap(if there) */
   1329 	KASSERT(amap == NULL ||
   1330 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1331 	/* (shadowed == true) if there is an anon at the faulting address */
   1332 	UVMHIST_LOG(maphist, "  shadowed=%jd, will_get=%jd", shadowed,
   1333 	    (ufi->entry->object.uvm_obj && shadowed != false),0,0);
   1334 
   1335 	/*
   1336 	 * note that if we are really short of RAM we could sleep in the above
   1337 	 * call to pmap_enter with everything locked.   bad?
   1338 	 *
   1339 	 * XXX Actually, that is bad; pmap_enter() should just fail in that
   1340 	 * XXX case.  --thorpej
   1341 	 */
   1342 
   1343 	return 0;
   1344 }
   1345 
   1346 /*
   1347  * uvm_fault_upper_neighbor: enter single upper neighbor page.
   1348  *
   1349  * => called with amap and anon locked.
   1350  */
   1351 
   1352 static void
   1353 uvm_fault_upper_neighbor(
   1354 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   1355 	vaddr_t currva, struct vm_page *pg, bool readonly)
   1356 {
   1357 	UVMHIST_FUNC("uvm_fault_upper_neighbor"); UVMHIST_CALLED(maphist);
   1358 
   1359 	/* locked: amap, anon */
   1360 
   1361 	KASSERT(pg->uobject == NULL);
   1362 	KASSERT(pg->uanon != NULL);
   1363 	KASSERT(rw_lock_op(pg->uanon->an_lock) == flt->upper_lock_type);
   1364 	KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
   1365 
   1366 	/*
   1367 	 * in the read-locked case, it's not possible for this to be a new
   1368 	 * page, therefore it's enqueued already.  there wasn't a direct
   1369 	 * fault on the page, so avoid the cost of re-enqueuing it unless
   1370 	 * write-locked.
   1371 	 */
   1372 
   1373 	if (flt->upper_lock_type == RW_WRITER) {
   1374 		uvm_pagelock(pg);
   1375 		uvm_pageenqueue(pg);
   1376 		uvm_pageunlock(pg);
   1377 	}
   1378 	UVMHIST_LOG(maphist,
   1379 	    "  MAPPING: n anon: pm=%#jx, va=%#jx, pg=%#jx",
   1380 	    (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
   1381 	cpu_count(CPU_COUNT_FLTNAMAP, 1);
   1382 
   1383 	/*
   1384 	 * Since this page isn't the page that's actually faulting,
   1385 	 * ignore pmap_enter() failures; it's not critical that we
   1386 	 * enter these right now.
   1387 	 */
   1388 
   1389 	(void) pmap_enter(ufi->orig_map->pmap, currva,
   1390 	    VM_PAGE_TO_PHYS(pg),
   1391 	    readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
   1392 	    flt->enter_prot,
   1393 	    PMAP_CANFAIL | (flt->wire_mapping ? PMAP_WIRED : 0));
   1394 }
   1395 
   1396 /*
   1397  * uvm_fault_upper: handle upper fault.
   1398  *
   1399  *	1. acquire anon lock.
   1400  *	2. get anon.  let uvmfault_anonget do the dirty work.
   1401  *	3. handle loan.
   1402  *	4. dispatch direct or promote handlers.
   1403  */
   1404 
   1405 static int
   1406 uvm_fault_upper(
   1407 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1408 	struct vm_anon **anons)
   1409 {
   1410 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   1411 	struct vm_anon * const anon = anons[flt->centeridx];
   1412 	struct uvm_object *uobj;
   1413 	int error;
   1414 	UVMHIST_FUNC("uvm_fault_upper"); UVMHIST_CALLED(maphist);
   1415 
   1416 	/* locked: maps(read), amap, anon */
   1417 	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1418 	KASSERT(anon->an_lock == amap->am_lock);
   1419 
   1420 	/*
   1421 	 * handle case 1: fault on an anon in our amap
   1422 	 */
   1423 
   1424 	UVMHIST_LOG(maphist, "  case 1 fault: anon=%#jx",
   1425 	    (uintptr_t)anon, 0, 0, 0);
   1426 
   1427 	/*
   1428 	 * no matter if we have case 1A or case 1B we are going to need to
   1429 	 * have the anon's memory resident.   ensure that now.
   1430 	 */
   1431 
   1432 	/*
   1433 	 * let uvmfault_anonget do the dirty work.
   1434 	 * if it fails (!OK) it will unlock everything for us.
   1435 	 * if it succeeds, locks are still valid and locked.
   1436 	 * also, if it is OK, then the anon's page is on the queues.
   1437 	 * if the page is on loan from a uvm_object, then anonget will
   1438 	 * lock that object for us if it does not fail.
   1439 	 */
   1440  retry:
   1441 	error = uvmfault_anonget(ufi, amap, anon);
   1442 	switch (error) {
   1443 	case 0:
   1444 		break;
   1445 
   1446 	case ERESTART:
   1447 		return ERESTART;
   1448 
   1449 	case EAGAIN:
   1450 		kpause("fltagain1", false, hz/2, NULL);
   1451 		return ERESTART;
   1452 
   1453 	case ENOLCK:
   1454 		/* it needs a write lock: retry */
   1455 		error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
   1456 		if (error != 0) {
   1457 			return error;
   1458 		}
   1459 		KASSERT(rw_write_held(amap->am_lock));
   1460 		goto retry;
   1461 
   1462 	default:
   1463 		return error;
   1464 	}
   1465 
   1466 	/*
   1467 	 * uobj is non null if the page is on loan from an object (i.e. uobj)
   1468 	 */
   1469 
   1470 	uobj = anon->an_page->uobject;	/* locked by anonget if !NULL */
   1471 
   1472 	/* locked: maps(read), amap, anon, uobj(if one) */
   1473 	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1474 	KASSERT(anon->an_lock == amap->am_lock);
   1475 	KASSERT(uobj == NULL ||
   1476 	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
   1477 
   1478 	/*
   1479 	 * special handling for loaned pages
   1480 	 */
   1481 
   1482 	if (anon->an_page->loan_count) {
   1483 		error = uvm_fault_upper_loan(ufi, flt, anon, &uobj);
   1484 		if (error != 0)
   1485 			return error;
   1486 	}
   1487 
   1488 	/*
   1489 	 * if we are case 1B then we will need to allocate a new blank
   1490 	 * anon to transfer the data into.   note that we have a lock
   1491 	 * on anon, so no one can busy or release the page until we are done.
   1492 	 * also note that the ref count can't drop to zero here because
   1493 	 * it is > 1 and we are only dropping one ref.
   1494 	 *
   1495 	 * in the (hopefully very rare) case that we are out of RAM we
   1496 	 * will unlock, wait for more RAM, and refault.
   1497 	 *
   1498 	 * if we are out of anon VM we kill the process (XXX: could wait?).
   1499 	 */
   1500 
   1501 	if (flt->cow_now && anon->an_ref > 1) {
   1502 		flt->promote = true;
   1503 		error = uvm_fault_upper_promote(ufi, flt, uobj, anon);
   1504 	} else {
   1505 		error = uvm_fault_upper_direct(ufi, flt, uobj, anon);
   1506 	}
   1507 	return error;
   1508 }
   1509 
   1510 /*
   1511  * uvm_fault_upper_loan: handle loaned upper page.
   1512  *
   1513  *	1. if not cow'ing now, simply adjust flt->enter_prot.
   1514  *	2. if cow'ing now, and if ref count is 1, break loan.
   1515  */
   1516 
   1517 static int
   1518 uvm_fault_upper_loan(
   1519 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1520 	struct vm_anon *anon, struct uvm_object **ruobj)
   1521 {
   1522 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   1523 	int error = 0;
   1524 	UVMHIST_FUNC("uvm_fault_upper_loan"); UVMHIST_CALLED(maphist);
   1525 
   1526 	if (!flt->cow_now) {
   1527 
   1528 		/*
   1529 		 * for read faults on loaned pages we just cap the
   1530 		 * protection at read-only.
   1531 		 */
   1532 
   1533 		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
   1534 
   1535 	} else {
   1536 		/*
   1537 		 * note that we can't allow writes into a loaned page!
   1538 		 *
   1539 		 * if we have a write fault on a loaned page in an
   1540 		 * anon then we need to look at the anon's ref count.
   1541 		 * if it is greater than one then we are going to do
   1542 		 * a normal copy-on-write fault into a new anon (this
   1543 		 * is not a problem).  however, if the reference count
   1544 		 * is one (a case where we would normally allow a
   1545 		 * write directly to the page) then we need to kill
   1546 		 * the loan before we continue.
   1547 		 */
   1548 
   1549 		/* >1 case is already ok */
   1550 		if (anon->an_ref == 1) {
   1551 			/* breaking loan requires a write lock. */
   1552 			error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
   1553 			if (error != 0) {
   1554 				return error;
   1555 			}
   1556 			KASSERT(rw_write_held(amap->am_lock));
   1557 
   1558 			error = uvm_loanbreak_anon(anon, *ruobj);
   1559 			if (error != 0) {
   1560 				uvmfault_unlockall(ufi, amap, *ruobj);
   1561 				uvm_wait("flt_noram2");
   1562 				return ERESTART;
   1563 			}
   1564 			/* if we were a loan receiver uobj is gone */
   1565 			if (*ruobj)
   1566 				*ruobj = NULL;
   1567 		}
   1568 	}
   1569 	return error;
   1570 }
   1571 
   1572 /*
   1573  * uvm_fault_upper_promote: promote upper page.
   1574  *
   1575  *	1. call uvmfault_promote.
   1576  *	2. enqueue page.
   1577  *	3. deref.
   1578  *	4. pass page to uvm_fault_upper_enter.
   1579  */
   1580 
   1581 static int
   1582 uvm_fault_upper_promote(
   1583 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1584 	struct uvm_object *uobj, struct vm_anon *anon)
   1585 {
   1586 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   1587 	struct vm_anon * const oanon = anon;
   1588 	struct vm_page *pg;
   1589 	int error;
   1590 	UVMHIST_FUNC("uvm_fault_upper_promote"); UVMHIST_CALLED(maphist);
   1591 
   1592 	UVMHIST_LOG(maphist, "  case 1B: COW fault",0,0,0,0);
   1593 	cpu_count(CPU_COUNT_FLT_ACOW, 1);
   1594 
   1595 	/* promoting requires a write lock. */
   1596 	error = uvm_fault_upper_upgrade(ufi, flt, amap, NULL);
   1597 	if (error != 0) {
   1598 		return error;
   1599 	}
   1600 	KASSERT(rw_write_held(amap->am_lock));
   1601 
   1602 	error = uvmfault_promote(ufi, oanon, PGO_DONTCARE, &anon,
   1603 	    &flt->anon_spare);
   1604 	switch (error) {
   1605 	case 0:
   1606 		break;
   1607 	case ERESTART:
   1608 		return ERESTART;
   1609 	default:
   1610 		return error;
   1611 	}
   1612 
   1613 	KASSERT(anon->an_lock == oanon->an_lock);
   1614 
   1615 	/* uvm_fault_upper_done will activate or enqueue the page */
   1616 	pg = anon->an_page;
   1617 	pg->flags &= ~(PG_BUSY|PG_FAKE);
   1618 	UVM_PAGE_OWN(pg, NULL);
   1619 
   1620 	/* deref: can not drop to zero here by defn! */
   1621 	KASSERT(oanon->an_ref > 1);
   1622 	oanon->an_ref--;
   1623 
   1624 	/*
   1625 	 * note: oanon is still locked, as is the new anon.  we
   1626 	 * need to check for this later when we unlock oanon; if
   1627 	 * oanon != anon, we'll have to unlock anon, too.
   1628 	 */
   1629 
   1630 	return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
   1631 }
   1632 
   1633 /*
   1634  * uvm_fault_upper_direct: handle direct fault.
   1635  */
   1636 
   1637 static int
   1638 uvm_fault_upper_direct(
   1639 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1640 	struct uvm_object *uobj, struct vm_anon *anon)
   1641 {
   1642 	struct vm_anon * const oanon = anon;
   1643 	struct vm_page *pg;
   1644 	UVMHIST_FUNC("uvm_fault_upper_direct"); UVMHIST_CALLED(maphist);
   1645 
   1646 	cpu_count(CPU_COUNT_FLT_ANON, 1);
   1647 	pg = anon->an_page;
   1648 	if (anon->an_ref > 1)     /* disallow writes to ref > 1 anons */
   1649 		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
   1650 
   1651 	return uvm_fault_upper_enter(ufi, flt, uobj, anon, pg, oanon);
   1652 }
   1653 
   1654 /*
   1655  * uvm_fault_upper_enter: enter h/w mapping of upper page.
   1656  */
   1657 
   1658 static int
   1659 uvm_fault_upper_enter(
   1660 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   1661 	struct uvm_object *uobj, struct vm_anon *anon, struct vm_page *pg,
   1662 	struct vm_anon *oanon)
   1663 {
   1664 	struct pmap *pmap = ufi->orig_map->pmap;
   1665 	vaddr_t va = ufi->orig_rvaddr;
   1666 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   1667 	UVMHIST_FUNC("uvm_fault_upper_enter"); UVMHIST_CALLED(maphist);
   1668 
   1669 	/* locked: maps(read), amap, oanon, anon(if different from oanon) */
   1670 	KASSERT(rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1671 	KASSERT(anon->an_lock == amap->am_lock);
   1672 	KASSERT(oanon->an_lock == amap->am_lock);
   1673 	KASSERT(uobj == NULL ||
   1674 	    rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
   1675 	KASSERT(uvm_pagegetdirty(pg) != UVM_PAGE_STATUS_CLEAN);
   1676 
   1677 	/*
   1678 	 * now map the page in.
   1679 	 */
   1680 
   1681 	UVMHIST_LOG(maphist,
   1682 	    "  MAPPING: anon: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
   1683 	    (uintptr_t)pmap, va, (uintptr_t)pg, flt->promote);
   1684 	if (pmap_enter(pmap, va, VM_PAGE_TO_PHYS(pg),
   1685 	    flt->enter_prot, flt->access_type | PMAP_CANFAIL |
   1686 	    (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
   1687 
   1688 		/*
   1689 		 * If pmap_enter() fails, it must not leave behind an existing
   1690 		 * pmap entry.  In particular, a now-stale entry for a different
   1691 		 * page would leave the pmap inconsistent with the vm_map.
   1692 		 * This is not to imply that pmap_enter() should remove an
   1693 		 * existing mapping in such a situation (since that could create
   1694 		 * different problems, eg. if the existing mapping is wired),
   1695 		 * but rather that the pmap should be designed such that it
   1696 		 * never needs to fail when the new mapping is replacing an
   1697 		 * existing mapping and the new page has no existing mappings.
   1698 		 */
   1699 
   1700 		KASSERT(!pmap_extract(pmap, va, NULL));
   1701 
   1702 		/*
   1703 		 * ensure that the page is queued in the case that
   1704 		 * we just promoted.
   1705 		 */
   1706 
   1707 		if (flt->upper_lock_type == RW_WRITER) {
   1708 			uvm_pagelock(pg);
   1709 			uvm_pageenqueue(pg);
   1710 			uvm_pageunlock(pg);
   1711 		}
   1712 
   1713 		/*
   1714 		 * No need to undo what we did; we can simply think of
   1715 		 * this as the pmap throwing away the mapping information.
   1716 		 *
   1717 		 * We do, however, have to go through the ReFault path,
   1718 		 * as the map may change while we're asleep.
   1719 		 */
   1720 
   1721 		uvmfault_unlockall(ufi, amap, uobj);
   1722 		if (!uvm_reclaimable()) {
   1723 			UVMHIST_LOG(maphist,
   1724 			    "<- failed.  out of VM",0,0,0,0);
   1725 			/* XXX instrumentation */
   1726 			return ENOMEM;
   1727 		}
   1728 		/* XXX instrumentation */
   1729 		uvm_wait("flt_pmfail1");
   1730 		return ERESTART;
   1731 	}
   1732 
   1733 	uvm_fault_upper_done(ufi, flt, anon, pg);
   1734 
   1735 	/*
   1736 	 * done case 1!  finish up by unlocking everything and returning success
   1737 	 */
   1738 
   1739 	pmap_update(pmap);
   1740 	uvmfault_unlockall(ufi, amap, uobj);
   1741 	return 0;
   1742 }
   1743 
   1744 /*
   1745  * uvm_fault_upper_done: queue upper center page.
   1746  */
   1747 
   1748 static void
   1749 uvm_fault_upper_done(
   1750 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   1751 	struct vm_anon *anon, struct vm_page *pg)
   1752 {
   1753 	const bool wire_paging = flt->wire_paging;
   1754 
   1755 	UVMHIST_FUNC("uvm_fault_upper_done"); UVMHIST_CALLED(maphist);
   1756 
   1757 	/*
   1758 	 * ... update the page queues.
   1759 	 */
   1760 
   1761 	uvm_pagelock(pg);
   1762 	if (wire_paging) {
   1763 		uvm_pagewire(pg);
   1764 	} else {
   1765 		uvm_pageactivate(pg);
   1766 	}
   1767 	uvm_pageunlock(pg);
   1768 
   1769 	if (wire_paging) {
   1770 		/*
   1771 		 * since the now-wired page cannot be paged out,
   1772 		 * release its swap resources for others to use.
   1773 		 * and since an anon with no swap cannot be clean,
   1774 		 * mark it dirty now.
   1775 		 */
   1776 
   1777 		uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
   1778 		uvm_anon_dropswap(anon);
   1779 	}
   1780 }
   1781 
   1782 /*
   1783  * uvm_fault_lower_upgrade: upgrade lower lock, reader -> writer
   1784  */
   1785 
   1786 static inline int
   1787 uvm_fault_lower_upgrade(struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1788     struct vm_amap *amap, struct uvm_object *uobj, struct vm_page *uobjpage)
   1789 {
   1790 
   1791 	KASSERT(uobj != NULL);
   1792 	KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
   1793 
   1794 	/*
   1795 	 * fast path.
   1796 	 */
   1797 
   1798 	if (__predict_true(flt->lower_lock_type == RW_WRITER)) {
   1799 		KASSERT(uobjpage == NULL || (uobjpage->flags & PG_BUSY) != 0);
   1800 		return 0;
   1801 	}
   1802 
   1803 	/*
   1804 	 * otherwise try for the upgrade.  if we don't get it, unlock
   1805 	 * everything, restart the fault and next time around get a writer
   1806 	 * lock.
   1807 	 */
   1808 
   1809 	flt->lower_lock_type = RW_WRITER;
   1810 	if (__predict_false(!rw_tryupgrade(uobj->vmobjlock))) {
   1811 		uvmfault_unlockall(ufi, amap, uobj);
   1812 		cpu_count(CPU_COUNT_FLTNOUP, 1);
   1813 		UVMHIST_LOG(maphist, "  !upgrade lower", 0, 0,0,0);
   1814 		return ERESTART;
   1815 	}
   1816 	cpu_count(CPU_COUNT_FLTUP, 1);
   1817 	KASSERT(flt->lower_lock_type == rw_lock_op(uobj->vmobjlock));
   1818 
   1819 	/*
   1820 	 * finally, if a page was supplied, assert that it's not busy
   1821 	 * (can't be with a reader lock) and then mark it busy now that
   1822 	 * we have a writer lock.
   1823 	 */
   1824 
   1825 	if (uobjpage != NULL) {
   1826 		KASSERT((uobjpage->flags & PG_BUSY) == 0);
   1827 		uobjpage->flags |= PG_BUSY;
   1828 		UVM_PAGE_OWN(uobjpage, "upgrdlwr");
   1829 	}
   1830 	return 0;
   1831 }
   1832 
   1833 /*
   1834  * uvm_fault_lower: handle lower fault.
   1835  *
   1836  *	1. check uobj
   1837  *	1.1. if null, ZFOD.
   1838  *	1.2. if not null, look up unnmapped neighbor pages.
   1839  *	2. for center page, check if promote.
   1840  *	2.1. ZFOD always needs promotion.
   1841  *	2.2. other uobjs, when entry is marked COW (usually MAP_PRIVATE vnode).
   1842  *	3. if uobj is not ZFOD and page is not found, do i/o.
   1843  *	4. dispatch either direct / promote fault.
   1844  */
   1845 
   1846 static int
   1847 uvm_fault_lower(
   1848 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   1849 	struct vm_page **pages)
   1850 {
   1851 	struct vm_amap *amap __diagused = ufi->entry->aref.ar_amap;
   1852 	struct uvm_object *uobj = ufi->entry->object.uvm_obj;
   1853 	struct vm_page *uobjpage;
   1854 	int error;
   1855 	UVMHIST_FUNC("uvm_fault_lower"); UVMHIST_CALLED(maphist);
   1856 
   1857 	/*
   1858 	 * now, if the desired page is not shadowed by the amap and we have
   1859 	 * a backing object that does not have a special fault routine, then
   1860 	 * we ask (with pgo_get) the object for resident pages that we care
   1861 	 * about and attempt to map them in.  we do not let pgo_get block
   1862 	 * (PGO_LOCKED).
   1863 	 */
   1864 
   1865 	if (uobj == NULL) {
   1866 		/* zero fill; don't care neighbor pages */
   1867 		uobjpage = NULL;
   1868 	} else {
   1869 		uvm_fault_lower_lookup(ufi, flt, pages);
   1870 		uobjpage = pages[flt->centeridx];
   1871 	}
   1872 
   1873 	/*
   1874 	 * note that at this point we are done with any front or back pages.
   1875 	 * we are now going to focus on the center page (i.e. the one we've
   1876 	 * faulted on).  if we have faulted on the upper (anon) layer
   1877 	 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
   1878 	 * not touched it yet).  if we have faulted on the bottom (uobj)
   1879 	 * layer [i.e. case 2] and the page was both present and available,
   1880 	 * then we've got a pointer to it as "uobjpage" and we've already
   1881 	 * made it BUSY.
   1882 	 */
   1883 
   1884 	/*
   1885 	 * locked:
   1886 	 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
   1887 	 */
   1888 	KASSERT(amap == NULL ||
   1889 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1890 	if (flt->lower_lock_type == RW_WRITER) {
   1891 		KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
   1892 		KASSERTMSG(uobjpage == NULL ||
   1893 		    (uobjpage->flags & PG_BUSY) != 0,
   1894 		    "page %p should be busy", uobjpage);
   1895 	} else {
   1896 		KASSERT(uobj == NULL || rw_read_held(uobj->vmobjlock));
   1897 		KASSERTMSG(uobjpage == NULL ||
   1898 		    (uobjpage->flags & PG_BUSY) == 0,
   1899 		    "page %p should not be busy", uobjpage);
   1900 	}
   1901 
   1902 	/*
   1903 	 * note that uobjpage can not be PGO_DONTCARE at this point.  we now
   1904 	 * set uobjpage to PGO_DONTCARE if we are doing a zero fill.  if we
   1905 	 * have a backing object, check and see if we are going to promote
   1906 	 * the data up to an anon during the fault.
   1907 	 */
   1908 
   1909 	if (uobj == NULL) {
   1910 		uobjpage = PGO_DONTCARE;
   1911 		flt->promote = true;		/* always need anon here */
   1912 	} else {
   1913 		KASSERT(uobjpage != PGO_DONTCARE);
   1914 		flt->promote = flt->cow_now && UVM_ET_ISCOPYONWRITE(ufi->entry);
   1915 	}
   1916 	UVMHIST_LOG(maphist, "  case 2 fault: promote=%jd, zfill=%jd",
   1917 	    flt->promote, (uobj == NULL), 0,0);
   1918 
   1919 	/*
   1920 	 * if uobjpage is not null then we do not need to do I/O to get the
   1921 	 * uobjpage.
   1922 	 *
   1923 	 * if uobjpage is null, then we need to unlock and ask the pager to
   1924 	 * get the data for us.   once we have the data, we need to reverify
   1925 	 * the state the world.   we are currently not holding any resources.
   1926 	 */
   1927 
   1928 	if (uobjpage) {
   1929 		/* update rusage counters */
   1930 		curlwp->l_ru.ru_minflt++;
   1931 	} else {
   1932 		error = uvm_fault_lower_io(ufi, flt, &uobj, &uobjpage);
   1933 		if (error != 0)
   1934 			return error;
   1935 	}
   1936 
   1937 	/*
   1938 	 * locked:
   1939 	 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
   1940 	 */
   1941 	KASSERT(amap == NULL ||
   1942 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   1943 	if (flt->lower_lock_type == RW_WRITER) {
   1944 		KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
   1945 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
   1946 	} else {
   1947 		KASSERT(uobj == NULL || rw_read_held(uobj->vmobjlock));
   1948 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) == 0);
   1949 	}
   1950 
   1951 	/*
   1952 	 * notes:
   1953 	 *  - at this point uobjpage can not be NULL
   1954 	 *  - at this point uobjpage can not be PG_RELEASED (since we checked
   1955 	 *  for it above)
   1956 	 *  - at this point uobjpage could be waited on (handle later)
   1957 	 */
   1958 
   1959 	KASSERT(uobjpage != NULL);
   1960 	KASSERT(uobj == NULL || uobj == uobjpage->uobject);
   1961 	KASSERT(uobj == NULL || !UVM_OBJ_IS_CLEAN(uobjpage->uobject) ||
   1962 	    uvm_pagegetdirty(uobjpage) == UVM_PAGE_STATUS_CLEAN);
   1963 
   1964 	if (!flt->promote) {
   1965 		error = uvm_fault_lower_direct(ufi, flt, uobj, uobjpage);
   1966 	} else {
   1967 		error = uvm_fault_lower_promote(ufi, flt, uobj, uobjpage);
   1968 	}
   1969 	return error;
   1970 }
   1971 
   1972 /*
   1973  * uvm_fault_lower_lookup: look up on-memory uobj pages.
   1974  *
   1975  *	1. get on-memory pages.
   1976  *	2. if failed, give up (get only center page later).
   1977  *	3. if succeeded, enter h/w mapping of neighbor pages.
   1978  */
   1979 
   1980 static void
   1981 uvm_fault_lower_lookup(
   1982 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   1983 	struct vm_page **pages)
   1984 {
   1985 	struct uvm_object *uobj = ufi->entry->object.uvm_obj;
   1986 	int lcv, gotpages;
   1987 	vaddr_t currva;
   1988 	UVMHIST_FUNC("uvm_fault_lower_lookup"); UVMHIST_CALLED(maphist);
   1989 
   1990 	rw_enter(uobj->vmobjlock, flt->lower_lock_type);
   1991 
   1992 	/*
   1993 	 * Locked: maps(read), amap(if there), uobj
   1994 	 *
   1995 	 * if we have a read lock on the object, do a PGO_NOBUSY get, which
   1996 	 * will return us pages with PG_BUSY clear.  if a write lock is held
   1997 	 * pages will be returned with PG_BUSY set.
   1998 	 */
   1999 
   2000 	cpu_count(CPU_COUNT_FLTLGET, 1);
   2001 	gotpages = flt->npages;
   2002 	(void) uobj->pgops->pgo_get(uobj,
   2003 	    ufi->entry->offset + flt->startva - ufi->entry->start,
   2004 	    pages, &gotpages, flt->centeridx,
   2005 	    flt->access_type & MASK(ufi->entry), ufi->entry->advice,
   2006 	    PGO_LOCKED | (flt->lower_lock_type == RW_WRITER ? 0 : PGO_NOBUSY));
   2007 
   2008 	KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
   2009 
   2010 	/*
   2011 	 * check for pages to map, if we got any
   2012 	 */
   2013 
   2014 	if (gotpages == 0) {
   2015 		pages[flt->centeridx] = NULL;
   2016 		return;
   2017 	}
   2018 
   2019 	currva = flt->startva;
   2020 	for (lcv = 0; lcv < flt->npages; lcv++, currva += PAGE_SIZE) {
   2021 		struct vm_page *curpg;
   2022 
   2023 		curpg = pages[lcv];
   2024 		if (curpg == NULL || curpg == PGO_DONTCARE) {
   2025 			continue;
   2026 		}
   2027 		KASSERT(curpg->uobject == uobj);
   2028 
   2029 		if (flt->lower_lock_type == RW_WRITER) {
   2030 			KASSERT(rw_write_held(uobj->vmobjlock));
   2031 			KASSERTMSG((curpg->flags & PG_BUSY) != 0,
   2032 			    "page %p should be busy", curpg);
   2033 		} else {
   2034 			KASSERT(rw_read_held(uobj->vmobjlock));
   2035 			KASSERTMSG((curpg->flags & PG_BUSY) == 0,
   2036 			    "page %p should not be busy", curpg);
   2037 		}
   2038 
   2039 		/*
   2040 		 * if center page is resident and not PG_BUSY|PG_RELEASED
   2041 		 * and !PGO_NOBUSY, then pgo_get made it PG_BUSY for us and
   2042 		 * gave us a handle to it.
   2043 		 */
   2044 
   2045 		if (lcv == flt->centeridx) {
   2046 			UVMHIST_LOG(maphist, "  got uobjpage (%#jx) "
   2047 			    "with locked get", (uintptr_t)curpg, 0, 0, 0);
   2048 		} else {
   2049 			uvm_fault_lower_neighbor(ufi, flt, currva, curpg);
   2050 		}
   2051 	}
   2052 	pmap_update(ufi->orig_map->pmap);
   2053 }
   2054 
   2055 /*
   2056  * uvm_fault_lower_neighbor: enter h/w mapping of lower neighbor page.
   2057  */
   2058 
   2059 static void
   2060 uvm_fault_lower_neighbor(
   2061 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   2062 	vaddr_t currva, struct vm_page *pg)
   2063 {
   2064 	const bool readonly = uvm_pagereadonly_p(pg) || pg->loan_count > 0;
   2065 	UVMHIST_FUNC(__func__); UVMHIST_CALLED(maphist);
   2066 
   2067 	/* locked: maps(read), amap(if there), uobj */
   2068 
   2069 	/*
   2070 	 * calling pgo_get with PGO_LOCKED returns us pages which
   2071 	 * are neither busy nor released, so we don't need to check
   2072 	 * for this.  we can just directly enter the pages.
   2073 	 */
   2074 
   2075 	/*
   2076 	 * in the read-locked case, it's not possible for this to be a new
   2077 	 * page.  it must be cached with the object and enqueued already.
   2078 	 * there wasn't a direct fault on the page, so avoid the cost of
   2079 	 * re-enqueuing it.
   2080 	 */
   2081 
   2082 	if (flt->lower_lock_type == RW_WRITER) {
   2083 		uvm_pagelock(pg);
   2084 		uvm_pageenqueue(pg);
   2085 		uvm_pageunlock(pg);
   2086 	}
   2087 	UVMHIST_LOG(maphist,
   2088 	    "  MAPPING: n obj: pm=%#jx, va=%#jx, pg=%#jx",
   2089 	    (uintptr_t)ufi->orig_map->pmap, currva, (uintptr_t)pg, 0);
   2090 	cpu_count(CPU_COUNT_FLTNOMAP, 1);
   2091 
   2092 	/*
   2093 	 * Since this page isn't the page that's actually faulting,
   2094 	 * ignore pmap_enter() failures; it's not critical that we
   2095 	 * enter these right now.
   2096 	 * NOTE: page can't be waited on or PG_RELEASED because we've
   2097 	 * held the lock the whole time we've had the handle.
   2098 	 */
   2099 	KASSERT((pg->flags & PG_PAGEOUT) == 0);
   2100 	KASSERT((pg->flags & PG_RELEASED) == 0);
   2101 	KASSERT(!UVM_OBJ_IS_CLEAN(pg->uobject) ||
   2102 	    uvm_pagegetdirty(pg) == UVM_PAGE_STATUS_CLEAN);
   2103 
   2104 	/*
   2105 	 * if a write lock was held on the object, the pages have been
   2106 	 * busied.  unbusy them now, as we are about to enter and then
   2107 	 * forget about them.
   2108 	 */
   2109 
   2110 	if (flt->lower_lock_type == RW_WRITER) {
   2111 		KASSERT((pg->flags & PG_BUSY) != 0);
   2112 		pg->flags &= ~(PG_BUSY);
   2113 		UVM_PAGE_OWN(pg, NULL);
   2114 	} else {
   2115 		KASSERT((pg->flags & PG_BUSY) == 0);
   2116 	}
   2117 	KASSERT(rw_lock_op(pg->uobject->vmobjlock) == flt->lower_lock_type);
   2118 
   2119 	const vm_prot_t mapprot =
   2120 	    readonly ? (flt->enter_prot & ~VM_PROT_WRITE) :
   2121 	    flt->enter_prot & MASK(ufi->entry);
   2122 	const u_int mapflags =
   2123 	    PMAP_CANFAIL | (flt->wire_mapping ? (mapprot | PMAP_WIRED) : 0);
   2124 	(void) pmap_enter(ufi->orig_map->pmap, currva,
   2125 	    VM_PAGE_TO_PHYS(pg), mapprot, mapflags);
   2126 }
   2127 
   2128 /*
   2129  * uvm_fault_lower_io: get lower page from backing store.
   2130  *
   2131  *	1. unlock everything, because i/o will block.
   2132  *	2. call pgo_get.
   2133  *	3. if failed, recover.
   2134  *	4. if succeeded, relock everything and verify things.
   2135  */
   2136 
   2137 static int
   2138 uvm_fault_lower_io(
   2139 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   2140 	struct uvm_object **ruobj, struct vm_page **ruobjpage)
   2141 {
   2142 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   2143 	struct uvm_object *uobj = *ruobj;
   2144 	struct vm_page *pg;
   2145 	bool locked;
   2146 	int gotpages;
   2147 	int error;
   2148 	voff_t uoff;
   2149 	vm_prot_t access_type;
   2150 	int advice;
   2151 	UVMHIST_FUNC("uvm_fault_lower_io"); UVMHIST_CALLED(maphist);
   2152 
   2153 	/* update rusage counters */
   2154 	curlwp->l_ru.ru_majflt++;
   2155 
   2156 	/* grab everything we need from the entry before we unlock */
   2157 	uoff = (ufi->orig_rvaddr - ufi->entry->start) + ufi->entry->offset;
   2158 	access_type = flt->access_type & MASK(ufi->entry);
   2159 	advice = ufi->entry->advice;
   2160 
   2161 	/* Locked: maps(read), amap(if there), uobj */
   2162 	KASSERT(rw_lock_op(uobj->vmobjlock) == flt->lower_lock_type);
   2163 
   2164 	/* Upgrade to a write lock if needed. */
   2165 	error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, NULL);
   2166 	if (error != 0) {
   2167 		return error;
   2168 	}
   2169 	uvmfault_unlockall(ufi, amap, NULL);
   2170 
   2171 	/* Locked: uobj(write) */
   2172 	KASSERT(rw_write_held(uobj->vmobjlock));
   2173 
   2174 	cpu_count(CPU_COUNT_FLTGET, 1);
   2175 	gotpages = 1;
   2176 	pg = NULL;
   2177 	error = uobj->pgops->pgo_get(uobj, uoff, &pg, &gotpages,
   2178 	    0, access_type, advice, PGO_SYNCIO);
   2179 	/* locked: pg(if no error) */
   2180 
   2181 	/*
   2182 	 * recover from I/O
   2183 	 */
   2184 
   2185 	if (error) {
   2186 		if (error == EAGAIN) {
   2187 			UVMHIST_LOG(maphist,
   2188 			    "  pgo_get says TRY AGAIN!",0,0,0,0);
   2189 			kpause("fltagain2", false, hz/2, NULL);
   2190 			return ERESTART;
   2191 		}
   2192 
   2193 #if 0
   2194 		KASSERT(error != ERESTART);
   2195 #else
   2196 		/* XXXUEBS don't re-fault? */
   2197 		if (error == ERESTART)
   2198 			error = EIO;
   2199 #endif
   2200 
   2201 		UVMHIST_LOG(maphist, "<- pgo_get failed (code %jd)",
   2202 		    error, 0,0,0);
   2203 		return error;
   2204 	}
   2205 
   2206 	/*
   2207 	 * re-verify the state of the world by first trying to relock
   2208 	 * the maps.  always relock the object.
   2209 	 */
   2210 
   2211 	locked = uvmfault_relock(ufi);
   2212 	if (locked && amap)
   2213 		amap_lock(amap, flt->upper_lock_type);
   2214 
   2215 	/* might be changed */
   2216 	uobj = pg->uobject;
   2217 
   2218 	rw_enter(uobj->vmobjlock, flt->lower_lock_type);
   2219 	KASSERT((pg->flags & PG_BUSY) != 0);
   2220 	KASSERT(flt->lower_lock_type == RW_WRITER);
   2221 
   2222 	uvm_pagelock(pg);
   2223 	uvm_pageactivate(pg);
   2224 	uvm_pageunlock(pg);
   2225 
   2226 	/* locked(locked): maps(read), amap(if !null), uobj, pg */
   2227 	/* locked(!locked): uobj, pg */
   2228 
   2229 	/*
   2230 	 * verify that the page has not be released and re-verify
   2231 	 * that amap slot is still free.   if there is a problem,
   2232 	 * we unlock and clean up.
   2233 	 */
   2234 
   2235 	if ((pg->flags & PG_RELEASED) != 0 ||
   2236 	    (locked && amap && amap_lookup(&ufi->entry->aref,
   2237 	      ufi->orig_rvaddr - ufi->entry->start))) {
   2238 		if (locked)
   2239 			uvmfault_unlockall(ufi, amap, NULL);
   2240 		locked = false;
   2241 	}
   2242 
   2243 	/*
   2244 	 * didn't get the lock?   release the page and retry.
   2245 	 */
   2246 
   2247 	if (locked == false) {
   2248 		UVMHIST_LOG(maphist,
   2249 		    "  wasn't able to relock after fault: retry",
   2250 		    0,0,0,0);
   2251 		if ((pg->flags & PG_RELEASED) == 0) {
   2252 			pg->flags &= ~PG_BUSY;
   2253 			uvm_pagelock(pg);
   2254 			uvm_pagewakeup(pg);
   2255 			uvm_pageunlock(pg);
   2256 			UVM_PAGE_OWN(pg, NULL);
   2257 		} else {
   2258 			cpu_count(CPU_COUNT_FLTPGRELE, 1);
   2259 			uvm_pagefree(pg);
   2260 		}
   2261 		rw_exit(uobj->vmobjlock);
   2262 		return ERESTART;
   2263 	}
   2264 
   2265 	/*
   2266 	 * we have the data in pg which is busy and
   2267 	 * not released.  we are holding object lock (so the page
   2268 	 * can't be released on us).
   2269 	 */
   2270 
   2271 	/* locked: maps(read), amap(if !null), uobj, pg */
   2272 
   2273 	*ruobj = uobj;
   2274 	*ruobjpage = pg;
   2275 	return 0;
   2276 }
   2277 
   2278 /*
   2279  * uvm_fault_lower_direct: fault lower center page
   2280  *
   2281  *	1. adjust flt->enter_prot.
   2282  *	2. if page is loaned, resolve.
   2283  */
   2284 
   2285 int
   2286 uvm_fault_lower_direct(
   2287 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   2288 	struct uvm_object *uobj, struct vm_page *uobjpage)
   2289 {
   2290 	struct vm_page *pg;
   2291 	UVMHIST_FUNC("uvm_fault_lower_direct"); UVMHIST_CALLED(maphist);
   2292 
   2293 	/*
   2294 	 * we are not promoting.   if the mapping is COW ensure that we
   2295 	 * don't give more access than we should (e.g. when doing a read
   2296 	 * fault on a COPYONWRITE mapping we want to map the COW page in
   2297 	 * R/O even though the entry protection could be R/W).
   2298 	 *
   2299 	 * set "pg" to the page we want to map in (uobjpage, usually)
   2300 	 */
   2301 
   2302 	cpu_count(CPU_COUNT_FLT_OBJ, 1);
   2303 	if (UVM_ET_ISCOPYONWRITE(ufi->entry) ||
   2304 	    UVM_OBJ_NEEDS_WRITEFAULT(uobjpage->uobject))
   2305 		flt->enter_prot &= ~VM_PROT_WRITE;
   2306 	pg = uobjpage;		/* map in the actual object */
   2307 
   2308 	KASSERT(uobjpage != PGO_DONTCARE);
   2309 
   2310 	/*
   2311 	 * we are faulting directly on the page.   be careful
   2312 	 * about writing to loaned pages...
   2313 	 */
   2314 
   2315 	if (uobjpage->loan_count) {
   2316 		uvm_fault_lower_direct_loan(ufi, flt, uobj, &pg, &uobjpage);
   2317 	}
   2318 	KASSERT(pg == uobjpage);
   2319 
   2320 	if (flt->lower_lock_type == RW_READER) {
   2321 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) == 0);
   2322 	} else {
   2323 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
   2324 	}
   2325 	return uvm_fault_lower_enter(ufi, flt, uobj, NULL, pg);
   2326 }
   2327 
   2328 /*
   2329  * uvm_fault_lower_direct_loan: resolve loaned page.
   2330  *
   2331  *	1. if not cow'ing, adjust flt->enter_prot.
   2332  *	2. if cow'ing, break loan.
   2333  */
   2334 
   2335 static int
   2336 uvm_fault_lower_direct_loan(
   2337 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   2338 	struct uvm_object *uobj, struct vm_page **rpg,
   2339 	struct vm_page **ruobjpage)
   2340 {
   2341 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   2342 	struct vm_page *pg;
   2343 	struct vm_page *uobjpage = *ruobjpage;
   2344 	int error;
   2345 	UVMHIST_FUNC("uvm_fault_lower_direct_loan"); UVMHIST_CALLED(maphist);
   2346 
   2347 	if (!flt->cow_now) {
   2348 		/* read fault: cap the protection at readonly */
   2349 		/* cap! */
   2350 		flt->enter_prot = flt->enter_prot & ~VM_PROT_WRITE;
   2351 	} else {
   2352 		/*
   2353 		 * write fault: must break the loan here.  to do this
   2354 		 * we need a write lock on the object.
   2355 		 */
   2356 
   2357 		error = uvm_fault_lower_upgrade(ufi, flt, amap, uobj, uobjpage);
   2358 		if (error != 0) {
   2359 			return error;
   2360 		}
   2361 		KASSERT(rw_write_held(uobj->vmobjlock));
   2362 
   2363 		pg = uvm_loanbreak(uobjpage);
   2364 		if (pg == NULL) {
   2365 
   2366 			/*
   2367 			 * drop ownership of page, it can't be released
   2368 			 */
   2369 
   2370 			uvm_pagelock(uobjpage);
   2371 			uvm_pagewakeup(uobjpage);
   2372 			uvm_pageunlock(uobjpage);
   2373 			uobjpage->flags &= ~PG_BUSY;
   2374 			UVM_PAGE_OWN(uobjpage, NULL);
   2375 
   2376 			uvmfault_unlockall(ufi, amap, uobj);
   2377 			UVMHIST_LOG(maphist,
   2378 			  "  out of RAM breaking loan, waiting",
   2379 			  0,0,0,0);
   2380 			cpu_count(CPU_COUNT_FLTNORAM, 1);
   2381 			uvm_wait("flt_noram4");
   2382 			return ERESTART;
   2383 		}
   2384 		*rpg = pg;
   2385 		*ruobjpage = pg;
   2386 	}
   2387 	return 0;
   2388 }
   2389 
   2390 /*
   2391  * uvm_fault_lower_promote: promote lower page.
   2392  *
   2393  *	1. call uvmfault_promote.
   2394  *	2. fill in data.
   2395  *	3. if not ZFOD, dispose old page.
   2396  */
   2397 
   2398 int
   2399 uvm_fault_lower_promote(
   2400 	struct uvm_faultinfo *ufi, struct uvm_faultctx *flt,
   2401 	struct uvm_object *uobj, struct vm_page *uobjpage)
   2402 {
   2403 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   2404 	struct vm_anon *anon;
   2405 	struct vm_page *pg;
   2406 	int error;
   2407 	UVMHIST_FUNC("uvm_fault_lower_promote"); UVMHIST_CALLED(maphist);
   2408 
   2409 	KASSERT(amap != NULL);
   2410 
   2411 	/* promoting requires a write lock. */
   2412 	error = uvm_fault_upper_upgrade(ufi, flt, amap, uobj);
   2413 	if (error != 0) {
   2414 		return error;
   2415 	}
   2416 	KASSERT(rw_write_held(amap->am_lock));
   2417 
   2418 	/*
   2419 	 * If we are going to promote the data to an anon we
   2420 	 * allocate a blank anon here and plug it into our amap.
   2421 	 */
   2422 	error = uvmfault_promote(ufi, NULL, uobjpage, &anon, &flt->anon_spare);
   2423 	switch (error) {
   2424 	case 0:
   2425 		break;
   2426 	case ERESTART:
   2427 		return ERESTART;
   2428 	default:
   2429 		return error;
   2430 	}
   2431 
   2432 	pg = anon->an_page;
   2433 
   2434 	/*
   2435 	 * Fill in the data.
   2436 	 */
   2437 	if (flt->lower_lock_type == RW_READER) {
   2438 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) == 0);
   2439 	} else {
   2440 		KASSERT(uobj == NULL || (uobjpage->flags & PG_BUSY) != 0);
   2441 	}
   2442 
   2443 	if (uobjpage != PGO_DONTCARE) {
   2444 		cpu_count(CPU_COUNT_FLT_PRCOPY, 1);
   2445 
   2446 		/*
   2447 		 * promote to shared amap?  make sure all sharing
   2448 		 * procs see it
   2449 		 */
   2450 
   2451 		if ((amap_flags(amap) & AMAP_SHARED) != 0) {
   2452 			pmap_page_protect(uobjpage, VM_PROT_NONE);
   2453 			/*
   2454 			 * XXX: PAGE MIGHT BE WIRED!
   2455 			 */
   2456 		}
   2457 
   2458 		/*
   2459 		 * dispose of uobjpage.  it can't be PG_RELEASED
   2460 		 * since we still hold the object lock.
   2461 		 */
   2462 
   2463 		if ((uobjpage->flags & PG_BUSY) != 0) {
   2464 			uobjpage->flags &= ~PG_BUSY;
   2465 			uvm_pagelock(uobjpage);
   2466 			uvm_pagewakeup(uobjpage);
   2467 			uvm_pageunlock(uobjpage);
   2468 			UVM_PAGE_OWN(uobjpage, NULL);
   2469 		}
   2470 
   2471 		UVMHIST_LOG(maphist,
   2472 		    "  promote uobjpage %#jx to anon/page %#jx/%#jx",
   2473 		    (uintptr_t)uobjpage, (uintptr_t)anon, (uintptr_t)pg, 0);
   2474 
   2475 	} else {
   2476 		cpu_count(CPU_COUNT_FLT_PRZERO, 1);
   2477 
   2478 		/*
   2479 		 * Page is zero'd and marked dirty by
   2480 		 * uvmfault_promote().
   2481 		 */
   2482 
   2483 		UVMHIST_LOG(maphist,"  zero fill anon/page %#jx/%#jx",
   2484 		    (uintptr_t)anon, (uintptr_t)pg, 0, 0);
   2485 	}
   2486 
   2487 	return uvm_fault_lower_enter(ufi, flt, uobj, anon, pg);
   2488 }
   2489 
   2490 /*
   2491  * uvm_fault_lower_enter: enter h/w mapping of lower page or anon page promoted
   2492  * from the lower page.
   2493  */
   2494 
   2495 int
   2496 uvm_fault_lower_enter(
   2497 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   2498 	struct uvm_object *uobj,
   2499 	struct vm_anon *anon, struct vm_page *pg)
   2500 {
   2501 	struct vm_amap * const amap = ufi->entry->aref.ar_amap;
   2502 	const bool readonly = uvm_pagereadonly_p(pg);
   2503 	int error;
   2504 	UVMHIST_FUNC("uvm_fault_lower_enter"); UVMHIST_CALLED(maphist);
   2505 
   2506 	/*
   2507 	 * Locked:
   2508 	 *
   2509 	 *	maps(read), amap(if !null), uobj(if !null),
   2510 	 *	anon(if !null), pg(if anon), unlock_uobj(if !null)
   2511 	 *
   2512 	 * anon must be write locked (promotion).  uobj can be either.
   2513 	 *
   2514 	 * Note: pg is either the uobjpage or the new page in the new anon.
   2515 	 */
   2516 	KASSERT(amap == NULL ||
   2517 	    rw_lock_op(amap->am_lock) == flt->upper_lock_type);
   2518 	KASSERT(anon == NULL || anon->an_lock == amap->am_lock);
   2519 	if (flt->lower_lock_type == RW_WRITER) {
   2520 		KASSERT(uobj == NULL || rw_write_held(uobj->vmobjlock));
   2521 		KASSERTMSG((pg->flags & PG_BUSY) != 0,
   2522 		    "page %p should be busy", pg);
   2523 	} else {
   2524 		KASSERT(uobj == NULL || rw_read_held(uobj->vmobjlock));
   2525 		KASSERTMSG(anon != NULL || (pg->flags & PG_BUSY) == 0,
   2526 		    "page %p should not be busy", pg);
   2527 	}
   2528 
   2529 	/*
   2530 	 * all resources are present.   we can now map it in and free our
   2531 	 * resources.
   2532 	 */
   2533 
   2534 	UVMHIST_LOG(maphist,
   2535 	    "  MAPPING: case2: pm=%#jx, va=%#jx, pg=%#jx, promote=%jd",
   2536 	    (uintptr_t)ufi->orig_map->pmap, ufi->orig_rvaddr,
   2537 	    (uintptr_t)pg, flt->promote);
   2538 	KASSERTMSG((flt->access_type & VM_PROT_WRITE) == 0 || !readonly,
   2539 	    "promote=%u cow_now=%u access_type=%x enter_prot=%x cow=%u "
   2540 	    "entry=%p map=%p orig_rvaddr=%p pg=%p",
   2541 	    flt->promote, flt->cow_now, flt->access_type, flt->enter_prot,
   2542 	    UVM_ET_ISCOPYONWRITE(ufi->entry), ufi->entry, ufi->orig_map,
   2543 	    (void *)ufi->orig_rvaddr, pg);
   2544 	KASSERT((flt->access_type & VM_PROT_WRITE) == 0 || !readonly);
   2545 	if (pmap_enter(ufi->orig_map->pmap, ufi->orig_rvaddr,
   2546 	    VM_PAGE_TO_PHYS(pg),
   2547 	    readonly ? flt->enter_prot & ~VM_PROT_WRITE : flt->enter_prot,
   2548 	    flt->access_type | PMAP_CANFAIL |
   2549 	    (flt->wire_mapping ? PMAP_WIRED : 0)) != 0) {
   2550 
   2551 		/*
   2552 		 * No need to undo what we did; we can simply think of
   2553 		 * this as the pmap throwing away the mapping information.
   2554 		 *
   2555 		 * We do, however, have to go through the ReFault path,
   2556 		 * as the map may change while we're asleep.
   2557 		 */
   2558 
   2559 		/*
   2560 		 * ensure that the page is queued in the case that
   2561 		 * we just promoted the page.
   2562 		 */
   2563 
   2564 		if (anon != NULL || flt->lower_lock_type == RW_WRITER) {
   2565 			uvm_pagelock(pg);
   2566 			uvm_pageenqueue(pg);
   2567 			uvm_pagewakeup(pg);
   2568 			uvm_pageunlock(pg);
   2569 		} else {
   2570 			KASSERT((pg->flags & PG_BUSY) == 0);
   2571 		}
   2572 
   2573 		/*
   2574 		 * note that pg can't be PG_RELEASED since we did not drop
   2575 		 * the object lock since the last time we checked.
   2576 		 */
   2577 		KASSERT((pg->flags & PG_RELEASED) == 0);
   2578 		if ((pg->flags & PG_BUSY) != 0) {
   2579 			pg->flags &= ~(PG_BUSY|PG_FAKE);
   2580 			UVM_PAGE_OWN(pg, NULL);
   2581 		}
   2582 
   2583 		uvmfault_unlockall(ufi, amap, uobj);
   2584 		if (!uvm_reclaimable()) {
   2585 			UVMHIST_LOG(maphist,
   2586 			    "<- failed.  out of VM",0,0,0,0);
   2587 			/* XXX instrumentation */
   2588 			error = ENOMEM;
   2589 			return error;
   2590 		}
   2591 		/* XXX instrumentation */
   2592 		uvm_wait("flt_pmfail2");
   2593 		return ERESTART;
   2594 	}
   2595 
   2596 	uvm_fault_lower_done(ufi, flt, uobj, pg);
   2597 
   2598 	/*
   2599 	 * note that pg can't be PG_RELEASED since we did not drop the object
   2600 	 * lock since the last time we checked.
   2601 	 */
   2602 	KASSERT((pg->flags & PG_RELEASED) == 0);
   2603 	if ((pg->flags & PG_BUSY) != 0) {
   2604 		uvm_pagelock(pg);
   2605 		uvm_pagewakeup(pg);
   2606 		uvm_pageunlock(pg);
   2607 		pg->flags &= ~(PG_BUSY|PG_FAKE);
   2608 		UVM_PAGE_OWN(pg, NULL);
   2609 	}
   2610 
   2611 	pmap_update(ufi->orig_map->pmap);
   2612 	uvmfault_unlockall(ufi, amap, uobj);
   2613 
   2614 	UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
   2615 	return 0;
   2616 }
   2617 
   2618 /*
   2619  * uvm_fault_lower_done: queue lower center page.
   2620  */
   2621 
   2622 void
   2623 uvm_fault_lower_done(
   2624 	struct uvm_faultinfo *ufi, const struct uvm_faultctx *flt,
   2625 	struct uvm_object *uobj, struct vm_page *pg)
   2626 {
   2627 	bool dropswap = false;
   2628 
   2629 	UVMHIST_FUNC("uvm_fault_lower_done"); UVMHIST_CALLED(maphist);
   2630 
   2631 	uvm_pagelock(pg);
   2632 	if (flt->wire_paging) {
   2633 		uvm_pagewire(pg);
   2634 		if (pg->flags & PG_AOBJ) {
   2635 
   2636 			/*
   2637 			 * since the now-wired page cannot be paged out,
   2638 			 * release its swap resources for others to use.
   2639 			 * since an aobj page with no swap cannot be clean,
   2640 			 * mark it dirty now.
   2641 			 */
   2642 
   2643 			KASSERT(uobj != NULL);
   2644 			uvm_pagemarkdirty(pg, UVM_PAGE_STATUS_DIRTY);
   2645 			dropswap = true;
   2646 		}
   2647 	} else {
   2648 		uvm_pageactivate(pg);
   2649 	}
   2650 	uvm_pageunlock(pg);
   2651 
   2652 	if (dropswap) {
   2653 		uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
   2654 	}
   2655 }
   2656 
   2657 
   2658 /*
   2659  * uvm_fault_wire: wire down a range of virtual addresses in a map.
   2660  *
   2661  * => map may be read-locked by caller, but MUST NOT be write-locked.
   2662  * => if map is read-locked, any operations which may cause map to
   2663  *	be write-locked in uvm_fault() must be taken care of by
   2664  *	the caller.  See uvm_map_pageable().
   2665  */
   2666 
   2667 int
   2668 uvm_fault_wire(struct vm_map *map, vaddr_t start, vaddr_t end,
   2669     vm_prot_t access_type, int maxprot)
   2670 {
   2671 	vaddr_t va;
   2672 	int error;
   2673 
   2674 	/*
   2675 	 * now fault it in a page at a time.   if the fault fails then we have
   2676 	 * to undo what we have done.   note that in uvm_fault VM_PROT_NONE
   2677 	 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
   2678 	 */
   2679 
   2680 	/*
   2681 	 * XXX work around overflowing a vaddr_t.  this prevents us from
   2682 	 * wiring the last page in the address space, though.
   2683 	 */
   2684 	if (start > end) {
   2685 		return EFAULT;
   2686 	}
   2687 
   2688 	for (va = start; va < end; va += PAGE_SIZE) {
   2689 		error = uvm_fault_internal(map, va, access_type,
   2690 		    (maxprot ? UVM_FAULT_MAXPROT : 0) | UVM_FAULT_WIRE);
   2691 		if (error) {
   2692 			if (va != start) {
   2693 				uvm_fault_unwire(map, start, va);
   2694 			}
   2695 			return error;
   2696 		}
   2697 	}
   2698 	return 0;
   2699 }
   2700 
   2701 /*
   2702  * uvm_fault_unwire(): unwire range of virtual space.
   2703  */
   2704 
   2705 void
   2706 uvm_fault_unwire(struct vm_map *map, vaddr_t start, vaddr_t end)
   2707 {
   2708 	vm_map_lock_read(map);
   2709 	uvm_fault_unwire_locked(map, start, end);
   2710 	vm_map_unlock_read(map);
   2711 }
   2712 
   2713 /*
   2714  * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
   2715  *
   2716  * => map must be at least read-locked.
   2717  */
   2718 
   2719 void
   2720 uvm_fault_unwire_locked(struct vm_map *map, vaddr_t start, vaddr_t end)
   2721 {
   2722 	struct vm_map_entry *entry, *oentry;
   2723 	pmap_t pmap = vm_map_pmap(map);
   2724 	vaddr_t va;
   2725 	paddr_t pa;
   2726 	struct vm_page *pg;
   2727 
   2728 	/*
   2729 	 * we assume that the area we are unwiring has actually been wired
   2730 	 * in the first place.   this means that we should be able to extract
   2731 	 * the PAs from the pmap.   we also lock out the page daemon so that
   2732 	 * we can call uvm_pageunwire.
   2733 	 */
   2734 
   2735 	/*
   2736 	 * find the beginning map entry for the region.
   2737 	 */
   2738 
   2739 	KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
   2740 	if (uvm_map_lookup_entry(map, start, &entry) == false)
   2741 		panic("uvm_fault_unwire_locked: address not in map");
   2742 
   2743 	oentry = NULL;
   2744 	for (va = start; va < end; va += PAGE_SIZE) {
   2745 
   2746 		/*
   2747 		 * find the map entry for the current address.
   2748 		 */
   2749 
   2750 		KASSERT(va >= entry->start);
   2751 		while (va >= entry->end) {
   2752 			KASSERT(entry->next != &map->header &&
   2753 				entry->next->start <= entry->end);
   2754 			entry = entry->next;
   2755 		}
   2756 
   2757 		/*
   2758 		 * lock it.
   2759 		 */
   2760 
   2761 		if (entry != oentry) {
   2762 			if (oentry != NULL) {
   2763 				uvm_map_unlock_entry(oentry);
   2764 			}
   2765 			uvm_map_lock_entry(entry, RW_WRITER);
   2766 			oentry = entry;
   2767 		}
   2768 
   2769 		/*
   2770 		 * if the entry is no longer wired, tell the pmap.
   2771 		 */
   2772 
   2773 		if (!pmap_extract(pmap, va, &pa))
   2774 			continue;
   2775 
   2776 		if (VM_MAPENT_ISWIRED(entry) == 0)
   2777 			pmap_unwire(pmap, va);
   2778 
   2779 		pg = PHYS_TO_VM_PAGE(pa);
   2780 		if (pg) {
   2781 			uvm_pagelock(pg);
   2782 			uvm_pageunwire(pg);
   2783 			uvm_pageunlock(pg);
   2784 		}
   2785 	}
   2786 
   2787 	if (oentry != NULL) {
   2788 		uvm_map_unlock_entry(entry);
   2789 	}
   2790 }
   2791