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.
156 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
157 * requires relocking and then checking the timestamp.
159 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
160 * not have to update fs->map_generation here.
163 relock_map(struct faultstate *fs)
165 if (fs->lookup_still_valid == FALSE && fs->map) {
166 vm_map_lock_read(fs->map);
167 fs->lookup_still_valid = TRUE;
172 unlock_map(struct faultstate *fs)
174 if (fs->lookup_still_valid && fs->map) {
175 vm_map_lookup_done(fs->map, fs->entry, 0);
176 fs->lookup_still_valid = FALSE;
181 * Clean up after a successful call to vm_fault_object() so another call
182 * to vm_fault_object() can be made.
184 * The caller must hold vm_token.
187 _cleanup_successful_fault(struct faultstate *fs, int relock)
189 if (fs->object != fs->first_object) {
190 vm_page_free(fs->first_m);
191 vm_object_pip_wakeup(fs->object);
194 fs->object = fs->first_object;
195 if (relock && fs->lookup_still_valid == FALSE) {
197 vm_map_lock_read(fs->map);
198 fs->lookup_still_valid = TRUE;
203 * The caller must hold vm_token.
206 _unlock_things(struct faultstate *fs, int dealloc)
208 vm_object_pip_wakeup(fs->first_object);
209 _cleanup_successful_fault(fs, 0);
211 vm_object_deallocate(fs->first_object);
212 fs->first_object = NULL;
215 if (fs->vp != NULL) {
221 #define unlock_things(fs) _unlock_things(fs, 0)
222 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
223 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
228 * Determine if the pager for the current object *might* contain the page.
230 * We only need to try the pager if this is not a default object (default
231 * objects are zero-fill and have no real pager), and if we are not taking
232 * a wiring fault or if the FS entry is wired.
234 #define TRYPAGER(fs) \
235 (fs->object->type != OBJT_DEFAULT && \
236 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
241 * Handle a page fault occuring at the given address, requiring the given
242 * permissions, in the map specified. If successful, the page is inserted
243 * into the associated physical map.
245 * NOTE: The given address should be truncated to the proper page address.
247 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
248 * a standard error specifying why the fault is fatal is returned.
250 * The map in question must be referenced, and remains so.
251 * The caller may hold no locks.
252 * No other requirements.
255 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
258 vm_pindex_t first_pindex;
259 struct faultstate fs;
262 mycpu->gd_cnt.v_vm_faults++;
266 fs.fault_flags = fault_flags;
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)
302 return (KERN_FAILURE);
310 * If we are user-wiring a r/w segment, and it is COW, then
311 * we need to do the COW operation. Note that we don't
312 * currently COW RO sections now, because it is NOT desirable
313 * to COW .text. We simply keep .text from ever being COW'ed
314 * and take the heat that one cannot debug wired .text sections.
316 result = vm_map_lookup(&fs.map, vaddr,
317 VM_PROT_READ|VM_PROT_WRITE|
318 VM_PROT_OVERRIDE_WRITE,
319 &fs.entry, &fs.first_object,
320 &first_pindex, &fs.first_prot,
322 if (result != KERN_SUCCESS)
326 * If we don't COW now, on a user wire, the user will never
327 * be able to write to the mapping. If we don't make this
328 * restriction, the bookkeeping would be nearly impossible.
330 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
331 fs.entry->max_protection &= ~VM_PROT_WRITE;
335 * fs.map is read-locked
337 * Misc checks. Save the map generation number to detect races.
339 fs.map_generation = fs.map->timestamp;
341 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
342 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
343 panic("vm_fault: fault on nofault entry, addr: %p",
346 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
347 vaddr >= fs.entry->start &&
348 vaddr < fs.entry->start + PAGE_SIZE) {
349 panic("vm_fault: fault on stack guard, addr: %p",
355 * A system map entry may return a NULL object. No object means
356 * no pager means an unrecoverable kernel fault.
358 if (fs.first_object == NULL) {
359 panic("vm_fault: unrecoverable fault at %p in entry %p",
360 (void *)vaddr, fs.entry);
364 * Make a reference to this object to prevent its disposal while we
365 * are messing with it. Once we have the reference, the map is free
366 * to be diddled. Since objects reference their shadows (and copies),
367 * they will stay around as well.
369 * Bump the paging-in-progress count to prevent size changes (e.g.
370 * truncation operations) during I/O. This must be done after
371 * obtaining the vnode lock in order to avoid possible deadlocks.
373 * The vm_object must be held before manipulation.
375 lwkt_gettoken(&vm_token);
376 vm_object_hold(fs.first_object);
377 vm_object_reference(fs.first_object);
378 fs.vp = vnode_pager_lock(fs.first_object);
379 vm_object_pip_add(fs.first_object, 1);
380 vm_object_drop(fs.first_object);
381 lwkt_reltoken(&vm_token);
383 fs.lookup_still_valid = TRUE;
385 fs.object = fs.first_object; /* so unlock_and_deallocate works */
388 * If the entry is wired we cannot change the page protection.
391 fault_type = fs.first_prot;
394 * The page we want is at (first_object, first_pindex), but if the
395 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
396 * page table to figure out the actual pindex.
398 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
401 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
402 result = vm_fault_vpagetable(&fs, &first_pindex,
403 fs.entry->aux.master_pde,
405 if (result == KERN_TRY_AGAIN)
407 if (result != KERN_SUCCESS)
412 * Now we have the actual (object, pindex), fault in the page. If
413 * vm_fault_object() fails it will unlock and deallocate the FS
414 * data. If it succeeds everything remains locked and fs->object
415 * will have an additional PIP count if it is not equal to
418 * vm_fault_object will set fs->prot for the pmap operation. It is
419 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
420 * page can be safely written. However, it will force a read-only
421 * mapping for a read fault if the memory is managed by a virtual
425 result = vm_fault_object(&fs, first_pindex, fault_type);
427 if (result == KERN_TRY_AGAIN) {
428 /*lwkt_reltoken(&vm_token);*/
431 if (result != KERN_SUCCESS) {
432 /*lwkt_reltoken(&vm_token);*/
437 * On success vm_fault_object() does not unlock or deallocate, and fs.m
438 * will contain a busied page.
440 * Enter the page into the pmap and do pmap-related adjustments.
442 vm_page_flag_set(fs.m, PG_REFERENCED);
443 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
446 * Burst in a few more pages if possible. The fs.map should still
449 if (fault_flags & VM_FAULT_BURST) {
450 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
452 vm_prefault(fs.map->pmap, vaddr, fs.entry, fs.prot);
455 lwkt_gettoken(&vm_token);
458 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
459 KKASSERT(fs.m->flags & PG_BUSY);
462 * If the page is not wired down, then put it where the pageout daemon
465 * We do not really need to get vm_token here but since all the
466 * vm_*() calls have to doing it here improves efficiency.
468 /*lwkt_gettoken(&vm_token);*/
470 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
471 lwkt_reltoken(&vm_token); /* before wire activate does not */
475 vm_page_unwire(fs.m, 1);
477 vm_page_activate(fs.m);
478 lwkt_reltoken(&vm_token); /* before wire activate does not */
480 /*lwkt_reltoken(&vm_token); after wire/activate works */
482 if (curthread->td_lwp) {
484 curthread->td_lwp->lwp_ru.ru_majflt++;
486 curthread->td_lwp->lwp_ru.ru_minflt++;
491 * Unlock everything, and return
493 vm_page_wakeup(fs.m);
494 vm_object_deallocate(fs.first_object);
496 /*fs.first_object = NULL; */
497 /*lwkt_reltoken(&vm_token);*/
499 return (KERN_SUCCESS);
503 * Fault in the specified virtual address in the current process map,
504 * returning a held VM page or NULL. See vm_fault_page() for more
510 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
512 struct lwp *lp = curthread->td_lwp;
515 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
516 fault_type, VM_FAULT_NORMAL, errorp);
521 * Fault in the specified virtual address in the specified map, doing all
522 * necessary manipulation of the object store and all necessary I/O. Return
523 * a held VM page or NULL, and set *errorp. The related pmap is not
526 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
527 * and marked PG_REFERENCED as well.
529 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
530 * error will be returned.
535 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
536 int fault_flags, int *errorp)
538 vm_pindex_t first_pindex;
539 struct faultstate fs;
541 vm_prot_t orig_fault_type = fault_type;
543 mycpu->gd_cnt.v_vm_faults++;
547 fs.fault_flags = fault_flags;
548 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
552 * Find the vm_map_entry representing the backing store and resolve
553 * the top level object and page index. This may have the side
554 * effect of executing a copy-on-write on the map entry and/or
555 * creating a shadow object, but will not COW any actual VM pages.
557 * On success fs.map is left read-locked and various other fields
558 * are initialized but not otherwise referenced or locked.
560 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
561 * if the map entry is a virtual page table and also writable,
562 * so we can set the 'A'accessed bit in the virtual page table entry.
565 result = vm_map_lookup(&fs.map, vaddr, fault_type,
566 &fs.entry, &fs.first_object,
567 &first_pindex, &fs.first_prot, &fs.wired);
569 if (result != KERN_SUCCESS) {
575 * fs.map is read-locked
577 * Misc checks. Save the map generation number to detect races.
579 fs.map_generation = fs.map->timestamp;
581 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
582 panic("vm_fault: fault on nofault entry, addr: %lx",
587 * A system map entry may return a NULL object. No object means
588 * no pager means an unrecoverable kernel fault.
590 if (fs.first_object == NULL) {
591 panic("vm_fault: unrecoverable fault at %p in entry %p",
592 (void *)vaddr, fs.entry);
596 * Make a reference to this object to prevent its disposal while we
597 * are messing with it. Once we have the reference, the map is free
598 * to be diddled. Since objects reference their shadows (and copies),
599 * they will stay around as well.
601 * Bump the paging-in-progress count to prevent size changes (e.g.
602 * truncation operations) during I/O. This must be done after
603 * obtaining the vnode lock in order to avoid possible deadlocks.
605 * The vm_object must be held before manipulation.
607 lwkt_gettoken(&vm_token);
608 vm_object_hold(fs.first_object);
609 vm_object_reference(fs.first_object);
610 fs.vp = vnode_pager_lock(fs.first_object);
611 vm_object_pip_add(fs.first_object, 1);
612 vm_object_drop(fs.first_object);
613 lwkt_reltoken(&vm_token);
615 fs.lookup_still_valid = TRUE;
617 fs.object = fs.first_object; /* so unlock_and_deallocate works */
620 * If the entry is wired we cannot change the page protection.
623 fault_type = fs.first_prot;
626 * The page we want is at (first_object, first_pindex), but if the
627 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
628 * page table to figure out the actual pindex.
630 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
633 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
634 result = vm_fault_vpagetable(&fs, &first_pindex,
635 fs.entry->aux.master_pde,
637 if (result == KERN_TRY_AGAIN)
639 if (result != KERN_SUCCESS) {
646 * Now we have the actual (object, pindex), fault in the page. If
647 * vm_fault_object() fails it will unlock and deallocate the FS
648 * data. If it succeeds everything remains locked and fs->object
649 * will have an additinal PIP count if it is not equal to
652 result = vm_fault_object(&fs, first_pindex, fault_type);
654 if (result == KERN_TRY_AGAIN)
656 if (result != KERN_SUCCESS) {
661 if ((orig_fault_type & VM_PROT_WRITE) &&
662 (fs.prot & VM_PROT_WRITE) == 0) {
663 *errorp = KERN_PROTECTION_FAILURE;
664 unlock_and_deallocate(&fs);
669 * On success vm_fault_object() does not unlock or deallocate, and fs.m
670 * will contain a busied page.
675 * Return a held page. We are not doing any pmap manipulation so do
676 * not set PG_MAPPED. However, adjust the page flags according to
677 * the fault type because the caller may not use a managed pmapping
678 * (so we don't want to lose the fact that the page will be dirtied
679 * if a write fault was specified).
681 lwkt_gettoken(&vm_token);
683 if (fault_type & VM_PROT_WRITE)
687 * Update the pmap. We really only have to do this if a COW
688 * occured to replace the read-only page with the new page. For
689 * now just do it unconditionally. XXX
691 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
692 vm_page_flag_set(fs.m, PG_REFERENCED);
695 * Unbusy the page by activating it. It remains held and will not
698 vm_page_activate(fs.m);
700 if (curthread->td_lwp) {
702 curthread->td_lwp->lwp_ru.ru_majflt++;
704 curthread->td_lwp->lwp_ru.ru_minflt++;
709 * Unlock everything, and return the held page.
711 vm_page_wakeup(fs.m);
712 vm_object_deallocate(fs.first_object);
713 /*fs.first_object = NULL; */
714 lwkt_reltoken(&vm_token);
721 * Fault in the specified (object,offset), dirty the returned page as
722 * needed. If the requested fault_type cannot be done NULL and an
725 * A held (but not busied) page is returned.
730 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
731 vm_prot_t fault_type, int fault_flags, int *errorp)
734 vm_pindex_t first_pindex;
735 struct faultstate fs;
736 struct vm_map_entry entry;
738 bzero(&entry, sizeof(entry));
739 entry.object.vm_object = object;
740 entry.maptype = VM_MAPTYPE_NORMAL;
741 entry.protection = entry.max_protection = fault_type;
745 fs.fault_flags = fault_flags;
747 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
751 fs.first_object = object;
752 first_pindex = OFF_TO_IDX(offset);
754 fs.first_prot = fault_type;
756 /*fs.map_generation = 0; unused */
759 * Make a reference to this object to prevent its disposal while we
760 * are messing with it. Once we have the reference, the map is free
761 * to be diddled. Since objects reference their shadows (and copies),
762 * they will stay around as well.
764 * Bump the paging-in-progress count to prevent size changes (e.g.
765 * truncation operations) during I/O. This must be done after
766 * obtaining the vnode lock in order to avoid possible deadlocks.
768 lwkt_gettoken(&vm_token);
769 vm_object_hold(fs.first_object);
770 vm_object_reference(fs.first_object);
771 fs.vp = vnode_pager_lock(fs.first_object);
772 vm_object_pip_add(fs.first_object, 1);
773 vm_object_drop(fs.first_object);
774 lwkt_reltoken(&vm_token);
776 fs.lookup_still_valid = TRUE;
778 fs.object = fs.first_object; /* so unlock_and_deallocate works */
781 /* XXX future - ability to operate on VM object using vpagetable */
782 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
783 result = vm_fault_vpagetable(&fs, &first_pindex,
784 fs.entry->aux.master_pde,
786 if (result == KERN_TRY_AGAIN)
788 if (result != KERN_SUCCESS) {
796 * Now we have the actual (object, pindex), fault in the page. If
797 * vm_fault_object() fails it will unlock and deallocate the FS
798 * data. If it succeeds everything remains locked and fs->object
799 * will have an additinal PIP count if it is not equal to
802 result = vm_fault_object(&fs, first_pindex, fault_type);
804 if (result == KERN_TRY_AGAIN)
806 if (result != KERN_SUCCESS) {
811 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
812 *errorp = KERN_PROTECTION_FAILURE;
813 unlock_and_deallocate(&fs);
818 * On success vm_fault_object() does not unlock or deallocate, and fs.m
819 * will contain a busied page.
824 * Return a held page. We are not doing any pmap manipulation so do
825 * not set PG_MAPPED. However, adjust the page flags according to
826 * the fault type because the caller may not use a managed pmapping
827 * (so we don't want to lose the fact that the page will be dirtied
828 * if a write fault was specified).
830 lwkt_gettoken(&vm_token);
832 if (fault_type & VM_PROT_WRITE)
835 if (fault_flags & VM_FAULT_DIRTY)
837 if (fault_flags & VM_FAULT_UNSWAP)
838 swap_pager_unswapped(fs.m);
841 * Indicate that the page was accessed.
843 vm_page_flag_set(fs.m, PG_REFERENCED);
846 * Unbusy the page by activating it. It remains held and will not
849 vm_page_activate(fs.m);
851 if (curthread->td_lwp) {
853 mycpu->gd_cnt.v_vm_faults++;
854 curthread->td_lwp->lwp_ru.ru_majflt++;
856 curthread->td_lwp->lwp_ru.ru_minflt++;
861 * Unlock everything, and return the held page.
863 vm_page_wakeup(fs.m);
864 vm_object_deallocate(fs.first_object);
865 /*fs.first_object = NULL; */
866 lwkt_reltoken(&vm_token);
873 * Translate the virtual page number (first_pindex) that is relative
874 * to the address space into a logical page number that is relative to the
875 * backing object. Use the virtual page table pointed to by (vpte).
877 * This implements an N-level page table. Any level can terminate the
878 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
879 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
881 * No requirements (vm_token need not be held).
885 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
886 vpte_t vpte, int fault_type)
889 struct lwbuf lwb_cache;
890 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
891 int result = KERN_SUCCESS;
896 * We cannot proceed if the vpte is not valid, not readable
897 * for a read fault, or not writable for a write fault.
899 if ((vpte & VPTE_V) == 0) {
900 unlock_and_deallocate(fs);
901 return (KERN_FAILURE);
903 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
904 unlock_and_deallocate(fs);
905 return (KERN_FAILURE);
907 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
908 unlock_and_deallocate(fs);
909 return (KERN_FAILURE);
911 if ((vpte & VPTE_PS) || vshift == 0)
913 KKASSERT(vshift >= VPTE_PAGE_BITS);
916 * Get the page table page. Nominally we only read the page
917 * table, but since we are actively setting VPTE_M and VPTE_A,
918 * tell vm_fault_object() that we are writing it.
920 * There is currently no real need to optimize this.
922 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
923 VM_PROT_READ|VM_PROT_WRITE);
924 if (result != KERN_SUCCESS)
928 * Process the returned fs.m and look up the page table
929 * entry in the page table page.
931 vshift -= VPTE_PAGE_BITS;
932 lwb = lwbuf_alloc(fs->m, &lwb_cache);
933 ptep = ((vpte_t *)lwbuf_kva(lwb) +
934 ((*pindex >> vshift) & VPTE_PAGE_MASK));
938 * Page table write-back. If the vpte is valid for the
939 * requested operation, do a write-back to the page table.
941 * XXX VPTE_M is not set properly for page directory pages.
942 * It doesn't get set in the page directory if the page table
943 * is modified during a read access.
945 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
947 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
948 atomic_set_long(ptep, VPTE_M | VPTE_A);
949 vm_page_dirty(fs->m);
952 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
954 if ((vpte & VPTE_A) == 0) {
955 atomic_set_long(ptep, VPTE_A);
956 vm_page_dirty(fs->m);
960 vm_page_flag_set(fs->m, PG_REFERENCED);
961 vm_page_activate(fs->m);
962 vm_page_wakeup(fs->m);
964 cleanup_successful_fault(fs);
967 * Combine remaining address bits with the vpte.
969 /* JG how many bits from each? */
970 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
971 (*pindex & ((1L << vshift) - 1));
972 return (KERN_SUCCESS);
977 * This is the core of the vm_fault code.
979 * Do all operations required to fault-in (fs.first_object, pindex). Run
980 * through the shadow chain as necessary and do required COW or virtual
981 * copy operations. The caller has already fully resolved the vm_map_entry
982 * and, if appropriate, has created a copy-on-write layer. All we need to
983 * do is iterate the object chain.
985 * On failure (fs) is unlocked and deallocated and the caller may return or
986 * retry depending on the failure code. On success (fs) is NOT unlocked or
987 * deallocated, fs.m will contained a resolved, busied page, and fs.object
988 * will have an additional PIP count if it is not equal to fs.first_object.
994 vm_fault_object(struct faultstate *fs,
995 vm_pindex_t first_pindex, vm_prot_t fault_type)
997 vm_object_t next_object;
1000 fs->prot = fs->first_prot;
1001 fs->object = fs->first_object;
1002 pindex = first_pindex;
1005 * If a read fault occurs we try to make the page writable if
1006 * possible. There are three cases where we cannot make the
1007 * page mapping writable:
1009 * (1) The mapping is read-only or the VM object is read-only,
1010 * fs->prot above will simply not have VM_PROT_WRITE set.
1012 * (2) If the mapping is a virtual page table we need to be able
1013 * to detect writes so we can set VPTE_M in the virtual page
1016 * (3) If the VM page is read-only or copy-on-write, upgrading would
1017 * just result in an unnecessary COW fault.
1019 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1020 * causes adjustments to the 'M'odify bit to also turn off write
1021 * access to force a re-fault.
1023 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1024 if ((fault_type & VM_PROT_WRITE) == 0)
1025 fs->prot &= ~VM_PROT_WRITE;
1028 lwkt_gettoken(&vm_token);
1032 * If the object is dead, we stop here
1034 if (fs->object->flags & OBJ_DEAD) {
1035 unlock_and_deallocate(fs);
1036 lwkt_reltoken(&vm_token);
1037 return (KERN_PROTECTION_FAILURE);
1041 * See if the page is resident.
1043 fs->m = vm_page_lookup(fs->object, pindex);
1044 if (fs->m != NULL) {
1047 * Wait/Retry if the page is busy. We have to do this
1048 * if the page is busy via either PG_BUSY or
1049 * vm_page_t->busy because the vm_pager may be using
1050 * vm_page_t->busy for pageouts ( and even pageins if
1051 * it is the vnode pager ), and we could end up trying
1052 * to pagein and pageout the same page simultaneously.
1054 * We can theoretically allow the busy case on a read
1055 * fault if the page is marked valid, but since such
1056 * pages are typically already pmap'd, putting that
1057 * special case in might be more effort then it is
1058 * worth. We cannot under any circumstances mess
1059 * around with a vm_page_t->busy page except, perhaps,
1062 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
1064 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1065 mycpu->gd_cnt.v_intrans++;
1066 vm_object_deallocate(fs->first_object);
1067 fs->first_object = NULL;
1068 lwkt_reltoken(&vm_token);
1069 return (KERN_TRY_AGAIN);
1073 * If reactivating a page from PQ_CACHE we may have
1076 queue = fs->m->queue;
1077 vm_page_unqueue_nowakeup(fs->m);
1079 if ((queue - fs->m->pc) == PQ_CACHE &&
1080 vm_page_count_severe()) {
1081 vm_page_activate(fs->m);
1082 unlock_and_deallocate(fs);
1084 lwkt_reltoken(&vm_token);
1085 return (KERN_TRY_AGAIN);
1089 * Mark page busy for other processes, and the
1090 * pagedaemon. If it still isn't completely valid
1091 * (readable), or if a read-ahead-mark is set on
1092 * the VM page, jump to readrest, else we found the
1093 * page and can return.
1095 * We can release the spl once we have marked the
1098 vm_page_busy(fs->m);
1100 if (fs->m->object != &kernel_object) {
1101 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1105 if (fs->m->flags & PG_RAM) {
1108 vm_page_flag_clear(fs->m, PG_RAM);
1112 break; /* break to PAGE HAS BEEN FOUND */
1116 * Page is not resident, If this is the search termination
1117 * or the pager might contain the page, allocate a new page.
1119 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1121 * If the page is beyond the object size we fail
1123 if (pindex >= fs->object->size) {
1124 lwkt_reltoken(&vm_token);
1125 unlock_and_deallocate(fs);
1126 return (KERN_PROTECTION_FAILURE);
1132 if (fs->didlimit == 0 && curproc != NULL) {
1135 limticks = vm_fault_ratelimit(curproc->p_vmspace);
1137 lwkt_reltoken(&vm_token);
1138 unlock_and_deallocate(fs);
1139 tsleep(curproc, 0, "vmrate", limticks);
1141 return (KERN_TRY_AGAIN);
1146 * Allocate a new page for this object/offset pair.
1149 if (!vm_page_count_severe()) {
1150 fs->m = vm_page_alloc(fs->object, pindex,
1151 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
1153 if (fs->m == NULL) {
1154 lwkt_reltoken(&vm_token);
1155 unlock_and_deallocate(fs);
1157 return (KERN_TRY_AGAIN);
1163 * We have found an invalid or partially valid page, a
1164 * page with a read-ahead mark which might be partially or
1165 * fully valid (and maybe dirty too), or we have allocated
1168 * Attempt to fault-in the page if there is a chance that the
1169 * pager has it, and potentially fault in additional pages
1172 * We are NOT in splvm here and if TRYPAGER is true then
1173 * fs.m will be non-NULL and will be PG_BUSY for us.
1178 u_char behavior = vm_map_entry_behavior(fs->entry);
1180 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1186 * If sequential access is detected then attempt
1187 * to deactivate/cache pages behind the scan to
1188 * prevent resource hogging.
1190 * Use of PG_RAM to detect sequential access
1191 * also simulates multi-zone sequential access
1192 * detection for free.
1194 * NOTE: Partially valid dirty pages cannot be
1195 * deactivated without causing NFS picemeal
1198 if ((fs->first_object->type != OBJT_DEVICE) &&
1199 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1200 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1201 (fs->m->flags & PG_RAM)))
1203 vm_pindex_t scan_pindex;
1204 int scan_count = 16;
1206 if (first_pindex < 16) {
1210 scan_pindex = first_pindex - 16;
1211 if (scan_pindex < 16)
1212 scan_count = scan_pindex;
1217 while (scan_count) {
1220 mt = vm_page_lookup(fs->first_object,
1223 (mt->valid != VM_PAGE_BITS_ALL)) {
1227 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
1234 vm_page_test_dirty(mt);
1238 vm_page_deactivate(mt);
1252 * Avoid deadlocking against the map when doing I/O.
1253 * fs.object and the page is PG_BUSY'd.
1255 * NOTE: Once unlocked, fs->entry can become stale
1256 * so this will NULL it out.
1258 * NOTE: fs->entry is invalid until we relock the
1259 * map and verify that the timestamp has not
1265 * Acquire the page data. We still hold a ref on
1266 * fs.object and the page has been PG_BUSY's.
1268 * The pager may replace the page (for example, in
1269 * order to enter a fictitious page into the
1270 * object). If it does so it is responsible for
1271 * cleaning up the passed page and properly setting
1272 * the new page PG_BUSY.
1274 * If we got here through a PG_RAM read-ahead
1275 * mark the page may be partially dirty and thus
1276 * not freeable. Don't bother checking to see
1277 * if the pager has the page because we can't free
1278 * it anyway. We have to depend on the get_page
1279 * operation filling in any gaps whether there is
1280 * backing store or not.
1282 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1284 if (rv == VM_PAGER_OK) {
1286 * Relookup in case pager changed page. Pager
1287 * is responsible for disposition of old page
1290 * XXX other code segments do relookups too.
1291 * It's a bad abstraction that needs to be
1294 fs->m = vm_page_lookup(fs->object, pindex);
1295 if (fs->m == NULL) {
1296 lwkt_reltoken(&vm_token);
1297 unlock_and_deallocate(fs);
1298 return (KERN_TRY_AGAIN);
1302 break; /* break to PAGE HAS BEEN FOUND */
1306 * Remove the bogus page (which does not exist at this
1307 * object/offset); before doing so, we must get back
1308 * our object lock to preserve our invariant.
1310 * Also wake up any other process that may want to bring
1313 * If this is the top-level object, we must leave the
1314 * busy page to prevent another process from rushing
1315 * past us, and inserting the page in that object at
1316 * the same time that we are.
1318 if (rv == VM_PAGER_ERROR) {
1320 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
1322 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
1326 * Data outside the range of the pager or an I/O error
1328 * The page may have been wired during the pagein,
1329 * e.g. by the buffer cache, and cannot simply be
1330 * freed. Call vnode_pager_freepage() to deal with it.
1333 * XXX - the check for kernel_map is a kludge to work
1334 * around having the machine panic on a kernel space
1335 * fault w/ I/O error.
1337 if (((fs->map != &kernel_map) &&
1338 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1339 vnode_pager_freepage(fs->m);
1340 lwkt_reltoken(&vm_token);
1342 unlock_and_deallocate(fs);
1343 if (rv == VM_PAGER_ERROR)
1344 return (KERN_FAILURE);
1346 return (KERN_PROTECTION_FAILURE);
1349 if (fs->object != fs->first_object) {
1350 vnode_pager_freepage(fs->m);
1353 * XXX - we cannot just fall out at this
1354 * point, m has been freed and is invalid!
1360 * We get here if the object has a default pager (or unwiring)
1361 * or the pager doesn't have the page.
1363 if (fs->object == fs->first_object)
1364 fs->first_m = fs->m;
1367 * Move on to the next object. Lock the next object before
1368 * unlocking the current one.
1370 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1371 next_object = fs->object->backing_object;
1372 if (next_object == NULL) {
1374 * If there's no object left, fill the page in the top
1375 * object with zeros.
1377 if (fs->object != fs->first_object) {
1378 vm_object_pip_wakeup(fs->object);
1380 fs->object = fs->first_object;
1381 pindex = first_pindex;
1382 fs->m = fs->first_m;
1387 * Zero the page if necessary and mark it valid.
1389 if ((fs->m->flags & PG_ZERO) == 0) {
1390 vm_page_zero_fill(fs->m);
1393 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1395 vm_page_flag_clear(fs->m, PG_ZERO);
1396 mycpu->gd_cnt.v_ozfod++;
1398 mycpu->gd_cnt.v_zfod++;
1399 fs->m->valid = VM_PAGE_BITS_ALL;
1400 break; /* break to PAGE HAS BEEN FOUND */
1402 if (fs->object != fs->first_object) {
1403 vm_object_pip_wakeup(fs->object);
1405 KASSERT(fs->object != next_object,
1406 ("object loop %p", next_object));
1407 fs->object = next_object;
1408 vm_object_pip_add(fs->object, 1);
1412 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1415 * vm_token is still held
1417 * If the page is being written, but isn't already owned by the
1418 * top-level object, we have to copy it into a new page owned by the
1421 KASSERT((fs->m->flags & PG_BUSY) != 0,
1422 ("vm_fault: not busy after main loop"));
1424 if (fs->object != fs->first_object) {
1426 * We only really need to copy if we want to write it.
1428 if (fault_type & VM_PROT_WRITE) {
1430 * This allows pages to be virtually copied from a
1431 * backing_object into the first_object, where the
1432 * backing object has no other refs to it, and cannot
1433 * gain any more refs. Instead of a bcopy, we just
1434 * move the page from the backing object to the
1435 * first object. Note that we must mark the page
1436 * dirty in the first object so that it will go out
1437 * to swap when needed.
1441 * Map, if present, has not changed
1444 fs->map_generation == fs->map->timestamp) &&
1446 * Only one shadow object
1448 (fs->object->shadow_count == 1) &&
1450 * No COW refs, except us
1452 (fs->object->ref_count == 1) &&
1454 * No one else can look this object up
1456 (fs->object->handle == NULL) &&
1458 * No other ways to look the object up
1460 ((fs->object->type == OBJT_DEFAULT) ||
1461 (fs->object->type == OBJT_SWAP)) &&
1463 * We don't chase down the shadow chain
1465 (fs->object == fs->first_object->backing_object) &&
1468 * grab the lock if we need to
1470 (fs->lookup_still_valid ||
1472 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1475 fs->lookup_still_valid = 1;
1477 * get rid of the unnecessary page
1479 vm_page_protect(fs->first_m, VM_PROT_NONE);
1480 vm_page_free(fs->first_m);
1484 * grab the page and put it into the
1485 * process'es object. The page is
1486 * automatically made dirty.
1488 vm_page_rename(fs->m, fs->first_object, first_pindex);
1489 fs->first_m = fs->m;
1490 vm_page_busy(fs->first_m);
1492 mycpu->gd_cnt.v_cow_optim++;
1495 * Oh, well, lets copy it.
1497 vm_page_copy(fs->m, fs->first_m);
1498 vm_page_event(fs->m, VMEVENT_COW);
1503 * We no longer need the old page or object.
1509 * fs->object != fs->first_object due to above
1512 vm_object_pip_wakeup(fs->object);
1515 * Only use the new page below...
1518 mycpu->gd_cnt.v_cow_faults++;
1519 fs->m = fs->first_m;
1520 fs->object = fs->first_object;
1521 pindex = first_pindex;
1524 * If it wasn't a write fault avoid having to copy
1525 * the page by mapping it read-only.
1527 fs->prot &= ~VM_PROT_WRITE;
1532 * Relock the map if necessary, then check the generation count.
1533 * relock_map() will update fs->timestamp to account for the
1534 * relocking if necessary.
1536 * If the count has changed after relocking then all sorts of
1537 * crap may have happened and we have to retry.
1539 if (fs->lookup_still_valid == FALSE && fs->map) {
1541 if (fs->map->timestamp != fs->map_generation) {
1543 lwkt_reltoken(&vm_token);
1544 unlock_and_deallocate(fs);
1545 return (KERN_TRY_AGAIN);
1550 * If the fault is a write, we know that this page is being
1551 * written NOW so dirty it explicitly to save on pmap_is_modified()
1554 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1555 * if the page is already dirty to prevent data written with
1556 * the expectation of being synced from not being synced.
1557 * Likewise if this entry does not request NOSYNC then make
1558 * sure the page isn't marked NOSYNC. Applications sharing
1559 * data should use the same flags to avoid ping ponging.
1561 * Also tell the backing pager, if any, that it should remove
1562 * any swap backing since the page is now dirty.
1564 if (fs->prot & VM_PROT_WRITE) {
1565 vm_object_set_writeable_dirty(fs->m->object);
1566 vm_set_nosync(fs->m, fs->entry);
1567 if (fs->fault_flags & VM_FAULT_DIRTY) {
1568 vm_page_dirty(fs->m);
1569 swap_pager_unswapped(fs->m);
1573 lwkt_reltoken(&vm_token);
1576 * Page had better still be busy. We are still locked up and
1577 * fs->object will have another PIP reference if it is not equal
1578 * to fs->first_object.
1580 KASSERT(fs->m->flags & PG_BUSY,
1581 ("vm_fault: page %p not busy!", fs->m));
1584 * Sanity check: page must be completely valid or it is not fit to
1585 * map into user space. vm_pager_get_pages() ensures this.
1587 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1588 vm_page_zero_invalid(fs->m, TRUE);
1589 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1591 vm_page_flag_clear(fs->m, PG_ZERO);
1593 return (KERN_SUCCESS);
1597 * Wire down a range of virtual addresses in a map. The entry in question
1598 * should be marked in-transition and the map must be locked. We must
1599 * release the map temporarily while faulting-in the page to avoid a
1600 * deadlock. Note that the entry may be clipped while we are blocked but
1601 * will never be freed.
1606 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1608 boolean_t fictitious;
1616 pmap = vm_map_pmap(map);
1617 start = entry->start;
1619 fictitious = entry->object.vm_object &&
1620 (entry->object.vm_object->type == OBJT_DEVICE);
1621 if (entry->eflags & MAP_ENTRY_KSTACK)
1623 lwkt_gettoken(&vm_token);
1628 * We simulate a fault to get the page and enter it in the physical
1631 for (va = start; va < end; va += PAGE_SIZE) {
1633 rv = vm_fault(map, va, VM_PROT_READ,
1634 VM_FAULT_USER_WIRE);
1636 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1637 VM_FAULT_CHANGE_WIRING);
1640 while (va > start) {
1642 if ((pa = pmap_extract(pmap, va)) == 0)
1644 pmap_change_wiring(pmap, va, FALSE);
1646 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1649 lwkt_reltoken(&vm_token);
1654 lwkt_reltoken(&vm_token);
1655 return (KERN_SUCCESS);
1659 * Unwire a range of virtual addresses in a map. The map should be
1663 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1665 boolean_t fictitious;
1672 pmap = vm_map_pmap(map);
1673 start = entry->start;
1675 fictitious = entry->object.vm_object &&
1676 (entry->object.vm_object->type == OBJT_DEVICE);
1677 if (entry->eflags & MAP_ENTRY_KSTACK)
1681 * Since the pages are wired down, we must be able to get their
1682 * mappings from the physical map system.
1684 lwkt_gettoken(&vm_token);
1685 for (va = start; va < end; va += PAGE_SIZE) {
1686 pa = pmap_extract(pmap, va);
1688 pmap_change_wiring(pmap, va, FALSE);
1690 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1693 lwkt_reltoken(&vm_token);
1697 * Reduce the rate at which memory is allocated to a process based
1698 * on the perceived load on the VM system. As the load increases
1699 * the allocation burst rate goes down and the delay increases.
1701 * Rate limiting does not apply when faulting active or inactive
1702 * pages. When faulting 'cache' pages, rate limiting only applies
1703 * if the system currently has a severe page deficit.
1705 * XXX vm_pagesupply should be increased when a page is freed.
1707 * We sleep up to 1/10 of a second.
1710 vm_fault_ratelimit(struct vmspace *vmspace)
1712 if (vm_load_enable == 0)
1714 if (vmspace->vm_pagesupply > 0) {
1715 --vmspace->vm_pagesupply; /* SMP race ok */
1719 if (vm_load_debug) {
1720 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1722 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1723 curproc->p_pid, curproc->p_comm);
1726 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1727 return(vm_load * hz / 10000);
1731 * Copy all of the pages from a wired-down map entry to another.
1733 * The source and destination maps must be locked for write.
1734 * The source map entry must be wired down (or be a sharing map
1735 * entry corresponding to a main map entry that is wired down).
1737 * No other requirements.
1740 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1741 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1743 vm_object_t dst_object;
1744 vm_object_t src_object;
1745 vm_ooffset_t dst_offset;
1746 vm_ooffset_t src_offset;
1756 src_object = src_entry->object.vm_object;
1757 src_offset = src_entry->offset;
1760 * Create the top-level object for the destination entry. (Doesn't
1761 * actually shadow anything - we copy the pages directly.)
1763 vm_map_entry_allocate_object(dst_entry);
1764 dst_object = dst_entry->object.vm_object;
1766 prot = dst_entry->max_protection;
1769 * Loop through all of the pages in the entry's range, copying each
1770 * one from the source object (it should be there) to the destination
1773 for (vaddr = dst_entry->start, dst_offset = 0;
1774 vaddr < dst_entry->end;
1775 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1778 * Allocate a page in the destination object
1781 dst_m = vm_page_alloc(dst_object,
1782 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1783 if (dst_m == NULL) {
1786 } while (dst_m == NULL);
1789 * Find the page in the source object, and copy it in.
1790 * (Because the source is wired down, the page will be in
1793 src_m = vm_page_lookup(src_object,
1794 OFF_TO_IDX(dst_offset + src_offset));
1796 panic("vm_fault_copy_wired: page missing");
1798 vm_page_copy(src_m, dst_m);
1799 vm_page_event(src_m, VMEVENT_COW);
1802 * Enter it in the pmap...
1805 vm_page_flag_clear(dst_m, PG_ZERO);
1806 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1809 * Mark it no longer busy, and put it on the active list.
1811 vm_page_activate(dst_m);
1812 vm_page_wakeup(dst_m);
1819 * This routine checks around the requested page for other pages that
1820 * might be able to be faulted in. This routine brackets the viable
1821 * pages for the pages to be paged in.
1824 * m, rbehind, rahead
1827 * marray (array of vm_page_t), reqpage (index of requested page)
1830 * number of pages in marray
1833 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1834 vm_page_t *marray, int *reqpage)
1838 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1840 int cbehind, cahead;
1846 * we don't fault-ahead for device pager
1848 if (object->type == OBJT_DEVICE) {
1855 * if the requested page is not available, then give up now
1857 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1858 *reqpage = 0; /* not used by caller, fix compiler warn */
1862 if ((cbehind == 0) && (cahead == 0)) {
1868 if (rahead > cahead) {
1872 if (rbehind > cbehind) {
1877 * Do not do any readahead if we have insufficient free memory.
1879 * XXX code was broken disabled before and has instability
1880 * with this conditonal fixed, so shortcut for now.
1882 if (burst_fault == 0 || vm_page_count_severe()) {
1889 * scan backward for the read behind pages -- in memory
1891 * Assume that if the page is not found an interrupt will not
1892 * create it. Theoretically interrupts can only remove (busy)
1893 * pages, not create new associations.
1896 if (rbehind > pindex) {
1900 startpindex = pindex - rbehind;
1903 lwkt_gettoken(&vm_token);
1904 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
1905 if (vm_page_lookup(object, tpindex - 1))
1910 while (tpindex < pindex) {
1911 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1913 lwkt_reltoken(&vm_token);
1914 for (j = 0; j < i; j++) {
1915 vm_page_free(marray[j]);
1925 lwkt_reltoken(&vm_token);
1931 * Assign requested page
1938 * Scan forwards for read-ahead pages
1940 tpindex = pindex + 1;
1941 endpindex = tpindex + rahead;
1942 if (endpindex > object->size)
1943 endpindex = object->size;
1945 lwkt_gettoken(&vm_token);
1946 while (tpindex < endpindex) {
1947 if (vm_page_lookup(object, tpindex))
1949 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM);
1956 lwkt_reltoken(&vm_token);
1964 * vm_prefault() provides a quick way of clustering pagefaults into a
1965 * processes address space. It is a "cousin" of pmap_object_init_pt,
1966 * except it runs at page fault time instead of mmap time.
1968 * This code used to be per-platform pmap_prefault(). It is now
1969 * machine-independent and enhanced to also pre-fault zero-fill pages
1970 * (see vm.fast_fault) as well as make them writable, which greatly
1971 * reduces the number of page faults programs incur.
1973 * Application performance when pre-faulting zero-fill pages is heavily
1974 * dependent on the application. Very tiny applications like /bin/echo
1975 * lose a little performance while applications of any appreciable size
1976 * gain performance. Prefaulting multiple pages also reduces SMP
1977 * congestion and can improve SMP performance significantly.
1979 * NOTE! prot may allow writing but this only applies to the top level
1980 * object. If we wind up mapping a page extracted from a backing
1981 * object we have to make sure it is read-only.
1983 * NOTE! The caller has already handled any COW operations on the
1984 * vm_map_entry via the normal fault code. Do NOT call this
1985 * shortcut unless the normal fault code has run on this entry.
1987 * No other requirements.
1991 #define PAGEORDER_SIZE (PFBAK+PFFOR)
1993 static int vm_prefault_pageorder[] = {
1994 -PAGE_SIZE, PAGE_SIZE,
1995 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
1996 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
1997 -4 * PAGE_SIZE, 4 * PAGE_SIZE
2001 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2002 * is not already dirty by other means. This will prevent passive
2003 * filesystem syncing as well as 'sync' from writing out the page.
2006 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2008 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2010 vm_page_flag_set(m, PG_NOSYNC);
2012 vm_page_flag_clear(m, PG_NOSYNC);
2017 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot)
2030 * We do not currently prefault mappings that use virtual page
2031 * tables. We do not prefault foreign pmaps.
2033 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2035 lp = curthread->td_lwp;
2036 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2039 lwkt_gettoken(&vm_token);
2041 object = entry->object.vm_object;
2042 KKASSERT(object != NULL);
2043 vm_object_hold(object);
2045 starta = addra - PFBAK * PAGE_SIZE;
2046 if (starta < entry->start)
2047 starta = entry->start;
2048 else if (starta > addra)
2051 KKASSERT(object == entry->object.vm_object);
2052 for (i = 0; i < PAGEORDER_SIZE; i++) {
2053 vm_object_t lobject;
2054 vm_object_t nobject;
2057 addr = addra + vm_prefault_pageorder[i];
2058 if (addr > addra + (PFFOR * PAGE_SIZE))
2061 if (addr < starta || addr >= entry->end)
2064 if (pmap_prefault_ok(pmap, addr) == 0)
2068 * Follow the VM object chain to obtain the page to be mapped
2071 * If we reach the terminal object without finding a page
2072 * and we determine it would be advantageous, then allocate
2073 * a zero-fill page for the base object. The base object
2074 * is guaranteed to be OBJT_DEFAULT for this case.
2076 * In order to not have to check the pager via *haspage*()
2077 * we stop if any non-default object is encountered. e.g.
2078 * a vnode or swap object would stop the loop.
2080 * XXX It is unclear whether hold chaining is sufficient
2081 * to maintain the validity of the backing object chain.
2083 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2088 KKASSERT(lobject == entry->object.vm_object);
2089 vm_object_hold(lobject);
2091 while ((m = vm_page_lookup(lobject, pindex)) == NULL) {
2092 if (lobject->type != OBJT_DEFAULT)
2094 if (lobject->backing_object == NULL) {
2095 if (vm_fast_fault == 0)
2097 if (vm_prefault_pageorder[i] < 0 ||
2098 (prot & VM_PROT_WRITE) == 0 ||
2099 vm_page_count_min(0)) {
2103 /* NOTE: allocated from base object */
2104 m = vm_page_alloc(object, index,
2105 VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
2107 if ((m->flags & PG_ZERO) == 0) {
2108 vm_page_zero_fill(m);
2111 pmap_page_assertzero(
2112 VM_PAGE_TO_PHYS(m));
2114 vm_page_flag_clear(m, PG_ZERO);
2115 mycpu->gd_cnt.v_ozfod++;
2117 mycpu->gd_cnt.v_zfod++;
2118 m->valid = VM_PAGE_BITS_ALL;
2121 /* lobject = object .. not needed */
2124 if (lobject->backing_object_offset & PAGE_MASK)
2126 while ((nobject = lobject->backing_object) != NULL) {
2127 vm_object_hold(nobject);
2128 if (nobject == lobject->backing_object) {
2130 lobject->backing_object_offset >>
2132 vm_object_lock_swap();
2133 vm_object_drop(lobject);
2137 vm_object_drop(nobject);
2139 if (nobject == NULL) {
2140 kprintf("vm_prefault: Warning, backing object "
2141 "race averted lobject %p\n",
2145 pprot &= ~VM_PROT_WRITE;
2147 vm_object_drop(lobject);
2150 * NOTE: lobject now invalid (if we did a zero-fill we didn't
2151 * bother assigning lobject = object).
2153 * Give-up if the page is not available.
2159 * Do not conditionalize on PG_RAM. If pages are present in
2160 * the VM system we assume optimal caching. If caching is
2161 * not optimal the I/O gravy train will be restarted when we
2162 * hit an unavailable page. We do not want to try to restart
2163 * the gravy train now because we really don't know how much
2164 * of the object has been cached. The cost for restarting
2165 * the gravy train should be low (since accesses will likely
2166 * be I/O bound anyway).
2168 * The object must be marked dirty if we are mapping a
2171 if (pprot & VM_PROT_WRITE)
2172 vm_object_set_writeable_dirty(m->object);
2175 * Enter the page into the pmap if appropriate. If we had
2176 * allocated the page we have to place it on a queue. If not
2177 * we just have to make sure it isn't on the cache queue
2178 * (pages on the cache queue are not allowed to be mapped).
2181 if (pprot & VM_PROT_WRITE)
2182 vm_set_nosync(m, entry);
2183 pmap_enter(pmap, addr, m, pprot, 0);
2184 vm_page_deactivate(m);
2187 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2189 (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
2191 * A fully valid page not undergoing soft I/O can
2192 * be immediately entered into the pmap.
2195 if ((m->queue - m->pc) == PQ_CACHE) {
2196 vm_page_deactivate(m);
2198 if (pprot & VM_PROT_WRITE)
2199 vm_set_nosync(m, entry);
2200 pmap_enter(pmap, addr, m, pprot, 0);
2204 vm_object_drop(object);
2205 lwkt_reltoken(&vm_token);