4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
12 * This code is derived from software contributed to Berkeley by
13 * The Mach Operating System project at Carnegie-Mellon University.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
51 * Permission to use, copy, modify and distribute this software and
52 * its documentation is hereby granted, provided that both the copyright
53 * notice and this permission notice appear in all copies of the
54 * software, derivative works or modified versions, and any portions
55 * thereof, and that both notices appear in supporting documentation.
57 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 * Carnegie Mellon requests users of this software to return to
63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
64 * School of Computer Science
65 * Carnegie Mellon University
66 * Pittsburgh PA 15213-3890
68 * any improvements or extensions that they make and grant Carnegie the
69 * rights to redistribute these changes.
71 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
72 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon Exp $
76 * Page fault handling module.
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/vnode.h>
84 #include <sys/resourcevar.h>
85 #include <sys/vmmeter.h>
86 #include <sys/vkernel.h>
88 #include <sys/sysctl.h>
90 #include <cpu/lwbuf.h>
93 #include <vm/vm_param.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vnode_pager.h>
102 #include <vm/vm_extern.h>
104 #include <sys/thread2.h>
105 #include <vm/vm_page2.h>
113 vm_object_t first_object;
114 vm_prot_t first_prot;
116 vm_map_entry_t entry;
117 int lookup_still_valid;
125 static int debug_cluster = 0;
126 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
127 static int vm_shared_fault = 0;
128 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW, &vm_shared_fault, 0,
129 "Allow shared token on vm_object");
130 static long vm_shared_hit = 0;
131 SYSCTL_LONG(_vm, OID_AUTO, shared_hit, CTLFLAG_RW, &vm_shared_hit, 0,
132 "Successful shared faults");
133 static long vm_shared_miss = 0;
134 SYSCTL_LONG(_vm, OID_AUTO, shared_miss, CTLFLAG_RW, &vm_shared_miss, 0,
135 "Successful shared faults");
137 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
138 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
140 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
142 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
143 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
144 vm_map_entry_t entry, int prot, int fault_flags);
145 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
146 vm_map_entry_t entry, int prot, int fault_flags);
149 release_page(struct faultstate *fs)
151 vm_page_deactivate(fs->m);
152 vm_page_wakeup(fs->m);
157 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
158 * requires relocking and then checking the timestamp.
160 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
161 * not have to update fs->map_generation here.
163 * NOTE: This function can fail due to a deadlock against the caller's
164 * holding of a vm_page BUSY.
167 relock_map(struct faultstate *fs)
171 if (fs->lookup_still_valid == FALSE && fs->map) {
172 error = vm_map_lock_read_to(fs->map);
174 fs->lookup_still_valid = TRUE;
182 unlock_map(struct faultstate *fs)
184 if (fs->lookup_still_valid && fs->map) {
185 vm_map_lookup_done(fs->map, fs->entry, 0);
186 fs->lookup_still_valid = FALSE;
191 * Clean up after a successful call to vm_fault_object() so another call
192 * to vm_fault_object() can be made.
195 _cleanup_successful_fault(struct faultstate *fs, int relock)
197 if (fs->object != fs->first_object) {
198 vm_page_free(fs->first_m);
199 vm_object_pip_wakeup(fs->object);
202 fs->object = fs->first_object;
203 if (relock && fs->lookup_still_valid == FALSE) {
205 vm_map_lock_read(fs->map);
206 fs->lookup_still_valid = TRUE;
211 _unlock_things(struct faultstate *fs, int dealloc)
213 _cleanup_successful_fault(fs, 0);
215 /*vm_object_deallocate(fs->first_object);*/
216 /*fs->first_object = NULL; drop used later on */
219 if (fs->vp != NULL) {
225 #define unlock_things(fs) _unlock_things(fs, 0)
226 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
227 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
232 * Determine if the pager for the current object *might* contain the page.
234 * We only need to try the pager if this is not a default object (default
235 * objects are zero-fill and have no real pager), and if we are not taking
236 * a wiring fault or if the FS entry is wired.
238 #define TRYPAGER(fs) \
239 (fs->object->type != OBJT_DEFAULT && \
240 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
245 * Handle a page fault occuring at the given address, requiring the given
246 * permissions, in the map specified. If successful, the page is inserted
247 * into the associated physical map.
249 * NOTE: The given address should be truncated to the proper page address.
251 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
252 * a standard error specifying why the fault is fatal is returned.
254 * The map in question must be referenced, and remains so.
255 * The caller may hold no locks.
256 * No other requirements.
259 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
262 vm_pindex_t first_pindex;
263 struct faultstate fs;
267 vm_page_pcpu_cache();
269 fs.fault_flags = fault_flags;
273 if ((lp = curthread->td_lwp) != NULL)
274 lp->lwp_flags |= LWP_PAGING;
276 lwkt_gettoken(&map->token);
280 * Find the vm_map_entry representing the backing store and resolve
281 * the top level object and page index. This may have the side
282 * effect of executing a copy-on-write on the map entry and/or
283 * creating a shadow object, but will not COW any actual VM pages.
285 * On success fs.map is left read-locked and various other fields
286 * are initialized but not otherwise referenced or locked.
288 * NOTE! vm_map_lookup will try to upgrade the fault_type to
289 * VM_FAULT_WRITE if the map entry is a virtual page table and also
290 * writable, so we can set the 'A'accessed bit in the virtual page
294 result = vm_map_lookup(&fs.map, vaddr, fault_type,
295 &fs.entry, &fs.first_object,
296 &first_pindex, &fs.first_prot, &fs.wired);
299 * If the lookup failed or the map protections are incompatible,
300 * the fault generally fails. However, if the caller is trying
301 * to do a user wiring we have more work to do.
303 if (result != KERN_SUCCESS) {
304 if (result != KERN_PROTECTION_FAILURE ||
305 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
307 if (result == KERN_INVALID_ADDRESS && growstack &&
308 map != &kernel_map && curproc != NULL) {
309 result = vm_map_growstack(curproc, vaddr);
310 if (result == KERN_SUCCESS) {
314 result = KERN_FAILURE;
320 * If we are user-wiring a r/w segment, and it is COW, then
321 * we need to do the COW operation. Note that we don't
322 * currently COW RO sections now, because it is NOT desirable
323 * to COW .text. We simply keep .text from ever being COW'ed
324 * and take the heat that one cannot debug wired .text sections.
326 result = vm_map_lookup(&fs.map, vaddr,
327 VM_PROT_READ|VM_PROT_WRITE|
328 VM_PROT_OVERRIDE_WRITE,
329 &fs.entry, &fs.first_object,
330 &first_pindex, &fs.first_prot,
332 if (result != KERN_SUCCESS) {
333 result = KERN_FAILURE;
338 * If we don't COW now, on a user wire, the user will never
339 * be able to write to the mapping. If we don't make this
340 * restriction, the bookkeeping would be nearly impossible.
342 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
343 fs.entry->max_protection &= ~VM_PROT_WRITE;
347 * fs.map is read-locked
349 * Misc checks. Save the map generation number to detect races.
351 fs.map_generation = fs.map->timestamp;
353 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
354 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
355 panic("vm_fault: fault on nofault entry, addr: %p",
358 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
359 vaddr >= fs.entry->start &&
360 vaddr < fs.entry->start + PAGE_SIZE) {
361 panic("vm_fault: fault on stack guard, addr: %p",
367 * A system map entry may return a NULL object. No object means
368 * no pager means an unrecoverable kernel fault.
370 if (fs.first_object == NULL) {
371 panic("vm_fault: unrecoverable fault at %p in entry %p",
372 (void *)vaddr, fs.entry);
376 * Attempt to shortcut the fault if the lookup returns a
377 * terminal object and the page is present. This allows us
378 * to obtain a shared token on the object instead of an exclusive
379 * token, which theoretically should allow concurrent faults.
381 if (vm_shared_fault &&
382 fs.first_object->backing_object == NULL &&
383 fs.entry->maptype == VM_MAPTYPE_NORMAL) {
385 vm_object_hold_shared(fs.first_object);
386 /*fs.vp = vnode_pager_lock(fs.first_object);*/
387 fs.m = vm_page_lookup_busy_try(fs.first_object,
390 if (error == 0 && fs.m) {
392 * Activate the page and figure out if we can
393 * short-cut a quick mapping.
395 * WARNING! We cannot call swap_pager_unswapped()
396 * with a shared token! Note that we
397 * have to test fs.first_prot here.
399 vm_page_activate(fs.m);
400 if (fs.m->valid == VM_PAGE_BITS_ALL &&
401 ((fs.m->flags & PG_SWAPPED) == 0 ||
402 (fs.first_prot & VM_PROT_WRITE) == 0 ||
403 (fs.fault_flags & VM_FAULT_DIRTY) == 0)) {
404 fs.lookup_still_valid = TRUE;
406 fs.object = fs.first_object;
407 fs.prot = fs.first_prot;
409 fault_type = fs.first_prot;
410 if (fs.prot & VM_PROT_WRITE) {
411 vm_object_set_writeable_dirty(
413 vm_set_nosync(fs.m, fs.entry);
414 if (fs.fault_flags & VM_FAULT_DIRTY) {
417 swap_pager_unswapped(fs.m);
420 result = KERN_SUCCESS;
421 fault_flags |= VM_FAULT_BURST_QUICK;
422 fault_flags &= ~VM_FAULT_BURST;
426 vm_page_wakeup(fs.m);
429 vm_object_drop(fs.first_object); /* XXX drop on shared tok?*/
434 * Bump the paging-in-progress count to prevent size changes (e.g.
435 * truncation operations) during I/O. This must be done after
436 * obtaining the vnode lock in order to avoid possible deadlocks.
438 vm_object_hold(fs.first_object);
440 fs.vp = vnode_pager_lock(fs.first_object);
442 fs.lookup_still_valid = TRUE;
444 fs.object = fs.first_object; /* so unlock_and_deallocate works */
447 * If the entry is wired we cannot change the page protection.
450 fault_type = fs.first_prot;
453 * The page we want is at (first_object, first_pindex), but if the
454 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
455 * page table to figure out the actual pindex.
457 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
460 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
461 result = vm_fault_vpagetable(&fs, &first_pindex,
462 fs.entry->aux.master_pde,
464 if (result == KERN_TRY_AGAIN) {
465 vm_object_drop(fs.first_object);
468 if (result != KERN_SUCCESS)
473 * Now we have the actual (object, pindex), fault in the page. If
474 * vm_fault_object() fails it will unlock and deallocate the FS
475 * data. If it succeeds everything remains locked and fs->object
476 * will have an additional PIP count if it is not equal to
479 * vm_fault_object will set fs->prot for the pmap operation. It is
480 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
481 * page can be safely written. However, it will force a read-only
482 * mapping for a read fault if the memory is managed by a virtual
486 result = vm_fault_object(&fs, first_pindex, fault_type);
488 if (result == KERN_TRY_AGAIN) {
489 vm_object_drop(fs.first_object);
492 if (result != KERN_SUCCESS)
497 * On success vm_fault_object() does not unlock or deallocate, and fs.m
498 * will contain a busied page.
500 * Enter the page into the pmap and do pmap-related adjustments.
502 vm_page_flag_set(fs.m, PG_REFERENCED);
503 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
504 mycpu->gd_cnt.v_vm_faults++;
505 if (curthread->td_lwp)
506 ++curthread->td_lwp->lwp_ru.ru_minflt;
508 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
509 KKASSERT(fs.m->flags & PG_BUSY);
512 * If the page is not wired down, then put it where the pageout daemon
515 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
519 vm_page_unwire(fs.m, 1);
521 vm_page_activate(fs.m);
523 vm_page_wakeup(fs.m);
526 * Burst in a few more pages if possible. The fs.map should still
527 * be locked. To avoid interlocking against a vnode->getblk
528 * operation we had to be sure to unbusy our primary vm_page above
531 if (fault_flags & VM_FAULT_BURST) {
532 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
534 vm_prefault(fs.map->pmap, vaddr,
535 fs.entry, fs.prot, fault_flags);
538 if (fault_flags & VM_FAULT_BURST_QUICK) {
539 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
541 vm_prefault_quick(fs.map->pmap, vaddr,
542 fs.entry, fs.prot, fault_flags);
547 * Unlock everything, and return
551 if (curthread->td_lwp) {
553 curthread->td_lwp->lwp_ru.ru_majflt++;
555 curthread->td_lwp->lwp_ru.ru_minflt++;
559 /*vm_object_deallocate(fs.first_object);*/
561 /*fs.first_object = NULL; must still drop later */
563 result = KERN_SUCCESS;
566 vm_object_drop(fs.first_object);
567 lwkt_reltoken(&map->token);
569 lp->lwp_flags &= ~LWP_PAGING;
574 * Fault in the specified virtual address in the current process map,
575 * returning a held VM page or NULL. See vm_fault_page() for more
581 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
583 struct lwp *lp = curthread->td_lwp;
586 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
587 fault_type, VM_FAULT_NORMAL, errorp);
592 * Fault in the specified virtual address in the specified map, doing all
593 * necessary manipulation of the object store and all necessary I/O. Return
594 * a held VM page or NULL, and set *errorp. The related pmap is not
597 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
598 * and marked PG_REFERENCED as well.
600 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
601 * error will be returned.
606 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
607 int fault_flags, int *errorp)
609 vm_pindex_t first_pindex;
610 struct faultstate fs;
612 vm_prot_t orig_fault_type = fault_type;
615 fs.fault_flags = fault_flags;
616 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
618 lwkt_gettoken(&map->token);
622 * Find the vm_map_entry representing the backing store and resolve
623 * the top level object and page index. This may have the side
624 * effect of executing a copy-on-write on the map entry and/or
625 * creating a shadow object, but will not COW any actual VM pages.
627 * On success fs.map is left read-locked and various other fields
628 * are initialized but not otherwise referenced or locked.
630 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
631 * if the map entry is a virtual page table and also writable,
632 * so we can set the 'A'accessed bit in the virtual page table entry.
635 result = vm_map_lookup(&fs.map, vaddr, fault_type,
636 &fs.entry, &fs.first_object,
637 &first_pindex, &fs.first_prot, &fs.wired);
639 if (result != KERN_SUCCESS) {
646 * fs.map is read-locked
648 * Misc checks. Save the map generation number to detect races.
650 fs.map_generation = fs.map->timestamp;
652 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
653 panic("vm_fault: fault on nofault entry, addr: %lx",
658 * A system map entry may return a NULL object. No object means
659 * no pager means an unrecoverable kernel fault.
661 if (fs.first_object == NULL) {
662 panic("vm_fault: unrecoverable fault at %p in entry %p",
663 (void *)vaddr, fs.entry);
667 * Make a reference to this object to prevent its disposal while we
668 * are messing with it. Once we have the reference, the map is free
669 * to be diddled. Since objects reference their shadows (and copies),
670 * they will stay around as well.
672 * The reference should also prevent an unexpected collapse of the
673 * parent that might move pages from the current object into the
674 * parent unexpectedly, resulting in corruption.
676 * Bump the paging-in-progress count to prevent size changes (e.g.
677 * truncation operations) during I/O. This must be done after
678 * obtaining the vnode lock in order to avoid possible deadlocks.
680 vm_object_hold(fs.first_object);
681 fs.vp = vnode_pager_lock(fs.first_object);
683 fs.lookup_still_valid = TRUE;
685 fs.object = fs.first_object; /* so unlock_and_deallocate works */
688 * If the entry is wired we cannot change the page protection.
691 fault_type = fs.first_prot;
694 * The page we want is at (first_object, first_pindex), but if the
695 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
696 * page table to figure out the actual pindex.
698 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
701 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
702 result = vm_fault_vpagetable(&fs, &first_pindex,
703 fs.entry->aux.master_pde,
705 if (result == KERN_TRY_AGAIN) {
706 vm_object_drop(fs.first_object);
709 if (result != KERN_SUCCESS) {
717 * Now we have the actual (object, pindex), fault in the page. If
718 * vm_fault_object() fails it will unlock and deallocate the FS
719 * data. If it succeeds everything remains locked and fs->object
720 * will have an additinal PIP count if it is not equal to
724 result = vm_fault_object(&fs, first_pindex, fault_type);
726 if (result == KERN_TRY_AGAIN) {
727 vm_object_drop(fs.first_object);
730 if (result != KERN_SUCCESS) {
736 if ((orig_fault_type & VM_PROT_WRITE) &&
737 (fs.prot & VM_PROT_WRITE) == 0) {
738 *errorp = KERN_PROTECTION_FAILURE;
739 unlock_and_deallocate(&fs);
745 * DO NOT UPDATE THE PMAP!!! This function may be called for
746 * a pmap unrelated to the current process pmap, in which case
747 * the current cpu core will not be listed in the pmap's pm_active
748 * mask. Thus invalidation interlocks will fail to work properly.
750 * (for example, 'ps' uses procfs to read program arguments from
751 * each process's stack).
753 * In addition to the above this function will be called to acquire
754 * a page that might already be faulted in, re-faulting it
755 * continuously is a waste of time.
757 * XXX could this have been the cause of our random seg-fault
758 * issues? procfs accesses user stacks.
760 vm_page_flag_set(fs.m, PG_REFERENCED);
762 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
763 mycpu->gd_cnt.v_vm_faults++;
764 if (curthread->td_lwp)
765 ++curthread->td_lwp->lwp_ru.ru_minflt;
769 * On success vm_fault_object() does not unlock or deallocate, and fs.m
770 * will contain a busied page. So we must unlock here after having
771 * messed with the pmap.
776 * Return a held page. We are not doing any pmap manipulation so do
777 * not set PG_MAPPED. However, adjust the page flags according to
778 * the fault type because the caller may not use a managed pmapping
779 * (so we don't want to lose the fact that the page will be dirtied
780 * if a write fault was specified).
783 vm_page_activate(fs.m);
784 if (fault_type & VM_PROT_WRITE)
787 if (curthread->td_lwp) {
789 curthread->td_lwp->lwp_ru.ru_majflt++;
791 curthread->td_lwp->lwp_ru.ru_minflt++;
796 * Unlock everything, and return the held page.
798 vm_page_wakeup(fs.m);
799 /*vm_object_deallocate(fs.first_object);*/
800 /*fs.first_object = NULL; */
805 vm_object_drop(fs.first_object);
806 lwkt_reltoken(&map->token);
811 * Fault in the specified (object,offset), dirty the returned page as
812 * needed. If the requested fault_type cannot be done NULL and an
815 * A held (but not busied) page is returned.
820 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
821 vm_prot_t fault_type, int fault_flags, int *errorp)
824 vm_pindex_t first_pindex;
825 struct faultstate fs;
826 struct vm_map_entry entry;
828 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
829 bzero(&entry, sizeof(entry));
830 entry.object.vm_object = object;
831 entry.maptype = VM_MAPTYPE_NORMAL;
832 entry.protection = entry.max_protection = fault_type;
835 fs.fault_flags = fault_flags;
837 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
841 fs.first_object = object;
842 first_pindex = OFF_TO_IDX(offset);
844 fs.first_prot = fault_type;
846 /*fs.map_generation = 0; unused */
849 * Make a reference to this object to prevent its disposal while we
850 * are messing with it. Once we have the reference, the map is free
851 * to be diddled. Since objects reference their shadows (and copies),
852 * they will stay around as well.
854 * The reference should also prevent an unexpected collapse of the
855 * parent that might move pages from the current object into the
856 * parent unexpectedly, resulting in corruption.
858 * Bump the paging-in-progress count to prevent size changes (e.g.
859 * truncation operations) during I/O. This must be done after
860 * obtaining the vnode lock in order to avoid possible deadlocks.
862 fs.vp = vnode_pager_lock(fs.first_object);
864 fs.lookup_still_valid = TRUE;
866 fs.object = fs.first_object; /* so unlock_and_deallocate works */
869 /* XXX future - ability to operate on VM object using vpagetable */
870 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
871 result = vm_fault_vpagetable(&fs, &first_pindex,
872 fs.entry->aux.master_pde,
874 if (result == KERN_TRY_AGAIN)
876 if (result != KERN_SUCCESS) {
884 * Now we have the actual (object, pindex), fault in the page. If
885 * vm_fault_object() fails it will unlock and deallocate the FS
886 * data. If it succeeds everything remains locked and fs->object
887 * will have an additinal PIP count if it is not equal to
890 result = vm_fault_object(&fs, first_pindex, fault_type);
892 if (result == KERN_TRY_AGAIN)
894 if (result != KERN_SUCCESS) {
899 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
900 *errorp = KERN_PROTECTION_FAILURE;
901 unlock_and_deallocate(&fs);
906 * On success vm_fault_object() does not unlock or deallocate, so we
907 * do it here. Note that the returned fs.m will be busied.
912 * Return a held page. We are not doing any pmap manipulation so do
913 * not set PG_MAPPED. However, adjust the page flags according to
914 * the fault type because the caller may not use a managed pmapping
915 * (so we don't want to lose the fact that the page will be dirtied
916 * if a write fault was specified).
919 vm_page_activate(fs.m);
920 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
922 if (fault_flags & VM_FAULT_UNSWAP)
923 swap_pager_unswapped(fs.m);
926 * Indicate that the page was accessed.
928 vm_page_flag_set(fs.m, PG_REFERENCED);
930 if (curthread->td_lwp) {
932 curthread->td_lwp->lwp_ru.ru_majflt++;
934 curthread->td_lwp->lwp_ru.ru_minflt++;
939 * Unlock everything, and return the held page.
941 vm_page_wakeup(fs.m);
942 /*vm_object_deallocate(fs.first_object);*/
943 /*fs.first_object = NULL; */
950 * Translate the virtual page number (first_pindex) that is relative
951 * to the address space into a logical page number that is relative to the
952 * backing object. Use the virtual page table pointed to by (vpte).
954 * This implements an N-level page table. Any level can terminate the
955 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
956 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
960 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
961 vpte_t vpte, int fault_type)
964 struct lwbuf lwb_cache;
965 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
966 int result = KERN_SUCCESS;
969 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
972 * We cannot proceed if the vpte is not valid, not readable
973 * for a read fault, or not writable for a write fault.
975 if ((vpte & VPTE_V) == 0) {
976 unlock_and_deallocate(fs);
977 return (KERN_FAILURE);
979 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
980 unlock_and_deallocate(fs);
981 return (KERN_FAILURE);
983 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
984 unlock_and_deallocate(fs);
985 return (KERN_FAILURE);
987 if ((vpte & VPTE_PS) || vshift == 0)
989 KKASSERT(vshift >= VPTE_PAGE_BITS);
992 * Get the page table page. Nominally we only read the page
993 * table, but since we are actively setting VPTE_M and VPTE_A,
994 * tell vm_fault_object() that we are writing it.
996 * There is currently no real need to optimize this.
998 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
999 VM_PROT_READ|VM_PROT_WRITE);
1000 if (result != KERN_SUCCESS)
1004 * Process the returned fs.m and look up the page table
1005 * entry in the page table page.
1007 vshift -= VPTE_PAGE_BITS;
1008 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1009 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1010 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1014 * Page table write-back. If the vpte is valid for the
1015 * requested operation, do a write-back to the page table.
1017 * XXX VPTE_M is not set properly for page directory pages.
1018 * It doesn't get set in the page directory if the page table
1019 * is modified during a read access.
1021 vm_page_activate(fs->m);
1022 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
1024 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
1025 atomic_set_long(ptep, VPTE_M | VPTE_A);
1026 vm_page_dirty(fs->m);
1029 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
1031 if ((vpte & VPTE_A) == 0) {
1032 atomic_set_long(ptep, VPTE_A);
1033 vm_page_dirty(fs->m);
1037 vm_page_flag_set(fs->m, PG_REFERENCED);
1038 vm_page_wakeup(fs->m);
1040 cleanup_successful_fault(fs);
1043 * Combine remaining address bits with the vpte.
1045 /* JG how many bits from each? */
1046 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1047 (*pindex & ((1L << vshift) - 1));
1048 return (KERN_SUCCESS);
1053 * This is the core of the vm_fault code.
1055 * Do all operations required to fault-in (fs.first_object, pindex). Run
1056 * through the shadow chain as necessary and do required COW or virtual
1057 * copy operations. The caller has already fully resolved the vm_map_entry
1058 * and, if appropriate, has created a copy-on-write layer. All we need to
1059 * do is iterate the object chain.
1061 * On failure (fs) is unlocked and deallocated and the caller may return or
1062 * retry depending on the failure code. On success (fs) is NOT unlocked or
1063 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1064 * will have an additional PIP count if it is not equal to fs.first_object.
1066 * fs->first_object must be held on call.
1070 vm_fault_object(struct faultstate *fs,
1071 vm_pindex_t first_pindex, vm_prot_t fault_type)
1073 vm_object_t next_object;
1077 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1078 fs->prot = fs->first_prot;
1079 fs->object = fs->first_object;
1080 pindex = first_pindex;
1082 vm_object_chain_acquire(fs->first_object);
1083 vm_object_pip_add(fs->first_object, 1);
1086 * If a read fault occurs we try to make the page writable if
1087 * possible. There are three cases where we cannot make the
1088 * page mapping writable:
1090 * (1) The mapping is read-only or the VM object is read-only,
1091 * fs->prot above will simply not have VM_PROT_WRITE set.
1093 * (2) If the mapping is a virtual page table we need to be able
1094 * to detect writes so we can set VPTE_M in the virtual page
1097 * (3) If the VM page is read-only or copy-on-write, upgrading would
1098 * just result in an unnecessary COW fault.
1100 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1101 * causes adjustments to the 'M'odify bit to also turn off write
1102 * access to force a re-fault.
1104 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1105 if ((fault_type & VM_PROT_WRITE) == 0)
1106 fs->prot &= ~VM_PROT_WRITE;
1109 /* vm_object_hold(fs->object); implied b/c object == first_object */
1113 * The entire backing chain from first_object to object
1114 * inclusive is chainlocked.
1116 * If the object is dead, we stop here
1118 if (fs->object->flags & OBJ_DEAD) {
1119 vm_object_pip_wakeup(fs->first_object);
1120 vm_object_chain_release_all(fs->first_object,
1122 if (fs->object != fs->first_object)
1123 vm_object_drop(fs->object);
1124 unlock_and_deallocate(fs);
1125 return (KERN_PROTECTION_FAILURE);
1129 * See if the page is resident. Wait/Retry if the page is
1130 * busy (lots of stuff may have changed so we can't continue
1133 * We can theoretically allow the soft-busy case on a read
1134 * fault if the page is marked valid, but since such
1135 * pages are typically already pmap'd, putting that
1136 * special case in might be more effort then it is
1137 * worth. We cannot under any circumstances mess
1138 * around with a vm_page_t->busy page except, perhaps,
1141 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1144 vm_object_pip_wakeup(fs->first_object);
1145 vm_object_chain_release_all(fs->first_object,
1147 if (fs->object != fs->first_object)
1148 vm_object_drop(fs->object);
1150 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1151 mycpu->gd_cnt.v_intrans++;
1152 /*vm_object_deallocate(fs->first_object);*/
1153 /*fs->first_object = NULL;*/
1155 return (KERN_TRY_AGAIN);
1159 * The page is busied for us.
1161 * If reactivating a page from PQ_CACHE we may have
1164 int queue = fs->m->queue;
1165 vm_page_unqueue_nowakeup(fs->m);
1167 if ((queue - fs->m->pc) == PQ_CACHE &&
1168 vm_page_count_severe()) {
1169 vm_page_activate(fs->m);
1170 vm_page_wakeup(fs->m);
1172 vm_object_pip_wakeup(fs->first_object);
1173 vm_object_chain_release_all(fs->first_object,
1175 if (fs->object != fs->first_object)
1176 vm_object_drop(fs->object);
1177 unlock_and_deallocate(fs);
1179 return (KERN_TRY_AGAIN);
1183 * If it still isn't completely valid (readable),
1184 * or if a read-ahead-mark is set on the VM page,
1185 * jump to readrest, else we found the page and
1188 * We can release the spl once we have marked the
1191 if (fs->m->object != &kernel_object) {
1192 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1196 if (fs->m->flags & PG_RAM) {
1199 vm_page_flag_clear(fs->m, PG_RAM);
1203 break; /* break to PAGE HAS BEEN FOUND */
1207 * Page is not resident, If this is the search termination
1208 * or the pager might contain the page, allocate a new page.
1210 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1212 * If the page is beyond the object size we fail
1214 if (pindex >= fs->object->size) {
1215 vm_object_pip_wakeup(fs->first_object);
1216 vm_object_chain_release_all(fs->first_object,
1218 if (fs->object != fs->first_object)
1219 vm_object_drop(fs->object);
1220 unlock_and_deallocate(fs);
1221 return (KERN_PROTECTION_FAILURE);
1225 * Allocate a new page for this object/offset pair.
1227 * It is possible for the allocation to race, so
1231 if (!vm_page_count_severe()) {
1232 fs->m = vm_page_alloc(fs->object, pindex,
1233 ((fs->vp || fs->object->backing_object) ?
1234 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1235 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1236 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1238 if (fs->m == NULL) {
1239 vm_object_pip_wakeup(fs->first_object);
1240 vm_object_chain_release_all(fs->first_object,
1242 if (fs->object != fs->first_object)
1243 vm_object_drop(fs->object);
1244 unlock_and_deallocate(fs);
1246 return (KERN_TRY_AGAIN);
1250 * Fall through to readrest. We have a new page which
1251 * will have to be paged (since m->valid will be 0).
1257 * We have found an invalid or partially valid page, a
1258 * page with a read-ahead mark which might be partially or
1259 * fully valid (and maybe dirty too), or we have allocated
1262 * Attempt to fault-in the page if there is a chance that the
1263 * pager has it, and potentially fault in additional pages
1266 * If TRYPAGER is true then fs.m will be non-NULL and busied
1272 u_char behavior = vm_map_entry_behavior(fs->entry);
1274 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1281 * If sequential access is detected then attempt
1282 * to deactivate/cache pages behind the scan to
1283 * prevent resource hogging.
1285 * Use of PG_RAM to detect sequential access
1286 * also simulates multi-zone sequential access
1287 * detection for free.
1289 * NOTE: Partially valid dirty pages cannot be
1290 * deactivated without causing NFS picemeal
1293 if ((fs->first_object->type != OBJT_DEVICE) &&
1294 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1295 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1296 (fs->m->flags & PG_RAM)))
1298 vm_pindex_t scan_pindex;
1299 int scan_count = 16;
1301 if (first_pindex < 16) {
1305 scan_pindex = first_pindex - 16;
1306 if (scan_pindex < 16)
1307 scan_count = scan_pindex;
1312 while (scan_count) {
1315 mt = vm_page_lookup(fs->first_object,
1319 if (vm_page_busy_try(mt, TRUE))
1322 if (mt->valid != VM_PAGE_BITS_ALL) {
1327 (PG_FICTITIOUS | PG_UNMANAGED)) ||
1334 vm_page_test_dirty(mt);
1338 vm_page_deactivate(mt);
1353 * Avoid deadlocking against the map when doing I/O.
1354 * fs.object and the page is PG_BUSY'd.
1356 * NOTE: Once unlocked, fs->entry can become stale
1357 * so this will NULL it out.
1359 * NOTE: fs->entry is invalid until we relock the
1360 * map and verify that the timestamp has not
1366 * Acquire the page data. We still hold a ref on
1367 * fs.object and the page has been PG_BUSY's.
1369 * The pager may replace the page (for example, in
1370 * order to enter a fictitious page into the
1371 * object). If it does so it is responsible for
1372 * cleaning up the passed page and properly setting
1373 * the new page PG_BUSY.
1375 * If we got here through a PG_RAM read-ahead
1376 * mark the page may be partially dirty and thus
1377 * not freeable. Don't bother checking to see
1378 * if the pager has the page because we can't free
1379 * it anyway. We have to depend on the get_page
1380 * operation filling in any gaps whether there is
1381 * backing store or not.
1383 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1385 if (rv == VM_PAGER_OK) {
1387 * Relookup in case pager changed page. Pager
1388 * is responsible for disposition of old page
1391 * XXX other code segments do relookups too.
1392 * It's a bad abstraction that needs to be
1395 fs->m = vm_page_lookup(fs->object, pindex);
1396 if (fs->m == NULL) {
1397 vm_object_pip_wakeup(fs->first_object);
1398 vm_object_chain_release_all(
1399 fs->first_object, fs->object);
1400 if (fs->object != fs->first_object)
1401 vm_object_drop(fs->object);
1402 unlock_and_deallocate(fs);
1403 return (KERN_TRY_AGAIN);
1407 break; /* break to PAGE HAS BEEN FOUND */
1411 * Remove the bogus page (which does not exist at this
1412 * object/offset); before doing so, we must get back
1413 * our object lock to preserve our invariant.
1415 * Also wake up any other process that may want to bring
1418 * If this is the top-level object, we must leave the
1419 * busy page to prevent another process from rushing
1420 * past us, and inserting the page in that object at
1421 * the same time that we are.
1423 if (rv == VM_PAGER_ERROR) {
1425 kprintf("vm_fault: pager read error, "
1430 kprintf("vm_fault: pager read error, "
1438 * Data outside the range of the pager or an I/O error
1440 * The page may have been wired during the pagein,
1441 * e.g. by the buffer cache, and cannot simply be
1442 * freed. Call vnode_pager_freepage() to deal with it.
1445 * XXX - the check for kernel_map is a kludge to work
1446 * around having the machine panic on a kernel space
1447 * fault w/ I/O error.
1449 if (((fs->map != &kernel_map) &&
1450 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1451 vnode_pager_freepage(fs->m);
1453 vm_object_pip_wakeup(fs->first_object);
1454 vm_object_chain_release_all(fs->first_object,
1456 if (fs->object != fs->first_object)
1457 vm_object_drop(fs->object);
1458 unlock_and_deallocate(fs);
1459 if (rv == VM_PAGER_ERROR)
1460 return (KERN_FAILURE);
1462 return (KERN_PROTECTION_FAILURE);
1465 if (fs->object != fs->first_object) {
1466 vnode_pager_freepage(fs->m);
1469 * XXX - we cannot just fall out at this
1470 * point, m has been freed and is invalid!
1476 * We get here if the object has a default pager (or unwiring)
1477 * or the pager doesn't have the page.
1479 if (fs->object == fs->first_object)
1480 fs->first_m = fs->m;
1483 * Move on to the next object. The chain lock should prevent
1484 * the backing_object from getting ripped out from under us.
1486 if ((next_object = fs->object->backing_object) != NULL) {
1487 vm_object_hold(next_object);
1488 vm_object_chain_acquire(next_object);
1489 KKASSERT(next_object == fs->object->backing_object);
1490 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1493 if (next_object == NULL) {
1495 * If there's no object left, fill the page in the top
1496 * object with zeros.
1498 if (fs->object != fs->first_object) {
1499 if (fs->first_object->backing_object !=
1501 vm_object_hold(fs->first_object->backing_object);
1503 vm_object_chain_release_all(
1504 fs->first_object->backing_object,
1506 if (fs->first_object->backing_object !=
1508 vm_object_drop(fs->first_object->backing_object);
1510 vm_object_pip_wakeup(fs->object);
1511 vm_object_drop(fs->object);
1512 fs->object = fs->first_object;
1513 pindex = first_pindex;
1514 fs->m = fs->first_m;
1519 * Zero the page if necessary and mark it valid.
1521 if ((fs->m->flags & PG_ZERO) == 0) {
1522 vm_page_zero_fill(fs->m);
1525 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1527 vm_page_flag_clear(fs->m, PG_ZERO);
1528 mycpu->gd_cnt.v_ozfod++;
1530 mycpu->gd_cnt.v_zfod++;
1531 fs->m->valid = VM_PAGE_BITS_ALL;
1532 break; /* break to PAGE HAS BEEN FOUND */
1534 if (fs->object != fs->first_object) {
1535 vm_object_pip_wakeup(fs->object);
1536 vm_object_lock_swap();
1537 vm_object_drop(fs->object);
1539 KASSERT(fs->object != next_object,
1540 ("object loop %p", next_object));
1541 fs->object = next_object;
1542 vm_object_pip_add(fs->object, 1);
1546 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1549 * object still held.
1551 * If the page is being written, but isn't already owned by the
1552 * top-level object, we have to copy it into a new page owned by the
1555 KASSERT((fs->m->flags & PG_BUSY) != 0,
1556 ("vm_fault: not busy after main loop"));
1558 if (fs->object != fs->first_object) {
1560 * We only really need to copy if we want to write it.
1562 if (fault_type & VM_PROT_WRITE) {
1564 * This allows pages to be virtually copied from a
1565 * backing_object into the first_object, where the
1566 * backing object has no other refs to it, and cannot
1567 * gain any more refs. Instead of a bcopy, we just
1568 * move the page from the backing object to the
1569 * first object. Note that we must mark the page
1570 * dirty in the first object so that it will go out
1571 * to swap when needed.
1575 * Map, if present, has not changed
1578 fs->map_generation == fs->map->timestamp) &&
1580 * Only one shadow object
1582 (fs->object->shadow_count == 1) &&
1584 * No COW refs, except us
1586 (fs->object->ref_count == 1) &&
1588 * No one else can look this object up
1590 (fs->object->handle == NULL) &&
1592 * No other ways to look the object up
1594 ((fs->object->type == OBJT_DEFAULT) ||
1595 (fs->object->type == OBJT_SWAP)) &&
1597 * We don't chase down the shadow chain
1599 (fs->object == fs->first_object->backing_object) &&
1602 * grab the lock if we need to
1604 (fs->lookup_still_valid ||
1606 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1609 * (first_m) and (m) are both busied. We have
1610 * move (m) into (first_m)'s object/pindex
1611 * in an atomic fashion, then free (first_m).
1613 * first_object is held so second remove
1614 * followed by the rename should wind
1615 * up being atomic. vm_page_free() might
1616 * block so we don't do it until after the
1619 fs->lookup_still_valid = 1;
1620 vm_page_protect(fs->first_m, VM_PROT_NONE);
1621 vm_page_remove(fs->first_m);
1622 vm_page_rename(fs->m, fs->first_object,
1624 vm_page_free(fs->first_m);
1625 fs->first_m = fs->m;
1627 mycpu->gd_cnt.v_cow_optim++;
1630 * Oh, well, lets copy it.
1632 * Why are we unmapping the original page
1633 * here? Well, in short, not all accessors
1634 * of user memory go through the pmap. The
1635 * procfs code doesn't have access user memory
1636 * via a local pmap, so vm_fault_page*()
1637 * can't call pmap_enter(). And the umtx*()
1638 * code may modify the COW'd page via a DMAP
1639 * or kernel mapping and not via the pmap,
1640 * leaving the original page still mapped
1641 * read-only into the pmap.
1643 * So we have to remove the page from at
1644 * least the current pmap if it is in it.
1645 * Just remove it from all pmaps.
1647 vm_page_copy(fs->m, fs->first_m);
1648 vm_page_protect(fs->m, VM_PROT_NONE);
1649 vm_page_event(fs->m, VMEVENT_COW);
1654 * We no longer need the old page or object.
1660 * We intend to revert to first_object, undo the
1661 * chain lock through to that.
1663 if (fs->first_object->backing_object != fs->object)
1664 vm_object_hold(fs->first_object->backing_object);
1665 vm_object_chain_release_all(
1666 fs->first_object->backing_object,
1668 if (fs->first_object->backing_object != fs->object)
1669 vm_object_drop(fs->first_object->backing_object);
1672 * fs->object != fs->first_object due to above
1675 vm_object_pip_wakeup(fs->object);
1676 vm_object_drop(fs->object);
1679 * Only use the new page below...
1682 mycpu->gd_cnt.v_cow_faults++;
1683 fs->m = fs->first_m;
1684 fs->object = fs->first_object;
1685 pindex = first_pindex;
1688 * If it wasn't a write fault avoid having to copy
1689 * the page by mapping it read-only.
1691 fs->prot &= ~VM_PROT_WRITE;
1696 * Relock the map if necessary, then check the generation count.
1697 * relock_map() will update fs->timestamp to account for the
1698 * relocking if necessary.
1700 * If the count has changed after relocking then all sorts of
1701 * crap may have happened and we have to retry.
1703 * NOTE: The relock_map() can fail due to a deadlock against
1704 * the vm_page we are holding BUSY.
1706 if (fs->lookup_still_valid == FALSE && fs->map) {
1707 if (relock_map(fs) ||
1708 fs->map->timestamp != fs->map_generation) {
1710 vm_object_pip_wakeup(fs->first_object);
1711 vm_object_chain_release_all(fs->first_object,
1713 if (fs->object != fs->first_object)
1714 vm_object_drop(fs->object);
1715 unlock_and_deallocate(fs);
1716 return (KERN_TRY_AGAIN);
1721 * If the fault is a write, we know that this page is being
1722 * written NOW so dirty it explicitly to save on pmap_is_modified()
1725 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1726 * if the page is already dirty to prevent data written with
1727 * the expectation of being synced from not being synced.
1728 * Likewise if this entry does not request NOSYNC then make
1729 * sure the page isn't marked NOSYNC. Applications sharing
1730 * data should use the same flags to avoid ping ponging.
1732 * Also tell the backing pager, if any, that it should remove
1733 * any swap backing since the page is now dirty.
1735 vm_page_activate(fs->m);
1736 if (fs->prot & VM_PROT_WRITE) {
1737 vm_object_set_writeable_dirty(fs->m->object);
1738 vm_set_nosync(fs->m, fs->entry);
1739 if (fs->fault_flags & VM_FAULT_DIRTY) {
1740 vm_page_dirty(fs->m);
1741 swap_pager_unswapped(fs->m);
1745 vm_object_pip_wakeup(fs->first_object);
1746 vm_object_chain_release_all(fs->first_object, fs->object);
1747 if (fs->object != fs->first_object)
1748 vm_object_drop(fs->object);
1751 * Page had better still be busy. We are still locked up and
1752 * fs->object will have another PIP reference if it is not equal
1753 * to fs->first_object.
1755 KASSERT(fs->m->flags & PG_BUSY,
1756 ("vm_fault: page %p not busy!", fs->m));
1759 * Sanity check: page must be completely valid or it is not fit to
1760 * map into user space. vm_pager_get_pages() ensures this.
1762 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1763 vm_page_zero_invalid(fs->m, TRUE);
1764 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1766 vm_page_flag_clear(fs->m, PG_ZERO);
1768 return (KERN_SUCCESS);
1772 * Wire down a range of virtual addresses in a map. The entry in question
1773 * should be marked in-transition and the map must be locked. We must
1774 * release the map temporarily while faulting-in the page to avoid a
1775 * deadlock. Note that the entry may be clipped while we are blocked but
1776 * will never be freed.
1781 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1783 boolean_t fictitious;
1792 lwkt_gettoken(&map->token);
1794 pmap = vm_map_pmap(map);
1795 start = entry->start;
1797 fictitious = entry->object.vm_object &&
1798 (entry->object.vm_object->type == OBJT_DEVICE);
1799 if (entry->eflags & MAP_ENTRY_KSTACK)
1805 * We simulate a fault to get the page and enter it in the physical
1808 for (va = start; va < end; va += PAGE_SIZE) {
1810 rv = vm_fault(map, va, VM_PROT_READ,
1811 VM_FAULT_USER_WIRE);
1813 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1814 VM_FAULT_CHANGE_WIRING);
1817 while (va > start) {
1819 if ((pa = pmap_extract(pmap, va)) == 0)
1821 pmap_change_wiring(pmap, va, FALSE);
1823 m = PHYS_TO_VM_PAGE(pa);
1824 vm_page_busy_wait(m, FALSE, "vmwrpg");
1825 vm_page_unwire(m, 1);
1835 lwkt_reltoken(&map->token);
1840 * Unwire a range of virtual addresses in a map. The map should be
1844 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1846 boolean_t fictitious;
1854 lwkt_gettoken(&map->token);
1856 pmap = vm_map_pmap(map);
1857 start = entry->start;
1859 fictitious = entry->object.vm_object &&
1860 (entry->object.vm_object->type == OBJT_DEVICE);
1861 if (entry->eflags & MAP_ENTRY_KSTACK)
1865 * Since the pages are wired down, we must be able to get their
1866 * mappings from the physical map system.
1868 for (va = start; va < end; va += PAGE_SIZE) {
1869 pa = pmap_extract(pmap, va);
1871 pmap_change_wiring(pmap, va, FALSE);
1873 m = PHYS_TO_VM_PAGE(pa);
1874 vm_page_busy_wait(m, FALSE, "vmwupg");
1875 vm_page_unwire(m, 1);
1880 lwkt_reltoken(&map->token);
1884 * Copy all of the pages from a wired-down map entry to another.
1886 * The source and destination maps must be locked for write.
1887 * The source and destination maps token must be held
1888 * The source map entry must be wired down (or be a sharing map
1889 * entry corresponding to a main map entry that is wired down).
1891 * No other requirements.
1894 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1895 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1897 vm_object_t dst_object;
1898 vm_object_t src_object;
1899 vm_ooffset_t dst_offset;
1900 vm_ooffset_t src_offset;
1906 src_object = src_entry->object.vm_object;
1907 src_offset = src_entry->offset;
1910 * Create the top-level object for the destination entry. (Doesn't
1911 * actually shadow anything - we copy the pages directly.)
1913 vm_map_entry_allocate_object(dst_entry);
1914 dst_object = dst_entry->object.vm_object;
1916 prot = dst_entry->max_protection;
1919 * Loop through all of the pages in the entry's range, copying each
1920 * one from the source object (it should be there) to the destination
1923 for (vaddr = dst_entry->start, dst_offset = 0;
1924 vaddr < dst_entry->end;
1925 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1928 * Allocate a page in the destination object
1931 dst_m = vm_page_alloc(dst_object,
1932 OFF_TO_IDX(dst_offset),
1934 if (dst_m == NULL) {
1937 } while (dst_m == NULL);
1940 * Find the page in the source object, and copy it in.
1941 * (Because the source is wired down, the page will be in
1944 src_m = vm_page_lookup(src_object,
1945 OFF_TO_IDX(dst_offset + src_offset));
1947 panic("vm_fault_copy_wired: page missing");
1949 vm_page_copy(src_m, dst_m);
1950 vm_page_event(src_m, VMEVENT_COW);
1953 * Enter it in the pmap...
1956 vm_page_flag_clear(dst_m, PG_ZERO);
1957 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1960 * Mark it no longer busy, and put it on the active list.
1962 vm_page_activate(dst_m);
1963 vm_page_wakeup(dst_m);
1970 * This routine checks around the requested page for other pages that
1971 * might be able to be faulted in. This routine brackets the viable
1972 * pages for the pages to be paged in.
1975 * m, rbehind, rahead
1978 * marray (array of vm_page_t), reqpage (index of requested page)
1981 * number of pages in marray
1984 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1985 vm_page_t *marray, int *reqpage)
1989 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1991 int cbehind, cahead;
1997 * we don't fault-ahead for device pager
1999 if (object->type == OBJT_DEVICE) {
2006 * if the requested page is not available, then give up now
2008 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2009 *reqpage = 0; /* not used by caller, fix compiler warn */
2013 if ((cbehind == 0) && (cahead == 0)) {
2019 if (rahead > cahead) {
2023 if (rbehind > cbehind) {
2028 * Do not do any readahead if we have insufficient free memory.
2030 * XXX code was broken disabled before and has instability
2031 * with this conditonal fixed, so shortcut for now.
2033 if (burst_fault == 0 || vm_page_count_severe()) {
2040 * scan backward for the read behind pages -- in memory
2042 * Assume that if the page is not found an interrupt will not
2043 * create it. Theoretically interrupts can only remove (busy)
2044 * pages, not create new associations.
2047 if (rbehind > pindex) {
2051 startpindex = pindex - rbehind;
2054 vm_object_hold(object);
2055 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2056 if (vm_page_lookup(object, tpindex - 1))
2061 while (tpindex < pindex) {
2062 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2065 for (j = 0; j < i; j++) {
2066 vm_page_free(marray[j]);
2068 vm_object_drop(object);
2077 vm_object_drop(object);
2083 * Assign requested page
2090 * Scan forwards for read-ahead pages
2092 tpindex = pindex + 1;
2093 endpindex = tpindex + rahead;
2094 if (endpindex > object->size)
2095 endpindex = object->size;
2097 vm_object_hold(object);
2098 while (tpindex < endpindex) {
2099 if (vm_page_lookup(object, tpindex))
2101 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2109 vm_object_drop(object);
2117 * vm_prefault() provides a quick way of clustering pagefaults into a
2118 * processes address space. It is a "cousin" of pmap_object_init_pt,
2119 * except it runs at page fault time instead of mmap time.
2121 * vm.fast_fault Enables pre-faulting zero-fill pages
2123 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2124 * prefault. Scan stops in either direction when
2125 * a page is found to already exist.
2127 * This code used to be per-platform pmap_prefault(). It is now
2128 * machine-independent and enhanced to also pre-fault zero-fill pages
2129 * (see vm.fast_fault) as well as make them writable, which greatly
2130 * reduces the number of page faults programs incur.
2132 * Application performance when pre-faulting zero-fill pages is heavily
2133 * dependent on the application. Very tiny applications like /bin/echo
2134 * lose a little performance while applications of any appreciable size
2135 * gain performance. Prefaulting multiple pages also reduces SMP
2136 * congestion and can improve SMP performance significantly.
2138 * NOTE! prot may allow writing but this only applies to the top level
2139 * object. If we wind up mapping a page extracted from a backing
2140 * object we have to make sure it is read-only.
2142 * NOTE! The caller has already handled any COW operations on the
2143 * vm_map_entry via the normal fault code. Do NOT call this
2144 * shortcut unless the normal fault code has run on this entry.
2146 * The related map must be locked.
2147 * No other requirements.
2149 static int vm_prefault_pages = 8;
2150 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2151 "Maximum number of pages to pre-fault");
2152 static int vm_fast_fault = 1;
2153 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2154 "Burst fault zero-fill regions");
2157 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2158 * is not already dirty by other means. This will prevent passive
2159 * filesystem syncing as well as 'sync' from writing out the page.
2162 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2164 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2166 vm_page_flag_set(m, PG_NOSYNC);
2168 vm_page_flag_clear(m, PG_NOSYNC);
2173 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2189 * Get stable max count value, disabled if set to 0
2191 maxpages = vm_prefault_pages;
2197 * We do not currently prefault mappings that use virtual page
2198 * tables. We do not prefault foreign pmaps.
2200 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2202 lp = curthread->td_lwp;
2203 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2207 * Limit pre-fault count to 1024 pages.
2209 if (maxpages > 1024)
2212 object = entry->object.vm_object;
2213 KKASSERT(object != NULL);
2214 KKASSERT(object == entry->object.vm_object);
2215 vm_object_hold(object);
2216 vm_object_chain_acquire(object);
2220 for (i = 0; i < maxpages; ++i) {
2221 vm_object_t lobject;
2222 vm_object_t nobject;
2227 * This can eat a lot of time on a heavily contended
2228 * machine so yield on the tick if needed.
2234 * Calculate the page to pre-fault, stopping the scan in
2235 * each direction separately if the limit is reached.
2240 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2244 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2246 if (addr < entry->start) {
2252 if (addr >= entry->end) {
2260 * Skip pages already mapped, and stop scanning in that
2261 * direction. When the scan terminates in both directions
2264 if (pmap_prefault_ok(pmap, addr) == 0) {
2275 * Follow the VM object chain to obtain the page to be mapped
2278 * If we reach the terminal object without finding a page
2279 * and we determine it would be advantageous, then allocate
2280 * a zero-fill page for the base object. The base object
2281 * is guaranteed to be OBJT_DEFAULT for this case.
2283 * In order to not have to check the pager via *haspage*()
2284 * we stop if any non-default object is encountered. e.g.
2285 * a vnode or swap object would stop the loop.
2287 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2292 KKASSERT(lobject == entry->object.vm_object);
2293 /*vm_object_hold(lobject); implied */
2295 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2296 TRUE, &error)) == NULL) {
2297 if (lobject->type != OBJT_DEFAULT)
2299 if (lobject->backing_object == NULL) {
2300 if (vm_fast_fault == 0)
2302 if ((prot & VM_PROT_WRITE) == 0 ||
2303 vm_page_count_min(0)) {
2308 * NOTE: Allocated from base object
2310 m = vm_page_alloc(object, index,
2319 /* lobject = object .. not needed */
2322 if (lobject->backing_object_offset & PAGE_MASK)
2324 nobject = lobject->backing_object;
2325 vm_object_hold(nobject);
2326 KKASSERT(nobject == lobject->backing_object);
2327 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2328 if (lobject != object) {
2329 vm_object_lock_swap();
2330 vm_object_drop(lobject);
2333 pprot &= ~VM_PROT_WRITE;
2334 vm_object_chain_acquire(lobject);
2338 * NOTE: A non-NULL (m) will be associated with lobject if
2339 * it was found there, otherwise it is probably a
2340 * zero-fill page associated with the base object.
2342 * Give-up if no page is available.
2345 if (lobject != object) {
2346 if (object->backing_object != lobject)
2347 vm_object_hold(object->backing_object);
2348 vm_object_chain_release_all(
2349 object->backing_object, lobject);
2350 if (object->backing_object != lobject)
2351 vm_object_drop(object->backing_object);
2352 vm_object_drop(lobject);
2358 * The object must be marked dirty if we are mapping a
2359 * writable page. m->object is either lobject or object,
2360 * both of which are still held. Do this before we
2361 * potentially drop the object.
2363 if (pprot & VM_PROT_WRITE)
2364 vm_object_set_writeable_dirty(m->object);
2367 * Do not conditionalize on PG_RAM. If pages are present in
2368 * the VM system we assume optimal caching. If caching is
2369 * not optimal the I/O gravy train will be restarted when we
2370 * hit an unavailable page. We do not want to try to restart
2371 * the gravy train now because we really don't know how much
2372 * of the object has been cached. The cost for restarting
2373 * the gravy train should be low (since accesses will likely
2374 * be I/O bound anyway).
2376 if (lobject != object) {
2377 if (object->backing_object != lobject)
2378 vm_object_hold(object->backing_object);
2379 vm_object_chain_release_all(object->backing_object,
2381 if (object->backing_object != lobject)
2382 vm_object_drop(object->backing_object);
2383 vm_object_drop(lobject);
2387 * Enter the page into the pmap if appropriate. If we had
2388 * allocated the page we have to place it on a queue. If not
2389 * we just have to make sure it isn't on the cache queue
2390 * (pages on the cache queue are not allowed to be mapped).
2394 * Page must be zerod.
2396 if ((m->flags & PG_ZERO) == 0) {
2397 vm_page_zero_fill(m);
2400 pmap_page_assertzero(
2401 VM_PAGE_TO_PHYS(m));
2403 vm_page_flag_clear(m, PG_ZERO);
2404 mycpu->gd_cnt.v_ozfod++;
2406 mycpu->gd_cnt.v_zfod++;
2407 m->valid = VM_PAGE_BITS_ALL;
2410 * Handle dirty page case
2412 if (pprot & VM_PROT_WRITE)
2413 vm_set_nosync(m, entry);
2414 pmap_enter(pmap, addr, m, pprot, 0);
2415 mycpu->gd_cnt.v_vm_faults++;
2416 if (curthread->td_lwp)
2417 ++curthread->td_lwp->lwp_ru.ru_minflt;
2418 vm_page_deactivate(m);
2419 if (pprot & VM_PROT_WRITE) {
2420 /*vm_object_set_writeable_dirty(m->object);*/
2421 vm_set_nosync(m, entry);
2422 if (fault_flags & VM_FAULT_DIRTY) {
2425 swap_pager_unswapped(m);
2430 /* couldn't busy page, no wakeup */
2432 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2433 (m->flags & PG_FICTITIOUS) == 0) {
2435 * A fully valid page not undergoing soft I/O can
2436 * be immediately entered into the pmap.
2438 if ((m->queue - m->pc) == PQ_CACHE)
2439 vm_page_deactivate(m);
2440 if (pprot & VM_PROT_WRITE) {
2441 /*vm_object_set_writeable_dirty(m->object);*/
2442 vm_set_nosync(m, entry);
2443 if (fault_flags & VM_FAULT_DIRTY) {
2446 swap_pager_unswapped(m);
2449 if (pprot & VM_PROT_WRITE)
2450 vm_set_nosync(m, entry);
2451 pmap_enter(pmap, addr, m, pprot, 0);
2452 mycpu->gd_cnt.v_vm_faults++;
2453 if (curthread->td_lwp)
2454 ++curthread->td_lwp->lwp_ru.ru_minflt;
2460 vm_object_chain_release(object);
2461 vm_object_drop(object);
2465 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2466 vm_map_entry_t entry, int prot, int fault_flags)
2479 * Get stable max count value, disabled if set to 0
2481 maxpages = vm_prefault_pages;
2487 * We do not currently prefault mappings that use virtual page
2488 * tables. We do not prefault foreign pmaps.
2490 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2492 lp = curthread->td_lwp;
2493 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2497 * Limit pre-fault count to 1024 pages.
2499 if (maxpages > 1024)
2502 object = entry->object.vm_object;
2503 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2504 KKASSERT(object->backing_object == NULL);
2508 for (i = 0; i < maxpages; ++i) {
2512 * Calculate the page to pre-fault, stopping the scan in
2513 * each direction separately if the limit is reached.
2518 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2522 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2524 if (addr < entry->start) {
2530 if (addr >= entry->end) {
2538 * Skip pages already mapped, and stop scanning in that
2539 * direction. When the scan terminates in both directions
2542 if (pmap_prefault_ok(pmap, addr) == 0) {
2553 * Follow the VM object chain to obtain the page to be mapped
2554 * into the pmap. This version of the prefault code only
2555 * works with terminal objects.
2557 * WARNING! We cannot call swap_pager_unswapped() with a
2560 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2562 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
2563 if (m == NULL || error)
2566 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2567 (m->flags & PG_FICTITIOUS) == 0 &&
2568 ((m->flags & PG_SWAPPED) == 0 ||
2569 (prot & VM_PROT_WRITE) == 0 ||
2570 (fault_flags & VM_FAULT_DIRTY) == 0)) {
2572 * A fully valid page not undergoing soft I/O can
2573 * be immediately entered into the pmap.
2575 if ((m->queue - m->pc) == PQ_CACHE)
2576 vm_page_deactivate(m);
2577 if (prot & VM_PROT_WRITE) {
2578 vm_object_set_writeable_dirty(m->object);
2579 vm_set_nosync(m, entry);
2580 if (fault_flags & VM_FAULT_DIRTY) {
2583 swap_pager_unswapped(m);
2586 pmap_enter(pmap, addr, m, prot, 0);
2587 mycpu->gd_cnt.v_vm_faults++;
2588 if (curthread->td_lwp)
2589 ++curthread->td_lwp->lwp_ru.ru_minflt;