Home | History | Annotate | Line # | Download | only in uvm
uvm_fault.c revision 1.45.2.6
      1 /*	$NetBSD: uvm_fault.c,v 1.45.2.6 2001/04/21 17:47:06 bouyer 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 #include "opt_uvmhist.h"
     38 
     39 /*
     40  * uvm_fault.c: fault handler
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/proc.h>
     47 #include <sys/malloc.h>
     48 #include <sys/mman.h>
     49 #include <sys/user.h>
     50 
     51 #include <uvm/uvm.h>
     52 
     53 /*
     54  *
     55  * a word on page faults:
     56  *
     57  * types of page faults we handle:
     58  *
     59  * CASE 1: upper layer faults                   CASE 2: lower layer faults
     60  *
     61  *    CASE 1A         CASE 1B                  CASE 2A        CASE 2B
     62  *    read/write1     write>1                  read/write   +-cow_write/zero
     63  *         |             |                         |        |
     64  *      +--|--+       +--|--+     +-----+       +  |  +     | +-----+
     65  * amap |  V  |       |  ----------->new|          |        | |  ^  |
     66  *      +-----+       +-----+     +-----+       +  |  +     | +--|--+
     67  *                                                 |        |    |
     68  *      +-----+       +-----+                   +--|--+     | +--|--+
     69  * uobj | d/c |       | d/c |                   |  V  |     +----|  |
     70  *      +-----+       +-----+                   +-----+       +-----+
     71  *
     72  * d/c = don't care
     73  *
     74  *   case [0]: layerless fault
     75  *	no amap or uobj is present.   this is an error.
     76  *
     77  *   case [1]: upper layer fault [anon active]
     78  *     1A: [read] or [write with anon->an_ref == 1]
     79  *		I/O takes place in top level anon and uobj is not touched.
     80  *     1B: [write with anon->an_ref > 1]
     81  *		new anon is alloc'd and data is copied off ["COW"]
     82  *
     83  *   case [2]: lower layer fault [uobj]
     84  *     2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
     85  *		I/O takes place directly in object.
     86  *     2B: [write to copy_on_write] or [read on NULL uobj]
     87  *		data is "promoted" from uobj to a new anon.
     88  *		if uobj is null, then we zero fill.
     89  *
     90  * we follow the standard UVM locking protocol ordering:
     91  *
     92  * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
     93  * we hold a PG_BUSY page if we unlock for I/O
     94  *
     95  *
     96  * the code is structured as follows:
     97  *
     98  *     - init the "IN" params in the ufi structure
     99  *   ReFault:
    100  *     - do lookups [locks maps], check protection, handle needs_copy
    101  *     - check for case 0 fault (error)
    102  *     - establish "range" of fault
    103  *     - if we have an amap lock it and extract the anons
    104  *     - if sequential advice deactivate pages behind us
    105  *     - at the same time check pmap for unmapped areas and anon for pages
    106  *	 that we could map in (and do map it if found)
    107  *     - check object for resident pages that we could map in
    108  *     - if (case 2) goto Case2
    109  *     - >>> handle case 1
    110  *           - ensure source anon is resident in RAM
    111  *           - if case 1B alloc new anon and copy from source
    112  *           - map the correct page in
    113  *   Case2:
    114  *     - >>> handle case 2
    115  *           - ensure source page is resident (if uobj)
    116  *           - if case 2B alloc new anon and copy from source (could be zero
    117  *		fill if uobj == NULL)
    118  *           - map the correct page in
    119  *     - done!
    120  *
    121  * note on paging:
    122  *   if we have to do I/O we place a PG_BUSY page in the correct object,
    123  * unlock everything, and do the I/O.   when I/O is done we must reverify
    124  * the state of the world before assuming that our data structures are
    125  * valid.   [because mappings could change while the map is unlocked]
    126  *
    127  *  alternative 1: unbusy the page in question and restart the page fault
    128  *    from the top (ReFault).   this is easy but does not take advantage
    129  *    of the information that we already have from our previous lookup,
    130  *    although it is possible that the "hints" in the vm_map will help here.
    131  *
    132  * alternative 2: the system already keeps track of a "version" number of
    133  *    a map.   [i.e. every time you write-lock a map (e.g. to change a
    134  *    mapping) you bump the version number up by one...]   so, we can save
    135  *    the version number of the map before we release the lock and start I/O.
    136  *    then when I/O is done we can relock and check the version numbers
    137  *    to see if anything changed.    this might save us some over 1 because
    138  *    we don't have to unbusy the page and may be less compares(?).
    139  *
    140  * alternative 3: put in backpointers or a way to "hold" part of a map
    141  *    in place while I/O is in progress.   this could be complex to
    142  *    implement (especially with structures like amap that can be referenced
    143  *    by multiple map entries, and figuring out what should wait could be
    144  *    complex as well...).
    145  *
    146  * given that we are not currently multiprocessor or multithreaded we might
    147  * as well choose alternative 2 now.   maybe alternative 3 would be useful
    148  * in the future.    XXX keep in mind for future consideration//rechecking.
    149  */
    150 
    151 /*
    152  * local data structures
    153  */
    154 
    155 struct uvm_advice {
    156 	int advice;
    157 	int nback;
    158 	int nforw;
    159 };
    160 
    161 /*
    162  * page range array:
    163  * note: index in array must match "advice" value
    164  * XXX: borrowed numbers from freebsd.   do they work well for us?
    165  */
    166 
    167 static struct uvm_advice uvmadvice[] = {
    168 	{ MADV_NORMAL, 3, 4 },
    169 	{ MADV_RANDOM, 0, 0 },
    170 	{ MADV_SEQUENTIAL, 8, 7},
    171 };
    172 
    173 #define UVM_MAXRANGE 16	/* must be max() of nback+nforw+1 */
    174 
    175 /*
    176  * private prototypes
    177  */
    178 
    179 static void uvmfault_amapcopy __P((struct uvm_faultinfo *));
    180 static __inline void uvmfault_anonflush __P((struct vm_anon **, int));
    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(anons, n)
    194 	struct vm_anon **anons;
    195 	int n;
    196 {
    197 	int lcv;
    198 	struct vm_page *pg;
    199 
    200 	for (lcv = 0 ; lcv < n ; lcv++) {
    201 		if (anons[lcv] == NULL)
    202 			continue;
    203 		simple_lock(&anons[lcv]->an_lock);
    204 		pg = anons[lcv]->u.an_page;
    205 		if (pg && (pg->flags & PG_BUSY) == 0 && pg->loan_count == 0) {
    206 			uvm_lock_pageq();
    207 			if (pg->wire_count == 0) {
    208 				pmap_clear_reference(pg);
    209 				uvm_pagedeactivate(pg);
    210 			}
    211 			uvm_unlock_pageq();
    212 		}
    213 		simple_unlock(&anons[lcv]->an_lock);
    214 	}
    215 }
    216 
    217 /*
    218  * normal functions
    219  */
    220 
    221 /*
    222  * uvmfault_amapcopy: clear "needs_copy" in a map.
    223  *
    224  * => called with VM data structures unlocked (usually, see below)
    225  * => we get a write lock on the maps and clear needs_copy for a VA
    226  * => if we are out of RAM we sleep (waiting for more)
    227  */
    228 
    229 static void
    230 uvmfault_amapcopy(ufi)
    231 	struct uvm_faultinfo *ufi;
    232 {
    233 
    234 	/*
    235 	 * while we haven't done the job
    236 	 */
    237 
    238 	while (1) {
    239 
    240 		/*
    241 		 * no mapping?  give up.
    242 		 */
    243 
    244 		if (uvmfault_lookup(ufi, TRUE) == FALSE)
    245 			return;
    246 
    247 		/*
    248 		 * copy if needed.
    249 		 */
    250 
    251 		if (UVM_ET_ISNEEDSCOPY(ufi->entry))
    252 			amap_copy(ufi->map, ufi->entry, M_NOWAIT, TRUE,
    253 				ufi->orig_rvaddr, ufi->orig_rvaddr + 1);
    254 
    255 		/*
    256 		 * didn't work?  must be out of RAM.   unlock and sleep.
    257 		 */
    258 
    259 		if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
    260 			uvmfault_unlockmaps(ufi, TRUE);
    261 			uvm_wait("fltamapcopy");
    262 			continue;
    263 		}
    264 
    265 		/*
    266 		 * got it!   unlock and return.
    267 		 */
    268 
    269 		uvmfault_unlockmaps(ufi, TRUE);
    270 		return;
    271 	}
    272 	/*NOTREACHED*/
    273 }
    274 
    275 /*
    276  * uvmfault_anonget: get data in an anon into a non-busy, non-released
    277  * page in that anon.
    278  *
    279  * => maps, amap, and anon locked by caller.
    280  * => if we fail (result != 0) we unlock everything.
    281  * => if we are successful, we return with everything still locked.
    282  * => we don't move the page on the queues [gets moved later]
    283  * => if we allocate a new page [we_own], it gets put on the queues.
    284  *    either way, the result is that the page is on the queues at return time
    285  * => for pages which are on loan from a uvm_object (and thus are not
    286  *    owned by the anon): if successful, we return with the owning object
    287  *    locked.   the caller must unlock this object when it unlocks everything
    288  *    else.
    289  */
    290 
    291 int
    292 uvmfault_anonget(ufi, amap, anon)
    293 	struct uvm_faultinfo *ufi;
    294 	struct vm_amap *amap;
    295 	struct vm_anon *anon;
    296 {
    297 	boolean_t we_own;	/* we own anon's page? */
    298 	boolean_t locked;	/* did we relock? */
    299 	struct vm_page *pg;
    300 	int error;
    301 	UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
    302 
    303 	LOCK_ASSERT(simple_lock_held(&anon->an_lock));
    304 
    305 	error = 0;
    306 	uvmexp.fltanget++;
    307         /* bump rusage counters */
    308 	if (anon->u.an_page)
    309 		curproc->p_addr->u_stats.p_ru.ru_minflt++;
    310 	else
    311 		curproc->p_addr->u_stats.p_ru.ru_majflt++;
    312 
    313 	/*
    314 	 * loop until we get it, or fail.
    315 	 */
    316 
    317 	while (1) {
    318 
    319 		we_own = FALSE;		/* TRUE if we set PG_BUSY on a page */
    320 		pg = anon->u.an_page;
    321 
    322 		/*
    323 		 * if there is a resident page and it is loaned, then anon
    324 		 * may not own it.   call out to uvm_anon_lockpage() to ensure
    325 		 * the real owner of the page has been identified and locked.
    326 		 */
    327 
    328 		if (pg && pg->loan_count)
    329 			pg = uvm_anon_lockloanpg(anon);
    330 
    331 		/*
    332 		 * page there?   make sure it is not busy/released.
    333 		 */
    334 
    335 		if (pg) {
    336 
    337 			/*
    338 			 * at this point, if the page has a uobject [meaning
    339 			 * we have it on loan], then that uobject is locked
    340 			 * by us!   if the page is busy, we drop all the
    341 			 * locks (including uobject) and try again.
    342 			 */
    343 
    344 			if ((pg->flags & (PG_BUSY|PG_RELEASED)) == 0) {
    345 				UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
    346 				return (0);
    347 			}
    348 			pg->flags |= PG_WANTED;
    349 			uvmexp.fltpgwait++;
    350 
    351 			/*
    352 			 * the last unlock must be an atomic unlock+wait on
    353 			 * the owner of page
    354 			 */
    355 			if (pg->uobject) {	/* owner is uobject ? */
    356 				uvmfault_unlockall(ufi, amap, NULL, anon);
    357 				UVMHIST_LOG(maphist, " unlock+wait on uobj",0,
    358 				    0,0,0);
    359 				UVM_UNLOCK_AND_WAIT(pg,
    360 				    &pg->uobject->vmobjlock,
    361 				    FALSE, "anonget1",0);
    362 			} else {
    363 				/* anon owns page */
    364 				uvmfault_unlockall(ufi, amap, NULL, NULL);
    365 				UVMHIST_LOG(maphist, " unlock+wait on anon",0,
    366 				    0,0,0);
    367 				UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0,
    368 				    "anonget2",0);
    369 			}
    370 			/* ready to relock and try again */
    371 
    372 		} else {
    373 
    374 			/*
    375 			 * no page, we must try and bring it in.
    376 			 */
    377 			pg = uvm_pagealloc(NULL, 0, anon, 0);
    378 
    379 			if (pg == NULL) {		/* out of RAM.  */
    380 
    381 				uvmfault_unlockall(ufi, amap, NULL, anon);
    382 				uvmexp.fltnoram++;
    383 				UVMHIST_LOG(maphist, "  noram -- UVM_WAIT",0,
    384 				    0,0,0);
    385 				uvm_wait("flt_noram1");
    386 				/* ready to relock and try again */
    387 
    388 			} else {
    389 
    390 				/* we set the PG_BUSY bit */
    391 				we_own = TRUE;
    392 				uvmfault_unlockall(ufi, amap, NULL, anon);
    393 
    394 				/*
    395 				 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN
    396 				 * page into the uvm_swap_get function with
    397 				 * all data structures unlocked.  note that
    398 				 * it is ok to read an_swslot here because
    399 				 * we hold PG_BUSY on the page.
    400 				 */
    401 				uvmexp.pageins++;
    402 				error = uvm_swap_get(pg, anon->an_swslot,
    403 				    PGO_SYNCIO);
    404 
    405 				/*
    406 				 * we clean up after the i/o below in the
    407 				 * "we_own" case
    408 				 */
    409 				/* ready to relock and try again */
    410 			}
    411 		}
    412 
    413 		/*
    414 		 * now relock and try again
    415 		 */
    416 
    417 		locked = uvmfault_relock(ufi);
    418 		if (locked && amap != NULL) {
    419 			amap_lock(amap);
    420 		}
    421 		if (locked || we_own)
    422 			simple_lock(&anon->an_lock);
    423 
    424 		/*
    425 		 * if we own the page (i.e. we set PG_BUSY), then we need
    426 		 * to clean up after the I/O. there are three cases to
    427 		 * consider:
    428 		 *   [1] page released during I/O: free anon and ReFault.
    429 		 *   [2] I/O not OK.   free the page and cause the fault
    430 		 *       to fail.
    431 		 *   [3] I/O OK!   activate the page and sync with the
    432 		 *       non-we_own case (i.e. drop anon lock if not locked).
    433 		 */
    434 
    435 		if (we_own) {
    436 
    437 			if (pg->flags & PG_WANTED) {
    438 				/* still holding object lock */
    439 				wakeup(pg);
    440 			}
    441 			/* un-busy! */
    442 			pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);
    443 			UVM_PAGE_OWN(pg, NULL);
    444 
    445 			/*
    446 			 * if we were RELEASED during I/O, then our anon is
    447 			 * no longer part of an amap.   we need to free the
    448 			 * anon and try again.
    449 			 */
    450 			if (pg->flags & PG_RELEASED) {
    451 				pmap_page_protect(pg, VM_PROT_NONE);
    452 				simple_unlock(&anon->an_lock);
    453 				uvm_anfree(anon);	/* frees page for us */
    454 				if (locked)
    455 					uvmfault_unlockall(ufi, amap, NULL,
    456 							   NULL);
    457 				uvmexp.fltpgrele++;
    458 				UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    459 				return (ERESTART);	/* refault! */
    460 			}
    461 
    462 			if (error) {
    463 				/* remove page from anon */
    464 				anon->u.an_page = NULL;
    465 
    466 				/*
    467 				 * remove the swap slot from the anon
    468 				 * and mark the anon as having no real slot.
    469 				 * don't free the swap slot, thus preventing
    470 				 * it from being used again.
    471 				 */
    472 				uvm_swap_markbad(anon->an_swslot, 1);
    473 				anon->an_swslot = SWSLOT_BAD;
    474 
    475 				/*
    476 				 * note: page was never !PG_BUSY, so it
    477 				 * can't be mapped and thus no need to
    478 				 * pmap_page_protect it...
    479 				 */
    480 				uvm_lock_pageq();
    481 				uvm_pagefree(pg);
    482 				uvm_unlock_pageq();
    483 
    484 				if (locked)
    485 					uvmfault_unlockall(ufi, amap, NULL,
    486 					    anon);
    487 				else
    488 					simple_unlock(&anon->an_lock);
    489 				UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
    490 				return error;
    491 			}
    492 
    493 			/*
    494 			 * must be OK, clear modify (already PG_CLEAN)
    495 			 * and activate
    496 			 */
    497 			pmap_clear_modify(pg);
    498 			uvm_lock_pageq();
    499 			uvm_pageactivate(pg);
    500 			uvm_unlock_pageq();
    501 			if (!locked)
    502 				simple_unlock(&anon->an_lock);
    503 		}
    504 
    505 		/*
    506 		 * we were not able to relock.   restart fault.
    507 		 */
    508 
    509 		if (!locked) {
    510 			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    511 			return (ERESTART);
    512 		}
    513 
    514 		/*
    515 		 * verify no one has touched the amap and moved the anon on us.
    516 		 */
    517 
    518 		if (ufi != NULL &&
    519 		    amap_lookup(&ufi->entry->aref,
    520 				ufi->orig_rvaddr - ufi->entry->start) != anon) {
    521 
    522 			uvmfault_unlockall(ufi, amap, NULL, anon);
    523 			UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    524 			return (ERESTART);
    525 		}
    526 
    527 		/*
    528 		 * try it again!
    529 		 */
    530 
    531 		uvmexp.fltanretry++;
    532 		continue;
    533 
    534 	} /* while (1) */
    535 
    536 	/*NOTREACHED*/
    537 }
    538 
    539 /*
    540  *   F A U L T   -   m a i n   e n t r y   p o i n t
    541  */
    542 
    543 /*
    544  * uvm_fault: page fault handler
    545  *
    546  * => called from MD code to resolve a page fault
    547  * => VM data structures usually should be unlocked.   however, it is
    548  *	possible to call here with the main map locked if the caller
    549  *	gets a write lock, sets it recusive, and then calls us (c.f.
    550  *	uvm_map_pageable).   this should be avoided because it keeps
    551  *	the map locked off during I/O.
    552  */
    553 
    554 #define MASK(entry)     (UVM_ET_ISCOPYONWRITE(entry) ? \
    555 			 ~VM_PROT_WRITE : VM_PROT_ALL)
    556 
    557 int
    558 uvm_fault(orig_map, vaddr, fault_type, access_type)
    559 	vm_map_t orig_map;
    560 	vaddr_t vaddr;
    561 	vm_fault_t fault_type;
    562 	vm_prot_t access_type;
    563 {
    564 	struct uvm_faultinfo ufi;
    565 	vm_prot_t enter_prot;
    566 	boolean_t wired, narrow, promote, locked, shadowed;
    567 	int npages, nback, nforw, centeridx, error, lcv, gotpages;
    568 	vaddr_t startva, objaddr, currva, offset, uoff;
    569 	paddr_t pa;
    570 	struct vm_amap *amap;
    571 	struct uvm_object *uobj;
    572 	struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon;
    573 	struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage;
    574 	UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
    575 
    576 	UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, ft=%d, at=%d)",
    577 	      orig_map, vaddr, fault_type, access_type);
    578 
    579 	anon = NULL;
    580 	pg = NULL;
    581 
    582 	uvmexp.faults++;	/* XXX: locking? */
    583 
    584 	/*
    585 	 * init the IN parameters in the ufi
    586 	 */
    587 
    588 	ufi.orig_map = orig_map;
    589 	ufi.orig_rvaddr = trunc_page(vaddr);
    590 	ufi.orig_size = PAGE_SIZE;	/* can't get any smaller than this */
    591 	if (fault_type == VM_FAULT_WIRE)
    592 		narrow = TRUE;		/* don't look for neighborhood
    593 					 * pages on wire */
    594 	else
    595 		narrow = FALSE;		/* normal fault */
    596 
    597 	/*
    598 	 * before we do anything else, if this is a fault on a kernel
    599 	 * address, check to see if the address is managed by an
    600 	 * interrupt-safe map.  If it is, we fail immediately.  Intrsafe
    601 	 * maps are never pageable, and this approach avoids an evil
    602 	 * locking mess.
    603 	 */
    604 
    605 	if (orig_map == kernel_map && uvmfault_check_intrsafe(&ufi)) {
    606 		UVMHIST_LOG(maphist, "<- VA 0x%lx in intrsafe map %p",
    607 		    ufi.orig_rvaddr, ufi.map, 0, 0);
    608 		return EFAULT;
    609 	}
    610 
    611 	/*
    612 	 * "goto ReFault" means restart the page fault from ground zero.
    613 	 */
    614 ReFault:
    615 
    616 	/*
    617 	 * lookup and lock the maps
    618 	 */
    619 
    620 	if (uvmfault_lookup(&ufi, FALSE) == FALSE) {
    621 		UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0);
    622 		return (EFAULT);
    623 	}
    624 	/* locked: maps(read) */
    625 
    626 	KASSERT(ufi.map->flags & VM_MAP_PAGEABLE);
    627 
    628 	/*
    629 	 * check protection
    630 	 */
    631 
    632 	if ((ufi.entry->protection & access_type) != access_type) {
    633 		UVMHIST_LOG(maphist,
    634 		    "<- protection failure (prot=0x%x, access=0x%x)",
    635 		    ufi.entry->protection, access_type, 0, 0);
    636 		uvmfault_unlockmaps(&ufi, FALSE);
    637 		return EACCES;
    638 	}
    639 
    640 	/*
    641 	 * "enter_prot" is the protection we want to enter the page in at.
    642 	 * for certain pages (e.g. copy-on-write pages) this protection can
    643 	 * be more strict than ufi.entry->protection.  "wired" means either
    644 	 * the entry is wired or we are fault-wiring the pg.
    645 	 */
    646 
    647 	enter_prot = ufi.entry->protection;
    648 	wired = VM_MAPENT_ISWIRED(ufi.entry) || (fault_type == VM_FAULT_WIRE);
    649 	if (wired)
    650 		access_type = enter_prot; /* full access for wired */
    651 
    652 	/*
    653 	 * handle "needs_copy" case.   if we need to copy the amap we will
    654 	 * have to drop our readlock and relock it with a write lock.  (we
    655 	 * need a write lock to change anything in a map entry [e.g.
    656 	 * needs_copy]).
    657 	 */
    658 
    659 	if (UVM_ET_ISNEEDSCOPY(ufi.entry)) {
    660 		if ((access_type & VM_PROT_WRITE) ||
    661 		    (ufi.entry->object.uvm_obj == NULL)) {
    662 			/* need to clear */
    663 			UVMHIST_LOG(maphist,
    664 			    "  need to clear needs_copy and refault",0,0,0,0);
    665 			uvmfault_unlockmaps(&ufi, FALSE);
    666 			uvmfault_amapcopy(&ufi);
    667 			uvmexp.fltamcopy++;
    668 			goto ReFault;
    669 
    670 		} else {
    671 
    672 			/*
    673 			 * ensure that we pmap_enter page R/O since
    674 			 * needs_copy is still true
    675 			 */
    676 			enter_prot &= ~VM_PROT_WRITE;
    677 
    678 		}
    679 	}
    680 
    681 	/*
    682 	 * identify the players
    683 	 */
    684 
    685 	amap = ufi.entry->aref.ar_amap;		/* top layer */
    686 	uobj = ufi.entry->object.uvm_obj;	/* bottom layer */
    687 
    688 	/*
    689 	 * check for a case 0 fault.  if nothing backing the entry then
    690 	 * error now.
    691 	 */
    692 
    693 	if (amap == NULL && uobj == NULL) {
    694 		uvmfault_unlockmaps(&ufi, FALSE);
    695 		UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
    696 		return (EFAULT);
    697 	}
    698 
    699 	/*
    700 	 * establish range of interest based on advice from mapper
    701 	 * and then clip to fit map entry.   note that we only want
    702 	 * to do this the first time through the fault.   if we
    703 	 * ReFault we will disable this by setting "narrow" to true.
    704 	 */
    705 
    706 	if (narrow == FALSE) {
    707 
    708 		/* wide fault (!narrow) */
    709 		KASSERT(uvmadvice[ufi.entry->advice].advice ==
    710 			 ufi.entry->advice);
    711 		nback = min(uvmadvice[ufi.entry->advice].nback,
    712 			    (ufi.orig_rvaddr - ufi.entry->start) >> PAGE_SHIFT);
    713 		startva = ufi.orig_rvaddr - (nback << PAGE_SHIFT);
    714 		nforw = min(uvmadvice[ufi.entry->advice].nforw,
    715 			    ((ufi.entry->end - ufi.orig_rvaddr) >>
    716 			     PAGE_SHIFT) - 1);
    717 		/*
    718 		 * note: "-1" because we don't want to count the
    719 		 * faulting page as forw
    720 		 */
    721 		npages = nback + nforw + 1;
    722 		centeridx = nback;
    723 
    724 		narrow = TRUE;	/* ensure only once per-fault */
    725 
    726 	} else {
    727 
    728 		/* narrow fault! */
    729 		nback = nforw = 0;
    730 		startva = ufi.orig_rvaddr;
    731 		npages = 1;
    732 		centeridx = 0;
    733 
    734 	}
    735 
    736 	/* locked: maps(read) */
    737 	UVMHIST_LOG(maphist, "  narrow=%d, back=%d, forw=%d, startva=0x%x",
    738 		    narrow, nback, nforw, startva);
    739 	UVMHIST_LOG(maphist, "  entry=0x%x, amap=0x%x, obj=0x%x", ufi.entry,
    740 		    amap, uobj, 0);
    741 
    742 	/*
    743 	 * if we've got an amap, lock it and extract current anons.
    744 	 */
    745 
    746 	if (amap) {
    747 		amap_lock(amap);
    748 		anons = anons_store;
    749 		amap_lookups(&ufi.entry->aref, startva - ufi.entry->start,
    750 		    anons, npages);
    751 	} else {
    752 		anons = NULL;	/* to be safe */
    753 	}
    754 
    755 	/* locked: maps(read), amap(if there) */
    756 
    757 	/*
    758 	 * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
    759 	 * now and then forget about them (for the rest of the fault).
    760 	 */
    761 
    762 	if (ufi.entry->advice == MADV_SEQUENTIAL) {
    763 
    764 		UVMHIST_LOG(maphist, "  MADV_SEQUENTIAL: flushing backpages",
    765 		    0,0,0,0);
    766 		/* flush back-page anons? */
    767 		if (amap)
    768 			uvmfault_anonflush(anons, nback);
    769 
    770 		/* flush object? */
    771 		if (uobj) {
    772 			objaddr =
    773 			    (startva - ufi.entry->start) + ufi.entry->offset;
    774 			simple_lock(&uobj->vmobjlock);
    775 			(void) uobj->pgops->pgo_flush(uobj, objaddr, objaddr +
    776 				    (nback << PAGE_SHIFT), PGO_DEACTIVATE);
    777 			simple_unlock(&uobj->vmobjlock);
    778 		}
    779 
    780 		/* now forget about the backpages */
    781 		if (amap)
    782 			anons += nback;
    783 		startva += (nback << PAGE_SHIFT);
    784 		npages -= nback;
    785 		nback = centeridx = 0;
    786 	}
    787 
    788 	/* locked: maps(read), amap(if there) */
    789 
    790 	/*
    791 	 * map in the backpages and frontpages we found in the amap in hopes
    792 	 * of preventing future faults.    we also init the pages[] array as
    793 	 * we go.
    794 	 */
    795 
    796 	currva = startva;
    797 	shadowed = FALSE;
    798 	for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) {
    799 
    800 		/*
    801 		 * dont play with VAs that are already mapped
    802 		 * except for center)
    803 		 */
    804 		if (lcv != centeridx &&
    805 		    pmap_extract(ufi.orig_map->pmap, currva, &pa)) {
    806 			pages[lcv] = PGO_DONTCARE;
    807 			continue;
    808 		}
    809 
    810 		/*
    811 		 * unmapped or center page.   check if any anon at this level.
    812 		 */
    813 		if (amap == NULL || anons[lcv] == NULL) {
    814 			pages[lcv] = NULL;
    815 			continue;
    816 		}
    817 
    818 		/*
    819 		 * check for present page and map if possible.   re-activate it.
    820 		 */
    821 
    822 		pages[lcv] = PGO_DONTCARE;
    823 		if (lcv == centeridx) {		/* save center for later! */
    824 			shadowed = TRUE;
    825 			continue;
    826 		}
    827 		anon = anons[lcv];
    828 		simple_lock(&anon->an_lock);
    829 		/* ignore loaned pages */
    830 		if (anon->u.an_page && anon->u.an_page->loan_count == 0 &&
    831 			(anon->u.an_page->flags & (PG_RELEASED|PG_BUSY)) == 0) {
    832 			uvm_lock_pageq();
    833 			uvm_pageactivate(anon->u.an_page);	/* reactivate */
    834 			uvm_unlock_pageq();
    835 			UVMHIST_LOG(maphist,
    836 			    "  MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
    837 			    ufi.orig_map->pmap, currva, anon->u.an_page, 0);
    838 			uvmexp.fltnamap++;
    839 
    840 			/*
    841 			 * Since this isn't the page that's actually faulting,
    842 			 * ignore pmap_enter() failures; it's not critical
    843 			 * that we enter these right now.
    844 			 */
    845 
    846 			(void) pmap_enter(ufi.orig_map->pmap, currva,
    847 			    VM_PAGE_TO_PHYS(anon->u.an_page),
    848 			    (anon->an_ref > 1) ? (enter_prot & ~VM_PROT_WRITE) :
    849 			    enter_prot,
    850 			    PMAP_CANFAIL |
    851 			     (VM_MAPENT_ISWIRED(ufi.entry) ? PMAP_WIRED : 0));
    852 		}
    853 		simple_unlock(&anon->an_lock);
    854 	}
    855 
    856 	/* locked: maps(read), amap(if there) */
    857 	/* (shadowed == TRUE) if there is an anon at the faulting address */
    858 	UVMHIST_LOG(maphist, "  shadowed=%d, will_get=%d", shadowed,
    859 	    (uobj && shadowed == FALSE),0,0);
    860 
    861 	/*
    862 	 * note that if we are really short of RAM we could sleep in the above
    863 	 * call to pmap_enter with everything locked.   bad?
    864 	 *
    865 	 * XXX Actually, that is bad; pmap_enter() should just fail in that
    866 	 * XXX case.  --thorpej
    867 	 */
    868 
    869 	/*
    870 	 * if the desired page is not shadowed by the amap and we have a
    871 	 * backing object, then we check to see if the backing object would
    872 	 * prefer to handle the fault itself (rather than letting us do it
    873 	 * with the usual pgo_get hook).  the backing object signals this by
    874 	 * providing a pgo_fault routine.
    875 	 */
    876 
    877 	if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) {
    878 		simple_lock(&uobj->vmobjlock);
    879 
    880 		/* locked: maps(read), amap (if there), uobj */
    881 		error = uobj->pgops->pgo_fault(&ufi, startva, pages, npages,
    882 		    centeridx, fault_type, access_type, PGO_LOCKED|PGO_SYNCIO);
    883 
    884 		/* locked: nothing, pgo_fault has unlocked everything */
    885 
    886 		if (error == ERESTART)
    887 			goto ReFault;		/* try again! */
    888 		return error;
    889 	}
    890 
    891 	/*
    892 	 * now, if the desired page is not shadowed by the amap and we have
    893 	 * a backing object that does not have a special fault routine, then
    894 	 * we ask (with pgo_get) the object for resident pages that we care
    895 	 * about and attempt to map them in.  we do not let pgo_get block
    896 	 * (PGO_LOCKED).
    897 	 *
    898 	 * ("get" has the option of doing a pmap_enter for us)
    899 	 */
    900 
    901 	if (uobj && shadowed == FALSE) {
    902 		simple_lock(&uobj->vmobjlock);
    903 
    904 		/* locked (!shadowed): maps(read), amap (if there), uobj */
    905 		/*
    906 		 * the following call to pgo_get does _not_ change locking state
    907 		 */
    908 
    909 		uvmexp.fltlget++;
    910 		gotpages = npages;
    911 		(void) uobj->pgops->pgo_get(uobj, ufi.entry->offset +
    912 				(startva - ufi.entry->start),
    913 				pages, &gotpages, centeridx,
    914 				access_type & MASK(ufi.entry),
    915 				ufi.entry->advice, PGO_LOCKED);
    916 
    917 		/*
    918 		 * check for pages to map, if we got any
    919 		 */
    920 
    921 		uobjpage = NULL;
    922 
    923 		if (gotpages) {
    924 			currva = startva;
    925 			for (lcv = 0 ; lcv < npages ;
    926 			    lcv++, currva += PAGE_SIZE) {
    927 
    928 				if (pages[lcv] == NULL ||
    929 				    pages[lcv] == PGO_DONTCARE)
    930 					continue;
    931 
    932 				KASSERT((pages[lcv]->flags & PG_RELEASED) == 0);
    933 
    934 				/*
    935 				 * if center page is resident and not
    936 				 * PG_BUSY|PG_RELEASED then pgo_get
    937 				 * made it PG_BUSY for us and gave
    938 				 * us a handle to it.   remember this
    939 				 * page as "uobjpage." (for later use).
    940 				 */
    941 
    942 				if (lcv == centeridx) {
    943 					uobjpage = pages[lcv];
    944 					UVMHIST_LOG(maphist, "  got uobjpage "
    945 					    "(0x%x) with locked get",
    946 					    uobjpage, 0,0,0);
    947 					continue;
    948 				}
    949 
    950 				/*
    951 				 * note: calling pgo_get with locked data
    952 				 * structures returns us pages which are
    953 				 * neither busy nor released, so we don't
    954 				 * need to check for this.   we can just
    955 				 * directly enter the page (after moving it
    956 				 * to the head of the active queue [useful?]).
    957 				 */
    958 
    959 				uvm_lock_pageq();
    960 				uvm_pageactivate(pages[lcv]);	/* reactivate */
    961 				uvm_unlock_pageq();
    962 				UVMHIST_LOG(maphist,
    963 				  "  MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
    964 				  ufi.orig_map->pmap, currva, pages[lcv], 0);
    965 				uvmexp.fltnomap++;
    966 
    967 				/*
    968 				 * Since this page isn't the page that's
    969 				 * actually fauling, ignore pmap_enter()
    970 				 * failures; it's not critical that we
    971 				 * enter these right now.
    972 				 */
    973 
    974 				(void) pmap_enter(ufi.orig_map->pmap, currva,
    975 				    VM_PAGE_TO_PHYS(pages[lcv]),
    976 				    pages[lcv]->flags & PG_RDONLY ?
    977 				    VM_PROT_READ : enter_prot & MASK(ufi.entry),
    978 				    PMAP_CANFAIL |
    979 				     (wired ? PMAP_WIRED : 0));
    980 
    981 				/*
    982 				 * NOTE: page can't be PG_WANTED or PG_RELEASED
    983 				 * because we've held the lock the whole time
    984 				 * we've had the handle.
    985 				 */
    986 
    987 				pages[lcv]->flags &= ~(PG_BUSY); /* un-busy! */
    988 				UVM_PAGE_OWN(pages[lcv], NULL);
    989 			}	/* for "lcv" loop */
    990 		}   /* "gotpages" != 0 */
    991 		/* note: object still _locked_ */
    992 	} else {
    993 		uobjpage = NULL;
    994 	}
    995 
    996 	/* locked (shadowed): maps(read), amap */
    997 	/* locked (!shadowed): maps(read), amap(if there),
    998 		 uobj(if !null), uobjpage(if !null) */
    999 
   1000 	/*
   1001 	 * note that at this point we are done with any front or back pages.
   1002 	 * we are now going to focus on the center page (i.e. the one we've
   1003 	 * faulted on).  if we have faulted on the top (anon) layer
   1004 	 * [i.e. case 1], then the anon we want is anons[centeridx] (we have
   1005 	 * not touched it yet).  if we have faulted on the bottom (uobj)
   1006 	 * layer [i.e. case 2] and the page was both present and available,
   1007 	 * then we've got a pointer to it as "uobjpage" and we've already
   1008 	 * made it BUSY.
   1009 	 */
   1010 
   1011 	/*
   1012 	 * there are four possible cases we must address: 1A, 1B, 2A, and 2B
   1013 	 */
   1014 
   1015 	/*
   1016 	 * redirect case 2: if we are not shadowed, go to case 2.
   1017 	 */
   1018 
   1019 	if (shadowed == FALSE)
   1020 		goto Case2;
   1021 
   1022 	/* locked: maps(read), amap */
   1023 
   1024 	/*
   1025 	 * handle case 1: fault on an anon in our amap
   1026 	 */
   1027 
   1028 	anon = anons[centeridx];
   1029 	UVMHIST_LOG(maphist, "  case 1 fault: anon=0x%x", anon, 0,0,0);
   1030 	simple_lock(&anon->an_lock);
   1031 
   1032 	/* locked: maps(read), amap, anon */
   1033 
   1034 	/*
   1035 	 * no matter if we have case 1A or case 1B we are going to need to
   1036 	 * have the anon's memory resident.   ensure that now.
   1037 	 */
   1038 
   1039 	/*
   1040 	 * let uvmfault_anonget do the dirty work.
   1041 	 * if it fails (!OK) it will unlock everything for us.
   1042 	 * if it succeeds, locks are still valid and locked.
   1043 	 * also, if it is OK, then the anon's page is on the queues.
   1044 	 * if the page is on loan from a uvm_object, then anonget will
   1045 	 * lock that object for us if it does not fail.
   1046 	 */
   1047 
   1048 	error = uvmfault_anonget(&ufi, amap, anon);
   1049 	switch (error) {
   1050 	case 0:
   1051 		break;
   1052 
   1053 	case ERESTART:
   1054 		goto ReFault;
   1055 
   1056 	case EAGAIN:
   1057 		tsleep(&lbolt, PVM, "fltagain1", 0);
   1058 		goto ReFault;
   1059 
   1060 	default:
   1061 		return error;
   1062 	}
   1063 
   1064 	/*
   1065 	 * uobj is non null if the page is on loan from an object (i.e. uobj)
   1066 	 */
   1067 
   1068 	uobj = anon->u.an_page->uobject;	/* locked by anonget if !NULL */
   1069 
   1070 	/* locked: maps(read), amap, anon, uobj(if one) */
   1071 
   1072 	/*
   1073 	 * special handling for loaned pages
   1074 	 */
   1075 
   1076 	if (anon->u.an_page->loan_count) {
   1077 
   1078 		if ((access_type & VM_PROT_WRITE) == 0) {
   1079 
   1080 			/*
   1081 			 * for read faults on loaned pages we just cap the
   1082 			 * protection at read-only.
   1083 			 */
   1084 
   1085 			enter_prot = enter_prot & ~VM_PROT_WRITE;
   1086 
   1087 		} else {
   1088 			/*
   1089 			 * note that we can't allow writes into a loaned page!
   1090 			 *
   1091 			 * if we have a write fault on a loaned page in an
   1092 			 * anon then we need to look at the anon's ref count.
   1093 			 * if it is greater than one then we are going to do
   1094 			 * a normal copy-on-write fault into a new anon (this
   1095 			 * is not a problem).  however, if the reference count
   1096 			 * is one (a case where we would normally allow a
   1097 			 * write directly to the page) then we need to kill
   1098 			 * the loan before we continue.
   1099 			 */
   1100 
   1101 			/* >1 case is already ok */
   1102 			if (anon->an_ref == 1) {
   1103 
   1104 				/* get new un-owned replacement page */
   1105 				pg = uvm_pagealloc(NULL, 0, NULL, 0);
   1106 				if (pg == NULL) {
   1107 					uvmfault_unlockall(&ufi, amap, uobj,
   1108 					    anon);
   1109 					uvm_wait("flt_noram2");
   1110 					goto ReFault;
   1111 				}
   1112 
   1113 				/*
   1114 				 * copy data, kill loan, and drop uobj lock
   1115 				 * (if any)
   1116 				 */
   1117 				/* copy old -> new */
   1118 				uvm_pagecopy(anon->u.an_page, pg);
   1119 
   1120 				/* force reload */
   1121 				pmap_page_protect(anon->u.an_page,
   1122 						  VM_PROT_NONE);
   1123 				uvm_lock_pageq();	  /* KILL loan */
   1124 				if (uobj)
   1125 					/* if we were loaning */
   1126 					anon->u.an_page->loan_count--;
   1127 				anon->u.an_page->uanon = NULL;
   1128 				/* in case we owned */
   1129 				anon->u.an_page->pqflags &= ~PQ_ANON;
   1130 				uvm_unlock_pageq();
   1131 				if (uobj) {
   1132 					simple_unlock(&uobj->vmobjlock);
   1133 					uobj = NULL;
   1134 				}
   1135 
   1136 				/* install new page in anon */
   1137 				anon->u.an_page = pg;
   1138 				pg->uanon = anon;
   1139 				pg->pqflags |= PQ_ANON;
   1140 				pg->flags &= ~(PG_BUSY|PG_FAKE);
   1141 				UVM_PAGE_OWN(pg, NULL);
   1142 
   1143 				/* done! */
   1144 			}     /* ref == 1 */
   1145 		}       /* write fault */
   1146 	}         /* loan count */
   1147 
   1148 	/*
   1149 	 * if we are case 1B then we will need to allocate a new blank
   1150 	 * anon to transfer the data into.   note that we have a lock
   1151 	 * on anon, so no one can busy or release the page until we are done.
   1152 	 * also note that the ref count can't drop to zero here because
   1153 	 * it is > 1 and we are only dropping one ref.
   1154 	 *
   1155 	 * in the (hopefully very rare) case that we are out of RAM we
   1156 	 * will unlock, wait for more RAM, and refault.
   1157 	 *
   1158 	 * if we are out of anon VM we kill the process (XXX: could wait?).
   1159 	 */
   1160 
   1161 	if ((access_type & VM_PROT_WRITE) != 0 && anon->an_ref > 1) {
   1162 
   1163 		UVMHIST_LOG(maphist, "  case 1B: COW fault",0,0,0,0);
   1164 		uvmexp.flt_acow++;
   1165 		oanon = anon;		/* oanon = old, locked anon */
   1166 		anon = uvm_analloc();
   1167 		if (anon) {
   1168 			/* new anon is locked! */
   1169 			pg = uvm_pagealloc(NULL, 0, anon, 0);
   1170 		}
   1171 
   1172 		/* check for out of RAM */
   1173 		if (anon == NULL || pg == NULL) {
   1174 			if (anon) {
   1175 				anon->an_ref--;
   1176 				simple_unlock(&anon->an_lock);
   1177 				uvm_anfree(anon);
   1178 			}
   1179 			uvmfault_unlockall(&ufi, amap, uobj, oanon);
   1180 			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1181 			if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) {
   1182 				UVMHIST_LOG(maphist,
   1183 				    "<- failed.  out of VM",0,0,0,0);
   1184 				uvmexp.fltnoanon++;
   1185 				return ENOMEM;
   1186 			}
   1187 
   1188 			uvmexp.fltnoram++;
   1189 			uvm_wait("flt_noram3");	/* out of RAM, wait for more */
   1190 			goto ReFault;
   1191 		}
   1192 
   1193 		/* got all resources, replace anon with nanon */
   1194 
   1195 		uvm_pagecopy(oanon->u.an_page, pg);	/* pg now !PG_CLEAN */
   1196 		pg->flags &= ~(PG_BUSY|PG_FAKE);	/* un-busy! new page */
   1197 		UVM_PAGE_OWN(pg, NULL);
   1198 		amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
   1199 		    anon, 1);
   1200 
   1201 		/* deref: can not drop to zero here by defn! */
   1202 		oanon->an_ref--;
   1203 
   1204 		/*
   1205 		 * note: oanon is still locked, as is the new anon.  we
   1206 		 * need to check for this later when we unlock oanon; if
   1207 		 * oanon != anon, we'll have to unlock anon, too.
   1208 		 */
   1209 
   1210 	} else {
   1211 
   1212 		uvmexp.flt_anon++;
   1213 		oanon = anon;		/* old, locked anon is same as anon */
   1214 		pg = anon->u.an_page;
   1215 		if (anon->an_ref > 1)     /* disallow writes to ref > 1 anons */
   1216 			enter_prot = enter_prot & ~VM_PROT_WRITE;
   1217 
   1218 	}
   1219 
   1220 	/* locked: maps(read), amap, oanon, anon (if different from oanon) */
   1221 
   1222 	/*
   1223 	 * now map the page in ...
   1224 	 * XXX: old fault unlocks object before pmap_enter.  this seems
   1225 	 * suspect since some other thread could blast the page out from
   1226 	 * under us between the unlock and the pmap_enter.
   1227 	 */
   1228 
   1229 	UVMHIST_LOG(maphist, "  MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
   1230 	    ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0);
   1231 	if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
   1232 	    enter_prot, access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0))
   1233 	    != 0) {
   1234 		/*
   1235 		 * No need to undo what we did; we can simply think of
   1236 		 * this as the pmap throwing away the mapping information.
   1237 		 *
   1238 		 * We do, however, have to go through the ReFault path,
   1239 		 * as the map may change while we're asleep.
   1240 		 */
   1241 		if (anon != oanon)
   1242 			simple_unlock(&anon->an_lock);
   1243 		uvmfault_unlockall(&ufi, amap, uobj, oanon);
   1244 		KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1245 		if (uvmexp.swpgonly == uvmexp.swpages) {
   1246 			UVMHIST_LOG(maphist,
   1247 			    "<- failed.  out of VM",0,0,0,0);
   1248 			/* XXX instrumentation */
   1249 			return ENOMEM;
   1250 		}
   1251 		/* XXX instrumentation */
   1252 		uvm_wait("flt_pmfail1");
   1253 		goto ReFault;
   1254 	}
   1255 
   1256 	/*
   1257 	 * ... update the page queues.
   1258 	 */
   1259 
   1260 	uvm_lock_pageq();
   1261 
   1262 	if (fault_type == VM_FAULT_WIRE) {
   1263 		uvm_pagewire(pg);
   1264 
   1265 		/*
   1266 		 * since the now-wired page cannot be paged out,
   1267 		 * release its swap resources for others to use.
   1268 		 * since an anon with no swap cannot be PG_CLEAN,
   1269 		 * clear its clean flag now.
   1270 		 */
   1271 
   1272 		pg->flags &= ~(PG_CLEAN);
   1273 		uvm_anon_dropswap(anon);
   1274 	} else {
   1275 		/* activate it */
   1276 		uvm_pageactivate(pg);
   1277 	}
   1278 
   1279 	uvm_unlock_pageq();
   1280 
   1281 	/*
   1282 	 * done case 1!  finish up by unlocking everything and returning success
   1283 	 */
   1284 
   1285 	if (anon != oanon)
   1286 		simple_unlock(&anon->an_lock);
   1287 	uvmfault_unlockall(&ufi, amap, uobj, oanon);
   1288 	return 0;
   1289 
   1290 
   1291 Case2:
   1292 	/*
   1293 	 * handle case 2: faulting on backing object or zero fill
   1294 	 */
   1295 
   1296 	/*
   1297 	 * locked:
   1298 	 * maps(read), amap(if there), uobj(if !null), uobjpage(if !null)
   1299 	 */
   1300 
   1301 	/*
   1302 	 * note that uobjpage can not be PGO_DONTCARE at this point.  we now
   1303 	 * set uobjpage to PGO_DONTCARE if we are doing a zero fill.  if we
   1304 	 * have a backing object, check and see if we are going to promote
   1305 	 * the data up to an anon during the fault.
   1306 	 */
   1307 
   1308 	if (uobj == NULL) {
   1309 		uobjpage = PGO_DONTCARE;
   1310 		promote = TRUE;		/* always need anon here */
   1311 	} else {
   1312 		KASSERT(uobjpage != PGO_DONTCARE);
   1313 		promote = (access_type & VM_PROT_WRITE) &&
   1314 		     UVM_ET_ISCOPYONWRITE(ufi.entry);
   1315 	}
   1316 	UVMHIST_LOG(maphist, "  case 2 fault: promote=%d, zfill=%d",
   1317 	    promote, (uobj == NULL), 0,0);
   1318 
   1319 	/*
   1320 	 * if uobjpage is not null then we do not need to do I/O to get the
   1321 	 * uobjpage.
   1322 	 *
   1323 	 * if uobjpage is null, then we need to unlock and ask the pager to
   1324 	 * get the data for us.   once we have the data, we need to reverify
   1325 	 * the state the world.   we are currently not holding any resources.
   1326 	 */
   1327 
   1328 	if (uobjpage) {
   1329 		/* update rusage counters */
   1330 		curproc->p_addr->u_stats.p_ru.ru_minflt++;
   1331 	} else {
   1332 		/* update rusage counters */
   1333 		curproc->p_addr->u_stats.p_ru.ru_majflt++;
   1334 
   1335 		/* locked: maps(read), amap(if there), uobj */
   1336 		uvmfault_unlockall(&ufi, amap, NULL, NULL);
   1337 		/* locked: uobj */
   1338 
   1339 		uvmexp.fltget++;
   1340 		gotpages = 1;
   1341 		uoff = (ufi.orig_rvaddr - ufi.entry->start) + ufi.entry->offset;
   1342 		error = uobj->pgops->pgo_get(uobj, uoff, &uobjpage, &gotpages,
   1343 		    0, access_type & MASK(ufi.entry), ufi.entry->advice,
   1344 		    PGO_SYNCIO);
   1345 
   1346 		/* locked: uobjpage(if no error) */
   1347 
   1348 		/*
   1349 		 * recover from I/O
   1350 		 */
   1351 
   1352 		if (error) {
   1353 			if (error == EAGAIN) {
   1354 				UVMHIST_LOG(maphist,
   1355 				    "  pgo_get says TRY AGAIN!",0,0,0,0);
   1356 				tsleep(&lbolt, PVM, "fltagain2", 0);
   1357 				goto ReFault;
   1358 			}
   1359 
   1360 			UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)",
   1361 			    error, 0,0,0);
   1362 			return error;
   1363 		}
   1364 
   1365 		/* locked: uobjpage */
   1366 
   1367 		/*
   1368 		 * re-verify the state of the world by first trying to relock
   1369 		 * the maps.  always relock the object.
   1370 		 */
   1371 
   1372 		locked = uvmfault_relock(&ufi);
   1373 		if (locked && amap)
   1374 			amap_lock(amap);
   1375 		simple_lock(&uobj->vmobjlock);
   1376 
   1377 		/* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
   1378 		/* locked(!locked): uobj, uobjpage */
   1379 
   1380 		/*
   1381 		 * verify that the page has not be released and re-verify
   1382 		 * that amap slot is still free.   if there is a problem,
   1383 		 * we unlock and clean up.
   1384 		 */
   1385 
   1386 		if ((uobjpage->flags & PG_RELEASED) != 0 ||
   1387 		    (locked && amap &&
   1388 		    amap_lookup(&ufi.entry->aref,
   1389 		      ufi.orig_rvaddr - ufi.entry->start))) {
   1390 			if (locked)
   1391 				uvmfault_unlockall(&ufi, amap, NULL, NULL);
   1392 			locked = FALSE;
   1393 		}
   1394 
   1395 		/*
   1396 		 * didn't get the lock?   release the page and retry.
   1397 		 */
   1398 
   1399 		if (locked == FALSE) {
   1400 
   1401 			UVMHIST_LOG(maphist,
   1402 			    "  wasn't able to relock after fault: retry",
   1403 			    0,0,0,0);
   1404 			if (uobjpage->flags & PG_WANTED)
   1405 				/* still holding object lock */
   1406 				wakeup(uobjpage);
   1407 
   1408 			if (uobjpage->flags & PG_RELEASED) {
   1409 				uvmexp.fltpgrele++;
   1410 				KASSERT(uobj->pgops->pgo_releasepg != NULL);
   1411 
   1412 				/* frees page */
   1413 				if (uobj->pgops->pgo_releasepg(uobjpage,NULL))
   1414 					/* unlock if still alive */
   1415 					simple_unlock(&uobj->vmobjlock);
   1416 				goto ReFault;
   1417 			}
   1418 
   1419 			uvm_lock_pageq();
   1420 			/* make sure it is in queues */
   1421 			uvm_pageactivate(uobjpage);
   1422 
   1423 			uvm_unlock_pageq();
   1424 			uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1425 			UVM_PAGE_OWN(uobjpage, NULL);
   1426 			simple_unlock(&uobj->vmobjlock);
   1427 			goto ReFault;
   1428 
   1429 		}
   1430 
   1431 		/*
   1432 		 * we have the data in uobjpage which is PG_BUSY and
   1433 		 * !PG_RELEASED.  we are holding object lock (so the page
   1434 		 * can't be released on us).
   1435 		 */
   1436 
   1437 		/* locked: maps(read), amap(if !null), uobj, uobjpage */
   1438 	}
   1439 
   1440 	/*
   1441 	 * locked:
   1442 	 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj)
   1443 	 */
   1444 
   1445 	/*
   1446 	 * notes:
   1447 	 *  - at this point uobjpage can not be NULL
   1448 	 *  - at this point uobjpage can not be PG_RELEASED (since we checked
   1449 	 *  for it above)
   1450 	 *  - at this point uobjpage could be PG_WANTED (handle later)
   1451 	 */
   1452 
   1453 	if (promote == FALSE) {
   1454 
   1455 		/*
   1456 		 * we are not promoting.   if the mapping is COW ensure that we
   1457 		 * don't give more access than we should (e.g. when doing a read
   1458 		 * fault on a COPYONWRITE mapping we want to map the COW page in
   1459 		 * R/O even though the entry protection could be R/W).
   1460 		 *
   1461 		 * set "pg" to the page we want to map in (uobjpage, usually)
   1462 		 */
   1463 
   1464 		/* no anon in this case. */
   1465 		anon = NULL;
   1466 
   1467 		uvmexp.flt_obj++;
   1468 		if (UVM_ET_ISCOPYONWRITE(ufi.entry))
   1469 			enter_prot &= ~VM_PROT_WRITE;
   1470 		pg = uobjpage;		/* map in the actual object */
   1471 
   1472 		/* assert(uobjpage != PGO_DONTCARE) */
   1473 
   1474 		/*
   1475 		 * we are faulting directly on the page.   be careful
   1476 		 * about writing to loaned pages...
   1477 		 */
   1478 		if (uobjpage->loan_count) {
   1479 
   1480 			if ((access_type & VM_PROT_WRITE) == 0) {
   1481 				/* read fault: cap the protection at readonly */
   1482 				/* cap! */
   1483 				enter_prot = enter_prot & ~VM_PROT_WRITE;
   1484 			} else {
   1485 				/* write fault: must break the loan here */
   1486 
   1487 				/* alloc new un-owned page */
   1488 				pg = uvm_pagealloc(NULL, 0, NULL, 0);
   1489 
   1490 				if (pg == NULL) {
   1491 					/*
   1492 					 * drop ownership of page, it can't
   1493 					 * be released
   1494 					 */
   1495 					if (uobjpage->flags & PG_WANTED)
   1496 						wakeup(uobjpage);
   1497 					uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1498 					UVM_PAGE_OWN(uobjpage, NULL);
   1499 
   1500 					uvm_lock_pageq();
   1501 					/* activate: we will need it later */
   1502 					uvm_pageactivate(uobjpage);
   1503 
   1504 					uvm_unlock_pageq();
   1505 					uvmfault_unlockall(&ufi, amap, uobj,
   1506 					  NULL);
   1507 					UVMHIST_LOG(maphist,
   1508 					  "  out of RAM breaking loan, waiting",
   1509 					  0,0,0,0);
   1510 					uvmexp.fltnoram++;
   1511 					uvm_wait("flt_noram4");
   1512 					goto ReFault;
   1513 				}
   1514 
   1515 				/*
   1516 				 * copy the data from the old page to the new
   1517 				 * one and clear the fake/clean flags on the
   1518 				 * new page (keep it busy).  force a reload
   1519 				 * of the old page by clearing it from all
   1520 				 * pmaps.  then lock the page queues to
   1521 				 * rename the pages.
   1522 				 */
   1523 				uvm_pagecopy(uobjpage, pg);	/* old -> new */
   1524 				pg->flags &= ~(PG_FAKE|PG_CLEAN);
   1525 				pmap_page_protect(uobjpage, VM_PROT_NONE);
   1526 				if (uobjpage->flags & PG_WANTED)
   1527 					wakeup(uobjpage);
   1528 				/* uobj still locked */
   1529 				uobjpage->flags &= ~(PG_WANTED|PG_BUSY);
   1530 				UVM_PAGE_OWN(uobjpage, NULL);
   1531 
   1532 				uvm_lock_pageq();
   1533 				offset = uobjpage->offset;
   1534 				/* remove old page */
   1535 				uvm_pagerealloc(uobjpage, NULL, 0);
   1536 
   1537 				/*
   1538 				 * at this point we have absolutely no
   1539 				 * control over uobjpage
   1540 				 */
   1541 				/* install new page */
   1542 				uvm_pagerealloc(pg, uobj, offset);
   1543 				uvm_unlock_pageq();
   1544 
   1545 				/*
   1546 				 * done!  loan is broken and "pg" is
   1547 				 * PG_BUSY.   it can now replace uobjpage.
   1548 				 */
   1549 
   1550 				uobjpage = pg;
   1551 
   1552 			}		/* write fault case */
   1553 		}		/* if loan_count */
   1554 
   1555 	} else {
   1556 
   1557 		/*
   1558 		 * if we are going to promote the data to an anon we
   1559 		 * allocate a blank anon here and plug it into our amap.
   1560 		 */
   1561 #if DIAGNOSTIC
   1562 		if (amap == NULL)
   1563 			panic("uvm_fault: want to promote data, but no anon");
   1564 #endif
   1565 
   1566 		anon = uvm_analloc();
   1567 		if (anon) {
   1568 			/*
   1569 			 * The new anon is locked.
   1570 			 *
   1571 			 * In `Fill in data...' below, if
   1572 			 * uobjpage == PGO_DONTCARE, we want
   1573 			 * a zero'd, dirty page, so have
   1574 			 * uvm_pagealloc() do that for us.
   1575 			 */
   1576 			pg = uvm_pagealloc(NULL, 0, anon,
   1577 			    (uobjpage == PGO_DONTCARE) ? UVM_PGA_ZERO : 0);
   1578 		}
   1579 
   1580 		/*
   1581 		 * out of memory resources?
   1582 		 */
   1583 		if (anon == NULL || pg == NULL) {
   1584 
   1585 			if (anon != NULL) {
   1586 				anon->an_ref--;
   1587 				simple_unlock(&anon->an_lock);
   1588 				uvm_anfree(anon);
   1589 			}
   1590 
   1591 			/*
   1592 			 * arg!  must unbusy our page and fail or sleep.
   1593 			 */
   1594 			if (uobjpage != PGO_DONTCARE) {
   1595 				if (uobjpage->flags & PG_WANTED)
   1596 					/* still holding object lock */
   1597 					wakeup(uobjpage);
   1598 
   1599 				uvm_lock_pageq();
   1600 				uvm_pageactivate(uobjpage);
   1601 				uvm_unlock_pageq();
   1602 				uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1603 				UVM_PAGE_OWN(uobjpage, NULL);
   1604 			}
   1605 
   1606 			/* unlock and fail ... */
   1607 			uvmfault_unlockall(&ufi, amap, uobj, NULL);
   1608 			KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1609 			if (anon == NULL || uvmexp.swpgonly == uvmexp.swpages) {
   1610 				UVMHIST_LOG(maphist, "  promote: out of VM",
   1611 				    0,0,0,0);
   1612 				uvmexp.fltnoanon++;
   1613 				return ENOMEM;
   1614 			}
   1615 
   1616 			UVMHIST_LOG(maphist, "  out of RAM, waiting for more",
   1617 			    0,0,0,0);
   1618 			uvmexp.fltnoram++;
   1619 			uvm_wait("flt_noram5");
   1620 			goto ReFault;
   1621 		}
   1622 
   1623 		/*
   1624 		 * fill in the data
   1625 		 */
   1626 
   1627 		if (uobjpage != PGO_DONTCARE) {
   1628 			uvmexp.flt_prcopy++;
   1629 			/* copy page [pg now dirty] */
   1630 			uvm_pagecopy(uobjpage, pg);
   1631 
   1632 			/*
   1633 			 * promote to shared amap?  make sure all sharing
   1634 			 * procs see it
   1635 			 */
   1636 			if ((amap_flags(amap) & AMAP_SHARED) != 0) {
   1637 				pmap_page_protect(uobjpage, VM_PROT_NONE);
   1638 			}
   1639 
   1640 			/*
   1641 			 * dispose of uobjpage.  it can't be PG_RELEASED
   1642 			 * since we still hold the object lock.
   1643 			 * drop handle to uobj as well.
   1644 			 */
   1645 
   1646 			if (uobjpage->flags & PG_WANTED)
   1647 				/* still have the obj lock */
   1648 				wakeup(uobjpage);
   1649 			uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1650 			UVM_PAGE_OWN(uobjpage, NULL);
   1651 			uvm_lock_pageq();
   1652 			uvm_pageactivate(uobjpage);
   1653 			uvm_unlock_pageq();
   1654 			simple_unlock(&uobj->vmobjlock);
   1655 			uobj = NULL;
   1656 
   1657 			UVMHIST_LOG(maphist,
   1658 			    "  promote uobjpage 0x%x to anon/page 0x%x/0x%x",
   1659 			    uobjpage, anon, pg, 0);
   1660 
   1661 		} else {
   1662 			uvmexp.flt_przero++;
   1663 			/*
   1664 			 * Page is zero'd and marked dirty by uvm_pagealloc()
   1665 			 * above.
   1666 			 */
   1667 			UVMHIST_LOG(maphist,"  zero fill anon/page 0x%x/0%x",
   1668 			    anon, pg, 0, 0);
   1669 		}
   1670 
   1671 		amap_add(&ufi.entry->aref, ufi.orig_rvaddr - ufi.entry->start,
   1672 		    anon, 0);
   1673 	}
   1674 
   1675 	/*
   1676 	 * locked:
   1677 	 * maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj),
   1678 	 *   anon(if !null), pg(if anon)
   1679 	 *
   1680 	 * note: pg is either the uobjpage or the new page in the new anon
   1681 	 */
   1682 
   1683 	/*
   1684 	 * all resources are present.   we can now map it in and free our
   1685 	 * resources.
   1686 	 */
   1687 
   1688 	UVMHIST_LOG(maphist,
   1689 	    "  MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
   1690 	    ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote);
   1691 	KASSERT(access_type == VM_PROT_READ || (pg->flags & PG_RDONLY) == 0);
   1692 	if (pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
   1693 	    pg->flags & PG_RDONLY ? VM_PROT_READ : enter_prot,
   1694 	    access_type | PMAP_CANFAIL | (wired ? PMAP_WIRED : 0)) != 0) {
   1695 
   1696 		/*
   1697 		 * No need to undo what we did; we can simply think of
   1698 		 * this as the pmap throwing away the mapping information.
   1699 		 *
   1700 		 * We do, however, have to go through the ReFault path,
   1701 		 * as the map may change while we're asleep.
   1702 		 */
   1703 
   1704 		if (pg->flags & PG_WANTED)
   1705 			wakeup(pg);		/* lock still held */
   1706 
   1707 		/*
   1708 		 * note that pg can't be PG_RELEASED since we did not drop
   1709 		 * the object lock since the last time we checked.
   1710 		 */
   1711 
   1712 		pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
   1713 		UVM_PAGE_OWN(pg, NULL);
   1714 		uvmfault_unlockall(&ufi, amap, uobj, anon);
   1715 		KASSERT(uvmexp.swpgonly <= uvmexp.swpages);
   1716 		if (uvmexp.swpgonly == uvmexp.swpages) {
   1717 			UVMHIST_LOG(maphist,
   1718 			    "<- failed.  out of VM",0,0,0,0);
   1719 			/* XXX instrumentation */
   1720 			return ENOMEM;
   1721 		}
   1722 		/* XXX instrumentation */
   1723 		uvm_wait("flt_pmfail2");
   1724 		goto ReFault;
   1725 	}
   1726 
   1727 	uvm_lock_pageq();
   1728 
   1729 	if (fault_type == VM_FAULT_WIRE) {
   1730 		uvm_pagewire(pg);
   1731 		if (pg->pqflags & PQ_AOBJ) {
   1732 
   1733 			/*
   1734 			 * since the now-wired page cannot be paged out,
   1735 			 * release its swap resources for others to use.
   1736 			 * since an aobj page with no swap cannot be PG_CLEAN,
   1737 			 * clear its clean flag now.
   1738 			 */
   1739 
   1740 			pg->flags &= ~(PG_CLEAN);
   1741 			uao_dropswap(uobj, pg->offset >> PAGE_SHIFT);
   1742 		}
   1743 	} else {
   1744 		/* activate it */
   1745 		uvm_pageactivate(pg);
   1746 	}
   1747 	uvm_unlock_pageq();
   1748 
   1749 	if (pg->flags & PG_WANTED)
   1750 		wakeup(pg);		/* lock still held */
   1751 
   1752 	/*
   1753 	 * note that pg can't be PG_RELEASED since we did not drop the object
   1754 	 * lock since the last time we checked.
   1755 	 */
   1756 
   1757 	pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
   1758 	UVM_PAGE_OWN(pg, NULL);
   1759 	uvmfault_unlockall(&ufi, amap, uobj, anon);
   1760 
   1761 	UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
   1762 	return 0;
   1763 }
   1764 
   1765 
   1766 /*
   1767  * uvm_fault_wire: wire down a range of virtual addresses in a map.
   1768  *
   1769  * => map may be read-locked by caller, but MUST NOT be write-locked.
   1770  * => if map is read-locked, any operations which may cause map to
   1771  *	be write-locked in uvm_fault() must be taken care of by
   1772  *	the caller.  See uvm_map_pageable().
   1773  */
   1774 
   1775 int
   1776 uvm_fault_wire(map, start, end, access_type)
   1777 	vm_map_t map;
   1778 	vaddr_t start, end;
   1779 	vm_prot_t access_type;
   1780 {
   1781 	vaddr_t va;
   1782 	pmap_t  pmap;
   1783 	int error;
   1784 
   1785 	pmap = vm_map_pmap(map);
   1786 
   1787 	/*
   1788 	 * now fault it in a page at a time.   if the fault fails then we have
   1789 	 * to undo what we have done.   note that in uvm_fault VM_PROT_NONE
   1790 	 * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
   1791 	 */
   1792 
   1793 	for (va = start ; va < end ; va += PAGE_SIZE) {
   1794 		error = uvm_fault(map, va, VM_FAULT_WIRE, access_type);
   1795 		if (error) {
   1796 			if (va != start) {
   1797 				uvm_fault_unwire(map, start, va);
   1798 			}
   1799 			return error;
   1800 		}
   1801 	}
   1802 
   1803 	return 0;
   1804 }
   1805 
   1806 /*
   1807  * uvm_fault_unwire(): unwire range of virtual space.
   1808  */
   1809 
   1810 void
   1811 uvm_fault_unwire(map, start, end)
   1812 	vm_map_t map;
   1813 	vaddr_t start, end;
   1814 {
   1815 
   1816 	vm_map_lock_read(map);
   1817 	uvm_fault_unwire_locked(map, start, end);
   1818 	vm_map_unlock_read(map);
   1819 }
   1820 
   1821 /*
   1822  * uvm_fault_unwire_locked(): the guts of uvm_fault_unwire().
   1823  *
   1824  * => map must be at least read-locked.
   1825  */
   1826 
   1827 void
   1828 uvm_fault_unwire_locked(map, start, end)
   1829 	vm_map_t map;
   1830 	vaddr_t start, end;
   1831 {
   1832 	vm_map_entry_t entry;
   1833 	pmap_t pmap = vm_map_pmap(map);
   1834 	vaddr_t va;
   1835 	paddr_t pa;
   1836 	struct vm_page *pg;
   1837 
   1838 	KASSERT((map->flags & VM_MAP_INTRSAFE) == 0);
   1839 
   1840 	/*
   1841 	 * we assume that the area we are unwiring has actually been wired
   1842 	 * in the first place.   this means that we should be able to extract
   1843 	 * the PAs from the pmap.   we also lock out the page daemon so that
   1844 	 * we can call uvm_pageunwire.
   1845 	 */
   1846 
   1847 	uvm_lock_pageq();
   1848 
   1849 	/*
   1850 	 * find the beginning map entry for the region.
   1851 	 */
   1852 	KASSERT(start >= vm_map_min(map) && end <= vm_map_max(map));
   1853 	if (uvm_map_lookup_entry(map, start, &entry) == FALSE)
   1854 		panic("uvm_fault_unwire_locked: address not in map");
   1855 
   1856 	for (va = start; va < end ; va += PAGE_SIZE) {
   1857 		if (pmap_extract(pmap, va, &pa) == FALSE)
   1858 			panic("uvm_fault_unwire_locked: unwiring "
   1859 			    "non-wired memory");
   1860 
   1861 		/*
   1862 		 * make sure the current entry is for the address we're
   1863 		 * dealing with.  if not, grab the next entry.
   1864 		 */
   1865 
   1866 		KASSERT(va >= entry->start);
   1867 		if (va >= entry->end) {
   1868 			KASSERT(entry->next != &map->header &&
   1869 				entry->next->start <= entry->end);
   1870 			entry = entry->next;
   1871 		}
   1872 
   1873 		/*
   1874 		 * if the entry is no longer wired, tell the pmap.
   1875 		 */
   1876 		if (VM_MAPENT_ISWIRED(entry) == 0)
   1877 			pmap_unwire(pmap, va);
   1878 
   1879 		pg = PHYS_TO_VM_PAGE(pa);
   1880 		if (pg)
   1881 			uvm_pageunwire(pg);
   1882 	}
   1883 
   1884 	uvm_unlock_pageq();
   1885 }
   1886