xref: /freebsd/sys/vm/vm_fault.c (revision 63281952f056d47eea92ca1a83d9f2a9f9f6c6cc)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  *	Page fault handling module.
72df8bae1dSRodney W. Grimes  */
73874651b1SDavid E. O'Brien 
74874651b1SDavid E. O'Brien #include <sys/cdefs.h>
75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
76874651b1SDavid E. O'Brien 
7735818d2eSJohn Baldwin #include "opt_ktrace.h"
78f8a47341SAlan Cox #include "opt_vm.h"
79f8a47341SAlan Cox 
80df8bae1dSRodney W. Grimes #include <sys/param.h>
81df8bae1dSRodney W. Grimes #include <sys/systm.h>
824edf4a58SJohn Baldwin #include <sys/kernel.h>
83fb919e4dSMark Murray #include <sys/lock.h>
8426f9a767SRodney W. Grimes #include <sys/proc.h>
8526f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8689f6b863SAttilio Rao #include <sys/rwlock.h>
8723955314SAlfred Perlstein #include <sys/sysctl.h>
884edf4a58SJohn Baldwin #include <sys/vmmeter.h>
894edf4a58SJohn Baldwin #include <sys/vnode.h>
9035818d2eSJohn Baldwin #ifdef KTRACE
9135818d2eSJohn Baldwin #include <sys/ktrace.h>
9235818d2eSJohn Baldwin #endif
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes #include <vm/vm.h>
95efeaf95aSDavid Greenman #include <vm/vm_param.h>
96efeaf95aSDavid Greenman #include <vm/pmap.h>
97efeaf95aSDavid Greenman #include <vm/vm_map.h>
98efeaf95aSDavid Greenman #include <vm/vm_object.h>
99df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
100df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
101a83c285cSDavid Greenman #include <vm/vm_kern.h>
10224a1cce3SDavid Greenman #include <vm/vm_pager.h>
103efeaf95aSDavid Greenman #include <vm/vm_extern.h>
104df8bae1dSRodney W. Grimes 
105566526a9SAlan Cox #define PFBAK 4
106566526a9SAlan Cox #define PFFOR 4
107566526a9SAlan Cox 
10811caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
10926f9a767SRodney W. Grimes 
11013458803SAlan Cox #define	VM_FAULT_READ_BEHIND	8
11113458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
11213458803SAlan Cox #define	VM_FAULT_NINCR		(VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
11313458803SAlan Cox #define	VM_FAULT_SUM		(VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
11413458803SAlan Cox #define	VM_FAULT_CACHE_BEHIND	(VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
11526f9a767SRodney W. Grimes 
1164866e085SJohn Dyson struct faultstate {
1174866e085SJohn Dyson 	vm_page_t m;
1184866e085SJohn Dyson 	vm_object_t object;
1194866e085SJohn Dyson 	vm_pindex_t pindex;
1204866e085SJohn Dyson 	vm_page_t first_m;
1214866e085SJohn Dyson 	vm_object_t	first_object;
1224866e085SJohn Dyson 	vm_pindex_t first_pindex;
1234866e085SJohn Dyson 	vm_map_t map;
1244866e085SJohn Dyson 	vm_map_entry_t entry;
12525adb370SBrian Feldman 	int lookup_still_valid;
1264866e085SJohn Dyson 	struct vnode *vp;
1274866e085SJohn Dyson };
1284866e085SJohn Dyson 
12913458803SAlan Cox static void vm_fault_cache_behind(const struct faultstate *fs, int distance);
130*63281952SAlan Cox static void vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
131*63281952SAlan Cox 	    int faultcount, int reqpage);
13213458803SAlan Cox 
13362a59e8fSWarner Losh static inline void
1344866e085SJohn Dyson release_page(struct faultstate *fs)
1354866e085SJohn Dyson {
1360d0be82aSKonstantin Belousov 
137c7aebda8SAttilio Rao 	vm_page_xunbusy(fs->m);
1382965a453SKip Macy 	vm_page_lock(fs->m);
1394866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1402965a453SKip Macy 	vm_page_unlock(fs->m);
1414866e085SJohn Dyson 	fs->m = NULL;
1424866e085SJohn Dyson }
1434866e085SJohn Dyson 
14462a59e8fSWarner Losh static inline void
1454866e085SJohn Dyson unlock_map(struct faultstate *fs)
1464866e085SJohn Dyson {
1470d0be82aSKonstantin Belousov 
14825adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1494866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15025adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1514866e085SJohn Dyson 	}
1524866e085SJohn Dyson }
1534866e085SJohn Dyson 
1544866e085SJohn Dyson static void
155a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1564866e085SJohn Dyson {
157f29ba63eSAlan Cox 
1584866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
15989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1604866e085SJohn Dyson 	if (fs->object != fs->first_object) {
16189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1622965a453SKip Macy 		vm_page_lock(fs->first_m);
1634866e085SJohn Dyson 		vm_page_free(fs->first_m);
1642965a453SKip Macy 		vm_page_unlock(fs->first_m);
1654866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
16689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1674866e085SJohn Dyson 		fs->first_m = NULL;
1684866e085SJohn Dyson 	}
1694866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1704866e085SJohn Dyson 	unlock_map(fs);
1714866e085SJohn Dyson 	if (fs->vp != NULL) {
1720cddd8f0SMatthew Dillon 		vput(fs->vp);
1734866e085SJohn Dyson 		fs->vp = NULL;
1744866e085SJohn Dyson 	}
1754866e085SJohn Dyson }
1764866e085SJohn Dyson 
177df8bae1dSRodney W. Grimes /*
17840360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
17940360b1bSMatthew Dillon  *	      current object *might* contain the page.
18040360b1bSMatthew Dillon  *
18140360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18240360b1bSMatthew Dillon  */
18340360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
1842db65ab4SAlan Cox 			((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
18540360b1bSMatthew Dillon 
18640360b1bSMatthew Dillon /*
187df8bae1dSRodney W. Grimes  *	vm_fault:
188df8bae1dSRodney W. Grimes  *
189956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
190df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
191df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
192df8bae1dSRodney W. Grimes  *	associated physical map.
193df8bae1dSRodney W. Grimes  *
194df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
195df8bae1dSRodney W. Grimes  *	proper page address.
196df8bae1dSRodney W. Grimes  *
197df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
198df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
199df8bae1dSRodney W. Grimes  *
200df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2010cddd8f0SMatthew Dillon  *	Caller may hold no locks.
202df8bae1dSRodney W. Grimes  */
203df8bae1dSRodney W. Grimes int
20423955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
20523955314SAlfred Perlstein     int fault_flags)
20623955314SAlfred Perlstein {
20735818d2eSJohn Baldwin 	struct thread *td;
20835818d2eSJohn Baldwin 	int result;
209acd11c74SAlan Cox 
21035818d2eSJohn Baldwin 	td = curthread;
21135818d2eSJohn Baldwin 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
2122801687dSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
21335818d2eSJohn Baldwin #ifdef KTRACE
21435818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
21535818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
21635818d2eSJohn Baldwin #endif
217be996836SAttilio Rao 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
218be996836SAttilio Rao 	    NULL);
21935818d2eSJohn Baldwin #ifdef KTRACE
22035818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
22135818d2eSJohn Baldwin 		ktrfaultend(result);
22235818d2eSJohn Baldwin #endif
22335818d2eSJohn Baldwin 	return (result);
224acd11c74SAlan Cox }
225acd11c74SAlan Cox 
226acd11c74SAlan Cox int
227be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
228acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
229acd11c74SAlan Cox {
230df8bae1dSRodney W. Grimes 	vm_prot_t prot;
23113458803SAlan Cox 	long ahead, behind;
23213458803SAlan Cox 	int alloc_req, era, faultcount, nera, reqpage, result;
23313458803SAlan Cox 	boolean_t growstack, is_first_object_locked, wired;
2342d8acc0fSJohn Dyson 	int map_generation;
235df8bae1dSRodney W. Grimes 	vm_object_t next_object;
23613458803SAlan Cox 	vm_page_t marray[VM_FAULT_READ_MAX];
2374866e085SJohn Dyson 	int hardfault;
2384866e085SJohn Dyson 	struct faultstate fs;
239d2bf64c3SKonstantin Belousov 	struct vnode *vp;
240d2bf64c3SKonstantin Belousov 	int locked, error;
241df8bae1dSRodney W. Grimes 
2424866e085SJohn Dyson 	hardfault = 0;
2436139043bSAlan Cox 	growstack = TRUE;
24467596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
245d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
24613458803SAlan Cox 	faultcount = reqpage = 0;
247df8bae1dSRodney W. Grimes 
248df8bae1dSRodney W. Grimes RetryFault:;
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	/*
2510d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2520d94caffSDavid Greenman 	 * search.
253df8bae1dSRodney W. Grimes 	 */
25440360b1bSMatthew Dillon 	fs.map = map;
25592de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
25692de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
25792de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
2586139043bSAlan Cox 		if (growstack && result == KERN_INVALID_ADDRESS &&
2592db65ab4SAlan Cox 		    map != kernel_map) {
2606139043bSAlan Cox 			result = vm_map_growstack(curproc, vaddr);
261a976eb5eSAlan Cox 			if (result != KERN_SUCCESS)
2626139043bSAlan Cox 				return (KERN_FAILURE);
2636139043bSAlan Cox 			growstack = FALSE;
2646139043bSAlan Cox 			goto RetryFault;
2656139043bSAlan Cox 		}
26692de35b0SAlan Cox 		return (result);
26709e0c6ccSJohn Dyson 	}
26809e0c6ccSJohn Dyson 
2694866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2702d8acc0fSJohn Dyson 
2714866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
27247221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
27392c4c4ebSBruce Evans 		    (u_long)vaddr);
2747aaaa4fdSJohn Dyson 	}
2757aaaa4fdSJohn Dyson 
2764f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
2774f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
2784f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
2794f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
2804f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
2814f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
2824f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2834f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
2844f9c9114SKonstantin Belousov 		} else
2854f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
2864f9c9114SKonstantin Belousov 		goto RetryFault;
2874f9c9114SKonstantin Belousov 	}
2884f9c9114SKonstantin Belousov 
28995e5e988SJohn Dyson 	/*
29095e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
29195e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
29295e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
29395e5e988SJohn Dyson 	 * they will stay around as well.
294fe8e0238SMatthew Dillon 	 *
295fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
296fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
297fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
29895e5e988SJohn Dyson 	 */
29989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.first_object);
300a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
301d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30295e5e988SJohn Dyson 
30325adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
304df8bae1dSRodney W. Grimes 
305df8bae1dSRodney W. Grimes 	if (wired)
306d8778512SAlan Cox 		fault_type = prot | (fault_type & VM_PROT_COPY);
307df8bae1dSRodney W. Grimes 
3084866e085SJohn Dyson 	fs.first_m = NULL;
309df8bae1dSRodney W. Grimes 
310df8bae1dSRodney W. Grimes 	/*
311df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
312df8bae1dSRodney W. Grimes 	 */
3134866e085SJohn Dyson 	fs.object = fs.first_object;
3144866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
315df8bae1dSRodney W. Grimes 	while (TRUE) {
3161c7c3c6aSMatthew Dillon 		/*
3171c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3181c7c3c6aSMatthew Dillon 		 */
3194866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3204866e085SJohn Dyson 			unlock_and_deallocate(&fs);
32147221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32247221757SJohn Dyson 		}
32347221757SJohn Dyson 
3241c7c3c6aSMatthew Dillon 		/*
3251c7c3c6aSMatthew Dillon 		 * See if page is resident
3261c7c3c6aSMatthew Dillon 		 */
3274866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3284866e085SJohn Dyson 		if (fs.m != NULL) {
32998cb733cSKenneth D. Merry 			/*
3301c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
331c7aebda8SAttilio Rao 			 * if the page is either exclusive or shared busy
332c7aebda8SAttilio Rao 			 * because the vm_pager may be using read busy for
333c7aebda8SAttilio Rao 			 * pageouts (and even pageins if it is the vnode
334c7aebda8SAttilio Rao 			 * pager), and we could end up trying to pagein and
335c7aebda8SAttilio Rao 			 * pageout the same page simultaneously.
3361c7c3c6aSMatthew Dillon 			 *
3371c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3381c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3391c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3401c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3411c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
342c7aebda8SAttilio Rao 			 * around with a shared busied page except, perhaps,
3431c7c3c6aSMatthew Dillon 			 * to pmap it.
344df8bae1dSRodney W. Grimes 			 */
345c7aebda8SAttilio Rao 			if (vm_page_busied(fs.m)) {
346b88b6c9dSAlan Cox 				/*
347b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
348b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
349b88b6c9dSAlan Cox 				 * likely to reclaim it.
350b88b6c9dSAlan Cox 				 */
3513407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
352a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
35389f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
354a6e38685SKonstantin Belousov 					    fs.first_object)) {
35589f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
35689f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
35789f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
358a6e38685SKonstantin Belousov 					}
3592965a453SKip Macy 					vm_page_lock(fs.first_m);
360a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3612965a453SKip Macy 					vm_page_unlock(fs.first_m);
362a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
36389f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
364a51b0840SAlan Cox 					fs.first_m = NULL;
365a51b0840SAlan Cox 				}
366a51b0840SAlan Cox 				unlock_map(&fs);
367a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
368a51b0840SAlan Cox 				    fs.pindex)) {
369c7aebda8SAttilio Rao 					vm_page_sleep_if_busy(fs.m, "vmpfw");
370a51b0840SAlan Cox 				}
371a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
37289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
37367596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
3744866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
375df8bae1dSRodney W. Grimes 				goto RetryFault;
376df8bae1dSRodney W. Grimes 			}
3773846a822SKonstantin Belousov 			vm_page_lock(fs.m);
3788d220203SAlan Cox 			vm_page_remque(fs.m);
3792965a453SKip Macy 			vm_page_unlock(fs.m);
3807615edaaSMatthew Dillon 
3811c7c3c6aSMatthew Dillon 			/*
3821c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
3831c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
3841c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
3851c7c3c6aSMatthew Dillon 			 * found the page ).
3861c7c3c6aSMatthew Dillon 			 */
387c7aebda8SAttilio Rao 			vm_page_xbusy(fs.m);
388ff5958e7SAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL)
3890d94caffSDavid Greenman 				goto readrest;
390df8bae1dSRodney W. Grimes 			break;
391df8bae1dSRodney W. Grimes 		}
3921c7c3c6aSMatthew Dillon 
3931c7c3c6aSMatthew Dillon 		/*
39440360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
39540360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
3961c7c3c6aSMatthew Dillon 		 */
39740360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
3984866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
3994866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4005f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4015f55e841SDavid Greenman 			}
40222ba64e8SJohn Dyson 
403df8bae1dSRodney W. Grimes 			/*
4040d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
4053f1c4c4fSKonstantin Belousov 			 *
4063f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
4073f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
4083f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
4093f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
410df8bae1dSRodney W. Grimes 			 */
41140360b1bSMatthew Dillon 			fs.m = NULL;
4123f1c4c4fSKonstantin Belousov 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
413f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
414f8a47341SAlan Cox 				if ((fs.object->flags & OBJ_COLORED) == 0) {
415f8a47341SAlan Cox 					fs.object->flags |= OBJ_COLORED;
416f8a47341SAlan Cox 					fs.object->pg_color = atop(vaddr) -
417f8a47341SAlan Cox 					    fs.pindex;
418f8a47341SAlan Cox 				}
419f8a47341SAlan Cox #endif
4203f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
4213f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
4223f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
4233f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
4243f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
4254866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4263f1c4c4fSKonstantin Belousov 				    alloc_req);
42740360b1bSMatthew Dillon 			}
4284866e085SJohn Dyson 			if (fs.m == NULL) {
4294866e085SJohn Dyson 				unlock_and_deallocate(&fs);
430ef6020d1SMike Silbersack 				VM_WAITPFAULT;
431df8bae1dSRodney W. Grimes 				goto RetryFault;
4320a2e596aSAlan Cox 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
4334ab8ab92SKonstantin Belousov 				break;
434df8bae1dSRodney W. Grimes 		}
43547221757SJohn Dyson 
4360d94caffSDavid Greenman readrest:
4371c7c3c6aSMatthew Dillon 		/*
43840360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
43940360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
44040360b1bSMatthew Dillon 		 * valid.
44140360b1bSMatthew Dillon 		 *
44240360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
44340360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
44440360b1bSMatthew Dillon 		 * at the same time.
4451c7c3c6aSMatthew Dillon 		 */
44640360b1bSMatthew Dillon 		if (TRYPAGER) {
447df8bae1dSRodney W. Grimes 			int rv;
4487f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
449867a482dSJohn Dyson 
4503f1c4c4fSKonstantin Belousov 			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
4513f1c4c4fSKonstantin Belousov 			    P_KILLED(curproc)) {
452867a482dSJohn Dyson 				behind = 0;
45313458803SAlan Cox 				ahead = 0;
45413458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
45513458803SAlan Cox 				behind = 0;
45613458803SAlan Cox 				ahead = atop(fs.entry->end - vaddr) - 1;
45713458803SAlan Cox 				if (ahead > VM_FAULT_READ_AHEAD_MAX)
45813458803SAlan Cox 					ahead = VM_FAULT_READ_AHEAD_MAX;
45913458803SAlan Cox 				if (fs.pindex == fs.entry->next_read)
46013458803SAlan Cox 					vm_fault_cache_behind(&fs,
46113458803SAlan Cox 					    VM_FAULT_READ_MAX);
4622d8acc0fSJohn Dyson 			} else {
46313458803SAlan Cox 				/*
46413458803SAlan Cox 				 * If this is a sequential page fault, then
46513458803SAlan Cox 				 * arithmetically increase the number of pages
46613458803SAlan Cox 				 * in the read-ahead window.  Otherwise, reset
46713458803SAlan Cox 				 * the read-ahead window to its smallest size.
46813458803SAlan Cox 				 */
46913458803SAlan Cox 				behind = atop(vaddr - fs.entry->start);
4702d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4712d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
47213458803SAlan Cox 				ahead = atop(fs.entry->end - vaddr) - 1;
47313458803SAlan Cox 				era = fs.entry->read_ahead;
47413458803SAlan Cox 				if (fs.pindex == fs.entry->next_read) {
47513458803SAlan Cox 					nera = era + behind;
47613458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
47713458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
478867a482dSJohn Dyson 					behind = 0;
47913458803SAlan Cox 					if (ahead > nera)
48013458803SAlan Cox 						ahead = nera;
48113458803SAlan Cox 					if (era == VM_FAULT_READ_AHEAD_MAX)
48213458803SAlan Cox 						vm_fault_cache_behind(&fs,
48313458803SAlan Cox 						    VM_FAULT_CACHE_BEHIND);
48413458803SAlan Cox 				} else if (ahead > VM_FAULT_READ_AHEAD_MIN)
48513458803SAlan Cox 					ahead = VM_FAULT_READ_AHEAD_MIN;
48613458803SAlan Cox 				if (era != ahead)
48713458803SAlan Cox 					fs.entry->read_ahead = ahead;
488867a482dSJohn Dyson 			}
489d2bf64c3SKonstantin Belousov 
490d2bf64c3SKonstantin Belousov 			/*
491d2bf64c3SKonstantin Belousov 			 * Call the pager to retrieve the data, if any, after
492d2bf64c3SKonstantin Belousov 			 * releasing the lock on the map.  We hold a ref on
493c7aebda8SAttilio Rao 			 * fs.object and the pages are exclusive busied.
494d2bf64c3SKonstantin Belousov 			 */
495d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
496d2bf64c3SKonstantin Belousov 
497d2bf64c3SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE) {
498d2bf64c3SKonstantin Belousov 				vp = fs.object->handle;
499d2bf64c3SKonstantin Belousov 				if (vp == fs.vp)
500d2bf64c3SKonstantin Belousov 					goto vnode_locked;
501d2bf64c3SKonstantin Belousov 				else if (fs.vp != NULL) {
502d2bf64c3SKonstantin Belousov 					vput(fs.vp);
503d2bf64c3SKonstantin Belousov 					fs.vp = NULL;
504d2bf64c3SKonstantin Belousov 				}
505d2bf64c3SKonstantin Belousov 				locked = VOP_ISLOCKED(vp);
506d2bf64c3SKonstantin Belousov 
507d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
508d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
509d2bf64c3SKonstantin Belousov 				/* Do not sleep for vnode lock while fs.m is busy */
510d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
511d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
512d2bf64c3SKonstantin Belousov 				if (error != 0) {
513d2bf64c3SKonstantin Belousov 					vhold(vp);
514d2bf64c3SKonstantin Belousov 					release_page(&fs);
515d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
516d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
517d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
518d2bf64c3SKonstantin Belousov 					vdrop(vp);
519d2bf64c3SKonstantin Belousov 					fs.vp = vp;
520d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
521d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
522d2bf64c3SKonstantin Belousov 					goto RetryFault;
523d2bf64c3SKonstantin Belousov 				}
524d2bf64c3SKonstantin Belousov 				fs.vp = vp;
525d2bf64c3SKonstantin Belousov 			}
526d2bf64c3SKonstantin Belousov vnode_locked:
527d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
528d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
529d2bf64c3SKonstantin Belousov 
530df8bae1dSRodney W. Grimes 			/*
5310d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5320d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5330d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5340d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5350d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5360d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5370d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5380d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5390d94caffSDavid Greenman 			 * return value is the index into the marray for the
5400d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5411c7c3c6aSMatthew Dillon 			 *
542c7aebda8SAttilio Rao 			 * fs.m plus the additional pages are exclusive busied.
54326f9a767SRodney W. Grimes 			 */
54405f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5454866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
546df8bae1dSRodney W. Grimes 
54726f9a767SRodney W. Grimes 			rv = faultcount ?
5484866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
54924a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
55022ba64e8SJohn Dyson 
55126f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
552df8bae1dSRodney W. Grimes 				/*
553f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
554f230c45cSJohn Dyson 				 * with it.
555f230c45cSJohn Dyson 				 */
556f230c45cSJohn Dyson 
557f230c45cSJohn Dyson 				/*
5580d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5590d94caffSDavid Greenman 				 * is responsible for disposition of old page
5600d94caffSDavid Greenman 				 * if moved.
561df8bae1dSRodney W. Grimes 				 */
5624866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5634866e085SJohn Dyson 				if (!fs.m) {
5644866e085SJohn Dyson 					unlock_and_deallocate(&fs);
565f6b04d2bSDavid Greenman 					goto RetryFault;
566f6b04d2bSDavid Greenman 				}
567f6b04d2bSDavid Greenman 
56826f9a767SRodney W. Grimes 				hardfault++;
5691c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
570df8bae1dSRodney W. Grimes 			}
571df8bae1dSRodney W. Grimes 			/*
5720d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5730d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5740d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
575df8bae1dSRodney W. Grimes 			 *
57624a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5770d94caffSDavid Greenman 			 * in this page.
578df8bae1dSRodney W. Grimes 			 *
5790d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
58024a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
5810d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
5820d94caffSDavid Greenman 			 * the same time that we are.
583df8bae1dSRodney W. Grimes 			 */
584a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
585f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
586f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
58726f9a767SRodney W. Grimes 			/*
588a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
58926f9a767SRodney W. Grimes 			 */
590a83c285cSDavid Greenman 			/*
5910d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
5920d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
5930d94caffSDavid Greenman 			 * fault w/ I/O error.
594a83c285cSDavid Greenman 			 */
5954866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
59647221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
5972965a453SKip Macy 				vm_page_lock(fs.m);
5984866e085SJohn Dyson 				vm_page_free(fs.m);
5992965a453SKip Macy 				vm_page_unlock(fs.m);
6004866e085SJohn Dyson 				fs.m = NULL;
6014866e085SJohn Dyson 				unlock_and_deallocate(&fs);
602a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
60326f9a767SRodney W. Grimes 			}
6044866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6052965a453SKip Macy 				vm_page_lock(fs.m);
6064866e085SJohn Dyson 				vm_page_free(fs.m);
6072965a453SKip Macy 				vm_page_unlock(fs.m);
6084866e085SJohn Dyson 				fs.m = NULL;
60926f9a767SRodney W. Grimes 				/*
61026f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
61126f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
61226f9a767SRodney W. Grimes 				 */
613df8bae1dSRodney W. Grimes 			}
614df8bae1dSRodney W. Grimes 		}
61540360b1bSMatthew Dillon 
616df8bae1dSRodney W. Grimes 		/*
6171c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6181c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
619df8bae1dSRodney W. Grimes 		 */
6204866e085SJohn Dyson 		if (fs.object == fs.first_object)
6214866e085SJohn Dyson 			fs.first_m = fs.m;
622df8bae1dSRodney W. Grimes 
623df8bae1dSRodney W. Grimes 		/*
6240d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6250d94caffSDavid Greenman 		 * unlocking the current one.
626df8bae1dSRodney W. Grimes 		 */
6274866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6284866e085SJohn Dyson 		next_object = fs.object->backing_object;
629df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
630df8bae1dSRodney W. Grimes 			/*
6310d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6320d94caffSDavid Greenman 			 * object with zeros.
633df8bae1dSRodney W. Grimes 			 */
6344866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6354866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
63689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
637df8bae1dSRodney W. Grimes 
6384866e085SJohn Dyson 				fs.object = fs.first_object;
6394866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6404866e085SJohn Dyson 				fs.m = fs.first_m;
64189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
642df8bae1dSRodney W. Grimes 			}
6434866e085SJohn Dyson 			fs.first_m = NULL;
644df8bae1dSRodney W. Grimes 
6454221e284SAlan Cox 			/*
6464221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6474221e284SAlan Cox 			 */
6484866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
649fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6504221e284SAlan Cox 			} else {
65167596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
6524221e284SAlan Cox 			}
65367596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
6544221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6551c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6560d94caffSDavid Greenman 		} else {
657c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
658c8567c3aSAlan Cox 			    ("object loop %p", next_object));
65989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
660c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
661c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6624866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
66389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
6644866e085SJohn Dyson 			fs.object = next_object;
665df8bae1dSRodney W. Grimes 		}
666df8bae1dSRodney W. Grimes 	}
6671c7c3c6aSMatthew Dillon 
668c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
669df8bae1dSRodney W. Grimes 
670df8bae1dSRodney W. Grimes 	/*
6710d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
672df8bae1dSRodney W. Grimes 	 * is held.]
673df8bae1dSRodney W. Grimes 	 */
674df8bae1dSRodney W. Grimes 
675df8bae1dSRodney W. Grimes 	/*
6760d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6770d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6780d94caffSDavid Greenman 	 * top-level object.
679df8bae1dSRodney W. Grimes 	 */
6804866e085SJohn Dyson 	if (fs.object != fs.first_object) {
681df8bae1dSRodney W. Grimes 		/*
6820d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
683df8bae1dSRodney W. Grimes 		 */
684a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
685df8bae1dSRodney W. Grimes 			/*
6861c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
6871c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
6881c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
6891c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
6901c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
6911c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
6921c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
6931c7c3c6aSMatthew Dillon 			 * to swap when needed.
694df8bae1dSRodney W. Grimes 			 */
695ebf75125SAlan Cox 			is_first_object_locked = FALSE;
696e50346b5SAlan Cox 			if (
697de5f6a77SJohn Dyson 				/*
698de5f6a77SJohn Dyson 				 * Only one shadow object
699de5f6a77SJohn Dyson 				 */
7004866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
701de5f6a77SJohn Dyson 				/*
702de5f6a77SJohn Dyson 				 * No COW refs, except us
703de5f6a77SJohn Dyson 				 */
7044866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
705de5f6a77SJohn Dyson 				/*
7065929bcfaSPhilippe Charnier 				 * No one else can look this object up
707de5f6a77SJohn Dyson 				 */
7084866e085SJohn Dyson 				(fs.object->handle == NULL) &&
709de5f6a77SJohn Dyson 				/*
710de5f6a77SJohn Dyson 				 * No other ways to look the object up
711de5f6a77SJohn Dyson 				 */
7124866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7134866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
71489f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
715de5f6a77SJohn Dyson 				/*
716de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
717de5f6a77SJohn Dyson 				 */
718e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
7192d8acc0fSJohn Dyson 				/*
720de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
721df8bae1dSRodney W. Grimes 				 */
722eb00b276SAlan Cox 				vm_page_lock(fs.first_m);
7234866e085SJohn Dyson 				vm_page_free(fs.first_m);
7242965a453SKip Macy 				vm_page_unlock(fs.first_m);
725de5f6a77SJohn Dyson 				/*
7261c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7271c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7281c7c3c6aSMatthew Dillon 				 * automatically made dirty.
729de5f6a77SJohn Dyson 				 */
730e946b949SAttilio Rao 				if (vm_page_rename(fs.m, fs.first_object,
731e946b949SAttilio Rao 				    fs.first_pindex)) {
732e946b949SAttilio Rao 					unlock_and_deallocate(&fs);
733e946b949SAttilio Rao 					goto RetryFault;
734e946b949SAttilio Rao 				}
735c7aebda8SAttilio Rao 				vm_page_xbusy(fs.m);
736d98ddc46SAlan Cox 				fs.first_m = fs.m;
7374866e085SJohn Dyson 				fs.m = NULL;
73867596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
739de5f6a77SJohn Dyson 			} else {
740de5f6a77SJohn Dyson 				/*
741de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
742de5f6a77SJohn Dyson 				 */
743669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
744669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
745d8778512SAlan Cox 				if (wired && (fault_flags &
746d8778512SAlan Cox 				    VM_FAULT_CHANGE_WIRING) == 0) {
7472965a453SKip Macy 					vm_page_lock(fs.first_m);
748d8778512SAlan Cox 					vm_page_wire(fs.first_m);
7492965a453SKip Macy 					vm_page_unlock(fs.first_m);
7502965a453SKip Macy 
7512965a453SKip Macy 					vm_page_lock(fs.m);
752d8778512SAlan Cox 					vm_page_unwire(fs.m, FALSE);
7532965a453SKip Macy 					vm_page_unlock(fs.m);
754de5f6a77SJohn Dyson 				}
755df8bae1dSRodney W. Grimes 				/*
756df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
757df8bae1dSRodney W. Grimes 				 */
7584866e085SJohn Dyson 				release_page(&fs);
759de5f6a77SJohn Dyson 			}
7601c7c3c6aSMatthew Dillon 			/*
7611c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7621c7c3c6aSMatthew Dillon 			 * conditional
7631c7c3c6aSMatthew Dillon 			 */
7644866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
76589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
766df8bae1dSRodney W. Grimes 			/*
767df8bae1dSRodney W. Grimes 			 * Only use the new page below...
768df8bae1dSRodney W. Grimes 			 */
7694866e085SJohn Dyson 			fs.object = fs.first_object;
7704866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
771d98ddc46SAlan Cox 			fs.m = fs.first_m;
772f29ba63eSAlan Cox 			if (!is_first_object_locked)
77389f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
77467596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
7754d34e019SKonstantin Belousov 			curthread->td_cow++;
7760d94caffSDavid Greenman 		} else {
777df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
778df8bae1dSRodney W. Grimes 		}
779df8bae1dSRodney W. Grimes 	}
780df8bae1dSRodney W. Grimes 
781df8bae1dSRodney W. Grimes 	/*
7820d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7830d94caffSDavid Greenman 	 * lookup.
784df8bae1dSRodney W. Grimes 	 */
78519dc5607STor Egge 	if (!fs.lookup_still_valid) {
786df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
787a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
788df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
789df8bae1dSRodney W. Grimes 
79019dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
791b823bbd6SMatthew Dillon 			release_page(&fs);
792b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
793b823bbd6SMatthew Dillon 			goto RetryFault;
794b823bbd6SMatthew Dillon 		}
79519dc5607STor Egge 		fs.lookup_still_valid = TRUE;
79619dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
79719dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
7984866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
799df8bae1dSRodney W. Grimes 
800df8bae1dSRodney W. Grimes 			/*
80144ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8020d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8030d94caffSDavid Greenman 			 * pageout will grab it eventually.
804df8bae1dSRodney W. Grimes 			 */
805df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8064866e085SJohn Dyson 				release_page(&fs);
8074866e085SJohn Dyson 				unlock_and_deallocate(&fs);
80819dc5607STor Egge 
80919dc5607STor Egge 				/*
81019dc5607STor Egge 				 * If retry of map lookup would have blocked then
81119dc5607STor Egge 				 * retry fault from start.
81219dc5607STor Egge 				 */
81319dc5607STor Egge 				if (result == KERN_FAILURE)
81419dc5607STor Egge 					goto RetryFault;
815df8bae1dSRodney W. Grimes 				return (result);
816df8bae1dSRodney W. Grimes 			}
8174866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8184866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8194866e085SJohn Dyson 				release_page(&fs);
8204866e085SJohn Dyson 				unlock_and_deallocate(&fs);
821df8bae1dSRodney W. Grimes 				goto RetryFault;
822df8bae1dSRodney W. Grimes 			}
82319dc5607STor Egge 
824df8bae1dSRodney W. Grimes 			/*
8250d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8260d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8270d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8280d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8290d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8300d94caffSDavid Greenman 			 * write-enabled after all.
831df8bae1dSRodney W. Grimes 			 */
832df8bae1dSRodney W. Grimes 			prot &= retry_prot;
833df8bae1dSRodney W. Grimes 		}
83419dc5607STor Egge 	}
835d2bf64c3SKonstantin Belousov 	/*
8365758fe71SAlan Cox 	 * If the page was filled by a pager, update the map entry's
8375758fe71SAlan Cox 	 * last read offset.  Since the pager does not return the
8385758fe71SAlan Cox 	 * actual set of pages that it read, this update is based on
8395758fe71SAlan Cox 	 * the requested set.  Typically, the requested and actual
8405758fe71SAlan Cox 	 * sets are the same.
841d2bf64c3SKonstantin Belousov 	 *
842d2bf64c3SKonstantin Belousov 	 * XXX The following assignment modifies the map
843d2bf64c3SKonstantin Belousov 	 * without holding a write lock on it.
844d2bf64c3SKonstantin Belousov 	 */
8455758fe71SAlan Cox 	if (hardfault)
84613458803SAlan Cox 		fs.entry->next_read = fs.pindex + faultcount - reqpage;
847d2bf64c3SKonstantin Belousov 
848acd11c74SAlan Cox 	if ((prot & VM_PROT_WRITE) != 0 ||
849acd11c74SAlan Cox 	    (fault_flags & VM_FAULT_DIRTY) != 0) {
850b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8514f79d873SMatthew Dillon 
8522ddba215SDavid Greenman 		/*
853b146f9e5SAlan Cox 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
8544f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8554f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8564f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8574f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8584f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8592ddba215SDavid Greenman 		 */
8604f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8614f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
862b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
8634f79d873SMatthew Dillon 		} else {
864b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
8654f79d873SMatthew Dillon 		}
866e2997feaSAlan Cox 
867e2997feaSAlan Cox 		/*
868e2997feaSAlan Cox 		 * If the fault is a write, we know that this page is being
869e2997feaSAlan Cox 		 * written NOW so dirty it explicitly to save on
870e2997feaSAlan Cox 		 * pmap_is_modified() calls later.
871e2997feaSAlan Cox 		 *
872e2997feaSAlan Cox 		 * Also tell the backing pager, if any, that it should remove
873e2997feaSAlan Cox 		 * any swap backing since the page is now dirty.
874e2997feaSAlan Cox 		 */
875acd11c74SAlan Cox 		if (((fault_type & VM_PROT_WRITE) != 0 &&
876acd11c74SAlan Cox 		    (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
877acd11c74SAlan Cox 		    (fault_flags & VM_FAULT_DIRTY) != 0) {
8787dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
8791c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
8802ddba215SDavid Greenman 		}
8812ddba215SDavid Greenman 	}
882f6b04d2bSDavid Greenman 
883c7aebda8SAttilio Rao 	vm_page_assert_xbusied(fs.m);
884c7aebda8SAttilio Rao 
8854221e284SAlan Cox 	/*
88678cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
8874221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8884221e284SAlan Cox 	 */
88978cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
89078cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
89189f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
892cbfbaad8SAlan Cox 
89386735996SAlan Cox 	/*
89486735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
89586735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
89686735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
89786735996SAlan Cox 	 * won't find it (yet).
89886735996SAlan Cox 	 */
899eb2a0517SAlan Cox 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
9002db65ab4SAlan Cox 	if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0)
901*63281952SAlan Cox 		vm_fault_prefault(&fs, vaddr, faultcount, reqpage);
90289f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
9032965a453SKip Macy 	vm_page_lock(fs.m);
904ff97964aSJohn Dyson 
905df8bae1dSRodney W. Grimes 	/*
9060d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9070d94caffSDavid Greenman 	 * can find it.
908df8bae1dSRodney W. Grimes 	 */
9092db65ab4SAlan Cox 	if (fault_flags & VM_FAULT_CHANGE_WIRING) {
910df8bae1dSRodney W. Grimes 		if (wired)
9114866e085SJohn Dyson 			vm_page_wire(fs.m);
912df8bae1dSRodney W. Grimes 		else
91373007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
91403679e23SAlan Cox 	} else
9154866e085SJohn Dyson 		vm_page_activate(fs.m);
916acd11c74SAlan Cox 	if (m_hold != NULL) {
917acd11c74SAlan Cox 		*m_hold = fs.m;
918acd11c74SAlan Cox 		vm_page_hold(fs.m);
919acd11c74SAlan Cox 	}
9202965a453SKip Macy 	vm_page_unlock(fs.m);
921c7aebda8SAttilio Rao 	vm_page_xunbusy(fs.m);
922eeec6babSJohn Baldwin 
923eebf3286SAlan Cox 	/*
924eebf3286SAlan Cox 	 * Unlock everything, and return
925eebf3286SAlan Cox 	 */
926eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
927b3a01bdfSAndrey Zonov 	if (hardfault) {
928b3a01bdfSAndrey Zonov 		PCPU_INC(cnt.v_io_faults);
9291c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
930b3a01bdfSAndrey Zonov 	} else
9311c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
934df8bae1dSRodney W. Grimes }
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes /*
93713458803SAlan Cox  * Speed up the reclamation of up to "distance" pages that precede the
93813458803SAlan Cox  * faulting pindex within the first object of the shadow chain.
93913458803SAlan Cox  */
94013458803SAlan Cox static void
94113458803SAlan Cox vm_fault_cache_behind(const struct faultstate *fs, int distance)
94213458803SAlan Cox {
94313458803SAlan Cox 	vm_object_t first_object, object;
94413458803SAlan Cox 	vm_page_t m, m_prev;
94513458803SAlan Cox 	vm_pindex_t pindex;
94613458803SAlan Cox 
94713458803SAlan Cox 	object = fs->object;
94889f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
94913458803SAlan Cox 	first_object = fs->first_object;
95013458803SAlan Cox 	if (first_object != object) {
95189f6b863SAttilio Rao 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
95289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
95389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(first_object);
95489f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
95513458803SAlan Cox 		}
95613458803SAlan Cox 	}
95728634820SAlan Cox 	/* Neither fictitious nor unmanaged pages can be cached. */
95828634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
95913458803SAlan Cox 		if (fs->first_pindex < distance)
96013458803SAlan Cox 			pindex = 0;
96113458803SAlan Cox 		else
96213458803SAlan Cox 			pindex = fs->first_pindex - distance;
96313458803SAlan Cox 		if (pindex < OFF_TO_IDX(fs->entry->offset))
96413458803SAlan Cox 			pindex = OFF_TO_IDX(fs->entry->offset);
96513458803SAlan Cox 		m = first_object != object ? fs->first_m : fs->m;
966c7aebda8SAttilio Rao 		vm_page_assert_xbusied(m);
96713458803SAlan Cox 		m_prev = vm_page_prev(m);
96813458803SAlan Cox 		while ((m = m_prev) != NULL && m->pindex >= pindex &&
96913458803SAlan Cox 		    m->valid == VM_PAGE_BITS_ALL) {
97013458803SAlan Cox 			m_prev = vm_page_prev(m);
971c7aebda8SAttilio Rao 			if (vm_page_busied(m))
97213458803SAlan Cox 				continue;
97313458803SAlan Cox 			vm_page_lock(m);
97413458803SAlan Cox 			if (m->hold_count == 0 && m->wire_count == 0) {
97513458803SAlan Cox 				pmap_remove_all(m);
97613458803SAlan Cox 				vm_page_aflag_clear(m, PGA_REFERENCED);
97713458803SAlan Cox 				if (m->dirty != 0)
97813458803SAlan Cox 					vm_page_deactivate(m);
97913458803SAlan Cox 				else
98013458803SAlan Cox 					vm_page_cache(m);
98113458803SAlan Cox 			}
98213458803SAlan Cox 			vm_page_unlock(m);
98313458803SAlan Cox 		}
98413458803SAlan Cox 	}
98513458803SAlan Cox 	if (first_object != object)
98689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(first_object);
98713458803SAlan Cox }
98813458803SAlan Cox 
98913458803SAlan Cox /*
990566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
991566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
992566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
993566526a9SAlan Cox  * of mmap time.
994566526a9SAlan Cox  */
995566526a9SAlan Cox static void
996*63281952SAlan Cox vm_fault_prefault(const struct faultstate *fs, vm_offset_t addra,
997*63281952SAlan Cox     int faultcount, int reqpage)
998566526a9SAlan Cox {
999*63281952SAlan Cox 	pmap_t pmap;
1000*63281952SAlan Cox 	vm_map_entry_t entry;
1001*63281952SAlan Cox 	vm_object_t backing_object, lobject;
1002566526a9SAlan Cox 	vm_offset_t addr, starta;
1003566526a9SAlan Cox 	vm_pindex_t pindex;
10042053c127SStephan Uphoff 	vm_page_t m;
1005*63281952SAlan Cox 	int backward, forward, i;
1006566526a9SAlan Cox 
1007*63281952SAlan Cox 	pmap = fs->map->pmap;
1008950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1009566526a9SAlan Cox 		return;
1010566526a9SAlan Cox 
1011*63281952SAlan Cox 	if (faultcount > 0) {
1012*63281952SAlan Cox 		backward = reqpage;
1013*63281952SAlan Cox 		forward = faultcount - reqpage - 1;
1014*63281952SAlan Cox 	} else {
1015*63281952SAlan Cox 		backward = PFBAK;
1016*63281952SAlan Cox 		forward = PFFOR;
1017*63281952SAlan Cox 	}
1018*63281952SAlan Cox 	entry = fs->entry;
1019566526a9SAlan Cox 
1020*63281952SAlan Cox 	starta = addra - backward * PAGE_SIZE;
1021566526a9SAlan Cox 	if (starta < entry->start) {
1022566526a9SAlan Cox 		starta = entry->start;
1023566526a9SAlan Cox 	} else if (starta > addra) {
1024566526a9SAlan Cox 		starta = 0;
1025566526a9SAlan Cox 	}
1026566526a9SAlan Cox 
1027*63281952SAlan Cox 	/*
1028*63281952SAlan Cox 	 * Generate the sequence of virtual addresses that are candidates for
1029*63281952SAlan Cox 	 * prefaulting in an outward spiral from the faulting virtual address,
1030*63281952SAlan Cox 	 * "addra".  Specifically, the sequence is "addra - PAGE_SIZE", "addra
1031*63281952SAlan Cox 	 * + PAGE_SIZE", "addra - 2 * PAGE_SIZE", "addra + 2 * PAGE_SIZE", ...
1032*63281952SAlan Cox 	 * If the candidate address doesn't have a backing physical page, then
1033*63281952SAlan Cox 	 * the loop immediately terminates.
1034*63281952SAlan Cox 	 */
1035*63281952SAlan Cox 	for (i = 0; i < 2 * imax(backward, forward); i++) {
1036*63281952SAlan Cox 		addr = addra + ((i >> 1) + 1) * ((i & 1) == 0 ? -PAGE_SIZE :
1037*63281952SAlan Cox 		    PAGE_SIZE);
1038*63281952SAlan Cox 		if (addr > addra + forward * PAGE_SIZE)
1039566526a9SAlan Cox 			addr = 0;
1040566526a9SAlan Cox 
1041566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1042566526a9SAlan Cox 			continue;
1043566526a9SAlan Cox 
1044566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1045566526a9SAlan Cox 			continue;
1046566526a9SAlan Cox 
1047566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1048*63281952SAlan Cox 		lobject = entry->object.vm_object;
1049c141ae7fSAlan Cox 		VM_OBJECT_RLOCK(lobject);
1050566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1051566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1052566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
105336930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
105436930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1055566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1056c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1057c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1058566526a9SAlan Cox 			lobject = backing_object;
1059566526a9SAlan Cox 		}
1060cbfbaad8SAlan Cox 		if (m == NULL) {
1061c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1062566526a9SAlan Cox 			break;
1063cbfbaad8SAlan Cox 		}
10640a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
10653c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
10667bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1067c141ae7fSAlan Cox 		VM_OBJECT_RUNLOCK(lobject);
1068566526a9SAlan Cox 	}
1069566526a9SAlan Cox }
1070566526a9SAlan Cox 
1071566526a9SAlan Cox /*
107282de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
107382de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
107482de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
107582de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
107682de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
107782de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
107882de724fSAlan Cox  */
107982de724fSAlan Cox int
108082de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
108182de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
108282de724fSAlan Cox {
108382de724fSAlan Cox 	vm_offset_t end, va;
108482de724fSAlan Cox 	vm_page_t *mp;
10857e14088dSKonstantin Belousov 	int count;
108682de724fSAlan Cox 	boolean_t pmap_failed;
108782de724fSAlan Cox 
1088af32c419SKonstantin Belousov 	if (len == 0)
1089af32c419SKonstantin Belousov 		return (0);
109082de724fSAlan Cox 	end = round_page(addr + len);
109182de724fSAlan Cox 	addr = trunc_page(addr);
109282de724fSAlan Cox 
109382de724fSAlan Cox 	/*
109482de724fSAlan Cox 	 * Check for illegal addresses.
109582de724fSAlan Cox 	 */
109682de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
109782de724fSAlan Cox 		return (-1);
109882de724fSAlan Cox 
10997e14088dSKonstantin Belousov 	if (atop(end - addr) > max_count)
110082de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
11017e14088dSKonstantin Belousov 	count = atop(end - addr);
110282de724fSAlan Cox 
110382de724fSAlan Cox 	/*
110482de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
110582de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
110682de724fSAlan Cox 	 */
110782de724fSAlan Cox 	pmap_failed = FALSE;
110882de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
110982de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
111082de724fSAlan Cox 		if (*mp == NULL)
111182de724fSAlan Cox 			pmap_failed = TRUE;
111282de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1113a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
111482de724fSAlan Cox 			/*
111582de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
111682de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
111782de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
111882de724fSAlan Cox 			 * operation.
11193c76db4cSAlan Cox 			 *
1120abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1121abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
112282de724fSAlan Cox 			 */
11233c76db4cSAlan Cox 			vm_page_dirty(*mp);
112482de724fSAlan Cox 		}
112582de724fSAlan Cox 	}
112682de724fSAlan Cox 	if (pmap_failed) {
112782de724fSAlan Cox 		/*
112882de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
112982de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
113082de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
113182de724fSAlan Cox 		 * and hold these pages.
113282de724fSAlan Cox 		 */
113382de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1134be996836SAttilio Rao 			if (*mp == NULL && vm_fault_hold(map, va, prot,
113582de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
113682de724fSAlan Cox 				goto error;
113782de724fSAlan Cox 	}
113882de724fSAlan Cox 	return (count);
113982de724fSAlan Cox error:
114082de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
114182de724fSAlan Cox 		if (*mp != NULL) {
114282de724fSAlan Cox 			vm_page_lock(*mp);
114382de724fSAlan Cox 			vm_page_unhold(*mp);
114482de724fSAlan Cox 			vm_page_unlock(*mp);
114582de724fSAlan Cox 		}
114682de724fSAlan Cox 	return (-1);
114782de724fSAlan Cox }
114882de724fSAlan Cox 
114982de724fSAlan Cox /*
1150df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1151df8bae1dSRodney W. Grimes  *
1152df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1153df8bae1dSRodney W. Grimes  */
1154df8bae1dSRodney W. Grimes int
11554be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
11562db65ab4SAlan Cox     boolean_t fictitious)
1157df8bae1dSRodney W. Grimes {
115854d92145SMatthew Dillon 	vm_offset_t va;
1159df8bae1dSRodney W. Grimes 	int rv;
1160df8bae1dSRodney W. Grimes 
1161df8bae1dSRodney W. Grimes 	/*
11620d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1163ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1164ef594d31SAlan Cox 	 * read-only sections.
1165df8bae1dSRodney W. Grimes 	 */
1166df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
11672db65ab4SAlan Cox 		rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
11687aaaa4fdSJohn Dyson 		if (rv) {
11697aaaa4fdSJohn Dyson 			if (va != start)
11704be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
11717aaaa4fdSJohn Dyson 			return (rv);
11727aaaa4fdSJohn Dyson 		}
11737aaaa4fdSJohn Dyson 	}
11747aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
11757aaaa4fdSJohn Dyson }
11767aaaa4fdSJohn Dyson 
11777aaaa4fdSJohn Dyson /*
1178df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1179df8bae1dSRodney W. Grimes  *
1180df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1181df8bae1dSRodney W. Grimes  */
118226f9a767SRodney W. Grimes void
11834be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
11844be14af9SAlan Cox     boolean_t fictitious)
1185df8bae1dSRodney W. Grimes {
1186227f9a1cSJake Burkholder 	vm_paddr_t pa;
1187227f9a1cSJake Burkholder 	vm_offset_t va;
1188e3ef0d2fSAlan Cox 	vm_page_t m;
118954d92145SMatthew Dillon 	pmap_t pmap;
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1192df8bae1dSRodney W. Grimes 
1193df8bae1dSRodney W. Grimes 	/*
11940d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
11950d94caffSDavid Greenman 	 * mappings from the physical map system.
1196df8bae1dSRodney W. Grimes 	 */
1197df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1198df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1199227f9a1cSJake Burkholder 		if (pa != 0) {
1200df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
12014be14af9SAlan Cox 			if (!fictitious) {
1202e3ef0d2fSAlan Cox 				m = PHYS_TO_VM_PAGE(pa);
1203e3ef0d2fSAlan Cox 				vm_page_lock(m);
1204e3ef0d2fSAlan Cox 				vm_page_unwire(m, TRUE);
1205e3ef0d2fSAlan Cox 				vm_page_unlock(m);
1206df8bae1dSRodney W. Grimes 			}
1207b18bfc3dSJohn Dyson 		}
12084be14af9SAlan Cox 	}
1209df8bae1dSRodney W. Grimes }
1210df8bae1dSRodney W. Grimes 
1211df8bae1dSRodney W. Grimes /*
1212df8bae1dSRodney W. Grimes  *	Routine:
1213df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1214df8bae1dSRodney W. Grimes  *	Function:
1215210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1216210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1217210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1218210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1219df8bae1dSRodney W. Grimes  *
1220df8bae1dSRodney W. Grimes  *	In/out conditions:
1221df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1222df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1223df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1224df8bae1dSRodney W. Grimes  */
122526f9a767SRodney W. Grimes void
1226121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1227121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1228121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1229df8bae1dSRodney W. Grimes {
1230210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
12317afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1232210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1233df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1234df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1235df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1236210a6886SKonstantin Belousov 	boolean_t src_readonly, upgrade;
1237df8bae1dSRodney W. Grimes 
1238df8bae1dSRodney W. Grimes #ifdef	lint
1239df8bae1dSRodney W. Grimes 	src_map++;
12400d94caffSDavid Greenman #endif	/* lint */
1241df8bae1dSRodney W. Grimes 
1242210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
1243210a6886SKonstantin Belousov 
1244df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
12457afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
12467afab86cSAlan Cox 	src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1247df8bae1dSRodney W. Grimes 
1248df8bae1dSRodney W. Grimes 	/*
12490d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
12500d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1251df8bae1dSRodney W. Grimes 	 */
125224a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
125357b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1254f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1255f8a47341SAlan Cox 	dst_object->flags |= OBJ_COLORED;
1256f8a47341SAlan Cox 	dst_object->pg_color = atop(dst_entry->start);
1257f8a47341SAlan Cox #endif
1258df8bae1dSRodney W. Grimes 
125989f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1260210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1261121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
1262df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1263df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
12643364c323SKonstantin Belousov 	dst_object->charge = dst_entry->end - dst_entry->start;
1265210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1266ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1267121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1268ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1269ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1270121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
1271210a6886SKonstantin Belousov 	} else {
1272ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1273ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1274210a6886SKonstantin Belousov 	}
1275e4ed417aSAlan Cox 	access = prot = dst_entry->protection;
1276210a6886SKonstantin Belousov 	/*
1277210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1278210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1279210a6886SKonstantin Belousov 	 * write accesses.
1280210a6886SKonstantin Belousov 	 *
1281210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1282210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1283210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1284210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1285210a6886SKonstantin Belousov 	 */
1286210a6886SKonstantin Belousov 	if (!upgrade)
1287210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1288df8bae1dSRodney W. Grimes 
1289df8bae1dSRodney W. Grimes 	/*
1290ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1291ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1292ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1293ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
1294ef45823eSKonstantin Belousov 	 * Since the destination object does share any backing storage
1295ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1296ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1297df8bae1dSRodney W. Grimes 	 */
12987afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1299df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
13007afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes 		/*
13037afab86cSAlan Cox 		 * Allocate a page in the destination object.
1304df8bae1dSRodney W. Grimes 		 */
1305df8bae1dSRodney W. Grimes 		do {
13067afab86cSAlan Cox 			dst_m = vm_page_alloc(dst_object, dst_pindex,
13077afab86cSAlan Cox 			    VM_ALLOC_NORMAL);
1308df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
130989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(dst_object);
1310df8bae1dSRodney W. Grimes 				VM_WAIT;
131189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(dst_object);
1312df8bae1dSRodney W. Grimes 			}
1313df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1314df8bae1dSRodney W. Grimes 
1315df8bae1dSRodney W. Grimes 		/*
1316df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
13170d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
13180d94caffSDavid Greenman 		 * memory.)
1319df8bae1dSRodney W. Grimes 		 */
132083b375eaSAttilio Rao 		VM_OBJECT_RLOCK(src_object);
1321c5b65a67SAlan Cox 		object = src_object;
13227afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
13237afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
13247afab86cSAlan Cox 		    src_readonly &&
1325c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1326c5b65a67SAlan Cox 			/*
1327c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1328c5b65a67SAlan Cox 			 */
132983b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1330c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
133183b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1332c5b65a67SAlan Cox 			object = backing_object;
1333c5b65a67SAlan Cox 		}
1334df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1335df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1336669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
133783b375eaSAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1338669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
1339bc79b37fSKonstantin Belousov 		dst_m->dirty = VM_PAGE_BITS_ALL;
134089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1341df8bae1dSRodney W. Grimes 
1342df8bae1dSRodney W. Grimes 		/*
1343210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1344210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1345210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
1346df8bae1dSRodney W. Grimes 		 */
1347210a6886SKonstantin Belousov 		pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1348df8bae1dSRodney W. Grimes 
1349df8bae1dSRodney W. Grimes 		/*
1350df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1351df8bae1dSRodney W. Grimes 		 */
135289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
13532965a453SKip Macy 
1354210a6886SKonstantin Belousov 		if (upgrade) {
13552965a453SKip Macy 			vm_page_lock(src_m);
1356210a6886SKonstantin Belousov 			vm_page_unwire(src_m, 0);
1357e20e8c15SKonstantin Belousov 			vm_page_unlock(src_m);
13582965a453SKip Macy 
13592965a453SKip Macy 			vm_page_lock(dst_m);
1360210a6886SKonstantin Belousov 			vm_page_wire(dst_m);
1361e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
13622965a453SKip Macy 		} else {
13632965a453SKip Macy 			vm_page_lock(dst_m);
1364df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1365e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
13662965a453SKip Macy 		}
1367c7aebda8SAttilio Rao 		vm_page_xunbusy(dst_m);
1368df8bae1dSRodney W. Grimes 	}
136989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1370210a6886SKonstantin Belousov 	if (upgrade) {
1371210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1372210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1373210a6886SKonstantin Belousov 	}
1374df8bae1dSRodney W. Grimes }
137526f9a767SRodney W. Grimes 
137626f9a767SRodney W. Grimes 
137726f9a767SRodney W. Grimes /*
137826f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
137922ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
138022ba64e8SJohn Dyson  * pages for the pages to be paged in.
138126f9a767SRodney W. Grimes  *
138226f9a767SRodney W. Grimes  * Inputs:
138322ba64e8SJohn Dyson  *	m, rbehind, rahead
138426f9a767SRodney W. Grimes  *
138526f9a767SRodney W. Grimes  * Outputs:
138626f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
138726f9a767SRodney W. Grimes  *
138826f9a767SRodney W. Grimes  * Return value:
138926f9a767SRodney W. Grimes  *  number of pages in marray
139026f9a767SRodney W. Grimes  */
1391303b270bSEivind Eklund static int
139222ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
139326f9a767SRodney W. Grimes 	vm_page_t m;
139426f9a767SRodney W. Grimes 	int rbehind;
139522ba64e8SJohn Dyson 	int rahead;
139626f9a767SRodney W. Grimes 	vm_page_t *marray;
139726f9a767SRodney W. Grimes 	int *reqpage;
139826f9a767SRodney W. Grimes {
13992d8acc0fSJohn Dyson 	int i,j;
140026f9a767SRodney W. Grimes 	vm_object_t object;
1401a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
140226f9a767SRodney W. Grimes 	vm_page_t rtm;
1403170db9c6SJohn Dyson 	int cbehind, cahead;
140426f9a767SRodney W. Grimes 
140589f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(m->object);
140623955314SAlfred Perlstein 
140726f9a767SRodney W. Grimes 	object = m->object;
1408a316d390SJohn Dyson 	pindex = m->pindex;
1409fcdd9721SPawel Jakub Dawidek 	cbehind = cahead = 0;
1410fcdd9721SPawel Jakub Dawidek 
1411f35329acSJohn Dyson 	/*
141226f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
141326f9a767SRodney W. Grimes 	 */
14141c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
141526f9a767SRodney W. Grimes 		return 0;
14162d8acc0fSJohn Dyson 	}
141726f9a767SRodney W. Grimes 
141822ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
141922ba64e8SJohn Dyson 		*reqpage = 0;
142022ba64e8SJohn Dyson 		marray[0] = m;
142122ba64e8SJohn Dyson 		return 1;
1422170db9c6SJohn Dyson 	}
142322ba64e8SJohn Dyson 
142422ba64e8SJohn Dyson 	if (rahead > cahead) {
142522ba64e8SJohn Dyson 		rahead = cahead;
142622ba64e8SJohn Dyson 	}
142722ba64e8SJohn Dyson 
1428170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1429170db9c6SJohn Dyson 		rbehind = cbehind;
1430170db9c6SJohn Dyson 	}
1431170db9c6SJohn Dyson 
143226f9a767SRodney W. Grimes 	/*
14332d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
143426f9a767SRodney W. Grimes 	 */
14352d8acc0fSJohn Dyson 	if (pindex > 0) {
14362d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1437a316d390SJohn Dyson 			rbehind = pindex;
14382d8acc0fSJohn Dyson 			startpindex = 0;
14392d8acc0fSJohn Dyson 		} else {
1440a316d390SJohn Dyson 			startpindex = pindex - rbehind;
14412d8acc0fSJohn Dyson 		}
14422d8acc0fSJohn Dyson 
14438f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
14448f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
14458f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
144626f9a767SRodney W. Grimes 
144780645364SAlan Cox 		/* tpindex is unsigned; beware of numeric underflow. */
144880645364SAlan Cox 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
144980645364SAlan Cox 		    tpindex < pindex; i++, tpindex--) {
145026f9a767SRodney W. Grimes 
14517bfda801SAlan Cox 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
14527bfda801SAlan Cox 			    VM_ALLOC_IFNOTCACHED);
1453ccbb2f72SJohn Dyson 			if (rtm == NULL) {
145480645364SAlan Cox 				/*
145580645364SAlan Cox 				 * Shift the allocated pages to the
145680645364SAlan Cox 				 * beginning of the array.
145780645364SAlan Cox 				 */
1458ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
145980645364SAlan Cox 					marray[j] = marray[j + tpindex + 1 -
146080645364SAlan Cox 					    startpindex];
146126f9a767SRodney W. Grimes 				}
146280645364SAlan Cox 				break;
146326f9a767SRodney W. Grimes 			}
1464170db9c6SJohn Dyson 
146580645364SAlan Cox 			marray[tpindex - startpindex] = rtm;
146626f9a767SRodney W. Grimes 		}
14672d8acc0fSJohn Dyson 	} else {
14682d8acc0fSJohn Dyson 		startpindex = 0;
14692d8acc0fSJohn Dyson 		i = 0;
14702d8acc0fSJohn Dyson 	}
14712d8acc0fSJohn Dyson 
14722d8acc0fSJohn Dyson 	marray[i] = m;
14732d8acc0fSJohn Dyson 	/* page offset of the required page */
14742d8acc0fSJohn Dyson 	*reqpage = i;
14752d8acc0fSJohn Dyson 
14762d8acc0fSJohn Dyson 	tpindex = pindex + 1;
14772d8acc0fSJohn Dyson 	i++;
14782d8acc0fSJohn Dyson 
14792d8acc0fSJohn Dyson 	/*
14802d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
14812d8acc0fSJohn Dyson 	 */
14822d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
14838f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
14848f8790a7SAlan Cox 		endpindex = rtm->pindex;
14852d8acc0fSJohn Dyson 	if (endpindex > object->size)
14862d8acc0fSJohn Dyson 		endpindex = object->size;
14872d8acc0fSJohn Dyson 
14882d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
14892d8acc0fSJohn Dyson 
14907bfda801SAlan Cox 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
14917bfda801SAlan Cox 		    VM_ALLOC_IFNOTCACHED);
14922d8acc0fSJohn Dyson 		if (rtm == NULL) {
14932d8acc0fSJohn Dyson 			break;
14942d8acc0fSJohn Dyson 		}
14952d8acc0fSJohn Dyson 
14962d8acc0fSJohn Dyson 		marray[i] = rtm;
14972d8acc0fSJohn Dyson 	}
14982d8acc0fSJohn Dyson 
149965ea29a6SAlan Cox 	/* return number of pages */
15002d8acc0fSJohn Dyson 	return i;
150126f9a767SRodney W. Grimes }
15022801687dSKonstantin Belousov 
15035730afc9SAlan Cox /*
15045730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
15055730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
15065730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
15075730afc9SAlan Cox  * spurious page faults.
15085730afc9SAlan Cox  */
15092801687dSKonstantin Belousov int
15102801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
15112801687dSKonstantin Belousov {
15122801687dSKonstantin Belousov 
15135730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
15142801687dSKonstantin Belousov }
15152801687dSKonstantin Belousov 
15162801687dSKonstantin Belousov void
15172801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
15182801687dSKonstantin Belousov {
15192801687dSKonstantin Belousov 
15202801687dSKonstantin Belousov 	curthread_pflags_restore(save);
15212801687dSKonstantin Belousov }
1522