xref: /freebsd/sys/vm/vm_fault.c (revision c82b01813e76de4e88702e49695f3d173f523544)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
22df8bae1dSRodney W. Grimes  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
683c4dd356SDavid Greenman  *
69c82b0181SJohn Dyson  * $Id: vm_fault.c,v 1.49 1996/06/08 06:48:32 dyson Exp $
70df8bae1dSRodney W. Grimes  */
71df8bae1dSRodney W. Grimes 
72df8bae1dSRodney W. Grimes /*
73df8bae1dSRodney W. Grimes  *	Page fault handling module.
74df8bae1dSRodney W. Grimes  */
75df8bae1dSRodney W. Grimes 
76df8bae1dSRodney W. Grimes #include <sys/param.h>
77df8bae1dSRodney W. Grimes #include <sys/systm.h>
7826f9a767SRodney W. Grimes #include <sys/proc.h>
7924a1cce3SDavid Greenman #include <sys/vnode.h>
8005f0fdd2SPoul-Henning Kamp #include <sys/resource.h>
8105f0fdd2SPoul-Henning Kamp #include <sys/signalvar.h>
8226f9a767SRodney W. Grimes #include <sys/resourcevar.h>
83efeaf95aSDavid Greenman #include <sys/vmmeter.h>
84df8bae1dSRodney W. Grimes 
85df8bae1dSRodney W. Grimes #include <vm/vm.h>
86efeaf95aSDavid Greenman #include <vm/vm_param.h>
87efeaf95aSDavid Greenman #include <vm/vm_prot.h>
88efeaf95aSDavid Greenman #include <vm/lock.h>
89efeaf95aSDavid Greenman #include <vm/pmap.h>
90efeaf95aSDavid Greenman #include <vm/vm_map.h>
91efeaf95aSDavid Greenman #include <vm/vm_object.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
94a83c285cSDavid Greenman #include <vm/vm_kern.h>
9524a1cce3SDavid Greenman #include <vm/vm_pager.h>
9624a1cce3SDavid Greenman #include <vm/vnode_pager.h>
97cd41fc12SDavid Greenman #include <vm/swap_pager.h>
98efeaf95aSDavid Greenman #include <vm/vm_extern.h>
99df8bae1dSRodney W. Grimes 
10022ba64e8SJohn Dyson int vm_fault_additional_pages __P((vm_page_t, int, int, vm_page_t *, int *));
10126f9a767SRodney W. Grimes 
10226f9a767SRodney W. Grimes #define VM_FAULT_READ_AHEAD 4
10326f9a767SRodney W. Grimes #define VM_FAULT_READ_BEHIND 3
10426f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
10526f9a767SRodney W. Grimes 
106de5f6a77SJohn Dyson int vm_fault_free_1;
107de5f6a77SJohn Dyson int vm_fault_copy_save_1;
108de5f6a77SJohn Dyson int vm_fault_copy_save_2;
109de5f6a77SJohn Dyson 
110df8bae1dSRodney W. Grimes /*
111df8bae1dSRodney W. Grimes  *	vm_fault:
112df8bae1dSRodney W. Grimes  *
113df8bae1dSRodney W. Grimes  *	Handle a page fault occuring at the given address,
114df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
115df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
116df8bae1dSRodney W. Grimes  *	associated physical map.
117df8bae1dSRodney W. Grimes  *
118df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
119df8bae1dSRodney W. Grimes  *	proper page address.
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
122df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *
125df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
126df8bae1dSRodney W. Grimes  *	Caller may hold no locks.
127df8bae1dSRodney W. Grimes  */
128df8bae1dSRodney W. Grimes int
129df8bae1dSRodney W. Grimes vm_fault(map, vaddr, fault_type, change_wiring)
130df8bae1dSRodney W. Grimes 	vm_map_t map;
131df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
132df8bae1dSRodney W. Grimes 	vm_prot_t fault_type;
133df8bae1dSRodney W. Grimes 	boolean_t change_wiring;
134df8bae1dSRodney W. Grimes {
135df8bae1dSRodney W. Grimes 	vm_object_t first_object;
136a316d390SJohn Dyson 	vm_pindex_t first_pindex;
137df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
138df8bae1dSRodney W. Grimes 	register vm_object_t object;
139a316d390SJohn Dyson 	register vm_pindex_t pindex;
14026f9a767SRodney W. Grimes 	vm_page_t m;
141df8bae1dSRodney W. Grimes 	vm_page_t first_m;
142df8bae1dSRodney W. Grimes 	vm_prot_t prot;
143df8bae1dSRodney W. Grimes 	int result;
144df8bae1dSRodney W. Grimes 	boolean_t wired;
145df8bae1dSRodney W. Grimes 	boolean_t su;
146df8bae1dSRodney W. Grimes 	boolean_t lookup_still_valid;
147df8bae1dSRodney W. Grimes 	vm_page_t old_m;
148df8bae1dSRodney W. Grimes 	vm_object_t next_object;
14926f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
15026f9a767SRodney W. Grimes 	int hardfault = 0;
151f6b04d2bSDavid Greenman 	struct vnode *vp = NULL;
152df8bae1dSRodney W. Grimes 
153b8d95f16SDavid Greenman 	cnt.v_vm_faults++;	/* needs lock XXX */
154df8bae1dSRodney W. Grimes /*
155df8bae1dSRodney W. Grimes  *	Recovery actions
156df8bae1dSRodney W. Grimes  */
157df8bae1dSRodney W. Grimes #define	FREE_PAGE(m)	{				\
158df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);					\
159df8bae1dSRodney W. Grimes 	vm_page_free(m);				\
160df8bae1dSRodney W. Grimes }
161df8bae1dSRodney W. Grimes 
162df8bae1dSRodney W. Grimes #define	RELEASE_PAGE(m)	{				\
163df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);					\
164bd7e5f99SJohn Dyson 	if (m->queue != PQ_ACTIVE) vm_page_activate(m);		\
165df8bae1dSRodney W. Grimes }
166df8bae1dSRodney W. Grimes 
167df8bae1dSRodney W. Grimes #define	UNLOCK_MAP	{				\
168df8bae1dSRodney W. Grimes 	if (lookup_still_valid) {			\
169df8bae1dSRodney W. Grimes 		vm_map_lookup_done(map, entry);		\
170df8bae1dSRodney W. Grimes 		lookup_still_valid = FALSE;		\
171df8bae1dSRodney W. Grimes 	}						\
172df8bae1dSRodney W. Grimes }
173df8bae1dSRodney W. Grimes 
174df8bae1dSRodney W. Grimes #define	UNLOCK_THINGS	{				\
175f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object); \
176df8bae1dSRodney W. Grimes 	if (object != first_object) {			\
177df8bae1dSRodney W. Grimes 		FREE_PAGE(first_m);			\
178f919ebdeSDavid Greenman 		vm_object_pip_wakeup(first_object); \
179df8bae1dSRodney W. Grimes 	}						\
180df8bae1dSRodney W. Grimes 	UNLOCK_MAP;					\
18124a1cce3SDavid Greenman 	if (vp != NULL) VOP_UNLOCK(vp);			\
182df8bae1dSRodney W. Grimes }
183df8bae1dSRodney W. Grimes 
184df8bae1dSRodney W. Grimes #define	UNLOCK_AND_DEALLOCATE	{			\
185df8bae1dSRodney W. Grimes 	UNLOCK_THINGS;					\
186df8bae1dSRodney W. Grimes 	vm_object_deallocate(first_object);		\
187df8bae1dSRodney W. Grimes }
188df8bae1dSRodney W. Grimes 
18926f9a767SRodney W. Grimes 
190df8bae1dSRodney W. Grimes RetryFault:;
191df8bae1dSRodney W. Grimes 
192df8bae1dSRodney W. Grimes 	/*
1930d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
1940d94caffSDavid Greenman 	 * search.
195df8bae1dSRodney W. Grimes 	 */
196df8bae1dSRodney W. Grimes 
19722ba64e8SJohn Dyson 	if ((result = vm_map_lookup(&map, vaddr,
19822ba64e8SJohn Dyson 		fault_type, &entry, &first_object,
199a316d390SJohn Dyson 		&first_pindex, &prot, &wired, &su)) != KERN_SUCCESS) {
200df8bae1dSRodney W. Grimes 		return (result);
201df8bae1dSRodney W. Grimes 	}
202f6b04d2bSDavid Greenman 
20324a1cce3SDavid Greenman 	vp = vnode_pager_lock(first_object);
204f6b04d2bSDavid Greenman 
205df8bae1dSRodney W. Grimes 	lookup_still_valid = TRUE;
206df8bae1dSRodney W. Grimes 
207df8bae1dSRodney W. Grimes 	if (wired)
208df8bae1dSRodney W. Grimes 		fault_type = prot;
209df8bae1dSRodney W. Grimes 
210df8bae1dSRodney W. Grimes 	first_m = NULL;
211df8bae1dSRodney W. Grimes 
212df8bae1dSRodney W. Grimes 	/*
2130d94caffSDavid Greenman 	 * Make a reference to this object to prevent its disposal while we
2140d94caffSDavid Greenman 	 * are messing with it.  Once we have the reference, the map is free
2150d94caffSDavid Greenman 	 * to be diddled.  Since objects reference their shadows (and copies),
2160d94caffSDavid Greenman 	 * they will stay around as well.
217df8bae1dSRodney W. Grimes 	 */
218df8bae1dSRodney W. Grimes 
219df8bae1dSRodney W. Grimes 	first_object->ref_count++;
220df8bae1dSRodney W. Grimes 	first_object->paging_in_progress++;
221df8bae1dSRodney W. Grimes 
222df8bae1dSRodney W. Grimes 	/*
223df8bae1dSRodney W. Grimes 	 * INVARIANTS (through entire routine):
224df8bae1dSRodney W. Grimes 	 *
2250d94caffSDavid Greenman 	 * 1)	At all times, we must either have the object lock or a busy
22624a1cce3SDavid Greenman 	 * page in some object to prevent some other process from trying to
2270d94caffSDavid Greenman 	 * bring in the same page.
228df8bae1dSRodney W. Grimes 	 *
2290d94caffSDavid Greenman 	 * Note that we cannot hold any locks during the pager access or when
2300d94caffSDavid Greenman 	 * waiting for memory, so we use a busy page then.
231df8bae1dSRodney W. Grimes 	 *
2320d94caffSDavid Greenman 	 * Note also that we aren't as concerned about more than one thead
2330d94caffSDavid Greenman 	 * attempting to pager_data_unlock the same page at once, so we don't
2340d94caffSDavid Greenman 	 * hold the page as busy then, but do record the highest unlock value
2350d94caffSDavid Greenman 	 * so far.  [Unlock requests may also be delivered out of order.]
236df8bae1dSRodney W. Grimes 	 *
2370d94caffSDavid Greenman 	 * 2)	Once we have a busy page, we must remove it from the pageout
2380d94caffSDavid Greenman 	 * queues, so that the pageout daemon will not grab it away.
239df8bae1dSRodney W. Grimes 	 *
24024a1cce3SDavid Greenman 	 * 3)	To prevent another process from racing us down the shadow chain
2410d94caffSDavid Greenman 	 * and entering a new page in the top object before we do, we must
2420d94caffSDavid Greenman 	 * keep a busy page in the top object while following the shadow
2430d94caffSDavid Greenman 	 * chain.
244df8bae1dSRodney W. Grimes 	 *
2450d94caffSDavid Greenman 	 * 4)	We must increment paging_in_progress on any object for which
2460d94caffSDavid Greenman 	 * we have a busy page, to prevent vm_object_collapse from removing
2470d94caffSDavid Greenman 	 * the busy page without our noticing.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	/*
251df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
252df8bae1dSRodney W. Grimes 	 */
253df8bae1dSRodney W. Grimes 
254df8bae1dSRodney W. Grimes 	object = first_object;
255a316d390SJohn Dyson 	pindex = first_pindex;
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	/*
258df8bae1dSRodney W. Grimes 	 * See whether this page is resident
259df8bae1dSRodney W. Grimes 	 */
260df8bae1dSRodney W. Grimes 
261df8bae1dSRodney W. Grimes 	while (TRUE) {
262a316d390SJohn Dyson 		m = vm_page_lookup(object, pindex);
263df8bae1dSRodney W. Grimes 		if (m != NULL) {
264c82b0181SJohn Dyson 			int queue;
265df8bae1dSRodney W. Grimes 			/*
2660d94caffSDavid Greenman 			 * If the page is being brought in, wait for it and
2670d94caffSDavid Greenman 			 * then retry.
268df8bae1dSRodney W. Grimes 			 */
2690d94caffSDavid Greenman 			if ((m->flags & PG_BUSY) || m->busy) {
27016f62314SDavid Greenman 				int s;
2710d94caffSDavid Greenman 
272df8bae1dSRodney W. Grimes 				UNLOCK_THINGS;
273b18bfc3dSJohn Dyson 				s = splvm();
274b18bfc3dSJohn Dyson 				if (((m->flags & PG_BUSY) || m->busy)) {
2750d94caffSDavid Greenman 					m->flags |= PG_WANTED | PG_REFERENCED;
276976e77fcSDavid Greenman 					cnt.v_intrans++;
27724a1cce3SDavid Greenman 					tsleep(m, PSWP, "vmpfw", 0);
27826f9a767SRodney W. Grimes 				}
27916f62314SDavid Greenman 				splx(s);
280df8bae1dSRodney W. Grimes 				vm_object_deallocate(first_object);
281df8bae1dSRodney W. Grimes 				goto RetryFault;
282df8bae1dSRodney W. Grimes 			}
283f6b04d2bSDavid Greenman 
284c82b0181SJohn Dyson 			queue = m->queue;
285c82b0181SJohn Dyson 			vm_page_unqueue_nowakeup(m);
286c82b0181SJohn Dyson 
287df8bae1dSRodney W. Grimes 			/*
28824a1cce3SDavid Greenman 			 * Mark page busy for other processes, and the pagedaemon.
289df8bae1dSRodney W. Grimes 			 */
290c82b0181SJohn Dyson 			if ((queue == PQ_CACHE) &&
291886d3e11SJohn Dyson 			    (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) {
292c82b0181SJohn Dyson 				vm_page_activate(m);
29322ba64e8SJohn Dyson 				UNLOCK_AND_DEALLOCATE;
29422ba64e8SJohn Dyson 				VM_WAIT;
29522ba64e8SJohn Dyson 				goto RetryFault;
29622ba64e8SJohn Dyson 			}
29722ba64e8SJohn Dyson 
298886d3e11SJohn Dyson 			m->flags |= PG_BUSY;
2990ed43762SJohn Dyson 
300bd7e5f99SJohn Dyson 			if (m->valid &&
301bd7e5f99SJohn Dyson 				((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
302f919ebdeSDavid Greenman 				m->object != kernel_object && m->object != kmem_object) {
3030d94caffSDavid Greenman 				goto readrest;
3040d94caffSDavid Greenman 			}
305df8bae1dSRodney W. Grimes 			break;
306df8bae1dSRodney W. Grimes 		}
30724a1cce3SDavid Greenman 		if (((object->type != OBJT_DEFAULT) && (!change_wiring || wired))
308df8bae1dSRodney W. Grimes 		    || (object == first_object)) {
309df8bae1dSRodney W. Grimes 
310a316d390SJohn Dyson 			if (pindex >= object->size) {
3115f55e841SDavid Greenman 				UNLOCK_AND_DEALLOCATE;
3125f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
3135f55e841SDavid Greenman 			}
31422ba64e8SJohn Dyson 
315df8bae1dSRodney W. Grimes 			/*
3160d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
317df8bae1dSRodney W. Grimes 			 */
318a316d390SJohn Dyson 			m = vm_page_alloc(object, pindex,
319b18bfc3dSJohn Dyson 				(vp || object->backing_object)?VM_ALLOC_NORMAL:VM_ALLOC_ZERO);
320df8bae1dSRodney W. Grimes 
321df8bae1dSRodney W. Grimes 			if (m == NULL) {
322df8bae1dSRodney W. Grimes 				UNLOCK_AND_DEALLOCATE;
323df8bae1dSRodney W. Grimes 				VM_WAIT;
324df8bae1dSRodney W. Grimes 				goto RetryFault;
325df8bae1dSRodney W. Grimes 			}
326df8bae1dSRodney W. Grimes 		}
3270d94caffSDavid Greenman readrest:
32824a1cce3SDavid Greenman 		if (object->type != OBJT_DEFAULT && (!change_wiring || wired)) {
329df8bae1dSRodney W. Grimes 			int rv;
33026f9a767SRodney W. Grimes 			int faultcount;
33126f9a767SRodney W. Grimes 			int reqpage;
332867a482dSJohn Dyson 			int ahead, behind;
333867a482dSJohn Dyson 
334867a482dSJohn Dyson 			ahead = VM_FAULT_READ_AHEAD;
335867a482dSJohn Dyson 			behind = VM_FAULT_READ_BEHIND;
336867a482dSJohn Dyson 			if (first_object->behavior == OBJ_RANDOM) {
337867a482dSJohn Dyson 				ahead = 0;
338867a482dSJohn Dyson 				behind = 0;
339867a482dSJohn Dyson 			}
340867a482dSJohn Dyson 
341ff97964aSJohn Dyson 			if ((first_object->type != OBJT_DEVICE) &&
342ff97964aSJohn Dyson 				(first_object->behavior == OBJ_SEQUENTIAL)) {
343867a482dSJohn Dyson 				vm_pindex_t firstpindex, tmppindex;
344867a482dSJohn Dyson 				if (first_pindex <
345867a482dSJohn Dyson 					2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1))
346867a482dSJohn Dyson 					firstpindex = 0;
347867a482dSJohn Dyson 				else
348867a482dSJohn Dyson 					firstpindex = first_pindex -
349867a482dSJohn Dyson 						2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1);
350867a482dSJohn Dyson 
351867a482dSJohn Dyson 				for(tmppindex = first_pindex - 1;
352867a482dSJohn Dyson 					tmppindex >= first_pindex;
353867a482dSJohn Dyson 					--tmppindex) {
354867a482dSJohn Dyson 					vm_page_t mt;
355867a482dSJohn Dyson 					mt = vm_page_lookup( first_object, tmppindex);
356867a482dSJohn Dyson 					if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
357867a482dSJohn Dyson 						break;
358ff97964aSJohn Dyson 					if (mt->busy ||
359ff97964aSJohn Dyson 						(mt->flags & (PG_BUSY|PG_FICTITIOUS)) ||
360ff97964aSJohn Dyson 						mt->hold_count ||
361867a482dSJohn Dyson 						mt->wire_count)
362867a482dSJohn Dyson 						continue;
363867a482dSJohn Dyson 					if (mt->dirty == 0)
364867a482dSJohn Dyson 						vm_page_test_dirty(mt);
365867a482dSJohn Dyson 					if (mt->dirty) {
366867a482dSJohn Dyson 						vm_page_protect(mt, VM_PROT_NONE);
367867a482dSJohn Dyson 						vm_page_deactivate(mt);
368867a482dSJohn Dyson 					} else {
369867a482dSJohn Dyson 						vm_page_cache(mt);
370867a482dSJohn Dyson 					}
371867a482dSJohn Dyson 				}
372867a482dSJohn Dyson 
373867a482dSJohn Dyson 				ahead += behind;
374867a482dSJohn Dyson 				behind = 0;
375867a482dSJohn Dyson 			}
376df8bae1dSRodney W. Grimes 
377df8bae1dSRodney W. Grimes 			/*
3780d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
3790d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
3800d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
3810d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
3820d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
3830d94caffSDavid Greenman 			 * array "marray" returned contains an array of
3840d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
3850d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
3860d94caffSDavid Greenman 			 * return value is the index into the marray for the
3870d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
38826f9a767SRodney W. Grimes 			 */
38905f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
390867a482dSJohn Dyson 			    m, behind, ahead, marray, &reqpage);
391df8bae1dSRodney W. Grimes 
392df8bae1dSRodney W. Grimes 			/*
3930d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
3940d94caffSDavid Greenman 			 * releasing the lock on the map.
395df8bae1dSRodney W. Grimes 			 */
396df8bae1dSRodney W. Grimes 			UNLOCK_MAP;
397df8bae1dSRodney W. Grimes 
39826f9a767SRodney W. Grimes 			rv = faultcount ?
39924a1cce3SDavid Greenman 			    vm_pager_get_pages(object, marray, faultcount,
40024a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
40122ba64e8SJohn Dyson 
40226f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
403df8bae1dSRodney W. Grimes 				/*
4040d94caffSDavid Greenman 				 * Found the page. Leave it busy while we play
4050d94caffSDavid Greenman 				 * with it.
406df8bae1dSRodney W. Grimes 				 */
40726f9a767SRodney W. Grimes 
408df8bae1dSRodney W. Grimes 				/*
4090d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
4100d94caffSDavid Greenman 				 * is responsible for disposition of old page
4110d94caffSDavid Greenman 				 * if moved.
412df8bae1dSRodney W. Grimes 				 */
413a316d390SJohn Dyson 				m = vm_page_lookup(object, pindex);
414f6b04d2bSDavid Greenman 				if( !m) {
415f6b04d2bSDavid Greenman 					UNLOCK_AND_DEALLOCATE;
416f6b04d2bSDavid Greenman 					goto RetryFault;
417f6b04d2bSDavid Greenman 				}
418f6b04d2bSDavid Greenman 
41926f9a767SRodney W. Grimes 				hardfault++;
420df8bae1dSRodney W. Grimes 				break;
421df8bae1dSRodney W. Grimes 			}
422df8bae1dSRodney W. Grimes 			/*
4230d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
4240d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
4250d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
426df8bae1dSRodney W. Grimes 			 *
42724a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
4280d94caffSDavid Greenman 			 * in this page.
429df8bae1dSRodney W. Grimes 			 *
4300d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
43124a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
4320d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
4330d94caffSDavid Greenman 			 * the same time that we are.
434df8bae1dSRodney W. Grimes 			 */
43526f9a767SRodney W. Grimes 
436a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
437a83c285cSDavid Greenman 				printf("vm_fault: pager input (probably hardware) error, PID %d failure\n",
438a83c285cSDavid Greenman 				    curproc->p_pid);
43926f9a767SRodney W. Grimes 			/*
440a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
44126f9a767SRodney W. Grimes 			 */
442a83c285cSDavid Greenman 			/*
4430d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
4440d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
4450d94caffSDavid Greenman 			 * fault w/ I/O error.
446a83c285cSDavid Greenman 			 */
447a83c285cSDavid Greenman 			if (((map != kernel_map) && (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
44826f9a767SRodney W. Grimes 				FREE_PAGE(m);
44926f9a767SRodney W. Grimes 				UNLOCK_AND_DEALLOCATE;
450a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
45126f9a767SRodney W. Grimes 			}
452df8bae1dSRodney W. Grimes 			if (object != first_object) {
453df8bae1dSRodney W. Grimes 				FREE_PAGE(m);
45426f9a767SRodney W. Grimes 				/*
45526f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
45626f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
45726f9a767SRodney W. Grimes 				 */
458df8bae1dSRodney W. Grimes 			}
459df8bae1dSRodney W. Grimes 		}
460df8bae1dSRodney W. Grimes 		/*
46124a1cce3SDavid Greenman 		 * We get here if the object has default pager (or unwiring) or the
4620d94caffSDavid Greenman 		 * pager doesn't have the page.
463df8bae1dSRodney W. Grimes 		 */
464df8bae1dSRodney W. Grimes 		if (object == first_object)
465df8bae1dSRodney W. Grimes 			first_m = m;
466df8bae1dSRodney W. Grimes 
467df8bae1dSRodney W. Grimes 		/*
4680d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
4690d94caffSDavid Greenman 		 * unlocking the current one.
470df8bae1dSRodney W. Grimes 		 */
471df8bae1dSRodney W. Grimes 
472a316d390SJohn Dyson 		pindex += OFF_TO_IDX(object->backing_object_offset);
47324a1cce3SDavid Greenman 		next_object = object->backing_object;
474df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
475df8bae1dSRodney W. Grimes 			/*
4760d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
4770d94caffSDavid Greenman 			 * object with zeros.
478df8bae1dSRodney W. Grimes 			 */
479df8bae1dSRodney W. Grimes 			if (object != first_object) {
480f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
481df8bae1dSRodney W. Grimes 
482df8bae1dSRodney W. Grimes 				object = first_object;
483a316d390SJohn Dyson 				pindex = first_pindex;
484df8bae1dSRodney W. Grimes 				m = first_m;
485df8bae1dSRodney W. Grimes 			}
486df8bae1dSRodney W. Grimes 			first_m = NULL;
487df8bae1dSRodney W. Grimes 
488f70f05f2SJohn Dyson 			if ((m->flags & PG_ZERO) == 0)
489df8bae1dSRodney W. Grimes 				vm_page_zero_fill(m);
490df8bae1dSRodney W. Grimes 			cnt.v_zfod++;
491df8bae1dSRodney W. Grimes 			break;
4920d94caffSDavid Greenman 		} else {
49326f9a767SRodney W. Grimes 			if (object != first_object) {
494f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
495c0503609SDavid Greenman 			}
496df8bae1dSRodney W. Grimes 			object = next_object;
497df8bae1dSRodney W. Grimes 			object->paging_in_progress++;
498df8bae1dSRodney W. Grimes 		}
499df8bae1dSRodney W. Grimes 	}
500df8bae1dSRodney W. Grimes 
501f919ebdeSDavid Greenman 	if ((m->flags & PG_BUSY) == 0)
502f919ebdeSDavid Greenman 		panic("vm_fault: not busy after main loop");
503df8bae1dSRodney W. Grimes 
504df8bae1dSRodney W. Grimes 	/*
5050d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
506df8bae1dSRodney W. Grimes 	 * is held.]
507df8bae1dSRodney W. Grimes 	 */
508df8bae1dSRodney W. Grimes 
509df8bae1dSRodney W. Grimes 	old_m = m;	/* save page that would be copied */
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 	/*
5120d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
5130d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
5140d94caffSDavid Greenman 	 * top-level object.
515df8bae1dSRodney W. Grimes 	 */
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 	if (object != first_object) {
518df8bae1dSRodney W. Grimes 		/*
5190d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
520df8bae1dSRodney W. Grimes 		 */
521df8bae1dSRodney W. Grimes 
522df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
523df8bae1dSRodney W. Grimes 
524df8bae1dSRodney W. Grimes 			/*
5250d94caffSDavid Greenman 			 * We already have an empty page in first_object - use
5260d94caffSDavid Greenman 			 * it.
527df8bae1dSRodney W. Grimes 			 */
528df8bae1dSRodney W. Grimes 
529de5f6a77SJohn Dyson 			if (lookup_still_valid &&
530de5f6a77SJohn Dyson 				/*
531de5f6a77SJohn Dyson 				 * Only one shadow object
532de5f6a77SJohn Dyson 				 */
533de5f6a77SJohn Dyson 				(object->shadow_count == 1) &&
534de5f6a77SJohn Dyson 				/*
535de5f6a77SJohn Dyson 				 * No COW refs, except us
536de5f6a77SJohn Dyson 				 */
537de5f6a77SJohn Dyson 				(object->ref_count == 1) &&
538de5f6a77SJohn Dyson 				/*
539de5f6a77SJohn Dyson 				 * Noone else can look this object up
540de5f6a77SJohn Dyson 				 */
541de5f6a77SJohn Dyson 				(object->handle == NULL) &&
542de5f6a77SJohn Dyson 				/*
543de5f6a77SJohn Dyson 				 * No other ways to look the object up
544de5f6a77SJohn Dyson 				 */
545de5f6a77SJohn Dyson 				((object->type == OBJT_DEFAULT) ||
546de5f6a77SJohn Dyson 				 (object->type == OBJT_SWAP)) &&
547de5f6a77SJohn Dyson 				/*
548de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
549de5f6a77SJohn Dyson 				 */
550de5f6a77SJohn Dyson 				(object == first_object->backing_object)) {
551df8bae1dSRodney W. Grimes 
552df8bae1dSRodney W. Grimes 				/*
553de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
554df8bae1dSRodney W. Grimes 				 */
555de5f6a77SJohn Dyson 				vm_page_protect(first_m, VM_PROT_NONE);
556de5f6a77SJohn Dyson 				PAGE_WAKEUP(first_m);
557de5f6a77SJohn Dyson 				vm_page_free(first_m);
558de5f6a77SJohn Dyson 				/*
559de5f6a77SJohn Dyson 				 * grab the page and put it into the process'es object
560de5f6a77SJohn Dyson 				 */
561de5f6a77SJohn Dyson 				vm_page_rename(m, first_object, first_pindex);
562de5f6a77SJohn Dyson 				first_m = m;
563de5f6a77SJohn Dyson 				m->dirty = VM_PAGE_BITS_ALL;
564de5f6a77SJohn Dyson 				m = NULL;
565de5f6a77SJohn Dyson 				++vm_fault_copy_save_1;
566de5f6a77SJohn Dyson 			} else {
567de5f6a77SJohn Dyson 				/*
568de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
569de5f6a77SJohn Dyson 				 */
570de5f6a77SJohn Dyson 				vm_page_copy(m, first_m);
571de5f6a77SJohn Dyson 			}
572df8bae1dSRodney W. Grimes 
573de5f6a77SJohn Dyson 			if (lookup_still_valid &&
574de5f6a77SJohn Dyson 				/*
575de5f6a77SJohn Dyson 				 * make sure that we have two shadow objs
576de5f6a77SJohn Dyson 				 */
577de5f6a77SJohn Dyson 				(object->shadow_count == 2) &&
578de5f6a77SJohn Dyson 				/*
579de5f6a77SJohn Dyson 				 * And no COW refs -- note that there are sometimes
580de5f6a77SJohn Dyson 				 * temp refs to objs, but ignore that case -- we just
581de5f6a77SJohn Dyson 				 * punt.
582de5f6a77SJohn Dyson 				 */
583de5f6a77SJohn Dyson 				(object->ref_count == 2) &&
584de5f6a77SJohn Dyson 				/*
585de5f6a77SJohn Dyson 				 * Noone else can look us up
586de5f6a77SJohn Dyson 				 */
587de5f6a77SJohn Dyson 				(object->handle == NULL) &&
588de5f6a77SJohn Dyson 				/*
589de5f6a77SJohn Dyson 				 * Not something that can be referenced elsewhere
590de5f6a77SJohn Dyson 				 */
591de5f6a77SJohn Dyson 				((object->type == OBJT_DEFAULT) ||
592de5f6a77SJohn Dyson 				 (object->type == OBJT_SWAP)) &&
593de5f6a77SJohn Dyson 				/*
594de5f6a77SJohn Dyson 				 * We don't bother chasing down object chain
595de5f6a77SJohn Dyson 				 */
596de5f6a77SJohn Dyson 				(object == first_object->backing_object)) {
597de5f6a77SJohn Dyson 
598de5f6a77SJohn Dyson 				vm_object_t other_object;
599de5f6a77SJohn Dyson 				vm_pindex_t other_pindex, other_pindex_offset;
600de5f6a77SJohn Dyson 				vm_page_t tm;
601de5f6a77SJohn Dyson 
602b18bfc3dSJohn Dyson 				other_object = TAILQ_FIRST(&object->shadow_head);
603de5f6a77SJohn Dyson 				if (other_object == first_object)
604b18bfc3dSJohn Dyson 					other_object = TAILQ_NEXT(other_object, shadow_list);
605de5f6a77SJohn Dyson 				if (!other_object)
606de5f6a77SJohn Dyson 					panic("vm_fault: other object missing");
607de5f6a77SJohn Dyson 				if (other_object &&
608de5f6a77SJohn Dyson 					(other_object->type == OBJT_DEFAULT) &&
609de5f6a77SJohn Dyson 					(other_object->paging_in_progress == 0)) {
610de5f6a77SJohn Dyson 					other_pindex_offset =
611de5f6a77SJohn Dyson 						OFF_TO_IDX(other_object->backing_object_offset);
612de5f6a77SJohn Dyson 					if (pindex >= other_pindex_offset) {
613de5f6a77SJohn Dyson 						other_pindex = pindex - other_pindex_offset;
614de5f6a77SJohn Dyson 						/*
615de5f6a77SJohn Dyson 						 * If the other object has the page, just free it.
616de5f6a77SJohn Dyson 						 */
617de5f6a77SJohn Dyson 						if ((tm = vm_page_lookup(other_object, other_pindex))) {
618de5f6a77SJohn Dyson 							if ((tm->flags & PG_BUSY) == 0 &&
619de5f6a77SJohn Dyson 								tm->busy == 0 &&
620de5f6a77SJohn Dyson 								tm->valid == VM_PAGE_BITS_ALL) {
621de5f6a77SJohn Dyson 								/*
622de5f6a77SJohn Dyson 								 * get rid of the unnecessary page
623de5f6a77SJohn Dyson 								 */
624de5f6a77SJohn Dyson 								vm_page_protect(m, VM_PROT_NONE);
625de5f6a77SJohn Dyson 								PAGE_WAKEUP(m);
626de5f6a77SJohn Dyson 								vm_page_free(m);
627de5f6a77SJohn Dyson 								m = NULL;
628de5f6a77SJohn Dyson 								++vm_fault_free_1;
629de5f6a77SJohn Dyson 								tm->dirty = VM_PAGE_BITS_ALL;
630de5f6a77SJohn Dyson 								first_m->dirty = VM_PAGE_BITS_ALL;
631de5f6a77SJohn Dyson 							}
632de5f6a77SJohn Dyson 						} else {
633de5f6a77SJohn Dyson 							/*
634de5f6a77SJohn Dyson 							 * If the other object doesn't have the page,
635de5f6a77SJohn Dyson 							 * then we move it there.
636de5f6a77SJohn Dyson 							 */
637de5f6a77SJohn Dyson 							vm_page_rename(m, other_object, other_pindex);
638de5f6a77SJohn Dyson 							m->dirty = VM_PAGE_BITS_ALL;
63965bc79b8SJohn Dyson 							m->valid = VM_PAGE_BITS_ALL;
640de5f6a77SJohn Dyson 							++vm_fault_copy_save_2;
641de5f6a77SJohn Dyson 						}
642de5f6a77SJohn Dyson 					}
643de5f6a77SJohn Dyson 				}
644de5f6a77SJohn Dyson 			}
645de5f6a77SJohn Dyson 
646de5f6a77SJohn Dyson 			if (m) {
647bd7e5f99SJohn Dyson 				if (m->queue != PQ_ACTIVE)
648df8bae1dSRodney W. Grimes 					vm_page_activate(m);
649df8bae1dSRodney W. Grimes 			/*
650df8bae1dSRodney W. Grimes 			 * We no longer need the old page or object.
651df8bae1dSRodney W. Grimes 			 */
652df8bae1dSRodney W. Grimes 				PAGE_WAKEUP(m);
653de5f6a77SJohn Dyson 			}
654df8bae1dSRodney W. Grimes 
655de5f6a77SJohn Dyson 			vm_object_pip_wakeup(object);
656df8bae1dSRodney W. Grimes 			/*
657df8bae1dSRodney W. Grimes 			 * Only use the new page below...
658df8bae1dSRodney W. Grimes 			 */
659df8bae1dSRodney W. Grimes 
660df8bae1dSRodney W. Grimes 			cnt.v_cow_faults++;
661df8bae1dSRodney W. Grimes 			m = first_m;
662df8bae1dSRodney W. Grimes 			object = first_object;
663a316d390SJohn Dyson 			pindex = first_pindex;
664df8bae1dSRodney W. Grimes 
665df8bae1dSRodney W. Grimes 			/*
6660d94caffSDavid Greenman 			 * Now that we've gotten the copy out of the way,
6670d94caffSDavid Greenman 			 * let's try to collapse the top object.
66824a1cce3SDavid Greenman 			 *
669df8bae1dSRodney W. Grimes 			 * But we have to play ugly games with
670df8bae1dSRodney W. Grimes 			 * paging_in_progress to do that...
671df8bae1dSRodney W. Grimes 			 */
672f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
673df8bae1dSRodney W. Grimes 			vm_object_collapse(object);
674df8bae1dSRodney W. Grimes 			object->paging_in_progress++;
6750d94caffSDavid Greenman 		} else {
676df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
677df8bae1dSRodney W. Grimes 		}
678df8bae1dSRodney W. Grimes 	}
679df8bae1dSRodney W. Grimes 
680df8bae1dSRodney W. Grimes 	/*
6810d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
6820d94caffSDavid Greenman 	 * lookup.
683df8bae1dSRodney W. Grimes 	 */
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes 	if (!lookup_still_valid) {
686df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
687a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
688df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 		/*
6910d94caffSDavid Greenman 		 * Since map entries may be pageable, make sure we can take a
6920d94caffSDavid Greenman 		 * page fault on them.
693df8bae1dSRodney W. Grimes 		 */
694df8bae1dSRodney W. Grimes 
695df8bae1dSRodney W. Grimes 		/*
69624a1cce3SDavid Greenman 		 * To avoid trying to write_lock the map while another process
6970d94caffSDavid Greenman 		 * has it read_locked (in vm_map_pageable), we do not try for
6980d94caffSDavid Greenman 		 * write permission.  If the page is still writable, we will
6990d94caffSDavid Greenman 		 * get write permission.  If it is not, or has been marked
7000d94caffSDavid Greenman 		 * needs_copy, we enter the mapping without write permission,
7010d94caffSDavid Greenman 		 * and will merely take another fault.
702df8bae1dSRodney W. Grimes 		 */
7030d94caffSDavid Greenman 		result = vm_map_lookup(&map, vaddr, fault_type & ~VM_PROT_WRITE,
704a316d390SJohn Dyson 		    &entry, &retry_object, &retry_pindex, &retry_prot, &wired, &su);
705df8bae1dSRodney W. Grimes 
706df8bae1dSRodney W. Grimes 		/*
7070d94caffSDavid Greenman 		 * If we don't need the page any longer, put it on the active
7080d94caffSDavid Greenman 		 * list (the easiest thing to do here).  If no one needs it,
7090d94caffSDavid Greenman 		 * pageout will grab it eventually.
710df8bae1dSRodney W. Grimes 		 */
711df8bae1dSRodney W. Grimes 
712df8bae1dSRodney W. Grimes 		if (result != KERN_SUCCESS) {
713df8bae1dSRodney W. Grimes 			RELEASE_PAGE(m);
714df8bae1dSRodney W. Grimes 			UNLOCK_AND_DEALLOCATE;
715df8bae1dSRodney W. Grimes 			return (result);
716df8bae1dSRodney W. Grimes 		}
717df8bae1dSRodney W. Grimes 		lookup_still_valid = TRUE;
718df8bae1dSRodney W. Grimes 
719df8bae1dSRodney W. Grimes 		if ((retry_object != first_object) ||
720a316d390SJohn Dyson 		    (retry_pindex != first_pindex)) {
721df8bae1dSRodney W. Grimes 			RELEASE_PAGE(m);
722df8bae1dSRodney W. Grimes 			UNLOCK_AND_DEALLOCATE;
723df8bae1dSRodney W. Grimes 			goto RetryFault;
724df8bae1dSRodney W. Grimes 		}
725df8bae1dSRodney W. Grimes 		/*
7260d94caffSDavid Greenman 		 * Check whether the protection has changed or the object has
7270d94caffSDavid Greenman 		 * been copied while we left the map unlocked. Changing from
7280d94caffSDavid Greenman 		 * read to write permission is OK - we leave the page
7290d94caffSDavid Greenman 		 * write-protected, and catch the write fault. Changing from
7300d94caffSDavid Greenman 		 * write to read permission means that we can't mark the page
7310d94caffSDavid Greenman 		 * write-enabled after all.
732df8bae1dSRodney W. Grimes 		 */
733df8bae1dSRodney W. Grimes 		prot &= retry_prot;
734df8bae1dSRodney W. Grimes 	}
735df8bae1dSRodney W. Grimes 
736df8bae1dSRodney W. Grimes 	/*
7370d94caffSDavid Greenman 	 * Put this page into the physical map. We had to do the unlock above
7380d94caffSDavid Greenman 	 * because pmap_enter may cause other faults.   We don't put the page
7390d94caffSDavid Greenman 	 * back on the active queue until later so that the page-out daemon
7400d94caffSDavid Greenman 	 * won't find us (yet).
741df8bae1dSRodney W. Grimes 	 */
742df8bae1dSRodney W. Grimes 
7432ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
744f919ebdeSDavid Greenman 		m->flags |= PG_WRITEABLE;
745aef922f5SJohn Dyson 		m->object->flags |= OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY;
7462ddba215SDavid Greenman 		/*
7472ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
7482ddba215SDavid Greenman 		 * written NOW. This will save on the pmap_is_modified() calls
7492ddba215SDavid Greenman 		 * later.
7502ddba215SDavid Greenman 		 */
7512ddba215SDavid Greenman 		if (fault_type & VM_PROT_WRITE) {
7522ddba215SDavid Greenman 			m->dirty = VM_PAGE_BITS_ALL;
7532ddba215SDavid Greenman 		}
7542ddba215SDavid Greenman 	}
755f6b04d2bSDavid Greenman 
75630dcfc09SJohn Dyson 	UNLOCK_THINGS;
75765bc79b8SJohn Dyson 	m->valid = VM_PAGE_BITS_ALL;
758ff97964aSJohn Dyson 	m->flags &= ~PG_ZERO;
759f919ebdeSDavid Greenman 
760df8bae1dSRodney W. Grimes 	pmap_enter(map->pmap, vaddr, VM_PAGE_TO_PHYS(m), prot, wired);
761b18bfc3dSJohn Dyson 	if ((change_wiring == 0) && (wired == 0))
7626d40c3d3SDavid Greenman 		pmap_prefault(map->pmap, vaddr, entry, first_object);
763df8bae1dSRodney W. Grimes 
764ff97964aSJohn Dyson 	m->flags |= PG_MAPPED|PG_REFERENCED;
765ff97964aSJohn Dyson 
766df8bae1dSRodney W. Grimes 	/*
7670d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
7680d94caffSDavid Greenman 	 * can find it.
769df8bae1dSRodney W. Grimes 	 */
770df8bae1dSRodney W. Grimes 	if (change_wiring) {
771df8bae1dSRodney W. Grimes 		if (wired)
772df8bae1dSRodney W. Grimes 			vm_page_wire(m);
773df8bae1dSRodney W. Grimes 		else
774df8bae1dSRodney W. Grimes 			vm_page_unwire(m);
7750d94caffSDavid Greenman 	} else {
776bd7e5f99SJohn Dyson 		if (m->queue != PQ_ACTIVE)
777df8bae1dSRodney W. Grimes 			vm_page_activate(m);
77826f9a767SRodney W. Grimes 	}
77926f9a767SRodney W. Grimes 
780a1f6d91cSDavid Greenman 	if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
78126f9a767SRodney W. Grimes 		if (hardfault) {
78226f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
78326f9a767SRodney W. Grimes 		} else {
78426f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
78526f9a767SRodney W. Grimes 		}
78626f9a767SRodney W. Grimes 	}
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	/*
789df8bae1dSRodney W. Grimes 	 * Unlock everything, and return
790df8bae1dSRodney W. Grimes 	 */
791df8bae1dSRodney W. Grimes 
792df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);
79330dcfc09SJohn Dyson 	vm_object_deallocate(first_object);
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
796df8bae1dSRodney W. Grimes 
797df8bae1dSRodney W. Grimes }
798df8bae1dSRodney W. Grimes 
799df8bae1dSRodney W. Grimes /*
800df8bae1dSRodney W. Grimes  *	vm_fault_wire:
801df8bae1dSRodney W. Grimes  *
802df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
803df8bae1dSRodney W. Grimes  */
804df8bae1dSRodney W. Grimes int
805df8bae1dSRodney W. Grimes vm_fault_wire(map, start, end)
806df8bae1dSRodney W. Grimes 	vm_map_t map;
807df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
808df8bae1dSRodney W. Grimes {
80926f9a767SRodney W. Grimes 
810df8bae1dSRodney W. Grimes 	register vm_offset_t va;
811df8bae1dSRodney W. Grimes 	register pmap_t pmap;
812df8bae1dSRodney W. Grimes 	int rv;
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
815df8bae1dSRodney W. Grimes 
816df8bae1dSRodney W. Grimes 	/*
8170d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
8180d94caffSDavid Greenman 	 * not fault, so that page tables and such can be locked down as well.
819df8bae1dSRodney W. Grimes 	 */
820df8bae1dSRodney W. Grimes 
821df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, FALSE);
822df8bae1dSRodney W. Grimes 
823df8bae1dSRodney W. Grimes 	/*
8240d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
8250d94caffSDavid Greenman 	 * map.
826df8bae1dSRodney W. Grimes 	 */
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
8296d40c3d3SDavid Greenman 
830f35329acSJohn Dyson /*
831a1f6d91cSDavid Greenman 		while( curproc != pageproc &&
832b18bfc3dSJohn Dyson 			(cnt.v_free_count <= cnt.v_pageout_free_min)) {
8336d40c3d3SDavid Greenman 			VM_WAIT;
834b18bfc3dSJohn Dyson 		}
835f35329acSJohn Dyson */
8366d40c3d3SDavid Greenman 
837a1f6d91cSDavid Greenman 		rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, TRUE);
838df8bae1dSRodney W. Grimes 		if (rv) {
839df8bae1dSRodney W. Grimes 			if (va != start)
840df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, start, va);
841df8bae1dSRodney W. Grimes 			return (rv);
842df8bae1dSRodney W. Grimes 		}
843df8bae1dSRodney W. Grimes 	}
844df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
845df8bae1dSRodney W. Grimes }
846df8bae1dSRodney W. Grimes 
847df8bae1dSRodney W. Grimes 
848df8bae1dSRodney W. Grimes /*
849df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
850df8bae1dSRodney W. Grimes  *
851df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
852df8bae1dSRodney W. Grimes  */
85326f9a767SRodney W. Grimes void
85426f9a767SRodney W. Grimes vm_fault_unwire(map, start, end)
855df8bae1dSRodney W. Grimes 	vm_map_t map;
856df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
857df8bae1dSRodney W. Grimes {
858df8bae1dSRodney W. Grimes 
859df8bae1dSRodney W. Grimes 	register vm_offset_t va, pa;
860df8bae1dSRodney W. Grimes 	register pmap_t pmap;
861df8bae1dSRodney W. Grimes 
862df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
863df8bae1dSRodney W. Grimes 
864df8bae1dSRodney W. Grimes 	/*
8650d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
8660d94caffSDavid Greenman 	 * mappings from the physical map system.
867df8bae1dSRodney W. Grimes 	 */
868df8bae1dSRodney W. Grimes 
869df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
870df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
871b18bfc3dSJohn Dyson 		if (pa != (vm_offset_t) 0) {
872df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
873df8bae1dSRodney W. Grimes 			vm_page_unwire(PHYS_TO_VM_PAGE(pa));
874df8bae1dSRodney W. Grimes 		}
875b18bfc3dSJohn Dyson 	}
876df8bae1dSRodney W. Grimes 
877df8bae1dSRodney W. Grimes 	/*
8780d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
8790d94caffSDavid Greenman 	 * fault, so that page tables and such may be unwired themselves.
880df8bae1dSRodney W. Grimes 	 */
881df8bae1dSRodney W. Grimes 
882df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, TRUE);
883df8bae1dSRodney W. Grimes 
884df8bae1dSRodney W. Grimes }
885df8bae1dSRodney W. Grimes 
886df8bae1dSRodney W. Grimes /*
887df8bae1dSRodney W. Grimes  *	Routine:
888df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
889df8bae1dSRodney W. Grimes  *	Function:
890df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
891df8bae1dSRodney W. Grimes  *
892df8bae1dSRodney W. Grimes  *	In/out conditions:
893df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
894df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
895df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
896df8bae1dSRodney W. Grimes  */
897df8bae1dSRodney W. Grimes 
89826f9a767SRodney W. Grimes void
89926f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
900df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
901df8bae1dSRodney W. Grimes 	vm_map_t src_map;
902df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
903df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
904df8bae1dSRodney W. Grimes {
905df8bae1dSRodney W. Grimes 	vm_object_t dst_object;
906df8bae1dSRodney W. Grimes 	vm_object_t src_object;
907a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
908a316d390SJohn Dyson 	vm_ooffset_t src_offset;
909df8bae1dSRodney W. Grimes 	vm_prot_t prot;
910df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
911df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
912df8bae1dSRodney W. Grimes 	vm_page_t src_m;
913df8bae1dSRodney W. Grimes 
914df8bae1dSRodney W. Grimes #ifdef	lint
915df8bae1dSRodney W. Grimes 	src_map++;
9160d94caffSDavid Greenman #endif	/* lint */
917df8bae1dSRodney W. Grimes 
918df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
919df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
920df8bae1dSRodney W. Grimes 
921df8bae1dSRodney W. Grimes 	/*
9220d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
9230d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
924df8bae1dSRodney W. Grimes 	 */
92524a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
926a316d390SJohn Dyson 	    (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
927df8bae1dSRodney W. Grimes 
928df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
929df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
930df8bae1dSRodney W. Grimes 
931df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
932df8bae1dSRodney W. Grimes 
933df8bae1dSRodney W. Grimes 	/*
9340d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
9350d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
9360d94caffSDavid Greenman 	 * object.
937df8bae1dSRodney W. Grimes 	 */
938df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
939df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
940df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
941df8bae1dSRodney W. Grimes 
942df8bae1dSRodney W. Grimes 		/*
943df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
944df8bae1dSRodney W. Grimes 		 */
945df8bae1dSRodney W. Grimes 		do {
946a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
947a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
948df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
949df8bae1dSRodney W. Grimes 				VM_WAIT;
950df8bae1dSRodney W. Grimes 			}
951df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
952df8bae1dSRodney W. Grimes 
953df8bae1dSRodney W. Grimes 		/*
954df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
9550d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
9560d94caffSDavid Greenman 		 * memory.)
957df8bae1dSRodney W. Grimes 		 */
958a316d390SJohn Dyson 		src_m = vm_page_lookup(src_object,
959a316d390SJohn Dyson 			OFF_TO_IDX(dst_offset + src_offset));
960df8bae1dSRodney W. Grimes 		if (src_m == NULL)
961df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
962df8bae1dSRodney W. Grimes 
963df8bae1dSRodney W. Grimes 		vm_page_copy(src_m, dst_m);
964df8bae1dSRodney W. Grimes 
965df8bae1dSRodney W. Grimes 		/*
966df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
967df8bae1dSRodney W. Grimes 		 */
968df8bae1dSRodney W. Grimes 
969ced399eeSJohn Dyson 		dst_m->flags |= PG_WRITEABLE|PG_MAPPED;
970ccbb2f72SJohn Dyson 		dst_m->flags &= ~PG_ZERO;
971df8bae1dSRodney W. Grimes 		pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m),
972df8bae1dSRodney W. Grimes 		    prot, FALSE);
973df8bae1dSRodney W. Grimes 
974df8bae1dSRodney W. Grimes 		/*
975df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
976df8bae1dSRodney W. Grimes 		 */
977df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
978df8bae1dSRodney W. Grimes 		PAGE_WAKEUP(dst_m);
979df8bae1dSRodney W. Grimes 	}
980df8bae1dSRodney W. Grimes }
98126f9a767SRodney W. Grimes 
98226f9a767SRodney W. Grimes 
98326f9a767SRodney W. Grimes /*
98426f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
98522ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
98622ba64e8SJohn Dyson  * pages for the pages to be paged in.
98726f9a767SRodney W. Grimes  *
98826f9a767SRodney W. Grimes  * Inputs:
98922ba64e8SJohn Dyson  *	m, rbehind, rahead
99026f9a767SRodney W. Grimes  *
99126f9a767SRodney W. Grimes  * Outputs:
99226f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
99326f9a767SRodney W. Grimes  *
99426f9a767SRodney W. Grimes  * Return value:
99526f9a767SRodney W. Grimes  *  number of pages in marray
99626f9a767SRodney W. Grimes  */
99726f9a767SRodney W. Grimes int
99822ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
99926f9a767SRodney W. Grimes 	vm_page_t m;
100026f9a767SRodney W. Grimes 	int rbehind;
100122ba64e8SJohn Dyson 	int rahead;
100226f9a767SRodney W. Grimes 	vm_page_t *marray;
100326f9a767SRodney W. Grimes 	int *reqpage;
100426f9a767SRodney W. Grimes {
100526f9a767SRodney W. Grimes 	int i;
100626f9a767SRodney W. Grimes 	vm_object_t object;
1007a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
1008a316d390SJohn Dyson 	vm_offset_t size;
100926f9a767SRodney W. Grimes 	vm_page_t rtm;
101026f9a767SRodney W. Grimes 	int treqpage;
1011170db9c6SJohn Dyson 	int cbehind, cahead;
101226f9a767SRodney W. Grimes 
101326f9a767SRodney W. Grimes 	object = m->object;
1014a316d390SJohn Dyson 	pindex = m->pindex;
101526f9a767SRodney W. Grimes 
101626f9a767SRodney W. Grimes 	/*
1017f35329acSJohn Dyson 	 * we don't fault-ahead for device pager
1018f35329acSJohn Dyson 	 */
1019f35329acSJohn Dyson 	if (object->type == OBJT_DEVICE) {
1020f35329acSJohn Dyson 		*reqpage = 0;
1021f35329acSJohn Dyson 		marray[0] = m;
1022f35329acSJohn Dyson 		return 1;
1023f35329acSJohn Dyson 	}
1024f35329acSJohn Dyson 
1025f35329acSJohn Dyson 	/*
102626f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
102726f9a767SRodney W. Grimes 	 */
102826f9a767SRodney W. Grimes 
1029170db9c6SJohn Dyson 	if (!vm_pager_has_page(object,
1030a316d390SJohn Dyson 		OFF_TO_IDX(object->paging_offset) + pindex, &cbehind, &cahead))
103126f9a767SRodney W. Grimes 		return 0;
103226f9a767SRodney W. Grimes 
103322ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
103422ba64e8SJohn Dyson 		*reqpage = 0;
103522ba64e8SJohn Dyson 		marray[0] = m;
103622ba64e8SJohn Dyson 		return 1;
1037170db9c6SJohn Dyson 	}
103822ba64e8SJohn Dyson 
103922ba64e8SJohn Dyson 	if (rahead > cahead) {
104022ba64e8SJohn Dyson 		rahead = cahead;
104122ba64e8SJohn Dyson 	}
104222ba64e8SJohn Dyson 
1043170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1044170db9c6SJohn Dyson 		rbehind = cbehind;
1045170db9c6SJohn Dyson 	}
1046170db9c6SJohn Dyson 
104726f9a767SRodney W. Grimes 	/*
104826f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
104926f9a767SRodney W. Grimes 	 */
1050ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
105122ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1052f919ebdeSDavid Greenman 		pagedaemon_wakeup();
105326f9a767SRodney W. Grimes 		*reqpage = 0;
105426f9a767SRodney W. Grimes 		marray[0] = m;
105526f9a767SRodney W. Grimes 		return 1;
105626f9a767SRodney W. Grimes 	}
105722ba64e8SJohn Dyson 
105826f9a767SRodney W. Grimes 	/*
10590d94caffSDavid Greenman 	 * scan backward for the read behind pages -- in memory or on disk not
10600d94caffSDavid Greenman 	 * in same object
106126f9a767SRodney W. Grimes 	 */
1062a316d390SJohn Dyson 	tpindex = pindex - 1;
1063a316d390SJohn Dyson 	if (tpindex < pindex) {
1064a316d390SJohn Dyson 		if (rbehind > pindex)
1065a316d390SJohn Dyson 			rbehind = pindex;
1066a316d390SJohn Dyson 		startpindex = pindex - rbehind;
1067a316d390SJohn Dyson 		while (tpindex >= startpindex) {
1068a316d390SJohn Dyson 			if (vm_page_lookup( object, tpindex)) {
1069a316d390SJohn Dyson 				startpindex = tpindex + 1;
107026f9a767SRodney W. Grimes 				break;
107126f9a767SRodney W. Grimes 			}
1072a316d390SJohn Dyson 			if (tpindex == 0)
107326f9a767SRodney W. Grimes 				break;
1074a316d390SJohn Dyson 			tpindex -= 1;
107526f9a767SRodney W. Grimes 		}
1076317205caSDavid Greenman 	} else {
1077a316d390SJohn Dyson 		startpindex = pindex;
1078317205caSDavid Greenman 	}
107926f9a767SRodney W. Grimes 
108026f9a767SRodney W. Grimes 	/*
10810d94caffSDavid Greenman 	 * scan forward for the read ahead pages -- in memory or on disk not
10820d94caffSDavid Greenman 	 * in same object
108326f9a767SRodney W. Grimes 	 */
1084a316d390SJohn Dyson 	tpindex = pindex + 1;
1085a316d390SJohn Dyson 	endpindex = pindex + (rahead + 1);
1086a316d390SJohn Dyson 	if (endpindex > object->size)
1087a316d390SJohn Dyson 		endpindex = object->size;
1088a316d390SJohn Dyson 	while (tpindex <  endpindex) {
1089a316d390SJohn Dyson 		if ( vm_page_lookup(object, tpindex)) {
109026f9a767SRodney W. Grimes 			break;
109126f9a767SRodney W. Grimes 		}
1092a316d390SJohn Dyson 		tpindex += 1;
109326f9a767SRodney W. Grimes 	}
1094a316d390SJohn Dyson 	endpindex = tpindex;
109526f9a767SRodney W. Grimes 
109626f9a767SRodney W. Grimes 	/* calculate number of bytes of pages */
1097a316d390SJohn Dyson 	size = endpindex - startpindex;
109826f9a767SRodney W. Grimes 
109926f9a767SRodney W. Grimes 	/* calculate the page offset of the required page */
1100a316d390SJohn Dyson 	treqpage = pindex - startpindex;
110126f9a767SRodney W. Grimes 
110226f9a767SRodney W. Grimes 	/* see if we have space (again) */
110322ba64e8SJohn Dyson 	if ((cnt.v_free_count + cnt.v_cache_count) >
110422ba64e8SJohn Dyson 		(cnt.v_free_reserved + size)) {
110526f9a767SRodney W. Grimes 		/*
110626f9a767SRodney W. Grimes 		 * get our pages and don't block for them
110726f9a767SRodney W. Grimes 		 */
110826f9a767SRodney W. Grimes 		for (i = 0; i < size; i++) {
1109170db9c6SJohn Dyson 			if (i != treqpage) {
1110ccbb2f72SJohn Dyson 				rtm = vm_page_alloc(object,
1111a316d390SJohn Dyson 					startpindex + i,
111222ba64e8SJohn Dyson 					VM_ALLOC_NORMAL);
1113ccbb2f72SJohn Dyson 				if (rtm == NULL) {
1114170db9c6SJohn Dyson 					if (i < treqpage) {
1115ccbb2f72SJohn Dyson 						int j;
1116ccbb2f72SJohn Dyson 						for (j = 0; j < i; j++) {
111721bf3904SJohn Dyson 							FREE_PAGE(marray[j]);
111826f9a767SRodney W. Grimes 						}
111926f9a767SRodney W. Grimes 						*reqpage = 0;
112026f9a767SRodney W. Grimes 						marray[0] = m;
112126f9a767SRodney W. Grimes 						return 1;
1122ccbb2f72SJohn Dyson 					} else {
1123ccbb2f72SJohn Dyson 						size = i;
1124ccbb2f72SJohn Dyson 						*reqpage = treqpage;
1125ccbb2f72SJohn Dyson 						return size;
1126ccbb2f72SJohn Dyson 					}
1127ccbb2f72SJohn Dyson 				}
1128ccbb2f72SJohn Dyson 				marray[i] = rtm;
1129ccbb2f72SJohn Dyson 			} else {
1130ccbb2f72SJohn Dyson 				marray[i] = m;
1131ccbb2f72SJohn Dyson 			}
113226f9a767SRodney W. Grimes 		}
1133170db9c6SJohn Dyson 
113426f9a767SRodney W. Grimes 		*reqpage = treqpage;
113526f9a767SRodney W. Grimes 		return size;
113626f9a767SRodney W. Grimes 	}
113726f9a767SRodney W. Grimes 	*reqpage = 0;
113826f9a767SRodney W. Grimes 	marray[0] = m;
113926f9a767SRodney W. Grimes 	return 1;
114026f9a767SRodney W. Grimes }
1141