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