xref: /freebsd/sys/vm/vm_fault.c (revision bd7e5f992efa91e837b384122c22470815b2be16)
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  *
69bd7e5f99SJohn Dyson  * $Id: vm_fault.c,v 1.39 1995/12/11 04:58:06 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 
106df8bae1dSRodney W. Grimes /*
107df8bae1dSRodney W. Grimes  *	vm_fault:
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Handle a page fault occuring at the given address,
110df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
111df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
112df8bae1dSRodney W. Grimes  *	associated physical map.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
115df8bae1dSRodney W. Grimes  *	proper page address.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
118df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
119df8bae1dSRodney W. Grimes  *
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
122df8bae1dSRodney W. Grimes  *	Caller may hold no locks.
123df8bae1dSRodney W. Grimes  */
124df8bae1dSRodney W. Grimes int
125df8bae1dSRodney W. Grimes vm_fault(map, vaddr, fault_type, change_wiring)
126df8bae1dSRodney W. Grimes 	vm_map_t map;
127df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
128df8bae1dSRodney W. Grimes 	vm_prot_t fault_type;
129df8bae1dSRodney W. Grimes 	boolean_t change_wiring;
130df8bae1dSRodney W. Grimes {
131df8bae1dSRodney W. Grimes 	vm_object_t first_object;
132a316d390SJohn Dyson 	vm_pindex_t first_pindex;
133df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
134df8bae1dSRodney W. Grimes 	register vm_object_t object;
135a316d390SJohn Dyson 	register vm_pindex_t pindex;
13626f9a767SRodney W. Grimes 	vm_page_t m;
137df8bae1dSRodney W. Grimes 	vm_page_t first_m;
138df8bae1dSRodney W. Grimes 	vm_prot_t prot;
139df8bae1dSRodney W. Grimes 	int result;
140df8bae1dSRodney W. Grimes 	boolean_t wired;
141df8bae1dSRodney W. Grimes 	boolean_t su;
142df8bae1dSRodney W. Grimes 	boolean_t lookup_still_valid;
143df8bae1dSRodney W. Grimes 	vm_page_t old_m;
144df8bae1dSRodney W. Grimes 	vm_object_t next_object;
14526f9a767SRodney W. Grimes 	vm_page_t marray[VM_FAULT_READ];
14626f9a767SRodney W. Grimes 	int hardfault = 0;
147f6b04d2bSDavid Greenman 	struct vnode *vp = NULL;
148df8bae1dSRodney W. Grimes 
149b8d95f16SDavid Greenman 	cnt.v_vm_faults++;	/* needs lock XXX */
150df8bae1dSRodney W. Grimes /*
151df8bae1dSRodney W. Grimes  *	Recovery actions
152df8bae1dSRodney W. Grimes  */
153df8bae1dSRodney W. Grimes #define	FREE_PAGE(m)	{				\
154df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);					\
155df8bae1dSRodney W. Grimes 	vm_page_free(m);				\
156df8bae1dSRodney W. Grimes }
157df8bae1dSRodney W. Grimes 
158df8bae1dSRodney W. Grimes #define	RELEASE_PAGE(m)	{				\
159df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);					\
160bd7e5f99SJohn Dyson 	if (m->queue != PQ_ACTIVE) vm_page_activate(m);		\
161df8bae1dSRodney W. Grimes }
162df8bae1dSRodney W. Grimes 
163df8bae1dSRodney W. Grimes #define	UNLOCK_MAP	{				\
164df8bae1dSRodney W. Grimes 	if (lookup_still_valid) {			\
165df8bae1dSRodney W. Grimes 		vm_map_lookup_done(map, entry);		\
166df8bae1dSRodney W. Grimes 		lookup_still_valid = FALSE;		\
167df8bae1dSRodney W. Grimes 	}						\
168df8bae1dSRodney W. Grimes }
169df8bae1dSRodney W. Grimes 
170df8bae1dSRodney W. Grimes #define	UNLOCK_THINGS	{				\
171f919ebdeSDavid Greenman 	vm_object_pip_wakeup(object); \
172df8bae1dSRodney W. Grimes 	if (object != first_object) {			\
173df8bae1dSRodney W. Grimes 		FREE_PAGE(first_m);			\
174f919ebdeSDavid Greenman 		vm_object_pip_wakeup(first_object); \
175df8bae1dSRodney W. Grimes 	}						\
176df8bae1dSRodney W. Grimes 	UNLOCK_MAP;					\
17724a1cce3SDavid Greenman 	if (vp != NULL) VOP_UNLOCK(vp);			\
178df8bae1dSRodney W. Grimes }
179df8bae1dSRodney W. Grimes 
180df8bae1dSRodney W. Grimes #define	UNLOCK_AND_DEALLOCATE	{			\
181df8bae1dSRodney W. Grimes 	UNLOCK_THINGS;					\
182df8bae1dSRodney W. Grimes 	vm_object_deallocate(first_object);		\
183df8bae1dSRodney W. Grimes }
184df8bae1dSRodney W. Grimes 
18526f9a767SRodney W. Grimes 
186df8bae1dSRodney W. Grimes RetryFault:;
187df8bae1dSRodney W. Grimes 
188df8bae1dSRodney W. Grimes 	/*
1890d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
1900d94caffSDavid Greenman 	 * search.
191df8bae1dSRodney W. Grimes 	 */
192df8bae1dSRodney W. Grimes 
19322ba64e8SJohn Dyson 	if ((result = vm_map_lookup(&map, vaddr,
19422ba64e8SJohn Dyson 		fault_type, &entry, &first_object,
195a316d390SJohn Dyson 		&first_pindex, &prot, &wired, &su)) != KERN_SUCCESS) {
196df8bae1dSRodney W. Grimes 		return (result);
197df8bae1dSRodney W. Grimes 	}
198f6b04d2bSDavid Greenman 
19924a1cce3SDavid Greenman 	vp = vnode_pager_lock(first_object);
200f6b04d2bSDavid Greenman 
201df8bae1dSRodney W. Grimes 	lookup_still_valid = TRUE;
202df8bae1dSRodney W. Grimes 
203df8bae1dSRodney W. Grimes 	if (wired)
204df8bae1dSRodney W. Grimes 		fault_type = prot;
205df8bae1dSRodney W. Grimes 
206df8bae1dSRodney W. Grimes 	first_m = NULL;
207df8bae1dSRodney W. Grimes 
208df8bae1dSRodney W. Grimes 	/*
2090d94caffSDavid Greenman 	 * Make a reference to this object to prevent its disposal while we
2100d94caffSDavid Greenman 	 * are messing with it.  Once we have the reference, the map is free
2110d94caffSDavid Greenman 	 * to be diddled.  Since objects reference their shadows (and copies),
2120d94caffSDavid Greenman 	 * they will stay around as well.
213df8bae1dSRodney W. Grimes 	 */
214df8bae1dSRodney W. Grimes 
215df8bae1dSRodney W. Grimes 	first_object->ref_count++;
216df8bae1dSRodney W. Grimes 	first_object->paging_in_progress++;
217df8bae1dSRodney W. Grimes 
218df8bae1dSRodney W. Grimes 	/*
219df8bae1dSRodney W. Grimes 	 * INVARIANTS (through entire routine):
220df8bae1dSRodney W. Grimes 	 *
2210d94caffSDavid Greenman 	 * 1)	At all times, we must either have the object lock or a busy
22224a1cce3SDavid Greenman 	 * page in some object to prevent some other process from trying to
2230d94caffSDavid Greenman 	 * bring in the same page.
224df8bae1dSRodney W. Grimes 	 *
2250d94caffSDavid Greenman 	 * Note that we cannot hold any locks during the pager access or when
2260d94caffSDavid Greenman 	 * waiting for memory, so we use a busy page then.
227df8bae1dSRodney W. Grimes 	 *
2280d94caffSDavid Greenman 	 * Note also that we aren't as concerned about more than one thead
2290d94caffSDavid Greenman 	 * attempting to pager_data_unlock the same page at once, so we don't
2300d94caffSDavid Greenman 	 * hold the page as busy then, but do record the highest unlock value
2310d94caffSDavid Greenman 	 * so far.  [Unlock requests may also be delivered out of order.]
232df8bae1dSRodney W. Grimes 	 *
2330d94caffSDavid Greenman 	 * 2)	Once we have a busy page, we must remove it from the pageout
2340d94caffSDavid Greenman 	 * queues, so that the pageout daemon will not grab it away.
235df8bae1dSRodney W. Grimes 	 *
23624a1cce3SDavid Greenman 	 * 3)	To prevent another process from racing us down the shadow chain
2370d94caffSDavid Greenman 	 * and entering a new page in the top object before we do, we must
2380d94caffSDavid Greenman 	 * keep a busy page in the top object while following the shadow
2390d94caffSDavid Greenman 	 * chain.
240df8bae1dSRodney W. Grimes 	 *
2410d94caffSDavid Greenman 	 * 4)	We must increment paging_in_progress on any object for which
2420d94caffSDavid Greenman 	 * we have a busy page, to prevent vm_object_collapse from removing
2430d94caffSDavid Greenman 	 * the busy page without our noticing.
244df8bae1dSRodney W. Grimes 	 */
245df8bae1dSRodney W. Grimes 
246df8bae1dSRodney W. Grimes 	/*
247df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
248df8bae1dSRodney W. Grimes 	 */
249df8bae1dSRodney W. Grimes 
250df8bae1dSRodney W. Grimes 	object = first_object;
251a316d390SJohn Dyson 	pindex = first_pindex;
252df8bae1dSRodney W. Grimes 
253df8bae1dSRodney W. Grimes 	/*
254df8bae1dSRodney W. Grimes 	 * See whether this page is resident
255df8bae1dSRodney W. Grimes 	 */
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	while (TRUE) {
258a316d390SJohn Dyson 		m = vm_page_lookup(object, pindex);
259df8bae1dSRodney W. Grimes 		if (m != NULL) {
260df8bae1dSRodney W. Grimes 			/*
2610d94caffSDavid Greenman 			 * If the page is being brought in, wait for it and
2620d94caffSDavid Greenman 			 * then retry.
263df8bae1dSRodney W. Grimes 			 */
2640d94caffSDavid Greenman 			if ((m->flags & PG_BUSY) || m->busy) {
26516f62314SDavid Greenman 				int s;
2660d94caffSDavid Greenman 
267df8bae1dSRodney W. Grimes 				UNLOCK_THINGS;
26816f62314SDavid Greenman 				s = splhigh();
2690d94caffSDavid Greenman 				if ((m->flags & PG_BUSY) || m->busy) {
2700d94caffSDavid Greenman 					m->flags |= PG_WANTED | PG_REFERENCED;
271976e77fcSDavid Greenman 					cnt.v_intrans++;
27224a1cce3SDavid Greenman 					tsleep(m, PSWP, "vmpfw", 0);
27326f9a767SRodney W. Grimes 				}
27416f62314SDavid Greenman 				splx(s);
275df8bae1dSRodney W. Grimes 				vm_object_deallocate(first_object);
276df8bae1dSRodney W. Grimes 				goto RetryFault;
277df8bae1dSRodney W. Grimes 			}
278f6b04d2bSDavid Greenman 
279df8bae1dSRodney W. Grimes 			/*
28024a1cce3SDavid Greenman 			 * Mark page busy for other processes, and the pagedaemon.
281df8bae1dSRodney W. Grimes 			 */
282df8bae1dSRodney W. Grimes 			m->flags |= PG_BUSY;
283bd7e5f99SJohn Dyson 			if ((m->queue == PQ_CACHE) &&
28422ba64e8SJohn Dyson 			    (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_reserved) {
28522ba64e8SJohn Dyson 				UNLOCK_AND_DEALLOCATE;
28622ba64e8SJohn Dyson 				VM_WAIT;
28722ba64e8SJohn Dyson 				PAGE_WAKEUP(m);
28822ba64e8SJohn Dyson 				goto RetryFault;
28922ba64e8SJohn Dyson 			}
29022ba64e8SJohn Dyson 
291bd7e5f99SJohn Dyson 			if (m->valid &&
292bd7e5f99SJohn Dyson 				((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
293f919ebdeSDavid Greenman 				m->object != kernel_object && m->object != kmem_object) {
2940d94caffSDavid Greenman 				goto readrest;
2950d94caffSDavid Greenman 			}
296df8bae1dSRodney W. Grimes 			break;
297df8bae1dSRodney W. Grimes 		}
29824a1cce3SDavid Greenman 		if (((object->type != OBJT_DEFAULT) && (!change_wiring || wired))
299df8bae1dSRodney W. Grimes 		    || (object == first_object)) {
300df8bae1dSRodney W. Grimes 
301a316d390SJohn Dyson 			if (pindex >= object->size) {
3025f55e841SDavid Greenman 				UNLOCK_AND_DEALLOCATE;
3035f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
3045f55e841SDavid Greenman 			}
30522ba64e8SJohn Dyson 
306df8bae1dSRodney W. Grimes 			/*
3070d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
308df8bae1dSRodney W. Grimes 			 */
309a316d390SJohn Dyson 			m = vm_page_alloc(object, pindex,
310bd7e5f99SJohn Dyson 				vp?VM_ALLOC_NORMAL:VM_ALLOC_ZERO);
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 			if (m == NULL) {
313df8bae1dSRodney W. Grimes 				UNLOCK_AND_DEALLOCATE;
314df8bae1dSRodney W. Grimes 				VM_WAIT;
315df8bae1dSRodney W. Grimes 				goto RetryFault;
316df8bae1dSRodney W. Grimes 			}
317df8bae1dSRodney W. Grimes 		}
3180d94caffSDavid Greenman readrest:
31924a1cce3SDavid Greenman 		if (object->type != OBJT_DEFAULT && (!change_wiring || wired)) {
320df8bae1dSRodney W. Grimes 			int rv;
32126f9a767SRodney W. Grimes 			int faultcount;
32226f9a767SRodney W. Grimes 			int reqpage;
323df8bae1dSRodney W. Grimes 
324df8bae1dSRodney W. Grimes 			/*
3250d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
3260d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
3270d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
3280d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
3290d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
3300d94caffSDavid Greenman 			 * array "marray" returned contains an array of
3310d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
3320d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
3330d94caffSDavid Greenman 			 * return value is the index into the marray for the
3340d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
33526f9a767SRodney W. Grimes 			 */
33605f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
33705f0fdd2SPoul-Henning Kamp 			    m, VM_FAULT_READ_BEHIND, VM_FAULT_READ_AHEAD,
33805f0fdd2SPoul-Henning Kamp 			    marray, &reqpage);
339df8bae1dSRodney W. Grimes 
340df8bae1dSRodney W. Grimes 			/*
3410d94caffSDavid Greenman 			 * Call the pager to retrieve the data, if any, after
3420d94caffSDavid Greenman 			 * releasing the lock on the map.
343df8bae1dSRodney W. Grimes 			 */
344df8bae1dSRodney W. Grimes 			UNLOCK_MAP;
345df8bae1dSRodney W. Grimes 
34626f9a767SRodney W. Grimes 			rv = faultcount ?
34724a1cce3SDavid Greenman 			    vm_pager_get_pages(object, marray, faultcount,
34824a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
34922ba64e8SJohn Dyson 
35026f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
351df8bae1dSRodney W. Grimes 				/*
3520d94caffSDavid Greenman 				 * Found the page. Leave it busy while we play
3530d94caffSDavid Greenman 				 * with it.
354df8bae1dSRodney W. Grimes 				 */
35526f9a767SRodney W. Grimes 
356df8bae1dSRodney W. Grimes 				/*
3570d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
3580d94caffSDavid Greenman 				 * is responsible for disposition of old page
3590d94caffSDavid Greenman 				 * if moved.
360df8bae1dSRodney W. Grimes 				 */
361a316d390SJohn Dyson 				m = vm_page_lookup(object, pindex);
362f6b04d2bSDavid Greenman 				if( !m) {
363f6b04d2bSDavid Greenman 					UNLOCK_AND_DEALLOCATE;
364f6b04d2bSDavid Greenman 					goto RetryFault;
365f6b04d2bSDavid Greenman 				}
366f6b04d2bSDavid Greenman 
36726f9a767SRodney W. Grimes 				hardfault++;
368df8bae1dSRodney W. Grimes 				break;
369df8bae1dSRodney W. Grimes 			}
370df8bae1dSRodney W. Grimes 			/*
3710d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
3720d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
3730d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
374df8bae1dSRodney W. Grimes 			 *
37524a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
3760d94caffSDavid Greenman 			 * in this page.
377df8bae1dSRodney W. Grimes 			 *
3780d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
37924a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
3800d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
3810d94caffSDavid Greenman 			 * the same time that we are.
382df8bae1dSRodney W. Grimes 			 */
38326f9a767SRodney W. Grimes 
384a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
385a83c285cSDavid Greenman 				printf("vm_fault: pager input (probably hardware) error, PID %d failure\n",
386a83c285cSDavid Greenman 				    curproc->p_pid);
38726f9a767SRodney W. Grimes 			/*
388a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
38926f9a767SRodney W. Grimes 			 */
390a83c285cSDavid Greenman 			/*
3910d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
3920d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
3930d94caffSDavid Greenman 			 * fault w/ I/O error.
394a83c285cSDavid Greenman 			 */
395a83c285cSDavid Greenman 			if (((map != kernel_map) && (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
39626f9a767SRodney W. Grimes 				FREE_PAGE(m);
39726f9a767SRodney W. Grimes 				UNLOCK_AND_DEALLOCATE;
398a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
39926f9a767SRodney W. Grimes 			}
400df8bae1dSRodney W. Grimes 			if (object != first_object) {
401df8bae1dSRodney W. Grimes 				FREE_PAGE(m);
40226f9a767SRodney W. Grimes 				/*
40326f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
40426f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
40526f9a767SRodney W. Grimes 				 */
406df8bae1dSRodney W. Grimes 			}
407df8bae1dSRodney W. Grimes 		}
408df8bae1dSRodney W. Grimes 		/*
40924a1cce3SDavid Greenman 		 * We get here if the object has default pager (or unwiring) or the
4100d94caffSDavid Greenman 		 * pager doesn't have the page.
411df8bae1dSRodney W. Grimes 		 */
412df8bae1dSRodney W. Grimes 		if (object == first_object)
413df8bae1dSRodney W. Grimes 			first_m = m;
414df8bae1dSRodney W. Grimes 
415df8bae1dSRodney W. Grimes 		/*
4160d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
4170d94caffSDavid Greenman 		 * unlocking the current one.
418df8bae1dSRodney W. Grimes 		 */
419df8bae1dSRodney W. Grimes 
420a316d390SJohn Dyson 		pindex += OFF_TO_IDX(object->backing_object_offset);
42124a1cce3SDavid Greenman 		next_object = object->backing_object;
422df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
423df8bae1dSRodney W. Grimes 			/*
4240d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
4250d94caffSDavid Greenman 			 * object with zeros.
426df8bae1dSRodney W. Grimes 			 */
427df8bae1dSRodney W. Grimes 			if (object != first_object) {
428f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
429df8bae1dSRodney W. Grimes 
430df8bae1dSRodney W. Grimes 				object = first_object;
431a316d390SJohn Dyson 				pindex = first_pindex;
432df8bae1dSRodney W. Grimes 				m = first_m;
433df8bae1dSRodney W. Grimes 			}
434df8bae1dSRodney W. Grimes 			first_m = NULL;
435df8bae1dSRodney W. Grimes 
436f70f05f2SJohn Dyson 			if ((m->flags & PG_ZERO) == 0)
437df8bae1dSRodney W. Grimes 				vm_page_zero_fill(m);
4380d94caffSDavid Greenman 			m->valid = VM_PAGE_BITS_ALL;
439df8bae1dSRodney W. Grimes 			cnt.v_zfod++;
440df8bae1dSRodney W. Grimes 			break;
4410d94caffSDavid Greenman 		} else {
44226f9a767SRodney W. Grimes 			if (object != first_object) {
443f919ebdeSDavid Greenman 				vm_object_pip_wakeup(object);
444c0503609SDavid Greenman 			}
445df8bae1dSRodney W. Grimes 			object = next_object;
446df8bae1dSRodney W. Grimes 			object->paging_in_progress++;
447df8bae1dSRodney W. Grimes 		}
448df8bae1dSRodney W. Grimes 	}
449df8bae1dSRodney W. Grimes 
450f919ebdeSDavid Greenman 	if ((m->flags & PG_BUSY) == 0)
451f919ebdeSDavid Greenman 		panic("vm_fault: not busy after main loop");
452df8bae1dSRodney W. Grimes 
453df8bae1dSRodney W. Grimes 	/*
4540d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
455df8bae1dSRodney W. Grimes 	 * is held.]
456df8bae1dSRodney W. Grimes 	 */
457df8bae1dSRodney W. Grimes 
458df8bae1dSRodney W. Grimes 	old_m = m;	/* save page that would be copied */
459df8bae1dSRodney W. Grimes 
460df8bae1dSRodney W. Grimes 	/*
4610d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
4620d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
4630d94caffSDavid Greenman 	 * top-level object.
464df8bae1dSRodney W. Grimes 	 */
465df8bae1dSRodney W. Grimes 
466df8bae1dSRodney W. Grimes 	if (object != first_object) {
467df8bae1dSRodney W. Grimes 		/*
4680d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
469df8bae1dSRodney W. Grimes 		 */
470df8bae1dSRodney W. Grimes 
471df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
472df8bae1dSRodney W. Grimes 
473df8bae1dSRodney W. Grimes 			/*
4740d94caffSDavid Greenman 			 * If we try to collapse first_object at this point,
4750d94caffSDavid Greenman 			 * we may deadlock when we try to get the lock on an
4760d94caffSDavid Greenman 			 * intermediate object (since we have the bottom
4770d94caffSDavid Greenman 			 * object locked).  We can't unlock the bottom object,
4780d94caffSDavid Greenman 			 * because the page we found may move (by collapse) if
4790d94caffSDavid Greenman 			 * we do.
480df8bae1dSRodney W. Grimes 			 *
4810d94caffSDavid Greenman 			 * Instead, we first copy the page.  Then, when we have
4820d94caffSDavid Greenman 			 * no more use for the bottom object, we unlock it and
4830d94caffSDavid Greenman 			 * try to collapse.
484df8bae1dSRodney W. Grimes 			 *
4850d94caffSDavid Greenman 			 * Note that we copy the page even if we didn't need
4860d94caffSDavid Greenman 			 * to... that's the breaks.
487df8bae1dSRodney W. Grimes 			 */
488df8bae1dSRodney W. Grimes 
489df8bae1dSRodney W. Grimes 			/*
4900d94caffSDavid Greenman 			 * We already have an empty page in first_object - use
4910d94caffSDavid Greenman 			 * it.
492df8bae1dSRodney W. Grimes 			 */
493df8bae1dSRodney W. Grimes 
494df8bae1dSRodney W. Grimes 			vm_page_copy(m, first_m);
4950d94caffSDavid Greenman 			first_m->valid = VM_PAGE_BITS_ALL;
496df8bae1dSRodney W. Grimes 
497df8bae1dSRodney W. Grimes 			/*
4980d94caffSDavid Greenman 			 * If another map is truly sharing this page with us,
4990d94caffSDavid Greenman 			 * we have to flush all uses of the original page,
5000d94caffSDavid Greenman 			 * since we can't distinguish those which want the
5010d94caffSDavid Greenman 			 * original from those which need the new copy.
502df8bae1dSRodney W. Grimes 			 *
5030d94caffSDavid Greenman 			 * XXX If we know that only one map has access to this
5040d94caffSDavid Greenman 			 * page, then we could avoid the pmap_page_protect()
5050d94caffSDavid Greenman 			 * call.
506df8bae1dSRodney W. Grimes 			 */
507df8bae1dSRodney W. Grimes 
508bd7e5f99SJohn Dyson 			if (m->queue != PQ_ACTIVE)
509df8bae1dSRodney W. Grimes 				vm_page_activate(m);
510df8bae1dSRodney W. Grimes 
511df8bae1dSRodney W. Grimes 			/*
512df8bae1dSRodney W. Grimes 			 * We no longer need the old page or object.
513df8bae1dSRodney W. Grimes 			 */
514df8bae1dSRodney W. Grimes 			PAGE_WAKEUP(m);
515f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
516df8bae1dSRodney W. Grimes 
517df8bae1dSRodney W. Grimes 			/*
518df8bae1dSRodney W. Grimes 			 * Only use the new page below...
519df8bae1dSRodney W. Grimes 			 */
520df8bae1dSRodney W. Grimes 
521df8bae1dSRodney W. Grimes 			cnt.v_cow_faults++;
522df8bae1dSRodney W. Grimes 			m = first_m;
523df8bae1dSRodney W. Grimes 			object = first_object;
524a316d390SJohn Dyson 			pindex = first_pindex;
525df8bae1dSRodney W. Grimes 
526df8bae1dSRodney W. Grimes 			/*
5270d94caffSDavid Greenman 			 * Now that we've gotten the copy out of the way,
5280d94caffSDavid Greenman 			 * let's try to collapse the top object.
52924a1cce3SDavid Greenman 			 *
530df8bae1dSRodney W. Grimes 			 * But we have to play ugly games with
531df8bae1dSRodney W. Grimes 			 * paging_in_progress to do that...
532df8bae1dSRodney W. Grimes 			 */
533f919ebdeSDavid Greenman 			vm_object_pip_wakeup(object);
534df8bae1dSRodney W. Grimes 			vm_object_collapse(object);
535df8bae1dSRodney W. Grimes 			object->paging_in_progress++;
5360d94caffSDavid Greenman 		} else {
537df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
538df8bae1dSRodney W. Grimes 		}
539df8bae1dSRodney W. Grimes 	}
540df8bae1dSRodney W. Grimes 
541df8bae1dSRodney W. Grimes 	/*
5420d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
5430d94caffSDavid Greenman 	 * lookup.
544df8bae1dSRodney W. Grimes 	 */
545df8bae1dSRodney W. Grimes 
546df8bae1dSRodney W. Grimes 	if (!lookup_still_valid) {
547df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
548a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
549df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
550df8bae1dSRodney W. Grimes 
551df8bae1dSRodney W. Grimes 		/*
5520d94caffSDavid Greenman 		 * Since map entries may be pageable, make sure we can take a
5530d94caffSDavid Greenman 		 * page fault on them.
554df8bae1dSRodney W. Grimes 		 */
555df8bae1dSRodney W. Grimes 
556df8bae1dSRodney W. Grimes 		/*
55724a1cce3SDavid Greenman 		 * To avoid trying to write_lock the map while another process
5580d94caffSDavid Greenman 		 * has it read_locked (in vm_map_pageable), we do not try for
5590d94caffSDavid Greenman 		 * write permission.  If the page is still writable, we will
5600d94caffSDavid Greenman 		 * get write permission.  If it is not, or has been marked
5610d94caffSDavid Greenman 		 * needs_copy, we enter the mapping without write permission,
5620d94caffSDavid Greenman 		 * and will merely take another fault.
563df8bae1dSRodney W. Grimes 		 */
5640d94caffSDavid Greenman 		result = vm_map_lookup(&map, vaddr, fault_type & ~VM_PROT_WRITE,
565a316d390SJohn Dyson 		    &entry, &retry_object, &retry_pindex, &retry_prot, &wired, &su);
566df8bae1dSRodney W. Grimes 
567df8bae1dSRodney W. Grimes 		/*
5680d94caffSDavid Greenman 		 * If we don't need the page any longer, put it on the active
5690d94caffSDavid Greenman 		 * list (the easiest thing to do here).  If no one needs it,
5700d94caffSDavid Greenman 		 * pageout will grab it eventually.
571df8bae1dSRodney W. Grimes 		 */
572df8bae1dSRodney W. Grimes 
573df8bae1dSRodney W. Grimes 		if (result != KERN_SUCCESS) {
574df8bae1dSRodney W. Grimes 			RELEASE_PAGE(m);
575df8bae1dSRodney W. Grimes 			UNLOCK_AND_DEALLOCATE;
576df8bae1dSRodney W. Grimes 			return (result);
577df8bae1dSRodney W. Grimes 		}
578df8bae1dSRodney W. Grimes 		lookup_still_valid = TRUE;
579df8bae1dSRodney W. Grimes 
580df8bae1dSRodney W. Grimes 		if ((retry_object != first_object) ||
581a316d390SJohn Dyson 		    (retry_pindex != first_pindex)) {
582df8bae1dSRodney W. Grimes 			RELEASE_PAGE(m);
583df8bae1dSRodney W. Grimes 			UNLOCK_AND_DEALLOCATE;
584df8bae1dSRodney W. Grimes 			goto RetryFault;
585df8bae1dSRodney W. Grimes 		}
586df8bae1dSRodney W. Grimes 		/*
5870d94caffSDavid Greenman 		 * Check whether the protection has changed or the object has
5880d94caffSDavid Greenman 		 * been copied while we left the map unlocked. Changing from
5890d94caffSDavid Greenman 		 * read to write permission is OK - we leave the page
5900d94caffSDavid Greenman 		 * write-protected, and catch the write fault. Changing from
5910d94caffSDavid Greenman 		 * write to read permission means that we can't mark the page
5920d94caffSDavid Greenman 		 * write-enabled after all.
593df8bae1dSRodney W. Grimes 		 */
594df8bae1dSRodney W. Grimes 		prot &= retry_prot;
595df8bae1dSRodney W. Grimes 	}
596df8bae1dSRodney W. Grimes 	/*
5970d94caffSDavid Greenman 	 * (the various bits we're fiddling with here are locked by the
5980d94caffSDavid Greenman 	 * object's lock)
599df8bae1dSRodney W. Grimes 	 */
600df8bae1dSRodney W. Grimes 
601df8bae1dSRodney W. Grimes 	/*
6020d94caffSDavid Greenman 	 * It's critically important that a wired-down page be faulted only
6030d94caffSDavid Greenman 	 * once in each map for which it is wired.
604df8bae1dSRodney W. Grimes 	 */
605df8bae1dSRodney W. Grimes 
606df8bae1dSRodney W. Grimes 	/*
6070d94caffSDavid Greenman 	 * Put this page into the physical map. We had to do the unlock above
6080d94caffSDavid Greenman 	 * because pmap_enter may cause other faults.   We don't put the page
6090d94caffSDavid Greenman 	 * back on the active queue until later so that the page-out daemon
6100d94caffSDavid Greenman 	 * won't find us (yet).
611df8bae1dSRodney W. Grimes 	 */
612df8bae1dSRodney W. Grimes 
6132ddba215SDavid Greenman 	if (prot & VM_PROT_WRITE) {
614f919ebdeSDavid Greenman 		m->flags |= PG_WRITEABLE;
615aef922f5SJohn Dyson 		m->object->flags |= OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY;
6162ddba215SDavid Greenman 		/*
6172ddba215SDavid Greenman 		 * If the fault is a write, we know that this page is being
6182ddba215SDavid Greenman 		 * written NOW. This will save on the pmap_is_modified() calls
6192ddba215SDavid Greenman 		 * later.
6202ddba215SDavid Greenman 		 */
6212ddba215SDavid Greenman 		if (fault_type & VM_PROT_WRITE) {
6222ddba215SDavid Greenman 			m->dirty = VM_PAGE_BITS_ALL;
6232ddba215SDavid Greenman 		}
6242ddba215SDavid Greenman 	}
625f6b04d2bSDavid Greenman 
626f70f05f2SJohn Dyson 	m->flags |= PG_MAPPED|PG_REFERENCED;
627ced399eeSJohn Dyson 	m->flags &= ~PG_ZERO;
628f919ebdeSDavid Greenman 
629df8bae1dSRodney W. Grimes 	pmap_enter(map->pmap, vaddr, VM_PAGE_TO_PHYS(m), prot, wired);
6301e9122e5SDavid Greenman #if 0
631f6b04d2bSDavid Greenman 	if (change_wiring == 0 && wired == 0)
6326d40c3d3SDavid Greenman 		pmap_prefault(map->pmap, vaddr, entry, first_object);
6331e9122e5SDavid Greenman #endif
634df8bae1dSRodney W. Grimes 
635df8bae1dSRodney W. Grimes 	/*
6360d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
6370d94caffSDavid Greenman 	 * can find it.
638df8bae1dSRodney W. Grimes 	 */
639df8bae1dSRodney W. Grimes 	if (change_wiring) {
640df8bae1dSRodney W. Grimes 		if (wired)
641df8bae1dSRodney W. Grimes 			vm_page_wire(m);
642df8bae1dSRodney W. Grimes 		else
643df8bae1dSRodney W. Grimes 			vm_page_unwire(m);
6440d94caffSDavid Greenman 	} else {
645bd7e5f99SJohn Dyson 		if (m->queue != PQ_ACTIVE)
646df8bae1dSRodney W. Grimes 			vm_page_activate(m);
64726f9a767SRodney W. Grimes 	}
64826f9a767SRodney W. Grimes 
649a1f6d91cSDavid Greenman 	if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
65026f9a767SRodney W. Grimes 		if (hardfault) {
65126f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_majflt++;
65226f9a767SRodney W. Grimes 		} else {
65326f9a767SRodney W. Grimes 			curproc->p_stats->p_ru.ru_minflt++;
65426f9a767SRodney W. Grimes 		}
65526f9a767SRodney W. Grimes 	}
656df8bae1dSRodney W. Grimes 
657df8bae1dSRodney W. Grimes 	/*
658df8bae1dSRodney W. Grimes 	 * Unlock everything, and return
659df8bae1dSRodney W. Grimes 	 */
660df8bae1dSRodney W. Grimes 
661df8bae1dSRodney W. Grimes 	PAGE_WAKEUP(m);
662df8bae1dSRodney W. Grimes 	UNLOCK_AND_DEALLOCATE;
663df8bae1dSRodney W. Grimes 
664df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
665df8bae1dSRodney W. Grimes 
666df8bae1dSRodney W. Grimes }
667df8bae1dSRodney W. Grimes 
668df8bae1dSRodney W. Grimes /*
669df8bae1dSRodney W. Grimes  *	vm_fault_wire:
670df8bae1dSRodney W. Grimes  *
671df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
672df8bae1dSRodney W. Grimes  */
673df8bae1dSRodney W. Grimes int
674df8bae1dSRodney W. Grimes vm_fault_wire(map, start, end)
675df8bae1dSRodney W. Grimes 	vm_map_t map;
676df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
677df8bae1dSRodney W. Grimes {
67826f9a767SRodney W. Grimes 
679df8bae1dSRodney W. Grimes 	register vm_offset_t va;
680df8bae1dSRodney W. Grimes 	register pmap_t pmap;
681df8bae1dSRodney W. Grimes 	int rv;
682df8bae1dSRodney W. Grimes 
683df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
684df8bae1dSRodney W. Grimes 
685df8bae1dSRodney W. Grimes 	/*
6860d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
6870d94caffSDavid Greenman 	 * not fault, so that page tables and such can be locked down as well.
688df8bae1dSRodney W. Grimes 	 */
689df8bae1dSRodney W. Grimes 
690df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, FALSE);
691df8bae1dSRodney W. Grimes 
692df8bae1dSRodney W. Grimes 	/*
6930d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
6940d94caffSDavid Greenman 	 * map.
695df8bae1dSRodney W. Grimes 	 */
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
6986d40c3d3SDavid Greenman 
699a1f6d91cSDavid Greenman 		while( curproc != pageproc &&
7006d40c3d3SDavid Greenman 			(cnt.v_free_count <= cnt.v_pageout_free_min))
7016d40c3d3SDavid Greenman 			VM_WAIT;
7026d40c3d3SDavid Greenman 
703a1f6d91cSDavid Greenman 		rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, TRUE);
704df8bae1dSRodney W. Grimes 		if (rv) {
705df8bae1dSRodney W. Grimes 			if (va != start)
706df8bae1dSRodney W. Grimes 				vm_fault_unwire(map, start, va);
707df8bae1dSRodney W. Grimes 			return (rv);
708df8bae1dSRodney W. Grimes 		}
709df8bae1dSRodney W. Grimes 	}
710df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
711df8bae1dSRodney W. Grimes }
712df8bae1dSRodney W. Grimes 
713df8bae1dSRodney W. Grimes 
714df8bae1dSRodney W. Grimes /*
715df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
716df8bae1dSRodney W. Grimes  *
717df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
718df8bae1dSRodney W. Grimes  */
71926f9a767SRodney W. Grimes void
72026f9a767SRodney W. Grimes vm_fault_unwire(map, start, end)
721df8bae1dSRodney W. Grimes 	vm_map_t map;
722df8bae1dSRodney W. Grimes 	vm_offset_t start, end;
723df8bae1dSRodney W. Grimes {
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes 	register vm_offset_t va, pa;
726df8bae1dSRodney W. Grimes 	register pmap_t pmap;
727df8bae1dSRodney W. Grimes 
728df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
729df8bae1dSRodney W. Grimes 
730df8bae1dSRodney W. Grimes 	/*
7310d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
7320d94caffSDavid Greenman 	 * mappings from the physical map system.
733df8bae1dSRodney W. Grimes 	 */
734df8bae1dSRodney W. Grimes 
735df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
736df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
737df8bae1dSRodney W. Grimes 		if (pa == (vm_offset_t) 0) {
738df8bae1dSRodney W. Grimes 			panic("unwire: page not in pmap");
739df8bae1dSRodney W. Grimes 		}
740df8bae1dSRodney W. Grimes 		pmap_change_wiring(pmap, va, FALSE);
741df8bae1dSRodney W. Grimes 		vm_page_unwire(PHYS_TO_VM_PAGE(pa));
742df8bae1dSRodney W. Grimes 	}
743df8bae1dSRodney W. Grimes 
744df8bae1dSRodney W. Grimes 	/*
7450d94caffSDavid Greenman 	 * Inform the physical mapping system that the range of addresses may
7460d94caffSDavid Greenman 	 * fault, so that page tables and such may be unwired themselves.
747df8bae1dSRodney W. Grimes 	 */
748df8bae1dSRodney W. Grimes 
749df8bae1dSRodney W. Grimes 	pmap_pageable(pmap, start, end, TRUE);
750df8bae1dSRodney W. Grimes 
751df8bae1dSRodney W. Grimes }
752df8bae1dSRodney W. Grimes 
753df8bae1dSRodney W. Grimes /*
754df8bae1dSRodney W. Grimes  *	Routine:
755df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
756df8bae1dSRodney W. Grimes  *	Function:
757df8bae1dSRodney W. Grimes  *		Copy all of the pages from a wired-down map entry to another.
758df8bae1dSRodney W. Grimes  *
759df8bae1dSRodney W. Grimes  *	In/out conditions:
760df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
761df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
762df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
763df8bae1dSRodney W. Grimes  */
764df8bae1dSRodney W. Grimes 
76526f9a767SRodney W. Grimes void
76626f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
767df8bae1dSRodney W. Grimes 	vm_map_t dst_map;
768df8bae1dSRodney W. Grimes 	vm_map_t src_map;
769df8bae1dSRodney W. Grimes 	vm_map_entry_t dst_entry;
770df8bae1dSRodney W. Grimes 	vm_map_entry_t src_entry;
771df8bae1dSRodney W. Grimes {
772df8bae1dSRodney W. Grimes 	vm_object_t dst_object;
773df8bae1dSRodney W. Grimes 	vm_object_t src_object;
774a316d390SJohn Dyson 	vm_ooffset_t dst_offset;
775a316d390SJohn Dyson 	vm_ooffset_t src_offset;
776df8bae1dSRodney W. Grimes 	vm_prot_t prot;
777df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
778df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
779df8bae1dSRodney W. Grimes 	vm_page_t src_m;
780df8bae1dSRodney W. Grimes 
781df8bae1dSRodney W. Grimes #ifdef	lint
782df8bae1dSRodney W. Grimes 	src_map++;
7830d94caffSDavid Greenman #endif	/* lint */
784df8bae1dSRodney W. Grimes 
785df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
786df8bae1dSRodney W. Grimes 	src_offset = src_entry->offset;
787df8bae1dSRodney W. Grimes 
788df8bae1dSRodney W. Grimes 	/*
7890d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
7900d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
791df8bae1dSRodney W. Grimes 	 */
79224a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
793a316d390SJohn Dyson 	    (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
794df8bae1dSRodney W. Grimes 
795df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
796df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes 	prot = dst_entry->max_protection;
799df8bae1dSRodney W. Grimes 
800df8bae1dSRodney W. Grimes 	/*
8010d94caffSDavid Greenman 	 * Loop through all of the pages in the entry's range, copying each
8020d94caffSDavid Greenman 	 * one from the source object (it should be there) to the destination
8030d94caffSDavid Greenman 	 * object.
804df8bae1dSRodney W. Grimes 	 */
805df8bae1dSRodney W. Grimes 	for (vaddr = dst_entry->start, dst_offset = 0;
806df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
807df8bae1dSRodney W. Grimes 	    vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
808df8bae1dSRodney W. Grimes 
809df8bae1dSRodney W. Grimes 		/*
810df8bae1dSRodney W. Grimes 		 * Allocate a page in the destination object
811df8bae1dSRodney W. Grimes 		 */
812df8bae1dSRodney W. Grimes 		do {
813a316d390SJohn Dyson 			dst_m = vm_page_alloc(dst_object,
814a316d390SJohn Dyson 				OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
815df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
816df8bae1dSRodney W. Grimes 				VM_WAIT;
817df8bae1dSRodney W. Grimes 			}
818df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
819df8bae1dSRodney W. Grimes 
820df8bae1dSRodney W. Grimes 		/*
821df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
8220d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
8230d94caffSDavid Greenman 		 * memory.)
824df8bae1dSRodney W. Grimes 		 */
825a316d390SJohn Dyson 		src_m = vm_page_lookup(src_object,
826a316d390SJohn Dyson 			OFF_TO_IDX(dst_offset + src_offset));
827df8bae1dSRodney W. Grimes 		if (src_m == NULL)
828df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
829df8bae1dSRodney W. Grimes 
830df8bae1dSRodney W. Grimes 		vm_page_copy(src_m, dst_m);
831df8bae1dSRodney W. Grimes 
832df8bae1dSRodney W. Grimes 		/*
833df8bae1dSRodney W. Grimes 		 * Enter it in the pmap...
834df8bae1dSRodney W. Grimes 		 */
835df8bae1dSRodney W. Grimes 
836ced399eeSJohn Dyson 		dst_m->flags |= PG_WRITEABLE|PG_MAPPED;
837ccbb2f72SJohn Dyson 		dst_m->flags &= ~PG_ZERO;
838df8bae1dSRodney W. Grimes 		pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m),
839df8bae1dSRodney W. Grimes 		    prot, FALSE);
840df8bae1dSRodney W. Grimes 
841df8bae1dSRodney W. Grimes 		/*
842df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
843df8bae1dSRodney W. Grimes 		 */
844df8bae1dSRodney W. Grimes 		vm_page_activate(dst_m);
845df8bae1dSRodney W. Grimes 		PAGE_WAKEUP(dst_m);
846df8bae1dSRodney W. Grimes 	}
847df8bae1dSRodney W. Grimes }
84826f9a767SRodney W. Grimes 
84926f9a767SRodney W. Grimes 
85026f9a767SRodney W. Grimes /*
85126f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
85222ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
85322ba64e8SJohn Dyson  * pages for the pages to be paged in.
85426f9a767SRodney W. Grimes  *
85526f9a767SRodney W. Grimes  * Inputs:
85622ba64e8SJohn Dyson  *	m, rbehind, rahead
85726f9a767SRodney W. Grimes  *
85826f9a767SRodney W. Grimes  * Outputs:
85926f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
86026f9a767SRodney W. Grimes  *
86126f9a767SRodney W. Grimes  * Return value:
86226f9a767SRodney W. Grimes  *  number of pages in marray
86326f9a767SRodney W. Grimes  */
86426f9a767SRodney W. Grimes int
86522ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
86626f9a767SRodney W. Grimes 	vm_page_t m;
86726f9a767SRodney W. Grimes 	int rbehind;
86822ba64e8SJohn Dyson 	int rahead;
86926f9a767SRodney W. Grimes 	vm_page_t *marray;
87026f9a767SRodney W. Grimes 	int *reqpage;
87126f9a767SRodney W. Grimes {
87226f9a767SRodney W. Grimes 	int i;
87326f9a767SRodney W. Grimes 	vm_object_t object;
874a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
875a316d390SJohn Dyson 	vm_offset_t size;
87626f9a767SRodney W. Grimes 	vm_page_t rtm;
87726f9a767SRodney W. Grimes 	int treqpage;
878170db9c6SJohn Dyson 	int cbehind, cahead;
87926f9a767SRodney W. Grimes 
88026f9a767SRodney W. Grimes 	object = m->object;
881a316d390SJohn Dyson 	pindex = m->pindex;
88226f9a767SRodney W. Grimes 
88326f9a767SRodney W. Grimes 	/*
88426f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
88526f9a767SRodney W. Grimes 	 */
88626f9a767SRodney W. Grimes 
887170db9c6SJohn Dyson 	if (!vm_pager_has_page(object,
888a316d390SJohn Dyson 		OFF_TO_IDX(object->paging_offset) + pindex, &cbehind, &cahead))
88926f9a767SRodney W. Grimes 		return 0;
89026f9a767SRodney W. Grimes 
89122ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
89222ba64e8SJohn Dyson 		*reqpage = 0;
89322ba64e8SJohn Dyson 		marray[0] = m;
89422ba64e8SJohn Dyson 		return 1;
895170db9c6SJohn Dyson 	}
89622ba64e8SJohn Dyson 
89722ba64e8SJohn Dyson 	if (rahead > cahead) {
89822ba64e8SJohn Dyson 		rahead = cahead;
89922ba64e8SJohn Dyson 	}
90022ba64e8SJohn Dyson 
901170db9c6SJohn Dyson 	if (rbehind > cbehind) {
902170db9c6SJohn Dyson 		rbehind = cbehind;
903170db9c6SJohn Dyson 	}
904170db9c6SJohn Dyson 
90526f9a767SRodney W. Grimes 	/*
90626f9a767SRodney W. Grimes 	 * try to do any readahead that we might have free pages for.
90726f9a767SRodney W. Grimes 	 */
908ccbb2f72SJohn Dyson 	if ((rahead + rbehind) >
90922ba64e8SJohn Dyson 		((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
910f919ebdeSDavid Greenman 		pagedaemon_wakeup();
91126f9a767SRodney W. Grimes 		*reqpage = 0;
91226f9a767SRodney W. Grimes 		marray[0] = m;
91326f9a767SRodney W. Grimes 		return 1;
91426f9a767SRodney W. Grimes 	}
91522ba64e8SJohn Dyson 
91626f9a767SRodney W. Grimes 	/*
9170d94caffSDavid Greenman 	 * scan backward for the read behind pages -- in memory or on disk not
9180d94caffSDavid Greenman 	 * in same object
91926f9a767SRodney W. Grimes 	 */
920a316d390SJohn Dyson 	tpindex = pindex - 1;
921a316d390SJohn Dyson 	if (tpindex < pindex) {
922a316d390SJohn Dyson 		if (rbehind > pindex)
923a316d390SJohn Dyson 			rbehind = pindex;
924a316d390SJohn Dyson 		startpindex = pindex - rbehind;
925a316d390SJohn Dyson 		while (tpindex >= startpindex) {
926a316d390SJohn Dyson 			if (vm_page_lookup( object, tpindex)) {
927a316d390SJohn Dyson 				startpindex = tpindex + 1;
92826f9a767SRodney W. Grimes 				break;
92926f9a767SRodney W. Grimes 			}
930a316d390SJohn Dyson 			if (tpindex == 0)
93126f9a767SRodney W. Grimes 				break;
932a316d390SJohn Dyson 			tpindex -= 1;
93326f9a767SRodney W. Grimes 		}
934317205caSDavid Greenman 	} else {
935a316d390SJohn Dyson 		startpindex = pindex;
936317205caSDavid Greenman 	}
93726f9a767SRodney W. Grimes 
93826f9a767SRodney W. Grimes 	/*
9390d94caffSDavid Greenman 	 * scan forward for the read ahead pages -- in memory or on disk not
9400d94caffSDavid Greenman 	 * in same object
94126f9a767SRodney W. Grimes 	 */
942a316d390SJohn Dyson 	tpindex = pindex + 1;
943a316d390SJohn Dyson 	endpindex = pindex + (rahead + 1);
944a316d390SJohn Dyson 	if (endpindex > object->size)
945a316d390SJohn Dyson 		endpindex = object->size;
946a316d390SJohn Dyson 	while (tpindex <  endpindex) {
947a316d390SJohn Dyson 		if ( vm_page_lookup(object, tpindex)) {
94826f9a767SRodney W. Grimes 			break;
94926f9a767SRodney W. Grimes 		}
950a316d390SJohn Dyson 		tpindex += 1;
95126f9a767SRodney W. Grimes 	}
952a316d390SJohn Dyson 	endpindex = tpindex;
95326f9a767SRodney W. Grimes 
95426f9a767SRodney W. Grimes 	/* calculate number of bytes of pages */
955a316d390SJohn Dyson 	size = endpindex - startpindex;
95626f9a767SRodney W. Grimes 
95726f9a767SRodney W. Grimes 	/* calculate the page offset of the required page */
958a316d390SJohn Dyson 	treqpage = pindex - startpindex;
95926f9a767SRodney W. Grimes 
96026f9a767SRodney W. Grimes 	/* see if we have space (again) */
96122ba64e8SJohn Dyson 	if ((cnt.v_free_count + cnt.v_cache_count) >
96222ba64e8SJohn Dyson 		(cnt.v_free_reserved + size)) {
96326f9a767SRodney W. Grimes 		/*
96426f9a767SRodney W. Grimes 		 * get our pages and don't block for them
96526f9a767SRodney W. Grimes 		 */
96626f9a767SRodney W. Grimes 		for (i = 0; i < size; i++) {
967170db9c6SJohn Dyson 			if (i != treqpage) {
968ccbb2f72SJohn Dyson 				rtm = vm_page_alloc(object,
969a316d390SJohn Dyson 					startpindex + i,
97022ba64e8SJohn Dyson 					VM_ALLOC_NORMAL);
971ccbb2f72SJohn Dyson 				if (rtm == NULL) {
972170db9c6SJohn Dyson 					if (i < treqpage) {
973ccbb2f72SJohn Dyson 						int j;
974ccbb2f72SJohn Dyson 						for (j = 0; j < i; j++) {
97521bf3904SJohn Dyson 							FREE_PAGE(marray[j]);
97626f9a767SRodney W. Grimes 						}
97726f9a767SRodney W. Grimes 						*reqpage = 0;
97826f9a767SRodney W. Grimes 						marray[0] = m;
97926f9a767SRodney W. Grimes 						return 1;
980ccbb2f72SJohn Dyson 					} else {
981ccbb2f72SJohn Dyson 						size = i;
982ccbb2f72SJohn Dyson 						*reqpage = treqpage;
983ccbb2f72SJohn Dyson 						return size;
984ccbb2f72SJohn Dyson 					}
985ccbb2f72SJohn Dyson 				}
986ccbb2f72SJohn Dyson 				marray[i] = rtm;
987ccbb2f72SJohn Dyson 			} else {
988ccbb2f72SJohn Dyson 				marray[i] = m;
989ccbb2f72SJohn Dyson 			}
99026f9a767SRodney W. Grimes 		}
991170db9c6SJohn Dyson 
99226f9a767SRodney W. Grimes 		*reqpage = treqpage;
99326f9a767SRodney W. Grimes 		return size;
99426f9a767SRodney W. Grimes 	}
99526f9a767SRodney W. Grimes 	*reqpage = 0;
99626f9a767SRodney W. Grimes 	marray[0] = m;
99726f9a767SRodney W. Grimes 	return 1;
99826f9a767SRodney W. Grimes }
999