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