xref: /freebsd/sys/vm/vm_fault.c (revision ec96dca7884d77ad76e9dbb58b58b7165aa5c6d2)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  *	Page fault handling module.
72df8bae1dSRodney W. Grimes  */
73874651b1SDavid E. O'Brien 
74874651b1SDavid E. O'Brien #include <sys/cdefs.h>
75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
76874651b1SDavid E. O'Brien 
77f8a47341SAlan Cox #include "opt_vm.h"
78f8a47341SAlan Cox 
79df8bae1dSRodney W. Grimes #include <sys/param.h>
80df8bae1dSRodney W. Grimes #include <sys/systm.h>
814edf4a58SJohn Baldwin #include <sys/kernel.h>
82fb919e4dSMark Murray #include <sys/lock.h>
83fb919e4dSMark Murray #include <sys/mutex.h>
8426f9a767SRodney W. Grimes #include <sys/proc.h>
8526f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8623955314SAlfred Perlstein #include <sys/sysctl.h>
874edf4a58SJohn Baldwin #include <sys/vmmeter.h>
884edf4a58SJohn Baldwin #include <sys/vnode.h>
89df8bae1dSRodney W. Grimes 
90df8bae1dSRodney W. Grimes #include <vm/vm.h>
91efeaf95aSDavid Greenman #include <vm/vm_param.h>
92efeaf95aSDavid Greenman #include <vm/pmap.h>
93efeaf95aSDavid Greenman #include <vm/vm_map.h>
94efeaf95aSDavid Greenman #include <vm/vm_object.h>
95df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
96df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
97a83c285cSDavid Greenman #include <vm/vm_kern.h>
9824a1cce3SDavid Greenman #include <vm/vm_pager.h>
9924a1cce3SDavid Greenman #include <vm/vnode_pager.h>
100efeaf95aSDavid Greenman #include <vm/vm_extern.h>
101df8bae1dSRodney W. Grimes 
102ae51ff11SJeff Roberson #include <sys/mount.h>	/* XXX Temporary for VFS_LOCK_GIANT() */
103ae51ff11SJeff Roberson 
104566526a9SAlan Cox #define PFBAK 4
105566526a9SAlan Cox #define PFFOR 4
106566526a9SAlan Cox #define PAGEORDER_SIZE (PFBAK+PFFOR)
107566526a9SAlan Cox 
108566526a9SAlan Cox static int prefault_pageorder[] = {
109566526a9SAlan Cox 	-1 * PAGE_SIZE, 1 * PAGE_SIZE,
110566526a9SAlan Cox 	-2 * PAGE_SIZE, 2 * PAGE_SIZE,
111566526a9SAlan Cox 	-3 * PAGE_SIZE, 3 * PAGE_SIZE,
112566526a9SAlan Cox 	-4 * PAGE_SIZE, 4 * PAGE_SIZE
113566526a9SAlan Cox };
114566526a9SAlan Cox 
11511caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
116566526a9SAlan Cox static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
11726f9a767SRodney W. Grimes 
11847221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8
11947221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7
12026f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
12126f9a767SRodney W. Grimes 
1224866e085SJohn Dyson struct faultstate {
1234866e085SJohn Dyson 	vm_page_t m;
1244866e085SJohn Dyson 	vm_object_t object;
1254866e085SJohn Dyson 	vm_pindex_t pindex;
1264866e085SJohn Dyson 	vm_page_t first_m;
1274866e085SJohn Dyson 	vm_object_t	first_object;
1284866e085SJohn Dyson 	vm_pindex_t first_pindex;
1294866e085SJohn Dyson 	vm_map_t map;
1304866e085SJohn Dyson 	vm_map_entry_t entry;
13125adb370SBrian Feldman 	int lookup_still_valid;
1324866e085SJohn Dyson 	struct vnode *vp;
1334866e085SJohn Dyson };
1344866e085SJohn Dyson 
13562a59e8fSWarner Losh static inline void
1364866e085SJohn Dyson release_page(struct faultstate *fs)
1374866e085SJohn Dyson {
138e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
13966bdd5d6SAlan Cox 	vm_page_lock_queues();
1404866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1412ad98273SAlan Cox 	vm_page_unlock_queues();
1424866e085SJohn Dyson 	fs->m = NULL;
1434866e085SJohn Dyson }
1444866e085SJohn Dyson 
14562a59e8fSWarner Losh static inline void
1464866e085SJohn Dyson unlock_map(struct faultstate *fs)
1474866e085SJohn Dyson {
14825adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1494866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15025adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1514866e085SJohn Dyson 	}
1524866e085SJohn Dyson }
1534866e085SJohn Dyson 
1544866e085SJohn Dyson static void
155a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1564866e085SJohn Dyson {
157f29ba63eSAlan Cox 
1584866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
159b009d5a0SAlan Cox 	VM_OBJECT_UNLOCK(fs->object);
1604866e085SJohn Dyson 	if (fs->object != fs->first_object) {
161b009d5a0SAlan Cox 		VM_OBJECT_LOCK(fs->first_object);
1622ad98273SAlan Cox 		vm_page_lock_queues();
1634866e085SJohn Dyson 		vm_page_free(fs->first_m);
1642ad98273SAlan Cox 		vm_page_unlock_queues();
1654866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
166b009d5a0SAlan Cox 		VM_OBJECT_UNLOCK(fs->first_object);
1674866e085SJohn Dyson 		fs->first_m = NULL;
1684866e085SJohn Dyson 	}
1694866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1704866e085SJohn Dyson 	unlock_map(fs);
1714866e085SJohn Dyson 	if (fs->vp != NULL) {
172ae51ff11SJeff Roberson 		int vfslocked;
173ae51ff11SJeff Roberson 
174ae51ff11SJeff Roberson 		vfslocked = VFS_LOCK_GIANT(fs->vp->v_mount);
1750cddd8f0SMatthew Dillon 		vput(fs->vp);
1764866e085SJohn Dyson 		fs->vp = NULL;
177ae51ff11SJeff Roberson 		VFS_UNLOCK_GIANT(vfslocked);
1784866e085SJohn Dyson 	}
1794866e085SJohn Dyson }
1804866e085SJohn Dyson 
181df8bae1dSRodney W. Grimes /*
18240360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
18340360b1bSMatthew Dillon  *	      current object *might* contain the page.
18440360b1bSMatthew Dillon  *
18540360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18640360b1bSMatthew Dillon  */
18740360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
18840360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
18940360b1bSMatthew Dillon 
19040360b1bSMatthew Dillon /*
191df8bae1dSRodney W. Grimes  *	vm_fault:
192df8bae1dSRodney W. Grimes  *
193956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
194df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
195df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
196df8bae1dSRodney W. Grimes  *	associated physical map.
197df8bae1dSRodney W. Grimes  *
198df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
199df8bae1dSRodney W. Grimes  *	proper page address.
200df8bae1dSRodney W. Grimes  *
201df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
202df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
203df8bae1dSRodney W. Grimes  *
204df8bae1dSRodney W. Grimes  *
205df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2060cddd8f0SMatthew Dillon  *	Caller may hold no locks.
207df8bae1dSRodney W. Grimes  */
208df8bae1dSRodney W. Grimes int
20923955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
21023955314SAlfred Perlstein 	 int fault_flags)
21123955314SAlfred Perlstein {
212df8bae1dSRodney W. Grimes 	vm_prot_t prot;
213ebf75125SAlan Cox 	int is_first_object_locked, result;
2146139043bSAlan Cox 	boolean_t growstack, wired;
2152d8acc0fSJohn Dyson 	int map_generation;
216df8bae1dSRodney W. Grimes 	vm_object_t next_object;
21726f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2184866e085SJohn Dyson 	int hardfault;
2192d8acc0fSJohn Dyson 	int faultcount;
2204866e085SJohn Dyson 	struct faultstate fs;
221df8bae1dSRodney W. Grimes 
2224866e085SJohn Dyson 	hardfault = 0;
2236139043bSAlan Cox 	growstack = TRUE;
22467596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
225df8bae1dSRodney W. Grimes 
226df8bae1dSRodney W. Grimes RetryFault:;
227df8bae1dSRodney W. Grimes 
228df8bae1dSRodney W. Grimes 	/*
2290d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2300d94caffSDavid Greenman 	 * search.
231df8bae1dSRodney W. Grimes 	 */
23240360b1bSMatthew Dillon 	fs.map = map;
23392de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
23492de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
23592de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
23692de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
23792de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2386139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2396139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2406139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
241a976eb5eSAlan Cox 				if (result != KERN_SUCCESS)
2426139043bSAlan Cox 					return (KERN_FAILURE);
2436139043bSAlan Cox 				growstack = FALSE;
2446139043bSAlan Cox 				goto RetryFault;
2456139043bSAlan Cox 			}
24692de35b0SAlan Cox 			return (result);
24709e0c6ccSJohn Dyson 		}
24809e0c6ccSJohn Dyson 
2497aaaa4fdSJohn Dyson 		/*
2507aaaa4fdSJohn Dyson    		 * If we are user-wiring a r/w segment, and it is COW, then
2517aaaa4fdSJohn Dyson    		 * we need to do the COW operation.  Note that we don't COW
252595236dfSJohn Dyson    		 * currently RO sections now, because it is NOT desirable
2537aaaa4fdSJohn Dyson    		 * to COW .text.  We simply keep .text from ever being COW'ed
2547aaaa4fdSJohn Dyson    		 * and take the heat that one cannot debug wired .text sections.
2557aaaa4fdSJohn Dyson    		 */
2564866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr,
25747221757SJohn Dyson 			VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
2584866e085SJohn Dyson 			&fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
259a976eb5eSAlan Cox 		if (result != KERN_SUCCESS)
2607788e219SAlan Cox 			return (result);
26147221757SJohn Dyson 
2627aaaa4fdSJohn Dyson 		/*
2637aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2647aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2657aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
266ff8f4ebeSAlan Cox 		 *
267ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
268ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2697aaaa4fdSJohn Dyson 		 */
2704866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2714866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2727aaaa4fdSJohn Dyson 	}
27347221757SJohn Dyson 
2744866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2752d8acc0fSJohn Dyson 
2764866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
27747221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
27892c4c4ebSBruce Evans 		    (u_long)vaddr);
2797aaaa4fdSJohn Dyson 	}
2807aaaa4fdSJohn Dyson 
28195e5e988SJohn Dyson 	/*
28295e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
28395e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
28495e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
28595e5e988SJohn Dyson 	 * they will stay around as well.
286fe8e0238SMatthew Dillon 	 *
287fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
288fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
289fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
290532eadefSAlan Cox 	 *
291532eadefSAlan Cox 	 * XXX vnode_pager_lock() can block without releasing the map lock.
29295e5e988SJohn Dyson 	 */
293ed4fe4f4SJeff Roberson 	if (fs.first_object->flags & OBJ_NEEDGIANT)
294a976eb5eSAlan Cox 		mtx_lock(&Giant);
295d22bc710SAlan Cox 	VM_OBJECT_LOCK(fs.first_object);
296a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
297417a26a1SAlan Cox 	fs.vp = vnode_pager_lock(fs.first_object);
2985e4bdb57SAlan Cox 	KASSERT(fs.vp == NULL || !fs.map->system_map,
2995e4bdb57SAlan Cox 	    ("vm_fault: vnode-backed object mapped by system map"));
300ed4fe4f4SJeff Roberson 	KASSERT((fs.first_object->flags & OBJ_NEEDGIANT) == 0 ||
301ed4fe4f4SJeff Roberson 	    !fs.map->system_map,
302ed4fe4f4SJeff Roberson 	    ("vm_fault: Object requiring giant mapped by system map"));
3032cf13952SAlan Cox 	if (fs.first_object->flags & OBJ_NEEDGIANT)
304c1fbc251SAlan Cox 		mtx_unlock(&Giant);
305d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30695e5e988SJohn Dyson 
30725adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
308df8bae1dSRodney W. Grimes 
309df8bae1dSRodney W. Grimes 	if (wired)
310df8bae1dSRodney W. Grimes 		fault_type = prot;
311df8bae1dSRodney W. Grimes 
3124866e085SJohn Dyson 	fs.first_m = NULL;
313df8bae1dSRodney W. Grimes 
314df8bae1dSRodney W. Grimes 	/*
315df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
316df8bae1dSRodney W. Grimes 	 */
3174866e085SJohn Dyson 	fs.object = fs.first_object;
3184866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
319df8bae1dSRodney W. Grimes 	while (TRUE) {
3201c7c3c6aSMatthew Dillon 		/*
3211c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3221c7c3c6aSMatthew Dillon 		 */
3234866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3244866e085SJohn Dyson 			unlock_and_deallocate(&fs);
32547221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32647221757SJohn Dyson 		}
32747221757SJohn Dyson 
3281c7c3c6aSMatthew Dillon 		/*
3291c7c3c6aSMatthew Dillon 		 * See if page is resident
3301c7c3c6aSMatthew Dillon 		 */
3314866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3324866e085SJohn Dyson 		if (fs.m != NULL) {
33398cb733cSKenneth D. Merry 			/*
3349b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3359b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3369b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3379b80d344SKenneth D. Merry 			 * used when the page in question is part of
3389b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3399b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3409b80d344SKenneth D. Merry 			 * which is not what we want.
34198cb733cSKenneth D. Merry 			 */
342f4ecdf05SAlan Cox 			vm_page_lock_queues();
34398cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3449b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3459b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
34698cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
347f4ecdf05SAlan Cox 				vm_page_unlock_queues();
348d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
34998cb733cSKenneth D. Merry 				goto RetryFault;
35098cb733cSKenneth D. Merry 			}
35198cb733cSKenneth D. Merry 
352df8bae1dSRodney W. Grimes 			/*
3531c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3549af80719SAlan Cox 			 * if the page is busy via either VPO_BUSY or
3551c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3561c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3571c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
358956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3591c7c3c6aSMatthew Dillon 			 *
3601c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3611c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3621c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3631c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3641c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3651c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3661c7c3c6aSMatthew Dillon 			 * to pmap it.
367df8bae1dSRodney W. Grimes 			 */
3689af80719SAlan Cox 			if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
3694abd55b2SAlan Cox 				vm_page_unlock_queues();
370a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
371a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
372a51b0840SAlan Cox 					VM_OBJECT_LOCK(fs.first_object);
3739a96b638SAlan Cox 					vm_page_lock_queues();
374a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3759a96b638SAlan Cox 					vm_page_unlock_queues();
376a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
377a51b0840SAlan Cox 					VM_OBJECT_UNLOCK(fs.first_object);
378a51b0840SAlan Cox 					fs.first_m = NULL;
379a51b0840SAlan Cox 				}
380a51b0840SAlan Cox 				unlock_map(&fs);
381a51b0840SAlan Cox 				if (fs.vp != NULL) {
382ed4fe4f4SJeff Roberson 					int vfslck;
383ed4fe4f4SJeff Roberson 
384ed4fe4f4SJeff Roberson 					vfslck = VFS_LOCK_GIANT(fs.vp->v_mount);
385a51b0840SAlan Cox 					vput(fs.vp);
386a51b0840SAlan Cox 					fs.vp = NULL;
387ed4fe4f4SJeff Roberson 					VFS_UNLOCK_GIANT(vfslck);
388a51b0840SAlan Cox 				}
389a51b0840SAlan Cox 				VM_OBJECT_LOCK(fs.object);
390a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
391a51b0840SAlan Cox 				    fs.pindex)) {
392e7e56b28SAlan Cox 					vm_page_sleep_if_busy(fs.m, TRUE,
393e7e56b28SAlan Cox 					    "vmpfw");
394a51b0840SAlan Cox 				}
395a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
396a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
39767596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
3984866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
399df8bae1dSRodney W. Grimes 				goto RetryFault;
400df8bae1dSRodney W. Grimes 			}
4017bfda801SAlan Cox 			vm_pageq_remove(fs.m);
402768131d2SAlan Cox 			vm_page_unlock_queues();
4037615edaaSMatthew Dillon 
4041c7c3c6aSMatthew Dillon 			/*
4051c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4061c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4071c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4081c7c3c6aSMatthew Dillon 			 * found the page ).
4091c7c3c6aSMatthew Dillon 			 */
410e69763a3SDoug Rabson 			vm_page_busy(fs.m);
4114866e085SJohn Dyson 			if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
4124866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
4130d94caffSDavid Greenman 				goto readrest;
4140d94caffSDavid Greenman 			}
415ffc82b0aSJohn Dyson 
416df8bae1dSRodney W. Grimes 			break;
417df8bae1dSRodney W. Grimes 		}
4181c7c3c6aSMatthew Dillon 
4191c7c3c6aSMatthew Dillon 		/*
42040360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
42140360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4221c7c3c6aSMatthew Dillon 		 */
42340360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4244866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4254866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4265f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4275f55e841SDavid Greenman 			}
42822ba64e8SJohn Dyson 
429df8bae1dSRodney W. Grimes 			/*
4300d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
431df8bae1dSRodney W. Grimes 			 */
43240360b1bSMatthew Dillon 			fs.m = NULL;
43340360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
434f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
435f8a47341SAlan Cox 				if ((fs.object->flags & OBJ_COLORED) == 0) {
436f8a47341SAlan Cox 					fs.object->flags |= OBJ_COLORED;
437f8a47341SAlan Cox 					fs.object->pg_color = atop(vaddr) -
438f8a47341SAlan Cox 					    fs.pindex;
439f8a47341SAlan Cox 				}
440f8a47341SAlan Cox #endif
4414866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4424866e085SJohn Dyson 				    (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
44340360b1bSMatthew Dillon 			}
4444866e085SJohn Dyson 			if (fs.m == NULL) {
4454866e085SJohn Dyson 				unlock_and_deallocate(&fs);
446ef6020d1SMike Silbersack 				VM_WAITPFAULT;
447df8bae1dSRodney W. Grimes 				goto RetryFault;
4484ab8ab92SKonstantin Belousov 			} else if ((fs.m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL)
4494ab8ab92SKonstantin Belousov 				break;
450df8bae1dSRodney W. Grimes 		}
45147221757SJohn Dyson 
4520d94caffSDavid Greenman readrest:
4531c7c3c6aSMatthew Dillon 		/*
45440360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
45540360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
45640360b1bSMatthew Dillon 		 * valid.
45740360b1bSMatthew Dillon 		 *
45840360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
45940360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
46040360b1bSMatthew Dillon 		 * at the same time.
4611c7c3c6aSMatthew Dillon 		 */
46240360b1bSMatthew Dillon 		if (TRYPAGER) {
463df8bae1dSRodney W. Grimes 			int rv;
4649dae7290SMatt Jacob 			int reqpage = 0;
465867a482dSJohn Dyson 			int ahead, behind;
4667f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
467867a482dSJohn Dyson 
4687f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
469867a482dSJohn Dyson 				ahead = 0;
470867a482dSJohn Dyson 				behind = 0;
4712d8acc0fSJohn Dyson 			} else {
4724866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4732d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4742d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4752d8acc0fSJohn Dyson 
4764866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4772d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4782d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
479867a482dSJohn Dyson 			}
4808d8b9c6eSAlan Cox 			is_first_object_locked = FALSE;
4818d8b9c6eSAlan Cox 			if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
48240360b1bSMatthew Dillon 			     (behavior != MAP_ENTRY_BEHAV_RANDOM &&
48340360b1bSMatthew Dillon 			      fs.pindex >= fs.entry->lastr &&
4848d8b9c6eSAlan Cox 			      fs.pindex < fs.entry->lastr + VM_FAULT_READ)) &&
4858d8b9c6eSAlan Cox 			    (fs.first_object == fs.object ||
4868d8b9c6eSAlan Cox 			     (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) &&
487ec96dca7SAlan Cox 			    fs.first_object->type != OBJT_DEVICE &&
488ec96dca7SAlan Cox 			    fs.first_object->type != OBJT_PHYS) {
489867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
49040360b1bSMatthew Dillon 
49140360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
492867a482dSJohn Dyson 					firstpindex = 0;
493867a482dSJohn Dyson 				else
49440360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
495867a482dSJohn Dyson 
49615a5d210SAlan Cox 				vm_page_lock_queues();
4974221e284SAlan Cox 				/*
4984221e284SAlan Cox 				 * note: partially valid pages cannot be
4994221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
5004221e284SAlan Cox 				 * writes will barf on it badly.
5014221e284SAlan Cox 				 */
5024866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
5032100d645SPeter Wemm 					tmppindex >= firstpindex;
504867a482dSJohn Dyson 					--tmppindex) {
505867a482dSJohn Dyson 					vm_page_t mt;
506a1287949SEivind Eklund 
5074866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
508867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
509867a482dSJohn Dyson 						break;
510ff97964aSJohn Dyson 					if (mt->busy ||
5119af80719SAlan Cox 					    (mt->oflags & VPO_BUSY) ||
512ff97964aSJohn Dyson 						mt->hold_count ||
513867a482dSJohn Dyson 						mt->wire_count)
514867a482dSJohn Dyson 						continue;
5154fec79beSAlan Cox 					pmap_remove_all(mt);
516c6d9ef2eSAlan Cox 					if (mt->dirty) {
517867a482dSJohn Dyson 						vm_page_deactivate(mt);
518867a482dSJohn Dyson 					} else {
519867a482dSJohn Dyson 						vm_page_cache(mt);
520867a482dSJohn Dyson 					}
521867a482dSJohn Dyson 				}
52215a5d210SAlan Cox 				vm_page_unlock_queues();
523867a482dSJohn Dyson 				ahead += behind;
524867a482dSJohn Dyson 				behind = 0;
525867a482dSJohn Dyson 			}
5268d8b9c6eSAlan Cox 			if (is_first_object_locked)
5278d8b9c6eSAlan Cox 				VM_OBJECT_UNLOCK(fs.first_object);
528df8bae1dSRodney W. Grimes 			/*
5290d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5300d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5310d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5320d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5330d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5340d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5350d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5360d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5370d94caffSDavid Greenman 			 * return value is the index into the marray for the
5380d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5391c7c3c6aSMatthew Dillon 			 *
5409af80719SAlan Cox 			 * fs.m plus the additional pages are VPO_BUSY'd.
541532eadefSAlan Cox 			 *
542532eadefSAlan Cox 			 * XXX vm_fault_additional_pages() can block
543532eadefSAlan Cox 			 * without releasing the map lock.
54426f9a767SRodney W. Grimes 			 */
54505f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5464866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
547df8bae1dSRodney W. Grimes 
548df8bae1dSRodney W. Grimes 			/*
54940360b1bSMatthew Dillon 			 * update lastr imperfectly (we do not know how much
55040360b1bSMatthew Dillon 			 * getpages will actually read), but good enough.
551532eadefSAlan Cox 			 *
552532eadefSAlan Cox 			 * XXX The following assignment modifies the map
553532eadefSAlan Cox 			 * without holding a write lock on it.
55440360b1bSMatthew Dillon 			 */
55540360b1bSMatthew Dillon 			fs.entry->lastr = fs.pindex + faultcount - behind;
55640360b1bSMatthew Dillon 
55740360b1bSMatthew Dillon 			/*
5580d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
5591c7c3c6aSMatthew Dillon 			 * releasing the lock on the map.  We hold a ref on
5609af80719SAlan Cox 			 * fs.object and the pages are VPO_BUSY'd.
561df8bae1dSRodney W. Grimes 			 */
5624866e085SJohn Dyson 			unlock_map(&fs);
563df8bae1dSRodney W. Grimes 
56426f9a767SRodney W. Grimes 			rv = faultcount ?
5654866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
56624a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
56722ba64e8SJohn Dyson 
56826f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
569df8bae1dSRodney W. Grimes 				/*
570f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
571f230c45cSJohn Dyson 				 * with it.
572f230c45cSJohn Dyson 				 */
573f230c45cSJohn Dyson 
574f230c45cSJohn Dyson 				/*
5750d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5760d94caffSDavid Greenman 				 * is responsible for disposition of old page
5770d94caffSDavid Greenman 				 * if moved.
578df8bae1dSRodney W. Grimes 				 */
5794866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5804866e085SJohn Dyson 				if (!fs.m) {
5814866e085SJohn Dyson 					unlock_and_deallocate(&fs);
582f6b04d2bSDavid Greenman 					goto RetryFault;
583f6b04d2bSDavid Greenman 				}
584f6b04d2bSDavid Greenman 
58526f9a767SRodney W. Grimes 				hardfault++;
5861c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
587df8bae1dSRodney W. Grimes 			}
588df8bae1dSRodney W. Grimes 			/*
5890d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5900d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
5910d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
592df8bae1dSRodney W. Grimes 			 *
59324a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
5940d94caffSDavid Greenman 			 * in this page.
595df8bae1dSRodney W. Grimes 			 *
5960d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
59724a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
5980d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
5990d94caffSDavid Greenman 			 * the same time that we are.
600df8bae1dSRodney W. Grimes 			 */
601a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
602f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
603f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
60426f9a767SRodney W. Grimes 			/*
605a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
60626f9a767SRodney W. Grimes 			 */
607a83c285cSDavid Greenman 			/*
6080d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6090d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6100d94caffSDavid Greenman 			 * fault w/ I/O error.
611a83c285cSDavid Greenman 			 */
6124866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
61347221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6142ad98273SAlan Cox 				vm_page_lock_queues();
6154866e085SJohn Dyson 				vm_page_free(fs.m);
6162ad98273SAlan Cox 				vm_page_unlock_queues();
6174866e085SJohn Dyson 				fs.m = NULL;
6184866e085SJohn Dyson 				unlock_and_deallocate(&fs);
619a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
62026f9a767SRodney W. Grimes 			}
6214866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6222ad98273SAlan Cox 				vm_page_lock_queues();
6234866e085SJohn Dyson 				vm_page_free(fs.m);
6242ad98273SAlan Cox 				vm_page_unlock_queues();
6254866e085SJohn Dyson 				fs.m = NULL;
62626f9a767SRodney W. Grimes 				/*
62726f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
62826f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
62926f9a767SRodney W. Grimes 				 */
630df8bae1dSRodney W. Grimes 			}
631df8bae1dSRodney W. Grimes 		}
63240360b1bSMatthew Dillon 
633df8bae1dSRodney W. Grimes 		/*
6341c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6351c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
636df8bae1dSRodney W. Grimes 		 */
6374866e085SJohn Dyson 		if (fs.object == fs.first_object)
6384866e085SJohn Dyson 			fs.first_m = fs.m;
639df8bae1dSRodney W. Grimes 
640df8bae1dSRodney W. Grimes 		/*
6410d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6420d94caffSDavid Greenman 		 * unlocking the current one.
643df8bae1dSRodney W. Grimes 		 */
6444866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6454866e085SJohn Dyson 		next_object = fs.object->backing_object;
646df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
647df8bae1dSRodney W. Grimes 			/*
6480d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6490d94caffSDavid Greenman 			 * object with zeros.
650df8bae1dSRodney W. Grimes 			 */
6514866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6524866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
653b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
654df8bae1dSRodney W. Grimes 
6554866e085SJohn Dyson 				fs.object = fs.first_object;
6564866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6574866e085SJohn Dyson 				fs.m = fs.first_m;
658f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
659df8bae1dSRodney W. Grimes 			}
6604866e085SJohn Dyson 			fs.first_m = NULL;
661df8bae1dSRodney W. Grimes 
6624221e284SAlan Cox 			/*
6634221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6644221e284SAlan Cox 			 */
6654866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
666fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6674221e284SAlan Cox 			} else {
66867596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
6694221e284SAlan Cox 			}
67067596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
6714221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6721c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6730d94caffSDavid Greenman 		} else {
674c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
675c8567c3aSAlan Cox 			    ("object loop %p", next_object));
676c8567c3aSAlan Cox 			VM_OBJECT_LOCK(next_object);
677c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
678c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6794866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
680b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
6814866e085SJohn Dyson 			fs.object = next_object;
682df8bae1dSRodney W. Grimes 		}
683df8bae1dSRodney W. Grimes 	}
6841c7c3c6aSMatthew Dillon 
6859af80719SAlan Cox 	KASSERT((fs.m->oflags & VPO_BUSY) != 0,
6865526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
687df8bae1dSRodney W. Grimes 
688df8bae1dSRodney W. Grimes 	/*
6890d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
690df8bae1dSRodney W. Grimes 	 * is held.]
691df8bae1dSRodney W. Grimes 	 */
692df8bae1dSRodney W. Grimes 
693df8bae1dSRodney W. Grimes 	/*
6940d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
6950d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
6960d94caffSDavid Greenman 	 * top-level object.
697df8bae1dSRodney W. Grimes 	 */
6984866e085SJohn Dyson 	if (fs.object != fs.first_object) {
699df8bae1dSRodney W. Grimes 		/*
7000d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
701df8bae1dSRodney W. Grimes 		 */
702df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
703df8bae1dSRodney W. Grimes 			/*
7041c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7051c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7061c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7071c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7081c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7091c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7101c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7111c7c3c6aSMatthew Dillon 			 * to swap when needed.
712df8bae1dSRodney W. Grimes 			 */
713ebf75125SAlan Cox 			is_first_object_locked = FALSE;
714e50346b5SAlan Cox 			if (
715de5f6a77SJohn Dyson 				/*
716de5f6a77SJohn Dyson 				 * Only one shadow object
717de5f6a77SJohn Dyson 				 */
7184866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
719de5f6a77SJohn Dyson 				/*
720de5f6a77SJohn Dyson 				 * No COW refs, except us
721de5f6a77SJohn Dyson 				 */
7224866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
723de5f6a77SJohn Dyson 				/*
7245929bcfaSPhilippe Charnier 				 * No one else can look this object up
725de5f6a77SJohn Dyson 				 */
7264866e085SJohn Dyson 				(fs.object->handle == NULL) &&
727de5f6a77SJohn Dyson 				/*
728de5f6a77SJohn Dyson 				 * No other ways to look the object up
729de5f6a77SJohn Dyson 				 */
7304866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7314866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
732ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
733de5f6a77SJohn Dyson 				/*
734de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
735de5f6a77SJohn Dyson 				 */
736e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
737d98ddc46SAlan Cox 				vm_page_lock_queues();
7382d8acc0fSJohn Dyson 				/*
739de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
740df8bae1dSRodney W. Grimes 				 */
7414866e085SJohn Dyson 				vm_page_free(fs.first_m);
742de5f6a77SJohn Dyson 				/*
7431c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7441c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7451c7c3c6aSMatthew Dillon 				 * automatically made dirty.
746de5f6a77SJohn Dyson 				 */
7474866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
7489a96b638SAlan Cox 				vm_page_unlock_queues();
749768131d2SAlan Cox 				vm_page_busy(fs.m);
750d98ddc46SAlan Cox 				fs.first_m = fs.m;
7514866e085SJohn Dyson 				fs.m = NULL;
75267596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
753de5f6a77SJohn Dyson 			} else {
754de5f6a77SJohn Dyson 				/*
755de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
756de5f6a77SJohn Dyson 				 */
757669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
758669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
759de5f6a77SJohn Dyson 			}
7604866e085SJohn Dyson 			if (fs.m) {
761df8bae1dSRodney W. Grimes 				/*
762df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
763df8bae1dSRodney W. Grimes 				 */
7644866e085SJohn Dyson 				release_page(&fs);
765de5f6a77SJohn Dyson 			}
7661c7c3c6aSMatthew Dillon 			/*
7671c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7681c7c3c6aSMatthew Dillon 			 * conditional
7691c7c3c6aSMatthew Dillon 			 */
7704866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
771b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
772df8bae1dSRodney W. Grimes 			/*
773df8bae1dSRodney W. Grimes 			 * Only use the new page below...
774df8bae1dSRodney W. Grimes 			 */
7754866e085SJohn Dyson 			fs.object = fs.first_object;
7764866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
777d98ddc46SAlan Cox 			fs.m = fs.first_m;
778f29ba63eSAlan Cox 			if (!is_first_object_locked)
779f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
78067596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
7810d94caffSDavid Greenman 		} else {
782df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
783df8bae1dSRodney W. Grimes 		}
784df8bae1dSRodney W. Grimes 	}
785df8bae1dSRodney W. Grimes 
786df8bae1dSRodney W. Grimes 	/*
7870d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
7880d94caffSDavid Greenman 	 * lookup.
789df8bae1dSRodney W. Grimes 	 */
79019dc5607STor Egge 	if (!fs.lookup_still_valid) {
791df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
792a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
793df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
794df8bae1dSRodney W. Grimes 
79519dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
796b823bbd6SMatthew Dillon 			release_page(&fs);
797b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
798b823bbd6SMatthew Dillon 			goto RetryFault;
799b823bbd6SMatthew Dillon 		}
80019dc5607STor Egge 		fs.lookup_still_valid = TRUE;
80119dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
80219dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8034866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
804df8bae1dSRodney W. Grimes 
805df8bae1dSRodney W. Grimes 			/*
80644ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8070d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8080d94caffSDavid Greenman 			 * pageout will grab it eventually.
809df8bae1dSRodney W. Grimes 			 */
810df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8114866e085SJohn Dyson 				release_page(&fs);
8124866e085SJohn Dyson 				unlock_and_deallocate(&fs);
81319dc5607STor Egge 
81419dc5607STor Egge 				/*
81519dc5607STor Egge 				 * If retry of map lookup would have blocked then
81619dc5607STor Egge 				 * retry fault from start.
81719dc5607STor Egge 				 */
81819dc5607STor Egge 				if (result == KERN_FAILURE)
81919dc5607STor Egge 					goto RetryFault;
820df8bae1dSRodney W. Grimes 				return (result);
821df8bae1dSRodney W. Grimes 			}
8224866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8234866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8244866e085SJohn Dyson 				release_page(&fs);
8254866e085SJohn Dyson 				unlock_and_deallocate(&fs);
826df8bae1dSRodney W. Grimes 				goto RetryFault;
827df8bae1dSRodney W. Grimes 			}
82819dc5607STor Egge 
829df8bae1dSRodney W. Grimes 			/*
8300d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8310d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8320d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8330d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8340d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8350d94caffSDavid Greenman 			 * write-enabled after all.
836df8bae1dSRodney W. Grimes 			 */
837df8bae1dSRodney W. Grimes 			prot &= retry_prot;
838df8bae1dSRodney W. Grimes 		}
83919dc5607STor Egge 	}
8402ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
841b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8424f79d873SMatthew Dillon 
8432ddba215SDavid Greenman 		/*
8442ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8454f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8464f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8474f79d873SMatthew Dillon 		 *
848b146f9e5SAlan Cox 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
8494f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8504f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8514f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8524f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8534f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8541c7c3c6aSMatthew Dillon 		 *
8551c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
8561c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
8572ddba215SDavid Greenman 		 */
8584f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8594f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
860b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
8614f79d873SMatthew Dillon 		} else {
862b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
8634f79d873SMatthew Dillon 		}
8645bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
8657dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
8661c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
8672ddba215SDavid Greenman 		}
8682ddba215SDavid Greenman 	}
869f6b04d2bSDavid Greenman 
870bc6d84a6SMatthew Dillon 	/*
871bc6d84a6SMatthew Dillon 	 * Page had better still be busy
872bc6d84a6SMatthew Dillon 	 */
8739af80719SAlan Cox 	KASSERT(fs.m->oflags & VPO_BUSY,
874ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
8754221e284SAlan Cox 	/*
8764221e284SAlan Cox 	 * Sanity check: page must be completely valid or it is not fit to
8774221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
8784221e284SAlan Cox 	 */
8794221e284SAlan Cox 	if (fs.m->valid != VM_PAGE_BITS_ALL) {
8804221e284SAlan Cox 		vm_page_zero_invalid(fs.m, TRUE);
8814221e284SAlan Cox 		printf("Warning: page %p partially invalid on fault\n", fs.m);
8824221e284SAlan Cox 	}
883eebf3286SAlan Cox 	VM_OBJECT_UNLOCK(fs.object);
884cbfbaad8SAlan Cox 
88586735996SAlan Cox 	/*
88686735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
88786735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
88886735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
88986735996SAlan Cox 	 * won't find it (yet).
89086735996SAlan Cox 	 */
891eb2a0517SAlan Cox 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
8922d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
893566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
8942d8acc0fSJohn Dyson 	}
895eebf3286SAlan Cox 	VM_OBJECT_LOCK(fs.object);
8962d09a6adSAlan Cox 	vm_page_lock_queues();
897db44450bSAlan Cox 	vm_page_flag_set(fs.m, PG_REFERENCED);
898ff97964aSJohn Dyson 
899df8bae1dSRodney W. Grimes 	/*
9000d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9010d94caffSDavid Greenman 	 * can find it.
902df8bae1dSRodney W. Grimes 	 */
903a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
904df8bae1dSRodney W. Grimes 		if (wired)
9054866e085SJohn Dyson 			vm_page_wire(fs.m);
906df8bae1dSRodney W. Grimes 		else
90773007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
9080d94caffSDavid Greenman 	} else {
9094866e085SJohn Dyson 		vm_page_activate(fs.m);
91026f9a767SRodney W. Grimes 	}
9112d09a6adSAlan Cox 	vm_page_unlock_queues();
91266bdd5d6SAlan Cox 	vm_page_wakeup(fs.m);
913eeec6babSJohn Baldwin 
914eebf3286SAlan Cox 	/*
915eebf3286SAlan Cox 	 * Unlock everything, and return
916eebf3286SAlan Cox 	 */
917eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
9181c4bcd05SJeff Roberson 	if (hardfault)
9191c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
9201c4bcd05SJeff Roberson 	else
9211c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
922df8bae1dSRodney W. Grimes 
923df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
924df8bae1dSRodney W. Grimes }
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes /*
927566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
928566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
929566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
930566526a9SAlan Cox  * of mmap time.
931566526a9SAlan Cox  */
932566526a9SAlan Cox static void
933566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
934566526a9SAlan Cox {
935566526a9SAlan Cox 	int i;
936566526a9SAlan Cox 	vm_offset_t addr, starta;
937566526a9SAlan Cox 	vm_pindex_t pindex;
9382053c127SStephan Uphoff 	vm_page_t m;
939566526a9SAlan Cox 	vm_object_t object;
940566526a9SAlan Cox 
941950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
942566526a9SAlan Cox 		return;
943566526a9SAlan Cox 
944566526a9SAlan Cox 	object = entry->object.vm_object;
945566526a9SAlan Cox 
946566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
947566526a9SAlan Cox 	if (starta < entry->start) {
948566526a9SAlan Cox 		starta = entry->start;
949566526a9SAlan Cox 	} else if (starta > addra) {
950566526a9SAlan Cox 		starta = 0;
951566526a9SAlan Cox 	}
952566526a9SAlan Cox 
953566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
954566526a9SAlan Cox 		vm_object_t backing_object, lobject;
955566526a9SAlan Cox 
956566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
957566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
958566526a9SAlan Cox 			addr = 0;
959566526a9SAlan Cox 
960566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
961566526a9SAlan Cox 			continue;
962566526a9SAlan Cox 
963566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
964566526a9SAlan Cox 			continue;
965566526a9SAlan Cox 
966566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
967566526a9SAlan Cox 		lobject = object;
968566526a9SAlan Cox 		VM_OBJECT_LOCK(lobject);
969566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
970566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
971566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
972566526a9SAlan Cox 			if (lobject->backing_object_offset & PAGE_MASK)
973566526a9SAlan Cox 				break;
974566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
975566526a9SAlan Cox 			VM_OBJECT_LOCK(backing_object);
976566526a9SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
977566526a9SAlan Cox 			lobject = backing_object;
978566526a9SAlan Cox 		}
979566526a9SAlan Cox 		/*
980566526a9SAlan Cox 		 * give-up when a page is not in memory
981566526a9SAlan Cox 		 */
982cbfbaad8SAlan Cox 		if (m == NULL) {
983cbfbaad8SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
984566526a9SAlan Cox 			break;
985cbfbaad8SAlan Cox 		}
986566526a9SAlan Cox 		if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
987566526a9SAlan Cox 			(m->busy == 0) &&
9889fea8cadSAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0) {
989566526a9SAlan Cox 
990566526a9SAlan Cox 			vm_page_lock_queues();
9917bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
9921f70d622SAlan Cox 			vm_page_unlock_queues();
99385f5b245SAlan Cox 		}
994cbfbaad8SAlan Cox 		VM_OBJECT_UNLOCK(lobject);
995566526a9SAlan Cox 	}
996566526a9SAlan Cox }
997566526a9SAlan Cox 
998566526a9SAlan Cox /*
999e3669ceeSMatthew Dillon  *	vm_fault_quick:
1000e3669ceeSMatthew Dillon  *
1001e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
1002e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
1003e3669ceeSMatthew Dillon  */
1004e3669ceeSMatthew Dillon int
1005e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
1006e3669ceeSMatthew Dillon {
1007e3669ceeSMatthew Dillon 	int r;
1008e3669ceeSMatthew Dillon 
1009e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
1010e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
1011e3669ceeSMatthew Dillon 	else
1012e3669ceeSMatthew Dillon 		r = fubyte(v);
1013e3669ceeSMatthew Dillon 	return(r);
1014e3669ceeSMatthew Dillon }
1015e3669ceeSMatthew Dillon 
1016e3669ceeSMatthew Dillon /*
1017df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1018df8bae1dSRodney W. Grimes  *
1019df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1020df8bae1dSRodney W. Grimes  */
1021df8bae1dSRodney W. Grimes int
10224be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10234be14af9SAlan Cox     boolean_t user_wire, boolean_t fictitious)
1024df8bae1dSRodney W. Grimes {
102554d92145SMatthew Dillon 	vm_offset_t va;
1026df8bae1dSRodney W. Grimes 	int rv;
1027df8bae1dSRodney W. Grimes 
1028df8bae1dSRodney W. Grimes 	/*
10290d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1030ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1031ef594d31SAlan Cox 	 * read-only sections.
1032df8bae1dSRodney W. Grimes 	 */
1033df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1034ef594d31SAlan Cox 		rv = vm_fault(map, va,
1035ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1036ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
10377aaaa4fdSJohn Dyson 		if (rv) {
10387aaaa4fdSJohn Dyson 			if (va != start)
10394be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
10407aaaa4fdSJohn Dyson 			return (rv);
10417aaaa4fdSJohn Dyson 		}
10427aaaa4fdSJohn Dyson 	}
10437aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
10447aaaa4fdSJohn Dyson }
10457aaaa4fdSJohn Dyson 
10467aaaa4fdSJohn Dyson /*
1047df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1048df8bae1dSRodney W. Grimes  *
1049df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1050df8bae1dSRodney W. Grimes  */
105126f9a767SRodney W. Grimes void
10524be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10534be14af9SAlan Cox     boolean_t fictitious)
1054df8bae1dSRodney W. Grimes {
1055227f9a1cSJake Burkholder 	vm_paddr_t pa;
1056227f9a1cSJake Burkholder 	vm_offset_t va;
105754d92145SMatthew Dillon 	pmap_t pmap;
1058df8bae1dSRodney W. Grimes 
1059df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1060df8bae1dSRodney W. Grimes 
1061df8bae1dSRodney W. Grimes 	/*
10620d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
10630d94caffSDavid Greenman 	 * mappings from the physical map system.
1064df8bae1dSRodney W. Grimes 	 */
1065df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1066df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1067227f9a1cSJake Burkholder 		if (pa != 0) {
1068df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
10694be14af9SAlan Cox 			if (!fictitious) {
10702d09a6adSAlan Cox 				vm_page_lock_queues();
107173007561SDavid Greenman 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
10722d09a6adSAlan Cox 				vm_page_unlock_queues();
1073df8bae1dSRodney W. Grimes 			}
1074b18bfc3dSJohn Dyson 		}
10754be14af9SAlan Cox 	}
1076df8bae1dSRodney W. Grimes }
1077df8bae1dSRodney W. Grimes 
1078df8bae1dSRodney W. Grimes /*
1079df8bae1dSRodney W. Grimes  *	Routine:
1080df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1081df8bae1dSRodney W. Grimes  *	Function:
1082df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
1083df8bae1dSRodney W. Grimes  *
1084df8bae1dSRodney W. Grimes  *	In/out conditions:
1085df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1086df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1087df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1088df8bae1dSRodney W. Grimes  */
108926f9a767SRodney W. Grimes void
109026f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1091df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
1092df8bae1dSRodney W. Grimes 	vm_map_t src_map;
1093df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
1094df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
1095df8bae1dSRodney W. Grimes {
1096c5b65a67SAlan Cox 	vm_object_t backing_object, dst_object, object;
1097df8bae1dSRodney W. Grimes 	vm_object_t src_object;
1098a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
1099a316d390SJohn Dyson 	vm_ooffset_t src_offset;
1100c5b65a67SAlan Cox 	vm_pindex_t pindex;
1101df8bae1dSRodney W. Grimes 	vm_prot_t prot;
1102df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1103df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1104df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1105df8bae1dSRodney W. Grimes 
1106df8bae1dSRodney W. Grimes #ifdef	lint
1107df8bae1dSRodney W. Grimes 	src_map++;
11080d94caffSDavid Greenman #endif	/* lint */
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
1111df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
1112df8bae1dSRodney W. Grimes 
1113df8bae1dSRodney W. Grimes 	/*
11140d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
11150d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1116df8bae1dSRodney W. Grimes 	 */
111724a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
111857b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1119f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1120f8a47341SAlan Cox 	dst_object->flags |= OBJ_COLORED;
1121f8a47341SAlan Cox 	dst_object->pg_color = atop(dst_entry->start);
1122f8a47341SAlan Cox #endif
1123df8bae1dSRodney W. Grimes 
11248afcf0ccSAlan Cox 	VM_OBJECT_LOCK(dst_object);
1125df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1126df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
1127df8bae1dSRodney W. Grimes 
1128df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
1129df8bae1dSRodney W. Grimes 
1130df8bae1dSRodney W. Grimes 	/*
11310d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
11320d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
11330d94caffSDavid Greenman 	 * object.
1134df8bae1dSRodney W. Grimes 	 */
1135df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
1136df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
1137df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1138df8bae1dSRodney W. Grimes 
1139df8bae1dSRodney W. Grimes 		/*
1140df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
1141df8bae1dSRodney W. Grimes 		 */
1142df8bae1dSRodney W. Grimes 		do {
1143a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
1144a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1145df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
11468afcf0ccSAlan Cox 				VM_OBJECT_UNLOCK(dst_object);
1147df8bae1dSRodney W. Grimes 				VM_WAIT;
11488afcf0ccSAlan Cox 				VM_OBJECT_LOCK(dst_object);
1149df8bae1dSRodney W. Grimes 			}
1150df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1151df8bae1dSRodney W. Grimes 
1152df8bae1dSRodney W. Grimes 		/*
1153df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
11540d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
11550d94caffSDavid Greenman 		 * memory.)
1156df8bae1dSRodney W. Grimes 		 */
1157f29ba63eSAlan Cox 		VM_OBJECT_LOCK(src_object);
1158c5b65a67SAlan Cox 		object = src_object;
1159c5b65a67SAlan Cox 		pindex = 0;
1160c5b65a67SAlan Cox 		while ((src_m = vm_page_lookup(object, pindex +
1161c5b65a67SAlan Cox 		    OFF_TO_IDX(dst_offset + src_offset))) == NULL &&
1162c5b65a67SAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) == 0 &&
1163c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1164c5b65a67SAlan Cox 			/*
1165c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1166c5b65a67SAlan Cox 			 */
1167c5b65a67SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1168c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
1169c5b65a67SAlan Cox 			VM_OBJECT_UNLOCK(object);
1170c5b65a67SAlan Cox 			object = backing_object;
1171c5b65a67SAlan Cox 		}
1172df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1173df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1174669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
1175c5b65a67SAlan Cox 		VM_OBJECT_UNLOCK(object);
1176669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
11778afcf0ccSAlan Cox 		VM_OBJECT_UNLOCK(dst_object);
1178df8bae1dSRodney W. Grimes 
1179df8bae1dSRodney W. Grimes 		/*
1180eb2a0517SAlan Cox 		 * Enter it in the pmap as a read and/or execute access.
1181df8bae1dSRodney W. Grimes 		 */
1182eb2a0517SAlan Cox 		pmap_enter(dst_map->pmap, vaddr, prot & ~VM_PROT_WRITE, dst_m,
1183eb2a0517SAlan Cox 		    prot, FALSE);
1184df8bae1dSRodney W. Grimes 
1185df8bae1dSRodney W. Grimes 		/*
1186df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1187df8bae1dSRodney W. Grimes 		 */
118844b8bd66SAlan Cox 		VM_OBJECT_LOCK(dst_object);
118944b8bd66SAlan Cox 		vm_page_lock_queues();
1190df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
11914abd55b2SAlan Cox 		vm_page_unlock_queues();
119266bdd5d6SAlan Cox 		vm_page_wakeup(dst_m);
1193df8bae1dSRodney W. Grimes 	}
11948afcf0ccSAlan Cox 	VM_OBJECT_UNLOCK(dst_object);
1195df8bae1dSRodney W. Grimes }
119626f9a767SRodney W. Grimes 
119726f9a767SRodney W. Grimes 
119826f9a767SRodney W. Grimes /*
119926f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
120022ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
120122ba64e8SJohn Dyson  * pages for the pages to be paged in.
120226f9a767SRodney W. Grimes  *
120326f9a767SRodney W. Grimes  * Inputs:
120422ba64e8SJohn Dyson  *	m, rbehind, rahead
120526f9a767SRodney W. Grimes  *
120626f9a767SRodney W. Grimes  * Outputs:
120726f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
120826f9a767SRodney W. Grimes  *
120926f9a767SRodney W. Grimes  * Return value:
121026f9a767SRodney W. Grimes  *  number of pages in marray
121123955314SAlfred Perlstein  *
121223955314SAlfred Perlstein  * This routine can't block.
121326f9a767SRodney W. Grimes  */
1214303b270bSEivind Eklund static int
121522ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
121626f9a767SRodney W. Grimes 	vm_page_t m;
121726f9a767SRodney W. Grimes 	int rbehind;
121822ba64e8SJohn Dyson 	int rahead;
121926f9a767SRodney W. Grimes 	vm_page_t *marray;
122026f9a767SRodney W. Grimes 	int *reqpage;
122126f9a767SRodney W. Grimes {
12222d8acc0fSJohn Dyson 	int i,j;
122326f9a767SRodney W. Grimes 	vm_object_t object;
1224a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
122526f9a767SRodney W. Grimes 	vm_page_t rtm;
1226170db9c6SJohn Dyson 	int cbehind, cahead;
122726f9a767SRodney W. Grimes 
1228f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
122923955314SAlfred Perlstein 
123026f9a767SRodney W. Grimes 	object = m->object;
1231a316d390SJohn Dyson 	pindex = m->pindex;
1232fcdd9721SPawel Jakub Dawidek 	cbehind = cahead = 0;
1233fcdd9721SPawel Jakub Dawidek 
1234f35329acSJohn Dyson 	/*
123526f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
123626f9a767SRodney W. Grimes 	 */
12371c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
123826f9a767SRodney W. Grimes 		return 0;
12392d8acc0fSJohn Dyson 	}
124026f9a767SRodney W. Grimes 
124122ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
124222ba64e8SJohn Dyson 		*reqpage = 0;
124322ba64e8SJohn Dyson 		marray[0] = m;
124422ba64e8SJohn Dyson 		return 1;
1245170db9c6SJohn Dyson 	}
124622ba64e8SJohn Dyson 
124722ba64e8SJohn Dyson 	if (rahead > cahead) {
124822ba64e8SJohn Dyson 		rahead = cahead;
124922ba64e8SJohn Dyson 	}
125022ba64e8SJohn Dyson 
1251170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1252170db9c6SJohn Dyson 		rbehind = cbehind;
1253170db9c6SJohn Dyson 	}
1254170db9c6SJohn Dyson 
125526f9a767SRodney W. Grimes 	/*
12562d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
125726f9a767SRodney W. Grimes 	 */
12582d8acc0fSJohn Dyson 	if (pindex > 0) {
12592d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1260a316d390SJohn Dyson 			rbehind = pindex;
12612d8acc0fSJohn Dyson 			startpindex = 0;
12622d8acc0fSJohn Dyson 		} else {
1263a316d390SJohn Dyson 			startpindex = pindex - rbehind;
12642d8acc0fSJohn Dyson 		}
12652d8acc0fSJohn Dyson 
12668f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
12678f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
12688f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
126926f9a767SRodney W. Grimes 
127080645364SAlan Cox 		/* tpindex is unsigned; beware of numeric underflow. */
127180645364SAlan Cox 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
127280645364SAlan Cox 		    tpindex < pindex; i++, tpindex--) {
127326f9a767SRodney W. Grimes 
12747bfda801SAlan Cox 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
12757bfda801SAlan Cox 			    VM_ALLOC_IFNOTCACHED);
1276ccbb2f72SJohn Dyson 			if (rtm == NULL) {
127780645364SAlan Cox 				/*
127880645364SAlan Cox 				 * Shift the allocated pages to the
127980645364SAlan Cox 				 * beginning of the array.
128080645364SAlan Cox 				 */
1281ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
128280645364SAlan Cox 					marray[j] = marray[j + tpindex + 1 -
128380645364SAlan Cox 					    startpindex];
128426f9a767SRodney W. Grimes 				}
128580645364SAlan Cox 				break;
128626f9a767SRodney W. Grimes 			}
1287170db9c6SJohn Dyson 
128880645364SAlan Cox 			marray[tpindex - startpindex] = rtm;
128926f9a767SRodney W. Grimes 		}
12902d8acc0fSJohn Dyson 	} else {
12912d8acc0fSJohn Dyson 		startpindex = 0;
12922d8acc0fSJohn Dyson 		i = 0;
12932d8acc0fSJohn Dyson 	}
12942d8acc0fSJohn Dyson 
12952d8acc0fSJohn Dyson 	marray[i] = m;
12962d8acc0fSJohn Dyson 	/* page offset of the required page */
12972d8acc0fSJohn Dyson 	*reqpage = i;
12982d8acc0fSJohn Dyson 
12992d8acc0fSJohn Dyson 	tpindex = pindex + 1;
13002d8acc0fSJohn Dyson 	i++;
13012d8acc0fSJohn Dyson 
13022d8acc0fSJohn Dyson 	/*
13032d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
13042d8acc0fSJohn Dyson 	 */
13052d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
13068f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
13078f8790a7SAlan Cox 		endpindex = rtm->pindex;
13082d8acc0fSJohn Dyson 	if (endpindex > object->size)
13092d8acc0fSJohn Dyson 		endpindex = object->size;
13102d8acc0fSJohn Dyson 
13112d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
13122d8acc0fSJohn Dyson 
13137bfda801SAlan Cox 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
13147bfda801SAlan Cox 		    VM_ALLOC_IFNOTCACHED);
13152d8acc0fSJohn Dyson 		if (rtm == NULL) {
13162d8acc0fSJohn Dyson 			break;
13172d8acc0fSJohn Dyson 		}
13182d8acc0fSJohn Dyson 
13192d8acc0fSJohn Dyson 		marray[i] = rtm;
13202d8acc0fSJohn Dyson 	}
13212d8acc0fSJohn Dyson 
132265ea29a6SAlan Cox 	/* return number of pages */
13232d8acc0fSJohn Dyson 	return i;
132426f9a767SRodney W. Grimes }
1325