1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 426f9a767SRodney W. Grimes * Copyright (c) 1994 John S. Dyson 526f9a767SRodney W. Grimes * All rights reserved. 626f9a767SRodney W. Grimes * Copyright (c) 1994 David Greenman 726f9a767SRodney W. Grimes * All rights reserved. 826f9a767SRodney W. Grimes * 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 11df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 12df8bae1dSRodney W. Grimes * 13df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 14df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 15df8bae1dSRodney W. Grimes * are met: 16df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 17df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 18df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 19df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 20df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 21df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 22df8bae1dSRodney W. Grimes * must display the following acknowledgement: 23df8bae1dSRodney W. Grimes * This product includes software developed by the University of 24df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 25df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 26df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 27df8bae1dSRodney W. Grimes * without specific prior written permission. 28df8bae1dSRodney W. Grimes * 29df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 30df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 31df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 32df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 33df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 34df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 35df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 37df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 38df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 39df8bae1dSRodney W. Grimes * SUCH DAMAGE. 40df8bae1dSRodney W. Grimes * 413c4dd356SDavid Greenman * from: @(#)vm_fault.c 8.4 (Berkeley) 1/12/94 42df8bae1dSRodney W. Grimes * 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 45df8bae1dSRodney W. Grimes * All rights reserved. 46df8bae1dSRodney W. Grimes * 47df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 48df8bae1dSRodney W. Grimes * 49df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 50df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 51df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 52df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 53df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 54df8bae1dSRodney W. Grimes * 55df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 56df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 57df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 62df8bae1dSRodney W. Grimes * School of Computer Science 63df8bae1dSRodney W. Grimes * Carnegie Mellon University 64df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 65df8bae1dSRodney W. Grimes * 66df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 67df8bae1dSRodney W. Grimes * rights to redistribute these changes. 683c4dd356SDavid Greenman * 69d474eaaaSDoug Rabson * $Id: vm_fault.c,v 1.85 1998/07/22 09:38:04 dg Exp $ 70df8bae1dSRodney W. Grimes */ 71df8bae1dSRodney W. Grimes 72df8bae1dSRodney W. Grimes /* 73df8bae1dSRodney W. Grimes * Page fault handling module. 74df8bae1dSRodney W. Grimes */ 75df8bae1dSRodney W. Grimes 76df8bae1dSRodney W. Grimes #include <sys/param.h> 77df8bae1dSRodney W. Grimes #include <sys/systm.h> 7826f9a767SRodney W. Grimes #include <sys/proc.h> 7924a1cce3SDavid Greenman #include <sys/vnode.h> 8026f9a767SRodney W. Grimes #include <sys/resourcevar.h> 81efeaf95aSDavid Greenman #include <sys/vmmeter.h> 82df8bae1dSRodney W. Grimes 83df8bae1dSRodney W. Grimes #include <vm/vm.h> 84efeaf95aSDavid Greenman #include <vm/vm_param.h> 85efeaf95aSDavid Greenman #include <vm/vm_prot.h> 86996c772fSJohn Dyson #include <sys/lock.h> 87efeaf95aSDavid Greenman #include <vm/pmap.h> 88efeaf95aSDavid Greenman #include <vm/vm_map.h> 89efeaf95aSDavid Greenman #include <vm/vm_object.h> 90df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 92a83c285cSDavid Greenman #include <vm/vm_kern.h> 9324a1cce3SDavid Greenman #include <vm/vm_pager.h> 9424a1cce3SDavid Greenman #include <vm/vnode_pager.h> 95efeaf95aSDavid Greenman #include <vm/vm_extern.h> 96df8bae1dSRodney W. Grimes 97303b270bSEivind Eklund static int vm_fault_additional_pages __P((vm_page_t, int, 98303b270bSEivind Eklund int, vm_page_t *, int *)); 9926f9a767SRodney W. Grimes 10047221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8 10147221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7 10226f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1) 10326f9a767SRodney W. Grimes 1044866e085SJohn Dyson struct faultstate { 1054866e085SJohn Dyson vm_page_t m; 1064866e085SJohn Dyson vm_object_t object; 1074866e085SJohn Dyson vm_pindex_t pindex; 1084866e085SJohn Dyson vm_page_t first_m; 1094866e085SJohn Dyson vm_object_t first_object; 1104866e085SJohn Dyson vm_pindex_t first_pindex; 1114866e085SJohn Dyson vm_map_t map; 1124866e085SJohn Dyson vm_map_entry_t entry; 1134866e085SJohn Dyson int lookup_still_valid; 1144866e085SJohn Dyson struct vnode *vp; 1154866e085SJohn Dyson }; 1164866e085SJohn Dyson 1174866e085SJohn Dyson static void 1184866e085SJohn Dyson release_page(struct faultstate *fs) 1194866e085SJohn Dyson { 1204866e085SJohn Dyson PAGE_WAKEUP(fs->m); 1214866e085SJohn Dyson vm_page_deactivate(fs->m); 1224866e085SJohn Dyson fs->m = NULL; 1234866e085SJohn Dyson } 1244866e085SJohn Dyson 1254866e085SJohn Dyson static void 1264866e085SJohn Dyson unlock_map(struct faultstate *fs) 1274866e085SJohn Dyson { 1284866e085SJohn Dyson if (fs->lookup_still_valid) { 1294866e085SJohn Dyson vm_map_lookup_done(fs->map, fs->entry); 1304866e085SJohn Dyson fs->lookup_still_valid = FALSE; 1314866e085SJohn Dyson } 1324866e085SJohn Dyson } 1334866e085SJohn Dyson 1344866e085SJohn Dyson static void 1354866e085SJohn Dyson _unlock_things(struct faultstate *fs, int dealloc) 1364866e085SJohn Dyson { 1374866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 1384866e085SJohn Dyson if (fs->object != fs->first_object) { 1394866e085SJohn Dyson vm_page_free(fs->first_m); 1404866e085SJohn Dyson vm_object_pip_wakeup(fs->first_object); 1414866e085SJohn Dyson fs->first_m = NULL; 1424866e085SJohn Dyson } 1434866e085SJohn Dyson if (dealloc) { 1444866e085SJohn Dyson vm_object_deallocate(fs->first_object); 1454866e085SJohn Dyson } 1464866e085SJohn Dyson unlock_map(fs); 1474866e085SJohn Dyson if (fs->vp != NULL) { 1484866e085SJohn Dyson vput(fs->vp); 1494866e085SJohn Dyson fs->vp = NULL; 1504866e085SJohn Dyson } 1514866e085SJohn Dyson } 1524866e085SJohn Dyson 1534866e085SJohn Dyson #define unlock_things(fs) _unlock_things(fs, 0) 1544866e085SJohn Dyson #define unlock_and_deallocate(fs) _unlock_things(fs, 1) 1554866e085SJohn Dyson 156df8bae1dSRodney W. Grimes /* 157df8bae1dSRodney W. Grimes * vm_fault: 158df8bae1dSRodney W. Grimes * 159df8bae1dSRodney W. Grimes * Handle a page fault occuring at the given address, 160df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 161df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 162df8bae1dSRodney W. Grimes * associated physical map. 163df8bae1dSRodney W. Grimes * 164df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 165df8bae1dSRodney W. Grimes * proper page address. 166df8bae1dSRodney W. Grimes * 167df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 168df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 169df8bae1dSRodney W. Grimes * 170df8bae1dSRodney W. Grimes * 171df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 172df8bae1dSRodney W. Grimes * Caller may hold no locks. 173df8bae1dSRodney W. Grimes */ 174df8bae1dSRodney W. Grimes int 175b9dcd593SBruce Evans vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, int fault_flags) 176df8bae1dSRodney W. Grimes { 177df8bae1dSRodney W. Grimes vm_prot_t prot; 178df8bae1dSRodney W. Grimes int result; 179df8bae1dSRodney W. Grimes boolean_t wired; 1802d8acc0fSJohn Dyson int map_generation; 181df8bae1dSRodney W. Grimes vm_page_t old_m; 182df8bae1dSRodney W. Grimes vm_object_t next_object; 18326f9a767SRodney W. Grimes vm_page_t marray[VM_FAULT_READ]; 1844866e085SJohn Dyson int hardfault; 1852d8acc0fSJohn Dyson int faultcount; 186996c772fSJohn Dyson struct proc *p = curproc; /* XXX */ 1874866e085SJohn Dyson struct faultstate fs; 188df8bae1dSRodney W. Grimes 189b8d95f16SDavid Greenman cnt.v_vm_faults++; /* needs lock XXX */ 1904866e085SJohn Dyson hardfault = 0; 191df8bae1dSRodney W. Grimes 192df8bae1dSRodney W. Grimes RetryFault:; 1934866e085SJohn Dyson fs.map = map; 194df8bae1dSRodney W. Grimes 195df8bae1dSRodney W. Grimes /* 1960d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 1970d94caffSDavid Greenman * search. 198df8bae1dSRodney W. Grimes */ 1994866e085SJohn Dyson if ((result = vm_map_lookup(&fs.map, vaddr, 2004866e085SJohn Dyson fault_type, &fs.entry, &fs.first_object, 2014866e085SJohn Dyson &fs.first_pindex, &prot, &wired)) != KERN_SUCCESS) { 20247221757SJohn Dyson if ((result != KERN_PROTECTION_FAILURE) || 20347221757SJohn Dyson ((fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE)) { 20447221757SJohn Dyson return result; 20509e0c6ccSJohn Dyson } 20609e0c6ccSJohn Dyson 2077aaaa4fdSJohn Dyson /* 2087aaaa4fdSJohn Dyson * If we are user-wiring a r/w segment, and it is COW, then 2097aaaa4fdSJohn Dyson * we need to do the COW operation. Note that we don't COW 210595236dfSJohn Dyson * currently RO sections now, because it is NOT desirable 2117aaaa4fdSJohn Dyson * to COW .text. We simply keep .text from ever being COW'ed 2127aaaa4fdSJohn Dyson * and take the heat that one cannot debug wired .text sections. 2137aaaa4fdSJohn Dyson */ 2144866e085SJohn Dyson result = vm_map_lookup(&fs.map, vaddr, 21547221757SJohn Dyson VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE, 2164866e085SJohn Dyson &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired); 21747221757SJohn Dyson if (result != KERN_SUCCESS) { 21847221757SJohn Dyson return result; 21947221757SJohn Dyson } 22047221757SJohn Dyson 2217aaaa4fdSJohn Dyson /* 2227aaaa4fdSJohn Dyson * If we don't COW now, on a user wire, the user will never 2237aaaa4fdSJohn Dyson * be able to write to the mapping. If we don't make this 2247aaaa4fdSJohn Dyson * restriction, the bookkeeping would be nearly impossible. 2257aaaa4fdSJohn Dyson */ 2264866e085SJohn Dyson if ((fs.entry->protection & VM_PROT_WRITE) == 0) 2274866e085SJohn Dyson fs.entry->max_protection &= ~VM_PROT_WRITE; 2287aaaa4fdSJohn Dyson } 22947221757SJohn Dyson 2304866e085SJohn Dyson map_generation = fs.map->timestamp; 2312d8acc0fSJohn Dyson 2324866e085SJohn Dyson if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 23347221757SJohn Dyson panic("vm_fault: fault on nofault entry, addr: %lx", 23492c4c4ebSBruce Evans (u_long)vaddr); 2357aaaa4fdSJohn Dyson } 2367aaaa4fdSJohn Dyson 23795e5e988SJohn Dyson /* 23895e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 23995e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 24095e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 24195e5e988SJohn Dyson * they will stay around as well. 24295e5e988SJohn Dyson */ 2434866e085SJohn Dyson vm_object_reference(fs.first_object); 244d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 24595e5e988SJohn Dyson 2464866e085SJohn Dyson fs.vp = vnode_pager_lock(fs.first_object); 2471efb74fbSJohn Dyson if ((fault_type & VM_PROT_WRITE) && 2484866e085SJohn Dyson (fs.first_object->type == OBJT_VNODE)) { 2494866e085SJohn Dyson vm_freeze_copyopts(fs.first_object, 2504866e085SJohn Dyson fs.first_pindex, fs.first_pindex + 1); 2511efb74fbSJohn Dyson } 252f6b04d2bSDavid Greenman 2534866e085SJohn Dyson fs.lookup_still_valid = TRUE; 254df8bae1dSRodney W. Grimes 255df8bae1dSRodney W. Grimes if (wired) 256df8bae1dSRodney W. Grimes fault_type = prot; 257df8bae1dSRodney W. Grimes 2584866e085SJohn Dyson fs.first_m = NULL; 259df8bae1dSRodney W. Grimes 260df8bae1dSRodney W. Grimes /* 261df8bae1dSRodney W. Grimes * Search for the page at object/offset. 262df8bae1dSRodney W. Grimes */ 263df8bae1dSRodney W. Grimes 2644866e085SJohn Dyson fs.object = fs.first_object; 2654866e085SJohn Dyson fs.pindex = fs.first_pindex; 266df8bae1dSRodney W. Grimes 267df8bae1dSRodney W. Grimes /* 268df8bae1dSRodney W. Grimes * See whether this page is resident 269df8bae1dSRodney W. Grimes */ 270df8bae1dSRodney W. Grimes while (TRUE) { 27147221757SJohn Dyson 2724866e085SJohn Dyson if (fs.object->flags & OBJ_DEAD) { 2734866e085SJohn Dyson unlock_and_deallocate(&fs); 27447221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 27547221757SJohn Dyson } 27647221757SJohn Dyson 2774866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 2784866e085SJohn Dyson if (fs.m != NULL) { 279c82b0181SJohn Dyson int queue; 280df8bae1dSRodney W. Grimes /* 2810d94caffSDavid Greenman * If the page is being brought in, wait for it and 2820d94caffSDavid Greenman * then retry. 283df8bae1dSRodney W. Grimes */ 2844866e085SJohn Dyson if ((fs.m->flags & PG_BUSY) || 2854866e085SJohn Dyson (fs.m->busy && 2864866e085SJohn Dyson (fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 28716f62314SDavid Greenman int s; 2880d94caffSDavid Greenman 2894866e085SJohn Dyson unlock_things(&fs); 290b18bfc3dSJohn Dyson s = splvm(); 2914866e085SJohn Dyson if ((fs.m->flags & PG_BUSY) || 2924866e085SJohn Dyson (fs.m->busy && 2934866e085SJohn Dyson (fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 2944866e085SJohn Dyson fs.m->flags |= PG_WANTED | PG_REFERENCED; 295976e77fcSDavid Greenman cnt.v_intrans++; 2964866e085SJohn Dyson tsleep(fs.m, PSWP, "vmpfw", 0); 29726f9a767SRodney W. Grimes } 29816f62314SDavid Greenman splx(s); 2994866e085SJohn Dyson vm_object_deallocate(fs.first_object); 300df8bae1dSRodney W. Grimes goto RetryFault; 301df8bae1dSRodney W. Grimes } 302f6b04d2bSDavid Greenman 3034866e085SJohn Dyson queue = fs.m->queue; 3044866e085SJohn Dyson vm_page_unqueue_nowakeup(fs.m); 305c82b0181SJohn Dyson 306df8bae1dSRodney W. Grimes /* 30724a1cce3SDavid Greenman * Mark page busy for other processes, and the pagedaemon. 308df8bae1dSRodney W. Grimes */ 3094866e085SJohn Dyson if (((queue - fs.m->pc) == PQ_CACHE) && 310886d3e11SJohn Dyson (cnt.v_free_count + cnt.v_cache_count) < cnt.v_free_min) { 3114866e085SJohn Dyson vm_page_activate(fs.m); 3124866e085SJohn Dyson unlock_and_deallocate(&fs); 31322ba64e8SJohn Dyson VM_WAIT; 31422ba64e8SJohn Dyson goto RetryFault; 31522ba64e8SJohn Dyson } 31622ba64e8SJohn Dyson 3174866e085SJohn Dyson fs.m->flags |= PG_BUSY; 3184866e085SJohn Dyson if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) && 3194866e085SJohn Dyson fs.m->object != kernel_object && fs.m->object != kmem_object) { 3200d94caffSDavid Greenman goto readrest; 3210d94caffSDavid Greenman } 322ffc82b0aSJohn Dyson 323df8bae1dSRodney W. Grimes break; 324df8bae1dSRodney W. Grimes } 3254866e085SJohn Dyson if (((fs.object->type != OBJT_DEFAULT) && 32647221757SJohn Dyson (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired)) 3274866e085SJohn Dyson || (fs.object == fs.first_object)) { 328df8bae1dSRodney W. Grimes 3294866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 3304866e085SJohn Dyson unlock_and_deallocate(&fs); 3315f55e841SDavid Greenman return (KERN_PROTECTION_FAILURE); 3325f55e841SDavid Greenman } 33322ba64e8SJohn Dyson 334df8bae1dSRodney W. Grimes /* 3350d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 336df8bae1dSRodney W. Grimes */ 3374866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 3384866e085SJohn Dyson (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO); 339df8bae1dSRodney W. Grimes 3404866e085SJohn Dyson if (fs.m == NULL) { 3414866e085SJohn Dyson unlock_and_deallocate(&fs); 342df8bae1dSRodney W. Grimes VM_WAIT; 343df8bae1dSRodney W. Grimes goto RetryFault; 344df8bae1dSRodney W. Grimes } 345df8bae1dSRodney W. Grimes } 34647221757SJohn Dyson 3470d94caffSDavid Greenman readrest: 3484866e085SJohn Dyson if (fs.object->type != OBJT_DEFAULT && 34947221757SJohn Dyson (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired)) { 350df8bae1dSRodney W. Grimes int rv; 35126f9a767SRodney W. Grimes int reqpage; 352867a482dSJohn Dyson int ahead, behind; 353867a482dSJohn Dyson 3544866e085SJohn Dyson if (fs.first_object->behavior == OBJ_RANDOM) { 355867a482dSJohn Dyson ahead = 0; 356867a482dSJohn Dyson behind = 0; 3572d8acc0fSJohn Dyson } else { 3584866e085SJohn Dyson behind = (vaddr - fs.entry->start) >> PAGE_SHIFT; 3592d8acc0fSJohn Dyson if (behind > VM_FAULT_READ_BEHIND) 3602d8acc0fSJohn Dyson behind = VM_FAULT_READ_BEHIND; 3612d8acc0fSJohn Dyson 3624866e085SJohn Dyson ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1; 3632d8acc0fSJohn Dyson if (ahead > VM_FAULT_READ_AHEAD) 3642d8acc0fSJohn Dyson ahead = VM_FAULT_READ_AHEAD; 365867a482dSJohn Dyson } 366867a482dSJohn Dyson 3674866e085SJohn Dyson if ((fs.first_object->type != OBJT_DEVICE) && 3684866e085SJohn Dyson (fs.first_object->behavior == OBJ_SEQUENTIAL)) { 369867a482dSJohn Dyson vm_pindex_t firstpindex, tmppindex; 3704866e085SJohn Dyson if (fs.first_pindex < 371867a482dSJohn Dyson 2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1)) 372867a482dSJohn Dyson firstpindex = 0; 373867a482dSJohn Dyson else 3744866e085SJohn Dyson firstpindex = fs.first_pindex - 375867a482dSJohn Dyson 2*(VM_FAULT_READ_BEHIND + VM_FAULT_READ_AHEAD + 1); 376867a482dSJohn Dyson 3774866e085SJohn Dyson for(tmppindex = fs.first_pindex - 1; 3782100d645SPeter Wemm tmppindex >= firstpindex; 379867a482dSJohn Dyson --tmppindex) { 380867a482dSJohn Dyson vm_page_t mt; 3814866e085SJohn Dyson mt = vm_page_lookup( fs.first_object, tmppindex); 382867a482dSJohn Dyson if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL)) 383867a482dSJohn Dyson break; 384ff97964aSJohn Dyson if (mt->busy || 385ff97964aSJohn Dyson (mt->flags & (PG_BUSY | PG_FICTITIOUS)) || 386ff97964aSJohn Dyson mt->hold_count || 387867a482dSJohn Dyson mt->wire_count) 388867a482dSJohn Dyson continue; 389867a482dSJohn Dyson if (mt->dirty == 0) 390867a482dSJohn Dyson vm_page_test_dirty(mt); 391867a482dSJohn Dyson if (mt->dirty) { 392867a482dSJohn Dyson vm_page_protect(mt, VM_PROT_NONE); 393867a482dSJohn Dyson vm_page_deactivate(mt); 394867a482dSJohn Dyson } else { 395867a482dSJohn Dyson vm_page_cache(mt); 396867a482dSJohn Dyson } 397867a482dSJohn Dyson } 398867a482dSJohn Dyson 399867a482dSJohn Dyson ahead += behind; 400867a482dSJohn Dyson behind = 0; 401867a482dSJohn Dyson } 402df8bae1dSRodney W. Grimes 403df8bae1dSRodney W. Grimes /* 4040d94caffSDavid Greenman * now we find out if any other pages should be paged 4050d94caffSDavid Greenman * in at this time this routine checks to see if the 4060d94caffSDavid Greenman * pages surrounding this fault reside in the same 4070d94caffSDavid Greenman * object as the page for this fault. If they do, 4080d94caffSDavid Greenman * then they are faulted in also into the object. The 4090d94caffSDavid Greenman * array "marray" returned contains an array of 4100d94caffSDavid Greenman * vm_page_t structs where one of them is the 4110d94caffSDavid Greenman * vm_page_t passed to the routine. The reqpage 4120d94caffSDavid Greenman * return value is the index into the marray for the 4130d94caffSDavid Greenman * vm_page_t passed to the routine. 41426f9a767SRodney W. Grimes */ 41505f0fdd2SPoul-Henning Kamp faultcount = vm_fault_additional_pages( 4164866e085SJohn Dyson fs.m, behind, ahead, marray, &reqpage); 417df8bae1dSRodney W. Grimes 418df8bae1dSRodney W. Grimes /* 4190d94caffSDavid Greenman * Call the pager to retrieve the data, if any, after 4200d94caffSDavid Greenman * releasing the lock on the map. 421df8bae1dSRodney W. Grimes */ 4224866e085SJohn Dyson unlock_map(&fs); 423df8bae1dSRodney W. Grimes 42426f9a767SRodney W. Grimes rv = faultcount ? 4254866e085SJohn Dyson vm_pager_get_pages(fs.object, marray, faultcount, 42624a1cce3SDavid Greenman reqpage) : VM_PAGER_FAIL; 42722ba64e8SJohn Dyson 42826f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 429df8bae1dSRodney W. Grimes /* 430f230c45cSJohn Dyson * Found the page. Leave it busy while we play 431f230c45cSJohn Dyson * with it. 432f230c45cSJohn Dyson */ 433f230c45cSJohn Dyson 434f230c45cSJohn Dyson /* 4350d94caffSDavid Greenman * Relookup in case pager changed page. Pager 4360d94caffSDavid Greenman * is responsible for disposition of old page 4370d94caffSDavid Greenman * if moved. 438df8bae1dSRodney W. Grimes */ 4394866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 4404866e085SJohn Dyson if(!fs.m) { 4414866e085SJohn Dyson unlock_and_deallocate(&fs); 442f6b04d2bSDavid Greenman goto RetryFault; 443f6b04d2bSDavid Greenman } 444f6b04d2bSDavid Greenman 44526f9a767SRodney W. Grimes hardfault++; 446df8bae1dSRodney W. Grimes break; 447df8bae1dSRodney W. Grimes } 448df8bae1dSRodney W. Grimes /* 4490d94caffSDavid Greenman * Remove the bogus page (which does not exist at this 4500d94caffSDavid Greenman * object/offset); before doing so, we must get back 4510d94caffSDavid Greenman * our object lock to preserve our invariant. 452df8bae1dSRodney W. Grimes * 45324a1cce3SDavid Greenman * Also wake up any other process that may want to bring 4540d94caffSDavid Greenman * in this page. 455df8bae1dSRodney W. Grimes * 4560d94caffSDavid Greenman * If this is the top-level object, we must leave the 45724a1cce3SDavid Greenman * busy page to prevent another process from rushing 4580d94caffSDavid Greenman * past us, and inserting the page in that object at 4590d94caffSDavid Greenman * the same time that we are. 460df8bae1dSRodney W. Grimes */ 46126f9a767SRodney W. Grimes 462a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 463f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 464f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 46526f9a767SRodney W. Grimes /* 466a83c285cSDavid Greenman * Data outside the range of the pager or an I/O error 46726f9a767SRodney W. Grimes */ 468a83c285cSDavid Greenman /* 4690d94caffSDavid Greenman * XXX - the check for kernel_map is a kludge to work 4700d94caffSDavid Greenman * around having the machine panic on a kernel space 4710d94caffSDavid Greenman * fault w/ I/O error. 472a83c285cSDavid Greenman */ 4734866e085SJohn Dyson if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 47447221757SJohn Dyson (rv == VM_PAGER_BAD)) { 4754866e085SJohn Dyson vm_page_free(fs.m); 4764866e085SJohn Dyson fs.m = NULL; 4774866e085SJohn Dyson unlock_and_deallocate(&fs); 478a83c285cSDavid Greenman return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 47926f9a767SRodney W. Grimes } 4804866e085SJohn Dyson if (fs.object != fs.first_object) { 4814866e085SJohn Dyson vm_page_free(fs.m); 4824866e085SJohn Dyson fs.m = NULL; 48326f9a767SRodney W. Grimes /* 48426f9a767SRodney W. Grimes * XXX - we cannot just fall out at this 48526f9a767SRodney W. Grimes * point, m has been freed and is invalid! 48626f9a767SRodney W. Grimes */ 487df8bae1dSRodney W. Grimes } 488df8bae1dSRodney W. Grimes } 489df8bae1dSRodney W. Grimes /* 49024a1cce3SDavid Greenman * We get here if the object has default pager (or unwiring) or the 4910d94caffSDavid Greenman * pager doesn't have the page. 492df8bae1dSRodney W. Grimes */ 4934866e085SJohn Dyson if (fs.object == fs.first_object) 4944866e085SJohn Dyson fs.first_m = fs.m; 495df8bae1dSRodney W. Grimes 496df8bae1dSRodney W. Grimes /* 4970d94caffSDavid Greenman * Move on to the next object. Lock the next object before 4980d94caffSDavid Greenman * unlocking the current one. 499df8bae1dSRodney W. Grimes */ 500df8bae1dSRodney W. Grimes 5014866e085SJohn Dyson fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 5024866e085SJohn Dyson next_object = fs.object->backing_object; 503df8bae1dSRodney W. Grimes if (next_object == NULL) { 504df8bae1dSRodney W. Grimes /* 5050d94caffSDavid Greenman * If there's no object left, fill the page in the top 5060d94caffSDavid Greenman * object with zeros. 507df8bae1dSRodney W. Grimes */ 5084866e085SJohn Dyson if (fs.object != fs.first_object) { 5094866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 510df8bae1dSRodney W. Grimes 5114866e085SJohn Dyson fs.object = fs.first_object; 5124866e085SJohn Dyson fs.pindex = fs.first_pindex; 5134866e085SJohn Dyson fs.m = fs.first_m; 514df8bae1dSRodney W. Grimes } 5154866e085SJohn Dyson fs.first_m = NULL; 516df8bae1dSRodney W. Grimes 5174866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 5184866e085SJohn Dyson vm_page_zero_fill(fs.m); 5194866e085SJohn Dyson cnt.v_ozfod++; 5204866e085SJohn Dyson } 521df8bae1dSRodney W. Grimes cnt.v_zfod++; 522df8bae1dSRodney W. Grimes break; 5230d94caffSDavid Greenman } else { 5244866e085SJohn Dyson if (fs.object != fs.first_object) { 5254866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 526c0503609SDavid Greenman } 5274866e085SJohn Dyson fs.object = next_object; 528d474eaaaSDoug Rabson vm_object_pip_add(fs.object, 1); 529df8bae1dSRodney W. Grimes } 530df8bae1dSRodney W. Grimes } 531df8bae1dSRodney W. Grimes 532925a3a41SJohn Dyson #if defined(DIAGNOSTIC) 5334866e085SJohn Dyson if ((fs.m->flags & PG_BUSY) == 0) 534f919ebdeSDavid Greenman panic("vm_fault: not busy after main loop"); 535925a3a41SJohn Dyson #endif 536df8bae1dSRodney W. Grimes 537df8bae1dSRodney W. Grimes /* 5380d94caffSDavid Greenman * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 539df8bae1dSRodney W. Grimes * is held.] 540df8bae1dSRodney W. Grimes */ 541df8bae1dSRodney W. Grimes 5424866e085SJohn Dyson old_m = fs.m; /* save page that would be copied */ 543df8bae1dSRodney W. Grimes 544df8bae1dSRodney W. Grimes /* 5450d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 5460d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 5470d94caffSDavid Greenman * top-level object. 548df8bae1dSRodney W. Grimes */ 549df8bae1dSRodney W. Grimes 5504866e085SJohn Dyson if (fs.object != fs.first_object) { 551df8bae1dSRodney W. Grimes /* 5520d94caffSDavid Greenman * We only really need to copy if we want to write it. 553df8bae1dSRodney W. Grimes */ 554df8bae1dSRodney W. Grimes 555df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 556df8bae1dSRodney W. Grimes 557df8bae1dSRodney W. Grimes /* 558b5b40fa6SJohn Dyson * This allows pages to be virtually copied from a backing_object 559b5b40fa6SJohn Dyson * into the first_object, where the backing object has no other 560b5b40fa6SJohn Dyson * refs to it, and cannot gain any more refs. Instead of a 561b5b40fa6SJohn Dyson * bcopy, we just move the page from the backing object to the 562b5b40fa6SJohn Dyson * first object. Note that we must mark the page dirty in the 563b5b40fa6SJohn Dyson * first object so that it will go out to swap when needed. 564df8bae1dSRodney W. Grimes */ 5654866e085SJohn Dyson if (map_generation == fs.map->timestamp && 566de5f6a77SJohn Dyson /* 567de5f6a77SJohn Dyson * Only one shadow object 568de5f6a77SJohn Dyson */ 5694866e085SJohn Dyson (fs.object->shadow_count == 1) && 570de5f6a77SJohn Dyson /* 571de5f6a77SJohn Dyson * No COW refs, except us 572de5f6a77SJohn Dyson */ 5734866e085SJohn Dyson (fs.object->ref_count == 1) && 574de5f6a77SJohn Dyson /* 575de5f6a77SJohn Dyson * Noone else can look this object up 576de5f6a77SJohn Dyson */ 5774866e085SJohn Dyson (fs.object->handle == NULL) && 578de5f6a77SJohn Dyson /* 579de5f6a77SJohn Dyson * No other ways to look the object up 580de5f6a77SJohn Dyson */ 5814866e085SJohn Dyson ((fs.object->type == OBJT_DEFAULT) || 5824866e085SJohn Dyson (fs.object->type == OBJT_SWAP)) && 583de5f6a77SJohn Dyson /* 584de5f6a77SJohn Dyson * We don't chase down the shadow chain 585de5f6a77SJohn Dyson */ 5864866e085SJohn Dyson (fs.object == fs.first_object->backing_object) && 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes /* 5892d8acc0fSJohn Dyson * grab the lock if we need to 5902d8acc0fSJohn Dyson */ 5914866e085SJohn Dyson (fs.lookup_still_valid || 5924866e085SJohn Dyson (((fs.entry->eflags & MAP_ENTRY_IS_A_MAP) == 0) && 5934866e085SJohn Dyson lockmgr(&fs.map->lock, 5942d8acc0fSJohn Dyson LK_EXCLUSIVE|LK_NOWAIT, (void *)0, curproc) == 0))) { 5952d8acc0fSJohn Dyson 5964866e085SJohn Dyson fs.lookup_still_valid = 1; 5972d8acc0fSJohn Dyson /* 598de5f6a77SJohn Dyson * get rid of the unnecessary page 599df8bae1dSRodney W. Grimes */ 6004866e085SJohn Dyson vm_page_protect(fs.first_m, VM_PROT_NONE); 6014866e085SJohn Dyson vm_page_free(fs.first_m); 6024866e085SJohn Dyson fs.first_m = NULL; 6034866e085SJohn Dyson 604de5f6a77SJohn Dyson /* 605de5f6a77SJohn Dyson * grab the page and put it into the process'es object 606de5f6a77SJohn Dyson */ 6074866e085SJohn Dyson vm_page_rename(fs.m, fs.first_object, fs.first_pindex); 6084866e085SJohn Dyson fs.first_m = fs.m; 609cbd8ec09SJohn Dyson fs.first_m->dirty = VM_PAGE_BITS_ALL; 610cbd8ec09SJohn Dyson fs.first_m->flags |= PG_BUSY; 6114866e085SJohn Dyson fs.m = NULL; 6124866e085SJohn Dyson cnt.v_cow_optim++; 613de5f6a77SJohn Dyson } else { 614de5f6a77SJohn Dyson /* 615de5f6a77SJohn Dyson * Oh, well, lets copy it. 616de5f6a77SJohn Dyson */ 6174866e085SJohn Dyson vm_page_copy(fs.m, fs.first_m); 618de5f6a77SJohn Dyson } 619df8bae1dSRodney W. Grimes 6204866e085SJohn Dyson if (fs.m) { 621df8bae1dSRodney W. Grimes /* 622df8bae1dSRodney W. Grimes * We no longer need the old page or object. 623df8bae1dSRodney W. Grimes */ 6244866e085SJohn Dyson release_page(&fs); 625de5f6a77SJohn Dyson } 626df8bae1dSRodney W. Grimes 6274866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 628df8bae1dSRodney W. Grimes /* 629df8bae1dSRodney W. Grimes * Only use the new page below... 630df8bae1dSRodney W. Grimes */ 631df8bae1dSRodney W. Grimes 632df8bae1dSRodney W. Grimes cnt.v_cow_faults++; 6334866e085SJohn Dyson fs.m = fs.first_m; 6344866e085SJohn Dyson fs.object = fs.first_object; 6354866e085SJohn Dyson fs.pindex = fs.first_pindex; 636df8bae1dSRodney W. Grimes 6370d94caffSDavid Greenman } else { 638df8bae1dSRodney W. Grimes prot &= ~VM_PROT_WRITE; 639df8bae1dSRodney W. Grimes } 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes 642df8bae1dSRodney W. Grimes /* 6430d94caffSDavid Greenman * We must verify that the maps have not changed since our last 6440d94caffSDavid Greenman * lookup. 645df8bae1dSRodney W. Grimes */ 646df8bae1dSRodney W. Grimes 6474866e085SJohn Dyson if (!fs.lookup_still_valid && 6484866e085SJohn Dyson (fs.map->timestamp != map_generation)) { 649df8bae1dSRodney W. Grimes vm_object_t retry_object; 650a316d390SJohn Dyson vm_pindex_t retry_pindex; 651df8bae1dSRodney W. Grimes vm_prot_t retry_prot; 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes /* 6540d94caffSDavid Greenman * Since map entries may be pageable, make sure we can take a 6550d94caffSDavid Greenman * page fault on them. 656df8bae1dSRodney W. Grimes */ 657df8bae1dSRodney W. Grimes 658df8bae1dSRodney W. Grimes /* 65924a1cce3SDavid Greenman * To avoid trying to write_lock the map while another process 6600d94caffSDavid Greenman * has it read_locked (in vm_map_pageable), we do not try for 6610d94caffSDavid Greenman * write permission. If the page is still writable, we will 6620d94caffSDavid Greenman * get write permission. If it is not, or has been marked 6630d94caffSDavid Greenman * needs_copy, we enter the mapping without write permission, 6640d94caffSDavid Greenman * and will merely take another fault. 665df8bae1dSRodney W. Grimes */ 6664866e085SJohn Dyson result = vm_map_lookup(&fs.map, vaddr, fault_type & ~VM_PROT_WRITE, 6674866e085SJohn Dyson &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 6684866e085SJohn Dyson map_generation = fs.map->timestamp; 669df8bae1dSRodney W. Grimes 670df8bae1dSRodney W. Grimes /* 6710d94caffSDavid Greenman * If we don't need the page any longer, put it on the active 6720d94caffSDavid Greenman * list (the easiest thing to do here). If no one needs it, 6730d94caffSDavid Greenman * pageout will grab it eventually. 674df8bae1dSRodney W. Grimes */ 675df8bae1dSRodney W. Grimes 676df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 6774866e085SJohn Dyson release_page(&fs); 6784866e085SJohn Dyson unlock_and_deallocate(&fs); 679df8bae1dSRodney W. Grimes return (result); 680df8bae1dSRodney W. Grimes } 6814866e085SJohn Dyson fs.lookup_still_valid = TRUE; 682df8bae1dSRodney W. Grimes 6834866e085SJohn Dyson if ((retry_object != fs.first_object) || 6844866e085SJohn Dyson (retry_pindex != fs.first_pindex)) { 6854866e085SJohn Dyson release_page(&fs); 6864866e085SJohn Dyson unlock_and_deallocate(&fs); 687df8bae1dSRodney W. Grimes goto RetryFault; 688df8bae1dSRodney W. Grimes } 689df8bae1dSRodney W. Grimes /* 6900d94caffSDavid Greenman * Check whether the protection has changed or the object has 6910d94caffSDavid Greenman * been copied while we left the map unlocked. Changing from 6920d94caffSDavid Greenman * read to write permission is OK - we leave the page 6930d94caffSDavid Greenman * write-protected, and catch the write fault. Changing from 6940d94caffSDavid Greenman * write to read permission means that we can't mark the page 6950d94caffSDavid Greenman * write-enabled after all. 696df8bae1dSRodney W. Grimes */ 697df8bae1dSRodney W. Grimes prot &= retry_prot; 698df8bae1dSRodney W. Grimes } 699df8bae1dSRodney W. Grimes 700df8bae1dSRodney W. Grimes /* 7010d94caffSDavid Greenman * Put this page into the physical map. We had to do the unlock above 7020d94caffSDavid Greenman * because pmap_enter may cause other faults. We don't put the page 7030d94caffSDavid Greenman * back on the active queue until later so that the page-out daemon 7040d94caffSDavid Greenman * won't find us (yet). 705df8bae1dSRodney W. Grimes */ 706df8bae1dSRodney W. Grimes 7072ddba215SDavid Greenman if (prot & VM_PROT_WRITE) { 7084866e085SJohn Dyson fs.m->flags |= PG_WRITEABLE; 7094866e085SJohn Dyson fs.m->object->flags |= OBJ_WRITEABLE|OBJ_MIGHTBEDIRTY; 7102ddba215SDavid Greenman /* 7112ddba215SDavid Greenman * If the fault is a write, we know that this page is being 7122ddba215SDavid Greenman * written NOW. This will save on the pmap_is_modified() calls 7132ddba215SDavid Greenman * later. 7142ddba215SDavid Greenman */ 715a04c970aSJohn Dyson if (fault_flags & VM_FAULT_DIRTY) { 7164866e085SJohn Dyson fs.m->dirty = VM_PAGE_BITS_ALL; 7172ddba215SDavid Greenman } 7182ddba215SDavid Greenman } 719f6b04d2bSDavid Greenman 7204866e085SJohn Dyson unlock_things(&fs); 7214866e085SJohn Dyson fs.m->valid = VM_PAGE_BITS_ALL; 7224866e085SJohn Dyson fs.m->flags &= ~PG_ZERO; 723f919ebdeSDavid Greenman 7244866e085SJohn Dyson pmap_enter(fs.map->pmap, vaddr, VM_PAGE_TO_PHYS(fs.m), prot, wired); 7252d8acc0fSJohn Dyson if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) { 7264866e085SJohn Dyson pmap_prefault(fs.map->pmap, vaddr, fs.entry); 7272d8acc0fSJohn Dyson } 728df8bae1dSRodney W. Grimes 7294866e085SJohn Dyson fs.m->flags |= PG_MAPPED|PG_REFERENCED; 730a04c970aSJohn Dyson if (fault_flags & VM_FAULT_HOLD) 7314866e085SJohn Dyson vm_page_hold(fs.m); 732ff97964aSJohn Dyson 733df8bae1dSRodney W. Grimes /* 7340d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 7350d94caffSDavid Greenman * can find it. 736df8bae1dSRodney W. Grimes */ 737a04c970aSJohn Dyson if (fault_flags & VM_FAULT_WIRE_MASK) { 738df8bae1dSRodney W. Grimes if (wired) 7394866e085SJohn Dyson vm_page_wire(fs.m); 740df8bae1dSRodney W. Grimes else 7414866e085SJohn Dyson vm_page_unwire(fs.m); 7420d94caffSDavid Greenman } else { 7434866e085SJohn Dyson vm_page_activate(fs.m); 74426f9a767SRodney W. Grimes } 74526f9a767SRodney W. Grimes 746a1f6d91cSDavid Greenman if (curproc && (curproc->p_flag & P_INMEM) && curproc->p_stats) { 74726f9a767SRodney W. Grimes if (hardfault) { 74826f9a767SRodney W. Grimes curproc->p_stats->p_ru.ru_majflt++; 74926f9a767SRodney W. Grimes } else { 75026f9a767SRodney W. Grimes curproc->p_stats->p_ru.ru_minflt++; 75126f9a767SRodney W. Grimes } 75226f9a767SRodney W. Grimes } 753df8bae1dSRodney W. Grimes 754df8bae1dSRodney W. Grimes /* 755df8bae1dSRodney W. Grimes * Unlock everything, and return 756df8bae1dSRodney W. Grimes */ 757df8bae1dSRodney W. Grimes 7584866e085SJohn Dyson PAGE_WAKEUP(fs.m); 7594866e085SJohn Dyson vm_object_deallocate(fs.first_object); 760df8bae1dSRodney W. Grimes 761df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes } 764df8bae1dSRodney W. Grimes 765df8bae1dSRodney W. Grimes /* 766df8bae1dSRodney W. Grimes * vm_fault_wire: 767df8bae1dSRodney W. Grimes * 768df8bae1dSRodney W. Grimes * Wire down a range of virtual addresses in a map. 769df8bae1dSRodney W. Grimes */ 770df8bae1dSRodney W. Grimes int 771df8bae1dSRodney W. Grimes vm_fault_wire(map, start, end) 772df8bae1dSRodney W. Grimes vm_map_t map; 773df8bae1dSRodney W. Grimes vm_offset_t start, end; 774df8bae1dSRodney W. Grimes { 77526f9a767SRodney W. Grimes 776df8bae1dSRodney W. Grimes register vm_offset_t va; 777df8bae1dSRodney W. Grimes register pmap_t pmap; 778df8bae1dSRodney W. Grimes int rv; 779df8bae1dSRodney W. Grimes 780df8bae1dSRodney W. Grimes pmap = vm_map_pmap(map); 781df8bae1dSRodney W. Grimes 782df8bae1dSRodney W. Grimes /* 7830d94caffSDavid Greenman * Inform the physical mapping system that the range of addresses may 7840d94caffSDavid Greenman * not fault, so that page tables and such can be locked down as well. 785df8bae1dSRodney W. Grimes */ 786df8bae1dSRodney W. Grimes 787df8bae1dSRodney W. Grimes pmap_pageable(pmap, start, end, FALSE); 788df8bae1dSRodney W. Grimes 789df8bae1dSRodney W. Grimes /* 7900d94caffSDavid Greenman * We simulate a fault to get the page and enter it in the physical 7910d94caffSDavid Greenman * map. 792df8bae1dSRodney W. Grimes */ 793df8bae1dSRodney W. Grimes 794df8bae1dSRodney W. Grimes for (va = start; va < end; va += PAGE_SIZE) { 7957aaaa4fdSJohn Dyson rv = vm_fault(map, va, VM_PROT_READ|VM_PROT_WRITE, 7967aaaa4fdSJohn Dyson VM_FAULT_CHANGE_WIRING); 7977aaaa4fdSJohn Dyson if (rv) { 7987aaaa4fdSJohn Dyson if (va != start) 7997aaaa4fdSJohn Dyson vm_fault_unwire(map, start, va); 8007aaaa4fdSJohn Dyson return (rv); 8017aaaa4fdSJohn Dyson } 8027aaaa4fdSJohn Dyson } 8037aaaa4fdSJohn Dyson return (KERN_SUCCESS); 8047aaaa4fdSJohn Dyson } 8057aaaa4fdSJohn Dyson 8067aaaa4fdSJohn Dyson /* 8077aaaa4fdSJohn Dyson * vm_fault_user_wire: 8087aaaa4fdSJohn Dyson * 8097aaaa4fdSJohn Dyson * Wire down a range of virtual addresses in a map. This 8107aaaa4fdSJohn Dyson * is for user mode though, so we only ask for read access 8117aaaa4fdSJohn Dyson * on currently read only sections. 8127aaaa4fdSJohn Dyson */ 8137aaaa4fdSJohn Dyson int 8147aaaa4fdSJohn Dyson vm_fault_user_wire(map, start, end) 8157aaaa4fdSJohn Dyson vm_map_t map; 8167aaaa4fdSJohn Dyson vm_offset_t start, end; 8177aaaa4fdSJohn Dyson { 8187aaaa4fdSJohn Dyson 8197aaaa4fdSJohn Dyson register vm_offset_t va; 8207aaaa4fdSJohn Dyson register pmap_t pmap; 8217aaaa4fdSJohn Dyson int rv; 8227aaaa4fdSJohn Dyson 8237aaaa4fdSJohn Dyson pmap = vm_map_pmap(map); 8247aaaa4fdSJohn Dyson 8257aaaa4fdSJohn Dyson /* 8267aaaa4fdSJohn Dyson * Inform the physical mapping system that the range of addresses may 8277aaaa4fdSJohn Dyson * not fault, so that page tables and such can be locked down as well. 8287aaaa4fdSJohn Dyson */ 8292d8acc0fSJohn Dyson 8307aaaa4fdSJohn Dyson pmap_pageable(pmap, start, end, FALSE); 8317aaaa4fdSJohn Dyson 8327aaaa4fdSJohn Dyson /* 8337aaaa4fdSJohn Dyson * We simulate a fault to get the page and enter it in the physical 8347aaaa4fdSJohn Dyson * map. 8357aaaa4fdSJohn Dyson */ 8367aaaa4fdSJohn Dyson for (va = start; va < end; va += PAGE_SIZE) { 8377aaaa4fdSJohn Dyson rv = vm_fault(map, va, VM_PROT_READ, VM_FAULT_USER_WIRE); 838df8bae1dSRodney W. Grimes if (rv) { 839df8bae1dSRodney W. Grimes if (va != start) 840df8bae1dSRodney W. Grimes vm_fault_unwire(map, start, va); 841df8bae1dSRodney W. Grimes return (rv); 842df8bae1dSRodney W. Grimes } 843df8bae1dSRodney W. Grimes } 844df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 845df8bae1dSRodney W. Grimes } 846df8bae1dSRodney W. Grimes 847df8bae1dSRodney W. Grimes 848df8bae1dSRodney W. Grimes /* 849df8bae1dSRodney W. Grimes * vm_fault_unwire: 850df8bae1dSRodney W. Grimes * 851df8bae1dSRodney W. Grimes * Unwire a range of virtual addresses in a map. 852df8bae1dSRodney W. Grimes */ 85326f9a767SRodney W. Grimes void 85426f9a767SRodney W. Grimes vm_fault_unwire(map, start, end) 855df8bae1dSRodney W. Grimes vm_map_t map; 856df8bae1dSRodney W. Grimes vm_offset_t start, end; 857df8bae1dSRodney W. Grimes { 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes register vm_offset_t va, pa; 860df8bae1dSRodney W. Grimes register pmap_t pmap; 861df8bae1dSRodney W. Grimes 862df8bae1dSRodney W. Grimes pmap = vm_map_pmap(map); 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes /* 8650d94caffSDavid Greenman * Since the pages are wired down, we must be able to get their 8660d94caffSDavid Greenman * mappings from the physical map system. 867df8bae1dSRodney W. Grimes */ 868df8bae1dSRodney W. Grimes 869df8bae1dSRodney W. Grimes for (va = start; va < end; va += PAGE_SIZE) { 870df8bae1dSRodney W. Grimes pa = pmap_extract(pmap, va); 871b18bfc3dSJohn Dyson if (pa != (vm_offset_t) 0) { 872df8bae1dSRodney W. Grimes pmap_change_wiring(pmap, va, FALSE); 873df8bae1dSRodney W. Grimes vm_page_unwire(PHYS_TO_VM_PAGE(pa)); 874df8bae1dSRodney W. Grimes } 875b18bfc3dSJohn Dyson } 876df8bae1dSRodney W. Grimes 877df8bae1dSRodney W. Grimes /* 8780d94caffSDavid Greenman * Inform the physical mapping system that the range of addresses may 8790d94caffSDavid Greenman * fault, so that page tables and such may be unwired themselves. 880df8bae1dSRodney W. Grimes */ 881df8bae1dSRodney W. Grimes 882df8bae1dSRodney W. Grimes pmap_pageable(pmap, start, end, TRUE); 883df8bae1dSRodney W. Grimes 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes 886df8bae1dSRodney W. Grimes /* 887df8bae1dSRodney W. Grimes * Routine: 888df8bae1dSRodney W. Grimes * vm_fault_copy_entry 889df8bae1dSRodney W. Grimes * Function: 890df8bae1dSRodney W. Grimes * Copy all of the pages from a wired-down map entry to another. 891df8bae1dSRodney W. Grimes * 892df8bae1dSRodney W. Grimes * In/out conditions: 893df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 894df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 895df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 896df8bae1dSRodney W. Grimes */ 897df8bae1dSRodney W. Grimes 89826f9a767SRodney W. Grimes void 89926f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry) 900df8bae1dSRodney W. Grimes vm_map_t dst_map; 901df8bae1dSRodney W. Grimes vm_map_t src_map; 902df8bae1dSRodney W. Grimes vm_map_entry_t dst_entry; 903df8bae1dSRodney W. Grimes vm_map_entry_t src_entry; 904df8bae1dSRodney W. Grimes { 905df8bae1dSRodney W. Grimes vm_object_t dst_object; 906df8bae1dSRodney W. Grimes vm_object_t src_object; 907a316d390SJohn Dyson vm_ooffset_t dst_offset; 908a316d390SJohn Dyson vm_ooffset_t src_offset; 909df8bae1dSRodney W. Grimes vm_prot_t prot; 910df8bae1dSRodney W. Grimes vm_offset_t vaddr; 911df8bae1dSRodney W. Grimes vm_page_t dst_m; 912df8bae1dSRodney W. Grimes vm_page_t src_m; 913df8bae1dSRodney W. Grimes 914df8bae1dSRodney W. Grimes #ifdef lint 915df8bae1dSRodney W. Grimes src_map++; 9160d94caffSDavid Greenman #endif /* lint */ 917df8bae1dSRodney W. Grimes 918df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 919df8bae1dSRodney W. Grimes src_offset = src_entry->offset; 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes /* 9220d94caffSDavid Greenman * Create the top-level object for the destination entry. (Doesn't 9230d94caffSDavid Greenman * actually shadow anything - we copy the pages directly.) 924df8bae1dSRodney W. Grimes */ 92524a1cce3SDavid Greenman dst_object = vm_object_allocate(OBJT_DEFAULT, 926a316d390SJohn Dyson (vm_size_t) OFF_TO_IDX(dst_entry->end - dst_entry->start)); 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 929df8bae1dSRodney W. Grimes dst_entry->offset = 0; 930df8bae1dSRodney W. Grimes 931df8bae1dSRodney W. Grimes prot = dst_entry->max_protection; 932df8bae1dSRodney W. Grimes 933df8bae1dSRodney W. Grimes /* 9340d94caffSDavid Greenman * Loop through all of the pages in the entry's range, copying each 9350d94caffSDavid Greenman * one from the source object (it should be there) to the destination 9360d94caffSDavid Greenman * object. 937df8bae1dSRodney W. Grimes */ 938df8bae1dSRodney W. Grimes for (vaddr = dst_entry->start, dst_offset = 0; 939df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 940df8bae1dSRodney W. Grimes vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 941df8bae1dSRodney W. Grimes 942df8bae1dSRodney W. Grimes /* 943df8bae1dSRodney W. Grimes * Allocate a page in the destination object 944df8bae1dSRodney W. Grimes */ 945df8bae1dSRodney W. Grimes do { 946a316d390SJohn Dyson dst_m = vm_page_alloc(dst_object, 947a316d390SJohn Dyson OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL); 948df8bae1dSRodney W. Grimes if (dst_m == NULL) { 949df8bae1dSRodney W. Grimes VM_WAIT; 950df8bae1dSRodney W. Grimes } 951df8bae1dSRodney W. Grimes } while (dst_m == NULL); 952df8bae1dSRodney W. Grimes 953df8bae1dSRodney W. Grimes /* 954df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 9550d94caffSDavid Greenman * (Because the source is wired down, the page will be in 9560d94caffSDavid Greenman * memory.) 957df8bae1dSRodney W. Grimes */ 958a316d390SJohn Dyson src_m = vm_page_lookup(src_object, 959a316d390SJohn Dyson OFF_TO_IDX(dst_offset + src_offset)); 960df8bae1dSRodney W. Grimes if (src_m == NULL) 961df8bae1dSRodney W. Grimes panic("vm_fault_copy_wired: page missing"); 962df8bae1dSRodney W. Grimes 963df8bae1dSRodney W. Grimes vm_page_copy(src_m, dst_m); 964df8bae1dSRodney W. Grimes 965df8bae1dSRodney W. Grimes /* 966df8bae1dSRodney W. Grimes * Enter it in the pmap... 967df8bae1dSRodney W. Grimes */ 968df8bae1dSRodney W. Grimes 969ccbb2f72SJohn Dyson dst_m->flags &= ~PG_ZERO; 970df8bae1dSRodney W. Grimes pmap_enter(dst_map->pmap, vaddr, VM_PAGE_TO_PHYS(dst_m), 971df8bae1dSRodney W. Grimes prot, FALSE); 972a6e6bcc5SJohn Dyson dst_m->flags |= PG_WRITEABLE|PG_MAPPED; 973df8bae1dSRodney W. Grimes 974df8bae1dSRodney W. Grimes /* 975df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 976df8bae1dSRodney W. Grimes */ 977df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 978df8bae1dSRodney W. Grimes PAGE_WAKEUP(dst_m); 979df8bae1dSRodney W. Grimes } 980df8bae1dSRodney W. Grimes } 98126f9a767SRodney W. Grimes 98226f9a767SRodney W. Grimes 98326f9a767SRodney W. Grimes /* 98426f9a767SRodney W. Grimes * This routine checks around the requested page for other pages that 98522ba64e8SJohn Dyson * might be able to be faulted in. This routine brackets the viable 98622ba64e8SJohn Dyson * pages for the pages to be paged in. 98726f9a767SRodney W. Grimes * 98826f9a767SRodney W. Grimes * Inputs: 98922ba64e8SJohn Dyson * m, rbehind, rahead 99026f9a767SRodney W. Grimes * 99126f9a767SRodney W. Grimes * Outputs: 99226f9a767SRodney W. Grimes * marray (array of vm_page_t), reqpage (index of requested page) 99326f9a767SRodney W. Grimes * 99426f9a767SRodney W. Grimes * Return value: 99526f9a767SRodney W. Grimes * number of pages in marray 99626f9a767SRodney W. Grimes */ 997303b270bSEivind Eklund static int 99822ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 99926f9a767SRodney W. Grimes vm_page_t m; 100026f9a767SRodney W. Grimes int rbehind; 100122ba64e8SJohn Dyson int rahead; 100226f9a767SRodney W. Grimes vm_page_t *marray; 100326f9a767SRodney W. Grimes int *reqpage; 100426f9a767SRodney W. Grimes { 10052d8acc0fSJohn Dyson int i,j; 100626f9a767SRodney W. Grimes vm_object_t object; 1007a316d390SJohn Dyson vm_pindex_t pindex, startpindex, endpindex, tpindex; 100826f9a767SRodney W. Grimes vm_page_t rtm; 1009170db9c6SJohn Dyson int cbehind, cahead; 101026f9a767SRodney W. Grimes 101126f9a767SRodney W. Grimes object = m->object; 1012a316d390SJohn Dyson pindex = m->pindex; 101326f9a767SRodney W. Grimes 101426f9a767SRodney W. Grimes /* 1015f35329acSJohn Dyson * we don't fault-ahead for device pager 1016f35329acSJohn Dyson */ 1017f35329acSJohn Dyson if (object->type == OBJT_DEVICE) { 1018f35329acSJohn Dyson *reqpage = 0; 1019f35329acSJohn Dyson marray[0] = m; 1020f35329acSJohn Dyson return 1; 1021f35329acSJohn Dyson } 1022f35329acSJohn Dyson 1023f35329acSJohn Dyson /* 102426f9a767SRodney W. Grimes * if the requested page is not available, then give up now 102526f9a767SRodney W. Grimes */ 102626f9a767SRodney W. Grimes 1027170db9c6SJohn Dyson if (!vm_pager_has_page(object, 10282d8acc0fSJohn Dyson OFF_TO_IDX(object->paging_offset) + pindex, &cbehind, &cahead)) { 102926f9a767SRodney W. Grimes return 0; 10302d8acc0fSJohn Dyson } 103126f9a767SRodney W. Grimes 103222ba64e8SJohn Dyson if ((cbehind == 0) && (cahead == 0)) { 103322ba64e8SJohn Dyson *reqpage = 0; 103422ba64e8SJohn Dyson marray[0] = m; 103522ba64e8SJohn Dyson return 1; 1036170db9c6SJohn Dyson } 103722ba64e8SJohn Dyson 103822ba64e8SJohn Dyson if (rahead > cahead) { 103922ba64e8SJohn Dyson rahead = cahead; 104022ba64e8SJohn Dyson } 104122ba64e8SJohn Dyson 1042170db9c6SJohn Dyson if (rbehind > cbehind) { 1043170db9c6SJohn Dyson rbehind = cbehind; 1044170db9c6SJohn Dyson } 1045170db9c6SJohn Dyson 104626f9a767SRodney W. Grimes /* 104726f9a767SRodney W. Grimes * try to do any readahead that we might have free pages for. 104826f9a767SRodney W. Grimes */ 1049ccbb2f72SJohn Dyson if ((rahead + rbehind) > 105022ba64e8SJohn Dyson ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) { 1051f919ebdeSDavid Greenman pagedaemon_wakeup(); 105226f9a767SRodney W. Grimes marray[0] = m; 10532d8acc0fSJohn Dyson *reqpage = 0; 105426f9a767SRodney W. Grimes return 1; 105526f9a767SRodney W. Grimes } 105622ba64e8SJohn Dyson 105726f9a767SRodney W. Grimes /* 10582d8acc0fSJohn Dyson * scan backward for the read behind pages -- in memory 105926f9a767SRodney W. Grimes */ 10602d8acc0fSJohn Dyson if (pindex > 0) { 10612d8acc0fSJohn Dyson if (rbehind > pindex) { 1062a316d390SJohn Dyson rbehind = pindex; 10632d8acc0fSJohn Dyson startpindex = 0; 10642d8acc0fSJohn Dyson } else { 1065a316d390SJohn Dyson startpindex = pindex - rbehind; 10662d8acc0fSJohn Dyson } 10672d8acc0fSJohn Dyson 10682d8acc0fSJohn Dyson for ( tpindex = pindex - 1; tpindex >= startpindex; tpindex -= 1) { 1069a316d390SJohn Dyson if (vm_page_lookup( object, tpindex)) { 1070a316d390SJohn Dyson startpindex = tpindex + 1; 107126f9a767SRodney W. Grimes break; 107226f9a767SRodney W. Grimes } 1073a316d390SJohn Dyson if (tpindex == 0) 107426f9a767SRodney W. Grimes break; 1075317205caSDavid Greenman } 107626f9a767SRodney W. Grimes 10772d8acc0fSJohn Dyson for(i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) { 107826f9a767SRodney W. Grimes 10792d8acc0fSJohn Dyson rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1080ccbb2f72SJohn Dyson if (rtm == NULL) { 1081ccbb2f72SJohn Dyson for (j = 0; j < i; j++) { 10824866e085SJohn Dyson vm_page_free(marray[j]); 108326f9a767SRodney W. Grimes } 108426f9a767SRodney W. Grimes marray[0] = m; 10852d8acc0fSJohn Dyson *reqpage = 0; 108626f9a767SRodney W. Grimes return 1; 108726f9a767SRodney W. Grimes } 1088170db9c6SJohn Dyson 10892d8acc0fSJohn Dyson marray[i] = rtm; 109026f9a767SRodney W. Grimes } 10912d8acc0fSJohn Dyson } else { 10922d8acc0fSJohn Dyson startpindex = 0; 10932d8acc0fSJohn Dyson i = 0; 10942d8acc0fSJohn Dyson } 10952d8acc0fSJohn Dyson 10962d8acc0fSJohn Dyson marray[i] = m; 10972d8acc0fSJohn Dyson /* page offset of the required page */ 10982d8acc0fSJohn Dyson *reqpage = i; 10992d8acc0fSJohn Dyson 11002d8acc0fSJohn Dyson tpindex = pindex + 1; 11012d8acc0fSJohn Dyson i++; 11022d8acc0fSJohn Dyson 11032d8acc0fSJohn Dyson /* 11042d8acc0fSJohn Dyson * scan forward for the read ahead pages 11052d8acc0fSJohn Dyson */ 11062d8acc0fSJohn Dyson endpindex = tpindex + rahead; 11072d8acc0fSJohn Dyson if (endpindex > object->size) 11082d8acc0fSJohn Dyson endpindex = object->size; 11092d8acc0fSJohn Dyson 11102d8acc0fSJohn Dyson for( ; tpindex < endpindex; i++, tpindex++) { 11112d8acc0fSJohn Dyson 11122d8acc0fSJohn Dyson if (vm_page_lookup(object, tpindex)) { 11132d8acc0fSJohn Dyson break; 11142d8acc0fSJohn Dyson } 11152d8acc0fSJohn Dyson 11162d8acc0fSJohn Dyson rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 11172d8acc0fSJohn Dyson if (rtm == NULL) { 11182d8acc0fSJohn Dyson break; 11192d8acc0fSJohn Dyson } 11202d8acc0fSJohn Dyson 11212d8acc0fSJohn Dyson marray[i] = rtm; 11222d8acc0fSJohn Dyson } 11232d8acc0fSJohn Dyson 11242d8acc0fSJohn Dyson /* return number of bytes of pages */ 11252d8acc0fSJohn Dyson return i; 112626f9a767SRodney W. Grimes } 1127