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