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