xref: /freebsd/sys/vm/vm_fault.c (revision a98ff317388a00b992f1bf8404dee596f9383f5e)
1 /*-
2  * Copyright (c) 1991, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  * Copyright (c) 1994 John S. Dyson
5  * All rights reserved.
6  * Copyright (c) 1994 David Greenman
7  * All rights reserved.
8  *
9  *
10  * This code is derived from software contributed to Berkeley by
11  * The Mach Operating System project at Carnegie-Mellon University.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  * 1. Redistributions of source code must retain the above copyright
17  *    notice, this list of conditions and the following disclaimer.
18  * 2. Redistributions in binary form must reproduce the above copyright
19  *    notice, this list of conditions and the following disclaimer in the
20  *    documentation and/or other materials provided with the distribution.
21  * 3. All advertising materials mentioning features or use of this software
22  *    must display the following acknowledgement:
23  *	This product includes software developed by the University of
24  *	California, Berkeley and its contributors.
25  * 4. Neither the name of the University nor the names of its contributors
26  *    may be used to endorse or promote products derived from this software
27  *    without specific prior written permission.
28  *
29  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39  * SUCH DAMAGE.
40  *
41  *	from: @(#)vm_fault.c	8.4 (Berkeley) 1/12/94
42  *
43  *
44  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45  * All rights reserved.
46  *
47  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
48  *
49  * Permission to use, copy, modify and distribute this software and
50  * its documentation is hereby granted, provided that both the copyright
51  * notice and this permission notice appear in all copies of the
52  * software, derivative works or modified versions, and any portions
53  * thereof, and that both notices appear in supporting documentation.
54  *
55  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
58  *
59  * Carnegie Mellon requests users of this software to return to
60  *
61  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
62  *  School of Computer Science
63  *  Carnegie Mellon University
64  *  Pittsburgh PA 15213-3890
65  *
66  * any improvements or extensions that they make and grant Carnegie the
67  * rights to redistribute these changes.
68  */
69 
70 /*
71  *	Page fault handling module.
72  */
73 
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
76 
77 #include "opt_ktrace.h"
78 #include "opt_vm.h"
79 
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
83 #include <sys/lock.h>
84 #include <sys/proc.h>
85 #include <sys/resourcevar.h>
86 #include <sys/rwlock.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
89 #include <sys/vnode.h>
90 #ifdef KTRACE
91 #include <sys/ktrace.h>
92 #endif
93 
94 #include <vm/vm.h>
95 #include <vm/vm_param.h>
96 #include <vm/pmap.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_object.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_pager.h>
103 #include <vm/vm_extern.h>
104 
105 #define PFBAK 4
106 #define PFFOR 4
107 #define PAGEORDER_SIZE (PFBAK+PFFOR)
108 
109 static int prefault_pageorder[] = {
110 	-1 * PAGE_SIZE, 1 * PAGE_SIZE,
111 	-2 * PAGE_SIZE, 2 * PAGE_SIZE,
112 	-3 * PAGE_SIZE, 3 * PAGE_SIZE,
113 	-4 * PAGE_SIZE, 4 * PAGE_SIZE
114 };
115 
116 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
117 static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
118 
119 #define	VM_FAULT_READ_BEHIND	8
120 #define	VM_FAULT_READ_MAX	(1 + VM_FAULT_READ_AHEAD_MAX)
121 #define	VM_FAULT_NINCR		(VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
122 #define	VM_FAULT_SUM		(VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
123 #define	VM_FAULT_CACHE_BEHIND	(VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
124 
125 struct faultstate {
126 	vm_page_t m;
127 	vm_object_t object;
128 	vm_pindex_t pindex;
129 	vm_page_t first_m;
130 	vm_object_t	first_object;
131 	vm_pindex_t first_pindex;
132 	vm_map_t map;
133 	vm_map_entry_t entry;
134 	int lookup_still_valid;
135 	struct vnode *vp;
136 };
137 
138 static void vm_fault_cache_behind(const struct faultstate *fs, int distance);
139 
140 static inline void
141 release_page(struct faultstate *fs)
142 {
143 
144 	vm_page_wakeup(fs->m);
145 	vm_page_lock(fs->m);
146 	vm_page_deactivate(fs->m);
147 	vm_page_unlock(fs->m);
148 	fs->m = NULL;
149 }
150 
151 static inline void
152 unlock_map(struct faultstate *fs)
153 {
154 
155 	if (fs->lookup_still_valid) {
156 		vm_map_lookup_done(fs->map, fs->entry);
157 		fs->lookup_still_valid = FALSE;
158 	}
159 }
160 
161 static void
162 unlock_and_deallocate(struct faultstate *fs)
163 {
164 
165 	vm_object_pip_wakeup(fs->object);
166 	VM_OBJECT_WUNLOCK(fs->object);
167 	if (fs->object != fs->first_object) {
168 		VM_OBJECT_WLOCK(fs->first_object);
169 		vm_page_lock(fs->first_m);
170 		vm_page_free(fs->first_m);
171 		vm_page_unlock(fs->first_m);
172 		vm_object_pip_wakeup(fs->first_object);
173 		VM_OBJECT_WUNLOCK(fs->first_object);
174 		fs->first_m = NULL;
175 	}
176 	vm_object_deallocate(fs->first_object);
177 	unlock_map(fs);
178 	if (fs->vp != NULL) {
179 		vput(fs->vp);
180 		fs->vp = NULL;
181 	}
182 }
183 
184 /*
185  * TRYPAGER - used by vm_fault to calculate whether the pager for the
186  *	      current object *might* contain the page.
187  *
188  *	      default objects are zero-fill, there is no real pager.
189  */
190 #define TRYPAGER	(fs.object->type != OBJT_DEFAULT && \
191 			((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
192 
193 /*
194  *	vm_fault:
195  *
196  *	Handle a page fault occurring at the given address,
197  *	requiring the given permissions, in the map specified.
198  *	If successful, the page is inserted into the
199  *	associated physical map.
200  *
201  *	NOTE: the given address should be truncated to the
202  *	proper page address.
203  *
204  *	KERN_SUCCESS is returned if the page fault is handled; otherwise,
205  *	a standard error specifying why the fault is fatal is returned.
206  *
207  *	The map in question must be referenced, and remains so.
208  *	Caller may hold no locks.
209  */
210 int
211 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
212     int fault_flags)
213 {
214 	struct thread *td;
215 	int result;
216 
217 	td = curthread;
218 	if ((td->td_pflags & TDP_NOFAULTING) != 0)
219 		return (KERN_PROTECTION_FAILURE);
220 #ifdef KTRACE
221 	if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
222 		ktrfault(vaddr, fault_type);
223 #endif
224 	result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
225 	    NULL);
226 #ifdef KTRACE
227 	if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
228 		ktrfaultend(result);
229 #endif
230 	return (result);
231 }
232 
233 int
234 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
235     int fault_flags, vm_page_t *m_hold)
236 {
237 	vm_prot_t prot;
238 	long ahead, behind;
239 	int alloc_req, era, faultcount, nera, reqpage, result;
240 	boolean_t growstack, is_first_object_locked, wired;
241 	int map_generation;
242 	vm_object_t next_object;
243 	vm_page_t marray[VM_FAULT_READ_MAX];
244 	int hardfault;
245 	struct faultstate fs;
246 	struct vnode *vp;
247 	int locked, error;
248 
249 	hardfault = 0;
250 	growstack = TRUE;
251 	PCPU_INC(cnt.v_vm_faults);
252 	fs.vp = NULL;
253 	faultcount = reqpage = 0;
254 
255 RetryFault:;
256 
257 	/*
258 	 * Find the backing store object and offset into it to begin the
259 	 * search.
260 	 */
261 	fs.map = map;
262 	result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
263 	    &fs.first_object, &fs.first_pindex, &prot, &wired);
264 	if (result != KERN_SUCCESS) {
265 		if (growstack && result == KERN_INVALID_ADDRESS &&
266 		    map != kernel_map) {
267 			result = vm_map_growstack(curproc, vaddr);
268 			if (result != KERN_SUCCESS)
269 				return (KERN_FAILURE);
270 			growstack = FALSE;
271 			goto RetryFault;
272 		}
273 		return (result);
274 	}
275 
276 	map_generation = fs.map->timestamp;
277 
278 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
279 		panic("vm_fault: fault on nofault entry, addr: %lx",
280 		    (u_long)vaddr);
281 	}
282 
283 	if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
284 	    fs.entry->wiring_thread != curthread) {
285 		vm_map_unlock_read(fs.map);
286 		vm_map_lock(fs.map);
287 		if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
288 		    (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
289 			fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
290 			vm_map_unlock_and_wait(fs.map, 0);
291 		} else
292 			vm_map_unlock(fs.map);
293 		goto RetryFault;
294 	}
295 
296 	/*
297 	 * Make a reference to this object to prevent its disposal while we
298 	 * are messing with it.  Once we have the reference, the map is free
299 	 * to be diddled.  Since objects reference their shadows (and copies),
300 	 * they will stay around as well.
301 	 *
302 	 * Bump the paging-in-progress count to prevent size changes (e.g.
303 	 * truncation operations) during I/O.  This must be done after
304 	 * obtaining the vnode lock in order to avoid possible deadlocks.
305 	 */
306 	VM_OBJECT_WLOCK(fs.first_object);
307 	vm_object_reference_locked(fs.first_object);
308 	vm_object_pip_add(fs.first_object, 1);
309 
310 	fs.lookup_still_valid = TRUE;
311 
312 	if (wired)
313 		fault_type = prot | (fault_type & VM_PROT_COPY);
314 
315 	fs.first_m = NULL;
316 
317 	/*
318 	 * Search for the page at object/offset.
319 	 */
320 	fs.object = fs.first_object;
321 	fs.pindex = fs.first_pindex;
322 	while (TRUE) {
323 		/*
324 		 * If the object is dead, we stop here
325 		 */
326 		if (fs.object->flags & OBJ_DEAD) {
327 			unlock_and_deallocate(&fs);
328 			return (KERN_PROTECTION_FAILURE);
329 		}
330 
331 		/*
332 		 * See if page is resident
333 		 */
334 		fs.m = vm_page_lookup(fs.object, fs.pindex);
335 		if (fs.m != NULL) {
336 			/*
337 			 * check for page-based copy on write.
338 			 * We check fs.object == fs.first_object so
339 			 * as to ensure the legacy COW mechanism is
340 			 * used when the page in question is part of
341 			 * a shadow object.  Otherwise, vm_page_cowfault()
342 			 * removes the page from the backing object,
343 			 * which is not what we want.
344 			 */
345 			vm_page_lock(fs.m);
346 			if ((fs.m->cow) &&
347 			    (fault_type & VM_PROT_WRITE) &&
348 			    (fs.object == fs.first_object)) {
349 				vm_page_cowfault(fs.m);
350 				unlock_and_deallocate(&fs);
351 				goto RetryFault;
352 			}
353 
354 			/*
355 			 * Wait/Retry if the page is busy.  We have to do this
356 			 * if the page is busy via either VPO_BUSY or
357 			 * vm_page_t->busy because the vm_pager may be using
358 			 * vm_page_t->busy for pageouts ( and even pageins if
359 			 * it is the vnode pager ), and we could end up trying
360 			 * to pagein and pageout the same page simultaneously.
361 			 *
362 			 * We can theoretically allow the busy case on a read
363 			 * fault if the page is marked valid, but since such
364 			 * pages are typically already pmap'd, putting that
365 			 * special case in might be more effort then it is
366 			 * worth.  We cannot under any circumstances mess
367 			 * around with a vm_page_t->busy page except, perhaps,
368 			 * to pmap it.
369 			 */
370 			if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
371 				/*
372 				 * Reference the page before unlocking and
373 				 * sleeping so that the page daemon is less
374 				 * likely to reclaim it.
375 				 */
376 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
377 				vm_page_unlock(fs.m);
378 				if (fs.object != fs.first_object) {
379 					if (!VM_OBJECT_TRYWLOCK(
380 					    fs.first_object)) {
381 						VM_OBJECT_WUNLOCK(fs.object);
382 						VM_OBJECT_WLOCK(fs.first_object);
383 						VM_OBJECT_WLOCK(fs.object);
384 					}
385 					vm_page_lock(fs.first_m);
386 					vm_page_free(fs.first_m);
387 					vm_page_unlock(fs.first_m);
388 					vm_object_pip_wakeup(fs.first_object);
389 					VM_OBJECT_WUNLOCK(fs.first_object);
390 					fs.first_m = NULL;
391 				}
392 				unlock_map(&fs);
393 				if (fs.m == vm_page_lookup(fs.object,
394 				    fs.pindex)) {
395 					vm_page_sleep_if_busy(fs.m, TRUE,
396 					    "vmpfw");
397 				}
398 				vm_object_pip_wakeup(fs.object);
399 				VM_OBJECT_WUNLOCK(fs.object);
400 				PCPU_INC(cnt.v_intrans);
401 				vm_object_deallocate(fs.first_object);
402 				goto RetryFault;
403 			}
404 			vm_page_remque(fs.m);
405 			vm_page_unlock(fs.m);
406 
407 			/*
408 			 * Mark page busy for other processes, and the
409 			 * pagedaemon.  If it still isn't completely valid
410 			 * (readable), jump to readrest, else break-out ( we
411 			 * found the page ).
412 			 */
413 			vm_page_busy(fs.m);
414 			if (fs.m->valid != VM_PAGE_BITS_ALL)
415 				goto readrest;
416 			break;
417 		}
418 
419 		/*
420 		 * Page is not resident, If this is the search termination
421 		 * or the pager might contain the page, allocate a new page.
422 		 */
423 		if (TRYPAGER || fs.object == fs.first_object) {
424 			if (fs.pindex >= fs.object->size) {
425 				unlock_and_deallocate(&fs);
426 				return (KERN_PROTECTION_FAILURE);
427 			}
428 
429 			/*
430 			 * Allocate a new page for this object/offset pair.
431 			 *
432 			 * Unlocked read of the p_flag is harmless. At
433 			 * worst, the P_KILLED might be not observed
434 			 * there, and allocation can fail, causing
435 			 * restart and new reading of the p_flag.
436 			 */
437 			fs.m = NULL;
438 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
439 #if VM_NRESERVLEVEL > 0
440 				if ((fs.object->flags & OBJ_COLORED) == 0) {
441 					fs.object->flags |= OBJ_COLORED;
442 					fs.object->pg_color = atop(vaddr) -
443 					    fs.pindex;
444 				}
445 #endif
446 				alloc_req = P_KILLED(curproc) ?
447 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
448 				if (fs.object->type != OBJT_VNODE &&
449 				    fs.object->backing_object == NULL)
450 					alloc_req |= VM_ALLOC_ZERO;
451 				fs.m = vm_page_alloc(fs.object, fs.pindex,
452 				    alloc_req);
453 			}
454 			if (fs.m == NULL) {
455 				unlock_and_deallocate(&fs);
456 				VM_WAITPFAULT;
457 				goto RetryFault;
458 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
459 				break;
460 		}
461 
462 readrest:
463 		/*
464 		 * We have found a valid page or we have allocated a new page.
465 		 * The page thus may not be valid or may not be entirely
466 		 * valid.
467 		 *
468 		 * Attempt to fault-in the page if there is a chance that the
469 		 * pager has it, and potentially fault in additional pages
470 		 * at the same time.
471 		 */
472 		if (TRYPAGER) {
473 			int rv;
474 			u_char behavior = vm_map_entry_behavior(fs.entry);
475 
476 			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
477 			    P_KILLED(curproc)) {
478 				behind = 0;
479 				ahead = 0;
480 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
481 				behind = 0;
482 				ahead = atop(fs.entry->end - vaddr) - 1;
483 				if (ahead > VM_FAULT_READ_AHEAD_MAX)
484 					ahead = VM_FAULT_READ_AHEAD_MAX;
485 				if (fs.pindex == fs.entry->next_read)
486 					vm_fault_cache_behind(&fs,
487 					    VM_FAULT_READ_MAX);
488 			} else {
489 				/*
490 				 * If this is a sequential page fault, then
491 				 * arithmetically increase the number of pages
492 				 * in the read-ahead window.  Otherwise, reset
493 				 * the read-ahead window to its smallest size.
494 				 */
495 				behind = atop(vaddr - fs.entry->start);
496 				if (behind > VM_FAULT_READ_BEHIND)
497 					behind = VM_FAULT_READ_BEHIND;
498 				ahead = atop(fs.entry->end - vaddr) - 1;
499 				era = fs.entry->read_ahead;
500 				if (fs.pindex == fs.entry->next_read) {
501 					nera = era + behind;
502 					if (nera > VM_FAULT_READ_AHEAD_MAX)
503 						nera = VM_FAULT_READ_AHEAD_MAX;
504 					behind = 0;
505 					if (ahead > nera)
506 						ahead = nera;
507 					if (era == VM_FAULT_READ_AHEAD_MAX)
508 						vm_fault_cache_behind(&fs,
509 						    VM_FAULT_CACHE_BEHIND);
510 				} else if (ahead > VM_FAULT_READ_AHEAD_MIN)
511 					ahead = VM_FAULT_READ_AHEAD_MIN;
512 				if (era != ahead)
513 					fs.entry->read_ahead = ahead;
514 			}
515 
516 			/*
517 			 * Call the pager to retrieve the data, if any, after
518 			 * releasing the lock on the map.  We hold a ref on
519 			 * fs.object and the pages are VPO_BUSY'd.
520 			 */
521 			unlock_map(&fs);
522 
523 			if (fs.object->type == OBJT_VNODE) {
524 				vp = fs.object->handle;
525 				if (vp == fs.vp)
526 					goto vnode_locked;
527 				else if (fs.vp != NULL) {
528 					vput(fs.vp);
529 					fs.vp = NULL;
530 				}
531 				locked = VOP_ISLOCKED(vp);
532 
533 				if (locked != LK_EXCLUSIVE)
534 					locked = LK_SHARED;
535 				/* Do not sleep for vnode lock while fs.m is busy */
536 				error = vget(vp, locked | LK_CANRECURSE |
537 				    LK_NOWAIT, curthread);
538 				if (error != 0) {
539 					vhold(vp);
540 					release_page(&fs);
541 					unlock_and_deallocate(&fs);
542 					error = vget(vp, locked | LK_RETRY |
543 					    LK_CANRECURSE, curthread);
544 					vdrop(vp);
545 					fs.vp = vp;
546 					KASSERT(error == 0,
547 					    ("vm_fault: vget failed"));
548 					goto RetryFault;
549 				}
550 				fs.vp = vp;
551 			}
552 vnode_locked:
553 			KASSERT(fs.vp == NULL || !fs.map->system_map,
554 			    ("vm_fault: vnode-backed object mapped by system map"));
555 
556 			/*
557 			 * now we find out if any other pages should be paged
558 			 * in at this time this routine checks to see if the
559 			 * pages surrounding this fault reside in the same
560 			 * object as the page for this fault.  If they do,
561 			 * then they are faulted in also into the object.  The
562 			 * array "marray" returned contains an array of
563 			 * vm_page_t structs where one of them is the
564 			 * vm_page_t passed to the routine.  The reqpage
565 			 * return value is the index into the marray for the
566 			 * vm_page_t passed to the routine.
567 			 *
568 			 * fs.m plus the additional pages are VPO_BUSY'd.
569 			 */
570 			faultcount = vm_fault_additional_pages(
571 			    fs.m, behind, ahead, marray, &reqpage);
572 
573 			rv = faultcount ?
574 			    vm_pager_get_pages(fs.object, marray, faultcount,
575 				reqpage) : VM_PAGER_FAIL;
576 
577 			if (rv == VM_PAGER_OK) {
578 				/*
579 				 * Found the page. Leave it busy while we play
580 				 * with it.
581 				 */
582 
583 				/*
584 				 * Relookup in case pager changed page. Pager
585 				 * is responsible for disposition of old page
586 				 * if moved.
587 				 */
588 				fs.m = vm_page_lookup(fs.object, fs.pindex);
589 				if (!fs.m) {
590 					unlock_and_deallocate(&fs);
591 					goto RetryFault;
592 				}
593 
594 				hardfault++;
595 				break; /* break to PAGE HAS BEEN FOUND */
596 			}
597 			/*
598 			 * Remove the bogus page (which does not exist at this
599 			 * object/offset); before doing so, we must get back
600 			 * our object lock to preserve our invariant.
601 			 *
602 			 * Also wake up any other process that may want to bring
603 			 * in this page.
604 			 *
605 			 * If this is the top-level object, we must leave the
606 			 * busy page to prevent another process from rushing
607 			 * past us, and inserting the page in that object at
608 			 * the same time that we are.
609 			 */
610 			if (rv == VM_PAGER_ERROR)
611 				printf("vm_fault: pager read error, pid %d (%s)\n",
612 				    curproc->p_pid, curproc->p_comm);
613 			/*
614 			 * Data outside the range of the pager or an I/O error
615 			 */
616 			/*
617 			 * XXX - the check for kernel_map is a kludge to work
618 			 * around having the machine panic on a kernel space
619 			 * fault w/ I/O error.
620 			 */
621 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
622 				(rv == VM_PAGER_BAD)) {
623 				vm_page_lock(fs.m);
624 				vm_page_free(fs.m);
625 				vm_page_unlock(fs.m);
626 				fs.m = NULL;
627 				unlock_and_deallocate(&fs);
628 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
629 			}
630 			if (fs.object != fs.first_object) {
631 				vm_page_lock(fs.m);
632 				vm_page_free(fs.m);
633 				vm_page_unlock(fs.m);
634 				fs.m = NULL;
635 				/*
636 				 * XXX - we cannot just fall out at this
637 				 * point, m has been freed and is invalid!
638 				 */
639 			}
640 		}
641 
642 		/*
643 		 * We get here if the object has default pager (or unwiring)
644 		 * or the pager doesn't have the page.
645 		 */
646 		if (fs.object == fs.first_object)
647 			fs.first_m = fs.m;
648 
649 		/*
650 		 * Move on to the next object.  Lock the next object before
651 		 * unlocking the current one.
652 		 */
653 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
654 		next_object = fs.object->backing_object;
655 		if (next_object == NULL) {
656 			/*
657 			 * If there's no object left, fill the page in the top
658 			 * object with zeros.
659 			 */
660 			if (fs.object != fs.first_object) {
661 				vm_object_pip_wakeup(fs.object);
662 				VM_OBJECT_WUNLOCK(fs.object);
663 
664 				fs.object = fs.first_object;
665 				fs.pindex = fs.first_pindex;
666 				fs.m = fs.first_m;
667 				VM_OBJECT_WLOCK(fs.object);
668 			}
669 			fs.first_m = NULL;
670 
671 			/*
672 			 * Zero the page if necessary and mark it valid.
673 			 */
674 			if ((fs.m->flags & PG_ZERO) == 0) {
675 				pmap_zero_page(fs.m);
676 			} else {
677 				PCPU_INC(cnt.v_ozfod);
678 			}
679 			PCPU_INC(cnt.v_zfod);
680 			fs.m->valid = VM_PAGE_BITS_ALL;
681 			break;	/* break to PAGE HAS BEEN FOUND */
682 		} else {
683 			KASSERT(fs.object != next_object,
684 			    ("object loop %p", next_object));
685 			VM_OBJECT_WLOCK(next_object);
686 			vm_object_pip_add(next_object, 1);
687 			if (fs.object != fs.first_object)
688 				vm_object_pip_wakeup(fs.object);
689 			VM_OBJECT_WUNLOCK(fs.object);
690 			fs.object = next_object;
691 		}
692 	}
693 
694 	KASSERT((fs.m->oflags & VPO_BUSY) != 0,
695 	    ("vm_fault: not busy after main loop"));
696 
697 	/*
698 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
699 	 * is held.]
700 	 */
701 
702 	/*
703 	 * If the page is being written, but isn't already owned by the
704 	 * top-level object, we have to copy it into a new page owned by the
705 	 * top-level object.
706 	 */
707 	if (fs.object != fs.first_object) {
708 		/*
709 		 * We only really need to copy if we want to write it.
710 		 */
711 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
712 			/*
713 			 * This allows pages to be virtually copied from a
714 			 * backing_object into the first_object, where the
715 			 * backing object has no other refs to it, and cannot
716 			 * gain any more refs.  Instead of a bcopy, we just
717 			 * move the page from the backing object to the
718 			 * first object.  Note that we must mark the page
719 			 * dirty in the first object so that it will go out
720 			 * to swap when needed.
721 			 */
722 			is_first_object_locked = FALSE;
723 			if (
724 				/*
725 				 * Only one shadow object
726 				 */
727 				(fs.object->shadow_count == 1) &&
728 				/*
729 				 * No COW refs, except us
730 				 */
731 				(fs.object->ref_count == 1) &&
732 				/*
733 				 * No one else can look this object up
734 				 */
735 				(fs.object->handle == NULL) &&
736 				/*
737 				 * No other ways to look the object up
738 				 */
739 				((fs.object->type == OBJT_DEFAULT) ||
740 				 (fs.object->type == OBJT_SWAP)) &&
741 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
742 				/*
743 				 * We don't chase down the shadow chain
744 				 */
745 			    fs.object == fs.first_object->backing_object) {
746 				/*
747 				 * get rid of the unnecessary page
748 				 */
749 				vm_page_lock(fs.first_m);
750 				vm_page_free(fs.first_m);
751 				vm_page_unlock(fs.first_m);
752 				/*
753 				 * grab the page and put it into the
754 				 * process'es object.  The page is
755 				 * automatically made dirty.
756 				 */
757 				vm_page_lock(fs.m);
758 				vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
759 				vm_page_unlock(fs.m);
760 				vm_page_busy(fs.m);
761 				fs.first_m = fs.m;
762 				fs.m = NULL;
763 				PCPU_INC(cnt.v_cow_optim);
764 			} else {
765 				/*
766 				 * Oh, well, lets copy it.
767 				 */
768 				pmap_copy_page(fs.m, fs.first_m);
769 				fs.first_m->valid = VM_PAGE_BITS_ALL;
770 				if (wired && (fault_flags &
771 				    VM_FAULT_CHANGE_WIRING) == 0) {
772 					vm_page_lock(fs.first_m);
773 					vm_page_wire(fs.first_m);
774 					vm_page_unlock(fs.first_m);
775 
776 					vm_page_lock(fs.m);
777 					vm_page_unwire(fs.m, FALSE);
778 					vm_page_unlock(fs.m);
779 				}
780 				/*
781 				 * We no longer need the old page or object.
782 				 */
783 				release_page(&fs);
784 			}
785 			/*
786 			 * fs.object != fs.first_object due to above
787 			 * conditional
788 			 */
789 			vm_object_pip_wakeup(fs.object);
790 			VM_OBJECT_WUNLOCK(fs.object);
791 			/*
792 			 * Only use the new page below...
793 			 */
794 			fs.object = fs.first_object;
795 			fs.pindex = fs.first_pindex;
796 			fs.m = fs.first_m;
797 			if (!is_first_object_locked)
798 				VM_OBJECT_WLOCK(fs.object);
799 			PCPU_INC(cnt.v_cow_faults);
800 			curthread->td_cow++;
801 		} else {
802 			prot &= ~VM_PROT_WRITE;
803 		}
804 	}
805 
806 	/*
807 	 * We must verify that the maps have not changed since our last
808 	 * lookup.
809 	 */
810 	if (!fs.lookup_still_valid) {
811 		vm_object_t retry_object;
812 		vm_pindex_t retry_pindex;
813 		vm_prot_t retry_prot;
814 
815 		if (!vm_map_trylock_read(fs.map)) {
816 			release_page(&fs);
817 			unlock_and_deallocate(&fs);
818 			goto RetryFault;
819 		}
820 		fs.lookup_still_valid = TRUE;
821 		if (fs.map->timestamp != map_generation) {
822 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
823 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
824 
825 			/*
826 			 * If we don't need the page any longer, put it on the inactive
827 			 * list (the easiest thing to do here).  If no one needs it,
828 			 * pageout will grab it eventually.
829 			 */
830 			if (result != KERN_SUCCESS) {
831 				release_page(&fs);
832 				unlock_and_deallocate(&fs);
833 
834 				/*
835 				 * If retry of map lookup would have blocked then
836 				 * retry fault from start.
837 				 */
838 				if (result == KERN_FAILURE)
839 					goto RetryFault;
840 				return (result);
841 			}
842 			if ((retry_object != fs.first_object) ||
843 			    (retry_pindex != fs.first_pindex)) {
844 				release_page(&fs);
845 				unlock_and_deallocate(&fs);
846 				goto RetryFault;
847 			}
848 
849 			/*
850 			 * Check whether the protection has changed or the object has
851 			 * been copied while we left the map unlocked. Changing from
852 			 * read to write permission is OK - we leave the page
853 			 * write-protected, and catch the write fault. Changing from
854 			 * write to read permission means that we can't mark the page
855 			 * write-enabled after all.
856 			 */
857 			prot &= retry_prot;
858 		}
859 	}
860 	/*
861 	 * If the page was filled by a pager, update the map entry's
862 	 * last read offset.  Since the pager does not return the
863 	 * actual set of pages that it read, this update is based on
864 	 * the requested set.  Typically, the requested and actual
865 	 * sets are the same.
866 	 *
867 	 * XXX The following assignment modifies the map
868 	 * without holding a write lock on it.
869 	 */
870 	if (hardfault)
871 		fs.entry->next_read = fs.pindex + faultcount - reqpage;
872 
873 	if ((prot & VM_PROT_WRITE) != 0 ||
874 	    (fault_flags & VM_FAULT_DIRTY) != 0) {
875 		vm_object_set_writeable_dirty(fs.object);
876 
877 		/*
878 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
879 		 * if the page is already dirty to prevent data written with
880 		 * the expectation of being synced from not being synced.
881 		 * Likewise if this entry does not request NOSYNC then make
882 		 * sure the page isn't marked NOSYNC.  Applications sharing
883 		 * data should use the same flags to avoid ping ponging.
884 		 */
885 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
886 			if (fs.m->dirty == 0)
887 				fs.m->oflags |= VPO_NOSYNC;
888 		} else {
889 			fs.m->oflags &= ~VPO_NOSYNC;
890 		}
891 
892 		/*
893 		 * If the fault is a write, we know that this page is being
894 		 * written NOW so dirty it explicitly to save on
895 		 * pmap_is_modified() calls later.
896 		 *
897 		 * Also tell the backing pager, if any, that it should remove
898 		 * any swap backing since the page is now dirty.
899 		 */
900 		if (((fault_type & VM_PROT_WRITE) != 0 &&
901 		    (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
902 		    (fault_flags & VM_FAULT_DIRTY) != 0) {
903 			vm_page_dirty(fs.m);
904 			vm_pager_page_unswapped(fs.m);
905 		}
906 	}
907 
908 	/*
909 	 * Page had better still be busy
910 	 */
911 	KASSERT(fs.m->oflags & VPO_BUSY,
912 		("vm_fault: page %p not busy!", fs.m));
913 	/*
914 	 * Page must be completely valid or it is not fit to
915 	 * map into user space.  vm_pager_get_pages() ensures this.
916 	 */
917 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
918 	    ("vm_fault: page %p partially invalid", fs.m));
919 	VM_OBJECT_WUNLOCK(fs.object);
920 
921 	/*
922 	 * Put this page into the physical map.  We had to do the unlock above
923 	 * because pmap_enter() may sleep.  We don't put the page
924 	 * back on the active queue until later so that the pageout daemon
925 	 * won't find it (yet).
926 	 */
927 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
928 	if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0)
929 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
930 	VM_OBJECT_WLOCK(fs.object);
931 	vm_page_lock(fs.m);
932 
933 	/*
934 	 * If the page is not wired down, then put it where the pageout daemon
935 	 * can find it.
936 	 */
937 	if (fault_flags & VM_FAULT_CHANGE_WIRING) {
938 		if (wired)
939 			vm_page_wire(fs.m);
940 		else
941 			vm_page_unwire(fs.m, 1);
942 	} else
943 		vm_page_activate(fs.m);
944 	if (m_hold != NULL) {
945 		*m_hold = fs.m;
946 		vm_page_hold(fs.m);
947 	}
948 	vm_page_unlock(fs.m);
949 	vm_page_wakeup(fs.m);
950 
951 	/*
952 	 * Unlock everything, and return
953 	 */
954 	unlock_and_deallocate(&fs);
955 	if (hardfault) {
956 		PCPU_INC(cnt.v_io_faults);
957 		curthread->td_ru.ru_majflt++;
958 	} else
959 		curthread->td_ru.ru_minflt++;
960 
961 	return (KERN_SUCCESS);
962 }
963 
964 /*
965  * Speed up the reclamation of up to "distance" pages that precede the
966  * faulting pindex within the first object of the shadow chain.
967  */
968 static void
969 vm_fault_cache_behind(const struct faultstate *fs, int distance)
970 {
971 	vm_object_t first_object, object;
972 	vm_page_t m, m_prev;
973 	vm_pindex_t pindex;
974 
975 	object = fs->object;
976 	VM_OBJECT_ASSERT_WLOCKED(object);
977 	first_object = fs->first_object;
978 	if (first_object != object) {
979 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
980 			VM_OBJECT_WUNLOCK(object);
981 			VM_OBJECT_WLOCK(first_object);
982 			VM_OBJECT_WLOCK(object);
983 		}
984 	}
985 	/* Neither fictitious nor unmanaged pages can be cached. */
986 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
987 		if (fs->first_pindex < distance)
988 			pindex = 0;
989 		else
990 			pindex = fs->first_pindex - distance;
991 		if (pindex < OFF_TO_IDX(fs->entry->offset))
992 			pindex = OFF_TO_IDX(fs->entry->offset);
993 		m = first_object != object ? fs->first_m : fs->m;
994 		KASSERT((m->oflags & VPO_BUSY) != 0,
995 		    ("vm_fault_cache_behind: page %p is not busy", m));
996 		m_prev = vm_page_prev(m);
997 		while ((m = m_prev) != NULL && m->pindex >= pindex &&
998 		    m->valid == VM_PAGE_BITS_ALL) {
999 			m_prev = vm_page_prev(m);
1000 			if (m->busy != 0 || (m->oflags & VPO_BUSY) != 0)
1001 				continue;
1002 			vm_page_lock(m);
1003 			if (m->hold_count == 0 && m->wire_count == 0) {
1004 				pmap_remove_all(m);
1005 				vm_page_aflag_clear(m, PGA_REFERENCED);
1006 				if (m->dirty != 0)
1007 					vm_page_deactivate(m);
1008 				else
1009 					vm_page_cache(m);
1010 			}
1011 			vm_page_unlock(m);
1012 		}
1013 	}
1014 	if (first_object != object)
1015 		VM_OBJECT_WUNLOCK(first_object);
1016 }
1017 
1018 /*
1019  * vm_fault_prefault provides a quick way of clustering
1020  * pagefaults into a processes address space.  It is a "cousin"
1021  * of vm_map_pmap_enter, except it runs at page fault time instead
1022  * of mmap time.
1023  */
1024 static void
1025 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
1026 {
1027 	int i;
1028 	vm_offset_t addr, starta;
1029 	vm_pindex_t pindex;
1030 	vm_page_t m;
1031 	vm_object_t object;
1032 
1033 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1034 		return;
1035 
1036 	object = entry->object.vm_object;
1037 
1038 	starta = addra - PFBAK * PAGE_SIZE;
1039 	if (starta < entry->start) {
1040 		starta = entry->start;
1041 	} else if (starta > addra) {
1042 		starta = 0;
1043 	}
1044 
1045 	for (i = 0; i < PAGEORDER_SIZE; i++) {
1046 		vm_object_t backing_object, lobject;
1047 
1048 		addr = addra + prefault_pageorder[i];
1049 		if (addr > addra + (PFFOR * PAGE_SIZE))
1050 			addr = 0;
1051 
1052 		if (addr < starta || addr >= entry->end)
1053 			continue;
1054 
1055 		if (!pmap_is_prefaultable(pmap, addr))
1056 			continue;
1057 
1058 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1059 		lobject = object;
1060 		VM_OBJECT_RLOCK(lobject);
1061 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1062 		    lobject->type == OBJT_DEFAULT &&
1063 		    (backing_object = lobject->backing_object) != NULL) {
1064 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1065 			    0, ("vm_fault_prefault: unaligned object offset"));
1066 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1067 			VM_OBJECT_RLOCK(backing_object);
1068 			VM_OBJECT_RUNLOCK(lobject);
1069 			lobject = backing_object;
1070 		}
1071 		/*
1072 		 * give-up when a page is not in memory
1073 		 */
1074 		if (m == NULL) {
1075 			VM_OBJECT_RUNLOCK(lobject);
1076 			break;
1077 		}
1078 		if (m->valid == VM_PAGE_BITS_ALL &&
1079 		    (m->flags & PG_FICTITIOUS) == 0)
1080 			pmap_enter_quick(pmap, addr, m, entry->protection);
1081 		VM_OBJECT_RUNLOCK(lobject);
1082 	}
1083 }
1084 
1085 /*
1086  * Hold each of the physical pages that are mapped by the specified range of
1087  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1088  * and allow the specified types of access, "prot".  If all of the implied
1089  * pages are successfully held, then the number of held pages is returned
1090  * together with pointers to those pages in the array "ma".  However, if any
1091  * of the pages cannot be held, -1 is returned.
1092  */
1093 int
1094 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1095     vm_prot_t prot, vm_page_t *ma, int max_count)
1096 {
1097 	vm_offset_t end, va;
1098 	vm_page_t *mp;
1099 	int count;
1100 	boolean_t pmap_failed;
1101 
1102 	if (len == 0)
1103 		return (0);
1104 	end = round_page(addr + len);
1105 	addr = trunc_page(addr);
1106 
1107 	/*
1108 	 * Check for illegal addresses.
1109 	 */
1110 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1111 		return (-1);
1112 
1113 	count = howmany(end - addr, PAGE_SIZE);
1114 	if (count > max_count)
1115 		panic("vm_fault_quick_hold_pages: count > max_count");
1116 
1117 	/*
1118 	 * Most likely, the physical pages are resident in the pmap, so it is
1119 	 * faster to try pmap_extract_and_hold() first.
1120 	 */
1121 	pmap_failed = FALSE;
1122 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1123 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
1124 		if (*mp == NULL)
1125 			pmap_failed = TRUE;
1126 		else if ((prot & VM_PROT_WRITE) != 0 &&
1127 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
1128 			/*
1129 			 * Explicitly dirty the physical page.  Otherwise, the
1130 			 * caller's changes may go unnoticed because they are
1131 			 * performed through an unmanaged mapping or by a DMA
1132 			 * operation.
1133 			 *
1134 			 * The object lock is not held here.
1135 			 * See vm_page_clear_dirty_mask().
1136 			 */
1137 			vm_page_dirty(*mp);
1138 		}
1139 	}
1140 	if (pmap_failed) {
1141 		/*
1142 		 * One or more pages could not be held by the pmap.  Either no
1143 		 * page was mapped at the specified virtual address or that
1144 		 * mapping had insufficient permissions.  Attempt to fault in
1145 		 * and hold these pages.
1146 		 */
1147 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1148 			if (*mp == NULL && vm_fault_hold(map, va, prot,
1149 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1150 				goto error;
1151 	}
1152 	return (count);
1153 error:
1154 	for (mp = ma; mp < ma + count; mp++)
1155 		if (*mp != NULL) {
1156 			vm_page_lock(*mp);
1157 			vm_page_unhold(*mp);
1158 			vm_page_unlock(*mp);
1159 		}
1160 	return (-1);
1161 }
1162 
1163 /*
1164  *	vm_fault_wire:
1165  *
1166  *	Wire down a range of virtual addresses in a map.
1167  */
1168 int
1169 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1170     boolean_t fictitious)
1171 {
1172 	vm_offset_t va;
1173 	int rv;
1174 
1175 	/*
1176 	 * We simulate a fault to get the page and enter it in the physical
1177 	 * map.  For user wiring, we only ask for read access on currently
1178 	 * read-only sections.
1179 	 */
1180 	for (va = start; va < end; va += PAGE_SIZE) {
1181 		rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
1182 		if (rv) {
1183 			if (va != start)
1184 				vm_fault_unwire(map, start, va, fictitious);
1185 			return (rv);
1186 		}
1187 	}
1188 	return (KERN_SUCCESS);
1189 }
1190 
1191 /*
1192  *	vm_fault_unwire:
1193  *
1194  *	Unwire a range of virtual addresses in a map.
1195  */
1196 void
1197 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1198     boolean_t fictitious)
1199 {
1200 	vm_paddr_t pa;
1201 	vm_offset_t va;
1202 	vm_page_t m;
1203 	pmap_t pmap;
1204 
1205 	pmap = vm_map_pmap(map);
1206 
1207 	/*
1208 	 * Since the pages are wired down, we must be able to get their
1209 	 * mappings from the physical map system.
1210 	 */
1211 	for (va = start; va < end; va += PAGE_SIZE) {
1212 		pa = pmap_extract(pmap, va);
1213 		if (pa != 0) {
1214 			pmap_change_wiring(pmap, va, FALSE);
1215 			if (!fictitious) {
1216 				m = PHYS_TO_VM_PAGE(pa);
1217 				vm_page_lock(m);
1218 				vm_page_unwire(m, TRUE);
1219 				vm_page_unlock(m);
1220 			}
1221 		}
1222 	}
1223 }
1224 
1225 /*
1226  *	Routine:
1227  *		vm_fault_copy_entry
1228  *	Function:
1229  *		Create new shadow object backing dst_entry with private copy of
1230  *		all underlying pages. When src_entry is equal to dst_entry,
1231  *		function implements COW for wired-down map entry. Otherwise,
1232  *		it forks wired entry into dst_map.
1233  *
1234  *	In/out conditions:
1235  *		The source and destination maps must be locked for write.
1236  *		The source map entry must be wired down (or be a sharing map
1237  *		entry corresponding to a main map entry that is wired down).
1238  */
1239 void
1240 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1241     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1242     vm_ooffset_t *fork_charge)
1243 {
1244 	vm_object_t backing_object, dst_object, object, src_object;
1245 	vm_pindex_t dst_pindex, pindex, src_pindex;
1246 	vm_prot_t access, prot;
1247 	vm_offset_t vaddr;
1248 	vm_page_t dst_m;
1249 	vm_page_t src_m;
1250 	boolean_t src_readonly, upgrade;
1251 
1252 #ifdef	lint
1253 	src_map++;
1254 #endif	/* lint */
1255 
1256 	upgrade = src_entry == dst_entry;
1257 
1258 	src_object = src_entry->object.vm_object;
1259 	src_pindex = OFF_TO_IDX(src_entry->offset);
1260 	src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1261 
1262 	/*
1263 	 * Create the top-level object for the destination entry. (Doesn't
1264 	 * actually shadow anything - we copy the pages directly.)
1265 	 */
1266 	dst_object = vm_object_allocate(OBJT_DEFAULT,
1267 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1268 #if VM_NRESERVLEVEL > 0
1269 	dst_object->flags |= OBJ_COLORED;
1270 	dst_object->pg_color = atop(dst_entry->start);
1271 #endif
1272 
1273 	VM_OBJECT_WLOCK(dst_object);
1274 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1275 	    ("vm_fault_copy_entry: vm_object not NULL"));
1276 	dst_entry->object.vm_object = dst_object;
1277 	dst_entry->offset = 0;
1278 	dst_object->charge = dst_entry->end - dst_entry->start;
1279 	if (fork_charge != NULL) {
1280 		KASSERT(dst_entry->cred == NULL,
1281 		    ("vm_fault_copy_entry: leaked swp charge"));
1282 		dst_object->cred = curthread->td_ucred;
1283 		crhold(dst_object->cred);
1284 		*fork_charge += dst_object->charge;
1285 	} else {
1286 		dst_object->cred = dst_entry->cred;
1287 		dst_entry->cred = NULL;
1288 	}
1289 	access = prot = dst_entry->protection;
1290 	/*
1291 	 * If not an upgrade, then enter the mappings in the pmap as
1292 	 * read and/or execute accesses.  Otherwise, enter them as
1293 	 * write accesses.
1294 	 *
1295 	 * A writeable large page mapping is only created if all of
1296 	 * the constituent small page mappings are modified. Marking
1297 	 * PTEs as modified on inception allows promotion to happen
1298 	 * without taking potentially large number of soft faults.
1299 	 */
1300 	if (!upgrade)
1301 		access &= ~VM_PROT_WRITE;
1302 
1303 	/*
1304 	 * Loop through all of the virtual pages within the entry's
1305 	 * range, copying each page from the source object to the
1306 	 * destination object.  Since the source is wired, those pages
1307 	 * must exist.  In contrast, the destination is pageable.
1308 	 * Since the destination object does share any backing storage
1309 	 * with the source object, all of its pages must be dirtied,
1310 	 * regardless of whether they can be written.
1311 	 */
1312 	for (vaddr = dst_entry->start, dst_pindex = 0;
1313 	    vaddr < dst_entry->end;
1314 	    vaddr += PAGE_SIZE, dst_pindex++) {
1315 
1316 		/*
1317 		 * Allocate a page in the destination object.
1318 		 */
1319 		do {
1320 			dst_m = vm_page_alloc(dst_object, dst_pindex,
1321 			    VM_ALLOC_NORMAL);
1322 			if (dst_m == NULL) {
1323 				VM_OBJECT_WUNLOCK(dst_object);
1324 				VM_WAIT;
1325 				VM_OBJECT_WLOCK(dst_object);
1326 			}
1327 		} while (dst_m == NULL);
1328 
1329 		/*
1330 		 * Find the page in the source object, and copy it in.
1331 		 * (Because the source is wired down, the page will be in
1332 		 * memory.)
1333 		 */
1334 		VM_OBJECT_RLOCK(src_object);
1335 		object = src_object;
1336 		pindex = src_pindex + dst_pindex;
1337 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1338 		    src_readonly &&
1339 		    (backing_object = object->backing_object) != NULL) {
1340 			/*
1341 			 * Allow fallback to backing objects if we are reading.
1342 			 */
1343 			VM_OBJECT_RLOCK(backing_object);
1344 			pindex += OFF_TO_IDX(object->backing_object_offset);
1345 			VM_OBJECT_RUNLOCK(object);
1346 			object = backing_object;
1347 		}
1348 		if (src_m == NULL)
1349 			panic("vm_fault_copy_wired: page missing");
1350 		pmap_copy_page(src_m, dst_m);
1351 		VM_OBJECT_RUNLOCK(object);
1352 		dst_m->valid = VM_PAGE_BITS_ALL;
1353 		dst_m->dirty = VM_PAGE_BITS_ALL;
1354 		VM_OBJECT_WUNLOCK(dst_object);
1355 
1356 		/*
1357 		 * Enter it in the pmap. If a wired, copy-on-write
1358 		 * mapping is being replaced by a write-enabled
1359 		 * mapping, then wire that new mapping.
1360 		 */
1361 		pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1362 
1363 		/*
1364 		 * Mark it no longer busy, and put it on the active list.
1365 		 */
1366 		VM_OBJECT_WLOCK(dst_object);
1367 
1368 		if (upgrade) {
1369 			vm_page_lock(src_m);
1370 			vm_page_unwire(src_m, 0);
1371 			vm_page_unlock(src_m);
1372 
1373 			vm_page_lock(dst_m);
1374 			vm_page_wire(dst_m);
1375 			vm_page_unlock(dst_m);
1376 		} else {
1377 			vm_page_lock(dst_m);
1378 			vm_page_activate(dst_m);
1379 			vm_page_unlock(dst_m);
1380 		}
1381 		vm_page_wakeup(dst_m);
1382 	}
1383 	VM_OBJECT_WUNLOCK(dst_object);
1384 	if (upgrade) {
1385 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1386 		vm_object_deallocate(src_object);
1387 	}
1388 }
1389 
1390 
1391 /*
1392  * This routine checks around the requested page for other pages that
1393  * might be able to be faulted in.  This routine brackets the viable
1394  * pages for the pages to be paged in.
1395  *
1396  * Inputs:
1397  *	m, rbehind, rahead
1398  *
1399  * Outputs:
1400  *  marray (array of vm_page_t), reqpage (index of requested page)
1401  *
1402  * Return value:
1403  *  number of pages in marray
1404  */
1405 static int
1406 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1407 	vm_page_t m;
1408 	int rbehind;
1409 	int rahead;
1410 	vm_page_t *marray;
1411 	int *reqpage;
1412 {
1413 	int i,j;
1414 	vm_object_t object;
1415 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
1416 	vm_page_t rtm;
1417 	int cbehind, cahead;
1418 
1419 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1420 
1421 	object = m->object;
1422 	pindex = m->pindex;
1423 	cbehind = cahead = 0;
1424 
1425 	/*
1426 	 * if the requested page is not available, then give up now
1427 	 */
1428 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1429 		return 0;
1430 	}
1431 
1432 	if ((cbehind == 0) && (cahead == 0)) {
1433 		*reqpage = 0;
1434 		marray[0] = m;
1435 		return 1;
1436 	}
1437 
1438 	if (rahead > cahead) {
1439 		rahead = cahead;
1440 	}
1441 
1442 	if (rbehind > cbehind) {
1443 		rbehind = cbehind;
1444 	}
1445 
1446 	/*
1447 	 * scan backward for the read behind pages -- in memory
1448 	 */
1449 	if (pindex > 0) {
1450 		if (rbehind > pindex) {
1451 			rbehind = pindex;
1452 			startpindex = 0;
1453 		} else {
1454 			startpindex = pindex - rbehind;
1455 		}
1456 
1457 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1458 		    rtm->pindex >= startpindex)
1459 			startpindex = rtm->pindex + 1;
1460 
1461 		/* tpindex is unsigned; beware of numeric underflow. */
1462 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1463 		    tpindex < pindex; i++, tpindex--) {
1464 
1465 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1466 			    VM_ALLOC_IFNOTCACHED);
1467 			if (rtm == NULL) {
1468 				/*
1469 				 * Shift the allocated pages to the
1470 				 * beginning of the array.
1471 				 */
1472 				for (j = 0; j < i; j++) {
1473 					marray[j] = marray[j + tpindex + 1 -
1474 					    startpindex];
1475 				}
1476 				break;
1477 			}
1478 
1479 			marray[tpindex - startpindex] = rtm;
1480 		}
1481 	} else {
1482 		startpindex = 0;
1483 		i = 0;
1484 	}
1485 
1486 	marray[i] = m;
1487 	/* page offset of the required page */
1488 	*reqpage = i;
1489 
1490 	tpindex = pindex + 1;
1491 	i++;
1492 
1493 	/*
1494 	 * scan forward for the read ahead pages
1495 	 */
1496 	endpindex = tpindex + rahead;
1497 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1498 		endpindex = rtm->pindex;
1499 	if (endpindex > object->size)
1500 		endpindex = object->size;
1501 
1502 	for (; tpindex < endpindex; i++, tpindex++) {
1503 
1504 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1505 		    VM_ALLOC_IFNOTCACHED);
1506 		if (rtm == NULL) {
1507 			break;
1508 		}
1509 
1510 		marray[i] = rtm;
1511 	}
1512 
1513 	/* return number of pages */
1514 	return i;
1515 }
1516 
1517 /*
1518  * Block entry into the machine-independent layer's page fault handler by
1519  * the calling thread.  Subsequent calls to vm_fault() by that thread will
1520  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
1521  * spurious page faults.
1522  */
1523 int
1524 vm_fault_disable_pagefaults(void)
1525 {
1526 
1527 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1528 }
1529 
1530 void
1531 vm_fault_enable_pagefaults(int save)
1532 {
1533 
1534 	curthread_pflags_restore(save);
1535 }
1536