xref: /freebsd/sys/vm/vm_fault.c (revision 0957b409)
1 /*-
2  * SPDX-License-Identifier: (BSD-4-Clause AND MIT-CMU)
3  *
4  * Copyright (c) 1991, 1993
5  *	The Regents of the University of California.  All rights reserved.
6  * Copyright (c) 1994 John S. Dyson
7  * All rights reserved.
8  * Copyright (c) 1994 David Greenman
9  * All rights reserved.
10  *
11  *
12  * This code is derived from software contributed to Berkeley by
13  * The Mach Operating System project at Carnegie-Mellon University.
14  *
15  * Redistribution and use in source and binary forms, with or without
16  * modification, are permitted provided that the following conditions
17  * are met:
18  * 1. Redistributions of source code must retain the above copyright
19  *    notice, this list of conditions and the following disclaimer.
20  * 2. Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the distribution.
23  * 3. All advertising materials mentioning features or use of this software
24  *    must display the following acknowledgement:
25  *	This product includes software developed by the University of
26  *	California, Berkeley and its contributors.
27  * 4. Neither the name of the University nor the names of its contributors
28  *    may be used to endorse or promote products derived from this software
29  *    without specific prior written permission.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41  * SUCH DAMAGE.
42  *
43  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
44  *
45  *
46  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47  * All rights reserved.
48  *
49  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
50  *
51  * Permission to use, copy, modify and distribute this software and
52  * its documentation is hereby granted, provided that both the copyright
53  * notice and this permission notice appear in all copies of the
54  * software, derivative works or modified versions, and any portions
55  * thereof, and that both notices appear in supporting documentation.
56  *
57  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
60  *
61  * Carnegie Mellon requests users of this software to return to
62  *
63  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
64  *  School of Computer Science
65  *  Carnegie Mellon University
66  *  Pittsburgh PA 15213-3890
67  *
68  * any improvements or extensions that they make and grant Carnegie the
69  * rights to redistribute these changes.
70  */
71 
72 /*
73  *	Page fault handling module.
74  */
75 
76 #include <sys/cdefs.h>
77 __FBSDID("$FreeBSD$");
78 
79 #include "opt_ktrace.h"
80 #include "opt_vm.h"
81 
82 #include <sys/param.h>
83 #include <sys/systm.h>
84 #include <sys/kernel.h>
85 #include <sys/lock.h>
86 #include <sys/mman.h>
87 #include <sys/proc.h>
88 #include <sys/racct.h>
89 #include <sys/resourcevar.h>
90 #include <sys/rwlock.h>
91 #include <sys/sysctl.h>
92 #include <sys/vmmeter.h>
93 #include <sys/vnode.h>
94 #ifdef KTRACE
95 #include <sys/ktrace.h>
96 #endif
97 
98 #include <vm/vm.h>
99 #include <vm/vm_param.h>
100 #include <vm/pmap.h>
101 #include <vm/vm_map.h>
102 #include <vm/vm_object.h>
103 #include <vm/vm_page.h>
104 #include <vm/vm_pageout.h>
105 #include <vm/vm_kern.h>
106 #include <vm/vm_pager.h>
107 #include <vm/vm_extern.h>
108 #include <vm/vm_reserv.h>
109 
110 #define PFBAK 4
111 #define PFFOR 4
112 
113 #define	VM_FAULT_READ_DEFAULT	(1 + VM_FAULT_READ_AHEAD_INIT)
114 #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
115 
116 #define	VM_FAULT_DONTNEED_MIN	1048576
117 
118 struct faultstate {
119 	vm_page_t m;
120 	vm_object_t object;
121 	vm_pindex_t pindex;
122 	vm_page_t first_m;
123 	vm_object_t	first_object;
124 	vm_pindex_t first_pindex;
125 	vm_map_t map;
126 	vm_map_entry_t entry;
127 	int map_generation;
128 	bool lookup_still_valid;
129 	struct vnode *vp;
130 };
131 
132 static void vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr,
133 	    int ahead);
134 static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
135 	    int backward, int forward, bool obj_locked);
136 
137 static inline void
138 release_page(struct faultstate *fs)
139 {
140 
141 	vm_page_xunbusy(fs->m);
142 	vm_page_lock(fs->m);
143 	vm_page_deactivate(fs->m);
144 	vm_page_unlock(fs->m);
145 	fs->m = NULL;
146 }
147 
148 static inline void
149 unlock_map(struct faultstate *fs)
150 {
151 
152 	if (fs->lookup_still_valid) {
153 		vm_map_lookup_done(fs->map, fs->entry);
154 		fs->lookup_still_valid = false;
155 	}
156 }
157 
158 static void
159 unlock_vp(struct faultstate *fs)
160 {
161 
162 	if (fs->vp != NULL) {
163 		vput(fs->vp);
164 		fs->vp = NULL;
165 	}
166 }
167 
168 static void
169 unlock_and_deallocate(struct faultstate *fs)
170 {
171 
172 	vm_object_pip_wakeup(fs->object);
173 	VM_OBJECT_WUNLOCK(fs->object);
174 	if (fs->object != fs->first_object) {
175 		VM_OBJECT_WLOCK(fs->first_object);
176 		vm_page_lock(fs->first_m);
177 		vm_page_free(fs->first_m);
178 		vm_page_unlock(fs->first_m);
179 		vm_object_pip_wakeup(fs->first_object);
180 		VM_OBJECT_WUNLOCK(fs->first_object);
181 		fs->first_m = NULL;
182 	}
183 	vm_object_deallocate(fs->first_object);
184 	unlock_map(fs);
185 	unlock_vp(fs);
186 }
187 
188 static void
189 vm_fault_dirty(vm_map_entry_t entry, vm_page_t m, vm_prot_t prot,
190     vm_prot_t fault_type, int fault_flags, bool set_wd)
191 {
192 	bool need_dirty;
193 
194 	if (((prot & VM_PROT_WRITE) == 0 &&
195 	    (fault_flags & VM_FAULT_DIRTY) == 0) ||
196 	    (m->oflags & VPO_UNMANAGED) != 0)
197 		return;
198 
199 	VM_OBJECT_ASSERT_LOCKED(m->object);
200 
201 	need_dirty = ((fault_type & VM_PROT_WRITE) != 0 &&
202 	    (fault_flags & VM_FAULT_WIRE) == 0) ||
203 	    (fault_flags & VM_FAULT_DIRTY) != 0;
204 
205 	if (set_wd)
206 		vm_object_set_writeable_dirty(m->object);
207 	else
208 		/*
209 		 * If two callers of vm_fault_dirty() with set_wd ==
210 		 * FALSE, one for the map entry with MAP_ENTRY_NOSYNC
211 		 * flag set, other with flag clear, race, it is
212 		 * possible for the no-NOSYNC thread to see m->dirty
213 		 * != 0 and not clear VPO_NOSYNC.  Take vm_page lock
214 		 * around manipulation of VPO_NOSYNC and
215 		 * vm_page_dirty() call, to avoid the race and keep
216 		 * m->oflags consistent.
217 		 */
218 		vm_page_lock(m);
219 
220 	/*
221 	 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
222 	 * if the page is already dirty to prevent data written with
223 	 * the expectation of being synced from not being synced.
224 	 * Likewise if this entry does not request NOSYNC then make
225 	 * sure the page isn't marked NOSYNC.  Applications sharing
226 	 * data should use the same flags to avoid ping ponging.
227 	 */
228 	if ((entry->eflags & MAP_ENTRY_NOSYNC) != 0) {
229 		if (m->dirty == 0) {
230 			m->oflags |= VPO_NOSYNC;
231 		}
232 	} else {
233 		m->oflags &= ~VPO_NOSYNC;
234 	}
235 
236 	/*
237 	 * If the fault is a write, we know that this page is being
238 	 * written NOW so dirty it explicitly to save on
239 	 * pmap_is_modified() calls later.
240 	 *
241 	 * Also, since the page is now dirty, we can possibly tell
242 	 * the pager to release any swap backing the page.  Calling
243 	 * the pager requires a write lock on the object.
244 	 */
245 	if (need_dirty)
246 		vm_page_dirty(m);
247 	if (!set_wd)
248 		vm_page_unlock(m);
249 	else if (need_dirty)
250 		vm_pager_page_unswapped(m);
251 }
252 
253 static void
254 vm_fault_fill_hold(vm_page_t *m_hold, vm_page_t m)
255 {
256 
257 	if (m_hold != NULL) {
258 		*m_hold = m;
259 		vm_page_lock(m);
260 		vm_page_hold(m);
261 		vm_page_unlock(m);
262 	}
263 }
264 
265 /*
266  * Unlocks fs.first_object and fs.map on success.
267  */
268 static int
269 vm_fault_soft_fast(struct faultstate *fs, vm_offset_t vaddr, vm_prot_t prot,
270     int fault_type, int fault_flags, boolean_t wired, vm_page_t *m_hold)
271 {
272 	vm_page_t m, m_map;
273 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
274     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
275     VM_NRESERVLEVEL > 0
276 	vm_page_t m_super;
277 	int flags;
278 #endif
279 	int psind, rv;
280 
281 	MPASS(fs->vp == NULL);
282 	m = vm_page_lookup(fs->first_object, fs->first_pindex);
283 	/* A busy page can be mapped for read|execute access. */
284 	if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
285 	    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
286 		return (KERN_FAILURE);
287 	m_map = m;
288 	psind = 0;
289 #if (defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
290     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)) && \
291     VM_NRESERVLEVEL > 0
292 	if ((m->flags & PG_FICTITIOUS) == 0 &&
293 	    (m_super = vm_reserv_to_superpage(m)) != NULL &&
294 	    rounddown2(vaddr, pagesizes[m_super->psind]) >= fs->entry->start &&
295 	    roundup2(vaddr + 1, pagesizes[m_super->psind]) <= fs->entry->end &&
296 	    (vaddr & (pagesizes[m_super->psind] - 1)) == (VM_PAGE_TO_PHYS(m) &
297 	    (pagesizes[m_super->psind] - 1)) &&
298 	    pmap_ps_enabled(fs->map->pmap)) {
299 		flags = PS_ALL_VALID;
300 		if ((prot & VM_PROT_WRITE) != 0) {
301 			/*
302 			 * Create a superpage mapping allowing write access
303 			 * only if none of the constituent pages are busy and
304 			 * all of them are already dirty (except possibly for
305 			 * the page that was faulted on).
306 			 */
307 			flags |= PS_NONE_BUSY;
308 			if ((fs->first_object->flags & OBJ_UNMANAGED) == 0)
309 				flags |= PS_ALL_DIRTY;
310 		}
311 		if (vm_page_ps_test(m_super, flags, m)) {
312 			m_map = m_super;
313 			psind = m_super->psind;
314 			vaddr = rounddown2(vaddr, pagesizes[psind]);
315 			/* Preset the modified bit for dirty superpages. */
316 			if ((flags & PS_ALL_DIRTY) != 0)
317 				fault_type |= VM_PROT_WRITE;
318 		}
319 	}
320 #endif
321 	rv = pmap_enter(fs->map->pmap, vaddr, m_map, prot, fault_type |
322 	    PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED : 0), psind);
323 	if (rv != KERN_SUCCESS)
324 		return (rv);
325 	vm_fault_fill_hold(m_hold, m);
326 	vm_fault_dirty(fs->entry, m, prot, fault_type, fault_flags, false);
327 	if (psind == 0 && !wired)
328 		vm_fault_prefault(fs, vaddr, PFBAK, PFFOR, true);
329 	VM_OBJECT_RUNLOCK(fs->first_object);
330 	vm_map_lookup_done(fs->map, fs->entry);
331 	curthread->td_ru.ru_minflt++;
332 	return (KERN_SUCCESS);
333 }
334 
335 static void
336 vm_fault_restore_map_lock(struct faultstate *fs)
337 {
338 
339 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
340 	MPASS(fs->first_object->paging_in_progress > 0);
341 
342 	if (!vm_map_trylock_read(fs->map)) {
343 		VM_OBJECT_WUNLOCK(fs->first_object);
344 		vm_map_lock_read(fs->map);
345 		VM_OBJECT_WLOCK(fs->first_object);
346 	}
347 	fs->lookup_still_valid = true;
348 }
349 
350 static void
351 vm_fault_populate_check_page(vm_page_t m)
352 {
353 
354 	/*
355 	 * Check each page to ensure that the pager is obeying the
356 	 * interface: the page must be installed in the object, fully
357 	 * valid, and exclusively busied.
358 	 */
359 	MPASS(m != NULL);
360 	MPASS(m->valid == VM_PAGE_BITS_ALL);
361 	MPASS(vm_page_xbusied(m));
362 }
363 
364 static void
365 vm_fault_populate_cleanup(vm_object_t object, vm_pindex_t first,
366     vm_pindex_t last)
367 {
368 	vm_page_t m;
369 	vm_pindex_t pidx;
370 
371 	VM_OBJECT_ASSERT_WLOCKED(object);
372 	MPASS(first <= last);
373 	for (pidx = first, m = vm_page_lookup(object, pidx);
374 	    pidx <= last; pidx++, m = vm_page_next(m)) {
375 		vm_fault_populate_check_page(m);
376 		vm_page_lock(m);
377 		vm_page_deactivate(m);
378 		vm_page_unlock(m);
379 		vm_page_xunbusy(m);
380 	}
381 }
382 
383 static int
384 vm_fault_populate(struct faultstate *fs, vm_prot_t prot, int fault_type,
385     int fault_flags, boolean_t wired, vm_page_t *m_hold)
386 {
387 	struct mtx *m_mtx;
388 	vm_offset_t vaddr;
389 	vm_page_t m;
390 	vm_pindex_t map_first, map_last, pager_first, pager_last, pidx;
391 	int i, npages, psind, rv;
392 
393 	MPASS(fs->object == fs->first_object);
394 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
395 	MPASS(fs->first_object->paging_in_progress > 0);
396 	MPASS(fs->first_object->backing_object == NULL);
397 	MPASS(fs->lookup_still_valid);
398 
399 	pager_first = OFF_TO_IDX(fs->entry->offset);
400 	pager_last = pager_first + atop(fs->entry->end - fs->entry->start) - 1;
401 	unlock_map(fs);
402 	unlock_vp(fs);
403 
404 	/*
405 	 * Call the pager (driver) populate() method.
406 	 *
407 	 * There is no guarantee that the method will be called again
408 	 * if the current fault is for read, and a future fault is
409 	 * for write.  Report the entry's maximum allowed protection
410 	 * to the driver.
411 	 */
412 	rv = vm_pager_populate(fs->first_object, fs->first_pindex,
413 	    fault_type, fs->entry->max_protection, &pager_first, &pager_last);
414 
415 	VM_OBJECT_ASSERT_WLOCKED(fs->first_object);
416 	if (rv == VM_PAGER_BAD) {
417 		/*
418 		 * VM_PAGER_BAD is the backdoor for a pager to request
419 		 * normal fault handling.
420 		 */
421 		vm_fault_restore_map_lock(fs);
422 		if (fs->map->timestamp != fs->map_generation)
423 			return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
424 		return (KERN_NOT_RECEIVER);
425 	}
426 	if (rv != VM_PAGER_OK)
427 		return (KERN_FAILURE); /* AKA SIGSEGV */
428 
429 	/* Ensure that the driver is obeying the interface. */
430 	MPASS(pager_first <= pager_last);
431 	MPASS(fs->first_pindex <= pager_last);
432 	MPASS(fs->first_pindex >= pager_first);
433 	MPASS(pager_last < fs->first_object->size);
434 
435 	vm_fault_restore_map_lock(fs);
436 	if (fs->map->timestamp != fs->map_generation) {
437 		vm_fault_populate_cleanup(fs->first_object, pager_first,
438 		    pager_last);
439 		return (KERN_RESOURCE_SHORTAGE); /* RetryFault */
440 	}
441 
442 	/*
443 	 * The map is unchanged after our last unlock.  Process the fault.
444 	 *
445 	 * The range [pager_first, pager_last] that is given to the
446 	 * pager is only a hint.  The pager may populate any range
447 	 * within the object that includes the requested page index.
448 	 * In case the pager expanded the range, clip it to fit into
449 	 * the map entry.
450 	 */
451 	map_first = OFF_TO_IDX(fs->entry->offset);
452 	if (map_first > pager_first) {
453 		vm_fault_populate_cleanup(fs->first_object, pager_first,
454 		    map_first - 1);
455 		pager_first = map_first;
456 	}
457 	map_last = map_first + atop(fs->entry->end - fs->entry->start) - 1;
458 	if (map_last < pager_last) {
459 		vm_fault_populate_cleanup(fs->first_object, map_last + 1,
460 		    pager_last);
461 		pager_last = map_last;
462 	}
463 	for (pidx = pager_first, m = vm_page_lookup(fs->first_object, pidx);
464 	    pidx <= pager_last;
465 	    pidx += npages, m = vm_page_next(&m[npages - 1])) {
466 		vaddr = fs->entry->start + IDX_TO_OFF(pidx) - fs->entry->offset;
467 #if defined(__aarch64__) || defined(__amd64__) || (defined(__arm__) && \
468     __ARM_ARCH >= 6) || defined(__i386__) || defined(__riscv)
469 		psind = m->psind;
470 		if (psind > 0 && ((vaddr & (pagesizes[psind] - 1)) != 0 ||
471 		    pidx + OFF_TO_IDX(pagesizes[psind]) - 1 > pager_last ||
472 		    !pmap_ps_enabled(fs->map->pmap)))
473 			psind = 0;
474 #else
475 		psind = 0;
476 #endif
477 		npages = atop(pagesizes[psind]);
478 		for (i = 0; i < npages; i++) {
479 			vm_fault_populate_check_page(&m[i]);
480 			vm_fault_dirty(fs->entry, &m[i], prot, fault_type,
481 			    fault_flags, true);
482 		}
483 		VM_OBJECT_WUNLOCK(fs->first_object);
484 		rv = pmap_enter(fs->map->pmap, vaddr, m, prot, fault_type |
485 		    (wired ? PMAP_ENTER_WIRED : 0), psind);
486 #if defined(__amd64__)
487 		if (psind > 0 && rv == KERN_FAILURE) {
488 			for (i = 0; i < npages; i++) {
489 				rv = pmap_enter(fs->map->pmap, vaddr + ptoa(i),
490 				    &m[i], prot, fault_type |
491 				    (wired ? PMAP_ENTER_WIRED : 0), 0);
492 				MPASS(rv == KERN_SUCCESS);
493 			}
494 		}
495 #else
496 		MPASS(rv == KERN_SUCCESS);
497 #endif
498 		VM_OBJECT_WLOCK(fs->first_object);
499 		m_mtx = NULL;
500 		for (i = 0; i < npages; i++) {
501 			vm_page_change_lock(&m[i], &m_mtx);
502 			if ((fault_flags & VM_FAULT_WIRE) != 0)
503 				vm_page_wire(&m[i]);
504 			else
505 				vm_page_activate(&m[i]);
506 			if (m_hold != NULL && m[i].pindex == fs->first_pindex) {
507 				*m_hold = &m[i];
508 				vm_page_hold(&m[i]);
509 			}
510 			vm_page_xunbusy_maybelocked(&m[i]);
511 		}
512 		if (m_mtx != NULL)
513 			mtx_unlock(m_mtx);
514 	}
515 	curthread->td_ru.ru_majflt++;
516 	return (KERN_SUCCESS);
517 }
518 
519 /*
520  *	vm_fault:
521  *
522  *	Handle a page fault occurring at the given address,
523  *	requiring the given permissions, in the map specified.
524  *	If successful, the page is inserted into the
525  *	associated physical map.
526  *
527  *	NOTE: the given address should be truncated to the
528  *	proper page address.
529  *
530  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
531  *	a standard error specifying why the fault is fatal is returned.
532  *
533  *	The map in question must be referenced, and remains so.
534  *	Caller may hold no locks.
535  */
536 int
537 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
538     int fault_flags)
539 {
540 	struct thread *td;
541 	int result;
542 
543 	td = curthread;
544 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
545 		return (KERN_PROTECTION_FAILURE);
546 #ifdef KTRACE
547 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
548 		ktrfault(vaddr, fault_type);
549 #endif
550 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
551 	    NULL);
552 #ifdef KTRACE
553 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
554 		ktrfaultend(result);
555 #endif
556 	return (result);
557 }
558 
559 int
560 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
561     int fault_flags, vm_page_t *m_hold)
562 {
563 	struct faultstate fs;
564 	struct vnode *vp;
565 	struct domainset *dset;
566 	vm_object_t next_object, retry_object;
567 	vm_offset_t e_end, e_start;
568 	vm_pindex_t retry_pindex;
569 	vm_prot_t prot, retry_prot;
570 	int ahead, alloc_req, behind, cluster_offset, error, era, faultcount;
571 	int locked, nera, result, rv;
572 	u_char behavior;
573 	boolean_t wired;	/* Passed by reference. */
574 	bool dead, hardfault, is_first_object_locked;
575 
576 	VM_CNT_INC(v_vm_faults);
577 	fs.vp = NULL;
578 	faultcount = 0;
579 	nera = -1;
580 	hardfault = false;
581 
582 RetryFault:;
583 
584 	/*
585 	 * Find the backing store object and offset into it to begin the
586 	 * search.
587 	 */
588 	fs.map = map;
589 	result = vm_map_lookup(&fs.map, vaddr, fault_type |
590 	    VM_PROT_FAULT_LOOKUP, &fs.entry, &fs.first_object,
591 	    &fs.first_pindex, &prot, &wired);
592 	if (result != KERN_SUCCESS) {
593 		unlock_vp(&fs);
594 		return (result);
595 	}
596 
597 	fs.map_generation = fs.map->timestamp;
598 
599 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
600 		panic("%s: fault on nofault entry, addr: %#lx",
601 		    __func__, (u_long)vaddr);
602 	}
603 
604 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
605 	    fs.entry->wiring_thread != curthread) {
606 		vm_map_unlock_read(fs.map);
607 		vm_map_lock(fs.map);
608 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
609 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
610 			unlock_vp(&fs);
611 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
612 			vm_map_unlock_and_wait(fs.map, 0);
613 		} else
614 			vm_map_unlock(fs.map);
615 		goto RetryFault;
616 	}
617 
618 	MPASS((fs.entry->eflags & MAP_ENTRY_GUARD) == 0);
619 
620 	if (wired)
621 		fault_type = prot | (fault_type & VM_PROT_COPY);
622 	else
623 		KASSERT((fault_flags & VM_FAULT_WIRE) == 0,
624 		    ("!wired && VM_FAULT_WIRE"));
625 
626 	/*
627 	 * Try to avoid lock contention on the top-level object through
628 	 * special-case handling of some types of page faults, specifically,
629 	 * those that are both (1) mapping an existing page from the top-
630 	 * level object and (2) not having to mark that object as containing
631 	 * dirty pages.  Under these conditions, a read lock on the top-level
632 	 * object suffices, allowing multiple page faults of a similar type to
633 	 * run in parallel on the same top-level object.
634 	 */
635 	if (fs.vp == NULL /* avoid locked vnode leak */ &&
636 	    (fault_flags & (VM_FAULT_WIRE | VM_FAULT_DIRTY)) == 0 &&
637 	    /* avoid calling vm_object_set_writeable_dirty() */
638 	    ((prot & VM_PROT_WRITE) == 0 ||
639 	    (fs.first_object->type != OBJT_VNODE &&
640 	    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
641 	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
642 		VM_OBJECT_RLOCK(fs.first_object);
643 		if ((prot & VM_PROT_WRITE) == 0 ||
644 		    (fs.first_object->type != OBJT_VNODE &&
645 		    (fs.first_object->flags & OBJ_TMPFS_NODE) == 0) ||
646 		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0) {
647 			rv = vm_fault_soft_fast(&fs, vaddr, prot, fault_type,
648 			    fault_flags, wired, m_hold);
649 			if (rv == KERN_SUCCESS)
650 				return (rv);
651 		}
652 		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
653 			VM_OBJECT_RUNLOCK(fs.first_object);
654 			VM_OBJECT_WLOCK(fs.first_object);
655 		}
656 	} else {
657 		VM_OBJECT_WLOCK(fs.first_object);
658 	}
659 
660 	/*
661 	 * Make a reference to this object to prevent its disposal while we
662 	 * are messing with it.  Once we have the reference, the map is free
663 	 * to be diddled.  Since objects reference their shadows (and copies),
664 	 * they will stay around as well.
665 	 *
666 	 * Bump the paging-in-progress count to prevent size changes (e.g.
667 	 * truncation operations) during I/O.
668 	 */
669 	vm_object_reference_locked(fs.first_object);
670 	vm_object_pip_add(fs.first_object, 1);
671 
672 	fs.lookup_still_valid = true;
673 
674 	fs.first_m = NULL;
675 
676 	/*
677 	 * Search for the page at object/offset.
678 	 */
679 	fs.object = fs.first_object;
680 	fs.pindex = fs.first_pindex;
681 	while (TRUE) {
682 		/*
683 		 * If the object is marked for imminent termination,
684 		 * we retry here, since the collapse pass has raced
685 		 * with us.  Otherwise, if we see terminally dead
686 		 * object, return fail.
687 		 */
688 		if ((fs.object->flags & OBJ_DEAD) != 0) {
689 			dead = fs.object->type == OBJT_DEAD;
690 			unlock_and_deallocate(&fs);
691 			if (dead)
692 				return (KERN_PROTECTION_FAILURE);
693 			pause("vmf_de", 1);
694 			goto RetryFault;
695 		}
696 
697 		/*
698 		 * See if page is resident
699 		 */
700 		fs.m = vm_page_lookup(fs.object, fs.pindex);
701 		if (fs.m != NULL) {
702 			/*
703 			 * Wait/Retry if the page is busy.  We have to do this
704 			 * if the page is either exclusive or shared busy
705 			 * because the vm_pager may be using read busy for
706 			 * pageouts (and even pageins if it is the vnode
707 			 * pager), and we could end up trying to pagein and
708 			 * pageout the same page simultaneously.
709 			 *
710 			 * We can theoretically allow the busy case on a read
711 			 * fault if the page is marked valid, but since such
712 			 * pages are typically already pmap'd, putting that
713 			 * special case in might be more effort then it is
714 			 * worth.  We cannot under any circumstances mess
715 			 * around with a shared busied page except, perhaps,
716 			 * to pmap it.
717 			 */
718 			if (vm_page_busied(fs.m)) {
719 				/*
720 				 * Reference the page before unlocking and
721 				 * sleeping so that the page daemon is less
722 				 * likely to reclaim it.
723 				 */
724 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
725 				if (fs.object != fs.first_object) {
726 					if (!VM_OBJECT_TRYWLOCK(
727 					    fs.first_object)) {
728 						VM_OBJECT_WUNLOCK(fs.object);
729 						VM_OBJECT_WLOCK(fs.first_object);
730 						VM_OBJECT_WLOCK(fs.object);
731 					}
732 					vm_page_lock(fs.first_m);
733 					vm_page_free(fs.first_m);
734 					vm_page_unlock(fs.first_m);
735 					vm_object_pip_wakeup(fs.first_object);
736 					VM_OBJECT_WUNLOCK(fs.first_object);
737 					fs.first_m = NULL;
738 				}
739 				unlock_map(&fs);
740 				if (fs.m == vm_page_lookup(fs.object,
741 				    fs.pindex)) {
742 					vm_page_sleep_if_busy(fs.m, "vmpfw");
743 				}
744 				vm_object_pip_wakeup(fs.object);
745 				VM_OBJECT_WUNLOCK(fs.object);
746 				VM_CNT_INC(v_intrans);
747 				vm_object_deallocate(fs.first_object);
748 				goto RetryFault;
749 			}
750 
751 			/*
752 			 * Mark page busy for other processes, and the
753 			 * pagedaemon.  If it still isn't completely valid
754 			 * (readable), jump to readrest, else break-out ( we
755 			 * found the page ).
756 			 */
757 			vm_page_xbusy(fs.m);
758 			if (fs.m->valid != VM_PAGE_BITS_ALL)
759 				goto readrest;
760 			break; /* break to PAGE HAS BEEN FOUND */
761 		}
762 		KASSERT(fs.m == NULL, ("fs.m should be NULL, not %p", fs.m));
763 
764 		/*
765 		 * Page is not resident.  If the pager might contain the page
766 		 * or this is the beginning of the search, allocate a new
767 		 * page.  (Default objects are zero-fill, so there is no real
768 		 * pager for them.)
769 		 */
770 		if (fs.object->type != OBJT_DEFAULT ||
771 		    fs.object == fs.first_object) {
772 			if (fs.pindex >= fs.object->size) {
773 				unlock_and_deallocate(&fs);
774 				return (KERN_PROTECTION_FAILURE);
775 			}
776 
777 			if (fs.object == fs.first_object &&
778 			    (fs.first_object->flags & OBJ_POPULATE) != 0 &&
779 			    fs.first_object->shadow_count == 0) {
780 				rv = vm_fault_populate(&fs, prot, fault_type,
781 				    fault_flags, wired, m_hold);
782 				switch (rv) {
783 				case KERN_SUCCESS:
784 				case KERN_FAILURE:
785 					unlock_and_deallocate(&fs);
786 					return (rv);
787 				case KERN_RESOURCE_SHORTAGE:
788 					unlock_and_deallocate(&fs);
789 					goto RetryFault;
790 				case KERN_NOT_RECEIVER:
791 					/*
792 					 * Pager's populate() method
793 					 * returned VM_PAGER_BAD.
794 					 */
795 					break;
796 				default:
797 					panic("inconsistent return codes");
798 				}
799 			}
800 
801 			/*
802 			 * Allocate a new page for this object/offset pair.
803 			 *
804 			 * Unlocked read of the p_flag is harmless. At
805 			 * worst, the P_KILLED might be not observed
806 			 * there, and allocation can fail, causing
807 			 * restart and new reading of the p_flag.
808 			 */
809 			dset = fs.object->domain.dr_policy;
810 			if (dset == NULL)
811 				dset = curthread->td_domain.dr_policy;
812 			if (!vm_page_count_severe_set(&dset->ds_mask) ||
813 			    P_KILLED(curproc)) {
814 #if VM_NRESERVLEVEL > 0
815 				vm_object_color(fs.object, atop(vaddr) -
816 				    fs.pindex);
817 #endif
818 				alloc_req = P_KILLED(curproc) ?
819 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
820 				if (fs.object->type != OBJT_VNODE &&
821 				    fs.object->backing_object == NULL)
822 					alloc_req |= VM_ALLOC_ZERO;
823 				fs.m = vm_page_alloc(fs.object, fs.pindex,
824 				    alloc_req);
825 			}
826 			if (fs.m == NULL) {
827 				unlock_and_deallocate(&fs);
828 				vm_waitpfault(dset);
829 				goto RetryFault;
830 			}
831 		}
832 
833 readrest:
834 		/*
835 		 * At this point, we have either allocated a new page or found
836 		 * an existing page that is only partially valid.
837 		 *
838 		 * We hold a reference on the current object and the page is
839 		 * exclusive busied.
840 		 */
841 
842 		/*
843 		 * If the pager for the current object might have the page,
844 		 * then determine the number of additional pages to read and
845 		 * potentially reprioritize previously read pages for earlier
846 		 * reclamation.  These operations should only be performed
847 		 * once per page fault.  Even if the current pager doesn't
848 		 * have the page, the number of additional pages to read will
849 		 * apply to subsequent objects in the shadow chain.
850 		 */
851 		if (fs.object->type != OBJT_DEFAULT && nera == -1 &&
852 		    !P_KILLED(curproc)) {
853 			KASSERT(fs.lookup_still_valid, ("map unlocked"));
854 			era = fs.entry->read_ahead;
855 			behavior = vm_map_entry_behavior(fs.entry);
856 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
857 				nera = 0;
858 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
859 				nera = VM_FAULT_READ_AHEAD_MAX;
860 				if (vaddr == fs.entry->next_read)
861 					vm_fault_dontneed(&fs, vaddr, nera);
862 			} else if (vaddr == fs.entry->next_read) {
863 				/*
864 				 * This is a sequential fault.  Arithmetically
865 				 * increase the requested number of pages in
866 				 * the read-ahead window.  The requested
867 				 * number of pages is "# of sequential faults
868 				 * x (read ahead min + 1) + read ahead min"
869 				 */
870 				nera = VM_FAULT_READ_AHEAD_MIN;
871 				if (era > 0) {
872 					nera += era + 1;
873 					if (nera > VM_FAULT_READ_AHEAD_MAX)
874 						nera = VM_FAULT_READ_AHEAD_MAX;
875 				}
876 				if (era == VM_FAULT_READ_AHEAD_MAX)
877 					vm_fault_dontneed(&fs, vaddr, nera);
878 			} else {
879 				/*
880 				 * This is a non-sequential fault.
881 				 */
882 				nera = 0;
883 			}
884 			if (era != nera) {
885 				/*
886 				 * A read lock on the map suffices to update
887 				 * the read ahead count safely.
888 				 */
889 				fs.entry->read_ahead = nera;
890 			}
891 
892 			/*
893 			 * Prepare for unlocking the map.  Save the map
894 			 * entry's start and end addresses, which are used to
895 			 * optimize the size of the pager operation below.
896 			 * Even if the map entry's addresses change after
897 			 * unlocking the map, using the saved addresses is
898 			 * safe.
899 			 */
900 			e_start = fs.entry->start;
901 			e_end = fs.entry->end;
902 		}
903 
904 		/*
905 		 * Call the pager to retrieve the page if there is a chance
906 		 * that the pager has it, and potentially retrieve additional
907 		 * pages at the same time.
908 		 */
909 		if (fs.object->type != OBJT_DEFAULT) {
910 			/*
911 			 * Release the map lock before locking the vnode or
912 			 * sleeping in the pager.  (If the current object has
913 			 * a shadow, then an earlier iteration of this loop
914 			 * may have already unlocked the map.)
915 			 */
916 			unlock_map(&fs);
917 
918 			if (fs.object->type == OBJT_VNODE &&
919 			    (vp = fs.object->handle) != fs.vp) {
920 				/*
921 				 * Perform an unlock in case the desired vnode
922 				 * changed while the map was unlocked during a
923 				 * retry.
924 				 */
925 				unlock_vp(&fs);
926 
927 				locked = VOP_ISLOCKED(vp);
928 				if (locked != LK_EXCLUSIVE)
929 					locked = LK_SHARED;
930 
931 				/*
932 				 * We must not sleep acquiring the vnode lock
933 				 * while we have the page exclusive busied or
934 				 * the object's paging-in-progress count
935 				 * incremented.  Otherwise, we could deadlock.
936 				 */
937 				error = vget(vp, locked | LK_CANRECURSE |
938 				    LK_NOWAIT, curthread);
939 				if (error != 0) {
940 					vhold(vp);
941 					release_page(&fs);
942 					unlock_and_deallocate(&fs);
943 					error = vget(vp, locked | LK_RETRY |
944 					    LK_CANRECURSE, curthread);
945 					vdrop(vp);
946 					fs.vp = vp;
947 					KASSERT(error == 0,
948 					    ("vm_fault: vget failed"));
949 					goto RetryFault;
950 				}
951 				fs.vp = vp;
952 			}
953 			KASSERT(fs.vp == NULL || !fs.map->system_map,
954 			    ("vm_fault: vnode-backed object mapped by system map"));
955 
956 			/*
957 			 * Page in the requested page and hint the pager,
958 			 * that it may bring up surrounding pages.
959 			 */
960 			if (nera == -1 || behavior == MAP_ENTRY_BEHAV_RANDOM ||
961 			    P_KILLED(curproc)) {
962 				behind = 0;
963 				ahead = 0;
964 			} else {
965 				/* Is this a sequential fault? */
966 				if (nera > 0) {
967 					behind = 0;
968 					ahead = nera;
969 				} else {
970 					/*
971 					 * Request a cluster of pages that is
972 					 * aligned to a VM_FAULT_READ_DEFAULT
973 					 * page offset boundary within the
974 					 * object.  Alignment to a page offset
975 					 * boundary is more likely to coincide
976 					 * with the underlying file system
977 					 * block than alignment to a virtual
978 					 * address boundary.
979 					 */
980 					cluster_offset = fs.pindex %
981 					    VM_FAULT_READ_DEFAULT;
982 					behind = ulmin(cluster_offset,
983 					    atop(vaddr - e_start));
984 					ahead = VM_FAULT_READ_DEFAULT - 1 -
985 					    cluster_offset;
986 				}
987 				ahead = ulmin(ahead, atop(e_end - vaddr) - 1);
988 			}
989 			rv = vm_pager_get_pages(fs.object, &fs.m, 1,
990 			    &behind, &ahead);
991 			if (rv == VM_PAGER_OK) {
992 				faultcount = behind + 1 + ahead;
993 				hardfault = true;
994 				break; /* break to PAGE HAS BEEN FOUND */
995 			}
996 			if (rv == VM_PAGER_ERROR)
997 				printf("vm_fault: pager read error, pid %d (%s)\n",
998 				    curproc->p_pid, curproc->p_comm);
999 
1000 			/*
1001 			 * If an I/O error occurred or the requested page was
1002 			 * outside the range of the pager, clean up and return
1003 			 * an error.
1004 			 */
1005 			if (rv == VM_PAGER_ERROR || rv == VM_PAGER_BAD) {
1006 				vm_page_lock(fs.m);
1007 				if (fs.m->wire_count == 0)
1008 					vm_page_free(fs.m);
1009 				else
1010 					vm_page_xunbusy_maybelocked(fs.m);
1011 				vm_page_unlock(fs.m);
1012 				fs.m = NULL;
1013 				unlock_and_deallocate(&fs);
1014 				return (rv == VM_PAGER_ERROR ? KERN_FAILURE :
1015 				    KERN_PROTECTION_FAILURE);
1016 			}
1017 
1018 			/*
1019 			 * The requested page does not exist at this object/
1020 			 * offset.  Remove the invalid page from the object,
1021 			 * waking up anyone waiting for it, and continue on to
1022 			 * the next object.  However, if this is the top-level
1023 			 * object, we must leave the busy page in place to
1024 			 * prevent another process from rushing past us, and
1025 			 * inserting the page in that object at the same time
1026 			 * that we are.
1027 			 */
1028 			if (fs.object != fs.first_object) {
1029 				vm_page_lock(fs.m);
1030 				if (fs.m->wire_count == 0)
1031 					vm_page_free(fs.m);
1032 				else
1033 					vm_page_xunbusy_maybelocked(fs.m);
1034 				vm_page_unlock(fs.m);
1035 				fs.m = NULL;
1036 			}
1037 		}
1038 
1039 		/*
1040 		 * We get here if the object has default pager (or unwiring)
1041 		 * or the pager doesn't have the page.
1042 		 */
1043 		if (fs.object == fs.first_object)
1044 			fs.first_m = fs.m;
1045 
1046 		/*
1047 		 * Move on to the next object.  Lock the next object before
1048 		 * unlocking the current one.
1049 		 */
1050 		next_object = fs.object->backing_object;
1051 		if (next_object == NULL) {
1052 			/*
1053 			 * If there's no object left, fill the page in the top
1054 			 * object with zeros.
1055 			 */
1056 			if (fs.object != fs.first_object) {
1057 				vm_object_pip_wakeup(fs.object);
1058 				VM_OBJECT_WUNLOCK(fs.object);
1059 
1060 				fs.object = fs.first_object;
1061 				fs.pindex = fs.first_pindex;
1062 				fs.m = fs.first_m;
1063 				VM_OBJECT_WLOCK(fs.object);
1064 			}
1065 			fs.first_m = NULL;
1066 
1067 			/*
1068 			 * Zero the page if necessary and mark it valid.
1069 			 */
1070 			if ((fs.m->flags & PG_ZERO) == 0) {
1071 				pmap_zero_page(fs.m);
1072 			} else {
1073 				VM_CNT_INC(v_ozfod);
1074 			}
1075 			VM_CNT_INC(v_zfod);
1076 			fs.m->valid = VM_PAGE_BITS_ALL;
1077 			/* Don't try to prefault neighboring pages. */
1078 			faultcount = 1;
1079 			break;	/* break to PAGE HAS BEEN FOUND */
1080 		} else {
1081 			KASSERT(fs.object != next_object,
1082 			    ("object loop %p", next_object));
1083 			VM_OBJECT_WLOCK(next_object);
1084 			vm_object_pip_add(next_object, 1);
1085 			if (fs.object != fs.first_object)
1086 				vm_object_pip_wakeup(fs.object);
1087 			fs.pindex +=
1088 			    OFF_TO_IDX(fs.object->backing_object_offset);
1089 			VM_OBJECT_WUNLOCK(fs.object);
1090 			fs.object = next_object;
1091 		}
1092 	}
1093 
1094 	vm_page_assert_xbusied(fs.m);
1095 
1096 	/*
1097 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1098 	 * is held.]
1099 	 */
1100 
1101 	/*
1102 	 * If the page is being written, but isn't already owned by the
1103 	 * top-level object, we have to copy it into a new page owned by the
1104 	 * top-level object.
1105 	 */
1106 	if (fs.object != fs.first_object) {
1107 		/*
1108 		 * We only really need to copy if we want to write it.
1109 		 */
1110 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1111 			/*
1112 			 * This allows pages to be virtually copied from a
1113 			 * backing_object into the first_object, where the
1114 			 * backing object has no other refs to it, and cannot
1115 			 * gain any more refs.  Instead of a bcopy, we just
1116 			 * move the page from the backing object to the
1117 			 * first object.  Note that we must mark the page
1118 			 * dirty in the first object so that it will go out
1119 			 * to swap when needed.
1120 			 */
1121 			is_first_object_locked = false;
1122 			if (
1123 				/*
1124 				 * Only one shadow object
1125 				 */
1126 				(fs.object->shadow_count == 1) &&
1127 				/*
1128 				 * No COW refs, except us
1129 				 */
1130 				(fs.object->ref_count == 1) &&
1131 				/*
1132 				 * No one else can look this object up
1133 				 */
1134 				(fs.object->handle == NULL) &&
1135 				/*
1136 				 * No other ways to look the object up
1137 				 */
1138 				((fs.object->type == OBJT_DEFAULT) ||
1139 				 (fs.object->type == OBJT_SWAP)) &&
1140 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
1141 				/*
1142 				 * We don't chase down the shadow chain
1143 				 */
1144 			    fs.object == fs.first_object->backing_object) {
1145 				vm_page_lock(fs.m);
1146 				vm_page_dequeue(fs.m);
1147 				vm_page_remove(fs.m);
1148 				vm_page_unlock(fs.m);
1149 				vm_page_lock(fs.first_m);
1150 				vm_page_replace_checked(fs.m, fs.first_object,
1151 				    fs.first_pindex, fs.first_m);
1152 				vm_page_free(fs.first_m);
1153 				vm_page_unlock(fs.first_m);
1154 				vm_page_dirty(fs.m);
1155 #if VM_NRESERVLEVEL > 0
1156 				/*
1157 				 * Rename the reservation.
1158 				 */
1159 				vm_reserv_rename(fs.m, fs.first_object,
1160 				    fs.object, OFF_TO_IDX(
1161 				    fs.first_object->backing_object_offset));
1162 #endif
1163 				/*
1164 				 * Removing the page from the backing object
1165 				 * unbusied it.
1166 				 */
1167 				vm_page_xbusy(fs.m);
1168 				fs.first_m = fs.m;
1169 				fs.m = NULL;
1170 				VM_CNT_INC(v_cow_optim);
1171 			} else {
1172 				/*
1173 				 * Oh, well, lets copy it.
1174 				 */
1175 				pmap_copy_page(fs.m, fs.first_m);
1176 				fs.first_m->valid = VM_PAGE_BITS_ALL;
1177 				if (wired && (fault_flags &
1178 				    VM_FAULT_WIRE) == 0) {
1179 					vm_page_lock(fs.first_m);
1180 					vm_page_wire(fs.first_m);
1181 					vm_page_unlock(fs.first_m);
1182 
1183 					vm_page_lock(fs.m);
1184 					vm_page_unwire(fs.m, PQ_INACTIVE);
1185 					vm_page_unlock(fs.m);
1186 				}
1187 				/*
1188 				 * We no longer need the old page or object.
1189 				 */
1190 				release_page(&fs);
1191 			}
1192 			/*
1193 			 * fs.object != fs.first_object due to above
1194 			 * conditional
1195 			 */
1196 			vm_object_pip_wakeup(fs.object);
1197 			VM_OBJECT_WUNLOCK(fs.object);
1198 
1199 			/*
1200 			 * We only try to prefault read-only mappings to the
1201 			 * neighboring pages when this copy-on-write fault is
1202 			 * a hard fault.  In other cases, trying to prefault
1203 			 * is typically wasted effort.
1204 			 */
1205 			if (faultcount == 0)
1206 				faultcount = 1;
1207 
1208 			/*
1209 			 * Only use the new page below...
1210 			 */
1211 			fs.object = fs.first_object;
1212 			fs.pindex = fs.first_pindex;
1213 			fs.m = fs.first_m;
1214 			if (!is_first_object_locked)
1215 				VM_OBJECT_WLOCK(fs.object);
1216 			VM_CNT_INC(v_cow_faults);
1217 			curthread->td_cow++;
1218 		} else {
1219 			prot &= ~VM_PROT_WRITE;
1220 		}
1221 	}
1222 
1223 	/*
1224 	 * We must verify that the maps have not changed since our last
1225 	 * lookup.
1226 	 */
1227 	if (!fs.lookup_still_valid) {
1228 		if (!vm_map_trylock_read(fs.map)) {
1229 			release_page(&fs);
1230 			unlock_and_deallocate(&fs);
1231 			goto RetryFault;
1232 		}
1233 		fs.lookup_still_valid = true;
1234 		if (fs.map->timestamp != fs.map_generation) {
1235 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
1236 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
1237 
1238 			/*
1239 			 * If we don't need the page any longer, put it on the inactive
1240 			 * list (the easiest thing to do here).  If no one needs it,
1241 			 * pageout will grab it eventually.
1242 			 */
1243 			if (result != KERN_SUCCESS) {
1244 				release_page(&fs);
1245 				unlock_and_deallocate(&fs);
1246 
1247 				/*
1248 				 * If retry of map lookup would have blocked then
1249 				 * retry fault from start.
1250 				 */
1251 				if (result == KERN_FAILURE)
1252 					goto RetryFault;
1253 				return (result);
1254 			}
1255 			if ((retry_object != fs.first_object) ||
1256 			    (retry_pindex != fs.first_pindex)) {
1257 				release_page(&fs);
1258 				unlock_and_deallocate(&fs);
1259 				goto RetryFault;
1260 			}
1261 
1262 			/*
1263 			 * Check whether the protection has changed or the object has
1264 			 * been copied while we left the map unlocked. Changing from
1265 			 * read to write permission is OK - we leave the page
1266 			 * write-protected, and catch the write fault. Changing from
1267 			 * write to read permission means that we can't mark the page
1268 			 * write-enabled after all.
1269 			 */
1270 			prot &= retry_prot;
1271 			fault_type &= retry_prot;
1272 			if (prot == 0) {
1273 				release_page(&fs);
1274 				unlock_and_deallocate(&fs);
1275 				goto RetryFault;
1276 			}
1277 
1278 			/* Reassert because wired may have changed. */
1279 			KASSERT(wired || (fault_flags & VM_FAULT_WIRE) == 0,
1280 			    ("!wired && VM_FAULT_WIRE"));
1281 		}
1282 	}
1283 
1284 	/*
1285 	 * If the page was filled by a pager, save the virtual address that
1286 	 * should be faulted on next under a sequential access pattern to the
1287 	 * map entry.  A read lock on the map suffices to update this address
1288 	 * safely.
1289 	 */
1290 	if (hardfault)
1291 		fs.entry->next_read = vaddr + ptoa(ahead) + PAGE_SIZE;
1292 
1293 	vm_fault_dirty(fs.entry, fs.m, prot, fault_type, fault_flags, true);
1294 	vm_page_assert_xbusied(fs.m);
1295 
1296 	/*
1297 	 * Page must be completely valid or it is not fit to
1298 	 * map into user space.  vm_pager_get_pages() ensures this.
1299 	 */
1300 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
1301 	    ("vm_fault: page %p partially invalid", fs.m));
1302 	VM_OBJECT_WUNLOCK(fs.object);
1303 
1304 	/*
1305 	 * Put this page into the physical map.  We had to do the unlock above
1306 	 * because pmap_enter() may sleep.  We don't put the page
1307 	 * back on the active queue until later so that the pageout daemon
1308 	 * won't find it (yet).
1309 	 */
1310 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
1311 	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
1312 	if (faultcount != 1 && (fault_flags & VM_FAULT_WIRE) == 0 &&
1313 	    wired == 0)
1314 		vm_fault_prefault(&fs, vaddr,
1315 		    faultcount > 0 ? behind : PFBAK,
1316 		    faultcount > 0 ? ahead : PFFOR, false);
1317 	VM_OBJECT_WLOCK(fs.object);
1318 	vm_page_lock(fs.m);
1319 
1320 	/*
1321 	 * If the page is not wired down, then put it where the pageout daemon
1322 	 * can find it.
1323 	 */
1324 	if ((fault_flags & VM_FAULT_WIRE) != 0)
1325 		vm_page_wire(fs.m);
1326 	else
1327 		vm_page_activate(fs.m);
1328 	if (m_hold != NULL) {
1329 		*m_hold = fs.m;
1330 		vm_page_hold(fs.m);
1331 	}
1332 	vm_page_unlock(fs.m);
1333 	vm_page_xunbusy(fs.m);
1334 
1335 	/*
1336 	 * Unlock everything, and return
1337 	 */
1338 	unlock_and_deallocate(&fs);
1339 	if (hardfault) {
1340 		VM_CNT_INC(v_io_faults);
1341 		curthread->td_ru.ru_majflt++;
1342 #ifdef RACCT
1343 		if (racct_enable && fs.object->type == OBJT_VNODE) {
1344 			PROC_LOCK(curproc);
1345 			if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
1346 				racct_add_force(curproc, RACCT_WRITEBPS,
1347 				    PAGE_SIZE + behind * PAGE_SIZE);
1348 				racct_add_force(curproc, RACCT_WRITEIOPS, 1);
1349 			} else {
1350 				racct_add_force(curproc, RACCT_READBPS,
1351 				    PAGE_SIZE + ahead * PAGE_SIZE);
1352 				racct_add_force(curproc, RACCT_READIOPS, 1);
1353 			}
1354 			PROC_UNLOCK(curproc);
1355 		}
1356 #endif
1357 	} else
1358 		curthread->td_ru.ru_minflt++;
1359 
1360 	return (KERN_SUCCESS);
1361 }
1362 
1363 /*
1364  * Speed up the reclamation of pages that precede the faulting pindex within
1365  * the first object of the shadow chain.  Essentially, perform the equivalent
1366  * to madvise(..., MADV_DONTNEED) on a large cluster of pages that precedes
1367  * the faulting pindex by the cluster size when the pages read by vm_fault()
1368  * cross a cluster-size boundary.  The cluster size is the greater of the
1369  * smallest superpage size and VM_FAULT_DONTNEED_MIN.
1370  *
1371  * When "fs->first_object" is a shadow object, the pages in the backing object
1372  * that precede the faulting pindex are deactivated by vm_fault().  So, this
1373  * function must only be concerned with pages in the first object.
1374  */
1375 static void
1376 vm_fault_dontneed(const struct faultstate *fs, vm_offset_t vaddr, int ahead)
1377 {
1378 	vm_map_entry_t entry;
1379 	vm_object_t first_object, object;
1380 	vm_offset_t end, start;
1381 	vm_page_t m, m_next;
1382 	vm_pindex_t pend, pstart;
1383 	vm_size_t size;
1384 
1385 	object = fs->object;
1386 	VM_OBJECT_ASSERT_WLOCKED(object);
1387 	first_object = fs->first_object;
1388 	if (first_object != object) {
1389 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
1390 			VM_OBJECT_WUNLOCK(object);
1391 			VM_OBJECT_WLOCK(first_object);
1392 			VM_OBJECT_WLOCK(object);
1393 		}
1394 	}
1395 	/* Neither fictitious nor unmanaged pages can be reclaimed. */
1396 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1397 		size = VM_FAULT_DONTNEED_MIN;
1398 		if (MAXPAGESIZES > 1 && size < pagesizes[1])
1399 			size = pagesizes[1];
1400 		end = rounddown2(vaddr, size);
1401 		if (vaddr - end >= size - PAGE_SIZE - ptoa(ahead) &&
1402 		    (entry = fs->entry)->start < end) {
1403 			if (end - entry->start < size)
1404 				start = entry->start;
1405 			else
1406 				start = end - size;
1407 			pmap_advise(fs->map->pmap, start, end, MADV_DONTNEED);
1408 			pstart = OFF_TO_IDX(entry->offset) + atop(start -
1409 			    entry->start);
1410 			m_next = vm_page_find_least(first_object, pstart);
1411 			pend = OFF_TO_IDX(entry->offset) + atop(end -
1412 			    entry->start);
1413 			while ((m = m_next) != NULL && m->pindex < pend) {
1414 				m_next = TAILQ_NEXT(m, listq);
1415 				if (m->valid != VM_PAGE_BITS_ALL ||
1416 				    vm_page_busied(m))
1417 					continue;
1418 
1419 				/*
1420 				 * Don't clear PGA_REFERENCED, since it would
1421 				 * likely represent a reference by a different
1422 				 * process.
1423 				 *
1424 				 * Typically, at this point, prefetched pages
1425 				 * are still in the inactive queue.  Only
1426 				 * pages that triggered page faults are in the
1427 				 * active queue.
1428 				 */
1429 				vm_page_lock(m);
1430 				if (!vm_page_inactive(m))
1431 					vm_page_deactivate(m);
1432 				vm_page_unlock(m);
1433 			}
1434 		}
1435 	}
1436 	if (first_object != object)
1437 		VM_OBJECT_WUNLOCK(first_object);
1438 }
1439 
1440 /*
1441  * vm_fault_prefault provides a quick way of clustering
1442  * pagefaults into a processes address space.  It is a "cousin"
1443  * of vm_map_pmap_enter, except it runs at page fault time instead
1444  * of mmap time.
1445  */
1446 static void
1447 vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1448     int backward, int forward, bool obj_locked)
1449 {
1450 	pmap_t pmap;
1451 	vm_map_entry_t entry;
1452 	vm_object_t backing_object, lobject;
1453 	vm_offset_t addr, starta;
1454 	vm_pindex_t pindex;
1455 	vm_page_t m;
1456 	int i;
1457 
1458 	pmap = fs->map->pmap;
1459 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1460 		return;
1461 
1462 	entry = fs->entry;
1463 
1464 	if (addra < backward * PAGE_SIZE) {
1465 		starta = entry->start;
1466 	} else {
1467 		starta = addra - backward * PAGE_SIZE;
1468 		if (starta < entry->start)
1469 			starta = entry->start;
1470 	}
1471 
1472 	/*
1473 	 * Generate the sequence of virtual addresses that are candidates for
1474 	 * prefaulting in an outward spiral from the faulting virtual address,
1475 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
1476 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1477 	 * If the candidate address doesn't have a backing physical page, then
1478 	 * the loop immediately terminates.
1479 	 */
1480 	for (i = 0; i < 2 * imax(backward, forward); i++) {
1481 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1482 		    PAGE_SIZE);
1483 		if (addr > addra + forward * PAGE_SIZE)
1484 			addr = 0;
1485 
1486 		if (addr < starta || addr >= entry->end)
1487 			continue;
1488 
1489 		if (!pmap_is_prefaultable(pmap, addr))
1490 			continue;
1491 
1492 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1493 		lobject = entry->object.vm_object;
1494 		if (!obj_locked)
1495 			VM_OBJECT_RLOCK(lobject);
1496 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1497 		    lobject->type == OBJT_DEFAULT &&
1498 		    (backing_object = lobject->backing_object) != NULL) {
1499 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1500 			    0, ("vm_fault_prefault: unaligned object offset"));
1501 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1502 			VM_OBJECT_RLOCK(backing_object);
1503 			if (!obj_locked || lobject != entry->object.vm_object)
1504 				VM_OBJECT_RUNLOCK(lobject);
1505 			lobject = backing_object;
1506 		}
1507 		if (m == NULL) {
1508 			if (!obj_locked || lobject != entry->object.vm_object)
1509 				VM_OBJECT_RUNLOCK(lobject);
1510 			break;
1511 		}
1512 		if (m->valid == VM_PAGE_BITS_ALL &&
1513 		    (m->flags & PG_FICTITIOUS) == 0)
1514 			pmap_enter_quick(pmap, addr, m, entry->protection);
1515 		if (!obj_locked || lobject != entry->object.vm_object)
1516 			VM_OBJECT_RUNLOCK(lobject);
1517 	}
1518 }
1519 
1520 /*
1521  * Hold each of the physical pages that are mapped by the specified range of
1522  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1523  * and allow the specified types of access, "prot".  If all of the implied
1524  * pages are successfully held, then the number of held pages is returned
1525  * together with pointers to those pages in the array "ma".  However, if any
1526  * of the pages cannot be held, -1 is returned.
1527  */
1528 int
1529 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1530     vm_prot_t prot, vm_page_t *ma, int max_count)
1531 {
1532 	vm_offset_t end, va;
1533 	vm_page_t *mp;
1534 	int count;
1535 	boolean_t pmap_failed;
1536 
1537 	if (len == 0)
1538 		return (0);
1539 	end = round_page(addr + len);
1540 	addr = trunc_page(addr);
1541 
1542 	/*
1543 	 * Check for illegal addresses.
1544 	 */
1545 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1546 		return (-1);
1547 
1548 	if (atop(end - addr) > max_count)
1549 		panic("vm_fault_quick_hold_pages: count > max_count");
1550 	count = atop(end - addr);
1551 
1552 	/*
1553 	 * Most likely, the physical pages are resident in the pmap, so it is
1554 	 * faster to try pmap_extract_and_hold() first.
1555 	 */
1556 	pmap_failed = FALSE;
1557 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1558 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
1559 		if (*mp == NULL)
1560 			pmap_failed = TRUE;
1561 		else if ((prot & VM_PROT_WRITE) != 0 &&
1562 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
1563 			/*
1564 			 * Explicitly dirty the physical page.  Otherwise, the
1565 			 * caller's changes may go unnoticed because they are
1566 			 * performed through an unmanaged mapping or by a DMA
1567 			 * operation.
1568 			 *
1569 			 * The object lock is not held here.
1570 			 * See vm_page_clear_dirty_mask().
1571 			 */
1572 			vm_page_dirty(*mp);
1573 		}
1574 	}
1575 	if (pmap_failed) {
1576 		/*
1577 		 * One or more pages could not be held by the pmap.  Either no
1578 		 * page was mapped at the specified virtual address or that
1579 		 * mapping had insufficient permissions.  Attempt to fault in
1580 		 * and hold these pages.
1581 		 *
1582 		 * If vm_fault_disable_pagefaults() was called,
1583 		 * i.e., TDP_NOFAULTING is set, we must not sleep nor
1584 		 * acquire MD VM locks, which means we must not call
1585 		 * vm_fault_hold().  Some (out of tree) callers mark
1586 		 * too wide a code area with vm_fault_disable_pagefaults()
1587 		 * already, use the VM_PROT_QUICK_NOFAULT flag to request
1588 		 * the proper behaviour explicitly.
1589 		 */
1590 		if ((prot & VM_PROT_QUICK_NOFAULT) != 0 &&
1591 		    (curthread->td_pflags & TDP_NOFAULTING) != 0)
1592 			goto error;
1593 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1594 			if (*mp == NULL && vm_fault_hold(map, va, prot,
1595 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1596 				goto error;
1597 	}
1598 	return (count);
1599 error:
1600 	for (mp = ma; mp < ma + count; mp++)
1601 		if (*mp != NULL) {
1602 			vm_page_lock(*mp);
1603 			vm_page_unhold(*mp);
1604 			vm_page_unlock(*mp);
1605 		}
1606 	return (-1);
1607 }
1608 
1609 /*
1610  *	Routine:
1611  *		vm_fault_copy_entry
1612  *	Function:
1613  *		Create new shadow object backing dst_entry with private copy of
1614  *		all underlying pages. When src_entry is equal to dst_entry,
1615  *		function implements COW for wired-down map entry. Otherwise,
1616  *		it forks wired entry into dst_map.
1617  *
1618  *	In/out conditions:
1619  *		The source and destination maps must be locked for write.
1620  *		The source map entry must be wired down (or be a sharing map
1621  *		entry corresponding to a main map entry that is wired down).
1622  */
1623 void
1624 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1625     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1626     vm_ooffset_t *fork_charge)
1627 {
1628 	vm_object_t backing_object, dst_object, object, src_object;
1629 	vm_pindex_t dst_pindex, pindex, src_pindex;
1630 	vm_prot_t access, prot;
1631 	vm_offset_t vaddr;
1632 	vm_page_t dst_m;
1633 	vm_page_t src_m;
1634 	boolean_t upgrade;
1635 
1636 #ifdef	lint
1637 	src_map++;
1638 #endif	/* lint */
1639 
1640 	upgrade = src_entry == dst_entry;
1641 	access = prot = dst_entry->protection;
1642 
1643 	src_object = src_entry->object.vm_object;
1644 	src_pindex = OFF_TO_IDX(src_entry->offset);
1645 
1646 	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1647 		dst_object = src_object;
1648 		vm_object_reference(dst_object);
1649 	} else {
1650 		/*
1651 		 * Create the top-level object for the destination entry. (Doesn't
1652 		 * actually shadow anything - we copy the pages directly.)
1653 		 */
1654 		dst_object = vm_object_allocate(OBJT_DEFAULT,
1655 		    atop(dst_entry->end - dst_entry->start));
1656 #if VM_NRESERVLEVEL > 0
1657 		dst_object->flags |= OBJ_COLORED;
1658 		dst_object->pg_color = atop(dst_entry->start);
1659 #endif
1660 		dst_object->domain = src_object->domain;
1661 		dst_object->charge = dst_entry->end - dst_entry->start;
1662 	}
1663 
1664 	VM_OBJECT_WLOCK(dst_object);
1665 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1666 	    ("vm_fault_copy_entry: vm_object not NULL"));
1667 	if (src_object != dst_object) {
1668 		dst_entry->object.vm_object = dst_object;
1669 		dst_entry->offset = 0;
1670 	}
1671 	if (fork_charge != NULL) {
1672 		KASSERT(dst_entry->cred == NULL,
1673 		    ("vm_fault_copy_entry: leaked swp charge"));
1674 		dst_object->cred = curthread->td_ucred;
1675 		crhold(dst_object->cred);
1676 		*fork_charge += dst_object->charge;
1677 	} else if ((dst_object->type == OBJT_DEFAULT ||
1678 	    dst_object->type == OBJT_SWAP) &&
1679 	    dst_object->cred == NULL) {
1680 		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1681 		    dst_entry));
1682 		dst_object->cred = dst_entry->cred;
1683 		dst_entry->cred = NULL;
1684 	}
1685 
1686 	/*
1687 	 * If not an upgrade, then enter the mappings in the pmap as
1688 	 * read and/or execute accesses.  Otherwise, enter them as
1689 	 * write accesses.
1690 	 *
1691 	 * A writeable large page mapping is only created if all of
1692 	 * the constituent small page mappings are modified. Marking
1693 	 * PTEs as modified on inception allows promotion to happen
1694 	 * without taking potentially large number of soft faults.
1695 	 */
1696 	if (!upgrade)
1697 		access &= ~VM_PROT_WRITE;
1698 
1699 	/*
1700 	 * Loop through all of the virtual pages within the entry's
1701 	 * range, copying each page from the source object to the
1702 	 * destination object.  Since the source is wired, those pages
1703 	 * must exist.  In contrast, the destination is pageable.
1704 	 * Since the destination object doesn't share any backing storage
1705 	 * with the source object, all of its pages must be dirtied,
1706 	 * regardless of whether they can be written.
1707 	 */
1708 	for (vaddr = dst_entry->start, dst_pindex = 0;
1709 	    vaddr < dst_entry->end;
1710 	    vaddr += PAGE_SIZE, dst_pindex++) {
1711 again:
1712 		/*
1713 		 * Find the page in the source object, and copy it in.
1714 		 * Because the source is wired down, the page will be
1715 		 * in memory.
1716 		 */
1717 		if (src_object != dst_object)
1718 			VM_OBJECT_RLOCK(src_object);
1719 		object = src_object;
1720 		pindex = src_pindex + dst_pindex;
1721 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1722 		    (backing_object = object->backing_object) != NULL) {
1723 			/*
1724 			 * Unless the source mapping is read-only or
1725 			 * it is presently being upgraded from
1726 			 * read-only, the first object in the shadow
1727 			 * chain should provide all of the pages.  In
1728 			 * other words, this loop body should never be
1729 			 * executed when the source mapping is already
1730 			 * read/write.
1731 			 */
1732 			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1733 			    upgrade,
1734 			    ("vm_fault_copy_entry: main object missing page"));
1735 
1736 			VM_OBJECT_RLOCK(backing_object);
1737 			pindex += OFF_TO_IDX(object->backing_object_offset);
1738 			if (object != dst_object)
1739 				VM_OBJECT_RUNLOCK(object);
1740 			object = backing_object;
1741 		}
1742 		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1743 
1744 		if (object != dst_object) {
1745 			/*
1746 			 * Allocate a page in the destination object.
1747 			 */
1748 			dst_m = vm_page_alloc(dst_object, (src_object ==
1749 			    dst_object ? src_pindex : 0) + dst_pindex,
1750 			    VM_ALLOC_NORMAL);
1751 			if (dst_m == NULL) {
1752 				VM_OBJECT_WUNLOCK(dst_object);
1753 				VM_OBJECT_RUNLOCK(object);
1754 				vm_wait(dst_object);
1755 				VM_OBJECT_WLOCK(dst_object);
1756 				goto again;
1757 			}
1758 			pmap_copy_page(src_m, dst_m);
1759 			VM_OBJECT_RUNLOCK(object);
1760 			dst_m->valid = VM_PAGE_BITS_ALL;
1761 			dst_m->dirty = VM_PAGE_BITS_ALL;
1762 		} else {
1763 			dst_m = src_m;
1764 			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1765 				goto again;
1766 			if (dst_m->pindex >= dst_object->size)
1767 				/*
1768 				 * We are upgrading.  Index can occur
1769 				 * out of bounds if the object type is
1770 				 * vnode and the file was truncated.
1771 				 */
1772 				break;
1773 			vm_page_xbusy(dst_m);
1774 			KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1775 			    ("invalid dst page %p", dst_m));
1776 		}
1777 		VM_OBJECT_WUNLOCK(dst_object);
1778 
1779 		/*
1780 		 * Enter it in the pmap. If a wired, copy-on-write
1781 		 * mapping is being replaced by a write-enabled
1782 		 * mapping, then wire that new mapping.
1783 		 */
1784 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1785 		    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1786 
1787 		/*
1788 		 * Mark it no longer busy, and put it on the active list.
1789 		 */
1790 		VM_OBJECT_WLOCK(dst_object);
1791 
1792 		if (upgrade) {
1793 			if (src_m != dst_m) {
1794 				vm_page_lock(src_m);
1795 				vm_page_unwire(src_m, PQ_INACTIVE);
1796 				vm_page_unlock(src_m);
1797 				vm_page_lock(dst_m);
1798 				vm_page_wire(dst_m);
1799 				vm_page_unlock(dst_m);
1800 			} else {
1801 				KASSERT(dst_m->wire_count > 0,
1802 				    ("dst_m %p is not wired", dst_m));
1803 			}
1804 		} else {
1805 			vm_page_lock(dst_m);
1806 			vm_page_activate(dst_m);
1807 			vm_page_unlock(dst_m);
1808 		}
1809 		vm_page_xunbusy(dst_m);
1810 	}
1811 	VM_OBJECT_WUNLOCK(dst_object);
1812 	if (upgrade) {
1813 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1814 		vm_object_deallocate(src_object);
1815 	}
1816 }
1817 
1818 /*
1819  * Block entry into the machine-independent layer's page fault handler by
1820  * the calling thread.  Subsequent calls to vm_fault() by that thread will
1821  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
1822  * spurious page faults.
1823  */
1824 int
1825 vm_fault_disable_pagefaults(void)
1826 {
1827 
1828 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1829 }
1830 
1831 void
1832 vm_fault_enable_pagefaults(int save)
1833 {
1834 
1835 	curthread_pflags_restore(save);
1836 }
1837