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