Sync ACPICA with Intel's version 20161222.
[dragonfly.git] / sys / vm / vm_fault.c
blob2bb985fddfb5109e3b8a2eb0a469040e7859e26f
1 /*
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
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * ---
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
49 * are met:
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
69 * SUCH DAMAGE.
71 * ---
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>
116 #include <vm/vm.h>
117 #include <vm/vm_param.h>
118 #include <vm/pmap.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>
131 struct faultstate {
132 vm_page_t m;
133 vm_object_t object;
134 vm_pindex_t pindex;
135 vm_prot_t prot;
136 vm_page_t first_m;
137 vm_object_t first_object;
138 vm_prot_t first_prot;
139 vm_map_t map;
140 vm_map_entry_t entry;
141 int lookup_still_valid;
142 int hardfault;
143 int fault_flags;
144 int map_generation;
145 int shared;
146 int first_shared;
147 boolean_t wired;
148 struct vnode *vp;
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 *,
171 vpte_t, int, int);
172 #if 0
173 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
174 #endif
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);
181 static __inline void
182 release_page(struct faultstate *fs)
184 vm_page_deactivate(fs->m);
185 vm_page_wakeup(fs->m);
186 fs->m = NULL;
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.
199 static __inline int
200 relock_map(struct faultstate *fs)
202 int error;
204 if (fs->lookup_still_valid == FALSE && fs->map) {
205 error = vm_map_lock_read_to(fs->map);
206 if (error == 0)
207 fs->lookup_still_valid = TRUE;
208 } else {
209 error = 0;
211 return error;
214 static __inline void
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.
227 static void
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);
238 fs->first_m = NULL;
242 * Reset fs->object.
244 fs->object = fs->first_object;
245 if (relock && fs->lookup_still_valid == FALSE) {
246 if (fs->map)
247 vm_map_lock_read(fs->map);
248 fs->lookup_still_valid = TRUE;
252 static void
253 _unlock_things(struct faultstate *fs, int dealloc)
255 _cleanup_successful_fault(fs, 0);
256 if (dealloc) {
257 /*vm_object_deallocate(fs->first_object);*/
258 /*fs->first_object = NULL; drop used later on */
260 unlock_map(fs);
261 if (fs->vp != NULL) {
262 vput(fs->vp);
263 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)
272 * TRYPAGER
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))
285 * vm_fault:
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)
303 int result;
304 vm_pindex_t first_pindex;
305 struct faultstate fs;
306 struct lwp *lp;
307 struct proc *p;
308 thread_t td;
309 int growstack;
310 int retry = 0;
311 int inherit_prot;
313 inherit_prot = fault_type & VM_PROT_NOSYNC;
314 fs.hardfault = 0;
315 fs.fault_flags = fault_flags;
316 fs.vp = NULL;
317 fs.shared = vm_shared_fault;
318 fs.first_shared = vm_shared_fault;
319 growstack = 1;
320 if (vm_shared_fault)
321 ++vm_shared_count;
324 * vm_map interactions
326 td = curthread;
327 if ((lp = td->td_lwp) != NULL)
328 lp->lwp_flags |= LWP_PAGING;
329 lwkt_gettoken(&map->token);
331 RetryFault:
333 * Find the vm_map_entry representing the backing store and resolve
334 * the top level object and page index. This may have the side
335 * effect of executing a copy-on-write on the map entry and/or
336 * creating a shadow object, but will not COW any actual VM pages.
338 * On success fs.map is left read-locked and various other fields
339 * are initialized but not otherwise referenced or locked.
341 * NOTE! vm_map_lookup will try to upgrade the fault_type to
342 * VM_FAULT_WRITE if the map entry is a virtual page table and also
343 * writable, so we can set the 'A'accessed bit in the virtual page
344 * table entry.
346 fs.map = map;
347 result = vm_map_lookup(&fs.map, vaddr, fault_type,
348 &fs.entry, &fs.first_object,
349 &first_pindex, &fs.first_prot, &fs.wired);
352 * If the lookup failed or the map protections are incompatible,
353 * the fault generally fails.
355 * The failure could be due to TDF_NOFAULT if vm_map_lookup()
356 * tried to do a COW fault.
358 * If the caller is trying to do a user wiring we have more work
359 * to do.
361 if (result != KERN_SUCCESS) {
362 if (result == KERN_FAILURE_NOFAULT) {
363 result = KERN_FAILURE;
364 goto done;
366 if (result != KERN_PROTECTION_FAILURE ||
367 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
369 if (result == KERN_INVALID_ADDRESS && growstack &&
370 map != &kernel_map && curproc != NULL) {
371 result = vm_map_growstack(curproc, vaddr);
372 if (result == KERN_SUCCESS) {
373 growstack = 0;
374 ++retry;
375 goto RetryFault;
377 result = KERN_FAILURE;
379 goto done;
383 * If we are user-wiring a r/w segment, and it is COW, then
384 * we need to do the COW operation. Note that we don't
385 * currently COW RO sections now, because it is NOT desirable
386 * to COW .text. We simply keep .text from ever being COW'ed
387 * and take the heat that one cannot debug wired .text sections.
389 result = vm_map_lookup(&fs.map, vaddr,
390 VM_PROT_READ|VM_PROT_WRITE|
391 VM_PROT_OVERRIDE_WRITE,
392 &fs.entry, &fs.first_object,
393 &first_pindex, &fs.first_prot,
394 &fs.wired);
395 if (result != KERN_SUCCESS) {
396 /* could also be KERN_FAILURE_NOFAULT */
397 result = KERN_FAILURE;
398 goto done;
402 * If we don't COW now, on a user wire, the user will never
403 * be able to write to the mapping. If we don't make this
404 * restriction, the bookkeeping would be nearly impossible.
406 * XXX We have a shared lock, this will have a MP race but
407 * I don't see how it can hurt anything.
409 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
410 fs.entry->max_protection &= ~VM_PROT_WRITE;
414 * fs.map is read-locked
416 * Misc checks. Save the map generation number to detect races.
418 fs.map_generation = fs.map->timestamp;
419 fs.lookup_still_valid = TRUE;
420 fs.first_m = NULL;
421 fs.object = fs.first_object; /* so unlock_and_deallocate works */
422 fs.prot = fs.first_prot; /* default (used by uksmap) */
424 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
425 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
426 panic("vm_fault: fault on nofault entry, addr: %p",
427 (void *)vaddr);
429 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
430 vaddr >= fs.entry->start &&
431 vaddr < fs.entry->start + PAGE_SIZE) {
432 panic("vm_fault: fault on stack guard, addr: %p",
433 (void *)vaddr);
438 * A user-kernel shared map has no VM object and bypasses
439 * everything. We execute the uksmap function with a temporary
440 * fictitious vm_page. The address is directly mapped with no
441 * management.
443 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
444 struct vm_page fakem;
446 bzero(&fakem, sizeof(fakem));
447 fakem.pindex = first_pindex;
448 fakem.flags = PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED;
449 fakem.valid = VM_PAGE_BITS_ALL;
450 fakem.pat_mode = VM_MEMATTR_DEFAULT;
451 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
452 result = KERN_FAILURE;
453 unlock_things(&fs);
454 goto done2;
456 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
457 fs.wired, fs.entry);
458 goto done_success;
462 * A system map entry may return a NULL object. No object means
463 * no pager means an unrecoverable kernel fault.
465 if (fs.first_object == NULL) {
466 panic("vm_fault: unrecoverable fault at %p in entry %p",
467 (void *)vaddr, fs.entry);
471 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
472 * is set.
474 * Unfortunately a deadlock can occur if we are forced to page-in
475 * from swap, but diving all the way into the vm_pager_get_page()
476 * function to find out is too much. Just check the object type.
478 * The deadlock is a CAM deadlock on a busy VM page when trying
479 * to finish an I/O if another process gets stuck in
480 * vop_helper_read_shortcut() due to a swap fault.
482 if ((td->td_flags & TDF_NOFAULT) &&
483 (retry ||
484 fs.first_object->type == OBJT_VNODE ||
485 fs.first_object->type == OBJT_SWAP ||
486 fs.first_object->backing_object)) {
487 result = KERN_FAILURE;
488 unlock_things(&fs);
489 goto done2;
493 * If the entry is wired we cannot change the page protection.
495 if (fs.wired)
496 fault_type = fs.first_prot;
499 * We generally want to avoid unnecessary exclusive modes on backing
500 * and terminal objects because this can seriously interfere with
501 * heavily fork()'d processes (particularly /bin/sh scripts).
503 * However, we also want to avoid unnecessary retries due to needed
504 * shared->exclusive promotion for common faults. Exclusive mode is
505 * always needed if any page insertion, rename, or free occurs in an
506 * object (and also indirectly if any I/O is done).
508 * The main issue here is going to be fs.first_shared. If the
509 * first_object has a backing object which isn't shadowed and the
510 * process is single-threaded we might as well use an exclusive
511 * lock/chain right off the bat.
513 if (fs.first_shared && fs.first_object->backing_object &&
514 LIST_EMPTY(&fs.first_object->shadow_head) &&
515 td->td_proc && td->td_proc->p_nthreads == 1) {
516 fs.first_shared = 0;
520 * swap_pager_unswapped() needs an exclusive object
522 if (fault_flags & (VM_FAULT_UNSWAP | VM_FAULT_DIRTY)) {
523 fs.first_shared = 0;
527 * Obtain a top-level object lock, shared or exclusive depending
528 * on fs.first_shared. If a shared lock winds up being insufficient
529 * we will retry with an exclusive lock.
531 * The vnode pager lock is always shared.
533 if (fs.first_shared)
534 vm_object_hold_shared(fs.first_object);
535 else
536 vm_object_hold(fs.first_object);
537 if (fs.vp == NULL)
538 fs.vp = vnode_pager_lock(fs.first_object);
541 * The page we want is at (first_object, first_pindex), but if the
542 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
543 * page table to figure out the actual pindex.
545 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
546 * ONLY
548 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
549 result = vm_fault_vpagetable(&fs, &first_pindex,
550 fs.entry->aux.master_pde,
551 fault_type, 1);
552 if (result == KERN_TRY_AGAIN) {
553 vm_object_drop(fs.first_object);
554 ++retry;
555 goto RetryFault;
557 if (result != KERN_SUCCESS)
558 goto done;
562 * Now we have the actual (object, pindex), fault in the page. If
563 * vm_fault_object() fails it will unlock and deallocate the FS
564 * data. If it succeeds everything remains locked and fs->object
565 * will have an additional PIP count if it is not equal to
566 * fs->first_object
568 * vm_fault_object will set fs->prot for the pmap operation. It is
569 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
570 * page can be safely written. However, it will force a read-only
571 * mapping for a read fault if the memory is managed by a virtual
572 * page table.
574 * If the fault code uses the shared object lock shortcut
575 * we must not try to burst (we can't allocate VM pages).
577 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
579 if (debug_fault > 0) {
580 --debug_fault;
581 kprintf("VM_FAULT result %d addr=%jx type=%02x flags=%02x "
582 "fs.m=%p fs.prot=%02x fs.wired=%02x fs.entry=%p\n",
583 result, (intmax_t)vaddr, fault_type, fault_flags,
584 fs.m, fs.prot, fs.wired, fs.entry);
587 if (result == KERN_TRY_AGAIN) {
588 vm_object_drop(fs.first_object);
589 ++retry;
590 goto RetryFault;
592 if (result != KERN_SUCCESS)
593 goto done;
596 * On success vm_fault_object() does not unlock or deallocate, and fs.m
597 * will contain a busied page.
599 * Enter the page into the pmap and do pmap-related adjustments.
601 KKASSERT(fs.lookup_still_valid == TRUE);
602 vm_page_flag_set(fs.m, PG_REFERENCED);
603 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot | inherit_prot,
604 fs.wired, fs.entry);
606 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
607 KKASSERT(fs.m->flags & PG_BUSY);
610 * If the page is not wired down, then put it where the pageout daemon
611 * can find it.
613 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
614 if (fs.wired)
615 vm_page_wire(fs.m);
616 else
617 vm_page_unwire(fs.m, 1);
618 } else {
619 vm_page_activate(fs.m);
621 vm_page_wakeup(fs.m);
624 * Burst in a few more pages if possible. The fs.map should still
625 * be locked. To avoid interlocking against a vnode->getblk
626 * operation we had to be sure to unbusy our primary vm_page above
627 * first.
629 * A normal burst can continue down backing store, only execute
630 * if we are holding an exclusive lock, otherwise the exclusive
631 * locks the burst code gets might cause excessive SMP collisions.
633 * A quick burst can be utilized when there is no backing object
634 * (i.e. a shared file mmap).
636 if ((fault_flags & VM_FAULT_BURST) &&
637 (fs.fault_flags & VM_FAULT_WIRE_MASK) == 0 &&
638 fs.wired == 0) {
639 if (fs.first_shared == 0 && fs.shared == 0) {
640 vm_prefault(fs.map->pmap, vaddr,
641 fs.entry, fs.prot, fault_flags);
642 } else {
643 vm_prefault_quick(fs.map->pmap, vaddr,
644 fs.entry, fs.prot, fault_flags);
648 done_success:
649 mycpu->gd_cnt.v_vm_faults++;
650 if (td->td_lwp)
651 ++td->td_lwp->lwp_ru.ru_minflt;
654 * Unlock everything, and return
656 unlock_things(&fs);
658 if (td->td_lwp) {
659 if (fs.hardfault) {
660 td->td_lwp->lwp_ru.ru_majflt++;
661 } else {
662 td->td_lwp->lwp_ru.ru_minflt++;
666 /*vm_object_deallocate(fs.first_object);*/
667 /*fs.m = NULL; */
668 /*fs.first_object = NULL; must still drop later */
670 result = KERN_SUCCESS;
671 done:
672 if (fs.first_object)
673 vm_object_drop(fs.first_object);
674 done2:
675 lwkt_reltoken(&map->token);
676 if (lp)
677 lp->lwp_flags &= ~LWP_PAGING;
678 if (vm_shared_fault && fs.shared == 0)
679 ++vm_shared_miss;
681 #if !defined(NO_SWAPPING)
683 * Check the process RSS limit and force deactivation and
684 * (asynchronous) paging if necessary. This is a complex operation,
685 * only do it for direct user-mode faults, for now.
687 * To reduce overhead implement approximately a ~16MB hysteresis.
689 p = td->td_proc;
690 if ((fault_flags & VM_FAULT_USERMODE) && lp &&
691 p->p_limit && map->pmap && vm_pageout_memuse_mode >= 1 &&
692 map != &kernel_map) {
693 vm_pindex_t limit;
694 vm_pindex_t size;
696 limit = OFF_TO_IDX(qmin(p->p_rlimit[RLIMIT_RSS].rlim_cur,
697 p->p_rlimit[RLIMIT_RSS].rlim_max));
698 size = pmap_resident_tlnw_count(map->pmap);
699 if (limit >= 0 && size > 4096 && size - 4096 >= limit) {
700 vm_pageout_map_deactivate_pages(map, limit);
703 #endif
705 return (result);
709 * Fault in the specified virtual address in the current process map,
710 * returning a held VM page or NULL. See vm_fault_page() for more
711 * information.
713 * No requirements.
715 vm_page_t
716 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
718 struct lwp *lp = curthread->td_lwp;
719 vm_page_t m;
721 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
722 fault_type, VM_FAULT_NORMAL, errorp);
723 return(m);
727 * Fault in the specified virtual address in the specified map, doing all
728 * necessary manipulation of the object store and all necessary I/O. Return
729 * a held VM page or NULL, and set *errorp. The related pmap is not
730 * updated.
732 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
733 * and marked PG_REFERENCED as well.
735 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
736 * error will be returned.
738 * No requirements.
740 vm_page_t
741 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
742 int fault_flags, int *errorp)
744 vm_pindex_t first_pindex;
745 struct faultstate fs;
746 int result;
747 int retry = 0;
748 vm_prot_t orig_fault_type = fault_type;
750 fs.hardfault = 0;
751 fs.fault_flags = fault_flags;
752 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
755 * Dive the pmap (concurrency possible). If we find the
756 * appropriate page we can terminate early and quickly.
758 fs.m = pmap_fault_page_quick(map->pmap, vaddr, fault_type);
759 if (fs.m) {
760 *errorp = 0;
761 return(fs.m);
765 * Otherwise take a concurrency hit and do a formal page
766 * fault.
768 fs.shared = vm_shared_fault;
769 fs.first_shared = vm_shared_fault;
770 fs.vp = NULL;
771 lwkt_gettoken(&map->token);
774 * swap_pager_unswapped() needs an exclusive object
776 if (fault_flags & (VM_FAULT_UNSWAP | VM_FAULT_DIRTY)) {
777 fs.first_shared = 0;
780 RetryFault:
782 * Find the vm_map_entry representing the backing store and resolve
783 * the top level object and page index. This may have the side
784 * effect of executing a copy-on-write on the map entry and/or
785 * creating a shadow object, but will not COW any actual VM pages.
787 * On success fs.map is left read-locked and various other fields
788 * are initialized but not otherwise referenced or locked.
790 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
791 * if the map entry is a virtual page table and also writable,
792 * so we can set the 'A'accessed bit in the virtual page table entry.
794 fs.map = map;
795 result = vm_map_lookup(&fs.map, vaddr, fault_type,
796 &fs.entry, &fs.first_object,
797 &first_pindex, &fs.first_prot, &fs.wired);
799 if (result != KERN_SUCCESS) {
800 *errorp = result;
801 fs.m = NULL;
802 goto done;
806 * fs.map is read-locked
808 * Misc checks. Save the map generation number to detect races.
810 fs.map_generation = fs.map->timestamp;
811 fs.lookup_still_valid = TRUE;
812 fs.first_m = NULL;
813 fs.object = fs.first_object; /* so unlock_and_deallocate works */
815 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
816 panic("vm_fault: fault on nofault entry, addr: %lx",
817 (u_long)vaddr);
821 * A user-kernel shared map has no VM object and bypasses
822 * everything. We execute the uksmap function with a temporary
823 * fictitious vm_page. The address is directly mapped with no
824 * management.
826 if (fs.entry->maptype == VM_MAPTYPE_UKSMAP) {
827 struct vm_page fakem;
829 bzero(&fakem, sizeof(fakem));
830 fakem.pindex = first_pindex;
831 fakem.flags = PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED;
832 fakem.valid = VM_PAGE_BITS_ALL;
833 fakem.pat_mode = VM_MEMATTR_DEFAULT;
834 if (fs.entry->object.uksmap(fs.entry->aux.dev, &fakem)) {
835 *errorp = KERN_FAILURE;
836 fs.m = NULL;
837 unlock_things(&fs);
838 goto done2;
840 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
841 vm_page_hold(fs.m);
843 unlock_things(&fs);
844 *errorp = 0;
845 goto done;
850 * A system map entry may return a NULL object. No object means
851 * no pager means an unrecoverable kernel fault.
853 if (fs.first_object == NULL) {
854 panic("vm_fault: unrecoverable fault at %p in entry %p",
855 (void *)vaddr, fs.entry);
859 * Fail here if not a trivial anonymous page fault and TDF_NOFAULT
860 * is set.
862 * Unfortunately a deadlock can occur if we are forced to page-in
863 * from swap, but diving all the way into the vm_pager_get_page()
864 * function to find out is too much. Just check the object type.
866 if ((curthread->td_flags & TDF_NOFAULT) &&
867 (retry ||
868 fs.first_object->type == OBJT_VNODE ||
869 fs.first_object->type == OBJT_SWAP ||
870 fs.first_object->backing_object)) {
871 *errorp = KERN_FAILURE;
872 unlock_things(&fs);
873 goto done2;
877 * If the entry is wired we cannot change the page protection.
879 if (fs.wired)
880 fault_type = fs.first_prot;
883 * Make a reference to this object to prevent its disposal while we
884 * are messing with it. Once we have the reference, the map is free
885 * to be diddled. Since objects reference their shadows (and copies),
886 * they will stay around as well.
888 * The reference should also prevent an unexpected collapse of the
889 * parent that might move pages from the current object into the
890 * parent unexpectedly, resulting in corruption.
892 * Bump the paging-in-progress count to prevent size changes (e.g.
893 * truncation operations) during I/O. This must be done after
894 * obtaining the vnode lock in order to avoid possible deadlocks.
896 if (fs.first_shared)
897 vm_object_hold_shared(fs.first_object);
898 else
899 vm_object_hold(fs.first_object);
900 if (fs.vp == NULL)
901 fs.vp = vnode_pager_lock(fs.first_object); /* shared */
904 * The page we want is at (first_object, first_pindex), but if the
905 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
906 * page table to figure out the actual pindex.
908 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
909 * ONLY
911 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
912 result = vm_fault_vpagetable(&fs, &first_pindex,
913 fs.entry->aux.master_pde,
914 fault_type, 1);
915 if (result == KERN_TRY_AGAIN) {
916 vm_object_drop(fs.first_object);
917 ++retry;
918 goto RetryFault;
920 if (result != KERN_SUCCESS) {
921 *errorp = result;
922 fs.m = NULL;
923 goto done;
928 * Now we have the actual (object, pindex), fault in the page. If
929 * vm_fault_object() fails it will unlock and deallocate the FS
930 * data. If it succeeds everything remains locked and fs->object
931 * will have an additinal PIP count if it is not equal to
932 * fs->first_object
934 fs.m = NULL;
935 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
937 if (result == KERN_TRY_AGAIN) {
938 vm_object_drop(fs.first_object);
939 ++retry;
940 goto RetryFault;
942 if (result != KERN_SUCCESS) {
943 *errorp = result;
944 fs.m = NULL;
945 goto done;
948 if ((orig_fault_type & VM_PROT_WRITE) &&
949 (fs.prot & VM_PROT_WRITE) == 0) {
950 *errorp = KERN_PROTECTION_FAILURE;
951 unlock_and_deallocate(&fs);
952 fs.m = NULL;
953 goto done;
957 * DO NOT UPDATE THE PMAP!!! This function may be called for
958 * a pmap unrelated to the current process pmap, in which case
959 * the current cpu core will not be listed in the pmap's pm_active
960 * mask. Thus invalidation interlocks will fail to work properly.
962 * (for example, 'ps' uses procfs to read program arguments from
963 * each process's stack).
965 * In addition to the above this function will be called to acquire
966 * a page that might already be faulted in, re-faulting it
967 * continuously is a waste of time.
969 * XXX could this have been the cause of our random seg-fault
970 * issues? procfs accesses user stacks.
972 vm_page_flag_set(fs.m, PG_REFERENCED);
973 #if 0
974 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired, NULL);
975 mycpu->gd_cnt.v_vm_faults++;
976 if (curthread->td_lwp)
977 ++curthread->td_lwp->lwp_ru.ru_minflt;
978 #endif
981 * On success vm_fault_object() does not unlock or deallocate, and fs.m
982 * will contain a busied page. So we must unlock here after having
983 * messed with the pmap.
985 unlock_things(&fs);
988 * Return a held page. We are not doing any pmap manipulation so do
989 * not set PG_MAPPED. However, adjust the page flags according to
990 * the fault type because the caller may not use a managed pmapping
991 * (so we don't want to lose the fact that the page will be dirtied
992 * if a write fault was specified).
994 vm_page_hold(fs.m);
995 vm_page_activate(fs.m);
996 if (fault_type & VM_PROT_WRITE)
997 vm_page_dirty(fs.m);
999 if (curthread->td_lwp) {
1000 if (fs.hardfault) {
1001 curthread->td_lwp->lwp_ru.ru_majflt++;
1002 } else {
1003 curthread->td_lwp->lwp_ru.ru_minflt++;
1008 * Unlock everything, and return the held page.
1010 vm_page_wakeup(fs.m);
1011 /*vm_object_deallocate(fs.first_object);*/
1012 /*fs.first_object = NULL; */
1013 *errorp = 0;
1015 done:
1016 if (fs.first_object)
1017 vm_object_drop(fs.first_object);
1018 done2:
1019 lwkt_reltoken(&map->token);
1020 return(fs.m);
1024 * Fault in the specified (object,offset), dirty the returned page as
1025 * needed. If the requested fault_type cannot be done NULL and an
1026 * error is returned.
1028 * A held (but not busied) page is returned.
1030 * The passed in object must be held as specified by the shared
1031 * argument.
1033 vm_page_t
1034 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
1035 vm_prot_t fault_type, int fault_flags,
1036 int *sharedp, int *errorp)
1038 int result;
1039 vm_pindex_t first_pindex;
1040 struct faultstate fs;
1041 struct vm_map_entry entry;
1043 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
1044 bzero(&entry, sizeof(entry));
1045 entry.object.vm_object = object;
1046 entry.maptype = VM_MAPTYPE_NORMAL;
1047 entry.protection = entry.max_protection = fault_type;
1049 fs.hardfault = 0;
1050 fs.fault_flags = fault_flags;
1051 fs.map = NULL;
1052 fs.shared = vm_shared_fault;
1053 fs.first_shared = *sharedp;
1054 fs.vp = NULL;
1055 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
1058 * Might require swap block adjustments
1060 if (fs.first_shared && (fault_flags & (VM_FAULT_UNSWAP | VM_FAULT_DIRTY))) {
1061 fs.first_shared = 0;
1062 vm_object_upgrade(object);
1066 * Retry loop as needed (typically for shared->exclusive transitions)
1068 RetryFault:
1069 *sharedp = fs.first_shared;
1070 first_pindex = OFF_TO_IDX(offset);
1071 fs.first_object = object;
1072 fs.entry = &entry;
1073 fs.first_prot = fault_type;
1074 fs.wired = 0;
1075 /*fs.map_generation = 0; unused */
1078 * Make a reference to this object to prevent its disposal while we
1079 * are messing with it. Once we have the reference, the map is free
1080 * to be diddled. Since objects reference their shadows (and copies),
1081 * they will stay around as well.
1083 * The reference should also prevent an unexpected collapse of the
1084 * parent that might move pages from the current object into the
1085 * parent unexpectedly, resulting in corruption.
1087 * Bump the paging-in-progress count to prevent size changes (e.g.
1088 * truncation operations) during I/O. This must be done after
1089 * obtaining the vnode lock in order to avoid possible deadlocks.
1091 if (fs.vp == NULL)
1092 fs.vp = vnode_pager_lock(fs.first_object);
1094 fs.lookup_still_valid = TRUE;
1095 fs.first_m = NULL;
1096 fs.object = fs.first_object; /* so unlock_and_deallocate works */
1098 #if 0
1099 /* XXX future - ability to operate on VM object using vpagetable */
1100 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1101 result = vm_fault_vpagetable(&fs, &first_pindex,
1102 fs.entry->aux.master_pde,
1103 fault_type, 0);
1104 if (result == KERN_TRY_AGAIN) {
1105 if (fs.first_shared == 0 && *sharedp)
1106 vm_object_upgrade(object);
1107 goto RetryFault;
1109 if (result != KERN_SUCCESS) {
1110 *errorp = result;
1111 return (NULL);
1114 #endif
1117 * Now we have the actual (object, pindex), fault in the page. If
1118 * vm_fault_object() fails it will unlock and deallocate the FS
1119 * data. If it succeeds everything remains locked and fs->object
1120 * will have an additinal PIP count if it is not equal to
1121 * fs->first_object
1123 * On KERN_TRY_AGAIN vm_fault_object() leaves fs.first_object intact.
1124 * We may have to upgrade its lock to handle the requested fault.
1126 result = vm_fault_object(&fs, first_pindex, fault_type, 0);
1128 if (result == KERN_TRY_AGAIN) {
1129 if (fs.first_shared == 0 && *sharedp)
1130 vm_object_upgrade(object);
1131 goto RetryFault;
1133 if (result != KERN_SUCCESS) {
1134 *errorp = result;
1135 return(NULL);
1138 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1139 *errorp = KERN_PROTECTION_FAILURE;
1140 unlock_and_deallocate(&fs);
1141 return(NULL);
1145 * On success vm_fault_object() does not unlock or deallocate, so we
1146 * do it here. Note that the returned fs.m will be busied.
1148 unlock_things(&fs);
1151 * Return a held page. We are not doing any pmap manipulation so do
1152 * not set PG_MAPPED. However, adjust the page flags according to
1153 * the fault type because the caller may not use a managed pmapping
1154 * (so we don't want to lose the fact that the page will be dirtied
1155 * if a write fault was specified).
1157 vm_page_hold(fs.m);
1158 vm_page_activate(fs.m);
1159 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
1160 vm_page_dirty(fs.m);
1161 if (fault_flags & VM_FAULT_UNSWAP)
1162 swap_pager_unswapped(fs.m);
1165 * Indicate that the page was accessed.
1167 vm_page_flag_set(fs.m, PG_REFERENCED);
1169 if (curthread->td_lwp) {
1170 if (fs.hardfault) {
1171 curthread->td_lwp->lwp_ru.ru_majflt++;
1172 } else {
1173 curthread->td_lwp->lwp_ru.ru_minflt++;
1178 * Unlock everything, and return the held page.
1180 vm_page_wakeup(fs.m);
1181 /*vm_object_deallocate(fs.first_object);*/
1182 /*fs.first_object = NULL; */
1184 *errorp = 0;
1185 return(fs.m);
1189 * Translate the virtual page number (first_pindex) that is relative
1190 * to the address space into a logical page number that is relative to the
1191 * backing object. Use the virtual page table pointed to by (vpte).
1193 * This implements an N-level page table. Any level can terminate the
1194 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
1195 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
1197 static
1199 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1200 vpte_t vpte, int fault_type, int allow_nofault)
1202 struct lwbuf *lwb;
1203 struct lwbuf lwb_cache;
1204 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1205 int result = KERN_SUCCESS;
1206 vpte_t *ptep;
1208 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1209 for (;;) {
1211 * We cannot proceed if the vpte is not valid, not readable
1212 * for a read fault, or not writable for a write fault.
1214 if ((vpte & VPTE_V) == 0) {
1215 unlock_and_deallocate(fs);
1216 return (KERN_FAILURE);
1218 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_RW) == 0) {
1219 unlock_and_deallocate(fs);
1220 return (KERN_FAILURE);
1222 if ((vpte & VPTE_PS) || vshift == 0)
1223 break;
1224 KKASSERT(vshift >= VPTE_PAGE_BITS);
1227 * Get the page table page. Nominally we only read the page
1228 * table, but since we are actively setting VPTE_M and VPTE_A,
1229 * tell vm_fault_object() that we are writing it.
1231 * There is currently no real need to optimize this.
1233 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1234 VM_PROT_READ|VM_PROT_WRITE,
1235 allow_nofault);
1236 if (result != KERN_SUCCESS)
1237 return (result);
1240 * Process the returned fs.m and look up the page table
1241 * entry in the page table page.
1243 vshift -= VPTE_PAGE_BITS;
1244 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1245 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1246 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1247 vpte = *ptep;
1250 * Page table write-back. If the vpte is valid for the
1251 * requested operation, do a write-back to the page table.
1253 * XXX VPTE_M is not set properly for page directory pages.
1254 * It doesn't get set in the page directory if the page table
1255 * is modified during a read access.
1257 vm_page_activate(fs->m);
1258 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
1259 (vpte & VPTE_RW)) {
1260 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
1261 atomic_set_long(ptep, VPTE_M | VPTE_A);
1262 vm_page_dirty(fs->m);
1265 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V)) {
1266 if ((vpte & VPTE_A) == 0) {
1267 atomic_set_long(ptep, VPTE_A);
1268 vm_page_dirty(fs->m);
1271 lwbuf_free(lwb);
1272 vm_page_flag_set(fs->m, PG_REFERENCED);
1273 vm_page_wakeup(fs->m);
1274 fs->m = NULL;
1275 cleanup_successful_fault(fs);
1278 * Combine remaining address bits with the vpte.
1280 /* JG how many bits from each? */
1281 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1282 (*pindex & ((1L << vshift) - 1));
1283 return (KERN_SUCCESS);
1288 * This is the core of the vm_fault code.
1290 * Do all operations required to fault-in (fs.first_object, pindex). Run
1291 * through the shadow chain as necessary and do required COW or virtual
1292 * copy operations. The caller has already fully resolved the vm_map_entry
1293 * and, if appropriate, has created a copy-on-write layer. All we need to
1294 * do is iterate the object chain.
1296 * On failure (fs) is unlocked and deallocated and the caller may return or
1297 * retry depending on the failure code. On success (fs) is NOT unlocked or
1298 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1299 * will have an additional PIP count if it is not equal to fs.first_object.
1301 * If locks based on fs->first_shared or fs->shared are insufficient,
1302 * clear the appropriate field(s) and return RETRY. COWs require that
1303 * first_shared be 0, while page allocations (or frees) require that
1304 * shared be 0. Renames require that both be 0.
1306 * fs->first_object must be held on call.
1308 static
1310 vm_fault_object(struct faultstate *fs, vm_pindex_t first_pindex,
1311 vm_prot_t fault_type, int allow_nofault)
1313 vm_object_t next_object;
1314 vm_pindex_t pindex;
1315 int error;
1317 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1318 fs->prot = fs->first_prot;
1319 fs->object = fs->first_object;
1320 pindex = first_pindex;
1322 vm_object_chain_acquire(fs->first_object, fs->shared);
1323 vm_object_pip_add(fs->first_object, 1);
1326 * If a read fault occurs we try to make the page writable if
1327 * possible. There are three cases where we cannot make the
1328 * page mapping writable:
1330 * (1) The mapping is read-only or the VM object is read-only,
1331 * fs->prot above will simply not have VM_PROT_WRITE set.
1333 * (2) If the mapping is a virtual page table we need to be able
1334 * to detect writes so we can set VPTE_M in the virtual page
1335 * table.
1337 * (3) If the VM page is read-only or copy-on-write, upgrading would
1338 * just result in an unnecessary COW fault.
1340 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1341 * causes adjustments to the 'M'odify bit to also turn off write
1342 * access to force a re-fault.
1344 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1345 if ((fault_type & VM_PROT_WRITE) == 0)
1346 fs->prot &= ~VM_PROT_WRITE;
1349 if (curthread->td_lwp && curthread->td_lwp->lwp_vmspace &&
1350 pmap_emulate_ad_bits(&curthread->td_lwp->lwp_vmspace->vm_pmap)) {
1351 if ((fault_type & VM_PROT_WRITE) == 0)
1352 fs->prot &= ~VM_PROT_WRITE;
1355 /* vm_object_hold(fs->object); implied b/c object == first_object */
1357 for (;;) {
1359 * The entire backing chain from first_object to object
1360 * inclusive is chainlocked.
1362 * If the object is dead, we stop here
1364 if (fs->object->flags & OBJ_DEAD) {
1365 vm_object_pip_wakeup(fs->first_object);
1366 vm_object_chain_release_all(fs->first_object,
1367 fs->object);
1368 if (fs->object != fs->first_object)
1369 vm_object_drop(fs->object);
1370 unlock_and_deallocate(fs);
1371 return (KERN_PROTECTION_FAILURE);
1375 * See if the page is resident. Wait/Retry if the page is
1376 * busy (lots of stuff may have changed so we can't continue
1377 * in that case).
1379 * We can theoretically allow the soft-busy case on a read
1380 * fault if the page is marked valid, but since such
1381 * pages are typically already pmap'd, putting that
1382 * special case in might be more effort then it is
1383 * worth. We cannot under any circumstances mess
1384 * around with a vm_page_t->busy page except, perhaps,
1385 * to pmap it.
1387 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1388 TRUE, &error);
1389 if (error) {
1390 vm_object_pip_wakeup(fs->first_object);
1391 vm_object_chain_release_all(fs->first_object,
1392 fs->object);
1393 if (fs->object != fs->first_object)
1394 vm_object_drop(fs->object);
1395 unlock_things(fs);
1396 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1397 mycpu->gd_cnt.v_intrans++;
1398 /*vm_object_deallocate(fs->first_object);*/
1399 /*fs->first_object = NULL;*/
1400 fs->m = NULL;
1401 return (KERN_TRY_AGAIN);
1403 if (fs->m) {
1405 * The page is busied for us.
1407 * If reactivating a page from PQ_CACHE we may have
1408 * to rate-limit.
1410 int queue = fs->m->queue;
1411 vm_page_unqueue_nowakeup(fs->m);
1413 if ((queue - fs->m->pc) == PQ_CACHE &&
1414 vm_page_count_severe()) {
1415 vm_page_activate(fs->m);
1416 vm_page_wakeup(fs->m);
1417 fs->m = NULL;
1418 vm_object_pip_wakeup(fs->first_object);
1419 vm_object_chain_release_all(fs->first_object,
1420 fs->object);
1421 if (fs->object != fs->first_object)
1422 vm_object_drop(fs->object);
1423 unlock_and_deallocate(fs);
1424 if (allow_nofault == 0 ||
1425 (curthread->td_flags & TDF_NOFAULT) == 0) {
1426 thread_t td;
1428 vm_wait_pfault();
1429 td = curthread;
1430 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1431 return (KERN_PROTECTION_FAILURE);
1433 return (KERN_TRY_AGAIN);
1437 * If it still isn't completely valid (readable),
1438 * or if a read-ahead-mark is set on the VM page,
1439 * jump to readrest, else we found the page and
1440 * can return.
1442 * We can release the spl once we have marked the
1443 * page busy.
1445 if (fs->m->object != &kernel_object) {
1446 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1447 VM_PAGE_BITS_ALL) {
1448 goto readrest;
1450 if (fs->m->flags & PG_RAM) {
1451 if (debug_cluster)
1452 kprintf("R");
1453 vm_page_flag_clear(fs->m, PG_RAM);
1454 goto readrest;
1457 break; /* break to PAGE HAS BEEN FOUND */
1461 * Page is not resident, If this is the search termination
1462 * or the pager might contain the page, allocate a new page.
1464 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1466 * Allocating, must be exclusive.
1468 if (fs->object == fs->first_object &&
1469 fs->first_shared) {
1470 fs->first_shared = 0;
1471 vm_object_pip_wakeup(fs->first_object);
1472 vm_object_chain_release_all(fs->first_object,
1473 fs->object);
1474 if (fs->object != fs->first_object)
1475 vm_object_drop(fs->object);
1476 unlock_and_deallocate(fs);
1477 return (KERN_TRY_AGAIN);
1479 if (fs->object != fs->first_object &&
1480 fs->shared) {
1481 fs->first_shared = 0;
1482 fs->shared = 0;
1483 vm_object_pip_wakeup(fs->first_object);
1484 vm_object_chain_release_all(fs->first_object,
1485 fs->object);
1486 if (fs->object != fs->first_object)
1487 vm_object_drop(fs->object);
1488 unlock_and_deallocate(fs);
1489 return (KERN_TRY_AGAIN);
1493 * If the page is beyond the object size we fail
1495 if (pindex >= fs->object->size) {
1496 vm_object_pip_wakeup(fs->first_object);
1497 vm_object_chain_release_all(fs->first_object,
1498 fs->object);
1499 if (fs->object != fs->first_object)
1500 vm_object_drop(fs->object);
1501 unlock_and_deallocate(fs);
1502 return (KERN_PROTECTION_FAILURE);
1506 * Allocate a new page for this object/offset pair.
1508 * It is possible for the allocation to race, so
1509 * handle the case.
1511 fs->m = NULL;
1512 if (!vm_page_count_severe()) {
1513 fs->m = vm_page_alloc(fs->object, pindex,
1514 ((fs->vp || fs->object->backing_object) ?
1515 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1516 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1517 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1519 if (fs->m == NULL) {
1520 vm_object_pip_wakeup(fs->first_object);
1521 vm_object_chain_release_all(fs->first_object,
1522 fs->object);
1523 if (fs->object != fs->first_object)
1524 vm_object_drop(fs->object);
1525 unlock_and_deallocate(fs);
1526 if (allow_nofault == 0 ||
1527 (curthread->td_flags & TDF_NOFAULT) == 0) {
1528 thread_t td;
1530 vm_wait_pfault();
1531 td = curthread;
1532 if (td->td_proc && (td->td_proc->p_flags & P_LOWMEMKILL))
1533 return (KERN_PROTECTION_FAILURE);
1535 return (KERN_TRY_AGAIN);
1539 * Fall through to readrest. We have a new page which
1540 * will have to be paged (since m->valid will be 0).
1544 readrest:
1546 * We have found an invalid or partially valid page, a
1547 * page with a read-ahead mark which might be partially or
1548 * fully valid (and maybe dirty too), or we have allocated
1549 * a new page.
1551 * Attempt to fault-in the page if there is a chance that the
1552 * pager has it, and potentially fault in additional pages
1553 * at the same time.
1555 * If TRYPAGER is true then fs.m will be non-NULL and busied
1556 * for us.
1558 if (TRYPAGER(fs)) {
1559 int rv;
1560 int seqaccess;
1561 u_char behavior = vm_map_entry_behavior(fs->entry);
1563 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1564 seqaccess = 0;
1565 else
1566 seqaccess = -1;
1569 * Doing I/O may synchronously insert additional
1570 * pages so we can't be shared at this point either.
1572 * NOTE: We can't free fs->m here in the allocated
1573 * case (fs->object != fs->first_object) as
1574 * this would require an exclusively locked
1575 * VM object.
1577 if (fs->object == fs->first_object &&
1578 fs->first_shared) {
1579 vm_page_deactivate(fs->m);
1580 vm_page_wakeup(fs->m);
1581 fs->m = NULL;
1582 fs->first_shared = 0;
1583 vm_object_pip_wakeup(fs->first_object);
1584 vm_object_chain_release_all(fs->first_object,
1585 fs->object);
1586 if (fs->object != fs->first_object)
1587 vm_object_drop(fs->object);
1588 unlock_and_deallocate(fs);
1589 return (KERN_TRY_AGAIN);
1591 if (fs->object != fs->first_object &&
1592 fs->shared) {
1593 vm_page_deactivate(fs->m);
1594 vm_page_wakeup(fs->m);
1595 fs->m = NULL;
1596 fs->first_shared = 0;
1597 fs->shared = 0;
1598 vm_object_pip_wakeup(fs->first_object);
1599 vm_object_chain_release_all(fs->first_object,
1600 fs->object);
1601 if (fs->object != fs->first_object)
1602 vm_object_drop(fs->object);
1603 unlock_and_deallocate(fs);
1604 return (KERN_TRY_AGAIN);
1608 * Avoid deadlocking against the map when doing I/O.
1609 * fs.object and the page is PG_BUSY'd.
1611 * NOTE: Once unlocked, fs->entry can become stale
1612 * so this will NULL it out.
1614 * NOTE: fs->entry is invalid until we relock the
1615 * map and verify that the timestamp has not
1616 * changed.
1618 unlock_map(fs);
1621 * Acquire the page data. We still hold a ref on
1622 * fs.object and the page has been PG_BUSY's.
1624 * The pager may replace the page (for example, in
1625 * order to enter a fictitious page into the
1626 * object). If it does so it is responsible for
1627 * cleaning up the passed page and properly setting
1628 * the new page PG_BUSY.
1630 * If we got here through a PG_RAM read-ahead
1631 * mark the page may be partially dirty and thus
1632 * not freeable. Don't bother checking to see
1633 * if the pager has the page because we can't free
1634 * it anyway. We have to depend on the get_page
1635 * operation filling in any gaps whether there is
1636 * backing store or not.
1638 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1640 if (rv == VM_PAGER_OK) {
1642 * Relookup in case pager changed page. Pager
1643 * is responsible for disposition of old page
1644 * if moved.
1646 * XXX other code segments do relookups too.
1647 * It's a bad abstraction that needs to be
1648 * fixed/removed.
1650 fs->m = vm_page_lookup(fs->object, pindex);
1651 if (fs->m == NULL) {
1652 vm_object_pip_wakeup(fs->first_object);
1653 vm_object_chain_release_all(
1654 fs->first_object, fs->object);
1655 if (fs->object != fs->first_object)
1656 vm_object_drop(fs->object);
1657 unlock_and_deallocate(fs);
1658 return (KERN_TRY_AGAIN);
1660 ++fs->hardfault;
1661 break; /* break to PAGE HAS BEEN FOUND */
1665 * Remove the bogus page (which does not exist at this
1666 * object/offset); before doing so, we must get back
1667 * our object lock to preserve our invariant.
1669 * Also wake up any other process that may want to bring
1670 * in this page.
1672 * If this is the top-level object, we must leave the
1673 * busy page to prevent another process from rushing
1674 * past us, and inserting the page in that object at
1675 * the same time that we are.
1677 if (rv == VM_PAGER_ERROR) {
1678 if (curproc) {
1679 kprintf("vm_fault: pager read error, "
1680 "pid %d (%s)\n",
1681 curproc->p_pid,
1682 curproc->p_comm);
1683 } else {
1684 kprintf("vm_fault: pager read error, "
1685 "thread %p (%s)\n",
1686 curthread,
1687 curproc->p_comm);
1692 * Data outside the range of the pager or an I/O error
1694 * The page may have been wired during the pagein,
1695 * e.g. by the buffer cache, and cannot simply be
1696 * freed. Call vnode_pager_freepage() to deal with it.
1698 * Also note that we cannot free the page if we are
1699 * holding the related object shared. XXX not sure
1700 * what to do in that case.
1702 if (fs->object != fs->first_object) {
1703 vnode_pager_freepage(fs->m);
1704 fs->m = NULL;
1706 * XXX - we cannot just fall out at this
1707 * point, m has been freed and is invalid!
1711 * XXX - the check for kernel_map is a kludge to work
1712 * around having the machine panic on a kernel space
1713 * fault w/ I/O error.
1715 if (((fs->map != &kernel_map) &&
1716 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1717 if (fs->m) {
1718 if (fs->first_shared) {
1719 vm_page_deactivate(fs->m);
1720 vm_page_wakeup(fs->m);
1721 } else {
1722 vnode_pager_freepage(fs->m);
1724 fs->m = NULL;
1726 vm_object_pip_wakeup(fs->first_object);
1727 vm_object_chain_release_all(fs->first_object,
1728 fs->object);
1729 if (fs->object != fs->first_object)
1730 vm_object_drop(fs->object);
1731 unlock_and_deallocate(fs);
1732 if (rv == VM_PAGER_ERROR)
1733 return (KERN_FAILURE);
1734 else
1735 return (KERN_PROTECTION_FAILURE);
1736 /* NOT REACHED */
1741 * We get here if the object has a default pager (or unwiring)
1742 * or the pager doesn't have the page.
1744 * fs->first_m will be used for the COW unless we find a
1745 * deeper page to be mapped read-only, in which case the
1746 * unlock*(fs) will free first_m.
1748 if (fs->object == fs->first_object)
1749 fs->first_m = fs->m;
1752 * Move on to the next object. The chain lock should prevent
1753 * the backing_object from getting ripped out from under us.
1755 * The object lock for the next object is governed by
1756 * fs->shared.
1758 if ((next_object = fs->object->backing_object) != NULL) {
1759 if (fs->shared)
1760 vm_object_hold_shared(next_object);
1761 else
1762 vm_object_hold(next_object);
1763 vm_object_chain_acquire(next_object, fs->shared);
1764 KKASSERT(next_object == fs->object->backing_object);
1765 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1768 if (next_object == NULL) {
1770 * If there's no object left, fill the page in the top
1771 * object with zeros.
1773 if (fs->object != fs->first_object) {
1774 #if 0
1775 if (fs->first_object->backing_object !=
1776 fs->object) {
1777 vm_object_hold(fs->first_object->backing_object);
1779 #endif
1780 vm_object_chain_release_all(
1781 fs->first_object->backing_object,
1782 fs->object);
1783 #if 0
1784 if (fs->first_object->backing_object !=
1785 fs->object) {
1786 vm_object_drop(fs->first_object->backing_object);
1788 #endif
1789 vm_object_pip_wakeup(fs->object);
1790 vm_object_drop(fs->object);
1791 fs->object = fs->first_object;
1792 pindex = first_pindex;
1793 fs->m = fs->first_m;
1795 fs->first_m = NULL;
1798 * Zero the page and mark it valid.
1800 vm_page_zero_fill(fs->m);
1801 mycpu->gd_cnt.v_zfod++;
1802 fs->m->valid = VM_PAGE_BITS_ALL;
1803 break; /* break to PAGE HAS BEEN FOUND */
1805 if (fs->object != fs->first_object) {
1806 vm_object_pip_wakeup(fs->object);
1807 vm_object_lock_swap();
1808 vm_object_drop(fs->object);
1810 KASSERT(fs->object != next_object,
1811 ("object loop %p", next_object));
1812 fs->object = next_object;
1813 vm_object_pip_add(fs->object, 1);
1817 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1818 * is held.]
1820 * object still held.
1822 * local shared variable may be different from fs->shared.
1824 * If the page is being written, but isn't already owned by the
1825 * top-level object, we have to copy it into a new page owned by the
1826 * top-level object.
1828 KASSERT((fs->m->flags & PG_BUSY) != 0,
1829 ("vm_fault: not busy after main loop"));
1831 if (fs->object != fs->first_object) {
1833 * We only really need to copy if we want to write it.
1835 if (fault_type & VM_PROT_WRITE) {
1837 * This allows pages to be virtually copied from a
1838 * backing_object into the first_object, where the
1839 * backing object has no other refs to it, and cannot
1840 * gain any more refs. Instead of a bcopy, we just
1841 * move the page from the backing object to the
1842 * first object. Note that we must mark the page
1843 * dirty in the first object so that it will go out
1844 * to swap when needed.
1846 if (
1848 * Must be holding exclusive locks
1850 fs->first_shared == 0 &&
1851 fs->shared == 0 &&
1853 * Map, if present, has not changed
1855 (fs->map == NULL ||
1856 fs->map_generation == fs->map->timestamp) &&
1858 * Only one shadow object
1860 (fs->object->shadow_count == 1) &&
1862 * No COW refs, except us
1864 (fs->object->ref_count == 1) &&
1866 * No one else can look this object up
1868 (fs->object->handle == NULL) &&
1870 * No other ways to look the object up
1872 ((fs->object->type == OBJT_DEFAULT) ||
1873 (fs->object->type == OBJT_SWAP)) &&
1875 * We don't chase down the shadow chain
1877 (fs->object == fs->first_object->backing_object) &&
1880 * grab the lock if we need to
1882 (fs->lookup_still_valid ||
1883 fs->map == NULL ||
1884 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1887 * (first_m) and (m) are both busied. We have
1888 * move (m) into (first_m)'s object/pindex
1889 * in an atomic fashion, then free (first_m).
1891 * first_object is held so second remove
1892 * followed by the rename should wind
1893 * up being atomic. vm_page_free() might
1894 * block so we don't do it until after the
1895 * rename.
1897 fs->lookup_still_valid = 1;
1898 vm_page_protect(fs->first_m, VM_PROT_NONE);
1899 vm_page_remove(fs->first_m);
1900 vm_page_rename(fs->m, fs->first_object,
1901 first_pindex);
1902 vm_page_free(fs->first_m);
1903 fs->first_m = fs->m;
1904 fs->m = NULL;
1905 mycpu->gd_cnt.v_cow_optim++;
1906 } else {
1908 * Oh, well, lets copy it.
1910 * Why are we unmapping the original page
1911 * here? Well, in short, not all accessors
1912 * of user memory go through the pmap. The
1913 * procfs code doesn't have access user memory
1914 * via a local pmap, so vm_fault_page*()
1915 * can't call pmap_enter(). And the umtx*()
1916 * code may modify the COW'd page via a DMAP
1917 * or kernel mapping and not via the pmap,
1918 * leaving the original page still mapped
1919 * read-only into the pmap.
1921 * So we have to remove the page from at
1922 * least the current pmap if it is in it.
1923 * Just remove it from all pmaps.
1925 KKASSERT(fs->first_shared == 0);
1926 vm_page_copy(fs->m, fs->first_m);
1927 vm_page_protect(fs->m, VM_PROT_NONE);
1928 vm_page_event(fs->m, VMEVENT_COW);
1932 * We no longer need the old page or object.
1934 if (fs->m)
1935 release_page(fs);
1938 * We intend to revert to first_object, undo the
1939 * chain lock through to that.
1941 #if 0
1942 if (fs->first_object->backing_object != fs->object)
1943 vm_object_hold(fs->first_object->backing_object);
1944 #endif
1945 vm_object_chain_release_all(
1946 fs->first_object->backing_object,
1947 fs->object);
1948 #if 0
1949 if (fs->first_object->backing_object != fs->object)
1950 vm_object_drop(fs->first_object->backing_object);
1951 #endif
1954 * fs->object != fs->first_object due to above
1955 * conditional
1957 vm_object_pip_wakeup(fs->object);
1958 vm_object_drop(fs->object);
1961 * Only use the new page below...
1963 mycpu->gd_cnt.v_cow_faults++;
1964 fs->m = fs->first_m;
1965 fs->object = fs->first_object;
1966 pindex = first_pindex;
1967 } else {
1969 * If it wasn't a write fault avoid having to copy
1970 * the page by mapping it read-only.
1972 fs->prot &= ~VM_PROT_WRITE;
1977 * Relock the map if necessary, then check the generation count.
1978 * relock_map() will update fs->timestamp to account for the
1979 * relocking if necessary.
1981 * If the count has changed after relocking then all sorts of
1982 * crap may have happened and we have to retry.
1984 * NOTE: The relock_map() can fail due to a deadlock against
1985 * the vm_page we are holding BUSY.
1987 if (fs->lookup_still_valid == FALSE && fs->map) {
1988 if (relock_map(fs) ||
1989 fs->map->timestamp != fs->map_generation) {
1990 release_page(fs);
1991 vm_object_pip_wakeup(fs->first_object);
1992 vm_object_chain_release_all(fs->first_object,
1993 fs->object);
1994 if (fs->object != fs->first_object)
1995 vm_object_drop(fs->object);
1996 unlock_and_deallocate(fs);
1997 return (KERN_TRY_AGAIN);
2002 * If the fault is a write, we know that this page is being
2003 * written NOW so dirty it explicitly to save on pmap_is_modified()
2004 * calls later.
2006 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
2007 * if the page is already dirty to prevent data written with
2008 * the expectation of being synced from not being synced.
2009 * Likewise if this entry does not request NOSYNC then make
2010 * sure the page isn't marked NOSYNC. Applications sharing
2011 * data should use the same flags to avoid ping ponging.
2013 * Also tell the backing pager, if any, that it should remove
2014 * any swap backing since the page is now dirty.
2016 vm_page_activate(fs->m);
2017 if (fs->prot & VM_PROT_WRITE) {
2018 vm_object_set_writeable_dirty(fs->m->object);
2019 vm_set_nosync(fs->m, fs->entry);
2020 if (fs->fault_flags & VM_FAULT_DIRTY) {
2021 vm_page_dirty(fs->m);
2022 swap_pager_unswapped(fs->m);
2026 vm_object_pip_wakeup(fs->first_object);
2027 vm_object_chain_release_all(fs->first_object, fs->object);
2028 if (fs->object != fs->first_object)
2029 vm_object_drop(fs->object);
2032 * Page had better still be busy. We are still locked up and
2033 * fs->object will have another PIP reference if it is not equal
2034 * to fs->first_object.
2036 KASSERT(fs->m->flags & PG_BUSY,
2037 ("vm_fault: page %p not busy!", fs->m));
2040 * Sanity check: page must be completely valid or it is not fit to
2041 * map into user space. vm_pager_get_pages() ensures this.
2043 if (fs->m->valid != VM_PAGE_BITS_ALL) {
2044 vm_page_zero_invalid(fs->m, TRUE);
2045 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
2048 return (KERN_SUCCESS);
2052 * Hold each of the physical pages that are mapped by the specified range of
2053 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
2054 * and allow the specified types of access, "prot". If all of the implied
2055 * pages are successfully held, then the number of held pages is returned
2056 * together with pointers to those pages in the array "ma". However, if any
2057 * of the pages cannot be held, -1 is returned.
2060 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
2061 vm_prot_t prot, vm_page_t *ma, int max_count)
2063 vm_offset_t start, end;
2064 int i, npages, error;
2066 start = trunc_page(addr);
2067 end = round_page(addr + len);
2069 npages = howmany(end - start, PAGE_SIZE);
2071 if (npages > max_count)
2072 return -1;
2074 for (i = 0; i < npages; i++) {
2075 // XXX error handling
2076 ma[i] = vm_fault_page_quick(start + (i * PAGE_SIZE),
2077 prot,
2078 &error);
2081 return npages;
2085 * Wire down a range of virtual addresses in a map. The entry in question
2086 * should be marked in-transition and the map must be locked. We must
2087 * release the map temporarily while faulting-in the page to avoid a
2088 * deadlock. Note that the entry may be clipped while we are blocked but
2089 * will never be freed.
2091 * No requirements.
2094 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2095 boolean_t user_wire, int kmflags)
2097 boolean_t fictitious;
2098 vm_offset_t start;
2099 vm_offset_t end;
2100 vm_offset_t va;
2101 vm_paddr_t pa;
2102 vm_page_t m;
2103 pmap_t pmap;
2104 int rv;
2105 int wire_prot;
2106 int fault_flags;
2108 lwkt_gettoken(&map->token);
2110 if (user_wire) {
2111 wire_prot = VM_PROT_READ;
2112 fault_flags = VM_FAULT_USER_WIRE;
2113 } else {
2114 wire_prot = VM_PROT_READ | VM_PROT_WRITE;
2115 fault_flags = VM_FAULT_CHANGE_WIRING;
2117 if (kmflags & KM_NOTLBSYNC)
2118 wire_prot |= VM_PROT_NOSYNC;
2120 pmap = vm_map_pmap(map);
2121 start = entry->start;
2122 end = entry->end;
2123 switch(entry->maptype) {
2124 case VM_MAPTYPE_NORMAL:
2125 case VM_MAPTYPE_VPAGETABLE:
2126 fictitious = entry->object.vm_object &&
2127 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2128 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2129 break;
2130 case VM_MAPTYPE_UKSMAP:
2131 fictitious = TRUE;
2132 break;
2133 default:
2134 fictitious = FALSE;
2135 break;
2138 if (entry->eflags & MAP_ENTRY_KSTACK)
2139 start += PAGE_SIZE;
2140 map->timestamp++;
2141 vm_map_unlock(map);
2144 * We simulate a fault to get the page and enter it in the physical
2145 * map.
2147 for (va = start; va < end; va += PAGE_SIZE) {
2148 rv = vm_fault(map, va, wire_prot, fault_flags);
2149 if (rv) {
2150 while (va > start) {
2151 va -= PAGE_SIZE;
2152 if ((pa = pmap_extract(pmap, va)) == 0)
2153 continue;
2154 pmap_change_wiring(pmap, va, FALSE, entry);
2155 if (!fictitious) {
2156 m = PHYS_TO_VM_PAGE(pa);
2157 vm_page_busy_wait(m, FALSE, "vmwrpg");
2158 vm_page_unwire(m, 1);
2159 vm_page_wakeup(m);
2162 goto done;
2165 rv = KERN_SUCCESS;
2166 done:
2167 vm_map_lock(map);
2168 lwkt_reltoken(&map->token);
2169 return (rv);
2173 * Unwire a range of virtual addresses in a map. The map should be
2174 * locked.
2176 void
2177 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2179 boolean_t fictitious;
2180 vm_offset_t start;
2181 vm_offset_t end;
2182 vm_offset_t va;
2183 vm_paddr_t pa;
2184 vm_page_t m;
2185 pmap_t pmap;
2187 lwkt_gettoken(&map->token);
2189 pmap = vm_map_pmap(map);
2190 start = entry->start;
2191 end = entry->end;
2192 fictitious = entry->object.vm_object &&
2193 ((entry->object.vm_object->type == OBJT_DEVICE) ||
2194 (entry->object.vm_object->type == OBJT_MGTDEVICE));
2195 if (entry->eflags & MAP_ENTRY_KSTACK)
2196 start += PAGE_SIZE;
2199 * Since the pages are wired down, we must be able to get their
2200 * mappings from the physical map system.
2202 for (va = start; va < end; va += PAGE_SIZE) {
2203 pa = pmap_extract(pmap, va);
2204 if (pa != 0) {
2205 pmap_change_wiring(pmap, va, FALSE, entry);
2206 if (!fictitious) {
2207 m = PHYS_TO_VM_PAGE(pa);
2208 vm_page_busy_wait(m, FALSE, "vmwupg");
2209 vm_page_unwire(m, 1);
2210 vm_page_wakeup(m);
2214 lwkt_reltoken(&map->token);
2218 * Copy all of the pages from a wired-down map entry to another.
2220 * The source and destination maps must be locked for write.
2221 * The source and destination maps token must be held
2222 * The source map entry must be wired down (or be a sharing map
2223 * entry corresponding to a main map entry that is wired down).
2225 * No other requirements.
2227 * XXX do segment optimization
2229 void
2230 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
2231 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
2233 vm_object_t dst_object;
2234 vm_object_t src_object;
2235 vm_ooffset_t dst_offset;
2236 vm_ooffset_t src_offset;
2237 vm_prot_t prot;
2238 vm_offset_t vaddr;
2239 vm_page_t dst_m;
2240 vm_page_t src_m;
2242 src_object = src_entry->object.vm_object;
2243 src_offset = src_entry->offset;
2246 * Create the top-level object for the destination entry. (Doesn't
2247 * actually shadow anything - we copy the pages directly.)
2249 vm_map_entry_allocate_object(dst_entry);
2250 dst_object = dst_entry->object.vm_object;
2252 prot = dst_entry->max_protection;
2255 * Loop through all of the pages in the entry's range, copying each
2256 * one from the source object (it should be there) to the destination
2257 * object.
2259 vm_object_hold(src_object);
2260 vm_object_hold(dst_object);
2261 for (vaddr = dst_entry->start, dst_offset = 0;
2262 vaddr < dst_entry->end;
2263 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
2266 * Allocate a page in the destination object
2268 do {
2269 dst_m = vm_page_alloc(dst_object,
2270 OFF_TO_IDX(dst_offset),
2271 VM_ALLOC_NORMAL);
2272 if (dst_m == NULL) {
2273 vm_wait(0);
2275 } while (dst_m == NULL);
2278 * Find the page in the source object, and copy it in.
2279 * (Because the source is wired down, the page will be in
2280 * memory.)
2282 src_m = vm_page_lookup(src_object,
2283 OFF_TO_IDX(dst_offset + src_offset));
2284 if (src_m == NULL)
2285 panic("vm_fault_copy_wired: page missing");
2287 vm_page_copy(src_m, dst_m);
2288 vm_page_event(src_m, VMEVENT_COW);
2291 * Enter it in the pmap...
2293 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
2296 * Mark it no longer busy, and put it on the active list.
2298 vm_page_activate(dst_m);
2299 vm_page_wakeup(dst_m);
2301 vm_object_drop(dst_object);
2302 vm_object_drop(src_object);
2305 #if 0
2308 * This routine checks around the requested page for other pages that
2309 * might be able to be faulted in. This routine brackets the viable
2310 * pages for the pages to be paged in.
2312 * Inputs:
2313 * m, rbehind, rahead
2315 * Outputs:
2316 * marray (array of vm_page_t), reqpage (index of requested page)
2318 * Return value:
2319 * number of pages in marray
2321 static int
2322 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2323 vm_page_t *marray, int *reqpage)
2325 int i,j;
2326 vm_object_t object;
2327 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2328 vm_page_t rtm;
2329 int cbehind, cahead;
2331 object = m->object;
2332 pindex = m->pindex;
2335 * we don't fault-ahead for device pager
2337 if ((object->type == OBJT_DEVICE) ||
2338 (object->type == OBJT_MGTDEVICE)) {
2339 *reqpage = 0;
2340 marray[0] = m;
2341 return 1;
2345 * if the requested page is not available, then give up now
2347 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2348 *reqpage = 0; /* not used by caller, fix compiler warn */
2349 return 0;
2352 if ((cbehind == 0) && (cahead == 0)) {
2353 *reqpage = 0;
2354 marray[0] = m;
2355 return 1;
2358 if (rahead > cahead) {
2359 rahead = cahead;
2362 if (rbehind > cbehind) {
2363 rbehind = cbehind;
2367 * Do not do any readahead if we have insufficient free memory.
2369 * XXX code was broken disabled before and has instability
2370 * with this conditonal fixed, so shortcut for now.
2372 if (burst_fault == 0 || vm_page_count_severe()) {
2373 marray[0] = m;
2374 *reqpage = 0;
2375 return 1;
2379 * scan backward for the read behind pages -- in memory
2381 * Assume that if the page is not found an interrupt will not
2382 * create it. Theoretically interrupts can only remove (busy)
2383 * pages, not create new associations.
2385 if (pindex > 0) {
2386 if (rbehind > pindex) {
2387 rbehind = pindex;
2388 startpindex = 0;
2389 } else {
2390 startpindex = pindex - rbehind;
2393 vm_object_hold(object);
2394 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2395 if (vm_page_lookup(object, tpindex - 1))
2396 break;
2399 i = 0;
2400 while (tpindex < pindex) {
2401 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2402 VM_ALLOC_NULL_OK);
2403 if (rtm == NULL) {
2404 for (j = 0; j < i; j++) {
2405 vm_page_free(marray[j]);
2407 vm_object_drop(object);
2408 marray[0] = m;
2409 *reqpage = 0;
2410 return 1;
2412 marray[i] = rtm;
2413 ++i;
2414 ++tpindex;
2416 vm_object_drop(object);
2417 } else {
2418 i = 0;
2422 * Assign requested page
2424 marray[i] = m;
2425 *reqpage = i;
2426 ++i;
2429 * Scan forwards for read-ahead pages
2431 tpindex = pindex + 1;
2432 endpindex = tpindex + rahead;
2433 if (endpindex > object->size)
2434 endpindex = object->size;
2436 vm_object_hold(object);
2437 while (tpindex < endpindex) {
2438 if (vm_page_lookup(object, tpindex))
2439 break;
2440 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2441 VM_ALLOC_NULL_OK);
2442 if (rtm == NULL)
2443 break;
2444 marray[i] = rtm;
2445 ++i;
2446 ++tpindex;
2448 vm_object_drop(object);
2450 return (i);
2453 #endif
2456 * vm_prefault() provides a quick way of clustering pagefaults into a
2457 * processes address space. It is a "cousin" of pmap_object_init_pt,
2458 * except it runs at page fault time instead of mmap time.
2460 * vm.fast_fault Enables pre-faulting zero-fill pages
2462 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2463 * prefault. Scan stops in either direction when
2464 * a page is found to already exist.
2466 * This code used to be per-platform pmap_prefault(). It is now
2467 * machine-independent and enhanced to also pre-fault zero-fill pages
2468 * (see vm.fast_fault) as well as make them writable, which greatly
2469 * reduces the number of page faults programs incur.
2471 * Application performance when pre-faulting zero-fill pages is heavily
2472 * dependent on the application. Very tiny applications like /bin/echo
2473 * lose a little performance while applications of any appreciable size
2474 * gain performance. Prefaulting multiple pages also reduces SMP
2475 * congestion and can improve SMP performance significantly.
2477 * NOTE! prot may allow writing but this only applies to the top level
2478 * object. If we wind up mapping a page extracted from a backing
2479 * object we have to make sure it is read-only.
2481 * NOTE! The caller has already handled any COW operations on the
2482 * vm_map_entry via the normal fault code. Do NOT call this
2483 * shortcut unless the normal fault code has run on this entry.
2485 * The related map must be locked.
2486 * No other requirements.
2488 static int vm_prefault_pages = 8;
2489 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2490 "Maximum number of pages to pre-fault");
2491 static int vm_fast_fault = 1;
2492 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2493 "Burst fault zero-fill regions");
2496 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2497 * is not already dirty by other means. This will prevent passive
2498 * filesystem syncing as well as 'sync' from writing out the page.
2500 static void
2501 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2503 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2504 if (m->dirty == 0)
2505 vm_page_flag_set(m, PG_NOSYNC);
2506 } else {
2507 vm_page_flag_clear(m, PG_NOSYNC);
2511 static void
2512 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2513 int fault_flags)
2515 struct lwp *lp;
2516 vm_page_t m;
2517 vm_offset_t addr;
2518 vm_pindex_t index;
2519 vm_pindex_t pindex;
2520 vm_object_t object;
2521 int pprot;
2522 int i;
2523 int noneg;
2524 int nopos;
2525 int maxpages;
2528 * Get stable max count value, disabled if set to 0
2530 maxpages = vm_prefault_pages;
2531 cpu_ccfence();
2532 if (maxpages <= 0)
2533 return;
2536 * We do not currently prefault mappings that use virtual page
2537 * tables. We do not prefault foreign pmaps.
2539 if (entry->maptype != VM_MAPTYPE_NORMAL)
2540 return;
2541 lp = curthread->td_lwp;
2542 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2543 return;
2546 * Limit pre-fault count to 1024 pages.
2548 if (maxpages > 1024)
2549 maxpages = 1024;
2551 object = entry->object.vm_object;
2552 KKASSERT(object != NULL);
2553 KKASSERT(object == entry->object.vm_object);
2554 vm_object_hold(object);
2555 vm_object_chain_acquire(object, 0);
2557 noneg = 0;
2558 nopos = 0;
2559 for (i = 0; i < maxpages; ++i) {
2560 vm_object_t lobject;
2561 vm_object_t nobject;
2562 int allocated = 0;
2563 int error;
2566 * This can eat a lot of time on a heavily contended
2567 * machine so yield on the tick if needed.
2569 if ((i & 7) == 7)
2570 lwkt_yield();
2573 * Calculate the page to pre-fault, stopping the scan in
2574 * each direction separately if the limit is reached.
2576 if (i & 1) {
2577 if (noneg)
2578 continue;
2579 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2580 } else {
2581 if (nopos)
2582 continue;
2583 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2585 if (addr < entry->start) {
2586 noneg = 1;
2587 if (noneg && nopos)
2588 break;
2589 continue;
2591 if (addr >= entry->end) {
2592 nopos = 1;
2593 if (noneg && nopos)
2594 break;
2595 continue;
2599 * Skip pages already mapped, and stop scanning in that
2600 * direction. When the scan terminates in both directions
2601 * we are done.
2603 if (pmap_prefault_ok(pmap, addr) == 0) {
2604 if (i & 1)
2605 noneg = 1;
2606 else
2607 nopos = 1;
2608 if (noneg && nopos)
2609 break;
2610 continue;
2614 * Follow the VM object chain to obtain the page to be mapped
2615 * into the pmap.
2617 * If we reach the terminal object without finding a page
2618 * and we determine it would be advantageous, then allocate
2619 * a zero-fill page for the base object. The base object
2620 * is guaranteed to be OBJT_DEFAULT for this case.
2622 * In order to not have to check the pager via *haspage*()
2623 * we stop if any non-default object is encountered. e.g.
2624 * a vnode or swap object would stop the loop.
2626 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2627 lobject = object;
2628 pindex = index;
2629 pprot = prot;
2631 KKASSERT(lobject == entry->object.vm_object);
2632 /*vm_object_hold(lobject); implied */
2634 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2635 TRUE, &error)) == NULL) {
2636 if (lobject->type != OBJT_DEFAULT)
2637 break;
2638 if (lobject->backing_object == NULL) {
2639 if (vm_fast_fault == 0)
2640 break;
2641 if ((prot & VM_PROT_WRITE) == 0 ||
2642 vm_page_count_min(0)) {
2643 break;
2647 * NOTE: Allocated from base object
2649 m = vm_page_alloc(object, index,
2650 VM_ALLOC_NORMAL |
2651 VM_ALLOC_ZERO |
2652 VM_ALLOC_USE_GD |
2653 VM_ALLOC_NULL_OK);
2654 if (m == NULL)
2655 break;
2656 allocated = 1;
2657 pprot = prot;
2658 /* lobject = object .. not needed */
2659 break;
2661 if (lobject->backing_object_offset & PAGE_MASK)
2662 break;
2663 nobject = lobject->backing_object;
2664 vm_object_hold(nobject);
2665 KKASSERT(nobject == lobject->backing_object);
2666 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2667 if (lobject != object) {
2668 vm_object_lock_swap();
2669 vm_object_drop(lobject);
2671 lobject = nobject;
2672 pprot &= ~VM_PROT_WRITE;
2673 vm_object_chain_acquire(lobject, 0);
2677 * NOTE: A non-NULL (m) will be associated with lobject if
2678 * it was found there, otherwise it is probably a
2679 * zero-fill page associated with the base object.
2681 * Give-up if no page is available.
2683 if (m == NULL) {
2684 if (lobject != object) {
2685 #if 0
2686 if (object->backing_object != lobject)
2687 vm_object_hold(object->backing_object);
2688 #endif
2689 vm_object_chain_release_all(
2690 object->backing_object, lobject);
2691 #if 0
2692 if (object->backing_object != lobject)
2693 vm_object_drop(object->backing_object);
2694 #endif
2695 vm_object_drop(lobject);
2697 break;
2701 * The object must be marked dirty if we are mapping a
2702 * writable page. m->object is either lobject or object,
2703 * both of which are still held. Do this before we
2704 * potentially drop the object.
2706 if (pprot & VM_PROT_WRITE)
2707 vm_object_set_writeable_dirty(m->object);
2710 * Do not conditionalize on PG_RAM. If pages are present in
2711 * the VM system we assume optimal caching. If caching is
2712 * not optimal the I/O gravy train will be restarted when we
2713 * hit an unavailable page. We do not want to try to restart
2714 * the gravy train now because we really don't know how much
2715 * of the object has been cached. The cost for restarting
2716 * the gravy train should be low (since accesses will likely
2717 * be I/O bound anyway).
2719 if (lobject != object) {
2720 #if 0
2721 if (object->backing_object != lobject)
2722 vm_object_hold(object->backing_object);
2723 #endif
2724 vm_object_chain_release_all(object->backing_object,
2725 lobject);
2726 #if 0
2727 if (object->backing_object != lobject)
2728 vm_object_drop(object->backing_object);
2729 #endif
2730 vm_object_drop(lobject);
2734 * Enter the page into the pmap if appropriate. If we had
2735 * allocated the page we have to place it on a queue. If not
2736 * we just have to make sure it isn't on the cache queue
2737 * (pages on the cache queue are not allowed to be mapped).
2739 if (allocated) {
2741 * Page must be zerod.
2743 vm_page_zero_fill(m);
2744 mycpu->gd_cnt.v_zfod++;
2745 m->valid = VM_PAGE_BITS_ALL;
2748 * Handle dirty page case
2750 if (pprot & VM_PROT_WRITE)
2751 vm_set_nosync(m, entry);
2752 pmap_enter(pmap, addr, m, pprot, 0, entry);
2753 mycpu->gd_cnt.v_vm_faults++;
2754 if (curthread->td_lwp)
2755 ++curthread->td_lwp->lwp_ru.ru_minflt;
2756 vm_page_deactivate(m);
2757 if (pprot & VM_PROT_WRITE) {
2758 /*vm_object_set_writeable_dirty(m->object);*/
2759 vm_set_nosync(m, entry);
2760 if (fault_flags & VM_FAULT_DIRTY) {
2761 vm_page_dirty(m);
2762 /*XXX*/
2763 swap_pager_unswapped(m);
2766 vm_page_wakeup(m);
2767 } else if (error) {
2768 /* couldn't busy page, no wakeup */
2769 } else if (
2770 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2771 (m->flags & PG_FICTITIOUS) == 0) {
2773 * A fully valid page not undergoing soft I/O can
2774 * be immediately entered into the pmap.
2776 if ((m->queue - m->pc) == PQ_CACHE)
2777 vm_page_deactivate(m);
2778 if (pprot & VM_PROT_WRITE) {
2779 /*vm_object_set_writeable_dirty(m->object);*/
2780 vm_set_nosync(m, entry);
2781 if (fault_flags & VM_FAULT_DIRTY) {
2782 vm_page_dirty(m);
2783 /*XXX*/
2784 swap_pager_unswapped(m);
2787 if (pprot & VM_PROT_WRITE)
2788 vm_set_nosync(m, entry);
2789 pmap_enter(pmap, addr, m, pprot, 0, entry);
2790 mycpu->gd_cnt.v_vm_faults++;
2791 if (curthread->td_lwp)
2792 ++curthread->td_lwp->lwp_ru.ru_minflt;
2793 vm_page_wakeup(m);
2794 } else {
2795 vm_page_wakeup(m);
2798 vm_object_chain_release(object);
2799 vm_object_drop(object);
2803 * Object can be held shared
2805 static void
2806 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2807 vm_map_entry_t entry, int prot, int fault_flags)
2809 struct lwp *lp;
2810 vm_page_t m;
2811 vm_offset_t addr;
2812 vm_pindex_t pindex;
2813 vm_object_t object;
2814 int i;
2815 int noneg;
2816 int nopos;
2817 int maxpages;
2820 * Get stable max count value, disabled if set to 0
2822 maxpages = vm_prefault_pages;
2823 cpu_ccfence();
2824 if (maxpages <= 0)
2825 return;
2828 * We do not currently prefault mappings that use virtual page
2829 * tables. We do not prefault foreign pmaps.
2831 if (entry->maptype != VM_MAPTYPE_NORMAL)
2832 return;
2833 lp = curthread->td_lwp;
2834 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2835 return;
2836 object = entry->object.vm_object;
2837 if (object->backing_object != NULL)
2838 return;
2839 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2842 * Limit pre-fault count to 1024 pages.
2844 if (maxpages > 1024)
2845 maxpages = 1024;
2847 noneg = 0;
2848 nopos = 0;
2849 for (i = 0; i < maxpages; ++i) {
2850 int error;
2853 * Calculate the page to pre-fault, stopping the scan in
2854 * each direction separately if the limit is reached.
2856 if (i & 1) {
2857 if (noneg)
2858 continue;
2859 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2860 } else {
2861 if (nopos)
2862 continue;
2863 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2865 if (addr < entry->start) {
2866 noneg = 1;
2867 if (noneg && nopos)
2868 break;
2869 continue;
2871 if (addr >= entry->end) {
2872 nopos = 1;
2873 if (noneg && nopos)
2874 break;
2875 continue;
2879 * Follow the VM object chain to obtain the page to be mapped
2880 * into the pmap. This version of the prefault code only
2881 * works with terminal objects.
2883 * The page must already exist. If we encounter a problem
2884 * we stop here.
2886 * WARNING! We cannot call swap_pager_unswapped() or insert
2887 * a new vm_page with a shared token.
2889 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2891 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
2892 if (m == NULL || error)
2893 break;
2896 * Skip pages already mapped, and stop scanning in that
2897 * direction. When the scan terminates in both directions
2898 * we are done.
2900 if (pmap_prefault_ok(pmap, addr) == 0) {
2901 vm_page_wakeup(m);
2902 if (i & 1)
2903 noneg = 1;
2904 else
2905 nopos = 1;
2906 if (noneg && nopos)
2907 break;
2908 continue;
2912 * Stop if the page cannot be trivially entered into the
2913 * pmap.
2915 if (((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) ||
2916 (m->flags & PG_FICTITIOUS) ||
2917 ((m->flags & PG_SWAPPED) &&
2918 (prot & VM_PROT_WRITE) &&
2919 (fault_flags & VM_FAULT_DIRTY))) {
2920 vm_page_wakeup(m);
2921 break;
2925 * Enter the page into the pmap. The object might be held
2926 * shared so we can't do any (serious) modifying operation
2927 * on it.
2929 if ((m->queue - m->pc) == PQ_CACHE)
2930 vm_page_deactivate(m);
2931 if (prot & VM_PROT_WRITE) {
2932 vm_object_set_writeable_dirty(m->object);
2933 vm_set_nosync(m, entry);
2934 if (fault_flags & VM_FAULT_DIRTY) {
2935 vm_page_dirty(m);
2936 /* can't happeen due to conditional above */
2937 /* swap_pager_unswapped(m); */
2940 pmap_enter(pmap, addr, m, prot, 0, entry);
2941 mycpu->gd_cnt.v_vm_faults++;
2942 if (curthread->td_lwp)
2943 ++curthread->td_lwp->lwp_ru.ru_minflt;
2944 vm_page_wakeup(m);