kernel - Implement segment pmap optimizations for x86-64
[dragonfly.git] / sys / vm / vm_fault.c
blob6fcc5decee720a8b4b67222642d429170382f882
1 /*
2 * (MPSAFE)
4 * Copyright (c) 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 * Copyright (c) 1994 John S. Dyson
7 * All rights reserved.
8 * Copyright (c) 1994 David Greenman
9 * All rights reserved.
12 * This code is derived from software contributed to Berkeley by
13 * The Mach Operating System project at Carnegie-Mellon University.
15 * Redistribution and use in source and binary forms, with or without
16 * modification, are permitted provided that the following conditions
17 * are met:
18 * 1. Redistributions of source code must retain the above copyright
19 * notice, this list of conditions and the following disclaimer.
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 * 3. All advertising materials mentioning features or use of this software
24 * must display the following acknowledgement:
25 * This product includes software developed by the University of
26 * California, Berkeley and its contributors.
27 * 4. Neither the name of the University nor the names of its contributors
28 * may be used to endorse or promote products derived from this software
29 * without specific prior written permission.
31 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
32 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
33 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
34 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
35 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
39 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
40 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * SUCH DAMAGE.
43 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
46 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
47 * All rights reserved.
49 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
51 * Permission to use, copy, modify and distribute this software and
52 * its documentation is hereby granted, provided that both the copyright
53 * notice and this permission notice appear in all copies of the
54 * software, derivative works or modified versions, and any portions
55 * thereof, and that both notices appear in supporting documentation.
57 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
58 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
59 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
61 * Carnegie Mellon requests users of this software to return to
63 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
64 * School of Computer Science
65 * Carnegie Mellon University
66 * Pittsburgh PA 15213-3890
68 * any improvements or extensions that they make and grant Carnegie the
69 * rights to redistribute these changes.
71 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
72 * $DragonFly: src/sys/vm/vm_fault.c,v 1.47 2008/07/01 02:02:56 dillon Exp $
76 * Page fault handling module.
79 #include <sys/param.h>
80 #include <sys/systm.h>
81 #include <sys/kernel.h>
82 #include <sys/proc.h>
83 #include <sys/vnode.h>
84 #include <sys/resourcevar.h>
85 #include <sys/vmmeter.h>
86 #include <sys/vkernel.h>
87 #include <sys/lock.h>
88 #include <sys/sysctl.h>
90 #include <cpu/lwbuf.h>
92 #include <vm/vm.h>
93 #include <vm/vm_param.h>
94 #include <vm/pmap.h>
95 #include <vm/vm_map.h>
96 #include <vm/vm_object.h>
97 #include <vm/vm_page.h>
98 #include <vm/vm_pageout.h>
99 #include <vm/vm_kern.h>
100 #include <vm/vm_pager.h>
101 #include <vm/vnode_pager.h>
102 #include <vm/vm_extern.h>
104 #include <sys/thread2.h>
105 #include <vm/vm_page2.h>
107 struct faultstate {
108 vm_page_t m;
109 vm_object_t object;
110 vm_pindex_t pindex;
111 vm_prot_t prot;
112 vm_page_t first_m;
113 vm_object_t first_object;
114 vm_prot_t first_prot;
115 vm_map_t map;
116 vm_map_entry_t entry;
117 int lookup_still_valid;
118 int hardfault;
119 int fault_flags;
120 int map_generation;
121 boolean_t wired;
122 struct vnode *vp;
125 static int debug_cluster = 0;
126 SYSCTL_INT(_vm, OID_AUTO, debug_cluster, CTLFLAG_RW, &debug_cluster, 0, "");
127 static int vm_shared_fault = 1;
128 SYSCTL_INT(_vm, OID_AUTO, shared_fault, CTLFLAG_RW, &vm_shared_fault, 0,
129 "Allow shared token on vm_object");
130 static long vm_shared_hit = 0;
131 SYSCTL_LONG(_vm, OID_AUTO, shared_hit, CTLFLAG_RW, &vm_shared_hit, 0,
132 "Successful shared faults");
133 static long vm_shared_miss = 0;
134 SYSCTL_LONG(_vm, OID_AUTO, shared_miss, CTLFLAG_RW, &vm_shared_miss, 0,
135 "Successful shared faults");
137 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
138 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t, int);
139 #if 0
140 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
141 #endif
142 static void vm_set_nosync(vm_page_t m, vm_map_entry_t entry);
143 static void vm_prefault(pmap_t pmap, vm_offset_t addra,
144 vm_map_entry_t entry, int prot, int fault_flags);
145 static void vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
146 vm_map_entry_t entry, int prot, int fault_flags);
148 static __inline void
149 release_page(struct faultstate *fs)
151 vm_page_deactivate(fs->m);
152 vm_page_wakeup(fs->m);
153 fs->m = NULL;
157 * NOTE: Once unlocked any cached fs->entry becomes invalid, any reuse
158 * requires relocking and then checking the timestamp.
160 * NOTE: vm_map_lock_read() does not bump fs->map->timestamp so we do
161 * not have to update fs->map_generation here.
163 * NOTE: This function can fail due to a deadlock against the caller's
164 * holding of a vm_page BUSY.
166 static __inline int
167 relock_map(struct faultstate *fs)
169 int error;
171 if (fs->lookup_still_valid == FALSE && fs->map) {
172 error = vm_map_lock_read_to(fs->map);
173 if (error == 0)
174 fs->lookup_still_valid = TRUE;
175 } else {
176 error = 0;
178 return error;
181 static __inline void
182 unlock_map(struct faultstate *fs)
184 if (fs->lookup_still_valid && fs->map) {
185 vm_map_lookup_done(fs->map, fs->entry, 0);
186 fs->lookup_still_valid = FALSE;
191 * Clean up after a successful call to vm_fault_object() so another call
192 * to vm_fault_object() can be made.
194 static void
195 _cleanup_successful_fault(struct faultstate *fs, int relock)
197 if (fs->object != fs->first_object) {
198 vm_page_free(fs->first_m);
199 vm_object_pip_wakeup(fs->object);
200 fs->first_m = NULL;
202 fs->object = fs->first_object;
203 if (relock && fs->lookup_still_valid == FALSE) {
204 if (fs->map)
205 vm_map_lock_read(fs->map);
206 fs->lookup_still_valid = TRUE;
210 static void
211 _unlock_things(struct faultstate *fs, int dealloc)
213 _cleanup_successful_fault(fs, 0);
214 if (dealloc) {
215 /*vm_object_deallocate(fs->first_object);*/
216 /*fs->first_object = NULL; drop used later on */
218 unlock_map(fs);
219 if (fs->vp != NULL) {
220 vput(fs->vp);
221 fs->vp = NULL;
225 #define unlock_things(fs) _unlock_things(fs, 0)
226 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
227 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
230 * TRYPAGER
232 * Determine if the pager for the current object *might* contain the page.
234 * We only need to try the pager if this is not a default object (default
235 * objects are zero-fill and have no real pager), and if we are not taking
236 * a wiring fault or if the FS entry is wired.
238 #define TRYPAGER(fs) \
239 (fs->object->type != OBJT_DEFAULT && \
240 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
243 * vm_fault:
245 * Handle a page fault occuring at the given address, requiring the given
246 * permissions, in the map specified. If successful, the page is inserted
247 * into the associated physical map.
249 * NOTE: The given address should be truncated to the proper page address.
251 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
252 * a standard error specifying why the fault is fatal is returned.
254 * The map in question must be referenced, and remains so.
255 * The caller may hold no locks.
256 * No other requirements.
259 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
261 int result;
262 vm_pindex_t first_pindex;
263 struct faultstate fs;
264 struct lwp *lp;
265 int growstack;
267 vm_page_pcpu_cache();
268 fs.hardfault = 0;
269 fs.fault_flags = fault_flags;
270 fs.vp = NULL;
271 growstack = 1;
273 if ((lp = curthread->td_lwp) != NULL)
274 lp->lwp_flags |= LWP_PAGING;
276 lwkt_gettoken(&map->token);
278 RetryFault:
280 * Find the vm_map_entry representing the backing store and resolve
281 * the top level object and page index. This may have the side
282 * effect of executing a copy-on-write on the map entry and/or
283 * creating a shadow object, but will not COW any actual VM pages.
285 * On success fs.map is left read-locked and various other fields
286 * are initialized but not otherwise referenced or locked.
288 * NOTE! vm_map_lookup will try to upgrade the fault_type to
289 * VM_FAULT_WRITE if the map entry is a virtual page table and also
290 * writable, so we can set the 'A'accessed bit in the virtual page
291 * table entry.
293 fs.map = map;
294 result = vm_map_lookup(&fs.map, vaddr, fault_type,
295 &fs.entry, &fs.first_object,
296 &first_pindex, &fs.first_prot, &fs.wired);
299 * If the lookup failed or the map protections are incompatible,
300 * the fault generally fails. However, if the caller is trying
301 * to do a user wiring we have more work to do.
303 if (result != KERN_SUCCESS) {
304 if (result != KERN_PROTECTION_FAILURE ||
305 (fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
307 if (result == KERN_INVALID_ADDRESS && growstack &&
308 map != &kernel_map && curproc != NULL) {
309 result = vm_map_growstack(curproc, vaddr);
310 if (result == KERN_SUCCESS) {
311 growstack = 0;
312 goto RetryFault;
314 result = KERN_FAILURE;
316 goto done;
320 * If we are user-wiring a r/w segment, and it is COW, then
321 * we need to do the COW operation. Note that we don't
322 * currently COW RO sections now, because it is NOT desirable
323 * to COW .text. We simply keep .text from ever being COW'ed
324 * and take the heat that one cannot debug wired .text sections.
326 result = vm_map_lookup(&fs.map, vaddr,
327 VM_PROT_READ|VM_PROT_WRITE|
328 VM_PROT_OVERRIDE_WRITE,
329 &fs.entry, &fs.first_object,
330 &first_pindex, &fs.first_prot,
331 &fs.wired);
332 if (result != KERN_SUCCESS) {
333 result = KERN_FAILURE;
334 goto done;
338 * If we don't COW now, on a user wire, the user will never
339 * be able to write to the mapping. If we don't make this
340 * restriction, the bookkeeping would be nearly impossible.
342 * XXX We have a shared lock, this will have a MP race but
343 * I don't see how it can hurt anything.
345 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
346 fs.entry->max_protection &= ~VM_PROT_WRITE;
350 * fs.map is read-locked
352 * Misc checks. Save the map generation number to detect races.
354 fs.map_generation = fs.map->timestamp;
356 if (fs.entry->eflags & (MAP_ENTRY_NOFAULT | MAP_ENTRY_KSTACK)) {
357 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
358 panic("vm_fault: fault on nofault entry, addr: %p",
359 (void *)vaddr);
361 if ((fs.entry->eflags & MAP_ENTRY_KSTACK) &&
362 vaddr >= fs.entry->start &&
363 vaddr < fs.entry->start + PAGE_SIZE) {
364 panic("vm_fault: fault on stack guard, addr: %p",
365 (void *)vaddr);
370 * A system map entry may return a NULL object. No object means
371 * no pager means an unrecoverable kernel fault.
373 if (fs.first_object == NULL) {
374 panic("vm_fault: unrecoverable fault at %p in entry %p",
375 (void *)vaddr, fs.entry);
379 * Attempt to shortcut the fault if the lookup returns a
380 * terminal object and the page is present. This allows us
381 * to obtain a shared token on the object instead of an exclusive
382 * token, which theoretically should allow concurrent faults.
384 * We cannot acquire a shared token on kernel_map, at least not
385 * on i386, because the i386 pmap code uses the kernel_object for
386 * its page table page management, resulting in a shared->exclusive
387 * sequence which will deadlock. This will not happen normally
388 * anyway, except on well cached pageable kmem (like pipe buffers),
389 * so it should not impact performance.
391 if (vm_shared_fault &&
392 fs.first_object->backing_object == NULL &&
393 fs.entry->maptype == VM_MAPTYPE_NORMAL &&
394 fs.map != &kernel_map) {
395 int error;
396 vm_object_hold_shared(fs.first_object);
397 /*fs.vp = vnode_pager_lock(fs.first_object);*/
398 fs.m = vm_page_lookup_busy_try(fs.first_object,
399 first_pindex,
400 TRUE, &error);
401 if (error == 0 && fs.m) {
403 * Activate the page and figure out if we can
404 * short-cut a quick mapping.
406 * WARNING! We cannot call swap_pager_unswapped()
407 * with a shared token! Note that we
408 * have to test fs.first_prot here.
410 vm_page_activate(fs.m);
411 if (fs.m->valid == VM_PAGE_BITS_ALL &&
412 ((fs.m->flags & PG_SWAPPED) == 0 ||
413 (fs.first_prot & VM_PROT_WRITE) == 0 ||
414 (fs.fault_flags & VM_FAULT_DIRTY) == 0)) {
415 fs.lookup_still_valid = TRUE;
416 fs.first_m = NULL;
417 fs.object = fs.first_object;
418 fs.prot = fs.first_prot;
419 if (fs.wired)
420 fault_type = fs.first_prot;
421 if (fs.prot & VM_PROT_WRITE) {
422 vm_object_set_writeable_dirty(
423 fs.m->object);
424 vm_set_nosync(fs.m, fs.entry);
425 if (fs.fault_flags & VM_FAULT_DIRTY) {
426 vm_page_dirty(fs.m);
427 /*XXX*/
428 swap_pager_unswapped(fs.m);
431 result = KERN_SUCCESS;
432 fault_flags |= VM_FAULT_BURST_QUICK;
433 fault_flags &= ~VM_FAULT_BURST;
434 ++vm_shared_hit;
435 goto quick;
437 vm_page_wakeup(fs.m);
438 fs.m = NULL;
440 vm_object_drop(fs.first_object); /* XXX drop on shared tok?*/
442 ++vm_shared_miss;
445 * Bump the paging-in-progress count to prevent size changes (e.g.
446 * truncation operations) during I/O. This must be done after
447 * obtaining the vnode lock in order to avoid possible deadlocks.
449 vm_object_hold(fs.first_object);
450 if (fs.vp == NULL)
451 fs.vp = vnode_pager_lock(fs.first_object);
453 fs.lookup_still_valid = TRUE;
454 fs.first_m = NULL;
455 fs.object = fs.first_object; /* so unlock_and_deallocate works */
458 * If the entry is wired we cannot change the page protection.
460 if (fs.wired)
461 fault_type = fs.first_prot;
464 * The page we want is at (first_object, first_pindex), but if the
465 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
466 * page table to figure out the actual pindex.
468 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
469 * ONLY
471 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
472 result = vm_fault_vpagetable(&fs, &first_pindex,
473 fs.entry->aux.master_pde,
474 fault_type);
475 if (result == KERN_TRY_AGAIN) {
476 vm_object_drop(fs.first_object);
477 goto RetryFault;
479 if (result != KERN_SUCCESS)
480 goto done;
484 * Now we have the actual (object, pindex), fault in the page. If
485 * vm_fault_object() fails it will unlock and deallocate the FS
486 * data. If it succeeds everything remains locked and fs->object
487 * will have an additional PIP count if it is not equal to
488 * fs->first_object
490 * vm_fault_object will set fs->prot for the pmap operation. It is
491 * allowed to set VM_PROT_WRITE if fault_type == VM_PROT_READ if the
492 * page can be safely written. However, it will force a read-only
493 * mapping for a read fault if the memory is managed by a virtual
494 * page table.
496 /* BEFORE */
497 result = vm_fault_object(&fs, first_pindex, fault_type);
499 if (result == KERN_TRY_AGAIN) {
500 vm_object_drop(fs.first_object);
501 goto RetryFault;
503 if (result != KERN_SUCCESS)
504 goto done;
506 quick:
508 * On success vm_fault_object() does not unlock or deallocate, and fs.m
509 * will contain a busied page.
511 * Enter the page into the pmap and do pmap-related adjustments.
513 vm_page_flag_set(fs.m, PG_REFERENCED);
514 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired, fs.entry);
515 mycpu->gd_cnt.v_vm_faults++;
516 if (curthread->td_lwp)
517 ++curthread->td_lwp->lwp_ru.ru_minflt;
519 /*KKASSERT(fs.m->queue == PQ_NONE); page-in op may deactivate page */
520 KKASSERT(fs.m->flags & PG_BUSY);
523 * If the page is not wired down, then put it where the pageout daemon
524 * can find it.
526 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
527 if (fs.wired)
528 vm_page_wire(fs.m);
529 else
530 vm_page_unwire(fs.m, 1);
531 } else {
532 vm_page_activate(fs.m);
534 vm_page_wakeup(fs.m);
537 * Burst in a few more pages if possible. The fs.map should still
538 * be locked. To avoid interlocking against a vnode->getblk
539 * operation we had to be sure to unbusy our primary vm_page above
540 * first.
542 if (fault_flags & VM_FAULT_BURST) {
543 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
544 && fs.wired == 0) {
545 vm_prefault(fs.map->pmap, vaddr,
546 fs.entry, fs.prot, fault_flags);
549 if (fault_flags & VM_FAULT_BURST_QUICK) {
550 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0
551 && fs.wired == 0) {
552 vm_prefault_quick(fs.map->pmap, vaddr,
553 fs.entry, fs.prot, fault_flags);
558 * Unlock everything, and return
560 unlock_things(&fs);
562 if (curthread->td_lwp) {
563 if (fs.hardfault) {
564 curthread->td_lwp->lwp_ru.ru_majflt++;
565 } else {
566 curthread->td_lwp->lwp_ru.ru_minflt++;
570 /*vm_object_deallocate(fs.first_object);*/
571 /*fs.m = NULL; */
572 /*fs.first_object = NULL; must still drop later */
574 result = KERN_SUCCESS;
575 done:
576 if (fs.first_object)
577 vm_object_drop(fs.first_object);
578 lwkt_reltoken(&map->token);
579 if (lp)
580 lp->lwp_flags &= ~LWP_PAGING;
581 return (result);
585 * Fault in the specified virtual address in the current process map,
586 * returning a held VM page or NULL. See vm_fault_page() for more
587 * information.
589 * No requirements.
591 vm_page_t
592 vm_fault_page_quick(vm_offset_t va, vm_prot_t fault_type, int *errorp)
594 struct lwp *lp = curthread->td_lwp;
595 vm_page_t m;
597 m = vm_fault_page(&lp->lwp_vmspace->vm_map, va,
598 fault_type, VM_FAULT_NORMAL, errorp);
599 return(m);
603 * Fault in the specified virtual address in the specified map, doing all
604 * necessary manipulation of the object store and all necessary I/O. Return
605 * a held VM page or NULL, and set *errorp. The related pmap is not
606 * updated.
608 * The returned page will be properly dirtied if VM_PROT_WRITE was specified,
609 * and marked PG_REFERENCED as well.
611 * If the page cannot be faulted writable and VM_PROT_WRITE was specified, an
612 * error will be returned.
614 * No requirements.
616 vm_page_t
617 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
618 int fault_flags, int *errorp)
620 vm_pindex_t first_pindex;
621 struct faultstate fs;
622 int result;
623 vm_prot_t orig_fault_type = fault_type;
625 fs.hardfault = 0;
626 fs.fault_flags = fault_flags;
627 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
629 lwkt_gettoken(&map->token);
631 RetryFault:
633 * Find the vm_map_entry representing the backing store and resolve
634 * the top level object and page index. This may have the side
635 * effect of executing a copy-on-write on the map entry and/or
636 * creating a shadow object, but will not COW any actual VM pages.
638 * On success fs.map is left read-locked and various other fields
639 * are initialized but not otherwise referenced or locked.
641 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
642 * if the map entry is a virtual page table and also writable,
643 * so we can set the 'A'accessed bit in the virtual page table entry.
645 fs.map = map;
646 result = vm_map_lookup(&fs.map, vaddr, fault_type,
647 &fs.entry, &fs.first_object,
648 &first_pindex, &fs.first_prot, &fs.wired);
650 if (result != KERN_SUCCESS) {
651 *errorp = result;
652 fs.m = NULL;
653 goto done;
657 * fs.map is read-locked
659 * Misc checks. Save the map generation number to detect races.
661 fs.map_generation = fs.map->timestamp;
663 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
664 panic("vm_fault: fault on nofault entry, addr: %lx",
665 (u_long)vaddr);
669 * A system map entry may return a NULL object. No object means
670 * no pager means an unrecoverable kernel fault.
672 if (fs.first_object == NULL) {
673 panic("vm_fault: unrecoverable fault at %p in entry %p",
674 (void *)vaddr, fs.entry);
678 * Make a reference to this object to prevent its disposal while we
679 * are messing with it. Once we have the reference, the map is free
680 * to be diddled. Since objects reference their shadows (and copies),
681 * they will stay around as well.
683 * The reference should also prevent an unexpected collapse of the
684 * parent that might move pages from the current object into the
685 * parent unexpectedly, resulting in corruption.
687 * Bump the paging-in-progress count to prevent size changes (e.g.
688 * truncation operations) during I/O. This must be done after
689 * obtaining the vnode lock in order to avoid possible deadlocks.
691 vm_object_hold(fs.first_object);
692 fs.vp = vnode_pager_lock(fs.first_object);
694 fs.lookup_still_valid = TRUE;
695 fs.first_m = NULL;
696 fs.object = fs.first_object; /* so unlock_and_deallocate works */
699 * If the entry is wired we cannot change the page protection.
701 if (fs.wired)
702 fault_type = fs.first_prot;
705 * The page we want is at (first_object, first_pindex), but if the
706 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
707 * page table to figure out the actual pindex.
709 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
710 * ONLY
712 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
713 result = vm_fault_vpagetable(&fs, &first_pindex,
714 fs.entry->aux.master_pde,
715 fault_type);
716 if (result == KERN_TRY_AGAIN) {
717 vm_object_drop(fs.first_object);
718 goto RetryFault;
720 if (result != KERN_SUCCESS) {
721 *errorp = result;
722 fs.m = NULL;
723 goto done;
728 * Now we have the actual (object, pindex), fault in the page. If
729 * vm_fault_object() fails it will unlock and deallocate the FS
730 * data. If it succeeds everything remains locked and fs->object
731 * will have an additinal PIP count if it is not equal to
732 * fs->first_object
734 fs.m = NULL;
735 result = vm_fault_object(&fs, first_pindex, fault_type);
737 if (result == KERN_TRY_AGAIN) {
738 vm_object_drop(fs.first_object);
739 goto RetryFault;
741 if (result != KERN_SUCCESS) {
742 *errorp = result;
743 fs.m = NULL;
744 goto done;
747 if ((orig_fault_type & VM_PROT_WRITE) &&
748 (fs.prot & VM_PROT_WRITE) == 0) {
749 *errorp = KERN_PROTECTION_FAILURE;
750 unlock_and_deallocate(&fs);
751 fs.m = NULL;
752 goto done;
756 * DO NOT UPDATE THE PMAP!!! This function may be called for
757 * a pmap unrelated to the current process pmap, in which case
758 * the current cpu core will not be listed in the pmap's pm_active
759 * mask. Thus invalidation interlocks will fail to work properly.
761 * (for example, 'ps' uses procfs to read program arguments from
762 * each process's stack).
764 * In addition to the above this function will be called to acquire
765 * a page that might already be faulted in, re-faulting it
766 * continuously is a waste of time.
768 * XXX could this have been the cause of our random seg-fault
769 * issues? procfs accesses user stacks.
771 vm_page_flag_set(fs.m, PG_REFERENCED);
772 #if 0
773 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired, NULL);
774 mycpu->gd_cnt.v_vm_faults++;
775 if (curthread->td_lwp)
776 ++curthread->td_lwp->lwp_ru.ru_minflt;
777 #endif
780 * On success vm_fault_object() does not unlock or deallocate, and fs.m
781 * will contain a busied page. So we must unlock here after having
782 * messed with the pmap.
784 unlock_things(&fs);
787 * Return a held page. We are not doing any pmap manipulation so do
788 * not set PG_MAPPED. However, adjust the page flags according to
789 * the fault type because the caller may not use a managed pmapping
790 * (so we don't want to lose the fact that the page will be dirtied
791 * if a write fault was specified).
793 vm_page_hold(fs.m);
794 vm_page_activate(fs.m);
795 if (fault_type & VM_PROT_WRITE)
796 vm_page_dirty(fs.m);
798 if (curthread->td_lwp) {
799 if (fs.hardfault) {
800 curthread->td_lwp->lwp_ru.ru_majflt++;
801 } else {
802 curthread->td_lwp->lwp_ru.ru_minflt++;
807 * Unlock everything, and return the held page.
809 vm_page_wakeup(fs.m);
810 /*vm_object_deallocate(fs.first_object);*/
811 /*fs.first_object = NULL; */
812 *errorp = 0;
814 done:
815 if (fs.first_object)
816 vm_object_drop(fs.first_object);
817 lwkt_reltoken(&map->token);
818 return(fs.m);
822 * Fault in the specified (object,offset), dirty the returned page as
823 * needed. If the requested fault_type cannot be done NULL and an
824 * error is returned.
826 * A held (but not busied) page is returned.
828 * No requirements.
830 vm_page_t
831 vm_fault_object_page(vm_object_t object, vm_ooffset_t offset,
832 vm_prot_t fault_type, int fault_flags, int *errorp)
834 int result;
835 vm_pindex_t first_pindex;
836 struct faultstate fs;
837 struct vm_map_entry entry;
839 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
840 bzero(&entry, sizeof(entry));
841 entry.object.vm_object = object;
842 entry.maptype = VM_MAPTYPE_NORMAL;
843 entry.protection = entry.max_protection = fault_type;
845 fs.hardfault = 0;
846 fs.fault_flags = fault_flags;
847 fs.map = NULL;
848 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
850 RetryFault:
852 fs.first_object = object;
853 first_pindex = OFF_TO_IDX(offset);
854 fs.entry = &entry;
855 fs.first_prot = fault_type;
856 fs.wired = 0;
857 /*fs.map_generation = 0; unused */
860 * Make a reference to this object to prevent its disposal while we
861 * are messing with it. Once we have the reference, the map is free
862 * to be diddled. Since objects reference their shadows (and copies),
863 * they will stay around as well.
865 * The reference should also prevent an unexpected collapse of the
866 * parent that might move pages from the current object into the
867 * parent unexpectedly, resulting in corruption.
869 * Bump the paging-in-progress count to prevent size changes (e.g.
870 * truncation operations) during I/O. This must be done after
871 * obtaining the vnode lock in order to avoid possible deadlocks.
873 fs.vp = vnode_pager_lock(fs.first_object);
875 fs.lookup_still_valid = TRUE;
876 fs.first_m = NULL;
877 fs.object = fs.first_object; /* so unlock_and_deallocate works */
879 #if 0
880 /* XXX future - ability to operate on VM object using vpagetable */
881 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
882 result = vm_fault_vpagetable(&fs, &first_pindex,
883 fs.entry->aux.master_pde,
884 fault_type);
885 if (result == KERN_TRY_AGAIN)
886 goto RetryFault;
887 if (result != KERN_SUCCESS) {
888 *errorp = result;
889 return (NULL);
892 #endif
895 * Now we have the actual (object, pindex), fault in the page. If
896 * vm_fault_object() fails it will unlock and deallocate the FS
897 * data. If it succeeds everything remains locked and fs->object
898 * will have an additinal PIP count if it is not equal to
899 * fs->first_object
901 result = vm_fault_object(&fs, first_pindex, fault_type);
903 if (result == KERN_TRY_AGAIN)
904 goto RetryFault;
905 if (result != KERN_SUCCESS) {
906 *errorp = result;
907 return(NULL);
910 if ((fault_type & VM_PROT_WRITE) && (fs.prot & VM_PROT_WRITE) == 0) {
911 *errorp = KERN_PROTECTION_FAILURE;
912 unlock_and_deallocate(&fs);
913 return(NULL);
917 * On success vm_fault_object() does not unlock or deallocate, so we
918 * do it here. Note that the returned fs.m will be busied.
920 unlock_things(&fs);
923 * Return a held page. We are not doing any pmap manipulation so do
924 * not set PG_MAPPED. However, adjust the page flags according to
925 * the fault type because the caller may not use a managed pmapping
926 * (so we don't want to lose the fact that the page will be dirtied
927 * if a write fault was specified).
929 vm_page_hold(fs.m);
930 vm_page_activate(fs.m);
931 if ((fault_type & VM_PROT_WRITE) || (fault_flags & VM_FAULT_DIRTY))
932 vm_page_dirty(fs.m);
933 if (fault_flags & VM_FAULT_UNSWAP)
934 swap_pager_unswapped(fs.m);
937 * Indicate that the page was accessed.
939 vm_page_flag_set(fs.m, PG_REFERENCED);
941 if (curthread->td_lwp) {
942 if (fs.hardfault) {
943 curthread->td_lwp->lwp_ru.ru_majflt++;
944 } else {
945 curthread->td_lwp->lwp_ru.ru_minflt++;
950 * Unlock everything, and return the held page.
952 vm_page_wakeup(fs.m);
953 /*vm_object_deallocate(fs.first_object);*/
954 /*fs.first_object = NULL; */
956 *errorp = 0;
957 return(fs.m);
961 * Translate the virtual page number (first_pindex) that is relative
962 * to the address space into a logical page number that is relative to the
963 * backing object. Use the virtual page table pointed to by (vpte).
965 * This implements an N-level page table. Any level can terminate the
966 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
967 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
969 static
971 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex,
972 vpte_t vpte, int fault_type)
974 struct lwbuf *lwb;
975 struct lwbuf lwb_cache;
976 int vshift = VPTE_FRAME_END - PAGE_SHIFT; /* index bits remaining */
977 int result = KERN_SUCCESS;
978 vpte_t *ptep;
980 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
981 for (;;) {
983 * We cannot proceed if the vpte is not valid, not readable
984 * for a read fault, or not writable for a write fault.
986 if ((vpte & VPTE_V) == 0) {
987 unlock_and_deallocate(fs);
988 return (KERN_FAILURE);
990 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_R) == 0) {
991 unlock_and_deallocate(fs);
992 return (KERN_FAILURE);
994 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_W) == 0) {
995 unlock_and_deallocate(fs);
996 return (KERN_FAILURE);
998 if ((vpte & VPTE_PS) || vshift == 0)
999 break;
1000 KKASSERT(vshift >= VPTE_PAGE_BITS);
1003 * Get the page table page. Nominally we only read the page
1004 * table, but since we are actively setting VPTE_M and VPTE_A,
1005 * tell vm_fault_object() that we are writing it.
1007 * There is currently no real need to optimize this.
1009 result = vm_fault_object(fs, (vpte & VPTE_FRAME) >> PAGE_SHIFT,
1010 VM_PROT_READ|VM_PROT_WRITE);
1011 if (result != KERN_SUCCESS)
1012 return (result);
1015 * Process the returned fs.m and look up the page table
1016 * entry in the page table page.
1018 vshift -= VPTE_PAGE_BITS;
1019 lwb = lwbuf_alloc(fs->m, &lwb_cache);
1020 ptep = ((vpte_t *)lwbuf_kva(lwb) +
1021 ((*pindex >> vshift) & VPTE_PAGE_MASK));
1022 vpte = *ptep;
1025 * Page table write-back. If the vpte is valid for the
1026 * requested operation, do a write-back to the page table.
1028 * XXX VPTE_M is not set properly for page directory pages.
1029 * It doesn't get set in the page directory if the page table
1030 * is modified during a read access.
1032 vm_page_activate(fs->m);
1033 if ((fault_type & VM_PROT_WRITE) && (vpte & VPTE_V) &&
1034 (vpte & VPTE_W)) {
1035 if ((vpte & (VPTE_M|VPTE_A)) != (VPTE_M|VPTE_A)) {
1036 atomic_set_long(ptep, VPTE_M | VPTE_A);
1037 vm_page_dirty(fs->m);
1040 if ((fault_type & VM_PROT_READ) && (vpte & VPTE_V) &&
1041 (vpte & VPTE_R)) {
1042 if ((vpte & VPTE_A) == 0) {
1043 atomic_set_long(ptep, VPTE_A);
1044 vm_page_dirty(fs->m);
1047 lwbuf_free(lwb);
1048 vm_page_flag_set(fs->m, PG_REFERENCED);
1049 vm_page_wakeup(fs->m);
1050 fs->m = NULL;
1051 cleanup_successful_fault(fs);
1054 * Combine remaining address bits with the vpte.
1056 /* JG how many bits from each? */
1057 *pindex = ((vpte & VPTE_FRAME) >> PAGE_SHIFT) +
1058 (*pindex & ((1L << vshift) - 1));
1059 return (KERN_SUCCESS);
1064 * This is the core of the vm_fault code.
1066 * Do all operations required to fault-in (fs.first_object, pindex). Run
1067 * through the shadow chain as necessary and do required COW or virtual
1068 * copy operations. The caller has already fully resolved the vm_map_entry
1069 * and, if appropriate, has created a copy-on-write layer. All we need to
1070 * do is iterate the object chain.
1072 * On failure (fs) is unlocked and deallocated and the caller may return or
1073 * retry depending on the failure code. On success (fs) is NOT unlocked or
1074 * deallocated, fs.m will contained a resolved, busied page, and fs.object
1075 * will have an additional PIP count if it is not equal to fs.first_object.
1077 * fs->first_object must be held on call.
1079 static
1081 vm_fault_object(struct faultstate *fs,
1082 vm_pindex_t first_pindex, vm_prot_t fault_type)
1084 vm_object_t next_object;
1085 vm_pindex_t pindex;
1086 int error;
1088 ASSERT_LWKT_TOKEN_HELD(vm_object_token(fs->first_object));
1089 fs->prot = fs->first_prot;
1090 fs->object = fs->first_object;
1091 pindex = first_pindex;
1093 vm_object_chain_acquire(fs->first_object);
1094 vm_object_pip_add(fs->first_object, 1);
1097 * If a read fault occurs we try to make the page writable if
1098 * possible. There are three cases where we cannot make the
1099 * page mapping writable:
1101 * (1) The mapping is read-only or the VM object is read-only,
1102 * fs->prot above will simply not have VM_PROT_WRITE set.
1104 * (2) If the mapping is a virtual page table we need to be able
1105 * to detect writes so we can set VPTE_M in the virtual page
1106 * table.
1108 * (3) If the VM page is read-only or copy-on-write, upgrading would
1109 * just result in an unnecessary COW fault.
1111 * VM_PROT_VPAGED is set if faulting via a virtual page table and
1112 * causes adjustments to the 'M'odify bit to also turn off write
1113 * access to force a re-fault.
1115 if (fs->entry->maptype == VM_MAPTYPE_VPAGETABLE) {
1116 if ((fault_type & VM_PROT_WRITE) == 0)
1117 fs->prot &= ~VM_PROT_WRITE;
1120 /* vm_object_hold(fs->object); implied b/c object == first_object */
1122 for (;;) {
1124 * The entire backing chain from first_object to object
1125 * inclusive is chainlocked.
1127 * If the object is dead, we stop here
1129 if (fs->object->flags & OBJ_DEAD) {
1130 vm_object_pip_wakeup(fs->first_object);
1131 vm_object_chain_release_all(fs->first_object,
1132 fs->object);
1133 if (fs->object != fs->first_object)
1134 vm_object_drop(fs->object);
1135 unlock_and_deallocate(fs);
1136 return (KERN_PROTECTION_FAILURE);
1140 * See if the page is resident. Wait/Retry if the page is
1141 * busy (lots of stuff may have changed so we can't continue
1142 * in that case).
1144 * We can theoretically allow the soft-busy case on a read
1145 * fault if the page is marked valid, but since such
1146 * pages are typically already pmap'd, putting that
1147 * special case in might be more effort then it is
1148 * worth. We cannot under any circumstances mess
1149 * around with a vm_page_t->busy page except, perhaps,
1150 * to pmap it.
1152 fs->m = vm_page_lookup_busy_try(fs->object, pindex,
1153 TRUE, &error);
1154 if (error) {
1155 vm_object_pip_wakeup(fs->first_object);
1156 vm_object_chain_release_all(fs->first_object,
1157 fs->object);
1158 if (fs->object != fs->first_object)
1159 vm_object_drop(fs->object);
1160 unlock_things(fs);
1161 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
1162 mycpu->gd_cnt.v_intrans++;
1163 /*vm_object_deallocate(fs->first_object);*/
1164 /*fs->first_object = NULL;*/
1165 fs->m = NULL;
1166 return (KERN_TRY_AGAIN);
1168 if (fs->m) {
1170 * The page is busied for us.
1172 * If reactivating a page from PQ_CACHE we may have
1173 * to rate-limit.
1175 int queue = fs->m->queue;
1176 vm_page_unqueue_nowakeup(fs->m);
1178 if ((queue - fs->m->pc) == PQ_CACHE &&
1179 vm_page_count_severe()) {
1180 vm_page_activate(fs->m);
1181 vm_page_wakeup(fs->m);
1182 fs->m = NULL;
1183 vm_object_pip_wakeup(fs->first_object);
1184 vm_object_chain_release_all(fs->first_object,
1185 fs->object);
1186 if (fs->object != fs->first_object)
1187 vm_object_drop(fs->object);
1188 unlock_and_deallocate(fs);
1189 vm_waitpfault();
1190 return (KERN_TRY_AGAIN);
1194 * If it still isn't completely valid (readable),
1195 * or if a read-ahead-mark is set on the VM page,
1196 * jump to readrest, else we found the page and
1197 * can return.
1199 * We can release the spl once we have marked the
1200 * page busy.
1202 if (fs->m->object != &kernel_object) {
1203 if ((fs->m->valid & VM_PAGE_BITS_ALL) !=
1204 VM_PAGE_BITS_ALL) {
1205 goto readrest;
1207 if (fs->m->flags & PG_RAM) {
1208 if (debug_cluster)
1209 kprintf("R");
1210 vm_page_flag_clear(fs->m, PG_RAM);
1211 goto readrest;
1214 break; /* break to PAGE HAS BEEN FOUND */
1218 * Page is not resident, If this is the search termination
1219 * or the pager might contain the page, allocate a new page.
1221 if (TRYPAGER(fs) || fs->object == fs->first_object) {
1223 * If the page is beyond the object size we fail
1225 if (pindex >= fs->object->size) {
1226 vm_object_pip_wakeup(fs->first_object);
1227 vm_object_chain_release_all(fs->first_object,
1228 fs->object);
1229 if (fs->object != fs->first_object)
1230 vm_object_drop(fs->object);
1231 unlock_and_deallocate(fs);
1232 return (KERN_PROTECTION_FAILURE);
1236 * Allocate a new page for this object/offset pair.
1238 * It is possible for the allocation to race, so
1239 * handle the case.
1241 fs->m = NULL;
1242 if (!vm_page_count_severe()) {
1243 fs->m = vm_page_alloc(fs->object, pindex,
1244 ((fs->vp || fs->object->backing_object) ?
1245 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL :
1246 VM_ALLOC_NULL_OK | VM_ALLOC_NORMAL |
1247 VM_ALLOC_USE_GD | VM_ALLOC_ZERO));
1249 if (fs->m == NULL) {
1250 vm_object_pip_wakeup(fs->first_object);
1251 vm_object_chain_release_all(fs->first_object,
1252 fs->object);
1253 if (fs->object != fs->first_object)
1254 vm_object_drop(fs->object);
1255 unlock_and_deallocate(fs);
1256 vm_waitpfault();
1257 return (KERN_TRY_AGAIN);
1261 * Fall through to readrest. We have a new page which
1262 * will have to be paged (since m->valid will be 0).
1266 readrest:
1268 * We have found an invalid or partially valid page, a
1269 * page with a read-ahead mark which might be partially or
1270 * fully valid (and maybe dirty too), or we have allocated
1271 * a new page.
1273 * Attempt to fault-in the page if there is a chance that the
1274 * pager has it, and potentially fault in additional pages
1275 * at the same time.
1277 * If TRYPAGER is true then fs.m will be non-NULL and busied
1278 * for us.
1280 if (TRYPAGER(fs)) {
1281 int rv;
1282 int seqaccess;
1283 u_char behavior = vm_map_entry_behavior(fs->entry);
1285 if (behavior == MAP_ENTRY_BEHAV_RANDOM)
1286 seqaccess = 0;
1287 else
1288 seqaccess = -1;
1290 #if 0
1292 * If sequential access is detected then attempt
1293 * to deactivate/cache pages behind the scan to
1294 * prevent resource hogging.
1296 * Use of PG_RAM to detect sequential access
1297 * also simulates multi-zone sequential access
1298 * detection for free.
1300 * NOTE: Partially valid dirty pages cannot be
1301 * deactivated without causing NFS picemeal
1302 * writes to barf.
1304 if ((fs->first_object->type != OBJT_DEVICE) &&
1305 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
1306 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
1307 (fs->m->flags & PG_RAM)))
1309 vm_pindex_t scan_pindex;
1310 int scan_count = 16;
1312 if (first_pindex < 16) {
1313 scan_pindex = 0;
1314 scan_count = 0;
1315 } else {
1316 scan_pindex = first_pindex - 16;
1317 if (scan_pindex < 16)
1318 scan_count = scan_pindex;
1319 else
1320 scan_count = 16;
1323 while (scan_count) {
1324 vm_page_t mt;
1326 mt = vm_page_lookup(fs->first_object,
1327 scan_pindex);
1328 if (mt == NULL)
1329 break;
1330 if (vm_page_busy_try(mt, TRUE))
1331 goto skip;
1333 if (mt->valid != VM_PAGE_BITS_ALL) {
1334 vm_page_wakeup(mt);
1335 break;
1337 if ((mt->flags &
1338 (PG_FICTITIOUS | PG_UNMANAGED |
1339 PG_NEED_COMMIT)) ||
1340 mt->hold_count ||
1341 mt->wire_count) {
1342 vm_page_wakeup(mt);
1343 goto skip;
1345 if (mt->dirty == 0)
1346 vm_page_test_dirty(mt);
1347 if (mt->dirty) {
1348 vm_page_protect(mt,
1349 VM_PROT_NONE);
1350 vm_page_deactivate(mt);
1351 vm_page_wakeup(mt);
1352 } else {
1353 vm_page_cache(mt);
1355 skip:
1356 --scan_count;
1357 --scan_pindex;
1360 seqaccess = 1;
1362 #endif
1365 * Avoid deadlocking against the map when doing I/O.
1366 * fs.object and the page is PG_BUSY'd.
1368 * NOTE: Once unlocked, fs->entry can become stale
1369 * so this will NULL it out.
1371 * NOTE: fs->entry is invalid until we relock the
1372 * map and verify that the timestamp has not
1373 * changed.
1375 unlock_map(fs);
1378 * Acquire the page data. We still hold a ref on
1379 * fs.object and the page has been PG_BUSY's.
1381 * The pager may replace the page (for example, in
1382 * order to enter a fictitious page into the
1383 * object). If it does so it is responsible for
1384 * cleaning up the passed page and properly setting
1385 * the new page PG_BUSY.
1387 * If we got here through a PG_RAM read-ahead
1388 * mark the page may be partially dirty and thus
1389 * not freeable. Don't bother checking to see
1390 * if the pager has the page because we can't free
1391 * it anyway. We have to depend on the get_page
1392 * operation filling in any gaps whether there is
1393 * backing store or not.
1395 rv = vm_pager_get_page(fs->object, &fs->m, seqaccess);
1397 if (rv == VM_PAGER_OK) {
1399 * Relookup in case pager changed page. Pager
1400 * is responsible for disposition of old page
1401 * if moved.
1403 * XXX other code segments do relookups too.
1404 * It's a bad abstraction that needs to be
1405 * fixed/removed.
1407 fs->m = vm_page_lookup(fs->object, pindex);
1408 if (fs->m == NULL) {
1409 vm_object_pip_wakeup(fs->first_object);
1410 vm_object_chain_release_all(
1411 fs->first_object, fs->object);
1412 if (fs->object != fs->first_object)
1413 vm_object_drop(fs->object);
1414 unlock_and_deallocate(fs);
1415 return (KERN_TRY_AGAIN);
1418 ++fs->hardfault;
1419 break; /* break to PAGE HAS BEEN FOUND */
1423 * Remove the bogus page (which does not exist at this
1424 * object/offset); before doing so, we must get back
1425 * our object lock to preserve our invariant.
1427 * Also wake up any other process that may want to bring
1428 * in this page.
1430 * If this is the top-level object, we must leave the
1431 * busy page to prevent another process from rushing
1432 * past us, and inserting the page in that object at
1433 * the same time that we are.
1435 if (rv == VM_PAGER_ERROR) {
1436 if (curproc) {
1437 kprintf("vm_fault: pager read error, "
1438 "pid %d (%s)\n",
1439 curproc->p_pid,
1440 curproc->p_comm);
1441 } else {
1442 kprintf("vm_fault: pager read error, "
1443 "thread %p (%s)\n",
1444 curthread,
1445 curproc->p_comm);
1450 * Data outside the range of the pager or an I/O error
1452 * The page may have been wired during the pagein,
1453 * e.g. by the buffer cache, and cannot simply be
1454 * freed. Call vnode_pager_freepage() to deal with it.
1457 * XXX - the check for kernel_map is a kludge to work
1458 * around having the machine panic on a kernel space
1459 * fault w/ I/O error.
1461 if (((fs->map != &kernel_map) &&
1462 (rv == VM_PAGER_ERROR)) || (rv == VM_PAGER_BAD)) {
1463 vnode_pager_freepage(fs->m);
1464 fs->m = NULL;
1465 vm_object_pip_wakeup(fs->first_object);
1466 vm_object_chain_release_all(fs->first_object,
1467 fs->object);
1468 if (fs->object != fs->first_object)
1469 vm_object_drop(fs->object);
1470 unlock_and_deallocate(fs);
1471 if (rv == VM_PAGER_ERROR)
1472 return (KERN_FAILURE);
1473 else
1474 return (KERN_PROTECTION_FAILURE);
1475 /* NOT REACHED */
1477 if (fs->object != fs->first_object) {
1478 vnode_pager_freepage(fs->m);
1479 fs->m = NULL;
1481 * XXX - we cannot just fall out at this
1482 * point, m has been freed and is invalid!
1488 * We get here if the object has a default pager (or unwiring)
1489 * or the pager doesn't have the page.
1491 if (fs->object == fs->first_object)
1492 fs->first_m = fs->m;
1495 * Move on to the next object. The chain lock should prevent
1496 * the backing_object from getting ripped out from under us.
1498 if ((next_object = fs->object->backing_object) != NULL) {
1499 vm_object_hold(next_object);
1500 vm_object_chain_acquire(next_object);
1501 KKASSERT(next_object == fs->object->backing_object);
1502 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
1505 if (next_object == NULL) {
1507 * If there's no object left, fill the page in the top
1508 * object with zeros.
1510 if (fs->object != fs->first_object) {
1511 if (fs->first_object->backing_object !=
1512 fs->object) {
1513 vm_object_hold(fs->first_object->backing_object);
1515 vm_object_chain_release_all(
1516 fs->first_object->backing_object,
1517 fs->object);
1518 if (fs->first_object->backing_object !=
1519 fs->object) {
1520 vm_object_drop(fs->first_object->backing_object);
1522 vm_object_pip_wakeup(fs->object);
1523 vm_object_drop(fs->object);
1524 fs->object = fs->first_object;
1525 pindex = first_pindex;
1526 fs->m = fs->first_m;
1528 fs->first_m = NULL;
1531 * Zero the page if necessary and mark it valid.
1533 if ((fs->m->flags & PG_ZERO) == 0) {
1534 vm_page_zero_fill(fs->m);
1535 } else {
1536 #ifdef PMAP_DEBUG
1537 pmap_page_assertzero(VM_PAGE_TO_PHYS(fs->m));
1538 #endif
1539 vm_page_flag_clear(fs->m, PG_ZERO);
1540 mycpu->gd_cnt.v_ozfod++;
1542 mycpu->gd_cnt.v_zfod++;
1543 fs->m->valid = VM_PAGE_BITS_ALL;
1544 break; /* break to PAGE HAS BEEN FOUND */
1546 if (fs->object != fs->first_object) {
1547 vm_object_pip_wakeup(fs->object);
1548 vm_object_lock_swap();
1549 vm_object_drop(fs->object);
1551 KASSERT(fs->object != next_object,
1552 ("object loop %p", next_object));
1553 fs->object = next_object;
1554 vm_object_pip_add(fs->object, 1);
1558 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1559 * is held.]
1561 * object still held.
1563 * If the page is being written, but isn't already owned by the
1564 * top-level object, we have to copy it into a new page owned by the
1565 * top-level object.
1567 KASSERT((fs->m->flags & PG_BUSY) != 0,
1568 ("vm_fault: not busy after main loop"));
1570 if (fs->object != fs->first_object) {
1572 * We only really need to copy if we want to write it.
1574 if (fault_type & VM_PROT_WRITE) {
1576 * This allows pages to be virtually copied from a
1577 * backing_object into the first_object, where the
1578 * backing object has no other refs to it, and cannot
1579 * gain any more refs. Instead of a bcopy, we just
1580 * move the page from the backing object to the
1581 * first object. Note that we must mark the page
1582 * dirty in the first object so that it will go out
1583 * to swap when needed.
1585 if (
1587 * Map, if present, has not changed
1589 (fs->map == NULL ||
1590 fs->map_generation == fs->map->timestamp) &&
1592 * Only one shadow object
1594 (fs->object->shadow_count == 1) &&
1596 * No COW refs, except us
1598 (fs->object->ref_count == 1) &&
1600 * No one else can look this object up
1602 (fs->object->handle == NULL) &&
1604 * No other ways to look the object up
1606 ((fs->object->type == OBJT_DEFAULT) ||
1607 (fs->object->type == OBJT_SWAP)) &&
1609 * We don't chase down the shadow chain
1611 (fs->object == fs->first_object->backing_object) &&
1614 * grab the lock if we need to
1616 (fs->lookup_still_valid ||
1617 fs->map == NULL ||
1618 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1621 * (first_m) and (m) are both busied. We have
1622 * move (m) into (first_m)'s object/pindex
1623 * in an atomic fashion, then free (first_m).
1625 * first_object is held so second remove
1626 * followed by the rename should wind
1627 * up being atomic. vm_page_free() might
1628 * block so we don't do it until after the
1629 * rename.
1631 fs->lookup_still_valid = 1;
1632 vm_page_protect(fs->first_m, VM_PROT_NONE);
1633 vm_page_remove(fs->first_m);
1634 vm_page_rename(fs->m, fs->first_object,
1635 first_pindex);
1636 vm_page_free(fs->first_m);
1637 fs->first_m = fs->m;
1638 fs->m = NULL;
1639 mycpu->gd_cnt.v_cow_optim++;
1640 } else {
1642 * Oh, well, lets copy it.
1644 * Why are we unmapping the original page
1645 * here? Well, in short, not all accessors
1646 * of user memory go through the pmap. The
1647 * procfs code doesn't have access user memory
1648 * via a local pmap, so vm_fault_page*()
1649 * can't call pmap_enter(). And the umtx*()
1650 * code may modify the COW'd page via a DMAP
1651 * or kernel mapping and not via the pmap,
1652 * leaving the original page still mapped
1653 * read-only into the pmap.
1655 * So we have to remove the page from at
1656 * least the current pmap if it is in it.
1657 * Just remove it from all pmaps.
1659 vm_page_copy(fs->m, fs->first_m);
1660 vm_page_protect(fs->m, VM_PROT_NONE);
1661 vm_page_event(fs->m, VMEVENT_COW);
1664 if (fs->m) {
1666 * We no longer need the old page or object.
1668 release_page(fs);
1672 * We intend to revert to first_object, undo the
1673 * chain lock through to that.
1675 if (fs->first_object->backing_object != fs->object)
1676 vm_object_hold(fs->first_object->backing_object);
1677 vm_object_chain_release_all(
1678 fs->first_object->backing_object,
1679 fs->object);
1680 if (fs->first_object->backing_object != fs->object)
1681 vm_object_drop(fs->first_object->backing_object);
1684 * fs->object != fs->first_object due to above
1685 * conditional
1687 vm_object_pip_wakeup(fs->object);
1688 vm_object_drop(fs->object);
1691 * Only use the new page below...
1694 mycpu->gd_cnt.v_cow_faults++;
1695 fs->m = fs->first_m;
1696 fs->object = fs->first_object;
1697 pindex = first_pindex;
1698 } else {
1700 * If it wasn't a write fault avoid having to copy
1701 * the page by mapping it read-only.
1703 fs->prot &= ~VM_PROT_WRITE;
1708 * Relock the map if necessary, then check the generation count.
1709 * relock_map() will update fs->timestamp to account for the
1710 * relocking if necessary.
1712 * If the count has changed after relocking then all sorts of
1713 * crap may have happened and we have to retry.
1715 * NOTE: The relock_map() can fail due to a deadlock against
1716 * the vm_page we are holding BUSY.
1718 if (fs->lookup_still_valid == FALSE && fs->map) {
1719 if (relock_map(fs) ||
1720 fs->map->timestamp != fs->map_generation) {
1721 release_page(fs);
1722 vm_object_pip_wakeup(fs->first_object);
1723 vm_object_chain_release_all(fs->first_object,
1724 fs->object);
1725 if (fs->object != fs->first_object)
1726 vm_object_drop(fs->object);
1727 unlock_and_deallocate(fs);
1728 return (KERN_TRY_AGAIN);
1733 * If the fault is a write, we know that this page is being
1734 * written NOW so dirty it explicitly to save on pmap_is_modified()
1735 * calls later.
1737 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1738 * if the page is already dirty to prevent data written with
1739 * the expectation of being synced from not being synced.
1740 * Likewise if this entry does not request NOSYNC then make
1741 * sure the page isn't marked NOSYNC. Applications sharing
1742 * data should use the same flags to avoid ping ponging.
1744 * Also tell the backing pager, if any, that it should remove
1745 * any swap backing since the page is now dirty.
1747 vm_page_activate(fs->m);
1748 if (fs->prot & VM_PROT_WRITE) {
1749 vm_object_set_writeable_dirty(fs->m->object);
1750 vm_set_nosync(fs->m, fs->entry);
1751 if (fs->fault_flags & VM_FAULT_DIRTY) {
1752 vm_page_dirty(fs->m);
1753 swap_pager_unswapped(fs->m);
1757 vm_object_pip_wakeup(fs->first_object);
1758 vm_object_chain_release_all(fs->first_object, fs->object);
1759 if (fs->object != fs->first_object)
1760 vm_object_drop(fs->object);
1763 * Page had better still be busy. We are still locked up and
1764 * fs->object will have another PIP reference if it is not equal
1765 * to fs->first_object.
1767 KASSERT(fs->m->flags & PG_BUSY,
1768 ("vm_fault: page %p not busy!", fs->m));
1771 * Sanity check: page must be completely valid or it is not fit to
1772 * map into user space. vm_pager_get_pages() ensures this.
1774 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1775 vm_page_zero_invalid(fs->m, TRUE);
1776 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1778 vm_page_flag_clear(fs->m, PG_ZERO);
1780 return (KERN_SUCCESS);
1784 * Wire down a range of virtual addresses in a map. The entry in question
1785 * should be marked in-transition and the map must be locked. We must
1786 * release the map temporarily while faulting-in the page to avoid a
1787 * deadlock. Note that the entry may be clipped while we are blocked but
1788 * will never be freed.
1790 * No requirements.
1793 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1795 boolean_t fictitious;
1796 vm_offset_t start;
1797 vm_offset_t end;
1798 vm_offset_t va;
1799 vm_paddr_t pa;
1800 vm_page_t m;
1801 pmap_t pmap;
1802 int rv;
1804 lwkt_gettoken(&map->token);
1806 pmap = vm_map_pmap(map);
1807 start = entry->start;
1808 end = entry->end;
1809 fictitious = entry->object.vm_object &&
1810 (entry->object.vm_object->type == OBJT_DEVICE);
1811 if (entry->eflags & MAP_ENTRY_KSTACK)
1812 start += PAGE_SIZE;
1813 map->timestamp++;
1814 vm_map_unlock(map);
1817 * We simulate a fault to get the page and enter it in the physical
1818 * map.
1820 for (va = start; va < end; va += PAGE_SIZE) {
1821 if (user_wire) {
1822 rv = vm_fault(map, va, VM_PROT_READ,
1823 VM_FAULT_USER_WIRE);
1824 } else {
1825 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1826 VM_FAULT_CHANGE_WIRING);
1828 if (rv) {
1829 while (va > start) {
1830 va -= PAGE_SIZE;
1831 if ((pa = pmap_extract(pmap, va)) == 0)
1832 continue;
1833 pmap_change_wiring(pmap, va, FALSE, entry);
1834 if (!fictitious) {
1835 m = PHYS_TO_VM_PAGE(pa);
1836 vm_page_busy_wait(m, FALSE, "vmwrpg");
1837 vm_page_unwire(m, 1);
1838 vm_page_wakeup(m);
1841 goto done;
1844 rv = KERN_SUCCESS;
1845 done:
1846 vm_map_lock(map);
1847 lwkt_reltoken(&map->token);
1848 return (rv);
1852 * Unwire a range of virtual addresses in a map. The map should be
1853 * locked.
1855 void
1856 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1858 boolean_t fictitious;
1859 vm_offset_t start;
1860 vm_offset_t end;
1861 vm_offset_t va;
1862 vm_paddr_t pa;
1863 vm_page_t m;
1864 pmap_t pmap;
1866 lwkt_gettoken(&map->token);
1868 pmap = vm_map_pmap(map);
1869 start = entry->start;
1870 end = entry->end;
1871 fictitious = entry->object.vm_object &&
1872 (entry->object.vm_object->type == OBJT_DEVICE);
1873 if (entry->eflags & MAP_ENTRY_KSTACK)
1874 start += PAGE_SIZE;
1877 * Since the pages are wired down, we must be able to get their
1878 * mappings from the physical map system.
1880 for (va = start; va < end; va += PAGE_SIZE) {
1881 pa = pmap_extract(pmap, va);
1882 if (pa != 0) {
1883 pmap_change_wiring(pmap, va, FALSE, entry);
1884 if (!fictitious) {
1885 m = PHYS_TO_VM_PAGE(pa);
1886 vm_page_busy_wait(m, FALSE, "vmwupg");
1887 vm_page_unwire(m, 1);
1888 vm_page_wakeup(m);
1892 lwkt_reltoken(&map->token);
1896 * Copy all of the pages from a wired-down map entry to another.
1898 * The source and destination maps must be locked for write.
1899 * The source and destination maps token must be held
1900 * The source map entry must be wired down (or be a sharing map
1901 * entry corresponding to a main map entry that is wired down).
1903 * No other requirements.
1905 * XXX do segment optimization
1907 void
1908 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1909 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1911 vm_object_t dst_object;
1912 vm_object_t src_object;
1913 vm_ooffset_t dst_offset;
1914 vm_ooffset_t src_offset;
1915 vm_prot_t prot;
1916 vm_offset_t vaddr;
1917 vm_page_t dst_m;
1918 vm_page_t src_m;
1920 src_object = src_entry->object.vm_object;
1921 src_offset = src_entry->offset;
1924 * Create the top-level object for the destination entry. (Doesn't
1925 * actually shadow anything - we copy the pages directly.)
1927 vm_map_entry_allocate_object(dst_entry);
1928 dst_object = dst_entry->object.vm_object;
1930 prot = dst_entry->max_protection;
1933 * Loop through all of the pages in the entry's range, copying each
1934 * one from the source object (it should be there) to the destination
1935 * object.
1937 vm_object_hold(src_object);
1938 vm_object_hold(dst_object);
1939 for (vaddr = dst_entry->start, dst_offset = 0;
1940 vaddr < dst_entry->end;
1941 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1944 * Allocate a page in the destination object
1946 do {
1947 dst_m = vm_page_alloc(dst_object,
1948 OFF_TO_IDX(dst_offset),
1949 VM_ALLOC_NORMAL);
1950 if (dst_m == NULL) {
1951 vm_wait(0);
1953 } while (dst_m == NULL);
1956 * Find the page in the source object, and copy it in.
1957 * (Because the source is wired down, the page will be in
1958 * memory.)
1960 src_m = vm_page_lookup(src_object,
1961 OFF_TO_IDX(dst_offset + src_offset));
1962 if (src_m == NULL)
1963 panic("vm_fault_copy_wired: page missing");
1965 vm_page_copy(src_m, dst_m);
1966 vm_page_event(src_m, VMEVENT_COW);
1969 * Enter it in the pmap...
1972 vm_page_flag_clear(dst_m, PG_ZERO);
1973 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE, dst_entry);
1976 * Mark it no longer busy, and put it on the active list.
1978 vm_page_activate(dst_m);
1979 vm_page_wakeup(dst_m);
1981 vm_object_drop(dst_object);
1982 vm_object_drop(src_object);
1985 #if 0
1988 * This routine checks around the requested page for other pages that
1989 * might be able to be faulted in. This routine brackets the viable
1990 * pages for the pages to be paged in.
1992 * Inputs:
1993 * m, rbehind, rahead
1995 * Outputs:
1996 * marray (array of vm_page_t), reqpage (index of requested page)
1998 * Return value:
1999 * number of pages in marray
2001 static int
2002 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
2003 vm_page_t *marray, int *reqpage)
2005 int i,j;
2006 vm_object_t object;
2007 vm_pindex_t pindex, startpindex, endpindex, tpindex;
2008 vm_page_t rtm;
2009 int cbehind, cahead;
2011 object = m->object;
2012 pindex = m->pindex;
2015 * we don't fault-ahead for device pager
2017 if (object->type == OBJT_DEVICE) {
2018 *reqpage = 0;
2019 marray[0] = m;
2020 return 1;
2024 * if the requested page is not available, then give up now
2026 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
2027 *reqpage = 0; /* not used by caller, fix compiler warn */
2028 return 0;
2031 if ((cbehind == 0) && (cahead == 0)) {
2032 *reqpage = 0;
2033 marray[0] = m;
2034 return 1;
2037 if (rahead > cahead) {
2038 rahead = cahead;
2041 if (rbehind > cbehind) {
2042 rbehind = cbehind;
2046 * Do not do any readahead if we have insufficient free memory.
2048 * XXX code was broken disabled before and has instability
2049 * with this conditonal fixed, so shortcut for now.
2051 if (burst_fault == 0 || vm_page_count_severe()) {
2052 marray[0] = m;
2053 *reqpage = 0;
2054 return 1;
2058 * scan backward for the read behind pages -- in memory
2060 * Assume that if the page is not found an interrupt will not
2061 * create it. Theoretically interrupts can only remove (busy)
2062 * pages, not create new associations.
2064 if (pindex > 0) {
2065 if (rbehind > pindex) {
2066 rbehind = pindex;
2067 startpindex = 0;
2068 } else {
2069 startpindex = pindex - rbehind;
2072 vm_object_hold(object);
2073 for (tpindex = pindex; tpindex > startpindex; --tpindex) {
2074 if (vm_page_lookup(object, tpindex - 1))
2075 break;
2078 i = 0;
2079 while (tpindex < pindex) {
2080 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2081 VM_ALLOC_NULL_OK);
2082 if (rtm == NULL) {
2083 for (j = 0; j < i; j++) {
2084 vm_page_free(marray[j]);
2086 vm_object_drop(object);
2087 marray[0] = m;
2088 *reqpage = 0;
2089 return 1;
2091 marray[i] = rtm;
2092 ++i;
2093 ++tpindex;
2095 vm_object_drop(object);
2096 } else {
2097 i = 0;
2101 * Assign requested page
2103 marray[i] = m;
2104 *reqpage = i;
2105 ++i;
2108 * Scan forwards for read-ahead pages
2110 tpindex = pindex + 1;
2111 endpindex = tpindex + rahead;
2112 if (endpindex > object->size)
2113 endpindex = object->size;
2115 vm_object_hold(object);
2116 while (tpindex < endpindex) {
2117 if (vm_page_lookup(object, tpindex))
2118 break;
2119 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_SYSTEM |
2120 VM_ALLOC_NULL_OK);
2121 if (rtm == NULL)
2122 break;
2123 marray[i] = rtm;
2124 ++i;
2125 ++tpindex;
2127 vm_object_drop(object);
2129 return (i);
2132 #endif
2135 * vm_prefault() provides a quick way of clustering pagefaults into a
2136 * processes address space. It is a "cousin" of pmap_object_init_pt,
2137 * except it runs at page fault time instead of mmap time.
2139 * vm.fast_fault Enables pre-faulting zero-fill pages
2141 * vm.prefault_pages Number of pages (1/2 negative, 1/2 positive) to
2142 * prefault. Scan stops in either direction when
2143 * a page is found to already exist.
2145 * This code used to be per-platform pmap_prefault(). It is now
2146 * machine-independent and enhanced to also pre-fault zero-fill pages
2147 * (see vm.fast_fault) as well as make them writable, which greatly
2148 * reduces the number of page faults programs incur.
2150 * Application performance when pre-faulting zero-fill pages is heavily
2151 * dependent on the application. Very tiny applications like /bin/echo
2152 * lose a little performance while applications of any appreciable size
2153 * gain performance. Prefaulting multiple pages also reduces SMP
2154 * congestion and can improve SMP performance significantly.
2156 * NOTE! prot may allow writing but this only applies to the top level
2157 * object. If we wind up mapping a page extracted from a backing
2158 * object we have to make sure it is read-only.
2160 * NOTE! The caller has already handled any COW operations on the
2161 * vm_map_entry via the normal fault code. Do NOT call this
2162 * shortcut unless the normal fault code has run on this entry.
2164 * The related map must be locked.
2165 * No other requirements.
2167 static int vm_prefault_pages = 8;
2168 SYSCTL_INT(_vm, OID_AUTO, prefault_pages, CTLFLAG_RW, &vm_prefault_pages, 0,
2169 "Maximum number of pages to pre-fault");
2170 static int vm_fast_fault = 1;
2171 SYSCTL_INT(_vm, OID_AUTO, fast_fault, CTLFLAG_RW, &vm_fast_fault, 0,
2172 "Burst fault zero-fill regions");
2175 * Set PG_NOSYNC if the map entry indicates so, but only if the page
2176 * is not already dirty by other means. This will prevent passive
2177 * filesystem syncing as well as 'sync' from writing out the page.
2179 static void
2180 vm_set_nosync(vm_page_t m, vm_map_entry_t entry)
2182 if (entry->eflags & MAP_ENTRY_NOSYNC) {
2183 if (m->dirty == 0)
2184 vm_page_flag_set(m, PG_NOSYNC);
2185 } else {
2186 vm_page_flag_clear(m, PG_NOSYNC);
2190 static void
2191 vm_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry, int prot,
2192 int fault_flags)
2194 struct lwp *lp;
2195 vm_page_t m;
2196 vm_offset_t addr;
2197 vm_pindex_t index;
2198 vm_pindex_t pindex;
2199 vm_object_t object;
2200 int pprot;
2201 int i;
2202 int noneg;
2203 int nopos;
2204 int maxpages;
2207 * Get stable max count value, disabled if set to 0
2209 maxpages = vm_prefault_pages;
2210 cpu_ccfence();
2211 if (maxpages <= 0)
2212 return;
2215 * We do not currently prefault mappings that use virtual page
2216 * tables. We do not prefault foreign pmaps.
2218 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2219 return;
2220 lp = curthread->td_lwp;
2221 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2222 return;
2225 * Limit pre-fault count to 1024 pages.
2227 if (maxpages > 1024)
2228 maxpages = 1024;
2230 object = entry->object.vm_object;
2231 KKASSERT(object != NULL);
2232 KKASSERT(object == entry->object.vm_object);
2233 vm_object_hold(object);
2234 vm_object_chain_acquire(object);
2236 noneg = 0;
2237 nopos = 0;
2238 for (i = 0; i < maxpages; ++i) {
2239 vm_object_t lobject;
2240 vm_object_t nobject;
2241 int allocated = 0;
2242 int error;
2245 * This can eat a lot of time on a heavily contended
2246 * machine so yield on the tick if needed.
2248 if ((i & 7) == 7)
2249 lwkt_yield();
2252 * Calculate the page to pre-fault, stopping the scan in
2253 * each direction separately if the limit is reached.
2255 if (i & 1) {
2256 if (noneg)
2257 continue;
2258 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2259 } else {
2260 if (nopos)
2261 continue;
2262 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2264 if (addr < entry->start) {
2265 noneg = 1;
2266 if (noneg && nopos)
2267 break;
2268 continue;
2270 if (addr >= entry->end) {
2271 nopos = 1;
2272 if (noneg && nopos)
2273 break;
2274 continue;
2278 * Skip pages already mapped, and stop scanning in that
2279 * direction. When the scan terminates in both directions
2280 * we are done.
2282 if (pmap_prefault_ok(pmap, addr) == 0) {
2283 if (i & 1)
2284 noneg = 1;
2285 else
2286 nopos = 1;
2287 if (noneg && nopos)
2288 break;
2289 continue;
2293 * Follow the VM object chain to obtain the page to be mapped
2294 * into the pmap.
2296 * If we reach the terminal object without finding a page
2297 * and we determine it would be advantageous, then allocate
2298 * a zero-fill page for the base object. The base object
2299 * is guaranteed to be OBJT_DEFAULT for this case.
2301 * In order to not have to check the pager via *haspage*()
2302 * we stop if any non-default object is encountered. e.g.
2303 * a vnode or swap object would stop the loop.
2305 index = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2306 lobject = object;
2307 pindex = index;
2308 pprot = prot;
2310 KKASSERT(lobject == entry->object.vm_object);
2311 /*vm_object_hold(lobject); implied */
2313 while ((m = vm_page_lookup_busy_try(lobject, pindex,
2314 TRUE, &error)) == NULL) {
2315 if (lobject->type != OBJT_DEFAULT)
2316 break;
2317 if (lobject->backing_object == NULL) {
2318 if (vm_fast_fault == 0)
2319 break;
2320 if ((prot & VM_PROT_WRITE) == 0 ||
2321 vm_page_count_min(0)) {
2322 break;
2326 * NOTE: Allocated from base object
2328 m = vm_page_alloc(object, index,
2329 VM_ALLOC_NORMAL |
2330 VM_ALLOC_ZERO |
2331 VM_ALLOC_USE_GD |
2332 VM_ALLOC_NULL_OK);
2333 if (m == NULL)
2334 break;
2335 allocated = 1;
2336 pprot = prot;
2337 /* lobject = object .. not needed */
2338 break;
2340 if (lobject->backing_object_offset & PAGE_MASK)
2341 break;
2342 nobject = lobject->backing_object;
2343 vm_object_hold(nobject);
2344 KKASSERT(nobject == lobject->backing_object);
2345 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
2346 if (lobject != object) {
2347 vm_object_lock_swap();
2348 vm_object_drop(lobject);
2350 lobject = nobject;
2351 pprot &= ~VM_PROT_WRITE;
2352 vm_object_chain_acquire(lobject);
2356 * NOTE: A non-NULL (m) will be associated with lobject if
2357 * it was found there, otherwise it is probably a
2358 * zero-fill page associated with the base object.
2360 * Give-up if no page is available.
2362 if (m == NULL) {
2363 if (lobject != object) {
2364 if (object->backing_object != lobject)
2365 vm_object_hold(object->backing_object);
2366 vm_object_chain_release_all(
2367 object->backing_object, lobject);
2368 if (object->backing_object != lobject)
2369 vm_object_drop(object->backing_object);
2370 vm_object_drop(lobject);
2372 break;
2376 * The object must be marked dirty if we are mapping a
2377 * writable page. m->object is either lobject or object,
2378 * both of which are still held. Do this before we
2379 * potentially drop the object.
2381 if (pprot & VM_PROT_WRITE)
2382 vm_object_set_writeable_dirty(m->object);
2385 * Do not conditionalize on PG_RAM. If pages are present in
2386 * the VM system we assume optimal caching. If caching is
2387 * not optimal the I/O gravy train will be restarted when we
2388 * hit an unavailable page. We do not want to try to restart
2389 * the gravy train now because we really don't know how much
2390 * of the object has been cached. The cost for restarting
2391 * the gravy train should be low (since accesses will likely
2392 * be I/O bound anyway).
2394 if (lobject != object) {
2395 if (object->backing_object != lobject)
2396 vm_object_hold(object->backing_object);
2397 vm_object_chain_release_all(object->backing_object,
2398 lobject);
2399 if (object->backing_object != lobject)
2400 vm_object_drop(object->backing_object);
2401 vm_object_drop(lobject);
2405 * Enter the page into the pmap if appropriate. If we had
2406 * allocated the page we have to place it on a queue. If not
2407 * we just have to make sure it isn't on the cache queue
2408 * (pages on the cache queue are not allowed to be mapped).
2410 if (allocated) {
2412 * Page must be zerod.
2414 if ((m->flags & PG_ZERO) == 0) {
2415 vm_page_zero_fill(m);
2416 } else {
2417 #ifdef PMAP_DEBUG
2418 pmap_page_assertzero(
2419 VM_PAGE_TO_PHYS(m));
2420 #endif
2421 vm_page_flag_clear(m, PG_ZERO);
2422 mycpu->gd_cnt.v_ozfod++;
2424 mycpu->gd_cnt.v_zfod++;
2425 m->valid = VM_PAGE_BITS_ALL;
2428 * Handle dirty page case
2430 if (pprot & VM_PROT_WRITE)
2431 vm_set_nosync(m, entry);
2432 pmap_enter(pmap, addr, m, pprot, 0, entry);
2433 mycpu->gd_cnt.v_vm_faults++;
2434 if (curthread->td_lwp)
2435 ++curthread->td_lwp->lwp_ru.ru_minflt;
2436 vm_page_deactivate(m);
2437 if (pprot & VM_PROT_WRITE) {
2438 /*vm_object_set_writeable_dirty(m->object);*/
2439 vm_set_nosync(m, entry);
2440 if (fault_flags & VM_FAULT_DIRTY) {
2441 vm_page_dirty(m);
2442 /*XXX*/
2443 swap_pager_unswapped(m);
2446 vm_page_wakeup(m);
2447 } else if (error) {
2448 /* couldn't busy page, no wakeup */
2449 } else if (
2450 ((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2451 (m->flags & PG_FICTITIOUS) == 0) {
2453 * A fully valid page not undergoing soft I/O can
2454 * be immediately entered into the pmap.
2456 if ((m->queue - m->pc) == PQ_CACHE)
2457 vm_page_deactivate(m);
2458 if (pprot & VM_PROT_WRITE) {
2459 /*vm_object_set_writeable_dirty(m->object);*/
2460 vm_set_nosync(m, entry);
2461 if (fault_flags & VM_FAULT_DIRTY) {
2462 vm_page_dirty(m);
2463 /*XXX*/
2464 swap_pager_unswapped(m);
2467 if (pprot & VM_PROT_WRITE)
2468 vm_set_nosync(m, entry);
2469 pmap_enter(pmap, addr, m, pprot, 0, entry);
2470 mycpu->gd_cnt.v_vm_faults++;
2471 if (curthread->td_lwp)
2472 ++curthread->td_lwp->lwp_ru.ru_minflt;
2473 vm_page_wakeup(m);
2474 } else {
2475 vm_page_wakeup(m);
2478 vm_object_chain_release(object);
2479 vm_object_drop(object);
2482 static void
2483 vm_prefault_quick(pmap_t pmap, vm_offset_t addra,
2484 vm_map_entry_t entry, int prot, int fault_flags)
2486 struct lwp *lp;
2487 vm_page_t m;
2488 vm_offset_t addr;
2489 vm_pindex_t pindex;
2490 vm_object_t object;
2491 int i;
2492 int noneg;
2493 int nopos;
2494 int maxpages;
2497 * Get stable max count value, disabled if set to 0
2499 maxpages = vm_prefault_pages;
2500 cpu_ccfence();
2501 if (maxpages <= 0)
2502 return;
2505 * We do not currently prefault mappings that use virtual page
2506 * tables. We do not prefault foreign pmaps.
2508 if (entry->maptype == VM_MAPTYPE_VPAGETABLE)
2509 return;
2510 lp = curthread->td_lwp;
2511 if (lp == NULL || (pmap != vmspace_pmap(lp->lwp_vmspace)))
2512 return;
2515 * Limit pre-fault count to 1024 pages.
2517 if (maxpages > 1024)
2518 maxpages = 1024;
2520 object = entry->object.vm_object;
2521 ASSERT_LWKT_TOKEN_HELD(vm_object_token(object));
2522 KKASSERT(object->backing_object == NULL);
2524 noneg = 0;
2525 nopos = 0;
2526 for (i = 0; i < maxpages; ++i) {
2527 int error;
2530 * Calculate the page to pre-fault, stopping the scan in
2531 * each direction separately if the limit is reached.
2533 if (i & 1) {
2534 if (noneg)
2535 continue;
2536 addr = addra - ((i + 1) >> 1) * PAGE_SIZE;
2537 } else {
2538 if (nopos)
2539 continue;
2540 addr = addra + ((i + 2) >> 1) * PAGE_SIZE;
2542 if (addr < entry->start) {
2543 noneg = 1;
2544 if (noneg && nopos)
2545 break;
2546 continue;
2548 if (addr >= entry->end) {
2549 nopos = 1;
2550 if (noneg && nopos)
2551 break;
2552 continue;
2556 * Skip pages already mapped, and stop scanning in that
2557 * direction. When the scan terminates in both directions
2558 * we are done.
2560 if (pmap_prefault_ok(pmap, addr) == 0) {
2561 if (i & 1)
2562 noneg = 1;
2563 else
2564 nopos = 1;
2565 if (noneg && nopos)
2566 break;
2567 continue;
2571 * Follow the VM object chain to obtain the page to be mapped
2572 * into the pmap. This version of the prefault code only
2573 * works with terminal objects.
2575 * WARNING! We cannot call swap_pager_unswapped() with a
2576 * shared token.
2578 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
2580 m = vm_page_lookup_busy_try(object, pindex, TRUE, &error);
2581 if (m == NULL || error)
2582 continue;
2584 if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) &&
2585 (m->flags & PG_FICTITIOUS) == 0 &&
2586 ((m->flags & PG_SWAPPED) == 0 ||
2587 (prot & VM_PROT_WRITE) == 0 ||
2588 (fault_flags & VM_FAULT_DIRTY) == 0)) {
2590 * A fully valid page not undergoing soft I/O can
2591 * be immediately entered into the pmap.
2593 if ((m->queue - m->pc) == PQ_CACHE)
2594 vm_page_deactivate(m);
2595 if (prot & VM_PROT_WRITE) {
2596 vm_object_set_writeable_dirty(m->object);
2597 vm_set_nosync(m, entry);
2598 if (fault_flags & VM_FAULT_DIRTY) {
2599 vm_page_dirty(m);
2600 /*XXX*/
2601 swap_pager_unswapped(m);
2604 pmap_enter(pmap, addr, m, prot, 0, entry);
2605 mycpu->gd_cnt.v_vm_faults++;
2606 if (curthread->td_lwp)
2607 ++curthread->td_lwp->lwp_ru.ru_minflt;
2609 vm_page_wakeup(m);