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
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
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
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
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
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>
117 #include <vm/vm_param.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 <vm/vm_page2.h>
136 vm_object_t first_object;
137 vm_prot_t first_prot;
139 vm_map_entry_t entry;
140 int lookup_still_valid;
150 static int debug_fault = 0;
151 SYSCTL_INT(_vm, OID_AUTO, debug_fault, CTLFLAG_RW, &debug_fault, 0, "");
152 static int debug_cluster = 0;
153 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
154 static int virtual_copy_enable = 1;
155 SYSCTL_INT(_vm, OID_AUTO, virtual_copy_enable, CTLFLAG_RW,
156 &virtual_copy_enable, 0, "");
157 int vm_shared_fault = 1;
158 TUNABLE_INT("vm.shared_fault", &vm_shared_fault);
159 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW,
160 &vm_shared_fault, 0, "Allow shared token on vm_object");
162 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t, int);
163 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *,
166 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
168 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
169 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
170 vm_map_entry_t entry, int prot, int fault_flags);
171 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
172 vm_map_entry_t entry, int prot, int fault_flags);
175 release_page(struct faultstate *fs)
177 vm_page_deactivate(fs->m);
178 vm_page_wakeup(fs->m);
183 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
184 * requires relocking and then checking the timestamp.
186 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
187 * not have to update fs->map_generation here.
189 * NOTE: This function can fail due to a deadlock against the caller's
190 * holding of a vm_page BUSY.
193 relock_map(struct faultstate *fs)
197 if (fs->lookup_still_valid == FALSE && fs->map) {
198 error = vm_map_lock_read_to(fs->map);
200 fs->lookup_still_valid = TRUE;
208 unlock_map(struct faultstate *fs)
210 if (fs->lookup_still_valid && fs->map) {
211 vm_map_lookup_done(fs->map, fs->entry, 0);
212 fs->lookup_still_valid = FALSE;
217 * Clean up after a successful call to vm_fault_object() so another call
218 * to vm_fault_object() can be made.
221 _cleanup_successful_fault(struct faultstate *fs, int relock)
224 * We allocated a junk page for a COW operation that did
225 * not occur, the page must be freed.
227 if (fs->object != fs->first_object) {
228 KKASSERT(fs->first_shared == 0);
229 vm_page_free(fs->first_m);
230 vm_object_pip_wakeup(fs->object);
237 fs->object = fs->first_object;
238 if (relock && fs->lookup_still_valid == FALSE) {
240 vm_map_lock_read(fs->map);
241 fs->lookup_still_valid = TRUE;
246 _unlock_things(struct faultstate *fs, int dealloc)
248 _cleanup_successful_fault(fs, 0);
250 /*vm_object_deallocate(fs->first_object);*/
251 /*fs->first_object = NULL; drop used later on */
254 if (fs->vp != NULL) {
260 #define unlock_things(fs) _unlock_things(fs, 0)
261 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
262 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
265 * Virtual copy tests. Used by the fault code to determine if a
266 * page can be moved from an orphan vm_object into its shadow
267 * instead of copying its contents.
270 virtual_copy_test(struct faultstate *fs)
273 * Must be holding exclusive locks
275 if (fs->first_shared || fs->shared || virtual_copy_enable == 0)
279 * Map, if present, has not changed
281 if (fs->map && fs->map_generation != fs->map->timestamp)
285 * Only one shadow object
287 if (fs->object->shadow_count != 1)
291 * No COW refs, except us
293 if (fs->object->ref_count != 1)
297 * No one else can look this object up
299 if (fs->object->handle != NULL)
303 * No other ways to look the object up
305 if (fs->object->type != OBJT_DEFAULT &&
306 fs->object->type != OBJT_SWAP)
310 * We don't chase down the shadow chain
312 if (fs->object != fs->first_object->backing_object)
319 virtual_copy_ok(struct faultstate *fs)
321 if (virtual_copy_test(fs)) {
323 * Grab the lock and re-test changeable items.
325 if (fs->lookup_still_valid == FALSE && fs->map) {
326 if (lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT))
328 fs->lookup_still_valid = TRUE;
329 if (virtual_copy_test(fs)) {
330 fs->map_generation = ++fs->map->timestamp;
333 fs->lookup_still_valid = FALSE;
334 lockmgr(&fs->map->lock, LK_RELEASE);
343 * Determine if the pager for the current object *might* contain the page.
345 * We only need to try the pager if this is not a default object (default
346 * objects are zero-fill and have no real pager), and if we are not taking
347 * a wiring fault or if the FS entry is wired.
349 #define TRYPAGER(fs) \
350 (fs->object->type != OBJT_DEFAULT && \
351 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || \
352 (fs->wflags & FW_WIRED)))
357 * Handle a page fault occuring at the given address, requiring the given
358 * permissions, in the map specified. If successful, the page is inserted
359 * into the associated physical map.
361 * NOTE: The given address should be truncated to the proper page address.
363 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
364 * a standard error specifying why the fault is fatal is returned.
366 * The map in question must be referenced, and remains so.
367 * The caller may hold no locks.
368 * No other requirements.
371 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
374 vm_pindex_t first_pindex;
375 struct faultstate fs;
379 struct vm_map_ilock ilock;
385 inherit_prot = fault_type & VM_PROT_NOSYNC;
387 fs.fault_flags = fault_flags;
389 fs.shared = vm_shared_fault;
390 fs.first_shared = vm_shared_fault;
394 * vm_map interactions
397 if ((lp = td->td_lwp) != NULL)
398 lp->lwp_flags |= LWP_PAGING;
402 * Find the vm_map_entry representing the backing store and resolve
403 * the top level object and page index. This may have the side
404 * effect of executing a copy-on-write on the map entry,
405 * creating a shadow object, or splitting an anonymous entry for
406 * performance, but will not COW any actual VM pages.
408 * On success fs.map is left read-locked and various other fields
409 * are initialized but not otherwise referenced or locked.
411 * NOTE! vm_map_lookup will try to upgrade the fault_type to
412 * VM_FAULT_WRITE if the map entry is a virtual page table
413 * and also writable, so we can set the 'A'accessed bit in
414 * the virtual page table entry.
417 result = vm_map_lookup(&fs.map, vaddr, fault_type,
418 &fs.entry, &fs.first_object,
419 &first_pindex, &fs.first_prot, &fs.wflags);
422 * If the lookup failed or the map protections are incompatible,
423 * the fault generally fails.
425 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
426 * tried to do a COW fault.
428 * If the caller is trying to do a user wiring we have more work
431 if (result != KERN_SUCCESS) {
432 if (result == KERN_FAILURE_NOFAULT) {
433 result = KERN_FAILURE;
436 if (result != KERN_PROTECTION_FAILURE ||
437 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
439 if (result == KERN_INVALID_ADDRESS && growstack &&
440 map != &kernel_map && curproc != NULL) {
441 result = vm_map_growstack(map, vaddr);
442 if (result == KERN_SUCCESS) {
447 result = KERN_FAILURE;
453 * If we are user-wiring a r/w segment, and it is COW, then
454 * we need to do the COW operation. Note that we don't
455 * currently COW RO sections now, because it is NOT desirable
456 * to COW .text. We simply keep .text from ever being COW'ed
457 * and take the heat that one cannot debug wired .text sections.
459 * XXX Try to allow the above by specifying OVERRIDE_WRITE.
461 result = vm_map_lookup(&fs.map, vaddr,
462 VM_PROT_READ|VM_PROT_WRITE|
463 VM_PROT_OVERRIDE_WRITE,
464 &fs.entry, &fs.first_object,
465 &first_pindex, &fs.first_prot,
467 if (result != KERN_SUCCESS) {
468 /* could also be KERN_FAILURE_NOFAULT */
469 result = KERN_FAILURE;
474 * If we don't COW now, on a user wire, the user will never
475 * be able to write to the mapping. If we don't make this
476 * restriction, the bookkeeping would be nearly impossible.
478 * XXX We have a shared lock, this will have a MP race but
479 * I don't see how it can hurt anything.
481 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
482 atomic_clear_char(&fs.entry->max_protection,
488 * fs.map is read-locked
490 * Misc checks. Save the map generation number to detect races.
492 fs.map_generation = fs.map->timestamp;
493 fs.lookup_still_valid = TRUE;
495 fs.object = fs.first_object; /* so unlock_and_deallocate works */
496 fs.prot = fs.first_prot; /* default (used by uksmap) */
498 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
499 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
500 panic("vm_fault: fault on nofault entry, addr: %p",
503 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
504 vaddr >= fs.entry->start &&
505 vaddr < fs.entry->start + PAGE_SIZE) {
506 panic("vm_fault: fault on stack guard, addr: %p",
512 * A user-kernel shared map has no VM object and bypasses
513 * everything. We execute the uksmap function with a temporary
514 * fictitious vm_page. The address is directly mapped with no
517 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
518 struct vm_page fakem;
520 bzero(&fakem, sizeof(fakem));
521 fakem.pindex = first_pindex;
522 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
523 fakem.busy_count = PBUSY_LOCKED;
524 fakem.valid = VM_PAGE_BITS_ALL;
525 fakem.pat_mode = VM_MEMATTR_DEFAULT;
526 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
527 result = KERN_FAILURE;
531 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
532 (fs.wflags & FW_WIRED), fs.entry);
537 * A system map entry may return a NULL object. No object means
538 * no pager means an unrecoverable kernel fault.
540 if (fs.first_object == NULL) {
541 panic("vm_fault: unrecoverable fault at %p in entry %p",
542 (void *)vaddr, fs.entry);
546 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
549 * Unfortunately a deadlock can occur if we are forced to page-in
550 * from swap, but diving all the way into the vm_pager_get_page()
551 * function to find out is too much. Just check the object type.
553 * The deadlock is a CAM deadlock on a busy VM page when trying
554 * to finish an I/O if another process gets stuck in
555 * vop_helper_read_shortcut() due to a swap fault.
557 if ((td->td_flags & TDF_NOFAULT) &&
559 fs.first_object->type == OBJT_VNODE ||
560 fs.first_object->type == OBJT_SWAP ||
561 fs.first_object->backing_object)) {
562 result = KERN_FAILURE;
568 * If the entry is wired we cannot change the page protection.
570 if (fs.wflags & FW_WIRED)
571 fault_type = fs.first_prot;
574 * We generally want to avoid unnecessary exclusive modes on backing
575 * and terminal objects because this can seriously interfere with
576 * heavily fork()'d processes (particularly /bin/sh scripts).
578 * However, we also want to avoid unnecessary retries due to needed
579 * shared->exclusive promotion for common faults. Exclusive mode is
580 * always needed if any page insertion, rename, or free occurs in an
581 * object (and also indirectly if any I/O is done).
583 * The main issue here is going to be fs.first_shared. If the
584 * first_object has a backing object which isn't shadowed and the
585 * process is single-threaded we might as well use an exclusive
586 * lock/chain right off the bat.
588 if (fs.first_shared && fs.first_object->backing_object &&
589 LIST_EMPTY(&fs.first_object->shadow_head) &&
590 td->td_proc && td->td_proc->p_nthreads == 1) {
595 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
596 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
597 * we can try shared first.
599 if (fault_flags & VM_FAULT_UNSWAP) {
604 * Obtain a top-level object lock, shared or exclusive depending
605 * on fs.first_shared. If a shared lock winds up being insufficient
606 * we will retry with an exclusive lock.
608 * The vnode pager lock is always shared.
611 vm_object_hold_shared(fs.first_object);
613 vm_object_hold(fs.first_object);
615 fs.vp = vnode_pager_lock(fs.first_object);
618 * The page we want is at (first_object, first_pindex), but if the
619 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
620 * page table to figure out the actual pindex.
622 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
626 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
627 vm_map_interlock(fs.map, &ilock, vaddr, vaddr + PAGE_SIZE);
629 result = vm_fault_vpagetable(&fs, &first_pindex,
630 fs.entry->aux.master_pde,
632 if (result == KERN_TRY_AGAIN) {
633 vm_map_deinterlock(fs.map, &ilock);
634 vm_object_drop(fs.first_object);
638 if (result != KERN_SUCCESS) {
639 vm_map_deinterlock(fs.map, &ilock);
645 * Now we have the actual (object, pindex), fault in the page. If
646 * vm_fault_object() fails it will unlock and deallocate the FS
647 * data. If it succeeds everything remains locked and fs->object
648 * will have an additional PIP count if it is not equal to
651 * vm_fault_object will set fs->prot for the pmap operation. It is
652 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
653 * page can be safely written. However, it will force a read-only
654 * mapping for a read fault if the memory is managed by a virtual
657 * If the fault code uses the shared object lock shortcut
658 * we must not try to burst (we can't allocate VM pages).
660 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
662 if (debug_fault > 0) {
664 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
665 "fs.m=%p fs.prot=%02x fs.wflags=%02x fs.entry=%p\n",
666 result, (intmax_t)vaddr, fault_type, fault_flags,
667 fs.m, fs.prot, fs.wflags, fs.entry);
670 if (result == KERN_TRY_AGAIN) {
672 vm_map_deinterlock(fs.map, &ilock);
673 vm_object_drop(fs.first_object);
677 if (result != KERN_SUCCESS) {
679 vm_map_deinterlock(fs.map, &ilock);
684 * On success vm_fault_object() does not unlock or deallocate, and fs.m
685 * will contain a busied page.
687 * Enter the page into the pmap and do pmap-related adjustments.
689 KKASSERT(fs.lookup_still_valid == TRUE);
690 vm_page_flag_set(fs.m, PG_REFERENCED);
691 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot,
692 fs.wflags & FW_WIRED, fs.entry);
695 vm_map_deinterlock(fs.map, &ilock);
697 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
698 KKASSERT(fs.m->busy_count & PBUSY_LOCKED);
701 * If the page is not wired down, then put it where the pageout daemon
704 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
705 if (fs.wflags & FW_WIRED)
708 vm_page_unwire(fs.m, 1);
710 vm_page_activate(fs.m);
712 vm_page_wakeup(fs.m);
715 * Burst in a few more pages if possible. The fs.map should still
716 * be locked. To avoid interlocking against a vnode->getblk
717 * operation we had to be sure to unbusy our primary vm_page above
720 * A normal burst can continue down backing store, only execute
721 * if we are holding an exclusive lock, otherwise the exclusive
722 * locks the burst code gets might cause excessive SMP collisions.
724 * A quick burst can be utilized when there is no backing object
725 * (i.e. a shared file mmap).
727 if ((fault_flags & VM_FAULT_BURST) &&
728 (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
729 (fs.wflags & FW_WIRED) == 0) {
730 if (fs.first_shared == 0 && fs.shared == 0) {
731 vm_prefault(fs.map->pmap, vaddr,
732 fs.entry, fs.prot, fault_flags);
734 vm_prefault_quick(fs.map->pmap, vaddr,
735 fs.entry, fs.prot, fault_flags);
740 mycpu->gd_cnt.v_vm_faults++;
742 ++td->td_lwp->lwp_ru.ru_minflt;
745 * Unlock everything, and return
751 td->td_lwp->lwp_ru.ru_majflt++;
753 td->td_lwp->lwp_ru.ru_minflt++;
757 /*vm_object_deallocate(fs.first_object);*/
759 /*fs.first_object = NULL; must still drop later */
761 result = KERN_SUCCESS;
764 vm_object_drop(fs.first_object);
767 lp->lwp_flags &= ~LWP_PAGING;
769 #if !defined(NO_SWAPPING)
771 * Check the process RSS limit and force deactivation and
772 * (asynchronous) paging if necessary. This is a complex operation,
773 * only do it for direct user-mode faults, for now.
775 * To reduce overhead implement approximately a ~16MB hysteresis.
778 if ((fault_flags & VM_FAULT_USERMODE) && lp &&
779 p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 &&
780 map != &kernel_map) {
784 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
785 p->p_rlimit[RLIMIT_RSS].rlim_max));
786 size = pmap_resident_tlnw_count(map->pmap);
787 if (limit >= 0 && size > 4096 && size - 4096 >= limit) {
788 vm_pageout_map_deactivate_pages(map, limit);
797 * Fault in the specified virtual address in the current process map,
798 * returning a held VM page or NULL. See vm_fault_page() for more
804 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type,
805 int *errorp, int *busyp)
807 struct lwp *lp = curthread->td_lwp;
810 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
811 fault_type, VM_FAULT_NORMAL,
817 * Fault in the specified virtual address in the specified map, doing all
818 * necessary manipulation of the object store and all necessary I/O. Return
819 * a held VM page or NULL, and set *errorp. The related pmap is not
822 * If busyp is not NULL then *busyp will be set to TRUE if this routine
823 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
824 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is
825 * NULL the returned page is only held.
827 * If the caller has no intention of writing to the page's contents, busyp
828 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
829 * without busying the page.
831 * The returned page will also be marked PG_REFERENCED.
833 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
834 * error will be returned.
839 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
840 int fault_flags, int *errorp, int *busyp)
842 vm_pindex_t first_pindex;
843 struct faultstate fs;
848 vm_prot_t orig_fault_type = fault_type;
853 fs.fault_flags = fault_flags;
854 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
857 * Dive the pmap (concurrency possible). If we find the
858 * appropriate page we can terminate early and quickly.
860 * This works great for normal programs but will always return
861 * NULL for host lookups of vkernel maps in VMM mode.
863 * NOTE: pmap_fault_page_quick() might not busy the page. If
864 * VM_PROT_WRITE is set in fault_type and pmap_fault_page_quick()
865 * returns non-NULL, it will safely dirty the returned vm_page_t
866 * for us. We cannot safely dirty it here (it might not be
869 fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type, busyp);
876 * Otherwise take a concurrency hit and do a formal page
880 fs.shared = vm_shared_fault;
881 fs.first_shared = vm_shared_fault;
885 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
886 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
887 * we can try shared first.
889 if (fault_flags & VM_FAULT_UNSWAP) {
895 * Find the vm_map_entry representing the backing store and resolve
896 * the top level object and page index. This may have the side
897 * effect of executing a copy-on-write on the map entry and/or
898 * creating a shadow object, but will not COW any actual VM pages.
900 * On success fs.map is left read-locked and various other fields
901 * are initialized but not otherwise referenced or locked.
903 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
904 * if the map entry is a virtual page table and also writable,
905 * so we can set the 'A'accessed bit in the virtual page table
909 result = vm_map_lookup(&fs.map, vaddr, fault_type,
910 &fs.entry, &fs.first_object,
911 &first_pindex, &fs.first_prot, &fs.wflags);
913 if (result != KERN_SUCCESS) {
914 if (result == KERN_FAILURE_NOFAULT) {
915 *errorp = KERN_FAILURE;
919 if (result != KERN_PROTECTION_FAILURE ||
920 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
922 if (result == KERN_INVALID_ADDRESS && growstack &&
923 map != &kernel_map && curproc != NULL) {
924 result = vm_map_growstack(map, vaddr);
925 if (result == KERN_SUCCESS) {
930 result = KERN_FAILURE;
938 * If we are user-wiring a r/w segment, and it is COW, then
939 * we need to do the COW operation. Note that we don't
940 * currently COW RO sections now, because it is NOT desirable
941 * to COW .text. We simply keep .text from ever being COW'ed
942 * and take the heat that one cannot debug wired .text sections.
944 result = vm_map_lookup(&fs.map, vaddr,
945 VM_PROT_READ|VM_PROT_WRITE|
946 VM_PROT_OVERRIDE_WRITE,
947 &fs.entry, &fs.first_object,
948 &first_pindex, &fs.first_prot,
950 if (result != KERN_SUCCESS) {
951 /* could also be KERN_FAILURE_NOFAULT */
952 *errorp = KERN_FAILURE;
958 * If we don't COW now, on a user wire, the user will never
959 * be able to write to the mapping. If we don't make this
960 * restriction, the bookkeeping would be nearly impossible.
962 * XXX We have a shared lock, this will have a MP race but
963 * I don't see how it can hurt anything.
965 if ((fs.entry->protection & VM_PROT_WRITE) == 0) {
966 atomic_clear_char(&fs.entry->max_protection,
972 * fs.map is read-locked
974 * Misc checks. Save the map generation number to detect races.
976 fs.map_generation = fs.map->timestamp;
977 fs.lookup_still_valid = TRUE;
979 fs.object = fs.first_object; /* so unlock_and_deallocate works */
981 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
982 panic("vm_fault: fault on nofault entry, addr: %lx",
987 * A user-kernel shared map has no VM object and bypasses
988 * everything. We execute the uksmap function with a temporary
989 * fictitious vm_page. The address is directly mapped with no
992 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
993 struct vm_page fakem;
995 bzero(&fakem, sizeof(fakem));
996 fakem.pindex = first_pindex;
997 fakem.flags = PG_FICTITIOUS | PG_UNMANAGED;
998 fakem.busy_count = PBUSY_LOCKED;
999 fakem.valid = VM_PAGE_BITS_ALL;
1000 fakem.pat_mode = VM_MEMATTR_DEFAULT;
1001 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
1002 *errorp = KERN_FAILURE;
1007 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
1010 *busyp = 0; /* don't need to busy R or W */
1018 * A system map entry may return a NULL object. No object means
1019 * no pager means an unrecoverable kernel fault.
1021 if (fs.first_object == NULL) {
1022 panic("vm_fault: unrecoverable fault at %p in entry %p",
1023 (void *)vaddr, fs.entry);
1027 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
1030 * Unfortunately a deadlock can occur if we are forced to page-in
1031 * from swap, but diving all the way into the vm_pager_get_page()
1032 * function to find out is too much. Just check the object type.
1034 if ((curthread->td_flags & TDF_NOFAULT) &&
1036 fs.first_object->type == OBJT_VNODE ||
1037 fs.first_object->type == OBJT_SWAP ||
1038 fs.first_object->backing_object)) {
1039 *errorp = KERN_FAILURE;
1046 * If the entry is wired we cannot change the page protection.
1048 if (fs.wflags & FW_WIRED)
1049 fault_type = fs.first_prot;
1052 * Make a reference to this object to prevent its disposal while we
1053 * are messing with it. Once we have the reference, the map is free
1054 * to be diddled. Since objects reference their shadows (and copies),
1055 * they will stay around as well.
1057 * The reference should also prevent an unexpected collapse of the
1058 * parent that might move pages from the current object into the
1059 * parent unexpectedly, resulting in corruption.
1061 * Bump the paging-in-progress count to prevent size changes (e.g.
1062 * truncation operations) during I/O. This must be done after
1063 * obtaining the vnode lock in order to avoid possible deadlocks.
1065 if (fs.first_shared)
1066 vm_object_hold_shared(fs.first_object);
1068 vm_object_hold(fs.first_object);
1070 fs.vp = vnode_pager_lock(fs.first_object); /* shared */
1073 * The page we want is at (first_object, first_pindex), but if the
1074 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
1075 * page table to figure out the actual pindex.
1077 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
1080 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1081 result = vm_fault_vpagetable(&fs, &first_pindex,
1082 fs.entry->aux.master_pde,
1084 if (result == KERN_TRY_AGAIN) {
1085 vm_object_drop(fs.first_object);
1089 if (result != KERN_SUCCESS) {
1097 * Now we have the actual (object, pindex), fault in the page. If
1098 * vm_fault_object() fails it will unlock and deallocate the FS
1099 * data. If it succeeds everything remains locked and fs->object
1100 * will have an additinal PIP count if it is not equal to
1104 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
1106 if (result == KERN_TRY_AGAIN) {
1107 vm_object_drop(fs.first_object);
1109 didcow |= fs.wflags & FW_DIDCOW;
1112 if (result != KERN_SUCCESS) {
1118 if ((orig_fault_type & VM_PROT_WRITE) &&
1119 (fs.prot & VM_PROT_WRITE) == 0) {
1120 *errorp = KERN_PROTECTION_FAILURE;
1121 unlock_and_deallocate(&fs);
1127 * Generally speaking we don't want to update the pmap because
1128 * this routine can be called many times for situations that do
1129 * not require updating the pmap, not to mention the page might
1130 * already be in the pmap.
1132 * However, if our vm_map_lookup() results in a COW, we need to
1133 * at least remove the pte from the pmap to guarantee proper
1134 * visibility of modifications made to the process. For example,
1135 * modifications made by vkernel uiocopy/related routines and
1136 * modifications made by ptrace().
1138 vm_page_flag_set(fs.m, PG_REFERENCED);
1140 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot,
1141 fs.wflags & FW_WIRED, NULL);
1142 mycpu->gd_cnt.v_vm_faults++;
1143 if (curthread->td_lwp)
1144 ++curthread->td_lwp->lwp_ru.ru_minflt;
1146 if ((fs.wflags | didcow) | FW_DIDCOW) {
1147 pmap_remove(fs.map->pmap,
1149 (vaddr & ~PAGE_MASK) + PAGE_SIZE);
1153 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1154 * will contain a busied page. So we must unlock here after having
1155 * messed with the pmap.
1160 * Return a held page. We are not doing any pmap manipulation so do
1161 * not set PG_MAPPED. However, adjust the page flags according to
1162 * the fault type because the caller may not use a managed pmapping
1163 * (so we don't want to lose the fact that the page will be dirtied
1164 * if a write fault was specified).
1166 if (fault_type & VM_PROT_WRITE)
1167 vm_page_dirty(fs.m);
1168 vm_page_activate(fs.m);
1170 if (curthread->td_lwp) {
1172 curthread->td_lwp->lwp_ru.ru_majflt++;
1174 curthread->td_lwp->lwp_ru.ru_minflt++;
1179 * Unlock everything, and return the held or busied page.
1182 if (fault_type & VM_PROT_WRITE) {
1183 vm_page_dirty(fs.m);
1188 vm_page_wakeup(fs.m);
1192 vm_page_wakeup(fs.m);
1194 /*vm_object_deallocate(fs.first_object);*/
1195 /*fs.first_object = NULL; */
1199 if (fs.first_object)
1200 vm_object_drop(fs.first_object);
1206 * Fault in the specified (object,offset), dirty the returned page as
1207 * needed. If the requested fault_type cannot be done NULL and an
1208 * error is returned.
1210 * A held (but not busied) page is returned.
1212 * The passed in object must be held as specified by the shared
1216 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
1217 vm_prot_t fault_type, int fault_flags,
1218 int *sharedp, int *errorp)
1221 vm_pindex_t first_pindex;
1222 struct faultstate fs;
1223 struct vm_map_entry entry;
1225 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1226 bzero(&entry, sizeof(entry));
1227 entry.object.vm_object = object;
1228 entry.maptype = VM_MAPTYPE_NORMAL;
1229 entry.protection = entry.max_protection = fault_type;
1232 fs.fault_flags = fault_flags;
1234 fs.shared = vm_shared_fault;
1235 fs.first_shared = *sharedp;
1237 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
1240 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1241 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
1242 * we can try shared first.
1244 if (fs.first_shared && (fault_flags & VM_FAULT_UNSWAP)) {
1245 fs.first_shared = 0;
1246 vm_object_upgrade(object);
1250 * Retry loop as needed (typically for shared->exclusive transitions)
1253 *sharedp = fs.first_shared;
1254 first_pindex = OFF_TO_IDX(offset);
1255 fs.first_object = object;
1257 fs.first_prot = fault_type;
1259 /*fs.map_generation = 0; unused */
1262 * Make a reference to this object to prevent its disposal while we
1263 * are messing with it. Once we have the reference, the map is free
1264 * to be diddled. Since objects reference their shadows (and copies),
1265 * they will stay around as well.
1267 * The reference should also prevent an unexpected collapse of the
1268 * parent that might move pages from the current object into the
1269 * parent unexpectedly, resulting in corruption.
1271 * Bump the paging-in-progress count to prevent size changes (e.g.
1272 * truncation operations) during I/O. This must be done after
1273 * obtaining the vnode lock in order to avoid possible deadlocks.
1276 fs.vp = vnode_pager_lock(fs.first_object);
1278 fs.lookup_still_valid = TRUE;
1280 fs.object = fs.first_object; /* so unlock_and_deallocate works */
1283 /* XXX future - ability to operate on VM object using vpagetable */
1284 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1285 result = vm_fault_vpagetable(&fs, &first_pindex,
1286 fs.entry->aux.master_pde,
1288 if (result == KERN_TRY_AGAIN) {
1289 if (fs.first_shared == 0 && *sharedp)
1290 vm_object_upgrade(object);
1293 if (result != KERN_SUCCESS) {
1301 * Now we have the actual (object, pindex), fault in the page. If
1302 * vm_fault_object() fails it will unlock and deallocate the FS
1303 * data. If it succeeds everything remains locked and fs->object
1304 * will have an additinal PIP count if it is not equal to
1307 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1308 * We may have to upgrade its lock to handle the requested fault.
1310 result = vm_fault_object(&fs, first_pindex, fault_type, 0);
1312 if (result == KERN_TRY_AGAIN) {
1313 if (fs.first_shared == 0 && *sharedp)
1314 vm_object_upgrade(object);
1317 if (result != KERN_SUCCESS) {
1322 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1323 *errorp = KERN_PROTECTION_FAILURE;
1324 unlock_and_deallocate(&fs);
1329 * On success vm_fault_object() does not unlock or deallocate, so we
1330 * do it here. Note that the returned fs.m will be busied.
1335 * Return a held page. We are not doing any pmap manipulation so do
1336 * not set PG_MAPPED. However, adjust the page flags according to
1337 * the fault type because the caller may not use a managed pmapping
1338 * (so we don't want to lose the fact that the page will be dirtied
1339 * if a write fault was specified).
1342 vm_page_activate(fs.m);
1343 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
1344 vm_page_dirty(fs.m);
1345 if (fault_flags & VM_FAULT_UNSWAP)
1346 swap_pager_unswapped(fs.m);
1349 * Indicate that the page was accessed.
1351 vm_page_flag_set(fs.m, PG_REFERENCED);
1353 if (curthread->td_lwp) {
1355 curthread->td_lwp->lwp_ru.ru_majflt++;
1357 curthread->td_lwp->lwp_ru.ru_minflt++;
1362 * Unlock everything, and return the held page.
1364 vm_page_wakeup(fs.m);
1365 /*vm_object_deallocate(fs.first_object);*/
1366 /*fs.first_object = NULL; */
1373 * Translate the virtual page number (first_pindex) that is relative
1374 * to the address space into a logical page number that is relative to the
1375 * backing object. Use the virtual page table pointed to by (vpte).
1377 * Possibly downgrade the protection based on the vpte bits.
1379 * This implements an N-level page table. Any level can terminate the
1380 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1381 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1385 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1386 vpte_t vpte, int fault_type, int allow_nofault)
1389 struct lwbuf lwb_cache;
1390 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1394 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1397 * We cannot proceed if the vpte is not valid, not readable
1398 * for a read fault, not writable for a write fault, or
1399 * not executable for an instruction execution fault.
1401 if ((vpte & VPTE_V) == 0) {
1402 unlock_and_deallocate(fs);
1403 return (KERN_FAILURE);
1405 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) {
1406 unlock_and_deallocate(fs);
1407 return (KERN_FAILURE);
1409 if ((fault_type & VM_PROT_EXECUTE) && (vpte & VPTE_NX)) {
1410 unlock_and_deallocate(fs);
1411 return (KERN_FAILURE);
1413 if ((vpte & VPTE_PS) || vshift == 0)
1417 * Get the page table page. Nominally we only read the page
1418 * table, but since we are actively setting VPTE_M and VPTE_A,
1419 * tell vm_fault_object() that we are writing it.
1421 * There is currently no real need to optimize this.
1423 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1424 VM_PROT_READ|VM_PROT_WRITE,
1426 if (result != KERN_SUCCESS)
1430 * Process the returned fs.m and look up the page table
1431 * entry in the page table page.
1433 vshift -= VPTE_PAGE_BITS;
1434 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1435 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1436 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1437 vm_page_activate(fs->m);
1440 * Page table write-back - entire operation including
1441 * validation of the pte must be atomic to avoid races
1442 * against the vkernel changing the pte.
1444 * If the vpte is valid for the* requested operation, do
1445 * a write-back to the page table.
1447 * XXX VPTE_M is not set properly for page directory pages.
1448 * It doesn't get set in the page directory if the page table
1449 * is modified during a read access.
1455 * Reload for the cmpset, but make sure the pte is
1462 if ((vpte & VPTE_V) == 0)
1465 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW))
1466 nvpte |= VPTE_M | VPTE_A;
1467 if (fault_type & (VM_PROT_READ | VM_PROT_EXECUTE))
1471 if (atomic_cmpset_long(ptep, vpte, nvpte)) {
1472 vm_page_dirty(fs->m);
1477 vm_page_flag_set(fs->m, PG_REFERENCED);
1478 vm_page_wakeup(fs->m);
1480 cleanup_successful_fault(fs);
1484 * When the vkernel sets VPTE_RW it expects the real kernel to
1485 * reflect VPTE_M back when the page is modified via the mapping.
1486 * In order to accomplish this the real kernel must map the page
1487 * read-only for read faults and use write faults to reflect VPTE_M
1490 * Once VPTE_M has been set, the real kernel's pte allows writing.
1491 * If the vkernel clears VPTE_M the vkernel must be sure to
1492 * MADV_INVAL the real kernel's mappings to force the real kernel
1493 * to re-fault on the next write so oit can set VPTE_M again.
1495 if ((fault_type & VM_PROT_WRITE) == 0 &&
1496 (vpte & (VPTE_RW | VPTE_M)) != (VPTE_RW | VPTE_M)) {
1497 fs->first_prot &= ~VM_PROT_WRITE;
1501 * Disable EXECUTE perms if NX bit is set.
1504 fs->first_prot &= ~VM_PROT_EXECUTE;
1507 * Combine remaining address bits with the vpte.
1509 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1510 (*pindex & ((1L << vshift) - 1));
1511 return (KERN_SUCCESS);
1516 * This is the core of the vm_fault code.
1518 * Do all operations required to fault-in (fs.first_object, pindex). Run
1519 * through the shadow chain as necessary and do required COW or virtual
1520 * copy operations. The caller has already fully resolved the vm_map_entry
1521 * and, if appropriate, has created a copy-on-write layer. All we need to
1522 * do is iterate the object chain.
1524 * On failure (fs) is unlocked and deallocated and the caller may return or
1525 * retry depending on the failure code. On success (fs) is NOT unlocked or
1526 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1527 * will have an additional PIP count if it is not equal to fs.first_object.
1529 * If locks based on fs->first_shared or fs->shared are insufficient,
1530 * clear the appropriate field(s) and return RETRY. COWs require that
1531 * first_shared be 0, while page allocations (or frees) require that
1532 * shared be 0. Renames require that both be 0.
1534 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1535 * we will have to retry with it exclusive if the vm_page is
1538 * fs->first_object must be held on call.
1542 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex,
1543 vm_prot_t fault_type, int allow_nofault)
1545 vm_object_t next_object;
1549 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1550 fs->prot = fs->first_prot;
1551 fs->object = fs->first_object;
1552 pindex = first_pindex;
1554 vm_object_chain_acquire(fs->first_object, fs->shared);
1555 vm_object_pip_add(fs->first_object, 1);
1558 * If a read fault occurs we try to upgrade the page protection
1559 * and make it also writable if possible. There are three cases
1560 * where we cannot make the page mapping writable:
1562 * (1) The mapping is read-only or the VM object is read-only,
1563 * fs->prot above will simply not have VM_PROT_WRITE set.
1565 * (2) If the mapping is a virtual page table fs->first_prot will
1566 * have already been properly adjusted by vm_fault_vpagetable().
1567 * to detect writes so we can set VPTE_M in the virtual page
1568 * table. Used by vkernels.
1570 * (3) If the VM page is read-only or copy-on-write, upgrading would
1571 * just result in an unnecessary COW fault.
1573 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1577 /* see vpagetable code */
1578 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1579 if ((fault_type & VM_PROT_WRITE) == 0)
1580 fs->prot &= ~VM_PROT_WRITE;
1584 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
1585 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
1586 if ((fault_type & VM_PROT_WRITE) == 0)
1587 fs->prot &= ~VM_PROT_WRITE;
1590 /* vm_object_hold(fs->object); implied b/c object == first_object */
1594 * The entire backing chain from first_object to object
1595 * inclusive is chainlocked.
1597 * If the object is dead, we stop here
1599 if (fs->object->flags & OBJ_DEAD) {
1600 vm_object_pip_wakeup(fs->first_object);
1601 vm_object_chain_release_all(fs->first_object,
1603 if (fs->object != fs->first_object)
1604 vm_object_drop(fs->object);
1605 unlock_and_deallocate(fs);
1606 return (KERN_PROTECTION_FAILURE);
1610 * See if the page is resident. Wait/Retry if the page is
1611 * busy (lots of stuff may have changed so we can't continue
1614 * We can theoretically allow the soft-busy case on a read
1615 * fault if the page is marked valid, but since such
1616 * pages are typically already pmap'd, putting that
1617 * special case in might be more effort then it is
1618 * worth. We cannot under any circumstances mess
1619 * around with a vm_page_t->busy page except, perhaps,
1622 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1625 vm_object_pip_wakeup(fs->first_object);
1626 vm_object_chain_release_all(fs->first_object,
1628 if (fs->object != fs->first_object)
1629 vm_object_drop(fs->object);
1631 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1632 mycpu->gd_cnt.v_intrans++;
1633 /*vm_object_deallocate(fs->first_object);*/
1634 /*fs->first_object = NULL;*/
1636 return (KERN_TRY_AGAIN);
1640 * The page is busied for us.
1642 * If reactivating a page from PQ_CACHE we may have
1645 int queue = fs->m->queue;
1646 vm_page_unqueue_nowakeup(fs->m);
1648 if ((queue - fs->m->pc) == PQ_CACHE &&
1649 vm_page_count_severe()) {
1650 vm_page_activate(fs->m);
1651 vm_page_wakeup(fs->m);
1653 vm_object_pip_wakeup(fs->first_object);
1654 vm_object_chain_release_all(fs->first_object,
1656 if (fs->object != fs->first_object)
1657 vm_object_drop(fs->object);
1658 unlock_and_deallocate(fs);
1659 if (allow_nofault == 0 ||
1660 (curthread->td_flags & TDF_NOFAULT) == 0) {
1665 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1666 return (KERN_PROTECTION_FAILURE);
1668 return (KERN_TRY_AGAIN);
1672 * If it still isn't completely valid (readable),
1673 * or if a read-ahead-mark is set on the VM page,
1674 * jump to readrest, else we found the page and
1677 * We can release the spl once we have marked the
1680 if (fs->m->object != &kernel_object) {
1681 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1685 if (fs->m->flags & PG_RAM) {
1688 vm_page_flag_clear(fs->m, PG_RAM);
1692 break; /* break to PAGE HAS BEEN FOUND */
1696 * Page is not resident, If this is the search termination
1697 * or the pager might contain the page, allocate a new page.
1699 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1701 * Allocating, must be exclusive.
1703 if (fs->object == fs->first_object &&
1705 fs->first_shared = 0;
1706 vm_object_pip_wakeup(fs->first_object);
1707 vm_object_chain_release_all(fs->first_object,
1709 if (fs->object != fs->first_object)
1710 vm_object_drop(fs->object);
1711 unlock_and_deallocate(fs);
1712 return (KERN_TRY_AGAIN);
1714 if (fs->object != fs->first_object &&
1716 fs->first_shared = 0;
1718 vm_object_pip_wakeup(fs->first_object);
1719 vm_object_chain_release_all(fs->first_object,
1721 if (fs->object != fs->first_object)
1722 vm_object_drop(fs->object);
1723 unlock_and_deallocate(fs);
1724 return (KERN_TRY_AGAIN);
1728 * If the page is beyond the object size we fail
1730 if (pindex >= fs->object->size) {
1731 vm_object_pip_wakeup(fs->first_object);
1732 vm_object_chain_release_all(fs->first_object,
1734 if (fs->object != fs->first_object)
1735 vm_object_drop(fs->object);
1736 unlock_and_deallocate(fs);
1737 return (KERN_PROTECTION_FAILURE);
1741 * Allocate a new page for this object/offset pair.
1743 * It is possible for the allocation to race, so
1747 if (!vm_page_count_severe()) {
1748 fs->m = vm_page_alloc(fs->object, pindex,
1749 ((fs->vp || fs->object->backing_object) ?
1750 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1751 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1752 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1754 if (fs->m == NULL) {
1755 vm_object_pip_wakeup(fs->first_object);
1756 vm_object_chain_release_all(fs->first_object,
1758 if (fs->object != fs->first_object)
1759 vm_object_drop(fs->object);
1760 unlock_and_deallocate(fs);
1761 if (allow_nofault == 0 ||
1762 (curthread->td_flags & TDF_NOFAULT) == 0) {
1767 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1768 return (KERN_PROTECTION_FAILURE);
1770 return (KERN_TRY_AGAIN);
1774 * Fall through to readrest. We have a new page which
1775 * will have to be paged (since m->valid will be 0).
1781 * We have found an invalid or partially valid page, a
1782 * page with a read-ahead mark which might be partially or
1783 * fully valid (and maybe dirty too), or we have allocated
1786 * Attempt to fault-in the page if there is a chance that the
1787 * pager has it, and potentially fault in additional pages
1790 * If TRYPAGER is true then fs.m will be non-NULL and busied
1796 u_char behavior = vm_map_entry_behavior(fs->entry);
1798 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1804 * Doing I/O may synchronously insert additional
1805 * pages so we can't be shared at this point either.
1807 * NOTE: We can't free fs->m here in the allocated
1808 * case (fs->object != fs->first_object) as
1809 * this would require an exclusively locked
1812 if (fs->object == fs->first_object &&
1814 vm_page_deactivate(fs->m);
1815 vm_page_wakeup(fs->m);
1817 fs->first_shared = 0;
1818 vm_object_pip_wakeup(fs->first_object);
1819 vm_object_chain_release_all(fs->first_object,
1821 if (fs->object != fs->first_object)
1822 vm_object_drop(fs->object);
1823 unlock_and_deallocate(fs);
1824 return (KERN_TRY_AGAIN);
1826 if (fs->object != fs->first_object &&
1828 vm_page_deactivate(fs->m);
1829 vm_page_wakeup(fs->m);
1831 fs->first_shared = 0;
1833 vm_object_pip_wakeup(fs->first_object);
1834 vm_object_chain_release_all(fs->first_object,
1836 if (fs->object != fs->first_object)
1837 vm_object_drop(fs->object);
1838 unlock_and_deallocate(fs);
1839 return (KERN_TRY_AGAIN);
1843 * Avoid deadlocking against the map when doing I/O.
1844 * fs.object and the page is BUSY'd.
1846 * NOTE: Once unlocked, fs->entry can become stale
1847 * so this will NULL it out.
1849 * NOTE: fs->entry is invalid until we relock the
1850 * map and verify that the timestamp has not
1856 * Acquire the page data. We still hold a ref on
1857 * fs.object and the page has been BUSY's.
1859 * The pager may replace the page (for example, in
1860 * order to enter a fictitious page into the
1861 * object). If it does so it is responsible for
1862 * cleaning up the passed page and properly setting
1863 * the new page BUSY.
1865 * If we got here through a PG_RAM read-ahead
1866 * mark the page may be partially dirty and thus
1867 * not freeable. Don't bother checking to see
1868 * if the pager has the page because we can't free
1869 * it anyway. We have to depend on the get_page
1870 * operation filling in any gaps whether there is
1871 * backing store or not.
1873 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1875 if (rv == VM_PAGER_OK) {
1877 * Relookup in case pager changed page. Pager
1878 * is responsible for disposition of old page
1881 * XXX other code segments do relookups too.
1882 * It's a bad abstraction that needs to be
1885 fs->m = vm_page_lookup(fs->object, pindex);
1886 if (fs->m == NULL) {
1887 vm_object_pip_wakeup(fs->first_object);
1888 vm_object_chain_release_all(
1889 fs->first_object, fs->object);
1890 if (fs->object != fs->first_object)
1891 vm_object_drop(fs->object);
1892 unlock_and_deallocate(fs);
1893 return (KERN_TRY_AGAIN);
1896 break; /* break to PAGE HAS BEEN FOUND */
1900 * Remove the bogus page (which does not exist at this
1901 * object/offset); before doing so, we must get back
1902 * our object lock to preserve our invariant.
1904 * Also wake up any other process that may want to bring
1907 * If this is the top-level object, we must leave the
1908 * busy page to prevent another process from rushing
1909 * past us, and inserting the page in that object at
1910 * the same time that we are.
1912 if (rv == VM_PAGER_ERROR) {
1914 kprintf("vm_fault: pager read error, "
1919 kprintf("vm_fault: pager read error, "
1922 curthread->td_comm);
1927 * Data outside the range of the pager or an I/O error
1929 * The page may have been wired during the pagein,
1930 * e.g. by the buffer cache, and cannot simply be
1931 * freed. Call vnode_pager_freepage() to deal with it.
1933 * Also note that we cannot free the page if we are
1934 * holding the related object shared. XXX not sure
1935 * what to do in that case.
1937 if (fs->object != fs->first_object) {
1939 * Scrap the page. Check to see if the
1940 * vm_pager_get_page() call has already
1944 vnode_pager_freepage(fs->m);
1949 * XXX - we cannot just fall out at this
1950 * point, m has been freed and is invalid!
1954 * XXX - the check for kernel_map is a kludge to work
1955 * around having the machine panic on a kernel space
1956 * fault w/ I/O error.
1958 if (((fs->map != &kernel_map) &&
1959 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1961 if (fs->first_shared) {
1962 vm_page_deactivate(fs->m);
1963 vm_page_wakeup(fs->m);
1965 vnode_pager_freepage(fs->m);
1969 vm_object_pip_wakeup(fs->first_object);
1970 vm_object_chain_release_all(fs->first_object,
1972 if (fs->object != fs->first_object)
1973 vm_object_drop(fs->object);
1974 unlock_and_deallocate(fs);
1975 if (rv == VM_PAGER_ERROR)
1976 return (KERN_FAILURE);
1978 return (KERN_PROTECTION_FAILURE);
1984 * We get here if the object has a default pager (or unwiring)
1985 * or the pager doesn't have the page.
1987 * fs->first_m will be used for the COW unless we find a
1988 * deeper page to be mapped read-only, in which case the
1989 * unlock*(fs) will free first_m.
1991 if (fs->object == fs->first_object)
1992 fs->first_m = fs->m;
1995 * Move on to the next object. The chain lock should prevent
1996 * the backing_object from getting ripped out from under us.
1998 * The object lock for the next object is governed by
2001 if ((next_object = fs->object->backing_object) != NULL) {
2003 vm_object_hold_shared(next_object);
2005 vm_object_hold(next_object);
2006 vm_object_chain_acquire(next_object, fs->shared);
2007 KKASSERT(next_object == fs->object->backing_object);
2008 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
2011 if (next_object == NULL) {
2013 * If there's no object left, fill the page in the top
2014 * object with zeros.
2016 if (fs->object != fs->first_object) {
2018 if (fs->first_object->backing_object !=
2020 vm_object_hold(fs->first_object->backing_object);
2023 vm_object_chain_release_all(
2024 fs->first_object->backing_object,
2027 if (fs->first_object->backing_object !=
2029 vm_object_drop(fs->first_object->backing_object);
2032 vm_object_pip_wakeup(fs->object);
2033 vm_object_drop(fs->object);
2034 fs->object = fs->first_object;
2035 pindex = first_pindex;
2036 fs->m = fs->first_m;
2041 * Zero the page and mark it valid.
2043 vm_page_zero_fill(fs->m);
2044 mycpu->gd_cnt.v_zfod++;
2045 fs->m->valid = VM_PAGE_BITS_ALL;
2046 break; /* break to PAGE HAS BEEN FOUND */
2048 if (fs->object != fs->first_object) {
2049 vm_object_pip_wakeup(fs->object);
2050 vm_object_lock_swap();
2051 vm_object_drop(fs->object);
2053 KASSERT(fs->object != next_object,
2054 ("object loop %p", next_object));
2055 fs->object = next_object;
2056 vm_object_pip_add(fs->object, 1);
2060 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
2063 * object still held.
2064 * vm_map may not be locked (determined by fs->lookup_still_valid)
2066 * local shared variable may be different from fs->shared.
2068 * If the page is being written, but isn't already owned by the
2069 * top-level object, we have to copy it into a new page owned by the
2072 KASSERT((fs->m->busy_count & PBUSY_LOCKED) != 0,
2073 ("vm_fault: not busy after main loop"));
2075 if (fs->object != fs->first_object) {
2077 * We only really need to copy if we want to write it.
2079 if (fault_type & VM_PROT_WRITE) {
2081 * This allows pages to be virtually copied from a
2082 * backing_object into the first_object, where the
2083 * backing object has no other refs to it, and cannot
2084 * gain any more refs. Instead of a bcopy, we just
2085 * move the page from the backing object to the
2086 * first object. Note that we must mark the page
2087 * dirty in the first object so that it will go out
2088 * to swap when needed.
2090 if (virtual_copy_ok(fs)) {
2092 * (first_m) and (m) are both busied. We have
2093 * move (m) into (first_m)'s object/pindex
2094 * in an atomic fashion, then free (first_m).
2096 * first_object is held so second remove
2097 * followed by the rename should wind
2098 * up being atomic. vm_page_free() might
2099 * block so we don't do it until after the
2102 vm_page_protect(fs->first_m, VM_PROT_NONE);
2103 vm_page_remove(fs->first_m);
2104 vm_page_rename(fs->m, fs->first_object,
2106 vm_page_free(fs->first_m);
2107 fs->first_m = fs->m;
2109 mycpu->gd_cnt.v_cow_optim++;
2112 * Oh, well, lets copy it.
2114 * Why are we unmapping the original page
2115 * here? Well, in short, not all accessors
2116 * of user memory go through the pmap. The
2117 * procfs code doesn't have access user memory
2118 * via a local pmap, so vm_fault_page*()
2119 * can't call pmap_enter(). And the umtx*()
2120 * code may modify the COW'd page via a DMAP
2121 * or kernel mapping and not via the pmap,
2122 * leaving the original page still mapped
2123 * read-only into the pmap.
2125 * So we have to remove the page from at
2126 * least the current pmap if it is in it.
2128 * We used to just remove it from all pmaps
2129 * but that creates inefficiencies on SMP,
2130 * particularly for COW program & library
2131 * mappings that are concurrently exec'd.
2132 * Only remove the page from the current
2135 KKASSERT(fs->first_shared == 0);
2136 vm_page_copy(fs->m, fs->first_m);
2137 /*vm_page_protect(fs->m, VM_PROT_NONE);*/
2138 pmap_remove_specific(
2139 &curthread->td_lwp->lwp_vmspace->vm_pmap,
2144 * We no longer need the old page or object.
2150 * We intend to revert to first_object, undo the
2151 * chain lock through to that.
2154 if (fs->first_object->backing_object != fs->object)
2155 vm_object_hold(fs->first_object->backing_object);
2157 vm_object_chain_release_all(
2158 fs->first_object->backing_object,
2161 if (fs->first_object->backing_object != fs->object)
2162 vm_object_drop(fs->first_object->backing_object);
2166 * fs->object != fs->first_object due to above
2169 vm_object_pip_wakeup(fs->object);
2170 vm_object_drop(fs->object);
2173 * Only use the new page below...
2175 mycpu->gd_cnt.v_cow_faults++;
2176 fs->m = fs->first_m;
2177 fs->object = fs->first_object;
2178 pindex = first_pindex;
2181 * If it wasn't a write fault avoid having to copy
2182 * the page by mapping it read-only.
2184 fs->prot &= ~VM_PROT_WRITE;
2189 * Relock the map if necessary, then check the generation count.
2190 * relock_map() will update fs->timestamp to account for the
2191 * relocking if necessary.
2193 * If the count has changed after relocking then all sorts of
2194 * crap may have happened and we have to retry.
2196 * NOTE: The relock_map() can fail due to a deadlock against
2197 * the vm_page we are holding BUSY.
2199 if (fs->lookup_still_valid == FALSE && fs->map) {
2200 if (relock_map(fs) ||
2201 fs->map->timestamp != fs->map_generation) {
2203 vm_object_pip_wakeup(fs->first_object);
2204 vm_object_chain_release_all(fs->first_object,
2206 if (fs->object != fs->first_object)
2207 vm_object_drop(fs->object);
2208 unlock_and_deallocate(fs);
2209 return (KERN_TRY_AGAIN);
2214 * If the fault is a write, we know that this page is being
2215 * written NOW so dirty it explicitly to save on pmap_is_modified()
2218 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2219 * if the page is already dirty to prevent data written with
2220 * the expectation of being synced from not being synced.
2221 * Likewise if this entry does not request NOSYNC then make
2222 * sure the page isn't marked NOSYNC. Applications sharing
2223 * data should use the same flags to avoid ping ponging.
2225 * Also tell the backing pager, if any, that it should remove
2226 * any swap backing since the page is now dirty.
2228 vm_page_activate(fs->m);
2229 if (fs->prot & VM_PROT_WRITE) {
2230 vm_object_set_writeable_dirty(fs->m->object);
2231 vm_set_nosync(fs->m, fs->entry);
2232 if (fs->fault_flags & VM_FAULT_DIRTY) {
2233 vm_page_dirty(fs->m);
2234 if (fs->m->flags & PG_SWAPPED) {
2236 * If the page is swapped out we have to call
2237 * swap_pager_unswapped() which requires an
2238 * exclusive object lock. If we are shared,
2239 * we must clear the shared flag and retry.
2241 if ((fs->object == fs->first_object &&
2242 fs->first_shared) ||
2243 (fs->object != fs->first_object &&
2245 vm_page_wakeup(fs->m);
2247 if (fs->object == fs->first_object)
2248 fs->first_shared = 0;
2251 vm_object_pip_wakeup(fs->first_object);
2252 vm_object_chain_release_all(
2253 fs->first_object, fs->object);
2254 if (fs->object != fs->first_object)
2255 vm_object_drop(fs->object);
2256 unlock_and_deallocate(fs);
2257 return (KERN_TRY_AGAIN);
2259 swap_pager_unswapped(fs->m);
2264 vm_object_pip_wakeup(fs->first_object);
2265 vm_object_chain_release_all(fs->first_object, fs->object);
2266 if (fs->object != fs->first_object)
2267 vm_object_drop(fs->object);
2270 * Page had better still be busy. We are still locked up and
2271 * fs->object will have another PIP reference if it is not equal
2272 * to fs->first_object.
2274 KASSERT(fs->m->busy_count & PBUSY_LOCKED,
2275 ("vm_fault: page %p not busy!", fs->m));
2278 * Sanity check: page must be completely valid or it is not fit to
2279 * map into user space. vm_pager_get_pages() ensures this.
2281 if (fs->m->valid != VM_PAGE_BITS_ALL) {
2282 vm_page_zero_invalid(fs->m, TRUE);
2283 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
2286 return (KERN_SUCCESS);
2290 * Wire down a range of virtual addresses in a map. The entry in question
2291 * should be marked in-transition and the map must be locked. We must
2292 * release the map temporarily while faulting-in the page to avoid a
2293 * deadlock. Note that the entry may be clipped while we are blocked but
2294 * will never be freed.
2299 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2300 boolean_t user_wire, int kmflags)
2302 boolean_t fictitious;
2313 wire_prot = VM_PROT_READ;
2314 fault_flags = VM_FAULT_USER_WIRE;
2316 wire_prot = VM_PROT_READ | VM_PROT_WRITE;
2317 fault_flags = VM_FAULT_CHANGE_WIRING;
2319 if (kmflags & KM_NOTLBSYNC)
2320 wire_prot |= VM_PROT_NOSYNC;
2322 pmap = vm_map_pmap(map);
2323 start = entry->start;
2326 switch(entry->maptype) {
2327 case VM_MAPTYPE_NORMAL:
2328 case VM_MAPTYPE_VPAGETABLE:
2329 fictitious = entry->object.vm_object &&
2330 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2331 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2333 case VM_MAPTYPE_UKSMAP:
2341 if (entry->eflags & MAP_ENTRY_KSTACK)
2347 * We simulate a fault to get the page and enter it in the physical
2350 for (va = start; va < end; va += PAGE_SIZE) {
2351 rv = vm_fault(map, va, wire_prot, fault_flags);
2353 while (va > start) {
2355 m = pmap_unwire(pmap, va);
2356 if (m && !fictitious) {
2357 vm_page_busy_wait(m, FALSE, "vmwrpg");
2358 vm_page_unwire(m, 1);
2373 * Unwire a range of virtual addresses in a map. The map should be
2377 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2379 boolean_t fictitious;
2386 pmap = vm_map_pmap(map);
2387 start = entry->start;
2389 fictitious = entry->object.vm_object &&
2390 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2391 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2392 if (entry->eflags & MAP_ENTRY_KSTACK)
2396 * Since the pages are wired down, we must be able to get their
2397 * mappings from the physical map system.
2399 for (va = start; va < end; va += PAGE_SIZE) {
2400 m = pmap_unwire(pmap, va);
2401 if (m && !fictitious) {
2402 vm_page_busy_wait(m, FALSE, "vmwrpg");
2403 vm_page_unwire(m, 1);
2410 * Copy all of the pages from a wired-down map entry to another.
2412 * The source and destination maps must be locked for write.
2413 * The source and destination maps token must be held
2414 * The source map entry must be wired down (or be a sharing map
2415 * entry corresponding to a main map entry that is wired down).
2417 * No other requirements.
2419 * XXX do segment optimization
2422 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
2423 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
2425 vm_object_t dst_object;
2426 vm_object_t src_object;
2427 vm_ooffset_t dst_offset;
2428 vm_ooffset_t src_offset;
2434 src_object = src_entry->object.vm_object;
2435 src_offset = src_entry->offset;
2438 * Create the top-level object for the destination entry. (Doesn't
2439 * actually shadow anything - we copy the pages directly.)
2441 vm_map_entry_allocate_object(dst_entry);
2442 dst_object = dst_entry->object.vm_object;
2444 prot = dst_entry->max_protection;
2447 * Loop through all of the pages in the entry's range, copying each
2448 * one from the source object (it should be there) to the destination
2451 vm_object_hold(src_object);
2452 vm_object_hold(dst_object);
2454 for (vaddr = dst_entry->start, dst_offset = 0;
2455 vaddr < dst_entry->end;
2456 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
2459 * Allocate a page in the destination object
2462 dst_m = vm_page_alloc(dst_object,
2463 OFF_TO_IDX(dst_offset),
2465 if (dst_m == NULL) {
2468 } while (dst_m == NULL);
2471 * Find the page in the source object, and copy it in.
2472 * (Because the source is wired down, the page will be in
2475 src_m = vm_page_lookup(src_object,
2476 OFF_TO_IDX(dst_offset + src_offset));
2478 panic("vm_fault_copy_wired: page missing");
2480 vm_page_copy(src_m, dst_m);
2483 * Enter it in the pmap...
2485 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
2488 * Mark it no longer busy, and put it on the active list.
2490 vm_page_activate(dst_m);
2491 vm_page_wakeup(dst_m);
2493 vm_object_drop(dst_object);
2494 vm_object_drop(src_object);
2500 * This routine checks around the requested page for other pages that
2501 * might be able to be faulted in. This routine brackets the viable
2502 * pages for the pages to be paged in.
2505 * m, rbehind, rahead
2508 * marray (array of vm_page_t), reqpage (index of requested page)
2511 * number of pages in marray
2514 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2515 vm_page_t *marray, int *reqpage)
2519 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2521 int cbehind, cahead;
2527 * we don't fault-ahead for device pager
2529 if ((object->type == OBJT_DEVICE) ||
2530 (object->type == OBJT_MGTDEVICE)) {
2537 * if the requested page is not available, then give up now
2539 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2540 *reqpage = 0; /* not used by caller, fix compiler warn */
2544 if ((cbehind == 0) && (cahead == 0)) {
2550 if (rahead > cahead) {
2554 if (rbehind > cbehind) {
2559 * Do not do any readahead if we have insufficient free memory.
2561 * XXX code was broken disabled before and has instability
2562 * with this conditonal fixed, so shortcut for now.
2564 if (burst_fault == 0 || vm_page_count_severe()) {
2571 * scan backward for the read behind pages -- in memory
2573 * Assume that if the page is not found an interrupt will not
2574 * create it. Theoretically interrupts can only remove (busy)
2575 * pages, not create new associations.
2578 if (rbehind > pindex) {
2582 startpindex = pindex - rbehind;
2585 vm_object_hold(object);
2586 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2587 if (vm_page_lookup(object, tpindex - 1))
2592 while (tpindex < pindex) {
2593 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2596 for (j = 0; j < i; j++) {
2597 vm_page_free(marray[j]);
2599 vm_object_drop(object);
2608 vm_object_drop(object);
2614 * Assign requested page
2621 * Scan forwards for read-ahead pages
2623 tpindex = pindex + 1;
2624 endpindex = tpindex + rahead;
2625 if (endpindex > object->size)
2626 endpindex = object->size;
2628 vm_object_hold(object);
2629 while (tpindex < endpindex) {
2630 if (vm_page_lookup(object, tpindex))
2632 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2640 vm_object_drop(object);
2648 * vm_prefault() provides a quick way of clustering pagefaults into a
2649 * processes address space. It is a "cousin" of pmap_object_init_pt,
2650 * except it runs at page fault time instead of mmap time.
2652 * vm.fast_fault Enables pre-faulting zero-fill pages
2654 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2655 * prefault. Scan stops in either direction when
2656 * a page is found to already exist.
2658 * This code used to be per-platform pmap_prefault(). It is now
2659 * machine-independent and enhanced to also pre-fault zero-fill pages
2660 * (see vm.fast_fault) as well as make them writable, which greatly
2661 * reduces the number of page faults programs incur.
2663 * Application performance when pre-faulting zero-fill pages is heavily
2664 * dependent on the application. Very tiny applications like /bin/echo
2665 * lose a little performance while applications of any appreciable size
2666 * gain performance. Prefaulting multiple pages also reduces SMP
2667 * congestion and can improve SMP performance significantly.
2669 * NOTE! prot may allow writing but this only applies to the top level
2670 * object. If we wind up mapping a page extracted from a backing
2671 * object we have to make sure it is read-only.
2673 * NOTE! The caller has already handled any COW operations on the
2674 * vm_map_entry via the normal fault code. Do NOT call this
2675 * shortcut unless the normal fault code has run on this entry.
2677 * The related map must be locked.
2678 * No other requirements.
2680 static int vm_prefault_pages = 8;
2681 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2682 "Maximum number of pages to pre-fault");
2683 static int vm_fast_fault = 1;
2684 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2685 "Burst fault zero-fill regions");
2688 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2689 * is not already dirty by other means. This will prevent passive
2690 * filesystem syncing as well as 'sync' from writing out the page.
2693 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2695 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2697 vm_page_flag_set(m, PG_NOSYNC);
2699 vm_page_flag_clear(m, PG_NOSYNC);
2704 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2720 * Get stable max count value, disabled if set to 0
2722 maxpages = vm_prefault_pages;
2728 * We do not currently prefault mappings that use virtual page
2729 * tables. We do not prefault foreign pmaps.
2731 if (entry->maptype != VM_MAPTYPE_NORMAL)
2733 lp = curthread->td_lwp;
2734 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2738 * Limit pre-fault count to 1024 pages.
2740 if (maxpages > 1024)
2743 object = entry->object.vm_object;
2744 KKASSERT(object != NULL);
2745 KKASSERT(object == entry->object.vm_object);
2748 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2749 * now (or do something more complex XXX).
2751 vm_object_hold(object);
2752 vm_object_chain_acquire(object, 0);
2756 for (i = 0; i < maxpages; ++i) {
2757 vm_object_t lobject;
2758 vm_object_t nobject;
2763 * This can eat a lot of time on a heavily contended
2764 * machine so yield on the tick if needed.
2770 * Calculate the page to pre-fault, stopping the scan in
2771 * each direction separately if the limit is reached.
2776 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2780 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2782 if (addr < entry->start) {
2788 if (addr >= entry->end) {
2796 * Skip pages already mapped, and stop scanning in that
2797 * direction. When the scan terminates in both directions
2800 if (pmap_prefault_ok(pmap, addr) == 0) {
2811 * Follow the VM object chain to obtain the page to be mapped
2814 * If we reach the terminal object without finding a page
2815 * and we determine it would be advantageous, then allocate
2816 * a zero-fill page for the base object. The base object
2817 * is guaranteed to be OBJT_DEFAULT for this case.
2819 * In order to not have to check the pager via *haspage*()
2820 * we stop if any non-default object is encountered. e.g.
2821 * a vnode or swap object would stop the loop.
2823 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2828 KKASSERT(lobject == entry->object.vm_object);
2829 /*vm_object_hold(lobject); implied */
2831 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2832 TRUE, &error)) == NULL) {
2833 if (lobject->type != OBJT_DEFAULT)
2835 if (lobject->backing_object == NULL) {
2836 if (vm_fast_fault == 0)
2838 if ((prot & VM_PROT_WRITE) == 0 ||
2839 vm_page_count_min(0)) {
2844 * NOTE: Allocated from base object
2846 m = vm_page_alloc(object, index,
2855 /* lobject = object .. not needed */
2858 if (lobject->backing_object_offset & PAGE_MASK)
2860 nobject = lobject->backing_object;
2861 vm_object_hold(nobject);
2862 KKASSERT(nobject == lobject->backing_object);
2863 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2864 if (lobject != object) {
2865 vm_object_lock_swap();
2866 vm_object_drop(lobject);
2869 pprot &= ~VM_PROT_WRITE;
2870 vm_object_chain_acquire(lobject, 0);
2874 * NOTE: A non-NULL (m) will be associated with lobject if
2875 * it was found there, otherwise it is probably a
2876 * zero-fill page associated with the base object.
2878 * Give-up if no page is available.
2881 if (lobject != object) {
2883 if (object->backing_object != lobject)
2884 vm_object_hold(object->backing_object);
2886 vm_object_chain_release_all(
2887 object->backing_object, lobject);
2889 if (object->backing_object != lobject)
2890 vm_object_drop(object->backing_object);
2892 vm_object_drop(lobject);
2898 * The object must be marked dirty if we are mapping a
2899 * writable page. m->object is either lobject or object,
2900 * both of which are still held. Do this before we
2901 * potentially drop the object.
2903 if (pprot & VM_PROT_WRITE)
2904 vm_object_set_writeable_dirty(m->object);
2907 * Do not conditionalize on PG_RAM. If pages are present in
2908 * the VM system we assume optimal caching. If caching is
2909 * not optimal the I/O gravy train will be restarted when we
2910 * hit an unavailable page. We do not want to try to restart
2911 * the gravy train now because we really don't know how much
2912 * of the object has been cached. The cost for restarting
2913 * the gravy train should be low (since accesses will likely
2914 * be I/O bound anyway).
2916 if (lobject != object) {
2918 if (object->backing_object != lobject)
2919 vm_object_hold(object->backing_object);
2921 vm_object_chain_release_all(object->backing_object,
2924 if (object->backing_object != lobject)
2925 vm_object_drop(object->backing_object);
2927 vm_object_drop(lobject);
2931 * Enter the page into the pmap if appropriate. If we had
2932 * allocated the page we have to place it on a queue. If not
2933 * we just have to make sure it isn't on the cache queue
2934 * (pages on the cache queue are not allowed to be mapped).
2938 * Page must be zerod.
2940 vm_page_zero_fill(m);
2941 mycpu->gd_cnt.v_zfod++;
2942 m->valid = VM_PAGE_BITS_ALL;
2945 * Handle dirty page case
2947 if (pprot & VM_PROT_WRITE)
2948 vm_set_nosync(m, entry);
2949 pmap_enter(pmap, addr, m, pprot, 0, entry);
2950 mycpu->gd_cnt.v_vm_faults++;
2951 if (curthread->td_lwp)
2952 ++curthread->td_lwp->lwp_ru.ru_minflt;
2953 vm_page_deactivate(m);
2954 if (pprot & VM_PROT_WRITE) {
2955 /*vm_object_set_writeable_dirty(m->object);*/
2956 vm_set_nosync(m, entry);
2957 if (fault_flags & VM_FAULT_DIRTY) {
2960 swap_pager_unswapped(m);
2965 /* couldn't busy page, no wakeup */
2967 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2968 (m->flags & PG_FICTITIOUS) == 0) {
2970 * A fully valid page not undergoing soft I/O can
2971 * be immediately entered into the pmap.
2973 if ((m->queue - m->pc) == PQ_CACHE)
2974 vm_page_deactivate(m);
2975 if (pprot & VM_PROT_WRITE) {
2976 /*vm_object_set_writeable_dirty(m->object);*/
2977 vm_set_nosync(m, entry);
2978 if (fault_flags & VM_FAULT_DIRTY) {
2981 swap_pager_unswapped(m);
2984 if (pprot & VM_PROT_WRITE)
2985 vm_set_nosync(m, entry);
2986 pmap_enter(pmap, addr, m, pprot, 0, entry);
2987 mycpu->gd_cnt.v_vm_faults++;
2988 if (curthread->td_lwp)
2989 ++curthread->td_lwp->lwp_ru.ru_minflt;
2995 vm_object_chain_release(object);
2996 vm_object_drop(object);
3000 * Object can be held shared
3003 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
3004 vm_map_entry_t entry, int prot, int fault_flags)
3017 * Get stable max count value, disabled if set to 0
3019 maxpages = vm_prefault_pages;
3025 * We do not currently prefault mappings that use virtual page
3026 * tables. We do not prefault foreign pmaps.
3028 if (entry->maptype != VM_MAPTYPE_NORMAL)
3030 lp = curthread->td_lwp;
3031 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
3033 object = entry->object.vm_object;
3034 if (object->backing_object != NULL)
3036 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
3039 * Limit pre-fault count to 1024 pages.
3041 if (maxpages > 1024)
3046 for (i = 0; i < maxpages; ++i) {
3050 * Calculate the page to pre-fault, stopping the scan in
3051 * each direction separately if the limit is reached.
3056 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
3060 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
3062 if (addr < entry->start) {
3068 if (addr >= entry->end) {
3076 * Follow the VM object chain to obtain the page to be mapped
3077 * into the pmap. This version of the prefault code only
3078 * works with terminal objects.
3080 * The page must already exist. If we encounter a problem
3083 * WARNING! We cannot call swap_pager_unswapped() or insert
3084 * a new vm_page with a shared token.
3086 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
3089 * Skip pages already mapped, and stop scanning in that
3090 * direction. When the scan terminates in both directions
3093 if (pmap_prefault_ok(pmap, addr) == 0) {
3104 * Shortcut the read-only mapping case using the far more
3105 * efficient vm_page_lookup_sbusy_try() function. This
3106 * allows us to acquire the page soft-busied only which
3107 * is especially nice for concurrent execs of the same
3110 * The lookup function also validates page suitability
3111 * (all valid bits set, and not fictitious).
3113 * If the page is in PQ_CACHE we have to fall-through
3114 * and hard-busy it so we can move it out of PQ_CACHE.
3116 if ((prot & VM_PROT_WRITE) == 0) {
3117 m = vm_page_lookup_sbusy_try(object, pindex,
3121 if ((m->queue - m->pc) != PQ_CACHE) {
3122 pmap_enter(pmap, addr, m, prot, 0, entry);
3123 mycpu->gd_cnt.v_vm_faults++;
3124 if (curthread->td_lwp)
3125 ++curthread->td_lwp->lwp_ru.ru_minflt;
3126 vm_page_sbusy_drop(m);
3129 vm_page_sbusy_drop(m);
3133 * Fallback to normal vm_page lookup code. This code
3134 * hard-busies the page. Not only that, but the page
3135 * can remain in that state for a significant period
3136 * time due to pmap_enter()'s overhead.
3138 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
3139 if (m == NULL || error)
3143 * Stop if the page cannot be trivially entered into the
3146 if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) ||
3147 (m->flags & PG_FICTITIOUS) ||
3148 ((m->flags & PG_SWAPPED) &&
3149 (prot & VM_PROT_WRITE) &&
3150 (fault_flags & VM_FAULT_DIRTY))) {
3156 * Enter the page into the pmap. The object might be held
3157 * shared so we can't do any (serious) modifying operation
3160 if ((m->queue - m->pc) == PQ_CACHE)
3161 vm_page_deactivate(m);
3162 if (prot & VM_PROT_WRITE) {
3163 vm_object_set_writeable_dirty(m->object);
3164 vm_set_nosync(m, entry);
3165 if (fault_flags & VM_FAULT_DIRTY) {
3167 /* can't happeen due to conditional above */
3168 /* swap_pager_unswapped(m); */
3171 pmap_enter(pmap, addr, m, prot, 0, entry);
3172 mycpu->gd_cnt.v_vm_faults++;
3173 if (curthread->td_lwp)
3174 ++curthread->td_lwp->lwp_ru.ru_minflt;