vm_fault.c revision 270630
1238104Sdes/*-
2238104Sdes * Copyright (c) 1991, 1993
3238104Sdes *	The Regents of the University of California.  All rights reserved.
4238104Sdes * Copyright (c) 1994 John S. Dyson
5269257Sdes * All rights reserved.
6269257Sdes * Copyright (c) 1994 David Greenman
7269257Sdes * All rights reserved.
8269257Sdes *
9269257Sdes *
10269257Sdes * This code is derived from software contributed to Berkeley by
11238104Sdes * The Mach Operating System project at Carnegie-Mellon University.
12238104Sdes *
13238104Sdes * Redistribution and use in source and binary forms, with or without
14238104Sdes * modification, are permitted provided that the following conditions
15238104Sdes * are met:
16238104Sdes * 1. Redistributions of source code must retain the above copyright
17238104Sdes *    notice, this list of conditions and the following disclaimer.
18238104Sdes * 2. Redistributions in binary form must reproduce the above copyright
19238104Sdes *    notice, this list of conditions and the following disclaimer in the
20238104Sdes *    documentation and/or other materials provided with the distribution.
21238104Sdes * 3. All advertising materials mentioning features or use of this software
22238104Sdes *    must display the following acknowledgement:
23238104Sdes *	This product includes software developed by the University of
24238104Sdes *	California, Berkeley and its contributors.
25269257Sdes * 4. Neither the name of the University nor the names of its contributors
26238104Sdes *    may be used to endorse or promote products derived from this software
27238104Sdes *    without specific prior written permission.
28238104Sdes *
29238104Sdes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30238104Sdes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31238104Sdes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32238104Sdes * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33238104Sdes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34238104Sdes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35238104Sdes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36238104Sdes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37238104Sdes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38238104Sdes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39238104Sdes * SUCH DAMAGE.
40238104Sdes *
41238104Sdes *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42238104Sdes *
43238104Sdes *
44238104Sdes * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45238104Sdes * All rights reserved.
46238104Sdes *
47238104Sdes * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48238104Sdes *
49238104Sdes * Permission to use, copy, modify and distribute this software and
50238104Sdes * its documentation is hereby granted, provided that both the copyright
51238104Sdes * notice and this permission notice appear in all copies of the
52238104Sdes * software, derivative works or modified versions, and any portions
53238104Sdes * thereof, and that both notices appear in supporting documentation.
54238104Sdes *
55238104Sdes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56238104Sdes * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57238104Sdes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58238104Sdes *
59238104Sdes * Carnegie Mellon requests users of this software to return to
60238104Sdes *
61238104Sdes *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62238104Sdes *  School of Computer Science
63238104Sdes *  Carnegie Mellon University
64238104Sdes *  Pittsburgh PA 15213-3890
65238104Sdes *
66238104Sdes * any improvements or extensions that they make and grant Carnegie the
67238104Sdes * rights to redistribute these changes.
68238104Sdes */
69238104Sdes
70238104Sdes/*
71238104Sdes *	Page fault handling module.
72238104Sdes */
73238104Sdes
74238104Sdes#include <sys/cdefs.h>
75238104Sdes__FBSDID("$FreeBSD: stable/10/sys/vm/vm_fault.c 270630 2014-08-25 21:21:29Z kib $");
76238104Sdes
77238104Sdes#include "opt_ktrace.h"
78238104Sdes#include "opt_vm.h"
79238104Sdes
80238104Sdes#include <sys/param.h>
81238104Sdes#include <sys/systm.h>
82238104Sdes#include <sys/kernel.h>
83238104Sdes#include <sys/lock.h>
84238104Sdes#include <sys/proc.h>
85238104Sdes#include <sys/resourcevar.h>
86238104Sdes#include <sys/rwlock.h>
87238104Sdes#include <sys/sysctl.h>
88238104Sdes#include <sys/vmmeter.h>
89238104Sdes#include <sys/vnode.h>
90238104Sdes#ifdef KTRACE
91238104Sdes#include <sys/ktrace.h>
92238104Sdes#endif
93238104Sdes
94238104Sdes#include <vm/vm.h>
95238104Sdes#include <vm/vm_param.h>
96238104Sdes#include <vm/pmap.h>
97238104Sdes#include <vm/vm_map.h>
98238104Sdes#include <vm/vm_object.h>
99238104Sdes#include <vm/vm_page.h>
100238104Sdes#include <vm/vm_pageout.h>
101238104Sdes#include <vm/vm_kern.h>
102238104Sdes#include <vm/vm_pager.h>
103238104Sdes#include <vm/vm_extern.h>
104238104Sdes
105238104Sdes#define PFBAK 4
106238104Sdes#define PFFOR 4
107238104Sdes
108238104Sdesstatic int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
109269257Sdes
110238104Sdes#define	VM_FAULT_READ_BEHIND	8
111238104Sdes#define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
112238104Sdes#define	VM_FAULT_NINCR		(VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
113238104Sdes#define	VM_FAULT_SUM		(VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
114238104Sdes#define	VM_FAULT_CACHE_BEHIND	(VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
115238104Sdes
116238104Sdesstruct faultstate {
117238104Sdes	vm_page_t m;
118238104Sdes	vm_object_t object;
119238104Sdes	vm_pindex_t pindex;
120238104Sdes	vm_page_t first_m;
121238104Sdes	vm_object_t	first_object;
122238104Sdes	vm_pindex_t first_pindex;
123238104Sdes	vm_map_t map;
124269257Sdes	vm_map_entry_t entry;
125238104Sdes	int lookup_still_valid;
126238104Sdes	struct vnode *vp;
127238104Sdes};
128238104Sdes
129238104Sdesstatic void vm_fault_cache_behind(const struct faultstate *fs, int distance);
130238104Sdesstatic void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
131238104Sdes	    int faultcount, int reqpage);
132238104Sdes
133238104Sdesstatic inline void
134238104Sdesrelease_page(struct faultstate *fs)
135238104Sdes{
136238104Sdes
137238104Sdes	vm_page_xunbusy(fs->m);
138238104Sdes	vm_page_lock(fs->m);
139238104Sdes	vm_page_deactivate(fs->m);
140238104Sdes	vm_page_unlock(fs->m);
141238104Sdes	fs->m = NULL;
142238104Sdes}
143238104Sdes
144238104Sdesstatic inline void
145238104Sdesunlock_map(struct faultstate *fs)
146238104Sdes{
147238104Sdes
148238104Sdes	if (fs->lookup_still_valid) {
149238104Sdes		vm_map_lookup_done(fs->map, fs->entry);
150238104Sdes		fs->lookup_still_valid = FALSE;
151238104Sdes	}
152238104Sdes}
153238104Sdes
154238104Sdesstatic void
155238104Sdesunlock_and_deallocate(struct faultstate *fs)
156238104Sdes{
157238104Sdes
158238104Sdes	vm_object_pip_wakeup(fs->object);
159238104Sdes	VM_OBJECT_WUNLOCK(fs->object);
160238104Sdes	if (fs->object != fs->first_object) {
161238104Sdes		VM_OBJECT_WLOCK(fs->first_object);
162238104Sdes		vm_page_lock(fs->first_m);
163238104Sdes		vm_page_free(fs->first_m);
164238104Sdes		vm_page_unlock(fs->first_m);
165238104Sdes		vm_object_pip_wakeup(fs->first_object);
166238104Sdes		VM_OBJECT_WUNLOCK(fs->first_object);
167238104Sdes		fs->first_m = NULL;
168238104Sdes	}
169238104Sdes	vm_object_deallocate(fs->first_object);
170238104Sdes	unlock_map(fs);
171238104Sdes	if (fs->vp != NULL) {
172238104Sdes		vput(fs->vp);
173238104Sdes		fs->vp = NULL;
174238104Sdes	}
175238104Sdes}
176238104Sdes
177238104Sdes/*
178238104Sdes * TRYPAGER - used by vm_fault to calculate whether the pager for the
179238104Sdes *	      current object *might* contain the page.
180238104Sdes *
181238104Sdes *	      default objects are zero-fill, there is no real pager.
182238104Sdes */
183238104Sdes#define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
184238104Sdes			((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
185238104Sdes
186238104Sdes/*
187238104Sdes *	vm_fault:
188238104Sdes *
189238104Sdes *	Handle a page fault occurring at the given address,
190238104Sdes *	requiring the given permissions, in the map specified.
191238104Sdes *	If successful, the page is inserted into the
192238104Sdes *	associated physical map.
193238104Sdes *
194238104Sdes *	NOTE: the given address should be truncated to the
195238104Sdes *	proper page address.
196238104Sdes *
197238104Sdes *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
198238104Sdes *	a standard error specifying why the fault is fatal is returned.
199238104Sdes *
200238104Sdes *	The map in question must be referenced, and remains so.
201238104Sdes *	Caller may hold no locks.
202238104Sdes */
203238104Sdesint
204238104Sdesvm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
205238104Sdes    int fault_flags)
206238104Sdes{
207238104Sdes	struct thread *td;
208238104Sdes	int result;
209238104Sdes
210238104Sdes	td = curthread;
211238104Sdes	if ((td->td_pflags & TDP_NOFAULTING) != 0)
212238104Sdes		return (KERN_PROTECTION_FAILURE);
213238104Sdes#ifdef KTRACE
214238104Sdes	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
215238104Sdes		ktrfault(vaddr, fault_type);
216238104Sdes#endif
217238104Sdes	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
218238104Sdes	    NULL);
219238104Sdes#ifdef KTRACE
220238104Sdes	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
221238104Sdes		ktrfaultend(result);
222238104Sdes#endif
223238104Sdes	return (result);
224238104Sdes}
225238104Sdes
226238104Sdesint
227238104Sdesvm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
228238104Sdes    int fault_flags, vm_page_t *m_hold)
229238104Sdes{
230238104Sdes	vm_prot_t prot;
231238104Sdes	long ahead, behind;
232238104Sdes	int alloc_req, era, faultcount, nera, reqpage, result;
233238104Sdes	boolean_t growstack, is_first_object_locked, wired;
234238104Sdes	int map_generation;
235238104Sdes	vm_object_t next_object;
236238104Sdes	vm_page_t marray[VM_FAULT_READ_MAX];
237238104Sdes	int hardfault;
238238104Sdes	struct faultstate fs;
239238104Sdes	struct vnode *vp;
240238104Sdes	vm_page_t m;
241238104Sdes	int locked, error;
242238104Sdes
243238104Sdes	hardfault = 0;
244238104Sdes	growstack = TRUE;
245238104Sdes	PCPU_INC(cnt.v_vm_faults);
246238104Sdes	fs.vp = NULL;
247238104Sdes	faultcount = reqpage = 0;
248238104Sdes
249238104SdesRetryFault:;
250238104Sdes
251238104Sdes	/*
252238104Sdes	 * Find the backing store object and offset into it to begin the
253238104Sdes	 * search.
254238104Sdes	 */
255238104Sdes	fs.map = map;
256238104Sdes	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
257238104Sdes	    &fs.first_object, &fs.first_pindex, &prot, &wired);
258238104Sdes	if (result != KERN_SUCCESS) {
259238104Sdes		if (growstack && result == KERN_INVALID_ADDRESS &&
260238104Sdes		    map != kernel_map) {
261238104Sdes			result = vm_map_growstack(curproc, vaddr);
262238104Sdes			if (result != KERN_SUCCESS)
263238104Sdes				return (KERN_FAILURE);
264238104Sdes			growstack = FALSE;
265238104Sdes			goto RetryFault;
266238104Sdes		}
267238104Sdes		return (result);
268238104Sdes	}
269238104Sdes
270238104Sdes	map_generation = fs.map->timestamp;
271238104Sdes
272238104Sdes	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
273238104Sdes		if ((curthread->td_pflags & TDP_DEVMEMIO) != 0) {
274238104Sdes			vm_map_unlock_read(fs.map);
275238104Sdes			return (KERN_FAILURE);
276238104Sdes		}
277238104Sdes		panic("vm_fault: fault on nofault entry, addr: %lx",
278238104Sdes		    (u_long)vaddr);
279238104Sdes	}
280238104Sdes
281238104Sdes	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
282238104Sdes	    fs.entry->wiring_thread != curthread) {
283238104Sdes		vm_map_unlock_read(fs.map);
284238104Sdes		vm_map_lock(fs.map);
285238104Sdes		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
286238104Sdes		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
287238104Sdes			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
288238104Sdes			vm_map_unlock_and_wait(fs.map, 0);
289238104Sdes		} else
290238104Sdes			vm_map_unlock(fs.map);
291238104Sdes		goto RetryFault;
292238104Sdes	}
293238104Sdes
294238104Sdes	if (wired)
295238104Sdes		fault_type = prot | (fault_type & VM_PROT_COPY);
296238104Sdes
297238104Sdes	if (fs.vp == NULL /* avoid locked vnode leak */ &&
298238104Sdes	    (fault_flags & (VM_FAULT_CHANGE_WIRING | VM_FAULT_DIRTY)) == 0 &&
299238104Sdes	    /* avoid calling vm_object_set_writeable_dirty() */
300238104Sdes	    ((prot & VM_PROT_WRITE) == 0 ||
301238104Sdes	    fs.first_object->type != OBJT_VNODE ||
302238104Sdes	    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) != 0)) {
303238104Sdes		VM_OBJECT_RLOCK(fs.first_object);
304238104Sdes		if ((prot & VM_PROT_WRITE) != 0 &&
305238104Sdes		    fs.first_object->type == OBJT_VNODE &&
306238104Sdes		    (fs.first_object->flags & OBJ_MIGHTBEDIRTY) == 0)
307238104Sdes			goto fast_failed;
308238104Sdes		m = vm_page_lookup(fs.first_object, fs.first_pindex);
309238104Sdes		/* A busy page can be mapped for read|execute access. */
310238104Sdes		if (m == NULL || ((prot & VM_PROT_WRITE) != 0 &&
311238104Sdes		    vm_page_busied(m)) || m->valid != VM_PAGE_BITS_ALL)
312238104Sdes			goto fast_failed;
313238104Sdes		result = pmap_enter(fs.map->pmap, vaddr, m, prot,
314238104Sdes		   fault_type | PMAP_ENTER_NOSLEEP | (wired ? PMAP_ENTER_WIRED :
315238104Sdes		   0), 0);
316238104Sdes		if (result != KERN_SUCCESS)
317238104Sdes			goto fast_failed;
318238104Sdes		if (m_hold != NULL) {
319238104Sdes			*m_hold = m;
320238104Sdes			vm_page_lock(m);
321238104Sdes			vm_page_hold(m);
322238104Sdes			vm_page_unlock(m);
323238104Sdes		}
324238104Sdes		if ((fault_type & VM_PROT_WRITE) != 0 &&
325238104Sdes		    (m->oflags & VPO_UNMANAGED) == 0) {
326238104Sdes			vm_page_dirty(m);
327238104Sdes			vm_pager_page_unswapped(m);
328238104Sdes		}
329238104Sdes		VM_OBJECT_RUNLOCK(fs.first_object);
330238104Sdes		if (!wired)
331238104Sdes			vm_fault_prefault(&fs, vaddr, 0, 0);
332238104Sdes		vm_map_lookup_done(fs.map, fs.entry);
333238104Sdes		curthread->td_ru.ru_minflt++;
334238104Sdes		return (KERN_SUCCESS);
335238104Sdesfast_failed:
336238104Sdes		if (!VM_OBJECT_TRYUPGRADE(fs.first_object)) {
337238104Sdes			VM_OBJECT_RUNLOCK(fs.first_object);
338238104Sdes			VM_OBJECT_WLOCK(fs.first_object);
339238104Sdes		}
340238104Sdes	} else {
341238104Sdes		VM_OBJECT_WLOCK(fs.first_object);
342238104Sdes	}
343238104Sdes
344238104Sdes	/*
345238104Sdes	 * Make a reference to this object to prevent its disposal while we
346238104Sdes	 * are messing with it.  Once we have the reference, the map is free
347238104Sdes	 * to be diddled.  Since objects reference their shadows (and copies),
348238104Sdes	 * they will stay around as well.
349238104Sdes	 *
350238104Sdes	 * Bump the paging-in-progress count to prevent size changes (e.g.
351238104Sdes	 * truncation operations) during I/O.  This must be done after
352238104Sdes	 * obtaining the vnode lock in order to avoid possible deadlocks.
353238104Sdes	 */
354238104Sdes	vm_object_reference_locked(fs.first_object);
355238104Sdes	vm_object_pip_add(fs.first_object, 1);
356238104Sdes
357238104Sdes	fs.lookup_still_valid = TRUE;
358238104Sdes
359238104Sdes	fs.first_m = NULL;
360238104Sdes
361238104Sdes	/*
362238104Sdes	 * Search for the page at object/offset.
363238104Sdes	 */
364238104Sdes	fs.object = fs.first_object;
365238104Sdes	fs.pindex = fs.first_pindex;
366238104Sdes	while (TRUE) {
367238104Sdes		/*
368238104Sdes		 * If the object is dead, we stop here
369238104Sdes		 */
370238104Sdes		if (fs.object->flags & OBJ_DEAD) {
371238104Sdes			unlock_and_deallocate(&fs);
372238104Sdes			return (KERN_PROTECTION_FAILURE);
373238104Sdes		}
374238104Sdes
375238104Sdes		/*
376238104Sdes		 * See if page is resident
377238104Sdes		 */
378238104Sdes		fs.m = vm_page_lookup(fs.object, fs.pindex);
379238104Sdes		if (fs.m != NULL) {
380238104Sdes			/*
381238104Sdes			 * Wait/Retry if the page is busy.  We have to do this
382238104Sdes			 * if the page is either exclusive or shared busy
383238104Sdes			 * because the vm_pager may be using read busy for
384238104Sdes			 * pageouts (and even pageins if it is the vnode
385238104Sdes			 * pager), and we could end up trying to pagein and
386238104Sdes			 * pageout the same page simultaneously.
387238104Sdes			 *
388238104Sdes			 * We can theoretically allow the busy case on a read
389238104Sdes			 * fault if the page is marked valid, but since such
390238104Sdes			 * pages are typically already pmap'd, putting that
391238104Sdes			 * special case in might be more effort then it is
392238104Sdes			 * worth.  We cannot under any circumstances mess
393238104Sdes			 * around with a shared busied page except, perhaps,
394238104Sdes			 * to pmap it.
395238104Sdes			 */
396238104Sdes			if (vm_page_busied(fs.m)) {
397238104Sdes				/*
398238104Sdes				 * Reference the page before unlocking and
399238104Sdes				 * sleeping so that the page daemon is less
400238104Sdes				 * likely to reclaim it.
401238104Sdes				 */
402238104Sdes				vm_page_aflag_set(fs.m, PGA_REFERENCED);
403238104Sdes				if (fs.object != fs.first_object) {
404238104Sdes					if (!VM_OBJECT_TRYWLOCK(
405238104Sdes					    fs.first_object)) {
406238104Sdes						VM_OBJECT_WUNLOCK(fs.object);
407238104Sdes						VM_OBJECT_WLOCK(fs.first_object);
408238104Sdes						VM_OBJECT_WLOCK(fs.object);
409238104Sdes					}
410238104Sdes					vm_page_lock(fs.first_m);
411238104Sdes					vm_page_free(fs.first_m);
412238104Sdes					vm_page_unlock(fs.first_m);
413269257Sdes					vm_object_pip_wakeup(fs.first_object);
414269257Sdes					VM_OBJECT_WUNLOCK(fs.first_object);
415269257Sdes					fs.first_m = NULL;
416269257Sdes				}
417269257Sdes				unlock_map(&fs);
418269257Sdes				if (fs.m == vm_page_lookup(fs.object,
419269257Sdes				    fs.pindex)) {
420269257Sdes					vm_page_sleep_if_busy(fs.m, "vmpfw");
421269257Sdes				}
422269257Sdes				vm_object_pip_wakeup(fs.object);
423269257Sdes				VM_OBJECT_WUNLOCK(fs.object);
424269257Sdes				PCPU_INC(cnt.v_intrans);
425269257Sdes				vm_object_deallocate(fs.first_object);
426269257Sdes				goto RetryFault;
427269257Sdes			}
428269257Sdes			vm_page_lock(fs.m);
429238104Sdes			vm_page_remque(fs.m);
430238104Sdes			vm_page_unlock(fs.m);
431238104Sdes
432238104Sdes			/*
433238104Sdes			 * Mark page busy for other processes, and the
434238104Sdes			 * pagedaemon.  If it still isn't completely valid
435238104Sdes			 * (readable), jump to readrest, else break-out ( we
436238104Sdes			 * found the page ).
437238104Sdes			 */
438238104Sdes			vm_page_xbusy(fs.m);
439238104Sdes			if (fs.m->valid != VM_PAGE_BITS_ALL)
440238104Sdes				goto readrest;
441238104Sdes			break;
442238104Sdes		}
443238104Sdes
444238104Sdes		/*
445238104Sdes		 * Page is not resident, If this is the search termination
446238104Sdes		 * or the pager might contain the page, allocate a new page.
447238104Sdes		 */
448238104Sdes		if (TRYPAGER || fs.object == fs.first_object) {
449238104Sdes			if (fs.pindex >= fs.object->size) {
450238104Sdes				unlock_and_deallocate(&fs);
451238104Sdes				return (KERN_PROTECTION_FAILURE);
452238104Sdes			}
453238104Sdes
454238104Sdes			/*
455238104Sdes			 * Allocate a new page for this object/offset pair.
456238104Sdes			 *
457238104Sdes			 * Unlocked read of the p_flag is harmless. At
458238104Sdes			 * worst, the P_KILLED might be not observed
459238104Sdes			 * there, and allocation can fail, causing
460238104Sdes			 * restart and new reading of the p_flag.
461238104Sdes			 */
462238104Sdes			fs.m = NULL;
463238104Sdes			if (!vm_page_count_severe() || P_KILLED(curproc)) {
464238104Sdes#if VM_NRESERVLEVEL > 0
465238104Sdes				if ((fs.object->flags & OBJ_COLORED) == 0) {
466238104Sdes					fs.object->flags |= OBJ_COLORED;
467238104Sdes					fs.object->pg_color = atop(vaddr) -
468238104Sdes					    fs.pindex;
469238104Sdes				}
470238104Sdes#endif
471238104Sdes				alloc_req = P_KILLED(curproc) ?
472238104Sdes				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
473238104Sdes				if (fs.object->type != OBJT_VNODE &&
474238104Sdes				    fs.object->backing_object == NULL)
475238104Sdes					alloc_req |= VM_ALLOC_ZERO;
476238104Sdes				fs.m = vm_page_alloc(fs.object, fs.pindex,
477238104Sdes				    alloc_req);
478238104Sdes			}
479238104Sdes			if (fs.m == NULL) {
480238104Sdes				unlock_and_deallocate(&fs);
481238104Sdes				VM_WAITPFAULT;
482238104Sdes				goto RetryFault;
483238104Sdes			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
484238104Sdes				break;
485238104Sdes		}
486238104Sdes
487238104Sdesreadrest:
488238104Sdes		/*
489238104Sdes		 * We have found a valid page or we have allocated a new page.
490238104Sdes		 * The page thus may not be valid or may not be entirely
491238104Sdes		 * valid.
492238104Sdes		 *
493238104Sdes		 * Attempt to fault-in the page if there is a chance that the
494238104Sdes		 * pager has it, and potentially fault in additional pages
495238104Sdes		 * at the same time.
496238104Sdes		 */
497238104Sdes		if (TRYPAGER) {
498238104Sdes			int rv;
499238104Sdes			u_char behavior = vm_map_entry_behavior(fs.entry);
500238104Sdes
501238104Sdes			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
502238104Sdes			    P_KILLED(curproc)) {
503238104Sdes				behind = 0;
504238104Sdes				ahead = 0;
505238104Sdes			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
506238104Sdes				behind = 0;
507238104Sdes				ahead = atop(fs.entry->end - vaddr) - 1;
508238104Sdes				if (ahead > VM_FAULT_READ_AHEAD_MAX)
509238104Sdes					ahead = VM_FAULT_READ_AHEAD_MAX;
510238104Sdes				if (fs.pindex == fs.entry->next_read)
511238104Sdes					vm_fault_cache_behind(&fs,
512238104Sdes					    VM_FAULT_READ_MAX);
513238104Sdes			} else {
514238104Sdes				/*
515238104Sdes				 * If this is a sequential page fault, then
516238104Sdes				 * arithmetically increase the number of pages
517238104Sdes				 * in the read-ahead window.  Otherwise, reset
518238104Sdes				 * the read-ahead window to its smallest size.
519238104Sdes				 */
520238104Sdes				behind = atop(vaddr - fs.entry->start);
521238104Sdes				if (behind > VM_FAULT_READ_BEHIND)
522238104Sdes					behind = VM_FAULT_READ_BEHIND;
523238104Sdes				ahead = atop(fs.entry->end - vaddr) - 1;
524238104Sdes				era = fs.entry->read_ahead;
525238104Sdes				if (fs.pindex == fs.entry->next_read) {
526238104Sdes					nera = era + behind;
527238104Sdes					if (nera > VM_FAULT_READ_AHEAD_MAX)
528238104Sdes						nera = VM_FAULT_READ_AHEAD_MAX;
529238104Sdes					behind = 0;
530238104Sdes					if (ahead > nera)
531238104Sdes						ahead = nera;
532238104Sdes					if (era == VM_FAULT_READ_AHEAD_MAX)
533238104Sdes						vm_fault_cache_behind(&fs,
534238104Sdes						    VM_FAULT_CACHE_BEHIND);
535238104Sdes				} else if (ahead > VM_FAULT_READ_AHEAD_MIN)
536238104Sdes					ahead = VM_FAULT_READ_AHEAD_MIN;
537238104Sdes				if (era != ahead)
538238104Sdes					fs.entry->read_ahead = ahead;
539238104Sdes			}
540238104Sdes
541238104Sdes			/*
542238104Sdes			 * Call the pager to retrieve the data, if any, after
543238104Sdes			 * releasing the lock on the map.  We hold a ref on
544238104Sdes			 * fs.object and the pages are exclusive busied.
545238104Sdes			 */
546238104Sdes			unlock_map(&fs);
547238104Sdes
548238104Sdes			if (fs.object->type == OBJT_VNODE) {
549238104Sdes				vp = fs.object->handle;
550238104Sdes				if (vp == fs.vp)
551238104Sdes					goto vnode_locked;
552238104Sdes				else if (fs.vp != NULL) {
553238104Sdes					vput(fs.vp);
554238104Sdes					fs.vp = NULL;
555238104Sdes				}
556238104Sdes				locked = VOP_ISLOCKED(vp);
557238104Sdes
558238104Sdes				if (locked != LK_EXCLUSIVE)
559238104Sdes					locked = LK_SHARED;
560238104Sdes				/* Do not sleep for vnode lock while fs.m is busy */
561238104Sdes				error = vget(vp, locked | LK_CANRECURSE |
562238104Sdes				    LK_NOWAIT, curthread);
563238104Sdes				if (error != 0) {
564238104Sdes					vhold(vp);
565238104Sdes					release_page(&fs);
566238104Sdes					unlock_and_deallocate(&fs);
567238104Sdes					error = vget(vp, locked | LK_RETRY |
568238104Sdes					    LK_CANRECURSE, curthread);
569238104Sdes					vdrop(vp);
570238104Sdes					fs.vp = vp;
571238104Sdes					KASSERT(error == 0,
572238104Sdes					    ("vm_fault: vget failed"));
573238104Sdes					goto RetryFault;
574238104Sdes				}
575238104Sdes				fs.vp = vp;
576238104Sdes			}
577238104Sdesvnode_locked:
578238104Sdes			KASSERT(fs.vp == NULL || !fs.map->system_map,
579238104Sdes			    ("vm_fault: vnode-backed object mapped by system map"));
580238104Sdes
581238104Sdes			/*
582238104Sdes			 * now we find out if any other pages should be paged
583238104Sdes			 * in at this time this routine checks to see if the
584238104Sdes			 * pages surrounding this fault reside in the same
585238104Sdes			 * object as the page for this fault.  If they do,
586238104Sdes			 * then they are faulted in also into the object.  The
587238104Sdes			 * array "marray" returned contains an array of
588238104Sdes			 * vm_page_t structs where one of them is the
589238104Sdes			 * vm_page_t passed to the routine.  The reqpage
590238104Sdes			 * return value is the index into the marray for the
591238104Sdes			 * vm_page_t passed to the routine.
592238104Sdes			 *
593238104Sdes			 * fs.m plus the additional pages are exclusive busied.
594238104Sdes			 */
595238104Sdes			faultcount = vm_fault_additional_pages(
596238104Sdes			    fs.m, behind, ahead, marray, &reqpage);
597238104Sdes
598238104Sdes			rv = faultcount ?
599238104Sdes			    vm_pager_get_pages(fs.object, marray, faultcount,
600238104Sdes				reqpage) : VM_PAGER_FAIL;
601238104Sdes
602238104Sdes			if (rv == VM_PAGER_OK) {
603238104Sdes				/*
604238104Sdes				 * Found the page. Leave it busy while we play
605238104Sdes				 * with it.
606238104Sdes				 */
607238104Sdes
608238104Sdes				/*
609238104Sdes				 * Relookup in case pager changed page. Pager
610238104Sdes				 * is responsible for disposition of old page
611238104Sdes				 * if moved.
612238104Sdes				 */
613238104Sdes				fs.m = vm_page_lookup(fs.object, fs.pindex);
614238104Sdes				if (!fs.m) {
615238104Sdes					unlock_and_deallocate(&fs);
616238104Sdes					goto RetryFault;
617238104Sdes				}
618238104Sdes
619238104Sdes				hardfault++;
620238104Sdes				break; /* break to PAGE HAS BEEN FOUND */
621238104Sdes			}
622238104Sdes			/*
623238104Sdes			 * Remove the bogus page (which does not exist at this
624238104Sdes			 * object/offset); before doing so, we must get back
625238104Sdes			 * our object lock to preserve our invariant.
626238104Sdes			 *
627238104Sdes			 * Also wake up any other process that may want to bring
628238104Sdes			 * in this page.
629238104Sdes			 *
630238104Sdes			 * If this is the top-level object, we must leave the
631238104Sdes			 * busy page to prevent another process from rushing
632238104Sdes			 * past us, and inserting the page in that object at
633238104Sdes			 * the same time that we are.
634238104Sdes			 */
635238104Sdes			if (rv == VM_PAGER_ERROR)
636238104Sdes				printf("vm_fault: pager read error, pid %d (%s)\n",
637238104Sdes				    curproc->p_pid, curproc->p_comm);
638238104Sdes			/*
639238104Sdes			 * Data outside the range of the pager or an I/O error
640238104Sdes			 */
641238104Sdes			/*
642238104Sdes			 * XXX - the check for kernel_map is a kludge to work
643238104Sdes			 * around having the machine panic on a kernel space
644238104Sdes			 * fault w/ I/O error.
645238104Sdes			 */
646238104Sdes			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
647238104Sdes				(rv == VM_PAGER_BAD)) {
648238104Sdes				vm_page_lock(fs.m);
649238104Sdes				vm_page_free(fs.m);
650238104Sdes				vm_page_unlock(fs.m);
651238104Sdes				fs.m = NULL;
652238104Sdes				unlock_and_deallocate(&fs);
653238104Sdes				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
654238104Sdes			}
655238104Sdes			if (fs.object != fs.first_object) {
656238104Sdes				vm_page_lock(fs.m);
657238104Sdes				vm_page_free(fs.m);
658238104Sdes				vm_page_unlock(fs.m);
659238104Sdes				fs.m = NULL;
660238104Sdes				/*
661238104Sdes				 * XXX - we cannot just fall out at this
662238104Sdes				 * point, m has been freed and is invalid!
663238104Sdes				 */
664238104Sdes			}
665238104Sdes		}
666238104Sdes
667238104Sdes		/*
668238104Sdes		 * We get here if the object has default pager (or unwiring)
669238104Sdes		 * or the pager doesn't have the page.
670238104Sdes		 */
671238104Sdes		if (fs.object == fs.first_object)
672238104Sdes			fs.first_m = fs.m;
673238104Sdes
674238104Sdes		/*
675238104Sdes		 * Move on to the next object.  Lock the next object before
676238104Sdes		 * unlocking the current one.
677238104Sdes		 */
678238104Sdes		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
679238104Sdes		next_object = fs.object->backing_object;
680238104Sdes		if (next_object == NULL) {
681238104Sdes			/*
682238104Sdes			 * If there's no object left, fill the page in the top
683238104Sdes			 * object with zeros.
684238104Sdes			 */
685238104Sdes			if (fs.object != fs.first_object) {
686238104Sdes				vm_object_pip_wakeup(fs.object);
687238104Sdes				VM_OBJECT_WUNLOCK(fs.object);
688238104Sdes
689238104Sdes				fs.object = fs.first_object;
690238104Sdes				fs.pindex = fs.first_pindex;
691238104Sdes				fs.m = fs.first_m;
692238104Sdes				VM_OBJECT_WLOCK(fs.object);
693238104Sdes			}
694238104Sdes			fs.first_m = NULL;
695238104Sdes
696238104Sdes			/*
697238104Sdes			 * Zero the page if necessary and mark it valid.
698238104Sdes			 */
699238104Sdes			if ((fs.m->flags & PG_ZERO) == 0) {
700238104Sdes				pmap_zero_page(fs.m);
701238104Sdes			} else {
702238104Sdes				PCPU_INC(cnt.v_ozfod);
703238104Sdes			}
704238104Sdes			PCPU_INC(cnt.v_zfod);
705238104Sdes			fs.m->valid = VM_PAGE_BITS_ALL;
706238104Sdes			/* Don't try to prefault neighboring pages. */
707238104Sdes			faultcount = 1;
708238104Sdes			break;	/* break to PAGE HAS BEEN FOUND */
709238104Sdes		} else {
710238104Sdes			KASSERT(fs.object != next_object,
711238104Sdes			    ("object loop %p", next_object));
712238104Sdes			VM_OBJECT_WLOCK(next_object);
713238104Sdes			vm_object_pip_add(next_object, 1);
714238104Sdes			if (fs.object != fs.first_object)
715238104Sdes				vm_object_pip_wakeup(fs.object);
716238104Sdes			VM_OBJECT_WUNLOCK(fs.object);
717238104Sdes			fs.object = next_object;
718238104Sdes		}
719238104Sdes	}
720238104Sdes
721238104Sdes	vm_page_assert_xbusied(fs.m);
722238104Sdes
723238104Sdes	/*
724238104Sdes	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
725238104Sdes	 * is held.]
726238104Sdes	 */
727238104Sdes
728238104Sdes	/*
729238104Sdes	 * If the page is being written, but isn't already owned by the
730238104Sdes	 * top-level object, we have to copy it into a new page owned by the
731238104Sdes	 * top-level object.
732238104Sdes	 */
733238104Sdes	if (fs.object != fs.first_object) {
734238104Sdes		/*
735238104Sdes		 * We only really need to copy if we want to write it.
736238104Sdes		 */
737238104Sdes		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
738238104Sdes			/*
739238104Sdes			 * This allows pages to be virtually copied from a
740238104Sdes			 * backing_object into the first_object, where the
741238104Sdes			 * backing object has no other refs to it, and cannot
742238104Sdes			 * gain any more refs.  Instead of a bcopy, we just
743238104Sdes			 * move the page from the backing object to the
744238104Sdes			 * first object.  Note that we must mark the page
745238104Sdes			 * dirty in the first object so that it will go out
746238104Sdes			 * to swap when needed.
747238104Sdes			 */
748238104Sdes			is_first_object_locked = FALSE;
749238104Sdes			if (
750238104Sdes				/*
751238104Sdes				 * Only one shadow object
752238104Sdes				 */
753238104Sdes				(fs.object->shadow_count == 1) &&
754238104Sdes				/*
755238104Sdes				 * No COW refs, except us
756238104Sdes				 */
757238104Sdes				(fs.object->ref_count == 1) &&
758238104Sdes				/*
759238104Sdes				 * No one else can look this object up
760238104Sdes				 */
761238104Sdes				(fs.object->handle == NULL) &&
762238104Sdes				/*
763238104Sdes				 * No other ways to look the object up
764238104Sdes				 */
765238104Sdes				((fs.object->type == OBJT_DEFAULT) ||
766238104Sdes				 (fs.object->type == OBJT_SWAP)) &&
767238104Sdes			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
768238104Sdes				/*
769238104Sdes				 * We don't chase down the shadow chain
770238104Sdes				 */
771238104Sdes			    fs.object == fs.first_object->backing_object) {
772238104Sdes				/*
773238104Sdes				 * get rid of the unnecessary page
774238104Sdes				 */
775238104Sdes				vm_page_lock(fs.first_m);
776238104Sdes				vm_page_free(fs.first_m);
777238104Sdes				vm_page_unlock(fs.first_m);
778238104Sdes				/*
779238104Sdes				 * grab the page and put it into the
780238104Sdes				 * process'es object.  The page is
781238104Sdes				 * automatically made dirty.
782238104Sdes				 */
783238104Sdes				if (vm_page_rename(fs.m, fs.first_object,
784238104Sdes				    fs.first_pindex)) {
785238104Sdes					unlock_and_deallocate(&fs);
786238104Sdes					goto RetryFault;
787238104Sdes				}
788238104Sdes				vm_page_xbusy(fs.m);
789238104Sdes				fs.first_m = fs.m;
790238104Sdes				fs.m = NULL;
791238104Sdes				PCPU_INC(cnt.v_cow_optim);
792238104Sdes			} else {
793238104Sdes				/*
794238104Sdes				 * Oh, well, lets copy it.
795238104Sdes				 */
796238104Sdes				pmap_copy_page(fs.m, fs.first_m);
797238104Sdes				fs.first_m->valid = VM_PAGE_BITS_ALL;
798238104Sdes				if (wired && (fault_flags &
799238104Sdes				    VM_FAULT_CHANGE_WIRING) == 0) {
800238104Sdes					vm_page_lock(fs.first_m);
801238104Sdes					vm_page_wire(fs.first_m);
802238104Sdes					vm_page_unlock(fs.first_m);
803238104Sdes
804238104Sdes					vm_page_lock(fs.m);
805238104Sdes					vm_page_unwire(fs.m, FALSE);
806238104Sdes					vm_page_unlock(fs.m);
807238104Sdes				}
808238104Sdes				/*
809238104Sdes				 * We no longer need the old page or object.
810238104Sdes				 */
811238104Sdes				release_page(&fs);
812238104Sdes			}
813238104Sdes			/*
814238104Sdes			 * fs.object != fs.first_object due to above
815238104Sdes			 * conditional
816238104Sdes			 */
817238104Sdes			vm_object_pip_wakeup(fs.object);
818238104Sdes			VM_OBJECT_WUNLOCK(fs.object);
819238104Sdes			/*
820238104Sdes			 * Only use the new page below...
821238104Sdes			 */
822238104Sdes			fs.object = fs.first_object;
823238104Sdes			fs.pindex = fs.first_pindex;
824238104Sdes			fs.m = fs.first_m;
825238104Sdes			if (!is_first_object_locked)
826238104Sdes				VM_OBJECT_WLOCK(fs.object);
827238104Sdes			PCPU_INC(cnt.v_cow_faults);
828238104Sdes			curthread->td_cow++;
829238104Sdes		} else {
830238104Sdes			prot &= ~VM_PROT_WRITE;
831238104Sdes		}
832238104Sdes	}
833238104Sdes
834238104Sdes	/*
835238104Sdes	 * We must verify that the maps have not changed since our last
836238104Sdes	 * lookup.
837238104Sdes	 */
838238104Sdes	if (!fs.lookup_still_valid) {
839238104Sdes		vm_object_t retry_object;
840238104Sdes		vm_pindex_t retry_pindex;
841238104Sdes		vm_prot_t retry_prot;
842238104Sdes
843238104Sdes		if (!vm_map_trylock_read(fs.map)) {
844238104Sdes			release_page(&fs);
845238104Sdes			unlock_and_deallocate(&fs);
846238104Sdes			goto RetryFault;
847238104Sdes		}
848238104Sdes		fs.lookup_still_valid = TRUE;
849238104Sdes		if (fs.map->timestamp != map_generation) {
850238104Sdes			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
851238104Sdes			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
852238104Sdes
853238104Sdes			/*
854238104Sdes			 * If we don't need the page any longer, put it on the inactive
855238104Sdes			 * list (the easiest thing to do here).  If no one needs it,
856238104Sdes			 * pageout will grab it eventually.
857238104Sdes			 */
858238104Sdes			if (result != KERN_SUCCESS) {
859238104Sdes				release_page(&fs);
860238104Sdes				unlock_and_deallocate(&fs);
861238104Sdes
862238104Sdes				/*
863238104Sdes				 * If retry of map lookup would have blocked then
864238104Sdes				 * retry fault from start.
865238104Sdes				 */
866238104Sdes				if (result == KERN_FAILURE)
867238104Sdes					goto RetryFault;
868238104Sdes				return (result);
869238104Sdes			}
870238104Sdes			if ((retry_object != fs.first_object) ||
871238104Sdes			    (retry_pindex != fs.first_pindex)) {
872238104Sdes				release_page(&fs);
873238104Sdes				unlock_and_deallocate(&fs);
874238104Sdes				goto RetryFault;
875238104Sdes			}
876238104Sdes
877238104Sdes			/*
878238104Sdes			 * Check whether the protection has changed or the object has
879238104Sdes			 * been copied while we left the map unlocked. Changing from
880238104Sdes			 * read to write permission is OK - we leave the page
881238104Sdes			 * write-protected, and catch the write fault. Changing from
882238104Sdes			 * write to read permission means that we can't mark the page
883238104Sdes			 * write-enabled after all.
884238104Sdes			 */
885238104Sdes			prot &= retry_prot;
886238104Sdes		}
887238104Sdes	}
888238104Sdes	/*
889238104Sdes	 * If the page was filled by a pager, update the map entry's
890238104Sdes	 * last read offset.  Since the pager does not return the
891238104Sdes	 * actual set of pages that it read, this update is based on
892238104Sdes	 * the requested set.  Typically, the requested and actual
893238104Sdes	 * sets are the same.
894238104Sdes	 *
895238104Sdes	 * XXX The following assignment modifies the map
896238104Sdes	 * without holding a write lock on it.
897238104Sdes	 */
898238104Sdes	if (hardfault)
899238104Sdes		fs.entry->next_read = fs.pindex + faultcount - reqpage;
900238104Sdes
901238104Sdes	if (((prot & VM_PROT_WRITE) != 0 ||
902238104Sdes	    (fault_flags & VM_FAULT_DIRTY) != 0) &&
903238104Sdes	    (fs.m->oflags & VPO_UNMANAGED) == 0) {
904238104Sdes		vm_object_set_writeable_dirty(fs.object);
905238104Sdes
906238104Sdes		/*
907238104Sdes		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
908238104Sdes		 * if the page is already dirty to prevent data written with
909238104Sdes		 * the expectation of being synced from not being synced.
910238104Sdes		 * Likewise if this entry does not request NOSYNC then make
911238104Sdes		 * sure the page isn't marked NOSYNC.  Applications sharing
912238104Sdes		 * data should use the same flags to avoid ping ponging.
913238104Sdes		 */
914238104Sdes		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
915238104Sdes			if (fs.m->dirty == 0)
916238104Sdes				fs.m->oflags |= VPO_NOSYNC;
917238104Sdes		} else {
918238104Sdes			fs.m->oflags &= ~VPO_NOSYNC;
919238104Sdes		}
920238104Sdes
921238104Sdes		/*
922238104Sdes		 * If the fault is a write, we know that this page is being
923238104Sdes		 * written NOW so dirty it explicitly to save on
924238104Sdes		 * pmap_is_modified() calls later.
925238104Sdes		 *
926238104Sdes		 * Also tell the backing pager, if any, that it should remove
927238104Sdes		 * any swap backing since the page is now dirty.
928238104Sdes		 */
929238104Sdes		if (((fault_type & VM_PROT_WRITE) != 0 &&
930238104Sdes		    (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
931238104Sdes		    (fault_flags & VM_FAULT_DIRTY) != 0) {
932238104Sdes			vm_page_dirty(fs.m);
933238104Sdes			vm_pager_page_unswapped(fs.m);
934238104Sdes		}
935238104Sdes	}
936238104Sdes
937238104Sdes	vm_page_assert_xbusied(fs.m);
938238104Sdes
939238104Sdes	/*
940238104Sdes	 * Page must be completely valid or it is not fit to
941238104Sdes	 * map into user space.  vm_pager_get_pages() ensures this.
942238104Sdes	 */
943238104Sdes	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
944238104Sdes	    ("vm_fault: page %p partially invalid", fs.m));
945238104Sdes	VM_OBJECT_WUNLOCK(fs.object);
946238104Sdes
947238104Sdes	/*
948238104Sdes	 * Put this page into the physical map.  We had to do the unlock above
949238104Sdes	 * because pmap_enter() may sleep.  We don't put the page
950238104Sdes	 * back on the active queue until later so that the pageout daemon
951238104Sdes	 * won't find it (yet).
952238104Sdes	 */
953238104Sdes	pmap_enter(fs.map->pmap, vaddr, fs.m, prot,
954238104Sdes	    fault_type | (wired ? PMAP_ENTER_WIRED : 0), 0);
955238104Sdes	if (faultcount != 1 && (fault_flags & VM_FAULT_CHANGE_WIRING) == 0 &&
956238104Sdes	    wired == 0)
957238104Sdes		vm_fault_prefault(&fs, vaddr, faultcount, reqpage);
958238104Sdes	VM_OBJECT_WLOCK(fs.object);
959238104Sdes	vm_page_lock(fs.m);
960238104Sdes
961238104Sdes	/*
962238104Sdes	 * If the page is not wired down, then put it where the pageout daemon
963238104Sdes	 * can find it.
964238104Sdes	 */
965238104Sdes	if (fault_flags & VM_FAULT_CHANGE_WIRING) {
966238104Sdes		if (wired)
967238104Sdes			vm_page_wire(fs.m);
968238104Sdes		else
969238104Sdes			vm_page_unwire(fs.m, 1);
970238104Sdes	} else
971238104Sdes		vm_page_activate(fs.m);
972238104Sdes	if (m_hold != NULL) {
973238104Sdes		*m_hold = fs.m;
974238104Sdes		vm_page_hold(fs.m);
975238104Sdes	}
976238104Sdes	vm_page_unlock(fs.m);
977238104Sdes	vm_page_xunbusy(fs.m);
978238104Sdes
979238104Sdes	/*
980238104Sdes	 * Unlock everything, and return
981238104Sdes	 */
982238104Sdes	unlock_and_deallocate(&fs);
983238104Sdes	if (hardfault) {
984238104Sdes		PCPU_INC(cnt.v_io_faults);
985238104Sdes		curthread->td_ru.ru_majflt++;
986238104Sdes	} else
987238104Sdes		curthread->td_ru.ru_minflt++;
988238104Sdes
989238104Sdes	return (KERN_SUCCESS);
990238104Sdes}
991238104Sdes
992238104Sdes/*
993238104Sdes * Speed up the reclamation of up to "distance" pages that precede the
994238104Sdes * faulting pindex within the first object of the shadow chain.
995238104Sdes */
996238104Sdesstatic void
997238104Sdesvm_fault_cache_behind(const struct faultstate *fs, int distance)
998238104Sdes{
999238104Sdes	vm_object_t first_object, object;
1000238104Sdes	vm_page_t m, m_prev;
1001238104Sdes	vm_pindex_t pindex;
1002238104Sdes
1003238104Sdes	object = fs->object;
1004238104Sdes	VM_OBJECT_ASSERT_WLOCKED(object);
1005238104Sdes	first_object = fs->first_object;
1006238104Sdes	if (first_object != object) {
1007238104Sdes		if (!VM_OBJECT_TRYWLOCK(first_object)) {
1008238104Sdes			VM_OBJECT_WUNLOCK(object);
1009238104Sdes			VM_OBJECT_WLOCK(first_object);
1010238104Sdes			VM_OBJECT_WLOCK(object);
1011238104Sdes		}
1012238104Sdes	}
1013238104Sdes	/* Neither fictitious nor unmanaged pages can be cached. */
1014238104Sdes	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
1015238104Sdes		if (fs->first_pindex < distance)
1016238104Sdes			pindex = 0;
1017238104Sdes		else
1018238104Sdes			pindex = fs->first_pindex - distance;
1019238104Sdes		if (pindex < OFF_TO_IDX(fs->entry->offset))
1020238104Sdes			pindex = OFF_TO_IDX(fs->entry->offset);
1021238104Sdes		m = first_object != object ? fs->first_m : fs->m;
1022238104Sdes		vm_page_assert_xbusied(m);
1023238104Sdes		m_prev = vm_page_prev(m);
1024238104Sdes		while ((m = m_prev) != NULL && m->pindex >= pindex &&
1025238104Sdes		    m->valid == VM_PAGE_BITS_ALL) {
1026238104Sdes			m_prev = vm_page_prev(m);
1027238104Sdes			if (vm_page_busied(m))
1028238104Sdes				continue;
1029238104Sdes			vm_page_lock(m);
1030238104Sdes			if (m->hold_count == 0 && m->wire_count == 0) {
1031238104Sdes				pmap_remove_all(m);
1032238104Sdes				vm_page_aflag_clear(m, PGA_REFERENCED);
1033238104Sdes				if (m->dirty != 0)
1034238104Sdes					vm_page_deactivate(m);
1035238104Sdes				else
1036238104Sdes					vm_page_cache(m);
1037238104Sdes			}
1038238104Sdes			vm_page_unlock(m);
1039238104Sdes		}
1040238104Sdes	}
1041238104Sdes	if (first_object != object)
1042238104Sdes		VM_OBJECT_WUNLOCK(first_object);
1043238104Sdes}
1044238104Sdes
1045238104Sdes/*
1046238104Sdes * vm_fault_prefault provides a quick way of clustering
1047238104Sdes * pagefaults into a processes address space.  It is a "cousin"
1048238104Sdes * of vm_map_pmap_enter, except it runs at page fault time instead
1049238104Sdes * of mmap time.
1050238104Sdes */
1051238104Sdesstatic void
1052238104Sdesvm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
1053238104Sdes    int faultcount, int reqpage)
1054238104Sdes{
1055238104Sdes	pmap_t pmap;
1056238104Sdes	vm_map_entry_t entry;
1057238104Sdes	vm_object_t backing_object, lobject;
1058238104Sdes	vm_offset_t addr, starta;
1059238104Sdes	vm_pindex_t pindex;
1060238104Sdes	vm_page_t m;
1061238104Sdes	int backward, forward, i;
1062238104Sdes
1063238104Sdes	pmap = fs->map->pmap;
1064238104Sdes	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1065238104Sdes		return;
1066238104Sdes
1067238104Sdes	if (faultcount > 0) {
1068238104Sdes		backward = reqpage;
1069238104Sdes		forward = faultcount - reqpage - 1;
1070238104Sdes	} else {
1071238104Sdes		backward = PFBAK;
1072238104Sdes		forward = PFFOR;
1073238104Sdes	}
1074238104Sdes	entry = fs->entry;
1075238104Sdes
1076238104Sdes	starta = addra - backward * PAGE_SIZE;
1077238104Sdes	if (starta < entry->start) {
1078238104Sdes		starta = entry->start;
1079238104Sdes	} else if (starta > addra) {
1080238104Sdes		starta = 0;
1081238104Sdes	}
1082238104Sdes
1083238104Sdes	/*
1084238104Sdes	 * Generate the sequence of virtual addresses that are candidates for
1085238104Sdes	 * prefaulting in an outward spiral from the faulting virtual address,
1086238104Sdes	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
1087238104Sdes	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1088238104Sdes	 * If the candidate address doesn't have a backing physical page, then
1089238104Sdes	 * the loop immediately terminates.
1090238104Sdes	 */
1091238104Sdes	for (i = 0; i < 2 * imax(backward, forward); i++) {
1092238104Sdes		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1093238104Sdes		    PAGE_SIZE);
1094238104Sdes		if (addr > addra + forward * PAGE_SIZE)
1095238104Sdes			addr = 0;
1096238104Sdes
1097238104Sdes		if (addr < starta || addr >= entry->end)
1098238104Sdes			continue;
1099238104Sdes
1100238104Sdes		if (!pmap_is_prefaultable(pmap, addr))
1101238104Sdes			continue;
1102238104Sdes
1103238104Sdes		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1104238104Sdes		lobject = entry->object.vm_object;
1105238104Sdes		VM_OBJECT_RLOCK(lobject);
1106238104Sdes		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1107238104Sdes		    lobject->type == OBJT_DEFAULT &&
1108238104Sdes		    (backing_object = lobject->backing_object) != NULL) {
1109238104Sdes			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1110238104Sdes			    0, ("vm_fault_prefault: unaligned object offset"));
1111238104Sdes			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1112238104Sdes			VM_OBJECT_RLOCK(backing_object);
1113238104Sdes			VM_OBJECT_RUNLOCK(lobject);
1114238104Sdes			lobject = backing_object;
1115238104Sdes		}
1116238104Sdes		if (m == NULL) {
1117238104Sdes			VM_OBJECT_RUNLOCK(lobject);
1118238104Sdes			break;
1119238104Sdes		}
1120238104Sdes		if (m->valid == VM_PAGE_BITS_ALL &&
1121238104Sdes		    (m->flags & PG_FICTITIOUS) == 0)
1122238104Sdes			pmap_enter_quick(pmap, addr, m, entry->protection);
1123238104Sdes		VM_OBJECT_RUNLOCK(lobject);
1124238104Sdes	}
1125238104Sdes}
1126238104Sdes
1127238104Sdes/*
1128238104Sdes * Hold each of the physical pages that are mapped by the specified range of
1129238104Sdes * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1130238104Sdes * and allow the specified types of access, "prot".  If all of the implied
1131238104Sdes * pages are successfully held, then the number of held pages is returned
1132238104Sdes * together with pointers to those pages in the array "ma".  However, if any
1133238104Sdes * of the pages cannot be held, -1 is returned.
1134238104Sdes */
1135238104Sdesint
1136238104Sdesvm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1137238104Sdes    vm_prot_t prot, vm_page_t *ma, int max_count)
1138238104Sdes{
1139238104Sdes	vm_offset_t end, va;
1140238104Sdes	vm_page_t *mp;
1141238104Sdes	int count;
1142238104Sdes	boolean_t pmap_failed;
1143238104Sdes
1144238104Sdes	if (len == 0)
1145238104Sdes		return (0);
1146238104Sdes	end = round_page(addr + len);
1147238104Sdes	addr = trunc_page(addr);
1148238104Sdes
1149238104Sdes	/*
1150238104Sdes	 * Check for illegal addresses.
1151238104Sdes	 */
1152238104Sdes	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1153238104Sdes		return (-1);
1154238104Sdes
1155238104Sdes	if (atop(end - addr) > max_count)
1156238104Sdes		panic("vm_fault_quick_hold_pages: count > max_count");
1157238104Sdes	count = atop(end - addr);
1158238104Sdes
1159238104Sdes	/*
1160238104Sdes	 * Most likely, the physical pages are resident in the pmap, so it is
1161238104Sdes	 * faster to try pmap_extract_and_hold() first.
1162238104Sdes	 */
1163238104Sdes	pmap_failed = FALSE;
1164238104Sdes	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1165238104Sdes		*mp = pmap_extract_and_hold(map->pmap, va, prot);
1166238104Sdes		if (*mp == NULL)
1167238104Sdes			pmap_failed = TRUE;
1168238104Sdes		else if ((prot & VM_PROT_WRITE) != 0 &&
1169238104Sdes		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
1170238104Sdes			/*
1171238104Sdes			 * Explicitly dirty the physical page.  Otherwise, the
1172238104Sdes			 * caller's changes may go unnoticed because they are
1173238104Sdes			 * performed through an unmanaged mapping or by a DMA
1174238104Sdes			 * operation.
1175238104Sdes			 *
1176238104Sdes			 * The object lock is not held here.
1177238104Sdes			 * See vm_page_clear_dirty_mask().
1178238104Sdes			 */
1179238104Sdes			vm_page_dirty(*mp);
1180238104Sdes		}
1181238104Sdes	}
1182238104Sdes	if (pmap_failed) {
1183238104Sdes		/*
1184238104Sdes		 * One or more pages could not be held by the pmap.  Either no
1185238104Sdes		 * page was mapped at the specified virtual address or that
1186238104Sdes		 * mapping had insufficient permissions.  Attempt to fault in
1187238104Sdes		 * and hold these pages.
1188238104Sdes		 */
1189238104Sdes		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1190238104Sdes			if (*mp == NULL && vm_fault_hold(map, va, prot,
1191238104Sdes			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1192238104Sdes				goto error;
1193238104Sdes	}
1194238104Sdes	return (count);
1195238104Sdeserror:
1196238104Sdes	for (mp = ma; mp < ma + count; mp++)
1197238104Sdes		if (*mp != NULL) {
1198238104Sdes			vm_page_lock(*mp);
1199238104Sdes			vm_page_unhold(*mp);
1200238104Sdes			vm_page_unlock(*mp);
1201238104Sdes		}
1202238104Sdes	return (-1);
1203238104Sdes}
1204238104Sdes
1205238104Sdes/*
1206238104Sdes *	vm_fault_wire:
1207238104Sdes *
1208238104Sdes *	Wire down a range of virtual addresses in a map.
1209238104Sdes */
1210238104Sdesint
1211238104Sdesvm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1212238104Sdes    boolean_t fictitious)
1213238104Sdes{
1214238104Sdes	vm_offset_t va;
1215238104Sdes	int rv;
1216238104Sdes
1217238104Sdes	/*
1218238104Sdes	 * We simulate a fault to get the page and enter it in the physical
1219269257Sdes	 * map.  For user wiring, we only ask for read access on currently
1220238104Sdes	 * read-only sections.
1221238104Sdes	 */
1222238104Sdes	for (va = start; va < end; va += PAGE_SIZE) {
1223238104Sdes		rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
1224238104Sdes		if (rv) {
1225238104Sdes			if (va != start)
1226238104Sdes				vm_fault_unwire(map, start, va, fictitious);
1227238104Sdes			return (rv);
1228238104Sdes		}
1229238104Sdes	}
1230238104Sdes	return (KERN_SUCCESS);
1231238104Sdes}
1232238104Sdes
1233238104Sdes/*
1234238104Sdes *	vm_fault_unwire:
1235238104Sdes *
1236238104Sdes *	Unwire a range of virtual addresses in a map.
1237238104Sdes */
1238238104Sdesvoid
1239238104Sdesvm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1240238104Sdes    boolean_t fictitious)
1241238104Sdes{
1242238104Sdes	vm_paddr_t pa;
1243238104Sdes	vm_offset_t va;
1244238104Sdes	vm_page_t m;
1245238104Sdes	pmap_t pmap;
1246238104Sdes
1247238104Sdes	pmap = vm_map_pmap(map);
1248238104Sdes
1249238104Sdes	/*
1250238104Sdes	 * Since the pages are wired down, we must be able to get their
1251238104Sdes	 * mappings from the physical map system.
1252238104Sdes	 */
1253238104Sdes	for (va = start; va < end; va += PAGE_SIZE) {
1254238104Sdes		pa = pmap_extract(pmap, va);
1255238104Sdes		if (pa != 0) {
1256238104Sdes			pmap_change_wiring(pmap, va, FALSE);
1257238104Sdes			if (!fictitious) {
1258238104Sdes				m = PHYS_TO_VM_PAGE(pa);
1259238104Sdes				vm_page_lock(m);
1260238104Sdes				vm_page_unwire(m, TRUE);
1261238104Sdes				vm_page_unlock(m);
1262238104Sdes			}
1263238104Sdes		}
1264238104Sdes	}
1265238104Sdes}
1266238104Sdes
1267238104Sdes/*
1268238104Sdes *	Routine:
1269238104Sdes *		vm_fault_copy_entry
1270238104Sdes *	Function:
1271238104Sdes *		Create new shadow object backing dst_entry with private copy of
1272238104Sdes *		all underlying pages. When src_entry is equal to dst_entry,
1273238104Sdes *		function implements COW for wired-down map entry. Otherwise,
1274238104Sdes *		it forks wired entry into dst_map.
1275238104Sdes *
1276238104Sdes *	In/out conditions:
1277238104Sdes *		The source and destination maps must be locked for write.
1278238104Sdes *		The source map entry must be wired down (or be a sharing map
1279238104Sdes *		entry corresponding to a main map entry that is wired down).
1280238104Sdes */
1281238104Sdesvoid
1282238104Sdesvm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1283238104Sdes    vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1284238104Sdes    vm_ooffset_t *fork_charge)
1285238104Sdes{
1286238104Sdes	vm_object_t backing_object, dst_object, object, src_object;
1287238104Sdes	vm_pindex_t dst_pindex, pindex, src_pindex;
1288238104Sdes	vm_prot_t access, prot;
1289238104Sdes	vm_offset_t vaddr;
1290238104Sdes	vm_page_t dst_m;
1291238104Sdes	vm_page_t src_m;
1292238104Sdes	boolean_t upgrade;
1293238104Sdes
1294238104Sdes#ifdef	lint
1295238104Sdes	src_map++;
1296238104Sdes#endif	/* lint */
1297238104Sdes
1298238104Sdes	upgrade = src_entry == dst_entry;
1299238104Sdes	access = prot = dst_entry->protection;
1300238104Sdes
1301238104Sdes	src_object = src_entry->object.vm_object;
1302238104Sdes	src_pindex = OFF_TO_IDX(src_entry->offset);
1303238104Sdes
1304238104Sdes	if (upgrade && (dst_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
1305238104Sdes		dst_object = src_object;
1306238104Sdes		vm_object_reference(dst_object);
1307238104Sdes	} else {
1308238104Sdes		/*
1309238104Sdes		 * Create the top-level object for the destination entry. (Doesn't
1310238104Sdes		 * actually shadow anything - we copy the pages directly.)
1311238104Sdes		 */
1312238104Sdes		dst_object = vm_object_allocate(OBJT_DEFAULT,
1313238104Sdes		    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1314238104Sdes#if VM_NRESERVLEVEL > 0
1315238104Sdes		dst_object->flags |= OBJ_COLORED;
1316238104Sdes		dst_object->pg_color = atop(dst_entry->start);
1317238104Sdes#endif
1318238104Sdes	}
1319238104Sdes
1320238104Sdes	VM_OBJECT_WLOCK(dst_object);
1321238104Sdes	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1322238104Sdes	    ("vm_fault_copy_entry: vm_object not NULL"));
1323238104Sdes	if (src_object != dst_object) {
1324238104Sdes		dst_entry->object.vm_object = dst_object;
1325238104Sdes		dst_entry->offset = 0;
1326238104Sdes		dst_object->charge = dst_entry->end - dst_entry->start;
1327238104Sdes	}
1328238104Sdes	if (fork_charge != NULL) {
1329238104Sdes		KASSERT(dst_entry->cred == NULL,
1330238104Sdes		    ("vm_fault_copy_entry: leaked swp charge"));
1331238104Sdes		dst_object->cred = curthread->td_ucred;
1332238104Sdes		crhold(dst_object->cred);
1333238104Sdes		*fork_charge += dst_object->charge;
1334238104Sdes	} else if (dst_object->cred == NULL) {
1335238104Sdes		KASSERT(dst_entry->cred != NULL, ("no cred for entry %p",
1336238104Sdes		    dst_entry));
1337238104Sdes		dst_object->cred = dst_entry->cred;
1338238104Sdes		dst_entry->cred = NULL;
1339238104Sdes	}
1340238104Sdes
1341238104Sdes	/*
1342238104Sdes	 * If not an upgrade, then enter the mappings in the pmap as
1343238104Sdes	 * read and/or execute accesses.  Otherwise, enter them as
1344238104Sdes	 * write accesses.
1345238104Sdes	 *
1346238104Sdes	 * A writeable large page mapping is only created if all of
1347238104Sdes	 * the constituent small page mappings are modified. Marking
1348238104Sdes	 * PTEs as modified on inception allows promotion to happen
1349238104Sdes	 * without taking potentially large number of soft faults.
1350238104Sdes	 */
1351238104Sdes	if (!upgrade)
1352238104Sdes		access &= ~VM_PROT_WRITE;
1353238104Sdes
1354238104Sdes	/*
1355238104Sdes	 * Loop through all of the virtual pages within the entry's
1356238104Sdes	 * range, copying each page from the source object to the
1357238104Sdes	 * destination object.  Since the source is wired, those pages
1358238104Sdes	 * must exist.  In contrast, the destination is pageable.
1359238104Sdes	 * Since the destination object does share any backing storage
1360238104Sdes	 * with the source object, all of its pages must be dirtied,
1361238104Sdes	 * regardless of whether they can be written.
1362238104Sdes	 */
1363238104Sdes	for (vaddr = dst_entry->start, dst_pindex = 0;
1364238104Sdes	    vaddr < dst_entry->end;
1365238104Sdes	    vaddr += PAGE_SIZE, dst_pindex++) {
1366238104Sdesagain:
1367238104Sdes		/*
1368238104Sdes		 * Find the page in the source object, and copy it in.
1369238104Sdes		 * Because the source is wired down, the page will be
1370238104Sdes		 * in memory.
1371238104Sdes		 */
1372238104Sdes		if (src_object != dst_object)
1373238104Sdes			VM_OBJECT_RLOCK(src_object);
1374238104Sdes		object = src_object;
1375238104Sdes		pindex = src_pindex + dst_pindex;
1376238104Sdes		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1377238104Sdes		    (backing_object = object->backing_object) != NULL) {
1378238104Sdes			/*
1379			 * Unless the source mapping is read-only or
1380			 * it is presently being upgraded from
1381			 * read-only, the first object in the shadow
1382			 * chain should provide all of the pages.  In
1383			 * other words, this loop body should never be
1384			 * executed when the source mapping is already
1385			 * read/write.
1386			 */
1387			KASSERT((src_entry->protection & VM_PROT_WRITE) == 0 ||
1388			    upgrade,
1389			    ("vm_fault_copy_entry: main object missing page"));
1390
1391			VM_OBJECT_RLOCK(backing_object);
1392			pindex += OFF_TO_IDX(object->backing_object_offset);
1393			if (object != dst_object)
1394				VM_OBJECT_RUNLOCK(object);
1395			object = backing_object;
1396		}
1397		KASSERT(src_m != NULL, ("vm_fault_copy_entry: page missing"));
1398
1399		if (object != dst_object) {
1400			/*
1401			 * Allocate a page in the destination object.
1402			 */
1403			dst_m = vm_page_alloc(dst_object, (src_object ==
1404			    dst_object ? src_pindex : 0) + dst_pindex,
1405			    VM_ALLOC_NORMAL);
1406			if (dst_m == NULL) {
1407				VM_OBJECT_WUNLOCK(dst_object);
1408				VM_OBJECT_RUNLOCK(object);
1409				VM_WAIT;
1410				VM_OBJECT_WLOCK(dst_object);
1411				goto again;
1412			}
1413			pmap_copy_page(src_m, dst_m);
1414			VM_OBJECT_RUNLOCK(object);
1415			dst_m->valid = VM_PAGE_BITS_ALL;
1416			dst_m->dirty = VM_PAGE_BITS_ALL;
1417		} else {
1418			dst_m = src_m;
1419			if (vm_page_sleep_if_busy(dst_m, "fltupg"))
1420				goto again;
1421			vm_page_xbusy(dst_m);
1422			KASSERT(dst_m->valid == VM_PAGE_BITS_ALL,
1423			    ("invalid dst page %p", dst_m));
1424		}
1425		VM_OBJECT_WUNLOCK(dst_object);
1426
1427		/*
1428		 * Enter it in the pmap. If a wired, copy-on-write
1429		 * mapping is being replaced by a write-enabled
1430		 * mapping, then wire that new mapping.
1431		 */
1432		pmap_enter(dst_map->pmap, vaddr, dst_m, prot,
1433		    access | (upgrade ? PMAP_ENTER_WIRED : 0), 0);
1434
1435		/*
1436		 * Mark it no longer busy, and put it on the active list.
1437		 */
1438		VM_OBJECT_WLOCK(dst_object);
1439
1440		if (upgrade) {
1441			if (src_m != dst_m) {
1442				vm_page_lock(src_m);
1443				vm_page_unwire(src_m, 0);
1444				vm_page_unlock(src_m);
1445				vm_page_lock(dst_m);
1446				vm_page_wire(dst_m);
1447				vm_page_unlock(dst_m);
1448			} else {
1449				KASSERT(dst_m->wire_count > 0,
1450				    ("dst_m %p is not wired", dst_m));
1451			}
1452		} else {
1453			vm_page_lock(dst_m);
1454			vm_page_activate(dst_m);
1455			vm_page_unlock(dst_m);
1456		}
1457		vm_page_xunbusy(dst_m);
1458	}
1459	VM_OBJECT_WUNLOCK(dst_object);
1460	if (upgrade) {
1461		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1462		vm_object_deallocate(src_object);
1463	}
1464}
1465
1466
1467/*
1468 * This routine checks around the requested page for other pages that
1469 * might be able to be faulted in.  This routine brackets the viable
1470 * pages for the pages to be paged in.
1471 *
1472 * Inputs:
1473 *	m, rbehind, rahead
1474 *
1475 * Outputs:
1476 *  marray (array of vm_page_t), reqpage (index of requested page)
1477 *
1478 * Return value:
1479 *  number of pages in marray
1480 */
1481static int
1482vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1483	vm_page_t m;
1484	int rbehind;
1485	int rahead;
1486	vm_page_t *marray;
1487	int *reqpage;
1488{
1489	int i,j;
1490	vm_object_t object;
1491	vm_pindex_t pindex, startpindex, endpindex, tpindex;
1492	vm_page_t rtm;
1493	int cbehind, cahead;
1494
1495	VM_OBJECT_ASSERT_WLOCKED(m->object);
1496
1497	object = m->object;
1498	pindex = m->pindex;
1499	cbehind = cahead = 0;
1500
1501	/*
1502	 * if the requested page is not available, then give up now
1503	 */
1504	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1505		return 0;
1506	}
1507
1508	if ((cbehind == 0) && (cahead == 0)) {
1509		*reqpage = 0;
1510		marray[0] = m;
1511		return 1;
1512	}
1513
1514	if (rahead > cahead) {
1515		rahead = cahead;
1516	}
1517
1518	if (rbehind > cbehind) {
1519		rbehind = cbehind;
1520	}
1521
1522	/*
1523	 * scan backward for the read behind pages -- in memory
1524	 */
1525	if (pindex > 0) {
1526		if (rbehind > pindex) {
1527			rbehind = pindex;
1528			startpindex = 0;
1529		} else {
1530			startpindex = pindex - rbehind;
1531		}
1532
1533		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1534		    rtm->pindex >= startpindex)
1535			startpindex = rtm->pindex + 1;
1536
1537		/* tpindex is unsigned; beware of numeric underflow. */
1538		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1539		    tpindex < pindex; i++, tpindex--) {
1540
1541			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1542			    VM_ALLOC_IFNOTCACHED);
1543			if (rtm == NULL) {
1544				/*
1545				 * Shift the allocated pages to the
1546				 * beginning of the array.
1547				 */
1548				for (j = 0; j < i; j++) {
1549					marray[j] = marray[j + tpindex + 1 -
1550					    startpindex];
1551				}
1552				break;
1553			}
1554
1555			marray[tpindex - startpindex] = rtm;
1556		}
1557	} else {
1558		startpindex = 0;
1559		i = 0;
1560	}
1561
1562	marray[i] = m;
1563	/* page offset of the required page */
1564	*reqpage = i;
1565
1566	tpindex = pindex + 1;
1567	i++;
1568
1569	/*
1570	 * scan forward for the read ahead pages
1571	 */
1572	endpindex = tpindex + rahead;
1573	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1574		endpindex = rtm->pindex;
1575	if (endpindex > object->size)
1576		endpindex = object->size;
1577
1578	for (; tpindex < endpindex; i++, tpindex++) {
1579
1580		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1581		    VM_ALLOC_IFNOTCACHED);
1582		if (rtm == NULL) {
1583			break;
1584		}
1585
1586		marray[i] = rtm;
1587	}
1588
1589	/* return number of pages */
1590	return i;
1591}
1592
1593/*
1594 * Block entry into the machine-independent layer's page fault handler by
1595 * the calling thread.  Subsequent calls to vm_fault() by that thread will
1596 * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
1597 * spurious page faults.
1598 */
1599int
1600vm_fault_disable_pagefaults(void)
1601{
1602
1603	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1604}
1605
1606void
1607vm_fault_enable_pagefaults(int save)
1608{
1609
1610	curthread_pflags_restore(save);
1611}
1612