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