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