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