kernel - Many fixes for vkernel support, plus a few main kernel fixes
[dragonfly.git] / sys / vm / vm_fault.c
blobc356c329dee1d206727064b1aee8740e784af47c
1 /*
2 * Copyright (c) 2003-2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * ---
36 * Copyright (c) 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 * Copyright (c) 1994 John S. Dyson
39 * All rights reserved.
40 * Copyright (c) 1994 David Greenman
41 * All rights reserved.
44 * This code is derived from software contributed to Berkeley by
45 * The Mach Operating System project at Carnegie-Mellon University.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
49 * are met:
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69 * SUCH DAMAGE.
71 * ---
73 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
74 * All rights reserved.
76 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
78 * Permission to use, copy, modify and distribute this software and
79 * its documentation is hereby granted, provided that both the copyright
80 * notice and this permission notice appear in all copies of the
81 * software, derivative works or modified versions, and any portions
82 * thereof, and that both notices appear in supporting documentation.
84 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
85 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
86 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
88 * Carnegie Mellon requests users of this software to return to
90 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
91 * School of Computer Science
92 * Carnegie Mellon University
93 * Pittsburgh PA 15213-3890
95 * any improvements or extensions that they make and grant Carnegie the
96 * rights to redistribute these changes.
100 * Page fault handling module.
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/proc.h>
107 #include <sys/vnode.h>
108 #include <sys/resourcevar.h>
109 #include <sys/vmmeter.h>
110 #include <sys/vkernel.h>
111 #include <sys/lock.h>
112 #include <sys/sysctl.h>
114 #include <cpu/lwbuf.h>
116 #include <vm/vm.h>
117 #include <vm/vm_param.h>
118 #include <vm/pmap.h>
119 #include <vm/vm_map.h>
120 #include <vm/vm_object.h>
121 #include <vm/vm_page.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_pager.h>
125 #include <vm/vnode_pager.h>
126 #include <vm/vm_extern.h>
128 #include <sys/thread2.h>
129 #include <vm/vm_page2.h>
131 struct faultstate {
132 vm_page_t m;
133 vm_object_t object;
134 vm_pindex_t pindex;
135 vm_prot_t prot;
136 vm_page_t first_m;
137 vm_object_t first_object;
138 vm_prot_t first_prot;
139 vm_map_t map;
140 vm_map_entry_t entry;
141 int lookup_still_valid;
142 int hardfault;
143 int fault_flags;
144 int map_generation;
145 int shared;
146 int first_shared;
147 boolean_t wired;
148 struct vnode *vp;
151 static int debug_fault = 0;
152 SYSCTL_INT(_vm, OID_AUTO, debug_fault, CTLFLAG_RW, &debug_fault, 0, "");
153 static int debug_cluster = 0;
154 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
155 int vm_shared_fault = 1;
156 TUNABLE_INT("vm.shared_fault", &vm_shared_fault);
157 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW, &vm_shared_fault, 0,
158 "Allow shared token on vm_object");
160 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t, int);
161 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *,
162 vpte_t, int, int);
163 #if 0
164 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
165 #endif
166 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
167 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
168 vm_map_entry_t entry, int prot, int fault_flags);
169 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
170 vm_map_entry_t entry, int prot, int fault_flags);
172 static __inline void
173 release_page(struct faultstate *fs)
175 vm_page_deactivate(fs->m);
176 vm_page_wakeup(fs->m);
177 fs->m = NULL;
181 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
182 * requires relocking and then checking the timestamp.
184 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
185 * not have to update fs->map_generation here.
187 * NOTE: This function can fail due to a deadlock against the caller's
188 * holding of a vm_page BUSY.
190 static __inline int
191 relock_map(struct faultstate *fs)
193 int error;
195 if (fs->lookup_still_valid == FALSE && fs->map) {
196 error = vm_map_lock_read_to(fs->map);
197 if (error == 0)
198 fs->lookup_still_valid = TRUE;
199 } else {
200 error = 0;
202 return error;
205 static __inline void
206 unlock_map(struct faultstate *fs)
208 if (fs->lookup_still_valid && fs->map) {
209 vm_map_lookup_done(fs->map, fs->entry, 0);
210 fs->lookup_still_valid = FALSE;
215 * Clean up after a successful call to vm_fault_object() so another call
216 * to vm_fault_object() can be made.
218 static void
219 _cleanup_successful_fault(struct faultstate *fs, int relock)
222 * We allocated a junk page for a COW operation that did
223 * not occur, the page must be freed.
225 if (fs->object != fs->first_object) {
226 KKASSERT(fs->first_shared == 0);
227 vm_page_free(fs->first_m);
228 vm_object_pip_wakeup(fs->object);
229 fs->first_m = NULL;
233 * Reset fs->object.
235 fs->object = fs->first_object;
236 if (relock && fs->lookup_still_valid == FALSE) {
237 if (fs->map)
238 vm_map_lock_read(fs->map);
239 fs->lookup_still_valid = TRUE;
243 static void
244 _unlock_things(struct faultstate *fs, int dealloc)
246 _cleanup_successful_fault(fs, 0);
247 if (dealloc) {
248 /*vm_object_deallocate(fs->first_object);*/
249 /*fs->first_object = NULL; drop used later on */
251 unlock_map(fs);
252 if (fs->vp != NULL) {
253 vput(fs->vp);
254 fs->vp = NULL;
258 #define unlock_things(fs) _unlock_things(fs, 0)
259 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
260 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
263 * TRYPAGER
265 * Determine if the pager for the current object *might* contain the page.
267 * We only need to try the pager if this is not a default object (default
268 * objects are zero-fill and have no real pager), and if we are not taking
269 * a wiring fault or if the FS entry is wired.
271 #define TRYPAGER(fs) \
272 (fs->object->type != OBJT_DEFAULT && \
273 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
276 * vm_fault:
278 * Handle a page fault occuring at the given address, requiring the given
279 * permissions, in the map specified. If successful, the page is inserted
280 * into the associated physical map.
282 * NOTE: The given address should be truncated to the proper page address.
284 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
285 * a standard error specifying why the fault is fatal is returned.
287 * The map in question must be referenced, and remains so.
288 * The caller may hold no locks.
289 * No other requirements.
292 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
294 int result;
295 vm_pindex_t first_pindex;
296 struct faultstate fs;
297 struct lwp *lp;
298 struct proc *p;
299 thread_t td;
300 int growstack;
301 int retry = 0;
302 int inherit_prot;
304 inherit_prot = fault_type & VM_PROT_NOSYNC;
305 fs.hardfault = 0;
306 fs.fault_flags = fault_flags;
307 fs.vp = NULL;
308 fs.shared = vm_shared_fault;
309 fs.first_shared = vm_shared_fault;
310 growstack = 1;
313 * vm_map interactions
315 td = curthread;
316 if ((lp = td->td_lwp) != NULL)
317 lp->lwp_flags |= LWP_PAGING;
318 lwkt_gettoken(&map->token);
320 RetryFault:
322 * Find the vm_map_entry representing the backing store and resolve
323 * the top level object and page index. This may have the side
324 * effect of executing a copy-on-write on the map entry and/or
325 * creating a shadow object, but will not COW any actual VM pages.
327 * On success fs.map is left read-locked and various other fields
328 * are initialized but not otherwise referenced or locked.
330 * NOTE! vm_map_lookup will try to upgrade the fault_type to
331 * VM_FAULT_WRITE if the map entry is a virtual page table
332 * and also writable, so we can set the 'A'accessed bit in
333 * the virtual page table entry.
335 fs.map = map;
336 result = vm_map_lookup(&fs.map, vaddr, fault_type,
337 &fs.entry, &fs.first_object,
338 &first_pindex, &fs.first_prot, &fs.wired);
341 * If the lookup failed or the map protections are incompatible,
342 * the fault generally fails.
344 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
345 * tried to do a COW fault.
347 * If the caller is trying to do a user wiring we have more work
348 * to do.
350 if (result != KERN_SUCCESS) {
351 if (result == KERN_FAILURE_NOFAULT) {
352 result = KERN_FAILURE;
353 goto done;
355 if (result != KERN_PROTECTION_FAILURE ||
356 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
358 if (result == KERN_INVALID_ADDRESS && growstack &&
359 map != &kernel_map && curproc != NULL) {
360 result = vm_map_growstack(map, vaddr);
361 if (result == KERN_SUCCESS) {
362 growstack = 0;
363 ++retry;
364 goto RetryFault;
366 result = KERN_FAILURE;
368 goto done;
372 * If we are user-wiring a r/w segment, and it is COW, then
373 * we need to do the COW operation. Note that we don't
374 * currently COW RO sections now, because it is NOT desirable
375 * to COW .text. We simply keep .text from ever being COW'ed
376 * and take the heat that one cannot debug wired .text sections.
378 result = vm_map_lookup(&fs.map, vaddr,
379 VM_PROT_READ|VM_PROT_WRITE|
380 VM_PROT_OVERRIDE_WRITE,
381 &fs.entry, &fs.first_object,
382 &first_pindex, &fs.first_prot,
383 &fs.wired);
384 if (result != KERN_SUCCESS) {
385 /* could also be KERN_FAILURE_NOFAULT */
386 result = KERN_FAILURE;
387 goto done;
391 * If we don't COW now, on a user wire, the user will never
392 * be able to write to the mapping. If we don't make this
393 * restriction, the bookkeeping would be nearly impossible.
395 * XXX We have a shared lock, this will have a MP race but
396 * I don't see how it can hurt anything.
398 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
399 atomic_clear_char(&fs.entry->max_protection,
400 VM_PROT_WRITE);
405 * fs.map is read-locked
407 * Misc checks. Save the map generation number to detect races.
409 fs.map_generation = fs.map->timestamp;
410 fs.lookup_still_valid = TRUE;
411 fs.first_m = NULL;
412 fs.object = fs.first_object; /* so unlock_and_deallocate works */
413 fs.prot = fs.first_prot; /* default (used by uksmap) */
415 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
416 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
417 panic("vm_fault: fault on nofault entry, addr: %p",
418 (void *)vaddr);
420 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
421 vaddr >= fs.entry->start &&
422 vaddr < fs.entry->start + PAGE_SIZE) {
423 panic("vm_fault: fault on stack guard, addr: %p",
424 (void *)vaddr);
429 * A user-kernel shared map has no VM object and bypasses
430 * everything. We execute the uksmap function with a temporary
431 * fictitious vm_page. The address is directly mapped with no
432 * management.
434 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
435 struct vm_page fakem;
437 bzero(&fakem, sizeof(fakem));
438 fakem.pindex = first_pindex;
439 fakem.flags = PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED;
440 fakem.valid = VM_PAGE_BITS_ALL;
441 fakem.pat_mode = VM_MEMATTR_DEFAULT;
442 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
443 result = KERN_FAILURE;
444 unlock_things(&fs);
445 goto done2;
447 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
448 fs.wired, fs.entry);
449 goto done_success;
453 * A system map entry may return a NULL object. No object means
454 * no pager means an unrecoverable kernel fault.
456 if (fs.first_object == NULL) {
457 panic("vm_fault: unrecoverable fault at %p in entry %p",
458 (void *)vaddr, fs.entry);
462 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
463 * is set.
465 * Unfortunately a deadlock can occur if we are forced to page-in
466 * from swap, but diving all the way into the vm_pager_get_page()
467 * function to find out is too much. Just check the object type.
469 * The deadlock is a CAM deadlock on a busy VM page when trying
470 * to finish an I/O if another process gets stuck in
471 * vop_helper_read_shortcut() due to a swap fault.
473 if ((td->td_flags & TDF_NOFAULT) &&
474 (retry ||
475 fs.first_object->type == OBJT_VNODE ||
476 fs.first_object->type == OBJT_SWAP ||
477 fs.first_object->backing_object)) {
478 result = KERN_FAILURE;
479 unlock_things(&fs);
480 goto done2;
484 * If the entry is wired we cannot change the page protection.
486 if (fs.wired)
487 fault_type = fs.first_prot;
490 * We generally want to avoid unnecessary exclusive modes on backing
491 * and terminal objects because this can seriously interfere with
492 * heavily fork()'d processes (particularly /bin/sh scripts).
494 * However, we also want to avoid unnecessary retries due to needed
495 * shared->exclusive promotion for common faults. Exclusive mode is
496 * always needed if any page insertion, rename, or free occurs in an
497 * object (and also indirectly if any I/O is done).
499 * The main issue here is going to be fs.first_shared. If the
500 * first_object has a backing object which isn't shadowed and the
501 * process is single-threaded we might as well use an exclusive
502 * lock/chain right off the bat.
504 if (fs.first_shared && fs.first_object->backing_object &&
505 LIST_EMPTY(&fs.first_object->shadow_head) &&
506 td->td_proc && td->td_proc->p_nthreads == 1) {
507 fs.first_shared = 0;
511 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
512 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
513 * we can try shared first.
515 if (fault_flags & VM_FAULT_UNSWAP) {
516 fs.first_shared = 0;
520 * Obtain a top-level object lock, shared or exclusive depending
521 * on fs.first_shared. If a shared lock winds up being insufficient
522 * we will retry with an exclusive lock.
524 * The vnode pager lock is always shared.
526 if (fs.first_shared)
527 vm_object_hold_shared(fs.first_object);
528 else
529 vm_object_hold(fs.first_object);
530 if (fs.vp == NULL)
531 fs.vp = vnode_pager_lock(fs.first_object);
534 * The page we want is at (first_object, first_pindex), but if the
535 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
536 * page table to figure out the actual pindex.
538 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
539 * ONLY
541 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
542 result = vm_fault_vpagetable(&fs, &first_pindex,
543 fs.entry->aux.master_pde,
544 fault_type, 1);
545 if (result == KERN_TRY_AGAIN) {
546 vm_object_drop(fs.first_object);
547 ++retry;
548 goto RetryFault;
550 if (result != KERN_SUCCESS)
551 goto done;
555 * Now we have the actual (object, pindex), fault in the page. If
556 * vm_fault_object() fails it will unlock and deallocate the FS
557 * data. If it succeeds everything remains locked and fs->object
558 * will have an additional PIP count if it is not equal to
559 * fs->first_object
561 * vm_fault_object will set fs->prot for the pmap operation. It is
562 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
563 * page can be safely written. However, it will force a read-only
564 * mapping for a read fault if the memory is managed by a virtual
565 * page table.
567 * If the fault code uses the shared object lock shortcut
568 * we must not try to burst (we can't allocate VM pages).
570 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
572 if (debug_fault > 0) {
573 --debug_fault;
574 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
575 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
576 result, (intmax_t)vaddr, fault_type, fault_flags,
577 fs.m, fs.prot, fs.wired, fs.entry);
580 if (result == KERN_TRY_AGAIN) {
581 vm_object_drop(fs.first_object);
582 ++retry;
583 goto RetryFault;
585 if (result != KERN_SUCCESS)
586 goto done;
589 * On success vm_fault_object() does not unlock or deallocate, and fs.m
590 * will contain a busied page.
592 * Enter the page into the pmap and do pmap-related adjustments.
594 KKASSERT(fs.lookup_still_valid == TRUE);
595 vm_page_flag_set(fs.m, PG_REFERENCED);
596 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot,
597 fs.wired, fs.entry);
599 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
600 KKASSERT(fs.m->flags & PG_BUSY);
603 * If the page is not wired down, then put it where the pageout daemon
604 * can find it.
606 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
607 if (fs.wired)
608 vm_page_wire(fs.m);
609 else
610 vm_page_unwire(fs.m, 1);
611 } else {
612 vm_page_activate(fs.m);
614 vm_page_wakeup(fs.m);
617 * Burst in a few more pages if possible. The fs.map should still
618 * be locked. To avoid interlocking against a vnode->getblk
619 * operation we had to be sure to unbusy our primary vm_page above
620 * first.
622 * A normal burst can continue down backing store, only execute
623 * if we are holding an exclusive lock, otherwise the exclusive
624 * locks the burst code gets might cause excessive SMP collisions.
626 * A quick burst can be utilized when there is no backing object
627 * (i.e. a shared file mmap).
629 if ((fault_flags & VM_FAULT_BURST) &&
630 (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
631 fs.wired == 0) {
632 if (fs.first_shared == 0 && fs.shared == 0) {
633 vm_prefault(fs.map->pmap, vaddr,
634 fs.entry, fs.prot, fault_flags);
635 } else {
636 vm_prefault_quick(fs.map->pmap, vaddr,
637 fs.entry, fs.prot, fault_flags);
641 done_success:
642 mycpu->gd_cnt.v_vm_faults++;
643 if (td->td_lwp)
644 ++td->td_lwp->lwp_ru.ru_minflt;
647 * Unlock everything, and return
649 unlock_things(&fs);
651 if (td->td_lwp) {
652 if (fs.hardfault) {
653 td->td_lwp->lwp_ru.ru_majflt++;
654 } else {
655 td->td_lwp->lwp_ru.ru_minflt++;
659 /*vm_object_deallocate(fs.first_object);*/
660 /*fs.m = NULL; */
661 /*fs.first_object = NULL; must still drop later */
663 result = KERN_SUCCESS;
664 done:
665 if (fs.first_object)
666 vm_object_drop(fs.first_object);
667 done2:
668 lwkt_reltoken(&map->token);
669 if (lp)
670 lp->lwp_flags &= ~LWP_PAGING;
672 #if !defined(NO_SWAPPING)
674 * Check the process RSS limit and force deactivation and
675 * (asynchronous) paging if necessary. This is a complex operation,
676 * only do it for direct user-mode faults, for now.
678 * To reduce overhead implement approximately a ~16MB hysteresis.
680 p = td->td_proc;
681 if ((fault_flags & VM_FAULT_USERMODE) && lp &&
682 p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 &&
683 map != &kernel_map) {
684 vm_pindex_t limit;
685 vm_pindex_t size;
687 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
688 p->p_rlimit[RLIMIT_RSS].rlim_max));
689 size = pmap_resident_tlnw_count(map->pmap);
690 if (limit >= 0 && size > 4096 && size - 4096 >= limit) {
691 vm_pageout_map_deactivate_pages(map, limit);
694 #endif
696 return (result);
700 * Fault in the specified virtual address in the current process map,
701 * returning a held VM page or NULL. See vm_fault_page() for more
702 * information.
704 * No requirements.
706 vm_page_t
707 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type,
708 int *errorp, int *busyp)
710 struct lwp *lp = curthread->td_lwp;
711 vm_page_t m;
713 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
714 fault_type, VM_FAULT_NORMAL,
715 errorp, busyp);
716 return(m);
720 * Fault in the specified virtual address in the specified map, doing all
721 * necessary manipulation of the object store and all necessary I/O. Return
722 * a held VM page or NULL, and set *errorp. The related pmap is not
723 * updated.
725 * If busyp is not NULL then *busyp will be set to TRUE if this routine
726 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
727 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is
728 * NULL the returned page is only held.
730 * If the caller has no intention of writing to the page's contents, busyp
731 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
732 * without busying the page.
734 * The returned page will also be marked PG_REFERENCED.
736 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
737 * error will be returned.
739 * No requirements.
741 vm_page_t
742 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
743 int fault_flags, int *errorp, int *busyp)
745 vm_pindex_t first_pindex;
746 struct faultstate fs;
747 int result;
748 int retry;
749 int growstack;
750 vm_prot_t orig_fault_type = fault_type;
752 retry = 0;
753 fs.hardfault = 0;
754 fs.fault_flags = fault_flags;
755 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
758 * Dive the pmap (concurrency possible). If we find the
759 * appropriate page we can terminate early and quickly.
761 * This works great for normal programs but will always return
762 * NULL for host lookups of vkernel maps in VMM mode.
764 fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type, busyp);
765 if (fs.m) {
766 if (fault_type & (VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE))
767 vm_page_dirty(fs.m);
768 *errorp = 0;
769 return(fs.m);
773 * Otherwise take a concurrency hit and do a formal page
774 * fault.
776 fs.vp = NULL;
777 fs.shared = vm_shared_fault;
778 fs.first_shared = vm_shared_fault;
779 growstack = 1;
780 lwkt_gettoken(&map->token);
783 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
784 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
785 * we can try shared first.
787 if (fault_flags & VM_FAULT_UNSWAP) {
788 fs.first_shared = 0;
791 RetryFault:
793 * Find the vm_map_entry representing the backing store and resolve
794 * the top level object and page index. This may have the side
795 * effect of executing a copy-on-write on the map entry and/or
796 * creating a shadow object, but will not COW any actual VM pages.
798 * On success fs.map is left read-locked and various other fields
799 * are initialized but not otherwise referenced or locked.
801 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
802 * if the map entry is a virtual page table and also writable,
803 * so we can set the 'A'accessed bit in the virtual page table
804 * entry.
806 fs.map = map;
807 result = vm_map_lookup(&fs.map, vaddr, fault_type,
808 &fs.entry, &fs.first_object,
809 &first_pindex, &fs.first_prot, &fs.wired);
811 if (result != KERN_SUCCESS) {
812 if (result == KERN_FAILURE_NOFAULT) {
813 *errorp = KERN_FAILURE;
814 fs.m = NULL;
815 goto done;
817 if (result != KERN_PROTECTION_FAILURE ||
818 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
820 if (result == KERN_INVALID_ADDRESS && growstack &&
821 map != &kernel_map && curproc != NULL) {
822 result = vm_map_growstack(map, vaddr);
823 if (result == KERN_SUCCESS) {
824 growstack = 0;
825 ++retry;
826 goto RetryFault;
828 result = KERN_FAILURE;
830 fs.m = NULL;
831 *errorp = result;
832 goto done;
836 * If we are user-wiring a r/w segment, and it is COW, then
837 * we need to do the COW operation. Note that we don't
838 * currently COW RO sections now, because it is NOT desirable
839 * to COW .text. We simply keep .text from ever being COW'ed
840 * and take the heat that one cannot debug wired .text sections.
842 result = vm_map_lookup(&fs.map, vaddr,
843 VM_PROT_READ|VM_PROT_WRITE|
844 VM_PROT_OVERRIDE_WRITE,
845 &fs.entry, &fs.first_object,
846 &first_pindex, &fs.first_prot,
847 &fs.wired);
848 if (result != KERN_SUCCESS) {
849 /* could also be KERN_FAILURE_NOFAULT */
850 *errorp = KERN_FAILURE;
851 fs.m = NULL;
852 goto done;
856 * If we don't COW now, on a user wire, the user will never
857 * be able to write to the mapping. If we don't make this
858 * restriction, the bookkeeping would be nearly impossible.
860 * XXX We have a shared lock, this will have a MP race but
861 * I don't see how it can hurt anything.
863 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
864 atomic_clear_char(&fs.entry->max_protection,
865 VM_PROT_WRITE);
870 * fs.map is read-locked
872 * Misc checks. Save the map generation number to detect races.
874 fs.map_generation = fs.map->timestamp;
875 fs.lookup_still_valid = TRUE;
876 fs.first_m = NULL;
877 fs.object = fs.first_object; /* so unlock_and_deallocate works */
879 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
880 panic("vm_fault: fault on nofault entry, addr: %lx",
881 (u_long)vaddr);
885 * A user-kernel shared map has no VM object and bypasses
886 * everything. We execute the uksmap function with a temporary
887 * fictitious vm_page. The address is directly mapped with no
888 * management.
890 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
891 struct vm_page fakem;
893 bzero(&fakem, sizeof(fakem));
894 fakem.pindex = first_pindex;
895 fakem.flags = PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED;
896 fakem.valid = VM_PAGE_BITS_ALL;
897 fakem.pat_mode = VM_MEMATTR_DEFAULT;
898 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
899 *errorp = KERN_FAILURE;
900 fs.m = NULL;
901 unlock_things(&fs);
902 goto done2;
904 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
905 vm_page_hold(fs.m);
906 if (busyp)
907 *busyp = 0; /* don't need to busy R or W */
908 unlock_things(&fs);
909 *errorp = 0;
910 goto done;
915 * A system map entry may return a NULL object. No object means
916 * no pager means an unrecoverable kernel fault.
918 if (fs.first_object == NULL) {
919 panic("vm_fault: unrecoverable fault at %p in entry %p",
920 (void *)vaddr, fs.entry);
924 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
925 * is set.
927 * Unfortunately a deadlock can occur if we are forced to page-in
928 * from swap, but diving all the way into the vm_pager_get_page()
929 * function to find out is too much. Just check the object type.
931 if ((curthread->td_flags & TDF_NOFAULT) &&
932 (retry ||
933 fs.first_object->type == OBJT_VNODE ||
934 fs.first_object->type == OBJT_SWAP ||
935 fs.first_object->backing_object)) {
936 *errorp = KERN_FAILURE;
937 unlock_things(&fs);
938 fs.m = NULL;
939 goto done2;
943 * If the entry is wired we cannot change the page protection.
945 if (fs.wired)
946 fault_type = fs.first_prot;
949 * Make a reference to this object to prevent its disposal while we
950 * are messing with it. Once we have the reference, the map is free
951 * to be diddled. Since objects reference their shadows (and copies),
952 * they will stay around as well.
954 * The reference should also prevent an unexpected collapse of the
955 * parent that might move pages from the current object into the
956 * parent unexpectedly, resulting in corruption.
958 * Bump the paging-in-progress count to prevent size changes (e.g.
959 * truncation operations) during I/O. This must be done after
960 * obtaining the vnode lock in order to avoid possible deadlocks.
962 if (fs.first_shared)
963 vm_object_hold_shared(fs.first_object);
964 else
965 vm_object_hold(fs.first_object);
966 if (fs.vp == NULL)
967 fs.vp = vnode_pager_lock(fs.first_object); /* shared */
970 * The page we want is at (first_object, first_pindex), but if the
971 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
972 * page table to figure out the actual pindex.
974 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
975 * ONLY
977 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
978 result = vm_fault_vpagetable(&fs, &first_pindex,
979 fs.entry->aux.master_pde,
980 fault_type, 1);
981 if (result == KERN_TRY_AGAIN) {
982 vm_object_drop(fs.first_object);
983 ++retry;
984 goto RetryFault;
986 if (result != KERN_SUCCESS) {
987 *errorp = result;
988 fs.m = NULL;
989 goto done;
994 * Now we have the actual (object, pindex), fault in the page. If
995 * vm_fault_object() fails it will unlock and deallocate the FS
996 * data. If it succeeds everything remains locked and fs->object
997 * will have an additinal PIP count if it is not equal to
998 * fs->first_object
1000 fs.m = NULL;
1001 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
1003 if (result == KERN_TRY_AGAIN) {
1004 vm_object_drop(fs.first_object);
1005 ++retry;
1006 goto RetryFault;
1008 if (result != KERN_SUCCESS) {
1009 *errorp = result;
1010 fs.m = NULL;
1011 goto done;
1014 if ((orig_fault_type & VM_PROT_WRITE) &&
1015 (fs.prot & VM_PROT_WRITE) == 0) {
1016 *errorp = KERN_PROTECTION_FAILURE;
1017 unlock_and_deallocate(&fs);
1018 fs.m = NULL;
1019 goto done;
1023 * DO NOT UPDATE THE PMAP!!! This function may be called for
1024 * a pmap unrelated to the current process pmap, in which case
1025 * the current cpu core will not be listed in the pmap's pm_active
1026 * mask. Thus invalidation interlocks will fail to work properly.
1028 * (for example, 'ps' uses procfs to read program arguments from
1029 * each process's stack).
1031 * In addition to the above this function will be called to acquire
1032 * a page that might already be faulted in, re-faulting it
1033 * continuously is a waste of time.
1035 * XXX could this have been the cause of our random seg-fault
1036 * issues? procfs accesses user stacks.
1038 vm_page_flag_set(fs.m, PG_REFERENCED);
1039 #if 0
1040 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired, NULL);
1041 mycpu->gd_cnt.v_vm_faults++;
1042 if (curthread->td_lwp)
1043 ++curthread->td_lwp->lwp_ru.ru_minflt;
1044 #endif
1047 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1048 * will contain a busied page. So we must unlock here after having
1049 * messed with the pmap.
1051 unlock_things(&fs);
1054 * Return a held page. We are not doing any pmap manipulation so do
1055 * not set PG_MAPPED. However, adjust the page flags according to
1056 * the fault type because the caller may not use a managed pmapping
1057 * (so we don't want to lose the fact that the page will be dirtied
1058 * if a write fault was specified).
1060 if (fault_type & VM_PROT_WRITE)
1061 vm_page_dirty(fs.m);
1062 vm_page_activate(fs.m);
1064 if (curthread->td_lwp) {
1065 if (fs.hardfault) {
1066 curthread->td_lwp->lwp_ru.ru_majflt++;
1067 } else {
1068 curthread->td_lwp->lwp_ru.ru_minflt++;
1073 * Unlock everything, and return the held or busied page.
1075 if (busyp) {
1076 if (fault_type & (VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE)) {
1077 vm_page_dirty(fs.m);
1078 *busyp = 1;
1079 } else {
1080 *busyp = 0;
1081 vm_page_hold(fs.m);
1082 vm_page_wakeup(fs.m);
1084 } else {
1085 vm_page_hold(fs.m);
1086 vm_page_wakeup(fs.m);
1088 /*vm_object_deallocate(fs.first_object);*/
1089 /*fs.first_object = NULL; */
1090 *errorp = 0;
1092 done:
1093 if (fs.first_object)
1094 vm_object_drop(fs.first_object);
1095 done2:
1096 lwkt_reltoken(&map->token);
1097 return(fs.m);
1101 * Fault in the specified (object,offset), dirty the returned page as
1102 * needed. If the requested fault_type cannot be done NULL and an
1103 * error is returned.
1105 * A held (but not busied) page is returned.
1107 * The passed in object must be held as specified by the shared
1108 * argument.
1110 vm_page_t
1111 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
1112 vm_prot_t fault_type, int fault_flags,
1113 int *sharedp, int *errorp)
1115 int result;
1116 vm_pindex_t first_pindex;
1117 struct faultstate fs;
1118 struct vm_map_entry entry;
1120 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1121 bzero(&entry, sizeof(entry));
1122 entry.object.vm_object = object;
1123 entry.maptype = VM_MAPTYPE_NORMAL;
1124 entry.protection = entry.max_protection = fault_type;
1126 fs.hardfault = 0;
1127 fs.fault_flags = fault_flags;
1128 fs.map = NULL;
1129 fs.shared = vm_shared_fault;
1130 fs.first_shared = *sharedp;
1131 fs.vp = NULL;
1132 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
1135 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1136 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
1137 * we can try shared first.
1139 if (fs.first_shared && (fault_flags & VM_FAULT_UNSWAP)) {
1140 fs.first_shared = 0;
1141 vm_object_upgrade(object);
1145 * Retry loop as needed (typically for shared->exclusive transitions)
1147 RetryFault:
1148 *sharedp = fs.first_shared;
1149 first_pindex = OFF_TO_IDX(offset);
1150 fs.first_object = object;
1151 fs.entry = &entry;
1152 fs.first_prot = fault_type;
1153 fs.wired = 0;
1154 /*fs.map_generation = 0; unused */
1157 * Make a reference to this object to prevent its disposal while we
1158 * are messing with it. Once we have the reference, the map is free
1159 * to be diddled. Since objects reference their shadows (and copies),
1160 * they will stay around as well.
1162 * The reference should also prevent an unexpected collapse of the
1163 * parent that might move pages from the current object into the
1164 * parent unexpectedly, resulting in corruption.
1166 * Bump the paging-in-progress count to prevent size changes (e.g.
1167 * truncation operations) during I/O. This must be done after
1168 * obtaining the vnode lock in order to avoid possible deadlocks.
1170 if (fs.vp == NULL)
1171 fs.vp = vnode_pager_lock(fs.first_object);
1173 fs.lookup_still_valid = TRUE;
1174 fs.first_m = NULL;
1175 fs.object = fs.first_object; /* so unlock_and_deallocate works */
1177 #if 0
1178 /* XXX future - ability to operate on VM object using vpagetable */
1179 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1180 result = vm_fault_vpagetable(&fs, &first_pindex,
1181 fs.entry->aux.master_pde,
1182 fault_type, 0);
1183 if (result == KERN_TRY_AGAIN) {
1184 if (fs.first_shared == 0 && *sharedp)
1185 vm_object_upgrade(object);
1186 goto RetryFault;
1188 if (result != KERN_SUCCESS) {
1189 *errorp = result;
1190 return (NULL);
1193 #endif
1196 * Now we have the actual (object, pindex), fault in the page. If
1197 * vm_fault_object() fails it will unlock and deallocate the FS
1198 * data. If it succeeds everything remains locked and fs->object
1199 * will have an additinal PIP count if it is not equal to
1200 * fs->first_object
1202 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1203 * We may have to upgrade its lock to handle the requested fault.
1205 result = vm_fault_object(&fs, first_pindex, fault_type, 0);
1207 if (result == KERN_TRY_AGAIN) {
1208 if (fs.first_shared == 0 && *sharedp)
1209 vm_object_upgrade(object);
1210 goto RetryFault;
1212 if (result != KERN_SUCCESS) {
1213 *errorp = result;
1214 return(NULL);
1217 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1218 *errorp = KERN_PROTECTION_FAILURE;
1219 unlock_and_deallocate(&fs);
1220 return(NULL);
1224 * On success vm_fault_object() does not unlock or deallocate, so we
1225 * do it here. Note that the returned fs.m will be busied.
1227 unlock_things(&fs);
1230 * Return a held page. We are not doing any pmap manipulation so do
1231 * not set PG_MAPPED. However, adjust the page flags according to
1232 * the fault type because the caller may not use a managed pmapping
1233 * (so we don't want to lose the fact that the page will be dirtied
1234 * if a write fault was specified).
1236 vm_page_hold(fs.m);
1237 vm_page_activate(fs.m);
1238 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
1239 vm_page_dirty(fs.m);
1240 if (fault_flags & VM_FAULT_UNSWAP)
1241 swap_pager_unswapped(fs.m);
1244 * Indicate that the page was accessed.
1246 vm_page_flag_set(fs.m, PG_REFERENCED);
1248 if (curthread->td_lwp) {
1249 if (fs.hardfault) {
1250 curthread->td_lwp->lwp_ru.ru_majflt++;
1251 } else {
1252 curthread->td_lwp->lwp_ru.ru_minflt++;
1257 * Unlock everything, and return the held page.
1259 vm_page_wakeup(fs.m);
1260 /*vm_object_deallocate(fs.first_object);*/
1261 /*fs.first_object = NULL; */
1263 *errorp = 0;
1264 return(fs.m);
1268 * Translate the virtual page number (first_pindex) that is relative
1269 * to the address space into a logical page number that is relative to the
1270 * backing object. Use the virtual page table pointed to by (vpte).
1272 * Possibly downgrade the protection based on the vpte bits.
1274 * This implements an N-level page table. Any level can terminate the
1275 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1276 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1278 static
1280 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1281 vpte_t vpte, int fault_type, int allow_nofault)
1283 struct lwbuf *lwb;
1284 struct lwbuf lwb_cache;
1285 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1286 int result;
1287 vpte_t *ptep;
1289 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1290 for (;;) {
1292 * We cannot proceed if the vpte is not valid, not readable
1293 * for a read fault, or not writable for a write fault.
1295 if ((vpte & VPTE_V) == 0) {
1296 unlock_and_deallocate(fs);
1297 return (KERN_FAILURE);
1299 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) {
1300 unlock_and_deallocate(fs);
1301 return (KERN_FAILURE);
1303 if ((vpte & VPTE_PS) || vshift == 0)
1304 break;
1307 * Get the page table page. Nominally we only read the page
1308 * table, but since we are actively setting VPTE_M and VPTE_A,
1309 * tell vm_fault_object() that we are writing it.
1311 * There is currently no real need to optimize this.
1313 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1314 VM_PROT_READ|VM_PROT_WRITE,
1315 allow_nofault);
1316 if (result != KERN_SUCCESS)
1317 return (result);
1320 * Process the returned fs.m and look up the page table
1321 * entry in the page table page.
1323 vshift -= VPTE_PAGE_BITS;
1324 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1325 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1326 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1327 vm_page_activate(fs->m);
1330 * Page table write-back - entire operation including
1331 * validation of the pte must be atomic to avoid races
1332 * against the vkernel changing the pte.
1334 * If the vpte is valid for the* requested operation, do
1335 * a write-back to the page table.
1337 * XXX VPTE_M is not set properly for page directory pages.
1338 * It doesn't get set in the page directory if the page table
1339 * is modified during a read access.
1341 for (;;) {
1342 vpte_t nvpte;
1345 * Reload for the cmpset, but make sure the pte is
1346 * still valid.
1348 vpte = *ptep;
1349 cpu_ccfence();
1350 nvpte = vpte;
1352 if ((vpte & VPTE_V) == 0)
1353 break;
1355 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW))
1356 nvpte |= VPTE_M | VPTE_A;
1357 if (fault_type & VM_PROT_READ)
1358 nvpte |= VPTE_A;
1359 if (vpte == nvpte)
1360 break;
1361 if (atomic_cmpset_long(ptep, vpte, nvpte)) {
1362 vm_page_dirty(fs->m);
1363 break;
1366 lwbuf_free(lwb);
1367 vm_page_flag_set(fs->m, PG_REFERENCED);
1368 vm_page_wakeup(fs->m);
1369 fs->m = NULL;
1370 cleanup_successful_fault(fs);
1374 * When the vkernel sets VPTE_RW it expects the real kernel to
1375 * reflect VPTE_M back when the page is modified via the mapping.
1376 * In order to accomplish this the real kernel must map the page
1377 * read-only for read faults and use write faults to reflect VPTE_M
1378 * back.
1380 * Once VPTE_M has been set, the real kernel's pte allows writing.
1381 * If the vkernel clears VPTE_M the vkernel must be sure to
1382 * MADV_INVAL the real kernel's mappings to force the real kernel
1383 * to re-fault on the next write so oit can set VPTE_M again.
1385 if ((fault_type & VM_PROT_WRITE) == 0 &&
1386 (vpte & (VPTE_RW | VPTE_M)) != (VPTE_RW | VPTE_M)) {
1387 fs->first_prot &= ~VM_PROT_WRITE;
1391 * Combine remaining address bits with the vpte.
1393 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1394 (*pindex & ((1L << vshift) - 1));
1395 return (KERN_SUCCESS);
1400 * This is the core of the vm_fault code.
1402 * Do all operations required to fault-in (fs.first_object, pindex). Run
1403 * through the shadow chain as necessary and do required COW or virtual
1404 * copy operations. The caller has already fully resolved the vm_map_entry
1405 * and, if appropriate, has created a copy-on-write layer. All we need to
1406 * do is iterate the object chain.
1408 * On failure (fs) is unlocked and deallocated and the caller may return or
1409 * retry depending on the failure code. On success (fs) is NOT unlocked or
1410 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1411 * will have an additional PIP count if it is not equal to fs.first_object.
1413 * If locks based on fs->first_shared or fs->shared are insufficient,
1414 * clear the appropriate field(s) and return RETRY. COWs require that
1415 * first_shared be 0, while page allocations (or frees) require that
1416 * shared be 0. Renames require that both be 0.
1418 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1419 * we will have to retry with it exclusive if the vm_page is
1420 * PG_SWAPPED.
1422 * fs->first_object must be held on call.
1424 static
1426 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex,
1427 vm_prot_t fault_type, int allow_nofault)
1429 vm_object_t next_object;
1430 vm_pindex_t pindex;
1431 int error;
1433 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1434 fs->prot = fs->first_prot;
1435 fs->object = fs->first_object;
1436 pindex = first_pindex;
1438 vm_object_chain_acquire(fs->first_object, fs->shared);
1439 vm_object_pip_add(fs->first_object, 1);
1442 * If a read fault occurs we try to upgrade the page protection
1443 * and make it also writable if possible. There are three cases
1444 * where we cannot make the page mapping writable:
1446 * (1) The mapping is read-only or the VM object is read-only,
1447 * fs->prot above will simply not have VM_PROT_WRITE set.
1449 * (2) If the mapping is a virtual page table fs->first_prot will
1450 * have already been properly adjusted by vm_fault_vpagetable().
1451 * to detect writes so we can set VPTE_M in the virtual page
1452 * table. Used by vkernels.
1454 * (3) If the VM page is read-only or copy-on-write, upgrading would
1455 * just result in an unnecessary COW fault.
1457 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1458 * here.
1460 #if 0
1461 /* see vpagetable code */
1462 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1463 if ((fault_type & VM_PROT_WRITE) == 0)
1464 fs->prot &= ~VM_PROT_WRITE;
1466 #endif
1468 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
1469 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
1470 if ((fault_type & VM_PROT_WRITE) == 0)
1471 fs->prot &= ~VM_PROT_WRITE;
1474 /* vm_object_hold(fs->object); implied b/c object == first_object */
1476 for (;;) {
1478 * The entire backing chain from first_object to object
1479 * inclusive is chainlocked.
1481 * If the object is dead, we stop here
1483 if (fs->object->flags & OBJ_DEAD) {
1484 vm_object_pip_wakeup(fs->first_object);
1485 vm_object_chain_release_all(fs->first_object,
1486 fs->object);
1487 if (fs->object != fs->first_object)
1488 vm_object_drop(fs->object);
1489 unlock_and_deallocate(fs);
1490 return (KERN_PROTECTION_FAILURE);
1494 * See if the page is resident. Wait/Retry if the page is
1495 * busy (lots of stuff may have changed so we can't continue
1496 * in that case).
1498 * We can theoretically allow the soft-busy case on a read
1499 * fault if the page is marked valid, but since such
1500 * pages are typically already pmap'd, putting that
1501 * special case in might be more effort then it is
1502 * worth. We cannot under any circumstances mess
1503 * around with a vm_page_t->busy page except, perhaps,
1504 * to pmap it.
1506 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1507 TRUE, &error);
1508 if (error) {
1509 vm_object_pip_wakeup(fs->first_object);
1510 vm_object_chain_release_all(fs->first_object,
1511 fs->object);
1512 if (fs->object != fs->first_object)
1513 vm_object_drop(fs->object);
1514 unlock_things(fs);
1515 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1516 mycpu->gd_cnt.v_intrans++;
1517 /*vm_object_deallocate(fs->first_object);*/
1518 /*fs->first_object = NULL;*/
1519 fs->m = NULL;
1520 return (KERN_TRY_AGAIN);
1522 if (fs->m) {
1524 * The page is busied for us.
1526 * If reactivating a page from PQ_CACHE we may have
1527 * to rate-limit.
1529 int queue = fs->m->queue;
1530 vm_page_unqueue_nowakeup(fs->m);
1532 if ((queue - fs->m->pc) == PQ_CACHE &&
1533 vm_page_count_severe()) {
1534 vm_page_activate(fs->m);
1535 vm_page_wakeup(fs->m);
1536 fs->m = NULL;
1537 vm_object_pip_wakeup(fs->first_object);
1538 vm_object_chain_release_all(fs->first_object,
1539 fs->object);
1540 if (fs->object != fs->first_object)
1541 vm_object_drop(fs->object);
1542 unlock_and_deallocate(fs);
1543 if (allow_nofault == 0 ||
1544 (curthread->td_flags & TDF_NOFAULT) == 0) {
1545 thread_t td;
1547 vm_wait_pfault();
1548 td = curthread;
1549 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1550 return (KERN_PROTECTION_FAILURE);
1552 return (KERN_TRY_AGAIN);
1556 * If it still isn't completely valid (readable),
1557 * or if a read-ahead-mark is set on the VM page,
1558 * jump to readrest, else we found the page and
1559 * can return.
1561 * We can release the spl once we have marked the
1562 * page busy.
1564 if (fs->m->object != &kernel_object) {
1565 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1566 VM_PAGE_BITS_ALL) {
1567 goto readrest;
1569 if (fs->m->flags & PG_RAM) {
1570 if (debug_cluster)
1571 kprintf("R");
1572 vm_page_flag_clear(fs->m, PG_RAM);
1573 goto readrest;
1576 break; /* break to PAGE HAS BEEN FOUND */
1580 * Page is not resident, If this is the search termination
1581 * or the pager might contain the page, allocate a new page.
1583 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1585 * Allocating, must be exclusive.
1587 if (fs->object == fs->first_object &&
1588 fs->first_shared) {
1589 fs->first_shared = 0;
1590 vm_object_pip_wakeup(fs->first_object);
1591 vm_object_chain_release_all(fs->first_object,
1592 fs->object);
1593 if (fs->object != fs->first_object)
1594 vm_object_drop(fs->object);
1595 unlock_and_deallocate(fs);
1596 return (KERN_TRY_AGAIN);
1598 if (fs->object != fs->first_object &&
1599 fs->shared) {
1600 fs->first_shared = 0;
1601 fs->shared = 0;
1602 vm_object_pip_wakeup(fs->first_object);
1603 vm_object_chain_release_all(fs->first_object,
1604 fs->object);
1605 if (fs->object != fs->first_object)
1606 vm_object_drop(fs->object);
1607 unlock_and_deallocate(fs);
1608 return (KERN_TRY_AGAIN);
1612 * If the page is beyond the object size we fail
1614 if (pindex >= fs->object->size) {
1615 vm_object_pip_wakeup(fs->first_object);
1616 vm_object_chain_release_all(fs->first_object,
1617 fs->object);
1618 if (fs->object != fs->first_object)
1619 vm_object_drop(fs->object);
1620 unlock_and_deallocate(fs);
1621 return (KERN_PROTECTION_FAILURE);
1625 * Allocate a new page for this object/offset pair.
1627 * It is possible for the allocation to race, so
1628 * handle the case.
1630 fs->m = NULL;
1631 if (!vm_page_count_severe()) {
1632 fs->m = vm_page_alloc(fs->object, pindex,
1633 ((fs->vp || fs->object->backing_object) ?
1634 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1635 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1636 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1638 if (fs->m == NULL) {
1639 vm_object_pip_wakeup(fs->first_object);
1640 vm_object_chain_release_all(fs->first_object,
1641 fs->object);
1642 if (fs->object != fs->first_object)
1643 vm_object_drop(fs->object);
1644 unlock_and_deallocate(fs);
1645 if (allow_nofault == 0 ||
1646 (curthread->td_flags & TDF_NOFAULT) == 0) {
1647 thread_t td;
1649 vm_wait_pfault();
1650 td = curthread;
1651 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1652 return (KERN_PROTECTION_FAILURE);
1654 return (KERN_TRY_AGAIN);
1658 * Fall through to readrest. We have a new page which
1659 * will have to be paged (since m->valid will be 0).
1663 readrest:
1665 * We have found an invalid or partially valid page, a
1666 * page with a read-ahead mark which might be partially or
1667 * fully valid (and maybe dirty too), or we have allocated
1668 * a new page.
1670 * Attempt to fault-in the page if there is a chance that the
1671 * pager has it, and potentially fault in additional pages
1672 * at the same time.
1674 * If TRYPAGER is true then fs.m will be non-NULL and busied
1675 * for us.
1677 if (TRYPAGER(fs)) {
1678 int rv;
1679 int seqaccess;
1680 u_char behavior = vm_map_entry_behavior(fs->entry);
1682 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1683 seqaccess = 0;
1684 else
1685 seqaccess = -1;
1688 * Doing I/O may synchronously insert additional
1689 * pages so we can't be shared at this point either.
1691 * NOTE: We can't free fs->m here in the allocated
1692 * case (fs->object != fs->first_object) as
1693 * this would require an exclusively locked
1694 * VM object.
1696 if (fs->object == fs->first_object &&
1697 fs->first_shared) {
1698 vm_page_deactivate(fs->m);
1699 vm_page_wakeup(fs->m);
1700 fs->m = NULL;
1701 fs->first_shared = 0;
1702 vm_object_pip_wakeup(fs->first_object);
1703 vm_object_chain_release_all(fs->first_object,
1704 fs->object);
1705 if (fs->object != fs->first_object)
1706 vm_object_drop(fs->object);
1707 unlock_and_deallocate(fs);
1708 return (KERN_TRY_AGAIN);
1710 if (fs->object != fs->first_object &&
1711 fs->shared) {
1712 vm_page_deactivate(fs->m);
1713 vm_page_wakeup(fs->m);
1714 fs->m = NULL;
1715 fs->first_shared = 0;
1716 fs->shared = 0;
1717 vm_object_pip_wakeup(fs->first_object);
1718 vm_object_chain_release_all(fs->first_object,
1719 fs->object);
1720 if (fs->object != fs->first_object)
1721 vm_object_drop(fs->object);
1722 unlock_and_deallocate(fs);
1723 return (KERN_TRY_AGAIN);
1727 * Avoid deadlocking against the map when doing I/O.
1728 * fs.object and the page is PG_BUSY'd.
1730 * NOTE: Once unlocked, fs->entry can become stale
1731 * so this will NULL it out.
1733 * NOTE: fs->entry is invalid until we relock the
1734 * map and verify that the timestamp has not
1735 * changed.
1737 unlock_map(fs);
1740 * Acquire the page data. We still hold a ref on
1741 * fs.object and the page has been PG_BUSY's.
1743 * The pager may replace the page (for example, in
1744 * order to enter a fictitious page into the
1745 * object). If it does so it is responsible for
1746 * cleaning up the passed page and properly setting
1747 * the new page PG_BUSY.
1749 * If we got here through a PG_RAM read-ahead
1750 * mark the page may be partially dirty and thus
1751 * not freeable. Don't bother checking to see
1752 * if the pager has the page because we can't free
1753 * it anyway. We have to depend on the get_page
1754 * operation filling in any gaps whether there is
1755 * backing store or not.
1757 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1759 if (rv == VM_PAGER_OK) {
1761 * Relookup in case pager changed page. Pager
1762 * is responsible for disposition of old page
1763 * if moved.
1765 * XXX other code segments do relookups too.
1766 * It's a bad abstraction that needs to be
1767 * fixed/removed.
1769 fs->m = vm_page_lookup(fs->object, pindex);
1770 if (fs->m == NULL) {
1771 vm_object_pip_wakeup(fs->first_object);
1772 vm_object_chain_release_all(
1773 fs->first_object, fs->object);
1774 if (fs->object != fs->first_object)
1775 vm_object_drop(fs->object);
1776 unlock_and_deallocate(fs);
1777 return (KERN_TRY_AGAIN);
1779 ++fs->hardfault;
1780 break; /* break to PAGE HAS BEEN FOUND */
1784 * Remove the bogus page (which does not exist at this
1785 * object/offset); before doing so, we must get back
1786 * our object lock to preserve our invariant.
1788 * Also wake up any other process that may want to bring
1789 * in this page.
1791 * If this is the top-level object, we must leave the
1792 * busy page to prevent another process from rushing
1793 * past us, and inserting the page in that object at
1794 * the same time that we are.
1796 if (rv == VM_PAGER_ERROR) {
1797 if (curproc) {
1798 kprintf("vm_fault: pager read error, "
1799 "pid %d (%s)\n",
1800 curproc->p_pid,
1801 curproc->p_comm);
1802 } else {
1803 kprintf("vm_fault: pager read error, "
1804 "thread %p (%s)\n",
1805 curthread,
1806 curproc->p_comm);
1811 * Data outside the range of the pager or an I/O error
1813 * The page may have been wired during the pagein,
1814 * e.g. by the buffer cache, and cannot simply be
1815 * freed. Call vnode_pager_freepage() to deal with it.
1817 * Also note that we cannot free the page if we are
1818 * holding the related object shared. XXX not sure
1819 * what to do in that case.
1821 if (fs->object != fs->first_object) {
1822 vnode_pager_freepage(fs->m);
1823 fs->m = NULL;
1825 * XXX - we cannot just fall out at this
1826 * point, m has been freed and is invalid!
1830 * XXX - the check for kernel_map is a kludge to work
1831 * around having the machine panic on a kernel space
1832 * fault w/ I/O error.
1834 if (((fs->map != &kernel_map) &&
1835 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1836 if (fs->m) {
1837 if (fs->first_shared) {
1838 vm_page_deactivate(fs->m);
1839 vm_page_wakeup(fs->m);
1840 } else {
1841 vnode_pager_freepage(fs->m);
1843 fs->m = NULL;
1845 vm_object_pip_wakeup(fs->first_object);
1846 vm_object_chain_release_all(fs->first_object,
1847 fs->object);
1848 if (fs->object != fs->first_object)
1849 vm_object_drop(fs->object);
1850 unlock_and_deallocate(fs);
1851 if (rv == VM_PAGER_ERROR)
1852 return (KERN_FAILURE);
1853 else
1854 return (KERN_PROTECTION_FAILURE);
1855 /* NOT REACHED */
1860 * We get here if the object has a default pager (or unwiring)
1861 * or the pager doesn't have the page.
1863 * fs->first_m will be used for the COW unless we find a
1864 * deeper page to be mapped read-only, in which case the
1865 * unlock*(fs) will free first_m.
1867 if (fs->object == fs->first_object)
1868 fs->first_m = fs->m;
1871 * Move on to the next object. The chain lock should prevent
1872 * the backing_object from getting ripped out from under us.
1874 * The object lock for the next object is governed by
1875 * fs->shared.
1877 if ((next_object = fs->object->backing_object) != NULL) {
1878 if (fs->shared)
1879 vm_object_hold_shared(next_object);
1880 else
1881 vm_object_hold(next_object);
1882 vm_object_chain_acquire(next_object, fs->shared);
1883 KKASSERT(next_object == fs->object->backing_object);
1884 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1887 if (next_object == NULL) {
1889 * If there's no object left, fill the page in the top
1890 * object with zeros.
1892 if (fs->object != fs->first_object) {
1893 #if 0
1894 if (fs->first_object->backing_object !=
1895 fs->object) {
1896 vm_object_hold(fs->first_object->backing_object);
1898 #endif
1899 vm_object_chain_release_all(
1900 fs->first_object->backing_object,
1901 fs->object);
1902 #if 0
1903 if (fs->first_object->backing_object !=
1904 fs->object) {
1905 vm_object_drop(fs->first_object->backing_object);
1907 #endif
1908 vm_object_pip_wakeup(fs->object);
1909 vm_object_drop(fs->object);
1910 fs->object = fs->first_object;
1911 pindex = first_pindex;
1912 fs->m = fs->first_m;
1914 fs->first_m = NULL;
1917 * Zero the page and mark it valid.
1919 vm_page_zero_fill(fs->m);
1920 mycpu->gd_cnt.v_zfod++;
1921 fs->m->valid = VM_PAGE_BITS_ALL;
1922 break; /* break to PAGE HAS BEEN FOUND */
1924 if (fs->object != fs->first_object) {
1925 vm_object_pip_wakeup(fs->object);
1926 vm_object_lock_swap();
1927 vm_object_drop(fs->object);
1929 KASSERT(fs->object != next_object,
1930 ("object loop %p", next_object));
1931 fs->object = next_object;
1932 vm_object_pip_add(fs->object, 1);
1936 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1937 * is held.]
1939 * object still held.
1941 * local shared variable may be different from fs->shared.
1943 * If the page is being written, but isn't already owned by the
1944 * top-level object, we have to copy it into a new page owned by the
1945 * top-level object.
1947 KASSERT((fs->m->flags & PG_BUSY) != 0,
1948 ("vm_fault: not busy after main loop"));
1950 if (fs->object != fs->first_object) {
1952 * We only really need to copy if we want to write it.
1954 if (fault_type & VM_PROT_WRITE) {
1956 * This allows pages to be virtually copied from a
1957 * backing_object into the first_object, where the
1958 * backing object has no other refs to it, and cannot
1959 * gain any more refs. Instead of a bcopy, we just
1960 * move the page from the backing object to the
1961 * first object. Note that we must mark the page
1962 * dirty in the first object so that it will go out
1963 * to swap when needed.
1965 if (
1967 * Must be holding exclusive locks
1969 fs->first_shared == 0 &&
1970 fs->shared == 0 &&
1972 * Map, if present, has not changed
1974 (fs->map == NULL ||
1975 fs->map_generation == fs->map->timestamp) &&
1977 * Only one shadow object
1979 (fs->object->shadow_count == 1) &&
1981 * No COW refs, except us
1983 (fs->object->ref_count == 1) &&
1985 * No one else can look this object up
1987 (fs->object->handle == NULL) &&
1989 * No other ways to look the object up
1991 ((fs->object->type == OBJT_DEFAULT) ||
1992 (fs->object->type == OBJT_SWAP)) &&
1994 * We don't chase down the shadow chain
1996 (fs->object == fs->first_object->backing_object) &&
1999 * grab the lock if we need to
2001 (fs->lookup_still_valid ||
2002 fs->map == NULL ||
2003 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
2006 * (first_m) and (m) are both busied. We have
2007 * move (m) into (first_m)'s object/pindex
2008 * in an atomic fashion, then free (first_m).
2010 * first_object is held so second remove
2011 * followed by the rename should wind
2012 * up being atomic. vm_page_free() might
2013 * block so we don't do it until after the
2014 * rename.
2016 fs->lookup_still_valid = 1;
2017 vm_page_protect(fs->first_m, VM_PROT_NONE);
2018 vm_page_remove(fs->first_m);
2019 vm_page_rename(fs->m, fs->first_object,
2020 first_pindex);
2021 vm_page_free(fs->first_m);
2022 fs->first_m = fs->m;
2023 fs->m = NULL;
2024 mycpu->gd_cnt.v_cow_optim++;
2025 } else {
2027 * Oh, well, lets copy it.
2029 * Why are we unmapping the original page
2030 * here? Well, in short, not all accessors
2031 * of user memory go through the pmap. The
2032 * procfs code doesn't have access user memory
2033 * via a local pmap, so vm_fault_page*()
2034 * can't call pmap_enter(). And the umtx*()
2035 * code may modify the COW'd page via a DMAP
2036 * or kernel mapping and not via the pmap,
2037 * leaving the original page still mapped
2038 * read-only into the pmap.
2040 * So we have to remove the page from at
2041 * least the current pmap if it is in it.
2042 * Just remove it from all pmaps.
2044 KKASSERT(fs->first_shared == 0);
2045 vm_page_copy(fs->m, fs->first_m);
2046 vm_page_protect(fs->m, VM_PROT_NONE);
2047 vm_page_event(fs->m, VMEVENT_COW);
2051 * We no longer need the old page or object.
2053 if (fs->m)
2054 release_page(fs);
2057 * We intend to revert to first_object, undo the
2058 * chain lock through to that.
2060 #if 0
2061 if (fs->first_object->backing_object != fs->object)
2062 vm_object_hold(fs->first_object->backing_object);
2063 #endif
2064 vm_object_chain_release_all(
2065 fs->first_object->backing_object,
2066 fs->object);
2067 #if 0
2068 if (fs->first_object->backing_object != fs->object)
2069 vm_object_drop(fs->first_object->backing_object);
2070 #endif
2073 * fs->object != fs->first_object due to above
2074 * conditional
2076 vm_object_pip_wakeup(fs->object);
2077 vm_object_drop(fs->object);
2080 * Only use the new page below...
2082 mycpu->gd_cnt.v_cow_faults++;
2083 fs->m = fs->first_m;
2084 fs->object = fs->first_object;
2085 pindex = first_pindex;
2086 } else {
2088 * If it wasn't a write fault avoid having to copy
2089 * the page by mapping it read-only.
2091 fs->prot &= ~VM_PROT_WRITE;
2096 * Relock the map if necessary, then check the generation count.
2097 * relock_map() will update fs->timestamp to account for the
2098 * relocking if necessary.
2100 * If the count has changed after relocking then all sorts of
2101 * crap may have happened and we have to retry.
2103 * NOTE: The relock_map() can fail due to a deadlock against
2104 * the vm_page we are holding BUSY.
2106 if (fs->lookup_still_valid == FALSE && fs->map) {
2107 if (relock_map(fs) ||
2108 fs->map->timestamp != fs->map_generation) {
2109 release_page(fs);
2110 vm_object_pip_wakeup(fs->first_object);
2111 vm_object_chain_release_all(fs->first_object,
2112 fs->object);
2113 if (fs->object != fs->first_object)
2114 vm_object_drop(fs->object);
2115 unlock_and_deallocate(fs);
2116 return (KERN_TRY_AGAIN);
2121 * If the fault is a write, we know that this page is being
2122 * written NOW so dirty it explicitly to save on pmap_is_modified()
2123 * calls later.
2125 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2126 * if the page is already dirty to prevent data written with
2127 * the expectation of being synced from not being synced.
2128 * Likewise if this entry does not request NOSYNC then make
2129 * sure the page isn't marked NOSYNC. Applications sharing
2130 * data should use the same flags to avoid ping ponging.
2132 * Also tell the backing pager, if any, that it should remove
2133 * any swap backing since the page is now dirty.
2135 vm_page_activate(fs->m);
2136 if (fs->prot & VM_PROT_WRITE) {
2137 vm_object_set_writeable_dirty(fs->m->object);
2138 vm_set_nosync(fs->m, fs->entry);
2139 if (fs->fault_flags & VM_FAULT_DIRTY) {
2140 vm_page_dirty(fs->m);
2141 if (fs->m->flags & PG_SWAPPED) {
2143 * If the page is swapped out we have to call
2144 * swap_pager_unswapped() which requires an
2145 * exclusive object lock. If we are shared,
2146 * we must clear the shared flag and retry.
2148 if ((fs->object == fs->first_object &&
2149 fs->first_shared) ||
2150 (fs->object != fs->first_object &&
2151 fs->shared)) {
2152 vm_page_wakeup(fs->m);
2153 fs->m = NULL;
2154 if (fs->object == fs->first_object)
2155 fs->first_shared = 0;
2156 else
2157 fs->shared = 0;
2158 vm_object_pip_wakeup(fs->first_object);
2159 vm_object_chain_release_all(
2160 fs->first_object, fs->object);
2161 if (fs->object != fs->first_object)
2162 vm_object_drop(fs->object);
2163 unlock_and_deallocate(fs);
2164 return (KERN_TRY_AGAIN);
2166 swap_pager_unswapped(fs->m);
2171 vm_object_pip_wakeup(fs->first_object);
2172 vm_object_chain_release_all(fs->first_object, fs->object);
2173 if (fs->object != fs->first_object)
2174 vm_object_drop(fs->object);
2177 * Page had better still be busy. We are still locked up and
2178 * fs->object will have another PIP reference if it is not equal
2179 * to fs->first_object.
2181 KASSERT(fs->m->flags & PG_BUSY,
2182 ("vm_fault: page %p not busy!", fs->m));
2185 * Sanity check: page must be completely valid or it is not fit to
2186 * map into user space. vm_pager_get_pages() ensures this.
2188 if (fs->m->valid != VM_PAGE_BITS_ALL) {
2189 vm_page_zero_invalid(fs->m, TRUE);
2190 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
2193 return (KERN_SUCCESS);
2197 * Wire down a range of virtual addresses in a map. The entry in question
2198 * should be marked in-transition and the map must be locked. We must
2199 * release the map temporarily while faulting-in the page to avoid a
2200 * deadlock. Note that the entry may be clipped while we are blocked but
2201 * will never be freed.
2203 * No requirements.
2206 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2207 boolean_t user_wire, int kmflags)
2209 boolean_t fictitious;
2210 vm_offset_t start;
2211 vm_offset_t end;
2212 vm_offset_t va;
2213 pmap_t pmap;
2214 int rv;
2215 int wire_prot;
2216 int fault_flags;
2217 vm_page_t m;
2219 lwkt_gettoken(&map->token);
2221 if (user_wire) {
2222 wire_prot = VM_PROT_READ;
2223 fault_flags = VM_FAULT_USER_WIRE;
2224 } else {
2225 wire_prot = VM_PROT_READ | VM_PROT_WRITE;
2226 fault_flags = VM_FAULT_CHANGE_WIRING;
2228 if (kmflags & KM_NOTLBSYNC)
2229 wire_prot |= VM_PROT_NOSYNC;
2231 pmap = vm_map_pmap(map);
2232 start = entry->start;
2233 end = entry->end;
2234 switch(entry->maptype) {
2235 case VM_MAPTYPE_NORMAL:
2236 case VM_MAPTYPE_VPAGETABLE:
2237 fictitious = entry->object.vm_object &&
2238 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2239 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2240 break;
2241 case VM_MAPTYPE_UKSMAP:
2242 fictitious = TRUE;
2243 break;
2244 default:
2245 fictitious = FALSE;
2246 break;
2249 if (entry->eflags & MAP_ENTRY_KSTACK)
2250 start += PAGE_SIZE;
2251 map->timestamp++;
2252 vm_map_unlock(map);
2255 * We simulate a fault to get the page and enter it in the physical
2256 * map.
2258 for (va = start; va < end; va += PAGE_SIZE) {
2259 rv = vm_fault(map, va, wire_prot, fault_flags);
2260 if (rv) {
2261 while (va > start) {
2262 va -= PAGE_SIZE;
2263 m = pmap_unwire(pmap, va);
2264 if (m && !fictitious) {
2265 vm_page_busy_wait(m, FALSE, "vmwrpg");
2266 vm_page_unwire(m, 1);
2267 vm_page_wakeup(m);
2270 goto done;
2273 rv = KERN_SUCCESS;
2274 done:
2275 vm_map_lock(map);
2276 lwkt_reltoken(&map->token);
2277 return (rv);
2281 * Unwire a range of virtual addresses in a map. The map should be
2282 * locked.
2284 void
2285 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2287 boolean_t fictitious;
2288 vm_offset_t start;
2289 vm_offset_t end;
2290 vm_offset_t va;
2291 pmap_t pmap;
2292 vm_page_t m;
2294 lwkt_gettoken(&map->token);
2296 pmap = vm_map_pmap(map);
2297 start = entry->start;
2298 end = entry->end;
2299 fictitious = entry->object.vm_object &&
2300 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2301 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2302 if (entry->eflags & MAP_ENTRY_KSTACK)
2303 start += PAGE_SIZE;
2306 * Since the pages are wired down, we must be able to get their
2307 * mappings from the physical map system.
2309 for (va = start; va < end; va += PAGE_SIZE) {
2310 m = pmap_unwire(pmap, va);
2311 if (m && !fictitious) {
2312 vm_page_busy_wait(m, FALSE, "vmwrpg");
2313 vm_page_unwire(m, 1);
2314 vm_page_wakeup(m);
2317 lwkt_reltoken(&map->token);
2321 * Copy all of the pages from a wired-down map entry to another.
2323 * The source and destination maps must be locked for write.
2324 * The source and destination maps token must be held
2325 * The source map entry must be wired down (or be a sharing map
2326 * entry corresponding to a main map entry that is wired down).
2328 * No other requirements.
2330 * XXX do segment optimization
2332 void
2333 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
2334 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
2336 vm_object_t dst_object;
2337 vm_object_t src_object;
2338 vm_ooffset_t dst_offset;
2339 vm_ooffset_t src_offset;
2340 vm_prot_t prot;
2341 vm_offset_t vaddr;
2342 vm_page_t dst_m;
2343 vm_page_t src_m;
2345 src_object = src_entry->object.vm_object;
2346 src_offset = src_entry->offset;
2349 * Create the top-level object for the destination entry. (Doesn't
2350 * actually shadow anything - we copy the pages directly.)
2352 vm_map_entry_allocate_object(dst_entry);
2353 dst_object = dst_entry->object.vm_object;
2355 prot = dst_entry->max_protection;
2358 * Loop through all of the pages in the entry's range, copying each
2359 * one from the source object (it should be there) to the destination
2360 * object.
2362 vm_object_hold(src_object);
2363 vm_object_hold(dst_object);
2364 for (vaddr = dst_entry->start, dst_offset = 0;
2365 vaddr < dst_entry->end;
2366 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
2369 * Allocate a page in the destination object
2371 do {
2372 dst_m = vm_page_alloc(dst_object,
2373 OFF_TO_IDX(dst_offset),
2374 VM_ALLOC_NORMAL);
2375 if (dst_m == NULL) {
2376 vm_wait(0);
2378 } while (dst_m == NULL);
2381 * Find the page in the source object, and copy it in.
2382 * (Because the source is wired down, the page will be in
2383 * memory.)
2385 src_m = vm_page_lookup(src_object,
2386 OFF_TO_IDX(dst_offset + src_offset));
2387 if (src_m == NULL)
2388 panic("vm_fault_copy_wired: page missing");
2390 vm_page_copy(src_m, dst_m);
2391 vm_page_event(src_m, VMEVENT_COW);
2394 * Enter it in the pmap...
2396 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
2399 * Mark it no longer busy, and put it on the active list.
2401 vm_page_activate(dst_m);
2402 vm_page_wakeup(dst_m);
2404 vm_object_drop(dst_object);
2405 vm_object_drop(src_object);
2408 #if 0
2411 * This routine checks around the requested page for other pages that
2412 * might be able to be faulted in. This routine brackets the viable
2413 * pages for the pages to be paged in.
2415 * Inputs:
2416 * m, rbehind, rahead
2418 * Outputs:
2419 * marray (array of vm_page_t), reqpage (index of requested page)
2421 * Return value:
2422 * number of pages in marray
2424 static int
2425 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2426 vm_page_t *marray, int *reqpage)
2428 int i,j;
2429 vm_object_t object;
2430 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2431 vm_page_t rtm;
2432 int cbehind, cahead;
2434 object = m->object;
2435 pindex = m->pindex;
2438 * we don't fault-ahead for device pager
2440 if ((object->type == OBJT_DEVICE) ||
2441 (object->type == OBJT_MGTDEVICE)) {
2442 *reqpage = 0;
2443 marray[0] = m;
2444 return 1;
2448 * if the requested page is not available, then give up now
2450 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2451 *reqpage = 0; /* not used by caller, fix compiler warn */
2452 return 0;
2455 if ((cbehind == 0) && (cahead == 0)) {
2456 *reqpage = 0;
2457 marray[0] = m;
2458 return 1;
2461 if (rahead > cahead) {
2462 rahead = cahead;
2465 if (rbehind > cbehind) {
2466 rbehind = cbehind;
2470 * Do not do any readahead if we have insufficient free memory.
2472 * XXX code was broken disabled before and has instability
2473 * with this conditonal fixed, so shortcut for now.
2475 if (burst_fault == 0 || vm_page_count_severe()) {
2476 marray[0] = m;
2477 *reqpage = 0;
2478 return 1;
2482 * scan backward for the read behind pages -- in memory
2484 * Assume that if the page is not found an interrupt will not
2485 * create it. Theoretically interrupts can only remove (busy)
2486 * pages, not create new associations.
2488 if (pindex > 0) {
2489 if (rbehind > pindex) {
2490 rbehind = pindex;
2491 startpindex = 0;
2492 } else {
2493 startpindex = pindex - rbehind;
2496 vm_object_hold(object);
2497 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2498 if (vm_page_lookup(object, tpindex - 1))
2499 break;
2502 i = 0;
2503 while (tpindex < pindex) {
2504 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2505 VM_ALLOC_NULL_OK);
2506 if (rtm == NULL) {
2507 for (j = 0; j < i; j++) {
2508 vm_page_free(marray[j]);
2510 vm_object_drop(object);
2511 marray[0] = m;
2512 *reqpage = 0;
2513 return 1;
2515 marray[i] = rtm;
2516 ++i;
2517 ++tpindex;
2519 vm_object_drop(object);
2520 } else {
2521 i = 0;
2525 * Assign requested page
2527 marray[i] = m;
2528 *reqpage = i;
2529 ++i;
2532 * Scan forwards for read-ahead pages
2534 tpindex = pindex + 1;
2535 endpindex = tpindex + rahead;
2536 if (endpindex > object->size)
2537 endpindex = object->size;
2539 vm_object_hold(object);
2540 while (tpindex < endpindex) {
2541 if (vm_page_lookup(object, tpindex))
2542 break;
2543 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2544 VM_ALLOC_NULL_OK);
2545 if (rtm == NULL)
2546 break;
2547 marray[i] = rtm;
2548 ++i;
2549 ++tpindex;
2551 vm_object_drop(object);
2553 return (i);
2556 #endif
2559 * vm_prefault() provides a quick way of clustering pagefaults into a
2560 * processes address space. It is a "cousin" of pmap_object_init_pt,
2561 * except it runs at page fault time instead of mmap time.
2563 * vm.fast_fault Enables pre-faulting zero-fill pages
2565 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2566 * prefault. Scan stops in either direction when
2567 * a page is found to already exist.
2569 * This code used to be per-platform pmap_prefault(). It is now
2570 * machine-independent and enhanced to also pre-fault zero-fill pages
2571 * (see vm.fast_fault) as well as make them writable, which greatly
2572 * reduces the number of page faults programs incur.
2574 * Application performance when pre-faulting zero-fill pages is heavily
2575 * dependent on the application. Very tiny applications like /bin/echo
2576 * lose a little performance while applications of any appreciable size
2577 * gain performance. Prefaulting multiple pages also reduces SMP
2578 * congestion and can improve SMP performance significantly.
2580 * NOTE! prot may allow writing but this only applies to the top level
2581 * object. If we wind up mapping a page extracted from a backing
2582 * object we have to make sure it is read-only.
2584 * NOTE! The caller has already handled any COW operations on the
2585 * vm_map_entry via the normal fault code. Do NOT call this
2586 * shortcut unless the normal fault code has run on this entry.
2588 * The related map must be locked.
2589 * No other requirements.
2591 static int vm_prefault_pages = 8;
2592 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2593 "Maximum number of pages to pre-fault");
2594 static int vm_fast_fault = 1;
2595 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2596 "Burst fault zero-fill regions");
2599 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2600 * is not already dirty by other means. This will prevent passive
2601 * filesystem syncing as well as 'sync' from writing out the page.
2603 static void
2604 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2606 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2607 if (m->dirty == 0)
2608 vm_page_flag_set(m, PG_NOSYNC);
2609 } else {
2610 vm_page_flag_clear(m, PG_NOSYNC);
2614 static void
2615 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2616 int fault_flags)
2618 struct lwp *lp;
2619 vm_page_t m;
2620 vm_offset_t addr;
2621 vm_pindex_t index;
2622 vm_pindex_t pindex;
2623 vm_object_t object;
2624 int pprot;
2625 int i;
2626 int noneg;
2627 int nopos;
2628 int maxpages;
2631 * Get stable max count value, disabled if set to 0
2633 maxpages = vm_prefault_pages;
2634 cpu_ccfence();
2635 if (maxpages <= 0)
2636 return;
2639 * We do not currently prefault mappings that use virtual page
2640 * tables. We do not prefault foreign pmaps.
2642 if (entry->maptype != VM_MAPTYPE_NORMAL)
2643 return;
2644 lp = curthread->td_lwp;
2645 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2646 return;
2649 * Limit pre-fault count to 1024 pages.
2651 if (maxpages > 1024)
2652 maxpages = 1024;
2654 object = entry->object.vm_object;
2655 KKASSERT(object != NULL);
2656 KKASSERT(object == entry->object.vm_object);
2659 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2660 * now (or do something more complex XXX).
2662 vm_object_hold(object);
2663 vm_object_chain_acquire(object, 0);
2665 noneg = 0;
2666 nopos = 0;
2667 for (i = 0; i < maxpages; ++i) {
2668 vm_object_t lobject;
2669 vm_object_t nobject;
2670 int allocated = 0;
2671 int error;
2674 * This can eat a lot of time on a heavily contended
2675 * machine so yield on the tick if needed.
2677 if ((i & 7) == 7)
2678 lwkt_yield();
2681 * Calculate the page to pre-fault, stopping the scan in
2682 * each direction separately if the limit is reached.
2684 if (i & 1) {
2685 if (noneg)
2686 continue;
2687 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2688 } else {
2689 if (nopos)
2690 continue;
2691 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2693 if (addr < entry->start) {
2694 noneg = 1;
2695 if (noneg && nopos)
2696 break;
2697 continue;
2699 if (addr >= entry->end) {
2700 nopos = 1;
2701 if (noneg && nopos)
2702 break;
2703 continue;
2707 * Skip pages already mapped, and stop scanning in that
2708 * direction. When the scan terminates in both directions
2709 * we are done.
2711 if (pmap_prefault_ok(pmap, addr) == 0) {
2712 if (i & 1)
2713 noneg = 1;
2714 else
2715 nopos = 1;
2716 if (noneg && nopos)
2717 break;
2718 continue;
2722 * Follow the VM object chain to obtain the page to be mapped
2723 * into the pmap.
2725 * If we reach the terminal object without finding a page
2726 * and we determine it would be advantageous, then allocate
2727 * a zero-fill page for the base object. The base object
2728 * is guaranteed to be OBJT_DEFAULT for this case.
2730 * In order to not have to check the pager via *haspage*()
2731 * we stop if any non-default object is encountered. e.g.
2732 * a vnode or swap object would stop the loop.
2734 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2735 lobject = object;
2736 pindex = index;
2737 pprot = prot;
2739 KKASSERT(lobject == entry->object.vm_object);
2740 /*vm_object_hold(lobject); implied */
2742 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2743 TRUE, &error)) == NULL) {
2744 if (lobject->type != OBJT_DEFAULT)
2745 break;
2746 if (lobject->backing_object == NULL) {
2747 if (vm_fast_fault == 0)
2748 break;
2749 if ((prot & VM_PROT_WRITE) == 0 ||
2750 vm_page_count_min(0)) {
2751 break;
2755 * NOTE: Allocated from base object
2757 m = vm_page_alloc(object, index,
2758 VM_ALLOC_NORMAL |
2759 VM_ALLOC_ZERO |
2760 VM_ALLOC_USE_GD |
2761 VM_ALLOC_NULL_OK);
2762 if (m == NULL)
2763 break;
2764 allocated = 1;
2765 pprot = prot;
2766 /* lobject = object .. not needed */
2767 break;
2769 if (lobject->backing_object_offset & PAGE_MASK)
2770 break;
2771 nobject = lobject->backing_object;
2772 vm_object_hold(nobject);
2773 KKASSERT(nobject == lobject->backing_object);
2774 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2775 if (lobject != object) {
2776 vm_object_lock_swap();
2777 vm_object_drop(lobject);
2779 lobject = nobject;
2780 pprot &= ~VM_PROT_WRITE;
2781 vm_object_chain_acquire(lobject, 0);
2785 * NOTE: A non-NULL (m) will be associated with lobject if
2786 * it was found there, otherwise it is probably a
2787 * zero-fill page associated with the base object.
2789 * Give-up if no page is available.
2791 if (m == NULL) {
2792 if (lobject != object) {
2793 #if 0
2794 if (object->backing_object != lobject)
2795 vm_object_hold(object->backing_object);
2796 #endif
2797 vm_object_chain_release_all(
2798 object->backing_object, lobject);
2799 #if 0
2800 if (object->backing_object != lobject)
2801 vm_object_drop(object->backing_object);
2802 #endif
2803 vm_object_drop(lobject);
2805 break;
2809 * The object must be marked dirty if we are mapping a
2810 * writable page. m->object is either lobject or object,
2811 * both of which are still held. Do this before we
2812 * potentially drop the object.
2814 if (pprot & VM_PROT_WRITE)
2815 vm_object_set_writeable_dirty(m->object);
2818 * Do not conditionalize on PG_RAM. If pages are present in
2819 * the VM system we assume optimal caching. If caching is
2820 * not optimal the I/O gravy train will be restarted when we
2821 * hit an unavailable page. We do not want to try to restart
2822 * the gravy train now because we really don't know how much
2823 * of the object has been cached. The cost for restarting
2824 * the gravy train should be low (since accesses will likely
2825 * be I/O bound anyway).
2827 if (lobject != object) {
2828 #if 0
2829 if (object->backing_object != lobject)
2830 vm_object_hold(object->backing_object);
2831 #endif
2832 vm_object_chain_release_all(object->backing_object,
2833 lobject);
2834 #if 0
2835 if (object->backing_object != lobject)
2836 vm_object_drop(object->backing_object);
2837 #endif
2838 vm_object_drop(lobject);
2842 * Enter the page into the pmap if appropriate. If we had
2843 * allocated the page we have to place it on a queue. If not
2844 * we just have to make sure it isn't on the cache queue
2845 * (pages on the cache queue are not allowed to be mapped).
2847 if (allocated) {
2849 * Page must be zerod.
2851 vm_page_zero_fill(m);
2852 mycpu->gd_cnt.v_zfod++;
2853 m->valid = VM_PAGE_BITS_ALL;
2856 * Handle dirty page case
2858 if (pprot & VM_PROT_WRITE)
2859 vm_set_nosync(m, entry);
2860 pmap_enter(pmap, addr, m, pprot, 0, entry);
2861 mycpu->gd_cnt.v_vm_faults++;
2862 if (curthread->td_lwp)
2863 ++curthread->td_lwp->lwp_ru.ru_minflt;
2864 vm_page_deactivate(m);
2865 if (pprot & VM_PROT_WRITE) {
2866 /*vm_object_set_writeable_dirty(m->object);*/
2867 vm_set_nosync(m, entry);
2868 if (fault_flags & VM_FAULT_DIRTY) {
2869 vm_page_dirty(m);
2870 /*XXX*/
2871 swap_pager_unswapped(m);
2874 vm_page_wakeup(m);
2875 } else if (error) {
2876 /* couldn't busy page, no wakeup */
2877 } else if (
2878 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2879 (m->flags & PG_FICTITIOUS) == 0) {
2881 * A fully valid page not undergoing soft I/O can
2882 * be immediately entered into the pmap.
2884 if ((m->queue - m->pc) == PQ_CACHE)
2885 vm_page_deactivate(m);
2886 if (pprot & VM_PROT_WRITE) {
2887 /*vm_object_set_writeable_dirty(m->object);*/
2888 vm_set_nosync(m, entry);
2889 if (fault_flags & VM_FAULT_DIRTY) {
2890 vm_page_dirty(m);
2891 /*XXX*/
2892 swap_pager_unswapped(m);
2895 if (pprot & VM_PROT_WRITE)
2896 vm_set_nosync(m, entry);
2897 pmap_enter(pmap, addr, m, pprot, 0, entry);
2898 mycpu->gd_cnt.v_vm_faults++;
2899 if (curthread->td_lwp)
2900 ++curthread->td_lwp->lwp_ru.ru_minflt;
2901 vm_page_wakeup(m);
2902 } else {
2903 vm_page_wakeup(m);
2906 vm_object_chain_release(object);
2907 vm_object_drop(object);
2911 * Object can be held shared
2913 static void
2914 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2915 vm_map_entry_t entry, int prot, int fault_flags)
2917 struct lwp *lp;
2918 vm_page_t m;
2919 vm_offset_t addr;
2920 vm_pindex_t pindex;
2921 vm_object_t object;
2922 int i;
2923 int noneg;
2924 int nopos;
2925 int maxpages;
2928 * Get stable max count value, disabled if set to 0
2930 maxpages = vm_prefault_pages;
2931 cpu_ccfence();
2932 if (maxpages <= 0)
2933 return;
2936 * We do not currently prefault mappings that use virtual page
2937 * tables. We do not prefault foreign pmaps.
2939 if (entry->maptype != VM_MAPTYPE_NORMAL)
2940 return;
2941 lp = curthread->td_lwp;
2942 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2943 return;
2944 object = entry->object.vm_object;
2945 if (object->backing_object != NULL)
2946 return;
2947 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2950 * Limit pre-fault count to 1024 pages.
2952 if (maxpages > 1024)
2953 maxpages = 1024;
2955 noneg = 0;
2956 nopos = 0;
2957 for (i = 0; i < maxpages; ++i) {
2958 int error;
2961 * Calculate the page to pre-fault, stopping the scan in
2962 * each direction separately if the limit is reached.
2964 if (i & 1) {
2965 if (noneg)
2966 continue;
2967 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2968 } else {
2969 if (nopos)
2970 continue;
2971 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2973 if (addr < entry->start) {
2974 noneg = 1;
2975 if (noneg && nopos)
2976 break;
2977 continue;
2979 if (addr >= entry->end) {
2980 nopos = 1;
2981 if (noneg && nopos)
2982 break;
2983 continue;
2987 * Follow the VM object chain to obtain the page to be mapped
2988 * into the pmap. This version of the prefault code only
2989 * works with terminal objects.
2991 * The page must already exist. If we encounter a problem
2992 * we stop here.
2994 * WARNING! We cannot call swap_pager_unswapped() or insert
2995 * a new vm_page with a shared token.
2997 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2999 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
3000 if (m == NULL || error)
3001 break;
3004 * Skip pages already mapped, and stop scanning in that
3005 * direction. When the scan terminates in both directions
3006 * we are done.
3008 if (pmap_prefault_ok(pmap, addr) == 0) {
3009 vm_page_wakeup(m);
3010 if (i & 1)
3011 noneg = 1;
3012 else
3013 nopos = 1;
3014 if (noneg && nopos)
3015 break;
3016 continue;
3020 * Stop if the page cannot be trivially entered into the
3021 * pmap.
3023 if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) ||
3024 (m->flags & PG_FICTITIOUS) ||
3025 ((m->flags & PG_SWAPPED) &&
3026 (prot & VM_PROT_WRITE) &&
3027 (fault_flags & VM_FAULT_DIRTY))) {
3028 vm_page_wakeup(m);
3029 break;
3033 * Enter the page into the pmap. The object might be held
3034 * shared so we can't do any (serious) modifying operation
3035 * on it.
3037 if ((m->queue - m->pc) == PQ_CACHE)
3038 vm_page_deactivate(m);
3039 if (prot & VM_PROT_WRITE) {
3040 vm_object_set_writeable_dirty(m->object);
3041 vm_set_nosync(m, entry);
3042 if (fault_flags & VM_FAULT_DIRTY) {
3043 vm_page_dirty(m);
3044 /* can't happeen due to conditional above */
3045 /* swap_pager_unswapped(m); */
3048 pmap_enter(pmap, addr, m, prot, 0, entry);
3049 mycpu->gd_cnt.v_vm_faults++;
3050 if (curthread->td_lwp)
3051 ++curthread->td_lwp->lwp_ru.ru_minflt;
3052 vm_page_wakeup(m);