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 103df8bae1dSRodney W. Grimes * entries; a single hint is used to speed up lookups. 104df8bae1dSRodney W. Grimes * 105956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 106df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 107df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 108df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 109df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 110df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 111df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 112df8bae1dSRodney W. Grimes * 113df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 114ad5fca3bSAlan Cox * by copying VM object references from one map to 115df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 116df8bae1dSRodney W. Grimes */ 117df8bae1dSRodney W. Grimes 118df8bae1dSRodney W. Grimes /* 119df8bae1dSRodney W. Grimes * vm_map_startup: 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * Initialize the vm_map module. Must be called before 122df8bae1dSRodney W. Grimes * any other vm_map routines. 123df8bae1dSRodney W. Grimes * 124df8bae1dSRodney W. Grimes * Map and entry structures are allocated from the general 125df8bae1dSRodney W. Grimes * purpose memory pool with some exceptions: 126df8bae1dSRodney W. Grimes * 127df8bae1dSRodney W. Grimes * - The kernel map and kmem submap are allocated statically. 128df8bae1dSRodney W. Grimes * - Kernel map entries are allocated out of a static pool. 129df8bae1dSRodney W. Grimes * 130df8bae1dSRodney W. Grimes * These restrictions are necessary since malloc() uses the 131df8bae1dSRodney W. Grimes * maps and requires map entries. 132df8bae1dSRodney W. Grimes */ 133df8bae1dSRodney W. Grimes 1343a92e5d5SAlan Cox static struct mtx map_sleep_mtx; 1358355f576SJeff Roberson static uma_zone_t mapentzone; 1368355f576SJeff Roberson static uma_zone_t kmapentzone; 1378355f576SJeff Roberson static uma_zone_t mapzone; 1388355f576SJeff Roberson static uma_zone_t vmspace_zone; 1398355f576SJeff Roberson static struct vm_object kmapentobj; 140b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags); 1418355f576SJeff Roberson static void vmspace_zfini(void *mem, int size); 142b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags); 1438355f576SJeff Roberson static void vm_map_zfini(void *mem, int size); 1448355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max); 1451fc43fd1SAlan Cox 1468355f576SJeff Roberson #ifdef INVARIANTS 1478355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg); 1488355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg); 1498355f576SJeff Roberson #endif 150b18bfc3dSJohn Dyson 15157051fdcSTor Egge /* 15257051fdcSTor Egge * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type 15357051fdcSTor Egge * stable. 15457051fdcSTor Egge */ 15557051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0) 15657051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0) 15757051fdcSTor Egge 158d239bd3cSKonstantin Belousov /* 159d239bd3cSKonstantin Belousov * VM_MAP_RANGE_CHECK: [ internal use only ] 160d239bd3cSKonstantin Belousov * 161d239bd3cSKonstantin Belousov * Asserts that the starting and ending region 162d239bd3cSKonstantin Belousov * addresses fall within the valid range of the map. 163d239bd3cSKonstantin Belousov */ 164d239bd3cSKonstantin Belousov #define VM_MAP_RANGE_CHECK(map, start, end) \ 165d239bd3cSKonstantin Belousov { \ 166d239bd3cSKonstantin Belousov if (start < vm_map_min(map)) \ 167d239bd3cSKonstantin Belousov start = vm_map_min(map); \ 168d239bd3cSKonstantin Belousov if (end > vm_map_max(map)) \ 169d239bd3cSKonstantin Belousov end = vm_map_max(map); \ 170d239bd3cSKonstantin Belousov if (start > end) \ 171d239bd3cSKonstantin Belousov start = end; \ 172d239bd3cSKonstantin Belousov } 173d239bd3cSKonstantin Belousov 1740d94caffSDavid Greenman void 1751b40f8c0SMatthew Dillon vm_map_startup(void) 176df8bae1dSRodney W. Grimes { 1773a92e5d5SAlan Cox mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF); 1788355f576SJeff Roberson mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL, 1798355f576SJeff Roberson #ifdef INVARIANTS 1808355f576SJeff Roberson vm_map_zdtor, 1818355f576SJeff Roberson #else 1828355f576SJeff Roberson NULL, 1838355f576SJeff Roberson #endif 1848355f576SJeff Roberson vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1858355f576SJeff Roberson uma_prealloc(mapzone, MAX_KMAP); 186670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry), 18718aa2de5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 18818aa2de5SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZONE_VM); 189670d17b5SJeff Roberson uma_prealloc(kmapentzone, MAX_KMAPENT); 190670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry), 191670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 192df8bae1dSRodney W. Grimes } 193df8bae1dSRodney W. Grimes 1948355f576SJeff Roberson static void 1958355f576SJeff Roberson vmspace_zfini(void *mem, int size) 1968355f576SJeff Roberson { 1978355f576SJeff Roberson struct vmspace *vm; 1988355f576SJeff Roberson 1998355f576SJeff Roberson vm = (struct vmspace *)mem; 2008355f576SJeff Roberson vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map)); 2018355f576SJeff Roberson } 2028355f576SJeff Roberson 203b23f72e9SBrian Feldman static int 204b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags) 2058355f576SJeff Roberson { 2068355f576SJeff Roberson struct vmspace *vm; 2078355f576SJeff Roberson 2088355f576SJeff Roberson vm = (struct vmspace *)mem; 2098355f576SJeff Roberson 21089b57fcfSKonstantin Belousov vm->vm_map.pmap = NULL; 211b23f72e9SBrian Feldman (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags); 212b23f72e9SBrian Feldman return (0); 2138355f576SJeff Roberson } 2148355f576SJeff Roberson 2158355f576SJeff Roberson static void 2168355f576SJeff Roberson vm_map_zfini(void *mem, int size) 2178355f576SJeff Roberson { 2188355f576SJeff Roberson vm_map_t map; 2198355f576SJeff Roberson 2208355f576SJeff Roberson map = (vm_map_t)mem; 22136daaecdSAlan Cox mtx_destroy(&map->system_mtx); 22212c64974SMaxime Henrion sx_destroy(&map->lock); 2238355f576SJeff Roberson } 2248355f576SJeff Roberson 225b23f72e9SBrian Feldman static int 226b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags) 2278355f576SJeff Roberson { 2288355f576SJeff Roberson vm_map_t map; 2298355f576SJeff Roberson 2308355f576SJeff Roberson map = (vm_map_t)mem; 2318355f576SJeff Roberson map->nentries = 0; 2328355f576SJeff Roberson map->size = 0; 233d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 23412c64974SMaxime Henrion sx_init(&map->lock, "user map"); 235b23f72e9SBrian Feldman return (0); 2368355f576SJeff Roberson } 2378355f576SJeff Roberson 2388355f576SJeff Roberson #ifdef INVARIANTS 2398355f576SJeff Roberson static void 2408355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg) 2418355f576SJeff Roberson { 2428355f576SJeff Roberson struct vmspace *vm; 2438355f576SJeff Roberson 2448355f576SJeff Roberson vm = (struct vmspace *)mem; 2458355f576SJeff Roberson 2468355f576SJeff Roberson vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg); 2478355f576SJeff Roberson } 2488355f576SJeff Roberson static void 2498355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg) 2508355f576SJeff Roberson { 2518355f576SJeff Roberson vm_map_t map; 2528355f576SJeff Roberson 2538355f576SJeff Roberson map = (vm_map_t)mem; 2548355f576SJeff Roberson KASSERT(map->nentries == 0, 2558355f576SJeff Roberson ("map %p nentries == %d on free.", 2568355f576SJeff Roberson map, map->nentries)); 2578355f576SJeff Roberson KASSERT(map->size == 0, 2588355f576SJeff Roberson ("map %p size == %lu on free.", 2599eb6e519SJeff Roberson map, (unsigned long)map->size)); 2608355f576SJeff Roberson } 2618355f576SJeff Roberson #endif /* INVARIANTS */ 2628355f576SJeff Roberson 263df8bae1dSRodney W. Grimes /* 264df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 265df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 266df8bae1dSRodney W. Grimes */ 267df8bae1dSRodney W. Grimes struct vmspace * 2682d8acc0fSJohn Dyson vmspace_alloc(min, max) 269df8bae1dSRodney W. Grimes vm_offset_t min, max; 270df8bae1dSRodney W. Grimes { 271c0877f10SJohn Dyson struct vmspace *vm; 2720d94caffSDavid Greenman 273a163d034SWarner Losh vm = uma_zalloc(vmspace_zone, M_WAITOK); 27489b57fcfSKonstantin Belousov if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) { 27589b57fcfSKonstantin Belousov uma_zfree(vmspace_zone, vm); 27689b57fcfSKonstantin Belousov return (NULL); 27789b57fcfSKonstantin Belousov } 27821c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_alloc: %p", vm); 2798355f576SJeff Roberson _vm_map_init(&vm->vm_map, min, max); 280b1028ad1SLuoqi Chen vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 281df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 2822d8acc0fSJohn Dyson vm->vm_shm = NULL; 28351ab6c28SAlan Cox vm->vm_swrss = 0; 28451ab6c28SAlan Cox vm->vm_tsize = 0; 28551ab6c28SAlan Cox vm->vm_dsize = 0; 28651ab6c28SAlan Cox vm->vm_ssize = 0; 28751ab6c28SAlan Cox vm->vm_taddr = 0; 28851ab6c28SAlan Cox vm->vm_daddr = 0; 28951ab6c28SAlan Cox vm->vm_maxsaddr = 0; 290df8bae1dSRodney W. Grimes return (vm); 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes 293df8bae1dSRodney W. Grimes void 2941b40f8c0SMatthew Dillon vm_init2(void) 2951b40f8c0SMatthew Dillon { 2962feb50bfSAttilio Rao uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count, 2973fde38dfSMike Silbersack (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 + 2983fde38dfSMike Silbersack maxproc * 2 + maxfiles); 2998355f576SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL, 3008355f576SJeff Roberson #ifdef INVARIANTS 3018355f576SJeff Roberson vmspace_zdtor, 3028355f576SJeff Roberson #else 3038355f576SJeff Roberson NULL, 3048355f576SJeff Roberson #endif 3058355f576SJeff Roberson vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 3063075778bSJohn Dyson } 3073075778bSJohn Dyson 30862a59e8fSWarner Losh static inline void 309582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm) 310df8bae1dSRodney W. Grimes { 31121c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_free: %p", vm); 3123db161e0SMatthew Dillon 3133db161e0SMatthew Dillon /* 3143db161e0SMatthew Dillon * Make sure any SysV shm is freed, it might not have been in 3153db161e0SMatthew Dillon * exit1(). 3163db161e0SMatthew Dillon */ 3173db161e0SMatthew Dillon shmexit(vm); 3183db161e0SMatthew Dillon 31930dcfc09SJohn Dyson /* 320df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 3210d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 3220d94caffSDavid Greenman * the pmap module to reclaim anything left. 323df8bae1dSRodney W. Grimes */ 324717f7d59SAlan Cox (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset, 325df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 3268355f576SJeff Roberson 32789b57fcfSKonstantin Belousov /* 32889b57fcfSKonstantin Belousov * XXX Comment out the pmap_release call for now. The 32989b57fcfSKonstantin Belousov * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause 33089b57fcfSKonstantin Belousov * pmap.resident_count to be != 0 on exit sometimes. 33189b57fcfSKonstantin Belousov */ 33289b57fcfSKonstantin Belousov /* pmap_release(vmspace_pmap(vm)); */ 3338355f576SJeff Roberson uma_zfree(vmspace_zone, vm); 334df8bae1dSRodney W. Grimes } 335582ec34cSAlfred Perlstein 336582ec34cSAlfred Perlstein void 337582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm) 338582ec34cSAlfred Perlstein { 3391a276a3fSAlan Cox int refcnt; 340582ec34cSAlfred Perlstein 341582ec34cSAlfred Perlstein if (vm->vm_refcnt == 0) 342582ec34cSAlfred Perlstein panic("vmspace_free: attempt to free already freed vmspace"); 343582ec34cSAlfred Perlstein 3441a276a3fSAlan Cox do 3451a276a3fSAlan Cox refcnt = vm->vm_refcnt; 3461a276a3fSAlan Cox while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 34757051fdcSTor Egge if (refcnt == 1) 348582ec34cSAlfred Perlstein vmspace_dofree(vm); 349582ec34cSAlfred Perlstein } 350582ec34cSAlfred Perlstein 351582ec34cSAlfred Perlstein void 352582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p) 353582ec34cSAlfred Perlstein { 354334f7061SPeter Wemm struct vmspace *vm; 355582ec34cSAlfred Perlstein 35657051fdcSTor Egge PROC_VMSPACE_LOCK(p); 357334f7061SPeter Wemm vm = p->p_vmspace; 358334f7061SPeter Wemm p->p_vmspace = NULL; 35957051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 36057051fdcSTor Egge KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace")); 36157051fdcSTor Egge vmspace_free(vm); 36257051fdcSTor Egge } 36357051fdcSTor Egge 36457051fdcSTor Egge void 36557051fdcSTor Egge vmspace_exit(struct thread *td) 36657051fdcSTor Egge { 36757051fdcSTor Egge int refcnt; 36857051fdcSTor Egge struct vmspace *vm; 36957051fdcSTor Egge struct proc *p; 370389d2b6eSMatthew Dillon 371389d2b6eSMatthew Dillon /* 37257051fdcSTor Egge * Release user portion of address space. 37357051fdcSTor Egge * This releases references to vnodes, 37457051fdcSTor Egge * which could cause I/O if the file has been unlinked. 37557051fdcSTor Egge * Need to do this early enough that we can still sleep. 376389d2b6eSMatthew Dillon * 37757051fdcSTor Egge * The last exiting process to reach this point releases as 37857051fdcSTor Egge * much of the environment as it can. vmspace_dofree() is the 37957051fdcSTor Egge * slower fallback in case another process had a temporary 38057051fdcSTor Egge * reference to the vmspace. 381389d2b6eSMatthew Dillon */ 38257051fdcSTor Egge 38357051fdcSTor Egge p = td->td_proc; 38457051fdcSTor Egge vm = p->p_vmspace; 38557051fdcSTor Egge atomic_add_int(&vmspace0.vm_refcnt, 1); 38657051fdcSTor Egge do { 38757051fdcSTor Egge refcnt = vm->vm_refcnt; 38857051fdcSTor Egge if (refcnt > 1 && p->p_vmspace != &vmspace0) { 38957051fdcSTor Egge /* Switch now since other proc might free vmspace */ 39057051fdcSTor Egge PROC_VMSPACE_LOCK(p); 39157051fdcSTor Egge p->p_vmspace = &vmspace0; 39257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 39357051fdcSTor Egge pmap_activate(td); 39457051fdcSTor Egge } 39557051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 39657051fdcSTor Egge if (refcnt == 1) { 39757051fdcSTor Egge if (p->p_vmspace != vm) { 39857051fdcSTor Egge /* vmspace not yet freed, switch back */ 39957051fdcSTor Egge PROC_VMSPACE_LOCK(p); 40057051fdcSTor Egge p->p_vmspace = vm; 40157051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 40257051fdcSTor Egge pmap_activate(td); 40357051fdcSTor Egge } 40457051fdcSTor Egge pmap_remove_pages(vmspace_pmap(vm)); 40557051fdcSTor Egge /* Switch now since this proc will free vmspace */ 40657051fdcSTor Egge PROC_VMSPACE_LOCK(p); 40757051fdcSTor Egge p->p_vmspace = &vmspace0; 40857051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 40957051fdcSTor Egge pmap_activate(td); 410334f7061SPeter Wemm vmspace_dofree(vm); 411334f7061SPeter Wemm } 41257051fdcSTor Egge } 41357051fdcSTor Egge 41457051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */ 41557051fdcSTor Egge 41657051fdcSTor Egge struct vmspace * 41757051fdcSTor Egge vmspace_acquire_ref(struct proc *p) 41857051fdcSTor Egge { 41957051fdcSTor Egge struct vmspace *vm; 42057051fdcSTor Egge int refcnt; 42157051fdcSTor Egge 42257051fdcSTor Egge PROC_VMSPACE_LOCK(p); 42357051fdcSTor Egge vm = p->p_vmspace; 42457051fdcSTor Egge if (vm == NULL) { 42557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 42657051fdcSTor Egge return (NULL); 42757051fdcSTor Egge } 42857051fdcSTor Egge do { 42957051fdcSTor Egge refcnt = vm->vm_refcnt; 43057051fdcSTor Egge if (refcnt <= 0) { /* Avoid 0->1 transition */ 43157051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 43257051fdcSTor Egge return (NULL); 43357051fdcSTor Egge } 43457051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1)); 43557051fdcSTor Egge if (vm != p->p_vmspace) { 43657051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 43757051fdcSTor Egge vmspace_free(vm); 43857051fdcSTor Egge return (NULL); 43957051fdcSTor Egge } 44057051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 44157051fdcSTor Egge return (vm); 44257051fdcSTor Egge } 443df8bae1dSRodney W. Grimes 4441b40f8c0SMatthew Dillon void 445780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line) 4461b40f8c0SMatthew Dillon { 447bc91c510SAlan Cox 44893bc4879SAlan Cox if (map->system_map) 44936daaecdSAlan Cox _mtx_lock_flags(&map->system_mtx, 0, file, line); 45012c64974SMaxime Henrion else 451f9819486SAttilio Rao (void)_sx_xlock(&map->lock, 0, file, line); 4521b40f8c0SMatthew Dillon map->timestamp++; 4531b40f8c0SMatthew Dillon } 4541b40f8c0SMatthew Dillon 4551b40f8c0SMatthew Dillon void 456780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line) 4570e0af8ecSBrian Feldman { 458bc91c510SAlan Cox 45936daaecdSAlan Cox if (map->system_map) 46036daaecdSAlan Cox _mtx_unlock_flags(&map->system_mtx, 0, file, line); 46136daaecdSAlan Cox else 46212c64974SMaxime Henrion _sx_xunlock(&map->lock, file, line); 4630e0af8ecSBrian Feldman } 4640e0af8ecSBrian Feldman 4650e0af8ecSBrian Feldman void 466780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line) 4670e0af8ecSBrian Feldman { 468bc91c510SAlan Cox 46993bc4879SAlan Cox if (map->system_map) 47036daaecdSAlan Cox _mtx_lock_flags(&map->system_mtx, 0, file, line); 47112c64974SMaxime Henrion else 472f9819486SAttilio Rao (void)_sx_xlock(&map->lock, 0, file, line); 47336daaecdSAlan Cox } 4740e0af8ecSBrian Feldman 4750e0af8ecSBrian Feldman void 476780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line) 4770e0af8ecSBrian Feldman { 478bc91c510SAlan Cox 47936daaecdSAlan Cox if (map->system_map) 48036daaecdSAlan Cox _mtx_unlock_flags(&map->system_mtx, 0, file, line); 48136daaecdSAlan Cox else 48212c64974SMaxime Henrion _sx_xunlock(&map->lock, file, line); 48325adb370SBrian Feldman } 48425adb370SBrian Feldman 485d974f03cSAlan Cox int 486780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line) 487d974f03cSAlan Cox { 48825adb370SBrian Feldman int error; 48925adb370SBrian Feldman 49036daaecdSAlan Cox error = map->system_map ? 49136daaecdSAlan Cox !_mtx_trylock(&map->system_mtx, 0, file, line) : 49212c64974SMaxime Henrion !_sx_try_xlock(&map->lock, file, line); 4933a92e5d5SAlan Cox if (error == 0) 4943a92e5d5SAlan Cox map->timestamp++; 495bc91c510SAlan Cox return (error == 0); 4960e0af8ecSBrian Feldman } 4970e0af8ecSBrian Feldman 4980e0af8ecSBrian Feldman int 49972d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line) 50072d97679SDavid Schultz { 50172d97679SDavid Schultz int error; 50272d97679SDavid Schultz 50372d97679SDavid Schultz error = map->system_map ? 50472d97679SDavid Schultz !_mtx_trylock(&map->system_mtx, 0, file, line) : 50512c64974SMaxime Henrion !_sx_try_xlock(&map->lock, file, line); 50672d97679SDavid Schultz return (error == 0); 50772d97679SDavid Schultz } 50872d97679SDavid Schultz 50972d97679SDavid Schultz int 510780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line) 5110e0af8ecSBrian Feldman { 512bc91c510SAlan Cox 51336daaecdSAlan Cox #ifdef INVARIANTS 51412c64974SMaxime Henrion if (map->system_map) { 51536daaecdSAlan Cox _mtx_assert(&map->system_mtx, MA_OWNED, file, line); 51636daaecdSAlan Cox } else 51712c64974SMaxime Henrion _sx_assert(&map->lock, SX_XLOCKED, file, line); 51812c64974SMaxime Henrion #endif 519bc91c510SAlan Cox map->timestamp++; 520bc91c510SAlan Cox return (0); 5210e0af8ecSBrian Feldman } 5220e0af8ecSBrian Feldman 5230e0af8ecSBrian Feldman void 524780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line) 5251b40f8c0SMatthew Dillon { 526bc91c510SAlan Cox 52736daaecdSAlan Cox #ifdef INVARIANTS 52812c64974SMaxime Henrion if (map->system_map) { 52936daaecdSAlan Cox _mtx_assert(&map->system_mtx, MA_OWNED, file, line); 53036daaecdSAlan Cox } else 53112c64974SMaxime Henrion _sx_assert(&map->lock, SX_XLOCKED, file, line); 53212c64974SMaxime Henrion #endif 53325adb370SBrian Feldman } 53425adb370SBrian Feldman 535acd9a301SAlan Cox /* 536acd9a301SAlan Cox * vm_map_unlock_and_wait: 537acd9a301SAlan Cox */ 5389688f931SAlan Cox int 5398ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(vm_map_t map, int timo) 540acd9a301SAlan Cox { 541acd9a301SAlan Cox 5423a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 543acd9a301SAlan Cox vm_map_unlock(map); 5448ce2d00aSPawel Jakub Dawidek return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", timo)); 545acd9a301SAlan Cox } 546acd9a301SAlan Cox 547acd9a301SAlan Cox /* 548acd9a301SAlan Cox * vm_map_wakeup: 549acd9a301SAlan Cox */ 5509688f931SAlan Cox void 551acd9a301SAlan Cox vm_map_wakeup(vm_map_t map) 552acd9a301SAlan Cox { 553acd9a301SAlan Cox 554b49ecb86SAlan Cox /* 5553a92e5d5SAlan Cox * Acquire and release map_sleep_mtx to prevent a wakeup() 5563a92e5d5SAlan Cox * from being performed (and lost) between the vm_map_unlock() 5573a92e5d5SAlan Cox * and the msleep() in vm_map_unlock_and_wait(). 558b49ecb86SAlan Cox */ 5593a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 5603a92e5d5SAlan Cox mtx_unlock(&map_sleep_mtx); 561acd9a301SAlan Cox wakeup(&map->root); 562acd9a301SAlan Cox } 563acd9a301SAlan Cox 5641b40f8c0SMatthew Dillon long 5651b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace) 5661b40f8c0SMatthew Dillon { 5671b40f8c0SMatthew Dillon return pmap_resident_count(vmspace_pmap(vmspace)); 5681b40f8c0SMatthew Dillon } 5691b40f8c0SMatthew Dillon 5702bc7dd56SBruce M Simpson long 5712bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace) 5722bc7dd56SBruce M Simpson { 5732bc7dd56SBruce M Simpson return pmap_wired_count(vmspace_pmap(vmspace)); 5742bc7dd56SBruce M Simpson } 5752bc7dd56SBruce M Simpson 576ff2b5645SMatthew Dillon /* 577df8bae1dSRodney W. Grimes * vm_map_create: 578df8bae1dSRodney W. Grimes * 579df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 580df8bae1dSRodney W. Grimes * the given physical map structure, and having 581df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 582df8bae1dSRodney W. Grimes */ 5830d94caffSDavid Greenman vm_map_t 5841b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max) 585df8bae1dSRodney W. Grimes { 586c0877f10SJohn Dyson vm_map_t result; 587df8bae1dSRodney W. Grimes 588a163d034SWarner Losh result = uma_zalloc(mapzone, M_WAITOK); 58921c641b2SJohn Baldwin CTR1(KTR_VM, "vm_map_create: %p", result); 5908355f576SJeff Roberson _vm_map_init(result, min, max); 591df8bae1dSRodney W. Grimes result->pmap = pmap; 592df8bae1dSRodney W. Grimes return (result); 593df8bae1dSRodney W. Grimes } 594df8bae1dSRodney W. Grimes 595df8bae1dSRodney W. Grimes /* 596df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 597df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 598df8bae1dSRodney W. Grimes * The pmap is set elsewhere. 599df8bae1dSRodney W. Grimes */ 6008355f576SJeff Roberson static void 6018355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 602df8bae1dSRodney W. Grimes { 60321c641b2SJohn Baldwin 604df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 6059688f931SAlan Cox map->needs_wakeup = FALSE; 6063075778bSJohn Dyson map->system_map = 0; 607df8bae1dSRodney W. Grimes map->min_offset = min; 608df8bae1dSRodney W. Grimes map->max_offset = max; 609af7cd0c5SBrian Feldman map->flags = 0; 6104e94f402SAlan Cox map->root = NULL; 611df8bae1dSRodney W. Grimes map->timestamp = 0; 612df8bae1dSRodney W. Grimes } 613df8bae1dSRodney W. Grimes 614a18b1f1dSJason Evans void 6158355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 616a18b1f1dSJason Evans { 6178355f576SJeff Roberson _vm_map_init(map, min, max); 618d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 61912c64974SMaxime Henrion sx_init(&map->lock, "user map"); 620a18b1f1dSJason Evans } 621a18b1f1dSJason Evans 622df8bae1dSRodney W. Grimes /* 623b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 624b18bfc3dSJohn Dyson * 625b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 626b18bfc3dSJohn Dyson */ 62762487bb4SJohn Dyson static void 6281b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry) 629b18bfc3dSJohn Dyson { 6302b4a2c27SAlan Cox uma_zfree(map->system_map ? kmapentzone : mapentzone, entry); 631b18bfc3dSJohn Dyson } 632b18bfc3dSJohn Dyson 633b18bfc3dSJohn Dyson /* 634df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 635df8bae1dSRodney W. Grimes * 636df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 637b28cb1caSAlfred Perlstein * No entry fields are filled in. 638df8bae1dSRodney W. Grimes */ 639f708ef1bSPoul-Henning Kamp static vm_map_entry_t 6401b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map) 641df8bae1dSRodney W. Grimes { 6421f6889a1SMatthew Dillon vm_map_entry_t new_entry; 6431f6889a1SMatthew Dillon 6442b4a2c27SAlan Cox if (map->system_map) 6452b4a2c27SAlan Cox new_entry = uma_zalloc(kmapentzone, M_NOWAIT); 6462b4a2c27SAlan Cox else 647a163d034SWarner Losh new_entry = uma_zalloc(mapentzone, M_WAITOK); 6481f6889a1SMatthew Dillon if (new_entry == NULL) 6491f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 6501f6889a1SMatthew Dillon return (new_entry); 651df8bae1dSRodney W. Grimes } 652df8bae1dSRodney W. Grimes 653df8bae1dSRodney W. Grimes /* 654794316a8SAlan Cox * vm_map_entry_set_behavior: 655794316a8SAlan Cox * 656794316a8SAlan Cox * Set the expected access behavior, either normal, random, or 657794316a8SAlan Cox * sequential. 658794316a8SAlan Cox */ 65962a59e8fSWarner Losh static inline void 660794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior) 661794316a8SAlan Cox { 662794316a8SAlan Cox entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) | 663794316a8SAlan Cox (behavior & MAP_ENTRY_BEHAV_MASK); 664794316a8SAlan Cox } 665794316a8SAlan Cox 666794316a8SAlan Cox /* 6670164e057SAlan Cox * vm_map_entry_set_max_free: 6680164e057SAlan Cox * 6690164e057SAlan Cox * Set the max_free field in a vm_map_entry. 6700164e057SAlan Cox */ 67162a59e8fSWarner Losh static inline void 6720164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry) 6730164e057SAlan Cox { 6740164e057SAlan Cox 6750164e057SAlan Cox entry->max_free = entry->adj_free; 6760164e057SAlan Cox if (entry->left != NULL && entry->left->max_free > entry->max_free) 6770164e057SAlan Cox entry->max_free = entry->left->max_free; 6780164e057SAlan Cox if (entry->right != NULL && entry->right->max_free > entry->max_free) 6790164e057SAlan Cox entry->max_free = entry->right->max_free; 6800164e057SAlan Cox } 6810164e057SAlan Cox 6820164e057SAlan Cox /* 6834e94f402SAlan Cox * vm_map_entry_splay: 6844e94f402SAlan Cox * 6850164e057SAlan Cox * The Sleator and Tarjan top-down splay algorithm with the 6860164e057SAlan Cox * following variation. Max_free must be computed bottom-up, so 6870164e057SAlan Cox * on the downward pass, maintain the left and right spines in 6880164e057SAlan Cox * reverse order. Then, make a second pass up each side to fix 6890164e057SAlan Cox * the pointers and compute max_free. The time bound is O(log n) 6900164e057SAlan Cox * amortized. 6910164e057SAlan Cox * 6920164e057SAlan Cox * The new root is the vm_map_entry containing "addr", or else an 6930164e057SAlan Cox * adjacent entry (lower or higher) if addr is not in the tree. 6940164e057SAlan Cox * 6950164e057SAlan Cox * The map must be locked, and leaves it so. 6960164e057SAlan Cox * 6970164e057SAlan Cox * Returns: the new root. 6984e94f402SAlan Cox */ 6994e94f402SAlan Cox static vm_map_entry_t 7000164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root) 7014e94f402SAlan Cox { 7020164e057SAlan Cox vm_map_entry_t llist, rlist; 7030164e057SAlan Cox vm_map_entry_t ltree, rtree; 7040164e057SAlan Cox vm_map_entry_t y; 7054e94f402SAlan Cox 7060164e057SAlan Cox /* Special case of empty tree. */ 7074e94f402SAlan Cox if (root == NULL) 7084e94f402SAlan Cox return (root); 7090164e057SAlan Cox 7100164e057SAlan Cox /* 7110164e057SAlan Cox * Pass One: Splay down the tree until we find addr or a NULL 7120164e057SAlan Cox * pointer where addr would go. llist and rlist are the two 7130164e057SAlan Cox * sides in reverse order (bottom-up), with llist linked by 7140164e057SAlan Cox * the right pointer and rlist linked by the left pointer in 7150164e057SAlan Cox * the vm_map_entry. Wait until Pass Two to set max_free on 7160164e057SAlan Cox * the two spines. 7170164e057SAlan Cox */ 7180164e057SAlan Cox llist = NULL; 7190164e057SAlan Cox rlist = NULL; 7200164e057SAlan Cox for (;;) { 7210164e057SAlan Cox /* root is never NULL in here. */ 7220164e057SAlan Cox if (addr < root->start) { 7230164e057SAlan Cox y = root->left; 7240164e057SAlan Cox if (y == NULL) 7254e94f402SAlan Cox break; 7260164e057SAlan Cox if (addr < y->start && y->left != NULL) { 7270164e057SAlan Cox /* Rotate right and put y on rlist. */ 7284e94f402SAlan Cox root->left = y->right; 7294e94f402SAlan Cox y->right = root; 7300164e057SAlan Cox vm_map_entry_set_max_free(root); 7310164e057SAlan Cox root = y->left; 7320164e057SAlan Cox y->left = rlist; 7330164e057SAlan Cox rlist = y; 7340164e057SAlan Cox } else { 7350164e057SAlan Cox /* Put root on rlist. */ 7360164e057SAlan Cox root->left = rlist; 7370164e057SAlan Cox rlist = root; 7384e94f402SAlan Cox root = y; 7394e94f402SAlan Cox } 7400164e057SAlan Cox } else { 7410164e057SAlan Cox y = root->right; 7420164e057SAlan Cox if (addr < root->end || y == NULL) 7434e94f402SAlan Cox break; 7440164e057SAlan Cox if (addr >= y->end && y->right != NULL) { 7450164e057SAlan Cox /* Rotate left and put y on llist. */ 7464e94f402SAlan Cox root->right = y->left; 7474e94f402SAlan Cox y->left = root; 7480164e057SAlan Cox vm_map_entry_set_max_free(root); 7490164e057SAlan Cox root = y->right; 7500164e057SAlan Cox y->right = llist; 7510164e057SAlan Cox llist = y; 7520164e057SAlan Cox } else { 7530164e057SAlan Cox /* Put root on llist. */ 7540164e057SAlan Cox root->right = llist; 7550164e057SAlan Cox llist = root; 7564e94f402SAlan Cox root = y; 7574e94f402SAlan Cox } 7584e94f402SAlan Cox } 7590164e057SAlan Cox } 7600164e057SAlan Cox 7610164e057SAlan Cox /* 7620164e057SAlan Cox * Pass Two: Walk back up the two spines, flip the pointers 7630164e057SAlan Cox * and set max_free. The subtrees of the root go at the 7640164e057SAlan Cox * bottom of llist and rlist. 7650164e057SAlan Cox */ 7660164e057SAlan Cox ltree = root->left; 7670164e057SAlan Cox while (llist != NULL) { 7680164e057SAlan Cox y = llist->right; 7690164e057SAlan Cox llist->right = ltree; 7700164e057SAlan Cox vm_map_entry_set_max_free(llist); 7710164e057SAlan Cox ltree = llist; 7720164e057SAlan Cox llist = y; 7730164e057SAlan Cox } 7740164e057SAlan Cox rtree = root->right; 7750164e057SAlan Cox while (rlist != NULL) { 7760164e057SAlan Cox y = rlist->left; 7770164e057SAlan Cox rlist->left = rtree; 7780164e057SAlan Cox vm_map_entry_set_max_free(rlist); 7790164e057SAlan Cox rtree = rlist; 7800164e057SAlan Cox rlist = y; 7810164e057SAlan Cox } 7820164e057SAlan Cox 7830164e057SAlan Cox /* 7840164e057SAlan Cox * Final assembly: add ltree and rtree as subtrees of root. 7850164e057SAlan Cox */ 7860164e057SAlan Cox root->left = ltree; 7870164e057SAlan Cox root->right = rtree; 7880164e057SAlan Cox vm_map_entry_set_max_free(root); 7890164e057SAlan Cox 7904e94f402SAlan Cox return (root); 7914e94f402SAlan Cox } 7924e94f402SAlan Cox 7934e94f402SAlan Cox /* 794df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 795df8bae1dSRodney W. Grimes * 796df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 797df8bae1dSRodney W. Grimes */ 7984e94f402SAlan Cox static void 79999c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 80099c81ca9SAlan Cox vm_map_entry_t after_where, 80199c81ca9SAlan Cox vm_map_entry_t entry) 80299c81ca9SAlan Cox { 80321c641b2SJohn Baldwin 80421c641b2SJohn Baldwin CTR4(KTR_VM, 80521c641b2SJohn Baldwin "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 80621c641b2SJohn Baldwin map->nentries, entry, after_where); 80799c81ca9SAlan Cox map->nentries++; 80899c81ca9SAlan Cox entry->prev = after_where; 80999c81ca9SAlan Cox entry->next = after_where->next; 81099c81ca9SAlan Cox entry->next->prev = entry; 81199c81ca9SAlan Cox after_where->next = entry; 8124e94f402SAlan Cox 8134e94f402SAlan Cox if (after_where != &map->header) { 8144e94f402SAlan Cox if (after_where != map->root) 8154e94f402SAlan Cox vm_map_entry_splay(after_where->start, map->root); 8164e94f402SAlan Cox entry->right = after_where->right; 8174e94f402SAlan Cox entry->left = after_where; 8184e94f402SAlan Cox after_where->right = NULL; 8190164e057SAlan Cox after_where->adj_free = entry->start - after_where->end; 8200164e057SAlan Cox vm_map_entry_set_max_free(after_where); 8214e94f402SAlan Cox } else { 8224e94f402SAlan Cox entry->right = map->root; 8234e94f402SAlan Cox entry->left = NULL; 8244e94f402SAlan Cox } 8250164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 8260164e057SAlan Cox entry->next->start) - entry->end; 8270164e057SAlan Cox vm_map_entry_set_max_free(entry); 8284e94f402SAlan Cox map->root = entry; 829df8bae1dSRodney W. Grimes } 83099c81ca9SAlan Cox 8314e94f402SAlan Cox static void 83299c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 83399c81ca9SAlan Cox vm_map_entry_t entry) 83499c81ca9SAlan Cox { 8354e94f402SAlan Cox vm_map_entry_t next, prev, root; 83699c81ca9SAlan Cox 8374e94f402SAlan Cox if (entry != map->root) 8384e94f402SAlan Cox vm_map_entry_splay(entry->start, map->root); 8394e94f402SAlan Cox if (entry->left == NULL) 8404e94f402SAlan Cox root = entry->right; 8414e94f402SAlan Cox else { 8424e94f402SAlan Cox root = vm_map_entry_splay(entry->start, entry->left); 8434e94f402SAlan Cox root->right = entry->right; 8440164e057SAlan Cox root->adj_free = (entry->next == &map->header ? map->max_offset : 8450164e057SAlan Cox entry->next->start) - root->end; 8460164e057SAlan Cox vm_map_entry_set_max_free(root); 8474e94f402SAlan Cox } 8484e94f402SAlan Cox map->root = root; 8494e94f402SAlan Cox 8504e94f402SAlan Cox prev = entry->prev; 8514e94f402SAlan Cox next = entry->next; 85299c81ca9SAlan Cox next->prev = prev; 85399c81ca9SAlan Cox prev->next = next; 85499c81ca9SAlan Cox map->nentries--; 85521c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 85621c641b2SJohn Baldwin map->nentries, entry); 857df8bae1dSRodney W. Grimes } 858df8bae1dSRodney W. Grimes 859df8bae1dSRodney W. Grimes /* 8600164e057SAlan Cox * vm_map_entry_resize_free: 8610164e057SAlan Cox * 8620164e057SAlan Cox * Recompute the amount of free space following a vm_map_entry 8630164e057SAlan Cox * and propagate that value up the tree. Call this function after 8640164e057SAlan Cox * resizing a map entry in-place, that is, without a call to 8650164e057SAlan Cox * vm_map_entry_link() or _unlink(). 8660164e057SAlan Cox * 8670164e057SAlan Cox * The map must be locked, and leaves it so. 8680164e057SAlan Cox */ 8690164e057SAlan Cox static void 8700164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry) 8710164e057SAlan Cox { 8720164e057SAlan Cox 8730164e057SAlan Cox /* 8740164e057SAlan Cox * Using splay trees without parent pointers, propagating 8750164e057SAlan Cox * max_free up the tree is done by moving the entry to the 8760164e057SAlan Cox * root and making the change there. 8770164e057SAlan Cox */ 8780164e057SAlan Cox if (entry != map->root) 8790164e057SAlan Cox map->root = vm_map_entry_splay(entry->start, map->root); 8800164e057SAlan Cox 8810164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 8820164e057SAlan Cox entry->next->start) - entry->end; 8830164e057SAlan Cox vm_map_entry_set_max_free(entry); 8840164e057SAlan Cox } 8850164e057SAlan Cox 8860164e057SAlan Cox /* 887df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 888df8bae1dSRodney W. Grimes * 889df8bae1dSRodney W. Grimes * Finds the map entry containing (or 890df8bae1dSRodney W. Grimes * immediately preceding) the specified address 891df8bae1dSRodney W. Grimes * in the given map; the entry is returned 892df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 893df8bae1dSRodney W. Grimes * result indicates whether the address is 894df8bae1dSRodney W. Grimes * actually contained in the map. 895df8bae1dSRodney W. Grimes */ 8960d94caffSDavid Greenman boolean_t 8971b40f8c0SMatthew Dillon vm_map_lookup_entry( 8981b40f8c0SMatthew Dillon vm_map_t map, 8991b40f8c0SMatthew Dillon vm_offset_t address, 9001b40f8c0SMatthew Dillon vm_map_entry_t *entry) /* OUT */ 901df8bae1dSRodney W. Grimes { 902c0877f10SJohn Dyson vm_map_entry_t cur; 903df8bae1dSRodney W. Grimes 9044e94f402SAlan Cox cur = vm_map_entry_splay(address, map->root); 9054e94f402SAlan Cox if (cur == NULL) 9064e94f402SAlan Cox *entry = &map->header; 9074e94f402SAlan Cox else { 9084e94f402SAlan Cox map->root = cur; 909df8bae1dSRodney W. Grimes 910df8bae1dSRodney W. Grimes if (address >= cur->start) { 911df8bae1dSRodney W. Grimes *entry = cur; 9124e94f402SAlan Cox if (cur->end > address) 913df8bae1dSRodney W. Grimes return (TRUE); 9144e94f402SAlan Cox } else 915df8bae1dSRodney W. Grimes *entry = cur->prev; 9164e94f402SAlan Cox } 917df8bae1dSRodney W. Grimes return (FALSE); 918df8bae1dSRodney W. Grimes } 919df8bae1dSRodney W. Grimes 920df8bae1dSRodney W. Grimes /* 92130dcfc09SJohn Dyson * vm_map_insert: 92230dcfc09SJohn Dyson * 92330dcfc09SJohn Dyson * Inserts the given whole VM object into the target 92430dcfc09SJohn Dyson * map at the specified address range. The object's 92530dcfc09SJohn Dyson * size should match that of the address range. 92630dcfc09SJohn Dyson * 92730dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 9282aaeadf8SMatthew Dillon * 9292aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 9302aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 93130dcfc09SJohn Dyson */ 93230dcfc09SJohn Dyson int 933b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 934b9dcd593SBruce Evans vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 935b9dcd593SBruce Evans int cow) 93630dcfc09SJohn Dyson { 937c0877f10SJohn Dyson vm_map_entry_t new_entry; 938c0877f10SJohn Dyson vm_map_entry_t prev_entry; 93930dcfc09SJohn Dyson vm_map_entry_t temp_entry; 9409730a5daSPaul Saab vm_eflags_t protoeflags; 94130dcfc09SJohn Dyson 94230dcfc09SJohn Dyson /* 94330dcfc09SJohn Dyson * Check that the start and end points are not bogus. 94430dcfc09SJohn Dyson */ 94530dcfc09SJohn Dyson if ((start < map->min_offset) || (end > map->max_offset) || 94630dcfc09SJohn Dyson (start >= end)) 94730dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 94830dcfc09SJohn Dyson 94930dcfc09SJohn Dyson /* 95030dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 95130dcfc09SJohn Dyson * of an existing entry, this range is bogus. 95230dcfc09SJohn Dyson */ 95330dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 95430dcfc09SJohn Dyson return (KERN_NO_SPACE); 95530dcfc09SJohn Dyson 95630dcfc09SJohn Dyson prev_entry = temp_entry; 95730dcfc09SJohn Dyson 95830dcfc09SJohn Dyson /* 95930dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 96030dcfc09SJohn Dyson */ 96130dcfc09SJohn Dyson if ((prev_entry->next != &map->header) && 96230dcfc09SJohn Dyson (prev_entry->next->start < end)) 96330dcfc09SJohn Dyson return (KERN_NO_SPACE); 96430dcfc09SJohn Dyson 965afa07f7eSJohn Dyson protoeflags = 0; 966afa07f7eSJohn Dyson 967afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 968e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 969afa07f7eSJohn Dyson 9704e045f93SAlan Cox if (cow & MAP_NOFAULT) { 971afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 972afa07f7eSJohn Dyson 9734e045f93SAlan Cox KASSERT(object == NULL, 9744e045f93SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 9754e045f93SAlan Cox } 9764f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 9774f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 9789730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 9799730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 9804f79d873SMatthew Dillon 9811d284e00SAlan Cox if (object != NULL) { 98230dcfc09SJohn Dyson /* 9831d284e00SAlan Cox * OBJ_ONEMAPPING must be cleared unless this mapping 9841d284e00SAlan Cox * is trivially proven to be the only mapping for any 9851d284e00SAlan Cox * of the object's pages. (Object granularity 9861d284e00SAlan Cox * reference counting is insufficient to recognize 9871d284e00SAlan Cox * aliases with precision.) 98830dcfc09SJohn Dyson */ 9891d284e00SAlan Cox VM_OBJECT_LOCK(object); 9901d284e00SAlan Cox if (object->ref_count > 1 || object->shadow_count != 0) 9912aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 9921d284e00SAlan Cox VM_OBJECT_UNLOCK(object); 9934e045f93SAlan Cox } 9944e045f93SAlan Cox else if ((prev_entry != &map->header) && 9954e045f93SAlan Cox (prev_entry->eflags == protoeflags) && 9968cc7e047SJohn Dyson (prev_entry->end == start) && 9974e045f93SAlan Cox (prev_entry->wired_count == 0) && 9984e045f93SAlan Cox ((prev_entry->object.vm_object == NULL) || 9998cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 100057a21abaSAlan Cox prev_entry->offset, 10018cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 1002cdc2c291SJohn Dyson (vm_size_t)(end - prev_entry->end)))) { 100330dcfc09SJohn Dyson /* 10042aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 10052aaeadf8SMatthew Dillon * can extend the previous map entry to include the 10062aaeadf8SMatthew Dillon * new range as well. 100730dcfc09SJohn Dyson */ 10088cc7e047SJohn Dyson if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 10098cc7e047SJohn Dyson (prev_entry->protection == prot) && 10108cc7e047SJohn Dyson (prev_entry->max_protection == max)) { 101130dcfc09SJohn Dyson map->size += (end - prev_entry->end); 101230dcfc09SJohn Dyson prev_entry->end = end; 10130164e057SAlan Cox vm_map_entry_resize_free(map, prev_entry); 10144e71e795SMatthew Dillon vm_map_simplify_entry(map, prev_entry); 101530dcfc09SJohn Dyson return (KERN_SUCCESS); 101630dcfc09SJohn Dyson } 10178cc7e047SJohn Dyson 10182aaeadf8SMatthew Dillon /* 10192aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 10202aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 10212aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 10224e71e795SMatthew Dillon * account for it. object may be NULL. 10232aaeadf8SMatthew Dillon */ 10242aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 10252aaeadf8SMatthew Dillon offset = prev_entry->offset + 10262aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 10278cc7e047SJohn Dyson vm_object_reference(object); 1028b18bfc3dSJohn Dyson } 10292aaeadf8SMatthew Dillon 10302aaeadf8SMatthew Dillon /* 10312aaeadf8SMatthew Dillon * NOTE: if conditionals fail, object can be NULL here. This occurs 10322aaeadf8SMatthew Dillon * in things like the buffer map where we manage kva but do not manage 10332aaeadf8SMatthew Dillon * backing objects. 10342aaeadf8SMatthew Dillon */ 10358cc7e047SJohn Dyson 103630dcfc09SJohn Dyson /* 103730dcfc09SJohn Dyson * Create a new entry 103830dcfc09SJohn Dyson */ 103930dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 104030dcfc09SJohn Dyson new_entry->start = start; 104130dcfc09SJohn Dyson new_entry->end = end; 104230dcfc09SJohn Dyson 1043afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 104430dcfc09SJohn Dyson new_entry->object.vm_object = object; 104530dcfc09SJohn Dyson new_entry->offset = offset; 10462267af78SJulian Elischer new_entry->avail_ssize = 0; 10472267af78SJulian Elischer 104830dcfc09SJohn Dyson new_entry->inheritance = VM_INHERIT_DEFAULT; 104930dcfc09SJohn Dyson new_entry->protection = prot; 105030dcfc09SJohn Dyson new_entry->max_protection = max; 105130dcfc09SJohn Dyson new_entry->wired_count = 0; 1052e5f251d2SAlan Cox 105330dcfc09SJohn Dyson /* 105430dcfc09SJohn Dyson * Insert the new entry into the list 105530dcfc09SJohn Dyson */ 105630dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 105730dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 105830dcfc09SJohn Dyson 10591a484d28SMatthew Dillon #if 0 10601a484d28SMatthew Dillon /* 10611a484d28SMatthew Dillon * Temporarily removed to avoid MAP_STACK panic, due to 10621a484d28SMatthew Dillon * MAP_STACK being a huge hack. Will be added back in 10631a484d28SMatthew Dillon * when MAP_STACK (and the user stack mapping) is fixed. 10641a484d28SMatthew Dillon */ 10654e71e795SMatthew Dillon /* 10664e71e795SMatthew Dillon * It may be possible to simplify the entry 10674e71e795SMatthew Dillon */ 10684e71e795SMatthew Dillon vm_map_simplify_entry(map, new_entry); 10691a484d28SMatthew Dillon #endif 10704e71e795SMatthew Dillon 10714f79d873SMatthew Dillon if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 10724da4d293SAlan Cox vm_map_pmap_enter(map, start, prot, 1073e972780aSAlan Cox object, OFF_TO_IDX(offset), end - start, 1074e972780aSAlan Cox cow & MAP_PREFAULT_PARTIAL); 10754f79d873SMatthew Dillon } 1076e972780aSAlan Cox 107730dcfc09SJohn Dyson return (KERN_SUCCESS); 107830dcfc09SJohn Dyson } 107930dcfc09SJohn Dyson 108030dcfc09SJohn Dyson /* 10810164e057SAlan Cox * vm_map_findspace: 10820164e057SAlan Cox * 10830164e057SAlan Cox * Find the first fit (lowest VM address) for "length" free bytes 10840164e057SAlan Cox * beginning at address >= start in the given map. 10850164e057SAlan Cox * 10860164e057SAlan Cox * In a vm_map_entry, "adj_free" is the amount of free space 10870164e057SAlan Cox * adjacent (higher address) to this entry, and "max_free" is the 10880164e057SAlan Cox * maximum amount of contiguous free space in its subtree. This 10890164e057SAlan Cox * allows finding a free region in one path down the tree, so 10900164e057SAlan Cox * O(log n) amortized with splay trees. 10910164e057SAlan Cox * 10920164e057SAlan Cox * The map must be locked, and leaves it so. 10930164e057SAlan Cox * 10940164e057SAlan Cox * Returns: 0 on success, and starting address in *addr, 10950164e057SAlan Cox * 1 if insufficient space. 1096df8bae1dSRodney W. Grimes */ 1097df8bae1dSRodney W. Grimes int 10980164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length, 10990164e057SAlan Cox vm_offset_t *addr) /* OUT */ 1100df8bae1dSRodney W. Grimes { 11010164e057SAlan Cox vm_map_entry_t entry; 11020164e057SAlan Cox vm_offset_t end, st; 1103df8bae1dSRodney W. Grimes 1104986b43f8SAlan Cox /* 1105986b43f8SAlan Cox * Request must fit within min/max VM address and must avoid 1106986b43f8SAlan Cox * address wrap. 1107986b43f8SAlan Cox */ 1108df8bae1dSRodney W. Grimes if (start < map->min_offset) 1109df8bae1dSRodney W. Grimes start = map->min_offset; 1110986b43f8SAlan Cox if (start + length > map->max_offset || start + length < start) 1111df8bae1dSRodney W. Grimes return (1); 1112df8bae1dSRodney W. Grimes 11130164e057SAlan Cox /* Empty tree means wide open address space. */ 11140164e057SAlan Cox if (map->root == NULL) { 1115df8bae1dSRodney W. Grimes *addr = start; 11160164e057SAlan Cox goto found; 111799448ed1SJohn Dyson } 11180164e057SAlan Cox 11190164e057SAlan Cox /* 11200164e057SAlan Cox * After splay, if start comes before root node, then there 11210164e057SAlan Cox * must be a gap from start to the root. 11220164e057SAlan Cox */ 11230164e057SAlan Cox map->root = vm_map_entry_splay(start, map->root); 11240164e057SAlan Cox if (start + length <= map->root->start) { 11250164e057SAlan Cox *addr = start; 11260164e057SAlan Cox goto found; 11270164e057SAlan Cox } 11280164e057SAlan Cox 11290164e057SAlan Cox /* 11300164e057SAlan Cox * Root is the last node that might begin its gap before 1131986b43f8SAlan Cox * start, and this is the last comparison where address 1132986b43f8SAlan Cox * wrap might be a problem. 11330164e057SAlan Cox */ 11340164e057SAlan Cox st = (start > map->root->end) ? start : map->root->end; 1135986b43f8SAlan Cox if (length <= map->root->end + map->root->adj_free - st) { 11360164e057SAlan Cox *addr = st; 11370164e057SAlan Cox goto found; 11380164e057SAlan Cox } 11390164e057SAlan Cox 11400164e057SAlan Cox /* With max_free, can immediately tell if no solution. */ 11410164e057SAlan Cox entry = map->root->right; 11420164e057SAlan Cox if (entry == NULL || length > entry->max_free) 11430164e057SAlan Cox return (1); 11440164e057SAlan Cox 11450164e057SAlan Cox /* 11460164e057SAlan Cox * Search the right subtree in the order: left subtree, root, 11470164e057SAlan Cox * right subtree (first fit). The previous splay implies that 11480164e057SAlan Cox * all regions in the right subtree have addresses > start. 11490164e057SAlan Cox */ 11500164e057SAlan Cox while (entry != NULL) { 11510164e057SAlan Cox if (entry->left != NULL && entry->left->max_free >= length) 11520164e057SAlan Cox entry = entry->left; 11530164e057SAlan Cox else if (entry->adj_free >= length) { 11540164e057SAlan Cox *addr = entry->end; 11550164e057SAlan Cox goto found; 11560164e057SAlan Cox } else 11570164e057SAlan Cox entry = entry->right; 11580164e057SAlan Cox } 11590164e057SAlan Cox 11600164e057SAlan Cox /* Can't get here, so panic if we do. */ 11610164e057SAlan Cox panic("vm_map_findspace: max_free corrupt"); 11620164e057SAlan Cox 11630164e057SAlan Cox found: 11640164e057SAlan Cox /* Expand the kernel pmap, if necessary. */ 11650164e057SAlan Cox if (map == kernel_map) { 11660164e057SAlan Cox end = round_page(*addr + length); 11670164e057SAlan Cox if (end > kernel_vm_end) 11680164e057SAlan Cox pmap_growkernel(end); 116999448ed1SJohn Dyson } 1170df8bae1dSRodney W. Grimes return (0); 1171df8bae1dSRodney W. Grimes } 1172df8bae1dSRodney W. Grimes 1173d239bd3cSKonstantin Belousov int 1174d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1175b8ca4ef2SAlan Cox vm_offset_t start, vm_size_t length, vm_prot_t prot, 1176d239bd3cSKonstantin Belousov vm_prot_t max, int cow) 1177d239bd3cSKonstantin Belousov { 1178b8ca4ef2SAlan Cox vm_offset_t end; 1179d239bd3cSKonstantin Belousov int result; 1180d239bd3cSKonstantin Belousov 1181d239bd3cSKonstantin Belousov vm_map_lock(map); 1182d239bd3cSKonstantin Belousov end = start + length; 1183d239bd3cSKonstantin Belousov VM_MAP_RANGE_CHECK(map, start, end); 1184d239bd3cSKonstantin Belousov (void) vm_map_delete(map, start, end); 1185d239bd3cSKonstantin Belousov result = vm_map_insert(map, object, offset, start, end, prot, 1186d239bd3cSKonstantin Belousov max, cow); 1187d239bd3cSKonstantin Belousov vm_map_unlock(map); 1188d239bd3cSKonstantin Belousov return (result); 1189d239bd3cSKonstantin Belousov } 1190d239bd3cSKonstantin Belousov 1191df8bae1dSRodney W. Grimes /* 1192df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 1193df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 1194df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 1195df8bae1dSRodney W. Grimes * returned in the same parameter. 1196df8bae1dSRodney W. Grimes * 11972aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 11982aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 1199df8bae1dSRodney W. Grimes */ 1200df8bae1dSRodney W. Grimes int 1201b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1202b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 120326c538ffSAlan Cox vm_size_t length, int find_space, vm_prot_t prot, 1204b9dcd593SBruce Evans vm_prot_t max, int cow) 1205df8bae1dSRodney W. Grimes { 1206c0877f10SJohn Dyson vm_offset_t start; 12076eaee3feSAlan Cox int result; 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes start = *addr; 1210bea41bcfSDavid Greenman vm_map_lock(map); 121126c538ffSAlan Cox do { 121226c538ffSAlan Cox if (find_space != VMFS_NO_SPACE) { 1213df8bae1dSRodney W. Grimes if (vm_map_findspace(map, start, length, addr)) { 1214df8bae1dSRodney W. Grimes vm_map_unlock(map); 1215df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 1216df8bae1dSRodney W. Grimes } 121726c538ffSAlan Cox if (find_space == VMFS_ALIGNED_SPACE) 121826c538ffSAlan Cox pmap_align_superpage(object, offset, addr, 121926c538ffSAlan Cox length); 1220df8bae1dSRodney W. Grimes start = *addr; 1221df8bae1dSRodney W. Grimes } 122226c538ffSAlan Cox result = vm_map_insert(map, object, offset, start, start + 122326c538ffSAlan Cox length, prot, max, cow); 122426c538ffSAlan Cox } while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE); 1225df8bae1dSRodney W. Grimes vm_map_unlock(map); 1226df8bae1dSRodney W. Grimes return (result); 1227df8bae1dSRodney W. Grimes } 1228df8bae1dSRodney W. Grimes 1229df8bae1dSRodney W. Grimes /* 1230b7b2aac2SJohn Dyson * vm_map_simplify_entry: 123167bf6868SJohn Dyson * 12324e71e795SMatthew Dillon * Simplify the given map entry by merging with either neighbor. This 12334e71e795SMatthew Dillon * routine also has the ability to merge with both neighbors. 12344e71e795SMatthew Dillon * 12354e71e795SMatthew Dillon * The map must be locked. 12364e71e795SMatthew Dillon * 12374e71e795SMatthew Dillon * This routine guarentees that the passed entry remains valid (though 12384e71e795SMatthew Dillon * possibly extended). When merging, this routine may delete one or 12394e71e795SMatthew Dillon * both neighbors. 1240df8bae1dSRodney W. Grimes */ 1241b7b2aac2SJohn Dyson void 12421b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1243df8bae1dSRodney W. Grimes { 1244308c24baSJohn Dyson vm_map_entry_t next, prev; 1245b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 1246df8bae1dSRodney W. Grimes 1247acd9a301SAlan Cox if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) 1248df8bae1dSRodney W. Grimes return; 1249308c24baSJohn Dyson 1250308c24baSJohn Dyson prev = entry->prev; 1251308c24baSJohn Dyson if (prev != &map->header) { 125267bf6868SJohn Dyson prevsize = prev->end - prev->start; 125367bf6868SJohn Dyson if ( (prev->end == entry->start) && 125467bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 125595e5e988SJohn Dyson (!prev->object.vm_object || 125667bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 1257afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 125867bf6868SJohn Dyson (prev->protection == entry->protection) && 125967bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 126067bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 1261b7b2aac2SJohn Dyson (prev->wired_count == entry->wired_count)) { 1262308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 1263308c24baSJohn Dyson entry->start = prev->start; 1264308c24baSJohn Dyson entry->offset = prev->offset; 12650164e057SAlan Cox if (entry->prev != &map->header) 12660164e057SAlan Cox vm_map_entry_resize_free(map, entry->prev); 1267b18bfc3dSJohn Dyson if (prev->object.vm_object) 1268308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 1269308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 1270308c24baSJohn Dyson } 1271308c24baSJohn Dyson } 1272de5f6a77SJohn Dyson 1273de5f6a77SJohn Dyson next = entry->next; 1274308c24baSJohn Dyson if (next != &map->header) { 127567bf6868SJohn Dyson esize = entry->end - entry->start; 127667bf6868SJohn Dyson if ((entry->end == next->start) && 127767bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 127867bf6868SJohn Dyson (!entry->object.vm_object || 127967bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 1280afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 128167bf6868SJohn Dyson (next->protection == entry->protection) && 128267bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 128367bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 1284b7b2aac2SJohn Dyson (next->wired_count == entry->wired_count)) { 1285de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 1286de5f6a77SJohn Dyson entry->end = next->end; 12870164e057SAlan Cox vm_map_entry_resize_free(map, entry); 1288b18bfc3dSJohn Dyson if (next->object.vm_object) 1289de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 1290de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 1291df8bae1dSRodney W. Grimes } 1292df8bae1dSRodney W. Grimes } 1293de5f6a77SJohn Dyson } 1294df8bae1dSRodney W. Grimes /* 1295df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 1296df8bae1dSRodney W. Grimes * 1297df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 1298df8bae1dSRodney W. Grimes * the specified address; if necessary, 1299df8bae1dSRodney W. Grimes * it splits the entry into two. 1300df8bae1dSRodney W. Grimes */ 1301df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 1302df8bae1dSRodney W. Grimes { \ 1303df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 1304df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 1305df8bae1dSRodney W. Grimes } 1306df8bae1dSRodney W. Grimes 1307df8bae1dSRodney W. Grimes /* 1308df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1309df8bae1dSRodney W. Grimes * the entry must be split. 1310df8bae1dSRodney W. Grimes */ 13110d94caffSDavid Greenman static void 13121b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1313df8bae1dSRodney W. Grimes { 1314c0877f10SJohn Dyson vm_map_entry_t new_entry; 1315df8bae1dSRodney W. Grimes 1316df8bae1dSRodney W. Grimes /* 13170d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 13180d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 13190d94caffSDavid Greenman * starting address. 1320df8bae1dSRodney W. Grimes */ 1321f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 1322f32dbbeeSJohn Dyson 132311cccda1SJohn Dyson /* 132411cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 132511cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 132611cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 132711cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 132811cccda1SJohn Dyson * put this improvement. 132911cccda1SJohn Dyson */ 13304e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 133111cccda1SJohn Dyson vm_object_t object; 133211cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1333c2e11a03SJohn Dyson atop(entry->end - entry->start)); 133411cccda1SJohn Dyson entry->object.vm_object = object; 133511cccda1SJohn Dyson entry->offset = 0; 133611cccda1SJohn Dyson } 133711cccda1SJohn Dyson 1338df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1339df8bae1dSRodney W. Grimes *new_entry = *entry; 1340df8bae1dSRodney W. Grimes 1341df8bae1dSRodney W. Grimes new_entry->end = start; 1342df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 1343df8bae1dSRodney W. Grimes entry->start = start; 1344df8bae1dSRodney W. Grimes 1345df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 1346df8bae1dSRodney W. Grimes 13479fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1348df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1349df8bae1dSRodney W. Grimes } 1350c0877f10SJohn Dyson } 1351df8bae1dSRodney W. Grimes 1352df8bae1dSRodney W. Grimes /* 1353df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 1354df8bae1dSRodney W. Grimes * 1355df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 1356df8bae1dSRodney W. Grimes * the specified address; if necessary, 1357df8bae1dSRodney W. Grimes * it splits the entry into two. 1358df8bae1dSRodney W. Grimes */ 1359df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 1360df8bae1dSRodney W. Grimes { \ 1361af045176SPoul-Henning Kamp if ((endaddr) < (entry->end)) \ 1362af045176SPoul-Henning Kamp _vm_map_clip_end((map), (entry), (endaddr)); \ 1363df8bae1dSRodney W. Grimes } 1364df8bae1dSRodney W. Grimes 1365df8bae1dSRodney W. Grimes /* 1366df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1367df8bae1dSRodney W. Grimes * the entry must be split. 1368df8bae1dSRodney W. Grimes */ 13690d94caffSDavid Greenman static void 13701b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1371df8bae1dSRodney W. Grimes { 1372c0877f10SJohn Dyson vm_map_entry_t new_entry; 1373df8bae1dSRodney W. Grimes 1374df8bae1dSRodney W. Grimes /* 137511cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 137611cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 137711cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 137811cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 137911cccda1SJohn Dyson * put this improvement. 138011cccda1SJohn Dyson */ 13814e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 138211cccda1SJohn Dyson vm_object_t object; 138311cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1384c2e11a03SJohn Dyson atop(entry->end - entry->start)); 138511cccda1SJohn Dyson entry->object.vm_object = object; 138611cccda1SJohn Dyson entry->offset = 0; 138711cccda1SJohn Dyson } 138811cccda1SJohn Dyson 138911cccda1SJohn Dyson /* 13900d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 1391df8bae1dSRodney W. Grimes */ 1392df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1393df8bae1dSRodney W. Grimes *new_entry = *entry; 1394df8bae1dSRodney W. Grimes 1395df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 1396df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 1397df8bae1dSRodney W. Grimes 1398df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 1399df8bae1dSRodney W. Grimes 14009fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1401df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1402df8bae1dSRodney W. Grimes } 1403c0877f10SJohn Dyson } 1404df8bae1dSRodney W. Grimes 1405df8bae1dSRodney W. Grimes /* 1406df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 1407df8bae1dSRodney W. Grimes * 1408df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 1409df8bae1dSRodney W. Grimes * 1410df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 1411df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 1412df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 1413df8bae1dSRodney W. Grimes * 1414df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 1415df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 1416df8bae1dSRodney W. Grimes * vm_fault 1417df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 1418df8bae1dSRodney W. Grimes * 1419df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 1420df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 1421df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 1422df8bae1dSRodney W. Grimes */ 1423df8bae1dSRodney W. Grimes int 14241b40f8c0SMatthew Dillon vm_map_submap( 14251b40f8c0SMatthew Dillon vm_map_t map, 14261b40f8c0SMatthew Dillon vm_offset_t start, 14271b40f8c0SMatthew Dillon vm_offset_t end, 14281b40f8c0SMatthew Dillon vm_map_t submap) 1429df8bae1dSRodney W. Grimes { 1430df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1431c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 1432df8bae1dSRodney W. Grimes 1433df8bae1dSRodney W. Grimes vm_map_lock(map); 1434df8bae1dSRodney W. Grimes 1435df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1436df8bae1dSRodney W. Grimes 1437df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1438df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 14390d94caffSDavid Greenman } else 1440df8bae1dSRodney W. Grimes entry = entry->next; 1441df8bae1dSRodney W. Grimes 1442df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1443df8bae1dSRodney W. Grimes 1444df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 14459fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 1446afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 14472d8acc0fSJohn Dyson entry->object.sub_map = submap; 1448afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1449df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 1450df8bae1dSRodney W. Grimes } 1451df8bae1dSRodney W. Grimes vm_map_unlock(map); 1452df8bae1dSRodney W. Grimes 1453df8bae1dSRodney W. Grimes return (result); 1454df8bae1dSRodney W. Grimes } 1455df8bae1dSRodney W. Grimes 1456df8bae1dSRodney W. Grimes /* 14571f78f902SAlan Cox * The maximum number of pages to map 14581f78f902SAlan Cox */ 14591f78f902SAlan Cox #define MAX_INIT_PT 96 14601f78f902SAlan Cox 14611f78f902SAlan Cox /* 14620551c08dSAlan Cox * vm_map_pmap_enter: 14630551c08dSAlan Cox * 1464cf4682aeSAlan Cox * Preload read-only mappings for the given object's resident pages into 1465cf4682aeSAlan Cox * the given map. This eliminates the soft faults on process startup and 1466c4169725SAlan Cox * immediately after an mmap(2). Because these are speculative mappings, 1467c4169725SAlan Cox * cached pages are not reactivated and mapped. 14680551c08dSAlan Cox */ 14690551c08dSAlan Cox void 14704da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 14710551c08dSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 14720551c08dSAlan Cox { 14738fece8c3SAlan Cox vm_offset_t start; 1474ce142d9eSAlan Cox vm_page_t p, p_start; 14758fece8c3SAlan Cox vm_pindex_t psize, tmpidx; 14761f70d622SAlan Cox boolean_t are_queues_locked; 14770551c08dSAlan Cox 1478ba8bca61SAlan Cox if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 14791f78f902SAlan Cox return; 14801f78f902SAlan Cox VM_OBJECT_LOCK(object); 14811f78f902SAlan Cox if (object->type == OBJT_DEVICE) { 14821f78f902SAlan Cox pmap_object_init_pt(map->pmap, addr, object, pindex, size); 14831f78f902SAlan Cox goto unlock_return; 14841f78f902SAlan Cox } 14851f78f902SAlan Cox 14861f78f902SAlan Cox psize = atop(size); 14871f78f902SAlan Cox 14881f78f902SAlan Cox if (object->type != OBJT_VNODE || 14891f78f902SAlan Cox ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) && 14901f78f902SAlan Cox (object->resident_page_count > MAX_INIT_PT))) { 14911f78f902SAlan Cox goto unlock_return; 14921f78f902SAlan Cox } 14931f78f902SAlan Cox 14941f78f902SAlan Cox if (psize + pindex > object->size) { 14951f78f902SAlan Cox if (object->size < pindex) 14961f78f902SAlan Cox goto unlock_return; 14971f78f902SAlan Cox psize = object->size - pindex; 14981f78f902SAlan Cox } 14991f78f902SAlan Cox 15001f70d622SAlan Cox are_queues_locked = FALSE; 1501ce142d9eSAlan Cox start = 0; 1502ce142d9eSAlan Cox p_start = NULL; 15031f78f902SAlan Cox 15041f78f902SAlan Cox if ((p = TAILQ_FIRST(&object->memq)) != NULL) { 15051f78f902SAlan Cox if (p->pindex < pindex) { 15061f78f902SAlan Cox p = vm_page_splay(pindex, object->root); 15071f78f902SAlan Cox if ((object->root = p)->pindex < pindex) 15081f78f902SAlan Cox p = TAILQ_NEXT(p, listq); 15091f78f902SAlan Cox } 15101f78f902SAlan Cox } 15111f78f902SAlan Cox /* 15121f78f902SAlan Cox * Assert: the variable p is either (1) the page with the 15131f78f902SAlan Cox * least pindex greater than or equal to the parameter pindex 15141f78f902SAlan Cox * or (2) NULL. 15151f78f902SAlan Cox */ 15161f78f902SAlan Cox for (; 15171f78f902SAlan Cox p != NULL && (tmpidx = p->pindex - pindex) < psize; 15181f78f902SAlan Cox p = TAILQ_NEXT(p, listq)) { 15191f78f902SAlan Cox /* 15201f78f902SAlan Cox * don't allow an madvise to blow away our really 15211f78f902SAlan Cox * free pages allocating pv entries. 15221f78f902SAlan Cox */ 15231f78f902SAlan Cox if ((flags & MAP_PREFAULT_MADVISE) && 15242feb50bfSAttilio Rao cnt.v_free_count < cnt.v_free_reserved) { 1525379fb642SAlan Cox psize = tmpidx; 15261f78f902SAlan Cox break; 15271f78f902SAlan Cox } 15281f78f902SAlan Cox if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL && 15298fece8c3SAlan Cox (p->busy == 0)) { 1530ce142d9eSAlan Cox if (p_start == NULL) { 1531ce142d9eSAlan Cox start = addr + ptoa(tmpidx); 1532ce142d9eSAlan Cox p_start = p; 1533ce142d9eSAlan Cox } 15347bfda801SAlan Cox } else if (p_start != NULL) { 15351f70d622SAlan Cox if (!are_queues_locked) { 15361f70d622SAlan Cox are_queues_locked = TRUE; 153785f5b245SAlan Cox vm_page_lock_queues(); 153885f5b245SAlan Cox } 1539cf4682aeSAlan Cox pmap_enter_object(map->pmap, start, addr + 1540cf4682aeSAlan Cox ptoa(tmpidx), p_start, prot); 1541cf4682aeSAlan Cox p_start = NULL; 1542cf4682aeSAlan Cox } 1543cf4682aeSAlan Cox } 15447bfda801SAlan Cox if (p_start != NULL) { 15457bfda801SAlan Cox if (!are_queues_locked) { 15467bfda801SAlan Cox are_queues_locked = TRUE; 15477bfda801SAlan Cox vm_page_lock_queues(); 15481f78f902SAlan Cox } 1549379fb642SAlan Cox pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1550379fb642SAlan Cox p_start, prot); 15517bfda801SAlan Cox } 15521f70d622SAlan Cox if (are_queues_locked) 15531f70d622SAlan Cox vm_page_unlock_queues(); 15541f78f902SAlan Cox unlock_return: 15551f78f902SAlan Cox VM_OBJECT_UNLOCK(object); 15560551c08dSAlan Cox } 15570551c08dSAlan Cox 15580551c08dSAlan Cox /* 1559df8bae1dSRodney W. Grimes * vm_map_protect: 1560df8bae1dSRodney W. Grimes * 1561df8bae1dSRodney W. Grimes * Sets the protection of the specified address 1562df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 1563df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 1564df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 1565df8bae1dSRodney W. Grimes */ 1566df8bae1dSRodney W. Grimes int 1567b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1568b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 1569df8bae1dSRodney W. Grimes { 1570c0877f10SJohn Dyson vm_map_entry_t current; 1571df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1572df8bae1dSRodney W. Grimes 1573df8bae1dSRodney W. Grimes vm_map_lock(map); 1574df8bae1dSRodney W. Grimes 1575df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1576df8bae1dSRodney W. Grimes 1577df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1578df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1579b7b2aac2SJohn Dyson } else { 1580df8bae1dSRodney W. Grimes entry = entry->next; 1581b7b2aac2SJohn Dyson } 1582df8bae1dSRodney W. Grimes 1583df8bae1dSRodney W. Grimes /* 15840d94caffSDavid Greenman * Make a first pass to check for protection violations. 1585df8bae1dSRodney W. Grimes */ 1586df8bae1dSRodney W. Grimes current = entry; 1587df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1588afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1589a1f6d91cSDavid Greenman vm_map_unlock(map); 1590df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1591a1f6d91cSDavid Greenman } 1592df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 1593df8bae1dSRodney W. Grimes vm_map_unlock(map); 1594df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 1595df8bae1dSRodney W. Grimes } 1596df8bae1dSRodney W. Grimes current = current->next; 1597df8bae1dSRodney W. Grimes } 1598df8bae1dSRodney W. Grimes 1599df8bae1dSRodney W. Grimes /* 16000d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 16010d94caffSDavid Greenman * necessary the second time.] 1602df8bae1dSRodney W. Grimes */ 1603df8bae1dSRodney W. Grimes current = entry; 1604df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1605df8bae1dSRodney W. Grimes vm_prot_t old_prot; 1606df8bae1dSRodney W. Grimes 1607df8bae1dSRodney W. Grimes vm_map_clip_end(map, current, end); 1608df8bae1dSRodney W. Grimes 1609df8bae1dSRodney W. Grimes old_prot = current->protection; 1610df8bae1dSRodney W. Grimes if (set_max) 1611df8bae1dSRodney W. Grimes current->protection = 1612df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 1613df8bae1dSRodney W. Grimes old_prot; 1614df8bae1dSRodney W. Grimes else 1615df8bae1dSRodney W. Grimes current->protection = new_prot; 1616df8bae1dSRodney W. Grimes 1617df8bae1dSRodney W. Grimes /* 16180d94caffSDavid Greenman * Update physical map if necessary. Worry about copy-on-write 16190d94caffSDavid Greenman * here -- CHECK THIS XXX 1620df8bae1dSRodney W. Grimes */ 1621df8bae1dSRodney W. Grimes if (current->protection != old_prot) { 1622afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1623df8bae1dSRodney W. Grimes VM_PROT_ALL) 1624df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 1625df8bae1dSRodney W. Grimes current->end, 16261c85e3dfSAlan Cox current->protection & MASK(current)); 1627df8bae1dSRodney W. Grimes #undef MASK 1628df8bae1dSRodney W. Grimes } 16297d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 1630df8bae1dSRodney W. Grimes current = current->next; 1631df8bae1dSRodney W. Grimes } 1632df8bae1dSRodney W. Grimes vm_map_unlock(map); 1633df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1634df8bae1dSRodney W. Grimes } 1635df8bae1dSRodney W. Grimes 1636df8bae1dSRodney W. Grimes /* 1637867a482dSJohn Dyson * vm_map_madvise: 1638867a482dSJohn Dyson * 1639867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 1640f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 1641f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 1642f7fc307aSAlan Cox * objects. 1643867a482dSJohn Dyson */ 1644b4309055SMatthew Dillon int 16451b40f8c0SMatthew Dillon vm_map_madvise( 16461b40f8c0SMatthew Dillon vm_map_t map, 16471b40f8c0SMatthew Dillon vm_offset_t start, 16481b40f8c0SMatthew Dillon vm_offset_t end, 16491b40f8c0SMatthew Dillon int behav) 1650867a482dSJohn Dyson { 1651f7fc307aSAlan Cox vm_map_entry_t current, entry; 1652b4309055SMatthew Dillon int modify_map = 0; 1653867a482dSJohn Dyson 1654b4309055SMatthew Dillon /* 1655b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 1656b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 1657b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 1658b4309055SMatthew Dillon * on the map. 1659b4309055SMatthew Dillon */ 1660b4309055SMatthew Dillon switch(behav) { 1661b4309055SMatthew Dillon case MADV_NORMAL: 1662b4309055SMatthew Dillon case MADV_SEQUENTIAL: 1663b4309055SMatthew Dillon case MADV_RANDOM: 16644f79d873SMatthew Dillon case MADV_NOSYNC: 16654f79d873SMatthew Dillon case MADV_AUTOSYNC: 16669730a5daSPaul Saab case MADV_NOCORE: 16679730a5daSPaul Saab case MADV_CORE: 1668b4309055SMatthew Dillon modify_map = 1; 1669867a482dSJohn Dyson vm_map_lock(map); 1670b4309055SMatthew Dillon break; 1671b4309055SMatthew Dillon case MADV_WILLNEED: 1672b4309055SMatthew Dillon case MADV_DONTNEED: 1673b4309055SMatthew Dillon case MADV_FREE: 1674f7fc307aSAlan Cox vm_map_lock_read(map); 1675b4309055SMatthew Dillon break; 1676b4309055SMatthew Dillon default: 1677b4309055SMatthew Dillon return (KERN_INVALID_ARGUMENT); 1678b4309055SMatthew Dillon } 1679b4309055SMatthew Dillon 1680b4309055SMatthew Dillon /* 1681b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 1682b4309055SMatthew Dillon */ 1683867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 1684867a482dSJohn Dyson 1685867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 1686f7fc307aSAlan Cox if (modify_map) 1687867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 1688b4309055SMatthew Dillon } else { 1689867a482dSJohn Dyson entry = entry->next; 1690b4309055SMatthew Dillon } 1691867a482dSJohn Dyson 1692f7fc307aSAlan Cox if (modify_map) { 1693f7fc307aSAlan Cox /* 1694f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 1695f7fc307aSAlan Cox * 1696f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 1697f7fc307aSAlan Cox * limited to the specified address range. 1698f7fc307aSAlan Cox */ 1699867a482dSJohn Dyson for (current = entry; 1700867a482dSJohn Dyson (current != &map->header) && (current->start < end); 1701b4309055SMatthew Dillon current = current->next 1702b4309055SMatthew Dillon ) { 1703f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1704867a482dSJohn Dyson continue; 1705fed9a903SJohn Dyson 170647221757SJohn Dyson vm_map_clip_end(map, current, end); 1707fed9a903SJohn Dyson 1708f7fc307aSAlan Cox switch (behav) { 1709867a482dSJohn Dyson case MADV_NORMAL: 17107f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1711867a482dSJohn Dyson break; 1712867a482dSJohn Dyson case MADV_SEQUENTIAL: 17137f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1714867a482dSJohn Dyson break; 1715867a482dSJohn Dyson case MADV_RANDOM: 17167f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1717867a482dSJohn Dyson break; 17184f79d873SMatthew Dillon case MADV_NOSYNC: 17194f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 17204f79d873SMatthew Dillon break; 17214f79d873SMatthew Dillon case MADV_AUTOSYNC: 17224f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 17234f79d873SMatthew Dillon break; 17249730a5daSPaul Saab case MADV_NOCORE: 17259730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 17269730a5daSPaul Saab break; 17279730a5daSPaul Saab case MADV_CORE: 17289730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 17299730a5daSPaul Saab break; 1730867a482dSJohn Dyson default: 1731867a482dSJohn Dyson break; 1732867a482dSJohn Dyson } 1733f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 1734867a482dSJohn Dyson } 1735867a482dSJohn Dyson vm_map_unlock(map); 1736b4309055SMatthew Dillon } else { 1737f7fc307aSAlan Cox vm_pindex_t pindex; 1738f7fc307aSAlan Cox int count; 1739f7fc307aSAlan Cox 1740f7fc307aSAlan Cox /* 1741f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 1742f7fc307aSAlan Cox * vm_object. 1743f7fc307aSAlan Cox * 1744f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 1745f7fc307aSAlan Cox * the vm_object pindex and count. 1746f7fc307aSAlan Cox */ 1747f7fc307aSAlan Cox for (current = entry; 1748f7fc307aSAlan Cox (current != &map->header) && (current->start < end); 1749b4309055SMatthew Dillon current = current->next 1750b4309055SMatthew Dillon ) { 17515f99b57cSMatthew Dillon vm_offset_t useStart; 17525f99b57cSMatthew Dillon 1753f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1754f7fc307aSAlan Cox continue; 1755f7fc307aSAlan Cox 1756f7fc307aSAlan Cox pindex = OFF_TO_IDX(current->offset); 1757f7fc307aSAlan Cox count = atop(current->end - current->start); 17585f99b57cSMatthew Dillon useStart = current->start; 1759f7fc307aSAlan Cox 1760f7fc307aSAlan Cox if (current->start < start) { 1761f7fc307aSAlan Cox pindex += atop(start - current->start); 1762f7fc307aSAlan Cox count -= atop(start - current->start); 17635f99b57cSMatthew Dillon useStart = start; 1764f7fc307aSAlan Cox } 1765f7fc307aSAlan Cox if (current->end > end) 1766f7fc307aSAlan Cox count -= atop(current->end - end); 1767f7fc307aSAlan Cox 1768f7fc307aSAlan Cox if (count <= 0) 1769f7fc307aSAlan Cox continue; 1770f7fc307aSAlan Cox 1771f7fc307aSAlan Cox vm_object_madvise(current->object.vm_object, 1772f7fc307aSAlan Cox pindex, count, behav); 1773b4309055SMatthew Dillon if (behav == MADV_WILLNEED) { 17740551c08dSAlan Cox vm_map_pmap_enter(map, 17755f99b57cSMatthew Dillon useStart, 17764da4d293SAlan Cox current->protection, 1777f7fc307aSAlan Cox current->object.vm_object, 1778b4309055SMatthew Dillon pindex, 1779b4309055SMatthew Dillon (count << PAGE_SHIFT), 1780e3026983SMatthew Dillon MAP_PREFAULT_MADVISE 1781b4309055SMatthew Dillon ); 1782f7fc307aSAlan Cox } 1783f7fc307aSAlan Cox } 1784f7fc307aSAlan Cox vm_map_unlock_read(map); 1785f7fc307aSAlan Cox } 1786b4309055SMatthew Dillon return (0); 1787867a482dSJohn Dyson } 1788867a482dSJohn Dyson 1789867a482dSJohn Dyson 1790867a482dSJohn Dyson /* 1791df8bae1dSRodney W. Grimes * vm_map_inherit: 1792df8bae1dSRodney W. Grimes * 1793df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 1794df8bae1dSRodney W. Grimes * range in the target map. Inheritance 1795df8bae1dSRodney W. Grimes * affects how the map will be shared with 1796df8bae1dSRodney W. Grimes * child maps at the time of vm_map_fork. 1797df8bae1dSRodney W. Grimes */ 1798df8bae1dSRodney W. Grimes int 1799b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1800b9dcd593SBruce Evans vm_inherit_t new_inheritance) 1801df8bae1dSRodney W. Grimes { 1802c0877f10SJohn Dyson vm_map_entry_t entry; 1803df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 1804df8bae1dSRodney W. Grimes 1805df8bae1dSRodney W. Grimes switch (new_inheritance) { 1806df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 1807df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 1808df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 1809df8bae1dSRodney W. Grimes break; 1810df8bae1dSRodney W. Grimes default: 1811df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1812df8bae1dSRodney W. Grimes } 1813df8bae1dSRodney W. Grimes vm_map_lock(map); 1814df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1815df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 1816df8bae1dSRodney W. Grimes entry = temp_entry; 1817df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 18180d94caffSDavid Greenman } else 1819df8bae1dSRodney W. Grimes entry = temp_entry->next; 1820df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1821df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1822df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 182344428f62SAlan Cox vm_map_simplify_entry(map, entry); 1824df8bae1dSRodney W. Grimes entry = entry->next; 1825df8bae1dSRodney W. Grimes } 1826df8bae1dSRodney W. Grimes vm_map_unlock(map); 1827df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1828df8bae1dSRodney W. Grimes } 1829df8bae1dSRodney W. Grimes 1830df8bae1dSRodney W. Grimes /* 1831acd9a301SAlan Cox * vm_map_unwire: 1832acd9a301SAlan Cox * 1833e27e17b7SAlan Cox * Implements both kernel and user unwiring. 1834acd9a301SAlan Cox */ 1835acd9a301SAlan Cox int 1836acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1837abd498aaSBruce M Simpson int flags) 1838acd9a301SAlan Cox { 1839acd9a301SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 1840acd9a301SAlan Cox vm_offset_t saved_start; 1841acd9a301SAlan Cox unsigned int last_timestamp; 1842acd9a301SAlan Cox int rv; 1843abd498aaSBruce M Simpson boolean_t need_wakeup, result, user_unwire; 1844acd9a301SAlan Cox 1845abd498aaSBruce M Simpson user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 1846acd9a301SAlan Cox vm_map_lock(map); 1847acd9a301SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 1848acd9a301SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 1849abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 1850cbef13d8SAlan Cox first_entry = first_entry->next; 1851abd498aaSBruce M Simpson else { 1852acd9a301SAlan Cox vm_map_unlock(map); 1853acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 1854acd9a301SAlan Cox } 1855abd498aaSBruce M Simpson } 1856acd9a301SAlan Cox last_timestamp = map->timestamp; 1857acd9a301SAlan Cox entry = first_entry; 1858acd9a301SAlan Cox while (entry != &map->header && entry->start < end) { 1859acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 1860acd9a301SAlan Cox /* 1861acd9a301SAlan Cox * We have not yet clipped the entry. 1862acd9a301SAlan Cox */ 1863acd9a301SAlan Cox saved_start = (start >= entry->start) ? start : 1864acd9a301SAlan Cox entry->start; 1865acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 18668ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 1867acd9a301SAlan Cox /* 1868acd9a301SAlan Cox * Allow interruption of user unwiring? 1869acd9a301SAlan Cox */ 1870acd9a301SAlan Cox } 1871acd9a301SAlan Cox vm_map_lock(map); 1872acd9a301SAlan Cox if (last_timestamp+1 != map->timestamp) { 1873acd9a301SAlan Cox /* 1874acd9a301SAlan Cox * Look again for the entry because the map was 1875acd9a301SAlan Cox * modified while it was unlocked. 1876acd9a301SAlan Cox * Specifically, the entry may have been 1877acd9a301SAlan Cox * clipped, merged, or deleted. 1878acd9a301SAlan Cox */ 1879acd9a301SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 1880acd9a301SAlan Cox &tmp_entry)) { 1881cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 1882cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 1883cbef13d8SAlan Cox else { 1884acd9a301SAlan Cox if (saved_start == start) { 1885acd9a301SAlan Cox /* 1886acd9a301SAlan Cox * First_entry has been deleted. 1887acd9a301SAlan Cox */ 1888acd9a301SAlan Cox vm_map_unlock(map); 1889acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 1890acd9a301SAlan Cox } 1891acd9a301SAlan Cox end = saved_start; 1892acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 1893acd9a301SAlan Cox goto done; 1894acd9a301SAlan Cox } 1895cbef13d8SAlan Cox } 1896acd9a301SAlan Cox if (entry == first_entry) 1897acd9a301SAlan Cox first_entry = tmp_entry; 1898acd9a301SAlan Cox else 1899acd9a301SAlan Cox first_entry = NULL; 1900acd9a301SAlan Cox entry = tmp_entry; 1901acd9a301SAlan Cox } 1902acd9a301SAlan Cox last_timestamp = map->timestamp; 1903acd9a301SAlan Cox continue; 1904acd9a301SAlan Cox } 1905acd9a301SAlan Cox vm_map_clip_start(map, entry, start); 1906acd9a301SAlan Cox vm_map_clip_end(map, entry, end); 1907acd9a301SAlan Cox /* 1908acd9a301SAlan Cox * Mark the entry in case the map lock is released. (See 1909acd9a301SAlan Cox * above.) 1910acd9a301SAlan Cox */ 1911acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 1912acd9a301SAlan Cox /* 1913acd9a301SAlan Cox * Check the map for holes in the specified region. 1914abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 1915acd9a301SAlan Cox */ 1916abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 1917abd498aaSBruce M Simpson (entry->end < end && (entry->next == &map->header || 1918abd498aaSBruce M Simpson entry->next->start > entry->end))) { 1919acd9a301SAlan Cox end = entry->end; 1920acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 1921acd9a301SAlan Cox goto done; 1922acd9a301SAlan Cox } 1923acd9a301SAlan Cox /* 19243ffbc0cdSAlan Cox * If system unwiring, require that the entry is system wired. 1925acd9a301SAlan Cox */ 19260ada205eSBrian Feldman if (!user_unwire && 19270ada205eSBrian Feldman vm_map_entry_system_wired_count(entry) == 0) { 1928acd9a301SAlan Cox end = entry->end; 1929acd9a301SAlan Cox rv = KERN_INVALID_ARGUMENT; 1930acd9a301SAlan Cox goto done; 1931acd9a301SAlan Cox } 1932acd9a301SAlan Cox entry = entry->next; 1933acd9a301SAlan Cox } 1934acd9a301SAlan Cox rv = KERN_SUCCESS; 1935acd9a301SAlan Cox done: 1936e27e17b7SAlan Cox need_wakeup = FALSE; 1937acd9a301SAlan Cox if (first_entry == NULL) { 1938acd9a301SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 1939cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 1940cbef13d8SAlan Cox first_entry = first_entry->next; 1941cbef13d8SAlan Cox else 1942acd9a301SAlan Cox KASSERT(result, ("vm_map_unwire: lookup failed")); 1943acd9a301SAlan Cox } 1944acd9a301SAlan Cox entry = first_entry; 1945acd9a301SAlan Cox while (entry != &map->header && entry->start < end) { 19463ffbc0cdSAlan Cox if (rv == KERN_SUCCESS && (!user_unwire || 19473ffbc0cdSAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED))) { 1948b2f3846aSAlan Cox if (user_unwire) 1949b2f3846aSAlan Cox entry->eflags &= ~MAP_ENTRY_USER_WIRED; 1950b2f3846aSAlan Cox entry->wired_count--; 19510ada205eSBrian Feldman if (entry->wired_count == 0) { 1952b2f3846aSAlan Cox /* 1953b2f3846aSAlan Cox * Retain the map lock. 1954b2f3846aSAlan Cox */ 19554be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 19564be14af9SAlan Cox entry->object.vm_object != NULL && 19574be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 1958b2f3846aSAlan Cox } 1959b2f3846aSAlan Cox } 1960acd9a301SAlan Cox KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 1961acd9a301SAlan Cox ("vm_map_unwire: in-transition flag missing")); 1962acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 1963acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 1964acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 1965acd9a301SAlan Cox need_wakeup = TRUE; 1966acd9a301SAlan Cox } 1967acd9a301SAlan Cox vm_map_simplify_entry(map, entry); 1968acd9a301SAlan Cox entry = entry->next; 1969acd9a301SAlan Cox } 1970acd9a301SAlan Cox vm_map_unlock(map); 1971acd9a301SAlan Cox if (need_wakeup) 1972acd9a301SAlan Cox vm_map_wakeup(map); 1973acd9a301SAlan Cox return (rv); 1974acd9a301SAlan Cox } 1975acd9a301SAlan Cox 1976acd9a301SAlan Cox /* 1977e27e17b7SAlan Cox * vm_map_wire: 1978e27e17b7SAlan Cox * 1979e27e17b7SAlan Cox * Implements both kernel and user wiring. 1980e27e17b7SAlan Cox */ 1981e27e17b7SAlan Cox int 1982e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 1983abd498aaSBruce M Simpson int flags) 1984e27e17b7SAlan Cox { 198512d7cc84SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 198612d7cc84SAlan Cox vm_offset_t saved_end, saved_start; 198712d7cc84SAlan Cox unsigned int last_timestamp; 198812d7cc84SAlan Cox int rv; 19894be14af9SAlan Cox boolean_t fictitious, need_wakeup, result, user_wire; 1990e27e17b7SAlan Cox 1991abd498aaSBruce M Simpson user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 199212d7cc84SAlan Cox vm_map_lock(map); 199312d7cc84SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 199412d7cc84SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 1995abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 1996cbef13d8SAlan Cox first_entry = first_entry->next; 1997abd498aaSBruce M Simpson else { 199812d7cc84SAlan Cox vm_map_unlock(map); 199912d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 200012d7cc84SAlan Cox } 2001abd498aaSBruce M Simpson } 200212d7cc84SAlan Cox last_timestamp = map->timestamp; 200312d7cc84SAlan Cox entry = first_entry; 200412d7cc84SAlan Cox while (entry != &map->header && entry->start < end) { 200512d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 200612d7cc84SAlan Cox /* 200712d7cc84SAlan Cox * We have not yet clipped the entry. 200812d7cc84SAlan Cox */ 200912d7cc84SAlan Cox saved_start = (start >= entry->start) ? start : 201012d7cc84SAlan Cox entry->start; 201112d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 20128ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 201312d7cc84SAlan Cox /* 201412d7cc84SAlan Cox * Allow interruption of user wiring? 201512d7cc84SAlan Cox */ 201612d7cc84SAlan Cox } 201712d7cc84SAlan Cox vm_map_lock(map); 201812d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 201912d7cc84SAlan Cox /* 202012d7cc84SAlan Cox * Look again for the entry because the map was 202112d7cc84SAlan Cox * modified while it was unlocked. 202212d7cc84SAlan Cox * Specifically, the entry may have been 202312d7cc84SAlan Cox * clipped, merged, or deleted. 202412d7cc84SAlan Cox */ 202512d7cc84SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 202612d7cc84SAlan Cox &tmp_entry)) { 2027cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2028cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2029cbef13d8SAlan Cox else { 203012d7cc84SAlan Cox if (saved_start == start) { 203112d7cc84SAlan Cox /* 203212d7cc84SAlan Cox * first_entry has been deleted. 203312d7cc84SAlan Cox */ 203412d7cc84SAlan Cox vm_map_unlock(map); 203512d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 203612d7cc84SAlan Cox } 203712d7cc84SAlan Cox end = saved_start; 203812d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 203912d7cc84SAlan Cox goto done; 204012d7cc84SAlan Cox } 2041cbef13d8SAlan Cox } 204212d7cc84SAlan Cox if (entry == first_entry) 204312d7cc84SAlan Cox first_entry = tmp_entry; 204412d7cc84SAlan Cox else 204512d7cc84SAlan Cox first_entry = NULL; 204612d7cc84SAlan Cox entry = tmp_entry; 204712d7cc84SAlan Cox } 204812d7cc84SAlan Cox last_timestamp = map->timestamp; 204912d7cc84SAlan Cox continue; 205012d7cc84SAlan Cox } 205112d7cc84SAlan Cox vm_map_clip_start(map, entry, start); 205212d7cc84SAlan Cox vm_map_clip_end(map, entry, end); 205312d7cc84SAlan Cox /* 205412d7cc84SAlan Cox * Mark the entry in case the map lock is released. (See 205512d7cc84SAlan Cox * above.) 205612d7cc84SAlan Cox */ 205712d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 205812d7cc84SAlan Cox /* 20590ada205eSBrian Feldman * 206012d7cc84SAlan Cox */ 20610ada205eSBrian Feldman if (entry->wired_count == 0) { 20620ada205eSBrian Feldman entry->wired_count++; 206312d7cc84SAlan Cox saved_start = entry->start; 206412d7cc84SAlan Cox saved_end = entry->end; 20654be14af9SAlan Cox fictitious = entry->object.vm_object != NULL && 20664be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE; 206712d7cc84SAlan Cox /* 206812d7cc84SAlan Cox * Release the map lock, relying on the in-transition 206912d7cc84SAlan Cox * mark. 207012d7cc84SAlan Cox */ 207112d7cc84SAlan Cox vm_map_unlock(map); 2072ef594d31SAlan Cox rv = vm_fault_wire(map, saved_start, saved_end, 20734be14af9SAlan Cox user_wire, fictitious); 207412d7cc84SAlan Cox vm_map_lock(map); 207512d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 207612d7cc84SAlan Cox /* 207712d7cc84SAlan Cox * Look again for the entry because the map was 207812d7cc84SAlan Cox * modified while it was unlocked. The entry 207912d7cc84SAlan Cox * may have been clipped, but NOT merged or 208012d7cc84SAlan Cox * deleted. 208112d7cc84SAlan Cox */ 208212d7cc84SAlan Cox result = vm_map_lookup_entry(map, saved_start, 208312d7cc84SAlan Cox &tmp_entry); 208412d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 208512d7cc84SAlan Cox if (entry == first_entry) 208612d7cc84SAlan Cox first_entry = tmp_entry; 208712d7cc84SAlan Cox else 208812d7cc84SAlan Cox first_entry = NULL; 208912d7cc84SAlan Cox entry = tmp_entry; 209028c58286SAlan Cox while (entry->end < saved_end) { 209128c58286SAlan Cox if (rv != KERN_SUCCESS) { 209228c58286SAlan Cox KASSERT(entry->wired_count == 1, 209328c58286SAlan Cox ("vm_map_wire: bad count")); 209428c58286SAlan Cox entry->wired_count = -1; 209528c58286SAlan Cox } 209612d7cc84SAlan Cox entry = entry->next; 209712d7cc84SAlan Cox } 209828c58286SAlan Cox } 209912d7cc84SAlan Cox last_timestamp = map->timestamp; 210012d7cc84SAlan Cox if (rv != KERN_SUCCESS) { 210128c58286SAlan Cox KASSERT(entry->wired_count == 1, 210228c58286SAlan Cox ("vm_map_wire: bad count")); 210312d7cc84SAlan Cox /* 210428c58286SAlan Cox * Assign an out-of-range value to represent 210528c58286SAlan Cox * the failure to wire this entry. 210612d7cc84SAlan Cox */ 210728c58286SAlan Cox entry->wired_count = -1; 210812d7cc84SAlan Cox end = entry->end; 210912d7cc84SAlan Cox goto done; 211012d7cc84SAlan Cox } 21110ada205eSBrian Feldman } else if (!user_wire || 21120ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 21130ada205eSBrian Feldman entry->wired_count++; 211412d7cc84SAlan Cox } 211512d7cc84SAlan Cox /* 211612d7cc84SAlan Cox * Check the map for holes in the specified region. 2117abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 211812d7cc84SAlan Cox */ 2119abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2120abd498aaSBruce M Simpson (entry->end < end && (entry->next == &map->header || 2121abd498aaSBruce M Simpson entry->next->start > entry->end))) { 212212d7cc84SAlan Cox end = entry->end; 212312d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 212412d7cc84SAlan Cox goto done; 212512d7cc84SAlan Cox } 212612d7cc84SAlan Cox entry = entry->next; 212712d7cc84SAlan Cox } 212812d7cc84SAlan Cox rv = KERN_SUCCESS; 212912d7cc84SAlan Cox done: 213012d7cc84SAlan Cox need_wakeup = FALSE; 213112d7cc84SAlan Cox if (first_entry == NULL) { 213212d7cc84SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2133cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2134cbef13d8SAlan Cox first_entry = first_entry->next; 2135cbef13d8SAlan Cox else 213612d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 213712d7cc84SAlan Cox } 213812d7cc84SAlan Cox entry = first_entry; 213912d7cc84SAlan Cox while (entry != &map->header && entry->start < end) { 214012d7cc84SAlan Cox if (rv == KERN_SUCCESS) { 214112d7cc84SAlan Cox if (user_wire) 214212d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_USER_WIRED; 214328c58286SAlan Cox } else if (entry->wired_count == -1) { 214428c58286SAlan Cox /* 214528c58286SAlan Cox * Wiring failed on this entry. Thus, unwiring is 214628c58286SAlan Cox * unnecessary. 214728c58286SAlan Cox */ 214828c58286SAlan Cox entry->wired_count = 0; 214912d7cc84SAlan Cox } else { 21500ada205eSBrian Feldman if (!user_wire || 21510ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) 215212d7cc84SAlan Cox entry->wired_count--; 21530ada205eSBrian Feldman if (entry->wired_count == 0) { 215412d7cc84SAlan Cox /* 215512d7cc84SAlan Cox * Retain the map lock. 215612d7cc84SAlan Cox */ 21574be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 21584be14af9SAlan Cox entry->object.vm_object != NULL && 21594be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 216012d7cc84SAlan Cox } 216112d7cc84SAlan Cox } 216212d7cc84SAlan Cox KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION, 216312d7cc84SAlan Cox ("vm_map_wire: in-transition flag missing")); 216412d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 216512d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 216612d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 216712d7cc84SAlan Cox need_wakeup = TRUE; 216812d7cc84SAlan Cox } 216912d7cc84SAlan Cox vm_map_simplify_entry(map, entry); 217012d7cc84SAlan Cox entry = entry->next; 217112d7cc84SAlan Cox } 217212d7cc84SAlan Cox vm_map_unlock(map); 217312d7cc84SAlan Cox if (need_wakeup) 217412d7cc84SAlan Cox vm_map_wakeup(map); 217512d7cc84SAlan Cox return (rv); 2176e27e17b7SAlan Cox } 2177e27e17b7SAlan Cox 2178e27e17b7SAlan Cox /* 2179950f8459SAlan Cox * vm_map_sync 2180df8bae1dSRodney W. Grimes * 2181df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 2182df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 2183df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 2184df8bae1dSRodney W. Grimes * 2185637315edSAlan Cox * If the size of the region from start to end is zero, we are 2186637315edSAlan Cox * supposed to flush all modified pages within the region containing 2187637315edSAlan Cox * start. Unfortunately, a region can be split or coalesced with 2188637315edSAlan Cox * neighboring regions, making it difficult to determine what the 2189637315edSAlan Cox * original region was. Therefore, we approximate this requirement by 2190637315edSAlan Cox * flushing the current region containing start. 2191637315edSAlan Cox * 2192df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 2193df8bae1dSRodney W. Grimes */ 2194df8bae1dSRodney W. Grimes int 2195950f8459SAlan Cox vm_map_sync( 21961b40f8c0SMatthew Dillon vm_map_t map, 21971b40f8c0SMatthew Dillon vm_offset_t start, 21981b40f8c0SMatthew Dillon vm_offset_t end, 21991b40f8c0SMatthew Dillon boolean_t syncio, 22001b40f8c0SMatthew Dillon boolean_t invalidate) 2201df8bae1dSRodney W. Grimes { 2202c0877f10SJohn Dyson vm_map_entry_t current; 2203df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2204df8bae1dSRodney W. Grimes vm_size_t size; 2205df8bae1dSRodney W. Grimes vm_object_t object; 2206a316d390SJohn Dyson vm_ooffset_t offset; 2207df8bae1dSRodney W. Grimes 2208df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2209df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2210df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 2211df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2212df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2213637315edSAlan Cox } else if (start == end) { 2214637315edSAlan Cox start = entry->start; 2215637315edSAlan Cox end = entry->end; 2216df8bae1dSRodney W. Grimes } 2217df8bae1dSRodney W. Grimes /* 2218b7b7cd44SAlan Cox * Make a first pass to check for user-wired memory and holes. 2219df8bae1dSRodney W. Grimes */ 22207b0e72d1SAlan Cox for (current = entry; current != &map->header && current->start < end; 22217b0e72d1SAlan Cox current = current->next) { 2222b7b7cd44SAlan Cox if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2223df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2224df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2225df8bae1dSRodney W. Grimes } 2226df8bae1dSRodney W. Grimes if (end > current->end && 2227df8bae1dSRodney W. Grimes (current->next == &map->header || 2228df8bae1dSRodney W. Grimes current->end != current->next->start)) { 2229df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2230df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2231df8bae1dSRodney W. Grimes } 2232df8bae1dSRodney W. Grimes } 2233df8bae1dSRodney W. Grimes 22342cf13952SAlan Cox if (invalidate) 2235bc105a67SAlan Cox pmap_remove(map->pmap, start, end); 22362cf13952SAlan Cox 2237df8bae1dSRodney W. Grimes /* 2238df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 2239df8bae1dSRodney W. Grimes * objects as we go. 2240df8bae1dSRodney W. Grimes */ 22417b0e72d1SAlan Cox for (current = entry; current != &map->header && current->start < end; 22427b0e72d1SAlan Cox current = current->next) { 2243df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 2244df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 22459fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2246c0877f10SJohn Dyson vm_map_t smap; 2247df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 2248df8bae1dSRodney W. Grimes vm_size_t tsize; 2249df8bae1dSRodney W. Grimes 22509fdfe602SMatthew Dillon smap = current->object.sub_map; 2251df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 2252df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 2253df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 2254df8bae1dSRodney W. Grimes if (tsize < size) 2255df8bae1dSRodney W. Grimes size = tsize; 2256df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 2257df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 2258df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 2259df8bae1dSRodney W. Grimes } else { 2260df8bae1dSRodney W. Grimes object = current->object.vm_object; 2261df8bae1dSRodney W. Grimes } 2262950f8459SAlan Cox vm_object_sync(object, offset, size, syncio, invalidate); 2263df8bae1dSRodney W. Grimes start += size; 2264df8bae1dSRodney W. Grimes } 2265df8bae1dSRodney W. Grimes 2266df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2267df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2268df8bae1dSRodney W. Grimes } 2269df8bae1dSRodney W. Grimes 2270df8bae1dSRodney W. Grimes /* 2271df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 2272df8bae1dSRodney W. Grimes * 2273df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 2274df8bae1dSRodney W. Grimes * 2275df8bae1dSRodney W. Grimes * The map in question should be locked. 2276df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 2277df8bae1dSRodney W. Grimes */ 22780362d7d7SJohn Dyson static void 22791b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2280df8bae1dSRodney W. Grimes { 22814be14af9SAlan Cox vm_fault_unwire(map, entry->start, entry->end, 22824be14af9SAlan Cox entry->object.vm_object != NULL && 22834be14af9SAlan Cox entry->object.vm_object->type == OBJT_DEVICE); 2284df8bae1dSRodney W. Grimes entry->wired_count = 0; 2285df8bae1dSRodney W. Grimes } 2286df8bae1dSRodney W. Grimes 2287df8bae1dSRodney W. Grimes /* 2288df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 2289df8bae1dSRodney W. Grimes * 2290df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 2291df8bae1dSRodney W. Grimes */ 22920362d7d7SJohn Dyson static void 22931b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2294df8bae1dSRodney W. Grimes { 229532a89c32SAlan Cox vm_object_t object; 229632a89c32SAlan Cox vm_pindex_t offidxstart, offidxend, count; 229732a89c32SAlan Cox 2298df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 2299df8bae1dSRodney W. Grimes map->size -= entry->end - entry->start; 2300df8bae1dSRodney W. Grimes 230132a89c32SAlan Cox if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 230232a89c32SAlan Cox (object = entry->object.vm_object) != NULL) { 230332a89c32SAlan Cox count = OFF_TO_IDX(entry->end - entry->start); 230432a89c32SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 230532a89c32SAlan Cox offidxend = offidxstart + count; 230632a89c32SAlan Cox VM_OBJECT_LOCK(object); 230732a89c32SAlan Cox if (object->ref_count != 1 && 230832a89c32SAlan Cox ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 23099f5c801bSAlan Cox object == kernel_object || object == kmem_object)) { 231032a89c32SAlan Cox vm_object_collapse(object); 2311b103b948SDag-Erling Smørgrav vm_object_page_remove(object, offidxstart, offidxend, FALSE); 231232a89c32SAlan Cox if (object->type == OBJT_SWAP) 231332a89c32SAlan Cox swap_pager_freespace(object, offidxstart, count); 231432a89c32SAlan Cox if (offidxend >= object->size && 231532a89c32SAlan Cox offidxstart < object->size) 231632a89c32SAlan Cox object->size = offidxstart; 231732a89c32SAlan Cox } 231832a89c32SAlan Cox VM_OBJECT_UNLOCK(object); 231932a89c32SAlan Cox vm_object_deallocate(object); 2320b5b40fa6SJohn Dyson } 2321df8bae1dSRodney W. Grimes 2322df8bae1dSRodney W. Grimes vm_map_entry_dispose(map, entry); 2323df8bae1dSRodney W. Grimes } 2324df8bae1dSRodney W. Grimes 2325df8bae1dSRodney W. Grimes /* 2326df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 2327df8bae1dSRodney W. Grimes * 2328df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 2329df8bae1dSRodney W. Grimes * map. 2330df8bae1dSRodney W. Grimes */ 2331df8bae1dSRodney W. Grimes int 23321b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2333df8bae1dSRodney W. Grimes { 2334c0877f10SJohn Dyson vm_map_entry_t entry; 2335df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 2336df8bae1dSRodney W. Grimes 2337df8bae1dSRodney W. Grimes /* 2338df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 2339df8bae1dSRodney W. Grimes */ 2340876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 2341df8bae1dSRodney W. Grimes entry = first_entry->next; 2342876318ecSAlan Cox else { 2343df8bae1dSRodney W. Grimes entry = first_entry; 2344df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 2345df8bae1dSRodney W. Grimes } 2346df8bae1dSRodney W. Grimes 2347df8bae1dSRodney W. Grimes /* 2348df8bae1dSRodney W. Grimes * Step through all entries in this region 2349df8bae1dSRodney W. Grimes */ 2350df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 2351df8bae1dSRodney W. Grimes vm_map_entry_t next; 2352df8bae1dSRodney W. Grimes 235373b2baceSAlan Cox /* 235473b2baceSAlan Cox * Wait for wiring or unwiring of an entry to complete. 23557c938963SBrian Feldman * Also wait for any system wirings to disappear on 23567c938963SBrian Feldman * user maps. 235773b2baceSAlan Cox */ 23587c938963SBrian Feldman if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 23597c938963SBrian Feldman (vm_map_pmap(map) != kernel_pmap && 23607c938963SBrian Feldman vm_map_entry_system_wired_count(entry) != 0)) { 236173b2baceSAlan Cox unsigned int last_timestamp; 236273b2baceSAlan Cox vm_offset_t saved_start; 236373b2baceSAlan Cox vm_map_entry_t tmp_entry; 236473b2baceSAlan Cox 236573b2baceSAlan Cox saved_start = entry->start; 236673b2baceSAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 236773b2baceSAlan Cox last_timestamp = map->timestamp; 23688ce2d00aSPawel Jakub Dawidek (void) vm_map_unlock_and_wait(map, 0); 236973b2baceSAlan Cox vm_map_lock(map); 237073b2baceSAlan Cox if (last_timestamp + 1 != map->timestamp) { 237173b2baceSAlan Cox /* 237273b2baceSAlan Cox * Look again for the entry because the map was 237373b2baceSAlan Cox * modified while it was unlocked. 237473b2baceSAlan Cox * Specifically, the entry may have been 237573b2baceSAlan Cox * clipped, merged, or deleted. 237673b2baceSAlan Cox */ 237773b2baceSAlan Cox if (!vm_map_lookup_entry(map, saved_start, 237873b2baceSAlan Cox &tmp_entry)) 237973b2baceSAlan Cox entry = tmp_entry->next; 238073b2baceSAlan Cox else { 238173b2baceSAlan Cox entry = tmp_entry; 238273b2baceSAlan Cox vm_map_clip_start(map, entry, 238373b2baceSAlan Cox saved_start); 238473b2baceSAlan Cox } 238573b2baceSAlan Cox } 238673b2baceSAlan Cox continue; 238773b2baceSAlan Cox } 2388df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 2389df8bae1dSRodney W. Grimes 2390c0877f10SJohn Dyson next = entry->next; 2391df8bae1dSRodney W. Grimes 2392df8bae1dSRodney W. Grimes /* 23930d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 23940d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 2395df8bae1dSRodney W. Grimes */ 2396c0877f10SJohn Dyson if (entry->wired_count != 0) { 2397df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 2398c0877f10SJohn Dyson } 2399df8bae1dSRodney W. Grimes 240032a89c32SAlan Cox pmap_remove(map->pmap, entry->start, entry->end); 2401df8bae1dSRodney W. Grimes 2402df8bae1dSRodney W. Grimes /* 24030d94caffSDavid Greenman * Delete the entry (which may delete the object) only after 24040d94caffSDavid Greenman * removing all pmap entries pointing to its pages. 24050d94caffSDavid Greenman * (Otherwise, its page frames may be reallocated, and any 24060d94caffSDavid Greenman * modify bits will be set in the wrong object!) 2407df8bae1dSRodney W. Grimes */ 2408df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 2409df8bae1dSRodney W. Grimes entry = next; 2410df8bae1dSRodney W. Grimes } 2411df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2412df8bae1dSRodney W. Grimes } 2413df8bae1dSRodney W. Grimes 2414df8bae1dSRodney W. Grimes /* 2415df8bae1dSRodney W. Grimes * vm_map_remove: 2416df8bae1dSRodney W. Grimes * 2417df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 2418df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 2419df8bae1dSRodney W. Grimes */ 2420df8bae1dSRodney W. Grimes int 24211b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2422df8bae1dSRodney W. Grimes { 24236eaee3feSAlan Cox int result; 2424df8bae1dSRodney W. Grimes 2425df8bae1dSRodney W. Grimes vm_map_lock(map); 2426df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2427df8bae1dSRodney W. Grimes result = vm_map_delete(map, start, end); 2428df8bae1dSRodney W. Grimes vm_map_unlock(map); 2429df8bae1dSRodney W. Grimes return (result); 2430df8bae1dSRodney W. Grimes } 2431df8bae1dSRodney W. Grimes 2432df8bae1dSRodney W. Grimes /* 2433df8bae1dSRodney W. Grimes * vm_map_check_protection: 2434df8bae1dSRodney W. Grimes * 24352d5c7e45SMatthew Dillon * Assert that the target map allows the specified privilege on the 24362d5c7e45SMatthew Dillon * entire address region given. The entire region must be allocated. 24372d5c7e45SMatthew Dillon * 24382d5c7e45SMatthew Dillon * WARNING! This code does not and should not check whether the 24392d5c7e45SMatthew Dillon * contents of the region is accessible. For example a smaller file 24402d5c7e45SMatthew Dillon * might be mapped into a larger address space. 24412d5c7e45SMatthew Dillon * 24422d5c7e45SMatthew Dillon * NOTE! This code is also called by munmap(). 2443d8834602SAlan Cox * 2444d8834602SAlan Cox * The map must be locked. A read lock is sufficient. 2445df8bae1dSRodney W. Grimes */ 24460d94caffSDavid Greenman boolean_t 2447b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2448b9dcd593SBruce Evans vm_prot_t protection) 2449df8bae1dSRodney W. Grimes { 2450c0877f10SJohn Dyson vm_map_entry_t entry; 2451df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2452df8bae1dSRodney W. Grimes 2453d8834602SAlan Cox if (!vm_map_lookup_entry(map, start, &tmp_entry)) 2454df8bae1dSRodney W. Grimes return (FALSE); 2455df8bae1dSRodney W. Grimes entry = tmp_entry; 2456df8bae1dSRodney W. Grimes 2457df8bae1dSRodney W. Grimes while (start < end) { 2458d8834602SAlan Cox if (entry == &map->header) 2459df8bae1dSRodney W. Grimes return (FALSE); 2460df8bae1dSRodney W. Grimes /* 2461df8bae1dSRodney W. Grimes * No holes allowed! 2462df8bae1dSRodney W. Grimes */ 2463d8834602SAlan Cox if (start < entry->start) 2464df8bae1dSRodney W. Grimes return (FALSE); 2465df8bae1dSRodney W. Grimes /* 2466df8bae1dSRodney W. Grimes * Check protection associated with entry. 2467df8bae1dSRodney W. Grimes */ 2468d8834602SAlan Cox if ((entry->protection & protection) != protection) 2469df8bae1dSRodney W. Grimes return (FALSE); 2470df8bae1dSRodney W. Grimes /* go to next entry */ 2471df8bae1dSRodney W. Grimes start = entry->end; 2472df8bae1dSRodney W. Grimes entry = entry->next; 2473df8bae1dSRodney W. Grimes } 2474df8bae1dSRodney W. Grimes return (TRUE); 2475df8bae1dSRodney W. Grimes } 2476df8bae1dSRodney W. Grimes 247786524867SJohn Dyson /* 2478df8bae1dSRodney W. Grimes * vm_map_copy_entry: 2479df8bae1dSRodney W. Grimes * 2480df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 2481df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 2482df8bae1dSRodney W. Grimes */ 2483f708ef1bSPoul-Henning Kamp static void 24841b40f8c0SMatthew Dillon vm_map_copy_entry( 24851b40f8c0SMatthew Dillon vm_map_t src_map, 24861b40f8c0SMatthew Dillon vm_map_t dst_map, 24871b40f8c0SMatthew Dillon vm_map_entry_t src_entry, 24881b40f8c0SMatthew Dillon vm_map_entry_t dst_entry) 2489df8bae1dSRodney W. Grimes { 2490c0877f10SJohn Dyson vm_object_t src_object; 2491c0877f10SJohn Dyson 24929fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2493df8bae1dSRodney W. Grimes return; 2494df8bae1dSRodney W. Grimes 2495df8bae1dSRodney W. Grimes if (src_entry->wired_count == 0) { 2496df8bae1dSRodney W. Grimes 2497df8bae1dSRodney W. Grimes /* 24980d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 24990d94caffSDavid Greenman * write-protected. 2500df8bae1dSRodney W. Grimes */ 2501afa07f7eSJohn Dyson if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2502df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 2503df8bae1dSRodney W. Grimes src_entry->start, 2504df8bae1dSRodney W. Grimes src_entry->end, 2505df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 2506df8bae1dSRodney W. Grimes } 2507b18bfc3dSJohn Dyson 2508df8bae1dSRodney W. Grimes /* 2509df8bae1dSRodney W. Grimes * Make a copy of the object. 2510df8bae1dSRodney W. Grimes */ 25118aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 2512a89c6258SAlan Cox VM_OBJECT_LOCK(src_object); 2513c0877f10SJohn Dyson if ((src_object->handle == NULL) && 2514c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 2515c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 2516c0877f10SJohn Dyson vm_object_collapse(src_object); 251796fb8cf2SJohn Dyson if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2518c5aaa06dSAlan Cox vm_object_split(src_entry); 2519c0877f10SJohn Dyson src_object = src_entry->object.vm_object; 2520a89c6258SAlan Cox } 2521a89c6258SAlan Cox } 2522b921a12bSAlan Cox vm_object_reference_locked(src_object); 2523069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2524e2479b4fSAlan Cox VM_OBJECT_UNLOCK(src_object); 2525c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 2526afa07f7eSJohn Dyson src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2527afa07f7eSJohn Dyson dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2528b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 2529b18bfc3dSJohn Dyson } else { 2530b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 2531b18bfc3dSJohn Dyson dst_entry->offset = 0; 2532b18bfc3dSJohn Dyson } 2533df8bae1dSRodney W. Grimes 2534df8bae1dSRodney W. Grimes pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2535df8bae1dSRodney W. Grimes dst_entry->end - dst_entry->start, src_entry->start); 25360d94caffSDavid Greenman } else { 2537df8bae1dSRodney W. Grimes /* 2538df8bae1dSRodney W. Grimes * Of course, wired down pages can't be set copy-on-write. 25390d94caffSDavid Greenman * Cause wired pages to be copied into the new map by 25400d94caffSDavid Greenman * simulating faults (the new pages are pageable) 2541df8bae1dSRodney W. Grimes */ 2542df8bae1dSRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2543df8bae1dSRodney W. Grimes } 2544df8bae1dSRodney W. Grimes } 2545df8bae1dSRodney W. Grimes 2546df8bae1dSRodney W. Grimes /* 25472a7be1b6SBrian Feldman * vmspace_map_entry_forked: 25482a7be1b6SBrian Feldman * Update the newly-forked vmspace each time a map entry is inherited 25492a7be1b6SBrian Feldman * or copied. The values for vm_dsize and vm_tsize are approximate 25502a7be1b6SBrian Feldman * (and mostly-obsolete ideas in the face of mmap(2) et al.) 25512a7be1b6SBrian Feldman */ 25522a7be1b6SBrian Feldman static void 25532a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 25542a7be1b6SBrian Feldman vm_map_entry_t entry) 25552a7be1b6SBrian Feldman { 25562a7be1b6SBrian Feldman vm_size_t entrysize; 25572a7be1b6SBrian Feldman vm_offset_t newend; 25582a7be1b6SBrian Feldman 25592a7be1b6SBrian Feldman entrysize = entry->end - entry->start; 25602a7be1b6SBrian Feldman vm2->vm_map.size += entrysize; 25612a7be1b6SBrian Feldman if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 25622a7be1b6SBrian Feldman vm2->vm_ssize += btoc(entrysize); 25632a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 25642a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 2565b351299cSAndrew Gallatin newend = MIN(entry->end, 25662a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 25672a7be1b6SBrian Feldman vm2->vm_dsize += btoc(newend - entry->start); 25682a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 25692a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 2570b351299cSAndrew Gallatin newend = MIN(entry->end, 25712a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 25722a7be1b6SBrian Feldman vm2->vm_tsize += btoc(newend - entry->start); 25732a7be1b6SBrian Feldman } 25742a7be1b6SBrian Feldman } 25752a7be1b6SBrian Feldman 25762a7be1b6SBrian Feldman /* 2577df8bae1dSRodney W. Grimes * vmspace_fork: 2578df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 2579df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 2580df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 2581df8bae1dSRodney W. Grimes * values on the regions in that map. 2582df8bae1dSRodney W. Grimes * 25832a7be1b6SBrian Feldman * XXX It might be worth coalescing the entries added to the new vmspace. 25842a7be1b6SBrian Feldman * 2585df8bae1dSRodney W. Grimes * The source map must not be locked. 2586df8bae1dSRodney W. Grimes */ 2587df8bae1dSRodney W. Grimes struct vmspace * 25881b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1) 2589df8bae1dSRodney W. Grimes { 2590c0877f10SJohn Dyson struct vmspace *vm2; 2591df8bae1dSRodney W. Grimes vm_map_t old_map = &vm1->vm_map; 2592df8bae1dSRodney W. Grimes vm_map_t new_map; 2593df8bae1dSRodney W. Grimes vm_map_entry_t old_entry; 2594df8bae1dSRodney W. Grimes vm_map_entry_t new_entry; 2595de5f6a77SJohn Dyson vm_object_t object; 2596df8bae1dSRodney W. Grimes 2597df8bae1dSRodney W. Grimes vm_map_lock(old_map); 2598df8bae1dSRodney W. Grimes 25992d8acc0fSJohn Dyson vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 260089b57fcfSKonstantin Belousov if (vm2 == NULL) 260189b57fcfSKonstantin Belousov goto unlock_and_return; 26022a7be1b6SBrian Feldman vm2->vm_taddr = vm1->vm_taddr; 26032a7be1b6SBrian Feldman vm2->vm_daddr = vm1->vm_daddr; 26042a7be1b6SBrian Feldman vm2->vm_maxsaddr = vm1->vm_maxsaddr; 2605df8bae1dSRodney W. Grimes new_map = &vm2->vm_map; /* XXX */ 260647221757SJohn Dyson new_map->timestamp = 1; 2607df8bae1dSRodney W. Grimes 2608df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 2609df8bae1dSRodney W. Grimes 2610df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 2611afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2612df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 2613df8bae1dSRodney W. Grimes 2614df8bae1dSRodney W. Grimes switch (old_entry->inheritance) { 2615df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2616df8bae1dSRodney W. Grimes break; 2617df8bae1dSRodney W. Grimes 2618df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 2619df8bae1dSRodney W. Grimes /* 2620fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 2621fed9a903SJohn Dyson */ 2622fed9a903SJohn Dyson object = old_entry->object.vm_object; 2623fed9a903SJohn Dyson if (object == NULL) { 2624fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 2625c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 2626fed9a903SJohn Dyson old_entry->object.vm_object = object; 262715d2d313SAlan Cox old_entry->offset = 0; 26289a2f6362SAlan Cox } 26299a2f6362SAlan Cox 26309a2f6362SAlan Cox /* 26319a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 26329a2f6362SAlan Cox * to insure that a shadow object is created. 26339a2f6362SAlan Cox */ 26349a2f6362SAlan Cox vm_object_reference(object); 26359a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 26365069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 26375069bf57SJohn Dyson &old_entry->offset, 2638c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 26395069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2640d30344bdSIan Dowse /* Transfer the second reference too. */ 2641d30344bdSIan Dowse vm_object_reference( 2642d30344bdSIan Dowse old_entry->object.vm_object); 2643d30344bdSIan Dowse vm_object_deallocate(object); 26445069bf57SJohn Dyson object = old_entry->object.vm_object; 2645fed9a903SJohn Dyson } 2646e2479b4fSAlan Cox VM_OBJECT_LOCK(object); 2647069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 2648e2479b4fSAlan Cox VM_OBJECT_UNLOCK(object); 2649fed9a903SJohn Dyson 2650fed9a903SJohn Dyson /* 2651ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 2652df8bae1dSRodney W. Grimes */ 2653df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2654df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2655028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2656df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2657df8bae1dSRodney W. Grimes 2658df8bae1dSRodney W. Grimes /* 26590d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 26600d94caffSDavid Greenman * inserting at the end of the new map. 2661df8bae1dSRodney W. Grimes */ 2662df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2663df8bae1dSRodney W. Grimes new_entry); 26642a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 2665df8bae1dSRodney W. Grimes 2666df8bae1dSRodney W. Grimes /* 2667df8bae1dSRodney W. Grimes * Update the physical map 2668df8bae1dSRodney W. Grimes */ 2669df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 2670df8bae1dSRodney W. Grimes new_entry->start, 2671df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 2672df8bae1dSRodney W. Grimes old_entry->start); 2673df8bae1dSRodney W. Grimes break; 2674df8bae1dSRodney W. Grimes 2675df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2676df8bae1dSRodney W. Grimes /* 2677df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 2678df8bae1dSRodney W. Grimes */ 2679df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2680df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2681028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2682df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2683df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 2684df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2685df8bae1dSRodney W. Grimes new_entry); 26862a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 2687bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 2688bd7e5f99SJohn Dyson new_entry); 2689df8bae1dSRodney W. Grimes break; 2690df8bae1dSRodney W. Grimes } 2691df8bae1dSRodney W. Grimes old_entry = old_entry->next; 2692df8bae1dSRodney W. Grimes } 269389b57fcfSKonstantin Belousov unlock_and_return: 2694df8bae1dSRodney W. Grimes vm_map_unlock(old_map); 2695df8bae1dSRodney W. Grimes 2696df8bae1dSRodney W. Grimes return (vm2); 2697df8bae1dSRodney W. Grimes } 2698df8bae1dSRodney W. Grimes 269994f7e29aSAlan Cox int 270094f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 270194f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 270294f7e29aSAlan Cox { 2703fd75d710SMarcel Moolenaar vm_map_entry_t new_entry, prev_entry; 2704fd75d710SMarcel Moolenaar vm_offset_t bot, top; 270594f7e29aSAlan Cox vm_size_t init_ssize; 2706fd75d710SMarcel Moolenaar int orient, rv; 270791d5354aSJohn Baldwin rlim_t vmemlim; 270894f7e29aSAlan Cox 2709fd75d710SMarcel Moolenaar /* 2710fd75d710SMarcel Moolenaar * The stack orientation is piggybacked with the cow argument. 2711fd75d710SMarcel Moolenaar * Extract it into orient and mask the cow argument so that we 2712fd75d710SMarcel Moolenaar * don't pass it around further. 2713fd75d710SMarcel Moolenaar * NOTE: We explicitly allow bi-directional stacks. 2714fd75d710SMarcel Moolenaar */ 2715fd75d710SMarcel Moolenaar orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP); 2716fd75d710SMarcel Moolenaar cow &= ~orient; 2717fd75d710SMarcel Moolenaar KASSERT(orient != 0, ("No stack grow direction")); 2718fd75d710SMarcel Moolenaar 271977bc7900SKonstantin Belousov if (addrbos < vm_map_min(map) || 272077bc7900SKonstantin Belousov addrbos > vm_map_max(map) || 272177bc7900SKonstantin Belousov addrbos + max_ssize < addrbos) 272294f7e29aSAlan Cox return (KERN_NO_SPACE); 2723fd75d710SMarcel Moolenaar 2724fd75d710SMarcel Moolenaar init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz; 272594f7e29aSAlan Cox 272691d5354aSJohn Baldwin PROC_LOCK(curthread->td_proc); 272791d5354aSJohn Baldwin vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM); 272891d5354aSJohn Baldwin PROC_UNLOCK(curthread->td_proc); 272991d5354aSJohn Baldwin 273094f7e29aSAlan Cox vm_map_lock(map); 273194f7e29aSAlan Cox 273294f7e29aSAlan Cox /* If addr is already mapped, no go */ 273394f7e29aSAlan Cox if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 273494f7e29aSAlan Cox vm_map_unlock(map); 273594f7e29aSAlan Cox return (KERN_NO_SPACE); 273694f7e29aSAlan Cox } 273794f7e29aSAlan Cox 2738a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 273991d5354aSJohn Baldwin if (map->size + init_ssize > vmemlim) { 2740a69ac174SMatthew Dillon vm_map_unlock(map); 2741a69ac174SMatthew Dillon return (KERN_NO_SPACE); 2742a69ac174SMatthew Dillon } 2743a69ac174SMatthew Dillon 2744fd75d710SMarcel Moolenaar /* 2745fd75d710SMarcel Moolenaar * If we can't accomodate max_ssize in the current mapping, no go. 2746fd75d710SMarcel Moolenaar * However, we need to be aware that subsequent user mappings might 2747fd75d710SMarcel Moolenaar * map into the space we have reserved for stack, and currently this 2748fd75d710SMarcel Moolenaar * space is not protected. 274994f7e29aSAlan Cox * 2750fd75d710SMarcel Moolenaar * Hopefully we will at least detect this condition when we try to 2751fd75d710SMarcel Moolenaar * grow the stack. 275294f7e29aSAlan Cox */ 275394f7e29aSAlan Cox if ((prev_entry->next != &map->header) && 275494f7e29aSAlan Cox (prev_entry->next->start < addrbos + max_ssize)) { 275594f7e29aSAlan Cox vm_map_unlock(map); 275694f7e29aSAlan Cox return (KERN_NO_SPACE); 275794f7e29aSAlan Cox } 275894f7e29aSAlan Cox 2759fd75d710SMarcel Moolenaar /* 2760fd75d710SMarcel Moolenaar * We initially map a stack of only init_ssize. We will grow as 2761fd75d710SMarcel Moolenaar * needed later. Depending on the orientation of the stack (i.e. 2762fd75d710SMarcel Moolenaar * the grow direction) we either map at the top of the range, the 2763fd75d710SMarcel Moolenaar * bottom of the range or in the middle. 276494f7e29aSAlan Cox * 2765fd75d710SMarcel Moolenaar * Note: we would normally expect prot and max to be VM_PROT_ALL, 2766fd75d710SMarcel Moolenaar * and cow to be 0. Possibly we should eliminate these as input 2767fd75d710SMarcel Moolenaar * parameters, and just pass these values here in the insert call. 276894f7e29aSAlan Cox */ 2769fd75d710SMarcel Moolenaar if (orient == MAP_STACK_GROWS_DOWN) 2770fd75d710SMarcel Moolenaar bot = addrbos + max_ssize - init_ssize; 2771fd75d710SMarcel Moolenaar else if (orient == MAP_STACK_GROWS_UP) 2772fd75d710SMarcel Moolenaar bot = addrbos; 2773fd75d710SMarcel Moolenaar else 2774fd75d710SMarcel Moolenaar bot = round_page(addrbos + max_ssize/2 - init_ssize/2); 2775fd75d710SMarcel Moolenaar top = bot + init_ssize; 2776fd75d710SMarcel Moolenaar rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 277794f7e29aSAlan Cox 2778fd75d710SMarcel Moolenaar /* Now set the avail_ssize amount. */ 277994f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 278029b45e9eSAlan Cox if (prev_entry != &map->header) 2781fd75d710SMarcel Moolenaar vm_map_clip_end(map, prev_entry, bot); 2782fd75d710SMarcel Moolenaar new_entry = prev_entry->next; 2783fd75d710SMarcel Moolenaar if (new_entry->end != top || new_entry->start != bot) 278494f7e29aSAlan Cox panic("Bad entry start/end for new stack entry"); 2785b21a0008SMarcel Moolenaar 2786fd75d710SMarcel Moolenaar new_entry->avail_ssize = max_ssize - init_ssize; 2787fd75d710SMarcel Moolenaar if (orient & MAP_STACK_GROWS_DOWN) 2788fd75d710SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 2789fd75d710SMarcel Moolenaar if (orient & MAP_STACK_GROWS_UP) 2790fd75d710SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_UP; 279194f7e29aSAlan Cox } 279294f7e29aSAlan Cox 279394f7e29aSAlan Cox vm_map_unlock(map); 279494f7e29aSAlan Cox return (rv); 279594f7e29aSAlan Cox } 279694f7e29aSAlan Cox 279794f7e29aSAlan Cox /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 279894f7e29aSAlan Cox * desired address is already mapped, or if we successfully grow 279994f7e29aSAlan Cox * the stack. Also returns KERN_SUCCESS if addr is outside the 280094f7e29aSAlan Cox * stack range (this is strange, but preserves compatibility with 280194f7e29aSAlan Cox * the grow function in vm_machdep.c). 280294f7e29aSAlan Cox */ 280394f7e29aSAlan Cox int 280494f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr) 280594f7e29aSAlan Cox { 2806b21a0008SMarcel Moolenaar vm_map_entry_t next_entry, prev_entry; 2807b21a0008SMarcel Moolenaar vm_map_entry_t new_entry, stack_entry; 280894f7e29aSAlan Cox struct vmspace *vm = p->p_vmspace; 280994f7e29aSAlan Cox vm_map_t map = &vm->vm_map; 281094f7e29aSAlan Cox vm_offset_t end; 2811b21a0008SMarcel Moolenaar size_t grow_amount, max_grow; 281291d5354aSJohn Baldwin rlim_t stacklim, vmemlim; 2813b21a0008SMarcel Moolenaar int is_procstack, rv; 281423955314SAlfred Perlstein 281594f7e29aSAlan Cox Retry: 281691d5354aSJohn Baldwin PROC_LOCK(p); 281791d5354aSJohn Baldwin stacklim = lim_cur(p, RLIMIT_STACK); 2818bfee999dSAlan Cox vmemlim = lim_cur(p, RLIMIT_VMEM); 281991d5354aSJohn Baldwin PROC_UNLOCK(p); 282091d5354aSJohn Baldwin 282194f7e29aSAlan Cox vm_map_lock_read(map); 282294f7e29aSAlan Cox 282394f7e29aSAlan Cox /* If addr is already in the entry range, no need to grow.*/ 282494f7e29aSAlan Cox if (vm_map_lookup_entry(map, addr, &prev_entry)) { 282594f7e29aSAlan Cox vm_map_unlock_read(map); 28260cddd8f0SMatthew Dillon return (KERN_SUCCESS); 282794f7e29aSAlan Cox } 282894f7e29aSAlan Cox 2829b21a0008SMarcel Moolenaar next_entry = prev_entry->next; 2830b21a0008SMarcel Moolenaar if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) { 2831b21a0008SMarcel Moolenaar /* 2832b21a0008SMarcel Moolenaar * This entry does not grow upwards. Since the address lies 2833b21a0008SMarcel Moolenaar * beyond this entry, the next entry (if one exists) has to 2834b21a0008SMarcel Moolenaar * be a downward growable entry. The entry list header is 2835b21a0008SMarcel Moolenaar * never a growable entry, so it suffices to check the flags. 283694f7e29aSAlan Cox */ 2837b21a0008SMarcel Moolenaar if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) { 283894f7e29aSAlan Cox vm_map_unlock_read(map); 28390cddd8f0SMatthew Dillon return (KERN_SUCCESS); 284094f7e29aSAlan Cox } 2841b21a0008SMarcel Moolenaar stack_entry = next_entry; 2842b21a0008SMarcel Moolenaar } else { 2843b21a0008SMarcel Moolenaar /* 2844b21a0008SMarcel Moolenaar * This entry grows upward. If the next entry does not at 2845b21a0008SMarcel Moolenaar * least grow downwards, this is the entry we need to grow. 2846b21a0008SMarcel Moolenaar * otherwise we have two possible choices and we have to 2847b21a0008SMarcel Moolenaar * select one. 2848b21a0008SMarcel Moolenaar */ 2849b21a0008SMarcel Moolenaar if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) { 2850b21a0008SMarcel Moolenaar /* 2851b21a0008SMarcel Moolenaar * We have two choices; grow the entry closest to 2852b21a0008SMarcel Moolenaar * the address to minimize the amount of growth. 2853b21a0008SMarcel Moolenaar */ 2854b21a0008SMarcel Moolenaar if (addr - prev_entry->end <= next_entry->start - addr) 2855b21a0008SMarcel Moolenaar stack_entry = prev_entry; 2856b21a0008SMarcel Moolenaar else 2857b21a0008SMarcel Moolenaar stack_entry = next_entry; 2858b21a0008SMarcel Moolenaar } else 2859b21a0008SMarcel Moolenaar stack_entry = prev_entry; 2860b21a0008SMarcel Moolenaar } 286194f7e29aSAlan Cox 2862b21a0008SMarcel Moolenaar if (stack_entry == next_entry) { 2863b21a0008SMarcel Moolenaar KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo")); 2864b21a0008SMarcel Moolenaar KASSERT(addr < stack_entry->start, ("foo")); 2865b21a0008SMarcel Moolenaar end = (prev_entry != &map->header) ? prev_entry->end : 2866b21a0008SMarcel Moolenaar stack_entry->start - stack_entry->avail_ssize; 286794f7e29aSAlan Cox grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE); 2868b21a0008SMarcel Moolenaar max_grow = stack_entry->start - end; 2869b21a0008SMarcel Moolenaar } else { 2870b21a0008SMarcel Moolenaar KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo")); 287108667f6dSMarcel Moolenaar KASSERT(addr >= stack_entry->end, ("foo")); 2872b21a0008SMarcel Moolenaar end = (next_entry != &map->header) ? next_entry->start : 2873b21a0008SMarcel Moolenaar stack_entry->end + stack_entry->avail_ssize; 2874fd75d710SMarcel Moolenaar grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE); 2875b21a0008SMarcel Moolenaar max_grow = end - stack_entry->end; 2876b21a0008SMarcel Moolenaar } 2877b21a0008SMarcel Moolenaar 287894f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 287994f7e29aSAlan Cox vm_map_unlock_read(map); 28800cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 288194f7e29aSAlan Cox } 288294f7e29aSAlan Cox 2883b21a0008SMarcel Moolenaar /* 2884b21a0008SMarcel Moolenaar * If there is no longer enough space between the entries nogo, and 2885b21a0008SMarcel Moolenaar * adjust the available space. Note: this should only happen if the 2886b21a0008SMarcel Moolenaar * user has mapped into the stack area after the stack was created, 2887b21a0008SMarcel Moolenaar * and is probably an error. 288894f7e29aSAlan Cox * 2889b21a0008SMarcel Moolenaar * This also effectively destroys any guard page the user might have 2890b21a0008SMarcel Moolenaar * intended by limiting the stack size. 289194f7e29aSAlan Cox */ 2892b21a0008SMarcel Moolenaar if (grow_amount > max_grow) { 289325adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 289494f7e29aSAlan Cox goto Retry; 289594f7e29aSAlan Cox 2896b21a0008SMarcel Moolenaar stack_entry->avail_ssize = max_grow; 289794f7e29aSAlan Cox 289894f7e29aSAlan Cox vm_map_unlock(map); 28990cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 290094f7e29aSAlan Cox } 290194f7e29aSAlan Cox 2902b21a0008SMarcel Moolenaar is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0; 290394f7e29aSAlan Cox 2904b21a0008SMarcel Moolenaar /* 2905b21a0008SMarcel Moolenaar * If this is the main process stack, see if we're over the stack 2906b21a0008SMarcel Moolenaar * limit. 290794f7e29aSAlan Cox */ 290891d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 290994f7e29aSAlan Cox vm_map_unlock_read(map); 29100cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 291194f7e29aSAlan Cox } 291294f7e29aSAlan Cox 291394f7e29aSAlan Cox /* Round up the grow amount modulo SGROWSIZ */ 2914cbc89bfbSPaul Saab grow_amount = roundup (grow_amount, sgrowsiz); 2915b21a0008SMarcel Moolenaar if (grow_amount > stack_entry->avail_ssize) 291694f7e29aSAlan Cox grow_amount = stack_entry->avail_ssize; 291791d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 291891d5354aSJohn Baldwin grow_amount = stacklim - ctob(vm->vm_ssize); 291994f7e29aSAlan Cox } 292094f7e29aSAlan Cox 2921a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 292291d5354aSJohn Baldwin if (map->size + grow_amount > vmemlim) { 2923a69ac174SMatthew Dillon vm_map_unlock_read(map); 2924a69ac174SMatthew Dillon return (KERN_NO_SPACE); 2925a69ac174SMatthew Dillon } 2926a69ac174SMatthew Dillon 292725adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 292894f7e29aSAlan Cox goto Retry; 292994f7e29aSAlan Cox 2930b21a0008SMarcel Moolenaar if (stack_entry == next_entry) { 2931b21a0008SMarcel Moolenaar /* 2932b21a0008SMarcel Moolenaar * Growing downward. 2933b21a0008SMarcel Moolenaar */ 293494f7e29aSAlan Cox /* Get the preliminary new entry start value */ 293594f7e29aSAlan Cox addr = stack_entry->start - grow_amount; 293694f7e29aSAlan Cox 2937b21a0008SMarcel Moolenaar /* 2938b21a0008SMarcel Moolenaar * If this puts us into the previous entry, cut back our 2939b21a0008SMarcel Moolenaar * growth to the available space. Also, see the note above. 294094f7e29aSAlan Cox */ 294194f7e29aSAlan Cox if (addr < end) { 2942b21a0008SMarcel Moolenaar stack_entry->avail_ssize = max_grow; 294394f7e29aSAlan Cox addr = end; 294494f7e29aSAlan Cox } 294594f7e29aSAlan Cox 294694f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 294705ba50f5SJake Burkholder p->p_sysent->sv_stackprot, VM_PROT_ALL, 0); 294894f7e29aSAlan Cox 294994f7e29aSAlan Cox /* Adjust the available stack space by the amount we grew. */ 295094f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 295129b45e9eSAlan Cox if (prev_entry != &map->header) 295229b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addr); 2953b21a0008SMarcel Moolenaar new_entry = prev_entry->next; 2954b21a0008SMarcel Moolenaar KASSERT(new_entry == stack_entry->prev, ("foo")); 2955b21a0008SMarcel Moolenaar KASSERT(new_entry->end == stack_entry->start, ("foo")); 2956b21a0008SMarcel Moolenaar KASSERT(new_entry->start == addr, ("foo")); 2957b21a0008SMarcel Moolenaar grow_amount = new_entry->end - new_entry->start; 2958b21a0008SMarcel Moolenaar new_entry->avail_ssize = stack_entry->avail_ssize - 2959b21a0008SMarcel Moolenaar grow_amount; 2960b21a0008SMarcel Moolenaar stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN; 2961b21a0008SMarcel Moolenaar new_entry->eflags |= MAP_ENTRY_GROWS_DOWN; 296294f7e29aSAlan Cox } 2963b21a0008SMarcel Moolenaar } else { 2964b21a0008SMarcel Moolenaar /* 2965b21a0008SMarcel Moolenaar * Growing upward. 2966b21a0008SMarcel Moolenaar */ 2967b21a0008SMarcel Moolenaar addr = stack_entry->end + grow_amount; 2968b21a0008SMarcel Moolenaar 2969b21a0008SMarcel Moolenaar /* 2970b21a0008SMarcel Moolenaar * If this puts us into the next entry, cut back our growth 2971b21a0008SMarcel Moolenaar * to the available space. Also, see the note above. 2972b21a0008SMarcel Moolenaar */ 2973b21a0008SMarcel Moolenaar if (addr > end) { 2974b21a0008SMarcel Moolenaar stack_entry->avail_ssize = end - stack_entry->end; 2975b21a0008SMarcel Moolenaar addr = end; 297694f7e29aSAlan Cox } 297794f7e29aSAlan Cox 2978b21a0008SMarcel Moolenaar grow_amount = addr - stack_entry->end; 2979b21a0008SMarcel Moolenaar 2980b21a0008SMarcel Moolenaar /* Grow the underlying object if applicable. */ 2981b21a0008SMarcel Moolenaar if (stack_entry->object.vm_object == NULL || 2982b21a0008SMarcel Moolenaar vm_object_coalesce(stack_entry->object.vm_object, 298357a21abaSAlan Cox stack_entry->offset, 2984b21a0008SMarcel Moolenaar (vm_size_t)(stack_entry->end - stack_entry->start), 2985b21a0008SMarcel Moolenaar (vm_size_t)grow_amount)) { 298608667f6dSMarcel Moolenaar map->size += (addr - stack_entry->end); 2987b21a0008SMarcel Moolenaar /* Update the current entry. */ 2988b21a0008SMarcel Moolenaar stack_entry->end = addr; 2989199c91abSMarcel Moolenaar stack_entry->avail_ssize -= grow_amount; 29900164e057SAlan Cox vm_map_entry_resize_free(map, stack_entry); 2991b21a0008SMarcel Moolenaar rv = KERN_SUCCESS; 2992b21a0008SMarcel Moolenaar 2993b21a0008SMarcel Moolenaar if (next_entry != &map->header) 2994b21a0008SMarcel Moolenaar vm_map_clip_start(map, next_entry, addr); 2995b21a0008SMarcel Moolenaar } else 2996b21a0008SMarcel Moolenaar rv = KERN_FAILURE; 2997b21a0008SMarcel Moolenaar } 2998b21a0008SMarcel Moolenaar 2999b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && is_procstack) 3000b21a0008SMarcel Moolenaar vm->vm_ssize += btoc(grow_amount); 3001b21a0008SMarcel Moolenaar 300294f7e29aSAlan Cox vm_map_unlock(map); 3003b21a0008SMarcel Moolenaar 3004abd498aaSBruce M Simpson /* 3005abd498aaSBruce M Simpson * Heed the MAP_WIREFUTURE flag if it was set for this process. 3006abd498aaSBruce M Simpson */ 3007b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) { 3008b21a0008SMarcel Moolenaar vm_map_wire(map, 3009b21a0008SMarcel Moolenaar (stack_entry == next_entry) ? addr : addr - grow_amount, 3010b21a0008SMarcel Moolenaar (stack_entry == next_entry) ? stack_entry->start : addr, 3011b21a0008SMarcel Moolenaar (p->p_flag & P_SYSTEM) 3012b21a0008SMarcel Moolenaar ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES 3013b21a0008SMarcel Moolenaar : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES); 3014b21a0008SMarcel Moolenaar } 3015abd498aaSBruce M Simpson 30160cddd8f0SMatthew Dillon return (rv); 301794f7e29aSAlan Cox } 301894f7e29aSAlan Cox 3019df8bae1dSRodney W. Grimes /* 30205856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 30215856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 30225856e12eSJohn Dyson */ 302389b57fcfSKonstantin Belousov int 30243ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 30251b40f8c0SMatthew Dillon { 30265856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 30275856e12eSJohn Dyson struct vmspace *newvmspace; 30285856e12eSJohn Dyson 30293ebc1248SPeter Wemm newvmspace = vmspace_alloc(minuser, maxuser); 303089b57fcfSKonstantin Belousov if (newvmspace == NULL) 303189b57fcfSKonstantin Belousov return (ENOMEM); 303251ab6c28SAlan Cox newvmspace->vm_swrss = oldvmspace->vm_swrss; 30335856e12eSJohn Dyson /* 30345856e12eSJohn Dyson * This code is written like this for prototype purposes. The 30355856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 30365856e12eSJohn Dyson * other process's that are still using the vmspace to finally 30375856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 30385856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 30395856e12eSJohn Dyson */ 304057051fdcSTor Egge PROC_VMSPACE_LOCK(p); 30415856e12eSJohn Dyson p->p_vmspace = newvmspace; 304257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 30436617724cSJeff Roberson if (p == curthread->td_proc) 3044b40ce416SJulian Elischer pmap_activate(curthread); 3045b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 304689b57fcfSKonstantin Belousov return (0); 30475856e12eSJohn Dyson } 30485856e12eSJohn Dyson 30495856e12eSJohn Dyson /* 30505856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 30515856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 30525856e12eSJohn Dyson */ 305389b57fcfSKonstantin Belousov int 30541b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p) 30551b40f8c0SMatthew Dillon { 30565856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 30575856e12eSJohn Dyson struct vmspace *newvmspace; 30585856e12eSJohn Dyson 30595856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 306089b57fcfSKonstantin Belousov return (0); 30615856e12eSJohn Dyson newvmspace = vmspace_fork(oldvmspace); 306289b57fcfSKonstantin Belousov if (newvmspace == NULL) 306389b57fcfSKonstantin Belousov return (ENOMEM); 306457051fdcSTor Egge PROC_VMSPACE_LOCK(p); 30655856e12eSJohn Dyson p->p_vmspace = newvmspace; 306657051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 30676617724cSJeff Roberson if (p == curthread->td_proc) 3068b40ce416SJulian Elischer pmap_activate(curthread); 3069b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 307089b57fcfSKonstantin Belousov return (0); 30715856e12eSJohn Dyson } 30725856e12eSJohn Dyson 30735856e12eSJohn Dyson /* 3074df8bae1dSRodney W. Grimes * vm_map_lookup: 3075df8bae1dSRodney W. Grimes * 3076df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 3077df8bae1dSRodney W. Grimes * protection for a given virtual address in the 3078df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 3079df8bae1dSRodney W. Grimes * type specified. 3080df8bae1dSRodney W. Grimes * 3081df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 3082df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 3083df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 3084df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 3085df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 3086df8bae1dSRodney W. Grimes * 3087df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 3088df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 3089df8bae1dSRodney W. Grimes * 3090df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 3091df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 3092df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 3093df8bae1dSRodney W. Grimes * remain the same. 3094df8bae1dSRodney W. Grimes */ 3095df8bae1dSRodney W. Grimes int 3096b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3097b9dcd593SBruce Evans vm_offset_t vaddr, 309847221757SJohn Dyson vm_prot_t fault_typea, 3099b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 3100b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 3101b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 3102b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 31032d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 3104df8bae1dSRodney W. Grimes { 3105c0877f10SJohn Dyson vm_map_entry_t entry; 3106c0877f10SJohn Dyson vm_map_t map = *var_map; 3107c0877f10SJohn Dyson vm_prot_t prot; 310847221757SJohn Dyson vm_prot_t fault_type = fault_typea; 3109df8bae1dSRodney W. Grimes 3110df8bae1dSRodney W. Grimes RetryLookup:; 3111df8bae1dSRodney W. Grimes /* 3112df8bae1dSRodney W. Grimes * Lookup the faulting address. 3113df8bae1dSRodney W. Grimes */ 3114df8bae1dSRodney W. Grimes 3115df8bae1dSRodney W. Grimes vm_map_lock_read(map); 3116df8bae1dSRodney W. Grimes #define RETURN(why) \ 3117df8bae1dSRodney W. Grimes { \ 3118df8bae1dSRodney W. Grimes vm_map_unlock_read(map); \ 3119df8bae1dSRodney W. Grimes return (why); \ 3120df8bae1dSRodney W. Grimes } 3121df8bae1dSRodney W. Grimes 3122df8bae1dSRodney W. Grimes /* 31230d94caffSDavid Greenman * If the map has an interesting hint, try it before calling full 31240d94caffSDavid Greenman * blown lookup routine. 3125df8bae1dSRodney W. Grimes */ 31264e94f402SAlan Cox entry = map->root; 3127df8bae1dSRodney W. Grimes *out_entry = entry; 31284e94f402SAlan Cox if (entry == NULL || 3129df8bae1dSRodney W. Grimes (vaddr < entry->start) || (vaddr >= entry->end)) { 3130df8bae1dSRodney W. Grimes /* 31310d94caffSDavid Greenman * Entry was either not a valid hint, or the vaddr was not 31320d94caffSDavid Greenman * contained in the entry, so do a full lookup. 3133df8bae1dSRodney W. Grimes */ 31344e94f402SAlan Cox if (!vm_map_lookup_entry(map, vaddr, out_entry)) 3135df8bae1dSRodney W. Grimes RETURN(KERN_INVALID_ADDRESS); 3136df8bae1dSRodney W. Grimes 31374e94f402SAlan Cox entry = *out_entry; 3138df8bae1dSRodney W. Grimes } 3139b7b2aac2SJohn Dyson 3140df8bae1dSRodney W. Grimes /* 3141df8bae1dSRodney W. Grimes * Handle submaps. 3142df8bae1dSRodney W. Grimes */ 3143afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3144df8bae1dSRodney W. Grimes vm_map_t old_map = map; 3145df8bae1dSRodney W. Grimes 3146df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 3147df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 3148df8bae1dSRodney W. Grimes goto RetryLookup; 3149df8bae1dSRodney W. Grimes } 3150a04c970aSJohn Dyson 3151df8bae1dSRodney W. Grimes /* 31520d94caffSDavid Greenman * Check whether this task is allowed to have this page. 3153a04c970aSJohn Dyson * Note the special case for MAP_ENTRY_COW 3154a04c970aSJohn Dyson * pages with an override. This is to implement a forced 3155a04c970aSJohn Dyson * COW for debuggers. 3156df8bae1dSRodney W. Grimes */ 3157480ba2f5SJohn Dyson if (fault_type & VM_PROT_OVERRIDE_WRITE) 3158480ba2f5SJohn Dyson prot = entry->max_protection; 3159480ba2f5SJohn Dyson else 3160df8bae1dSRodney W. Grimes prot = entry->protection; 316147221757SJohn Dyson fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 316247221757SJohn Dyson if ((fault_type & prot) != fault_type) { 316347221757SJohn Dyson RETURN(KERN_PROTECTION_FAILURE); 316447221757SJohn Dyson } 31652ed14a92SAlan Cox if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 316647221757SJohn Dyson (entry->eflags & MAP_ENTRY_COW) && 31672ed14a92SAlan Cox (fault_type & VM_PROT_WRITE) && 316847221757SJohn Dyson (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 3169df8bae1dSRodney W. Grimes RETURN(KERN_PROTECTION_FAILURE); 3170a04c970aSJohn Dyson } 3171df8bae1dSRodney W. Grimes 3172df8bae1dSRodney W. Grimes /* 31730d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 31740d94caffSDavid Greenman * accesses. 3175df8bae1dSRodney W. Grimes */ 317605f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 317705f0fdd2SPoul-Henning Kamp if (*wired) 3178df8bae1dSRodney W. Grimes prot = fault_type = entry->protection; 3179df8bae1dSRodney W. Grimes 3180df8bae1dSRodney W. Grimes /* 3181df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 3182df8bae1dSRodney W. Grimes */ 3183afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 3184df8bae1dSRodney W. Grimes /* 31850d94caffSDavid Greenman * If we want to write the page, we may as well handle that 3186ad5fca3bSAlan Cox * now since we've got the map locked. 3187df8bae1dSRodney W. Grimes * 31880d94caffSDavid Greenman * If we don't need to write the page, we just demote the 31890d94caffSDavid Greenman * permissions allowed. 3190df8bae1dSRodney W. Grimes */ 3191df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 3192df8bae1dSRodney W. Grimes /* 31930d94caffSDavid Greenman * Make a new object, and place it in the object 31940d94caffSDavid Greenman * chain. Note that no new references have appeared 3195ad5fca3bSAlan Cox * -- one just moved from the map to the new 31960d94caffSDavid Greenman * object. 3197df8bae1dSRodney W. Grimes */ 319825adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 3199df8bae1dSRodney W. Grimes goto RetryLookup; 32009917e010SAlan Cox 3201df8bae1dSRodney W. Grimes vm_object_shadow( 3202df8bae1dSRodney W. Grimes &entry->object.vm_object, 3203df8bae1dSRodney W. Grimes &entry->offset, 3204c2e11a03SJohn Dyson atop(entry->end - entry->start)); 3205afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 32069917e010SAlan Cox 32079b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 32080d94caffSDavid Greenman } else { 3209df8bae1dSRodney W. Grimes /* 32100d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 32110d94caffSDavid Greenman * don't allow writes. 3212df8bae1dSRodney W. Grimes */ 32132d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 3214df8bae1dSRodney W. Grimes } 3215df8bae1dSRodney W. Grimes } 32162d8acc0fSJohn Dyson 3217df8bae1dSRodney W. Grimes /* 3218df8bae1dSRodney W. Grimes * Create an object if necessary. 3219df8bae1dSRodney W. Grimes */ 32204e71e795SMatthew Dillon if (entry->object.vm_object == NULL && 32214e71e795SMatthew Dillon !map->system_map) { 322225adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 3223df8bae1dSRodney W. Grimes goto RetryLookup; 322424a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 3225c2e11a03SJohn Dyson atop(entry->end - entry->start)); 3226df8bae1dSRodney W. Grimes entry->offset = 0; 32279b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 3228df8bae1dSRodney W. Grimes } 3229b5b40fa6SJohn Dyson 3230df8bae1dSRodney W. Grimes /* 32310d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 32320d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 3233df8bae1dSRodney W. Grimes */ 32349b09b6c7SMatthew Dillon *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 3235df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 3236df8bae1dSRodney W. Grimes 3237df8bae1dSRodney W. Grimes *out_prot = prot; 3238df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 3239df8bae1dSRodney W. Grimes 3240df8bae1dSRodney W. Grimes #undef RETURN 3241df8bae1dSRodney W. Grimes } 3242df8bae1dSRodney W. Grimes 3243df8bae1dSRodney W. Grimes /* 324419dc5607STor Egge * vm_map_lookup_locked: 324519dc5607STor Egge * 324619dc5607STor Egge * Lookup the faulting address. A version of vm_map_lookup that returns 324719dc5607STor Egge * KERN_FAILURE instead of blocking on map lock or memory allocation. 324819dc5607STor Egge */ 324919dc5607STor Egge int 325019dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 325119dc5607STor Egge vm_offset_t vaddr, 325219dc5607STor Egge vm_prot_t fault_typea, 325319dc5607STor Egge vm_map_entry_t *out_entry, /* OUT */ 325419dc5607STor Egge vm_object_t *object, /* OUT */ 325519dc5607STor Egge vm_pindex_t *pindex, /* OUT */ 325619dc5607STor Egge vm_prot_t *out_prot, /* OUT */ 325719dc5607STor Egge boolean_t *wired) /* OUT */ 325819dc5607STor Egge { 325919dc5607STor Egge vm_map_entry_t entry; 326019dc5607STor Egge vm_map_t map = *var_map; 326119dc5607STor Egge vm_prot_t prot; 326219dc5607STor Egge vm_prot_t fault_type = fault_typea; 326319dc5607STor Egge 326419dc5607STor Egge /* 326519dc5607STor Egge * If the map has an interesting hint, try it before calling full 326619dc5607STor Egge * blown lookup routine. 326719dc5607STor Egge */ 326819dc5607STor Egge entry = map->root; 326919dc5607STor Egge *out_entry = entry; 327019dc5607STor Egge if (entry == NULL || 327119dc5607STor Egge (vaddr < entry->start) || (vaddr >= entry->end)) { 327219dc5607STor Egge /* 327319dc5607STor Egge * Entry was either not a valid hint, or the vaddr was not 327419dc5607STor Egge * contained in the entry, so do a full lookup. 327519dc5607STor Egge */ 327619dc5607STor Egge if (!vm_map_lookup_entry(map, vaddr, out_entry)) 327719dc5607STor Egge return (KERN_INVALID_ADDRESS); 327819dc5607STor Egge 327919dc5607STor Egge entry = *out_entry; 328019dc5607STor Egge } 328119dc5607STor Egge 328219dc5607STor Egge /* 328319dc5607STor Egge * Fail if the entry refers to a submap. 328419dc5607STor Egge */ 328519dc5607STor Egge if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 328619dc5607STor Egge return (KERN_FAILURE); 328719dc5607STor Egge 328819dc5607STor Egge /* 328919dc5607STor Egge * Check whether this task is allowed to have this page. 329019dc5607STor Egge * Note the special case for MAP_ENTRY_COW 329119dc5607STor Egge * pages with an override. This is to implement a forced 329219dc5607STor Egge * COW for debuggers. 329319dc5607STor Egge */ 329419dc5607STor Egge if (fault_type & VM_PROT_OVERRIDE_WRITE) 329519dc5607STor Egge prot = entry->max_protection; 329619dc5607STor Egge else 329719dc5607STor Egge prot = entry->protection; 329819dc5607STor Egge fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 329919dc5607STor Egge if ((fault_type & prot) != fault_type) 330019dc5607STor Egge return (KERN_PROTECTION_FAILURE); 330119dc5607STor Egge if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 330219dc5607STor Egge (entry->eflags & MAP_ENTRY_COW) && 330319dc5607STor Egge (fault_type & VM_PROT_WRITE) && 330419dc5607STor Egge (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) 330519dc5607STor Egge return (KERN_PROTECTION_FAILURE); 330619dc5607STor Egge 330719dc5607STor Egge /* 330819dc5607STor Egge * If this page is not pageable, we have to get it for all possible 330919dc5607STor Egge * accesses. 331019dc5607STor Egge */ 331119dc5607STor Egge *wired = (entry->wired_count != 0); 331219dc5607STor Egge if (*wired) 331319dc5607STor Egge prot = fault_type = entry->protection; 331419dc5607STor Egge 331519dc5607STor Egge if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 331619dc5607STor Egge /* 331719dc5607STor Egge * Fail if the entry was copy-on-write for a write fault. 331819dc5607STor Egge */ 331919dc5607STor Egge if (fault_type & VM_PROT_WRITE) 332019dc5607STor Egge return (KERN_FAILURE); 332119dc5607STor Egge /* 332219dc5607STor Egge * We're attempting to read a copy-on-write page -- 332319dc5607STor Egge * don't allow writes. 332419dc5607STor Egge */ 332519dc5607STor Egge prot &= ~VM_PROT_WRITE; 332619dc5607STor Egge } 332719dc5607STor Egge 332819dc5607STor Egge /* 332919dc5607STor Egge * Fail if an object should be created. 333019dc5607STor Egge */ 333119dc5607STor Egge if (entry->object.vm_object == NULL && !map->system_map) 333219dc5607STor Egge return (KERN_FAILURE); 333319dc5607STor Egge 333419dc5607STor Egge /* 333519dc5607STor Egge * Return the object/offset from this entry. If the entry was 333619dc5607STor Egge * copy-on-write or empty, it has been fixed up. 333719dc5607STor Egge */ 333819dc5607STor Egge *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 333919dc5607STor Egge *object = entry->object.vm_object; 334019dc5607STor Egge 334119dc5607STor Egge *out_prot = prot; 334219dc5607STor Egge return (KERN_SUCCESS); 334319dc5607STor Egge } 334419dc5607STor Egge 334519dc5607STor Egge /* 3346df8bae1dSRodney W. Grimes * vm_map_lookup_done: 3347df8bae1dSRodney W. Grimes * 3348df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 3349df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 3350df8bae1dSRodney W. Grimes */ 33510d94caffSDavid Greenman void 33521b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 3353df8bae1dSRodney W. Grimes { 3354df8bae1dSRodney W. Grimes /* 3355df8bae1dSRodney W. Grimes * Unlock the main-level map 3356df8bae1dSRodney W. Grimes */ 3357df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3358df8bae1dSRodney W. Grimes } 3359df8bae1dSRodney W. Grimes 3360c7c34a24SBruce Evans #include "opt_ddb.h" 3361c3cb3e12SDavid Greenman #ifdef DDB 3362c7c34a24SBruce Evans #include <sys/kernel.h> 3363c7c34a24SBruce Evans 3364c7c34a24SBruce Evans #include <ddb/ddb.h> 3365c7c34a24SBruce Evans 3366df8bae1dSRodney W. Grimes /* 3367df8bae1dSRodney W. Grimes * vm_map_print: [ debug ] 3368df8bae1dSRodney W. Grimes */ 3369c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print) 3370df8bae1dSRodney W. Grimes { 337195e5e988SJohn Dyson static int nlines; 3372c7c34a24SBruce Evans /* XXX convert args. */ 3373c0877f10SJohn Dyson vm_map_t map = (vm_map_t)addr; 3374c7c34a24SBruce Evans boolean_t full = have_addr; 3375df8bae1dSRodney W. Grimes 3376c0877f10SJohn Dyson vm_map_entry_t entry; 3377c7c34a24SBruce Evans 3378e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3379e5f251d2SAlan Cox (void *)map, 3380101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 338195e5e988SJohn Dyson nlines++; 3382df8bae1dSRodney W. Grimes 3383c7c34a24SBruce Evans if (!full && db_indent) 3384df8bae1dSRodney W. Grimes return; 3385df8bae1dSRodney W. Grimes 3386c7c34a24SBruce Evans db_indent += 2; 3387df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 3388df8bae1dSRodney W. Grimes entry = entry->next) { 3389fc62ef1fSBruce Evans db_iprintf("map entry %p: start=%p, end=%p\n", 3390fc62ef1fSBruce Evans (void *)entry, (void *)entry->start, (void *)entry->end); 339195e5e988SJohn Dyson nlines++; 3392e5f251d2SAlan Cox { 3393df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 3394df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 33950d94caffSDavid Greenman 339695e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 3397df8bae1dSRodney W. Grimes entry->protection, 3398df8bae1dSRodney W. Grimes entry->max_protection, 33998aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 3400df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 340195e5e988SJohn Dyson db_printf(", wired"); 3402df8bae1dSRodney W. Grimes } 34039fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3404cd034a5bSMaxime Henrion db_printf(", share=%p, offset=0x%jx\n", 34059fdfe602SMatthew Dillon (void *)entry->object.sub_map, 3406cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 340795e5e988SJohn Dyson nlines++; 3408df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 34099fdfe602SMatthew Dillon (entry->prev->object.sub_map != 34109fdfe602SMatthew Dillon entry->object.sub_map)) { 3411c7c34a24SBruce Evans db_indent += 2; 3412101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t) 34139fdfe602SMatthew Dillon entry->object.sub_map, 3414914181e7SBruce Evans full, 0, (char *)0); 3415c7c34a24SBruce Evans db_indent -= 2; 3416df8bae1dSRodney W. Grimes } 34170d94caffSDavid Greenman } else { 3418cd034a5bSMaxime Henrion db_printf(", object=%p, offset=0x%jx", 3419101eeb7fSBruce Evans (void *)entry->object.vm_object, 3420cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 3421afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 3422c7c34a24SBruce Evans db_printf(", copy (%s)", 3423afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3424c7c34a24SBruce Evans db_printf("\n"); 342595e5e988SJohn Dyson nlines++; 3426df8bae1dSRodney W. Grimes 3427df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 3428df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 3429df8bae1dSRodney W. Grimes entry->object.vm_object)) { 3430c7c34a24SBruce Evans db_indent += 2; 3431101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 3432101eeb7fSBruce Evans entry->object.vm_object, 3433914181e7SBruce Evans full, 0, (char *)0); 343495e5e988SJohn Dyson nlines += 4; 3435c7c34a24SBruce Evans db_indent -= 2; 3436df8bae1dSRodney W. Grimes } 3437df8bae1dSRodney W. Grimes } 3438df8bae1dSRodney W. Grimes } 3439c7c34a24SBruce Evans db_indent -= 2; 344095e5e988SJohn Dyson if (db_indent == 0) 344195e5e988SJohn Dyson nlines = 0; 3442df8bae1dSRodney W. Grimes } 344395e5e988SJohn Dyson 344495e5e988SJohn Dyson 344595e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 344695e5e988SJohn Dyson { 344795e5e988SJohn Dyson struct proc *p; 344895e5e988SJohn Dyson 344995e5e988SJohn Dyson if (have_addr) { 345095e5e988SJohn Dyson p = (struct proc *) addr; 345195e5e988SJohn Dyson } else { 345295e5e988SJohn Dyson p = curproc; 345395e5e988SJohn Dyson } 345495e5e988SJohn Dyson 3455ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3456ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3457b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 345895e5e988SJohn Dyson 3459101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 346095e5e988SJohn Dyson } 346195e5e988SJohn Dyson 3462c7c34a24SBruce Evans #endif /* DDB */ 3463