xref: /freebsd/sys/vm/vm_fault.c (revision 210a688642f7e851e453853f45cdc90e016df1ff)
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;
133d2bf64c3SKonstantin Belousov 	int vfslocked;
1344866e085SJohn Dyson };
1354866e085SJohn Dyson 
13662a59e8fSWarner Losh static inline void
1374866e085SJohn Dyson release_page(struct faultstate *fs)
1384866e085SJohn Dyson {
1390d0be82aSKonstantin Belousov 
140e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
14166bdd5d6SAlan Cox 	vm_page_lock_queues();
1424866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1432ad98273SAlan Cox 	vm_page_unlock_queues();
1444866e085SJohn Dyson 	fs->m = NULL;
1454866e085SJohn Dyson }
1464866e085SJohn Dyson 
14762a59e8fSWarner Losh static inline void
1484866e085SJohn Dyson unlock_map(struct faultstate *fs)
1494866e085SJohn Dyson {
1500d0be82aSKonstantin Belousov 
15125adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1524866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15325adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1544866e085SJohn Dyson 	}
1554866e085SJohn Dyson }
1564866e085SJohn Dyson 
1574866e085SJohn Dyson static void
158a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1594866e085SJohn Dyson {
160f29ba63eSAlan Cox 
1614866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
162b009d5a0SAlan Cox 	VM_OBJECT_UNLOCK(fs->object);
1634866e085SJohn Dyson 	if (fs->object != fs->first_object) {
164b009d5a0SAlan Cox 		VM_OBJECT_LOCK(fs->first_object);
1652ad98273SAlan Cox 		vm_page_lock_queues();
1664866e085SJohn Dyson 		vm_page_free(fs->first_m);
1672ad98273SAlan Cox 		vm_page_unlock_queues();
1684866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
169b009d5a0SAlan Cox 		VM_OBJECT_UNLOCK(fs->first_object);
1704866e085SJohn Dyson 		fs->first_m = NULL;
1714866e085SJohn Dyson 	}
1724866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1734866e085SJohn Dyson 	unlock_map(fs);
1744866e085SJohn Dyson 	if (fs->vp != NULL) {
1750cddd8f0SMatthew Dillon 		vput(fs->vp);
1764866e085SJohn Dyson 		fs->vp = NULL;
1774866e085SJohn Dyson 	}
178d2bf64c3SKonstantin Belousov 	VFS_UNLOCK_GIANT(fs->vfslocked);
179d2bf64c3SKonstantin Belousov 	fs->vfslocked = 0;
1804866e085SJohn Dyson }
1814866e085SJohn Dyson 
182df8bae1dSRodney W. Grimes /*
18340360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
18440360b1bSMatthew Dillon  *	      current object *might* contain the page.
18540360b1bSMatthew Dillon  *
18640360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18740360b1bSMatthew Dillon  */
18840360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
18940360b1bSMatthew Dillon 			(((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
19040360b1bSMatthew Dillon 
19140360b1bSMatthew Dillon /*
192df8bae1dSRodney W. Grimes  *	vm_fault:
193df8bae1dSRodney W. Grimes  *
194956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
195df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
196df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
197df8bae1dSRodney W. Grimes  *	associated physical map.
198df8bae1dSRodney W. Grimes  *
199df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
200df8bae1dSRodney W. Grimes  *	proper page address.
201df8bae1dSRodney W. Grimes  *
202df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
203df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
204df8bae1dSRodney W. Grimes  *
205df8bae1dSRodney W. Grimes  *
206df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2070cddd8f0SMatthew Dillon  *	Caller may hold no locks.
208df8bae1dSRodney W. Grimes  */
209df8bae1dSRodney W. Grimes int
21023955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
21123955314SAlfred Perlstein 	 int fault_flags)
21223955314SAlfred Perlstein {
213df8bae1dSRodney W. Grimes 	vm_prot_t prot;
214ebf75125SAlan Cox 	int is_first_object_locked, result;
215c722e407SAlan Cox 	boolean_t are_queues_locked, growstack, wired;
2162d8acc0fSJohn Dyson 	int map_generation;
217df8bae1dSRodney W. Grimes 	vm_object_t next_object;
21826f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
2194866e085SJohn Dyson 	int hardfault;
220d2bf64c3SKonstantin Belousov 	int faultcount, ahead, behind;
2214866e085SJohn Dyson 	struct faultstate fs;
222d2bf64c3SKonstantin Belousov 	struct vnode *vp;
223d2bf64c3SKonstantin Belousov 	int locked, error;
224df8bae1dSRodney W. Grimes 
2254866e085SJohn Dyson 	hardfault = 0;
2266139043bSAlan Cox 	growstack = TRUE;
22767596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
228d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
229d2bf64c3SKonstantin Belousov 	fs.vfslocked = 0;
230d2bf64c3SKonstantin Belousov 	faultcount = behind = 0;
231df8bae1dSRodney W. Grimes 
232df8bae1dSRodney W. Grimes RetryFault:;
233df8bae1dSRodney W. Grimes 
234df8bae1dSRodney W. Grimes 	/*
2350d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2360d94caffSDavid Greenman 	 * search.
237df8bae1dSRodney W. Grimes 	 */
23840360b1bSMatthew Dillon 	fs.map = map;
23992de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
24092de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
24192de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
24292de35b0SAlan Cox 		if (result != KERN_PROTECTION_FAILURE ||
24392de35b0SAlan Cox 		    (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
2446139043bSAlan Cox 			if (growstack && result == KERN_INVALID_ADDRESS &&
2456139043bSAlan Cox 			    map != kernel_map && curproc != NULL) {
2466139043bSAlan Cox 				result = vm_map_growstack(curproc, vaddr);
247a976eb5eSAlan Cox 				if (result != KERN_SUCCESS)
2486139043bSAlan Cox 					return (KERN_FAILURE);
2496139043bSAlan Cox 				growstack = FALSE;
2506139043bSAlan Cox 				goto RetryFault;
2516139043bSAlan Cox 			}
25292de35b0SAlan Cox 			return (result);
25309e0c6ccSJohn Dyson 		}
25409e0c6ccSJohn Dyson 
2557aaaa4fdSJohn Dyson 		/*
2567aaaa4fdSJohn Dyson    		 * If we are user-wiring a r/w segment, and it is COW, then
2577aaaa4fdSJohn Dyson    		 * we need to do the COW operation.  Note that we don't COW
258595236dfSJohn Dyson    		 * currently RO sections now, because it is NOT desirable
2597aaaa4fdSJohn Dyson    		 * to COW .text.  We simply keep .text from ever being COW'ed
2607aaaa4fdSJohn Dyson    		 * and take the heat that one cannot debug wired .text sections.
2617aaaa4fdSJohn Dyson    		 */
2624866e085SJohn Dyson 		result = vm_map_lookup(&fs.map, vaddr,
26347221757SJohn Dyson 			VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
2644866e085SJohn Dyson 			&fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
265a976eb5eSAlan Cox 		if (result != KERN_SUCCESS)
2667788e219SAlan Cox 			return (result);
26747221757SJohn Dyson 
2687aaaa4fdSJohn Dyson 		/*
2697aaaa4fdSJohn Dyson 		 * If we don't COW now, on a user wire, the user will never
2707aaaa4fdSJohn Dyson 		 * be able to write to the mapping.  If we don't make this
2717aaaa4fdSJohn Dyson 		 * restriction, the bookkeeping would be nearly impossible.
272ff8f4ebeSAlan Cox 		 *
273ff8f4ebeSAlan Cox 		 * XXX The following assignment modifies the map without
274ff8f4ebeSAlan Cox 		 * holding a write lock on it.
2757aaaa4fdSJohn Dyson 		 */
2764866e085SJohn Dyson 		if ((fs.entry->protection & VM_PROT_WRITE) == 0)
2774866e085SJohn Dyson 			fs.entry->max_protection &= ~VM_PROT_WRITE;
2787aaaa4fdSJohn Dyson 	}
27947221757SJohn Dyson 
2804866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2812d8acc0fSJohn Dyson 
2824866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
28347221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
28492c4c4ebSBruce Evans 		    (u_long)vaddr);
2857aaaa4fdSJohn Dyson 	}
2867aaaa4fdSJohn Dyson 
28795e5e988SJohn Dyson 	/*
28895e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
28995e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
29095e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
29195e5e988SJohn Dyson 	 * they will stay around as well.
292fe8e0238SMatthew Dillon 	 *
293fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
294fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
295fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
29695e5e988SJohn Dyson 	 */
297d22bc710SAlan Cox 	VM_OBJECT_LOCK(fs.first_object);
298a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
299d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30095e5e988SJohn Dyson 
30125adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes 	if (wired)
304df8bae1dSRodney W. Grimes 		fault_type = prot;
305df8bae1dSRodney W. Grimes 
3064866e085SJohn Dyson 	fs.first_m = NULL;
307df8bae1dSRodney W. Grimes 
308df8bae1dSRodney W. Grimes 	/*
309df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
310df8bae1dSRodney W. Grimes 	 */
3114866e085SJohn Dyson 	fs.object = fs.first_object;
3124866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
313df8bae1dSRodney W. Grimes 	while (TRUE) {
3141c7c3c6aSMatthew Dillon 		/*
3151c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3161c7c3c6aSMatthew Dillon 		 */
3174866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3184866e085SJohn Dyson 			unlock_and_deallocate(&fs);
31947221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32047221757SJohn Dyson 		}
32147221757SJohn Dyson 
3221c7c3c6aSMatthew Dillon 		/*
3231c7c3c6aSMatthew Dillon 		 * See if page is resident
3241c7c3c6aSMatthew Dillon 		 */
3254866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3264866e085SJohn Dyson 		if (fs.m != NULL) {
32798cb733cSKenneth D. Merry 			/*
3289b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3299b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3309b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3319b80d344SKenneth D. Merry 			 * used when the page in question is part of
3329b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3339b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3349b80d344SKenneth D. Merry 			 * which is not what we want.
33598cb733cSKenneth D. Merry 			 */
336f4ecdf05SAlan Cox 			vm_page_lock_queues();
33798cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3389b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3399b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
34098cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
341f4ecdf05SAlan Cox 				vm_page_unlock_queues();
342d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
34398cb733cSKenneth D. Merry 				goto RetryFault;
34498cb733cSKenneth D. Merry 			}
34598cb733cSKenneth D. Merry 
346df8bae1dSRodney W. Grimes 			/*
3471c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3489af80719SAlan Cox 			 * if the page is busy via either VPO_BUSY or
3491c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3501c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3511c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
352956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3531c7c3c6aSMatthew Dillon 			 *
3541c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3551c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3561c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3571c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3581c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3591c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3601c7c3c6aSMatthew Dillon 			 * to pmap it.
361df8bae1dSRodney W. Grimes 			 */
3629af80719SAlan Cox 			if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
3634abd55b2SAlan Cox 				vm_page_unlock_queues();
364a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
365a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
366a51b0840SAlan Cox 					VM_OBJECT_LOCK(fs.first_object);
3679a96b638SAlan Cox 					vm_page_lock_queues();
368a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3699a96b638SAlan Cox 					vm_page_unlock_queues();
370a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
371a51b0840SAlan Cox 					VM_OBJECT_UNLOCK(fs.first_object);
372a51b0840SAlan Cox 					fs.first_m = NULL;
373a51b0840SAlan Cox 				}
374a51b0840SAlan Cox 				unlock_map(&fs);
375a51b0840SAlan Cox 				VM_OBJECT_LOCK(fs.object);
376a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
377a51b0840SAlan Cox 				    fs.pindex)) {
378e7e56b28SAlan Cox 					vm_page_sleep_if_busy(fs.m, TRUE,
379e7e56b28SAlan Cox 					    "vmpfw");
380a51b0840SAlan Cox 				}
381a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
382a51b0840SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
38367596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
3844866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
385df8bae1dSRodney W. Grimes 				goto RetryFault;
386df8bae1dSRodney W. Grimes 			}
3877bfda801SAlan Cox 			vm_pageq_remove(fs.m);
388768131d2SAlan Cox 			vm_page_unlock_queues();
3897615edaaSMatthew Dillon 
3901c7c3c6aSMatthew Dillon 			/*
3911c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
3921c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
3931c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
3941c7c3c6aSMatthew Dillon 			 * found the page ).
3951c7c3c6aSMatthew Dillon 			 */
396e69763a3SDoug Rabson 			vm_page_busy(fs.m);
3970a2e596aSAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL &&
3984866e085SJohn Dyson 				fs.m->object != kernel_object && fs.m->object != kmem_object) {
3990d94caffSDavid Greenman 				goto readrest;
4000d94caffSDavid Greenman 			}
401ffc82b0aSJohn Dyson 
402df8bae1dSRodney W. Grimes 			break;
403df8bae1dSRodney W. Grimes 		}
4041c7c3c6aSMatthew Dillon 
4051c7c3c6aSMatthew Dillon 		/*
40640360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
40740360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4081c7c3c6aSMatthew Dillon 		 */
40940360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4104866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4114866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4125f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4135f55e841SDavid Greenman 			}
41422ba64e8SJohn Dyson 
415df8bae1dSRodney W. Grimes 			/*
4160d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
417df8bae1dSRodney W. Grimes 			 */
41840360b1bSMatthew Dillon 			fs.m = NULL;
41940360b1bSMatthew Dillon 			if (!vm_page_count_severe()) {
420f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
421f8a47341SAlan Cox 				if ((fs.object->flags & OBJ_COLORED) == 0) {
422f8a47341SAlan Cox 					fs.object->flags |= OBJ_COLORED;
423f8a47341SAlan Cox 					fs.object->pg_color = atop(vaddr) -
424f8a47341SAlan Cox 					    fs.pindex;
425f8a47341SAlan Cox 				}
426f8a47341SAlan Cox #endif
4274866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
428d2bf64c3SKonstantin Belousov 				    (fs.object->type == OBJT_VNODE ||
429d2bf64c3SKonstantin Belousov 				     fs.object->backing_object != NULL) ?
430d2bf64c3SKonstantin Belousov 				    VM_ALLOC_NORMAL : VM_ALLOC_ZERO);
43140360b1bSMatthew Dillon 			}
4324866e085SJohn Dyson 			if (fs.m == NULL) {
4334866e085SJohn Dyson 				unlock_and_deallocate(&fs);
434ef6020d1SMike Silbersack 				VM_WAITPFAULT;
435df8bae1dSRodney W. Grimes 				goto RetryFault;
4360a2e596aSAlan Cox 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
4374ab8ab92SKonstantin Belousov 				break;
438df8bae1dSRodney W. Grimes 		}
43947221757SJohn Dyson 
4400d94caffSDavid Greenman readrest:
4411c7c3c6aSMatthew Dillon 		/*
44240360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
44340360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
44440360b1bSMatthew Dillon 		 * valid.
44540360b1bSMatthew Dillon 		 *
44640360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
44740360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
44840360b1bSMatthew Dillon 		 * at the same time.
4491c7c3c6aSMatthew Dillon 		 */
45040360b1bSMatthew Dillon 		if (TRYPAGER) {
451df8bae1dSRodney W. Grimes 			int rv;
4529dae7290SMatt Jacob 			int reqpage = 0;
4537f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
454867a482dSJohn Dyson 
4557f866e4bSAlan Cox 			if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
456867a482dSJohn Dyson 				ahead = 0;
457867a482dSJohn Dyson 				behind = 0;
4582d8acc0fSJohn Dyson 			} else {
4594866e085SJohn Dyson 				behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
4602d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4612d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
4622d8acc0fSJohn Dyson 
4634866e085SJohn Dyson 				ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
4642d8acc0fSJohn Dyson 				if (ahead > VM_FAULT_READ_AHEAD)
4652d8acc0fSJohn Dyson 					ahead = VM_FAULT_READ_AHEAD;
466867a482dSJohn Dyson 			}
4678d8b9c6eSAlan Cox 			is_first_object_locked = FALSE;
4688d8b9c6eSAlan Cox 			if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
46940360b1bSMatthew Dillon 			     (behavior != MAP_ENTRY_BEHAV_RANDOM &&
47040360b1bSMatthew Dillon 			      fs.pindex >= fs.entry->lastr &&
4718d8b9c6eSAlan Cox 			      fs.pindex < fs.entry->lastr + VM_FAULT_READ)) &&
4728d8b9c6eSAlan Cox 			    (fs.first_object == fs.object ||
4738d8b9c6eSAlan Cox 			     (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) &&
474ec96dca7SAlan Cox 			    fs.first_object->type != OBJT_DEVICE &&
47501381811SJohn Baldwin 			    fs.first_object->type != OBJT_PHYS &&
47601381811SJohn Baldwin 			    fs.first_object->type != OBJT_SG) {
477867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
47840360b1bSMatthew Dillon 
47940360b1bSMatthew Dillon 				if (fs.first_pindex < 2 * VM_FAULT_READ)
480867a482dSJohn Dyson 					firstpindex = 0;
481867a482dSJohn Dyson 				else
48240360b1bSMatthew Dillon 					firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
483867a482dSJohn Dyson 
484c722e407SAlan Cox 				are_queues_locked = FALSE;
4854221e284SAlan Cox 				/*
4864221e284SAlan Cox 				 * note: partially valid pages cannot be
4874221e284SAlan Cox 				 * included in the lookahead - NFS piecemeal
4884221e284SAlan Cox 				 * writes will barf on it badly.
4894221e284SAlan Cox 				 */
4904866e085SJohn Dyson 				for (tmppindex = fs.first_pindex - 1;
4912100d645SPeter Wemm 					tmppindex >= firstpindex;
492867a482dSJohn Dyson 					--tmppindex) {
493867a482dSJohn Dyson 					vm_page_t mt;
494a1287949SEivind Eklund 
4954866e085SJohn Dyson 					mt = vm_page_lookup(fs.first_object, tmppindex);
496867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
497867a482dSJohn Dyson 						break;
498ff97964aSJohn Dyson 					if (mt->busy ||
499c722e407SAlan Cox 					    (mt->oflags & VPO_BUSY))
500c722e407SAlan Cox 						continue;
501c722e407SAlan Cox 					if (!are_queues_locked) {
502c722e407SAlan Cox 						are_queues_locked = TRUE;
503c722e407SAlan Cox 						vm_page_lock_queues();
504c722e407SAlan Cox 					}
505c722e407SAlan Cox 					if (mt->hold_count ||
506867a482dSJohn Dyson 						mt->wire_count)
507867a482dSJohn Dyson 						continue;
5084fec79beSAlan Cox 					pmap_remove_all(mt);
509c6d9ef2eSAlan Cox 					if (mt->dirty) {
510867a482dSJohn Dyson 						vm_page_deactivate(mt);
511867a482dSJohn Dyson 					} else {
512867a482dSJohn Dyson 						vm_page_cache(mt);
513867a482dSJohn Dyson 					}
514867a482dSJohn Dyson 				}
515c722e407SAlan Cox 				if (are_queues_locked)
51615a5d210SAlan Cox 					vm_page_unlock_queues();
517867a482dSJohn Dyson 				ahead += behind;
518867a482dSJohn Dyson 				behind = 0;
519867a482dSJohn Dyson 			}
5208d8b9c6eSAlan Cox 			if (is_first_object_locked)
5218d8b9c6eSAlan Cox 				VM_OBJECT_UNLOCK(fs.first_object);
522d2bf64c3SKonstantin Belousov 
523d2bf64c3SKonstantin Belousov 			/*
524d2bf64c3SKonstantin Belousov 			 * Call the pager to retrieve the data, if any, after
525d2bf64c3SKonstantin Belousov 			 * releasing the lock on the map.  We hold a ref on
526d2bf64c3SKonstantin Belousov 			 * fs.object and the pages are VPO_BUSY'd.
527d2bf64c3SKonstantin Belousov 			 */
528d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
529d2bf64c3SKonstantin Belousov 
530d2bf64c3SKonstantin Belousov vnode_lock:
531d2bf64c3SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE) {
532d2bf64c3SKonstantin Belousov 				vp = fs.object->handle;
533d2bf64c3SKonstantin Belousov 				if (vp == fs.vp)
534d2bf64c3SKonstantin Belousov 					goto vnode_locked;
535d2bf64c3SKonstantin Belousov 				else if (fs.vp != NULL) {
536d2bf64c3SKonstantin Belousov 					vput(fs.vp);
537d2bf64c3SKonstantin Belousov 					fs.vp = NULL;
538d2bf64c3SKonstantin Belousov 				}
539d2bf64c3SKonstantin Belousov 				locked = VOP_ISLOCKED(vp);
540d2bf64c3SKonstantin Belousov 
541d2bf64c3SKonstantin Belousov 				if (VFS_NEEDSGIANT(vp->v_mount) && !fs.vfslocked) {
542d2bf64c3SKonstantin Belousov 					fs.vfslocked = 1;
543d2bf64c3SKonstantin Belousov 					if (!mtx_trylock(&Giant)) {
544d2bf64c3SKonstantin Belousov 						VM_OBJECT_UNLOCK(fs.object);
545d2bf64c3SKonstantin Belousov 						mtx_lock(&Giant);
546d2bf64c3SKonstantin Belousov 						VM_OBJECT_LOCK(fs.object);
547d2bf64c3SKonstantin Belousov 						goto vnode_lock;
548d2bf64c3SKonstantin Belousov 					}
549d2bf64c3SKonstantin Belousov 				}
550d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
551d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
552d2bf64c3SKonstantin Belousov 				/* Do not sleep for vnode lock while fs.m is busy */
553d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
554d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
555d2bf64c3SKonstantin Belousov 				if (error != 0) {
556d2bf64c3SKonstantin Belousov 					int vfslocked;
557d2bf64c3SKonstantin Belousov 
558d2bf64c3SKonstantin Belousov 					vfslocked = fs.vfslocked;
559d2bf64c3SKonstantin Belousov 					fs.vfslocked = 0; /* Keep Giant */
560d2bf64c3SKonstantin Belousov 					vhold(vp);
561d2bf64c3SKonstantin Belousov 					release_page(&fs);
562d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
563d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
564d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
565d2bf64c3SKonstantin Belousov 					vdrop(vp);
566d2bf64c3SKonstantin Belousov 					fs.vp = vp;
567d2bf64c3SKonstantin Belousov 					fs.vfslocked = vfslocked;
568d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
569d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
570d2bf64c3SKonstantin Belousov 					goto RetryFault;
571d2bf64c3SKonstantin Belousov 				}
572d2bf64c3SKonstantin Belousov 				fs.vp = vp;
573d2bf64c3SKonstantin Belousov 			}
574d2bf64c3SKonstantin Belousov vnode_locked:
575d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
576d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
577d2bf64c3SKonstantin Belousov 
578df8bae1dSRodney W. Grimes 			/*
5790d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5800d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5810d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5820d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5830d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5840d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5850d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5860d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5870d94caffSDavid Greenman 			 * return value is the index into the marray for the
5880d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5891c7c3c6aSMatthew Dillon 			 *
5909af80719SAlan Cox 			 * fs.m plus the additional pages are VPO_BUSY'd.
59126f9a767SRodney W. Grimes 			 */
59205f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5934866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
594df8bae1dSRodney W. Grimes 
59526f9a767SRodney W. Grimes 			rv = faultcount ?
5964866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
59724a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
59822ba64e8SJohn Dyson 
59926f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
600df8bae1dSRodney W. Grimes 				/*
601f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
602f230c45cSJohn Dyson 				 * with it.
603f230c45cSJohn Dyson 				 */
604f230c45cSJohn Dyson 
605f230c45cSJohn Dyson 				/*
6060d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
6070d94caffSDavid Greenman 				 * is responsible for disposition of old page
6080d94caffSDavid Greenman 				 * if moved.
609df8bae1dSRodney W. Grimes 				 */
6104866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
6114866e085SJohn Dyson 				if (!fs.m) {
6124866e085SJohn Dyson 					unlock_and_deallocate(&fs);
613f6b04d2bSDavid Greenman 					goto RetryFault;
614f6b04d2bSDavid Greenman 				}
615f6b04d2bSDavid Greenman 
61626f9a767SRodney W. Grimes 				hardfault++;
6171c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
618df8bae1dSRodney W. Grimes 			}
619df8bae1dSRodney W. Grimes 			/*
6200d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
6210d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
6220d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
623df8bae1dSRodney W. Grimes 			 *
62424a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
6250d94caffSDavid Greenman 			 * in this page.
626df8bae1dSRodney W. Grimes 			 *
6270d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
62824a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
6290d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
6300d94caffSDavid Greenman 			 * the same time that we are.
631df8bae1dSRodney W. Grimes 			 */
632a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
633f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
634f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
63526f9a767SRodney W. Grimes 			/*
636a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
63726f9a767SRodney W. Grimes 			 */
638a83c285cSDavid Greenman 			/*
6390d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6400d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6410d94caffSDavid Greenman 			 * fault w/ I/O error.
642a83c285cSDavid Greenman 			 */
6434866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
64447221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6452ad98273SAlan Cox 				vm_page_lock_queues();
6464866e085SJohn Dyson 				vm_page_free(fs.m);
6472ad98273SAlan Cox 				vm_page_unlock_queues();
6484866e085SJohn Dyson 				fs.m = NULL;
6494866e085SJohn Dyson 				unlock_and_deallocate(&fs);
650a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
65126f9a767SRodney W. Grimes 			}
6524866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6532ad98273SAlan Cox 				vm_page_lock_queues();
6544866e085SJohn Dyson 				vm_page_free(fs.m);
6552ad98273SAlan Cox 				vm_page_unlock_queues();
6564866e085SJohn Dyson 				fs.m = NULL;
65726f9a767SRodney W. Grimes 				/*
65826f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
65926f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
66026f9a767SRodney W. Grimes 				 */
661df8bae1dSRodney W. Grimes 			}
662df8bae1dSRodney W. Grimes 		}
66340360b1bSMatthew Dillon 
664df8bae1dSRodney W. Grimes 		/*
6651c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6661c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
667df8bae1dSRodney W. Grimes 		 */
6684866e085SJohn Dyson 		if (fs.object == fs.first_object)
6694866e085SJohn Dyson 			fs.first_m = fs.m;
670df8bae1dSRodney W. Grimes 
671df8bae1dSRodney W. Grimes 		/*
6720d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6730d94caffSDavid Greenman 		 * unlocking the current one.
674df8bae1dSRodney W. Grimes 		 */
6754866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6764866e085SJohn Dyson 		next_object = fs.object->backing_object;
677df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
678df8bae1dSRodney W. Grimes 			/*
6790d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6800d94caffSDavid Greenman 			 * object with zeros.
681df8bae1dSRodney W. Grimes 			 */
6824866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6834866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
684b009d5a0SAlan Cox 				VM_OBJECT_UNLOCK(fs.object);
685df8bae1dSRodney W. Grimes 
6864866e085SJohn Dyson 				fs.object = fs.first_object;
6874866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6884866e085SJohn Dyson 				fs.m = fs.first_m;
689f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
690df8bae1dSRodney W. Grimes 			}
6914866e085SJohn Dyson 			fs.first_m = NULL;
692df8bae1dSRodney W. Grimes 
6934221e284SAlan Cox 			/*
6944221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6954221e284SAlan Cox 			 */
6964866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
697fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6984221e284SAlan Cox 			} else {
69967596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
7004221e284SAlan Cox 			}
70167596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
7024221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
7031c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
7040d94caffSDavid Greenman 		} else {
705c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
706c8567c3aSAlan Cox 			    ("object loop %p", next_object));
707c8567c3aSAlan Cox 			VM_OBJECT_LOCK(next_object);
708c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
709c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
7104866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
711b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
7124866e085SJohn Dyson 			fs.object = next_object;
713df8bae1dSRodney W. Grimes 		}
714df8bae1dSRodney W. Grimes 	}
7151c7c3c6aSMatthew Dillon 
7169af80719SAlan Cox 	KASSERT((fs.m->oflags & VPO_BUSY) != 0,
7175526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 	/*
7200d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
721df8bae1dSRodney W. Grimes 	 * is held.]
722df8bae1dSRodney W. Grimes 	 */
723df8bae1dSRodney W. Grimes 
724df8bae1dSRodney W. Grimes 	/*
7250d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
7260d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
7270d94caffSDavid Greenman 	 * top-level object.
728df8bae1dSRodney W. Grimes 	 */
7294866e085SJohn Dyson 	if (fs.object != fs.first_object) {
730df8bae1dSRodney W. Grimes 		/*
7310d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
732df8bae1dSRodney W. Grimes 		 */
733df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
734df8bae1dSRodney W. Grimes 			/*
7351c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7361c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7371c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7381c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7391c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7401c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7411c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7421c7c3c6aSMatthew Dillon 			 * to swap when needed.
743df8bae1dSRodney W. Grimes 			 */
744ebf75125SAlan Cox 			is_first_object_locked = FALSE;
745e50346b5SAlan Cox 			if (
746de5f6a77SJohn Dyson 				/*
747de5f6a77SJohn Dyson 				 * Only one shadow object
748de5f6a77SJohn Dyson 				 */
7494866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
750de5f6a77SJohn Dyson 				/*
751de5f6a77SJohn Dyson 				 * No COW refs, except us
752de5f6a77SJohn Dyson 				 */
7534866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
754de5f6a77SJohn Dyson 				/*
7555929bcfaSPhilippe Charnier 				 * No one else can look this object up
756de5f6a77SJohn Dyson 				 */
7574866e085SJohn Dyson 				(fs.object->handle == NULL) &&
758de5f6a77SJohn Dyson 				/*
759de5f6a77SJohn Dyson 				 * No other ways to look the object up
760de5f6a77SJohn Dyson 				 */
7614866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7624866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
763ebf75125SAlan Cox 			    (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
764de5f6a77SJohn Dyson 				/*
765de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
766de5f6a77SJohn Dyson 				 */
767e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
768d98ddc46SAlan Cox 				vm_page_lock_queues();
7692d8acc0fSJohn Dyson 				/*
770de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
771df8bae1dSRodney W. Grimes 				 */
7724866e085SJohn Dyson 				vm_page_free(fs.first_m);
773de5f6a77SJohn Dyson 				/*
7741c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7751c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7761c7c3c6aSMatthew Dillon 				 * automatically made dirty.
777de5f6a77SJohn Dyson 				 */
7784866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
7799a96b638SAlan Cox 				vm_page_unlock_queues();
780768131d2SAlan Cox 				vm_page_busy(fs.m);
781d98ddc46SAlan Cox 				fs.first_m = fs.m;
7824866e085SJohn Dyson 				fs.m = NULL;
78367596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
784de5f6a77SJohn Dyson 			} else {
785de5f6a77SJohn Dyson 				/*
786de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
787de5f6a77SJohn Dyson 				 */
788669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
789669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
790de5f6a77SJohn Dyson 			}
7914866e085SJohn Dyson 			if (fs.m) {
792df8bae1dSRodney W. Grimes 				/*
793df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
794df8bae1dSRodney W. Grimes 				 */
7954866e085SJohn Dyson 				release_page(&fs);
796de5f6a77SJohn Dyson 			}
7971c7c3c6aSMatthew Dillon 			/*
7981c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7991c7c3c6aSMatthew Dillon 			 * conditional
8001c7c3c6aSMatthew Dillon 			 */
8014866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
802b009d5a0SAlan Cox 			VM_OBJECT_UNLOCK(fs.object);
803df8bae1dSRodney W. Grimes 			/*
804df8bae1dSRodney W. Grimes 			 * Only use the new page below...
805df8bae1dSRodney W. Grimes 			 */
8064866e085SJohn Dyson 			fs.object = fs.first_object;
8074866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
808d98ddc46SAlan Cox 			fs.m = fs.first_m;
809f29ba63eSAlan Cox 			if (!is_first_object_locked)
810f29ba63eSAlan Cox 				VM_OBJECT_LOCK(fs.object);
81167596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
8120d94caffSDavid Greenman 		} else {
813df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
814df8bae1dSRodney W. Grimes 		}
815df8bae1dSRodney W. Grimes 	}
816df8bae1dSRodney W. Grimes 
817df8bae1dSRodney W. Grimes 	/*
8180d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
8190d94caffSDavid Greenman 	 * lookup.
820df8bae1dSRodney W. Grimes 	 */
82119dc5607STor Egge 	if (!fs.lookup_still_valid) {
822df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
823a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
824df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
825df8bae1dSRodney W. Grimes 
82619dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
827b823bbd6SMatthew Dillon 			release_page(&fs);
828b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
829b823bbd6SMatthew Dillon 			goto RetryFault;
830b823bbd6SMatthew Dillon 		}
83119dc5607STor Egge 		fs.lookup_still_valid = TRUE;
83219dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
83319dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8344866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
835df8bae1dSRodney W. Grimes 
836df8bae1dSRodney W. Grimes 			/*
83744ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8380d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8390d94caffSDavid Greenman 			 * pageout will grab it eventually.
840df8bae1dSRodney W. Grimes 			 */
841df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8424866e085SJohn Dyson 				release_page(&fs);
8434866e085SJohn Dyson 				unlock_and_deallocate(&fs);
84419dc5607STor Egge 
84519dc5607STor Egge 				/*
84619dc5607STor Egge 				 * If retry of map lookup would have blocked then
84719dc5607STor Egge 				 * retry fault from start.
84819dc5607STor Egge 				 */
84919dc5607STor Egge 				if (result == KERN_FAILURE)
85019dc5607STor Egge 					goto RetryFault;
851df8bae1dSRodney W. Grimes 				return (result);
852df8bae1dSRodney W. Grimes 			}
8534866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8544866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8554866e085SJohn Dyson 				release_page(&fs);
8564866e085SJohn Dyson 				unlock_and_deallocate(&fs);
857df8bae1dSRodney W. Grimes 				goto RetryFault;
858df8bae1dSRodney W. Grimes 			}
85919dc5607STor Egge 
860df8bae1dSRodney W. Grimes 			/*
8610d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8620d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8630d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8640d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8650d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8660d94caffSDavid Greenman 			 * write-enabled after all.
867df8bae1dSRodney W. Grimes 			 */
868df8bae1dSRodney W. Grimes 			prot &= retry_prot;
869df8bae1dSRodney W. Grimes 		}
87019dc5607STor Egge 	}
871d2bf64c3SKonstantin Belousov 	/*
8725758fe71SAlan Cox 	 * If the page was filled by a pager, update the map entry's
8735758fe71SAlan Cox 	 * last read offset.  Since the pager does not return the
8745758fe71SAlan Cox 	 * actual set of pages that it read, this update is based on
8755758fe71SAlan Cox 	 * the requested set.  Typically, the requested and actual
8765758fe71SAlan Cox 	 * sets are the same.
877d2bf64c3SKonstantin Belousov 	 *
878d2bf64c3SKonstantin Belousov 	 * XXX The following assignment modifies the map
879d2bf64c3SKonstantin Belousov 	 * without holding a write lock on it.
880d2bf64c3SKonstantin Belousov 	 */
8815758fe71SAlan Cox 	if (hardfault)
882d2bf64c3SKonstantin Belousov 		fs.entry->lastr = fs.pindex + faultcount - behind;
883d2bf64c3SKonstantin Belousov 
8842ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
885b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8864f79d873SMatthew Dillon 
8872ddba215SDavid Greenman 		/*
8882ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
8894f79d873SMatthew Dillon 		 * written NOW so dirty it explicitly to save on
8904f79d873SMatthew Dillon 		 * pmap_is_modified() calls later.
8914f79d873SMatthew Dillon 		 *
892b146f9e5SAlan Cox 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
8934f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8944f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8954f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8964f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8974f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8981c7c3c6aSMatthew Dillon 		 *
8991c7c3c6aSMatthew Dillon 		 * Also tell the backing pager, if any, that it should remove
9001c7c3c6aSMatthew Dillon 		 * any swap backing since the page is now dirty.
9012ddba215SDavid Greenman 		 */
9024f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
9034f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
904b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
9054f79d873SMatthew Dillon 		} else {
906b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
9074f79d873SMatthew Dillon 		}
9085bf53acbSMatthew Dillon 		if (fault_flags & VM_FAULT_DIRTY) {
9097dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
9101c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
9112ddba215SDavid Greenman 		}
9122ddba215SDavid Greenman 	}
913f6b04d2bSDavid Greenman 
914bc6d84a6SMatthew Dillon 	/*
915bc6d84a6SMatthew Dillon 	 * Page had better still be busy
916bc6d84a6SMatthew Dillon 	 */
9179af80719SAlan Cox 	KASSERT(fs.m->oflags & VPO_BUSY,
918ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
9194221e284SAlan Cox 	/*
92078cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
9214221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
9224221e284SAlan Cox 	 */
92378cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
92478cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
925eebf3286SAlan Cox 	VM_OBJECT_UNLOCK(fs.object);
926cbfbaad8SAlan Cox 
92786735996SAlan Cox 	/*
92886735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
92986735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
93086735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
93186735996SAlan Cox 	 * won't find it (yet).
93286735996SAlan Cox 	 */
933eb2a0517SAlan Cox 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
9342d8acc0fSJohn Dyson 	if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
935566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
9362d8acc0fSJohn Dyson 	}
937eebf3286SAlan Cox 	VM_OBJECT_LOCK(fs.object);
9382d09a6adSAlan Cox 	vm_page_lock_queues();
939db44450bSAlan Cox 	vm_page_flag_set(fs.m, PG_REFERENCED);
940ff97964aSJohn Dyson 
941df8bae1dSRodney W. Grimes 	/*
9420d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9430d94caffSDavid Greenman 	 * can find it.
944df8bae1dSRodney W. Grimes 	 */
945a04c970aSJohn Dyson 	if (fault_flags & VM_FAULT_WIRE_MASK) {
946df8bae1dSRodney W. Grimes 		if (wired)
9474866e085SJohn Dyson 			vm_page_wire(fs.m);
948df8bae1dSRodney W. Grimes 		else
94973007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
9500d94caffSDavid Greenman 	} else {
9514866e085SJohn Dyson 		vm_page_activate(fs.m);
95226f9a767SRodney W. Grimes 	}
9532d09a6adSAlan Cox 	vm_page_unlock_queues();
95466bdd5d6SAlan Cox 	vm_page_wakeup(fs.m);
955eeec6babSJohn Baldwin 
956eebf3286SAlan Cox 	/*
957eebf3286SAlan Cox 	 * Unlock everything, and return
958eebf3286SAlan Cox 	 */
959eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
9601c4bcd05SJeff Roberson 	if (hardfault)
9611c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
9621c4bcd05SJeff Roberson 	else
9631c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
966df8bae1dSRodney W. Grimes }
967df8bae1dSRodney W. Grimes 
968df8bae1dSRodney W. Grimes /*
969566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
970566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
971566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
972566526a9SAlan Cox  * of mmap time.
973566526a9SAlan Cox  */
974566526a9SAlan Cox static void
975566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
976566526a9SAlan Cox {
977566526a9SAlan Cox 	int i;
978566526a9SAlan Cox 	vm_offset_t addr, starta;
979566526a9SAlan Cox 	vm_pindex_t pindex;
9802053c127SStephan Uphoff 	vm_page_t m;
981566526a9SAlan Cox 	vm_object_t object;
982566526a9SAlan Cox 
983950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
984566526a9SAlan Cox 		return;
985566526a9SAlan Cox 
986566526a9SAlan Cox 	object = entry->object.vm_object;
987566526a9SAlan Cox 
988566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
989566526a9SAlan Cox 	if (starta < entry->start) {
990566526a9SAlan Cox 		starta = entry->start;
991566526a9SAlan Cox 	} else if (starta > addra) {
992566526a9SAlan Cox 		starta = 0;
993566526a9SAlan Cox 	}
994566526a9SAlan Cox 
995566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
996566526a9SAlan Cox 		vm_object_t backing_object, lobject;
997566526a9SAlan Cox 
998566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
999566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
1000566526a9SAlan Cox 			addr = 0;
1001566526a9SAlan Cox 
1002566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1003566526a9SAlan Cox 			continue;
1004566526a9SAlan Cox 
1005566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1006566526a9SAlan Cox 			continue;
1007566526a9SAlan Cox 
1008566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1009566526a9SAlan Cox 		lobject = object;
1010566526a9SAlan Cox 		VM_OBJECT_LOCK(lobject);
1011566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1012566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1013566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
101436930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
101536930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1016566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1017566526a9SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1018566526a9SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
1019566526a9SAlan Cox 			lobject = backing_object;
1020566526a9SAlan Cox 		}
1021566526a9SAlan Cox 		/*
1022566526a9SAlan Cox 		 * give-up when a page is not in memory
1023566526a9SAlan Cox 		 */
1024cbfbaad8SAlan Cox 		if (m == NULL) {
1025cbfbaad8SAlan Cox 			VM_OBJECT_UNLOCK(lobject);
1026566526a9SAlan Cox 			break;
1027cbfbaad8SAlan Cox 		}
10280a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
10299fea8cadSAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0) {
1030566526a9SAlan Cox 			vm_page_lock_queues();
10317bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
10321f70d622SAlan Cox 			vm_page_unlock_queues();
103385f5b245SAlan Cox 		}
1034cbfbaad8SAlan Cox 		VM_OBJECT_UNLOCK(lobject);
1035566526a9SAlan Cox 	}
1036566526a9SAlan Cox }
1037566526a9SAlan Cox 
1038566526a9SAlan Cox /*
1039e3669ceeSMatthew Dillon  *	vm_fault_quick:
1040e3669ceeSMatthew Dillon  *
1041e3669ceeSMatthew Dillon  *	Ensure that the requested virtual address, which may be in userland,
1042e3669ceeSMatthew Dillon  *	is valid.  Fault-in the page if necessary.  Return -1 on failure.
1043e3669ceeSMatthew Dillon  */
1044e3669ceeSMatthew Dillon int
1045e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot)
1046e3669ceeSMatthew Dillon {
1047e3669ceeSMatthew Dillon 	int r;
1048e3669ceeSMatthew Dillon 
1049e3669ceeSMatthew Dillon 	if (prot & VM_PROT_WRITE)
1050e3669ceeSMatthew Dillon 		r = subyte(v, fubyte(v));
1051e3669ceeSMatthew Dillon 	else
1052e3669ceeSMatthew Dillon 		r = fubyte(v);
1053e3669ceeSMatthew Dillon 	return(r);
1054e3669ceeSMatthew Dillon }
1055e3669ceeSMatthew Dillon 
1056e3669ceeSMatthew Dillon /*
1057df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1058df8bae1dSRodney W. Grimes  *
1059df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1060df8bae1dSRodney W. Grimes  */
1061df8bae1dSRodney W. Grimes int
10624be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10634be14af9SAlan Cox     boolean_t user_wire, boolean_t fictitious)
1064df8bae1dSRodney W. Grimes {
106554d92145SMatthew Dillon 	vm_offset_t va;
1066df8bae1dSRodney W. Grimes 	int rv;
1067df8bae1dSRodney W. Grimes 
1068df8bae1dSRodney W. Grimes 	/*
10690d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1070ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1071ef594d31SAlan Cox 	 * read-only sections.
1072df8bae1dSRodney W. Grimes 	 */
1073df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1074ef594d31SAlan Cox 		rv = vm_fault(map, va,
1075ef594d31SAlan Cox 		    user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1076ef594d31SAlan Cox 		    user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
10777aaaa4fdSJohn Dyson 		if (rv) {
10787aaaa4fdSJohn Dyson 			if (va != start)
10794be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
10807aaaa4fdSJohn Dyson 			return (rv);
10817aaaa4fdSJohn Dyson 		}
10827aaaa4fdSJohn Dyson 	}
10837aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
10847aaaa4fdSJohn Dyson }
10857aaaa4fdSJohn Dyson 
10867aaaa4fdSJohn Dyson /*
1087df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1088df8bae1dSRodney W. Grimes  *
1089df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1090df8bae1dSRodney W. Grimes  */
109126f9a767SRodney W. Grimes void
10924be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
10934be14af9SAlan Cox     boolean_t fictitious)
1094df8bae1dSRodney W. Grimes {
1095227f9a1cSJake Burkholder 	vm_paddr_t pa;
1096227f9a1cSJake Burkholder 	vm_offset_t va;
109754d92145SMatthew Dillon 	pmap_t pmap;
1098df8bae1dSRodney W. Grimes 
1099df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes 	/*
11020d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
11030d94caffSDavid Greenman 	 * mappings from the physical map system.
1104df8bae1dSRodney W. Grimes 	 */
1105df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1106df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1107227f9a1cSJake Burkholder 		if (pa != 0) {
1108df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
11094be14af9SAlan Cox 			if (!fictitious) {
11102d09a6adSAlan Cox 				vm_page_lock_queues();
111173007561SDavid Greenman 				vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
11122d09a6adSAlan Cox 				vm_page_unlock_queues();
1113df8bae1dSRodney W. Grimes 			}
1114b18bfc3dSJohn Dyson 		}
11154be14af9SAlan Cox 	}
1116df8bae1dSRodney W. Grimes }
1117df8bae1dSRodney W. Grimes 
1118df8bae1dSRodney W. Grimes /*
1119df8bae1dSRodney W. Grimes  *	Routine:
1120df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1121df8bae1dSRodney W. Grimes  *	Function:
1122210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1123210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1124210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1125210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1126df8bae1dSRodney W. Grimes  *
1127df8bae1dSRodney W. Grimes  *	In/out conditions:
1128df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1129df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1130df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1131df8bae1dSRodney W. Grimes  */
113226f9a767SRodney W. Grimes void
1133121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1134121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1135121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1136df8bae1dSRodney W. Grimes {
1137210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
11387afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1139210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1140df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1141df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1142df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1143210a6886SKonstantin Belousov 	boolean_t src_readonly, upgrade;
1144df8bae1dSRodney W. Grimes 
1145df8bae1dSRodney W. Grimes #ifdef	lint
1146df8bae1dSRodney W. Grimes 	src_map++;
11470d94caffSDavid Greenman #endif	/* lint */
1148df8bae1dSRodney W. Grimes 
1149210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
1150210a6886SKonstantin Belousov 
1151df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
11527afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
11537afab86cSAlan Cox 	src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1154df8bae1dSRodney W. Grimes 
1155df8bae1dSRodney W. Grimes 	/*
11560d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
11570d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1158df8bae1dSRodney W. Grimes 	 */
115924a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
116057b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1161f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1162f8a47341SAlan Cox 	dst_object->flags |= OBJ_COLORED;
1163f8a47341SAlan Cox 	dst_object->pg_color = atop(dst_entry->start);
1164f8a47341SAlan Cox #endif
1165df8bae1dSRodney W. Grimes 
11668afcf0ccSAlan Cox 	VM_OBJECT_LOCK(dst_object);
1167210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1168121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
1169df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1170df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
11713364c323SKonstantin Belousov 	dst_object->charge = dst_entry->end - dst_entry->start;
1172210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1173121fd461SKonstantin Belousov 		KASSERT(dst_entry->uip == NULL,
1174121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1175210a6886SKonstantin Belousov 		dst_object->uip = curthread->td_ucred->cr_ruidinfo;
1176210a6886SKonstantin Belousov 		uihold(dst_object->uip);
1177121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
1178210a6886SKonstantin Belousov 	} else {
1179210a6886SKonstantin Belousov 		dst_object->uip = dst_entry->uip;
1180210a6886SKonstantin Belousov 		dst_entry->uip = NULL;
1181210a6886SKonstantin Belousov 	}
1182210a6886SKonstantin Belousov 	access = prot = dst_entry->max_protection;
1183210a6886SKonstantin Belousov 	/*
1184210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1185210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1186210a6886SKonstantin Belousov 	 * write accesses.
1187210a6886SKonstantin Belousov 	 *
1188210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1189210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1190210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1191210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1192210a6886SKonstantin Belousov 	 */
1193210a6886SKonstantin Belousov 	if (!upgrade)
1194210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1195df8bae1dSRodney W. Grimes 
1196df8bae1dSRodney W. Grimes 	/*
11970d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
11980d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
11990d94caffSDavid Greenman 	 * object.
1200df8bae1dSRodney W. Grimes 	 */
12017afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1202df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
12037afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
1204df8bae1dSRodney W. Grimes 
1205df8bae1dSRodney W. Grimes 		/*
12067afab86cSAlan Cox 		 * Allocate a page in the destination object.
1207df8bae1dSRodney W. Grimes 		 */
1208df8bae1dSRodney W. Grimes 		do {
12097afab86cSAlan Cox 			dst_m = vm_page_alloc(dst_object, dst_pindex,
12107afab86cSAlan Cox 			    VM_ALLOC_NORMAL);
1211df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
12128afcf0ccSAlan Cox 				VM_OBJECT_UNLOCK(dst_object);
1213df8bae1dSRodney W. Grimes 				VM_WAIT;
12148afcf0ccSAlan Cox 				VM_OBJECT_LOCK(dst_object);
1215df8bae1dSRodney W. Grimes 			}
1216df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1217df8bae1dSRodney W. Grimes 
1218df8bae1dSRodney W. Grimes 		/*
1219df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
12200d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
12210d94caffSDavid Greenman 		 * memory.)
1222df8bae1dSRodney W. Grimes 		 */
1223f29ba63eSAlan Cox 		VM_OBJECT_LOCK(src_object);
1224c5b65a67SAlan Cox 		object = src_object;
12257afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
12267afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
12277afab86cSAlan Cox 		    src_readonly &&
1228c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1229c5b65a67SAlan Cox 			/*
1230c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1231c5b65a67SAlan Cox 			 */
1232c5b65a67SAlan Cox 			VM_OBJECT_LOCK(backing_object);
1233c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
1234c5b65a67SAlan Cox 			VM_OBJECT_UNLOCK(object);
1235c5b65a67SAlan Cox 			object = backing_object;
1236c5b65a67SAlan Cox 		}
1237df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1238df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1239669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
1240c5b65a67SAlan Cox 		VM_OBJECT_UNLOCK(object);
1241669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
12428afcf0ccSAlan Cox 		VM_OBJECT_UNLOCK(dst_object);
1243df8bae1dSRodney W. Grimes 
1244df8bae1dSRodney W. Grimes 		/*
1245210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1246210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1247210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
1248df8bae1dSRodney W. Grimes 		 */
1249210a6886SKonstantin Belousov 		pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1250df8bae1dSRodney W. Grimes 
1251df8bae1dSRodney W. Grimes 		/*
1252df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1253df8bae1dSRodney W. Grimes 		 */
125444b8bd66SAlan Cox 		VM_OBJECT_LOCK(dst_object);
125544b8bd66SAlan Cox 		vm_page_lock_queues();
1256210a6886SKonstantin Belousov 		if (upgrade) {
1257210a6886SKonstantin Belousov 			vm_page_unwire(src_m, 0);
1258210a6886SKonstantin Belousov 			vm_page_wire(dst_m);
1259210a6886SKonstantin Belousov 		} else
1260df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
12614abd55b2SAlan Cox 		vm_page_unlock_queues();
126266bdd5d6SAlan Cox 		vm_page_wakeup(dst_m);
1263df8bae1dSRodney W. Grimes 	}
12648afcf0ccSAlan Cox 	VM_OBJECT_UNLOCK(dst_object);
1265210a6886SKonstantin Belousov 	if (upgrade) {
1266210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1267210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1268210a6886SKonstantin Belousov 	}
1269df8bae1dSRodney W. Grimes }
127026f9a767SRodney W. Grimes 
127126f9a767SRodney W. Grimes 
127226f9a767SRodney W. Grimes /*
127326f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
127422ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
127522ba64e8SJohn Dyson  * pages for the pages to be paged in.
127626f9a767SRodney W. Grimes  *
127726f9a767SRodney W. Grimes  * Inputs:
127822ba64e8SJohn Dyson  *	m, rbehind, rahead
127926f9a767SRodney W. Grimes  *
128026f9a767SRodney W. Grimes  * Outputs:
128126f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
128226f9a767SRodney W. Grimes  *
128326f9a767SRodney W. Grimes  * Return value:
128426f9a767SRodney W. Grimes  *  number of pages in marray
128526f9a767SRodney W. Grimes  */
1286303b270bSEivind Eklund static int
128722ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
128826f9a767SRodney W. Grimes 	vm_page_t m;
128926f9a767SRodney W. Grimes 	int rbehind;
129022ba64e8SJohn Dyson 	int rahead;
129126f9a767SRodney W. Grimes 	vm_page_t *marray;
129226f9a767SRodney W. Grimes 	int *reqpage;
129326f9a767SRodney W. Grimes {
12942d8acc0fSJohn Dyson 	int i,j;
129526f9a767SRodney W. Grimes 	vm_object_t object;
1296a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
129726f9a767SRodney W. Grimes 	vm_page_t rtm;
1298170db9c6SJohn Dyson 	int cbehind, cahead;
129926f9a767SRodney W. Grimes 
1300f29ba63eSAlan Cox 	VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
130123955314SAlfred Perlstein 
130226f9a767SRodney W. Grimes 	object = m->object;
1303a316d390SJohn Dyson 	pindex = m->pindex;
1304fcdd9721SPawel Jakub Dawidek 	cbehind = cahead = 0;
1305fcdd9721SPawel Jakub Dawidek 
1306f35329acSJohn Dyson 	/*
130726f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
130826f9a767SRodney W. Grimes 	 */
13091c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
131026f9a767SRodney W. Grimes 		return 0;
13112d8acc0fSJohn Dyson 	}
131226f9a767SRodney W. Grimes 
131322ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
131422ba64e8SJohn Dyson 		*reqpage = 0;
131522ba64e8SJohn Dyson 		marray[0] = m;
131622ba64e8SJohn Dyson 		return 1;
1317170db9c6SJohn Dyson 	}
131822ba64e8SJohn Dyson 
131922ba64e8SJohn Dyson 	if (rahead > cahead) {
132022ba64e8SJohn Dyson 		rahead = cahead;
132122ba64e8SJohn Dyson 	}
132222ba64e8SJohn Dyson 
1323170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1324170db9c6SJohn Dyson 		rbehind = cbehind;
1325170db9c6SJohn Dyson 	}
1326170db9c6SJohn Dyson 
132726f9a767SRodney W. Grimes 	/*
13282d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
132926f9a767SRodney W. Grimes 	 */
13302d8acc0fSJohn Dyson 	if (pindex > 0) {
13312d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1332a316d390SJohn Dyson 			rbehind = pindex;
13332d8acc0fSJohn Dyson 			startpindex = 0;
13342d8acc0fSJohn Dyson 		} else {
1335a316d390SJohn Dyson 			startpindex = pindex - rbehind;
13362d8acc0fSJohn Dyson 		}
13372d8acc0fSJohn Dyson 
13388f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
13398f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
13408f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
134126f9a767SRodney W. Grimes 
134280645364SAlan Cox 		/* tpindex is unsigned; beware of numeric underflow. */
134380645364SAlan Cox 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
134480645364SAlan Cox 		    tpindex < pindex; i++, tpindex--) {
134526f9a767SRodney W. Grimes 
13467bfda801SAlan Cox 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
13477bfda801SAlan Cox 			    VM_ALLOC_IFNOTCACHED);
1348ccbb2f72SJohn Dyson 			if (rtm == NULL) {
134980645364SAlan Cox 				/*
135080645364SAlan Cox 				 * Shift the allocated pages to the
135180645364SAlan Cox 				 * beginning of the array.
135280645364SAlan Cox 				 */
1353ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
135480645364SAlan Cox 					marray[j] = marray[j + tpindex + 1 -
135580645364SAlan Cox 					    startpindex];
135626f9a767SRodney W. Grimes 				}
135780645364SAlan Cox 				break;
135826f9a767SRodney W. Grimes 			}
1359170db9c6SJohn Dyson 
136080645364SAlan Cox 			marray[tpindex - startpindex] = rtm;
136126f9a767SRodney W. Grimes 		}
13622d8acc0fSJohn Dyson 	} else {
13632d8acc0fSJohn Dyson 		startpindex = 0;
13642d8acc0fSJohn Dyson 		i = 0;
13652d8acc0fSJohn Dyson 	}
13662d8acc0fSJohn Dyson 
13672d8acc0fSJohn Dyson 	marray[i] = m;
13682d8acc0fSJohn Dyson 	/* page offset of the required page */
13692d8acc0fSJohn Dyson 	*reqpage = i;
13702d8acc0fSJohn Dyson 
13712d8acc0fSJohn Dyson 	tpindex = pindex + 1;
13722d8acc0fSJohn Dyson 	i++;
13732d8acc0fSJohn Dyson 
13742d8acc0fSJohn Dyson 	/*
13752d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
13762d8acc0fSJohn Dyson 	 */
13772d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
13788f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
13798f8790a7SAlan Cox 		endpindex = rtm->pindex;
13802d8acc0fSJohn Dyson 	if (endpindex > object->size)
13812d8acc0fSJohn Dyson 		endpindex = object->size;
13822d8acc0fSJohn Dyson 
13832d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
13842d8acc0fSJohn Dyson 
13857bfda801SAlan Cox 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
13867bfda801SAlan Cox 		    VM_ALLOC_IFNOTCACHED);
13872d8acc0fSJohn Dyson 		if (rtm == NULL) {
13882d8acc0fSJohn Dyson 			break;
13892d8acc0fSJohn Dyson 		}
13902d8acc0fSJohn Dyson 
13912d8acc0fSJohn Dyson 		marray[i] = rtm;
13922d8acc0fSJohn Dyson 	}
13932d8acc0fSJohn Dyson 
139465ea29a6SAlan Cox 	/* return number of pages */
13952d8acc0fSJohn Dyson 	return i;
139626f9a767SRodney W. Grimes }
1397