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