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