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
;
320 lwkt_gettoken(&map
->token
);
324 * Find the vm_map_entry representing the backing store and resolve
325 * the top level object and page index. This may have the side
326 * effect of executing a copy-on-write on the map entry and/or
327 * creating a shadow object, but will not COW any actual VM pages.
329 * On success fs.map is left read-locked and various other fields
330 * are initialized but not otherwise referenced or locked.
332 * NOTE! vm_map_lookup will try to upgrade the fault_type to
333 * VM_FAULT_WRITE if the map entry is a virtual page table
334 * and also writable, so we can set the 'A'accessed bit in
335 * the virtual page table entry.
338 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
339 &fs
.entry
, &fs
.first_object
,
340 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
343 * If the lookup failed or the map protections are incompatible,
344 * the fault generally fails.
346 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
347 * tried to do a COW fault.
349 * If the caller is trying to do a user wiring we have more work
352 if (result
!= KERN_SUCCESS
) {
353 if (result
== KERN_FAILURE_NOFAULT
) {
354 result
= KERN_FAILURE
;
357 if (result
!= KERN_PROTECTION_FAILURE
||
358 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
360 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
361 map
!= &kernel_map
&& curproc
!= NULL
) {
362 result
= vm_map_growstack(map
, vaddr
);
363 if (result
== KERN_SUCCESS
) {
368 result
= KERN_FAILURE
;
374 * If we are user-wiring a r/w segment, and it is COW, then
375 * we need to do the COW operation. Note that we don't
376 * currently COW RO sections now, because it is NOT desirable
377 * to COW .text. We simply keep .text from ever being COW'ed
378 * and take the heat that one cannot debug wired .text sections.
380 result
= vm_map_lookup(&fs
.map
, vaddr
,
381 VM_PROT_READ
|VM_PROT_WRITE
|
382 VM_PROT_OVERRIDE_WRITE
,
383 &fs
.entry
, &fs
.first_object
,
384 &first_pindex
, &fs
.first_prot
,
386 if (result
!= KERN_SUCCESS
) {
387 /* could also be KERN_FAILURE_NOFAULT */
388 result
= KERN_FAILURE
;
393 * If we don't COW now, on a user wire, the user will never
394 * be able to write to the mapping. If we don't make this
395 * restriction, the bookkeeping would be nearly impossible.
397 * XXX We have a shared lock, this will have a MP race but
398 * I don't see how it can hurt anything.
400 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
401 atomic_clear_char(&fs
.entry
->max_protection
,
407 * fs.map is read-locked
409 * Misc checks. Save the map generation number to detect races.
411 fs
.map_generation
= fs
.map
->timestamp
;
412 fs
.lookup_still_valid
= TRUE
;
414 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
415 fs
.prot
= fs
.first_prot
; /* default (used by uksmap) */
417 if (fs
.entry
->eflags
& (MAP_ENTRY_NOFAULT
| MAP_ENTRY_KSTACK
)) {
418 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
419 panic("vm_fault: fault on nofault entry, addr: %p",
422 if ((fs
.entry
->eflags
& MAP_ENTRY_KSTACK
) &&
423 vaddr
>= fs
.entry
->start
&&
424 vaddr
< fs
.entry
->start
+ PAGE_SIZE
) {
425 panic("vm_fault: fault on stack guard, addr: %p",
431 * A user-kernel shared map has no VM object and bypasses
432 * everything. We execute the uksmap function with a temporary
433 * fictitious vm_page. The address is directly mapped with no
436 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
437 struct vm_page fakem
;
439 bzero(&fakem
, sizeof(fakem
));
440 fakem
.pindex
= first_pindex
;
441 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
442 fakem
.valid
= VM_PAGE_BITS_ALL
;
443 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
444 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
445 result
= KERN_FAILURE
;
449 pmap_enter(fs
.map
->pmap
, vaddr
, &fakem
, fs
.prot
| inherit_prot
,
455 * A system map entry may return a NULL object. No object means
456 * no pager means an unrecoverable kernel fault.
458 if (fs
.first_object
== NULL
) {
459 panic("vm_fault: unrecoverable fault at %p in entry %p",
460 (void *)vaddr
, fs
.entry
);
464 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
467 * Unfortunately a deadlock can occur if we are forced to page-in
468 * from swap, but diving all the way into the vm_pager_get_page()
469 * function to find out is too much. Just check the object type.
471 * The deadlock is a CAM deadlock on a busy VM page when trying
472 * to finish an I/O if another process gets stuck in
473 * vop_helper_read_shortcut() due to a swap fault.
475 if ((td
->td_flags
& TDF_NOFAULT
) &&
477 fs
.first_object
->type
== OBJT_VNODE
||
478 fs
.first_object
->type
== OBJT_SWAP
||
479 fs
.first_object
->backing_object
)) {
480 result
= KERN_FAILURE
;
486 * If the entry is wired we cannot change the page protection.
489 fault_type
= fs
.first_prot
;
492 * We generally want to avoid unnecessary exclusive modes on backing
493 * and terminal objects because this can seriously interfere with
494 * heavily fork()'d processes (particularly /bin/sh scripts).
496 * However, we also want to avoid unnecessary retries due to needed
497 * shared->exclusive promotion for common faults. Exclusive mode is
498 * always needed if any page insertion, rename, or free occurs in an
499 * object (and also indirectly if any I/O is done).
501 * The main issue here is going to be fs.first_shared. If the
502 * first_object has a backing object which isn't shadowed and the
503 * process is single-threaded we might as well use an exclusive
504 * lock/chain right off the bat.
506 if (fs
.first_shared
&& fs
.first_object
->backing_object
&&
507 LIST_EMPTY(&fs
.first_object
->shadow_head
) &&
508 td
->td_proc
&& td
->td_proc
->p_nthreads
== 1) {
513 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
514 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
515 * we can try shared first.
517 if (fault_flags
& VM_FAULT_UNSWAP
) {
522 * Obtain a top-level object lock, shared or exclusive depending
523 * on fs.first_shared. If a shared lock winds up being insufficient
524 * we will retry with an exclusive lock.
526 * The vnode pager lock is always shared.
529 vm_object_hold_shared(fs
.first_object
);
531 vm_object_hold(fs
.first_object
);
533 fs
.vp
= vnode_pager_lock(fs
.first_object
);
536 * The page we want is at (first_object, first_pindex), but if the
537 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
538 * page table to figure out the actual pindex.
540 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
544 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
545 vm_map_interlock(fs
.map
, &ilock
, vaddr
, vaddr
+ PAGE_SIZE
);
547 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
548 fs
.entry
->aux
.master_pde
,
550 if (result
== KERN_TRY_AGAIN
) {
551 vm_map_deinterlock(fs
.map
, &ilock
);
552 vm_object_drop(fs
.first_object
);
556 if (result
!= KERN_SUCCESS
) {
557 vm_map_deinterlock(fs
.map
, &ilock
);
563 * Now we have the actual (object, pindex), fault in the page. If
564 * vm_fault_object() fails it will unlock and deallocate the FS
565 * data. If it succeeds everything remains locked and fs->object
566 * will have an additional PIP count if it is not equal to
569 * vm_fault_object will set fs->prot for the pmap operation. It is
570 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
571 * page can be safely written. However, it will force a read-only
572 * mapping for a read fault if the memory is managed by a virtual
575 * If the fault code uses the shared object lock shortcut
576 * we must not try to burst (we can't allocate VM pages).
578 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
580 if (debug_fault
> 0) {
582 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
583 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
584 result
, (intmax_t)vaddr
, fault_type
, fault_flags
,
585 fs
.m
, fs
.prot
, fs
.wired
, fs
.entry
);
588 if (result
== KERN_TRY_AGAIN
) {
590 vm_map_deinterlock(fs
.map
, &ilock
);
591 vm_object_drop(fs
.first_object
);
595 if (result
!= KERN_SUCCESS
) {
597 vm_map_deinterlock(fs
.map
, &ilock
);
602 * On success vm_fault_object() does not unlock or deallocate, and fs.m
603 * will contain a busied page.
605 * Enter the page into the pmap and do pmap-related adjustments.
607 KKASSERT(fs
.lookup_still_valid
== TRUE
);
608 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
609 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
| inherit_prot
,
613 vm_map_deinterlock(fs
.map
, &ilock
);
615 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
616 KKASSERT(fs
.m
->flags
& PG_BUSY
);
619 * If the page is not wired down, then put it where the pageout daemon
622 if (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) {
626 vm_page_unwire(fs
.m
, 1);
628 vm_page_activate(fs
.m
);
630 vm_page_wakeup(fs
.m
);
633 * Burst in a few more pages if possible. The fs.map should still
634 * be locked. To avoid interlocking against a vnode->getblk
635 * operation we had to be sure to unbusy our primary vm_page above
638 * A normal burst can continue down backing store, only execute
639 * if we are holding an exclusive lock, otherwise the exclusive
640 * locks the burst code gets might cause excessive SMP collisions.
642 * A quick burst can be utilized when there is no backing object
643 * (i.e. a shared file mmap).
645 if ((fault_flags
& VM_FAULT_BURST
) &&
646 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) == 0 &&
648 if (fs
.first_shared
== 0 && fs
.shared
== 0) {
649 vm_prefault(fs
.map
->pmap
, vaddr
,
650 fs
.entry
, fs
.prot
, fault_flags
);
652 vm_prefault_quick(fs
.map
->pmap
, vaddr
,
653 fs
.entry
, fs
.prot
, fault_flags
);
658 mycpu
->gd_cnt
.v_vm_faults
++;
660 ++td
->td_lwp
->lwp_ru
.ru_minflt
;
663 * Unlock everything, and return
669 td
->td_lwp
->lwp_ru
.ru_majflt
++;
671 td
->td_lwp
->lwp_ru
.ru_minflt
++;
675 /*vm_object_deallocate(fs.first_object);*/
677 /*fs.first_object = NULL; must still drop later */
679 result
= KERN_SUCCESS
;
682 vm_object_drop(fs
.first_object
);
684 lwkt_reltoken(&map
->token
);
686 lp
->lwp_flags
&= ~LWP_PAGING
;
688 #if !defined(NO_SWAPPING)
690 * Check the process RSS limit and force deactivation and
691 * (asynchronous) paging if necessary. This is a complex operation,
692 * only do it for direct user-mode faults, for now.
694 * To reduce overhead implement approximately a ~16MB hysteresis.
697 if ((fault_flags
& VM_FAULT_USERMODE
) && lp
&&
698 p
->p_limit
&& map
->pmap
&& vm_pageout_memuse_mode
>= 1 &&
699 map
!= &kernel_map
) {
703 limit
= OFF_TO_IDX(qmin(p
->p_rlimit
[RLIMIT_RSS
].rlim_cur
,
704 p
->p_rlimit
[RLIMIT_RSS
].rlim_max
));
705 size
= pmap_resident_tlnw_count(map
->pmap
);
706 if (limit
>= 0 && size
> 4096 && size
- 4096 >= limit
) {
707 vm_pageout_map_deactivate_pages(map
, limit
);
716 * Fault in the specified virtual address in the current process map,
717 * returning a held VM page or NULL. See vm_fault_page() for more
723 vm_fault_page_quick(vm_offset_t va
, vm_prot_t fault_type
,
724 int *errorp
, int *busyp
)
726 struct lwp
*lp
= curthread
->td_lwp
;
729 m
= vm_fault_page(&lp
->lwp_vmspace
->vm_map
, va
,
730 fault_type
, VM_FAULT_NORMAL
,
736 * Fault in the specified virtual address in the specified map, doing all
737 * necessary manipulation of the object store and all necessary I/O. Return
738 * a held VM page or NULL, and set *errorp. The related pmap is not
741 * If busyp is not NULL then *busyp will be set to TRUE if this routine
742 * decides to return a busied page (aka VM_PROT_WRITE), or FALSE if it
743 * does not (VM_PROT_WRITE not specified or busyp is NULL). If busyp is
744 * NULL the returned page is only held.
746 * If the caller has no intention of writing to the page's contents, busyp
747 * can be passed as NULL along with VM_PROT_WRITE to force a COW operation
748 * without busying the page.
750 * The returned page will also be marked PG_REFERENCED.
752 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
753 * error will be returned.
758 vm_fault_page(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
,
759 int fault_flags
, int *errorp
, int *busyp
)
761 vm_pindex_t first_pindex
;
762 struct faultstate fs
;
766 vm_prot_t orig_fault_type
= fault_type
;
770 fs
.fault_flags
= fault_flags
;
771 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
774 * Dive the pmap (concurrency possible). If we find the
775 * appropriate page we can terminate early and quickly.
777 * This works great for normal programs but will always return
778 * NULL for host lookups of vkernel maps in VMM mode.
780 fs
.m
= pmap_fault_page_quick(map
->pmap
, vaddr
, fault_type
, busyp
);
782 if (fault_type
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
))
789 * Otherwise take a concurrency hit and do a formal page
793 fs
.shared
= vm_shared_fault
;
794 fs
.first_shared
= vm_shared_fault
;
796 lwkt_gettoken(&map
->token
);
799 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
800 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
801 * we can try shared first.
803 if (fault_flags
& VM_FAULT_UNSWAP
) {
809 * Find the vm_map_entry representing the backing store and resolve
810 * the top level object and page index. This may have the side
811 * effect of executing a copy-on-write on the map entry and/or
812 * creating a shadow object, but will not COW any actual VM pages.
814 * On success fs.map is left read-locked and various other fields
815 * are initialized but not otherwise referenced or locked.
817 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
818 * if the map entry is a virtual page table and also writable,
819 * so we can set the 'A'accessed bit in the virtual page table
823 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
824 &fs
.entry
, &fs
.first_object
,
825 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
827 if (result
!= KERN_SUCCESS
) {
828 if (result
== KERN_FAILURE_NOFAULT
) {
829 *errorp
= KERN_FAILURE
;
833 if (result
!= KERN_PROTECTION_FAILURE
||
834 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
836 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
837 map
!= &kernel_map
&& curproc
!= NULL
) {
838 result
= vm_map_growstack(map
, vaddr
);
839 if (result
== KERN_SUCCESS
) {
844 result
= KERN_FAILURE
;
852 * If we are user-wiring a r/w segment, and it is COW, then
853 * we need to do the COW operation. Note that we don't
854 * currently COW RO sections now, because it is NOT desirable
855 * to COW .text. We simply keep .text from ever being COW'ed
856 * and take the heat that one cannot debug wired .text sections.
858 result
= vm_map_lookup(&fs
.map
, vaddr
,
859 VM_PROT_READ
|VM_PROT_WRITE
|
860 VM_PROT_OVERRIDE_WRITE
,
861 &fs
.entry
, &fs
.first_object
,
862 &first_pindex
, &fs
.first_prot
,
864 if (result
!= KERN_SUCCESS
) {
865 /* could also be KERN_FAILURE_NOFAULT */
866 *errorp
= KERN_FAILURE
;
872 * If we don't COW now, on a user wire, the user will never
873 * be able to write to the mapping. If we don't make this
874 * restriction, the bookkeeping would be nearly impossible.
876 * XXX We have a shared lock, this will have a MP race but
877 * I don't see how it can hurt anything.
879 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0) {
880 atomic_clear_char(&fs
.entry
->max_protection
,
886 * fs.map is read-locked
888 * Misc checks. Save the map generation number to detect races.
890 fs
.map_generation
= fs
.map
->timestamp
;
891 fs
.lookup_still_valid
= TRUE
;
893 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
895 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
896 panic("vm_fault: fault on nofault entry, addr: %lx",
901 * A user-kernel shared map has no VM object and bypasses
902 * everything. We execute the uksmap function with a temporary
903 * fictitious vm_page. The address is directly mapped with no
906 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
907 struct vm_page fakem
;
909 bzero(&fakem
, sizeof(fakem
));
910 fakem
.pindex
= first_pindex
;
911 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
912 fakem
.valid
= VM_PAGE_BITS_ALL
;
913 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
914 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
915 *errorp
= KERN_FAILURE
;
920 fs
.m
= PHYS_TO_VM_PAGE(fakem
.phys_addr
);
923 *busyp
= 0; /* don't need to busy R or W */
931 * A system map entry may return a NULL object. No object means
932 * no pager means an unrecoverable kernel fault.
934 if (fs
.first_object
== NULL
) {
935 panic("vm_fault: unrecoverable fault at %p in entry %p",
936 (void *)vaddr
, fs
.entry
);
940 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
943 * Unfortunately a deadlock can occur if we are forced to page-in
944 * from swap, but diving all the way into the vm_pager_get_page()
945 * function to find out is too much. Just check the object type.
947 if ((curthread
->td_flags
& TDF_NOFAULT
) &&
949 fs
.first_object
->type
== OBJT_VNODE
||
950 fs
.first_object
->type
== OBJT_SWAP
||
951 fs
.first_object
->backing_object
)) {
952 *errorp
= KERN_FAILURE
;
959 * If the entry is wired we cannot change the page protection.
962 fault_type
= fs
.first_prot
;
965 * Make a reference to this object to prevent its disposal while we
966 * are messing with it. Once we have the reference, the map is free
967 * to be diddled. Since objects reference their shadows (and copies),
968 * they will stay around as well.
970 * The reference should also prevent an unexpected collapse of the
971 * parent that might move pages from the current object into the
972 * parent unexpectedly, resulting in corruption.
974 * Bump the paging-in-progress count to prevent size changes (e.g.
975 * truncation operations) during I/O. This must be done after
976 * obtaining the vnode lock in order to avoid possible deadlocks.
979 vm_object_hold_shared(fs
.first_object
);
981 vm_object_hold(fs
.first_object
);
983 fs
.vp
= vnode_pager_lock(fs
.first_object
); /* shared */
986 * The page we want is at (first_object, first_pindex), but if the
987 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
988 * page table to figure out the actual pindex.
990 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
993 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
994 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
995 fs
.entry
->aux
.master_pde
,
997 if (result
== KERN_TRY_AGAIN
) {
998 vm_object_drop(fs
.first_object
);
1002 if (result
!= KERN_SUCCESS
) {
1010 * Now we have the actual (object, pindex), fault in the page. If
1011 * vm_fault_object() fails it will unlock and deallocate the FS
1012 * data. If it succeeds everything remains locked and fs->object
1013 * will have an additinal PIP count if it is not equal to
1017 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
1019 if (result
== KERN_TRY_AGAIN
) {
1020 vm_object_drop(fs
.first_object
);
1024 if (result
!= KERN_SUCCESS
) {
1030 if ((orig_fault_type
& VM_PROT_WRITE
) &&
1031 (fs
.prot
& VM_PROT_WRITE
) == 0) {
1032 *errorp
= KERN_PROTECTION_FAILURE
;
1033 unlock_and_deallocate(&fs
);
1039 * DO NOT UPDATE THE PMAP!!! This function may be called for
1040 * a pmap unrelated to the current process pmap, in which case
1041 * the current cpu core will not be listed in the pmap's pm_active
1042 * mask. Thus invalidation interlocks will fail to work properly.
1044 * (for example, 'ps' uses procfs to read program arguments from
1045 * each process's stack).
1047 * In addition to the above this function will be called to acquire
1048 * a page that might already be faulted in, re-faulting it
1049 * continuously is a waste of time.
1051 * XXX could this have been the cause of our random seg-fault
1052 * issues? procfs accesses user stacks.
1054 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1056 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
, fs
.wired
, NULL
);
1057 mycpu
->gd_cnt
.v_vm_faults
++;
1058 if (curthread
->td_lwp
)
1059 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
1063 * On success vm_fault_object() does not unlock or deallocate, and fs.m
1064 * will contain a busied page. So we must unlock here after having
1065 * messed with the pmap.
1070 * Return a held page. We are not doing any pmap manipulation so do
1071 * not set PG_MAPPED. However, adjust the page flags according to
1072 * the fault type because the caller may not use a managed pmapping
1073 * (so we don't want to lose the fact that the page will be dirtied
1074 * if a write fault was specified).
1076 if (fault_type
& VM_PROT_WRITE
)
1077 vm_page_dirty(fs
.m
);
1078 vm_page_activate(fs
.m
);
1080 if (curthread
->td_lwp
) {
1082 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1084 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1089 * Unlock everything, and return the held or busied page.
1092 if (fault_type
& (VM_PROT_WRITE
|VM_PROT_OVERRIDE_WRITE
)) {
1093 vm_page_dirty(fs
.m
);
1098 vm_page_wakeup(fs
.m
);
1102 vm_page_wakeup(fs
.m
);
1104 /*vm_object_deallocate(fs.first_object);*/
1105 /*fs.first_object = NULL; */
1109 if (fs
.first_object
)
1110 vm_object_drop(fs
.first_object
);
1112 lwkt_reltoken(&map
->token
);
1117 * Fault in the specified (object,offset), dirty the returned page as
1118 * needed. If the requested fault_type cannot be done NULL and an
1119 * error is returned.
1121 * A held (but not busied) page is returned.
1123 * The passed in object must be held as specified by the shared
1127 vm_fault_object_page(vm_object_t object
, vm_ooffset_t offset
,
1128 vm_prot_t fault_type
, int fault_flags
,
1129 int *sharedp
, int *errorp
)
1132 vm_pindex_t first_pindex
;
1133 struct faultstate fs
;
1134 struct vm_map_entry entry
;
1136 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
1137 bzero(&entry
, sizeof(entry
));
1138 entry
.object
.vm_object
= object
;
1139 entry
.maptype
= VM_MAPTYPE_NORMAL
;
1140 entry
.protection
= entry
.max_protection
= fault_type
;
1143 fs
.fault_flags
= fault_flags
;
1145 fs
.shared
= vm_shared_fault
;
1146 fs
.first_shared
= *sharedp
;
1148 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
1151 * VM_FAULT_UNSWAP - swap_pager_unswapped() needs an exclusive object
1152 * VM_FAULT_DIRTY - may require swap_pager_unswapped() later, but
1153 * we can try shared first.
1155 if (fs
.first_shared
&& (fault_flags
& VM_FAULT_UNSWAP
)) {
1156 fs
.first_shared
= 0;
1157 vm_object_upgrade(object
);
1161 * Retry loop as needed (typically for shared->exclusive transitions)
1164 *sharedp
= fs
.first_shared
;
1165 first_pindex
= OFF_TO_IDX(offset
);
1166 fs
.first_object
= object
;
1168 fs
.first_prot
= fault_type
;
1170 /*fs.map_generation = 0; unused */
1173 * Make a reference to this object to prevent its disposal while we
1174 * are messing with it. Once we have the reference, the map is free
1175 * to be diddled. Since objects reference their shadows (and copies),
1176 * they will stay around as well.
1178 * The reference should also prevent an unexpected collapse of the
1179 * parent that might move pages from the current object into the
1180 * parent unexpectedly, resulting in corruption.
1182 * Bump the paging-in-progress count to prevent size changes (e.g.
1183 * truncation operations) during I/O. This must be done after
1184 * obtaining the vnode lock in order to avoid possible deadlocks.
1187 fs
.vp
= vnode_pager_lock(fs
.first_object
);
1189 fs
.lookup_still_valid
= TRUE
;
1191 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
1194 /* XXX future - ability to operate on VM object using vpagetable */
1195 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1196 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
1197 fs
.entry
->aux
.master_pde
,
1199 if (result
== KERN_TRY_AGAIN
) {
1200 if (fs
.first_shared
== 0 && *sharedp
)
1201 vm_object_upgrade(object
);
1204 if (result
!= KERN_SUCCESS
) {
1212 * Now we have the actual (object, pindex), fault in the page. If
1213 * vm_fault_object() fails it will unlock and deallocate the FS
1214 * data. If it succeeds everything remains locked and fs->object
1215 * will have an additinal PIP count if it is not equal to
1218 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1219 * We may have to upgrade its lock to handle the requested fault.
1221 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 0);
1223 if (result
== KERN_TRY_AGAIN
) {
1224 if (fs
.first_shared
== 0 && *sharedp
)
1225 vm_object_upgrade(object
);
1228 if (result
!= KERN_SUCCESS
) {
1233 if ((fault_type
& VM_PROT_WRITE
) && (fs
.prot
& VM_PROT_WRITE
) == 0) {
1234 *errorp
= KERN_PROTECTION_FAILURE
;
1235 unlock_and_deallocate(&fs
);
1240 * On success vm_fault_object() does not unlock or deallocate, so we
1241 * do it here. Note that the returned fs.m will be busied.
1246 * Return a held page. We are not doing any pmap manipulation so do
1247 * not set PG_MAPPED. However, adjust the page flags according to
1248 * the fault type because the caller may not use a managed pmapping
1249 * (so we don't want to lose the fact that the page will be dirtied
1250 * if a write fault was specified).
1253 vm_page_activate(fs
.m
);
1254 if ((fault_type
& VM_PROT_WRITE
) || (fault_flags
& VM_FAULT_DIRTY
))
1255 vm_page_dirty(fs
.m
);
1256 if (fault_flags
& VM_FAULT_UNSWAP
)
1257 swap_pager_unswapped(fs
.m
);
1260 * Indicate that the page was accessed.
1262 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1264 if (curthread
->td_lwp
) {
1266 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1268 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1273 * Unlock everything, and return the held page.
1275 vm_page_wakeup(fs
.m
);
1276 /*vm_object_deallocate(fs.first_object);*/
1277 /*fs.first_object = NULL; */
1284 * Translate the virtual page number (first_pindex) that is relative
1285 * to the address space into a logical page number that is relative to the
1286 * backing object. Use the virtual page table pointed to by (vpte).
1288 * Possibly downgrade the protection based on the vpte bits.
1290 * This implements an N-level page table. Any level can terminate the
1291 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1292 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1296 vm_fault_vpagetable(struct faultstate
*fs
, vm_pindex_t
*pindex
,
1297 vpte_t vpte
, int fault_type
, int allow_nofault
)
1300 struct lwbuf lwb_cache
;
1301 int vshift
= VPTE_FRAME_END
- PAGE_SHIFT
; /* index bits remaining */
1305 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1308 * We cannot proceed if the vpte is not valid, not readable
1309 * for a read fault, or not writable for a write fault.
1311 if ((vpte
& VPTE_V
) == 0) {
1312 unlock_and_deallocate(fs
);
1313 return (KERN_FAILURE
);
1315 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
) == 0) {
1316 unlock_and_deallocate(fs
);
1317 return (KERN_FAILURE
);
1319 if ((vpte
& VPTE_PS
) || vshift
== 0)
1323 * Get the page table page. Nominally we only read the page
1324 * table, but since we are actively setting VPTE_M and VPTE_A,
1325 * tell vm_fault_object() that we are writing it.
1327 * There is currently no real need to optimize this.
1329 result
= vm_fault_object(fs
, (vpte
& VPTE_FRAME
) >> PAGE_SHIFT
,
1330 VM_PROT_READ
|VM_PROT_WRITE
,
1332 if (result
!= KERN_SUCCESS
)
1336 * Process the returned fs.m and look up the page table
1337 * entry in the page table page.
1339 vshift
-= VPTE_PAGE_BITS
;
1340 lwb
= lwbuf_alloc(fs
->m
, &lwb_cache
);
1341 ptep
= ((vpte_t
*)lwbuf_kva(lwb
) +
1342 ((*pindex
>> vshift
) & VPTE_PAGE_MASK
));
1343 vm_page_activate(fs
->m
);
1346 * Page table write-back - entire operation including
1347 * validation of the pte must be atomic to avoid races
1348 * against the vkernel changing the pte.
1350 * If the vpte is valid for the* requested operation, do
1351 * a write-back to the page table.
1353 * XXX VPTE_M is not set properly for page directory pages.
1354 * It doesn't get set in the page directory if the page table
1355 * is modified during a read access.
1361 * Reload for the cmpset, but make sure the pte is
1368 if ((vpte
& VPTE_V
) == 0)
1371 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
))
1372 nvpte
|= VPTE_M
| VPTE_A
;
1373 if (fault_type
& VM_PROT_READ
)
1377 if (atomic_cmpset_long(ptep
, vpte
, nvpte
)) {
1378 vm_page_dirty(fs
->m
);
1383 vm_page_flag_set(fs
->m
, PG_REFERENCED
);
1384 vm_page_wakeup(fs
->m
);
1386 cleanup_successful_fault(fs
);
1390 * When the vkernel sets VPTE_RW it expects the real kernel to
1391 * reflect VPTE_M back when the page is modified via the mapping.
1392 * In order to accomplish this the real kernel must map the page
1393 * read-only for read faults and use write faults to reflect VPTE_M
1396 * Once VPTE_M has been set, the real kernel's pte allows writing.
1397 * If the vkernel clears VPTE_M the vkernel must be sure to
1398 * MADV_INVAL the real kernel's mappings to force the real kernel
1399 * to re-fault on the next write so oit can set VPTE_M again.
1401 if ((fault_type
& VM_PROT_WRITE
) == 0 &&
1402 (vpte
& (VPTE_RW
| VPTE_M
)) != (VPTE_RW
| VPTE_M
)) {
1403 fs
->first_prot
&= ~VM_PROT_WRITE
;
1407 * Combine remaining address bits with the vpte.
1409 *pindex
= ((vpte
& VPTE_FRAME
) >> PAGE_SHIFT
) +
1410 (*pindex
& ((1L << vshift
) - 1));
1411 return (KERN_SUCCESS
);
1416 * This is the core of the vm_fault code.
1418 * Do all operations required to fault-in (fs.first_object, pindex). Run
1419 * through the shadow chain as necessary and do required COW or virtual
1420 * copy operations. The caller has already fully resolved the vm_map_entry
1421 * and, if appropriate, has created a copy-on-write layer. All we need to
1422 * do is iterate the object chain.
1424 * On failure (fs) is unlocked and deallocated and the caller may return or
1425 * retry depending on the failure code. On success (fs) is NOT unlocked or
1426 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1427 * will have an additional PIP count if it is not equal to fs.first_object.
1429 * If locks based on fs->first_shared or fs->shared are insufficient,
1430 * clear the appropriate field(s) and return RETRY. COWs require that
1431 * first_shared be 0, while page allocations (or frees) require that
1432 * shared be 0. Renames require that both be 0.
1434 * NOTE! fs->[first_]shared might be set with VM_FAULT_DIRTY also set.
1435 * we will have to retry with it exclusive if the vm_page is
1438 * fs->first_object must be held on call.
1442 vm_fault_object(struct faultstate
*fs
, vm_pindex_t first_pindex
,
1443 vm_prot_t fault_type
, int allow_nofault
)
1445 vm_object_t next_object
;
1449 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1450 fs
->prot
= fs
->first_prot
;
1451 fs
->object
= fs
->first_object
;
1452 pindex
= first_pindex
;
1454 vm_object_chain_acquire(fs
->first_object
, fs
->shared
);
1455 vm_object_pip_add(fs
->first_object
, 1);
1458 * If a read fault occurs we try to upgrade the page protection
1459 * and make it also writable if possible. There are three cases
1460 * where we cannot make the page mapping writable:
1462 * (1) The mapping is read-only or the VM object is read-only,
1463 * fs->prot above will simply not have VM_PROT_WRITE set.
1465 * (2) If the mapping is a virtual page table fs->first_prot will
1466 * have already been properly adjusted by vm_fault_vpagetable().
1467 * to detect writes so we can set VPTE_M in the virtual page
1468 * table. Used by vkernels.
1470 * (3) If the VM page is read-only or copy-on-write, upgrading would
1471 * just result in an unnecessary COW fault.
1473 * (4) If the pmap specifically requests A/M bit emulation, downgrade
1477 /* see vpagetable code */
1478 if (fs
->entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1479 if ((fault_type
& VM_PROT_WRITE
) == 0)
1480 fs
->prot
&= ~VM_PROT_WRITE
;
1484 if (curthread
->td_lwp
&& curthread
->td_lwp
->lwp_vmspace
&&
1485 pmap_emulate_ad_bits(&curthread
->td_lwp
->lwp_vmspace
->vm_pmap
)) {
1486 if ((fault_type
& VM_PROT_WRITE
) == 0)
1487 fs
->prot
&= ~VM_PROT_WRITE
;
1490 /* vm_object_hold(fs->object); implied b/c object == first_object */
1494 * The entire backing chain from first_object to object
1495 * inclusive is chainlocked.
1497 * If the object is dead, we stop here
1499 if (fs
->object
->flags
& OBJ_DEAD
) {
1500 vm_object_pip_wakeup(fs
->first_object
);
1501 vm_object_chain_release_all(fs
->first_object
,
1503 if (fs
->object
!= fs
->first_object
)
1504 vm_object_drop(fs
->object
);
1505 unlock_and_deallocate(fs
);
1506 return (KERN_PROTECTION_FAILURE
);
1510 * See if the page is resident. Wait/Retry if the page is
1511 * busy (lots of stuff may have changed so we can't continue
1514 * We can theoretically allow the soft-busy case on a read
1515 * fault if the page is marked valid, but since such
1516 * pages are typically already pmap'd, putting that
1517 * special case in might be more effort then it is
1518 * worth. We cannot under any circumstances mess
1519 * around with a vm_page_t->busy page except, perhaps,
1522 fs
->m
= vm_page_lookup_busy_try(fs
->object
, pindex
,
1525 vm_object_pip_wakeup(fs
->first_object
);
1526 vm_object_chain_release_all(fs
->first_object
,
1528 if (fs
->object
!= fs
->first_object
)
1529 vm_object_drop(fs
->object
);
1531 vm_page_sleep_busy(fs
->m
, TRUE
, "vmpfw");
1532 mycpu
->gd_cnt
.v_intrans
++;
1533 /*vm_object_deallocate(fs->first_object);*/
1534 /*fs->first_object = NULL;*/
1536 return (KERN_TRY_AGAIN
);
1540 * The page is busied for us.
1542 * If reactivating a page from PQ_CACHE we may have
1545 int queue
= fs
->m
->queue
;
1546 vm_page_unqueue_nowakeup(fs
->m
);
1548 if ((queue
- fs
->m
->pc
) == PQ_CACHE
&&
1549 vm_page_count_severe()) {
1550 vm_page_activate(fs
->m
);
1551 vm_page_wakeup(fs
->m
);
1553 vm_object_pip_wakeup(fs
->first_object
);
1554 vm_object_chain_release_all(fs
->first_object
,
1556 if (fs
->object
!= fs
->first_object
)
1557 vm_object_drop(fs
->object
);
1558 unlock_and_deallocate(fs
);
1559 if (allow_nofault
== 0 ||
1560 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1565 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1566 return (KERN_PROTECTION_FAILURE
);
1568 return (KERN_TRY_AGAIN
);
1572 * If it still isn't completely valid (readable),
1573 * or if a read-ahead-mark is set on the VM page,
1574 * jump to readrest, else we found the page and
1577 * We can release the spl once we have marked the
1580 if (fs
->m
->object
!= &kernel_object
) {
1581 if ((fs
->m
->valid
& VM_PAGE_BITS_ALL
) !=
1585 if (fs
->m
->flags
& PG_RAM
) {
1588 vm_page_flag_clear(fs
->m
, PG_RAM
);
1592 break; /* break to PAGE HAS BEEN FOUND */
1596 * Page is not resident, If this is the search termination
1597 * or the pager might contain the page, allocate a new page.
1599 if (TRYPAGER(fs
) || fs
->object
== fs
->first_object
) {
1601 * Allocating, must be exclusive.
1603 if (fs
->object
== fs
->first_object
&&
1605 fs
->first_shared
= 0;
1606 vm_object_pip_wakeup(fs
->first_object
);
1607 vm_object_chain_release_all(fs
->first_object
,
1609 if (fs
->object
!= fs
->first_object
)
1610 vm_object_drop(fs
->object
);
1611 unlock_and_deallocate(fs
);
1612 return (KERN_TRY_AGAIN
);
1614 if (fs
->object
!= fs
->first_object
&&
1616 fs
->first_shared
= 0;
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
);
1623 unlock_and_deallocate(fs
);
1624 return (KERN_TRY_AGAIN
);
1628 * If the page is beyond the object size we fail
1630 if (pindex
>= fs
->object
->size
) {
1631 vm_object_pip_wakeup(fs
->first_object
);
1632 vm_object_chain_release_all(fs
->first_object
,
1634 if (fs
->object
!= fs
->first_object
)
1635 vm_object_drop(fs
->object
);
1636 unlock_and_deallocate(fs
);
1637 return (KERN_PROTECTION_FAILURE
);
1641 * Allocate a new page for this object/offset pair.
1643 * It is possible for the allocation to race, so
1647 if (!vm_page_count_severe()) {
1648 fs
->m
= vm_page_alloc(fs
->object
, pindex
,
1649 ((fs
->vp
|| fs
->object
->backing_object
) ?
1650 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
:
1651 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
|
1652 VM_ALLOC_USE_GD
| VM_ALLOC_ZERO
));
1654 if (fs
->m
== NULL
) {
1655 vm_object_pip_wakeup(fs
->first_object
);
1656 vm_object_chain_release_all(fs
->first_object
,
1658 if (fs
->object
!= fs
->first_object
)
1659 vm_object_drop(fs
->object
);
1660 unlock_and_deallocate(fs
);
1661 if (allow_nofault
== 0 ||
1662 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1667 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1668 return (KERN_PROTECTION_FAILURE
);
1670 return (KERN_TRY_AGAIN
);
1674 * Fall through to readrest. We have a new page which
1675 * will have to be paged (since m->valid will be 0).
1681 * We have found an invalid or partially valid page, a
1682 * page with a read-ahead mark which might be partially or
1683 * fully valid (and maybe dirty too), or we have allocated
1686 * Attempt to fault-in the page if there is a chance that the
1687 * pager has it, and potentially fault in additional pages
1690 * If TRYPAGER is true then fs.m will be non-NULL and busied
1696 u_char behavior
= vm_map_entry_behavior(fs
->entry
);
1698 if (behavior
== MAP_ENTRY_BEHAV_RANDOM
)
1704 * Doing I/O may synchronously insert additional
1705 * pages so we can't be shared at this point either.
1707 * NOTE: We can't free fs->m here in the allocated
1708 * case (fs->object != fs->first_object) as
1709 * this would require an exclusively locked
1712 if (fs
->object
== fs
->first_object
&&
1714 vm_page_deactivate(fs
->m
);
1715 vm_page_wakeup(fs
->m
);
1717 fs
->first_shared
= 0;
1718 vm_object_pip_wakeup(fs
->first_object
);
1719 vm_object_chain_release_all(fs
->first_object
,
1721 if (fs
->object
!= fs
->first_object
)
1722 vm_object_drop(fs
->object
);
1723 unlock_and_deallocate(fs
);
1724 return (KERN_TRY_AGAIN
);
1726 if (fs
->object
!= fs
->first_object
&&
1728 vm_page_deactivate(fs
->m
);
1729 vm_page_wakeup(fs
->m
);
1731 fs
->first_shared
= 0;
1733 vm_object_pip_wakeup(fs
->first_object
);
1734 vm_object_chain_release_all(fs
->first_object
,
1736 if (fs
->object
!= fs
->first_object
)
1737 vm_object_drop(fs
->object
);
1738 unlock_and_deallocate(fs
);
1739 return (KERN_TRY_AGAIN
);
1743 * Avoid deadlocking against the map when doing I/O.
1744 * fs.object and the page is PG_BUSY'd.
1746 * NOTE: Once unlocked, fs->entry can become stale
1747 * so this will NULL it out.
1749 * NOTE: fs->entry is invalid until we relock the
1750 * map and verify that the timestamp has not
1756 * Acquire the page data. We still hold a ref on
1757 * fs.object and the page has been PG_BUSY's.
1759 * The pager may replace the page (for example, in
1760 * order to enter a fictitious page into the
1761 * object). If it does so it is responsible for
1762 * cleaning up the passed page and properly setting
1763 * the new page PG_BUSY.
1765 * If we got here through a PG_RAM read-ahead
1766 * mark the page may be partially dirty and thus
1767 * not freeable. Don't bother checking to see
1768 * if the pager has the page because we can't free
1769 * it anyway. We have to depend on the get_page
1770 * operation filling in any gaps whether there is
1771 * backing store or not.
1773 rv
= vm_pager_get_page(fs
->object
, &fs
->m
, seqaccess
);
1775 if (rv
== VM_PAGER_OK
) {
1777 * Relookup in case pager changed page. Pager
1778 * is responsible for disposition of old page
1781 * XXX other code segments do relookups too.
1782 * It's a bad abstraction that needs to be
1785 fs
->m
= vm_page_lookup(fs
->object
, pindex
);
1786 if (fs
->m
== NULL
) {
1787 vm_object_pip_wakeup(fs
->first_object
);
1788 vm_object_chain_release_all(
1789 fs
->first_object
, fs
->object
);
1790 if (fs
->object
!= fs
->first_object
)
1791 vm_object_drop(fs
->object
);
1792 unlock_and_deallocate(fs
);
1793 return (KERN_TRY_AGAIN
);
1796 break; /* break to PAGE HAS BEEN FOUND */
1800 * Remove the bogus page (which does not exist at this
1801 * object/offset); before doing so, we must get back
1802 * our object lock to preserve our invariant.
1804 * Also wake up any other process that may want to bring
1807 * If this is the top-level object, we must leave the
1808 * busy page to prevent another process from rushing
1809 * past us, and inserting the page in that object at
1810 * the same time that we are.
1812 if (rv
== VM_PAGER_ERROR
) {
1814 kprintf("vm_fault: pager read error, "
1819 kprintf("vm_fault: pager read error, "
1827 * Data outside the range of the pager or an I/O error
1829 * The page may have been wired during the pagein,
1830 * e.g. by the buffer cache, and cannot simply be
1831 * freed. Call vnode_pager_freepage() to deal with it.
1833 * Also note that we cannot free the page if we are
1834 * holding the related object shared. XXX not sure
1835 * what to do in that case.
1837 if (fs
->object
!= fs
->first_object
) {
1838 vnode_pager_freepage(fs
->m
);
1841 * XXX - we cannot just fall out at this
1842 * point, m has been freed and is invalid!
1846 * XXX - the check for kernel_map is a kludge to work
1847 * around having the machine panic on a kernel space
1848 * fault w/ I/O error.
1850 if (((fs
->map
!= &kernel_map
) &&
1851 (rv
== VM_PAGER_ERROR
)) || (rv
== VM_PAGER_BAD
)) {
1853 if (fs
->first_shared
) {
1854 vm_page_deactivate(fs
->m
);
1855 vm_page_wakeup(fs
->m
);
1857 vnode_pager_freepage(fs
->m
);
1861 vm_object_pip_wakeup(fs
->first_object
);
1862 vm_object_chain_release_all(fs
->first_object
,
1864 if (fs
->object
!= fs
->first_object
)
1865 vm_object_drop(fs
->object
);
1866 unlock_and_deallocate(fs
);
1867 if (rv
== VM_PAGER_ERROR
)
1868 return (KERN_FAILURE
);
1870 return (KERN_PROTECTION_FAILURE
);
1876 * We get here if the object has a default pager (or unwiring)
1877 * or the pager doesn't have the page.
1879 * fs->first_m will be used for the COW unless we find a
1880 * deeper page to be mapped read-only, in which case the
1881 * unlock*(fs) will free first_m.
1883 if (fs
->object
== fs
->first_object
)
1884 fs
->first_m
= fs
->m
;
1887 * Move on to the next object. The chain lock should prevent
1888 * the backing_object from getting ripped out from under us.
1890 * The object lock for the next object is governed by
1893 if ((next_object
= fs
->object
->backing_object
) != NULL
) {
1895 vm_object_hold_shared(next_object
);
1897 vm_object_hold(next_object
);
1898 vm_object_chain_acquire(next_object
, fs
->shared
);
1899 KKASSERT(next_object
== fs
->object
->backing_object
);
1900 pindex
+= OFF_TO_IDX(fs
->object
->backing_object_offset
);
1903 if (next_object
== NULL
) {
1905 * If there's no object left, fill the page in the top
1906 * object with zeros.
1908 if (fs
->object
!= fs
->first_object
) {
1910 if (fs
->first_object
->backing_object
!=
1912 vm_object_hold(fs
->first_object
->backing_object
);
1915 vm_object_chain_release_all(
1916 fs
->first_object
->backing_object
,
1919 if (fs
->first_object
->backing_object
!=
1921 vm_object_drop(fs
->first_object
->backing_object
);
1924 vm_object_pip_wakeup(fs
->object
);
1925 vm_object_drop(fs
->object
);
1926 fs
->object
= fs
->first_object
;
1927 pindex
= first_pindex
;
1928 fs
->m
= fs
->first_m
;
1933 * Zero the page and mark it valid.
1935 vm_page_zero_fill(fs
->m
);
1936 mycpu
->gd_cnt
.v_zfod
++;
1937 fs
->m
->valid
= VM_PAGE_BITS_ALL
;
1938 break; /* break to PAGE HAS BEEN FOUND */
1940 if (fs
->object
!= fs
->first_object
) {
1941 vm_object_pip_wakeup(fs
->object
);
1942 vm_object_lock_swap();
1943 vm_object_drop(fs
->object
);
1945 KASSERT(fs
->object
!= next_object
,
1946 ("object loop %p", next_object
));
1947 fs
->object
= next_object
;
1948 vm_object_pip_add(fs
->object
, 1);
1952 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1955 * object still held.
1957 * local shared variable may be different from fs->shared.
1959 * If the page is being written, but isn't already owned by the
1960 * top-level object, we have to copy it into a new page owned by the
1963 KASSERT((fs
->m
->flags
& PG_BUSY
) != 0,
1964 ("vm_fault: not busy after main loop"));
1966 if (fs
->object
!= fs
->first_object
) {
1968 * We only really need to copy if we want to write it.
1970 if (fault_type
& VM_PROT_WRITE
) {
1972 * This allows pages to be virtually copied from a
1973 * backing_object into the first_object, where the
1974 * backing object has no other refs to it, and cannot
1975 * gain any more refs. Instead of a bcopy, we just
1976 * move the page from the backing object to the
1977 * first object. Note that we must mark the page
1978 * dirty in the first object so that it will go out
1979 * to swap when needed.
1983 * Must be holding exclusive locks
1985 fs
->first_shared
== 0 &&
1988 * Map, if present, has not changed
1991 fs
->map_generation
== fs
->map
->timestamp
) &&
1993 * Only one shadow object
1995 (fs
->object
->shadow_count
== 1) &&
1997 * No COW refs, except us
1999 (fs
->object
->ref_count
== 1) &&
2001 * No one else can look this object up
2003 (fs
->object
->handle
== NULL
) &&
2005 * No other ways to look the object up
2007 ((fs
->object
->type
== OBJT_DEFAULT
) ||
2008 (fs
->object
->type
== OBJT_SWAP
)) &&
2010 * We don't chase down the shadow chain
2012 (fs
->object
== fs
->first_object
->backing_object
) &&
2015 * grab the lock if we need to
2017 (fs
->lookup_still_valid
||
2019 lockmgr(&fs
->map
->lock
, LK_EXCLUSIVE
|LK_NOWAIT
) == 0)
2022 * (first_m) and (m) are both busied. We have
2023 * move (m) into (first_m)'s object/pindex
2024 * in an atomic fashion, then free (first_m).
2026 * first_object is held so second remove
2027 * followed by the rename should wind
2028 * up being atomic. vm_page_free() might
2029 * block so we don't do it until after the
2032 fs
->lookup_still_valid
= 1;
2033 vm_page_protect(fs
->first_m
, VM_PROT_NONE
);
2034 vm_page_remove(fs
->first_m
);
2035 vm_page_rename(fs
->m
, fs
->first_object
,
2037 vm_page_free(fs
->first_m
);
2038 fs
->first_m
= fs
->m
;
2040 mycpu
->gd_cnt
.v_cow_optim
++;
2043 * Oh, well, lets copy it.
2045 * Why are we unmapping the original page
2046 * here? Well, in short, not all accessors
2047 * of user memory go through the pmap. The
2048 * procfs code doesn't have access user memory
2049 * via a local pmap, so vm_fault_page*()
2050 * can't call pmap_enter(). And the umtx*()
2051 * code may modify the COW'd page via a DMAP
2052 * or kernel mapping and not via the pmap,
2053 * leaving the original page still mapped
2054 * read-only into the pmap.
2056 * So we have to remove the page from at
2057 * least the current pmap if it is in it.
2058 * Just remove it from all pmaps.
2060 KKASSERT(fs
->first_shared
== 0);
2061 vm_page_copy(fs
->m
, fs
->first_m
);
2062 vm_page_protect(fs
->m
, VM_PROT_NONE
);
2063 vm_page_event(fs
->m
, VMEVENT_COW
);
2067 * We no longer need the old page or object.
2073 * We intend to revert to first_object, undo the
2074 * chain lock through to that.
2077 if (fs
->first_object
->backing_object
!= fs
->object
)
2078 vm_object_hold(fs
->first_object
->backing_object
);
2080 vm_object_chain_release_all(
2081 fs
->first_object
->backing_object
,
2084 if (fs
->first_object
->backing_object
!= fs
->object
)
2085 vm_object_drop(fs
->first_object
->backing_object
);
2089 * fs->object != fs->first_object due to above
2092 vm_object_pip_wakeup(fs
->object
);
2093 vm_object_drop(fs
->object
);
2096 * Only use the new page below...
2098 mycpu
->gd_cnt
.v_cow_faults
++;
2099 fs
->m
= fs
->first_m
;
2100 fs
->object
= fs
->first_object
;
2101 pindex
= first_pindex
;
2104 * If it wasn't a write fault avoid having to copy
2105 * the page by mapping it read-only.
2107 fs
->prot
&= ~VM_PROT_WRITE
;
2112 * Relock the map if necessary, then check the generation count.
2113 * relock_map() will update fs->timestamp to account for the
2114 * relocking if necessary.
2116 * If the count has changed after relocking then all sorts of
2117 * crap may have happened and we have to retry.
2119 * NOTE: The relock_map() can fail due to a deadlock against
2120 * the vm_page we are holding BUSY.
2122 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
2123 if (relock_map(fs
) ||
2124 fs
->map
->timestamp
!= fs
->map_generation
) {
2126 vm_object_pip_wakeup(fs
->first_object
);
2127 vm_object_chain_release_all(fs
->first_object
,
2129 if (fs
->object
!= fs
->first_object
)
2130 vm_object_drop(fs
->object
);
2131 unlock_and_deallocate(fs
);
2132 return (KERN_TRY_AGAIN
);
2137 * If the fault is a write, we know that this page is being
2138 * written NOW so dirty it explicitly to save on pmap_is_modified()
2141 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2142 * if the page is already dirty to prevent data written with
2143 * the expectation of being synced from not being synced.
2144 * Likewise if this entry does not request NOSYNC then make
2145 * sure the page isn't marked NOSYNC. Applications sharing
2146 * data should use the same flags to avoid ping ponging.
2148 * Also tell the backing pager, if any, that it should remove
2149 * any swap backing since the page is now dirty.
2151 vm_page_activate(fs
->m
);
2152 if (fs
->prot
& VM_PROT_WRITE
) {
2153 vm_object_set_writeable_dirty(fs
->m
->object
);
2154 vm_set_nosync(fs
->m
, fs
->entry
);
2155 if (fs
->fault_flags
& VM_FAULT_DIRTY
) {
2156 vm_page_dirty(fs
->m
);
2157 if (fs
->m
->flags
& PG_SWAPPED
) {
2159 * If the page is swapped out we have to call
2160 * swap_pager_unswapped() which requires an
2161 * exclusive object lock. If we are shared,
2162 * we must clear the shared flag and retry.
2164 if ((fs
->object
== fs
->first_object
&&
2165 fs
->first_shared
) ||
2166 (fs
->object
!= fs
->first_object
&&
2168 vm_page_wakeup(fs
->m
);
2170 if (fs
->object
== fs
->first_object
)
2171 fs
->first_shared
= 0;
2174 vm_object_pip_wakeup(fs
->first_object
);
2175 vm_object_chain_release_all(
2176 fs
->first_object
, fs
->object
);
2177 if (fs
->object
!= fs
->first_object
)
2178 vm_object_drop(fs
->object
);
2179 unlock_and_deallocate(fs
);
2180 return (KERN_TRY_AGAIN
);
2182 swap_pager_unswapped(fs
->m
);
2187 vm_object_pip_wakeup(fs
->first_object
);
2188 vm_object_chain_release_all(fs
->first_object
, fs
->object
);
2189 if (fs
->object
!= fs
->first_object
)
2190 vm_object_drop(fs
->object
);
2193 * Page had better still be busy. We are still locked up and
2194 * fs->object will have another PIP reference if it is not equal
2195 * to fs->first_object.
2197 KASSERT(fs
->m
->flags
& PG_BUSY
,
2198 ("vm_fault: page %p not busy!", fs
->m
));
2201 * Sanity check: page must be completely valid or it is not fit to
2202 * map into user space. vm_pager_get_pages() ensures this.
2204 if (fs
->m
->valid
!= VM_PAGE_BITS_ALL
) {
2205 vm_page_zero_invalid(fs
->m
, TRUE
);
2206 kprintf("Warning: page %p partially invalid on fault\n", fs
->m
);
2209 return (KERN_SUCCESS
);
2213 * Wire down a range of virtual addresses in a map. The entry in question
2214 * should be marked in-transition and the map must be locked. We must
2215 * release the map temporarily while faulting-in the page to avoid a
2216 * deadlock. Note that the entry may be clipped while we are blocked but
2217 * will never be freed.
2222 vm_fault_wire(vm_map_t map
, vm_map_entry_t entry
,
2223 boolean_t user_wire
, int kmflags
)
2225 boolean_t fictitious
;
2235 lwkt_gettoken(&map
->token
);
2238 wire_prot
= VM_PROT_READ
;
2239 fault_flags
= VM_FAULT_USER_WIRE
;
2241 wire_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
2242 fault_flags
= VM_FAULT_CHANGE_WIRING
;
2244 if (kmflags
& KM_NOTLBSYNC
)
2245 wire_prot
|= VM_PROT_NOSYNC
;
2247 pmap
= vm_map_pmap(map
);
2248 start
= entry
->start
;
2250 switch(entry
->maptype
) {
2251 case VM_MAPTYPE_NORMAL
:
2252 case VM_MAPTYPE_VPAGETABLE
:
2253 fictitious
= entry
->object
.vm_object
&&
2254 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2255 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2257 case VM_MAPTYPE_UKSMAP
:
2265 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2271 * We simulate a fault to get the page and enter it in the physical
2274 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2275 rv
= vm_fault(map
, va
, wire_prot
, fault_flags
);
2277 while (va
> start
) {
2279 m
= pmap_unwire(pmap
, va
);
2280 if (m
&& !fictitious
) {
2281 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2282 vm_page_unwire(m
, 1);
2292 lwkt_reltoken(&map
->token
);
2297 * Unwire a range of virtual addresses in a map. The map should be
2301 vm_fault_unwire(vm_map_t map
, vm_map_entry_t entry
)
2303 boolean_t fictitious
;
2310 lwkt_gettoken(&map
->token
);
2312 pmap
= vm_map_pmap(map
);
2313 start
= entry
->start
;
2315 fictitious
= entry
->object
.vm_object
&&
2316 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2317 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2318 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2322 * Since the pages are wired down, we must be able to get their
2323 * mappings from the physical map system.
2325 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2326 m
= pmap_unwire(pmap
, va
);
2327 if (m
&& !fictitious
) {
2328 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2329 vm_page_unwire(m
, 1);
2333 lwkt_reltoken(&map
->token
);
2337 * Copy all of the pages from a wired-down map entry to another.
2339 * The source and destination maps must be locked for write.
2340 * The source and destination maps token must be held
2341 * The source map entry must be wired down (or be a sharing map
2342 * entry corresponding to a main map entry that is wired down).
2344 * No other requirements.
2346 * XXX do segment optimization
2349 vm_fault_copy_entry(vm_map_t dst_map
, vm_map_t src_map
,
2350 vm_map_entry_t dst_entry
, vm_map_entry_t src_entry
)
2352 vm_object_t dst_object
;
2353 vm_object_t src_object
;
2354 vm_ooffset_t dst_offset
;
2355 vm_ooffset_t src_offset
;
2361 src_object
= src_entry
->object
.vm_object
;
2362 src_offset
= src_entry
->offset
;
2365 * Create the top-level object for the destination entry. (Doesn't
2366 * actually shadow anything - we copy the pages directly.)
2368 vm_map_entry_allocate_object(dst_entry
);
2369 dst_object
= dst_entry
->object
.vm_object
;
2371 prot
= dst_entry
->max_protection
;
2374 * Loop through all of the pages in the entry's range, copying each
2375 * one from the source object (it should be there) to the destination
2378 vm_object_hold(src_object
);
2379 vm_object_hold(dst_object
);
2380 for (vaddr
= dst_entry
->start
, dst_offset
= 0;
2381 vaddr
< dst_entry
->end
;
2382 vaddr
+= PAGE_SIZE
, dst_offset
+= PAGE_SIZE
) {
2385 * Allocate a page in the destination object
2388 dst_m
= vm_page_alloc(dst_object
,
2389 OFF_TO_IDX(dst_offset
),
2391 if (dst_m
== NULL
) {
2394 } while (dst_m
== NULL
);
2397 * Find the page in the source object, and copy it in.
2398 * (Because the source is wired down, the page will be in
2401 src_m
= vm_page_lookup(src_object
,
2402 OFF_TO_IDX(dst_offset
+ src_offset
));
2404 panic("vm_fault_copy_wired: page missing");
2406 vm_page_copy(src_m
, dst_m
);
2407 vm_page_event(src_m
, VMEVENT_COW
);
2410 * Enter it in the pmap...
2412 pmap_enter(dst_map
->pmap
, vaddr
, dst_m
, prot
, FALSE
, dst_entry
);
2415 * Mark it no longer busy, and put it on the active list.
2417 vm_page_activate(dst_m
);
2418 vm_page_wakeup(dst_m
);
2420 vm_object_drop(dst_object
);
2421 vm_object_drop(src_object
);
2427 * This routine checks around the requested page for other pages that
2428 * might be able to be faulted in. This routine brackets the viable
2429 * pages for the pages to be paged in.
2432 * m, rbehind, rahead
2435 * marray (array of vm_page_t), reqpage (index of requested page)
2438 * number of pages in marray
2441 vm_fault_additional_pages(vm_page_t m
, int rbehind
, int rahead
,
2442 vm_page_t
*marray
, int *reqpage
)
2446 vm_pindex_t pindex
, startpindex
, endpindex
, tpindex
;
2448 int cbehind
, cahead
;
2454 * we don't fault-ahead for device pager
2456 if ((object
->type
== OBJT_DEVICE
) ||
2457 (object
->type
== OBJT_MGTDEVICE
)) {
2464 * if the requested page is not available, then give up now
2466 if (!vm_pager_has_page(object
, pindex
, &cbehind
, &cahead
)) {
2467 *reqpage
= 0; /* not used by caller, fix compiler warn */
2471 if ((cbehind
== 0) && (cahead
== 0)) {
2477 if (rahead
> cahead
) {
2481 if (rbehind
> cbehind
) {
2486 * Do not do any readahead if we have insufficient free memory.
2488 * XXX code was broken disabled before and has instability
2489 * with this conditonal fixed, so shortcut for now.
2491 if (burst_fault
== 0 || vm_page_count_severe()) {
2498 * scan backward for the read behind pages -- in memory
2500 * Assume that if the page is not found an interrupt will not
2501 * create it. Theoretically interrupts can only remove (busy)
2502 * pages, not create new associations.
2505 if (rbehind
> pindex
) {
2509 startpindex
= pindex
- rbehind
;
2512 vm_object_hold(object
);
2513 for (tpindex
= pindex
; tpindex
> startpindex
; --tpindex
) {
2514 if (vm_page_lookup(object
, tpindex
- 1))
2519 while (tpindex
< pindex
) {
2520 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2523 for (j
= 0; j
< i
; j
++) {
2524 vm_page_free(marray
[j
]);
2526 vm_object_drop(object
);
2535 vm_object_drop(object
);
2541 * Assign requested page
2548 * Scan forwards for read-ahead pages
2550 tpindex
= pindex
+ 1;
2551 endpindex
= tpindex
+ rahead
;
2552 if (endpindex
> object
->size
)
2553 endpindex
= object
->size
;
2555 vm_object_hold(object
);
2556 while (tpindex
< endpindex
) {
2557 if (vm_page_lookup(object
, tpindex
))
2559 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2567 vm_object_drop(object
);
2575 * vm_prefault() provides a quick way of clustering pagefaults into a
2576 * processes address space. It is a "cousin" of pmap_object_init_pt,
2577 * except it runs at page fault time instead of mmap time.
2579 * vm.fast_fault Enables pre-faulting zero-fill pages
2581 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2582 * prefault. Scan stops in either direction when
2583 * a page is found to already exist.
2585 * This code used to be per-platform pmap_prefault(). It is now
2586 * machine-independent and enhanced to also pre-fault zero-fill pages
2587 * (see vm.fast_fault) as well as make them writable, which greatly
2588 * reduces the number of page faults programs incur.
2590 * Application performance when pre-faulting zero-fill pages is heavily
2591 * dependent on the application. Very tiny applications like /bin/echo
2592 * lose a little performance while applications of any appreciable size
2593 * gain performance. Prefaulting multiple pages also reduces SMP
2594 * congestion and can improve SMP performance significantly.
2596 * NOTE! prot may allow writing but this only applies to the top level
2597 * object. If we wind up mapping a page extracted from a backing
2598 * object we have to make sure it is read-only.
2600 * NOTE! The caller has already handled any COW operations on the
2601 * vm_map_entry via the normal fault code. Do NOT call this
2602 * shortcut unless the normal fault code has run on this entry.
2604 * The related map must be locked.
2605 * No other requirements.
2607 static int vm_prefault_pages
= 8;
2608 SYSCTL_INT(_vm
, OID_AUTO
, prefault_pages
, CTLFLAG_RW
, &vm_prefault_pages
, 0,
2609 "Maximum number of pages to pre-fault");
2610 static int vm_fast_fault
= 1;
2611 SYSCTL_INT(_vm
, OID_AUTO
, fast_fault
, CTLFLAG_RW
, &vm_fast_fault
, 0,
2612 "Burst fault zero-fill regions");
2615 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2616 * is not already dirty by other means. This will prevent passive
2617 * filesystem syncing as well as 'sync' from writing out the page.
2620 vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
)
2622 if (entry
->eflags
& MAP_ENTRY_NOSYNC
) {
2624 vm_page_flag_set(m
, PG_NOSYNC
);
2626 vm_page_flag_clear(m
, PG_NOSYNC
);
2631 vm_prefault(pmap_t pmap
, vm_offset_t addra
, vm_map_entry_t entry
, int prot
,
2647 * Get stable max count value, disabled if set to 0
2649 maxpages
= vm_prefault_pages
;
2655 * We do not currently prefault mappings that use virtual page
2656 * tables. We do not prefault foreign pmaps.
2658 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2660 lp
= curthread
->td_lwp
;
2661 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2665 * Limit pre-fault count to 1024 pages.
2667 if (maxpages
> 1024)
2670 object
= entry
->object
.vm_object
;
2671 KKASSERT(object
!= NULL
);
2672 KKASSERT(object
== entry
->object
.vm_object
);
2675 * NOTE: VM_FAULT_DIRTY allowed later so must hold object exclusively
2676 * now (or do something more complex XXX).
2678 vm_object_hold(object
);
2679 vm_object_chain_acquire(object
, 0);
2683 for (i
= 0; i
< maxpages
; ++i
) {
2684 vm_object_t lobject
;
2685 vm_object_t nobject
;
2690 * This can eat a lot of time on a heavily contended
2691 * machine so yield on the tick if needed.
2697 * Calculate the page to pre-fault, stopping the scan in
2698 * each direction separately if the limit is reached.
2703 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2707 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2709 if (addr
< entry
->start
) {
2715 if (addr
>= entry
->end
) {
2723 * Skip pages already mapped, and stop scanning in that
2724 * direction. When the scan terminates in both directions
2727 if (pmap_prefault_ok(pmap
, addr
) == 0) {
2738 * Follow the VM object chain to obtain the page to be mapped
2741 * If we reach the terminal object without finding a page
2742 * and we determine it would be advantageous, then allocate
2743 * a zero-fill page for the base object. The base object
2744 * is guaranteed to be OBJT_DEFAULT for this case.
2746 * In order to not have to check the pager via *haspage*()
2747 * we stop if any non-default object is encountered. e.g.
2748 * a vnode or swap object would stop the loop.
2750 index
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
2755 KKASSERT(lobject
== entry
->object
.vm_object
);
2756 /*vm_object_hold(lobject); implied */
2758 while ((m
= vm_page_lookup_busy_try(lobject
, pindex
,
2759 TRUE
, &error
)) == NULL
) {
2760 if (lobject
->type
!= OBJT_DEFAULT
)
2762 if (lobject
->backing_object
== NULL
) {
2763 if (vm_fast_fault
== 0)
2765 if ((prot
& VM_PROT_WRITE
) == 0 ||
2766 vm_page_count_min(0)) {
2771 * NOTE: Allocated from base object
2773 m
= vm_page_alloc(object
, index
,
2782 /* lobject = object .. not needed */
2785 if (lobject
->backing_object_offset
& PAGE_MASK
)
2787 nobject
= lobject
->backing_object
;
2788 vm_object_hold(nobject
);
2789 KKASSERT(nobject
== lobject
->backing_object
);
2790 pindex
+= lobject
->backing_object_offset
>> PAGE_SHIFT
;
2791 if (lobject
!= object
) {
2792 vm_object_lock_swap();
2793 vm_object_drop(lobject
);
2796 pprot
&= ~VM_PROT_WRITE
;
2797 vm_object_chain_acquire(lobject
, 0);
2801 * NOTE: A non-NULL (m) will be associated with lobject if
2802 * it was found there, otherwise it is probably a
2803 * zero-fill page associated with the base object.
2805 * Give-up if no page is available.
2808 if (lobject
!= object
) {
2810 if (object
->backing_object
!= lobject
)
2811 vm_object_hold(object
->backing_object
);
2813 vm_object_chain_release_all(
2814 object
->backing_object
, lobject
);
2816 if (object
->backing_object
!= lobject
)
2817 vm_object_drop(object
->backing_object
);
2819 vm_object_drop(lobject
);
2825 * The object must be marked dirty if we are mapping a
2826 * writable page. m->object is either lobject or object,
2827 * both of which are still held. Do this before we
2828 * potentially drop the object.
2830 if (pprot
& VM_PROT_WRITE
)
2831 vm_object_set_writeable_dirty(m
->object
);
2834 * Do not conditionalize on PG_RAM. If pages are present in
2835 * the VM system we assume optimal caching. If caching is
2836 * not optimal the I/O gravy train will be restarted when we
2837 * hit an unavailable page. We do not want to try to restart
2838 * the gravy train now because we really don't know how much
2839 * of the object has been cached. The cost for restarting
2840 * the gravy train should be low (since accesses will likely
2841 * be I/O bound anyway).
2843 if (lobject
!= object
) {
2845 if (object
->backing_object
!= lobject
)
2846 vm_object_hold(object
->backing_object
);
2848 vm_object_chain_release_all(object
->backing_object
,
2851 if (object
->backing_object
!= lobject
)
2852 vm_object_drop(object
->backing_object
);
2854 vm_object_drop(lobject
);
2858 * Enter the page into the pmap if appropriate. If we had
2859 * allocated the page we have to place it on a queue. If not
2860 * we just have to make sure it isn't on the cache queue
2861 * (pages on the cache queue are not allowed to be mapped).
2865 * Page must be zerod.
2867 vm_page_zero_fill(m
);
2868 mycpu
->gd_cnt
.v_zfod
++;
2869 m
->valid
= VM_PAGE_BITS_ALL
;
2872 * Handle dirty page case
2874 if (pprot
& VM_PROT_WRITE
)
2875 vm_set_nosync(m
, entry
);
2876 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2877 mycpu
->gd_cnt
.v_vm_faults
++;
2878 if (curthread
->td_lwp
)
2879 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2880 vm_page_deactivate(m
);
2881 if (pprot
& VM_PROT_WRITE
) {
2882 /*vm_object_set_writeable_dirty(m->object);*/
2883 vm_set_nosync(m
, entry
);
2884 if (fault_flags
& VM_FAULT_DIRTY
) {
2887 swap_pager_unswapped(m
);
2892 /* couldn't busy page, no wakeup */
2894 ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
2895 (m
->flags
& PG_FICTITIOUS
) == 0) {
2897 * A fully valid page not undergoing soft I/O can
2898 * be immediately entered into the pmap.
2900 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
2901 vm_page_deactivate(m
);
2902 if (pprot
& VM_PROT_WRITE
) {
2903 /*vm_object_set_writeable_dirty(m->object);*/
2904 vm_set_nosync(m
, entry
);
2905 if (fault_flags
& VM_FAULT_DIRTY
) {
2908 swap_pager_unswapped(m
);
2911 if (pprot
& VM_PROT_WRITE
)
2912 vm_set_nosync(m
, entry
);
2913 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2914 mycpu
->gd_cnt
.v_vm_faults
++;
2915 if (curthread
->td_lwp
)
2916 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2922 vm_object_chain_release(object
);
2923 vm_object_drop(object
);
2927 * Object can be held shared
2930 vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
2931 vm_map_entry_t entry
, int prot
, int fault_flags
)
2944 * Get stable max count value, disabled if set to 0
2946 maxpages
= vm_prefault_pages
;
2952 * We do not currently prefault mappings that use virtual page
2953 * tables. We do not prefault foreign pmaps.
2955 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2957 lp
= curthread
->td_lwp
;
2958 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2960 object
= entry
->object
.vm_object
;
2961 if (object
->backing_object
!= NULL
)
2963 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
2966 * Limit pre-fault count to 1024 pages.
2968 if (maxpages
> 1024)
2973 for (i
= 0; i
< maxpages
; ++i
) {
2977 * Calculate the page to pre-fault, stopping the scan in
2978 * each direction separately if the limit is reached.
2983 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2987 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2989 if (addr
< entry
->start
) {
2995 if (addr
>= entry
->end
) {
3003 * Follow the VM object chain to obtain the page to be mapped
3004 * into the pmap. This version of the prefault code only
3005 * works with terminal objects.
3007 * The page must already exist. If we encounter a problem
3010 * WARNING! We cannot call swap_pager_unswapped() or insert
3011 * a new vm_page with a shared token.
3013 pindex
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
3015 m
= vm_page_lookup_busy_try(object
, pindex
, TRUE
, &error
);
3016 if (m
== NULL
|| error
)
3020 * Skip pages already mapped, and stop scanning in that
3021 * direction. When the scan terminates in both directions
3024 if (pmap_prefault_ok(pmap
, addr
) == 0) {
3036 * Stop if the page cannot be trivially entered into the
3039 if (((m
->valid
& VM_PAGE_BITS_ALL
) != VM_PAGE_BITS_ALL
) ||
3040 (m
->flags
& PG_FICTITIOUS
) ||
3041 ((m
->flags
& PG_SWAPPED
) &&
3042 (prot
& VM_PROT_WRITE
) &&
3043 (fault_flags
& VM_FAULT_DIRTY
))) {
3049 * Enter the page into the pmap. The object might be held
3050 * shared so we can't do any (serious) modifying operation
3053 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
3054 vm_page_deactivate(m
);
3055 if (prot
& VM_PROT_WRITE
) {
3056 vm_object_set_writeable_dirty(m
->object
);
3057 vm_set_nosync(m
, entry
);
3058 if (fault_flags
& VM_FAULT_DIRTY
) {
3060 /* can't happeen due to conditional above */
3061 /* swap_pager_unswapped(m); */
3064 pmap_enter(pmap
, addr
, m
, prot
, 0, entry
);
3065 mycpu
->gd_cnt
.v_vm_faults
++;
3066 if (curthread
->td_lwp
)
3067 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;