Home | History | Annotate | Line # | Download | only in uvm
uvm_fault.c revision 1.1.1.1
      1 /*	$Id: uvm_fault.c,v 1.1.1.1 1998/02/05 06:25:10 mrg Exp $	*/
      2 
      3 /*
      4  * XXXCDC: "ROUGH DRAFT" QUALITY UVM PRE-RELEASE FILE!
      5  *	   >>>USE AT YOUR OWN RISK, WORK IS NOT FINISHED<<<
      6  */
      7 /*
      8  *
      9  * Copyright (c) 1997 Charles D. Cranor and Washington University.
     10  * All rights reserved.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *      This product includes software developed by Charles D. Cranor and
     23  *      Washington University.
     24  * 4. The name of the author may not be used to endorse or promote products
     25  *    derived from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     28  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     29  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     30  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     31  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     32  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     33  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     34  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     36  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * uvm_fault.c: fault handler
     41  */
     42 
     43 #include <sys/param.h>
     44 #include <sys/systm.h>
     45 #include <sys/kernel.h>
     46 #include <sys/mount.h>
     47 #include <sys/proc.h>
     48 #include <sys/malloc.h>
     49 #include <sys/mman.h>
     50 
     51 #include <vm/vm.h>
     52 #include <vm/vm_page.h>
     53 #include <vm/vm_kern.h>
     54 
     55 #include <sys/syscallargs.h>
     56 
     57 #include <uvm/uvm.h>
     58 
     59 UVMHIST_DECL(maphist);
     60 
     61 /*
     62  *
     63  * a word on page faults:
     64  *
     65  * types of page faults we handle:
     66  *
     67  * CASE 1: upper layer faults                   CASE 2: lower layer faults
     68  *
     69  *    CASE 1A         CASE 1B                  CASE 2A        CASE 2B
     70  *    read/write1     write>1                  read/write   +-cow_write/zero
     71  *         |             |                         |        |
     72  *      +--|--+       +--|--+     +-----+       +  |  +     | +-----+
     73  * amap |  V  |       |  ----------->new|          |        | |  ^  |
     74  *      +-----+       +-----+     +-----+       +  |  +     | +--|--+
     75  *                                                 |        |    |
     76  *      +-----+       +-----+                   +--|--+     | +--|--+
     77  * uobj | d/c |       | d/c |                   |  V  |     +----|  |
     78  *      +-----+       +-----+                   +-----+       +-----+
     79  *
     80  * d/c = don't care
     81  *
     82  *   case [0]: layerless fault
     83  *	no amap or uobj is present.   this is an error.
     84  *
     85  *   case [1]: upper layer fault [anon active]
     86  *     1A: [read] or [write with anon->an_ref == 1]
     87  *		I/O takes place in top level anon and uobj is not touched.
     88  *     1B: [write with anon->an_ref > 1]
     89  *		new anon is alloc'd and data is copied off ["COW"]
     90  *
     91  *   case [2]: lower layer fault [uobj]
     92  *     2A: [read on non-NULL uobj] or [write to non-copy_on_write area]
     93  *		I/O takes place directly in object.
     94  *     2B: [write to copy_on_write] or [read on NULL uobj]
     95  *		data is "promoted" from uobj to a new anon.
     96  *		if uobj is null, then we zero fill.
     97  *
     98  * we follow the standard UVM locking protocol ordering:
     99  *
    100  * MAPS => AMAP => UOBJ => ANON => PAGE QUEUES (PQ)
    101  * we hold a PG_BUSY page if we unlock for I/O
    102  *
    103  *
    104  * the code is structured as follows:
    105  *
    106  *     - init the "IN" params in the ufi structure
    107  *   ReFault:
    108  *     - do lookups [locks maps], check protection, handle needs_copy
    109  *     - check for case 0 fault (error)
    110  *     - establish "range" of fault
    111  *     - if we have an amap lock it and extract the anons
    112  *     - if sequential advice deactivate pages behind us
    113  *     - at the same time check pmap for unmapped areas and anon for pages
    114  *	 that we could map in (and do map it if found)
    115  *     - check object for resident pages that we could map in
    116  *     - if (case 2) goto Case2
    117  *     - >>> handle case 1
    118  *           - ensure source anon is resident in RAM
    119  *           - if case 1B alloc new anon and copy from source
    120  *           - map the correct page in
    121  *   Case2:
    122  *     - >>> handle case 2
    123  *           - ensure source page is resident (if uobj)
    124  *           - if case 2B alloc new anon and copy from source (could be zero
    125  *		fill if uobj == NULL)
    126  *           - map the correct page in
    127  *     - done!
    128  *
    129  * note on paging:
    130  *   if we have to do I/O we place a PG_BUSY page in the correct object,
    131  * unlock everything, and do the I/O.   when I/O is done we must reverify
    132  * the state of the world before assuming that our data structures are
    133  * valid.   [because mappings could change while the map is unlocked]
    134  *
    135  *  alternative 1: unbusy the page in question and restart the page fault
    136  *    from the top (ReFault).   this is easy but does not take advantage
    137  *    of the information that we already have from our previous lookup,
    138  *    although it is possible that the "hints" in the vm_map will help here.
    139  *
    140  * alternative 2: the system already keeps track of a "version" number of
    141  *    a map.   [i.e. every time you write-lock a map (e.g. to change a
    142  *    mapping) you bump the version number up by one...]   so, we can save
    143  *    the version number of the map before we release the lock and start I/O.
    144  *    then when I/O is done we can relock and check the version numbers
    145  *    to see if anything changed.    this might save us some over 1 because
    146  *    we don't have to unbusy the page and may be less compares(?).
    147  *
    148  * alternative 3: put in backpointers or a way to "hold" part of a map
    149  *    in place while I/O is in progress.   this could be complex to
    150  *    implement (especially with structures like amap that can be referenced
    151  *    by multiple map entries, and figuring out what should wait could be
    152  *    complex as well...).
    153  *
    154  * given that we are not currently multiprocessor or multithreaded we might
    155  * as well choose alternative 2 now.   maybe alternative 3 would be useful
    156  * in the future.    XXX keep in mind for future consideration//rechecking.
    157  */
    158 
    159 /*
    160  * local data structures
    161  */
    162 
    163 struct uvm_advice {
    164   int advice;
    165   int nback;
    166   int nforw;
    167 };
    168 
    169 /*
    170  * page range array:
    171  * note: index in array must match "advice" value
    172  * XXX: borrowed numbers from freebsd.   do they work well for us?
    173  */
    174 
    175 static struct uvm_advice uvmadvice[] = {
    176   { MADV_NORMAL, 3, 4 },
    177   { MADV_RANDOM, 0, 0 },
    178   { MADV_SEQUENTIAL, 8, 7},
    179 };
    180 
    181 #define UVM_MAXRANGE 16	/* must be max() of nback+nforw+1 */
    182 
    183 /*
    184  * private prototypes
    185  */
    186 
    187 static void uvmfault_amapcopy __P((struct uvm_faultinfo *));
    188 static __inline void uvmfault_anonflush __P((struct vm_anon **, int));
    189 
    190 /*
    191  * inline functions
    192  */
    193 
    194 /*
    195  * uvmfault_anonflush: try and deactivate pages in specified anons
    196  *
    197  * => does not have to deactivate page if it is busy
    198  */
    199 
    200 static __inline void uvmfault_anonflush(anons, n)
    201 
    202 struct vm_anon **anons;
    203 int n;
    204 
    205 {
    206   int lcv;
    207   struct vm_page *pg;
    208 
    209   for (lcv = 0 ; lcv < n ; lcv++) {
    210     if (anons[lcv] == NULL)
    211       continue;
    212     simple_lock(&anons[lcv]->an_lock);
    213     pg = anons[lcv]->u.an_page;
    214     if (pg && (pg->flags & PG_BUSY) == 0 && pg->loan_count == 0) {
    215       uvm_lock_pageq();
    216       if (pg->wire_count == 0) {
    217         pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE);
    218         uvm_pagedeactivate(pg);
    219       }
    220       uvm_unlock_pageq();
    221     }
    222     simple_unlock(&anons[lcv]->an_lock);
    223   }
    224 }
    225 
    226 /*
    227  * normal functions
    228  */
    229 
    230 /*
    231  * uvmfault_amapcopy: clear "needs_copy" in a map.
    232  *
    233  * => called with VM data structures unlocked (usually, see below)
    234  * => we get a write lock on the maps and clear needs_copy for a VA
    235  * => if we are out of RAM we sleep (waiting for more)
    236  */
    237 
    238 static void uvmfault_amapcopy(ufi)
    239 
    240 struct uvm_faultinfo *ufi;
    241 
    242 {
    243 
    244   /*
    245    * while we haven't done the job
    246    */
    247 
    248   while (1) {
    249 
    250     /*
    251      * no mapping?  give up.
    252      */
    253 
    254     if (uvmfault_lookup(ufi, TRUE) == FALSE)
    255       return;
    256 
    257     /*
    258      * copy if needed.
    259      */
    260 
    261     if (UVM_ET_ISNEEDSCOPY(ufi->entry))
    262       amap_copy(ufi->map, ufi->entry, M_NOWAIT, TRUE, ufi->rvaddr,
    263 		ufi->rvaddr + 1);
    264 
    265     /*
    266      * didn't work?  must be out of RAM.   unlock and sleep.
    267      */
    268 
    269     if (UVM_ET_ISNEEDSCOPY(ufi->entry)) {
    270       uvmfault_unlockmaps(ufi, TRUE);
    271       uvm_wait("fltamapcopy");
    272       continue;
    273     }
    274 
    275     /*
    276      * got it!   unlock and return.
    277      */
    278 
    279     uvmfault_unlockmaps(ufi, TRUE);
    280     return;
    281   }
    282   /*NOTREACHED*/
    283 }
    284 
    285 /*
    286  * uvmfault_anonget: get data in an anon into a non-busy, non-released
    287  * page in that anon.
    288  *
    289  * => maps, amap, and anon locked by caller.
    290  * => if we fail (result != VM_PAGER_OK) we unlock everything.
    291  * => if we are successful, we return with everything still locked.
    292  * => we don't move the page on the queues [gets moved later]
    293  * => if we allocate a new page [we_own], it gets put on the queues.
    294  *    either way, the result is that the page is on the queues at return time
    295  * => for pages which are on loan from a uvm_object (and thus are not
    296  *    owned by the anon): if successful, we return with the owning object
    297  *    locked.   the caller must unlock this object when it unlocks everything
    298  *    else.
    299  */
    300 
    301 int uvmfault_anonget(ufi, amap, anon)
    302 
    303 struct uvm_faultinfo *ufi;
    304 struct vm_amap *amap;
    305 struct vm_anon *anon;
    306 
    307 {
    308   boolean_t we_own;	/* we own anon's page? */
    309   boolean_t locked;	/* did we relock? */
    310   struct vm_page *pg;
    311   int result;
    312   UVMHIST_FUNC("uvmfault_anonget"); UVMHIST_CALLED(maphist);
    313 
    314   result = 0;		/* XXX shut up gcc */
    315   uvmexp.fltanget++;	/* XXXDCD should this go here or below? */
    316 
    317   /*
    318    * loop until we get it, or fail.
    319    */
    320 
    321   while (1) {
    322 
    323     we_own = FALSE;		/* TRUE if we set PG_BUSY on a page */
    324     pg = anon->u.an_page;
    325 
    326     /*
    327      * if there is a resident page and it is loaned, then anon may not
    328      * own it.   call out to uvm_anon_lockpage() to ensure the real owner
    329      * of the page has been identified and locked.
    330      */
    331 
    332     if (pg && pg->loan_count)
    333         pg = uvm_anon_lockloanpg(anon);
    334 
    335     /*
    336      * page there?   make sure it is not busy/released.
    337      */
    338 
    339     if (pg) {
    340 
    341       /*
    342        * at this point, if the page has a uobject [meaning we have it on
    343        * loan], then that uobject is locked by us!   if the page is busy,
    344        * we drop all the locks (including uobject) and try again.
    345        */
    346 
    347       if ((pg->flags & (PG_BUSY|PG_RELEASED)) == 0) {
    348         UVMHIST_LOG(maphist, "<- OK",0,0,0,0);
    349 	return(VM_PAGER_OK);
    350       }
    351       pg->flags |= PG_WANTED;
    352       uvmexp.fltpgwait++;
    353 
    354       /* the last unlock must be an atomic unlock+wait on the owner of page */
    355       if (pg->uobject) {	/* owner is uobject ? */
    356         uvmfault_unlockall(ufi, amap, NULL, anon);
    357         UVMHIST_LOG(maphist, " unlock+wait on uobj",0,0,0,0);
    358         UVM_UNLOCK_AND_WAIT(pg, &pg->uobject->vmobjlock, FALSE, "anonget1",0);
    359       } else {
    360         /* anon owns page */
    361         uvmfault_unlockall(ufi, amap, NULL, NULL);
    362         UVMHIST_LOG(maphist, " unlock+wait on anon",0,0,0,0);
    363         UVM_UNLOCK_AND_WAIT(pg,&anon->an_lock,0,"anonget2",0);
    364       }
    365       /* ready to relock and try again */
    366 
    367     } else {
    368 
    369       /*
    370        * no page, we must try and bring it in.
    371        */
    372       pg = uvm_pagealloc(NULL, 0, anon);
    373 
    374       if (pg == NULL) {		/* out of RAM.  */
    375 
    376 	uvmfault_unlockall(ufi, amap, NULL, anon);
    377 	uvmexp.fltnoram++;
    378         UVMHIST_LOG(maphist, "  noram -- UVM_WAIT",0,0,0,0);
    379 	uvm_wait("flt_noram1");
    380 	/* ready to relock and try again */
    381 
    382       } else {
    383 
    384 	we_own = TRUE;			/* we set the PG_BUSY bit */
    385 	uvmfault_unlockall(ufi, amap, NULL, anon);
    386 
    387 	/*
    388 	 * we are passing a PG_BUSY+PG_FAKE+PG_CLEAN page into the
    389 	 * uvm_swap_get function with all data structures unlocked.
    390 	 */
    391 	result = uvm_swap_get(pg, anon->an_swslot, PGO_SYNCIO);
    392 	/* XXXMRG: we need a counter here! */
    393 
    394 	/*
    395 	 * we clean up after the i/o below in the "we_own" case
    396 	 */
    397 	/* ready to relock and try again */
    398       }
    399 
    400     }
    401 
    402     /*
    403      * now relock and try again
    404      */
    405 
    406     locked = uvmfault_relock(ufi);
    407     if (locked) {
    408       simple_lock(&amap->am_l);
    409     }
    410     if (locked || we_own)
    411       simple_lock(&anon->an_lock);
    412 
    413     /*
    414      * if we own the page (i.e. we set PG_BUSY), then we need to clean
    415      * up after the I/O. there are three cases to consider:
    416      *   [1] page released during I/O: free anon and ReFault.
    417      *   [2] I/O not OK.   free the page and cause the fault to fail.
    418      *   [3] I/O OK!   activate the page and sync with the non-we_own
    419      *		case (i.e. drop anon lock if not locked).
    420      */
    421 
    422     if (we_own) {
    423 
    424       if (pg->flags & PG_WANTED) {
    425 	thread_wakeup(pg);		/* still holding object lock */
    426       }
    427       pg->flags &= ~(PG_WANTED|PG_BUSY|PG_FAKE);	/* un-busy! */
    428       UVM_PAGE_OWN(pg, NULL);
    429 
    430       /*
    431        * if we were RELEASED during I/O, then our anon is no longer
    432        * part of an amap.   we need to free the anon and try again.
    433        */
    434       if (pg->flags & PG_RELEASED) {
    435         pmap_page_protect(PMAP_PGARG(pg), VM_PROT_NONE); /* to be safe */
    436 	uvm_anfree(anon);	/* frees page for us */
    437 	if (locked)
    438 	  uvmfault_unlockall(ufi, amap, NULL, NULL);
    439 	uvmexp.fltpgrele++;
    440         UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    441 	return(VM_PAGER_REFAULT);		/* refault! */
    442       }
    443 
    444       if (result != VM_PAGER_OK) {
    445 #ifdef DIAGNOSTIC
    446 	if (result == VM_PAGER_PEND)
    447 	  panic("uvmfault_anonget: got PENDING for non-async I/O");
    448 #endif
    449 	/* remove page from anon */
    450 	anon->u.an_page = NULL;
    451 	/*
    452 	 * note: page was never !PG_BUSY, so it can't be mapped and thus
    453 	 * no need to pmap_page_protect it...
    454 	 */
    455         uvm_lock_pageq();
    456 	uvm_pagefree(pg);
    457         uvm_unlock_pageq();
    458 
    459  	if (locked)
    460 	  uvmfault_unlockall(ufi, amap, NULL, anon);
    461 	else
    462 	  simple_unlock(&anon->an_lock);
    463         UVMHIST_LOG(maphist, "<- ERROR", 0,0,0,0);
    464 	return(VM_PAGER_ERROR);
    465       }
    466 
    467       /* must be OK, clear modify (already PG_CLEAN) and activate */
    468       pmap_clear_modify(PMAP_PGARG(pg));
    469       uvm_lock_pageq();
    470       uvm_pageactivate(pg);
    471       uvm_unlock_pageq();
    472       if (!locked)
    473 	simple_unlock(&anon->an_lock);
    474     }
    475 
    476     /*
    477      * we were not able to relock.   restart fault.
    478      */
    479 
    480     if (!locked) {
    481       UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    482       return(VM_PAGER_REFAULT);
    483     }
    484 
    485     /*
    486      * verify no one has touched the amap and moved the anon on us.
    487      */
    488 
    489     if (amap_lookup(&ufi->entry->aref,
    490 		    ufi->rvaddr - ufi->entry->start) != anon) {
    491 
    492       uvmfault_unlockall(ufi, amap, NULL, anon);
    493       UVMHIST_LOG(maphist, "<- REFAULT", 0,0,0,0);
    494       return(VM_PAGER_REFAULT);
    495     }
    496 
    497     /*
    498      * try it again!
    499      */
    500 
    501     uvmexp.fltanretry++;
    502     continue;
    503   } /* while (1) */
    504 
    505   /*NOTREACHED*/
    506 }
    507 
    508 /*
    509  *   F A U L T   -   m a i n   e n t r y   p o i n t
    510  */
    511 
    512 /*
    513  * uvm_fault: page fault handler
    514  *
    515  * => called from MD code to resolve a page fault
    516  * => VM data structures usually should be unlocked.   however, it is
    517  *	possible to call here with the main map locked if the caller
    518  *	gets a write lock, sets it recusive, and then calls us (c.f.
    519  *	uvm_map_pageable).   this should be avoided because it keeps
    520  *	the map locked off during I/O.
    521  */
    522 
    523 int uvm_fault(orig_map, vaddr, fault_type, access_type)
    524 
    525 vm_map_t orig_map;
    526 vm_offset_t vaddr;
    527 vm_fault_t fault_type;
    528 vm_prot_t access_type;
    529 
    530 {
    531   struct uvm_faultinfo ufi;
    532   vm_prot_t enter_prot;
    533   boolean_t wired, narrow, promote, locked, shadowed;
    534   int npages, nback, nforw, centeridx, result, lcv, gotpages;
    535   vm_offset_t orig_startva, startva, objaddr, currva, pa, offset;
    536   struct vm_amap *amap;
    537   struct uvm_object *uobj;
    538   struct vm_anon *anons_store[UVM_MAXRANGE], **anons, *anon, *oanon;
    539   struct vm_page *pages[UVM_MAXRANGE], *pg, *uobjpage;
    540   UVMHIST_FUNC("uvm_fault"); UVMHIST_CALLED(maphist);
    541 
    542   UVMHIST_LOG(maphist, "(map=0x%x, vaddr=0x%x, ft=%d, at=%d)",
    543 	      orig_map, vaddr, fault_type, access_type);
    544 
    545   anon = NULL; /* XXX: shut up gcc */
    546 
    547   uvmexp.faults++;	/* XXX: locking? */
    548 
    549   /*
    550    * init the IN parameters in the ufi
    551    */
    552 
    553   ufi.orig_map = orig_map;
    554   ufi.orig_rvaddr = trunc_page(vaddr);
    555   ufi.orig_size = PAGE_SIZE;	/* can't get any smaller than this */
    556   if (fault_type == VM_FAULT_WIRE)
    557     narrow = TRUE;		/* don't look for neighborhood pages on wire */
    558   else
    559     narrow = FALSE;		/* normal fault */
    560 
    561   /*
    562    * "goto ReFault" means restart the page fault from ground zero.
    563    */
    564 ReFault:
    565 
    566   /*
    567    * lookup and lock the maps
    568    */
    569 
    570   if (uvmfault_lookup(&ufi, FALSE) == FALSE) {
    571     UVMHIST_LOG(maphist, "<- no mapping @ 0x%x", vaddr, 0,0,0);
    572     return(KERN_INVALID_ADDRESS);
    573   }
    574   /* locked: maps(read) */
    575 
    576   /*
    577    * check protection
    578    */
    579 
    580   if ((ufi.entry->protection & access_type) != access_type) {
    581     UVMHIST_LOG(maphist, "<- protection failure (prot=0x%x, access=0x%x)",
    582                 ufi.entry->protection, access_type, 0, 0);
    583     uvmfault_unlockmaps(&ufi, FALSE);
    584     return(KERN_PROTECTION_FAILURE);
    585   }
    586 
    587   /*
    588    * "enter_prot" is the protection we want to enter the page in at.
    589    * for certain pages (e.g. copy-on-write pages) this protection can
    590    * be more strict than ufi.entry->protection.
    591    * "wired" means either the entry is wired or we are fault-wiring the pg.
    592    */
    593 
    594   enter_prot = ufi.entry->protection;
    595   wired = (ufi.entry->wired_count != 0) || (fault_type == VM_FAULT_WIRE);
    596   if (wired)
    597     access_type = enter_prot; /* full access for wired */
    598 
    599   /*
    600    * handle "needs_copy" case.   if we need to copy the amap we will
    601    * have to drop our readlock and relock it with a write lock.  (we
    602    * need a write lock to change anything in a map entry [e.g. needs_copy]).
    603    */
    604 
    605   if (UVM_ET_ISNEEDSCOPY(ufi.entry)) {
    606     if ((access_type & VM_PROT_WRITE) || (ufi.entry->object.uvm_obj == NULL)) {
    607 
    608       /* need to clear */
    609       UVMHIST_LOG(maphist, "  need to clear needs_copy and refault",0,0,0,0);
    610       uvmfault_unlockmaps(&ufi, FALSE);
    611       uvmfault_amapcopy(&ufi);
    612       uvmexp.fltamcopy++;
    613       goto ReFault;
    614 
    615     } else {
    616 
    617       /* ensure that we pmap_enter page R/O since needs_copy is still true */
    618       enter_prot = enter_prot & ~VM_PROT_WRITE;
    619 
    620     }
    621   }
    622 
    623   /*
    624    * identify the players
    625    */
    626 
    627   amap = ufi.entry->aref.ar_amap;	/* top layer */
    628   uobj = ufi.entry->object.uvm_obj;	/* bottom layer */
    629 
    630   /*
    631    * check for a case 0 fault.  if nothing backing the entry then error now.
    632    */
    633 
    634   if (amap == NULL && uobj == NULL) {
    635     uvmfault_unlockmaps(&ufi, FALSE);
    636     UVMHIST_LOG(maphist,"<- no backing store, no overlay",0,0,0,0);
    637     return(KERN_INVALID_ADDRESS);
    638   }
    639 
    640   /*
    641    * establish range of interest based on advice from mapper
    642    * and then clip to fit map entry.   note that we only want
    643    * to do this the first time through the fault.   if we
    644    * ReFault we will disable this by setting "narrow" to true.
    645    */
    646 
    647   if (narrow == FALSE) {
    648 
    649     /* wide fault (!narrow) */
    650 #ifdef DIAGNOSTIC
    651     if (uvmadvice[ufi.entry->advice].advice != ufi.entry->advice)
    652       panic("fault: advice mismatch!");
    653 #endif
    654     nback = min(uvmadvice[ufi.entry->advice].nback,
    655 		(ufi.rvaddr - ufi.entry->start) / PAGE_SIZE);
    656     startva = ufi.rvaddr - (nback * PAGE_SIZE);
    657     orig_startva = ufi.orig_rvaddr - (nback * PAGE_SIZE);
    658     nforw = min(uvmadvice[ufi.entry->advice].nforw,
    659 		((ufi.entry->end - ufi.rvaddr) / PAGE_SIZE) - 1);
    660     /* note: "-1" because we don't want to count the faulting page as forw */
    661     npages = nback + nforw + 1;
    662     centeridx = nback;
    663 
    664     narrow = FALSE;	/* ensure only once per-fault */
    665 
    666   } else {
    667 
    668     /* narrow fault! */
    669     nback = nforw = 0;
    670     startva = ufi.rvaddr;
    671     orig_startva = ufi.orig_rvaddr;
    672     npages = 1;
    673     centeridx = 0;
    674 
    675   }
    676 
    677   /* locked: maps(read) */
    678   UVMHIST_LOG(maphist, "  narrow=%d, back=%d, forw=%d, orig_startva=0x%x",
    679 	narrow, nback, nforw, orig_startva);
    680   UVMHIST_LOG(maphist, "  entry=0x%x, amap=0x%x, obj=0x%x", ufi.entry,
    681 	amap, uobj, 0);
    682 #if 0
    683 /* TEST TEST TEST ONLY ... DELETE  */
    684 {/*XXXCDC:DBG*/
    685 vm_map_t map = ufi.map;
    686 UVMHIST_LOG(maphist, "  startva=0x%x, entrystart=0x%x", startva,
    687 	ufi.entry->start,0,0); /*XXXCDC: tmp */
    688 uvm_map_print(map, 1);
    689 }/*XXXCDC:DBG*/
    690 #endif
    691 
    692   /*
    693    * if we've got an amap, lock it and extract current anons.
    694    */
    695 
    696   if (amap) {
    697     simple_lock(&amap->am_l);
    698     anons = anons_store;
    699     amap_lookups(&ufi.entry->aref, startva - ufi.entry->start, anons, npages);
    700   } else {
    701     anons = NULL;	/* to be safe */
    702   }
    703 
    704   /* locked: maps(read), amap(if there) */
    705 
    706   /*
    707    * for MADV_SEQUENTIAL mappings we want to deactivate the back pages
    708    * now and then forget about them (for the rest of the fault).
    709    */
    710 
    711   if (ufi.entry->advice == MADV_SEQUENTIAL) {
    712 
    713     UVMHIST_LOG(maphist, "  MADV_SEQUENTIAL: flushing backpages",0,0,0,0);
    714     /* flush back-page anons? */
    715     if (amap)
    716       uvmfault_anonflush(anons, nback);
    717 
    718     /* flush object? */
    719     if (uobj) {
    720       objaddr = (startva - ufi.entry->start) + ufi.entry->offset;
    721       simple_lock(&uobj->vmobjlock);
    722       (void) uobj->pgops->pgo_flush(uobj, objaddr, objaddr +
    723 				    (nback * PAGE_SIZE), PGO_DEACTIVATE);
    724       simple_unlock(&uobj->vmobjlock);
    725     }
    726 
    727     /* now forget about the backpages */
    728     if (amap)
    729       anons += nback;
    730     startva = startva + (nback * PAGE_SIZE);
    731     orig_startva = orig_startva + (nback * PAGE_SIZE);
    732     npages -= nback;
    733     nback = centeridx = 0;
    734   }
    735 
    736   /* locked: maps(read), amap(if there) */
    737 
    738   /*
    739    * map in the backpages and frontpages we found in the amap in hopes
    740    * of preventing future faults.    we also init the pages[] array as
    741    * we go.
    742    */
    743 
    744   currva = orig_startva;
    745   shadowed = FALSE;
    746   for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) {
    747 
    748     /*
    749      * dont play with VAs that are already mapped (except for center)
    750      * XXX: return value of pmap_extract disallows PA 0
    751      */
    752     if (lcv != centeridx) {
    753       pa = pmap_extract(ufi.orig_map->pmap, currva);
    754       if (pa != NULL) {
    755         pages[lcv] = PGO_DONTCARE;
    756         continue;
    757       }
    758     }
    759 
    760     /*
    761      * unmapped or center page.   check if any anon at this level.
    762      */
    763     if (amap == NULL || anons[lcv] == NULL) {
    764       pages[lcv] = NULL;
    765       continue;
    766     }
    767 
    768     /*
    769      * check for present page and map if possible.   re-activate it.
    770      */
    771 
    772     pages[lcv] = PGO_DONTCARE;
    773     if (lcv == centeridx) {		/* save center for later! */
    774       shadowed = TRUE;
    775       continue;
    776     }
    777     anon = anons[lcv];
    778     simple_lock(&anon->an_lock);
    779     /* ignore loaned pages */
    780     if (anon->u.an_page && anon->u.an_page->loan_count == 0 &&
    781 	(anon->u.an_page->flags & (PG_RELEASED|PG_BUSY)) == 0) {
    782       uvm_lock_pageq();
    783       uvm_pageactivate(anon->u.an_page);	/* reactivate */
    784       uvm_unlock_pageq();
    785       UVMHIST_LOG(maphist, "  MAPPING: n anon: pm=0x%x, va=0x%x, pg=0x%x",
    786 	ufi.orig_map->pmap, currva, anon->u.an_page, 0);
    787       uvmexp.fltnamap++;
    788       pmap_enter(ufi.orig_map->pmap, currva, VM_PAGE_TO_PHYS(anon->u.an_page),
    789 		 (anon->an_ref > 1) ? VM_PROT_READ : enter_prot,
    790 		 (ufi.entry->wired_count != 0));
    791     }
    792     simple_unlock(&anon->an_lock);
    793   }
    794 
    795   /* locked: maps(read), amap(if there) */
    796   /* (shadowed == TRUE) if there is an anon at the faulting address */
    797   UVMHIST_LOG(maphist, "  shadowed=%d, will_get=%d", shadowed,
    798 	(uobj && shadowed == FALSE),0,0);
    799 
    800   /*
    801    * note that if we are really short of RAM we could sleep in the above
    802    * call to pmap_enter with everything locked.   bad?
    803    * XXXCDC: this is fixed in PMAP_NEW (no sleep alloc's in pmap)
    804    */
    805 
    806   /*
    807    * if the desired page is not shadowed by the amap and we have a
    808    * backing object, then we check to see if the backing object would
    809    * prefer to handle the fault itself (rather than letting us do it
    810    * with the usual pgo_get hook).  the backing object signals this by
    811    * providing a pgo_fault routine.
    812    *
    813    * note: pgo_fault can obtain the correct VA for pmap_enter by using:
    814    *    real_va = [ ufi->orig_rvaddr + (startva - ufi->rvaddr)]
    815    */
    816 
    817   if (uobj && shadowed == FALSE && uobj->pgops->pgo_fault != NULL) {
    818 
    819     simple_lock(&uobj->vmobjlock);
    820 
    821     /* locked: maps(read), amap (if there), uobj */
    822     result = uobj->pgops->pgo_fault(&ufi, startva, pages, npages,
    823 				    centeridx, fault_type, access_type,
    824 				    PGO_LOCKED);
    825     /* locked: nothing, pgo_fault has unlocked everything */
    826 
    827     if (result == VM_PAGER_OK)
    828       return(KERN_SUCCESS);           /* pgo_fault did pmap enter */
    829     else if (result == VM_PAGER_REFAULT)
    830       goto ReFault;                   /* try again! */
    831     else
    832       return(KERN_PROTECTION_FAILURE);
    833   }
    834 
    835   /*
    836    * now, if the desired page is not shadowed by the amap and we have
    837    * a backing object that does not have a special fault routine, then
    838    * we ask (with pgo_get) the object for resident pages that we care
    839    * about and attempt to map them in.  we do not let pgo_get block
    840    * (PGO_LOCKED).
    841    *
    842    * ("get" has the option of doing a pmap_enter for us)
    843    */
    844 
    845   if (uobj && shadowed == FALSE) {
    846     simple_lock(&uobj->vmobjlock);
    847 
    848     /* locked (!shadowed): maps(read), amap (if there), uobj */
    849     /* the following call to pgo_get does _not_ change locking state */
    850 
    851     uvmexp.fltlget++;
    852     gotpages = npages;
    853     result = uobj->pgops->pgo_get(uobj, ufi.entry->offset +
    854 				(startva - ufi.entry->start),
    855 				pages, &gotpages, centeridx,
    856 				UVM_ET_ISCOPYONWRITE(ufi.entry) ?
    857 				VM_PROT_READ : access_type,
    858 				ufi.entry->advice, PGO_LOCKED);
    859 
    860     /*
    861      * check for pages to map, if we got any
    862      */
    863 
    864     uobjpage = NULL;
    865 
    866     if (gotpages) {
    867       currva = orig_startva;
    868       for (lcv = 0 ; lcv < npages ; lcv++, currva += PAGE_SIZE) {
    869 
    870         if (pages[lcv] == NULL || pages[lcv] == PGO_DONTCARE)
    871 	  continue;
    872 
    873 #ifdef DIAGNOSTIC
    874         /*
    875          * pager sanity check: pgo_get with PGO_LOCKED should never
    876          * return a released page to us.
    877          */
    878         if (pages[lcv]->flags & PG_RELEASED)
    879 	  panic("uvm_fault: pgo_get PGO_LOCKED gave us a RELEASED page");
    880 #endif
    881 
    882         /*
    883          * if center page is resident and not PG_BUSY|PG_RELEASED
    884          * then pgo_get made it PG_BUSY for us and gave us a handle
    885          * to it.   remember this page as "uobjpage." (for later use).
    886          */
    887 
    888         if (lcv == centeridx) {
    889 	  uobjpage = pages[lcv];
    890 	  UVMHIST_LOG(maphist, "  got uobjpage (0x%x) with locked get",
    891 		  uobjpage, 0,0,0);
    892 	  continue;
    893         }
    894 
    895         /*
    896          * note: calling pgo_get with locked data structures returns us
    897          * pages which are neither busy nor released, so we don't need to
    898          * check for this.   we can just directly enter the page (after
    899          * moving it to the head of the active queue [useful?]).
    900          */
    901 
    902         uvm_lock_pageq();
    903         uvm_pageactivate(pages[lcv]);	/* reactivate */
    904         uvm_unlock_pageq();
    905         UVMHIST_LOG(maphist, "  MAPPING: n obj: pm=0x%x, va=0x%x, pg=0x%x",
    906 	  ufi.orig_map->pmap, currva, pages[lcv], 0);
    907         uvmexp.fltnomap++;
    908         pmap_enter(ufi.orig_map->pmap, currva, VM_PAGE_TO_PHYS(pages[lcv]),
    909 	   UVM_ET_ISCOPYONWRITE(ufi.entry) ?  VM_PROT_READ : enter_prot, wired);
    910 
    911         /*
    912          * NOTE: page can't be PG_WANTED or PG_RELEASED because we've held
    913          * the lock the whole time we've had the handle.
    914          */
    915         pages[lcv]->flags &= ~(PG_BUSY);		/* un-busy! */
    916         UVM_PAGE_OWN(pages[lcv], NULL);
    917 
    918         /* done! */
    919       }	/* for "lcv" loop */
    920     }   /* "gotpages" != 0 */
    921 
    922     /* note: object still _locked_ */
    923   } else {
    924 
    925     uobjpage = NULL;
    926 
    927   }
    928 
    929   /* locked (shadowed): maps(read), amap */
    930   /* locked (!shadowed): maps(read), amap(if there),
    931      uobj(if !null), uobjpage(if !null) */
    932 
    933   /*
    934    * note that at this point we are done with any front or back pages.
    935    * we are now going to focus on the center page (i.e. the one we've
    936    * faulted on).  if we have faulted on the top (anon) layer
    937    * [i.e. case 1], then the anon we want is anons[centeridx] (we have
    938    * not touched it yet).  if we have faulted on the bottom (uobj)
    939    * layer [i.e. case 2] and the page was both present and available,
    940    * then we've got a pointer to it as "uobjpage" and we've already
    941    * made it BUSY and tmpwired it.
    942    */
    943 
    944   /*
    945    * there are four possible cases we must address: 1A, 1B, 2A, and 2B
    946    */
    947 
    948   /*
    949    * redirect case 2: if we are not shadowed, go to case 2.
    950    */
    951 
    952   if (shadowed == FALSE)
    953     goto Case2;
    954 
    955   /* locked: maps(read), amap */
    956 
    957   /*
    958    * handle case 1: fault on an anon in our amap
    959    */
    960 
    961   anon = anons[centeridx];
    962   UVMHIST_LOG(maphist, "  case 1 fault: anon=0x%x", anon, 0,0,0);
    963   simple_lock(&anon->an_lock);
    964 
    965   /* locked: maps(read), amap, anon */
    966 
    967   /*
    968    * no matter if we have case 1A or case 1B we are going to need to
    969    * have the anon's memory resident.   ensure that now.
    970    */
    971 
    972   /*
    973    * let uvmfault_anonget do the dirty work.   if it fails (!OK) it will
    974    * unlock for us.   if it is OK, locks are still valid and locked.
    975    * also, if it is OK, then the anon's page is on the queues.
    976    * if the page is on loan from a uvm_object, then anonget will
    977    * lock that object for us if it does not fail.
    978    */
    979 
    980   result = uvmfault_anonget(&ufi, amap, anon);
    981 
    982   if (result == VM_PAGER_REFAULT)
    983     goto ReFault;
    984 
    985   if (result == VM_PAGER_AGAIN) {
    986     tsleep((caddr_t)&lbolt, PVM, "fltagain1", 0);
    987     goto ReFault;
    988   }
    989 
    990   if (result != VM_PAGER_OK)
    991     return(KERN_PROTECTION_FAILURE);		/* XXX??? */
    992 
    993   /*
    994    * uobj is non null if the page is on loan from an object (i.e. uobj)
    995    */
    996 
    997   uobj = anon->u.an_page->uobject;	/* locked by anonget if !NULL */
    998 
    999   /* locked: maps(read), amap, anon, uobj(if one) */
   1000 
   1001   /*
   1002    * special handling for loaned pages
   1003    */
   1004   if (anon->u.an_page->loan_count) {
   1005 
   1006     if ((access_type & VM_PROT_WRITE) == 0) {
   1007 
   1008       /*
   1009        * for read faults on loaned pages we just cap the protection
   1010        * at read-only.
   1011        */
   1012 
   1013       enter_prot = enter_prot & ~VM_PROT_WRITE;
   1014 
   1015     } else {
   1016       /*
   1017        * note that we can't allow writes into a loaned page!
   1018        *
   1019        * if we have a write fault on a loaned page in an anon then we
   1020        * need to look at the anon's ref count.  if it is greater than
   1021        * one then we are going to do a normal copy-on-write fault into
   1022        * a new anon (this is not a problem).  however, if the
   1023        * reference count is one (a case where we would normally allow
   1024        * a write directly to the page) then we need to kill the loan
   1025        * before we continue.
   1026        */
   1027 
   1028       if (anon->an_ref == 1) {		/* >1 case is already ok */
   1029 
   1030 	/* get new un-owned replacement page */
   1031 	pg = uvm_pagealloc(NULL, 0, NULL);
   1032 	if (pg == NULL) {
   1033 	  uvmfault_unlockall(&ufi, amap, uobj, anon);
   1034 	  uvm_wait("flt_noram2");
   1035 	  goto ReFault;
   1036 	}
   1037 
   1038 	/* copy data, kill loan, and drop uobj lock (if any) */
   1039 	uvm_pagecopy(anon->u.an_page, pg);		  /* copy old -> new */
   1040 	/* force reload */
   1041 	pmap_page_protect(PMAP_PGARG(anon->u.an_page), VM_PROT_NONE);
   1042 	uvm_lock_pageq();				  /* KILL loan */
   1043         if (uobj)
   1044 	  anon->u.an_page->loan_count--;		/* if we were loaning */
   1045 	anon->u.an_page->uanon = NULL;
   1046 	anon->u.an_page->pqflags &= ~PQ_ANON;		/* in case we owned */
   1047 	uvm_unlock_pageq();
   1048 	if (uobj) {
   1049 	  simple_unlock(&uobj->vmobjlock);
   1050 	  uobj = NULL;
   1051 	}
   1052 
   1053 	/* install new page in anon */
   1054 	anon->u.an_page = pg;
   1055 	pg->uanon = anon;
   1056 	pg->pqflags |= PQ_ANON;
   1057         pg->flags &= ~(PG_BUSY|PG_FAKE);
   1058         UVM_PAGE_OWN(pg, NULL);
   1059 
   1060 	/* done! */
   1061       }     /* ref == 1 */
   1062     }       /* write fault */
   1063   }         /* loan count */
   1064 
   1065   /*
   1066    * if we are case 1B then we will need to allocate a new blank
   1067    * anon to transfer the data into.   note that we have a lock
   1068    * on anon, so no one can busy or release the page until we are done.
   1069    * also note that the ref count can't drop to zero here because
   1070    * it is > 1 and we are only dropping one ref.
   1071    *
   1072    * in the (hopefully very rare) case that we are out of RAM we
   1073    * will unlock, wait for more RAM, and refault.
   1074    *
   1075    * if we are out of anon VM we kill the process (XXX: could wait?).
   1076    */
   1077 
   1078   if ((access_type & VM_PROT_WRITE) != 0 && anon->an_ref > 1) {
   1079 
   1080     UVMHIST_LOG(maphist, "  case 1B: COW fault",0,0,0,0);
   1081     uvmexp.flt_acow++;
   1082     oanon = anon;		/* oanon = old, locked anon */
   1083     anon = uvm_analloc();
   1084     if (anon)
   1085       pg = uvm_pagealloc(NULL, 0, anon);
   1086 #if defined(sparc)
   1087     else
   1088       pg = NULL; /* XXX: shut up gcc unused var warning on sparc */
   1089 #endif
   1090 
   1091     /* check for out of RAM */
   1092     if (anon == NULL || pg == NULL) {
   1093       if (anon)
   1094 	uvm_anfree(anon);
   1095       uvmfault_unlockall(&ufi, amap, uobj, oanon);
   1096       if (anon == NULL) {
   1097 	UVMHIST_LOG(maphist, "<- failed.  out of VM",0,0,0,0);
   1098 	uvmexp.fltnoanon++;
   1099 	return(KERN_RESOURCE_SHORTAGE); /* XXX: OUT OF VM, ??? */
   1100       }
   1101       uvmexp.fltnoram++;
   1102       uvm_wait("flt_noram3");	/* out of RAM, wait for more */
   1103       goto ReFault;
   1104     }
   1105 
   1106     /* got all resources, replace anon with nanon */
   1107 
   1108     uvm_pagecopy(oanon->u.an_page, pg);	/* pg now !PG_CLEAN */
   1109     pg->flags &= ~(PG_BUSY|PG_FAKE);	/* un-busy! new page */
   1110     UVM_PAGE_OWN(pg, NULL);
   1111     amap_add(&ufi.entry->aref, ufi.rvaddr - ufi.entry->start, anon, 1);
   1112     oanon->an_ref--;   /* deref: can not drop to zero here by defn! */
   1113 
   1114     /*
   1115      * note: oanon still locked.   anon is _not_ locked, but we have
   1116      * the sole references to in from amap which _is_ locked.   thus,
   1117      * no one can get at it until we are done with it.
   1118      */
   1119 
   1120   } else {
   1121 
   1122     uvmexp.flt_anon++;
   1123     oanon = anon;		/* old, locked anon is same as anon */
   1124     pg = anon->u.an_page;
   1125     if (anon->an_ref > 1)     /* never allow writes to ref > 1 anons */
   1126       enter_prot = enter_prot & ~VM_PROT_WRITE;
   1127 
   1128   }
   1129 
   1130   /* locked: maps(read), amap, anon */
   1131 
   1132   /*
   1133    * now map the page in ...
   1134    * XXX: old fault unlocks object before pmap_enter.  this seems
   1135    * suspect since some other thread could blast the page out from
   1136    * under us between the unlock and the pmap_enter.
   1137    */
   1138 
   1139   UVMHIST_LOG(maphist, "  MAPPING: anon: pm=0x%x, va=0x%x, pg=0x%x",
   1140     ufi.orig_map->pmap, ufi.orig_rvaddr, pg, 0);
   1141   pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
   1142 	     enter_prot, wired);
   1143 
   1144   /*
   1145    * ... and update the page queues.
   1146    */
   1147 
   1148   uvm_lock_pageq();
   1149 
   1150   if (fault_type == VM_FAULT_WIRE) {
   1151 
   1152       uvm_pagewire(pg, FALSE);
   1153 
   1154   } else {
   1155 
   1156     /* tmpwire page to remove from q's if we didn't just allocate it */
   1157     if (anon == oanon)
   1158       uvm_pagewire(pg, TRUE);
   1159 
   1160     /* activate it */
   1161     uvm_pageactivate(pg);
   1162 
   1163   }
   1164 
   1165   uvm_unlock_pageq();
   1166 
   1167   /*
   1168    * done case 1!  finish up by unlocking everything and returning success
   1169    */
   1170 
   1171   uvmfault_unlockall(&ufi, amap, uobj, oanon);
   1172   return(KERN_SUCCESS);
   1173 
   1174 
   1175 Case2:
   1176   /*
   1177    * handle case 2: faulting on backing object or zero fill
   1178    */
   1179 
   1180   /* locked: maps(read), amap(if there), uobj(if !null), uobjpage(if !null) */
   1181 
   1182   /*
   1183    * note that uobjpage can not be PGO_DONTCARE at this point.  we now
   1184    * set uobjpage to PGO_DONTCARE if we are doing a zero fill.  if we
   1185    * have a backing object, check and see if we are going to promote
   1186    * the data up to an anon during the fault.
   1187    */
   1188 
   1189   if (uobj == NULL) {
   1190     uobjpage = PGO_DONTCARE;
   1191     promote = TRUE;		/* always need anon here */
   1192   } else {
   1193     /* assert(uobjpage != PGO_DONTCARE) */
   1194     promote = (access_type & VM_PROT_WRITE) && UVM_ET_ISCOPYONWRITE(ufi.entry);
   1195   }
   1196   UVMHIST_LOG(maphist, "  case 2 fault: promote=%d, zfill=%d",
   1197 	promote, (uobj == NULL), 0,0);
   1198 
   1199   /*
   1200    * if uobjpage is null, then we need to unlock and ask the pager to
   1201    * get the data for us.   once we have the data, we need to reverify
   1202    * the state the world.   we are currently not holding any resources.
   1203    */
   1204 
   1205   if (uobjpage == NULL) {
   1206 
   1207     /* locked: maps(read), amap(if there), uobj */
   1208     uvmfault_unlockall(&ufi, amap, NULL, NULL);
   1209     /* locked: uobj */
   1210 
   1211     uvmexp.fltget++;
   1212     gotpages = 1;
   1213     result = uobj->pgops->pgo_get(uobj, (ufi.rvaddr - ufi.entry->start) +
   1214 				ufi.entry->offset, &uobjpage, &gotpages,
   1215 				0, UVM_ET_ISCOPYONWRITE(ufi.entry) ?
   1216 				VM_PROT_READ : access_type,
   1217 				ufi.entry->advice, 0);
   1218 
   1219     /* locked: uobjpage(if result OK) */
   1220 
   1221     /*
   1222      * recover from I/O
   1223      */
   1224 
   1225     if (result != VM_PAGER_OK) {
   1226 
   1227 #ifdef DIAGNOSTIC
   1228       if (result == VM_PAGER_PEND)
   1229 	panic("uvm_fault: pgo_get got PENDing on non-async I/O");
   1230 #endif
   1231 
   1232       if (result == VM_PAGER_AGAIN) {
   1233 	UVMHIST_LOG(maphist, "  pgo_get says TRY AGAIN!",0,0,0,0);
   1234         tsleep((caddr_t)&lbolt, PVM, "fltagain2", 0);
   1235 	goto ReFault;
   1236       }
   1237 
   1238       UVMHIST_LOG(maphist, "<- pgo_get failed (code %d)", result, 0,0,0);
   1239       return(KERN_PROTECTION_FAILURE); /* XXX i/o error */
   1240     }
   1241 
   1242     /* locked: uobjpage */
   1243 
   1244     /*
   1245      * re-verify the state of the world by first trying to relock the maps.
   1246      * always relock the object.
   1247      */
   1248 
   1249     locked = uvmfault_relock(&ufi);
   1250     if (locked)
   1251       simple_lock(&amap->am_l);
   1252     simple_lock(&uobj->vmobjlock);
   1253 
   1254     /* locked(locked): maps(read), amap(if !null), uobj, uobjpage */
   1255     /* locked(!locked): uobj, uobjpage */
   1256 
   1257     /*
   1258      * verify that the page has not be released and re-verify that amap
   1259      * slot is still free.   if there is a problem, we unlock and clean
   1260      * up.
   1261      */
   1262 
   1263     if ((uobjpage->flags & PG_RELEASED) != 0 ||
   1264 	(locked && amap &&
   1265 	 amap_lookup(&ufi.entry->aref, ufi.rvaddr - ufi.entry->start))) {
   1266 
   1267       if (locked)
   1268 	uvmfault_unlockall(&ufi, amap, NULL, NULL);
   1269       locked = FALSE;
   1270     }
   1271 
   1272     /*
   1273      * didn't get the lock?   release the page and retry.
   1274      */
   1275 
   1276     if (locked == FALSE) {
   1277 
   1278       UVMHIST_LOG(maphist, "  wasn't able to relock after fault: retry",
   1279 	0,0,0,0);
   1280       if (uobjpage->flags & PG_WANTED)
   1281 	thread_wakeup(uobjpage);	/* still holding object lock */
   1282 
   1283       if (uobjpage->flags & PG_RELEASED) {
   1284         uvmexp.fltpgrele++;
   1285 #ifdef DIAGNOSTIC
   1286 	if (uobj->pgops->pgo_releasepg == NULL)
   1287 	  panic("uvm_fault: object has no releasepg function");
   1288 #endif
   1289         if (uobj->pgops->pgo_releasepg(uobjpage,NULL))  /* frees page */
   1290           simple_unlock(&uobj->vmobjlock);  /* unlock if still alive */
   1291         goto ReFault;
   1292       }
   1293 
   1294       uvm_lock_pageq();
   1295       uvm_pageactivate(uobjpage);	/* make sure it is in queues */
   1296       uvm_unlock_pageq();
   1297       uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1298       UVM_PAGE_OWN(uobjpage, NULL);
   1299       simple_unlock(&uobj->vmobjlock);
   1300       goto ReFault;
   1301 
   1302     }
   1303 
   1304     /*
   1305      * we have the data in uobjpage which is PG_BUSY and !PG_RELEASED.
   1306      * we are holding object lock (so the page can't be released on us).
   1307      */
   1308 
   1309     /* locked: maps(read), amap(if !null), uobj, uobjpage */
   1310 
   1311   }
   1312 
   1313   /* locked: maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) */
   1314   /*
   1315    * notes:
   1316    *  - at this point uobjpage can not be NULL
   1317    *  - at this point uobjpage can not be PG_RELEASED (since we checked
   1318    *  for it above)
   1319    *  - at this point uobjpage could be PG_WANTED (handle later)
   1320    */
   1321 
   1322   if (promote == FALSE) {
   1323 
   1324     /*
   1325      * we are not promoting.   if the mapping is COW ensure that we
   1326      * don't give more access than we should (e.g. when doing a read
   1327      * fault on a COPYONWRITE mapping we want to map the COW page in
   1328      * R/O even though the entry protection could be R/W).
   1329      *
   1330      * set "pg" to the page we want to map in (uobjpage, usually)
   1331      */
   1332 
   1333     uvmexp.flt_obj++;
   1334     if (UVM_ET_ISCOPYONWRITE(ufi.entry))
   1335       enter_prot = enter_prot & ~VM_PROT_WRITE;
   1336     pg = uobjpage;		/* map in the actual object */
   1337 
   1338     /* assert(uobjpage != PGO_DONTCARE) */
   1339 
   1340     /*
   1341      * we are faulting directly on the page.   be careful about writing
   1342      * to loaned pages...
   1343      */
   1344     if (uobjpage->loan_count) {
   1345 
   1346       if ((access_type & VM_PROT_WRITE) == 0) {
   1347 	/* read fault: cap the protection at read-only */
   1348 	enter_prot = enter_prot & ~VM_PROT_WRITE;	/* cap! */
   1349 
   1350       } else {
   1351 	/* write fault: must break the loan here */
   1352 	pg = uvm_pagealloc(NULL, 0, NULL);	/* alloc new un-owned page */
   1353 	if (pg == NULL) {
   1354 	  /* drop ownership of page, it can't be released */
   1355 	  if (uobjpage->flags & PG_WANTED)
   1356 	    thread_wakeup(uobjpage);
   1357 	  uobjpage->flags &= ~(PG_BUSY|PG_WANTED);
   1358 	  UVM_PAGE_OWN(uobjpage, NULL);
   1359 
   1360 	  uvm_lock_pageq();
   1361 	  uvm_pageactivate(uobjpage); /* activate: we will need it later */
   1362 	  uvm_unlock_pageq();
   1363 	  uvmfault_unlockall(&ufi, amap, uobj, NULL);
   1364 	  UVMHIST_LOG(maphist, "  out of RAM breaking loan, waiting", 0,0,0,0);
   1365 	  uvmexp.fltnoram++;
   1366 	  uvm_wait("flt_noram4");
   1367 	  goto ReFault;
   1368 	}
   1369 
   1370 	/*
   1371 	 * copy the data from the old page to the new one and clear
   1372 	 * the fake/clean flags on the new page (keep it busy).
   1373 	 * force a reload of the old page by clearing it from all
   1374 	 * pmaps.
   1375 	 *
   1376 	 * then lock the page queues to rename the pages.
   1377 	 */
   1378 	uvm_pagecopy(uobjpage, pg);		/* old -> new */
   1379 	pg->flags &= ~(PG_FAKE|PG_CLEAN);
   1380 	pmap_page_protect(PMAP_PGARG(uobjpage), VM_PROT_NONE);
   1381 	if (uobjpage->flags & PG_WANTED)
   1382 	  thread_wakeup(uobjpage);
   1383 	uobjpage->flags &= ~(PG_WANTED|PG_BUSY);  /* uobj still locked */
   1384 	UVM_PAGE_OWN(uobjpage, NULL);
   1385 
   1386 	uvm_lock_pageq();
   1387 	offset = uobjpage->offset;
   1388 	uvm_pagerealloc(uobjpage, NULL, 0);	/* remove old page */
   1389 	/* at this point we have absolutely no control over uobjpage */
   1390 	uvm_pagerealloc(pg, uobj, offset);	/* install new page */
   1391 	uvm_unlock_pageq();
   1392 
   1393 	/*
   1394 	 * done!  loan is broken and "pg" is PG_BUSY.   it can now
   1395 	 * replace uobjpage.
   1396 	 */
   1397 
   1398 	uobjpage = pg;
   1399 
   1400       }		/* write fault case */
   1401     }		/* if loan_count */
   1402 
   1403   } else {
   1404 
   1405     /*
   1406      * if we are going to promote the data to an anon we allocate a blank
   1407      * anon here and plug it into our amap.
   1408      */
   1409 #if DIAGNOSTIC
   1410     if (amap == NULL)
   1411       panic("uvm_fault: want to promote data, but no anon");
   1412 #endif
   1413 
   1414     anon = uvm_analloc();
   1415     if (anon)
   1416       pg = uvm_pagealloc(NULL, 0, anon);	/* BUSY+CLEAN+FAKE */
   1417 #if defined(sparc)
   1418     else
   1419       pg = NULL; /* XXX: shutup unused var compiler warning on sparc */
   1420 #endif
   1421 
   1422     /*
   1423      * out of memory resources?
   1424      */
   1425     if (anon == NULL || pg == NULL) {
   1426 
   1427       /*
   1428        * arg!  must unbusy our page and fail or sleep.
   1429        */
   1430       if (uobjpage != PGO_DONTCARE) {
   1431 	if (uobjpage->flags & PG_WANTED)
   1432 	  thread_wakeup(uobjpage);	/* still holding object lock */
   1433 
   1434 	uvm_lock_pageq();
   1435 	uvm_pageactivate(uobjpage);	/* make sure it is in queues */
   1436 	uvm_unlock_pageq();
   1437 	uobjpage->flags &= ~(PG_BUSY|PG_WANTED); /* un-busy! (still locked) */
   1438         UVM_PAGE_OWN(uobjpage, NULL);
   1439       }
   1440 
   1441       /* unlock and fail ... */
   1442       uvmfault_unlockall(&ufi, amap, uobj, NULL);
   1443       if (anon == NULL) {
   1444         UVMHIST_LOG(maphist, "  promote: out of VM", 0,0,0,0);
   1445         uvmexp.fltnoanon++;
   1446 	return(KERN_RESOURCE_SHORTAGE);	/* XXX: out of VM */
   1447       }
   1448       UVMHIST_LOG(maphist, "  out of RAM, waiting for more",0,0,0,0);
   1449       uvm_anfree(anon);
   1450       uvmexp.fltnoram++;
   1451       uvm_wait("flt_noram5");
   1452       goto ReFault;
   1453     }
   1454 
   1455     /*
   1456      * fill in the data
   1457      */
   1458 
   1459     if (uobjpage != PGO_DONTCARE) {
   1460       uvmexp.flt_prcopy++;
   1461       uvm_pagecopy(uobjpage, pg);	/* copy page [pg now dirty] */
   1462 
   1463       /* promote to shared amap?  make sure all sharing procs see it */
   1464       if ((amap->am_flags & AMAP_SHARED) != 0) {
   1465         pmap_page_protect(PMAP_PGARG(uobjpage), VM_PROT_NONE);
   1466       }
   1467 
   1468       /*
   1469        * dispose of uobjpage.  it can't be PG_RELEASED since we still hold
   1470        * the object lock.   drop handle to uobj as well.
   1471        */
   1472 
   1473       if (uobjpage->flags & PG_WANTED)
   1474 	thread_wakeup(uobjpage);	/* still have the obj lock */
   1475       uobjpage->flags &= ~(PG_BUSY|PG_WANTED);		/* un-busy! */
   1476       UVM_PAGE_OWN(uobjpage, NULL);
   1477       uvm_lock_pageq();
   1478       uvm_pageactivate(uobjpage);	/* put it back */
   1479       uvm_unlock_pageq();
   1480       simple_unlock(&uobj->vmobjlock);
   1481       uobj = NULL;
   1482       UVMHIST_LOG(maphist,"  promote uobjpage 0x%x to anon/page 0x%x/0x%x",
   1483 	uobjpage, anon, pg, 0);
   1484 
   1485     } else {
   1486       uvmexp.flt_przero++;
   1487       uvm_pagezero(pg);			/* zero page [pg now dirty] */
   1488       UVMHIST_LOG(maphist,"  zero fill anon/page 0x%x/0%x", anon, pg, 0, 0);
   1489     }
   1490 
   1491     amap_add(&ufi.entry->aref, ufi.rvaddr - ufi.entry->start, anon, 0);
   1492 
   1493   }
   1494 
   1495   /* locked: maps(read), amap(if !null), uobj(if !null), uobjpage(if uobj) */
   1496   /* note: pg is either the uobjpage or the new page in the new anon */
   1497 
   1498   /*
   1499    * all resources are present.   we can now map it in and free our
   1500    * resources.
   1501    */
   1502 
   1503   UVMHIST_LOG(maphist,"  MAPPING: case2: pm=0x%x, va=0x%x, pg=0x%x, promote=%d",
   1504     ufi.orig_map->pmap, ufi.orig_rvaddr, pg, promote);
   1505   pmap_enter(ufi.orig_map->pmap, ufi.orig_rvaddr, VM_PAGE_TO_PHYS(pg),
   1506 	     enter_prot, wired);
   1507 
   1508   uvm_lock_pageq();
   1509 
   1510   if (fault_type == VM_FAULT_WIRE) {
   1511 
   1512       uvm_pagewire(pg, FALSE);
   1513 
   1514   } else {
   1515 
   1516     /* activate it */
   1517     if (pg == uobjpage)
   1518       uvm_pagewire(pg, TRUE);	/* remove from q's before activating */
   1519     uvm_pageactivate(pg);
   1520 
   1521   }
   1522 
   1523   uvm_unlock_pageq();
   1524 
   1525   if (pg->flags & PG_WANTED)
   1526     thread_wakeup(pg);		/* lock still held */
   1527 
   1528   /*
   1529    * note that pg can't be PG_RELEASED since we did not drop the object
   1530    * lock since the last time we checked.
   1531    */
   1532 
   1533   pg->flags &= ~(PG_BUSY|PG_FAKE|PG_WANTED);
   1534   UVM_PAGE_OWN(pg, NULL);
   1535   uvmfault_unlockall(&ufi, amap, uobj, NULL);
   1536 
   1537   UVMHIST_LOG(maphist, "<- done (SUCCESS!)",0,0,0,0);
   1538   return(KERN_SUCCESS);
   1539 }
   1540 
   1541 
   1542 /*
   1543  * uvm_fault_wire: wire down a range of virtual addresses in a map.
   1544  *
   1545  * => map should be locked by caller?   If so how can we call
   1546  *	uvm_fault?   WRONG.
   1547  * => XXXCDC: locking here is all screwed up!!!  start with
   1548  *	uvm_map_pageable and fix it.
   1549  */
   1550 
   1551 int uvm_fault_wire(map, start, end)
   1552 
   1553 vm_map_t map;
   1554 vm_offset_t start, end;
   1555 
   1556 {
   1557   vm_offset_t va;
   1558   pmap_t  pmap;
   1559   int rv;
   1560 
   1561   pmap = vm_map_pmap(map);
   1562 
   1563   /*
   1564    * call pmap pageable: this tells the pmap layer to lock down these
   1565    * page tables.
   1566    */
   1567 
   1568   pmap_pageable(pmap, start, end, FALSE);
   1569 
   1570   /*
   1571    * now fault it in page at a time.   if the fault fails then we have
   1572    * to undo what we have done.   note that in uvm_fault VM_PROT_NONE
   1573    * is replaced with the max protection if fault_type is VM_FAULT_WIRE.
   1574    */
   1575 
   1576   for (va = start ; va < end ; va += PAGE_SIZE) {
   1577     rv = uvm_fault(map, va, VM_FAULT_WIRE, VM_PROT_NONE);
   1578     if (rv) {
   1579       if (va != start) {
   1580 	uvm_fault_unwire(map->pmap, start, va);
   1581       }
   1582       return(rv);
   1583     }
   1584   }
   1585 
   1586   return(KERN_SUCCESS);
   1587 }
   1588 
   1589 /*
   1590  * uvm_fault_unwire(): unwire range of virtual space.
   1591  *
   1592  * => caller holds reference to pmap (via its map)
   1593  */
   1594 
   1595 void uvm_fault_unwire(pmap, start, end)
   1596 
   1597 struct pmap *pmap;
   1598 vm_offset_t start, end;
   1599 
   1600 {
   1601   vm_offset_t va, pa;
   1602   struct vm_page *pg;
   1603 
   1604   /*
   1605    * we assume that the area we are unwiring has actually been wired
   1606    * in the first place.   this means that we should be able to extract
   1607    * the PAs from the pmap.   we also lock out the page daemon so that
   1608    * we can call uvm_pageunwire.
   1609    */
   1610 
   1611   uvm_lock_pageq();
   1612 
   1613   for (va = start; va < end ; va += PAGE_SIZE) {
   1614     pa = pmap_extract(pmap, va);
   1615 
   1616     if (pa == (vm_offset_t) 0) { /* XXX: assumes PA 0 cannot be in map */
   1617       panic("uvm_fault_unwire: unwiring non-wired memory");
   1618     }
   1619     pmap_change_wiring(pmap, va, FALSE);  /* tell the pmap */
   1620     pg = PHYS_TO_VM_PAGE(pa);
   1621     if (pg)
   1622       uvm_pageunwire(pg);
   1623   }
   1624 
   1625   uvm_unlock_pageq();
   1626 
   1627   /*
   1628    * now we call pmap_pageable to let the pmap know that the page tables
   1629    * in this space no longer need to be wired.
   1630    */
   1631 
   1632   pmap_pageable(pmap, start, end, TRUE);
   1633 
   1634 }
   1635