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