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 int vm_shared_fault
= 1;
156 TUNABLE_INT("vm.shared_fault", &vm_shared_fault
);
157 SYSCTL_INT(_vm
, OID_AUTO
, shared_fault
, CTLFLAG_RW
, &vm_shared_fault
, 0,
158 "Allow shared token on vm_object");
160 static int vm_fault_object(struct faultstate
*, vm_pindex_t
, vm_prot_t
, int);
161 static int vm_fault_vpagetable(struct faultstate
*, vm_pindex_t
*,
164 static int vm_fault_additional_pages (vm_page_t
, int, int, vm_page_t
*, int *);
166 static void vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
);
167 static void vm_prefault(pmap_t pmap
, vm_offset_t addra
,
168 vm_map_entry_t entry
, int prot
, int fault_flags
);
169 static void vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
170 vm_map_entry_t entry
, int prot
, int fault_flags
);
173 release_page(struct faultstate
*fs
)
175 vm_page_deactivate(fs
->m
);
176 vm_page_wakeup(fs
->m
);
181 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
182 * requires relocking and then checking the timestamp.
184 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
185 * not have to update fs->map_generation here.
187 * NOTE: This function can fail due to a deadlock against the caller's
188 * holding of a vm_page BUSY.
191 relock_map(struct faultstate
*fs
)
195 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
196 error
= vm_map_lock_read_to(fs
->map
);
198 fs
->lookup_still_valid
= TRUE
;
206 unlock_map(struct faultstate
*fs
)
208 if (fs
->lookup_still_valid
&& fs
->map
) {
209 vm_map_lookup_done(fs
->map
, fs
->entry
, 0);
210 fs
->lookup_still_valid
= FALSE
;
215 * Clean up after a successful call to vm_fault_object() so another call
216 * to vm_fault_object() can be made.
219 _cleanup_successful_fault(struct faultstate
*fs
, int relock
)
222 * We allocated a junk page for a COW operation that did
223 * not occur, the page must be freed.
225 if (fs
->object
!= fs
->first_object
) {
226 KKASSERT(fs
->first_shared
== 0);
227 vm_page_free(fs
->first_m
);
228 vm_object_pip_wakeup(fs
->object
);
235 fs
->object
= fs
->first_object
;
236 if (relock
&& fs
->lookup_still_valid
== FALSE
) {
238 vm_map_lock_read(fs
->map
);
239 fs
->lookup_still_valid
= TRUE
;
244 _unlock_things(struct faultstate
*fs
, int dealloc
)
246 _cleanup_successful_fault(fs
, 0);
248 /*vm_object_deallocate(fs->first_object);*/
249 /*fs->first_object = NULL; drop used later on */
252 if (fs
->vp
!= NULL
) {
258 #define unlock_things(fs) _unlock_things(fs, 0)
259 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
260 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
265 * Determine if the pager for the current object *might* contain the page.
267 * We only need to try the pager if this is not a default object (default
268 * objects are zero-fill and have no real pager), and if we are not taking
269 * a wiring fault or if the FS entry is wired.
271 #define TRYPAGER(fs) \
272 (fs->object->type != OBJT_DEFAULT && \
273 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
278 * Handle a page fault occuring at the given address, requiring the given
279 * permissions, in the map specified. If successful, the page is inserted
280 * into the associated physical map.
282 * NOTE: The given address should be truncated to the proper page address.
284 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
285 * a standard error specifying why the fault is fatal is returned.
287 * The map in question must be referenced, and remains so.
288 * The caller may hold no locks.
289 * No other requirements.
292 vm_fault(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
, int fault_flags
)
295 vm_pindex_t first_pindex
;
296 struct faultstate fs
;
300 struct vm_map_ilock ilock
;
306 inherit_prot
= fault_type
& VM_PROT_NOSYNC
;
308 fs
.fault_flags
= fault_flags
;
310 fs
.shared
= vm_shared_fault
;
311 fs
.first_shared
= vm_shared_fault
;
315 * vm_map interactions
318 if ((lp
= td
->td_lwp
) != NULL
)
319 lp
->lwp_flags
|= LWP_PAGING
;
323 * Find the vm_map_entry representing the backing store and resolve
324 * the top level object and page index. This may have the side
325 * effect of executing a copy-on-write on the map entry and/or
326 * creating a shadow object, but will not COW any actual VM pages.
328 * On success fs.map is left read-locked and various other fields
329 * are initialized but not otherwise referenced or locked.
331 * NOTE! vm_map_lookup will try to upgrade the fault_type to
332 * VM_FAULT_WRITE if the map entry is a virtual page table
333 * and also writable, so we can set the 'A'accessed bit in
334 * the virtual page table entry.
337 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
338 &fs
.entry
, &fs
.first_object
,
339 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
342 * If the lookup failed or the map protections are incompatible,
343 * the fault generally fails.
345 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
346 * tried to do a COW fault.
348 * If the caller is trying to do a user wiring we have more work
351 if (result
!= KERN_SUCCESS
) {
352 if (result
== KERN_FAILURE_NOFAULT
) {
353 result
= KERN_FAILURE
;
356 if (result
!= KERN_PROTECTION_FAILURE
||
357 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
359 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
360 map
!= &kernel_map
&& curproc
!= NULL
) {
361 result
= vm_map_growstack(map
, vaddr
);
362 if (result
== KERN_SUCCESS
) {
367 result
= KERN_FAILURE
;
373 * If we are user-wiring a r/w segment, and it is COW, then
374 * we need to do the COW operation. Note that we don't
375 * currently COW RO sections now, because it is NOT desirable
376 * to COW .text. We simply keep .text from ever being COW'ed
377 * and take the heat that one cannot debug wired .text sections.
379 result
= vm_map_lookup(&fs
.map
, vaddr
,
380 VM_PROT_READ
|VM_PROT_WRITE
|
381 VM_PROT_OVERRIDE_WRITE
,
382 &fs
.entry
, &fs
.first_object
,
383 &first_pindex
, &fs
.first_prot
,
385 if (result
!= KERN_SUCCESS
) {
386 /* could also be KERN_FAILURE_NOFAULT */
387 result
= KERN_FAILURE
;
392 * If we don't COW now, on a user wire, the user will never
393 * be able to write to the mapping. If we don't make this
394 * restriction, the bookkeeping would be nearly impossible.
396 * XXX We have a shared lock, this will have a MP race but
397 * I don't see how it can hurt anything.
399 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
400 atomic_clear_char(&fs
.entry
->max_protection
,
406 * fs.map is read-locked
408 * Misc checks. Save the map generation number to detect races.
410 fs
.map_generation
= fs
.map
->timestamp
;
411 fs
.lookup_still_valid
= TRUE
;
413 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
414 fs
.prot
= fs
.first_prot
; /* default (used by uksmap) */
416 if (fs
.entry
->eflags
& (MAP_ENTRY_NOFAULT
| MAP_ENTRY_KSTACK
)) {
417 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
418 panic("vm_fault: fault on nofault entry, addr: %p",
421 if ((fs
.entry
->eflags
& MAP_ENTRY_KSTACK
) &&
422 vaddr
>= fs
.entry
->start
&&
423 vaddr
< fs
.entry
->start
+ PAGE_SIZE
) {
424 panic("vm_fault: fault on stack guard, addr: %p",
430 * A user-kernel shared map has no VM object and bypasses
431 * everything. We execute the uksmap function with a temporary
432 * fictitious vm_page. The address is directly mapped with no
435 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
436 struct vm_page fakem
;
438 bzero(&fakem
, sizeof(fakem
));
439 fakem
.pindex
= first_pindex
;
440 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
441 fakem
.valid
= VM_PAGE_BITS_ALL
;
442 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
443 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
444 result
= KERN_FAILURE
;
448 pmap_enter(fs
.map
->pmap
, vaddr
, &fakem
, fs
.prot
| inherit_prot
,
454 * A system map entry may return a NULL object. No object means
455 * no pager means an unrecoverable kernel fault.
457 if (fs
.first_object
== NULL
) {
458 panic("vm_fault: unrecoverable fault at %p in entry %p",
459 (void *)vaddr
, fs
.entry
);
463 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
466 * Unfortunately a deadlock can occur if we are forced to page-in
467 * from swap, but diving all the way into the vm_pager_get_page()
468 * function to find out is too much. Just check the object type.
470 * The deadlock is a CAM deadlock on a busy VM page when trying
471 * to finish an I/O if another process gets stuck in
472 * vop_helper_read_shortcut() due to a swap fault.
474 if ((td
->td_flags
& TDF_NOFAULT
) &&
476 fs
.first_object
->type
== OBJT_VNODE
||
477 fs
.first_object
->type
== OBJT_SWAP
||
478 fs
.first_object
->backing_object
)) {
479 result
= KERN_FAILURE
;
485 * If the entry is wired we cannot change the page protection.
488 fault_type
= fs
.first_prot
;
491 * We generally want to avoid unnecessary exclusive modes on backing
492 * and terminal objects because this can seriously interfere with
493 * heavily fork()'d processes (particularly /bin/sh scripts).
495 * However, we also want to avoid unnecessary retries due to needed
496 * shared->exclusive promotion for common faults. Exclusive mode is
497 * always needed if any page insertion, rename, or free occurs in an
498 * object (and also indirectly if any I/O is done).
500 * The main issue here is going to be fs.first_shared. If the
501 * first_object has a backing object which isn't shadowed and the
502 * process is single-threaded we might as well use an exclusive
503 * lock/chain right off the bat.
505 if (fs
.first_shared
&& fs
.first_object
->backing_object
&&
506 LIST_EMPTY(&fs
.first_object
->shadow_head
) &&
507 td
->td_proc
&& td
->td_proc
->p_nthreads
== 1) {
512 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
513 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
514 * we can try shared first.
516 if (fault_flags
& VM_FAULT_UNSWAP
) {
521 * Obtain a top-level object lock, shared or exclusive depending
522 * on fs.first_shared. If a shared lock winds up being insufficient
523 * we will retry with an exclusive lock.
525 * The vnode pager lock is always shared.
528 vm_object_hold_shared(fs
.first_object
);
530 vm_object_hold(fs
.first_object
);
532 fs
.vp
= vnode_pager_lock(fs
.first_object
);
535 * The page we want is at (first_object, first_pindex), but if the
536 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
537 * page table to figure out the actual pindex.
539 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
543 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
544 vm_map_interlock(fs
.map
, &ilock
, vaddr
, vaddr
+ PAGE_SIZE
);
546 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
547 fs
.entry
->aux
.master_pde
,
549 if (result
== KERN_TRY_AGAIN
) {
550 vm_map_deinterlock(fs
.map
, &ilock
);
551 vm_object_drop(fs
.first_object
);
555 if (result
!= KERN_SUCCESS
) {
556 vm_map_deinterlock(fs
.map
, &ilock
);
562 * Now we have the actual (object, pindex), fault in the page. If
563 * vm_fault_object() fails it will unlock and deallocate the FS
564 * data. If it succeeds everything remains locked and fs->object
565 * will have an additional PIP count if it is not equal to
568 * vm_fault_object will set fs->prot for the pmap operation. It is
569 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
570 * page can be safely written. However, it will force a read-only
571 * mapping for a read fault if the memory is managed by a virtual
574 * If the fault code uses the shared object lock shortcut
575 * we must not try to burst (we can't allocate VM pages).
577 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
579 if (debug_fault
> 0) {
581 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
582 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
583 result
, (intmax_t)vaddr
, fault_type
, fault_flags
,
584 fs
.m
, fs
.prot
, fs
.wired
, fs
.entry
);
587 if (result
== KERN_TRY_AGAIN
) {
589 vm_map_deinterlock(fs
.map
, &ilock
);
590 vm_object_drop(fs
.first_object
);
594 if (result
!= KERN_SUCCESS
) {
596 vm_map_deinterlock(fs
.map
, &ilock
);
601 * On success vm_fault_object() does not unlock or deallocate, and fs.m
602 * will contain a busied page.
604 * Enter the page into the pmap and do pmap-related adjustments.
606 KKASSERT(fs
.lookup_still_valid
== TRUE
);
607 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
608 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
| inherit_prot
,
612 vm_map_deinterlock(fs
.map
, &ilock
);
614 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
615 KKASSERT(fs
.m
->flags
& PG_BUSY
);
618 * If the page is not wired down, then put it where the pageout daemon
621 if (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) {
625 vm_page_unwire(fs
.m
, 1);
627 vm_page_activate(fs
.m
);
629 vm_page_wakeup(fs
.m
);
632 * Burst in a few more pages if possible. The fs.map should still
633 * be locked. To avoid interlocking against a vnode->getblk
634 * operation we had to be sure to unbusy our primary vm_page above
637 * A normal burst can continue down backing store, only execute
638 * if we are holding an exclusive lock, otherwise the exclusive
639 * locks the burst code gets might cause excessive SMP collisions.
641 * A quick burst can be utilized when there is no backing object
642 * (i.e. a shared file mmap).
644 if ((fault_flags
& VM_FAULT_BURST
) &&
645 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) == 0 &&
647 if (fs
.first_shared
== 0 && fs
.shared
== 0) {
648 vm_prefault(fs
.map
->pmap
, vaddr
,
649 fs
.entry
, fs
.prot
, fault_flags
);
651 vm_prefault_quick(fs
.map
->pmap
, vaddr
,
652 fs
.entry
, fs
.prot
, fault_flags
);
657 mycpu
->gd_cnt
.v_vm_faults
++;
659 ++td
->td_lwp
->lwp_ru
.ru_minflt
;
662 * Unlock everything, and return
668 td
->td_lwp
->lwp_ru
.ru_majflt
++;
670 td
->td_lwp
->lwp_ru
.ru_minflt
++;
674 /*vm_object_deallocate(fs.first_object);*/
676 /*fs.first_object = NULL; must still drop later */
678 result
= KERN_SUCCESS
;
681 vm_object_drop(fs
.first_object
);
684 lp
->lwp_flags
&= ~LWP_PAGING
;
686 #if !defined(NO_SWAPPING)
688 * Check the process RSS limit and force deactivation and
689 * (asynchronous) paging if necessary. This is a complex operation,
690 * only do it for direct user-mode faults, for now.
692 * To reduce overhead implement approximately a ~16MB hysteresis.
695 if ((fault_flags
& VM_FAULT_USERMODE
) && lp
&&
696 p
->p_limit
&& map
->pmap
&& vm_pageout_memuse_mode
>= 1 &&
697 map
!= &kernel_map
) {
701 limit
= OFF_TO_IDX(qmin(p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
,
702 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
));
703 size
= pmap_resident_tlnw_count(map
->pmap
);
704 if (limit
>= 0 && size
> 4096 && size
- 4096 >= limit
) {
705 vm_pageout_map_deactivate_pages(map
, limit
);
714 * Fault in the specified virtual address in the current process map,
715 * returning a held VM page or NULL. See vm_fault_page() for more
721 vm_fault_page_quick(vm_offset_t va
, vm_prot_t fault_type
,
722 int *errorp
, int *busyp
)
724 struct lwp
*lp
= curthread
->td_lwp
;
727 m
= vm_fault_page(&lp
->lwp_vmspace
->vm_map
, va
,
728 fault_type
, VM_FAULT_NORMAL
,
734 * Fault in the specified virtual address in the specified map, doing all
735 * necessary manipulation of the object store and all necessary I/O. Return
736 * a held VM page or NULL, and set *errorp. The related pmap is not
739 * If busyp is not NULL then *busyp will be set to TRUE if this routine
740 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
741 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is
742 * NULL the returned page is only held.
744 * If the caller has no intention of writing to the page's contents, busyp
745 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
746 * without busying the page.
748 * The returned page will also be marked PG_REFERENCED.
750 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
751 * error will be returned.
756 vm_fault_page(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
,
757 int fault_flags
, int *errorp
, int *busyp
)
759 vm_pindex_t first_pindex
;
760 struct faultstate fs
;
764 vm_prot_t orig_fault_type
= fault_type
;
768 fs
.fault_flags
= fault_flags
;
769 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
772 * Dive the pmap (concurrency possible). If we find the
773 * appropriate page we can terminate early and quickly.
775 * This works great for normal programs but will always return
776 * NULL for host lookups of vkernel maps in VMM mode.
778 fs
.m
= pmap_fault_page_quick(map
->pmap
, vaddr
, fault_type
, busyp
);
780 if (fault_type
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
))
787 * Otherwise take a concurrency hit and do a formal page
791 fs
.shared
= vm_shared_fault
;
792 fs
.first_shared
= vm_shared_fault
;
796 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
797 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
798 * we can try shared first.
800 if (fault_flags
& VM_FAULT_UNSWAP
) {
806 * Find the vm_map_entry representing the backing store and resolve
807 * the top level object and page index. This may have the side
808 * effect of executing a copy-on-write on the map entry and/or
809 * creating a shadow object, but will not COW any actual VM pages.
811 * On success fs.map is left read-locked and various other fields
812 * are initialized but not otherwise referenced or locked.
814 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
815 * if the map entry is a virtual page table and also writable,
816 * so we can set the 'A'accessed bit in the virtual page table
820 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
821 &fs
.entry
, &fs
.first_object
,
822 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
824 if (result
!= KERN_SUCCESS
) {
825 if (result
== KERN_FAILURE_NOFAULT
) {
826 *errorp
= KERN_FAILURE
;
830 if (result
!= KERN_PROTECTION_FAILURE
||
831 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
833 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
834 map
!= &kernel_map
&& curproc
!= NULL
) {
835 result
= vm_map_growstack(map
, vaddr
);
836 if (result
== KERN_SUCCESS
) {
841 result
= KERN_FAILURE
;
849 * If we are user-wiring a r/w segment, and it is COW, then
850 * we need to do the COW operation. Note that we don't
851 * currently COW RO sections now, because it is NOT desirable
852 * to COW .text. We simply keep .text from ever being COW'ed
853 * and take the heat that one cannot debug wired .text sections.
855 result
= vm_map_lookup(&fs
.map
, vaddr
,
856 VM_PROT_READ
|VM_PROT_WRITE
|
857 VM_PROT_OVERRIDE_WRITE
,
858 &fs
.entry
, &fs
.first_object
,
859 &first_pindex
, &fs
.first_prot
,
861 if (result
!= KERN_SUCCESS
) {
862 /* could also be KERN_FAILURE_NOFAULT */
863 *errorp
= KERN_FAILURE
;
869 * If we don't COW now, on a user wire, the user will never
870 * be able to write to the mapping. If we don't make this
871 * restriction, the bookkeeping would be nearly impossible.
873 * XXX We have a shared lock, this will have a MP race but
874 * I don't see how it can hurt anything.
876 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
877 atomic_clear_char(&fs
.entry
->max_protection
,
883 * fs.map is read-locked
885 * Misc checks. Save the map generation number to detect races.
887 fs
.map_generation
= fs
.map
->timestamp
;
888 fs
.lookup_still_valid
= TRUE
;
890 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
892 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
893 panic("vm_fault: fault on nofault entry, addr: %lx",
898 * A user-kernel shared map has no VM object and bypasses
899 * everything. We execute the uksmap function with a temporary
900 * fictitious vm_page. The address is directly mapped with no
903 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
904 struct vm_page fakem
;
906 bzero(&fakem
, sizeof(fakem
));
907 fakem
.pindex
= first_pindex
;
908 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
909 fakem
.valid
= VM_PAGE_BITS_ALL
;
910 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
911 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
912 *errorp
= KERN_FAILURE
;
917 fs
.m
= PHYS_TO_VM_PAGE(fakem
.phys_addr
);
920 *busyp
= 0; /* don't need to busy R or W */
928 * A system map entry may return a NULL object. No object means
929 * no pager means an unrecoverable kernel fault.
931 if (fs
.first_object
== NULL
) {
932 panic("vm_fault: unrecoverable fault at %p in entry %p",
933 (void *)vaddr
, fs
.entry
);
937 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
940 * Unfortunately a deadlock can occur if we are forced to page-in
941 * from swap, but diving all the way into the vm_pager_get_page()
942 * function to find out is too much. Just check the object type.
944 if ((curthread
->td_flags
& TDF_NOFAULT
) &&
946 fs
.first_object
->type
== OBJT_VNODE
||
947 fs
.first_object
->type
== OBJT_SWAP
||
948 fs
.first_object
->backing_object
)) {
949 *errorp
= KERN_FAILURE
;
956 * If the entry is wired we cannot change the page protection.
959 fault_type
= fs
.first_prot
;
962 * Make a reference to this object to prevent its disposal while we
963 * are messing with it. Once we have the reference, the map is free
964 * to be diddled. Since objects reference their shadows (and copies),
965 * they will stay around as well.
967 * The reference should also prevent an unexpected collapse of the
968 * parent that might move pages from the current object into the
969 * parent unexpectedly, resulting in corruption.
971 * Bump the paging-in-progress count to prevent size changes (e.g.
972 * truncation operations) during I/O. This must be done after
973 * obtaining the vnode lock in order to avoid possible deadlocks.
976 vm_object_hold_shared(fs
.first_object
);
978 vm_object_hold(fs
.first_object
);
980 fs
.vp
= vnode_pager_lock(fs
.first_object
); /* shared */
983 * The page we want is at (first_object, first_pindex), but if the
984 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
985 * page table to figure out the actual pindex.
987 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
990 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
991 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
992 fs
.entry
->aux
.master_pde
,
994 if (result
== KERN_TRY_AGAIN
) {
995 vm_object_drop(fs
.first_object
);
999 if (result
!= KERN_SUCCESS
) {
1007 * Now we have the actual (object, pindex), fault in the page. If
1008 * vm_fault_object() fails it will unlock and deallocate the FS
1009 * data. If it succeeds everything remains locked and fs->object
1010 * will have an additinal PIP count if it is not equal to
1014 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
1016 if (result
== KERN_TRY_AGAIN
) {
1017 vm_object_drop(fs
.first_object
);
1021 if (result
!= KERN_SUCCESS
) {
1027 if ((orig_fault_type
& VM_PROT_WRITE
) &&
1028 (fs
.prot
& VM_PROT_WRITE
) == 0) {
1029 *errorp
= KERN_PROTECTION_FAILURE
;
1030 unlock_and_deallocate(&fs
);
1036 * DO NOT UPDATE THE PMAP!!! This function may be called for
1037 * a pmap unrelated to the current process pmap, in which case
1038 * the current cpu core will not be listed in the pmap's pm_active
1039 * mask. Thus invalidation interlocks will fail to work properly.
1041 * (for example, 'ps' uses procfs to read program arguments from
1042 * each process's stack).
1044 * In addition to the above this function will be called to acquire
1045 * a page that might already be faulted in, re-faulting it
1046 * continuously is a waste of time.
1048 * XXX could this have been the cause of our random seg-fault
1049 * issues? procfs accesses user stacks.
1051 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1053 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
, fs
.wired
, NULL
);
1054 mycpu
->gd_cnt
.v_vm_faults
++;
1055 if (curthread
->td_lwp
)
1056 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
1060 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1061 * will contain a busied page. So we must unlock here after having
1062 * messed with the pmap.
1067 * Return a held page. We are not doing any pmap manipulation so do
1068 * not set PG_MAPPED. However, adjust the page flags according to
1069 * the fault type because the caller may not use a managed pmapping
1070 * (so we don't want to lose the fact that the page will be dirtied
1071 * if a write fault was specified).
1073 if (fault_type
& VM_PROT_WRITE
)
1074 vm_page_dirty(fs
.m
);
1075 vm_page_activate(fs
.m
);
1077 if (curthread
->td_lwp
) {
1079 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1081 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1086 * Unlock everything, and return the held or busied page.
1089 if (fault_type
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
)) {
1090 vm_page_dirty(fs
.m
);
1095 vm_page_wakeup(fs
.m
);
1099 vm_page_wakeup(fs
.m
);
1101 /*vm_object_deallocate(fs.first_object);*/
1102 /*fs.first_object = NULL; */
1106 if (fs
.first_object
)
1107 vm_object_drop(fs
.first_object
);
1113 * Fault in the specified (object,offset), dirty the returned page as
1114 * needed. If the requested fault_type cannot be done NULL and an
1115 * error is returned.
1117 * A held (but not busied) page is returned.
1119 * The passed in object must be held as specified by the shared
1123 vm_fault_object_page(vm_object_t object
, vm_ooffset_t offset
,
1124 vm_prot_t fault_type
, int fault_flags
,
1125 int *sharedp
, int *errorp
)
1128 vm_pindex_t first_pindex
;
1129 struct faultstate fs
;
1130 struct vm_map_entry entry
;
1132 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
1133 bzero(&entry
, sizeof(entry
));
1134 entry
.object
.vm_object
= object
;
1135 entry
.maptype
= VM_MAPTYPE_NORMAL
;
1136 entry
.protection
= entry
.max_protection
= fault_type
;
1139 fs
.fault_flags
= fault_flags
;
1141 fs
.shared
= vm_shared_fault
;
1142 fs
.first_shared
= *sharedp
;
1144 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
1147 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1148 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
1149 * we can try shared first.
1151 if (fs
.first_shared
&& (fault_flags
& VM_FAULT_UNSWAP
)) {
1152 fs
.first_shared
= 0;
1153 vm_object_upgrade(object
);
1157 * Retry loop as needed (typically for shared->exclusive transitions)
1160 *sharedp
= fs
.first_shared
;
1161 first_pindex
= OFF_TO_IDX(offset
);
1162 fs
.first_object
= object
;
1164 fs
.first_prot
= fault_type
;
1166 /*fs.map_generation = 0; unused */
1169 * Make a reference to this object to prevent its disposal while we
1170 * are messing with it. Once we have the reference, the map is free
1171 * to be diddled. Since objects reference their shadows (and copies),
1172 * they will stay around as well.
1174 * The reference should also prevent an unexpected collapse of the
1175 * parent that might move pages from the current object into the
1176 * parent unexpectedly, resulting in corruption.
1178 * Bump the paging-in-progress count to prevent size changes (e.g.
1179 * truncation operations) during I/O. This must be done after
1180 * obtaining the vnode lock in order to avoid possible deadlocks.
1183 fs
.vp
= vnode_pager_lock(fs
.first_object
);
1185 fs
.lookup_still_valid
= TRUE
;
1187 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
1190 /* XXX future - ability to operate on VM object using vpagetable */
1191 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1192 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
1193 fs
.entry
->aux
.master_pde
,
1195 if (result
== KERN_TRY_AGAIN
) {
1196 if (fs
.first_shared
== 0 && *sharedp
)
1197 vm_object_upgrade(object
);
1200 if (result
!= KERN_SUCCESS
) {
1208 * Now we have the actual (object, pindex), fault in the page. If
1209 * vm_fault_object() fails it will unlock and deallocate the FS
1210 * data. If it succeeds everything remains locked and fs->object
1211 * will have an additinal PIP count if it is not equal to
1214 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1215 * We may have to upgrade its lock to handle the requested fault.
1217 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 0);
1219 if (result
== KERN_TRY_AGAIN
) {
1220 if (fs
.first_shared
== 0 && *sharedp
)
1221 vm_object_upgrade(object
);
1224 if (result
!= KERN_SUCCESS
) {
1229 if ((fault_type
& VM_PROT_WRITE
) && (fs
.prot
& VM_PROT_WRITE
) == 0) {
1230 *errorp
= KERN_PROTECTION_FAILURE
;
1231 unlock_and_deallocate(&fs
);
1236 * On success vm_fault_object() does not unlock or deallocate, so we
1237 * do it here. Note that the returned fs.m will be busied.
1242 * Return a held page. We are not doing any pmap manipulation so do
1243 * not set PG_MAPPED. However, adjust the page flags according to
1244 * the fault type because the caller may not use a managed pmapping
1245 * (so we don't want to lose the fact that the page will be dirtied
1246 * if a write fault was specified).
1249 vm_page_activate(fs
.m
);
1250 if ((fault_type
& VM_PROT_WRITE
) || (fault_flags
& VM_FAULT_DIRTY
))
1251 vm_page_dirty(fs
.m
);
1252 if (fault_flags
& VM_FAULT_UNSWAP
)
1253 swap_pager_unswapped(fs
.m
);
1256 * Indicate that the page was accessed.
1258 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1260 if (curthread
->td_lwp
) {
1262 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1264 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1269 * Unlock everything, and return the held page.
1271 vm_page_wakeup(fs
.m
);
1272 /*vm_object_deallocate(fs.first_object);*/
1273 /*fs.first_object = NULL; */
1280 * Translate the virtual page number (first_pindex) that is relative
1281 * to the address space into a logical page number that is relative to the
1282 * backing object. Use the virtual page table pointed to by (vpte).
1284 * Possibly downgrade the protection based on the vpte bits.
1286 * This implements an N-level page table. Any level can terminate the
1287 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1288 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1292 vm_fault_vpagetable(struct faultstate
*fs
, vm_pindex_t
*pindex
,
1293 vpte_t vpte
, int fault_type
, int allow_nofault
)
1296 struct lwbuf lwb_cache
;
1297 int vshift
= VPTE_FRAME_END
- PAGE_SHIFT
; /* index bits remaining */
1301 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1304 * We cannot proceed if the vpte is not valid, not readable
1305 * for a read fault, or not writable for a write fault.
1307 if ((vpte
& VPTE_V
) == 0) {
1308 unlock_and_deallocate(fs
);
1309 return (KERN_FAILURE
);
1311 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
) == 0) {
1312 unlock_and_deallocate(fs
);
1313 return (KERN_FAILURE
);
1315 if ((vpte
& VPTE_PS
) || vshift
== 0)
1319 * Get the page table page. Nominally we only read the page
1320 * table, but since we are actively setting VPTE_M and VPTE_A,
1321 * tell vm_fault_object() that we are writing it.
1323 * There is currently no real need to optimize this.
1325 result
= vm_fault_object(fs
, (vpte
& VPTE_FRAME
) >> PAGE_SHIFT
,
1326 VM_PROT_READ
|VM_PROT_WRITE
,
1328 if (result
!= KERN_SUCCESS
)
1332 * Process the returned fs.m and look up the page table
1333 * entry in the page table page.
1335 vshift
-= VPTE_PAGE_BITS
;
1336 lwb
= lwbuf_alloc(fs
->m
, &lwb_cache
);
1337 ptep
= ((vpte_t
*)lwbuf_kva(lwb
) +
1338 ((*pindex
>> vshift
) & VPTE_PAGE_MASK
));
1339 vm_page_activate(fs
->m
);
1342 * Page table write-back - entire operation including
1343 * validation of the pte must be atomic to avoid races
1344 * against the vkernel changing the pte.
1346 * If the vpte is valid for the* requested operation, do
1347 * a write-back to the page table.
1349 * XXX VPTE_M is not set properly for page directory pages.
1350 * It doesn't get set in the page directory if the page table
1351 * is modified during a read access.
1357 * Reload for the cmpset, but make sure the pte is
1364 if ((vpte
& VPTE_V
) == 0)
1367 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
))
1368 nvpte
|= VPTE_M
| VPTE_A
;
1369 if (fault_type
& VM_PROT_READ
)
1373 if (atomic_cmpset_long(ptep
, vpte
, nvpte
)) {
1374 vm_page_dirty(fs
->m
);
1379 vm_page_flag_set(fs
->m
, PG_REFERENCED
);
1380 vm_page_wakeup(fs
->m
);
1382 cleanup_successful_fault(fs
);
1386 * When the vkernel sets VPTE_RW it expects the real kernel to
1387 * reflect VPTE_M back when the page is modified via the mapping.
1388 * In order to accomplish this the real kernel must map the page
1389 * read-only for read faults and use write faults to reflect VPTE_M
1392 * Once VPTE_M has been set, the real kernel's pte allows writing.
1393 * If the vkernel clears VPTE_M the vkernel must be sure to
1394 * MADV_INVAL the real kernel's mappings to force the real kernel
1395 * to re-fault on the next write so oit can set VPTE_M again.
1397 if ((fault_type
& VM_PROT_WRITE
) == 0 &&
1398 (vpte
& (VPTE_RW
| VPTE_M
)) != (VPTE_RW
| VPTE_M
)) {
1399 fs
->first_prot
&= ~VM_PROT_WRITE
;
1403 * Combine remaining address bits with the vpte.
1405 *pindex
= ((vpte
& VPTE_FRAME
) >> PAGE_SHIFT
) +
1406 (*pindex
& ((1L << vshift
) - 1));
1407 return (KERN_SUCCESS
);
1412 * This is the core of the vm_fault code.
1414 * Do all operations required to fault-in (fs.first_object, pindex). Run
1415 * through the shadow chain as necessary and do required COW or virtual
1416 * copy operations. The caller has already fully resolved the vm_map_entry
1417 * and, if appropriate, has created a copy-on-write layer. All we need to
1418 * do is iterate the object chain.
1420 * On failure (fs) is unlocked and deallocated and the caller may return or
1421 * retry depending on the failure code. On success (fs) is NOT unlocked or
1422 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1423 * will have an additional PIP count if it is not equal to fs.first_object.
1425 * If locks based on fs->first_shared or fs->shared are insufficient,
1426 * clear the appropriate field(s) and return RETRY. COWs require that
1427 * first_shared be 0, while page allocations (or frees) require that
1428 * shared be 0. Renames require that both be 0.
1430 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1431 * we will have to retry with it exclusive if the vm_page is
1434 * fs->first_object must be held on call.
1438 vm_fault_object(struct faultstate
*fs
, vm_pindex_t first_pindex
,
1439 vm_prot_t fault_type
, int allow_nofault
)
1441 vm_object_t next_object
;
1445 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1446 fs
->prot
= fs
->first_prot
;
1447 fs
->object
= fs
->first_object
;
1448 pindex
= first_pindex
;
1450 vm_object_chain_acquire(fs
->first_object
, fs
->shared
);
1451 vm_object_pip_add(fs
->first_object
, 1);
1454 * If a read fault occurs we try to upgrade the page protection
1455 * and make it also writable if possible. There are three cases
1456 * where we cannot make the page mapping writable:
1458 * (1) The mapping is read-only or the VM object is read-only,
1459 * fs->prot above will simply not have VM_PROT_WRITE set.
1461 * (2) If the mapping is a virtual page table fs->first_prot will
1462 * have already been properly adjusted by vm_fault_vpagetable().
1463 * to detect writes so we can set VPTE_M in the virtual page
1464 * table. Used by vkernels.
1466 * (3) If the VM page is read-only or copy-on-write, upgrading would
1467 * just result in an unnecessary COW fault.
1469 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1473 /* see vpagetable code */
1474 if (fs
->entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1475 if ((fault_type
& VM_PROT_WRITE
) == 0)
1476 fs
->prot
&= ~VM_PROT_WRITE
;
1480 if (curthread
->td_lwp
&& curthread
->td_lwp
->lwp_vmspace
&&
1481 pmap_emulate_ad_bits(&curthread
->td_lwp
->lwp_vmspace
->vm_pmap
)) {
1482 if ((fault_type
& VM_PROT_WRITE
) == 0)
1483 fs
->prot
&= ~VM_PROT_WRITE
;
1486 /* vm_object_hold(fs->object); implied b/c object == first_object */
1490 * The entire backing chain from first_object to object
1491 * inclusive is chainlocked.
1493 * If the object is dead, we stop here
1495 if (fs
->object
->flags
& OBJ_DEAD
) {
1496 vm_object_pip_wakeup(fs
->first_object
);
1497 vm_object_chain_release_all(fs
->first_object
,
1499 if (fs
->object
!= fs
->first_object
)
1500 vm_object_drop(fs
->object
);
1501 unlock_and_deallocate(fs
);
1502 return (KERN_PROTECTION_FAILURE
);
1506 * See if the page is resident. Wait/Retry if the page is
1507 * busy (lots of stuff may have changed so we can't continue
1510 * We can theoretically allow the soft-busy case on a read
1511 * fault if the page is marked valid, but since such
1512 * pages are typically already pmap'd, putting that
1513 * special case in might be more effort then it is
1514 * worth. We cannot under any circumstances mess
1515 * around with a vm_page_t->busy page except, perhaps,
1518 fs
->m
= vm_page_lookup_busy_try(fs
->object
, pindex
,
1521 vm_object_pip_wakeup(fs
->first_object
);
1522 vm_object_chain_release_all(fs
->first_object
,
1524 if (fs
->object
!= fs
->first_object
)
1525 vm_object_drop(fs
->object
);
1527 vm_page_sleep_busy(fs
->m
, TRUE
, "vmpfw");
1528 mycpu
->gd_cnt
.v_intrans
++;
1529 /*vm_object_deallocate(fs->first_object);*/
1530 /*fs->first_object = NULL;*/
1532 return (KERN_TRY_AGAIN
);
1536 * The page is busied for us.
1538 * If reactivating a page from PQ_CACHE we may have
1541 int queue
= fs
->m
->queue
;
1542 vm_page_unqueue_nowakeup(fs
->m
);
1544 if ((queue
- fs
->m
->pc
) == PQ_CACHE
&&
1545 vm_page_count_severe()) {
1546 vm_page_activate(fs
->m
);
1547 vm_page_wakeup(fs
->m
);
1549 vm_object_pip_wakeup(fs
->first_object
);
1550 vm_object_chain_release_all(fs
->first_object
,
1552 if (fs
->object
!= fs
->first_object
)
1553 vm_object_drop(fs
->object
);
1554 unlock_and_deallocate(fs
);
1555 if (allow_nofault
== 0 ||
1556 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1561 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1562 return (KERN_PROTECTION_FAILURE
);
1564 return (KERN_TRY_AGAIN
);
1568 * If it still isn't completely valid (readable),
1569 * or if a read-ahead-mark is set on the VM page,
1570 * jump to readrest, else we found the page and
1573 * We can release the spl once we have marked the
1576 if (fs
->m
->object
!= &kernel_object
) {
1577 if ((fs
->m
->valid
& VM_PAGE_BITS_ALL
) !=
1581 if (fs
->m
->flags
& PG_RAM
) {
1584 vm_page_flag_clear(fs
->m
, PG_RAM
);
1588 break; /* break to PAGE HAS BEEN FOUND */
1592 * Page is not resident, If this is the search termination
1593 * or the pager might contain the page, allocate a new page.
1595 if (TRYPAGER(fs
) || fs
->object
== fs
->first_object
) {
1597 * Allocating, must be exclusive.
1599 if (fs
->object
== fs
->first_object
&&
1601 fs
->first_shared
= 0;
1602 vm_object_pip_wakeup(fs
->first_object
);
1603 vm_object_chain_release_all(fs
->first_object
,
1605 if (fs
->object
!= fs
->first_object
)
1606 vm_object_drop(fs
->object
);
1607 unlock_and_deallocate(fs
);
1608 return (KERN_TRY_AGAIN
);
1610 if (fs
->object
!= fs
->first_object
&&
1612 fs
->first_shared
= 0;
1614 vm_object_pip_wakeup(fs
->first_object
);
1615 vm_object_chain_release_all(fs
->first_object
,
1617 if (fs
->object
!= fs
->first_object
)
1618 vm_object_drop(fs
->object
);
1619 unlock_and_deallocate(fs
);
1620 return (KERN_TRY_AGAIN
);
1624 * If the page is beyond the object size we fail
1626 if (pindex
>= fs
->object
->size
) {
1627 vm_object_pip_wakeup(fs
->first_object
);
1628 vm_object_chain_release_all(fs
->first_object
,
1630 if (fs
->object
!= fs
->first_object
)
1631 vm_object_drop(fs
->object
);
1632 unlock_and_deallocate(fs
);
1633 return (KERN_PROTECTION_FAILURE
);
1637 * Allocate a new page for this object/offset pair.
1639 * It is possible for the allocation to race, so
1643 if (!vm_page_count_severe()) {
1644 fs
->m
= vm_page_alloc(fs
->object
, pindex
,
1645 ((fs
->vp
|| fs
->object
->backing_object
) ?
1646 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
:
1647 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
|
1648 VM_ALLOC_USE_GD
| VM_ALLOC_ZERO
));
1650 if (fs
->m
== NULL
) {
1651 vm_object_pip_wakeup(fs
->first_object
);
1652 vm_object_chain_release_all(fs
->first_object
,
1654 if (fs
->object
!= fs
->first_object
)
1655 vm_object_drop(fs
->object
);
1656 unlock_and_deallocate(fs
);
1657 if (allow_nofault
== 0 ||
1658 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1663 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1664 return (KERN_PROTECTION_FAILURE
);
1666 return (KERN_TRY_AGAIN
);
1670 * Fall through to readrest. We have a new page which
1671 * will have to be paged (since m->valid will be 0).
1677 * We have found an invalid or partially valid page, a
1678 * page with a read-ahead mark which might be partially or
1679 * fully valid (and maybe dirty too), or we have allocated
1682 * Attempt to fault-in the page if there is a chance that the
1683 * pager has it, and potentially fault in additional pages
1686 * If TRYPAGER is true then fs.m will be non-NULL and busied
1692 u_char behavior
= vm_map_entry_behavior(fs
->entry
);
1694 if (behavior
== MAP_ENTRY_BEHAV_RANDOM
)
1700 * Doing I/O may synchronously insert additional
1701 * pages so we can't be shared at this point either.
1703 * NOTE: We can't free fs->m here in the allocated
1704 * case (fs->object != fs->first_object) as
1705 * this would require an exclusively locked
1708 if (fs
->object
== fs
->first_object
&&
1710 vm_page_deactivate(fs
->m
);
1711 vm_page_wakeup(fs
->m
);
1713 fs
->first_shared
= 0;
1714 vm_object_pip_wakeup(fs
->first_object
);
1715 vm_object_chain_release_all(fs
->first_object
,
1717 if (fs
->object
!= fs
->first_object
)
1718 vm_object_drop(fs
->object
);
1719 unlock_and_deallocate(fs
);
1720 return (KERN_TRY_AGAIN
);
1722 if (fs
->object
!= fs
->first_object
&&
1724 vm_page_deactivate(fs
->m
);
1725 vm_page_wakeup(fs
->m
);
1727 fs
->first_shared
= 0;
1729 vm_object_pip_wakeup(fs
->first_object
);
1730 vm_object_chain_release_all(fs
->first_object
,
1732 if (fs
->object
!= fs
->first_object
)
1733 vm_object_drop(fs
->object
);
1734 unlock_and_deallocate(fs
);
1735 return (KERN_TRY_AGAIN
);
1739 * Avoid deadlocking against the map when doing I/O.
1740 * fs.object and the page is PG_BUSY'd.
1742 * NOTE: Once unlocked, fs->entry can become stale
1743 * so this will NULL it out.
1745 * NOTE: fs->entry is invalid until we relock the
1746 * map and verify that the timestamp has not
1752 * Acquire the page data. We still hold a ref on
1753 * fs.object and the page has been PG_BUSY's.
1755 * The pager may replace the page (for example, in
1756 * order to enter a fictitious page into the
1757 * object). If it does so it is responsible for
1758 * cleaning up the passed page and properly setting
1759 * the new page PG_BUSY.
1761 * If we got here through a PG_RAM read-ahead
1762 * mark the page may be partially dirty and thus
1763 * not freeable. Don't bother checking to see
1764 * if the pager has the page because we can't free
1765 * it anyway. We have to depend on the get_page
1766 * operation filling in any gaps whether there is
1767 * backing store or not.
1769 rv
= vm_pager_get_page(fs
->object
, &fs
->m
, seqaccess
);
1771 if (rv
== VM_PAGER_OK
) {
1773 * Relookup in case pager changed page. Pager
1774 * is responsible for disposition of old page
1777 * XXX other code segments do relookups too.
1778 * It's a bad abstraction that needs to be
1781 fs
->m
= vm_page_lookup(fs
->object
, pindex
);
1782 if (fs
->m
== NULL
) {
1783 vm_object_pip_wakeup(fs
->first_object
);
1784 vm_object_chain_release_all(
1785 fs
->first_object
, fs
->object
);
1786 if (fs
->object
!= fs
->first_object
)
1787 vm_object_drop(fs
->object
);
1788 unlock_and_deallocate(fs
);
1789 return (KERN_TRY_AGAIN
);
1792 break; /* break to PAGE HAS BEEN FOUND */
1796 * Remove the bogus page (which does not exist at this
1797 * object/offset); before doing so, we must get back
1798 * our object lock to preserve our invariant.
1800 * Also wake up any other process that may want to bring
1803 * If this is the top-level object, we must leave the
1804 * busy page to prevent another process from rushing
1805 * past us, and inserting the page in that object at
1806 * the same time that we are.
1808 if (rv
== VM_PAGER_ERROR
) {
1810 kprintf("vm_fault: pager read error, "
1815 kprintf("vm_fault: pager read error, "
1823 * Data outside the range of the pager or an I/O error
1825 * The page may have been wired during the pagein,
1826 * e.g. by the buffer cache, and cannot simply be
1827 * freed. Call vnode_pager_freepage() to deal with it.
1829 * Also note that we cannot free the page if we are
1830 * holding the related object shared. XXX not sure
1831 * what to do in that case.
1833 if (fs
->object
!= fs
->first_object
) {
1834 vnode_pager_freepage(fs
->m
);
1837 * XXX - we cannot just fall out at this
1838 * point, m has been freed and is invalid!
1842 * XXX - the check for kernel_map is a kludge to work
1843 * around having the machine panic on a kernel space
1844 * fault w/ I/O error.
1846 if (((fs
->map
!= &kernel_map
) &&
1847 (rv
== VM_PAGER_ERROR
)) || (rv
== VM_PAGER_BAD
)) {
1849 if (fs
->first_shared
) {
1850 vm_page_deactivate(fs
->m
);
1851 vm_page_wakeup(fs
->m
);
1853 vnode_pager_freepage(fs
->m
);
1857 vm_object_pip_wakeup(fs
->first_object
);
1858 vm_object_chain_release_all(fs
->first_object
,
1860 if (fs
->object
!= fs
->first_object
)
1861 vm_object_drop(fs
->object
);
1862 unlock_and_deallocate(fs
);
1863 if (rv
== VM_PAGER_ERROR
)
1864 return (KERN_FAILURE
);
1866 return (KERN_PROTECTION_FAILURE
);
1872 * We get here if the object has a default pager (or unwiring)
1873 * or the pager doesn't have the page.
1875 * fs->first_m will be used for the COW unless we find a
1876 * deeper page to be mapped read-only, in which case the
1877 * unlock*(fs) will free first_m.
1879 if (fs
->object
== fs
->first_object
)
1880 fs
->first_m
= fs
->m
;
1883 * Move on to the next object. The chain lock should prevent
1884 * the backing_object from getting ripped out from under us.
1886 * The object lock for the next object is governed by
1889 if ((next_object
= fs
->object
->backing_object
) != NULL
) {
1891 vm_object_hold_shared(next_object
);
1893 vm_object_hold(next_object
);
1894 vm_object_chain_acquire(next_object
, fs
->shared
);
1895 KKASSERT(next_object
== fs
->object
->backing_object
);
1896 pindex
+= OFF_TO_IDX(fs
->object
->backing_object_offset
);
1899 if (next_object
== NULL
) {
1901 * If there's no object left, fill the page in the top
1902 * object with zeros.
1904 if (fs
->object
!= fs
->first_object
) {
1906 if (fs
->first_object
->backing_object
!=
1908 vm_object_hold(fs
->first_object
->backing_object
);
1911 vm_object_chain_release_all(
1912 fs
->first_object
->backing_object
,
1915 if (fs
->first_object
->backing_object
!=
1917 vm_object_drop(fs
->first_object
->backing_object
);
1920 vm_object_pip_wakeup(fs
->object
);
1921 vm_object_drop(fs
->object
);
1922 fs
->object
= fs
->first_object
;
1923 pindex
= first_pindex
;
1924 fs
->m
= fs
->first_m
;
1929 * Zero the page and mark it valid.
1931 vm_page_zero_fill(fs
->m
);
1932 mycpu
->gd_cnt
.v_zfod
++;
1933 fs
->m
->valid
= VM_PAGE_BITS_ALL
;
1934 break; /* break to PAGE HAS BEEN FOUND */
1936 if (fs
->object
!= fs
->first_object
) {
1937 vm_object_pip_wakeup(fs
->object
);
1938 vm_object_lock_swap();
1939 vm_object_drop(fs
->object
);
1941 KASSERT(fs
->object
!= next_object
,
1942 ("object loop %p", next_object
));
1943 fs
->object
= next_object
;
1944 vm_object_pip_add(fs
->object
, 1);
1948 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1951 * object still held.
1953 * local shared variable may be different from fs->shared.
1955 * If the page is being written, but isn't already owned by the
1956 * top-level object, we have to copy it into a new page owned by the
1959 KASSERT((fs
->m
->flags
& PG_BUSY
) != 0,
1960 ("vm_fault: not busy after main loop"));
1962 if (fs
->object
!= fs
->first_object
) {
1964 * We only really need to copy if we want to write it.
1966 if (fault_type
& VM_PROT_WRITE
) {
1968 * This allows pages to be virtually copied from a
1969 * backing_object into the first_object, where the
1970 * backing object has no other refs to it, and cannot
1971 * gain any more refs. Instead of a bcopy, we just
1972 * move the page from the backing object to the
1973 * first object. Note that we must mark the page
1974 * dirty in the first object so that it will go out
1975 * to swap when needed.
1979 * Must be holding exclusive locks
1981 fs
->first_shared
== 0 &&
1984 * Map, if present, has not changed
1987 fs
->map_generation
== fs
->map
->timestamp
) &&
1989 * Only one shadow object
1991 (fs
->object
->shadow_count
== 1) &&
1993 * No COW refs, except us
1995 (fs
->object
->ref_count
== 1) &&
1997 * No one else can look this object up
1999 (fs
->object
->handle
== NULL
) &&
2001 * No other ways to look the object up
2003 ((fs
->object
->type
== OBJT_DEFAULT
) ||
2004 (fs
->object
->type
== OBJT_SWAP
)) &&
2006 * We don't chase down the shadow chain
2008 (fs
->object
== fs
->first_object
->backing_object
) &&
2011 * grab the lock if we need to
2013 (fs
->lookup_still_valid
||
2015 lockmgr(&fs
->map
->lock
, LK_EXCLUSIVE
|LK_NOWAIT
) == 0)
2018 * (first_m) and (m) are both busied. We have
2019 * move (m) into (first_m)'s object/pindex
2020 * in an atomic fashion, then free (first_m).
2022 * first_object is held so second remove
2023 * followed by the rename should wind
2024 * up being atomic. vm_page_free() might
2025 * block so we don't do it until after the
2028 fs
->lookup_still_valid
= 1;
2029 vm_page_protect(fs
->first_m
, VM_PROT_NONE
);
2030 vm_page_remove(fs
->first_m
);
2031 vm_page_rename(fs
->m
, fs
->first_object
,
2033 vm_page_free(fs
->first_m
);
2034 fs
->first_m
= fs
->m
;
2036 mycpu
->gd_cnt
.v_cow_optim
++;
2039 * Oh, well, lets copy it.
2041 * Why are we unmapping the original page
2042 * here? Well, in short, not all accessors
2043 * of user memory go through the pmap. The
2044 * procfs code doesn't have access user memory
2045 * via a local pmap, so vm_fault_page*()
2046 * can't call pmap_enter(). And the umtx*()
2047 * code may modify the COW'd page via a DMAP
2048 * or kernel mapping and not via the pmap,
2049 * leaving the original page still mapped
2050 * read-only into the pmap.
2052 * So we have to remove the page from at
2053 * least the current pmap if it is in it.
2054 * Just remove it from all pmaps.
2056 KKASSERT(fs
->first_shared
== 0);
2057 vm_page_copy(fs
->m
, fs
->first_m
);
2058 vm_page_protect(fs
->m
, VM_PROT_NONE
);
2059 vm_page_event(fs
->m
, VMEVENT_COW
);
2063 * We no longer need the old page or object.
2069 * We intend to revert to first_object, undo the
2070 * chain lock through to that.
2073 if (fs
->first_object
->backing_object
!= fs
->object
)
2074 vm_object_hold(fs
->first_object
->backing_object
);
2076 vm_object_chain_release_all(
2077 fs
->first_object
->backing_object
,
2080 if (fs
->first_object
->backing_object
!= fs
->object
)
2081 vm_object_drop(fs
->first_object
->backing_object
);
2085 * fs->object != fs->first_object due to above
2088 vm_object_pip_wakeup(fs
->object
);
2089 vm_object_drop(fs
->object
);
2092 * Only use the new page below...
2094 mycpu
->gd_cnt
.v_cow_faults
++;
2095 fs
->m
= fs
->first_m
;
2096 fs
->object
= fs
->first_object
;
2097 pindex
= first_pindex
;
2100 * If it wasn't a write fault avoid having to copy
2101 * the page by mapping it read-only.
2103 fs
->prot
&= ~VM_PROT_WRITE
;
2108 * Relock the map if necessary, then check the generation count.
2109 * relock_map() will update fs->timestamp to account for the
2110 * relocking if necessary.
2112 * If the count has changed after relocking then all sorts of
2113 * crap may have happened and we have to retry.
2115 * NOTE: The relock_map() can fail due to a deadlock against
2116 * the vm_page we are holding BUSY.
2118 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
2119 if (relock_map(fs
) ||
2120 fs
->map
->timestamp
!= fs
->map_generation
) {
2122 vm_object_pip_wakeup(fs
->first_object
);
2123 vm_object_chain_release_all(fs
->first_object
,
2125 if (fs
->object
!= fs
->first_object
)
2126 vm_object_drop(fs
->object
);
2127 unlock_and_deallocate(fs
);
2128 return (KERN_TRY_AGAIN
);
2133 * If the fault is a write, we know that this page is being
2134 * written NOW so dirty it explicitly to save on pmap_is_modified()
2137 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2138 * if the page is already dirty to prevent data written with
2139 * the expectation of being synced from not being synced.
2140 * Likewise if this entry does not request NOSYNC then make
2141 * sure the page isn't marked NOSYNC. Applications sharing
2142 * data should use the same flags to avoid ping ponging.
2144 * Also tell the backing pager, if any, that it should remove
2145 * any swap backing since the page is now dirty.
2147 vm_page_activate(fs
->m
);
2148 if (fs
->prot
& VM_PROT_WRITE
) {
2149 vm_object_set_writeable_dirty(fs
->m
->object
);
2150 vm_set_nosync(fs
->m
, fs
->entry
);
2151 if (fs
->fault_flags
& VM_FAULT_DIRTY
) {
2152 vm_page_dirty(fs
->m
);
2153 if (fs
->m
->flags
& PG_SWAPPED
) {
2155 * If the page is swapped out we have to call
2156 * swap_pager_unswapped() which requires an
2157 * exclusive object lock. If we are shared,
2158 * we must clear the shared flag and retry.
2160 if ((fs
->object
== fs
->first_object
&&
2161 fs
->first_shared
) ||
2162 (fs
->object
!= fs
->first_object
&&
2164 vm_page_wakeup(fs
->m
);
2166 if (fs
->object
== fs
->first_object
)
2167 fs
->first_shared
= 0;
2170 vm_object_pip_wakeup(fs
->first_object
);
2171 vm_object_chain_release_all(
2172 fs
->first_object
, fs
->object
);
2173 if (fs
->object
!= fs
->first_object
)
2174 vm_object_drop(fs
->object
);
2175 unlock_and_deallocate(fs
);
2176 return (KERN_TRY_AGAIN
);
2178 swap_pager_unswapped(fs
->m
);
2183 vm_object_pip_wakeup(fs
->first_object
);
2184 vm_object_chain_release_all(fs
->first_object
, fs
->object
);
2185 if (fs
->object
!= fs
->first_object
)
2186 vm_object_drop(fs
->object
);
2189 * Page had better still be busy. We are still locked up and
2190 * fs->object will have another PIP reference if it is not equal
2191 * to fs->first_object.
2193 KASSERT(fs
->m
->flags
& PG_BUSY
,
2194 ("vm_fault: page %p not busy!", fs
->m
));
2197 * Sanity check: page must be completely valid or it is not fit to
2198 * map into user space. vm_pager_get_pages() ensures this.
2200 if (fs
->m
->valid
!= VM_PAGE_BITS_ALL
) {
2201 vm_page_zero_invalid(fs
->m
, TRUE
);
2202 kprintf("Warning: page %p partially invalid on fault\n", fs
->m
);
2205 return (KERN_SUCCESS
);
2209 * Wire down a range of virtual addresses in a map. The entry in question
2210 * should be marked in-transition and the map must be locked. We must
2211 * release the map temporarily while faulting-in the page to avoid a
2212 * deadlock. Note that the entry may be clipped while we are blocked but
2213 * will never be freed.
2218 vm_fault_wire(vm_map_t map
, vm_map_entry_t entry
,
2219 boolean_t user_wire
, int kmflags
)
2221 boolean_t fictitious
;
2232 wire_prot
= VM_PROT_READ
;
2233 fault_flags
= VM_FAULT_USER_WIRE
;
2235 wire_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
2236 fault_flags
= VM_FAULT_CHANGE_WIRING
;
2238 if (kmflags
& KM_NOTLBSYNC
)
2239 wire_prot
|= VM_PROT_NOSYNC
;
2241 pmap
= vm_map_pmap(map
);
2242 start
= entry
->start
;
2245 switch(entry
->maptype
) {
2246 case VM_MAPTYPE_NORMAL
:
2247 case VM_MAPTYPE_VPAGETABLE
:
2248 fictitious
= entry
->object
.vm_object
&&
2249 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2250 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2252 case VM_MAPTYPE_UKSMAP
:
2260 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2266 * We simulate a fault to get the page and enter it in the physical
2269 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2270 rv
= vm_fault(map
, va
, wire_prot
, fault_flags
);
2272 while (va
> start
) {
2274 m
= pmap_unwire(pmap
, va
);
2275 if (m
&& !fictitious
) {
2276 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2277 vm_page_unwire(m
, 1);
2292 * Unwire a range of virtual addresses in a map. The map should be
2296 vm_fault_unwire(vm_map_t map
, vm_map_entry_t entry
)
2298 boolean_t fictitious
;
2305 pmap
= vm_map_pmap(map
);
2306 start
= entry
->start
;
2308 fictitious
= entry
->object
.vm_object
&&
2309 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2310 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2311 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2315 * Since the pages are wired down, we must be able to get their
2316 * mappings from the physical map system.
2318 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2319 m
= pmap_unwire(pmap
, va
);
2320 if (m
&& !fictitious
) {
2321 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2322 vm_page_unwire(m
, 1);
2329 * Copy all of the pages from a wired-down map entry to another.
2331 * The source and destination maps must be locked for write.
2332 * The source and destination maps token must be held
2333 * The source map entry must be wired down (or be a sharing map
2334 * entry corresponding to a main map entry that is wired down).
2336 * No other requirements.
2338 * XXX do segment optimization
2341 vm_fault_copy_entry(vm_map_t dst_map
, vm_map_t src_map
,
2342 vm_map_entry_t dst_entry
, vm_map_entry_t src_entry
)
2344 vm_object_t dst_object
;
2345 vm_object_t src_object
;
2346 vm_ooffset_t dst_offset
;
2347 vm_ooffset_t src_offset
;
2353 src_object
= src_entry
->object
.vm_object
;
2354 src_offset
= src_entry
->offset
;
2357 * Create the top-level object for the destination entry. (Doesn't
2358 * actually shadow anything - we copy the pages directly.)
2360 vm_map_entry_allocate_object(dst_entry
);
2361 dst_object
= dst_entry
->object
.vm_object
;
2363 prot
= dst_entry
->max_protection
;
2366 * Loop through all of the pages in the entry's range, copying each
2367 * one from the source object (it should be there) to the destination
2370 vm_object_hold(src_object
);
2371 vm_object_hold(dst_object
);
2372 for (vaddr
= dst_entry
->start
, dst_offset
= 0;
2373 vaddr
< dst_entry
->end
;
2374 vaddr
+= PAGE_SIZE
, dst_offset
+= PAGE_SIZE
) {
2377 * Allocate a page in the destination object
2380 dst_m
= vm_page_alloc(dst_object
,
2381 OFF_TO_IDX(dst_offset
),
2383 if (dst_m
== NULL
) {
2386 } while (dst_m
== NULL
);
2389 * Find the page in the source object, and copy it in.
2390 * (Because the source is wired down, the page will be in
2393 src_m
= vm_page_lookup(src_object
,
2394 OFF_TO_IDX(dst_offset
+ src_offset
));
2396 panic("vm_fault_copy_wired: page missing");
2398 vm_page_copy(src_m
, dst_m
);
2399 vm_page_event(src_m
, VMEVENT_COW
);
2402 * Enter it in the pmap...
2404 pmap_enter(dst_map
->pmap
, vaddr
, dst_m
, prot
, FALSE
, dst_entry
);
2407 * Mark it no longer busy, and put it on the active list.
2409 vm_page_activate(dst_m
);
2410 vm_page_wakeup(dst_m
);
2412 vm_object_drop(dst_object
);
2413 vm_object_drop(src_object
);
2419 * This routine checks around the requested page for other pages that
2420 * might be able to be faulted in. This routine brackets the viable
2421 * pages for the pages to be paged in.
2424 * m, rbehind, rahead
2427 * marray (array of vm_page_t), reqpage (index of requested page)
2430 * number of pages in marray
2433 vm_fault_additional_pages(vm_page_t m
, int rbehind
, int rahead
,
2434 vm_page_t
*marray
, int *reqpage
)
2438 vm_pindex_t pindex
, startpindex
, endpindex
, tpindex
;
2440 int cbehind
, cahead
;
2446 * we don't fault-ahead for device pager
2448 if ((object
->type
== OBJT_DEVICE
) ||
2449 (object
->type
== OBJT_MGTDEVICE
)) {
2456 * if the requested page is not available, then give up now
2458 if (!vm_pager_has_page(object
, pindex
, &cbehind
, &cahead
)) {
2459 *reqpage
= 0; /* not used by caller, fix compiler warn */
2463 if ((cbehind
== 0) && (cahead
== 0)) {
2469 if (rahead
> cahead
) {
2473 if (rbehind
> cbehind
) {
2478 * Do not do any readahead if we have insufficient free memory.
2480 * XXX code was broken disabled before and has instability
2481 * with this conditonal fixed, so shortcut for now.
2483 if (burst_fault
== 0 || vm_page_count_severe()) {
2490 * scan backward for the read behind pages -- in memory
2492 * Assume that if the page is not found an interrupt will not
2493 * create it. Theoretically interrupts can only remove (busy)
2494 * pages, not create new associations.
2497 if (rbehind
> pindex
) {
2501 startpindex
= pindex
- rbehind
;
2504 vm_object_hold(object
);
2505 for (tpindex
= pindex
; tpindex
> startpindex
; --tpindex
) {
2506 if (vm_page_lookup(object
, tpindex
- 1))
2511 while (tpindex
< pindex
) {
2512 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2515 for (j
= 0; j
< i
; j
++) {
2516 vm_page_free(marray
[j
]);
2518 vm_object_drop(object
);
2527 vm_object_drop(object
);
2533 * Assign requested page
2540 * Scan forwards for read-ahead pages
2542 tpindex
= pindex
+ 1;
2543 endpindex
= tpindex
+ rahead
;
2544 if (endpindex
> object
->size
)
2545 endpindex
= object
->size
;
2547 vm_object_hold(object
);
2548 while (tpindex
< endpindex
) {
2549 if (vm_page_lookup(object
, tpindex
))
2551 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2559 vm_object_drop(object
);
2567 * vm_prefault() provides a quick way of clustering pagefaults into a
2568 * processes address space. It is a "cousin" of pmap_object_init_pt,
2569 * except it runs at page fault time instead of mmap time.
2571 * vm.fast_fault Enables pre-faulting zero-fill pages
2573 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2574 * prefault. Scan stops in either direction when
2575 * a page is found to already exist.
2577 * This code used to be per-platform pmap_prefault(). It is now
2578 * machine-independent and enhanced to also pre-fault zero-fill pages
2579 * (see vm.fast_fault) as well as make them writable, which greatly
2580 * reduces the number of page faults programs incur.
2582 * Application performance when pre-faulting zero-fill pages is heavily
2583 * dependent on the application. Very tiny applications like /bin/echo
2584 * lose a little performance while applications of any appreciable size
2585 * gain performance. Prefaulting multiple pages also reduces SMP
2586 * congestion and can improve SMP performance significantly.
2588 * NOTE! prot may allow writing but this only applies to the top level
2589 * object. If we wind up mapping a page extracted from a backing
2590 * object we have to make sure it is read-only.
2592 * NOTE! The caller has already handled any COW operations on the
2593 * vm_map_entry via the normal fault code. Do NOT call this
2594 * shortcut unless the normal fault code has run on this entry.
2596 * The related map must be locked.
2597 * No other requirements.
2599 static int vm_prefault_pages
= 8;
2600 SYSCTL_INT(_vm
, OID_AUTO
, prefault_pages
, CTLFLAG_RW
, &vm_prefault_pages
, 0,
2601 "Maximum number of pages to pre-fault");
2602 static int vm_fast_fault
= 1;
2603 SYSCTL_INT(_vm
, OID_AUTO
, fast_fault
, CTLFLAG_RW
, &vm_fast_fault
, 0,
2604 "Burst fault zero-fill regions");
2607 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2608 * is not already dirty by other means. This will prevent passive
2609 * filesystem syncing as well as 'sync' from writing out the page.
2612 vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
)
2614 if (entry
->eflags
& MAP_ENTRY_NOSYNC
) {
2616 vm_page_flag_set(m
, PG_NOSYNC
);
2618 vm_page_flag_clear(m
, PG_NOSYNC
);
2623 vm_prefault(pmap_t pmap
, vm_offset_t addra
, vm_map_entry_t entry
, int prot
,
2639 * Get stable max count value, disabled if set to 0
2641 maxpages
= vm_prefault_pages
;
2647 * We do not currently prefault mappings that use virtual page
2648 * tables. We do not prefault foreign pmaps.
2650 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2652 lp
= curthread
->td_lwp
;
2653 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2657 * Limit pre-fault count to 1024 pages.
2659 if (maxpages
> 1024)
2662 object
= entry
->object
.vm_object
;
2663 KKASSERT(object
!= NULL
);
2664 KKASSERT(object
== entry
->object
.vm_object
);
2667 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2668 * now (or do something more complex XXX).
2670 vm_object_hold(object
);
2671 vm_object_chain_acquire(object
, 0);
2675 for (i
= 0; i
< maxpages
; ++i
) {
2676 vm_object_t lobject
;
2677 vm_object_t nobject
;
2682 * This can eat a lot of time on a heavily contended
2683 * machine so yield on the tick if needed.
2689 * Calculate the page to pre-fault, stopping the scan in
2690 * each direction separately if the limit is reached.
2695 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2699 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2701 if (addr
< entry
->start
) {
2707 if (addr
>= entry
->end
) {
2715 * Skip pages already mapped, and stop scanning in that
2716 * direction. When the scan terminates in both directions
2719 if (pmap_prefault_ok(pmap
, addr
) == 0) {
2730 * Follow the VM object chain to obtain the page to be mapped
2733 * If we reach the terminal object without finding a page
2734 * and we determine it would be advantageous, then allocate
2735 * a zero-fill page for the base object. The base object
2736 * is guaranteed to be OBJT_DEFAULT for this case.
2738 * In order to not have to check the pager via *haspage*()
2739 * we stop if any non-default object is encountered. e.g.
2740 * a vnode or swap object would stop the loop.
2742 index
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
2747 KKASSERT(lobject
== entry
->object
.vm_object
);
2748 /*vm_object_hold(lobject); implied */
2750 while ((m
= vm_page_lookup_busy_try(lobject
, pindex
,
2751 TRUE
, &error
)) == NULL
) {
2752 if (lobject
->type
!= OBJT_DEFAULT
)
2754 if (lobject
->backing_object
== NULL
) {
2755 if (vm_fast_fault
== 0)
2757 if ((prot
& VM_PROT_WRITE
) == 0 ||
2758 vm_page_count_min(0)) {
2763 * NOTE: Allocated from base object
2765 m
= vm_page_alloc(object
, index
,
2774 /* lobject = object .. not needed */
2777 if (lobject
->backing_object_offset
& PAGE_MASK
)
2779 nobject
= lobject
->backing_object
;
2780 vm_object_hold(nobject
);
2781 KKASSERT(nobject
== lobject
->backing_object
);
2782 pindex
+= lobject
->backing_object_offset
>> PAGE_SHIFT
;
2783 if (lobject
!= object
) {
2784 vm_object_lock_swap();
2785 vm_object_drop(lobject
);
2788 pprot
&= ~VM_PROT_WRITE
;
2789 vm_object_chain_acquire(lobject
, 0);
2793 * NOTE: A non-NULL (m) will be associated with lobject if
2794 * it was found there, otherwise it is probably a
2795 * zero-fill page associated with the base object.
2797 * Give-up if no page is available.
2800 if (lobject
!= object
) {
2802 if (object
->backing_object
!= lobject
)
2803 vm_object_hold(object
->backing_object
);
2805 vm_object_chain_release_all(
2806 object
->backing_object
, lobject
);
2808 if (object
->backing_object
!= lobject
)
2809 vm_object_drop(object
->backing_object
);
2811 vm_object_drop(lobject
);
2817 * The object must be marked dirty if we are mapping a
2818 * writable page. m->object is either lobject or object,
2819 * both of which are still held. Do this before we
2820 * potentially drop the object.
2822 if (pprot
& VM_PROT_WRITE
)
2823 vm_object_set_writeable_dirty(m
->object
);
2826 * Do not conditionalize on PG_RAM. If pages are present in
2827 * the VM system we assume optimal caching. If caching is
2828 * not optimal the I/O gravy train will be restarted when we
2829 * hit an unavailable page. We do not want to try to restart
2830 * the gravy train now because we really don't know how much
2831 * of the object has been cached. The cost for restarting
2832 * the gravy train should be low (since accesses will likely
2833 * be I/O bound anyway).
2835 if (lobject
!= object
) {
2837 if (object
->backing_object
!= lobject
)
2838 vm_object_hold(object
->backing_object
);
2840 vm_object_chain_release_all(object
->backing_object
,
2843 if (object
->backing_object
!= lobject
)
2844 vm_object_drop(object
->backing_object
);
2846 vm_object_drop(lobject
);
2850 * Enter the page into the pmap if appropriate. If we had
2851 * allocated the page we have to place it on a queue. If not
2852 * we just have to make sure it isn't on the cache queue
2853 * (pages on the cache queue are not allowed to be mapped).
2857 * Page must be zerod.
2859 vm_page_zero_fill(m
);
2860 mycpu
->gd_cnt
.v_zfod
++;
2861 m
->valid
= VM_PAGE_BITS_ALL
;
2864 * Handle dirty page case
2866 if (pprot
& VM_PROT_WRITE
)
2867 vm_set_nosync(m
, entry
);
2868 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2869 mycpu
->gd_cnt
.v_vm_faults
++;
2870 if (curthread
->td_lwp
)
2871 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2872 vm_page_deactivate(m
);
2873 if (pprot
& VM_PROT_WRITE
) {
2874 /*vm_object_set_writeable_dirty(m->object);*/
2875 vm_set_nosync(m
, entry
);
2876 if (fault_flags
& VM_FAULT_DIRTY
) {
2879 swap_pager_unswapped(m
);
2884 /* couldn't busy page, no wakeup */
2886 ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
2887 (m
->flags
& PG_FICTITIOUS
) == 0) {
2889 * A fully valid page not undergoing soft I/O can
2890 * be immediately entered into the pmap.
2892 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
2893 vm_page_deactivate(m
);
2894 if (pprot
& VM_PROT_WRITE
) {
2895 /*vm_object_set_writeable_dirty(m->object);*/
2896 vm_set_nosync(m
, entry
);
2897 if (fault_flags
& VM_FAULT_DIRTY
) {
2900 swap_pager_unswapped(m
);
2903 if (pprot
& VM_PROT_WRITE
)
2904 vm_set_nosync(m
, entry
);
2905 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2906 mycpu
->gd_cnt
.v_vm_faults
++;
2907 if (curthread
->td_lwp
)
2908 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2914 vm_object_chain_release(object
);
2915 vm_object_drop(object
);
2919 * Object can be held shared
2922 vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
2923 vm_map_entry_t entry
, int prot
, int fault_flags
)
2936 * Get stable max count value, disabled if set to 0
2938 maxpages
= vm_prefault_pages
;
2944 * We do not currently prefault mappings that use virtual page
2945 * tables. We do not prefault foreign pmaps.
2947 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2949 lp
= curthread
->td_lwp
;
2950 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2952 object
= entry
->object
.vm_object
;
2953 if (object
->backing_object
!= NULL
)
2955 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
2958 * Limit pre-fault count to 1024 pages.
2960 if (maxpages
> 1024)
2965 for (i
= 0; i
< maxpages
; ++i
) {
2969 * Calculate the page to pre-fault, stopping the scan in
2970 * each direction separately if the limit is reached.
2975 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2979 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2981 if (addr
< entry
->start
) {
2987 if (addr
>= entry
->end
) {
2995 * Follow the VM object chain to obtain the page to be mapped
2996 * into the pmap. This version of the prefault code only
2997 * works with terminal objects.
2999 * The page must already exist. If we encounter a problem
3002 * WARNING! We cannot call swap_pager_unswapped() or insert
3003 * a new vm_page with a shared token.
3005 pindex
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
3007 m
= vm_page_lookup_busy_try(object
, pindex
, TRUE
, &error
);
3008 if (m
== NULL
|| error
)
3012 * Skip pages already mapped, and stop scanning in that
3013 * direction. When the scan terminates in both directions
3016 if (pmap_prefault_ok(pmap
, addr
) == 0) {
3028 * Stop if the page cannot be trivially entered into the
3031 if (((m
->valid
& VM_PAGE_BITS_ALL
) != VM_PAGE_BITS_ALL
) ||
3032 (m
->flags
& PG_FICTITIOUS
) ||
3033 ((m
->flags
& PG_SWAPPED
) &&
3034 (prot
& VM_PROT_WRITE
) &&
3035 (fault_flags
& VM_FAULT_DIRTY
))) {
3041 * Enter the page into the pmap. The object might be held
3042 * shared so we can't do any (serious) modifying operation
3045 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
3046 vm_page_deactivate(m
);
3047 if (prot
& VM_PROT_WRITE
) {
3048 vm_object_set_writeable_dirty(m
->object
);
3049 vm_set_nosync(m
, entry
);
3050 if (fault_flags
& VM_FAULT_DIRTY
) {
3052 /* can't happeen due to conditional above */
3053 /* swap_pager_unswapped(m); */
3056 pmap_enter(pmap
, addr
, m
, prot
, 0, entry
);
3057 mycpu
->gd_cnt
.v_vm_faults
++;
3058 if (curthread
->td_lwp
)
3059 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;