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");
159 static long vm_shared_hit
= 0;
160 SYSCTL_LONG(_vm
, OID_AUTO
, shared_hit
, CTLFLAG_RW
, &vm_shared_hit
, 0,
161 "Successful shared faults");
162 static long vm_shared_count
= 0;
163 SYSCTL_LONG(_vm
, OID_AUTO
, shared_count
, CTLFLAG_RW
, &vm_shared_count
, 0,
164 "Shared fault attempts");
165 static long vm_shared_miss
= 0;
166 SYSCTL_LONG(_vm
, OID_AUTO
, shared_miss
, CTLFLAG_RW
, &vm_shared_miss
, 0,
167 "Unsuccessful shared faults");
169 static int vm_fault_object(struct faultstate
*, vm_pindex_t
, vm_prot_t
, int);
170 static int vm_fault_vpagetable(struct faultstate
*, vm_pindex_t
*,
173 static int vm_fault_additional_pages (vm_page_t
, int, int, vm_page_t
*, int *);
175 static void vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
);
176 static void vm_prefault(pmap_t pmap
, vm_offset_t addra
,
177 vm_map_entry_t entry
, int prot
, int fault_flags
);
178 static void vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
179 vm_map_entry_t entry
, int prot
, int fault_flags
);
182 release_page(struct faultstate
*fs
)
184 vm_page_deactivate(fs
->m
);
185 vm_page_wakeup(fs
->m
);
190 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
191 * requires relocking and then checking the timestamp.
193 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
194 * not have to update fs->map_generation here.
196 * NOTE: This function can fail due to a deadlock against the caller's
197 * holding of a vm_page BUSY.
200 relock_map(struct faultstate
*fs
)
204 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
205 error
= vm_map_lock_read_to(fs
->map
);
207 fs
->lookup_still_valid
= TRUE
;
215 unlock_map(struct faultstate
*fs
)
217 if (fs
->lookup_still_valid
&& fs
->map
) {
218 vm_map_lookup_done(fs
->map
, fs
->entry
, 0);
219 fs
->lookup_still_valid
= FALSE
;
224 * Clean up after a successful call to vm_fault_object() so another call
225 * to vm_fault_object() can be made.
228 _cleanup_successful_fault(struct faultstate
*fs
, int relock
)
231 * We allocated a junk page for a COW operation that did
232 * not occur, the page must be freed.
234 if (fs
->object
!= fs
->first_object
) {
235 KKASSERT(fs
->first_shared
== 0);
236 vm_page_free(fs
->first_m
);
237 vm_object_pip_wakeup(fs
->object
);
244 fs
->object
= fs
->first_object
;
245 if (relock
&& fs
->lookup_still_valid
== FALSE
) {
247 vm_map_lock_read(fs
->map
);
248 fs
->lookup_still_valid
= TRUE
;
253 _unlock_things(struct faultstate
*fs
, int dealloc
)
255 _cleanup_successful_fault(fs
, 0);
257 /*vm_object_deallocate(fs->first_object);*/
258 /*fs->first_object = NULL; drop used later on */
261 if (fs
->vp
!= NULL
) {
267 #define unlock_things(fs) _unlock_things(fs, 0)
268 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
269 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
274 * Determine if the pager for the current object *might* contain the page.
276 * We only need to try the pager if this is not a default object (default
277 * objects are zero-fill and have no real pager), and if we are not taking
278 * a wiring fault or if the FS entry is wired.
280 #define TRYPAGER(fs) \
281 (fs->object->type != OBJT_DEFAULT && \
282 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
287 * Handle a page fault occuring at the given address, requiring the given
288 * permissions, in the map specified. If successful, the page is inserted
289 * into the associated physical map.
291 * NOTE: The given address should be truncated to the proper page address.
293 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
294 * a standard error specifying why the fault is fatal is returned.
296 * The map in question must be referenced, and remains so.
297 * The caller may hold no locks.
298 * No other requirements.
301 vm_fault(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
, int fault_flags
)
304 vm_pindex_t first_pindex
;
305 struct faultstate fs
;
311 inherit_prot
= fault_type
& VM_PROT_NOSYNC
;
313 fs
.fault_flags
= fault_flags
;
315 fs
.shared
= vm_shared_fault
;
316 fs
.first_shared
= vm_shared_fault
;
322 * vm_map interactions
324 if ((lp
= curthread
->td_lwp
) != NULL
)
325 lp
->lwp_flags
|= LWP_PAGING
;
326 lwkt_gettoken(&map
->token
);
330 * Find the vm_map_entry representing the backing store and resolve
331 * the top level object and page index. This may have the side
332 * effect of executing a copy-on-write on the map entry and/or
333 * creating a shadow object, but will not COW any actual VM pages.
335 * On success fs.map is left read-locked and various other fields
336 * are initialized but not otherwise referenced or locked.
338 * NOTE! vm_map_lookup will try to upgrade the fault_type to
339 * VM_FAULT_WRITE if the map entry is a virtual page table and also
340 * writable, so we can set the 'A'accessed bit in the virtual page
344 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
345 &fs
.entry
, &fs
.first_object
,
346 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
349 * If the lookup failed or the map protections are incompatible,
350 * the fault generally fails.
352 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
353 * tried to do a COW fault.
355 * If the caller is trying to do a user wiring we have more work
358 if (result
!= KERN_SUCCESS
) {
359 if (result
== KERN_FAILURE_NOFAULT
) {
360 result
= KERN_FAILURE
;
363 if (result
!= KERN_PROTECTION_FAILURE
||
364 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) != VM_FAULT_USER_WIRE
)
366 if (result
== KERN_INVALID_ADDRESS
&& growstack
&&
367 map
!= &kernel_map
&& curproc
!= NULL
) {
368 result
= vm_map_growstack(curproc
, vaddr
);
369 if (result
== KERN_SUCCESS
) {
374 result
= KERN_FAILURE
;
380 * If we are user-wiring a r/w segment, and it is COW, then
381 * we need to do the COW operation. Note that we don't
382 * currently COW RO sections now, because it is NOT desirable
383 * to COW .text. We simply keep .text from ever being COW'ed
384 * and take the heat that one cannot debug wired .text sections.
386 result
= vm_map_lookup(&fs
.map
, vaddr
,
387 VM_PROT_READ
|VM_PROT_WRITE
|
388 VM_PROT_OVERRIDE_WRITE
,
389 &fs
.entry
, &fs
.first_object
,
390 &first_pindex
, &fs
.first_prot
,
392 if (result
!= KERN_SUCCESS
) {
393 /* could also be KERN_FAILURE_NOFAULT */
394 result
= KERN_FAILURE
;
399 * If we don't COW now, on a user wire, the user will never
400 * be able to write to the mapping. If we don't make this
401 * restriction, the bookkeeping would be nearly impossible.
403 * XXX We have a shared lock, this will have a MP race but
404 * I don't see how it can hurt anything.
406 if ((fs
.entry
->protection
& VM_PROT_WRITE
) == 0)
407 fs
.entry
->max_protection
&= ~VM_PROT_WRITE
;
411 * fs.map is read-locked
413 * Misc checks. Save the map generation number to detect races.
415 fs
.map_generation
= fs
.map
->timestamp
;
416 fs
.lookup_still_valid
= TRUE
;
418 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
419 fs
.prot
= fs
.first_prot
; /* default (used by uksmap) */
421 if (fs
.entry
->eflags
& (MAP_ENTRY_NOFAULT
| MAP_ENTRY_KSTACK
)) {
422 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
423 panic("vm_fault: fault on nofault entry, addr: %p",
426 if ((fs
.entry
->eflags
& MAP_ENTRY_KSTACK
) &&
427 vaddr
>= fs
.entry
->start
&&
428 vaddr
< fs
.entry
->start
+ PAGE_SIZE
) {
429 panic("vm_fault: fault on stack guard, addr: %p",
435 * A user-kernel shared map has no VM object and bypasses
436 * everything. We execute the uksmap function with a temporary
437 * fictitious vm_page. The address is directly mapped with no
440 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
441 struct vm_page fakem
;
443 bzero(&fakem
, sizeof(fakem
));
444 fakem
.pindex
= first_pindex
;
445 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
446 fakem
.valid
= VM_PAGE_BITS_ALL
;
447 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
448 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
449 result
= KERN_FAILURE
;
453 pmap_enter(fs
.map
->pmap
, vaddr
, &fakem
, fs
.prot
| inherit_prot
,
459 * A system map entry may return a NULL object. No object means
460 * no pager means an unrecoverable kernel fault.
462 if (fs
.first_object
== NULL
) {
463 panic("vm_fault: unrecoverable fault at %p in entry %p",
464 (void *)vaddr
, fs
.entry
);
468 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
471 * Unfortunately a deadlock can occur if we are forced to page-in
472 * from swap, but diving all the way into the vm_pager_get_page()
473 * function to find out is too much. Just check the object type.
475 * The deadlock is a CAM deadlock on a busy VM page when trying
476 * to finish an I/O if another process gets stuck in
477 * vop_helper_read_shortcut() due to a swap fault.
479 if ((curthread
->td_flags
& TDF_NOFAULT
) &&
481 fs
.first_object
->type
== OBJT_VNODE
||
482 fs
.first_object
->type
== OBJT_SWAP
||
483 fs
.first_object
->backing_object
)) {
484 result
= KERN_FAILURE
;
490 * If the entry is wired we cannot change the page protection.
493 fault_type
= fs
.first_prot
;
496 * We generally want to avoid unnecessary exclusive modes on backing
497 * and terminal objects because this can seriously interfere with
498 * heavily fork()'d processes (particularly /bin/sh scripts).
500 * However, we also want to avoid unnecessary retries due to needed
501 * shared->exclusive promotion for common faults. Exclusive mode is
502 * always needed if any page insertion, rename, or free occurs in an
503 * object (and also indirectly if any I/O is done).
505 * The main issue here is going to be fs.first_shared. If the
506 * first_object has a backing object which isn't shadowed and the
507 * process is single-threaded we might as well use an exclusive
508 * lock/chain right off the bat.
510 if (fs
.first_shared
&& fs
.first_object
->backing_object
&&
511 LIST_EMPTY(&fs
.first_object
->shadow_head
) &&
512 curthread
->td_proc
&& curthread
->td_proc
->p_nthreads
== 1) {
517 * swap_pager_unswapped() needs an exclusive object
519 if (fault_flags
& (VM_FAULT_UNSWAP
| VM_FAULT_DIRTY
)) {
524 * Obtain a top-level object lock, shared or exclusive depending
525 * on fs.first_shared. If a shared lock winds up being insufficient
526 * we will retry with an exclusive lock.
528 * The vnode pager lock is always shared.
531 vm_object_hold_shared(fs
.first_object
);
533 vm_object_hold(fs
.first_object
);
535 fs
.vp
= vnode_pager_lock(fs
.first_object
);
538 * The page we want is at (first_object, first_pindex), but if the
539 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
540 * page table to figure out the actual pindex.
542 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
545 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
546 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
547 fs
.entry
->aux
.master_pde
,
549 if (result
== KERN_TRY_AGAIN
) {
550 vm_object_drop(fs
.first_object
);
554 if (result
!= KERN_SUCCESS
)
559 * Now we have the actual (object, pindex), fault in the page. If
560 * vm_fault_object() fails it will unlock and deallocate the FS
561 * data. If it succeeds everything remains locked and fs->object
562 * will have an additional PIP count if it is not equal to
565 * vm_fault_object will set fs->prot for the pmap operation. It is
566 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
567 * page can be safely written. However, it will force a read-only
568 * mapping for a read fault if the memory is managed by a virtual
571 * If the fault code uses the shared object lock shortcut
572 * we must not try to burst (we can't allocate VM pages).
574 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
576 if (debug_fault
> 0) {
578 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
579 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
580 result
, (intmax_t)vaddr
, fault_type
, fault_flags
,
581 fs
.m
, fs
.prot
, fs
.wired
, fs
.entry
);
584 if (result
== KERN_TRY_AGAIN
) {
585 vm_object_drop(fs
.first_object
);
589 if (result
!= KERN_SUCCESS
)
593 * On success vm_fault_object() does not unlock or deallocate, and fs.m
594 * will contain a busied page.
596 * Enter the page into the pmap and do pmap-related adjustments.
598 KKASSERT(fs
.lookup_still_valid
== TRUE
);
599 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
600 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
| inherit_prot
,
603 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
604 KKASSERT(fs
.m
->flags
& PG_BUSY
);
607 * If the page is not wired down, then put it where the pageout daemon
610 if (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) {
614 vm_page_unwire(fs
.m
, 1);
616 vm_page_activate(fs
.m
);
618 vm_page_wakeup(fs
.m
);
621 * Burst in a few more pages if possible. The fs.map should still
622 * be locked. To avoid interlocking against a vnode->getblk
623 * operation we had to be sure to unbusy our primary vm_page above
626 * A normal burst can continue down backing store, only execute
627 * if we are holding an exclusive lock, otherwise the exclusive
628 * locks the burst code gets might cause excessive SMP collisions.
630 * A quick burst can be utilized when there is no backing object
631 * (i.e. a shared file mmap).
633 if ((fault_flags
& VM_FAULT_BURST
) &&
634 (fs
.fault_flags
& VM_FAULT_WIRE_MASK
) == 0 &&
636 if (fs
.first_shared
== 0 && fs
.shared
== 0) {
637 vm_prefault(fs
.map
->pmap
, vaddr
,
638 fs
.entry
, fs
.prot
, fault_flags
);
640 vm_prefault_quick(fs
.map
->pmap
, vaddr
,
641 fs
.entry
, fs
.prot
, fault_flags
);
646 mycpu
->gd_cnt
.v_vm_faults
++;
647 if (curthread
->td_lwp
)
648 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
651 * Unlock everything, and return
655 if (curthread
->td_lwp
) {
657 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
659 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
663 /*vm_object_deallocate(fs.first_object);*/
665 /*fs.first_object = NULL; must still drop later */
667 result
= KERN_SUCCESS
;
670 vm_object_drop(fs
.first_object
);
672 lwkt_reltoken(&map
->token
);
674 lp
->lwp_flags
&= ~LWP_PAGING
;
675 if (vm_shared_fault
&& fs
.shared
== 0)
681 * Fault in the specified virtual address in the current process map,
682 * returning a held VM page or NULL. See vm_fault_page() for more
688 vm_fault_page_quick(vm_offset_t va
, vm_prot_t fault_type
, int *errorp
)
690 struct lwp
*lp
= curthread
->td_lwp
;
693 m
= vm_fault_page(&lp
->lwp_vmspace
->vm_map
, va
,
694 fault_type
, VM_FAULT_NORMAL
, errorp
);
699 * Fault in the specified virtual address in the specified map, doing all
700 * necessary manipulation of the object store and all necessary I/O. Return
701 * a held VM page or NULL, and set *errorp. The related pmap is not
704 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
705 * and marked PG_REFERENCED as well.
707 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
708 * error will be returned.
713 vm_fault_page(vm_map_t map
, vm_offset_t vaddr
, vm_prot_t fault_type
,
714 int fault_flags
, int *errorp
)
716 vm_pindex_t first_pindex
;
717 struct faultstate fs
;
720 vm_prot_t orig_fault_type
= fault_type
;
723 fs
.fault_flags
= fault_flags
;
724 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
727 * Dive the pmap (concurrency possible). If we find the
728 * appropriate page we can terminate early and quickly.
730 fs
.m
= pmap_fault_page_quick(map
->pmap
, vaddr
, fault_type
);
737 * Otherwise take a concurrency hit and do a formal page
740 fs
.shared
= vm_shared_fault
;
741 fs
.first_shared
= vm_shared_fault
;
743 lwkt_gettoken(&map
->token
);
746 * swap_pager_unswapped() needs an exclusive object
748 if (fault_flags
& (VM_FAULT_UNSWAP
| VM_FAULT_DIRTY
)) {
754 * Find the vm_map_entry representing the backing store and resolve
755 * the top level object and page index. This may have the side
756 * effect of executing a copy-on-write on the map entry and/or
757 * creating a shadow object, but will not COW any actual VM pages.
759 * On success fs.map is left read-locked and various other fields
760 * are initialized but not otherwise referenced or locked.
762 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
763 * if the map entry is a virtual page table and also writable,
764 * so we can set the 'A'accessed bit in the virtual page table entry.
767 result
= vm_map_lookup(&fs
.map
, vaddr
, fault_type
,
768 &fs
.entry
, &fs
.first_object
,
769 &first_pindex
, &fs
.first_prot
, &fs
.wired
);
771 if (result
!= KERN_SUCCESS
) {
778 * fs.map is read-locked
780 * Misc checks. Save the map generation number to detect races.
782 fs
.map_generation
= fs
.map
->timestamp
;
783 fs
.lookup_still_valid
= TRUE
;
785 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
787 if (fs
.entry
->eflags
& MAP_ENTRY_NOFAULT
) {
788 panic("vm_fault: fault on nofault entry, addr: %lx",
793 * A user-kernel shared map has no VM object and bypasses
794 * everything. We execute the uksmap function with a temporary
795 * fictitious vm_page. The address is directly mapped with no
798 if (fs
.entry
->maptype
== VM_MAPTYPE_UKSMAP
) {
799 struct vm_page fakem
;
801 bzero(&fakem
, sizeof(fakem
));
802 fakem
.pindex
= first_pindex
;
803 fakem
.flags
= PG_BUSY
| PG_FICTITIOUS
| PG_UNMANAGED
;
804 fakem
.valid
= VM_PAGE_BITS_ALL
;
805 fakem
.pat_mode
= VM_MEMATTR_DEFAULT
;
806 if (fs
.entry
->object
.uksmap(fs
.entry
->aux
.dev
, &fakem
)) {
807 *errorp
= KERN_FAILURE
;
812 fs
.m
= PHYS_TO_VM_PAGE(fakem
.phys_addr
);
822 * A system map entry may return a NULL object. No object means
823 * no pager means an unrecoverable kernel fault.
825 if (fs
.first_object
== NULL
) {
826 panic("vm_fault: unrecoverable fault at %p in entry %p",
827 (void *)vaddr
, fs
.entry
);
831 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
834 * Unfortunately a deadlock can occur if we are forced to page-in
835 * from swap, but diving all the way into the vm_pager_get_page()
836 * function to find out is too much. Just check the object type.
838 if ((curthread
->td_flags
& TDF_NOFAULT
) &&
840 fs
.first_object
->type
== OBJT_VNODE
||
841 fs
.first_object
->type
== OBJT_SWAP
||
842 fs
.first_object
->backing_object
)) {
843 *errorp
= KERN_FAILURE
;
849 * If the entry is wired we cannot change the page protection.
852 fault_type
= fs
.first_prot
;
855 * Make a reference to this object to prevent its disposal while we
856 * are messing with it. Once we have the reference, the map is free
857 * to be diddled. Since objects reference their shadows (and copies),
858 * they will stay around as well.
860 * The reference should also prevent an unexpected collapse of the
861 * parent that might move pages from the current object into the
862 * parent unexpectedly, resulting in corruption.
864 * Bump the paging-in-progress count to prevent size changes (e.g.
865 * truncation operations) during I/O. This must be done after
866 * obtaining the vnode lock in order to avoid possible deadlocks.
869 vm_object_hold_shared(fs
.first_object
);
871 vm_object_hold(fs
.first_object
);
873 fs
.vp
= vnode_pager_lock(fs
.first_object
); /* shared */
876 * The page we want is at (first_object, first_pindex), but if the
877 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
878 * page table to figure out the actual pindex.
880 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
883 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
884 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
885 fs
.entry
->aux
.master_pde
,
887 if (result
== KERN_TRY_AGAIN
) {
888 vm_object_drop(fs
.first_object
);
892 if (result
!= KERN_SUCCESS
) {
900 * Now we have the actual (object, pindex), fault in the page. If
901 * vm_fault_object() fails it will unlock and deallocate the FS
902 * data. If it succeeds everything remains locked and fs->object
903 * will have an additinal PIP count if it is not equal to
907 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 1);
909 if (result
== KERN_TRY_AGAIN
) {
910 vm_object_drop(fs
.first_object
);
914 if (result
!= KERN_SUCCESS
) {
920 if ((orig_fault_type
& VM_PROT_WRITE
) &&
921 (fs
.prot
& VM_PROT_WRITE
) == 0) {
922 *errorp
= KERN_PROTECTION_FAILURE
;
923 unlock_and_deallocate(&fs
);
929 * DO NOT UPDATE THE PMAP!!! This function may be called for
930 * a pmap unrelated to the current process pmap, in which case
931 * the current cpu core will not be listed in the pmap's pm_active
932 * mask. Thus invalidation interlocks will fail to work properly.
934 * (for example, 'ps' uses procfs to read program arguments from
935 * each process's stack).
937 * In addition to the above this function will be called to acquire
938 * a page that might already be faulted in, re-faulting it
939 * continuously is a waste of time.
941 * XXX could this have been the cause of our random seg-fault
942 * issues? procfs accesses user stacks.
944 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
946 pmap_enter(fs
.map
->pmap
, vaddr
, fs
.m
, fs
.prot
, fs
.wired
, NULL
);
947 mycpu
->gd_cnt
.v_vm_faults
++;
948 if (curthread
->td_lwp
)
949 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
953 * On success vm_fault_object() does not unlock or deallocate, and fs.m
954 * will contain a busied page. So we must unlock here after having
955 * messed with the pmap.
960 * Return a held page. We are not doing any pmap manipulation so do
961 * not set PG_MAPPED. However, adjust the page flags according to
962 * the fault type because the caller may not use a managed pmapping
963 * (so we don't want to lose the fact that the page will be dirtied
964 * if a write fault was specified).
967 vm_page_activate(fs
.m
);
968 if (fault_type
& VM_PROT_WRITE
)
971 if (curthread
->td_lwp
) {
973 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
975 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
980 * Unlock everything, and return the held page.
982 vm_page_wakeup(fs
.m
);
983 /*vm_object_deallocate(fs.first_object);*/
984 /*fs.first_object = NULL; */
989 vm_object_drop(fs
.first_object
);
991 lwkt_reltoken(&map
->token
);
996 * Fault in the specified (object,offset), dirty the returned page as
997 * needed. If the requested fault_type cannot be done NULL and an
1000 * A held (but not busied) page is returned.
1002 * The passed in object must be held as specified by the shared
1006 vm_fault_object_page(vm_object_t object
, vm_ooffset_t offset
,
1007 vm_prot_t fault_type
, int fault_flags
,
1008 int *sharedp
, int *errorp
)
1011 vm_pindex_t first_pindex
;
1012 struct faultstate fs
;
1013 struct vm_map_entry entry
;
1015 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
1016 bzero(&entry
, sizeof(entry
));
1017 entry
.object
.vm_object
= object
;
1018 entry
.maptype
= VM_MAPTYPE_NORMAL
;
1019 entry
.protection
= entry
.max_protection
= fault_type
;
1022 fs
.fault_flags
= fault_flags
;
1024 fs
.shared
= vm_shared_fault
;
1025 fs
.first_shared
= *sharedp
;
1027 KKASSERT((fault_flags
& VM_FAULT_WIRE_MASK
) == 0);
1030 * Might require swap block adjustments
1032 if (fs
.first_shared
&& (fault_flags
& (VM_FAULT_UNSWAP
| VM_FAULT_DIRTY
))) {
1033 fs
.first_shared
= 0;
1034 vm_object_upgrade(object
);
1038 * Retry loop as needed (typically for shared->exclusive transitions)
1041 *sharedp
= fs
.first_shared
;
1042 first_pindex
= OFF_TO_IDX(offset
);
1043 fs
.first_object
= object
;
1045 fs
.first_prot
= fault_type
;
1047 /*fs.map_generation = 0; unused */
1050 * Make a reference to this object to prevent its disposal while we
1051 * are messing with it. Once we have the reference, the map is free
1052 * to be diddled. Since objects reference their shadows (and copies),
1053 * they will stay around as well.
1055 * The reference should also prevent an unexpected collapse of the
1056 * parent that might move pages from the current object into the
1057 * parent unexpectedly, resulting in corruption.
1059 * Bump the paging-in-progress count to prevent size changes (e.g.
1060 * truncation operations) during I/O. This must be done after
1061 * obtaining the vnode lock in order to avoid possible deadlocks.
1064 fs
.vp
= vnode_pager_lock(fs
.first_object
);
1066 fs
.lookup_still_valid
= TRUE
;
1068 fs
.object
= fs
.first_object
; /* so unlock_and_deallocate works */
1071 /* XXX future - ability to operate on VM object using vpagetable */
1072 if (fs
.entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1073 result
= vm_fault_vpagetable(&fs
, &first_pindex
,
1074 fs
.entry
->aux
.master_pde
,
1076 if (result
== KERN_TRY_AGAIN
) {
1077 if (fs
.first_shared
== 0 && *sharedp
)
1078 vm_object_upgrade(object
);
1081 if (result
!= KERN_SUCCESS
) {
1089 * Now we have the actual (object, pindex), fault in the page. If
1090 * vm_fault_object() fails it will unlock and deallocate the FS
1091 * data. If it succeeds everything remains locked and fs->object
1092 * will have an additinal PIP count if it is not equal to
1095 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1096 * We may have to upgrade its lock to handle the requested fault.
1098 result
= vm_fault_object(&fs
, first_pindex
, fault_type
, 0);
1100 if (result
== KERN_TRY_AGAIN
) {
1101 if (fs
.first_shared
== 0 && *sharedp
)
1102 vm_object_upgrade(object
);
1105 if (result
!= KERN_SUCCESS
) {
1110 if ((fault_type
& VM_PROT_WRITE
) && (fs
.prot
& VM_PROT_WRITE
) == 0) {
1111 *errorp
= KERN_PROTECTION_FAILURE
;
1112 unlock_and_deallocate(&fs
);
1117 * On success vm_fault_object() does not unlock or deallocate, so we
1118 * do it here. Note that the returned fs.m will be busied.
1123 * Return a held page. We are not doing any pmap manipulation so do
1124 * not set PG_MAPPED. However, adjust the page flags according to
1125 * the fault type because the caller may not use a managed pmapping
1126 * (so we don't want to lose the fact that the page will be dirtied
1127 * if a write fault was specified).
1130 vm_page_activate(fs
.m
);
1131 if ((fault_type
& VM_PROT_WRITE
) || (fault_flags
& VM_FAULT_DIRTY
))
1132 vm_page_dirty(fs
.m
);
1133 if (fault_flags
& VM_FAULT_UNSWAP
)
1134 swap_pager_unswapped(fs
.m
);
1137 * Indicate that the page was accessed.
1139 vm_page_flag_set(fs
.m
, PG_REFERENCED
);
1141 if (curthread
->td_lwp
) {
1143 curthread
->td_lwp
->lwp_ru
.ru_majflt
++;
1145 curthread
->td_lwp
->lwp_ru
.ru_minflt
++;
1150 * Unlock everything, and return the held page.
1152 vm_page_wakeup(fs
.m
);
1153 /*vm_object_deallocate(fs.first_object);*/
1154 /*fs.first_object = NULL; */
1161 * Translate the virtual page number (first_pindex) that is relative
1162 * to the address space into a logical page number that is relative to the
1163 * backing object. Use the virtual page table pointed to by (vpte).
1165 * This implements an N-level page table. Any level can terminate the
1166 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1167 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1171 vm_fault_vpagetable(struct faultstate
*fs
, vm_pindex_t
*pindex
,
1172 vpte_t vpte
, int fault_type
, int allow_nofault
)
1175 struct lwbuf lwb_cache
;
1176 int vshift
= VPTE_FRAME_END
- PAGE_SHIFT
; /* index bits remaining */
1177 int result
= KERN_SUCCESS
;
1180 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1183 * We cannot proceed if the vpte is not valid, not readable
1184 * for a read fault, or not writable for a write fault.
1186 if ((vpte
& VPTE_V
) == 0) {
1187 unlock_and_deallocate(fs
);
1188 return (KERN_FAILURE
);
1190 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_RW
) == 0) {
1191 unlock_and_deallocate(fs
);
1192 return (KERN_FAILURE
);
1194 if ((vpte
& VPTE_PS
) || vshift
== 0)
1196 KKASSERT(vshift
>= VPTE_PAGE_BITS
);
1199 * Get the page table page. Nominally we only read the page
1200 * table, but since we are actively setting VPTE_M and VPTE_A,
1201 * tell vm_fault_object() that we are writing it.
1203 * There is currently no real need to optimize this.
1205 result
= vm_fault_object(fs
, (vpte
& VPTE_FRAME
) >> PAGE_SHIFT
,
1206 VM_PROT_READ
|VM_PROT_WRITE
,
1208 if (result
!= KERN_SUCCESS
)
1212 * Process the returned fs.m and look up the page table
1213 * entry in the page table page.
1215 vshift
-= VPTE_PAGE_BITS
;
1216 lwb
= lwbuf_alloc(fs
->m
, &lwb_cache
);
1217 ptep
= ((vpte_t
*)lwbuf_kva(lwb
) +
1218 ((*pindex
>> vshift
) & VPTE_PAGE_MASK
));
1222 * Page table write-back. If the vpte is valid for the
1223 * requested operation, do a write-back to the page table.
1225 * XXX VPTE_M is not set properly for page directory pages.
1226 * It doesn't get set in the page directory if the page table
1227 * is modified during a read access.
1229 vm_page_activate(fs
->m
);
1230 if ((fault_type
& VM_PROT_WRITE
) && (vpte
& VPTE_V
) &&
1232 if ((vpte
& (VPTE_M
|VPTE_A
)) != (VPTE_M
|VPTE_A
)) {
1233 atomic_set_long(ptep
, VPTE_M
| VPTE_A
);
1234 vm_page_dirty(fs
->m
);
1237 if ((fault_type
& VM_PROT_READ
) && (vpte
& VPTE_V
)) {
1238 if ((vpte
& VPTE_A
) == 0) {
1239 atomic_set_long(ptep
, VPTE_A
);
1240 vm_page_dirty(fs
->m
);
1244 vm_page_flag_set(fs
->m
, PG_REFERENCED
);
1245 vm_page_wakeup(fs
->m
);
1247 cleanup_successful_fault(fs
);
1250 * Combine remaining address bits with the vpte.
1252 /* JG how many bits from each? */
1253 *pindex
= ((vpte
& VPTE_FRAME
) >> PAGE_SHIFT
) +
1254 (*pindex
& ((1L << vshift
) - 1));
1255 return (KERN_SUCCESS
);
1260 * This is the core of the vm_fault code.
1262 * Do all operations required to fault-in (fs.first_object, pindex). Run
1263 * through the shadow chain as necessary and do required COW or virtual
1264 * copy operations. The caller has already fully resolved the vm_map_entry
1265 * and, if appropriate, has created a copy-on-write layer. All we need to
1266 * do is iterate the object chain.
1268 * On failure (fs) is unlocked and deallocated and the caller may return or
1269 * retry depending on the failure code. On success (fs) is NOT unlocked or
1270 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1271 * will have an additional PIP count if it is not equal to fs.first_object.
1273 * If locks based on fs->first_shared or fs->shared are insufficient,
1274 * clear the appropriate field(s) and return RETRY. COWs require that
1275 * first_shared be 0, while page allocations (or frees) require that
1276 * shared be 0. Renames require that both be 0.
1278 * fs->first_object must be held on call.
1282 vm_fault_object(struct faultstate
*fs
, vm_pindex_t first_pindex
,
1283 vm_prot_t fault_type
, int allow_nofault
)
1285 vm_object_t next_object
;
1289 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs
->first_object
));
1290 fs
->prot
= fs
->first_prot
;
1291 fs
->object
= fs
->first_object
;
1292 pindex
= first_pindex
;
1294 vm_object_chain_acquire(fs
->first_object
, fs
->shared
);
1295 vm_object_pip_add(fs
->first_object
, 1);
1298 * If a read fault occurs we try to make the page writable if
1299 * possible. There are three cases where we cannot make the
1300 * page mapping writable:
1302 * (1) The mapping is read-only or the VM object is read-only,
1303 * fs->prot above will simply not have VM_PROT_WRITE set.
1305 * (2) If the mapping is a virtual page table we need to be able
1306 * to detect writes so we can set VPTE_M in the virtual page
1309 * (3) If the VM page is read-only or copy-on-write, upgrading would
1310 * just result in an unnecessary COW fault.
1312 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1313 * causes adjustments to the 'M'odify bit to also turn off write
1314 * access to force a re-fault.
1316 if (fs
->entry
->maptype
== VM_MAPTYPE_VPAGETABLE
) {
1317 if ((fault_type
& VM_PROT_WRITE
) == 0)
1318 fs
->prot
&= ~VM_PROT_WRITE
;
1321 if (curthread
->td_lwp
&& curthread
->td_lwp
->lwp_vmspace
&&
1322 pmap_emulate_ad_bits(&curthread
->td_lwp
->lwp_vmspace
->vm_pmap
)) {
1323 if ((fault_type
& VM_PROT_WRITE
) == 0)
1324 fs
->prot
&= ~VM_PROT_WRITE
;
1327 /* vm_object_hold(fs->object); implied b/c object == first_object */
1331 * The entire backing chain from first_object to object
1332 * inclusive is chainlocked.
1334 * If the object is dead, we stop here
1336 if (fs
->object
->flags
& OBJ_DEAD
) {
1337 vm_object_pip_wakeup(fs
->first_object
);
1338 vm_object_chain_release_all(fs
->first_object
,
1340 if (fs
->object
!= fs
->first_object
)
1341 vm_object_drop(fs
->object
);
1342 unlock_and_deallocate(fs
);
1343 return (KERN_PROTECTION_FAILURE
);
1347 * See if the page is resident. Wait/Retry if the page is
1348 * busy (lots of stuff may have changed so we can't continue
1351 * We can theoretically allow the soft-busy case on a read
1352 * fault if the page is marked valid, but since such
1353 * pages are typically already pmap'd, putting that
1354 * special case in might be more effort then it is
1355 * worth. We cannot under any circumstances mess
1356 * around with a vm_page_t->busy page except, perhaps,
1359 fs
->m
= vm_page_lookup_busy_try(fs
->object
, pindex
,
1362 vm_object_pip_wakeup(fs
->first_object
);
1363 vm_object_chain_release_all(fs
->first_object
,
1365 if (fs
->object
!= fs
->first_object
)
1366 vm_object_drop(fs
->object
);
1368 vm_page_sleep_busy(fs
->m
, TRUE
, "vmpfw");
1369 mycpu
->gd_cnt
.v_intrans
++;
1370 /*vm_object_deallocate(fs->first_object);*/
1371 /*fs->first_object = NULL;*/
1373 return (KERN_TRY_AGAIN
);
1377 * The page is busied for us.
1379 * If reactivating a page from PQ_CACHE we may have
1382 int queue
= fs
->m
->queue
;
1383 vm_page_unqueue_nowakeup(fs
->m
);
1385 if ((queue
- fs
->m
->pc
) == PQ_CACHE
&&
1386 vm_page_count_severe()) {
1387 vm_page_activate(fs
->m
);
1388 vm_page_wakeup(fs
->m
);
1390 vm_object_pip_wakeup(fs
->first_object
);
1391 vm_object_chain_release_all(fs
->first_object
,
1393 if (fs
->object
!= fs
->first_object
)
1394 vm_object_drop(fs
->object
);
1395 unlock_and_deallocate(fs
);
1396 if (allow_nofault
== 0 ||
1397 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1402 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1403 return (KERN_PROTECTION_FAILURE
);
1405 return (KERN_TRY_AGAIN
);
1409 * If it still isn't completely valid (readable),
1410 * or if a read-ahead-mark is set on the VM page,
1411 * jump to readrest, else we found the page and
1414 * We can release the spl once we have marked the
1417 if (fs
->m
->object
!= &kernel_object
) {
1418 if ((fs
->m
->valid
& VM_PAGE_BITS_ALL
) !=
1422 if (fs
->m
->flags
& PG_RAM
) {
1425 vm_page_flag_clear(fs
->m
, PG_RAM
);
1429 break; /* break to PAGE HAS BEEN FOUND */
1433 * Page is not resident, If this is the search termination
1434 * or the pager might contain the page, allocate a new page.
1436 if (TRYPAGER(fs
) || fs
->object
== fs
->first_object
) {
1438 * Allocating, must be exclusive.
1440 if (fs
->object
== fs
->first_object
&&
1442 fs
->first_shared
= 0;
1443 vm_object_pip_wakeup(fs
->first_object
);
1444 vm_object_chain_release_all(fs
->first_object
,
1446 if (fs
->object
!= fs
->first_object
)
1447 vm_object_drop(fs
->object
);
1448 unlock_and_deallocate(fs
);
1449 return (KERN_TRY_AGAIN
);
1451 if (fs
->object
!= fs
->first_object
&&
1453 fs
->first_shared
= 0;
1455 vm_object_pip_wakeup(fs
->first_object
);
1456 vm_object_chain_release_all(fs
->first_object
,
1458 if (fs
->object
!= fs
->first_object
)
1459 vm_object_drop(fs
->object
);
1460 unlock_and_deallocate(fs
);
1461 return (KERN_TRY_AGAIN
);
1465 * If the page is beyond the object size we fail
1467 if (pindex
>= fs
->object
->size
) {
1468 vm_object_pip_wakeup(fs
->first_object
);
1469 vm_object_chain_release_all(fs
->first_object
,
1471 if (fs
->object
!= fs
->first_object
)
1472 vm_object_drop(fs
->object
);
1473 unlock_and_deallocate(fs
);
1474 return (KERN_PROTECTION_FAILURE
);
1478 * Allocate a new page for this object/offset pair.
1480 * It is possible for the allocation to race, so
1484 if (!vm_page_count_severe()) {
1485 fs
->m
= vm_page_alloc(fs
->object
, pindex
,
1486 ((fs
->vp
|| fs
->object
->backing_object
) ?
1487 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
:
1488 VM_ALLOC_NULL_OK
| VM_ALLOC_NORMAL
|
1489 VM_ALLOC_USE_GD
| VM_ALLOC_ZERO
));
1491 if (fs
->m
== NULL
) {
1492 vm_object_pip_wakeup(fs
->first_object
);
1493 vm_object_chain_release_all(fs
->first_object
,
1495 if (fs
->object
!= fs
->first_object
)
1496 vm_object_drop(fs
->object
);
1497 unlock_and_deallocate(fs
);
1498 if (allow_nofault
== 0 ||
1499 (curthread
->td_flags
& TDF_NOFAULT
) == 0) {
1504 if (td
->td_proc
&& (td
->td_proc
->p_flags
& P_LOWMEMKILL
))
1505 return (KERN_PROTECTION_FAILURE
);
1507 return (KERN_TRY_AGAIN
);
1511 * Fall through to readrest. We have a new page which
1512 * will have to be paged (since m->valid will be 0).
1518 * We have found an invalid or partially valid page, a
1519 * page with a read-ahead mark which might be partially or
1520 * fully valid (and maybe dirty too), or we have allocated
1523 * Attempt to fault-in the page if there is a chance that the
1524 * pager has it, and potentially fault in additional pages
1527 * If TRYPAGER is true then fs.m will be non-NULL and busied
1533 u_char behavior
= vm_map_entry_behavior(fs
->entry
);
1535 if (behavior
== MAP_ENTRY_BEHAV_RANDOM
)
1541 * Doing I/O may synchronously insert additional
1542 * pages so we can't be shared at this point either.
1544 * NOTE: We can't free fs->m here in the allocated
1545 * case (fs->object != fs->first_object) as
1546 * this would require an exclusively locked
1549 if (fs
->object
== fs
->first_object
&&
1551 vm_page_deactivate(fs
->m
);
1552 vm_page_wakeup(fs
->m
);
1554 fs
->first_shared
= 0;
1555 vm_object_pip_wakeup(fs
->first_object
);
1556 vm_object_chain_release_all(fs
->first_object
,
1558 if (fs
->object
!= fs
->first_object
)
1559 vm_object_drop(fs
->object
);
1560 unlock_and_deallocate(fs
);
1561 return (KERN_TRY_AGAIN
);
1563 if (fs
->object
!= fs
->first_object
&&
1565 vm_page_deactivate(fs
->m
);
1566 vm_page_wakeup(fs
->m
);
1568 fs
->first_shared
= 0;
1570 vm_object_pip_wakeup(fs
->first_object
);
1571 vm_object_chain_release_all(fs
->first_object
,
1573 if (fs
->object
!= fs
->first_object
)
1574 vm_object_drop(fs
->object
);
1575 unlock_and_deallocate(fs
);
1576 return (KERN_TRY_AGAIN
);
1580 * Avoid deadlocking against the map when doing I/O.
1581 * fs.object and the page is PG_BUSY'd.
1583 * NOTE: Once unlocked, fs->entry can become stale
1584 * so this will NULL it out.
1586 * NOTE: fs->entry is invalid until we relock the
1587 * map and verify that the timestamp has not
1593 * Acquire the page data. We still hold a ref on
1594 * fs.object and the page has been PG_BUSY's.
1596 * The pager may replace the page (for example, in
1597 * order to enter a fictitious page into the
1598 * object). If it does so it is responsible for
1599 * cleaning up the passed page and properly setting
1600 * the new page PG_BUSY.
1602 * If we got here through a PG_RAM read-ahead
1603 * mark the page may be partially dirty and thus
1604 * not freeable. Don't bother checking to see
1605 * if the pager has the page because we can't free
1606 * it anyway. We have to depend on the get_page
1607 * operation filling in any gaps whether there is
1608 * backing store or not.
1610 rv
= vm_pager_get_page(fs
->object
, &fs
->m
, seqaccess
);
1612 if (rv
== VM_PAGER_OK
) {
1614 * Relookup in case pager changed page. Pager
1615 * is responsible for disposition of old page
1618 * XXX other code segments do relookups too.
1619 * It's a bad abstraction that needs to be
1622 fs
->m
= vm_page_lookup(fs
->object
, pindex
);
1623 if (fs
->m
== NULL
) {
1624 vm_object_pip_wakeup(fs
->first_object
);
1625 vm_object_chain_release_all(
1626 fs
->first_object
, fs
->object
);
1627 if (fs
->object
!= fs
->first_object
)
1628 vm_object_drop(fs
->object
);
1629 unlock_and_deallocate(fs
);
1630 return (KERN_TRY_AGAIN
);
1633 break; /* break to PAGE HAS BEEN FOUND */
1637 * Remove the bogus page (which does not exist at this
1638 * object/offset); before doing so, we must get back
1639 * our object lock to preserve our invariant.
1641 * Also wake up any other process that may want to bring
1644 * If this is the top-level object, we must leave the
1645 * busy page to prevent another process from rushing
1646 * past us, and inserting the page in that object at
1647 * the same time that we are.
1649 if (rv
== VM_PAGER_ERROR
) {
1651 kprintf("vm_fault: pager read error, "
1656 kprintf("vm_fault: pager read error, "
1664 * Data outside the range of the pager or an I/O error
1666 * The page may have been wired during the pagein,
1667 * e.g. by the buffer cache, and cannot simply be
1668 * freed. Call vnode_pager_freepage() to deal with it.
1670 * Also note that we cannot free the page if we are
1671 * holding the related object shared. XXX not sure
1672 * what to do in that case.
1674 if (fs
->object
!= fs
->first_object
) {
1675 vnode_pager_freepage(fs
->m
);
1678 * XXX - we cannot just fall out at this
1679 * point, m has been freed and is invalid!
1683 * XXX - the check for kernel_map is a kludge to work
1684 * around having the machine panic on a kernel space
1685 * fault w/ I/O error.
1687 if (((fs
->map
!= &kernel_map
) &&
1688 (rv
== VM_PAGER_ERROR
)) || (rv
== VM_PAGER_BAD
)) {
1690 if (fs
->first_shared
) {
1691 vm_page_deactivate(fs
->m
);
1692 vm_page_wakeup(fs
->m
);
1694 vnode_pager_freepage(fs
->m
);
1698 vm_object_pip_wakeup(fs
->first_object
);
1699 vm_object_chain_release_all(fs
->first_object
,
1701 if (fs
->object
!= fs
->first_object
)
1702 vm_object_drop(fs
->object
);
1703 unlock_and_deallocate(fs
);
1704 if (rv
== VM_PAGER_ERROR
)
1705 return (KERN_FAILURE
);
1707 return (KERN_PROTECTION_FAILURE
);
1713 * We get here if the object has a default pager (or unwiring)
1714 * or the pager doesn't have the page.
1716 * fs->first_m will be used for the COW unless we find a
1717 * deeper page to be mapped read-only, in which case the
1718 * unlock*(fs) will free first_m.
1720 if (fs
->object
== fs
->first_object
)
1721 fs
->first_m
= fs
->m
;
1724 * Move on to the next object. The chain lock should prevent
1725 * the backing_object from getting ripped out from under us.
1727 * The object lock for the next object is governed by
1730 if ((next_object
= fs
->object
->backing_object
) != NULL
) {
1732 vm_object_hold_shared(next_object
);
1734 vm_object_hold(next_object
);
1735 vm_object_chain_acquire(next_object
, fs
->shared
);
1736 KKASSERT(next_object
== fs
->object
->backing_object
);
1737 pindex
+= OFF_TO_IDX(fs
->object
->backing_object_offset
);
1740 if (next_object
== NULL
) {
1742 * If there's no object left, fill the page in the top
1743 * object with zeros.
1745 if (fs
->object
!= fs
->first_object
) {
1747 if (fs
->first_object
->backing_object
!=
1749 vm_object_hold(fs
->first_object
->backing_object
);
1752 vm_object_chain_release_all(
1753 fs
->first_object
->backing_object
,
1756 if (fs
->first_object
->backing_object
!=
1758 vm_object_drop(fs
->first_object
->backing_object
);
1761 vm_object_pip_wakeup(fs
->object
);
1762 vm_object_drop(fs
->object
);
1763 fs
->object
= fs
->first_object
;
1764 pindex
= first_pindex
;
1765 fs
->m
= fs
->first_m
;
1770 * Zero the page and mark it valid.
1772 vm_page_zero_fill(fs
->m
);
1773 mycpu
->gd_cnt
.v_zfod
++;
1774 fs
->m
->valid
= VM_PAGE_BITS_ALL
;
1775 break; /* break to PAGE HAS BEEN FOUND */
1777 if (fs
->object
!= fs
->first_object
) {
1778 vm_object_pip_wakeup(fs
->object
);
1779 vm_object_lock_swap();
1780 vm_object_drop(fs
->object
);
1782 KASSERT(fs
->object
!= next_object
,
1783 ("object loop %p", next_object
));
1784 fs
->object
= next_object
;
1785 vm_object_pip_add(fs
->object
, 1);
1789 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1792 * object still held.
1794 * local shared variable may be different from fs->shared.
1796 * If the page is being written, but isn't already owned by the
1797 * top-level object, we have to copy it into a new page owned by the
1800 KASSERT((fs
->m
->flags
& PG_BUSY
) != 0,
1801 ("vm_fault: not busy after main loop"));
1803 if (fs
->object
!= fs
->first_object
) {
1805 * We only really need to copy if we want to write it.
1807 if (fault_type
& VM_PROT_WRITE
) {
1809 * This allows pages to be virtually copied from a
1810 * backing_object into the first_object, where the
1811 * backing object has no other refs to it, and cannot
1812 * gain any more refs. Instead of a bcopy, we just
1813 * move the page from the backing object to the
1814 * first object. Note that we must mark the page
1815 * dirty in the first object so that it will go out
1816 * to swap when needed.
1820 * Must be holding exclusive locks
1822 fs
->first_shared
== 0 &&
1825 * Map, if present, has not changed
1828 fs
->map_generation
== fs
->map
->timestamp
) &&
1830 * Only one shadow object
1832 (fs
->object
->shadow_count
== 1) &&
1834 * No COW refs, except us
1836 (fs
->object
->ref_count
== 1) &&
1838 * No one else can look this object up
1840 (fs
->object
->handle
== NULL
) &&
1842 * No other ways to look the object up
1844 ((fs
->object
->type
== OBJT_DEFAULT
) ||
1845 (fs
->object
->type
== OBJT_SWAP
)) &&
1847 * We don't chase down the shadow chain
1849 (fs
->object
== fs
->first_object
->backing_object
) &&
1852 * grab the lock if we need to
1854 (fs
->lookup_still_valid
||
1856 lockmgr(&fs
->map
->lock
, LK_EXCLUSIVE
|LK_NOWAIT
) == 0)
1859 * (first_m) and (m) are both busied. We have
1860 * move (m) into (first_m)'s object/pindex
1861 * in an atomic fashion, then free (first_m).
1863 * first_object is held so second remove
1864 * followed by the rename should wind
1865 * up being atomic. vm_page_free() might
1866 * block so we don't do it until after the
1869 fs
->lookup_still_valid
= 1;
1870 vm_page_protect(fs
->first_m
, VM_PROT_NONE
);
1871 vm_page_remove(fs
->first_m
);
1872 vm_page_rename(fs
->m
, fs
->first_object
,
1874 vm_page_free(fs
->first_m
);
1875 fs
->first_m
= fs
->m
;
1877 mycpu
->gd_cnt
.v_cow_optim
++;
1880 * Oh, well, lets copy it.
1882 * Why are we unmapping the original page
1883 * here? Well, in short, not all accessors
1884 * of user memory go through the pmap. The
1885 * procfs code doesn't have access user memory
1886 * via a local pmap, so vm_fault_page*()
1887 * can't call pmap_enter(). And the umtx*()
1888 * code may modify the COW'd page via a DMAP
1889 * or kernel mapping and not via the pmap,
1890 * leaving the original page still mapped
1891 * read-only into the pmap.
1893 * So we have to remove the page from at
1894 * least the current pmap if it is in it.
1895 * Just remove it from all pmaps.
1897 KKASSERT(fs
->first_shared
== 0);
1898 vm_page_copy(fs
->m
, fs
->first_m
);
1899 vm_page_protect(fs
->m
, VM_PROT_NONE
);
1900 vm_page_event(fs
->m
, VMEVENT_COW
);
1904 * We no longer need the old page or object.
1910 * We intend to revert to first_object, undo the
1911 * chain lock through to that.
1914 if (fs
->first_object
->backing_object
!= fs
->object
)
1915 vm_object_hold(fs
->first_object
->backing_object
);
1917 vm_object_chain_release_all(
1918 fs
->first_object
->backing_object
,
1921 if (fs
->first_object
->backing_object
!= fs
->object
)
1922 vm_object_drop(fs
->first_object
->backing_object
);
1926 * fs->object != fs->first_object due to above
1929 vm_object_pip_wakeup(fs
->object
);
1930 vm_object_drop(fs
->object
);
1933 * Only use the new page below...
1935 mycpu
->gd_cnt
.v_cow_faults
++;
1936 fs
->m
= fs
->first_m
;
1937 fs
->object
= fs
->first_object
;
1938 pindex
= first_pindex
;
1941 * If it wasn't a write fault avoid having to copy
1942 * the page by mapping it read-only.
1944 fs
->prot
&= ~VM_PROT_WRITE
;
1949 * Relock the map if necessary, then check the generation count.
1950 * relock_map() will update fs->timestamp to account for the
1951 * relocking if necessary.
1953 * If the count has changed after relocking then all sorts of
1954 * crap may have happened and we have to retry.
1956 * NOTE: The relock_map() can fail due to a deadlock against
1957 * the vm_page we are holding BUSY.
1959 if (fs
->lookup_still_valid
== FALSE
&& fs
->map
) {
1960 if (relock_map(fs
) ||
1961 fs
->map
->timestamp
!= fs
->map_generation
) {
1963 vm_object_pip_wakeup(fs
->first_object
);
1964 vm_object_chain_release_all(fs
->first_object
,
1966 if (fs
->object
!= fs
->first_object
)
1967 vm_object_drop(fs
->object
);
1968 unlock_and_deallocate(fs
);
1969 return (KERN_TRY_AGAIN
);
1974 * If the fault is a write, we know that this page is being
1975 * written NOW so dirty it explicitly to save on pmap_is_modified()
1978 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1979 * if the page is already dirty to prevent data written with
1980 * the expectation of being synced from not being synced.
1981 * Likewise if this entry does not request NOSYNC then make
1982 * sure the page isn't marked NOSYNC. Applications sharing
1983 * data should use the same flags to avoid ping ponging.
1985 * Also tell the backing pager, if any, that it should remove
1986 * any swap backing since the page is now dirty.
1988 vm_page_activate(fs
->m
);
1989 if (fs
->prot
& VM_PROT_WRITE
) {
1990 vm_object_set_writeable_dirty(fs
->m
->object
);
1991 vm_set_nosync(fs
->m
, fs
->entry
);
1992 if (fs
->fault_flags
& VM_FAULT_DIRTY
) {
1993 vm_page_dirty(fs
->m
);
1994 swap_pager_unswapped(fs
->m
);
1998 vm_object_pip_wakeup(fs
->first_object
);
1999 vm_object_chain_release_all(fs
->first_object
, fs
->object
);
2000 if (fs
->object
!= fs
->first_object
)
2001 vm_object_drop(fs
->object
);
2004 * Page had better still be busy. We are still locked up and
2005 * fs->object will have another PIP reference if it is not equal
2006 * to fs->first_object.
2008 KASSERT(fs
->m
->flags
& PG_BUSY
,
2009 ("vm_fault: page %p not busy!", fs
->m
));
2012 * Sanity check: page must be completely valid or it is not fit to
2013 * map into user space. vm_pager_get_pages() ensures this.
2015 if (fs
->m
->valid
!= VM_PAGE_BITS_ALL
) {
2016 vm_page_zero_invalid(fs
->m
, TRUE
);
2017 kprintf("Warning: page %p partially invalid on fault\n", fs
->m
);
2020 return (KERN_SUCCESS
);
2024 * Hold each of the physical pages that are mapped by the specified range of
2025 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
2026 * and allow the specified types of access, "prot". If all of the implied
2027 * pages are successfully held, then the number of held pages is returned
2028 * together with pointers to those pages in the array "ma". However, if any
2029 * of the pages cannot be held, -1 is returned.
2032 vm_fault_quick_hold_pages(vm_map_t map
, vm_offset_t addr
, vm_size_t len
,
2033 vm_prot_t prot
, vm_page_t
*ma
, int max_count
)
2035 vm_offset_t start
, end
;
2036 int i
, npages
, error
;
2038 start
= trunc_page(addr
);
2039 end
= round_page(addr
+ len
);
2041 npages
= howmany(end
- start
, PAGE_SIZE
);
2043 if (npages
> max_count
)
2046 for (i
= 0; i
< npages
; i
++) {
2047 // XXX error handling
2048 ma
[i
] = vm_fault_page_quick(start
+ (i
* PAGE_SIZE
),
2057 * Wire down a range of virtual addresses in a map. The entry in question
2058 * should be marked in-transition and the map must be locked. We must
2059 * release the map temporarily while faulting-in the page to avoid a
2060 * deadlock. Note that the entry may be clipped while we are blocked but
2061 * will never be freed.
2066 vm_fault_wire(vm_map_t map
, vm_map_entry_t entry
,
2067 boolean_t user_wire
, int kmflags
)
2069 boolean_t fictitious
;
2080 lwkt_gettoken(&map
->token
);
2083 wire_prot
= VM_PROT_READ
;
2084 fault_flags
= VM_FAULT_USER_WIRE
;
2086 wire_prot
= VM_PROT_READ
| VM_PROT_WRITE
;
2087 fault_flags
= VM_FAULT_CHANGE_WIRING
;
2089 if (kmflags
& KM_NOTLBSYNC
)
2090 wire_prot
|= VM_PROT_NOSYNC
;
2092 pmap
= vm_map_pmap(map
);
2093 start
= entry
->start
;
2095 switch(entry
->maptype
) {
2096 case VM_MAPTYPE_NORMAL
:
2097 case VM_MAPTYPE_VPAGETABLE
:
2098 fictitious
= entry
->object
.vm_object
&&
2099 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2100 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2102 case VM_MAPTYPE_UKSMAP
:
2110 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2116 * We simulate a fault to get the page and enter it in the physical
2119 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2120 rv
= vm_fault(map
, va
, wire_prot
, fault_flags
);
2122 while (va
> start
) {
2124 if ((pa
= pmap_extract(pmap
, va
)) == 0)
2126 pmap_change_wiring(pmap
, va
, FALSE
, entry
);
2128 m
= PHYS_TO_VM_PAGE(pa
);
2129 vm_page_busy_wait(m
, FALSE
, "vmwrpg");
2130 vm_page_unwire(m
, 1);
2140 lwkt_reltoken(&map
->token
);
2145 * Unwire a range of virtual addresses in a map. The map should be
2149 vm_fault_unwire(vm_map_t map
, vm_map_entry_t entry
)
2151 boolean_t fictitious
;
2159 lwkt_gettoken(&map
->token
);
2161 pmap
= vm_map_pmap(map
);
2162 start
= entry
->start
;
2164 fictitious
= entry
->object
.vm_object
&&
2165 ((entry
->object
.vm_object
->type
== OBJT_DEVICE
) ||
2166 (entry
->object
.vm_object
->type
== OBJT_MGTDEVICE
));
2167 if (entry
->eflags
& MAP_ENTRY_KSTACK
)
2171 * Since the pages are wired down, we must be able to get their
2172 * mappings from the physical map system.
2174 for (va
= start
; va
< end
; va
+= PAGE_SIZE
) {
2175 pa
= pmap_extract(pmap
, va
);
2177 pmap_change_wiring(pmap
, va
, FALSE
, entry
);
2179 m
= PHYS_TO_VM_PAGE(pa
);
2180 vm_page_busy_wait(m
, FALSE
, "vmwupg");
2181 vm_page_unwire(m
, 1);
2186 lwkt_reltoken(&map
->token
);
2190 * Copy all of the pages from a wired-down map entry to another.
2192 * The source and destination maps must be locked for write.
2193 * The source and destination maps token must be held
2194 * The source map entry must be wired down (or be a sharing map
2195 * entry corresponding to a main map entry that is wired down).
2197 * No other requirements.
2199 * XXX do segment optimization
2202 vm_fault_copy_entry(vm_map_t dst_map
, vm_map_t src_map
,
2203 vm_map_entry_t dst_entry
, vm_map_entry_t src_entry
)
2205 vm_object_t dst_object
;
2206 vm_object_t src_object
;
2207 vm_ooffset_t dst_offset
;
2208 vm_ooffset_t src_offset
;
2214 src_object
= src_entry
->object
.vm_object
;
2215 src_offset
= src_entry
->offset
;
2218 * Create the top-level object for the destination entry. (Doesn't
2219 * actually shadow anything - we copy the pages directly.)
2221 vm_map_entry_allocate_object(dst_entry
);
2222 dst_object
= dst_entry
->object
.vm_object
;
2224 prot
= dst_entry
->max_protection
;
2227 * Loop through all of the pages in the entry's range, copying each
2228 * one from the source object (it should be there) to the destination
2231 vm_object_hold(src_object
);
2232 vm_object_hold(dst_object
);
2233 for (vaddr
= dst_entry
->start
, dst_offset
= 0;
2234 vaddr
< dst_entry
->end
;
2235 vaddr
+= PAGE_SIZE
, dst_offset
+= PAGE_SIZE
) {
2238 * Allocate a page in the destination object
2241 dst_m
= vm_page_alloc(dst_object
,
2242 OFF_TO_IDX(dst_offset
),
2244 if (dst_m
== NULL
) {
2247 } while (dst_m
== NULL
);
2250 * Find the page in the source object, and copy it in.
2251 * (Because the source is wired down, the page will be in
2254 src_m
= vm_page_lookup(src_object
,
2255 OFF_TO_IDX(dst_offset
+ src_offset
));
2257 panic("vm_fault_copy_wired: page missing");
2259 vm_page_copy(src_m
, dst_m
);
2260 vm_page_event(src_m
, VMEVENT_COW
);
2263 * Enter it in the pmap...
2265 pmap_enter(dst_map
->pmap
, vaddr
, dst_m
, prot
, FALSE
, dst_entry
);
2268 * Mark it no longer busy, and put it on the active list.
2270 vm_page_activate(dst_m
);
2271 vm_page_wakeup(dst_m
);
2273 vm_object_drop(dst_object
);
2274 vm_object_drop(src_object
);
2280 * This routine checks around the requested page for other pages that
2281 * might be able to be faulted in. This routine brackets the viable
2282 * pages for the pages to be paged in.
2285 * m, rbehind, rahead
2288 * marray (array of vm_page_t), reqpage (index of requested page)
2291 * number of pages in marray
2294 vm_fault_additional_pages(vm_page_t m
, int rbehind
, int rahead
,
2295 vm_page_t
*marray
, int *reqpage
)
2299 vm_pindex_t pindex
, startpindex
, endpindex
, tpindex
;
2301 int cbehind
, cahead
;
2307 * we don't fault-ahead for device pager
2309 if ((object
->type
== OBJT_DEVICE
) ||
2310 (object
->type
== OBJT_MGTDEVICE
)) {
2317 * if the requested page is not available, then give up now
2319 if (!vm_pager_has_page(object
, pindex
, &cbehind
, &cahead
)) {
2320 *reqpage
= 0; /* not used by caller, fix compiler warn */
2324 if ((cbehind
== 0) && (cahead
== 0)) {
2330 if (rahead
> cahead
) {
2334 if (rbehind
> cbehind
) {
2339 * Do not do any readahead if we have insufficient free memory.
2341 * XXX code was broken disabled before and has instability
2342 * with this conditonal fixed, so shortcut for now.
2344 if (burst_fault
== 0 || vm_page_count_severe()) {
2351 * scan backward for the read behind pages -- in memory
2353 * Assume that if the page is not found an interrupt will not
2354 * create it. Theoretically interrupts can only remove (busy)
2355 * pages, not create new associations.
2358 if (rbehind
> pindex
) {
2362 startpindex
= pindex
- rbehind
;
2365 vm_object_hold(object
);
2366 for (tpindex
= pindex
; tpindex
> startpindex
; --tpindex
) {
2367 if (vm_page_lookup(object
, tpindex
- 1))
2372 while (tpindex
< pindex
) {
2373 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2376 for (j
= 0; j
< i
; j
++) {
2377 vm_page_free(marray
[j
]);
2379 vm_object_drop(object
);
2388 vm_object_drop(object
);
2394 * Assign requested page
2401 * Scan forwards for read-ahead pages
2403 tpindex
= pindex
+ 1;
2404 endpindex
= tpindex
+ rahead
;
2405 if (endpindex
> object
->size
)
2406 endpindex
= object
->size
;
2408 vm_object_hold(object
);
2409 while (tpindex
< endpindex
) {
2410 if (vm_page_lookup(object
, tpindex
))
2412 rtm
= vm_page_alloc(object
, tpindex
, VM_ALLOC_SYSTEM
|
2420 vm_object_drop(object
);
2428 * vm_prefault() provides a quick way of clustering pagefaults into a
2429 * processes address space. It is a "cousin" of pmap_object_init_pt,
2430 * except it runs at page fault time instead of mmap time.
2432 * vm.fast_fault Enables pre-faulting zero-fill pages
2434 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2435 * prefault. Scan stops in either direction when
2436 * a page is found to already exist.
2438 * This code used to be per-platform pmap_prefault(). It is now
2439 * machine-independent and enhanced to also pre-fault zero-fill pages
2440 * (see vm.fast_fault) as well as make them writable, which greatly
2441 * reduces the number of page faults programs incur.
2443 * Application performance when pre-faulting zero-fill pages is heavily
2444 * dependent on the application. Very tiny applications like /bin/echo
2445 * lose a little performance while applications of any appreciable size
2446 * gain performance. Prefaulting multiple pages also reduces SMP
2447 * congestion and can improve SMP performance significantly.
2449 * NOTE! prot may allow writing but this only applies to the top level
2450 * object. If we wind up mapping a page extracted from a backing
2451 * object we have to make sure it is read-only.
2453 * NOTE! The caller has already handled any COW operations on the
2454 * vm_map_entry via the normal fault code. Do NOT call this
2455 * shortcut unless the normal fault code has run on this entry.
2457 * The related map must be locked.
2458 * No other requirements.
2460 static int vm_prefault_pages
= 8;
2461 SYSCTL_INT(_vm
, OID_AUTO
, prefault_pages
, CTLFLAG_RW
, &vm_prefault_pages
, 0,
2462 "Maximum number of pages to pre-fault");
2463 static int vm_fast_fault
= 1;
2464 SYSCTL_INT(_vm
, OID_AUTO
, fast_fault
, CTLFLAG_RW
, &vm_fast_fault
, 0,
2465 "Burst fault zero-fill regions");
2468 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2469 * is not already dirty by other means. This will prevent passive
2470 * filesystem syncing as well as 'sync' from writing out the page.
2473 vm_set_nosync(vm_page_t m
, vm_map_entry_t entry
)
2475 if (entry
->eflags
& MAP_ENTRY_NOSYNC
) {
2477 vm_page_flag_set(m
, PG_NOSYNC
);
2479 vm_page_flag_clear(m
, PG_NOSYNC
);
2484 vm_prefault(pmap_t pmap
, vm_offset_t addra
, vm_map_entry_t entry
, int prot
,
2500 * Get stable max count value, disabled if set to 0
2502 maxpages
= vm_prefault_pages
;
2508 * We do not currently prefault mappings that use virtual page
2509 * tables. We do not prefault foreign pmaps.
2511 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2513 lp
= curthread
->td_lwp
;
2514 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2518 * Limit pre-fault count to 1024 pages.
2520 if (maxpages
> 1024)
2523 object
= entry
->object
.vm_object
;
2524 KKASSERT(object
!= NULL
);
2525 KKASSERT(object
== entry
->object
.vm_object
);
2526 vm_object_hold(object
);
2527 vm_object_chain_acquire(object
, 0);
2531 for (i
= 0; i
< maxpages
; ++i
) {
2532 vm_object_t lobject
;
2533 vm_object_t nobject
;
2538 * This can eat a lot of time on a heavily contended
2539 * machine so yield on the tick if needed.
2545 * Calculate the page to pre-fault, stopping the scan in
2546 * each direction separately if the limit is reached.
2551 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2555 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2557 if (addr
< entry
->start
) {
2563 if (addr
>= entry
->end
) {
2571 * Skip pages already mapped, and stop scanning in that
2572 * direction. When the scan terminates in both directions
2575 if (pmap_prefault_ok(pmap
, addr
) == 0) {
2586 * Follow the VM object chain to obtain the page to be mapped
2589 * If we reach the terminal object without finding a page
2590 * and we determine it would be advantageous, then allocate
2591 * a zero-fill page for the base object. The base object
2592 * is guaranteed to be OBJT_DEFAULT for this case.
2594 * In order to not have to check the pager via *haspage*()
2595 * we stop if any non-default object is encountered. e.g.
2596 * a vnode or swap object would stop the loop.
2598 index
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
2603 KKASSERT(lobject
== entry
->object
.vm_object
);
2604 /*vm_object_hold(lobject); implied */
2606 while ((m
= vm_page_lookup_busy_try(lobject
, pindex
,
2607 TRUE
, &error
)) == NULL
) {
2608 if (lobject
->type
!= OBJT_DEFAULT
)
2610 if (lobject
->backing_object
== NULL
) {
2611 if (vm_fast_fault
== 0)
2613 if ((prot
& VM_PROT_WRITE
) == 0 ||
2614 vm_page_count_min(0)) {
2619 * NOTE: Allocated from base object
2621 m
= vm_page_alloc(object
, index
,
2630 /* lobject = object .. not needed */
2633 if (lobject
->backing_object_offset
& PAGE_MASK
)
2635 nobject
= lobject
->backing_object
;
2636 vm_object_hold(nobject
);
2637 KKASSERT(nobject
== lobject
->backing_object
);
2638 pindex
+= lobject
->backing_object_offset
>> PAGE_SHIFT
;
2639 if (lobject
!= object
) {
2640 vm_object_lock_swap();
2641 vm_object_drop(lobject
);
2644 pprot
&= ~VM_PROT_WRITE
;
2645 vm_object_chain_acquire(lobject
, 0);
2649 * NOTE: A non-NULL (m) will be associated with lobject if
2650 * it was found there, otherwise it is probably a
2651 * zero-fill page associated with the base object.
2653 * Give-up if no page is available.
2656 if (lobject
!= object
) {
2658 if (object
->backing_object
!= lobject
)
2659 vm_object_hold(object
->backing_object
);
2661 vm_object_chain_release_all(
2662 object
->backing_object
, lobject
);
2664 if (object
->backing_object
!= lobject
)
2665 vm_object_drop(object
->backing_object
);
2667 vm_object_drop(lobject
);
2673 * The object must be marked dirty if we are mapping a
2674 * writable page. m->object is either lobject or object,
2675 * both of which are still held. Do this before we
2676 * potentially drop the object.
2678 if (pprot
& VM_PROT_WRITE
)
2679 vm_object_set_writeable_dirty(m
->object
);
2682 * Do not conditionalize on PG_RAM. If pages are present in
2683 * the VM system we assume optimal caching. If caching is
2684 * not optimal the I/O gravy train will be restarted when we
2685 * hit an unavailable page. We do not want to try to restart
2686 * the gravy train now because we really don't know how much
2687 * of the object has been cached. The cost for restarting
2688 * the gravy train should be low (since accesses will likely
2689 * be I/O bound anyway).
2691 if (lobject
!= object
) {
2693 if (object
->backing_object
!= lobject
)
2694 vm_object_hold(object
->backing_object
);
2696 vm_object_chain_release_all(object
->backing_object
,
2699 if (object
->backing_object
!= lobject
)
2700 vm_object_drop(object
->backing_object
);
2702 vm_object_drop(lobject
);
2706 * Enter the page into the pmap if appropriate. If we had
2707 * allocated the page we have to place it on a queue. If not
2708 * we just have to make sure it isn't on the cache queue
2709 * (pages on the cache queue are not allowed to be mapped).
2713 * Page must be zerod.
2715 vm_page_zero_fill(m
);
2716 mycpu
->gd_cnt
.v_zfod
++;
2717 m
->valid
= VM_PAGE_BITS_ALL
;
2720 * Handle dirty page case
2722 if (pprot
& VM_PROT_WRITE
)
2723 vm_set_nosync(m
, entry
);
2724 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2725 mycpu
->gd_cnt
.v_vm_faults
++;
2726 if (curthread
->td_lwp
)
2727 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2728 vm_page_deactivate(m
);
2729 if (pprot
& VM_PROT_WRITE
) {
2730 /*vm_object_set_writeable_dirty(m->object);*/
2731 vm_set_nosync(m
, entry
);
2732 if (fault_flags
& VM_FAULT_DIRTY
) {
2735 swap_pager_unswapped(m
);
2740 /* couldn't busy page, no wakeup */
2742 ((m
->valid
& VM_PAGE_BITS_ALL
) == VM_PAGE_BITS_ALL
) &&
2743 (m
->flags
& PG_FICTITIOUS
) == 0) {
2745 * A fully valid page not undergoing soft I/O can
2746 * be immediately entered into the pmap.
2748 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
2749 vm_page_deactivate(m
);
2750 if (pprot
& VM_PROT_WRITE
) {
2751 /*vm_object_set_writeable_dirty(m->object);*/
2752 vm_set_nosync(m
, entry
);
2753 if (fault_flags
& VM_FAULT_DIRTY
) {
2756 swap_pager_unswapped(m
);
2759 if (pprot
& VM_PROT_WRITE
)
2760 vm_set_nosync(m
, entry
);
2761 pmap_enter(pmap
, addr
, m
, pprot
, 0, entry
);
2762 mycpu
->gd_cnt
.v_vm_faults
++;
2763 if (curthread
->td_lwp
)
2764 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;
2770 vm_object_chain_release(object
);
2771 vm_object_drop(object
);
2775 * Object can be held shared
2778 vm_prefault_quick(pmap_t pmap
, vm_offset_t addra
,
2779 vm_map_entry_t entry
, int prot
, int fault_flags
)
2792 * Get stable max count value, disabled if set to 0
2794 maxpages
= vm_prefault_pages
;
2800 * We do not currently prefault mappings that use virtual page
2801 * tables. We do not prefault foreign pmaps.
2803 if (entry
->maptype
!= VM_MAPTYPE_NORMAL
)
2805 lp
= curthread
->td_lwp
;
2806 if (lp
== NULL
|| (pmap
!= vmspace_pmap(lp
->lwp_vmspace
)))
2808 object
= entry
->object
.vm_object
;
2809 if (object
->backing_object
!= NULL
)
2811 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object
));
2814 * Limit pre-fault count to 1024 pages.
2816 if (maxpages
> 1024)
2821 for (i
= 0; i
< maxpages
; ++i
) {
2825 * Calculate the page to pre-fault, stopping the scan in
2826 * each direction separately if the limit is reached.
2831 addr
= addra
- ((i
+ 1) >> 1) * PAGE_SIZE
;
2835 addr
= addra
+ ((i
+ 2) >> 1) * PAGE_SIZE
;
2837 if (addr
< entry
->start
) {
2843 if (addr
>= entry
->end
) {
2851 * Follow the VM object chain to obtain the page to be mapped
2852 * into the pmap. This version of the prefault code only
2853 * works with terminal objects.
2855 * The page must already exist. If we encounter a problem
2858 * WARNING! We cannot call swap_pager_unswapped() or insert
2859 * a new vm_page with a shared token.
2861 pindex
= ((addr
- entry
->start
) + entry
->offset
) >> PAGE_SHIFT
;
2863 m
= vm_page_lookup_busy_try(object
, pindex
, TRUE
, &error
);
2864 if (m
== NULL
|| error
)
2868 * Skip pages already mapped, and stop scanning in that
2869 * direction. When the scan terminates in both directions
2872 if (pmap_prefault_ok(pmap
, addr
) == 0) {
2884 * Stop if the page cannot be trivially entered into the
2887 if (((m
->valid
& VM_PAGE_BITS_ALL
) != VM_PAGE_BITS_ALL
) ||
2888 (m
->flags
& PG_FICTITIOUS
) ||
2889 ((m
->flags
& PG_SWAPPED
) &&
2890 (prot
& VM_PROT_WRITE
) &&
2891 (fault_flags
& VM_FAULT_DIRTY
))) {
2897 * Enter the page into the pmap. The object might be held
2898 * shared so we can't do any (serious) modifying operation
2901 if ((m
->queue
- m
->pc
) == PQ_CACHE
)
2902 vm_page_deactivate(m
);
2903 if (prot
& VM_PROT_WRITE
) {
2904 vm_object_set_writeable_dirty(m
->object
);
2905 vm_set_nosync(m
, entry
);
2906 if (fault_flags
& VM_FAULT_DIRTY
) {
2908 /* can't happeen due to conditional above */
2909 /* swap_pager_unswapped(m); */
2912 pmap_enter(pmap
, addr
, m
, prot
, 0, entry
);
2913 mycpu
->gd_cnt
.v_vm_faults
++;
2914 if (curthread
->td_lwp
)
2915 ++curthread
->td_lwp
->lwp_ru
.ru_minflt
;