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