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