2 * Copyright (c) 1991, 1993
3 * The Regents of the University of California. All rights reserved.
4 * Copyright (c) 1994 John S. Dyson
6 * Copyright (c) 1994 David Greenman
10 * This code is derived from software contributed to Berkeley by
11 * The Mach Operating System project at Carnegie-Mellon University.
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. All advertising materials mentioning features or use of this software
22 * must display the following acknowledgement:
23 * This product includes software developed by the University of
24 * California, Berkeley and its contributors.
25 * 4. Neither the name of the University nor the names of its contributors
26 * may be used to endorse or promote products derived from this software
27 * without specific prior written permission.
29 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
30 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
31 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
32 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
33 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
34 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
35 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
37 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
38 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94
44 * Copyright (c) 1987, 1990 Carnegie-Mellon University.
45 * All rights reserved.
47 * Authors: Avadis Tevanian, Jr., Michael Wayne Young
49 * Permission to use, copy, modify and distribute this software and
50 * its documentation is hereby granted, provided that both the copyright
51 * notice and this permission notice appear in all copies of the
52 * software, derivative works or modified versions, and any portions
53 * thereof, and that both notices appear in supporting documentation.
55 * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
56 * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
57 * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
59 * Carnegie Mellon requests users of this software to return to
61 * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU
62 * School of Computer Science
63 * Carnegie Mellon University
64 * Pittsburgh PA 15213-3890
66 * any improvements or extensions that they make and grant Carnegie the
67 * rights to redistribute these changes.
69 * $FreeBSD: src/sys/vm/vm_fault.c,v 1.108.2.8 2002/02/26 05:49:27 silby Exp $
70 * $DragonFly: src/sys/vm/vm_fault.c,v 1.35 2007/01/06 22:35:47 dillon Exp $
74 * Page fault handling module.
77 #include <sys/param.h>
78 #include <sys/systm.h>
79 #include <sys/kernel.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
84 #include <sys/vkernel.h>
85 #include <sys/sfbuf.h>
89 #include <vm/vm_param.h>
91 #include <vm/vm_map.h>
92 #include <vm/vm_object.h>
93 #include <vm/vm_page.h>
94 #include <vm/vm_pageout.h>
95 #include <vm/vm_kern.h>
96 #include <vm/vm_pager.h>
97 #include <vm/vnode_pager.h>
98 #include <vm/vm_extern.h>
100 #include <sys/thread2.h>
101 #include <vm/vm_page2.h>
103 #define VM_FAULT_READ_AHEAD 8
104 #define VM_FAULT_READ_BEHIND 7
105 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
113 vm_object_t first_object;
114 vm_prot_t first_prot;
116 vm_map_entry_t entry;
117 int lookup_still_valid;
126 static int vm_fault_object(struct faultstate *, vm_pindex_t, vm_prot_t);
127 static int vm_fault_vpagetable(struct faultstate *, vm_pindex_t *, vpte_t);
128 static int vm_fault_additional_pages (vm_page_t, int, int, vm_page_t *, int *);
129 static int vm_fault_ratelimit(struct vmspace *);
132 release_page(struct faultstate *fs)
134 vm_page_wakeup(fs->m);
135 vm_page_deactivate(fs->m);
140 unlock_map(struct faultstate *fs)
142 if (fs->lookup_still_valid) {
143 vm_map_lookup_done(fs->map, fs->entry, 0);
144 fs->lookup_still_valid = FALSE;
149 * Clean up after a successful call to vm_fault_object() so another call
150 * to vm_fault_object() can be made.
153 _cleanup_successful_fault(struct faultstate *fs, int relock)
155 if (fs->object != fs->first_object) {
156 vm_page_free(fs->first_m);
157 vm_object_pip_wakeup(fs->object);
160 fs->object = fs->first_object;
161 if (relock && fs->lookup_still_valid == FALSE) {
162 vm_map_lock_read(fs->map);
163 fs->lookup_still_valid = TRUE;
168 _unlock_things(struct faultstate *fs, int dealloc)
170 vm_object_pip_wakeup(fs->first_object);
171 _cleanup_successful_fault(fs, 0);
173 vm_object_deallocate(fs->first_object);
176 if (fs->vp != NULL) {
182 #define unlock_things(fs) _unlock_things(fs, 0)
183 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
184 #define cleanup_successful_fault(fs) _cleanup_successful_fault(fs, 1)
189 * Determine if the pager for the current object *might* contain the page.
191 * We only need to try the pager if this is not a default object (default
192 * objects are zero-fill and have no real pager), and if we are not taking
193 * a wiring fault or if the FS entry is wired.
195 #define TRYPAGER(fs) \
196 (fs->object->type != OBJT_DEFAULT && \
197 (((fs->fault_flags & VM_FAULT_WIRE_MASK) == 0) || fs->wired))
202 * Handle a page fault occuring at the given address, requiring the given
203 * permissions, in the map specified. If successful, the page is inserted
204 * into the associated physical map.
206 * NOTE: The given address should be truncated to the proper page address.
208 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
209 * a standard error specifying why the fault is fatal is returned.
211 * The map in question must be referenced, and remains so.
212 * The caller may hold no locks.
215 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
218 vm_pindex_t first_pindex;
219 struct faultstate fs;
221 mycpu->gd_cnt.v_vm_faults++;
225 fs.fault_flags = fault_flags;
229 * Find the vm_map_entry representing the backing store and resolve
230 * the top level object and page index. This may have the side
231 * effect of executing a copy-on-write on the map entry and/or
232 * creating a shadow object, but will not COW any actual VM pages.
234 * On success fs.map is left read-locked and various other fields
235 * are initialized but not otherwise referenced or locked.
237 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
238 * if the map entry is a virtual page table and also writable,
239 * so we can set the 'A'accessed bit in the virtual page table entry.
242 result = vm_map_lookup(&fs.map, vaddr, fault_type,
243 &fs.entry, &fs.first_object,
244 &first_pindex, &fs.first_prot, &fs.wired);
247 * If the lookup failed or the map protections are incompatible,
248 * the fault generally fails. However, if the caller is trying
249 * to do a user wiring we have more work to do.
251 if (result != KERN_SUCCESS) {
252 if (result != KERN_PROTECTION_FAILURE)
254 if ((fs.fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)
258 * If we are user-wiring a r/w segment, and it is COW, then
259 * we need to do the COW operation. Note that we don't
260 * currently COW RO sections now, because it is NOT desirable
261 * to COW .text. We simply keep .text from ever being COW'ed
262 * and take the heat that one cannot debug wired .text sections.
264 result = vm_map_lookup(&fs.map, vaddr,
265 VM_PROT_READ|VM_PROT_WRITE|
266 VM_PROT_OVERRIDE_WRITE,
267 &fs.entry, &fs.first_object,
268 &first_pindex, &fs.first_prot,
270 if (result != KERN_SUCCESS)
274 * If we don't COW now, on a user wire, the user will never
275 * be able to write to the mapping. If we don't make this
276 * restriction, the bookkeeping would be nearly impossible.
278 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
279 fs.entry->max_protection &= ~VM_PROT_WRITE;
283 * fs.map is read-locked
285 * Misc checks. Save the map generation number to detect races.
287 fs.map_generation = fs.map->timestamp;
289 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
290 panic("vm_fault: fault on nofault entry, addr: %lx",
295 * A system map entry may return a NULL object. No object means
296 * no pager means an unrecoverable kernel fault.
298 if (fs.first_object == NULL) {
299 panic("vm_fault: unrecoverable fault at %p in entry %p",
300 (void *)vaddr, fs.entry);
304 * Make a reference to this object to prevent its disposal while we
305 * are messing with it. Once we have the reference, the map is free
306 * to be diddled. Since objects reference their shadows (and copies),
307 * they will stay around as well.
309 * Bump the paging-in-progress count to prevent size changes (e.g.
310 * truncation operations) during I/O. This must be done after
311 * obtaining the vnode lock in order to avoid possible deadlocks.
313 vm_object_reference(fs.first_object);
314 fs.vp = vnode_pager_lock(fs.first_object);
315 vm_object_pip_add(fs.first_object, 1);
317 fs.lookup_still_valid = TRUE;
319 fs.object = fs.first_object; /* so unlock_and_deallocate works */
322 * If the entry is wired we cannot change the page protection.
325 fault_type = fs.first_prot;
328 * The page we want is at (first_object, first_pindex), but if the
329 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
330 * page table to figure out the actual pindex.
332 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
335 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
336 result = vm_fault_vpagetable(&fs, &first_pindex,
337 fs.entry->aux.master_pde);
338 if (result == KERN_TRY_AGAIN)
340 if (result != KERN_SUCCESS)
345 * Now we have the actual (object, pindex), fault in the page. If
346 * vm_fault_object() fails it will unlock and deallocate the FS
347 * data. If it succeeds everything remains locked and fs->object
348 * will have an additinal PIP count if it is not equal to
351 result = vm_fault_object(&fs, first_pindex, fault_type);
353 if (result == KERN_TRY_AGAIN)
355 if (result != KERN_SUCCESS)
359 * On success vm_fault_object() does not unlock or deallocate, and fs.m
360 * will contain a busied page.
362 * Enter the page into the pmap and do pmap-related adjustments.
365 pmap_enter(fs.map->pmap, vaddr, fs.m, fs.prot, fs.wired);
367 if (((fs.fault_flags & VM_FAULT_WIRE_MASK) == 0) && (fs.wired == 0)) {
368 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
371 vm_page_flag_clear(fs.m, PG_ZERO);
372 vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
373 if (fs.fault_flags & VM_FAULT_HOLD)
377 * If the page is not wired down, then put it where the pageout daemon
380 if (fs.fault_flags & VM_FAULT_WIRE_MASK) {
384 vm_page_unwire(fs.m, 1);
386 vm_page_activate(fs.m);
389 if (curthread->td_lwp) {
391 curthread->td_lwp->lwp_ru.ru_majflt++;
393 curthread->td_lwp->lwp_ru.ru_minflt++;
398 * Unlock everything, and return
400 vm_page_wakeup(fs.m);
401 vm_object_deallocate(fs.first_object);
403 return (KERN_SUCCESS);
407 * Fault-in the specified virtual address in the specified map, doing all
408 * necessary manipulation of the object store and all necessary I/O. Return
409 * a held VM page or NULL, and set *errorp. The related pmap is not
412 * Since the pmap is not updated, this routine may not be used to wire
416 vm_fault_page(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
417 int fault_flags, int *errorp)
420 vm_pindex_t first_pindex;
421 struct faultstate fs;
423 mycpu->gd_cnt.v_vm_faults++;
427 fs.fault_flags = fault_flags;
428 KKASSERT((fault_flags & VM_FAULT_WIRE_MASK) == 0);
432 * Find the vm_map_entry representing the backing store and resolve
433 * the top level object and page index. This may have the side
434 * effect of executing a copy-on-write on the map entry and/or
435 * creating a shadow object, but will not COW any actual VM pages.
437 * On success fs.map is left read-locked and various other fields
438 * are initialized but not otherwise referenced or locked.
440 * NOTE! vm_map_lookup will upgrade the fault_type to VM_FAULT_WRITE
441 * if the map entry is a virtual page table and also writable,
442 * so we can set the 'A'accessed bit in the virtual page table entry.
445 result = vm_map_lookup(&fs.map, vaddr, fault_type,
446 &fs.entry, &fs.first_object,
447 &first_pindex, &fs.first_prot, &fs.wired);
449 if (result != KERN_SUCCESS) {
455 * fs.map is read-locked
457 * Misc checks. Save the map generation number to detect races.
459 fs.map_generation = fs.map->timestamp;
461 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
462 panic("vm_fault: fault on nofault entry, addr: %lx",
467 * A system map entry may return a NULL object. No object means
468 * no pager means an unrecoverable kernel fault.
470 if (fs.first_object == NULL) {
471 panic("vm_fault: unrecoverable fault at %p in entry %p",
472 (void *)vaddr, fs.entry);
476 * Make a reference to this object to prevent its disposal while we
477 * are messing with it. Once we have the reference, the map is free
478 * to be diddled. Since objects reference their shadows (and copies),
479 * they will stay around as well.
481 * Bump the paging-in-progress count to prevent size changes (e.g.
482 * truncation operations) during I/O. This must be done after
483 * obtaining the vnode lock in order to avoid possible deadlocks.
485 vm_object_reference(fs.first_object);
486 fs.vp = vnode_pager_lock(fs.first_object);
487 vm_object_pip_add(fs.first_object, 1);
489 fs.lookup_still_valid = TRUE;
491 fs.object = fs.first_object; /* so unlock_and_deallocate works */
494 * If the entry is wired we cannot change the page protection.
497 fault_type = fs.first_prot;
500 * The page we want is at (first_object, first_pindex), but if the
501 * vm_map_entry is VM_MAPTYPE_VPAGETABLE we have to traverse the
502 * page table to figure out the actual pindex.
504 * NOTE! DEVELOPMENT IN PROGRESS, THIS IS AN INITIAL IMPLEMENTATION
507 if (fs.entry->maptype == VM_MAPTYPE_VPAGETABLE) {
508 result = vm_fault_vpagetable(&fs, &first_pindex,
509 fs.entry->aux.master_pde);
510 if (result == KERN_TRY_AGAIN)
512 if (result != KERN_SUCCESS) {
519 * Now we have the actual (object, pindex), fault in the page. If
520 * vm_fault_object() fails it will unlock and deallocate the FS
521 * data. If it succeeds everything remains locked and fs->object
522 * will have an additinal PIP count if it is not equal to
525 result = vm_fault_object(&fs, first_pindex, fault_type);
527 if (result == KERN_TRY_AGAIN)
529 if (result != KERN_SUCCESS) {
535 * On success vm_fault_object() does not unlock or deallocate, and fs.m
536 * will contain a busied page.
541 * Return a held page. We are not doing any pmap manipulation so do
544 vm_page_flag_clear(fs.m, PG_ZERO);
545 vm_page_flag_set(fs.m, PG_REFERENCED);
549 * Unbusy the page by activating it. It remains held and will not
552 vm_page_activate(fs.m);
554 if (curthread->td_lwp) {
556 curthread->td_lwp->lwp_ru.ru_majflt++;
558 curthread->td_lwp->lwp_ru.ru_minflt++;
563 * Unlock everything, and return the held page.
565 vm_page_wakeup(fs.m);
566 vm_object_deallocate(fs.first_object);
573 * Translate the virtual page number (first_pindex) that is relative
574 * to the address space into a logical page number that is relative to the
575 * backing object. Use the virtual page table pointed to by (vpte).
577 * This implements an N-level page table. Any level can terminate the
578 * scan by setting VPTE_PS. A linear mapping is accomplished by setting
579 * VPTE_PS in the master page directory entry set via mcontrol(MADV_SETMAP).
583 vm_fault_vpagetable(struct faultstate *fs, vm_pindex_t *pindex, vpte_t vpte)
586 int vshift = 32 - PAGE_SHIFT; /* page index bits remaining */
587 int result = KERN_SUCCESS;
590 if ((vpte & VPTE_V) == 0) {
591 unlock_and_deallocate(fs);
592 return (KERN_FAILURE);
594 if ((vpte & VPTE_PS) || vshift == 0)
596 KKASSERT(vshift >= VPTE_PAGE_BITS);
599 * Get the page table page
601 result = vm_fault_object(fs, vpte >> PAGE_SHIFT, VM_PROT_READ);
602 if (result != KERN_SUCCESS)
606 * Process the returned fs.m and look up the page table
607 * entry in the page table page.
609 vshift -= VPTE_PAGE_BITS;
610 sf = sf_buf_alloc(fs->m, SFB_CPUPRIVATE);
611 vpte = *((vpte_t *)sf_buf_kva(sf) +
612 ((*pindex >> vshift) & VPTE_PAGE_MASK));
614 vm_page_flag_set(fs->m, PG_REFERENCED);
615 vm_page_activate(fs->m);
616 vm_page_wakeup(fs->m);
617 cleanup_successful_fault(fs);
620 * Combine remaining address bits with the vpte.
622 *pindex = (vpte >> PAGE_SHIFT) +
623 (*pindex & ((1 << vshift) - 1));
624 return (KERN_SUCCESS);
629 * Do all operations required to fault-in (fs.first_object, pindex). Run
630 * through the shadow chain as necessary and do required COW or virtual
631 * copy operations. The caller has already fully resolved the vm_map_entry
632 * and, if appropriate, has created a copy-on-write layer. All we need to
633 * do is iterate the object chain.
635 * On failure (fs) is unlocked and deallocated and the caller may return or
636 * retry depending on the failure code. On success (fs) is NOT unlocked or
637 * deallocated, fs.m will contained a resolved, busied page, and fs.object
638 * will have an additional PIP count if it is not equal to fs.first_object.
642 vm_fault_object(struct faultstate *fs,
643 vm_pindex_t first_pindex, vm_prot_t fault_type)
645 vm_object_t next_object;
646 vm_page_t marray[VM_FAULT_READ];
650 fs->prot = fs->first_prot;
651 fs->object = fs->first_object;
652 pindex = first_pindex;
656 * If the object is dead, we stop here
658 if (fs->object->flags & OBJ_DEAD) {
659 unlock_and_deallocate(fs);
660 return (KERN_PROTECTION_FAILURE);
664 * See if page is resident. spl protection is required
665 * to avoid an interrupt unbusy/free race against our
666 * lookup. We must hold the protection through a page
667 * allocation or busy.
670 fs->m = vm_page_lookup(fs->object, pindex);
674 * Wait/Retry if the page is busy. We have to do this
675 * if the page is busy via either PG_BUSY or
676 * vm_page_t->busy because the vm_pager may be using
677 * vm_page_t->busy for pageouts ( and even pageins if
678 * it is the vnode pager ), and we could end up trying
679 * to pagein and pageout the same page simultaneously.
681 * We can theoretically allow the busy case on a read
682 * fault if the page is marked valid, but since such
683 * pages are typically already pmap'd, putting that
684 * special case in might be more effort then it is
685 * worth. We cannot under any circumstances mess
686 * around with a vm_page_t->busy page except, perhaps,
689 if ((fs->m->flags & PG_BUSY) || fs->m->busy) {
691 vm_page_sleep_busy(fs->m, TRUE, "vmpfw");
692 mycpu->gd_cnt.v_intrans++;
693 vm_object_deallocate(fs->first_object);
695 return (KERN_TRY_AGAIN);
699 * If reactivating a page from PQ_CACHE we may have
702 queue = fs->m->queue;
703 vm_page_unqueue_nowakeup(fs->m);
705 if ((queue - fs->m->pc) == PQ_CACHE &&
706 vm_page_count_severe()) {
707 vm_page_activate(fs->m);
708 unlock_and_deallocate(fs);
711 return (KERN_TRY_AGAIN);
715 * Mark page busy for other processes, and the
716 * pagedaemon. If it still isn't completely valid
717 * (readable), jump to readrest, else we found the
718 * page and can return.
720 * We can release the spl once we have marked the
726 if (((fs->m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
727 fs->m->object != &kernel_object) {
730 break; /* break to PAGE HAS BEEN FOUND */
734 * Page is not resident, If this is the search termination
735 * or the pager might contain the page, allocate a new page.
737 * NOTE: We are still in a critical section.
739 if (TRYPAGER(fs) || fs->object == fs->first_object) {
741 * If the page is beyond the object size we fail
743 if (pindex >= fs->object->size) {
745 unlock_and_deallocate(fs);
746 return (KERN_PROTECTION_FAILURE);
752 if (fs->didlimit == 0 && curproc != NULL) {
755 limticks = vm_fault_ratelimit(curproc->p_vmspace);
758 unlock_and_deallocate(fs);
759 tsleep(curproc, 0, "vmrate", limticks);
761 return (KERN_TRY_AGAIN);
766 * Allocate a new page for this object/offset pair.
769 if (!vm_page_count_severe()) {
770 fs->m = vm_page_alloc(fs->object, pindex,
771 (fs->vp || fs->object->backing_object) ? VM_ALLOC_NORMAL : VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
775 unlock_and_deallocate(fs);
777 return (KERN_TRY_AGAIN);
784 * We have found a valid page or we have allocated a new page.
785 * The page thus may not be valid or may not be entirely
788 * Attempt to fault-in the page if there is a chance that the
789 * pager has it, and potentially fault in additional pages
792 * We are NOT in splvm here and if TRYPAGER is true then
793 * fs.m will be non-NULL and will be PG_BUSY for us.
800 u_char behavior = vm_map_entry_behavior(fs->entry);
802 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
807 if (behind > VM_FAULT_READ_BEHIND)
808 behind = VM_FAULT_READ_BEHIND;
810 ahead = fs->object->size - pindex;
813 if (ahead > VM_FAULT_READ_AHEAD)
814 ahead = VM_FAULT_READ_AHEAD;
817 if ((fs->first_object->type != OBJT_DEVICE) &&
818 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
819 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
820 pindex >= fs->entry->lastr &&
821 pindex < fs->entry->lastr + VM_FAULT_READ))
823 vm_pindex_t firstpindex, tmppindex;
825 if (first_pindex < 2 * VM_FAULT_READ)
828 firstpindex = first_pindex - 2 * VM_FAULT_READ;
831 * note: partially valid pages cannot be
832 * included in the lookahead - NFS piecemeal
833 * writes will barf on it badly.
835 * spl protection is required to avoid races
836 * between the lookup and an interrupt
837 * unbusy/free sequence occuring prior to
841 for (tmppindex = first_pindex - 1;
842 tmppindex >= firstpindex;
847 mt = vm_page_lookup(fs->first_object, tmppindex);
848 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
851 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
856 vm_page_test_dirty(mt);
858 vm_page_protect(mt, VM_PROT_NONE);
859 vm_page_deactivate(mt);
871 * now we find out if any other pages should be paged
872 * in at this time this routine checks to see if the
873 * pages surrounding this fault reside in the same
874 * object as the page for this fault. If they do,
875 * then they are faulted in also into the object. The
876 * array "marray" returned contains an array of
877 * vm_page_t structs where one of them is the
878 * vm_page_t passed to the routine. The reqpage
879 * return value is the index into the marray for the
880 * vm_page_t passed to the routine.
882 * fs.m plus the additional pages are PG_BUSY'd.
884 faultcount = vm_fault_additional_pages(
885 fs->m, behind, ahead, marray, &reqpage);
888 * update lastr imperfectly (we do not know how much
889 * getpages will actually read), but good enough.
891 fs->entry->lastr = pindex + faultcount - behind;
894 * Call the pager to retrieve the data, if any, after
895 * releasing the lock on the map. We hold a ref on
896 * fs.object and the pages are PG_BUSY'd.
901 rv = vm_pager_get_pages(fs->object, marray,
902 faultcount, reqpage);
907 if (rv == VM_PAGER_OK) {
909 * Found the page. Leave it busy while we play
914 * Relookup in case pager changed page. Pager
915 * is responsible for disposition of old page
918 * XXX other code segments do relookups too.
919 * It's a bad abstraction that needs to be
922 fs->m = vm_page_lookup(fs->object, pindex);
924 unlock_and_deallocate(fs);
925 return (KERN_TRY_AGAIN);
929 break; /* break to PAGE HAS BEEN FOUND */
933 * Remove the bogus page (which does not exist at this
934 * object/offset); before doing so, we must get back
935 * our object lock to preserve our invariant.
937 * Also wake up any other process that may want to bring
940 * If this is the top-level object, we must leave the
941 * busy page to prevent another process from rushing
942 * past us, and inserting the page in that object at
943 * the same time that we are.
945 if (rv == VM_PAGER_ERROR) {
947 kprintf("vm_fault: pager read error, pid %d (%s)\n", curproc->p_pid, curproc->p_comm);
949 kprintf("vm_fault: pager read error, thread %p (%s)\n", curthread, curproc->p_comm);
952 * Data outside the range of the pager or an I/O error
955 * XXX - the check for kernel_map is a kludge to work
956 * around having the machine panic on a kernel space
957 * fault w/ I/O error.
959 if (((fs->map != &kernel_map) && (rv == VM_PAGER_ERROR)) ||
960 (rv == VM_PAGER_BAD)) {
963 unlock_and_deallocate(fs);
964 if (rv == VM_PAGER_ERROR)
965 return (KERN_FAILURE);
967 return (KERN_PROTECTION_FAILURE);
970 if (fs->object != fs->first_object) {
974 * XXX - we cannot just fall out at this
975 * point, m has been freed and is invalid!
981 * We get here if the object has a default pager (or unwiring)
982 * or the pager doesn't have the page.
984 if (fs->object == fs->first_object)
988 * Move on to the next object. Lock the next object before
989 * unlocking the current one.
991 pindex += OFF_TO_IDX(fs->object->backing_object_offset);
992 next_object = fs->object->backing_object;
993 if (next_object == NULL) {
995 * If there's no object left, fill the page in the top
998 if (fs->object != fs->first_object) {
999 vm_object_pip_wakeup(fs->object);
1001 fs->object = fs->first_object;
1002 pindex = first_pindex;
1003 fs->m = fs->first_m;
1008 * Zero the page if necessary and mark it valid.
1010 if ((fs->m->flags & PG_ZERO) == 0) {
1011 vm_page_zero_fill(fs->m);
1013 mycpu->gd_cnt.v_ozfod++;
1015 mycpu->gd_cnt.v_zfod++;
1016 fs->m->valid = VM_PAGE_BITS_ALL;
1017 break; /* break to PAGE HAS BEEN FOUND */
1019 if (fs->object != fs->first_object) {
1020 vm_object_pip_wakeup(fs->object);
1022 KASSERT(fs->object != next_object, ("object loop %p", next_object));
1023 fs->object = next_object;
1024 vm_object_pip_add(fs->object, 1);
1028 KASSERT((fs->m->flags & PG_BUSY) != 0,
1029 ("vm_fault: not busy after main loop"));
1032 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
1037 * If the page is being written, but isn't already owned by the
1038 * top-level object, we have to copy it into a new page owned by the
1041 if (fs->object != fs->first_object) {
1043 * We only really need to copy if we want to write it.
1045 if (fault_type & VM_PROT_WRITE) {
1047 * This allows pages to be virtually copied from a
1048 * backing_object into the first_object, where the
1049 * backing object has no other refs to it, and cannot
1050 * gain any more refs. Instead of a bcopy, we just
1051 * move the page from the backing object to the
1052 * first object. Note that we must mark the page
1053 * dirty in the first object so that it will go out
1054 * to swap when needed.
1056 if (fs->map_generation == fs->map->timestamp &&
1058 * Only one shadow object
1060 (fs->object->shadow_count == 1) &&
1062 * No COW refs, except us
1064 (fs->object->ref_count == 1) &&
1066 * No one else can look this object up
1068 (fs->object->handle == NULL) &&
1070 * No other ways to look the object up
1072 ((fs->object->type == OBJT_DEFAULT) ||
1073 (fs->object->type == OBJT_SWAP)) &&
1075 * We don't chase down the shadow chain
1077 (fs->object == fs->first_object->backing_object) &&
1080 * grab the lock if we need to
1082 (fs->lookup_still_valid ||
1083 lockmgr(&fs->map->lock, LK_EXCLUSIVE|LK_NOWAIT) == 0)
1086 fs->lookup_still_valid = 1;
1088 * get rid of the unnecessary page
1090 vm_page_protect(fs->first_m, VM_PROT_NONE);
1091 vm_page_free(fs->first_m);
1095 * grab the page and put it into the
1096 * process'es object. The page is
1097 * automatically made dirty.
1099 vm_page_rename(fs->m, fs->first_object, first_pindex);
1100 fs->first_m = fs->m;
1101 vm_page_busy(fs->first_m);
1103 mycpu->gd_cnt.v_cow_optim++;
1106 * Oh, well, lets copy it.
1108 vm_page_copy(fs->m, fs->first_m);
1113 * We no longer need the old page or object.
1119 * fs->object != fs->first_object due to above
1122 vm_object_pip_wakeup(fs->object);
1125 * Only use the new page below...
1128 mycpu->gd_cnt.v_cow_faults++;
1129 fs->m = fs->first_m;
1130 fs->object = fs->first_object;
1131 pindex = first_pindex;
1134 * If it wasn't a write fault avoid having to copy
1135 * the page by mapping it read-only.
1137 fs->prot &= ~VM_PROT_WRITE;
1142 * We may have had to unlock a map to do I/O. If we did then
1143 * lookup_still_valid will be FALSE. If the map generation count
1144 * also changed then all sorts of things could have happened while
1145 * we were doing the I/O and we need to retry.
1148 if (!fs->lookup_still_valid &&
1149 (fs->map->timestamp != fs->map_generation)) {
1151 unlock_and_deallocate(fs);
1152 return (KERN_TRY_AGAIN);
1156 * Put this page into the physical map. We had to do the unlock above
1157 * because pmap_enter may cause other faults. We don't put the page
1158 * back on the active queue until later so that the page-out daemon
1159 * won't find us (yet).
1161 if (fs->prot & VM_PROT_WRITE) {
1162 vm_page_flag_set(fs->m, PG_WRITEABLE);
1163 vm_object_set_writeable_dirty(fs->m->object);
1166 * If the fault is a write, we know that this page is being
1167 * written NOW so dirty it explicitly to save on
1168 * pmap_is_modified() calls later.
1170 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
1171 * if the page is already dirty to prevent data written with
1172 * the expectation of being synced from not being synced.
1173 * Likewise if this entry does not request NOSYNC then make
1174 * sure the page isn't marked NOSYNC. Applications sharing
1175 * data should use the same flags to avoid ping ponging.
1177 * Also tell the backing pager, if any, that it should remove
1178 * any swap backing since the page is now dirty.
1180 if (fs->entry->eflags & MAP_ENTRY_NOSYNC) {
1181 if (fs->m->dirty == 0)
1182 vm_page_flag_set(fs->m, PG_NOSYNC);
1184 vm_page_flag_clear(fs->m, PG_NOSYNC);
1186 if (fs->fault_flags & VM_FAULT_DIRTY) {
1188 vm_page_dirty(fs->m);
1189 vm_pager_page_unswapped(fs->m);
1195 * Page had better still be busy. We are still locked up and
1196 * fs->object will have another PIP reference if it is not equal
1197 * to fs->first_object.
1199 KASSERT(fs->m->flags & PG_BUSY,
1200 ("vm_fault: page %p not busy!", fs->m));
1203 * Sanity check: page must be completely valid or it is not fit to
1204 * map into user space. vm_pager_get_pages() ensures this.
1206 if (fs->m->valid != VM_PAGE_BITS_ALL) {
1207 vm_page_zero_invalid(fs->m, TRUE);
1208 kprintf("Warning: page %p partially invalid on fault\n", fs->m);
1211 return (KERN_SUCCESS);
1215 * quick version of vm_fault
1218 vm_fault_quick(caddr_t v, int prot)
1222 if (prot & VM_PROT_WRITE)
1223 r = subyte(v, fubyte(v));
1230 * Wire down a range of virtual addresses in a map. The entry in question
1231 * should be marked in-transition and the map must be locked. We must
1232 * release the map temporarily while faulting-in the page to avoid a
1233 * deadlock. Note that the entry may be clipped while we are blocked but
1234 * will never be freed.
1237 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
1239 boolean_t fictitious;
1247 pmap = vm_map_pmap(map);
1248 start = entry->start;
1250 fictitious = entry->object.vm_object &&
1251 (entry->object.vm_object->type == OBJT_DEVICE);
1257 * We simulate a fault to get the page and enter it in the physical
1260 for (va = start; va < end; va += PAGE_SIZE) {
1262 rv = vm_fault(map, va, VM_PROT_READ,
1263 VM_FAULT_USER_WIRE);
1265 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
1266 VM_FAULT_CHANGE_WIRING);
1269 while (va > start) {
1271 if ((pa = pmap_extract(pmap, va)) == 0)
1273 pmap_change_wiring(pmap, va, FALSE);
1275 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1282 return (KERN_SUCCESS);
1286 * Unwire a range of virtual addresses in a map. The map should be
1290 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
1292 boolean_t fictitious;
1299 pmap = vm_map_pmap(map);
1300 start = entry->start;
1302 fictitious = entry->object.vm_object &&
1303 (entry->object.vm_object->type == OBJT_DEVICE);
1306 * Since the pages are wired down, we must be able to get their
1307 * mappings from the physical map system.
1309 for (va = start; va < end; va += PAGE_SIZE) {
1310 pa = pmap_extract(pmap, va);
1312 pmap_change_wiring(pmap, va, FALSE);
1314 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1320 * Reduce the rate at which memory is allocated to a process based
1321 * on the perceived load on the VM system. As the load increases
1322 * the allocation burst rate goes down and the delay increases.
1324 * Rate limiting does not apply when faulting active or inactive
1325 * pages. When faulting 'cache' pages, rate limiting only applies
1326 * if the system currently has a severe page deficit.
1328 * XXX vm_pagesupply should be increased when a page is freed.
1330 * We sleep up to 1/10 of a second.
1333 vm_fault_ratelimit(struct vmspace *vmspace)
1335 if (vm_load_enable == 0)
1337 if (vmspace->vm_pagesupply > 0) {
1338 --vmspace->vm_pagesupply;
1342 if (vm_load_debug) {
1343 kprintf("load %-4d give %d pgs, wait %d, pid %-5d (%s)\n",
1345 (1000 - vm_load ) / 10, vm_load * hz / 10000,
1346 curproc->p_pid, curproc->p_comm);
1349 vmspace->vm_pagesupply = (1000 - vm_load) / 10;
1350 return(vm_load * hz / 10000);
1355 * vm_fault_copy_entry
1357 * Copy all of the pages from a wired-down map entry to another.
1359 * In/out conditions:
1360 * The source and destination maps must be locked for write.
1361 * The source map entry must be wired down (or be a sharing map
1362 * entry corresponding to a main map entry that is wired down).
1366 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1367 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1369 vm_object_t dst_object;
1370 vm_object_t src_object;
1371 vm_ooffset_t dst_offset;
1372 vm_ooffset_t src_offset;
1382 src_object = src_entry->object.vm_object;
1383 src_offset = src_entry->offset;
1386 * Create the top-level object for the destination entry. (Doesn't
1387 * actually shadow anything - we copy the pages directly.)
1389 vm_map_entry_allocate_object(dst_entry);
1390 dst_object = dst_entry->object.vm_object;
1392 prot = dst_entry->max_protection;
1395 * Loop through all of the pages in the entry's range, copying each
1396 * one from the source object (it should be there) to the destination
1399 for (vaddr = dst_entry->start, dst_offset = 0;
1400 vaddr < dst_entry->end;
1401 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1404 * Allocate a page in the destination object
1407 dst_m = vm_page_alloc(dst_object,
1408 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1409 if (dst_m == NULL) {
1412 } while (dst_m == NULL);
1415 * Find the page in the source object, and copy it in.
1416 * (Because the source is wired down, the page will be in
1419 src_m = vm_page_lookup(src_object,
1420 OFF_TO_IDX(dst_offset + src_offset));
1422 panic("vm_fault_copy_wired: page missing");
1424 vm_page_copy(src_m, dst_m);
1427 * Enter it in the pmap...
1430 vm_page_flag_clear(dst_m, PG_ZERO);
1431 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1432 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1435 * Mark it no longer busy, and put it on the active list.
1437 vm_page_activate(dst_m);
1438 vm_page_wakeup(dst_m);
1444 * This routine checks around the requested page for other pages that
1445 * might be able to be faulted in. This routine brackets the viable
1446 * pages for the pages to be paged in.
1449 * m, rbehind, rahead
1452 * marray (array of vm_page_t), reqpage (index of requested page)
1455 * number of pages in marray
1458 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1459 vm_page_t *marray, int *reqpage)
1463 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1465 int cbehind, cahead;
1471 * we don't fault-ahead for device pager
1473 if (object->type == OBJT_DEVICE) {
1480 * if the requested page is not available, then give up now
1483 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1487 if ((cbehind == 0) && (cahead == 0)) {
1493 if (rahead > cahead) {
1497 if (rbehind > cbehind) {
1502 * try to do any readahead that we might have free pages for.
1504 if ((rahead + rbehind) >
1505 ((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1506 pagedaemon_wakeup();
1513 * scan backward for the read behind pages -- in memory
1515 * Assume that if the page is not found an interrupt will not
1516 * create it. Theoretically interrupts can only remove (busy)
1517 * pages, not create new associations.
1520 if (rbehind > pindex) {
1524 startpindex = pindex - rbehind;
1528 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1529 if (vm_page_lookup( object, tpindex)) {
1530 startpindex = tpindex + 1;
1537 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1539 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1542 for (j = 0; j < i; j++) {
1543 vm_page_free(marray[j]);
1559 /* page offset of the required page */
1562 tpindex = pindex + 1;
1566 * scan forward for the read ahead pages
1568 endpindex = tpindex + rahead;
1569 if (endpindex > object->size)
1570 endpindex = object->size;
1573 for( ; tpindex < endpindex; i++, tpindex++) {
1575 if (vm_page_lookup(object, tpindex)) {
1579 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1588 /* return number of bytes of pages */