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. 16fbbd9655SWarner Losh * 3. 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> 709a6d144fSKonstantin Belousov #include <sys/kernel.h> 7161d80e90SJohn Baldwin #include <sys/ktr.h> 72fb919e4dSMark Murray #include <sys/lock.h> 73fb919e4dSMark Murray #include <sys/mutex.h> 74b5e8ce9fSBruce Evans #include <sys/proc.h> 75efeaf95aSDavid Greenman #include <sys/vmmeter.h> 76867a482dSJohn Dyson #include <sys/mman.h> 771efb74fbSJohn Dyson #include <sys/vnode.h> 781ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 792267af78SJulian Elischer #include <sys/resourcevar.h> 8089f6b863SAttilio Rao #include <sys/rwlock.h> 813fde38dfSMike Silbersack #include <sys/file.h> 829a6d144fSKonstantin Belousov #include <sys/sysctl.h> 8305ba50f5SJake Burkholder #include <sys/sysent.h> 843db161e0SMatthew Dillon #include <sys/shm.h> 85df8bae1dSRodney W. Grimes 86df8bae1dSRodney W. Grimes #include <vm/vm.h> 87efeaf95aSDavid Greenman #include <vm/vm_param.h> 88efeaf95aSDavid Greenman #include <vm/pmap.h> 89efeaf95aSDavid Greenman #include <vm/vm_map.h> 90df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 91df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 9247221757SJohn Dyson #include <vm/vm_pager.h> 9326f9a767SRodney W. Grimes #include <vm/vm_kern.h> 94efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9584110e7eSKonstantin Belousov #include <vm/vnode_pager.h> 9621cd6e62SSeigo Tanimura #include <vm/swap_pager.h> 97670d17b5SJeff Roberson #include <vm/uma.h> 98df8bae1dSRodney W. Grimes 99df8bae1dSRodney W. Grimes /* 100df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 101df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 102df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 103df8bae1dSRodney W. Grimes * memory from one map to another. 104df8bae1dSRodney W. Grimes * 105df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 106df8bae1dSRodney W. Grimes * 107df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 108e2abaaaaSAlan Cox * entries; a self-adjusting binary search tree of these 109e2abaaaaSAlan Cox * entries is used to speed up lookups. 110df8bae1dSRodney W. Grimes * 111956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 112df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 113df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 114df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 115df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 116df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 117df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 118df8bae1dSRodney W. Grimes * 119df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 120ad5fca3bSAlan Cox * by copying VM object references from one map to 121df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 122df8bae1dSRodney W. Grimes */ 123df8bae1dSRodney W. Grimes 1243a92e5d5SAlan Cox static struct mtx map_sleep_mtx; 1258355f576SJeff Roberson static uma_zone_t mapentzone; 1268355f576SJeff Roberson static uma_zone_t kmapentzone; 1278355f576SJeff Roberson static uma_zone_t mapzone; 1288355f576SJeff Roberson static uma_zone_t vmspace_zone; 129b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags); 130b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags); 13192351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, 13292351f16SAlan Cox vm_offset_t max); 1330b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map); 134655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry); 13503462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry); 13619bd0d9cSKonstantin Belousov static int vm_map_growstack(vm_map_t map, vm_offset_t addr, 13719bd0d9cSKonstantin Belousov vm_map_entry_t gap_entry); 138077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 139077ec27cSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags); 1408355f576SJeff Roberson #ifdef INVARIANTS 1418355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg); 1428355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg); 1438355f576SJeff Roberson #endif 1444648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, 1454648ba0aSKonstantin Belousov vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max, 1464648ba0aSKonstantin Belousov int cow); 14766cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry, 14866cd575bSAlan Cox vm_offset_t failed_addr); 149b18bfc3dSJohn Dyson 150ef694c1aSEdward Tomasz Napierala #define ENTRY_CHARGED(e) ((e)->cred != NULL || \ 151ef694c1aSEdward Tomasz Napierala ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \ 1523364c323SKonstantin Belousov !((e)->eflags & MAP_ENTRY_NEEDS_COPY))) 1533364c323SKonstantin Belousov 15457051fdcSTor Egge /* 15557051fdcSTor Egge * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type 15657051fdcSTor Egge * stable. 15757051fdcSTor Egge */ 15857051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0) 15957051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0) 16057051fdcSTor Egge 161d239bd3cSKonstantin Belousov /* 162d239bd3cSKonstantin Belousov * VM_MAP_RANGE_CHECK: [ internal use only ] 163d239bd3cSKonstantin Belousov * 164d239bd3cSKonstantin Belousov * Asserts that the starting and ending region 165d239bd3cSKonstantin Belousov * addresses fall within the valid range of the map. 166d239bd3cSKonstantin Belousov */ 167d239bd3cSKonstantin Belousov #define VM_MAP_RANGE_CHECK(map, start, end) \ 168d239bd3cSKonstantin Belousov { \ 169d239bd3cSKonstantin Belousov if (start < vm_map_min(map)) \ 170d239bd3cSKonstantin Belousov start = vm_map_min(map); \ 171d239bd3cSKonstantin Belousov if (end > vm_map_max(map)) \ 172d239bd3cSKonstantin Belousov end = vm_map_max(map); \ 173d239bd3cSKonstantin Belousov if (start > end) \ 174d239bd3cSKonstantin Belousov start = end; \ 175d239bd3cSKonstantin Belousov } 176d239bd3cSKonstantin Belousov 1776fecb26bSKonstantin Belousov /* 1786fecb26bSKonstantin Belousov * vm_map_startup: 1796fecb26bSKonstantin Belousov * 1806fecb26bSKonstantin Belousov * Initialize the vm_map module. Must be called before 1816fecb26bSKonstantin Belousov * any other vm_map routines. 1826fecb26bSKonstantin Belousov * 1836fecb26bSKonstantin Belousov * Map and entry structures are allocated from the general 1846fecb26bSKonstantin Belousov * purpose memory pool with some exceptions: 1856fecb26bSKonstantin Belousov * 1866fecb26bSKonstantin Belousov * - The kernel map and kmem submap are allocated statically. 1876fecb26bSKonstantin Belousov * - Kernel map entries are allocated out of a static pool. 1886fecb26bSKonstantin Belousov * 1896fecb26bSKonstantin Belousov * These restrictions are necessary since malloc() uses the 1906fecb26bSKonstantin Belousov * maps and requires map entries. 1916fecb26bSKonstantin Belousov */ 1926fecb26bSKonstantin Belousov 1930d94caffSDavid Greenman void 1941b40f8c0SMatthew Dillon vm_map_startup(void) 195df8bae1dSRodney W. Grimes { 1963a92e5d5SAlan Cox mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF); 1978355f576SJeff Roberson mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL, 1988355f576SJeff Roberson #ifdef INVARIANTS 1998355f576SJeff Roberson vm_map_zdtor, 2008355f576SJeff Roberson #else 2018355f576SJeff Roberson NULL, 2028355f576SJeff Roberson #endif 203f872f6eaSAlan Cox vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 2048355f576SJeff Roberson uma_prealloc(mapzone, MAX_KMAP); 205670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry), 20618aa2de5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 20718aa2de5SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZONE_VM); 208670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry), 209670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2105df87b21SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL, 2115df87b21SJeff Roberson #ifdef INVARIANTS 2125df87b21SJeff Roberson vmspace_zdtor, 2135df87b21SJeff Roberson #else 2145df87b21SJeff Roberson NULL, 2155df87b21SJeff Roberson #endif 216f872f6eaSAlan Cox vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 2178355f576SJeff Roberson } 2188355f576SJeff Roberson 219b23f72e9SBrian Feldman static int 220b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags) 2218355f576SJeff Roberson { 2228355f576SJeff Roberson struct vmspace *vm; 2238355f576SJeff Roberson 2248355f576SJeff Roberson vm = (struct vmspace *)mem; 2258355f576SJeff Roberson 22689b57fcfSKonstantin Belousov vm->vm_map.pmap = NULL; 227b23f72e9SBrian Feldman (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags); 228e68c64f0SKonstantin Belousov PMAP_LOCK_INIT(vmspace_pmap(vm)); 229b23f72e9SBrian Feldman return (0); 2308355f576SJeff Roberson } 2318355f576SJeff Roberson 232b23f72e9SBrian Feldman static int 233b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags) 2348355f576SJeff Roberson { 2358355f576SJeff Roberson vm_map_t map; 2368355f576SJeff Roberson 2378355f576SJeff Roberson map = (vm_map_t)mem; 238763d9566STim Kientzle memset(map, 0, sizeof(*map)); 239e30df26eSAlan Cox mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK); 240e30df26eSAlan Cox sx_init(&map->lock, "vm map (user)"); 241b23f72e9SBrian Feldman return (0); 2428355f576SJeff Roberson } 2438355f576SJeff Roberson 2448355f576SJeff Roberson #ifdef INVARIANTS 2458355f576SJeff Roberson static void 2468355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg) 2478355f576SJeff Roberson { 2488355f576SJeff Roberson struct vmspace *vm; 2498355f576SJeff Roberson 2508355f576SJeff Roberson vm = (struct vmspace *)mem; 2518355f576SJeff Roberson 2528355f576SJeff Roberson vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg); 2538355f576SJeff Roberson } 2548355f576SJeff Roberson static void 2558355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg) 2568355f576SJeff Roberson { 2578355f576SJeff Roberson vm_map_t map; 2588355f576SJeff Roberson 2598355f576SJeff Roberson map = (vm_map_t)mem; 2608355f576SJeff Roberson KASSERT(map->nentries == 0, 2618355f576SJeff Roberson ("map %p nentries == %d on free.", 2628355f576SJeff Roberson map, map->nentries)); 2638355f576SJeff Roberson KASSERT(map->size == 0, 2648355f576SJeff Roberson ("map %p size == %lu on free.", 2659eb6e519SJeff Roberson map, (unsigned long)map->size)); 2668355f576SJeff Roberson } 2678355f576SJeff Roberson #endif /* INVARIANTS */ 2688355f576SJeff Roberson 269df8bae1dSRodney W. Grimes /* 270df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 271df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 27274d1d2b7SNeel Natu * 27374d1d2b7SNeel Natu * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit(). 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes struct vmspace * 27674d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit) 277df8bae1dSRodney W. Grimes { 278c0877f10SJohn Dyson struct vmspace *vm; 2790d94caffSDavid Greenman 280a163d034SWarner Losh vm = uma_zalloc(vmspace_zone, M_WAITOK); 28174d1d2b7SNeel Natu 28274d1d2b7SNeel Natu KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL")); 28374d1d2b7SNeel Natu 28474d1d2b7SNeel Natu if (pinit == NULL) 28574d1d2b7SNeel Natu pinit = &pmap_pinit; 28674d1d2b7SNeel Natu 28774d1d2b7SNeel Natu if (!pinit(vmspace_pmap(vm))) { 28889b57fcfSKonstantin Belousov uma_zfree(vmspace_zone, vm); 28989b57fcfSKonstantin Belousov return (NULL); 29089b57fcfSKonstantin Belousov } 29121c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_alloc: %p", vm); 29292351f16SAlan Cox _vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max); 293df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 2942d8acc0fSJohn Dyson vm->vm_shm = NULL; 29551ab6c28SAlan Cox vm->vm_swrss = 0; 29651ab6c28SAlan Cox vm->vm_tsize = 0; 29751ab6c28SAlan Cox vm->vm_dsize = 0; 29851ab6c28SAlan Cox vm->vm_ssize = 0; 29951ab6c28SAlan Cox vm->vm_taddr = 0; 30051ab6c28SAlan Cox vm->vm_daddr = 0; 30151ab6c28SAlan Cox vm->vm_maxsaddr = 0; 302df8bae1dSRodney W. Grimes return (vm); 303df8bae1dSRodney W. Grimes } 304df8bae1dSRodney W. Grimes 3054b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 3061ba5ad42SEdward Tomasz Napierala static void 3071ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p) 3081ba5ad42SEdward Tomasz Napierala { 3091ba5ad42SEdward Tomasz Napierala 3101ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 3111ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_DATA, 0); 3121ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_STACK, 0); 3131ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_RSS, 0); 3141ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_MEMLOCK, 0); 3151ba5ad42SEdward Tomasz Napierala racct_set(p, RACCT_VMEM, 0); 3161ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 3171ba5ad42SEdward Tomasz Napierala } 3184b5c9cf6SEdward Tomasz Napierala #endif 3191ba5ad42SEdward Tomasz Napierala 32062a59e8fSWarner Losh static inline void 321582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm) 322df8bae1dSRodney W. Grimes { 3230ef12795SAlan Cox 32421c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_free: %p", vm); 3253db161e0SMatthew Dillon 3263db161e0SMatthew Dillon /* 3273db161e0SMatthew Dillon * Make sure any SysV shm is freed, it might not have been in 3283db161e0SMatthew Dillon * exit1(). 3293db161e0SMatthew Dillon */ 3303db161e0SMatthew Dillon shmexit(vm); 3313db161e0SMatthew Dillon 33230dcfc09SJohn Dyson /* 333df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 3340d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 3350d94caffSDavid Greenman * the pmap module to reclaim anything left. 336df8bae1dSRodney W. Grimes */ 337717f7d59SAlan Cox (void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset, 338df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 3398355f576SJeff Roberson 3400ef12795SAlan Cox pmap_release(vmspace_pmap(vm)); 3410ef12795SAlan Cox vm->vm_map.pmap = NULL; 3428355f576SJeff Roberson uma_zfree(vmspace_zone, vm); 343df8bae1dSRodney W. Grimes } 344582ec34cSAlfred Perlstein 345582ec34cSAlfred Perlstein void 346582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm) 347582ec34cSAlfred Perlstein { 348582ec34cSAlfred Perlstein 349423521aaSRyan Stone WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL, 350164a37a5SJohn Baldwin "vmspace_free() called"); 351423521aaSRyan Stone 352582ec34cSAlfred Perlstein if (vm->vm_refcnt == 0) 353582ec34cSAlfred Perlstein panic("vmspace_free: attempt to free already freed vmspace"); 354582ec34cSAlfred Perlstein 3551a587ef2SJohn Baldwin if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1) 356582ec34cSAlfred Perlstein vmspace_dofree(vm); 357582ec34cSAlfred Perlstein } 358582ec34cSAlfred Perlstein 359582ec34cSAlfred Perlstein void 360582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p) 361582ec34cSAlfred Perlstein { 362334f7061SPeter Wemm struct vmspace *vm; 363582ec34cSAlfred Perlstein 36457051fdcSTor Egge PROC_VMSPACE_LOCK(p); 365334f7061SPeter Wemm vm = p->p_vmspace; 366334f7061SPeter Wemm p->p_vmspace = NULL; 36757051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 36857051fdcSTor Egge KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace")); 36957051fdcSTor Egge vmspace_free(vm); 37057051fdcSTor Egge } 37157051fdcSTor Egge 37257051fdcSTor Egge void 37357051fdcSTor Egge vmspace_exit(struct thread *td) 37457051fdcSTor Egge { 37557051fdcSTor Egge int refcnt; 37657051fdcSTor Egge struct vmspace *vm; 37757051fdcSTor Egge struct proc *p; 378389d2b6eSMatthew Dillon 379389d2b6eSMatthew Dillon /* 38057051fdcSTor Egge * Release user portion of address space. 38157051fdcSTor Egge * This releases references to vnodes, 38257051fdcSTor Egge * which could cause I/O if the file has been unlinked. 38357051fdcSTor Egge * Need to do this early enough that we can still sleep. 384389d2b6eSMatthew Dillon * 38557051fdcSTor Egge * The last exiting process to reach this point releases as 38657051fdcSTor Egge * much of the environment as it can. vmspace_dofree() is the 38757051fdcSTor Egge * slower fallback in case another process had a temporary 38857051fdcSTor Egge * reference to the vmspace. 389389d2b6eSMatthew Dillon */ 39057051fdcSTor Egge 39157051fdcSTor Egge p = td->td_proc; 39257051fdcSTor Egge vm = p->p_vmspace; 39357051fdcSTor Egge atomic_add_int(&vmspace0.vm_refcnt, 1); 39457051fdcSTor Egge do { 39557051fdcSTor Egge refcnt = vm->vm_refcnt; 39657051fdcSTor Egge if (refcnt > 1 && p->p_vmspace != &vmspace0) { 39757051fdcSTor Egge /* Switch now since other proc might free vmspace */ 39857051fdcSTor Egge PROC_VMSPACE_LOCK(p); 39957051fdcSTor Egge p->p_vmspace = &vmspace0; 40057051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 40157051fdcSTor Egge pmap_activate(td); 40257051fdcSTor Egge } 40357051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1)); 40457051fdcSTor Egge if (refcnt == 1) { 40557051fdcSTor Egge if (p->p_vmspace != vm) { 40657051fdcSTor Egge /* vmspace not yet freed, switch back */ 40757051fdcSTor Egge PROC_VMSPACE_LOCK(p); 40857051fdcSTor Egge p->p_vmspace = vm; 40957051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 41057051fdcSTor Egge pmap_activate(td); 41157051fdcSTor Egge } 41257051fdcSTor Egge pmap_remove_pages(vmspace_pmap(vm)); 41357051fdcSTor Egge /* Switch now since this proc will free vmspace */ 41457051fdcSTor Egge PROC_VMSPACE_LOCK(p); 41557051fdcSTor Egge p->p_vmspace = &vmspace0; 41657051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 41757051fdcSTor Egge pmap_activate(td); 418334f7061SPeter Wemm vmspace_dofree(vm); 419334f7061SPeter Wemm } 4204b5c9cf6SEdward Tomasz Napierala #ifdef RACCT 4214b5c9cf6SEdward Tomasz Napierala if (racct_enable) 4221ba5ad42SEdward Tomasz Napierala vmspace_container_reset(p); 4234b5c9cf6SEdward Tomasz Napierala #endif 42457051fdcSTor Egge } 42557051fdcSTor Egge 42657051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */ 42757051fdcSTor Egge 42857051fdcSTor Egge struct vmspace * 42957051fdcSTor Egge vmspace_acquire_ref(struct proc *p) 43057051fdcSTor Egge { 43157051fdcSTor Egge struct vmspace *vm; 43257051fdcSTor Egge int refcnt; 43357051fdcSTor Egge 43457051fdcSTor Egge PROC_VMSPACE_LOCK(p); 43557051fdcSTor Egge vm = p->p_vmspace; 43657051fdcSTor Egge if (vm == NULL) { 43757051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 43857051fdcSTor Egge return (NULL); 43957051fdcSTor Egge } 44057051fdcSTor Egge do { 44157051fdcSTor Egge refcnt = vm->vm_refcnt; 44257051fdcSTor Egge if (refcnt <= 0) { /* Avoid 0->1 transition */ 44357051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 44457051fdcSTor Egge return (NULL); 44557051fdcSTor Egge } 44657051fdcSTor Egge } while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1)); 44757051fdcSTor Egge if (vm != p->p_vmspace) { 44857051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 44957051fdcSTor Egge vmspace_free(vm); 45057051fdcSTor Egge return (NULL); 45157051fdcSTor Egge } 45257051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 45357051fdcSTor Egge return (vm); 45457051fdcSTor Egge } 455df8bae1dSRodney W. Grimes 4568a4dc40fSJohn Baldwin /* 4578a4dc40fSJohn Baldwin * Switch between vmspaces in an AIO kernel process. 4588a4dc40fSJohn Baldwin * 4598a4dc40fSJohn Baldwin * The AIO kernel processes switch to and from a user process's 4608a4dc40fSJohn Baldwin * vmspace while performing an I/O operation on behalf of a user 4618a4dc40fSJohn Baldwin * process. The new vmspace is either the vmspace of a user process 4628a4dc40fSJohn Baldwin * obtained from an active AIO request or the initial vmspace of the 4638a4dc40fSJohn Baldwin * AIO kernel process (when it is idling). Because user processes 4648a4dc40fSJohn Baldwin * will block to drain any active AIO requests before proceeding in 4658a4dc40fSJohn Baldwin * exit() or execve(), the vmspace reference count for these vmspaces 4668a4dc40fSJohn Baldwin * can never be 0. This allows for a much simpler implementation than 4678a4dc40fSJohn Baldwin * the loop in vmspace_acquire_ref() above. Similarly, AIO kernel 4688a4dc40fSJohn Baldwin * processes hold an extra reference on their initial vmspace for the 4698a4dc40fSJohn Baldwin * life of the process so that this guarantee is true for any vmspace 4708a4dc40fSJohn Baldwin * passed as 'newvm'. 4718a4dc40fSJohn Baldwin */ 4728a4dc40fSJohn Baldwin void 4738a4dc40fSJohn Baldwin vmspace_switch_aio(struct vmspace *newvm) 4748a4dc40fSJohn Baldwin { 4758a4dc40fSJohn Baldwin struct vmspace *oldvm; 4768a4dc40fSJohn Baldwin 4778a4dc40fSJohn Baldwin /* XXX: Need some way to assert that this is an aio daemon. */ 4788a4dc40fSJohn Baldwin 4798a4dc40fSJohn Baldwin KASSERT(newvm->vm_refcnt > 0, 4808a4dc40fSJohn Baldwin ("vmspace_switch_aio: newvm unreferenced")); 4818a4dc40fSJohn Baldwin 4828a4dc40fSJohn Baldwin oldvm = curproc->p_vmspace; 4838a4dc40fSJohn Baldwin if (oldvm == newvm) 4848a4dc40fSJohn Baldwin return; 4858a4dc40fSJohn Baldwin 4868a4dc40fSJohn Baldwin /* 4878a4dc40fSJohn Baldwin * Point to the new address space and refer to it. 4888a4dc40fSJohn Baldwin */ 4898a4dc40fSJohn Baldwin curproc->p_vmspace = newvm; 4908a4dc40fSJohn Baldwin atomic_add_int(&newvm->vm_refcnt, 1); 4918a4dc40fSJohn Baldwin 4928a4dc40fSJohn Baldwin /* Activate the new mapping. */ 4938a4dc40fSJohn Baldwin pmap_activate(curthread); 4948a4dc40fSJohn Baldwin 4958a4dc40fSJohn Baldwin /* Remove the daemon's reference to the old address space. */ 4968a4dc40fSJohn Baldwin KASSERT(oldvm->vm_refcnt > 1, 4978a4dc40fSJohn Baldwin ("vmspace_switch_aio: oldvm dropping last reference")); 4988a4dc40fSJohn Baldwin vmspace_free(oldvm); 4998a4dc40fSJohn Baldwin } 5008a4dc40fSJohn Baldwin 5011b40f8c0SMatthew Dillon void 502780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line) 5031b40f8c0SMatthew Dillon { 504bc91c510SAlan Cox 50593bc4879SAlan Cox if (map->system_map) 506ccdf2333SAttilio Rao mtx_lock_flags_(&map->system_mtx, 0, file, line); 50712c64974SMaxime Henrion else 5089fde98bbSAttilio Rao sx_xlock_(&map->lock, file, line); 5091b40f8c0SMatthew Dillon map->timestamp++; 5101b40f8c0SMatthew Dillon } 5111b40f8c0SMatthew Dillon 5120b367bd8SKonstantin Belousov static void 5130b367bd8SKonstantin Belousov vm_map_process_deferred(void) 5140e0af8ecSBrian Feldman { 5150b367bd8SKonstantin Belousov struct thread *td; 5166fbe60faSJohn Baldwin vm_map_entry_t entry, next; 51784110e7eSKonstantin Belousov vm_object_t object; 518655c3490SKonstantin Belousov 5190b367bd8SKonstantin Belousov td = curthread; 5206fbe60faSJohn Baldwin entry = td->td_map_def_user; 5216fbe60faSJohn Baldwin td->td_map_def_user = NULL; 5226fbe60faSJohn Baldwin while (entry != NULL) { 5236fbe60faSJohn Baldwin next = entry->next; 52484110e7eSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) { 52584110e7eSKonstantin Belousov /* 52684110e7eSKonstantin Belousov * Decrement the object's writemappings and 52784110e7eSKonstantin Belousov * possibly the vnode's v_writecount. 52884110e7eSKonstantin Belousov */ 52984110e7eSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0, 53084110e7eSKonstantin Belousov ("Submap with writecount")); 53184110e7eSKonstantin Belousov object = entry->object.vm_object; 53284110e7eSKonstantin Belousov KASSERT(object != NULL, ("No object for writecount")); 53384110e7eSKonstantin Belousov vnode_pager_release_writecount(object, entry->start, 53484110e7eSKonstantin Belousov entry->end); 53584110e7eSKonstantin Belousov } 5360b367bd8SKonstantin Belousov vm_map_entry_deallocate(entry, FALSE); 5376fbe60faSJohn Baldwin entry = next; 5380b367bd8SKonstantin Belousov } 5390b367bd8SKonstantin Belousov } 5400b367bd8SKonstantin Belousov 5410b367bd8SKonstantin Belousov void 5420b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line) 5430b367bd8SKonstantin Belousov { 5440b367bd8SKonstantin Belousov 5450b367bd8SKonstantin Belousov if (map->system_map) 546ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line); 5470b367bd8SKonstantin Belousov else { 5489fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line); 5490b367bd8SKonstantin Belousov vm_map_process_deferred(); 550655c3490SKonstantin Belousov } 5510e0af8ecSBrian Feldman } 5520e0af8ecSBrian Feldman 5530e0af8ecSBrian Feldman void 554780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line) 5550e0af8ecSBrian Feldman { 556bc91c510SAlan Cox 55793bc4879SAlan Cox if (map->system_map) 558ccdf2333SAttilio Rao mtx_lock_flags_(&map->system_mtx, 0, file, line); 55912c64974SMaxime Henrion else 5609fde98bbSAttilio Rao sx_slock_(&map->lock, file, line); 56136daaecdSAlan Cox } 5620e0af8ecSBrian Feldman 5630e0af8ecSBrian Feldman void 564780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line) 5650e0af8ecSBrian Feldman { 566bc91c510SAlan Cox 56736daaecdSAlan Cox if (map->system_map) 568ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line); 5690b367bd8SKonstantin Belousov else { 5709fde98bbSAttilio Rao sx_sunlock_(&map->lock, file, line); 5710b367bd8SKonstantin Belousov vm_map_process_deferred(); 5720b367bd8SKonstantin Belousov } 57325adb370SBrian Feldman } 57425adb370SBrian Feldman 575d974f03cSAlan Cox int 576780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line) 577d974f03cSAlan Cox { 57825adb370SBrian Feldman int error; 57925adb370SBrian Feldman 58036daaecdSAlan Cox error = map->system_map ? 581ccdf2333SAttilio Rao !mtx_trylock_flags_(&map->system_mtx, 0, file, line) : 5829fde98bbSAttilio Rao !sx_try_xlock_(&map->lock, file, line); 5833a92e5d5SAlan Cox if (error == 0) 5843a92e5d5SAlan Cox map->timestamp++; 585bc91c510SAlan Cox return (error == 0); 5860e0af8ecSBrian Feldman } 5870e0af8ecSBrian Feldman 5880e0af8ecSBrian Feldman int 58972d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line) 59072d97679SDavid Schultz { 59172d97679SDavid Schultz int error; 59272d97679SDavid Schultz 59372d97679SDavid Schultz error = map->system_map ? 594ccdf2333SAttilio Rao !mtx_trylock_flags_(&map->system_mtx, 0, file, line) : 5959fde98bbSAttilio Rao !sx_try_slock_(&map->lock, file, line); 59672d97679SDavid Schultz return (error == 0); 59772d97679SDavid Schultz } 59872d97679SDavid Schultz 59905a8c414SAlan Cox /* 60005a8c414SAlan Cox * _vm_map_lock_upgrade: [ internal use only ] 60105a8c414SAlan Cox * 60205a8c414SAlan Cox * Tries to upgrade a read (shared) lock on the specified map to a write 60305a8c414SAlan Cox * (exclusive) lock. Returns the value "0" if the upgrade succeeds and a 60405a8c414SAlan Cox * non-zero value if the upgrade fails. If the upgrade fails, the map is 60505a8c414SAlan Cox * returned without a read or write lock held. 60605a8c414SAlan Cox * 60705a8c414SAlan Cox * Requires that the map be read locked. 60805a8c414SAlan Cox */ 60972d97679SDavid Schultz int 610780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line) 6110e0af8ecSBrian Feldman { 61205a8c414SAlan Cox unsigned int last_timestamp; 613bc91c510SAlan Cox 61412c64974SMaxime Henrion if (map->system_map) { 615ccdf2333SAttilio Rao mtx_assert_(&map->system_mtx, MA_OWNED, file, line); 61605a8c414SAlan Cox } else { 6179fde98bbSAttilio Rao if (!sx_try_upgrade_(&map->lock, file, line)) { 61805a8c414SAlan Cox last_timestamp = map->timestamp; 6199fde98bbSAttilio Rao sx_sunlock_(&map->lock, file, line); 6200b367bd8SKonstantin Belousov vm_map_process_deferred(); 62105a8c414SAlan Cox /* 62205a8c414SAlan Cox * If the map's timestamp does not change while the 62305a8c414SAlan Cox * map is unlocked, then the upgrade succeeds. 62405a8c414SAlan Cox */ 6259fde98bbSAttilio Rao sx_xlock_(&map->lock, file, line); 62605a8c414SAlan Cox if (last_timestamp != map->timestamp) { 6279fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line); 62805a8c414SAlan Cox return (1); 62905a8c414SAlan Cox } 63005a8c414SAlan Cox } 63105a8c414SAlan Cox } 632bc91c510SAlan Cox map->timestamp++; 633bc91c510SAlan Cox return (0); 6340e0af8ecSBrian Feldman } 6350e0af8ecSBrian Feldman 6360e0af8ecSBrian Feldman void 637780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line) 6381b40f8c0SMatthew Dillon { 639bc91c510SAlan Cox 64012c64974SMaxime Henrion if (map->system_map) { 641ccdf2333SAttilio Rao mtx_assert_(&map->system_mtx, MA_OWNED, file, line); 64205a8c414SAlan Cox } else 6439fde98bbSAttilio Rao sx_downgrade_(&map->lock, file, line); 64405a8c414SAlan Cox } 64505a8c414SAlan Cox 64605a8c414SAlan Cox /* 64705a8c414SAlan Cox * vm_map_locked: 64805a8c414SAlan Cox * 64905a8c414SAlan Cox * Returns a non-zero value if the caller holds a write (exclusive) lock 65005a8c414SAlan Cox * on the specified map and the value "0" otherwise. 65105a8c414SAlan Cox */ 65205a8c414SAlan Cox int 65305a8c414SAlan Cox vm_map_locked(vm_map_t map) 65405a8c414SAlan Cox { 65505a8c414SAlan Cox 65605a8c414SAlan Cox if (map->system_map) 65705a8c414SAlan Cox return (mtx_owned(&map->system_mtx)); 65805a8c414SAlan Cox else 65905a8c414SAlan Cox return (sx_xlocked(&map->lock)); 66025adb370SBrian Feldman } 66125adb370SBrian Feldman 6623a0916b8SKonstantin Belousov #ifdef INVARIANTS 6633a0916b8SKonstantin Belousov static void 6643a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line) 6653a0916b8SKonstantin Belousov { 6663a0916b8SKonstantin Belousov 6673a0916b8SKonstantin Belousov if (map->system_map) 668ccdf2333SAttilio Rao mtx_assert_(&map->system_mtx, MA_OWNED, file, line); 6693a0916b8SKonstantin Belousov else 6709fde98bbSAttilio Rao sx_assert_(&map->lock, SA_XLOCKED, file, line); 6713a0916b8SKonstantin Belousov } 6723a0916b8SKonstantin Belousov 6733a0916b8SKonstantin Belousov #define VM_MAP_ASSERT_LOCKED(map) \ 6743a0916b8SKonstantin Belousov _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE) 6753a0916b8SKonstantin Belousov #else 6763a0916b8SKonstantin Belousov #define VM_MAP_ASSERT_LOCKED(map) 6773a0916b8SKonstantin Belousov #endif 6783a0916b8SKonstantin Belousov 679acd9a301SAlan Cox /* 6808304adaaSAlan Cox * _vm_map_unlock_and_wait: 6818304adaaSAlan Cox * 6828304adaaSAlan Cox * Atomically releases the lock on the specified map and puts the calling 6838304adaaSAlan Cox * thread to sleep. The calling thread will remain asleep until either 6848304adaaSAlan Cox * vm_map_wakeup() is performed on the map or the specified timeout is 6858304adaaSAlan Cox * exceeded. 6868304adaaSAlan Cox * 6878304adaaSAlan Cox * WARNING! This function does not perform deferred deallocations of 6888304adaaSAlan Cox * objects and map entries. Therefore, the calling thread is expected to 6898304adaaSAlan Cox * reacquire the map lock after reawakening and later perform an ordinary 6908304adaaSAlan Cox * unlock operation, such as vm_map_unlock(), before completing its 6918304adaaSAlan Cox * operation on the map. 692acd9a301SAlan Cox */ 6939688f931SAlan Cox int 6948304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line) 695acd9a301SAlan Cox { 696acd9a301SAlan Cox 6973a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 6988304adaaSAlan Cox if (map->system_map) 699ccdf2333SAttilio Rao mtx_unlock_flags_(&map->system_mtx, 0, file, line); 7008304adaaSAlan Cox else 7019fde98bbSAttilio Rao sx_xunlock_(&map->lock, file, line); 7028304adaaSAlan Cox return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 7038304adaaSAlan Cox timo)); 704acd9a301SAlan Cox } 705acd9a301SAlan Cox 706acd9a301SAlan Cox /* 707acd9a301SAlan Cox * vm_map_wakeup: 7088304adaaSAlan Cox * 7098304adaaSAlan Cox * Awaken any threads that have slept on the map using 7108304adaaSAlan Cox * vm_map_unlock_and_wait(). 711acd9a301SAlan Cox */ 7129688f931SAlan Cox void 713acd9a301SAlan Cox vm_map_wakeup(vm_map_t map) 714acd9a301SAlan Cox { 715acd9a301SAlan Cox 716b49ecb86SAlan Cox /* 7173a92e5d5SAlan Cox * Acquire and release map_sleep_mtx to prevent a wakeup() 7188304adaaSAlan Cox * from being performed (and lost) between the map unlock 7198304adaaSAlan Cox * and the msleep() in _vm_map_unlock_and_wait(). 720b49ecb86SAlan Cox */ 7213a92e5d5SAlan Cox mtx_lock(&map_sleep_mtx); 7223a92e5d5SAlan Cox mtx_unlock(&map_sleep_mtx); 723acd9a301SAlan Cox wakeup(&map->root); 724acd9a301SAlan Cox } 725acd9a301SAlan Cox 726a5db445dSMax Laier void 727a5db445dSMax Laier vm_map_busy(vm_map_t map) 728a5db445dSMax Laier { 729a5db445dSMax Laier 730a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map); 731a5db445dSMax Laier map->busy++; 732a5db445dSMax Laier } 733a5db445dSMax Laier 734a5db445dSMax Laier void 735a5db445dSMax Laier vm_map_unbusy(vm_map_t map) 736a5db445dSMax Laier { 737a5db445dSMax Laier 738a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map); 739a5db445dSMax Laier KASSERT(map->busy, ("vm_map_unbusy: not busy")); 740a5db445dSMax Laier if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) { 741a5db445dSMax Laier vm_map_modflags(map, 0, MAP_BUSY_WAKEUP); 742a5db445dSMax Laier wakeup(&map->busy); 743a5db445dSMax Laier } 744a5db445dSMax Laier } 745a5db445dSMax Laier 746a5db445dSMax Laier void 747a5db445dSMax Laier vm_map_wait_busy(vm_map_t map) 748a5db445dSMax Laier { 749a5db445dSMax Laier 750a5db445dSMax Laier VM_MAP_ASSERT_LOCKED(map); 751a5db445dSMax Laier while (map->busy) { 752a5db445dSMax Laier vm_map_modflags(map, MAP_BUSY_WAKEUP, 0); 753a5db445dSMax Laier if (map->system_map) 754a5db445dSMax Laier msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0); 755a5db445dSMax Laier else 756a5db445dSMax Laier sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0); 757a5db445dSMax Laier } 758a5db445dSMax Laier map->timestamp++; 759a5db445dSMax Laier } 760a5db445dSMax Laier 7611b40f8c0SMatthew Dillon long 7621b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace) 7631b40f8c0SMatthew Dillon { 7641b40f8c0SMatthew Dillon return pmap_resident_count(vmspace_pmap(vmspace)); 7651b40f8c0SMatthew Dillon } 7661b40f8c0SMatthew Dillon 767ff2b5645SMatthew Dillon /* 768df8bae1dSRodney W. Grimes * vm_map_create: 769df8bae1dSRodney W. Grimes * 770df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 771df8bae1dSRodney W. Grimes * the given physical map structure, and having 772df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 773df8bae1dSRodney W. Grimes */ 7740d94caffSDavid Greenman vm_map_t 7751b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max) 776df8bae1dSRodney W. Grimes { 777c0877f10SJohn Dyson vm_map_t result; 778df8bae1dSRodney W. Grimes 779a163d034SWarner Losh result = uma_zalloc(mapzone, M_WAITOK); 78021c641b2SJohn Baldwin CTR1(KTR_VM, "vm_map_create: %p", result); 78192351f16SAlan Cox _vm_map_init(result, pmap, min, max); 782df8bae1dSRodney W. Grimes return (result); 783df8bae1dSRodney W. Grimes } 784df8bae1dSRodney W. Grimes 785df8bae1dSRodney W. Grimes /* 786df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 787df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 788df8bae1dSRodney W. Grimes */ 7898355f576SJeff Roberson static void 79092351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max) 791df8bae1dSRodney W. Grimes { 79221c641b2SJohn Baldwin 793df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 7949688f931SAlan Cox map->needs_wakeup = FALSE; 7953075778bSJohn Dyson map->system_map = 0; 79692351f16SAlan Cox map->pmap = pmap; 797df8bae1dSRodney W. Grimes map->min_offset = min; 798df8bae1dSRodney W. Grimes map->max_offset = max; 799af7cd0c5SBrian Feldman map->flags = 0; 8004e94f402SAlan Cox map->root = NULL; 801df8bae1dSRodney W. Grimes map->timestamp = 0; 802a5db445dSMax Laier map->busy = 0; 803df8bae1dSRodney W. Grimes } 804df8bae1dSRodney W. Grimes 805a18b1f1dSJason Evans void 80692351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max) 807a18b1f1dSJason Evans { 80892351f16SAlan Cox 80992351f16SAlan Cox _vm_map_init(map, pmap, min, max); 810d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 81112c64974SMaxime Henrion sx_init(&map->lock, "user map"); 812a18b1f1dSJason Evans } 813a18b1f1dSJason Evans 814df8bae1dSRodney W. Grimes /* 815b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 816b18bfc3dSJohn Dyson * 817b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 818b18bfc3dSJohn Dyson */ 81962487bb4SJohn Dyson static void 8201b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry) 821b18bfc3dSJohn Dyson { 8222b4a2c27SAlan Cox uma_zfree(map->system_map ? kmapentzone : mapentzone, entry); 823b18bfc3dSJohn Dyson } 824b18bfc3dSJohn Dyson 825b18bfc3dSJohn Dyson /* 826df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 827df8bae1dSRodney W. Grimes * 828df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 829b28cb1caSAlfred Perlstein * No entry fields are filled in. 830df8bae1dSRodney W. Grimes */ 831f708ef1bSPoul-Henning Kamp static vm_map_entry_t 8321b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map) 833df8bae1dSRodney W. Grimes { 8341f6889a1SMatthew Dillon vm_map_entry_t new_entry; 8351f6889a1SMatthew Dillon 8362b4a2c27SAlan Cox if (map->system_map) 8372b4a2c27SAlan Cox new_entry = uma_zalloc(kmapentzone, M_NOWAIT); 8382b4a2c27SAlan Cox else 839a163d034SWarner Losh new_entry = uma_zalloc(mapentzone, M_WAITOK); 8401f6889a1SMatthew Dillon if (new_entry == NULL) 8411f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 8421f6889a1SMatthew Dillon return (new_entry); 843df8bae1dSRodney W. Grimes } 844df8bae1dSRodney W. Grimes 845df8bae1dSRodney W. Grimes /* 846794316a8SAlan Cox * vm_map_entry_set_behavior: 847794316a8SAlan Cox * 848794316a8SAlan Cox * Set the expected access behavior, either normal, random, or 849794316a8SAlan Cox * sequential. 850794316a8SAlan Cox */ 85162a59e8fSWarner Losh static inline void 852794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior) 853794316a8SAlan Cox { 854794316a8SAlan Cox entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) | 855794316a8SAlan Cox (behavior & MAP_ENTRY_BEHAV_MASK); 856794316a8SAlan Cox } 857794316a8SAlan Cox 858794316a8SAlan Cox /* 8590164e057SAlan Cox * vm_map_entry_set_max_free: 8600164e057SAlan Cox * 8610164e057SAlan Cox * Set the max_free field in a vm_map_entry. 8620164e057SAlan Cox */ 86362a59e8fSWarner Losh static inline void 8640164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry) 8650164e057SAlan Cox { 8660164e057SAlan Cox 8670164e057SAlan Cox entry->max_free = entry->adj_free; 8680164e057SAlan Cox if (entry->left != NULL && entry->left->max_free > entry->max_free) 8690164e057SAlan Cox entry->max_free = entry->left->max_free; 8700164e057SAlan Cox if (entry->right != NULL && entry->right->max_free > entry->max_free) 8710164e057SAlan Cox entry->max_free = entry->right->max_free; 8720164e057SAlan Cox } 8730164e057SAlan Cox 8740164e057SAlan Cox /* 8754e94f402SAlan Cox * vm_map_entry_splay: 8764e94f402SAlan Cox * 8770164e057SAlan Cox * The Sleator and Tarjan top-down splay algorithm with the 8780164e057SAlan Cox * following variation. Max_free must be computed bottom-up, so 8790164e057SAlan Cox * on the downward pass, maintain the left and right spines in 8800164e057SAlan Cox * reverse order. Then, make a second pass up each side to fix 8810164e057SAlan Cox * the pointers and compute max_free. The time bound is O(log n) 8820164e057SAlan Cox * amortized. 8830164e057SAlan Cox * 8840164e057SAlan Cox * The new root is the vm_map_entry containing "addr", or else an 8850164e057SAlan Cox * adjacent entry (lower or higher) if addr is not in the tree. 8860164e057SAlan Cox * 8870164e057SAlan Cox * The map must be locked, and leaves it so. 8880164e057SAlan Cox * 8890164e057SAlan Cox * Returns: the new root. 8904e94f402SAlan Cox */ 8914e94f402SAlan Cox static vm_map_entry_t 8920164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root) 8934e94f402SAlan Cox { 8940164e057SAlan Cox vm_map_entry_t llist, rlist; 8950164e057SAlan Cox vm_map_entry_t ltree, rtree; 8960164e057SAlan Cox vm_map_entry_t y; 8974e94f402SAlan Cox 8980164e057SAlan Cox /* Special case of empty tree. */ 8994e94f402SAlan Cox if (root == NULL) 9004e94f402SAlan Cox return (root); 9010164e057SAlan Cox 9020164e057SAlan Cox /* 9030164e057SAlan Cox * Pass One: Splay down the tree until we find addr or a NULL 9040164e057SAlan Cox * pointer where addr would go. llist and rlist are the two 9050164e057SAlan Cox * sides in reverse order (bottom-up), with llist linked by 9060164e057SAlan Cox * the right pointer and rlist linked by the left pointer in 9070164e057SAlan Cox * the vm_map_entry. Wait until Pass Two to set max_free on 9080164e057SAlan Cox * the two spines. 9090164e057SAlan Cox */ 9100164e057SAlan Cox llist = NULL; 9110164e057SAlan Cox rlist = NULL; 9120164e057SAlan Cox for (;;) { 9130164e057SAlan Cox /* root is never NULL in here. */ 9140164e057SAlan Cox if (addr < root->start) { 9150164e057SAlan Cox y = root->left; 9160164e057SAlan Cox if (y == NULL) 9174e94f402SAlan Cox break; 9180164e057SAlan Cox if (addr < y->start && y->left != NULL) { 9190164e057SAlan Cox /* Rotate right and put y on rlist. */ 9204e94f402SAlan Cox root->left = y->right; 9214e94f402SAlan Cox y->right = root; 9220164e057SAlan Cox vm_map_entry_set_max_free(root); 9230164e057SAlan Cox root = y->left; 9240164e057SAlan Cox y->left = rlist; 9250164e057SAlan Cox rlist = y; 9260164e057SAlan Cox } else { 9270164e057SAlan Cox /* Put root on rlist. */ 9280164e057SAlan Cox root->left = rlist; 9290164e057SAlan Cox rlist = root; 9304e94f402SAlan Cox root = y; 9314e94f402SAlan Cox } 9327438d60bSAlan Cox } else if (addr >= root->end) { 9330164e057SAlan Cox y = root->right; 9347438d60bSAlan Cox if (y == NULL) 9354e94f402SAlan Cox break; 9360164e057SAlan Cox if (addr >= y->end && y->right != NULL) { 9370164e057SAlan Cox /* Rotate left and put y on llist. */ 9384e94f402SAlan Cox root->right = y->left; 9394e94f402SAlan Cox y->left = root; 9400164e057SAlan Cox vm_map_entry_set_max_free(root); 9410164e057SAlan Cox root = y->right; 9420164e057SAlan Cox y->right = llist; 9430164e057SAlan Cox llist = y; 9440164e057SAlan Cox } else { 9450164e057SAlan Cox /* Put root on llist. */ 9460164e057SAlan Cox root->right = llist; 9470164e057SAlan Cox llist = root; 9484e94f402SAlan Cox root = y; 9494e94f402SAlan Cox } 9507438d60bSAlan Cox } else 9517438d60bSAlan Cox break; 9520164e057SAlan Cox } 9530164e057SAlan Cox 9540164e057SAlan Cox /* 9550164e057SAlan Cox * Pass Two: Walk back up the two spines, flip the pointers 9560164e057SAlan Cox * and set max_free. The subtrees of the root go at the 9570164e057SAlan Cox * bottom of llist and rlist. 9580164e057SAlan Cox */ 9590164e057SAlan Cox ltree = root->left; 9600164e057SAlan Cox while (llist != NULL) { 9610164e057SAlan Cox y = llist->right; 9620164e057SAlan Cox llist->right = ltree; 9630164e057SAlan Cox vm_map_entry_set_max_free(llist); 9640164e057SAlan Cox ltree = llist; 9650164e057SAlan Cox llist = y; 9660164e057SAlan Cox } 9670164e057SAlan Cox rtree = root->right; 9680164e057SAlan Cox while (rlist != NULL) { 9690164e057SAlan Cox y = rlist->left; 9700164e057SAlan Cox rlist->left = rtree; 9710164e057SAlan Cox vm_map_entry_set_max_free(rlist); 9720164e057SAlan Cox rtree = rlist; 9730164e057SAlan Cox rlist = y; 9740164e057SAlan Cox } 9750164e057SAlan Cox 9760164e057SAlan Cox /* 9770164e057SAlan Cox * Final assembly: add ltree and rtree as subtrees of root. 9780164e057SAlan Cox */ 9790164e057SAlan Cox root->left = ltree; 9800164e057SAlan Cox root->right = rtree; 9810164e057SAlan Cox vm_map_entry_set_max_free(root); 9820164e057SAlan Cox 9834e94f402SAlan Cox return (root); 9844e94f402SAlan Cox } 9854e94f402SAlan Cox 9864e94f402SAlan Cox /* 987df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 988df8bae1dSRodney W. Grimes * 989df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 990df8bae1dSRodney W. Grimes */ 9914e94f402SAlan Cox static void 99299c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 99399c81ca9SAlan Cox vm_map_entry_t after_where, 99499c81ca9SAlan Cox vm_map_entry_t entry) 99599c81ca9SAlan Cox { 99621c641b2SJohn Baldwin 99721c641b2SJohn Baldwin CTR4(KTR_VM, 99821c641b2SJohn Baldwin "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 99921c641b2SJohn Baldwin map->nentries, entry, after_where); 10003a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 10015831f5fcSKonstantin Belousov KASSERT(after_where == &map->header || 10025831f5fcSKonstantin Belousov after_where->end <= entry->start, 10035831f5fcSKonstantin Belousov ("vm_map_entry_link: prev end %jx new start %jx overlap", 10045831f5fcSKonstantin Belousov (uintmax_t)after_where->end, (uintmax_t)entry->start)); 10055831f5fcSKonstantin Belousov KASSERT(after_where->next == &map->header || 10065831f5fcSKonstantin Belousov entry->end <= after_where->next->start, 10075831f5fcSKonstantin Belousov ("vm_map_entry_link: new end %jx next start %jx overlap", 10085831f5fcSKonstantin Belousov (uintmax_t)entry->end, (uintmax_t)after_where->next->start)); 10095831f5fcSKonstantin Belousov 101099c81ca9SAlan Cox map->nentries++; 101199c81ca9SAlan Cox entry->prev = after_where; 101299c81ca9SAlan Cox entry->next = after_where->next; 101399c81ca9SAlan Cox entry->next->prev = entry; 101499c81ca9SAlan Cox after_where->next = entry; 10154e94f402SAlan Cox 10164e94f402SAlan Cox if (after_where != &map->header) { 10174e94f402SAlan Cox if (after_where != map->root) 10184e94f402SAlan Cox vm_map_entry_splay(after_where->start, map->root); 10194e94f402SAlan Cox entry->right = after_where->right; 10204e94f402SAlan Cox entry->left = after_where; 10214e94f402SAlan Cox after_where->right = NULL; 10220164e057SAlan Cox after_where->adj_free = entry->start - after_where->end; 10230164e057SAlan Cox vm_map_entry_set_max_free(after_where); 10244e94f402SAlan Cox } else { 10254e94f402SAlan Cox entry->right = map->root; 10264e94f402SAlan Cox entry->left = NULL; 10274e94f402SAlan Cox } 10280164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 10290164e057SAlan Cox entry->next->start) - entry->end; 10300164e057SAlan Cox vm_map_entry_set_max_free(entry); 10314e94f402SAlan Cox map->root = entry; 1032df8bae1dSRodney W. Grimes } 103399c81ca9SAlan Cox 10344e94f402SAlan Cox static void 103599c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 103699c81ca9SAlan Cox vm_map_entry_t entry) 103799c81ca9SAlan Cox { 10384e94f402SAlan Cox vm_map_entry_t next, prev, root; 103999c81ca9SAlan Cox 10403a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 10414e94f402SAlan Cox if (entry != map->root) 10424e94f402SAlan Cox vm_map_entry_splay(entry->start, map->root); 10434e94f402SAlan Cox if (entry->left == NULL) 10444e94f402SAlan Cox root = entry->right; 10454e94f402SAlan Cox else { 10464e94f402SAlan Cox root = vm_map_entry_splay(entry->start, entry->left); 10474e94f402SAlan Cox root->right = entry->right; 10480164e057SAlan Cox root->adj_free = (entry->next == &map->header ? map->max_offset : 10490164e057SAlan Cox entry->next->start) - root->end; 10500164e057SAlan Cox vm_map_entry_set_max_free(root); 10514e94f402SAlan Cox } 10524e94f402SAlan Cox map->root = root; 10534e94f402SAlan Cox 10544e94f402SAlan Cox prev = entry->prev; 10554e94f402SAlan Cox next = entry->next; 105699c81ca9SAlan Cox next->prev = prev; 105799c81ca9SAlan Cox prev->next = next; 105899c81ca9SAlan Cox map->nentries--; 105921c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 106021c641b2SJohn Baldwin map->nentries, entry); 1061df8bae1dSRodney W. Grimes } 1062df8bae1dSRodney W. Grimes 1063df8bae1dSRodney W. Grimes /* 10640164e057SAlan Cox * vm_map_entry_resize_free: 10650164e057SAlan Cox * 10660164e057SAlan Cox * Recompute the amount of free space following a vm_map_entry 10670164e057SAlan Cox * and propagate that value up the tree. Call this function after 10680164e057SAlan Cox * resizing a map entry in-place, that is, without a call to 10690164e057SAlan Cox * vm_map_entry_link() or _unlink(). 10700164e057SAlan Cox * 10710164e057SAlan Cox * The map must be locked, and leaves it so. 10720164e057SAlan Cox */ 10730164e057SAlan Cox static void 10740164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry) 10750164e057SAlan Cox { 10760164e057SAlan Cox 10770164e057SAlan Cox /* 10780164e057SAlan Cox * Using splay trees without parent pointers, propagating 10790164e057SAlan Cox * max_free up the tree is done by moving the entry to the 10800164e057SAlan Cox * root and making the change there. 10810164e057SAlan Cox */ 10820164e057SAlan Cox if (entry != map->root) 10830164e057SAlan Cox map->root = vm_map_entry_splay(entry->start, map->root); 10840164e057SAlan Cox 10850164e057SAlan Cox entry->adj_free = (entry->next == &map->header ? map->max_offset : 10860164e057SAlan Cox entry->next->start) - entry->end; 10870164e057SAlan Cox vm_map_entry_set_max_free(entry); 10880164e057SAlan Cox } 10890164e057SAlan Cox 10900164e057SAlan Cox /* 1091df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 1092df8bae1dSRodney W. Grimes * 1093df8bae1dSRodney W. Grimes * Finds the map entry containing (or 1094df8bae1dSRodney W. Grimes * immediately preceding) the specified address 1095df8bae1dSRodney W. Grimes * in the given map; the entry is returned 1096df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 1097df8bae1dSRodney W. Grimes * result indicates whether the address is 1098df8bae1dSRodney W. Grimes * actually contained in the map. 1099df8bae1dSRodney W. Grimes */ 11000d94caffSDavid Greenman boolean_t 11011b40f8c0SMatthew Dillon vm_map_lookup_entry( 11021b40f8c0SMatthew Dillon vm_map_t map, 11031b40f8c0SMatthew Dillon vm_offset_t address, 11041b40f8c0SMatthew Dillon vm_map_entry_t *entry) /* OUT */ 1105df8bae1dSRodney W. Grimes { 1106c0877f10SJohn Dyson vm_map_entry_t cur; 110705a8c414SAlan Cox boolean_t locked; 1108df8bae1dSRodney W. Grimes 11094c3ef59eSAlan Cox /* 11104c3ef59eSAlan Cox * If the map is empty, then the map entry immediately preceding 11114c3ef59eSAlan Cox * "address" is the map's header. 11124c3ef59eSAlan Cox */ 11134c3ef59eSAlan Cox cur = map->root; 11144e94f402SAlan Cox if (cur == NULL) 11154e94f402SAlan Cox *entry = &map->header; 11164c3ef59eSAlan Cox else if (address >= cur->start && cur->end > address) { 11174c3ef59eSAlan Cox *entry = cur; 11184c3ef59eSAlan Cox return (TRUE); 111905a8c414SAlan Cox } else if ((locked = vm_map_locked(map)) || 112005a8c414SAlan Cox sx_try_upgrade(&map->lock)) { 112105a8c414SAlan Cox /* 112205a8c414SAlan Cox * Splay requires a write lock on the map. However, it only 112305a8c414SAlan Cox * restructures the binary search tree; it does not otherwise 112405a8c414SAlan Cox * change the map. Thus, the map's timestamp need not change 112505a8c414SAlan Cox * on a temporary upgrade. 112605a8c414SAlan Cox */ 11274c3ef59eSAlan Cox map->root = cur = vm_map_entry_splay(address, cur); 112805a8c414SAlan Cox if (!locked) 112905a8c414SAlan Cox sx_downgrade(&map->lock); 1130df8bae1dSRodney W. Grimes 11314c3ef59eSAlan Cox /* 11324c3ef59eSAlan Cox * If "address" is contained within a map entry, the new root 11334c3ef59eSAlan Cox * is that map entry. Otherwise, the new root is a map entry 11344c3ef59eSAlan Cox * immediately before or after "address". 11354c3ef59eSAlan Cox */ 1136df8bae1dSRodney W. Grimes if (address >= cur->start) { 1137df8bae1dSRodney W. Grimes *entry = cur; 11384e94f402SAlan Cox if (cur->end > address) 1139df8bae1dSRodney W. Grimes return (TRUE); 11404e94f402SAlan Cox } else 1141df8bae1dSRodney W. Grimes *entry = cur->prev; 114205a8c414SAlan Cox } else 114305a8c414SAlan Cox /* 114405a8c414SAlan Cox * Since the map is only locked for read access, perform a 114505a8c414SAlan Cox * standard binary search tree lookup for "address". 114605a8c414SAlan Cox */ 114705a8c414SAlan Cox for (;;) { 114805a8c414SAlan Cox if (address < cur->start) { 114905a8c414SAlan Cox if (cur->left == NULL) { 115005a8c414SAlan Cox *entry = cur->prev; 115105a8c414SAlan Cox break; 115205a8c414SAlan Cox } 115305a8c414SAlan Cox cur = cur->left; 115405a8c414SAlan Cox } else if (cur->end > address) { 115505a8c414SAlan Cox *entry = cur; 115605a8c414SAlan Cox return (TRUE); 115705a8c414SAlan Cox } else { 115805a8c414SAlan Cox if (cur->right == NULL) { 115905a8c414SAlan Cox *entry = cur; 116005a8c414SAlan Cox break; 116105a8c414SAlan Cox } 116205a8c414SAlan Cox cur = cur->right; 116305a8c414SAlan Cox } 11644e94f402SAlan Cox } 1165df8bae1dSRodney W. Grimes return (FALSE); 1166df8bae1dSRodney W. Grimes } 1167df8bae1dSRodney W. Grimes 1168df8bae1dSRodney W. Grimes /* 116930dcfc09SJohn Dyson * vm_map_insert: 117030dcfc09SJohn Dyson * 117130dcfc09SJohn Dyson * Inserts the given whole VM object into the target 117230dcfc09SJohn Dyson * map at the specified address range. The object's 117330dcfc09SJohn Dyson * size should match that of the address range. 117430dcfc09SJohn Dyson * 117530dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 11762aaeadf8SMatthew Dillon * 11772aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 11782aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 117930dcfc09SJohn Dyson */ 118030dcfc09SJohn Dyson int 1181b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 118233314db0SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow) 118330dcfc09SJohn Dyson { 118433314db0SAlan Cox vm_map_entry_t new_entry, prev_entry, temp_entry; 1185ef694c1aSEdward Tomasz Napierala struct ucred *cred; 11861569205fSKonstantin Belousov vm_eflags_t protoeflags; 11878211bd45SKonstantin Belousov vm_inherit_t inheritance; 118830dcfc09SJohn Dyson 11893a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 119033314db0SAlan Cox KASSERT((object != kmem_object && object != kernel_object) || 119133314db0SAlan Cox (cow & MAP_COPY_ON_WRITE) == 0, 119233314db0SAlan Cox ("vm_map_insert: kmem or kernel object and COW")); 119333314db0SAlan Cox KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0, 119433314db0SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 119500de6773SKonstantin Belousov KASSERT((prot & ~max) == 0, 119600de6773SKonstantin Belousov ("prot %#x is not subset of max_prot %#x", prot, max)); 11973a0916b8SKonstantin Belousov 119830dcfc09SJohn Dyson /* 119930dcfc09SJohn Dyson * Check that the start and end points are not bogus. 120030dcfc09SJohn Dyson */ 12011569205fSKonstantin Belousov if (start < map->min_offset || end > map->max_offset || start >= end) 120230dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 120330dcfc09SJohn Dyson 120430dcfc09SJohn Dyson /* 120530dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 120630dcfc09SJohn Dyson * of an existing entry, this range is bogus. 120730dcfc09SJohn Dyson */ 120830dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 120930dcfc09SJohn Dyson return (KERN_NO_SPACE); 121030dcfc09SJohn Dyson 121130dcfc09SJohn Dyson prev_entry = temp_entry; 121230dcfc09SJohn Dyson 121330dcfc09SJohn Dyson /* 121430dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 121530dcfc09SJohn Dyson */ 12161569205fSKonstantin Belousov if (prev_entry->next != &map->header && prev_entry->next->start < end) 121730dcfc09SJohn Dyson return (KERN_NO_SPACE); 121830dcfc09SJohn Dyson 121919bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL || 122019bd0d9cSKonstantin Belousov max != VM_PROT_NONE)) 122119bd0d9cSKonstantin Belousov return (KERN_INVALID_ARGUMENT); 122219bd0d9cSKonstantin Belousov 1223afa07f7eSJohn Dyson protoeflags = 0; 1224afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 1225e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY; 122633314db0SAlan Cox if (cow & MAP_NOFAULT) 1227afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 12284f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 12294f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 12309730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 12319730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 1232712efe66SAlan Cox if (cow & MAP_STACK_GROWS_DOWN) 1233712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_DOWN; 1234712efe66SAlan Cox if (cow & MAP_STACK_GROWS_UP) 1235712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_UP; 123684110e7eSKonstantin Belousov if (cow & MAP_VN_WRITECOUNT) 123784110e7eSKonstantin Belousov protoeflags |= MAP_ENTRY_VN_WRITECNT; 123819bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0) 123919bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_GUARD; 124019bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_DN) != 0) 124119bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_DN; 124219bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_UP) != 0) 124319bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_UP; 12448211bd45SKonstantin Belousov if (cow & MAP_INHERIT_SHARE) 12458211bd45SKonstantin Belousov inheritance = VM_INHERIT_SHARE; 12468211bd45SKonstantin Belousov else 12478211bd45SKonstantin Belousov inheritance = VM_INHERIT_DEFAULT; 12484f79d873SMatthew Dillon 1249ef694c1aSEdward Tomasz Napierala cred = NULL; 125019bd0d9cSKonstantin Belousov if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0) 12513364c323SKonstantin Belousov goto charged; 12523364c323SKonstantin Belousov if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) && 12533364c323SKonstantin Belousov ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) { 12543364c323SKonstantin Belousov if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start)) 12553364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 12561569205fSKonstantin Belousov KASSERT(object == NULL || 12571569205fSKonstantin Belousov (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 || 1258ef694c1aSEdward Tomasz Napierala object->cred == NULL, 12591569205fSKonstantin Belousov ("overcommit: vm_map_insert o %p", object)); 1260ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 12613364c323SKonstantin Belousov } 12623364c323SKonstantin Belousov 12633364c323SKonstantin Belousov charged: 1264f8616ebfSAlan Cox /* Expand the kernel pmap, if necessary. */ 1265f8616ebfSAlan Cox if (map == kernel_map && end > kernel_vm_end) 1266f8616ebfSAlan Cox pmap_growkernel(end); 12671d284e00SAlan Cox if (object != NULL) { 126830dcfc09SJohn Dyson /* 12691d284e00SAlan Cox * OBJ_ONEMAPPING must be cleared unless this mapping 12701d284e00SAlan Cox * is trivially proven to be the only mapping for any 12711d284e00SAlan Cox * of the object's pages. (Object granularity 12721d284e00SAlan Cox * reference counting is insufficient to recognize 12731d284e00SAlan Cox * aliases with precision.) 127430dcfc09SJohn Dyson */ 127589f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12761d284e00SAlan Cox if (object->ref_count > 1 || object->shadow_count != 0) 12772aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 127889f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12791569205fSKonstantin Belousov } else if (prev_entry != &map->header && 12801569205fSKonstantin Belousov prev_entry->eflags == protoeflags && 1281b5f8c226SKonstantin Belousov (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 && 12821569205fSKonstantin Belousov prev_entry->end == start && prev_entry->wired_count == 0 && 1283ef694c1aSEdward Tomasz Napierala (prev_entry->cred == cred || 12843364c323SKonstantin Belousov (prev_entry->object.vm_object != NULL && 12851569205fSKonstantin Belousov prev_entry->object.vm_object->cred == cred)) && 12868cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 128757a21abaSAlan Cox prev_entry->offset, 12888cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 128960169c88SAlan Cox (vm_size_t)(end - prev_entry->end), cred != NULL && 129060169c88SAlan Cox (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) { 129130dcfc09SJohn Dyson /* 12922aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 12932aaeadf8SMatthew Dillon * can extend the previous map entry to include the 12942aaeadf8SMatthew Dillon * new range as well. 129530dcfc09SJohn Dyson */ 12961569205fSKonstantin Belousov if (prev_entry->inheritance == inheritance && 12971569205fSKonstantin Belousov prev_entry->protection == prot && 12981569205fSKonstantin Belousov prev_entry->max_protection == max) { 129919bd0d9cSKonstantin Belousov if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0) 13001569205fSKonstantin Belousov map->size += end - prev_entry->end; 130130dcfc09SJohn Dyson prev_entry->end = end; 13020164e057SAlan Cox vm_map_entry_resize_free(map, prev_entry); 13034e71e795SMatthew Dillon vm_map_simplify_entry(map, prev_entry); 130430dcfc09SJohn Dyson return (KERN_SUCCESS); 130530dcfc09SJohn Dyson } 13068cc7e047SJohn Dyson 13072aaeadf8SMatthew Dillon /* 13082aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 13092aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 13102aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 13114e71e795SMatthew Dillon * account for it. object may be NULL. 13122aaeadf8SMatthew Dillon */ 13132aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 13142aaeadf8SMatthew Dillon offset = prev_entry->offset + 13152aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 13168cc7e047SJohn Dyson vm_object_reference(object); 1317ef694c1aSEdward Tomasz Napierala if (cred != NULL && object != NULL && object->cred != NULL && 13183364c323SKonstantin Belousov !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) { 13193364c323SKonstantin Belousov /* Object already accounts for this uid. */ 1320ef694c1aSEdward Tomasz Napierala cred = NULL; 13213364c323SKonstantin Belousov } 1322b18bfc3dSJohn Dyson } 132360169c88SAlan Cox if (cred != NULL) 132460169c88SAlan Cox crhold(cred); 13252aaeadf8SMatthew Dillon 13262aaeadf8SMatthew Dillon /* 132730dcfc09SJohn Dyson * Create a new entry 132830dcfc09SJohn Dyson */ 132930dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 133030dcfc09SJohn Dyson new_entry->start = start; 133130dcfc09SJohn Dyson new_entry->end = end; 1332ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL; 133330dcfc09SJohn Dyson 1334afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 133530dcfc09SJohn Dyson new_entry->object.vm_object = object; 133630dcfc09SJohn Dyson new_entry->offset = offset; 13372267af78SJulian Elischer 13388211bd45SKonstantin Belousov new_entry->inheritance = inheritance; 133930dcfc09SJohn Dyson new_entry->protection = prot; 134030dcfc09SJohn Dyson new_entry->max_protection = max; 134130dcfc09SJohn Dyson new_entry->wired_count = 0; 1342997ac690SKonstantin Belousov new_entry->wiring_thread = NULL; 134313458803SAlan Cox new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT; 1344381b7242SAlan Cox new_entry->next_read = start; 1345e5f251d2SAlan Cox 1346ef694c1aSEdward Tomasz Napierala KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry), 13471569205fSKonstantin Belousov ("overcommit: vm_map_insert leaks vm_map %p", new_entry)); 1348ef694c1aSEdward Tomasz Napierala new_entry->cred = cred; 13493364c323SKonstantin Belousov 135030dcfc09SJohn Dyson /* 135130dcfc09SJohn Dyson * Insert the new entry into the list 135230dcfc09SJohn Dyson */ 135330dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 135419bd0d9cSKonstantin Belousov if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0) 135530dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 135630dcfc09SJohn Dyson 13571a484d28SMatthew Dillon /* 1358eaaf9f7fSAlan Cox * Try to coalesce the new entry with both the previous and next 1359eaaf9f7fSAlan Cox * entries in the list. Previously, we only attempted to coalesce 1360eaaf9f7fSAlan Cox * with the previous entry when object is NULL. Here, we handle the 1361eaaf9f7fSAlan Cox * other cases, which are less common. 13621a484d28SMatthew Dillon */ 13634e71e795SMatthew Dillon vm_map_simplify_entry(map, new_entry); 13644e71e795SMatthew Dillon 13651569205fSKonstantin Belousov if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) { 13661569205fSKonstantin Belousov vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset), 13671569205fSKonstantin Belousov end - start, cow & MAP_PREFAULT_PARTIAL); 13684f79d873SMatthew Dillon } 1369e972780aSAlan Cox 137030dcfc09SJohn Dyson return (KERN_SUCCESS); 137130dcfc09SJohn Dyson } 137230dcfc09SJohn Dyson 137330dcfc09SJohn Dyson /* 13740164e057SAlan Cox * vm_map_findspace: 13750164e057SAlan Cox * 13760164e057SAlan Cox * Find the first fit (lowest VM address) for "length" free bytes 13770164e057SAlan Cox * beginning at address >= start in the given map. 13780164e057SAlan Cox * 13790164e057SAlan Cox * In a vm_map_entry, "adj_free" is the amount of free space 13800164e057SAlan Cox * adjacent (higher address) to this entry, and "max_free" is the 13810164e057SAlan Cox * maximum amount of contiguous free space in its subtree. This 13820164e057SAlan Cox * allows finding a free region in one path down the tree, so 13830164e057SAlan Cox * O(log n) amortized with splay trees. 13840164e057SAlan Cox * 13850164e057SAlan Cox * The map must be locked, and leaves it so. 13860164e057SAlan Cox * 13870164e057SAlan Cox * Returns: 0 on success, and starting address in *addr, 13880164e057SAlan Cox * 1 if insufficient space. 1389df8bae1dSRodney W. Grimes */ 1390df8bae1dSRodney W. Grimes int 13910164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length, 13920164e057SAlan Cox vm_offset_t *addr) /* OUT */ 1393df8bae1dSRodney W. Grimes { 13940164e057SAlan Cox vm_map_entry_t entry; 1395f8616ebfSAlan Cox vm_offset_t st; 1396df8bae1dSRodney W. Grimes 1397986b43f8SAlan Cox /* 1398986b43f8SAlan Cox * Request must fit within min/max VM address and must avoid 1399986b43f8SAlan Cox * address wrap. 1400986b43f8SAlan Cox */ 1401df8bae1dSRodney W. Grimes if (start < map->min_offset) 1402df8bae1dSRodney W. Grimes start = map->min_offset; 1403986b43f8SAlan Cox if (start + length > map->max_offset || start + length < start) 1404df8bae1dSRodney W. Grimes return (1); 1405df8bae1dSRodney W. Grimes 14060164e057SAlan Cox /* Empty tree means wide open address space. */ 14070164e057SAlan Cox if (map->root == NULL) { 1408df8bae1dSRodney W. Grimes *addr = start; 1409f8616ebfSAlan Cox return (0); 141099448ed1SJohn Dyson } 14110164e057SAlan Cox 14120164e057SAlan Cox /* 14130164e057SAlan Cox * After splay, if start comes before root node, then there 14140164e057SAlan Cox * must be a gap from start to the root. 14150164e057SAlan Cox */ 14160164e057SAlan Cox map->root = vm_map_entry_splay(start, map->root); 14170164e057SAlan Cox if (start + length <= map->root->start) { 14180164e057SAlan Cox *addr = start; 1419f8616ebfSAlan Cox return (0); 14200164e057SAlan Cox } 14210164e057SAlan Cox 14220164e057SAlan Cox /* 14230164e057SAlan Cox * Root is the last node that might begin its gap before 1424986b43f8SAlan Cox * start, and this is the last comparison where address 1425986b43f8SAlan Cox * wrap might be a problem. 14260164e057SAlan Cox */ 14270164e057SAlan Cox st = (start > map->root->end) ? start : map->root->end; 1428986b43f8SAlan Cox if (length <= map->root->end + map->root->adj_free - st) { 14290164e057SAlan Cox *addr = st; 1430f8616ebfSAlan Cox return (0); 14310164e057SAlan Cox } 14320164e057SAlan Cox 14330164e057SAlan Cox /* With max_free, can immediately tell if no solution. */ 14340164e057SAlan Cox entry = map->root->right; 14350164e057SAlan Cox if (entry == NULL || length > entry->max_free) 14360164e057SAlan Cox return (1); 14370164e057SAlan Cox 14380164e057SAlan Cox /* 14390164e057SAlan Cox * Search the right subtree in the order: left subtree, root, 14400164e057SAlan Cox * right subtree (first fit). The previous splay implies that 14410164e057SAlan Cox * all regions in the right subtree have addresses > start. 14420164e057SAlan Cox */ 14430164e057SAlan Cox while (entry != NULL) { 14440164e057SAlan Cox if (entry->left != NULL && entry->left->max_free >= length) 14450164e057SAlan Cox entry = entry->left; 14460164e057SAlan Cox else if (entry->adj_free >= length) { 14470164e057SAlan Cox *addr = entry->end; 1448f8616ebfSAlan Cox return (0); 14490164e057SAlan Cox } else 14500164e057SAlan Cox entry = entry->right; 14510164e057SAlan Cox } 14520164e057SAlan Cox 14530164e057SAlan Cox /* Can't get here, so panic if we do. */ 14540164e057SAlan Cox panic("vm_map_findspace: max_free corrupt"); 1455df8bae1dSRodney W. Grimes } 1456df8bae1dSRodney W. Grimes 1457d239bd3cSKonstantin Belousov int 1458d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1459b8ca4ef2SAlan Cox vm_offset_t start, vm_size_t length, vm_prot_t prot, 1460d239bd3cSKonstantin Belousov vm_prot_t max, int cow) 1461d239bd3cSKonstantin Belousov { 1462b8ca4ef2SAlan Cox vm_offset_t end; 1463d239bd3cSKonstantin Belousov int result; 1464d239bd3cSKonstantin Belousov 1465d239bd3cSKonstantin Belousov end = start + length; 14664648ba0aSKonstantin Belousov KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 || 14674648ba0aSKonstantin Belousov object == NULL, 14684648ba0aSKonstantin Belousov ("vm_map_fixed: non-NULL backing object for stack")); 1469897d81a0SKonstantin Belousov vm_map_lock(map); 1470d239bd3cSKonstantin Belousov VM_MAP_RANGE_CHECK(map, start, end); 147111c42bccSKonstantin Belousov if ((cow & MAP_CHECK_EXCL) == 0) 147211c42bccSKonstantin Belousov vm_map_delete(map, start, end); 14734648ba0aSKonstantin Belousov if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) { 14744648ba0aSKonstantin Belousov result = vm_map_stack_locked(map, start, length, sgrowsiz, 14754648ba0aSKonstantin Belousov prot, max, cow); 14764648ba0aSKonstantin Belousov } else { 14774648ba0aSKonstantin Belousov result = vm_map_insert(map, object, offset, start, end, 14784648ba0aSKonstantin Belousov prot, max, cow); 14794648ba0aSKonstantin Belousov } 1480d239bd3cSKonstantin Belousov vm_map_unlock(map); 1481d239bd3cSKonstantin Belousov return (result); 1482d239bd3cSKonstantin Belousov } 1483d239bd3cSKonstantin Belousov 1484df8bae1dSRodney W. Grimes /* 1485df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 1486df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 1487df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 1488df8bae1dSRodney W. Grimes * returned in the same parameter. 1489df8bae1dSRodney W. Grimes * 14902aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 14912aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 1492df8bae1dSRodney W. Grimes */ 1493df8bae1dSRodney W. Grimes int 1494b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1495b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 1496edb572a3SJohn Baldwin vm_size_t length, vm_offset_t max_addr, int find_space, 1497edb572a3SJohn Baldwin vm_prot_t prot, vm_prot_t max, int cow) 1498df8bae1dSRodney W. Grimes { 14995aa60b6fSJohn Baldwin vm_offset_t alignment, initial_addr, start; 15006eaee3feSAlan Cox int result; 1501df8bae1dSRodney W. Grimes 15024648ba0aSKonstantin Belousov KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 || 15034648ba0aSKonstantin Belousov object == NULL, 15044648ba0aSKonstantin Belousov ("vm_map_find: non-NULL backing object for stack")); 1505ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL || 1506ff74a3faSJohn Baldwin (object->flags & OBJ_COLORED) == 0)) 1507ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE; 15085aa60b6fSJohn Baldwin if (find_space >> 8 != 0) { 15095aa60b6fSJohn Baldwin KASSERT((find_space & 0xff) == 0, ("bad VMFS flags")); 15105aa60b6fSJohn Baldwin alignment = (vm_offset_t)1 << (find_space >> 8); 15115aa60b6fSJohn Baldwin } else 15125aa60b6fSJohn Baldwin alignment = 0; 1513ff74a3faSJohn Baldwin initial_addr = *addr; 1514ff74a3faSJohn Baldwin again: 1515ff74a3faSJohn Baldwin start = initial_addr; 1516bea41bcfSDavid Greenman vm_map_lock(map); 151726c538ffSAlan Cox do { 151826c538ffSAlan Cox if (find_space != VMFS_NO_SPACE) { 1519edb572a3SJohn Baldwin if (vm_map_findspace(map, start, length, addr) || 1520edb572a3SJohn Baldwin (max_addr != 0 && *addr + length > max_addr)) { 1521df8bae1dSRodney W. Grimes vm_map_unlock(map); 1522ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE) { 1523ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE; 1524ff74a3faSJohn Baldwin goto again; 1525ff74a3faSJohn Baldwin } 1526df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 1527df8bae1dSRodney W. Grimes } 1528ca596a25SJuli Mallett switch (find_space) { 15295aa60b6fSJohn Baldwin case VMFS_SUPER_SPACE: 1530ff74a3faSJohn Baldwin case VMFS_OPTIMAL_SPACE: 153126c538ffSAlan Cox pmap_align_superpage(object, offset, addr, 153226c538ffSAlan Cox length); 1533ca596a25SJuli Mallett break; 15345aa60b6fSJohn Baldwin case VMFS_ANY_SPACE: 15355aa60b6fSJohn Baldwin break; 1536ca596a25SJuli Mallett default: 15375aa60b6fSJohn Baldwin if ((*addr & (alignment - 1)) != 0) { 15385aa60b6fSJohn Baldwin *addr &= ~(alignment - 1); 15395aa60b6fSJohn Baldwin *addr += alignment; 15405aa60b6fSJohn Baldwin } 1541ca596a25SJuli Mallett break; 1542ca596a25SJuli Mallett } 1543ca596a25SJuli Mallett 1544df8bae1dSRodney W. Grimes start = *addr; 1545df8bae1dSRodney W. Grimes } 15464648ba0aSKonstantin Belousov if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) { 15474648ba0aSKonstantin Belousov result = vm_map_stack_locked(map, start, length, 15484648ba0aSKonstantin Belousov sgrowsiz, prot, max, cow); 15494648ba0aSKonstantin Belousov } else { 15504648ba0aSKonstantin Belousov result = vm_map_insert(map, object, offset, start, 15514648ba0aSKonstantin Belousov start + length, prot, max, cow); 15524648ba0aSKonstantin Belousov } 15535aa60b6fSJohn Baldwin } while (result == KERN_NO_SPACE && find_space != VMFS_NO_SPACE && 15545aa60b6fSJohn Baldwin find_space != VMFS_ANY_SPACE); 1555df8bae1dSRodney W. Grimes vm_map_unlock(map); 1556df8bae1dSRodney W. Grimes return (result); 1557df8bae1dSRodney W. Grimes } 1558df8bae1dSRodney W. Grimes 15596a97a3f7SKonstantin Belousov int 15606a97a3f7SKonstantin Belousov vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 15616a97a3f7SKonstantin Belousov vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr, 15626a97a3f7SKonstantin Belousov vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max, 15636a97a3f7SKonstantin Belousov int cow) 15646a97a3f7SKonstantin Belousov { 15656a97a3f7SKonstantin Belousov vm_offset_t hint; 15666a97a3f7SKonstantin Belousov int rv; 15676a97a3f7SKonstantin Belousov 15686a97a3f7SKonstantin Belousov hint = *addr; 15696a97a3f7SKonstantin Belousov for (;;) { 15706a97a3f7SKonstantin Belousov rv = vm_map_find(map, object, offset, addr, length, max_addr, 15716a97a3f7SKonstantin Belousov find_space, prot, max, cow); 15726a97a3f7SKonstantin Belousov if (rv == KERN_SUCCESS || min_addr >= hint) 15736a97a3f7SKonstantin Belousov return (rv); 15746a97a3f7SKonstantin Belousov *addr = min_addr; 15756a97a3f7SKonstantin Belousov } 15766a97a3f7SKonstantin Belousov } 15776a97a3f7SKonstantin Belousov 1578df8bae1dSRodney W. Grimes /* 1579b7b2aac2SJohn Dyson * vm_map_simplify_entry: 158067bf6868SJohn Dyson * 15814e71e795SMatthew Dillon * Simplify the given map entry by merging with either neighbor. This 15824e71e795SMatthew Dillon * routine also has the ability to merge with both neighbors. 15834e71e795SMatthew Dillon * 15844e71e795SMatthew Dillon * The map must be locked. 15854e71e795SMatthew Dillon * 1586ba7c64d1SKonstantin Belousov * This routine guarantees that the passed entry remains valid (though 15874e71e795SMatthew Dillon * possibly extended). When merging, this routine may delete one or 15884e71e795SMatthew Dillon * both neighbors. 1589df8bae1dSRodney W. Grimes */ 15900afcd3afSAlan Cox void 15911b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1592df8bae1dSRodney W. Grimes { 1593308c24baSJohn Dyson vm_map_entry_t next, prev; 1594b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 1595df8bae1dSRodney W. Grimes 1596eaaf9f7fSAlan Cox if ((entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | 1597eaaf9f7fSAlan Cox MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) != 0) 1598df8bae1dSRodney W. Grimes return; 1599308c24baSJohn Dyson 1600308c24baSJohn Dyson prev = entry->prev; 1601308c24baSJohn Dyson if (prev != &map->header) { 160267bf6868SJohn Dyson prevsize = prev->end - prev->start; 160367bf6868SJohn Dyson if ( (prev->end == entry->start) && 160467bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 160595e5e988SJohn Dyson (!prev->object.vm_object || 160667bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 1607afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 160867bf6868SJohn Dyson (prev->protection == entry->protection) && 160967bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 161067bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 16113364c323SKonstantin Belousov (prev->wired_count == entry->wired_count) && 1612ef694c1aSEdward Tomasz Napierala (prev->cred == entry->cred)) { 1613308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 1614308c24baSJohn Dyson entry->start = prev->start; 1615308c24baSJohn Dyson entry->offset = prev->offset; 16160164e057SAlan Cox if (entry->prev != &map->header) 16170164e057SAlan Cox vm_map_entry_resize_free(map, entry->prev); 16187fd10fb3SKonstantin Belousov 16197fd10fb3SKonstantin Belousov /* 1620b0994946SKonstantin Belousov * If the backing object is a vnode object, 1621b0994946SKonstantin Belousov * vm_object_deallocate() calls vrele(). 1622b0994946SKonstantin Belousov * However, vrele() does not lock the vnode 1623b0994946SKonstantin Belousov * because the vnode has additional 1624b0994946SKonstantin Belousov * references. Thus, the map lock can be kept 1625b0994946SKonstantin Belousov * without causing a lock-order reversal with 1626b0994946SKonstantin Belousov * the vnode lock. 162784110e7eSKonstantin Belousov * 162884110e7eSKonstantin Belousov * Since we count the number of virtual page 162984110e7eSKonstantin Belousov * mappings in object->un_pager.vnp.writemappings, 163084110e7eSKonstantin Belousov * the writemappings value should not be adjusted 163184110e7eSKonstantin Belousov * when the entry is disposed of. 16327fd10fb3SKonstantin Belousov */ 1633b18bfc3dSJohn Dyson if (prev->object.vm_object) 1634308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 1635ef694c1aSEdward Tomasz Napierala if (prev->cred != NULL) 1636ef694c1aSEdward Tomasz Napierala crfree(prev->cred); 1637308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 1638308c24baSJohn Dyson } 1639308c24baSJohn Dyson } 1640de5f6a77SJohn Dyson 1641de5f6a77SJohn Dyson next = entry->next; 1642308c24baSJohn Dyson if (next != &map->header) { 164367bf6868SJohn Dyson esize = entry->end - entry->start; 164467bf6868SJohn Dyson if ((entry->end == next->start) && 164567bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 164667bf6868SJohn Dyson (!entry->object.vm_object || 164767bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 1648afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 164967bf6868SJohn Dyson (next->protection == entry->protection) && 165067bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 165167bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 16523364c323SKonstantin Belousov (next->wired_count == entry->wired_count) && 1653ef694c1aSEdward Tomasz Napierala (next->cred == entry->cred)) { 1654de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 1655de5f6a77SJohn Dyson entry->end = next->end; 16560164e057SAlan Cox vm_map_entry_resize_free(map, entry); 16577fd10fb3SKonstantin Belousov 16587fd10fb3SKonstantin Belousov /* 16597fd10fb3SKonstantin Belousov * See comment above. 16607fd10fb3SKonstantin Belousov */ 1661b18bfc3dSJohn Dyson if (next->object.vm_object) 1662de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 1663ef694c1aSEdward Tomasz Napierala if (next->cred != NULL) 1664ef694c1aSEdward Tomasz Napierala crfree(next->cred); 1665de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 1666df8bae1dSRodney W. Grimes } 1667df8bae1dSRodney W. Grimes } 1668de5f6a77SJohn Dyson } 1669df8bae1dSRodney W. Grimes /* 1670df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 1671df8bae1dSRodney W. Grimes * 1672df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 1673df8bae1dSRodney W. Grimes * the specified address; if necessary, 1674df8bae1dSRodney W. Grimes * it splits the entry into two. 1675df8bae1dSRodney W. Grimes */ 1676df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 1677df8bae1dSRodney W. Grimes { \ 1678df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 1679df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 1680df8bae1dSRodney W. Grimes } 1681df8bae1dSRodney W. Grimes 1682df8bae1dSRodney W. Grimes /* 1683df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1684df8bae1dSRodney W. Grimes * the entry must be split. 1685df8bae1dSRodney W. Grimes */ 16860d94caffSDavid Greenman static void 16871b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1688df8bae1dSRodney W. Grimes { 1689c0877f10SJohn Dyson vm_map_entry_t new_entry; 1690df8bae1dSRodney W. Grimes 16913a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 1692ed11e4d7SMark Johnston KASSERT(entry->end > start && entry->start < start, 1693ed11e4d7SMark Johnston ("_vm_map_clip_start: invalid clip of entry %p", entry)); 16943a0916b8SKonstantin Belousov 1695df8bae1dSRodney W. Grimes /* 16960d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 16970d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 16980d94caffSDavid Greenman * starting address. 1699df8bae1dSRodney W. Grimes */ 1700f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 1701f32dbbeeSJohn Dyson 170211cccda1SJohn Dyson /* 170311cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 170411cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 170511cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 170611cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 170711cccda1SJohn Dyson * put this improvement. 170811cccda1SJohn Dyson */ 170919bd0d9cSKonstantin Belousov if (entry->object.vm_object == NULL && !map->system_map && 171019bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) == 0) { 171111cccda1SJohn Dyson vm_object_t object; 171211cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1713c2e11a03SJohn Dyson atop(entry->end - entry->start)); 171411cccda1SJohn Dyson entry->object.vm_object = object; 171511cccda1SJohn Dyson entry->offset = 0; 1716ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 1717ef694c1aSEdward Tomasz Napierala object->cred = entry->cred; 17183364c323SKonstantin Belousov object->charge = entry->end - entry->start; 1719ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 17203364c323SKonstantin Belousov } 17213364c323SKonstantin Belousov } else if (entry->object.vm_object != NULL && 17223364c323SKonstantin Belousov ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1723ef694c1aSEdward Tomasz Napierala entry->cred != NULL) { 172489f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 1725ef694c1aSEdward Tomasz Napierala KASSERT(entry->object.vm_object->cred == NULL, 1726ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry)); 1727ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 17283364c323SKonstantin Belousov entry->object.vm_object->charge = entry->end - entry->start; 172989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 1730ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 173111cccda1SJohn Dyson } 173211cccda1SJohn Dyson 1733df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1734df8bae1dSRodney W. Grimes *new_entry = *entry; 1735df8bae1dSRodney W. Grimes 1736df8bae1dSRodney W. Grimes new_entry->end = start; 1737df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 1738df8bae1dSRodney W. Grimes entry->start = start; 1739ef694c1aSEdward Tomasz Napierala if (new_entry->cred != NULL) 1740ef694c1aSEdward Tomasz Napierala crhold(entry->cred); 1741df8bae1dSRodney W. Grimes 1742df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 1743df8bae1dSRodney W. Grimes 17449fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1745df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 174684110e7eSKonstantin Belousov /* 174784110e7eSKonstantin Belousov * The object->un_pager.vnp.writemappings for the 174884110e7eSKonstantin Belousov * object of MAP_ENTRY_VN_WRITECNT type entry shall be 174984110e7eSKonstantin Belousov * kept as is here. The virtual pages are 175084110e7eSKonstantin Belousov * re-distributed among the clipped entries, so the sum is 175184110e7eSKonstantin Belousov * left the same. 175284110e7eSKonstantin Belousov */ 1753df8bae1dSRodney W. Grimes } 1754c0877f10SJohn Dyson } 1755df8bae1dSRodney W. Grimes 1756df8bae1dSRodney W. Grimes /* 1757df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 1758df8bae1dSRodney W. Grimes * 1759df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 1760df8bae1dSRodney W. Grimes * the specified address; if necessary, 1761df8bae1dSRodney W. Grimes * it splits the entry into two. 1762df8bae1dSRodney W. Grimes */ 1763df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 1764df8bae1dSRodney W. Grimes { \ 1765af045176SPoul-Henning Kamp if ((endaddr) < (entry->end)) \ 1766af045176SPoul-Henning Kamp _vm_map_clip_end((map), (entry), (endaddr)); \ 1767df8bae1dSRodney W. Grimes } 1768df8bae1dSRodney W. Grimes 1769df8bae1dSRodney W. Grimes /* 1770df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1771df8bae1dSRodney W. Grimes * the entry must be split. 1772df8bae1dSRodney W. Grimes */ 17730d94caffSDavid Greenman static void 17741b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1775df8bae1dSRodney W. Grimes { 1776c0877f10SJohn Dyson vm_map_entry_t new_entry; 1777df8bae1dSRodney W. Grimes 17783a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 1779ed11e4d7SMark Johnston KASSERT(entry->start < end && entry->end > end, 1780ed11e4d7SMark Johnston ("_vm_map_clip_end: invalid clip of entry %p", entry)); 17813a0916b8SKonstantin Belousov 1782df8bae1dSRodney W. Grimes /* 178311cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 178411cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 178511cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 178611cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 178711cccda1SJohn Dyson * put this improvement. 178811cccda1SJohn Dyson */ 178919bd0d9cSKonstantin Belousov if (entry->object.vm_object == NULL && !map->system_map && 179019bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) == 0) { 179111cccda1SJohn Dyson vm_object_t object; 179211cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1793c2e11a03SJohn Dyson atop(entry->end - entry->start)); 179411cccda1SJohn Dyson entry->object.vm_object = object; 179511cccda1SJohn Dyson entry->offset = 0; 1796ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 1797ef694c1aSEdward Tomasz Napierala object->cred = entry->cred; 17983364c323SKonstantin Belousov object->charge = entry->end - entry->start; 1799ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 18003364c323SKonstantin Belousov } 18013364c323SKonstantin Belousov } else if (entry->object.vm_object != NULL && 18023364c323SKonstantin Belousov ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1803ef694c1aSEdward Tomasz Napierala entry->cred != NULL) { 180489f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 1805ef694c1aSEdward Tomasz Napierala KASSERT(entry->object.vm_object->cred == NULL, 1806ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry)); 1807ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 18083364c323SKonstantin Belousov entry->object.vm_object->charge = entry->end - entry->start; 180989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 1810ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 181111cccda1SJohn Dyson } 181211cccda1SJohn Dyson 181311cccda1SJohn Dyson /* 18140d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 1815df8bae1dSRodney W. Grimes */ 1816df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1817df8bae1dSRodney W. Grimes *new_entry = *entry; 1818df8bae1dSRodney W. Grimes 1819df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 1820df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 1821ef694c1aSEdward Tomasz Napierala if (new_entry->cred != NULL) 1822ef694c1aSEdward Tomasz Napierala crhold(entry->cred); 1823df8bae1dSRodney W. Grimes 1824df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 1825df8bae1dSRodney W. Grimes 18269fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1827df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1828df8bae1dSRodney W. Grimes } 1829c0877f10SJohn Dyson } 1830df8bae1dSRodney W. Grimes 1831df8bae1dSRodney W. Grimes /* 1832df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 1833df8bae1dSRodney W. Grimes * 1834df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 1835df8bae1dSRodney W. Grimes * 1836df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 1837df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 1838df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 1839df8bae1dSRodney W. Grimes * 1840df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 1841df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 1842df8bae1dSRodney W. Grimes * vm_fault 1843df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 1844df8bae1dSRodney W. Grimes * 1845df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 1846df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 1847df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 1848df8bae1dSRodney W. Grimes */ 1849df8bae1dSRodney W. Grimes int 18501b40f8c0SMatthew Dillon vm_map_submap( 18511b40f8c0SMatthew Dillon vm_map_t map, 18521b40f8c0SMatthew Dillon vm_offset_t start, 18531b40f8c0SMatthew Dillon vm_offset_t end, 18541b40f8c0SMatthew Dillon vm_map_t submap) 1855df8bae1dSRodney W. Grimes { 1856df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1857c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 1858df8bae1dSRodney W. Grimes 1859df8bae1dSRodney W. Grimes vm_map_lock(map); 1860df8bae1dSRodney W. Grimes 1861df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1862df8bae1dSRodney W. Grimes 1863df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1864df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 18650d94caffSDavid Greenman } else 1866df8bae1dSRodney W. Grimes entry = entry->next; 1867df8bae1dSRodney W. Grimes 1868df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1869df8bae1dSRodney W. Grimes 1870df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 18719fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 1872afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 18732d8acc0fSJohn Dyson entry->object.sub_map = submap; 1874afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1875df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 1876df8bae1dSRodney W. Grimes } 1877df8bae1dSRodney W. Grimes vm_map_unlock(map); 1878df8bae1dSRodney W. Grimes 1879df8bae1dSRodney W. Grimes return (result); 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes 1882df8bae1dSRodney W. Grimes /* 1883dd05fa19SAlan Cox * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified 18841f78f902SAlan Cox */ 18851f78f902SAlan Cox #define MAX_INIT_PT 96 18861f78f902SAlan Cox 18871f78f902SAlan Cox /* 18880551c08dSAlan Cox * vm_map_pmap_enter: 18890551c08dSAlan Cox * 1890dd05fa19SAlan Cox * Preload the specified map's pmap with mappings to the specified 1891dd05fa19SAlan Cox * object's memory-resident pages. No further physical pages are 1892dd05fa19SAlan Cox * allocated, and no further virtual pages are retrieved from secondary 1893dd05fa19SAlan Cox * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a 1894dd05fa19SAlan Cox * limited number of page mappings are created at the low-end of the 1895dd05fa19SAlan Cox * specified address range. (For this purpose, a superpage mapping 1896dd05fa19SAlan Cox * counts as one page mapping.) Otherwise, all resident pages within 18973453bca8SAlan Cox * the specified address range are mapped. 18980551c08dSAlan Cox */ 1899077ec27cSAlan Cox static void 19004da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 19010551c08dSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 19020551c08dSAlan Cox { 19038fece8c3SAlan Cox vm_offset_t start; 1904ce142d9eSAlan Cox vm_page_t p, p_start; 1905dd05fa19SAlan Cox vm_pindex_t mask, psize, threshold, tmpidx; 19060551c08dSAlan Cox 1907ba8bca61SAlan Cox if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 19081f78f902SAlan Cox return; 19099af6d512SAttilio Rao VM_OBJECT_RLOCK(object); 19109af6d512SAttilio Rao if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 19119af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 191289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 191301381811SJohn Baldwin if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 19149af6d512SAttilio Rao pmap_object_init_pt(map->pmap, addr, object, pindex, 19159af6d512SAttilio Rao size); 19169af6d512SAttilio Rao VM_OBJECT_WUNLOCK(object); 19179af6d512SAttilio Rao return; 19189af6d512SAttilio Rao } 19199af6d512SAttilio Rao VM_OBJECT_LOCK_DOWNGRADE(object); 19201f78f902SAlan Cox } 19211f78f902SAlan Cox 19221f78f902SAlan Cox psize = atop(size); 19231f78f902SAlan Cox if (psize + pindex > object->size) { 19249af6d512SAttilio Rao if (object->size < pindex) { 19259af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 19269af6d512SAttilio Rao return; 19279af6d512SAttilio Rao } 19281f78f902SAlan Cox psize = object->size - pindex; 19291f78f902SAlan Cox } 19301f78f902SAlan Cox 1931ce142d9eSAlan Cox start = 0; 1932ce142d9eSAlan Cox p_start = NULL; 1933dd05fa19SAlan Cox threshold = MAX_INIT_PT; 19341f78f902SAlan Cox 1935b382c10aSKonstantin Belousov p = vm_page_find_least(object, pindex); 19361f78f902SAlan Cox /* 19371f78f902SAlan Cox * Assert: the variable p is either (1) the page with the 19381f78f902SAlan Cox * least pindex greater than or equal to the parameter pindex 19391f78f902SAlan Cox * or (2) NULL. 19401f78f902SAlan Cox */ 19411f78f902SAlan Cox for (; 19421f78f902SAlan Cox p != NULL && (tmpidx = p->pindex - pindex) < psize; 19431f78f902SAlan Cox p = TAILQ_NEXT(p, listq)) { 19441f78f902SAlan Cox /* 19451f78f902SAlan Cox * don't allow an madvise to blow away our really 19461f78f902SAlan Cox * free pages allocating pv entries. 19471f78f902SAlan Cox */ 1948dd05fa19SAlan Cox if (((flags & MAP_PREFAULT_MADVISE) != 0 && 1949dd05fa19SAlan Cox vm_cnt.v_free_count < vm_cnt.v_free_reserved) || 1950dd05fa19SAlan Cox ((flags & MAP_PREFAULT_PARTIAL) != 0 && 1951dd05fa19SAlan Cox tmpidx >= threshold)) { 1952379fb642SAlan Cox psize = tmpidx; 19531f78f902SAlan Cox break; 19541f78f902SAlan Cox } 19550a2e596aSAlan Cox if (p->valid == VM_PAGE_BITS_ALL) { 1956ce142d9eSAlan Cox if (p_start == NULL) { 1957ce142d9eSAlan Cox start = addr + ptoa(tmpidx); 1958ce142d9eSAlan Cox p_start = p; 1959ce142d9eSAlan Cox } 1960dd05fa19SAlan Cox /* Jump ahead if a superpage mapping is possible. */ 1961dd05fa19SAlan Cox if (p->psind > 0 && ((addr + ptoa(tmpidx)) & 1962dd05fa19SAlan Cox (pagesizes[p->psind] - 1)) == 0) { 1963dd05fa19SAlan Cox mask = atop(pagesizes[p->psind]) - 1; 1964dd05fa19SAlan Cox if (tmpidx + mask < psize && 1965dd05fa19SAlan Cox vm_page_ps_is_valid(p)) { 1966dd05fa19SAlan Cox p += mask; 1967dd05fa19SAlan Cox threshold += mask; 1968dd05fa19SAlan Cox } 1969dd05fa19SAlan Cox } 19707bfda801SAlan Cox } else if (p_start != NULL) { 1971cf4682aeSAlan Cox pmap_enter_object(map->pmap, start, addr + 1972cf4682aeSAlan Cox ptoa(tmpidx), p_start, prot); 1973cf4682aeSAlan Cox p_start = NULL; 1974cf4682aeSAlan Cox } 1975cf4682aeSAlan Cox } 1976c46b90e9SAlan Cox if (p_start != NULL) 1977379fb642SAlan Cox pmap_enter_object(map->pmap, start, addr + ptoa(psize), 1978379fb642SAlan Cox p_start, prot); 19799af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 19800551c08dSAlan Cox } 19810551c08dSAlan Cox 19820551c08dSAlan Cox /* 1983df8bae1dSRodney W. Grimes * vm_map_protect: 1984df8bae1dSRodney W. Grimes * 1985df8bae1dSRodney W. Grimes * Sets the protection of the specified address 1986df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 1987df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 1988df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 1989df8bae1dSRodney W. Grimes */ 1990df8bae1dSRodney W. Grimes int 1991b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1992b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 1993df8bae1dSRodney W. Grimes { 1994210a6886SKonstantin Belousov vm_map_entry_t current, entry; 19953364c323SKonstantin Belousov vm_object_t obj; 1996ef694c1aSEdward Tomasz Napierala struct ucred *cred; 1997210a6886SKonstantin Belousov vm_prot_t old_prot; 1998df8bae1dSRodney W. Grimes 199979e9451fSKonstantin Belousov if (start == end) 200079e9451fSKonstantin Belousov return (KERN_SUCCESS); 200179e9451fSKonstantin Belousov 2002df8bae1dSRodney W. Grimes vm_map_lock(map); 2003df8bae1dSRodney W. Grimes 2004e1cb9d37SMark Johnston /* 2005e1cb9d37SMark Johnston * Ensure that we are not concurrently wiring pages. vm_map_wire() may 2006e1cb9d37SMark Johnston * need to fault pages into the map and will drop the map lock while 2007e1cb9d37SMark Johnston * doing so, and the VM object may end up in an inconsistent state if we 2008e1cb9d37SMark Johnston * update the protection on the map entry in between faults. 2009e1cb9d37SMark Johnston */ 2010e1cb9d37SMark Johnston vm_map_wait_busy(map); 2011e1cb9d37SMark Johnston 2012df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2013df8bae1dSRodney W. Grimes 2014df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 2015df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 2016b7b2aac2SJohn Dyson } else { 2017df8bae1dSRodney W. Grimes entry = entry->next; 2018b7b2aac2SJohn Dyson } 2019df8bae1dSRodney W. Grimes 2020df8bae1dSRodney W. Grimes /* 20210d94caffSDavid Greenman * Make a first pass to check for protection violations. 2022df8bae1dSRodney W. Grimes */ 20231c2d20a1SMark Johnston for (current = entry; current != &map->header && current->start < end; 20241c2d20a1SMark Johnston current = current->next) { 20258a89ca94SKonstantin Belousov if ((current->eflags & MAP_ENTRY_GUARD) != 0) 20268a89ca94SKonstantin Belousov continue; 2027afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2028a1f6d91cSDavid Greenman vm_map_unlock(map); 2029df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2030a1f6d91cSDavid Greenman } 2031df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 2032df8bae1dSRodney W. Grimes vm_map_unlock(map); 2033df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 2034df8bae1dSRodney W. Grimes } 2035df8bae1dSRodney W. Grimes } 2036df8bae1dSRodney W. Grimes 20373364c323SKonstantin Belousov /* 20383364c323SKonstantin Belousov * Do an accounting pass for private read-only mappings that 20393364c323SKonstantin Belousov * now will do cow due to allowed write (e.g. debugger sets 20403364c323SKonstantin Belousov * breakpoint on text segment) 20413364c323SKonstantin Belousov */ 20421c2d20a1SMark Johnston for (current = entry; current != &map->header && current->start < end; 20431c2d20a1SMark Johnston current = current->next) { 20443364c323SKonstantin Belousov 20453364c323SKonstantin Belousov vm_map_clip_end(map, current, end); 20463364c323SKonstantin Belousov 20473364c323SKonstantin Belousov if (set_max || 20483364c323SKonstantin Belousov ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 || 204919bd0d9cSKonstantin Belousov ENTRY_CHARGED(current) || 205019bd0d9cSKonstantin Belousov (current->eflags & MAP_ENTRY_GUARD) != 0) { 20513364c323SKonstantin Belousov continue; 20523364c323SKonstantin Belousov } 20533364c323SKonstantin Belousov 2054ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 20553364c323SKonstantin Belousov obj = current->object.vm_object; 20563364c323SKonstantin Belousov 20573364c323SKonstantin Belousov if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) { 20583364c323SKonstantin Belousov if (!swap_reserve(current->end - current->start)) { 20593364c323SKonstantin Belousov vm_map_unlock(map); 20603364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 20613364c323SKonstantin Belousov } 2062ef694c1aSEdward Tomasz Napierala crhold(cred); 2063ef694c1aSEdward Tomasz Napierala current->cred = cred; 20643364c323SKonstantin Belousov continue; 20653364c323SKonstantin Belousov } 20663364c323SKonstantin Belousov 206789f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 20683364c323SKonstantin Belousov if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) { 206989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 20703364c323SKonstantin Belousov continue; 20713364c323SKonstantin Belousov } 20723364c323SKonstantin Belousov 20733364c323SKonstantin Belousov /* 20743364c323SKonstantin Belousov * Charge for the whole object allocation now, since 20753364c323SKonstantin Belousov * we cannot distinguish between non-charged and 20763364c323SKonstantin Belousov * charged clipped mapping of the same object later. 20773364c323SKonstantin Belousov */ 20783364c323SKonstantin Belousov KASSERT(obj->charge == 0, 20793d95614fSKonstantin Belousov ("vm_map_protect: object %p overcharged (entry %p)", 20803d95614fSKonstantin Belousov obj, current)); 20813364c323SKonstantin Belousov if (!swap_reserve(ptoa(obj->size))) { 208289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 20833364c323SKonstantin Belousov vm_map_unlock(map); 20843364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 20853364c323SKonstantin Belousov } 20863364c323SKonstantin Belousov 2087ef694c1aSEdward Tomasz Napierala crhold(cred); 2088ef694c1aSEdward Tomasz Napierala obj->cred = cred; 20893364c323SKonstantin Belousov obj->charge = ptoa(obj->size); 209089f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 20913364c323SKonstantin Belousov } 20923364c323SKonstantin Belousov 2093df8bae1dSRodney W. Grimes /* 20940d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 20950d94caffSDavid Greenman * necessary the second time.] 2096df8bae1dSRodney W. Grimes */ 20971c2d20a1SMark Johnston for (current = entry; current != &map->header && current->start < end; 20981c2d20a1SMark Johnston current = current->next) { 209919bd0d9cSKonstantin Belousov if ((current->eflags & MAP_ENTRY_GUARD) != 0) 210019bd0d9cSKonstantin Belousov continue; 210119bd0d9cSKonstantin Belousov 2102df8bae1dSRodney W. Grimes old_prot = current->protection; 2103210a6886SKonstantin Belousov 2104df8bae1dSRodney W. Grimes if (set_max) 2105df8bae1dSRodney W. Grimes current->protection = 2106df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 2107df8bae1dSRodney W. Grimes old_prot; 2108df8bae1dSRodney W. Grimes else 2109df8bae1dSRodney W. Grimes current->protection = new_prot; 2110df8bae1dSRodney W. Grimes 2111dd006a1bSAlan Cox /* 2112dd006a1bSAlan Cox * For user wired map entries, the normal lazy evaluation of 2113dd006a1bSAlan Cox * write access upgrades through soft page faults is 2114dd006a1bSAlan Cox * undesirable. Instead, immediately copy any pages that are 2115dd006a1bSAlan Cox * copy-on-write and enable write access in the physical map. 2116dd006a1bSAlan Cox */ 2117dd006a1bSAlan Cox if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 && 2118210a6886SKonstantin Belousov (current->protection & VM_PROT_WRITE) != 0 && 21195930251aSKonstantin Belousov (old_prot & VM_PROT_WRITE) == 0) 2120210a6886SKonstantin Belousov vm_fault_copy_entry(map, map, current, current, NULL); 2121210a6886SKonstantin Belousov 2122df8bae1dSRodney W. Grimes /* 21232fafce9eSAlan Cox * When restricting access, update the physical map. Worry 21242fafce9eSAlan Cox * about copy-on-write here. 2125df8bae1dSRodney W. Grimes */ 21262fafce9eSAlan Cox if ((old_prot & ~current->protection) != 0) { 2127afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 2128df8bae1dSRodney W. Grimes VM_PROT_ALL) 2129df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 2130df8bae1dSRodney W. Grimes current->end, 21311c85e3dfSAlan Cox current->protection & MASK(current)); 2132df8bae1dSRodney W. Grimes #undef MASK 2133df8bae1dSRodney W. Grimes } 21347d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 2135df8bae1dSRodney W. Grimes } 2136df8bae1dSRodney W. Grimes vm_map_unlock(map); 2137df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2138df8bae1dSRodney W. Grimes } 2139df8bae1dSRodney W. Grimes 2140df8bae1dSRodney W. Grimes /* 2141867a482dSJohn Dyson * vm_map_madvise: 2142867a482dSJohn Dyson * 2143867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 2144f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 2145f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 2146f7fc307aSAlan Cox * objects. 2147867a482dSJohn Dyson */ 2148b4309055SMatthew Dillon int 21491b40f8c0SMatthew Dillon vm_map_madvise( 21501b40f8c0SMatthew Dillon vm_map_t map, 21511b40f8c0SMatthew Dillon vm_offset_t start, 21521b40f8c0SMatthew Dillon vm_offset_t end, 21531b40f8c0SMatthew Dillon int behav) 2154867a482dSJohn Dyson { 2155f7fc307aSAlan Cox vm_map_entry_t current, entry; 2156b4309055SMatthew Dillon int modify_map = 0; 2157867a482dSJohn Dyson 2158b4309055SMatthew Dillon /* 2159b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 2160b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 2161b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 2162b4309055SMatthew Dillon * on the map. 2163b4309055SMatthew Dillon */ 2164b4309055SMatthew Dillon switch(behav) { 2165b4309055SMatthew Dillon case MADV_NORMAL: 2166b4309055SMatthew Dillon case MADV_SEQUENTIAL: 2167b4309055SMatthew Dillon case MADV_RANDOM: 21684f79d873SMatthew Dillon case MADV_NOSYNC: 21694f79d873SMatthew Dillon case MADV_AUTOSYNC: 21709730a5daSPaul Saab case MADV_NOCORE: 21719730a5daSPaul Saab case MADV_CORE: 217279e9451fSKonstantin Belousov if (start == end) 217379e9451fSKonstantin Belousov return (KERN_SUCCESS); 2174b4309055SMatthew Dillon modify_map = 1; 2175867a482dSJohn Dyson vm_map_lock(map); 2176b4309055SMatthew Dillon break; 2177b4309055SMatthew Dillon case MADV_WILLNEED: 2178b4309055SMatthew Dillon case MADV_DONTNEED: 2179b4309055SMatthew Dillon case MADV_FREE: 218079e9451fSKonstantin Belousov if (start == end) 218179e9451fSKonstantin Belousov return (KERN_SUCCESS); 2182f7fc307aSAlan Cox vm_map_lock_read(map); 2183b4309055SMatthew Dillon break; 2184b4309055SMatthew Dillon default: 2185b4309055SMatthew Dillon return (KERN_INVALID_ARGUMENT); 2186b4309055SMatthew Dillon } 2187b4309055SMatthew Dillon 2188b4309055SMatthew Dillon /* 2189b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 2190b4309055SMatthew Dillon */ 2191867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 2192867a482dSJohn Dyson 2193867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 2194f7fc307aSAlan Cox if (modify_map) 2195867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 2196b4309055SMatthew Dillon } else { 2197867a482dSJohn Dyson entry = entry->next; 2198b4309055SMatthew Dillon } 2199867a482dSJohn Dyson 2200f7fc307aSAlan Cox if (modify_map) { 2201f7fc307aSAlan Cox /* 2202f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 2203f7fc307aSAlan Cox * 2204f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 2205f7fc307aSAlan Cox * limited to the specified address range. 2206f7fc307aSAlan Cox */ 2207867a482dSJohn Dyson for (current = entry; 2208867a482dSJohn Dyson (current != &map->header) && (current->start < end); 2209b4309055SMatthew Dillon current = current->next 2210b4309055SMatthew Dillon ) { 2211f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2212867a482dSJohn Dyson continue; 2213fed9a903SJohn Dyson 221447221757SJohn Dyson vm_map_clip_end(map, current, end); 2215fed9a903SJohn Dyson 2216f7fc307aSAlan Cox switch (behav) { 2217867a482dSJohn Dyson case MADV_NORMAL: 22187f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 2219867a482dSJohn Dyson break; 2220867a482dSJohn Dyson case MADV_SEQUENTIAL: 22217f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 2222867a482dSJohn Dyson break; 2223867a482dSJohn Dyson case MADV_RANDOM: 22247f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 2225867a482dSJohn Dyson break; 22264f79d873SMatthew Dillon case MADV_NOSYNC: 22274f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 22284f79d873SMatthew Dillon break; 22294f79d873SMatthew Dillon case MADV_AUTOSYNC: 22304f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 22314f79d873SMatthew Dillon break; 22329730a5daSPaul Saab case MADV_NOCORE: 22339730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 22349730a5daSPaul Saab break; 22359730a5daSPaul Saab case MADV_CORE: 22369730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 22379730a5daSPaul Saab break; 2238867a482dSJohn Dyson default: 2239867a482dSJohn Dyson break; 2240867a482dSJohn Dyson } 2241f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 2242867a482dSJohn Dyson } 2243867a482dSJohn Dyson vm_map_unlock(map); 2244b4309055SMatthew Dillon } else { 224592a59946SJohn Baldwin vm_pindex_t pstart, pend; 2246f7fc307aSAlan Cox 2247f7fc307aSAlan Cox /* 2248f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 2249f7fc307aSAlan Cox * vm_object. 2250f7fc307aSAlan Cox * 2251f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 2252f7fc307aSAlan Cox * the vm_object pindex and count. 2253f7fc307aSAlan Cox */ 2254f7fc307aSAlan Cox for (current = entry; 2255f7fc307aSAlan Cox (current != &map->header) && (current->start < end); 2256b4309055SMatthew Dillon current = current->next 2257b4309055SMatthew Dillon ) { 225851321f7cSAlan Cox vm_offset_t useEnd, useStart; 22595f99b57cSMatthew Dillon 2260f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2261f7fc307aSAlan Cox continue; 2262f7fc307aSAlan Cox 226392a59946SJohn Baldwin pstart = OFF_TO_IDX(current->offset); 226492a59946SJohn Baldwin pend = pstart + atop(current->end - current->start); 22655f99b57cSMatthew Dillon useStart = current->start; 226651321f7cSAlan Cox useEnd = current->end; 2267f7fc307aSAlan Cox 2268f7fc307aSAlan Cox if (current->start < start) { 226992a59946SJohn Baldwin pstart += atop(start - current->start); 22705f99b57cSMatthew Dillon useStart = start; 2271f7fc307aSAlan Cox } 227251321f7cSAlan Cox if (current->end > end) { 227392a59946SJohn Baldwin pend -= atop(current->end - end); 227451321f7cSAlan Cox useEnd = end; 227551321f7cSAlan Cox } 2276f7fc307aSAlan Cox 227792a59946SJohn Baldwin if (pstart >= pend) 2278f7fc307aSAlan Cox continue; 2279f7fc307aSAlan Cox 228051321f7cSAlan Cox /* 228151321f7cSAlan Cox * Perform the pmap_advise() before clearing 228251321f7cSAlan Cox * PGA_REFERENCED in vm_page_advise(). Otherwise, a 228351321f7cSAlan Cox * concurrent pmap operation, such as pmap_remove(), 228451321f7cSAlan Cox * could clear a reference in the pmap and set 228551321f7cSAlan Cox * PGA_REFERENCED on the page before the pmap_advise() 228651321f7cSAlan Cox * had completed. Consequently, the page would appear 228751321f7cSAlan Cox * referenced based upon an old reference that 228851321f7cSAlan Cox * occurred before this pmap_advise() ran. 228951321f7cSAlan Cox */ 229051321f7cSAlan Cox if (behav == MADV_DONTNEED || behav == MADV_FREE) 229151321f7cSAlan Cox pmap_advise(map->pmap, useStart, useEnd, 229251321f7cSAlan Cox behav); 229351321f7cSAlan Cox 229492a59946SJohn Baldwin vm_object_madvise(current->object.vm_object, pstart, 229592a59946SJohn Baldwin pend, behav); 229654432196SKonstantin Belousov 229754432196SKonstantin Belousov /* 229854432196SKonstantin Belousov * Pre-populate paging structures in the 229954432196SKonstantin Belousov * WILLNEED case. For wired entries, the 230054432196SKonstantin Belousov * paging structures are already populated. 230154432196SKonstantin Belousov */ 230254432196SKonstantin Belousov if (behav == MADV_WILLNEED && 230354432196SKonstantin Belousov current->wired_count == 0) { 23040551c08dSAlan Cox vm_map_pmap_enter(map, 23055f99b57cSMatthew Dillon useStart, 23064da4d293SAlan Cox current->protection, 2307f7fc307aSAlan Cox current->object.vm_object, 230892a59946SJohn Baldwin pstart, 230992a59946SJohn Baldwin ptoa(pend - pstart), 2310e3026983SMatthew Dillon MAP_PREFAULT_MADVISE 2311b4309055SMatthew Dillon ); 2312f7fc307aSAlan Cox } 2313f7fc307aSAlan Cox } 2314f7fc307aSAlan Cox vm_map_unlock_read(map); 2315f7fc307aSAlan Cox } 2316b4309055SMatthew Dillon return (0); 2317867a482dSJohn Dyson } 2318867a482dSJohn Dyson 2319867a482dSJohn Dyson 2320867a482dSJohn Dyson /* 2321df8bae1dSRodney W. Grimes * vm_map_inherit: 2322df8bae1dSRodney W. Grimes * 2323df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 2324df8bae1dSRodney W. Grimes * range in the target map. Inheritance 2325df8bae1dSRodney W. Grimes * affects how the map will be shared with 2326e2abaaaaSAlan Cox * child maps at the time of vmspace_fork. 2327df8bae1dSRodney W. Grimes */ 2328df8bae1dSRodney W. Grimes int 2329b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 2330b9dcd593SBruce Evans vm_inherit_t new_inheritance) 2331df8bae1dSRodney W. Grimes { 2332c0877f10SJohn Dyson vm_map_entry_t entry; 2333df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 2334df8bae1dSRodney W. Grimes 2335df8bae1dSRodney W. Grimes switch (new_inheritance) { 2336df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2337df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2338df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 233978d7964bSXin LI case VM_INHERIT_ZERO: 2340df8bae1dSRodney W. Grimes break; 2341df8bae1dSRodney W. Grimes default: 2342df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2343df8bae1dSRodney W. Grimes } 234479e9451fSKonstantin Belousov if (start == end) 234579e9451fSKonstantin Belousov return (KERN_SUCCESS); 2346df8bae1dSRodney W. Grimes vm_map_lock(map); 2347df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2348df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 2349df8bae1dSRodney W. Grimes entry = temp_entry; 2350df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 23510d94caffSDavid Greenman } else 2352df8bae1dSRodney W. Grimes entry = temp_entry->next; 2353df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 2354df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 235519bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) == 0 || 235619bd0d9cSKonstantin Belousov new_inheritance != VM_INHERIT_ZERO) 2357df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 235844428f62SAlan Cox vm_map_simplify_entry(map, entry); 2359df8bae1dSRodney W. Grimes entry = entry->next; 2360df8bae1dSRodney W. Grimes } 2361df8bae1dSRodney W. Grimes vm_map_unlock(map); 2362df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2363df8bae1dSRodney W. Grimes } 2364df8bae1dSRodney W. Grimes 2365df8bae1dSRodney W. Grimes /* 2366acd9a301SAlan Cox * vm_map_unwire: 2367acd9a301SAlan Cox * 2368e27e17b7SAlan Cox * Implements both kernel and user unwiring. 2369acd9a301SAlan Cox */ 2370acd9a301SAlan Cox int 2371acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2372abd498aaSBruce M Simpson int flags) 2373acd9a301SAlan Cox { 2374acd9a301SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 2375acd9a301SAlan Cox vm_offset_t saved_start; 2376acd9a301SAlan Cox unsigned int last_timestamp; 2377acd9a301SAlan Cox int rv; 2378abd498aaSBruce M Simpson boolean_t need_wakeup, result, user_unwire; 2379acd9a301SAlan Cox 238079e9451fSKonstantin Belousov if (start == end) 238179e9451fSKonstantin Belousov return (KERN_SUCCESS); 2382abd498aaSBruce M Simpson user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 2383acd9a301SAlan Cox vm_map_lock(map); 2384acd9a301SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 2385acd9a301SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 2386abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 2387cbef13d8SAlan Cox first_entry = first_entry->next; 2388abd498aaSBruce M Simpson else { 2389acd9a301SAlan Cox vm_map_unlock(map); 2390acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 2391acd9a301SAlan Cox } 2392abd498aaSBruce M Simpson } 2393acd9a301SAlan Cox last_timestamp = map->timestamp; 2394acd9a301SAlan Cox entry = first_entry; 2395acd9a301SAlan Cox while (entry != &map->header && entry->start < end) { 2396acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2397acd9a301SAlan Cox /* 2398acd9a301SAlan Cox * We have not yet clipped the entry. 2399acd9a301SAlan Cox */ 2400acd9a301SAlan Cox saved_start = (start >= entry->start) ? start : 2401acd9a301SAlan Cox entry->start; 2402acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 24038ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 2404acd9a301SAlan Cox /* 2405acd9a301SAlan Cox * Allow interruption of user unwiring? 2406acd9a301SAlan Cox */ 2407acd9a301SAlan Cox } 2408acd9a301SAlan Cox vm_map_lock(map); 2409acd9a301SAlan Cox if (last_timestamp+1 != map->timestamp) { 2410acd9a301SAlan Cox /* 2411acd9a301SAlan Cox * Look again for the entry because the map was 2412acd9a301SAlan Cox * modified while it was unlocked. 2413acd9a301SAlan Cox * Specifically, the entry may have been 2414acd9a301SAlan Cox * clipped, merged, or deleted. 2415acd9a301SAlan Cox */ 2416acd9a301SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 2417acd9a301SAlan Cox &tmp_entry)) { 2418cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2419cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2420cbef13d8SAlan Cox else { 2421acd9a301SAlan Cox if (saved_start == start) { 2422acd9a301SAlan Cox /* 2423acd9a301SAlan Cox * First_entry has been deleted. 2424acd9a301SAlan Cox */ 2425acd9a301SAlan Cox vm_map_unlock(map); 2426acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 2427acd9a301SAlan Cox } 2428acd9a301SAlan Cox end = saved_start; 2429acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 2430acd9a301SAlan Cox goto done; 2431acd9a301SAlan Cox } 2432cbef13d8SAlan Cox } 2433acd9a301SAlan Cox if (entry == first_entry) 2434acd9a301SAlan Cox first_entry = tmp_entry; 2435acd9a301SAlan Cox else 2436acd9a301SAlan Cox first_entry = NULL; 2437acd9a301SAlan Cox entry = tmp_entry; 2438acd9a301SAlan Cox } 2439acd9a301SAlan Cox last_timestamp = map->timestamp; 2440acd9a301SAlan Cox continue; 2441acd9a301SAlan Cox } 2442acd9a301SAlan Cox vm_map_clip_start(map, entry, start); 2443acd9a301SAlan Cox vm_map_clip_end(map, entry, end); 2444acd9a301SAlan Cox /* 2445acd9a301SAlan Cox * Mark the entry in case the map lock is released. (See 2446acd9a301SAlan Cox * above.) 2447acd9a301SAlan Cox */ 2448ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2449ff3ae454SKonstantin Belousov entry->wiring_thread == NULL, 2450ff3ae454SKonstantin Belousov ("owned map entry %p", entry)); 2451acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 24520acea7dfSKonstantin Belousov entry->wiring_thread = curthread; 2453acd9a301SAlan Cox /* 2454acd9a301SAlan Cox * Check the map for holes in the specified region. 2455abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2456acd9a301SAlan Cox */ 2457abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 2458abd498aaSBruce M Simpson (entry->end < end && (entry->next == &map->header || 2459abd498aaSBruce M Simpson entry->next->start > entry->end))) { 2460acd9a301SAlan Cox end = entry->end; 2461acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 2462acd9a301SAlan Cox goto done; 2463acd9a301SAlan Cox } 2464acd9a301SAlan Cox /* 24653ffbc0cdSAlan Cox * If system unwiring, require that the entry is system wired. 2466acd9a301SAlan Cox */ 24670ada205eSBrian Feldman if (!user_unwire && 24680ada205eSBrian Feldman vm_map_entry_system_wired_count(entry) == 0) { 2469acd9a301SAlan Cox end = entry->end; 2470acd9a301SAlan Cox rv = KERN_INVALID_ARGUMENT; 2471acd9a301SAlan Cox goto done; 2472acd9a301SAlan Cox } 2473acd9a301SAlan Cox entry = entry->next; 2474acd9a301SAlan Cox } 2475acd9a301SAlan Cox rv = KERN_SUCCESS; 2476acd9a301SAlan Cox done: 2477e27e17b7SAlan Cox need_wakeup = FALSE; 2478acd9a301SAlan Cox if (first_entry == NULL) { 2479acd9a301SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2480cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2481cbef13d8SAlan Cox first_entry = first_entry->next; 2482cbef13d8SAlan Cox else 2483acd9a301SAlan Cox KASSERT(result, ("vm_map_unwire: lookup failed")); 2484acd9a301SAlan Cox } 24850acea7dfSKonstantin Belousov for (entry = first_entry; entry != &map->header && entry->start < end; 24860acea7dfSKonstantin Belousov entry = entry->next) { 24870acea7dfSKonstantin Belousov /* 24880acea7dfSKonstantin Belousov * If VM_MAP_WIRE_HOLESOK was specified, an empty 24890acea7dfSKonstantin Belousov * space in the unwired region could have been mapped 24900acea7dfSKonstantin Belousov * while the map lock was dropped for draining 24910acea7dfSKonstantin Belousov * MAP_ENTRY_IN_TRANSITION. Moreover, another thread 24920acea7dfSKonstantin Belousov * could be simultaneously wiring this new mapping 24930acea7dfSKonstantin Belousov * entry. Detect these cases and skip any entries 24940acea7dfSKonstantin Belousov * marked as in transition by us. 24950acea7dfSKonstantin Belousov */ 24960acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 24970acea7dfSKonstantin Belousov entry->wiring_thread != curthread) { 24980acea7dfSKonstantin Belousov KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 24990acea7dfSKonstantin Belousov ("vm_map_unwire: !HOLESOK and new/changed entry")); 25000acea7dfSKonstantin Belousov continue; 25010acea7dfSKonstantin Belousov } 25020acea7dfSKonstantin Belousov 25033ffbc0cdSAlan Cox if (rv == KERN_SUCCESS && (!user_unwire || 25043ffbc0cdSAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED))) { 2505b2f3846aSAlan Cox if (user_unwire) 2506b2f3846aSAlan Cox entry->eflags &= ~MAP_ENTRY_USER_WIRED; 250703462509SAlan Cox if (entry->wired_count == 1) 250803462509SAlan Cox vm_map_entry_unwire(map, entry); 250903462509SAlan Cox else 2510b2f3846aSAlan Cox entry->wired_count--; 2511b2f3846aSAlan Cox } 25120acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 2513ff3ae454SKonstantin Belousov ("vm_map_unwire: in-transition flag missing %p", entry)); 2514ff3ae454SKonstantin Belousov KASSERT(entry->wiring_thread == curthread, 2515ff3ae454SKonstantin Belousov ("vm_map_unwire: alien wire %p", entry)); 2516acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 25170acea7dfSKonstantin Belousov entry->wiring_thread = NULL; 2518acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2519acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2520acd9a301SAlan Cox need_wakeup = TRUE; 2521acd9a301SAlan Cox } 2522acd9a301SAlan Cox vm_map_simplify_entry(map, entry); 2523acd9a301SAlan Cox } 2524acd9a301SAlan Cox vm_map_unlock(map); 2525acd9a301SAlan Cox if (need_wakeup) 2526acd9a301SAlan Cox vm_map_wakeup(map); 2527acd9a301SAlan Cox return (rv); 2528acd9a301SAlan Cox } 2529acd9a301SAlan Cox 2530acd9a301SAlan Cox /* 253166cd575bSAlan Cox * vm_map_wire_entry_failure: 253266cd575bSAlan Cox * 253366cd575bSAlan Cox * Handle a wiring failure on the given entry. 253466cd575bSAlan Cox * 253566cd575bSAlan Cox * The map should be locked. 253666cd575bSAlan Cox */ 253766cd575bSAlan Cox static void 253866cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry, 253966cd575bSAlan Cox vm_offset_t failed_addr) 254066cd575bSAlan Cox { 254166cd575bSAlan Cox 254266cd575bSAlan Cox VM_MAP_ASSERT_LOCKED(map); 254366cd575bSAlan Cox KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 && 254466cd575bSAlan Cox entry->wired_count == 1, 254566cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p isn't being wired", entry)); 254666cd575bSAlan Cox KASSERT(failed_addr < entry->end, 254766cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p was fully wired", entry)); 254866cd575bSAlan Cox 254966cd575bSAlan Cox /* 255066cd575bSAlan Cox * If any pages at the start of this entry were successfully wired, 255166cd575bSAlan Cox * then unwire them. 255266cd575bSAlan Cox */ 255366cd575bSAlan Cox if (failed_addr > entry->start) { 255466cd575bSAlan Cox pmap_unwire(map->pmap, entry->start, failed_addr); 255566cd575bSAlan Cox vm_object_unwire(entry->object.vm_object, entry->offset, 255666cd575bSAlan Cox failed_addr - entry->start, PQ_ACTIVE); 255766cd575bSAlan Cox } 255866cd575bSAlan Cox 255966cd575bSAlan Cox /* 256066cd575bSAlan Cox * Assign an out-of-range value to represent the failure to wire this 256166cd575bSAlan Cox * entry. 256266cd575bSAlan Cox */ 256366cd575bSAlan Cox entry->wired_count = -1; 256466cd575bSAlan Cox } 256566cd575bSAlan Cox 256666cd575bSAlan Cox /* 2567e27e17b7SAlan Cox * vm_map_wire: 2568e27e17b7SAlan Cox * 2569e27e17b7SAlan Cox * Implements both kernel and user wiring. 2570e27e17b7SAlan Cox */ 2571e27e17b7SAlan Cox int 2572e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2573abd498aaSBruce M Simpson int flags) 2574e27e17b7SAlan Cox { 257512d7cc84SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 257666cd575bSAlan Cox vm_offset_t faddr, saved_end, saved_start; 257712d7cc84SAlan Cox unsigned int last_timestamp; 257812d7cc84SAlan Cox int rv; 257966cd575bSAlan Cox boolean_t need_wakeup, result, user_wire; 2580e4cd31ddSJeff Roberson vm_prot_t prot; 2581e27e17b7SAlan Cox 258279e9451fSKonstantin Belousov if (start == end) 258379e9451fSKonstantin Belousov return (KERN_SUCCESS); 2584e4cd31ddSJeff Roberson prot = 0; 2585e4cd31ddSJeff Roberson if (flags & VM_MAP_WIRE_WRITE) 2586e4cd31ddSJeff Roberson prot |= VM_PROT_WRITE; 2587abd498aaSBruce M Simpson user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 258812d7cc84SAlan Cox vm_map_lock(map); 258912d7cc84SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 259012d7cc84SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 2591abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 2592cbef13d8SAlan Cox first_entry = first_entry->next; 2593abd498aaSBruce M Simpson else { 259412d7cc84SAlan Cox vm_map_unlock(map); 259512d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 259612d7cc84SAlan Cox } 2597abd498aaSBruce M Simpson } 259812d7cc84SAlan Cox last_timestamp = map->timestamp; 259912d7cc84SAlan Cox entry = first_entry; 260012d7cc84SAlan Cox while (entry != &map->header && entry->start < end) { 260112d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 260212d7cc84SAlan Cox /* 260312d7cc84SAlan Cox * We have not yet clipped the entry. 260412d7cc84SAlan Cox */ 260512d7cc84SAlan Cox saved_start = (start >= entry->start) ? start : 260612d7cc84SAlan Cox entry->start; 260712d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 26088ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 260912d7cc84SAlan Cox /* 261012d7cc84SAlan Cox * Allow interruption of user wiring? 261112d7cc84SAlan Cox */ 261212d7cc84SAlan Cox } 261312d7cc84SAlan Cox vm_map_lock(map); 261412d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 261512d7cc84SAlan Cox /* 261612d7cc84SAlan Cox * Look again for the entry because the map was 261712d7cc84SAlan Cox * modified while it was unlocked. 261812d7cc84SAlan Cox * Specifically, the entry may have been 261912d7cc84SAlan Cox * clipped, merged, or deleted. 262012d7cc84SAlan Cox */ 262112d7cc84SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 262212d7cc84SAlan Cox &tmp_entry)) { 2623cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2624cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2625cbef13d8SAlan Cox else { 262612d7cc84SAlan Cox if (saved_start == start) { 262712d7cc84SAlan Cox /* 262812d7cc84SAlan Cox * first_entry has been deleted. 262912d7cc84SAlan Cox */ 263012d7cc84SAlan Cox vm_map_unlock(map); 263112d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 263212d7cc84SAlan Cox } 263312d7cc84SAlan Cox end = saved_start; 263412d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 263512d7cc84SAlan Cox goto done; 263612d7cc84SAlan Cox } 2637cbef13d8SAlan Cox } 263812d7cc84SAlan Cox if (entry == first_entry) 263912d7cc84SAlan Cox first_entry = tmp_entry; 264012d7cc84SAlan Cox else 264112d7cc84SAlan Cox first_entry = NULL; 264212d7cc84SAlan Cox entry = tmp_entry; 264312d7cc84SAlan Cox } 264412d7cc84SAlan Cox last_timestamp = map->timestamp; 264512d7cc84SAlan Cox continue; 264612d7cc84SAlan Cox } 264712d7cc84SAlan Cox vm_map_clip_start(map, entry, start); 264812d7cc84SAlan Cox vm_map_clip_end(map, entry, end); 264912d7cc84SAlan Cox /* 265012d7cc84SAlan Cox * Mark the entry in case the map lock is released. (See 265112d7cc84SAlan Cox * above.) 265212d7cc84SAlan Cox */ 2653ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2654ff3ae454SKonstantin Belousov entry->wiring_thread == NULL, 2655ff3ae454SKonstantin Belousov ("owned map entry %p", entry)); 265612d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 26570acea7dfSKonstantin Belousov entry->wiring_thread = curthread; 2658e4cd31ddSJeff Roberson if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 2659e4cd31ddSJeff Roberson || (entry->protection & prot) != prot) { 2660529ab57bSKonstantin Belousov entry->eflags |= MAP_ENTRY_WIRE_SKIPPED; 26616d7e8091SKonstantin Belousov if ((flags & VM_MAP_WIRE_HOLESOK) == 0) { 26626d7e8091SKonstantin Belousov end = entry->end; 26636d7e8091SKonstantin Belousov rv = KERN_INVALID_ADDRESS; 26646d7e8091SKonstantin Belousov goto done; 26656d7e8091SKonstantin Belousov } 26666d7e8091SKonstantin Belousov goto next_entry; 26676d7e8091SKonstantin Belousov } 2668e4cd31ddSJeff Roberson if (entry->wired_count == 0) { 26690ada205eSBrian Feldman entry->wired_count++; 267012d7cc84SAlan Cox saved_start = entry->start; 267112d7cc84SAlan Cox saved_end = entry->end; 267266cd575bSAlan Cox 267312d7cc84SAlan Cox /* 267412d7cc84SAlan Cox * Release the map lock, relying on the in-transition 2675a5db445dSMax Laier * mark. Mark the map busy for fork. 267612d7cc84SAlan Cox */ 2677a5db445dSMax Laier vm_map_busy(map); 267812d7cc84SAlan Cox vm_map_unlock(map); 267966cd575bSAlan Cox 26800b695684SAlan Cox faddr = saved_start; 26810b695684SAlan Cox do { 268266cd575bSAlan Cox /* 268366cd575bSAlan Cox * Simulate a fault to get the page and enter 268466cd575bSAlan Cox * it into the physical map. 268566cd575bSAlan Cox */ 268666cd575bSAlan Cox if ((rv = vm_fault(map, faddr, VM_PROT_NONE, 26876a875bf9SKonstantin Belousov VM_FAULT_WIRE)) != KERN_SUCCESS) 268866cd575bSAlan Cox break; 26890b695684SAlan Cox } while ((faddr += PAGE_SIZE) < saved_end); 269012d7cc84SAlan Cox vm_map_lock(map); 2691a5db445dSMax Laier vm_map_unbusy(map); 269212d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 269312d7cc84SAlan Cox /* 269412d7cc84SAlan Cox * Look again for the entry because the map was 269512d7cc84SAlan Cox * modified while it was unlocked. The entry 269612d7cc84SAlan Cox * may have been clipped, but NOT merged or 269712d7cc84SAlan Cox * deleted. 269812d7cc84SAlan Cox */ 269912d7cc84SAlan Cox result = vm_map_lookup_entry(map, saved_start, 270012d7cc84SAlan Cox &tmp_entry); 270112d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 270212d7cc84SAlan Cox if (entry == first_entry) 270312d7cc84SAlan Cox first_entry = tmp_entry; 270412d7cc84SAlan Cox else 270512d7cc84SAlan Cox first_entry = NULL; 270612d7cc84SAlan Cox entry = tmp_entry; 270728c58286SAlan Cox while (entry->end < saved_end) { 270866cd575bSAlan Cox /* 270966cd575bSAlan Cox * In case of failure, handle entries 271066cd575bSAlan Cox * that were not fully wired here; 271166cd575bSAlan Cox * fully wired entries are handled 271266cd575bSAlan Cox * later. 271366cd575bSAlan Cox */ 271466cd575bSAlan Cox if (rv != KERN_SUCCESS && 271566cd575bSAlan Cox faddr < entry->end) 271666cd575bSAlan Cox vm_map_wire_entry_failure(map, 271766cd575bSAlan Cox entry, faddr); 271812d7cc84SAlan Cox entry = entry->next; 271912d7cc84SAlan Cox } 272028c58286SAlan Cox } 272112d7cc84SAlan Cox last_timestamp = map->timestamp; 272212d7cc84SAlan Cox if (rv != KERN_SUCCESS) { 272366cd575bSAlan Cox vm_map_wire_entry_failure(map, entry, faddr); 272412d7cc84SAlan Cox end = entry->end; 272512d7cc84SAlan Cox goto done; 272612d7cc84SAlan Cox } 27270ada205eSBrian Feldman } else if (!user_wire || 27280ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 27290ada205eSBrian Feldman entry->wired_count++; 273012d7cc84SAlan Cox } 273112d7cc84SAlan Cox /* 273212d7cc84SAlan Cox * Check the map for holes in the specified region. 2733abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 273412d7cc84SAlan Cox */ 27356d7e8091SKonstantin Belousov next_entry: 2736f141ed73SKonstantin Belousov if ((flags & VM_MAP_WIRE_HOLESOK) == 0 && 2737f141ed73SKonstantin Belousov entry->end < end && (entry->next == &map->header || 2738f141ed73SKonstantin Belousov entry->next->start > entry->end)) { 273912d7cc84SAlan Cox end = entry->end; 274012d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 274112d7cc84SAlan Cox goto done; 274212d7cc84SAlan Cox } 274312d7cc84SAlan Cox entry = entry->next; 274412d7cc84SAlan Cox } 274512d7cc84SAlan Cox rv = KERN_SUCCESS; 274612d7cc84SAlan Cox done: 274712d7cc84SAlan Cox need_wakeup = FALSE; 274812d7cc84SAlan Cox if (first_entry == NULL) { 274912d7cc84SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2750cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2751cbef13d8SAlan Cox first_entry = first_entry->next; 2752cbef13d8SAlan Cox else 275312d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 275412d7cc84SAlan Cox } 27550acea7dfSKonstantin Belousov for (entry = first_entry; entry != &map->header && entry->start < end; 27560acea7dfSKonstantin Belousov entry = entry->next) { 27570acea7dfSKonstantin Belousov /* 27580acea7dfSKonstantin Belousov * If VM_MAP_WIRE_HOLESOK was specified, an empty 27590acea7dfSKonstantin Belousov * space in the unwired region could have been mapped 27600acea7dfSKonstantin Belousov * while the map lock was dropped for faulting in the 27610acea7dfSKonstantin Belousov * pages or draining MAP_ENTRY_IN_TRANSITION. 27620acea7dfSKonstantin Belousov * Moreover, another thread could be simultaneously 27630acea7dfSKonstantin Belousov * wiring this new mapping entry. Detect these cases 2764546bb2d7SKonstantin Belousov * and skip any entries marked as in transition not by us. 27650acea7dfSKonstantin Belousov */ 27660acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 27670acea7dfSKonstantin Belousov entry->wiring_thread != curthread) { 27680acea7dfSKonstantin Belousov KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 27690acea7dfSKonstantin Belousov ("vm_map_wire: !HOLESOK and new/changed entry")); 27700acea7dfSKonstantin Belousov continue; 27710acea7dfSKonstantin Belousov } 27720acea7dfSKonstantin Belousov 2773546bb2d7SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) 2774546bb2d7SKonstantin Belousov goto next_entry_done; 2775546bb2d7SKonstantin Belousov 277612d7cc84SAlan Cox if (rv == KERN_SUCCESS) { 277712d7cc84SAlan Cox if (user_wire) 277812d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_USER_WIRED; 277928c58286SAlan Cox } else if (entry->wired_count == -1) { 278028c58286SAlan Cox /* 278128c58286SAlan Cox * Wiring failed on this entry. Thus, unwiring is 278228c58286SAlan Cox * unnecessary. 278328c58286SAlan Cox */ 278428c58286SAlan Cox entry->wired_count = 0; 278503462509SAlan Cox } else if (!user_wire || 278603462509SAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 278766cd575bSAlan Cox /* 278866cd575bSAlan Cox * Undo the wiring. Wiring succeeded on this entry 278966cd575bSAlan Cox * but failed on a later entry. 279066cd575bSAlan Cox */ 279103462509SAlan Cox if (entry->wired_count == 1) 279203462509SAlan Cox vm_map_entry_unwire(map, entry); 279303462509SAlan Cox else 279412d7cc84SAlan Cox entry->wired_count--; 279512d7cc84SAlan Cox } 27966d7e8091SKonstantin Belousov next_entry_done: 27970acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 27980acea7dfSKonstantin Belousov ("vm_map_wire: in-transition flag missing %p", entry)); 27990acea7dfSKonstantin Belousov KASSERT(entry->wiring_thread == curthread, 28000acea7dfSKonstantin Belousov ("vm_map_wire: alien wire %p", entry)); 28010acea7dfSKonstantin Belousov entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION | 28020acea7dfSKonstantin Belousov MAP_ENTRY_WIRE_SKIPPED); 28030acea7dfSKonstantin Belousov entry->wiring_thread = NULL; 280412d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 280512d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 280612d7cc84SAlan Cox need_wakeup = TRUE; 280712d7cc84SAlan Cox } 280812d7cc84SAlan Cox vm_map_simplify_entry(map, entry); 280912d7cc84SAlan Cox } 281012d7cc84SAlan Cox vm_map_unlock(map); 281112d7cc84SAlan Cox if (need_wakeup) 281212d7cc84SAlan Cox vm_map_wakeup(map); 281312d7cc84SAlan Cox return (rv); 2814e27e17b7SAlan Cox } 2815e27e17b7SAlan Cox 2816e27e17b7SAlan Cox /* 2817950f8459SAlan Cox * vm_map_sync 2818df8bae1dSRodney W. Grimes * 2819df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 2820df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 2821df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 2822df8bae1dSRodney W. Grimes * 2823637315edSAlan Cox * If the size of the region from start to end is zero, we are 2824637315edSAlan Cox * supposed to flush all modified pages within the region containing 2825637315edSAlan Cox * start. Unfortunately, a region can be split or coalesced with 2826637315edSAlan Cox * neighboring regions, making it difficult to determine what the 2827637315edSAlan Cox * original region was. Therefore, we approximate this requirement by 2828637315edSAlan Cox * flushing the current region containing start. 2829637315edSAlan Cox * 2830df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 2831df8bae1dSRodney W. Grimes */ 2832df8bae1dSRodney W. Grimes int 2833950f8459SAlan Cox vm_map_sync( 28341b40f8c0SMatthew Dillon vm_map_t map, 28351b40f8c0SMatthew Dillon vm_offset_t start, 28361b40f8c0SMatthew Dillon vm_offset_t end, 28371b40f8c0SMatthew Dillon boolean_t syncio, 28381b40f8c0SMatthew Dillon boolean_t invalidate) 2839df8bae1dSRodney W. Grimes { 2840c0877f10SJohn Dyson vm_map_entry_t current; 2841df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2842df8bae1dSRodney W. Grimes vm_size_t size; 2843df8bae1dSRodney W. Grimes vm_object_t object; 2844a316d390SJohn Dyson vm_ooffset_t offset; 2845e53fa61bSKonstantin Belousov unsigned int last_timestamp; 2846126d6082SKonstantin Belousov boolean_t failed; 2847df8bae1dSRodney W. Grimes 2848df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2849df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2850df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 2851df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2852df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2853637315edSAlan Cox } else if (start == end) { 2854637315edSAlan Cox start = entry->start; 2855637315edSAlan Cox end = entry->end; 2856df8bae1dSRodney W. Grimes } 2857df8bae1dSRodney W. Grimes /* 2858b7b7cd44SAlan Cox * Make a first pass to check for user-wired memory and holes. 2859df8bae1dSRodney W. Grimes */ 28607b0e72d1SAlan Cox for (current = entry; current != &map->header && current->start < end; 28617b0e72d1SAlan Cox current = current->next) { 2862b7b7cd44SAlan Cox if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 2863df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2864df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2865df8bae1dSRodney W. Grimes } 2866df8bae1dSRodney W. Grimes if (end > current->end && 2867df8bae1dSRodney W. Grimes (current->next == &map->header || 2868df8bae1dSRodney W. Grimes current->end != current->next->start)) { 2869df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2870df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 2871df8bae1dSRodney W. Grimes } 2872df8bae1dSRodney W. Grimes } 2873df8bae1dSRodney W. Grimes 28742cf13952SAlan Cox if (invalidate) 2875bc105a67SAlan Cox pmap_remove(map->pmap, start, end); 2876126d6082SKonstantin Belousov failed = FALSE; 28772cf13952SAlan Cox 2878df8bae1dSRodney W. Grimes /* 2879df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 2880df8bae1dSRodney W. Grimes * objects as we go. 2881df8bae1dSRodney W. Grimes */ 2882e53fa61bSKonstantin Belousov for (current = entry; current != &map->header && current->start < end;) { 2883df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 2884df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 28859fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2886c0877f10SJohn Dyson vm_map_t smap; 2887df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 2888df8bae1dSRodney W. Grimes vm_size_t tsize; 2889df8bae1dSRodney W. Grimes 28909fdfe602SMatthew Dillon smap = current->object.sub_map; 2891df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 2892df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 2893df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 2894df8bae1dSRodney W. Grimes if (tsize < size) 2895df8bae1dSRodney W. Grimes size = tsize; 2896df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 2897df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 2898df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 2899df8bae1dSRodney W. Grimes } else { 2900df8bae1dSRodney W. Grimes object = current->object.vm_object; 2901df8bae1dSRodney W. Grimes } 2902e53fa61bSKonstantin Belousov vm_object_reference(object); 2903e53fa61bSKonstantin Belousov last_timestamp = map->timestamp; 2904e53fa61bSKonstantin Belousov vm_map_unlock_read(map); 2905126d6082SKonstantin Belousov if (!vm_object_sync(object, offset, size, syncio, invalidate)) 2906126d6082SKonstantin Belousov failed = TRUE; 2907df8bae1dSRodney W. Grimes start += size; 2908e53fa61bSKonstantin Belousov vm_object_deallocate(object); 2909e53fa61bSKonstantin Belousov vm_map_lock_read(map); 2910e53fa61bSKonstantin Belousov if (last_timestamp == map->timestamp || 2911e53fa61bSKonstantin Belousov !vm_map_lookup_entry(map, start, ¤t)) 2912e53fa61bSKonstantin Belousov current = current->next; 2913df8bae1dSRodney W. Grimes } 2914df8bae1dSRodney W. Grimes 2915df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2916126d6082SKonstantin Belousov return (failed ? KERN_FAILURE : KERN_SUCCESS); 2917df8bae1dSRodney W. Grimes } 2918df8bae1dSRodney W. Grimes 2919df8bae1dSRodney W. Grimes /* 2920df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 2921df8bae1dSRodney W. Grimes * 2922df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 2923df8bae1dSRodney W. Grimes * 2924df8bae1dSRodney W. Grimes * The map in question should be locked. 2925df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 2926df8bae1dSRodney W. Grimes */ 29270362d7d7SJohn Dyson static void 29281b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2929df8bae1dSRodney W. Grimes { 293003462509SAlan Cox 293103462509SAlan Cox VM_MAP_ASSERT_LOCKED(map); 293203462509SAlan Cox KASSERT(entry->wired_count > 0, 293303462509SAlan Cox ("vm_map_entry_unwire: entry %p isn't wired", entry)); 293403462509SAlan Cox pmap_unwire(map->pmap, entry->start, entry->end); 293503462509SAlan Cox vm_object_unwire(entry->object.vm_object, entry->offset, entry->end - 293603462509SAlan Cox entry->start, PQ_ACTIVE); 2937df8bae1dSRodney W. Grimes entry->wired_count = 0; 2938df8bae1dSRodney W. Grimes } 2939df8bae1dSRodney W. Grimes 29400b367bd8SKonstantin Belousov static void 29410b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map) 29420b367bd8SKonstantin Belousov { 29430b367bd8SKonstantin Belousov 29440b367bd8SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) 29450b367bd8SKonstantin Belousov vm_object_deallocate(entry->object.vm_object); 29460b367bd8SKonstantin Belousov uma_zfree(system_map ? kmapentzone : mapentzone, entry); 29470b367bd8SKonstantin Belousov } 29480b367bd8SKonstantin Belousov 2949df8bae1dSRodney W. Grimes /* 2950df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 2951df8bae1dSRodney W. Grimes * 2952df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 2953df8bae1dSRodney W. Grimes */ 29540362d7d7SJohn Dyson static void 29551b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2956df8bae1dSRodney W. Grimes { 295732a89c32SAlan Cox vm_object_t object; 29583364c323SKonstantin Belousov vm_pindex_t offidxstart, offidxend, count, size1; 2959d1780e8dSKonstantin Belousov vm_size_t size; 296032a89c32SAlan Cox 2961df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 29623364c323SKonstantin Belousov object = entry->object.vm_object; 296319bd0d9cSKonstantin Belousov 296419bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) { 296519bd0d9cSKonstantin Belousov MPASS(entry->cred == NULL); 296619bd0d9cSKonstantin Belousov MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0); 296719bd0d9cSKonstantin Belousov MPASS(object == NULL); 296819bd0d9cSKonstantin Belousov vm_map_entry_deallocate(entry, map->system_map); 296919bd0d9cSKonstantin Belousov return; 297019bd0d9cSKonstantin Belousov } 297119bd0d9cSKonstantin Belousov 29723364c323SKonstantin Belousov size = entry->end - entry->start; 29733364c323SKonstantin Belousov map->size -= size; 29743364c323SKonstantin Belousov 2975ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 2976ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred); 2977ef694c1aSEdward Tomasz Napierala crfree(entry->cred); 29783364c323SKonstantin Belousov } 2979df8bae1dSRodney W. Grimes 298032a89c32SAlan Cox if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 29813364c323SKonstantin Belousov (object != NULL)) { 2982ef694c1aSEdward Tomasz Napierala KASSERT(entry->cred == NULL || object->cred == NULL || 29833364c323SKonstantin Belousov (entry->eflags & MAP_ENTRY_NEEDS_COPY), 2984ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry)); 2985d1780e8dSKonstantin Belousov count = atop(size); 298632a89c32SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 298732a89c32SAlan Cox offidxend = offidxstart + count; 298889f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 29899a4ee196SKonstantin Belousov if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT | 29909a4ee196SKonstantin Belousov OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 29919f5c801bSAlan Cox object == kernel_object || object == kmem_object)) { 299232a89c32SAlan Cox vm_object_collapse(object); 29936bbee8e2SAlan Cox 29946bbee8e2SAlan Cox /* 29956bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED can be passed here 29966bbee8e2SAlan Cox * because vm_map_delete() already performed 29976bbee8e2SAlan Cox * pmap_remove() on the only mapping to this range 29986bbee8e2SAlan Cox * of pages. 29996bbee8e2SAlan Cox */ 30006bbee8e2SAlan Cox vm_object_page_remove(object, offidxstart, offidxend, 30016bbee8e2SAlan Cox OBJPR_NOTMAPPED); 300232a89c32SAlan Cox if (object->type == OBJT_SWAP) 30039a4ee196SKonstantin Belousov swap_pager_freespace(object, offidxstart, 30049a4ee196SKonstantin Belousov count); 300532a89c32SAlan Cox if (offidxend >= object->size && 30063364c323SKonstantin Belousov offidxstart < object->size) { 30073364c323SKonstantin Belousov size1 = object->size; 300832a89c32SAlan Cox object->size = offidxstart; 3009ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 30103364c323SKonstantin Belousov size1 -= object->size; 30113364c323SKonstantin Belousov KASSERT(object->charge >= ptoa(size1), 30129a4ee196SKonstantin Belousov ("object %p charge < 0", object)); 30139a4ee196SKonstantin Belousov swap_release_by_cred(ptoa(size1), 30149a4ee196SKonstantin Belousov object->cred); 30153364c323SKonstantin Belousov object->charge -= ptoa(size1); 30163364c323SKonstantin Belousov } 30173364c323SKonstantin Belousov } 301832a89c32SAlan Cox } 301989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3020897d81a0SKonstantin Belousov } else 3021897d81a0SKonstantin Belousov entry->object.vm_object = NULL; 30220b367bd8SKonstantin Belousov if (map->system_map) 30230b367bd8SKonstantin Belousov vm_map_entry_deallocate(entry, TRUE); 30240b367bd8SKonstantin Belousov else { 30250b367bd8SKonstantin Belousov entry->next = curthread->td_map_def_user; 30260b367bd8SKonstantin Belousov curthread->td_map_def_user = entry; 30270b367bd8SKonstantin Belousov } 3028df8bae1dSRodney W. Grimes } 3029df8bae1dSRodney W. Grimes 3030df8bae1dSRodney W. Grimes /* 3031df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 3032df8bae1dSRodney W. Grimes * 3033df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 3034df8bae1dSRodney W. Grimes * map. 3035df8bae1dSRodney W. Grimes */ 3036df8bae1dSRodney W. Grimes int 3037655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 3038df8bae1dSRodney W. Grimes { 3039c0877f10SJohn Dyson vm_map_entry_t entry; 3040df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 3041df8bae1dSRodney W. Grimes 30423a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 304379e9451fSKonstantin Belousov if (start == end) 304479e9451fSKonstantin Belousov return (KERN_SUCCESS); 30453a0916b8SKonstantin Belousov 3046df8bae1dSRodney W. Grimes /* 3047df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 3048df8bae1dSRodney W. Grimes */ 3049876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 3050df8bae1dSRodney W. Grimes entry = first_entry->next; 3051876318ecSAlan Cox else { 3052df8bae1dSRodney W. Grimes entry = first_entry; 3053df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 3054df8bae1dSRodney W. Grimes } 3055df8bae1dSRodney W. Grimes 3056df8bae1dSRodney W. Grimes /* 3057df8bae1dSRodney W. Grimes * Step through all entries in this region 3058df8bae1dSRodney W. Grimes */ 3059df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 3060df8bae1dSRodney W. Grimes vm_map_entry_t next; 3061df8bae1dSRodney W. Grimes 306273b2baceSAlan Cox /* 306373b2baceSAlan Cox * Wait for wiring or unwiring of an entry to complete. 30647c938963SBrian Feldman * Also wait for any system wirings to disappear on 30657c938963SBrian Feldman * user maps. 306673b2baceSAlan Cox */ 30677c938963SBrian Feldman if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 30687c938963SBrian Feldman (vm_map_pmap(map) != kernel_pmap && 30697c938963SBrian Feldman vm_map_entry_system_wired_count(entry) != 0)) { 307073b2baceSAlan Cox unsigned int last_timestamp; 307173b2baceSAlan Cox vm_offset_t saved_start; 307273b2baceSAlan Cox vm_map_entry_t tmp_entry; 307373b2baceSAlan Cox 307473b2baceSAlan Cox saved_start = entry->start; 307573b2baceSAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 307673b2baceSAlan Cox last_timestamp = map->timestamp; 30778ce2d00aSPawel Jakub Dawidek (void) vm_map_unlock_and_wait(map, 0); 307873b2baceSAlan Cox vm_map_lock(map); 307973b2baceSAlan Cox if (last_timestamp + 1 != map->timestamp) { 308073b2baceSAlan Cox /* 308173b2baceSAlan Cox * Look again for the entry because the map was 308273b2baceSAlan Cox * modified while it was unlocked. 308373b2baceSAlan Cox * Specifically, the entry may have been 308473b2baceSAlan Cox * clipped, merged, or deleted. 308573b2baceSAlan Cox */ 308673b2baceSAlan Cox if (!vm_map_lookup_entry(map, saved_start, 308773b2baceSAlan Cox &tmp_entry)) 308873b2baceSAlan Cox entry = tmp_entry->next; 308973b2baceSAlan Cox else { 309073b2baceSAlan Cox entry = tmp_entry; 309173b2baceSAlan Cox vm_map_clip_start(map, entry, 309273b2baceSAlan Cox saved_start); 309373b2baceSAlan Cox } 309473b2baceSAlan Cox } 309573b2baceSAlan Cox continue; 309673b2baceSAlan Cox } 3097df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 3098df8bae1dSRodney W. Grimes 3099c0877f10SJohn Dyson next = entry->next; 3100df8bae1dSRodney W. Grimes 3101df8bae1dSRodney W. Grimes /* 31020d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 31030d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 3104df8bae1dSRodney W. Grimes */ 3105c0877f10SJohn Dyson if (entry->wired_count != 0) { 3106df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 3107c0877f10SJohn Dyson } 3108df8bae1dSRodney W. Grimes 310932a89c32SAlan Cox pmap_remove(map->pmap, entry->start, entry->end); 3110df8bae1dSRodney W. Grimes 3111df8bae1dSRodney W. Grimes /* 3112e608cc3cSKonstantin Belousov * Delete the entry only after removing all pmap 3113e608cc3cSKonstantin Belousov * entries pointing to its pages. (Otherwise, its 3114e608cc3cSKonstantin Belousov * page frames may be reallocated, and any modify bits 3115e608cc3cSKonstantin Belousov * will be set in the wrong object!) 3116df8bae1dSRodney W. Grimes */ 3117df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 3118df8bae1dSRodney W. Grimes entry = next; 3119df8bae1dSRodney W. Grimes } 3120df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 3121df8bae1dSRodney W. Grimes } 3122df8bae1dSRodney W. Grimes 3123df8bae1dSRodney W. Grimes /* 3124df8bae1dSRodney W. Grimes * vm_map_remove: 3125df8bae1dSRodney W. Grimes * 3126df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 3127df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 3128df8bae1dSRodney W. Grimes */ 3129df8bae1dSRodney W. Grimes int 31301b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 3131df8bae1dSRodney W. Grimes { 31326eaee3feSAlan Cox int result; 3133df8bae1dSRodney W. Grimes 3134df8bae1dSRodney W. Grimes vm_map_lock(map); 3135df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 3136655c3490SKonstantin Belousov result = vm_map_delete(map, start, end); 3137df8bae1dSRodney W. Grimes vm_map_unlock(map); 3138df8bae1dSRodney W. Grimes return (result); 3139df8bae1dSRodney W. Grimes } 3140df8bae1dSRodney W. Grimes 3141df8bae1dSRodney W. Grimes /* 3142df8bae1dSRodney W. Grimes * vm_map_check_protection: 3143df8bae1dSRodney W. Grimes * 31442d5c7e45SMatthew Dillon * Assert that the target map allows the specified privilege on the 31452d5c7e45SMatthew Dillon * entire address region given. The entire region must be allocated. 31462d5c7e45SMatthew Dillon * 31472d5c7e45SMatthew Dillon * WARNING! This code does not and should not check whether the 31482d5c7e45SMatthew Dillon * contents of the region is accessible. For example a smaller file 31492d5c7e45SMatthew Dillon * might be mapped into a larger address space. 31502d5c7e45SMatthew Dillon * 31512d5c7e45SMatthew Dillon * NOTE! This code is also called by munmap(). 3152d8834602SAlan Cox * 3153d8834602SAlan Cox * The map must be locked. A read lock is sufficient. 3154df8bae1dSRodney W. Grimes */ 31550d94caffSDavid Greenman boolean_t 3156b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 3157b9dcd593SBruce Evans vm_prot_t protection) 3158df8bae1dSRodney W. Grimes { 3159c0877f10SJohn Dyson vm_map_entry_t entry; 3160df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 3161df8bae1dSRodney W. Grimes 3162d8834602SAlan Cox if (!vm_map_lookup_entry(map, start, &tmp_entry)) 3163df8bae1dSRodney W. Grimes return (FALSE); 3164df8bae1dSRodney W. Grimes entry = tmp_entry; 3165df8bae1dSRodney W. Grimes 3166df8bae1dSRodney W. Grimes while (start < end) { 3167d8834602SAlan Cox if (entry == &map->header) 3168df8bae1dSRodney W. Grimes return (FALSE); 3169df8bae1dSRodney W. Grimes /* 3170df8bae1dSRodney W. Grimes * No holes allowed! 3171df8bae1dSRodney W. Grimes */ 3172d8834602SAlan Cox if (start < entry->start) 3173df8bae1dSRodney W. Grimes return (FALSE); 3174df8bae1dSRodney W. Grimes /* 3175df8bae1dSRodney W. Grimes * Check protection associated with entry. 3176df8bae1dSRodney W. Grimes */ 3177d8834602SAlan Cox if ((entry->protection & protection) != protection) 3178df8bae1dSRodney W. Grimes return (FALSE); 3179df8bae1dSRodney W. Grimes /* go to next entry */ 3180df8bae1dSRodney W. Grimes start = entry->end; 3181df8bae1dSRodney W. Grimes entry = entry->next; 3182df8bae1dSRodney W. Grimes } 3183df8bae1dSRodney W. Grimes return (TRUE); 3184df8bae1dSRodney W. Grimes } 3185df8bae1dSRodney W. Grimes 318686524867SJohn Dyson /* 3187df8bae1dSRodney W. Grimes * vm_map_copy_entry: 3188df8bae1dSRodney W. Grimes * 3189df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 3190df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 3191df8bae1dSRodney W. Grimes */ 3192f708ef1bSPoul-Henning Kamp static void 31931b40f8c0SMatthew Dillon vm_map_copy_entry( 31941b40f8c0SMatthew Dillon vm_map_t src_map, 31951b40f8c0SMatthew Dillon vm_map_t dst_map, 31961b40f8c0SMatthew Dillon vm_map_entry_t src_entry, 31973364c323SKonstantin Belousov vm_map_entry_t dst_entry, 31983364c323SKonstantin Belousov vm_ooffset_t *fork_charge) 3199df8bae1dSRodney W. Grimes { 3200c0877f10SJohn Dyson vm_object_t src_object; 320184110e7eSKonstantin Belousov vm_map_entry_t fake_entry; 32023364c323SKonstantin Belousov vm_offset_t size; 3203ef694c1aSEdward Tomasz Napierala struct ucred *cred; 32043364c323SKonstantin Belousov int charged; 3205c0877f10SJohn Dyson 32063a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(dst_map); 32073a0916b8SKonstantin Belousov 32089fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 3209df8bae1dSRodney W. Grimes return; 3210df8bae1dSRodney W. Grimes 3211afaa41f6SAlan Cox if (src_entry->wired_count == 0 || 3212afaa41f6SAlan Cox (src_entry->protection & VM_PROT_WRITE) == 0) { 3213df8bae1dSRodney W. Grimes /* 32140d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 32150d94caffSDavid Greenman * write-protected. 3216df8bae1dSRodney W. Grimes */ 3217d9a9209aSAlan Cox if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 && 3218d9a9209aSAlan Cox (src_entry->protection & VM_PROT_WRITE) != 0) { 3219df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 3220df8bae1dSRodney W. Grimes src_entry->start, 3221df8bae1dSRodney W. Grimes src_entry->end, 3222df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 3223df8bae1dSRodney W. Grimes } 3224b18bfc3dSJohn Dyson 3225df8bae1dSRodney W. Grimes /* 3226df8bae1dSRodney W. Grimes * Make a copy of the object. 3227df8bae1dSRodney W. Grimes */ 32283364c323SKonstantin Belousov size = src_entry->end - src_entry->start; 32298aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 323089f6b863SAttilio Rao VM_OBJECT_WLOCK(src_object); 32313364c323SKonstantin Belousov charged = ENTRY_CHARGED(src_entry); 32329a4ee196SKonstantin Belousov if (src_object->handle == NULL && 3233c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 3234c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 3235c0877f10SJohn Dyson vm_object_collapse(src_object); 32369a4ee196SKonstantin Belousov if ((src_object->flags & (OBJ_NOSPLIT | 32379a4ee196SKonstantin Belousov OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 3238c5aaa06dSAlan Cox vm_object_split(src_entry); 32399a4ee196SKonstantin Belousov src_object = 32409a4ee196SKonstantin Belousov src_entry->object.vm_object; 3241a89c6258SAlan Cox } 3242a89c6258SAlan Cox } 3243b921a12bSAlan Cox vm_object_reference_locked(src_object); 3244069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 3245ef694c1aSEdward Tomasz Napierala if (src_entry->cred != NULL && 32463364c323SKonstantin Belousov !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) { 3247ef694c1aSEdward Tomasz Napierala KASSERT(src_object->cred == NULL, 3248ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_map_copy_entry: cred %p", 32493364c323SKonstantin Belousov src_object)); 3250ef694c1aSEdward Tomasz Napierala src_object->cred = src_entry->cred; 32513364c323SKonstantin Belousov src_object->charge = size; 32523364c323SKonstantin Belousov } 325389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(src_object); 3254c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 32553364c323SKonstantin Belousov if (charged) { 3256ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 3257ef694c1aSEdward Tomasz Napierala crhold(cred); 3258ef694c1aSEdward Tomasz Napierala dst_entry->cred = cred; 32593364c323SKonstantin Belousov *fork_charge += size; 32603364c323SKonstantin Belousov if (!(src_entry->eflags & 32613364c323SKonstantin Belousov MAP_ENTRY_NEEDS_COPY)) { 3262ef694c1aSEdward Tomasz Napierala crhold(cred); 3263ef694c1aSEdward Tomasz Napierala src_entry->cred = cred; 32643364c323SKonstantin Belousov *fork_charge += size; 32653364c323SKonstantin Belousov } 32663364c323SKonstantin Belousov } 32679a4ee196SKonstantin Belousov src_entry->eflags |= MAP_ENTRY_COW | 32689a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY; 32699a4ee196SKonstantin Belousov dst_entry->eflags |= MAP_ENTRY_COW | 32709a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY; 3271b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 327284110e7eSKonstantin Belousov if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 327384110e7eSKonstantin Belousov /* 327484110e7eSKonstantin Belousov * MAP_ENTRY_VN_WRITECNT cannot 327584110e7eSKonstantin Belousov * indicate write reference from 327684110e7eSKonstantin Belousov * src_entry, since the entry is 327784110e7eSKonstantin Belousov * marked as needs copy. Allocate a 327884110e7eSKonstantin Belousov * fake entry that is used to 327984110e7eSKonstantin Belousov * decrement object->un_pager.vnp.writecount 328084110e7eSKonstantin Belousov * at the appropriate time. Attach 328184110e7eSKonstantin Belousov * fake_entry to the deferred list. 328284110e7eSKonstantin Belousov */ 328384110e7eSKonstantin Belousov fake_entry = vm_map_entry_create(dst_map); 328484110e7eSKonstantin Belousov fake_entry->eflags = MAP_ENTRY_VN_WRITECNT; 328584110e7eSKonstantin Belousov src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT; 328684110e7eSKonstantin Belousov vm_object_reference(src_object); 328784110e7eSKonstantin Belousov fake_entry->object.vm_object = src_object; 328884110e7eSKonstantin Belousov fake_entry->start = src_entry->start; 328984110e7eSKonstantin Belousov fake_entry->end = src_entry->end; 329084110e7eSKonstantin Belousov fake_entry->next = curthread->td_map_def_user; 329184110e7eSKonstantin Belousov curthread->td_map_def_user = fake_entry; 329284110e7eSKonstantin Belousov } 32930ec97ffcSKonstantin Belousov 32940ec97ffcSKonstantin Belousov pmap_copy(dst_map->pmap, src_map->pmap, 32950ec97ffcSKonstantin Belousov dst_entry->start, dst_entry->end - dst_entry->start, 32960ec97ffcSKonstantin Belousov src_entry->start); 3297b18bfc3dSJohn Dyson } else { 3298b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 3299b18bfc3dSJohn Dyson dst_entry->offset = 0; 3300ef694c1aSEdward Tomasz Napierala if (src_entry->cred != NULL) { 3301ef694c1aSEdward Tomasz Napierala dst_entry->cred = curthread->td_ucred; 3302ef694c1aSEdward Tomasz Napierala crhold(dst_entry->cred); 33033364c323SKonstantin Belousov *fork_charge += size; 33043364c323SKonstantin Belousov } 3305b18bfc3dSJohn Dyson } 33060d94caffSDavid Greenman } else { 3307df8bae1dSRodney W. Grimes /* 3308afaa41f6SAlan Cox * We don't want to make writeable wired pages copy-on-write. 3309afaa41f6SAlan Cox * Immediately copy these pages into the new map by simulating 3310afaa41f6SAlan Cox * page faults. The new pages are pageable. 3311df8bae1dSRodney W. Grimes */ 3312121fd461SKonstantin Belousov vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry, 3313121fd461SKonstantin Belousov fork_charge); 3314df8bae1dSRodney W. Grimes } 3315df8bae1dSRodney W. Grimes } 3316df8bae1dSRodney W. Grimes 3317df8bae1dSRodney W. Grimes /* 33182a7be1b6SBrian Feldman * vmspace_map_entry_forked: 33192a7be1b6SBrian Feldman * Update the newly-forked vmspace each time a map entry is inherited 33202a7be1b6SBrian Feldman * or copied. The values for vm_dsize and vm_tsize are approximate 33212a7be1b6SBrian Feldman * (and mostly-obsolete ideas in the face of mmap(2) et al.) 33222a7be1b6SBrian Feldman */ 33232a7be1b6SBrian Feldman static void 33242a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 33252a7be1b6SBrian Feldman vm_map_entry_t entry) 33262a7be1b6SBrian Feldman { 33272a7be1b6SBrian Feldman vm_size_t entrysize; 33282a7be1b6SBrian Feldman vm_offset_t newend; 33292a7be1b6SBrian Feldman 333019bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) 333119bd0d9cSKonstantin Belousov return; 33322a7be1b6SBrian Feldman entrysize = entry->end - entry->start; 33332a7be1b6SBrian Feldman vm2->vm_map.size += entrysize; 33342a7be1b6SBrian Feldman if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 33352a7be1b6SBrian Feldman vm2->vm_ssize += btoc(entrysize); 33362a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 33372a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 3338b351299cSAndrew Gallatin newend = MIN(entry->end, 33392a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 33402a7be1b6SBrian Feldman vm2->vm_dsize += btoc(newend - entry->start); 33412a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 33422a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 3343b351299cSAndrew Gallatin newend = MIN(entry->end, 33442a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 33452a7be1b6SBrian Feldman vm2->vm_tsize += btoc(newend - entry->start); 33462a7be1b6SBrian Feldman } 33472a7be1b6SBrian Feldman } 33482a7be1b6SBrian Feldman 33492a7be1b6SBrian Feldman /* 3350df8bae1dSRodney W. Grimes * vmspace_fork: 3351df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 3352df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 3353df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 3354df8bae1dSRodney W. Grimes * values on the regions in that map. 3355df8bae1dSRodney W. Grimes * 33562a7be1b6SBrian Feldman * XXX It might be worth coalescing the entries added to the new vmspace. 33572a7be1b6SBrian Feldman * 3358df8bae1dSRodney W. Grimes * The source map must not be locked. 3359df8bae1dSRodney W. Grimes */ 3360df8bae1dSRodney W. Grimes struct vmspace * 33613364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge) 3362df8bae1dSRodney W. Grimes { 3363c0877f10SJohn Dyson struct vmspace *vm2; 336479e53838SAlan Cox vm_map_t new_map, old_map; 336579e53838SAlan Cox vm_map_entry_t new_entry, old_entry; 3366de5f6a77SJohn Dyson vm_object_t object; 33671fac7d7fSKonstantin Belousov int locked; 336819bd0d9cSKonstantin Belousov vm_inherit_t inh; 3369df8bae1dSRodney W. Grimes 337079e53838SAlan Cox old_map = &vm1->vm_map; 337179e53838SAlan Cox /* Copy immutable fields of vm1 to vm2. */ 337274d1d2b7SNeel Natu vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, NULL); 337389b57fcfSKonstantin Belousov if (vm2 == NULL) 337479e53838SAlan Cox return (NULL); 33752a7be1b6SBrian Feldman vm2->vm_taddr = vm1->vm_taddr; 33762a7be1b6SBrian Feldman vm2->vm_daddr = vm1->vm_daddr; 33772a7be1b6SBrian Feldman vm2->vm_maxsaddr = vm1->vm_maxsaddr; 337879e53838SAlan Cox vm_map_lock(old_map); 337979e53838SAlan Cox if (old_map->busy) 338079e53838SAlan Cox vm_map_wait_busy(old_map); 338179e53838SAlan Cox new_map = &vm2->vm_map; 33821fac7d7fSKonstantin Belousov locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */ 33831fac7d7fSKonstantin Belousov KASSERT(locked, ("vmspace_fork: lock failed")); 3384df8bae1dSRodney W. Grimes 3385df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 3386df8bae1dSRodney W. Grimes 3387df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 3388afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 3389df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 3390df8bae1dSRodney W. Grimes 339119bd0d9cSKonstantin Belousov inh = old_entry->inheritance; 339219bd0d9cSKonstantin Belousov if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 && 339319bd0d9cSKonstantin Belousov inh != VM_INHERIT_NONE) 339419bd0d9cSKonstantin Belousov inh = VM_INHERIT_COPY; 339519bd0d9cSKonstantin Belousov 339619bd0d9cSKonstantin Belousov switch (inh) { 3397df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 3398df8bae1dSRodney W. Grimes break; 3399df8bae1dSRodney W. Grimes 3400df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 3401df8bae1dSRodney W. Grimes /* 3402fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 3403fed9a903SJohn Dyson */ 3404fed9a903SJohn Dyson object = old_entry->object.vm_object; 3405fed9a903SJohn Dyson if (object == NULL) { 3406fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 3407c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 3408fed9a903SJohn Dyson old_entry->object.vm_object = object; 340915d2d313SAlan Cox old_entry->offset = 0; 3410ef694c1aSEdward Tomasz Napierala if (old_entry->cred != NULL) { 3411ef694c1aSEdward Tomasz Napierala object->cred = old_entry->cred; 34123364c323SKonstantin Belousov object->charge = old_entry->end - 34133364c323SKonstantin Belousov old_entry->start; 3414ef694c1aSEdward Tomasz Napierala old_entry->cred = NULL; 34153364c323SKonstantin Belousov } 34169a2f6362SAlan Cox } 34179a2f6362SAlan Cox 34189a2f6362SAlan Cox /* 34199a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 34209a2f6362SAlan Cox * to insure that a shadow object is created. 34219a2f6362SAlan Cox */ 34229a2f6362SAlan Cox vm_object_reference(object); 34239a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 34245069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 34255069bf57SJohn Dyson &old_entry->offset, 34260cc74f14SAlan Cox old_entry->end - old_entry->start); 34275069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3428d30344bdSIan Dowse /* Transfer the second reference too. */ 3429d30344bdSIan Dowse vm_object_reference( 3430d30344bdSIan Dowse old_entry->object.vm_object); 34317fd10fb3SKonstantin Belousov 34327fd10fb3SKonstantin Belousov /* 34337fd10fb3SKonstantin Belousov * As in vm_map_simplify_entry(), the 3434b0994946SKonstantin Belousov * vnode lock will not be acquired in 34357fd10fb3SKonstantin Belousov * this call to vm_object_deallocate(). 34367fd10fb3SKonstantin Belousov */ 3437d30344bdSIan Dowse vm_object_deallocate(object); 34385069bf57SJohn Dyson object = old_entry->object.vm_object; 3439fed9a903SJohn Dyson } 344089f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3441069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 3442ef694c1aSEdward Tomasz Napierala if (old_entry->cred != NULL) { 3443ef694c1aSEdward Tomasz Napierala KASSERT(object->cred == NULL, ("vmspace_fork both cred")); 3444ef694c1aSEdward Tomasz Napierala object->cred = old_entry->cred; 34453364c323SKonstantin Belousov object->charge = old_entry->end - old_entry->start; 3446ef694c1aSEdward Tomasz Napierala old_entry->cred = NULL; 34473364c323SKonstantin Belousov } 3448b9781cf6SKonstantin Belousov 3449b9781cf6SKonstantin Belousov /* 3450b9781cf6SKonstantin Belousov * Assert the correct state of the vnode 3451b9781cf6SKonstantin Belousov * v_writecount while the object is locked, to 3452b9781cf6SKonstantin Belousov * not relock it later for the assertion 3453b9781cf6SKonstantin Belousov * correctness. 3454b9781cf6SKonstantin Belousov */ 3455b9781cf6SKonstantin Belousov if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT && 3456b9781cf6SKonstantin Belousov object->type == OBJT_VNODE) { 3457b9781cf6SKonstantin Belousov KASSERT(((struct vnode *)object->handle)-> 3458b9781cf6SKonstantin Belousov v_writecount > 0, 3459b9781cf6SKonstantin Belousov ("vmspace_fork: v_writecount %p", object)); 3460b9781cf6SKonstantin Belousov KASSERT(object->un_pager.vnp.writemappings > 0, 3461b9781cf6SKonstantin Belousov ("vmspace_fork: vnp.writecount %p", 3462b9781cf6SKonstantin Belousov object)); 3463b9781cf6SKonstantin Belousov } 346489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3465fed9a903SJohn Dyson 3466fed9a903SJohn Dyson /* 3467ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 3468df8bae1dSRodney W. Grimes */ 3469df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 3470df8bae1dSRodney W. Grimes *new_entry = *old_entry; 34719f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 34729f6acfd1SKonstantin Belousov MAP_ENTRY_IN_TRANSITION); 34730acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL; 3474df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 347584110e7eSKonstantin Belousov if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 347684110e7eSKonstantin Belousov vnode_pager_update_writecount(object, 347784110e7eSKonstantin Belousov new_entry->start, new_entry->end); 347884110e7eSKonstantin Belousov } 3479df8bae1dSRodney W. Grimes 3480df8bae1dSRodney W. Grimes /* 34810d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 34820d94caffSDavid Greenman * inserting at the end of the new map. 3483df8bae1dSRodney W. Grimes */ 3484df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 3485df8bae1dSRodney W. Grimes new_entry); 34862a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 3487df8bae1dSRodney W. Grimes 3488df8bae1dSRodney W. Grimes /* 3489df8bae1dSRodney W. Grimes * Update the physical map 3490df8bae1dSRodney W. Grimes */ 3491df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 3492df8bae1dSRodney W. Grimes new_entry->start, 3493df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 3494df8bae1dSRodney W. Grimes old_entry->start); 3495df8bae1dSRodney W. Grimes break; 3496df8bae1dSRodney W. Grimes 3497df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 3498df8bae1dSRodney W. Grimes /* 3499df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 3500df8bae1dSRodney W. Grimes */ 3501df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 3502df8bae1dSRodney W. Grimes *new_entry = *old_entry; 350384110e7eSKonstantin Belousov /* 350484110e7eSKonstantin Belousov * Copied entry is COW over the old object. 350584110e7eSKonstantin Belousov */ 35069f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 350784110e7eSKonstantin Belousov MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT); 35080acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL; 3509df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 3510df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 3511ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL; 3512df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 3513df8bae1dSRodney W. Grimes new_entry); 35142a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 3515bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 35163364c323SKonstantin Belousov new_entry, fork_charge); 3517df8bae1dSRodney W. Grimes break; 351878d7964bSXin LI 351978d7964bSXin LI case VM_INHERIT_ZERO: 352078d7964bSXin LI /* 352178d7964bSXin LI * Create a new anonymous mapping entry modelled from 352278d7964bSXin LI * the old one. 352378d7964bSXin LI */ 352478d7964bSXin LI new_entry = vm_map_entry_create(new_map); 352578d7964bSXin LI memset(new_entry, 0, sizeof(*new_entry)); 352678d7964bSXin LI 352778d7964bSXin LI new_entry->start = old_entry->start; 352878d7964bSXin LI new_entry->end = old_entry->end; 352978d7964bSXin LI new_entry->eflags = old_entry->eflags & 353078d7964bSXin LI ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION | 353178d7964bSXin LI MAP_ENTRY_VN_WRITECNT); 353278d7964bSXin LI new_entry->protection = old_entry->protection; 353378d7964bSXin LI new_entry->max_protection = old_entry->max_protection; 353478d7964bSXin LI new_entry->inheritance = VM_INHERIT_ZERO; 353578d7964bSXin LI 353678d7964bSXin LI vm_map_entry_link(new_map, new_map->header.prev, 353778d7964bSXin LI new_entry); 353878d7964bSXin LI vmspace_map_entry_forked(vm1, vm2, new_entry); 353978d7964bSXin LI 354078d7964bSXin LI new_entry->cred = curthread->td_ucred; 354178d7964bSXin LI crhold(new_entry->cred); 354278d7964bSXin LI *fork_charge += (new_entry->end - new_entry->start); 354378d7964bSXin LI 354478d7964bSXin LI break; 3545df8bae1dSRodney W. Grimes } 3546df8bae1dSRodney W. Grimes old_entry = old_entry->next; 3547df8bae1dSRodney W. Grimes } 354884110e7eSKonstantin Belousov /* 354984110e7eSKonstantin Belousov * Use inlined vm_map_unlock() to postpone handling the deferred 355084110e7eSKonstantin Belousov * map entries, which cannot be done until both old_map and 355184110e7eSKonstantin Belousov * new_map locks are released. 355284110e7eSKonstantin Belousov */ 355384110e7eSKonstantin Belousov sx_xunlock(&old_map->lock); 355484110e7eSKonstantin Belousov sx_xunlock(&new_map->lock); 355584110e7eSKonstantin Belousov vm_map_process_deferred(); 3556df8bae1dSRodney W. Grimes 3557df8bae1dSRodney W. Grimes return (vm2); 3558df8bae1dSRodney W. Grimes } 3559df8bae1dSRodney W. Grimes 3560*8056df6eSAlan Cox /* 3561*8056df6eSAlan Cox * Create a process's stack for exec_new_vmspace(). This function is never 3562*8056df6eSAlan Cox * asked to wire the newly created stack. 3563*8056df6eSAlan Cox */ 356494f7e29aSAlan Cox int 356594f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 356694f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 356794f7e29aSAlan Cox { 35684648ba0aSKonstantin Belousov vm_size_t growsize, init_ssize; 3569*8056df6eSAlan Cox rlim_t vmemlim; 35704648ba0aSKonstantin Belousov int rv; 35714648ba0aSKonstantin Belousov 3572*8056df6eSAlan Cox MPASS((map->flags & MAP_WIREFUTURE) == 0); 35734648ba0aSKonstantin Belousov growsize = sgrowsiz; 35744648ba0aSKonstantin Belousov init_ssize = (max_ssize < growsize) ? max_ssize : growsize; 35754648ba0aSKonstantin Belousov vm_map_lock(map); 3576f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM); 35774648ba0aSKonstantin Belousov /* If we would blow our VMEM resource limit, no go */ 35784648ba0aSKonstantin Belousov if (map->size + init_ssize > vmemlim) { 35794648ba0aSKonstantin Belousov rv = KERN_NO_SPACE; 35804648ba0aSKonstantin Belousov goto out; 35814648ba0aSKonstantin Belousov } 3582e1f92cccSAlan Cox rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot, 35834648ba0aSKonstantin Belousov max, cow); 35844648ba0aSKonstantin Belousov out: 35854648ba0aSKonstantin Belousov vm_map_unlock(map); 35864648ba0aSKonstantin Belousov return (rv); 35874648ba0aSKonstantin Belousov } 35884648ba0aSKonstantin Belousov 358919f49ad3SKonstantin Belousov static int stack_guard_page = 1; 359019f49ad3SKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN, 359119f49ad3SKonstantin Belousov &stack_guard_page, 0, 359219f49ad3SKonstantin Belousov "Specifies the number of guard pages for a stack that grows"); 359319f49ad3SKonstantin Belousov 35944648ba0aSKonstantin Belousov static int 35954648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 35964648ba0aSKonstantin Belousov vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow) 35974648ba0aSKonstantin Belousov { 3598fd75d710SMarcel Moolenaar vm_map_entry_t new_entry, prev_entry; 359919bd0d9cSKonstantin Belousov vm_offset_t bot, gap_bot, gap_top, top; 360019f49ad3SKonstantin Belousov vm_size_t init_ssize, sgp; 3601fd75d710SMarcel Moolenaar int orient, rv; 360294f7e29aSAlan Cox 3603fd75d710SMarcel Moolenaar /* 3604fd75d710SMarcel Moolenaar * The stack orientation is piggybacked with the cow argument. 3605fd75d710SMarcel Moolenaar * Extract it into orient and mask the cow argument so that we 3606fd75d710SMarcel Moolenaar * don't pass it around further. 3607fd75d710SMarcel Moolenaar */ 3608fd75d710SMarcel Moolenaar orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP); 3609fd75d710SMarcel Moolenaar KASSERT(orient != 0, ("No stack grow direction")); 361019bd0d9cSKonstantin Belousov KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP), 361119bd0d9cSKonstantin Belousov ("bi-dir stack")); 3612fd75d710SMarcel Moolenaar 361319f49ad3SKonstantin Belousov sgp = (vm_size_t)stack_guard_page * PAGE_SIZE; 361477bc7900SKonstantin Belousov if (addrbos < vm_map_min(map) || 361577bc7900SKonstantin Belousov addrbos > vm_map_max(map) || 361619f49ad3SKonstantin Belousov addrbos + max_ssize < addrbos || 361719f49ad3SKonstantin Belousov sgp >= max_ssize) 361894f7e29aSAlan Cox return (KERN_NO_SPACE); 3619fd75d710SMarcel Moolenaar 362019f49ad3SKonstantin Belousov init_ssize = growsize; 362119f49ad3SKonstantin Belousov if (max_ssize < init_ssize + sgp) 362219f49ad3SKonstantin Belousov init_ssize = max_ssize - sgp; 362394f7e29aSAlan Cox 362494f7e29aSAlan Cox /* If addr is already mapped, no go */ 36254648ba0aSKonstantin Belousov if (vm_map_lookup_entry(map, addrbos, &prev_entry)) 362694f7e29aSAlan Cox return (KERN_NO_SPACE); 3627a69ac174SMatthew Dillon 3628fd75d710SMarcel Moolenaar /* 3629763df3ecSPedro F. Giffuni * If we can't accommodate max_ssize in the current mapping, no go. 363094f7e29aSAlan Cox */ 363194f7e29aSAlan Cox if ((prev_entry->next != &map->header) && 36324648ba0aSKonstantin Belousov (prev_entry->next->start < addrbos + max_ssize)) 363394f7e29aSAlan Cox return (KERN_NO_SPACE); 363494f7e29aSAlan Cox 3635fd75d710SMarcel Moolenaar /* 3636fd75d710SMarcel Moolenaar * We initially map a stack of only init_ssize. We will grow as 3637fd75d710SMarcel Moolenaar * needed later. Depending on the orientation of the stack (i.e. 3638fd75d710SMarcel Moolenaar * the grow direction) we either map at the top of the range, the 3639fd75d710SMarcel Moolenaar * bottom of the range or in the middle. 364094f7e29aSAlan Cox * 3641fd75d710SMarcel Moolenaar * Note: we would normally expect prot and max to be VM_PROT_ALL, 3642fd75d710SMarcel Moolenaar * and cow to be 0. Possibly we should eliminate these as input 3643fd75d710SMarcel Moolenaar * parameters, and just pass these values here in the insert call. 364494f7e29aSAlan Cox */ 364519bd0d9cSKonstantin Belousov if (orient == MAP_STACK_GROWS_DOWN) { 3646fd75d710SMarcel Moolenaar bot = addrbos + max_ssize - init_ssize; 3647fd75d710SMarcel Moolenaar top = bot + init_ssize; 364819bd0d9cSKonstantin Belousov gap_bot = addrbos; 364919bd0d9cSKonstantin Belousov gap_top = bot; 365019bd0d9cSKonstantin Belousov } else /* if (orient == MAP_STACK_GROWS_UP) */ { 365119bd0d9cSKonstantin Belousov bot = addrbos; 365219bd0d9cSKonstantin Belousov top = bot + init_ssize; 365319bd0d9cSKonstantin Belousov gap_bot = top; 365419bd0d9cSKonstantin Belousov gap_top = addrbos + max_ssize; 365519bd0d9cSKonstantin Belousov } 3656fd75d710SMarcel Moolenaar rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 365719bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) 365819bd0d9cSKonstantin Belousov return (rv); 3659fd75d710SMarcel Moolenaar new_entry = prev_entry->next; 366019bd0d9cSKonstantin Belousov KASSERT(new_entry->end == top || new_entry->start == bot, 366119bd0d9cSKonstantin Belousov ("Bad entry start/end for new stack entry")); 3662712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 || 3663712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0, 3664712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_DOWN")); 3665712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_UP) == 0 || 3666712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0, 3667712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_UP")); 366819bd0d9cSKonstantin Belousov rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE, 366919bd0d9cSKonstantin Belousov VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ? 367019bd0d9cSKonstantin Belousov MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP)); 367119bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) 367219bd0d9cSKonstantin Belousov (void)vm_map_delete(map, bot, top); 367394f7e29aSAlan Cox return (rv); 367494f7e29aSAlan Cox } 367594f7e29aSAlan Cox 367619bd0d9cSKonstantin Belousov /* 367719bd0d9cSKonstantin Belousov * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we 367819bd0d9cSKonstantin Belousov * successfully grow the stack. 367994f7e29aSAlan Cox */ 368019bd0d9cSKonstantin Belousov static int 368119bd0d9cSKonstantin Belousov vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry) 368294f7e29aSAlan Cox { 368319bd0d9cSKonstantin Belousov vm_map_entry_t stack_entry; 368419bd0d9cSKonstantin Belousov struct proc *p; 368519bd0d9cSKonstantin Belousov struct vmspace *vm; 368619bd0d9cSKonstantin Belousov struct ucred *cred; 368719bd0d9cSKonstantin Belousov vm_offset_t gap_end, gap_start, grow_start; 3688b21a0008SMarcel Moolenaar size_t grow_amount, max_grow; 36897e19eda4SAndrey Zonov rlim_t lmemlim, stacklim, vmemlim; 369019bd0d9cSKonstantin Belousov int rv, rv1; 369119bd0d9cSKonstantin Belousov bool gap_deleted, grow_down, is_procstack; 36921ba5ad42SEdward Tomasz Napierala #ifdef notyet 36931ba5ad42SEdward Tomasz Napierala uint64_t limit; 36941ba5ad42SEdward Tomasz Napierala #endif 3695afcc55f3SEdward Tomasz Napierala #ifdef RACCT 36961ba5ad42SEdward Tomasz Napierala int error; 3697afcc55f3SEdward Tomasz Napierala #endif 369823955314SAlfred Perlstein 369919bd0d9cSKonstantin Belousov p = curproc; 370019bd0d9cSKonstantin Belousov vm = p->p_vmspace; 370119bd0d9cSKonstantin Belousov MPASS(map == &p->p_vmspace->vm_map); 370219bd0d9cSKonstantin Belousov MPASS(!map->system_map); 370319bd0d9cSKonstantin Belousov 3704f6f6d240SMateusz Guzik lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK); 3705f6f6d240SMateusz Guzik stacklim = lim_cur(curthread, RLIMIT_STACK); 3706f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM); 370719bd0d9cSKonstantin Belousov retry: 370819bd0d9cSKonstantin Belousov /* If addr is not in a hole for a stack grow area, no need to grow. */ 370919bd0d9cSKonstantin Belousov if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry)) 371019bd0d9cSKonstantin Belousov return (KERN_FAILURE); 371119bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0) 37120cddd8f0SMatthew Dillon return (KERN_SUCCESS); 371319bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) { 371419bd0d9cSKonstantin Belousov stack_entry = gap_entry->next; 371519bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 || 371619bd0d9cSKonstantin Belousov stack_entry->start != gap_entry->end) 371719bd0d9cSKonstantin Belousov return (KERN_FAILURE); 371819bd0d9cSKonstantin Belousov grow_amount = round_page(stack_entry->start - addr); 371919bd0d9cSKonstantin Belousov grow_down = true; 372019bd0d9cSKonstantin Belousov } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) { 372119bd0d9cSKonstantin Belousov stack_entry = gap_entry->prev; 372219bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 || 372319bd0d9cSKonstantin Belousov stack_entry->end != gap_entry->start) 372419bd0d9cSKonstantin Belousov return (KERN_FAILURE); 372519bd0d9cSKonstantin Belousov grow_amount = round_page(addr + 1 - stack_entry->end); 372619bd0d9cSKonstantin Belousov grow_down = false; 3727b21a0008SMarcel Moolenaar } else { 372819bd0d9cSKonstantin Belousov return (KERN_FAILURE); 3729b21a0008SMarcel Moolenaar } 373019bd0d9cSKonstantin Belousov max_grow = gap_entry->end - gap_entry->start - stack_guard_page * 373119bd0d9cSKonstantin Belousov PAGE_SIZE; 373219bd0d9cSKonstantin Belousov if (grow_amount > max_grow) 37330cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 373494f7e29aSAlan Cox 3735b21a0008SMarcel Moolenaar /* 3736b21a0008SMarcel Moolenaar * If this is the main process stack, see if we're over the stack 3737b21a0008SMarcel Moolenaar * limit. 373894f7e29aSAlan Cox */ 373919bd0d9cSKonstantin Belousov is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr && 374019bd0d9cSKonstantin Belousov addr < (vm_offset_t)p->p_sysent->sv_usrstack; 374119bd0d9cSKonstantin Belousov if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) 37420cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 374319bd0d9cSKonstantin Belousov 3744afcc55f3SEdward Tomasz Napierala #ifdef RACCT 37454b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 37461ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 37474b5c9cf6SEdward Tomasz Napierala if (is_procstack && racct_set(p, RACCT_STACK, 37484b5c9cf6SEdward Tomasz Napierala ctob(vm->vm_ssize) + grow_amount)) { 37491ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 37501ba5ad42SEdward Tomasz Napierala return (KERN_NO_SPACE); 37511ba5ad42SEdward Tomasz Napierala } 37521ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 37534b5c9cf6SEdward Tomasz Napierala } 3754afcc55f3SEdward Tomasz Napierala #endif 375594f7e29aSAlan Cox 375619bd0d9cSKonstantin Belousov grow_amount = roundup(grow_amount, sgrowsiz); 375719bd0d9cSKonstantin Belousov if (grow_amount > max_grow) 375819bd0d9cSKonstantin Belousov grow_amount = max_grow; 375991d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 3760e4826248SAlan Cox grow_amount = trunc_page((vm_size_t)stacklim) - 3761e4826248SAlan Cox ctob(vm->vm_ssize); 376294f7e29aSAlan Cox } 376319bd0d9cSKonstantin Belousov 37641ba5ad42SEdward Tomasz Napierala #ifdef notyet 37651ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 37661ba5ad42SEdward Tomasz Napierala limit = racct_get_available(p, RACCT_STACK); 37671ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 37681ba5ad42SEdward Tomasz Napierala if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit)) 37691ba5ad42SEdward Tomasz Napierala grow_amount = limit - ctob(vm->vm_ssize); 37701ba5ad42SEdward Tomasz Napierala #endif 377119bd0d9cSKonstantin Belousov 377219bd0d9cSKonstantin Belousov if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) { 37733ac7d297SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) { 37747e19eda4SAndrey Zonov rv = KERN_NO_SPACE; 37757e19eda4SAndrey Zonov goto out; 37767e19eda4SAndrey Zonov } 37777e19eda4SAndrey Zonov #ifdef RACCT 37784b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 37797e19eda4SAndrey Zonov PROC_LOCK(p); 37807e19eda4SAndrey Zonov if (racct_set(p, RACCT_MEMLOCK, 37813ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap)) + grow_amount)) { 37827e19eda4SAndrey Zonov PROC_UNLOCK(p); 37837e19eda4SAndrey Zonov rv = KERN_NO_SPACE; 37847e19eda4SAndrey Zonov goto out; 37857e19eda4SAndrey Zonov } 37867e19eda4SAndrey Zonov PROC_UNLOCK(p); 37874b5c9cf6SEdward Tomasz Napierala } 37887e19eda4SAndrey Zonov #endif 37897e19eda4SAndrey Zonov } 379019bd0d9cSKonstantin Belousov 3791a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 379291d5354aSJohn Baldwin if (map->size + grow_amount > vmemlim) { 37931ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE; 37941ba5ad42SEdward Tomasz Napierala goto out; 3795a69ac174SMatthew Dillon } 3796afcc55f3SEdward Tomasz Napierala #ifdef RACCT 37974b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 37981ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 37991ba5ad42SEdward Tomasz Napierala if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) { 38001ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 38011ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE; 38021ba5ad42SEdward Tomasz Napierala goto out; 38031ba5ad42SEdward Tomasz Napierala } 38041ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 38054b5c9cf6SEdward Tomasz Napierala } 3806afcc55f3SEdward Tomasz Napierala #endif 3807a69ac174SMatthew Dillon 380819bd0d9cSKonstantin Belousov if (vm_map_lock_upgrade(map)) { 380919bd0d9cSKonstantin Belousov gap_entry = NULL; 381019bd0d9cSKonstantin Belousov vm_map_lock_read(map); 381119bd0d9cSKonstantin Belousov goto retry; 381294f7e29aSAlan Cox } 381394f7e29aSAlan Cox 381419bd0d9cSKonstantin Belousov if (grow_down) { 381519bd0d9cSKonstantin Belousov grow_start = gap_entry->end - grow_amount; 381619bd0d9cSKonstantin Belousov if (gap_entry->start + grow_amount == gap_entry->end) { 381719bd0d9cSKonstantin Belousov gap_start = gap_entry->start; 381819bd0d9cSKonstantin Belousov gap_end = gap_entry->end; 381919bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry); 382019bd0d9cSKonstantin Belousov gap_deleted = true; 382119bd0d9cSKonstantin Belousov } else { 382219bd0d9cSKonstantin Belousov MPASS(gap_entry->start < gap_entry->end - grow_amount); 382319bd0d9cSKonstantin Belousov gap_entry->end -= grow_amount; 382419bd0d9cSKonstantin Belousov vm_map_entry_resize_free(map, gap_entry); 382519bd0d9cSKonstantin Belousov gap_deleted = false; 382619bd0d9cSKonstantin Belousov } 382719bd0d9cSKonstantin Belousov rv = vm_map_insert(map, NULL, 0, grow_start, 382819bd0d9cSKonstantin Belousov grow_start + grow_amount, 382919bd0d9cSKonstantin Belousov stack_entry->protection, stack_entry->max_protection, 3830712efe66SAlan Cox MAP_STACK_GROWS_DOWN); 383119bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) { 383219bd0d9cSKonstantin Belousov if (gap_deleted) { 383319bd0d9cSKonstantin Belousov rv1 = vm_map_insert(map, NULL, 0, gap_start, 383419bd0d9cSKonstantin Belousov gap_end, VM_PROT_NONE, VM_PROT_NONE, 383519bd0d9cSKonstantin Belousov MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN); 383619bd0d9cSKonstantin Belousov MPASS(rv1 == KERN_SUCCESS); 383719bd0d9cSKonstantin Belousov } else { 383819bd0d9cSKonstantin Belousov gap_entry->end += grow_amount; 383919bd0d9cSKonstantin Belousov vm_map_entry_resize_free(map, gap_entry); 384019bd0d9cSKonstantin Belousov } 384194f7e29aSAlan Cox } 3842b21a0008SMarcel Moolenaar } else { 384319bd0d9cSKonstantin Belousov grow_start = stack_entry->end; 3844ef694c1aSEdward Tomasz Napierala cred = stack_entry->cred; 3845ef694c1aSEdward Tomasz Napierala if (cred == NULL && stack_entry->object.vm_object != NULL) 3846ef694c1aSEdward Tomasz Napierala cred = stack_entry->object.vm_object->cred; 3847ef694c1aSEdward Tomasz Napierala if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred)) 38483364c323SKonstantin Belousov rv = KERN_NO_SPACE; 3849b21a0008SMarcel Moolenaar /* Grow the underlying object if applicable. */ 38503364c323SKonstantin Belousov else if (stack_entry->object.vm_object == NULL || 3851b21a0008SMarcel Moolenaar vm_object_coalesce(stack_entry->object.vm_object, 385257a21abaSAlan Cox stack_entry->offset, 3853b21a0008SMarcel Moolenaar (vm_size_t)(stack_entry->end - stack_entry->start), 3854ef694c1aSEdward Tomasz Napierala (vm_size_t)grow_amount, cred != NULL)) { 385519bd0d9cSKonstantin Belousov if (gap_entry->start + grow_amount == gap_entry->end) 385619bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry); 385719bd0d9cSKonstantin Belousov else 385819bd0d9cSKonstantin Belousov gap_entry->start += grow_amount; 385919bd0d9cSKonstantin Belousov stack_entry->end += grow_amount; 386019bd0d9cSKonstantin Belousov map->size += grow_amount; 38610164e057SAlan Cox vm_map_entry_resize_free(map, stack_entry); 3862b21a0008SMarcel Moolenaar rv = KERN_SUCCESS; 3863b21a0008SMarcel Moolenaar } else 3864b21a0008SMarcel Moolenaar rv = KERN_FAILURE; 3865b21a0008SMarcel Moolenaar } 3866b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && is_procstack) 3867b21a0008SMarcel Moolenaar vm->vm_ssize += btoc(grow_amount); 3868b21a0008SMarcel Moolenaar 3869abd498aaSBruce M Simpson /* 3870abd498aaSBruce M Simpson * Heed the MAP_WIREFUTURE flag if it was set for this process. 3871abd498aaSBruce M Simpson */ 387219bd0d9cSKonstantin Belousov if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) { 387319bd0d9cSKonstantin Belousov vm_map_unlock(map); 387419bd0d9cSKonstantin Belousov vm_map_wire(map, grow_start, grow_start + grow_amount, 3875212e02c8SKonstantin Belousov VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 387619bd0d9cSKonstantin Belousov vm_map_lock_read(map); 387719bd0d9cSKonstantin Belousov } else 387819bd0d9cSKonstantin Belousov vm_map_lock_downgrade(map); 3879abd498aaSBruce M Simpson 38801ba5ad42SEdward Tomasz Napierala out: 3881afcc55f3SEdward Tomasz Napierala #ifdef RACCT 38824b5c9cf6SEdward Tomasz Napierala if (racct_enable && rv != KERN_SUCCESS) { 38831ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 38841ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_VMEM, map->size); 38851ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_VMEM failed")); 38867e19eda4SAndrey Zonov if (!old_mlock) { 38877e19eda4SAndrey Zonov error = racct_set(p, RACCT_MEMLOCK, 38883ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 38897e19eda4SAndrey Zonov KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed")); 38907e19eda4SAndrey Zonov } 38911ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize)); 38921ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_STACK failed")); 38931ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 38941ba5ad42SEdward Tomasz Napierala } 3895afcc55f3SEdward Tomasz Napierala #endif 38961ba5ad42SEdward Tomasz Napierala 38970cddd8f0SMatthew Dillon return (rv); 389894f7e29aSAlan Cox } 389994f7e29aSAlan Cox 3900df8bae1dSRodney W. Grimes /* 39015856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 39025856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 39035856e12eSJohn Dyson */ 390489b57fcfSKonstantin Belousov int 39053ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 39061b40f8c0SMatthew Dillon { 39075856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 39085856e12eSJohn Dyson struct vmspace *newvmspace; 39095856e12eSJohn Dyson 39107032434eSKonstantin Belousov KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0, 39117032434eSKonstantin Belousov ("vmspace_exec recursed")); 391274d1d2b7SNeel Natu newvmspace = vmspace_alloc(minuser, maxuser, NULL); 391389b57fcfSKonstantin Belousov if (newvmspace == NULL) 391489b57fcfSKonstantin Belousov return (ENOMEM); 391551ab6c28SAlan Cox newvmspace->vm_swrss = oldvmspace->vm_swrss; 39165856e12eSJohn Dyson /* 39175856e12eSJohn Dyson * This code is written like this for prototype purposes. The 39185856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 39195856e12eSJohn Dyson * other process's that are still using the vmspace to finally 39205856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 39215856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 39225856e12eSJohn Dyson */ 392357051fdcSTor Egge PROC_VMSPACE_LOCK(p); 39245856e12eSJohn Dyson p->p_vmspace = newvmspace; 392557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 39266617724cSJeff Roberson if (p == curthread->td_proc) 3927b40ce416SJulian Elischer pmap_activate(curthread); 39287032434eSKonstantin Belousov curthread->td_pflags |= TDP_EXECVMSPC; 392989b57fcfSKonstantin Belousov return (0); 39305856e12eSJohn Dyson } 39315856e12eSJohn Dyson 39325856e12eSJohn Dyson /* 39335856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 39345856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 39355856e12eSJohn Dyson */ 393689b57fcfSKonstantin Belousov int 39371b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p) 39381b40f8c0SMatthew Dillon { 39395856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 39405856e12eSJohn Dyson struct vmspace *newvmspace; 39413364c323SKonstantin Belousov vm_ooffset_t fork_charge; 39425856e12eSJohn Dyson 39435856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 394489b57fcfSKonstantin Belousov return (0); 39453364c323SKonstantin Belousov fork_charge = 0; 39463364c323SKonstantin Belousov newvmspace = vmspace_fork(oldvmspace, &fork_charge); 394789b57fcfSKonstantin Belousov if (newvmspace == NULL) 394889b57fcfSKonstantin Belousov return (ENOMEM); 3949ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) { 39503364c323SKonstantin Belousov vmspace_free(newvmspace); 39513364c323SKonstantin Belousov return (ENOMEM); 39523364c323SKonstantin Belousov } 395357051fdcSTor Egge PROC_VMSPACE_LOCK(p); 39545856e12eSJohn Dyson p->p_vmspace = newvmspace; 395557051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 39566617724cSJeff Roberson if (p == curthread->td_proc) 3957b40ce416SJulian Elischer pmap_activate(curthread); 3958b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 395989b57fcfSKonstantin Belousov return (0); 39605856e12eSJohn Dyson } 39615856e12eSJohn Dyson 39625856e12eSJohn Dyson /* 3963df8bae1dSRodney W. Grimes * vm_map_lookup: 3964df8bae1dSRodney W. Grimes * 3965df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 3966df8bae1dSRodney W. Grimes * protection for a given virtual address in the 3967df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 3968df8bae1dSRodney W. Grimes * type specified. 3969df8bae1dSRodney W. Grimes * 3970df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 3971df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 3972df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 3973df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 3974df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 3975df8bae1dSRodney W. Grimes * 3976df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 3977df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 3978df8bae1dSRodney W. Grimes * 3979df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 3980df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 3981df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 3982df8bae1dSRodney W. Grimes * remain the same. 3983df8bae1dSRodney W. Grimes */ 3984df8bae1dSRodney W. Grimes int 3985b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 3986b9dcd593SBruce Evans vm_offset_t vaddr, 398747221757SJohn Dyson vm_prot_t fault_typea, 3988b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 3989b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 3990b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 3991b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 39922d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 3993df8bae1dSRodney W. Grimes { 3994c0877f10SJohn Dyson vm_map_entry_t entry; 3995c0877f10SJohn Dyson vm_map_t map = *var_map; 3996c0877f10SJohn Dyson vm_prot_t prot; 399747221757SJohn Dyson vm_prot_t fault_type = fault_typea; 39983364c323SKonstantin Belousov vm_object_t eobject; 39990cc74f14SAlan Cox vm_size_t size; 4000ef694c1aSEdward Tomasz Napierala struct ucred *cred; 4001df8bae1dSRodney W. Grimes 400219bd0d9cSKonstantin Belousov RetryLookup: 4003df8bae1dSRodney W. Grimes 4004df8bae1dSRodney W. Grimes vm_map_lock_read(map); 4005df8bae1dSRodney W. Grimes 400619bd0d9cSKonstantin Belousov RetryLookupLocked: 4007df8bae1dSRodney W. Grimes /* 40084c3ef59eSAlan Cox * Lookup the faulting address. 4009df8bae1dSRodney W. Grimes */ 4010095104acSAlan Cox if (!vm_map_lookup_entry(map, vaddr, out_entry)) { 4011095104acSAlan Cox vm_map_unlock_read(map); 4012095104acSAlan Cox return (KERN_INVALID_ADDRESS); 4013095104acSAlan Cox } 4014df8bae1dSRodney W. Grimes 40154e94f402SAlan Cox entry = *out_entry; 4016b7b2aac2SJohn Dyson 4017df8bae1dSRodney W. Grimes /* 4018df8bae1dSRodney W. Grimes * Handle submaps. 4019df8bae1dSRodney W. Grimes */ 4020afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 4021df8bae1dSRodney W. Grimes vm_map_t old_map = map; 4022df8bae1dSRodney W. Grimes 4023df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 4024df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 4025df8bae1dSRodney W. Grimes goto RetryLookup; 4026df8bae1dSRodney W. Grimes } 4027a04c970aSJohn Dyson 4028df8bae1dSRodney W. Grimes /* 40290d94caffSDavid Greenman * Check whether this task is allowed to have this page. 4030df8bae1dSRodney W. Grimes */ 4031df8bae1dSRodney W. Grimes prot = entry->protection; 403219bd0d9cSKonstantin Belousov if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) { 403319bd0d9cSKonstantin Belousov fault_typea &= ~VM_PROT_FAULT_LOOKUP; 403419bd0d9cSKonstantin Belousov if (prot == VM_PROT_NONE && map != kernel_map && 403519bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) != 0 && 403619bd0d9cSKonstantin Belousov (entry->eflags & (MAP_ENTRY_STACK_GAP_DN | 403719bd0d9cSKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) != 0 && 403819bd0d9cSKonstantin Belousov vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS) 403919bd0d9cSKonstantin Belousov goto RetryLookupLocked; 404019bd0d9cSKonstantin Belousov } 404119bd0d9cSKonstantin Belousov fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 40422db65ab4SAlan Cox if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) { 4043095104acSAlan Cox vm_map_unlock_read(map); 4044095104acSAlan Cox return (KERN_PROTECTION_FAILURE); 404547221757SJohn Dyson } 4046b8db9776SKonstantin Belousov KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags & 4047b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) != 4048b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY), 4049b8db9776SKonstantin Belousov ("entry %p flags %x", entry, entry->eflags)); 40505b3e0257SDag-Erling Smørgrav if ((fault_typea & VM_PROT_COPY) != 0 && 40515b3e0257SDag-Erling Smørgrav (entry->max_protection & VM_PROT_WRITE) == 0 && 40525b3e0257SDag-Erling Smørgrav (entry->eflags & MAP_ENTRY_COW) == 0) { 40535b3e0257SDag-Erling Smørgrav vm_map_unlock_read(map); 40545b3e0257SDag-Erling Smørgrav return (KERN_PROTECTION_FAILURE); 40555b3e0257SDag-Erling Smørgrav } 4056df8bae1dSRodney W. Grimes 4057df8bae1dSRodney W. Grimes /* 40580d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 40590d94caffSDavid Greenman * accesses. 4060df8bae1dSRodney W. Grimes */ 406105f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 406205f0fdd2SPoul-Henning Kamp if (*wired) 4063a6d42a0dSAlan Cox fault_type = entry->protection; 40643364c323SKonstantin Belousov size = entry->end - entry->start; 4065df8bae1dSRodney W. Grimes /* 4066df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 4067df8bae1dSRodney W. Grimes */ 4068afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 4069df8bae1dSRodney W. Grimes /* 40700d94caffSDavid Greenman * If we want to write the page, we may as well handle that 4071ad5fca3bSAlan Cox * now since we've got the map locked. 4072df8bae1dSRodney W. Grimes * 40730d94caffSDavid Greenman * If we don't need to write the page, we just demote the 40740d94caffSDavid Greenman * permissions allowed. 4075df8bae1dSRodney W. Grimes */ 4076a6d42a0dSAlan Cox if ((fault_type & VM_PROT_WRITE) != 0 || 4077a6d42a0dSAlan Cox (fault_typea & VM_PROT_COPY) != 0) { 4078df8bae1dSRodney W. Grimes /* 40790d94caffSDavid Greenman * Make a new object, and place it in the object 40800d94caffSDavid Greenman * chain. Note that no new references have appeared 4081ad5fca3bSAlan Cox * -- one just moved from the map to the new 40820d94caffSDavid Greenman * object. 4083df8bae1dSRodney W. Grimes */ 408425adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 4085df8bae1dSRodney W. Grimes goto RetryLookup; 40869917e010SAlan Cox 4087ef694c1aSEdward Tomasz Napierala if (entry->cred == NULL) { 40883364c323SKonstantin Belousov /* 40893364c323SKonstantin Belousov * The debugger owner is charged for 40903364c323SKonstantin Belousov * the memory. 40913364c323SKonstantin Belousov */ 4092ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 4093ef694c1aSEdward Tomasz Napierala crhold(cred); 4094ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(size, cred)) { 4095ef694c1aSEdward Tomasz Napierala crfree(cred); 40963364c323SKonstantin Belousov vm_map_unlock(map); 40973364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 40983364c323SKonstantin Belousov } 4099ef694c1aSEdward Tomasz Napierala entry->cred = cred; 41003364c323SKonstantin Belousov } 41010cc74f14SAlan Cox vm_object_shadow(&entry->object.vm_object, 41020cc74f14SAlan Cox &entry->offset, size); 4103afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 41043364c323SKonstantin Belousov eobject = entry->object.vm_object; 4105ef694c1aSEdward Tomasz Napierala if (eobject->cred != NULL) { 41063364c323SKonstantin Belousov /* 41073364c323SKonstantin Belousov * The object was not shadowed. 41083364c323SKonstantin Belousov */ 4109ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred); 4110ef694c1aSEdward Tomasz Napierala crfree(entry->cred); 4111ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 4112ef694c1aSEdward Tomasz Napierala } else if (entry->cred != NULL) { 411389f6b863SAttilio Rao VM_OBJECT_WLOCK(eobject); 4114ef694c1aSEdward Tomasz Napierala eobject->cred = entry->cred; 41153364c323SKonstantin Belousov eobject->charge = size; 411689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(eobject); 4117ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 41183364c323SKonstantin Belousov } 41199917e010SAlan Cox 41209b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 41210d94caffSDavid Greenman } else { 4122df8bae1dSRodney W. Grimes /* 41230d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 41240d94caffSDavid Greenman * don't allow writes. 4125df8bae1dSRodney W. Grimes */ 41262d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 4127df8bae1dSRodney W. Grimes } 4128df8bae1dSRodney W. Grimes } 41292d8acc0fSJohn Dyson 4130df8bae1dSRodney W. Grimes /* 4131df8bae1dSRodney W. Grimes * Create an object if necessary. 4132df8bae1dSRodney W. Grimes */ 41334e71e795SMatthew Dillon if (entry->object.vm_object == NULL && 41344e71e795SMatthew Dillon !map->system_map) { 413525adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 4136df8bae1dSRodney W. Grimes goto RetryLookup; 413724a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 41383364c323SKonstantin Belousov atop(size)); 4139df8bae1dSRodney W. Grimes entry->offset = 0; 4140ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 414189f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 4142ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 41433364c323SKonstantin Belousov entry->object.vm_object->charge = size; 414489f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 4145ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 41463364c323SKonstantin Belousov } 41479b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 4148df8bae1dSRodney W. Grimes } 4149b5b40fa6SJohn Dyson 4150df8bae1dSRodney W. Grimes /* 41510d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 41520d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 4153df8bae1dSRodney W. Grimes */ 41542b6d1a63SKonstantin Belousov *pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset); 4155df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 4156df8bae1dSRodney W. Grimes 4157df8bae1dSRodney W. Grimes *out_prot = prot; 4158df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 4159df8bae1dSRodney W. Grimes } 4160df8bae1dSRodney W. Grimes 4161df8bae1dSRodney W. Grimes /* 416219dc5607STor Egge * vm_map_lookup_locked: 416319dc5607STor Egge * 416419dc5607STor Egge * Lookup the faulting address. A version of vm_map_lookup that returns 416519dc5607STor Egge * KERN_FAILURE instead of blocking on map lock or memory allocation. 416619dc5607STor Egge */ 416719dc5607STor Egge int 416819dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 416919dc5607STor Egge vm_offset_t vaddr, 417019dc5607STor Egge vm_prot_t fault_typea, 417119dc5607STor Egge vm_map_entry_t *out_entry, /* OUT */ 417219dc5607STor Egge vm_object_t *object, /* OUT */ 417319dc5607STor Egge vm_pindex_t *pindex, /* OUT */ 417419dc5607STor Egge vm_prot_t *out_prot, /* OUT */ 417519dc5607STor Egge boolean_t *wired) /* OUT */ 417619dc5607STor Egge { 417719dc5607STor Egge vm_map_entry_t entry; 417819dc5607STor Egge vm_map_t map = *var_map; 417919dc5607STor Egge vm_prot_t prot; 418019dc5607STor Egge vm_prot_t fault_type = fault_typea; 418119dc5607STor Egge 418219dc5607STor Egge /* 41834c3ef59eSAlan Cox * Lookup the faulting address. 418419dc5607STor Egge */ 418519dc5607STor Egge if (!vm_map_lookup_entry(map, vaddr, out_entry)) 418619dc5607STor Egge return (KERN_INVALID_ADDRESS); 418719dc5607STor Egge 418819dc5607STor Egge entry = *out_entry; 418919dc5607STor Egge 419019dc5607STor Egge /* 419119dc5607STor Egge * Fail if the entry refers to a submap. 419219dc5607STor Egge */ 419319dc5607STor Egge if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 419419dc5607STor Egge return (KERN_FAILURE); 419519dc5607STor Egge 419619dc5607STor Egge /* 419719dc5607STor Egge * Check whether this task is allowed to have this page. 419819dc5607STor Egge */ 419919dc5607STor Egge prot = entry->protection; 420019dc5607STor Egge fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 420119dc5607STor Egge if ((fault_type & prot) != fault_type) 420219dc5607STor Egge return (KERN_PROTECTION_FAILURE); 420319dc5607STor Egge 420419dc5607STor Egge /* 420519dc5607STor Egge * If this page is not pageable, we have to get it for all possible 420619dc5607STor Egge * accesses. 420719dc5607STor Egge */ 420819dc5607STor Egge *wired = (entry->wired_count != 0); 420919dc5607STor Egge if (*wired) 4210a6d42a0dSAlan Cox fault_type = entry->protection; 421119dc5607STor Egge 421219dc5607STor Egge if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 421319dc5607STor Egge /* 421419dc5607STor Egge * Fail if the entry was copy-on-write for a write fault. 421519dc5607STor Egge */ 421619dc5607STor Egge if (fault_type & VM_PROT_WRITE) 421719dc5607STor Egge return (KERN_FAILURE); 421819dc5607STor Egge /* 421919dc5607STor Egge * We're attempting to read a copy-on-write page -- 422019dc5607STor Egge * don't allow writes. 422119dc5607STor Egge */ 422219dc5607STor Egge prot &= ~VM_PROT_WRITE; 422319dc5607STor Egge } 422419dc5607STor Egge 422519dc5607STor Egge /* 422619dc5607STor Egge * Fail if an object should be created. 422719dc5607STor Egge */ 422819dc5607STor Egge if (entry->object.vm_object == NULL && !map->system_map) 422919dc5607STor Egge return (KERN_FAILURE); 423019dc5607STor Egge 423119dc5607STor Egge /* 423219dc5607STor Egge * Return the object/offset from this entry. If the entry was 423319dc5607STor Egge * copy-on-write or empty, it has been fixed up. 423419dc5607STor Egge */ 42352b6d1a63SKonstantin Belousov *pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset); 423619dc5607STor Egge *object = entry->object.vm_object; 423719dc5607STor Egge 423819dc5607STor Egge *out_prot = prot; 423919dc5607STor Egge return (KERN_SUCCESS); 424019dc5607STor Egge } 424119dc5607STor Egge 424219dc5607STor Egge /* 4243df8bae1dSRodney W. Grimes * vm_map_lookup_done: 4244df8bae1dSRodney W. Grimes * 4245df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 4246df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 4247df8bae1dSRodney W. Grimes */ 42480d94caffSDavid Greenman void 42491b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 4250df8bae1dSRodney W. Grimes { 4251df8bae1dSRodney W. Grimes /* 4252df8bae1dSRodney W. Grimes * Unlock the main-level map 4253df8bae1dSRodney W. Grimes */ 4254df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 4255df8bae1dSRodney W. Grimes } 4256df8bae1dSRodney W. Grimes 4257c7c34a24SBruce Evans #include "opt_ddb.h" 4258c3cb3e12SDavid Greenman #ifdef DDB 4259c7c34a24SBruce Evans #include <sys/kernel.h> 4260c7c34a24SBruce Evans 4261c7c34a24SBruce Evans #include <ddb/ddb.h> 4262c7c34a24SBruce Evans 42632ebcd458SAttilio Rao static void 42642ebcd458SAttilio Rao vm_map_print(vm_map_t map) 4265df8bae1dSRodney W. Grimes { 4266c0877f10SJohn Dyson vm_map_entry_t entry; 4267c7c34a24SBruce Evans 4268e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 4269e5f251d2SAlan Cox (void *)map, 4270101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 4271df8bae1dSRodney W. Grimes 4272c7c34a24SBruce Evans db_indent += 2; 4273df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 4274df8bae1dSRodney W. Grimes entry = entry->next) { 427519bd0d9cSKonstantin Belousov db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n", 427619bd0d9cSKonstantin Belousov (void *)entry, (void *)entry->start, (void *)entry->end, 427719bd0d9cSKonstantin Belousov entry->eflags); 4278e5f251d2SAlan Cox { 4279df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 4280df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 42810d94caffSDavid Greenman 428295e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 4283df8bae1dSRodney W. Grimes entry->protection, 4284df8bae1dSRodney W. Grimes entry->max_protection, 42858aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 4286df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 428795e5e988SJohn Dyson db_printf(", wired"); 4288df8bae1dSRodney W. Grimes } 42899fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 4290cd034a5bSMaxime Henrion db_printf(", share=%p, offset=0x%jx\n", 42919fdfe602SMatthew Dillon (void *)entry->object.sub_map, 4292cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 4293df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 42949fdfe602SMatthew Dillon (entry->prev->object.sub_map != 42959fdfe602SMatthew Dillon entry->object.sub_map)) { 4296c7c34a24SBruce Evans db_indent += 2; 42972ebcd458SAttilio Rao vm_map_print((vm_map_t)entry->object.sub_map); 4298c7c34a24SBruce Evans db_indent -= 2; 4299df8bae1dSRodney W. Grimes } 43000d94caffSDavid Greenman } else { 4301ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) 4302ef694c1aSEdward Tomasz Napierala db_printf(", ruid %d", entry->cred->cr_ruid); 4303cd034a5bSMaxime Henrion db_printf(", object=%p, offset=0x%jx", 4304101eeb7fSBruce Evans (void *)entry->object.vm_object, 4305cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 4306ef694c1aSEdward Tomasz Napierala if (entry->object.vm_object && entry->object.vm_object->cred) 4307ef694c1aSEdward Tomasz Napierala db_printf(", obj ruid %d charge %jx", 4308ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred->cr_ruid, 43093364c323SKonstantin Belousov (uintmax_t)entry->object.vm_object->charge); 4310afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 4311c7c34a24SBruce Evans db_printf(", copy (%s)", 4312afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 4313c7c34a24SBruce Evans db_printf("\n"); 4314df8bae1dSRodney W. Grimes 4315df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 4316df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 4317df8bae1dSRodney W. Grimes entry->object.vm_object)) { 4318c7c34a24SBruce Evans db_indent += 2; 4319101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 4320101eeb7fSBruce Evans entry->object.vm_object, 432144bbc3b7SKonstantin Belousov 0, 0, (char *)0); 4322c7c34a24SBruce Evans db_indent -= 2; 4323df8bae1dSRodney W. Grimes } 4324df8bae1dSRodney W. Grimes } 4325df8bae1dSRodney W. Grimes } 4326c7c34a24SBruce Evans db_indent -= 2; 4327df8bae1dSRodney W. Grimes } 432895e5e988SJohn Dyson 43292ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map) 43302ebcd458SAttilio Rao { 43312ebcd458SAttilio Rao 43322ebcd458SAttilio Rao if (!have_addr) { 43332ebcd458SAttilio Rao db_printf("usage: show map <addr>\n"); 43342ebcd458SAttilio Rao return; 43352ebcd458SAttilio Rao } 43362ebcd458SAttilio Rao vm_map_print((vm_map_t)addr); 43372ebcd458SAttilio Rao } 433895e5e988SJohn Dyson 433995e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 434095e5e988SJohn Dyson { 434195e5e988SJohn Dyson struct proc *p; 434295e5e988SJohn Dyson 434395e5e988SJohn Dyson if (have_addr) { 4344a9546a6bSJohn Baldwin p = db_lookup_proc(addr); 434595e5e988SJohn Dyson } else { 434695e5e988SJohn Dyson p = curproc; 434795e5e988SJohn Dyson } 434895e5e988SJohn Dyson 4349ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 4350ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 4351b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 435295e5e988SJohn Dyson 43532ebcd458SAttilio Rao vm_map_print((vm_map_t)&p->p_vmspace->vm_map); 435495e5e988SJohn Dyson } 435595e5e988SJohn Dyson 4356c7c34a24SBruce Evans #endif /* DDB */ 4357