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.
73 * Page fault handling module.
76 #include <sys/param.h>
77 #include <sys/systm.h>
79 #include <sys/mutex.h>
81 #include <sys/vnode.h>
82 #include <sys/resourcevar.h>
83 #include <sys/vmmeter.h>
86 #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>
97 static int vm_fault_additional_pages __P((vm_page_t, int,
98 int, vm_page_t *, int *));
100 #define VM_FAULT_READ_AHEAD 8
101 #define VM_FAULT_READ_BEHIND 7
102 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
109 vm_object_t first_object;
110 vm_pindex_t first_pindex;
112 vm_map_entry_t entry;
113 int lookup_still_valid;
118 release_page(struct faultstate *fs)
120 vm_page_wakeup(fs->m);
121 vm_page_deactivate(fs->m);
126 unlock_map(struct faultstate *fs)
128 if (fs->lookup_still_valid) {
129 vm_map_lookup_done(fs->map, fs->entry);
130 fs->lookup_still_valid = FALSE;
135 _unlock_things(struct faultstate *fs, int dealloc)
137 vm_object_pip_wakeup(fs->object);
138 if (fs->object != fs->first_object) {
139 vm_page_free(fs->first_m);
140 vm_object_pip_wakeup(fs->first_object);
144 vm_object_deallocate(fs->first_object);
147 if (fs->vp != NULL) {
153 #define unlock_things(fs) _unlock_things(fs, 0)
154 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
157 * TRYPAGER - used by vm_fault to calculate whether the pager for the
158 * current object *might* contain the page.
160 * default objects are zero-fill, there is no real pager.
163 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
164 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
169 * Handle a page fault occurring at the given address,
170 * requiring the given permissions, in the map specified.
171 * If successful, the page is inserted into the
172 * associated physical map.
174 * NOTE: the given address should be truncated to the
175 * proper page address.
177 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
178 * a standard error specifying why the fault is fatal is returned.
181 * The map in question must be referenced, and remains so.
182 * Caller may hold no locks.
185 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
191 vm_object_t next_object;
192 vm_page_t marray[VM_FAULT_READ];
195 struct faultstate fs;
197 cnt.v_vm_faults++; /* needs lock XXX */
203 * Find the backing store object and offset into it to begin the
207 if ((result = vm_map_lookup(&fs.map, vaddr,
208 fault_type, &fs.entry, &fs.first_object,
209 &fs.first_pindex, &prot, &wired)) != KERN_SUCCESS) {
210 if ((result != KERN_PROTECTION_FAILURE) ||
211 ((fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)) {
216 * If we are user-wiring a r/w segment, and it is COW, then
217 * we need to do the COW operation. Note that we don't COW
218 * currently RO sections now, because it is NOT desirable
219 * to COW .text. We simply keep .text from ever being COW'ed
220 * and take the heat that one cannot debug wired .text sections.
222 result = vm_map_lookup(&fs.map, vaddr,
223 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
224 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
225 if (result != KERN_SUCCESS) {
230 * If we don't COW now, on a user wire, the user will never
231 * be able to write to the mapping. If we don't make this
232 * restriction, the bookkeeping would be nearly impossible.
234 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
235 fs.entry->max_protection &= ~VM_PROT_WRITE;
238 map_generation = fs.map->timestamp;
240 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
241 panic("vm_fault: fault on nofault entry, addr: %lx",
246 * Make a reference to this object to prevent its disposal while we
247 * are messing with it. Once we have the reference, the map is free
248 * to be diddled. Since objects reference their shadows (and copies),
249 * they will stay around as well.
251 vm_object_reference(fs.first_object);
252 vm_object_pip_add(fs.first_object, 1);
254 fs.vp = vnode_pager_lock(fs.first_object);
255 if ((fault_type & VM_PROT_WRITE) &&
256 (fs.first_object->type == OBJT_VNODE)) {
257 vm_freeze_copyopts(fs.first_object,
258 fs.first_pindex, fs.first_pindex + 1);
261 fs.lookup_still_valid = TRUE;
269 * Search for the page at object/offset.
272 fs.object = fs.first_object;
273 fs.pindex = fs.first_pindex;
277 * If the object is dead, we stop here
280 if (fs.object->flags & OBJ_DEAD) {
281 unlock_and_deallocate(&fs);
282 return (KERN_PROTECTION_FAILURE);
286 * See if page is resident
289 fs.m = vm_page_lookup(fs.object, fs.pindex);
293 * Wait/Retry if the page is busy. We have to do this
294 * if the page is busy via either PG_BUSY or
295 * vm_page_t->busy because the vm_pager may be using
296 * vm_page_t->busy for pageouts ( and even pageins if
297 * it is the vnode pager ), and we could end up trying
298 * to pagein and pageout the same page simultaneously.
300 * We can theoretically allow the busy case on a read
301 * fault if the page is marked valid, but since such
302 * pages are typically already pmap'd, putting that
303 * special case in might be more effort then it is
304 * worth. We cannot under any circumstances mess
305 * around with a vm_page_t->busy page except, perhaps,
308 if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
310 (void)vm_page_sleep_busy(fs.m, TRUE, "vmpfw");
312 vm_object_deallocate(fs.first_object);
318 vm_page_unqueue_nowakeup(fs.m);
321 if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
322 vm_page_activate(fs.m);
323 unlock_and_deallocate(&fs);
329 * Mark page busy for other processes, and the
330 * pagedaemon. If it still isn't completely valid
331 * (readable), jump to readrest, else break-out ( we
336 if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
337 fs.m->object != kernel_object && fs.m->object != kmem_object) {
345 * Page is not resident, If this is the search termination
346 * or the pager might contain the page, allocate a new page.
349 if (TRYPAGER || fs.object == fs.first_object) {
350 if (fs.pindex >= fs.object->size) {
351 unlock_and_deallocate(&fs);
352 return (KERN_PROTECTION_FAILURE);
356 * Allocate a new page for this object/offset pair.
359 if (!vm_page_count_severe()) {
360 fs.m = vm_page_alloc(fs.object, fs.pindex,
361 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO);
364 unlock_and_deallocate(&fs);
372 * We have found a valid page or we have allocated a new page.
373 * The page thus may not be valid or may not be entirely
376 * Attempt to fault-in the page if there is a chance that the
377 * pager has it, and potentially fault in additional pages
385 u_char behavior = vm_map_entry_behavior(fs.entry);
387 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
391 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
392 if (behind > VM_FAULT_READ_BEHIND)
393 behind = VM_FAULT_READ_BEHIND;
395 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
396 if (ahead > VM_FAULT_READ_AHEAD)
397 ahead = VM_FAULT_READ_AHEAD;
400 if ((fs.first_object->type != OBJT_DEVICE) &&
401 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
402 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
403 fs.pindex >= fs.entry->lastr &&
404 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
406 vm_pindex_t firstpindex, tmppindex;
408 if (fs.first_pindex < 2 * VM_FAULT_READ)
411 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
414 * note: partially valid pages cannot be
415 * included in the lookahead - NFS piecemeal
416 * writes will barf on it badly.
419 for(tmppindex = fs.first_pindex - 1;
420 tmppindex >= firstpindex;
423 mt = vm_page_lookup( fs.first_object, tmppindex);
424 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
427 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
432 vm_page_test_dirty(mt);
434 vm_page_protect(mt, VM_PROT_NONE);
435 vm_page_deactivate(mt);
446 * now we find out if any other pages should be paged
447 * in at this time this routine checks to see if the
448 * pages surrounding this fault reside in the same
449 * object as the page for this fault. If they do,
450 * then they are faulted in also into the object. The
451 * array "marray" returned contains an array of
452 * vm_page_t structs where one of them is the
453 * vm_page_t passed to the routine. The reqpage
454 * return value is the index into the marray for the
455 * vm_page_t passed to the routine.
457 * fs.m plus the additional pages are PG_BUSY'd.
459 faultcount = vm_fault_additional_pages(
460 fs.m, behind, ahead, marray, &reqpage);
463 * update lastr imperfectly (we do not know how much
464 * getpages will actually read), but good enough.
466 fs.entry->lastr = fs.pindex + faultcount - behind;
469 * Call the pager to retrieve the data, if any, after
470 * releasing the lock on the map. We hold a ref on
471 * fs.object and the pages are PG_BUSY'd.
476 vm_pager_get_pages(fs.object, marray, faultcount,
477 reqpage) : VM_PAGER_FAIL;
479 if (rv == VM_PAGER_OK) {
481 * Found the page. Leave it busy while we play
486 * Relookup in case pager changed page. Pager
487 * is responsible for disposition of old page
490 fs.m = vm_page_lookup(fs.object, fs.pindex);
492 unlock_and_deallocate(&fs);
497 break; /* break to PAGE HAS BEEN FOUND */
500 * Remove the bogus page (which does not exist at this
501 * object/offset); before doing so, we must get back
502 * our object lock to preserve our invariant.
504 * Also wake up any other process that may want to bring
507 * If this is the top-level object, we must leave the
508 * busy page to prevent another process from rushing
509 * past us, and inserting the page in that object at
510 * the same time that we are.
513 if (rv == VM_PAGER_ERROR)
514 printf("vm_fault: pager read error, pid %d (%s)\n",
515 curproc->p_pid, curproc->p_comm);
517 * Data outside the range of the pager or an I/O error
520 * XXX - the check for kernel_map is a kludge to work
521 * around having the machine panic on a kernel space
522 * fault w/ I/O error.
524 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
525 (rv == VM_PAGER_BAD)) {
528 unlock_and_deallocate(&fs);
529 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
531 if (fs.object != fs.first_object) {
535 * XXX - we cannot just fall out at this
536 * point, m has been freed and is invalid!
542 * We get here if the object has default pager (or unwiring)
543 * or the pager doesn't have the page.
545 if (fs.object == fs.first_object)
549 * Move on to the next object. Lock the next object before
550 * unlocking the current one.
553 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
554 next_object = fs.object->backing_object;
555 if (next_object == NULL) {
557 * If there's no object left, fill the page in the top
560 if (fs.object != fs.first_object) {
561 vm_object_pip_wakeup(fs.object);
563 fs.object = fs.first_object;
564 fs.pindex = fs.first_pindex;
570 * Zero the page if necessary and mark it valid.
572 if ((fs.m->flags & PG_ZERO) == 0) {
573 vm_page_zero_fill(fs.m);
578 fs.m->valid = VM_PAGE_BITS_ALL;
579 break; /* break to PAGE HAS BEEN FOUND */
581 if (fs.object != fs.first_object) {
582 vm_object_pip_wakeup(fs.object);
584 KASSERT(fs.object != next_object, ("object loop %p", next_object));
585 fs.object = next_object;
586 vm_object_pip_add(fs.object, 1);
590 KASSERT((fs.m->flags & PG_BUSY) != 0,
591 ("vm_fault: not busy after main loop"));
594 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
599 * If the page is being written, but isn't already owned by the
600 * top-level object, we have to copy it into a new page owned by the
604 if (fs.object != fs.first_object) {
606 * We only really need to copy if we want to write it.
609 if (fault_type & VM_PROT_WRITE) {
611 * This allows pages to be virtually copied from a
612 * backing_object into the first_object, where the
613 * backing object has no other refs to it, and cannot
614 * gain any more refs. Instead of a bcopy, we just
615 * move the page from the backing object to the
616 * first object. Note that we must mark the page
617 * dirty in the first object so that it will go out
618 * to swap when needed.
620 if (map_generation == fs.map->timestamp &&
622 * Only one shadow object
624 (fs.object->shadow_count == 1) &&
626 * No COW refs, except us
628 (fs.object->ref_count == 1) &&
630 * No one else can look this object up
632 (fs.object->handle == NULL) &&
634 * No other ways to look the object up
636 ((fs.object->type == OBJT_DEFAULT) ||
637 (fs.object->type == OBJT_SWAP)) &&
639 * We don't chase down the shadow chain
641 (fs.object == fs.first_object->backing_object) &&
644 * grab the lock if we need to
646 (fs.lookup_still_valid ||
647 lockmgr(&fs.map->lock, LK_EXCLUSIVE|LK_NOWAIT, (void *)0, curproc) == 0)
650 fs.lookup_still_valid = 1;
652 * get rid of the unnecessary page
654 vm_page_protect(fs.first_m, VM_PROT_NONE);
655 vm_page_free(fs.first_m);
659 * grab the page and put it into the
660 * process'es object. The page is
661 * automatically made dirty.
663 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
665 vm_page_busy(fs.first_m);
670 * Oh, well, lets copy it.
672 vm_page_copy(fs.m, fs.first_m);
677 * We no longer need the old page or object.
683 * fs.object != fs.first_object due to above
687 vm_object_pip_wakeup(fs.object);
690 * Only use the new page below...
695 fs.object = fs.first_object;
696 fs.pindex = fs.first_pindex;
699 prot &= ~VM_PROT_WRITE;
704 * We must verify that the maps have not changed since our last
708 if (!fs.lookup_still_valid &&
709 (fs.map->timestamp != map_generation)) {
710 vm_object_t retry_object;
711 vm_pindex_t retry_pindex;
712 vm_prot_t retry_prot;
715 * Since map entries may be pageable, make sure we can take a
716 * page fault on them.
720 * Unlock vnode before the lookup to avoid deadlock. E.G.
721 * avoid a deadlock between the inode and exec_map that can
722 * occur due to locks being obtained in different orders.
730 if (fs.map->infork) {
732 unlock_and_deallocate(&fs);
737 * To avoid trying to write_lock the map while another process
738 * has it read_locked (in vm_map_pageable), we do not try for
739 * write permission. If the page is still writable, we will
740 * get write permission. If it is not, or has been marked
741 * needs_copy, we enter the mapping without write permission,
742 * and will merely take another fault.
744 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
745 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
746 map_generation = fs.map->timestamp;
749 * If we don't need the page any longer, put it on the active
750 * list (the easiest thing to do here). If no one needs it,
751 * pageout will grab it eventually.
754 if (result != KERN_SUCCESS) {
756 unlock_and_deallocate(&fs);
759 fs.lookup_still_valid = TRUE;
761 if ((retry_object != fs.first_object) ||
762 (retry_pindex != fs.first_pindex)) {
764 unlock_and_deallocate(&fs);
768 * Check whether the protection has changed or the object has
769 * been copied while we left the map unlocked. Changing from
770 * read to write permission is OK - we leave the page
771 * write-protected, and catch the write fault. Changing from
772 * write to read permission means that we can't mark the page
773 * write-enabled after all.
779 * Put this page into the physical map. We had to do the unlock above
780 * because pmap_enter may cause other faults. We don't put the page
781 * back on the active queue until later so that the page-out daemon
782 * won't find us (yet).
785 if (prot & VM_PROT_WRITE) {
786 vm_page_flag_set(fs.m, PG_WRITEABLE);
787 vm_object_set_flag(fs.m->object,
788 OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY);
791 * If the fault is a write, we know that this page is being
792 * written NOW so dirty it explicitly to save on
793 * pmap_is_modified() calls later.
795 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
796 * if the page is already dirty to prevent data written with
797 * the expectation of being synced from not being synced.
798 * Likewise if this entry does not request NOSYNC then make
799 * sure the page isn't marked NOSYNC. Applications sharing
800 * data should use the same flags to avoid ping ponging.
802 * Also tell the backing pager, if any, that it should remove
803 * any swap backing since the page is now dirty.
805 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
806 if (fs.m->dirty == 0)
807 vm_page_flag_set(fs.m, PG_NOSYNC);
809 vm_page_flag_clear(fs.m, PG_NOSYNC);
811 if (fault_flags & VM_FAULT_DIRTY) {
815 vm_pager_page_unswapped(fs.m);
821 * Page had better still be busy
824 KASSERT(fs.m->flags & PG_BUSY,
825 ("vm_fault: page %p not busy!", fs.m));
830 * Sanity check: page must be completely valid or it is not fit to
831 * map into user space. vm_pager_get_pages() ensures this.
834 if (fs.m->valid != VM_PAGE_BITS_ALL) {
835 vm_page_zero_invalid(fs.m, TRUE);
836 printf("Warning: page %p partially invalid on fault\n", fs.m);
839 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
841 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
842 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
845 vm_page_flag_clear(fs.m, PG_ZERO);
846 vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
847 if (fault_flags & VM_FAULT_HOLD)
851 * If the page is not wired down, then put it where the pageout daemon
855 if (fault_flags & VM_FAULT_WIRE_MASK) {
859 vm_page_unwire(fs.m, 1);
861 vm_page_activate(fs.m);
864 mtx_lock_spin(&sched_lock);
865 if (curproc && (curproc->p_sflag & PS_INMEM) && curproc->p_stats) {
867 curproc->p_stats->p_ru.ru_majflt++;
869 curproc->p_stats->p_ru.ru_minflt++;
872 mtx_unlock_spin(&sched_lock);
875 * Unlock everything, and return
878 vm_page_wakeup(fs.m);
879 vm_object_deallocate(fs.first_object);
881 return (KERN_SUCCESS);
888 * Wire down a range of virtual addresses in a map.
891 vm_fault_wire(map, start, end)
893 vm_offset_t start, end;
896 register vm_offset_t va;
897 register pmap_t pmap;
900 pmap = vm_map_pmap(map);
903 * Inform the physical mapping system that the range of addresses may
904 * not fault, so that page tables and such can be locked down as well.
907 pmap_pageable(pmap, start, end, FALSE);
910 * We simulate a fault to get the page and enter it in the physical
914 for (va = start; va < end; va += PAGE_SIZE) {
915 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
916 VM_FAULT_CHANGE_WIRING);
919 vm_fault_unwire(map, start, va);
923 return (KERN_SUCCESS);
927 * vm_fault_user_wire:
929 * Wire down a range of virtual addresses in a map. This
930 * is for user mode though, so we only ask for read access
931 * on currently read only sections.
934 vm_fault_user_wire(map, start, end)
936 vm_offset_t start, end;
939 register vm_offset_t va;
940 register pmap_t pmap;
943 pmap = vm_map_pmap(map);
946 * Inform the physical mapping system that the range of addresses may
947 * not fault, so that page tables and such can be locked down as well.
950 pmap_pageable(pmap, start, end, FALSE);
953 * We simulate a fault to get the page and enter it in the physical
956 for (va = start; va < end; va += PAGE_SIZE) {
957 rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_USER_WIRE);
960 vm_fault_unwire(map, start, va);
964 return (KERN_SUCCESS);
971 * Unwire a range of virtual addresses in a map.
974 vm_fault_unwire(map, start, end)
976 vm_offset_t start, end;
979 register vm_offset_t va, pa;
980 register pmap_t pmap;
982 pmap = vm_map_pmap(map);
985 * Since the pages are wired down, we must be able to get their
986 * mappings from the physical map system.
989 for (va = start; va < end; va += PAGE_SIZE) {
990 pa = pmap_extract(pmap, va);
991 if (pa != (vm_offset_t) 0) {
992 pmap_change_wiring(pmap, va, FALSE);
993 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
998 * Inform the physical mapping system that the range of addresses may
999 * fault, so that page tables and such may be unwired themselves.
1002 pmap_pageable(pmap, start, end, TRUE);
1008 * vm_fault_copy_entry
1010 * Copy all of the pages from a wired-down map entry to another.
1012 * In/out conditions:
1013 * The source and destination maps must be locked for write.
1014 * The source map entry must be wired down (or be a sharing map
1015 * entry corresponding to a main map entry that is wired down).
1019 vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry)
1022 vm_map_entry_t dst_entry;
1023 vm_map_entry_t src_entry;
1025 vm_object_t dst_object;
1026 vm_object_t src_object;
1027 vm_ooffset_t dst_offset;
1028 vm_ooffset_t src_offset;
1038 src_object = src_entry->object.vm_object;
1039 src_offset = src_entry->offset;
1042 * Create the top-level object for the destination entry. (Doesn't
1043 * actually shadow anything - we copy the pages directly.)
1045 dst_object = vm_object_allocate(OBJT_DEFAULT,
1046 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1048 dst_entry->object.vm_object = dst_object;
1049 dst_entry->offset = 0;
1051 prot = dst_entry->max_protection;
1054 * Loop through all of the pages in the entry's range, copying each
1055 * one from the source object (it should be there) to the destination
1058 for (vaddr = dst_entry->start, dst_offset = 0;
1059 vaddr < dst_entry->end;
1060 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1063 * Allocate a page in the destination object
1066 dst_m = vm_page_alloc(dst_object,
1067 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1068 if (dst_m == NULL) {
1071 } while (dst_m == NULL);
1074 * Find the page in the source object, and copy it in.
1075 * (Because the source is wired down, the page will be in
1078 src_m = vm_page_lookup(src_object,
1079 OFF_TO_IDX(dst_offset + src_offset));
1081 panic("vm_fault_copy_wired: page missing");
1083 vm_page_copy(src_m, dst_m);
1086 * Enter it in the pmap...
1089 vm_page_flag_clear(dst_m, PG_ZERO);
1090 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1091 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1094 * Mark it no longer busy, and put it on the active list.
1096 vm_page_activate(dst_m);
1097 vm_page_wakeup(dst_m);
1103 * This routine checks around the requested page for other pages that
1104 * might be able to be faulted in. This routine brackets the viable
1105 * pages for the pages to be paged in.
1108 * m, rbehind, rahead
1111 * marray (array of vm_page_t), reqpage (index of requested page)
1114 * number of pages in marray
1117 vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage)
1126 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1128 int cbehind, cahead;
1134 * we don't fault-ahead for device pager
1136 if (object->type == OBJT_DEVICE) {
1143 * if the requested page is not available, then give up now
1146 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1150 if ((cbehind == 0) && (cahead == 0)) {
1156 if (rahead > cahead) {
1160 if (rbehind > cbehind) {
1165 * try to do any readahead that we might have free pages for.
1167 if ((rahead + rbehind) >
1168 ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) {
1169 pagedaemon_wakeup();
1176 * scan backward for the read behind pages -- in memory
1179 if (rbehind > pindex) {
1183 startpindex = pindex - rbehind;
1186 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1187 if (vm_page_lookup( object, tpindex)) {
1188 startpindex = tpindex + 1;
1195 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1197 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1199 for (j = 0; j < i; j++) {
1200 vm_page_free(marray[j]);
1215 /* page offset of the required page */
1218 tpindex = pindex + 1;
1222 * scan forward for the read ahead pages
1224 endpindex = tpindex + rahead;
1225 if (endpindex > object->size)
1226 endpindex = object->size;
1228 for( ; tpindex < endpindex; i++, tpindex++) {
1230 if (vm_page_lookup(object, tpindex)) {
1234 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1242 /* return number of bytes of pages */