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