2 * Copyright (c) 2003-2014 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1991, 1993
37 * The Regents of the University of California. All rights reserved.
38 * Copyright (c) 1994 John S. Dyson
39 * All rights reserved.
40 * Copyright (c) 1994 David Greenman
41 * All rights reserved.
44 * This code is derived from software contributed to Berkeley by
45 * The Mach Operating System project at Carnegie-Mellon University.
47 * Redistribution and use in source and binary forms, with or without
48 * modification, are permitted provided that the following conditions
50 * 1. Redistributions of source code must retain the above copyright
51 * notice, this list of conditions and the following disclaimer.
52 * 2. Redistributions in binary form must reproduce the above copyright
53 * notice, this list of conditions and the following disclaimer in the
54 * documentation and/or other materials provided with the distribution.
55 * 3. Neither the name of the University nor the names of its contributors
56 * may be used to endorse or promote products derived from this software
57 * without specific prior written permission.
59 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
74 * All rights reserved.
76 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
78 * Permission to use, copy, modify and distribute this software and
79 * its documentation is hereby granted, provided that both the copyright
80 * notice and this permission notice appear in all copies of the
81 * software, derivative works or modified versions, and any portions
82 * thereof, and that both notices appear in supporting documentation.
84 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
85 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
86 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
88 * Carnegie Mellon requests users of this software to return to
90 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
91 * School of Computer Science
92 * Carnegie Mellon University
93 * Pittsburgh PA 15213-3890
95 * any improvements or extensions that they make and grant Carnegie the
96 * rights to redistribute these changes.
100 * Page fault handling module.
103 #include <sys/param.h>
104 #include <sys/systm.h>
105 #include <sys/kernel.h>
106 #include <sys/proc.h>
107 #include <sys/vnode.h>
108 #include <sys/resourcevar.h>
109 #include <sys/vmmeter.h>
110 #include <sys/vkernel.h>
111 #include <sys/lock.h>
112 #include <sys/sysctl.h>
114 #include <cpu/lwbuf.h>
117 #include <vm/vm_param.h>
119 #include <vm/vm_map.h>
120 #include <vm/vm_object.h>
121 #include <vm/vm_page.h>
122 #include <vm/vm_pageout.h>
123 #include <vm/vm_kern.h>
124 #include <vm/vm_pager.h>
125 #include <vm/vnode_pager.h>
126 #include <vm/vm_extern.h>
128 #include <sys/thread2.h>
129 #include <vm/vm_page2.h>
137 vm_object_t first_object
;
138 vm_prot_t first_prot
;
140 vm_map_entry_t entry
;
141 int lookup_still_valid
;
151 static int debug_fault
= 0;
152 SYSCTL_INT(_vm
, OID_AUTO
, debug_fault
, CTLFLAG_RW
, &debug_fault
, 0, "");
153 static int debug_cluster
= 0;
154 SYSCTL_INT(_vm
, OID_AUTO
, debug_cluster
, CTLFLAG_RW
, &debug_cluster
, 0, "");
155 static int virtual_copy_enable
= 1;
156 SYSCTL_INT(_vm
, OID_AUTO
, virtual_copy_enable
, CTLFLAG_RW
,
157 &virtual_copy_enable
, 0, "");
158 int vm_shared_fault
= 1;
159 TUNABLE_INT("vm.shared_fault", &vm_shared_fault
);
160 SYSCTL_INT(_vm
, OID_AUTO
, shared_fault
, CTLFLAG_RW
,
161 &vm_shared_fault
, 0, "Allow shared token on vm_object");
163 static int vm_fault_object(struct faultstate
*, vm_pindex_t
, vm_prot_t
, int);
164 static int vm_fault_vpagetable(struct faultstate
*, vm_pindex_t
*,
167 static int vm_fault_additional_pages (vm_page_t
, int, int, vm_page_t
*, int *);
169 static void vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
);
170 static void vm_prefault(pmap_t pmap
, vm_offset_t addra
,
171 vm_map_entry_t entry
, int prot
, int fault_flags
);
172 static void vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
173 vm_map_entry_t entry
, int prot
, int fault_flags
);
176 release_page(struct faultstate
*fs
)
178 vm_page_deactivate(fs
->m
);
179 vm_page_wakeup(fs
->m
);
184 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
185 * requires relocking and then checking the timestamp.
187 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
188 * not have to update fs->map_generation here.
190 * NOTE: This function can fail due to a deadlock against the caller's
191 * holding of a vm_page BUSY.
194 relock_map(struct faultstate
*fs
)
198 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
199 error
= vm_map_lock_read_to(fs
->map
);
201 fs
->lookup_still_valid
= TRUE
;
209 unlock_map(struct faultstate
*fs
)
211 if (fs
->lookup_still_valid
&& fs
->map
) {
212 vm_map_lookup_done(fs
->map
, fs
->entry
, 0);
213 fs
->lookup_still_valid
= FALSE
;
218 * Clean up after a successful call to vm_fault_object() so another call
219 * to vm_fault_object() can be made.
222 _cleanup_successful_fault(struct faultstate
*fs
, int relock
)
225 * We allocated a junk page for a COW operation that did
226 * not occur, the page must be freed.
228 if (fs
->object
!= fs
->first_object
) {
229 KKASSERT(fs
->first_shared
== 0);
230 vm_page_free(fs
->first_m
);
231 vm_object_pip_wakeup(fs
->object
);
238 fs
->object
= fs
->first_object
;
239 if (relock
&& fs
->lookup_still_valid
== FALSE
) {
241 vm_map_lock_read(fs
->map
);
242 fs
->lookup_still_valid
= TRUE
;
247 _unlock_things(struct faultstate
*fs
, int dealloc
)
249 _cleanup_successful_fault(fs
, 0);
251 /*vm_object_deallocate(fs->first_object);*/
252 /*fs->first_object = NULL; drop used later on */
255 if (fs
->vp
!= NULL
) {
261 #define unlock_things(fs) _unlock_things(fs, 0)
262 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
263 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
266 * Virtual copy tests. Used by the fault code to determine if a
267 * page can be moved from an orphan vm_object into its shadow
268 * instead of copying its contents.
271 virtual_copy_test(struct faultstate
*fs
)
274 * Must be holding exclusive locks
276 if (fs
->first_shared
|| fs
->shared
|| virtual_copy_enable
== 0)
280 * Map, if present, has not changed
282 if (fs
->map
&& fs
->map_generation
!= fs
->map
->timestamp
)
286 * Only one shadow object
288 if (fs
->object
->shadow_count
!= 1)
292 * No COW refs, except us
294 if (fs
->object
->ref_count
!= 1)
298 * No one else can look this object up
300 if (fs
->object
->handle
!= NULL
)
304 * No other ways to look the object up
306 if (fs
->object
->type
!= OBJT_DEFAULT
&&
307 fs
->object
->type
!= OBJT_SWAP
)
311 * We don't chase down the shadow chain
313 if (fs
->object
!= fs
->first_object
->backing_object
)
320 virtual_copy_ok(struct faultstate
*fs
)
322 if (virtual_copy_test(fs
)) {
324 * Grab the lock and re-test changeable items.
326 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
327 if (lockmgr(&fs
->map
->lock
, LK_EXCLUSIVE
|LK_NOWAIT
))
329 fs
->lookup_still_valid
= TRUE
;
330 if (virtual_copy_test(fs
)) {
331 fs
->map_generation
= ++fs
->map
->timestamp
;
334 fs
->lookup_still_valid
= FALSE
;
335 lockmgr(&fs
->map
->lock
, LK_RELEASE
);
344 * Determine if the pager for the current object *might* contain the page.
346 * We only need to try the pager if this is not a default object (default
347 * objects are zero-fill and have no real pager), and if we are not taking
348 * a wiring fault or if the FS entry is wired.
350 #define TRYPAGER(fs) \
351 (fs->object->type != OBJT_DEFAULT && \
352 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
357 * Handle a page fault occuring at the given address, requiring the given
358 * permissions, in the map specified. If successful, the page is inserted
359 * into the associated physical map.
361 * NOTE: The given address should be truncated to the proper page address.
363 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
364 * a standard error specifying why the fault is fatal is returned.
366 * The map in question must be referenced, and remains so.
367 * The caller may hold no locks.
368 * No other requirements.
371 vm_fault(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
, int fault_flags
)
374 vm_pindex_t first_pindex
;
375 struct faultstate fs
;
379 struct vm_map_ilock ilock
;
385 inherit_prot
= fault_type
& VM_PROT_NOSYNC
;
387 fs
.fault_flags
= fault_flags
;
389 fs
.shared
= vm_shared_fault
;
390 fs
.first_shared
= vm_shared_fault
;
394 * vm_map interactions
397 if ((lp
= td
->td_lwp
) != NULL
)
398 lp
->lwp_flags
|= LWP_PAGING
;
402 * Find the vm_map_entry representing the backing store and resolve
403 * the top level object and page index. This may have the side
404 * effect of executing a copy-on-write on the map entry,
405 * creating a shadow object, or splitting an anonymous entry for
406 * performance, but will not COW any actual VM pages.
408 * On success fs.map is left read-locked and various other fields
409 * are initialized but not otherwise referenced or locked.
411 * NOTE! vm_map_lookup will try to upgrade the fault_type to
412 * VM_FAULT_WRITE if the map entry is a virtual page table
413 * and also writable, so we can set the 'A'accessed bit in
414 * the virtual page table entry.
417 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
418 &fs
.entry
, &fs
.first_object
,
419 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
422 * If the lookup failed or the map protections are incompatible,
423 * the fault generally fails.
425 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
426 * tried to do a COW fault.
428 * If the caller is trying to do a user wiring we have more work
431 if (result
!= KERN_SUCCESS
) {
432 if (result
== KERN_FAILURE_NOFAULT
) {
433 result
= KERN_FAILURE
;
436 if (result
!= KERN_PROTECTION_FAILURE
||
437 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
439 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
440 map
!= &kernel_map
&& curproc
!= NULL
) {
441 result
= vm_map_growstack(map
, vaddr
);
442 if (result
== KERN_SUCCESS
) {
447 result
= KERN_FAILURE
;
453 * If we are user-wiring a r/w segment, and it is COW, then
454 * we need to do the COW operation. Note that we don't
455 * currently COW RO sections now, because it is NOT desirable
456 * to COW .text. We simply keep .text from ever being COW'ed
457 * and take the heat that one cannot debug wired .text sections.
459 result
= vm_map_lookup(&fs
.map
, vaddr
,
460 VM_PROT_READ
|VM_PROT_WRITE
|
461 VM_PROT_OVERRIDE_WRITE
,
462 &fs
.entry
, &fs
.first_object
,
463 &first_pindex
, &fs
.first_prot
,
465 if (result
!= KERN_SUCCESS
) {
466 /* could also be KERN_FAILURE_NOFAULT */
467 result
= KERN_FAILURE
;
472 * If we don't COW now, on a user wire, the user will never
473 * be able to write to the mapping. If we don't make this
474 * restriction, the bookkeeping would be nearly impossible.
476 * XXX We have a shared lock, this will have a MP race but
477 * I don't see how it can hurt anything.
479 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
480 atomic_clear_char(&fs
.entry
->max_protection
,
486 * fs.map is read-locked
488 * Misc checks. Save the map generation number to detect races.
490 fs
.map_generation
= fs
.map
->timestamp
;
491 fs
.lookup_still_valid
= TRUE
;
493 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
494 fs
.prot
= fs
.first_prot
; /* default (used by uksmap) */
496 if (fs
.entry
->eflags
& (MAP_ENTRY_NOFAULT
| MAP_ENTRY_KSTACK
)) {
497 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
498 panic("vm_fault: fault on nofault entry, addr: %p",
501 if ((fs
.entry
->eflags
& MAP_ENTRY_KSTACK
) &&
502 vaddr
>= fs
.entry
->start
&&
503 vaddr
< fs
.entry
->start
+ PAGE_SIZE
) {
504 panic("vm_fault: fault on stack guard, addr: %p",
510 * A user-kernel shared map has no VM object and bypasses
511 * everything. We execute the uksmap function with a temporary
512 * fictitious vm_page. The address is directly mapped with no
515 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
516 struct vm_page fakem
;
518 bzero(&fakem
, sizeof(fakem
));
519 fakem
.pindex
= first_pindex
;
520 fakem
.flags
= PG_FICTITIOUS
| PG_UNMANAGED
;
521 fakem
.busy_count
= PBUSY_LOCKED
;
522 fakem
.valid
= VM_PAGE_BITS_ALL
;
523 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
524 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
525 result
= KERN_FAILURE
;
529 pmap_enter(fs
.map
->pmap
, vaddr
, &fakem
, fs
.prot
| inherit_prot
,
535 * A system map entry may return a NULL object. No object means
536 * no pager means an unrecoverable kernel fault.
538 if (fs
.first_object
== NULL
) {
539 panic("vm_fault: unrecoverable fault at %p in entry %p",
540 (void *)vaddr
, fs
.entry
);
544 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
547 * Unfortunately a deadlock can occur if we are forced to page-in
548 * from swap, but diving all the way into the vm_pager_get_page()
549 * function to find out is too much. Just check the object type.
551 * The deadlock is a CAM deadlock on a busy VM page when trying
552 * to finish an I/O if another process gets stuck in
553 * vop_helper_read_shortcut() due to a swap fault.
555 if ((td
->td_flags
& TDF_NOFAULT
) &&
557 fs
.first_object
->type
== OBJT_VNODE
||
558 fs
.first_object
->type
== OBJT_SWAP
||
559 fs
.first_object
->backing_object
)) {
560 result
= KERN_FAILURE
;
566 * If the entry is wired we cannot change the page protection.
569 fault_type
= fs
.first_prot
;
572 * We generally want to avoid unnecessary exclusive modes on backing
573 * and terminal objects because this can seriously interfere with
574 * heavily fork()'d processes (particularly /bin/sh scripts).
576 * However, we also want to avoid unnecessary retries due to needed
577 * shared->exclusive promotion for common faults. Exclusive mode is
578 * always needed if any page insertion, rename, or free occurs in an
579 * object (and also indirectly if any I/O is done).
581 * The main issue here is going to be fs.first_shared. If the
582 * first_object has a backing object which isn't shadowed and the
583 * process is single-threaded we might as well use an exclusive
584 * lock/chain right off the bat.
586 if (fs
.first_shared
&& fs
.first_object
->backing_object
&&
587 LIST_EMPTY(&fs
.first_object
->shadow_head
) &&
588 td
->td_proc
&& td
->td_proc
->p_nthreads
== 1) {
593 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
594 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
595 * we can try shared first.
597 if (fault_flags
& VM_FAULT_UNSWAP
) {
602 * Obtain a top-level object lock, shared or exclusive depending
603 * on fs.first_shared. If a shared lock winds up being insufficient
604 * we will retry with an exclusive lock.
606 * The vnode pager lock is always shared.
609 vm_object_hold_shared(fs
.first_object
);
611 vm_object_hold(fs
.first_object
);
613 fs
.vp
= vnode_pager_lock(fs
.first_object
);
616 * The page we want is at (first_object, first_pindex), but if the
617 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
618 * page table to figure out the actual pindex.
620 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
624 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
625 vm_map_interlock(fs
.map
, &ilock
, vaddr
, vaddr
+ PAGE_SIZE
);
627 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
628 fs
.entry
->aux
.master_pde
,
630 if (result
== KERN_TRY_AGAIN
) {
631 vm_map_deinterlock(fs
.map
, &ilock
);
632 vm_object_drop(fs
.first_object
);
636 if (result
!= KERN_SUCCESS
) {
637 vm_map_deinterlock(fs
.map
, &ilock
);
643 * Now we have the actual (object, pindex), fault in the page. If
644 * vm_fault_object() fails it will unlock and deallocate the FS
645 * data. If it succeeds everything remains locked and fs->object
646 * will have an additional PIP count if it is not equal to
649 * vm_fault_object will set fs->prot for the pmap operation. It is
650 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
651 * page can be safely written. However, it will force a read-only
652 * mapping for a read fault if the memory is managed by a virtual
655 * If the fault code uses the shared object lock shortcut
656 * we must not try to burst (we can't allocate VM pages).
658 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
660 if (debug_fault
> 0) {
662 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
663 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
664 result
, (intmax_t)vaddr
, fault_type
, fault_flags
,
665 fs
.m
, fs
.prot
, fs
.wired
, fs
.entry
);
668 if (result
== KERN_TRY_AGAIN
) {
670 vm_map_deinterlock(fs
.map
, &ilock
);
671 vm_object_drop(fs
.first_object
);
675 if (result
!= KERN_SUCCESS
) {
677 vm_map_deinterlock(fs
.map
, &ilock
);
682 * On success vm_fault_object() does not unlock or deallocate, and fs.m
683 * will contain a busied page.
685 * Enter the page into the pmap and do pmap-related adjustments.
687 KKASSERT(fs
.lookup_still_valid
== TRUE
);
688 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
689 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
| inherit_prot
,
693 vm_map_deinterlock(fs
.map
, &ilock
);
695 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
696 KKASSERT(fs
.m
->busy_count
& PBUSY_LOCKED
);
699 * If the page is not wired down, then put it where the pageout daemon
702 if (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) {
706 vm_page_unwire(fs
.m
, 1);
708 vm_page_activate(fs
.m
);
710 vm_page_wakeup(fs
.m
);
713 * Burst in a few more pages if possible. The fs.map should still
714 * be locked. To avoid interlocking against a vnode->getblk
715 * operation we had to be sure to unbusy our primary vm_page above
718 * A normal burst can continue down backing store, only execute
719 * if we are holding an exclusive lock, otherwise the exclusive
720 * locks the burst code gets might cause excessive SMP collisions.
722 * A quick burst can be utilized when there is no backing object
723 * (i.e. a shared file mmap).
725 if ((fault_flags
& VM_FAULT_BURST
) &&
726 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) == 0 &&
728 if (fs
.first_shared
== 0 && fs
.shared
== 0) {
729 vm_prefault(fs
.map
->pmap
, vaddr
,
730 fs
.entry
, fs
.prot
, fault_flags
);
732 vm_prefault_quick(fs
.map
->pmap
, vaddr
,
733 fs
.entry
, fs
.prot
, fault_flags
);
738 mycpu
->gd_cnt
.v_vm_faults
++;
740 ++td
->td_lwp
->lwp_ru
.ru_minflt
;
743 * Unlock everything, and return
749 td
->td_lwp
->lwp_ru
.ru_majflt
++;
751 td
->td_lwp
->lwp_ru
.ru_minflt
++;
755 /*vm_object_deallocate(fs.first_object);*/
757 /*fs.first_object = NULL; must still drop later */
759 result
= KERN_SUCCESS
;
762 vm_object_drop(fs
.first_object
);
765 lp
->lwp_flags
&= ~LWP_PAGING
;
767 #if !defined(NO_SWAPPING)
769 * Check the process RSS limit and force deactivation and
770 * (asynchronous) paging if necessary. This is a complex operation,
771 * only do it for direct user-mode faults, for now.
773 * To reduce overhead implement approximately a ~16MB hysteresis.
776 if ((fault_flags
& VM_FAULT_USERMODE
) && lp
&&
777 p
->p_limit
&& map
->pmap
&& vm_pageout_memuse_mode
>= 1 &&
778 map
!= &kernel_map
) {
782 limit
= OFF_TO_IDX(qmin(p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
,
783 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
));
784 size
= pmap_resident_tlnw_count(map
->pmap
);
785 if (limit
>= 0 && size
> 4096 && size
- 4096 >= limit
) {
786 vm_pageout_map_deactivate_pages(map
, limit
);
795 * Fault in the specified virtual address in the current process map,
796 * returning a held VM page or NULL. See vm_fault_page() for more
802 vm_fault_page_quick(vm_offset_t va
, vm_prot_t fault_type
,
803 int *errorp
, int *busyp
)
805 struct lwp
*lp
= curthread
->td_lwp
;
808 m
= vm_fault_page(&lp
->lwp_vmspace
->vm_map
, va
,
809 fault_type
, VM_FAULT_NORMAL
,
815 * Fault in the specified virtual address in the specified map, doing all
816 * necessary manipulation of the object store and all necessary I/O. Return
817 * a held VM page or NULL, and set *errorp. The related pmap is not
820 * If busyp is not NULL then *busyp will be set to TRUE if this routine
821 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
822 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is
823 * NULL the returned page is only held.
825 * If the caller has no intention of writing to the page's contents, busyp
826 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
827 * without busying the page.
829 * The returned page will also be marked PG_REFERENCED.
831 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
832 * error will be returned.
837 vm_fault_page(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
,
838 int fault_flags
, int *errorp
, int *busyp
)
840 vm_pindex_t first_pindex
;
841 struct faultstate fs
;
845 vm_prot_t orig_fault_type
= fault_type
;
849 fs
.fault_flags
= fault_flags
;
850 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
853 * Dive the pmap (concurrency possible). If we find the
854 * appropriate page we can terminate early and quickly.
856 * This works great for normal programs but will always return
857 * NULL for host lookups of vkernel maps in VMM mode.
859 * NOTE: pmap_fault_page_quick() might not busy the page. If
860 * VM_PROT_WRITE or VM_PROT_OVERRIDE_WRITE is set in
861 * fault_type and pmap_fault_page_quick() returns non-NULL,
862 * it will safely dirty the returned vm_page_t for us. We
863 * cannot safely dirty it here (it might not be busy).
865 fs
.m
= pmap_fault_page_quick(map
->pmap
, vaddr
, fault_type
, busyp
);
872 * Otherwise take a concurrency hit and do a formal page
876 fs
.shared
= vm_shared_fault
;
877 fs
.first_shared
= vm_shared_fault
;
881 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
882 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
883 * we can try shared first.
885 if (fault_flags
& VM_FAULT_UNSWAP
) {
891 * Find the vm_map_entry representing the backing store and resolve
892 * the top level object and page index. This may have the side
893 * effect of executing a copy-on-write on the map entry and/or
894 * creating a shadow object, but will not COW any actual VM pages.
896 * On success fs.map is left read-locked and various other fields
897 * are initialized but not otherwise referenced or locked.
899 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
900 * if the map entry is a virtual page table and also writable,
901 * so we can set the 'A'accessed bit in the virtual page table
905 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
906 &fs
.entry
, &fs
.first_object
,
907 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
909 if (result
!= KERN_SUCCESS
) {
910 if (result
== KERN_FAILURE_NOFAULT
) {
911 *errorp
= KERN_FAILURE
;
915 if (result
!= KERN_PROTECTION_FAILURE
||
916 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
918 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
919 map
!= &kernel_map
&& curproc
!= NULL
) {
920 result
= vm_map_growstack(map
, vaddr
);
921 if (result
== KERN_SUCCESS
) {
926 result
= KERN_FAILURE
;
934 * If we are user-wiring a r/w segment, and it is COW, then
935 * we need to do the COW operation. Note that we don't
936 * currently COW RO sections now, because it is NOT desirable
937 * to COW .text. We simply keep .text from ever being COW'ed
938 * and take the heat that one cannot debug wired .text sections.
940 result
= vm_map_lookup(&fs
.map
, vaddr
,
941 VM_PROT_READ
|VM_PROT_WRITE
|
942 VM_PROT_OVERRIDE_WRITE
,
943 &fs
.entry
, &fs
.first_object
,
944 &first_pindex
, &fs
.first_prot
,
946 if (result
!= KERN_SUCCESS
) {
947 /* could also be KERN_FAILURE_NOFAULT */
948 *errorp
= KERN_FAILURE
;
954 * If we don't COW now, on a user wire, the user will never
955 * be able to write to the mapping. If we don't make this
956 * restriction, the bookkeeping would be nearly impossible.
958 * XXX We have a shared lock, this will have a MP race but
959 * I don't see how it can hurt anything.
961 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
962 atomic_clear_char(&fs
.entry
->max_protection
,
968 * fs.map is read-locked
970 * Misc checks. Save the map generation number to detect races.
972 fs
.map_generation
= fs
.map
->timestamp
;
973 fs
.lookup_still_valid
= TRUE
;
975 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
977 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
978 panic("vm_fault: fault on nofault entry, addr: %lx",
983 * A user-kernel shared map has no VM object and bypasses
984 * everything. We execute the uksmap function with a temporary
985 * fictitious vm_page. The address is directly mapped with no
988 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
989 struct vm_page fakem
;
991 bzero(&fakem
, sizeof(fakem
));
992 fakem
.pindex
= first_pindex
;
993 fakem
.flags
= PG_FICTITIOUS
| PG_UNMANAGED
;
994 fakem
.busy_count
= PBUSY_LOCKED
;
995 fakem
.valid
= VM_PAGE_BITS_ALL
;
996 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
997 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
998 *errorp
= KERN_FAILURE
;
1003 fs
.m
= PHYS_TO_VM_PAGE(fakem
.phys_addr
);
1006 *busyp
= 0; /* don't need to busy R or W */
1014 * A system map entry may return a NULL object. No object means
1015 * no pager means an unrecoverable kernel fault.
1017 if (fs
.first_object
== NULL
) {
1018 panic("vm_fault: unrecoverable fault at %p in entry %p",
1019 (void *)vaddr
, fs
.entry
);
1023 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
1026 * Unfortunately a deadlock can occur if we are forced to page-in
1027 * from swap, but diving all the way into the vm_pager_get_page()
1028 * function to find out is too much. Just check the object type.
1030 if ((curthread
->td_flags
& TDF_NOFAULT
) &&
1032 fs
.first_object
->type
== OBJT_VNODE
||
1033 fs
.first_object
->type
== OBJT_SWAP
||
1034 fs
.first_object
->backing_object
)) {
1035 *errorp
= KERN_FAILURE
;
1042 * If the entry is wired we cannot change the page protection.
1045 fault_type
= fs
.first_prot
;
1048 * Make a reference to this object to prevent its disposal while we
1049 * are messing with it. Once we have the reference, the map is free
1050 * to be diddled. Since objects reference their shadows (and copies),
1051 * they will stay around as well.
1053 * The reference should also prevent an unexpected collapse of the
1054 * parent that might move pages from the current object into the
1055 * parent unexpectedly, resulting in corruption.
1057 * Bump the paging-in-progress count to prevent size changes (e.g.
1058 * truncation operations) during I/O. This must be done after
1059 * obtaining the vnode lock in order to avoid possible deadlocks.
1061 if (fs
.first_shared
)
1062 vm_object_hold_shared(fs
.first_object
);
1064 vm_object_hold(fs
.first_object
);
1066 fs
.vp
= vnode_pager_lock(fs
.first_object
); /* shared */
1069 * The page we want is at (first_object, first_pindex), but if the
1070 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
1071 * page table to figure out the actual pindex.
1073 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
1076 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1077 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
1078 fs
.entry
->aux
.master_pde
,
1080 if (result
== KERN_TRY_AGAIN
) {
1081 vm_object_drop(fs
.first_object
);
1085 if (result
!= KERN_SUCCESS
) {
1093 * Now we have the actual (object, pindex), fault in the page. If
1094 * vm_fault_object() fails it will unlock and deallocate the FS
1095 * data. If it succeeds everything remains locked and fs->object
1096 * will have an additinal PIP count if it is not equal to
1100 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
1102 if (result
== KERN_TRY_AGAIN
) {
1103 vm_object_drop(fs
.first_object
);
1107 if (result
!= KERN_SUCCESS
) {
1113 if ((orig_fault_type
& VM_PROT_WRITE
) &&
1114 (fs
.prot
& VM_PROT_WRITE
) == 0) {
1115 *errorp
= KERN_PROTECTION_FAILURE
;
1116 unlock_and_deallocate(&fs
);
1122 * DO NOT UPDATE THE PMAP!!! This function may be called for
1123 * a pmap unrelated to the current process pmap, in which case
1124 * the current cpu core will not be listed in the pmap's pm_active
1125 * mask. Thus invalidation interlocks will fail to work properly.
1127 * (for example, 'ps' uses procfs to read program arguments from
1128 * each process's stack).
1130 * In addition to the above this function will be called to acquire
1131 * a page that might already be faulted in, re-faulting it
1132 * continuously is a waste of time.
1134 * XXX could this have been the cause of our random seg-fault
1135 * issues? procfs accesses user stacks.
1137 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1139 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
, fs
.wired
, NULL
);
1140 mycpu
->gd_cnt
.v_vm_faults
++;
1141 if (curthread
->td_lwp
)
1142 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
1146 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1147 * will contain a busied page. So we must unlock here after having
1148 * messed with the pmap.
1153 * Return a held page. We are not doing any pmap manipulation so do
1154 * not set PG_MAPPED. However, adjust the page flags according to
1155 * the fault type because the caller may not use a managed pmapping
1156 * (so we don't want to lose the fact that the page will be dirtied
1157 * if a write fault was specified).
1159 if (fault_type
& VM_PROT_WRITE
)
1160 vm_page_dirty(fs
.m
);
1161 vm_page_activate(fs
.m
);
1163 if (curthread
->td_lwp
) {
1165 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1167 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1172 * Unlock everything, and return the held or busied page.
1175 if (fault_type
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
)) {
1176 vm_page_dirty(fs
.m
);
1181 vm_page_wakeup(fs
.m
);
1185 vm_page_wakeup(fs
.m
);
1187 /*vm_object_deallocate(fs.first_object);*/
1188 /*fs.first_object = NULL; */
1192 if (fs
.first_object
)
1193 vm_object_drop(fs
.first_object
);
1199 * Fault in the specified (object,offset), dirty the returned page as
1200 * needed. If the requested fault_type cannot be done NULL and an
1201 * error is returned.
1203 * A held (but not busied) page is returned.
1205 * The passed in object must be held as specified by the shared
1209 vm_fault_object_page(vm_object_t object
, vm_ooffset_t offset
,
1210 vm_prot_t fault_type
, int fault_flags
,
1211 int *sharedp
, int *errorp
)
1214 vm_pindex_t first_pindex
;
1215 struct faultstate fs
;
1216 struct vm_map_entry entry
;
1218 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
1219 bzero(&entry
, sizeof(entry
));
1220 entry
.object
.vm_object
= object
;
1221 entry
.maptype
= VM_MAPTYPE_NORMAL
;
1222 entry
.protection
= entry
.max_protection
= fault_type
;
1225 fs
.fault_flags
= fault_flags
;
1227 fs
.shared
= vm_shared_fault
;
1228 fs
.first_shared
= *sharedp
;
1230 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
1233 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1234 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
1235 * we can try shared first.
1237 if (fs
.first_shared
&& (fault_flags
& VM_FAULT_UNSWAP
)) {
1238 fs
.first_shared
= 0;
1239 vm_object_upgrade(object
);
1243 * Retry loop as needed (typically for shared->exclusive transitions)
1246 *sharedp
= fs
.first_shared
;
1247 first_pindex
= OFF_TO_IDX(offset
);
1248 fs
.first_object
= object
;
1250 fs
.first_prot
= fault_type
;
1252 /*fs.map_generation = 0; unused */
1255 * Make a reference to this object to prevent its disposal while we
1256 * are messing with it. Once we have the reference, the map is free
1257 * to be diddled. Since objects reference their shadows (and copies),
1258 * they will stay around as well.
1260 * The reference should also prevent an unexpected collapse of the
1261 * parent that might move pages from the current object into the
1262 * parent unexpectedly, resulting in corruption.
1264 * Bump the paging-in-progress count to prevent size changes (e.g.
1265 * truncation operations) during I/O. This must be done after
1266 * obtaining the vnode lock in order to avoid possible deadlocks.
1269 fs
.vp
= vnode_pager_lock(fs
.first_object
);
1271 fs
.lookup_still_valid
= TRUE
;
1273 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
1276 /* XXX future - ability to operate on VM object using vpagetable */
1277 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1278 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
1279 fs
.entry
->aux
.master_pde
,
1281 if (result
== KERN_TRY_AGAIN
) {
1282 if (fs
.first_shared
== 0 && *sharedp
)
1283 vm_object_upgrade(object
);
1286 if (result
!= KERN_SUCCESS
) {
1294 * Now we have the actual (object, pindex), fault in the page. If
1295 * vm_fault_object() fails it will unlock and deallocate the FS
1296 * data. If it succeeds everything remains locked and fs->object
1297 * will have an additinal PIP count if it is not equal to
1300 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1301 * We may have to upgrade its lock to handle the requested fault.
1303 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 0);
1305 if (result
== KERN_TRY_AGAIN
) {
1306 if (fs
.first_shared
== 0 && *sharedp
)
1307 vm_object_upgrade(object
);
1310 if (result
!= KERN_SUCCESS
) {
1315 if ((fault_type
& VM_PROT_WRITE
) && (fs
.prot
& VM_PROT_WRITE
) == 0) {
1316 *errorp
= KERN_PROTECTION_FAILURE
;
1317 unlock_and_deallocate(&fs
);
1322 * On success vm_fault_object() does not unlock or deallocate, so we
1323 * do it here. Note that the returned fs.m will be busied.
1328 * Return a held page. We are not doing any pmap manipulation so do
1329 * not set PG_MAPPED. However, adjust the page flags according to
1330 * the fault type because the caller may not use a managed pmapping
1331 * (so we don't want to lose the fact that the page will be dirtied
1332 * if a write fault was specified).
1335 vm_page_activate(fs
.m
);
1336 if ((fault_type
& VM_PROT_WRITE
) || (fault_flags
& VM_FAULT_DIRTY
))
1337 vm_page_dirty(fs
.m
);
1338 if (fault_flags
& VM_FAULT_UNSWAP
)
1339 swap_pager_unswapped(fs
.m
);
1342 * Indicate that the page was accessed.
1344 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1346 if (curthread
->td_lwp
) {
1348 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1350 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1355 * Unlock everything, and return the held page.
1357 vm_page_wakeup(fs
.m
);
1358 /*vm_object_deallocate(fs.first_object);*/
1359 /*fs.first_object = NULL; */
1366 * Translate the virtual page number (first_pindex) that is relative
1367 * to the address space into a logical page number that is relative to the
1368 * backing object. Use the virtual page table pointed to by (vpte).
1370 * Possibly downgrade the protection based on the vpte bits.
1372 * This implements an N-level page table. Any level can terminate the
1373 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1374 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1378 vm_fault_vpagetable(struct faultstate
*fs
, vm_pindex_t
*pindex
,
1379 vpte_t vpte
, int fault_type
, int allow_nofault
)
1382 struct lwbuf lwb_cache
;
1383 int vshift
= VPTE_FRAME_END
- PAGE_SHIFT
; /* index bits remaining */
1387 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1390 * We cannot proceed if the vpte is not valid, not readable
1391 * for a read fault, not writable for a write fault, or
1392 * not executable for an instruction execution fault.
1394 if ((vpte
& VPTE_V
) == 0) {
1395 unlock_and_deallocate(fs
);
1396 return (KERN_FAILURE
);
1398 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
) == 0) {
1399 unlock_and_deallocate(fs
);
1400 return (KERN_FAILURE
);
1402 if ((fault_type
& VM_PROT_EXECUTE
) && (vpte
& VPTE_NX
)) {
1403 unlock_and_deallocate(fs
);
1404 return (KERN_FAILURE
);
1406 if ((vpte
& VPTE_PS
) || vshift
== 0)
1410 * Get the page table page. Nominally we only read the page
1411 * table, but since we are actively setting VPTE_M and VPTE_A,
1412 * tell vm_fault_object() that we are writing it.
1414 * There is currently no real need to optimize this.
1416 result
= vm_fault_object(fs
, (vpte
& VPTE_FRAME
) >> PAGE_SHIFT
,
1417 VM_PROT_READ
|VM_PROT_WRITE
,
1419 if (result
!= KERN_SUCCESS
)
1423 * Process the returned fs.m and look up the page table
1424 * entry in the page table page.
1426 vshift
-= VPTE_PAGE_BITS
;
1427 lwb
= lwbuf_alloc(fs
->m
, &lwb_cache
);
1428 ptep
= ((vpte_t
*)lwbuf_kva(lwb
) +
1429 ((*pindex
>> vshift
) & VPTE_PAGE_MASK
));
1430 vm_page_activate(fs
->m
);
1433 * Page table write-back - entire operation including
1434 * validation of the pte must be atomic to avoid races
1435 * against the vkernel changing the pte.
1437 * If the vpte is valid for the* requested operation, do
1438 * a write-back to the page table.
1440 * XXX VPTE_M is not set properly for page directory pages.
1441 * It doesn't get set in the page directory if the page table
1442 * is modified during a read access.
1448 * Reload for the cmpset, but make sure the pte is
1455 if ((vpte
& VPTE_V
) == 0)
1458 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
))
1459 nvpte
|= VPTE_M
| VPTE_A
;
1460 if (fault_type
& (VM_PROT_READ
| VM_PROT_EXECUTE
))
1464 if (atomic_cmpset_long(ptep
, vpte
, nvpte
)) {
1465 vm_page_dirty(fs
->m
);
1470 vm_page_flag_set(fs
->m
, PG_REFERENCED
);
1471 vm_page_wakeup(fs
->m
);
1473 cleanup_successful_fault(fs
);
1477 * When the vkernel sets VPTE_RW it expects the real kernel to
1478 * reflect VPTE_M back when the page is modified via the mapping.
1479 * In order to accomplish this the real kernel must map the page
1480 * read-only for read faults and use write faults to reflect VPTE_M
1483 * Once VPTE_M has been set, the real kernel's pte allows writing.
1484 * If the vkernel clears VPTE_M the vkernel must be sure to
1485 * MADV_INVAL the real kernel's mappings to force the real kernel
1486 * to re-fault on the next write so oit can set VPTE_M again.
1488 if ((fault_type
& VM_PROT_WRITE
) == 0 &&
1489 (vpte
& (VPTE_RW
| VPTE_M
)) != (VPTE_RW
| VPTE_M
)) {
1490 fs
->first_prot
&= ~VM_PROT_WRITE
;
1494 * Disable EXECUTE perms if NX bit is set.
1497 fs
->first_prot
&= ~VM_PROT_EXECUTE
;
1500 * Combine remaining address bits with the vpte.
1502 *pindex
= ((vpte
& VPTE_FRAME
) >> PAGE_SHIFT
) +
1503 (*pindex
& ((1L << vshift
) - 1));
1504 return (KERN_SUCCESS
);
1509 * This is the core of the vm_fault code.
1511 * Do all operations required to fault-in (fs.first_object, pindex). Run
1512 * through the shadow chain as necessary and do required COW or virtual
1513 * copy operations. The caller has already fully resolved the vm_map_entry
1514 * and, if appropriate, has created a copy-on-write layer. All we need to
1515 * do is iterate the object chain.
1517 * On failure (fs) is unlocked and deallocated and the caller may return or
1518 * retry depending on the failure code. On success (fs) is NOT unlocked or
1519 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1520 * will have an additional PIP count if it is not equal to fs.first_object.
1522 * If locks based on fs->first_shared or fs->shared are insufficient,
1523 * clear the appropriate field(s) and return RETRY. COWs require that
1524 * first_shared be 0, while page allocations (or frees) require that
1525 * shared be 0. Renames require that both be 0.
1527 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1528 * we will have to retry with it exclusive if the vm_page is
1531 * fs->first_object must be held on call.
1535 vm_fault_object(struct faultstate
*fs
, vm_pindex_t first_pindex
,
1536 vm_prot_t fault_type
, int allow_nofault
)
1538 vm_object_t next_object
;
1542 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1543 fs
->prot
= fs
->first_prot
;
1544 fs
->object
= fs
->first_object
;
1545 pindex
= first_pindex
;
1547 vm_object_chain_acquire(fs
->first_object
, fs
->shared
);
1548 vm_object_pip_add(fs
->first_object
, 1);
1551 * If a read fault occurs we try to upgrade the page protection
1552 * and make it also writable if possible. There are three cases
1553 * where we cannot make the page mapping writable:
1555 * (1) The mapping is read-only or the VM object is read-only,
1556 * fs->prot above will simply not have VM_PROT_WRITE set.
1558 * (2) If the mapping is a virtual page table fs->first_prot will
1559 * have already been properly adjusted by vm_fault_vpagetable().
1560 * to detect writes so we can set VPTE_M in the virtual page
1561 * table. Used by vkernels.
1563 * (3) If the VM page is read-only or copy-on-write, upgrading would
1564 * just result in an unnecessary COW fault.
1566 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1570 /* see vpagetable code */
1571 if (fs
->entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1572 if ((fault_type
& VM_PROT_WRITE
) == 0)
1573 fs
->prot
&= ~VM_PROT_WRITE
;
1577 if (curthread
->td_lwp
&& curthread
->td_lwp
->lwp_vmspace
&&
1578 pmap_emulate_ad_bits(&curthread
->td_lwp
->lwp_vmspace
->vm_pmap
)) {
1579 if ((fault_type
& VM_PROT_WRITE
) == 0)
1580 fs
->prot
&= ~VM_PROT_WRITE
;
1583 /* vm_object_hold(fs->object); implied b/c object == first_object */
1587 * The entire backing chain from first_object to object
1588 * inclusive is chainlocked.
1590 * If the object is dead, we stop here
1592 if (fs
->object
->flags
& OBJ_DEAD
) {
1593 vm_object_pip_wakeup(fs
->first_object
);
1594 vm_object_chain_release_all(fs
->first_object
,
1596 if (fs
->object
!= fs
->first_object
)
1597 vm_object_drop(fs
->object
);
1598 unlock_and_deallocate(fs
);
1599 return (KERN_PROTECTION_FAILURE
);
1603 * See if the page is resident. Wait/Retry if the page is
1604 * busy (lots of stuff may have changed so we can't continue
1607 * We can theoretically allow the soft-busy case on a read
1608 * fault if the page is marked valid, but since such
1609 * pages are typically already pmap'd, putting that
1610 * special case in might be more effort then it is
1611 * worth. We cannot under any circumstances mess
1612 * around with a vm_page_t->busy page except, perhaps,
1615 fs
->m
= vm_page_lookup_busy_try(fs
->object
, pindex
,
1618 vm_object_pip_wakeup(fs
->first_object
);
1619 vm_object_chain_release_all(fs
->first_object
,
1621 if (fs
->object
!= fs
->first_object
)
1622 vm_object_drop(fs
->object
);
1624 vm_page_sleep_busy(fs
->m
, TRUE
, "vmpfw");
1625 mycpu
->gd_cnt
.v_intrans
++;
1626 /*vm_object_deallocate(fs->first_object);*/
1627 /*fs->first_object = NULL;*/
1629 return (KERN_TRY_AGAIN
);
1633 * The page is busied for us.
1635 * If reactivating a page from PQ_CACHE we may have
1638 int queue
= fs
->m
->queue
;
1639 vm_page_unqueue_nowakeup(fs
->m
);
1641 if ((queue
- fs
->m
->pc
) == PQ_CACHE
&&
1642 vm_page_count_severe()) {
1643 vm_page_activate(fs
->m
);
1644 vm_page_wakeup(fs
->m
);
1646 vm_object_pip_wakeup(fs
->first_object
);
1647 vm_object_chain_release_all(fs
->first_object
,
1649 if (fs
->object
!= fs
->first_object
)
1650 vm_object_drop(fs
->object
);
1651 unlock_and_deallocate(fs
);
1652 if (allow_nofault
== 0 ||
1653 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1658 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1659 return (KERN_PROTECTION_FAILURE
);
1661 return (KERN_TRY_AGAIN
);
1665 * If it still isn't completely valid (readable),
1666 * or if a read-ahead-mark is set on the VM page,
1667 * jump to readrest, else we found the page and
1670 * We can release the spl once we have marked the
1673 if (fs
->m
->object
!= &kernel_object
) {
1674 if ((fs
->m
->valid
& VM_PAGE_BITS_ALL
) !=
1678 if (fs
->m
->flags
& PG_RAM
) {
1681 vm_page_flag_clear(fs
->m
, PG_RAM
);
1685 break; /* break to PAGE HAS BEEN FOUND */
1689 * Page is not resident, If this is the search termination
1690 * or the pager might contain the page, allocate a new page.
1692 if (TRYPAGER(fs
) || fs
->object
== fs
->first_object
) {
1694 * Allocating, must be exclusive.
1696 if (fs
->object
== fs
->first_object
&&
1698 fs
->first_shared
= 0;
1699 vm_object_pip_wakeup(fs
->first_object
);
1700 vm_object_chain_release_all(fs
->first_object
,
1702 if (fs
->object
!= fs
->first_object
)
1703 vm_object_drop(fs
->object
);
1704 unlock_and_deallocate(fs
);
1705 return (KERN_TRY_AGAIN
);
1707 if (fs
->object
!= fs
->first_object
&&
1709 fs
->first_shared
= 0;
1711 vm_object_pip_wakeup(fs
->first_object
);
1712 vm_object_chain_release_all(fs
->first_object
,
1714 if (fs
->object
!= fs
->first_object
)
1715 vm_object_drop(fs
->object
);
1716 unlock_and_deallocate(fs
);
1717 return (KERN_TRY_AGAIN
);
1721 * If the page is beyond the object size we fail
1723 if (pindex
>= fs
->object
->size
) {
1724 vm_object_pip_wakeup(fs
->first_object
);
1725 vm_object_chain_release_all(fs
->first_object
,
1727 if (fs
->object
!= fs
->first_object
)
1728 vm_object_drop(fs
->object
);
1729 unlock_and_deallocate(fs
);
1730 return (KERN_PROTECTION_FAILURE
);
1734 * Allocate a new page for this object/offset pair.
1736 * It is possible for the allocation to race, so
1740 if (!vm_page_count_severe()) {
1741 fs
->m
= vm_page_alloc(fs
->object
, pindex
,
1742 ((fs
->vp
|| fs
->object
->backing_object
) ?
1743 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
:
1744 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
|
1745 VM_ALLOC_USE_GD
| VM_ALLOC_ZERO
));
1747 if (fs
->m
== NULL
) {
1748 vm_object_pip_wakeup(fs
->first_object
);
1749 vm_object_chain_release_all(fs
->first_object
,
1751 if (fs
->object
!= fs
->first_object
)
1752 vm_object_drop(fs
->object
);
1753 unlock_and_deallocate(fs
);
1754 if (allow_nofault
== 0 ||
1755 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1760 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1761 return (KERN_PROTECTION_FAILURE
);
1763 return (KERN_TRY_AGAIN
);
1767 * Fall through to readrest. We have a new page which
1768 * will have to be paged (since m->valid will be 0).
1774 * We have found an invalid or partially valid page, a
1775 * page with a read-ahead mark which might be partially or
1776 * fully valid (and maybe dirty too), or we have allocated
1779 * Attempt to fault-in the page if there is a chance that the
1780 * pager has it, and potentially fault in additional pages
1783 * If TRYPAGER is true then fs.m will be non-NULL and busied
1789 u_char behavior
= vm_map_entry_behavior(fs
->entry
);
1791 if (behavior
== MAP_ENTRY_BEHAV_RANDOM
)
1797 * Doing I/O may synchronously insert additional
1798 * pages so we can't be shared at this point either.
1800 * NOTE: We can't free fs->m here in the allocated
1801 * case (fs->object != fs->first_object) as
1802 * this would require an exclusively locked
1805 if (fs
->object
== fs
->first_object
&&
1807 vm_page_deactivate(fs
->m
);
1808 vm_page_wakeup(fs
->m
);
1810 fs
->first_shared
= 0;
1811 vm_object_pip_wakeup(fs
->first_object
);
1812 vm_object_chain_release_all(fs
->first_object
,
1814 if (fs
->object
!= fs
->first_object
)
1815 vm_object_drop(fs
->object
);
1816 unlock_and_deallocate(fs
);
1817 return (KERN_TRY_AGAIN
);
1819 if (fs
->object
!= fs
->first_object
&&
1821 vm_page_deactivate(fs
->m
);
1822 vm_page_wakeup(fs
->m
);
1824 fs
->first_shared
= 0;
1826 vm_object_pip_wakeup(fs
->first_object
);
1827 vm_object_chain_release_all(fs
->first_object
,
1829 if (fs
->object
!= fs
->first_object
)
1830 vm_object_drop(fs
->object
);
1831 unlock_and_deallocate(fs
);
1832 return (KERN_TRY_AGAIN
);
1836 * Avoid deadlocking against the map when doing I/O.
1837 * fs.object and the page is BUSY'd.
1839 * NOTE: Once unlocked, fs->entry can become stale
1840 * so this will NULL it out.
1842 * NOTE: fs->entry is invalid until we relock the
1843 * map and verify that the timestamp has not
1849 * Acquire the page data. We still hold a ref on
1850 * fs.object and the page has been BUSY's.
1852 * The pager may replace the page (for example, in
1853 * order to enter a fictitious page into the
1854 * object). If it does so it is responsible for
1855 * cleaning up the passed page and properly setting
1856 * the new page BUSY.
1858 * If we got here through a PG_RAM read-ahead
1859 * mark the page may be partially dirty and thus
1860 * not freeable. Don't bother checking to see
1861 * if the pager has the page because we can't free
1862 * it anyway. We have to depend on the get_page
1863 * operation filling in any gaps whether there is
1864 * backing store or not.
1866 rv
= vm_pager_get_page(fs
->object
, &fs
->m
, seqaccess
);
1868 if (rv
== VM_PAGER_OK
) {
1870 * Relookup in case pager changed page. Pager
1871 * is responsible for disposition of old page
1874 * XXX other code segments do relookups too.
1875 * It's a bad abstraction that needs to be
1878 fs
->m
= vm_page_lookup(fs
->object
, pindex
);
1879 if (fs
->m
== NULL
) {
1880 vm_object_pip_wakeup(fs
->first_object
);
1881 vm_object_chain_release_all(
1882 fs
->first_object
, fs
->object
);
1883 if (fs
->object
!= fs
->first_object
)
1884 vm_object_drop(fs
->object
);
1885 unlock_and_deallocate(fs
);
1886 return (KERN_TRY_AGAIN
);
1889 break; /* break to PAGE HAS BEEN FOUND */
1893 * Remove the bogus page (which does not exist at this
1894 * object/offset); before doing so, we must get back
1895 * our object lock to preserve our invariant.
1897 * Also wake up any other process that may want to bring
1900 * If this is the top-level object, we must leave the
1901 * busy page to prevent another process from rushing
1902 * past us, and inserting the page in that object at
1903 * the same time that we are.
1905 if (rv
== VM_PAGER_ERROR
) {
1907 kprintf("vm_fault: pager read error, "
1912 kprintf("vm_fault: pager read error, "
1920 * Data outside the range of the pager or an I/O error
1922 * The page may have been wired during the pagein,
1923 * e.g. by the buffer cache, and cannot simply be
1924 * freed. Call vnode_pager_freepage() to deal with it.
1926 * Also note that we cannot free the page if we are
1927 * holding the related object shared. XXX not sure
1928 * what to do in that case.
1930 if (fs
->object
!= fs
->first_object
) {
1932 * Scrap the page. Check to see if the
1933 * vm_pager_get_page() call has already
1937 vnode_pager_freepage(fs
->m
);
1942 * XXX - we cannot just fall out at this
1943 * point, m has been freed and is invalid!
1947 * XXX - the check for kernel_map is a kludge to work
1948 * around having the machine panic on a kernel space
1949 * fault w/ I/O error.
1951 if (((fs
->map
!= &kernel_map
) &&
1952 (rv
== VM_PAGER_ERROR
)) || (rv
== VM_PAGER_BAD
)) {
1954 if (fs
->first_shared
) {
1955 vm_page_deactivate(fs
->m
);
1956 vm_page_wakeup(fs
->m
);
1958 vnode_pager_freepage(fs
->m
);
1962 vm_object_pip_wakeup(fs
->first_object
);
1963 vm_object_chain_release_all(fs
->first_object
,
1965 if (fs
->object
!= fs
->first_object
)
1966 vm_object_drop(fs
->object
);
1967 unlock_and_deallocate(fs
);
1968 if (rv
== VM_PAGER_ERROR
)
1969 return (KERN_FAILURE
);
1971 return (KERN_PROTECTION_FAILURE
);
1977 * We get here if the object has a default pager (or unwiring)
1978 * or the pager doesn't have the page.
1980 * fs->first_m will be used for the COW unless we find a
1981 * deeper page to be mapped read-only, in which case the
1982 * unlock*(fs) will free first_m.
1984 if (fs
->object
== fs
->first_object
)
1985 fs
->first_m
= fs
->m
;
1988 * Move on to the next object. The chain lock should prevent
1989 * the backing_object from getting ripped out from under us.
1991 * The object lock for the next object is governed by
1994 if ((next_object
= fs
->object
->backing_object
) != NULL
) {
1996 vm_object_hold_shared(next_object
);
1998 vm_object_hold(next_object
);
1999 vm_object_chain_acquire(next_object
, fs
->shared
);
2000 KKASSERT(next_object
== fs
->object
->backing_object
);
2001 pindex
+= OFF_TO_IDX(fs
->object
->backing_object_offset
);
2004 if (next_object
== NULL
) {
2006 * If there's no object left, fill the page in the top
2007 * object with zeros.
2009 if (fs
->object
!= fs
->first_object
) {
2011 if (fs
->first_object
->backing_object
!=
2013 vm_object_hold(fs
->first_object
->backing_object
);
2016 vm_object_chain_release_all(
2017 fs
->first_object
->backing_object
,
2020 if (fs
->first_object
->backing_object
!=
2022 vm_object_drop(fs
->first_object
->backing_object
);
2025 vm_object_pip_wakeup(fs
->object
);
2026 vm_object_drop(fs
->object
);
2027 fs
->object
= fs
->first_object
;
2028 pindex
= first_pindex
;
2029 fs
->m
= fs
->first_m
;
2034 * Zero the page and mark it valid.
2036 vm_page_zero_fill(fs
->m
);
2037 mycpu
->gd_cnt
.v_zfod
++;
2038 fs
->m
->valid
= VM_PAGE_BITS_ALL
;
2039 break; /* break to PAGE HAS BEEN FOUND */
2041 if (fs
->object
!= fs
->first_object
) {
2042 vm_object_pip_wakeup(fs
->object
);
2043 vm_object_lock_swap();
2044 vm_object_drop(fs
->object
);
2046 KASSERT(fs
->object
!= next_object
,
2047 ("object loop %p", next_object
));
2048 fs
->object
= next_object
;
2049 vm_object_pip_add(fs
->object
, 1);
2053 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
2056 * object still held.
2057 * vm_map may not be locked (determined by fs->lookup_still_valid)
2059 * local shared variable may be different from fs->shared.
2061 * If the page is being written, but isn't already owned by the
2062 * top-level object, we have to copy it into a new page owned by the
2065 KASSERT((fs
->m
->busy_count
& PBUSY_LOCKED
) != 0,
2066 ("vm_fault: not busy after main loop"));
2068 if (fs
->object
!= fs
->first_object
) {
2070 * We only really need to copy if we want to write it.
2072 if (fault_type
& VM_PROT_WRITE
) {
2074 * This allows pages to be virtually copied from a
2075 * backing_object into the first_object, where the
2076 * backing object has no other refs to it, and cannot
2077 * gain any more refs. Instead of a bcopy, we just
2078 * move the page from the backing object to the
2079 * first object. Note that we must mark the page
2080 * dirty in the first object so that it will go out
2081 * to swap when needed.
2083 if (virtual_copy_ok(fs
)) {
2085 * (first_m) and (m) are both busied. We have
2086 * move (m) into (first_m)'s object/pindex
2087 * in an atomic fashion, then free (first_m).
2089 * first_object is held so second remove
2090 * followed by the rename should wind
2091 * up being atomic. vm_page_free() might
2092 * block so we don't do it until after the
2095 vm_page_protect(fs
->first_m
, VM_PROT_NONE
);
2096 vm_page_remove(fs
->first_m
);
2097 vm_page_rename(fs
->m
, fs
->first_object
,
2099 vm_page_free(fs
->first_m
);
2100 fs
->first_m
= fs
->m
;
2102 mycpu
->gd_cnt
.v_cow_optim
++;
2105 * Oh, well, lets copy it.
2107 * Why are we unmapping the original page
2108 * here? Well, in short, not all accessors
2109 * of user memory go through the pmap. The
2110 * procfs code doesn't have access user memory
2111 * via a local pmap, so vm_fault_page*()
2112 * can't call pmap_enter(). And the umtx*()
2113 * code may modify the COW'd page via a DMAP
2114 * or kernel mapping and not via the pmap,
2115 * leaving the original page still mapped
2116 * read-only into the pmap.
2118 * So we have to remove the page from at
2119 * least the current pmap if it is in it.
2121 * We used to just remove it from all pmaps
2122 * but that creates inefficiencies on SMP,
2123 * particularly for COW program & library
2124 * mappings that are concurrently exec'd.
2125 * Only remove the page from the current
2128 KKASSERT(fs
->first_shared
== 0);
2129 vm_page_copy(fs
->m
, fs
->first_m
);
2130 /*vm_page_protect(fs->m, VM_PROT_NONE);*/
2131 pmap_remove_specific(
2132 &curthread
->td_lwp
->lwp_vmspace
->vm_pmap
,
2137 * We no longer need the old page or object.
2143 * We intend to revert to first_object, undo the
2144 * chain lock through to that.
2147 if (fs
->first_object
->backing_object
!= fs
->object
)
2148 vm_object_hold(fs
->first_object
->backing_object
);
2150 vm_object_chain_release_all(
2151 fs
->first_object
->backing_object
,
2154 if (fs
->first_object
->backing_object
!= fs
->object
)
2155 vm_object_drop(fs
->first_object
->backing_object
);
2159 * fs->object != fs->first_object due to above
2162 vm_object_pip_wakeup(fs
->object
);
2163 vm_object_drop(fs
->object
);
2166 * Only use the new page below...
2168 mycpu
->gd_cnt
.v_cow_faults
++;
2169 fs
->m
= fs
->first_m
;
2170 fs
->object
= fs
->first_object
;
2171 pindex
= first_pindex
;
2174 * If it wasn't a write fault avoid having to copy
2175 * the page by mapping it read-only.
2177 fs
->prot
&= ~VM_PROT_WRITE
;
2182 * Relock the map if necessary, then check the generation count.
2183 * relock_map() will update fs->timestamp to account for the
2184 * relocking if necessary.
2186 * If the count has changed after relocking then all sorts of
2187 * crap may have happened and we have to retry.
2189 * NOTE: The relock_map() can fail due to a deadlock against
2190 * the vm_page we are holding BUSY.
2192 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
2193 if (relock_map(fs
) ||
2194 fs
->map
->timestamp
!= fs
->map_generation
) {
2196 vm_object_pip_wakeup(fs
->first_object
);
2197 vm_object_chain_release_all(fs
->first_object
,
2199 if (fs
->object
!= fs
->first_object
)
2200 vm_object_drop(fs
->object
);
2201 unlock_and_deallocate(fs
);
2202 return (KERN_TRY_AGAIN
);
2207 * If the fault is a write, we know that this page is being
2208 * written NOW so dirty it explicitly to save on pmap_is_modified()
2211 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2212 * if the page is already dirty to prevent data written with
2213 * the expectation of being synced from not being synced.
2214 * Likewise if this entry does not request NOSYNC then make
2215 * sure the page isn't marked NOSYNC. Applications sharing
2216 * data should use the same flags to avoid ping ponging.
2218 * Also tell the backing pager, if any, that it should remove
2219 * any swap backing since the page is now dirty.
2221 vm_page_activate(fs
->m
);
2222 if (fs
->prot
& VM_PROT_WRITE
) {
2223 vm_object_set_writeable_dirty(fs
->m
->object
);
2224 vm_set_nosync(fs
->m
, fs
->entry
);
2225 if (fs
->fault_flags
& VM_FAULT_DIRTY
) {
2226 vm_page_dirty(fs
->m
);
2227 if (fs
->m
->flags
& PG_SWAPPED
) {
2229 * If the page is swapped out we have to call
2230 * swap_pager_unswapped() which requires an
2231 * exclusive object lock. If we are shared,
2232 * we must clear the shared flag and retry.
2234 if ((fs
->object
== fs
->first_object
&&
2235 fs
->first_shared
) ||
2236 (fs
->object
!= fs
->first_object
&&
2238 vm_page_wakeup(fs
->m
);
2240 if (fs
->object
== fs
->first_object
)
2241 fs
->first_shared
= 0;
2244 vm_object_pip_wakeup(fs
->first_object
);
2245 vm_object_chain_release_all(
2246 fs
->first_object
, fs
->object
);
2247 if (fs
->object
!= fs
->first_object
)
2248 vm_object_drop(fs
->object
);
2249 unlock_and_deallocate(fs
);
2250 return (KERN_TRY_AGAIN
);
2252 swap_pager_unswapped(fs
->m
);
2257 vm_object_pip_wakeup(fs
->first_object
);
2258 vm_object_chain_release_all(fs
->first_object
, fs
->object
);
2259 if (fs
->object
!= fs
->first_object
)
2260 vm_object_drop(fs
->object
);
2263 * Page had better still be busy. We are still locked up and
2264 * fs->object will have another PIP reference if it is not equal
2265 * to fs->first_object.
2267 KASSERT(fs
->m
->busy_count
& PBUSY_LOCKED
,
2268 ("vm_fault: page %p not busy!", fs
->m
));
2271 * Sanity check: page must be completely valid or it is not fit to
2272 * map into user space. vm_pager_get_pages() ensures this.
2274 if (fs
->m
->valid
!= VM_PAGE_BITS_ALL
) {
2275 vm_page_zero_invalid(fs
->m
, TRUE
);
2276 kprintf("Warning: page %p partially invalid on fault\n", fs
->m
);
2279 return (KERN_SUCCESS
);
2283 * Wire down a range of virtual addresses in a map. The entry in question
2284 * should be marked in-transition and the map must be locked. We must
2285 * release the map temporarily while faulting-in the page to avoid a
2286 * deadlock. Note that the entry may be clipped while we are blocked but
2287 * will never be freed.
2292 vm_fault_wire(vm_map_t map
, vm_map_entry_t entry
,
2293 boolean_t user_wire
, int kmflags
)
2295 boolean_t fictitious
;
2306 wire_prot
= VM_PROT_READ
;
2307 fault_flags
= VM_FAULT_USER_WIRE
;
2309 wire_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
2310 fault_flags
= VM_FAULT_CHANGE_WIRING
;
2312 if (kmflags
& KM_NOTLBSYNC
)
2313 wire_prot
|= VM_PROT_NOSYNC
;
2315 pmap
= vm_map_pmap(map
);
2316 start
= entry
->start
;
2319 switch(entry
->maptype
) {
2320 case VM_MAPTYPE_NORMAL
:
2321 case VM_MAPTYPE_VPAGETABLE
:
2322 fictitious
= entry
->object
.vm_object
&&
2323 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2324 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2326 case VM_MAPTYPE_UKSMAP
:
2334 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2340 * We simulate a fault to get the page and enter it in the physical
2343 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2344 rv
= vm_fault(map
, va
, wire_prot
, fault_flags
);
2346 while (va
> start
) {
2348 m
= pmap_unwire(pmap
, va
);
2349 if (m
&& !fictitious
) {
2350 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2351 vm_page_unwire(m
, 1);
2366 * Unwire a range of virtual addresses in a map. The map should be
2370 vm_fault_unwire(vm_map_t map
, vm_map_entry_t entry
)
2372 boolean_t fictitious
;
2379 pmap
= vm_map_pmap(map
);
2380 start
= entry
->start
;
2382 fictitious
= entry
->object
.vm_object
&&
2383 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2384 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2385 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2389 * Since the pages are wired down, we must be able to get their
2390 * mappings from the physical map system.
2392 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2393 m
= pmap_unwire(pmap
, va
);
2394 if (m
&& !fictitious
) {
2395 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2396 vm_page_unwire(m
, 1);
2403 * Copy all of the pages from a wired-down map entry to another.
2405 * The source and destination maps must be locked for write.
2406 * The source and destination maps token must be held
2407 * The source map entry must be wired down (or be a sharing map
2408 * entry corresponding to a main map entry that is wired down).
2410 * No other requirements.
2412 * XXX do segment optimization
2415 vm_fault_copy_entry(vm_map_t dst_map
, vm_map_t src_map
,
2416 vm_map_entry_t dst_entry
, vm_map_entry_t src_entry
)
2418 vm_object_t dst_object
;
2419 vm_object_t src_object
;
2420 vm_ooffset_t dst_offset
;
2421 vm_ooffset_t src_offset
;
2427 src_object
= src_entry
->object
.vm_object
;
2428 src_offset
= src_entry
->offset
;
2431 * Create the top-level object for the destination entry. (Doesn't
2432 * actually shadow anything - we copy the pages directly.)
2434 vm_map_entry_allocate_object(dst_entry
);
2435 dst_object
= dst_entry
->object
.vm_object
;
2437 prot
= dst_entry
->max_protection
;
2440 * Loop through all of the pages in the entry's range, copying each
2441 * one from the source object (it should be there) to the destination
2444 vm_object_hold(src_object
);
2445 vm_object_hold(dst_object
);
2447 for (vaddr
= dst_entry
->start
, dst_offset
= 0;
2448 vaddr
< dst_entry
->end
;
2449 vaddr
+= PAGE_SIZE
, dst_offset
+= PAGE_SIZE
) {
2452 * Allocate a page in the destination object
2455 dst_m
= vm_page_alloc(dst_object
,
2456 OFF_TO_IDX(dst_offset
),
2458 if (dst_m
== NULL
) {
2461 } while (dst_m
== NULL
);
2464 * Find the page in the source object, and copy it in.
2465 * (Because the source is wired down, the page will be in
2468 src_m
= vm_page_lookup(src_object
,
2469 OFF_TO_IDX(dst_offset
+ src_offset
));
2471 panic("vm_fault_copy_wired: page missing");
2473 vm_page_copy(src_m
, dst_m
);
2476 * Enter it in the pmap...
2478 pmap_enter(dst_map
->pmap
, vaddr
, dst_m
, prot
, FALSE
, dst_entry
);
2481 * Mark it no longer busy, and put it on the active list.
2483 vm_page_activate(dst_m
);
2484 vm_page_wakeup(dst_m
);
2486 vm_object_drop(dst_object
);
2487 vm_object_drop(src_object
);
2493 * This routine checks around the requested page for other pages that
2494 * might be able to be faulted in. This routine brackets the viable
2495 * pages for the pages to be paged in.
2498 * m, rbehind, rahead
2501 * marray (array of vm_page_t), reqpage (index of requested page)
2504 * number of pages in marray
2507 vm_fault_additional_pages(vm_page_t m
, int rbehind
, int rahead
,
2508 vm_page_t
*marray
, int *reqpage
)
2512 vm_pindex_t pindex
, startpindex
, endpindex
, tpindex
;
2514 int cbehind
, cahead
;
2520 * we don't fault-ahead for device pager
2522 if ((object
->type
== OBJT_DEVICE
) ||
2523 (object
->type
== OBJT_MGTDEVICE
)) {
2530 * if the requested page is not available, then give up now
2532 if (!vm_pager_has_page(object
, pindex
, &cbehind
, &cahead
)) {
2533 *reqpage
= 0; /* not used by caller, fix compiler warn */
2537 if ((cbehind
== 0) && (cahead
== 0)) {
2543 if (rahead
> cahead
) {
2547 if (rbehind
> cbehind
) {
2552 * Do not do any readahead if we have insufficient free memory.
2554 * XXX code was broken disabled before and has instability
2555 * with this conditonal fixed, so shortcut for now.
2557 if (burst_fault
== 0 || vm_page_count_severe()) {
2564 * scan backward for the read behind pages -- in memory
2566 * Assume that if the page is not found an interrupt will not
2567 * create it. Theoretically interrupts can only remove (busy)
2568 * pages, not create new associations.
2571 if (rbehind
> pindex
) {
2575 startpindex
= pindex
- rbehind
;
2578 vm_object_hold(object
);
2579 for (tpindex
= pindex
; tpindex
> startpindex
; --tpindex
) {
2580 if (vm_page_lookup(object
, tpindex
- 1))
2585 while (tpindex
< pindex
) {
2586 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2589 for (j
= 0; j
< i
; j
++) {
2590 vm_page_free(marray
[j
]);
2592 vm_object_drop(object
);
2601 vm_object_drop(object
);
2607 * Assign requested page
2614 * Scan forwards for read-ahead pages
2616 tpindex
= pindex
+ 1;
2617 endpindex
= tpindex
+ rahead
;
2618 if (endpindex
> object
->size
)
2619 endpindex
= object
->size
;
2621 vm_object_hold(object
);
2622 while (tpindex
< endpindex
) {
2623 if (vm_page_lookup(object
, tpindex
))
2625 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2633 vm_object_drop(object
);
2641 * vm_prefault() provides a quick way of clustering pagefaults into a
2642 * processes address space. It is a "cousin" of pmap_object_init_pt,
2643 * except it runs at page fault time instead of mmap time.
2645 * vm.fast_fault Enables pre-faulting zero-fill pages
2647 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2648 * prefault. Scan stops in either direction when
2649 * a page is found to already exist.
2651 * This code used to be per-platform pmap_prefault(). It is now
2652 * machine-independent and enhanced to also pre-fault zero-fill pages
2653 * (see vm.fast_fault) as well as make them writable, which greatly
2654 * reduces the number of page faults programs incur.
2656 * Application performance when pre-faulting zero-fill pages is heavily
2657 * dependent on the application. Very tiny applications like /bin/echo
2658 * lose a little performance while applications of any appreciable size
2659 * gain performance. Prefaulting multiple pages also reduces SMP
2660 * congestion and can improve SMP performance significantly.
2662 * NOTE! prot may allow writing but this only applies to the top level
2663 * object. If we wind up mapping a page extracted from a backing
2664 * object we have to make sure it is read-only.
2666 * NOTE! The caller has already handled any COW operations on the
2667 * vm_map_entry via the normal fault code. Do NOT call this
2668 * shortcut unless the normal fault code has run on this entry.
2670 * The related map must be locked.
2671 * No other requirements.
2673 static int vm_prefault_pages
= 8;
2674 SYSCTL_INT(_vm
, OID_AUTO
, prefault_pages
, CTLFLAG_RW
, &vm_prefault_pages
, 0,
2675 "Maximum number of pages to pre-fault");
2676 static int vm_fast_fault
= 1;
2677 SYSCTL_INT(_vm
, OID_AUTO
, fast_fault
, CTLFLAG_RW
, &vm_fast_fault
, 0,
2678 "Burst fault zero-fill regions");
2681 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2682 * is not already dirty by other means. This will prevent passive
2683 * filesystem syncing as well as 'sync' from writing out the page.
2686 vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
)
2688 if (entry
->eflags
& MAP_ENTRY_NOSYNC
) {
2690 vm_page_flag_set(m
, PG_NOSYNC
);
2692 vm_page_flag_clear(m
, PG_NOSYNC
);
2697 vm_prefault(pmap_t pmap
, vm_offset_t addra
, vm_map_entry_t entry
, int prot
,
2713 * Get stable max count value, disabled if set to 0
2715 maxpages
= vm_prefault_pages
;
2721 * We do not currently prefault mappings that use virtual page
2722 * tables. We do not prefault foreign pmaps.
2724 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2726 lp
= curthread
->td_lwp
;
2727 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2731 * Limit pre-fault count to 1024 pages.
2733 if (maxpages
> 1024)
2736 object
= entry
->object
.vm_object
;
2737 KKASSERT(object
!= NULL
);
2738 KKASSERT(object
== entry
->object
.vm_object
);
2741 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2742 * now (or do something more complex XXX).
2744 vm_object_hold(object
);
2745 vm_object_chain_acquire(object
, 0);
2749 for (i
= 0; i
< maxpages
; ++i
) {
2750 vm_object_t lobject
;
2751 vm_object_t nobject
;
2756 * This can eat a lot of time on a heavily contended
2757 * machine so yield on the tick if needed.
2763 * Calculate the page to pre-fault, stopping the scan in
2764 * each direction separately if the limit is reached.
2769 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2773 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2775 if (addr
< entry
->start
) {
2781 if (addr
>= entry
->end
) {
2789 * Skip pages already mapped, and stop scanning in that
2790 * direction. When the scan terminates in both directions
2793 if (pmap_prefault_ok(pmap
, addr
) == 0) {
2804 * Follow the VM object chain to obtain the page to be mapped
2807 * If we reach the terminal object without finding a page
2808 * and we determine it would be advantageous, then allocate
2809 * a zero-fill page for the base object. The base object
2810 * is guaranteed to be OBJT_DEFAULT for this case.
2812 * In order to not have to check the pager via *haspage*()
2813 * we stop if any non-default object is encountered. e.g.
2814 * a vnode or swap object would stop the loop.
2816 index
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
2821 KKASSERT(lobject
== entry
->object
.vm_object
);
2822 /*vm_object_hold(lobject); implied */
2824 while ((m
= vm_page_lookup_busy_try(lobject
, pindex
,
2825 TRUE
, &error
)) == NULL
) {
2826 if (lobject
->type
!= OBJT_DEFAULT
)
2828 if (lobject
->backing_object
== NULL
) {
2829 if (vm_fast_fault
== 0)
2831 if ((prot
& VM_PROT_WRITE
) == 0 ||
2832 vm_page_count_min(0)) {
2837 * NOTE: Allocated from base object
2839 m
= vm_page_alloc(object
, index
,
2848 /* lobject = object .. not needed */
2851 if (lobject
->backing_object_offset
& PAGE_MASK
)
2853 nobject
= lobject
->backing_object
;
2854 vm_object_hold(nobject
);
2855 KKASSERT(nobject
== lobject
->backing_object
);
2856 pindex
+= lobject
->backing_object_offset
>> PAGE_SHIFT
;
2857 if (lobject
!= object
) {
2858 vm_object_lock_swap();
2859 vm_object_drop(lobject
);
2862 pprot
&= ~VM_PROT_WRITE
;
2863 vm_object_chain_acquire(lobject
, 0);
2867 * NOTE: A non-NULL (m) will be associated with lobject if
2868 * it was found there, otherwise it is probably a
2869 * zero-fill page associated with the base object.
2871 * Give-up if no page is available.
2874 if (lobject
!= object
) {
2876 if (object
->backing_object
!= lobject
)
2877 vm_object_hold(object
->backing_object
);
2879 vm_object_chain_release_all(
2880 object
->backing_object
, lobject
);
2882 if (object
->backing_object
!= lobject
)
2883 vm_object_drop(object
->backing_object
);
2885 vm_object_drop(lobject
);
2891 * The object must be marked dirty if we are mapping a
2892 * writable page. m->object is either lobject or object,
2893 * both of which are still held. Do this before we
2894 * potentially drop the object.
2896 if (pprot
& VM_PROT_WRITE
)
2897 vm_object_set_writeable_dirty(m
->object
);
2900 * Do not conditionalize on PG_RAM. If pages are present in
2901 * the VM system we assume optimal caching. If caching is
2902 * not optimal the I/O gravy train will be restarted when we
2903 * hit an unavailable page. We do not want to try to restart
2904 * the gravy train now because we really don't know how much
2905 * of the object has been cached. The cost for restarting
2906 * the gravy train should be low (since accesses will likely
2907 * be I/O bound anyway).
2909 if (lobject
!= object
) {
2911 if (object
->backing_object
!= lobject
)
2912 vm_object_hold(object
->backing_object
);
2914 vm_object_chain_release_all(object
->backing_object
,
2917 if (object
->backing_object
!= lobject
)
2918 vm_object_drop(object
->backing_object
);
2920 vm_object_drop(lobject
);
2924 * Enter the page into the pmap if appropriate. If we had
2925 * allocated the page we have to place it on a queue. If not
2926 * we just have to make sure it isn't on the cache queue
2927 * (pages on the cache queue are not allowed to be mapped).
2931 * Page must be zerod.
2933 vm_page_zero_fill(m
);
2934 mycpu
->gd_cnt
.v_zfod
++;
2935 m
->valid
= VM_PAGE_BITS_ALL
;
2938 * Handle dirty page case
2940 if (pprot
& VM_PROT_WRITE
)
2941 vm_set_nosync(m
, entry
);
2942 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2943 mycpu
->gd_cnt
.v_vm_faults
++;
2944 if (curthread
->td_lwp
)
2945 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2946 vm_page_deactivate(m
);
2947 if (pprot
& VM_PROT_WRITE
) {
2948 /*vm_object_set_writeable_dirty(m->object);*/
2949 vm_set_nosync(m
, entry
);
2950 if (fault_flags
& VM_FAULT_DIRTY
) {
2953 swap_pager_unswapped(m
);
2958 /* couldn't busy page, no wakeup */
2960 ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
2961 (m
->flags
& PG_FICTITIOUS
) == 0) {
2963 * A fully valid page not undergoing soft I/O can
2964 * be immediately entered into the pmap.
2966 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
2967 vm_page_deactivate(m
);
2968 if (pprot
& VM_PROT_WRITE
) {
2969 /*vm_object_set_writeable_dirty(m->object);*/
2970 vm_set_nosync(m
, entry
);
2971 if (fault_flags
& VM_FAULT_DIRTY
) {
2974 swap_pager_unswapped(m
);
2977 if (pprot
& VM_PROT_WRITE
)
2978 vm_set_nosync(m
, entry
);
2979 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2980 mycpu
->gd_cnt
.v_vm_faults
++;
2981 if (curthread
->td_lwp
)
2982 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2988 vm_object_chain_release(object
);
2989 vm_object_drop(object
);
2993 * Object can be held shared
2996 vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
2997 vm_map_entry_t entry
, int prot
, int fault_flags
)
3010 * Get stable max count value, disabled if set to 0
3012 maxpages
= vm_prefault_pages
;
3018 * We do not currently prefault mappings that use virtual page
3019 * tables. We do not prefault foreign pmaps.
3021 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
3023 lp
= curthread
->td_lwp
;
3024 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
3026 object
= entry
->object
.vm_object
;
3027 if (object
->backing_object
!= NULL
)
3029 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
3032 * Limit pre-fault count to 1024 pages.
3034 if (maxpages
> 1024)
3039 for (i
= 0; i
< maxpages
; ++i
) {
3043 * Calculate the page to pre-fault, stopping the scan in
3044 * each direction separately if the limit is reached.
3049 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
3053 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
3055 if (addr
< entry
->start
) {
3061 if (addr
>= entry
->end
) {
3069 * Follow the VM object chain to obtain the page to be mapped
3070 * into the pmap. This version of the prefault code only
3071 * works with terminal objects.
3073 * The page must already exist. If we encounter a problem
3076 * WARNING! We cannot call swap_pager_unswapped() or insert
3077 * a new vm_page with a shared token.
3079 pindex
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
3082 * Skip pages already mapped, and stop scanning in that
3083 * direction. When the scan terminates in both directions
3086 if (pmap_prefault_ok(pmap
, addr
) == 0) {
3097 * Shortcut the read-only mapping case using the far more
3098 * efficient vm_page_lookup_sbusy_try() function. This
3099 * allows us to acquire the page soft-busied only which
3100 * is especially nice for concurrent execs of the same
3103 * The lookup function also validates page suitability
3104 * (all valid bits set, and not fictitious).
3106 * If the page is in PQ_CACHE we have to fall-through
3107 * and hard-busy it so we can move it out of PQ_CACHE.
3109 if ((prot
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
)) == 0) {
3110 m
= vm_page_lookup_sbusy_try(object
, pindex
,
3114 if ((m
->queue
- m
->pc
) != PQ_CACHE
) {
3115 pmap_enter(pmap
, addr
, m
, prot
, 0, entry
);
3116 mycpu
->gd_cnt
.v_vm_faults
++;
3117 if (curthread
->td_lwp
)
3118 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
3119 vm_page_sbusy_drop(m
);
3122 vm_page_sbusy_drop(m
);
3126 * Fallback to normal vm_page lookup code. This code
3127 * hard-busies the page. Not only that, but the page
3128 * can remain in that state for a significant period
3129 * time due to pmap_enter()'s overhead.
3131 m
= vm_page_lookup_busy_try(object
, pindex
, TRUE
, &error
);
3132 if (m
== NULL
|| error
)
3136 * Stop if the page cannot be trivially entered into the
3139 if (((m
->valid
& VM_PAGE_BITS_ALL
) != VM_PAGE_BITS_ALL
) ||
3140 (m
->flags
& PG_FICTITIOUS
) ||
3141 ((m
->flags
& PG_SWAPPED
) &&
3142 (prot
& VM_PROT_WRITE
) &&
3143 (fault_flags
& VM_FAULT_DIRTY
))) {
3149 * Enter the page into the pmap. The object might be held
3150 * shared so we can't do any (serious) modifying operation
3153 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
3154 vm_page_deactivate(m
);
3155 if (prot
& VM_PROT_WRITE
) {
3156 vm_object_set_writeable_dirty(m
->object
);
3157 vm_set_nosync(m
, entry
);
3158 if (fault_flags
& VM_FAULT_DIRTY
) {
3160 /* can't happeen due to conditional above */
3161 /* swap_pager_unswapped(m); */
3164 pmap_enter(pmap
, addr
, m
, prot
, 0, entry
);
3165 mycpu
->gd_cnt
.v_vm_faults
++;
3166 if (curthread
->td_lwp
)
3167 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;