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>
84 #include <sys/mutex.h>
86 #include <sys/resourcevar.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/vnode_pager.h>
104 #include <vm/vm_extern.h>
106 #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT() */
110 #define PAGEORDER_SIZE (PFBAK+PFFOR)
112 static int prefault_pageorder[] = {
113 -1 * PAGE_SIZE, 1 * PAGE_SIZE,
114 -2 * PAGE_SIZE, 2 * PAGE_SIZE,
115 -3 * PAGE_SIZE, 3 * PAGE_SIZE,
116 -4 * PAGE_SIZE, 4 * PAGE_SIZE
119 static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *);
120 static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t);
122 static int vm_fault_traced(vm_map_t, vm_offset_t, vm_prot_t, int);
125 #define VM_FAULT_READ_AHEAD 8
126 #define VM_FAULT_READ_BEHIND 7
127 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
134 vm_object_t first_object;
135 vm_pindex_t first_pindex;
137 vm_map_entry_t entry;
138 int lookup_still_valid;
144 release_page(struct faultstate *fs)
147 vm_page_wakeup(fs->m);
148 vm_page_lock_queues();
149 vm_page_deactivate(fs->m);
150 vm_page_unlock_queues();
155 unlock_map(struct faultstate *fs)
158 if (fs->lookup_still_valid) {
159 vm_map_lookup_done(fs->map, fs->entry);
160 fs->lookup_still_valid = FALSE;
165 unlock_and_deallocate(struct faultstate *fs)
168 vm_object_pip_wakeup(fs->object);
169 VM_OBJECT_UNLOCK(fs->object);
170 if (fs->object != fs->first_object) {
171 VM_OBJECT_LOCK(fs->first_object);
172 vm_page_lock_queues();
173 vm_page_free(fs->first_m);
174 vm_page_unlock_queues();
175 vm_object_pip_wakeup(fs->first_object);
176 VM_OBJECT_UNLOCK(fs->first_object);
179 vm_object_deallocate(fs->first_object);
181 if (fs->vp != NULL) {
185 VFS_UNLOCK_GIANT(fs->vfslocked);
190 * TRYPAGER - used by vm_fault to calculate whether the pager for the
191 * current object *might* contain the page.
193 * default objects are zero-fill, there is no real pager.
195 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
196 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
201 * Handle a page fault occurring at the given address,
202 * requiring the given permissions, in the map specified.
203 * If successful, the page is inserted into the
204 * associated physical map.
206 * NOTE: the given address should be truncated to the
207 * proper page address.
209 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
210 * a standard error specifying why the fault is fatal is returned.
213 * The map in question must be referenced, and remains so.
214 * Caller may hold no locks.
217 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
225 if (map != kernel_map && KTRPOINT(td, KTR_FAULT))
226 ktrfault(vaddr, fault_type);
227 result = vm_fault_traced(map, vaddr, fault_type, fault_flags);
228 if (map != kernel_map && KTRPOINT(td, KTR_FAULTEND))
234 vm_fault_traced(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type,
239 int is_first_object_locked, result;
240 boolean_t are_queues_locked, growstack, wired;
242 vm_object_t next_object;
243 vm_page_t marray[VM_FAULT_READ], mt, mt_prev;
245 int faultcount, ahead, behind, alloc_req;
246 struct faultstate fs;
250 if ((curthread->td_pflags & TDP_NOFAULTING) != 0)
251 return (KERN_PROTECTION_FAILURE);
255 PCPU_INC(cnt.v_vm_faults);
258 faultcount = behind = 0;
263 * Find the backing store object and offset into it to begin the
267 result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry,
268 &fs.first_object, &fs.first_pindex, &prot, &wired);
269 if (result != KERN_SUCCESS) {
270 if (result != KERN_PROTECTION_FAILURE ||
271 (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) {
272 if (growstack && result == KERN_INVALID_ADDRESS &&
273 map != kernel_map && curproc != NULL) {
274 result = vm_map_growstack(curproc, vaddr);
275 if (result != KERN_SUCCESS)
276 return (KERN_FAILURE);
284 * If we are user-wiring a r/w segment, and it is COW, then
285 * we need to do the COW operation. Note that we don't COW
286 * currently RO sections now, because it is NOT desirable
287 * to COW .text. We simply keep .text from ever being COW'ed
288 * and take the heat that one cannot debug wired .text sections.
290 result = vm_map_lookup(&fs.map, vaddr,
291 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
292 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
293 if (result != KERN_SUCCESS)
297 * If we don't COW now, on a user wire, the user will never
298 * be able to write to the mapping. If we don't make this
299 * restriction, the bookkeeping would be nearly impossible.
301 * XXX The following assignment modifies the map without
302 * holding a write lock on it.
304 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
305 fs.entry->max_protection &= ~VM_PROT_WRITE;
308 map_generation = fs.map->timestamp;
310 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
311 panic("vm_fault: fault on nofault entry, addr: %lx",
316 * Make a reference to this object to prevent its disposal while we
317 * are messing with it. Once we have the reference, the map is free
318 * to be diddled. Since objects reference their shadows (and copies),
319 * they will stay around as well.
321 * Bump the paging-in-progress count to prevent size changes (e.g.
322 * truncation operations) during I/O. This must be done after
323 * obtaining the vnode lock in order to avoid possible deadlocks.
325 VM_OBJECT_LOCK(fs.first_object);
326 vm_object_reference_locked(fs.first_object);
327 vm_object_pip_add(fs.first_object, 1);
329 fs.lookup_still_valid = TRUE;
337 * Search for the page at object/offset.
339 fs.object = fs.first_object;
340 fs.pindex = fs.first_pindex;
343 * If the object is dead, we stop here
345 if (fs.object->flags & OBJ_DEAD) {
346 unlock_and_deallocate(&fs);
347 return (KERN_PROTECTION_FAILURE);
351 * See if page is resident
353 fs.m = vm_page_lookup(fs.object, fs.pindex);
356 * check for page-based copy on write.
357 * We check fs.object == fs.first_object so
358 * as to ensure the legacy COW mechanism is
359 * used when the page in question is part of
360 * a shadow object. Otherwise, vm_page_cowfault()
361 * removes the page from the backing object,
362 * which is not what we want.
364 vm_page_lock_queues();
366 (fault_type & VM_PROT_WRITE) &&
367 (fs.object == fs.first_object)) {
368 vm_page_cowfault(fs.m);
369 vm_page_unlock_queues();
370 unlock_and_deallocate(&fs);
375 * Wait/Retry if the page is busy. We have to do this
376 * if the page is busy via either VPO_BUSY or
377 * vm_page_t->busy because the vm_pager may be using
378 * vm_page_t->busy for pageouts ( and even pageins if
379 * it is the vnode pager ), and we could end up trying
380 * to pagein and pageout the same page simultaneously.
382 * We can theoretically allow the busy case on a read
383 * fault if the page is marked valid, but since such
384 * pages are typically already pmap'd, putting that
385 * special case in might be more effort then it is
386 * worth. We cannot under any circumstances mess
387 * around with a vm_page_t->busy page except, perhaps,
390 if ((fs.m->oflags & VPO_BUSY) || fs.m->busy) {
391 vm_page_unlock_queues();
392 if (fs.object != fs.first_object) {
393 if (!VM_OBJECT_TRYLOCK(
395 VM_OBJECT_UNLOCK(fs.object);
396 VM_OBJECT_LOCK(fs.first_object);
397 VM_OBJECT_LOCK(fs.object);
399 vm_page_lock_queues();
400 vm_page_free(fs.first_m);
401 vm_page_unlock_queues();
402 vm_object_pip_wakeup(fs.first_object);
403 VM_OBJECT_UNLOCK(fs.first_object);
407 if (fs.m == vm_page_lookup(fs.object,
409 vm_page_sleep_if_busy(fs.m, TRUE,
412 vm_object_pip_wakeup(fs.object);
413 VM_OBJECT_UNLOCK(fs.object);
414 PCPU_INC(cnt.v_intrans);
415 vm_object_deallocate(fs.first_object);
418 vm_pageq_remove(fs.m);
419 vm_page_unlock_queues();
422 * Mark page busy for other processes, and the
423 * pagedaemon. If it still isn't completely valid
424 * (readable), jump to readrest, else break-out ( we
428 if (fs.m->valid != VM_PAGE_BITS_ALL)
434 * Page is not resident, If this is the search termination
435 * or the pager might contain the page, allocate a new page.
437 if (TRYPAGER || fs.object == fs.first_object) {
438 if (fs.pindex >= fs.object->size) {
439 unlock_and_deallocate(&fs);
440 return (KERN_PROTECTION_FAILURE);
444 * Allocate a new page for this object/offset pair.
446 * Unlocked read of the p_flag is harmless. At
447 * worst, the P_KILLED might be not observed
448 * there, and allocation can fail, causing
449 * restart and new reading of the p_flag.
452 if (!vm_page_count_severe() || P_KILLED(curproc)) {
453 #if VM_NRESERVLEVEL > 0
454 if ((fs.object->flags & OBJ_COLORED) == 0) {
455 fs.object->flags |= OBJ_COLORED;
456 fs.object->pg_color = atop(vaddr) -
460 alloc_req = P_KILLED(curproc) ?
461 VM_ALLOC_SYSTEM : VM_ALLOC_NORMAL;
462 if (fs.object->type != OBJT_VNODE &&
463 fs.object->backing_object == NULL)
464 alloc_req |= VM_ALLOC_ZERO;
465 fs.m = vm_page_alloc(fs.object, fs.pindex,
469 unlock_and_deallocate(&fs);
472 } else if (fs.m->valid == VM_PAGE_BITS_ALL)
478 * We have found a valid page or we have allocated a new page.
479 * The page thus may not be valid or may not be entirely
482 * Attempt to fault-in the page if there is a chance that the
483 * pager has it, and potentially fault in additional pages
489 u_char behavior = vm_map_entry_behavior(fs.entry);
491 if (behavior == MAP_ENTRY_BEHAV_RANDOM ||
496 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
497 if (behind > VM_FAULT_READ_BEHIND)
498 behind = VM_FAULT_READ_BEHIND;
500 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
501 if (ahead > VM_FAULT_READ_AHEAD)
502 ahead = VM_FAULT_READ_AHEAD;
504 is_first_object_locked = FALSE;
505 if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
506 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
507 fs.pindex >= fs.entry->lastr &&
508 fs.pindex < fs.entry->lastr + VM_FAULT_READ)) &&
509 (fs.first_object == fs.object ||
510 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) &&
511 fs.first_object->type != OBJT_DEVICE &&
512 fs.first_object->type != OBJT_PHYS &&
513 fs.first_object->type != OBJT_SG) {
514 vm_pindex_t firstpindex;
516 if (fs.first_pindex < 2 * VM_FAULT_READ)
519 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
520 mt = fs.first_object != fs.object ?
522 KASSERT(mt != NULL, ("vm_fault: missing mt"));
523 KASSERT((mt->oflags & VPO_BUSY) != 0,
524 ("vm_fault: mt %p not busy", mt));
525 mt_prev = vm_page_prev(mt);
527 are_queues_locked = FALSE;
529 * note: partially valid pages cannot be
530 * included in the lookahead - NFS piecemeal
531 * writes will barf on it badly.
533 while ((mt = mt_prev) != NULL &&
534 mt->pindex >= firstpindex &&
535 mt->valid == VM_PAGE_BITS_ALL) {
536 mt_prev = vm_page_prev(mt);
538 (mt->oflags & VPO_BUSY))
540 if (!are_queues_locked) {
541 are_queues_locked = TRUE;
542 vm_page_lock_queues();
544 if (mt->hold_count ||
549 vm_page_deactivate(mt);
554 if (are_queues_locked)
555 vm_page_unlock_queues();
559 if (is_first_object_locked)
560 VM_OBJECT_UNLOCK(fs.first_object);
563 * Call the pager to retrieve the data, if any, after
564 * releasing the lock on the map. We hold a ref on
565 * fs.object and the pages are VPO_BUSY'd.
570 if (fs.object->type == OBJT_VNODE) {
571 vp = fs.object->handle;
574 else if (fs.vp != NULL) {
578 locked = VOP_ISLOCKED(vp);
580 if (VFS_NEEDSGIANT(vp->v_mount) && !fs.vfslocked) {
582 if (!mtx_trylock(&Giant)) {
583 VM_OBJECT_UNLOCK(fs.object);
585 VM_OBJECT_LOCK(fs.object);
589 if (locked != LK_EXCLUSIVE)
591 /* Do not sleep for vnode lock while fs.m is busy */
592 error = vget(vp, locked | LK_CANRECURSE |
593 LK_NOWAIT, curthread);
597 vfslocked = fs.vfslocked;
598 fs.vfslocked = 0; /* Keep Giant */
601 unlock_and_deallocate(&fs);
602 error = vget(vp, locked | LK_RETRY |
603 LK_CANRECURSE, curthread);
606 fs.vfslocked = vfslocked;
608 ("vm_fault: vget failed"));
614 KASSERT(fs.vp == NULL || !fs.map->system_map,
615 ("vm_fault: vnode-backed object mapped by system map"));
618 * now we find out if any other pages should be paged
619 * in at this time this routine checks to see if the
620 * pages surrounding this fault reside in the same
621 * object as the page for this fault. If they do,
622 * then they are faulted in also into the object. The
623 * array "marray" returned contains an array of
624 * vm_page_t structs where one of them is the
625 * vm_page_t passed to the routine. The reqpage
626 * return value is the index into the marray for the
627 * vm_page_t passed to the routine.
629 * fs.m plus the additional pages are VPO_BUSY'd.
631 faultcount = vm_fault_additional_pages(
632 fs.m, behind, ahead, marray, &reqpage);
635 vm_pager_get_pages(fs.object, marray, faultcount,
636 reqpage) : VM_PAGER_FAIL;
638 if (rv == VM_PAGER_OK) {
640 * Found the page. Leave it busy while we play
645 * Relookup in case pager changed page. Pager
646 * is responsible for disposition of old page
649 fs.m = vm_page_lookup(fs.object, fs.pindex);
651 unlock_and_deallocate(&fs);
656 break; /* break to PAGE HAS BEEN FOUND */
659 * Remove the bogus page (which does not exist at this
660 * object/offset); before doing so, we must get back
661 * our object lock to preserve our invariant.
663 * Also wake up any other process that may want to bring
666 * If this is the top-level object, we must leave the
667 * busy page to prevent another process from rushing
668 * past us, and inserting the page in that object at
669 * the same time that we are.
671 if (rv == VM_PAGER_ERROR)
672 printf("vm_fault: pager read error, pid %d (%s)\n",
673 curproc->p_pid, curproc->p_comm);
675 * Data outside the range of the pager or an I/O error
678 * XXX - the check for kernel_map is a kludge to work
679 * around having the machine panic on a kernel space
680 * fault w/ I/O error.
682 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
683 (rv == VM_PAGER_BAD)) {
684 vm_page_lock_queues();
686 vm_page_unlock_queues();
688 unlock_and_deallocate(&fs);
689 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
691 if (fs.object != fs.first_object) {
692 vm_page_lock_queues();
694 vm_page_unlock_queues();
697 * XXX - we cannot just fall out at this
698 * point, m has been freed and is invalid!
704 * We get here if the object has default pager (or unwiring)
705 * or the pager doesn't have the page.
707 if (fs.object == fs.first_object)
711 * Move on to the next object. Lock the next object before
712 * unlocking the current one.
714 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
715 next_object = fs.object->backing_object;
716 if (next_object == NULL) {
718 * If there's no object left, fill the page in the top
721 if (fs.object != fs.first_object) {
722 vm_object_pip_wakeup(fs.object);
723 VM_OBJECT_UNLOCK(fs.object);
725 fs.object = fs.first_object;
726 fs.pindex = fs.first_pindex;
728 VM_OBJECT_LOCK(fs.object);
733 * Zero the page if necessary and mark it valid.
735 if ((fs.m->flags & PG_ZERO) == 0) {
736 pmap_zero_page(fs.m);
738 PCPU_INC(cnt.v_ozfod);
740 PCPU_INC(cnt.v_zfod);
741 fs.m->valid = VM_PAGE_BITS_ALL;
742 break; /* break to PAGE HAS BEEN FOUND */
744 KASSERT(fs.object != next_object,
745 ("object loop %p", next_object));
746 VM_OBJECT_LOCK(next_object);
747 vm_object_pip_add(next_object, 1);
748 if (fs.object != fs.first_object)
749 vm_object_pip_wakeup(fs.object);
750 VM_OBJECT_UNLOCK(fs.object);
751 fs.object = next_object;
755 KASSERT((fs.m->oflags & VPO_BUSY) != 0,
756 ("vm_fault: not busy after main loop"));
759 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
764 * If the page is being written, but isn't already owned by the
765 * top-level object, we have to copy it into a new page owned by the
768 if (fs.object != fs.first_object) {
770 * We only really need to copy if we want to write it.
772 if (fault_type & VM_PROT_WRITE) {
774 * This allows pages to be virtually copied from a
775 * backing_object into the first_object, where the
776 * backing object has no other refs to it, and cannot
777 * gain any more refs. Instead of a bcopy, we just
778 * move the page from the backing object to the
779 * first object. Note that we must mark the page
780 * dirty in the first object so that it will go out
781 * to swap when needed.
783 is_first_object_locked = FALSE;
786 * Only one shadow object
788 (fs.object->shadow_count == 1) &&
790 * No COW refs, except us
792 (fs.object->ref_count == 1) &&
794 * No one else can look this object up
796 (fs.object->handle == NULL) &&
798 * No other ways to look the object up
800 ((fs.object->type == OBJT_DEFAULT) ||
801 (fs.object->type == OBJT_SWAP)) &&
802 (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) &&
804 * We don't chase down the shadow chain
806 fs.object == fs.first_object->backing_object) {
807 vm_page_lock_queues();
809 * get rid of the unnecessary page
811 vm_page_free(fs.first_m);
813 * grab the page and put it into the
814 * process'es object. The page is
815 * automatically made dirty.
817 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
818 vm_page_unlock_queues();
822 PCPU_INC(cnt.v_cow_optim);
825 * Oh, well, lets copy it.
827 pmap_copy_page(fs.m, fs.first_m);
828 fs.first_m->valid = VM_PAGE_BITS_ALL;
832 * We no longer need the old page or object.
837 * fs.object != fs.first_object due to above
840 vm_object_pip_wakeup(fs.object);
841 VM_OBJECT_UNLOCK(fs.object);
843 * Only use the new page below...
845 fs.object = fs.first_object;
846 fs.pindex = fs.first_pindex;
848 if (!is_first_object_locked)
849 VM_OBJECT_LOCK(fs.object);
850 PCPU_INC(cnt.v_cow_faults);
853 prot &= ~VM_PROT_WRITE;
858 * We must verify that the maps have not changed since our last
861 if (!fs.lookup_still_valid) {
862 vm_object_t retry_object;
863 vm_pindex_t retry_pindex;
864 vm_prot_t retry_prot;
866 if (!vm_map_trylock_read(fs.map)) {
868 unlock_and_deallocate(&fs);
871 fs.lookup_still_valid = TRUE;
872 if (fs.map->timestamp != map_generation) {
873 result = vm_map_lookup_locked(&fs.map, vaddr, fault_type,
874 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
877 * If we don't need the page any longer, put it on the inactive
878 * list (the easiest thing to do here). If no one needs it,
879 * pageout will grab it eventually.
881 if (result != KERN_SUCCESS) {
883 unlock_and_deallocate(&fs);
886 * If retry of map lookup would have blocked then
887 * retry fault from start.
889 if (result == KERN_FAILURE)
893 if ((retry_object != fs.first_object) ||
894 (retry_pindex != fs.first_pindex)) {
896 unlock_and_deallocate(&fs);
901 * Check whether the protection has changed or the object has
902 * been copied while we left the map unlocked. Changing from
903 * read to write permission is OK - we leave the page
904 * write-protected, and catch the write fault. Changing from
905 * write to read permission means that we can't mark the page
906 * write-enabled after all.
912 * If the page was filled by a pager, update the map entry's
913 * last read offset. Since the pager does not return the
914 * actual set of pages that it read, this update is based on
915 * the requested set. Typically, the requested and actual
918 * XXX The following assignment modifies the map
919 * without holding a write lock on it.
922 fs.entry->lastr = fs.pindex + faultcount - behind;
924 if (prot & VM_PROT_WRITE) {
925 vm_object_set_writeable_dirty(fs.object);
928 * If the fault is a write, we know that this page is being
929 * written NOW so dirty it explicitly to save on
930 * pmap_is_modified() calls later.
932 * If this is a NOSYNC mmap we do not want to set VPO_NOSYNC
933 * if the page is already dirty to prevent data written with
934 * the expectation of being synced from not being synced.
935 * Likewise if this entry does not request NOSYNC then make
936 * sure the page isn't marked NOSYNC. Applications sharing
937 * data should use the same flags to avoid ping ponging.
939 * Also tell the backing pager, if any, that it should remove
940 * any swap backing since the page is now dirty.
942 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
943 if (fs.m->dirty == 0)
944 fs.m->oflags |= VPO_NOSYNC;
946 fs.m->oflags &= ~VPO_NOSYNC;
948 if (fault_flags & VM_FAULT_DIRTY) {
950 vm_pager_page_unswapped(fs.m);
955 * Page had better still be busy
957 KASSERT(fs.m->oflags & VPO_BUSY,
958 ("vm_fault: page %p not busy!", fs.m));
960 * Page must be completely valid or it is not fit to
961 * map into user space. vm_pager_get_pages() ensures this.
963 KASSERT(fs.m->valid == VM_PAGE_BITS_ALL,
964 ("vm_fault: page %p partially invalid", fs.m));
965 VM_OBJECT_UNLOCK(fs.object);
968 * Put this page into the physical map. We had to do the unlock above
969 * because pmap_enter() may sleep. We don't put the page
970 * back on the active queue until later so that the pageout daemon
971 * won't find it (yet).
973 pmap_enter(fs.map->pmap, vaddr, fault_type, fs.m, prot, wired);
974 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
975 vm_fault_prefault(fs.map->pmap, vaddr, fs.entry);
977 VM_OBJECT_LOCK(fs.object);
978 vm_page_lock_queues();
979 vm_page_flag_set(fs.m, PG_REFERENCED);
982 * If the page is not wired down, then put it where the pageout daemon
985 if (fault_flags & VM_FAULT_WIRE_MASK) {
989 vm_page_unwire(fs.m, 1);
991 vm_page_activate(fs.m);
993 vm_page_unlock_queues();
994 vm_page_wakeup(fs.m);
997 * Unlock everything, and return
999 unlock_and_deallocate(&fs);
1001 curthread->td_ru.ru_majflt++;
1003 curthread->td_ru.ru_minflt++;
1005 return (KERN_SUCCESS);
1009 * vm_fault_prefault provides a quick way of clustering
1010 * pagefaults into a processes address space. It is a "cousin"
1011 * of vm_map_pmap_enter, except it runs at page fault time instead
1015 vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry)
1018 vm_offset_t addr, starta;
1023 if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace))
1026 object = entry->object.vm_object;
1028 starta = addra - PFBAK * PAGE_SIZE;
1029 if (starta < entry->start) {
1030 starta = entry->start;
1031 } else if (starta > addra) {
1035 for (i = 0; i < PAGEORDER_SIZE; i++) {
1036 vm_object_t backing_object, lobject;
1038 addr = addra + prefault_pageorder[i];
1039 if (addr > addra + (PFFOR * PAGE_SIZE))
1042 if (addr < starta || addr >= entry->end)
1045 if (!pmap_is_prefaultable(pmap, addr))
1048 pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT;
1050 VM_OBJECT_LOCK(lobject);
1051 while ((m = vm_page_lookup(lobject, pindex)) == NULL &&
1052 lobject->type == OBJT_DEFAULT &&
1053 (backing_object = lobject->backing_object) != NULL) {
1054 KASSERT((lobject->backing_object_offset & PAGE_MASK) ==
1055 0, ("vm_fault_prefault: unaligned object offset"));
1056 pindex += lobject->backing_object_offset >> PAGE_SHIFT;
1057 VM_OBJECT_LOCK(backing_object);
1058 VM_OBJECT_UNLOCK(lobject);
1059 lobject = backing_object;
1062 * give-up when a page is not in memory
1065 VM_OBJECT_UNLOCK(lobject);
1068 if (m->valid == VM_PAGE_BITS_ALL &&
1069 (m->flags & PG_FICTITIOUS) == 0) {
1070 vm_page_lock_queues();
1071 pmap_enter_quick(pmap, addr, m, entry->protection);
1072 vm_page_unlock_queues();
1074 VM_OBJECT_UNLOCK(lobject);
1081 * Ensure that the requested virtual address, which may be in userland,
1082 * is valid. Fault-in the page if necessary. Return -1 on failure.
1085 vm_fault_quick(caddr_t v, int prot)
1089 if (prot & VM_PROT_WRITE)
1090 r = subyte(v, fubyte(v));
1099 * Wire down a range of virtual addresses in a map.
1102 vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1103 boolean_t user_wire, boolean_t fictitious)
1109 * We simulate a fault to get the page and enter it in the physical
1110 * map. For user wiring, we only ask for read access on currently
1111 * read-only sections.
1113 for (va = start; va < end; va += PAGE_SIZE) {
1114 rv = vm_fault(map, va,
1115 user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE,
1116 user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING);
1119 vm_fault_unwire(map, start, va, fictitious);
1123 return (KERN_SUCCESS);
1129 * Unwire a range of virtual addresses in a map.
1132 vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1133 boolean_t fictitious)
1139 pmap = vm_map_pmap(map);
1142 * Since the pages are wired down, we must be able to get their
1143 * mappings from the physical map system.
1145 for (va = start; va < end; va += PAGE_SIZE) {
1146 pa = pmap_extract(pmap, va);
1148 pmap_change_wiring(pmap, va, FALSE);
1150 vm_page_lock_queues();
1151 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1152 vm_page_unlock_queues();
1160 * vm_fault_copy_entry
1162 * Create new shadow object backing dst_entry with private copy of
1163 * all underlying pages. When src_entry is equal to dst_entry,
1164 * function implements COW for wired-down map entry. Otherwise,
1165 * it forks wired entry into dst_map.
1167 * In/out conditions:
1168 * The source and destination maps must be locked for write.
1169 * The source map entry must be wired down (or be a sharing map
1170 * entry corresponding to a main map entry that is wired down).
1173 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1174 vm_map_entry_t dst_entry, vm_map_entry_t src_entry,
1175 vm_ooffset_t *fork_charge)
1177 vm_object_t backing_object, dst_object, object, src_object;
1178 vm_pindex_t dst_pindex, pindex, src_pindex;
1179 vm_prot_t access, prot;
1183 boolean_t src_readonly, upgrade;
1189 upgrade = src_entry == dst_entry;
1191 src_object = src_entry->object.vm_object;
1192 src_pindex = OFF_TO_IDX(src_entry->offset);
1193 src_readonly = (src_entry->protection & VM_PROT_WRITE) == 0;
1196 * Create the top-level object for the destination entry. (Doesn't
1197 * actually shadow anything - we copy the pages directly.)
1199 dst_object = vm_object_allocate(OBJT_DEFAULT,
1200 OFF_TO_IDX(dst_entry->end - dst_entry->start));
1201 #if VM_NRESERVLEVEL > 0
1202 dst_object->flags |= OBJ_COLORED;
1203 dst_object->pg_color = atop(dst_entry->start);
1206 VM_OBJECT_LOCK(dst_object);
1207 KASSERT(upgrade || dst_entry->object.vm_object == NULL,
1208 ("vm_fault_copy_entry: vm_object not NULL"));
1209 dst_entry->object.vm_object = dst_object;
1210 dst_entry->offset = 0;
1211 dst_object->charge = dst_entry->end - dst_entry->start;
1212 if (fork_charge != NULL) {
1213 KASSERT(dst_entry->uip == NULL,
1214 ("vm_fault_copy_entry: leaked swp charge"));
1215 dst_object->uip = curthread->td_ucred->cr_ruidinfo;
1216 uihold(dst_object->uip);
1217 *fork_charge += dst_object->charge;
1219 dst_object->uip = dst_entry->uip;
1220 dst_entry->uip = NULL;
1222 access = prot = dst_entry->max_protection;
1224 * If not an upgrade, then enter the mappings in the pmap as
1225 * read and/or execute accesses. Otherwise, enter them as
1228 * A writeable large page mapping is only created if all of
1229 * the constituent small page mappings are modified. Marking
1230 * PTEs as modified on inception allows promotion to happen
1231 * without taking potentially large number of soft faults.
1234 access &= ~VM_PROT_WRITE;
1237 * Loop through all of the pages in the entry's range, copying each
1238 * one from the source object (it should be there) to the destination
1241 for (vaddr = dst_entry->start, dst_pindex = 0;
1242 vaddr < dst_entry->end;
1243 vaddr += PAGE_SIZE, dst_pindex++) {
1246 * Allocate a page in the destination object.
1249 dst_m = vm_page_alloc(dst_object, dst_pindex,
1251 if (dst_m == NULL) {
1252 VM_OBJECT_UNLOCK(dst_object);
1254 VM_OBJECT_LOCK(dst_object);
1256 } while (dst_m == NULL);
1259 * Find the page in the source object, and copy it in.
1260 * (Because the source is wired down, the page will be in
1263 VM_OBJECT_LOCK(src_object);
1264 object = src_object;
1265 pindex = src_pindex + dst_pindex;
1266 while ((src_m = vm_page_lookup(object, pindex)) == NULL &&
1268 (backing_object = object->backing_object) != NULL) {
1270 * Allow fallback to backing objects if we are reading.
1272 VM_OBJECT_LOCK(backing_object);
1273 pindex += OFF_TO_IDX(object->backing_object_offset);
1274 VM_OBJECT_UNLOCK(object);
1275 object = backing_object;
1278 panic("vm_fault_copy_wired: page missing");
1279 pmap_copy_page(src_m, dst_m);
1280 VM_OBJECT_UNLOCK(object);
1281 dst_m->valid = VM_PAGE_BITS_ALL;
1282 VM_OBJECT_UNLOCK(dst_object);
1285 * Enter it in the pmap. If a wired, copy-on-write
1286 * mapping is being replaced by a write-enabled
1287 * mapping, then wire that new mapping.
1289 pmap_enter(dst_map->pmap, vaddr, access, dst_m, prot, upgrade);
1292 * Mark it no longer busy, and put it on the active list.
1294 VM_OBJECT_LOCK(dst_object);
1295 vm_page_lock_queues();
1297 vm_page_unwire(src_m, 0);
1298 vm_page_wire(dst_m);
1300 vm_page_activate(dst_m);
1301 vm_page_unlock_queues();
1302 vm_page_wakeup(dst_m);
1304 VM_OBJECT_UNLOCK(dst_object);
1306 dst_entry->eflags &= ~(MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY);
1307 vm_object_deallocate(src_object);
1313 * This routine checks around the requested page for other pages that
1314 * might be able to be faulted in. This routine brackets the viable
1315 * pages for the pages to be paged in.
1318 * m, rbehind, rahead
1321 * marray (array of vm_page_t), reqpage (index of requested page)
1324 * number of pages in marray
1327 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1336 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1338 int cbehind, cahead;
1340 VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED);
1344 cbehind = cahead = 0;
1347 * if the requested page is not available, then give up now
1349 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1353 if ((cbehind == 0) && (cahead == 0)) {
1359 if (rahead > cahead) {
1363 if (rbehind > cbehind) {
1368 * scan backward for the read behind pages -- in memory
1371 if (rbehind > pindex) {
1375 startpindex = pindex - rbehind;
1378 if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL &&
1379 rtm->pindex >= startpindex)
1380 startpindex = rtm->pindex + 1;
1382 /* tpindex is unsigned; beware of numeric underflow. */
1383 for (i = 0, tpindex = pindex - 1; tpindex >= startpindex &&
1384 tpindex < pindex; i++, tpindex--) {
1386 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1387 VM_ALLOC_IFNOTCACHED);
1390 * Shift the allocated pages to the
1391 * beginning of the array.
1393 for (j = 0; j < i; j++) {
1394 marray[j] = marray[j + tpindex + 1 -
1400 marray[tpindex - startpindex] = rtm;
1408 /* page offset of the required page */
1411 tpindex = pindex + 1;
1415 * scan forward for the read ahead pages
1417 endpindex = tpindex + rahead;
1418 if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex)
1419 endpindex = rtm->pindex;
1420 if (endpindex > object->size)
1421 endpindex = object->size;
1423 for (; tpindex < endpindex; i++, tpindex++) {
1425 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL |
1426 VM_ALLOC_IFNOTCACHED);
1434 /* return number of pages */
1439 * Block entry into the machine-independent layer's page fault handler by
1440 * the calling thread. Subsequent calls to vm_fault() by that thread will
1441 * return KERN_PROTECTION_FAILURE. Enable machine-dependent handling of
1442 * spurious page faults.
1445 vm_fault_disable_pagefaults(void)
1448 return (curthread_pflags_set(TDP_NOFAULTING | TDP_RESETSPUR));
1452 vm_fault_enable_pagefaults(int save)
1455 curthread_pflags_restore(save);