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