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