xref: /dragonfly/sys/vm/vm_fault.c (revision bf31779e)
1 /*
2  * Copyright (c) 2003-2014 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  *
34  * ---
35  *
36  * Copyright (c) 1991, 1993
37  *	The Regents of the University of California.  All rights reserved.
38  * Copyright (c) 1994 John S. Dyson
39  * All rights reserved.
40  * Copyright (c) 1994 David Greenman
41  * All rights reserved.
42  *
43  *
44  * This code is derived from software contributed to Berkeley by
45  * The Mach Operating System project at Carnegie-Mellon University.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  * ---
72  *
73  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
74  * All rights reserved.
75  *
76  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
77  *
78  * Permission to use, copy, modify and distribute this software and
79  * its documentation is hereby granted, provided that both the copyright
80  * notice and this permission notice appear in all copies of the
81  * software, derivative works or modified versions, and any portions
82  * thereof, and that both notices appear in supporting documentation.
83  *
84  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
85  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
86  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
87  *
88  * Carnegie Mellon requests users of this software to return to
89  *
90  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
91  *  School of Computer Science
92  *  Carnegie Mellon University
93  *  Pittsburgh PA 15213-3890
94  *
95  * any improvements or extensions that they make and grant Carnegie the
96  * rights to redistribute these changes.
97  */
98 
99 /*
100  *	Page fault handling module.
101  */
102 
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/proc.h>
107 #include <sys/vnode.h>
108 #include <sys/resourcevar.h>
109 #include <sys/vmmeter.h>
110 #include <sys/vkernel.h>
111 #include <sys/lock.h>
112 #include <sys/sysctl.h>
113 
114 #include <cpu/lwbuf.h>
115 
116 #include <vm/vm.h>
117 #include <vm/vm_param.h>
118 #include <vm/pmap.h>
119 #include <vm/vm_map.h>
120 #include <vm/vm_object.h>
121 #include <vm/vm_page.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_pager.h>
125 #include <vm/vnode_pager.h>
126 #include <vm/vm_extern.h>
127 
128 #include <sys/thread2.h>
129 #include <vm/vm_page2.h>
130 
131 struct faultstate {
132 	vm_page_t m;
133 	vm_object_t object;
134 	vm_pindex_t pindex;
135 	vm_prot_t prot;
136 	vm_page_t first_m;
137 	vm_object_t first_object;
138 	vm_prot_t first_prot;
139 	vm_map_t map;
140 	vm_map_entry_t entry;
141 	int lookup_still_valid;
142 	int hardfault;
143 	int fault_flags;
144 	int map_generation;
145 	int shared;
146 	int first_shared;
147 	boolean_t wired;
148 	struct vnode *vp;
149 };
150 
151 static int debug_fault = 0;
152 SYSCTL_INT(_vm, OID_AUTO, debug_fault, CTLFLAG_RW, &debug_fault, 0, "");
153 static int debug_cluster = 0;
154 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
155 static int virtual_copy_enable = 1;
156 SYSCTL_INT(_vm, OID_AUTO, virtual_copy_enable, CTLFLAG_RW,
157 		&virtual_copy_enable, 0, "");
158 int vm_shared_fault = 1;
159 TUNABLE_INT("vm.shared_fault", &vm_shared_fault);
160 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW,
161 		&vm_shared_fault, 0, "Allow shared token on vm_object");
162 
163 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t, int);
164 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *,
165 			vpte_t, int, int);
166 #if 0
167 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
168 #endif
169 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
170 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
171 			vm_map_entry_t entry, int prot, int fault_flags);
172 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
173 			vm_map_entry_t entry, int prot, int fault_flags);
174 
175 static __inline void
176 release_page(struct faultstate *fs)
177 {
178 	vm_page_deactivate(fs->m);
179 	vm_page_wakeup(fs->m);
180 	fs->m = NULL;
181 }
182 
183 /*
184  * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
185  *	 requires relocking and then checking the timestamp.
186  *
187  * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
188  *	 not have to update fs->map_generation here.
189  *
190  * NOTE: This function can fail due to a deadlock against the caller's
191  *	 holding of a vm_page BUSY.
192  */
193 static __inline int
194 relock_map(struct faultstate *fs)
195 {
196 	int error;
197 
198 	if (fs->lookup_still_valid == FALSE && fs->map) {
199 		error = vm_map_lock_read_to(fs->map);
200 		if (error == 0)
201 			fs->lookup_still_valid = TRUE;
202 	} else {
203 		error = 0;
204 	}
205 	return error;
206 }
207 
208 static __inline void
209 unlock_map(struct faultstate *fs)
210 {
211 	if (fs->lookup_still_valid && fs->map) {
212 		vm_map_lookup_done(fs->map, fs->entry, 0);
213 		fs->lookup_still_valid = FALSE;
214 	}
215 }
216 
217 /*
218  * Clean up after a successful call to vm_fault_object() so another call
219  * to vm_fault_object() can be made.
220  */
221 static void
222 _cleanup_successful_fault(struct faultstate *fs, int relock)
223 {
224 	/*
225 	 * We allocated a junk page for a COW operation that did
226 	 * not occur, the page must be freed.
227 	 */
228 	if (fs->object != fs->first_object) {
229 		KKASSERT(fs->first_shared == 0);
230 		vm_page_free(fs->first_m);
231 		vm_object_pip_wakeup(fs->object);
232 		fs->first_m = NULL;
233 	}
234 
235 	/*
236 	 * Reset fs->object.
237 	 */
238 	fs->object = fs->first_object;
239 	if (relock && fs->lookup_still_valid == FALSE) {
240 		if (fs->map)
241 			vm_map_lock_read(fs->map);
242 		fs->lookup_still_valid = TRUE;
243 	}
244 }
245 
246 static void
247 _unlock_things(struct faultstate *fs, int dealloc)
248 {
249 	_cleanup_successful_fault(fs, 0);
250 	if (dealloc) {
251 		/*vm_object_deallocate(fs->first_object);*/
252 		/*fs->first_object = NULL; drop used later on */
253 	}
254 	unlock_map(fs);
255 	if (fs->vp != NULL) {
256 		vput(fs->vp);
257 		fs->vp = NULL;
258 	}
259 }
260 
261 #define unlock_things(fs) _unlock_things(fs, 0)
262 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
263 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
264 
265 /*
266  * Virtual copy tests.   Used by the fault code to determine if a
267  * page can be moved from an orphan vm_object into its shadow
268  * instead of copying its contents.
269  */
270 static __inline int
271 virtual_copy_test(struct faultstate *fs)
272 {
273 	/*
274 	 * Must be holding exclusive locks
275 	 */
276 	if (fs->first_shared || fs->shared || virtual_copy_enable == 0)
277 		return 0;
278 
279 	/*
280 	 * Map, if present, has not changed
281 	 */
282 	if (fs->map && fs->map_generation != fs->map->timestamp)
283 		return 0;
284 
285 	/*
286 	 * Only one shadow object
287 	 */
288 	if (fs->object->shadow_count != 1)
289 		return 0;
290 
291 	/*
292 	 * No COW refs, except us
293 	 */
294 	if (fs->object->ref_count != 1)
295 		return 0;
296 
297 	/*
298 	 * No one else can look this object up
299 	 */
300 	if (fs->object->handle != NULL)
301 		return 0;
302 
303 	/*
304 	 * No other ways to look the object up
305 	 */
306 	if (fs->object->type != OBJT_DEFAULT &&
307 	    fs->object->type != OBJT_SWAP)
308 		return 0;
309 
310 	/*
311 	 * We don't chase down the shadow chain
312 	 */
313 	if (fs->object != fs->first_object->backing_object)
314 		return 0;
315 
316 	return 1;
317 }
318 
319 static __inline int
320 virtual_copy_ok(struct faultstate *fs)
321 {
322 	if (virtual_copy_test(fs)) {
323 		/*
324 		 * Grab the lock and re-test changeable items.
325 		 */
326 		if (fs->lookup_still_valid == FALSE && fs->map) {
327 			if (lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT))
328 				return 0;
329 			fs->lookup_still_valid = TRUE;
330 			if (virtual_copy_test(fs)) {
331 				fs->map_generation = ++fs->map->timestamp;
332 				return 1;
333 			}
334 			fs->lookup_still_valid = FALSE;
335 			lockmgr(&fs->map->lock, LK_RELEASE);
336 		}
337 	}
338 	return 0;
339 }
340 
341 /*
342  * TRYPAGER
343  *
344  * Determine if the pager for the current object *might* contain the page.
345  *
346  * We only need to try the pager if this is not a default object (default
347  * objects are zero-fill and have no real pager), and if we are not taking
348  * a wiring fault or if the FS entry is wired.
349  */
350 #define TRYPAGER(fs)	\
351 		(fs->object->type != OBJT_DEFAULT && \
352 		(((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
353 
354 /*
355  * vm_fault:
356  *
357  * Handle a page fault occuring at the given address, requiring the given
358  * permissions, in the map specified.  If successful, the page is inserted
359  * into the associated physical map.
360  *
361  * NOTE: The given address should be truncated to the proper page address.
362  *
363  * KERN_SUCCESS is returned if the page fault is handled; otherwise,
364  * a standard error specifying why the fault is fatal is returned.
365  *
366  * The map in question must be referenced, and remains so.
367  * The caller may hold no locks.
368  * No other requirements.
369  */
370 int
371 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
372 {
373 	int result;
374 	vm_pindex_t first_pindex;
375 	struct faultstate fs;
376 	struct lwp *lp;
377 	struct proc *p;
378 	thread_t td;
379 	struct vm_map_ilock ilock;
380 	int didilock;
381 	int growstack;
382 	int retry = 0;
383 	int inherit_prot;
384 
385 	inherit_prot = fault_type & VM_PROT_NOSYNC;
386 	fs.hardfault = 0;
387 	fs.fault_flags = fault_flags;
388 	fs.vp = NULL;
389 	fs.shared = vm_shared_fault;
390 	fs.first_shared = vm_shared_fault;
391 	growstack = 1;
392 
393 	/*
394 	 * vm_map interactions
395 	 */
396 	td = curthread;
397 	if ((lp = td->td_lwp) != NULL)
398 		lp->lwp_flags |= LWP_PAGING;
399 
400 RetryFault:
401 	/*
402 	 * Find the vm_map_entry representing the backing store and resolve
403 	 * the top level object and page index.  This may have the side
404 	 * effect of executing a copy-on-write on the map entry,
405 	 * creating a shadow object, or splitting an anonymous entry for
406 	 * performance, but will not COW any actual VM pages.
407 	 *
408 	 * On success fs.map is left read-locked and various other fields
409 	 * are initialized but not otherwise referenced or locked.
410 	 *
411 	 * NOTE!  vm_map_lookup will try to upgrade the fault_type to
412 	 *	  VM_FAULT_WRITE if the map entry is a virtual page table
413 	 *	  and also writable, so we can set the 'A'accessed bit in
414 	 *	  the virtual page table entry.
415 	 */
416 	fs.map = map;
417 	result = vm_map_lookup(&fs.map, vaddr, fault_type,
418 			       &fs.entry, &fs.first_object,
419 			       &first_pindex, &fs.first_prot, &fs.wired);
420 
421 	/*
422 	 * If the lookup failed or the map protections are incompatible,
423 	 * the fault generally fails.
424 	 *
425 	 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
426 	 * tried to do a COW fault.
427 	 *
428 	 * If the caller is trying to do a user wiring we have more work
429 	 * to do.
430 	 */
431 	if (result != KERN_SUCCESS) {
432 		if (result == KERN_FAILURE_NOFAULT) {
433 			result = KERN_FAILURE;
434 			goto done;
435 		}
436 		if (result != KERN_PROTECTION_FAILURE ||
437 		    (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
438 		{
439 			if (result == KERN_INVALID_ADDRESS && growstack &&
440 			    map != &kernel_map && curproc != NULL) {
441 				result = vm_map_growstack(map, vaddr);
442 				if (result == KERN_SUCCESS) {
443 					growstack = 0;
444 					++retry;
445 					goto RetryFault;
446 				}
447 				result = KERN_FAILURE;
448 			}
449 			goto done;
450 		}
451 
452 		/*
453 		 * If we are user-wiring a r/w segment, and it is COW, then
454 		 * we need to do the COW operation.  Note that we don't
455 		 * currently COW RO sections now, because it is NOT desirable
456 		 * to COW .text.  We simply keep .text from ever being COW'ed
457 		 * and take the heat that one cannot debug wired .text sections.
458 		 */
459 		result = vm_map_lookup(&fs.map, vaddr,
460 				       VM_PROT_READ|VM_PROT_WRITE|
461 				        VM_PROT_OVERRIDE_WRITE,
462 				       &fs.entry, &fs.first_object,
463 				       &first_pindex, &fs.first_prot,
464 				       &fs.wired);
465 		if (result != KERN_SUCCESS) {
466 			/* could also be KERN_FAILURE_NOFAULT */
467 			result = KERN_FAILURE;
468 			goto done;
469 		}
470 
471 		/*
472 		 * If we don't COW now, on a user wire, the user will never
473 		 * be able to write to the mapping.  If we don't make this
474 		 * restriction, the bookkeeping would be nearly impossible.
475 		 *
476 		 * XXX We have a shared lock, this will have a MP race but
477 		 * I don't see how it can hurt anything.
478 		 */
479 		if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
480 			atomic_clear_char(&fs.entry->max_protection,
481 					  VM_PROT_WRITE);
482 		}
483 	}
484 
485 	/*
486 	 * fs.map is read-locked
487 	 *
488 	 * Misc checks.  Save the map generation number to detect races.
489 	 */
490 	fs.map_generation = fs.map->timestamp;
491 	fs.lookup_still_valid = TRUE;
492 	fs.first_m = NULL;
493 	fs.object = fs.first_object;	/* so unlock_and_deallocate works */
494 	fs.prot = fs.first_prot;	/* default (used by uksmap) */
495 
496 	if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
497 		if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
498 			panic("vm_fault: fault on nofault entry, addr: %p",
499 			      (void *)vaddr);
500 		}
501 		if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
502 		    vaddr >= fs.entry->start &&
503 		    vaddr < fs.entry->start + PAGE_SIZE) {
504 			panic("vm_fault: fault on stack guard, addr: %p",
505 			      (void *)vaddr);
506 		}
507 	}
508 
509 	/*
510 	 * A user-kernel shared map has no VM object and bypasses
511 	 * everything.  We execute the uksmap function with a temporary
512 	 * fictitious vm_page.  The address is directly mapped with no
513 	 * management.
514 	 */
515 	if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
516 		struct vm_page fakem;
517 
518 		bzero(&fakem, sizeof(fakem));
519 		fakem.pindex = first_pindex;
520 		fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
521 		fakem.busy_count = PBUSY_LOCKED;
522 		fakem.valid = VM_PAGE_BITS_ALL;
523 		fakem.pat_mode = VM_MEMATTR_DEFAULT;
524 		if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
525 			result = KERN_FAILURE;
526 			unlock_things(&fs);
527 			goto done2;
528 		}
529 		pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
530 			   fs.wired, fs.entry);
531 		goto done_success;
532 	}
533 
534 	/*
535 	 * A system map entry may return a NULL object.  No object means
536 	 * no pager means an unrecoverable kernel fault.
537 	 */
538 	if (fs.first_object == NULL) {
539 		panic("vm_fault: unrecoverable fault at %p in entry %p",
540 			(void *)vaddr, fs.entry);
541 	}
542 
543 	/*
544 	 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
545 	 * is set.
546 	 *
547 	 * Unfortunately a deadlock can occur if we are forced to page-in
548 	 * from swap, but diving all the way into the vm_pager_get_page()
549 	 * function to find out is too much.  Just check the object type.
550 	 *
551 	 * The deadlock is a CAM deadlock on a busy VM page when trying
552 	 * to finish an I/O if another process gets stuck in
553 	 * vop_helper_read_shortcut() due to a swap fault.
554 	 */
555 	if ((td->td_flags & TDF_NOFAULT) &&
556 	    (retry ||
557 	     fs.first_object->type == OBJT_VNODE ||
558 	     fs.first_object->type == OBJT_SWAP ||
559 	     fs.first_object->backing_object)) {
560 		result = KERN_FAILURE;
561 		unlock_things(&fs);
562 		goto done2;
563 	}
564 
565 	/*
566 	 * If the entry is wired we cannot change the page protection.
567 	 */
568 	if (fs.wired)
569 		fault_type = fs.first_prot;
570 
571 	/*
572 	 * We generally want to avoid unnecessary exclusive modes on backing
573 	 * and terminal objects because this can seriously interfere with
574 	 * heavily fork()'d processes (particularly /bin/sh scripts).
575 	 *
576 	 * However, we also want to avoid unnecessary retries due to needed
577 	 * shared->exclusive promotion for common faults.  Exclusive mode is
578 	 * always needed if any page insertion, rename, or free occurs in an
579 	 * object (and also indirectly if any I/O is done).
580 	 *
581 	 * The main issue here is going to be fs.first_shared.  If the
582 	 * first_object has a backing object which isn't shadowed and the
583 	 * process is single-threaded we might as well use an exclusive
584 	 * lock/chain right off the bat.
585 	 */
586 	if (fs.first_shared && fs.first_object->backing_object &&
587 	    LIST_EMPTY(&fs.first_object->shadow_head) &&
588 	    td->td_proc && td->td_proc->p_nthreads == 1) {
589 		fs.first_shared = 0;
590 	}
591 
592 	/*
593 	 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
594 	 * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
595 	 *		     we can try shared first.
596 	 */
597 	if (fault_flags & VM_FAULT_UNSWAP) {
598 		fs.first_shared = 0;
599 	}
600 
601 	/*
602 	 * Obtain a top-level object lock, shared or exclusive depending
603 	 * on fs.first_shared.  If a shared lock winds up being insufficient
604 	 * we will retry with an exclusive lock.
605 	 *
606 	 * The vnode pager lock is always shared.
607 	 */
608 	if (fs.first_shared)
609 		vm_object_hold_shared(fs.first_object);
610 	else
611 		vm_object_hold(fs.first_object);
612 	if (fs.vp == NULL)
613 		fs.vp = vnode_pager_lock(fs.first_object);
614 
615 	/*
616 	 * The page we want is at (first_object, first_pindex), but if the
617 	 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
618 	 * page table to figure out the actual pindex.
619 	 *
620 	 * NOTE!  DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
621 	 * ONLY
622 	 */
623 	didilock = 0;
624 	if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
625 		vm_map_interlock(fs.map, &ilock, vaddr, vaddr + PAGE_SIZE);
626 		didilock = 1;
627 		result = vm_fault_vpagetable(&fs, &first_pindex,
628 					     fs.entry->aux.master_pde,
629 					     fault_type, 1);
630 		if (result == KERN_TRY_AGAIN) {
631 			vm_map_deinterlock(fs.map, &ilock);
632 			vm_object_drop(fs.first_object);
633 			++retry;
634 			goto RetryFault;
635 		}
636 		if (result != KERN_SUCCESS) {
637 			vm_map_deinterlock(fs.map, &ilock);
638 			goto done;
639 		}
640 	}
641 
642 	/*
643 	 * Now we have the actual (object, pindex), fault in the page.  If
644 	 * vm_fault_object() fails it will unlock and deallocate the FS
645 	 * data.   If it succeeds everything remains locked and fs->object
646 	 * will have an additional PIP count if it is not equal to
647 	 * fs->first_object
648 	 *
649 	 * vm_fault_object will set fs->prot for the pmap operation.  It is
650 	 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
651 	 * page can be safely written.  However, it will force a read-only
652 	 * mapping for a read fault if the memory is managed by a virtual
653 	 * page table.
654 	 *
655 	 * If the fault code uses the shared object lock shortcut
656 	 * we must not try to burst (we can't allocate VM pages).
657 	 */
658 	result = vm_fault_object(&fs, first_pindex, fault_type, 1);
659 
660 	if (debug_fault > 0) {
661 		--debug_fault;
662 		kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
663 			"fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
664 			result, (intmax_t)vaddr, fault_type, fault_flags,
665 			fs.m, fs.prot, fs.wired, fs.entry);
666 	}
667 
668 	if (result == KERN_TRY_AGAIN) {
669 		if (didilock)
670 			vm_map_deinterlock(fs.map, &ilock);
671 		vm_object_drop(fs.first_object);
672 		++retry;
673 		goto RetryFault;
674 	}
675 	if (result != KERN_SUCCESS) {
676 		if (didilock)
677 			vm_map_deinterlock(fs.map, &ilock);
678 		goto done;
679 	}
680 
681 	/*
682 	 * On success vm_fault_object() does not unlock or deallocate, and fs.m
683 	 * will contain a busied page.
684 	 *
685 	 * Enter the page into the pmap and do pmap-related adjustments.
686 	 */
687 	KKASSERT(fs.lookup_still_valid == TRUE);
688 	vm_page_flag_set(fs.m, PG_REFERENCED);
689 	pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot,
690 		   fs.wired, fs.entry);
691 
692 	if (didilock)
693 		vm_map_deinterlock(fs.map, &ilock);
694 
695 	/*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
696 	KKASSERT(fs.m->busy_count & PBUSY_LOCKED);
697 
698 	/*
699 	 * If the page is not wired down, then put it where the pageout daemon
700 	 * can find it.
701 	 */
702 	if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
703 		if (fs.wired)
704 			vm_page_wire(fs.m);
705 		else
706 			vm_page_unwire(fs.m, 1);
707 	} else {
708 		vm_page_activate(fs.m);
709 	}
710 	vm_page_wakeup(fs.m);
711 
712 	/*
713 	 * Burst in a few more pages if possible.  The fs.map should still
714 	 * be locked.  To avoid interlocking against a vnode->getblk
715 	 * operation we had to be sure to unbusy our primary vm_page above
716 	 * first.
717 	 *
718 	 * A normal burst can continue down backing store, only execute
719 	 * if we are holding an exclusive lock, otherwise the exclusive
720 	 * locks the burst code gets might cause excessive SMP collisions.
721 	 *
722 	 * A quick burst can be utilized when there is no backing object
723 	 * (i.e. a shared file mmap).
724 	 */
725 	if ((fault_flags & VM_FAULT_BURST) &&
726 	    (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
727 	    fs.wired == 0) {
728 		if (fs.first_shared == 0 && fs.shared == 0) {
729 			vm_prefault(fs.map->pmap, vaddr,
730 				    fs.entry, fs.prot, fault_flags);
731 		} else {
732 			vm_prefault_quick(fs.map->pmap, vaddr,
733 					  fs.entry, fs.prot, fault_flags);
734 		}
735 	}
736 
737 done_success:
738 	mycpu->gd_cnt.v_vm_faults++;
739 	if (td->td_lwp)
740 		++td->td_lwp->lwp_ru.ru_minflt;
741 
742 	/*
743 	 * Unlock everything, and return
744 	 */
745 	unlock_things(&fs);
746 
747 	if (td->td_lwp) {
748 		if (fs.hardfault) {
749 			td->td_lwp->lwp_ru.ru_majflt++;
750 		} else {
751 			td->td_lwp->lwp_ru.ru_minflt++;
752 		}
753 	}
754 
755 	/*vm_object_deallocate(fs.first_object);*/
756 	/*fs.m = NULL; */
757 	/*fs.first_object = NULL; must still drop later */
758 
759 	result = KERN_SUCCESS;
760 done:
761 	if (fs.first_object)
762 		vm_object_drop(fs.first_object);
763 done2:
764 	if (lp)
765 		lp->lwp_flags &= ~LWP_PAGING;
766 
767 #if !defined(NO_SWAPPING)
768 	/*
769 	 * Check the process RSS limit and force deactivation and
770 	 * (asynchronous) paging if necessary.  This is a complex operation,
771 	 * only do it for direct user-mode faults, for now.
772 	 *
773 	 * To reduce overhead implement approximately a ~16MB hysteresis.
774 	 */
775 	p = td->td_proc;
776 	if ((fault_flags & VM_FAULT_USERMODE) && lp &&
777 	    p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 &&
778 	    map != &kernel_map) {
779 		vm_pindex_t limit;
780 		vm_pindex_t size;
781 
782 		limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
783 					p->p_rlimit[RLIMIT_RSS].rlim_max));
784 		size = pmap_resident_tlnw_count(map->pmap);
785 		if (limit >= 0 && size > 4096 && size - 4096 >= limit) {
786 			vm_pageout_map_deactivate_pages(map, limit);
787 		}
788 	}
789 #endif
790 
791 	return (result);
792 }
793 
794 /*
795  * Fault in the specified virtual address in the current process map,
796  * returning a held VM page or NULL.  See vm_fault_page() for more
797  * information.
798  *
799  * No requirements.
800  */
801 vm_page_t
802 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type,
803 		    int *errorp, int *busyp)
804 {
805 	struct lwp *lp = curthread->td_lwp;
806 	vm_page_t m;
807 
808 	m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
809 			  fault_type, VM_FAULT_NORMAL,
810 			  errorp, busyp);
811 	return(m);
812 }
813 
814 /*
815  * Fault in the specified virtual address in the specified map, doing all
816  * necessary manipulation of the object store and all necessary I/O.  Return
817  * a held VM page or NULL, and set *errorp.  The related pmap is not
818  * updated.
819  *
820  * If busyp is not NULL then *busyp will be set to TRUE if this routine
821  * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
822  * does not (VM_PROT_WRITE not specified or busyp is NULL).  If busyp is
823  * NULL the returned page is only held.
824  *
825  * If the caller has no intention of writing to the page's contents, busyp
826  * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
827  * without busying the page.
828  *
829  * The returned page will also be marked PG_REFERENCED.
830  *
831  * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
832  * error will be returned.
833  *
834  * No requirements.
835  */
836 vm_page_t
837 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
838 	      int fault_flags, int *errorp, int *busyp)
839 {
840 	vm_pindex_t first_pindex;
841 	struct faultstate fs;
842 	int result;
843 	int retry;
844 	int growstack;
845 	vm_prot_t orig_fault_type = fault_type;
846 
847 	retry = 0;
848 	fs.hardfault = 0;
849 	fs.fault_flags = fault_flags;
850 	KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
851 
852 	/*
853 	 * Dive the pmap (concurrency possible).  If we find the
854 	 * appropriate page we can terminate early and quickly.
855 	 *
856 	 * This works great for normal programs but will always return
857 	 * NULL for host lookups of vkernel maps in VMM mode.
858 	 *
859 	 * NOTE: pmap_fault_page_quick() might not busy the page.  If
860 	 *	 VM_PROT_WRITE or VM_PROT_OVERRIDE_WRITE is set in
861 	 *	 fault_type and pmap_fault_page_quick() returns non-NULL,
862 	 *	 it will safely dirty the returned vm_page_t for us.  We
863 	 *	 cannot safely dirty it here (it might not be busy).
864 	 */
865 	fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type, busyp);
866 	if (fs.m) {
867 		*errorp = 0;
868 		return(fs.m);
869 	}
870 
871 	/*
872 	 * Otherwise take a concurrency hit and do a formal page
873 	 * fault.
874 	 */
875 	fs.vp = NULL;
876 	fs.shared = vm_shared_fault;
877 	fs.first_shared = vm_shared_fault;
878 	growstack = 1;
879 
880 	/*
881 	 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
882 	 * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
883 	 *		     we can try shared first.
884 	 */
885 	if (fault_flags & VM_FAULT_UNSWAP) {
886 		fs.first_shared = 0;
887 	}
888 
889 RetryFault:
890 	/*
891 	 * Find the vm_map_entry representing the backing store and resolve
892 	 * the top level object and page index.  This may have the side
893 	 * effect of executing a copy-on-write on the map entry and/or
894 	 * creating a shadow object, but will not COW any actual VM pages.
895 	 *
896 	 * On success fs.map is left read-locked and various other fields
897 	 * are initialized but not otherwise referenced or locked.
898 	 *
899 	 * NOTE!  vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
900 	 *	  if the map entry is a virtual page table and also writable,
901 	 *	  so we can set the 'A'accessed bit in the virtual page table
902 	 *	  entry.
903 	 */
904 	fs.map = map;
905 	result = vm_map_lookup(&fs.map, vaddr, fault_type,
906 			       &fs.entry, &fs.first_object,
907 			       &first_pindex, &fs.first_prot, &fs.wired);
908 
909 	if (result != KERN_SUCCESS) {
910 		if (result == KERN_FAILURE_NOFAULT) {
911 			*errorp = KERN_FAILURE;
912 			fs.m = NULL;
913 			goto done;
914 		}
915 		if (result != KERN_PROTECTION_FAILURE ||
916 		    (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
917 		{
918 			if (result == KERN_INVALID_ADDRESS && growstack &&
919 			    map != &kernel_map && curproc != NULL) {
920 				result = vm_map_growstack(map, vaddr);
921 				if (result == KERN_SUCCESS) {
922 					growstack = 0;
923 					++retry;
924 					goto RetryFault;
925 				}
926 				result = KERN_FAILURE;
927 			}
928 			fs.m = NULL;
929 			*errorp = result;
930 			goto done;
931 		}
932 
933 		/*
934 		 * If we are user-wiring a r/w segment, and it is COW, then
935 		 * we need to do the COW operation.  Note that we don't
936 		 * currently COW RO sections now, because it is NOT desirable
937 		 * to COW .text.  We simply keep .text from ever being COW'ed
938 		 * and take the heat that one cannot debug wired .text sections.
939 		 */
940 		result = vm_map_lookup(&fs.map, vaddr,
941 				       VM_PROT_READ|VM_PROT_WRITE|
942 				        VM_PROT_OVERRIDE_WRITE,
943 				       &fs.entry, &fs.first_object,
944 				       &first_pindex, &fs.first_prot,
945 				       &fs.wired);
946 		if (result != KERN_SUCCESS) {
947 			/* could also be KERN_FAILURE_NOFAULT */
948 			*errorp = KERN_FAILURE;
949 			fs.m = NULL;
950 			goto done;
951 		}
952 
953 		/*
954 		 * If we don't COW now, on a user wire, the user will never
955 		 * be able to write to the mapping.  If we don't make this
956 		 * restriction, the bookkeeping would be nearly impossible.
957 		 *
958 		 * XXX We have a shared lock, this will have a MP race but
959 		 * I don't see how it can hurt anything.
960 		 */
961 		if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
962 			atomic_clear_char(&fs.entry->max_protection,
963 					  VM_PROT_WRITE);
964 		}
965 	}
966 
967 	/*
968 	 * fs.map is read-locked
969 	 *
970 	 * Misc checks.  Save the map generation number to detect races.
971 	 */
972 	fs.map_generation = fs.map->timestamp;
973 	fs.lookup_still_valid = TRUE;
974 	fs.first_m = NULL;
975 	fs.object = fs.first_object;	/* so unlock_and_deallocate works */
976 
977 	if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
978 		panic("vm_fault: fault on nofault entry, addr: %lx",
979 		    (u_long)vaddr);
980 	}
981 
982 	/*
983 	 * A user-kernel shared map has no VM object and bypasses
984 	 * everything.  We execute the uksmap function with a temporary
985 	 * fictitious vm_page.  The address is directly mapped with no
986 	 * management.
987 	 */
988 	if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
989 		struct vm_page fakem;
990 
991 		bzero(&fakem, sizeof(fakem));
992 		fakem.pindex = first_pindex;
993 		fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
994 		fakem.busy_count = PBUSY_LOCKED;
995 		fakem.valid = VM_PAGE_BITS_ALL;
996 		fakem.pat_mode = VM_MEMATTR_DEFAULT;
997 		if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
998 			*errorp = KERN_FAILURE;
999 			fs.m = NULL;
1000 			unlock_things(&fs);
1001 			goto done2;
1002 		}
1003 		fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
1004 		vm_page_hold(fs.m);
1005 		if (busyp)
1006 			*busyp = 0;	/* don't need to busy R or W */
1007 		unlock_things(&fs);
1008 		*errorp = 0;
1009 		goto done;
1010 	}
1011 
1012 
1013 	/*
1014 	 * A system map entry may return a NULL object.  No object means
1015 	 * no pager means an unrecoverable kernel fault.
1016 	 */
1017 	if (fs.first_object == NULL) {
1018 		panic("vm_fault: unrecoverable fault at %p in entry %p",
1019 			(void *)vaddr, fs.entry);
1020 	}
1021 
1022 	/*
1023 	 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
1024 	 * is set.
1025 	 *
1026 	 * Unfortunately a deadlock can occur if we are forced to page-in
1027 	 * from swap, but diving all the way into the vm_pager_get_page()
1028 	 * function to find out is too much.  Just check the object type.
1029 	 */
1030 	if ((curthread->td_flags & TDF_NOFAULT) &&
1031 	    (retry ||
1032 	     fs.first_object->type == OBJT_VNODE ||
1033 	     fs.first_object->type == OBJT_SWAP ||
1034 	     fs.first_object->backing_object)) {
1035 		*errorp = KERN_FAILURE;
1036 		unlock_things(&fs);
1037 		fs.m = NULL;
1038 		goto done2;
1039 	}
1040 
1041 	/*
1042 	 * If the entry is wired we cannot change the page protection.
1043 	 */
1044 	if (fs.wired)
1045 		fault_type = fs.first_prot;
1046 
1047 	/*
1048 	 * Make a reference to this object to prevent its disposal while we
1049 	 * are messing with it.  Once we have the reference, the map is free
1050 	 * to be diddled.  Since objects reference their shadows (and copies),
1051 	 * they will stay around as well.
1052 	 *
1053 	 * The reference should also prevent an unexpected collapse of the
1054 	 * parent that might move pages from the current object into the
1055 	 * parent unexpectedly, resulting in corruption.
1056 	 *
1057 	 * Bump the paging-in-progress count to prevent size changes (e.g.
1058 	 * truncation operations) during I/O.  This must be done after
1059 	 * obtaining the vnode lock in order to avoid possible deadlocks.
1060 	 */
1061 	if (fs.first_shared)
1062 		vm_object_hold_shared(fs.first_object);
1063 	else
1064 		vm_object_hold(fs.first_object);
1065 	if (fs.vp == NULL)
1066 		fs.vp = vnode_pager_lock(fs.first_object);	/* shared */
1067 
1068 	/*
1069 	 * The page we want is at (first_object, first_pindex), but if the
1070 	 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
1071 	 * page table to figure out the actual pindex.
1072 	 *
1073 	 * NOTE!  DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
1074 	 * ONLY
1075 	 */
1076 	if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1077 		result = vm_fault_vpagetable(&fs, &first_pindex,
1078 					     fs.entry->aux.master_pde,
1079 					     fault_type, 1);
1080 		if (result == KERN_TRY_AGAIN) {
1081 			vm_object_drop(fs.first_object);
1082 			++retry;
1083 			goto RetryFault;
1084 		}
1085 		if (result != KERN_SUCCESS) {
1086 			*errorp = result;
1087 			fs.m = NULL;
1088 			goto done;
1089 		}
1090 	}
1091 
1092 	/*
1093 	 * Now we have the actual (object, pindex), fault in the page.  If
1094 	 * vm_fault_object() fails it will unlock and deallocate the FS
1095 	 * data.   If it succeeds everything remains locked and fs->object
1096 	 * will have an additinal PIP count if it is not equal to
1097 	 * fs->first_object
1098 	 */
1099 	fs.m = NULL;
1100 	result = vm_fault_object(&fs, first_pindex, fault_type, 1);
1101 
1102 	if (result == KERN_TRY_AGAIN) {
1103 		vm_object_drop(fs.first_object);
1104 		++retry;
1105 		goto RetryFault;
1106 	}
1107 	if (result != KERN_SUCCESS) {
1108 		*errorp = result;
1109 		fs.m = NULL;
1110 		goto done;
1111 	}
1112 
1113 	if ((orig_fault_type & VM_PROT_WRITE) &&
1114 	    (fs.prot & VM_PROT_WRITE) == 0) {
1115 		*errorp = KERN_PROTECTION_FAILURE;
1116 		unlock_and_deallocate(&fs);
1117 		fs.m = NULL;
1118 		goto done;
1119 	}
1120 
1121 	/*
1122 	 * DO NOT UPDATE THE PMAP!!!  This function may be called for
1123 	 * a pmap unrelated to the current process pmap, in which case
1124 	 * the current cpu core will not be listed in the pmap's pm_active
1125 	 * mask.  Thus invalidation interlocks will fail to work properly.
1126 	 *
1127 	 * (for example, 'ps' uses procfs to read program arguments from
1128 	 * each process's stack).
1129 	 *
1130 	 * In addition to the above this function will be called to acquire
1131 	 * a page that might already be faulted in, re-faulting it
1132 	 * continuously is a waste of time.
1133 	 *
1134 	 * XXX could this have been the cause of our random seg-fault
1135 	 *     issues?  procfs accesses user stacks.
1136 	 */
1137 	vm_page_flag_set(fs.m, PG_REFERENCED);
1138 #if 0
1139 	pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired, NULL);
1140 	mycpu->gd_cnt.v_vm_faults++;
1141 	if (curthread->td_lwp)
1142 		++curthread->td_lwp->lwp_ru.ru_minflt;
1143 #endif
1144 
1145 	/*
1146 	 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1147 	 * will contain a busied page.  So we must unlock here after having
1148 	 * messed with the pmap.
1149 	 */
1150 	unlock_things(&fs);
1151 
1152 	/*
1153 	 * Return a held page.  We are not doing any pmap manipulation so do
1154 	 * not set PG_MAPPED.  However, adjust the page flags according to
1155 	 * the fault type because the caller may not use a managed pmapping
1156 	 * (so we don't want to lose the fact that the page will be dirtied
1157 	 * if a write fault was specified).
1158 	 */
1159 	if (fault_type & VM_PROT_WRITE)
1160 		vm_page_dirty(fs.m);
1161 	vm_page_activate(fs.m);
1162 
1163 	if (curthread->td_lwp) {
1164 		if (fs.hardfault) {
1165 			curthread->td_lwp->lwp_ru.ru_majflt++;
1166 		} else {
1167 			curthread->td_lwp->lwp_ru.ru_minflt++;
1168 		}
1169 	}
1170 
1171 	/*
1172 	 * Unlock everything, and return the held or busied page.
1173 	 */
1174 	if (busyp) {
1175 		if (fault_type & (VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE)) {
1176 			vm_page_dirty(fs.m);
1177 			*busyp = 1;
1178 		} else {
1179 			*busyp = 0;
1180 			vm_page_hold(fs.m);
1181 			vm_page_wakeup(fs.m);
1182 		}
1183 	} else {
1184 		vm_page_hold(fs.m);
1185 		vm_page_wakeup(fs.m);
1186 	}
1187 	/*vm_object_deallocate(fs.first_object);*/
1188 	/*fs.first_object = NULL; */
1189 	*errorp = 0;
1190 
1191 done:
1192 	if (fs.first_object)
1193 		vm_object_drop(fs.first_object);
1194 done2:
1195 	return(fs.m);
1196 }
1197 
1198 /*
1199  * Fault in the specified (object,offset), dirty the returned page as
1200  * needed.  If the requested fault_type cannot be done NULL and an
1201  * error is returned.
1202  *
1203  * A held (but not busied) page is returned.
1204  *
1205  * The passed in object must be held as specified by the shared
1206  * argument.
1207  */
1208 vm_page_t
1209 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
1210 		     vm_prot_t fault_type, int fault_flags,
1211 		     int *sharedp, int *errorp)
1212 {
1213 	int result;
1214 	vm_pindex_t first_pindex;
1215 	struct faultstate fs;
1216 	struct vm_map_entry entry;
1217 
1218 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1219 	bzero(&entry, sizeof(entry));
1220 	entry.object.vm_object = object;
1221 	entry.maptype = VM_MAPTYPE_NORMAL;
1222 	entry.protection = entry.max_protection = fault_type;
1223 
1224 	fs.hardfault = 0;
1225 	fs.fault_flags = fault_flags;
1226 	fs.map = NULL;
1227 	fs.shared = vm_shared_fault;
1228 	fs.first_shared = *sharedp;
1229 	fs.vp = NULL;
1230 	KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
1231 
1232 	/*
1233 	 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1234 	 * VM_FAULT_DIRTY  - may require swap_pager_unswapped() later, but
1235 	 *		     we can try shared first.
1236 	 */
1237 	if (fs.first_shared && (fault_flags & VM_FAULT_UNSWAP)) {
1238 		fs.first_shared = 0;
1239 		vm_object_upgrade(object);
1240 	}
1241 
1242 	/*
1243 	 * Retry loop as needed (typically for shared->exclusive transitions)
1244 	 */
1245 RetryFault:
1246 	*sharedp = fs.first_shared;
1247 	first_pindex = OFF_TO_IDX(offset);
1248 	fs.first_object = object;
1249 	fs.entry = &entry;
1250 	fs.first_prot = fault_type;
1251 	fs.wired = 0;
1252 	/*fs.map_generation = 0; unused */
1253 
1254 	/*
1255 	 * Make a reference to this object to prevent its disposal while we
1256 	 * are messing with it.  Once we have the reference, the map is free
1257 	 * to be diddled.  Since objects reference their shadows (and copies),
1258 	 * they will stay around as well.
1259 	 *
1260 	 * The reference should also prevent an unexpected collapse of the
1261 	 * parent that might move pages from the current object into the
1262 	 * parent unexpectedly, resulting in corruption.
1263 	 *
1264 	 * Bump the paging-in-progress count to prevent size changes (e.g.
1265 	 * truncation operations) during I/O.  This must be done after
1266 	 * obtaining the vnode lock in order to avoid possible deadlocks.
1267 	 */
1268 	if (fs.vp == NULL)
1269 		fs.vp = vnode_pager_lock(fs.first_object);
1270 
1271 	fs.lookup_still_valid = TRUE;
1272 	fs.first_m = NULL;
1273 	fs.object = fs.first_object;	/* so unlock_and_deallocate works */
1274 
1275 #if 0
1276 	/* XXX future - ability to operate on VM object using vpagetable */
1277 	if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1278 		result = vm_fault_vpagetable(&fs, &first_pindex,
1279 					     fs.entry->aux.master_pde,
1280 					     fault_type, 0);
1281 		if (result == KERN_TRY_AGAIN) {
1282 			if (fs.first_shared == 0 && *sharedp)
1283 				vm_object_upgrade(object);
1284 			goto RetryFault;
1285 		}
1286 		if (result != KERN_SUCCESS) {
1287 			*errorp = result;
1288 			return (NULL);
1289 		}
1290 	}
1291 #endif
1292 
1293 	/*
1294 	 * Now we have the actual (object, pindex), fault in the page.  If
1295 	 * vm_fault_object() fails it will unlock and deallocate the FS
1296 	 * data.   If it succeeds everything remains locked and fs->object
1297 	 * will have an additinal PIP count if it is not equal to
1298 	 * fs->first_object
1299 	 *
1300 	 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1301 	 * We may have to upgrade its lock to handle the requested fault.
1302 	 */
1303 	result = vm_fault_object(&fs, first_pindex, fault_type, 0);
1304 
1305 	if (result == KERN_TRY_AGAIN) {
1306 		if (fs.first_shared == 0 && *sharedp)
1307 			vm_object_upgrade(object);
1308 		goto RetryFault;
1309 	}
1310 	if (result != KERN_SUCCESS) {
1311 		*errorp = result;
1312 		return(NULL);
1313 	}
1314 
1315 	if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1316 		*errorp = KERN_PROTECTION_FAILURE;
1317 		unlock_and_deallocate(&fs);
1318 		return(NULL);
1319 	}
1320 
1321 	/*
1322 	 * On success vm_fault_object() does not unlock or deallocate, so we
1323 	 * do it here.  Note that the returned fs.m will be busied.
1324 	 */
1325 	unlock_things(&fs);
1326 
1327 	/*
1328 	 * Return a held page.  We are not doing any pmap manipulation so do
1329 	 * not set PG_MAPPED.  However, adjust the page flags according to
1330 	 * the fault type because the caller may not use a managed pmapping
1331 	 * (so we don't want to lose the fact that the page will be dirtied
1332 	 * if a write fault was specified).
1333 	 */
1334 	vm_page_hold(fs.m);
1335 	vm_page_activate(fs.m);
1336 	if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
1337 		vm_page_dirty(fs.m);
1338 	if (fault_flags & VM_FAULT_UNSWAP)
1339 		swap_pager_unswapped(fs.m);
1340 
1341 	/*
1342 	 * Indicate that the page was accessed.
1343 	 */
1344 	vm_page_flag_set(fs.m, PG_REFERENCED);
1345 
1346 	if (curthread->td_lwp) {
1347 		if (fs.hardfault) {
1348 			curthread->td_lwp->lwp_ru.ru_majflt++;
1349 		} else {
1350 			curthread->td_lwp->lwp_ru.ru_minflt++;
1351 		}
1352 	}
1353 
1354 	/*
1355 	 * Unlock everything, and return the held page.
1356 	 */
1357 	vm_page_wakeup(fs.m);
1358 	/*vm_object_deallocate(fs.first_object);*/
1359 	/*fs.first_object = NULL; */
1360 
1361 	*errorp = 0;
1362 	return(fs.m);
1363 }
1364 
1365 /*
1366  * Translate the virtual page number (first_pindex) that is relative
1367  * to the address space into a logical page number that is relative to the
1368  * backing object.  Use the virtual page table pointed to by (vpte).
1369  *
1370  * Possibly downgrade the protection based on the vpte bits.
1371  *
1372  * This implements an N-level page table.  Any level can terminate the
1373  * scan by setting VPTE_PS.   A linear mapping is accomplished by setting
1374  * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1375  */
1376 static
1377 int
1378 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1379 		    vpte_t vpte, int fault_type, int allow_nofault)
1380 {
1381 	struct lwbuf *lwb;
1382 	struct lwbuf lwb_cache;
1383 	int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1384 	int result;
1385 	vpte_t *ptep;
1386 
1387 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1388 	for (;;) {
1389 		/*
1390 		 * We cannot proceed if the vpte is not valid, not readable
1391 		 * for a read fault, not writable for a write fault, or
1392 		 * not executable for an instruction execution fault.
1393 		 */
1394 		if ((vpte & VPTE_V) == 0) {
1395 			unlock_and_deallocate(fs);
1396 			return (KERN_FAILURE);
1397 		}
1398 		if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) {
1399 			unlock_and_deallocate(fs);
1400 			return (KERN_FAILURE);
1401 		}
1402 		if ((fault_type & VM_PROT_EXECUTE) && (vpte & VPTE_NX)) {
1403 			unlock_and_deallocate(fs);
1404 			return (KERN_FAILURE);
1405 		}
1406 		if ((vpte & VPTE_PS) || vshift == 0)
1407 			break;
1408 
1409 		/*
1410 		 * Get the page table page.  Nominally we only read the page
1411 		 * table, but since we are actively setting VPTE_M and VPTE_A,
1412 		 * tell vm_fault_object() that we are writing it.
1413 		 *
1414 		 * There is currently no real need to optimize this.
1415 		 */
1416 		result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1417 					 VM_PROT_READ|VM_PROT_WRITE,
1418 					 allow_nofault);
1419 		if (result != KERN_SUCCESS)
1420 			return (result);
1421 
1422 		/*
1423 		 * Process the returned fs.m and look up the page table
1424 		 * entry in the page table page.
1425 		 */
1426 		vshift -= VPTE_PAGE_BITS;
1427 		lwb = lwbuf_alloc(fs->m, &lwb_cache);
1428 		ptep = ((vpte_t *)lwbuf_kva(lwb) +
1429 		        ((*pindex >> vshift) & VPTE_PAGE_MASK));
1430 		vm_page_activate(fs->m);
1431 
1432 		/*
1433 		 * Page table write-back - entire operation including
1434 		 * validation of the pte must be atomic to avoid races
1435 		 * against the vkernel changing the pte.
1436 		 *
1437 		 * If the vpte is valid for the* requested operation, do
1438 		 * a write-back to the page table.
1439 		 *
1440 		 * XXX VPTE_M is not set properly for page directory pages.
1441 		 * It doesn't get set in the page directory if the page table
1442 		 * is modified during a read access.
1443 		 */
1444 		for (;;) {
1445 			vpte_t nvpte;
1446 
1447 			/*
1448 			 * Reload for the cmpset, but make sure the pte is
1449 			 * still valid.
1450 			 */
1451 			vpte = *ptep;
1452 			cpu_ccfence();
1453 			nvpte = vpte;
1454 
1455 			if ((vpte & VPTE_V) == 0)
1456 				break;
1457 
1458 			if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW))
1459 				nvpte |= VPTE_M | VPTE_A;
1460 			if (fault_type & (VM_PROT_READ | VM_PROT_EXECUTE))
1461 				nvpte |= VPTE_A;
1462 			if (vpte == nvpte)
1463 				break;
1464 			if (atomic_cmpset_long(ptep, vpte, nvpte)) {
1465 				vm_page_dirty(fs->m);
1466 				break;
1467 			}
1468 		}
1469 		lwbuf_free(lwb);
1470 		vm_page_flag_set(fs->m, PG_REFERENCED);
1471 		vm_page_wakeup(fs->m);
1472 		fs->m = NULL;
1473 		cleanup_successful_fault(fs);
1474 	}
1475 
1476 	/*
1477 	 * When the vkernel sets VPTE_RW it expects the real kernel to
1478 	 * reflect VPTE_M back when the page is modified via the mapping.
1479 	 * In order to accomplish this the real kernel must map the page
1480 	 * read-only for read faults and use write faults to reflect VPTE_M
1481 	 * back.
1482 	 *
1483 	 * Once VPTE_M has been set, the real kernel's pte allows writing.
1484 	 * If the vkernel clears VPTE_M the vkernel must be sure to
1485 	 * MADV_INVAL the real kernel's mappings to force the real kernel
1486 	 * to re-fault on the next write so oit can set VPTE_M again.
1487 	 */
1488 	if ((fault_type & VM_PROT_WRITE) == 0 &&
1489 	    (vpte & (VPTE_RW | VPTE_M)) != (VPTE_RW | VPTE_M)) {
1490 		fs->first_prot &= ~VM_PROT_WRITE;
1491 	}
1492 
1493 	/*
1494 	 * Disable EXECUTE perms if NX bit is set.
1495 	 */
1496 	if (vpte & VPTE_NX)
1497 		fs->first_prot &= ~VM_PROT_EXECUTE;
1498 
1499 	/*
1500 	 * Combine remaining address bits with the vpte.
1501 	 */
1502 	*pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1503 		  (*pindex & ((1L << vshift) - 1));
1504 	return (KERN_SUCCESS);
1505 }
1506 
1507 
1508 /*
1509  * This is the core of the vm_fault code.
1510  *
1511  * Do all operations required to fault-in (fs.first_object, pindex).  Run
1512  * through the shadow chain as necessary and do required COW or virtual
1513  * copy operations.  The caller has already fully resolved the vm_map_entry
1514  * and, if appropriate, has created a copy-on-write layer.  All we need to
1515  * do is iterate the object chain.
1516  *
1517  * On failure (fs) is unlocked and deallocated and the caller may return or
1518  * retry depending on the failure code.  On success (fs) is NOT unlocked or
1519  * deallocated, fs.m will contained a resolved, busied page, and fs.object
1520  * will have an additional PIP count if it is not equal to fs.first_object.
1521  *
1522  * If locks based on fs->first_shared or fs->shared are insufficient,
1523  * clear the appropriate field(s) and return RETRY.  COWs require that
1524  * first_shared be 0, while page allocations (or frees) require that
1525  * shared be 0.  Renames require that both be 0.
1526  *
1527  * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1528  *	 we will have to retry with it exclusive if the vm_page is
1529  *	 PG_SWAPPED.
1530  *
1531  * fs->first_object must be held on call.
1532  */
1533 static
1534 int
1535 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex,
1536 		vm_prot_t fault_type, int allow_nofault)
1537 {
1538 	vm_object_t next_object;
1539 	vm_pindex_t pindex;
1540 	int error;
1541 
1542 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1543 	fs->prot = fs->first_prot;
1544 	fs->object = fs->first_object;
1545 	pindex = first_pindex;
1546 
1547 	vm_object_chain_acquire(fs->first_object, fs->shared);
1548 	vm_object_pip_add(fs->first_object, 1);
1549 
1550 	/*
1551 	 * If a read fault occurs we try to upgrade the page protection
1552 	 * and make it also writable if possible.  There are three cases
1553 	 * where we cannot make the page mapping writable:
1554 	 *
1555 	 * (1) The mapping is read-only or the VM object is read-only,
1556 	 *     fs->prot above will simply not have VM_PROT_WRITE set.
1557 	 *
1558 	 * (2) If the mapping is a virtual page table fs->first_prot will
1559 	 *     have already been properly adjusted by vm_fault_vpagetable().
1560 	 *     to detect writes so we can set VPTE_M in the virtual page
1561 	 *     table.  Used by vkernels.
1562 	 *
1563 	 * (3) If the VM page is read-only or copy-on-write, upgrading would
1564 	 *     just result in an unnecessary COW fault.
1565 	 *
1566 	 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1567 	 *     here.
1568 	 */
1569 #if 0
1570 	/* see vpagetable code */
1571 	if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1572 		if ((fault_type & VM_PROT_WRITE) == 0)
1573 			fs->prot &= ~VM_PROT_WRITE;
1574 	}
1575 #endif
1576 
1577 	if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
1578 	    pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
1579 		if ((fault_type & VM_PROT_WRITE) == 0)
1580 			fs->prot &= ~VM_PROT_WRITE;
1581 	}
1582 
1583 	/* vm_object_hold(fs->object); implied b/c object == first_object */
1584 
1585 	for (;;) {
1586 		/*
1587 		 * The entire backing chain from first_object to object
1588 		 * inclusive is chainlocked.
1589 		 *
1590 		 * If the object is dead, we stop here
1591 		 */
1592 		if (fs->object->flags & OBJ_DEAD) {
1593 			vm_object_pip_wakeup(fs->first_object);
1594 			vm_object_chain_release_all(fs->first_object,
1595 						    fs->object);
1596 			if (fs->object != fs->first_object)
1597 				vm_object_drop(fs->object);
1598 			unlock_and_deallocate(fs);
1599 			return (KERN_PROTECTION_FAILURE);
1600 		}
1601 
1602 		/*
1603 		 * See if the page is resident.  Wait/Retry if the page is
1604 		 * busy (lots of stuff may have changed so we can't continue
1605 		 * in that case).
1606 		 *
1607 		 * We can theoretically allow the soft-busy case on a read
1608 		 * fault if the page is marked valid, but since such
1609 		 * pages are typically already pmap'd, putting that
1610 		 * special case in might be more effort then it is
1611 		 * worth.  We cannot under any circumstances mess
1612 		 * around with a vm_page_t->busy page except, perhaps,
1613 		 * to pmap it.
1614 		 */
1615 		fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1616 						TRUE, &error);
1617 		if (error) {
1618 			vm_object_pip_wakeup(fs->first_object);
1619 			vm_object_chain_release_all(fs->first_object,
1620 						    fs->object);
1621 			if (fs->object != fs->first_object)
1622 				vm_object_drop(fs->object);
1623 			unlock_things(fs);
1624 			vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1625 			mycpu->gd_cnt.v_intrans++;
1626 			/*vm_object_deallocate(fs->first_object);*/
1627 			/*fs->first_object = NULL;*/
1628 			fs->m = NULL;
1629 			return (KERN_TRY_AGAIN);
1630 		}
1631 		if (fs->m) {
1632 			/*
1633 			 * The page is busied for us.
1634 			 *
1635 			 * If reactivating a page from PQ_CACHE we may have
1636 			 * to rate-limit.
1637 			 */
1638 			int queue = fs->m->queue;
1639 			vm_page_unqueue_nowakeup(fs->m);
1640 
1641 			if ((queue - fs->m->pc) == PQ_CACHE &&
1642 			    vm_page_count_severe()) {
1643 				vm_page_activate(fs->m);
1644 				vm_page_wakeup(fs->m);
1645 				fs->m = NULL;
1646 				vm_object_pip_wakeup(fs->first_object);
1647 				vm_object_chain_release_all(fs->first_object,
1648 							    fs->object);
1649 				if (fs->object != fs->first_object)
1650 					vm_object_drop(fs->object);
1651 				unlock_and_deallocate(fs);
1652 				if (allow_nofault == 0 ||
1653 				    (curthread->td_flags & TDF_NOFAULT) == 0) {
1654 					thread_t td;
1655 
1656 					vm_wait_pfault();
1657 					td = curthread;
1658 					if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1659 						return (KERN_PROTECTION_FAILURE);
1660 				}
1661 				return (KERN_TRY_AGAIN);
1662 			}
1663 
1664 			/*
1665 			 * If it still isn't completely valid (readable),
1666 			 * or if a read-ahead-mark is set on the VM page,
1667 			 * jump to readrest, else we found the page and
1668 			 * can return.
1669 			 *
1670 			 * We can release the spl once we have marked the
1671 			 * page busy.
1672 			 */
1673 			if (fs->m->object != &kernel_object) {
1674 				if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1675 				    VM_PAGE_BITS_ALL) {
1676 					goto readrest;
1677 				}
1678 				if (fs->m->flags & PG_RAM) {
1679 					if (debug_cluster)
1680 						kprintf("R");
1681 					vm_page_flag_clear(fs->m, PG_RAM);
1682 					goto readrest;
1683 				}
1684 			}
1685 			break; /* break to PAGE HAS BEEN FOUND */
1686 		}
1687 
1688 		/*
1689 		 * Page is not resident, If this is the search termination
1690 		 * or the pager might contain the page, allocate a new page.
1691 		 */
1692 		if (TRYPAGER(fs) || fs->object == fs->first_object) {
1693 			/*
1694 			 * Allocating, must be exclusive.
1695 			 */
1696 			if (fs->object == fs->first_object &&
1697 			    fs->first_shared) {
1698 				fs->first_shared = 0;
1699 				vm_object_pip_wakeup(fs->first_object);
1700 				vm_object_chain_release_all(fs->first_object,
1701 							    fs->object);
1702 				if (fs->object != fs->first_object)
1703 					vm_object_drop(fs->object);
1704 				unlock_and_deallocate(fs);
1705 				return (KERN_TRY_AGAIN);
1706 			}
1707 			if (fs->object != fs->first_object &&
1708 			    fs->shared) {
1709 				fs->first_shared = 0;
1710 				fs->shared = 0;
1711 				vm_object_pip_wakeup(fs->first_object);
1712 				vm_object_chain_release_all(fs->first_object,
1713 							    fs->object);
1714 				if (fs->object != fs->first_object)
1715 					vm_object_drop(fs->object);
1716 				unlock_and_deallocate(fs);
1717 				return (KERN_TRY_AGAIN);
1718 			}
1719 
1720 			/*
1721 			 * If the page is beyond the object size we fail
1722 			 */
1723 			if (pindex >= fs->object->size) {
1724 				vm_object_pip_wakeup(fs->first_object);
1725 				vm_object_chain_release_all(fs->first_object,
1726 							    fs->object);
1727 				if (fs->object != fs->first_object)
1728 					vm_object_drop(fs->object);
1729 				unlock_and_deallocate(fs);
1730 				return (KERN_PROTECTION_FAILURE);
1731 			}
1732 
1733 			/*
1734 			 * Allocate a new page for this object/offset pair.
1735 			 *
1736 			 * It is possible for the allocation to race, so
1737 			 * handle the case.
1738 			 */
1739 			fs->m = NULL;
1740 			if (!vm_page_count_severe()) {
1741 				fs->m = vm_page_alloc(fs->object, pindex,
1742 				    ((fs->vp || fs->object->backing_object) ?
1743 					VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1744 					VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1745 					VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1746 			}
1747 			if (fs->m == NULL) {
1748 				vm_object_pip_wakeup(fs->first_object);
1749 				vm_object_chain_release_all(fs->first_object,
1750 							    fs->object);
1751 				if (fs->object != fs->first_object)
1752 					vm_object_drop(fs->object);
1753 				unlock_and_deallocate(fs);
1754 				if (allow_nofault == 0 ||
1755 				    (curthread->td_flags & TDF_NOFAULT) == 0) {
1756 					thread_t td;
1757 
1758 					vm_wait_pfault();
1759 					td = curthread;
1760 					if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1761 						return (KERN_PROTECTION_FAILURE);
1762 				}
1763 				return (KERN_TRY_AGAIN);
1764 			}
1765 
1766 			/*
1767 			 * Fall through to readrest.  We have a new page which
1768 			 * will have to be paged (since m->valid will be 0).
1769 			 */
1770 		}
1771 
1772 readrest:
1773 		/*
1774 		 * We have found an invalid or partially valid page, a
1775 		 * page with a read-ahead mark which might be partially or
1776 		 * fully valid (and maybe dirty too), or we have allocated
1777 		 * a new page.
1778 		 *
1779 		 * Attempt to fault-in the page if there is a chance that the
1780 		 * pager has it, and potentially fault in additional pages
1781 		 * at the same time.
1782 		 *
1783 		 * If TRYPAGER is true then fs.m will be non-NULL and busied
1784 		 * for us.
1785 		 */
1786 		if (TRYPAGER(fs)) {
1787 			int rv;
1788 			int seqaccess;
1789 			u_char behavior = vm_map_entry_behavior(fs->entry);
1790 
1791 			if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1792 				seqaccess = 0;
1793 			else
1794 				seqaccess = -1;
1795 
1796 			/*
1797 			 * Doing I/O may synchronously insert additional
1798 			 * pages so we can't be shared at this point either.
1799 			 *
1800 			 * NOTE: We can't free fs->m here in the allocated
1801 			 *	 case (fs->object != fs->first_object) as
1802 			 *	 this would require an exclusively locked
1803 			 *	 VM object.
1804 			 */
1805 			if (fs->object == fs->first_object &&
1806 			    fs->first_shared) {
1807 				vm_page_deactivate(fs->m);
1808 				vm_page_wakeup(fs->m);
1809 				fs->m = NULL;
1810 				fs->first_shared = 0;
1811 				vm_object_pip_wakeup(fs->first_object);
1812 				vm_object_chain_release_all(fs->first_object,
1813 							    fs->object);
1814 				if (fs->object != fs->first_object)
1815 					vm_object_drop(fs->object);
1816 				unlock_and_deallocate(fs);
1817 				return (KERN_TRY_AGAIN);
1818 			}
1819 			if (fs->object != fs->first_object &&
1820 			    fs->shared) {
1821 				vm_page_deactivate(fs->m);
1822 				vm_page_wakeup(fs->m);
1823 				fs->m = NULL;
1824 				fs->first_shared = 0;
1825 				fs->shared = 0;
1826 				vm_object_pip_wakeup(fs->first_object);
1827 				vm_object_chain_release_all(fs->first_object,
1828 							    fs->object);
1829 				if (fs->object != fs->first_object)
1830 					vm_object_drop(fs->object);
1831 				unlock_and_deallocate(fs);
1832 				return (KERN_TRY_AGAIN);
1833 			}
1834 
1835 			/*
1836 			 * Avoid deadlocking against the map when doing I/O.
1837 			 * fs.object and the page is BUSY'd.
1838 			 *
1839 			 * NOTE: Once unlocked, fs->entry can become stale
1840 			 *	 so this will NULL it out.
1841 			 *
1842 			 * NOTE: fs->entry is invalid until we relock the
1843 			 *	 map and verify that the timestamp has not
1844 			 *	 changed.
1845 			 */
1846 			unlock_map(fs);
1847 
1848 			/*
1849 			 * Acquire the page data.  We still hold a ref on
1850 			 * fs.object and the page has been BUSY's.
1851 			 *
1852 			 * The pager may replace the page (for example, in
1853 			 * order to enter a fictitious page into the
1854 			 * object).  If it does so it is responsible for
1855 			 * cleaning up the passed page and properly setting
1856 			 * the new page BUSY.
1857 			 *
1858 			 * If we got here through a PG_RAM read-ahead
1859 			 * mark the page may be partially dirty and thus
1860 			 * not freeable.  Don't bother checking to see
1861 			 * if the pager has the page because we can't free
1862 			 * it anyway.  We have to depend on the get_page
1863 			 * operation filling in any gaps whether there is
1864 			 * backing store or not.
1865 			 */
1866 			rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1867 
1868 			if (rv == VM_PAGER_OK) {
1869 				/*
1870 				 * Relookup in case pager changed page. Pager
1871 				 * is responsible for disposition of old page
1872 				 * if moved.
1873 				 *
1874 				 * XXX other code segments do relookups too.
1875 				 * It's a bad abstraction that needs to be
1876 				 * fixed/removed.
1877 				 */
1878 				fs->m = vm_page_lookup(fs->object, pindex);
1879 				if (fs->m == NULL) {
1880 					vm_object_pip_wakeup(fs->first_object);
1881 					vm_object_chain_release_all(
1882 						fs->first_object, fs->object);
1883 					if (fs->object != fs->first_object)
1884 						vm_object_drop(fs->object);
1885 					unlock_and_deallocate(fs);
1886 					return (KERN_TRY_AGAIN);
1887 				}
1888 				++fs->hardfault;
1889 				break; /* break to PAGE HAS BEEN FOUND */
1890 			}
1891 
1892 			/*
1893 			 * Remove the bogus page (which does not exist at this
1894 			 * object/offset); before doing so, we must get back
1895 			 * our object lock to preserve our invariant.
1896 			 *
1897 			 * Also wake up any other process that may want to bring
1898 			 * in this page.
1899 			 *
1900 			 * If this is the top-level object, we must leave the
1901 			 * busy page to prevent another process from rushing
1902 			 * past us, and inserting the page in that object at
1903 			 * the same time that we are.
1904 			 */
1905 			if (rv == VM_PAGER_ERROR) {
1906 				if (curproc) {
1907 					kprintf("vm_fault: pager read error, "
1908 						"pid %d (%s)\n",
1909 						curproc->p_pid,
1910 						curproc->p_comm);
1911 				} else {
1912 					kprintf("vm_fault: pager read error, "
1913 						"thread %p (%s)\n",
1914 						curthread,
1915 						curproc->p_comm);
1916 				}
1917 			}
1918 
1919 			/*
1920 			 * Data outside the range of the pager or an I/O error
1921 			 *
1922 			 * The page may have been wired during the pagein,
1923 			 * e.g. by the buffer cache, and cannot simply be
1924 			 * freed.  Call vnode_pager_freepage() to deal with it.
1925 			 *
1926 			 * Also note that we cannot free the page if we are
1927 			 * holding the related object shared. XXX not sure
1928 			 * what to do in that case.
1929 			 */
1930 			if (fs->object != fs->first_object) {
1931 				/*
1932 				 * Scrap the page.  Check to see if the
1933 				 * vm_pager_get_page() call has already
1934 				 * dealt with it.
1935 				 */
1936 				if (fs->m) {
1937 					vnode_pager_freepage(fs->m);
1938 					fs->m = NULL;
1939 				}
1940 
1941 				/*
1942 				 * XXX - we cannot just fall out at this
1943 				 * point, m has been freed and is invalid!
1944 				 */
1945 			}
1946 			/*
1947 			 * XXX - the check for kernel_map is a kludge to work
1948 			 * around having the machine panic on a kernel space
1949 			 * fault w/ I/O error.
1950 			 */
1951 			if (((fs->map != &kernel_map) &&
1952 			    (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1953 				if (fs->m) {
1954 					if (fs->first_shared) {
1955 						vm_page_deactivate(fs->m);
1956 						vm_page_wakeup(fs->m);
1957 					} else {
1958 						vnode_pager_freepage(fs->m);
1959 					}
1960 					fs->m = NULL;
1961 				}
1962 				vm_object_pip_wakeup(fs->first_object);
1963 				vm_object_chain_release_all(fs->first_object,
1964 							    fs->object);
1965 				if (fs->object != fs->first_object)
1966 					vm_object_drop(fs->object);
1967 				unlock_and_deallocate(fs);
1968 				if (rv == VM_PAGER_ERROR)
1969 					return (KERN_FAILURE);
1970 				else
1971 					return (KERN_PROTECTION_FAILURE);
1972 				/* NOT REACHED */
1973 			}
1974 		}
1975 
1976 		/*
1977 		 * We get here if the object has a default pager (or unwiring)
1978 		 * or the pager doesn't have the page.
1979 		 *
1980 		 * fs->first_m will be used for the COW unless we find a
1981 		 * deeper page to be mapped read-only, in which case the
1982 		 * unlock*(fs) will free first_m.
1983 		 */
1984 		if (fs->object == fs->first_object)
1985 			fs->first_m = fs->m;
1986 
1987 		/*
1988 		 * Move on to the next object.  The chain lock should prevent
1989 		 * the backing_object from getting ripped out from under us.
1990 		 *
1991 		 * The object lock for the next object is governed by
1992 		 * fs->shared.
1993 		 */
1994 		if ((next_object = fs->object->backing_object) != NULL) {
1995 			if (fs->shared)
1996 				vm_object_hold_shared(next_object);
1997 			else
1998 				vm_object_hold(next_object);
1999 			vm_object_chain_acquire(next_object, fs->shared);
2000 			KKASSERT(next_object == fs->object->backing_object);
2001 			pindex += OFF_TO_IDX(fs->object->backing_object_offset);
2002 		}
2003 
2004 		if (next_object == NULL) {
2005 			/*
2006 			 * If there's no object left, fill the page in the top
2007 			 * object with zeros.
2008 			 */
2009 			if (fs->object != fs->first_object) {
2010 #if 0
2011 				if (fs->first_object->backing_object !=
2012 				    fs->object) {
2013 					vm_object_hold(fs->first_object->backing_object);
2014 				}
2015 #endif
2016 				vm_object_chain_release_all(
2017 					fs->first_object->backing_object,
2018 					fs->object);
2019 #if 0
2020 				if (fs->first_object->backing_object !=
2021 				    fs->object) {
2022 					vm_object_drop(fs->first_object->backing_object);
2023 				}
2024 #endif
2025 				vm_object_pip_wakeup(fs->object);
2026 				vm_object_drop(fs->object);
2027 				fs->object = fs->first_object;
2028 				pindex = first_pindex;
2029 				fs->m = fs->first_m;
2030 			}
2031 			fs->first_m = NULL;
2032 
2033 			/*
2034 			 * Zero the page and mark it valid.
2035 			 */
2036 			vm_page_zero_fill(fs->m);
2037 			mycpu->gd_cnt.v_zfod++;
2038 			fs->m->valid = VM_PAGE_BITS_ALL;
2039 			break;	/* break to PAGE HAS BEEN FOUND */
2040 		}
2041 		if (fs->object != fs->first_object) {
2042 			vm_object_pip_wakeup(fs->object);
2043 			vm_object_lock_swap();
2044 			vm_object_drop(fs->object);
2045 		}
2046 		KASSERT(fs->object != next_object,
2047 			("object loop %p", next_object));
2048 		fs->object = next_object;
2049 		vm_object_pip_add(fs->object, 1);
2050 	}
2051 
2052 	/*
2053 	 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
2054 	 * is held.]
2055 	 *
2056 	 * object still held.
2057 	 * vm_map may not be locked (determined by fs->lookup_still_valid)
2058 	 *
2059 	 * local shared variable may be different from fs->shared.
2060 	 *
2061 	 * If the page is being written, but isn't already owned by the
2062 	 * top-level object, we have to copy it into a new page owned by the
2063 	 * top-level object.
2064 	 */
2065 	KASSERT((fs->m->busy_count & PBUSY_LOCKED) != 0,
2066 		("vm_fault: not busy after main loop"));
2067 
2068 	if (fs->object != fs->first_object) {
2069 		/*
2070 		 * We only really need to copy if we want to write it.
2071 		 */
2072 		if (fault_type & VM_PROT_WRITE) {
2073 			/*
2074 			 * This allows pages to be virtually copied from a
2075 			 * backing_object into the first_object, where the
2076 			 * backing object has no other refs to it, and cannot
2077 			 * gain any more refs.  Instead of a bcopy, we just
2078 			 * move the page from the backing object to the
2079 			 * first object.  Note that we must mark the page
2080 			 * dirty in the first object so that it will go out
2081 			 * to swap when needed.
2082 			 */
2083 			if (virtual_copy_ok(fs)) {
2084 				/*
2085 				 * (first_m) and (m) are both busied.  We have
2086 				 * move (m) into (first_m)'s object/pindex
2087 				 * in an atomic fashion, then free (first_m).
2088 				 *
2089 				 * first_object is held so second remove
2090 				 * followed by the rename should wind
2091 				 * up being atomic.  vm_page_free() might
2092 				 * block so we don't do it until after the
2093 				 * rename.
2094 				 */
2095 				vm_page_protect(fs->first_m, VM_PROT_NONE);
2096 				vm_page_remove(fs->first_m);
2097 				vm_page_rename(fs->m, fs->first_object,
2098 					       first_pindex);
2099 				vm_page_free(fs->first_m);
2100 				fs->first_m = fs->m;
2101 				fs->m = NULL;
2102 				mycpu->gd_cnt.v_cow_optim++;
2103 			} else {
2104 				/*
2105 				 * Oh, well, lets copy it.
2106 				 *
2107 				 * Why are we unmapping the original page
2108 				 * here?  Well, in short, not all accessors
2109 				 * of user memory go through the pmap.  The
2110 				 * procfs code doesn't have access user memory
2111 				 * via a local pmap, so vm_fault_page*()
2112 				 * can't call pmap_enter().  And the umtx*()
2113 				 * code may modify the COW'd page via a DMAP
2114 				 * or kernel mapping and not via the pmap,
2115 				 * leaving the original page still mapped
2116 				 * read-only into the pmap.
2117 				 *
2118 				 * So we have to remove the page from at
2119 				 * least the current pmap if it is in it.
2120 				 *
2121 				 * We used to just remove it from all pmaps
2122 				 * but that creates inefficiencies on SMP,
2123 				 * particularly for COW program & library
2124 				 * mappings that are concurrently exec'd.
2125 				 * Only remove the page from the current
2126 				 * pmap.
2127 				 */
2128 				KKASSERT(fs->first_shared == 0);
2129 				vm_page_copy(fs->m, fs->first_m);
2130 				/*vm_page_protect(fs->m, VM_PROT_NONE);*/
2131 				pmap_remove_specific(
2132 				    &curthread->td_lwp->lwp_vmspace->vm_pmap,
2133 				    fs->m);
2134 			}
2135 
2136 			/*
2137 			 * We no longer need the old page or object.
2138 			 */
2139 			if (fs->m)
2140 				release_page(fs);
2141 
2142 			/*
2143 			 * We intend to revert to first_object, undo the
2144 			 * chain lock through to that.
2145 			 */
2146 #if 0
2147 			if (fs->first_object->backing_object != fs->object)
2148 				vm_object_hold(fs->first_object->backing_object);
2149 #endif
2150 			vm_object_chain_release_all(
2151 					fs->first_object->backing_object,
2152 					fs->object);
2153 #if 0
2154 			if (fs->first_object->backing_object != fs->object)
2155 				vm_object_drop(fs->first_object->backing_object);
2156 #endif
2157 
2158 			/*
2159 			 * fs->object != fs->first_object due to above
2160 			 * conditional
2161 			 */
2162 			vm_object_pip_wakeup(fs->object);
2163 			vm_object_drop(fs->object);
2164 
2165 			/*
2166 			 * Only use the new page below...
2167 			 */
2168 			mycpu->gd_cnt.v_cow_faults++;
2169 			fs->m = fs->first_m;
2170 			fs->object = fs->first_object;
2171 			pindex = first_pindex;
2172 		} else {
2173 			/*
2174 			 * If it wasn't a write fault avoid having to copy
2175 			 * the page by mapping it read-only.
2176 			 */
2177 			fs->prot &= ~VM_PROT_WRITE;
2178 		}
2179 	}
2180 
2181 	/*
2182 	 * Relock the map if necessary, then check the generation count.
2183 	 * relock_map() will update fs->timestamp to account for the
2184 	 * relocking if necessary.
2185 	 *
2186 	 * If the count has changed after relocking then all sorts of
2187 	 * crap may have happened and we have to retry.
2188 	 *
2189 	 * NOTE: The relock_map() can fail due to a deadlock against
2190 	 *	 the vm_page we are holding BUSY.
2191 	 */
2192 	if (fs->lookup_still_valid == FALSE && fs->map) {
2193 		if (relock_map(fs) ||
2194 		    fs->map->timestamp != fs->map_generation) {
2195 			release_page(fs);
2196 			vm_object_pip_wakeup(fs->first_object);
2197 			vm_object_chain_release_all(fs->first_object,
2198 						    fs->object);
2199 			if (fs->object != fs->first_object)
2200 				vm_object_drop(fs->object);
2201 			unlock_and_deallocate(fs);
2202 			return (KERN_TRY_AGAIN);
2203 		}
2204 	}
2205 
2206 	/*
2207 	 * If the fault is a write, we know that this page is being
2208 	 * written NOW so dirty it explicitly to save on pmap_is_modified()
2209 	 * calls later.
2210 	 *
2211 	 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2212 	 * if the page is already dirty to prevent data written with
2213 	 * the expectation of being synced from not being synced.
2214 	 * Likewise if this entry does not request NOSYNC then make
2215 	 * sure the page isn't marked NOSYNC.  Applications sharing
2216 	 * data should use the same flags to avoid ping ponging.
2217 	 *
2218 	 * Also tell the backing pager, if any, that it should remove
2219 	 * any swap backing since the page is now dirty.
2220 	 */
2221 	vm_page_activate(fs->m);
2222 	if (fs->prot & VM_PROT_WRITE) {
2223 		vm_object_set_writeable_dirty(fs->m->object);
2224 		vm_set_nosync(fs->m, fs->entry);
2225 		if (fs->fault_flags & VM_FAULT_DIRTY) {
2226 			vm_page_dirty(fs->m);
2227 			if (fs->m->flags & PG_SWAPPED) {
2228 				/*
2229 				 * If the page is swapped out we have to call
2230 				 * swap_pager_unswapped() which requires an
2231 				 * exclusive object lock.  If we are shared,
2232 				 * we must clear the shared flag and retry.
2233 				 */
2234 				if ((fs->object == fs->first_object &&
2235 				     fs->first_shared) ||
2236 				    (fs->object != fs->first_object &&
2237 				     fs->shared)) {
2238 					vm_page_wakeup(fs->m);
2239 					fs->m = NULL;
2240 					if (fs->object == fs->first_object)
2241 						fs->first_shared = 0;
2242 					else
2243 						fs->shared = 0;
2244 					vm_object_pip_wakeup(fs->first_object);
2245 					vm_object_chain_release_all(
2246 						fs->first_object, fs->object);
2247 					if (fs->object != fs->first_object)
2248 						vm_object_drop(fs->object);
2249 					unlock_and_deallocate(fs);
2250 					return (KERN_TRY_AGAIN);
2251 				}
2252 				swap_pager_unswapped(fs->m);
2253 			}
2254 		}
2255 	}
2256 
2257 	vm_object_pip_wakeup(fs->first_object);
2258 	vm_object_chain_release_all(fs->first_object, fs->object);
2259 	if (fs->object != fs->first_object)
2260 		vm_object_drop(fs->object);
2261 
2262 	/*
2263 	 * Page had better still be busy.  We are still locked up and
2264 	 * fs->object will have another PIP reference if it is not equal
2265 	 * to fs->first_object.
2266 	 */
2267 	KASSERT(fs->m->busy_count & PBUSY_LOCKED,
2268 		("vm_fault: page %p not busy!", fs->m));
2269 
2270 	/*
2271 	 * Sanity check: page must be completely valid or it is not fit to
2272 	 * map into user space.  vm_pager_get_pages() ensures this.
2273 	 */
2274 	if (fs->m->valid != VM_PAGE_BITS_ALL) {
2275 		vm_page_zero_invalid(fs->m, TRUE);
2276 		kprintf("Warning: page %p partially invalid on fault\n", fs->m);
2277 	}
2278 
2279 	return (KERN_SUCCESS);
2280 }
2281 
2282 /*
2283  * Wire down a range of virtual addresses in a map.  The entry in question
2284  * should be marked in-transition and the map must be locked.  We must
2285  * release the map temporarily while faulting-in the page to avoid a
2286  * deadlock.  Note that the entry may be clipped while we are blocked but
2287  * will never be freed.
2288  *
2289  * No requirements.
2290  */
2291 int
2292 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2293 	      boolean_t user_wire, int kmflags)
2294 {
2295 	boolean_t fictitious;
2296 	vm_offset_t start;
2297 	vm_offset_t end;
2298 	vm_offset_t va;
2299 	pmap_t pmap;
2300 	int rv;
2301 	int wire_prot;
2302 	int fault_flags;
2303 	vm_page_t m;
2304 
2305 	if (user_wire) {
2306 		wire_prot = VM_PROT_READ;
2307 		fault_flags = VM_FAULT_USER_WIRE;
2308 	} else {
2309 		wire_prot = VM_PROT_READ | VM_PROT_WRITE;
2310 		fault_flags = VM_FAULT_CHANGE_WIRING;
2311 	}
2312 	if (kmflags & KM_NOTLBSYNC)
2313 		wire_prot |= VM_PROT_NOSYNC;
2314 
2315 	pmap = vm_map_pmap(map);
2316 	start = entry->start;
2317 	end = entry->end;
2318 
2319 	switch(entry->maptype) {
2320 	case VM_MAPTYPE_NORMAL:
2321 	case VM_MAPTYPE_VPAGETABLE:
2322 		fictitious = entry->object.vm_object &&
2323 			    ((entry->object.vm_object->type == OBJT_DEVICE) ||
2324 			     (entry->object.vm_object->type == OBJT_MGTDEVICE));
2325 		break;
2326 	case VM_MAPTYPE_UKSMAP:
2327 		fictitious = TRUE;
2328 		break;
2329 	default:
2330 		fictitious = FALSE;
2331 		break;
2332 	}
2333 
2334 	if (entry->eflags & MAP_ENTRY_KSTACK)
2335 		start += PAGE_SIZE;
2336 	map->timestamp++;
2337 	vm_map_unlock(map);
2338 
2339 	/*
2340 	 * We simulate a fault to get the page and enter it in the physical
2341 	 * map.
2342 	 */
2343 	for (va = start; va < end; va += PAGE_SIZE) {
2344 		rv = vm_fault(map, va, wire_prot, fault_flags);
2345 		if (rv) {
2346 			while (va > start) {
2347 				va -= PAGE_SIZE;
2348 				m = pmap_unwire(pmap, va);
2349 				if (m && !fictitious) {
2350 					vm_page_busy_wait(m, FALSE, "vmwrpg");
2351 					vm_page_unwire(m, 1);
2352 					vm_page_wakeup(m);
2353 				}
2354 			}
2355 			goto done;
2356 		}
2357 	}
2358 	rv = KERN_SUCCESS;
2359 done:
2360 	vm_map_lock(map);
2361 
2362 	return (rv);
2363 }
2364 
2365 /*
2366  * Unwire a range of virtual addresses in a map.  The map should be
2367  * locked.
2368  */
2369 void
2370 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2371 {
2372 	boolean_t fictitious;
2373 	vm_offset_t start;
2374 	vm_offset_t end;
2375 	vm_offset_t va;
2376 	pmap_t pmap;
2377 	vm_page_t m;
2378 
2379 	pmap = vm_map_pmap(map);
2380 	start = entry->start;
2381 	end = entry->end;
2382 	fictitious = entry->object.vm_object &&
2383 			((entry->object.vm_object->type == OBJT_DEVICE) ||
2384 			 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2385 	if (entry->eflags & MAP_ENTRY_KSTACK)
2386 		start += PAGE_SIZE;
2387 
2388 	/*
2389 	 * Since the pages are wired down, we must be able to get their
2390 	 * mappings from the physical map system.
2391 	 */
2392 	for (va = start; va < end; va += PAGE_SIZE) {
2393 		m = pmap_unwire(pmap, va);
2394 		if (m && !fictitious) {
2395 			vm_page_busy_wait(m, FALSE, "vmwrpg");
2396 			vm_page_unwire(m, 1);
2397 			vm_page_wakeup(m);
2398 		}
2399 	}
2400 }
2401 
2402 /*
2403  * Copy all of the pages from a wired-down map entry to another.
2404  *
2405  * The source and destination maps must be locked for write.
2406  * The source and destination maps token must be held
2407  * The source map entry must be wired down (or be a sharing map
2408  * entry corresponding to a main map entry that is wired down).
2409  *
2410  * No other requirements.
2411  *
2412  * XXX do segment optimization
2413  */
2414 void
2415 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
2416 		    vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
2417 {
2418 	vm_object_t dst_object;
2419 	vm_object_t src_object;
2420 	vm_ooffset_t dst_offset;
2421 	vm_ooffset_t src_offset;
2422 	vm_prot_t prot;
2423 	vm_offset_t vaddr;
2424 	vm_page_t dst_m;
2425 	vm_page_t src_m;
2426 
2427 	src_object = src_entry->object.vm_object;
2428 	src_offset = src_entry->offset;
2429 
2430 	/*
2431 	 * Create the top-level object for the destination entry. (Doesn't
2432 	 * actually shadow anything - we copy the pages directly.)
2433 	 */
2434 	vm_map_entry_allocate_object(dst_entry);
2435 	dst_object = dst_entry->object.vm_object;
2436 
2437 	prot = dst_entry->max_protection;
2438 
2439 	/*
2440 	 * Loop through all of the pages in the entry's range, copying each
2441 	 * one from the source object (it should be there) to the destination
2442 	 * object.
2443 	 */
2444 	vm_object_hold(src_object);
2445 	vm_object_hold(dst_object);
2446 
2447 	for (vaddr = dst_entry->start, dst_offset = 0;
2448 	     vaddr < dst_entry->end;
2449 	     vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
2450 
2451 		/*
2452 		 * Allocate a page in the destination object
2453 		 */
2454 		do {
2455 			dst_m = vm_page_alloc(dst_object,
2456 					      OFF_TO_IDX(dst_offset),
2457 					      VM_ALLOC_NORMAL);
2458 			if (dst_m == NULL) {
2459 				vm_wait(0);
2460 			}
2461 		} while (dst_m == NULL);
2462 
2463 		/*
2464 		 * Find the page in the source object, and copy it in.
2465 		 * (Because the source is wired down, the page will be in
2466 		 * memory.)
2467 		 */
2468 		src_m = vm_page_lookup(src_object,
2469 				       OFF_TO_IDX(dst_offset + src_offset));
2470 		if (src_m == NULL)
2471 			panic("vm_fault_copy_wired: page missing");
2472 
2473 		vm_page_copy(src_m, dst_m);
2474 
2475 		/*
2476 		 * Enter it in the pmap...
2477 		 */
2478 		pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
2479 
2480 		/*
2481 		 * Mark it no longer busy, and put it on the active list.
2482 		 */
2483 		vm_page_activate(dst_m);
2484 		vm_page_wakeup(dst_m);
2485 	}
2486 	vm_object_drop(dst_object);
2487 	vm_object_drop(src_object);
2488 }
2489 
2490 #if 0
2491 
2492 /*
2493  * This routine checks around the requested page for other pages that
2494  * might be able to be faulted in.  This routine brackets the viable
2495  * pages for the pages to be paged in.
2496  *
2497  * Inputs:
2498  *	m, rbehind, rahead
2499  *
2500  * Outputs:
2501  *  marray (array of vm_page_t), reqpage (index of requested page)
2502  *
2503  * Return value:
2504  *  number of pages in marray
2505  */
2506 static int
2507 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2508 			  vm_page_t *marray, int *reqpage)
2509 {
2510 	int i,j;
2511 	vm_object_t object;
2512 	vm_pindex_t pindex, startpindex, endpindex, tpindex;
2513 	vm_page_t rtm;
2514 	int cbehind, cahead;
2515 
2516 	object = m->object;
2517 	pindex = m->pindex;
2518 
2519 	/*
2520 	 * we don't fault-ahead for device pager
2521 	 */
2522 	if ((object->type == OBJT_DEVICE) ||
2523 	    (object->type == OBJT_MGTDEVICE)) {
2524 		*reqpage = 0;
2525 		marray[0] = m;
2526 		return 1;
2527 	}
2528 
2529 	/*
2530 	 * if the requested page is not available, then give up now
2531 	 */
2532 	if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2533 		*reqpage = 0;	/* not used by caller, fix compiler warn */
2534 		return 0;
2535 	}
2536 
2537 	if ((cbehind == 0) && (cahead == 0)) {
2538 		*reqpage = 0;
2539 		marray[0] = m;
2540 		return 1;
2541 	}
2542 
2543 	if (rahead > cahead) {
2544 		rahead = cahead;
2545 	}
2546 
2547 	if (rbehind > cbehind) {
2548 		rbehind = cbehind;
2549 	}
2550 
2551 	/*
2552 	 * Do not do any readahead if we have insufficient free memory.
2553 	 *
2554 	 * XXX code was broken disabled before and has instability
2555 	 * with this conditonal fixed, so shortcut for now.
2556 	 */
2557 	if (burst_fault == 0 || vm_page_count_severe()) {
2558 		marray[0] = m;
2559 		*reqpage = 0;
2560 		return 1;
2561 	}
2562 
2563 	/*
2564 	 * scan backward for the read behind pages -- in memory
2565 	 *
2566 	 * Assume that if the page is not found an interrupt will not
2567 	 * create it.  Theoretically interrupts can only remove (busy)
2568 	 * pages, not create new associations.
2569 	 */
2570 	if (pindex > 0) {
2571 		if (rbehind > pindex) {
2572 			rbehind = pindex;
2573 			startpindex = 0;
2574 		} else {
2575 			startpindex = pindex - rbehind;
2576 		}
2577 
2578 		vm_object_hold(object);
2579 		for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2580 			if (vm_page_lookup(object, tpindex - 1))
2581 				break;
2582 		}
2583 
2584 		i = 0;
2585 		while (tpindex < pindex) {
2586 			rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2587 							     VM_ALLOC_NULL_OK);
2588 			if (rtm == NULL) {
2589 				for (j = 0; j < i; j++) {
2590 					vm_page_free(marray[j]);
2591 				}
2592 				vm_object_drop(object);
2593 				marray[0] = m;
2594 				*reqpage = 0;
2595 				return 1;
2596 			}
2597 			marray[i] = rtm;
2598 			++i;
2599 			++tpindex;
2600 		}
2601 		vm_object_drop(object);
2602 	} else {
2603 		i = 0;
2604 	}
2605 
2606 	/*
2607 	 * Assign requested page
2608 	 */
2609 	marray[i] = m;
2610 	*reqpage = i;
2611 	++i;
2612 
2613 	/*
2614 	 * Scan forwards for read-ahead pages
2615 	 */
2616 	tpindex = pindex + 1;
2617 	endpindex = tpindex + rahead;
2618 	if (endpindex > object->size)
2619 		endpindex = object->size;
2620 
2621 	vm_object_hold(object);
2622 	while (tpindex < endpindex) {
2623 		if (vm_page_lookup(object, tpindex))
2624 			break;
2625 		rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2626 						     VM_ALLOC_NULL_OK);
2627 		if (rtm == NULL)
2628 			break;
2629 		marray[i] = rtm;
2630 		++i;
2631 		++tpindex;
2632 	}
2633 	vm_object_drop(object);
2634 
2635 	return (i);
2636 }
2637 
2638 #endif
2639 
2640 /*
2641  * vm_prefault() provides a quick way of clustering pagefaults into a
2642  * processes address space.  It is a "cousin" of pmap_object_init_pt,
2643  * except it runs at page fault time instead of mmap time.
2644  *
2645  * vm.fast_fault	Enables pre-faulting zero-fill pages
2646  *
2647  * vm.prefault_pages	Number of pages (1/2 negative, 1/2 positive) to
2648  *			prefault.  Scan stops in either direction when
2649  *			a page is found to already exist.
2650  *
2651  * This code used to be per-platform pmap_prefault().  It is now
2652  * machine-independent and enhanced to also pre-fault zero-fill pages
2653  * (see vm.fast_fault) as well as make them writable, which greatly
2654  * reduces the number of page faults programs incur.
2655  *
2656  * Application performance when pre-faulting zero-fill pages is heavily
2657  * dependent on the application.  Very tiny applications like /bin/echo
2658  * lose a little performance while applications of any appreciable size
2659  * gain performance.  Prefaulting multiple pages also reduces SMP
2660  * congestion and can improve SMP performance significantly.
2661  *
2662  * NOTE!  prot may allow writing but this only applies to the top level
2663  *	  object.  If we wind up mapping a page extracted from a backing
2664  *	  object we have to make sure it is read-only.
2665  *
2666  * NOTE!  The caller has already handled any COW operations on the
2667  *	  vm_map_entry via the normal fault code.  Do NOT call this
2668  *	  shortcut unless the normal fault code has run on this entry.
2669  *
2670  * The related map must be locked.
2671  * No other requirements.
2672  */
2673 static int vm_prefault_pages = 8;
2674 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2675 	   "Maximum number of pages to pre-fault");
2676 static int vm_fast_fault = 1;
2677 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2678 	   "Burst fault zero-fill regions");
2679 
2680 /*
2681  * Set PG_NOSYNC if the map entry indicates so, but only if the page
2682  * is not already dirty by other means.  This will prevent passive
2683  * filesystem syncing as well as 'sync' from writing out the page.
2684  */
2685 static void
2686 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2687 {
2688 	if (entry->eflags & MAP_ENTRY_NOSYNC) {
2689 		if (m->dirty == 0)
2690 			vm_page_flag_set(m, PG_NOSYNC);
2691 	} else {
2692 		vm_page_flag_clear(m, PG_NOSYNC);
2693 	}
2694 }
2695 
2696 static void
2697 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2698 	    int fault_flags)
2699 {
2700 	struct lwp *lp;
2701 	vm_page_t m;
2702 	vm_offset_t addr;
2703 	vm_pindex_t index;
2704 	vm_pindex_t pindex;
2705 	vm_object_t object;
2706 	int pprot;
2707 	int i;
2708 	int noneg;
2709 	int nopos;
2710 	int maxpages;
2711 
2712 	/*
2713 	 * Get stable max count value, disabled if set to 0
2714 	 */
2715 	maxpages = vm_prefault_pages;
2716 	cpu_ccfence();
2717 	if (maxpages <= 0)
2718 		return;
2719 
2720 	/*
2721 	 * We do not currently prefault mappings that use virtual page
2722 	 * tables.  We do not prefault foreign pmaps.
2723 	 */
2724 	if (entry->maptype != VM_MAPTYPE_NORMAL)
2725 		return;
2726 	lp = curthread->td_lwp;
2727 	if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2728 		return;
2729 
2730 	/*
2731 	 * Limit pre-fault count to 1024 pages.
2732 	 */
2733 	if (maxpages > 1024)
2734 		maxpages = 1024;
2735 
2736 	object = entry->object.vm_object;
2737 	KKASSERT(object != NULL);
2738 	KKASSERT(object == entry->object.vm_object);
2739 
2740 	/*
2741 	 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2742 	 *	 now (or do something more complex XXX).
2743 	 */
2744 	vm_object_hold(object);
2745 	vm_object_chain_acquire(object, 0);
2746 
2747 	noneg = 0;
2748 	nopos = 0;
2749 	for (i = 0; i < maxpages; ++i) {
2750 		vm_object_t lobject;
2751 		vm_object_t nobject;
2752 		int allocated = 0;
2753 		int error;
2754 
2755 		/*
2756 		 * This can eat a lot of time on a heavily contended
2757 		 * machine so yield on the tick if needed.
2758 		 */
2759 		if ((i & 7) == 7)
2760 			lwkt_yield();
2761 
2762 		/*
2763 		 * Calculate the page to pre-fault, stopping the scan in
2764 		 * each direction separately if the limit is reached.
2765 		 */
2766 		if (i & 1) {
2767 			if (noneg)
2768 				continue;
2769 			addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2770 		} else {
2771 			if (nopos)
2772 				continue;
2773 			addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2774 		}
2775 		if (addr < entry->start) {
2776 			noneg = 1;
2777 			if (noneg && nopos)
2778 				break;
2779 			continue;
2780 		}
2781 		if (addr >= entry->end) {
2782 			nopos = 1;
2783 			if (noneg && nopos)
2784 				break;
2785 			continue;
2786 		}
2787 
2788 		/*
2789 		 * Skip pages already mapped, and stop scanning in that
2790 		 * direction.  When the scan terminates in both directions
2791 		 * we are done.
2792 		 */
2793 		if (pmap_prefault_ok(pmap, addr) == 0) {
2794 			if (i & 1)
2795 				noneg = 1;
2796 			else
2797 				nopos = 1;
2798 			if (noneg && nopos)
2799 				break;
2800 			continue;
2801 		}
2802 
2803 		/*
2804 		 * Follow the VM object chain to obtain the page to be mapped
2805 		 * into the pmap.
2806 		 *
2807 		 * If we reach the terminal object without finding a page
2808 		 * and we determine it would be advantageous, then allocate
2809 		 * a zero-fill page for the base object.  The base object
2810 		 * is guaranteed to be OBJT_DEFAULT for this case.
2811 		 *
2812 		 * In order to not have to check the pager via *haspage*()
2813 		 * we stop if any non-default object is encountered.  e.g.
2814 		 * a vnode or swap object would stop the loop.
2815 		 */
2816 		index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2817 		lobject = object;
2818 		pindex = index;
2819 		pprot = prot;
2820 
2821 		KKASSERT(lobject == entry->object.vm_object);
2822 		/*vm_object_hold(lobject); implied */
2823 
2824 		while ((m = vm_page_lookup_busy_try(lobject, pindex,
2825 						    TRUE, &error)) == NULL) {
2826 			if (lobject->type != OBJT_DEFAULT)
2827 				break;
2828 			if (lobject->backing_object == NULL) {
2829 				if (vm_fast_fault == 0)
2830 					break;
2831 				if ((prot & VM_PROT_WRITE) == 0 ||
2832 				    vm_page_count_min(0)) {
2833 					break;
2834 				}
2835 
2836 				/*
2837 				 * NOTE: Allocated from base object
2838 				 */
2839 				m = vm_page_alloc(object, index,
2840 						  VM_ALLOC_NORMAL |
2841 						  VM_ALLOC_ZERO |
2842 						  VM_ALLOC_USE_GD |
2843 						  VM_ALLOC_NULL_OK);
2844 				if (m == NULL)
2845 					break;
2846 				allocated = 1;
2847 				pprot = prot;
2848 				/* lobject = object .. not needed */
2849 				break;
2850 			}
2851 			if (lobject->backing_object_offset & PAGE_MASK)
2852 				break;
2853 			nobject = lobject->backing_object;
2854 			vm_object_hold(nobject);
2855 			KKASSERT(nobject == lobject->backing_object);
2856 			pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2857 			if (lobject != object) {
2858 				vm_object_lock_swap();
2859 				vm_object_drop(lobject);
2860 			}
2861 			lobject = nobject;
2862 			pprot &= ~VM_PROT_WRITE;
2863 			vm_object_chain_acquire(lobject, 0);
2864 		}
2865 
2866 		/*
2867 		 * NOTE: A non-NULL (m) will be associated with lobject if
2868 		 *	 it was found there, otherwise it is probably a
2869 		 *	 zero-fill page associated with the base object.
2870 		 *
2871 		 * Give-up if no page is available.
2872 		 */
2873 		if (m == NULL) {
2874 			if (lobject != object) {
2875 #if 0
2876 				if (object->backing_object != lobject)
2877 					vm_object_hold(object->backing_object);
2878 #endif
2879 				vm_object_chain_release_all(
2880 					object->backing_object, lobject);
2881 #if 0
2882 				if (object->backing_object != lobject)
2883 					vm_object_drop(object->backing_object);
2884 #endif
2885 				vm_object_drop(lobject);
2886 			}
2887 			break;
2888 		}
2889 
2890 		/*
2891 		 * The object must be marked dirty if we are mapping a
2892 		 * writable page.  m->object is either lobject or object,
2893 		 * both of which are still held.  Do this before we
2894 		 * potentially drop the object.
2895 		 */
2896 		if (pprot & VM_PROT_WRITE)
2897 			vm_object_set_writeable_dirty(m->object);
2898 
2899 		/*
2900 		 * Do not conditionalize on PG_RAM.  If pages are present in
2901 		 * the VM system we assume optimal caching.  If caching is
2902 		 * not optimal the I/O gravy train will be restarted when we
2903 		 * hit an unavailable page.  We do not want to try to restart
2904 		 * the gravy train now because we really don't know how much
2905 		 * of the object has been cached.  The cost for restarting
2906 		 * the gravy train should be low (since accesses will likely
2907 		 * be I/O bound anyway).
2908 		 */
2909 		if (lobject != object) {
2910 #if 0
2911 			if (object->backing_object != lobject)
2912 				vm_object_hold(object->backing_object);
2913 #endif
2914 			vm_object_chain_release_all(object->backing_object,
2915 						    lobject);
2916 #if 0
2917 			if (object->backing_object != lobject)
2918 				vm_object_drop(object->backing_object);
2919 #endif
2920 			vm_object_drop(lobject);
2921 		}
2922 
2923 		/*
2924 		 * Enter the page into the pmap if appropriate.  If we had
2925 		 * allocated the page we have to place it on a queue.  If not
2926 		 * we just have to make sure it isn't on the cache queue
2927 		 * (pages on the cache queue are not allowed to be mapped).
2928 		 */
2929 		if (allocated) {
2930 			/*
2931 			 * Page must be zerod.
2932 			 */
2933 			vm_page_zero_fill(m);
2934 			mycpu->gd_cnt.v_zfod++;
2935 			m->valid = VM_PAGE_BITS_ALL;
2936 
2937 			/*
2938 			 * Handle dirty page case
2939 			 */
2940 			if (pprot & VM_PROT_WRITE)
2941 				vm_set_nosync(m, entry);
2942 			pmap_enter(pmap, addr, m, pprot, 0, entry);
2943 			mycpu->gd_cnt.v_vm_faults++;
2944 			if (curthread->td_lwp)
2945 				++curthread->td_lwp->lwp_ru.ru_minflt;
2946 			vm_page_deactivate(m);
2947 			if (pprot & VM_PROT_WRITE) {
2948 				/*vm_object_set_writeable_dirty(m->object);*/
2949 				vm_set_nosync(m, entry);
2950 				if (fault_flags & VM_FAULT_DIRTY) {
2951 					vm_page_dirty(m);
2952 					/*XXX*/
2953 					swap_pager_unswapped(m);
2954 				}
2955 			}
2956 			vm_page_wakeup(m);
2957 		} else if (error) {
2958 			/* couldn't busy page, no wakeup */
2959 		} else if (
2960 		    ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2961 		    (m->flags & PG_FICTITIOUS) == 0) {
2962 			/*
2963 			 * A fully valid page not undergoing soft I/O can
2964 			 * be immediately entered into the pmap.
2965 			 */
2966 			if ((m->queue - m->pc) == PQ_CACHE)
2967 				vm_page_deactivate(m);
2968 			if (pprot & VM_PROT_WRITE) {
2969 				/*vm_object_set_writeable_dirty(m->object);*/
2970 				vm_set_nosync(m, entry);
2971 				if (fault_flags & VM_FAULT_DIRTY) {
2972 					vm_page_dirty(m);
2973 					/*XXX*/
2974 					swap_pager_unswapped(m);
2975 				}
2976 			}
2977 			if (pprot & VM_PROT_WRITE)
2978 				vm_set_nosync(m, entry);
2979 			pmap_enter(pmap, addr, m, pprot, 0, entry);
2980 			mycpu->gd_cnt.v_vm_faults++;
2981 			if (curthread->td_lwp)
2982 				++curthread->td_lwp->lwp_ru.ru_minflt;
2983 			vm_page_wakeup(m);
2984 		} else {
2985 			vm_page_wakeup(m);
2986 		}
2987 	}
2988 	vm_object_chain_release(object);
2989 	vm_object_drop(object);
2990 }
2991 
2992 /*
2993  * Object can be held shared
2994  */
2995 static void
2996 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2997 		  vm_map_entry_t entry, int prot, int fault_flags)
2998 {
2999 	struct lwp *lp;
3000 	vm_page_t m;
3001 	vm_offset_t addr;
3002 	vm_pindex_t pindex;
3003 	vm_object_t object;
3004 	int i;
3005 	int noneg;
3006 	int nopos;
3007 	int maxpages;
3008 
3009 	/*
3010 	 * Get stable max count value, disabled if set to 0
3011 	 */
3012 	maxpages = vm_prefault_pages;
3013 	cpu_ccfence();
3014 	if (maxpages <= 0)
3015 		return;
3016 
3017 	/*
3018 	 * We do not currently prefault mappings that use virtual page
3019 	 * tables.  We do not prefault foreign pmaps.
3020 	 */
3021 	if (entry->maptype != VM_MAPTYPE_NORMAL)
3022 		return;
3023 	lp = curthread->td_lwp;
3024 	if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
3025 		return;
3026 	object = entry->object.vm_object;
3027 	if (object->backing_object != NULL)
3028 		return;
3029 	ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
3030 
3031 	/*
3032 	 * Limit pre-fault count to 1024 pages.
3033 	 */
3034 	if (maxpages > 1024)
3035 		maxpages = 1024;
3036 
3037 	noneg = 0;
3038 	nopos = 0;
3039 	for (i = 0; i < maxpages; ++i) {
3040 		int error;
3041 
3042 		/*
3043 		 * Calculate the page to pre-fault, stopping the scan in
3044 		 * each direction separately if the limit is reached.
3045 		 */
3046 		if (i & 1) {
3047 			if (noneg)
3048 				continue;
3049 			addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
3050 		} else {
3051 			if (nopos)
3052 				continue;
3053 			addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
3054 		}
3055 		if (addr < entry->start) {
3056 			noneg = 1;
3057 			if (noneg && nopos)
3058 				break;
3059 			continue;
3060 		}
3061 		if (addr >= entry->end) {
3062 			nopos = 1;
3063 			if (noneg && nopos)
3064 				break;
3065 			continue;
3066 		}
3067 
3068 		/*
3069 		 * Follow the VM object chain to obtain the page to be mapped
3070 		 * into the pmap.  This version of the prefault code only
3071 		 * works with terminal objects.
3072 		 *
3073 		 * The page must already exist.  If we encounter a problem
3074 		 * we stop here.
3075 		 *
3076 		 * WARNING!  We cannot call swap_pager_unswapped() or insert
3077 		 *	     a new vm_page with a shared token.
3078 		 */
3079 		pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
3080 
3081 		/*
3082 		 * Skip pages already mapped, and stop scanning in that
3083 		 * direction.  When the scan terminates in both directions
3084 		 * we are done.
3085 		 */
3086 		if (pmap_prefault_ok(pmap, addr) == 0) {
3087 			if (i & 1)
3088 				noneg = 1;
3089 			else
3090 				nopos = 1;
3091 			if (noneg && nopos)
3092 				break;
3093 			continue;
3094 		}
3095 
3096 		/*
3097 		 * Shortcut the read-only mapping case using the far more
3098 		 * efficient vm_page_lookup_sbusy_try() function.  This
3099 		 * allows us to acquire the page soft-busied only which
3100 		 * is especially nice for concurrent execs of the same
3101 		 * program.
3102 		 *
3103 		 * The lookup function also validates page suitability
3104 		 * (all valid bits set, and not fictitious).
3105 		 *
3106 		 * If the page is in PQ_CACHE we have to fall-through
3107 		 * and hard-busy it so we can move it out of PQ_CACHE.
3108 		 */
3109 		if ((prot & (VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE)) == 0) {
3110 			m = vm_page_lookup_sbusy_try(object, pindex,
3111 						     0, PAGE_SIZE);
3112 			if (m == NULL)
3113 				break;
3114 			if ((m->queue - m->pc) != PQ_CACHE) {
3115 				pmap_enter(pmap, addr, m, prot, 0, entry);
3116 				mycpu->gd_cnt.v_vm_faults++;
3117 				if (curthread->td_lwp)
3118 					++curthread->td_lwp->lwp_ru.ru_minflt;
3119 				vm_page_sbusy_drop(m);
3120 				continue;
3121 			}
3122 			vm_page_sbusy_drop(m);
3123 		}
3124 
3125 		/*
3126 		 * Fallback to normal vm_page lookup code.  This code
3127 		 * hard-busies the page.  Not only that, but the page
3128 		 * can remain in that state for a significant period
3129 		 * time due to pmap_enter()'s overhead.
3130 		 */
3131 		m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
3132 		if (m == NULL || error)
3133 			break;
3134 
3135 		/*
3136 		 * Stop if the page cannot be trivially entered into the
3137 		 * pmap.
3138 		 */
3139 		if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) ||
3140 		    (m->flags & PG_FICTITIOUS) ||
3141 		    ((m->flags & PG_SWAPPED) &&
3142 		     (prot & VM_PROT_WRITE) &&
3143 		     (fault_flags & VM_FAULT_DIRTY))) {
3144 			vm_page_wakeup(m);
3145 			break;
3146 		}
3147 
3148 		/*
3149 		 * Enter the page into the pmap.  The object might be held
3150 		 * shared so we can't do any (serious) modifying operation
3151 		 * on it.
3152 		 */
3153 		if ((m->queue - m->pc) == PQ_CACHE)
3154 			vm_page_deactivate(m);
3155 		if (prot & VM_PROT_WRITE) {
3156 			vm_object_set_writeable_dirty(m->object);
3157 			vm_set_nosync(m, entry);
3158 			if (fault_flags & VM_FAULT_DIRTY) {
3159 				vm_page_dirty(m);
3160 				/* can't happeen due to conditional above */
3161 				/* swap_pager_unswapped(m); */
3162 			}
3163 		}
3164 		pmap_enter(pmap, addr, m, prot, 0, entry);
3165 		mycpu->gd_cnt.v_vm_faults++;
3166 		if (curthread->td_lwp)
3167 			++curthread->td_lwp->lwp_ru.ru_minflt;
3168 		vm_page_wakeup(m);
3169 	}
3170 }
3171