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