xref: /freebsd/sys/vm/vm_fault.c (revision be9968363742a7b369819f598be0d2f08f1c92ab)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
426f9a767SRodney W. Grimes  * Copyright (c) 1994 John S. Dyson
526f9a767SRodney W. Grimes  * All rights reserved.
626f9a767SRodney W. Grimes  * Copyright (c) 1994 David Greenman
726f9a767SRodney W. Grimes  * All rights reserved.
826f9a767SRodney W. Grimes  *
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
11df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
12df8bae1dSRodney W. Grimes  *
13df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
14df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
15df8bae1dSRodney W. Grimes  * are met:
16df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
17df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
18df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
19df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
20df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
21df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
225929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
23df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
24df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
25df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
26df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
27df8bae1dSRodney W. Grimes  *    without specific prior written permission.
28df8bae1dSRodney W. Grimes  *
29df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
40df8bae1dSRodney W. Grimes  *
413c4dd356SDavid Greenman  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42df8bae1dSRodney W. Grimes  *
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45df8bae1dSRodney W. Grimes  * All rights reserved.
46df8bae1dSRodney W. Grimes  *
47df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48df8bae1dSRodney W. Grimes  *
49df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
50df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
51df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
52df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
53df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
54df8bae1dSRodney W. Grimes  *
55df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62df8bae1dSRodney W. Grimes  *  School of Computer Science
63df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
64df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
65df8bae1dSRodney W. Grimes  *
66df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
67df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
68df8bae1dSRodney W. Grimes  */
69df8bae1dSRodney W. Grimes 
70df8bae1dSRodney W. Grimes /*
71df8bae1dSRodney W. Grimes  *	Page fault handling module.
72df8bae1dSRodney W. Grimes  */
73874651b1SDavid E. O'Brien 
74874651b1SDavid E. O'Brien #include <sys/cdefs.h>
75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
76874651b1SDavid E. O'Brien 
7735818d2eSJohn Baldwin #include "opt_ktrace.h"
78f8a47341SAlan Cox #include "opt_vm.h"
79f8a47341SAlan Cox 
80df8bae1dSRodney W. Grimes #include <sys/param.h>
81df8bae1dSRodney W. Grimes #include <sys/systm.h>
824edf4a58SJohn Baldwin #include <sys/kernel.h>
83fb919e4dSMark Murray #include <sys/lock.h>
8426f9a767SRodney W. Grimes #include <sys/proc.h>
8526f9a767SRodney W. Grimes #include <sys/resourcevar.h>
8689f6b863SAttilio Rao #include <sys/rwlock.h>
8723955314SAlfred Perlstein #include <sys/sysctl.h>
884edf4a58SJohn Baldwin #include <sys/vmmeter.h>
894edf4a58SJohn Baldwin #include <sys/vnode.h>
9035818d2eSJohn Baldwin #ifdef KTRACE
9135818d2eSJohn Baldwin #include <sys/ktrace.h>
9235818d2eSJohn Baldwin #endif
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes #include <vm/vm.h>
95efeaf95aSDavid Greenman #include <vm/vm_param.h>
96efeaf95aSDavid Greenman #include <vm/pmap.h>
97efeaf95aSDavid Greenman #include <vm/vm_map.h>
98efeaf95aSDavid Greenman #include <vm/vm_object.h>
99df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
100df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h>
101a83c285cSDavid Greenman #include <vm/vm_kern.h>
10224a1cce3SDavid Greenman #include <vm/vm_pager.h>
103efeaf95aSDavid Greenman #include <vm/vm_extern.h>
104df8bae1dSRodney W. Grimes 
105566526a9SAlan Cox #define PFBAK 4
106566526a9SAlan Cox #define PFFOR 4
107566526a9SAlan Cox #define PAGEORDER_SIZE (PFBAK+PFFOR)
108566526a9SAlan Cox 
109566526a9SAlan Cox static int prefault_pageorder[] = {
110566526a9SAlan Cox 	-1 * PAGE_SIZE, 1 * PAGE_SIZE,
111566526a9SAlan Cox 	-2 * PAGE_SIZE, 2 * PAGE_SIZE,
112566526a9SAlan Cox 	-3 * PAGE_SIZE, 3 * PAGE_SIZE,
113566526a9SAlan Cox 	-4 * PAGE_SIZE, 4 * PAGE_SIZE
114566526a9SAlan Cox };
115566526a9SAlan Cox 
11611caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
117566526a9SAlan Cox static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
11826f9a767SRodney W. Grimes 
11913458803SAlan Cox #define	VM_FAULT_READ_BEHIND	8
12013458803SAlan Cox #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
12113458803SAlan Cox #define	VM_FAULT_NINCR		(VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
12213458803SAlan Cox #define	VM_FAULT_SUM		(VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
12313458803SAlan Cox #define	VM_FAULT_CACHE_BEHIND	(VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
12426f9a767SRodney W. Grimes 
1254866e085SJohn Dyson struct faultstate {
1264866e085SJohn Dyson 	vm_page_t m;
1274866e085SJohn Dyson 	vm_object_t object;
1284866e085SJohn Dyson 	vm_pindex_t pindex;
1294866e085SJohn Dyson 	vm_page_t first_m;
1304866e085SJohn Dyson 	vm_object_t	first_object;
1314866e085SJohn Dyson 	vm_pindex_t first_pindex;
1324866e085SJohn Dyson 	vm_map_t map;
1334866e085SJohn Dyson 	vm_map_entry_t entry;
13425adb370SBrian Feldman 	int lookup_still_valid;
1354866e085SJohn Dyson 	struct vnode *vp;
1364866e085SJohn Dyson };
1374866e085SJohn Dyson 
13813458803SAlan Cox static void vm_fault_cache_behind(const struct faultstate *fs, int distance);
13913458803SAlan Cox 
14062a59e8fSWarner Losh static inline void
1414866e085SJohn Dyson release_page(struct faultstate *fs)
1424866e085SJohn Dyson {
1430d0be82aSKonstantin Belousov 
144e69763a3SDoug Rabson 	vm_page_wakeup(fs->m);
1452965a453SKip Macy 	vm_page_lock(fs->m);
1464866e085SJohn Dyson 	vm_page_deactivate(fs->m);
1472965a453SKip Macy 	vm_page_unlock(fs->m);
1484866e085SJohn Dyson 	fs->m = NULL;
1494866e085SJohn Dyson }
1504866e085SJohn Dyson 
15162a59e8fSWarner Losh static inline void
1524866e085SJohn Dyson unlock_map(struct faultstate *fs)
1534866e085SJohn Dyson {
1540d0be82aSKonstantin Belousov 
15525adb370SBrian Feldman 	if (fs->lookup_still_valid) {
1564866e085SJohn Dyson 		vm_map_lookup_done(fs->map, fs->entry);
15725adb370SBrian Feldman 		fs->lookup_still_valid = FALSE;
1584866e085SJohn Dyson 	}
1594866e085SJohn Dyson }
1604866e085SJohn Dyson 
1614866e085SJohn Dyson static void
162a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs)
1634866e085SJohn Dyson {
164f29ba63eSAlan Cox 
1654866e085SJohn Dyson 	vm_object_pip_wakeup(fs->object);
16689f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs->object);
1674866e085SJohn Dyson 	if (fs->object != fs->first_object) {
16889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(fs->first_object);
1692965a453SKip Macy 		vm_page_lock(fs->first_m);
1704866e085SJohn Dyson 		vm_page_free(fs->first_m);
1712965a453SKip Macy 		vm_page_unlock(fs->first_m);
1724866e085SJohn Dyson 		vm_object_pip_wakeup(fs->first_object);
17389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(fs->first_object);
1744866e085SJohn Dyson 		fs->first_m = NULL;
1754866e085SJohn Dyson 	}
1764866e085SJohn Dyson 	vm_object_deallocate(fs->first_object);
1774866e085SJohn Dyson 	unlock_map(fs);
1784866e085SJohn Dyson 	if (fs->vp != NULL) {
1790cddd8f0SMatthew Dillon 		vput(fs->vp);
1804866e085SJohn Dyson 		fs->vp = NULL;
1814866e085SJohn Dyson 	}
1824866e085SJohn Dyson }
1834866e085SJohn Dyson 
184df8bae1dSRodney W. Grimes /*
18540360b1bSMatthew Dillon  * TRYPAGER - used by vm_fault to calculate whether the pager for the
18640360b1bSMatthew Dillon  *	      current object *might* contain the page.
18740360b1bSMatthew Dillon  *
18840360b1bSMatthew Dillon  *	      default objects are zero-fill, there is no real pager.
18940360b1bSMatthew Dillon  */
19040360b1bSMatthew Dillon #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
1912db65ab4SAlan Cox 			((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
19240360b1bSMatthew Dillon 
19340360b1bSMatthew Dillon /*
194df8bae1dSRodney W. Grimes  *	vm_fault:
195df8bae1dSRodney W. Grimes  *
196956f3135SPhilippe Charnier  *	Handle a page fault occurring at the given address,
197df8bae1dSRodney W. Grimes  *	requiring the given permissions, in the map specified.
198df8bae1dSRodney W. Grimes  *	If successful, the page is inserted into the
199df8bae1dSRodney W. Grimes  *	associated physical map.
200df8bae1dSRodney W. Grimes  *
201df8bae1dSRodney W. Grimes  *	NOTE: the given address should be truncated to the
202df8bae1dSRodney W. Grimes  *	proper page address.
203df8bae1dSRodney W. Grimes  *
204df8bae1dSRodney W. Grimes  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
205df8bae1dSRodney W. Grimes  *	a standard error specifying why the fault is fatal is returned.
206df8bae1dSRodney W. Grimes  *
207df8bae1dSRodney W. Grimes  *	The map in question must be referenced, and remains so.
2080cddd8f0SMatthew Dillon  *	Caller may hold no locks.
209df8bae1dSRodney W. Grimes  */
210df8bae1dSRodney W. Grimes int
21123955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
21223955314SAlfred Perlstein     int fault_flags)
21323955314SAlfred Perlstein {
21435818d2eSJohn Baldwin 	struct thread *td;
21535818d2eSJohn Baldwin 	int result;
216acd11c74SAlan Cox 
21735818d2eSJohn Baldwin 	td = curthread;
21835818d2eSJohn Baldwin 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
2192801687dSKonstantin Belousov 		return (KERN_PROTECTION_FAILURE);
22035818d2eSJohn Baldwin #ifdef KTRACE
22135818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
22235818d2eSJohn Baldwin 		ktrfault(vaddr, fault_type);
22335818d2eSJohn Baldwin #endif
224*be996836SAttilio Rao 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
225*be996836SAttilio Rao 	    NULL);
22635818d2eSJohn Baldwin #ifdef KTRACE
22735818d2eSJohn Baldwin 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
22835818d2eSJohn Baldwin 		ktrfaultend(result);
22935818d2eSJohn Baldwin #endif
23035818d2eSJohn Baldwin 	return (result);
231acd11c74SAlan Cox }
232acd11c74SAlan Cox 
233acd11c74SAlan Cox int
234*be996836SAttilio Rao vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
235acd11c74SAlan Cox     int fault_flags, vm_page_t *m_hold)
236acd11c74SAlan Cox {
237df8bae1dSRodney W. Grimes 	vm_prot_t prot;
23813458803SAlan Cox 	long ahead, behind;
23913458803SAlan Cox 	int alloc_req, era, faultcount, nera, reqpage, result;
24013458803SAlan Cox 	boolean_t growstack, is_first_object_locked, wired;
2412d8acc0fSJohn Dyson 	int map_generation;
242df8bae1dSRodney W. Grimes 	vm_object_t next_object;
24313458803SAlan Cox 	vm_page_t marray[VM_FAULT_READ_MAX];
2444866e085SJohn Dyson 	int hardfault;
2454866e085SJohn Dyson 	struct faultstate fs;
246d2bf64c3SKonstantin Belousov 	struct vnode *vp;
247d2bf64c3SKonstantin Belousov 	int locked, error;
248df8bae1dSRodney W. Grimes 
2494866e085SJohn Dyson 	hardfault = 0;
2506139043bSAlan Cox 	growstack = TRUE;
25167596082SAttilio Rao 	PCPU_INC(cnt.v_vm_faults);
252d2bf64c3SKonstantin Belousov 	fs.vp = NULL;
25313458803SAlan Cox 	faultcount = reqpage = 0;
254df8bae1dSRodney W. Grimes 
255df8bae1dSRodney W. Grimes RetryFault:;
256df8bae1dSRodney W. Grimes 
257df8bae1dSRodney W. Grimes 	/*
2580d94caffSDavid Greenman 	 * Find the backing store object and offset into it to begin the
2590d94caffSDavid Greenman 	 * search.
260df8bae1dSRodney W. Grimes 	 */
26140360b1bSMatthew Dillon 	fs.map = map;
26292de35b0SAlan Cox 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
26392de35b0SAlan Cox 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
26492de35b0SAlan Cox 	if (result != KERN_SUCCESS) {
2656139043bSAlan Cox 		if (growstack && result == KERN_INVALID_ADDRESS &&
2662db65ab4SAlan Cox 		    map != kernel_map) {
2676139043bSAlan Cox 			result = vm_map_growstack(curproc, vaddr);
268a976eb5eSAlan Cox 			if (result != KERN_SUCCESS)
2696139043bSAlan Cox 				return (KERN_FAILURE);
2706139043bSAlan Cox 			growstack = FALSE;
2716139043bSAlan Cox 			goto RetryFault;
2726139043bSAlan Cox 		}
27392de35b0SAlan Cox 		return (result);
27409e0c6ccSJohn Dyson 	}
27509e0c6ccSJohn Dyson 
2764866e085SJohn Dyson 	map_generation = fs.map->timestamp;
2772d8acc0fSJohn Dyson 
2784866e085SJohn Dyson 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
27947221757SJohn Dyson 		panic("vm_fault: fault on nofault entry, addr: %lx",
28092c4c4ebSBruce Evans 		    (u_long)vaddr);
2817aaaa4fdSJohn Dyson 	}
2827aaaa4fdSJohn Dyson 
2834f9c9114SKonstantin Belousov 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
2844f9c9114SKonstantin Belousov 	    fs.entry->wiring_thread != curthread) {
2854f9c9114SKonstantin Belousov 		vm_map_unlock_read(fs.map);
2864f9c9114SKonstantin Belousov 		vm_map_lock(fs.map);
2874f9c9114SKonstantin Belousov 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
2884f9c9114SKonstantin Belousov 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
2894f9c9114SKonstantin Belousov 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
2904f9c9114SKonstantin Belousov 			vm_map_unlock_and_wait(fs.map, 0);
2914f9c9114SKonstantin Belousov 		} else
2924f9c9114SKonstantin Belousov 			vm_map_unlock(fs.map);
2934f9c9114SKonstantin Belousov 		goto RetryFault;
2944f9c9114SKonstantin Belousov 	}
2954f9c9114SKonstantin Belousov 
29695e5e988SJohn Dyson 	/*
29795e5e988SJohn Dyson 	 * Make a reference to this object to prevent its disposal while we
29895e5e988SJohn Dyson 	 * are messing with it.  Once we have the reference, the map is free
29995e5e988SJohn Dyson 	 * to be diddled.  Since objects reference their shadows (and copies),
30095e5e988SJohn Dyson 	 * they will stay around as well.
301fe8e0238SMatthew Dillon 	 *
302fe8e0238SMatthew Dillon 	 * Bump the paging-in-progress count to prevent size changes (e.g.
303fe8e0238SMatthew Dillon 	 * truncation operations) during I/O.  This must be done after
304fe8e0238SMatthew Dillon 	 * obtaining the vnode lock in order to avoid possible deadlocks.
30595e5e988SJohn Dyson 	 */
30689f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.first_object);
307a976eb5eSAlan Cox 	vm_object_reference_locked(fs.first_object);
308d474eaaaSDoug Rabson 	vm_object_pip_add(fs.first_object, 1);
30995e5e988SJohn Dyson 
31025adb370SBrian Feldman 	fs.lookup_still_valid = TRUE;
311df8bae1dSRodney W. Grimes 
312df8bae1dSRodney W. Grimes 	if (wired)
313d8778512SAlan Cox 		fault_type = prot | (fault_type & VM_PROT_COPY);
314df8bae1dSRodney W. Grimes 
3154866e085SJohn Dyson 	fs.first_m = NULL;
316df8bae1dSRodney W. Grimes 
317df8bae1dSRodney W. Grimes 	/*
318df8bae1dSRodney W. Grimes 	 * Search for the page at object/offset.
319df8bae1dSRodney W. Grimes 	 */
3204866e085SJohn Dyson 	fs.object = fs.first_object;
3214866e085SJohn Dyson 	fs.pindex = fs.first_pindex;
322df8bae1dSRodney W. Grimes 	while (TRUE) {
3231c7c3c6aSMatthew Dillon 		/*
3241c7c3c6aSMatthew Dillon 		 * If the object is dead, we stop here
3251c7c3c6aSMatthew Dillon 		 */
3264866e085SJohn Dyson 		if (fs.object->flags & OBJ_DEAD) {
3274866e085SJohn Dyson 			unlock_and_deallocate(&fs);
32847221757SJohn Dyson 			return (KERN_PROTECTION_FAILURE);
32947221757SJohn Dyson 		}
33047221757SJohn Dyson 
3311c7c3c6aSMatthew Dillon 		/*
3321c7c3c6aSMatthew Dillon 		 * See if page is resident
3331c7c3c6aSMatthew Dillon 		 */
3344866e085SJohn Dyson 		fs.m = vm_page_lookup(fs.object, fs.pindex);
3354866e085SJohn Dyson 		if (fs.m != NULL) {
33698cb733cSKenneth D. Merry 			/*
3379b80d344SKenneth D. Merry 			 * check for page-based copy on write.
3389b80d344SKenneth D. Merry 			 * We check fs.object == fs.first_object so
3399b80d344SKenneth D. Merry 			 * as to ensure the legacy COW mechanism is
3409b80d344SKenneth D. Merry 			 * used when the page in question is part of
3419b80d344SKenneth D. Merry 			 * a shadow object.  Otherwise, vm_page_cowfault()
3429b80d344SKenneth D. Merry 			 * removes the page from the backing object,
3439b80d344SKenneth D. Merry 			 * which is not what we want.
34498cb733cSKenneth D. Merry 			 */
3452965a453SKip Macy 			vm_page_lock(fs.m);
34698cb733cSKenneth D. Merry 			if ((fs.m->cow) &&
3479b80d344SKenneth D. Merry 			    (fault_type & VM_PROT_WRITE) &&
3489b80d344SKenneth D. Merry 			    (fs.object == fs.first_object)) {
34998cb733cSKenneth D. Merry 				vm_page_cowfault(fs.m);
350d18e8afeSAlan Cox 				unlock_and_deallocate(&fs);
35198cb733cSKenneth D. Merry 				goto RetryFault;
35298cb733cSKenneth D. Merry 			}
35398cb733cSKenneth D. Merry 
354df8bae1dSRodney W. Grimes 			/*
3551c7c3c6aSMatthew Dillon 			 * Wait/Retry if the page is busy.  We have to do this
3569af80719SAlan Cox 			 * if the page is busy via either VPO_BUSY or
3571c7c3c6aSMatthew Dillon 			 * vm_page_t->busy because the vm_pager may be using
3581c7c3c6aSMatthew Dillon 			 * vm_page_t->busy for pageouts ( and even pageins if
3591c7c3c6aSMatthew Dillon 			 * it is the vnode pager ), and we could end up trying
360956f3135SPhilippe Charnier 			 * to pagein and pageout the same page simultaneously.
3611c7c3c6aSMatthew Dillon 			 *
3621c7c3c6aSMatthew Dillon 			 * We can theoretically allow the busy case on a read
3631c7c3c6aSMatthew Dillon 			 * fault if the page is marked valid, but since such
3641c7c3c6aSMatthew Dillon 			 * pages are typically already pmap'd, putting that
3651c7c3c6aSMatthew Dillon 			 * special case in might be more effort then it is
3661c7c3c6aSMatthew Dillon 			 * worth.  We cannot under any circumstances mess
3671c7c3c6aSMatthew Dillon 			 * around with a vm_page_t->busy page except, perhaps,
3681c7c3c6aSMatthew Dillon 			 * to pmap it.
369df8bae1dSRodney W. Grimes 			 */
3709af80719SAlan Cox 			if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
371b88b6c9dSAlan Cox 				/*
372b88b6c9dSAlan Cox 				 * Reference the page before unlocking and
373b88b6c9dSAlan Cox 				 * sleeping so that the page daemon is less
374b88b6c9dSAlan Cox 				 * likely to reclaim it.
375b88b6c9dSAlan Cox 				 */
3763407fefeSKonstantin Belousov 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
3772965a453SKip Macy 				vm_page_unlock(fs.m);
378a51b0840SAlan Cox 				if (fs.object != fs.first_object) {
37989f6b863SAttilio Rao 					if (!VM_OBJECT_TRYWLOCK(
380a6e38685SKonstantin Belousov 					    fs.first_object)) {
38189f6b863SAttilio Rao 						VM_OBJECT_WUNLOCK(fs.object);
38289f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.first_object);
38389f6b863SAttilio Rao 						VM_OBJECT_WLOCK(fs.object);
384a6e38685SKonstantin Belousov 					}
3852965a453SKip Macy 					vm_page_lock(fs.first_m);
386a51b0840SAlan Cox 					vm_page_free(fs.first_m);
3872965a453SKip Macy 					vm_page_unlock(fs.first_m);
388a51b0840SAlan Cox 					vm_object_pip_wakeup(fs.first_object);
38989f6b863SAttilio Rao 					VM_OBJECT_WUNLOCK(fs.first_object);
390a51b0840SAlan Cox 					fs.first_m = NULL;
391a51b0840SAlan Cox 				}
392a51b0840SAlan Cox 				unlock_map(&fs);
393a51b0840SAlan Cox 				if (fs.m == vm_page_lookup(fs.object,
394a51b0840SAlan Cox 				    fs.pindex)) {
395e7e56b28SAlan Cox 					vm_page_sleep_if_busy(fs.m, TRUE,
396e7e56b28SAlan Cox 					    "vmpfw");
397a51b0840SAlan Cox 				}
398a51b0840SAlan Cox 				vm_object_pip_wakeup(fs.object);
39989f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
40067596082SAttilio Rao 				PCPU_INC(cnt.v_intrans);
4014866e085SJohn Dyson 				vm_object_deallocate(fs.first_object);
402df8bae1dSRodney W. Grimes 				goto RetryFault;
403df8bae1dSRodney W. Grimes 			}
4048d220203SAlan Cox 			vm_page_remque(fs.m);
4052965a453SKip Macy 			vm_page_unlock(fs.m);
4067615edaaSMatthew Dillon 
4071c7c3c6aSMatthew Dillon 			/*
4081c7c3c6aSMatthew Dillon 			 * Mark page busy for other processes, and the
4091c7c3c6aSMatthew Dillon 			 * pagedaemon.  If it still isn't completely valid
4101c7c3c6aSMatthew Dillon 			 * (readable), jump to readrest, else break-out ( we
4111c7c3c6aSMatthew Dillon 			 * found the page ).
4121c7c3c6aSMatthew Dillon 			 */
413e69763a3SDoug Rabson 			vm_page_busy(fs.m);
414ff5958e7SAlan Cox 			if (fs.m->valid != VM_PAGE_BITS_ALL)
4150d94caffSDavid Greenman 				goto readrest;
416df8bae1dSRodney W. Grimes 			break;
417df8bae1dSRodney W. Grimes 		}
4181c7c3c6aSMatthew Dillon 
4191c7c3c6aSMatthew Dillon 		/*
42040360b1bSMatthew Dillon 		 * Page is not resident, If this is the search termination
42140360b1bSMatthew Dillon 		 * or the pager might contain the page, allocate a new page.
4221c7c3c6aSMatthew Dillon 		 */
42340360b1bSMatthew Dillon 		if (TRYPAGER || fs.object == fs.first_object) {
4244866e085SJohn Dyson 			if (fs.pindex >= fs.object->size) {
4254866e085SJohn Dyson 				unlock_and_deallocate(&fs);
4265f55e841SDavid Greenman 				return (KERN_PROTECTION_FAILURE);
4275f55e841SDavid Greenman 			}
42822ba64e8SJohn Dyson 
429df8bae1dSRodney W. Grimes 			/*
4300d94caffSDavid Greenman 			 * Allocate a new page for this object/offset pair.
4313f1c4c4fSKonstantin Belousov 			 *
4323f1c4c4fSKonstantin Belousov 			 * Unlocked read of the p_flag is harmless. At
4333f1c4c4fSKonstantin Belousov 			 * worst, the P_KILLED might be not observed
4343f1c4c4fSKonstantin Belousov 			 * there, and allocation can fail, causing
4353f1c4c4fSKonstantin Belousov 			 * restart and new reading of the p_flag.
436df8bae1dSRodney W. Grimes 			 */
43740360b1bSMatthew Dillon 			fs.m = NULL;
4383f1c4c4fSKonstantin Belousov 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
439f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
440f8a47341SAlan Cox 				if ((fs.object->flags & OBJ_COLORED) == 0) {
441f8a47341SAlan Cox 					fs.object->flags |= OBJ_COLORED;
442f8a47341SAlan Cox 					fs.object->pg_color = atop(vaddr) -
443f8a47341SAlan Cox 					    fs.pindex;
444f8a47341SAlan Cox 				}
445f8a47341SAlan Cox #endif
4463f1c4c4fSKonstantin Belousov 				alloc_req = P_KILLED(curproc) ?
4473f1c4c4fSKonstantin Belousov 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
4483f1c4c4fSKonstantin Belousov 				if (fs.object->type != OBJT_VNODE &&
4493f1c4c4fSKonstantin Belousov 				    fs.object->backing_object == NULL)
4503f1c4c4fSKonstantin Belousov 					alloc_req |= VM_ALLOC_ZERO;
4514866e085SJohn Dyson 				fs.m = vm_page_alloc(fs.object, fs.pindex,
4523f1c4c4fSKonstantin Belousov 				    alloc_req);
45340360b1bSMatthew Dillon 			}
4544866e085SJohn Dyson 			if (fs.m == NULL) {
4554866e085SJohn Dyson 				unlock_and_deallocate(&fs);
456ef6020d1SMike Silbersack 				VM_WAITPFAULT;
457df8bae1dSRodney W. Grimes 				goto RetryFault;
4580a2e596aSAlan Cox 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
4594ab8ab92SKonstantin Belousov 				break;
460df8bae1dSRodney W. Grimes 		}
46147221757SJohn Dyson 
4620d94caffSDavid Greenman readrest:
4631c7c3c6aSMatthew Dillon 		/*
46440360b1bSMatthew Dillon 		 * We have found a valid page or we have allocated a new page.
46540360b1bSMatthew Dillon 		 * The page thus may not be valid or may not be entirely
46640360b1bSMatthew Dillon 		 * valid.
46740360b1bSMatthew Dillon 		 *
46840360b1bSMatthew Dillon 		 * Attempt to fault-in the page if there is a chance that the
46940360b1bSMatthew Dillon 		 * pager has it, and potentially fault in additional pages
47040360b1bSMatthew Dillon 		 * at the same time.
4711c7c3c6aSMatthew Dillon 		 */
47240360b1bSMatthew Dillon 		if (TRYPAGER) {
473df8bae1dSRodney W. Grimes 			int rv;
4747f866e4bSAlan Cox 			u_char behavior = vm_map_entry_behavior(fs.entry);
475867a482dSJohn Dyson 
4763f1c4c4fSKonstantin Belousov 			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
4773f1c4c4fSKonstantin Belousov 			    P_KILLED(curproc)) {
478867a482dSJohn Dyson 				behind = 0;
47913458803SAlan Cox 				ahead = 0;
48013458803SAlan Cox 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
48113458803SAlan Cox 				behind = 0;
48213458803SAlan Cox 				ahead = atop(fs.entry->end - vaddr) - 1;
48313458803SAlan Cox 				if (ahead > VM_FAULT_READ_AHEAD_MAX)
48413458803SAlan Cox 					ahead = VM_FAULT_READ_AHEAD_MAX;
48513458803SAlan Cox 				if (fs.pindex == fs.entry->next_read)
48613458803SAlan Cox 					vm_fault_cache_behind(&fs,
48713458803SAlan Cox 					    VM_FAULT_READ_MAX);
4882d8acc0fSJohn Dyson 			} else {
48913458803SAlan Cox 				/*
49013458803SAlan Cox 				 * If this is a sequential page fault, then
49113458803SAlan Cox 				 * arithmetically increase the number of pages
49213458803SAlan Cox 				 * in the read-ahead window.  Otherwise, reset
49313458803SAlan Cox 				 * the read-ahead window to its smallest size.
49413458803SAlan Cox 				 */
49513458803SAlan Cox 				behind = atop(vaddr - fs.entry->start);
4962d8acc0fSJohn Dyson 				if (behind > VM_FAULT_READ_BEHIND)
4972d8acc0fSJohn Dyson 					behind = VM_FAULT_READ_BEHIND;
49813458803SAlan Cox 				ahead = atop(fs.entry->end - vaddr) - 1;
49913458803SAlan Cox 				era = fs.entry->read_ahead;
50013458803SAlan Cox 				if (fs.pindex == fs.entry->next_read) {
50113458803SAlan Cox 					nera = era + behind;
50213458803SAlan Cox 					if (nera > VM_FAULT_READ_AHEAD_MAX)
50313458803SAlan Cox 						nera = VM_FAULT_READ_AHEAD_MAX;
504867a482dSJohn Dyson 					behind = 0;
50513458803SAlan Cox 					if (ahead > nera)
50613458803SAlan Cox 						ahead = nera;
50713458803SAlan Cox 					if (era == VM_FAULT_READ_AHEAD_MAX)
50813458803SAlan Cox 						vm_fault_cache_behind(&fs,
50913458803SAlan Cox 						    VM_FAULT_CACHE_BEHIND);
51013458803SAlan Cox 				} else if (ahead > VM_FAULT_READ_AHEAD_MIN)
51113458803SAlan Cox 					ahead = VM_FAULT_READ_AHEAD_MIN;
51213458803SAlan Cox 				if (era != ahead)
51313458803SAlan Cox 					fs.entry->read_ahead = ahead;
514867a482dSJohn Dyson 			}
515d2bf64c3SKonstantin Belousov 
516d2bf64c3SKonstantin Belousov 			/*
517d2bf64c3SKonstantin Belousov 			 * Call the pager to retrieve the data, if any, after
518d2bf64c3SKonstantin Belousov 			 * releasing the lock on the map.  We hold a ref on
519d2bf64c3SKonstantin Belousov 			 * fs.object and the pages are VPO_BUSY'd.
520d2bf64c3SKonstantin Belousov 			 */
521d2bf64c3SKonstantin Belousov 			unlock_map(&fs);
522d2bf64c3SKonstantin Belousov 
523d2bf64c3SKonstantin Belousov 			if (fs.object->type == OBJT_VNODE) {
524d2bf64c3SKonstantin Belousov 				vp = fs.object->handle;
525d2bf64c3SKonstantin Belousov 				if (vp == fs.vp)
526d2bf64c3SKonstantin Belousov 					goto vnode_locked;
527d2bf64c3SKonstantin Belousov 				else if (fs.vp != NULL) {
528d2bf64c3SKonstantin Belousov 					vput(fs.vp);
529d2bf64c3SKonstantin Belousov 					fs.vp = NULL;
530d2bf64c3SKonstantin Belousov 				}
531d2bf64c3SKonstantin Belousov 				locked = VOP_ISLOCKED(vp);
532d2bf64c3SKonstantin Belousov 
533d2bf64c3SKonstantin Belousov 				if (locked != LK_EXCLUSIVE)
534d2bf64c3SKonstantin Belousov 					locked = LK_SHARED;
535d2bf64c3SKonstantin Belousov 				/* Do not sleep for vnode lock while fs.m is busy */
536d2bf64c3SKonstantin Belousov 				error = vget(vp, locked | LK_CANRECURSE |
537d2bf64c3SKonstantin Belousov 				    LK_NOWAIT, curthread);
538d2bf64c3SKonstantin Belousov 				if (error != 0) {
539d2bf64c3SKonstantin Belousov 					vhold(vp);
540d2bf64c3SKonstantin Belousov 					release_page(&fs);
541d2bf64c3SKonstantin Belousov 					unlock_and_deallocate(&fs);
542d2bf64c3SKonstantin Belousov 					error = vget(vp, locked | LK_RETRY |
543d2bf64c3SKonstantin Belousov 					    LK_CANRECURSE, curthread);
544d2bf64c3SKonstantin Belousov 					vdrop(vp);
545d2bf64c3SKonstantin Belousov 					fs.vp = vp;
546d2bf64c3SKonstantin Belousov 					KASSERT(error == 0,
547d2bf64c3SKonstantin Belousov 					    ("vm_fault: vget failed"));
548d2bf64c3SKonstantin Belousov 					goto RetryFault;
549d2bf64c3SKonstantin Belousov 				}
550d2bf64c3SKonstantin Belousov 				fs.vp = vp;
551d2bf64c3SKonstantin Belousov 			}
552d2bf64c3SKonstantin Belousov vnode_locked:
553d2bf64c3SKonstantin Belousov 			KASSERT(fs.vp == NULL || !fs.map->system_map,
554d2bf64c3SKonstantin Belousov 			    ("vm_fault: vnode-backed object mapped by system map"));
555d2bf64c3SKonstantin Belousov 
556df8bae1dSRodney W. Grimes 			/*
5570d94caffSDavid Greenman 			 * now we find out if any other pages should be paged
5580d94caffSDavid Greenman 			 * in at this time this routine checks to see if the
5590d94caffSDavid Greenman 			 * pages surrounding this fault reside in the same
5600d94caffSDavid Greenman 			 * object as the page for this fault.  If they do,
5610d94caffSDavid Greenman 			 * then they are faulted in also into the object.  The
5620d94caffSDavid Greenman 			 * array "marray" returned contains an array of
5630d94caffSDavid Greenman 			 * vm_page_t structs where one of them is the
5640d94caffSDavid Greenman 			 * vm_page_t passed to the routine.  The reqpage
5650d94caffSDavid Greenman 			 * return value is the index into the marray for the
5660d94caffSDavid Greenman 			 * vm_page_t passed to the routine.
5671c7c3c6aSMatthew Dillon 			 *
5689af80719SAlan Cox 			 * fs.m plus the additional pages are VPO_BUSY'd.
56926f9a767SRodney W. Grimes 			 */
57005f0fdd2SPoul-Henning Kamp 			faultcount = vm_fault_additional_pages(
5714866e085SJohn Dyson 			    fs.m, behind, ahead, marray, &reqpage);
572df8bae1dSRodney W. Grimes 
57326f9a767SRodney W. Grimes 			rv = faultcount ?
5744866e085SJohn Dyson 			    vm_pager_get_pages(fs.object, marray, faultcount,
57524a1cce3SDavid Greenman 				reqpage) : VM_PAGER_FAIL;
57622ba64e8SJohn Dyson 
57726f9a767SRodney W. Grimes 			if (rv == VM_PAGER_OK) {
578df8bae1dSRodney W. Grimes 				/*
579f230c45cSJohn Dyson 				 * Found the page. Leave it busy while we play
580f230c45cSJohn Dyson 				 * with it.
581f230c45cSJohn Dyson 				 */
582f230c45cSJohn Dyson 
583f230c45cSJohn Dyson 				/*
5840d94caffSDavid Greenman 				 * Relookup in case pager changed page. Pager
5850d94caffSDavid Greenman 				 * is responsible for disposition of old page
5860d94caffSDavid Greenman 				 * if moved.
587df8bae1dSRodney W. Grimes 				 */
5884866e085SJohn Dyson 				fs.m = vm_page_lookup(fs.object, fs.pindex);
5894866e085SJohn Dyson 				if (!fs.m) {
5904866e085SJohn Dyson 					unlock_and_deallocate(&fs);
591f6b04d2bSDavid Greenman 					goto RetryFault;
592f6b04d2bSDavid Greenman 				}
593f6b04d2bSDavid Greenman 
59426f9a767SRodney W. Grimes 				hardfault++;
5951c7c3c6aSMatthew Dillon 				break; /* break to PAGE HAS BEEN FOUND */
596df8bae1dSRodney W. Grimes 			}
597df8bae1dSRodney W. Grimes 			/*
5980d94caffSDavid Greenman 			 * Remove the bogus page (which does not exist at this
5990d94caffSDavid Greenman 			 * object/offset); before doing so, we must get back
6000d94caffSDavid Greenman 			 * our object lock to preserve our invariant.
601df8bae1dSRodney W. Grimes 			 *
60224a1cce3SDavid Greenman 			 * Also wake up any other process that may want to bring
6030d94caffSDavid Greenman 			 * in this page.
604df8bae1dSRodney W. Grimes 			 *
6050d94caffSDavid Greenman 			 * If this is the top-level object, we must leave the
60624a1cce3SDavid Greenman 			 * busy page to prevent another process from rushing
6070d94caffSDavid Greenman 			 * past us, and inserting the page in that object at
6080d94caffSDavid Greenman 			 * the same time that we are.
609df8bae1dSRodney W. Grimes 			 */
610a83c285cSDavid Greenman 			if (rv == VM_PAGER_ERROR)
611f3679e35SDavid Greenman 				printf("vm_fault: pager read error, pid %d (%s)\n",
612f3679e35SDavid Greenman 				    curproc->p_pid, curproc->p_comm);
61326f9a767SRodney W. Grimes 			/*
614a83c285cSDavid Greenman 			 * Data outside the range of the pager or an I/O error
61526f9a767SRodney W. Grimes 			 */
616a83c285cSDavid Greenman 			/*
6170d94caffSDavid Greenman 			 * XXX - the check for kernel_map is a kludge to work
6180d94caffSDavid Greenman 			 * around having the machine panic on a kernel space
6190d94caffSDavid Greenman 			 * fault w/ I/O error.
620a83c285cSDavid Greenman 			 */
6214866e085SJohn Dyson 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
62247221757SJohn Dyson 				(rv == VM_PAGER_BAD)) {
6232965a453SKip Macy 				vm_page_lock(fs.m);
6244866e085SJohn Dyson 				vm_page_free(fs.m);
6252965a453SKip Macy 				vm_page_unlock(fs.m);
6264866e085SJohn Dyson 				fs.m = NULL;
6274866e085SJohn Dyson 				unlock_and_deallocate(&fs);
628a83c285cSDavid Greenman 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
62926f9a767SRodney W. Grimes 			}
6304866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6312965a453SKip Macy 				vm_page_lock(fs.m);
6324866e085SJohn Dyson 				vm_page_free(fs.m);
6332965a453SKip Macy 				vm_page_unlock(fs.m);
6344866e085SJohn Dyson 				fs.m = NULL;
63526f9a767SRodney W. Grimes 				/*
63626f9a767SRodney W. Grimes 				 * XXX - we cannot just fall out at this
63726f9a767SRodney W. Grimes 				 * point, m has been freed and is invalid!
63826f9a767SRodney W. Grimes 				 */
639df8bae1dSRodney W. Grimes 			}
640df8bae1dSRodney W. Grimes 		}
64140360b1bSMatthew Dillon 
642df8bae1dSRodney W. Grimes 		/*
6431c7c3c6aSMatthew Dillon 		 * We get here if the object has default pager (or unwiring)
6441c7c3c6aSMatthew Dillon 		 * or the pager doesn't have the page.
645df8bae1dSRodney W. Grimes 		 */
6464866e085SJohn Dyson 		if (fs.object == fs.first_object)
6474866e085SJohn Dyson 			fs.first_m = fs.m;
648df8bae1dSRodney W. Grimes 
649df8bae1dSRodney W. Grimes 		/*
6500d94caffSDavid Greenman 		 * Move on to the next object.  Lock the next object before
6510d94caffSDavid Greenman 		 * unlocking the current one.
652df8bae1dSRodney W. Grimes 		 */
6534866e085SJohn Dyson 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
6544866e085SJohn Dyson 		next_object = fs.object->backing_object;
655df8bae1dSRodney W. Grimes 		if (next_object == NULL) {
656df8bae1dSRodney W. Grimes 			/*
6570d94caffSDavid Greenman 			 * If there's no object left, fill the page in the top
6580d94caffSDavid Greenman 			 * object with zeros.
659df8bae1dSRodney W. Grimes 			 */
6604866e085SJohn Dyson 			if (fs.object != fs.first_object) {
6614866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
66289f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(fs.object);
663df8bae1dSRodney W. Grimes 
6644866e085SJohn Dyson 				fs.object = fs.first_object;
6654866e085SJohn Dyson 				fs.pindex = fs.first_pindex;
6664866e085SJohn Dyson 				fs.m = fs.first_m;
66789f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
668df8bae1dSRodney W. Grimes 			}
6694866e085SJohn Dyson 			fs.first_m = NULL;
670df8bae1dSRodney W. Grimes 
6714221e284SAlan Cox 			/*
6724221e284SAlan Cox 			 * Zero the page if necessary and mark it valid.
6734221e284SAlan Cox 			 */
6744866e085SJohn Dyson 			if ((fs.m->flags & PG_ZERO) == 0) {
675fff6062aSAlan Cox 				pmap_zero_page(fs.m);
6764221e284SAlan Cox 			} else {
67767596082SAttilio Rao 				PCPU_INC(cnt.v_ozfod);
6784221e284SAlan Cox 			}
67967596082SAttilio Rao 			PCPU_INC(cnt.v_zfod);
6804221e284SAlan Cox 			fs.m->valid = VM_PAGE_BITS_ALL;
6811c7c3c6aSMatthew Dillon 			break;	/* break to PAGE HAS BEEN FOUND */
6820d94caffSDavid Greenman 		} else {
683c8567c3aSAlan Cox 			KASSERT(fs.object != next_object,
684c8567c3aSAlan Cox 			    ("object loop %p", next_object));
68589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(next_object);
686c8567c3aSAlan Cox 			vm_object_pip_add(next_object, 1);
687c8567c3aSAlan Cox 			if (fs.object != fs.first_object)
6884866e085SJohn Dyson 				vm_object_pip_wakeup(fs.object);
68989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
6904866e085SJohn Dyson 			fs.object = next_object;
691df8bae1dSRodney W. Grimes 		}
692df8bae1dSRodney W. Grimes 	}
6931c7c3c6aSMatthew Dillon 
6949af80719SAlan Cox 	KASSERT((fs.m->oflags & VPO_BUSY) != 0,
6955526d2d9SEivind Eklund 	    ("vm_fault: not busy after main loop"));
696df8bae1dSRodney W. Grimes 
697df8bae1dSRodney W. Grimes 	/*
6980d94caffSDavid Greenman 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
699df8bae1dSRodney W. Grimes 	 * is held.]
700df8bae1dSRodney W. Grimes 	 */
701df8bae1dSRodney W. Grimes 
702df8bae1dSRodney W. Grimes 	/*
7030d94caffSDavid Greenman 	 * If the page is being written, but isn't already owned by the
7040d94caffSDavid Greenman 	 * top-level object, we have to copy it into a new page owned by the
7050d94caffSDavid Greenman 	 * top-level object.
706df8bae1dSRodney W. Grimes 	 */
7074866e085SJohn Dyson 	if (fs.object != fs.first_object) {
708df8bae1dSRodney W. Grimes 		/*
7090d94caffSDavid Greenman 		 * We only really need to copy if we want to write it.
710df8bae1dSRodney W. Grimes 		 */
711a6d42a0dSAlan Cox 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
712df8bae1dSRodney W. Grimes 			/*
7131c7c3c6aSMatthew Dillon 			 * This allows pages to be virtually copied from a
7141c7c3c6aSMatthew Dillon 			 * backing_object into the first_object, where the
7151c7c3c6aSMatthew Dillon 			 * backing object has no other refs to it, and cannot
7161c7c3c6aSMatthew Dillon 			 * gain any more refs.  Instead of a bcopy, we just
7171c7c3c6aSMatthew Dillon 			 * move the page from the backing object to the
7181c7c3c6aSMatthew Dillon 			 * first object.  Note that we must mark the page
7191c7c3c6aSMatthew Dillon 			 * dirty in the first object so that it will go out
7201c7c3c6aSMatthew Dillon 			 * to swap when needed.
721df8bae1dSRodney W. Grimes 			 */
722ebf75125SAlan Cox 			is_first_object_locked = FALSE;
723e50346b5SAlan Cox 			if (
724de5f6a77SJohn Dyson 				/*
725de5f6a77SJohn Dyson 				 * Only one shadow object
726de5f6a77SJohn Dyson 				 */
7274866e085SJohn Dyson 				(fs.object->shadow_count == 1) &&
728de5f6a77SJohn Dyson 				/*
729de5f6a77SJohn Dyson 				 * No COW refs, except us
730de5f6a77SJohn Dyson 				 */
7314866e085SJohn Dyson 				(fs.object->ref_count == 1) &&
732de5f6a77SJohn Dyson 				/*
7335929bcfaSPhilippe Charnier 				 * No one else can look this object up
734de5f6a77SJohn Dyson 				 */
7354866e085SJohn Dyson 				(fs.object->handle == NULL) &&
736de5f6a77SJohn Dyson 				/*
737de5f6a77SJohn Dyson 				 * No other ways to look the object up
738de5f6a77SJohn Dyson 				 */
7394866e085SJohn Dyson 				((fs.object->type == OBJT_DEFAULT) ||
7404866e085SJohn Dyson 				 (fs.object->type == OBJT_SWAP)) &&
74189f6b863SAttilio Rao 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
742de5f6a77SJohn Dyson 				/*
743de5f6a77SJohn Dyson 				 * We don't chase down the shadow chain
744de5f6a77SJohn Dyson 				 */
745e50346b5SAlan Cox 			    fs.object == fs.first_object->backing_object) {
7462d8acc0fSJohn Dyson 				/*
747de5f6a77SJohn Dyson 				 * get rid of the unnecessary page
748df8bae1dSRodney W. Grimes 				 */
749eb00b276SAlan Cox 				vm_page_lock(fs.first_m);
7504866e085SJohn Dyson 				vm_page_free(fs.first_m);
7512965a453SKip Macy 				vm_page_unlock(fs.first_m);
752de5f6a77SJohn Dyson 				/*
7531c7c3c6aSMatthew Dillon 				 * grab the page and put it into the
7541c7c3c6aSMatthew Dillon 				 * process'es object.  The page is
7551c7c3c6aSMatthew Dillon 				 * automatically made dirty.
756de5f6a77SJohn Dyson 				 */
7572965a453SKip Macy 				vm_page_lock(fs.m);
7584866e085SJohn Dyson 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
7592965a453SKip Macy 				vm_page_unlock(fs.m);
760768131d2SAlan Cox 				vm_page_busy(fs.m);
761d98ddc46SAlan Cox 				fs.first_m = fs.m;
7624866e085SJohn Dyson 				fs.m = NULL;
76367596082SAttilio Rao 				PCPU_INC(cnt.v_cow_optim);
764de5f6a77SJohn Dyson 			} else {
765de5f6a77SJohn Dyson 				/*
766de5f6a77SJohn Dyson 				 * Oh, well, lets copy it.
767de5f6a77SJohn Dyson 				 */
768669890eaSAlan Cox 				pmap_copy_page(fs.m, fs.first_m);
769669890eaSAlan Cox 				fs.first_m->valid = VM_PAGE_BITS_ALL;
770d8778512SAlan Cox 				if (wired && (fault_flags &
771d8778512SAlan Cox 				    VM_FAULT_CHANGE_WIRING) == 0) {
7722965a453SKip Macy 					vm_page_lock(fs.first_m);
773d8778512SAlan Cox 					vm_page_wire(fs.first_m);
7742965a453SKip Macy 					vm_page_unlock(fs.first_m);
7752965a453SKip Macy 
7762965a453SKip Macy 					vm_page_lock(fs.m);
777d8778512SAlan Cox 					vm_page_unwire(fs.m, FALSE);
7782965a453SKip Macy 					vm_page_unlock(fs.m);
779de5f6a77SJohn Dyson 				}
780df8bae1dSRodney W. Grimes 				/*
781df8bae1dSRodney W. Grimes 				 * We no longer need the old page or object.
782df8bae1dSRodney W. Grimes 				 */
7834866e085SJohn Dyson 				release_page(&fs);
784de5f6a77SJohn Dyson 			}
7851c7c3c6aSMatthew Dillon 			/*
7861c7c3c6aSMatthew Dillon 			 * fs.object != fs.first_object due to above
7871c7c3c6aSMatthew Dillon 			 * conditional
7881c7c3c6aSMatthew Dillon 			 */
7894866e085SJohn Dyson 			vm_object_pip_wakeup(fs.object);
79089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(fs.object);
791df8bae1dSRodney W. Grimes 			/*
792df8bae1dSRodney W. Grimes 			 * Only use the new page below...
793df8bae1dSRodney W. Grimes 			 */
7944866e085SJohn Dyson 			fs.object = fs.first_object;
7954866e085SJohn Dyson 			fs.pindex = fs.first_pindex;
796d98ddc46SAlan Cox 			fs.m = fs.first_m;
797f29ba63eSAlan Cox 			if (!is_first_object_locked)
79889f6b863SAttilio Rao 				VM_OBJECT_WLOCK(fs.object);
79967596082SAttilio Rao 			PCPU_INC(cnt.v_cow_faults);
8004d34e019SKonstantin Belousov 			curthread->td_cow++;
8010d94caffSDavid Greenman 		} else {
802df8bae1dSRodney W. Grimes 			prot &= ~VM_PROT_WRITE;
803df8bae1dSRodney W. Grimes 		}
804df8bae1dSRodney W. Grimes 	}
805df8bae1dSRodney W. Grimes 
806df8bae1dSRodney W. Grimes 	/*
8070d94caffSDavid Greenman 	 * We must verify that the maps have not changed since our last
8080d94caffSDavid Greenman 	 * lookup.
809df8bae1dSRodney W. Grimes 	 */
81019dc5607STor Egge 	if (!fs.lookup_still_valid) {
811df8bae1dSRodney W. Grimes 		vm_object_t retry_object;
812a316d390SJohn Dyson 		vm_pindex_t retry_pindex;
813df8bae1dSRodney W. Grimes 		vm_prot_t retry_prot;
814df8bae1dSRodney W. Grimes 
81519dc5607STor Egge 		if (!vm_map_trylock_read(fs.map)) {
816b823bbd6SMatthew Dillon 			release_page(&fs);
817b823bbd6SMatthew Dillon 			unlock_and_deallocate(&fs);
818b823bbd6SMatthew Dillon 			goto RetryFault;
819b823bbd6SMatthew Dillon 		}
82019dc5607STor Egge 		fs.lookup_still_valid = TRUE;
82119dc5607STor Egge 		if (fs.map->timestamp != map_generation) {
82219dc5607STor Egge 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
8234866e085SJohn Dyson 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
824df8bae1dSRodney W. Grimes 
825df8bae1dSRodney W. Grimes 			/*
82644ed3417STor Egge 			 * If we don't need the page any longer, put it on the inactive
8270d94caffSDavid Greenman 			 * list (the easiest thing to do here).  If no one needs it,
8280d94caffSDavid Greenman 			 * pageout will grab it eventually.
829df8bae1dSRodney W. Grimes 			 */
830df8bae1dSRodney W. Grimes 			if (result != KERN_SUCCESS) {
8314866e085SJohn Dyson 				release_page(&fs);
8324866e085SJohn Dyson 				unlock_and_deallocate(&fs);
83319dc5607STor Egge 
83419dc5607STor Egge 				/*
83519dc5607STor Egge 				 * If retry of map lookup would have blocked then
83619dc5607STor Egge 				 * retry fault from start.
83719dc5607STor Egge 				 */
83819dc5607STor Egge 				if (result == KERN_FAILURE)
83919dc5607STor Egge 					goto RetryFault;
840df8bae1dSRodney W. Grimes 				return (result);
841df8bae1dSRodney W. Grimes 			}
8424866e085SJohn Dyson 			if ((retry_object != fs.first_object) ||
8434866e085SJohn Dyson 			    (retry_pindex != fs.first_pindex)) {
8444866e085SJohn Dyson 				release_page(&fs);
8454866e085SJohn Dyson 				unlock_and_deallocate(&fs);
846df8bae1dSRodney W. Grimes 				goto RetryFault;
847df8bae1dSRodney W. Grimes 			}
84819dc5607STor Egge 
849df8bae1dSRodney W. Grimes 			/*
8500d94caffSDavid Greenman 			 * Check whether the protection has changed or the object has
8510d94caffSDavid Greenman 			 * been copied while we left the map unlocked. Changing from
8520d94caffSDavid Greenman 			 * read to write permission is OK - we leave the page
8530d94caffSDavid Greenman 			 * write-protected, and catch the write fault. Changing from
8540d94caffSDavid Greenman 			 * write to read permission means that we can't mark the page
8550d94caffSDavid Greenman 			 * write-enabled after all.
856df8bae1dSRodney W. Grimes 			 */
857df8bae1dSRodney W. Grimes 			prot &= retry_prot;
858df8bae1dSRodney W. Grimes 		}
85919dc5607STor Egge 	}
860d2bf64c3SKonstantin Belousov 	/*
8615758fe71SAlan Cox 	 * If the page was filled by a pager, update the map entry's
8625758fe71SAlan Cox 	 * last read offset.  Since the pager does not return the
8635758fe71SAlan Cox 	 * actual set of pages that it read, this update is based on
8645758fe71SAlan Cox 	 * the requested set.  Typically, the requested and actual
8655758fe71SAlan Cox 	 * sets are the same.
866d2bf64c3SKonstantin Belousov 	 *
867d2bf64c3SKonstantin Belousov 	 * XXX The following assignment modifies the map
868d2bf64c3SKonstantin Belousov 	 * without holding a write lock on it.
869d2bf64c3SKonstantin Belousov 	 */
8705758fe71SAlan Cox 	if (hardfault)
87113458803SAlan Cox 		fs.entry->next_read = fs.pindex + faultcount - reqpage;
872d2bf64c3SKonstantin Belousov 
873acd11c74SAlan Cox 	if ((prot & VM_PROT_WRITE) != 0 ||
874acd11c74SAlan Cox 	    (fault_flags & VM_FAULT_DIRTY) != 0) {
875b146f9e5SAlan Cox 		vm_object_set_writeable_dirty(fs.object);
8764f79d873SMatthew Dillon 
8772ddba215SDavid Greenman 		/*
878b146f9e5SAlan Cox 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
8794f79d873SMatthew Dillon 		 * if the page is already dirty to prevent data written with
8804f79d873SMatthew Dillon 		 * the expectation of being synced from not being synced.
8814f79d873SMatthew Dillon 		 * Likewise if this entry does not request NOSYNC then make
8824f79d873SMatthew Dillon 		 * sure the page isn't marked NOSYNC.  Applications sharing
8834f79d873SMatthew Dillon 		 * data should use the same flags to avoid ping ponging.
8842ddba215SDavid Greenman 		 */
8854f79d873SMatthew Dillon 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
8864f79d873SMatthew Dillon 			if (fs.m->dirty == 0)
887b146f9e5SAlan Cox 				fs.m->oflags |= VPO_NOSYNC;
8884f79d873SMatthew Dillon 		} else {
889b146f9e5SAlan Cox 			fs.m->oflags &= ~VPO_NOSYNC;
8904f79d873SMatthew Dillon 		}
891e2997feaSAlan Cox 
892e2997feaSAlan Cox 		/*
893e2997feaSAlan Cox 		 * If the fault is a write, we know that this page is being
894e2997feaSAlan Cox 		 * written NOW so dirty it explicitly to save on
895e2997feaSAlan Cox 		 * pmap_is_modified() calls later.
896e2997feaSAlan Cox 		 *
897e2997feaSAlan Cox 		 * Also tell the backing pager, if any, that it should remove
898e2997feaSAlan Cox 		 * any swap backing since the page is now dirty.
899e2997feaSAlan Cox 		 */
900acd11c74SAlan Cox 		if (((fault_type & VM_PROT_WRITE) != 0 &&
901acd11c74SAlan Cox 		    (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
902acd11c74SAlan Cox 		    (fault_flags & VM_FAULT_DIRTY) != 0) {
9037dbf82dcSMatthew Dillon 			vm_page_dirty(fs.m);
9041c7c3c6aSMatthew Dillon 			vm_pager_page_unswapped(fs.m);
9052ddba215SDavid Greenman 		}
9062ddba215SDavid Greenman 	}
907f6b04d2bSDavid Greenman 
908bc6d84a6SMatthew Dillon 	/*
909bc6d84a6SMatthew Dillon 	 * Page had better still be busy
910bc6d84a6SMatthew Dillon 	 */
9119af80719SAlan Cox 	KASSERT(fs.m->oflags & VPO_BUSY,
912ca06c247SAlan Cox 		("vm_fault: page %p not busy!", fs.m));
9134221e284SAlan Cox 	/*
91478cfe1f7SAlan Cox 	 * Page must be completely valid or it is not fit to
9154221e284SAlan Cox 	 * map into user space.  vm_pager_get_pages() ensures this.
9164221e284SAlan Cox 	 */
91778cfe1f7SAlan Cox 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
91878cfe1f7SAlan Cox 	    ("vm_fault: page %p partially invalid", fs.m));
91989f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(fs.object);
920cbfbaad8SAlan Cox 
92186735996SAlan Cox 	/*
92286735996SAlan Cox 	 * Put this page into the physical map.  We had to do the unlock above
92386735996SAlan Cox 	 * because pmap_enter() may sleep.  We don't put the page
92486735996SAlan Cox 	 * back on the active queue until later so that the pageout daemon
92586735996SAlan Cox 	 * won't find it (yet).
92686735996SAlan Cox 	 */
927eb2a0517SAlan Cox 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
9282db65ab4SAlan Cox 	if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0)
929566526a9SAlan Cox 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
93089f6b863SAttilio Rao 	VM_OBJECT_WLOCK(fs.object);
9312965a453SKip Macy 	vm_page_lock(fs.m);
932ff97964aSJohn Dyson 
933df8bae1dSRodney W. Grimes 	/*
9340d94caffSDavid Greenman 	 * If the page is not wired down, then put it where the pageout daemon
9350d94caffSDavid Greenman 	 * can find it.
936df8bae1dSRodney W. Grimes 	 */
9372db65ab4SAlan Cox 	if (fault_flags & VM_FAULT_CHANGE_WIRING) {
938df8bae1dSRodney W. Grimes 		if (wired)
9394866e085SJohn Dyson 			vm_page_wire(fs.m);
940df8bae1dSRodney W. Grimes 		else
94173007561SDavid Greenman 			vm_page_unwire(fs.m, 1);
94203679e23SAlan Cox 	} else
9434866e085SJohn Dyson 		vm_page_activate(fs.m);
944acd11c74SAlan Cox 	if (m_hold != NULL) {
945acd11c74SAlan Cox 		*m_hold = fs.m;
946acd11c74SAlan Cox 		vm_page_hold(fs.m);
947acd11c74SAlan Cox 	}
9482965a453SKip Macy 	vm_page_unlock(fs.m);
94966bdd5d6SAlan Cox 	vm_page_wakeup(fs.m);
950eeec6babSJohn Baldwin 
951eebf3286SAlan Cox 	/*
952eebf3286SAlan Cox 	 * Unlock everything, and return
953eebf3286SAlan Cox 	 */
954eebf3286SAlan Cox 	unlock_and_deallocate(&fs);
955b3a01bdfSAndrey Zonov 	if (hardfault) {
956b3a01bdfSAndrey Zonov 		PCPU_INC(cnt.v_io_faults);
9571c4bcd05SJeff Roberson 		curthread->td_ru.ru_majflt++;
958b3a01bdfSAndrey Zonov 	} else
9591c4bcd05SJeff Roberson 		curthread->td_ru.ru_minflt++;
960df8bae1dSRodney W. Grimes 
961df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
962df8bae1dSRodney W. Grimes }
963df8bae1dSRodney W. Grimes 
964df8bae1dSRodney W. Grimes /*
96513458803SAlan Cox  * Speed up the reclamation of up to "distance" pages that precede the
96613458803SAlan Cox  * faulting pindex within the first object of the shadow chain.
96713458803SAlan Cox  */
96813458803SAlan Cox static void
96913458803SAlan Cox vm_fault_cache_behind(const struct faultstate *fs, int distance)
97013458803SAlan Cox {
97113458803SAlan Cox 	vm_object_t first_object, object;
97213458803SAlan Cox 	vm_page_t m, m_prev;
97313458803SAlan Cox 	vm_pindex_t pindex;
97413458803SAlan Cox 
97513458803SAlan Cox 	object = fs->object;
97689f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(object);
97713458803SAlan Cox 	first_object = fs->first_object;
97813458803SAlan Cox 	if (first_object != object) {
97989f6b863SAttilio Rao 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
98089f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
98189f6b863SAttilio Rao 			VM_OBJECT_WLOCK(first_object);
98289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
98313458803SAlan Cox 		}
98413458803SAlan Cox 	}
98528634820SAlan Cox 	/* Neither fictitious nor unmanaged pages can be cached. */
98628634820SAlan Cox 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
98713458803SAlan Cox 		if (fs->first_pindex < distance)
98813458803SAlan Cox 			pindex = 0;
98913458803SAlan Cox 		else
99013458803SAlan Cox 			pindex = fs->first_pindex - distance;
99113458803SAlan Cox 		if (pindex < OFF_TO_IDX(fs->entry->offset))
99213458803SAlan Cox 			pindex = OFF_TO_IDX(fs->entry->offset);
99313458803SAlan Cox 		m = first_object != object ? fs->first_m : fs->m;
99413458803SAlan Cox 		KASSERT((m->oflags & VPO_BUSY) != 0,
99513458803SAlan Cox 		    ("vm_fault_cache_behind: page %p is not busy", m));
99613458803SAlan Cox 		m_prev = vm_page_prev(m);
99713458803SAlan Cox 		while ((m = m_prev) != NULL && m->pindex >= pindex &&
99813458803SAlan Cox 		    m->valid == VM_PAGE_BITS_ALL) {
99913458803SAlan Cox 			m_prev = vm_page_prev(m);
100013458803SAlan Cox 			if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0)
100113458803SAlan Cox 				continue;
100213458803SAlan Cox 			vm_page_lock(m);
100313458803SAlan Cox 			if (m->hold_count == 0 && m->wire_count == 0) {
100413458803SAlan Cox 				pmap_remove_all(m);
100513458803SAlan Cox 				vm_page_aflag_clear(m, PGA_REFERENCED);
100613458803SAlan Cox 				if (m->dirty != 0)
100713458803SAlan Cox 					vm_page_deactivate(m);
100813458803SAlan Cox 				else
100913458803SAlan Cox 					vm_page_cache(m);
101013458803SAlan Cox 			}
101113458803SAlan Cox 			vm_page_unlock(m);
101213458803SAlan Cox 		}
101313458803SAlan Cox 	}
101413458803SAlan Cox 	if (first_object != object)
101589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(first_object);
101613458803SAlan Cox }
101713458803SAlan Cox 
101813458803SAlan Cox /*
1019566526a9SAlan Cox  * vm_fault_prefault provides a quick way of clustering
1020566526a9SAlan Cox  * pagefaults into a processes address space.  It is a "cousin"
1021566526a9SAlan Cox  * of vm_map_pmap_enter, except it runs at page fault time instead
1022566526a9SAlan Cox  * of mmap time.
1023566526a9SAlan Cox  */
1024566526a9SAlan Cox static void
1025566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
1026566526a9SAlan Cox {
1027566526a9SAlan Cox 	int i;
1028566526a9SAlan Cox 	vm_offset_t addr, starta;
1029566526a9SAlan Cox 	vm_pindex_t pindex;
10302053c127SStephan Uphoff 	vm_page_t m;
1031566526a9SAlan Cox 	vm_object_t object;
1032566526a9SAlan Cox 
1033950d5f7aSAlan Cox 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1034566526a9SAlan Cox 		return;
1035566526a9SAlan Cox 
1036566526a9SAlan Cox 	object = entry->object.vm_object;
1037566526a9SAlan Cox 
1038566526a9SAlan Cox 	starta = addra - PFBAK * PAGE_SIZE;
1039566526a9SAlan Cox 	if (starta < entry->start) {
1040566526a9SAlan Cox 		starta = entry->start;
1041566526a9SAlan Cox 	} else if (starta > addra) {
1042566526a9SAlan Cox 		starta = 0;
1043566526a9SAlan Cox 	}
1044566526a9SAlan Cox 
1045566526a9SAlan Cox 	for (i = 0; i < PAGEORDER_SIZE; i++) {
1046566526a9SAlan Cox 		vm_object_t backing_object, lobject;
1047566526a9SAlan Cox 
1048566526a9SAlan Cox 		addr = addra + prefault_pageorder[i];
1049566526a9SAlan Cox 		if (addr > addra + (PFFOR * PAGE_SIZE))
1050566526a9SAlan Cox 			addr = 0;
1051566526a9SAlan Cox 
1052566526a9SAlan Cox 		if (addr < starta || addr >= entry->end)
1053566526a9SAlan Cox 			continue;
1054566526a9SAlan Cox 
1055566526a9SAlan Cox 		if (!pmap_is_prefaultable(pmap, addr))
1056566526a9SAlan Cox 			continue;
1057566526a9SAlan Cox 
1058566526a9SAlan Cox 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1059566526a9SAlan Cox 		lobject = object;
1060c141ae7fSAlan Cox 		VM_OBJECT_RLOCK(lobject);
1061566526a9SAlan Cox 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1062566526a9SAlan Cox 		    lobject->type == OBJT_DEFAULT &&
1063566526a9SAlan Cox 		    (backing_object = lobject->backing_object) != NULL) {
106436930fc9SAlan Cox 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
106536930fc9SAlan Cox 			    0, ("vm_fault_prefault: unaligned object offset"));
1066566526a9SAlan Cox 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1067c141ae7fSAlan Cox 			VM_OBJECT_RLOCK(backing_object);
1068c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1069566526a9SAlan Cox 			lobject = backing_object;
1070566526a9SAlan Cox 		}
1071566526a9SAlan Cox 		/*
1072566526a9SAlan Cox 		 * give-up when a page is not in memory
1073566526a9SAlan Cox 		 */
1074cbfbaad8SAlan Cox 		if (m == NULL) {
1075c141ae7fSAlan Cox 			VM_OBJECT_RUNLOCK(lobject);
1076566526a9SAlan Cox 			break;
1077cbfbaad8SAlan Cox 		}
10780a2e596aSAlan Cox 		if (m->valid == VM_PAGE_BITS_ALL &&
10793c4a2440SAlan Cox 		    (m->flags & PG_FICTITIOUS) == 0)
10807bfda801SAlan Cox 			pmap_enter_quick(pmap, addr, m, entry->protection);
1081c141ae7fSAlan Cox 		VM_OBJECT_RUNLOCK(lobject);
1082566526a9SAlan Cox 	}
1083566526a9SAlan Cox }
1084566526a9SAlan Cox 
1085566526a9SAlan Cox /*
108682de724fSAlan Cox  * Hold each of the physical pages that are mapped by the specified range of
108782de724fSAlan Cox  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
108882de724fSAlan Cox  * and allow the specified types of access, "prot".  If all of the implied
108982de724fSAlan Cox  * pages are successfully held, then the number of held pages is returned
109082de724fSAlan Cox  * together with pointers to those pages in the array "ma".  However, if any
109182de724fSAlan Cox  * of the pages cannot be held, -1 is returned.
109282de724fSAlan Cox  */
109382de724fSAlan Cox int
109482de724fSAlan Cox vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
109582de724fSAlan Cox     vm_prot_t prot, vm_page_t *ma, int max_count)
109682de724fSAlan Cox {
109782de724fSAlan Cox 	vm_offset_t end, va;
109882de724fSAlan Cox 	vm_page_t *mp;
109982de724fSAlan Cox 	int count;
110082de724fSAlan Cox 	boolean_t pmap_failed;
110182de724fSAlan Cox 
1102af32c419SKonstantin Belousov 	if (len == 0)
1103af32c419SKonstantin Belousov 		return (0);
110482de724fSAlan Cox 	end = round_page(addr + len);
110582de724fSAlan Cox 	addr = trunc_page(addr);
110682de724fSAlan Cox 
110782de724fSAlan Cox 	/*
110882de724fSAlan Cox 	 * Check for illegal addresses.
110982de724fSAlan Cox 	 */
111082de724fSAlan Cox 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
111182de724fSAlan Cox 		return (-1);
111282de724fSAlan Cox 
111382de724fSAlan Cox 	count = howmany(end - addr, PAGE_SIZE);
111482de724fSAlan Cox 	if (count > max_count)
111582de724fSAlan Cox 		panic("vm_fault_quick_hold_pages: count > max_count");
111682de724fSAlan Cox 
111782de724fSAlan Cox 	/*
111882de724fSAlan Cox 	 * Most likely, the physical pages are resident in the pmap, so it is
111982de724fSAlan Cox 	 * faster to try pmap_extract_and_hold() first.
112082de724fSAlan Cox 	 */
112182de724fSAlan Cox 	pmap_failed = FALSE;
112282de724fSAlan Cox 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
112382de724fSAlan Cox 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
112482de724fSAlan Cox 		if (*mp == NULL)
112582de724fSAlan Cox 			pmap_failed = TRUE;
112682de724fSAlan Cox 		else if ((prot & VM_PROT_WRITE) != 0 &&
1127a5dbab54SAlan Cox 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
112882de724fSAlan Cox 			/*
112982de724fSAlan Cox 			 * Explicitly dirty the physical page.  Otherwise, the
113082de724fSAlan Cox 			 * caller's changes may go unnoticed because they are
113182de724fSAlan Cox 			 * performed through an unmanaged mapping or by a DMA
113282de724fSAlan Cox 			 * operation.
11333c76db4cSAlan Cox 			 *
1134abb9b935SKonstantin Belousov 			 * The object lock is not held here.
1135abb9b935SKonstantin Belousov 			 * See vm_page_clear_dirty_mask().
113682de724fSAlan Cox 			 */
11373c76db4cSAlan Cox 			vm_page_dirty(*mp);
113882de724fSAlan Cox 		}
113982de724fSAlan Cox 	}
114082de724fSAlan Cox 	if (pmap_failed) {
114182de724fSAlan Cox 		/*
114282de724fSAlan Cox 		 * One or more pages could not be held by the pmap.  Either no
114382de724fSAlan Cox 		 * page was mapped at the specified virtual address or that
114482de724fSAlan Cox 		 * mapping had insufficient permissions.  Attempt to fault in
114582de724fSAlan Cox 		 * and hold these pages.
114682de724fSAlan Cox 		 */
114782de724fSAlan Cox 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1148*be996836SAttilio Rao 			if (*mp == NULL && vm_fault_hold(map, va, prot,
114982de724fSAlan Cox 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
115082de724fSAlan Cox 				goto error;
115182de724fSAlan Cox 	}
115282de724fSAlan Cox 	return (count);
115382de724fSAlan Cox error:
115482de724fSAlan Cox 	for (mp = ma; mp < ma + count; mp++)
115582de724fSAlan Cox 		if (*mp != NULL) {
115682de724fSAlan Cox 			vm_page_lock(*mp);
115782de724fSAlan Cox 			vm_page_unhold(*mp);
115882de724fSAlan Cox 			vm_page_unlock(*mp);
115982de724fSAlan Cox 		}
116082de724fSAlan Cox 	return (-1);
116182de724fSAlan Cox }
116282de724fSAlan Cox 
116382de724fSAlan Cox /*
1164df8bae1dSRodney W. Grimes  *	vm_fault_wire:
1165df8bae1dSRodney W. Grimes  *
1166df8bae1dSRodney W. Grimes  *	Wire down a range of virtual addresses in a map.
1167df8bae1dSRodney W. Grimes  */
1168df8bae1dSRodney W. Grimes int
11694be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
11702db65ab4SAlan Cox     boolean_t fictitious)
1171df8bae1dSRodney W. Grimes {
117254d92145SMatthew Dillon 	vm_offset_t va;
1173df8bae1dSRodney W. Grimes 	int rv;
1174df8bae1dSRodney W. Grimes 
1175df8bae1dSRodney W. Grimes 	/*
11760d94caffSDavid Greenman 	 * We simulate a fault to get the page and enter it in the physical
1177ef594d31SAlan Cox 	 * map.  For user wiring, we only ask for read access on currently
1178ef594d31SAlan Cox 	 * read-only sections.
1179df8bae1dSRodney W. Grimes 	 */
1180df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
11812db65ab4SAlan Cox 		rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
11827aaaa4fdSJohn Dyson 		if (rv) {
11837aaaa4fdSJohn Dyson 			if (va != start)
11844be14af9SAlan Cox 				vm_fault_unwire(map, start, va, fictitious);
11857aaaa4fdSJohn Dyson 			return (rv);
11867aaaa4fdSJohn Dyson 		}
11877aaaa4fdSJohn Dyson 	}
11887aaaa4fdSJohn Dyson 	return (KERN_SUCCESS);
11897aaaa4fdSJohn Dyson }
11907aaaa4fdSJohn Dyson 
11917aaaa4fdSJohn Dyson /*
1192df8bae1dSRodney W. Grimes  *	vm_fault_unwire:
1193df8bae1dSRodney W. Grimes  *
1194df8bae1dSRodney W. Grimes  *	Unwire a range of virtual addresses in a map.
1195df8bae1dSRodney W. Grimes  */
119626f9a767SRodney W. Grimes void
11974be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
11984be14af9SAlan Cox     boolean_t fictitious)
1199df8bae1dSRodney W. Grimes {
1200227f9a1cSJake Burkholder 	vm_paddr_t pa;
1201227f9a1cSJake Burkholder 	vm_offset_t va;
1202e3ef0d2fSAlan Cox 	vm_page_t m;
120354d92145SMatthew Dillon 	pmap_t pmap;
1204df8bae1dSRodney W. Grimes 
1205df8bae1dSRodney W. Grimes 	pmap = vm_map_pmap(map);
1206df8bae1dSRodney W. Grimes 
1207df8bae1dSRodney W. Grimes 	/*
12080d94caffSDavid Greenman 	 * Since the pages are wired down, we must be able to get their
12090d94caffSDavid Greenman 	 * mappings from the physical map system.
1210df8bae1dSRodney W. Grimes 	 */
1211df8bae1dSRodney W. Grimes 	for (va = start; va < end; va += PAGE_SIZE) {
1212df8bae1dSRodney W. Grimes 		pa = pmap_extract(pmap, va);
1213227f9a1cSJake Burkholder 		if (pa != 0) {
1214df8bae1dSRodney W. Grimes 			pmap_change_wiring(pmap, va, FALSE);
12154be14af9SAlan Cox 			if (!fictitious) {
1216e3ef0d2fSAlan Cox 				m = PHYS_TO_VM_PAGE(pa);
1217e3ef0d2fSAlan Cox 				vm_page_lock(m);
1218e3ef0d2fSAlan Cox 				vm_page_unwire(m, TRUE);
1219e3ef0d2fSAlan Cox 				vm_page_unlock(m);
1220df8bae1dSRodney W. Grimes 			}
1221b18bfc3dSJohn Dyson 		}
12224be14af9SAlan Cox 	}
1223df8bae1dSRodney W. Grimes }
1224df8bae1dSRodney W. Grimes 
1225df8bae1dSRodney W. Grimes /*
1226df8bae1dSRodney W. Grimes  *	Routine:
1227df8bae1dSRodney W. Grimes  *		vm_fault_copy_entry
1228df8bae1dSRodney W. Grimes  *	Function:
1229210a6886SKonstantin Belousov  *		Create new shadow object backing dst_entry with private copy of
1230210a6886SKonstantin Belousov  *		all underlying pages. When src_entry is equal to dst_entry,
1231210a6886SKonstantin Belousov  *		function implements COW for wired-down map entry. Otherwise,
1232210a6886SKonstantin Belousov  *		it forks wired entry into dst_map.
1233df8bae1dSRodney W. Grimes  *
1234df8bae1dSRodney W. Grimes  *	In/out conditions:
1235df8bae1dSRodney W. Grimes  *		The source and destination maps must be locked for write.
1236df8bae1dSRodney W. Grimes  *		The source map entry must be wired down (or be a sharing map
1237df8bae1dSRodney W. Grimes  *		entry corresponding to a main map entry that is wired down).
1238df8bae1dSRodney W. Grimes  */
123926f9a767SRodney W. Grimes void
1240121fd461SKonstantin Belousov vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1241121fd461SKonstantin Belousov     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1242121fd461SKonstantin Belousov     vm_ooffset_t *fork_charge)
1243df8bae1dSRodney W. Grimes {
1244210a6886SKonstantin Belousov 	vm_object_t backing_object, dst_object, object, src_object;
12457afab86cSAlan Cox 	vm_pindex_t dst_pindex, pindex, src_pindex;
1246210a6886SKonstantin Belousov 	vm_prot_t access, prot;
1247df8bae1dSRodney W. Grimes 	vm_offset_t vaddr;
1248df8bae1dSRodney W. Grimes 	vm_page_t dst_m;
1249df8bae1dSRodney W. Grimes 	vm_page_t src_m;
1250210a6886SKonstantin Belousov 	boolean_t src_readonly, upgrade;
1251df8bae1dSRodney W. Grimes 
1252df8bae1dSRodney W. Grimes #ifdef	lint
1253df8bae1dSRodney W. Grimes 	src_map++;
12540d94caffSDavid Greenman #endif	/* lint */
1255df8bae1dSRodney W. Grimes 
1256210a6886SKonstantin Belousov 	upgrade = src_entry == dst_entry;
1257210a6886SKonstantin Belousov 
1258df8bae1dSRodney W. Grimes 	src_object = src_entry->object.vm_object;
12597afab86cSAlan Cox 	src_pindex = OFF_TO_IDX(src_entry->offset);
12607afab86cSAlan Cox 	src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1261df8bae1dSRodney W. Grimes 
1262df8bae1dSRodney W. Grimes 	/*
12630d94caffSDavid Greenman 	 * Create the top-level object for the destination entry. (Doesn't
12640d94caffSDavid Greenman 	 * actually shadow anything - we copy the pages directly.)
1265df8bae1dSRodney W. Grimes 	 */
126624a1cce3SDavid Greenman 	dst_object = vm_object_allocate(OBJT_DEFAULT,
126757b5187bSAlan Cox 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1268f8a47341SAlan Cox #if VM_NRESERVLEVEL > 0
1269f8a47341SAlan Cox 	dst_object->flags |= OBJ_COLORED;
1270f8a47341SAlan Cox 	dst_object->pg_color = atop(dst_entry->start);
1271f8a47341SAlan Cox #endif
1272df8bae1dSRodney W. Grimes 
127389f6b863SAttilio Rao 	VM_OBJECT_WLOCK(dst_object);
1274210a6886SKonstantin Belousov 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1275121fd461SKonstantin Belousov 	    ("vm_fault_copy_entry: vm_object not NULL"));
1276df8bae1dSRodney W. Grimes 	dst_entry->object.vm_object = dst_object;
1277df8bae1dSRodney W. Grimes 	dst_entry->offset = 0;
12783364c323SKonstantin Belousov 	dst_object->charge = dst_entry->end - dst_entry->start;
1279210a6886SKonstantin Belousov 	if (fork_charge != NULL) {
1280ef694c1aSEdward Tomasz Napierala 		KASSERT(dst_entry->cred == NULL,
1281121fd461SKonstantin Belousov 		    ("vm_fault_copy_entry: leaked swp charge"));
1282ef694c1aSEdward Tomasz Napierala 		dst_object->cred = curthread->td_ucred;
1283ef694c1aSEdward Tomasz Napierala 		crhold(dst_object->cred);
1284121fd461SKonstantin Belousov 		*fork_charge += dst_object->charge;
1285210a6886SKonstantin Belousov 	} else {
1286ef694c1aSEdward Tomasz Napierala 		dst_object->cred = dst_entry->cred;
1287ef694c1aSEdward Tomasz Napierala 		dst_entry->cred = NULL;
1288210a6886SKonstantin Belousov 	}
1289e4ed417aSAlan Cox 	access = prot = dst_entry->protection;
1290210a6886SKonstantin Belousov 	/*
1291210a6886SKonstantin Belousov 	 * If not an upgrade, then enter the mappings in the pmap as
1292210a6886SKonstantin Belousov 	 * read and/or execute accesses.  Otherwise, enter them as
1293210a6886SKonstantin Belousov 	 * write accesses.
1294210a6886SKonstantin Belousov 	 *
1295210a6886SKonstantin Belousov 	 * A writeable large page mapping is only created if all of
1296210a6886SKonstantin Belousov 	 * the constituent small page mappings are modified. Marking
1297210a6886SKonstantin Belousov 	 * PTEs as modified on inception allows promotion to happen
1298210a6886SKonstantin Belousov 	 * without taking potentially large number of soft faults.
1299210a6886SKonstantin Belousov 	 */
1300210a6886SKonstantin Belousov 	if (!upgrade)
1301210a6886SKonstantin Belousov 		access &= ~VM_PROT_WRITE;
1302df8bae1dSRodney W. Grimes 
1303df8bae1dSRodney W. Grimes 	/*
1304ef45823eSKonstantin Belousov 	 * Loop through all of the virtual pages within the entry's
1305ef45823eSKonstantin Belousov 	 * range, copying each page from the source object to the
1306ef45823eSKonstantin Belousov 	 * destination object.  Since the source is wired, those pages
1307ef45823eSKonstantin Belousov 	 * must exist.  In contrast, the destination is pageable.
1308ef45823eSKonstantin Belousov 	 * Since the destination object does share any backing storage
1309ef45823eSKonstantin Belousov 	 * with the source object, all of its pages must be dirtied,
1310ef45823eSKonstantin Belousov 	 * regardless of whether they can be written.
1311df8bae1dSRodney W. Grimes 	 */
13127afab86cSAlan Cox 	for (vaddr = dst_entry->start, dst_pindex = 0;
1313df8bae1dSRodney W. Grimes 	    vaddr < dst_entry->end;
13147afab86cSAlan Cox 	    vaddr += PAGE_SIZE, dst_pindex++) {
1315df8bae1dSRodney W. Grimes 
1316df8bae1dSRodney W. Grimes 		/*
13177afab86cSAlan Cox 		 * Allocate a page in the destination object.
1318df8bae1dSRodney W. Grimes 		 */
1319df8bae1dSRodney W. Grimes 		do {
13207afab86cSAlan Cox 			dst_m = vm_page_alloc(dst_object, dst_pindex,
13217afab86cSAlan Cox 			    VM_ALLOC_NORMAL);
1322df8bae1dSRodney W. Grimes 			if (dst_m == NULL) {
132389f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(dst_object);
1324df8bae1dSRodney W. Grimes 				VM_WAIT;
132589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(dst_object);
1326df8bae1dSRodney W. Grimes 			}
1327df8bae1dSRodney W. Grimes 		} while (dst_m == NULL);
1328df8bae1dSRodney W. Grimes 
1329df8bae1dSRodney W. Grimes 		/*
1330df8bae1dSRodney W. Grimes 		 * Find the page in the source object, and copy it in.
13310d94caffSDavid Greenman 		 * (Because the source is wired down, the page will be in
13320d94caffSDavid Greenman 		 * memory.)
1333df8bae1dSRodney W. Grimes 		 */
133483b375eaSAttilio Rao 		VM_OBJECT_RLOCK(src_object);
1335c5b65a67SAlan Cox 		object = src_object;
13367afab86cSAlan Cox 		pindex = src_pindex + dst_pindex;
13377afab86cSAlan Cox 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
13387afab86cSAlan Cox 		    src_readonly &&
1339c5b65a67SAlan Cox 		    (backing_object = object->backing_object) != NULL) {
1340c5b65a67SAlan Cox 			/*
1341c5b65a67SAlan Cox 			 * Allow fallback to backing objects if we are reading.
1342c5b65a67SAlan Cox 			 */
134383b375eaSAttilio Rao 			VM_OBJECT_RLOCK(backing_object);
1344c5b65a67SAlan Cox 			pindex += OFF_TO_IDX(object->backing_object_offset);
134583b375eaSAttilio Rao 			VM_OBJECT_RUNLOCK(object);
1346c5b65a67SAlan Cox 			object = backing_object;
1347c5b65a67SAlan Cox 		}
1348df8bae1dSRodney W. Grimes 		if (src_m == NULL)
1349df8bae1dSRodney W. Grimes 			panic("vm_fault_copy_wired: page missing");
1350669890eaSAlan Cox 		pmap_copy_page(src_m, dst_m);
135183b375eaSAttilio Rao 		VM_OBJECT_RUNLOCK(object);
1352669890eaSAlan Cox 		dst_m->valid = VM_PAGE_BITS_ALL;
1353bc79b37fSKonstantin Belousov 		dst_m->dirty = VM_PAGE_BITS_ALL;
135489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(dst_object);
1355df8bae1dSRodney W. Grimes 
1356df8bae1dSRodney W. Grimes 		/*
1357210a6886SKonstantin Belousov 		 * Enter it in the pmap. If a wired, copy-on-write
1358210a6886SKonstantin Belousov 		 * mapping is being replaced by a write-enabled
1359210a6886SKonstantin Belousov 		 * mapping, then wire that new mapping.
1360df8bae1dSRodney W. Grimes 		 */
1361210a6886SKonstantin Belousov 		pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1362df8bae1dSRodney W. Grimes 
1363df8bae1dSRodney W. Grimes 		/*
1364df8bae1dSRodney W. Grimes 		 * Mark it no longer busy, and put it on the active list.
1365df8bae1dSRodney W. Grimes 		 */
136689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(dst_object);
13672965a453SKip Macy 
1368210a6886SKonstantin Belousov 		if (upgrade) {
13692965a453SKip Macy 			vm_page_lock(src_m);
1370210a6886SKonstantin Belousov 			vm_page_unwire(src_m, 0);
1371e20e8c15SKonstantin Belousov 			vm_page_unlock(src_m);
13722965a453SKip Macy 
13732965a453SKip Macy 			vm_page_lock(dst_m);
1374210a6886SKonstantin Belousov 			vm_page_wire(dst_m);
1375e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
13762965a453SKip Macy 		} else {
13772965a453SKip Macy 			vm_page_lock(dst_m);
1378df8bae1dSRodney W. Grimes 			vm_page_activate(dst_m);
1379e20e8c15SKonstantin Belousov 			vm_page_unlock(dst_m);
13802965a453SKip Macy 		}
138166bdd5d6SAlan Cox 		vm_page_wakeup(dst_m);
1382df8bae1dSRodney W. Grimes 	}
138389f6b863SAttilio Rao 	VM_OBJECT_WUNLOCK(dst_object);
1384210a6886SKonstantin Belousov 	if (upgrade) {
1385210a6886SKonstantin Belousov 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1386210a6886SKonstantin Belousov 		vm_object_deallocate(src_object);
1387210a6886SKonstantin Belousov 	}
1388df8bae1dSRodney W. Grimes }
138926f9a767SRodney W. Grimes 
139026f9a767SRodney W. Grimes 
139126f9a767SRodney W. Grimes /*
139226f9a767SRodney W. Grimes  * This routine checks around the requested page for other pages that
139322ba64e8SJohn Dyson  * might be able to be faulted in.  This routine brackets the viable
139422ba64e8SJohn Dyson  * pages for the pages to be paged in.
139526f9a767SRodney W. Grimes  *
139626f9a767SRodney W. Grimes  * Inputs:
139722ba64e8SJohn Dyson  *	m, rbehind, rahead
139826f9a767SRodney W. Grimes  *
139926f9a767SRodney W. Grimes  * Outputs:
140026f9a767SRodney W. Grimes  *  marray (array of vm_page_t), reqpage (index of requested page)
140126f9a767SRodney W. Grimes  *
140226f9a767SRodney W. Grimes  * Return value:
140326f9a767SRodney W. Grimes  *  number of pages in marray
140426f9a767SRodney W. Grimes  */
1405303b270bSEivind Eklund static int
140622ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
140726f9a767SRodney W. Grimes 	vm_page_t m;
140826f9a767SRodney W. Grimes 	int rbehind;
140922ba64e8SJohn Dyson 	int rahead;
141026f9a767SRodney W. Grimes 	vm_page_t *marray;
141126f9a767SRodney W. Grimes 	int *reqpage;
141226f9a767SRodney W. Grimes {
14132d8acc0fSJohn Dyson 	int i,j;
141426f9a767SRodney W. Grimes 	vm_object_t object;
1415a316d390SJohn Dyson 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
141626f9a767SRodney W. Grimes 	vm_page_t rtm;
1417170db9c6SJohn Dyson 	int cbehind, cahead;
141826f9a767SRodney W. Grimes 
141989f6b863SAttilio Rao 	VM_OBJECT_ASSERT_WLOCKED(m->object);
142023955314SAlfred Perlstein 
142126f9a767SRodney W. Grimes 	object = m->object;
1422a316d390SJohn Dyson 	pindex = m->pindex;
1423fcdd9721SPawel Jakub Dawidek 	cbehind = cahead = 0;
1424fcdd9721SPawel Jakub Dawidek 
1425f35329acSJohn Dyson 	/*
142626f9a767SRodney W. Grimes 	 * if the requested page is not available, then give up now
142726f9a767SRodney W. Grimes 	 */
14281c7c3c6aSMatthew Dillon 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
142926f9a767SRodney W. Grimes 		return 0;
14302d8acc0fSJohn Dyson 	}
143126f9a767SRodney W. Grimes 
143222ba64e8SJohn Dyson 	if ((cbehind == 0) && (cahead == 0)) {
143322ba64e8SJohn Dyson 		*reqpage = 0;
143422ba64e8SJohn Dyson 		marray[0] = m;
143522ba64e8SJohn Dyson 		return 1;
1436170db9c6SJohn Dyson 	}
143722ba64e8SJohn Dyson 
143822ba64e8SJohn Dyson 	if (rahead > cahead) {
143922ba64e8SJohn Dyson 		rahead = cahead;
144022ba64e8SJohn Dyson 	}
144122ba64e8SJohn Dyson 
1442170db9c6SJohn Dyson 	if (rbehind > cbehind) {
1443170db9c6SJohn Dyson 		rbehind = cbehind;
1444170db9c6SJohn Dyson 	}
1445170db9c6SJohn Dyson 
144626f9a767SRodney W. Grimes 	/*
14472d8acc0fSJohn Dyson 	 * scan backward for the read behind pages -- in memory
144826f9a767SRodney W. Grimes 	 */
14492d8acc0fSJohn Dyson 	if (pindex > 0) {
14502d8acc0fSJohn Dyson 		if (rbehind > pindex) {
1451a316d390SJohn Dyson 			rbehind = pindex;
14522d8acc0fSJohn Dyson 			startpindex = 0;
14532d8acc0fSJohn Dyson 		} else {
1454a316d390SJohn Dyson 			startpindex = pindex - rbehind;
14552d8acc0fSJohn Dyson 		}
14562d8acc0fSJohn Dyson 
14578f8790a7SAlan Cox 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
14588f8790a7SAlan Cox 		    rtm->pindex >= startpindex)
14598f8790a7SAlan Cox 			startpindex = rtm->pindex + 1;
146026f9a767SRodney W. Grimes 
146180645364SAlan Cox 		/* tpindex is unsigned; beware of numeric underflow. */
146280645364SAlan Cox 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
146380645364SAlan Cox 		    tpindex < pindex; i++, tpindex--) {
146426f9a767SRodney W. Grimes 
14657bfda801SAlan Cox 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
14667bfda801SAlan Cox 			    VM_ALLOC_IFNOTCACHED);
1467ccbb2f72SJohn Dyson 			if (rtm == NULL) {
146880645364SAlan Cox 				/*
146980645364SAlan Cox 				 * Shift the allocated pages to the
147080645364SAlan Cox 				 * beginning of the array.
147180645364SAlan Cox 				 */
1472ccbb2f72SJohn Dyson 				for (j = 0; j < i; j++) {
147380645364SAlan Cox 					marray[j] = marray[j + tpindex + 1 -
147480645364SAlan Cox 					    startpindex];
147526f9a767SRodney W. Grimes 				}
147680645364SAlan Cox 				break;
147726f9a767SRodney W. Grimes 			}
1478170db9c6SJohn Dyson 
147980645364SAlan Cox 			marray[tpindex - startpindex] = rtm;
148026f9a767SRodney W. Grimes 		}
14812d8acc0fSJohn Dyson 	} else {
14822d8acc0fSJohn Dyson 		startpindex = 0;
14832d8acc0fSJohn Dyson 		i = 0;
14842d8acc0fSJohn Dyson 	}
14852d8acc0fSJohn Dyson 
14862d8acc0fSJohn Dyson 	marray[i] = m;
14872d8acc0fSJohn Dyson 	/* page offset of the required page */
14882d8acc0fSJohn Dyson 	*reqpage = i;
14892d8acc0fSJohn Dyson 
14902d8acc0fSJohn Dyson 	tpindex = pindex + 1;
14912d8acc0fSJohn Dyson 	i++;
14922d8acc0fSJohn Dyson 
14932d8acc0fSJohn Dyson 	/*
14942d8acc0fSJohn Dyson 	 * scan forward for the read ahead pages
14952d8acc0fSJohn Dyson 	 */
14962d8acc0fSJohn Dyson 	endpindex = tpindex + rahead;
14978f8790a7SAlan Cox 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
14988f8790a7SAlan Cox 		endpindex = rtm->pindex;
14992d8acc0fSJohn Dyson 	if (endpindex > object->size)
15002d8acc0fSJohn Dyson 		endpindex = object->size;
15012d8acc0fSJohn Dyson 
15022d8acc0fSJohn Dyson 	for (; tpindex < endpindex; i++, tpindex++) {
15032d8acc0fSJohn Dyson 
15047bfda801SAlan Cox 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
15057bfda801SAlan Cox 		    VM_ALLOC_IFNOTCACHED);
15062d8acc0fSJohn Dyson 		if (rtm == NULL) {
15072d8acc0fSJohn Dyson 			break;
15082d8acc0fSJohn Dyson 		}
15092d8acc0fSJohn Dyson 
15102d8acc0fSJohn Dyson 		marray[i] = rtm;
15112d8acc0fSJohn Dyson 	}
15122d8acc0fSJohn Dyson 
151365ea29a6SAlan Cox 	/* return number of pages */
15142d8acc0fSJohn Dyson 	return i;
151526f9a767SRodney W. Grimes }
15162801687dSKonstantin Belousov 
15175730afc9SAlan Cox /*
15185730afc9SAlan Cox  * Block entry into the machine-independent layer's page fault handler by
15195730afc9SAlan Cox  * the calling thread.  Subsequent calls to vm_fault() by that thread will
15205730afc9SAlan Cox  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
15215730afc9SAlan Cox  * spurious page faults.
15225730afc9SAlan Cox  */
15232801687dSKonstantin Belousov int
15242801687dSKonstantin Belousov vm_fault_disable_pagefaults(void)
15252801687dSKonstantin Belousov {
15262801687dSKonstantin Belousov 
15275730afc9SAlan Cox 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
15282801687dSKonstantin Belousov }
15292801687dSKonstantin Belousov 
15302801687dSKonstantin Belousov void
15312801687dSKonstantin Belousov vm_fault_enable_pagefaults(int save)
15322801687dSKonstantin Belousov {
15332801687dSKonstantin Belousov 
15342801687dSKonstantin Belousov 	curthread_pflags_restore(save);
15352801687dSKonstantin Belousov }
1536