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