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;
126 static int vm_fast_fault = 1;
127 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
128 "Burst fault zero-fill regions");
129 static int debug_cluster = 0;
130 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
132 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
133 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
135 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
137 static int vm_fault_ratelimit(struct vmspace *);
138 static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry,
142 * The caller must hold vm_token.
145 release_page(struct faultstate *fs)
147 vm_page_deactivate(fs->m);
148 vm_page_wakeup(fs->m);
153 * The caller must hold vm_token.
156 unlock_map(struct faultstate *fs)
158 if (fs->lookup_still_valid && fs->map) {
159 vm_map_lookup_done(fs->map, fs->entry, 0);
160 fs->lookup_still_valid = FALSE;
165 * Clean up after a successful call to vm_fault_object() so another call
166 * to vm_fault_object() can be made.
168 * The caller must hold vm_token.
171 _cleanup_successful_fault(struct faultstate *fs, int relock)
173 if (fs->object != fs->first_object) {
174 vm_page_free(fs->first_m);
175 vm_object_pip_wakeup(fs->object);
178 fs->object = fs->first_object;
179 if (relock && fs->lookup_still_valid == FALSE) {
181 vm_map_lock_read(fs->map);
182 fs->lookup_still_valid = TRUE;
187 * The caller must hold vm_token.
190 _unlock_things(struct faultstate *fs, int dealloc)
192 vm_object_pip_wakeup(fs->first_object);
193 _cleanup_successful_fault(fs, 0);
195 vm_object_deallocate(fs->first_object);
196 fs->first_object = NULL;
199 if (fs->vp != NULL) {
205 #define unlock_things(fs) _unlock_things(fs, 0)
206 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
207 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
212 * Determine if the pager for the current object *might* contain the page.
214 * We only need to try the pager if this is not a default object (default
215 * objects are zero-fill and have no real pager), and if we are not taking
216 * a wiring fault or if the FS entry is wired.
218 #define TRYPAGER(fs) \
219 (fs->object->type != OBJT_DEFAULT && \
220 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
225 * Handle a page fault occuring at the given address, requiring the given
226 * permissions, in the map specified. If successful, the page is inserted
227 * into the associated physical map.
229 * NOTE: The given address should be truncated to the proper page address.
231 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
232 * a standard error specifying why the fault is fatal is returned.
234 * The map in question must be referenced, and remains so.
235 * The caller may hold no locks.
236 * No other requirements.
239 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
242 vm_pindex_t first_pindex;
243 struct faultstate fs;
246 mycpu->gd_cnt.v_vm_faults++;
250 fs.fault_flags = fault_flags;
255 * Find the vm_map_entry representing the backing store and resolve
256 * the top level object and page index. This may have the side
257 * effect of executing a copy-on-write on the map entry and/or
258 * creating a shadow object, but will not COW any actual VM pages.
260 * On success fs.map is left read-locked and various other fields
261 * are initialized but not otherwise referenced or locked.
263 * NOTE! vm_map_lookup will try to upgrade the fault_type to
264 * VM_FAULT_WRITE if the map entry is a virtual page table and also
265 * writable, so we can set the 'A'accessed bit in the virtual page
269 result = vm_map_lookup(&fs.map, vaddr, fault_type,
270 &fs.entry, &fs.first_object,
271 &first_pindex, &fs.first_prot, &fs.wired);
274 * If the lookup failed or the map protections are incompatible,
275 * the fault generally fails. However, if the caller is trying
276 * to do a user wiring we have more work to do.
278 if (result != KERN_SUCCESS) {
279 if (result != KERN_PROTECTION_FAILURE ||
280 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
282 if (result == KERN_INVALID_ADDRESS && growstack &&
283 map != &kernel_map && curproc != NULL) {
284 result = vm_map_growstack(curproc, vaddr);
285 if (result != KERN_SUCCESS)
286 return (KERN_FAILURE);
294 * If we are user-wiring a r/w segment, and it is COW, then
295 * we need to do the COW operation. Note that we don't
296 * currently COW RO sections now, because it is NOT desirable
297 * to COW .text. We simply keep .text from ever being COW'ed
298 * and take the heat that one cannot debug wired .text sections.
300 result = vm_map_lookup(&fs.map, vaddr,
301 VM_PROT_READ|VM_PROT_WRITE|
302 VM_PROT_OVERRIDE_WRITE,
303 &fs.entry, &fs.first_object,
304 &first_pindex, &fs.first_prot,
306 if (result != KERN_SUCCESS)
310 * If we don't COW now, on a user wire, the user will never
311 * be able to write to the mapping. If we don't make this
312 * restriction, the bookkeeping would be nearly impossible.
314 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
315 fs.entry->max_protection &= ~VM_PROT_WRITE;
319 * fs.map is read-locked
321 * Misc checks. Save the map generation number to detect races.
323 fs.map_generation = fs.map->timestamp;
325 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
326 panic("vm_fault: fault on nofault entry, addr: %lx",
331 * A system map entry may return a NULL object. No object means
332 * no pager means an unrecoverable kernel fault.
334 if (fs.first_object == NULL) {
335 panic("vm_fault: unrecoverable fault at %p in entry %p",
336 (void *)vaddr, fs.entry);
340 * Make a reference to this object to prevent its disposal while we
341 * are messing with it. Once we have the reference, the map is free
342 * to be diddled. Since objects reference their shadows (and copies),
343 * they will stay around as well.
345 * Bump the paging-in-progress count to prevent size changes (e.g.
346 * truncation operations) during I/O. This must be done after
347 * obtaining the vnode lock in order to avoid possible deadlocks.
349 * The vm_token is needed to manipulate the vm_object
351 lwkt_gettoken(&vm_token);
352 vm_object_reference(fs.first_object);
353 fs.vp = vnode_pager_lock(fs.first_object);
354 vm_object_pip_add(fs.first_object, 1);
355 lwkt_reltoken(&vm_token);
357 fs.lookup_still_valid = TRUE;
359 fs.object = fs.first_object; /* so unlock_and_deallocate works */
362 * If the entry is wired we cannot change the page protection.
365 fault_type = fs.first_prot;
368 * The page we want is at (first_object, first_pindex), but if the
369 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
370 * page table to figure out the actual pindex.
372 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
375 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
376 result = vm_fault_vpagetable(&fs, &first_pindex,
377 fs.entry->aux.master_pde,
379 if (result == KERN_TRY_AGAIN)
381 if (result != KERN_SUCCESS)
386 * Now we have the actual (object, pindex), fault in the page. If
387 * vm_fault_object() fails it will unlock and deallocate the FS
388 * data. If it succeeds everything remains locked and fs->object
389 * will have an additional PIP count if it is not equal to
392 * vm_fault_object will set fs->prot for the pmap operation. It is
393 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
394 * page can be safely written. However, it will force a read-only
395 * mapping for a read fault if the memory is managed by a virtual
398 result = vm_fault_object(&fs, first_pindex, fault_type);
400 if (result == KERN_TRY_AGAIN)
402 if (result != KERN_SUCCESS)
406 * On success vm_fault_object() does not unlock or deallocate, and fs.m
407 * will contain a busied page.
409 * Enter the page into the pmap and do pmap-related adjustments.
411 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
414 * Burst in a few more pages if possible. The fs.map should still
417 if (fault_flags & VM_FAULT_BURST) {
418 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
420 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
425 vm_page_flag_clear(fs.m, PG_ZERO);
426 vm_page_flag_set(fs.m, PG_REFERENCED);
429 * If the page is not wired down, then put it where the pageout daemon
432 * We do not really need to get vm_token here but since all the
433 * vm_*() calls have to doing it here improves efficiency.
435 lwkt_gettoken(&vm_token);
436 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
440 vm_page_unwire(fs.m, 1);
442 vm_page_activate(fs.m);
445 if (curthread->td_lwp) {
447 curthread->td_lwp->lwp_ru.ru_majflt++;
449 curthread->td_lwp->lwp_ru.ru_minflt++;
454 * Unlock everything, and return
456 vm_page_wakeup(fs.m);
457 vm_object_deallocate(fs.first_object);
458 lwkt_reltoken(&vm_token);
460 return (KERN_SUCCESS);
464 * Fault in the specified virtual address in the current process map,
465 * returning a held VM page or NULL. See vm_fault_page() for more
471 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
473 struct lwp *lp = curthread->td_lwp;
476 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
477 fault_type, VM_FAULT_NORMAL, errorp);
482 * Fault in the specified virtual address in the specified map, doing all
483 * necessary manipulation of the object store and all necessary I/O. Return
484 * a held VM page or NULL, and set *errorp. The related pmap is not
487 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
488 * and marked PG_REFERENCED as well.
490 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
491 * error will be returned.
496 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
497 int fault_flags, int *errorp)
499 vm_pindex_t first_pindex;
500 struct faultstate fs;
502 vm_prot_t orig_fault_type = fault_type;
504 mycpu->gd_cnt.v_vm_faults++;
508 fs.fault_flags = fault_flags;
509 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
513 * Find the vm_map_entry representing the backing store and resolve
514 * the top level object and page index. This may have the side
515 * effect of executing a copy-on-write on the map entry and/or
516 * creating a shadow object, but will not COW any actual VM pages.
518 * On success fs.map is left read-locked and various other fields
519 * are initialized but not otherwise referenced or locked.
521 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
522 * if the map entry is a virtual page table and also writable,
523 * so we can set the 'A'accessed bit in the virtual page table entry.
526 result = vm_map_lookup(&fs.map, vaddr, fault_type,
527 &fs.entry, &fs.first_object,
528 &first_pindex, &fs.first_prot, &fs.wired);
530 if (result != KERN_SUCCESS) {
536 * fs.map is read-locked
538 * Misc checks. Save the map generation number to detect races.
540 fs.map_generation = fs.map->timestamp;
542 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
543 panic("vm_fault: fault on nofault entry, addr: %lx",
548 * A system map entry may return a NULL object. No object means
549 * no pager means an unrecoverable kernel fault.
551 if (fs.first_object == NULL) {
552 panic("vm_fault: unrecoverable fault at %p in entry %p",
553 (void *)vaddr, fs.entry);
557 * Make a reference to this object to prevent its disposal while we
558 * are messing with it. Once we have the reference, the map is free
559 * to be diddled. Since objects reference their shadows (and copies),
560 * they will stay around as well.
562 * Bump the paging-in-progress count to prevent size changes (e.g.
563 * truncation operations) during I/O. This must be done after
564 * obtaining the vnode lock in order to avoid possible deadlocks.
566 * The vm_token is needed to manipulate the vm_object
568 lwkt_gettoken(&vm_token);
569 vm_object_reference(fs.first_object);
570 fs.vp = vnode_pager_lock(fs.first_object);
571 vm_object_pip_add(fs.first_object, 1);
572 lwkt_reltoken(&vm_token);
574 fs.lookup_still_valid = TRUE;
576 fs.object = fs.first_object; /* so unlock_and_deallocate works */
579 * If the entry is wired we cannot change the page protection.
582 fault_type = fs.first_prot;
585 * The page we want is at (first_object, first_pindex), but if the
586 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
587 * page table to figure out the actual pindex.
589 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
592 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
593 result = vm_fault_vpagetable(&fs, &first_pindex,
594 fs.entry->aux.master_pde,
596 if (result == KERN_TRY_AGAIN)
598 if (result != KERN_SUCCESS) {
605 * Now we have the actual (object, pindex), fault in the page. If
606 * vm_fault_object() fails it will unlock and deallocate the FS
607 * data. If it succeeds everything remains locked and fs->object
608 * will have an additinal PIP count if it is not equal to
611 result = vm_fault_object(&fs, first_pindex, fault_type);
613 if (result == KERN_TRY_AGAIN)
615 if (result != KERN_SUCCESS) {
620 if ((orig_fault_type & VM_PROT_WRITE) &&
621 (fs.prot & VM_PROT_WRITE) == 0) {
622 *errorp = KERN_PROTECTION_FAILURE;
623 unlock_and_deallocate(&fs);
628 * On success vm_fault_object() does not unlock or deallocate, and fs.m
629 * will contain a busied page.
634 * Return a held page. We are not doing any pmap manipulation so do
635 * not set PG_MAPPED. However, adjust the page flags according to
636 * the fault type because the caller may not use a managed pmapping
637 * (so we don't want to lose the fact that the page will be dirtied
638 * if a write fault was specified).
640 lwkt_gettoken(&vm_token);
642 vm_page_flag_clear(fs.m, PG_ZERO);
643 if (fault_type & VM_PROT_WRITE)
647 * Update the pmap. We really only have to do this if a COW
648 * occured to replace the read-only page with the new page. For
649 * now just do it unconditionally. XXX
651 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
652 vm_page_flag_set(fs.m, PG_REFERENCED);
655 * Unbusy the page by activating it. It remains held and will not
658 vm_page_activate(fs.m);
660 if (curthread->td_lwp) {
662 curthread->td_lwp->lwp_ru.ru_majflt++;
664 curthread->td_lwp->lwp_ru.ru_minflt++;
669 * Unlock everything, and return the held page.
671 vm_page_wakeup(fs.m);
672 vm_object_deallocate(fs.first_object);
673 lwkt_reltoken(&vm_token);
680 * Fault in the specified (object,offset), dirty the returned page as
681 * needed. If the requested fault_type cannot be done NULL and an
684 * A held (but not busied) page is returned.
689 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
690 vm_prot_t fault_type, int fault_flags, int *errorp)
693 vm_pindex_t first_pindex;
694 struct faultstate fs;
695 struct vm_map_entry entry;
697 bzero(&entry, sizeof(entry));
698 entry.object.vm_object = object;
699 entry.maptype = VM_MAPTYPE_NORMAL;
700 entry.protection = entry.max_protection = fault_type;
704 fs.fault_flags = fault_flags;
706 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
710 fs.first_object = object;
711 first_pindex = OFF_TO_IDX(offset);
713 fs.first_prot = fault_type;
715 /*fs.map_generation = 0; unused */
718 * Make a reference to this object to prevent its disposal while we
719 * are messing with it. Once we have the reference, the map is free
720 * to be diddled. Since objects reference their shadows (and copies),
721 * they will stay around as well.
723 * Bump the paging-in-progress count to prevent size changes (e.g.
724 * truncation operations) during I/O. This must be done after
725 * obtaining the vnode lock in order to avoid possible deadlocks.
727 lwkt_gettoken(&vm_token);
728 vm_object_reference(fs.first_object);
729 fs.vp = vnode_pager_lock(fs.first_object);
730 vm_object_pip_add(fs.first_object, 1);
731 lwkt_reltoken(&vm_token);
733 fs.lookup_still_valid = TRUE;
735 fs.object = fs.first_object; /* so unlock_and_deallocate works */
738 /* XXX future - ability to operate on VM object using vpagetable */
739 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
740 result = vm_fault_vpagetable(&fs, &first_pindex,
741 fs.entry->aux.master_pde,
743 if (result == KERN_TRY_AGAIN)
745 if (result != KERN_SUCCESS) {
753 * Now we have the actual (object, pindex), fault in the page. If
754 * vm_fault_object() fails it will unlock and deallocate the FS
755 * data. If it succeeds everything remains locked and fs->object
756 * will have an additinal PIP count if it is not equal to
759 result = vm_fault_object(&fs, first_pindex, fault_type);
761 if (result == KERN_TRY_AGAIN)
763 if (result != KERN_SUCCESS) {
768 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
769 *errorp = KERN_PROTECTION_FAILURE;
770 unlock_and_deallocate(&fs);
775 * On success vm_fault_object() does not unlock or deallocate, and fs.m
776 * will contain a busied page.
781 * Return a held page. We are not doing any pmap manipulation so do
782 * not set PG_MAPPED. However, adjust the page flags according to
783 * the fault type because the caller may not use a managed pmapping
784 * (so we don't want to lose the fact that the page will be dirtied
785 * if a write fault was specified).
787 lwkt_gettoken(&vm_token);
789 vm_page_flag_clear(fs.m, PG_ZERO);
790 if (fault_type & VM_PROT_WRITE)
794 * Indicate that the page was accessed.
796 vm_page_flag_set(fs.m, PG_REFERENCED);
799 * Unbusy the page by activating it. It remains held and will not
802 vm_page_activate(fs.m);
804 if (curthread->td_lwp) {
806 mycpu->gd_cnt.v_vm_faults++;
807 curthread->td_lwp->lwp_ru.ru_majflt++;
809 curthread->td_lwp->lwp_ru.ru_minflt++;
814 * Unlock everything, and return the held page.
816 vm_page_wakeup(fs.m);
817 vm_object_deallocate(fs.first_object);
818 lwkt_reltoken(&vm_token);
825 * Translate the virtual page number (first_pindex) that is relative
826 * to the address space into a logical page number that is relative to the
827 * backing object. Use the virtual page table pointed to by (vpte).
829 * This implements an N-level page table. Any level can terminate the
830 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
831 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
833 * No requirements (vm_token need not be held).
837 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
838 vpte_t vpte, int fault_type)
841 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
842 int result = KERN_SUCCESS;
847 * We cannot proceed if the vpte is not valid, not readable
848 * for a read fault, or not writable for a write fault.
850 if ((vpte & VPTE_V) == 0) {
851 unlock_and_deallocate(fs);
852 return (KERN_FAILURE);
854 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
855 unlock_and_deallocate(fs);
856 return (KERN_FAILURE);
858 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
859 unlock_and_deallocate(fs);
860 return (KERN_FAILURE);
862 if ((vpte & VPTE_PS) || vshift == 0)
864 KKASSERT(vshift >= VPTE_PAGE_BITS);
867 * Get the page table page. Nominally we only read the page
868 * table, but since we are actively setting VPTE_M and VPTE_A,
869 * tell vm_fault_object() that we are writing it.
871 * There is currently no real need to optimize this.
873 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
874 VM_PROT_READ|VM_PROT_WRITE);
875 if (result != KERN_SUCCESS)
879 * Process the returned fs.m and look up the page table
880 * entry in the page table page.
882 vshift -= VPTE_PAGE_BITS;
883 lwb = lwbuf_alloc(fs->m);
884 ptep = ((vpte_t *)lwbuf_kva(lwb) +
885 ((*pindex >> vshift) & VPTE_PAGE_MASK));
889 * Page table write-back. If the vpte is valid for the
890 * requested operation, do a write-back to the page table.
892 * XXX VPTE_M is not set properly for page directory pages.
893 * It doesn't get set in the page directory if the page table
894 * is modified during a read access.
896 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
898 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
899 atomic_set_long(ptep, VPTE_M | VPTE_A);
900 vm_page_dirty(fs->m);
903 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
905 if ((vpte & VPTE_A) == 0) {
906 atomic_set_long(ptep, VPTE_A);
907 vm_page_dirty(fs->m);
911 vm_page_flag_set(fs->m, PG_REFERENCED);
912 vm_page_activate(fs->m);
913 vm_page_wakeup(fs->m);
914 cleanup_successful_fault(fs);
917 * Combine remaining address bits with the vpte.
919 /* JG how many bits from each? */
920 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
921 (*pindex & ((1L << vshift) - 1));
922 return (KERN_SUCCESS);
927 * This is the core of the vm_fault code.
929 * Do all operations required to fault-in (fs.first_object, pindex). Run
930 * through the shadow chain as necessary and do required COW or virtual
931 * copy operations. The caller has already fully resolved the vm_map_entry
932 * and, if appropriate, has created a copy-on-write layer. All we need to
933 * do is iterate the object chain.
935 * On failure (fs) is unlocked and deallocated and the caller may return or
936 * retry depending on the failure code. On success (fs) is NOT unlocked or
937 * deallocated, fs.m will contained a resolved, busied page, and fs.object
938 * will have an additional PIP count if it is not equal to fs.first_object.
944 vm_fault_object(struct faultstate *fs,
945 vm_pindex_t first_pindex, vm_prot_t fault_type)
947 vm_object_t next_object;
950 fs->prot = fs->first_prot;
951 fs->object = fs->first_object;
952 pindex = first_pindex;
955 * If a read fault occurs we try to make the page writable if
956 * possible. There are three cases where we cannot make the
957 * page mapping writable:
959 * (1) The mapping is read-only or the VM object is read-only,
960 * fs->prot above will simply not have VM_PROT_WRITE set.
962 * (2) If the mapping is a virtual page table we need to be able
963 * to detect writes so we can set VPTE_M in the virtual page
966 * (3) If the VM page is read-only or copy-on-write, upgrading would
967 * just result in an unnecessary COW fault.
969 * VM_PROT_VPAGED is set if faulting via a virtual page table and
970 * causes adjustments to the 'M'odify bit to also turn off write
971 * access to force a re-fault.
973 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
974 if ((fault_type & VM_PROT_WRITE) == 0)
975 fs->prot &= ~VM_PROT_WRITE;
978 lwkt_gettoken(&vm_token);
982 * If the object is dead, we stop here
984 if (fs->object->flags & OBJ_DEAD) {
985 unlock_and_deallocate(fs);
986 lwkt_reltoken(&vm_token);
987 return (KERN_PROTECTION_FAILURE);
991 * See if page is resident. spl protection is required
992 * to avoid an interrupt unbusy/free race against our
993 * lookup. We must hold the protection through a page
994 * allocation or busy.
997 fs->m = vm_page_lookup(fs->object, pindex);
1001 * Wait/Retry if the page is busy. We have to do this
1002 * if the page is busy via either PG_BUSY or
1003 * vm_page_t->busy because the vm_pager may be using
1004 * vm_page_t->busy for pageouts ( and even pageins if
1005 * it is the vnode pager ), and we could end up trying
1006 * to pagein and pageout the same page simultaneously.
1008 * We can theoretically allow the busy case on a read
1009 * fault if the page is marked valid, but since such
1010 * pages are typically already pmap'd, putting that
1011 * special case in might be more effort then it is
1012 * worth. We cannot under any circumstances mess
1013 * around with a vm_page_t->busy page except, perhaps,
1016 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
1018 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1019 mycpu->gd_cnt.v_intrans++;
1020 vm_object_deallocate(fs->first_object);
1021 fs->first_object = NULL;
1022 lwkt_reltoken(&vm_token);
1024 return (KERN_TRY_AGAIN);
1028 * If reactivating a page from PQ_CACHE we may have
1031 queue = fs->m->queue;
1032 vm_page_unqueue_nowakeup(fs->m);
1034 if ((queue - fs->m->pc) == PQ_CACHE &&
1035 vm_page_count_severe()) {
1036 vm_page_activate(fs->m);
1037 unlock_and_deallocate(fs);
1039 lwkt_reltoken(&vm_token);
1041 return (KERN_TRY_AGAIN);
1045 * Mark page busy for other processes, and the
1046 * pagedaemon. If it still isn't completely valid
1047 * (readable), or if a read-ahead-mark is set on
1048 * the VM page, jump to readrest, else we found the
1049 * page and can return.
1051 * We can release the spl once we have marked the
1054 vm_page_busy(fs->m);
1057 if (fs->m->object != &kernel_object) {
1058 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1062 if (fs->m->flags & PG_RAM) {
1065 vm_page_flag_clear(fs->m, PG_RAM);
1069 break; /* break to PAGE HAS BEEN FOUND */
1073 * Page is not resident, If this is the search termination
1074 * or the pager might contain the page, allocate a new page.
1076 * NOTE: We are still in a critical section.
1078 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1080 * If the page is beyond the object size we fail
1082 if (pindex >= fs->object->size) {
1083 lwkt_reltoken(&vm_token);
1085 unlock_and_deallocate(fs);
1086 return (KERN_PROTECTION_FAILURE);
1092 if (fs->didlimit == 0 && curproc != NULL) {
1095 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1097 lwkt_reltoken(&vm_token);
1099 unlock_and_deallocate(fs);
1100 tsleep(curproc, 0, "vmrate", limticks);
1102 return (KERN_TRY_AGAIN);
1107 * Allocate a new page for this object/offset pair.
1110 if (!vm_page_count_severe()) {
1111 fs->m = vm_page_alloc(fs->object, pindex,
1112 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1114 if (fs->m == NULL) {
1115 lwkt_reltoken(&vm_token);
1117 unlock_and_deallocate(fs);
1119 return (KERN_TRY_AGAIN);
1126 * We have found an invalid or partially valid page, a
1127 * page with a read-ahead mark which might be partially or
1128 * fully valid (and maybe dirty too), or we have allocated
1131 * Attempt to fault-in the page if there is a chance that the
1132 * pager has it, and potentially fault in additional pages
1135 * We are NOT in splvm here and if TRYPAGER is true then
1136 * fs.m will be non-NULL and will be PG_BUSY for us.
1141 u_char behavior = vm_map_entry_behavior(fs->entry);
1143 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1149 * If sequential access is detected then attempt
1150 * to deactivate/cache pages behind the scan to
1151 * prevent resource hogging.
1153 * Use of PG_RAM to detect sequential access
1154 * also simulates multi-zone sequential access
1155 * detection for free.
1157 * NOTE: Partially valid dirty pages cannot be
1158 * deactivated without causing NFS picemeal
1161 if ((fs->first_object->type != OBJT_DEVICE) &&
1162 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1163 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1164 (fs->m->flags & PG_RAM)))
1166 vm_pindex_t scan_pindex;
1167 int scan_count = 16;
1169 if (first_pindex < 16) {
1173 scan_pindex = first_pindex - 16;
1174 if (scan_pindex < 16)
1175 scan_count = scan_pindex;
1181 while (scan_count) {
1184 mt = vm_page_lookup(fs->first_object,
1187 (mt->valid != VM_PAGE_BITS_ALL)) {
1191 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1197 vm_page_test_dirty(mt);
1202 vm_page_deactivate(mt);
1217 * Avoid deadlocking against the map when doing I/O.
1218 * fs.object and the page is PG_BUSY'd.
1223 * Acquire the page data. We still hold a ref on
1224 * fs.object and the page has been PG_BUSY's.
1226 * The pager may replace the page (for example, in
1227 * order to enter a fictitious page into the
1228 * object). If it does so it is responsible for
1229 * cleaning up the passed page and properly setting
1230 * the new page PG_BUSY.
1232 * If we got here through a PG_RAM read-ahead
1233 * mark the page may be partially dirty and thus
1234 * not freeable. Don't bother checking to see
1235 * if the pager has the page because we can't free
1236 * it anyway. We have to depend on the get_page
1237 * operation filling in any gaps whether there is
1238 * backing store or not.
1240 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1242 if (rv == VM_PAGER_OK) {
1244 * Relookup in case pager changed page. Pager
1245 * is responsible for disposition of old page
1248 * XXX other code segments do relookups too.
1249 * It's a bad abstraction that needs to be
1252 fs->m = vm_page_lookup(fs->object, pindex);
1253 if (fs->m == NULL) {
1254 lwkt_reltoken(&vm_token);
1255 unlock_and_deallocate(fs);
1256 return (KERN_TRY_AGAIN);
1260 break; /* break to PAGE HAS BEEN FOUND */
1264 * Remove the bogus page (which does not exist at this
1265 * object/offset); before doing so, we must get back
1266 * our object lock to preserve our invariant.
1268 * Also wake up any other process that may want to bring
1271 * If this is the top-level object, we must leave the
1272 * busy page to prevent another process from rushing
1273 * past us, and inserting the page in that object at
1274 * the same time that we are.
1276 if (rv == VM_PAGER_ERROR) {
1278 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
1280 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
1284 * Data outside the range of the pager or an I/O error
1286 * The page may have been wired during the pagein,
1287 * e.g. by the buffer cache, and cannot simply be
1288 * freed. Call vnode_pager_freepage() to deal with it.
1291 * XXX - the check for kernel_map is a kludge to work
1292 * around having the machine panic on a kernel space
1293 * fault w/ I/O error.
1295 if (((fs->map != &kernel_map) &&
1296 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1297 vnode_pager_freepage(fs->m);
1298 lwkt_reltoken(&vm_token);
1300 unlock_and_deallocate(fs);
1301 if (rv == VM_PAGER_ERROR)
1302 return (KERN_FAILURE);
1304 return (KERN_PROTECTION_FAILURE);
1307 if (fs->object != fs->first_object) {
1308 vnode_pager_freepage(fs->m);
1311 * XXX - we cannot just fall out at this
1312 * point, m has been freed and is invalid!
1318 * We get here if the object has a default pager (or unwiring)
1319 * or the pager doesn't have the page.
1321 if (fs->object == fs->first_object)
1322 fs->first_m = fs->m;
1325 * Move on to the next object. Lock the next object before
1326 * unlocking the current one.
1328 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1329 next_object = fs->object->backing_object;
1330 if (next_object == NULL) {
1332 * If there's no object left, fill the page in the top
1333 * object with zeros.
1335 if (fs->object != fs->first_object) {
1336 vm_object_pip_wakeup(fs->object);
1338 fs->object = fs->first_object;
1339 pindex = first_pindex;
1340 fs->m = fs->first_m;
1345 * Zero the page if necessary and mark it valid.
1347 if ((fs->m->flags & PG_ZERO) == 0) {
1348 vm_page_zero_fill(fs->m);
1350 mycpu->gd_cnt.v_ozfod++;
1352 mycpu->gd_cnt.v_zfod++;
1353 fs->m->valid = VM_PAGE_BITS_ALL;
1354 break; /* break to PAGE HAS BEEN FOUND */
1356 if (fs->object != fs->first_object) {
1357 vm_object_pip_wakeup(fs->object);
1359 KASSERT(fs->object != next_object,
1360 ("object loop %p", next_object));
1361 fs->object = next_object;
1362 vm_object_pip_add(fs->object, 1);
1366 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1369 * vm_token is still held
1371 * If the page is being written, but isn't already owned by the
1372 * top-level object, we have to copy it into a new page owned by the
1375 KASSERT((fs->m->flags & PG_BUSY) != 0,
1376 ("vm_fault: not busy after main loop"));
1378 if (fs->object != fs->first_object) {
1380 * We only really need to copy if we want to write it.
1382 if (fault_type & VM_PROT_WRITE) {
1384 * This allows pages to be virtually copied from a
1385 * backing_object into the first_object, where the
1386 * backing object has no other refs to it, and cannot
1387 * gain any more refs. Instead of a bcopy, we just
1388 * move the page from the backing object to the
1389 * first object. Note that we must mark the page
1390 * dirty in the first object so that it will go out
1391 * to swap when needed.
1395 * Map, if present, has not changed
1398 fs->map_generation == fs->map->timestamp) &&
1400 * Only one shadow object
1402 (fs->object->shadow_count == 1) &&
1404 * No COW refs, except us
1406 (fs->object->ref_count == 1) &&
1408 * No one else can look this object up
1410 (fs->object->handle == NULL) &&
1412 * No other ways to look the object up
1414 ((fs->object->type == OBJT_DEFAULT) ||
1415 (fs->object->type == OBJT_SWAP)) &&
1417 * We don't chase down the shadow chain
1419 (fs->object == fs->first_object->backing_object) &&
1422 * grab the lock if we need to
1424 (fs->lookup_still_valid ||
1426 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1429 fs->lookup_still_valid = 1;
1431 * get rid of the unnecessary page
1433 vm_page_protect(fs->first_m, VM_PROT_NONE);
1434 vm_page_free(fs->first_m);
1438 * grab the page and put it into the
1439 * process'es object. The page is
1440 * automatically made dirty.
1442 vm_page_rename(fs->m, fs->first_object, first_pindex);
1443 fs->first_m = fs->m;
1444 vm_page_busy(fs->first_m);
1446 mycpu->gd_cnt.v_cow_optim++;
1449 * Oh, well, lets copy it.
1451 vm_page_copy(fs->m, fs->first_m);
1452 vm_page_event(fs->m, VMEVENT_COW);
1457 * We no longer need the old page or object.
1463 * fs->object != fs->first_object due to above
1466 vm_object_pip_wakeup(fs->object);
1469 * Only use the new page below...
1472 mycpu->gd_cnt.v_cow_faults++;
1473 fs->m = fs->first_m;
1474 fs->object = fs->first_object;
1475 pindex = first_pindex;
1478 * If it wasn't a write fault avoid having to copy
1479 * the page by mapping it read-only.
1481 fs->prot &= ~VM_PROT_WRITE;
1486 * We may have had to unlock a map to do I/O. If we did then
1487 * lookup_still_valid will be FALSE. If the map generation count
1488 * also changed then all sorts of things could have happened while
1489 * we were doing the I/O and we need to retry.
1492 if (!fs->lookup_still_valid &&
1494 (fs->map->timestamp != fs->map_generation)) {
1496 lwkt_reltoken(&vm_token);
1497 unlock_and_deallocate(fs);
1498 return (KERN_TRY_AGAIN);
1502 * If the fault is a write, we know that this page is being
1503 * written NOW so dirty it explicitly to save on pmap_is_modified()
1506 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1507 * if the page is already dirty to prevent data written with
1508 * the expectation of being synced from not being synced.
1509 * Likewise if this entry does not request NOSYNC then make
1510 * sure the page isn't marked NOSYNC. Applications sharing
1511 * data should use the same flags to avoid ping ponging.
1513 * Also tell the backing pager, if any, that it should remove
1514 * any swap backing since the page is now dirty.
1516 if (fs->prot & VM_PROT_WRITE) {
1517 vm_object_set_writeable_dirty(fs->m->object);
1518 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1519 if (fs->m->dirty == 0)
1520 vm_page_flag_set(fs->m, PG_NOSYNC);
1522 vm_page_flag_clear(fs->m, PG_NOSYNC);
1524 if (fs->fault_flags & VM_FAULT_DIRTY) {
1526 vm_page_dirty(fs->m);
1527 swap_pager_unswapped(fs->m);
1532 lwkt_reltoken(&vm_token);
1535 * Page had better still be busy. We are still locked up and
1536 * fs->object will have another PIP reference if it is not equal
1537 * to fs->first_object.
1539 KASSERT(fs->m->flags & PG_BUSY,
1540 ("vm_fault: page %p not busy!", fs->m));
1543 * Sanity check: page must be completely valid or it is not fit to
1544 * map into user space. vm_pager_get_pages() ensures this.
1546 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1547 vm_page_zero_invalid(fs->m, TRUE);
1548 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1551 return (KERN_SUCCESS);
1555 * Wire down a range of virtual addresses in a map. The entry in question
1556 * should be marked in-transition and the map must be locked. We must
1557 * release the map temporarily while faulting-in the page to avoid a
1558 * deadlock. Note that the entry may be clipped while we are blocked but
1559 * will never be freed.
1564 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1566 boolean_t fictitious;
1574 pmap = vm_map_pmap(map);
1575 start = entry->start;
1577 fictitious = entry->object.vm_object &&
1578 (entry->object.vm_object->type == OBJT_DEVICE);
1580 lwkt_gettoken(&vm_token);
1585 * We simulate a fault to get the page and enter it in the physical
1588 for (va = start; va < end; va += PAGE_SIZE) {
1590 rv = vm_fault(map, va, VM_PROT_READ,
1591 VM_FAULT_USER_WIRE);
1593 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1594 VM_FAULT_CHANGE_WIRING);
1597 while (va > start) {
1599 if ((pa = pmap_extract(pmap, va)) == 0)
1601 pmap_change_wiring(pmap, va, FALSE);
1603 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1606 lwkt_reltoken(&vm_token);
1611 lwkt_reltoken(&vm_token);
1612 return (KERN_SUCCESS);
1616 * Unwire a range of virtual addresses in a map. The map should be
1620 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1622 boolean_t fictitious;
1629 pmap = vm_map_pmap(map);
1630 start = entry->start;
1632 fictitious = entry->object.vm_object &&
1633 (entry->object.vm_object->type == OBJT_DEVICE);
1636 * Since the pages are wired down, we must be able to get their
1637 * mappings from the physical map system.
1639 lwkt_gettoken(&vm_token);
1640 for (va = start; va < end; va += PAGE_SIZE) {
1641 pa = pmap_extract(pmap, va);
1643 pmap_change_wiring(pmap, va, FALSE);
1645 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1648 lwkt_reltoken(&vm_token);
1652 * Reduce the rate at which memory is allocated to a process based
1653 * on the perceived load on the VM system. As the load increases
1654 * the allocation burst rate goes down and the delay increases.
1656 * Rate limiting does not apply when faulting active or inactive
1657 * pages. When faulting 'cache' pages, rate limiting only applies
1658 * if the system currently has a severe page deficit.
1660 * XXX vm_pagesupply should be increased when a page is freed.
1662 * We sleep up to 1/10 of a second.
1665 vm_fault_ratelimit(struct vmspace *vmspace)
1667 if (vm_load_enable == 0)
1669 if (vmspace->vm_pagesupply > 0) {
1670 --vmspace->vm_pagesupply; /* SMP race ok */
1674 if (vm_load_debug) {
1675 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1677 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1678 curproc->p_pid, curproc->p_comm);
1681 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1682 return(vm_load * hz / 10000);
1686 * Copy all of the pages from a wired-down map entry to another.
1688 * The source and destination maps must be locked for write.
1689 * The source map entry must be wired down (or be a sharing map
1690 * entry corresponding to a main map entry that is wired down).
1692 * No other requirements.
1695 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1696 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1698 vm_object_t dst_object;
1699 vm_object_t src_object;
1700 vm_ooffset_t dst_offset;
1701 vm_ooffset_t src_offset;
1711 src_object = src_entry->object.vm_object;
1712 src_offset = src_entry->offset;
1715 * Create the top-level object for the destination entry. (Doesn't
1716 * actually shadow anything - we copy the pages directly.)
1718 vm_map_entry_allocate_object(dst_entry);
1719 dst_object = dst_entry->object.vm_object;
1721 prot = dst_entry->max_protection;
1724 * Loop through all of the pages in the entry's range, copying each
1725 * one from the source object (it should be there) to the destination
1728 for (vaddr = dst_entry->start, dst_offset = 0;
1729 vaddr < dst_entry->end;
1730 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1733 * Allocate a page in the destination object
1736 dst_m = vm_page_alloc(dst_object,
1737 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1738 if (dst_m == NULL) {
1741 } while (dst_m == NULL);
1744 * Find the page in the source object, and copy it in.
1745 * (Because the source is wired down, the page will be in
1748 src_m = vm_page_lookup(src_object,
1749 OFF_TO_IDX(dst_offset + src_offset));
1751 panic("vm_fault_copy_wired: page missing");
1753 vm_page_copy(src_m, dst_m);
1754 vm_page_event(src_m, VMEVENT_COW);
1757 * Enter it in the pmap...
1760 vm_page_flag_clear(dst_m, PG_ZERO);
1761 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1764 * Mark it no longer busy, and put it on the active list.
1766 vm_page_activate(dst_m);
1767 vm_page_wakeup(dst_m);
1774 * This routine checks around the requested page for other pages that
1775 * might be able to be faulted in. This routine brackets the viable
1776 * pages for the pages to be paged in.
1779 * m, rbehind, rahead
1782 * marray (array of vm_page_t), reqpage (index of requested page)
1785 * number of pages in marray
1788 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1789 vm_page_t *marray, int *reqpage)
1793 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1795 int cbehind, cahead;
1801 * we don't fault-ahead for device pager
1803 if (object->type == OBJT_DEVICE) {
1810 * if the requested page is not available, then give up now
1812 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1813 *reqpage = 0; /* not used by caller, fix compiler warn */
1817 if ((cbehind == 0) && (cahead == 0)) {
1823 if (rahead > cahead) {
1827 if (rbehind > cbehind) {
1832 * Do not do any readahead if we have insufficient free memory.
1834 * XXX code was broken disabled before and has instability
1835 * with this conditonal fixed, so shortcut for now.
1837 if (burst_fault == 0 || vm_page_count_severe()) {
1844 * scan backward for the read behind pages -- in memory
1846 * Assume that if the page is not found an interrupt will not
1847 * create it. Theoretically interrupts can only remove (busy)
1848 * pages, not create new associations.
1851 if (rbehind > pindex) {
1855 startpindex = pindex - rbehind;
1859 lwkt_gettoken(&vm_token);
1860 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1861 if (vm_page_lookup(object, tpindex - 1))
1866 while (tpindex < pindex) {
1867 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1869 lwkt_reltoken(&vm_token);
1871 for (j = 0; j < i; j++) {
1872 vm_page_free(marray[j]);
1882 lwkt_reltoken(&vm_token);
1889 * Assign requested page
1896 * Scan forwards for read-ahead pages
1898 tpindex = pindex + 1;
1899 endpindex = tpindex + rahead;
1900 if (endpindex > object->size)
1901 endpindex = object->size;
1904 lwkt_gettoken(&vm_token);
1905 while (tpindex < endpindex) {
1906 if (vm_page_lookup(object, tpindex))
1908 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1915 lwkt_reltoken(&vm_token);
1924 * vm_prefault() provides a quick way of clustering pagefaults into a
1925 * processes address space. It is a "cousin" of pmap_object_init_pt,
1926 * except it runs at page fault time instead of mmap time.
1928 * This code used to be per-platform pmap_prefault(). It is now
1929 * machine-independent and enhanced to also pre-fault zero-fill pages
1930 * (see vm.fast_fault) as well as make them writable, which greatly
1931 * reduces the number of page faults programs incur.
1933 * Application performance when pre-faulting zero-fill pages is heavily
1934 * dependent on the application. Very tiny applications like /bin/echo
1935 * lose a little performance while applications of any appreciable size
1936 * gain performance. Prefaulting multiple pages also reduces SMP
1937 * congestion and can improve SMP performance significantly.
1939 * NOTE! prot may allow writing but this only applies to the top level
1940 * object. If we wind up mapping a page extracted from a backing
1941 * object we have to make sure it is read-only.
1943 * NOTE! The caller has already handled any COW operations on the
1944 * vm_map_entry via the normal fault code. Do NOT call this
1945 * shortcut unless the normal fault code has run on this entry.
1947 * No other requirements.
1951 #define PAGEORDER_SIZE (PFBAK+PFFOR)
1953 static int vm_prefault_pageorder[] = {
1954 -PAGE_SIZE, PAGE_SIZE,
1955 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1956 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1957 -4 * PAGE_SIZE, 4 * PAGE_SIZE
1961 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
1974 * We do not currently prefault mappings that use virtual page
1975 * tables. We do not prefault foreign pmaps.
1977 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
1979 lp = curthread->td_lwp;
1980 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
1983 object = entry->object.vm_object;
1985 starta = addra - PFBAK * PAGE_SIZE;
1986 if (starta < entry->start)
1987 starta = entry->start;
1988 else if (starta > addra)
1992 * critical section protection is required to maintain the
1993 * page/object association, interrupts can free pages and remove
1994 * them from their objects.
1997 lwkt_gettoken(&vm_token);
1998 for (i = 0; i < PAGEORDER_SIZE; i++) {
1999 vm_object_t lobject;
2002 addr = addra + vm_prefault_pageorder[i];
2003 if (addr > addra + (PFFOR * PAGE_SIZE))
2006 if (addr < starta || addr >= entry->end)
2009 if (pmap_prefault_ok(pmap, addr) == 0)
2013 * Follow the VM object chain to obtain the page to be mapped
2016 * If we reach the terminal object without finding a page
2017 * and we determine it would be advantageous, then allocate
2018 * a zero-fill page for the base object. The base object
2019 * is guaranteed to be OBJT_DEFAULT for this case.
2021 * In order to not have to check the pager via *haspage*()
2022 * we stop if any non-default object is encountered. e.g.
2023 * a vnode or swap object would stop the loop.
2025 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2030 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
2031 if (lobject->type != OBJT_DEFAULT)
2033 if (lobject->backing_object == NULL) {
2034 if (vm_fast_fault == 0)
2036 if (vm_prefault_pageorder[i] < 0 ||
2037 (prot & VM_PROT_WRITE) == 0 ||
2038 vm_page_count_min(0)) {
2041 /* note: allocate from base object */
2042 m = vm_page_alloc(object, index,
2043 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
2045 if ((m->flags & PG_ZERO) == 0) {
2046 vm_page_zero_fill(m);
2048 vm_page_flag_clear(m, PG_ZERO);
2049 mycpu->gd_cnt.v_ozfod++;
2051 mycpu->gd_cnt.v_zfod++;
2052 m->valid = VM_PAGE_BITS_ALL;
2055 /* lobject = object .. not needed */
2058 if (lobject->backing_object_offset & PAGE_MASK)
2060 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2061 lobject = lobject->backing_object;
2062 pprot &= ~VM_PROT_WRITE;
2065 * NOTE: lobject now invalid (if we did a zero-fill we didn't
2066 * bother assigning lobject = object).
2068 * Give-up if the page is not available.
2074 * Do not conditionalize on PG_RAM. If pages are present in
2075 * the VM system we assume optimal caching. If caching is
2076 * not optimal the I/O gravy train will be restarted when we
2077 * hit an unavailable page. We do not want to try to restart
2078 * the gravy train now because we really don't know how much
2079 * of the object has been cached. The cost for restarting
2080 * the gravy train should be low (since accesses will likely
2081 * be I/O bound anyway).
2083 * The object must be marked dirty if we are mapping a
2086 if (pprot & VM_PROT_WRITE)
2087 vm_object_set_writeable_dirty(m->object);
2090 * Enter the page into the pmap if appropriate. If we had
2091 * allocated the page we have to place it on a queue. If not
2092 * we just have to make sure it isn't on the cache queue
2093 * (pages on the cache queue are not allowed to be mapped).
2096 pmap_enter(pmap, addr, m, pprot, 0);
2097 vm_page_deactivate(m);
2099 } else if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2101 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2103 if ((m->queue - m->pc) == PQ_CACHE) {
2104 vm_page_deactivate(m);
2107 pmap_enter(pmap, addr, m, pprot, 0);
2111 lwkt_reltoken(&vm_token);