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