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