udp: Don't propagate connect error, as long as the inpcb has local port.
[dragonfly.git] / sys / vm / vm_fault.c
blobba25f982110c3326ddb8137ea90db21ca9f78069
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 int growstack;
308 int retry = 0;
309 int inherit_prot;
311 inherit_prot = fault_type & VM_PROT_NOSYNC;
312 fs.hardfault = 0;
313 fs.fault_flags = fault_flags;
314 fs.vp = NULL;
315 fs.shared = vm_shared_fault;
316 fs.first_shared = vm_shared_fault;
317 growstack = 1;
318 if (vm_shared_fault)
319 ++vm_shared_count;
322 * vm_map interactions
324 if ((lp = curthread->td_lwp) != NULL)
325 lp->lwp_flags |= LWP_PAGING;
326 lwkt_gettoken(&map->token);
328 RetryFault:
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
341 * table entry.
343 fs.map = map;
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
356 * to do.
358 if (result != KERN_SUCCESS) {
359 if (result == KERN_FAILURE_NOFAULT) {
360 result = KERN_FAILURE;
361 goto done;
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) {
370 growstack = 0;
371 ++retry;
372 goto RetryFault;
374 result = KERN_FAILURE;
376 goto done;
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,
391 &fs.wired);
392 if (result != KERN_SUCCESS) {
393 /* could also be KERN_FAILURE_NOFAULT */
394 result = KERN_FAILURE;
395 goto done;
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;
417 fs.first_m = NULL;
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",
424 (void *)vaddr);
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",
430 (void *)vaddr);
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
438 * management.
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;
450 unlock_things(&fs);
451 goto done2;
453 pmap_enter(fs.map->pmap, vaddr, &fakem, fs.prot | inherit_prot,
454 fs.wired, fs.entry);
455 goto done_success;
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
469 * is set.
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) &&
480 (retry ||
481 fs.first_object->type == OBJT_VNODE ||
482 fs.first_object->type == OBJT_SWAP ||
483 fs.first_object->backing_object)) {
484 result = KERN_FAILURE;
485 unlock_things(&fs);
486 goto done2;
490 * If the entry is wired we cannot change the page protection.
492 if (fs.wired)
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) {
513 fs.first_shared = 0;
517 * swap_pager_unswapped() needs an exclusive object
519 if (fault_flags & (VM_FAULT_UNSWAP | VM_FAULT_DIRTY)) {
520 fs.first_shared = 0;
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.
530 if (fs.first_shared)
531 vm_object_hold_shared(fs.first_object);
532 else
533 vm_object_hold(fs.first_object);
534 if (fs.vp == NULL)
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
543 * ONLY
545 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
546 result = vm_fault_vpagetable(&fs, &first_pindex,
547 fs.entry->aux.master_pde,
548 fault_type, 1);
549 if (result == KERN_TRY_AGAIN) {
550 vm_object_drop(fs.first_object);
551 ++retry;
552 goto RetryFault;
554 if (result != KERN_SUCCESS)
555 goto done;
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
563 * fs->first_object
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
569 * page table.
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) {
577 --debug_fault;
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);
586 ++retry;
587 goto RetryFault;
589 if (result != KERN_SUCCESS)
590 goto done;
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,
601 fs.wired, fs.entry);
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
608 * can find it.
610 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
611 if (fs.wired)
612 vm_page_wire(fs.m);
613 else
614 vm_page_unwire(fs.m, 1);
615 } else {
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
624 * first.
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 &&
635 fs.wired == 0) {
636 if (fs.first_shared == 0 && fs.shared == 0) {
637 vm_prefault(fs.map->pmap, vaddr,
638 fs.entry, fs.prot, fault_flags);
639 } else {
640 vm_prefault_quick(fs.map->pmap, vaddr,
641 fs.entry, fs.prot, fault_flags);
645 done_success:
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
653 unlock_things(&fs);
655 if (curthread->td_lwp) {
656 if (fs.hardfault) {
657 curthread->td_lwp->lwp_ru.ru_majflt++;
658 } else {
659 curthread->td_lwp->lwp_ru.ru_minflt++;
663 /*vm_object_deallocate(fs.first_object);*/
664 /*fs.m = NULL; */
665 /*fs.first_object = NULL; must still drop later */
667 result = KERN_SUCCESS;
668 done:
669 if (fs.first_object)
670 vm_object_drop(fs.first_object);
671 done2:
672 lwkt_reltoken(&map->token);
673 if (lp)
674 lp->lwp_flags &= ~LWP_PAGING;
675 if (vm_shared_fault && fs.shared == 0)
676 ++vm_shared_miss;
677 return (result);
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
683 * information.
685 * No requirements.
687 vm_page_t
688 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
690 struct lwp *lp = curthread->td_lwp;
691 vm_page_t m;
693 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
694 fault_type, VM_FAULT_NORMAL, errorp);
695 return(m);
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
702 * updated.
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.
710 * No requirements.
712 vm_page_t
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;
718 int result;
719 int retry = 0;
720 vm_prot_t orig_fault_type = fault_type;
722 fs.hardfault = 0;
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);
731 if (fs.m) {
732 *errorp = 0;
733 return(fs.m);
737 * Otherwise take a concurrency hit and do a formal page
738 * fault.
740 fs.shared = vm_shared_fault;
741 fs.first_shared = vm_shared_fault;
742 fs.vp = NULL;
743 lwkt_gettoken(&map->token);
746 * swap_pager_unswapped() needs an exclusive object
748 if (fault_flags & (VM_FAULT_UNSWAP | VM_FAULT_DIRTY)) {
749 fs.first_shared = 0;
752 RetryFault:
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.
766 fs.map = map;
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) {
772 *errorp = result;
773 fs.m = NULL;
774 goto done;
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;
784 fs.first_m = NULL;
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",
789 (u_long)vaddr);
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
796 * management.
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;
808 fs.m = NULL;
809 unlock_things(&fs);
810 goto done2;
812 fs.m = PHYS_TO_VM_PAGE(fakem.phys_addr);
813 vm_page_hold(fs.m);
815 unlock_things(&fs);
816 *errorp = 0;
817 goto done;
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
832 * is set.
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) &&
839 (retry ||
840 fs.first_object->type == OBJT_VNODE ||
841 fs.first_object->type == OBJT_SWAP ||
842 fs.first_object->backing_object)) {
843 *errorp = KERN_FAILURE;
844 unlock_things(&fs);
845 goto done2;
849 * If the entry is wired we cannot change the page protection.
851 if (fs.wired)
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.
868 if (fs.first_shared)
869 vm_object_hold_shared(fs.first_object);
870 else
871 vm_object_hold(fs.first_object);
872 if (fs.vp == NULL)
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
881 * ONLY
883 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
884 result = vm_fault_vpagetable(&fs, &first_pindex,
885 fs.entry->aux.master_pde,
886 fault_type, 1);
887 if (result == KERN_TRY_AGAIN) {
888 vm_object_drop(fs.first_object);
889 ++retry;
890 goto RetryFault;
892 if (result != KERN_SUCCESS) {
893 *errorp = result;
894 fs.m = NULL;
895 goto done;
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
904 * fs->first_object
906 fs.m = NULL;
907 result = vm_fault_object(&fs, first_pindex, fault_type, 1);
909 if (result == KERN_TRY_AGAIN) {
910 vm_object_drop(fs.first_object);
911 ++retry;
912 goto RetryFault;
914 if (result != KERN_SUCCESS) {
915 *errorp = result;
916 fs.m = NULL;
917 goto done;
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);
924 fs.m = NULL;
925 goto done;
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);
945 #if 0
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;
950 #endif
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.
957 unlock_things(&fs);
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).
966 vm_page_hold(fs.m);
967 vm_page_activate(fs.m);
968 if (fault_type & VM_PROT_WRITE)
969 vm_page_dirty(fs.m);
971 if (curthread->td_lwp) {
972 if (fs.hardfault) {
973 curthread->td_lwp->lwp_ru.ru_majflt++;
974 } else {
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; */
985 *errorp = 0;
987 done:
988 if (fs.first_object)
989 vm_object_drop(fs.first_object);
990 done2:
991 lwkt_reltoken(&map->token);
992 return(fs.m);
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
998 * error is returned.
1000 * A held (but not busied) page is returned.
1002 * The passed in object must be held as specified by the shared
1003 * argument.
1005 vm_page_t
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)
1010 int result;
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;
1021 fs.hardfault = 0;
1022 fs.fault_flags = fault_flags;
1023 fs.map = NULL;
1024 fs.shared = vm_shared_fault;
1025 fs.first_shared = *sharedp;
1026 fs.vp = NULL;
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)
1040 RetryFault:
1041 *sharedp = fs.first_shared;
1042 first_pindex = OFF_TO_IDX(offset);
1043 fs.first_object = object;
1044 fs.entry = &entry;
1045 fs.first_prot = fault_type;
1046 fs.wired = 0;
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.
1063 if (fs.vp == NULL)
1064 fs.vp = vnode_pager_lock(fs.first_object);
1066 fs.lookup_still_valid = TRUE;
1067 fs.first_m = NULL;
1068 fs.object = fs.first_object; /* so unlock_and_deallocate works */
1070 #if 0
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,
1075 fault_type, 0);
1076 if (result == KERN_TRY_AGAIN) {
1077 if (fs.first_shared == 0 && *sharedp)
1078 vm_object_upgrade(object);
1079 goto RetryFault;
1081 if (result != KERN_SUCCESS) {
1082 *errorp = result;
1083 return (NULL);
1086 #endif
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
1093 * fs->first_object
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);
1103 goto RetryFault;
1105 if (result != KERN_SUCCESS) {
1106 *errorp = result;
1107 return(NULL);
1110 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
1111 *errorp = KERN_PROTECTION_FAILURE;
1112 unlock_and_deallocate(&fs);
1113 return(NULL);
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.
1120 unlock_things(&fs);
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).
1129 vm_page_hold(fs.m);
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) {
1142 if (fs.hardfault) {
1143 curthread->td_lwp->lwp_ru.ru_majflt++;
1144 } else {
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; */
1156 *errorp = 0;
1157 return(fs.m);
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).
1169 static
1171 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
1172 vpte_t vpte, int fault_type, int allow_nofault)
1174 struct lwbuf *lwb;
1175 struct lwbuf lwb_cache;
1176 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
1177 int result = KERN_SUCCESS;
1178 vpte_t *ptep;
1180 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1181 for (;;) {
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)
1195 break;
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,
1207 allow_nofault);
1208 if (result != KERN_SUCCESS)
1209 return (result);
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));
1219 vpte = *ptep;
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) &&
1231 (vpte & VPTE_RW)) {
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);
1243 lwbuf_free(lwb);
1244 vm_page_flag_set(fs->m, PG_REFERENCED);
1245 vm_page_wakeup(fs->m);
1246 fs->m = NULL;
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.
1280 static
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;
1286 vm_pindex_t pindex;
1287 int error;
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
1307 * table.
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 */
1329 for (;;) {
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,
1339 fs->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
1349 * in that case).
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,
1357 * to pmap it.
1359 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1360 TRUE, &error);
1361 if (error) {
1362 vm_object_pip_wakeup(fs->first_object);
1363 vm_object_chain_release_all(fs->first_object,
1364 fs->object);
1365 if (fs->object != fs->first_object)
1366 vm_object_drop(fs->object);
1367 unlock_things(fs);
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;*/
1372 fs->m = NULL;
1373 return (KERN_TRY_AGAIN);
1375 if (fs->m) {
1377 * The page is busied for us.
1379 * If reactivating a page from PQ_CACHE we may have
1380 * to rate-limit.
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);
1389 fs->m = NULL;
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_and_deallocate(fs);
1396 if (allow_nofault == 0 ||
1397 (curthread->td_flags & TDF_NOFAULT) == 0) {
1398 thread_t td;
1400 vm_wait_pfault();
1401 td = curthread;
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
1412 * can return.
1414 * We can release the spl once we have marked the
1415 * page busy.
1417 if (fs->m->object != &kernel_object) {
1418 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1419 VM_PAGE_BITS_ALL) {
1420 goto readrest;
1422 if (fs->m->flags & PG_RAM) {
1423 if (debug_cluster)
1424 kprintf("R");
1425 vm_page_flag_clear(fs->m, PG_RAM);
1426 goto readrest;
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 &&
1441 fs->first_shared) {
1442 fs->first_shared = 0;
1443 vm_object_pip_wakeup(fs->first_object);
1444 vm_object_chain_release_all(fs->first_object,
1445 fs->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 &&
1452 fs->shared) {
1453 fs->first_shared = 0;
1454 fs->shared = 0;
1455 vm_object_pip_wakeup(fs->first_object);
1456 vm_object_chain_release_all(fs->first_object,
1457 fs->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,
1470 fs->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
1481 * handle the case.
1483 fs->m = NULL;
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,
1494 fs->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) {
1500 thread_t td;
1502 vm_wait_pfault();
1503 td = curthread;
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).
1516 readrest:
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
1521 * a new page.
1523 * Attempt to fault-in the page if there is a chance that the
1524 * pager has it, and potentially fault in additional pages
1525 * at the same time.
1527 * If TRYPAGER is true then fs.m will be non-NULL and busied
1528 * for us.
1530 if (TRYPAGER(fs)) {
1531 int rv;
1532 int seqaccess;
1533 u_char behavior = vm_map_entry_behavior(fs->entry);
1535 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1536 seqaccess = 0;
1537 else
1538 seqaccess = -1;
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
1547 * VM object.
1549 if (fs->object == fs->first_object &&
1550 fs->first_shared) {
1551 vm_page_deactivate(fs->m);
1552 vm_page_wakeup(fs->m);
1553 fs->m = NULL;
1554 fs->first_shared = 0;
1555 vm_object_pip_wakeup(fs->first_object);
1556 vm_object_chain_release_all(fs->first_object,
1557 fs->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 &&
1564 fs->shared) {
1565 vm_page_deactivate(fs->m);
1566 vm_page_wakeup(fs->m);
1567 fs->m = NULL;
1568 fs->first_shared = 0;
1569 fs->shared = 0;
1570 vm_object_pip_wakeup(fs->first_object);
1571 vm_object_chain_release_all(fs->first_object,
1572 fs->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
1588 * changed.
1590 unlock_map(fs);
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
1616 * if moved.
1618 * XXX other code segments do relookups too.
1619 * It's a bad abstraction that needs to be
1620 * fixed/removed.
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);
1632 ++fs->hardfault;
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
1642 * in this page.
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) {
1650 if (curproc) {
1651 kprintf("vm_fault: pager read error, "
1652 "pid %d (%s)\n",
1653 curproc->p_pid,
1654 curproc->p_comm);
1655 } else {
1656 kprintf("vm_fault: pager read error, "
1657 "thread %p (%s)\n",
1658 curthread,
1659 curproc->p_comm);
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);
1676 fs->m = NULL;
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)) {
1689 if (fs->m) {
1690 if (fs->first_shared) {
1691 vm_page_deactivate(fs->m);
1692 vm_page_wakeup(fs->m);
1693 } else {
1694 vnode_pager_freepage(fs->m);
1696 fs->m = NULL;
1698 vm_object_pip_wakeup(fs->first_object);
1699 vm_object_chain_release_all(fs->first_object,
1700 fs->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);
1706 else
1707 return (KERN_PROTECTION_FAILURE);
1708 /* NOT REACHED */
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
1728 * fs->shared.
1730 if ((next_object = fs->object->backing_object) != NULL) {
1731 if (fs->shared)
1732 vm_object_hold_shared(next_object);
1733 else
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) {
1746 #if 0
1747 if (fs->first_object->backing_object !=
1748 fs->object) {
1749 vm_object_hold(fs->first_object->backing_object);
1751 #endif
1752 vm_object_chain_release_all(
1753 fs->first_object->backing_object,
1754 fs->object);
1755 #if 0
1756 if (fs->first_object->backing_object !=
1757 fs->object) {
1758 vm_object_drop(fs->first_object->backing_object);
1760 #endif
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;
1767 fs->first_m = NULL;
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
1790 * is held.]
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
1798 * top-level object.
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.
1818 if (
1820 * Must be holding exclusive locks
1822 fs->first_shared == 0 &&
1823 fs->shared == 0 &&
1825 * Map, if present, has not changed
1827 (fs->map == NULL ||
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 ||
1855 fs->map == NULL ||
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
1867 * rename.
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,
1873 first_pindex);
1874 vm_page_free(fs->first_m);
1875 fs->first_m = fs->m;
1876 fs->m = NULL;
1877 mycpu->gd_cnt.v_cow_optim++;
1878 } else {
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.
1906 if (fs->m)
1907 release_page(fs);
1910 * We intend to revert to first_object, undo the
1911 * chain lock through to that.
1913 #if 0
1914 if (fs->first_object->backing_object != fs->object)
1915 vm_object_hold(fs->first_object->backing_object);
1916 #endif
1917 vm_object_chain_release_all(
1918 fs->first_object->backing_object,
1919 fs->object);
1920 #if 0
1921 if (fs->first_object->backing_object != fs->object)
1922 vm_object_drop(fs->first_object->backing_object);
1923 #endif
1926 * fs->object != fs->first_object due to above
1927 * conditional
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;
1939 } else {
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) {
1962 release_page(fs);
1963 vm_object_pip_wakeup(fs->first_object);
1964 vm_object_chain_release_all(fs->first_object,
1965 fs->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()
1976 * calls later.
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)
2044 return -1;
2046 for (i = 0; i < npages; i++) {
2047 // XXX error handling
2048 ma[i] = vm_fault_page_quick(start + (i * PAGE_SIZE),
2049 prot,
2050 &error);
2053 return npages;
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.
2063 * No requirements.
2066 vm_fault_wire(vm_map_t map, vm_map_entry_t entry,
2067 boolean_t user_wire, int kmflags)
2069 boolean_t fictitious;
2070 vm_offset_t start;
2071 vm_offset_t end;
2072 vm_offset_t va;
2073 vm_paddr_t pa;
2074 vm_page_t m;
2075 pmap_t pmap;
2076 int rv;
2077 int wire_prot;
2078 int fault_flags;
2080 lwkt_gettoken(&map->token);
2082 if (user_wire) {
2083 wire_prot = VM_PROT_READ;
2084 fault_flags = VM_FAULT_USER_WIRE;
2085 } else {
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;
2094 end = entry->end;
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));
2101 break;
2102 case VM_MAPTYPE_UKSMAP:
2103 fictitious = TRUE;
2104 break;
2105 default:
2106 fictitious = FALSE;
2107 break;
2110 if (entry->eflags & MAP_ENTRY_KSTACK)
2111 start += PAGE_SIZE;
2112 map->timestamp++;
2113 vm_map_unlock(map);
2116 * We simulate a fault to get the page and enter it in the physical
2117 * map.
2119 for (va = start; va < end; va += PAGE_SIZE) {
2120 rv = vm_fault(map, va, wire_prot, fault_flags);
2121 if (rv) {
2122 while (va > start) {
2123 va -= PAGE_SIZE;
2124 if ((pa = pmap_extract(pmap, va)) == 0)
2125 continue;
2126 pmap_change_wiring(pmap, va, FALSE, entry);
2127 if (!fictitious) {
2128 m = PHYS_TO_VM_PAGE(pa);
2129 vm_page_busy_wait(m, FALSE, "vmwrpg");
2130 vm_page_unwire(m, 1);
2131 vm_page_wakeup(m);
2134 goto done;
2137 rv = KERN_SUCCESS;
2138 done:
2139 vm_map_lock(map);
2140 lwkt_reltoken(&map->token);
2141 return (rv);
2145 * Unwire a range of virtual addresses in a map. The map should be
2146 * locked.
2148 void
2149 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
2151 boolean_t fictitious;
2152 vm_offset_t start;
2153 vm_offset_t end;
2154 vm_offset_t va;
2155 vm_paddr_t pa;
2156 vm_page_t m;
2157 pmap_t pmap;
2159 lwkt_gettoken(&map->token);
2161 pmap = vm_map_pmap(map);
2162 start = entry->start;
2163 end = entry->end;
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)
2168 start += PAGE_SIZE;
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);
2176 if (pa != 0) {
2177 pmap_change_wiring(pmap, va, FALSE, entry);
2178 if (!fictitious) {
2179 m = PHYS_TO_VM_PAGE(pa);
2180 vm_page_busy_wait(m, FALSE, "vmwupg");
2181 vm_page_unwire(m, 1);
2182 vm_page_wakeup(m);
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
2201 void
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;
2209 vm_prot_t prot;
2210 vm_offset_t vaddr;
2211 vm_page_t dst_m;
2212 vm_page_t src_m;
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
2229 * object.
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
2240 do {
2241 dst_m = vm_page_alloc(dst_object,
2242 OFF_TO_IDX(dst_offset),
2243 VM_ALLOC_NORMAL);
2244 if (dst_m == NULL) {
2245 vm_wait(0);
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
2252 * memory.)
2254 src_m = vm_page_lookup(src_object,
2255 OFF_TO_IDX(dst_offset + src_offset));
2256 if (src_m == NULL)
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);
2277 #if 0
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.
2284 * Inputs:
2285 * m, rbehind, rahead
2287 * Outputs:
2288 * marray (array of vm_page_t), reqpage (index of requested page)
2290 * Return value:
2291 * number of pages in marray
2293 static int
2294 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2295 vm_page_t *marray, int *reqpage)
2297 int i,j;
2298 vm_object_t object;
2299 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2300 vm_page_t rtm;
2301 int cbehind, cahead;
2303 object = m->object;
2304 pindex = m->pindex;
2307 * we don't fault-ahead for device pager
2309 if ((object->type == OBJT_DEVICE) ||
2310 (object->type == OBJT_MGTDEVICE)) {
2311 *reqpage = 0;
2312 marray[0] = m;
2313 return 1;
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 */
2321 return 0;
2324 if ((cbehind == 0) && (cahead == 0)) {
2325 *reqpage = 0;
2326 marray[0] = m;
2327 return 1;
2330 if (rahead > cahead) {
2331 rahead = cahead;
2334 if (rbehind > cbehind) {
2335 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()) {
2345 marray[0] = m;
2346 *reqpage = 0;
2347 return 1;
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.
2357 if (pindex > 0) {
2358 if (rbehind > pindex) {
2359 rbehind = pindex;
2360 startpindex = 0;
2361 } else {
2362 startpindex = pindex - rbehind;
2365 vm_object_hold(object);
2366 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2367 if (vm_page_lookup(object, tpindex - 1))
2368 break;
2371 i = 0;
2372 while (tpindex < pindex) {
2373 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2374 VM_ALLOC_NULL_OK);
2375 if (rtm == NULL) {
2376 for (j = 0; j < i; j++) {
2377 vm_page_free(marray[j]);
2379 vm_object_drop(object);
2380 marray[0] = m;
2381 *reqpage = 0;
2382 return 1;
2384 marray[i] = rtm;
2385 ++i;
2386 ++tpindex;
2388 vm_object_drop(object);
2389 } else {
2390 i = 0;
2394 * Assign requested page
2396 marray[i] = m;
2397 *reqpage = i;
2398 ++i;
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))
2411 break;
2412 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2413 VM_ALLOC_NULL_OK);
2414 if (rtm == NULL)
2415 break;
2416 marray[i] = rtm;
2417 ++i;
2418 ++tpindex;
2420 vm_object_drop(object);
2422 return (i);
2425 #endif
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.
2472 static void
2473 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2475 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2476 if (m->dirty == 0)
2477 vm_page_flag_set(m, PG_NOSYNC);
2478 } else {
2479 vm_page_flag_clear(m, PG_NOSYNC);
2483 static void
2484 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2485 int fault_flags)
2487 struct lwp *lp;
2488 vm_page_t m;
2489 vm_offset_t addr;
2490 vm_pindex_t index;
2491 vm_pindex_t pindex;
2492 vm_object_t object;
2493 int pprot;
2494 int i;
2495 int noneg;
2496 int nopos;
2497 int maxpages;
2500 * Get stable max count value, disabled if set to 0
2502 maxpages = vm_prefault_pages;
2503 cpu_ccfence();
2504 if (maxpages <= 0)
2505 return;
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)
2512 return;
2513 lp = curthread->td_lwp;
2514 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2515 return;
2518 * Limit pre-fault count to 1024 pages.
2520 if (maxpages > 1024)
2521 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);
2529 noneg = 0;
2530 nopos = 0;
2531 for (i = 0; i < maxpages; ++i) {
2532 vm_object_t lobject;
2533 vm_object_t nobject;
2534 int allocated = 0;
2535 int error;
2538 * This can eat a lot of time on a heavily contended
2539 * machine so yield on the tick if needed.
2541 if ((i & 7) == 7)
2542 lwkt_yield();
2545 * Calculate the page to pre-fault, stopping the scan in
2546 * each direction separately if the limit is reached.
2548 if (i & 1) {
2549 if (noneg)
2550 continue;
2551 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2552 } else {
2553 if (nopos)
2554 continue;
2555 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2557 if (addr < entry->start) {
2558 noneg = 1;
2559 if (noneg && nopos)
2560 break;
2561 continue;
2563 if (addr >= entry->end) {
2564 nopos = 1;
2565 if (noneg && nopos)
2566 break;
2567 continue;
2571 * Skip pages already mapped, and stop scanning in that
2572 * direction. When the scan terminates in both directions
2573 * we are done.
2575 if (pmap_prefault_ok(pmap, addr) == 0) {
2576 if (i & 1)
2577 noneg = 1;
2578 else
2579 nopos = 1;
2580 if (noneg && nopos)
2581 break;
2582 continue;
2586 * Follow the VM object chain to obtain the page to be mapped
2587 * into the pmap.
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;
2599 lobject = object;
2600 pindex = index;
2601 pprot = prot;
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)
2609 break;
2610 if (lobject->backing_object == NULL) {
2611 if (vm_fast_fault == 0)
2612 break;
2613 if ((prot & VM_PROT_WRITE) == 0 ||
2614 vm_page_count_min(0)) {
2615 break;
2619 * NOTE: Allocated from base object
2621 m = vm_page_alloc(object, index,
2622 VM_ALLOC_NORMAL |
2623 VM_ALLOC_ZERO |
2624 VM_ALLOC_USE_GD |
2625 VM_ALLOC_NULL_OK);
2626 if (m == NULL)
2627 break;
2628 allocated = 1;
2629 pprot = prot;
2630 /* lobject = object .. not needed */
2631 break;
2633 if (lobject->backing_object_offset & PAGE_MASK)
2634 break;
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);
2643 lobject = nobject;
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.
2655 if (m == NULL) {
2656 if (lobject != object) {
2657 #if 0
2658 if (object->backing_object != lobject)
2659 vm_object_hold(object->backing_object);
2660 #endif
2661 vm_object_chain_release_all(
2662 object->backing_object, lobject);
2663 #if 0
2664 if (object->backing_object != lobject)
2665 vm_object_drop(object->backing_object);
2666 #endif
2667 vm_object_drop(lobject);
2669 break;
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) {
2692 #if 0
2693 if (object->backing_object != lobject)
2694 vm_object_hold(object->backing_object);
2695 #endif
2696 vm_object_chain_release_all(object->backing_object,
2697 lobject);
2698 #if 0
2699 if (object->backing_object != lobject)
2700 vm_object_drop(object->backing_object);
2701 #endif
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).
2711 if (allocated) {
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) {
2733 vm_page_dirty(m);
2734 /*XXX*/
2735 swap_pager_unswapped(m);
2738 vm_page_wakeup(m);
2739 } else if (error) {
2740 /* couldn't busy page, no wakeup */
2741 } else if (
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) {
2754 vm_page_dirty(m);
2755 /*XXX*/
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;
2765 vm_page_wakeup(m);
2766 } else {
2767 vm_page_wakeup(m);
2770 vm_object_chain_release(object);
2771 vm_object_drop(object);
2775 * Object can be held shared
2777 static void
2778 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2779 vm_map_entry_t entry, int prot, int fault_flags)
2781 struct lwp *lp;
2782 vm_page_t m;
2783 vm_offset_t addr;
2784 vm_pindex_t pindex;
2785 vm_object_t object;
2786 int i;
2787 int noneg;
2788 int nopos;
2789 int maxpages;
2792 * Get stable max count value, disabled if set to 0
2794 maxpages = vm_prefault_pages;
2795 cpu_ccfence();
2796 if (maxpages <= 0)
2797 return;
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)
2804 return;
2805 lp = curthread->td_lwp;
2806 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2807 return;
2808 object = entry->object.vm_object;
2809 if (object->backing_object != NULL)
2810 return;
2811 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2814 * Limit pre-fault count to 1024 pages.
2816 if (maxpages > 1024)
2817 maxpages = 1024;
2819 noneg = 0;
2820 nopos = 0;
2821 for (i = 0; i < maxpages; ++i) {
2822 int error;
2825 * Calculate the page to pre-fault, stopping the scan in
2826 * each direction separately if the limit is reached.
2828 if (i & 1) {
2829 if (noneg)
2830 continue;
2831 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2832 } else {
2833 if (nopos)
2834 continue;
2835 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2837 if (addr < entry->start) {
2838 noneg = 1;
2839 if (noneg && nopos)
2840 break;
2841 continue;
2843 if (addr >= entry->end) {
2844 nopos = 1;
2845 if (noneg && nopos)
2846 break;
2847 continue;
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
2856 * we stop here.
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)
2865 break;
2868 * Skip pages already mapped, and stop scanning in that
2869 * direction. When the scan terminates in both directions
2870 * we are done.
2872 if (pmap_prefault_ok(pmap, addr) == 0) {
2873 vm_page_wakeup(m);
2874 if (i & 1)
2875 noneg = 1;
2876 else
2877 nopos = 1;
2878 if (noneg && nopos)
2879 break;
2880 continue;
2884 * Stop if the page cannot be trivially entered into the
2885 * pmap.
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))) {
2892 vm_page_wakeup(m);
2893 break;
2897 * Enter the page into the pmap. The object might be held
2898 * shared so we can't do any (serious) modifying operation
2899 * on it.
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) {
2907 vm_page_dirty(m);
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;
2916 vm_page_wakeup(m);