xref: /freebsd/sys/vm/vm_fault.c (revision fcdd9721e4f97fb0c8e6b6a26167b58a278f45df)
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 
77df8bae1dSRodney W. Grimes #include <sys/param.h>
78df8bae1dSRodney W. Grimes #include <sys/systm.h>
794edf4a58SJohn Baldwin #include <sys/kernel.h>
80fb919e4dSMark Murray #include <sys/lock.h>
81fb919e4dSMark Murray #include <sys/mutex.h>
8226f9a767SRodney W. Grimes #include <sys/proc.h>
8326f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8423955314SAlfred Perlstein #include <sys/sysctl.h>
854edf4a58SJohn Baldwin #include <sys/vmmeter.h>
864edf4a58SJohn Baldwin #include <sys/vnode.h>
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes #include <vm/vm.h>
89efeaf95aSDavid Greenman #include <vm/vm_param.h>
90efeaf95aSDavid Greenman #include <vm/pmap.h>
91efeaf95aSDavid Greenman #include <vm/vm_map.h>
92efeaf95aSDavid Greenman #include <vm/vm_object.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
94df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
95a83c285cSDavid Greenman #include <vm/vm_kern.h>
9624a1cce3SDavid Greenman #include <vm/vm_pager.h>
9724a1cce3SDavid Greenman #include <vm/vnode_pager.h>
98efeaf95aSDavid Greenman #include <vm/vm_extern.h>
99df8bae1dSRodney W. Grimes 
100ae51ff11SJeff Roberson #include <sys/mount.h>	/* XXX Temporary for VFS_LOCK_GIANT() */
101ae51ff11SJeff Roberson 
102566526a9SAlan Cox #define PFBAK 4
103566526a9SAlan Cox #define PFFOR 4
104566526a9SAlan Cox #define PAGEORDER_SIZE (PFBAK+PFFOR)
105566526a9SAlan Cox 
106566526a9SAlan Cox static int prefault_pageorder[] = {
107566526a9SAlan Cox 	-1 * PAGE_SIZE, 1 * PAGE_SIZE,
108566526a9SAlan Cox 	-2 * PAGE_SIZE, 2 * PAGE_SIZE,
109566526a9SAlan Cox 	-3 * PAGE_SIZE, 3 * PAGE_SIZE,
110566526a9SAlan Cox 	-4 * PAGE_SIZE, 4 * PAGE_SIZE
111566526a9SAlan Cox };
112566526a9SAlan Cox 
11311caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
114566526a9SAlan Cox static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
11526f9a767SRodney W. Grimes 
11647221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8
11747221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7
11826f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
11926f9a767SRodney W. Grimes 
1204866e085SJohn Dyson struct faultstate {
1214866e085SJohn Dyson 	vm_page_t m;
1224866e085SJohn Dyson 	vm_object_t object;
1234866e085SJohn Dyson 	vm_pindex_t pindex;
1244866e085SJohn Dyson 	vm_page_t first_m;
1254866e085SJohn Dyson 	vm_object_t	first_object;
1264866e085SJohn Dyson 	vm_pindex_t first_pindex;
1274866e085SJohn Dyson 	vm_map_t map;
1284866e085SJohn Dyson 	vm_map_entry_t entry;
12925adb370SBrian Feldman 	int lookup_still_valid;
1304866e085SJohn Dyson 	struct vnode *vp;
1314866e085SJohn Dyson };
1324866e085SJohn Dyson 
13362a59e8fSWarner Losh static inline void
1344866e085SJohn Dyson release_page(struct faultstate *fs)
1354866e085SJohn Dyson {
136e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
13766bdd5d6SAlan Cox 	vm_page_lock_queues();
1384866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1392ad98273SAlan Cox 	vm_page_unlock_queues();
1404866e085SJohn Dyson 	fs->m = NULL;
1414866e085SJohn Dyson }
1424866e085SJohn Dyson 
14362a59e8fSWarner Losh static inline void
1444866e085SJohn Dyson unlock_map(struct faultstate *fs)
1454866e085SJohn Dyson {
14625adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1474866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
14825adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1494866e085SJohn Dyson 	}
1504866e085SJohn Dyson }
1514866e085SJohn Dyson 
1524866e085SJohn Dyson static void
153a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1544866e085SJohn Dyson {
155f29ba63eSAlan Cox 
1564866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
157b009d5a0SAlan Cox 	VM_OBJECT_UNLOCK(fs->object);
1584866e085SJohn Dyson 	if (fs->object != fs->first_object) {
159b009d5a0SAlan Cox 		VM_OBJECT_LOCK(fs->first_object);
1602ad98273SAlan Cox 		vm_page_lock_queues();
1614866e085SJohn Dyson 		vm_page_free(fs->first_m);
1622ad98273SAlan Cox 		vm_page_unlock_queues();
1634866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
164b009d5a0SAlan Cox 		VM_OBJECT_UNLOCK(fs->first_object);
1654866e085SJohn Dyson 		fs->first_m = NULL;
1664866e085SJohn Dyson 	}
1674866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1684866e085SJohn Dyson 	unlock_map(fs);
1694866e085SJohn Dyson 	if (fs->vp != NULL) {
170ae51ff11SJeff Roberson 		int vfslocked;
171ae51ff11SJeff Roberson 
172ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(fs->vp->v_mount);
1730cddd8f0SMatthew Dillon 		vput(fs->vp);
1744866e085SJohn Dyson 		fs->vp = NULL;
175ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
1764866e085SJohn Dyson 	}
1774866e085SJohn Dyson }
1784866e085SJohn Dyson 
179df8bae1dSRodney W. Grimes /*
18040360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
18140360b1bSMatthew Dillon  *	      current object *might* contain the page.
18240360b1bSMatthew Dillon  *
18340360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18440360b1bSMatthew Dillon  */
18540360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
18640360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
18740360b1bSMatthew Dillon 
18840360b1bSMatthew Dillon /*
189df8bae1dSRodney W. Grimes  *	vm_fault:
190df8bae1dSRodney W. Grimes  *
191956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
192df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
193df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
194df8bae1dSRodney W. Grimes  *	associated physical map.
195df8bae1dSRodney W. Grimes  *
196df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
197df8bae1dSRodney W. Grimes  *	proper page address.
198df8bae1dSRodney W. Grimes  *
199df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
200df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
201df8bae1dSRodney W. Grimes  *
202df8bae1dSRodney W. Grimes  *
203df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2040cddd8f0SMatthew Dillon  *	Caller may hold no locks.
205df8bae1dSRodney W. Grimes  */
206df8bae1dSRodney W. Grimes int
20723955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
20823955314SAlfred Perlstein 	 int fault_flags)
20923955314SAlfred Perlstein {
210df8bae1dSRodney W. Grimes 	vm_prot_t prot;
211ebf75125SAlan Cox 	int is_first_object_locked, result;
2126139043bSAlan Cox 	boolean_t growstack, wired;
2132d8acc0fSJohn Dyson 	int map_generation;
214df8bae1dSRodney W. Grimes 	vm_object_t next_object;
21526f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2164866e085SJohn Dyson 	int hardfault;
2172d8acc0fSJohn Dyson 	int faultcount;
2184866e085SJohn Dyson 	struct faultstate fs;
219df8bae1dSRodney W. Grimes 
2204866e085SJohn Dyson 	hardfault = 0;
2216139043bSAlan Cox 	growstack = TRUE;
222d8810d89SAlan Cox 	PCPU_LAZY_INC(cnt.v_vm_faults);
223df8bae1dSRodney W. Grimes 
224df8bae1dSRodney W. Grimes RetryFault:;
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes 	/*
2270d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2280d94caffSDavid Greenman 	 * search.
229df8bae1dSRodney W. Grimes 	 */
23040360b1bSMatthew Dillon 	fs.map = map;
23192de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
23292de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
23392de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
23492de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
23592de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2366139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2376139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2386139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
239a976eb5eSAlan Cox 				if (result != KERN_SUCCESS)
2406139043bSAlan Cox 					return (KERN_FAILURE);
2416139043bSAlan Cox 				growstack = FALSE;
2426139043bSAlan Cox 				goto RetryFault;
2436139043bSAlan Cox 			}
24492de35b0SAlan Cox 			return (result);
24509e0c6ccSJohn Dyson 		}
24609e0c6ccSJohn Dyson 
2477aaaa4fdSJohn Dyson 		/*
2487aaaa4fdSJohn Dyson    		 * If we are user-wiring a r/w segment, and it is COW, then
2497aaaa4fdSJohn Dyson    		 * we need to do the COW operation.  Note that we don't COW
250595236dfSJohn Dyson    		 * currently RO sections now, because it is NOT desirable
2517aaaa4fdSJohn Dyson    		 * to COW .text.  We simply keep .text from ever being COW'ed
2527aaaa4fdSJohn Dyson    		 * and take the heat that one cannot debug wired .text sections.
2537aaaa4fdSJohn Dyson    		 */
2544866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr,
25547221757SJohn Dyson 			VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
2564866e085SJohn Dyson 			&fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
257a976eb5eSAlan Cox 		if (result != KERN_SUCCESS)
2587788e219SAlan Cox 			return (result);
25947221757SJohn Dyson 
2607aaaa4fdSJohn Dyson 		/*
2617aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2627aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2637aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
264ff8f4ebeSAlan Cox 		 *
265ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
266ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2677aaaa4fdSJohn Dyson 		 */
2684866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2694866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2707aaaa4fdSJohn Dyson 	}
27147221757SJohn Dyson 
2724866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2732d8acc0fSJohn Dyson 
2744866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
27547221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
27692c4c4ebSBruce Evans 		    (u_long)vaddr);
2777aaaa4fdSJohn Dyson 	}
2787aaaa4fdSJohn Dyson 
27995e5e988SJohn Dyson 	/*
28095e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
28195e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
28295e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
28395e5e988SJohn Dyson 	 * they will stay around as well.
284fe8e0238SMatthew Dillon 	 *
285fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
286fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
287fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
288532eadefSAlan Cox 	 *
289532eadefSAlan Cox 	 * XXX vnode_pager_lock() can block without releasing the map lock.
29095e5e988SJohn Dyson 	 */
291ed4fe4f4SJeff Roberson 	if (fs.first_object->flags & OBJ_NEEDGIANT)
292a976eb5eSAlan Cox 		mtx_lock(&Giant);
293d22bc710SAlan Cox 	VM_OBJECT_LOCK(fs.first_object);
294a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
295417a26a1SAlan Cox 	fs.vp = vnode_pager_lock(fs.first_object);
2965e4bdb57SAlan Cox 	KASSERT(fs.vp == NULL || !fs.map->system_map,
2975e4bdb57SAlan Cox 	    ("vm_fault: vnode-backed object mapped by system map"));
298ed4fe4f4SJeff Roberson 	KASSERT((fs.first_object->flags & OBJ_NEEDGIANT) == 0 ||
299ed4fe4f4SJeff Roberson 	    !fs.map->system_map,
300ed4fe4f4SJeff Roberson 	    ("vm_fault: Object requiring giant mapped by system map"));
3012cf13952SAlan Cox 	if (fs.first_object->flags & OBJ_NEEDGIANT)
302c1fbc251SAlan Cox 		mtx_unlock(&Giant);
303d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30495e5e988SJohn Dyson 
30525adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
306df8bae1dSRodney W. Grimes 
307df8bae1dSRodney W. Grimes 	if (wired)
308df8bae1dSRodney W. Grimes 		fault_type = prot;
309df8bae1dSRodney W. Grimes 
3104866e085SJohn Dyson 	fs.first_m = NULL;
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	/*
313df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
314df8bae1dSRodney W. Grimes 	 */
3154866e085SJohn Dyson 	fs.object = fs.first_object;
3164866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
317df8bae1dSRodney W. Grimes 	while (TRUE) {
3181c7c3c6aSMatthew Dillon 		/*
3191c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3201c7c3c6aSMatthew Dillon 		 */
3214866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3224866e085SJohn Dyson 			unlock_and_deallocate(&fs);
32347221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32447221757SJohn Dyson 		}
32547221757SJohn Dyson 
3261c7c3c6aSMatthew Dillon 		/*
3271c7c3c6aSMatthew Dillon 		 * See if page is resident
3281c7c3c6aSMatthew Dillon 		 */
3294866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3304866e085SJohn Dyson 		if (fs.m != NULL) {
33186735996SAlan Cox 			int queue;
33298cb733cSKenneth D. Merry 
33398cb733cSKenneth D. Merry 			/*
3349b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3359b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3369b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3379b80d344SKenneth D. Merry 			 * used when the page in question is part of
3389b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3399b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3409b80d344SKenneth D. Merry 			 * which is not what we want.
34198cb733cSKenneth D. Merry 			 */
342f4ecdf05SAlan Cox 			vm_page_lock_queues();
34398cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3449b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3459b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
34698cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
347f4ecdf05SAlan Cox 				vm_page_unlock_queues();
348d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
34998cb733cSKenneth D. Merry 				goto RetryFault;
35098cb733cSKenneth D. Merry 			}
35198cb733cSKenneth D. Merry 
352df8bae1dSRodney W. Grimes 			/*
3531c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3549af80719SAlan Cox 			 * if the page is busy via either VPO_BUSY or
3551c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3561c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3571c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
358956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3591c7c3c6aSMatthew Dillon 			 *
3601c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3611c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3621c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3631c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3641c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3651c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3661c7c3c6aSMatthew Dillon 			 * to pmap it.
367df8bae1dSRodney W. Grimes 			 */
3689af80719SAlan Cox 			if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
3694abd55b2SAlan Cox 				vm_page_unlock_queues();
370a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
371a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
372a51b0840SAlan Cox 					VM_OBJECT_LOCK(fs.first_object);
3739a96b638SAlan Cox 					vm_page_lock_queues();
374a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3759a96b638SAlan Cox 					vm_page_unlock_queues();
376a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
377a51b0840SAlan Cox 					VM_OBJECT_UNLOCK(fs.first_object);
378a51b0840SAlan Cox 					fs.first_m = NULL;
379a51b0840SAlan Cox 				}
380a51b0840SAlan Cox 				unlock_map(&fs);
381a51b0840SAlan Cox 				if (fs.vp != NULL) {
382ed4fe4f4SJeff Roberson 					int vfslck;
383ed4fe4f4SJeff Roberson 
384ed4fe4f4SJeff Roberson 					vfslck = VFS_LOCK_GIANT(fs.vp->v_mount);
385a51b0840SAlan Cox 					vput(fs.vp);
386a51b0840SAlan Cox 					fs.vp = NULL;
387ed4fe4f4SJeff Roberson 					VFS_UNLOCK_GIANT(vfslck);
388a51b0840SAlan Cox 				}
389a51b0840SAlan Cox 				VM_OBJECT_LOCK(fs.object);
390a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
391a51b0840SAlan Cox 				    fs.pindex)) {
392e7e56b28SAlan Cox 					vm_page_sleep_if_busy(fs.m, TRUE,
393e7e56b28SAlan Cox 					    "vmpfw");
394a51b0840SAlan Cox 				}
395a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
396a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
397d8810d89SAlan Cox 				PCPU_LAZY_INC(cnt.v_intrans);
3984866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
399df8bae1dSRodney W. Grimes 				goto RetryFault;
400df8bae1dSRodney W. Grimes 			}
4014866e085SJohn Dyson 			queue = fs.m->queue;
402a1287949SEivind Eklund 
4036d03d577SMatthew Dillon 			vm_pageq_remove_nowakeup(fs.m);
404c82b0181SJohn Dyson 
40582eedee4SAlan Cox 			if (VM_PAGE_RESOLVEQUEUE(fs.m, queue) == PQ_CACHE &&
40682eedee4SAlan Cox 			    vm_page_count_severe()) {
4074866e085SJohn Dyson 				vm_page_activate(fs.m);
4084abd55b2SAlan Cox 				vm_page_unlock_queues();
4094866e085SJohn Dyson 				unlock_and_deallocate(&fs);
410ef6020d1SMike Silbersack 				VM_WAITPFAULT;
41122ba64e8SJohn Dyson 				goto RetryFault;
41222ba64e8SJohn Dyson 			}
413768131d2SAlan Cox 			vm_page_unlock_queues();
4147615edaaSMatthew Dillon 
4151c7c3c6aSMatthew Dillon 			/*
4161c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4171c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4181c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4191c7c3c6aSMatthew Dillon 			 * found the page ).
4201c7c3c6aSMatthew Dillon 			 */
421e69763a3SDoug Rabson 			vm_page_busy(fs.m);
4224866e085SJohn Dyson 			if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
4234866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
4240d94caffSDavid Greenman 				goto readrest;
4250d94caffSDavid Greenman 			}
426ffc82b0aSJohn Dyson 
427df8bae1dSRodney W. Grimes 			break;
428df8bae1dSRodney W. Grimes 		}
4291c7c3c6aSMatthew Dillon 
4301c7c3c6aSMatthew Dillon 		/*
43140360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
43240360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4331c7c3c6aSMatthew Dillon 		 */
43440360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4354866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4364866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4375f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4385f55e841SDavid Greenman 			}
43922ba64e8SJohn Dyson 
440df8bae1dSRodney W. Grimes 			/*
4410d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
442df8bae1dSRodney W. Grimes 			 */
44340360b1bSMatthew Dillon 			fs.m = NULL;
44440360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
4454866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4464866e085SJohn Dyson 				    (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
44740360b1bSMatthew Dillon 			}
4484866e085SJohn Dyson 			if (fs.m == NULL) {
4494866e085SJohn Dyson 				unlock_and_deallocate(&fs);
450ef6020d1SMike Silbersack 				VM_WAITPFAULT;
451df8bae1dSRodney W. Grimes 				goto RetryFault;
452df8bae1dSRodney W. Grimes 			}
453df8bae1dSRodney W. Grimes 		}
45447221757SJohn Dyson 
4550d94caffSDavid Greenman readrest:
4561c7c3c6aSMatthew Dillon 		/*
45740360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
45840360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
45940360b1bSMatthew Dillon 		 * valid.
46040360b1bSMatthew Dillon 		 *
46140360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
46240360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
46340360b1bSMatthew Dillon 		 * at the same time.
4641c7c3c6aSMatthew Dillon 		 */
46540360b1bSMatthew Dillon 		if (TRYPAGER) {
466df8bae1dSRodney W. Grimes 			int rv;
46726f9a767SRodney W. Grimes 			int reqpage;
468867a482dSJohn Dyson 			int ahead, behind;
4697f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
470867a482dSJohn Dyson 
4717f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
472867a482dSJohn Dyson 				ahead = 0;
473867a482dSJohn Dyson 				behind = 0;
4742d8acc0fSJohn Dyson 			} else {
4754866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4762d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4772d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4782d8acc0fSJohn Dyson 
4794866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4802d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4812d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
482867a482dSJohn Dyson 			}
4838d8b9c6eSAlan Cox 			is_first_object_locked = FALSE;
4848d8b9c6eSAlan Cox 			if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
48540360b1bSMatthew Dillon 			     (behavior != MAP_ENTRY_BEHAV_RANDOM &&
48640360b1bSMatthew Dillon 			      fs.pindex >= fs.entry->lastr &&
4878d8b9c6eSAlan Cox 			      fs.pindex < fs.entry->lastr + VM_FAULT_READ)) &&
4888d8b9c6eSAlan Cox 			    (fs.first_object == fs.object ||
4898d8b9c6eSAlan Cox 			     (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) &&
4908d8b9c6eSAlan Cox 			    fs.first_object->type != OBJT_DEVICE) {
491867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
49240360b1bSMatthew Dillon 
49340360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
494867a482dSJohn Dyson 					firstpindex = 0;
495867a482dSJohn Dyson 				else
49640360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
497867a482dSJohn Dyson 
49815a5d210SAlan Cox 				vm_page_lock_queues();
4994221e284SAlan Cox 				/*
5004221e284SAlan Cox 				 * note: partially valid pages cannot be
5014221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
5024221e284SAlan Cox 				 * writes will barf on it badly.
5034221e284SAlan Cox 				 */
5044866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
5052100d645SPeter Wemm 					tmppindex >= firstpindex;
506867a482dSJohn Dyson 					--tmppindex) {
507867a482dSJohn Dyson 					vm_page_t mt;
508a1287949SEivind Eklund 
5094866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
510867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
511867a482dSJohn Dyson 						break;
512ff97964aSJohn Dyson 					if (mt->busy ||
5139af80719SAlan Cox 					    (mt->oflags & VPO_BUSY) ||
5149af80719SAlan Cox 					    (mt->flags & (PG_FICTITIOUS | PG_UNMANAGED)) ||
515ff97964aSJohn Dyson 						mt->hold_count ||
516867a482dSJohn Dyson 						mt->wire_count)
517867a482dSJohn Dyson 						continue;
5184fec79beSAlan Cox 					pmap_remove_all(mt);
519c6d9ef2eSAlan Cox 					if (mt->dirty) {
520867a482dSJohn Dyson 						vm_page_deactivate(mt);
521867a482dSJohn Dyson 					} else {
522867a482dSJohn Dyson 						vm_page_cache(mt);
523867a482dSJohn Dyson 					}
524867a482dSJohn Dyson 				}
52515a5d210SAlan Cox 				vm_page_unlock_queues();
526867a482dSJohn Dyson 				ahead += behind;
527867a482dSJohn Dyson 				behind = 0;
528867a482dSJohn Dyson 			}
5298d8b9c6eSAlan Cox 			if (is_first_object_locked)
5308d8b9c6eSAlan Cox 				VM_OBJECT_UNLOCK(fs.first_object);
531df8bae1dSRodney W. Grimes 			/*
5320d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5330d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5340d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5350d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5360d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5370d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5380d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5390d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5400d94caffSDavid Greenman 			 * return value is the index into the marray for the
5410d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5421c7c3c6aSMatthew Dillon 			 *
5439af80719SAlan Cox 			 * fs.m plus the additional pages are VPO_BUSY'd.
544532eadefSAlan Cox 			 *
545532eadefSAlan Cox 			 * XXX vm_fault_additional_pages() can block
546532eadefSAlan Cox 			 * without releasing the map lock.
54726f9a767SRodney W. Grimes 			 */
54805f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5494866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes 			/*
55240360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
55340360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
554532eadefSAlan Cox 			 *
555532eadefSAlan Cox 			 * XXX The following assignment modifies the map
556532eadefSAlan Cox 			 * without holding a write lock on it.
55740360b1bSMatthew Dillon 			 */
55840360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
55940360b1bSMatthew Dillon 
56040360b1bSMatthew Dillon 			/*
5610d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
5621c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
5639af80719SAlan Cox 			 * fs.object and the pages are VPO_BUSY'd.
564df8bae1dSRodney W. Grimes 			 */
5654866e085SJohn Dyson 			unlock_map(&fs);
566df8bae1dSRodney W. Grimes 
56726f9a767SRodney W. Grimes 			rv = faultcount ?
5684866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
56924a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
57022ba64e8SJohn Dyson 
57126f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
572df8bae1dSRodney W. Grimes 				/*
573f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
574f230c45cSJohn Dyson 				 * with it.
575f230c45cSJohn Dyson 				 */
576f230c45cSJohn Dyson 
577f230c45cSJohn Dyson 				/*
5780d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5790d94caffSDavid Greenman 				 * is responsible for disposition of old page
5800d94caffSDavid Greenman 				 * if moved.
581df8bae1dSRodney W. Grimes 				 */
5824866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5834866e085SJohn Dyson 				if (!fs.m) {
5844866e085SJohn Dyson 					unlock_and_deallocate(&fs);
585f6b04d2bSDavid Greenman 					goto RetryFault;
586f6b04d2bSDavid Greenman 				}
587f6b04d2bSDavid Greenman 
58826f9a767SRodney W. Grimes 				hardfault++;
5891c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
590df8bae1dSRodney W. Grimes 			}
591df8bae1dSRodney W. Grimes 			/*
5920d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5930d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5940d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
595df8bae1dSRodney W. Grimes 			 *
59624a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5970d94caffSDavid Greenman 			 * in this page.
598df8bae1dSRodney W. Grimes 			 *
5990d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
60024a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
6010d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
6020d94caffSDavid Greenman 			 * the same time that we are.
603df8bae1dSRodney W. Grimes 			 */
604a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
605f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
606f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
60726f9a767SRodney W. Grimes 			/*
608a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
60926f9a767SRodney W. Grimes 			 */
610a83c285cSDavid Greenman 			/*
6110d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6120d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6130d94caffSDavid Greenman 			 * fault w/ I/O error.
614a83c285cSDavid Greenman 			 */
6154866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
61647221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6172ad98273SAlan Cox 				vm_page_lock_queues();
6184866e085SJohn Dyson 				vm_page_free(fs.m);
6192ad98273SAlan Cox 				vm_page_unlock_queues();
6204866e085SJohn Dyson 				fs.m = NULL;
6214866e085SJohn Dyson 				unlock_and_deallocate(&fs);
622a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
62326f9a767SRodney W. Grimes 			}
6244866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6252ad98273SAlan Cox 				vm_page_lock_queues();
6264866e085SJohn Dyson 				vm_page_free(fs.m);
6272ad98273SAlan Cox 				vm_page_unlock_queues();
6284866e085SJohn Dyson 				fs.m = NULL;
62926f9a767SRodney W. Grimes 				/*
63026f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
63126f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
63226f9a767SRodney W. Grimes 				 */
633df8bae1dSRodney W. Grimes 			}
634df8bae1dSRodney W. Grimes 		}
63540360b1bSMatthew Dillon 
636df8bae1dSRodney W. Grimes 		/*
6371c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6381c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
639df8bae1dSRodney W. Grimes 		 */
6404866e085SJohn Dyson 		if (fs.object == fs.first_object)
6414866e085SJohn Dyson 			fs.first_m = fs.m;
642df8bae1dSRodney W. Grimes 
643df8bae1dSRodney W. Grimes 		/*
6440d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6450d94caffSDavid Greenman 		 * unlocking the current one.
646df8bae1dSRodney W. Grimes 		 */
6474866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6484866e085SJohn Dyson 		next_object = fs.object->backing_object;
649df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
650df8bae1dSRodney W. Grimes 			/*
6510d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6520d94caffSDavid Greenman 			 * object with zeros.
653df8bae1dSRodney W. Grimes 			 */
6544866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6554866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
656b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
657df8bae1dSRodney W. Grimes 
6584866e085SJohn Dyson 				fs.object = fs.first_object;
6594866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6604866e085SJohn Dyson 				fs.m = fs.first_m;
661f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
662df8bae1dSRodney W. Grimes 			}
6634866e085SJohn Dyson 			fs.first_m = NULL;
664df8bae1dSRodney W. Grimes 
6654221e284SAlan Cox 			/*
6664221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6674221e284SAlan Cox 			 */
6684866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
669fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6704221e284SAlan Cox 			} else {
671d8810d89SAlan Cox 				PCPU_LAZY_INC(cnt.v_ozfod);
6724221e284SAlan Cox 			}
673d8810d89SAlan Cox 			PCPU_LAZY_INC(cnt.v_zfod);
6744221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6751c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6760d94caffSDavid Greenman 		} else {
677c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
678c8567c3aSAlan Cox 			    ("object loop %p", next_object));
679c8567c3aSAlan Cox 			VM_OBJECT_LOCK(next_object);
680c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
681c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6824866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
683b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
6844866e085SJohn Dyson 			fs.object = next_object;
685df8bae1dSRodney W. Grimes 		}
686df8bae1dSRodney W. Grimes 	}
6871c7c3c6aSMatthew Dillon 
6889af80719SAlan Cox 	KASSERT((fs.m->oflags & VPO_BUSY) != 0,
6895526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes 	/*
6920d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
693df8bae1dSRodney W. Grimes 	 * is held.]
694df8bae1dSRodney W. Grimes 	 */
695df8bae1dSRodney W. Grimes 
696df8bae1dSRodney W. Grimes 	/*
6970d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6980d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6990d94caffSDavid Greenman 	 * top-level object.
700df8bae1dSRodney W. Grimes 	 */
7014866e085SJohn Dyson 	if (fs.object != fs.first_object) {
702df8bae1dSRodney W. Grimes 		/*
7030d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
704df8bae1dSRodney W. Grimes 		 */
705df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
706df8bae1dSRodney W. Grimes 			/*
7071c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7081c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7091c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7101c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7111c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7121c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7131c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7141c7c3c6aSMatthew Dillon 			 * to swap when needed.
715df8bae1dSRodney W. Grimes 			 */
716ebf75125SAlan Cox 			is_first_object_locked = FALSE;
717e50346b5SAlan Cox 			if (
718de5f6a77SJohn Dyson 				/*
719de5f6a77SJohn Dyson 				 * Only one shadow object
720de5f6a77SJohn Dyson 				 */
7214866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
722de5f6a77SJohn Dyson 				/*
723de5f6a77SJohn Dyson 				 * No COW refs, except us
724de5f6a77SJohn Dyson 				 */
7254866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
726de5f6a77SJohn Dyson 				/*
7275929bcfaSPhilippe Charnier 				 * No one else can look this object up
728de5f6a77SJohn Dyson 				 */
7294866e085SJohn Dyson 				(fs.object->handle == NULL) &&
730de5f6a77SJohn Dyson 				/*
731de5f6a77SJohn Dyson 				 * No other ways to look the object up
732de5f6a77SJohn Dyson 				 */
7334866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7344866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
735ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
736de5f6a77SJohn Dyson 				/*
737de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
738de5f6a77SJohn Dyson 				 */
739e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
740d98ddc46SAlan Cox 				vm_page_lock_queues();
7412d8acc0fSJohn Dyson 				/*
742de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
743df8bae1dSRodney W. Grimes 				 */
7444866e085SJohn Dyson 				vm_page_free(fs.first_m);
745de5f6a77SJohn Dyson 				/*
7461c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7471c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7481c7c3c6aSMatthew Dillon 				 * automatically made dirty.
749de5f6a77SJohn Dyson 				 */
7504866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
7519a96b638SAlan Cox 				vm_page_unlock_queues();
752768131d2SAlan Cox 				vm_page_busy(fs.m);
753d98ddc46SAlan Cox 				fs.first_m = fs.m;
7544866e085SJohn Dyson 				fs.m = NULL;
755d8810d89SAlan Cox 				PCPU_LAZY_INC(cnt.v_cow_optim);
756de5f6a77SJohn Dyson 			} else {
757de5f6a77SJohn Dyson 				/*
758de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
759de5f6a77SJohn Dyson 				 */
760669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
761669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
762de5f6a77SJohn Dyson 			}
7634866e085SJohn Dyson 			if (fs.m) {
764df8bae1dSRodney W. Grimes 				/*
765df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
766df8bae1dSRodney W. Grimes 				 */
7674866e085SJohn Dyson 				release_page(&fs);
768de5f6a77SJohn Dyson 			}
7691c7c3c6aSMatthew Dillon 			/*
7701c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7711c7c3c6aSMatthew Dillon 			 * conditional
7721c7c3c6aSMatthew Dillon 			 */
7734866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
774b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
775df8bae1dSRodney W. Grimes 			/*
776df8bae1dSRodney W. Grimes 			 * Only use the new page below...
777df8bae1dSRodney W. Grimes 			 */
7784866e085SJohn Dyson 			fs.object = fs.first_object;
7794866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
780d98ddc46SAlan Cox 			fs.m = fs.first_m;
781f29ba63eSAlan Cox 			if (!is_first_object_locked)
782f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
783d8810d89SAlan Cox 			PCPU_LAZY_INC(cnt.v_cow_faults);
7840d94caffSDavid Greenman 		} else {
785df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
786df8bae1dSRodney W. Grimes 		}
787df8bae1dSRodney W. Grimes 	}
788df8bae1dSRodney W. Grimes 
789df8bae1dSRodney W. Grimes 	/*
7900d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7910d94caffSDavid Greenman 	 * lookup.
792df8bae1dSRodney W. Grimes 	 */
79319dc5607STor Egge 	if (!fs.lookup_still_valid) {
794df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
795a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
796df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
797df8bae1dSRodney W. Grimes 
79819dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
799b823bbd6SMatthew Dillon 			release_page(&fs);
800b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
801b823bbd6SMatthew Dillon 			goto RetryFault;
802b823bbd6SMatthew Dillon 		}
80319dc5607STor Egge 		fs.lookup_still_valid = TRUE;
80419dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
80519dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8064866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 			/*
80944ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8100d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8110d94caffSDavid Greenman 			 * pageout will grab it eventually.
812df8bae1dSRodney W. Grimes 			 */
813df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8144866e085SJohn Dyson 				release_page(&fs);
8154866e085SJohn Dyson 				unlock_and_deallocate(&fs);
81619dc5607STor Egge 
81719dc5607STor Egge 				/*
81819dc5607STor Egge 				 * If retry of map lookup would have blocked then
81919dc5607STor Egge 				 * retry fault from start.
82019dc5607STor Egge 				 */
82119dc5607STor Egge 				if (result == KERN_FAILURE)
82219dc5607STor Egge 					goto RetryFault;
823df8bae1dSRodney W. Grimes 				return (result);
824df8bae1dSRodney W. Grimes 			}
8254866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8264866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8274866e085SJohn Dyson 				release_page(&fs);
8284866e085SJohn Dyson 				unlock_and_deallocate(&fs);
829df8bae1dSRodney W. Grimes 				goto RetryFault;
830df8bae1dSRodney W. Grimes 			}
83119dc5607STor Egge 
832df8bae1dSRodney W. Grimes 			/*
8330d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8340d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8350d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8360d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8370d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8380d94caffSDavid Greenman 			 * write-enabled after all.
839df8bae1dSRodney W. Grimes 			 */
840df8bae1dSRodney W. Grimes 			prot &= retry_prot;
841df8bae1dSRodney W. Grimes 		}
84219dc5607STor Egge 	}
8432ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
844b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8454f79d873SMatthew Dillon 
8462ddba215SDavid Greenman 		/*
8472ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8484f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8494f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8504f79d873SMatthew Dillon 		 *
851b146f9e5SAlan Cox 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
8524f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8534f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8544f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8554f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8564f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8571c7c3c6aSMatthew Dillon 		 *
8581c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8591c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8602ddba215SDavid Greenman 		 */
8614f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8624f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
863b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
8644f79d873SMatthew Dillon 		} else {
865b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
8664f79d873SMatthew Dillon 		}
8675bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8687dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
8691c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
8702ddba215SDavid Greenman 		}
8712ddba215SDavid Greenman 	}
872f6b04d2bSDavid Greenman 
873bc6d84a6SMatthew Dillon 	/*
874bc6d84a6SMatthew Dillon 	 * Page had better still be busy
875bc6d84a6SMatthew Dillon 	 */
8769af80719SAlan Cox 	KASSERT(fs.m->oflags & VPO_BUSY,
877ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
8784221e284SAlan Cox 	/*
8794221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8804221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8814221e284SAlan Cox 	 */
8824221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8834221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8844221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8854221e284SAlan Cox 	}
886eebf3286SAlan Cox 	VM_OBJECT_UNLOCK(fs.object);
887cbfbaad8SAlan Cox 
88886735996SAlan Cox 	/*
88986735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
89086735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
89186735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
89286735996SAlan Cox 	 * won't find it (yet).
89386735996SAlan Cox 	 */
8940385347cSPeter Wemm 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
8952d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
896566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
8972d8acc0fSJohn Dyson 	}
898eebf3286SAlan Cox 	VM_OBJECT_LOCK(fs.object);
8992d09a6adSAlan Cox 	vm_page_lock_queues();
900db44450bSAlan Cox 	vm_page_flag_set(fs.m, PG_REFERENCED);
901ff97964aSJohn Dyson 
902df8bae1dSRodney W. Grimes 	/*
9030d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9040d94caffSDavid Greenman 	 * can find it.
905df8bae1dSRodney W. Grimes 	 */
906a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
907df8bae1dSRodney W. Grimes 		if (wired)
9084866e085SJohn Dyson 			vm_page_wire(fs.m);
909df8bae1dSRodney W. Grimes 		else
91073007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
9110d94caffSDavid Greenman 	} else {
9124866e085SJohn Dyson 		vm_page_activate(fs.m);
91326f9a767SRodney W. Grimes 	}
9142d09a6adSAlan Cox 	vm_page_unlock_queues();
91566bdd5d6SAlan Cox 	vm_page_wakeup(fs.m);
916eeec6babSJohn Baldwin 
917eebf3286SAlan Cox 	/*
918eebf3286SAlan Cox 	 * Unlock everything, and return
919eebf3286SAlan Cox 	 */
920eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
921eeec6babSJohn Baldwin 	PROC_LOCK(curproc);
922eeec6babSJohn Baldwin 	if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
92326f9a767SRodney W. Grimes 		if (hardfault) {
92426f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
92526f9a767SRodney W. Grimes 		} else {
92626f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
92726f9a767SRodney W. Grimes 		}
92826f9a767SRodney W. Grimes 	}
929eeec6babSJohn Baldwin 	PROC_UNLOCK(curproc);
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
932df8bae1dSRodney W. Grimes }
933df8bae1dSRodney W. Grimes 
934df8bae1dSRodney W. Grimes /*
935566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
936566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
937566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
938566526a9SAlan Cox  * of mmap time.
939566526a9SAlan Cox  */
940566526a9SAlan Cox static void
941566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
942566526a9SAlan Cox {
943566526a9SAlan Cox 	int i;
944566526a9SAlan Cox 	vm_offset_t addr, starta;
945566526a9SAlan Cox 	vm_pindex_t pindex;
9462053c127SStephan Uphoff 	vm_page_t m;
947566526a9SAlan Cox 	vm_object_t object;
948566526a9SAlan Cox 
949950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
950566526a9SAlan Cox 		return;
951566526a9SAlan Cox 
952566526a9SAlan Cox 	object = entry->object.vm_object;
953566526a9SAlan Cox 
954566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
955566526a9SAlan Cox 	if (starta < entry->start) {
956566526a9SAlan Cox 		starta = entry->start;
957566526a9SAlan Cox 	} else if (starta > addra) {
958566526a9SAlan Cox 		starta = 0;
959566526a9SAlan Cox 	}
960566526a9SAlan Cox 
961566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
962566526a9SAlan Cox 		vm_object_t backing_object, lobject;
963566526a9SAlan Cox 
964566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
965566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
966566526a9SAlan Cox 			addr = 0;
967566526a9SAlan Cox 
968566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
969566526a9SAlan Cox 			continue;
970566526a9SAlan Cox 
971566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
972566526a9SAlan Cox 			continue;
973566526a9SAlan Cox 
974566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
975566526a9SAlan Cox 		lobject = object;
976566526a9SAlan Cox 		VM_OBJECT_LOCK(lobject);
977566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
978566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
979566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
980566526a9SAlan Cox 			if (lobject->backing_object_offset & PAGE_MASK)
981566526a9SAlan Cox 				break;
982566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
983566526a9SAlan Cox 			VM_OBJECT_LOCK(backing_object);
984566526a9SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
985566526a9SAlan Cox 			lobject = backing_object;
986566526a9SAlan Cox 		}
987566526a9SAlan Cox 		/*
988566526a9SAlan Cox 		 * give-up when a page is not in memory
989566526a9SAlan Cox 		 */
990cbfbaad8SAlan Cox 		if (m == NULL) {
991cbfbaad8SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
992566526a9SAlan Cox 			break;
993cbfbaad8SAlan Cox 		}
994566526a9SAlan Cox 		if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
995566526a9SAlan Cox 			(m->busy == 0) &&
9969fea8cadSAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0) {
997566526a9SAlan Cox 
998566526a9SAlan Cox 			vm_page_lock_queues();
999ef39c05bSAlexander Leidinger 			if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
100085f5b245SAlan Cox 				vm_page_deactivate(m);
10012053c127SStephan Uphoff 			pmap_enter_quick(pmap, addr, m, entry->protection);
10021f70d622SAlan Cox 			vm_page_unlock_queues();
100385f5b245SAlan Cox 		}
1004cbfbaad8SAlan Cox 		VM_OBJECT_UNLOCK(lobject);
1005566526a9SAlan Cox 	}
1006566526a9SAlan Cox }
1007566526a9SAlan Cox 
1008566526a9SAlan Cox /*
1009e3669ceeSMatthew Dillon  *	vm_fault_quick:
1010e3669ceeSMatthew Dillon  *
1011e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
1012e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
1013e3669ceeSMatthew Dillon  */
1014e3669ceeSMatthew Dillon int
1015e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
1016e3669ceeSMatthew Dillon {
1017e3669ceeSMatthew Dillon 	int r;
1018e3669ceeSMatthew Dillon 
1019e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
1020e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
1021e3669ceeSMatthew Dillon 	else
1022e3669ceeSMatthew Dillon 		r = fubyte(v);
1023e3669ceeSMatthew Dillon 	return(r);
1024e3669ceeSMatthew Dillon }
1025e3669ceeSMatthew Dillon 
1026e3669ceeSMatthew Dillon /*
1027df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1028df8bae1dSRodney W. Grimes  *
1029df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1030df8bae1dSRodney W. Grimes  */
1031df8bae1dSRodney W. Grimes int
10324be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10334be14af9SAlan Cox     boolean_t user_wire, boolean_t fictitious)
1034df8bae1dSRodney W. Grimes {
103554d92145SMatthew Dillon 	vm_offset_t va;
1036df8bae1dSRodney W. Grimes 	int rv;
1037df8bae1dSRodney W. Grimes 
1038df8bae1dSRodney W. Grimes 	/*
10390d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1040ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1041ef594d31SAlan Cox 	 * read-only sections.
1042df8bae1dSRodney W. Grimes 	 */
1043df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1044ef594d31SAlan Cox 		rv = vm_fault(map, va,
1045ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1046ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
10477aaaa4fdSJohn Dyson 		if (rv) {
10487aaaa4fdSJohn Dyson 			if (va != start)
10494be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
10507aaaa4fdSJohn Dyson 			return (rv);
10517aaaa4fdSJohn Dyson 		}
10527aaaa4fdSJohn Dyson 	}
10537aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
10547aaaa4fdSJohn Dyson }
10557aaaa4fdSJohn Dyson 
10567aaaa4fdSJohn Dyson /*
1057df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1058df8bae1dSRodney W. Grimes  *
1059df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1060df8bae1dSRodney W. Grimes  */
106126f9a767SRodney W. Grimes void
10624be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10634be14af9SAlan Cox     boolean_t fictitious)
1064df8bae1dSRodney W. Grimes {
1065227f9a1cSJake Burkholder 	vm_paddr_t pa;
1066227f9a1cSJake Burkholder 	vm_offset_t va;
106754d92145SMatthew Dillon 	pmap_t pmap;
1068df8bae1dSRodney W. Grimes 
1069df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	/*
10720d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
10730d94caffSDavid Greenman 	 * mappings from the physical map system.
1074df8bae1dSRodney W. Grimes 	 */
1075df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1076df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1077227f9a1cSJake Burkholder 		if (pa != 0) {
1078df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
10794be14af9SAlan Cox 			if (!fictitious) {
10802d09a6adSAlan Cox 				vm_page_lock_queues();
108173007561SDavid Greenman 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
10822d09a6adSAlan Cox 				vm_page_unlock_queues();
1083df8bae1dSRodney W. Grimes 			}
1084b18bfc3dSJohn Dyson 		}
10854be14af9SAlan Cox 	}
1086df8bae1dSRodney W. Grimes }
1087df8bae1dSRodney W. Grimes 
1088df8bae1dSRodney W. Grimes /*
1089df8bae1dSRodney W. Grimes  *	Routine:
1090df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1091df8bae1dSRodney W. Grimes  *	Function:
1092df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1093df8bae1dSRodney W. Grimes  *
1094df8bae1dSRodney W. Grimes  *	In/out conditions:
1095df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1096df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1097df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1098df8bae1dSRodney W. Grimes  */
109926f9a767SRodney W. Grimes void
110026f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1101df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1102df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1103df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1104df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1105df8bae1dSRodney W. Grimes {
1106c5b65a67SAlan Cox 	vm_object_t backing_object, dst_object, object;
1107df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1108a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1109a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1110c5b65a67SAlan Cox 	vm_pindex_t pindex;
1111df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1112df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1113df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1114df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1115df8bae1dSRodney W. Grimes 
1116df8bae1dSRodney W. Grimes #ifdef	lint
1117df8bae1dSRodney W. Grimes 	src_map++;
11180d94caffSDavid Greenman #endif	/* lint */
1119df8bae1dSRodney W. Grimes 
1120df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1121df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1122df8bae1dSRodney W. Grimes 
1123df8bae1dSRodney W. Grimes 	/*
11240d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
11250d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1126df8bae1dSRodney W. Grimes 	 */
112724a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
112857b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1129df8bae1dSRodney W. Grimes 
11308afcf0ccSAlan Cox 	VM_OBJECT_LOCK(dst_object);
1131df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1132df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1133df8bae1dSRodney W. Grimes 
1134df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 	/*
11370d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
11380d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
11390d94caffSDavid Greenman 	 * object.
1140df8bae1dSRodney W. Grimes 	 */
1141df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1142df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1143df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes 		/*
1146df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1147df8bae1dSRodney W. Grimes 		 */
1148df8bae1dSRodney W. Grimes 		do {
1149a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1150a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1151df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
11528afcf0ccSAlan Cox 				VM_OBJECT_UNLOCK(dst_object);
1153df8bae1dSRodney W. Grimes 				VM_WAIT;
11548afcf0ccSAlan Cox 				VM_OBJECT_LOCK(dst_object);
1155df8bae1dSRodney W. Grimes 			}
1156df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1157df8bae1dSRodney W. Grimes 
1158df8bae1dSRodney W. Grimes 		/*
1159df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
11600d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
11610d94caffSDavid Greenman 		 * memory.)
1162df8bae1dSRodney W. Grimes 		 */
1163f29ba63eSAlan Cox 		VM_OBJECT_LOCK(src_object);
1164c5b65a67SAlan Cox 		object = src_object;
1165c5b65a67SAlan Cox 		pindex = 0;
1166c5b65a67SAlan Cox 		while ((src_m = vm_page_lookup(object, pindex +
1167c5b65a67SAlan Cox 		    OFF_TO_IDX(dst_offset + src_offset))) == NULL &&
1168c5b65a67SAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) == 0 &&
1169c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1170c5b65a67SAlan Cox 			/*
1171c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1172c5b65a67SAlan Cox 			 */
1173c5b65a67SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1174c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
1175c5b65a67SAlan Cox 			VM_OBJECT_UNLOCK(object);
1176c5b65a67SAlan Cox 			object = backing_object;
1177c5b65a67SAlan Cox 		}
1178df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1179df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1180669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
1181c5b65a67SAlan Cox 		VM_OBJECT_UNLOCK(object);
1182669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
11838afcf0ccSAlan Cox 		VM_OBJECT_UNLOCK(dst_object);
1184df8bae1dSRodney W. Grimes 
1185df8bae1dSRodney W. Grimes 		/*
1186df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
1187df8bae1dSRodney W. Grimes 		 */
11880385347cSPeter Wemm 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1189df8bae1dSRodney W. Grimes 
1190df8bae1dSRodney W. Grimes 		/*
1191df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1192df8bae1dSRodney W. Grimes 		 */
119344b8bd66SAlan Cox 		VM_OBJECT_LOCK(dst_object);
119444b8bd66SAlan Cox 		vm_page_lock_queues();
1195df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
11964abd55b2SAlan Cox 		vm_page_unlock_queues();
119766bdd5d6SAlan Cox 		vm_page_wakeup(dst_m);
1198df8bae1dSRodney W. Grimes 	}
11998afcf0ccSAlan Cox 	VM_OBJECT_UNLOCK(dst_object);
1200df8bae1dSRodney W. Grimes }
120126f9a767SRodney W. Grimes 
120226f9a767SRodney W. Grimes 
120326f9a767SRodney W. Grimes /*
120426f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
120522ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
120622ba64e8SJohn Dyson  * pages for the pages to be paged in.
120726f9a767SRodney W. Grimes  *
120826f9a767SRodney W. Grimes  * Inputs:
120922ba64e8SJohn Dyson  *	m, rbehind, rahead
121026f9a767SRodney W. Grimes  *
121126f9a767SRodney W. Grimes  * Outputs:
121226f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
121326f9a767SRodney W. Grimes  *
121426f9a767SRodney W. Grimes  * Return value:
121526f9a767SRodney W. Grimes  *  number of pages in marray
121623955314SAlfred Perlstein  *
121723955314SAlfred Perlstein  * This routine can't block.
121826f9a767SRodney W. Grimes  */
1219303b270bSEivind Eklund static int
122022ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
122126f9a767SRodney W. Grimes 	vm_page_t m;
122226f9a767SRodney W. Grimes 	int rbehind;
122322ba64e8SJohn Dyson 	int rahead;
122426f9a767SRodney W. Grimes 	vm_page_t *marray;
122526f9a767SRodney W. Grimes 	int *reqpage;
122626f9a767SRodney W. Grimes {
12272d8acc0fSJohn Dyson 	int i,j;
122826f9a767SRodney W. Grimes 	vm_object_t object;
1229a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
123026f9a767SRodney W. Grimes 	vm_page_t rtm;
1231170db9c6SJohn Dyson 	int cbehind, cahead;
123226f9a767SRodney W. Grimes 
1233f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
123423955314SAlfred Perlstein 
123526f9a767SRodney W. Grimes 	object = m->object;
1236a316d390SJohn Dyson 	pindex = m->pindex;
123726f9a767SRodney W. Grimes 
123826f9a767SRodney W. Grimes 	/*
1239f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1240f35329acSJohn Dyson 	 */
1241f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1242f35329acSJohn Dyson 		*reqpage = 0;
1243f35329acSJohn Dyson 		marray[0] = m;
1244f35329acSJohn Dyson 		return 1;
1245f35329acSJohn Dyson 	}
1246f35329acSJohn Dyson 
1247fcdd9721SPawel Jakub Dawidek 	cbehind = cahead = 0;
1248fcdd9721SPawel Jakub Dawidek 
1249f35329acSJohn Dyson 	/*
125026f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
125126f9a767SRodney W. Grimes 	 */
12521c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
125326f9a767SRodney W. Grimes 		return 0;
12542d8acc0fSJohn Dyson 	}
125526f9a767SRodney W. Grimes 
125622ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
125722ba64e8SJohn Dyson 		*reqpage = 0;
125822ba64e8SJohn Dyson 		marray[0] = m;
125922ba64e8SJohn Dyson 		return 1;
1260170db9c6SJohn Dyson 	}
126122ba64e8SJohn Dyson 
126222ba64e8SJohn Dyson 	if (rahead > cahead) {
126322ba64e8SJohn Dyson 		rahead = cahead;
126422ba64e8SJohn Dyson 	}
126522ba64e8SJohn Dyson 
1266170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1267170db9c6SJohn Dyson 		rbehind = cbehind;
1268170db9c6SJohn Dyson 	}
1269170db9c6SJohn Dyson 
127026f9a767SRodney W. Grimes 	/*
127126f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
127226f9a767SRodney W. Grimes 	 */
1273ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
127422ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1275f919ebdeSDavid Greenman 		pagedaemon_wakeup();
127626f9a767SRodney W. Grimes 		marray[0] = m;
12772d8acc0fSJohn Dyson 		*reqpage = 0;
127826f9a767SRodney W. Grimes 		return 1;
127926f9a767SRodney W. Grimes 	}
128022ba64e8SJohn Dyson 
128126f9a767SRodney W. Grimes 	/*
12822d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
128326f9a767SRodney W. Grimes 	 */
12842d8acc0fSJohn Dyson 	if (pindex > 0) {
12852d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1286a316d390SJohn Dyson 			rbehind = pindex;
12872d8acc0fSJohn Dyson 			startpindex = 0;
12882d8acc0fSJohn Dyson 		} else {
1289a316d390SJohn Dyson 			startpindex = pindex - rbehind;
12902d8acc0fSJohn Dyson 		}
12912d8acc0fSJohn Dyson 
12928f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
12938f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
12948f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
129526f9a767SRodney W. Grimes 
12962d8acc0fSJohn Dyson 		for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
129726f9a767SRodney W. Grimes 
12982d8acc0fSJohn Dyson 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1299ccbb2f72SJohn Dyson 			if (rtm == NULL) {
13002ad98273SAlan Cox 				vm_page_lock_queues();
1301ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
13024866e085SJohn Dyson 					vm_page_free(marray[j]);
130326f9a767SRodney W. Grimes 				}
13042ad98273SAlan Cox 				vm_page_unlock_queues();
130526f9a767SRodney W. Grimes 				marray[0] = m;
13062d8acc0fSJohn Dyson 				*reqpage = 0;
130726f9a767SRodney W. Grimes 				return 1;
130826f9a767SRodney W. Grimes 			}
1309170db9c6SJohn Dyson 
13102d8acc0fSJohn Dyson 			marray[i] = rtm;
131126f9a767SRodney W. Grimes 		}
13122d8acc0fSJohn Dyson 	} else {
13132d8acc0fSJohn Dyson 		startpindex = 0;
13142d8acc0fSJohn Dyson 		i = 0;
13152d8acc0fSJohn Dyson 	}
13162d8acc0fSJohn Dyson 
13172d8acc0fSJohn Dyson 	marray[i] = m;
13182d8acc0fSJohn Dyson 	/* page offset of the required page */
13192d8acc0fSJohn Dyson 	*reqpage = i;
13202d8acc0fSJohn Dyson 
13212d8acc0fSJohn Dyson 	tpindex = pindex + 1;
13222d8acc0fSJohn Dyson 	i++;
13232d8acc0fSJohn Dyson 
13242d8acc0fSJohn Dyson 	/*
13252d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
13262d8acc0fSJohn Dyson 	 */
13272d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
13288f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
13298f8790a7SAlan Cox 		endpindex = rtm->pindex;
13302d8acc0fSJohn Dyson 	if (endpindex > object->size)
13312d8acc0fSJohn Dyson 		endpindex = object->size;
13322d8acc0fSJohn Dyson 
13332d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
13342d8acc0fSJohn Dyson 
13352d8acc0fSJohn Dyson 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
13362d8acc0fSJohn Dyson 		if (rtm == NULL) {
13372d8acc0fSJohn Dyson 			break;
13382d8acc0fSJohn Dyson 		}
13392d8acc0fSJohn Dyson 
13402d8acc0fSJohn Dyson 		marray[i] = rtm;
13412d8acc0fSJohn Dyson 	}
13422d8acc0fSJohn Dyson 
13432d8acc0fSJohn Dyson 	/* return number of bytes of pages */
13442d8acc0fSJohn Dyson 	return i;
134526f9a767SRodney W. Grimes }
1346