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.
71 * Page fault handling module.
74 #include <sys/cdefs.h>
75 __FBSDID("$FreeBSD$");
77 #include "opt_ktrace.h"
80 #include <sys/param.h>
81 #include <sys/systm.h>
82 #include <sys/kernel.h>
85 #include <sys/resourcevar.h>
86 #include <sys/rwlock.h>
87 #include <sys/sysctl.h>
88 #include <sys/vmmeter.h>
89 #include <sys/vnode.h>
91 #include <sys/ktrace.h>
95 #include <vm/vm_param.h>
97 #include <vm/vm_map.h>
98 #include <vm/vm_object.h>
99 #include <vm/vm_page.h>
100 #include <vm/vm_pageout.h>
101 #include <vm/vm_kern.h>
102 #include <vm/vm_pager.h>
103 #include <vm/vm_extern.h>
107 #define PAGEORDER_SIZE (PFBAK+PFFOR)
109 static int prefault_pageorder[] = {
110 -1 * PAGE_SIZE, 1 * PAGE_SIZE,
111 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
112 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
113 -4 * PAGE_SIZE, 4 * PAGE_SIZE
116 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
117 static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
119 #define VM_FAULT_READ_BEHIND 8
120 #define VM_FAULT_READ_MAX (1 + VM_FAULT_READ_AHEAD_MAX)
121 #define VM_FAULT_NINCR (VM_FAULT_READ_MAX / VM_FAULT_READ_BEHIND)
122 #define VM_FAULT_SUM (VM_FAULT_NINCR * (VM_FAULT_NINCR + 1) / 2)
123 #define VM_FAULT_CACHE_BEHIND (VM_FAULT_READ_BEHIND * VM_FAULT_SUM)
130 vm_object_t first_object;
131 vm_pindex_t first_pindex;
133 vm_map_entry_t entry;
134 int lookup_still_valid;
138 static void vm_fault_cache_behind(const struct faultstate *fs, int distance);
141 release_page(struct faultstate *fs)
144 vm_page_xunbusy(fs->m);
146 vm_page_deactivate(fs->m);
147 vm_page_unlock(fs->m);
152 unlock_map(struct faultstate *fs)
155 if (fs->lookup_still_valid) {
156 vm_map_lookup_done(fs->map, fs->entry);
157 fs->lookup_still_valid = FALSE;
162 unlock_and_deallocate(struct faultstate *fs)
165 vm_object_pip_wakeup(fs->object);
166 VM_OBJECT_WUNLOCK(fs->object);
167 if (fs->object != fs->first_object) {
168 VM_OBJECT_WLOCK(fs->first_object);
169 vm_page_lock(fs->first_m);
170 vm_page_free(fs->first_m);
171 vm_page_unlock(fs->first_m);
172 vm_object_pip_wakeup(fs->first_object);
173 VM_OBJECT_WUNLOCK(fs->first_object);
176 vm_object_deallocate(fs->first_object);
178 if (fs->vp != NULL) {
185 * TRYPAGER - used by vm_fault to calculate whether the pager for the
186 * current object *might* contain the page.
188 * default objects are zero-fill, there is no real pager.
190 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
191 ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 || wired))
196 * Handle a page fault occurring at the given address,
197 * requiring the given permissions, in the map specified.
198 * If successful, the page is inserted into the
199 * associated physical map.
201 * NOTE: the given address should be truncated to the
202 * proper page address.
204 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
205 * a standard error specifying why the fault is fatal is returned.
207 * The map in question must be referenced, and remains so.
208 * Caller may hold no locks.
211 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
218 if ((td->td_pflags & TDP_NOFAULTING) != 0)
219 return (KERN_PROTECTION_FAILURE);
221 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
222 ktrfault(vaddr, fault_type);
224 result = vm_fault_hold(map, trunc_page(vaddr), fault_type, fault_flags,
227 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
234 vm_fault_hold(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
235 int fault_flags, vm_page_t *m_hold)
239 int alloc_req, era, faultcount, nera, reqpage, result;
240 boolean_t growstack, is_first_object_locked, wired;
242 vm_object_t next_object;
243 vm_page_t marray[VM_FAULT_READ_MAX];
245 struct faultstate fs;
251 PCPU_INC(cnt.v_vm_faults);
253 faultcount = reqpage = 0;
258 * Find the backing store object and offset into it to begin the
262 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
263 &fs.first_object, &fs.first_pindex, &prot, &wired);
264 if (result != KERN_SUCCESS) {
265 if (growstack && result == KERN_INVALID_ADDRESS &&
267 result = vm_map_growstack(curproc, vaddr);
268 if (result != KERN_SUCCESS)
269 return (KERN_FAILURE);
276 map_generation = fs.map->timestamp;
278 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
279 panic("vm_fault: fault on nofault entry, addr: %lx",
283 if (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION &&
284 fs.entry->wiring_thread != curthread) {
285 vm_map_unlock_read(fs.map);
287 if (vm_map_lookup_entry(fs.map, vaddr, &fs.entry) &&
288 (fs.entry->eflags & MAP_ENTRY_IN_TRANSITION)) {
289 fs.entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
290 vm_map_unlock_and_wait(fs.map, 0);
292 vm_map_unlock(fs.map);
297 * Make a reference to this object to prevent its disposal while we
298 * are messing with it. Once we have the reference, the map is free
299 * to be diddled. Since objects reference their shadows (and copies),
300 * they will stay around as well.
302 * Bump the paging-in-progress count to prevent size changes (e.g.
303 * truncation operations) during I/O. This must be done after
304 * obtaining the vnode lock in order to avoid possible deadlocks.
306 VM_OBJECT_WLOCK(fs.first_object);
307 vm_object_reference_locked(fs.first_object);
308 vm_object_pip_add(fs.first_object, 1);
310 fs.lookup_still_valid = TRUE;
313 fault_type = prot | (fault_type & VM_PROT_COPY);
318 * Search for the page at object/offset.
320 fs.object = fs.first_object;
321 fs.pindex = fs.first_pindex;
324 * If the object is dead, we stop here
326 if (fs.object->flags & OBJ_DEAD) {
327 unlock_and_deallocate(&fs);
328 return (KERN_PROTECTION_FAILURE);
332 * See if page is resident
334 fs.m = vm_page_lookup(fs.object, fs.pindex);
337 * check for page-based copy on write.
338 * We check fs.object == fs.first_object so
339 * as to ensure the legacy COW mechanism is
340 * used when the page in question is part of
341 * a shadow object. Otherwise, vm_page_cowfault()
342 * removes the page from the backing object,
343 * which is not what we want.
347 (fault_type & VM_PROT_WRITE) &&
348 (fs.object == fs.first_object)) {
349 vm_page_cowfault(fs.m);
350 unlock_and_deallocate(&fs);
355 * Wait/Retry if the page is busy. We have to do this
356 * if the page is either exclusive or shared busy
357 * because the vm_pager may be using read busy for
358 * pageouts (and even pageins if it is the vnode
359 * pager), and we could end up trying to pagein and
360 * pageout the same page simultaneously.
362 * We can theoretically allow the busy case on a read
363 * fault if the page is marked valid, but since such
364 * pages are typically already pmap'd, putting that
365 * special case in might be more effort then it is
366 * worth. We cannot under any circumstances mess
367 * around with a shared busied page except, perhaps,
370 if (vm_page_busied(fs.m)) {
372 * Reference the page before unlocking and
373 * sleeping so that the page daemon is less
374 * likely to reclaim it.
376 vm_page_aflag_set(fs.m, PGA_REFERENCED);
377 vm_page_unlock(fs.m);
378 if (fs.object != fs.first_object) {
379 if (!VM_OBJECT_TRYWLOCK(
381 VM_OBJECT_WUNLOCK(fs.object);
382 VM_OBJECT_WLOCK(fs.first_object);
383 VM_OBJECT_WLOCK(fs.object);
385 vm_page_lock(fs.first_m);
386 vm_page_free(fs.first_m);
387 vm_page_unlock(fs.first_m);
388 vm_object_pip_wakeup(fs.first_object);
389 VM_OBJECT_WUNLOCK(fs.first_object);
393 if (fs.m == vm_page_lookup(fs.object,
395 vm_page_sleep_if_busy(fs.m, "vmpfw");
397 vm_object_pip_wakeup(fs.object);
398 VM_OBJECT_WUNLOCK(fs.object);
399 PCPU_INC(cnt.v_intrans);
400 vm_object_deallocate(fs.first_object);
403 vm_page_remque(fs.m);
404 vm_page_unlock(fs.m);
407 * Mark page busy for other processes, and the
408 * pagedaemon. If it still isn't completely valid
409 * (readable), jump to readrest, else break-out ( we
413 if (fs.m->valid != VM_PAGE_BITS_ALL)
419 * Page is not resident, If this is the search termination
420 * or the pager might contain the page, allocate a new page.
422 if (TRYPAGER || fs.object == fs.first_object) {
423 if (fs.pindex >= fs.object->size) {
424 unlock_and_deallocate(&fs);
425 return (KERN_PROTECTION_FAILURE);
429 * Allocate a new page for this object/offset pair.
431 * Unlocked read of the p_flag is harmless. At
432 * worst, the P_KILLED might be not observed
433 * there, and allocation can fail, causing
434 * restart and new reading of the p_flag.
437 if (!vm_page_count_severe() || P_KILLED(curproc)) {
438 #if VM_NRESERVLEVEL > 0
439 if ((fs.object->flags & OBJ_COLORED) == 0) {
440 fs.object->flags |= OBJ_COLORED;
441 fs.object->pg_color = atop(vaddr) -
445 alloc_req = P_KILLED(curproc) ?
446 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
447 if (fs.object->type != OBJT_VNODE &&
448 fs.object->backing_object == NULL)
449 alloc_req |= VM_ALLOC_ZERO;
450 fs.m = vm_page_alloc(fs.object, fs.pindex,
454 unlock_and_deallocate(&fs);
457 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
463 * We have found a valid page or we have allocated a new page.
464 * The page thus may not be valid or may not be entirely
467 * Attempt to fault-in the page if there is a chance that the
468 * pager has it, and potentially fault in additional pages
473 u_char behavior = vm_map_entry_behavior(fs.entry);
475 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
479 } else if (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL) {
481 ahead = atop(fs.entry->end - vaddr) - 1;
482 if (ahead > VM_FAULT_READ_AHEAD_MAX)
483 ahead = VM_FAULT_READ_AHEAD_MAX;
484 if (fs.pindex == fs.entry->next_read)
485 vm_fault_cache_behind(&fs,
489 * If this is a sequential page fault, then
490 * arithmetically increase the number of pages
491 * in the read-ahead window. Otherwise, reset
492 * the read-ahead window to its smallest size.
494 behind = atop(vaddr - fs.entry->start);
495 if (behind > VM_FAULT_READ_BEHIND)
496 behind = VM_FAULT_READ_BEHIND;
497 ahead = atop(fs.entry->end - vaddr) - 1;
498 era = fs.entry->read_ahead;
499 if (fs.pindex == fs.entry->next_read) {
501 if (nera > VM_FAULT_READ_AHEAD_MAX)
502 nera = VM_FAULT_READ_AHEAD_MAX;
506 if (era == VM_FAULT_READ_AHEAD_MAX)
507 vm_fault_cache_behind(&fs,
508 VM_FAULT_CACHE_BEHIND);
509 } else if (ahead > VM_FAULT_READ_AHEAD_MIN)
510 ahead = VM_FAULT_READ_AHEAD_MIN;
512 fs.entry->read_ahead = ahead;
516 * Call the pager to retrieve the data, if any, after
517 * releasing the lock on the map. We hold a ref on
518 * fs.object and the pages are exclusive busied.
522 if (fs.object->type == OBJT_VNODE) {
523 vp = fs.object->handle;
526 else if (fs.vp != NULL) {
530 locked = VOP_ISLOCKED(vp);
532 if (locked != LK_EXCLUSIVE)
534 /* Do not sleep for vnode lock while fs.m is busy */
535 error = vget(vp, locked | LK_CANRECURSE |
536 LK_NOWAIT, curthread);
540 unlock_and_deallocate(&fs);
541 error = vget(vp, locked | LK_RETRY |
542 LK_CANRECURSE, curthread);
546 ("vm_fault: vget failed"));
552 KASSERT(fs.vp == NULL || !fs.map->system_map,
553 ("vm_fault: vnode-backed object mapped by system map"));
556 * now we find out if any other pages should be paged
557 * in at this time this routine checks to see if the
558 * pages surrounding this fault reside in the same
559 * object as the page for this fault. If they do,
560 * then they are faulted in also into the object. The
561 * array "marray" returned contains an array of
562 * vm_page_t structs where one of them is the
563 * vm_page_t passed to the routine. The reqpage
564 * return value is the index into the marray for the
565 * vm_page_t passed to the routine.
567 * fs.m plus the additional pages are exclusive busied.
569 faultcount = vm_fault_additional_pages(
570 fs.m, behind, ahead, marray, &reqpage);
573 vm_pager_get_pages(fs.object, marray, faultcount,
574 reqpage) : VM_PAGER_FAIL;
576 if (rv == VM_PAGER_OK) {
578 * Found the page. Leave it busy while we play
583 * Relookup in case pager changed page. Pager
584 * is responsible for disposition of old page
587 fs.m = vm_page_lookup(fs.object, fs.pindex);
589 unlock_and_deallocate(&fs);
594 break; /* break to PAGE HAS BEEN FOUND */
597 * Remove the bogus page (which does not exist at this
598 * object/offset); before doing so, we must get back
599 * our object lock to preserve our invariant.
601 * Also wake up any other process that may want to bring
604 * If this is the top-level object, we must leave the
605 * busy page to prevent another process from rushing
606 * past us, and inserting the page in that object at
607 * the same time that we are.
609 if (rv == VM_PAGER_ERROR)
610 printf("vm_fault: pager read error, pid %d (%s)\n",
611 curproc->p_pid, curproc->p_comm);
613 * Data outside the range of the pager or an I/O error
616 * XXX - the check for kernel_map is a kludge to work
617 * around having the machine panic on a kernel space
618 * fault w/ I/O error.
620 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
621 (rv == VM_PAGER_BAD)) {
624 vm_page_unlock(fs.m);
626 unlock_and_deallocate(&fs);
627 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
629 if (fs.object != fs.first_object) {
632 vm_page_unlock(fs.m);
635 * XXX - we cannot just fall out at this
636 * point, m has been freed and is invalid!
642 * We get here if the object has default pager (or unwiring)
643 * or the pager doesn't have the page.
645 if (fs.object == fs.first_object)
649 * Move on to the next object. Lock the next object before
650 * unlocking the current one.
652 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
653 next_object = fs.object->backing_object;
654 if (next_object == NULL) {
656 * If there's no object left, fill the page in the top
659 if (fs.object != fs.first_object) {
660 vm_object_pip_wakeup(fs.object);
661 VM_OBJECT_WUNLOCK(fs.object);
663 fs.object = fs.first_object;
664 fs.pindex = fs.first_pindex;
666 VM_OBJECT_WLOCK(fs.object);
671 * Zero the page if necessary and mark it valid.
673 if ((fs.m->flags & PG_ZERO) == 0) {
674 pmap_zero_page(fs.m);
676 PCPU_INC(cnt.v_ozfod);
678 PCPU_INC(cnt.v_zfod);
679 fs.m->valid = VM_PAGE_BITS_ALL;
680 break; /* break to PAGE HAS BEEN FOUND */
682 KASSERT(fs.object != next_object,
683 ("object loop %p", next_object));
684 VM_OBJECT_WLOCK(next_object);
685 vm_object_pip_add(next_object, 1);
686 if (fs.object != fs.first_object)
687 vm_object_pip_wakeup(fs.object);
688 VM_OBJECT_WUNLOCK(fs.object);
689 fs.object = next_object;
693 vm_page_assert_xbusied(fs.m);
696 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
701 * If the page is being written, but isn't already owned by the
702 * top-level object, we have to copy it into a new page owned by the
705 if (fs.object != fs.first_object) {
707 * We only really need to copy if we want to write it.
709 if ((fault_type & (VM_PROT_COPY | VM_PROT_WRITE)) != 0) {
711 * This allows pages to be virtually copied from a
712 * backing_object into the first_object, where the
713 * backing object has no other refs to it, and cannot
714 * gain any more refs. Instead of a bcopy, we just
715 * move the page from the backing object to the
716 * first object. Note that we must mark the page
717 * dirty in the first object so that it will go out
718 * to swap when needed.
720 is_first_object_locked = FALSE;
723 * Only one shadow object
725 (fs.object->shadow_count == 1) &&
727 * No COW refs, except us
729 (fs.object->ref_count == 1) &&
731 * No one else can look this object up
733 (fs.object->handle == NULL) &&
735 * No other ways to look the object up
737 ((fs.object->type == OBJT_DEFAULT) ||
738 (fs.object->type == OBJT_SWAP)) &&
739 (is_first_object_locked = VM_OBJECT_TRYWLOCK(fs.first_object)) &&
741 * We don't chase down the shadow chain
743 fs.object == fs.first_object->backing_object) {
745 * get rid of the unnecessary page
747 vm_page_lock(fs.first_m);
748 vm_page_free(fs.first_m);
749 vm_page_unlock(fs.first_m);
751 * grab the page and put it into the
752 * process'es object. The page is
753 * automatically made dirty.
755 if (vm_page_rename(fs.m, fs.first_object,
757 unlock_and_deallocate(&fs);
763 PCPU_INC(cnt.v_cow_optim);
766 * Oh, well, lets copy it.
768 pmap_copy_page(fs.m, fs.first_m);
769 fs.first_m->valid = VM_PAGE_BITS_ALL;
770 if (wired && (fault_flags &
771 VM_FAULT_CHANGE_WIRING) == 0) {
772 vm_page_lock(fs.first_m);
773 vm_page_wire(fs.first_m);
774 vm_page_unlock(fs.first_m);
777 vm_page_unwire(fs.m, FALSE);
778 vm_page_unlock(fs.m);
781 * We no longer need the old page or object.
786 * fs.object != fs.first_object due to above
789 vm_object_pip_wakeup(fs.object);
790 VM_OBJECT_WUNLOCK(fs.object);
792 * Only use the new page below...
794 fs.object = fs.first_object;
795 fs.pindex = fs.first_pindex;
797 if (!is_first_object_locked)
798 VM_OBJECT_WLOCK(fs.object);
799 PCPU_INC(cnt.v_cow_faults);
802 prot &= ~VM_PROT_WRITE;
807 * We must verify that the maps have not changed since our last
810 if (!fs.lookup_still_valid) {
811 vm_object_t retry_object;
812 vm_pindex_t retry_pindex;
813 vm_prot_t retry_prot;
815 if (!vm_map_trylock_read(fs.map)) {
817 unlock_and_deallocate(&fs);
820 fs.lookup_still_valid = TRUE;
821 if (fs.map->timestamp != map_generation) {
822 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
823 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
826 * If we don't need the page any longer, put it on the inactive
827 * list (the easiest thing to do here). If no one needs it,
828 * pageout will grab it eventually.
830 if (result != KERN_SUCCESS) {
832 unlock_and_deallocate(&fs);
835 * If retry of map lookup would have blocked then
836 * retry fault from start.
838 if (result == KERN_FAILURE)
842 if ((retry_object != fs.first_object) ||
843 (retry_pindex != fs.first_pindex)) {
845 unlock_and_deallocate(&fs);
850 * Check whether the protection has changed or the object has
851 * been copied while we left the map unlocked. Changing from
852 * read to write permission is OK - we leave the page
853 * write-protected, and catch the write fault. Changing from
854 * write to read permission means that we can't mark the page
855 * write-enabled after all.
861 * If the page was filled by a pager, update the map entry's
862 * last read offset. Since the pager does not return the
863 * actual set of pages that it read, this update is based on
864 * the requested set. Typically, the requested and actual
867 * XXX The following assignment modifies the map
868 * without holding a write lock on it.
871 fs.entry->next_read = fs.pindex + faultcount - reqpage;
873 if ((prot & VM_PROT_WRITE) != 0 ||
874 (fault_flags & VM_FAULT_DIRTY) != 0) {
875 vm_object_set_writeable_dirty(fs.object);
878 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
879 * if the page is already dirty to prevent data written with
880 * the expectation of being synced from not being synced.
881 * Likewise if this entry does not request NOSYNC then make
882 * sure the page isn't marked NOSYNC. Applications sharing
883 * data should use the same flags to avoid ping ponging.
885 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
886 if (fs.m->dirty == 0)
887 fs.m->oflags |= VPO_NOSYNC;
889 fs.m->oflags &= ~VPO_NOSYNC;
893 * If the fault is a write, we know that this page is being
894 * written NOW so dirty it explicitly to save on
895 * pmap_is_modified() calls later.
897 * Also tell the backing pager, if any, that it should remove
898 * any swap backing since the page is now dirty.
900 if (((fault_type & VM_PROT_WRITE) != 0 &&
901 (fault_flags & VM_FAULT_CHANGE_WIRING) == 0) ||
902 (fault_flags & VM_FAULT_DIRTY) != 0) {
904 vm_pager_page_unswapped(fs.m);
908 vm_page_assert_xbusied(fs.m);
911 * Page must be completely valid or it is not fit to
912 * map into user space. vm_pager_get_pages() ensures this.
914 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
915 ("vm_fault: page %p partially invalid", fs.m));
916 VM_OBJECT_WUNLOCK(fs.object);
919 * Put this page into the physical map. We had to do the unlock above
920 * because pmap_enter() may sleep. We don't put the page
921 * back on the active queue until later so that the pageout daemon
922 * won't find it (yet).
924 pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
925 if ((fault_flags & VM_FAULT_CHANGE_WIRING) == 0 && wired == 0)
926 vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
927 VM_OBJECT_WLOCK(fs.object);
931 * If the page is not wired down, then put it where the pageout daemon
934 if (fault_flags & VM_FAULT_CHANGE_WIRING) {
938 vm_page_unwire(fs.m, 1);
940 vm_page_activate(fs.m);
941 if (m_hold != NULL) {
945 vm_page_unlock(fs.m);
946 vm_page_xunbusy(fs.m);
949 * Unlock everything, and return
951 unlock_and_deallocate(&fs);
953 PCPU_INC(cnt.v_io_faults);
954 curthread->td_ru.ru_majflt++;
956 curthread->td_ru.ru_minflt++;
958 return (KERN_SUCCESS);
962 * Speed up the reclamation of up to "distance" pages that precede the
963 * faulting pindex within the first object of the shadow chain.
966 vm_fault_cache_behind(const struct faultstate *fs, int distance)
968 vm_object_t first_object, object;
973 VM_OBJECT_ASSERT_WLOCKED(object);
974 first_object = fs->first_object;
975 if (first_object != object) {
976 if (!VM_OBJECT_TRYWLOCK(first_object)) {
977 VM_OBJECT_WUNLOCK(object);
978 VM_OBJECT_WLOCK(first_object);
979 VM_OBJECT_WLOCK(object);
982 /* Neither fictitious nor unmanaged pages can be cached. */
983 if ((first_object->flags & (OBJ_FICTITIOUS | OBJ_UNMANAGED)) == 0) {
984 if (fs->first_pindex < distance)
987 pindex = fs->first_pindex - distance;
988 if (pindex < OFF_TO_IDX(fs->entry->offset))
989 pindex = OFF_TO_IDX(fs->entry->offset);
990 m = first_object != object ? fs->first_m : fs->m;
991 vm_page_assert_xbusied(m);
992 m_prev = vm_page_prev(m);
993 while ((m = m_prev) != NULL && m->pindex >= pindex &&
994 m->valid == VM_PAGE_BITS_ALL) {
995 m_prev = vm_page_prev(m);
996 if (vm_page_busied(m))
999 if (m->hold_count == 0 && m->wire_count == 0) {
1001 vm_page_aflag_clear(m, PGA_REFERENCED);
1003 vm_page_deactivate(m);
1010 if (first_object != object)
1011 VM_OBJECT_WUNLOCK(first_object);
1015 * vm_fault_prefault provides a quick way of clustering
1016 * pagefaults into a processes address space. It is a "cousin"
1017 * of vm_map_pmap_enter, except it runs at page fault time instead
1021 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
1024 vm_offset_t addr, starta;
1029 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1032 object = entry->object.vm_object;
1034 starta = addra - PFBAK * PAGE_SIZE;
1035 if (starta < entry->start) {
1036 starta = entry->start;
1037 } else if (starta > addra) {
1041 for (i = 0; i < PAGEORDER_SIZE; i++) {
1042 vm_object_t backing_object, lobject;
1044 addr = addra + prefault_pageorder[i];
1045 if (addr > addra + (PFFOR * PAGE_SIZE))
1048 if (addr < starta || addr >= entry->end)
1051 if (!pmap_is_prefaultable(pmap, addr))
1054 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1056 VM_OBJECT_RLOCK(lobject);
1057 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1058 lobject->type == OBJT_DEFAULT &&
1059 (backing_object = lobject->backing_object) != NULL) {
1060 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1061 0, ("vm_fault_prefault: unaligned object offset"));
1062 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1063 VM_OBJECT_RLOCK(backing_object);
1064 VM_OBJECT_RUNLOCK(lobject);
1065 lobject = backing_object;
1068 * give-up when a page is not in memory
1071 VM_OBJECT_RUNLOCK(lobject);
1074 if (m->valid == VM_PAGE_BITS_ALL &&
1075 (m->flags & PG_FICTITIOUS) == 0)
1076 pmap_enter_quick(pmap, addr, m, entry->protection);
1077 VM_OBJECT_RUNLOCK(lobject);
1082 * Hold each of the physical pages that are mapped by the specified range of
1083 * virtual addresses, ["addr", "addr" + "len"), if those mappings are valid
1084 * and allow the specified types of access, "prot". If all of the implied
1085 * pages are successfully held, then the number of held pages is returned
1086 * together with pointers to those pages in the array "ma". However, if any
1087 * of the pages cannot be held, -1 is returned.
1090 vm_fault_quick_hold_pages(vm_map_t map, vm_offset_t addr, vm_size_t len,
1091 vm_prot_t prot, vm_page_t *ma, int max_count)
1093 vm_offset_t end, va;
1096 boolean_t pmap_failed;
1100 end = round_page(addr + len);
1101 addr = trunc_page(addr);
1104 * Check for illegal addresses.
1106 if (addr < vm_map_min(map) || addr > end || end > vm_map_max(map))
1109 count = howmany(end - addr, PAGE_SIZE);
1110 if (count > max_count)
1111 panic("vm_fault_quick_hold_pages: count > max_count");
1114 * Most likely, the physical pages are resident in the pmap, so it is
1115 * faster to try pmap_extract_and_hold() first.
1117 pmap_failed = FALSE;
1118 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE) {
1119 *mp = pmap_extract_and_hold(map->pmap, va, prot);
1122 else if ((prot & VM_PROT_WRITE) != 0 &&
1123 (*mp)->dirty != VM_PAGE_BITS_ALL) {
1125 * Explicitly dirty the physical page. Otherwise, the
1126 * caller's changes may go unnoticed because they are
1127 * performed through an unmanaged mapping or by a DMA
1130 * The object lock is not held here.
1131 * See vm_page_clear_dirty_mask().
1138 * One or more pages could not be held by the pmap. Either no
1139 * page was mapped at the specified virtual address or that
1140 * mapping had insufficient permissions. Attempt to fault in
1141 * and hold these pages.
1143 for (mp = ma, va = addr; va < end; mp++, va += PAGE_SIZE)
1144 if (*mp == NULL && vm_fault_hold(map, va, prot,
1145 VM_FAULT_NORMAL, mp) != KERN_SUCCESS)
1150 for (mp = ma; mp < ma + count; mp++)
1153 vm_page_unhold(*mp);
1154 vm_page_unlock(*mp);
1162 * Wire down a range of virtual addresses in a map.
1165 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1166 boolean_t fictitious)
1172 * We simulate a fault to get the page and enter it in the physical
1173 * map. For user wiring, we only ask for read access on currently
1174 * read-only sections.
1176 for (va = start; va < end; va += PAGE_SIZE) {
1177 rv = vm_fault(map, va, VM_PROT_NONE, VM_FAULT_CHANGE_WIRING);
1180 vm_fault_unwire(map, start, va, fictitious);
1184 return (KERN_SUCCESS);
1190 * Unwire a range of virtual addresses in a map.
1193 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1194 boolean_t fictitious)
1201 pmap = vm_map_pmap(map);
1204 * Since the pages are wired down, we must be able to get their
1205 * mappings from the physical map system.
1207 for (va = start; va < end; va += PAGE_SIZE) {
1208 pa = pmap_extract(pmap, va);
1210 pmap_change_wiring(pmap, va, FALSE);
1212 m = PHYS_TO_VM_PAGE(pa);
1214 vm_page_unwire(m, TRUE);
1223 * vm_fault_copy_entry
1225 * Create new shadow object backing dst_entry with private copy of
1226 * all underlying pages. When src_entry is equal to dst_entry,
1227 * function implements COW for wired-down map entry. Otherwise,
1228 * it forks wired entry into dst_map.
1230 * In/out conditions:
1231 * The source and destination maps must be locked for write.
1232 * The source map entry must be wired down (or be a sharing map
1233 * entry corresponding to a main map entry that is wired down).
1236 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1237 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1238 vm_ooffset_t *fork_charge)
1240 vm_object_t backing_object, dst_object, object, src_object;
1241 vm_pindex_t dst_pindex, pindex, src_pindex;
1242 vm_prot_t access, prot;
1246 boolean_t src_readonly, upgrade;
1252 upgrade = src_entry == dst_entry;
1254 src_object = src_entry->object.vm_object;
1255 src_pindex = OFF_TO_IDX(src_entry->offset);
1256 src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1259 * Create the top-level object for the destination entry. (Doesn't
1260 * actually shadow anything - we copy the pages directly.)
1262 dst_object = vm_object_allocate(OBJT_DEFAULT,
1263 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1264 #if VM_NRESERVLEVEL > 0
1265 dst_object->flags |= OBJ_COLORED;
1266 dst_object->pg_color = atop(dst_entry->start);
1269 VM_OBJECT_WLOCK(dst_object);
1270 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1271 ("vm_fault_copy_entry: vm_object not NULL"));
1272 dst_entry->object.vm_object = dst_object;
1273 dst_entry->offset = 0;
1274 dst_object->charge = dst_entry->end - dst_entry->start;
1275 if (fork_charge != NULL) {
1276 KASSERT(dst_entry->cred == NULL,
1277 ("vm_fault_copy_entry: leaked swp charge"));
1278 dst_object->cred = curthread->td_ucred;
1279 crhold(dst_object->cred);
1280 *fork_charge += dst_object->charge;
1282 dst_object->cred = dst_entry->cred;
1283 dst_entry->cred = NULL;
1285 access = prot = dst_entry->protection;
1287 * If not an upgrade, then enter the mappings in the pmap as
1288 * read and/or execute accesses. Otherwise, enter them as
1291 * A writeable large page mapping is only created if all of
1292 * the constituent small page mappings are modified. Marking
1293 * PTEs as modified on inception allows promotion to happen
1294 * without taking potentially large number of soft faults.
1297 access &= ~VM_PROT_WRITE;
1300 * Loop through all of the virtual pages within the entry's
1301 * range, copying each page from the source object to the
1302 * destination object. Since the source is wired, those pages
1303 * must exist. In contrast, the destination is pageable.
1304 * Since the destination object does share any backing storage
1305 * with the source object, all of its pages must be dirtied,
1306 * regardless of whether they can be written.
1308 for (vaddr = dst_entry->start, dst_pindex = 0;
1309 vaddr < dst_entry->end;
1310 vaddr += PAGE_SIZE, dst_pindex++) {
1313 * Allocate a page in the destination object.
1316 dst_m = vm_page_alloc(dst_object, dst_pindex,
1318 if (dst_m == NULL) {
1319 VM_OBJECT_WUNLOCK(dst_object);
1321 VM_OBJECT_WLOCK(dst_object);
1323 } while (dst_m == NULL);
1326 * Find the page in the source object, and copy it in.
1327 * (Because the source is wired down, the page will be in
1330 VM_OBJECT_RLOCK(src_object);
1331 object = src_object;
1332 pindex = src_pindex + dst_pindex;
1333 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1335 (backing_object = object->backing_object) != NULL) {
1337 * Allow fallback to backing objects if we are reading.
1339 VM_OBJECT_RLOCK(backing_object);
1340 pindex += OFF_TO_IDX(object->backing_object_offset);
1341 VM_OBJECT_RUNLOCK(object);
1342 object = backing_object;
1345 panic("vm_fault_copy_wired: page missing");
1346 pmap_copy_page(src_m, dst_m);
1347 VM_OBJECT_RUNLOCK(object);
1348 dst_m->valid = VM_PAGE_BITS_ALL;
1349 dst_m->dirty = VM_PAGE_BITS_ALL;
1350 VM_OBJECT_WUNLOCK(dst_object);
1353 * Enter it in the pmap. If a wired, copy-on-write
1354 * mapping is being replaced by a write-enabled
1355 * mapping, then wire that new mapping.
1357 pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1360 * Mark it no longer busy, and put it on the active list.
1362 VM_OBJECT_WLOCK(dst_object);
1365 vm_page_lock(src_m);
1366 vm_page_unwire(src_m, 0);
1367 vm_page_unlock(src_m);
1369 vm_page_lock(dst_m);
1370 vm_page_wire(dst_m);
1371 vm_page_unlock(dst_m);
1373 vm_page_lock(dst_m);
1374 vm_page_activate(dst_m);
1375 vm_page_unlock(dst_m);
1377 vm_page_xunbusy(dst_m);
1379 VM_OBJECT_WUNLOCK(dst_object);
1381 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1382 vm_object_deallocate(src_object);
1388 * This routine checks around the requested page for other pages that
1389 * might be able to be faulted in. This routine brackets the viable
1390 * pages for the pages to be paged in.
1393 * m, rbehind, rahead
1396 * marray (array of vm_page_t), reqpage (index of requested page)
1399 * number of pages in marray
1402 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1411 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1413 int cbehind, cahead;
1415 VM_OBJECT_ASSERT_WLOCKED(m->object);
1419 cbehind = cahead = 0;
1422 * if the requested page is not available, then give up now
1424 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1428 if ((cbehind == 0) && (cahead == 0)) {
1434 if (rahead > cahead) {
1438 if (rbehind > cbehind) {
1443 * scan backward for the read behind pages -- in memory
1446 if (rbehind > pindex) {
1450 startpindex = pindex - rbehind;
1453 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1454 rtm->pindex >= startpindex)
1455 startpindex = rtm->pindex + 1;
1457 /* tpindex is unsigned; beware of numeric underflow. */
1458 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1459 tpindex < pindex; i++, tpindex--) {
1461 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1462 VM_ALLOC_IFNOTCACHED);
1465 * Shift the allocated pages to the
1466 * beginning of the array.
1468 for (j = 0; j < i; j++) {
1469 marray[j] = marray[j + tpindex + 1 -
1475 marray[tpindex - startpindex] = rtm;
1483 /* page offset of the required page */
1486 tpindex = pindex + 1;
1490 * scan forward for the read ahead pages
1492 endpindex = tpindex + rahead;
1493 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1494 endpindex = rtm->pindex;
1495 if (endpindex > object->size)
1496 endpindex = object->size;
1498 for (; tpindex < endpindex; i++, tpindex++) {
1500 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1501 VM_ALLOC_IFNOTCACHED);
1509 /* return number of pages */
1514 * Block entry into the machine-independent layer's page fault handler by
1515 * the calling thread. Subsequent calls to vm_fault() by that thread will
1516 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1517 * spurious page faults.
1520 vm_fault_disable_pagefaults(void)
1523 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1527 vm_fault_enable_pagefaults(int save)
1530 curthread_pflags_restore(save);