xref: /freebsd/sys/vm/vm_fault.c (revision b146f9e5d29e6b1045d8e7612875884338297a38)
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 {
1362ad98273SAlan Cox 	vm_page_lock_queues();
137e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
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;
22292de35b0SAlan Cox 	atomic_add_int(&cnt.v_vm_faults, 1);
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
3541c7c3c6aSMatthew Dillon 			 * if the page is busy via either PG_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 			 */
3681c7c3c6aSMatthew Dillon 			if ((fs.m->flags & PG_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);
397c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_intrans, 1);
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 			}
4137615edaaSMatthew Dillon 
4141c7c3c6aSMatthew Dillon 			/*
4151c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4161c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4171c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4181c7c3c6aSMatthew Dillon 			 * found the page ).
4191c7c3c6aSMatthew Dillon 			 */
420e69763a3SDoug Rabson 			vm_page_busy(fs.m);
4214abd55b2SAlan Cox 			vm_page_unlock_queues();
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 ||
5138b03c8edSMatthew Dillon 						(mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
514ff97964aSJohn Dyson 						mt->hold_count ||
515867a482dSJohn Dyson 						mt->wire_count)
516867a482dSJohn Dyson 						continue;
5174fec79beSAlan Cox 					pmap_remove_all(mt);
518c6d9ef2eSAlan Cox 					if (mt->dirty) {
519867a482dSJohn Dyson 						vm_page_deactivate(mt);
520867a482dSJohn Dyson 					} else {
521867a482dSJohn Dyson 						vm_page_cache(mt);
522867a482dSJohn Dyson 					}
523867a482dSJohn Dyson 				}
52415a5d210SAlan Cox 				vm_page_unlock_queues();
525867a482dSJohn Dyson 				ahead += behind;
526867a482dSJohn Dyson 				behind = 0;
527867a482dSJohn Dyson 			}
5288d8b9c6eSAlan Cox 			if (is_first_object_locked)
5298d8b9c6eSAlan Cox 				VM_OBJECT_UNLOCK(fs.first_object);
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 			 *
5421c7c3c6aSMatthew Dillon 			 * fs.m plus the additional pages are PG_BUSY'd.
543532eadefSAlan Cox 			 *
544532eadefSAlan Cox 			 * XXX vm_fault_additional_pages() can block
545532eadefSAlan Cox 			 * without releasing the map lock.
54626f9a767SRodney W. Grimes 			 */
54705f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5484866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
549df8bae1dSRodney W. Grimes 
550df8bae1dSRodney W. Grimes 			/*
55140360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
55240360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
553532eadefSAlan Cox 			 *
554532eadefSAlan Cox 			 * XXX The following assignment modifies the map
555532eadefSAlan Cox 			 * without holding a write lock on it.
55640360b1bSMatthew Dillon 			 */
55740360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
55840360b1bSMatthew Dillon 
55940360b1bSMatthew Dillon 			/*
5600d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
5611c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
5621c7c3c6aSMatthew Dillon 			 * fs.object and the pages are PG_BUSY'd.
563df8bae1dSRodney W. Grimes 			 */
5644866e085SJohn Dyson 			unlock_map(&fs);
565df8bae1dSRodney W. Grimes 
56626f9a767SRodney W. Grimes 			rv = faultcount ?
5674866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
56824a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
56922ba64e8SJohn Dyson 
57026f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
571df8bae1dSRodney W. Grimes 				/*
572f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
573f230c45cSJohn Dyson 				 * with it.
574f230c45cSJohn Dyson 				 */
575f230c45cSJohn Dyson 
576f230c45cSJohn Dyson 				/*
5770d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5780d94caffSDavid Greenman 				 * is responsible for disposition of old page
5790d94caffSDavid Greenman 				 * if moved.
580df8bae1dSRodney W. Grimes 				 */
5814866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5824866e085SJohn Dyson 				if (!fs.m) {
5834866e085SJohn Dyson 					unlock_and_deallocate(&fs);
584f6b04d2bSDavid Greenman 					goto RetryFault;
585f6b04d2bSDavid Greenman 				}
586f6b04d2bSDavid Greenman 
58726f9a767SRodney W. Grimes 				hardfault++;
5881c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
589df8bae1dSRodney W. Grimes 			}
590df8bae1dSRodney W. Grimes 			/*
5910d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5920d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5930d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
594df8bae1dSRodney W. Grimes 			 *
59524a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5960d94caffSDavid Greenman 			 * in this page.
597df8bae1dSRodney W. Grimes 			 *
5980d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
59924a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
6000d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
6010d94caffSDavid Greenman 			 * the same time that we are.
602df8bae1dSRodney W. Grimes 			 */
603a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
604f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
605f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
60626f9a767SRodney W. Grimes 			/*
607a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
60826f9a767SRodney W. Grimes 			 */
609a83c285cSDavid Greenman 			/*
6100d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6110d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6120d94caffSDavid Greenman 			 * fault w/ I/O error.
613a83c285cSDavid Greenman 			 */
6144866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
61547221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6162ad98273SAlan Cox 				vm_page_lock_queues();
6174866e085SJohn Dyson 				vm_page_free(fs.m);
6182ad98273SAlan Cox 				vm_page_unlock_queues();
6194866e085SJohn Dyson 				fs.m = NULL;
6204866e085SJohn Dyson 				unlock_and_deallocate(&fs);
621a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
62226f9a767SRodney W. Grimes 			}
6234866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6242ad98273SAlan Cox 				vm_page_lock_queues();
6254866e085SJohn Dyson 				vm_page_free(fs.m);
6262ad98273SAlan Cox 				vm_page_unlock_queues();
6274866e085SJohn Dyson 				fs.m = NULL;
62826f9a767SRodney W. Grimes 				/*
62926f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
63026f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
63126f9a767SRodney W. Grimes 				 */
632df8bae1dSRodney W. Grimes 			}
633df8bae1dSRodney W. Grimes 		}
63440360b1bSMatthew Dillon 
635df8bae1dSRodney W. Grimes 		/*
6361c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6371c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
638df8bae1dSRodney W. Grimes 		 */
6394866e085SJohn Dyson 		if (fs.object == fs.first_object)
6404866e085SJohn Dyson 			fs.first_m = fs.m;
641df8bae1dSRodney W. Grimes 
642df8bae1dSRodney W. Grimes 		/*
6430d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6440d94caffSDavid Greenman 		 * unlocking the current one.
645df8bae1dSRodney W. Grimes 		 */
6464866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6474866e085SJohn Dyson 		next_object = fs.object->backing_object;
648df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
649df8bae1dSRodney W. Grimes 			/*
6500d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6510d94caffSDavid Greenman 			 * object with zeros.
652df8bae1dSRodney W. Grimes 			 */
6534866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6544866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
655b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
656df8bae1dSRodney W. Grimes 
6574866e085SJohn Dyson 				fs.object = fs.first_object;
6584866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6594866e085SJohn Dyson 				fs.m = fs.first_m;
660f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
661df8bae1dSRodney W. Grimes 			}
6624866e085SJohn Dyson 			fs.first_m = NULL;
663df8bae1dSRodney W. Grimes 
6644221e284SAlan Cox 			/*
6654221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6664221e284SAlan Cox 			 */
6674866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
668fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6694221e284SAlan Cox 			} else {
670c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_ozfod, 1);
6714221e284SAlan Cox 			}
672c1fbc251SAlan Cox 			atomic_add_int(&cnt.v_zfod, 1);
6734221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6741c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6750d94caffSDavid Greenman 		} else {
676c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
677c8567c3aSAlan Cox 			    ("object loop %p", next_object));
678c8567c3aSAlan Cox 			VM_OBJECT_LOCK(next_object);
679c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
680c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6814866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
682b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
6834866e085SJohn Dyson 			fs.object = next_object;
684df8bae1dSRodney W. Grimes 		}
685df8bae1dSRodney W. Grimes 	}
6861c7c3c6aSMatthew Dillon 
6875526d2d9SEivind Eklund 	KASSERT((fs.m->flags & PG_BUSY) != 0,
6885526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	/*
6910d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
692df8bae1dSRodney W. Grimes 	 * is held.]
693df8bae1dSRodney W. Grimes 	 */
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	/*
6960d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6970d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6980d94caffSDavid Greenman 	 * top-level object.
699df8bae1dSRodney W. Grimes 	 */
7004866e085SJohn Dyson 	if (fs.object != fs.first_object) {
701df8bae1dSRodney W. Grimes 		/*
7020d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
703df8bae1dSRodney W. Grimes 		 */
704df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
705df8bae1dSRodney W. Grimes 			/*
7061c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7071c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7081c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7091c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7101c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7111c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7121c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7131c7c3c6aSMatthew Dillon 			 * to swap when needed.
714df8bae1dSRodney W. Grimes 			 */
715ebf75125SAlan Cox 			is_first_object_locked = FALSE;
716e50346b5SAlan Cox 			if (
717de5f6a77SJohn Dyson 				/*
718de5f6a77SJohn Dyson 				 * Only one shadow object
719de5f6a77SJohn Dyson 				 */
7204866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
721de5f6a77SJohn Dyson 				/*
722de5f6a77SJohn Dyson 				 * No COW refs, except us
723de5f6a77SJohn Dyson 				 */
7244866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
725de5f6a77SJohn Dyson 				/*
7265929bcfaSPhilippe Charnier 				 * No one else can look this object up
727de5f6a77SJohn Dyson 				 */
7284866e085SJohn Dyson 				(fs.object->handle == NULL) &&
729de5f6a77SJohn Dyson 				/*
730de5f6a77SJohn Dyson 				 * No other ways to look the object up
731de5f6a77SJohn Dyson 				 */
7324866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7334866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
734ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
735de5f6a77SJohn Dyson 				/*
736de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
737de5f6a77SJohn Dyson 				 */
738e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
739d98ddc46SAlan Cox 				vm_page_lock_queues();
7402d8acc0fSJohn Dyson 				/*
741de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
742df8bae1dSRodney W. Grimes 				 */
7434866e085SJohn Dyson 				vm_page_free(fs.first_m);
744de5f6a77SJohn Dyson 				/*
7451c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7461c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7471c7c3c6aSMatthew Dillon 				 * automatically made dirty.
748de5f6a77SJohn Dyson 				 */
7494866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
750d98ddc46SAlan Cox 				vm_page_busy(fs.m);
7519a96b638SAlan Cox 				vm_page_unlock_queues();
752d98ddc46SAlan Cox 				fs.first_m = fs.m;
7534866e085SJohn Dyson 				fs.m = NULL;
754c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_cow_optim, 1);
755de5f6a77SJohn Dyson 			} else {
756de5f6a77SJohn Dyson 				/*
757de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
758de5f6a77SJohn Dyson 				 */
759669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
760669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
761de5f6a77SJohn Dyson 			}
7624866e085SJohn Dyson 			if (fs.m) {
763df8bae1dSRodney W. Grimes 				/*
764df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
765df8bae1dSRodney W. Grimes 				 */
7664866e085SJohn Dyson 				release_page(&fs);
767de5f6a77SJohn Dyson 			}
7681c7c3c6aSMatthew Dillon 			/*
7691c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7701c7c3c6aSMatthew Dillon 			 * conditional
7711c7c3c6aSMatthew Dillon 			 */
7724866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
773b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
774df8bae1dSRodney W. Grimes 			/*
775df8bae1dSRodney W. Grimes 			 * Only use the new page below...
776df8bae1dSRodney W. Grimes 			 */
7774866e085SJohn Dyson 			fs.object = fs.first_object;
7784866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
779d98ddc46SAlan Cox 			fs.m = fs.first_m;
780f29ba63eSAlan Cox 			if (!is_first_object_locked)
781f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
782c1fbc251SAlan Cox 			atomic_add_int(&cnt.v_cow_faults, 1);
7830d94caffSDavid Greenman 		} else {
784df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
785df8bae1dSRodney W. Grimes 		}
786df8bae1dSRodney W. Grimes 	}
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	/*
7890d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7900d94caffSDavid Greenman 	 * lookup.
791df8bae1dSRodney W. Grimes 	 */
79219dc5607STor Egge 	if (!fs.lookup_still_valid) {
793df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
794a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
795df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
796df8bae1dSRodney W. Grimes 
79719dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
798b823bbd6SMatthew Dillon 			release_page(&fs);
799b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
800b823bbd6SMatthew Dillon 			goto RetryFault;
801b823bbd6SMatthew Dillon 		}
80219dc5607STor Egge 		fs.lookup_still_valid = TRUE;
80319dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
80419dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8054866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes 			/*
80844ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8090d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8100d94caffSDavid Greenman 			 * pageout will grab it eventually.
811df8bae1dSRodney W. Grimes 			 */
812df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8134866e085SJohn Dyson 				release_page(&fs);
8144866e085SJohn Dyson 				unlock_and_deallocate(&fs);
81519dc5607STor Egge 
81619dc5607STor Egge 				/*
81719dc5607STor Egge 				 * If retry of map lookup would have blocked then
81819dc5607STor Egge 				 * retry fault from start.
81919dc5607STor Egge 				 */
82019dc5607STor Egge 				if (result == KERN_FAILURE)
82119dc5607STor Egge 					goto RetryFault;
822df8bae1dSRodney W. Grimes 				return (result);
823df8bae1dSRodney W. Grimes 			}
8244866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8254866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8264866e085SJohn Dyson 				release_page(&fs);
8274866e085SJohn Dyson 				unlock_and_deallocate(&fs);
828df8bae1dSRodney W. Grimes 				goto RetryFault;
829df8bae1dSRodney W. Grimes 			}
83019dc5607STor Egge 
831df8bae1dSRodney W. Grimes 			/*
8320d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8330d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8340d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8350d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8360d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8370d94caffSDavid Greenman 			 * write-enabled after all.
838df8bae1dSRodney W. Grimes 			 */
839df8bae1dSRodney W. Grimes 			prot &= retry_prot;
840df8bae1dSRodney W. Grimes 		}
84119dc5607STor Egge 	}
8422ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
8439a96b638SAlan Cox 		vm_page_lock_queues();
844e69763a3SDoug Rabson 		vm_page_flag_set(fs.m, PG_WRITEABLE);
845b146f9e5SAlan Cox 		vm_page_unlock_queues();
846b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8474f79d873SMatthew Dillon 
8482ddba215SDavid Greenman 		/*
8492ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8504f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8514f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8524f79d873SMatthew Dillon 		 *
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.
8591c7c3c6aSMatthew Dillon 		 *
8601c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8611c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8622ddba215SDavid Greenman 		 */
8634f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8644f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
865b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
8664f79d873SMatthew Dillon 		} else {
867b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
8684f79d873SMatthew Dillon 		}
8695bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8707dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
8711c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
8722ddba215SDavid Greenman 		}
8732ddba215SDavid Greenman 	}
874f6b04d2bSDavid Greenman 
875bc6d84a6SMatthew Dillon 	/*
876bc6d84a6SMatthew Dillon 	 * Page had better still be busy
877bc6d84a6SMatthew Dillon 	 */
878ca06c247SAlan Cox 	KASSERT(fs.m->flags & PG_BUSY,
879ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
8804221e284SAlan Cox 	/*
8814221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8824221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8834221e284SAlan Cox 	 */
8844221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8854221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8864221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8874221e284SAlan Cox 	}
888eebf3286SAlan Cox 	VM_OBJECT_UNLOCK(fs.object);
889cbfbaad8SAlan Cox 
89086735996SAlan Cox 	/*
89186735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
89286735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
89386735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
89486735996SAlan Cox 	 * won't find it (yet).
89586735996SAlan Cox 	 */
8960385347cSPeter Wemm 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
8972d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
898566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
8992d8acc0fSJohn Dyson 	}
900eebf3286SAlan Cox 	VM_OBJECT_LOCK(fs.object);
9012d09a6adSAlan Cox 	vm_page_lock_queues();
902db44450bSAlan Cox 	vm_page_flag_set(fs.m, PG_REFERENCED);
903ff97964aSJohn Dyson 
904df8bae1dSRodney W. Grimes 	/*
9050d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9060d94caffSDavid Greenman 	 * can find it.
907df8bae1dSRodney W. Grimes 	 */
908a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
909df8bae1dSRodney W. Grimes 		if (wired)
9104866e085SJohn Dyson 			vm_page_wire(fs.m);
911df8bae1dSRodney W. Grimes 		else
91273007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
9130d94caffSDavid Greenman 	} else {
9144866e085SJohn Dyson 		vm_page_activate(fs.m);
91526f9a767SRodney W. Grimes 	}
916a9911f9aSAlan Cox 	vm_page_wakeup(fs.m);
9172d09a6adSAlan Cox 	vm_page_unlock_queues();
918eeec6babSJohn Baldwin 
919eebf3286SAlan Cox 	/*
920eebf3286SAlan Cox 	 * Unlock everything, and return
921eebf3286SAlan Cox 	 */
922eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
923eeec6babSJohn Baldwin 	PROC_LOCK(curproc);
924eeec6babSJohn Baldwin 	if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
92526f9a767SRodney W. Grimes 		if (hardfault) {
92626f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
92726f9a767SRodney W. Grimes 		} else {
92826f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
92926f9a767SRodney W. Grimes 		}
93026f9a767SRodney W. Grimes 	}
931eeec6babSJohn Baldwin 	PROC_UNLOCK(curproc);
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
934df8bae1dSRodney W. Grimes }
935df8bae1dSRodney W. Grimes 
936df8bae1dSRodney W. Grimes /*
937566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
938566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
939566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
940566526a9SAlan Cox  * of mmap time.
941566526a9SAlan Cox  */
942566526a9SAlan Cox static void
943566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
944566526a9SAlan Cox {
945566526a9SAlan Cox 	int i;
946566526a9SAlan Cox 	vm_offset_t addr, starta;
947566526a9SAlan Cox 	vm_pindex_t pindex;
9482053c127SStephan Uphoff 	vm_page_t m;
949566526a9SAlan Cox 	vm_object_t object;
950566526a9SAlan Cox 
951950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
952566526a9SAlan Cox 		return;
953566526a9SAlan Cox 
954566526a9SAlan Cox 	object = entry->object.vm_object;
955566526a9SAlan Cox 
956566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
957566526a9SAlan Cox 	if (starta < entry->start) {
958566526a9SAlan Cox 		starta = entry->start;
959566526a9SAlan Cox 	} else if (starta > addra) {
960566526a9SAlan Cox 		starta = 0;
961566526a9SAlan Cox 	}
962566526a9SAlan Cox 
963566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
964566526a9SAlan Cox 		vm_object_t backing_object, lobject;
965566526a9SAlan Cox 
966566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
967566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
968566526a9SAlan Cox 			addr = 0;
969566526a9SAlan Cox 
970566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
971566526a9SAlan Cox 			continue;
972566526a9SAlan Cox 
973566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
974566526a9SAlan Cox 			continue;
975566526a9SAlan Cox 
976566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
977566526a9SAlan Cox 		lobject = object;
978566526a9SAlan Cox 		VM_OBJECT_LOCK(lobject);
979566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
980566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
981566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
982566526a9SAlan Cox 			if (lobject->backing_object_offset & PAGE_MASK)
983566526a9SAlan Cox 				break;
984566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
985566526a9SAlan Cox 			VM_OBJECT_LOCK(backing_object);
986566526a9SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
987566526a9SAlan Cox 			lobject = backing_object;
988566526a9SAlan Cox 		}
989566526a9SAlan Cox 		/*
990566526a9SAlan Cox 		 * give-up when a page is not in memory
991566526a9SAlan Cox 		 */
992cbfbaad8SAlan Cox 		if (m == NULL) {
993cbfbaad8SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
994566526a9SAlan Cox 			break;
995cbfbaad8SAlan Cox 		}
996566526a9SAlan Cox 		if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
997566526a9SAlan Cox 			(m->busy == 0) &&
998566526a9SAlan Cox 		    (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
999566526a9SAlan Cox 
1000566526a9SAlan Cox 			vm_page_lock_queues();
1001ef39c05bSAlexander Leidinger 			if (VM_PAGE_INQUEUE1(m, PQ_CACHE))
100285f5b245SAlan Cox 				vm_page_deactivate(m);
10032053c127SStephan Uphoff 			pmap_enter_quick(pmap, addr, m, entry->protection);
10041f70d622SAlan Cox 			vm_page_unlock_queues();
100585f5b245SAlan Cox 		}
1006cbfbaad8SAlan Cox 		VM_OBJECT_UNLOCK(lobject);
1007566526a9SAlan Cox 	}
1008566526a9SAlan Cox }
1009566526a9SAlan Cox 
1010566526a9SAlan Cox /*
1011e3669ceeSMatthew Dillon  *	vm_fault_quick:
1012e3669ceeSMatthew Dillon  *
1013e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
1014e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
1015e3669ceeSMatthew Dillon  */
1016e3669ceeSMatthew Dillon int
1017e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
1018e3669ceeSMatthew Dillon {
1019e3669ceeSMatthew Dillon 	int r;
1020e3669ceeSMatthew Dillon 
1021e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
1022e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
1023e3669ceeSMatthew Dillon 	else
1024e3669ceeSMatthew Dillon 		r = fubyte(v);
1025e3669ceeSMatthew Dillon 	return(r);
1026e3669ceeSMatthew Dillon }
1027e3669ceeSMatthew Dillon 
1028e3669ceeSMatthew Dillon /*
1029df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1030df8bae1dSRodney W. Grimes  *
1031df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1032df8bae1dSRodney W. Grimes  */
1033df8bae1dSRodney W. Grimes int
10344be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10354be14af9SAlan Cox     boolean_t user_wire, boolean_t fictitious)
1036df8bae1dSRodney W. Grimes {
103754d92145SMatthew Dillon 	vm_offset_t va;
1038df8bae1dSRodney W. Grimes 	int rv;
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 	/*
10410d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1042ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1043ef594d31SAlan Cox 	 * read-only sections.
1044df8bae1dSRodney W. Grimes 	 */
1045df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1046ef594d31SAlan Cox 		rv = vm_fault(map, va,
1047ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1048ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
10497aaaa4fdSJohn Dyson 		if (rv) {
10507aaaa4fdSJohn Dyson 			if (va != start)
10514be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
10527aaaa4fdSJohn Dyson 			return (rv);
10537aaaa4fdSJohn Dyson 		}
10547aaaa4fdSJohn Dyson 	}
10557aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
10567aaaa4fdSJohn Dyson }
10577aaaa4fdSJohn Dyson 
10587aaaa4fdSJohn Dyson /*
1059df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1060df8bae1dSRodney W. Grimes  *
1061df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1062df8bae1dSRodney W. Grimes  */
106326f9a767SRodney W. Grimes void
10644be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10654be14af9SAlan Cox     boolean_t fictitious)
1066df8bae1dSRodney W. Grimes {
1067227f9a1cSJake Burkholder 	vm_paddr_t pa;
1068227f9a1cSJake Burkholder 	vm_offset_t va;
106954d92145SMatthew Dillon 	pmap_t pmap;
1070df8bae1dSRodney W. Grimes 
1071df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1072df8bae1dSRodney W. Grimes 
1073df8bae1dSRodney W. Grimes 	/*
10740d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
10750d94caffSDavid Greenman 	 * mappings from the physical map system.
1076df8bae1dSRodney W. Grimes 	 */
1077df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1078df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1079227f9a1cSJake Burkholder 		if (pa != 0) {
1080df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
10814be14af9SAlan Cox 			if (!fictitious) {
10822d09a6adSAlan Cox 				vm_page_lock_queues();
108373007561SDavid Greenman 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
10842d09a6adSAlan Cox 				vm_page_unlock_queues();
1085df8bae1dSRodney W. Grimes 			}
1086b18bfc3dSJohn Dyson 		}
10874be14af9SAlan Cox 	}
1088df8bae1dSRodney W. Grimes }
1089df8bae1dSRodney W. Grimes 
1090df8bae1dSRodney W. Grimes /*
1091df8bae1dSRodney W. Grimes  *	Routine:
1092df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1093df8bae1dSRodney W. Grimes  *	Function:
1094df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1095df8bae1dSRodney W. Grimes  *
1096df8bae1dSRodney W. Grimes  *	In/out conditions:
1097df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1098df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1099df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1100df8bae1dSRodney W. Grimes  */
110126f9a767SRodney W. Grimes void
110226f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1103df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1104df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1105df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1106df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1107df8bae1dSRodney W. Grimes {
1108c5b65a67SAlan Cox 	vm_object_t backing_object, dst_object, object;
1109df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1110a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1111a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1112c5b65a67SAlan Cox 	vm_pindex_t pindex;
1113df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1114df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1115df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1116df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes #ifdef	lint
1119df8bae1dSRodney W. Grimes 	src_map++;
11200d94caffSDavid Greenman #endif	/* lint */
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1123df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1124df8bae1dSRodney W. Grimes 
1125df8bae1dSRodney W. Grimes 	/*
11260d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
11270d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1128df8bae1dSRodney W. Grimes 	 */
112924a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
113057b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1131df8bae1dSRodney W. Grimes 
11328afcf0ccSAlan Cox 	VM_OBJECT_LOCK(dst_object);
1133df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1134df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1135df8bae1dSRodney W. Grimes 
1136df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1137df8bae1dSRodney W. Grimes 
1138df8bae1dSRodney W. Grimes 	/*
11390d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
11400d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
11410d94caffSDavid Greenman 	 * object.
1142df8bae1dSRodney W. Grimes 	 */
1143df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1144df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1145df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1146df8bae1dSRodney W. Grimes 
1147df8bae1dSRodney W. Grimes 		/*
1148df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1149df8bae1dSRodney W. Grimes 		 */
1150df8bae1dSRodney W. Grimes 		do {
1151a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1152a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1153df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
11548afcf0ccSAlan Cox 				VM_OBJECT_UNLOCK(dst_object);
1155df8bae1dSRodney W. Grimes 				VM_WAIT;
11568afcf0ccSAlan Cox 				VM_OBJECT_LOCK(dst_object);
1157df8bae1dSRodney W. Grimes 			}
1158df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1159df8bae1dSRodney W. Grimes 
1160df8bae1dSRodney W. Grimes 		/*
1161df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
11620d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
11630d94caffSDavid Greenman 		 * memory.)
1164df8bae1dSRodney W. Grimes 		 */
1165f29ba63eSAlan Cox 		VM_OBJECT_LOCK(src_object);
1166c5b65a67SAlan Cox 		object = src_object;
1167c5b65a67SAlan Cox 		pindex = 0;
1168c5b65a67SAlan Cox 		while ((src_m = vm_page_lookup(object, pindex +
1169c5b65a67SAlan Cox 		    OFF_TO_IDX(dst_offset + src_offset))) == NULL &&
1170c5b65a67SAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) == 0 &&
1171c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1172c5b65a67SAlan Cox 			/*
1173c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1174c5b65a67SAlan Cox 			 */
1175c5b65a67SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1176c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
1177c5b65a67SAlan Cox 			VM_OBJECT_UNLOCK(object);
1178c5b65a67SAlan Cox 			object = backing_object;
1179c5b65a67SAlan Cox 		}
1180df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1181df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1182669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
1183c5b65a67SAlan Cox 		VM_OBJECT_UNLOCK(object);
1184669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
11858afcf0ccSAlan Cox 		VM_OBJECT_UNLOCK(dst_object);
1186df8bae1dSRodney W. Grimes 
1187df8bae1dSRodney W. Grimes 		/*
1188df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
1189df8bae1dSRodney W. Grimes 		 */
11900385347cSPeter Wemm 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
11918afcf0ccSAlan Cox 		VM_OBJECT_LOCK(dst_object);
11924abd55b2SAlan Cox 		vm_page_lock_queues();
1193c5b65a67SAlan Cox 		if ((prot & VM_PROT_WRITE) != 0)
1194db44450bSAlan Cox 			vm_page_flag_set(dst_m, PG_WRITEABLE);
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 		/*
1197df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1198df8bae1dSRodney W. Grimes 		 */
1199df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
1200e69763a3SDoug Rabson 		vm_page_wakeup(dst_m);
12014abd55b2SAlan Cox 		vm_page_unlock_queues();
1202df8bae1dSRodney W. Grimes 	}
12038afcf0ccSAlan Cox 	VM_OBJECT_UNLOCK(dst_object);
1204df8bae1dSRodney W. Grimes }
120526f9a767SRodney W. Grimes 
120626f9a767SRodney W. Grimes 
120726f9a767SRodney W. Grimes /*
120826f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
120922ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
121022ba64e8SJohn Dyson  * pages for the pages to be paged in.
121126f9a767SRodney W. Grimes  *
121226f9a767SRodney W. Grimes  * Inputs:
121322ba64e8SJohn Dyson  *	m, rbehind, rahead
121426f9a767SRodney W. Grimes  *
121526f9a767SRodney W. Grimes  * Outputs:
121626f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
121726f9a767SRodney W. Grimes  *
121826f9a767SRodney W. Grimes  * Return value:
121926f9a767SRodney W. Grimes  *  number of pages in marray
122023955314SAlfred Perlstein  *
122123955314SAlfred Perlstein  * This routine can't block.
122226f9a767SRodney W. Grimes  */
1223303b270bSEivind Eklund static int
122422ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
122526f9a767SRodney W. Grimes 	vm_page_t m;
122626f9a767SRodney W. Grimes 	int rbehind;
122722ba64e8SJohn Dyson 	int rahead;
122826f9a767SRodney W. Grimes 	vm_page_t *marray;
122926f9a767SRodney W. Grimes 	int *reqpage;
123026f9a767SRodney W. Grimes {
12312d8acc0fSJohn Dyson 	int i,j;
123226f9a767SRodney W. Grimes 	vm_object_t object;
1233a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
123426f9a767SRodney W. Grimes 	vm_page_t rtm;
1235170db9c6SJohn Dyson 	int cbehind, cahead;
123626f9a767SRodney W. Grimes 
1237f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
123823955314SAlfred Perlstein 
123926f9a767SRodney W. Grimes 	object = m->object;
1240a316d390SJohn Dyson 	pindex = m->pindex;
124126f9a767SRodney W. Grimes 
124226f9a767SRodney W. Grimes 	/*
1243f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1244f35329acSJohn Dyson 	 */
1245f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1246f35329acSJohn Dyson 		*reqpage = 0;
1247f35329acSJohn Dyson 		marray[0] = m;
1248f35329acSJohn Dyson 		return 1;
1249f35329acSJohn Dyson 	}
1250f35329acSJohn Dyson 
1251f35329acSJohn Dyson 	/*
125226f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
125326f9a767SRodney W. Grimes 	 */
12541c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
125526f9a767SRodney W. Grimes 		return 0;
12562d8acc0fSJohn Dyson 	}
125726f9a767SRodney W. Grimes 
125822ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
125922ba64e8SJohn Dyson 		*reqpage = 0;
126022ba64e8SJohn Dyson 		marray[0] = m;
126122ba64e8SJohn Dyson 		return 1;
1262170db9c6SJohn Dyson 	}
126322ba64e8SJohn Dyson 
126422ba64e8SJohn Dyson 	if (rahead > cahead) {
126522ba64e8SJohn Dyson 		rahead = cahead;
126622ba64e8SJohn Dyson 	}
126722ba64e8SJohn Dyson 
1268170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1269170db9c6SJohn Dyson 		rbehind = cbehind;
1270170db9c6SJohn Dyson 	}
1271170db9c6SJohn Dyson 
127226f9a767SRodney W. Grimes 	/*
127326f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
127426f9a767SRodney W. Grimes 	 */
1275ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
127622ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1277f919ebdeSDavid Greenman 		pagedaemon_wakeup();
127826f9a767SRodney W. Grimes 		marray[0] = m;
12792d8acc0fSJohn Dyson 		*reqpage = 0;
128026f9a767SRodney W. Grimes 		return 1;
128126f9a767SRodney W. Grimes 	}
128222ba64e8SJohn Dyson 
128326f9a767SRodney W. Grimes 	/*
12842d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
128526f9a767SRodney W. Grimes 	 */
12862d8acc0fSJohn Dyson 	if (pindex > 0) {
12872d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1288a316d390SJohn Dyson 			rbehind = pindex;
12892d8acc0fSJohn Dyson 			startpindex = 0;
12902d8acc0fSJohn Dyson 		} else {
1291a316d390SJohn Dyson 			startpindex = pindex - rbehind;
12922d8acc0fSJohn Dyson 		}
12932d8acc0fSJohn Dyson 
12948f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
12958f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
12968f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
129726f9a767SRodney W. Grimes 
12982d8acc0fSJohn Dyson 		for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
129926f9a767SRodney W. Grimes 
13002d8acc0fSJohn Dyson 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1301ccbb2f72SJohn Dyson 			if (rtm == NULL) {
13022ad98273SAlan Cox 				vm_page_lock_queues();
1303ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
13044866e085SJohn Dyson 					vm_page_free(marray[j]);
130526f9a767SRodney W. Grimes 				}
13062ad98273SAlan Cox 				vm_page_unlock_queues();
130726f9a767SRodney W. Grimes 				marray[0] = m;
13082d8acc0fSJohn Dyson 				*reqpage = 0;
130926f9a767SRodney W. Grimes 				return 1;
131026f9a767SRodney W. Grimes 			}
1311170db9c6SJohn Dyson 
13122d8acc0fSJohn Dyson 			marray[i] = rtm;
131326f9a767SRodney W. Grimes 		}
13142d8acc0fSJohn Dyson 	} else {
13152d8acc0fSJohn Dyson 		startpindex = 0;
13162d8acc0fSJohn Dyson 		i = 0;
13172d8acc0fSJohn Dyson 	}
13182d8acc0fSJohn Dyson 
13192d8acc0fSJohn Dyson 	marray[i] = m;
13202d8acc0fSJohn Dyson 	/* page offset of the required page */
13212d8acc0fSJohn Dyson 	*reqpage = i;
13222d8acc0fSJohn Dyson 
13232d8acc0fSJohn Dyson 	tpindex = pindex + 1;
13242d8acc0fSJohn Dyson 	i++;
13252d8acc0fSJohn Dyson 
13262d8acc0fSJohn Dyson 	/*
13272d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
13282d8acc0fSJohn Dyson 	 */
13292d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
13308f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
13318f8790a7SAlan Cox 		endpindex = rtm->pindex;
13322d8acc0fSJohn Dyson 	if (endpindex > object->size)
13332d8acc0fSJohn Dyson 		endpindex = object->size;
13342d8acc0fSJohn Dyson 
13352d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
13362d8acc0fSJohn Dyson 
13372d8acc0fSJohn Dyson 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
13382d8acc0fSJohn Dyson 		if (rtm == NULL) {
13392d8acc0fSJohn Dyson 			break;
13402d8acc0fSJohn Dyson 		}
13412d8acc0fSJohn Dyson 
13422d8acc0fSJohn Dyson 		marray[i] = rtm;
13432d8acc0fSJohn Dyson 	}
13442d8acc0fSJohn Dyson 
13452d8acc0fSJohn Dyson 	/* return number of bytes of pages */
13462d8acc0fSJohn Dyson 	return i;
134726f9a767SRodney W. Grimes }
1348