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