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