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 = 32 - 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 >> 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_int(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_int(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 *pindex = (vpte >> PAGE_SHIFT) +
876 (*pindex & ((1 << vshift) - 1));
877 return (KERN_SUCCESS);
882 * Do all operations required to fault-in (fs.first_object, pindex). Run
883 * through the shadow chain as necessary and do required COW or virtual
884 * copy operations. The caller has already fully resolved the vm_map_entry
885 * and, if appropriate, has created a copy-on-write layer. All we need to
886 * do is iterate the object chain.
888 * On failure (fs) is unlocked and deallocated and the caller may return or
889 * retry depending on the failure code. On success (fs) is NOT unlocked or
890 * deallocated, fs.m will contained a resolved, busied page, and fs.object
891 * will have an additional PIP count if it is not equal to fs.first_object.
895 vm_fault_object(struct faultstate *fs,
896 vm_pindex_t first_pindex, vm_prot_t fault_type)
898 vm_object_t next_object;
901 fs->prot = fs->first_prot;
902 fs->object = fs->first_object;
903 pindex = first_pindex;
906 * If a read fault occurs we try to make the page writable if
907 * possible. There are three cases where we cannot make the
908 * page mapping writable:
910 * (1) The mapping is read-only or the VM object is read-only,
911 * fs->prot above will simply not have VM_PROT_WRITE set.
913 * (2) If the mapping is a virtual page table we need to be able
914 * to detect writes so we can set VPTE_M in the virtual page
917 * (3) If the VM page is read-only or copy-on-write, upgrading would
918 * just result in an unnecessary COW fault.
920 * VM_PROT_VPAGED is set if faulting via a virtual page table and
921 * causes adjustments to the 'M'odify bit to also turn off write
922 * access to force a re-fault.
924 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
925 if ((fault_type & VM_PROT_WRITE) == 0)
926 fs->prot &= ~VM_PROT_WRITE;
931 * If the object is dead, we stop here
933 if (fs->object->flags & OBJ_DEAD) {
934 unlock_and_deallocate(fs);
935 return (KERN_PROTECTION_FAILURE);
939 * See if page is resident. spl protection is required
940 * to avoid an interrupt unbusy/free race against our
941 * lookup. We must hold the protection through a page
942 * allocation or busy.
945 fs->m = vm_page_lookup(fs->object, pindex);
949 * Wait/Retry if the page is busy. We have to do this
950 * if the page is busy via either PG_BUSY or
951 * vm_page_t->busy because the vm_pager may be using
952 * vm_page_t->busy for pageouts ( and even pageins if
953 * it is the vnode pager ), and we could end up trying
954 * to pagein and pageout the same page simultaneously.
956 * We can theoretically allow the busy case on a read
957 * fault if the page is marked valid, but since such
958 * pages are typically already pmap'd, putting that
959 * special case in might be more effort then it is
960 * worth. We cannot under any circumstances mess
961 * around with a vm_page_t->busy page except, perhaps,
964 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
966 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
967 mycpu->gd_cnt.v_intrans++;
968 vm_object_deallocate(fs->first_object);
969 fs->first_object = NULL;
971 return (KERN_TRY_AGAIN);
975 * If reactivating a page from PQ_CACHE we may have
978 queue = fs->m->queue;
979 vm_page_unqueue_nowakeup(fs->m);
981 if ((queue - fs->m->pc) == PQ_CACHE &&
982 vm_page_count_severe()) {
983 vm_page_activate(fs->m);
984 unlock_and_deallocate(fs);
987 return (KERN_TRY_AGAIN);
991 * Mark page busy for other processes, and the
992 * pagedaemon. If it still isn't completely valid
993 * (readable), or if a read-ahead-mark is set on
994 * the VM page, jump to readrest, else we found the
995 * page and can return.
997 * We can release the spl once we have marked the
1000 vm_page_busy(fs->m);
1003 if (fs->m->object != &kernel_object) {
1004 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1008 if (fs->m->flags & PG_RAM) {
1011 vm_page_flag_clear(fs->m, PG_RAM);
1015 break; /* break to PAGE HAS BEEN FOUND */
1019 * Page is not resident, If this is the search termination
1020 * or the pager might contain the page, allocate a new page.
1022 * NOTE: We are still in a critical section.
1024 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1026 * If the page is beyond the object size we fail
1028 if (pindex >= fs->object->size) {
1030 unlock_and_deallocate(fs);
1031 return (KERN_PROTECTION_FAILURE);
1037 if (fs->didlimit == 0 && curproc != NULL) {
1040 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1043 unlock_and_deallocate(fs);
1044 tsleep(curproc, 0, "vmrate", limticks);
1046 return (KERN_TRY_AGAIN);
1051 * Allocate a new page for this object/offset pair.
1054 if (!vm_page_count_severe()) {
1055 fs->m = vm_page_alloc(fs->object, pindex,
1056 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1058 if (fs->m == NULL) {
1060 unlock_and_deallocate(fs);
1062 return (KERN_TRY_AGAIN);
1069 * We have found an invalid or partially valid page, a
1070 * page with a read-ahead mark which might be partially or
1071 * fully valid (and maybe dirty too), or we have allocated
1074 * Attempt to fault-in the page if there is a chance that the
1075 * pager has it, and potentially fault in additional pages
1078 * We are NOT in splvm here and if TRYPAGER is true then
1079 * fs.m will be non-NULL and will be PG_BUSY for us.
1084 u_char behavior = vm_map_entry_behavior(fs->entry);
1086 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1092 * If sequential access is detected then attempt
1093 * to deactivate/cache pages behind the scan to
1094 * prevent resource hogging.
1096 * Use of PG_RAM to detect sequential access
1097 * also simulates multi-zone sequential access
1098 * detection for free.
1100 * NOTE: Partially valid dirty pages cannot be
1101 * deactivated without causing NFS picemeal
1104 if ((fs->first_object->type != OBJT_DEVICE) &&
1105 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1106 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1107 (fs->m->flags & PG_RAM)))
1109 vm_pindex_t scan_pindex;
1110 int scan_count = 16;
1112 if (first_pindex < 16) {
1116 scan_pindex = first_pindex - 16;
1117 if (scan_pindex < 16)
1118 scan_count = scan_pindex;
1124 while (scan_count) {
1127 mt = vm_page_lookup(fs->first_object,
1130 (mt->valid != VM_PAGE_BITS_ALL)) {
1134 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1140 vm_page_test_dirty(mt);
1145 vm_page_deactivate(mt);
1160 * Avoid deadlocking against the map when doing I/O.
1161 * fs.object and the page is PG_BUSY'd.
1166 * Acquire the page data. We still hold a ref on
1167 * fs.object and the page has been PG_BUSY's.
1169 * The pager may replace the page (for example, in
1170 * order to enter a fictitious page into the
1171 * object). If it does so it is responsible for
1172 * cleaning up the passed page and properly setting
1173 * the new page PG_BUSY.
1175 * If we got here through a PG_RAM read-ahead
1176 * mark the page may be partially dirty and thus
1177 * not freeable. Don't bother checking to see
1178 * if the pager has the page because we can't free
1179 * it anyway. We have to depend on the get_page
1180 * operation filling in any gaps whether there is
1181 * backing store or not.
1183 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1185 if (rv == VM_PAGER_OK) {
1187 * Relookup in case pager changed page. Pager
1188 * is responsible for disposition of old page
1191 * XXX other code segments do relookups too.
1192 * It's a bad abstraction that needs to be
1195 fs->m = vm_page_lookup(fs->object, pindex);
1196 if (fs->m == NULL) {
1197 unlock_and_deallocate(fs);
1198 return (KERN_TRY_AGAIN);
1202 break; /* break to PAGE HAS BEEN FOUND */
1206 * Remove the bogus page (which does not exist at this
1207 * object/offset); before doing so, we must get back
1208 * our object lock to preserve our invariant.
1210 * Also wake up any other process that may want to bring
1213 * If this is the top-level object, we must leave the
1214 * busy page to prevent another process from rushing
1215 * past us, and inserting the page in that object at
1216 * the same time that we are.
1218 if (rv == VM_PAGER_ERROR) {
1220 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
1222 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
1226 * Data outside the range of the pager or an I/O error
1228 * The page may have been wired during the pagein,
1229 * e.g. by the buffer cache, and cannot simply be
1230 * freed. Call vnode_pager_freepage() to deal with it.
1233 * XXX - the check for kernel_map is a kludge to work
1234 * around having the machine panic on a kernel space
1235 * fault w/ I/O error.
1237 if (((fs->map != &kernel_map) &&
1238 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1239 vnode_pager_freepage(fs->m);
1241 unlock_and_deallocate(fs);
1242 if (rv == VM_PAGER_ERROR)
1243 return (KERN_FAILURE);
1245 return (KERN_PROTECTION_FAILURE);
1248 if (fs->object != fs->first_object) {
1249 vnode_pager_freepage(fs->m);
1252 * XXX - we cannot just fall out at this
1253 * point, m has been freed and is invalid!
1259 * We get here if the object has a default pager (or unwiring)
1260 * or the pager doesn't have the page.
1262 if (fs->object == fs->first_object)
1263 fs->first_m = fs->m;
1266 * Move on to the next object. Lock the next object before
1267 * unlocking the current one.
1269 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1270 next_object = fs->object->backing_object;
1271 if (next_object == NULL) {
1273 * If there's no object left, fill the page in the top
1274 * object with zeros.
1276 if (fs->object != fs->first_object) {
1277 vm_object_pip_wakeup(fs->object);
1279 fs->object = fs->first_object;
1280 pindex = first_pindex;
1281 fs->m = fs->first_m;
1286 * Zero the page if necessary and mark it valid.
1288 if ((fs->m->flags & PG_ZERO) == 0) {
1289 vm_page_zero_fill(fs->m);
1291 mycpu->gd_cnt.v_ozfod++;
1293 mycpu->gd_cnt.v_zfod++;
1294 fs->m->valid = VM_PAGE_BITS_ALL;
1295 break; /* break to PAGE HAS BEEN FOUND */
1297 if (fs->object != fs->first_object) {
1298 vm_object_pip_wakeup(fs->object);
1300 KASSERT(fs->object != next_object, ("object loop %p", next_object));
1301 fs->object = next_object;
1302 vm_object_pip_add(fs->object, 1);
1307 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1310 * If the page is being written, but isn't already owned by the
1311 * top-level object, we have to copy it into a new page owned by the
1314 KASSERT((fs->m->flags & PG_BUSY) != 0,
1315 ("vm_fault: not busy after main loop"));
1317 if (fs->object != fs->first_object) {
1319 * We only really need to copy if we want to write it.
1321 if (fault_type & VM_PROT_WRITE) {
1323 * This allows pages to be virtually copied from a
1324 * backing_object into the first_object, where the
1325 * backing object has no other refs to it, and cannot
1326 * gain any more refs. Instead of a bcopy, we just
1327 * move the page from the backing object to the
1328 * first object. Note that we must mark the page
1329 * dirty in the first object so that it will go out
1330 * to swap when needed.
1334 * Map, if present, has not changed
1337 fs->map_generation == fs->map->timestamp) &&
1339 * Only one shadow object
1341 (fs->object->shadow_count == 1) &&
1343 * No COW refs, except us
1345 (fs->object->ref_count == 1) &&
1347 * No one else can look this object up
1349 (fs->object->handle == NULL) &&
1351 * No other ways to look the object up
1353 ((fs->object->type == OBJT_DEFAULT) ||
1354 (fs->object->type == OBJT_SWAP)) &&
1356 * We don't chase down the shadow chain
1358 (fs->object == fs->first_object->backing_object) &&
1361 * grab the lock if we need to
1363 (fs->lookup_still_valid ||
1365 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1368 fs->lookup_still_valid = 1;
1370 * get rid of the unnecessary page
1372 vm_page_protect(fs->first_m, VM_PROT_NONE);
1373 vm_page_free(fs->first_m);
1377 * grab the page and put it into the
1378 * process'es object. The page is
1379 * automatically made dirty.
1381 vm_page_rename(fs->m, fs->first_object, first_pindex);
1382 fs->first_m = fs->m;
1383 vm_page_busy(fs->first_m);
1385 mycpu->gd_cnt.v_cow_optim++;
1388 * Oh, well, lets copy it.
1390 vm_page_copy(fs->m, fs->first_m);
1391 vm_page_event(fs->m, VMEVENT_COW);
1396 * We no longer need the old page or object.
1402 * fs->object != fs->first_object due to above
1405 vm_object_pip_wakeup(fs->object);
1408 * Only use the new page below...
1411 mycpu->gd_cnt.v_cow_faults++;
1412 fs->m = fs->first_m;
1413 fs->object = fs->first_object;
1414 pindex = first_pindex;
1417 * If it wasn't a write fault avoid having to copy
1418 * the page by mapping it read-only.
1420 fs->prot &= ~VM_PROT_WRITE;
1425 * We may have had to unlock a map to do I/O. If we did then
1426 * lookup_still_valid will be FALSE. If the map generation count
1427 * also changed then all sorts of things could have happened while
1428 * we were doing the I/O and we need to retry.
1431 if (!fs->lookup_still_valid &&
1433 (fs->map->timestamp != fs->map_generation)) {
1435 unlock_and_deallocate(fs);
1436 return (KERN_TRY_AGAIN);
1440 * If the fault is a write, we know that this page is being
1441 * written NOW so dirty it explicitly to save on pmap_is_modified()
1444 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1445 * if the page is already dirty to prevent data written with
1446 * the expectation of being synced from not being synced.
1447 * Likewise if this entry does not request NOSYNC then make
1448 * sure the page isn't marked NOSYNC. Applications sharing
1449 * data should use the same flags to avoid ping ponging.
1451 * Also tell the backing pager, if any, that it should remove
1452 * any swap backing since the page is now dirty.
1454 if (fs->prot & VM_PROT_WRITE) {
1455 vm_object_set_writeable_dirty(fs->m->object);
1456 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1457 if (fs->m->dirty == 0)
1458 vm_page_flag_set(fs->m, PG_NOSYNC);
1460 vm_page_flag_clear(fs->m, PG_NOSYNC);
1462 if (fs->fault_flags & VM_FAULT_DIRTY) {
1464 vm_page_dirty(fs->m);
1465 swap_pager_unswapped(fs->m);
1471 * Page had better still be busy. We are still locked up and
1472 * fs->object will have another PIP reference if it is not equal
1473 * to fs->first_object.
1475 KASSERT(fs->m->flags & PG_BUSY,
1476 ("vm_fault: page %p not busy!", fs->m));
1479 * Sanity check: page must be completely valid or it is not fit to
1480 * map into user space. vm_pager_get_pages() ensures this.
1482 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1483 vm_page_zero_invalid(fs->m, TRUE);
1484 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1487 return (KERN_SUCCESS);
1491 * Wire down a range of virtual addresses in a map. The entry in question
1492 * should be marked in-transition and the map must be locked. We must
1493 * release the map temporarily while faulting-in the page to avoid a
1494 * deadlock. Note that the entry may be clipped while we are blocked but
1495 * will never be freed.
1498 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1500 boolean_t fictitious;
1508 pmap = vm_map_pmap(map);
1509 start = entry->start;
1511 fictitious = entry->object.vm_object &&
1512 (entry->object.vm_object->type == OBJT_DEVICE);
1518 * We simulate a fault to get the page and enter it in the physical
1521 for (va = start; va < end; va += PAGE_SIZE) {
1523 rv = vm_fault(map, va, VM_PROT_READ,
1524 VM_FAULT_USER_WIRE);
1526 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1527 VM_FAULT_CHANGE_WIRING);
1530 while (va > start) {
1532 if ((pa = pmap_extract(pmap, va)) == 0)
1534 pmap_change_wiring(pmap, va, FALSE);
1536 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1543 return (KERN_SUCCESS);
1547 * Unwire a range of virtual addresses in a map. The map should be
1551 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1553 boolean_t fictitious;
1560 pmap = vm_map_pmap(map);
1561 start = entry->start;
1563 fictitious = entry->object.vm_object &&
1564 (entry->object.vm_object->type == OBJT_DEVICE);
1567 * Since the pages are wired down, we must be able to get their
1568 * mappings from the physical map system.
1570 for (va = start; va < end; va += PAGE_SIZE) {
1571 pa = pmap_extract(pmap, va);
1573 pmap_change_wiring(pmap, va, FALSE);
1575 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1581 * Reduce the rate at which memory is allocated to a process based
1582 * on the perceived load on the VM system. As the load increases
1583 * the allocation burst rate goes down and the delay increases.
1585 * Rate limiting does not apply when faulting active or inactive
1586 * pages. When faulting 'cache' pages, rate limiting only applies
1587 * if the system currently has a severe page deficit.
1589 * XXX vm_pagesupply should be increased when a page is freed.
1591 * We sleep up to 1/10 of a second.
1594 vm_fault_ratelimit(struct vmspace *vmspace)
1596 if (vm_load_enable == 0)
1598 if (vmspace->vm_pagesupply > 0) {
1599 --vmspace->vm_pagesupply;
1603 if (vm_load_debug) {
1604 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1606 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1607 curproc->p_pid, curproc->p_comm);
1610 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1611 return(vm_load * hz / 10000);
1616 * vm_fault_copy_entry
1618 * Copy all of the pages from a wired-down map entry to another.
1620 * In/out conditions:
1621 * The source and destination maps must be locked for write.
1622 * The source map entry must be wired down (or be a sharing map
1623 * entry corresponding to a main map entry that is wired down).
1627 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1628 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1630 vm_object_t dst_object;
1631 vm_object_t src_object;
1632 vm_ooffset_t dst_offset;
1633 vm_ooffset_t src_offset;
1643 src_object = src_entry->object.vm_object;
1644 src_offset = src_entry->offset;
1647 * Create the top-level object for the destination entry. (Doesn't
1648 * actually shadow anything - we copy the pages directly.)
1650 vm_map_entry_allocate_object(dst_entry);
1651 dst_object = dst_entry->object.vm_object;
1653 prot = dst_entry->max_protection;
1656 * Loop through all of the pages in the entry's range, copying each
1657 * one from the source object (it should be there) to the destination
1660 for (vaddr = dst_entry->start, dst_offset = 0;
1661 vaddr < dst_entry->end;
1662 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1665 * Allocate a page in the destination object
1668 dst_m = vm_page_alloc(dst_object,
1669 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1670 if (dst_m == NULL) {
1673 } while (dst_m == NULL);
1676 * Find the page in the source object, and copy it in.
1677 * (Because the source is wired down, the page will be in
1680 src_m = vm_page_lookup(src_object,
1681 OFF_TO_IDX(dst_offset + src_offset));
1683 panic("vm_fault_copy_wired: page missing");
1685 vm_page_copy(src_m, dst_m);
1686 vm_page_event(src_m, VMEVENT_COW);
1689 * Enter it in the pmap...
1692 vm_page_flag_clear(dst_m, PG_ZERO);
1693 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1696 * Mark it no longer busy, and put it on the active list.
1698 vm_page_activate(dst_m);
1699 vm_page_wakeup(dst_m);
1706 * This routine checks around the requested page for other pages that
1707 * might be able to be faulted in. This routine brackets the viable
1708 * pages for the pages to be paged in.
1711 * m, rbehind, rahead
1714 * marray (array of vm_page_t), reqpage (index of requested page)
1717 * number of pages in marray
1720 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1721 vm_page_t *marray, int *reqpage)
1725 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1727 int cbehind, cahead;
1733 * we don't fault-ahead for device pager
1735 if (object->type == OBJT_DEVICE) {
1742 * if the requested page is not available, then give up now
1744 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1745 *reqpage = 0; /* not used by caller, fix compiler warn */
1749 if ((cbehind == 0) && (cahead == 0)) {
1755 if (rahead > cahead) {
1759 if (rbehind > cbehind) {
1764 * Do not do any readahead if we have insufficient free memory.
1766 * XXX code was broken disabled before and has instability
1767 * with this conditonal fixed, so shortcut for now.
1769 if (burst_fault == 0 || vm_page_count_severe()) {
1776 * scan backward for the read behind pages -- in memory
1778 * Assume that if the page is not found an interrupt will not
1779 * create it. Theoretically interrupts can only remove (busy)
1780 * pages, not create new associations.
1783 if (rbehind > pindex) {
1787 startpindex = pindex - rbehind;
1791 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1792 if (vm_page_lookup(object, tpindex - 1))
1797 while (tpindex < pindex) {
1798 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1801 for (j = 0; j < i; j++) {
1802 vm_page_free(marray[j]);
1818 * Assign requested page
1825 * Scan forwards for read-ahead pages
1827 tpindex = pindex + 1;
1828 endpindex = tpindex + rahead;
1829 if (endpindex > object->size)
1830 endpindex = object->size;
1833 while (tpindex < endpindex) {
1834 if (vm_page_lookup(object, tpindex))
1836 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1851 * vm_prefault() provides a quick way of clustering pagefaults into a
1852 * processes address space. It is a "cousin" of pmap_object_init_pt,
1853 * except it runs at page fault time instead of mmap time.
1855 * This code used to be per-platform pmap_prefault(). It is now
1856 * machine-independent and enhanced to also pre-fault zero-fill pages
1857 * (see vm.fast_fault) as well as make them writable, which greatly
1858 * reduces the number of page faults programs incur.
1860 * Application performance when pre-faulting zero-fill pages is heavily
1861 * dependent on the application. Very tiny applications like /bin/echo
1862 * lose a little performance while applications of any appreciable size
1863 * gain performance. Prefaulting multiple pages also reduces SMP
1864 * congestion and can improve SMP performance significantly.
1866 * NOTE! prot may allow writing but this only applies to the top level
1867 * object. If we wind up mapping a page extracted from a backing
1868 * object we have to make sure it is read-only.
1870 * NOTE! The caller has already handled any COW operations on the
1871 * vm_map_entry via the normal fault code. Do NOT call this
1872 * shortcut unless the normal fault code has run on this entry.
1876 #define PAGEORDER_SIZE (PFBAK+PFFOR)
1878 static int vm_prefault_pageorder[] = {
1879 -PAGE_SIZE, PAGE_SIZE,
1880 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1881 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1882 -4 * PAGE_SIZE, 4 * PAGE_SIZE
1886 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
1899 * We do not currently prefault mappings that use virtual page
1900 * tables. We do not prefault foreign pmaps.
1902 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
1904 lp = curthread->td_lwp;
1905 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
1908 object = entry->object.vm_object;
1910 starta = addra - PFBAK * PAGE_SIZE;
1911 if (starta < entry->start)
1912 starta = entry->start;
1913 else if (starta > addra)
1917 * critical section protection is required to maintain the
1918 * page/object association, interrupts can free pages and remove
1919 * them from their objects.
1922 for (i = 0; i < PAGEORDER_SIZE; i++) {
1923 vm_object_t lobject;
1926 addr = addra + vm_prefault_pageorder[i];
1927 if (addr > addra + (PFFOR * PAGE_SIZE))
1930 if (addr < starta || addr >= entry->end)
1933 if (pmap_prefault_ok(pmap, addr) == 0)
1937 * Follow the VM object chain to obtain the page to be mapped
1940 * If we reach the terminal object without finding a page
1941 * and we determine it would be advantageous, then allocate
1942 * a zero-fill page for the base object. The base object
1943 * is guaranteed to be OBJT_DEFAULT for this case.
1945 * In order to not have to check the pager via *haspage*()
1946 * we stop if any non-default object is encountered. e.g.
1947 * a vnode or swap object would stop the loop.
1949 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1954 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
1955 if (lobject->type != OBJT_DEFAULT)
1957 if (lobject->backing_object == NULL) {
1958 if (vm_fast_fault == 0)
1960 if (vm_prefault_pageorder[i] < 0 ||
1961 (prot & VM_PROT_WRITE) == 0 ||
1962 vm_page_count_min(0)) {
1965 /* note: allocate from base object */
1966 m = vm_page_alloc(object, index,
1967 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1969 if ((m->flags & PG_ZERO) == 0) {
1970 vm_page_zero_fill(m);
1972 vm_page_flag_clear(m, PG_ZERO);
1973 mycpu->gd_cnt.v_ozfod++;
1975 mycpu->gd_cnt.v_zfod++;
1976 m->valid = VM_PAGE_BITS_ALL;
1979 /* lobject = object .. not needed */
1982 if (lobject->backing_object_offset & PAGE_MASK)
1984 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1985 lobject = lobject->backing_object;
1986 pprot &= ~VM_PROT_WRITE;
1989 * NOTE: lobject now invalid (if we did a zero-fill we didn't
1990 * bother assigning lobject = object).
1992 * Give-up if the page is not available.
1998 * Do not conditionalize on PG_RAM. If pages are present in
1999 * the VM system we assume optimal caching. If caching is
2000 * not optimal the I/O gravy train will be restarted when we
2001 * hit an unavailable page. We do not want to try to restart
2002 * the gravy train now because we really don't know how much
2003 * of the object has been cached. The cost for restarting
2004 * the gravy train should be low (since accesses will likely
2005 * be I/O bound anyway).
2007 * The object must be marked dirty if we are mapping a
2010 if (pprot & VM_PROT_WRITE)
2011 vm_object_set_writeable_dirty(m->object);
2014 * Enter the page into the pmap if appropriate. If we had
2015 * allocated the page we have to place it on a queue. If not
2016 * we just have to make sure it isn't on the cache queue
2017 * (pages on the cache queue are not allowed to be mapped).
2020 pmap_enter(pmap, addr, m, pprot, 0);
2021 vm_page_deactivate(m);
2023 } else if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2025 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2027 if ((m->queue - m->pc) == PQ_CACHE) {
2028 vm_page_deactivate(m);
2031 pmap_enter(pmap, addr, m, pprot, 0);