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