xref: /freebsd/sys/vm/vm_fault.c (revision ebf75125322418c4fed1edfcae67aa5f0229c671)
1df8bae1dSRodney W. Grimes /*
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 
10011caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
10126f9a767SRodney W. Grimes 
10247221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8
10347221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7
10426f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
10526f9a767SRodney W. Grimes 
1064866e085SJohn Dyson struct faultstate {
1074866e085SJohn Dyson 	vm_page_t m;
1084866e085SJohn Dyson 	vm_object_t object;
1094866e085SJohn Dyson 	vm_pindex_t pindex;
1104866e085SJohn Dyson 	vm_page_t first_m;
1114866e085SJohn Dyson 	vm_object_t	first_object;
1124866e085SJohn Dyson 	vm_pindex_t first_pindex;
1134866e085SJohn Dyson 	vm_map_t map;
1144866e085SJohn Dyson 	vm_map_entry_t entry;
11525adb370SBrian Feldman 	int lookup_still_valid;
1164866e085SJohn Dyson 	struct vnode *vp;
1174866e085SJohn Dyson };
1184866e085SJohn Dyson 
1191c7c3c6aSMatthew Dillon static __inline void
1204866e085SJohn Dyson release_page(struct faultstate *fs)
1214866e085SJohn Dyson {
1222ad98273SAlan Cox 	vm_page_lock_queues();
123e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
1244866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1252ad98273SAlan Cox 	vm_page_unlock_queues();
1264866e085SJohn Dyson 	fs->m = NULL;
1274866e085SJohn Dyson }
1284866e085SJohn Dyson 
1291c7c3c6aSMatthew Dillon static __inline void
1304866e085SJohn Dyson unlock_map(struct faultstate *fs)
1314866e085SJohn Dyson {
13225adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1334866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
13425adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1354866e085SJohn Dyson 	}
1364866e085SJohn Dyson }
1374866e085SJohn Dyson 
1384866e085SJohn Dyson static void
1394866e085SJohn Dyson _unlock_things(struct faultstate *fs, int dealloc)
1404866e085SJohn Dyson {
1410cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
142b009d5a0SAlan Cox 	VM_OBJECT_LOCK(fs->object);
1434866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
144b009d5a0SAlan Cox 	VM_OBJECT_UNLOCK(fs->object);
1454866e085SJohn Dyson 	if (fs->object != fs->first_object) {
146b009d5a0SAlan Cox 		VM_OBJECT_LOCK(fs->first_object);
1472ad98273SAlan Cox 		vm_page_lock_queues();
1484866e085SJohn Dyson 		vm_page_free(fs->first_m);
1492ad98273SAlan Cox 		vm_page_unlock_queues();
1504866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
151b009d5a0SAlan Cox 		VM_OBJECT_UNLOCK(fs->first_object);
1524866e085SJohn Dyson 		fs->first_m = NULL;
1534866e085SJohn Dyson 	}
1544866e085SJohn Dyson 	if (dealloc) {
1554866e085SJohn Dyson 		vm_object_deallocate(fs->first_object);
1564866e085SJohn Dyson 	}
1574866e085SJohn Dyson 	unlock_map(fs);
1584866e085SJohn Dyson 	if (fs->vp != NULL) {
1590cddd8f0SMatthew Dillon 		vput(fs->vp);
1604866e085SJohn Dyson 		fs->vp = NULL;
1614866e085SJohn Dyson 	}
1624866e085SJohn Dyson }
1634866e085SJohn Dyson 
1644866e085SJohn Dyson #define unlock_things(fs) _unlock_things(fs, 0)
1654866e085SJohn Dyson #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
1664866e085SJohn Dyson 
167df8bae1dSRodney W. Grimes /*
16840360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
16940360b1bSMatthew Dillon  *	      current object *might* contain the page.
17040360b1bSMatthew Dillon  *
17140360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
17240360b1bSMatthew Dillon  */
17340360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
17440360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
17540360b1bSMatthew Dillon 
17640360b1bSMatthew Dillon /*
177df8bae1dSRodney W. Grimes  *	vm_fault:
178df8bae1dSRodney W. Grimes  *
179956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
180df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
181df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
182df8bae1dSRodney W. Grimes  *	associated physical map.
183df8bae1dSRodney W. Grimes  *
184df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
185df8bae1dSRodney W. Grimes  *	proper page address.
186df8bae1dSRodney W. Grimes  *
187df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
188df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
189df8bae1dSRodney W. Grimes  *
190df8bae1dSRodney W. Grimes  *
191df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
1920cddd8f0SMatthew Dillon  *	Caller may hold no locks.
193df8bae1dSRodney W. Grimes  */
194df8bae1dSRodney W. Grimes int
19523955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
19623955314SAlfred Perlstein 	 int fault_flags)
19723955314SAlfred Perlstein {
198df8bae1dSRodney W. Grimes 	vm_prot_t prot;
199ebf75125SAlan Cox 	int is_first_object_locked, result;
2006139043bSAlan Cox 	boolean_t growstack, wired;
2012d8acc0fSJohn Dyson 	int map_generation;
202df8bae1dSRodney W. Grimes 	vm_object_t next_object;
20326f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2044866e085SJohn Dyson 	int hardfault;
2052d8acc0fSJohn Dyson 	int faultcount;
2064866e085SJohn Dyson 	struct faultstate fs;
207df8bae1dSRodney W. Grimes 
2084866e085SJohn Dyson 	hardfault = 0;
2096139043bSAlan Cox 	growstack = TRUE;
21092de35b0SAlan Cox 	atomic_add_int(&cnt.v_vm_faults, 1);
211df8bae1dSRodney W. Grimes 
21292de35b0SAlan Cox 	mtx_lock(&Giant);
213df8bae1dSRodney W. Grimes RetryFault:;
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes 	/*
2160d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2170d94caffSDavid Greenman 	 * search.
218df8bae1dSRodney W. Grimes 	 */
21940360b1bSMatthew Dillon 	fs.map = map;
22092de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
22192de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
22292de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
22392de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
22492de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2256139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2266139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2276139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
2287788e219SAlan Cox 				if (result != KERN_SUCCESS) {
2297788e219SAlan Cox 					mtx_unlock(&Giant);
2306139043bSAlan Cox 					return (KERN_FAILURE);
2317788e219SAlan Cox 				}
2326139043bSAlan Cox 				growstack = FALSE;
2336139043bSAlan Cox 				goto RetryFault;
2346139043bSAlan Cox 			}
2357788e219SAlan Cox 			mtx_unlock(&Giant);
23692de35b0SAlan Cox 			return (result);
23709e0c6ccSJohn Dyson 		}
23809e0c6ccSJohn Dyson 
2397aaaa4fdSJohn Dyson 		/*
2407aaaa4fdSJohn Dyson    		 * If we are user-wiring a r/w segment, and it is COW, then
2417aaaa4fdSJohn Dyson    		 * we need to do the COW operation.  Note that we don't COW
242595236dfSJohn Dyson    		 * currently RO sections now, because it is NOT desirable
2437aaaa4fdSJohn Dyson    		 * to COW .text.  We simply keep .text from ever being COW'ed
2447aaaa4fdSJohn Dyson    		 * and take the heat that one cannot debug wired .text sections.
2457aaaa4fdSJohn Dyson    		 */
2464866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr,
24747221757SJohn Dyson 			VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
2484866e085SJohn Dyson 			&fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
24947221757SJohn Dyson 		if (result != KERN_SUCCESS) {
2507788e219SAlan Cox 			mtx_unlock(&Giant);
2517788e219SAlan Cox 			return (result);
25247221757SJohn Dyson 		}
25347221757SJohn Dyson 
2547aaaa4fdSJohn Dyson 		/*
2557aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2567aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2577aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
258ff8f4ebeSAlan Cox 		 *
259ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
260ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2617aaaa4fdSJohn Dyson 		 */
2624866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2634866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2647aaaa4fdSJohn Dyson 	}
26547221757SJohn Dyson 
2664866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2672d8acc0fSJohn Dyson 
2684866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
26947221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
27092c4c4ebSBruce Evans 		    (u_long)vaddr);
2717aaaa4fdSJohn Dyson 	}
2727aaaa4fdSJohn Dyson 
27395e5e988SJohn Dyson 	/*
27495e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
27595e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
27695e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
27795e5e988SJohn Dyson 	 * they will stay around as well.
278fe8e0238SMatthew Dillon 	 *
279fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
280fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
281fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
282532eadefSAlan Cox 	 *
283532eadefSAlan Cox 	 * XXX vnode_pager_lock() can block without releasing the map lock.
28495e5e988SJohn Dyson 	 */
2854866e085SJohn Dyson 	vm_object_reference(fs.first_object);
286fe8e0238SMatthew Dillon 	fs.vp = vnode_pager_lock(fs.first_object);
287d22bc710SAlan Cox 	VM_OBJECT_LOCK(fs.first_object);
288d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
289d22bc710SAlan Cox 	VM_OBJECT_UNLOCK(fs.first_object);
29095e5e988SJohn Dyson 
29125adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
292df8bae1dSRodney W. Grimes 
293df8bae1dSRodney W. Grimes 	if (wired)
294df8bae1dSRodney W. Grimes 		fault_type = prot;
295df8bae1dSRodney W. Grimes 
2964866e085SJohn Dyson 	fs.first_m = NULL;
297df8bae1dSRodney W. Grimes 
298df8bae1dSRodney W. Grimes 	/*
299df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
300df8bae1dSRodney W. Grimes 	 */
3014866e085SJohn Dyson 	fs.object = fs.first_object;
3024866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
303df8bae1dSRodney W. Grimes 	while (TRUE) {
3041c7c3c6aSMatthew Dillon 		/*
3051c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3061c7c3c6aSMatthew Dillon 		 */
3074866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3084866e085SJohn Dyson 			unlock_and_deallocate(&fs);
3097788e219SAlan Cox 			mtx_unlock(&Giant);
31047221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
31147221757SJohn Dyson 		}
31247221757SJohn Dyson 
3131c7c3c6aSMatthew Dillon 		/*
3141c7c3c6aSMatthew Dillon 		 * See if page is resident
3151c7c3c6aSMatthew Dillon 		 */
3164866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3174866e085SJohn Dyson 		if (fs.m != NULL) {
318c699f45eSDavid Greenman 			int queue, s;
31998cb733cSKenneth D. Merry 
32098cb733cSKenneth D. Merry 			/*
3219b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3229b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3239b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3249b80d344SKenneth D. Merry 			 * used when the page in question is part of
3259b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3269b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3279b80d344SKenneth D. Merry 			 * which is not what we want.
32898cb733cSKenneth D. Merry 			 */
329f4ecdf05SAlan Cox 			vm_page_lock_queues();
33098cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3319b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3329b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
33398cb733cSKenneth D. Merry 				s = splvm();
33498cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
33598cb733cSKenneth D. Merry 				splx(s);
336f4ecdf05SAlan Cox 				vm_page_unlock_queues();
337d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
33898cb733cSKenneth D. Merry 				goto RetryFault;
33998cb733cSKenneth D. Merry 			}
34098cb733cSKenneth D. Merry 
341df8bae1dSRodney W. Grimes 			/*
3421c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3431c7c3c6aSMatthew Dillon 			 * if the page is busy via either PG_BUSY or
3441c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3451c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3461c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
347956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3481c7c3c6aSMatthew Dillon 			 *
3491c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3501c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3511c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3521c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3531c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3541c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3551c7c3c6aSMatthew Dillon 			 * to pmap it.
356df8bae1dSRodney W. Grimes 			 */
3571c7c3c6aSMatthew Dillon 			if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
3584abd55b2SAlan Cox 				vm_page_unlock_queues();
3594866e085SJohn Dyson 				unlock_things(&fs);
3609a96b638SAlan Cox 				vm_page_lock_queues();
3619a96b638SAlan Cox 				if (!vm_page_sleep_if_busy(fs.m, TRUE, "vmpfw"))
3629a96b638SAlan Cox 					vm_page_unlock_queues();
363976e77fcSDavid Greenman 				cnt.v_intrans++;
3644866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
365df8bae1dSRodney W. Grimes 				goto RetryFault;
366df8bae1dSRodney W. Grimes 			}
3674866e085SJohn Dyson 			queue = fs.m->queue;
368a1287949SEivind Eklund 
369c699f45eSDavid Greenman 			s = splvm();
3706d03d577SMatthew Dillon 			vm_pageq_remove_nowakeup(fs.m);
371c699f45eSDavid Greenman 			splx(s);
372c82b0181SJohn Dyson 
37340360b1bSMatthew Dillon 			if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
3744866e085SJohn Dyson 				vm_page_activate(fs.m);
3754abd55b2SAlan Cox 				vm_page_unlock_queues();
3764866e085SJohn Dyson 				unlock_and_deallocate(&fs);
377ef6020d1SMike Silbersack 				VM_WAITPFAULT;
37822ba64e8SJohn Dyson 				goto RetryFault;
37922ba64e8SJohn Dyson 			}
3807615edaaSMatthew Dillon 
3811c7c3c6aSMatthew Dillon 			/*
3821c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
3831c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
3841c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
3851c7c3c6aSMatthew Dillon 			 * found the page ).
3861c7c3c6aSMatthew Dillon 			 */
387e69763a3SDoug Rabson 			vm_page_busy(fs.m);
3884abd55b2SAlan Cox 			vm_page_unlock_queues();
3894866e085SJohn Dyson 			if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
3904866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
3910d94caffSDavid Greenman 				goto readrest;
3920d94caffSDavid Greenman 			}
393ffc82b0aSJohn Dyson 
394df8bae1dSRodney W. Grimes 			break;
395df8bae1dSRodney W. Grimes 		}
3961c7c3c6aSMatthew Dillon 
3971c7c3c6aSMatthew Dillon 		/*
39840360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
39940360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4001c7c3c6aSMatthew Dillon 		 */
40140360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4024866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4034866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4047788e219SAlan Cox 				mtx_unlock(&Giant);
4055f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4065f55e841SDavid Greenman 			}
40722ba64e8SJohn Dyson 
408df8bae1dSRodney W. Grimes 			/*
4090d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
410df8bae1dSRodney W. Grimes 			 */
41140360b1bSMatthew Dillon 			fs.m = NULL;
41240360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
4134866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4144866e085SJohn Dyson 				    (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
41540360b1bSMatthew Dillon 			}
4164866e085SJohn Dyson 			if (fs.m == NULL) {
4174866e085SJohn Dyson 				unlock_and_deallocate(&fs);
418ef6020d1SMike Silbersack 				VM_WAITPFAULT;
419df8bae1dSRodney W. Grimes 				goto RetryFault;
420df8bae1dSRodney W. Grimes 			}
421df8bae1dSRodney W. Grimes 		}
42247221757SJohn Dyson 
4230d94caffSDavid Greenman readrest:
4241c7c3c6aSMatthew Dillon 		/*
42540360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
42640360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
42740360b1bSMatthew Dillon 		 * valid.
42840360b1bSMatthew Dillon 		 *
42940360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
43040360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
43140360b1bSMatthew Dillon 		 * at the same time.
4321c7c3c6aSMatthew Dillon 		 */
43340360b1bSMatthew Dillon 		if (TRYPAGER) {
434df8bae1dSRodney W. Grimes 			int rv;
43526f9a767SRodney W. Grimes 			int reqpage;
436867a482dSJohn Dyson 			int ahead, behind;
4377f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
438867a482dSJohn Dyson 
4397f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
440867a482dSJohn Dyson 				ahead = 0;
441867a482dSJohn Dyson 				behind = 0;
4422d8acc0fSJohn Dyson 			} else {
4434866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4442d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4452d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4462d8acc0fSJohn Dyson 
4474866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4482d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4492d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
450867a482dSJohn Dyson 			}
451867a482dSJohn Dyson 
4524866e085SJohn Dyson 			if ((fs.first_object->type != OBJT_DEVICE) &&
45340360b1bSMatthew Dillon 			    (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
45440360b1bSMatthew Dillon                                 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
45540360b1bSMatthew Dillon                                 fs.pindex >= fs.entry->lastr &&
45640360b1bSMatthew Dillon                                 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
45740360b1bSMatthew Dillon 			) {
458867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
45940360b1bSMatthew Dillon 
46040360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
461867a482dSJohn Dyson 					firstpindex = 0;
462867a482dSJohn Dyson 				else
46340360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
464867a482dSJohn Dyson 
46515a5d210SAlan Cox 				vm_page_lock_queues();
4664221e284SAlan Cox 				/*
4674221e284SAlan Cox 				 * note: partially valid pages cannot be
4684221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
4694221e284SAlan Cox 				 * writes will barf on it badly.
4704221e284SAlan Cox 				 */
4714866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
4722100d645SPeter Wemm 					tmppindex >= firstpindex;
473867a482dSJohn Dyson 					--tmppindex) {
474867a482dSJohn Dyson 					vm_page_t mt;
475a1287949SEivind Eklund 
4764866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
477867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
478867a482dSJohn Dyson 						break;
479ff97964aSJohn Dyson 					if (mt->busy ||
4808b03c8edSMatthew Dillon 						(mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
481ff97964aSJohn Dyson 						mt->hold_count ||
482867a482dSJohn Dyson 						mt->wire_count)
483867a482dSJohn Dyson 						continue;
484867a482dSJohn Dyson 					if (mt->dirty == 0)
485867a482dSJohn Dyson 						vm_page_test_dirty(mt);
486867a482dSJohn Dyson 					if (mt->dirty) {
4874fec79beSAlan Cox 						pmap_remove_all(mt);
488867a482dSJohn Dyson 						vm_page_deactivate(mt);
489867a482dSJohn Dyson 					} else {
490867a482dSJohn Dyson 						vm_page_cache(mt);
491867a482dSJohn Dyson 					}
492867a482dSJohn Dyson 				}
49315a5d210SAlan Cox 				vm_page_unlock_queues();
494867a482dSJohn Dyson 				ahead += behind;
495867a482dSJohn Dyson 				behind = 0;
496867a482dSJohn Dyson 			}
497df8bae1dSRodney W. Grimes 
498df8bae1dSRodney W. Grimes 			/*
4990d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5000d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5010d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5020d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5030d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5040d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5050d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5060d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5070d94caffSDavid Greenman 			 * return value is the index into the marray for the
5080d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5091c7c3c6aSMatthew Dillon 			 *
5101c7c3c6aSMatthew Dillon 			 * fs.m plus the additional pages are PG_BUSY'd.
511532eadefSAlan Cox 			 *
512532eadefSAlan Cox 			 * XXX vm_fault_additional_pages() can block
513532eadefSAlan Cox 			 * without releasing the map lock.
51426f9a767SRodney W. Grimes 			 */
51505f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5164866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
517df8bae1dSRodney W. Grimes 
518df8bae1dSRodney W. Grimes 			/*
51940360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
52040360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
521532eadefSAlan Cox 			 *
522532eadefSAlan Cox 			 * XXX The following assignment modifies the map
523532eadefSAlan Cox 			 * without holding a write lock on it.
52440360b1bSMatthew Dillon 			 */
52540360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
52640360b1bSMatthew Dillon 
52740360b1bSMatthew Dillon 			/*
5280d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
5291c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
5301c7c3c6aSMatthew Dillon 			 * fs.object and the pages are PG_BUSY'd.
531df8bae1dSRodney W. Grimes 			 */
5324866e085SJohn Dyson 			unlock_map(&fs);
533df8bae1dSRodney W. Grimes 
53426f9a767SRodney W. Grimes 			rv = faultcount ?
5354866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
53624a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
53722ba64e8SJohn Dyson 
53826f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
539df8bae1dSRodney W. Grimes 				/*
540f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
541f230c45cSJohn Dyson 				 * with it.
542f230c45cSJohn Dyson 				 */
543f230c45cSJohn Dyson 
544f230c45cSJohn Dyson 				/*
5450d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5460d94caffSDavid Greenman 				 * is responsible for disposition of old page
5470d94caffSDavid Greenman 				 * if moved.
548df8bae1dSRodney W. Grimes 				 */
5494866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5504866e085SJohn Dyson 				if (!fs.m) {
5514866e085SJohn Dyson 					unlock_and_deallocate(&fs);
552f6b04d2bSDavid Greenman 					goto RetryFault;
553f6b04d2bSDavid Greenman 				}
554f6b04d2bSDavid Greenman 
55526f9a767SRodney W. Grimes 				hardfault++;
5561c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
557df8bae1dSRodney W. Grimes 			}
558df8bae1dSRodney W. Grimes 			/*
5590d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5600d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5610d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
562df8bae1dSRodney W. Grimes 			 *
56324a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5640d94caffSDavid Greenman 			 * in this page.
565df8bae1dSRodney W. Grimes 			 *
5660d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
56724a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
5680d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
5690d94caffSDavid Greenman 			 * the same time that we are.
570df8bae1dSRodney W. Grimes 			 */
571a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
572f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
573f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
57426f9a767SRodney W. Grimes 			/*
575a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
57626f9a767SRodney W. Grimes 			 */
577a83c285cSDavid Greenman 			/*
5780d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
5790d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
5800d94caffSDavid Greenman 			 * fault w/ I/O error.
581a83c285cSDavid Greenman 			 */
5824866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
58347221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
5842ad98273SAlan Cox 				vm_page_lock_queues();
5854866e085SJohn Dyson 				vm_page_free(fs.m);
5862ad98273SAlan Cox 				vm_page_unlock_queues();
5874866e085SJohn Dyson 				fs.m = NULL;
5884866e085SJohn Dyson 				unlock_and_deallocate(&fs);
5897788e219SAlan Cox 				mtx_unlock(&Giant);
590a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
59126f9a767SRodney W. Grimes 			}
5924866e085SJohn Dyson 			if (fs.object != fs.first_object) {
5932ad98273SAlan Cox 				vm_page_lock_queues();
5944866e085SJohn Dyson 				vm_page_free(fs.m);
5952ad98273SAlan Cox 				vm_page_unlock_queues();
5964866e085SJohn Dyson 				fs.m = NULL;
59726f9a767SRodney W. Grimes 				/*
59826f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
59926f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
60026f9a767SRodney W. Grimes 				 */
601df8bae1dSRodney W. Grimes 			}
602df8bae1dSRodney W. Grimes 		}
60340360b1bSMatthew Dillon 
604df8bae1dSRodney W. Grimes 		/*
6051c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6061c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
607df8bae1dSRodney W. Grimes 		 */
6084866e085SJohn Dyson 		if (fs.object == fs.first_object)
6094866e085SJohn Dyson 			fs.first_m = fs.m;
610df8bae1dSRodney W. Grimes 
611df8bae1dSRodney W. Grimes 		/*
6120d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6130d94caffSDavid Greenman 		 * unlocking the current one.
614df8bae1dSRodney W. Grimes 		 */
6154866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6164866e085SJohn Dyson 		next_object = fs.object->backing_object;
617df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
618df8bae1dSRodney W. Grimes 			/*
6190d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6200d94caffSDavid Greenman 			 * object with zeros.
621df8bae1dSRodney W. Grimes 			 */
6224866e085SJohn Dyson 			if (fs.object != fs.first_object) {
623b009d5a0SAlan Cox 				VM_OBJECT_LOCK(fs.object);
6244866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
625b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
626df8bae1dSRodney W. Grimes 
6274866e085SJohn Dyson 				fs.object = fs.first_object;
6284866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6294866e085SJohn Dyson 				fs.m = fs.first_m;
630df8bae1dSRodney W. Grimes 			}
6314866e085SJohn Dyson 			fs.first_m = NULL;
632df8bae1dSRodney W. Grimes 
6334221e284SAlan Cox 			/*
6344221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6354221e284SAlan Cox 			 */
6364866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
637fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6384221e284SAlan Cox 			} else {
63914286e5eSAlan Cox 				cnt.v_ozfod++;
6404221e284SAlan Cox 			}
641df8bae1dSRodney W. Grimes 			cnt.v_zfod++;
6424221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6431c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6440d94caffSDavid Greenman 		} else {
6454866e085SJohn Dyson 			if (fs.object != fs.first_object) {
646b009d5a0SAlan Cox 				VM_OBJECT_LOCK(fs.object);
6474866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
648b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
649c0503609SDavid Greenman 			}
6501c7c3c6aSMatthew Dillon 			KASSERT(fs.object != next_object, ("object loop %p", next_object));
6514866e085SJohn Dyson 			fs.object = next_object;
652d22bc710SAlan Cox 			VM_OBJECT_LOCK(fs.object);
653d474eaaaSDoug Rabson 			vm_object_pip_add(fs.object, 1);
654d22bc710SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
655df8bae1dSRodney W. Grimes 		}
656df8bae1dSRodney W. Grimes 	}
6571c7c3c6aSMatthew Dillon 
6585526d2d9SEivind Eklund 	KASSERT((fs.m->flags & PG_BUSY) != 0,
6595526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	/*
6620d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
663df8bae1dSRodney W. Grimes 	 * is held.]
664df8bae1dSRodney W. Grimes 	 */
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes 	/*
6670d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6680d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6690d94caffSDavid Greenman 	 * top-level object.
670df8bae1dSRodney W. Grimes 	 */
6714866e085SJohn Dyson 	if (fs.object != fs.first_object) {
672df8bae1dSRodney W. Grimes 		/*
6730d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
674df8bae1dSRodney W. Grimes 		 */
675df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
676df8bae1dSRodney W. Grimes 			/*
6771c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
6781c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
6791c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
6801c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
6811c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
6821c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
6831c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
6841c7c3c6aSMatthew Dillon 			 * to swap when needed.
685df8bae1dSRodney W. Grimes 			 */
686ebf75125SAlan Cox 			is_first_object_locked = FALSE;
687e50346b5SAlan Cox 			if (
688de5f6a77SJohn Dyson 				/*
689de5f6a77SJohn Dyson 				 * Only one shadow object
690de5f6a77SJohn Dyson 				 */
6914866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
692de5f6a77SJohn Dyson 				/*
693de5f6a77SJohn Dyson 				 * No COW refs, except us
694de5f6a77SJohn Dyson 				 */
6954866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
696de5f6a77SJohn Dyson 				/*
6975929bcfaSPhilippe Charnier 				 * No one else can look this object up
698de5f6a77SJohn Dyson 				 */
6994866e085SJohn Dyson 				(fs.object->handle == NULL) &&
700de5f6a77SJohn Dyson 				/*
701de5f6a77SJohn Dyson 				 * No other ways to look the object up
702de5f6a77SJohn Dyson 				 */
7034866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7044866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
705ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
706de5f6a77SJohn Dyson 				/*
707de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
708de5f6a77SJohn Dyson 				 */
709e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
7102d8acc0fSJohn Dyson 				/*
711de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
712df8bae1dSRodney W. Grimes 				 */
7132ad98273SAlan Cox 				vm_page_lock_queues();
7144fec79beSAlan Cox 				pmap_remove_all(fs.first_m);
7154866e085SJohn Dyson 				vm_page_free(fs.first_m);
7164866e085SJohn Dyson 				fs.first_m = NULL;
7174866e085SJohn Dyson 
718de5f6a77SJohn Dyson 				/*
7191c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7201c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7211c7c3c6aSMatthew Dillon 				 * automatically made dirty.
722de5f6a77SJohn Dyson 				 */
7234866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
7244866e085SJohn Dyson 				fs.first_m = fs.m;
725e69763a3SDoug Rabson 				vm_page_busy(fs.first_m);
7269a96b638SAlan Cox 				vm_page_unlock_queues();
7274866e085SJohn Dyson 				fs.m = NULL;
7284866e085SJohn Dyson 				cnt.v_cow_optim++;
729de5f6a77SJohn Dyson 			} else {
730de5f6a77SJohn Dyson 				/*
731de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
732de5f6a77SJohn Dyson 				 */
7334866e085SJohn Dyson 				vm_page_copy(fs.m, fs.first_m);
734de5f6a77SJohn Dyson 			}
735ebf75125SAlan Cox 			if (is_first_object_locked)
736ebf75125SAlan Cox /*XXX*/				VM_OBJECT_UNLOCK(fs.first_object);
7374866e085SJohn Dyson 			if (fs.m) {
738df8bae1dSRodney W. Grimes 				/*
739df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
740df8bae1dSRodney W. Grimes 				 */
7414866e085SJohn Dyson 				release_page(&fs);
742de5f6a77SJohn Dyson 			}
743df8bae1dSRodney W. Grimes 
7441c7c3c6aSMatthew Dillon 			/*
7451c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7461c7c3c6aSMatthew Dillon 			 * conditional
7471c7c3c6aSMatthew Dillon 			 */
748b009d5a0SAlan Cox 			VM_OBJECT_LOCK(fs.object);
7494866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
750b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
7511c7c3c6aSMatthew Dillon 
752df8bae1dSRodney W. Grimes 			/*
753df8bae1dSRodney W. Grimes 			 * Only use the new page below...
754df8bae1dSRodney W. Grimes 			 */
755df8bae1dSRodney W. Grimes 			cnt.v_cow_faults++;
7564866e085SJohn Dyson 			fs.m = fs.first_m;
7574866e085SJohn Dyson 			fs.object = fs.first_object;
7584866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
759df8bae1dSRodney W. Grimes 
7600d94caffSDavid Greenman 		} else {
761df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
762df8bae1dSRodney W. Grimes 		}
763df8bae1dSRodney W. Grimes 	}
764df8bae1dSRodney W. Grimes 
765df8bae1dSRodney W. Grimes 	/*
7660d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7670d94caffSDavid Greenman 	 * lookup.
768df8bae1dSRodney W. Grimes 	 */
76925adb370SBrian Feldman 	if (!fs.lookup_still_valid &&
7704866e085SJohn Dyson 		(fs.map->timestamp != map_generation)) {
771df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
772a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
773df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
774df8bae1dSRodney W. Grimes 
775df8bae1dSRodney W. Grimes 		/*
7760d94caffSDavid Greenman 		 * Since map entries may be pageable, make sure we can take a
7770d94caffSDavid Greenman 		 * page fault on them.
778df8bae1dSRodney W. Grimes 		 */
779df8bae1dSRodney W. Grimes 
780df8bae1dSRodney W. Grimes 		/*
781b33fb764SMatthew Dillon 		 * Unlock vnode before the lookup to avoid deadlock.   E.G.
782b33fb764SMatthew Dillon 		 * avoid a deadlock between the inode and exec_map that can
783b33fb764SMatthew Dillon 		 * occur due to locks being obtained in different orders.
784b33fb764SMatthew Dillon 		 */
785b33fb764SMatthew Dillon 		if (fs.vp != NULL) {
786b33fb764SMatthew Dillon 			vput(fs.vp);
787b33fb764SMatthew Dillon 			fs.vp = NULL;
788b33fb764SMatthew Dillon 		}
789b823bbd6SMatthew Dillon 
790b823bbd6SMatthew Dillon 		if (fs.map->infork) {
791b823bbd6SMatthew Dillon 			release_page(&fs);
792b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
793b823bbd6SMatthew Dillon 			goto RetryFault;
794b823bbd6SMatthew Dillon 		}
795b33fb764SMatthew Dillon 
796b33fb764SMatthew Dillon 		/*
79724a1cce3SDavid Greenman 		 * To avoid trying to write_lock the map while another process
7980d94caffSDavid Greenman 		 * has it read_locked (in vm_map_pageable), we do not try for
7990d94caffSDavid Greenman 		 * write permission.  If the page is still writable, we will
8000d94caffSDavid Greenman 		 * get write permission.  If it is not, or has been marked
8010d94caffSDavid Greenman 		 * needs_copy, we enter the mapping without write permission,
8020d94caffSDavid Greenman 		 * and will merely take another fault.
803df8bae1dSRodney W. Grimes 		 */
8044866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
8054866e085SJohn Dyson 		    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
8064866e085SJohn Dyson 		map_generation = fs.map->timestamp;
807df8bae1dSRodney W. Grimes 
808df8bae1dSRodney W. Grimes 		/*
8090d94caffSDavid Greenman 		 * If we don't need the page any longer, put it on the active
8100d94caffSDavid Greenman 		 * list (the easiest thing to do here).  If no one needs it,
8110d94caffSDavid Greenman 		 * pageout will grab it eventually.
812df8bae1dSRodney W. Grimes 		 */
813df8bae1dSRodney W. Grimes 		if (result != KERN_SUCCESS) {
8144866e085SJohn Dyson 			release_page(&fs);
8154866e085SJohn Dyson 			unlock_and_deallocate(&fs);
8167788e219SAlan Cox 			mtx_unlock(&Giant);
817df8bae1dSRodney W. Grimes 			return (result);
818df8bae1dSRodney W. Grimes 		}
81925adb370SBrian Feldman 		fs.lookup_still_valid = TRUE;
820df8bae1dSRodney W. Grimes 
8214866e085SJohn Dyson 		if ((retry_object != fs.first_object) ||
8224866e085SJohn Dyson 		    (retry_pindex != fs.first_pindex)) {
8234866e085SJohn Dyson 			release_page(&fs);
8244866e085SJohn Dyson 			unlock_and_deallocate(&fs);
825df8bae1dSRodney W. Grimes 			goto RetryFault;
826df8bae1dSRodney W. Grimes 		}
827df8bae1dSRodney W. Grimes 		/*
8280d94caffSDavid Greenman 		 * Check whether the protection has changed or the object has
8290d94caffSDavid Greenman 		 * been copied while we left the map unlocked. Changing from
8300d94caffSDavid Greenman 		 * read to write permission is OK - we leave the page
8310d94caffSDavid Greenman 		 * write-protected, and catch the write fault. Changing from
8320d94caffSDavid Greenman 		 * write to read permission means that we can't mark the page
8330d94caffSDavid Greenman 		 * write-enabled after all.
834df8bae1dSRodney W. Grimes 		 */
835df8bae1dSRodney W. Grimes 		prot &= retry_prot;
836df8bae1dSRodney W. Grimes 	}
837df8bae1dSRodney W. Grimes 
838df8bae1dSRodney W. Grimes 	/*
8390d94caffSDavid Greenman 	 * Put this page into the physical map. We had to do the unlock above
8400d94caffSDavid Greenman 	 * because pmap_enter may cause other faults.   We don't put the page
8410d94caffSDavid Greenman 	 * back on the active queue until later so that the page-out daemon
8420d94caffSDavid Greenman 	 * won't find us (yet).
843df8bae1dSRodney W. Grimes 	 */
844df8bae1dSRodney W. Grimes 
8452ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
8469a96b638SAlan Cox 		vm_page_lock_queues();
847e69763a3SDoug Rabson 		vm_page_flag_set(fs.m, PG_WRITEABLE);
848245df27cSMatthew Dillon 		vm_object_set_writeable_dirty(fs.m->object);
8494f79d873SMatthew Dillon 
8502ddba215SDavid Greenman 		/*
8512ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8524f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8534f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8544f79d873SMatthew Dillon 		 *
8554f79d873SMatthew Dillon 		 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
8564f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8574f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8584f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8594f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8604f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8611c7c3c6aSMatthew Dillon 		 *
8621c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8631c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8642ddba215SDavid Greenman 		 */
8654f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8664f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
8674f79d873SMatthew Dillon 				vm_page_flag_set(fs.m, PG_NOSYNC);
8684f79d873SMatthew Dillon 		} else {
8694f79d873SMatthew Dillon 			vm_page_flag_clear(fs.m, PG_NOSYNC);
8704f79d873SMatthew Dillon 		}
8719a96b638SAlan Cox 		vm_page_unlock_queues();
8725bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8735bf53acbSMatthew Dillon 			int s;
8747dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
875ef0646f9SMatthew Dillon 			s = splvm();
8761c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
877ef0646f9SMatthew Dillon 			splx(s);
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));
8864866e085SJohn Dyson 	unlock_things(&fs);
8874221e284SAlan Cox 
8884221e284SAlan Cox 	/*
8894221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8904221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8914221e284SAlan Cox 	 */
8924221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8934221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8944221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8954221e284SAlan 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)) {
8984866e085SJohn Dyson 		pmap_prefault(fs.map->pmap, vaddr, fs.entry);
8992d8acc0fSJohn Dyson 	}
9002d09a6adSAlan Cox 	vm_page_lock_queues();
9014221e284SAlan Cox 	vm_page_flag_clear(fs.m, PG_ZERO);
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 
919eeec6babSJohn Baldwin 	PROC_LOCK(curproc);
920eeec6babSJohn Baldwin 	if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
92126f9a767SRodney W. Grimes 		if (hardfault) {
92226f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
92326f9a767SRodney W. Grimes 		} else {
92426f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
92526f9a767SRodney W. Grimes 		}
92626f9a767SRodney W. Grimes 	}
927eeec6babSJohn Baldwin 	PROC_UNLOCK(curproc);
928df8bae1dSRodney W. Grimes 
929df8bae1dSRodney W. Grimes 	/*
930df8bae1dSRodney W. Grimes 	 * Unlock everything, and return
931df8bae1dSRodney W. Grimes 	 */
9324866e085SJohn Dyson 	vm_object_deallocate(fs.first_object);
9337788e219SAlan Cox 	mtx_unlock(&Giant);
934df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
935df8bae1dSRodney W. Grimes }
936df8bae1dSRodney W. Grimes 
937df8bae1dSRodney W. Grimes /*
938e3669ceeSMatthew Dillon  *	vm_fault_quick:
939e3669ceeSMatthew Dillon  *
940e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
941e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
942e3669ceeSMatthew Dillon  */
943e3669ceeSMatthew Dillon int
944e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
945e3669ceeSMatthew Dillon {
946e3669ceeSMatthew Dillon 	int r;
947e3669ceeSMatthew Dillon 
948e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
949e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
950e3669ceeSMatthew Dillon 	else
951e3669ceeSMatthew Dillon 		r = fubyte(v);
952e3669ceeSMatthew Dillon 	return(r);
953e3669ceeSMatthew Dillon }
954e3669ceeSMatthew Dillon 
955e3669ceeSMatthew Dillon /*
956df8bae1dSRodney W. Grimes  *	vm_fault_wire:
957df8bae1dSRodney W. Grimes  *
958df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
959df8bae1dSRodney W. Grimes  */
960df8bae1dSRodney W. Grimes int
961ef594d31SAlan Cox vm_fault_wire(map, start, end, user_wire)
962df8bae1dSRodney W. Grimes 	vm_map_t map;
963df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
964ef594d31SAlan Cox 	boolean_t user_wire;
965df8bae1dSRodney W. Grimes {
96654d92145SMatthew Dillon 	vm_offset_t va;
967df8bae1dSRodney W. Grimes 	int rv;
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes 	/*
9700d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
971ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
972ef594d31SAlan Cox 	 * read-only sections.
973df8bae1dSRodney W. Grimes 	 */
974df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
975ef594d31SAlan Cox 		rv = vm_fault(map, va,
976ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
977ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
9787aaaa4fdSJohn Dyson 		if (rv) {
9797aaaa4fdSJohn Dyson 			if (va != start)
9807aaaa4fdSJohn Dyson 				vm_fault_unwire(map, start, va);
9817aaaa4fdSJohn Dyson 			return (rv);
9827aaaa4fdSJohn Dyson 		}
9837aaaa4fdSJohn Dyson 	}
9847aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
9857aaaa4fdSJohn Dyson }
9867aaaa4fdSJohn Dyson 
9877aaaa4fdSJohn Dyson /*
988df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
989df8bae1dSRodney W. Grimes  *
990df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
991df8bae1dSRodney W. Grimes  */
99226f9a767SRodney W. Grimes void
99326f9a767SRodney W. Grimes vm_fault_unwire(map, start, end)
994df8bae1dSRodney W. Grimes 	vm_map_t map;
995df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
996df8bae1dSRodney W. Grimes {
997227f9a1cSJake Burkholder 	vm_paddr_t pa;
998227f9a1cSJake Burkholder 	vm_offset_t va;
99954d92145SMatthew Dillon 	pmap_t pmap;
1000df8bae1dSRodney W. Grimes 
1001df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1002df8bae1dSRodney W. Grimes 
10034b9fdc2bSAlan Cox 	mtx_lock(&Giant);
1004df8bae1dSRodney W. Grimes 	/*
10050d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
10060d94caffSDavid Greenman 	 * mappings from the physical map system.
1007df8bae1dSRodney W. Grimes 	 */
1008df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1009df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1010227f9a1cSJake Burkholder 		if (pa != 0) {
1011df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
10122d09a6adSAlan Cox 			vm_page_lock_queues();
101373007561SDavid Greenman 			vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
10142d09a6adSAlan Cox 			vm_page_unlock_queues();
1015df8bae1dSRodney W. Grimes 		}
1016b18bfc3dSJohn Dyson 	}
1017042bb299SAlan Cox 	mtx_unlock(&Giant);
1018df8bae1dSRodney W. Grimes }
1019df8bae1dSRodney W. Grimes 
1020df8bae1dSRodney W. Grimes /*
1021df8bae1dSRodney W. Grimes  *	Routine:
1022df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1023df8bae1dSRodney W. Grimes  *	Function:
1024df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1025df8bae1dSRodney W. Grimes  *
1026df8bae1dSRodney W. Grimes  *	In/out conditions:
1027df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1028df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1029df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1030df8bae1dSRodney W. Grimes  */
103126f9a767SRodney W. Grimes void
103226f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1033df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1034df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1035df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1036df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1037df8bae1dSRodney W. Grimes {
1038df8bae1dSRodney W. Grimes 	vm_object_t dst_object;
1039df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1040a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1041a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1042df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1043df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1044df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1045df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes #ifdef	lint
1048df8bae1dSRodney W. Grimes 	src_map++;
10490d94caffSDavid Greenman #endif	/* lint */
1050df8bae1dSRodney W. Grimes 
1051df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1052df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1053df8bae1dSRodney W. Grimes 
1054df8bae1dSRodney W. Grimes 	/*
10550d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
10560d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1057df8bae1dSRodney W. Grimes 	 */
105824a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
1059a316d390SJohn Dyson 	    (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1062df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1063df8bae1dSRodney W. Grimes 
1064df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1065df8bae1dSRodney W. Grimes 
1066df8bae1dSRodney W. Grimes 	/*
10670d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
10680d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
10690d94caffSDavid Greenman 	 * object.
1070df8bae1dSRodney W. Grimes 	 */
1071df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1072df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1073df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1074df8bae1dSRodney W. Grimes 
1075df8bae1dSRodney W. Grimes 		/*
1076df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1077df8bae1dSRodney W. Grimes 		 */
1078df8bae1dSRodney W. Grimes 		do {
1079a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1080a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1081df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
1082df8bae1dSRodney W. Grimes 				VM_WAIT;
1083df8bae1dSRodney W. Grimes 			}
1084df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes 		/*
1087df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
10880d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
10890d94caffSDavid Greenman 		 * memory.)
1090df8bae1dSRodney W. Grimes 		 */
1091a316d390SJohn Dyson 		src_m = vm_page_lookup(src_object,
1092a316d390SJohn Dyson 			OFF_TO_IDX(dst_offset + src_offset));
1093df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1094df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1095df8bae1dSRodney W. Grimes 
1096df8bae1dSRodney W. Grimes 		vm_page_copy(src_m, dst_m);
1097df8bae1dSRodney W. Grimes 
1098df8bae1dSRodney W. Grimes 		/*
1099df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
1100df8bae1dSRodney W. Grimes 		 */
11010385347cSPeter Wemm 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
11024abd55b2SAlan Cox 		vm_page_lock_queues();
1103db44450bSAlan Cox 		vm_page_flag_set(dst_m, PG_WRITEABLE);
1104df8bae1dSRodney W. Grimes 
1105df8bae1dSRodney W. Grimes 		/*
1106df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1107df8bae1dSRodney W. Grimes 		 */
1108df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
1109e69763a3SDoug Rabson 		vm_page_wakeup(dst_m);
11104abd55b2SAlan Cox 		vm_page_unlock_queues();
1111df8bae1dSRodney W. Grimes 	}
1112df8bae1dSRodney W. Grimes }
111326f9a767SRodney W. Grimes 
111426f9a767SRodney W. Grimes 
111526f9a767SRodney W. Grimes /*
111626f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
111722ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
111822ba64e8SJohn Dyson  * pages for the pages to be paged in.
111926f9a767SRodney W. Grimes  *
112026f9a767SRodney W. Grimes  * Inputs:
112122ba64e8SJohn Dyson  *	m, rbehind, rahead
112226f9a767SRodney W. Grimes  *
112326f9a767SRodney W. Grimes  * Outputs:
112426f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
112526f9a767SRodney W. Grimes  *
112626f9a767SRodney W. Grimes  * Return value:
112726f9a767SRodney W. Grimes  *  number of pages in marray
112823955314SAlfred Perlstein  *
112923955314SAlfred Perlstein  * This routine can't block.
113026f9a767SRodney W. Grimes  */
1131303b270bSEivind Eklund static int
113222ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
113326f9a767SRodney W. Grimes 	vm_page_t m;
113426f9a767SRodney W. Grimes 	int rbehind;
113522ba64e8SJohn Dyson 	int rahead;
113626f9a767SRodney W. Grimes 	vm_page_t *marray;
113726f9a767SRodney W. Grimes 	int *reqpage;
113826f9a767SRodney W. Grimes {
11392d8acc0fSJohn Dyson 	int i,j;
114026f9a767SRodney W. Grimes 	vm_object_t object;
1141a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
114226f9a767SRodney W. Grimes 	vm_page_t rtm;
1143170db9c6SJohn Dyson 	int cbehind, cahead;
114426f9a767SRodney W. Grimes 
11450cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
114623955314SAlfred Perlstein 
114726f9a767SRodney W. Grimes 	object = m->object;
1148a316d390SJohn Dyson 	pindex = m->pindex;
114926f9a767SRodney W. Grimes 
115026f9a767SRodney W. Grimes 	/*
1151f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1152f35329acSJohn Dyson 	 */
1153f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1154f35329acSJohn Dyson 		*reqpage = 0;
1155f35329acSJohn Dyson 		marray[0] = m;
1156f35329acSJohn Dyson 		return 1;
1157f35329acSJohn Dyson 	}
1158f35329acSJohn Dyson 
1159f35329acSJohn Dyson 	/*
116026f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
116126f9a767SRodney W. Grimes 	 */
11621c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
116326f9a767SRodney W. Grimes 		return 0;
11642d8acc0fSJohn Dyson 	}
116526f9a767SRodney W. Grimes 
116622ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
116722ba64e8SJohn Dyson 		*reqpage = 0;
116822ba64e8SJohn Dyson 		marray[0] = m;
116922ba64e8SJohn Dyson 		return 1;
1170170db9c6SJohn Dyson 	}
117122ba64e8SJohn Dyson 
117222ba64e8SJohn Dyson 	if (rahead > cahead) {
117322ba64e8SJohn Dyson 		rahead = cahead;
117422ba64e8SJohn Dyson 	}
117522ba64e8SJohn Dyson 
1176170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1177170db9c6SJohn Dyson 		rbehind = cbehind;
1178170db9c6SJohn Dyson 	}
1179170db9c6SJohn Dyson 
118026f9a767SRodney W. Grimes 	/*
118126f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
118226f9a767SRodney W. Grimes 	 */
1183ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
118422ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1185f919ebdeSDavid Greenman 		pagedaemon_wakeup();
118626f9a767SRodney W. Grimes 		marray[0] = m;
11872d8acc0fSJohn Dyson 		*reqpage = 0;
118826f9a767SRodney W. Grimes 		return 1;
118926f9a767SRodney W. Grimes 	}
119022ba64e8SJohn Dyson 
119126f9a767SRodney W. Grimes 	/*
11922d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
119326f9a767SRodney W. Grimes 	 */
11942d8acc0fSJohn Dyson 	if (pindex > 0) {
11952d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1196a316d390SJohn Dyson 			rbehind = pindex;
11972d8acc0fSJohn Dyson 			startpindex = 0;
11982d8acc0fSJohn Dyson 		} else {
1199a316d390SJohn Dyson 			startpindex = pindex - rbehind;
12002d8acc0fSJohn Dyson 		}
12012d8acc0fSJohn Dyson 
12022d8acc0fSJohn Dyson 		for (tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1203a316d390SJohn Dyson 			if (vm_page_lookup(object, tpindex)) {
1204a316d390SJohn Dyson 				startpindex = tpindex + 1;
120526f9a767SRodney W. Grimes 				break;
120626f9a767SRodney W. Grimes 			}
1207a316d390SJohn Dyson 			if (tpindex == 0)
120826f9a767SRodney W. Grimes 				break;
1209317205caSDavid Greenman 		}
121026f9a767SRodney W. Grimes 
12112d8acc0fSJohn Dyson 		for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
121226f9a767SRodney W. Grimes 
12132d8acc0fSJohn Dyson 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1214ccbb2f72SJohn Dyson 			if (rtm == NULL) {
12152ad98273SAlan Cox 				vm_page_lock_queues();
1216ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
12174866e085SJohn Dyson 					vm_page_free(marray[j]);
121826f9a767SRodney W. Grimes 				}
12192ad98273SAlan Cox 				vm_page_unlock_queues();
122026f9a767SRodney W. Grimes 				marray[0] = m;
12212d8acc0fSJohn Dyson 				*reqpage = 0;
122226f9a767SRodney W. Grimes 				return 1;
122326f9a767SRodney W. Grimes 			}
1224170db9c6SJohn Dyson 
12252d8acc0fSJohn Dyson 			marray[i] = rtm;
122626f9a767SRodney W. Grimes 		}
12272d8acc0fSJohn Dyson 	} else {
12282d8acc0fSJohn Dyson 		startpindex = 0;
12292d8acc0fSJohn Dyson 		i = 0;
12302d8acc0fSJohn Dyson 	}
12312d8acc0fSJohn Dyson 
12322d8acc0fSJohn Dyson 	marray[i] = m;
12332d8acc0fSJohn Dyson 	/* page offset of the required page */
12342d8acc0fSJohn Dyson 	*reqpage = i;
12352d8acc0fSJohn Dyson 
12362d8acc0fSJohn Dyson 	tpindex = pindex + 1;
12372d8acc0fSJohn Dyson 	i++;
12382d8acc0fSJohn Dyson 
12392d8acc0fSJohn Dyson 	/*
12402d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
12412d8acc0fSJohn Dyson 	 */
12422d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
12432d8acc0fSJohn Dyson 	if (endpindex > object->size)
12442d8acc0fSJohn Dyson 		endpindex = object->size;
12452d8acc0fSJohn Dyson 
12462d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
12472d8acc0fSJohn Dyson 
12482d8acc0fSJohn Dyson 		if (vm_page_lookup(object, tpindex)) {
12492d8acc0fSJohn Dyson 			break;
12502d8acc0fSJohn Dyson 		}
12512d8acc0fSJohn Dyson 
12522d8acc0fSJohn Dyson 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
12532d8acc0fSJohn Dyson 		if (rtm == NULL) {
12542d8acc0fSJohn Dyson 			break;
12552d8acc0fSJohn Dyson 		}
12562d8acc0fSJohn Dyson 
12572d8acc0fSJohn Dyson 		marray[i] = rtm;
12582d8acc0fSJohn Dyson 	}
12592d8acc0fSJohn Dyson 
12602d8acc0fSJohn Dyson 	/* return number of bytes of pages */
12612d8acc0fSJohn Dyson 	return i;
126226f9a767SRodney W. Grimes }
1263