160727d8bSWarner Losh /*- 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 225929bcfaSPhilippe Charnier * 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. 68df8bae1dSRodney W. Grimes */ 69df8bae1dSRodney W. Grimes 70df8bae1dSRodney W. Grimes /* 71df8bae1dSRodney W. Grimes * Page fault handling module. 72df8bae1dSRodney W. Grimes */ 73874651b1SDavid E. O'Brien 74874651b1SDavid E. O'Brien #include <sys/cdefs.h> 75874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 76874651b1SDavid E. O'Brien 77df8bae1dSRodney W. Grimes #include <sys/param.h> 78df8bae1dSRodney W. Grimes #include <sys/systm.h> 794edf4a58SJohn Baldwin #include <sys/kernel.h> 80fb919e4dSMark Murray #include <sys/lock.h> 81fb919e4dSMark Murray #include <sys/mutex.h> 8226f9a767SRodney W. Grimes #include <sys/proc.h> 8326f9a767SRodney W. Grimes #include <sys/resourcevar.h> 8423955314SAlfred Perlstein #include <sys/sysctl.h> 854edf4a58SJohn Baldwin #include <sys/vmmeter.h> 864edf4a58SJohn Baldwin #include <sys/vnode.h> 87df8bae1dSRodney W. Grimes 88df8bae1dSRodney W. Grimes #include <vm/vm.h> 89efeaf95aSDavid Greenman #include <vm/vm_param.h> 90efeaf95aSDavid Greenman #include <vm/pmap.h> 91efeaf95aSDavid Greenman #include <vm/vm_map.h> 92efeaf95aSDavid Greenman #include <vm/vm_object.h> 93df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 94df8bae1dSRodney W. Grimes #include <vm/vm_pageout.h> 95a83c285cSDavid Greenman #include <vm/vm_kern.h> 9624a1cce3SDavid Greenman #include <vm/vm_pager.h> 9724a1cce3SDavid Greenman #include <vm/vnode_pager.h> 98efeaf95aSDavid Greenman #include <vm/vm_extern.h> 99df8bae1dSRodney W. Grimes 100ae51ff11SJeff Roberson #include <sys/mount.h> /* XXX Temporary for VFS_LOCK_GIANT() */ 101ae51ff11SJeff Roberson 102566526a9SAlan Cox #define PFBAK 4 103566526a9SAlan Cox #define PFFOR 4 104566526a9SAlan Cox #define PAGEORDER_SIZE (PFBAK+PFFOR) 105566526a9SAlan Cox 106566526a9SAlan Cox static int prefault_pageorder[] = { 107566526a9SAlan Cox -1 * PAGE_SIZE, 1 * PAGE_SIZE, 108566526a9SAlan Cox -2 * PAGE_SIZE, 2 * PAGE_SIZE, 109566526a9SAlan Cox -3 * PAGE_SIZE, 3 * PAGE_SIZE, 110566526a9SAlan Cox -4 * PAGE_SIZE, 4 * PAGE_SIZE 111566526a9SAlan Cox }; 112566526a9SAlan Cox 11311caded3SAlfred Perlstein static int vm_fault_additional_pages(vm_page_t, int, int, vm_page_t *, int *); 114566526a9SAlan Cox static void vm_fault_prefault(pmap_t, vm_offset_t, vm_map_entry_t); 11526f9a767SRodney W. Grimes 11647221757SJohn Dyson #define VM_FAULT_READ_AHEAD 8 11747221757SJohn Dyson #define VM_FAULT_READ_BEHIND 7 11826f9a767SRodney W. Grimes #define VM_FAULT_READ (VM_FAULT_READ_AHEAD+VM_FAULT_READ_BEHIND+1) 11926f9a767SRodney W. Grimes 1204866e085SJohn Dyson struct faultstate { 1214866e085SJohn Dyson vm_page_t m; 1224866e085SJohn Dyson vm_object_t object; 1234866e085SJohn Dyson vm_pindex_t pindex; 1244866e085SJohn Dyson vm_page_t first_m; 1254866e085SJohn Dyson vm_object_t first_object; 1264866e085SJohn Dyson vm_pindex_t first_pindex; 1274866e085SJohn Dyson vm_map_t map; 1284866e085SJohn Dyson vm_map_entry_t entry; 12925adb370SBrian Feldman int lookup_still_valid; 1304866e085SJohn Dyson struct vnode *vp; 1314866e085SJohn Dyson }; 1324866e085SJohn Dyson 13362a59e8fSWarner Losh static inline void 1344866e085SJohn Dyson release_page(struct faultstate *fs) 1354866e085SJohn Dyson { 1362ad98273SAlan Cox vm_page_lock_queues(); 137e69763a3SDoug Rabson vm_page_wakeup(fs->m); 1384866e085SJohn Dyson vm_page_deactivate(fs->m); 1392ad98273SAlan Cox vm_page_unlock_queues(); 1404866e085SJohn Dyson fs->m = NULL; 1414866e085SJohn Dyson } 1424866e085SJohn Dyson 14362a59e8fSWarner Losh static inline void 1444866e085SJohn Dyson unlock_map(struct faultstate *fs) 1454866e085SJohn Dyson { 14625adb370SBrian Feldman if (fs->lookup_still_valid) { 1474866e085SJohn Dyson vm_map_lookup_done(fs->map, fs->entry); 14825adb370SBrian Feldman fs->lookup_still_valid = FALSE; 1494866e085SJohn Dyson } 1504866e085SJohn Dyson } 1514866e085SJohn Dyson 1524866e085SJohn Dyson static void 153a51b0840SAlan Cox unlock_and_deallocate(struct faultstate *fs) 1544866e085SJohn Dyson { 155b898bb1bSTor Egge boolean_t firstobjneedgiant; 156f29ba63eSAlan Cox 1574866e085SJohn Dyson vm_object_pip_wakeup(fs->object); 158b009d5a0SAlan Cox VM_OBJECT_UNLOCK(fs->object); 1594866e085SJohn Dyson if (fs->object != fs->first_object) { 160b009d5a0SAlan Cox VM_OBJECT_LOCK(fs->first_object); 1612ad98273SAlan Cox vm_page_lock_queues(); 1624866e085SJohn Dyson vm_page_free(fs->first_m); 1632ad98273SAlan Cox vm_page_unlock_queues(); 1644866e085SJohn Dyson vm_object_pip_wakeup(fs->first_object); 165b009d5a0SAlan Cox VM_OBJECT_UNLOCK(fs->first_object); 1664866e085SJohn Dyson fs->first_m = NULL; 1674866e085SJohn Dyson } 168b898bb1bSTor Egge firstobjneedgiant = (fs->first_object->flags & OBJ_NEEDGIANT) != 0; 1694866e085SJohn Dyson vm_object_deallocate(fs->first_object); 1704866e085SJohn Dyson unlock_map(fs); 1714866e085SJohn Dyson if (fs->vp != NULL) { 172ae51ff11SJeff Roberson int vfslocked; 173ae51ff11SJeff Roberson 174ae51ff11SJeff Roberson vfslocked = VFS_LOCK_GIANT(fs->vp->v_mount); 1750cddd8f0SMatthew Dillon vput(fs->vp); 1764866e085SJohn Dyson fs->vp = NULL; 177ae51ff11SJeff Roberson VFS_UNLOCK_GIANT(vfslocked); 1784866e085SJohn Dyson } 179b898bb1bSTor Egge if (firstobjneedgiant) 180c1fbc251SAlan Cox VM_UNLOCK_GIANT(); 1814866e085SJohn Dyson } 1824866e085SJohn Dyson 183df8bae1dSRodney W. Grimes /* 18440360b1bSMatthew Dillon * TRYPAGER - used by vm_fault to calculate whether the pager for the 18540360b1bSMatthew Dillon * current object *might* contain the page. 18640360b1bSMatthew Dillon * 18740360b1bSMatthew Dillon * default objects are zero-fill, there is no real pager. 18840360b1bSMatthew Dillon */ 18940360b1bSMatthew Dillon #define TRYPAGER (fs.object->type != OBJT_DEFAULT && \ 19040360b1bSMatthew Dillon (((fault_flags & VM_FAULT_WIRE_MASK) == 0) || wired)) 19140360b1bSMatthew Dillon 19240360b1bSMatthew Dillon /* 193df8bae1dSRodney W. Grimes * vm_fault: 194df8bae1dSRodney W. Grimes * 195956f3135SPhilippe Charnier * Handle a page fault occurring at the given address, 196df8bae1dSRodney W. Grimes * requiring the given permissions, in the map specified. 197df8bae1dSRodney W. Grimes * If successful, the page is inserted into the 198df8bae1dSRodney W. Grimes * associated physical map. 199df8bae1dSRodney W. Grimes * 200df8bae1dSRodney W. Grimes * NOTE: the given address should be truncated to the 201df8bae1dSRodney W. Grimes * proper page address. 202df8bae1dSRodney W. Grimes * 203df8bae1dSRodney W. Grimes * KERN_SUCCESS is returned if the page fault is handled; otherwise, 204df8bae1dSRodney W. Grimes * a standard error specifying why the fault is fatal is returned. 205df8bae1dSRodney W. Grimes * 206df8bae1dSRodney W. Grimes * 207df8bae1dSRodney W. Grimes * The map in question must be referenced, and remains so. 2080cddd8f0SMatthew Dillon * Caller may hold no locks. 209df8bae1dSRodney W. Grimes */ 210df8bae1dSRodney W. Grimes int 21123955314SAlfred Perlstein vm_fault(vm_map_t map, vm_offset_t vaddr, vm_prot_t fault_type, 21223955314SAlfred Perlstein int fault_flags) 21323955314SAlfred Perlstein { 214df8bae1dSRodney W. Grimes vm_prot_t prot; 215ebf75125SAlan Cox int is_first_object_locked, result; 2166139043bSAlan Cox boolean_t growstack, wired; 2172d8acc0fSJohn Dyson int map_generation; 218df8bae1dSRodney W. Grimes vm_object_t next_object; 21926f9a767SRodney W. Grimes vm_page_t marray[VM_FAULT_READ]; 2204866e085SJohn Dyson int hardfault; 2212d8acc0fSJohn Dyson int faultcount; 2224866e085SJohn Dyson struct faultstate fs; 223df8bae1dSRodney W. Grimes 2244866e085SJohn Dyson hardfault = 0; 2256139043bSAlan Cox growstack = TRUE; 22692de35b0SAlan Cox atomic_add_int(&cnt.v_vm_faults, 1); 227df8bae1dSRodney W. Grimes 228df8bae1dSRodney W. Grimes RetryFault:; 229df8bae1dSRodney W. Grimes 230df8bae1dSRodney W. Grimes /* 2310d94caffSDavid Greenman * Find the backing store object and offset into it to begin the 2320d94caffSDavid Greenman * search. 233df8bae1dSRodney W. Grimes */ 23440360b1bSMatthew Dillon fs.map = map; 23592de35b0SAlan Cox result = vm_map_lookup(&fs.map, vaddr, fault_type, &fs.entry, 23692de35b0SAlan Cox &fs.first_object, &fs.first_pindex, &prot, &wired); 23792de35b0SAlan Cox if (result != KERN_SUCCESS) { 23892de35b0SAlan Cox if (result != KERN_PROTECTION_FAILURE || 23992de35b0SAlan Cox (fault_flags & VM_FAULT_WIRE_MASK) != VM_FAULT_USER_WIRE) { 2406139043bSAlan Cox if (growstack && result == KERN_INVALID_ADDRESS && 2416139043bSAlan Cox map != kernel_map && curproc != NULL) { 2426139043bSAlan Cox result = vm_map_growstack(curproc, vaddr); 243a976eb5eSAlan Cox if (result != KERN_SUCCESS) 2446139043bSAlan Cox return (KERN_FAILURE); 2456139043bSAlan Cox growstack = FALSE; 2466139043bSAlan Cox goto RetryFault; 2476139043bSAlan Cox } 24892de35b0SAlan Cox return (result); 24909e0c6ccSJohn Dyson } 25009e0c6ccSJohn Dyson 2517aaaa4fdSJohn Dyson /* 2527aaaa4fdSJohn Dyson * If we are user-wiring a r/w segment, and it is COW, then 2537aaaa4fdSJohn Dyson * we need to do the COW operation. Note that we don't COW 254595236dfSJohn Dyson * currently RO sections now, because it is NOT desirable 2557aaaa4fdSJohn Dyson * to COW .text. We simply keep .text from ever being COW'ed 2567aaaa4fdSJohn Dyson * and take the heat that one cannot debug wired .text sections. 2577aaaa4fdSJohn Dyson */ 2584866e085SJohn Dyson result = vm_map_lookup(&fs.map, vaddr, 25947221757SJohn Dyson VM_PROT_READ|VM_PROT_WRITE|VM_PROT_OVERRIDE_WRITE, 2604866e085SJohn Dyson &fs.entry, &fs.first_object, &fs.first_pindex, &prot, &wired); 261a976eb5eSAlan Cox if (result != KERN_SUCCESS) 2627788e219SAlan Cox return (result); 26347221757SJohn Dyson 2647aaaa4fdSJohn Dyson /* 2657aaaa4fdSJohn Dyson * If we don't COW now, on a user wire, the user will never 2667aaaa4fdSJohn Dyson * be able to write to the mapping. If we don't make this 2677aaaa4fdSJohn Dyson * restriction, the bookkeeping would be nearly impossible. 268ff8f4ebeSAlan Cox * 269ff8f4ebeSAlan Cox * XXX The following assignment modifies the map without 270ff8f4ebeSAlan Cox * holding a write lock on it. 2717aaaa4fdSJohn Dyson */ 2724866e085SJohn Dyson if ((fs.entry->protection & VM_PROT_WRITE) == 0) 2734866e085SJohn Dyson fs.entry->max_protection &= ~VM_PROT_WRITE; 2747aaaa4fdSJohn Dyson } 27547221757SJohn Dyson 2764866e085SJohn Dyson map_generation = fs.map->timestamp; 2772d8acc0fSJohn Dyson 2784866e085SJohn Dyson if (fs.entry->eflags & MAP_ENTRY_NOFAULT) { 27947221757SJohn Dyson panic("vm_fault: fault on nofault entry, addr: %lx", 28092c4c4ebSBruce Evans (u_long)vaddr); 2817aaaa4fdSJohn Dyson } 2827aaaa4fdSJohn Dyson 28395e5e988SJohn Dyson /* 28495e5e988SJohn Dyson * Make a reference to this object to prevent its disposal while we 28595e5e988SJohn Dyson * are messing with it. Once we have the reference, the map is free 28695e5e988SJohn Dyson * to be diddled. Since objects reference their shadows (and copies), 28795e5e988SJohn Dyson * they will stay around as well. 288fe8e0238SMatthew Dillon * 289fe8e0238SMatthew Dillon * Bump the paging-in-progress count to prevent size changes (e.g. 290fe8e0238SMatthew Dillon * truncation operations) during I/O. This must be done after 291fe8e0238SMatthew Dillon * obtaining the vnode lock in order to avoid possible deadlocks. 292532eadefSAlan Cox * 293532eadefSAlan Cox * XXX vnode_pager_lock() can block without releasing the map lock. 29495e5e988SJohn Dyson */ 295ed4fe4f4SJeff Roberson if (fs.first_object->flags & OBJ_NEEDGIANT) 296a976eb5eSAlan Cox mtx_lock(&Giant); 297d22bc710SAlan Cox VM_OBJECT_LOCK(fs.first_object); 298a976eb5eSAlan Cox vm_object_reference_locked(fs.first_object); 299417a26a1SAlan Cox fs.vp = vnode_pager_lock(fs.first_object); 3005e4bdb57SAlan Cox KASSERT(fs.vp == NULL || !fs.map->system_map, 3015e4bdb57SAlan Cox ("vm_fault: vnode-backed object mapped by system map")); 302ed4fe4f4SJeff Roberson KASSERT((fs.first_object->flags & OBJ_NEEDGIANT) == 0 || 303ed4fe4f4SJeff Roberson !fs.map->system_map, 304ed4fe4f4SJeff Roberson ("vm_fault: Object requiring giant mapped by system map")); 305ed4fe4f4SJeff Roberson if (fs.first_object->flags & OBJ_NEEDGIANT && debug_mpsafevm) 306c1fbc251SAlan Cox mtx_unlock(&Giant); 307d474eaaaSDoug Rabson vm_object_pip_add(fs.first_object, 1); 30895e5e988SJohn Dyson 30925adb370SBrian Feldman fs.lookup_still_valid = TRUE; 310df8bae1dSRodney W. Grimes 311df8bae1dSRodney W. Grimes if (wired) 312df8bae1dSRodney W. Grimes fault_type = prot; 313df8bae1dSRodney W. Grimes 3144866e085SJohn Dyson fs.first_m = NULL; 315df8bae1dSRodney W. Grimes 316df8bae1dSRodney W. Grimes /* 317df8bae1dSRodney W. Grimes * Search for the page at object/offset. 318df8bae1dSRodney W. Grimes */ 3194866e085SJohn Dyson fs.object = fs.first_object; 3204866e085SJohn Dyson fs.pindex = fs.first_pindex; 321df8bae1dSRodney W. Grimes while (TRUE) { 3221c7c3c6aSMatthew Dillon /* 3231c7c3c6aSMatthew Dillon * If the object is dead, we stop here 3241c7c3c6aSMatthew Dillon */ 3254866e085SJohn Dyson if (fs.object->flags & OBJ_DEAD) { 3264866e085SJohn Dyson unlock_and_deallocate(&fs); 32747221757SJohn Dyson return (KERN_PROTECTION_FAILURE); 32847221757SJohn Dyson } 32947221757SJohn Dyson 3301c7c3c6aSMatthew Dillon /* 3311c7c3c6aSMatthew Dillon * See if page is resident 3321c7c3c6aSMatthew Dillon */ 3334866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 3344866e085SJohn Dyson if (fs.m != NULL) { 33586735996SAlan Cox int queue; 33698cb733cSKenneth D. Merry 33798cb733cSKenneth D. Merry /* 3389b80d344SKenneth D. Merry * check for page-based copy on write. 3399b80d344SKenneth D. Merry * We check fs.object == fs.first_object so 3409b80d344SKenneth D. Merry * as to ensure the legacy COW mechanism is 3419b80d344SKenneth D. Merry * used when the page in question is part of 3429b80d344SKenneth D. Merry * a shadow object. Otherwise, vm_page_cowfault() 3439b80d344SKenneth D. Merry * removes the page from the backing object, 3449b80d344SKenneth D. Merry * which is not what we want. 34598cb733cSKenneth D. Merry */ 346f4ecdf05SAlan Cox vm_page_lock_queues(); 34798cb733cSKenneth D. Merry if ((fs.m->cow) && 3489b80d344SKenneth D. Merry (fault_type & VM_PROT_WRITE) && 3499b80d344SKenneth D. Merry (fs.object == fs.first_object)) { 35098cb733cSKenneth D. Merry vm_page_cowfault(fs.m); 351f4ecdf05SAlan Cox vm_page_unlock_queues(); 352d18e8afeSAlan Cox unlock_and_deallocate(&fs); 35398cb733cSKenneth D. Merry goto RetryFault; 35498cb733cSKenneth D. Merry } 35598cb733cSKenneth D. Merry 356df8bae1dSRodney W. Grimes /* 3571c7c3c6aSMatthew Dillon * Wait/Retry if the page is busy. We have to do this 3581c7c3c6aSMatthew Dillon * if the page is busy via either PG_BUSY or 3591c7c3c6aSMatthew Dillon * vm_page_t->busy because the vm_pager may be using 3601c7c3c6aSMatthew Dillon * vm_page_t->busy for pageouts ( and even pageins if 3611c7c3c6aSMatthew Dillon * it is the vnode pager ), and we could end up trying 362956f3135SPhilippe Charnier * to pagein and pageout the same page simultaneously. 3631c7c3c6aSMatthew Dillon * 3641c7c3c6aSMatthew Dillon * We can theoretically allow the busy case on a read 3651c7c3c6aSMatthew Dillon * fault if the page is marked valid, but since such 3661c7c3c6aSMatthew Dillon * pages are typically already pmap'd, putting that 3671c7c3c6aSMatthew Dillon * special case in might be more effort then it is 3681c7c3c6aSMatthew Dillon * worth. We cannot under any circumstances mess 3691c7c3c6aSMatthew Dillon * around with a vm_page_t->busy page except, perhaps, 3701c7c3c6aSMatthew Dillon * to pmap it. 371df8bae1dSRodney W. Grimes */ 3721c7c3c6aSMatthew Dillon if ((fs.m->flags & PG_BUSY) || fs.m->busy) { 3734abd55b2SAlan Cox vm_page_unlock_queues(); 374a51b0840SAlan Cox VM_OBJECT_UNLOCK(fs.object); 375a51b0840SAlan Cox if (fs.object != fs.first_object) { 376a51b0840SAlan Cox VM_OBJECT_LOCK(fs.first_object); 3779a96b638SAlan Cox vm_page_lock_queues(); 378a51b0840SAlan Cox vm_page_free(fs.first_m); 3799a96b638SAlan Cox vm_page_unlock_queues(); 380a51b0840SAlan Cox vm_object_pip_wakeup(fs.first_object); 381a51b0840SAlan Cox VM_OBJECT_UNLOCK(fs.first_object); 382a51b0840SAlan Cox fs.first_m = NULL; 383a51b0840SAlan Cox } 384a51b0840SAlan Cox unlock_map(&fs); 385a51b0840SAlan Cox if (fs.vp != NULL) { 386ed4fe4f4SJeff Roberson int vfslck; 387ed4fe4f4SJeff Roberson 388ed4fe4f4SJeff Roberson vfslck = VFS_LOCK_GIANT(fs.vp->v_mount); 389a51b0840SAlan Cox vput(fs.vp); 390a51b0840SAlan Cox fs.vp = NULL; 391ed4fe4f4SJeff Roberson VFS_UNLOCK_GIANT(vfslck); 392a51b0840SAlan Cox } 393a51b0840SAlan Cox VM_OBJECT_LOCK(fs.object); 394a51b0840SAlan Cox if (fs.m == vm_page_lookup(fs.object, 395a51b0840SAlan Cox fs.pindex)) { 396a51b0840SAlan Cox vm_page_lock_queues(); 397a51b0840SAlan Cox if (!vm_page_sleep_if_busy(fs.m, TRUE, 398a51b0840SAlan Cox "vmpfw")) 399a51b0840SAlan Cox vm_page_unlock_queues(); 400a51b0840SAlan Cox } 401a51b0840SAlan Cox vm_object_pip_wakeup(fs.object); 402a51b0840SAlan Cox VM_OBJECT_UNLOCK(fs.object); 403c1fbc251SAlan Cox atomic_add_int(&cnt.v_intrans, 1); 404b9d3b805SJohn Baldwin if (fs.first_object->flags & OBJ_NEEDGIANT) 405c1fbc251SAlan Cox VM_UNLOCK_GIANT(); 4064866e085SJohn Dyson vm_object_deallocate(fs.first_object); 407df8bae1dSRodney W. Grimes goto RetryFault; 408df8bae1dSRodney W. Grimes } 4094866e085SJohn Dyson queue = fs.m->queue; 410a1287949SEivind Eklund 4116d03d577SMatthew Dillon vm_pageq_remove_nowakeup(fs.m); 412c82b0181SJohn Dyson 41382eedee4SAlan Cox if (VM_PAGE_RESOLVEQUEUE(fs.m, queue) == PQ_CACHE && 41482eedee4SAlan Cox vm_page_count_severe()) { 4154866e085SJohn Dyson vm_page_activate(fs.m); 4164abd55b2SAlan Cox vm_page_unlock_queues(); 4174866e085SJohn Dyson unlock_and_deallocate(&fs); 418ef6020d1SMike Silbersack VM_WAITPFAULT; 41922ba64e8SJohn Dyson goto RetryFault; 42022ba64e8SJohn Dyson } 4217615edaaSMatthew Dillon 4221c7c3c6aSMatthew Dillon /* 4231c7c3c6aSMatthew Dillon * Mark page busy for other processes, and the 4241c7c3c6aSMatthew Dillon * pagedaemon. If it still isn't completely valid 4251c7c3c6aSMatthew Dillon * (readable), jump to readrest, else break-out ( we 4261c7c3c6aSMatthew Dillon * found the page ). 4271c7c3c6aSMatthew Dillon */ 428e69763a3SDoug Rabson vm_page_busy(fs.m); 4294abd55b2SAlan Cox vm_page_unlock_queues(); 4304866e085SJohn Dyson if (((fs.m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL) && 4314866e085SJohn Dyson fs.m->object != kernel_object && fs.m->object != kmem_object) { 4320d94caffSDavid Greenman goto readrest; 4330d94caffSDavid Greenman } 434ffc82b0aSJohn Dyson 435df8bae1dSRodney W. Grimes break; 436df8bae1dSRodney W. Grimes } 4371c7c3c6aSMatthew Dillon 4381c7c3c6aSMatthew Dillon /* 43940360b1bSMatthew Dillon * Page is not resident, If this is the search termination 44040360b1bSMatthew Dillon * or the pager might contain the page, allocate a new page. 4411c7c3c6aSMatthew Dillon */ 44240360b1bSMatthew Dillon if (TRYPAGER || fs.object == fs.first_object) { 4434866e085SJohn Dyson if (fs.pindex >= fs.object->size) { 4444866e085SJohn Dyson unlock_and_deallocate(&fs); 4455f55e841SDavid Greenman return (KERN_PROTECTION_FAILURE); 4465f55e841SDavid Greenman } 44722ba64e8SJohn Dyson 448df8bae1dSRodney W. Grimes /* 4490d94caffSDavid Greenman * Allocate a new page for this object/offset pair. 450df8bae1dSRodney W. Grimes */ 45140360b1bSMatthew Dillon fs.m = NULL; 45240360b1bSMatthew Dillon if (!vm_page_count_severe()) { 4534866e085SJohn Dyson fs.m = vm_page_alloc(fs.object, fs.pindex, 4544866e085SJohn Dyson (fs.vp || fs.object->backing_object)? VM_ALLOC_NORMAL: VM_ALLOC_ZERO); 45540360b1bSMatthew Dillon } 4564866e085SJohn Dyson if (fs.m == NULL) { 4574866e085SJohn Dyson unlock_and_deallocate(&fs); 458ef6020d1SMike Silbersack VM_WAITPFAULT; 459df8bae1dSRodney W. Grimes goto RetryFault; 460df8bae1dSRodney W. Grimes } 461df8bae1dSRodney W. Grimes } 46247221757SJohn Dyson 4630d94caffSDavid Greenman readrest: 4641c7c3c6aSMatthew Dillon /* 46540360b1bSMatthew Dillon * We have found a valid page or we have allocated a new page. 46640360b1bSMatthew Dillon * The page thus may not be valid or may not be entirely 46740360b1bSMatthew Dillon * valid. 46840360b1bSMatthew Dillon * 46940360b1bSMatthew Dillon * Attempt to fault-in the page if there is a chance that the 47040360b1bSMatthew Dillon * pager has it, and potentially fault in additional pages 47140360b1bSMatthew Dillon * at the same time. 4721c7c3c6aSMatthew Dillon */ 47340360b1bSMatthew Dillon if (TRYPAGER) { 474df8bae1dSRodney W. Grimes int rv; 47526f9a767SRodney W. Grimes int reqpage; 476867a482dSJohn Dyson int ahead, behind; 4777f866e4bSAlan Cox u_char behavior = vm_map_entry_behavior(fs.entry); 478867a482dSJohn Dyson 4797f866e4bSAlan Cox if (behavior == MAP_ENTRY_BEHAV_RANDOM) { 480867a482dSJohn Dyson ahead = 0; 481867a482dSJohn Dyson behind = 0; 4822d8acc0fSJohn Dyson } else { 4834866e085SJohn Dyson behind = (vaddr - fs.entry->start) >> PAGE_SHIFT; 4842d8acc0fSJohn Dyson if (behind > VM_FAULT_READ_BEHIND) 4852d8acc0fSJohn Dyson behind = VM_FAULT_READ_BEHIND; 4862d8acc0fSJohn Dyson 4874866e085SJohn Dyson ahead = ((fs.entry->end - vaddr) >> PAGE_SHIFT) - 1; 4882d8acc0fSJohn Dyson if (ahead > VM_FAULT_READ_AHEAD) 4892d8acc0fSJohn Dyson ahead = VM_FAULT_READ_AHEAD; 490867a482dSJohn Dyson } 4918d8b9c6eSAlan Cox is_first_object_locked = FALSE; 4928d8b9c6eSAlan Cox if ((behavior == MAP_ENTRY_BEHAV_SEQUENTIAL || 49340360b1bSMatthew Dillon (behavior != MAP_ENTRY_BEHAV_RANDOM && 49440360b1bSMatthew Dillon fs.pindex >= fs.entry->lastr && 4958d8b9c6eSAlan Cox fs.pindex < fs.entry->lastr + VM_FAULT_READ)) && 4968d8b9c6eSAlan Cox (fs.first_object == fs.object || 4978d8b9c6eSAlan Cox (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object))) && 4988d8b9c6eSAlan Cox fs.first_object->type != OBJT_DEVICE) { 499867a482dSJohn Dyson vm_pindex_t firstpindex, tmppindex; 50040360b1bSMatthew Dillon 50140360b1bSMatthew Dillon if (fs.first_pindex < 2 * VM_FAULT_READ) 502867a482dSJohn Dyson firstpindex = 0; 503867a482dSJohn Dyson else 50440360b1bSMatthew Dillon firstpindex = fs.first_pindex - 2 * VM_FAULT_READ; 505867a482dSJohn Dyson 50615a5d210SAlan Cox vm_page_lock_queues(); 5074221e284SAlan Cox /* 5084221e284SAlan Cox * note: partially valid pages cannot be 5094221e284SAlan Cox * included in the lookahead - NFS piecemeal 5104221e284SAlan Cox * writes will barf on it badly. 5114221e284SAlan Cox */ 5124866e085SJohn Dyson for (tmppindex = fs.first_pindex - 1; 5132100d645SPeter Wemm tmppindex >= firstpindex; 514867a482dSJohn Dyson --tmppindex) { 515867a482dSJohn Dyson vm_page_t mt; 516a1287949SEivind Eklund 5174866e085SJohn Dyson mt = vm_page_lookup(fs.first_object, tmppindex); 518867a482dSJohn Dyson if (mt == NULL || (mt->valid != VM_PAGE_BITS_ALL)) 519867a482dSJohn Dyson break; 520ff97964aSJohn Dyson if (mt->busy || 5218b03c8edSMatthew Dillon (mt->flags & (PG_BUSY | PG_FICTITIOUS | PG_UNMANAGED)) || 522ff97964aSJohn Dyson mt->hold_count || 523867a482dSJohn Dyson mt->wire_count) 524867a482dSJohn Dyson continue; 5254fec79beSAlan Cox pmap_remove_all(mt); 526c6d9ef2eSAlan Cox if (mt->dirty) { 527867a482dSJohn Dyson vm_page_deactivate(mt); 528867a482dSJohn Dyson } else { 529867a482dSJohn Dyson vm_page_cache(mt); 530867a482dSJohn Dyson } 531867a482dSJohn Dyson } 53215a5d210SAlan Cox vm_page_unlock_queues(); 533867a482dSJohn Dyson ahead += behind; 534867a482dSJohn Dyson behind = 0; 535867a482dSJohn Dyson } 5368d8b9c6eSAlan Cox if (is_first_object_locked) 5378d8b9c6eSAlan Cox VM_OBJECT_UNLOCK(fs.first_object); 538df8bae1dSRodney W. Grimes /* 5390d94caffSDavid Greenman * now we find out if any other pages should be paged 5400d94caffSDavid Greenman * in at this time this routine checks to see if the 5410d94caffSDavid Greenman * pages surrounding this fault reside in the same 5420d94caffSDavid Greenman * object as the page for this fault. If they do, 5430d94caffSDavid Greenman * then they are faulted in also into the object. The 5440d94caffSDavid Greenman * array "marray" returned contains an array of 5450d94caffSDavid Greenman * vm_page_t structs where one of them is the 5460d94caffSDavid Greenman * vm_page_t passed to the routine. The reqpage 5470d94caffSDavid Greenman * return value is the index into the marray for the 5480d94caffSDavid Greenman * vm_page_t passed to the routine. 5491c7c3c6aSMatthew Dillon * 5501c7c3c6aSMatthew Dillon * fs.m plus the additional pages are PG_BUSY'd. 551532eadefSAlan Cox * 552532eadefSAlan Cox * XXX vm_fault_additional_pages() can block 553532eadefSAlan Cox * without releasing the map lock. 55426f9a767SRodney W. Grimes */ 55505f0fdd2SPoul-Henning Kamp faultcount = vm_fault_additional_pages( 5564866e085SJohn Dyson fs.m, behind, ahead, marray, &reqpage); 557df8bae1dSRodney W. Grimes 558df8bae1dSRodney W. Grimes /* 55940360b1bSMatthew Dillon * update lastr imperfectly (we do not know how much 56040360b1bSMatthew Dillon * getpages will actually read), but good enough. 561532eadefSAlan Cox * 562532eadefSAlan Cox * XXX The following assignment modifies the map 563532eadefSAlan Cox * without holding a write lock on it. 56440360b1bSMatthew Dillon */ 56540360b1bSMatthew Dillon fs.entry->lastr = fs.pindex + faultcount - behind; 56640360b1bSMatthew Dillon 56740360b1bSMatthew Dillon /* 5680d94caffSDavid Greenman * Call the pager to retrieve the data, if any, after 5691c7c3c6aSMatthew Dillon * releasing the lock on the map. We hold a ref on 5701c7c3c6aSMatthew Dillon * fs.object and the pages are PG_BUSY'd. 571df8bae1dSRodney W. Grimes */ 5724866e085SJohn Dyson unlock_map(&fs); 573df8bae1dSRodney W. Grimes 57426f9a767SRodney W. Grimes rv = faultcount ? 5754866e085SJohn Dyson vm_pager_get_pages(fs.object, marray, faultcount, 57624a1cce3SDavid Greenman reqpage) : VM_PAGER_FAIL; 57722ba64e8SJohn Dyson 57826f9a767SRodney W. Grimes if (rv == VM_PAGER_OK) { 579df8bae1dSRodney W. Grimes /* 580f230c45cSJohn Dyson * Found the page. Leave it busy while we play 581f230c45cSJohn Dyson * with it. 582f230c45cSJohn Dyson */ 583f230c45cSJohn Dyson 584f230c45cSJohn Dyson /* 5850d94caffSDavid Greenman * Relookup in case pager changed page. Pager 5860d94caffSDavid Greenman * is responsible for disposition of old page 5870d94caffSDavid Greenman * if moved. 588df8bae1dSRodney W. Grimes */ 5894866e085SJohn Dyson fs.m = vm_page_lookup(fs.object, fs.pindex); 5904866e085SJohn Dyson if (!fs.m) { 5914866e085SJohn Dyson unlock_and_deallocate(&fs); 592f6b04d2bSDavid Greenman goto RetryFault; 593f6b04d2bSDavid Greenman } 594f6b04d2bSDavid Greenman 59526f9a767SRodney W. Grimes hardfault++; 5961c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 597df8bae1dSRodney W. Grimes } 598df8bae1dSRodney W. Grimes /* 5990d94caffSDavid Greenman * Remove the bogus page (which does not exist at this 6000d94caffSDavid Greenman * object/offset); before doing so, we must get back 6010d94caffSDavid Greenman * our object lock to preserve our invariant. 602df8bae1dSRodney W. Grimes * 60324a1cce3SDavid Greenman * Also wake up any other process that may want to bring 6040d94caffSDavid Greenman * in this page. 605df8bae1dSRodney W. Grimes * 6060d94caffSDavid Greenman * If this is the top-level object, we must leave the 60724a1cce3SDavid Greenman * busy page to prevent another process from rushing 6080d94caffSDavid Greenman * past us, and inserting the page in that object at 6090d94caffSDavid Greenman * the same time that we are. 610df8bae1dSRodney W. Grimes */ 611a83c285cSDavid Greenman if (rv == VM_PAGER_ERROR) 612f3679e35SDavid Greenman printf("vm_fault: pager read error, pid %d (%s)\n", 613f3679e35SDavid Greenman curproc->p_pid, curproc->p_comm); 61426f9a767SRodney W. Grimes /* 615a83c285cSDavid Greenman * Data outside the range of the pager or an I/O error 61626f9a767SRodney W. Grimes */ 617a83c285cSDavid Greenman /* 6180d94caffSDavid Greenman * XXX - the check for kernel_map is a kludge to work 6190d94caffSDavid Greenman * around having the machine panic on a kernel space 6200d94caffSDavid Greenman * fault w/ I/O error. 621a83c285cSDavid Greenman */ 6224866e085SJohn Dyson if (((fs.map != kernel_map) && (rv == VM_PAGER_ERROR)) || 62347221757SJohn Dyson (rv == VM_PAGER_BAD)) { 6242ad98273SAlan Cox vm_page_lock_queues(); 6254866e085SJohn Dyson vm_page_free(fs.m); 6262ad98273SAlan Cox vm_page_unlock_queues(); 6274866e085SJohn Dyson fs.m = NULL; 6284866e085SJohn Dyson unlock_and_deallocate(&fs); 629a83c285cSDavid Greenman return ((rv == VM_PAGER_ERROR) ? KERN_FAILURE : KERN_PROTECTION_FAILURE); 63026f9a767SRodney W. Grimes } 6314866e085SJohn Dyson if (fs.object != fs.first_object) { 6322ad98273SAlan Cox vm_page_lock_queues(); 6334866e085SJohn Dyson vm_page_free(fs.m); 6342ad98273SAlan Cox vm_page_unlock_queues(); 6354866e085SJohn Dyson fs.m = NULL; 63626f9a767SRodney W. Grimes /* 63726f9a767SRodney W. Grimes * XXX - we cannot just fall out at this 63826f9a767SRodney W. Grimes * point, m has been freed and is invalid! 63926f9a767SRodney W. Grimes */ 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes } 64240360b1bSMatthew Dillon 643df8bae1dSRodney W. Grimes /* 6441c7c3c6aSMatthew Dillon * We get here if the object has default pager (or unwiring) 6451c7c3c6aSMatthew Dillon * or the pager doesn't have the page. 646df8bae1dSRodney W. Grimes */ 6474866e085SJohn Dyson if (fs.object == fs.first_object) 6484866e085SJohn Dyson fs.first_m = fs.m; 649df8bae1dSRodney W. Grimes 650df8bae1dSRodney W. Grimes /* 6510d94caffSDavid Greenman * Move on to the next object. Lock the next object before 6520d94caffSDavid Greenman * unlocking the current one. 653df8bae1dSRodney W. Grimes */ 6544866e085SJohn Dyson fs.pindex += OFF_TO_IDX(fs.object->backing_object_offset); 6554866e085SJohn Dyson next_object = fs.object->backing_object; 656df8bae1dSRodney W. Grimes if (next_object == NULL) { 657df8bae1dSRodney W. Grimes /* 6580d94caffSDavid Greenman * If there's no object left, fill the page in the top 6590d94caffSDavid Greenman * object with zeros. 660df8bae1dSRodney W. Grimes */ 6614866e085SJohn Dyson if (fs.object != fs.first_object) { 6624866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 663b009d5a0SAlan Cox VM_OBJECT_UNLOCK(fs.object); 664df8bae1dSRodney W. Grimes 6654866e085SJohn Dyson fs.object = fs.first_object; 6664866e085SJohn Dyson fs.pindex = fs.first_pindex; 6674866e085SJohn Dyson fs.m = fs.first_m; 668f29ba63eSAlan Cox VM_OBJECT_LOCK(fs.object); 669df8bae1dSRodney W. Grimes } 6704866e085SJohn Dyson fs.first_m = NULL; 671df8bae1dSRodney W. Grimes 6724221e284SAlan Cox /* 6734221e284SAlan Cox * Zero the page if necessary and mark it valid. 6744221e284SAlan Cox */ 6754866e085SJohn Dyson if ((fs.m->flags & PG_ZERO) == 0) { 676fff6062aSAlan Cox pmap_zero_page(fs.m); 6774221e284SAlan Cox } else { 678c1fbc251SAlan Cox atomic_add_int(&cnt.v_ozfod, 1); 6794221e284SAlan Cox } 680c1fbc251SAlan Cox atomic_add_int(&cnt.v_zfod, 1); 6814221e284SAlan Cox fs.m->valid = VM_PAGE_BITS_ALL; 6821c7c3c6aSMatthew Dillon break; /* break to PAGE HAS BEEN FOUND */ 6830d94caffSDavid Greenman } else { 684c8567c3aSAlan Cox KASSERT(fs.object != next_object, 685c8567c3aSAlan Cox ("object loop %p", next_object)); 686c8567c3aSAlan Cox VM_OBJECT_LOCK(next_object); 687c8567c3aSAlan Cox vm_object_pip_add(next_object, 1); 688c8567c3aSAlan Cox if (fs.object != fs.first_object) 6894866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 690b009d5a0SAlan Cox VM_OBJECT_UNLOCK(fs.object); 6914866e085SJohn Dyson fs.object = next_object; 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes } 6941c7c3c6aSMatthew Dillon 6955526d2d9SEivind Eklund KASSERT((fs.m->flags & PG_BUSY) != 0, 6965526d2d9SEivind Eklund ("vm_fault: not busy after main loop")); 697df8bae1dSRodney W. Grimes 698df8bae1dSRodney W. Grimes /* 6990d94caffSDavid Greenman * PAGE HAS BEEN FOUND. [Loop invariant still holds -- the object lock 700df8bae1dSRodney W. Grimes * is held.] 701df8bae1dSRodney W. Grimes */ 702df8bae1dSRodney W. Grimes 703df8bae1dSRodney W. Grimes /* 7040d94caffSDavid Greenman * If the page is being written, but isn't already owned by the 7050d94caffSDavid Greenman * top-level object, we have to copy it into a new page owned by the 7060d94caffSDavid Greenman * top-level object. 707df8bae1dSRodney W. Grimes */ 7084866e085SJohn Dyson if (fs.object != fs.first_object) { 709df8bae1dSRodney W. Grimes /* 7100d94caffSDavid Greenman * We only really need to copy if we want to write it. 711df8bae1dSRodney W. Grimes */ 712df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 713df8bae1dSRodney W. Grimes /* 7141c7c3c6aSMatthew Dillon * This allows pages to be virtually copied from a 7151c7c3c6aSMatthew Dillon * backing_object into the first_object, where the 7161c7c3c6aSMatthew Dillon * backing object has no other refs to it, and cannot 7171c7c3c6aSMatthew Dillon * gain any more refs. Instead of a bcopy, we just 7181c7c3c6aSMatthew Dillon * move the page from the backing object to the 7191c7c3c6aSMatthew Dillon * first object. Note that we must mark the page 7201c7c3c6aSMatthew Dillon * dirty in the first object so that it will go out 7211c7c3c6aSMatthew Dillon * to swap when needed. 722df8bae1dSRodney W. Grimes */ 723ebf75125SAlan Cox is_first_object_locked = FALSE; 724e50346b5SAlan Cox if ( 725de5f6a77SJohn Dyson /* 726de5f6a77SJohn Dyson * Only one shadow object 727de5f6a77SJohn Dyson */ 7284866e085SJohn Dyson (fs.object->shadow_count == 1) && 729de5f6a77SJohn Dyson /* 730de5f6a77SJohn Dyson * No COW refs, except us 731de5f6a77SJohn Dyson */ 7324866e085SJohn Dyson (fs.object->ref_count == 1) && 733de5f6a77SJohn Dyson /* 7345929bcfaSPhilippe Charnier * No one else can look this object up 735de5f6a77SJohn Dyson */ 7364866e085SJohn Dyson (fs.object->handle == NULL) && 737de5f6a77SJohn Dyson /* 738de5f6a77SJohn Dyson * No other ways to look the object up 739de5f6a77SJohn Dyson */ 7404866e085SJohn Dyson ((fs.object->type == OBJT_DEFAULT) || 7414866e085SJohn Dyson (fs.object->type == OBJT_SWAP)) && 742ebf75125SAlan Cox (is_first_object_locked = VM_OBJECT_TRYLOCK(fs.first_object)) && 743de5f6a77SJohn Dyson /* 744de5f6a77SJohn Dyson * We don't chase down the shadow chain 745de5f6a77SJohn Dyson */ 746e50346b5SAlan Cox fs.object == fs.first_object->backing_object) { 747d98ddc46SAlan Cox vm_page_lock_queues(); 7482d8acc0fSJohn Dyson /* 749de5f6a77SJohn Dyson * get rid of the unnecessary page 750df8bae1dSRodney W. Grimes */ 7514866e085SJohn Dyson vm_page_free(fs.first_m); 752de5f6a77SJohn Dyson /* 7531c7c3c6aSMatthew Dillon * grab the page and put it into the 7541c7c3c6aSMatthew Dillon * process'es object. The page is 7551c7c3c6aSMatthew Dillon * automatically made dirty. 756de5f6a77SJohn Dyson */ 7574866e085SJohn Dyson vm_page_rename(fs.m, fs.first_object, fs.first_pindex); 758d98ddc46SAlan Cox vm_page_busy(fs.m); 7599a96b638SAlan Cox vm_page_unlock_queues(); 760d98ddc46SAlan Cox fs.first_m = fs.m; 7614866e085SJohn Dyson fs.m = NULL; 762c1fbc251SAlan Cox atomic_add_int(&cnt.v_cow_optim, 1); 763de5f6a77SJohn Dyson } else { 764de5f6a77SJohn Dyson /* 765de5f6a77SJohn Dyson * Oh, well, lets copy it. 766de5f6a77SJohn Dyson */ 767669890eaSAlan Cox pmap_copy_page(fs.m, fs.first_m); 768669890eaSAlan Cox fs.first_m->valid = VM_PAGE_BITS_ALL; 769de5f6a77SJohn Dyson } 7704866e085SJohn Dyson if (fs.m) { 771df8bae1dSRodney W. Grimes /* 772df8bae1dSRodney W. Grimes * We no longer need the old page or object. 773df8bae1dSRodney W. Grimes */ 7744866e085SJohn Dyson release_page(&fs); 775de5f6a77SJohn Dyson } 7761c7c3c6aSMatthew Dillon /* 7771c7c3c6aSMatthew Dillon * fs.object != fs.first_object due to above 7781c7c3c6aSMatthew Dillon * conditional 7791c7c3c6aSMatthew Dillon */ 7804866e085SJohn Dyson vm_object_pip_wakeup(fs.object); 781b009d5a0SAlan Cox VM_OBJECT_UNLOCK(fs.object); 782df8bae1dSRodney W. Grimes /* 783df8bae1dSRodney W. Grimes * Only use the new page below... 784df8bae1dSRodney W. Grimes */ 7854866e085SJohn Dyson fs.object = fs.first_object; 7864866e085SJohn Dyson fs.pindex = fs.first_pindex; 787d98ddc46SAlan Cox fs.m = fs.first_m; 788f29ba63eSAlan Cox if (!is_first_object_locked) 789f29ba63eSAlan Cox VM_OBJECT_LOCK(fs.object); 790c1fbc251SAlan Cox atomic_add_int(&cnt.v_cow_faults, 1); 7910d94caffSDavid Greenman } else { 792df8bae1dSRodney W. Grimes prot &= ~VM_PROT_WRITE; 793df8bae1dSRodney W. Grimes } 794df8bae1dSRodney W. Grimes } 795df8bae1dSRodney W. Grimes 796df8bae1dSRodney W. Grimes /* 7970d94caffSDavid Greenman * We must verify that the maps have not changed since our last 7980d94caffSDavid Greenman * lookup. 799df8bae1dSRodney W. Grimes */ 80019dc5607STor Egge if (!fs.lookup_still_valid) { 801df8bae1dSRodney W. Grimes vm_object_t retry_object; 802a316d390SJohn Dyson vm_pindex_t retry_pindex; 803df8bae1dSRodney W. Grimes vm_prot_t retry_prot; 804df8bae1dSRodney W. Grimes 80519dc5607STor Egge if (!vm_map_trylock_read(fs.map)) { 806b823bbd6SMatthew Dillon release_page(&fs); 807b823bbd6SMatthew Dillon unlock_and_deallocate(&fs); 808b823bbd6SMatthew Dillon goto RetryFault; 809b823bbd6SMatthew Dillon } 81019dc5607STor Egge fs.lookup_still_valid = TRUE; 81119dc5607STor Egge if (fs.map->timestamp != map_generation) { 81219dc5607STor Egge result = vm_map_lookup_locked(&fs.map, vaddr, fault_type, 8134866e085SJohn Dyson &fs.entry, &retry_object, &retry_pindex, &retry_prot, &wired); 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes /* 81644ed3417STor Egge * If we don't need the page any longer, put it on the inactive 8170d94caffSDavid Greenman * list (the easiest thing to do here). If no one needs it, 8180d94caffSDavid Greenman * pageout will grab it eventually. 819df8bae1dSRodney W. Grimes */ 820df8bae1dSRodney W. Grimes if (result != KERN_SUCCESS) { 8214866e085SJohn Dyson release_page(&fs); 8224866e085SJohn Dyson unlock_and_deallocate(&fs); 82319dc5607STor Egge 82419dc5607STor Egge /* 82519dc5607STor Egge * If retry of map lookup would have blocked then 82619dc5607STor Egge * retry fault from start. 82719dc5607STor Egge */ 82819dc5607STor Egge if (result == KERN_FAILURE) 82919dc5607STor Egge goto RetryFault; 830df8bae1dSRodney W. Grimes return (result); 831df8bae1dSRodney W. Grimes } 8324866e085SJohn Dyson if ((retry_object != fs.first_object) || 8334866e085SJohn Dyson (retry_pindex != fs.first_pindex)) { 8344866e085SJohn Dyson release_page(&fs); 8354866e085SJohn Dyson unlock_and_deallocate(&fs); 836df8bae1dSRodney W. Grimes goto RetryFault; 837df8bae1dSRodney W. Grimes } 83819dc5607STor Egge 839df8bae1dSRodney W. Grimes /* 8400d94caffSDavid Greenman * Check whether the protection has changed or the object has 8410d94caffSDavid Greenman * been copied while we left the map unlocked. Changing from 8420d94caffSDavid Greenman * read to write permission is OK - we leave the page 8430d94caffSDavid Greenman * write-protected, and catch the write fault. Changing from 8440d94caffSDavid Greenman * write to read permission means that we can't mark the page 8450d94caffSDavid Greenman * write-enabled after all. 846df8bae1dSRodney W. Grimes */ 847df8bae1dSRodney W. Grimes prot &= retry_prot; 848df8bae1dSRodney W. Grimes } 84919dc5607STor Egge } 8502ddba215SDavid Greenman if (prot & VM_PROT_WRITE) { 8519a96b638SAlan Cox vm_page_lock_queues(); 852e69763a3SDoug Rabson vm_page_flag_set(fs.m, PG_WRITEABLE); 853245df27cSMatthew Dillon vm_object_set_writeable_dirty(fs.m->object); 8544f79d873SMatthew Dillon 8552ddba215SDavid Greenman /* 8562ddba215SDavid Greenman * If the fault is a write, we know that this page is being 8574f79d873SMatthew Dillon * written NOW so dirty it explicitly to save on 8584f79d873SMatthew Dillon * pmap_is_modified() calls later. 8594f79d873SMatthew Dillon * 8604f79d873SMatthew Dillon * If this is a NOSYNC mmap we do not want to set PG_NOSYNC 8614f79d873SMatthew Dillon * if the page is already dirty to prevent data written with 8624f79d873SMatthew Dillon * the expectation of being synced from not being synced. 8634f79d873SMatthew Dillon * Likewise if this entry does not request NOSYNC then make 8644f79d873SMatthew Dillon * sure the page isn't marked NOSYNC. Applications sharing 8654f79d873SMatthew Dillon * data should use the same flags to avoid ping ponging. 8661c7c3c6aSMatthew Dillon * 8671c7c3c6aSMatthew Dillon * Also tell the backing pager, if any, that it should remove 8681c7c3c6aSMatthew Dillon * any swap backing since the page is now dirty. 8692ddba215SDavid Greenman */ 8704f79d873SMatthew Dillon if (fs.entry->eflags & MAP_ENTRY_NOSYNC) { 8714f79d873SMatthew Dillon if (fs.m->dirty == 0) 8724f79d873SMatthew Dillon vm_page_flag_set(fs.m, PG_NOSYNC); 8734f79d873SMatthew Dillon } else { 8744f79d873SMatthew Dillon vm_page_flag_clear(fs.m, PG_NOSYNC); 8754f79d873SMatthew Dillon } 8769a96b638SAlan Cox vm_page_unlock_queues(); 8775bf53acbSMatthew Dillon if (fault_flags & VM_FAULT_DIRTY) { 8787dbf82dcSMatthew Dillon vm_page_dirty(fs.m); 8791c7c3c6aSMatthew Dillon vm_pager_page_unswapped(fs.m); 8802ddba215SDavid Greenman } 8812ddba215SDavid Greenman } 882f6b04d2bSDavid Greenman 883bc6d84a6SMatthew Dillon /* 884bc6d84a6SMatthew Dillon * Page had better still be busy 885bc6d84a6SMatthew Dillon */ 886ca06c247SAlan Cox KASSERT(fs.m->flags & PG_BUSY, 887ca06c247SAlan Cox ("vm_fault: page %p not busy!", fs.m)); 8884221e284SAlan Cox /* 8894221e284SAlan Cox * Sanity check: page must be completely valid or it is not fit to 8904221e284SAlan Cox * map into user space. vm_pager_get_pages() ensures this. 8914221e284SAlan Cox */ 8924221e284SAlan Cox if (fs.m->valid != VM_PAGE_BITS_ALL) { 8934221e284SAlan Cox vm_page_zero_invalid(fs.m, TRUE); 8944221e284SAlan Cox printf("Warning: page %p partially invalid on fault\n", fs.m); 8954221e284SAlan Cox } 896eebf3286SAlan Cox VM_OBJECT_UNLOCK(fs.object); 897cbfbaad8SAlan Cox 89886735996SAlan Cox /* 89986735996SAlan Cox * Put this page into the physical map. We had to do the unlock above 90086735996SAlan Cox * because pmap_enter() may sleep. We don't put the page 90186735996SAlan Cox * back on the active queue until later so that the pageout daemon 90286735996SAlan Cox * won't find it (yet). 90386735996SAlan Cox */ 9040385347cSPeter Wemm pmap_enter(fs.map->pmap, vaddr, fs.m, prot, wired); 9052d8acc0fSJohn Dyson if (((fault_flags & VM_FAULT_WIRE_MASK) == 0) && (wired == 0)) { 906566526a9SAlan Cox vm_fault_prefault(fs.map->pmap, vaddr, fs.entry); 9072d8acc0fSJohn Dyson } 908eebf3286SAlan Cox VM_OBJECT_LOCK(fs.object); 9092d09a6adSAlan Cox vm_page_lock_queues(); 910db44450bSAlan Cox vm_page_flag_set(fs.m, PG_REFERENCED); 911ff97964aSJohn Dyson 912df8bae1dSRodney W. Grimes /* 9130d94caffSDavid Greenman * If the page is not wired down, then put it where the pageout daemon 9140d94caffSDavid Greenman * can find it. 915df8bae1dSRodney W. Grimes */ 916a04c970aSJohn Dyson if (fault_flags & VM_FAULT_WIRE_MASK) { 917df8bae1dSRodney W. Grimes if (wired) 9184866e085SJohn Dyson vm_page_wire(fs.m); 919df8bae1dSRodney W. Grimes else 92073007561SDavid Greenman vm_page_unwire(fs.m, 1); 9210d94caffSDavid Greenman } else { 9224866e085SJohn Dyson vm_page_activate(fs.m); 92326f9a767SRodney W. Grimes } 924a9911f9aSAlan Cox vm_page_wakeup(fs.m); 9252d09a6adSAlan Cox vm_page_unlock_queues(); 926eeec6babSJohn Baldwin 927eebf3286SAlan Cox /* 928eebf3286SAlan Cox * Unlock everything, and return 929eebf3286SAlan Cox */ 930eebf3286SAlan Cox unlock_and_deallocate(&fs); 931eeec6babSJohn Baldwin PROC_LOCK(curproc); 932eeec6babSJohn Baldwin if ((curproc->p_sflag & PS_INMEM) && curproc->p_stats) { 93326f9a767SRodney W. Grimes if (hardfault) { 93426f9a767SRodney W. Grimes curproc->p_stats->p_ru.ru_majflt++; 93526f9a767SRodney W. Grimes } else { 93626f9a767SRodney W. Grimes curproc->p_stats->p_ru.ru_minflt++; 93726f9a767SRodney W. Grimes } 93826f9a767SRodney W. Grimes } 939eeec6babSJohn Baldwin PROC_UNLOCK(curproc); 940df8bae1dSRodney W. Grimes 941df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 942df8bae1dSRodney W. Grimes } 943df8bae1dSRodney W. Grimes 944df8bae1dSRodney W. Grimes /* 945566526a9SAlan Cox * vm_fault_prefault provides a quick way of clustering 946566526a9SAlan Cox * pagefaults into a processes address space. It is a "cousin" 947566526a9SAlan Cox * of vm_map_pmap_enter, except it runs at page fault time instead 948566526a9SAlan Cox * of mmap time. 949566526a9SAlan Cox */ 950566526a9SAlan Cox static void 951566526a9SAlan Cox vm_fault_prefault(pmap_t pmap, vm_offset_t addra, vm_map_entry_t entry) 952566526a9SAlan Cox { 953566526a9SAlan Cox int i; 954566526a9SAlan Cox vm_offset_t addr, starta; 955566526a9SAlan Cox vm_pindex_t pindex; 956566526a9SAlan Cox vm_page_t m, mpte; 957566526a9SAlan Cox vm_object_t object; 958566526a9SAlan Cox 959950d5f7aSAlan Cox if (pmap != vmspace_pmap(curthread->td_proc->p_vmspace)) 960566526a9SAlan Cox return; 961566526a9SAlan Cox 962566526a9SAlan Cox object = entry->object.vm_object; 963566526a9SAlan Cox 964566526a9SAlan Cox starta = addra - PFBAK * PAGE_SIZE; 965566526a9SAlan Cox if (starta < entry->start) { 966566526a9SAlan Cox starta = entry->start; 967566526a9SAlan Cox } else if (starta > addra) { 968566526a9SAlan Cox starta = 0; 969566526a9SAlan Cox } 970566526a9SAlan Cox 971566526a9SAlan Cox mpte = NULL; 972566526a9SAlan Cox for (i = 0; i < PAGEORDER_SIZE; i++) { 973566526a9SAlan Cox vm_object_t backing_object, lobject; 974566526a9SAlan Cox 975566526a9SAlan Cox addr = addra + prefault_pageorder[i]; 976566526a9SAlan Cox if (addr > addra + (PFFOR * PAGE_SIZE)) 977566526a9SAlan Cox addr = 0; 978566526a9SAlan Cox 979566526a9SAlan Cox if (addr < starta || addr >= entry->end) 980566526a9SAlan Cox continue; 981566526a9SAlan Cox 982566526a9SAlan Cox if (!pmap_is_prefaultable(pmap, addr)) 983566526a9SAlan Cox continue; 984566526a9SAlan Cox 985566526a9SAlan Cox pindex = ((addr - entry->start) + entry->offset) >> PAGE_SHIFT; 986566526a9SAlan Cox lobject = object; 987566526a9SAlan Cox VM_OBJECT_LOCK(lobject); 988566526a9SAlan Cox while ((m = vm_page_lookup(lobject, pindex)) == NULL && 989566526a9SAlan Cox lobject->type == OBJT_DEFAULT && 990566526a9SAlan Cox (backing_object = lobject->backing_object) != NULL) { 991566526a9SAlan Cox if (lobject->backing_object_offset & PAGE_MASK) 992566526a9SAlan Cox break; 993566526a9SAlan Cox pindex += lobject->backing_object_offset >> PAGE_SHIFT; 994566526a9SAlan Cox VM_OBJECT_LOCK(backing_object); 995566526a9SAlan Cox VM_OBJECT_UNLOCK(lobject); 996566526a9SAlan Cox lobject = backing_object; 997566526a9SAlan Cox } 998566526a9SAlan Cox /* 999566526a9SAlan Cox * give-up when a page is not in memory 1000566526a9SAlan Cox */ 1001cbfbaad8SAlan Cox if (m == NULL) { 1002cbfbaad8SAlan Cox VM_OBJECT_UNLOCK(lobject); 1003566526a9SAlan Cox break; 1004cbfbaad8SAlan Cox } 1005566526a9SAlan Cox if (((m->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL) && 1006566526a9SAlan Cox (m->busy == 0) && 1007566526a9SAlan Cox (m->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) { 1008566526a9SAlan Cox 1009566526a9SAlan Cox vm_page_lock_queues(); 1010ef39c05bSAlexander Leidinger if (VM_PAGE_INQUEUE1(m, PQ_CACHE)) 101185f5b245SAlan Cox vm_page_deactivate(m); 1012ba8bca61SAlan Cox mpte = pmap_enter_quick(pmap, addr, m, 1013ba8bca61SAlan Cox entry->protection, mpte); 10141f70d622SAlan Cox vm_page_unlock_queues(); 101585f5b245SAlan Cox } 1016cbfbaad8SAlan Cox VM_OBJECT_UNLOCK(lobject); 1017566526a9SAlan Cox } 1018566526a9SAlan Cox } 1019566526a9SAlan Cox 1020566526a9SAlan Cox /* 1021e3669ceeSMatthew Dillon * vm_fault_quick: 1022e3669ceeSMatthew Dillon * 1023e3669ceeSMatthew Dillon * Ensure that the requested virtual address, which may be in userland, 1024e3669ceeSMatthew Dillon * is valid. Fault-in the page if necessary. Return -1 on failure. 1025e3669ceeSMatthew Dillon */ 1026e3669ceeSMatthew Dillon int 1027e3669ceeSMatthew Dillon vm_fault_quick(caddr_t v, int prot) 1028e3669ceeSMatthew Dillon { 1029e3669ceeSMatthew Dillon int r; 1030e3669ceeSMatthew Dillon 1031e3669ceeSMatthew Dillon if (prot & VM_PROT_WRITE) 1032e3669ceeSMatthew Dillon r = subyte(v, fubyte(v)); 1033e3669ceeSMatthew Dillon else 1034e3669ceeSMatthew Dillon r = fubyte(v); 1035e3669ceeSMatthew Dillon return(r); 1036e3669ceeSMatthew Dillon } 1037e3669ceeSMatthew Dillon 1038e3669ceeSMatthew Dillon /* 1039df8bae1dSRodney W. Grimes * vm_fault_wire: 1040df8bae1dSRodney W. Grimes * 1041df8bae1dSRodney W. Grimes * Wire down a range of virtual addresses in a map. 1042df8bae1dSRodney W. Grimes */ 1043df8bae1dSRodney W. Grimes int 10444be14af9SAlan Cox vm_fault_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 10454be14af9SAlan Cox boolean_t user_wire, boolean_t fictitious) 1046df8bae1dSRodney W. Grimes { 104754d92145SMatthew Dillon vm_offset_t va; 1048df8bae1dSRodney W. Grimes int rv; 1049df8bae1dSRodney W. Grimes 1050df8bae1dSRodney W. Grimes /* 10510d94caffSDavid Greenman * We simulate a fault to get the page and enter it in the physical 1052ef594d31SAlan Cox * map. For user wiring, we only ask for read access on currently 1053ef594d31SAlan Cox * read-only sections. 1054df8bae1dSRodney W. Grimes */ 1055df8bae1dSRodney W. Grimes for (va = start; va < end; va += PAGE_SIZE) { 1056ef594d31SAlan Cox rv = vm_fault(map, va, 1057ef594d31SAlan Cox user_wire ? VM_PROT_READ : VM_PROT_READ | VM_PROT_WRITE, 1058ef594d31SAlan Cox user_wire ? VM_FAULT_USER_WIRE : VM_FAULT_CHANGE_WIRING); 10597aaaa4fdSJohn Dyson if (rv) { 10607aaaa4fdSJohn Dyson if (va != start) 10614be14af9SAlan Cox vm_fault_unwire(map, start, va, fictitious); 10627aaaa4fdSJohn Dyson return (rv); 10637aaaa4fdSJohn Dyson } 10647aaaa4fdSJohn Dyson } 10657aaaa4fdSJohn Dyson return (KERN_SUCCESS); 10667aaaa4fdSJohn Dyson } 10677aaaa4fdSJohn Dyson 10687aaaa4fdSJohn Dyson /* 1069df8bae1dSRodney W. Grimes * vm_fault_unwire: 1070df8bae1dSRodney W. Grimes * 1071df8bae1dSRodney W. Grimes * Unwire a range of virtual addresses in a map. 1072df8bae1dSRodney W. Grimes */ 107326f9a767SRodney W. Grimes void 10744be14af9SAlan Cox vm_fault_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 10754be14af9SAlan Cox boolean_t fictitious) 1076df8bae1dSRodney W. Grimes { 1077227f9a1cSJake Burkholder vm_paddr_t pa; 1078227f9a1cSJake Burkholder vm_offset_t va; 107954d92145SMatthew Dillon pmap_t pmap; 1080df8bae1dSRodney W. Grimes 1081df8bae1dSRodney W. Grimes pmap = vm_map_pmap(map); 1082df8bae1dSRodney W. Grimes 1083df8bae1dSRodney W. Grimes /* 10840d94caffSDavid Greenman * Since the pages are wired down, we must be able to get their 10850d94caffSDavid Greenman * mappings from the physical map system. 1086df8bae1dSRodney W. Grimes */ 1087df8bae1dSRodney W. Grimes for (va = start; va < end; va += PAGE_SIZE) { 1088df8bae1dSRodney W. Grimes pa = pmap_extract(pmap, va); 1089227f9a1cSJake Burkholder if (pa != 0) { 1090df8bae1dSRodney W. Grimes pmap_change_wiring(pmap, va, FALSE); 10914be14af9SAlan Cox if (!fictitious) { 10922d09a6adSAlan Cox vm_page_lock_queues(); 109373007561SDavid Greenman vm_page_unwire(PHYS_TO_VM_PAGE(pa), 1); 10942d09a6adSAlan Cox vm_page_unlock_queues(); 1095df8bae1dSRodney W. Grimes } 1096b18bfc3dSJohn Dyson } 10974be14af9SAlan Cox } 1098df8bae1dSRodney W. Grimes } 1099df8bae1dSRodney W. Grimes 1100df8bae1dSRodney W. Grimes /* 1101df8bae1dSRodney W. Grimes * Routine: 1102df8bae1dSRodney W. Grimes * vm_fault_copy_entry 1103df8bae1dSRodney W. Grimes * Function: 1104df8bae1dSRodney W. Grimes * Copy all of the pages from a wired-down map entry to another. 1105df8bae1dSRodney W. Grimes * 1106df8bae1dSRodney W. Grimes * In/out conditions: 1107df8bae1dSRodney W. Grimes * The source and destination maps must be locked for write. 1108df8bae1dSRodney W. Grimes * The source map entry must be wired down (or be a sharing map 1109df8bae1dSRodney W. Grimes * entry corresponding to a main map entry that is wired down). 1110df8bae1dSRodney W. Grimes */ 111126f9a767SRodney W. Grimes void 111226f9a767SRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry) 1113df8bae1dSRodney W. Grimes vm_map_t dst_map; 1114df8bae1dSRodney W. Grimes vm_map_t src_map; 1115df8bae1dSRodney W. Grimes vm_map_entry_t dst_entry; 1116df8bae1dSRodney W. Grimes vm_map_entry_t src_entry; 1117df8bae1dSRodney W. Grimes { 1118c5b65a67SAlan Cox vm_object_t backing_object, dst_object, object; 1119df8bae1dSRodney W. Grimes vm_object_t src_object; 1120a316d390SJohn Dyson vm_ooffset_t dst_offset; 1121a316d390SJohn Dyson vm_ooffset_t src_offset; 1122c5b65a67SAlan Cox vm_pindex_t pindex; 1123df8bae1dSRodney W. Grimes vm_prot_t prot; 1124df8bae1dSRodney W. Grimes vm_offset_t vaddr; 1125df8bae1dSRodney W. Grimes vm_page_t dst_m; 1126df8bae1dSRodney W. Grimes vm_page_t src_m; 1127df8bae1dSRodney W. Grimes 1128df8bae1dSRodney W. Grimes #ifdef lint 1129df8bae1dSRodney W. Grimes src_map++; 11300d94caffSDavid Greenman #endif /* lint */ 1131df8bae1dSRodney W. Grimes 1132df8bae1dSRodney W. Grimes src_object = src_entry->object.vm_object; 1133df8bae1dSRodney W. Grimes src_offset = src_entry->offset; 1134df8bae1dSRodney W. Grimes 1135df8bae1dSRodney W. Grimes /* 11360d94caffSDavid Greenman * Create the top-level object for the destination entry. (Doesn't 11370d94caffSDavid Greenman * actually shadow anything - we copy the pages directly.) 1138df8bae1dSRodney W. Grimes */ 113924a1cce3SDavid Greenman dst_object = vm_object_allocate(OBJT_DEFAULT, 114057b5187bSAlan Cox OFF_TO_IDX(dst_entry->end - dst_entry->start)); 1141df8bae1dSRodney W. Grimes 11428afcf0ccSAlan Cox VM_OBJECT_LOCK(dst_object); 1143df8bae1dSRodney W. Grimes dst_entry->object.vm_object = dst_object; 1144df8bae1dSRodney W. Grimes dst_entry->offset = 0; 1145df8bae1dSRodney W. Grimes 1146df8bae1dSRodney W. Grimes prot = dst_entry->max_protection; 1147df8bae1dSRodney W. Grimes 1148df8bae1dSRodney W. Grimes /* 11490d94caffSDavid Greenman * Loop through all of the pages in the entry's range, copying each 11500d94caffSDavid Greenman * one from the source object (it should be there) to the destination 11510d94caffSDavid Greenman * object. 1152df8bae1dSRodney W. Grimes */ 1153df8bae1dSRodney W. Grimes for (vaddr = dst_entry->start, dst_offset = 0; 1154df8bae1dSRodney W. Grimes vaddr < dst_entry->end; 1155df8bae1dSRodney W. Grimes vaddr += PAGE_SIZE, dst_offset += PAGE_SIZE) { 1156df8bae1dSRodney W. Grimes 1157df8bae1dSRodney W. Grimes /* 1158df8bae1dSRodney W. Grimes * Allocate a page in the destination object 1159df8bae1dSRodney W. Grimes */ 1160df8bae1dSRodney W. Grimes do { 1161a316d390SJohn Dyson dst_m = vm_page_alloc(dst_object, 1162a316d390SJohn Dyson OFF_TO_IDX(dst_offset), VM_ALLOC_NORMAL); 1163df8bae1dSRodney W. Grimes if (dst_m == NULL) { 11648afcf0ccSAlan Cox VM_OBJECT_UNLOCK(dst_object); 1165df8bae1dSRodney W. Grimes VM_WAIT; 11668afcf0ccSAlan Cox VM_OBJECT_LOCK(dst_object); 1167df8bae1dSRodney W. Grimes } 1168df8bae1dSRodney W. Grimes } while (dst_m == NULL); 1169df8bae1dSRodney W. Grimes 1170df8bae1dSRodney W. Grimes /* 1171df8bae1dSRodney W. Grimes * Find the page in the source object, and copy it in. 11720d94caffSDavid Greenman * (Because the source is wired down, the page will be in 11730d94caffSDavid Greenman * memory.) 1174df8bae1dSRodney W. Grimes */ 1175f29ba63eSAlan Cox VM_OBJECT_LOCK(src_object); 1176c5b65a67SAlan Cox object = src_object; 1177c5b65a67SAlan Cox pindex = 0; 1178c5b65a67SAlan Cox while ((src_m = vm_page_lookup(object, pindex + 1179c5b65a67SAlan Cox OFF_TO_IDX(dst_offset + src_offset))) == NULL && 1180c5b65a67SAlan Cox (src_entry->protection & VM_PROT_WRITE) == 0 && 1181c5b65a67SAlan Cox (backing_object = object->backing_object) != NULL) { 1182c5b65a67SAlan Cox /* 1183c5b65a67SAlan Cox * Allow fallback to backing objects if we are reading. 1184c5b65a67SAlan Cox */ 1185c5b65a67SAlan Cox VM_OBJECT_LOCK(backing_object); 1186c5b65a67SAlan Cox pindex += OFF_TO_IDX(object->backing_object_offset); 1187c5b65a67SAlan Cox VM_OBJECT_UNLOCK(object); 1188c5b65a67SAlan Cox object = backing_object; 1189c5b65a67SAlan Cox } 1190df8bae1dSRodney W. Grimes if (src_m == NULL) 1191df8bae1dSRodney W. Grimes panic("vm_fault_copy_wired: page missing"); 1192669890eaSAlan Cox pmap_copy_page(src_m, dst_m); 1193c5b65a67SAlan Cox VM_OBJECT_UNLOCK(object); 1194669890eaSAlan Cox dst_m->valid = VM_PAGE_BITS_ALL; 11958afcf0ccSAlan Cox VM_OBJECT_UNLOCK(dst_object); 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes /* 1198df8bae1dSRodney W. Grimes * Enter it in the pmap... 1199df8bae1dSRodney W. Grimes */ 12000385347cSPeter Wemm pmap_enter(dst_map->pmap, vaddr, dst_m, prot, FALSE); 12018afcf0ccSAlan Cox VM_OBJECT_LOCK(dst_object); 12024abd55b2SAlan Cox vm_page_lock_queues(); 1203c5b65a67SAlan Cox if ((prot & VM_PROT_WRITE) != 0) 1204db44450bSAlan Cox vm_page_flag_set(dst_m, PG_WRITEABLE); 1205df8bae1dSRodney W. Grimes 1206df8bae1dSRodney W. Grimes /* 1207df8bae1dSRodney W. Grimes * Mark it no longer busy, and put it on the active list. 1208df8bae1dSRodney W. Grimes */ 1209df8bae1dSRodney W. Grimes vm_page_activate(dst_m); 1210e69763a3SDoug Rabson vm_page_wakeup(dst_m); 12114abd55b2SAlan Cox vm_page_unlock_queues(); 1212df8bae1dSRodney W. Grimes } 12138afcf0ccSAlan Cox VM_OBJECT_UNLOCK(dst_object); 1214df8bae1dSRodney W. Grimes } 121526f9a767SRodney W. Grimes 121626f9a767SRodney W. Grimes 121726f9a767SRodney W. Grimes /* 121826f9a767SRodney W. Grimes * This routine checks around the requested page for other pages that 121922ba64e8SJohn Dyson * might be able to be faulted in. This routine brackets the viable 122022ba64e8SJohn Dyson * pages for the pages to be paged in. 122126f9a767SRodney W. Grimes * 122226f9a767SRodney W. Grimes * Inputs: 122322ba64e8SJohn Dyson * m, rbehind, rahead 122426f9a767SRodney W. Grimes * 122526f9a767SRodney W. Grimes * Outputs: 122626f9a767SRodney W. Grimes * marray (array of vm_page_t), reqpage (index of requested page) 122726f9a767SRodney W. Grimes * 122826f9a767SRodney W. Grimes * Return value: 122926f9a767SRodney W. Grimes * number of pages in marray 123023955314SAlfred Perlstein * 123123955314SAlfred Perlstein * This routine can't block. 123226f9a767SRodney W. Grimes */ 1233303b270bSEivind Eklund static int 123422ba64e8SJohn Dyson vm_fault_additional_pages(m, rbehind, rahead, marray, reqpage) 123526f9a767SRodney W. Grimes vm_page_t m; 123626f9a767SRodney W. Grimes int rbehind; 123722ba64e8SJohn Dyson int rahead; 123826f9a767SRodney W. Grimes vm_page_t *marray; 123926f9a767SRodney W. Grimes int *reqpage; 124026f9a767SRodney W. Grimes { 12412d8acc0fSJohn Dyson int i,j; 124226f9a767SRodney W. Grimes vm_object_t object; 1243a316d390SJohn Dyson vm_pindex_t pindex, startpindex, endpindex, tpindex; 124426f9a767SRodney W. Grimes vm_page_t rtm; 1245170db9c6SJohn Dyson int cbehind, cahead; 124626f9a767SRodney W. Grimes 1247f29ba63eSAlan Cox VM_OBJECT_LOCK_ASSERT(m->object, MA_OWNED); 124823955314SAlfred Perlstein 124926f9a767SRodney W. Grimes object = m->object; 1250a316d390SJohn Dyson pindex = m->pindex; 125126f9a767SRodney W. Grimes 125226f9a767SRodney W. Grimes /* 1253f35329acSJohn Dyson * we don't fault-ahead for device pager 1254f35329acSJohn Dyson */ 1255f35329acSJohn Dyson if (object->type == OBJT_DEVICE) { 1256f35329acSJohn Dyson *reqpage = 0; 1257f35329acSJohn Dyson marray[0] = m; 1258f35329acSJohn Dyson return 1; 1259f35329acSJohn Dyson } 1260f35329acSJohn Dyson 1261f35329acSJohn Dyson /* 126226f9a767SRodney W. Grimes * if the requested page is not available, then give up now 126326f9a767SRodney W. Grimes */ 12641c7c3c6aSMatthew Dillon if (!vm_pager_has_page(object, pindex, &cbehind, &cahead)) { 126526f9a767SRodney W. Grimes return 0; 12662d8acc0fSJohn Dyson } 126726f9a767SRodney W. Grimes 126822ba64e8SJohn Dyson if ((cbehind == 0) && (cahead == 0)) { 126922ba64e8SJohn Dyson *reqpage = 0; 127022ba64e8SJohn Dyson marray[0] = m; 127122ba64e8SJohn Dyson return 1; 1272170db9c6SJohn Dyson } 127322ba64e8SJohn Dyson 127422ba64e8SJohn Dyson if (rahead > cahead) { 127522ba64e8SJohn Dyson rahead = cahead; 127622ba64e8SJohn Dyson } 127722ba64e8SJohn Dyson 1278170db9c6SJohn Dyson if (rbehind > cbehind) { 1279170db9c6SJohn Dyson rbehind = cbehind; 1280170db9c6SJohn Dyson } 1281170db9c6SJohn Dyson 128226f9a767SRodney W. Grimes /* 128326f9a767SRodney W. Grimes * try to do any readahead that we might have free pages for. 128426f9a767SRodney W. Grimes */ 1285ccbb2f72SJohn Dyson if ((rahead + rbehind) > 128622ba64e8SJohn Dyson ((cnt.v_free_count + cnt.v_cache_count) - cnt.v_free_reserved)) { 1287f919ebdeSDavid Greenman pagedaemon_wakeup(); 128826f9a767SRodney W. Grimes marray[0] = m; 12892d8acc0fSJohn Dyson *reqpage = 0; 129026f9a767SRodney W. Grimes return 1; 129126f9a767SRodney W. Grimes } 129222ba64e8SJohn Dyson 129326f9a767SRodney W. Grimes /* 12942d8acc0fSJohn Dyson * scan backward for the read behind pages -- in memory 129526f9a767SRodney W. Grimes */ 12962d8acc0fSJohn Dyson if (pindex > 0) { 12972d8acc0fSJohn Dyson if (rbehind > pindex) { 1298a316d390SJohn Dyson rbehind = pindex; 12992d8acc0fSJohn Dyson startpindex = 0; 13002d8acc0fSJohn Dyson } else { 1301a316d390SJohn Dyson startpindex = pindex - rbehind; 13022d8acc0fSJohn Dyson } 13032d8acc0fSJohn Dyson 13048f8790a7SAlan Cox if ((rtm = TAILQ_PREV(m, pglist, listq)) != NULL && 13058f8790a7SAlan Cox rtm->pindex >= startpindex) 13068f8790a7SAlan Cox startpindex = rtm->pindex + 1; 130726f9a767SRodney W. Grimes 13082d8acc0fSJohn Dyson for (i = 0, tpindex = startpindex; tpindex < pindex; i++, tpindex++) { 130926f9a767SRodney W. Grimes 13102d8acc0fSJohn Dyson rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 1311ccbb2f72SJohn Dyson if (rtm == NULL) { 13122ad98273SAlan Cox vm_page_lock_queues(); 1313ccbb2f72SJohn Dyson for (j = 0; j < i; j++) { 13144866e085SJohn Dyson vm_page_free(marray[j]); 131526f9a767SRodney W. Grimes } 13162ad98273SAlan Cox vm_page_unlock_queues(); 131726f9a767SRodney W. Grimes marray[0] = m; 13182d8acc0fSJohn Dyson *reqpage = 0; 131926f9a767SRodney W. Grimes return 1; 132026f9a767SRodney W. Grimes } 1321170db9c6SJohn Dyson 13222d8acc0fSJohn Dyson marray[i] = rtm; 132326f9a767SRodney W. Grimes } 13242d8acc0fSJohn Dyson } else { 13252d8acc0fSJohn Dyson startpindex = 0; 13262d8acc0fSJohn Dyson i = 0; 13272d8acc0fSJohn Dyson } 13282d8acc0fSJohn Dyson 13292d8acc0fSJohn Dyson marray[i] = m; 13302d8acc0fSJohn Dyson /* page offset of the required page */ 13312d8acc0fSJohn Dyson *reqpage = i; 13322d8acc0fSJohn Dyson 13332d8acc0fSJohn Dyson tpindex = pindex + 1; 13342d8acc0fSJohn Dyson i++; 13352d8acc0fSJohn Dyson 13362d8acc0fSJohn Dyson /* 13372d8acc0fSJohn Dyson * scan forward for the read ahead pages 13382d8acc0fSJohn Dyson */ 13392d8acc0fSJohn Dyson endpindex = tpindex + rahead; 13408f8790a7SAlan Cox if ((rtm = TAILQ_NEXT(m, listq)) != NULL && rtm->pindex < endpindex) 13418f8790a7SAlan Cox endpindex = rtm->pindex; 13422d8acc0fSJohn Dyson if (endpindex > object->size) 13432d8acc0fSJohn Dyson endpindex = object->size; 13442d8acc0fSJohn Dyson 13452d8acc0fSJohn Dyson for (; tpindex < endpindex; i++, tpindex++) { 13462d8acc0fSJohn Dyson 13472d8acc0fSJohn Dyson rtm = vm_page_alloc(object, tpindex, VM_ALLOC_NORMAL); 13482d8acc0fSJohn Dyson if (rtm == NULL) { 13492d8acc0fSJohn Dyson break; 13502d8acc0fSJohn Dyson } 13512d8acc0fSJohn Dyson 13522d8acc0fSJohn Dyson marray[i] = rtm; 13532d8acc0fSJohn Dyson } 13542d8acc0fSJohn Dyson 13552d8acc0fSJohn Dyson /* return number of bytes of pages */ 13562d8acc0fSJohn Dyson return i; 135726f9a767SRodney W. Grimes } 1358