xref: /freebsd/sys/vm/vm_fault.c (revision 57b5187b16cde6f8d481a49866110b282197023a)
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 
1331c7c3c6aSMatthew Dillon 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 
1431c7c3c6aSMatthew Dillon 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 	}
17710b00dd4SPeter Grehan 	if (fs->first_object->flags & OBJ_NEEDGIANT)
178c1fbc251SAlan Cox 		VM_UNLOCK_GIANT();
1794866e085SJohn Dyson }
1804866e085SJohn Dyson 
181df8bae1dSRodney W. Grimes /*
18240360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
18340360b1bSMatthew Dillon  *	      current object *might* contain the page.
18440360b1bSMatthew Dillon  *
18540360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18640360b1bSMatthew Dillon  */
18740360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
18840360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
18940360b1bSMatthew Dillon 
19040360b1bSMatthew Dillon /*
191df8bae1dSRodney W. Grimes  *	vm_fault:
192df8bae1dSRodney W. Grimes  *
193956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
194df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
195df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
196df8bae1dSRodney W. Grimes  *	associated physical map.
197df8bae1dSRodney W. Grimes  *
198df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
199df8bae1dSRodney W. Grimes  *	proper page address.
200df8bae1dSRodney W. Grimes  *
201df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
202df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
203df8bae1dSRodney W. Grimes  *
204df8bae1dSRodney W. Grimes  *
205df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2060cddd8f0SMatthew Dillon  *	Caller may hold no locks.
207df8bae1dSRodney W. Grimes  */
208df8bae1dSRodney W. Grimes int
20923955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
21023955314SAlfred Perlstein 	 int fault_flags)
21123955314SAlfred Perlstein {
212df8bae1dSRodney W. Grimes 	vm_prot_t prot;
213ebf75125SAlan Cox 	int is_first_object_locked, result;
2146139043bSAlan Cox 	boolean_t growstack, wired;
2152d8acc0fSJohn Dyson 	int map_generation;
216df8bae1dSRodney W. Grimes 	vm_object_t next_object;
21726f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2184866e085SJohn Dyson 	int hardfault;
2192d8acc0fSJohn Dyson 	int faultcount;
2204866e085SJohn Dyson 	struct faultstate fs;
221df8bae1dSRodney W. Grimes 
2224866e085SJohn Dyson 	hardfault = 0;
2236139043bSAlan Cox 	growstack = TRUE;
22492de35b0SAlan Cox 	atomic_add_int(&cnt.v_vm_faults, 1);
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes RetryFault:;
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes 	/*
2290d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2300d94caffSDavid Greenman 	 * search.
231df8bae1dSRodney W. Grimes 	 */
23240360b1bSMatthew Dillon 	fs.map = map;
23392de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
23492de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
23592de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
23692de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
23792de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2386139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2396139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2406139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
241a976eb5eSAlan Cox 				if (result != KERN_SUCCESS)
2426139043bSAlan Cox 					return (KERN_FAILURE);
2436139043bSAlan Cox 				growstack = FALSE;
2446139043bSAlan Cox 				goto RetryFault;
2456139043bSAlan Cox 			}
24692de35b0SAlan Cox 			return (result);
24709e0c6ccSJohn Dyson 		}
24809e0c6ccSJohn Dyson 
2497aaaa4fdSJohn Dyson 		/*
2507aaaa4fdSJohn Dyson    		 * If we are user-wiring a r/w segment, and it is COW, then
2517aaaa4fdSJohn Dyson    		 * we need to do the COW operation.  Note that we don't COW
252595236dfSJohn Dyson    		 * currently RO sections now, because it is NOT desirable
2537aaaa4fdSJohn Dyson    		 * to COW .text.  We simply keep .text from ever being COW'ed
2547aaaa4fdSJohn Dyson    		 * and take the heat that one cannot debug wired .text sections.
2557aaaa4fdSJohn Dyson    		 */
2564866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr,
25747221757SJohn Dyson 			VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
2584866e085SJohn Dyson 			&fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
259a976eb5eSAlan Cox 		if (result != KERN_SUCCESS)
2607788e219SAlan Cox 			return (result);
26147221757SJohn Dyson 
2627aaaa4fdSJohn Dyson 		/*
2637aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2647aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2657aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
266ff8f4ebeSAlan Cox 		 *
267ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
268ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2697aaaa4fdSJohn Dyson 		 */
2704866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2714866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2727aaaa4fdSJohn Dyson 	}
27347221757SJohn Dyson 
2744866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2752d8acc0fSJohn Dyson 
2764866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
27747221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
27892c4c4ebSBruce Evans 		    (u_long)vaddr);
2797aaaa4fdSJohn Dyson 	}
2807aaaa4fdSJohn Dyson 
28195e5e988SJohn Dyson 	/*
28295e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
28395e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
28495e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
28595e5e988SJohn Dyson 	 * they will stay around as well.
286fe8e0238SMatthew Dillon 	 *
287fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
288fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
289fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
290532eadefSAlan Cox 	 *
291532eadefSAlan Cox 	 * XXX vnode_pager_lock() can block without releasing the map lock.
29295e5e988SJohn Dyson 	 */
293ed4fe4f4SJeff Roberson 	if (fs.first_object->flags & OBJ_NEEDGIANT)
294a976eb5eSAlan Cox 		mtx_lock(&Giant);
295d22bc710SAlan Cox 	VM_OBJECT_LOCK(fs.first_object);
296a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
297417a26a1SAlan Cox 	fs.vp = vnode_pager_lock(fs.first_object);
2985e4bdb57SAlan Cox 	KASSERT(fs.vp == NULL || !fs.map->system_map,
2995e4bdb57SAlan Cox 	    ("vm_fault: vnode-backed object mapped by system map"));
300ed4fe4f4SJeff Roberson 	KASSERT((fs.first_object->flags & OBJ_NEEDGIANT) == 0 ||
301ed4fe4f4SJeff Roberson 	    !fs.map->system_map,
302ed4fe4f4SJeff Roberson 	    ("vm_fault: Object requiring giant mapped by system map"));
303ed4fe4f4SJeff Roberson 	if (fs.first_object->flags & OBJ_NEEDGIANT && debug_mpsafevm)
304c1fbc251SAlan Cox 		mtx_unlock(&Giant);
305d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30695e5e988SJohn Dyson 
30725adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	if (wired)
310df8bae1dSRodney W. Grimes 		fault_type = prot;
311df8bae1dSRodney W. Grimes 
3124866e085SJohn Dyson 	fs.first_m = NULL;
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	/*
315df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
316df8bae1dSRodney W. Grimes 	 */
3174866e085SJohn Dyson 	fs.object = fs.first_object;
3184866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
319df8bae1dSRodney W. Grimes 	while (TRUE) {
3201c7c3c6aSMatthew Dillon 		/*
3211c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3221c7c3c6aSMatthew Dillon 		 */
3234866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3244866e085SJohn Dyson 			unlock_and_deallocate(&fs);
32547221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32647221757SJohn Dyson 		}
32747221757SJohn Dyson 
3281c7c3c6aSMatthew Dillon 		/*
3291c7c3c6aSMatthew Dillon 		 * See if page is resident
3301c7c3c6aSMatthew Dillon 		 */
3314866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3324866e085SJohn Dyson 		if (fs.m != NULL) {
33386735996SAlan Cox 			int queue;
33498cb733cSKenneth D. Merry 
33598cb733cSKenneth D. Merry 			/*
3369b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3379b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3389b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3399b80d344SKenneth D. Merry 			 * used when the page in question is part of
3409b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3419b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3429b80d344SKenneth D. Merry 			 * which is not what we want.
34398cb733cSKenneth D. Merry 			 */
344f4ecdf05SAlan Cox 			vm_page_lock_queues();
34598cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3469b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3479b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
34898cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
349f4ecdf05SAlan Cox 				vm_page_unlock_queues();
350d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
35198cb733cSKenneth D. Merry 				goto RetryFault;
35298cb733cSKenneth D. Merry 			}
35398cb733cSKenneth D. Merry 
354df8bae1dSRodney W. Grimes 			/*
3551c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3561c7c3c6aSMatthew Dillon 			 * if the page is busy via either PG_BUSY or
3571c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3581c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3591c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
360956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3611c7c3c6aSMatthew Dillon 			 *
3621c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3631c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3641c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3651c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3661c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3671c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3681c7c3c6aSMatthew Dillon 			 * to pmap it.
369df8bae1dSRodney W. Grimes 			 */
3701c7c3c6aSMatthew Dillon 			if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
3714abd55b2SAlan Cox 				vm_page_unlock_queues();
372a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
373a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
374a51b0840SAlan Cox 					VM_OBJECT_LOCK(fs.first_object);
3759a96b638SAlan Cox 					vm_page_lock_queues();
376a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3779a96b638SAlan Cox 					vm_page_unlock_queues();
378a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
379a51b0840SAlan Cox 					VM_OBJECT_UNLOCK(fs.first_object);
380a51b0840SAlan Cox 					fs.first_m = NULL;
381a51b0840SAlan Cox 				}
382a51b0840SAlan Cox 				unlock_map(&fs);
383a51b0840SAlan Cox 				if (fs.vp != NULL) {
384ed4fe4f4SJeff Roberson 					int vfslck;
385ed4fe4f4SJeff Roberson 
386ed4fe4f4SJeff Roberson 					vfslck = VFS_LOCK_GIANT(fs.vp->v_mount);
387a51b0840SAlan Cox 					vput(fs.vp);
388a51b0840SAlan Cox 					fs.vp = NULL;
389ed4fe4f4SJeff Roberson 					VFS_UNLOCK_GIANT(vfslck);
390a51b0840SAlan Cox 				}
391a51b0840SAlan Cox 				VM_OBJECT_LOCK(fs.object);
392a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
393a51b0840SAlan Cox 				    fs.pindex)) {
394a51b0840SAlan Cox 					vm_page_lock_queues();
395a51b0840SAlan Cox 					if (!vm_page_sleep_if_busy(fs.m, TRUE,
396a51b0840SAlan Cox 					    "vmpfw"))
397a51b0840SAlan Cox 						vm_page_unlock_queues();
398a51b0840SAlan Cox 				}
399a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
400a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
401c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_intrans, 1);
402b9d3b805SJohn Baldwin 				if (fs.first_object->flags & OBJ_NEEDGIANT)
403c1fbc251SAlan Cox 					VM_UNLOCK_GIANT();
4044866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
405df8bae1dSRodney W. Grimes 				goto RetryFault;
406df8bae1dSRodney W. Grimes 			}
4074866e085SJohn Dyson 			queue = fs.m->queue;
408a1287949SEivind Eklund 
4096d03d577SMatthew Dillon 			vm_pageq_remove_nowakeup(fs.m);
410c82b0181SJohn Dyson 
41140360b1bSMatthew Dillon 			if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
4124866e085SJohn Dyson 				vm_page_activate(fs.m);
4134abd55b2SAlan Cox 				vm_page_unlock_queues();
4144866e085SJohn Dyson 				unlock_and_deallocate(&fs);
415ef6020d1SMike Silbersack 				VM_WAITPFAULT;
41622ba64e8SJohn Dyson 				goto RetryFault;
41722ba64e8SJohn Dyson 			}
4187615edaaSMatthew Dillon 
4191c7c3c6aSMatthew Dillon 			/*
4201c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4211c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4221c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4231c7c3c6aSMatthew Dillon 			 * found the page ).
4241c7c3c6aSMatthew Dillon 			 */
425e69763a3SDoug Rabson 			vm_page_busy(fs.m);
4264abd55b2SAlan Cox 			vm_page_unlock_queues();
4274866e085SJohn Dyson 			if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
4284866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
4290d94caffSDavid Greenman 				goto readrest;
4300d94caffSDavid Greenman 			}
431ffc82b0aSJohn Dyson 
432df8bae1dSRodney W. Grimes 			break;
433df8bae1dSRodney W. Grimes 		}
4341c7c3c6aSMatthew Dillon 
4351c7c3c6aSMatthew Dillon 		/*
43640360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
43740360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4381c7c3c6aSMatthew Dillon 		 */
43940360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4404866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4414866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4425f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4435f55e841SDavid Greenman 			}
44422ba64e8SJohn Dyson 
445df8bae1dSRodney W. Grimes 			/*
4460d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
447df8bae1dSRodney W. Grimes 			 */
44840360b1bSMatthew Dillon 			fs.m = NULL;
44940360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
4504866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4514866e085SJohn Dyson 				    (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
45240360b1bSMatthew Dillon 			}
4534866e085SJohn Dyson 			if (fs.m == NULL) {
4544866e085SJohn Dyson 				unlock_and_deallocate(&fs);
455ef6020d1SMike Silbersack 				VM_WAITPFAULT;
456df8bae1dSRodney W. Grimes 				goto RetryFault;
457df8bae1dSRodney W. Grimes 			}
458df8bae1dSRodney W. Grimes 		}
45947221757SJohn Dyson 
4600d94caffSDavid Greenman readrest:
4611c7c3c6aSMatthew Dillon 		/*
46240360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
46340360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
46440360b1bSMatthew Dillon 		 * valid.
46540360b1bSMatthew Dillon 		 *
46640360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
46740360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
46840360b1bSMatthew Dillon 		 * at the same time.
4691c7c3c6aSMatthew Dillon 		 */
47040360b1bSMatthew Dillon 		if (TRYPAGER) {
471df8bae1dSRodney W. Grimes 			int rv;
47226f9a767SRodney W. Grimes 			int reqpage;
473867a482dSJohn Dyson 			int ahead, behind;
4747f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
475867a482dSJohn Dyson 
4767f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
477867a482dSJohn Dyson 				ahead = 0;
478867a482dSJohn Dyson 				behind = 0;
4792d8acc0fSJohn Dyson 			} else {
4804866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4812d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4822d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4832d8acc0fSJohn Dyson 
4844866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4852d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4862d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
487867a482dSJohn Dyson 			}
4888d8b9c6eSAlan Cox 			is_first_object_locked = FALSE;
4898d8b9c6eSAlan Cox 			if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
49040360b1bSMatthew Dillon 			     (behavior != MAP_ENTRY_BEHAV_RANDOM &&
49140360b1bSMatthew Dillon 			      fs.pindex >= fs.entry->lastr &&
4928d8b9c6eSAlan Cox 			      fs.pindex < fs.entry->lastr + VM_FAULT_READ)) &&
4938d8b9c6eSAlan Cox 			    (fs.first_object == fs.object ||
4948d8b9c6eSAlan Cox 			     (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) &&
4958d8b9c6eSAlan Cox 			    fs.first_object->type != OBJT_DEVICE) {
496867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
49740360b1bSMatthew Dillon 
49840360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
499867a482dSJohn Dyson 					firstpindex = 0;
500867a482dSJohn Dyson 				else
50140360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
502867a482dSJohn Dyson 
50315a5d210SAlan Cox 				vm_page_lock_queues();
5044221e284SAlan Cox 				/*
5054221e284SAlan Cox 				 * note: partially valid pages cannot be
5064221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
5074221e284SAlan Cox 				 * writes will barf on it badly.
5084221e284SAlan Cox 				 */
5094866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
5102100d645SPeter Wemm 					tmppindex >= firstpindex;
511867a482dSJohn Dyson 					--tmppindex) {
512867a482dSJohn Dyson 					vm_page_t mt;
513a1287949SEivind Eklund 
5144866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
515867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
516867a482dSJohn Dyson 						break;
517ff97964aSJohn Dyson 					if (mt->busy ||
5188b03c8edSMatthew Dillon 						(mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
519ff97964aSJohn Dyson 						mt->hold_count ||
520867a482dSJohn Dyson 						mt->wire_count)
521867a482dSJohn Dyson 						continue;
5224fec79beSAlan Cox 					pmap_remove_all(mt);
523c6d9ef2eSAlan Cox 					if (mt->dirty) {
524867a482dSJohn Dyson 						vm_page_deactivate(mt);
525867a482dSJohn Dyson 					} else {
526867a482dSJohn Dyson 						vm_page_cache(mt);
527867a482dSJohn Dyson 					}
528867a482dSJohn Dyson 				}
52915a5d210SAlan Cox 				vm_page_unlock_queues();
530867a482dSJohn Dyson 				ahead += behind;
531867a482dSJohn Dyson 				behind = 0;
532867a482dSJohn Dyson 			}
5338d8b9c6eSAlan Cox 			if (is_first_object_locked)
5348d8b9c6eSAlan Cox 				VM_OBJECT_UNLOCK(fs.first_object);
535df8bae1dSRodney W. Grimes 			/*
5360d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5370d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5380d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5390d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5400d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5410d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5420d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5430d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5440d94caffSDavid Greenman 			 * return value is the index into the marray for the
5450d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5461c7c3c6aSMatthew Dillon 			 *
5471c7c3c6aSMatthew Dillon 			 * fs.m plus the additional pages are PG_BUSY'd.
548532eadefSAlan Cox 			 *
549532eadefSAlan Cox 			 * XXX vm_fault_additional_pages() can block
550532eadefSAlan Cox 			 * without releasing the map lock.
55126f9a767SRodney W. Grimes 			 */
55205f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5534866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
554df8bae1dSRodney W. Grimes 
555df8bae1dSRodney W. Grimes 			/*
55640360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
55740360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
558532eadefSAlan Cox 			 *
559532eadefSAlan Cox 			 * XXX The following assignment modifies the map
560532eadefSAlan Cox 			 * without holding a write lock on it.
56140360b1bSMatthew Dillon 			 */
56240360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
56340360b1bSMatthew Dillon 
56440360b1bSMatthew Dillon 			/*
5650d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
5661c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
5671c7c3c6aSMatthew Dillon 			 * fs.object and the pages are PG_BUSY'd.
568df8bae1dSRodney W. Grimes 			 */
5694866e085SJohn Dyson 			unlock_map(&fs);
570df8bae1dSRodney W. Grimes 
57126f9a767SRodney W. Grimes 			rv = faultcount ?
5724866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
57324a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
57422ba64e8SJohn Dyson 
57526f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
576df8bae1dSRodney W. Grimes 				/*
577f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
578f230c45cSJohn Dyson 				 * with it.
579f230c45cSJohn Dyson 				 */
580f230c45cSJohn Dyson 
581f230c45cSJohn Dyson 				/*
5820d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5830d94caffSDavid Greenman 				 * is responsible for disposition of old page
5840d94caffSDavid Greenman 				 * if moved.
585df8bae1dSRodney W. Grimes 				 */
5864866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5874866e085SJohn Dyson 				if (!fs.m) {
5884866e085SJohn Dyson 					unlock_and_deallocate(&fs);
589f6b04d2bSDavid Greenman 					goto RetryFault;
590f6b04d2bSDavid Greenman 				}
591f6b04d2bSDavid Greenman 
59226f9a767SRodney W. Grimes 				hardfault++;
5931c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
594df8bae1dSRodney W. Grimes 			}
595df8bae1dSRodney W. Grimes 			/*
5960d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5970d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5980d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
599df8bae1dSRodney W. Grimes 			 *
60024a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
6010d94caffSDavid Greenman 			 * in this page.
602df8bae1dSRodney W. Grimes 			 *
6030d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
60424a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
6050d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
6060d94caffSDavid Greenman 			 * the same time that we are.
607df8bae1dSRodney W. Grimes 			 */
608a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
609f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
610f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
61126f9a767SRodney W. Grimes 			/*
612a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
61326f9a767SRodney W. Grimes 			 */
614a83c285cSDavid Greenman 			/*
6150d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6160d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6170d94caffSDavid Greenman 			 * fault w/ I/O error.
618a83c285cSDavid Greenman 			 */
6194866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
62047221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6212ad98273SAlan Cox 				vm_page_lock_queues();
6224866e085SJohn Dyson 				vm_page_free(fs.m);
6232ad98273SAlan Cox 				vm_page_unlock_queues();
6244866e085SJohn Dyson 				fs.m = NULL;
6254866e085SJohn Dyson 				unlock_and_deallocate(&fs);
626a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
62726f9a767SRodney W. Grimes 			}
6284866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6292ad98273SAlan Cox 				vm_page_lock_queues();
6304866e085SJohn Dyson 				vm_page_free(fs.m);
6312ad98273SAlan Cox 				vm_page_unlock_queues();
6324866e085SJohn Dyson 				fs.m = NULL;
63326f9a767SRodney W. Grimes 				/*
63426f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
63526f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
63626f9a767SRodney W. Grimes 				 */
637df8bae1dSRodney W. Grimes 			}
638df8bae1dSRodney W. Grimes 		}
63940360b1bSMatthew Dillon 
640df8bae1dSRodney W. Grimes 		/*
6411c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6421c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
643df8bae1dSRodney W. Grimes 		 */
6444866e085SJohn Dyson 		if (fs.object == fs.first_object)
6454866e085SJohn Dyson 			fs.first_m = fs.m;
646df8bae1dSRodney W. Grimes 
647df8bae1dSRodney W. Grimes 		/*
6480d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6490d94caffSDavid Greenman 		 * unlocking the current one.
650df8bae1dSRodney W. Grimes 		 */
6514866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6524866e085SJohn Dyson 		next_object = fs.object->backing_object;
653df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
654df8bae1dSRodney W. Grimes 			/*
6550d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6560d94caffSDavid Greenman 			 * object with zeros.
657df8bae1dSRodney W. Grimes 			 */
6584866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6594866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
660b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
661df8bae1dSRodney W. Grimes 
6624866e085SJohn Dyson 				fs.object = fs.first_object;
6634866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6644866e085SJohn Dyson 				fs.m = fs.first_m;
665f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
666df8bae1dSRodney W. Grimes 			}
6674866e085SJohn Dyson 			fs.first_m = NULL;
668df8bae1dSRodney W. Grimes 
6694221e284SAlan Cox 			/*
6704221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6714221e284SAlan Cox 			 */
6724866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
673fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6744221e284SAlan Cox 			} else {
675c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_ozfod, 1);
6764221e284SAlan Cox 			}
677c1fbc251SAlan Cox 			atomic_add_int(&cnt.v_zfod, 1);
6784221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6791c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6800d94caffSDavid Greenman 		} else {
681c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
682c8567c3aSAlan Cox 			    ("object loop %p", next_object));
683c8567c3aSAlan Cox 			VM_OBJECT_LOCK(next_object);
684c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
685c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6864866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
687b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
6884866e085SJohn Dyson 			fs.object = next_object;
689df8bae1dSRodney W. Grimes 		}
690df8bae1dSRodney W. Grimes 	}
6911c7c3c6aSMatthew Dillon 
6925526d2d9SEivind Eklund 	KASSERT((fs.m->flags & PG_BUSY) != 0,
6935526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 	/*
6960d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
697df8bae1dSRodney W. Grimes 	 * is held.]
698df8bae1dSRodney W. Grimes 	 */
699df8bae1dSRodney W. Grimes 
700df8bae1dSRodney W. Grimes 	/*
7010d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
7020d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
7030d94caffSDavid Greenman 	 * top-level object.
704df8bae1dSRodney W. Grimes 	 */
7054866e085SJohn Dyson 	if (fs.object != fs.first_object) {
706df8bae1dSRodney W. Grimes 		/*
7070d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
708df8bae1dSRodney W. Grimes 		 */
709df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
710df8bae1dSRodney W. Grimes 			/*
7111c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7121c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7131c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7141c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7151c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7161c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7171c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7181c7c3c6aSMatthew Dillon 			 * to swap when needed.
719df8bae1dSRodney W. Grimes 			 */
720ebf75125SAlan Cox 			is_first_object_locked = FALSE;
721e50346b5SAlan Cox 			if (
722de5f6a77SJohn Dyson 				/*
723de5f6a77SJohn Dyson 				 * Only one shadow object
724de5f6a77SJohn Dyson 				 */
7254866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
726de5f6a77SJohn Dyson 				/*
727de5f6a77SJohn Dyson 				 * No COW refs, except us
728de5f6a77SJohn Dyson 				 */
7294866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
730de5f6a77SJohn Dyson 				/*
7315929bcfaSPhilippe Charnier 				 * No one else can look this object up
732de5f6a77SJohn Dyson 				 */
7334866e085SJohn Dyson 				(fs.object->handle == NULL) &&
734de5f6a77SJohn Dyson 				/*
735de5f6a77SJohn Dyson 				 * No other ways to look the object up
736de5f6a77SJohn Dyson 				 */
7374866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7384866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
739ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
740de5f6a77SJohn Dyson 				/*
741de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
742de5f6a77SJohn Dyson 				 */
743e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
744d98ddc46SAlan Cox 				vm_page_lock_queues();
7452d8acc0fSJohn Dyson 				/*
746de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
747df8bae1dSRodney W. Grimes 				 */
7484fec79beSAlan Cox 				pmap_remove_all(fs.first_m);
7494866e085SJohn Dyson 				vm_page_free(fs.first_m);
750de5f6a77SJohn Dyson 				/*
7511c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7521c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7531c7c3c6aSMatthew Dillon 				 * automatically made dirty.
754de5f6a77SJohn Dyson 				 */
7554866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
756d98ddc46SAlan Cox 				vm_page_busy(fs.m);
7579a96b638SAlan Cox 				vm_page_unlock_queues();
758d98ddc46SAlan Cox 				fs.first_m = fs.m;
7594866e085SJohn Dyson 				fs.m = NULL;
760c1fbc251SAlan Cox 				atomic_add_int(&cnt.v_cow_optim, 1);
761de5f6a77SJohn Dyson 			} else {
762de5f6a77SJohn Dyson 				/*
763de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
764de5f6a77SJohn Dyson 				 */
765669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
766669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
767de5f6a77SJohn Dyson 			}
7684866e085SJohn Dyson 			if (fs.m) {
769df8bae1dSRodney W. Grimes 				/*
770df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
771df8bae1dSRodney W. Grimes 				 */
7724866e085SJohn Dyson 				release_page(&fs);
773de5f6a77SJohn Dyson 			}
7741c7c3c6aSMatthew Dillon 			/*
7751c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7761c7c3c6aSMatthew Dillon 			 * conditional
7771c7c3c6aSMatthew Dillon 			 */
7784866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
779b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
780df8bae1dSRodney W. Grimes 			/*
781df8bae1dSRodney W. Grimes 			 * Only use the new page below...
782df8bae1dSRodney W. Grimes 			 */
7834866e085SJohn Dyson 			fs.object = fs.first_object;
7844866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
785d98ddc46SAlan Cox 			fs.m = fs.first_m;
786f29ba63eSAlan Cox 			if (!is_first_object_locked)
787f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
788c1fbc251SAlan Cox 			atomic_add_int(&cnt.v_cow_faults, 1);
7890d94caffSDavid Greenman 		} else {
790df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
791df8bae1dSRodney W. Grimes 		}
792df8bae1dSRodney W. Grimes 	}
793df8bae1dSRodney W. Grimes 
794df8bae1dSRodney W. Grimes 	/*
7950d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7960d94caffSDavid Greenman 	 * lookup.
797df8bae1dSRodney W. Grimes 	 */
79819dc5607STor Egge 	if (!fs.lookup_still_valid) {
799df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
800a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
801df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
802df8bae1dSRodney W. Grimes 
80319dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
804b823bbd6SMatthew Dillon 			release_page(&fs);
805b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
806b823bbd6SMatthew Dillon 			goto RetryFault;
807b823bbd6SMatthew Dillon 		}
80819dc5607STor Egge 		fs.lookup_still_valid = TRUE;
80919dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
81019dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8114866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
812df8bae1dSRodney W. Grimes 
813df8bae1dSRodney W. Grimes 			/*
8140d94caffSDavid Greenman 			 * If we don't need the page any longer, put it on the active
8150d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8160d94caffSDavid Greenman 			 * pageout will grab it eventually.
817df8bae1dSRodney W. Grimes 			 */
818df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8194866e085SJohn Dyson 				release_page(&fs);
8204866e085SJohn Dyson 				unlock_and_deallocate(&fs);
82119dc5607STor Egge 
82219dc5607STor Egge 				/*
82319dc5607STor Egge 				 * If retry of map lookup would have blocked then
82419dc5607STor Egge 				 * retry fault from start.
82519dc5607STor Egge 				 */
82619dc5607STor Egge 				if (result == KERN_FAILURE)
82719dc5607STor Egge 					goto RetryFault;
828df8bae1dSRodney W. Grimes 				return (result);
829df8bae1dSRodney W. Grimes 			}
8304866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8314866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8324866e085SJohn Dyson 				release_page(&fs);
8334866e085SJohn Dyson 				unlock_and_deallocate(&fs);
834df8bae1dSRodney W. Grimes 				goto RetryFault;
835df8bae1dSRodney W. Grimes 			}
83619dc5607STor Egge 
837df8bae1dSRodney W. Grimes 			/*
8380d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8390d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8400d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8410d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8420d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8430d94caffSDavid Greenman 			 * write-enabled after all.
844df8bae1dSRodney W. Grimes 			 */
845df8bae1dSRodney W. Grimes 			prot &= retry_prot;
846df8bae1dSRodney W. Grimes 		}
84719dc5607STor Egge 	}
8482ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
8499a96b638SAlan Cox 		vm_page_lock_queues();
850e69763a3SDoug Rabson 		vm_page_flag_set(fs.m, PG_WRITEABLE);
851245df27cSMatthew Dillon 		vm_object_set_writeable_dirty(fs.m->object);
8524f79d873SMatthew Dillon 
8532ddba215SDavid Greenman 		/*
8542ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8554f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8564f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8574f79d873SMatthew Dillon 		 *
8584f79d873SMatthew Dillon 		 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
8594f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8604f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8614f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8624f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8634f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8641c7c3c6aSMatthew Dillon 		 *
8651c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8661c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8672ddba215SDavid Greenman 		 */
8684f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8694f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
8704f79d873SMatthew Dillon 				vm_page_flag_set(fs.m, PG_NOSYNC);
8714f79d873SMatthew Dillon 		} else {
8724f79d873SMatthew Dillon 			vm_page_flag_clear(fs.m, PG_NOSYNC);
8734f79d873SMatthew Dillon 		}
8749a96b638SAlan Cox 		vm_page_unlock_queues();
8755bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8767dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
8771c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
8782ddba215SDavid Greenman 		}
8792ddba215SDavid Greenman 	}
880f6b04d2bSDavid Greenman 
881bc6d84a6SMatthew Dillon 	/*
882bc6d84a6SMatthew Dillon 	 * Page had better still be busy
883bc6d84a6SMatthew Dillon 	 */
884ca06c247SAlan Cox 	KASSERT(fs.m->flags & PG_BUSY,
885ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
8864221e284SAlan Cox 	/*
8874221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8884221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8894221e284SAlan Cox 	 */
8904221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8914221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8924221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8934221e284SAlan Cox 	}
894eebf3286SAlan Cox 	VM_OBJECT_UNLOCK(fs.object);
895cbfbaad8SAlan Cox 
89686735996SAlan Cox 	/*
89786735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
89886735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
89986735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
90086735996SAlan Cox 	 * won't find it (yet).
90186735996SAlan Cox 	 */
9020385347cSPeter Wemm 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
9032d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
904566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
9052d8acc0fSJohn Dyson 	}
906eebf3286SAlan Cox 	VM_OBJECT_LOCK(fs.object);
9072d09a6adSAlan Cox 	vm_page_lock_queues();
908db44450bSAlan Cox 	vm_page_flag_set(fs.m, PG_REFERENCED);
909ff97964aSJohn Dyson 
910df8bae1dSRodney W. Grimes 	/*
9110d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9120d94caffSDavid Greenman 	 * can find it.
913df8bae1dSRodney W. Grimes 	 */
914a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
915df8bae1dSRodney W. Grimes 		if (wired)
9164866e085SJohn Dyson 			vm_page_wire(fs.m);
917df8bae1dSRodney W. Grimes 		else
91873007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
9190d94caffSDavid Greenman 	} else {
9204866e085SJohn Dyson 		vm_page_activate(fs.m);
92126f9a767SRodney W. Grimes 	}
922a9911f9aSAlan Cox 	vm_page_wakeup(fs.m);
9232d09a6adSAlan Cox 	vm_page_unlock_queues();
924eeec6babSJohn Baldwin 
925eebf3286SAlan Cox 	/*
926eebf3286SAlan Cox 	 * Unlock everything, and return
927eebf3286SAlan Cox 	 */
928eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
929eeec6babSJohn Baldwin 	PROC_LOCK(curproc);
930eeec6babSJohn Baldwin 	if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
93126f9a767SRodney W. Grimes 		if (hardfault) {
93226f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
93326f9a767SRodney W. Grimes 		} else {
93426f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
93526f9a767SRodney W. Grimes 		}
93626f9a767SRodney W. Grimes 	}
937eeec6babSJohn Baldwin 	PROC_UNLOCK(curproc);
938df8bae1dSRodney W. Grimes 
939df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
940df8bae1dSRodney W. Grimes }
941df8bae1dSRodney W. Grimes 
942df8bae1dSRodney W. Grimes /*
943566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
944566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
945566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
946566526a9SAlan Cox  * of mmap time.
947566526a9SAlan Cox  */
948566526a9SAlan Cox static void
949566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
950566526a9SAlan Cox {
951566526a9SAlan Cox 	int i;
952566526a9SAlan Cox 	vm_offset_t addr, starta;
953566526a9SAlan Cox 	vm_pindex_t pindex;
954566526a9SAlan Cox 	vm_page_t m, mpte;
955566526a9SAlan Cox 	vm_object_t object;
956566526a9SAlan Cox 
957950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
958566526a9SAlan Cox 		return;
959566526a9SAlan Cox 
960566526a9SAlan Cox 	object = entry->object.vm_object;
961566526a9SAlan Cox 
962566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
963566526a9SAlan Cox 	if (starta < entry->start) {
964566526a9SAlan Cox 		starta = entry->start;
965566526a9SAlan Cox 	} else if (starta > addra) {
966566526a9SAlan Cox 		starta = 0;
967566526a9SAlan Cox 	}
968566526a9SAlan Cox 
969566526a9SAlan Cox 	mpte = NULL;
970566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
971566526a9SAlan Cox 		vm_object_t backing_object, lobject;
972566526a9SAlan Cox 
973566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
974566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
975566526a9SAlan Cox 			addr = 0;
976566526a9SAlan Cox 
977566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
978566526a9SAlan Cox 			continue;
979566526a9SAlan Cox 
980566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
981566526a9SAlan Cox 			continue;
982566526a9SAlan Cox 
983566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
984566526a9SAlan Cox 		lobject = object;
985566526a9SAlan Cox 		VM_OBJECT_LOCK(lobject);
986566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
987566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
988566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
989566526a9SAlan Cox 			if (lobject->backing_object_offset & PAGE_MASK)
990566526a9SAlan Cox 				break;
991566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
992566526a9SAlan Cox 			VM_OBJECT_LOCK(backing_object);
993566526a9SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
994566526a9SAlan Cox 			lobject = backing_object;
995566526a9SAlan Cox 		}
996566526a9SAlan Cox 		/*
997566526a9SAlan Cox 		 * give-up when a page is not in memory
998566526a9SAlan Cox 		 */
999cbfbaad8SAlan Cox 		if (m == NULL) {
1000cbfbaad8SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
1001566526a9SAlan Cox 			break;
1002cbfbaad8SAlan Cox 		}
1003566526a9SAlan Cox 		if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
1004566526a9SAlan Cox 			(m->busy == 0) &&
1005566526a9SAlan Cox 		    (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
1006566526a9SAlan Cox 
1007566526a9SAlan Cox 			vm_page_lock_queues();
10081f70d622SAlan Cox 			if ((m->queue - m->pc) == PQ_CACHE)
100985f5b245SAlan Cox 				vm_page_deactivate(m);
1010ba8bca61SAlan Cox 			mpte = pmap_enter_quick(pmap, addr, m,
1011ba8bca61SAlan Cox 			    entry->protection, mpte);
10121f70d622SAlan Cox 			vm_page_unlock_queues();
101385f5b245SAlan Cox 		}
1014cbfbaad8SAlan Cox 		VM_OBJECT_UNLOCK(lobject);
1015566526a9SAlan Cox 	}
1016566526a9SAlan Cox }
1017566526a9SAlan Cox 
1018566526a9SAlan Cox /*
1019e3669ceeSMatthew Dillon  *	vm_fault_quick:
1020e3669ceeSMatthew Dillon  *
1021e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
1022e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
1023e3669ceeSMatthew Dillon  */
1024e3669ceeSMatthew Dillon int
1025e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
1026e3669ceeSMatthew Dillon {
1027e3669ceeSMatthew Dillon 	int r;
1028e3669ceeSMatthew Dillon 
1029e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
1030e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
1031e3669ceeSMatthew Dillon 	else
1032e3669ceeSMatthew Dillon 		r = fubyte(v);
1033e3669ceeSMatthew Dillon 	return(r);
1034e3669ceeSMatthew Dillon }
1035e3669ceeSMatthew Dillon 
1036e3669ceeSMatthew Dillon /*
1037df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1038df8bae1dSRodney W. Grimes  *
1039df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1040df8bae1dSRodney W. Grimes  */
1041df8bae1dSRodney W. Grimes int
10424be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10434be14af9SAlan Cox     boolean_t user_wire, boolean_t fictitious)
1044df8bae1dSRodney W. Grimes {
104554d92145SMatthew Dillon 	vm_offset_t va;
1046df8bae1dSRodney W. Grimes 	int rv;
1047df8bae1dSRodney W. Grimes 
1048df8bae1dSRodney W. Grimes 	/*
10490d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1050ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1051ef594d31SAlan Cox 	 * read-only sections.
1052df8bae1dSRodney W. Grimes 	 */
1053df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1054ef594d31SAlan Cox 		rv = vm_fault(map, va,
1055ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1056ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
10577aaaa4fdSJohn Dyson 		if (rv) {
10587aaaa4fdSJohn Dyson 			if (va != start)
10594be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
10607aaaa4fdSJohn Dyson 			return (rv);
10617aaaa4fdSJohn Dyson 		}
10627aaaa4fdSJohn Dyson 	}
10637aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
10647aaaa4fdSJohn Dyson }
10657aaaa4fdSJohn Dyson 
10667aaaa4fdSJohn Dyson /*
1067df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1068df8bae1dSRodney W. Grimes  *
1069df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1070df8bae1dSRodney W. Grimes  */
107126f9a767SRodney W. Grimes void
10724be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10734be14af9SAlan Cox     boolean_t fictitious)
1074df8bae1dSRodney W. Grimes {
1075227f9a1cSJake Burkholder 	vm_paddr_t pa;
1076227f9a1cSJake Burkholder 	vm_offset_t va;
107754d92145SMatthew Dillon 	pmap_t pmap;
1078df8bae1dSRodney W. Grimes 
1079df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1080df8bae1dSRodney W. Grimes 
1081df8bae1dSRodney W. Grimes 	/*
10820d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
10830d94caffSDavid Greenman 	 * mappings from the physical map system.
1084df8bae1dSRodney W. Grimes 	 */
1085df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1086df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1087227f9a1cSJake Burkholder 		if (pa != 0) {
1088df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
10894be14af9SAlan Cox 			if (!fictitious) {
10902d09a6adSAlan Cox 				vm_page_lock_queues();
109173007561SDavid Greenman 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
10922d09a6adSAlan Cox 				vm_page_unlock_queues();
1093df8bae1dSRodney W. Grimes 			}
1094b18bfc3dSJohn Dyson 		}
10954be14af9SAlan Cox 	}
1096df8bae1dSRodney W. Grimes }
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes /*
1099df8bae1dSRodney W. Grimes  *	Routine:
1100df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1101df8bae1dSRodney W. Grimes  *	Function:
1102df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1103df8bae1dSRodney W. Grimes  *
1104df8bae1dSRodney W. Grimes  *	In/out conditions:
1105df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1106df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1107df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1108df8bae1dSRodney W. Grimes  */
110926f9a767SRodney W. Grimes void
111026f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1111df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1112df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1113df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1114df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1115df8bae1dSRodney W. Grimes {
1116c5b65a67SAlan Cox 	vm_object_t backing_object, dst_object, object;
1117df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1118a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1119a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1120c5b65a67SAlan Cox 	vm_pindex_t pindex;
1121df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1122df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1123df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1124df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1125df8bae1dSRodney W. Grimes 
1126df8bae1dSRodney W. Grimes #ifdef	lint
1127df8bae1dSRodney W. Grimes 	src_map++;
11280d94caffSDavid Greenman #endif	/* lint */
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1131df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1132df8bae1dSRodney W. Grimes 
1133df8bae1dSRodney W. Grimes 	/*
11340d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
11350d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1136df8bae1dSRodney W. Grimes 	 */
113724a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
113857b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1139df8bae1dSRodney W. Grimes 
11408afcf0ccSAlan Cox 	VM_OBJECT_LOCK(dst_object);
1141df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1142df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1145df8bae1dSRodney W. Grimes 
1146df8bae1dSRodney W. Grimes 	/*
11470d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
11480d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
11490d94caffSDavid Greenman 	 * object.
1150df8bae1dSRodney W. Grimes 	 */
1151df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1152df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1153df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes 		/*
1156df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1157df8bae1dSRodney W. Grimes 		 */
1158df8bae1dSRodney W. Grimes 		do {
1159a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1160a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1161df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
11628afcf0ccSAlan Cox 				VM_OBJECT_UNLOCK(dst_object);
1163df8bae1dSRodney W. Grimes 				VM_WAIT;
11648afcf0ccSAlan Cox 				VM_OBJECT_LOCK(dst_object);
1165df8bae1dSRodney W. Grimes 			}
1166df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 		/*
1169df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
11700d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
11710d94caffSDavid Greenman 		 * memory.)
1172df8bae1dSRodney W. Grimes 		 */
1173f29ba63eSAlan Cox 		VM_OBJECT_LOCK(src_object);
1174c5b65a67SAlan Cox 		object = src_object;
1175c5b65a67SAlan Cox 		pindex = 0;
1176c5b65a67SAlan Cox 		while ((src_m = vm_page_lookup(object, pindex +
1177c5b65a67SAlan Cox 		    OFF_TO_IDX(dst_offset + src_offset))) == NULL &&
1178c5b65a67SAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) == 0 &&
1179c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1180c5b65a67SAlan Cox 			/*
1181c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1182c5b65a67SAlan Cox 			 */
1183c5b65a67SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1184c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
1185c5b65a67SAlan Cox 			VM_OBJECT_UNLOCK(object);
1186c5b65a67SAlan Cox 			object = backing_object;
1187c5b65a67SAlan Cox 		}
1188df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1189df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1190669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
1191c5b65a67SAlan Cox 		VM_OBJECT_UNLOCK(object);
1192669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
11938afcf0ccSAlan Cox 		VM_OBJECT_UNLOCK(dst_object);
1194df8bae1dSRodney W. Grimes 
1195df8bae1dSRodney W. Grimes 		/*
1196df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
1197df8bae1dSRodney W. Grimes 		 */
11980385347cSPeter Wemm 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
11998afcf0ccSAlan Cox 		VM_OBJECT_LOCK(dst_object);
12004abd55b2SAlan Cox 		vm_page_lock_queues();
1201c5b65a67SAlan Cox 		if ((prot & VM_PROT_WRITE) != 0)
1202db44450bSAlan Cox 			vm_page_flag_set(dst_m, PG_WRITEABLE);
1203df8bae1dSRodney W. Grimes 
1204df8bae1dSRodney W. Grimes 		/*
1205df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1206df8bae1dSRodney W. Grimes 		 */
1207df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
1208e69763a3SDoug Rabson 		vm_page_wakeup(dst_m);
12094abd55b2SAlan Cox 		vm_page_unlock_queues();
1210df8bae1dSRodney W. Grimes 	}
12118afcf0ccSAlan Cox 	VM_OBJECT_UNLOCK(dst_object);
1212df8bae1dSRodney W. Grimes }
121326f9a767SRodney W. Grimes 
121426f9a767SRodney W. Grimes 
121526f9a767SRodney W. Grimes /*
121626f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
121722ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
121822ba64e8SJohn Dyson  * pages for the pages to be paged in.
121926f9a767SRodney W. Grimes  *
122026f9a767SRodney W. Grimes  * Inputs:
122122ba64e8SJohn Dyson  *	m, rbehind, rahead
122226f9a767SRodney W. Grimes  *
122326f9a767SRodney W. Grimes  * Outputs:
122426f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
122526f9a767SRodney W. Grimes  *
122626f9a767SRodney W. Grimes  * Return value:
122726f9a767SRodney W. Grimes  *  number of pages in marray
122823955314SAlfred Perlstein  *
122923955314SAlfred Perlstein  * This routine can't block.
123026f9a767SRodney W. Grimes  */
1231303b270bSEivind Eklund static int
123222ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
123326f9a767SRodney W. Grimes 	vm_page_t m;
123426f9a767SRodney W. Grimes 	int rbehind;
123522ba64e8SJohn Dyson 	int rahead;
123626f9a767SRodney W. Grimes 	vm_page_t *marray;
123726f9a767SRodney W. Grimes 	int *reqpage;
123826f9a767SRodney W. Grimes {
12392d8acc0fSJohn Dyson 	int i,j;
124026f9a767SRodney W. Grimes 	vm_object_t object;
1241a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
124226f9a767SRodney W. Grimes 	vm_page_t rtm;
1243170db9c6SJohn Dyson 	int cbehind, cahead;
124426f9a767SRodney W. Grimes 
1245f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
124623955314SAlfred Perlstein 
124726f9a767SRodney W. Grimes 	object = m->object;
1248a316d390SJohn Dyson 	pindex = m->pindex;
124926f9a767SRodney W. Grimes 
125026f9a767SRodney W. Grimes 	/*
1251f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1252f35329acSJohn Dyson 	 */
1253f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1254f35329acSJohn Dyson 		*reqpage = 0;
1255f35329acSJohn Dyson 		marray[0] = m;
1256f35329acSJohn Dyson 		return 1;
1257f35329acSJohn Dyson 	}
1258f35329acSJohn Dyson 
1259f35329acSJohn Dyson 	/*
126026f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
126126f9a767SRodney W. Grimes 	 */
12621c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
126326f9a767SRodney W. Grimes 		return 0;
12642d8acc0fSJohn Dyson 	}
126526f9a767SRodney W. Grimes 
126622ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
126722ba64e8SJohn Dyson 		*reqpage = 0;
126822ba64e8SJohn Dyson 		marray[0] = m;
126922ba64e8SJohn Dyson 		return 1;
1270170db9c6SJohn Dyson 	}
127122ba64e8SJohn Dyson 
127222ba64e8SJohn Dyson 	if (rahead > cahead) {
127322ba64e8SJohn Dyson 		rahead = cahead;
127422ba64e8SJohn Dyson 	}
127522ba64e8SJohn Dyson 
1276170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1277170db9c6SJohn Dyson 		rbehind = cbehind;
1278170db9c6SJohn Dyson 	}
1279170db9c6SJohn Dyson 
128026f9a767SRodney W. Grimes 	/*
128126f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
128226f9a767SRodney W. Grimes 	 */
1283ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
128422ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1285f919ebdeSDavid Greenman 		pagedaemon_wakeup();
128626f9a767SRodney W. Grimes 		marray[0] = m;
12872d8acc0fSJohn Dyson 		*reqpage = 0;
128826f9a767SRodney W. Grimes 		return 1;
128926f9a767SRodney W. Grimes 	}
129022ba64e8SJohn Dyson 
129126f9a767SRodney W. Grimes 	/*
12922d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
129326f9a767SRodney W. Grimes 	 */
12942d8acc0fSJohn Dyson 	if (pindex > 0) {
12952d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1296a316d390SJohn Dyson 			rbehind = pindex;
12972d8acc0fSJohn Dyson 			startpindex = 0;
12982d8acc0fSJohn Dyson 		} else {
1299a316d390SJohn Dyson 			startpindex = pindex - rbehind;
13002d8acc0fSJohn Dyson 		}
13012d8acc0fSJohn Dyson 
13022d8acc0fSJohn Dyson 		for (tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1303a316d390SJohn Dyson 			if (vm_page_lookup(object, tpindex)) {
1304a316d390SJohn Dyson 				startpindex = tpindex + 1;
130526f9a767SRodney W. Grimes 				break;
130626f9a767SRodney W. Grimes 			}
1307a316d390SJohn Dyson 			if (tpindex == 0)
130826f9a767SRodney W. Grimes 				break;
1309317205caSDavid Greenman 		}
131026f9a767SRodney W. Grimes 
13112d8acc0fSJohn Dyson 		for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
131226f9a767SRodney W. Grimes 
13132d8acc0fSJohn Dyson 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1314ccbb2f72SJohn Dyson 			if (rtm == NULL) {
13152ad98273SAlan Cox 				vm_page_lock_queues();
1316ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
13174866e085SJohn Dyson 					vm_page_free(marray[j]);
131826f9a767SRodney W. Grimes 				}
13192ad98273SAlan Cox 				vm_page_unlock_queues();
132026f9a767SRodney W. Grimes 				marray[0] = m;
13212d8acc0fSJohn Dyson 				*reqpage = 0;
132226f9a767SRodney W. Grimes 				return 1;
132326f9a767SRodney W. Grimes 			}
1324170db9c6SJohn Dyson 
13252d8acc0fSJohn Dyson 			marray[i] = rtm;
132626f9a767SRodney W. Grimes 		}
13272d8acc0fSJohn Dyson 	} else {
13282d8acc0fSJohn Dyson 		startpindex = 0;
13292d8acc0fSJohn Dyson 		i = 0;
13302d8acc0fSJohn Dyson 	}
13312d8acc0fSJohn Dyson 
13322d8acc0fSJohn Dyson 	marray[i] = m;
13332d8acc0fSJohn Dyson 	/* page offset of the required page */
13342d8acc0fSJohn Dyson 	*reqpage = i;
13352d8acc0fSJohn Dyson 
13362d8acc0fSJohn Dyson 	tpindex = pindex + 1;
13372d8acc0fSJohn Dyson 	i++;
13382d8acc0fSJohn Dyson 
13392d8acc0fSJohn Dyson 	/*
13402d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
13412d8acc0fSJohn Dyson 	 */
13422d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
13432d8acc0fSJohn Dyson 	if (endpindex > object->size)
13442d8acc0fSJohn Dyson 		endpindex = object->size;
13452d8acc0fSJohn Dyson 
13462d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
13472d8acc0fSJohn Dyson 
13482d8acc0fSJohn Dyson 		if (vm_page_lookup(object, tpindex)) {
13492d8acc0fSJohn Dyson 			break;
13502d8acc0fSJohn Dyson 		}
13512d8acc0fSJohn Dyson 
13522d8acc0fSJohn Dyson 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
13532d8acc0fSJohn Dyson 		if (rtm == NULL) {
13542d8acc0fSJohn Dyson 			break;
13552d8acc0fSJohn Dyson 		}
13562d8acc0fSJohn Dyson 
13572d8acc0fSJohn Dyson 		marray[i] = rtm;
13582d8acc0fSJohn Dyson 	}
13592d8acc0fSJohn Dyson 
13602d8acc0fSJohn Dyson 	/* return number of bytes of pages */
13612d8acc0fSJohn Dyson 	return i;
136226f9a767SRodney W. Grimes }
1363