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