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