xref: /freebsd/sys/vm/vm_fault.c (revision 92de35b0cefe448b08dfc1ec55bb0791884d0663)
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.
683c4dd356SDavid Greenman  *
69c3aac50fSPeter Wemm  * $FreeBSD$
70df8bae1dSRodney W. Grimes  */
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  *	Page fault handling module.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes #include <sys/param.h>
76df8bae1dSRodney W. Grimes #include <sys/systm.h>
774edf4a58SJohn Baldwin #include <sys/kernel.h>
78fb919e4dSMark Murray #include <sys/lock.h>
79fb919e4dSMark Murray #include <sys/mutex.h>
8026f9a767SRodney W. Grimes #include <sys/proc.h>
8126f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8223955314SAlfred Perlstein #include <sys/sysctl.h>
834edf4a58SJohn Baldwin #include <sys/vmmeter.h>
844edf4a58SJohn Baldwin #include <sys/vnode.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90efeaf95aSDavid Greenman #include <vm/vm_object.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
93a83c285cSDavid Greenman #include <vm/vm_kern.h>
9424a1cce3SDavid Greenman #include <vm/vm_pager.h>
9524a1cce3SDavid Greenman #include <vm/vnode_pager.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
97df8bae1dSRodney W. Grimes 
9811caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
9926f9a767SRodney W. Grimes 
10047221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8
10147221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7
10226f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
10326f9a767SRodney W. Grimes 
1044866e085SJohn Dyson struct faultstate {
1054866e085SJohn Dyson 	vm_page_t m;
1064866e085SJohn Dyson 	vm_object_t object;
1074866e085SJohn Dyson 	vm_pindex_t pindex;
1084866e085SJohn Dyson 	vm_page_t first_m;
1094866e085SJohn Dyson 	vm_object_t	first_object;
1104866e085SJohn Dyson 	vm_pindex_t first_pindex;
1114866e085SJohn Dyson 	vm_map_t map;
1124866e085SJohn Dyson 	vm_map_entry_t entry;
11325adb370SBrian Feldman 	int lookup_still_valid;
1144866e085SJohn Dyson 	struct vnode *vp;
1154866e085SJohn Dyson };
1164866e085SJohn Dyson 
1171c7c3c6aSMatthew Dillon static __inline void
1184866e085SJohn Dyson release_page(struct faultstate *fs)
1194866e085SJohn Dyson {
120e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
1214866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1224866e085SJohn Dyson 	fs->m = NULL;
1234866e085SJohn Dyson }
1244866e085SJohn Dyson 
1251c7c3c6aSMatthew Dillon static __inline void
1264866e085SJohn Dyson unlock_map(struct faultstate *fs)
1274866e085SJohn Dyson {
12825adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1294866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
13025adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1314866e085SJohn Dyson 	}
1324866e085SJohn Dyson }
1334866e085SJohn Dyson 
1344866e085SJohn Dyson static void
1354866e085SJohn Dyson _unlock_things(struct faultstate *fs, int dealloc)
1364866e085SJohn Dyson {
1370cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
1384866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
1394866e085SJohn Dyson 	if (fs->object != fs->first_object) {
1404866e085SJohn Dyson 		vm_page_free(fs->first_m);
1414866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
1424866e085SJohn Dyson 		fs->first_m = NULL;
1434866e085SJohn Dyson 	}
1444866e085SJohn Dyson 	if (dealloc) {
1454866e085SJohn Dyson 		vm_object_deallocate(fs->first_object);
1464866e085SJohn Dyson 	}
1474866e085SJohn Dyson 	unlock_map(fs);
1484866e085SJohn Dyson 	if (fs->vp != NULL) {
1490cddd8f0SMatthew Dillon 		vput(fs->vp);
1504866e085SJohn Dyson 		fs->vp = NULL;
1514866e085SJohn Dyson 	}
1524866e085SJohn Dyson }
1534866e085SJohn Dyson 
1544866e085SJohn Dyson #define unlock_things(fs) _unlock_things(fs, 0)
1554866e085SJohn Dyson #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
1564866e085SJohn Dyson 
157df8bae1dSRodney W. Grimes /*
15840360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
15940360b1bSMatthew Dillon  *	      current object *might* contain the page.
16040360b1bSMatthew Dillon  *
16140360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
16240360b1bSMatthew Dillon  */
16340360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
16440360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
16540360b1bSMatthew Dillon 
16640360b1bSMatthew Dillon /*
167df8bae1dSRodney W. Grimes  *	vm_fault:
168df8bae1dSRodney W. Grimes  *
169956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
170df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
171df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
172df8bae1dSRodney W. Grimes  *	associated physical map.
173df8bae1dSRodney W. Grimes  *
174df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
175df8bae1dSRodney W. Grimes  *	proper page address.
176df8bae1dSRodney W. Grimes  *
177df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
178df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
179df8bae1dSRodney W. Grimes  *
180df8bae1dSRodney W. Grimes  *
181df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
1820cddd8f0SMatthew Dillon  *	Caller may hold no locks.
183df8bae1dSRodney W. Grimes  */
18411caded3SAlfred Perlstein static int vm_fault1(vm_map_t, vm_offset_t, vm_prot_t, int);
18523955314SAlfred Perlstein 
186df8bae1dSRodney W. Grimes int
18723955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
18823955314SAlfred Perlstein 	 int fault_flags)
18923955314SAlfred Perlstein {
1900cddd8f0SMatthew Dillon 	int ret;
19123955314SAlfred Perlstein 
19223955314SAlfred Perlstein 	ret = vm_fault1(map, vaddr, fault_type, fault_flags);
193576f0c5fSJohn Baldwin 	mtx_unlock(&Giant);
19423955314SAlfred Perlstein 	return (ret);
19523955314SAlfred Perlstein }
19623955314SAlfred Perlstein 
19723955314SAlfred Perlstein static int
19823955314SAlfred Perlstein vm_fault1(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
19923955314SAlfred Perlstein 	  int fault_flags)
200df8bae1dSRodney W. Grimes {
201df8bae1dSRodney W. Grimes 	vm_prot_t prot;
202df8bae1dSRodney W. Grimes 	int result;
2036139043bSAlan Cox 	boolean_t growstack, wired;
2042d8acc0fSJohn Dyson 	int map_generation;
205df8bae1dSRodney W. Grimes 	vm_object_t next_object;
20626f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2074866e085SJohn Dyson 	int hardfault;
2082d8acc0fSJohn Dyson 	int faultcount;
2094866e085SJohn Dyson 	struct faultstate fs;
210df8bae1dSRodney W. Grimes 
2114866e085SJohn Dyson 	hardfault = 0;
2126139043bSAlan Cox 	growstack = TRUE;
21392de35b0SAlan Cox 	atomic_add_int(&cnt.v_vm_faults, 1);
214df8bae1dSRodney W. Grimes 
21592de35b0SAlan Cox 	mtx_lock(&Giant);
216df8bae1dSRodney W. Grimes RetryFault:;
217df8bae1dSRodney W. Grimes 
218df8bae1dSRodney W. Grimes 	/*
2190d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2200d94caffSDavid Greenman 	 * search.
221df8bae1dSRodney W. Grimes 	 */
22240360b1bSMatthew Dillon 	fs.map = map;
22392de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
22492de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
22592de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
22692de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
22792de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2286139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2296139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2306139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
2316139043bSAlan Cox 				if (result != KERN_SUCCESS)
2326139043bSAlan Cox 					return (KERN_FAILURE);
2336139043bSAlan Cox 				growstack = FALSE;
2346139043bSAlan Cox 				goto RetryFault;
2356139043bSAlan Cox 			}
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) {
25047221757SJohn Dyson 			return result;
25147221757SJohn Dyson 		}
25247221757SJohn Dyson 
2537aaaa4fdSJohn Dyson 		/*
2547aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2557aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2567aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
257ff8f4ebeSAlan Cox 		 *
258ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
259ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2607aaaa4fdSJohn Dyson 		 */
2614866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2624866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2637aaaa4fdSJohn Dyson 	}
26447221757SJohn Dyson 
2654866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2662d8acc0fSJohn Dyson 
2674866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
26847221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
26992c4c4ebSBruce Evans 		    (u_long)vaddr);
2707aaaa4fdSJohn Dyson 	}
2717aaaa4fdSJohn Dyson 
27295e5e988SJohn Dyson 	/*
27395e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
27495e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
27595e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
27695e5e988SJohn Dyson 	 * they will stay around as well.
277fe8e0238SMatthew Dillon 	 *
278fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
279fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
280fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
28195e5e988SJohn Dyson 	 */
2824866e085SJohn Dyson 	vm_object_reference(fs.first_object);
283fe8e0238SMatthew Dillon 	fs.vp = vnode_pager_lock(fs.first_object);
284d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
28595e5e988SJohn Dyson 
2861efb74fbSJohn Dyson 	if ((fault_type & VM_PROT_WRITE) &&
2874866e085SJohn Dyson 		(fs.first_object->type == OBJT_VNODE)) {
2884866e085SJohn Dyson 		vm_freeze_copyopts(fs.first_object,
2894866e085SJohn Dyson 			fs.first_pindex, fs.first_pindex + 1);
2901efb74fbSJohn Dyson 	}
291f6b04d2bSDavid Greenman 
29225adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes 	if (wired)
295df8bae1dSRodney W. Grimes 		fault_type = prot;
296df8bae1dSRodney W. Grimes 
2974866e085SJohn Dyson 	fs.first_m = NULL;
298df8bae1dSRodney W. Grimes 
299df8bae1dSRodney W. Grimes 	/*
300df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
301df8bae1dSRodney W. Grimes 	 */
3024866e085SJohn Dyson 	fs.object = fs.first_object;
3034866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
304df8bae1dSRodney W. Grimes 	while (TRUE) {
3051c7c3c6aSMatthew Dillon 		/*
3061c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3071c7c3c6aSMatthew Dillon 		 */
3084866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3094866e085SJohn Dyson 			unlock_and_deallocate(&fs);
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;
319df8bae1dSRodney W. Grimes 			/*
3201c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3211c7c3c6aSMatthew Dillon 			 * if the page is busy via either PG_BUSY or
3221c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3231c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3241c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
325956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3261c7c3c6aSMatthew Dillon 			 *
3271c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3281c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3291c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3301c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3311c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3321c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3331c7c3c6aSMatthew Dillon 			 * to pmap it.
334df8bae1dSRodney W. Grimes 			 */
3351c7c3c6aSMatthew Dillon 			if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
3364866e085SJohn Dyson 				unlock_things(&fs);
3371c7c3c6aSMatthew Dillon 				(void)vm_page_sleep_busy(fs.m, TRUE, "vmpfw");
338976e77fcSDavid Greenman 				cnt.v_intrans++;
3394866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
340df8bae1dSRodney W. Grimes 				goto RetryFault;
341df8bae1dSRodney W. Grimes 			}
3424866e085SJohn Dyson 			queue = fs.m->queue;
343a1287949SEivind Eklund 
344c699f45eSDavid Greenman 			s = splvm();
3456d03d577SMatthew Dillon 			vm_pageq_remove_nowakeup(fs.m);
346c699f45eSDavid Greenman 			splx(s);
347c82b0181SJohn Dyson 
34840360b1bSMatthew Dillon 			if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
3494866e085SJohn Dyson 				vm_page_activate(fs.m);
3504866e085SJohn Dyson 				unlock_and_deallocate(&fs);
351ef6020d1SMike Silbersack 				VM_WAITPFAULT;
35222ba64e8SJohn Dyson 				goto RetryFault;
35322ba64e8SJohn Dyson 			}
3547615edaaSMatthew Dillon 
3551c7c3c6aSMatthew Dillon 			/*
3561c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
3571c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
3581c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
3591c7c3c6aSMatthew Dillon 			 * found the page ).
3601c7c3c6aSMatthew Dillon 			 */
361e69763a3SDoug Rabson 			vm_page_busy(fs.m);
3624866e085SJohn Dyson 			if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
3634866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
3640d94caffSDavid Greenman 				goto readrest;
3650d94caffSDavid Greenman 			}
366ffc82b0aSJohn Dyson 
367df8bae1dSRodney W. Grimes 			break;
368df8bae1dSRodney W. Grimes 		}
3691c7c3c6aSMatthew Dillon 
3701c7c3c6aSMatthew Dillon 		/*
37140360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
37240360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
3731c7c3c6aSMatthew Dillon 		 */
37440360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
3754866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
3764866e085SJohn Dyson 				unlock_and_deallocate(&fs);
3775f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
3785f55e841SDavid Greenman 			}
37922ba64e8SJohn Dyson 
380df8bae1dSRodney W. Grimes 			/*
3810d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
382df8bae1dSRodney W. Grimes 			 */
38340360b1bSMatthew Dillon 			fs.m = NULL;
38440360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
3854866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
3864866e085SJohn Dyson 				    (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
38740360b1bSMatthew Dillon 			}
3884866e085SJohn Dyson 			if (fs.m == NULL) {
3894866e085SJohn Dyson 				unlock_and_deallocate(&fs);
390ef6020d1SMike Silbersack 				VM_WAITPFAULT;
391df8bae1dSRodney W. Grimes 				goto RetryFault;
392df8bae1dSRodney W. Grimes 			}
393df8bae1dSRodney W. Grimes 		}
39447221757SJohn Dyson 
3950d94caffSDavid Greenman readrest:
3961c7c3c6aSMatthew Dillon 		/*
39740360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
39840360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
39940360b1bSMatthew Dillon 		 * valid.
40040360b1bSMatthew Dillon 		 *
40140360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
40240360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
40340360b1bSMatthew Dillon 		 * at the same time.
4041c7c3c6aSMatthew Dillon 		 */
40540360b1bSMatthew Dillon 		if (TRYPAGER) {
406df8bae1dSRodney W. Grimes 			int rv;
40726f9a767SRodney W. Grimes 			int reqpage;
408867a482dSJohn Dyson 			int ahead, behind;
4097f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
410867a482dSJohn Dyson 
4117f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
412867a482dSJohn Dyson 				ahead = 0;
413867a482dSJohn Dyson 				behind = 0;
4142d8acc0fSJohn Dyson 			} else {
4154866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4162d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4172d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4182d8acc0fSJohn Dyson 
4194866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4202d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4212d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
422867a482dSJohn Dyson 			}
423867a482dSJohn Dyson 
4244866e085SJohn Dyson 			if ((fs.first_object->type != OBJT_DEVICE) &&
42540360b1bSMatthew Dillon 			    (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
42640360b1bSMatthew Dillon                                 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
42740360b1bSMatthew Dillon                                 fs.pindex >= fs.entry->lastr &&
42840360b1bSMatthew Dillon                                 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
42940360b1bSMatthew Dillon 			) {
430867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
43140360b1bSMatthew Dillon 
43240360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
433867a482dSJohn Dyson 					firstpindex = 0;
434867a482dSJohn Dyson 				else
43540360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
436867a482dSJohn Dyson 
4374221e284SAlan Cox 				/*
4384221e284SAlan Cox 				 * note: partially valid pages cannot be
4394221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
4404221e284SAlan Cox 				 * writes will barf on it badly.
4414221e284SAlan Cox 				 */
4424866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
4432100d645SPeter Wemm 					tmppindex >= firstpindex;
444867a482dSJohn Dyson 					--tmppindex) {
445867a482dSJohn Dyson 					vm_page_t mt;
446a1287949SEivind Eklund 
4474866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
448867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
449867a482dSJohn Dyson 						break;
450ff97964aSJohn Dyson 					if (mt->busy ||
4518b03c8edSMatthew Dillon 						(mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
452ff97964aSJohn Dyson 						mt->hold_count ||
453867a482dSJohn Dyson 						mt->wire_count)
454867a482dSJohn Dyson 						continue;
455867a482dSJohn Dyson 					if (mt->dirty == 0)
456867a482dSJohn Dyson 						vm_page_test_dirty(mt);
457867a482dSJohn Dyson 					if (mt->dirty) {
458867a482dSJohn Dyson 						vm_page_protect(mt, VM_PROT_NONE);
459867a482dSJohn Dyson 						vm_page_deactivate(mt);
460867a482dSJohn Dyson 					} else {
461867a482dSJohn Dyson 						vm_page_cache(mt);
462867a482dSJohn Dyson 					}
463867a482dSJohn Dyson 				}
464867a482dSJohn Dyson 
465867a482dSJohn Dyson 				ahead += behind;
466867a482dSJohn Dyson 				behind = 0;
467867a482dSJohn Dyson 			}
468df8bae1dSRodney W. Grimes 
469df8bae1dSRodney W. Grimes 			/*
4700d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
4710d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
4720d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
4730d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
4740d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
4750d94caffSDavid Greenman 			 * array "marray" returned contains an array of
4760d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
4770d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
4780d94caffSDavid Greenman 			 * return value is the index into the marray for the
4790d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
4801c7c3c6aSMatthew Dillon 			 *
4811c7c3c6aSMatthew Dillon 			 * fs.m plus the additional pages are PG_BUSY'd.
48226f9a767SRodney W. Grimes 			 */
48305f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
4844866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
485df8bae1dSRodney W. Grimes 
486df8bae1dSRodney W. Grimes 			/*
48740360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
48840360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
48940360b1bSMatthew Dillon 			 */
49040360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
49140360b1bSMatthew Dillon 
49240360b1bSMatthew Dillon 			/*
4930d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
4941c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
4951c7c3c6aSMatthew Dillon 			 * fs.object and the pages are PG_BUSY'd.
496df8bae1dSRodney W. Grimes 			 */
4974866e085SJohn Dyson 			unlock_map(&fs);
498df8bae1dSRodney W. Grimes 
49926f9a767SRodney W. Grimes 			rv = faultcount ?
5004866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
50124a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
50222ba64e8SJohn Dyson 
50326f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
504df8bae1dSRodney W. Grimes 				/*
505f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
506f230c45cSJohn Dyson 				 * with it.
507f230c45cSJohn Dyson 				 */
508f230c45cSJohn Dyson 
509f230c45cSJohn Dyson 				/*
5100d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5110d94caffSDavid Greenman 				 * is responsible for disposition of old page
5120d94caffSDavid Greenman 				 * if moved.
513df8bae1dSRodney W. Grimes 				 */
5144866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5154866e085SJohn Dyson 				if (!fs.m) {
5164866e085SJohn Dyson 					unlock_and_deallocate(&fs);
517f6b04d2bSDavid Greenman 					goto RetryFault;
518f6b04d2bSDavid Greenman 				}
519f6b04d2bSDavid Greenman 
52026f9a767SRodney W. Grimes 				hardfault++;
5211c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
522df8bae1dSRodney W. Grimes 			}
523df8bae1dSRodney W. Grimes 			/*
5240d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5250d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5260d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
527df8bae1dSRodney W. Grimes 			 *
52824a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5290d94caffSDavid Greenman 			 * in this page.
530df8bae1dSRodney W. Grimes 			 *
5310d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
53224a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
5330d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
5340d94caffSDavid Greenman 			 * the same time that we are.
535df8bae1dSRodney W. Grimes 			 */
536a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
537f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
538f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
53926f9a767SRodney W. Grimes 			/*
540a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
54126f9a767SRodney W. Grimes 			 */
542a83c285cSDavid Greenman 			/*
5430d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
5440d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
5450d94caffSDavid Greenman 			 * fault w/ I/O error.
546a83c285cSDavid Greenman 			 */
5474866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
54847221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
5494866e085SJohn Dyson 				vm_page_free(fs.m);
5504866e085SJohn Dyson 				fs.m = NULL;
5514866e085SJohn Dyson 				unlock_and_deallocate(&fs);
552a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
55326f9a767SRodney W. Grimes 			}
5544866e085SJohn Dyson 			if (fs.object != fs.first_object) {
5554866e085SJohn Dyson 				vm_page_free(fs.m);
5564866e085SJohn Dyson 				fs.m = NULL;
55726f9a767SRodney W. Grimes 				/*
55826f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
55926f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
56026f9a767SRodney W. Grimes 				 */
561df8bae1dSRodney W. Grimes 			}
562df8bae1dSRodney W. Grimes 		}
56340360b1bSMatthew Dillon 
564df8bae1dSRodney W. Grimes 		/*
5651c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
5661c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
567df8bae1dSRodney W. Grimes 		 */
5684866e085SJohn Dyson 		if (fs.object == fs.first_object)
5694866e085SJohn Dyson 			fs.first_m = fs.m;
570df8bae1dSRodney W. Grimes 
571df8bae1dSRodney W. Grimes 		/*
5720d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
5730d94caffSDavid Greenman 		 * unlocking the current one.
574df8bae1dSRodney W. Grimes 		 */
5754866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
5764866e085SJohn Dyson 		next_object = fs.object->backing_object;
577df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
578df8bae1dSRodney W. Grimes 			/*
5790d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
5800d94caffSDavid Greenman 			 * object with zeros.
581df8bae1dSRodney W. Grimes 			 */
5824866e085SJohn Dyson 			if (fs.object != fs.first_object) {
5834866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
584df8bae1dSRodney W. Grimes 
5854866e085SJohn Dyson 				fs.object = fs.first_object;
5864866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
5874866e085SJohn Dyson 				fs.m = fs.first_m;
588df8bae1dSRodney W. Grimes 			}
5894866e085SJohn Dyson 			fs.first_m = NULL;
590df8bae1dSRodney W. Grimes 
5914221e284SAlan Cox 			/*
5924221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
5934221e284SAlan Cox 			 */
5944866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
5954866e085SJohn Dyson 				vm_page_zero_fill(fs.m);
5964221e284SAlan Cox 			} else {
59714286e5eSAlan Cox 				cnt.v_ozfod++;
5984221e284SAlan Cox 			}
599df8bae1dSRodney W. Grimes 			cnt.v_zfod++;
6004221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6011c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6020d94caffSDavid Greenman 		} else {
6034866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6044866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
605c0503609SDavid Greenman 			}
6061c7c3c6aSMatthew Dillon 			KASSERT(fs.object != next_object, ("object loop %p", next_object));
6074866e085SJohn Dyson 			fs.object = next_object;
608d474eaaaSDoug Rabson 			vm_object_pip_add(fs.object, 1);
609df8bae1dSRodney W. Grimes 		}
610df8bae1dSRodney W. Grimes 	}
6111c7c3c6aSMatthew Dillon 
6125526d2d9SEivind Eklund 	KASSERT((fs.m->flags & PG_BUSY) != 0,
6135526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
614df8bae1dSRodney W. Grimes 
615df8bae1dSRodney W. Grimes 	/*
6160d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
617df8bae1dSRodney W. Grimes 	 * is held.]
618df8bae1dSRodney W. Grimes 	 */
619df8bae1dSRodney W. Grimes 
620df8bae1dSRodney W. Grimes 	/*
6210d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6220d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6230d94caffSDavid Greenman 	 * top-level object.
624df8bae1dSRodney W. Grimes 	 */
6254866e085SJohn Dyson 	if (fs.object != fs.first_object) {
626df8bae1dSRodney W. Grimes 		/*
6270d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
628df8bae1dSRodney W. Grimes 		 */
629df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
630df8bae1dSRodney W. Grimes 			/*
6311c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
6321c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
6331c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
6341c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
6351c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
6361c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
6371c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
6381c7c3c6aSMatthew Dillon 			 * to swap when needed.
639df8bae1dSRodney W. Grimes 			 */
6404866e085SJohn Dyson 			if (map_generation == fs.map->timestamp &&
641de5f6a77SJohn Dyson 				/*
642de5f6a77SJohn Dyson 				 * Only one shadow object
643de5f6a77SJohn Dyson 				 */
6444866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
645de5f6a77SJohn Dyson 				/*
646de5f6a77SJohn Dyson 				 * No COW refs, except us
647de5f6a77SJohn Dyson 				 */
6484866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
649de5f6a77SJohn Dyson 				/*
6505929bcfaSPhilippe Charnier 				 * No one else can look this object up
651de5f6a77SJohn Dyson 				 */
6524866e085SJohn Dyson 				(fs.object->handle == NULL) &&
653de5f6a77SJohn Dyson 				/*
654de5f6a77SJohn Dyson 				 * No other ways to look the object up
655de5f6a77SJohn Dyson 				 */
6564866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
6574866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
658de5f6a77SJohn Dyson 				/*
659de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
660de5f6a77SJohn Dyson 				 */
6614866e085SJohn Dyson 				(fs.object == fs.first_object->backing_object) &&
662df8bae1dSRodney W. Grimes 
663df8bae1dSRodney W. Grimes 				/*
6642d8acc0fSJohn Dyson 				 * grab the lock if we need to
6652d8acc0fSJohn Dyson 				 */
66625adb370SBrian Feldman 				(fs.lookup_still_valid ||
66725adb370SBrian Feldman 				 lockmgr(&fs.map->lock, LK_EXCLUSIVE|LK_NOWAIT, (void *)0, curthread) == 0)
6689fdfe602SMatthew Dillon 			    ) {
66925adb370SBrian Feldman 
67025adb370SBrian Feldman 				fs.lookup_still_valid = 1;
6712d8acc0fSJohn Dyson 				/*
672de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
673df8bae1dSRodney W. Grimes 				 */
6744866e085SJohn Dyson 				vm_page_protect(fs.first_m, VM_PROT_NONE);
6754866e085SJohn Dyson 				vm_page_free(fs.first_m);
6764866e085SJohn Dyson 				fs.first_m = NULL;
6774866e085SJohn Dyson 
678de5f6a77SJohn Dyson 				/*
6791c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
6801c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
6811c7c3c6aSMatthew Dillon 				 * automatically made dirty.
682de5f6a77SJohn Dyson 				 */
6834866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
6844866e085SJohn Dyson 				fs.first_m = fs.m;
685e69763a3SDoug Rabson 				vm_page_busy(fs.first_m);
6864866e085SJohn Dyson 				fs.m = NULL;
6874866e085SJohn Dyson 				cnt.v_cow_optim++;
688de5f6a77SJohn Dyson 			} else {
689de5f6a77SJohn Dyson 				/*
690de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
691de5f6a77SJohn Dyson 				 */
6924866e085SJohn Dyson 				vm_page_copy(fs.m, fs.first_m);
693de5f6a77SJohn Dyson 			}
694df8bae1dSRodney W. Grimes 
6954866e085SJohn Dyson 			if (fs.m) {
696df8bae1dSRodney W. Grimes 				/*
697df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
698df8bae1dSRodney W. Grimes 				 */
6994866e085SJohn Dyson 				release_page(&fs);
700de5f6a77SJohn Dyson 			}
701df8bae1dSRodney W. Grimes 
7021c7c3c6aSMatthew Dillon 			/*
7031c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7041c7c3c6aSMatthew Dillon 			 * conditional
7051c7c3c6aSMatthew Dillon 			 */
7064866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
7071c7c3c6aSMatthew Dillon 
708df8bae1dSRodney W. Grimes 			/*
709df8bae1dSRodney W. Grimes 			 * Only use the new page below...
710df8bae1dSRodney W. Grimes 			 */
711df8bae1dSRodney W. Grimes 			cnt.v_cow_faults++;
7124866e085SJohn Dyson 			fs.m = fs.first_m;
7134866e085SJohn Dyson 			fs.object = fs.first_object;
7144866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
715df8bae1dSRodney W. Grimes 
7160d94caffSDavid Greenman 		} else {
717df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
718df8bae1dSRodney W. Grimes 		}
719df8bae1dSRodney W. Grimes 	}
720df8bae1dSRodney W. Grimes 
721df8bae1dSRodney W. Grimes 	/*
7220d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7230d94caffSDavid Greenman 	 * lookup.
724df8bae1dSRodney W. Grimes 	 */
72525adb370SBrian Feldman 	if (!fs.lookup_still_valid &&
7264866e085SJohn Dyson 		(fs.map->timestamp != map_generation)) {
727df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
728a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
729df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
730df8bae1dSRodney W. Grimes 
731df8bae1dSRodney W. Grimes 		/*
7320d94caffSDavid Greenman 		 * Since map entries may be pageable, make sure we can take a
7330d94caffSDavid Greenman 		 * page fault on them.
734df8bae1dSRodney W. Grimes 		 */
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes 		/*
737b33fb764SMatthew Dillon 		 * Unlock vnode before the lookup to avoid deadlock.   E.G.
738b33fb764SMatthew Dillon 		 * avoid a deadlock between the inode and exec_map that can
739b33fb764SMatthew Dillon 		 * occur due to locks being obtained in different orders.
740b33fb764SMatthew Dillon 		 */
741b33fb764SMatthew Dillon 		if (fs.vp != NULL) {
742b33fb764SMatthew Dillon 			vput(fs.vp);
743b33fb764SMatthew Dillon 			fs.vp = NULL;
744b33fb764SMatthew Dillon 		}
745b823bbd6SMatthew Dillon 
746b823bbd6SMatthew Dillon 		if (fs.map->infork) {
747b823bbd6SMatthew Dillon 			release_page(&fs);
748b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
749b823bbd6SMatthew Dillon 			goto RetryFault;
750b823bbd6SMatthew Dillon 		}
751b33fb764SMatthew Dillon 
752b33fb764SMatthew Dillon 		/*
75324a1cce3SDavid Greenman 		 * To avoid trying to write_lock the map while another process
7540d94caffSDavid Greenman 		 * has it read_locked (in vm_map_pageable), we do not try for
7550d94caffSDavid Greenman 		 * write permission.  If the page is still writable, we will
7560d94caffSDavid Greenman 		 * get write permission.  If it is not, or has been marked
7570d94caffSDavid Greenman 		 * needs_copy, we enter the mapping without write permission,
7580d94caffSDavid Greenman 		 * and will merely take another fault.
759df8bae1dSRodney W. Grimes 		 */
7604866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
7614866e085SJohn Dyson 		    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
7624866e085SJohn Dyson 		map_generation = fs.map->timestamp;
763df8bae1dSRodney W. Grimes 
764df8bae1dSRodney W. Grimes 		/*
7650d94caffSDavid Greenman 		 * If we don't need the page any longer, put it on the active
7660d94caffSDavid Greenman 		 * list (the easiest thing to do here).  If no one needs it,
7670d94caffSDavid Greenman 		 * pageout will grab it eventually.
768df8bae1dSRodney W. Grimes 		 */
769df8bae1dSRodney W. Grimes 		if (result != KERN_SUCCESS) {
7704866e085SJohn Dyson 			release_page(&fs);
7714866e085SJohn Dyson 			unlock_and_deallocate(&fs);
772df8bae1dSRodney W. Grimes 			return (result);
773df8bae1dSRodney W. Grimes 		}
77425adb370SBrian Feldman 		fs.lookup_still_valid = TRUE;
775df8bae1dSRodney W. Grimes 
7764866e085SJohn Dyson 		if ((retry_object != fs.first_object) ||
7774866e085SJohn Dyson 		    (retry_pindex != fs.first_pindex)) {
7784866e085SJohn Dyson 			release_page(&fs);
7794866e085SJohn Dyson 			unlock_and_deallocate(&fs);
780df8bae1dSRodney W. Grimes 			goto RetryFault;
781df8bae1dSRodney W. Grimes 		}
782df8bae1dSRodney W. Grimes 		/*
7830d94caffSDavid Greenman 		 * Check whether the protection has changed or the object has
7840d94caffSDavid Greenman 		 * been copied while we left the map unlocked. Changing from
7850d94caffSDavid Greenman 		 * read to write permission is OK - we leave the page
7860d94caffSDavid Greenman 		 * write-protected, and catch the write fault. Changing from
7870d94caffSDavid Greenman 		 * write to read permission means that we can't mark the page
7880d94caffSDavid Greenman 		 * write-enabled after all.
789df8bae1dSRodney W. Grimes 		 */
790df8bae1dSRodney W. Grimes 		prot &= retry_prot;
791df8bae1dSRodney W. Grimes 	}
792df8bae1dSRodney W. Grimes 
793df8bae1dSRodney W. Grimes 	/*
7940d94caffSDavid Greenman 	 * Put this page into the physical map. We had to do the unlock above
7950d94caffSDavid Greenman 	 * because pmap_enter may cause other faults.   We don't put the page
7960d94caffSDavid Greenman 	 * back on the active queue until later so that the page-out daemon
7970d94caffSDavid Greenman 	 * won't find us (yet).
798df8bae1dSRodney W. Grimes 	 */
799df8bae1dSRodney W. Grimes 
8002ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
801e69763a3SDoug Rabson 		vm_page_flag_set(fs.m, PG_WRITEABLE);
802245df27cSMatthew Dillon 		vm_object_set_writeable_dirty(fs.m->object);
8034f79d873SMatthew Dillon 
8042ddba215SDavid Greenman 		/*
8052ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8064f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8074f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8084f79d873SMatthew Dillon 		 *
8094f79d873SMatthew Dillon 		 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
8104f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8114f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8124f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8134f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8144f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8151c7c3c6aSMatthew Dillon 		 *
8161c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8171c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8182ddba215SDavid Greenman 		 */
8194f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8204f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
8214f79d873SMatthew Dillon 				vm_page_flag_set(fs.m, PG_NOSYNC);
8224f79d873SMatthew Dillon 		} else {
8234f79d873SMatthew Dillon 			vm_page_flag_clear(fs.m, PG_NOSYNC);
8244f79d873SMatthew Dillon 		}
8255bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8265bf53acbSMatthew Dillon 			int s;
8277dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
828ef0646f9SMatthew Dillon 			s = splvm();
8291c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
830ef0646f9SMatthew Dillon 			splx(s);
8312ddba215SDavid Greenman 		}
8322ddba215SDavid Greenman 	}
833f6b04d2bSDavid Greenman 
834bc6d84a6SMatthew Dillon 	/*
835bc6d84a6SMatthew Dillon 	 * Page had better still be busy
836bc6d84a6SMatthew Dillon 	 */
837ca06c247SAlan Cox 	KASSERT(fs.m->flags & PG_BUSY,
838ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
8394866e085SJohn Dyson 	unlock_things(&fs);
8404221e284SAlan Cox 
8414221e284SAlan Cox 	/*
8424221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8434221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8444221e284SAlan Cox 	 */
8454221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8464221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8474221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8484221e284SAlan Cox 	}
8490385347cSPeter Wemm 	pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
8502d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
8514866e085SJohn Dyson 		pmap_prefault(fs.map->pmap, vaddr, fs.entry);
8522d8acc0fSJohn Dyson 	}
8534221e284SAlan Cox 	vm_page_flag_clear(fs.m, PG_ZERO);
854e69763a3SDoug Rabson 	vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
855ff97964aSJohn Dyson 
856df8bae1dSRodney W. Grimes 	/*
8570d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
8580d94caffSDavid Greenman 	 * can find it.
859df8bae1dSRodney W. Grimes 	 */
860a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
861df8bae1dSRodney W. Grimes 		if (wired)
8624866e085SJohn Dyson 			vm_page_wire(fs.m);
863df8bae1dSRodney W. Grimes 		else
86473007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
8650d94caffSDavid Greenman 	} else {
8664866e085SJohn Dyson 		vm_page_activate(fs.m);
86726f9a767SRodney W. Grimes 	}
86826f9a767SRodney W. Grimes 
8699ed346baSBosko Milekic 	mtx_lock_spin(&sched_lock);
870b9393356SJohn Baldwin 	if (curproc && (curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
87126f9a767SRodney W. Grimes 		if (hardfault) {
87226f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
87326f9a767SRodney W. Grimes 		} else {
87426f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
87526f9a767SRodney W. Grimes 		}
87626f9a767SRodney W. Grimes 	}
8779ed346baSBosko Milekic 	mtx_unlock_spin(&sched_lock);
878df8bae1dSRodney W. Grimes 
879df8bae1dSRodney W. Grimes 	/*
880df8bae1dSRodney W. Grimes 	 * Unlock everything, and return
881df8bae1dSRodney W. Grimes 	 */
882e69763a3SDoug Rabson 	vm_page_wakeup(fs.m);
8834866e085SJohn Dyson 	vm_object_deallocate(fs.first_object);
884df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
885df8bae1dSRodney W. Grimes 
886df8bae1dSRodney W. Grimes }
887df8bae1dSRodney W. Grimes 
888df8bae1dSRodney W. Grimes /*
889df8bae1dSRodney W. Grimes  *	vm_fault_wire:
890df8bae1dSRodney W. Grimes  *
891df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
892df8bae1dSRodney W. Grimes  */
893df8bae1dSRodney W. Grimes int
894df8bae1dSRodney W. Grimes vm_fault_wire(map, start, end)
895df8bae1dSRodney W. Grimes 	vm_map_t map;
896df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
897df8bae1dSRodney W. Grimes {
89826f9a767SRodney W. Grimes 
89954d92145SMatthew Dillon 	vm_offset_t va;
90054d92145SMatthew Dillon 	pmap_t pmap;
901df8bae1dSRodney W. Grimes 	int rv;
902df8bae1dSRodney W. Grimes 
903df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
904df8bae1dSRodney W. Grimes 
905df8bae1dSRodney W. Grimes 	/*
9060d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
9070d94caffSDavid Greenman 	 * not fault, so that page tables and such can be locked down as well.
908df8bae1dSRodney W. Grimes 	 */
909df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, FALSE);
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes 	/*
9120d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
9130d94caffSDavid Greenman 	 * map.
914df8bae1dSRodney W. Grimes 	 */
915df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
9167aaaa4fdSJohn Dyson 		rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
9177aaaa4fdSJohn Dyson 			VM_FAULT_CHANGE_WIRING);
9187aaaa4fdSJohn Dyson 		if (rv) {
9197aaaa4fdSJohn Dyson 			if (va != start)
9207aaaa4fdSJohn Dyson 				vm_fault_unwire(map, start, va);
9217aaaa4fdSJohn Dyson 			return (rv);
9227aaaa4fdSJohn Dyson 		}
9237aaaa4fdSJohn Dyson 	}
9247aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
9257aaaa4fdSJohn Dyson }
9267aaaa4fdSJohn Dyson 
9277aaaa4fdSJohn Dyson /*
9287aaaa4fdSJohn Dyson  *	vm_fault_user_wire:
9297aaaa4fdSJohn Dyson  *
9307aaaa4fdSJohn Dyson  *	Wire down a range of virtual addresses in a map.  This
9317aaaa4fdSJohn Dyson  *	is for user mode though, so we only ask for read access
9327aaaa4fdSJohn Dyson  *	on currently read only sections.
9337aaaa4fdSJohn Dyson  */
9347aaaa4fdSJohn Dyson int
9357aaaa4fdSJohn Dyson vm_fault_user_wire(map, start, end)
9367aaaa4fdSJohn Dyson 	vm_map_t map;
9377aaaa4fdSJohn Dyson 	vm_offset_t start, end;
9387aaaa4fdSJohn Dyson {
9397aaaa4fdSJohn Dyson 
94054d92145SMatthew Dillon 	vm_offset_t va;
94154d92145SMatthew Dillon 	pmap_t pmap;
9427aaaa4fdSJohn Dyson 	int rv;
9437aaaa4fdSJohn Dyson 
9440cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
9450cddd8f0SMatthew Dillon 
9467aaaa4fdSJohn Dyson 	pmap = vm_map_pmap(map);
9477aaaa4fdSJohn Dyson 
9487aaaa4fdSJohn Dyson 	/*
9497aaaa4fdSJohn Dyson 	 * Inform the physical mapping system that the range of addresses may
9507aaaa4fdSJohn Dyson 	 * not fault, so that page tables and such can be locked down as well.
9517aaaa4fdSJohn Dyson 	 */
9527aaaa4fdSJohn Dyson 	pmap_pageable(pmap, start, end, FALSE);
9537aaaa4fdSJohn Dyson 
9547aaaa4fdSJohn Dyson 	/*
9557aaaa4fdSJohn Dyson 	 * We simulate a fault to get the page and enter it in the physical
9567aaaa4fdSJohn Dyson 	 * map.
9577aaaa4fdSJohn Dyson 	 */
9587aaaa4fdSJohn Dyson 	for (va = start; va < end; va += PAGE_SIZE) {
9597aaaa4fdSJohn Dyson 		rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_USER_WIRE);
960df8bae1dSRodney W. Grimes 		if (rv) {
961df8bae1dSRodney W. Grimes 			if (va != start)
962df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, start, va);
963df8bae1dSRodney W. Grimes 			return (rv);
964df8bae1dSRodney W. Grimes 		}
965df8bae1dSRodney W. Grimes 	}
966df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
967df8bae1dSRodney W. Grimes }
968df8bae1dSRodney W. Grimes 
969df8bae1dSRodney W. Grimes 
970df8bae1dSRodney W. Grimes /*
971df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
972df8bae1dSRodney W. Grimes  *
973df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
974df8bae1dSRodney W. Grimes  */
97526f9a767SRodney W. Grimes void
97626f9a767SRodney W. Grimes vm_fault_unwire(map, start, end)
977df8bae1dSRodney W. Grimes 	vm_map_t map;
978df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
979df8bae1dSRodney W. Grimes {
980df8bae1dSRodney W. Grimes 
98154d92145SMatthew Dillon 	vm_offset_t va, pa;
98254d92145SMatthew Dillon 	pmap_t pmap;
983df8bae1dSRodney W. Grimes 
984df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
985df8bae1dSRodney W. Grimes 
986df8bae1dSRodney W. Grimes 	/*
9870d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
9880d94caffSDavid Greenman 	 * mappings from the physical map system.
989df8bae1dSRodney W. Grimes 	 */
990df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
991df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
992b18bfc3dSJohn Dyson 		if (pa != (vm_offset_t) 0) {
993df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
99473007561SDavid Greenman 			vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
995df8bae1dSRodney W. Grimes 		}
996b18bfc3dSJohn Dyson 	}
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes 	/*
9990d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
10000d94caffSDavid Greenman 	 * fault, so that page tables and such may be unwired themselves.
1001df8bae1dSRodney W. Grimes 	 */
1002df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, TRUE);
1003df8bae1dSRodney W. Grimes 
1004df8bae1dSRodney W. Grimes }
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes /*
1007df8bae1dSRodney W. Grimes  *	Routine:
1008df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1009df8bae1dSRodney W. Grimes  *	Function:
1010df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1011df8bae1dSRodney W. Grimes  *
1012df8bae1dSRodney W. Grimes  *	In/out conditions:
1013df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1014df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1015df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1016df8bae1dSRodney W. Grimes  */
101726f9a767SRodney W. Grimes void
101826f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1019df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1020df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1021df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1022df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1023df8bae1dSRodney W. Grimes {
1024df8bae1dSRodney W. Grimes 	vm_object_t dst_object;
1025df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1026a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1027a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1028df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1029df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1030df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1031df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1032df8bae1dSRodney W. Grimes 
1033df8bae1dSRodney W. Grimes #ifdef	lint
1034df8bae1dSRodney W. Grimes 	src_map++;
10350d94caffSDavid Greenman #endif	/* lint */
1036df8bae1dSRodney W. Grimes 
1037df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1038df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1039df8bae1dSRodney W. Grimes 
1040df8bae1dSRodney W. Grimes 	/*
10410d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
10420d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1043df8bae1dSRodney W. Grimes 	 */
104424a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
1045a316d390SJohn Dyson 	    (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1046df8bae1dSRodney W. Grimes 
1047df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1048df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1049df8bae1dSRodney W. Grimes 
1050df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1051df8bae1dSRodney W. Grimes 
1052df8bae1dSRodney W. Grimes 	/*
10530d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
10540d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
10550d94caffSDavid Greenman 	 * object.
1056df8bae1dSRodney W. Grimes 	 */
1057df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1058df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1059df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 		/*
1062df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1063df8bae1dSRodney W. Grimes 		 */
1064df8bae1dSRodney W. Grimes 		do {
1065a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1066a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1067df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
1068df8bae1dSRodney W. Grimes 				VM_WAIT;
1069df8bae1dSRodney W. Grimes 			}
1070df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1071df8bae1dSRodney W. Grimes 
1072df8bae1dSRodney W. Grimes 		/*
1073df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
10740d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
10750d94caffSDavid Greenman 		 * memory.)
1076df8bae1dSRodney W. Grimes 		 */
1077a316d390SJohn Dyson 		src_m = vm_page_lookup(src_object,
1078a316d390SJohn Dyson 			OFF_TO_IDX(dst_offset + src_offset));
1079df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1080df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1081df8bae1dSRodney W. Grimes 
1082df8bae1dSRodney W. Grimes 		vm_page_copy(src_m, dst_m);
1083df8bae1dSRodney W. Grimes 
1084df8bae1dSRodney W. Grimes 		/*
1085df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
1086df8bae1dSRodney W. Grimes 		 */
1087e69763a3SDoug Rabson 		vm_page_flag_clear(dst_m, PG_ZERO);
10880385347cSPeter Wemm 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1089e69763a3SDoug Rabson 		vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1090df8bae1dSRodney W. Grimes 
1091df8bae1dSRodney W. Grimes 		/*
1092df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1093df8bae1dSRodney W. Grimes 		 */
1094df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
1095e69763a3SDoug Rabson 		vm_page_wakeup(dst_m);
1096df8bae1dSRodney W. Grimes 	}
1097df8bae1dSRodney W. Grimes }
109826f9a767SRodney W. Grimes 
109926f9a767SRodney W. Grimes 
110026f9a767SRodney W. Grimes /*
110126f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
110222ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
110322ba64e8SJohn Dyson  * pages for the pages to be paged in.
110426f9a767SRodney W. Grimes  *
110526f9a767SRodney W. Grimes  * Inputs:
110622ba64e8SJohn Dyson  *	m, rbehind, rahead
110726f9a767SRodney W. Grimes  *
110826f9a767SRodney W. Grimes  * Outputs:
110926f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
111026f9a767SRodney W. Grimes  *
111126f9a767SRodney W. Grimes  * Return value:
111226f9a767SRodney W. Grimes  *  number of pages in marray
111323955314SAlfred Perlstein  *
111423955314SAlfred Perlstein  * This routine can't block.
111526f9a767SRodney W. Grimes  */
1116303b270bSEivind Eklund static int
111722ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
111826f9a767SRodney W. Grimes 	vm_page_t m;
111926f9a767SRodney W. Grimes 	int rbehind;
112022ba64e8SJohn Dyson 	int rahead;
112126f9a767SRodney W. Grimes 	vm_page_t *marray;
112226f9a767SRodney W. Grimes 	int *reqpage;
112326f9a767SRodney W. Grimes {
11242d8acc0fSJohn Dyson 	int i,j;
112526f9a767SRodney W. Grimes 	vm_object_t object;
1126a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
112726f9a767SRodney W. Grimes 	vm_page_t rtm;
1128170db9c6SJohn Dyson 	int cbehind, cahead;
112926f9a767SRodney W. Grimes 
11300cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
113123955314SAlfred Perlstein 
113226f9a767SRodney W. Grimes 	object = m->object;
1133a316d390SJohn Dyson 	pindex = m->pindex;
113426f9a767SRodney W. Grimes 
113526f9a767SRodney W. Grimes 	/*
1136f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1137f35329acSJohn Dyson 	 */
1138f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1139f35329acSJohn Dyson 		*reqpage = 0;
1140f35329acSJohn Dyson 		marray[0] = m;
1141f35329acSJohn Dyson 		return 1;
1142f35329acSJohn Dyson 	}
1143f35329acSJohn Dyson 
1144f35329acSJohn Dyson 	/*
114526f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
114626f9a767SRodney W. Grimes 	 */
11471c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
114826f9a767SRodney W. Grimes 		return 0;
11492d8acc0fSJohn Dyson 	}
115026f9a767SRodney W. Grimes 
115122ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
115222ba64e8SJohn Dyson 		*reqpage = 0;
115322ba64e8SJohn Dyson 		marray[0] = m;
115422ba64e8SJohn Dyson 		return 1;
1155170db9c6SJohn Dyson 	}
115622ba64e8SJohn Dyson 
115722ba64e8SJohn Dyson 	if (rahead > cahead) {
115822ba64e8SJohn Dyson 		rahead = cahead;
115922ba64e8SJohn Dyson 	}
116022ba64e8SJohn Dyson 
1161170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1162170db9c6SJohn Dyson 		rbehind = cbehind;
1163170db9c6SJohn Dyson 	}
1164170db9c6SJohn Dyson 
116526f9a767SRodney W. Grimes 	/*
116626f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
116726f9a767SRodney W. Grimes 	 */
1168ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
116922ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1170f919ebdeSDavid Greenman 		pagedaemon_wakeup();
117126f9a767SRodney W. Grimes 		marray[0] = m;
11722d8acc0fSJohn Dyson 		*reqpage = 0;
117326f9a767SRodney W. Grimes 		return 1;
117426f9a767SRodney W. Grimes 	}
117522ba64e8SJohn Dyson 
117626f9a767SRodney W. Grimes 	/*
11772d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
117826f9a767SRodney W. Grimes 	 */
11792d8acc0fSJohn Dyson 	if (pindex > 0) {
11802d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1181a316d390SJohn Dyson 			rbehind = pindex;
11822d8acc0fSJohn Dyson 			startpindex = 0;
11832d8acc0fSJohn Dyson 		} else {
1184a316d390SJohn Dyson 			startpindex = pindex - rbehind;
11852d8acc0fSJohn Dyson 		}
11862d8acc0fSJohn Dyson 
11872d8acc0fSJohn Dyson 		for (tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1188a316d390SJohn Dyson 			if (vm_page_lookup(object, tpindex)) {
1189a316d390SJohn Dyson 				startpindex = tpindex + 1;
119026f9a767SRodney W. Grimes 				break;
119126f9a767SRodney W. Grimes 			}
1192a316d390SJohn Dyson 			if (tpindex == 0)
119326f9a767SRodney W. Grimes 				break;
1194317205caSDavid Greenman 		}
119526f9a767SRodney W. Grimes 
11962d8acc0fSJohn Dyson 		for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
119726f9a767SRodney W. Grimes 
11982d8acc0fSJohn Dyson 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1199ccbb2f72SJohn Dyson 			if (rtm == NULL) {
1200ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
12014866e085SJohn Dyson 					vm_page_free(marray[j]);
120226f9a767SRodney W. Grimes 				}
120326f9a767SRodney W. Grimes 				marray[0] = m;
12042d8acc0fSJohn Dyson 				*reqpage = 0;
120526f9a767SRodney W. Grimes 				return 1;
120626f9a767SRodney W. Grimes 			}
1207170db9c6SJohn Dyson 
12082d8acc0fSJohn Dyson 			marray[i] = rtm;
120926f9a767SRodney W. Grimes 		}
12102d8acc0fSJohn Dyson 	} else {
12112d8acc0fSJohn Dyson 		startpindex = 0;
12122d8acc0fSJohn Dyson 		i = 0;
12132d8acc0fSJohn Dyson 	}
12142d8acc0fSJohn Dyson 
12152d8acc0fSJohn Dyson 	marray[i] = m;
12162d8acc0fSJohn Dyson 	/* page offset of the required page */
12172d8acc0fSJohn Dyson 	*reqpage = i;
12182d8acc0fSJohn Dyson 
12192d8acc0fSJohn Dyson 	tpindex = pindex + 1;
12202d8acc0fSJohn Dyson 	i++;
12212d8acc0fSJohn Dyson 
12222d8acc0fSJohn Dyson 	/*
12232d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
12242d8acc0fSJohn Dyson 	 */
12252d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
12262d8acc0fSJohn Dyson 	if (endpindex > object->size)
12272d8acc0fSJohn Dyson 		endpindex = object->size;
12282d8acc0fSJohn Dyson 
12292d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
12302d8acc0fSJohn Dyson 
12312d8acc0fSJohn Dyson 		if (vm_page_lookup(object, tpindex)) {
12322d8acc0fSJohn Dyson 			break;
12332d8acc0fSJohn Dyson 		}
12342d8acc0fSJohn Dyson 
12352d8acc0fSJohn Dyson 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
12362d8acc0fSJohn Dyson 		if (rtm == NULL) {
12372d8acc0fSJohn Dyson 			break;
12382d8acc0fSJohn Dyson 		}
12392d8acc0fSJohn Dyson 
12402d8acc0fSJohn Dyson 		marray[i] = rtm;
12412d8acc0fSJohn Dyson 	}
12422d8acc0fSJohn Dyson 
12432d8acc0fSJohn Dyson 	/* return number of bytes of pages */
12442d8acc0fSJohn Dyson 	return i;
124526f9a767SRodney W. Grimes }
1246