2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
44 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45 * All rights reserved.
47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
69 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
70 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon Exp $
74 * Page fault handling module.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vkernel.h>
86 #include <sys/sysctl.h>
88 #include <cpu/lwbuf.h>
91 #include <vm/vm_param.h>
93 #include <vm/vm_map.h>
94 #include <vm/vm_object.h>
95 #include <vm/vm_page.h>
96 #include <vm/vm_pageout.h>
97 #include <vm/vm_kern.h>
98 #include <vm/vm_pager.h>
99 #include <vm/vnode_pager.h>
100 #include <vm/vm_extern.h>
102 #include <sys/thread2.h>
103 #include <vm/vm_page2.h>
111 vm_object_t first_object;
112 vm_prot_t first_prot;
114 vm_map_entry_t entry;
115 int lookup_still_valid;
124 static int vm_fast_fault = 1;
125 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0, "");
126 static int debug_cluster = 0;
127 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
129 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
130 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
132 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
134 static int vm_fault_ratelimit(struct vmspace *);
135 static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry,
139 release_page(struct faultstate *fs)
141 vm_page_deactivate(fs->m);
142 vm_page_wakeup(fs->m);
147 unlock_map(struct faultstate *fs)
149 if (fs->lookup_still_valid && fs->map) {
150 vm_map_lookup_done(fs->map, fs->entry, 0);
151 fs->lookup_still_valid = FALSE;
156 * Clean up after a successful call to vm_fault_object() so another call
157 * to vm_fault_object() can be made.
160 _cleanup_successful_fault(struct faultstate *fs, int relock)
162 if (fs->object != fs->first_object) {
163 vm_page_free(fs->first_m);
164 vm_object_pip_wakeup(fs->object);
167 fs->object = fs->first_object;
168 if (relock && fs->lookup_still_valid == FALSE) {
170 vm_map_lock_read(fs->map);
171 fs->lookup_still_valid = TRUE;
176 _unlock_things(struct faultstate *fs, int dealloc)
178 vm_object_pip_wakeup(fs->first_object);
179 _cleanup_successful_fault(fs, 0);
181 vm_object_deallocate(fs->first_object);
182 fs->first_object = NULL;
185 if (fs->vp != NULL) {
191 #define unlock_things(fs) _unlock_things(fs, 0)
192 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
193 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
198 * Determine if the pager for the current object *might* contain the page.
200 * We only need to try the pager if this is not a default object (default
201 * objects are zero-fill and have no real pager), and if we are not taking
202 * a wiring fault or if the FS entry is wired.
204 #define TRYPAGER(fs) \
205 (fs->object->type != OBJT_DEFAULT && \
206 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
211 * Handle a page fault occuring at the given address, requiring the given
212 * permissions, in the map specified. If successful, the page is inserted
213 * into the associated physical map.
215 * NOTE: The given address should be truncated to the proper page address.
217 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
218 * a standard error specifying why the fault is fatal is returned.
220 * The map in question must be referenced, and remains so.
221 * The caller may hold no locks.
224 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
227 vm_pindex_t first_pindex;
228 struct faultstate fs;
231 mycpu->gd_cnt.v_vm_faults++;
235 fs.fault_flags = fault_flags;
240 * Find the vm_map_entry representing the backing store and resolve
241 * the top level object and page index. This may have the side
242 * effect of executing a copy-on-write on the map entry and/or
243 * creating a shadow object, but will not COW any actual VM pages.
245 * On success fs.map is left read-locked and various other fields
246 * are initialized but not otherwise referenced or locked.
248 * NOTE! vm_map_lookup will try to upgrade the fault_type to
249 * VM_FAULT_WRITE if the map entry is a virtual page table and also
250 * writable, so we can set the 'A'accessed bit in the virtual page
254 result = vm_map_lookup(&fs.map, vaddr, fault_type,
255 &fs.entry, &fs.first_object,
256 &first_pindex, &fs.first_prot, &fs.wired);
259 * If the lookup failed or the map protections are incompatible,
260 * the fault generally fails. However, if the caller is trying
261 * to do a user wiring we have more work to do.
263 if (result != KERN_SUCCESS) {
264 if (result != KERN_PROTECTION_FAILURE ||
265 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
267 if (result == KERN_INVALID_ADDRESS && growstack &&
268 map != &kernel_map && curproc != NULL) {
269 result = vm_map_growstack(curproc, vaddr);
270 if (result != KERN_SUCCESS)
271 return (KERN_FAILURE);
279 * If we are user-wiring a r/w segment, and it is COW, then
280 * we need to do the COW operation. Note that we don't
281 * currently COW RO sections now, because it is NOT desirable
282 * to COW .text. We simply keep .text from ever being COW'ed
283 * and take the heat that one cannot debug wired .text sections.
285 result = vm_map_lookup(&fs.map, vaddr,
286 VM_PROT_READ|VM_PROT_WRITE|
287 VM_PROT_OVERRIDE_WRITE,
288 &fs.entry, &fs.first_object,
289 &first_pindex, &fs.first_prot,
291 if (result != KERN_SUCCESS)
295 * If we don't COW now, on a user wire, the user will never
296 * be able to write to the mapping. If we don't make this
297 * restriction, the bookkeeping would be nearly impossible.
299 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
300 fs.entry->max_protection &= ~VM_PROT_WRITE;
304 * fs.map is read-locked
306 * Misc checks. Save the map generation number to detect races.
308 fs.map_generation = fs.map->timestamp;
310 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
311 panic("vm_fault: fault on nofault entry, addr: %lx",
316 * A system map entry may return a NULL object. No object means
317 * no pager means an unrecoverable kernel fault.
319 if (fs.first_object == NULL) {
320 panic("vm_fault: unrecoverable fault at %p in entry %p",
321 (void *)vaddr, fs.entry);
325 * Make a reference to this object to prevent its disposal while we
326 * are messing with it. Once we have the reference, the map is free
327 * to be diddled. Since objects reference their shadows (and copies),
328 * they will stay around as well.
330 * Bump the paging-in-progress count to prevent size changes (e.g.
331 * truncation operations) during I/O. This must be done after
332 * obtaining the vnode lock in order to avoid possible deadlocks.
334 vm_object_reference(fs.first_object);
335 fs.vp = vnode_pager_lock(fs.first_object);
336 vm_object_pip_add(fs.first_object, 1);
338 fs.lookup_still_valid = TRUE;
340 fs.object = fs.first_object; /* so unlock_and_deallocate works */
343 * If the entry is wired we cannot change the page protection.
346 fault_type = fs.first_prot;
349 * The page we want is at (first_object, first_pindex), but if the
350 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
351 * page table to figure out the actual pindex.
353 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
356 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
357 result = vm_fault_vpagetable(&fs, &first_pindex,
358 fs.entry->aux.master_pde,
360 if (result == KERN_TRY_AGAIN)
362 if (result != KERN_SUCCESS)
367 * Now we have the actual (object, pindex), fault in the page. If
368 * vm_fault_object() fails it will unlock and deallocate the FS
369 * data. If it succeeds everything remains locked and fs->object
370 * will have an additinal PIP count if it is not equal to
373 * vm_fault_object will set fs->prot for the pmap operation. It is
374 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
375 * page can be safely written. However, it will force a read-only
376 * mapping for a read fault if the memory is managed by a virtual
379 result = vm_fault_object(&fs, first_pindex, fault_type);
381 if (result == KERN_TRY_AGAIN)
383 if (result != KERN_SUCCESS)
387 * On success vm_fault_object() does not unlock or deallocate, and fs.m
388 * will contain a busied page.
390 * Enter the page into the pmap and do pmap-related adjustments.
392 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
395 * Burst in a few more pages if possible. The fs.map should still
398 if (fault_flags & VM_FAULT_BURST) {
399 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
401 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
406 vm_page_flag_clear(fs.m, PG_ZERO);
407 vm_page_flag_set(fs.m, PG_REFERENCED);
410 * If the page is not wired down, then put it where the pageout daemon
413 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
417 vm_page_unwire(fs.m, 1);
419 vm_page_activate(fs.m);
422 if (curthread->td_lwp) {
424 curthread->td_lwp->lwp_ru.ru_majflt++;
426 curthread->td_lwp->lwp_ru.ru_minflt++;
431 * Unlock everything, and return
433 vm_page_wakeup(fs.m);
434 vm_object_deallocate(fs.first_object);
436 return (KERN_SUCCESS);
440 * Fault in the specified virtual address in the current process map,
441 * returning a held VM page or NULL. See vm_fault_page() for more
445 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
447 struct lwp *lp = curthread->td_lwp;
450 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
451 fault_type, VM_FAULT_NORMAL, errorp);
456 * Fault in the specified virtual address in the specified map, doing all
457 * necessary manipulation of the object store and all necessary I/O. Return
458 * a held VM page or NULL, and set *errorp. The related pmap is not
461 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
462 * and marked PG_REFERENCED as well.
464 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
465 * error will be returned.
468 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
469 int fault_flags, int *errorp)
471 vm_pindex_t first_pindex;
472 struct faultstate fs;
474 vm_prot_t orig_fault_type = fault_type;
476 mycpu->gd_cnt.v_vm_faults++;
480 fs.fault_flags = fault_flags;
481 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
485 * Find the vm_map_entry representing the backing store and resolve
486 * the top level object and page index. This may have the side
487 * effect of executing a copy-on-write on the map entry and/or
488 * creating a shadow object, but will not COW any actual VM pages.
490 * On success fs.map is left read-locked and various other fields
491 * are initialized but not otherwise referenced or locked.
493 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
494 * if the map entry is a virtual page table and also writable,
495 * so we can set the 'A'accessed bit in the virtual page table entry.
498 result = vm_map_lookup(&fs.map, vaddr, fault_type,
499 &fs.entry, &fs.first_object,
500 &first_pindex, &fs.first_prot, &fs.wired);
502 if (result != KERN_SUCCESS) {
508 * fs.map is read-locked
510 * Misc checks. Save the map generation number to detect races.
512 fs.map_generation = fs.map->timestamp;
514 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
515 panic("vm_fault: fault on nofault entry, addr: %lx",
520 * A system map entry may return a NULL object. No object means
521 * no pager means an unrecoverable kernel fault.
523 if (fs.first_object == NULL) {
524 panic("vm_fault: unrecoverable fault at %p in entry %p",
525 (void *)vaddr, fs.entry);
529 * Make a reference to this object to prevent its disposal while we
530 * are messing with it. Once we have the reference, the map is free
531 * to be diddled. Since objects reference their shadows (and copies),
532 * they will stay around as well.
534 * Bump the paging-in-progress count to prevent size changes (e.g.
535 * truncation operations) during I/O. This must be done after
536 * obtaining the vnode lock in order to avoid possible deadlocks.
538 vm_object_reference(fs.first_object);
539 fs.vp = vnode_pager_lock(fs.first_object);
540 vm_object_pip_add(fs.first_object, 1);
542 fs.lookup_still_valid = TRUE;
544 fs.object = fs.first_object; /* so unlock_and_deallocate works */
547 * If the entry is wired we cannot change the page protection.
550 fault_type = fs.first_prot;
553 * The page we want is at (first_object, first_pindex), but if the
554 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
555 * page table to figure out the actual pindex.
557 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
560 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
561 result = vm_fault_vpagetable(&fs, &first_pindex,
562 fs.entry->aux.master_pde,
564 if (result == KERN_TRY_AGAIN)
566 if (result != KERN_SUCCESS) {
573 * Now we have the actual (object, pindex), fault in the page. If
574 * vm_fault_object() fails it will unlock and deallocate the FS
575 * data. If it succeeds everything remains locked and fs->object
576 * will have an additinal PIP count if it is not equal to
579 result = vm_fault_object(&fs, first_pindex, fault_type);
581 if (result == KERN_TRY_AGAIN)
583 if (result != KERN_SUCCESS) {
588 if ((orig_fault_type & VM_PROT_WRITE) &&
589 (fs.prot & VM_PROT_WRITE) == 0) {
590 *errorp = KERN_PROTECTION_FAILURE;
591 unlock_and_deallocate(&fs);
596 * On success vm_fault_object() does not unlock or deallocate, and fs.m
597 * will contain a busied page.
602 * Return a held page. We are not doing any pmap manipulation so do
603 * not set PG_MAPPED. However, adjust the page flags according to
604 * the fault type because the caller may not use a managed pmapping
605 * (so we don't want to lose the fact that the page will be dirtied
606 * if a write fault was specified).
609 vm_page_flag_clear(fs.m, PG_ZERO);
610 if (fault_type & VM_PROT_WRITE)
614 * Update the pmap. We really only have to do this if a COW
615 * occured to replace the read-only page with the new page. For
616 * now just do it unconditionally. XXX
618 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
619 vm_page_flag_set(fs.m, PG_REFERENCED);
622 * Unbusy the page by activating it. It remains held and will not
625 vm_page_activate(fs.m);
627 if (curthread->td_lwp) {
629 curthread->td_lwp->lwp_ru.ru_majflt++;
631 curthread->td_lwp->lwp_ru.ru_minflt++;
636 * Unlock everything, and return the held page.
638 vm_page_wakeup(fs.m);
639 vm_object_deallocate(fs.first_object);
646 * Fault in the specified (object,offset), dirty the returned page as
647 * needed. If the requested fault_type cannot be done NULL and an
651 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
652 vm_prot_t fault_type, int fault_flags, int *errorp)
655 vm_pindex_t first_pindex;
656 struct faultstate fs;
657 struct vm_map_entry entry;
659 bzero(&entry, sizeof(entry));
660 entry.object.vm_object = object;
661 entry.maptype = VM_MAPTYPE_NORMAL;
662 entry.protection = entry.max_protection = fault_type;
666 fs.fault_flags = fault_flags;
668 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
672 fs.first_object = object;
673 first_pindex = OFF_TO_IDX(offset);
675 fs.first_prot = fault_type;
677 /*fs.map_generation = 0; unused */
680 * Make a reference to this object to prevent its disposal while we
681 * are messing with it. Once we have the reference, the map is free
682 * to be diddled. Since objects reference their shadows (and copies),
683 * they will stay around as well.
685 * Bump the paging-in-progress count to prevent size changes (e.g.
686 * truncation operations) during I/O. This must be done after
687 * obtaining the vnode lock in order to avoid possible deadlocks.
689 vm_object_reference(fs.first_object);
690 fs.vp = vnode_pager_lock(fs.first_object);
691 vm_object_pip_add(fs.first_object, 1);
693 fs.lookup_still_valid = TRUE;
695 fs.object = fs.first_object; /* so unlock_and_deallocate works */
698 /* XXX future - ability to operate on VM object using vpagetable */
699 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
700 result = vm_fault_vpagetable(&fs, &first_pindex,
701 fs.entry->aux.master_pde,
703 if (result == KERN_TRY_AGAIN)
705 if (result != KERN_SUCCESS) {
713 * Now we have the actual (object, pindex), fault in the page. If
714 * vm_fault_object() fails it will unlock and deallocate the FS
715 * data. If it succeeds everything remains locked and fs->object
716 * will have an additinal PIP count if it is not equal to
719 result = vm_fault_object(&fs, first_pindex, fault_type);
721 if (result == KERN_TRY_AGAIN)
723 if (result != KERN_SUCCESS) {
728 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
729 *errorp = KERN_PROTECTION_FAILURE;
730 unlock_and_deallocate(&fs);
735 * On success vm_fault_object() does not unlock or deallocate, and fs.m
736 * will contain a busied page.
741 * Return a held page. We are not doing any pmap manipulation so do
742 * not set PG_MAPPED. However, adjust the page flags according to
743 * the fault type because the caller may not use a managed pmapping
744 * (so we don't want to lose the fact that the page will be dirtied
745 * if a write fault was specified).
748 vm_page_flag_clear(fs.m, PG_ZERO);
749 if (fault_type & VM_PROT_WRITE)
753 * Indicate that the page was accessed.
755 vm_page_flag_set(fs.m, PG_REFERENCED);
758 * Unbusy the page by activating it. It remains held and will not
761 vm_page_activate(fs.m);
763 if (curthread->td_lwp) {
765 mycpu->gd_cnt.v_vm_faults++;
766 curthread->td_lwp->lwp_ru.ru_majflt++;
768 curthread->td_lwp->lwp_ru.ru_minflt++;
773 * Unlock everything, and return the held page.
775 vm_page_wakeup(fs.m);
776 vm_object_deallocate(fs.first_object);
783 * Translate the virtual page number (first_pindex) that is relative
784 * to the address space into a logical page number that is relative to the
785 * backing object. Use the virtual page table pointed to by (vpte).
787 * This implements an N-level page table. Any level can terminate the
788 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
789 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
793 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
794 vpte_t vpte, int fault_type)
797 int vshift = 48 - PAGE_SHIFT; /* page index bits remaining */
798 int result = KERN_SUCCESS;
803 * We cannot proceed if the vpte is not valid, not readable
804 * for a read fault, or not writable for a write fault.
806 if ((vpte & VPTE_V) == 0) {
807 unlock_and_deallocate(fs);
808 return (KERN_FAILURE);
810 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
811 unlock_and_deallocate(fs);
812 return (KERN_FAILURE);
814 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
815 unlock_and_deallocate(fs);
816 return (KERN_FAILURE);
818 if ((vpte & VPTE_PS) || vshift == 0)
820 KKASSERT(vshift >= VPTE_PAGE_BITS);
823 * Get the page table page. Nominally we only read the page
824 * table, but since we are actively setting VPTE_M and VPTE_A,
825 * tell vm_fault_object() that we are writing it.
827 * There is currently no real need to optimize this.
829 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
830 VM_PROT_READ|VM_PROT_WRITE);
831 if (result != KERN_SUCCESS)
835 * Process the returned fs.m and look up the page table
836 * entry in the page table page.
838 vshift -= VPTE_PAGE_BITS;
839 lwb = lwbuf_alloc(fs->m);
840 ptep = ((vpte_t *)lwbuf_kva(lwb) +
841 ((*pindex >> vshift) & VPTE_PAGE_MASK));
845 * Page table write-back. If the vpte is valid for the
846 * requested operation, do a write-back to the page table.
848 * XXX VPTE_M is not set properly for page directory pages.
849 * It doesn't get set in the page directory if the page table
850 * is modified during a read access.
852 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
854 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
855 atomic_set_long(ptep, VPTE_M|VPTE_A);
856 vm_page_dirty(fs->m);
859 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
861 if ((vpte & VPTE_A) == 0) {
862 atomic_set_long(ptep, VPTE_A);
863 vm_page_dirty(fs->m);
867 vm_page_flag_set(fs->m, PG_REFERENCED);
868 vm_page_activate(fs->m);
869 vm_page_wakeup(fs->m);
870 cleanup_successful_fault(fs);
873 * Combine remaining address bits with the vpte.
875 /* JG how many bits from each? */
876 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
877 (*pindex & ((1L << vshift) - 1));
878 return (KERN_SUCCESS);
883 * Do all operations required to fault-in (fs.first_object, pindex). Run
884 * through the shadow chain as necessary and do required COW or virtual
885 * copy operations. The caller has already fully resolved the vm_map_entry
886 * and, if appropriate, has created a copy-on-write layer. All we need to
887 * do is iterate the object chain.
889 * On failure (fs) is unlocked and deallocated and the caller may return or
890 * retry depending on the failure code. On success (fs) is NOT unlocked or
891 * deallocated, fs.m will contained a resolved, busied page, and fs.object
892 * will have an additional PIP count if it is not equal to fs.first_object.
896 vm_fault_object(struct faultstate *fs,
897 vm_pindex_t first_pindex, vm_prot_t fault_type)
899 vm_object_t next_object;
902 fs->prot = fs->first_prot;
903 fs->object = fs->first_object;
904 pindex = first_pindex;
907 * If a read fault occurs we try to make the page writable if
908 * possible. There are three cases where we cannot make the
909 * page mapping writable:
911 * (1) The mapping is read-only or the VM object is read-only,
912 * fs->prot above will simply not have VM_PROT_WRITE set.
914 * (2) If the mapping is a virtual page table we need to be able
915 * to detect writes so we can set VPTE_M in the virtual page
918 * (3) If the VM page is read-only or copy-on-write, upgrading would
919 * just result in an unnecessary COW fault.
921 * VM_PROT_VPAGED is set if faulting via a virtual page table and
922 * causes adjustments to the 'M'odify bit to also turn off write
923 * access to force a re-fault.
925 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
926 if ((fault_type & VM_PROT_WRITE) == 0)
927 fs->prot &= ~VM_PROT_WRITE;
932 * If the object is dead, we stop here
934 if (fs->object->flags & OBJ_DEAD) {
935 unlock_and_deallocate(fs);
936 return (KERN_PROTECTION_FAILURE);
940 * See if page is resident. spl protection is required
941 * to avoid an interrupt unbusy/free race against our
942 * lookup. We must hold the protection through a page
943 * allocation or busy.
946 fs->m = vm_page_lookup(fs->object, pindex);
950 * Wait/Retry if the page is busy. We have to do this
951 * if the page is busy via either PG_BUSY or
952 * vm_page_t->busy because the vm_pager may be using
953 * vm_page_t->busy for pageouts ( and even pageins if
954 * it is the vnode pager ), and we could end up trying
955 * to pagein and pageout the same page simultaneously.
957 * We can theoretically allow the busy case on a read
958 * fault if the page is marked valid, but since such
959 * pages are typically already pmap'd, putting that
960 * special case in might be more effort then it is
961 * worth. We cannot under any circumstances mess
962 * around with a vm_page_t->busy page except, perhaps,
965 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
967 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
968 mycpu->gd_cnt.v_intrans++;
969 vm_object_deallocate(fs->first_object);
970 fs->first_object = NULL;
972 return (KERN_TRY_AGAIN);
976 * If reactivating a page from PQ_CACHE we may have
979 queue = fs->m->queue;
980 vm_page_unqueue_nowakeup(fs->m);
982 if ((queue - fs->m->pc) == PQ_CACHE &&
983 vm_page_count_severe()) {
984 vm_page_activate(fs->m);
985 unlock_and_deallocate(fs);
988 return (KERN_TRY_AGAIN);
992 * Mark page busy for other processes, and the
993 * pagedaemon. If it still isn't completely valid
994 * (readable), or if a read-ahead-mark is set on
995 * the VM page, jump to readrest, else we found the
996 * page and can return.
998 * We can release the spl once we have marked the
1001 vm_page_busy(fs->m);
1004 if (fs->m->object != &kernel_object) {
1005 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1009 if (fs->m->flags & PG_RAM) {
1012 vm_page_flag_clear(fs->m, PG_RAM);
1016 break; /* break to PAGE HAS BEEN FOUND */
1020 * Page is not resident, If this is the search termination
1021 * or the pager might contain the page, allocate a new page.
1023 * NOTE: We are still in a critical section.
1025 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1027 * If the page is beyond the object size we fail
1029 if (pindex >= fs->object->size) {
1031 unlock_and_deallocate(fs);
1032 return (KERN_PROTECTION_FAILURE);
1038 if (fs->didlimit == 0 && curproc != NULL) {
1041 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1044 unlock_and_deallocate(fs);
1045 tsleep(curproc, 0, "vmrate", limticks);
1047 return (KERN_TRY_AGAIN);
1052 * Allocate a new page for this object/offset pair.
1055 if (!vm_page_count_severe()) {
1056 fs->m = vm_page_alloc(fs->object, pindex,
1057 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1059 if (fs->m == NULL) {
1061 unlock_and_deallocate(fs);
1063 return (KERN_TRY_AGAIN);
1070 * We have found an invalid or partially valid page, a
1071 * page with a read-ahead mark which might be partially or
1072 * fully valid (and maybe dirty too), or we have allocated
1075 * Attempt to fault-in the page if there is a chance that the
1076 * pager has it, and potentially fault in additional pages
1079 * We are NOT in splvm here and if TRYPAGER is true then
1080 * fs.m will be non-NULL and will be PG_BUSY for us.
1085 u_char behavior = vm_map_entry_behavior(fs->entry);
1087 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1093 * If sequential access is detected then attempt
1094 * to deactivate/cache pages behind the scan to
1095 * prevent resource hogging.
1097 * Use of PG_RAM to detect sequential access
1098 * also simulates multi-zone sequential access
1099 * detection for free.
1101 * NOTE: Partially valid dirty pages cannot be
1102 * deactivated without causing NFS picemeal
1105 if ((fs->first_object->type != OBJT_DEVICE) &&
1106 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1107 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1108 (fs->m->flags & PG_RAM)))
1110 vm_pindex_t scan_pindex;
1111 int scan_count = 16;
1113 if (first_pindex < 16) {
1117 scan_pindex = first_pindex - 16;
1118 if (scan_pindex < 16)
1119 scan_count = scan_pindex;
1125 while (scan_count) {
1128 mt = vm_page_lookup(fs->first_object,
1131 (mt->valid != VM_PAGE_BITS_ALL)) {
1135 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1141 vm_page_test_dirty(mt);
1146 vm_page_deactivate(mt);
1161 * Avoid deadlocking against the map when doing I/O.
1162 * fs.object and the page is PG_BUSY'd.
1167 * Acquire the page data. We still hold a ref on
1168 * fs.object and the page has been PG_BUSY's.
1170 * The pager may replace the page (for example, in
1171 * order to enter a fictitious page into the
1172 * object). If it does so it is responsible for
1173 * cleaning up the passed page and properly setting
1174 * the new page PG_BUSY.
1176 * If we got here through a PG_RAM read-ahead
1177 * mark the page may be partially dirty and thus
1178 * not freeable. Don't bother checking to see
1179 * if the pager has the page because we can't free
1180 * it anyway. We have to depend on the get_page
1181 * operation filling in any gaps whether there is
1182 * backing store or not.
1184 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1186 if (rv == VM_PAGER_OK) {
1188 * Relookup in case pager changed page. Pager
1189 * is responsible for disposition of old page
1192 * XXX other code segments do relookups too.
1193 * It's a bad abstraction that needs to be
1196 fs->m = vm_page_lookup(fs->object, pindex);
1197 if (fs->m == NULL) {
1198 unlock_and_deallocate(fs);
1199 return (KERN_TRY_AGAIN);
1203 break; /* break to PAGE HAS BEEN FOUND */
1207 * Remove the bogus page (which does not exist at this
1208 * object/offset); before doing so, we must get back
1209 * our object lock to preserve our invariant.
1211 * Also wake up any other process that may want to bring
1214 * If this is the top-level object, we must leave the
1215 * busy page to prevent another process from rushing
1216 * past us, and inserting the page in that object at
1217 * the same time that we are.
1219 if (rv == VM_PAGER_ERROR) {
1221 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
1223 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
1227 * Data outside the range of the pager or an I/O error
1229 * The page may have been wired during the pagein,
1230 * e.g. by the buffer cache, and cannot simply be
1231 * freed. Call vnode_pager_freepage() to deal with it.
1234 * XXX - the check for kernel_map is a kludge to work
1235 * around having the machine panic on a kernel space
1236 * fault w/ I/O error.
1238 if (((fs->map != &kernel_map) &&
1239 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1240 vnode_pager_freepage(fs->m);
1242 unlock_and_deallocate(fs);
1243 if (rv == VM_PAGER_ERROR)
1244 return (KERN_FAILURE);
1246 return (KERN_PROTECTION_FAILURE);
1249 if (fs->object != fs->first_object) {
1250 vnode_pager_freepage(fs->m);
1253 * XXX - we cannot just fall out at this
1254 * point, m has been freed and is invalid!
1260 * We get here if the object has a default pager (or unwiring)
1261 * or the pager doesn't have the page.
1263 if (fs->object == fs->first_object)
1264 fs->first_m = fs->m;
1267 * Move on to the next object. Lock the next object before
1268 * unlocking the current one.
1270 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1271 next_object = fs->object->backing_object;
1272 if (next_object == NULL) {
1274 * If there's no object left, fill the page in the top
1275 * object with zeros.
1277 if (fs->object != fs->first_object) {
1278 vm_object_pip_wakeup(fs->object);
1280 fs->object = fs->first_object;
1281 pindex = first_pindex;
1282 fs->m = fs->first_m;
1287 * Zero the page if necessary and mark it valid.
1289 if ((fs->m->flags & PG_ZERO) == 0) {
1290 vm_page_zero_fill(fs->m);
1292 mycpu->gd_cnt.v_ozfod++;
1294 mycpu->gd_cnt.v_zfod++;
1295 fs->m->valid = VM_PAGE_BITS_ALL;
1296 break; /* break to PAGE HAS BEEN FOUND */
1298 if (fs->object != fs->first_object) {
1299 vm_object_pip_wakeup(fs->object);
1301 KASSERT(fs->object != next_object, ("object loop %p", next_object));
1302 fs->object = next_object;
1303 vm_object_pip_add(fs->object, 1);
1308 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1311 * If the page is being written, but isn't already owned by the
1312 * top-level object, we have to copy it into a new page owned by the
1315 KASSERT((fs->m->flags & PG_BUSY) != 0,
1316 ("vm_fault: not busy after main loop"));
1318 if (fs->object != fs->first_object) {
1320 * We only really need to copy if we want to write it.
1322 if (fault_type & VM_PROT_WRITE) {
1324 * This allows pages to be virtually copied from a
1325 * backing_object into the first_object, where the
1326 * backing object has no other refs to it, and cannot
1327 * gain any more refs. Instead of a bcopy, we just
1328 * move the page from the backing object to the
1329 * first object. Note that we must mark the page
1330 * dirty in the first object so that it will go out
1331 * to swap when needed.
1335 * Map, if present, has not changed
1338 fs->map_generation == fs->map->timestamp) &&
1340 * Only one shadow object
1342 (fs->object->shadow_count == 1) &&
1344 * No COW refs, except us
1346 (fs->object->ref_count == 1) &&
1348 * No one else can look this object up
1350 (fs->object->handle == NULL) &&
1352 * No other ways to look the object up
1354 ((fs->object->type == OBJT_DEFAULT) ||
1355 (fs->object->type == OBJT_SWAP)) &&
1357 * We don't chase down the shadow chain
1359 (fs->object == fs->first_object->backing_object) &&
1362 * grab the lock if we need to
1364 (fs->lookup_still_valid ||
1366 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1369 fs->lookup_still_valid = 1;
1371 * get rid of the unnecessary page
1373 vm_page_protect(fs->first_m, VM_PROT_NONE);
1374 vm_page_free(fs->first_m);
1378 * grab the page and put it into the
1379 * process'es object. The page is
1380 * automatically made dirty.
1382 vm_page_rename(fs->m, fs->first_object, first_pindex);
1383 fs->first_m = fs->m;
1384 vm_page_busy(fs->first_m);
1386 mycpu->gd_cnt.v_cow_optim++;
1389 * Oh, well, lets copy it.
1391 vm_page_copy(fs->m, fs->first_m);
1392 vm_page_event(fs->m, VMEVENT_COW);
1397 * We no longer need the old page or object.
1403 * fs->object != fs->first_object due to above
1406 vm_object_pip_wakeup(fs->object);
1409 * Only use the new page below...
1412 mycpu->gd_cnt.v_cow_faults++;
1413 fs->m = fs->first_m;
1414 fs->object = fs->first_object;
1415 pindex = first_pindex;
1418 * If it wasn't a write fault avoid having to copy
1419 * the page by mapping it read-only.
1421 fs->prot &= ~VM_PROT_WRITE;
1426 * We may have had to unlock a map to do I/O. If we did then
1427 * lookup_still_valid will be FALSE. If the map generation count
1428 * also changed then all sorts of things could have happened while
1429 * we were doing the I/O and we need to retry.
1432 if (!fs->lookup_still_valid &&
1434 (fs->map->timestamp != fs->map_generation)) {
1436 unlock_and_deallocate(fs);
1437 return (KERN_TRY_AGAIN);
1441 * If the fault is a write, we know that this page is being
1442 * written NOW so dirty it explicitly to save on pmap_is_modified()
1445 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1446 * if the page is already dirty to prevent data written with
1447 * the expectation of being synced from not being synced.
1448 * Likewise if this entry does not request NOSYNC then make
1449 * sure the page isn't marked NOSYNC. Applications sharing
1450 * data should use the same flags to avoid ping ponging.
1452 * Also tell the backing pager, if any, that it should remove
1453 * any swap backing since the page is now dirty.
1455 if (fs->prot & VM_PROT_WRITE) {
1456 vm_object_set_writeable_dirty(fs->m->object);
1457 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1458 if (fs->m->dirty == 0)
1459 vm_page_flag_set(fs->m, PG_NOSYNC);
1461 vm_page_flag_clear(fs->m, PG_NOSYNC);
1463 if (fs->fault_flags & VM_FAULT_DIRTY) {
1465 vm_page_dirty(fs->m);
1466 swap_pager_unswapped(fs->m);
1472 * Page had better still be busy. We are still locked up and
1473 * fs->object will have another PIP reference if it is not equal
1474 * to fs->first_object.
1476 KASSERT(fs->m->flags & PG_BUSY,
1477 ("vm_fault: page %p not busy!", fs->m));
1480 * Sanity check: page must be completely valid or it is not fit to
1481 * map into user space. vm_pager_get_pages() ensures this.
1483 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1484 vm_page_zero_invalid(fs->m, TRUE);
1485 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1488 return (KERN_SUCCESS);
1492 * Wire down a range of virtual addresses in a map. The entry in question
1493 * should be marked in-transition and the map must be locked. We must
1494 * release the map temporarily while faulting-in the page to avoid a
1495 * deadlock. Note that the entry may be clipped while we are blocked but
1496 * will never be freed.
1499 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1501 boolean_t fictitious;
1509 pmap = vm_map_pmap(map);
1510 start = entry->start;
1512 fictitious = entry->object.vm_object &&
1513 (entry->object.vm_object->type == OBJT_DEVICE);
1519 * We simulate a fault to get the page and enter it in the physical
1522 for (va = start; va < end; va += PAGE_SIZE) {
1524 rv = vm_fault(map, va, VM_PROT_READ,
1525 VM_FAULT_USER_WIRE);
1527 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1528 VM_FAULT_CHANGE_WIRING);
1531 while (va > start) {
1533 if ((pa = pmap_extract(pmap, va)) == 0)
1535 pmap_change_wiring(pmap, va, FALSE);
1537 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1544 return (KERN_SUCCESS);
1548 * Unwire a range of virtual addresses in a map. The map should be
1552 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1554 boolean_t fictitious;
1561 pmap = vm_map_pmap(map);
1562 start = entry->start;
1564 fictitious = entry->object.vm_object &&
1565 (entry->object.vm_object->type == OBJT_DEVICE);
1568 * Since the pages are wired down, we must be able to get their
1569 * mappings from the physical map system.
1571 for (va = start; va < end; va += PAGE_SIZE) {
1572 pa = pmap_extract(pmap, va);
1574 pmap_change_wiring(pmap, va, FALSE);
1576 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1582 * Reduce the rate at which memory is allocated to a process based
1583 * on the perceived load on the VM system. As the load increases
1584 * the allocation burst rate goes down and the delay increases.
1586 * Rate limiting does not apply when faulting active or inactive
1587 * pages. When faulting 'cache' pages, rate limiting only applies
1588 * if the system currently has a severe page deficit.
1590 * XXX vm_pagesupply should be increased when a page is freed.
1592 * We sleep up to 1/10 of a second.
1595 vm_fault_ratelimit(struct vmspace *vmspace)
1597 if (vm_load_enable == 0)
1599 if (vmspace->vm_pagesupply > 0) {
1600 --vmspace->vm_pagesupply;
1604 if (vm_load_debug) {
1605 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1607 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1608 curproc->p_pid, curproc->p_comm);
1611 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1612 return(vm_load * hz / 10000);
1617 * vm_fault_copy_entry
1619 * Copy all of the pages from a wired-down map entry to another.
1621 * In/out conditions:
1622 * The source and destination maps must be locked for write.
1623 * The source map entry must be wired down (or be a sharing map
1624 * entry corresponding to a main map entry that is wired down).
1628 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1629 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1631 vm_object_t dst_object;
1632 vm_object_t src_object;
1633 vm_ooffset_t dst_offset;
1634 vm_ooffset_t src_offset;
1644 src_object = src_entry->object.vm_object;
1645 src_offset = src_entry->offset;
1648 * Create the top-level object for the destination entry. (Doesn't
1649 * actually shadow anything - we copy the pages directly.)
1651 vm_map_entry_allocate_object(dst_entry);
1652 dst_object = dst_entry->object.vm_object;
1654 prot = dst_entry->max_protection;
1657 * Loop through all of the pages in the entry's range, copying each
1658 * one from the source object (it should be there) to the destination
1661 for (vaddr = dst_entry->start, dst_offset = 0;
1662 vaddr < dst_entry->end;
1663 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1666 * Allocate a page in the destination object
1669 dst_m = vm_page_alloc(dst_object,
1670 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1671 if (dst_m == NULL) {
1674 } while (dst_m == NULL);
1677 * Find the page in the source object, and copy it in.
1678 * (Because the source is wired down, the page will be in
1681 src_m = vm_page_lookup(src_object,
1682 OFF_TO_IDX(dst_offset + src_offset));
1684 panic("vm_fault_copy_wired: page missing");
1686 vm_page_copy(src_m, dst_m);
1687 vm_page_event(src_m, VMEVENT_COW);
1690 * Enter it in the pmap...
1693 vm_page_flag_clear(dst_m, PG_ZERO);
1694 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1697 * Mark it no longer busy, and put it on the active list.
1699 vm_page_activate(dst_m);
1700 vm_page_wakeup(dst_m);
1707 * This routine checks around the requested page for other pages that
1708 * might be able to be faulted in. This routine brackets the viable
1709 * pages for the pages to be paged in.
1712 * m, rbehind, rahead
1715 * marray (array of vm_page_t), reqpage (index of requested page)
1718 * number of pages in marray
1721 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1722 vm_page_t *marray, int *reqpage)
1726 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1728 int cbehind, cahead;
1734 * we don't fault-ahead for device pager
1736 if (object->type == OBJT_DEVICE) {
1743 * if the requested page is not available, then give up now
1745 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1746 *reqpage = 0; /* not used by caller, fix compiler warn */
1750 if ((cbehind == 0) && (cahead == 0)) {
1756 if (rahead > cahead) {
1760 if (rbehind > cbehind) {
1765 * Do not do any readahead if we have insufficient free memory.
1767 * XXX code was broken disabled before and has instability
1768 * with this conditonal fixed, so shortcut for now.
1770 if (burst_fault == 0 || vm_page_count_severe()) {
1777 * scan backward for the read behind pages -- in memory
1779 * Assume that if the page is not found an interrupt will not
1780 * create it. Theoretically interrupts can only remove (busy)
1781 * pages, not create new associations.
1784 if (rbehind > pindex) {
1788 startpindex = pindex - rbehind;
1792 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1793 if (vm_page_lookup(object, tpindex - 1))
1798 while (tpindex < pindex) {
1799 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1802 for (j = 0; j < i; j++) {
1803 vm_page_free(marray[j]);
1819 * Assign requested page
1826 * Scan forwards for read-ahead pages
1828 tpindex = pindex + 1;
1829 endpindex = tpindex + rahead;
1830 if (endpindex > object->size)
1831 endpindex = object->size;
1834 while (tpindex < endpindex) {
1835 if (vm_page_lookup(object, tpindex))
1837 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1852 * vm_prefault() provides a quick way of clustering pagefaults into a
1853 * processes address space. It is a "cousin" of pmap_object_init_pt,
1854 * except it runs at page fault time instead of mmap time.
1856 * This code used to be per-platform pmap_prefault(). It is now
1857 * machine-independent and enhanced to also pre-fault zero-fill pages
1858 * (see vm.fast_fault) as well as make them writable, which greatly
1859 * reduces the number of page faults programs incur.
1861 * Application performance when pre-faulting zero-fill pages is heavily
1862 * dependent on the application. Very tiny applications like /bin/echo
1863 * lose a little performance while applications of any appreciable size
1864 * gain performance. Prefaulting multiple pages also reduces SMP
1865 * congestion and can improve SMP performance significantly.
1867 * NOTE! prot may allow writing but this only applies to the top level
1868 * object. If we wind up mapping a page extracted from a backing
1869 * object we have to make sure it is read-only.
1871 * NOTE! The caller has already handled any COW operations on the
1872 * vm_map_entry via the normal fault code. Do NOT call this
1873 * shortcut unless the normal fault code has run on this entry.
1877 #define PAGEORDER_SIZE (PFBAK+PFFOR)
1879 static int vm_prefault_pageorder[] = {
1880 -PAGE_SIZE, PAGE_SIZE,
1881 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1882 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1883 -4 * PAGE_SIZE, 4 * PAGE_SIZE
1887 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
1900 * We do not currently prefault mappings that use virtual page
1901 * tables. We do not prefault foreign pmaps.
1903 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
1905 lp = curthread->td_lwp;
1906 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
1909 object = entry->object.vm_object;
1911 starta = addra - PFBAK * PAGE_SIZE;
1912 if (starta < entry->start)
1913 starta = entry->start;
1914 else if (starta > addra)
1918 * critical section protection is required to maintain the
1919 * page/object association, interrupts can free pages and remove
1920 * them from their objects.
1923 for (i = 0; i < PAGEORDER_SIZE; i++) {
1924 vm_object_t lobject;
1927 addr = addra + vm_prefault_pageorder[i];
1928 if (addr > addra + (PFFOR * PAGE_SIZE))
1931 if (addr < starta || addr >= entry->end)
1934 if (pmap_prefault_ok(pmap, addr) == 0)
1938 * Follow the VM object chain to obtain the page to be mapped
1941 * If we reach the terminal object without finding a page
1942 * and we determine it would be advantageous, then allocate
1943 * a zero-fill page for the base object. The base object
1944 * is guaranteed to be OBJT_DEFAULT for this case.
1946 * In order to not have to check the pager via *haspage*()
1947 * we stop if any non-default object is encountered. e.g.
1948 * a vnode or swap object would stop the loop.
1950 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1955 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
1956 if (lobject->type != OBJT_DEFAULT)
1958 if (lobject->backing_object == NULL) {
1959 if (vm_fast_fault == 0)
1961 if (vm_prefault_pageorder[i] < 0 ||
1962 (prot & VM_PROT_WRITE) == 0 ||
1963 vm_page_count_min(0)) {
1966 /* note: allocate from base object */
1967 m = vm_page_alloc(object, index,
1968 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1970 if ((m->flags & PG_ZERO) == 0) {
1971 vm_page_zero_fill(m);
1973 vm_page_flag_clear(m, PG_ZERO);
1974 mycpu->gd_cnt.v_ozfod++;
1976 mycpu->gd_cnt.v_zfod++;
1977 m->valid = VM_PAGE_BITS_ALL;
1980 /* lobject = object .. not needed */
1983 if (lobject->backing_object_offset & PAGE_MASK)
1985 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1986 lobject = lobject->backing_object;
1987 pprot &= ~VM_PROT_WRITE;
1990 * NOTE: lobject now invalid (if we did a zero-fill we didn't
1991 * bother assigning lobject = object).
1993 * Give-up if the page is not available.
1999 * Do not conditionalize on PG_RAM. If pages are present in
2000 * the VM system we assume optimal caching. If caching is
2001 * not optimal the I/O gravy train will be restarted when we
2002 * hit an unavailable page. We do not want to try to restart
2003 * the gravy train now because we really don't know how much
2004 * of the object has been cached. The cost for restarting
2005 * the gravy train should be low (since accesses will likely
2006 * be I/O bound anyway).
2008 * The object must be marked dirty if we are mapping a
2011 if (pprot & VM_PROT_WRITE)
2012 vm_object_set_writeable_dirty(m->object);
2015 * Enter the page into the pmap if appropriate. If we had
2016 * allocated the page we have to place it on a queue. If not
2017 * we just have to make sure it isn't on the cache queue
2018 * (pages on the cache queue are not allowed to be mapped).
2021 pmap_enter(pmap, addr, m, pprot, 0);
2022 vm_page_deactivate(m);
2024 } else if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2026 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2028 if ((m->queue - m->pc) == PQ_CACHE) {
2029 vm_page_deactivate(m);
2032 pmap_enter(pmap, addr, m, pprot, 0);