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