4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
8 * Copyright (c) 1994 David Greenman
12 * This code is derived from software contributed to Berkeley by
13 * The Mach Operating System project at Carnegie-Mellon University.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
51 * Permission to use, copy, modify and distribute this software and
52 * its documentation is hereby granted, provided that both the copyright
53 * notice and this permission notice appear in all copies of the
54 * software, derivative works or modified versions, and any portions
55 * thereof, and that both notices appear in supporting documentation.
57 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 * Carnegie Mellon requests users of this software to return to
63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
64 * School of Computer Science
65 * Carnegie Mellon University
66 * Pittsburgh PA 15213-3890
68 * any improvements or extensions that they make and grant Carnegie the
69 * rights to redistribute these changes.
71 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
72 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon Exp $
76 * Page fault handling module.
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
83 #include <sys/vnode.h>
84 #include <sys/resourcevar.h>
85 #include <sys/vmmeter.h>
86 #include <sys/vkernel.h>
88 #include <sys/sysctl.h>
90 #include <cpu/lwbuf.h>
93 #include <vm/vm_param.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vnode_pager.h>
102 #include <vm/vm_extern.h>
104 #include <sys/thread2.h>
105 #include <vm/vm_page2.h>
113 vm_object_t first_object;
114 vm_prot_t first_prot;
116 vm_map_entry_t entry;
117 int lookup_still_valid;
126 static int vm_fast_fault = 1;
127 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
128 "Burst fault zero-fill regions");
129 static int debug_cluster = 0;
130 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
132 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
133 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
135 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
137 static int vm_fault_ratelimit(struct vmspace *);
138 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
139 static void vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry,
143 * The caller must hold vm_token.
146 release_page(struct faultstate *fs)
148 vm_page_deactivate(fs->m);
149 vm_page_wakeup(fs->m);
154 * The caller must hold vm_token.
157 unlock_map(struct faultstate *fs)
159 if (fs->lookup_still_valid && fs->map) {
160 vm_map_lookup_done(fs->map, fs->entry, 0);
161 fs->lookup_still_valid = FALSE;
166 * Clean up after a successful call to vm_fault_object() so another call
167 * to vm_fault_object() can be made.
169 * The caller must hold vm_token.
172 _cleanup_successful_fault(struct faultstate *fs, int relock)
174 if (fs->object != fs->first_object) {
175 vm_page_free(fs->first_m);
176 vm_object_pip_wakeup(fs->object);
179 fs->object = fs->first_object;
180 if (relock && fs->lookup_still_valid == FALSE) {
182 vm_map_lock_read(fs->map);
183 fs->lookup_still_valid = TRUE;
188 * The caller must hold vm_token.
191 _unlock_things(struct faultstate *fs, int dealloc)
193 vm_object_pip_wakeup(fs->first_object);
194 _cleanup_successful_fault(fs, 0);
196 vm_object_deallocate(fs->first_object);
197 fs->first_object = NULL;
200 if (fs->vp != NULL) {
206 #define unlock_things(fs) _unlock_things(fs, 0)
207 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
208 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
213 * Determine if the pager for the current object *might* contain the page.
215 * We only need to try the pager if this is not a default object (default
216 * objects are zero-fill and have no real pager), and if we are not taking
217 * a wiring fault or if the FS entry is wired.
219 #define TRYPAGER(fs) \
220 (fs->object->type != OBJT_DEFAULT && \
221 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
226 * Handle a page fault occuring at the given address, requiring the given
227 * permissions, in the map specified. If successful, the page is inserted
228 * into the associated physical map.
230 * NOTE: The given address should be truncated to the proper page address.
232 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
233 * a standard error specifying why the fault is fatal is returned.
235 * The map in question must be referenced, and remains so.
236 * The caller may hold no locks.
237 * No other requirements.
240 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
243 vm_pindex_t first_pindex;
244 struct faultstate fs;
247 mycpu->gd_cnt.v_vm_faults++;
251 fs.fault_flags = fault_flags;
256 * Find the vm_map_entry representing the backing store and resolve
257 * the top level object and page index. This may have the side
258 * effect of executing a copy-on-write on the map entry and/or
259 * creating a shadow object, but will not COW any actual VM pages.
261 * On success fs.map is left read-locked and various other fields
262 * are initialized but not otherwise referenced or locked.
264 * NOTE! vm_map_lookup will try to upgrade the fault_type to
265 * VM_FAULT_WRITE if the map entry is a virtual page table and also
266 * writable, so we can set the 'A'accessed bit in the virtual page
270 result = vm_map_lookup(&fs.map, vaddr, fault_type,
271 &fs.entry, &fs.first_object,
272 &first_pindex, &fs.first_prot, &fs.wired);
275 * If the lookup failed or the map protections are incompatible,
276 * the fault generally fails. However, if the caller is trying
277 * to do a user wiring we have more work to do.
279 if (result != KERN_SUCCESS) {
280 if (result != KERN_PROTECTION_FAILURE ||
281 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
283 if (result == KERN_INVALID_ADDRESS && growstack &&
284 map != &kernel_map && curproc != NULL) {
285 result = vm_map_growstack(curproc, vaddr);
286 if (result != KERN_SUCCESS)
287 return (KERN_FAILURE);
295 * If we are user-wiring a r/w segment, and it is COW, then
296 * we need to do the COW operation. Note that we don't
297 * currently COW RO sections now, because it is NOT desirable
298 * to COW .text. We simply keep .text from ever being COW'ed
299 * and take the heat that one cannot debug wired .text sections.
301 result = vm_map_lookup(&fs.map, vaddr,
302 VM_PROT_READ|VM_PROT_WRITE|
303 VM_PROT_OVERRIDE_WRITE,
304 &fs.entry, &fs.first_object,
305 &first_pindex, &fs.first_prot,
307 if (result != KERN_SUCCESS)
311 * If we don't COW now, on a user wire, the user will never
312 * be able to write to the mapping. If we don't make this
313 * restriction, the bookkeeping would be nearly impossible.
315 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
316 fs.entry->max_protection &= ~VM_PROT_WRITE;
320 * fs.map is read-locked
322 * Misc checks. Save the map generation number to detect races.
324 fs.map_generation = fs.map->timestamp;
326 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
327 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
328 panic("vm_fault: fault on nofault entry, addr: %p",
331 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
332 vaddr >= fs.entry->start &&
333 vaddr < fs.entry->start + PAGE_SIZE) {
334 panic("vm_fault: fault on stack guard, addr: %p",
340 * A system map entry may return a NULL object. No object means
341 * no pager means an unrecoverable kernel fault.
343 if (fs.first_object == NULL) {
344 panic("vm_fault: unrecoverable fault at %p in entry %p",
345 (void *)vaddr, fs.entry);
349 * Make a reference to this object to prevent its disposal while we
350 * are messing with it. Once we have the reference, the map is free
351 * to be diddled. Since objects reference their shadows (and copies),
352 * they will stay around as well.
354 * Bump the paging-in-progress count to prevent size changes (e.g.
355 * truncation operations) during I/O. This must be done after
356 * obtaining the vnode lock in order to avoid possible deadlocks.
358 * The vm_token is needed to manipulate the vm_object
360 lwkt_gettoken(&vm_token);
361 vm_object_reference(fs.first_object);
362 fs.vp = vnode_pager_lock(fs.first_object);
363 vm_object_pip_add(fs.first_object, 1);
364 lwkt_reltoken(&vm_token);
366 fs.lookup_still_valid = TRUE;
368 fs.object = fs.first_object; /* so unlock_and_deallocate works */
371 * If the entry is wired we cannot change the page protection.
374 fault_type = fs.first_prot;
377 * The page we want is at (first_object, first_pindex), but if the
378 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
379 * page table to figure out the actual pindex.
381 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
384 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
385 result = vm_fault_vpagetable(&fs, &first_pindex,
386 fs.entry->aux.master_pde,
388 if (result == KERN_TRY_AGAIN)
390 if (result != KERN_SUCCESS)
395 * Now we have the actual (object, pindex), fault in the page. If
396 * vm_fault_object() fails it will unlock and deallocate the FS
397 * data. If it succeeds everything remains locked and fs->object
398 * will have an additional PIP count if it is not equal to
401 * vm_fault_object will set fs->prot for the pmap operation. It is
402 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
403 * page can be safely written. However, it will force a read-only
404 * mapping for a read fault if the memory is managed by a virtual
408 result = vm_fault_object(&fs, first_pindex, fault_type);
410 if (result == KERN_TRY_AGAIN) {
411 /*lwkt_reltoken(&vm_token);*/
414 if (result != KERN_SUCCESS) {
415 /*lwkt_reltoken(&vm_token);*/
420 * On success vm_fault_object() does not unlock or deallocate, and fs.m
421 * will contain a busied page.
423 * Enter the page into the pmap and do pmap-related adjustments.
425 vm_page_flag_set(fs.m, PG_REFERENCED);
426 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
429 * Burst in a few more pages if possible. The fs.map should still
432 if (fault_flags & VM_FAULT_BURST) {
433 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
435 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
438 lwkt_gettoken(&vm_token);
441 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
442 KKASSERT(fs.m->flags & PG_BUSY);
445 * If the page is not wired down, then put it where the pageout daemon
448 * We do not really need to get vm_token here but since all the
449 * vm_*() calls have to doing it here improves efficiency.
451 /*lwkt_gettoken(&vm_token);*/
453 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
454 lwkt_reltoken(&vm_token); /* before wire activate does not */
458 vm_page_unwire(fs.m, 1);
460 vm_page_activate(fs.m);
461 lwkt_reltoken(&vm_token); /* before wire activate does not */
463 /*lwkt_reltoken(&vm_token); after wire/activate works */
465 if (curthread->td_lwp) {
467 curthread->td_lwp->lwp_ru.ru_majflt++;
469 curthread->td_lwp->lwp_ru.ru_minflt++;
474 * Unlock everything, and return
476 vm_page_wakeup(fs.m);
477 vm_object_deallocate(fs.first_object);
479 /*fs.first_object = NULL; */
480 /*lwkt_reltoken(&vm_token);*/
482 return (KERN_SUCCESS);
486 * Fault in the specified virtual address in the current process map,
487 * returning a held VM page or NULL. See vm_fault_page() for more
493 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
495 struct lwp *lp = curthread->td_lwp;
498 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
499 fault_type, VM_FAULT_NORMAL, errorp);
504 * Fault in the specified virtual address in the specified map, doing all
505 * necessary manipulation of the object store and all necessary I/O. Return
506 * a held VM page or NULL, and set *errorp. The related pmap is not
509 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
510 * and marked PG_REFERENCED as well.
512 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
513 * error will be returned.
518 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
519 int fault_flags, int *errorp)
521 vm_pindex_t first_pindex;
522 struct faultstate fs;
524 vm_prot_t orig_fault_type = fault_type;
526 mycpu->gd_cnt.v_vm_faults++;
530 fs.fault_flags = fault_flags;
531 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
535 * Find the vm_map_entry representing the backing store and resolve
536 * the top level object and page index. This may have the side
537 * effect of executing a copy-on-write on the map entry and/or
538 * creating a shadow object, but will not COW any actual VM pages.
540 * On success fs.map is left read-locked and various other fields
541 * are initialized but not otherwise referenced or locked.
543 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
544 * if the map entry is a virtual page table and also writable,
545 * so we can set the 'A'accessed bit in the virtual page table entry.
548 result = vm_map_lookup(&fs.map, vaddr, fault_type,
549 &fs.entry, &fs.first_object,
550 &first_pindex, &fs.first_prot, &fs.wired);
552 if (result != KERN_SUCCESS) {
558 * fs.map is read-locked
560 * Misc checks. Save the map generation number to detect races.
562 fs.map_generation = fs.map->timestamp;
564 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
565 panic("vm_fault: fault on nofault entry, addr: %lx",
570 * A system map entry may return a NULL object. No object means
571 * no pager means an unrecoverable kernel fault.
573 if (fs.first_object == NULL) {
574 panic("vm_fault: unrecoverable fault at %p in entry %p",
575 (void *)vaddr, fs.entry);
579 * Make a reference to this object to prevent its disposal while we
580 * are messing with it. Once we have the reference, the map is free
581 * to be diddled. Since objects reference their shadows (and copies),
582 * they will stay around as well.
584 * Bump the paging-in-progress count to prevent size changes (e.g.
585 * truncation operations) during I/O. This must be done after
586 * obtaining the vnode lock in order to avoid possible deadlocks.
588 * The vm_token is needed to manipulate the vm_object
590 lwkt_gettoken(&vm_token);
591 vm_object_reference(fs.first_object);
592 fs.vp = vnode_pager_lock(fs.first_object);
593 vm_object_pip_add(fs.first_object, 1);
594 lwkt_reltoken(&vm_token);
596 fs.lookup_still_valid = TRUE;
598 fs.object = fs.first_object; /* so unlock_and_deallocate works */
601 * If the entry is wired we cannot change the page protection.
604 fault_type = fs.first_prot;
607 * The page we want is at (first_object, first_pindex), but if the
608 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
609 * page table to figure out the actual pindex.
611 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
614 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
615 result = vm_fault_vpagetable(&fs, &first_pindex,
616 fs.entry->aux.master_pde,
618 if (result == KERN_TRY_AGAIN)
620 if (result != KERN_SUCCESS) {
627 * Now we have the actual (object, pindex), fault in the page. If
628 * vm_fault_object() fails it will unlock and deallocate the FS
629 * data. If it succeeds everything remains locked and fs->object
630 * will have an additinal PIP count if it is not equal to
633 result = vm_fault_object(&fs, first_pindex, fault_type);
635 if (result == KERN_TRY_AGAIN)
637 if (result != KERN_SUCCESS) {
642 if ((orig_fault_type & VM_PROT_WRITE) &&
643 (fs.prot & VM_PROT_WRITE) == 0) {
644 *errorp = KERN_PROTECTION_FAILURE;
645 unlock_and_deallocate(&fs);
650 * On success vm_fault_object() does not unlock or deallocate, and fs.m
651 * will contain a busied page.
656 * Return a held page. We are not doing any pmap manipulation so do
657 * not set PG_MAPPED. However, adjust the page flags according to
658 * the fault type because the caller may not use a managed pmapping
659 * (so we don't want to lose the fact that the page will be dirtied
660 * if a write fault was specified).
662 lwkt_gettoken(&vm_token);
664 if (fault_type & VM_PROT_WRITE)
668 * Update the pmap. We really only have to do this if a COW
669 * occured to replace the read-only page with the new page. For
670 * now just do it unconditionally. XXX
672 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
673 vm_page_flag_set(fs.m, PG_REFERENCED);
676 * Unbusy the page by activating it. It remains held and will not
679 vm_page_activate(fs.m);
681 if (curthread->td_lwp) {
683 curthread->td_lwp->lwp_ru.ru_majflt++;
685 curthread->td_lwp->lwp_ru.ru_minflt++;
690 * Unlock everything, and return the held page.
692 vm_page_wakeup(fs.m);
693 vm_object_deallocate(fs.first_object);
694 /*fs.first_object = NULL; */
695 lwkt_reltoken(&vm_token);
702 * Fault in the specified (object,offset), dirty the returned page as
703 * needed. If the requested fault_type cannot be done NULL and an
706 * A held (but not busied) page is returned.
711 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
712 vm_prot_t fault_type, int fault_flags, int *errorp)
715 vm_pindex_t first_pindex;
716 struct faultstate fs;
717 struct vm_map_entry entry;
719 bzero(&entry, sizeof(entry));
720 entry.object.vm_object = object;
721 entry.maptype = VM_MAPTYPE_NORMAL;
722 entry.protection = entry.max_protection = fault_type;
726 fs.fault_flags = fault_flags;
728 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
732 fs.first_object = object;
733 first_pindex = OFF_TO_IDX(offset);
735 fs.first_prot = fault_type;
737 /*fs.map_generation = 0; unused */
740 * Make a reference to this object to prevent its disposal while we
741 * are messing with it. Once we have the reference, the map is free
742 * to be diddled. Since objects reference their shadows (and copies),
743 * they will stay around as well.
745 * Bump the paging-in-progress count to prevent size changes (e.g.
746 * truncation operations) during I/O. This must be done after
747 * obtaining the vnode lock in order to avoid possible deadlocks.
749 lwkt_gettoken(&vm_token);
750 vm_object_reference(fs.first_object);
751 fs.vp = vnode_pager_lock(fs.first_object);
752 vm_object_pip_add(fs.first_object, 1);
753 lwkt_reltoken(&vm_token);
755 fs.lookup_still_valid = TRUE;
757 fs.object = fs.first_object; /* so unlock_and_deallocate works */
760 /* XXX future - ability to operate on VM object using vpagetable */
761 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
762 result = vm_fault_vpagetable(&fs, &first_pindex,
763 fs.entry->aux.master_pde,
765 if (result == KERN_TRY_AGAIN)
767 if (result != KERN_SUCCESS) {
775 * Now we have the actual (object, pindex), fault in the page. If
776 * vm_fault_object() fails it will unlock and deallocate the FS
777 * data. If it succeeds everything remains locked and fs->object
778 * will have an additinal PIP count if it is not equal to
781 result = vm_fault_object(&fs, first_pindex, fault_type);
783 if (result == KERN_TRY_AGAIN)
785 if (result != KERN_SUCCESS) {
790 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
791 *errorp = KERN_PROTECTION_FAILURE;
792 unlock_and_deallocate(&fs);
797 * On success vm_fault_object() does not unlock or deallocate, and fs.m
798 * will contain a busied page.
803 * Return a held page. We are not doing any pmap manipulation so do
804 * not set PG_MAPPED. However, adjust the page flags according to
805 * the fault type because the caller may not use a managed pmapping
806 * (so we don't want to lose the fact that the page will be dirtied
807 * if a write fault was specified).
809 lwkt_gettoken(&vm_token);
811 if (fault_type & VM_PROT_WRITE)
814 if (fault_flags & VM_FAULT_DIRTY)
816 if (fault_flags & VM_FAULT_UNSWAP)
817 swap_pager_unswapped(fs.m);
820 * Indicate that the page was accessed.
822 vm_page_flag_set(fs.m, PG_REFERENCED);
825 * Unbusy the page by activating it. It remains held and will not
828 vm_page_activate(fs.m);
830 if (curthread->td_lwp) {
832 mycpu->gd_cnt.v_vm_faults++;
833 curthread->td_lwp->lwp_ru.ru_majflt++;
835 curthread->td_lwp->lwp_ru.ru_minflt++;
840 * Unlock everything, and return the held page.
842 vm_page_wakeup(fs.m);
843 vm_object_deallocate(fs.first_object);
844 /*fs.first_object = NULL; */
845 lwkt_reltoken(&vm_token);
852 * Translate the virtual page number (first_pindex) that is relative
853 * to the address space into a logical page number that is relative to the
854 * backing object. Use the virtual page table pointed to by (vpte).
856 * This implements an N-level page table. Any level can terminate the
857 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
858 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
860 * No requirements (vm_token need not be held).
864 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
865 vpte_t vpte, int fault_type)
868 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
869 int result = KERN_SUCCESS;
874 * We cannot proceed if the vpte is not valid, not readable
875 * for a read fault, or not writable for a write fault.
877 if ((vpte & VPTE_V) == 0) {
878 unlock_and_deallocate(fs);
879 return (KERN_FAILURE);
881 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
882 unlock_and_deallocate(fs);
883 return (KERN_FAILURE);
885 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
886 unlock_and_deallocate(fs);
887 return (KERN_FAILURE);
889 if ((vpte & VPTE_PS) || vshift == 0)
891 KKASSERT(vshift >= VPTE_PAGE_BITS);
894 * Get the page table page. Nominally we only read the page
895 * table, but since we are actively setting VPTE_M and VPTE_A,
896 * tell vm_fault_object() that we are writing it.
898 * There is currently no real need to optimize this.
900 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
901 VM_PROT_READ|VM_PROT_WRITE);
902 if (result != KERN_SUCCESS)
906 * Process the returned fs.m and look up the page table
907 * entry in the page table page.
909 vshift -= VPTE_PAGE_BITS;
910 lwb = lwbuf_alloc(fs->m);
911 ptep = ((vpte_t *)lwbuf_kva(lwb) +
912 ((*pindex >> vshift) & VPTE_PAGE_MASK));
916 * Page table write-back. If the vpte is valid for the
917 * requested operation, do a write-back to the page table.
919 * XXX VPTE_M is not set properly for page directory pages.
920 * It doesn't get set in the page directory if the page table
921 * is modified during a read access.
923 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
925 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
926 atomic_set_long(ptep, VPTE_M | VPTE_A);
927 vm_page_dirty(fs->m);
930 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
932 if ((vpte & VPTE_A) == 0) {
933 atomic_set_long(ptep, VPTE_A);
934 vm_page_dirty(fs->m);
938 vm_page_flag_set(fs->m, PG_REFERENCED);
939 vm_page_activate(fs->m);
940 vm_page_wakeup(fs->m);
942 cleanup_successful_fault(fs);
945 * Combine remaining address bits with the vpte.
947 /* JG how many bits from each? */
948 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
949 (*pindex & ((1L << vshift) - 1));
950 return (KERN_SUCCESS);
955 * This is the core of the vm_fault code.
957 * Do all operations required to fault-in (fs.first_object, pindex). Run
958 * through the shadow chain as necessary and do required COW or virtual
959 * copy operations. The caller has already fully resolved the vm_map_entry
960 * and, if appropriate, has created a copy-on-write layer. All we need to
961 * do is iterate the object chain.
963 * On failure (fs) is unlocked and deallocated and the caller may return or
964 * retry depending on the failure code. On success (fs) is NOT unlocked or
965 * deallocated, fs.m will contained a resolved, busied page, and fs.object
966 * will have an additional PIP count if it is not equal to fs.first_object.
972 vm_fault_object(struct faultstate *fs,
973 vm_pindex_t first_pindex, vm_prot_t fault_type)
975 vm_object_t next_object;
978 fs->prot = fs->first_prot;
979 fs->object = fs->first_object;
980 pindex = first_pindex;
983 * If a read fault occurs we try to make the page writable if
984 * possible. There are three cases where we cannot make the
985 * page mapping writable:
987 * (1) The mapping is read-only or the VM object is read-only,
988 * fs->prot above will simply not have VM_PROT_WRITE set.
990 * (2) If the mapping is a virtual page table we need to be able
991 * to detect writes so we can set VPTE_M in the virtual page
994 * (3) If the VM page is read-only or copy-on-write, upgrading would
995 * just result in an unnecessary COW fault.
997 * VM_PROT_VPAGED is set if faulting via a virtual page table and
998 * causes adjustments to the 'M'odify bit to also turn off write
999 * access to force a re-fault.
1001 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1002 if ((fault_type & VM_PROT_WRITE) == 0)
1003 fs->prot &= ~VM_PROT_WRITE;
1006 lwkt_gettoken(&vm_token);
1010 * If the object is dead, we stop here
1012 if (fs->object->flags & OBJ_DEAD) {
1013 unlock_and_deallocate(fs);
1014 lwkt_reltoken(&vm_token);
1015 return (KERN_PROTECTION_FAILURE);
1019 * See if the page is resident.
1021 fs->m = vm_page_lookup(fs->object, pindex);
1022 if (fs->m != NULL) {
1025 * Wait/Retry if the page is busy. We have to do this
1026 * if the page is busy via either PG_BUSY or
1027 * vm_page_t->busy because the vm_pager may be using
1028 * vm_page_t->busy for pageouts ( and even pageins if
1029 * it is the vnode pager ), and we could end up trying
1030 * to pagein and pageout the same page simultaneously.
1032 * We can theoretically allow the busy case on a read
1033 * fault if the page is marked valid, but since such
1034 * pages are typically already pmap'd, putting that
1035 * special case in might be more effort then it is
1036 * worth. We cannot under any circumstances mess
1037 * around with a vm_page_t->busy page except, perhaps,
1040 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
1042 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1043 mycpu->gd_cnt.v_intrans++;
1044 vm_object_deallocate(fs->first_object);
1045 fs->first_object = NULL;
1046 lwkt_reltoken(&vm_token);
1047 return (KERN_TRY_AGAIN);
1051 * If reactivating a page from PQ_CACHE we may have
1054 queue = fs->m->queue;
1055 vm_page_unqueue_nowakeup(fs->m);
1057 if ((queue - fs->m->pc) == PQ_CACHE &&
1058 vm_page_count_severe()) {
1059 vm_page_activate(fs->m);
1060 unlock_and_deallocate(fs);
1062 lwkt_reltoken(&vm_token);
1063 return (KERN_TRY_AGAIN);
1067 * Mark page busy for other processes, and the
1068 * pagedaemon. If it still isn't completely valid
1069 * (readable), or if a read-ahead-mark is set on
1070 * the VM page, jump to readrest, else we found the
1071 * page and can return.
1073 * We can release the spl once we have marked the
1076 vm_page_busy(fs->m);
1078 if (fs->m->object != &kernel_object) {
1079 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1083 if (fs->m->flags & PG_RAM) {
1086 vm_page_flag_clear(fs->m, PG_RAM);
1090 break; /* break to PAGE HAS BEEN FOUND */
1094 * Page is not resident, If this is the search termination
1095 * or the pager might contain the page, allocate a new page.
1097 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1099 * If the page is beyond the object size we fail
1101 if (pindex >= fs->object->size) {
1102 lwkt_reltoken(&vm_token);
1103 unlock_and_deallocate(fs);
1104 return (KERN_PROTECTION_FAILURE);
1110 if (fs->didlimit == 0 && curproc != NULL) {
1113 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1115 lwkt_reltoken(&vm_token);
1116 unlock_and_deallocate(fs);
1117 tsleep(curproc, 0, "vmrate", limticks);
1119 return (KERN_TRY_AGAIN);
1124 * Allocate a new page for this object/offset pair.
1127 if (!vm_page_count_severe()) {
1128 fs->m = vm_page_alloc(fs->object, pindex,
1129 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1131 if (fs->m == NULL) {
1132 lwkt_reltoken(&vm_token);
1133 unlock_and_deallocate(fs);
1135 return (KERN_TRY_AGAIN);
1141 * We have found an invalid or partially valid page, a
1142 * page with a read-ahead mark which might be partially or
1143 * fully valid (and maybe dirty too), or we have allocated
1146 * Attempt to fault-in the page if there is a chance that the
1147 * pager has it, and potentially fault in additional pages
1150 * We are NOT in splvm here and if TRYPAGER is true then
1151 * fs.m will be non-NULL and will be PG_BUSY for us.
1156 u_char behavior = vm_map_entry_behavior(fs->entry);
1158 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1164 * If sequential access is detected then attempt
1165 * to deactivate/cache pages behind the scan to
1166 * prevent resource hogging.
1168 * Use of PG_RAM to detect sequential access
1169 * also simulates multi-zone sequential access
1170 * detection for free.
1172 * NOTE: Partially valid dirty pages cannot be
1173 * deactivated without causing NFS picemeal
1176 if ((fs->first_object->type != OBJT_DEVICE) &&
1177 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1178 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1179 (fs->m->flags & PG_RAM)))
1181 vm_pindex_t scan_pindex;
1182 int scan_count = 16;
1184 if (first_pindex < 16) {
1188 scan_pindex = first_pindex - 16;
1189 if (scan_pindex < 16)
1190 scan_count = scan_pindex;
1195 while (scan_count) {
1198 mt = vm_page_lookup(fs->first_object,
1201 (mt->valid != VM_PAGE_BITS_ALL)) {
1205 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1212 vm_page_test_dirty(mt);
1216 vm_page_deactivate(mt);
1230 * Avoid deadlocking against the map when doing I/O.
1231 * fs.object and the page is PG_BUSY'd.
1236 * Acquire the page data. We still hold a ref on
1237 * fs.object and the page has been PG_BUSY's.
1239 * The pager may replace the page (for example, in
1240 * order to enter a fictitious page into the
1241 * object). If it does so it is responsible for
1242 * cleaning up the passed page and properly setting
1243 * the new page PG_BUSY.
1245 * If we got here through a PG_RAM read-ahead
1246 * mark the page may be partially dirty and thus
1247 * not freeable. Don't bother checking to see
1248 * if the pager has the page because we can't free
1249 * it anyway. We have to depend on the get_page
1250 * operation filling in any gaps whether there is
1251 * backing store or not.
1253 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1255 if (rv == VM_PAGER_OK) {
1257 * Relookup in case pager changed page. Pager
1258 * is responsible for disposition of old page
1261 * XXX other code segments do relookups too.
1262 * It's a bad abstraction that needs to be
1265 fs->m = vm_page_lookup(fs->object, pindex);
1266 if (fs->m == NULL) {
1267 lwkt_reltoken(&vm_token);
1268 unlock_and_deallocate(fs);
1269 return (KERN_TRY_AGAIN);
1273 break; /* break to PAGE HAS BEEN FOUND */
1277 * Remove the bogus page (which does not exist at this
1278 * object/offset); before doing so, we must get back
1279 * our object lock to preserve our invariant.
1281 * Also wake up any other process that may want to bring
1284 * If this is the top-level object, we must leave the
1285 * busy page to prevent another process from rushing
1286 * past us, and inserting the page in that object at
1287 * the same time that we are.
1289 if (rv == VM_PAGER_ERROR) {
1291 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
1293 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
1297 * Data outside the range of the pager or an I/O error
1299 * The page may have been wired during the pagein,
1300 * e.g. by the buffer cache, and cannot simply be
1301 * freed. Call vnode_pager_freepage() to deal with it.
1304 * XXX - the check for kernel_map is a kludge to work
1305 * around having the machine panic on a kernel space
1306 * fault w/ I/O error.
1308 if (((fs->map != &kernel_map) &&
1309 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1310 vnode_pager_freepage(fs->m);
1311 lwkt_reltoken(&vm_token);
1313 unlock_and_deallocate(fs);
1314 if (rv == VM_PAGER_ERROR)
1315 return (KERN_FAILURE);
1317 return (KERN_PROTECTION_FAILURE);
1320 if (fs->object != fs->first_object) {
1321 vnode_pager_freepage(fs->m);
1324 * XXX - we cannot just fall out at this
1325 * point, m has been freed and is invalid!
1331 * We get here if the object has a default pager (or unwiring)
1332 * or the pager doesn't have the page.
1334 if (fs->object == fs->first_object)
1335 fs->first_m = fs->m;
1338 * Move on to the next object. Lock the next object before
1339 * unlocking the current one.
1341 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1342 next_object = fs->object->backing_object;
1343 if (next_object == NULL) {
1345 * If there's no object left, fill the page in the top
1346 * object with zeros.
1348 if (fs->object != fs->first_object) {
1349 vm_object_pip_wakeup(fs->object);
1351 fs->object = fs->first_object;
1352 pindex = first_pindex;
1353 fs->m = fs->first_m;
1358 * Zero the page if necessary and mark it valid.
1360 if ((fs->m->flags & PG_ZERO) == 0) {
1361 vm_page_zero_fill(fs->m);
1364 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1366 vm_page_flag_clear(fs->m, PG_ZERO);
1367 mycpu->gd_cnt.v_ozfod++;
1369 mycpu->gd_cnt.v_zfod++;
1370 fs->m->valid = VM_PAGE_BITS_ALL;
1371 break; /* break to PAGE HAS BEEN FOUND */
1373 if (fs->object != fs->first_object) {
1374 vm_object_pip_wakeup(fs->object);
1376 KASSERT(fs->object != next_object,
1377 ("object loop %p", next_object));
1378 fs->object = next_object;
1379 vm_object_pip_add(fs->object, 1);
1383 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1386 * vm_token is still held
1388 * If the page is being written, but isn't already owned by the
1389 * top-level object, we have to copy it into a new page owned by the
1392 KASSERT((fs->m->flags & PG_BUSY) != 0,
1393 ("vm_fault: not busy after main loop"));
1395 if (fs->object != fs->first_object) {
1397 * We only really need to copy if we want to write it.
1399 if (fault_type & VM_PROT_WRITE) {
1401 * This allows pages to be virtually copied from a
1402 * backing_object into the first_object, where the
1403 * backing object has no other refs to it, and cannot
1404 * gain any more refs. Instead of a bcopy, we just
1405 * move the page from the backing object to the
1406 * first object. Note that we must mark the page
1407 * dirty in the first object so that it will go out
1408 * to swap when needed.
1412 * Map, if present, has not changed
1415 fs->map_generation == fs->map->timestamp) &&
1417 * Only one shadow object
1419 (fs->object->shadow_count == 1) &&
1421 * No COW refs, except us
1423 (fs->object->ref_count == 1) &&
1425 * No one else can look this object up
1427 (fs->object->handle == NULL) &&
1429 * No other ways to look the object up
1431 ((fs->object->type == OBJT_DEFAULT) ||
1432 (fs->object->type == OBJT_SWAP)) &&
1434 * We don't chase down the shadow chain
1436 (fs->object == fs->first_object->backing_object) &&
1439 * grab the lock if we need to
1441 (fs->lookup_still_valid ||
1443 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1446 fs->lookup_still_valid = 1;
1448 * get rid of the unnecessary page
1450 vm_page_protect(fs->first_m, VM_PROT_NONE);
1451 vm_page_free(fs->first_m);
1455 * grab the page and put it into the
1456 * process'es object. The page is
1457 * automatically made dirty.
1459 vm_page_rename(fs->m, fs->first_object, first_pindex);
1460 fs->first_m = fs->m;
1461 vm_page_busy(fs->first_m);
1463 mycpu->gd_cnt.v_cow_optim++;
1466 * Oh, well, lets copy it.
1468 vm_page_copy(fs->m, fs->first_m);
1469 vm_page_event(fs->m, VMEVENT_COW);
1474 * We no longer need the old page or object.
1480 * fs->object != fs->first_object due to above
1483 vm_object_pip_wakeup(fs->object);
1486 * Only use the new page below...
1489 mycpu->gd_cnt.v_cow_faults++;
1490 fs->m = fs->first_m;
1491 fs->object = fs->first_object;
1492 pindex = first_pindex;
1495 * If it wasn't a write fault avoid having to copy
1496 * the page by mapping it read-only.
1498 fs->prot &= ~VM_PROT_WRITE;
1503 * We may have had to unlock a map to do I/O. If we did then
1504 * lookup_still_valid will be FALSE. If the map generation count
1505 * also changed then all sorts of things could have happened while
1506 * we were doing the I/O and we need to retry.
1509 if (!fs->lookup_still_valid &&
1511 (fs->map->timestamp != fs->map_generation)) {
1513 lwkt_reltoken(&vm_token);
1514 unlock_and_deallocate(fs);
1515 return (KERN_TRY_AGAIN);
1519 * If the fault is a write, we know that this page is being
1520 * written NOW so dirty it explicitly to save on pmap_is_modified()
1523 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1524 * if the page is already dirty to prevent data written with
1525 * the expectation of being synced from not being synced.
1526 * Likewise if this entry does not request NOSYNC then make
1527 * sure the page isn't marked NOSYNC. Applications sharing
1528 * data should use the same flags to avoid ping ponging.
1530 * Also tell the backing pager, if any, that it should remove
1531 * any swap backing since the page is now dirty.
1533 if (fs->prot & VM_PROT_WRITE) {
1534 vm_object_set_writeable_dirty(fs->m->object);
1535 vm_set_nosync(fs->m, fs->entry);
1536 if (fs->fault_flags & VM_FAULT_DIRTY) {
1537 vm_page_dirty(fs->m);
1538 swap_pager_unswapped(fs->m);
1542 lwkt_reltoken(&vm_token);
1545 * Page had better still be busy. We are still locked up and
1546 * fs->object will have another PIP reference if it is not equal
1547 * to fs->first_object.
1549 KASSERT(fs->m->flags & PG_BUSY,
1550 ("vm_fault: page %p not busy!", fs->m));
1553 * Sanity check: page must be completely valid or it is not fit to
1554 * map into user space. vm_pager_get_pages() ensures this.
1556 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1557 vm_page_zero_invalid(fs->m, TRUE);
1558 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1560 vm_page_flag_clear(fs->m, PG_ZERO);
1562 return (KERN_SUCCESS);
1566 * Wire down a range of virtual addresses in a map. The entry in question
1567 * should be marked in-transition and the map must be locked. We must
1568 * release the map temporarily while faulting-in the page to avoid a
1569 * deadlock. Note that the entry may be clipped while we are blocked but
1570 * will never be freed.
1575 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1577 boolean_t fictitious;
1585 pmap = vm_map_pmap(map);
1586 start = entry->start;
1588 fictitious = entry->object.vm_object &&
1589 (entry->object.vm_object->type == OBJT_DEVICE);
1590 if (entry->eflags & MAP_ENTRY_KSTACK)
1592 lwkt_gettoken(&vm_token);
1597 * We simulate a fault to get the page and enter it in the physical
1600 for (va = start; va < end; va += PAGE_SIZE) {
1602 rv = vm_fault(map, va, VM_PROT_READ,
1603 VM_FAULT_USER_WIRE);
1605 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1606 VM_FAULT_CHANGE_WIRING);
1609 while (va > start) {
1611 if ((pa = pmap_extract(pmap, va)) == 0)
1613 pmap_change_wiring(pmap, va, FALSE);
1615 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1618 lwkt_reltoken(&vm_token);
1623 lwkt_reltoken(&vm_token);
1624 return (KERN_SUCCESS);
1628 * Unwire a range of virtual addresses in a map. The map should be
1632 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1634 boolean_t fictitious;
1641 pmap = vm_map_pmap(map);
1642 start = entry->start;
1644 fictitious = entry->object.vm_object &&
1645 (entry->object.vm_object->type == OBJT_DEVICE);
1646 if (entry->eflags & MAP_ENTRY_KSTACK)
1650 * Since the pages are wired down, we must be able to get their
1651 * mappings from the physical map system.
1653 lwkt_gettoken(&vm_token);
1654 for (va = start; va < end; va += PAGE_SIZE) {
1655 pa = pmap_extract(pmap, va);
1657 pmap_change_wiring(pmap, va, FALSE);
1659 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1662 lwkt_reltoken(&vm_token);
1666 * Reduce the rate at which memory is allocated to a process based
1667 * on the perceived load on the VM system. As the load increases
1668 * the allocation burst rate goes down and the delay increases.
1670 * Rate limiting does not apply when faulting active or inactive
1671 * pages. When faulting 'cache' pages, rate limiting only applies
1672 * if the system currently has a severe page deficit.
1674 * XXX vm_pagesupply should be increased when a page is freed.
1676 * We sleep up to 1/10 of a second.
1679 vm_fault_ratelimit(struct vmspace *vmspace)
1681 if (vm_load_enable == 0)
1683 if (vmspace->vm_pagesupply > 0) {
1684 --vmspace->vm_pagesupply; /* SMP race ok */
1688 if (vm_load_debug) {
1689 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1691 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1692 curproc->p_pid, curproc->p_comm);
1695 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1696 return(vm_load * hz / 10000);
1700 * Copy all of the pages from a wired-down map entry to another.
1702 * The source and destination maps must be locked for write.
1703 * The source map entry must be wired down (or be a sharing map
1704 * entry corresponding to a main map entry that is wired down).
1706 * No other requirements.
1709 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1710 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1712 vm_object_t dst_object;
1713 vm_object_t src_object;
1714 vm_ooffset_t dst_offset;
1715 vm_ooffset_t src_offset;
1725 src_object = src_entry->object.vm_object;
1726 src_offset = src_entry->offset;
1729 * Create the top-level object for the destination entry. (Doesn't
1730 * actually shadow anything - we copy the pages directly.)
1732 vm_map_entry_allocate_object(dst_entry);
1733 dst_object = dst_entry->object.vm_object;
1735 prot = dst_entry->max_protection;
1738 * Loop through all of the pages in the entry's range, copying each
1739 * one from the source object (it should be there) to the destination
1742 for (vaddr = dst_entry->start, dst_offset = 0;
1743 vaddr < dst_entry->end;
1744 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1747 * Allocate a page in the destination object
1750 dst_m = vm_page_alloc(dst_object,
1751 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1752 if (dst_m == NULL) {
1755 } while (dst_m == NULL);
1758 * Find the page in the source object, and copy it in.
1759 * (Because the source is wired down, the page will be in
1762 src_m = vm_page_lookup(src_object,
1763 OFF_TO_IDX(dst_offset + src_offset));
1765 panic("vm_fault_copy_wired: page missing");
1767 vm_page_copy(src_m, dst_m);
1768 vm_page_event(src_m, VMEVENT_COW);
1771 * Enter it in the pmap...
1774 vm_page_flag_clear(dst_m, PG_ZERO);
1775 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1778 * Mark it no longer busy, and put it on the active list.
1780 vm_page_activate(dst_m);
1781 vm_page_wakeup(dst_m);
1788 * This routine checks around the requested page for other pages that
1789 * might be able to be faulted in. This routine brackets the viable
1790 * pages for the pages to be paged in.
1793 * m, rbehind, rahead
1796 * marray (array of vm_page_t), reqpage (index of requested page)
1799 * number of pages in marray
1802 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1803 vm_page_t *marray, int *reqpage)
1807 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1809 int cbehind, cahead;
1815 * we don't fault-ahead for device pager
1817 if (object->type == OBJT_DEVICE) {
1824 * if the requested page is not available, then give up now
1826 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1827 *reqpage = 0; /* not used by caller, fix compiler warn */
1831 if ((cbehind == 0) && (cahead == 0)) {
1837 if (rahead > cahead) {
1841 if (rbehind > cbehind) {
1846 * Do not do any readahead if we have insufficient free memory.
1848 * XXX code was broken disabled before and has instability
1849 * with this conditonal fixed, so shortcut for now.
1851 if (burst_fault == 0 || vm_page_count_severe()) {
1858 * scan backward for the read behind pages -- in memory
1860 * Assume that if the page is not found an interrupt will not
1861 * create it. Theoretically interrupts can only remove (busy)
1862 * pages, not create new associations.
1865 if (rbehind > pindex) {
1869 startpindex = pindex - rbehind;
1872 lwkt_gettoken(&vm_token);
1873 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1874 if (vm_page_lookup(object, tpindex - 1))
1879 while (tpindex < pindex) {
1880 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1882 lwkt_reltoken(&vm_token);
1883 for (j = 0; j < i; j++) {
1884 vm_page_free(marray[j]);
1894 lwkt_reltoken(&vm_token);
1900 * Assign requested page
1907 * Scan forwards for read-ahead pages
1909 tpindex = pindex + 1;
1910 endpindex = tpindex + rahead;
1911 if (endpindex > object->size)
1912 endpindex = object->size;
1914 lwkt_gettoken(&vm_token);
1915 while (tpindex < endpindex) {
1916 if (vm_page_lookup(object, tpindex))
1918 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1925 lwkt_reltoken(&vm_token);
1933 * vm_prefault() provides a quick way of clustering pagefaults into a
1934 * processes address space. It is a "cousin" of pmap_object_init_pt,
1935 * except it runs at page fault time instead of mmap time.
1937 * This code used to be per-platform pmap_prefault(). It is now
1938 * machine-independent and enhanced to also pre-fault zero-fill pages
1939 * (see vm.fast_fault) as well as make them writable, which greatly
1940 * reduces the number of page faults programs incur.
1942 * Application performance when pre-faulting zero-fill pages is heavily
1943 * dependent on the application. Very tiny applications like /bin/echo
1944 * lose a little performance while applications of any appreciable size
1945 * gain performance. Prefaulting multiple pages also reduces SMP
1946 * congestion and can improve SMP performance significantly.
1948 * NOTE! prot may allow writing but this only applies to the top level
1949 * object. If we wind up mapping a page extracted from a backing
1950 * object we have to make sure it is read-only.
1952 * NOTE! The caller has already handled any COW operations on the
1953 * vm_map_entry via the normal fault code. Do NOT call this
1954 * shortcut unless the normal fault code has run on this entry.
1956 * No other requirements.
1960 #define PAGEORDER_SIZE (PFBAK+PFFOR)
1962 static int vm_prefault_pageorder[] = {
1963 -PAGE_SIZE, PAGE_SIZE,
1964 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1965 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1966 -4 * PAGE_SIZE, 4 * PAGE_SIZE
1970 * Set PG_NOSYNC if the map entry indicates so, but only if the page
1971 * is not already dirty by other means. This will prevent passive
1972 * filesystem syncing as well as 'sync' from writing out the page.
1975 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
1977 if (entry->eflags & MAP_ENTRY_NOSYNC) {
1979 vm_page_flag_set(m, PG_NOSYNC);
1981 vm_page_flag_clear(m, PG_NOSYNC);
1986 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
1999 * We do not currently prefault mappings that use virtual page
2000 * tables. We do not prefault foreign pmaps.
2002 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2004 lp = curthread->td_lwp;
2005 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2008 object = entry->object.vm_object;
2010 starta = addra - PFBAK * PAGE_SIZE;
2011 if (starta < entry->start)
2012 starta = entry->start;
2013 else if (starta > addra)
2016 lwkt_gettoken(&vm_token);
2017 for (i = 0; i < PAGEORDER_SIZE; i++) {
2018 vm_object_t lobject;
2021 addr = addra + vm_prefault_pageorder[i];
2022 if (addr > addra + (PFFOR * PAGE_SIZE))
2025 if (addr < starta || addr >= entry->end)
2028 if (pmap_prefault_ok(pmap, addr) == 0)
2032 * Follow the VM object chain to obtain the page to be mapped
2035 * If we reach the terminal object without finding a page
2036 * and we determine it would be advantageous, then allocate
2037 * a zero-fill page for the base object. The base object
2038 * is guaranteed to be OBJT_DEFAULT for this case.
2040 * In order to not have to check the pager via *haspage*()
2041 * we stop if any non-default object is encountered. e.g.
2042 * a vnode or swap object would stop the loop.
2044 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2049 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
2050 if (lobject->type != OBJT_DEFAULT)
2052 if (lobject->backing_object == NULL) {
2053 if (vm_fast_fault == 0)
2055 if (vm_prefault_pageorder[i] < 0 ||
2056 (prot & VM_PROT_WRITE) == 0 ||
2057 vm_page_count_min(0)) {
2060 /* note: allocate from base object */
2061 m = vm_page_alloc(object, index,
2062 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
2064 if ((m->flags & PG_ZERO) == 0) {
2065 vm_page_zero_fill(m);
2068 pmap_page_assertzero(VM_PAGE_TO_PHYS(m));
2070 vm_page_flag_clear(m, PG_ZERO);
2071 mycpu->gd_cnt.v_ozfod++;
2073 mycpu->gd_cnt.v_zfod++;
2074 m->valid = VM_PAGE_BITS_ALL;
2077 /* lobject = object .. not needed */
2080 if (lobject->backing_object_offset & PAGE_MASK)
2082 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2083 lobject = lobject->backing_object;
2084 pprot &= ~VM_PROT_WRITE;
2087 * NOTE: lobject now invalid (if we did a zero-fill we didn't
2088 * bother assigning lobject = object).
2090 * Give-up if the page is not available.
2096 * Do not conditionalize on PG_RAM. If pages are present in
2097 * the VM system we assume optimal caching. If caching is
2098 * not optimal the I/O gravy train will be restarted when we
2099 * hit an unavailable page. We do not want to try to restart
2100 * the gravy train now because we really don't know how much
2101 * of the object has been cached. The cost for restarting
2102 * the gravy train should be low (since accesses will likely
2103 * be I/O bound anyway).
2105 * The object must be marked dirty if we are mapping a
2108 if (pprot & VM_PROT_WRITE)
2109 vm_object_set_writeable_dirty(m->object);
2112 * Enter the page into the pmap if appropriate. If we had
2113 * allocated the page we have to place it on a queue. If not
2114 * we just have to make sure it isn't on the cache queue
2115 * (pages on the cache queue are not allowed to be mapped).
2118 if (pprot & VM_PROT_WRITE)
2119 vm_set_nosync(m, entry);
2120 pmap_enter(pmap, addr, m, pprot, 0);
2121 vm_page_deactivate(m);
2123 } else if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2125 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2128 if ((m->queue - m->pc) == PQ_CACHE) {
2129 vm_page_deactivate(m);
2131 if (pprot & VM_PROT_WRITE)
2132 vm_set_nosync(m, entry);
2133 pmap_enter(pmap, addr, m, pprot, 0);
2137 lwkt_reltoken(&vm_token);