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