160727d8bSWarner Losh /*- 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 17df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 18df8bae1dSRodney W. Grimes * without specific prior written permission. 19df8bae1dSRodney W. Grimes * 20df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 21df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 22df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 23df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 24df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 25df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 26df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 27df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 28df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 29df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 30df8bae1dSRodney W. Grimes * SUCH DAMAGE. 31df8bae1dSRodney W. Grimes * 323c4dd356SDavid Greenman * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 33df8bae1dSRodney W. Grimes * 34df8bae1dSRodney W. Grimes * 35df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 36df8bae1dSRodney W. Grimes * All rights reserved. 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 41df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 42df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 43df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 44df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 45df8bae1dSRodney W. Grimes * 46df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 47df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 48df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 53df8bae1dSRodney W. Grimes * School of Computer Science 54df8bae1dSRodney W. Grimes * Carnegie Mellon University 55df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 56df8bae1dSRodney W. Grimes * 57df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 58df8bae1dSRodney W. Grimes * rights to redistribute these changes. 59df8bae1dSRodney W. Grimes */ 60df8bae1dSRodney W. Grimes 61df8bae1dSRodney W. Grimes /* 62df8bae1dSRodney W. Grimes * Virtual memory mapping module. 63df8bae1dSRodney W. Grimes */ 64df8bae1dSRodney W. Grimes 65874651b1SDavid E. O'Brien #include <sys/cdefs.h> 66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 67874651b1SDavid E. O'Brien 68df8bae1dSRodney W. Grimes #include <sys/param.h> 69df8bae1dSRodney W. Grimes #include <sys/systm.h> 7061d80e90SJohn Baldwin #include <sys/ktr.h> 71fb919e4dSMark Murray #include <sys/lock.h> 72fb919e4dSMark Murray #include <sys/mutex.h> 73b5e8ce9fSBruce Evans #include <sys/proc.h> 74efeaf95aSDavid Greenman #include <sys/vmmeter.h> 75867a482dSJohn Dyson #include <sys/mman.h> 761efb74fbSJohn Dyson #include <sys/vnode.h> 772267af78SJulian Elischer #include <sys/resourcevar.h> 783fde38dfSMike Silbersack #include <sys/file.h> 7905ba50f5SJake Burkholder #include <sys/sysent.h> 803db161e0SMatthew Dillon #include <sys/shm.h> 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes #include <vm/vm.h> 83efeaf95aSDavid Greenman #include <vm/vm_param.h> 84efeaf95aSDavid Greenman #include <vm/pmap.h> 85efeaf95aSDavid Greenman #include <vm/vm_map.h> 86df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 8847221757SJohn Dyson #include <vm/vm_pager.h> 8926f9a767SRodney W. Grimes #include <vm/vm_kern.h> 90efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9121cd6e62SSeigo Tanimura #include <vm/swap_pager.h> 92670d17b5SJeff Roberson #include <vm/uma.h> 93df8bae1dSRodney W. Grimes 94df8bae1dSRodney W. Grimes /* 95df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 96df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 97df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 98df8bae1dSRodney W. Grimes * memory from one map to another. 99df8bae1dSRodney W. Grimes * 100df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 101df8bae1dSRodney W. Grimes * 102df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 103e2abaaaaSAlan Cox * entries; a self-adjusting binary search tree of these 104e2abaaaaSAlan Cox * entries is used to speed up lookups. 105df8bae1dSRodney W. Grimes * 106956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 107df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 108df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 109df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 110df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 111df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 112df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 113df8bae1dSRodney W. Grimes * 114df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 115ad5fca3bSAlan Cox * by copying VM object references from one map to 116df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 117df8bae1dSRodney W. Grimes */ 118df8bae1dSRodney W. Grimes 119df8bae1dSRodney W. Grimes /* 120df8bae1dSRodney W. Grimes * vm_map_startup: 121df8bae1dSRodney W. Grimes * 122df8bae1dSRodney W. Grimes * Initialize the vm_map module. Must be called before 123df8bae1dSRodney W. Grimes * any other vm_map routines. 124df8bae1dSRodney W. Grimes * 125df8bae1dSRodney W. Grimes * Map and entry structures are allocated from the general 126df8bae1dSRodney W. Grimes * purpose memory pool with some exceptions: 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * - The kernel map and kmem submap are allocated statically. 129df8bae1dSRodney W. Grimes * - Kernel map entries are allocated out of a static pool. 130df8bae1dSRodney W. Grimes * 131df8bae1dSRodney W. Grimes * These restrictions are necessary since malloc() uses the 132df8bae1dSRodney W. Grimes * maps and requires map entries. 133df8bae1dSRodney W. Grimes */ 134df8bae1dSRodney W. Grimes 1353a92e5d5SAlan Cox static struct mtx map_sleep_mtx; 1368355f576SJeff Roberson static uma_zone_t mapentzone; 1378355f576SJeff Roberson static uma_zone_t kmapentzone; 1388355f576SJeff Roberson static uma_zone_t mapzone; 1398355f576SJeff Roberson static uma_zone_t vmspace_zone; 1408355f576SJeff Roberson static struct vm_object kmapentobj; 141b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags); 1428355f576SJeff Roberson static void vmspace_zfini(void *mem, int size); 143b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags); 1448355f576SJeff Roberson static void vm_map_zfini(void *mem, int size); 1458355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max); 1461fc43fd1SAlan Cox 1478355f576SJeff Roberson #ifdef INVARIANTS 1488355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg); 1498355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg); 1508355f576SJeff Roberson #endif 151b18bfc3dSJohn Dyson 15257051fdcSTor Egge /* 15357051fdcSTor Egge * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type 15457051fdcSTor Egge * stable. 15557051fdcSTor Egge */ 15657051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0) 15757051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0) 15857051fdcSTor Egge 159d239bd3cSKonstantin Belousov /* 160d239bd3cSKonstantin Belousov * VM_MAP_RANGE_CHECK: [ internal use only ] 161d239bd3cSKonstantin Belousov * 162d239bd3cSKonstantin Belousov * Asserts that the starting and ending region 163d239bd3cSKonstantin Belousov * addresses fall within the valid range of the map. 164d239bd3cSKonstantin Belousov */ 165d239bd3cSKonstantin Belousov #define VM_MAP_RANGE_CHECK(map, start, end) \ 166d239bd3cSKonstantin Belousov { \ 167d239bd3cSKonstantin Belousov if (start < vm_map_min(map)) \ 168d239bd3cSKonstantin Belousov start = vm_map_min(map); \ 169d239bd3cSKonstantin Belousov if (end > vm_map_max(map)) \ 170d239bd3cSKonstantin Belousov end = vm_map_max(map); \ 171d239bd3cSKonstantin Belousov if (start > end) \ 172d239bd3cSKonstantin Belousov start = end; \ 173d239bd3cSKonstantin Belousov } 174d239bd3cSKonstantin Belousov 1750d94caffSDavid Greenman void 1761b40f8c0SMatthew Dillon vm_map_startup(void) 177df8bae1dSRodney W. Grimes { 1783a92e5d5SAlan Cox mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF); 1798355f576SJeff Roberson mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL, 1808355f576SJeff Roberson #ifdef INVARIANTS 1818355f576SJeff Roberson vm_map_zdtor, 1828355f576SJeff Roberson #else 1838355f576SJeff Roberson NULL, 1848355f576SJeff Roberson #endif 1858355f576SJeff Roberson vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1868355f576SJeff Roberson uma_prealloc(mapzone, MAX_KMAP); 187670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry), 18818aa2de5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 18918aa2de5SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZONE_VM); 190670d17b5SJeff Roberson uma_prealloc(kmapentzone, MAX_KMAPENT); 191670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry), 192670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 193df8bae1dSRodney W. Grimes } 194df8bae1dSRodney W. Grimes 1958355f576SJeff Roberson static void 1968355f576SJeff Roberson vmspace_zfini(void *mem, int size) 1978355f576SJeff Roberson { 1988355f576SJeff Roberson struct vmspace *vm; 1998355f576SJeff Roberson 2008355f576SJeff Roberson vm = (struct vmspace *)mem; 2018355f576SJeff Roberson vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map)); 2028355f576SJeff Roberson } 2038355f576SJeff Roberson 204b23f72e9SBrian Feldman static int 205b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags) 2068355f576SJeff Roberson { 2078355f576SJeff Roberson struct vmspace *vm; 2088355f576SJeff Roberson 2098355f576SJeff Roberson vm = (struct vmspace *)mem; 2108355f576SJeff Roberson 21189b57fcfSKonstantin Belousov vm->vm_map.pmap = NULL; 212b23f72e9SBrian Feldman (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags); 213b23f72e9SBrian Feldman return (0); 2148355f576SJeff Roberson } 2158355f576SJeff Roberson 2168355f576SJeff Roberson static void 2178355f576SJeff Roberson vm_map_zfini(void *mem, int size) 2188355f576SJeff Roberson { 2198355f576SJeff Roberson vm_map_t map; 2208355f576SJeff Roberson 2218355f576SJeff Roberson map = (vm_map_t)mem; 22236daaecdSAlan Cox mtx_destroy(&map->system_mtx); 22312c64974SMaxime Henrion sx_destroy(&map->lock); 2248355f576SJeff Roberson } 2258355f576SJeff Roberson 226b23f72e9SBrian Feldman static int 227b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags) 2288355f576SJeff Roberson { 2298355f576SJeff Roberson vm_map_t map; 2308355f576SJeff Roberson 2318355f576SJeff Roberson map = (vm_map_t)mem; 2328355f576SJeff Roberson map->nentries = 0; 2338355f576SJeff Roberson map->size = 0; 234d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 23512c64974SMaxime Henrion sx_init(&map->lock, "user map"); 236b23f72e9SBrian Feldman return (0); 2378355f576SJeff Roberson } 2388355f576SJeff Roberson 2398355f576SJeff Roberson #ifdef INVARIANTS 2408355f576SJeff Roberson static void 2418355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg) 2428355f576SJeff Roberson { 2438355f576SJeff Roberson struct vmspace *vm; 2448355f576SJeff Roberson 2458355f576SJeff Roberson vm = (struct vmspace *)mem; 2468355f576SJeff Roberson 2478355f576SJeff Roberson vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg); 2488355f576SJeff Roberson } 2498355f576SJeff Roberson static void 2508355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg) 2518355f576SJeff Roberson { 2528355f576SJeff Roberson vm_map_t map; 2538355f576SJeff Roberson 2548355f576SJeff Roberson map = (vm_map_t)mem; 2558355f576SJeff Roberson KASSERT(map->nentries == 0, 2568355f576SJeff Roberson ("map %p nentries == %d on free.", 2578355f576SJeff Roberson map, map->nentries)); 2588355f576SJeff Roberson KASSERT(map->size == 0, 2598355f576SJeff Roberson ("map %p size == %lu on free.", 2609eb6e519SJeff Roberson map, (unsigned long)map->size)); 2618355f576SJeff Roberson } 2628355f576SJeff Roberson #endif /* INVARIANTS */ 2638355f576SJeff Roberson 264df8bae1dSRodney W. Grimes /* 265df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 266df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 267df8bae1dSRodney W. Grimes */ 268df8bae1dSRodney W. Grimes struct vmspace * 2692d8acc0fSJohn Dyson vmspace_alloc(min, max) 270df8bae1dSRodney W. Grimes vm_offset_t min, max; 271df8bae1dSRodney W. Grimes { 272c0877f10SJohn Dyson struct vmspace *vm; 2730d94caffSDavid Greenman 274a163d034SWarner Losh vm = uma_zalloc(vmspace_zone, M_WAITOK); 27589b57fcfSKonstantin Belousov if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) { 27689b57fcfSKonstantin Belousov uma_zfree(vmspace_zone, vm); 27789b57fcfSKonstantin Belousov return (NULL); 27889b57fcfSKonstantin Belousov } 27921c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_alloc: %p", vm); 2808355f576SJeff Roberson _vm_map_init(&vm->vm_map, min, max); 281b1028ad1SLuoqi Chen vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 282df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 2832d8acc0fSJohn Dyson vm->vm_shm = NULL; 28451ab6c28SAlan Cox vm->vm_swrss = 0; 28551ab6c28SAlan Cox vm->vm_tsize = 0; 28651ab6c28SAlan Cox vm->vm_dsize = 0; 28751ab6c28SAlan Cox vm->vm_ssize = 0; 28851ab6c28SAlan Cox vm->vm_taddr = 0; 28951ab6c28SAlan Cox vm->vm_daddr = 0; 29051ab6c28SAlan Cox vm->vm_maxsaddr = 0; 291df8bae1dSRodney W. Grimes return (vm); 292df8bae1dSRodney W. Grimes } 293df8bae1dSRodney W. Grimes 294df8bae1dSRodney W. Grimes void 2951b40f8c0SMatthew Dillon vm_init2(void) 2961b40f8c0SMatthew Dillon { 2972feb50bfSAttilio Rao uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count, 298c1f02198SAlan Cox (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 + 2993fde38dfSMike Silbersack maxproc * 2 + maxfiles); 3008355f576SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL, 3018355f576SJeff Roberson #ifdef INVARIANTS 3028355f576SJeff Roberson vmspace_zdtor, 3038355f576SJeff Roberson #else 3048355f576SJeff Roberson NULL, 3058355f576SJeff Roberson #endif 3068355f576SJeff Roberson vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 3073075778bSJohn Dyson } 3083075778bSJohn Dyson 30962a59e8fSWarner Losh static inline void 310582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm) 311df8bae1dSRodney W. Grimes { 31221c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_free: %p", vm); 3133db161e0SMatthew Dillon 3143db161e0SMatthew Dillon /* 3153db161e0SMatthew Dillon * Make sure any SysV shm is freed, it might not have been in 3163db161e0SMatthew Dillon * exit1(). 3173db161e0SMatthew Dillon */ 3183db161e0SMatthew Dillon shmexit(vm); 3193db161e0SMatthew Dillon 32030dcfc09SJohn Dyson /* 321df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 3220d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 3230d94caffSDavid Greenman * the pmap module to reclaim anything left. 324df8bae1dSRodney W. Grimes */ 325717f7d59SAlan Cox (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset, 326df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 3278355f576SJeff Roberson 32889b57fcfSKonstantin Belousov /* 32989b57fcfSKonstantin Belousov * XXX Comment out the pmap_release call for now. The 33089b57fcfSKonstantin Belousov * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause 33189b57fcfSKonstantin Belousov * pmap.resident_count to be != 0 on exit sometimes. 33289b57fcfSKonstantin Belousov */ 33389b57fcfSKonstantin Belousov /* pmap_release(vmspace_pmap(vm)); */ 3348355f576SJeff Roberson uma_zfree(vmspace_zone, vm); 335df8bae1dSRodney W. Grimes } 336582ec34cSAlfred Perlstein 337582ec34cSAlfred Perlstein void 338582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm) 339582ec34cSAlfred Perlstein { 3401a276a3fSAlan Cox int refcnt; 341582ec34cSAlfred Perlstein 342582ec34cSAlfred Perlstein if (vm->vm_refcnt == 0) 343582ec34cSAlfred Perlstein panic("vmspace_free: attempt to free already freed vmspace"); 344582ec34cSAlfred Perlstein 3451a276a3fSAlan Cox do 3461a276a3fSAlan Cox refcnt = vm->vm_refcnt; 3471a276a3fSAlan Cox while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 34857051fdcSTor Egge if (refcnt == 1) 349582ec34cSAlfred Perlstein vmspace_dofree(vm); 350582ec34cSAlfred Perlstein } 351582ec34cSAlfred Perlstein 352582ec34cSAlfred Perlstein void 353582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p) 354582ec34cSAlfred Perlstein { 355334f7061SPeter Wemm struct vmspace *vm; 356582ec34cSAlfred Perlstein 35757051fdcSTor Egge PROC_VMSPACE_LOCK(p); 358334f7061SPeter Wemm vm = p->p_vmspace; 359334f7061SPeter Wemm p->p_vmspace = NULL; 36057051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 36157051fdcSTor Egge KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace")); 36257051fdcSTor Egge vmspace_free(vm); 36357051fdcSTor Egge } 36457051fdcSTor Egge 36557051fdcSTor Egge void 36657051fdcSTor Egge vmspace_exit(struct thread *td) 36757051fdcSTor Egge { 36857051fdcSTor Egge int refcnt; 36957051fdcSTor Egge struct vmspace *vm; 37057051fdcSTor Egge struct proc *p; 371389d2b6eSMatthew Dillon 372389d2b6eSMatthew Dillon /* 37357051fdcSTor Egge * Release user portion of address space. 37457051fdcSTor Egge * This releases references to vnodes, 37557051fdcSTor Egge * which could cause I/O if the file has been unlinked. 37657051fdcSTor Egge * Need to do this early enough that we can still sleep. 377389d2b6eSMatthew Dillon * 37857051fdcSTor Egge * The last exiting process to reach this point releases as 37957051fdcSTor Egge * much of the environment as it can. vmspace_dofree() is the 38057051fdcSTor Egge * slower fallback in case another process had a temporary 38157051fdcSTor Egge * reference to the vmspace. 382389d2b6eSMatthew Dillon */ 38357051fdcSTor Egge 38457051fdcSTor Egge p = td->td_proc; 38557051fdcSTor Egge vm = p->p_vmspace; 38657051fdcSTor Egge atomic_add_int(&vmspace0.vm_refcnt, 1); 38757051fdcSTor Egge do { 38857051fdcSTor Egge refcnt = vm->vm_refcnt; 38957051fdcSTor Egge if (refcnt > 1 && p->p_vmspace != &vmspace0) { 39057051fdcSTor Egge /* Switch now since other proc might free vmspace */ 39157051fdcSTor Egge PROC_VMSPACE_LOCK(p); 39257051fdcSTor Egge p->p_vmspace = &vmspace0; 39357051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 39457051fdcSTor Egge pmap_activate(td); 39557051fdcSTor Egge } 39657051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 39757051fdcSTor Egge if (refcnt == 1) { 39857051fdcSTor Egge if (p->p_vmspace != vm) { 39957051fdcSTor Egge /* vmspace not yet freed, switch back */ 40057051fdcSTor Egge PROC_VMSPACE_LOCK(p); 40157051fdcSTor Egge p->p_vmspace = vm; 40257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 40357051fdcSTor Egge pmap_activate(td); 40457051fdcSTor Egge } 40557051fdcSTor Egge pmap_remove_pages(vmspace_pmap(vm)); 40657051fdcSTor Egge /* Switch now since this proc will free vmspace */ 40757051fdcSTor Egge PROC_VMSPACE_LOCK(p); 40857051fdcSTor Egge p->p_vmspace = &vmspace0; 40957051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 41057051fdcSTor Egge pmap_activate(td); 411334f7061SPeter Wemm vmspace_dofree(vm); 412334f7061SPeter Wemm } 41357051fdcSTor Egge } 41457051fdcSTor Egge 41557051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */ 41657051fdcSTor Egge 41757051fdcSTor Egge struct vmspace * 41857051fdcSTor Egge vmspace_acquire_ref(struct proc *p) 41957051fdcSTor Egge { 42057051fdcSTor Egge struct vmspace *vm; 42157051fdcSTor Egge int refcnt; 42257051fdcSTor Egge 42357051fdcSTor Egge PROC_VMSPACE_LOCK(p); 42457051fdcSTor Egge vm = p->p_vmspace; 42557051fdcSTor Egge if (vm == NULL) { 42657051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 42757051fdcSTor Egge return (NULL); 42857051fdcSTor Egge } 42957051fdcSTor Egge do { 43057051fdcSTor Egge refcnt = vm->vm_refcnt; 43157051fdcSTor Egge if (refcnt <= 0) { /* Avoid 0->1 transition */ 43257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 43357051fdcSTor Egge return (NULL); 43457051fdcSTor Egge } 43557051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1)); 43657051fdcSTor Egge if (vm != p->p_vmspace) { 43757051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 43857051fdcSTor Egge vmspace_free(vm); 43957051fdcSTor Egge return (NULL); 44057051fdcSTor Egge } 44157051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 44257051fdcSTor Egge return (vm); 44357051fdcSTor Egge } 444df8bae1dSRodney W. Grimes 4451b40f8c0SMatthew Dillon void 446780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line) 4471b40f8c0SMatthew Dillon { 448bc91c510SAlan Cox 44993bc4879SAlan Cox if (map->system_map) 45036daaecdSAlan Cox _mtx_lock_flags(&map->system_mtx, 0, file, line); 45112c64974SMaxime Henrion else 452f9819486SAttilio Rao (void)_sx_xlock(&map->lock, 0, file, line); 4531b40f8c0SMatthew Dillon map->timestamp++; 4541b40f8c0SMatthew Dillon } 4551b40f8c0SMatthew Dillon 4561b40f8c0SMatthew Dillon void 457780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line) 4580e0af8ecSBrian Feldman { 459bc91c510SAlan Cox 46036daaecdSAlan Cox if (map->system_map) 46136daaecdSAlan Cox _mtx_unlock_flags(&map->system_mtx, 0, file, line); 46236daaecdSAlan Cox else 46312c64974SMaxime Henrion _sx_xunlock(&map->lock, file, line); 4640e0af8ecSBrian Feldman } 4650e0af8ecSBrian Feldman 4660e0af8ecSBrian Feldman void 467780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line) 4680e0af8ecSBrian Feldman { 469bc91c510SAlan Cox 47093bc4879SAlan Cox if (map->system_map) 47136daaecdSAlan Cox _mtx_lock_flags(&map->system_mtx, 0, file, line); 47212c64974SMaxime Henrion else 47305a8c414SAlan Cox (void)_sx_slock(&map->lock, 0, file, line); 47436daaecdSAlan Cox } 4750e0af8ecSBrian Feldman 4760e0af8ecSBrian Feldman void 477780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line) 4780e0af8ecSBrian Feldman { 479bc91c510SAlan Cox 48036daaecdSAlan Cox if (map->system_map) 48136daaecdSAlan Cox _mtx_unlock_flags(&map->system_mtx, 0, file, line); 48236daaecdSAlan Cox else 48305a8c414SAlan Cox _sx_sunlock(&map->lock, file, line); 48425adb370SBrian Feldman } 48525adb370SBrian Feldman 486d974f03cSAlan Cox int 487780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line) 488d974f03cSAlan Cox { 48925adb370SBrian Feldman int error; 49025adb370SBrian Feldman 49136daaecdSAlan Cox error = map->system_map ? 49236daaecdSAlan Cox !_mtx_trylock(&map->system_mtx, 0, file, line) : 49312c64974SMaxime Henrion !_sx_try_xlock(&map->lock, file, line); 4943a92e5d5SAlan Cox if (error == 0) 4953a92e5d5SAlan Cox map->timestamp++; 496bc91c510SAlan Cox return (error == 0); 4970e0af8ecSBrian Feldman } 4980e0af8ecSBrian Feldman 4990e0af8ecSBrian Feldman int 50072d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line) 50172d97679SDavid Schultz { 50272d97679SDavid Schultz int error; 50372d97679SDavid Schultz 50472d97679SDavid Schultz error = map->system_map ? 50572d97679SDavid Schultz !_mtx_trylock(&map->system_mtx, 0, file, line) : 50605a8c414SAlan Cox !_sx_try_slock(&map->lock, file, line); 50772d97679SDavid Schultz return (error == 0); 50872d97679SDavid Schultz } 50972d97679SDavid Schultz 51005a8c414SAlan Cox /* 51105a8c414SAlan Cox * _vm_map_lock_upgrade: [ internal use only ] 51205a8c414SAlan Cox * 51305a8c414SAlan Cox * Tries to upgrade a read (shared) lock on the specified map to a write 51405a8c414SAlan Cox * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a 51505a8c414SAlan Cox * non-zero value if the upgrade fails. If the upgrade fails, the map is 51605a8c414SAlan Cox * returned without a read or write lock held. 51705a8c414SAlan Cox * 51805a8c414SAlan Cox * Requires that the map be read locked. 51905a8c414SAlan Cox */ 52072d97679SDavid Schultz int 521780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line) 5220e0af8ecSBrian Feldman { 52305a8c414SAlan Cox unsigned int last_timestamp; 524bc91c510SAlan Cox 52512c64974SMaxime Henrion if (map->system_map) { 52605a8c414SAlan Cox #ifdef INVARIANTS 52736daaecdSAlan Cox _mtx_assert(&map->system_mtx, MA_OWNED, file, line); 52812c64974SMaxime Henrion #endif 52905a8c414SAlan Cox } else { 53005a8c414SAlan Cox if (!_sx_try_upgrade(&map->lock, file, line)) { 53105a8c414SAlan Cox last_timestamp = map->timestamp; 53205a8c414SAlan Cox _sx_sunlock(&map->lock, file, line); 53305a8c414SAlan Cox /* 53405a8c414SAlan Cox * If the map's timestamp does not change while the 53505a8c414SAlan Cox * map is unlocked, then the upgrade succeeds. 53605a8c414SAlan Cox */ 53705a8c414SAlan Cox (void)_sx_xlock(&map->lock, 0, file, line); 53805a8c414SAlan Cox if (last_timestamp != map->timestamp) { 53905a8c414SAlan Cox _sx_xunlock(&map->lock, file, line); 54005a8c414SAlan Cox return (1); 54105a8c414SAlan Cox } 54205a8c414SAlan Cox } 54305a8c414SAlan Cox } 544bc91c510SAlan Cox map->timestamp++; 545bc91c510SAlan Cox return (0); 5460e0af8ecSBrian Feldman } 5470e0af8ecSBrian Feldman 5480e0af8ecSBrian Feldman void 549780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line) 5501b40f8c0SMatthew Dillon { 551bc91c510SAlan Cox 55212c64974SMaxime Henrion if (map->system_map) { 55305a8c414SAlan Cox #ifdef INVARIANTS 55436daaecdSAlan Cox _mtx_assert(&map->system_mtx, MA_OWNED, file, line); 55512c64974SMaxime Henrion #endif 55605a8c414SAlan Cox } else 55705a8c414SAlan Cox _sx_downgrade(&map->lock, file, line); 55805a8c414SAlan Cox } 55905a8c414SAlan Cox 56005a8c414SAlan Cox /* 56105a8c414SAlan Cox * vm_map_locked: 56205a8c414SAlan Cox * 56305a8c414SAlan Cox * Returns a non-zero value if the caller holds a write (exclusive) lock 56405a8c414SAlan Cox * on the specified map and the value "0" otherwise. 56505a8c414SAlan Cox */ 56605a8c414SAlan Cox int 56705a8c414SAlan Cox vm_map_locked(vm_map_t map) 56805a8c414SAlan Cox { 56905a8c414SAlan Cox 57005a8c414SAlan Cox if (map->system_map) 57105a8c414SAlan Cox return (mtx_owned(&map->system_mtx)); 57205a8c414SAlan Cox else 57305a8c414SAlan Cox return (sx_xlocked(&map->lock)); 57425adb370SBrian Feldman } 57525adb370SBrian Feldman 576acd9a301SAlan Cox /* 577acd9a301SAlan Cox * vm_map_unlock_and_wait: 578acd9a301SAlan Cox */ 5799688f931SAlan Cox int 5808ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(vm_map_t map, int timo) 581acd9a301SAlan Cox { 582acd9a301SAlan Cox 5833a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 584acd9a301SAlan Cox vm_map_unlock(map); 5858ce2d00aSPawel Jakub Dawidek return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", timo)); 586acd9a301SAlan Cox } 587acd9a301SAlan Cox 588acd9a301SAlan Cox /* 589acd9a301SAlan Cox * vm_map_wakeup: 590acd9a301SAlan Cox */ 5919688f931SAlan Cox void 592acd9a301SAlan Cox vm_map_wakeup(vm_map_t map) 593acd9a301SAlan Cox { 594acd9a301SAlan Cox 595b49ecb86SAlan Cox /* 5963a92e5d5SAlan Cox * Acquire and release map_sleep_mtx to prevent a wakeup() 5973a92e5d5SAlan Cox * from being performed (and lost) between the vm_map_unlock() 5983a92e5d5SAlan Cox * and the msleep() in vm_map_unlock_and_wait(). 599b49ecb86SAlan Cox */ 6003a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 6013a92e5d5SAlan Cox mtx_unlock(&map_sleep_mtx); 602acd9a301SAlan Cox wakeup(&map->root); 603acd9a301SAlan Cox } 604acd9a301SAlan Cox 6051b40f8c0SMatthew Dillon long 6061b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace) 6071b40f8c0SMatthew Dillon { 6081b40f8c0SMatthew Dillon return pmap_resident_count(vmspace_pmap(vmspace)); 6091b40f8c0SMatthew Dillon } 6101b40f8c0SMatthew Dillon 6112bc7dd56SBruce M Simpson long 6122bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace) 6132bc7dd56SBruce M Simpson { 6142bc7dd56SBruce M Simpson return pmap_wired_count(vmspace_pmap(vmspace)); 6152bc7dd56SBruce M Simpson } 6162bc7dd56SBruce M Simpson 617ff2b5645SMatthew Dillon /* 618df8bae1dSRodney W. Grimes * vm_map_create: 619df8bae1dSRodney W. Grimes * 620df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 621df8bae1dSRodney W. Grimes * the given physical map structure, and having 622df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 623df8bae1dSRodney W. Grimes */ 6240d94caffSDavid Greenman vm_map_t 6251b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max) 626df8bae1dSRodney W. Grimes { 627c0877f10SJohn Dyson vm_map_t result; 628df8bae1dSRodney W. Grimes 629a163d034SWarner Losh result = uma_zalloc(mapzone, M_WAITOK); 63021c641b2SJohn Baldwin CTR1(KTR_VM, "vm_map_create: %p", result); 6318355f576SJeff Roberson _vm_map_init(result, min, max); 632df8bae1dSRodney W. Grimes result->pmap = pmap; 633df8bae1dSRodney W. Grimes return (result); 634df8bae1dSRodney W. Grimes } 635df8bae1dSRodney W. Grimes 636df8bae1dSRodney W. Grimes /* 637df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 638df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 639df8bae1dSRodney W. Grimes * The pmap is set elsewhere. 640df8bae1dSRodney W. Grimes */ 6418355f576SJeff Roberson static void 6428355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 643df8bae1dSRodney W. Grimes { 64421c641b2SJohn Baldwin 645df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 6469688f931SAlan Cox map->needs_wakeup = FALSE; 6473075778bSJohn Dyson map->system_map = 0; 648df8bae1dSRodney W. Grimes map->min_offset = min; 649df8bae1dSRodney W. Grimes map->max_offset = max; 650af7cd0c5SBrian Feldman map->flags = 0; 6514e94f402SAlan Cox map->root = NULL; 652df8bae1dSRodney W. Grimes map->timestamp = 0; 653df8bae1dSRodney W. Grimes } 654df8bae1dSRodney W. Grimes 655a18b1f1dSJason Evans void 6568355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 657a18b1f1dSJason Evans { 6588355f576SJeff Roberson _vm_map_init(map, min, max); 659d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 66012c64974SMaxime Henrion sx_init(&map->lock, "user map"); 661a18b1f1dSJason Evans } 662a18b1f1dSJason Evans 663df8bae1dSRodney W. Grimes /* 664b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 665b18bfc3dSJohn Dyson * 666b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 667b18bfc3dSJohn Dyson */ 66862487bb4SJohn Dyson static void 6691b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry) 670b18bfc3dSJohn Dyson { 6712b4a2c27SAlan Cox uma_zfree(map->system_map ? kmapentzone : mapentzone, entry); 672b18bfc3dSJohn Dyson } 673b18bfc3dSJohn Dyson 674b18bfc3dSJohn Dyson /* 675df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 676df8bae1dSRodney W. Grimes * 677df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 678b28cb1caSAlfred Perlstein * No entry fields are filled in. 679df8bae1dSRodney W. Grimes */ 680f708ef1bSPoul-Henning Kamp static vm_map_entry_t 6811b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map) 682df8bae1dSRodney W. Grimes { 6831f6889a1SMatthew Dillon vm_map_entry_t new_entry; 6841f6889a1SMatthew Dillon 6852b4a2c27SAlan Cox if (map->system_map) 6862b4a2c27SAlan Cox new_entry = uma_zalloc(kmapentzone, M_NOWAIT); 6872b4a2c27SAlan Cox else 688a163d034SWarner Losh new_entry = uma_zalloc(mapentzone, M_WAITOK); 6891f6889a1SMatthew Dillon if (new_entry == NULL) 6901f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 6911f6889a1SMatthew Dillon return (new_entry); 692df8bae1dSRodney W. Grimes } 693df8bae1dSRodney W. Grimes 694df8bae1dSRodney W. Grimes /* 695794316a8SAlan Cox * vm_map_entry_set_behavior: 696794316a8SAlan Cox * 697794316a8SAlan Cox * Set the expected access behavior, either normal, random, or 698794316a8SAlan Cox * sequential. 699794316a8SAlan Cox */ 70062a59e8fSWarner Losh static inline void 701794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior) 702794316a8SAlan Cox { 703794316a8SAlan Cox entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) | 704794316a8SAlan Cox (behavior & MAP_ENTRY_BEHAV_MASK); 705794316a8SAlan Cox } 706794316a8SAlan Cox 707794316a8SAlan Cox /* 7080164e057SAlan Cox * vm_map_entry_set_max_free: 7090164e057SAlan Cox * 7100164e057SAlan Cox * Set the max_free field in a vm_map_entry. 7110164e057SAlan Cox */ 71262a59e8fSWarner Losh static inline void 7130164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry) 7140164e057SAlan Cox { 7150164e057SAlan Cox 7160164e057SAlan Cox entry->max_free = entry->adj_free; 7170164e057SAlan Cox if (entry->left != NULL && entry->left->max_free > entry->max_free) 7180164e057SAlan Cox entry->max_free = entry->left->max_free; 7190164e057SAlan Cox if (entry->right != NULL && entry->right->max_free > entry->max_free) 7200164e057SAlan Cox entry->max_free = entry->right->max_free; 7210164e057SAlan Cox } 7220164e057SAlan Cox 7230164e057SAlan Cox /* 7244e94f402SAlan Cox * vm_map_entry_splay: 7254e94f402SAlan Cox * 7260164e057SAlan Cox * The Sleator and Tarjan top-down splay algorithm with the 7270164e057SAlan Cox * following variation. Max_free must be computed bottom-up, so 7280164e057SAlan Cox * on the downward pass, maintain the left and right spines in 7290164e057SAlan Cox * reverse order. Then, make a second pass up each side to fix 7300164e057SAlan Cox * the pointers and compute max_free. The time bound is O(log n) 7310164e057SAlan Cox * amortized. 7320164e057SAlan Cox * 7330164e057SAlan Cox * The new root is the vm_map_entry containing "addr", or else an 7340164e057SAlan Cox * adjacent entry (lower or higher) if addr is not in the tree. 7350164e057SAlan Cox * 7360164e057SAlan Cox * The map must be locked, and leaves it so. 7370164e057SAlan Cox * 7380164e057SAlan Cox * Returns: the new root. 7394e94f402SAlan Cox */ 7404e94f402SAlan Cox static vm_map_entry_t 7410164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root) 7424e94f402SAlan Cox { 7430164e057SAlan Cox vm_map_entry_t llist, rlist; 7440164e057SAlan Cox vm_map_entry_t ltree, rtree; 7450164e057SAlan Cox vm_map_entry_t y; 7464e94f402SAlan Cox 7470164e057SAlan Cox /* Special case of empty tree. */ 7484e94f402SAlan Cox if (root == NULL) 7494e94f402SAlan Cox return (root); 7500164e057SAlan Cox 7510164e057SAlan Cox /* 7520164e057SAlan Cox * Pass One: Splay down the tree until we find addr or a NULL 7530164e057SAlan Cox * pointer where addr would go. llist and rlist are the two 7540164e057SAlan Cox * sides in reverse order (bottom-up), with llist linked by 7550164e057SAlan Cox * the right pointer and rlist linked by the left pointer in 7560164e057SAlan Cox * the vm_map_entry. Wait until Pass Two to set max_free on 7570164e057SAlan Cox * the two spines. 7580164e057SAlan Cox */ 7590164e057SAlan Cox llist = NULL; 7600164e057SAlan Cox rlist = NULL; 7610164e057SAlan Cox for (;;) { 7620164e057SAlan Cox /* root is never NULL in here. */ 7630164e057SAlan Cox if (addr < root->start) { 7640164e057SAlan Cox y = root->left; 7650164e057SAlan Cox if (y == NULL) 7664e94f402SAlan Cox break; 7670164e057SAlan Cox if (addr < y->start && y->left != NULL) { 7680164e057SAlan Cox /* Rotate right and put y on rlist. */ 7694e94f402SAlan Cox root->left = y->right; 7704e94f402SAlan Cox y->right = root; 7710164e057SAlan Cox vm_map_entry_set_max_free(root); 7720164e057SAlan Cox root = y->left; 7730164e057SAlan Cox y->left = rlist; 7740164e057SAlan Cox rlist = y; 7750164e057SAlan Cox } else { 7760164e057SAlan Cox /* Put root on rlist. */ 7770164e057SAlan Cox root->left = rlist; 7780164e057SAlan Cox rlist = root; 7794e94f402SAlan Cox root = y; 7804e94f402SAlan Cox } 7817438d60bSAlan Cox } else if (addr >= root->end) { 7820164e057SAlan Cox y = root->right; 7837438d60bSAlan Cox if (y == NULL) 7844e94f402SAlan Cox break; 7850164e057SAlan Cox if (addr >= y->end && y->right != NULL) { 7860164e057SAlan Cox /* Rotate left and put y on llist. */ 7874e94f402SAlan Cox root->right = y->left; 7884e94f402SAlan Cox y->left = root; 7890164e057SAlan Cox vm_map_entry_set_max_free(root); 7900164e057SAlan Cox root = y->right; 7910164e057SAlan Cox y->right = llist; 7920164e057SAlan Cox llist = y; 7930164e057SAlan Cox } else { 7940164e057SAlan Cox /* Put root on llist. */ 7950164e057SAlan Cox root->right = llist; 7960164e057SAlan Cox llist = root; 7974e94f402SAlan Cox root = y; 7984e94f402SAlan Cox } 7997438d60bSAlan Cox } else 8007438d60bSAlan Cox break; 8010164e057SAlan Cox } 8020164e057SAlan Cox 8030164e057SAlan Cox /* 8040164e057SAlan Cox * Pass Two: Walk back up the two spines, flip the pointers 8050164e057SAlan Cox * and set max_free. The subtrees of the root go at the 8060164e057SAlan Cox * bottom of llist and rlist. 8070164e057SAlan Cox */ 8080164e057SAlan Cox ltree = root->left; 8090164e057SAlan Cox while (llist != NULL) { 8100164e057SAlan Cox y = llist->right; 8110164e057SAlan Cox llist->right = ltree; 8120164e057SAlan Cox vm_map_entry_set_max_free(llist); 8130164e057SAlan Cox ltree = llist; 8140164e057SAlan Cox llist = y; 8150164e057SAlan Cox } 8160164e057SAlan Cox rtree = root->right; 8170164e057SAlan Cox while (rlist != NULL) { 8180164e057SAlan Cox y = rlist->left; 8190164e057SAlan Cox rlist->left = rtree; 8200164e057SAlan Cox vm_map_entry_set_max_free(rlist); 8210164e057SAlan Cox rtree = rlist; 8220164e057SAlan Cox rlist = y; 8230164e057SAlan Cox } 8240164e057SAlan Cox 8250164e057SAlan Cox /* 8260164e057SAlan Cox * Final assembly: add ltree and rtree as subtrees of root. 8270164e057SAlan Cox */ 8280164e057SAlan Cox root->left = ltree; 8290164e057SAlan Cox root->right = rtree; 8300164e057SAlan Cox vm_map_entry_set_max_free(root); 8310164e057SAlan Cox 8324e94f402SAlan Cox return (root); 8334e94f402SAlan Cox } 8344e94f402SAlan Cox 8354e94f402SAlan Cox /* 836df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 837df8bae1dSRodney W. Grimes * 838df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 839df8bae1dSRodney W. Grimes */ 8404e94f402SAlan Cox static void 84199c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 84299c81ca9SAlan Cox vm_map_entry_t after_where, 84399c81ca9SAlan Cox vm_map_entry_t entry) 84499c81ca9SAlan Cox { 84521c641b2SJohn Baldwin 84621c641b2SJohn Baldwin CTR4(KTR_VM, 84721c641b2SJohn Baldwin "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 84821c641b2SJohn Baldwin map->nentries, entry, after_where); 84999c81ca9SAlan Cox map->nentries++; 85099c81ca9SAlan Cox entry->prev = after_where; 85199c81ca9SAlan Cox entry->next = after_where->next; 85299c81ca9SAlan Cox entry->next->prev = entry; 85399c81ca9SAlan Cox after_where->next = entry; 8544e94f402SAlan Cox 8554e94f402SAlan Cox if (after_where != &map->header) { 8564e94f402SAlan Cox if (after_where != map->root) 8574e94f402SAlan Cox vm_map_entry_splay(after_where->start, map->root); 8584e94f402SAlan Cox entry->right = after_where->right; 8594e94f402SAlan Cox entry->left = after_where; 8604e94f402SAlan Cox after_where->right = NULL; 8610164e057SAlan Cox after_where->adj_free = entry->start - after_where->end; 8620164e057SAlan Cox vm_map_entry_set_max_free(after_where); 8634e94f402SAlan Cox } else { 8644e94f402SAlan Cox entry->right = map->root; 8654e94f402SAlan Cox entry->left = NULL; 8664e94f402SAlan Cox } 8670164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 8680164e057SAlan Cox entry->next->start) - entry->end; 8690164e057SAlan Cox vm_map_entry_set_max_free(entry); 8704e94f402SAlan Cox map->root = entry; 871df8bae1dSRodney W. Grimes } 87299c81ca9SAlan Cox 8734e94f402SAlan Cox static void 87499c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 87599c81ca9SAlan Cox vm_map_entry_t entry) 87699c81ca9SAlan Cox { 8774e94f402SAlan Cox vm_map_entry_t next, prev, root; 87899c81ca9SAlan Cox 8794e94f402SAlan Cox if (entry != map->root) 8804e94f402SAlan Cox vm_map_entry_splay(entry->start, map->root); 8814e94f402SAlan Cox if (entry->left == NULL) 8824e94f402SAlan Cox root = entry->right; 8834e94f402SAlan Cox else { 8844e94f402SAlan Cox root = vm_map_entry_splay(entry->start, entry->left); 8854e94f402SAlan Cox root->right = entry->right; 8860164e057SAlan Cox root->adj_free = (entry->next == &map->header ? map->max_offset : 8870164e057SAlan Cox entry->next->start) - root->end; 8880164e057SAlan Cox vm_map_entry_set_max_free(root); 8894e94f402SAlan Cox } 8904e94f402SAlan Cox map->root = root; 8914e94f402SAlan Cox 8924e94f402SAlan Cox prev = entry->prev; 8934e94f402SAlan Cox next = entry->next; 89499c81ca9SAlan Cox next->prev = prev; 89599c81ca9SAlan Cox prev->next = next; 89699c81ca9SAlan Cox map->nentries--; 89721c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 89821c641b2SJohn Baldwin map->nentries, entry); 899df8bae1dSRodney W. Grimes } 900df8bae1dSRodney W. Grimes 901df8bae1dSRodney W. Grimes /* 9020164e057SAlan Cox * vm_map_entry_resize_free: 9030164e057SAlan Cox * 9040164e057SAlan Cox * Recompute the amount of free space following a vm_map_entry 9050164e057SAlan Cox * and propagate that value up the tree. Call this function after 9060164e057SAlan Cox * resizing a map entry in-place, that is, without a call to 9070164e057SAlan Cox * vm_map_entry_link() or _unlink(). 9080164e057SAlan Cox * 9090164e057SAlan Cox * The map must be locked, and leaves it so. 9100164e057SAlan Cox */ 9110164e057SAlan Cox static void 9120164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry) 9130164e057SAlan Cox { 9140164e057SAlan Cox 9150164e057SAlan Cox /* 9160164e057SAlan Cox * Using splay trees without parent pointers, propagating 9170164e057SAlan Cox * max_free up the tree is done by moving the entry to the 9180164e057SAlan Cox * root and making the change there. 9190164e057SAlan Cox */ 9200164e057SAlan Cox if (entry != map->root) 9210164e057SAlan Cox map->root = vm_map_entry_splay(entry->start, map->root); 9220164e057SAlan Cox 9230164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 9240164e057SAlan Cox entry->next->start) - entry->end; 9250164e057SAlan Cox vm_map_entry_set_max_free(entry); 9260164e057SAlan Cox } 9270164e057SAlan Cox 9280164e057SAlan Cox /* 929df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 930df8bae1dSRodney W. Grimes * 931df8bae1dSRodney W. Grimes * Finds the map entry containing (or 932df8bae1dSRodney W. Grimes * immediately preceding) the specified address 933df8bae1dSRodney W. Grimes * in the given map; the entry is returned 934df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 935df8bae1dSRodney W. Grimes * result indicates whether the address is 936df8bae1dSRodney W. Grimes * actually contained in the map. 937df8bae1dSRodney W. Grimes */ 9380d94caffSDavid Greenman boolean_t 9391b40f8c0SMatthew Dillon vm_map_lookup_entry( 9401b40f8c0SMatthew Dillon vm_map_t map, 9411b40f8c0SMatthew Dillon vm_offset_t address, 9421b40f8c0SMatthew Dillon vm_map_entry_t *entry) /* OUT */ 943df8bae1dSRodney W. Grimes { 944c0877f10SJohn Dyson vm_map_entry_t cur; 94505a8c414SAlan Cox boolean_t locked; 946df8bae1dSRodney W. Grimes 9474c3ef59eSAlan Cox /* 9484c3ef59eSAlan Cox * If the map is empty, then the map entry immediately preceding 9494c3ef59eSAlan Cox * "address" is the map's header. 9504c3ef59eSAlan Cox */ 9514c3ef59eSAlan Cox cur = map->root; 9524e94f402SAlan Cox if (cur == NULL) 9534e94f402SAlan Cox *entry = &map->header; 9544c3ef59eSAlan Cox else if (address >= cur->start && cur->end > address) { 9554c3ef59eSAlan Cox *entry = cur; 9564c3ef59eSAlan Cox return (TRUE); 95705a8c414SAlan Cox } else if ((locked = vm_map_locked(map)) || 95805a8c414SAlan Cox sx_try_upgrade(&map->lock)) { 95905a8c414SAlan Cox /* 96005a8c414SAlan Cox * Splay requires a write lock on the map. However, it only 96105a8c414SAlan Cox * restructures the binary search tree; it does not otherwise 96205a8c414SAlan Cox * change the map. Thus, the map's timestamp need not change 96305a8c414SAlan Cox * on a temporary upgrade. 96405a8c414SAlan Cox */ 9654c3ef59eSAlan Cox map->root = cur = vm_map_entry_splay(address, cur); 96605a8c414SAlan Cox if (!locked) 96705a8c414SAlan Cox sx_downgrade(&map->lock); 968df8bae1dSRodney W. Grimes 9694c3ef59eSAlan Cox /* 9704c3ef59eSAlan Cox * If "address" is contained within a map entry, the new root 9714c3ef59eSAlan Cox * is that map entry. Otherwise, the new root is a map entry 9724c3ef59eSAlan Cox * immediately before or after "address". 9734c3ef59eSAlan Cox */ 974df8bae1dSRodney W. Grimes if (address >= cur->start) { 975df8bae1dSRodney W. Grimes *entry = cur; 9764e94f402SAlan Cox if (cur->end > address) 977df8bae1dSRodney W. Grimes return (TRUE); 9784e94f402SAlan Cox } else 979df8bae1dSRodney W. Grimes *entry = cur->prev; 98005a8c414SAlan Cox } else 98105a8c414SAlan Cox /* 98205a8c414SAlan Cox * Since the map is only locked for read access, perform a 98305a8c414SAlan Cox * standard binary search tree lookup for "address". 98405a8c414SAlan Cox */ 98505a8c414SAlan Cox for (;;) { 98605a8c414SAlan Cox if (address < cur->start) { 98705a8c414SAlan Cox if (cur->left == NULL) { 98805a8c414SAlan Cox *entry = cur->prev; 98905a8c414SAlan Cox break; 99005a8c414SAlan Cox } 99105a8c414SAlan Cox cur = cur->left; 99205a8c414SAlan Cox } else if (cur->end > address) { 99305a8c414SAlan Cox *entry = cur; 99405a8c414SAlan Cox return (TRUE); 99505a8c414SAlan Cox } else { 99605a8c414SAlan Cox if (cur->right == NULL) { 99705a8c414SAlan Cox *entry = cur; 99805a8c414SAlan Cox break; 99905a8c414SAlan Cox } 100005a8c414SAlan Cox cur = cur->right; 100105a8c414SAlan Cox } 10024e94f402SAlan Cox } 1003df8bae1dSRodney W. Grimes return (FALSE); 1004df8bae1dSRodney W. Grimes } 1005df8bae1dSRodney W. Grimes 1006df8bae1dSRodney W. Grimes /* 100730dcfc09SJohn Dyson * vm_map_insert: 100830dcfc09SJohn Dyson * 100930dcfc09SJohn Dyson * Inserts the given whole VM object into the target 101030dcfc09SJohn Dyson * map at the specified address range. The object's 101130dcfc09SJohn Dyson * size should match that of the address range. 101230dcfc09SJohn Dyson * 101330dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 10142aaeadf8SMatthew Dillon * 10152aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 10162aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 101730dcfc09SJohn Dyson */ 101830dcfc09SJohn Dyson int 1019b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1020b9dcd593SBruce Evans vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 1021b9dcd593SBruce Evans int cow) 102230dcfc09SJohn Dyson { 1023c0877f10SJohn Dyson vm_map_entry_t new_entry; 1024c0877f10SJohn Dyson vm_map_entry_t prev_entry; 102530dcfc09SJohn Dyson vm_map_entry_t temp_entry; 10269730a5daSPaul Saab vm_eflags_t protoeflags; 102730dcfc09SJohn Dyson 102830dcfc09SJohn Dyson /* 102930dcfc09SJohn Dyson * Check that the start and end points are not bogus. 103030dcfc09SJohn Dyson */ 103130dcfc09SJohn Dyson if ((start < map->min_offset) || (end > map->max_offset) || 103230dcfc09SJohn Dyson (start >= end)) 103330dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 103430dcfc09SJohn Dyson 103530dcfc09SJohn Dyson /* 103630dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 103730dcfc09SJohn Dyson * of an existing entry, this range is bogus. 103830dcfc09SJohn Dyson */ 103930dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 104030dcfc09SJohn Dyson return (KERN_NO_SPACE); 104130dcfc09SJohn Dyson 104230dcfc09SJohn Dyson prev_entry = temp_entry; 104330dcfc09SJohn Dyson 104430dcfc09SJohn Dyson /* 104530dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 104630dcfc09SJohn Dyson */ 104730dcfc09SJohn Dyson if ((prev_entry->next != &map->header) && 104830dcfc09SJohn Dyson (prev_entry->next->start < end)) 104930dcfc09SJohn Dyson return (KERN_NO_SPACE); 105030dcfc09SJohn Dyson 1051afa07f7eSJohn Dyson protoeflags = 0; 1052afa07f7eSJohn Dyson 1053afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 1054e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 1055afa07f7eSJohn Dyson 10564e045f93SAlan Cox if (cow & MAP_NOFAULT) { 1057afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 1058afa07f7eSJohn Dyson 10594e045f93SAlan Cox KASSERT(object == NULL, 10604e045f93SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 10614e045f93SAlan Cox } 10624f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 10634f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 10649730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 10659730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 10664f79d873SMatthew Dillon 10671d284e00SAlan Cox if (object != NULL) { 106830dcfc09SJohn Dyson /* 10691d284e00SAlan Cox * OBJ_ONEMAPPING must be cleared unless this mapping 10701d284e00SAlan Cox * is trivially proven to be the only mapping for any 10711d284e00SAlan Cox * of the object's pages. (Object granularity 10721d284e00SAlan Cox * reference counting is insufficient to recognize 10731d284e00SAlan Cox * aliases with precision.) 107430dcfc09SJohn Dyson */ 10751d284e00SAlan Cox VM_OBJECT_LOCK(object); 10761d284e00SAlan Cox if (object->ref_count > 1 || object->shadow_count != 0) 10772aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 10781d284e00SAlan Cox VM_OBJECT_UNLOCK(object); 10794e045f93SAlan Cox } 10804e045f93SAlan Cox else if ((prev_entry != &map->header) && 10814e045f93SAlan Cox (prev_entry->eflags == protoeflags) && 10828cc7e047SJohn Dyson (prev_entry->end == start) && 10834e045f93SAlan Cox (prev_entry->wired_count == 0) && 10844e045f93SAlan Cox ((prev_entry->object.vm_object == NULL) || 10858cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 108657a21abaSAlan Cox prev_entry->offset, 10878cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 1088cdc2c291SJohn Dyson (vm_size_t)(end - prev_entry->end)))) { 108930dcfc09SJohn Dyson /* 10902aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 10912aaeadf8SMatthew Dillon * can extend the previous map entry to include the 10922aaeadf8SMatthew Dillon * new range as well. 109330dcfc09SJohn Dyson */ 10948cc7e047SJohn Dyson if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 10958cc7e047SJohn Dyson (prev_entry->protection == prot) && 10968cc7e047SJohn Dyson (prev_entry->max_protection == max)) { 109730dcfc09SJohn Dyson map->size += (end - prev_entry->end); 109830dcfc09SJohn Dyson prev_entry->end = end; 10990164e057SAlan Cox vm_map_entry_resize_free(map, prev_entry); 11004e71e795SMatthew Dillon vm_map_simplify_entry(map, prev_entry); 110130dcfc09SJohn Dyson return (KERN_SUCCESS); 110230dcfc09SJohn Dyson } 11038cc7e047SJohn Dyson 11042aaeadf8SMatthew Dillon /* 11052aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 11062aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 11072aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 11084e71e795SMatthew Dillon * account for it. object may be NULL. 11092aaeadf8SMatthew Dillon */ 11102aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 11112aaeadf8SMatthew Dillon offset = prev_entry->offset + 11122aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 11138cc7e047SJohn Dyson vm_object_reference(object); 1114b18bfc3dSJohn Dyson } 11152aaeadf8SMatthew Dillon 11162aaeadf8SMatthew Dillon /* 11172aaeadf8SMatthew Dillon * NOTE: if conditionals fail, object can be NULL here. This occurs 11182aaeadf8SMatthew Dillon * in things like the buffer map where we manage kva but do not manage 11192aaeadf8SMatthew Dillon * backing objects. 11202aaeadf8SMatthew Dillon */ 11218cc7e047SJohn Dyson 112230dcfc09SJohn Dyson /* 112330dcfc09SJohn Dyson * Create a new entry 112430dcfc09SJohn Dyson */ 112530dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 112630dcfc09SJohn Dyson new_entry->start = start; 112730dcfc09SJohn Dyson new_entry->end = end; 112830dcfc09SJohn Dyson 1129afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 113030dcfc09SJohn Dyson new_entry->object.vm_object = object; 113130dcfc09SJohn Dyson new_entry->offset = offset; 11322267af78SJulian Elischer new_entry->avail_ssize = 0; 11332267af78SJulian Elischer 113430dcfc09SJohn Dyson new_entry->inheritance = VM_INHERIT_DEFAULT; 113530dcfc09SJohn Dyson new_entry->protection = prot; 113630dcfc09SJohn Dyson new_entry->max_protection = max; 113730dcfc09SJohn Dyson new_entry->wired_count = 0; 1138e5f251d2SAlan Cox 113930dcfc09SJohn Dyson /* 114030dcfc09SJohn Dyson * Insert the new entry into the list 114130dcfc09SJohn Dyson */ 114230dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 114330dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 114430dcfc09SJohn Dyson 11451a484d28SMatthew Dillon #if 0 11461a484d28SMatthew Dillon /* 11471a484d28SMatthew Dillon * Temporarily removed to avoid MAP_STACK panic, due to 11481a484d28SMatthew Dillon * MAP_STACK being a huge hack. Will be added back in 11491a484d28SMatthew Dillon * when MAP_STACK (and the user stack mapping) is fixed. 11501a484d28SMatthew Dillon */ 11514e71e795SMatthew Dillon /* 11524e71e795SMatthew Dillon * It may be possible to simplify the entry 11534e71e795SMatthew Dillon */ 11544e71e795SMatthew Dillon vm_map_simplify_entry(map, new_entry); 11551a484d28SMatthew Dillon #endif 11564e71e795SMatthew Dillon 11574f79d873SMatthew Dillon if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 11584da4d293SAlan Cox vm_map_pmap_enter(map, start, prot, 1159e972780aSAlan Cox object, OFF_TO_IDX(offset), end - start, 1160e972780aSAlan Cox cow & MAP_PREFAULT_PARTIAL); 11614f79d873SMatthew Dillon } 1162e972780aSAlan Cox 116330dcfc09SJohn Dyson return (KERN_SUCCESS); 116430dcfc09SJohn Dyson } 116530dcfc09SJohn Dyson 116630dcfc09SJohn Dyson /* 11670164e057SAlan Cox * vm_map_findspace: 11680164e057SAlan Cox * 11690164e057SAlan Cox * Find the first fit (lowest VM address) for "length" free bytes 11700164e057SAlan Cox * beginning at address >= start in the given map. 11710164e057SAlan Cox * 11720164e057SAlan Cox * In a vm_map_entry, "adj_free" is the amount of free space 11730164e057SAlan Cox * adjacent (higher address) to this entry, and "max_free" is the 11740164e057SAlan Cox * maximum amount of contiguous free space in its subtree. This 11750164e057SAlan Cox * allows finding a free region in one path down the tree, so 11760164e057SAlan Cox * O(log n) amortized with splay trees. 11770164e057SAlan Cox * 11780164e057SAlan Cox * The map must be locked, and leaves it so. 11790164e057SAlan Cox * 11800164e057SAlan Cox * Returns: 0 on success, and starting address in *addr, 11810164e057SAlan Cox * 1 if insufficient space. 1182df8bae1dSRodney W. Grimes */ 1183df8bae1dSRodney W. Grimes int 11840164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length, 11850164e057SAlan Cox vm_offset_t *addr) /* OUT */ 1186df8bae1dSRodney W. Grimes { 11870164e057SAlan Cox vm_map_entry_t entry; 11880164e057SAlan Cox vm_offset_t end, st; 1189df8bae1dSRodney W. Grimes 1190986b43f8SAlan Cox /* 1191986b43f8SAlan Cox * Request must fit within min/max VM address and must avoid 1192986b43f8SAlan Cox * address wrap. 1193986b43f8SAlan Cox */ 1194df8bae1dSRodney W. Grimes if (start < map->min_offset) 1195df8bae1dSRodney W. Grimes start = map->min_offset; 1196986b43f8SAlan Cox if (start + length > map->max_offset || start + length < start) 1197df8bae1dSRodney W. Grimes return (1); 1198df8bae1dSRodney W. Grimes 11990164e057SAlan Cox /* Empty tree means wide open address space. */ 12000164e057SAlan Cox if (map->root == NULL) { 1201df8bae1dSRodney W. Grimes *addr = start; 12020164e057SAlan Cox goto found; 120399448ed1SJohn Dyson } 12040164e057SAlan Cox 12050164e057SAlan Cox /* 12060164e057SAlan Cox * After splay, if start comes before root node, then there 12070164e057SAlan Cox * must be a gap from start to the root. 12080164e057SAlan Cox */ 12090164e057SAlan Cox map->root = vm_map_entry_splay(start, map->root); 12100164e057SAlan Cox if (start + length <= map->root->start) { 12110164e057SAlan Cox *addr = start; 12120164e057SAlan Cox goto found; 12130164e057SAlan Cox } 12140164e057SAlan Cox 12150164e057SAlan Cox /* 12160164e057SAlan Cox * Root is the last node that might begin its gap before 1217986b43f8SAlan Cox * start, and this is the last comparison where address 1218986b43f8SAlan Cox * wrap might be a problem. 12190164e057SAlan Cox */ 12200164e057SAlan Cox st = (start > map->root->end) ? start : map->root->end; 1221986b43f8SAlan Cox if (length <= map->root->end + map->root->adj_free - st) { 12220164e057SAlan Cox *addr = st; 12230164e057SAlan Cox goto found; 12240164e057SAlan Cox } 12250164e057SAlan Cox 12260164e057SAlan Cox /* With max_free, can immediately tell if no solution. */ 12270164e057SAlan Cox entry = map->root->right; 12280164e057SAlan Cox if (entry == NULL || length > entry->max_free) 12290164e057SAlan Cox return (1); 12300164e057SAlan Cox 12310164e057SAlan Cox /* 12320164e057SAlan Cox * Search the right subtree in the order: left subtree, root, 12330164e057SAlan Cox * right subtree (first fit). The previous splay implies that 12340164e057SAlan Cox * all regions in the right subtree have addresses > start. 12350164e057SAlan Cox */ 12360164e057SAlan Cox while (entry != NULL) { 12370164e057SAlan Cox if (entry->left != NULL && entry->left->max_free >= length) 12380164e057SAlan Cox entry = entry->left; 12390164e057SAlan Cox else if (entry->adj_free >= length) { 12400164e057SAlan Cox *addr = entry->end; 12410164e057SAlan Cox goto found; 12420164e057SAlan Cox } else 12430164e057SAlan Cox entry = entry->right; 12440164e057SAlan Cox } 12450164e057SAlan Cox 12460164e057SAlan Cox /* Can't get here, so panic if we do. */ 12470164e057SAlan Cox panic("vm_map_findspace: max_free corrupt"); 12480164e057SAlan Cox 12490164e057SAlan Cox found: 12500164e057SAlan Cox /* Expand the kernel pmap, if necessary. */ 12510164e057SAlan Cox if (map == kernel_map) { 12520164e057SAlan Cox end = round_page(*addr + length); 12530164e057SAlan Cox if (end > kernel_vm_end) 12540164e057SAlan Cox pmap_growkernel(end); 125599448ed1SJohn Dyson } 1256df8bae1dSRodney W. Grimes return (0); 1257df8bae1dSRodney W. Grimes } 1258df8bae1dSRodney W. Grimes 1259d239bd3cSKonstantin Belousov int 1260d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1261b8ca4ef2SAlan Cox vm_offset_t start, vm_size_t length, vm_prot_t prot, 1262d239bd3cSKonstantin Belousov vm_prot_t max, int cow) 1263d239bd3cSKonstantin Belousov { 1264b8ca4ef2SAlan Cox vm_offset_t end; 1265d239bd3cSKonstantin Belousov int result; 1266d239bd3cSKonstantin Belousov 1267d239bd3cSKonstantin Belousov vm_map_lock(map); 1268d239bd3cSKonstantin Belousov end = start + length; 1269d239bd3cSKonstantin Belousov VM_MAP_RANGE_CHECK(map, start, end); 1270d239bd3cSKonstantin Belousov (void) vm_map_delete(map, start, end); 1271d239bd3cSKonstantin Belousov result = vm_map_insert(map, object, offset, start, end, prot, 1272d239bd3cSKonstantin Belousov max, cow); 1273d239bd3cSKonstantin Belousov vm_map_unlock(map); 1274d239bd3cSKonstantin Belousov return (result); 1275d239bd3cSKonstantin Belousov } 1276d239bd3cSKonstantin Belousov 1277df8bae1dSRodney W. Grimes /* 1278df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 1279df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 1280df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 1281df8bae1dSRodney W. Grimes * returned in the same parameter. 1282df8bae1dSRodney W. Grimes * 12832aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 12842aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 1285df8bae1dSRodney W. Grimes */ 1286df8bae1dSRodney W. Grimes int 1287b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1288b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 128926c538ffSAlan Cox vm_size_t length, int find_space, vm_prot_t prot, 1290b9dcd593SBruce Evans vm_prot_t max, int cow) 1291df8bae1dSRodney W. Grimes { 1292c0877f10SJohn Dyson vm_offset_t start; 12936eaee3feSAlan Cox int result; 1294df8bae1dSRodney W. Grimes 1295df8bae1dSRodney W. Grimes start = *addr; 1296bea41bcfSDavid Greenman vm_map_lock(map); 129726c538ffSAlan Cox do { 129826c538ffSAlan Cox if (find_space != VMFS_NO_SPACE) { 1299df8bae1dSRodney W. Grimes if (vm_map_findspace(map, start, length, addr)) { 1300df8bae1dSRodney W. Grimes vm_map_unlock(map); 1301df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 1302df8bae1dSRodney W. Grimes } 130326c538ffSAlan Cox if (find_space == VMFS_ALIGNED_SPACE) 130426c538ffSAlan Cox pmap_align_superpage(object, offset, addr, 130526c538ffSAlan Cox length); 1306df8bae1dSRodney W. Grimes start = *addr; 1307df8bae1dSRodney W. Grimes } 130826c538ffSAlan Cox result = vm_map_insert(map, object, offset, start, start + 130926c538ffSAlan Cox length, prot, max, cow); 131026c538ffSAlan Cox } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE); 1311df8bae1dSRodney W. Grimes vm_map_unlock(map); 1312df8bae1dSRodney W. Grimes return (result); 1313df8bae1dSRodney W. Grimes } 1314df8bae1dSRodney W. Grimes 1315df8bae1dSRodney W. Grimes /* 1316b7b2aac2SJohn Dyson * vm_map_simplify_entry: 131767bf6868SJohn Dyson * 13184e71e795SMatthew Dillon * Simplify the given map entry by merging with either neighbor. This 13194e71e795SMatthew Dillon * routine also has the ability to merge with both neighbors. 13204e71e795SMatthew Dillon * 13214e71e795SMatthew Dillon * The map must be locked. 13224e71e795SMatthew Dillon * 13234e71e795SMatthew Dillon * This routine guarentees that the passed entry remains valid (though 13244e71e795SMatthew Dillon * possibly extended). When merging, this routine may delete one or 13254e71e795SMatthew Dillon * both neighbors. 1326df8bae1dSRodney W. Grimes */ 1327b7b2aac2SJohn Dyson void 13281b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1329df8bae1dSRodney W. Grimes { 1330308c24baSJohn Dyson vm_map_entry_t next, prev; 1331b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 1332df8bae1dSRodney W. Grimes 1333acd9a301SAlan Cox if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) 1334df8bae1dSRodney W. Grimes return; 1335308c24baSJohn Dyson 1336308c24baSJohn Dyson prev = entry->prev; 1337308c24baSJohn Dyson if (prev != &map->header) { 133867bf6868SJohn Dyson prevsize = prev->end - prev->start; 133967bf6868SJohn Dyson if ( (prev->end == entry->start) && 134067bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 134195e5e988SJohn Dyson (!prev->object.vm_object || 134267bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 1343afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 134467bf6868SJohn Dyson (prev->protection == entry->protection) && 134567bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 134667bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 1347b7b2aac2SJohn Dyson (prev->wired_count == entry->wired_count)) { 1348308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 1349308c24baSJohn Dyson entry->start = prev->start; 1350308c24baSJohn Dyson entry->offset = prev->offset; 13510164e057SAlan Cox if (entry->prev != &map->header) 13520164e057SAlan Cox vm_map_entry_resize_free(map, entry->prev); 1353b18bfc3dSJohn Dyson if (prev->object.vm_object) 1354308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 1355308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 1356308c24baSJohn Dyson } 1357308c24baSJohn Dyson } 1358de5f6a77SJohn Dyson 1359de5f6a77SJohn Dyson next = entry->next; 1360308c24baSJohn Dyson if (next != &map->header) { 136167bf6868SJohn Dyson esize = entry->end - entry->start; 136267bf6868SJohn Dyson if ((entry->end == next->start) && 136367bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 136467bf6868SJohn Dyson (!entry->object.vm_object || 136567bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 1366afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 136767bf6868SJohn Dyson (next->protection == entry->protection) && 136867bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 136967bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 1370b7b2aac2SJohn Dyson (next->wired_count == entry->wired_count)) { 1371de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 1372de5f6a77SJohn Dyson entry->end = next->end; 13730164e057SAlan Cox vm_map_entry_resize_free(map, entry); 1374b18bfc3dSJohn Dyson if (next->object.vm_object) 1375de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 1376de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 1377df8bae1dSRodney W. Grimes } 1378df8bae1dSRodney W. Grimes } 1379de5f6a77SJohn Dyson } 1380df8bae1dSRodney W. Grimes /* 1381df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 1382df8bae1dSRodney W. Grimes * 1383df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 1384df8bae1dSRodney W. Grimes * the specified address; if necessary, 1385df8bae1dSRodney W. Grimes * it splits the entry into two. 1386df8bae1dSRodney W. Grimes */ 1387df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 1388df8bae1dSRodney W. Grimes { \ 1389df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 1390df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 1391df8bae1dSRodney W. Grimes } 1392df8bae1dSRodney W. Grimes 1393df8bae1dSRodney W. Grimes /* 1394df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1395df8bae1dSRodney W. Grimes * the entry must be split. 1396df8bae1dSRodney W. Grimes */ 13970d94caffSDavid Greenman static void 13981b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1399df8bae1dSRodney W. Grimes { 1400c0877f10SJohn Dyson vm_map_entry_t new_entry; 1401df8bae1dSRodney W. Grimes 1402df8bae1dSRodney W. Grimes /* 14030d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 14040d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 14050d94caffSDavid Greenman * starting address. 1406df8bae1dSRodney W. Grimes */ 1407f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 1408f32dbbeeSJohn Dyson 140911cccda1SJohn Dyson /* 141011cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 141111cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 141211cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 141311cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 141411cccda1SJohn Dyson * put this improvement. 141511cccda1SJohn Dyson */ 14164e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 141711cccda1SJohn Dyson vm_object_t object; 141811cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1419c2e11a03SJohn Dyson atop(entry->end - entry->start)); 142011cccda1SJohn Dyson entry->object.vm_object = object; 142111cccda1SJohn Dyson entry->offset = 0; 142211cccda1SJohn Dyson } 142311cccda1SJohn Dyson 1424df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1425df8bae1dSRodney W. Grimes *new_entry = *entry; 1426df8bae1dSRodney W. Grimes 1427df8bae1dSRodney W. Grimes new_entry->end = start; 1428df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 1429df8bae1dSRodney W. Grimes entry->start = start; 1430df8bae1dSRodney W. Grimes 1431df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 1432df8bae1dSRodney W. Grimes 14339fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1434df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1435df8bae1dSRodney W. Grimes } 1436c0877f10SJohn Dyson } 1437df8bae1dSRodney W. Grimes 1438df8bae1dSRodney W. Grimes /* 1439df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 1440df8bae1dSRodney W. Grimes * 1441df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 1442df8bae1dSRodney W. Grimes * the specified address; if necessary, 1443df8bae1dSRodney W. Grimes * it splits the entry into two. 1444df8bae1dSRodney W. Grimes */ 1445df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 1446df8bae1dSRodney W. Grimes { \ 1447af045176SPoul-Henning Kamp if ((endaddr) < (entry->end)) \ 1448af045176SPoul-Henning Kamp _vm_map_clip_end((map), (entry), (endaddr)); \ 1449df8bae1dSRodney W. Grimes } 1450df8bae1dSRodney W. Grimes 1451df8bae1dSRodney W. Grimes /* 1452df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1453df8bae1dSRodney W. Grimes * the entry must be split. 1454df8bae1dSRodney W. Grimes */ 14550d94caffSDavid Greenman static void 14561b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1457df8bae1dSRodney W. Grimes { 1458c0877f10SJohn Dyson vm_map_entry_t new_entry; 1459df8bae1dSRodney W. Grimes 1460df8bae1dSRodney W. Grimes /* 146111cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 146211cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 146311cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 146411cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 146511cccda1SJohn Dyson * put this improvement. 146611cccda1SJohn Dyson */ 14674e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 146811cccda1SJohn Dyson vm_object_t object; 146911cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1470c2e11a03SJohn Dyson atop(entry->end - entry->start)); 147111cccda1SJohn Dyson entry->object.vm_object = object; 147211cccda1SJohn Dyson entry->offset = 0; 147311cccda1SJohn Dyson } 147411cccda1SJohn Dyson 147511cccda1SJohn Dyson /* 14760d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 1477df8bae1dSRodney W. Grimes */ 1478df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1479df8bae1dSRodney W. Grimes *new_entry = *entry; 1480df8bae1dSRodney W. Grimes 1481df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 1482df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 1483df8bae1dSRodney W. Grimes 1484df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 1485df8bae1dSRodney W. Grimes 14869fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1487df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1488df8bae1dSRodney W. Grimes } 1489c0877f10SJohn Dyson } 1490df8bae1dSRodney W. Grimes 1491df8bae1dSRodney W. Grimes /* 1492df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 1493df8bae1dSRodney W. Grimes * 1494df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 1495df8bae1dSRodney W. Grimes * 1496df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 1497df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 1498df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 1499df8bae1dSRodney W. Grimes * 1500df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 1501df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 1502df8bae1dSRodney W. Grimes * vm_fault 1503df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 1504df8bae1dSRodney W. Grimes * 1505df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 1506df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 1507df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 1508df8bae1dSRodney W. Grimes */ 1509df8bae1dSRodney W. Grimes int 15101b40f8c0SMatthew Dillon vm_map_submap( 15111b40f8c0SMatthew Dillon vm_map_t map, 15121b40f8c0SMatthew Dillon vm_offset_t start, 15131b40f8c0SMatthew Dillon vm_offset_t end, 15141b40f8c0SMatthew Dillon vm_map_t submap) 1515df8bae1dSRodney W. Grimes { 1516df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1517c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 1518df8bae1dSRodney W. Grimes 1519df8bae1dSRodney W. Grimes vm_map_lock(map); 1520df8bae1dSRodney W. Grimes 1521df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1522df8bae1dSRodney W. Grimes 1523df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1524df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 15250d94caffSDavid Greenman } else 1526df8bae1dSRodney W. Grimes entry = entry->next; 1527df8bae1dSRodney W. Grimes 1528df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1529df8bae1dSRodney W. Grimes 1530df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 15319fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 1532afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 15332d8acc0fSJohn Dyson entry->object.sub_map = submap; 1534afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1535df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 1536df8bae1dSRodney W. Grimes } 1537df8bae1dSRodney W. Grimes vm_map_unlock(map); 1538df8bae1dSRodney W. Grimes 1539df8bae1dSRodney W. Grimes return (result); 1540df8bae1dSRodney W. Grimes } 1541df8bae1dSRodney W. Grimes 1542df8bae1dSRodney W. Grimes /* 15431f78f902SAlan Cox * The maximum number of pages to map 15441f78f902SAlan Cox */ 15451f78f902SAlan Cox #define MAX_INIT_PT 96 15461f78f902SAlan Cox 15471f78f902SAlan Cox /* 15480551c08dSAlan Cox * vm_map_pmap_enter: 15490551c08dSAlan Cox * 1550cf4682aeSAlan Cox * Preload read-only mappings for the given object's resident pages into 1551cf4682aeSAlan Cox * the given map. This eliminates the soft faults on process startup and 1552c4169725SAlan Cox * immediately after an mmap(2). Because these are speculative mappings, 1553c4169725SAlan Cox * cached pages are not reactivated and mapped. 15540551c08dSAlan Cox */ 15550551c08dSAlan Cox void 15564da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 15570551c08dSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 15580551c08dSAlan Cox { 15598fece8c3SAlan Cox vm_offset_t start; 1560ce142d9eSAlan Cox vm_page_t p, p_start; 15618fece8c3SAlan Cox vm_pindex_t psize, tmpidx; 15621f70d622SAlan Cox boolean_t are_queues_locked; 15630551c08dSAlan Cox 1564ba8bca61SAlan Cox if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 15651f78f902SAlan Cox return; 15661f78f902SAlan Cox VM_OBJECT_LOCK(object); 15671f78f902SAlan Cox if (object->type == OBJT_DEVICE) { 15681f78f902SAlan Cox pmap_object_init_pt(map->pmap, addr, object, pindex, size); 15691f78f902SAlan Cox goto unlock_return; 15701f78f902SAlan Cox } 15711f78f902SAlan Cox 15721f78f902SAlan Cox psize = atop(size); 15731f78f902SAlan Cox 15741f78f902SAlan Cox if (object->type != OBJT_VNODE || 15751f78f902SAlan Cox ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) && 15761f78f902SAlan Cox (object->resident_page_count > MAX_INIT_PT))) { 15771f78f902SAlan Cox goto unlock_return; 15781f78f902SAlan Cox } 15791f78f902SAlan Cox 15801f78f902SAlan Cox if (psize + pindex > object->size) { 15811f78f902SAlan Cox if (object->size < pindex) 15821f78f902SAlan Cox goto unlock_return; 15831f78f902SAlan Cox psize = object->size - pindex; 15841f78f902SAlan Cox } 15851f78f902SAlan Cox 15861f70d622SAlan Cox are_queues_locked = FALSE; 1587ce142d9eSAlan Cox start = 0; 1588ce142d9eSAlan Cox p_start = NULL; 15891f78f902SAlan Cox 15901f78f902SAlan Cox if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 15911f78f902SAlan Cox if (p->pindex < pindex) { 15921f78f902SAlan Cox p = vm_page_splay(pindex, object->root); 15931f78f902SAlan Cox if ((object->root = p)->pindex < pindex) 15941f78f902SAlan Cox p = TAILQ_NEXT(p, listq); 15951f78f902SAlan Cox } 15961f78f902SAlan Cox } 15971f78f902SAlan Cox /* 15981f78f902SAlan Cox * Assert: the variable p is either (1) the page with the 15991f78f902SAlan Cox * least pindex greater than or equal to the parameter pindex 16001f78f902SAlan Cox * or (2) NULL. 16011f78f902SAlan Cox */ 16021f78f902SAlan Cox for (; 16031f78f902SAlan Cox p != NULL && (tmpidx = p->pindex - pindex) < psize; 16041f78f902SAlan Cox p = TAILQ_NEXT(p, listq)) { 16051f78f902SAlan Cox /* 16061f78f902SAlan Cox * don't allow an madvise to blow away our really 16071f78f902SAlan Cox * free pages allocating pv entries. 16081f78f902SAlan Cox */ 16091f78f902SAlan Cox if ((flags & MAP_PREFAULT_MADVISE) && 16102feb50bfSAttilio Rao cnt.v_free_count < cnt.v_free_reserved) { 1611379fb642SAlan Cox psize = tmpidx; 16121f78f902SAlan Cox break; 16131f78f902SAlan Cox } 16141f78f902SAlan Cox if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL && 16158fece8c3SAlan Cox (p->busy == 0)) { 1616ce142d9eSAlan Cox if (p_start == NULL) { 1617ce142d9eSAlan Cox start = addr + ptoa(tmpidx); 1618ce142d9eSAlan Cox p_start = p; 1619ce142d9eSAlan Cox } 16207bfda801SAlan Cox } else if (p_start != NULL) { 16211f70d622SAlan Cox if (!are_queues_locked) { 16221f70d622SAlan Cox are_queues_locked = TRUE; 162385f5b245SAlan Cox vm_page_lock_queues(); 162485f5b245SAlan Cox } 1625cf4682aeSAlan Cox pmap_enter_object(map->pmap, start, addr + 1626cf4682aeSAlan Cox ptoa(tmpidx), p_start, prot); 1627cf4682aeSAlan Cox p_start = NULL; 1628cf4682aeSAlan Cox } 1629cf4682aeSAlan Cox } 16307bfda801SAlan Cox if (p_start != NULL) { 16317bfda801SAlan Cox if (!are_queues_locked) { 16327bfda801SAlan Cox are_queues_locked = TRUE; 16337bfda801SAlan Cox vm_page_lock_queues(); 16341f78f902SAlan Cox } 1635379fb642SAlan Cox pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1636379fb642SAlan Cox p_start, prot); 16377bfda801SAlan Cox } 16381f70d622SAlan Cox if (are_queues_locked) 16391f70d622SAlan Cox vm_page_unlock_queues(); 16401f78f902SAlan Cox unlock_return: 16411f78f902SAlan Cox VM_OBJECT_UNLOCK(object); 16420551c08dSAlan Cox } 16430551c08dSAlan Cox 16440551c08dSAlan Cox /* 1645df8bae1dSRodney W. Grimes * vm_map_protect: 1646df8bae1dSRodney W. Grimes * 1647df8bae1dSRodney W. Grimes * Sets the protection of the specified address 1648df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 1649df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 1650df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 1651df8bae1dSRodney W. Grimes */ 1652df8bae1dSRodney W. Grimes int 1653b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1654b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 1655df8bae1dSRodney W. Grimes { 1656c0877f10SJohn Dyson vm_map_entry_t current; 1657df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1658df8bae1dSRodney W. Grimes 1659df8bae1dSRodney W. Grimes vm_map_lock(map); 1660df8bae1dSRodney W. Grimes 1661df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1662df8bae1dSRodney W. Grimes 1663df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1664df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1665b7b2aac2SJohn Dyson } else { 1666df8bae1dSRodney W. Grimes entry = entry->next; 1667b7b2aac2SJohn Dyson } 1668df8bae1dSRodney W. Grimes 1669df8bae1dSRodney W. Grimes /* 16700d94caffSDavid Greenman * Make a first pass to check for protection violations. 1671df8bae1dSRodney W. Grimes */ 1672df8bae1dSRodney W. Grimes current = entry; 1673df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1674afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1675a1f6d91cSDavid Greenman vm_map_unlock(map); 1676df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1677a1f6d91cSDavid Greenman } 1678df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 1679df8bae1dSRodney W. Grimes vm_map_unlock(map); 1680df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 1681df8bae1dSRodney W. Grimes } 1682df8bae1dSRodney W. Grimes current = current->next; 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes 1685df8bae1dSRodney W. Grimes /* 16860d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 16870d94caffSDavid Greenman * necessary the second time.] 1688df8bae1dSRodney W. Grimes */ 1689df8bae1dSRodney W. Grimes current = entry; 1690df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1691df8bae1dSRodney W. Grimes vm_prot_t old_prot; 1692df8bae1dSRodney W. Grimes 1693df8bae1dSRodney W. Grimes vm_map_clip_end(map, current, end); 1694df8bae1dSRodney W. Grimes 1695df8bae1dSRodney W. Grimes old_prot = current->protection; 1696df8bae1dSRodney W. Grimes if (set_max) 1697df8bae1dSRodney W. Grimes current->protection = 1698df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 1699df8bae1dSRodney W. Grimes old_prot; 1700df8bae1dSRodney W. Grimes else 1701df8bae1dSRodney W. Grimes current->protection = new_prot; 1702df8bae1dSRodney W. Grimes 1703df8bae1dSRodney W. Grimes /* 17040d94caffSDavid Greenman * Update physical map if necessary. Worry about copy-on-write 1705e2abaaaaSAlan Cox * here. 1706df8bae1dSRodney W. Grimes */ 1707df8bae1dSRodney W. Grimes if (current->protection != old_prot) { 1708afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1709df8bae1dSRodney W. Grimes VM_PROT_ALL) 1710df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 1711df8bae1dSRodney W. Grimes current->end, 17121c85e3dfSAlan Cox current->protection & MASK(current)); 1713df8bae1dSRodney W. Grimes #undef MASK 1714df8bae1dSRodney W. Grimes } 17157d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 1716df8bae1dSRodney W. Grimes current = current->next; 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes vm_map_unlock(map); 1719df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1720df8bae1dSRodney W. Grimes } 1721df8bae1dSRodney W. Grimes 1722df8bae1dSRodney W. Grimes /* 1723867a482dSJohn Dyson * vm_map_madvise: 1724867a482dSJohn Dyson * 1725867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 1726f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 1727f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 1728f7fc307aSAlan Cox * objects. 1729867a482dSJohn Dyson */ 1730b4309055SMatthew Dillon int 17311b40f8c0SMatthew Dillon vm_map_madvise( 17321b40f8c0SMatthew Dillon vm_map_t map, 17331b40f8c0SMatthew Dillon vm_offset_t start, 17341b40f8c0SMatthew Dillon vm_offset_t end, 17351b40f8c0SMatthew Dillon int behav) 1736867a482dSJohn Dyson { 1737f7fc307aSAlan Cox vm_map_entry_t current, entry; 1738b4309055SMatthew Dillon int modify_map = 0; 1739867a482dSJohn Dyson 1740b4309055SMatthew Dillon /* 1741b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 1742b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 1743b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 1744b4309055SMatthew Dillon * on the map. 1745b4309055SMatthew Dillon */ 1746b4309055SMatthew Dillon switch(behav) { 1747b4309055SMatthew Dillon case MADV_NORMAL: 1748b4309055SMatthew Dillon case MADV_SEQUENTIAL: 1749b4309055SMatthew Dillon case MADV_RANDOM: 17504f79d873SMatthew Dillon case MADV_NOSYNC: 17514f79d873SMatthew Dillon case MADV_AUTOSYNC: 17529730a5daSPaul Saab case MADV_NOCORE: 17539730a5daSPaul Saab case MADV_CORE: 1754b4309055SMatthew Dillon modify_map = 1; 1755867a482dSJohn Dyson vm_map_lock(map); 1756b4309055SMatthew Dillon break; 1757b4309055SMatthew Dillon case MADV_WILLNEED: 1758b4309055SMatthew Dillon case MADV_DONTNEED: 1759b4309055SMatthew Dillon case MADV_FREE: 1760f7fc307aSAlan Cox vm_map_lock_read(map); 1761b4309055SMatthew Dillon break; 1762b4309055SMatthew Dillon default: 1763b4309055SMatthew Dillon return (KERN_INVALID_ARGUMENT); 1764b4309055SMatthew Dillon } 1765b4309055SMatthew Dillon 1766b4309055SMatthew Dillon /* 1767b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 1768b4309055SMatthew Dillon */ 1769867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 1770867a482dSJohn Dyson 1771867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 1772f7fc307aSAlan Cox if (modify_map) 1773867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 1774b4309055SMatthew Dillon } else { 1775867a482dSJohn Dyson entry = entry->next; 1776b4309055SMatthew Dillon } 1777867a482dSJohn Dyson 1778f7fc307aSAlan Cox if (modify_map) { 1779f7fc307aSAlan Cox /* 1780f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 1781f7fc307aSAlan Cox * 1782f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 1783f7fc307aSAlan Cox * limited to the specified address range. 1784f7fc307aSAlan Cox */ 1785867a482dSJohn Dyson for (current = entry; 1786867a482dSJohn Dyson (current != &map->header) && (current->start < end); 1787b4309055SMatthew Dillon current = current->next 1788b4309055SMatthew Dillon ) { 1789f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1790867a482dSJohn Dyson continue; 1791fed9a903SJohn Dyson 179247221757SJohn Dyson vm_map_clip_end(map, current, end); 1793fed9a903SJohn Dyson 1794f7fc307aSAlan Cox switch (behav) { 1795867a482dSJohn Dyson case MADV_NORMAL: 17967f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1797867a482dSJohn Dyson break; 1798867a482dSJohn Dyson case MADV_SEQUENTIAL: 17997f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1800867a482dSJohn Dyson break; 1801867a482dSJohn Dyson case MADV_RANDOM: 18027f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1803867a482dSJohn Dyson break; 18044f79d873SMatthew Dillon case MADV_NOSYNC: 18054f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 18064f79d873SMatthew Dillon break; 18074f79d873SMatthew Dillon case MADV_AUTOSYNC: 18084f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 18094f79d873SMatthew Dillon break; 18109730a5daSPaul Saab case MADV_NOCORE: 18119730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 18129730a5daSPaul Saab break; 18139730a5daSPaul Saab case MADV_CORE: 18149730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 18159730a5daSPaul Saab break; 1816867a482dSJohn Dyson default: 1817867a482dSJohn Dyson break; 1818867a482dSJohn Dyson } 1819f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 1820867a482dSJohn Dyson } 1821867a482dSJohn Dyson vm_map_unlock(map); 1822b4309055SMatthew Dillon } else { 1823f7fc307aSAlan Cox vm_pindex_t pindex; 1824f7fc307aSAlan Cox int count; 1825f7fc307aSAlan Cox 1826f7fc307aSAlan Cox /* 1827f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 1828f7fc307aSAlan Cox * vm_object. 1829f7fc307aSAlan Cox * 1830f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 1831f7fc307aSAlan Cox * the vm_object pindex and count. 1832f7fc307aSAlan Cox */ 1833f7fc307aSAlan Cox for (current = entry; 1834f7fc307aSAlan Cox (current != &map->header) && (current->start < end); 1835b4309055SMatthew Dillon current = current->next 1836b4309055SMatthew Dillon ) { 18375f99b57cSMatthew Dillon vm_offset_t useStart; 18385f99b57cSMatthew Dillon 1839f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1840f7fc307aSAlan Cox continue; 1841f7fc307aSAlan Cox 1842f7fc307aSAlan Cox pindex = OFF_TO_IDX(current->offset); 1843f7fc307aSAlan Cox count = atop(current->end - current->start); 18445f99b57cSMatthew Dillon useStart = current->start; 1845f7fc307aSAlan Cox 1846f7fc307aSAlan Cox if (current->start < start) { 1847f7fc307aSAlan Cox pindex += atop(start - current->start); 1848f7fc307aSAlan Cox count -= atop(start - current->start); 18495f99b57cSMatthew Dillon useStart = start; 1850f7fc307aSAlan Cox } 1851f7fc307aSAlan Cox if (current->end > end) 1852f7fc307aSAlan Cox count -= atop(current->end - end); 1853f7fc307aSAlan Cox 1854f7fc307aSAlan Cox if (count <= 0) 1855f7fc307aSAlan Cox continue; 1856f7fc307aSAlan Cox 1857f7fc307aSAlan Cox vm_object_madvise(current->object.vm_object, 1858f7fc307aSAlan Cox pindex, count, behav); 1859b4309055SMatthew Dillon if (behav == MADV_WILLNEED) { 18600551c08dSAlan Cox vm_map_pmap_enter(map, 18615f99b57cSMatthew Dillon useStart, 18624da4d293SAlan Cox current->protection, 1863f7fc307aSAlan Cox current->object.vm_object, 1864b4309055SMatthew Dillon pindex, 1865b4309055SMatthew Dillon (count << PAGE_SHIFT), 1866e3026983SMatthew Dillon MAP_PREFAULT_MADVISE 1867b4309055SMatthew Dillon ); 1868f7fc307aSAlan Cox } 1869f7fc307aSAlan Cox } 1870f7fc307aSAlan Cox vm_map_unlock_read(map); 1871f7fc307aSAlan Cox } 1872b4309055SMatthew Dillon return (0); 1873867a482dSJohn Dyson } 1874867a482dSJohn Dyson 1875867a482dSJohn Dyson 1876867a482dSJohn Dyson /* 1877df8bae1dSRodney W. Grimes * vm_map_inherit: 1878df8bae1dSRodney W. Grimes * 1879df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 1880df8bae1dSRodney W. Grimes * range in the target map. Inheritance 1881df8bae1dSRodney W. Grimes * affects how the map will be shared with 1882e2abaaaaSAlan Cox * child maps at the time of vmspace_fork. 1883df8bae1dSRodney W. Grimes */ 1884df8bae1dSRodney W. Grimes int 1885b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1886b9dcd593SBruce Evans vm_inherit_t new_inheritance) 1887df8bae1dSRodney W. Grimes { 1888c0877f10SJohn Dyson vm_map_entry_t entry; 1889df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 1890df8bae1dSRodney W. Grimes 1891df8bae1dSRodney W. Grimes switch (new_inheritance) { 1892df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 1893df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 1894df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 1895df8bae1dSRodney W. Grimes break; 1896df8bae1dSRodney W. Grimes default: 1897df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1898df8bae1dSRodney W. Grimes } 1899df8bae1dSRodney W. Grimes vm_map_lock(map); 1900df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1901df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 1902df8bae1dSRodney W. Grimes entry = temp_entry; 1903df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 19040d94caffSDavid Greenman } else 1905df8bae1dSRodney W. Grimes entry = temp_entry->next; 1906df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1907df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1908df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 190944428f62SAlan Cox vm_map_simplify_entry(map, entry); 1910df8bae1dSRodney W. Grimes entry = entry->next; 1911df8bae1dSRodney W. Grimes } 1912df8bae1dSRodney W. Grimes vm_map_unlock(map); 1913df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1914df8bae1dSRodney W. Grimes } 1915df8bae1dSRodney W. Grimes 1916df8bae1dSRodney W. Grimes /* 1917acd9a301SAlan Cox * vm_map_unwire: 1918acd9a301SAlan Cox * 1919e27e17b7SAlan Cox * Implements both kernel and user unwiring. 1920acd9a301SAlan Cox */ 1921acd9a301SAlan Cox int 1922acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1923abd498aaSBruce M Simpson int flags) 1924acd9a301SAlan Cox { 1925acd9a301SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 1926acd9a301SAlan Cox vm_offset_t saved_start; 1927acd9a301SAlan Cox unsigned int last_timestamp; 1928acd9a301SAlan Cox int rv; 1929abd498aaSBruce M Simpson boolean_t need_wakeup, result, user_unwire; 1930acd9a301SAlan Cox 1931abd498aaSBruce M Simpson user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1932acd9a301SAlan Cox vm_map_lock(map); 1933acd9a301SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 1934acd9a301SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 1935abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 1936cbef13d8SAlan Cox first_entry = first_entry->next; 1937abd498aaSBruce M Simpson else { 1938acd9a301SAlan Cox vm_map_unlock(map); 1939acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 1940acd9a301SAlan Cox } 1941abd498aaSBruce M Simpson } 1942acd9a301SAlan Cox last_timestamp = map->timestamp; 1943acd9a301SAlan Cox entry = first_entry; 1944acd9a301SAlan Cox while (entry != &map->header && entry->start < end) { 1945acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1946acd9a301SAlan Cox /* 1947acd9a301SAlan Cox * We have not yet clipped the entry. 1948acd9a301SAlan Cox */ 1949acd9a301SAlan Cox saved_start = (start >= entry->start) ? start : 1950acd9a301SAlan Cox entry->start; 1951acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 19528ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 1953acd9a301SAlan Cox /* 1954acd9a301SAlan Cox * Allow interruption of user unwiring? 1955acd9a301SAlan Cox */ 1956acd9a301SAlan Cox } 1957acd9a301SAlan Cox vm_map_lock(map); 1958acd9a301SAlan Cox if (last_timestamp+1 != map->timestamp) { 1959acd9a301SAlan Cox /* 1960acd9a301SAlan Cox * Look again for the entry because the map was 1961acd9a301SAlan Cox * modified while it was unlocked. 1962acd9a301SAlan Cox * Specifically, the entry may have been 1963acd9a301SAlan Cox * clipped, merged, or deleted. 1964acd9a301SAlan Cox */ 1965acd9a301SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 1966acd9a301SAlan Cox &tmp_entry)) { 1967cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 1968cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 1969cbef13d8SAlan Cox else { 1970acd9a301SAlan Cox if (saved_start == start) { 1971acd9a301SAlan Cox /* 1972acd9a301SAlan Cox * First_entry has been deleted. 1973acd9a301SAlan Cox */ 1974acd9a301SAlan Cox vm_map_unlock(map); 1975acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 1976acd9a301SAlan Cox } 1977acd9a301SAlan Cox end = saved_start; 1978acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 1979acd9a301SAlan Cox goto done; 1980acd9a301SAlan Cox } 1981cbef13d8SAlan Cox } 1982acd9a301SAlan Cox if (entry == first_entry) 1983acd9a301SAlan Cox first_entry = tmp_entry; 1984acd9a301SAlan Cox else 1985acd9a301SAlan Cox first_entry = NULL; 1986acd9a301SAlan Cox entry = tmp_entry; 1987acd9a301SAlan Cox } 1988acd9a301SAlan Cox last_timestamp = map->timestamp; 1989acd9a301SAlan Cox continue; 1990acd9a301SAlan Cox } 1991acd9a301SAlan Cox vm_map_clip_start(map, entry, start); 1992acd9a301SAlan Cox vm_map_clip_end(map, entry, end); 1993acd9a301SAlan Cox /* 1994acd9a301SAlan Cox * Mark the entry in case the map lock is released. (See 1995acd9a301SAlan Cox * above.) 1996acd9a301SAlan Cox */ 1997acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1998acd9a301SAlan Cox /* 1999acd9a301SAlan Cox * Check the map for holes in the specified region. 2000abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2001acd9a301SAlan Cox */ 2002abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2003abd498aaSBruce M Simpson (entry->end < end && (entry->next == &map->header || 2004abd498aaSBruce M Simpson entry->next->start > entry->end))) { 2005acd9a301SAlan Cox end = entry->end; 2006acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 2007acd9a301SAlan Cox goto done; 2008acd9a301SAlan Cox } 2009acd9a301SAlan Cox /* 20103ffbc0cdSAlan Cox * If system unwiring, require that the entry is system wired. 2011acd9a301SAlan Cox */ 20120ada205eSBrian Feldman if (!user_unwire && 20130ada205eSBrian Feldman vm_map_entry_system_wired_count(entry) == 0) { 2014acd9a301SAlan Cox end = entry->end; 2015acd9a301SAlan Cox rv = KERN_INVALID_ARGUMENT; 2016acd9a301SAlan Cox goto done; 2017acd9a301SAlan Cox } 2018acd9a301SAlan Cox entry = entry->next; 2019acd9a301SAlan Cox } 2020acd9a301SAlan Cox rv = KERN_SUCCESS; 2021acd9a301SAlan Cox done: 2022e27e17b7SAlan Cox need_wakeup = FALSE; 2023acd9a301SAlan Cox if (first_entry == NULL) { 2024acd9a301SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2025cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2026cbef13d8SAlan Cox first_entry = first_entry->next; 2027cbef13d8SAlan Cox else 2028acd9a301SAlan Cox KASSERT(result, ("vm_map_unwire: lookup failed")); 2029acd9a301SAlan Cox } 2030acd9a301SAlan Cox entry = first_entry; 2031acd9a301SAlan Cox while (entry != &map->header && entry->start < end) { 20323ffbc0cdSAlan Cox if (rv == KERN_SUCCESS && (!user_unwire || 20333ffbc0cdSAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED))) { 2034b2f3846aSAlan Cox if (user_unwire) 2035b2f3846aSAlan Cox entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2036b2f3846aSAlan Cox entry->wired_count--; 20370ada205eSBrian Feldman if (entry->wired_count == 0) { 2038b2f3846aSAlan Cox /* 2039b2f3846aSAlan Cox * Retain the map lock. 2040b2f3846aSAlan Cox */ 20414be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 20424be14af9SAlan Cox entry->object.vm_object != NULL && 20434be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 2044b2f3846aSAlan Cox } 2045b2f3846aSAlan Cox } 2046acd9a301SAlan Cox KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 2047acd9a301SAlan Cox ("vm_map_unwire: in-transition flag missing")); 2048acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 2049acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2050acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2051acd9a301SAlan Cox need_wakeup = TRUE; 2052acd9a301SAlan Cox } 2053acd9a301SAlan Cox vm_map_simplify_entry(map, entry); 2054acd9a301SAlan Cox entry = entry->next; 2055acd9a301SAlan Cox } 2056acd9a301SAlan Cox vm_map_unlock(map); 2057acd9a301SAlan Cox if (need_wakeup) 2058acd9a301SAlan Cox vm_map_wakeup(map); 2059acd9a301SAlan Cox return (rv); 2060acd9a301SAlan Cox } 2061acd9a301SAlan Cox 2062acd9a301SAlan Cox /* 2063e27e17b7SAlan Cox * vm_map_wire: 2064e27e17b7SAlan Cox * 2065e27e17b7SAlan Cox * Implements both kernel and user wiring. 2066e27e17b7SAlan Cox */ 2067e27e17b7SAlan Cox int 2068e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2069abd498aaSBruce M Simpson int flags) 2070e27e17b7SAlan Cox { 207112d7cc84SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 207212d7cc84SAlan Cox vm_offset_t saved_end, saved_start; 207312d7cc84SAlan Cox unsigned int last_timestamp; 207412d7cc84SAlan Cox int rv; 20754be14af9SAlan Cox boolean_t fictitious, need_wakeup, result, user_wire; 2076e27e17b7SAlan Cox 2077abd498aaSBruce M Simpson user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 207812d7cc84SAlan Cox vm_map_lock(map); 207912d7cc84SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 208012d7cc84SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 2081abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 2082cbef13d8SAlan Cox first_entry = first_entry->next; 2083abd498aaSBruce M Simpson else { 208412d7cc84SAlan Cox vm_map_unlock(map); 208512d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 208612d7cc84SAlan Cox } 2087abd498aaSBruce M Simpson } 208812d7cc84SAlan Cox last_timestamp = map->timestamp; 208912d7cc84SAlan Cox entry = first_entry; 209012d7cc84SAlan Cox while (entry != &map->header && entry->start < end) { 209112d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 209212d7cc84SAlan Cox /* 209312d7cc84SAlan Cox * We have not yet clipped the entry. 209412d7cc84SAlan Cox */ 209512d7cc84SAlan Cox saved_start = (start >= entry->start) ? start : 209612d7cc84SAlan Cox entry->start; 209712d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 20988ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 209912d7cc84SAlan Cox /* 210012d7cc84SAlan Cox * Allow interruption of user wiring? 210112d7cc84SAlan Cox */ 210212d7cc84SAlan Cox } 210312d7cc84SAlan Cox vm_map_lock(map); 210412d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 210512d7cc84SAlan Cox /* 210612d7cc84SAlan Cox * Look again for the entry because the map was 210712d7cc84SAlan Cox * modified while it was unlocked. 210812d7cc84SAlan Cox * Specifically, the entry may have been 210912d7cc84SAlan Cox * clipped, merged, or deleted. 211012d7cc84SAlan Cox */ 211112d7cc84SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 211212d7cc84SAlan Cox &tmp_entry)) { 2113cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2114cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2115cbef13d8SAlan Cox else { 211612d7cc84SAlan Cox if (saved_start == start) { 211712d7cc84SAlan Cox /* 211812d7cc84SAlan Cox * first_entry has been deleted. 211912d7cc84SAlan Cox */ 212012d7cc84SAlan Cox vm_map_unlock(map); 212112d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 212212d7cc84SAlan Cox } 212312d7cc84SAlan Cox end = saved_start; 212412d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 212512d7cc84SAlan Cox goto done; 212612d7cc84SAlan Cox } 2127cbef13d8SAlan Cox } 212812d7cc84SAlan Cox if (entry == first_entry) 212912d7cc84SAlan Cox first_entry = tmp_entry; 213012d7cc84SAlan Cox else 213112d7cc84SAlan Cox first_entry = NULL; 213212d7cc84SAlan Cox entry = tmp_entry; 213312d7cc84SAlan Cox } 213412d7cc84SAlan Cox last_timestamp = map->timestamp; 213512d7cc84SAlan Cox continue; 213612d7cc84SAlan Cox } 213712d7cc84SAlan Cox vm_map_clip_start(map, entry, start); 213812d7cc84SAlan Cox vm_map_clip_end(map, entry, end); 213912d7cc84SAlan Cox /* 214012d7cc84SAlan Cox * Mark the entry in case the map lock is released. (See 214112d7cc84SAlan Cox * above.) 214212d7cc84SAlan Cox */ 214312d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 214412d7cc84SAlan Cox /* 21450ada205eSBrian Feldman * 214612d7cc84SAlan Cox */ 21470ada205eSBrian Feldman if (entry->wired_count == 0) { 21480ada205eSBrian Feldman entry->wired_count++; 214912d7cc84SAlan Cox saved_start = entry->start; 215012d7cc84SAlan Cox saved_end = entry->end; 21514be14af9SAlan Cox fictitious = entry->object.vm_object != NULL && 21524be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE; 215312d7cc84SAlan Cox /* 215412d7cc84SAlan Cox * Release the map lock, relying on the in-transition 215512d7cc84SAlan Cox * mark. 215612d7cc84SAlan Cox */ 215712d7cc84SAlan Cox vm_map_unlock(map); 2158ef594d31SAlan Cox rv = vm_fault_wire(map, saved_start, saved_end, 21594be14af9SAlan Cox user_wire, fictitious); 216012d7cc84SAlan Cox vm_map_lock(map); 216112d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 216212d7cc84SAlan Cox /* 216312d7cc84SAlan Cox * Look again for the entry because the map was 216412d7cc84SAlan Cox * modified while it was unlocked. The entry 216512d7cc84SAlan Cox * may have been clipped, but NOT merged or 216612d7cc84SAlan Cox * deleted. 216712d7cc84SAlan Cox */ 216812d7cc84SAlan Cox result = vm_map_lookup_entry(map, saved_start, 216912d7cc84SAlan Cox &tmp_entry); 217012d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 217112d7cc84SAlan Cox if (entry == first_entry) 217212d7cc84SAlan Cox first_entry = tmp_entry; 217312d7cc84SAlan Cox else 217412d7cc84SAlan Cox first_entry = NULL; 217512d7cc84SAlan Cox entry = tmp_entry; 217628c58286SAlan Cox while (entry->end < saved_end) { 217728c58286SAlan Cox if (rv != KERN_SUCCESS) { 217828c58286SAlan Cox KASSERT(entry->wired_count == 1, 217928c58286SAlan Cox ("vm_map_wire: bad count")); 218028c58286SAlan Cox entry->wired_count = -1; 218128c58286SAlan Cox } 218212d7cc84SAlan Cox entry = entry->next; 218312d7cc84SAlan Cox } 218428c58286SAlan Cox } 218512d7cc84SAlan Cox last_timestamp = map->timestamp; 218612d7cc84SAlan Cox if (rv != KERN_SUCCESS) { 218728c58286SAlan Cox KASSERT(entry->wired_count == 1, 218828c58286SAlan Cox ("vm_map_wire: bad count")); 218912d7cc84SAlan Cox /* 219028c58286SAlan Cox * Assign an out-of-range value to represent 219128c58286SAlan Cox * the failure to wire this entry. 219212d7cc84SAlan Cox */ 219328c58286SAlan Cox entry->wired_count = -1; 219412d7cc84SAlan Cox end = entry->end; 219512d7cc84SAlan Cox goto done; 219612d7cc84SAlan Cox } 21970ada205eSBrian Feldman } else if (!user_wire || 21980ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 21990ada205eSBrian Feldman entry->wired_count++; 220012d7cc84SAlan Cox } 220112d7cc84SAlan Cox /* 220212d7cc84SAlan Cox * Check the map for holes in the specified region. 2203abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 220412d7cc84SAlan Cox */ 2205abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2206abd498aaSBruce M Simpson (entry->end < end && (entry->next == &map->header || 2207abd498aaSBruce M Simpson entry->next->start > entry->end))) { 220812d7cc84SAlan Cox end = entry->end; 220912d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 221012d7cc84SAlan Cox goto done; 221112d7cc84SAlan Cox } 221212d7cc84SAlan Cox entry = entry->next; 221312d7cc84SAlan Cox } 221412d7cc84SAlan Cox rv = KERN_SUCCESS; 221512d7cc84SAlan Cox done: 221612d7cc84SAlan Cox need_wakeup = FALSE; 221712d7cc84SAlan Cox if (first_entry == NULL) { 221812d7cc84SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2219cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2220cbef13d8SAlan Cox first_entry = first_entry->next; 2221cbef13d8SAlan Cox else 222212d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 222312d7cc84SAlan Cox } 222412d7cc84SAlan Cox entry = first_entry; 222512d7cc84SAlan Cox while (entry != &map->header && entry->start < end) { 222612d7cc84SAlan Cox if (rv == KERN_SUCCESS) { 222712d7cc84SAlan Cox if (user_wire) 222812d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_USER_WIRED; 222928c58286SAlan Cox } else if (entry->wired_count == -1) { 223028c58286SAlan Cox /* 223128c58286SAlan Cox * Wiring failed on this entry. Thus, unwiring is 223228c58286SAlan Cox * unnecessary. 223328c58286SAlan Cox */ 223428c58286SAlan Cox entry->wired_count = 0; 223512d7cc84SAlan Cox } else { 22360ada205eSBrian Feldman if (!user_wire || 22370ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 223812d7cc84SAlan Cox entry->wired_count--; 22390ada205eSBrian Feldman if (entry->wired_count == 0) { 224012d7cc84SAlan Cox /* 224112d7cc84SAlan Cox * Retain the map lock. 224212d7cc84SAlan Cox */ 22434be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 22444be14af9SAlan Cox entry->object.vm_object != NULL && 22454be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 224612d7cc84SAlan Cox } 224712d7cc84SAlan Cox } 224812d7cc84SAlan Cox KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 224912d7cc84SAlan Cox ("vm_map_wire: in-transition flag missing")); 225012d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 225112d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 225212d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 225312d7cc84SAlan Cox need_wakeup = TRUE; 225412d7cc84SAlan Cox } 225512d7cc84SAlan Cox vm_map_simplify_entry(map, entry); 225612d7cc84SAlan Cox entry = entry->next; 225712d7cc84SAlan Cox } 225812d7cc84SAlan Cox vm_map_unlock(map); 225912d7cc84SAlan Cox if (need_wakeup) 226012d7cc84SAlan Cox vm_map_wakeup(map); 226112d7cc84SAlan Cox return (rv); 2262e27e17b7SAlan Cox } 2263e27e17b7SAlan Cox 2264e27e17b7SAlan Cox /* 2265950f8459SAlan Cox * vm_map_sync 2266df8bae1dSRodney W. Grimes * 2267df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 2268df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 2269df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 2270df8bae1dSRodney W. Grimes * 2271637315edSAlan Cox * If the size of the region from start to end is zero, we are 2272637315edSAlan Cox * supposed to flush all modified pages within the region containing 2273637315edSAlan Cox * start. Unfortunately, a region can be split or coalesced with 2274637315edSAlan Cox * neighboring regions, making it difficult to determine what the 2275637315edSAlan Cox * original region was. Therefore, we approximate this requirement by 2276637315edSAlan Cox * flushing the current region containing start. 2277637315edSAlan Cox * 2278df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 2279df8bae1dSRodney W. Grimes */ 2280df8bae1dSRodney W. Grimes int 2281950f8459SAlan Cox vm_map_sync( 22821b40f8c0SMatthew Dillon vm_map_t map, 22831b40f8c0SMatthew Dillon vm_offset_t start, 22841b40f8c0SMatthew Dillon vm_offset_t end, 22851b40f8c0SMatthew Dillon boolean_t syncio, 22861b40f8c0SMatthew Dillon boolean_t invalidate) 2287df8bae1dSRodney W. Grimes { 2288c0877f10SJohn Dyson vm_map_entry_t current; 2289df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2290df8bae1dSRodney W. Grimes vm_size_t size; 2291df8bae1dSRodney W. Grimes vm_object_t object; 2292a316d390SJohn Dyson vm_ooffset_t offset; 2293df8bae1dSRodney W. Grimes 2294df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2295df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2296df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 2297df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2298df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2299637315edSAlan Cox } else if (start == end) { 2300637315edSAlan Cox start = entry->start; 2301637315edSAlan Cox end = entry->end; 2302df8bae1dSRodney W. Grimes } 2303df8bae1dSRodney W. Grimes /* 2304b7b7cd44SAlan Cox * Make a first pass to check for user-wired memory and holes. 2305df8bae1dSRodney W. Grimes */ 23067b0e72d1SAlan Cox for (current = entry; current != &map->header && current->start < end; 23077b0e72d1SAlan Cox current = current->next) { 2308b7b7cd44SAlan Cox if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2309df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2310df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2311df8bae1dSRodney W. Grimes } 2312df8bae1dSRodney W. Grimes if (end > current->end && 2313df8bae1dSRodney W. Grimes (current->next == &map->header || 2314df8bae1dSRodney W. Grimes current->end != current->next->start)) { 2315df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2316df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2317df8bae1dSRodney W. Grimes } 2318df8bae1dSRodney W. Grimes } 2319df8bae1dSRodney W. Grimes 23202cf13952SAlan Cox if (invalidate) 2321bc105a67SAlan Cox pmap_remove(map->pmap, start, end); 23222cf13952SAlan Cox 2323df8bae1dSRodney W. Grimes /* 2324df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 2325df8bae1dSRodney W. Grimes * objects as we go. 2326df8bae1dSRodney W. Grimes */ 23277b0e72d1SAlan Cox for (current = entry; current != &map->header && current->start < end; 23287b0e72d1SAlan Cox current = current->next) { 2329df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 2330df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 23319fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2332c0877f10SJohn Dyson vm_map_t smap; 2333df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 2334df8bae1dSRodney W. Grimes vm_size_t tsize; 2335df8bae1dSRodney W. Grimes 23369fdfe602SMatthew Dillon smap = current->object.sub_map; 2337df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 2338df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 2339df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 2340df8bae1dSRodney W. Grimes if (tsize < size) 2341df8bae1dSRodney W. Grimes size = tsize; 2342df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 2343df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 2344df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 2345df8bae1dSRodney W. Grimes } else { 2346df8bae1dSRodney W. Grimes object = current->object.vm_object; 2347df8bae1dSRodney W. Grimes } 2348950f8459SAlan Cox vm_object_sync(object, offset, size, syncio, invalidate); 2349df8bae1dSRodney W. Grimes start += size; 2350df8bae1dSRodney W. Grimes } 2351df8bae1dSRodney W. Grimes 2352df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2353df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2354df8bae1dSRodney W. Grimes } 2355df8bae1dSRodney W. Grimes 2356df8bae1dSRodney W. Grimes /* 2357df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 2358df8bae1dSRodney W. Grimes * 2359df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 2360df8bae1dSRodney W. Grimes * 2361df8bae1dSRodney W. Grimes * The map in question should be locked. 2362df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 2363df8bae1dSRodney W. Grimes */ 23640362d7d7SJohn Dyson static void 23651b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2366df8bae1dSRodney W. Grimes { 23674be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 23684be14af9SAlan Cox entry->object.vm_object != NULL && 23694be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 2370df8bae1dSRodney W. Grimes entry->wired_count = 0; 2371df8bae1dSRodney W. Grimes } 2372df8bae1dSRodney W. Grimes 2373df8bae1dSRodney W. Grimes /* 2374df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 2375df8bae1dSRodney W. Grimes * 2376df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 2377df8bae1dSRodney W. Grimes */ 23780362d7d7SJohn Dyson static void 23791b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2380df8bae1dSRodney W. Grimes { 238132a89c32SAlan Cox vm_object_t object; 238232a89c32SAlan Cox vm_pindex_t offidxstart, offidxend, count; 238332a89c32SAlan Cox 2384df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 2385df8bae1dSRodney W. Grimes map->size -= entry->end - entry->start; 2386df8bae1dSRodney W. Grimes 238732a89c32SAlan Cox if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 238832a89c32SAlan Cox (object = entry->object.vm_object) != NULL) { 238932a89c32SAlan Cox count = OFF_TO_IDX(entry->end - entry->start); 239032a89c32SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 239132a89c32SAlan Cox offidxend = offidxstart + count; 239232a89c32SAlan Cox VM_OBJECT_LOCK(object); 239332a89c32SAlan Cox if (object->ref_count != 1 && 239432a89c32SAlan Cox ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 23959f5c801bSAlan Cox object == kernel_object || object == kmem_object)) { 239632a89c32SAlan Cox vm_object_collapse(object); 2397b103b948SDag-Erling Smørgrav vm_object_page_remove(object, offidxstart, offidxend, FALSE); 239832a89c32SAlan Cox if (object->type == OBJT_SWAP) 239932a89c32SAlan Cox swap_pager_freespace(object, offidxstart, count); 240032a89c32SAlan Cox if (offidxend >= object->size && 240132a89c32SAlan Cox offidxstart < object->size) 240232a89c32SAlan Cox object->size = offidxstart; 240332a89c32SAlan Cox } 240432a89c32SAlan Cox VM_OBJECT_UNLOCK(object); 240532a89c32SAlan Cox vm_object_deallocate(object); 2406b5b40fa6SJohn Dyson } 2407df8bae1dSRodney W. Grimes 2408df8bae1dSRodney W. Grimes vm_map_entry_dispose(map, entry); 2409df8bae1dSRodney W. Grimes } 2410df8bae1dSRodney W. Grimes 2411df8bae1dSRodney W. Grimes /* 2412df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 2413df8bae1dSRodney W. Grimes * 2414df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 2415df8bae1dSRodney W. Grimes * map. 2416df8bae1dSRodney W. Grimes */ 2417df8bae1dSRodney W. Grimes int 24181b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2419df8bae1dSRodney W. Grimes { 2420c0877f10SJohn Dyson vm_map_entry_t entry; 2421df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 2422df8bae1dSRodney W. Grimes 2423df8bae1dSRodney W. Grimes /* 2424df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 2425df8bae1dSRodney W. Grimes */ 2426876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 2427df8bae1dSRodney W. Grimes entry = first_entry->next; 2428876318ecSAlan Cox else { 2429df8bae1dSRodney W. Grimes entry = first_entry; 2430df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 2431df8bae1dSRodney W. Grimes } 2432df8bae1dSRodney W. Grimes 2433df8bae1dSRodney W. Grimes /* 2434df8bae1dSRodney W. Grimes * Step through all entries in this region 2435df8bae1dSRodney W. Grimes */ 2436df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 2437df8bae1dSRodney W. Grimes vm_map_entry_t next; 2438df8bae1dSRodney W. Grimes 243973b2baceSAlan Cox /* 244073b2baceSAlan Cox * Wait for wiring or unwiring of an entry to complete. 24417c938963SBrian Feldman * Also wait for any system wirings to disappear on 24427c938963SBrian Feldman * user maps. 244373b2baceSAlan Cox */ 24447c938963SBrian Feldman if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 24457c938963SBrian Feldman (vm_map_pmap(map) != kernel_pmap && 24467c938963SBrian Feldman vm_map_entry_system_wired_count(entry) != 0)) { 244773b2baceSAlan Cox unsigned int last_timestamp; 244873b2baceSAlan Cox vm_offset_t saved_start; 244973b2baceSAlan Cox vm_map_entry_t tmp_entry; 245073b2baceSAlan Cox 245173b2baceSAlan Cox saved_start = entry->start; 245273b2baceSAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 245373b2baceSAlan Cox last_timestamp = map->timestamp; 24548ce2d00aSPawel Jakub Dawidek (void) vm_map_unlock_and_wait(map, 0); 245573b2baceSAlan Cox vm_map_lock(map); 245673b2baceSAlan Cox if (last_timestamp + 1 != map->timestamp) { 245773b2baceSAlan Cox /* 245873b2baceSAlan Cox * Look again for the entry because the map was 245973b2baceSAlan Cox * modified while it was unlocked. 246073b2baceSAlan Cox * Specifically, the entry may have been 246173b2baceSAlan Cox * clipped, merged, or deleted. 246273b2baceSAlan Cox */ 246373b2baceSAlan Cox if (!vm_map_lookup_entry(map, saved_start, 246473b2baceSAlan Cox &tmp_entry)) 246573b2baceSAlan Cox entry = tmp_entry->next; 246673b2baceSAlan Cox else { 246773b2baceSAlan Cox entry = tmp_entry; 246873b2baceSAlan Cox vm_map_clip_start(map, entry, 246973b2baceSAlan Cox saved_start); 247073b2baceSAlan Cox } 247173b2baceSAlan Cox } 247273b2baceSAlan Cox continue; 247373b2baceSAlan Cox } 2474df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 2475df8bae1dSRodney W. Grimes 2476c0877f10SJohn Dyson next = entry->next; 2477df8bae1dSRodney W. Grimes 2478df8bae1dSRodney W. Grimes /* 24790d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 24800d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 2481df8bae1dSRodney W. Grimes */ 2482c0877f10SJohn Dyson if (entry->wired_count != 0) { 2483df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 2484c0877f10SJohn Dyson } 2485df8bae1dSRodney W. Grimes 248632a89c32SAlan Cox pmap_remove(map->pmap, entry->start, entry->end); 2487df8bae1dSRodney W. Grimes 2488df8bae1dSRodney W. Grimes /* 24890d94caffSDavid Greenman * Delete the entry (which may delete the object) only after 24900d94caffSDavid Greenman * removing all pmap entries pointing to its pages. 24910d94caffSDavid Greenman * (Otherwise, its page frames may be reallocated, and any 24920d94caffSDavid Greenman * modify bits will be set in the wrong object!) 2493df8bae1dSRodney W. Grimes */ 2494df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 2495df8bae1dSRodney W. Grimes entry = next; 2496df8bae1dSRodney W. Grimes } 2497df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2498df8bae1dSRodney W. Grimes } 2499df8bae1dSRodney W. Grimes 2500df8bae1dSRodney W. Grimes /* 2501df8bae1dSRodney W. Grimes * vm_map_remove: 2502df8bae1dSRodney W. Grimes * 2503df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 2504df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 2505df8bae1dSRodney W. Grimes */ 2506df8bae1dSRodney W. Grimes int 25071b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2508df8bae1dSRodney W. Grimes { 25096eaee3feSAlan Cox int result; 2510df8bae1dSRodney W. Grimes 2511df8bae1dSRodney W. Grimes vm_map_lock(map); 2512df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2513df8bae1dSRodney W. Grimes result = vm_map_delete(map, start, end); 2514df8bae1dSRodney W. Grimes vm_map_unlock(map); 2515df8bae1dSRodney W. Grimes return (result); 2516df8bae1dSRodney W. Grimes } 2517df8bae1dSRodney W. Grimes 2518df8bae1dSRodney W. Grimes /* 2519df8bae1dSRodney W. Grimes * vm_map_check_protection: 2520df8bae1dSRodney W. Grimes * 25212d5c7e45SMatthew Dillon * Assert that the target map allows the specified privilege on the 25222d5c7e45SMatthew Dillon * entire address region given. The entire region must be allocated. 25232d5c7e45SMatthew Dillon * 25242d5c7e45SMatthew Dillon * WARNING! This code does not and should not check whether the 25252d5c7e45SMatthew Dillon * contents of the region is accessible. For example a smaller file 25262d5c7e45SMatthew Dillon * might be mapped into a larger address space. 25272d5c7e45SMatthew Dillon * 25282d5c7e45SMatthew Dillon * NOTE! This code is also called by munmap(). 2529d8834602SAlan Cox * 2530d8834602SAlan Cox * The map must be locked. A read lock is sufficient. 2531df8bae1dSRodney W. Grimes */ 25320d94caffSDavid Greenman boolean_t 2533b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2534b9dcd593SBruce Evans vm_prot_t protection) 2535df8bae1dSRodney W. Grimes { 2536c0877f10SJohn Dyson vm_map_entry_t entry; 2537df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2538df8bae1dSRodney W. Grimes 2539d8834602SAlan Cox if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2540df8bae1dSRodney W. Grimes return (FALSE); 2541df8bae1dSRodney W. Grimes entry = tmp_entry; 2542df8bae1dSRodney W. Grimes 2543df8bae1dSRodney W. Grimes while (start < end) { 2544d8834602SAlan Cox if (entry == &map->header) 2545df8bae1dSRodney W. Grimes return (FALSE); 2546df8bae1dSRodney W. Grimes /* 2547df8bae1dSRodney W. Grimes * No holes allowed! 2548df8bae1dSRodney W. Grimes */ 2549d8834602SAlan Cox if (start < entry->start) 2550df8bae1dSRodney W. Grimes return (FALSE); 2551df8bae1dSRodney W. Grimes /* 2552df8bae1dSRodney W. Grimes * Check protection associated with entry. 2553df8bae1dSRodney W. Grimes */ 2554d8834602SAlan Cox if ((entry->protection & protection) != protection) 2555df8bae1dSRodney W. Grimes return (FALSE); 2556df8bae1dSRodney W. Grimes /* go to next entry */ 2557df8bae1dSRodney W. Grimes start = entry->end; 2558df8bae1dSRodney W. Grimes entry = entry->next; 2559df8bae1dSRodney W. Grimes } 2560df8bae1dSRodney W. Grimes return (TRUE); 2561df8bae1dSRodney W. Grimes } 2562df8bae1dSRodney W. Grimes 256386524867SJohn Dyson /* 2564df8bae1dSRodney W. Grimes * vm_map_copy_entry: 2565df8bae1dSRodney W. Grimes * 2566df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 2567df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 2568df8bae1dSRodney W. Grimes */ 2569f708ef1bSPoul-Henning Kamp static void 25701b40f8c0SMatthew Dillon vm_map_copy_entry( 25711b40f8c0SMatthew Dillon vm_map_t src_map, 25721b40f8c0SMatthew Dillon vm_map_t dst_map, 25731b40f8c0SMatthew Dillon vm_map_entry_t src_entry, 25741b40f8c0SMatthew Dillon vm_map_entry_t dst_entry) 2575df8bae1dSRodney W. Grimes { 2576c0877f10SJohn Dyson vm_object_t src_object; 2577c0877f10SJohn Dyson 25789fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2579df8bae1dSRodney W. Grimes return; 2580df8bae1dSRodney W. Grimes 2581df8bae1dSRodney W. Grimes if (src_entry->wired_count == 0) { 2582df8bae1dSRodney W. Grimes 2583df8bae1dSRodney W. Grimes /* 25840d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 25850d94caffSDavid Greenman * write-protected. 2586df8bae1dSRodney W. Grimes */ 2587afa07f7eSJohn Dyson if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2588df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 2589df8bae1dSRodney W. Grimes src_entry->start, 2590df8bae1dSRodney W. Grimes src_entry->end, 2591df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 2592df8bae1dSRodney W. Grimes } 2593b18bfc3dSJohn Dyson 2594df8bae1dSRodney W. Grimes /* 2595df8bae1dSRodney W. Grimes * Make a copy of the object. 2596df8bae1dSRodney W. Grimes */ 25978aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 2598a89c6258SAlan Cox VM_OBJECT_LOCK(src_object); 2599c0877f10SJohn Dyson if ((src_object->handle == NULL) && 2600c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 2601c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 2602c0877f10SJohn Dyson vm_object_collapse(src_object); 260396fb8cf2SJohn Dyson if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2604c5aaa06dSAlan Cox vm_object_split(src_entry); 2605c0877f10SJohn Dyson src_object = src_entry->object.vm_object; 2606a89c6258SAlan Cox } 2607a89c6258SAlan Cox } 2608b921a12bSAlan Cox vm_object_reference_locked(src_object); 2609069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2610e2479b4fSAlan Cox VM_OBJECT_UNLOCK(src_object); 2611c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 2612afa07f7eSJohn Dyson src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2613afa07f7eSJohn Dyson dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2614b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 2615b18bfc3dSJohn Dyson } else { 2616b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 2617b18bfc3dSJohn Dyson dst_entry->offset = 0; 2618b18bfc3dSJohn Dyson } 2619df8bae1dSRodney W. Grimes 2620df8bae1dSRodney W. Grimes pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2621df8bae1dSRodney W. Grimes dst_entry->end - dst_entry->start, src_entry->start); 26220d94caffSDavid Greenman } else { 2623df8bae1dSRodney W. Grimes /* 2624df8bae1dSRodney W. Grimes * Of course, wired down pages can't be set copy-on-write. 26250d94caffSDavid Greenman * Cause wired pages to be copied into the new map by 26260d94caffSDavid Greenman * simulating faults (the new pages are pageable) 2627df8bae1dSRodney W. Grimes */ 2628df8bae1dSRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2629df8bae1dSRodney W. Grimes } 2630df8bae1dSRodney W. Grimes } 2631df8bae1dSRodney W. Grimes 2632df8bae1dSRodney W. Grimes /* 26332a7be1b6SBrian Feldman * vmspace_map_entry_forked: 26342a7be1b6SBrian Feldman * Update the newly-forked vmspace each time a map entry is inherited 26352a7be1b6SBrian Feldman * or copied. The values for vm_dsize and vm_tsize are approximate 26362a7be1b6SBrian Feldman * (and mostly-obsolete ideas in the face of mmap(2) et al.) 26372a7be1b6SBrian Feldman */ 26382a7be1b6SBrian Feldman static void 26392a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 26402a7be1b6SBrian Feldman vm_map_entry_t entry) 26412a7be1b6SBrian Feldman { 26422a7be1b6SBrian Feldman vm_size_t entrysize; 26432a7be1b6SBrian Feldman vm_offset_t newend; 26442a7be1b6SBrian Feldman 26452a7be1b6SBrian Feldman entrysize = entry->end - entry->start; 26462a7be1b6SBrian Feldman vm2->vm_map.size += entrysize; 26472a7be1b6SBrian Feldman if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 26482a7be1b6SBrian Feldman vm2->vm_ssize += btoc(entrysize); 26492a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 26502a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 2651b351299cSAndrew Gallatin newend = MIN(entry->end, 26522a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 26532a7be1b6SBrian Feldman vm2->vm_dsize += btoc(newend - entry->start); 26542a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 26552a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 2656b351299cSAndrew Gallatin newend = MIN(entry->end, 26572a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 26582a7be1b6SBrian Feldman vm2->vm_tsize += btoc(newend - entry->start); 26592a7be1b6SBrian Feldman } 26602a7be1b6SBrian Feldman } 26612a7be1b6SBrian Feldman 26622a7be1b6SBrian Feldman /* 2663df8bae1dSRodney W. Grimes * vmspace_fork: 2664df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 2665df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 2666df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 2667df8bae1dSRodney W. Grimes * values on the regions in that map. 2668df8bae1dSRodney W. Grimes * 26692a7be1b6SBrian Feldman * XXX It might be worth coalescing the entries added to the new vmspace. 26702a7be1b6SBrian Feldman * 2671df8bae1dSRodney W. Grimes * The source map must not be locked. 2672df8bae1dSRodney W. Grimes */ 2673df8bae1dSRodney W. Grimes struct vmspace * 26741b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1) 2675df8bae1dSRodney W. Grimes { 2676c0877f10SJohn Dyson struct vmspace *vm2; 2677df8bae1dSRodney W. Grimes vm_map_t old_map = &vm1->vm_map; 2678df8bae1dSRodney W. Grimes vm_map_t new_map; 2679df8bae1dSRodney W. Grimes vm_map_entry_t old_entry; 2680df8bae1dSRodney W. Grimes vm_map_entry_t new_entry; 2681de5f6a77SJohn Dyson vm_object_t object; 2682df8bae1dSRodney W. Grimes 2683df8bae1dSRodney W. Grimes vm_map_lock(old_map); 2684df8bae1dSRodney W. Grimes 26852d8acc0fSJohn Dyson vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 268689b57fcfSKonstantin Belousov if (vm2 == NULL) 268789b57fcfSKonstantin Belousov goto unlock_and_return; 26882a7be1b6SBrian Feldman vm2->vm_taddr = vm1->vm_taddr; 26892a7be1b6SBrian Feldman vm2->vm_daddr = vm1->vm_daddr; 26902a7be1b6SBrian Feldman vm2->vm_maxsaddr = vm1->vm_maxsaddr; 2691df8bae1dSRodney W. Grimes new_map = &vm2->vm_map; /* XXX */ 269247221757SJohn Dyson new_map->timestamp = 1; 2693df8bae1dSRodney W. Grimes 2694df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 2695df8bae1dSRodney W. Grimes 2696df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 2697afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2698df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 2699df8bae1dSRodney W. Grimes 2700df8bae1dSRodney W. Grimes switch (old_entry->inheritance) { 2701df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2702df8bae1dSRodney W. Grimes break; 2703df8bae1dSRodney W. Grimes 2704df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 2705df8bae1dSRodney W. Grimes /* 2706fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 2707fed9a903SJohn Dyson */ 2708fed9a903SJohn Dyson object = old_entry->object.vm_object; 2709fed9a903SJohn Dyson if (object == NULL) { 2710fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 2711c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 2712fed9a903SJohn Dyson old_entry->object.vm_object = object; 271315d2d313SAlan Cox old_entry->offset = 0; 27149a2f6362SAlan Cox } 27159a2f6362SAlan Cox 27169a2f6362SAlan Cox /* 27179a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 27189a2f6362SAlan Cox * to insure that a shadow object is created. 27199a2f6362SAlan Cox */ 27209a2f6362SAlan Cox vm_object_reference(object); 27219a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 27225069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 27235069bf57SJohn Dyson &old_entry->offset, 2724c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 27255069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2726d30344bdSIan Dowse /* Transfer the second reference too. */ 2727d30344bdSIan Dowse vm_object_reference( 2728d30344bdSIan Dowse old_entry->object.vm_object); 2729d30344bdSIan Dowse vm_object_deallocate(object); 27305069bf57SJohn Dyson object = old_entry->object.vm_object; 2731fed9a903SJohn Dyson } 2732e2479b4fSAlan Cox VM_OBJECT_LOCK(object); 2733069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 2734e2479b4fSAlan Cox VM_OBJECT_UNLOCK(object); 2735fed9a903SJohn Dyson 2736fed9a903SJohn Dyson /* 2737ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 2738df8bae1dSRodney W. Grimes */ 2739df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2740df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2741028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2742df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2743df8bae1dSRodney W. Grimes 2744df8bae1dSRodney W. Grimes /* 27450d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 27460d94caffSDavid Greenman * inserting at the end of the new map. 2747df8bae1dSRodney W. Grimes */ 2748df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2749df8bae1dSRodney W. Grimes new_entry); 27502a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 2751df8bae1dSRodney W. Grimes 2752df8bae1dSRodney W. Grimes /* 2753df8bae1dSRodney W. Grimes * Update the physical map 2754df8bae1dSRodney W. Grimes */ 2755df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 2756df8bae1dSRodney W. Grimes new_entry->start, 2757df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 2758df8bae1dSRodney W. Grimes old_entry->start); 2759df8bae1dSRodney W. Grimes break; 2760df8bae1dSRodney W. Grimes 2761df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2762df8bae1dSRodney W. Grimes /* 2763df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 2764df8bae1dSRodney W. Grimes */ 2765df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2766df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2767028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2768df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2769df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 2770df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2771df8bae1dSRodney W. Grimes new_entry); 27722a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 2773bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 2774bd7e5f99SJohn Dyson new_entry); 2775df8bae1dSRodney W. Grimes break; 2776df8bae1dSRodney W. Grimes } 2777df8bae1dSRodney W. Grimes old_entry = old_entry->next; 2778df8bae1dSRodney W. Grimes } 277989b57fcfSKonstantin Belousov unlock_and_return: 2780df8bae1dSRodney W. Grimes vm_map_unlock(old_map); 2781df8bae1dSRodney W. Grimes 2782df8bae1dSRodney W. Grimes return (vm2); 2783df8bae1dSRodney W. Grimes } 2784df8bae1dSRodney W. Grimes 278594f7e29aSAlan Cox int 278694f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 278794f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 278894f7e29aSAlan Cox { 2789fd75d710SMarcel Moolenaar vm_map_entry_t new_entry, prev_entry; 2790fd75d710SMarcel Moolenaar vm_offset_t bot, top; 279194f7e29aSAlan Cox vm_size_t init_ssize; 2792fd75d710SMarcel Moolenaar int orient, rv; 279391d5354aSJohn Baldwin rlim_t vmemlim; 279494f7e29aSAlan Cox 2795fd75d710SMarcel Moolenaar /* 2796fd75d710SMarcel Moolenaar * The stack orientation is piggybacked with the cow argument. 2797fd75d710SMarcel Moolenaar * Extract it into orient and mask the cow argument so that we 2798fd75d710SMarcel Moolenaar * don't pass it around further. 2799fd75d710SMarcel Moolenaar * NOTE: We explicitly allow bi-directional stacks. 2800fd75d710SMarcel Moolenaar */ 2801fd75d710SMarcel Moolenaar orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 2802fd75d710SMarcel Moolenaar cow &= ~orient; 2803fd75d710SMarcel Moolenaar KASSERT(orient != 0, ("No stack grow direction")); 2804fd75d710SMarcel Moolenaar 280577bc7900SKonstantin Belousov if (addrbos < vm_map_min(map) || 280677bc7900SKonstantin Belousov addrbos > vm_map_max(map) || 280777bc7900SKonstantin Belousov addrbos + max_ssize < addrbos) 280894f7e29aSAlan Cox return (KERN_NO_SPACE); 2809fd75d710SMarcel Moolenaar 2810fd75d710SMarcel Moolenaar init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz; 281194f7e29aSAlan Cox 281291d5354aSJohn Baldwin PROC_LOCK(curthread->td_proc); 281391d5354aSJohn Baldwin vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM); 281491d5354aSJohn Baldwin PROC_UNLOCK(curthread->td_proc); 281591d5354aSJohn Baldwin 281694f7e29aSAlan Cox vm_map_lock(map); 281794f7e29aSAlan Cox 281894f7e29aSAlan Cox /* If addr is already mapped, no go */ 281994f7e29aSAlan Cox if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 282094f7e29aSAlan Cox vm_map_unlock(map); 282194f7e29aSAlan Cox return (KERN_NO_SPACE); 282294f7e29aSAlan Cox } 282394f7e29aSAlan Cox 2824a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 282591d5354aSJohn Baldwin if (map->size + init_ssize > vmemlim) { 2826a69ac174SMatthew Dillon vm_map_unlock(map); 2827a69ac174SMatthew Dillon return (KERN_NO_SPACE); 2828a69ac174SMatthew Dillon } 2829a69ac174SMatthew Dillon 2830fd75d710SMarcel Moolenaar /* 2831fd75d710SMarcel Moolenaar * If we can't accomodate max_ssize in the current mapping, no go. 2832fd75d710SMarcel Moolenaar * However, we need to be aware that subsequent user mappings might 2833fd75d710SMarcel Moolenaar * map into the space we have reserved for stack, and currently this 2834fd75d710SMarcel Moolenaar * space is not protected. 283594f7e29aSAlan Cox * 2836fd75d710SMarcel Moolenaar * Hopefully we will at least detect this condition when we try to 2837fd75d710SMarcel Moolenaar * grow the stack. 283894f7e29aSAlan Cox */ 283994f7e29aSAlan Cox if ((prev_entry->next != &map->header) && 284094f7e29aSAlan Cox (prev_entry->next->start < addrbos + max_ssize)) { 284194f7e29aSAlan Cox vm_map_unlock(map); 284294f7e29aSAlan Cox return (KERN_NO_SPACE); 284394f7e29aSAlan Cox } 284494f7e29aSAlan Cox 2845fd75d710SMarcel Moolenaar /* 2846fd75d710SMarcel Moolenaar * We initially map a stack of only init_ssize. We will grow as 2847fd75d710SMarcel Moolenaar * needed later. Depending on the orientation of the stack (i.e. 2848fd75d710SMarcel Moolenaar * the grow direction) we either map at the top of the range, the 2849fd75d710SMarcel Moolenaar * bottom of the range or in the middle. 285094f7e29aSAlan Cox * 2851fd75d710SMarcel Moolenaar * Note: we would normally expect prot and max to be VM_PROT_ALL, 2852fd75d710SMarcel Moolenaar * and cow to be 0. Possibly we should eliminate these as input 2853fd75d710SMarcel Moolenaar * parameters, and just pass these values here in the insert call. 285494f7e29aSAlan Cox */ 2855fd75d710SMarcel Moolenaar if (orient == MAP_STACK_GROWS_DOWN) 2856fd75d710SMarcel Moolenaar bot = addrbos + max_ssize - init_ssize; 2857fd75d710SMarcel Moolenaar else if (orient == MAP_STACK_GROWS_UP) 2858fd75d710SMarcel Moolenaar bot = addrbos; 2859fd75d710SMarcel Moolenaar else 2860fd75d710SMarcel Moolenaar bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 2861fd75d710SMarcel Moolenaar top = bot + init_ssize; 2862fd75d710SMarcel Moolenaar rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 286394f7e29aSAlan Cox 2864fd75d710SMarcel Moolenaar /* Now set the avail_ssize amount. */ 286594f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 286629b45e9eSAlan Cox if (prev_entry != &map->header) 2867fd75d710SMarcel Moolenaar vm_map_clip_end(map, prev_entry, bot); 2868fd75d710SMarcel Moolenaar new_entry = prev_entry->next; 2869fd75d710SMarcel Moolenaar if (new_entry->end != top || new_entry->start != bot) 287094f7e29aSAlan Cox panic("Bad entry start/end for new stack entry"); 2871b21a0008SMarcel Moolenaar 2872fd75d710SMarcel Moolenaar new_entry->avail_ssize = max_ssize - init_ssize; 2873fd75d710SMarcel Moolenaar if (orient & MAP_STACK_GROWS_DOWN) 2874fd75d710SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2875fd75d710SMarcel Moolenaar if (orient & MAP_STACK_GROWS_UP) 2876fd75d710SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_UP; 287794f7e29aSAlan Cox } 287894f7e29aSAlan Cox 287994f7e29aSAlan Cox vm_map_unlock(map); 288094f7e29aSAlan Cox return (rv); 288194f7e29aSAlan Cox } 288294f7e29aSAlan Cox 288394f7e29aSAlan Cox /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 288494f7e29aSAlan Cox * desired address is already mapped, or if we successfully grow 288594f7e29aSAlan Cox * the stack. Also returns KERN_SUCCESS if addr is outside the 288694f7e29aSAlan Cox * stack range (this is strange, but preserves compatibility with 288794f7e29aSAlan Cox * the grow function in vm_machdep.c). 288894f7e29aSAlan Cox */ 288994f7e29aSAlan Cox int 289094f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr) 289194f7e29aSAlan Cox { 2892b21a0008SMarcel Moolenaar vm_map_entry_t next_entry, prev_entry; 2893b21a0008SMarcel Moolenaar vm_map_entry_t new_entry, stack_entry; 289494f7e29aSAlan Cox struct vmspace *vm = p->p_vmspace; 289594f7e29aSAlan Cox vm_map_t map = &vm->vm_map; 289694f7e29aSAlan Cox vm_offset_t end; 2897b21a0008SMarcel Moolenaar size_t grow_amount, max_grow; 289891d5354aSJohn Baldwin rlim_t stacklim, vmemlim; 2899b21a0008SMarcel Moolenaar int is_procstack, rv; 290023955314SAlfred Perlstein 290194f7e29aSAlan Cox Retry: 290291d5354aSJohn Baldwin PROC_LOCK(p); 290391d5354aSJohn Baldwin stacklim = lim_cur(p, RLIMIT_STACK); 2904bfee999dSAlan Cox vmemlim = lim_cur(p, RLIMIT_VMEM); 290591d5354aSJohn Baldwin PROC_UNLOCK(p); 290691d5354aSJohn Baldwin 290794f7e29aSAlan Cox vm_map_lock_read(map); 290894f7e29aSAlan Cox 290994f7e29aSAlan Cox /* If addr is already in the entry range, no need to grow.*/ 291094f7e29aSAlan Cox if (vm_map_lookup_entry(map, addr, &prev_entry)) { 291194f7e29aSAlan Cox vm_map_unlock_read(map); 29120cddd8f0SMatthew Dillon return (KERN_SUCCESS); 291394f7e29aSAlan Cox } 291494f7e29aSAlan Cox 2915b21a0008SMarcel Moolenaar next_entry = prev_entry->next; 2916b21a0008SMarcel Moolenaar if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 2917b21a0008SMarcel Moolenaar /* 2918b21a0008SMarcel Moolenaar * This entry does not grow upwards. Since the address lies 2919b21a0008SMarcel Moolenaar * beyond this entry, the next entry (if one exists) has to 2920b21a0008SMarcel Moolenaar * be a downward growable entry. The entry list header is 2921b21a0008SMarcel Moolenaar * never a growable entry, so it suffices to check the flags. 292294f7e29aSAlan Cox */ 2923b21a0008SMarcel Moolenaar if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 292494f7e29aSAlan Cox vm_map_unlock_read(map); 29250cddd8f0SMatthew Dillon return (KERN_SUCCESS); 292694f7e29aSAlan Cox } 2927b21a0008SMarcel Moolenaar stack_entry = next_entry; 2928b21a0008SMarcel Moolenaar } else { 2929b21a0008SMarcel Moolenaar /* 2930b21a0008SMarcel Moolenaar * This entry grows upward. If the next entry does not at 2931b21a0008SMarcel Moolenaar * least grow downwards, this is the entry we need to grow. 2932b21a0008SMarcel Moolenaar * otherwise we have two possible choices and we have to 2933b21a0008SMarcel Moolenaar * select one. 2934b21a0008SMarcel Moolenaar */ 2935b21a0008SMarcel Moolenaar if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 2936b21a0008SMarcel Moolenaar /* 2937b21a0008SMarcel Moolenaar * We have two choices; grow the entry closest to 2938b21a0008SMarcel Moolenaar * the address to minimize the amount of growth. 2939b21a0008SMarcel Moolenaar */ 2940b21a0008SMarcel Moolenaar if (addr - prev_entry->end <= next_entry->start - addr) 2941b21a0008SMarcel Moolenaar stack_entry = prev_entry; 2942b21a0008SMarcel Moolenaar else 2943b21a0008SMarcel Moolenaar stack_entry = next_entry; 2944b21a0008SMarcel Moolenaar } else 2945b21a0008SMarcel Moolenaar stack_entry = prev_entry; 2946b21a0008SMarcel Moolenaar } 294794f7e29aSAlan Cox 2948b21a0008SMarcel Moolenaar if (stack_entry == next_entry) { 2949b21a0008SMarcel Moolenaar KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 2950b21a0008SMarcel Moolenaar KASSERT(addr < stack_entry->start, ("foo")); 2951b21a0008SMarcel Moolenaar end = (prev_entry != &map->header) ? prev_entry->end : 2952b21a0008SMarcel Moolenaar stack_entry->start - stack_entry->avail_ssize; 295394f7e29aSAlan Cox grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 2954b21a0008SMarcel Moolenaar max_grow = stack_entry->start - end; 2955b21a0008SMarcel Moolenaar } else { 2956b21a0008SMarcel Moolenaar KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 295708667f6dSMarcel Moolenaar KASSERT(addr >= stack_entry->end, ("foo")); 2958b21a0008SMarcel Moolenaar end = (next_entry != &map->header) ? next_entry->start : 2959b21a0008SMarcel Moolenaar stack_entry->end + stack_entry->avail_ssize; 2960fd75d710SMarcel Moolenaar grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 2961b21a0008SMarcel Moolenaar max_grow = end - stack_entry->end; 2962b21a0008SMarcel Moolenaar } 2963b21a0008SMarcel Moolenaar 296494f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 296594f7e29aSAlan Cox vm_map_unlock_read(map); 29660cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 296794f7e29aSAlan Cox } 296894f7e29aSAlan Cox 2969b21a0008SMarcel Moolenaar /* 2970b21a0008SMarcel Moolenaar * If there is no longer enough space between the entries nogo, and 2971b21a0008SMarcel Moolenaar * adjust the available space. Note: this should only happen if the 2972b21a0008SMarcel Moolenaar * user has mapped into the stack area after the stack was created, 2973b21a0008SMarcel Moolenaar * and is probably an error. 297494f7e29aSAlan Cox * 2975b21a0008SMarcel Moolenaar * This also effectively destroys any guard page the user might have 2976b21a0008SMarcel Moolenaar * intended by limiting the stack size. 297794f7e29aSAlan Cox */ 2978b21a0008SMarcel Moolenaar if (grow_amount > max_grow) { 297925adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 298094f7e29aSAlan Cox goto Retry; 298194f7e29aSAlan Cox 2982b21a0008SMarcel Moolenaar stack_entry->avail_ssize = max_grow; 298394f7e29aSAlan Cox 298494f7e29aSAlan Cox vm_map_unlock(map); 29850cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 298694f7e29aSAlan Cox } 298794f7e29aSAlan Cox 2988b21a0008SMarcel Moolenaar is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 298994f7e29aSAlan Cox 2990b21a0008SMarcel Moolenaar /* 2991b21a0008SMarcel Moolenaar * If this is the main process stack, see if we're over the stack 2992b21a0008SMarcel Moolenaar * limit. 299394f7e29aSAlan Cox */ 299491d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 299594f7e29aSAlan Cox vm_map_unlock_read(map); 29960cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 299794f7e29aSAlan Cox } 299894f7e29aSAlan Cox 299994f7e29aSAlan Cox /* Round up the grow amount modulo SGROWSIZ */ 3000cbc89bfbSPaul Saab grow_amount = roundup (grow_amount, sgrowsiz); 3001b21a0008SMarcel Moolenaar if (grow_amount > stack_entry->avail_ssize) 300294f7e29aSAlan Cox grow_amount = stack_entry->avail_ssize; 300391d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 300491d5354aSJohn Baldwin grow_amount = stacklim - ctob(vm->vm_ssize); 300594f7e29aSAlan Cox } 300694f7e29aSAlan Cox 3007a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 300891d5354aSJohn Baldwin if (map->size + grow_amount > vmemlim) { 3009a69ac174SMatthew Dillon vm_map_unlock_read(map); 3010a69ac174SMatthew Dillon return (KERN_NO_SPACE); 3011a69ac174SMatthew Dillon } 3012a69ac174SMatthew Dillon 301325adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 301494f7e29aSAlan Cox goto Retry; 301594f7e29aSAlan Cox 3016b21a0008SMarcel Moolenaar if (stack_entry == next_entry) { 3017b21a0008SMarcel Moolenaar /* 3018b21a0008SMarcel Moolenaar * Growing downward. 3019b21a0008SMarcel Moolenaar */ 302094f7e29aSAlan Cox /* Get the preliminary new entry start value */ 302194f7e29aSAlan Cox addr = stack_entry->start - grow_amount; 302294f7e29aSAlan Cox 3023b21a0008SMarcel Moolenaar /* 3024b21a0008SMarcel Moolenaar * If this puts us into the previous entry, cut back our 3025b21a0008SMarcel Moolenaar * growth to the available space. Also, see the note above. 302694f7e29aSAlan Cox */ 302794f7e29aSAlan Cox if (addr < end) { 3028b21a0008SMarcel Moolenaar stack_entry->avail_ssize = max_grow; 302994f7e29aSAlan Cox addr = end; 303094f7e29aSAlan Cox } 303194f7e29aSAlan Cox 303294f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 303305ba50f5SJake Burkholder p->p_sysent->sv_stackprot, VM_PROT_ALL, 0); 303494f7e29aSAlan Cox 303594f7e29aSAlan Cox /* Adjust the available stack space by the amount we grew. */ 303694f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 303729b45e9eSAlan Cox if (prev_entry != &map->header) 303829b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addr); 3039b21a0008SMarcel Moolenaar new_entry = prev_entry->next; 3040b21a0008SMarcel Moolenaar KASSERT(new_entry == stack_entry->prev, ("foo")); 3041b21a0008SMarcel Moolenaar KASSERT(new_entry->end == stack_entry->start, ("foo")); 3042b21a0008SMarcel Moolenaar KASSERT(new_entry->start == addr, ("foo")); 3043b21a0008SMarcel Moolenaar grow_amount = new_entry->end - new_entry->start; 3044b21a0008SMarcel Moolenaar new_entry->avail_ssize = stack_entry->avail_ssize - 3045b21a0008SMarcel Moolenaar grow_amount; 3046b21a0008SMarcel Moolenaar stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 3047b21a0008SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 304894f7e29aSAlan Cox } 3049b21a0008SMarcel Moolenaar } else { 3050b21a0008SMarcel Moolenaar /* 3051b21a0008SMarcel Moolenaar * Growing upward. 3052b21a0008SMarcel Moolenaar */ 3053b21a0008SMarcel Moolenaar addr = stack_entry->end + grow_amount; 3054b21a0008SMarcel Moolenaar 3055b21a0008SMarcel Moolenaar /* 3056b21a0008SMarcel Moolenaar * If this puts us into the next entry, cut back our growth 3057b21a0008SMarcel Moolenaar * to the available space. Also, see the note above. 3058b21a0008SMarcel Moolenaar */ 3059b21a0008SMarcel Moolenaar if (addr > end) { 3060b21a0008SMarcel Moolenaar stack_entry->avail_ssize = end - stack_entry->end; 3061b21a0008SMarcel Moolenaar addr = end; 306294f7e29aSAlan Cox } 306394f7e29aSAlan Cox 3064b21a0008SMarcel Moolenaar grow_amount = addr - stack_entry->end; 3065b21a0008SMarcel Moolenaar 3066b21a0008SMarcel Moolenaar /* Grow the underlying object if applicable. */ 3067b21a0008SMarcel Moolenaar if (stack_entry->object.vm_object == NULL || 3068b21a0008SMarcel Moolenaar vm_object_coalesce(stack_entry->object.vm_object, 306957a21abaSAlan Cox stack_entry->offset, 3070b21a0008SMarcel Moolenaar (vm_size_t)(stack_entry->end - stack_entry->start), 3071b21a0008SMarcel Moolenaar (vm_size_t)grow_amount)) { 307208667f6dSMarcel Moolenaar map->size += (addr - stack_entry->end); 3073b21a0008SMarcel Moolenaar /* Update the current entry. */ 3074b21a0008SMarcel Moolenaar stack_entry->end = addr; 3075199c91abSMarcel Moolenaar stack_entry->avail_ssize -= grow_amount; 30760164e057SAlan Cox vm_map_entry_resize_free(map, stack_entry); 3077b21a0008SMarcel Moolenaar rv = KERN_SUCCESS; 3078b21a0008SMarcel Moolenaar 3079b21a0008SMarcel Moolenaar if (next_entry != &map->header) 3080b21a0008SMarcel Moolenaar vm_map_clip_start(map, next_entry, addr); 3081b21a0008SMarcel Moolenaar } else 3082b21a0008SMarcel Moolenaar rv = KERN_FAILURE; 3083b21a0008SMarcel Moolenaar } 3084b21a0008SMarcel Moolenaar 3085b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && is_procstack) 3086b21a0008SMarcel Moolenaar vm->vm_ssize += btoc(grow_amount); 3087b21a0008SMarcel Moolenaar 308894f7e29aSAlan Cox vm_map_unlock(map); 3089b21a0008SMarcel Moolenaar 3090abd498aaSBruce M Simpson /* 3091abd498aaSBruce M Simpson * Heed the MAP_WIREFUTURE flag if it was set for this process. 3092abd498aaSBruce M Simpson */ 3093b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 3094b21a0008SMarcel Moolenaar vm_map_wire(map, 3095b21a0008SMarcel Moolenaar (stack_entry == next_entry) ? addr : addr - grow_amount, 3096b21a0008SMarcel Moolenaar (stack_entry == next_entry) ? stack_entry->start : addr, 3097b21a0008SMarcel Moolenaar (p->p_flag & P_SYSTEM) 3098b21a0008SMarcel Moolenaar ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 3099b21a0008SMarcel Moolenaar : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 3100b21a0008SMarcel Moolenaar } 3101abd498aaSBruce M Simpson 31020cddd8f0SMatthew Dillon return (rv); 310394f7e29aSAlan Cox } 310494f7e29aSAlan Cox 3105df8bae1dSRodney W. Grimes /* 31065856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 31075856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 31085856e12eSJohn Dyson */ 310989b57fcfSKonstantin Belousov int 31103ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 31111b40f8c0SMatthew Dillon { 31125856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 31135856e12eSJohn Dyson struct vmspace *newvmspace; 31145856e12eSJohn Dyson 31153ebc1248SPeter Wemm newvmspace = vmspace_alloc(minuser, maxuser); 311689b57fcfSKonstantin Belousov if (newvmspace == NULL) 311789b57fcfSKonstantin Belousov return (ENOMEM); 311851ab6c28SAlan Cox newvmspace->vm_swrss = oldvmspace->vm_swrss; 31195856e12eSJohn Dyson /* 31205856e12eSJohn Dyson * This code is written like this for prototype purposes. The 31215856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 31225856e12eSJohn Dyson * other process's that are still using the vmspace to finally 31235856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 31245856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 31255856e12eSJohn Dyson */ 312657051fdcSTor Egge PROC_VMSPACE_LOCK(p); 31275856e12eSJohn Dyson p->p_vmspace = newvmspace; 312857051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 31296617724cSJeff Roberson if (p == curthread->td_proc) 3130b40ce416SJulian Elischer pmap_activate(curthread); 3131b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 313289b57fcfSKonstantin Belousov return (0); 31335856e12eSJohn Dyson } 31345856e12eSJohn Dyson 31355856e12eSJohn Dyson /* 31365856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 31375856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 31385856e12eSJohn Dyson */ 313989b57fcfSKonstantin Belousov int 31401b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p) 31411b40f8c0SMatthew Dillon { 31425856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 31435856e12eSJohn Dyson struct vmspace *newvmspace; 31445856e12eSJohn Dyson 31455856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 314689b57fcfSKonstantin Belousov return (0); 31475856e12eSJohn Dyson newvmspace = vmspace_fork(oldvmspace); 314889b57fcfSKonstantin Belousov if (newvmspace == NULL) 314989b57fcfSKonstantin Belousov return (ENOMEM); 315057051fdcSTor Egge PROC_VMSPACE_LOCK(p); 31515856e12eSJohn Dyson p->p_vmspace = newvmspace; 315257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 31536617724cSJeff Roberson if (p == curthread->td_proc) 3154b40ce416SJulian Elischer pmap_activate(curthread); 3155b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 315689b57fcfSKonstantin Belousov return (0); 31575856e12eSJohn Dyson } 31585856e12eSJohn Dyson 31595856e12eSJohn Dyson /* 3160df8bae1dSRodney W. Grimes * vm_map_lookup: 3161df8bae1dSRodney W. Grimes * 3162df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 3163df8bae1dSRodney W. Grimes * protection for a given virtual address in the 3164df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 3165df8bae1dSRodney W. Grimes * type specified. 3166df8bae1dSRodney W. Grimes * 3167df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 3168df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 3169df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 3170df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 3171df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 3172df8bae1dSRodney W. Grimes * 3173df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 3174df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 3175df8bae1dSRodney W. Grimes * 3176df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 3177df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 3178df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 3179df8bae1dSRodney W. Grimes * remain the same. 3180df8bae1dSRodney W. Grimes */ 3181df8bae1dSRodney W. Grimes int 3182b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3183b9dcd593SBruce Evans vm_offset_t vaddr, 318447221757SJohn Dyson vm_prot_t fault_typea, 3185b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 3186b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 3187b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 3188b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 31892d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 3190df8bae1dSRodney W. Grimes { 3191c0877f10SJohn Dyson vm_map_entry_t entry; 3192c0877f10SJohn Dyson vm_map_t map = *var_map; 3193c0877f10SJohn Dyson vm_prot_t prot; 319447221757SJohn Dyson vm_prot_t fault_type = fault_typea; 3195df8bae1dSRodney W. Grimes 3196df8bae1dSRodney W. Grimes RetryLookup:; 3197df8bae1dSRodney W. Grimes 3198df8bae1dSRodney W. Grimes vm_map_lock_read(map); 3199df8bae1dSRodney W. Grimes 3200df8bae1dSRodney W. Grimes /* 32014c3ef59eSAlan Cox * Lookup the faulting address. 3202df8bae1dSRodney W. Grimes */ 3203095104acSAlan Cox if (!vm_map_lookup_entry(map, vaddr, out_entry)) { 3204095104acSAlan Cox vm_map_unlock_read(map); 3205095104acSAlan Cox return (KERN_INVALID_ADDRESS); 3206095104acSAlan Cox } 3207df8bae1dSRodney W. Grimes 32084e94f402SAlan Cox entry = *out_entry; 3209b7b2aac2SJohn Dyson 3210df8bae1dSRodney W. Grimes /* 3211df8bae1dSRodney W. Grimes * Handle submaps. 3212df8bae1dSRodney W. Grimes */ 3213afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3214df8bae1dSRodney W. Grimes vm_map_t old_map = map; 3215df8bae1dSRodney W. Grimes 3216df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 3217df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 3218df8bae1dSRodney W. Grimes goto RetryLookup; 3219df8bae1dSRodney W. Grimes } 3220a04c970aSJohn Dyson 3221df8bae1dSRodney W. Grimes /* 32220d94caffSDavid Greenman * Check whether this task is allowed to have this page. 3223a04c970aSJohn Dyson * Note the special case for MAP_ENTRY_COW 3224a04c970aSJohn Dyson * pages with an override. This is to implement a forced 3225a04c970aSJohn Dyson * COW for debuggers. 3226df8bae1dSRodney W. Grimes */ 3227480ba2f5SJohn Dyson if (fault_type & VM_PROT_OVERRIDE_WRITE) 3228480ba2f5SJohn Dyson prot = entry->max_protection; 3229480ba2f5SJohn Dyson else 3230df8bae1dSRodney W. Grimes prot = entry->protection; 323147221757SJohn Dyson fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 323247221757SJohn Dyson if ((fault_type & prot) != fault_type) { 3233095104acSAlan Cox vm_map_unlock_read(map); 3234095104acSAlan Cox return (KERN_PROTECTION_FAILURE); 323547221757SJohn Dyson } 32362ed14a92SAlan Cox if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 323747221757SJohn Dyson (entry->eflags & MAP_ENTRY_COW) && 32382ed14a92SAlan Cox (fault_type & VM_PROT_WRITE) && 323947221757SJohn Dyson (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 3240095104acSAlan Cox vm_map_unlock_read(map); 3241095104acSAlan Cox return (KERN_PROTECTION_FAILURE); 3242a04c970aSJohn Dyson } 3243df8bae1dSRodney W. Grimes 3244df8bae1dSRodney W. Grimes /* 32450d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 32460d94caffSDavid Greenman * accesses. 3247df8bae1dSRodney W. Grimes */ 324805f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 324905f0fdd2SPoul-Henning Kamp if (*wired) 3250df8bae1dSRodney W. Grimes prot = fault_type = entry->protection; 3251df8bae1dSRodney W. Grimes 3252df8bae1dSRodney W. Grimes /* 3253df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 3254df8bae1dSRodney W. Grimes */ 3255afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3256df8bae1dSRodney W. Grimes /* 32570d94caffSDavid Greenman * If we want to write the page, we may as well handle that 3258ad5fca3bSAlan Cox * now since we've got the map locked. 3259df8bae1dSRodney W. Grimes * 32600d94caffSDavid Greenman * If we don't need to write the page, we just demote the 32610d94caffSDavid Greenman * permissions allowed. 3262df8bae1dSRodney W. Grimes */ 3263df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 3264df8bae1dSRodney W. Grimes /* 32650d94caffSDavid Greenman * Make a new object, and place it in the object 32660d94caffSDavid Greenman * chain. Note that no new references have appeared 3267ad5fca3bSAlan Cox * -- one just moved from the map to the new 32680d94caffSDavid Greenman * object. 3269df8bae1dSRodney W. Grimes */ 327025adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 3271df8bae1dSRodney W. Grimes goto RetryLookup; 32729917e010SAlan Cox 3273df8bae1dSRodney W. Grimes vm_object_shadow( 3274df8bae1dSRodney W. Grimes &entry->object.vm_object, 3275df8bae1dSRodney W. Grimes &entry->offset, 3276c2e11a03SJohn Dyson atop(entry->end - entry->start)); 3277afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 32789917e010SAlan Cox 32799b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 32800d94caffSDavid Greenman } else { 3281df8bae1dSRodney W. Grimes /* 32820d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 32830d94caffSDavid Greenman * don't allow writes. 3284df8bae1dSRodney W. Grimes */ 32852d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 3286df8bae1dSRodney W. Grimes } 3287df8bae1dSRodney W. Grimes } 32882d8acc0fSJohn Dyson 3289df8bae1dSRodney W. Grimes /* 3290df8bae1dSRodney W. Grimes * Create an object if necessary. 3291df8bae1dSRodney W. Grimes */ 32924e71e795SMatthew Dillon if (entry->object.vm_object == NULL && 32934e71e795SMatthew Dillon !map->system_map) { 329425adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 3295df8bae1dSRodney W. Grimes goto RetryLookup; 329624a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 3297c2e11a03SJohn Dyson atop(entry->end - entry->start)); 3298df8bae1dSRodney W. Grimes entry->offset = 0; 32999b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 3300df8bae1dSRodney W. Grimes } 3301b5b40fa6SJohn Dyson 3302df8bae1dSRodney W. Grimes /* 33030d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 33040d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 3305df8bae1dSRodney W. Grimes */ 33069b09b6c7SMatthew Dillon *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3307df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 3308df8bae1dSRodney W. Grimes 3309df8bae1dSRodney W. Grimes *out_prot = prot; 3310df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 3311df8bae1dSRodney W. Grimes } 3312df8bae1dSRodney W. Grimes 3313df8bae1dSRodney W. Grimes /* 331419dc5607STor Egge * vm_map_lookup_locked: 331519dc5607STor Egge * 331619dc5607STor Egge * Lookup the faulting address. A version of vm_map_lookup that returns 331719dc5607STor Egge * KERN_FAILURE instead of blocking on map lock or memory allocation. 331819dc5607STor Egge */ 331919dc5607STor Egge int 332019dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 332119dc5607STor Egge vm_offset_t vaddr, 332219dc5607STor Egge vm_prot_t fault_typea, 332319dc5607STor Egge vm_map_entry_t *out_entry, /* OUT */ 332419dc5607STor Egge vm_object_t *object, /* OUT */ 332519dc5607STor Egge vm_pindex_t *pindex, /* OUT */ 332619dc5607STor Egge vm_prot_t *out_prot, /* OUT */ 332719dc5607STor Egge boolean_t *wired) /* OUT */ 332819dc5607STor Egge { 332919dc5607STor Egge vm_map_entry_t entry; 333019dc5607STor Egge vm_map_t map = *var_map; 333119dc5607STor Egge vm_prot_t prot; 333219dc5607STor Egge vm_prot_t fault_type = fault_typea; 333319dc5607STor Egge 333419dc5607STor Egge /* 33354c3ef59eSAlan Cox * Lookup the faulting address. 333619dc5607STor Egge */ 333719dc5607STor Egge if (!vm_map_lookup_entry(map, vaddr, out_entry)) 333819dc5607STor Egge return (KERN_INVALID_ADDRESS); 333919dc5607STor Egge 334019dc5607STor Egge entry = *out_entry; 334119dc5607STor Egge 334219dc5607STor Egge /* 334319dc5607STor Egge * Fail if the entry refers to a submap. 334419dc5607STor Egge */ 334519dc5607STor Egge if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 334619dc5607STor Egge return (KERN_FAILURE); 334719dc5607STor Egge 334819dc5607STor Egge /* 334919dc5607STor Egge * Check whether this task is allowed to have this page. 335019dc5607STor Egge * Note the special case for MAP_ENTRY_COW 335119dc5607STor Egge * pages with an override. This is to implement a forced 335219dc5607STor Egge * COW for debuggers. 335319dc5607STor Egge */ 335419dc5607STor Egge if (fault_type & VM_PROT_OVERRIDE_WRITE) 335519dc5607STor Egge prot = entry->max_protection; 335619dc5607STor Egge else 335719dc5607STor Egge prot = entry->protection; 335819dc5607STor Egge fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 335919dc5607STor Egge if ((fault_type & prot) != fault_type) 336019dc5607STor Egge return (KERN_PROTECTION_FAILURE); 336119dc5607STor Egge if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 336219dc5607STor Egge (entry->eflags & MAP_ENTRY_COW) && 336319dc5607STor Egge (fault_type & VM_PROT_WRITE) && 336419dc5607STor Egge (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) 336519dc5607STor Egge return (KERN_PROTECTION_FAILURE); 336619dc5607STor Egge 336719dc5607STor Egge /* 336819dc5607STor Egge * If this page is not pageable, we have to get it for all possible 336919dc5607STor Egge * accesses. 337019dc5607STor Egge */ 337119dc5607STor Egge *wired = (entry->wired_count != 0); 337219dc5607STor Egge if (*wired) 337319dc5607STor Egge prot = fault_type = entry->protection; 337419dc5607STor Egge 337519dc5607STor Egge if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 337619dc5607STor Egge /* 337719dc5607STor Egge * Fail if the entry was copy-on-write for a write fault. 337819dc5607STor Egge */ 337919dc5607STor Egge if (fault_type & VM_PROT_WRITE) 338019dc5607STor Egge return (KERN_FAILURE); 338119dc5607STor Egge /* 338219dc5607STor Egge * We're attempting to read a copy-on-write page -- 338319dc5607STor Egge * don't allow writes. 338419dc5607STor Egge */ 338519dc5607STor Egge prot &= ~VM_PROT_WRITE; 338619dc5607STor Egge } 338719dc5607STor Egge 338819dc5607STor Egge /* 338919dc5607STor Egge * Fail if an object should be created. 339019dc5607STor Egge */ 339119dc5607STor Egge if (entry->object.vm_object == NULL && !map->system_map) 339219dc5607STor Egge return (KERN_FAILURE); 339319dc5607STor Egge 339419dc5607STor Egge /* 339519dc5607STor Egge * Return the object/offset from this entry. If the entry was 339619dc5607STor Egge * copy-on-write or empty, it has been fixed up. 339719dc5607STor Egge */ 339819dc5607STor Egge *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 339919dc5607STor Egge *object = entry->object.vm_object; 340019dc5607STor Egge 340119dc5607STor Egge *out_prot = prot; 340219dc5607STor Egge return (KERN_SUCCESS); 340319dc5607STor Egge } 340419dc5607STor Egge 340519dc5607STor Egge /* 3406df8bae1dSRodney W. Grimes * vm_map_lookup_done: 3407df8bae1dSRodney W. Grimes * 3408df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 3409df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 3410df8bae1dSRodney W. Grimes */ 34110d94caffSDavid Greenman void 34121b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 3413df8bae1dSRodney W. Grimes { 3414df8bae1dSRodney W. Grimes /* 3415df8bae1dSRodney W. Grimes * Unlock the main-level map 3416df8bae1dSRodney W. Grimes */ 3417df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3418df8bae1dSRodney W. Grimes } 3419df8bae1dSRodney W. Grimes 3420c7c34a24SBruce Evans #include "opt_ddb.h" 3421c3cb3e12SDavid Greenman #ifdef DDB 3422c7c34a24SBruce Evans #include <sys/kernel.h> 3423c7c34a24SBruce Evans 3424c7c34a24SBruce Evans #include <ddb/ddb.h> 3425c7c34a24SBruce Evans 3426df8bae1dSRodney W. Grimes /* 3427df8bae1dSRodney W. Grimes * vm_map_print: [ debug ] 3428df8bae1dSRodney W. Grimes */ 3429c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print) 3430df8bae1dSRodney W. Grimes { 343195e5e988SJohn Dyson static int nlines; 3432c7c34a24SBruce Evans /* XXX convert args. */ 3433c0877f10SJohn Dyson vm_map_t map = (vm_map_t)addr; 3434c7c34a24SBruce Evans boolean_t full = have_addr; 3435df8bae1dSRodney W. Grimes 3436c0877f10SJohn Dyson vm_map_entry_t entry; 3437c7c34a24SBruce Evans 3438e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3439e5f251d2SAlan Cox (void *)map, 3440101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 344195e5e988SJohn Dyson nlines++; 3442df8bae1dSRodney W. Grimes 3443c7c34a24SBruce Evans if (!full && db_indent) 3444df8bae1dSRodney W. Grimes return; 3445df8bae1dSRodney W. Grimes 3446c7c34a24SBruce Evans db_indent += 2; 3447df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 3448df8bae1dSRodney W. Grimes entry = entry->next) { 3449fc62ef1fSBruce Evans db_iprintf("map entry %p: start=%p, end=%p\n", 3450fc62ef1fSBruce Evans (void *)entry, (void *)entry->start, (void *)entry->end); 345195e5e988SJohn Dyson nlines++; 3452e5f251d2SAlan Cox { 3453df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 3454df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 34550d94caffSDavid Greenman 345695e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 3457df8bae1dSRodney W. Grimes entry->protection, 3458df8bae1dSRodney W. Grimes entry->max_protection, 34598aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 3460df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 346195e5e988SJohn Dyson db_printf(", wired"); 3462df8bae1dSRodney W. Grimes } 34639fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3464cd034a5bSMaxime Henrion db_printf(", share=%p, offset=0x%jx\n", 34659fdfe602SMatthew Dillon (void *)entry->object.sub_map, 3466cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 346795e5e988SJohn Dyson nlines++; 3468df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 34699fdfe602SMatthew Dillon (entry->prev->object.sub_map != 34709fdfe602SMatthew Dillon entry->object.sub_map)) { 3471c7c34a24SBruce Evans db_indent += 2; 3472101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t) 34739fdfe602SMatthew Dillon entry->object.sub_map, 3474914181e7SBruce Evans full, 0, (char *)0); 3475c7c34a24SBruce Evans db_indent -= 2; 3476df8bae1dSRodney W. Grimes } 34770d94caffSDavid Greenman } else { 3478cd034a5bSMaxime Henrion db_printf(", object=%p, offset=0x%jx", 3479101eeb7fSBruce Evans (void *)entry->object.vm_object, 3480cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 3481afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 3482c7c34a24SBruce Evans db_printf(", copy (%s)", 3483afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3484c7c34a24SBruce Evans db_printf("\n"); 348595e5e988SJohn Dyson nlines++; 3486df8bae1dSRodney W. Grimes 3487df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 3488df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 3489df8bae1dSRodney W. Grimes entry->object.vm_object)) { 3490c7c34a24SBruce Evans db_indent += 2; 3491101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 3492101eeb7fSBruce Evans entry->object.vm_object, 3493914181e7SBruce Evans full, 0, (char *)0); 349495e5e988SJohn Dyson nlines += 4; 3495c7c34a24SBruce Evans db_indent -= 2; 3496df8bae1dSRodney W. Grimes } 3497df8bae1dSRodney W. Grimes } 3498df8bae1dSRodney W. Grimes } 3499c7c34a24SBruce Evans db_indent -= 2; 350095e5e988SJohn Dyson if (db_indent == 0) 350195e5e988SJohn Dyson nlines = 0; 3502df8bae1dSRodney W. Grimes } 350395e5e988SJohn Dyson 350495e5e988SJohn Dyson 350595e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 350695e5e988SJohn Dyson { 350795e5e988SJohn Dyson struct proc *p; 350895e5e988SJohn Dyson 350995e5e988SJohn Dyson if (have_addr) { 351095e5e988SJohn Dyson p = (struct proc *) addr; 351195e5e988SJohn Dyson } else { 351295e5e988SJohn Dyson p = curproc; 351395e5e988SJohn Dyson } 351495e5e988SJohn Dyson 3515ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3516ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3517b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 351895e5e988SJohn Dyson 3519101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 352095e5e988SJohn Dyson } 352195e5e988SJohn Dyson 3522c7c34a24SBruce Evans #endif /* DDB */ 3523