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