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.18 2004/10/12 19:29:34 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>
97 #include <sys/thread2.h>
98 #include <vm/vm_page2.h>
100 static int vm_fault_additional_pages (vm_page_t, int,
101 int, vm_page_t *, int *);
103 #define VM_FAULT_READ_AHEAD 8
104 #define VM_FAULT_READ_BEHIND 7
105 #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1)
112 vm_object_t first_object;
113 vm_pindex_t first_pindex;
115 vm_map_entry_t entry;
116 int lookup_still_valid;
121 release_page(struct faultstate *fs)
123 vm_page_wakeup(fs->m);
124 vm_page_deactivate(fs->m);
129 unlock_map(struct faultstate *fs)
131 if (fs->lookup_still_valid) {
132 vm_map_lookup_done(fs->map, fs->entry, 0);
133 fs->lookup_still_valid = FALSE;
138 _unlock_things(struct faultstate *fs, int dealloc)
140 vm_object_pip_wakeup(fs->object);
141 if (fs->object != fs->first_object) {
142 vm_page_free(fs->first_m);
143 vm_object_pip_wakeup(fs->first_object);
147 vm_object_deallocate(fs->first_object);
150 if (fs->vp != NULL) {
156 #define unlock_things(fs) _unlock_things(fs, 0)
157 #define unlock_and_deallocate(fs) _unlock_things(fs, 1)
160 * TRYPAGER - used by vm_fault to calculate whether the pager for the
161 * current object *might* contain the page.
163 * default objects are zero-fill, there is no real pager.
166 #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \
167 (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired))
172 * Handle a page fault occurring at the given address,
173 * requiring the given permissions, in the map specified.
174 * If successful, the page is inserted into the
175 * associated physical map.
177 * NOTE: the given address should be truncated to the
178 * proper page address.
180 * KERN_SUCCESS is returned if the page fault is handled; otherwise,
181 * a standard error specifying why the fault is fatal is returned.
184 * The map in question must be referenced, and remains so.
185 * Caller may hold no locks.
188 vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags)
194 vm_object_t next_object;
195 vm_page_t marray[VM_FAULT_READ];
198 struct faultstate fs;
200 mycpu->gd_cnt.v_vm_faults++;
205 * Find the backing store object and offset into it to begin the
209 if ((result = vm_map_lookup(&fs.map, vaddr,
210 fault_type, &fs.entry, &fs.first_object,
211 &fs.first_pindex, &prot, &wired)) != KERN_SUCCESS) {
212 if ((result != KERN_PROTECTION_FAILURE) ||
213 ((fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)) {
218 * If we are user-wiring a r/w segment, and it is COW, then
219 * we need to do the COW operation. Note that we don't COW
220 * currently RO sections now, because it is NOT desirable
221 * to COW .text. We simply keep .text from ever being COW'ed
222 * and take the heat that one cannot debug wired .text sections.
224 result = vm_map_lookup(&fs.map, vaddr,
225 VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE,
226 &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired);
227 if (result != KERN_SUCCESS) {
232 * If we don't COW now, on a user wire, the user will never
233 * be able to write to the mapping. If we don't make this
234 * restriction, the bookkeeping would be nearly impossible.
236 if ((fs.entry->protection & VM_PROT_WRITE) == 0)
237 fs.entry->max_protection &= ~VM_PROT_WRITE;
240 map_generation = fs.map->timestamp;
242 if (fs.entry->eflags & MAP_ENTRY_NOFAULT) {
243 panic("vm_fault: fault on nofault entry, addr: %lx",
248 * Make a reference to this object to prevent its disposal while we
249 * are messing with it. Once we have the reference, the map is free
250 * to be diddled. Since objects reference their shadows (and copies),
251 * they will stay around as well.
253 * Bump the paging-in-progress count to prevent size changes (e.g.
254 * truncation operations) during I/O. This must be done after
255 * obtaining the vnode lock in order to avoid possible deadlocks.
257 vm_object_reference(fs.first_object);
258 fs.vp = vnode_pager_lock(fs.first_object);
259 vm_object_pip_add(fs.first_object, 1);
261 if ((fault_type & VM_PROT_WRITE) &&
262 (fs.first_object->type == OBJT_VNODE)) {
263 vm_freeze_copyopts(fs.first_object,
264 fs.first_pindex, fs.first_pindex + 1);
267 fs.lookup_still_valid = TRUE;
275 * Search for the page at object/offset.
278 fs.object = fs.first_object;
279 fs.pindex = fs.first_pindex;
283 * If the object is dead, we stop here
286 if (fs.object->flags & OBJ_DEAD) {
287 unlock_and_deallocate(&fs);
288 return (KERN_PROTECTION_FAILURE);
292 * See if page is resident. spl protection is required
293 * to avoid an interrupt unbusy/free race against our
294 * lookup. We must hold the protection through a page
295 * allocation or busy.
298 fs.m = vm_page_lookup(fs.object, fs.pindex);
302 * Wait/Retry if the page is busy. We have to do this
303 * if the page is busy via either PG_BUSY or
304 * vm_page_t->busy because the vm_pager may be using
305 * vm_page_t->busy for pageouts ( and even pageins if
306 * it is the vnode pager ), and we could end up trying
307 * to pagein and pageout the same page simultaneously.
309 * We can theoretically allow the busy case on a read
310 * fault if the page is marked valid, but since such
311 * pages are typically already pmap'd, putting that
312 * special case in might be more effort then it is
313 * worth. We cannot under any circumstances mess
314 * around with a vm_page_t->busy page except, perhaps,
317 if ((fs.m->flags & PG_BUSY) || fs.m->busy) {
319 vm_page_sleep_busy(fs.m, TRUE, "vmpfw");
320 mycpu->gd_cnt.v_intrans++;
321 vm_object_deallocate(fs.first_object);
327 vm_page_unqueue_nowakeup(fs.m);
329 if ((queue - fs.m->pc) == PQ_CACHE && vm_page_count_severe()) {
330 vm_page_activate(fs.m);
331 unlock_and_deallocate(&fs);
338 * Mark page busy for other processes, and the
339 * pagedaemon. If it still isn't completely valid
340 * (readable), jump to readrest, else break-out ( we
343 * We can release the spl once we have marked the
350 if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) &&
351 fs.m->object != kernel_object && fs.m->object != kmem_object) {
359 * Page is not resident, If this is the search termination
360 * or the pager might contain the page, allocate a new page.
362 * note: we are still in splvm().
365 if (TRYPAGER || fs.object == fs.first_object) {
366 if (fs.pindex >= fs.object->size) {
368 unlock_and_deallocate(&fs);
369 return (KERN_PROTECTION_FAILURE);
373 * Allocate a new page for this object/offset pair.
376 if (!vm_page_count_severe()) {
377 fs.m = vm_page_alloc(fs.object, fs.pindex,
378 (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_NORMAL | VM_ALLOC_ZERO);
382 unlock_and_deallocate(&fs);
391 * We have found a valid page or we have allocated a new page.
392 * The page thus may not be valid or may not be entirely
395 * Attempt to fault-in the page if there is a chance that the
396 * pager has it, and potentially fault in additional pages
399 * We are NOT in splvm here and if TRYPAGER is true then
400 * fs.m will be non-NULL and will be PG_BUSY for us.
407 u_char behavior = vm_map_entry_behavior(fs.entry);
409 if (behavior == MAP_ENTRY_BEHAV_RANDOM) {
413 behind = (vaddr - fs.entry->start) >> PAGE_SHIFT;
414 if (behind > VM_FAULT_READ_BEHIND)
415 behind = VM_FAULT_READ_BEHIND;
417 ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1;
418 if (ahead > VM_FAULT_READ_AHEAD)
419 ahead = VM_FAULT_READ_AHEAD;
422 if ((fs.first_object->type != OBJT_DEVICE) &&
423 (behavior == MAP_ENTRY_BEHAV_SEQUENTIAL ||
424 (behavior != MAP_ENTRY_BEHAV_RANDOM &&
425 fs.pindex >= fs.entry->lastr &&
426 fs.pindex < fs.entry->lastr + VM_FAULT_READ))
428 vm_pindex_t firstpindex, tmppindex;
430 if (fs.first_pindex < 2 * VM_FAULT_READ)
433 firstpindex = fs.first_pindex - 2 * VM_FAULT_READ;
436 * note: partially valid pages cannot be
437 * included in the lookahead - NFS piecemeal
438 * writes will barf on it badly.
440 * spl protection is required to avoid races
441 * between the lookup and an interrupt
442 * unbusy/free sequence occuring prior to
446 for (tmppindex = fs.first_pindex - 1;
447 tmppindex >= firstpindex;
451 mt = vm_page_lookup( fs.first_object, tmppindex);
452 if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL))
455 (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) ||
460 vm_page_test_dirty(mt);
462 vm_page_protect(mt, VM_PROT_NONE);
463 vm_page_deactivate(mt);
475 * now we find out if any other pages should be paged
476 * in at this time this routine checks to see if the
477 * pages surrounding this fault reside in the same
478 * object as the page for this fault. If they do,
479 * then they are faulted in also into the object. The
480 * array "marray" returned contains an array of
481 * vm_page_t structs where one of them is the
482 * vm_page_t passed to the routine. The reqpage
483 * return value is the index into the marray for the
484 * vm_page_t passed to the routine.
486 * fs.m plus the additional pages are PG_BUSY'd.
488 faultcount = vm_fault_additional_pages(
489 fs.m, behind, ahead, marray, &reqpage);
492 * update lastr imperfectly (we do not know how much
493 * getpages will actually read), but good enough.
495 fs.entry->lastr = fs.pindex + faultcount - behind;
498 * Call the pager to retrieve the data, if any, after
499 * releasing the lock on the map. We hold a ref on
500 * fs.object and the pages are PG_BUSY'd.
505 vm_pager_get_pages(fs.object, marray, faultcount,
506 reqpage) : VM_PAGER_FAIL;
508 if (rv == VM_PAGER_OK) {
510 * Found the page. Leave it busy while we play
515 * Relookup in case pager changed page. Pager
516 * is responsible for disposition of old page
519 * XXX other code segments do relookups too.
520 * It's a bad abstraction that needs to be
523 fs.m = vm_page_lookup(fs.object, fs.pindex);
525 unlock_and_deallocate(&fs);
530 break; /* break to PAGE HAS BEEN FOUND */
533 * Remove the bogus page (which does not exist at this
534 * object/offset); before doing so, we must get back
535 * our object lock to preserve our invariant.
537 * Also wake up any other process that may want to bring
540 * If this is the top-level object, we must leave the
541 * busy page to prevent another process from rushing
542 * past us, and inserting the page in that object at
543 * the same time that we are.
546 if (rv == VM_PAGER_ERROR)
547 printf("vm_fault: pager read error, pid %d (%s)\n",
548 curproc->p_pid, curproc->p_comm);
550 * Data outside the range of the pager or an I/O error
553 * XXX - the check for kernel_map is a kludge to work
554 * around having the machine panic on a kernel space
555 * fault w/ I/O error.
557 if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) ||
558 (rv == VM_PAGER_BAD)) {
561 unlock_and_deallocate(&fs);
562 return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE);
564 if (fs.object != fs.first_object) {
568 * XXX - we cannot just fall out at this
569 * point, m has been freed and is invalid!
575 * We get here if the object has default pager (or unwiring)
576 * or the pager doesn't have the page.
578 if (fs.object == fs.first_object)
582 * Move on to the next object. Lock the next object before
583 * unlocking the current one.
586 fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset);
587 next_object = fs.object->backing_object;
588 if (next_object == NULL) {
590 * If there's no object left, fill the page in the top
593 if (fs.object != fs.first_object) {
594 vm_object_pip_wakeup(fs.object);
596 fs.object = fs.first_object;
597 fs.pindex = fs.first_pindex;
603 * Zero the page if necessary and mark it valid.
605 if ((fs.m->flags & PG_ZERO) == 0) {
606 vm_page_zero_fill(fs.m);
608 mycpu->gd_cnt.v_ozfod++;
610 mycpu->gd_cnt.v_zfod++;
611 fs.m->valid = VM_PAGE_BITS_ALL;
612 break; /* break to PAGE HAS BEEN FOUND */
614 if (fs.object != fs.first_object) {
615 vm_object_pip_wakeup(fs.object);
617 KASSERT(fs.object != next_object, ("object loop %p", next_object));
618 fs.object = next_object;
619 vm_object_pip_add(fs.object, 1);
623 KASSERT((fs.m->flags & PG_BUSY) != 0,
624 ("vm_fault: not busy after main loop"));
627 * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock
632 * If the page is being written, but isn't already owned by the
633 * top-level object, we have to copy it into a new page owned by the
637 if (fs.object != fs.first_object) {
639 * We only really need to copy if we want to write it.
642 if (fault_type & VM_PROT_WRITE) {
644 * This allows pages to be virtually copied from a
645 * backing_object into the first_object, where the
646 * backing object has no other refs to it, and cannot
647 * gain any more refs. Instead of a bcopy, we just
648 * move the page from the backing object to the
649 * first object. Note that we must mark the page
650 * dirty in the first object so that it will go out
651 * to swap when needed.
653 if (map_generation == fs.map->timestamp &&
655 * Only one shadow object
657 (fs.object->shadow_count == 1) &&
659 * No COW refs, except us
661 (fs.object->ref_count == 1) &&
663 * No one else can look this object up
665 (fs.object->handle == NULL) &&
667 * No other ways to look the object up
669 ((fs.object->type == OBJT_DEFAULT) ||
670 (fs.object->type == OBJT_SWAP)) &&
672 * We don't chase down the shadow chain
674 (fs.object == fs.first_object->backing_object) &&
677 * grab the lock if we need to
679 (fs.lookup_still_valid ||
680 lockmgr(&fs.map->lock, LK_EXCLUSIVE|LK_NOWAIT, NULL, curthread) == 0)
683 fs.lookup_still_valid = 1;
685 * get rid of the unnecessary page
687 vm_page_protect(fs.first_m, VM_PROT_NONE);
688 vm_page_free(fs.first_m);
692 * grab the page and put it into the
693 * process'es object. The page is
694 * automatically made dirty.
696 vm_page_rename(fs.m, fs.first_object, fs.first_pindex);
698 vm_page_busy(fs.first_m);
700 mycpu->gd_cnt.v_cow_optim++;
703 * Oh, well, lets copy it.
705 vm_page_copy(fs.m, fs.first_m);
710 * We no longer need the old page or object.
716 * fs.object != fs.first_object due to above
720 vm_object_pip_wakeup(fs.object);
723 * Only use the new page below...
726 mycpu->gd_cnt.v_cow_faults++;
728 fs.object = fs.first_object;
729 fs.pindex = fs.first_pindex;
732 prot &= ~VM_PROT_WRITE;
737 * We must verify that the maps have not changed since our last
741 if (!fs.lookup_still_valid &&
742 (fs.map->timestamp != map_generation)) {
743 vm_object_t retry_object;
744 vm_pindex_t retry_pindex;
745 vm_prot_t retry_prot;
748 * Since map entries may be pageable, make sure we can take a
749 * page fault on them.
753 * Unlock vnode before the lookup to avoid deadlock. E.G.
754 * avoid a deadlock between the inode and exec_map that can
755 * occur due to locks being obtained in different orders.
763 if (fs.map->infork) {
765 unlock_and_deallocate(&fs);
770 * To avoid trying to write_lock the map while another process
771 * has it read_locked (in vm_map_wire), we do not try for
772 * write permission. If the page is still writable, we will
773 * get write permission. If it is not, or has been marked
774 * needs_copy, we enter the mapping without write permission,
775 * and will merely take another fault.
777 result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE,
778 &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired);
779 map_generation = fs.map->timestamp;
782 * If we don't need the page any longer, put it on the active
783 * list (the easiest thing to do here). If no one needs it,
784 * pageout will grab it eventually.
787 if (result != KERN_SUCCESS) {
789 unlock_and_deallocate(&fs);
792 fs.lookup_still_valid = TRUE;
794 if ((retry_object != fs.first_object) ||
795 (retry_pindex != fs.first_pindex)) {
797 unlock_and_deallocate(&fs);
801 * Check whether the protection has changed or the object has
802 * been copied while we left the map unlocked. Changing from
803 * read to write permission is OK - we leave the page
804 * write-protected, and catch the write fault. Changing from
805 * write to read permission means that we can't mark the page
806 * write-enabled after all.
812 * Put this page into the physical map. We had to do the unlock above
813 * because pmap_enter may cause other faults. We don't put the page
814 * back on the active queue until later so that the page-out daemon
815 * won't find us (yet).
818 if (prot & VM_PROT_WRITE) {
819 vm_page_flag_set(fs.m, PG_WRITEABLE);
820 vm_object_set_writeable_dirty(fs.m->object);
823 * If the fault is a write, we know that this page is being
824 * written NOW so dirty it explicitly to save on
825 * pmap_is_modified() calls later.
827 * If this is a NOSYNC mmap we do not want to set PG_NOSYNC
828 * if the page is already dirty to prevent data written with
829 * the expectation of being synced from not being synced.
830 * Likewise if this entry does not request NOSYNC then make
831 * sure the page isn't marked NOSYNC. Applications sharing
832 * data should use the same flags to avoid ping ponging.
834 * Also tell the backing pager, if any, that it should remove
835 * any swap backing since the page is now dirty.
837 if (fs.entry->eflags & MAP_ENTRY_NOSYNC) {
838 if (fs.m->dirty == 0)
839 vm_page_flag_set(fs.m, PG_NOSYNC);
841 vm_page_flag_clear(fs.m, PG_NOSYNC);
843 if (fault_flags & VM_FAULT_DIRTY) {
846 vm_pager_page_unswapped(fs.m);
852 * Page had better still be busy
855 KASSERT(fs.m->flags & PG_BUSY,
856 ("vm_fault: page %p not busy!", fs.m));
861 * Sanity check: page must be completely valid or it is not fit to
862 * map into user space. vm_pager_get_pages() ensures this.
865 if (fs.m->valid != VM_PAGE_BITS_ALL) {
866 vm_page_zero_invalid(fs.m, TRUE);
867 printf("Warning: page %p partially invalid on fault\n", fs.m);
870 pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired);
872 if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) {
873 pmap_prefault(fs.map->pmap, vaddr, fs.entry);
876 vm_page_flag_clear(fs.m, PG_ZERO);
877 vm_page_flag_set(fs.m, PG_MAPPED|PG_REFERENCED);
878 if (fault_flags & VM_FAULT_HOLD)
882 * If the page is not wired down, then put it where the pageout daemon
886 if (fault_flags & VM_FAULT_WIRE_MASK) {
890 vm_page_unwire(fs.m, 1);
892 vm_page_activate(fs.m);
895 if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) {
897 curproc->p_stats->p_ru.ru_majflt++;
899 curproc->p_stats->p_ru.ru_minflt++;
904 * Unlock everything, and return
907 vm_page_wakeup(fs.m);
908 vm_object_deallocate(fs.first_object);
910 return (KERN_SUCCESS);
915 * quick version of vm_fault
918 vm_fault_quick(caddr_t v, int prot)
922 if (prot & VM_PROT_WRITE)
923 r = subyte(v, fubyte(v));
930 * Wire down a range of virtual addresses in a map. The entry in question
931 * should be marked in-transition and the map must be locked. We must
932 * release the map temporarily while faulting-in the page to avoid a
933 * deadlock. Note that the entry may be clipped while we are blocked but
934 * will never be freed.
937 vm_fault_wire(vm_map_t map, vm_map_entry_t entry, boolean_t user_wire)
939 boolean_t fictitious;
947 pmap = vm_map_pmap(map);
948 start = entry->start;
950 fictitious = entry->object.vm_object &&
951 (entry->object.vm_object->type == OBJT_DEVICE);
957 * We simulate a fault to get the page and enter it in the physical
960 for (va = start; va < end; va += PAGE_SIZE) {
962 rv = vm_fault(map, va, VM_PROT_READ,
965 rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE,
966 VM_FAULT_CHANGE_WIRING);
971 if ((pa = pmap_extract(pmap, va)) == 0)
973 pmap_change_wiring(pmap, va, FALSE);
975 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
982 return (KERN_SUCCESS);
986 * Unwire a range of virtual addresses in a map. The map should be
990 vm_fault_unwire(vm_map_t map, vm_map_entry_t entry)
992 boolean_t fictitious;
999 pmap = vm_map_pmap(map);
1000 start = entry->start;
1002 fictitious = entry->object.vm_object &&
1003 (entry->object.vm_object->type == OBJT_DEVICE);
1006 * Since the pages are wired down, we must be able to get their
1007 * mappings from the physical map system.
1009 for (va = start; va < end; va += PAGE_SIZE) {
1010 pa = pmap_extract(pmap, va);
1012 pmap_change_wiring(pmap, va, FALSE);
1014 vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1);
1021 * vm_fault_copy_entry
1023 * Copy all of the pages from a wired-down map entry to another.
1025 * In/out conditions:
1026 * The source and destination maps must be locked for write.
1027 * The source map entry must be wired down (or be a sharing map
1028 * entry corresponding to a main map entry that is wired down).
1032 vm_fault_copy_entry(vm_map_t dst_map, vm_map_t src_map,
1033 vm_map_entry_t dst_entry, vm_map_entry_t src_entry)
1035 vm_object_t dst_object;
1036 vm_object_t src_object;
1037 vm_ooffset_t dst_offset;
1038 vm_ooffset_t src_offset;
1048 src_object = src_entry->object.vm_object;
1049 src_offset = src_entry->offset;
1052 * Create the top-level object for the destination entry. (Doesn't
1053 * actually shadow anything - we copy the pages directly.)
1055 dst_object = vm_object_allocate(OBJT_DEFAULT,
1056 (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start));
1058 dst_entry->object.vm_object = dst_object;
1059 dst_entry->offset = 0;
1061 prot = dst_entry->max_protection;
1064 * Loop through all of the pages in the entry's range, copying each
1065 * one from the source object (it should be there) to the destination
1068 for (vaddr = dst_entry->start, dst_offset = 0;
1069 vaddr < dst_entry->end;
1070 vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) {
1073 * Allocate a page in the destination object
1076 dst_m = vm_page_alloc(dst_object,
1077 OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL);
1078 if (dst_m == NULL) {
1081 } while (dst_m == NULL);
1084 * Find the page in the source object, and copy it in.
1085 * (Because the source is wired down, the page will be in
1088 src_m = vm_page_lookup(src_object,
1089 OFF_TO_IDX(dst_offset + src_offset));
1091 panic("vm_fault_copy_wired: page missing");
1093 vm_page_copy(src_m, dst_m);
1096 * Enter it in the pmap...
1099 vm_page_flag_clear(dst_m, PG_ZERO);
1100 pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE);
1101 vm_page_flag_set(dst_m, PG_WRITEABLE|PG_MAPPED);
1104 * Mark it no longer busy, and put it on the active list.
1106 vm_page_activate(dst_m);
1107 vm_page_wakeup(dst_m);
1113 * This routine checks around the requested page for other pages that
1114 * might be able to be faulted in. This routine brackets the viable
1115 * pages for the pages to be paged in.
1118 * m, rbehind, rahead
1121 * marray (array of vm_page_t), reqpage (index of requested page)
1124 * number of pages in marray
1127 vm_fault_additional_pages(vm_page_t m, int rbehind, int rahead,
1128 vm_page_t *marray, int *reqpage)
1132 vm_pindex_t pindex, startpindex, endpindex, tpindex;
1134 int cbehind, cahead;
1140 * we don't fault-ahead for device pager
1142 if (object->type == OBJT_DEVICE) {
1149 * if the requested page is not available, then give up now
1152 if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) {
1156 if ((cbehind == 0) && (cahead == 0)) {
1162 if (rahead > cahead) {
1166 if (rbehind > cbehind) {
1171 * try to do any readahead that we might have free pages for.
1173 if ((rahead + rbehind) >
1174 ((vmstats.v_free_count + vmstats.v_cache_count) - vmstats.v_free_reserved)) {
1175 pagedaemon_wakeup();
1182 * scan backward for the read behind pages -- in memory
1184 * Assume that if the page is not found an interrupt will not
1185 * create it. Theoretically interrupts can only remove (busy)
1186 * pages, not create new associations.
1189 if (rbehind > pindex) {
1193 startpindex = pindex - rbehind;
1197 for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) {
1198 if (vm_page_lookup( object, tpindex)) {
1199 startpindex = tpindex + 1;
1206 for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) {
1208 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1211 for (j = 0; j < i; j++) {
1212 vm_page_free(marray[j]);
1228 /* page offset of the required page */
1231 tpindex = pindex + 1;
1235 * scan forward for the read ahead pages
1237 endpindex = tpindex + rahead;
1238 if (endpindex > object->size)
1239 endpindex = object->size;
1242 for( ; tpindex < endpindex; i++, tpindex++) {
1244 if (vm_page_lookup(object, tpindex)) {
1248 rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL);
1257 /* return number of bytes of pages */