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 = 1;
128 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW, &vm_shared_fault, 0,
129 "Allow shared token on vm_object");
130 static long vm_shared_hit = 0;
131 SYSCTL_LONG(_vm, OID_AUTO, shared_hit, CTLFLAG_RW, &vm_shared_hit, 0,
132 "Successful shared faults");
133 static long vm_shared_miss = 0;
134 SYSCTL_LONG(_vm, OID_AUTO, shared_miss, CTLFLAG_RW, &vm_shared_miss, 0,
135 "Successful shared faults");
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 * XXX We have a shared lock, this will have a MP race but
343 * I don't see how it can hurt anything.
345 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
346 fs.entry->max_protection &= ~VM_PROT_WRITE;
350 * fs.map is read-locked
352 * Misc checks. Save the map generation number to detect races.
354 fs.map_generation = fs.map->timestamp;
356 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
357 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
358 panic("vm_fault: fault on nofault entry, addr: %p",
361 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
362 vaddr >= fs.entry->start &&
363 vaddr < fs.entry->start + PAGE_SIZE) {
364 panic("vm_fault: fault on stack guard, addr: %p",
370 * A system map entry may return a NULL object. No object means
371 * no pager means an unrecoverable kernel fault.
373 if (fs.first_object == NULL) {
374 panic("vm_fault: unrecoverable fault at %p in entry %p",
375 (void *)vaddr, fs.entry);
379 * Attempt to shortcut the fault if the lookup returns a
380 * terminal object and the page is present. This allows us
381 * to obtain a shared token on the object instead of an exclusive
382 * token, which theoretically should allow concurrent faults.
384 * We cannot acquire a shared token on kernel_map, at least not
385 * on i386, because the i386 pmap code uses the kernel_object for
386 * its page table page management, resulting in a shared->exclusive
387 * sequence which will deadlock. This will not happen normally
388 * anyway, except on well cached pageable kmem (like pipe buffers),
389 * so it should not impact performance.
391 if (vm_shared_fault &&
392 fs.first_object->backing_object == NULL &&
393 fs.entry->maptype == VM_MAPTYPE_NORMAL &&
394 fs.map != &kernel_map) {
396 vm_object_hold_shared(fs.first_object);
397 /*fs.vp = vnode_pager_lock(fs.first_object);*/
398 fs.m = vm_page_lookup_busy_try(fs.first_object,
401 if (error == 0 && fs.m) {
403 * Activate the page and figure out if we can
404 * short-cut a quick mapping.
406 * WARNING! We cannot call swap_pager_unswapped()
407 * with a shared token! Note that we
408 * have to test fs.first_prot here.
410 vm_page_activate(fs.m);
411 if (fs.m->valid == VM_PAGE_BITS_ALL &&
412 ((fs.m->flags & PG_SWAPPED) == 0 ||
413 (fs.first_prot & VM_PROT_WRITE) == 0 ||
414 (fs.fault_flags & VM_FAULT_DIRTY) == 0)) {
415 fs.lookup_still_valid = TRUE;
417 fs.object = fs.first_object;
418 fs.prot = fs.first_prot;
420 fault_type = fs.first_prot;
421 if (fs.prot & VM_PROT_WRITE) {
422 vm_object_set_writeable_dirty(
424 vm_set_nosync(fs.m, fs.entry);
425 if (fs.fault_flags & VM_FAULT_DIRTY) {
428 swap_pager_unswapped(fs.m);
431 result = KERN_SUCCESS;
432 fault_flags |= VM_FAULT_BURST_QUICK;
433 fault_flags &= ~VM_FAULT_BURST;
437 vm_page_wakeup(fs.m);
440 vm_object_drop(fs.first_object); /* XXX drop on shared tok?*/
445 * Bump the paging-in-progress count to prevent size changes (e.g.
446 * truncation operations) during I/O. This must be done after
447 * obtaining the vnode lock in order to avoid possible deadlocks.
449 vm_object_hold(fs.first_object);
451 fs.vp = vnode_pager_lock(fs.first_object);
453 fs.lookup_still_valid = TRUE;
455 fs.object = fs.first_object; /* so unlock_and_deallocate works */
458 * If the entry is wired we cannot change the page protection.
461 fault_type = fs.first_prot;
464 * The page we want is at (first_object, first_pindex), but if the
465 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
466 * page table to figure out the actual pindex.
468 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
471 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
472 result = vm_fault_vpagetable(&fs, &first_pindex,
473 fs.entry->aux.master_pde,
475 if (result == KERN_TRY_AGAIN) {
476 vm_object_drop(fs.first_object);
479 if (result != KERN_SUCCESS)
484 * Now we have the actual (object, pindex), fault in the page. If
485 * vm_fault_object() fails it will unlock and deallocate the FS
486 * data. If it succeeds everything remains locked and fs->object
487 * will have an additional PIP count if it is not equal to
490 * vm_fault_object will set fs->prot for the pmap operation. It is
491 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
492 * page can be safely written. However, it will force a read-only
493 * mapping for a read fault if the memory is managed by a virtual
497 result = vm_fault_object(&fs, first_pindex, fault_type);
499 if (result == KERN_TRY_AGAIN) {
500 vm_object_drop(fs.first_object);
503 if (result != KERN_SUCCESS)
508 * On success vm_fault_object() does not unlock or deallocate, and fs.m
509 * will contain a busied page.
511 * Enter the page into the pmap and do pmap-related adjustments.
513 vm_page_flag_set(fs.m, PG_REFERENCED);
514 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
515 mycpu->gd_cnt.v_vm_faults++;
516 if (curthread->td_lwp)
517 ++curthread->td_lwp->lwp_ru.ru_minflt;
519 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
520 KKASSERT(fs.m->flags & PG_BUSY);
523 * If the page is not wired down, then put it where the pageout daemon
526 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
530 vm_page_unwire(fs.m, 1);
532 vm_page_activate(fs.m);
534 vm_page_wakeup(fs.m);
537 * Burst in a few more pages if possible. The fs.map should still
538 * be locked. To avoid interlocking against a vnode->getblk
539 * operation we had to be sure to unbusy our primary vm_page above
542 if (fault_flags & VM_FAULT_BURST) {
543 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
545 vm_prefault(fs.map->pmap, vaddr,
546 fs.entry, fs.prot, fault_flags);
549 if (fault_flags & VM_FAULT_BURST_QUICK) {
550 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
552 vm_prefault_quick(fs.map->pmap, vaddr,
553 fs.entry, fs.prot, fault_flags);
558 * Unlock everything, and return
562 if (curthread->td_lwp) {
564 curthread->td_lwp->lwp_ru.ru_majflt++;
566 curthread->td_lwp->lwp_ru.ru_minflt++;
570 /*vm_object_deallocate(fs.first_object);*/
572 /*fs.first_object = NULL; must still drop later */
574 result = KERN_SUCCESS;
577 vm_object_drop(fs.first_object);
578 lwkt_reltoken(&map->token);
580 lp->lwp_flags &= ~LWP_PAGING;
585 * Fault in the specified virtual address in the current process map,
586 * returning a held VM page or NULL. See vm_fault_page() for more
592 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
594 struct lwp *lp = curthread->td_lwp;
597 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
598 fault_type, VM_FAULT_NORMAL, errorp);
603 * Fault in the specified virtual address in the specified map, doing all
604 * necessary manipulation of the object store and all necessary I/O. Return
605 * a held VM page or NULL, and set *errorp. The related pmap is not
608 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
609 * and marked PG_REFERENCED as well.
611 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
612 * error will be returned.
617 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
618 int fault_flags, int *errorp)
620 vm_pindex_t first_pindex;
621 struct faultstate fs;
623 vm_prot_t orig_fault_type = fault_type;
626 fs.fault_flags = fault_flags;
627 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
629 lwkt_gettoken(&map->token);
633 * Find the vm_map_entry representing the backing store and resolve
634 * the top level object and page index. This may have the side
635 * effect of executing a copy-on-write on the map entry and/or
636 * creating a shadow object, but will not COW any actual VM pages.
638 * On success fs.map is left read-locked and various other fields
639 * are initialized but not otherwise referenced or locked.
641 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
642 * if the map entry is a virtual page table and also writable,
643 * so we can set the 'A'accessed bit in the virtual page table entry.
646 result = vm_map_lookup(&fs.map, vaddr, fault_type,
647 &fs.entry, &fs.first_object,
648 &first_pindex, &fs.first_prot, &fs.wired);
650 if (result != KERN_SUCCESS) {
657 * fs.map is read-locked
659 * Misc checks. Save the map generation number to detect races.
661 fs.map_generation = fs.map->timestamp;
663 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
664 panic("vm_fault: fault on nofault entry, addr: %lx",
669 * A system map entry may return a NULL object. No object means
670 * no pager means an unrecoverable kernel fault.
672 if (fs.first_object == NULL) {
673 panic("vm_fault: unrecoverable fault at %p in entry %p",
674 (void *)vaddr, fs.entry);
678 * Make a reference to this object to prevent its disposal while we
679 * are messing with it. Once we have the reference, the map is free
680 * to be diddled. Since objects reference their shadows (and copies),
681 * they will stay around as well.
683 * The reference should also prevent an unexpected collapse of the
684 * parent that might move pages from the current object into the
685 * parent unexpectedly, resulting in corruption.
687 * Bump the paging-in-progress count to prevent size changes (e.g.
688 * truncation operations) during I/O. This must be done after
689 * obtaining the vnode lock in order to avoid possible deadlocks.
691 vm_object_hold(fs.first_object);
692 fs.vp = vnode_pager_lock(fs.first_object);
694 fs.lookup_still_valid = TRUE;
696 fs.object = fs.first_object; /* so unlock_and_deallocate works */
699 * If the entry is wired we cannot change the page protection.
702 fault_type = fs.first_prot;
705 * The page we want is at (first_object, first_pindex), but if the
706 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
707 * page table to figure out the actual pindex.
709 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
712 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
713 result = vm_fault_vpagetable(&fs, &first_pindex,
714 fs.entry->aux.master_pde,
716 if (result == KERN_TRY_AGAIN) {
717 vm_object_drop(fs.first_object);
720 if (result != KERN_SUCCESS) {
728 * Now we have the actual (object, pindex), fault in the page. If
729 * vm_fault_object() fails it will unlock and deallocate the FS
730 * data. If it succeeds everything remains locked and fs->object
731 * will have an additinal PIP count if it is not equal to
735 result = vm_fault_object(&fs, first_pindex, fault_type);
737 if (result == KERN_TRY_AGAIN) {
738 vm_object_drop(fs.first_object);
741 if (result != KERN_SUCCESS) {
747 if ((orig_fault_type & VM_PROT_WRITE) &&
748 (fs.prot & VM_PROT_WRITE) == 0) {
749 *errorp = KERN_PROTECTION_FAILURE;
750 unlock_and_deallocate(&fs);
756 * DO NOT UPDATE THE PMAP!!! This function may be called for
757 * a pmap unrelated to the current process pmap, in which case
758 * the current cpu core will not be listed in the pmap's pm_active
759 * mask. Thus invalidation interlocks will fail to work properly.
761 * (for example, 'ps' uses procfs to read program arguments from
762 * each process's stack).
764 * In addition to the above this function will be called to acquire
765 * a page that might already be faulted in, re-faulting it
766 * continuously is a waste of time.
768 * XXX could this have been the cause of our random seg-fault
769 * issues? procfs accesses user stacks.
771 vm_page_flag_set(fs.m, PG_REFERENCED);
773 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
774 mycpu->gd_cnt.v_vm_faults++;
775 if (curthread->td_lwp)
776 ++curthread->td_lwp->lwp_ru.ru_minflt;
780 * On success vm_fault_object() does not unlock or deallocate, and fs.m
781 * will contain a busied page. So we must unlock here after having
782 * messed with the pmap.
787 * Return a held page. We are not doing any pmap manipulation so do
788 * not set PG_MAPPED. However, adjust the page flags according to
789 * the fault type because the caller may not use a managed pmapping
790 * (so we don't want to lose the fact that the page will be dirtied
791 * if a write fault was specified).
794 vm_page_activate(fs.m);
795 if (fault_type & VM_PROT_WRITE)
798 if (curthread->td_lwp) {
800 curthread->td_lwp->lwp_ru.ru_majflt++;
802 curthread->td_lwp->lwp_ru.ru_minflt++;
807 * Unlock everything, and return the held page.
809 vm_page_wakeup(fs.m);
810 /*vm_object_deallocate(fs.first_object);*/
811 /*fs.first_object = NULL; */
816 vm_object_drop(fs.first_object);
817 lwkt_reltoken(&map->token);
822 * Fault in the specified (object,offset), dirty the returned page as
823 * needed. If the requested fault_type cannot be done NULL and an
826 * A held (but not busied) page is returned.
831 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
832 vm_prot_t fault_type, int fault_flags, int *errorp)
835 vm_pindex_t first_pindex;
836 struct faultstate fs;
837 struct vm_map_entry entry;
839 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
840 bzero(&entry, sizeof(entry));
841 entry.object.vm_object = object;
842 entry.maptype = VM_MAPTYPE_NORMAL;
843 entry.protection = entry.max_protection = fault_type;
846 fs.fault_flags = fault_flags;
848 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
852 fs.first_object = object;
853 first_pindex = OFF_TO_IDX(offset);
855 fs.first_prot = fault_type;
857 /*fs.map_generation = 0; unused */
860 * Make a reference to this object to prevent its disposal while we
861 * are messing with it. Once we have the reference, the map is free
862 * to be diddled. Since objects reference their shadows (and copies),
863 * they will stay around as well.
865 * The reference should also prevent an unexpected collapse of the
866 * parent that might move pages from the current object into the
867 * parent unexpectedly, resulting in corruption.
869 * Bump the paging-in-progress count to prevent size changes (e.g.
870 * truncation operations) during I/O. This must be done after
871 * obtaining the vnode lock in order to avoid possible deadlocks.
873 fs.vp = vnode_pager_lock(fs.first_object);
875 fs.lookup_still_valid = TRUE;
877 fs.object = fs.first_object; /* so unlock_and_deallocate works */
880 /* XXX future - ability to operate on VM object using vpagetable */
881 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
882 result = vm_fault_vpagetable(&fs, &first_pindex,
883 fs.entry->aux.master_pde,
885 if (result == KERN_TRY_AGAIN)
887 if (result != KERN_SUCCESS) {
895 * Now we have the actual (object, pindex), fault in the page. If
896 * vm_fault_object() fails it will unlock and deallocate the FS
897 * data. If it succeeds everything remains locked and fs->object
898 * will have an additinal PIP count if it is not equal to
901 result = vm_fault_object(&fs, first_pindex, fault_type);
903 if (result == KERN_TRY_AGAIN)
905 if (result != KERN_SUCCESS) {
910 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
911 *errorp = KERN_PROTECTION_FAILURE;
912 unlock_and_deallocate(&fs);
917 * On success vm_fault_object() does not unlock or deallocate, so we
918 * do it here. Note that the returned fs.m will be busied.
923 * Return a held page. We are not doing any pmap manipulation so do
924 * not set PG_MAPPED. However, adjust the page flags according to
925 * the fault type because the caller may not use a managed pmapping
926 * (so we don't want to lose the fact that the page will be dirtied
927 * if a write fault was specified).
930 vm_page_activate(fs.m);
931 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
933 if (fault_flags & VM_FAULT_UNSWAP)
934 swap_pager_unswapped(fs.m);
937 * Indicate that the page was accessed.
939 vm_page_flag_set(fs.m, PG_REFERENCED);
941 if (curthread->td_lwp) {
943 curthread->td_lwp->lwp_ru.ru_majflt++;
945 curthread->td_lwp->lwp_ru.ru_minflt++;
950 * Unlock everything, and return the held page.
952 vm_page_wakeup(fs.m);
953 /*vm_object_deallocate(fs.first_object);*/
954 /*fs.first_object = NULL; */
961 * Translate the virtual page number (first_pindex) that is relative
962 * to the address space into a logical page number that is relative to the
963 * backing object. Use the virtual page table pointed to by (vpte).
965 * This implements an N-level page table. Any level can terminate the
966 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
967 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
971 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
972 vpte_t vpte, int fault_type)
975 struct lwbuf lwb_cache;
976 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
977 int result = KERN_SUCCESS;
980 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
983 * We cannot proceed if the vpte is not valid, not readable
984 * for a read fault, or not writable for a write fault.
986 if ((vpte & VPTE_V) == 0) {
987 unlock_and_deallocate(fs);
988 return (KERN_FAILURE);
990 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
991 unlock_and_deallocate(fs);
992 return (KERN_FAILURE);
994 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
995 unlock_and_deallocate(fs);
996 return (KERN_FAILURE);
998 if ((vpte & VPTE_PS) || vshift == 0)
1000 KKASSERT(vshift >= VPTE_PAGE_BITS);
1003 * Get the page table page. Nominally we only read the page
1004 * table, but since we are actively setting VPTE_M and VPTE_A,
1005 * tell vm_fault_object() that we are writing it.
1007 * There is currently no real need to optimize this.
1009 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1010 VM_PROT_READ|VM_PROT_WRITE);
1011 if (result != KERN_SUCCESS)
1015 * Process the returned fs.m and look up the page table
1016 * entry in the page table page.
1018 vshift -= VPTE_PAGE_BITS;
1019 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1020 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1021 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1025 * Page table write-back. If the vpte is valid for the
1026 * requested operation, do a write-back to the page table.
1028 * XXX VPTE_M is not set properly for page directory pages.
1029 * It doesn't get set in the page directory if the page table
1030 * is modified during a read access.
1032 vm_page_activate(fs->m);
1033 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
1035 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
1036 atomic_set_long(ptep, VPTE_M | VPTE_A);
1037 vm_page_dirty(fs->m);
1040 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
1042 if ((vpte & VPTE_A) == 0) {
1043 atomic_set_long(ptep, VPTE_A);
1044 vm_page_dirty(fs->m);
1048 vm_page_flag_set(fs->m, PG_REFERENCED);
1049 vm_page_wakeup(fs->m);
1051 cleanup_successful_fault(fs);
1054 * Combine remaining address bits with the vpte.
1056 /* JG how many bits from each? */
1057 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1058 (*pindex & ((1L << vshift) - 1));
1059 return (KERN_SUCCESS);
1064 * This is the core of the vm_fault code.
1066 * Do all operations required to fault-in (fs.first_object, pindex). Run
1067 * through the shadow chain as necessary and do required COW or virtual
1068 * copy operations. The caller has already fully resolved the vm_map_entry
1069 * and, if appropriate, has created a copy-on-write layer. All we need to
1070 * do is iterate the object chain.
1072 * On failure (fs) is unlocked and deallocated and the caller may return or
1073 * retry depending on the failure code. On success (fs) is NOT unlocked or
1074 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1075 * will have an additional PIP count if it is not equal to fs.first_object.
1077 * fs->first_object must be held on call.
1081 vm_fault_object(struct faultstate *fs,
1082 vm_pindex_t first_pindex, vm_prot_t fault_type)
1084 vm_object_t next_object;
1088 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1089 fs->prot = fs->first_prot;
1090 fs->object = fs->first_object;
1091 pindex = first_pindex;
1093 vm_object_chain_acquire(fs->first_object);
1094 vm_object_pip_add(fs->first_object, 1);
1097 * If a read fault occurs we try to make the page writable if
1098 * possible. There are three cases where we cannot make the
1099 * page mapping writable:
1101 * (1) The mapping is read-only or the VM object is read-only,
1102 * fs->prot above will simply not have VM_PROT_WRITE set.
1104 * (2) If the mapping is a virtual page table we need to be able
1105 * to detect writes so we can set VPTE_M in the virtual page
1108 * (3) If the VM page is read-only or copy-on-write, upgrading would
1109 * just result in an unnecessary COW fault.
1111 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1112 * causes adjustments to the 'M'odify bit to also turn off write
1113 * access to force a re-fault.
1115 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1116 if ((fault_type & VM_PROT_WRITE) == 0)
1117 fs->prot &= ~VM_PROT_WRITE;
1120 /* vm_object_hold(fs->object); implied b/c object == first_object */
1124 * The entire backing chain from first_object to object
1125 * inclusive is chainlocked.
1127 * If the object is dead, we stop here
1129 if (fs->object->flags & OBJ_DEAD) {
1130 vm_object_pip_wakeup(fs->first_object);
1131 vm_object_chain_release_all(fs->first_object,
1133 if (fs->object != fs->first_object)
1134 vm_object_drop(fs->object);
1135 unlock_and_deallocate(fs);
1136 return (KERN_PROTECTION_FAILURE);
1140 * See if the page is resident. Wait/Retry if the page is
1141 * busy (lots of stuff may have changed so we can't continue
1144 * We can theoretically allow the soft-busy case on a read
1145 * fault if the page is marked valid, but since such
1146 * pages are typically already pmap'd, putting that
1147 * special case in might be more effort then it is
1148 * worth. We cannot under any circumstances mess
1149 * around with a vm_page_t->busy page except, perhaps,
1152 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1155 vm_object_pip_wakeup(fs->first_object);
1156 vm_object_chain_release_all(fs->first_object,
1158 if (fs->object != fs->first_object)
1159 vm_object_drop(fs->object);
1161 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1162 mycpu->gd_cnt.v_intrans++;
1163 /*vm_object_deallocate(fs->first_object);*/
1164 /*fs->first_object = NULL;*/
1166 return (KERN_TRY_AGAIN);
1170 * The page is busied for us.
1172 * If reactivating a page from PQ_CACHE we may have
1175 int queue = fs->m->queue;
1176 vm_page_unqueue_nowakeup(fs->m);
1178 if ((queue - fs->m->pc) == PQ_CACHE &&
1179 vm_page_count_severe()) {
1180 vm_page_activate(fs->m);
1181 vm_page_wakeup(fs->m);
1183 vm_object_pip_wakeup(fs->first_object);
1184 vm_object_chain_release_all(fs->first_object,
1186 if (fs->object != fs->first_object)
1187 vm_object_drop(fs->object);
1188 unlock_and_deallocate(fs);
1190 return (KERN_TRY_AGAIN);
1194 * If it still isn't completely valid (readable),
1195 * or if a read-ahead-mark is set on the VM page,
1196 * jump to readrest, else we found the page and
1199 * We can release the spl once we have marked the
1202 if (fs->m->object != &kernel_object) {
1203 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1207 if (fs->m->flags & PG_RAM) {
1210 vm_page_flag_clear(fs->m, PG_RAM);
1214 break; /* break to PAGE HAS BEEN FOUND */
1218 * Page is not resident, If this is the search termination
1219 * or the pager might contain the page, allocate a new page.
1221 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1223 * If the page is beyond the object size we fail
1225 if (pindex >= fs->object->size) {
1226 vm_object_pip_wakeup(fs->first_object);
1227 vm_object_chain_release_all(fs->first_object,
1229 if (fs->object != fs->first_object)
1230 vm_object_drop(fs->object);
1231 unlock_and_deallocate(fs);
1232 return (KERN_PROTECTION_FAILURE);
1236 * Allocate a new page for this object/offset pair.
1238 * It is possible for the allocation to race, so
1242 if (!vm_page_count_severe()) {
1243 fs->m = vm_page_alloc(fs->object, pindex,
1244 ((fs->vp || fs->object->backing_object) ?
1245 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1246 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1247 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1249 if (fs->m == NULL) {
1250 vm_object_pip_wakeup(fs->first_object);
1251 vm_object_chain_release_all(fs->first_object,
1253 if (fs->object != fs->first_object)
1254 vm_object_drop(fs->object);
1255 unlock_and_deallocate(fs);
1257 return (KERN_TRY_AGAIN);
1261 * Fall through to readrest. We have a new page which
1262 * will have to be paged (since m->valid will be 0).
1268 * We have found an invalid or partially valid page, a
1269 * page with a read-ahead mark which might be partially or
1270 * fully valid (and maybe dirty too), or we have allocated
1273 * Attempt to fault-in the page if there is a chance that the
1274 * pager has it, and potentially fault in additional pages
1277 * If TRYPAGER is true then fs.m will be non-NULL and busied
1283 u_char behavior = vm_map_entry_behavior(fs->entry);
1285 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1292 * If sequential access is detected then attempt
1293 * to deactivate/cache pages behind the scan to
1294 * prevent resource hogging.
1296 * Use of PG_RAM to detect sequential access
1297 * also simulates multi-zone sequential access
1298 * detection for free.
1300 * NOTE: Partially valid dirty pages cannot be
1301 * deactivated without causing NFS picemeal
1304 if ((fs->first_object->type != OBJT_DEVICE) &&
1305 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1306 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1307 (fs->m->flags & PG_RAM)))
1309 vm_pindex_t scan_pindex;
1310 int scan_count = 16;
1312 if (first_pindex < 16) {
1316 scan_pindex = first_pindex - 16;
1317 if (scan_pindex < 16)
1318 scan_count = scan_pindex;
1323 while (scan_count) {
1326 mt = vm_page_lookup(fs->first_object,
1330 if (vm_page_busy_try(mt, TRUE))
1333 if (mt->valid != VM_PAGE_BITS_ALL) {
1338 (PG_FICTITIOUS | PG_UNMANAGED |
1346 vm_page_test_dirty(mt);
1350 vm_page_deactivate(mt);
1365 * Avoid deadlocking against the map when doing I/O.
1366 * fs.object and the page is PG_BUSY'd.
1368 * NOTE: Once unlocked, fs->entry can become stale
1369 * so this will NULL it out.
1371 * NOTE: fs->entry is invalid until we relock the
1372 * map and verify that the timestamp has not
1378 * Acquire the page data. We still hold a ref on
1379 * fs.object and the page has been PG_BUSY's.
1381 * The pager may replace the page (for example, in
1382 * order to enter a fictitious page into the
1383 * object). If it does so it is responsible for
1384 * cleaning up the passed page and properly setting
1385 * the new page PG_BUSY.
1387 * If we got here through a PG_RAM read-ahead
1388 * mark the page may be partially dirty and thus
1389 * not freeable. Don't bother checking to see
1390 * if the pager has the page because we can't free
1391 * it anyway. We have to depend on the get_page
1392 * operation filling in any gaps whether there is
1393 * backing store or not.
1395 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1397 if (rv == VM_PAGER_OK) {
1399 * Relookup in case pager changed page. Pager
1400 * is responsible for disposition of old page
1403 * XXX other code segments do relookups too.
1404 * It's a bad abstraction that needs to be
1407 fs->m = vm_page_lookup(fs->object, pindex);
1408 if (fs->m == NULL) {
1409 vm_object_pip_wakeup(fs->first_object);
1410 vm_object_chain_release_all(
1411 fs->first_object, fs->object);
1412 if (fs->object != fs->first_object)
1413 vm_object_drop(fs->object);
1414 unlock_and_deallocate(fs);
1415 return (KERN_TRY_AGAIN);
1419 break; /* break to PAGE HAS BEEN FOUND */
1423 * Remove the bogus page (which does not exist at this
1424 * object/offset); before doing so, we must get back
1425 * our object lock to preserve our invariant.
1427 * Also wake up any other process that may want to bring
1430 * If this is the top-level object, we must leave the
1431 * busy page to prevent another process from rushing
1432 * past us, and inserting the page in that object at
1433 * the same time that we are.
1435 if (rv == VM_PAGER_ERROR) {
1437 kprintf("vm_fault: pager read error, "
1442 kprintf("vm_fault: pager read error, "
1450 * Data outside the range of the pager or an I/O error
1452 * The page may have been wired during the pagein,
1453 * e.g. by the buffer cache, and cannot simply be
1454 * freed. Call vnode_pager_freepage() to deal with it.
1457 * XXX - the check for kernel_map is a kludge to work
1458 * around having the machine panic on a kernel space
1459 * fault w/ I/O error.
1461 if (((fs->map != &kernel_map) &&
1462 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1463 vnode_pager_freepage(fs->m);
1465 vm_object_pip_wakeup(fs->first_object);
1466 vm_object_chain_release_all(fs->first_object,
1468 if (fs->object != fs->first_object)
1469 vm_object_drop(fs->object);
1470 unlock_and_deallocate(fs);
1471 if (rv == VM_PAGER_ERROR)
1472 return (KERN_FAILURE);
1474 return (KERN_PROTECTION_FAILURE);
1477 if (fs->object != fs->first_object) {
1478 vnode_pager_freepage(fs->m);
1481 * XXX - we cannot just fall out at this
1482 * point, m has been freed and is invalid!
1488 * We get here if the object has a default pager (or unwiring)
1489 * or the pager doesn't have the page.
1491 if (fs->object == fs->first_object)
1492 fs->first_m = fs->m;
1495 * Move on to the next object. The chain lock should prevent
1496 * the backing_object from getting ripped out from under us.
1498 if ((next_object = fs->object->backing_object) != NULL) {
1499 vm_object_hold(next_object);
1500 vm_object_chain_acquire(next_object);
1501 KKASSERT(next_object == fs->object->backing_object);
1502 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1505 if (next_object == NULL) {
1507 * If there's no object left, fill the page in the top
1508 * object with zeros.
1510 if (fs->object != fs->first_object) {
1511 if (fs->first_object->backing_object !=
1513 vm_object_hold(fs->first_object->backing_object);
1515 vm_object_chain_release_all(
1516 fs->first_object->backing_object,
1518 if (fs->first_object->backing_object !=
1520 vm_object_drop(fs->first_object->backing_object);
1522 vm_object_pip_wakeup(fs->object);
1523 vm_object_drop(fs->object);
1524 fs->object = fs->first_object;
1525 pindex = first_pindex;
1526 fs->m = fs->first_m;
1531 * Zero the page if necessary and mark it valid.
1533 if ((fs->m->flags & PG_ZERO) == 0) {
1534 vm_page_zero_fill(fs->m);
1537 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1539 vm_page_flag_clear(fs->m, PG_ZERO);
1540 mycpu->gd_cnt.v_ozfod++;
1542 mycpu->gd_cnt.v_zfod++;
1543 fs->m->valid = VM_PAGE_BITS_ALL;
1544 break; /* break to PAGE HAS BEEN FOUND */
1546 if (fs->object != fs->first_object) {
1547 vm_object_pip_wakeup(fs->object);
1548 vm_object_lock_swap();
1549 vm_object_drop(fs->object);
1551 KASSERT(fs->object != next_object,
1552 ("object loop %p", next_object));
1553 fs->object = next_object;
1554 vm_object_pip_add(fs->object, 1);
1558 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1561 * object still held.
1563 * If the page is being written, but isn't already owned by the
1564 * top-level object, we have to copy it into a new page owned by the
1567 KASSERT((fs->m->flags & PG_BUSY) != 0,
1568 ("vm_fault: not busy after main loop"));
1570 if (fs->object != fs->first_object) {
1572 * We only really need to copy if we want to write it.
1574 if (fault_type & VM_PROT_WRITE) {
1576 * This allows pages to be virtually copied from a
1577 * backing_object into the first_object, where the
1578 * backing object has no other refs to it, and cannot
1579 * gain any more refs. Instead of a bcopy, we just
1580 * move the page from the backing object to the
1581 * first object. Note that we must mark the page
1582 * dirty in the first object so that it will go out
1583 * to swap when needed.
1587 * Map, if present, has not changed
1590 fs->map_generation == fs->map->timestamp) &&
1592 * Only one shadow object
1594 (fs->object->shadow_count == 1) &&
1596 * No COW refs, except us
1598 (fs->object->ref_count == 1) &&
1600 * No one else can look this object up
1602 (fs->object->handle == NULL) &&
1604 * No other ways to look the object up
1606 ((fs->object->type == OBJT_DEFAULT) ||
1607 (fs->object->type == OBJT_SWAP)) &&
1609 * We don't chase down the shadow chain
1611 (fs->object == fs->first_object->backing_object) &&
1614 * grab the lock if we need to
1616 (fs->lookup_still_valid ||
1618 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1621 * (first_m) and (m) are both busied. We have
1622 * move (m) into (first_m)'s object/pindex
1623 * in an atomic fashion, then free (first_m).
1625 * first_object is held so second remove
1626 * followed by the rename should wind
1627 * up being atomic. vm_page_free() might
1628 * block so we don't do it until after the
1631 fs->lookup_still_valid = 1;
1632 vm_page_protect(fs->first_m, VM_PROT_NONE);
1633 vm_page_remove(fs->first_m);
1634 vm_page_rename(fs->m, fs->first_object,
1636 vm_page_free(fs->first_m);
1637 fs->first_m = fs->m;
1639 mycpu->gd_cnt.v_cow_optim++;
1642 * Oh, well, lets copy it.
1644 * Why are we unmapping the original page
1645 * here? Well, in short, not all accessors
1646 * of user memory go through the pmap. The
1647 * procfs code doesn't have access user memory
1648 * via a local pmap, so vm_fault_page*()
1649 * can't call pmap_enter(). And the umtx*()
1650 * code may modify the COW'd page via a DMAP
1651 * or kernel mapping and not via the pmap,
1652 * leaving the original page still mapped
1653 * read-only into the pmap.
1655 * So we have to remove the page from at
1656 * least the current pmap if it is in it.
1657 * Just remove it from all pmaps.
1659 vm_page_copy(fs->m, fs->first_m);
1660 vm_page_protect(fs->m, VM_PROT_NONE);
1661 vm_page_event(fs->m, VMEVENT_COW);
1666 * We no longer need the old page or object.
1672 * We intend to revert to first_object, undo the
1673 * chain lock through to that.
1675 if (fs->first_object->backing_object != fs->object)
1676 vm_object_hold(fs->first_object->backing_object);
1677 vm_object_chain_release_all(
1678 fs->first_object->backing_object,
1680 if (fs->first_object->backing_object != fs->object)
1681 vm_object_drop(fs->first_object->backing_object);
1684 * fs->object != fs->first_object due to above
1687 vm_object_pip_wakeup(fs->object);
1688 vm_object_drop(fs->object);
1691 * Only use the new page below...
1694 mycpu->gd_cnt.v_cow_faults++;
1695 fs->m = fs->first_m;
1696 fs->object = fs->first_object;
1697 pindex = first_pindex;
1700 * If it wasn't a write fault avoid having to copy
1701 * the page by mapping it read-only.
1703 fs->prot &= ~VM_PROT_WRITE;
1708 * Relock the map if necessary, then check the generation count.
1709 * relock_map() will update fs->timestamp to account for the
1710 * relocking if necessary.
1712 * If the count has changed after relocking then all sorts of
1713 * crap may have happened and we have to retry.
1715 * NOTE: The relock_map() can fail due to a deadlock against
1716 * the vm_page we are holding BUSY.
1718 if (fs->lookup_still_valid == FALSE && fs->map) {
1719 if (relock_map(fs) ||
1720 fs->map->timestamp != fs->map_generation) {
1722 vm_object_pip_wakeup(fs->first_object);
1723 vm_object_chain_release_all(fs->first_object,
1725 if (fs->object != fs->first_object)
1726 vm_object_drop(fs->object);
1727 unlock_and_deallocate(fs);
1728 return (KERN_TRY_AGAIN);
1733 * If the fault is a write, we know that this page is being
1734 * written NOW so dirty it explicitly to save on pmap_is_modified()
1737 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1738 * if the page is already dirty to prevent data written with
1739 * the expectation of being synced from not being synced.
1740 * Likewise if this entry does not request NOSYNC then make
1741 * sure the page isn't marked NOSYNC. Applications sharing
1742 * data should use the same flags to avoid ping ponging.
1744 * Also tell the backing pager, if any, that it should remove
1745 * any swap backing since the page is now dirty.
1747 vm_page_activate(fs->m);
1748 if (fs->prot & VM_PROT_WRITE) {
1749 vm_object_set_writeable_dirty(fs->m->object);
1750 vm_set_nosync(fs->m, fs->entry);
1751 if (fs->fault_flags & VM_FAULT_DIRTY) {
1752 vm_page_dirty(fs->m);
1753 swap_pager_unswapped(fs->m);
1757 vm_object_pip_wakeup(fs->first_object);
1758 vm_object_chain_release_all(fs->first_object, fs->object);
1759 if (fs->object != fs->first_object)
1760 vm_object_drop(fs->object);
1763 * Page had better still be busy. We are still locked up and
1764 * fs->object will have another PIP reference if it is not equal
1765 * to fs->first_object.
1767 KASSERT(fs->m->flags & PG_BUSY,
1768 ("vm_fault: page %p not busy!", fs->m));
1771 * Sanity check: page must be completely valid or it is not fit to
1772 * map into user space. vm_pager_get_pages() ensures this.
1774 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1775 vm_page_zero_invalid(fs->m, TRUE);
1776 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1778 vm_page_flag_clear(fs->m, PG_ZERO);
1780 return (KERN_SUCCESS);
1784 * Wire down a range of virtual addresses in a map. The entry in question
1785 * should be marked in-transition and the map must be locked. We must
1786 * release the map temporarily while faulting-in the page to avoid a
1787 * deadlock. Note that the entry may be clipped while we are blocked but
1788 * will never be freed.
1793 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1795 boolean_t fictitious;
1804 lwkt_gettoken(&map->token);
1806 pmap = vm_map_pmap(map);
1807 start = entry->start;
1809 fictitious = entry->object.vm_object &&
1810 (entry->object.vm_object->type == OBJT_DEVICE);
1811 if (entry->eflags & MAP_ENTRY_KSTACK)
1817 * We simulate a fault to get the page and enter it in the physical
1820 for (va = start; va < end; va += PAGE_SIZE) {
1822 rv = vm_fault(map, va, VM_PROT_READ,
1823 VM_FAULT_USER_WIRE);
1825 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1826 VM_FAULT_CHANGE_WIRING);
1829 while (va > start) {
1831 if ((pa = pmap_extract(pmap, va)) == 0)
1833 pmap_change_wiring(pmap, va, FALSE);
1835 m = PHYS_TO_VM_PAGE(pa);
1836 vm_page_busy_wait(m, FALSE, "vmwrpg");
1837 vm_page_unwire(m, 1);
1847 lwkt_reltoken(&map->token);
1852 * Unwire a range of virtual addresses in a map. The map should be
1856 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1858 boolean_t fictitious;
1866 lwkt_gettoken(&map->token);
1868 pmap = vm_map_pmap(map);
1869 start = entry->start;
1871 fictitious = entry->object.vm_object &&
1872 (entry->object.vm_object->type == OBJT_DEVICE);
1873 if (entry->eflags & MAP_ENTRY_KSTACK)
1877 * Since the pages are wired down, we must be able to get their
1878 * mappings from the physical map system.
1880 for (va = start; va < end; va += PAGE_SIZE) {
1881 pa = pmap_extract(pmap, va);
1883 pmap_change_wiring(pmap, va, FALSE);
1885 m = PHYS_TO_VM_PAGE(pa);
1886 vm_page_busy_wait(m, FALSE, "vmwupg");
1887 vm_page_unwire(m, 1);
1892 lwkt_reltoken(&map->token);
1896 * Copy all of the pages from a wired-down map entry to another.
1898 * The source and destination maps must be locked for write.
1899 * The source and destination maps token must be held
1900 * The source map entry must be wired down (or be a sharing map
1901 * entry corresponding to a main map entry that is wired down).
1903 * No other requirements.
1906 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1907 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1909 vm_object_t dst_object;
1910 vm_object_t src_object;
1911 vm_ooffset_t dst_offset;
1912 vm_ooffset_t src_offset;
1918 src_object = src_entry->object.vm_object;
1919 src_offset = src_entry->offset;
1922 * Create the top-level object for the destination entry. (Doesn't
1923 * actually shadow anything - we copy the pages directly.)
1925 vm_map_entry_allocate_object(dst_entry);
1926 dst_object = dst_entry->object.vm_object;
1928 prot = dst_entry->max_protection;
1931 * Loop through all of the pages in the entry's range, copying each
1932 * one from the source object (it should be there) to the destination
1935 vm_object_hold(src_object);
1936 vm_object_hold(dst_object);
1937 for (vaddr = dst_entry->start, dst_offset = 0;
1938 vaddr < dst_entry->end;
1939 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1942 * Allocate a page in the destination object
1945 dst_m = vm_page_alloc(dst_object,
1946 OFF_TO_IDX(dst_offset),
1948 if (dst_m == NULL) {
1951 } while (dst_m == NULL);
1954 * Find the page in the source object, and copy it in.
1955 * (Because the source is wired down, the page will be in
1958 src_m = vm_page_lookup(src_object,
1959 OFF_TO_IDX(dst_offset + src_offset));
1961 panic("vm_fault_copy_wired: page missing");
1963 vm_page_copy(src_m, dst_m);
1964 vm_page_event(src_m, VMEVENT_COW);
1967 * Enter it in the pmap...
1970 vm_page_flag_clear(dst_m, PG_ZERO);
1971 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1974 * Mark it no longer busy, and put it on the active list.
1976 vm_page_activate(dst_m);
1977 vm_page_wakeup(dst_m);
1979 vm_object_drop(dst_object);
1980 vm_object_drop(src_object);
1986 * This routine checks around the requested page for other pages that
1987 * might be able to be faulted in. This routine brackets the viable
1988 * pages for the pages to be paged in.
1991 * m, rbehind, rahead
1994 * marray (array of vm_page_t), reqpage (index of requested page)
1997 * number of pages in marray
2000 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2001 vm_page_t *marray, int *reqpage)
2005 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2007 int cbehind, cahead;
2013 * we don't fault-ahead for device pager
2015 if (object->type == OBJT_DEVICE) {
2022 * if the requested page is not available, then give up now
2024 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2025 *reqpage = 0; /* not used by caller, fix compiler warn */
2029 if ((cbehind == 0) && (cahead == 0)) {
2035 if (rahead > cahead) {
2039 if (rbehind > cbehind) {
2044 * Do not do any readahead if we have insufficient free memory.
2046 * XXX code was broken disabled before and has instability
2047 * with this conditonal fixed, so shortcut for now.
2049 if (burst_fault == 0 || vm_page_count_severe()) {
2056 * scan backward for the read behind pages -- in memory
2058 * Assume that if the page is not found an interrupt will not
2059 * create it. Theoretically interrupts can only remove (busy)
2060 * pages, not create new associations.
2063 if (rbehind > pindex) {
2067 startpindex = pindex - rbehind;
2070 vm_object_hold(object);
2071 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2072 if (vm_page_lookup(object, tpindex - 1))
2077 while (tpindex < pindex) {
2078 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2081 for (j = 0; j < i; j++) {
2082 vm_page_free(marray[j]);
2084 vm_object_drop(object);
2093 vm_object_drop(object);
2099 * Assign requested page
2106 * Scan forwards for read-ahead pages
2108 tpindex = pindex + 1;
2109 endpindex = tpindex + rahead;
2110 if (endpindex > object->size)
2111 endpindex = object->size;
2113 vm_object_hold(object);
2114 while (tpindex < endpindex) {
2115 if (vm_page_lookup(object, tpindex))
2117 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2125 vm_object_drop(object);
2133 * vm_prefault() provides a quick way of clustering pagefaults into a
2134 * processes address space. It is a "cousin" of pmap_object_init_pt,
2135 * except it runs at page fault time instead of mmap time.
2137 * vm.fast_fault Enables pre-faulting zero-fill pages
2139 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2140 * prefault. Scan stops in either direction when
2141 * a page is found to already exist.
2143 * This code used to be per-platform pmap_prefault(). It is now
2144 * machine-independent and enhanced to also pre-fault zero-fill pages
2145 * (see vm.fast_fault) as well as make them writable, which greatly
2146 * reduces the number of page faults programs incur.
2148 * Application performance when pre-faulting zero-fill pages is heavily
2149 * dependent on the application. Very tiny applications like /bin/echo
2150 * lose a little performance while applications of any appreciable size
2151 * gain performance. Prefaulting multiple pages also reduces SMP
2152 * congestion and can improve SMP performance significantly.
2154 * NOTE! prot may allow writing but this only applies to the top level
2155 * object. If we wind up mapping a page extracted from a backing
2156 * object we have to make sure it is read-only.
2158 * NOTE! The caller has already handled any COW operations on the
2159 * vm_map_entry via the normal fault code. Do NOT call this
2160 * shortcut unless the normal fault code has run on this entry.
2162 * The related map must be locked.
2163 * No other requirements.
2165 static int vm_prefault_pages = 8;
2166 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2167 "Maximum number of pages to pre-fault");
2168 static int vm_fast_fault = 1;
2169 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2170 "Burst fault zero-fill regions");
2173 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2174 * is not already dirty by other means. This will prevent passive
2175 * filesystem syncing as well as 'sync' from writing out the page.
2178 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2180 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2182 vm_page_flag_set(m, PG_NOSYNC);
2184 vm_page_flag_clear(m, PG_NOSYNC);
2189 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2205 * Get stable max count value, disabled if set to 0
2207 maxpages = vm_prefault_pages;
2213 * We do not currently prefault mappings that use virtual page
2214 * tables. We do not prefault foreign pmaps.
2216 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2218 lp = curthread->td_lwp;
2219 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2223 * Limit pre-fault count to 1024 pages.
2225 if (maxpages > 1024)
2228 object = entry->object.vm_object;
2229 KKASSERT(object != NULL);
2230 KKASSERT(object == entry->object.vm_object);
2231 vm_object_hold(object);
2232 vm_object_chain_acquire(object);
2236 for (i = 0; i < maxpages; ++i) {
2237 vm_object_t lobject;
2238 vm_object_t nobject;
2243 * This can eat a lot of time on a heavily contended
2244 * machine so yield on the tick if needed.
2250 * Calculate the page to pre-fault, stopping the scan in
2251 * each direction separately if the limit is reached.
2256 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2260 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2262 if (addr < entry->start) {
2268 if (addr >= entry->end) {
2276 * Skip pages already mapped, and stop scanning in that
2277 * direction. When the scan terminates in both directions
2280 if (pmap_prefault_ok(pmap, addr) == 0) {
2291 * Follow the VM object chain to obtain the page to be mapped
2294 * If we reach the terminal object without finding a page
2295 * and we determine it would be advantageous, then allocate
2296 * a zero-fill page for the base object. The base object
2297 * is guaranteed to be OBJT_DEFAULT for this case.
2299 * In order to not have to check the pager via *haspage*()
2300 * we stop if any non-default object is encountered. e.g.
2301 * a vnode or swap object would stop the loop.
2303 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2308 KKASSERT(lobject == entry->object.vm_object);
2309 /*vm_object_hold(lobject); implied */
2311 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2312 TRUE, &error)) == NULL) {
2313 if (lobject->type != OBJT_DEFAULT)
2315 if (lobject->backing_object == NULL) {
2316 if (vm_fast_fault == 0)
2318 if ((prot & VM_PROT_WRITE) == 0 ||
2319 vm_page_count_min(0)) {
2324 * NOTE: Allocated from base object
2326 m = vm_page_alloc(object, index,
2335 /* lobject = object .. not needed */
2338 if (lobject->backing_object_offset & PAGE_MASK)
2340 nobject = lobject->backing_object;
2341 vm_object_hold(nobject);
2342 KKASSERT(nobject == lobject->backing_object);
2343 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2344 if (lobject != object) {
2345 vm_object_lock_swap();
2346 vm_object_drop(lobject);
2349 pprot &= ~VM_PROT_WRITE;
2350 vm_object_chain_acquire(lobject);
2354 * NOTE: A non-NULL (m) will be associated with lobject if
2355 * it was found there, otherwise it is probably a
2356 * zero-fill page associated with the base object.
2358 * Give-up if no page is available.
2361 if (lobject != object) {
2362 if (object->backing_object != lobject)
2363 vm_object_hold(object->backing_object);
2364 vm_object_chain_release_all(
2365 object->backing_object, lobject);
2366 if (object->backing_object != lobject)
2367 vm_object_drop(object->backing_object);
2368 vm_object_drop(lobject);
2374 * The object must be marked dirty if we are mapping a
2375 * writable page. m->object is either lobject or object,
2376 * both of which are still held. Do this before we
2377 * potentially drop the object.
2379 if (pprot & VM_PROT_WRITE)
2380 vm_object_set_writeable_dirty(m->object);
2383 * Do not conditionalize on PG_RAM. If pages are present in
2384 * the VM system we assume optimal caching. If caching is
2385 * not optimal the I/O gravy train will be restarted when we
2386 * hit an unavailable page. We do not want to try to restart
2387 * the gravy train now because we really don't know how much
2388 * of the object has been cached. The cost for restarting
2389 * the gravy train should be low (since accesses will likely
2390 * be I/O bound anyway).
2392 if (lobject != object) {
2393 if (object->backing_object != lobject)
2394 vm_object_hold(object->backing_object);
2395 vm_object_chain_release_all(object->backing_object,
2397 if (object->backing_object != lobject)
2398 vm_object_drop(object->backing_object);
2399 vm_object_drop(lobject);
2403 * Enter the page into the pmap if appropriate. If we had
2404 * allocated the page we have to place it on a queue. If not
2405 * we just have to make sure it isn't on the cache queue
2406 * (pages on the cache queue are not allowed to be mapped).
2410 * Page must be zerod.
2412 if ((m->flags & PG_ZERO) == 0) {
2413 vm_page_zero_fill(m);
2416 pmap_page_assertzero(
2417 VM_PAGE_TO_PHYS(m));
2419 vm_page_flag_clear(m, PG_ZERO);
2420 mycpu->gd_cnt.v_ozfod++;
2422 mycpu->gd_cnt.v_zfod++;
2423 m->valid = VM_PAGE_BITS_ALL;
2426 * Handle dirty page case
2428 if (pprot & VM_PROT_WRITE)
2429 vm_set_nosync(m, entry);
2430 pmap_enter(pmap, addr, m, pprot, 0);
2431 mycpu->gd_cnt.v_vm_faults++;
2432 if (curthread->td_lwp)
2433 ++curthread->td_lwp->lwp_ru.ru_minflt;
2434 vm_page_deactivate(m);
2435 if (pprot & VM_PROT_WRITE) {
2436 /*vm_object_set_writeable_dirty(m->object);*/
2437 vm_set_nosync(m, entry);
2438 if (fault_flags & VM_FAULT_DIRTY) {
2441 swap_pager_unswapped(m);
2446 /* couldn't busy page, no wakeup */
2448 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2449 (m->flags & PG_FICTITIOUS) == 0) {
2451 * A fully valid page not undergoing soft I/O can
2452 * be immediately entered into the pmap.
2454 if ((m->queue - m->pc) == PQ_CACHE)
2455 vm_page_deactivate(m);
2456 if (pprot & VM_PROT_WRITE) {
2457 /*vm_object_set_writeable_dirty(m->object);*/
2458 vm_set_nosync(m, entry);
2459 if (fault_flags & VM_FAULT_DIRTY) {
2462 swap_pager_unswapped(m);
2465 if (pprot & VM_PROT_WRITE)
2466 vm_set_nosync(m, entry);
2467 pmap_enter(pmap, addr, m, pprot, 0);
2468 mycpu->gd_cnt.v_vm_faults++;
2469 if (curthread->td_lwp)
2470 ++curthread->td_lwp->lwp_ru.ru_minflt;
2476 vm_object_chain_release(object);
2477 vm_object_drop(object);
2481 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2482 vm_map_entry_t entry, int prot, int fault_flags)
2495 * Get stable max count value, disabled if set to 0
2497 maxpages = vm_prefault_pages;
2503 * We do not currently prefault mappings that use virtual page
2504 * tables. We do not prefault foreign pmaps.
2506 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2508 lp = curthread->td_lwp;
2509 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2513 * Limit pre-fault count to 1024 pages.
2515 if (maxpages > 1024)
2518 object = entry->object.vm_object;
2519 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2520 KKASSERT(object->backing_object == NULL);
2524 for (i = 0; i < maxpages; ++i) {
2528 * Calculate the page to pre-fault, stopping the scan in
2529 * each direction separately if the limit is reached.
2534 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2538 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2540 if (addr < entry->start) {
2546 if (addr >= entry->end) {
2554 * Skip pages already mapped, and stop scanning in that
2555 * direction. When the scan terminates in both directions
2558 if (pmap_prefault_ok(pmap, addr) == 0) {
2569 * Follow the VM object chain to obtain the page to be mapped
2570 * into the pmap. This version of the prefault code only
2571 * works with terminal objects.
2573 * WARNING! We cannot call swap_pager_unswapped() with a
2576 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2578 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
2579 if (m == NULL || error)
2582 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2583 (m->flags & PG_FICTITIOUS) == 0 &&
2584 ((m->flags & PG_SWAPPED) == 0 ||
2585 (prot & VM_PROT_WRITE) == 0 ||
2586 (fault_flags & VM_FAULT_DIRTY) == 0)) {
2588 * A fully valid page not undergoing soft I/O can
2589 * be immediately entered into the pmap.
2591 if ((m->queue - m->pc) == PQ_CACHE)
2592 vm_page_deactivate(m);
2593 if (prot & VM_PROT_WRITE) {
2594 vm_object_set_writeable_dirty(m->object);
2595 vm_set_nosync(m, entry);
2596 if (fault_flags & VM_FAULT_DIRTY) {
2599 swap_pager_unswapped(m);
2602 pmap_enter(pmap, addr, m, prot, 0);
2603 mycpu->gd_cnt.v_vm_faults++;
2604 if (curthread->td_lwp)
2605 ++curthread->td_lwp->lwp_ru.ru_minflt;