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.11 2004/03/01 06:33:24 dillon Exp $
74 * Page fault handling module.
77 #include <sys/param.h>
78 #include <sys/systm.h>
80 #include <sys/vnode.h>
81 #include <sys/resourcevar.h>
82 #include <sys/vmmeter.h>
85 #include <vm/vm_param.h>
88 #include <vm/vm_map.h>
89 #include <vm/vm_object.h>
90 #include <vm/vm_page.h>
91 #include <vm/vm_pageout.h>
92 #include <vm/vm_kern.h>
93 #include <vm/vm_pager.h>
94 #include <vm/vnode_pager.h>
95 #include <vm/vm_extern.h>
96 #include <vm/vm_page2.h>
98 static int vm_fault_additional_pages (vm_page_t, int,
99 int, vm_page_t *, int *);
101 #define VM_FAULT_READ_AHEAD 8
102 #define VM_FAULT_READ_BEHIND 7
103 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
110 vm_object_t first_object;
111 vm_pindex_t first_pindex;
113 vm_map_entry_t entry;
114 int lookup_still_valid;
119 release_page(struct faultstate *fs)
121 vm_page_wakeup(fs->m);
122 vm_page_deactivate(fs->m);
127 unlock_map(struct faultstate *fs)
129 if (fs->lookup_still_valid) {
130 vm_map_lookup_done(fs->map, fs->entry, 0);
131 fs->lookup_still_valid = FALSE;
136 _unlock_things(struct faultstate *fs, int dealloc)
138 vm_object_pip_wakeup(fs->object);
139 if (fs->object != fs->first_object) {
140 vm_page_free(fs->first_m);
141 vm_object_pip_wakeup(fs->first_object);
145 vm_object_deallocate(fs->first_object);
148 if (fs->vp != NULL) {
154 #define unlock_things(fs) _unlock_things(fs, 0)
155 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
158 * TRYPAGER - used by vm_fault to calculate whether the pager for the
159 * current object *might* contain the page.
161 * default objects are zero-fill, there is no real pager.
164 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
165 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
170 * Handle a page fault occurring at the given address,
171 * requiring the given permissions, in the map specified.
172 * If successful, the page is inserted into the
173 * associated physical map.
175 * NOTE: the given address should be truncated to the
176 * proper page address.
178 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
179 * a standard error specifying why the fault is fatal is returned.
182 * The map in question must be referenced, and remains so.
183 * Caller may hold no locks.
186 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
192 vm_object_t next_object;
193 vm_page_t marray[VM_FAULT_READ];
196 struct faultstate fs;
198 mycpu->gd_cnt.v_vm_faults++;
204 * Find the backing store object and offset into it to begin the
208 if ((result = vm_map_lookup(&fs.map, vaddr,
209 fault_type, &fs.entry, &fs.first_object,
210 &fs.first_pindex, &prot, &wired)) != KERN_SUCCESS) {
211 if ((result != KERN_PROTECTION_FAILURE) ||
212 ((fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)) {
217 * If we are user-wiring a r/w segment, and it is COW, then
218 * we need to do the COW operation. Note that we don't COW
219 * currently RO sections now, because it is NOT desirable
220 * to COW .text. We simply keep .text from ever being COW'ed
221 * and take the heat that one cannot debug wired .text sections.
223 result = vm_map_lookup(&fs.map, vaddr,
224 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
225 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
226 if (result != KERN_SUCCESS) {
231 * If we don't COW now, on a user wire, the user will never
232 * be able to write to the mapping. If we don't make this
233 * restriction, the bookkeeping would be nearly impossible.
235 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
236 fs.entry->max_protection &= ~VM_PROT_WRITE;
239 map_generation = fs.map->timestamp;
241 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
242 panic("vm_fault: fault on nofault entry, addr: %lx",
247 * Make a reference to this object to prevent its disposal while we
248 * are messing with it. Once we have the reference, the map is free
249 * to be diddled. Since objects reference their shadows (and copies),
250 * they will stay around as well.
252 * Bump the paging-in-progress count to prevent size changes (e.g.
253 * truncation operations) during I/O. This must be done after
254 * obtaining the vnode lock in order to avoid possible deadlocks.
256 vm_object_reference(fs.first_object);
257 fs.vp = vnode_pager_lock(fs.first_object);
258 vm_object_pip_add(fs.first_object, 1);
260 if ((fault_type & VM_PROT_WRITE) &&
261 (fs.first_object->type == OBJT_VNODE)) {
262 vm_freeze_copyopts(fs.first_object,
263 fs.first_pindex, fs.first_pindex + 1);
266 fs.lookup_still_valid = TRUE;
274 * Search for the page at object/offset.
277 fs.object = fs.first_object;
278 fs.pindex = fs.first_pindex;
282 * If the object is dead, we stop here
285 if (fs.object->flags & OBJ_DEAD) {
286 unlock_and_deallocate(&fs);
287 return (KERN_PROTECTION_FAILURE);
291 * See if page is resident
294 fs.m = vm_page_lookup(fs.object, fs.pindex);
298 * Wait/Retry if the page is busy. We have to do this
299 * if the page is busy via either PG_BUSY or
300 * vm_page_t->busy because the vm_pager may be using
301 * vm_page_t->busy for pageouts ( and even pageins if
302 * it is the vnode pager ), and we could end up trying
303 * to pagein and pageout the same page simultaneously.
305 * We can theoretically allow the busy case on a read
306 * fault if the page is marked valid, but since such
307 * pages are typically already pmap'd, putting that
308 * special case in might be more effort then it is
309 * worth. We cannot under any circumstances mess
310 * around with a vm_page_t->busy page except, perhaps,
313 if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
315 (void)vm_page_sleep_busy(fs.m, TRUE, "vmpfw");
316 mycpu->gd_cnt.v_intrans++;
317 vm_object_deallocate(fs.first_object);
323 vm_page_unqueue_nowakeup(fs.m);
326 if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
327 vm_page_activate(fs.m);
328 unlock_and_deallocate(&fs);
334 * Mark page busy for other processes, and the
335 * pagedaemon. If it still isn't completely valid
336 * (readable), jump to readrest, else break-out ( we
341 if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
342 fs.m->object != kernel_object && fs.m->object != kmem_object) {
350 * Page is not resident, If this is the search termination
351 * or the pager might contain the page, allocate a new page.
354 if (TRYPAGER || fs.object == fs.first_object) {
355 if (fs.pindex >= fs.object->size) {
356 unlock_and_deallocate(&fs);
357 return (KERN_PROTECTION_FAILURE);
361 * Allocate a new page for this object/offset pair.
364 if (!vm_page_count_severe()) {
365 fs.m = vm_page_alloc(fs.object, fs.pindex,
366 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
369 unlock_and_deallocate(&fs);
377 * We have found a valid page or we have allocated a new page.
378 * The page thus may not be valid or may not be entirely
381 * Attempt to fault-in the page if there is a chance that the
382 * pager has it, and potentially fault in additional pages
390 u_char behavior = vm_map_entry_behavior(fs.entry);
392 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
396 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
397 if (behind > VM_FAULT_READ_BEHIND)
398 behind = VM_FAULT_READ_BEHIND;
400 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
401 if (ahead > VM_FAULT_READ_AHEAD)
402 ahead = VM_FAULT_READ_AHEAD;
405 if ((fs.first_object->type != OBJT_DEVICE) &&
406 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
407 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
408 fs.pindex >= fs.entry->lastr &&
409 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
411 vm_pindex_t firstpindex, tmppindex;
413 if (fs.first_pindex < 2 * VM_FAULT_READ)
416 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
419 * note: partially valid pages cannot be
420 * included in the lookahead - NFS piecemeal
421 * writes will barf on it badly.
424 for(tmppindex = fs.first_pindex - 1;
425 tmppindex >= firstpindex;
428 mt = vm_page_lookup( fs.first_object, tmppindex);
429 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
432 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
437 vm_page_test_dirty(mt);
439 vm_page_protect(mt, VM_PROT_NONE);
440 vm_page_deactivate(mt);
451 * now we find out if any other pages should be paged
452 * in at this time this routine checks to see if the
453 * pages surrounding this fault reside in the same
454 * object as the page for this fault. If they do,
455 * then they are faulted in also into the object. The
456 * array "marray" returned contains an array of
457 * vm_page_t structs where one of them is the
458 * vm_page_t passed to the routine. The reqpage
459 * return value is the index into the marray for the
460 * vm_page_t passed to the routine.
462 * fs.m plus the additional pages are PG_BUSY'd.
464 faultcount = vm_fault_additional_pages(
465 fs.m, behind, ahead, marray, &reqpage);
468 * update lastr imperfectly (we do not know how much
469 * getpages will actually read), but good enough.
471 fs.entry->lastr = fs.pindex + faultcount - behind;
474 * Call the pager to retrieve the data, if any, after
475 * releasing the lock on the map. We hold a ref on
476 * fs.object and the pages are PG_BUSY'd.
481 vm_pager_get_pages(fs.object, marray, faultcount,
482 reqpage) : VM_PAGER_FAIL;
484 if (rv == VM_PAGER_OK) {
486 * Found the page. Leave it busy while we play
491 * Relookup in case pager changed page. Pager
492 * is responsible for disposition of old page
495 fs.m = vm_page_lookup(fs.object, fs.pindex);
497 unlock_and_deallocate(&fs);
502 break; /* break to PAGE HAS BEEN FOUND */
505 * Remove the bogus page (which does not exist at this
506 * object/offset); before doing so, we must get back
507 * our object lock to preserve our invariant.
509 * Also wake up any other process that may want to bring
512 * If this is the top-level object, we must leave the
513 * busy page to prevent another process from rushing
514 * past us, and inserting the page in that object at
515 * the same time that we are.
518 if (rv == VM_PAGER_ERROR)
519 printf("vm_fault: pager read error, pid %d (%s)\n",
520 curproc->p_pid, curproc->p_comm);
522 * Data outside the range of the pager or an I/O error
525 * XXX - the check for kernel_map is a kludge to work
526 * around having the machine panic on a kernel space
527 * fault w/ I/O error.
529 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
530 (rv == VM_PAGER_BAD)) {
533 unlock_and_deallocate(&fs);
534 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
536 if (fs.object != fs.first_object) {
540 * XXX - we cannot just fall out at this
541 * point, m has been freed and is invalid!
547 * We get here if the object has default pager (or unwiring)
548 * or the pager doesn't have the page.
550 if (fs.object == fs.first_object)
554 * Move on to the next object. Lock the next object before
555 * unlocking the current one.
558 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
559 next_object = fs.object->backing_object;
560 if (next_object == NULL) {
562 * If there's no object left, fill the page in the top
565 if (fs.object != fs.first_object) {
566 vm_object_pip_wakeup(fs.object);
568 fs.object = fs.first_object;
569 fs.pindex = fs.first_pindex;
575 * Zero the page if necessary and mark it valid.
577 if ((fs.m->flags & PG_ZERO) == 0) {
578 vm_page_zero_fill(fs.m);
580 mycpu->gd_cnt.v_ozfod++;
582 mycpu->gd_cnt.v_zfod++;
583 fs.m->valid = VM_PAGE_BITS_ALL;
584 break; /* break to PAGE HAS BEEN FOUND */
586 if (fs.object != fs.first_object) {
587 vm_object_pip_wakeup(fs.object);
589 KASSERT(fs.object != next_object, ("object loop %p", next_object));
590 fs.object = next_object;
591 vm_object_pip_add(fs.object, 1);
595 KASSERT((fs.m->flags & PG_BUSY) != 0,
596 ("vm_fault: not busy after main loop"));
599 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
604 * If the page is being written, but isn't already owned by the
605 * top-level object, we have to copy it into a new page owned by the
609 if (fs.object != fs.first_object) {
611 * We only really need to copy if we want to write it.
614 if (fault_type & VM_PROT_WRITE) {
616 * This allows pages to be virtually copied from a
617 * backing_object into the first_object, where the
618 * backing object has no other refs to it, and cannot
619 * gain any more refs. Instead of a bcopy, we just
620 * move the page from the backing object to the
621 * first object. Note that we must mark the page
622 * dirty in the first object so that it will go out
623 * to swap when needed.
625 if (map_generation == fs.map->timestamp &&
627 * Only one shadow object
629 (fs.object->shadow_count == 1) &&
631 * No COW refs, except us
633 (fs.object->ref_count == 1) &&
635 * No one else can look this object up
637 (fs.object->handle == NULL) &&
639 * No other ways to look the object up
641 ((fs.object->type == OBJT_DEFAULT) ||
642 (fs.object->type == OBJT_SWAP)) &&
644 * We don't chase down the shadow chain
646 (fs.object == fs.first_object->backing_object) &&
649 * grab the lock if we need to
651 (fs.lookup_still_valid ||
652 lockmgr(&fs.map->lock, LK_EXCLUSIVE|LK_NOWAIT, NULL, curthread) == 0)
655 fs.lookup_still_valid = 1;
657 * get rid of the unnecessary page
659 vm_page_protect(fs.first_m, VM_PROT_NONE);
660 vm_page_free(fs.first_m);
664 * grab the page and put it into the
665 * process'es object. The page is
666 * automatically made dirty.
668 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
670 vm_page_busy(fs.first_m);
672 mycpu->gd_cnt.v_cow_optim++;
675 * Oh, well, lets copy it.
677 vm_page_copy(fs.m, fs.first_m);
682 * We no longer need the old page or object.
688 * fs.object != fs.first_object due to above
692 vm_object_pip_wakeup(fs.object);
695 * Only use the new page below...
698 mycpu->gd_cnt.v_cow_faults++;
700 fs.object = fs.first_object;
701 fs.pindex = fs.first_pindex;
704 prot &= ~VM_PROT_WRITE;
709 * We must verify that the maps have not changed since our last
713 if (!fs.lookup_still_valid &&
714 (fs.map->timestamp != map_generation)) {
715 vm_object_t retry_object;
716 vm_pindex_t retry_pindex;
717 vm_prot_t retry_prot;
720 * Since map entries may be pageable, make sure we can take a
721 * page fault on them.
725 * Unlock vnode before the lookup to avoid deadlock. E.G.
726 * avoid a deadlock between the inode and exec_map that can
727 * occur due to locks being obtained in different orders.
735 if (fs.map->infork) {
737 unlock_and_deallocate(&fs);
742 * To avoid trying to write_lock the map while another process
743 * has it read_locked (in vm_map_wire), we do not try for
744 * write permission. If the page is still writable, we will
745 * get write permission. If it is not, or has been marked
746 * needs_copy, we enter the mapping without write permission,
747 * and will merely take another fault.
749 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
750 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
751 map_generation = fs.map->timestamp;
754 * If we don't need the page any longer, put it on the active
755 * list (the easiest thing to do here). If no one needs it,
756 * pageout will grab it eventually.
759 if (result != KERN_SUCCESS) {
761 unlock_and_deallocate(&fs);
764 fs.lookup_still_valid = TRUE;
766 if ((retry_object != fs.first_object) ||
767 (retry_pindex != fs.first_pindex)) {
769 unlock_and_deallocate(&fs);
773 * Check whether the protection has changed or the object has
774 * been copied while we left the map unlocked. Changing from
775 * read to write permission is OK - we leave the page
776 * write-protected, and catch the write fault. Changing from
777 * write to read permission means that we can't mark the page
778 * write-enabled after all.
784 * Put this page into the physical map. We had to do the unlock above
785 * because pmap_enter may cause other faults. We don't put the page
786 * back on the active queue until later so that the page-out daemon
787 * won't find us (yet).
790 if (prot & VM_PROT_WRITE) {
791 vm_page_flag_set(fs.m, PG_WRITEABLE);
792 vm_object_set_writeable_dirty(fs.m->object);
795 * If the fault is a write, we know that this page is being
796 * written NOW so dirty it explicitly to save on
797 * pmap_is_modified() calls later.
799 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
800 * if the page is already dirty to prevent data written with
801 * the expectation of being synced from not being synced.
802 * Likewise if this entry does not request NOSYNC then make
803 * sure the page isn't marked NOSYNC. Applications sharing
804 * data should use the same flags to avoid ping ponging.
806 * Also tell the backing pager, if any, that it should remove
807 * any swap backing since the page is now dirty.
809 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
810 if (fs.m->dirty == 0)
811 vm_page_flag_set(fs.m, PG_NOSYNC);
813 vm_page_flag_clear(fs.m, PG_NOSYNC);
815 if (fault_flags & VM_FAULT_DIRTY) {
819 vm_pager_page_unswapped(fs.m);
825 * Page had better still be busy
828 KASSERT(fs.m->flags & PG_BUSY,
829 ("vm_fault: page %p not busy!", fs.m));
834 * Sanity check: page must be completely valid or it is not fit to
835 * map into user space. vm_pager_get_pages() ensures this.
838 if (fs.m->valid != VM_PAGE_BITS_ALL) {
839 vm_page_zero_invalid(fs.m, TRUE);
840 printf("Warning: page %p partially invalid on fault\n", fs.m);
843 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
845 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
846 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
849 vm_page_flag_clear(fs.m, PG_ZERO);
850 vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
851 if (fault_flags & VM_FAULT_HOLD)
855 * If the page is not wired down, then put it where the pageout daemon
859 if (fault_flags & VM_FAULT_WIRE_MASK) {
863 vm_page_unwire(fs.m, 1);
865 vm_page_activate(fs.m);
868 if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
870 curproc->p_stats->p_ru.ru_majflt++;
872 curproc->p_stats->p_ru.ru_minflt++;
877 * Unlock everything, and return
880 vm_page_wakeup(fs.m);
881 vm_object_deallocate(fs.first_object);
883 return (KERN_SUCCESS);
890 * Wire down a range of virtual addresses in a map.
893 vm_fault_wire(map, start, end)
895 vm_offset_t start, end;
902 pmap = vm_map_pmap(map);
905 * Inform the physical mapping system that the range of addresses may
906 * not fault, so that page tables and such can be locked down as well.
909 pmap_pageable(pmap, start, end, FALSE);
912 * We simulate a fault to get the page and enter it in the physical
916 for (va = start; va < end; va += PAGE_SIZE) {
917 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
918 VM_FAULT_CHANGE_WIRING);
921 vm_fault_unwire(map, start, va);
925 return (KERN_SUCCESS);
929 * vm_fault_user_wire:
931 * Wire down a range of virtual addresses in a map. This
932 * is for user mode though, so we only ask for read access
933 * on currently read only sections.
936 vm_fault_user_wire(map, start, end)
938 vm_offset_t start, end;
945 pmap = vm_map_pmap(map);
948 * Inform the physical mapping system that the range of addresses may
949 * not fault, so that page tables and such can be locked down as well.
952 pmap_pageable(pmap, start, end, FALSE);
955 * We simulate a fault to get the page and enter it in the physical
958 for (va = start; va < end; va += PAGE_SIZE) {
959 rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_USER_WIRE);
962 vm_fault_unwire(map, start, va);
966 return (KERN_SUCCESS);
973 * Unwire a range of virtual addresses in a map.
976 vm_fault_unwire(map, start, end)
978 vm_offset_t start, end;
985 pmap = vm_map_pmap(map);
988 * Since the pages are wired down, we must be able to get their
989 * mappings from the physical map system.
992 for (va = start; va < end; va += PAGE_SIZE) {
993 pa = pmap_extract(pmap, va);
995 pmap_change_wiring(pmap, va, FALSE);
996 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1001 * Inform the physical mapping system that the range of addresses may
1002 * fault, so that page tables and such may be unwired themselves.
1005 pmap_pageable(pmap, start, end, TRUE);
1011 * vm_fault_copy_entry
1013 * Copy all of the pages from a wired-down map entry to another.
1015 * In/out conditions:
1016 * The source and destination maps must be locked for write.
1017 * The source map entry must be wired down (or be a sharing map
1018 * entry corresponding to a main map entry that is wired down).
1022 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1025 vm_map_entry_t dst_entry;
1026 vm_map_entry_t src_entry;
1028 vm_object_t dst_object;
1029 vm_object_t src_object;
1030 vm_ooffset_t dst_offset;
1031 vm_ooffset_t src_offset;
1041 src_object = src_entry->object.vm_object;
1042 src_offset = src_entry->offset;
1045 * Create the top-level object for the destination entry. (Doesn't
1046 * actually shadow anything - we copy the pages directly.)
1048 dst_object = vm_object_allocate(OBJT_DEFAULT,
1049 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1051 dst_entry->object.vm_object = dst_object;
1052 dst_entry->offset = 0;
1054 prot = dst_entry->max_protection;
1057 * Loop through all of the pages in the entry's range, copying each
1058 * one from the source object (it should be there) to the destination
1061 for (vaddr = dst_entry->start, dst_offset = 0;
1062 vaddr < dst_entry->end;
1063 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1066 * Allocate a page in the destination object
1069 dst_m = vm_page_alloc(dst_object,
1070 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1071 if (dst_m == NULL) {
1074 } while (dst_m == NULL);
1077 * Find the page in the source object, and copy it in.
1078 * (Because the source is wired down, the page will be in
1081 src_m = vm_page_lookup(src_object,
1082 OFF_TO_IDX(dst_offset + src_offset));
1084 panic("vm_fault_copy_wired: page missing");
1086 vm_page_copy(src_m, dst_m);
1089 * Enter it in the pmap...
1092 vm_page_flag_clear(dst_m, PG_ZERO);
1093 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1094 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1097 * Mark it no longer busy, and put it on the active list.
1099 vm_page_activate(dst_m);
1100 vm_page_wakeup(dst_m);
1106 * This routine checks around the requested page for other pages that
1107 * might be able to be faulted in. This routine brackets the viable
1108 * pages for the pages to be paged in.
1111 * m, rbehind, rahead
1114 * marray (array of vm_page_t), reqpage (index of requested page)
1117 * number of pages in marray
1120 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1129 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1131 int cbehind, cahead;
1137 * we don't fault-ahead for device pager
1139 if (object->type == OBJT_DEVICE) {
1146 * if the requested page is not available, then give up now
1149 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1153 if ((cbehind == 0) && (cahead == 0)) {
1159 if (rahead > cahead) {
1163 if (rbehind > cbehind) {
1168 * try to do any readahead that we might have free pages for.
1170 if ((rahead + rbehind) >
1171 ((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1172 pagedaemon_wakeup();
1179 * scan backward for the read behind pages -- in memory
1182 if (rbehind > pindex) {
1186 startpindex = pindex - rbehind;
1189 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1190 if (vm_page_lookup( object, tpindex)) {
1191 startpindex = tpindex + 1;
1198 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1200 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1202 for (j = 0; j < i; j++) {
1203 vm_page_free(marray[j]);
1218 /* page offset of the required page */
1221 tpindex = pindex + 1;
1225 * scan forward for the read ahead pages
1227 endpindex = tpindex + rahead;
1228 if (endpindex > object->size)
1229 endpindex = object->size;
1231 for( ; tpindex < endpindex; i++, tpindex++) {
1233 if (vm_page_lookup(object, tpindex)) {
1237 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1245 /* return number of bytes of pages */