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 release_page(struct faultstate *fs)
145 vm_page_deactivate(fs->m);
146 vm_page_wakeup(fs->m);
151 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
152 * requires relocking and then checking the timestamp.
154 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
155 * not have to update fs->map_generation here.
157 * NOTE: This function can fail due to a deadlock against the caller's
158 * holding of a vm_page BUSY.
161 relock_map(struct faultstate *fs)
165 if (fs->lookup_still_valid == FALSE && fs->map) {
166 error = vm_map_lock_read_to(fs->map);
168 fs->lookup_still_valid = TRUE;
176 unlock_map(struct faultstate *fs)
178 if (fs->lookup_still_valid && fs->map) {
179 vm_map_lookup_done(fs->map, fs->entry, 0);
180 fs->lookup_still_valid = FALSE;
185 * Clean up after a successful call to vm_fault_object() so another call
186 * to vm_fault_object() can be made.
189 _cleanup_successful_fault(struct faultstate *fs, int relock)
191 if (fs->object != fs->first_object) {
192 vm_page_free(fs->first_m);
193 vm_object_pip_wakeup(fs->object);
196 fs->object = fs->first_object;
197 if (relock && fs->lookup_still_valid == FALSE) {
199 vm_map_lock_read(fs->map);
200 fs->lookup_still_valid = TRUE;
205 _unlock_things(struct faultstate *fs, int dealloc)
207 _cleanup_successful_fault(fs, 0);
209 /*vm_object_deallocate(fs->first_object);*/
210 /*fs->first_object = NULL; drop used later on */
213 if (fs->vp != NULL) {
219 #define unlock_things(fs) _unlock_things(fs, 0)
220 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
221 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
226 * Determine if the pager for the current object *might* contain the page.
228 * We only need to try the pager if this is not a default object (default
229 * objects are zero-fill and have no real pager), and if we are not taking
230 * a wiring fault or if the FS entry is wired.
232 #define TRYPAGER(fs) \
233 (fs->object->type != OBJT_DEFAULT && \
234 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
239 * Handle a page fault occuring at the given address, requiring the given
240 * permissions, in the map specified. If successful, the page is inserted
241 * into the associated physical map.
243 * NOTE: The given address should be truncated to the proper page address.
245 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
246 * a standard error specifying why the fault is fatal is returned.
248 * The map in question must be referenced, and remains so.
249 * The caller may hold no locks.
250 * No other requirements.
253 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
256 vm_pindex_t first_pindex;
257 struct faultstate fs;
260 mycpu->gd_cnt.v_vm_faults++;
264 fs.fault_flags = fault_flags;
267 lwkt_gettoken(&map->token);
271 * Find the vm_map_entry representing the backing store and resolve
272 * the top level object and page index. This may have the side
273 * effect of executing a copy-on-write on the map entry and/or
274 * creating a shadow object, but will not COW any actual VM pages.
276 * On success fs.map is left read-locked and various other fields
277 * are initialized but not otherwise referenced or locked.
279 * NOTE! vm_map_lookup will try to upgrade the fault_type to
280 * VM_FAULT_WRITE if the map entry is a virtual page table and also
281 * writable, so we can set the 'A'accessed bit in the virtual page
285 result = vm_map_lookup(&fs.map, vaddr, fault_type,
286 &fs.entry, &fs.first_object,
287 &first_pindex, &fs.first_prot, &fs.wired);
290 * If the lookup failed or the map protections are incompatible,
291 * the fault generally fails. However, if the caller is trying
292 * to do a user wiring we have more work to do.
294 if (result != KERN_SUCCESS) {
295 if (result != KERN_PROTECTION_FAILURE ||
296 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
298 if (result == KERN_INVALID_ADDRESS && growstack &&
299 map != &kernel_map && curproc != NULL) {
300 result = vm_map_growstack(curproc, vaddr);
301 if (result == KERN_SUCCESS) {
305 result = KERN_FAILURE;
311 * If we are user-wiring a r/w segment, and it is COW, then
312 * we need to do the COW operation. Note that we don't
313 * currently COW RO sections now, because it is NOT desirable
314 * to COW .text. We simply keep .text from ever being COW'ed
315 * and take the heat that one cannot debug wired .text sections.
317 result = vm_map_lookup(&fs.map, vaddr,
318 VM_PROT_READ|VM_PROT_WRITE|
319 VM_PROT_OVERRIDE_WRITE,
320 &fs.entry, &fs.first_object,
321 &first_pindex, &fs.first_prot,
323 if (result != KERN_SUCCESS) {
324 result = KERN_FAILURE;
329 * If we don't COW now, on a user wire, the user will never
330 * be able to write to the mapping. If we don't make this
331 * restriction, the bookkeeping would be nearly impossible.
333 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
334 fs.entry->max_protection &= ~VM_PROT_WRITE;
338 * fs.map is read-locked
340 * Misc checks. Save the map generation number to detect races.
342 fs.map_generation = fs.map->timestamp;
344 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
345 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
346 panic("vm_fault: fault on nofault entry, addr: %p",
349 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
350 vaddr >= fs.entry->start &&
351 vaddr < fs.entry->start + PAGE_SIZE) {
352 panic("vm_fault: fault on stack guard, addr: %p",
358 * A system map entry may return a NULL object. No object means
359 * no pager means an unrecoverable kernel fault.
361 if (fs.first_object == NULL) {
362 panic("vm_fault: unrecoverable fault at %p in entry %p",
363 (void *)vaddr, fs.entry);
367 * Bump the paging-in-progress count to prevent size changes (e.g.
368 * truncation operations) during I/O. This must be done after
369 * obtaining the vnode lock in order to avoid possible deadlocks.
371 vm_object_hold(fs.first_object);
372 fs.vp = vnode_pager_lock(fs.first_object);
374 fs.lookup_still_valid = TRUE;
376 fs.object = fs.first_object; /* so unlock_and_deallocate works */
379 * If the entry is wired we cannot change the page protection.
382 fault_type = fs.first_prot;
385 * The page we want is at (first_object, first_pindex), but if the
386 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
387 * page table to figure out the actual pindex.
389 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
392 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
393 result = vm_fault_vpagetable(&fs, &first_pindex,
394 fs.entry->aux.master_pde,
396 if (result == KERN_TRY_AGAIN) {
397 vm_object_drop(fs.first_object);
400 if (result != KERN_SUCCESS)
405 * Now we have the actual (object, pindex), fault in the page. If
406 * vm_fault_object() fails it will unlock and deallocate the FS
407 * data. If it succeeds everything remains locked and fs->object
408 * will have an additional PIP count if it is not equal to
411 * vm_fault_object will set fs->prot for the pmap operation. It is
412 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
413 * page can be safely written. However, it will force a read-only
414 * mapping for a read fault if the memory is managed by a virtual
418 result = vm_fault_object(&fs, first_pindex, fault_type);
420 if (result == KERN_TRY_AGAIN) {
421 vm_object_drop(fs.first_object);
424 if (result != KERN_SUCCESS)
428 * On success vm_fault_object() does not unlock or deallocate, and fs.m
429 * will contain a busied page.
431 * Enter the page into the pmap and do pmap-related adjustments.
433 vm_page_flag_set(fs.m, PG_REFERENCED);
434 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
437 * Burst in a few more pages if possible. The fs.map should still
440 if (fault_flags & VM_FAULT_BURST) {
441 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
443 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
448 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
449 KKASSERT(fs.m->flags & PG_BUSY);
452 * If the page is not wired down, then put it where the pageout daemon
456 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
460 vm_page_unwire(fs.m, 1);
462 vm_page_activate(fs.m);
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; must still drop later */
481 result = KERN_SUCCESS;
484 vm_object_drop(fs.first_object);
485 lwkt_reltoken(&map->token);
490 * Fault in the specified virtual address in the current process map,
491 * returning a held VM page or NULL. See vm_fault_page() for more
497 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
499 struct lwp *lp = curthread->td_lwp;
502 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
503 fault_type, VM_FAULT_NORMAL, errorp);
508 * Fault in the specified virtual address in the specified map, doing all
509 * necessary manipulation of the object store and all necessary I/O. Return
510 * a held VM page or NULL, and set *errorp. The related pmap is not
513 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
514 * and marked PG_REFERENCED as well.
516 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
517 * error will be returned.
522 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
523 int fault_flags, int *errorp)
525 vm_pindex_t first_pindex;
526 struct faultstate fs;
528 vm_prot_t orig_fault_type = fault_type;
530 mycpu->gd_cnt.v_vm_faults++;
534 fs.fault_flags = fault_flags;
535 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
537 lwkt_gettoken(&map->token);
541 * Find the vm_map_entry representing the backing store and resolve
542 * the top level object and page index. This may have the side
543 * effect of executing a copy-on-write on the map entry and/or
544 * creating a shadow object, but will not COW any actual VM pages.
546 * On success fs.map is left read-locked and various other fields
547 * are initialized but not otherwise referenced or locked.
549 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
550 * if the map entry is a virtual page table and also writable,
551 * so we can set the 'A'accessed bit in the virtual page table entry.
554 result = vm_map_lookup(&fs.map, vaddr, fault_type,
555 &fs.entry, &fs.first_object,
556 &first_pindex, &fs.first_prot, &fs.wired);
558 if (result != KERN_SUCCESS) {
565 * fs.map is read-locked
567 * Misc checks. Save the map generation number to detect races.
569 fs.map_generation = fs.map->timestamp;
571 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
572 panic("vm_fault: fault on nofault entry, addr: %lx",
577 * A system map entry may return a NULL object. No object means
578 * no pager means an unrecoverable kernel fault.
580 if (fs.first_object == NULL) {
581 panic("vm_fault: unrecoverable fault at %p in entry %p",
582 (void *)vaddr, fs.entry);
586 * Make a reference to this object to prevent its disposal while we
587 * are messing with it. Once we have the reference, the map is free
588 * to be diddled. Since objects reference their shadows (and copies),
589 * they will stay around as well.
591 * The reference should also prevent an unexpected collapse of the
592 * parent that might move pages from the current object into the
593 * parent unexpectedly, resulting in corruption.
595 * Bump the paging-in-progress count to prevent size changes (e.g.
596 * truncation operations) during I/O. This must be done after
597 * obtaining the vnode lock in order to avoid possible deadlocks.
599 vm_object_hold(fs.first_object);
600 fs.vp = vnode_pager_lock(fs.first_object);
602 fs.lookup_still_valid = TRUE;
604 fs.object = fs.first_object; /* so unlock_and_deallocate works */
607 * If the entry is wired we cannot change the page protection.
610 fault_type = fs.first_prot;
613 * The page we want is at (first_object, first_pindex), but if the
614 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
615 * page table to figure out the actual pindex.
617 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
620 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
621 result = vm_fault_vpagetable(&fs, &first_pindex,
622 fs.entry->aux.master_pde,
624 if (result == KERN_TRY_AGAIN) {
625 vm_object_drop(fs.first_object);
628 if (result != KERN_SUCCESS) {
636 * Now we have the actual (object, pindex), fault in the page. If
637 * vm_fault_object() fails it will unlock and deallocate the FS
638 * data. If it succeeds everything remains locked and fs->object
639 * will have an additinal PIP count if it is not equal to
642 result = vm_fault_object(&fs, first_pindex, fault_type);
644 if (result == KERN_TRY_AGAIN) {
645 vm_object_drop(fs.first_object);
648 if (result != KERN_SUCCESS) {
654 if ((orig_fault_type & VM_PROT_WRITE) &&
655 (fs.prot & VM_PROT_WRITE) == 0) {
656 *errorp = KERN_PROTECTION_FAILURE;
657 unlock_and_deallocate(&fs);
663 * On success vm_fault_object() does not unlock or deallocate, and fs.m
664 * will contain a busied page.
669 * Return a held page. We are not doing any pmap manipulation so do
670 * not set PG_MAPPED. However, adjust the page flags according to
671 * the fault type because the caller may not use a managed pmapping
672 * (so we don't want to lose the fact that the page will be dirtied
673 * if a write fault was specified).
676 if (fault_type & VM_PROT_WRITE)
680 * Update the pmap. We really only have to do this if a COW
681 * occured to replace the read-only page with the new page. For
682 * now just do it unconditionally. XXX
684 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
685 vm_page_flag_set(fs.m, PG_REFERENCED);
688 * Unbusy the page by activating it. It remains held and will not
691 vm_page_activate(fs.m);
693 if (curthread->td_lwp) {
695 curthread->td_lwp->lwp_ru.ru_majflt++;
697 curthread->td_lwp->lwp_ru.ru_minflt++;
702 * Unlock everything, and return the held page.
704 vm_page_wakeup(fs.m);
705 /*vm_object_deallocate(fs.first_object);*/
706 /*fs.first_object = NULL; */
711 vm_object_drop(fs.first_object);
712 lwkt_reltoken(&map->token);
717 * Fault in the specified (object,offset), dirty the returned page as
718 * needed. If the requested fault_type cannot be done NULL and an
721 * A held (but not busied) page is returned.
726 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
727 vm_prot_t fault_type, int fault_flags, int *errorp)
730 vm_pindex_t first_pindex;
731 struct faultstate fs;
732 struct vm_map_entry entry;
734 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
735 bzero(&entry, sizeof(entry));
736 entry.object.vm_object = object;
737 entry.maptype = VM_MAPTYPE_NORMAL;
738 entry.protection = entry.max_protection = fault_type;
742 fs.fault_flags = fault_flags;
744 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
748 fs.first_object = object;
749 first_pindex = OFF_TO_IDX(offset);
751 fs.first_prot = fault_type;
753 /*fs.map_generation = 0; unused */
756 * Make a reference to this object to prevent its disposal while we
757 * are messing with it. Once we have the reference, the map is free
758 * to be diddled. Since objects reference their shadows (and copies),
759 * they will stay around as well.
761 * The reference should also prevent an unexpected collapse of the
762 * parent that might move pages from the current object into the
763 * parent unexpectedly, resulting in corruption.
765 * Bump the paging-in-progress count to prevent size changes (e.g.
766 * truncation operations) during I/O. This must be done after
767 * obtaining the vnode lock in order to avoid possible deadlocks.
769 fs.vp = vnode_pager_lock(fs.first_object);
771 fs.lookup_still_valid = TRUE;
773 fs.object = fs.first_object; /* so unlock_and_deallocate works */
776 /* XXX future - ability to operate on VM object using vpagetable */
777 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
778 result = vm_fault_vpagetable(&fs, &first_pindex,
779 fs.entry->aux.master_pde,
781 if (result == KERN_TRY_AGAIN)
783 if (result != KERN_SUCCESS) {
791 * Now we have the actual (object, pindex), fault in the page. If
792 * vm_fault_object() fails it will unlock and deallocate the FS
793 * data. If it succeeds everything remains locked and fs->object
794 * will have an additinal PIP count if it is not equal to
797 result = vm_fault_object(&fs, first_pindex, fault_type);
799 if (result == KERN_TRY_AGAIN)
801 if (result != KERN_SUCCESS) {
806 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
807 *errorp = KERN_PROTECTION_FAILURE;
808 unlock_and_deallocate(&fs);
813 * On success vm_fault_object() does not unlock or deallocate, and fs.m
814 * will contain a busied page.
819 * Return a held page. We are not doing any pmap manipulation so do
820 * not set PG_MAPPED. However, adjust the page flags according to
821 * the fault type because the caller may not use a managed pmapping
822 * (so we don't want to lose the fact that the page will be dirtied
823 * if a write fault was specified).
826 if (fault_type & VM_PROT_WRITE)
829 if (fault_flags & VM_FAULT_DIRTY)
831 if (fault_flags & VM_FAULT_UNSWAP)
832 swap_pager_unswapped(fs.m);
835 * Indicate that the page was accessed.
837 vm_page_flag_set(fs.m, PG_REFERENCED);
840 * Unbusy the page by activating it. It remains held and will not
843 vm_page_activate(fs.m);
845 if (curthread->td_lwp) {
847 mycpu->gd_cnt.v_vm_faults++;
848 curthread->td_lwp->lwp_ru.ru_majflt++;
850 curthread->td_lwp->lwp_ru.ru_minflt++;
855 * Unlock everything, and return the held page.
857 vm_page_wakeup(fs.m);
858 /*vm_object_deallocate(fs.first_object);*/
859 /*fs.first_object = NULL; */
866 * Translate the virtual page number (first_pindex) that is relative
867 * to the address space into a logical page number that is relative to the
868 * backing object. Use the virtual page table pointed to by (vpte).
870 * This implements an N-level page table. Any level can terminate the
871 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
872 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
876 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
877 vpte_t vpte, int fault_type)
880 struct lwbuf lwb_cache;
881 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
882 int result = KERN_SUCCESS;
885 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
888 * We cannot proceed if the vpte is not valid, not readable
889 * for a read fault, or not writable for a write fault.
891 if ((vpte & VPTE_V) == 0) {
892 unlock_and_deallocate(fs);
893 return (KERN_FAILURE);
895 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
896 unlock_and_deallocate(fs);
897 return (KERN_FAILURE);
899 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
900 unlock_and_deallocate(fs);
901 return (KERN_FAILURE);
903 if ((vpte & VPTE_PS) || vshift == 0)
905 KKASSERT(vshift >= VPTE_PAGE_BITS);
908 * Get the page table page. Nominally we only read the page
909 * table, but since we are actively setting VPTE_M and VPTE_A,
910 * tell vm_fault_object() that we are writing it.
912 * There is currently no real need to optimize this.
914 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
915 VM_PROT_READ|VM_PROT_WRITE);
916 if (result != KERN_SUCCESS)
920 * Process the returned fs.m and look up the page table
921 * entry in the page table page.
923 vshift -= VPTE_PAGE_BITS;
924 lwb = lwbuf_alloc(fs->m, &lwb_cache);
925 ptep = ((vpte_t *)lwbuf_kva(lwb) +
926 ((*pindex >> vshift) & VPTE_PAGE_MASK));
930 * Page table write-back. If the vpte is valid for the
931 * requested operation, do a write-back to the page table.
933 * XXX VPTE_M is not set properly for page directory pages.
934 * It doesn't get set in the page directory if the page table
935 * is modified during a read access.
937 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
939 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
940 atomic_set_long(ptep, VPTE_M | VPTE_A);
941 vm_page_dirty(fs->m);
944 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
946 if ((vpte & VPTE_A) == 0) {
947 atomic_set_long(ptep, VPTE_A);
948 vm_page_dirty(fs->m);
952 vm_page_flag_set(fs->m, PG_REFERENCED);
953 vm_page_activate(fs->m);
954 vm_page_wakeup(fs->m);
956 cleanup_successful_fault(fs);
959 * Combine remaining address bits with the vpte.
961 /* JG how many bits from each? */
962 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
963 (*pindex & ((1L << vshift) - 1));
964 return (KERN_SUCCESS);
969 * This is the core of the vm_fault code.
971 * Do all operations required to fault-in (fs.first_object, pindex). Run
972 * through the shadow chain as necessary and do required COW or virtual
973 * copy operations. The caller has already fully resolved the vm_map_entry
974 * and, if appropriate, has created a copy-on-write layer. All we need to
975 * do is iterate the object chain.
977 * On failure (fs) is unlocked and deallocated and the caller may return or
978 * retry depending on the failure code. On success (fs) is NOT unlocked or
979 * deallocated, fs.m will contained a resolved, busied page, and fs.object
980 * will have an additional PIP count if it is not equal to fs.first_object.
982 * fs->first_object must be held on call.
986 vm_fault_object(struct faultstate *fs,
987 vm_pindex_t first_pindex, vm_prot_t fault_type)
989 vm_object_t next_object;
993 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
994 fs->prot = fs->first_prot;
995 fs->object = fs->first_object;
996 pindex = first_pindex;
998 vm_object_chain_acquire(fs->first_object);
999 vm_object_pip_add(fs->first_object, 1);
1002 * If a read fault occurs we try to make the page writable if
1003 * possible. There are three cases where we cannot make the
1004 * page mapping writable:
1006 * (1) The mapping is read-only or the VM object is read-only,
1007 * fs->prot above will simply not have VM_PROT_WRITE set.
1009 * (2) If the mapping is a virtual page table we need to be able
1010 * to detect writes so we can set VPTE_M in the virtual page
1013 * (3) If the VM page is read-only or copy-on-write, upgrading would
1014 * just result in an unnecessary COW fault.
1016 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1017 * causes adjustments to the 'M'odify bit to also turn off write
1018 * access to force a re-fault.
1020 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1021 if ((fault_type & VM_PROT_WRITE) == 0)
1022 fs->prot &= ~VM_PROT_WRITE;
1025 /* vm_object_hold(fs->object); implied b/c object == first_object */
1029 * If the object is dead, we stop here
1031 if (fs->object->flags & OBJ_DEAD) {
1032 vm_object_pip_wakeup(fs->first_object);
1033 vm_object_chain_release_all(fs->first_object,
1035 if (fs->object != fs->first_object)
1036 vm_object_drop(fs->object);
1037 unlock_and_deallocate(fs);
1038 return (KERN_PROTECTION_FAILURE);
1042 * See if the page is resident. Wait/Retry if the page is
1043 * busy (lots of stuff may have changed so we can't continue
1046 * We can theoretically allow the soft-busy case on a read
1047 * fault if the page is marked valid, but since such
1048 * pages are typically already pmap'd, putting that
1049 * special case in might be more effort then it is
1050 * worth. We cannot under any circumstances mess
1051 * around with a vm_page_t->busy page except, perhaps,
1054 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1057 vm_object_pip_wakeup(fs->first_object);
1058 vm_object_chain_release_all(fs->first_object,
1060 if (fs->object != fs->first_object)
1061 vm_object_drop(fs->object);
1063 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1064 mycpu->gd_cnt.v_intrans++;
1065 /*vm_object_deallocate(fs->first_object);*/
1066 /*fs->first_object = NULL;*/
1068 return (KERN_TRY_AGAIN);
1072 * The page is busied for us.
1074 * If reactivating a page from PQ_CACHE we may have
1077 int queue = fs->m->queue;
1078 vm_page_unqueue_nowakeup(fs->m);
1080 if ((queue - fs->m->pc) == PQ_CACHE &&
1081 vm_page_count_severe()) {
1082 vm_page_activate(fs->m);
1083 vm_page_wakeup(fs->m);
1085 vm_object_pip_wakeup(fs->first_object);
1086 vm_object_chain_release_all(fs->first_object,
1088 if (fs->object != fs->first_object)
1089 vm_object_drop(fs->object);
1090 unlock_and_deallocate(fs);
1092 return (KERN_TRY_AGAIN);
1096 * If it still isn't completely valid (readable),
1097 * or if a read-ahead-mark is set on the VM page,
1098 * jump to readrest, else we found the page and
1101 * We can release the spl once we have marked the
1104 if (fs->m->object != &kernel_object) {
1105 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1109 if (fs->m->flags & PG_RAM) {
1112 vm_page_flag_clear(fs->m, PG_RAM);
1116 break; /* break to PAGE HAS BEEN FOUND */
1120 * Page is not resident, If this is the search termination
1121 * or the pager might contain the page, allocate a new page.
1123 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1125 * If the page is beyond the object size we fail
1127 if (pindex >= fs->object->size) {
1128 vm_object_pip_wakeup(fs->first_object);
1129 vm_object_chain_release_all(fs->first_object,
1131 if (fs->object != fs->first_object)
1132 vm_object_drop(fs->object);
1133 unlock_and_deallocate(fs);
1134 return (KERN_PROTECTION_FAILURE);
1140 if (fs->didlimit == 0 && curproc != NULL) {
1143 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1145 vm_object_pip_wakeup(fs->first_object);
1146 vm_object_chain_release_all(
1147 fs->first_object, fs->object);
1148 if (fs->object != fs->first_object)
1149 vm_object_drop(fs->object);
1150 unlock_and_deallocate(fs);
1151 tsleep(curproc, 0, "vmrate", limticks);
1153 return (KERN_TRY_AGAIN);
1158 * Allocate a new page for this object/offset pair.
1161 if (!vm_page_count_severe()) {
1162 fs->m = vm_page_alloc(fs->object, pindex,
1163 ((fs->vp || fs->object->backing_object) ?
1165 VM_ALLOC_NORMAL | VM_ALLOC_ZERO));
1167 if (fs->m == NULL) {
1168 vm_object_pip_wakeup(fs->first_object);
1169 vm_object_chain_release_all(fs->first_object,
1171 if (fs->object != fs->first_object)
1172 vm_object_drop(fs->object);
1173 unlock_and_deallocate(fs);
1175 return (KERN_TRY_AGAIN);
1179 * Fall through to readrest. We have a new page which
1180 * will have to be paged (since m->valid will be 0).
1186 * We have found an invalid or partially valid page, a
1187 * page with a read-ahead mark which might be partially or
1188 * fully valid (and maybe dirty too), or we have allocated
1191 * Attempt to fault-in the page if there is a chance that the
1192 * pager has it, and potentially fault in additional pages
1195 * We are NOT in splvm here and if TRYPAGER is true then
1196 * fs.m will be non-NULL and will be PG_BUSY for us.
1201 u_char behavior = vm_map_entry_behavior(fs->entry);
1203 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1209 * If sequential access is detected then attempt
1210 * to deactivate/cache pages behind the scan to
1211 * prevent resource hogging.
1213 * Use of PG_RAM to detect sequential access
1214 * also simulates multi-zone sequential access
1215 * detection for free.
1217 * NOTE: Partially valid dirty pages cannot be
1218 * deactivated without causing NFS picemeal
1221 if ((fs->first_object->type != OBJT_DEVICE) &&
1222 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1223 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1224 (fs->m->flags & PG_RAM)))
1226 vm_pindex_t scan_pindex;
1227 int scan_count = 16;
1229 if (first_pindex < 16) {
1233 scan_pindex = first_pindex - 16;
1234 if (scan_pindex < 16)
1235 scan_count = scan_pindex;
1240 while (scan_count) {
1243 mt = vm_page_lookup(fs->first_object,
1247 if (vm_page_busy_try(mt, TRUE))
1250 if (mt->valid != VM_PAGE_BITS_ALL) {
1255 (PG_FICTITIOUS | PG_UNMANAGED)) ||
1262 vm_page_test_dirty(mt);
1266 vm_page_deactivate(mt);
1280 * Avoid deadlocking against the map when doing I/O.
1281 * fs.object and the page is PG_BUSY'd.
1283 * NOTE: Once unlocked, fs->entry can become stale
1284 * so this will NULL it out.
1286 * NOTE: fs->entry is invalid until we relock the
1287 * map and verify that the timestamp has not
1293 * Acquire the page data. We still hold a ref on
1294 * fs.object and the page has been PG_BUSY's.
1296 * The pager may replace the page (for example, in
1297 * order to enter a fictitious page into the
1298 * object). If it does so it is responsible for
1299 * cleaning up the passed page and properly setting
1300 * the new page PG_BUSY.
1302 * If we got here through a PG_RAM read-ahead
1303 * mark the page may be partially dirty and thus
1304 * not freeable. Don't bother checking to see
1305 * if the pager has the page because we can't free
1306 * it anyway. We have to depend on the get_page
1307 * operation filling in any gaps whether there is
1308 * backing store or not.
1310 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1312 if (rv == VM_PAGER_OK) {
1314 * Relookup in case pager changed page. Pager
1315 * is responsible for disposition of old page
1318 * XXX other code segments do relookups too.
1319 * It's a bad abstraction that needs to be
1322 fs->m = vm_page_lookup(fs->object, pindex);
1323 if (fs->m == NULL) {
1324 vm_object_pip_wakeup(fs->first_object);
1325 vm_object_chain_release_all(
1326 fs->first_object, fs->object);
1327 if (fs->object != fs->first_object)
1328 vm_object_drop(fs->object);
1329 unlock_and_deallocate(fs);
1330 return (KERN_TRY_AGAIN);
1334 break; /* break to PAGE HAS BEEN FOUND */
1338 * Remove the bogus page (which does not exist at this
1339 * object/offset); before doing so, we must get back
1340 * our object lock to preserve our invariant.
1342 * Also wake up any other process that may want to bring
1345 * If this is the top-level object, we must leave the
1346 * busy page to prevent another process from rushing
1347 * past us, and inserting the page in that object at
1348 * the same time that we are.
1350 if (rv == VM_PAGER_ERROR) {
1352 kprintf("vm_fault: pager read error, "
1357 kprintf("vm_fault: pager read error, "
1365 * Data outside the range of the pager or an I/O error
1367 * The page may have been wired during the pagein,
1368 * e.g. by the buffer cache, and cannot simply be
1369 * freed. Call vnode_pager_freepage() to deal with it.
1372 * XXX - the check for kernel_map is a kludge to work
1373 * around having the machine panic on a kernel space
1374 * fault w/ I/O error.
1376 if (((fs->map != &kernel_map) &&
1377 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1378 vnode_pager_freepage(fs->m);
1380 vm_object_pip_wakeup(fs->first_object);
1381 vm_object_chain_release_all(fs->first_object,
1383 if (fs->object != fs->first_object)
1384 vm_object_drop(fs->object);
1385 unlock_and_deallocate(fs);
1386 if (rv == VM_PAGER_ERROR)
1387 return (KERN_FAILURE);
1389 return (KERN_PROTECTION_FAILURE);
1392 if (fs->object != fs->first_object) {
1393 vnode_pager_freepage(fs->m);
1396 * XXX - we cannot just fall out at this
1397 * point, m has been freed and is invalid!
1403 * We get here if the object has a default pager (or unwiring)
1404 * or the pager doesn't have the page.
1406 if (fs->object == fs->first_object)
1407 fs->first_m = fs->m;
1410 * Move on to the next object. The chain lock should prevent
1411 * the backing_object from getting ripped out from under us.
1413 if ((next_object = fs->object->backing_object) != NULL) {
1414 vm_object_hold(next_object);
1415 vm_object_chain_acquire(next_object);
1416 KKASSERT(next_object == fs->object->backing_object);
1417 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1420 if (next_object == NULL) {
1422 * If there's no object left, fill the page in the top
1423 * object with zeros.
1425 if (fs->object != fs->first_object) {
1426 if (fs->first_object->backing_object !=
1428 vm_object_hold(fs->first_object->backing_object);
1430 vm_object_chain_release_all(
1431 fs->first_object->backing_object,
1433 if (fs->first_object->backing_object !=
1435 vm_object_drop(fs->first_object->backing_object);
1437 vm_object_pip_wakeup(fs->object);
1438 vm_object_drop(fs->object);
1439 fs->object = fs->first_object;
1440 pindex = first_pindex;
1441 fs->m = fs->first_m;
1446 * Zero the page if necessary and mark it valid.
1448 if ((fs->m->flags & PG_ZERO) == 0) {
1449 vm_page_zero_fill(fs->m);
1452 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1454 vm_page_flag_clear(fs->m, PG_ZERO);
1455 mycpu->gd_cnt.v_ozfod++;
1457 mycpu->gd_cnt.v_zfod++;
1458 fs->m->valid = VM_PAGE_BITS_ALL;
1459 break; /* break to PAGE HAS BEEN FOUND */
1461 if (fs->object != fs->first_object) {
1462 vm_object_pip_wakeup(fs->object);
1463 vm_object_lock_swap();
1464 vm_object_drop(fs->object);
1466 KASSERT(fs->object != next_object,
1467 ("object loop %p", next_object));
1468 fs->object = next_object;
1469 vm_object_pip_add(fs->object, 1);
1473 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1476 * object still held.
1478 * If the page is being written, but isn't already owned by the
1479 * top-level object, we have to copy it into a new page owned by the
1482 KASSERT((fs->m->flags & PG_BUSY) != 0,
1483 ("vm_fault: not busy after main loop"));
1485 if (fs->object != fs->first_object) {
1487 * We only really need to copy if we want to write it.
1489 if (fault_type & VM_PROT_WRITE) {
1491 * This allows pages to be virtually copied from a
1492 * backing_object into the first_object, where the
1493 * backing object has no other refs to it, and cannot
1494 * gain any more refs. Instead of a bcopy, we just
1495 * move the page from the backing object to the
1496 * first object. Note that we must mark the page
1497 * dirty in the first object so that it will go out
1498 * to swap when needed.
1502 * Map, if present, has not changed
1505 fs->map_generation == fs->map->timestamp) &&
1507 * Only one shadow object
1509 (fs->object->shadow_count == 1) &&
1511 * No COW refs, except us
1513 (fs->object->ref_count == 1) &&
1515 * No one else can look this object up
1517 (fs->object->handle == NULL) &&
1519 * No other ways to look the object up
1521 ((fs->object->type == OBJT_DEFAULT) ||
1522 (fs->object->type == OBJT_SWAP)) &&
1524 * We don't chase down the shadow chain
1526 (fs->object == fs->first_object->backing_object) &&
1529 * grab the lock if we need to
1531 (fs->lookup_still_valid ||
1533 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1536 * (first_m) and (m) are both busied. We have
1537 * move (m) into (first_m)'s object/pindex
1538 * in an atomic fashion, then free (first_m).
1540 * first_object is held so second remove
1541 * followed by the rename should wind
1542 * up being atomic. vm_page_free() might
1543 * block so we don't do it until after the
1546 fs->lookup_still_valid = 1;
1547 vm_page_protect(fs->first_m, VM_PROT_NONE);
1548 vm_page_remove(fs->first_m);
1549 vm_page_rename(fs->m, fs->first_object,
1551 vm_page_free(fs->first_m);
1552 fs->first_m = fs->m;
1554 mycpu->gd_cnt.v_cow_optim++;
1557 * Oh, well, lets copy it.
1559 vm_page_copy(fs->m, fs->first_m);
1560 vm_page_event(fs->m, VMEVENT_COW);
1565 * We no longer need the old page or object.
1571 * We intend to revert to first_object, undo the
1572 * chain lock through to that.
1574 if (fs->first_object->backing_object != fs->object)
1575 vm_object_hold(fs->first_object->backing_object);
1576 vm_object_chain_release_all(
1577 fs->first_object->backing_object,
1579 if (fs->first_object->backing_object != fs->object)
1580 vm_object_drop(fs->first_object->backing_object);
1583 * fs->object != fs->first_object due to above
1586 vm_object_pip_wakeup(fs->object);
1587 vm_object_drop(fs->object);
1590 * Only use the new page below...
1593 mycpu->gd_cnt.v_cow_faults++;
1594 fs->m = fs->first_m;
1595 fs->object = fs->first_object;
1596 pindex = first_pindex;
1599 * If it wasn't a write fault avoid having to copy
1600 * the page by mapping it read-only.
1602 fs->prot &= ~VM_PROT_WRITE;
1607 * Relock the map if necessary, then check the generation count.
1608 * relock_map() will update fs->timestamp to account for the
1609 * relocking if necessary.
1611 * If the count has changed after relocking then all sorts of
1612 * crap may have happened and we have to retry.
1614 * NOTE: The relock_map() can fail due to a deadlock against
1615 * the vm_page we are holding BUSY.
1617 if (fs->lookup_still_valid == FALSE && fs->map) {
1618 if (relock_map(fs) ||
1619 fs->map->timestamp != fs->map_generation) {
1621 vm_object_pip_wakeup(fs->first_object);
1622 vm_object_chain_release_all(fs->first_object,
1624 if (fs->object != fs->first_object)
1625 vm_object_drop(fs->object);
1626 unlock_and_deallocate(fs);
1627 return (KERN_TRY_AGAIN);
1632 * If the fault is a write, we know that this page is being
1633 * written NOW so dirty it explicitly to save on pmap_is_modified()
1636 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1637 * if the page is already dirty to prevent data written with
1638 * the expectation of being synced from not being synced.
1639 * Likewise if this entry does not request NOSYNC then make
1640 * sure the page isn't marked NOSYNC. Applications sharing
1641 * data should use the same flags to avoid ping ponging.
1643 * Also tell the backing pager, if any, that it should remove
1644 * any swap backing since the page is now dirty.
1646 if (fs->prot & VM_PROT_WRITE) {
1647 vm_object_set_writeable_dirty(fs->m->object);
1648 vm_set_nosync(fs->m, fs->entry);
1649 if (fs->fault_flags & VM_FAULT_DIRTY) {
1650 vm_page_dirty(fs->m);
1651 swap_pager_unswapped(fs->m);
1655 vm_object_pip_wakeup(fs->first_object);
1656 vm_object_chain_release_all(fs->first_object, fs->object);
1657 if (fs->object != fs->first_object)
1658 vm_object_drop(fs->object);
1661 * Page had better still be busy. We are still locked up and
1662 * fs->object will have another PIP reference if it is not equal
1663 * to fs->first_object.
1665 KASSERT(fs->m->flags & PG_BUSY,
1666 ("vm_fault: page %p not busy!", fs->m));
1669 * Sanity check: page must be completely valid or it is not fit to
1670 * map into user space. vm_pager_get_pages() ensures this.
1672 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1673 vm_page_zero_invalid(fs->m, TRUE);
1674 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1676 vm_page_flag_clear(fs->m, PG_ZERO);
1678 return (KERN_SUCCESS);
1682 * Wire down a range of virtual addresses in a map. The entry in question
1683 * should be marked in-transition and the map must be locked. We must
1684 * release the map temporarily while faulting-in the page to avoid a
1685 * deadlock. Note that the entry may be clipped while we are blocked but
1686 * will never be freed.
1691 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1693 boolean_t fictitious;
1702 lwkt_gettoken(&map->token);
1704 pmap = vm_map_pmap(map);
1705 start = entry->start;
1707 fictitious = entry->object.vm_object &&
1708 (entry->object.vm_object->type == OBJT_DEVICE);
1709 if (entry->eflags & MAP_ENTRY_KSTACK)
1715 * We simulate a fault to get the page and enter it in the physical
1718 for (va = start; va < end; va += PAGE_SIZE) {
1720 rv = vm_fault(map, va, VM_PROT_READ,
1721 VM_FAULT_USER_WIRE);
1723 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1724 VM_FAULT_CHANGE_WIRING);
1727 while (va > start) {
1729 if ((pa = pmap_extract(pmap, va)) == 0)
1731 pmap_change_wiring(pmap, va, FALSE);
1733 m = PHYS_TO_VM_PAGE(pa);
1734 vm_page_busy_wait(m, FALSE, "vmwrpg");
1735 vm_page_unwire(m, 1);
1745 lwkt_reltoken(&map->token);
1750 * Unwire a range of virtual addresses in a map. The map should be
1754 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1756 boolean_t fictitious;
1764 lwkt_gettoken(&map->token);
1766 pmap = vm_map_pmap(map);
1767 start = entry->start;
1769 fictitious = entry->object.vm_object &&
1770 (entry->object.vm_object->type == OBJT_DEVICE);
1771 if (entry->eflags & MAP_ENTRY_KSTACK)
1775 * Since the pages are wired down, we must be able to get their
1776 * mappings from the physical map system.
1778 for (va = start; va < end; va += PAGE_SIZE) {
1779 pa = pmap_extract(pmap, va);
1781 pmap_change_wiring(pmap, va, FALSE);
1783 m = PHYS_TO_VM_PAGE(pa);
1784 vm_page_busy_wait(m, FALSE, "vmwupg");
1785 vm_page_unwire(m, 1);
1790 lwkt_reltoken(&map->token);
1794 * Reduce the rate at which memory is allocated to a process based
1795 * on the perceived load on the VM system. As the load increases
1796 * the allocation burst rate goes down and the delay increases.
1798 * Rate limiting does not apply when faulting active or inactive
1799 * pages. When faulting 'cache' pages, rate limiting only applies
1800 * if the system currently has a severe page deficit.
1802 * XXX vm_pagesupply should be increased when a page is freed.
1804 * We sleep up to 1/10 of a second.
1807 vm_fault_ratelimit(struct vmspace *vmspace)
1809 if (vm_load_enable == 0)
1811 if (vmspace->vm_pagesupply > 0) {
1812 --vmspace->vm_pagesupply; /* SMP race ok */
1816 if (vm_load_debug) {
1817 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1819 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1820 curproc->p_pid, curproc->p_comm);
1823 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1824 return(vm_load * hz / 10000);
1828 * Copy all of the pages from a wired-down map entry to another.
1830 * The source and destination maps must be locked for write.
1831 * The source and destination maps token must be held
1832 * The source map entry must be wired down (or be a sharing map
1833 * entry corresponding to a main map entry that is wired down).
1835 * No other requirements.
1838 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1839 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1841 vm_object_t dst_object;
1842 vm_object_t src_object;
1843 vm_ooffset_t dst_offset;
1844 vm_ooffset_t src_offset;
1850 src_object = src_entry->object.vm_object;
1851 src_offset = src_entry->offset;
1854 * Create the top-level object for the destination entry. (Doesn't
1855 * actually shadow anything - we copy the pages directly.)
1857 vm_map_entry_allocate_object(dst_entry);
1858 dst_object = dst_entry->object.vm_object;
1860 prot = dst_entry->max_protection;
1863 * Loop through all of the pages in the entry's range, copying each
1864 * one from the source object (it should be there) to the destination
1867 for (vaddr = dst_entry->start, dst_offset = 0;
1868 vaddr < dst_entry->end;
1869 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1872 * Allocate a page in the destination object
1875 dst_m = vm_page_alloc(dst_object,
1876 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1877 if (dst_m == NULL) {
1880 } while (dst_m == NULL);
1883 * Find the page in the source object, and copy it in.
1884 * (Because the source is wired down, the page will be in
1887 src_m = vm_page_lookup(src_object,
1888 OFF_TO_IDX(dst_offset + src_offset));
1890 panic("vm_fault_copy_wired: page missing");
1892 vm_page_copy(src_m, dst_m);
1893 vm_page_event(src_m, VMEVENT_COW);
1896 * Enter it in the pmap...
1899 vm_page_flag_clear(dst_m, PG_ZERO);
1900 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1903 * Mark it no longer busy, and put it on the active list.
1905 vm_page_activate(dst_m);
1906 vm_page_wakeup(dst_m);
1913 * This routine checks around the requested page for other pages that
1914 * might be able to be faulted in. This routine brackets the viable
1915 * pages for the pages to be paged in.
1918 * m, rbehind, rahead
1921 * marray (array of vm_page_t), reqpage (index of requested page)
1924 * number of pages in marray
1927 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1928 vm_page_t *marray, int *reqpage)
1932 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1934 int cbehind, cahead;
1940 * we don't fault-ahead for device pager
1942 if (object->type == OBJT_DEVICE) {
1949 * if the requested page is not available, then give up now
1951 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1952 *reqpage = 0; /* not used by caller, fix compiler warn */
1956 if ((cbehind == 0) && (cahead == 0)) {
1962 if (rahead > cahead) {
1966 if (rbehind > cbehind) {
1971 * Do not do any readahead if we have insufficient free memory.
1973 * XXX code was broken disabled before and has instability
1974 * with this conditonal fixed, so shortcut for now.
1976 if (burst_fault == 0 || vm_page_count_severe()) {
1983 * scan backward for the read behind pages -- in memory
1985 * Assume that if the page is not found an interrupt will not
1986 * create it. Theoretically interrupts can only remove (busy)
1987 * pages, not create new associations.
1990 if (rbehind > pindex) {
1994 startpindex = pindex - rbehind;
1997 vm_object_hold(object);
1998 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1999 if (vm_page_lookup(object, tpindex - 1))
2004 while (tpindex < pindex) {
2005 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
2007 for (j = 0; j < i; j++) {
2008 vm_page_free(marray[j]);
2010 vm_object_drop(object);
2019 vm_object_drop(object);
2025 * Assign requested page
2032 * Scan forwards for read-ahead pages
2034 tpindex = pindex + 1;
2035 endpindex = tpindex + rahead;
2036 if (endpindex > object->size)
2037 endpindex = object->size;
2039 vm_object_hold(object);
2040 while (tpindex < endpindex) {
2041 if (vm_page_lookup(object, tpindex))
2043 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
2050 vm_object_drop(object);
2058 * vm_prefault() provides a quick way of clustering pagefaults into a
2059 * processes address space. It is a "cousin" of pmap_object_init_pt,
2060 * except it runs at page fault time instead of mmap time.
2062 * This code used to be per-platform pmap_prefault(). It is now
2063 * machine-independent and enhanced to also pre-fault zero-fill pages
2064 * (see vm.fast_fault) as well as make them writable, which greatly
2065 * reduces the number of page faults programs incur.
2067 * Application performance when pre-faulting zero-fill pages is heavily
2068 * dependent on the application. Very tiny applications like /bin/echo
2069 * lose a little performance while applications of any appreciable size
2070 * gain performance. Prefaulting multiple pages also reduces SMP
2071 * congestion and can improve SMP performance significantly.
2073 * NOTE! prot may allow writing but this only applies to the top level
2074 * object. If we wind up mapping a page extracted from a backing
2075 * object we have to make sure it is read-only.
2077 * NOTE! The caller has already handled any COW operations on the
2078 * vm_map_entry via the normal fault code. Do NOT call this
2079 * shortcut unless the normal fault code has run on this entry.
2081 * No other requirements.
2085 #define PAGEORDER_SIZE (PFBAK+PFFOR)
2087 static int vm_prefault_pageorder[] = {
2088 -PAGE_SIZE, PAGE_SIZE,
2089 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
2090 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
2091 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2095 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2096 * is not already dirty by other means. This will prevent passive
2097 * filesystem syncing as well as 'sync' from writing out the page.
2100 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2102 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2104 vm_page_flag_set(m, PG_NOSYNC);
2106 vm_page_flag_clear(m, PG_NOSYNC);
2111 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
2124 * We do not currently prefault mappings that use virtual page
2125 * tables. We do not prefault foreign pmaps.
2127 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2129 lp = curthread->td_lwp;
2130 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2133 starta = addra - PFBAK * PAGE_SIZE;
2134 if (starta < entry->start)
2135 starta = entry->start;
2136 else if (starta > addra)
2139 object = entry->object.vm_object;
2140 KKASSERT(object != NULL);
2141 vm_object_hold(object);
2142 KKASSERT(object == entry->object.vm_object);
2143 vm_object_chain_acquire(object);
2145 for (i = 0; i < PAGEORDER_SIZE; i++) {
2146 vm_object_t lobject;
2147 vm_object_t nobject;
2151 addr = addra + vm_prefault_pageorder[i];
2152 if (addr > addra + (PFFOR * PAGE_SIZE))
2155 if (addr < starta || addr >= entry->end)
2158 if (pmap_prefault_ok(pmap, addr) == 0)
2162 * Follow the VM object chain to obtain the page to be mapped
2165 * If we reach the terminal object without finding a page
2166 * and we determine it would be advantageous, then allocate
2167 * a zero-fill page for the base object. The base object
2168 * is guaranteed to be OBJT_DEFAULT for this case.
2170 * In order to not have to check the pager via *haspage*()
2171 * we stop if any non-default object is encountered. e.g.
2172 * a vnode or swap object would stop the loop.
2174 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2179 KKASSERT(lobject == entry->object.vm_object);
2180 /*vm_object_hold(lobject); implied */
2182 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2183 TRUE, &error)) == NULL) {
2184 if (lobject->type != OBJT_DEFAULT)
2186 if (lobject->backing_object == NULL) {
2187 if (vm_fast_fault == 0)
2189 if (vm_prefault_pageorder[i] < 0 ||
2190 (prot & VM_PROT_WRITE) == 0 ||
2191 vm_page_count_min(0)) {
2196 * NOTE: Allocated from base object
2198 m = vm_page_alloc(object, index,
2199 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
2201 if ((m->flags & PG_ZERO) == 0) {
2202 vm_page_zero_fill(m);
2205 pmap_page_assertzero(
2206 VM_PAGE_TO_PHYS(m));
2208 vm_page_flag_clear(m, PG_ZERO);
2209 mycpu->gd_cnt.v_ozfod++;
2211 mycpu->gd_cnt.v_zfod++;
2212 m->valid = VM_PAGE_BITS_ALL;
2215 /* lobject = object .. not needed */
2218 if (lobject->backing_object_offset & PAGE_MASK)
2220 nobject = lobject->backing_object;
2221 vm_object_hold(nobject);
2222 KKASSERT(nobject == lobject->backing_object);
2223 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2224 if (lobject != object) {
2225 vm_object_lock_swap();
2226 vm_object_drop(lobject);
2229 pprot &= ~VM_PROT_WRITE;
2230 vm_object_chain_acquire(lobject);
2234 * NOTE: A non-NULL (m) will be associated with lobject if
2235 * it was found there, otherwise it is probably a
2236 * zero-fill page associated with the base object.
2238 * Give-up if no page is available.
2241 if (lobject != object) {
2242 if (object->backing_object != lobject)
2243 vm_object_hold(object->backing_object);
2244 vm_object_chain_release_all(
2245 object->backing_object, lobject);
2246 if (object->backing_object != lobject)
2247 vm_object_drop(object->backing_object);
2248 vm_object_drop(lobject);
2254 * Do not conditionalize on PG_RAM. If pages are present in
2255 * the VM system we assume optimal caching. If caching is
2256 * not optimal the I/O gravy train will be restarted when we
2257 * hit an unavailable page. We do not want to try to restart
2258 * the gravy train now because we really don't know how much
2259 * of the object has been cached. The cost for restarting
2260 * the gravy train should be low (since accesses will likely
2261 * be I/O bound anyway).
2263 * The object must be marked dirty if we are mapping a
2264 * writable page. m->object is either lobject or object,
2265 * both of which are still held.
2267 if (pprot & VM_PROT_WRITE)
2268 vm_object_set_writeable_dirty(m->object);
2270 if (lobject != object) {
2271 if (object->backing_object != lobject)
2272 vm_object_hold(object->backing_object);
2273 vm_object_chain_release_all(object->backing_object,
2275 if (object->backing_object != lobject)
2276 vm_object_drop(object->backing_object);
2277 vm_object_drop(lobject);
2281 * Enter the page into the pmap if appropriate. If we had
2282 * allocated the page we have to place it on a queue. If not
2283 * we just have to make sure it isn't on the cache queue
2284 * (pages on the cache queue are not allowed to be mapped).
2287 if (pprot & VM_PROT_WRITE)
2288 vm_set_nosync(m, entry);
2289 pmap_enter(pmap, addr, m, pprot, 0);
2290 vm_page_deactivate(m);
2293 /* couldn't busy page, no wakeup */
2295 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2296 (m->flags & PG_FICTITIOUS) == 0) {
2298 * A fully valid page not undergoing soft I/O can
2299 * be immediately entered into the pmap.
2301 if ((m->queue - m->pc) == PQ_CACHE)
2302 vm_page_deactivate(m);
2303 if (pprot & VM_PROT_WRITE)
2304 vm_set_nosync(m, entry);
2305 pmap_enter(pmap, addr, m, pprot, 0);
2311 vm_object_chain_release(object);
2312 vm_object_drop(object);