xref: /freebsd/sys/vm/vm_fault.c (revision 13de33a5dc2304b13d595d75d48c51793958474f)
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_xunbusy(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 			 * Wait/Retry if the page is busy.  We have to do this
338 			 * if the page is either exclusive or shared busy
339 			 * because the vm_pager may be using read busy for
340 			 * pageouts (and even pageins if it is the vnode
341 			 * pager), and we could end up trying to pagein and
342 			 * pageout the same page simultaneously.
343 			 *
344 			 * We can theoretically allow the busy case on a read
345 			 * fault if the page is marked valid, but since such
346 			 * pages are typically already pmap'd, putting that
347 			 * special case in might be more effort then it is
348 			 * worth.  We cannot under any circumstances mess
349 			 * around with a shared busied page except, perhaps,
350 			 * to pmap it.
351 			 */
352 			if (vm_page_busied(fs.m)) {
353 				/*
354 				 * Reference the page before unlocking and
355 				 * sleeping so that the page daemon is less
356 				 * likely to reclaim it.
357 				 */
358 				vm_page_aflag_set(fs.m, PGA_REFERENCED);
359 				if (fs.object != fs.first_object) {
360 					if (!VM_OBJECT_TRYWLOCK(
361 					    fs.first_object)) {
362 						VM_OBJECT_WUNLOCK(fs.object);
363 						VM_OBJECT_WLOCK(fs.first_object);
364 						VM_OBJECT_WLOCK(fs.object);
365 					}
366 					vm_page_lock(fs.first_m);
367 					vm_page_free(fs.first_m);
368 					vm_page_unlock(fs.first_m);
369 					vm_object_pip_wakeup(fs.first_object);
370 					VM_OBJECT_WUNLOCK(fs.first_object);
371 					fs.first_m = NULL;
372 				}
373 				unlock_map(&fs);
374 				if (fs.m == vm_page_lookup(fs.object,
375 				    fs.pindex)) {
376 					vm_page_sleep_if_busy(fs.m, "vmpfw");
377 				}
378 				vm_object_pip_wakeup(fs.object);
379 				VM_OBJECT_WUNLOCK(fs.object);
380 				PCPU_INC(cnt.v_intrans);
381 				vm_object_deallocate(fs.first_object);
382 				goto RetryFault;
383 			}
384 			vm_page_lock(fs.m);
385 			vm_page_remque(fs.m);
386 			vm_page_unlock(fs.m);
387 
388 			/*
389 			 * Mark page busy for other processes, and the
390 			 * pagedaemon.  If it still isn't completely valid
391 			 * (readable), jump to readrest, else break-out ( we
392 			 * found the page ).
393 			 */
394 			vm_page_xbusy(fs.m);
395 			if (fs.m->valid != VM_PAGE_BITS_ALL)
396 				goto readrest;
397 			break;
398 		}
399 
400 		/*
401 		 * Page is not resident, If this is the search termination
402 		 * or the pager might contain the page, allocate a new page.
403 		 */
404 		if (TRYPAGER || fs.object == fs.first_object) {
405 			if (fs.pindex >= fs.object->size) {
406 				unlock_and_deallocate(&fs);
407 				return (KERN_PROTECTION_FAILURE);
408 			}
409 
410 			/*
411 			 * Allocate a new page for this object/offset pair.
412 			 *
413 			 * Unlocked read of the p_flag is harmless. At
414 			 * worst, the P_KILLED might be not observed
415 			 * there, and allocation can fail, causing
416 			 * restart and new reading of the p_flag.
417 			 */
418 			fs.m = NULL;
419 			if (!vm_page_count_severe() || P_KILLED(curproc)) {
420 #if VM_NRESERVLEVEL > 0
421 				if ((fs.object->flags & OBJ_COLORED) == 0) {
422 					fs.object->flags |= OBJ_COLORED;
423 					fs.object->pg_color = atop(vaddr) -
424 					    fs.pindex;
425 				}
426 #endif
427 				alloc_req = P_KILLED(curproc) ?
428 				    VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
429 				if (fs.object->type != OBJT_VNODE &&
430 				    fs.object->backing_object == NULL)
431 					alloc_req |= VM_ALLOC_ZERO;
432 				fs.m = vm_page_alloc(fs.object, fs.pindex,
433 				    alloc_req);
434 			}
435 			if (fs.m == NULL) {
436 				unlock_and_deallocate(&fs);
437 				VM_WAITPFAULT;
438 				goto RetryFault;
439 			} else if (fs.m->valid == VM_PAGE_BITS_ALL)
440 				break;
441 		}
442 
443 readrest:
444 		/*
445 		 * We have found a valid page or we have allocated a new page.
446 		 * The page thus may not be valid or may not be entirely
447 		 * valid.
448 		 *
449 		 * Attempt to fault-in the page if there is a chance that the
450 		 * pager has it, and potentially fault in additional pages
451 		 * at the same time.
452 		 */
453 		if (TRYPAGER) {
454 			int rv;
455 			u_char behavior = vm_map_entry_behavior(fs.entry);
456 
457 			if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
458 			    P_KILLED(curproc)) {
459 				behind = 0;
460 				ahead = 0;
461 			} else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
462 				behind = 0;
463 				ahead = atop(fs.entry->end - vaddr) - 1;
464 				if (ahead > VM_FAULT_READ_AHEAD_MAX)
465 					ahead = VM_FAULT_READ_AHEAD_MAX;
466 				if (fs.pindex == fs.entry->next_read)
467 					vm_fault_cache_behind(&fs,
468 					    VM_FAULT_READ_MAX);
469 			} else {
470 				/*
471 				 * If this is a sequential page fault, then
472 				 * arithmetically increase the number of pages
473 				 * in the read-ahead window.  Otherwise, reset
474 				 * the read-ahead window to its smallest size.
475 				 */
476 				behind = atop(vaddr - fs.entry->start);
477 				if (behind > VM_FAULT_READ_BEHIND)
478 					behind = VM_FAULT_READ_BEHIND;
479 				ahead = atop(fs.entry->end - vaddr) - 1;
480 				era = fs.entry->read_ahead;
481 				if (fs.pindex == fs.entry->next_read) {
482 					nera = era + behind;
483 					if (nera > VM_FAULT_READ_AHEAD_MAX)
484 						nera = VM_FAULT_READ_AHEAD_MAX;
485 					behind = 0;
486 					if (ahead > nera)
487 						ahead = nera;
488 					if (era == VM_FAULT_READ_AHEAD_MAX)
489 						vm_fault_cache_behind(&fs,
490 						    VM_FAULT_CACHE_BEHIND);
491 				} else if (ahead > VM_FAULT_READ_AHEAD_MIN)
492 					ahead = VM_FAULT_READ_AHEAD_MIN;
493 				if (era != ahead)
494 					fs.entry->read_ahead = ahead;
495 			}
496 
497 			/*
498 			 * Call the pager to retrieve the data, if any, after
499 			 * releasing the lock on the map.  We hold a ref on
500 			 * fs.object and the pages are exclusive busied.
501 			 */
502 			unlock_map(&fs);
503 
504 			if (fs.object->type == OBJT_VNODE) {
505 				vp = fs.object->handle;
506 				if (vp == fs.vp)
507 					goto vnode_locked;
508 				else if (fs.vp != NULL) {
509 					vput(fs.vp);
510 					fs.vp = NULL;
511 				}
512 				locked = VOP_ISLOCKED(vp);
513 
514 				if (locked != LK_EXCLUSIVE)
515 					locked = LK_SHARED;
516 				/* Do not sleep for vnode lock while fs.m is busy */
517 				error = vget(vp, locked | LK_CANRECURSE |
518 				    LK_NOWAIT, curthread);
519 				if (error != 0) {
520 					vhold(vp);
521 					release_page(&fs);
522 					unlock_and_deallocate(&fs);
523 					error = vget(vp, locked | LK_RETRY |
524 					    LK_CANRECURSE, curthread);
525 					vdrop(vp);
526 					fs.vp = vp;
527 					KASSERT(error == 0,
528 					    ("vm_fault: vget failed"));
529 					goto RetryFault;
530 				}
531 				fs.vp = vp;
532 			}
533 vnode_locked:
534 			KASSERT(fs.vp == NULL || !fs.map->system_map,
535 			    ("vm_fault: vnode-backed object mapped by system map"));
536 
537 			/*
538 			 * now we find out if any other pages should be paged
539 			 * in at this time this routine checks to see if the
540 			 * pages surrounding this fault reside in the same
541 			 * object as the page for this fault.  If they do,
542 			 * then they are faulted in also into the object.  The
543 			 * array "marray" returned contains an array of
544 			 * vm_page_t structs where one of them is the
545 			 * vm_page_t passed to the routine.  The reqpage
546 			 * return value is the index into the marray for the
547 			 * vm_page_t passed to the routine.
548 			 *
549 			 * fs.m plus the additional pages are exclusive busied.
550 			 */
551 			faultcount = vm_fault_additional_pages(
552 			    fs.m, behind, ahead, marray, &reqpage);
553 
554 			rv = faultcount ?
555 			    vm_pager_get_pages(fs.object, marray, faultcount,
556 				reqpage) : VM_PAGER_FAIL;
557 
558 			if (rv == VM_PAGER_OK) {
559 				/*
560 				 * Found the page. Leave it busy while we play
561 				 * with it.
562 				 */
563 
564 				/*
565 				 * Relookup in case pager changed page. Pager
566 				 * is responsible for disposition of old page
567 				 * if moved.
568 				 */
569 				fs.m = vm_page_lookup(fs.object, fs.pindex);
570 				if (!fs.m) {
571 					unlock_and_deallocate(&fs);
572 					goto RetryFault;
573 				}
574 
575 				hardfault++;
576 				break; /* break to PAGE HAS BEEN FOUND */
577 			}
578 			/*
579 			 * Remove the bogus page (which does not exist at this
580 			 * object/offset); before doing so, we must get back
581 			 * our object lock to preserve our invariant.
582 			 *
583 			 * Also wake up any other process that may want to bring
584 			 * in this page.
585 			 *
586 			 * If this is the top-level object, we must leave the
587 			 * busy page to prevent another process from rushing
588 			 * past us, and inserting the page in that object at
589 			 * the same time that we are.
590 			 */
591 			if (rv == VM_PAGER_ERROR)
592 				printf("vm_fault: pager read error, pid %d (%s)\n",
593 				    curproc->p_pid, curproc->p_comm);
594 			/*
595 			 * Data outside the range of the pager or an I/O error
596 			 */
597 			/*
598 			 * XXX - the check for kernel_map is a kludge to work
599 			 * around having the machine panic on a kernel space
600 			 * fault w/ I/O error.
601 			 */
602 			if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
603 				(rv == VM_PAGER_BAD)) {
604 				vm_page_lock(fs.m);
605 				vm_page_free(fs.m);
606 				vm_page_unlock(fs.m);
607 				fs.m = NULL;
608 				unlock_and_deallocate(&fs);
609 				return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
610 			}
611 			if (fs.object != fs.first_object) {
612 				vm_page_lock(fs.m);
613 				vm_page_free(fs.m);
614 				vm_page_unlock(fs.m);
615 				fs.m = NULL;
616 				/*
617 				 * XXX - we cannot just fall out at this
618 				 * point, m has been freed and is invalid!
619 				 */
620 			}
621 		}
622 
623 		/*
624 		 * We get here if the object has default pager (or unwiring)
625 		 * or the pager doesn't have the page.
626 		 */
627 		if (fs.object == fs.first_object)
628 			fs.first_m = fs.m;
629 
630 		/*
631 		 * Move on to the next object.  Lock the next object before
632 		 * unlocking the current one.
633 		 */
634 		fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
635 		next_object = fs.object->backing_object;
636 		if (next_object == NULL) {
637 			/*
638 			 * If there's no object left, fill the page in the top
639 			 * object with zeros.
640 			 */
641 			if (fs.object != fs.first_object) {
642 				vm_object_pip_wakeup(fs.object);
643 				VM_OBJECT_WUNLOCK(fs.object);
644 
645 				fs.object = fs.first_object;
646 				fs.pindex = fs.first_pindex;
647 				fs.m = fs.first_m;
648 				VM_OBJECT_WLOCK(fs.object);
649 			}
650 			fs.first_m = NULL;
651 
652 			/*
653 			 * Zero the page if necessary and mark it valid.
654 			 */
655 			if ((fs.m->flags & PG_ZERO) == 0) {
656 				pmap_zero_page(fs.m);
657 			} else {
658 				PCPU_INC(cnt.v_ozfod);
659 			}
660 			PCPU_INC(cnt.v_zfod);
661 			fs.m->valid = VM_PAGE_BITS_ALL;
662 			break;	/* break to PAGE HAS BEEN FOUND */
663 		} else {
664 			KASSERT(fs.object != next_object,
665 			    ("object loop %p", next_object));
666 			VM_OBJECT_WLOCK(next_object);
667 			vm_object_pip_add(next_object, 1);
668 			if (fs.object != fs.first_object)
669 				vm_object_pip_wakeup(fs.object);
670 			VM_OBJECT_WUNLOCK(fs.object);
671 			fs.object = next_object;
672 		}
673 	}
674 
675 	vm_page_assert_xbusied(fs.m);
676 
677 	/*
678 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
679 	 * is held.]
680 	 */
681 
682 	/*
683 	 * If the page is being written, but isn't already owned by the
684 	 * top-level object, we have to copy it into a new page owned by the
685 	 * top-level object.
686 	 */
687 	if (fs.object != fs.first_object) {
688 		/*
689 		 * We only really need to copy if we want to write it.
690 		 */
691 		if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
692 			/*
693 			 * This allows pages to be virtually copied from a
694 			 * backing_object into the first_object, where the
695 			 * backing object has no other refs to it, and cannot
696 			 * gain any more refs.  Instead of a bcopy, we just
697 			 * move the page from the backing object to the
698 			 * first object.  Note that we must mark the page
699 			 * dirty in the first object so that it will go out
700 			 * to swap when needed.
701 			 */
702 			is_first_object_locked = FALSE;
703 			if (
704 				/*
705 				 * Only one shadow object
706 				 */
707 				(fs.object->shadow_count == 1) &&
708 				/*
709 				 * No COW refs, except us
710 				 */
711 				(fs.object->ref_count == 1) &&
712 				/*
713 				 * No one else can look this object up
714 				 */
715 				(fs.object->handle == NULL) &&
716 				/*
717 				 * No other ways to look the object up
718 				 */
719 				((fs.object->type == OBJT_DEFAULT) ||
720 				 (fs.object->type == OBJT_SWAP)) &&
721 			    (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
722 				/*
723 				 * We don't chase down the shadow chain
724 				 */
725 			    fs.object == fs.first_object->backing_object) {
726 				/*
727 				 * get rid of the unnecessary page
728 				 */
729 				vm_page_lock(fs.first_m);
730 				vm_page_free(fs.first_m);
731 				vm_page_unlock(fs.first_m);
732 				/*
733 				 * grab the page and put it into the
734 				 * process'es object.  The page is
735 				 * automatically made dirty.
736 				 */
737 				if (vm_page_rename(fs.m, fs.first_object,
738 				    fs.first_pindex)) {
739 					unlock_and_deallocate(&fs);
740 					goto RetryFault;
741 				}
742 				vm_page_xbusy(fs.m);
743 				fs.first_m = fs.m;
744 				fs.m = NULL;
745 				PCPU_INC(cnt.v_cow_optim);
746 			} else {
747 				/*
748 				 * Oh, well, lets copy it.
749 				 */
750 				pmap_copy_page(fs.m, fs.first_m);
751 				fs.first_m->valid = VM_PAGE_BITS_ALL;
752 				if (wired && (fault_flags &
753 				    VM_FAULT_CHANGE_WIRING) == 0) {
754 					vm_page_lock(fs.first_m);
755 					vm_page_wire(fs.first_m);
756 					vm_page_unlock(fs.first_m);
757 
758 					vm_page_lock(fs.m);
759 					vm_page_unwire(fs.m, FALSE);
760 					vm_page_unlock(fs.m);
761 				}
762 				/*
763 				 * We no longer need the old page or object.
764 				 */
765 				release_page(&fs);
766 			}
767 			/*
768 			 * fs.object != fs.first_object due to above
769 			 * conditional
770 			 */
771 			vm_object_pip_wakeup(fs.object);
772 			VM_OBJECT_WUNLOCK(fs.object);
773 			/*
774 			 * Only use the new page below...
775 			 */
776 			fs.object = fs.first_object;
777 			fs.pindex = fs.first_pindex;
778 			fs.m = fs.first_m;
779 			if (!is_first_object_locked)
780 				VM_OBJECT_WLOCK(fs.object);
781 			PCPU_INC(cnt.v_cow_faults);
782 			curthread->td_cow++;
783 		} else {
784 			prot &= ~VM_PROT_WRITE;
785 		}
786 	}
787 
788 	/*
789 	 * We must verify that the maps have not changed since our last
790 	 * lookup.
791 	 */
792 	if (!fs.lookup_still_valid) {
793 		vm_object_t retry_object;
794 		vm_pindex_t retry_pindex;
795 		vm_prot_t retry_prot;
796 
797 		if (!vm_map_trylock_read(fs.map)) {
798 			release_page(&fs);
799 			unlock_and_deallocate(&fs);
800 			goto RetryFault;
801 		}
802 		fs.lookup_still_valid = TRUE;
803 		if (fs.map->timestamp != map_generation) {
804 			result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
805 			    &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
806 
807 			/*
808 			 * If we don't need the page any longer, put it on the inactive
809 			 * list (the easiest thing to do here).  If no one needs it,
810 			 * pageout will grab it eventually.
811 			 */
812 			if (result != KERN_SUCCESS) {
813 				release_page(&fs);
814 				unlock_and_deallocate(&fs);
815 
816 				/*
817 				 * If retry of map lookup would have blocked then
818 				 * retry fault from start.
819 				 */
820 				if (result == KERN_FAILURE)
821 					goto RetryFault;
822 				return (result);
823 			}
824 			if ((retry_object != fs.first_object) ||
825 			    (retry_pindex != fs.first_pindex)) {
826 				release_page(&fs);
827 				unlock_and_deallocate(&fs);
828 				goto RetryFault;
829 			}
830 
831 			/*
832 			 * Check whether the protection has changed or the object has
833 			 * been copied while we left the map unlocked. Changing from
834 			 * read to write permission is OK - we leave the page
835 			 * write-protected, and catch the write fault. Changing from
836 			 * write to read permission means that we can't mark the page
837 			 * write-enabled after all.
838 			 */
839 			prot &= retry_prot;
840 		}
841 	}
842 	/*
843 	 * If the page was filled by a pager, update the map entry's
844 	 * last read offset.  Since the pager does not return the
845 	 * actual set of pages that it read, this update is based on
846 	 * the requested set.  Typically, the requested and actual
847 	 * sets are the same.
848 	 *
849 	 * XXX The following assignment modifies the map
850 	 * without holding a write lock on it.
851 	 */
852 	if (hardfault)
853 		fs.entry->next_read = fs.pindex + faultcount - reqpage;
854 
855 	if ((prot & VM_PROT_WRITE) != 0 ||
856 	    (fault_flags & VM_FAULT_DIRTY) != 0) {
857 		vm_object_set_writeable_dirty(fs.object);
858 
859 		/*
860 		 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
861 		 * if the page is already dirty to prevent data written with
862 		 * the expectation of being synced from not being synced.
863 		 * Likewise if this entry does not request NOSYNC then make
864 		 * sure the page isn't marked NOSYNC.  Applications sharing
865 		 * data should use the same flags to avoid ping ponging.
866 		 */
867 		if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
868 			if (fs.m->dirty == 0)
869 				fs.m->oflags |= VPO_NOSYNC;
870 		} else {
871 			fs.m->oflags &= ~VPO_NOSYNC;
872 		}
873 
874 		/*
875 		 * If the fault is a write, we know that this page is being
876 		 * written NOW so dirty it explicitly to save on
877 		 * pmap_is_modified() calls later.
878 		 *
879 		 * Also tell the backing pager, if any, that it should remove
880 		 * any swap backing since the page is now dirty.
881 		 */
882 		if (((fault_type & VM_PROT_WRITE) != 0 &&
883 		    (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
884 		    (fault_flags & VM_FAULT_DIRTY) != 0) {
885 			vm_page_dirty(fs.m);
886 			vm_pager_page_unswapped(fs.m);
887 		}
888 	}
889 
890 	vm_page_assert_xbusied(fs.m);
891 
892 	/*
893 	 * Page must be completely valid or it is not fit to
894 	 * map into user space.  vm_pager_get_pages() ensures this.
895 	 */
896 	KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
897 	    ("vm_fault: page %p partially invalid", fs.m));
898 	VM_OBJECT_WUNLOCK(fs.object);
899 
900 	/*
901 	 * Put this page into the physical map.  We had to do the unlock above
902 	 * because pmap_enter() may sleep.  We don't put the page
903 	 * back on the active queue until later so that the pageout daemon
904 	 * won't find it (yet).
905 	 */
906 	pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
907 	if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0)
908 		vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
909 	VM_OBJECT_WLOCK(fs.object);
910 	vm_page_lock(fs.m);
911 
912 	/*
913 	 * If the page is not wired down, then put it where the pageout daemon
914 	 * can find it.
915 	 */
916 	if (fault_flags & VM_FAULT_CHANGE_WIRING) {
917 		if (wired)
918 			vm_page_wire(fs.m);
919 		else
920 			vm_page_unwire(fs.m, 1);
921 	} else
922 		vm_page_activate(fs.m);
923 	if (m_hold != NULL) {
924 		*m_hold = fs.m;
925 		vm_page_hold(fs.m);
926 	}
927 	vm_page_unlock(fs.m);
928 	vm_page_xunbusy(fs.m);
929 
930 	/*
931 	 * Unlock everything, and return
932 	 */
933 	unlock_and_deallocate(&fs);
934 	if (hardfault) {
935 		PCPU_INC(cnt.v_io_faults);
936 		curthread->td_ru.ru_majflt++;
937 	} else
938 		curthread->td_ru.ru_minflt++;
939 
940 	return (KERN_SUCCESS);
941 }
942 
943 /*
944  * Speed up the reclamation of up to "distance" pages that precede the
945  * faulting pindex within the first object of the shadow chain.
946  */
947 static void
948 vm_fault_cache_behind(const struct faultstate *fs, int distance)
949 {
950 	vm_object_t first_object, object;
951 	vm_page_t m, m_prev;
952 	vm_pindex_t pindex;
953 
954 	object = fs->object;
955 	VM_OBJECT_ASSERT_WLOCKED(object);
956 	first_object = fs->first_object;
957 	if (first_object != object) {
958 		if (!VM_OBJECT_TRYWLOCK(first_object)) {
959 			VM_OBJECT_WUNLOCK(object);
960 			VM_OBJECT_WLOCK(first_object);
961 			VM_OBJECT_WLOCK(object);
962 		}
963 	}
964 	/* Neither fictitious nor unmanaged pages can be cached. */
965 	if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
966 		if (fs->first_pindex < distance)
967 			pindex = 0;
968 		else
969 			pindex = fs->first_pindex - distance;
970 		if (pindex < OFF_TO_IDX(fs->entry->offset))
971 			pindex = OFF_TO_IDX(fs->entry->offset);
972 		m = first_object != object ? fs->first_m : fs->m;
973 		vm_page_assert_xbusied(m);
974 		m_prev = vm_page_prev(m);
975 		while ((m = m_prev) != NULL && m->pindex >= pindex &&
976 		    m->valid == VM_PAGE_BITS_ALL) {
977 			m_prev = vm_page_prev(m);
978 			if (vm_page_busied(m))
979 				continue;
980 			vm_page_lock(m);
981 			if (m->hold_count == 0 && m->wire_count == 0) {
982 				pmap_remove_all(m);
983 				vm_page_aflag_clear(m, PGA_REFERENCED);
984 				if (m->dirty != 0)
985 					vm_page_deactivate(m);
986 				else
987 					vm_page_cache(m);
988 			}
989 			vm_page_unlock(m);
990 		}
991 	}
992 	if (first_object != object)
993 		VM_OBJECT_WUNLOCK(first_object);
994 }
995 
996 /*
997  * vm_fault_prefault provides a quick way of clustering
998  * pagefaults into a processes address space.  It is a "cousin"
999  * of vm_map_pmap_enter, except it runs at page fault time instead
1000  * of mmap time.
1001  */
1002 static void
1003 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
1004 {
1005 	int i;
1006 	vm_offset_t addr, starta;
1007 	vm_pindex_t pindex;
1008 	vm_page_t m;
1009 	vm_object_t object;
1010 
1011 	if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1012 		return;
1013 
1014 	object = entry->object.vm_object;
1015 
1016 	starta = addra - PFBAK * PAGE_SIZE;
1017 	if (starta < entry->start) {
1018 		starta = entry->start;
1019 	} else if (starta > addra) {
1020 		starta = 0;
1021 	}
1022 
1023 	for (i = 0; i < PAGEORDER_SIZE; i++) {
1024 		vm_object_t backing_object, lobject;
1025 
1026 		addr = addra + prefault_pageorder[i];
1027 		if (addr > addra + (PFFOR * PAGE_SIZE))
1028 			addr = 0;
1029 
1030 		if (addr < starta || addr >= entry->end)
1031 			continue;
1032 
1033 		if (!pmap_is_prefaultable(pmap, addr))
1034 			continue;
1035 
1036 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1037 		lobject = object;
1038 		VM_OBJECT_RLOCK(lobject);
1039 		while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1040 		    lobject->type == OBJT_DEFAULT &&
1041 		    (backing_object = lobject->backing_object) != NULL) {
1042 			KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1043 			    0, ("vm_fault_prefault: unaligned object offset"));
1044 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1045 			VM_OBJECT_RLOCK(backing_object);
1046 			VM_OBJECT_RUNLOCK(lobject);
1047 			lobject = backing_object;
1048 		}
1049 		/*
1050 		 * give-up when a page is not in memory
1051 		 */
1052 		if (m == NULL) {
1053 			VM_OBJECT_RUNLOCK(lobject);
1054 			break;
1055 		}
1056 		if (m->valid == VM_PAGE_BITS_ALL &&
1057 		    (m->flags & PG_FICTITIOUS) == 0)
1058 			pmap_enter_quick(pmap, addr, m, entry->protection);
1059 		VM_OBJECT_RUNLOCK(lobject);
1060 	}
1061 }
1062 
1063 /*
1064  * Hold each of the physical pages that are mapped by the specified range of
1065  * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1066  * and allow the specified types of access, "prot".  If all of the implied
1067  * pages are successfully held, then the number of held pages is returned
1068  * together with pointers to those pages in the array "ma".  However, if any
1069  * of the pages cannot be held, -1 is returned.
1070  */
1071 int
1072 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1073     vm_prot_t prot, vm_page_t *ma, int max_count)
1074 {
1075 	vm_offset_t end, va;
1076 	vm_page_t *mp;
1077 	int count;
1078 	boolean_t pmap_failed;
1079 
1080 	if (len == 0)
1081 		return (0);
1082 	end = round_page(addr + len);
1083 	addr = trunc_page(addr);
1084 
1085 	/*
1086 	 * Check for illegal addresses.
1087 	 */
1088 	if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1089 		return (-1);
1090 
1091 	if (atop(end - addr) > max_count)
1092 		panic("vm_fault_quick_hold_pages: count > max_count");
1093 	count = atop(end - addr);
1094 
1095 	/*
1096 	 * Most likely, the physical pages are resident in the pmap, so it is
1097 	 * faster to try pmap_extract_and_hold() first.
1098 	 */
1099 	pmap_failed = FALSE;
1100 	for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1101 		*mp = pmap_extract_and_hold(map->pmap, va, prot);
1102 		if (*mp == NULL)
1103 			pmap_failed = TRUE;
1104 		else if ((prot & VM_PROT_WRITE) != 0 &&
1105 		    (*mp)->dirty != VM_PAGE_BITS_ALL) {
1106 			/*
1107 			 * Explicitly dirty the physical page.  Otherwise, the
1108 			 * caller's changes may go unnoticed because they are
1109 			 * performed through an unmanaged mapping or by a DMA
1110 			 * operation.
1111 			 *
1112 			 * The object lock is not held here.
1113 			 * See vm_page_clear_dirty_mask().
1114 			 */
1115 			vm_page_dirty(*mp);
1116 		}
1117 	}
1118 	if (pmap_failed) {
1119 		/*
1120 		 * One or more pages could not be held by the pmap.  Either no
1121 		 * page was mapped at the specified virtual address or that
1122 		 * mapping had insufficient permissions.  Attempt to fault in
1123 		 * and hold these pages.
1124 		 */
1125 		for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1126 			if (*mp == NULL && vm_fault_hold(map, va, prot,
1127 			    VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1128 				goto error;
1129 	}
1130 	return (count);
1131 error:
1132 	for (mp = ma; mp < ma + count; mp++)
1133 		if (*mp != NULL) {
1134 			vm_page_lock(*mp);
1135 			vm_page_unhold(*mp);
1136 			vm_page_unlock(*mp);
1137 		}
1138 	return (-1);
1139 }
1140 
1141 /*
1142  *	vm_fault_wire:
1143  *
1144  *	Wire down a range of virtual addresses in a map.
1145  */
1146 int
1147 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1148     boolean_t fictitious)
1149 {
1150 	vm_offset_t va;
1151 	int rv;
1152 
1153 	/*
1154 	 * We simulate a fault to get the page and enter it in the physical
1155 	 * map.  For user wiring, we only ask for read access on currently
1156 	 * read-only sections.
1157 	 */
1158 	for (va = start; va < end; va += PAGE_SIZE) {
1159 		rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
1160 		if (rv) {
1161 			if (va != start)
1162 				vm_fault_unwire(map, start, va, fictitious);
1163 			return (rv);
1164 		}
1165 	}
1166 	return (KERN_SUCCESS);
1167 }
1168 
1169 /*
1170  *	vm_fault_unwire:
1171  *
1172  *	Unwire a range of virtual addresses in a map.
1173  */
1174 void
1175 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1176     boolean_t fictitious)
1177 {
1178 	vm_paddr_t pa;
1179 	vm_offset_t va;
1180 	vm_page_t m;
1181 	pmap_t pmap;
1182 
1183 	pmap = vm_map_pmap(map);
1184 
1185 	/*
1186 	 * Since the pages are wired down, we must be able to get their
1187 	 * mappings from the physical map system.
1188 	 */
1189 	for (va = start; va < end; va += PAGE_SIZE) {
1190 		pa = pmap_extract(pmap, va);
1191 		if (pa != 0) {
1192 			pmap_change_wiring(pmap, va, FALSE);
1193 			if (!fictitious) {
1194 				m = PHYS_TO_VM_PAGE(pa);
1195 				vm_page_lock(m);
1196 				vm_page_unwire(m, TRUE);
1197 				vm_page_unlock(m);
1198 			}
1199 		}
1200 	}
1201 }
1202 
1203 /*
1204  *	Routine:
1205  *		vm_fault_copy_entry
1206  *	Function:
1207  *		Create new shadow object backing dst_entry with private copy of
1208  *		all underlying pages. When src_entry is equal to dst_entry,
1209  *		function implements COW for wired-down map entry. Otherwise,
1210  *		it forks wired entry into dst_map.
1211  *
1212  *	In/out conditions:
1213  *		The source and destination maps must be locked for write.
1214  *		The source map entry must be wired down (or be a sharing map
1215  *		entry corresponding to a main map entry that is wired down).
1216  */
1217 void
1218 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1219     vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1220     vm_ooffset_t *fork_charge)
1221 {
1222 	vm_object_t backing_object, dst_object, object, src_object;
1223 	vm_pindex_t dst_pindex, pindex, src_pindex;
1224 	vm_prot_t access, prot;
1225 	vm_offset_t vaddr;
1226 	vm_page_t dst_m;
1227 	vm_page_t src_m;
1228 	boolean_t src_readonly, upgrade;
1229 
1230 #ifdef	lint
1231 	src_map++;
1232 #endif	/* lint */
1233 
1234 	upgrade = src_entry == dst_entry;
1235 
1236 	src_object = src_entry->object.vm_object;
1237 	src_pindex = OFF_TO_IDX(src_entry->offset);
1238 	src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1239 
1240 	/*
1241 	 * Create the top-level object for the destination entry. (Doesn't
1242 	 * actually shadow anything - we copy the pages directly.)
1243 	 */
1244 	dst_object = vm_object_allocate(OBJT_DEFAULT,
1245 	    OFF_TO_IDX(dst_entry->end - dst_entry->start));
1246 #if VM_NRESERVLEVEL > 0
1247 	dst_object->flags |= OBJ_COLORED;
1248 	dst_object->pg_color = atop(dst_entry->start);
1249 #endif
1250 
1251 	VM_OBJECT_WLOCK(dst_object);
1252 	KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1253 	    ("vm_fault_copy_entry: vm_object not NULL"));
1254 	dst_entry->object.vm_object = dst_object;
1255 	dst_entry->offset = 0;
1256 	dst_object->charge = dst_entry->end - dst_entry->start;
1257 	if (fork_charge != NULL) {
1258 		KASSERT(dst_entry->cred == NULL,
1259 		    ("vm_fault_copy_entry: leaked swp charge"));
1260 		dst_object->cred = curthread->td_ucred;
1261 		crhold(dst_object->cred);
1262 		*fork_charge += dst_object->charge;
1263 	} else {
1264 		dst_object->cred = dst_entry->cred;
1265 		dst_entry->cred = NULL;
1266 	}
1267 	access = prot = dst_entry->protection;
1268 	/*
1269 	 * If not an upgrade, then enter the mappings in the pmap as
1270 	 * read and/or execute accesses.  Otherwise, enter them as
1271 	 * write accesses.
1272 	 *
1273 	 * A writeable large page mapping is only created if all of
1274 	 * the constituent small page mappings are modified. Marking
1275 	 * PTEs as modified on inception allows promotion to happen
1276 	 * without taking potentially large number of soft faults.
1277 	 */
1278 	if (!upgrade)
1279 		access &= ~VM_PROT_WRITE;
1280 
1281 	/*
1282 	 * Loop through all of the virtual pages within the entry's
1283 	 * range, copying each page from the source object to the
1284 	 * destination object.  Since the source is wired, those pages
1285 	 * must exist.  In contrast, the destination is pageable.
1286 	 * Since the destination object does share any backing storage
1287 	 * with the source object, all of its pages must be dirtied,
1288 	 * regardless of whether they can be written.
1289 	 */
1290 	for (vaddr = dst_entry->start, dst_pindex = 0;
1291 	    vaddr < dst_entry->end;
1292 	    vaddr += PAGE_SIZE, dst_pindex++) {
1293 
1294 		/*
1295 		 * Allocate a page in the destination object.
1296 		 */
1297 		do {
1298 			dst_m = vm_page_alloc(dst_object, dst_pindex,
1299 			    VM_ALLOC_NORMAL);
1300 			if (dst_m == NULL) {
1301 				VM_OBJECT_WUNLOCK(dst_object);
1302 				VM_WAIT;
1303 				VM_OBJECT_WLOCK(dst_object);
1304 			}
1305 		} while (dst_m == NULL);
1306 
1307 		/*
1308 		 * Find the page in the source object, and copy it in.
1309 		 * (Because the source is wired down, the page will be in
1310 		 * memory.)
1311 		 */
1312 		VM_OBJECT_RLOCK(src_object);
1313 		object = src_object;
1314 		pindex = src_pindex + dst_pindex;
1315 		while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1316 		    src_readonly &&
1317 		    (backing_object = object->backing_object) != NULL) {
1318 			/*
1319 			 * Allow fallback to backing objects if we are reading.
1320 			 */
1321 			VM_OBJECT_RLOCK(backing_object);
1322 			pindex += OFF_TO_IDX(object->backing_object_offset);
1323 			VM_OBJECT_RUNLOCK(object);
1324 			object = backing_object;
1325 		}
1326 		if (src_m == NULL)
1327 			panic("vm_fault_copy_wired: page missing");
1328 		pmap_copy_page(src_m, dst_m);
1329 		VM_OBJECT_RUNLOCK(object);
1330 		dst_m->valid = VM_PAGE_BITS_ALL;
1331 		dst_m->dirty = VM_PAGE_BITS_ALL;
1332 		VM_OBJECT_WUNLOCK(dst_object);
1333 
1334 		/*
1335 		 * Enter it in the pmap. If a wired, copy-on-write
1336 		 * mapping is being replaced by a write-enabled
1337 		 * mapping, then wire that new mapping.
1338 		 */
1339 		pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1340 
1341 		/*
1342 		 * Mark it no longer busy, and put it on the active list.
1343 		 */
1344 		VM_OBJECT_WLOCK(dst_object);
1345 
1346 		if (upgrade) {
1347 			vm_page_lock(src_m);
1348 			vm_page_unwire(src_m, 0);
1349 			vm_page_unlock(src_m);
1350 
1351 			vm_page_lock(dst_m);
1352 			vm_page_wire(dst_m);
1353 			vm_page_unlock(dst_m);
1354 		} else {
1355 			vm_page_lock(dst_m);
1356 			vm_page_activate(dst_m);
1357 			vm_page_unlock(dst_m);
1358 		}
1359 		vm_page_xunbusy(dst_m);
1360 	}
1361 	VM_OBJECT_WUNLOCK(dst_object);
1362 	if (upgrade) {
1363 		dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1364 		vm_object_deallocate(src_object);
1365 	}
1366 }
1367 
1368 
1369 /*
1370  * This routine checks around the requested page for other pages that
1371  * might be able to be faulted in.  This routine brackets the viable
1372  * pages for the pages to be paged in.
1373  *
1374  * Inputs:
1375  *	m, rbehind, rahead
1376  *
1377  * Outputs:
1378  *  marray (array of vm_page_t), reqpage (index of requested page)
1379  *
1380  * Return value:
1381  *  number of pages in marray
1382  */
1383 static int
1384 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1385 	vm_page_t m;
1386 	int rbehind;
1387 	int rahead;
1388 	vm_page_t *marray;
1389 	int *reqpage;
1390 {
1391 	int i,j;
1392 	vm_object_t object;
1393 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
1394 	vm_page_t rtm;
1395 	int cbehind, cahead;
1396 
1397 	VM_OBJECT_ASSERT_WLOCKED(m->object);
1398 
1399 	object = m->object;
1400 	pindex = m->pindex;
1401 	cbehind = cahead = 0;
1402 
1403 	/*
1404 	 * if the requested page is not available, then give up now
1405 	 */
1406 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1407 		return 0;
1408 	}
1409 
1410 	if ((cbehind == 0) && (cahead == 0)) {
1411 		*reqpage = 0;
1412 		marray[0] = m;
1413 		return 1;
1414 	}
1415 
1416 	if (rahead > cahead) {
1417 		rahead = cahead;
1418 	}
1419 
1420 	if (rbehind > cbehind) {
1421 		rbehind = cbehind;
1422 	}
1423 
1424 	/*
1425 	 * scan backward for the read behind pages -- in memory
1426 	 */
1427 	if (pindex > 0) {
1428 		if (rbehind > pindex) {
1429 			rbehind = pindex;
1430 			startpindex = 0;
1431 		} else {
1432 			startpindex = pindex - rbehind;
1433 		}
1434 
1435 		if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1436 		    rtm->pindex >= startpindex)
1437 			startpindex = rtm->pindex + 1;
1438 
1439 		/* tpindex is unsigned; beware of numeric underflow. */
1440 		for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1441 		    tpindex < pindex; i++, tpindex--) {
1442 
1443 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1444 			    VM_ALLOC_IFNOTCACHED);
1445 			if (rtm == NULL) {
1446 				/*
1447 				 * Shift the allocated pages to the
1448 				 * beginning of the array.
1449 				 */
1450 				for (j = 0; j < i; j++) {
1451 					marray[j] = marray[j + tpindex + 1 -
1452 					    startpindex];
1453 				}
1454 				break;
1455 			}
1456 
1457 			marray[tpindex - startpindex] = rtm;
1458 		}
1459 	} else {
1460 		startpindex = 0;
1461 		i = 0;
1462 	}
1463 
1464 	marray[i] = m;
1465 	/* page offset of the required page */
1466 	*reqpage = i;
1467 
1468 	tpindex = pindex + 1;
1469 	i++;
1470 
1471 	/*
1472 	 * scan forward for the read ahead pages
1473 	 */
1474 	endpindex = tpindex + rahead;
1475 	if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1476 		endpindex = rtm->pindex;
1477 	if (endpindex > object->size)
1478 		endpindex = object->size;
1479 
1480 	for (; tpindex < endpindex; i++, tpindex++) {
1481 
1482 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1483 		    VM_ALLOC_IFNOTCACHED);
1484 		if (rtm == NULL) {
1485 			break;
1486 		}
1487 
1488 		marray[i] = rtm;
1489 	}
1490 
1491 	/* return number of pages */
1492 	return i;
1493 }
1494 
1495 /*
1496  * Block entry into the machine-independent layer's page fault handler by
1497  * the calling thread.  Subsequent calls to vm_fault() by that thread will
1498  * return KERN_PROTECTION_FAILURE.  Enable machine-dependent handling of
1499  * spurious page faults.
1500  */
1501 int
1502 vm_fault_disable_pagefaults(void)
1503 {
1504 
1505 	return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1506 }
1507 
1508 void
1509 vm_fault_enable_pagefaults(int save)
1510 {
1511 
1512 	curthread_pflags_restore(save);
1513 }
1514