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