160727d8bSWarner Losh /*- 2796df753SPedro F. Giffuni * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU) 351369649SPedro F. Giffuni * 4df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 5df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 6df8bae1dSRodney W. Grimes * 7df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 8df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 9df8bae1dSRodney W. Grimes * 10df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 11df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 12df8bae1dSRodney W. Grimes * are met: 13df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 15df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 16df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 17df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 18fbbd9655SWarner Losh * 3. Neither the name of the University nor the names of its contributors 19df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 20df8bae1dSRodney W. Grimes * without specific prior written permission. 21df8bae1dSRodney W. Grimes * 22df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 23df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 26df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 28df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 30df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 31df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 32df8bae1dSRodney W. Grimes * SUCH DAMAGE. 33df8bae1dSRodney W. Grimes * 343c4dd356SDavid Greenman * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 35df8bae1dSRodney W. Grimes * 36df8bae1dSRodney W. Grimes * 37df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 38df8bae1dSRodney W. Grimes * All rights reserved. 39df8bae1dSRodney W. Grimes * 40df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 43df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 44df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 45df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 46df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 47df8bae1dSRodney W. Grimes * 48df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 49df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 50df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 51df8bae1dSRodney W. Grimes * 52df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 55df8bae1dSRodney W. Grimes * School of Computer Science 56df8bae1dSRodney W. Grimes * Carnegie Mellon University 57df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 58df8bae1dSRodney W. Grimes * 59df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 60df8bae1dSRodney W. Grimes * rights to redistribute these changes. 61df8bae1dSRodney W. Grimes */ 62df8bae1dSRodney W. Grimes 63df8bae1dSRodney W. Grimes /* 64df8bae1dSRodney W. Grimes * Virtual memory mapping module. 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67874651b1SDavid E. O'Brien #include <sys/cdefs.h> 68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$"); 69874651b1SDavid E. O'Brien 70df8bae1dSRodney W. Grimes #include <sys/param.h> 71df8bae1dSRodney W. Grimes #include <sys/systm.h> 729a6d144fSKonstantin Belousov #include <sys/kernel.h> 7361d80e90SJohn Baldwin #include <sys/ktr.h> 74fb919e4dSMark Murray #include <sys/lock.h> 75fb919e4dSMark Murray #include <sys/mutex.h> 76b5e8ce9fSBruce Evans #include <sys/proc.h> 77efeaf95aSDavid Greenman #include <sys/vmmeter.h> 78867a482dSJohn Dyson #include <sys/mman.h> 791efb74fbSJohn Dyson #include <sys/vnode.h> 801ba5ad42SEdward Tomasz Napierala #include <sys/racct.h> 812267af78SJulian Elischer #include <sys/resourcevar.h> 8289f6b863SAttilio Rao #include <sys/rwlock.h> 833fde38dfSMike Silbersack #include <sys/file.h> 849a6d144fSKonstantin Belousov #include <sys/sysctl.h> 8505ba50f5SJake Burkholder #include <sys/sysent.h> 863db161e0SMatthew Dillon #include <sys/shm.h> 87df8bae1dSRodney W. Grimes 88df8bae1dSRodney W. Grimes #include <vm/vm.h> 89efeaf95aSDavid Greenman #include <vm/vm_param.h> 90efeaf95aSDavid Greenman #include <vm/pmap.h> 91efeaf95aSDavid Greenman #include <vm/vm_map.h> 92df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 93df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 9447221757SJohn Dyson #include <vm/vm_pager.h> 9526f9a767SRodney W. Grimes #include <vm/vm_kern.h> 96efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9784110e7eSKonstantin Belousov #include <vm/vnode_pager.h> 9821cd6e62SSeigo Tanimura #include <vm/swap_pager.h> 99670d17b5SJeff Roberson #include <vm/uma.h> 100df8bae1dSRodney W. Grimes 101df8bae1dSRodney W. Grimes /* 102df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 103df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 104df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 105df8bae1dSRodney W. Grimes * memory from one map to another. 106df8bae1dSRodney W. Grimes * 107df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 108df8bae1dSRodney W. Grimes * 109df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 110e2abaaaaSAlan Cox * entries; a self-adjusting binary search tree of these 111e2abaaaaSAlan Cox * entries is used to speed up lookups. 112df8bae1dSRodney W. Grimes * 113956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 114df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 115df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 116df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 117df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 118df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 119df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 122ad5fca3bSAlan Cox * by copying VM object references from one map to 123df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 124df8bae1dSRodney W. Grimes */ 125df8bae1dSRodney W. Grimes 1263a92e5d5SAlan Cox static struct mtx map_sleep_mtx; 1278355f576SJeff Roberson static uma_zone_t mapentzone; 1288355f576SJeff Roberson static uma_zone_t kmapentzone; 1298355f576SJeff Roberson static uma_zone_t mapzone; 1308355f576SJeff Roberson static uma_zone_t vmspace_zone; 131b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags); 132b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags); 13392351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, 13492351f16SAlan Cox vm_offset_t max); 135fec29688SAlan Cox static int vm_map_alignspace(vm_map_t map, vm_object_t object, 136fec29688SAlan Cox vm_ooffset_t offset, vm_offset_t *addr, vm_size_t length, 137fec29688SAlan Cox vm_offset_t max_addr, vm_offset_t alignment); 1380b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map); 139655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry); 14003462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry); 14119bd0d9cSKonstantin Belousov static int vm_map_growstack(vm_map_t map, vm_offset_t addr, 14219bd0d9cSKonstantin Belousov vm_map_entry_t gap_entry); 143077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 144077ec27cSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags); 1458355f576SJeff Roberson #ifdef INVARIANTS 1468355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg); 1478355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg); 1488355f576SJeff Roberson #endif 1494648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, 1504648ba0aSKonstantin Belousov vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max, 1514648ba0aSKonstantin Belousov int cow); 15266cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry, 15366cd575bSAlan Cox vm_offset_t failed_addr); 154b18bfc3dSJohn Dyson 155ef694c1aSEdward Tomasz Napierala #define ENTRY_CHARGED(e) ((e)->cred != NULL || \ 156ef694c1aSEdward Tomasz Napierala ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \ 1573364c323SKonstantin Belousov !((e)->eflags & MAP_ENTRY_NEEDS_COPY))) 1583364c323SKonstantin Belousov 15957051fdcSTor Egge /* 16057051fdcSTor Egge * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type 16157051fdcSTor Egge * stable. 16257051fdcSTor Egge */ 16357051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0) 16457051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0) 16557051fdcSTor Egge 166d239bd3cSKonstantin Belousov /* 167d239bd3cSKonstantin Belousov * VM_MAP_RANGE_CHECK: [ internal use only ] 168d239bd3cSKonstantin Belousov * 169d239bd3cSKonstantin Belousov * Asserts that the starting and ending region 170d239bd3cSKonstantin Belousov * addresses fall within the valid range of the map. 171d239bd3cSKonstantin Belousov */ 172d239bd3cSKonstantin Belousov #define VM_MAP_RANGE_CHECK(map, start, end) \ 173d239bd3cSKonstantin Belousov { \ 174d239bd3cSKonstantin Belousov if (start < vm_map_min(map)) \ 175d239bd3cSKonstantin Belousov start = vm_map_min(map); \ 176d239bd3cSKonstantin Belousov if (end > vm_map_max(map)) \ 177d239bd3cSKonstantin Belousov end = vm_map_max(map); \ 178d239bd3cSKonstantin Belousov if (start > end) \ 179d239bd3cSKonstantin Belousov start = end; \ 180d239bd3cSKonstantin Belousov } 181d239bd3cSKonstantin Belousov 1826fecb26bSKonstantin Belousov /* 1836fecb26bSKonstantin Belousov * vm_map_startup: 1846fecb26bSKonstantin Belousov * 1856fecb26bSKonstantin Belousov * Initialize the vm_map module. Must be called before 1866fecb26bSKonstantin Belousov * any other vm_map routines. 1876fecb26bSKonstantin Belousov * 1886fecb26bSKonstantin Belousov * Map and entry structures are allocated from the general 1896fecb26bSKonstantin Belousov * purpose memory pool with some exceptions: 1906fecb26bSKonstantin Belousov * 1916fecb26bSKonstantin Belousov * - The kernel map and kmem submap are allocated statically. 1926fecb26bSKonstantin Belousov * - Kernel map entries are allocated out of a static pool. 1936fecb26bSKonstantin Belousov * 1946fecb26bSKonstantin Belousov * These restrictions are necessary since malloc() uses the 1956fecb26bSKonstantin Belousov * maps and requires map entries. 1966fecb26bSKonstantin Belousov */ 1976fecb26bSKonstantin Belousov 1980d94caffSDavid Greenman void 1991b40f8c0SMatthew Dillon vm_map_startup(void) 200df8bae1dSRodney W. Grimes { 2013a92e5d5SAlan Cox mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF); 2028355f576SJeff Roberson mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL, 2038355f576SJeff Roberson #ifdef INVARIANTS 2048355f576SJeff Roberson vm_map_zdtor, 2058355f576SJeff Roberson #else 2068355f576SJeff Roberson NULL, 2078355f576SJeff Roberson #endif 208f872f6eaSAlan Cox vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 2098355f576SJeff Roberson uma_prealloc(mapzone, MAX_KMAP); 210670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry), 21118aa2de5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 21218aa2de5SJeff Roberson UMA_ZONE_MTXCLASS | UMA_ZONE_VM); 213670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry), 214670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 2155df87b21SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL, 2165df87b21SJeff Roberson #ifdef INVARIANTS 2175df87b21SJeff Roberson vmspace_zdtor, 2185df87b21SJeff Roberson #else 2195df87b21SJeff Roberson NULL, 2205df87b21SJeff Roberson #endif 221f872f6eaSAlan Cox vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 2228355f576SJeff Roberson } 2238355f576SJeff Roberson 224b23f72e9SBrian Feldman static int 225b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags) 2268355f576SJeff Roberson { 2278355f576SJeff Roberson struct vmspace *vm; 2288355f576SJeff Roberson 2298355f576SJeff Roberson vm = (struct vmspace *)mem; 2308355f576SJeff Roberson 23189b57fcfSKonstantin Belousov vm->vm_map.pmap = NULL; 232b23f72e9SBrian Feldman (void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags); 233e68c64f0SKonstantin Belousov PMAP_LOCK_INIT(vmspace_pmap(vm)); 234b23f72e9SBrian Feldman return (0); 2358355f576SJeff Roberson } 2368355f576SJeff Roberson 237b23f72e9SBrian Feldman static int 238b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags) 2398355f576SJeff Roberson { 2408355f576SJeff Roberson vm_map_t map; 2418355f576SJeff Roberson 2428355f576SJeff Roberson map = (vm_map_t)mem; 243763d9566STim Kientzle memset(map, 0, sizeof(*map)); 244e30df26eSAlan Cox mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK); 245e30df26eSAlan Cox sx_init(&map->lock, "vm map (user)"); 246b23f72e9SBrian Feldman return (0); 2478355f576SJeff Roberson } 2488355f576SJeff Roberson 2498355f576SJeff Roberson #ifdef INVARIANTS 2508355f576SJeff Roberson static void 2518355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg) 2528355f576SJeff Roberson { 2538355f576SJeff Roberson struct vmspace *vm; 2548355f576SJeff Roberson 2558355f576SJeff Roberson vm = (struct vmspace *)mem; 2568355f576SJeff Roberson 2578355f576SJeff Roberson vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg); 2588355f576SJeff Roberson } 2598355f576SJeff Roberson static void 2608355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg) 2618355f576SJeff Roberson { 2628355f576SJeff Roberson vm_map_t map; 2638355f576SJeff Roberson 2648355f576SJeff Roberson map = (vm_map_t)mem; 2658355f576SJeff Roberson KASSERT(map->nentries == 0, 2668355f576SJeff Roberson ("map %p nentries == %d on free.", 2678355f576SJeff Roberson map, map->nentries)); 2688355f576SJeff Roberson KASSERT(map->size == 0, 2698355f576SJeff Roberson ("map %p size == %lu on free.", 2709eb6e519SJeff Roberson map, (unsigned long)map->size)); 2718355f576SJeff Roberson } 2728355f576SJeff Roberson #endif /* INVARIANTS */ 2738355f576SJeff Roberson 274df8bae1dSRodney W. Grimes /* 275df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 276df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 27774d1d2b7SNeel Natu * 27874d1d2b7SNeel Natu * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit(). 279df8bae1dSRodney W. Grimes */ 280df8bae1dSRodney W. Grimes struct vmspace * 28174d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit) 282df8bae1dSRodney W. Grimes { 283c0877f10SJohn Dyson struct vmspace *vm; 2840d94caffSDavid Greenman 285a163d034SWarner Losh vm = uma_zalloc(vmspace_zone, M_WAITOK); 28674d1d2b7SNeel Natu KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL")); 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 */ 337f0165b1cSKonstantin Belousov (void)vm_map_remove(&vm->vm_map, vm_map_min(&vm->vm_map), 338f0165b1cSKonstantin Belousov vm_map_max(&vm->vm_map)); 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 refcnt = vm->vm_refcnt; 39583764b44SMateusz Guzik do { 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 } 40383764b44SMateusz Guzik } while (!atomic_fcmpset_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 refcnt = vm->vm_refcnt; 44183764b44SMateusz Guzik do { 44257051fdcSTor Egge if (refcnt <= 0) { /* Avoid 0->1 transition */ 44357051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 44457051fdcSTor Egge return (NULL); 44557051fdcSTor Egge } 44683764b44SMateusz Guzik } while (!atomic_fcmpset_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; 7942203c46dSMark Johnston map->header.eflags = MAP_ENTRY_HEADER; 7959688f931SAlan Cox map->needs_wakeup = FALSE; 7963075778bSJohn Dyson map->system_map = 0; 79792351f16SAlan Cox map->pmap = pmap; 798f0165b1cSKonstantin Belousov map->header.end = min; 799f0165b1cSKonstantin Belousov map->header.start = max; 800af7cd0c5SBrian Feldman map->flags = 0; 8014e94f402SAlan Cox map->root = NULL; 802df8bae1dSRodney W. Grimes map->timestamp = 0; 803a5db445dSMax Laier map->busy = 0; 804fa50a355SKonstantin Belousov map->anon_loc = 0; 805df8bae1dSRodney W. Grimes } 806df8bae1dSRodney W. Grimes 807a18b1f1dSJason Evans void 80892351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max) 809a18b1f1dSJason Evans { 81092351f16SAlan Cox 81192351f16SAlan Cox _vm_map_init(map, pmap, min, max); 812d923c598SAlan Cox mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK); 81312c64974SMaxime Henrion sx_init(&map->lock, "user map"); 814a18b1f1dSJason Evans } 815a18b1f1dSJason Evans 816df8bae1dSRodney W. Grimes /* 817b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 818b18bfc3dSJohn Dyson * 819b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 820b18bfc3dSJohn Dyson */ 82162487bb4SJohn Dyson static void 8221b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry) 823b18bfc3dSJohn Dyson { 8242b4a2c27SAlan Cox uma_zfree(map->system_map ? kmapentzone : mapentzone, entry); 825b18bfc3dSJohn Dyson } 826b18bfc3dSJohn Dyson 827b18bfc3dSJohn Dyson /* 828df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 829df8bae1dSRodney W. Grimes * 830df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 831b28cb1caSAlfred Perlstein * No entry fields are filled in. 832df8bae1dSRodney W. Grimes */ 833f708ef1bSPoul-Henning Kamp static vm_map_entry_t 8341b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map) 835df8bae1dSRodney W. Grimes { 8361f6889a1SMatthew Dillon vm_map_entry_t new_entry; 8371f6889a1SMatthew Dillon 8382b4a2c27SAlan Cox if (map->system_map) 8392b4a2c27SAlan Cox new_entry = uma_zalloc(kmapentzone, M_NOWAIT); 8402b4a2c27SAlan Cox else 841a163d034SWarner Losh new_entry = uma_zalloc(mapentzone, M_WAITOK); 8421f6889a1SMatthew Dillon if (new_entry == NULL) 8431f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 8441f6889a1SMatthew Dillon return (new_entry); 845df8bae1dSRodney W. Grimes } 846df8bae1dSRodney W. Grimes 847df8bae1dSRodney W. Grimes /* 848794316a8SAlan Cox * vm_map_entry_set_behavior: 849794316a8SAlan Cox * 850794316a8SAlan Cox * Set the expected access behavior, either normal, random, or 851794316a8SAlan Cox * sequential. 852794316a8SAlan Cox */ 85362a59e8fSWarner Losh static inline void 854794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior) 855794316a8SAlan Cox { 856794316a8SAlan Cox entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) | 857794316a8SAlan Cox (behavior & MAP_ENTRY_BEHAV_MASK); 858794316a8SAlan Cox } 859794316a8SAlan Cox 860794316a8SAlan Cox /* 8610164e057SAlan Cox * vm_map_entry_set_max_free: 8620164e057SAlan Cox * 8630164e057SAlan Cox * Set the max_free field in a vm_map_entry. 8640164e057SAlan Cox */ 86562a59e8fSWarner Losh static inline void 8660164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry) 8670164e057SAlan Cox { 8680164e057SAlan Cox 8690164e057SAlan Cox entry->max_free = entry->adj_free; 8700164e057SAlan Cox if (entry->left != NULL && entry->left->max_free > entry->max_free) 8710164e057SAlan Cox entry->max_free = entry->left->max_free; 8720164e057SAlan Cox if (entry->right != NULL && entry->right->max_free > entry->max_free) 8730164e057SAlan Cox entry->max_free = entry->right->max_free; 8740164e057SAlan Cox } 8750164e057SAlan Cox 8760164e057SAlan Cox /* 8774e94f402SAlan Cox * vm_map_entry_splay: 8784e94f402SAlan Cox * 8790164e057SAlan Cox * The Sleator and Tarjan top-down splay algorithm with the 8800164e057SAlan Cox * following variation. Max_free must be computed bottom-up, so 8810164e057SAlan Cox * on the downward pass, maintain the left and right spines in 8820164e057SAlan Cox * reverse order. Then, make a second pass up each side to fix 8830164e057SAlan Cox * the pointers and compute max_free. The time bound is O(log n) 8840164e057SAlan Cox * amortized. 8850164e057SAlan Cox * 8860164e057SAlan Cox * The new root is the vm_map_entry containing "addr", or else an 8870164e057SAlan Cox * adjacent entry (lower or higher) if addr is not in the tree. 8880164e057SAlan Cox * 8890164e057SAlan Cox * The map must be locked, and leaves it so. 8900164e057SAlan Cox * 8910164e057SAlan Cox * Returns: the new root. 8924e94f402SAlan Cox */ 8934e94f402SAlan Cox static vm_map_entry_t 8940164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root) 8954e94f402SAlan Cox { 8960164e057SAlan Cox vm_map_entry_t llist, rlist; 8970164e057SAlan Cox vm_map_entry_t ltree, rtree; 8980164e057SAlan Cox vm_map_entry_t y; 8994e94f402SAlan Cox 9000164e057SAlan Cox /* Special case of empty tree. */ 9014e94f402SAlan Cox if (root == NULL) 9024e94f402SAlan Cox return (root); 9030164e057SAlan Cox 9040164e057SAlan Cox /* 9050164e057SAlan Cox * Pass One: Splay down the tree until we find addr or a NULL 9060164e057SAlan Cox * pointer where addr would go. llist and rlist are the two 9070164e057SAlan Cox * sides in reverse order (bottom-up), with llist linked by 9080164e057SAlan Cox * the right pointer and rlist linked by the left pointer in 9090164e057SAlan Cox * the vm_map_entry. Wait until Pass Two to set max_free on 9100164e057SAlan Cox * the two spines. 9110164e057SAlan Cox */ 9120164e057SAlan Cox llist = NULL; 9130164e057SAlan Cox rlist = NULL; 9140164e057SAlan Cox for (;;) { 9150164e057SAlan Cox /* root is never NULL in here. */ 9160164e057SAlan Cox if (addr < root->start) { 9170164e057SAlan Cox y = root->left; 9180164e057SAlan Cox if (y == NULL) 9194e94f402SAlan Cox break; 9200164e057SAlan Cox if (addr < y->start && y->left != NULL) { 9210164e057SAlan Cox /* Rotate right and put y on rlist. */ 9224e94f402SAlan Cox root->left = y->right; 9234e94f402SAlan Cox y->right = root; 9240164e057SAlan Cox vm_map_entry_set_max_free(root); 9250164e057SAlan Cox root = y->left; 9260164e057SAlan Cox y->left = rlist; 9270164e057SAlan Cox rlist = y; 9280164e057SAlan Cox } else { 9290164e057SAlan Cox /* Put root on rlist. */ 9300164e057SAlan Cox root->left = rlist; 9310164e057SAlan Cox rlist = root; 9324e94f402SAlan Cox root = y; 9334e94f402SAlan Cox } 9347438d60bSAlan Cox } else if (addr >= root->end) { 9350164e057SAlan Cox y = root->right; 9367438d60bSAlan Cox if (y == NULL) 9374e94f402SAlan Cox break; 9380164e057SAlan Cox if (addr >= y->end && y->right != NULL) { 9390164e057SAlan Cox /* Rotate left and put y on llist. */ 9404e94f402SAlan Cox root->right = y->left; 9414e94f402SAlan Cox y->left = root; 9420164e057SAlan Cox vm_map_entry_set_max_free(root); 9430164e057SAlan Cox root = y->right; 9440164e057SAlan Cox y->right = llist; 9450164e057SAlan Cox llist = y; 9460164e057SAlan Cox } else { 9470164e057SAlan Cox /* Put root on llist. */ 9480164e057SAlan Cox root->right = llist; 9490164e057SAlan Cox llist = root; 9504e94f402SAlan Cox root = y; 9514e94f402SAlan Cox } 9527438d60bSAlan Cox } else 9537438d60bSAlan Cox break; 9540164e057SAlan Cox } 9550164e057SAlan Cox 9560164e057SAlan Cox /* 9570164e057SAlan Cox * Pass Two: Walk back up the two spines, flip the pointers 9580164e057SAlan Cox * and set max_free. The subtrees of the root go at the 9590164e057SAlan Cox * bottom of llist and rlist. 9600164e057SAlan Cox */ 9610164e057SAlan Cox ltree = root->left; 9620164e057SAlan Cox while (llist != NULL) { 9630164e057SAlan Cox y = llist->right; 9640164e057SAlan Cox llist->right = ltree; 9650164e057SAlan Cox vm_map_entry_set_max_free(llist); 9660164e057SAlan Cox ltree = llist; 9670164e057SAlan Cox llist = y; 9680164e057SAlan Cox } 9690164e057SAlan Cox rtree = root->right; 9700164e057SAlan Cox while (rlist != NULL) { 9710164e057SAlan Cox y = rlist->left; 9720164e057SAlan Cox rlist->left = rtree; 9730164e057SAlan Cox vm_map_entry_set_max_free(rlist); 9740164e057SAlan Cox rtree = rlist; 9750164e057SAlan Cox rlist = y; 9760164e057SAlan Cox } 9770164e057SAlan Cox 9780164e057SAlan Cox /* 9790164e057SAlan Cox * Final assembly: add ltree and rtree as subtrees of root. 9800164e057SAlan Cox */ 9810164e057SAlan Cox root->left = ltree; 9820164e057SAlan Cox root->right = rtree; 9830164e057SAlan Cox vm_map_entry_set_max_free(root); 9840164e057SAlan Cox 9854e94f402SAlan Cox return (root); 9864e94f402SAlan Cox } 9874e94f402SAlan Cox 9884e94f402SAlan Cox /* 989df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 990df8bae1dSRodney W. Grimes * 991df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 992df8bae1dSRodney W. Grimes */ 9934e94f402SAlan Cox static void 99499c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 99599c81ca9SAlan Cox vm_map_entry_t after_where, 99699c81ca9SAlan Cox vm_map_entry_t entry) 99799c81ca9SAlan Cox { 99821c641b2SJohn Baldwin 99921c641b2SJohn Baldwin CTR4(KTR_VM, 100021c641b2SJohn Baldwin "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 100121c641b2SJohn Baldwin map->nentries, entry, after_where); 10023a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 10031c5196c3SKonstantin Belousov KASSERT(after_where->end <= entry->start, 10045831f5fcSKonstantin Belousov ("vm_map_entry_link: prev end %jx new start %jx overlap", 10055831f5fcSKonstantin Belousov (uintmax_t)after_where->end, (uintmax_t)entry->start)); 10061c5196c3SKonstantin Belousov KASSERT(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 } 10281c5196c3SKonstantin Belousov entry->adj_free = entry->next->start - entry->end; 10290164e057SAlan Cox vm_map_entry_set_max_free(entry); 10304e94f402SAlan Cox map->root = entry; 1031df8bae1dSRodney W. Grimes } 103299c81ca9SAlan Cox 10334e94f402SAlan Cox static void 103499c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 103599c81ca9SAlan Cox vm_map_entry_t entry) 103699c81ca9SAlan Cox { 10374e94f402SAlan Cox vm_map_entry_t next, prev, root; 103899c81ca9SAlan Cox 10393a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 10404e94f402SAlan Cox if (entry != map->root) 10414e94f402SAlan Cox vm_map_entry_splay(entry->start, map->root); 10424e94f402SAlan Cox if (entry->left == NULL) 10434e94f402SAlan Cox root = entry->right; 10444e94f402SAlan Cox else { 10454e94f402SAlan Cox root = vm_map_entry_splay(entry->start, entry->left); 10464e94f402SAlan Cox root->right = entry->right; 10471c5196c3SKonstantin Belousov root->adj_free = entry->next->start - root->end; 10480164e057SAlan Cox vm_map_entry_set_max_free(root); 10494e94f402SAlan Cox } 10504e94f402SAlan Cox map->root = root; 10514e94f402SAlan Cox 10524e94f402SAlan Cox prev = entry->prev; 10534e94f402SAlan Cox next = entry->next; 105499c81ca9SAlan Cox next->prev = prev; 105599c81ca9SAlan Cox prev->next = next; 105699c81ca9SAlan Cox map->nentries--; 105721c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 105821c641b2SJohn Baldwin map->nentries, entry); 1059df8bae1dSRodney W. Grimes } 1060df8bae1dSRodney W. Grimes 1061df8bae1dSRodney W. Grimes /* 10620164e057SAlan Cox * vm_map_entry_resize_free: 10630164e057SAlan Cox * 10640164e057SAlan Cox * Recompute the amount of free space following a vm_map_entry 10650164e057SAlan Cox * and propagate that value up the tree. Call this function after 10660164e057SAlan Cox * resizing a map entry in-place, that is, without a call to 10670164e057SAlan Cox * vm_map_entry_link() or _unlink(). 10680164e057SAlan Cox * 10690164e057SAlan Cox * The map must be locked, and leaves it so. 10700164e057SAlan Cox */ 10710164e057SAlan Cox static void 10720164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry) 10730164e057SAlan Cox { 10740164e057SAlan Cox 10750164e057SAlan Cox /* 10760164e057SAlan Cox * Using splay trees without parent pointers, propagating 10770164e057SAlan Cox * max_free up the tree is done by moving the entry to the 10780164e057SAlan Cox * root and making the change there. 10790164e057SAlan Cox */ 10800164e057SAlan Cox if (entry != map->root) 10810164e057SAlan Cox map->root = vm_map_entry_splay(entry->start, map->root); 10820164e057SAlan Cox 10831c5196c3SKonstantin Belousov entry->adj_free = entry->next->start - entry->end; 10840164e057SAlan Cox vm_map_entry_set_max_free(entry); 10850164e057SAlan Cox } 10860164e057SAlan Cox 10870164e057SAlan Cox /* 1088df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 1089df8bae1dSRodney W. Grimes * 1090df8bae1dSRodney W. Grimes * Finds the map entry containing (or 1091df8bae1dSRodney W. Grimes * immediately preceding) the specified address 1092df8bae1dSRodney W. Grimes * in the given map; the entry is returned 1093df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 1094df8bae1dSRodney W. Grimes * result indicates whether the address is 1095df8bae1dSRodney W. Grimes * actually contained in the map. 1096df8bae1dSRodney W. Grimes */ 10970d94caffSDavid Greenman boolean_t 10981b40f8c0SMatthew Dillon vm_map_lookup_entry( 10991b40f8c0SMatthew Dillon vm_map_t map, 11001b40f8c0SMatthew Dillon vm_offset_t address, 11011b40f8c0SMatthew Dillon vm_map_entry_t *entry) /* OUT */ 1102df8bae1dSRodney W. Grimes { 1103c0877f10SJohn Dyson vm_map_entry_t cur; 110405a8c414SAlan Cox boolean_t locked; 1105df8bae1dSRodney W. Grimes 11064c3ef59eSAlan Cox /* 11074c3ef59eSAlan Cox * If the map is empty, then the map entry immediately preceding 11084c3ef59eSAlan Cox * "address" is the map's header. 11094c3ef59eSAlan Cox */ 11104c3ef59eSAlan Cox cur = map->root; 11114e94f402SAlan Cox if (cur == NULL) 11124e94f402SAlan Cox *entry = &map->header; 11134c3ef59eSAlan Cox else if (address >= cur->start && cur->end > address) { 11144c3ef59eSAlan Cox *entry = cur; 11154c3ef59eSAlan Cox return (TRUE); 111605a8c414SAlan Cox } else if ((locked = vm_map_locked(map)) || 111705a8c414SAlan Cox sx_try_upgrade(&map->lock)) { 111805a8c414SAlan Cox /* 111905a8c414SAlan Cox * Splay requires a write lock on the map. However, it only 112005a8c414SAlan Cox * restructures the binary search tree; it does not otherwise 112105a8c414SAlan Cox * change the map. Thus, the map's timestamp need not change 112205a8c414SAlan Cox * on a temporary upgrade. 112305a8c414SAlan Cox */ 11244c3ef59eSAlan Cox map->root = cur = vm_map_entry_splay(address, cur); 112505a8c414SAlan Cox if (!locked) 112605a8c414SAlan Cox sx_downgrade(&map->lock); 1127df8bae1dSRodney W. Grimes 11284c3ef59eSAlan Cox /* 11294c3ef59eSAlan Cox * If "address" is contained within a map entry, the new root 11304c3ef59eSAlan Cox * is that map entry. Otherwise, the new root is a map entry 11314c3ef59eSAlan Cox * immediately before or after "address". 11324c3ef59eSAlan Cox */ 1133df8bae1dSRodney W. Grimes if (address >= cur->start) { 1134df8bae1dSRodney W. Grimes *entry = cur; 11354e94f402SAlan Cox if (cur->end > address) 1136df8bae1dSRodney W. Grimes return (TRUE); 11374e94f402SAlan Cox } else 1138df8bae1dSRodney W. Grimes *entry = cur->prev; 113905a8c414SAlan Cox } else 114005a8c414SAlan Cox /* 114105a8c414SAlan Cox * Since the map is only locked for read access, perform a 114205a8c414SAlan Cox * standard binary search tree lookup for "address". 114305a8c414SAlan Cox */ 114405a8c414SAlan Cox for (;;) { 114505a8c414SAlan Cox if (address < cur->start) { 114605a8c414SAlan Cox if (cur->left == NULL) { 114705a8c414SAlan Cox *entry = cur->prev; 114805a8c414SAlan Cox break; 114905a8c414SAlan Cox } 115005a8c414SAlan Cox cur = cur->left; 115105a8c414SAlan Cox } else if (cur->end > address) { 115205a8c414SAlan Cox *entry = cur; 115305a8c414SAlan Cox return (TRUE); 115405a8c414SAlan Cox } else { 115505a8c414SAlan Cox if (cur->right == NULL) { 115605a8c414SAlan Cox *entry = cur; 115705a8c414SAlan Cox break; 115805a8c414SAlan Cox } 115905a8c414SAlan Cox cur = cur->right; 116005a8c414SAlan Cox } 11614e94f402SAlan Cox } 1162df8bae1dSRodney W. Grimes return (FALSE); 1163df8bae1dSRodney W. Grimes } 1164df8bae1dSRodney W. Grimes 1165df8bae1dSRodney W. Grimes /* 116630dcfc09SJohn Dyson * vm_map_insert: 116730dcfc09SJohn Dyson * 116830dcfc09SJohn Dyson * Inserts the given whole VM object into the target 116930dcfc09SJohn Dyson * map at the specified address range. The object's 117030dcfc09SJohn Dyson * size should match that of the address range. 117130dcfc09SJohn Dyson * 117230dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 11732aaeadf8SMatthew Dillon * 11742aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 11752aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 117630dcfc09SJohn Dyson */ 117730dcfc09SJohn Dyson int 1178b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 117933314db0SAlan Cox vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow) 118030dcfc09SJohn Dyson { 118133314db0SAlan Cox vm_map_entry_t new_entry, prev_entry, temp_entry; 1182ef694c1aSEdward Tomasz Napierala struct ucred *cred; 11831569205fSKonstantin Belousov vm_eflags_t protoeflags; 11848211bd45SKonstantin Belousov vm_inherit_t inheritance; 118530dcfc09SJohn Dyson 11863a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 11872e47807cSJeff Roberson KASSERT(object != kernel_object || 118833314db0SAlan Cox (cow & MAP_COPY_ON_WRITE) == 0, 11892e47807cSJeff Roberson ("vm_map_insert: kernel object and COW")); 119033314db0SAlan Cox KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0, 119133314db0SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 119200de6773SKonstantin Belousov KASSERT((prot & ~max) == 0, 119300de6773SKonstantin Belousov ("prot %#x is not subset of max_prot %#x", prot, max)); 11943a0916b8SKonstantin Belousov 119530dcfc09SJohn Dyson /* 119630dcfc09SJohn Dyson * Check that the start and end points are not bogus. 119730dcfc09SJohn Dyson */ 1198f0165b1cSKonstantin Belousov if (start < vm_map_min(map) || end > vm_map_max(map) || 1199f0165b1cSKonstantin Belousov start >= end) 120030dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 120130dcfc09SJohn Dyson 120230dcfc09SJohn Dyson /* 120330dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 120430dcfc09SJohn Dyson * of an existing entry, this range is bogus. 120530dcfc09SJohn Dyson */ 120630dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 120730dcfc09SJohn Dyson return (KERN_NO_SPACE); 120830dcfc09SJohn Dyson 120930dcfc09SJohn Dyson prev_entry = temp_entry; 121030dcfc09SJohn Dyson 121130dcfc09SJohn Dyson /* 121230dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 121330dcfc09SJohn Dyson */ 12141c5196c3SKonstantin Belousov if (prev_entry->next->start < end) 121530dcfc09SJohn Dyson return (KERN_NO_SPACE); 121630dcfc09SJohn Dyson 121719bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL || 121819bd0d9cSKonstantin Belousov max != VM_PROT_NONE)) 121919bd0d9cSKonstantin Belousov return (KERN_INVALID_ARGUMENT); 122019bd0d9cSKonstantin Belousov 1221afa07f7eSJohn Dyson protoeflags = 0; 1222afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 1223e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY; 122433314db0SAlan Cox if (cow & MAP_NOFAULT) 1225afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 12264f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 12274f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 12289730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 12299730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 1230712efe66SAlan Cox if (cow & MAP_STACK_GROWS_DOWN) 1231712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_DOWN; 1232712efe66SAlan Cox if (cow & MAP_STACK_GROWS_UP) 1233712efe66SAlan Cox protoeflags |= MAP_ENTRY_GROWS_UP; 123484110e7eSKonstantin Belousov if (cow & MAP_VN_WRITECOUNT) 123584110e7eSKonstantin Belousov protoeflags |= MAP_ENTRY_VN_WRITECNT; 123619bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_GUARD) != 0) 123719bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_GUARD; 123819bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_DN) != 0) 123919bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_DN; 124019bd0d9cSKonstantin Belousov if ((cow & MAP_CREATE_STACK_GAP_UP) != 0) 124119bd0d9cSKonstantin Belousov protoeflags |= MAP_ENTRY_STACK_GAP_UP; 12428211bd45SKonstantin Belousov if (cow & MAP_INHERIT_SHARE) 12438211bd45SKonstantin Belousov inheritance = VM_INHERIT_SHARE; 12448211bd45SKonstantin Belousov else 12458211bd45SKonstantin Belousov inheritance = VM_INHERIT_DEFAULT; 12464f79d873SMatthew Dillon 1247ef694c1aSEdward Tomasz Napierala cred = NULL; 124819bd0d9cSKonstantin Belousov if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0) 12493364c323SKonstantin Belousov goto charged; 12503364c323SKonstantin Belousov if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) && 12513364c323SKonstantin Belousov ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) { 12523364c323SKonstantin Belousov if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start)) 12533364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 12541569205fSKonstantin Belousov KASSERT(object == NULL || 12551569205fSKonstantin Belousov (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 || 1256ef694c1aSEdward Tomasz Napierala object->cred == NULL, 12571569205fSKonstantin Belousov ("overcommit: vm_map_insert o %p", object)); 1258ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 12593364c323SKonstantin Belousov } 12603364c323SKonstantin Belousov 12613364c323SKonstantin Belousov charged: 1262f8616ebfSAlan Cox /* Expand the kernel pmap, if necessary. */ 1263f8616ebfSAlan Cox if (map == kernel_map && end > kernel_vm_end) 1264f8616ebfSAlan Cox pmap_growkernel(end); 12651d284e00SAlan Cox if (object != NULL) { 126630dcfc09SJohn Dyson /* 12671d284e00SAlan Cox * OBJ_ONEMAPPING must be cleared unless this mapping 12681d284e00SAlan Cox * is trivially proven to be the only mapping for any 12691d284e00SAlan Cox * of the object's pages. (Object granularity 12701d284e00SAlan Cox * reference counting is insufficient to recognize 12711d284e00SAlan Cox * aliases with precision.) 127230dcfc09SJohn Dyson */ 127389f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 12741d284e00SAlan Cox if (object->ref_count > 1 || object->shadow_count != 0) 12752aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 127689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 12772203c46dSMark Johnston } else if ((prev_entry->eflags & ~MAP_ENTRY_USER_WIRED) == 12782203c46dSMark Johnston protoeflags && 1279b5f8c226SKonstantin Belousov (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 && 1280737e25f7SAlan Cox prev_entry->end == start && (prev_entry->cred == cred || 12813364c323SKonstantin Belousov (prev_entry->object.vm_object != NULL && 12821569205fSKonstantin Belousov prev_entry->object.vm_object->cred == cred)) && 12838cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 128457a21abaSAlan Cox prev_entry->offset, 12858cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 128660169c88SAlan Cox (vm_size_t)(end - prev_entry->end), cred != NULL && 128760169c88SAlan Cox (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) { 128830dcfc09SJohn Dyson /* 12892aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 12902aaeadf8SMatthew Dillon * can extend the previous map entry to include the 12912aaeadf8SMatthew Dillon * new range as well. 129230dcfc09SJohn Dyson */ 12931569205fSKonstantin Belousov if (prev_entry->inheritance == inheritance && 12941569205fSKonstantin Belousov prev_entry->protection == prot && 1295737e25f7SAlan Cox prev_entry->max_protection == max && 1296737e25f7SAlan Cox prev_entry->wired_count == 0) { 1297737e25f7SAlan Cox KASSERT((prev_entry->eflags & MAP_ENTRY_USER_WIRED) == 1298737e25f7SAlan Cox 0, ("prev_entry %p has incoherent wiring", 1299737e25f7SAlan Cox prev_entry)); 130019bd0d9cSKonstantin Belousov if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0) 13011569205fSKonstantin Belousov map->size += end - prev_entry->end; 130230dcfc09SJohn Dyson prev_entry->end = end; 13030164e057SAlan Cox vm_map_entry_resize_free(map, prev_entry); 13044e71e795SMatthew Dillon vm_map_simplify_entry(map, prev_entry); 130530dcfc09SJohn Dyson return (KERN_SUCCESS); 130630dcfc09SJohn Dyson } 13078cc7e047SJohn Dyson 13082aaeadf8SMatthew Dillon /* 13092aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 13102aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 13112aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 13124e71e795SMatthew Dillon * account for it. object may be NULL. 13132aaeadf8SMatthew Dillon */ 13142aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 13152aaeadf8SMatthew Dillon offset = prev_entry->offset + 13162aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 13178cc7e047SJohn Dyson vm_object_reference(object); 1318ef694c1aSEdward Tomasz Napierala if (cred != NULL && object != NULL && object->cred != NULL && 13193364c323SKonstantin Belousov !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) { 13203364c323SKonstantin Belousov /* Object already accounts for this uid. */ 1321ef694c1aSEdward Tomasz Napierala cred = NULL; 13223364c323SKonstantin Belousov } 1323b18bfc3dSJohn Dyson } 132460169c88SAlan Cox if (cred != NULL) 132560169c88SAlan Cox crhold(cred); 13262aaeadf8SMatthew Dillon 13272aaeadf8SMatthew Dillon /* 132830dcfc09SJohn Dyson * Create a new entry 132930dcfc09SJohn Dyson */ 133030dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 133130dcfc09SJohn Dyson new_entry->start = start; 133230dcfc09SJohn Dyson new_entry->end = end; 1333ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL; 133430dcfc09SJohn Dyson 1335afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 133630dcfc09SJohn Dyson new_entry->object.vm_object = object; 133730dcfc09SJohn Dyson new_entry->offset = offset; 13382267af78SJulian Elischer 13398211bd45SKonstantin Belousov new_entry->inheritance = inheritance; 134030dcfc09SJohn Dyson new_entry->protection = prot; 134130dcfc09SJohn Dyson new_entry->max_protection = max; 134230dcfc09SJohn Dyson new_entry->wired_count = 0; 1343997ac690SKonstantin Belousov new_entry->wiring_thread = NULL; 134413458803SAlan Cox new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT; 1345381b7242SAlan Cox new_entry->next_read = start; 1346e5f251d2SAlan Cox 1347ef694c1aSEdward Tomasz Napierala KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry), 13481569205fSKonstantin Belousov ("overcommit: vm_map_insert leaks vm_map %p", new_entry)); 1349ef694c1aSEdward Tomasz Napierala new_entry->cred = cred; 13503364c323SKonstantin Belousov 135130dcfc09SJohn Dyson /* 135230dcfc09SJohn Dyson * Insert the new entry into the list 135330dcfc09SJohn Dyson */ 135430dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 135519bd0d9cSKonstantin Belousov if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0) 135630dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 135730dcfc09SJohn Dyson 13581a484d28SMatthew Dillon /* 1359eaaf9f7fSAlan Cox * Try to coalesce the new entry with both the previous and next 1360eaaf9f7fSAlan Cox * entries in the list. Previously, we only attempted to coalesce 1361eaaf9f7fSAlan Cox * with the previous entry when object is NULL. Here, we handle the 1362eaaf9f7fSAlan Cox * other cases, which are less common. 13631a484d28SMatthew Dillon */ 13644e71e795SMatthew Dillon vm_map_simplify_entry(map, new_entry); 13654e71e795SMatthew Dillon 13661569205fSKonstantin Belousov if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) { 13671569205fSKonstantin Belousov vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset), 13681569205fSKonstantin Belousov end - start, cow & MAP_PREFAULT_PARTIAL); 13694f79d873SMatthew Dillon } 1370e972780aSAlan Cox 137130dcfc09SJohn Dyson return (KERN_SUCCESS); 137230dcfc09SJohn Dyson } 137330dcfc09SJohn Dyson 137430dcfc09SJohn Dyson /* 13750164e057SAlan Cox * vm_map_findspace: 13760164e057SAlan Cox * 13770164e057SAlan Cox * Find the first fit (lowest VM address) for "length" free bytes 13780164e057SAlan Cox * beginning at address >= start in the given map. 13790164e057SAlan Cox * 13800164e057SAlan Cox * In a vm_map_entry, "adj_free" is the amount of free space 13810164e057SAlan Cox * adjacent (higher address) to this entry, and "max_free" is the 13820164e057SAlan Cox * maximum amount of contiguous free space in its subtree. This 13830164e057SAlan Cox * allows finding a free region in one path down the tree, so 13840164e057SAlan Cox * O(log n) amortized with splay trees. 13850164e057SAlan Cox * 13860164e057SAlan Cox * The map must be locked, and leaves it so. 13870164e057SAlan Cox * 13880164e057SAlan Cox * Returns: 0 on success, and starting address in *addr, 13890164e057SAlan Cox * 1 if insufficient space. 1390df8bae1dSRodney W. Grimes */ 1391df8bae1dSRodney W. Grimes int 13920164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length, 13930164e057SAlan Cox vm_offset_t *addr) /* OUT */ 1394df8bae1dSRodney W. Grimes { 13950164e057SAlan Cox vm_map_entry_t entry; 1396f8616ebfSAlan Cox vm_offset_t st; 1397df8bae1dSRodney W. Grimes 1398986b43f8SAlan Cox /* 1399986b43f8SAlan Cox * Request must fit within min/max VM address and must avoid 1400986b43f8SAlan Cox * address wrap. 1401986b43f8SAlan Cox */ 1402f0165b1cSKonstantin Belousov start = MAX(start, vm_map_min(map)); 1403f0165b1cSKonstantin Belousov if (start + length > vm_map_max(map) || 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 1484fa50a355SKonstantin Belousov static const int aslr_pages_rnd_64[2] = {0x1000, 0x10}; 1485fa50a355SKonstantin Belousov static const int aslr_pages_rnd_32[2] = {0x100, 0x4}; 1486fa50a355SKonstantin Belousov 1487fa50a355SKonstantin Belousov static int cluster_anon = 1; 1488fa50a355SKonstantin Belousov SYSCTL_INT(_vm, OID_AUTO, cluster_anon, CTLFLAG_RW, 1489fa50a355SKonstantin Belousov &cluster_anon, 0, 1490*484e9d03SKonstantin Belousov "Cluster anonymous mappings: 0 = no, 1 = yes if no hint, 2 = always"); 1491*484e9d03SKonstantin Belousov 1492*484e9d03SKonstantin Belousov static bool 1493*484e9d03SKonstantin Belousov clustering_anon_allowed(vm_offset_t addr) 1494*484e9d03SKonstantin Belousov { 1495*484e9d03SKonstantin Belousov 1496*484e9d03SKonstantin Belousov switch (cluster_anon) { 1497*484e9d03SKonstantin Belousov case 0: 1498*484e9d03SKonstantin Belousov return (false); 1499*484e9d03SKonstantin Belousov case 1: 1500*484e9d03SKonstantin Belousov return (addr == 0); 1501*484e9d03SKonstantin Belousov case 2: 1502*484e9d03SKonstantin Belousov default: 1503*484e9d03SKonstantin Belousov return (true); 1504*484e9d03SKonstantin Belousov } 1505*484e9d03SKonstantin Belousov } 1506fa50a355SKonstantin Belousov 1507fa50a355SKonstantin Belousov static long aslr_restarts; 1508fa50a355SKonstantin Belousov SYSCTL_LONG(_vm, OID_AUTO, aslr_restarts, CTLFLAG_RD, 1509fa50a355SKonstantin Belousov &aslr_restarts, 0, 1510fa50a355SKonstantin Belousov "Number of aslr failures"); 1511fa50a355SKonstantin Belousov 1512fa50a355SKonstantin Belousov #define MAP_32BIT_MAX_ADDR ((vm_offset_t)1 << 31) 1513fa50a355SKonstantin Belousov 1514df8bae1dSRodney W. Grimes /* 1515fec29688SAlan Cox * Searches for the specified amount of free space in the given map with the 1516fec29688SAlan Cox * specified alignment. Performs an address-ordered, first-fit search from 1517fec29688SAlan Cox * the given address "*addr", with an optional upper bound "max_addr". If the 1518fec29688SAlan Cox * parameter "alignment" is zero, then the alignment is computed from the 1519fec29688SAlan Cox * given (object, offset) pair so as to enable the greatest possible use of 1520fec29688SAlan Cox * superpage mappings. Returns KERN_SUCCESS and the address of the free space 1521fec29688SAlan Cox * in "*addr" if successful. Otherwise, returns KERN_NO_SPACE. 1522fec29688SAlan Cox * 1523fec29688SAlan Cox * The map must be locked. Initially, there must be at least "length" bytes 1524fec29688SAlan Cox * of free space at the given address. 1525fec29688SAlan Cox */ 1526fec29688SAlan Cox static int 1527fec29688SAlan Cox vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1528fec29688SAlan Cox vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr, 1529fec29688SAlan Cox vm_offset_t alignment) 1530fec29688SAlan Cox { 1531fec29688SAlan Cox vm_offset_t aligned_addr, free_addr; 1532fec29688SAlan Cox 1533fec29688SAlan Cox VM_MAP_ASSERT_LOCKED(map); 1534fec29688SAlan Cox free_addr = *addr; 1535fec29688SAlan Cox KASSERT(!vm_map_findspace(map, free_addr, length, addr) && 1536fec29688SAlan Cox free_addr == *addr, ("caller provided insufficient free space")); 1537fec29688SAlan Cox for (;;) { 1538fec29688SAlan Cox /* 1539fec29688SAlan Cox * At the start of every iteration, the free space at address 1540fec29688SAlan Cox * "*addr" is at least "length" bytes. 1541fec29688SAlan Cox */ 1542fec29688SAlan Cox if (alignment == 0) 1543fec29688SAlan Cox pmap_align_superpage(object, offset, addr, length); 1544fec29688SAlan Cox else if ((*addr & (alignment - 1)) != 0) { 1545fec29688SAlan Cox *addr &= ~(alignment - 1); 1546fec29688SAlan Cox *addr += alignment; 1547fec29688SAlan Cox } 1548fec29688SAlan Cox aligned_addr = *addr; 1549fec29688SAlan Cox if (aligned_addr == free_addr) { 1550fec29688SAlan Cox /* 1551fec29688SAlan Cox * Alignment did not change "*addr", so "*addr" must 1552fec29688SAlan Cox * still provide sufficient free space. 1553fec29688SAlan Cox */ 1554fec29688SAlan Cox return (KERN_SUCCESS); 1555fec29688SAlan Cox } 1556fec29688SAlan Cox 1557fec29688SAlan Cox /* 1558fec29688SAlan Cox * Test for address wrap on "*addr". A wrapped "*addr" could 1559fec29688SAlan Cox * be a valid address, in which case vm_map_findspace() cannot 1560fec29688SAlan Cox * be relied upon to fail. 1561fec29688SAlan Cox */ 1562fec29688SAlan Cox if (aligned_addr < free_addr || 1563fec29688SAlan Cox vm_map_findspace(map, aligned_addr, length, addr) || 1564fec29688SAlan Cox (max_addr != 0 && *addr + length > max_addr)) 1565fec29688SAlan Cox return (KERN_NO_SPACE); 1566fec29688SAlan Cox free_addr = *addr; 1567fec29688SAlan Cox if (free_addr == aligned_addr) { 1568fec29688SAlan Cox /* 1569fec29688SAlan Cox * If a successful call to vm_map_findspace() did not 1570fec29688SAlan Cox * change "*addr", then "*addr" must still be aligned 1571fec29688SAlan Cox * and provide sufficient free space. 1572fec29688SAlan Cox */ 1573fec29688SAlan Cox return (KERN_SUCCESS); 1574fec29688SAlan Cox } 1575fec29688SAlan Cox } 1576fec29688SAlan Cox } 1577fec29688SAlan Cox 1578fec29688SAlan Cox /* 1579df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 1580df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 1581df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 1582df8bae1dSRodney W. Grimes * returned in the same parameter. 1583df8bae1dSRodney W. Grimes * 15842aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 15852aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 1586df8bae1dSRodney W. Grimes */ 1587df8bae1dSRodney W. Grimes int 1588b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 1589b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 1590edb572a3SJohn Baldwin vm_size_t length, vm_offset_t max_addr, int find_space, 1591edb572a3SJohn Baldwin vm_prot_t prot, vm_prot_t max, int cow) 1592df8bae1dSRodney W. Grimes { 1593fa50a355SKonstantin Belousov vm_offset_t alignment, curr_min_addr, min_addr; 1594fa50a355SKonstantin Belousov int gap, pidx, rv, try; 1595fa50a355SKonstantin Belousov bool cluster, en_aslr, update_anon; 1596df8bae1dSRodney W. Grimes 15974648ba0aSKonstantin Belousov KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 || 15984648ba0aSKonstantin Belousov object == NULL, 15994648ba0aSKonstantin Belousov ("vm_map_find: non-NULL backing object for stack")); 1600ea7e7006SKonstantin Belousov MPASS((cow & MAP_REMAP) == 0 || (find_space == VMFS_NO_SPACE && 1601ea7e7006SKonstantin Belousov (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0)); 1602ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL || 1603ff74a3faSJohn Baldwin (object->flags & OBJ_COLORED) == 0)) 1604ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE; 16055aa60b6fSJohn Baldwin if (find_space >> 8 != 0) { 16065aa60b6fSJohn Baldwin KASSERT((find_space & 0xff) == 0, ("bad VMFS flags")); 16075aa60b6fSJohn Baldwin alignment = (vm_offset_t)1 << (find_space >> 8); 16085aa60b6fSJohn Baldwin } else 16095aa60b6fSJohn Baldwin alignment = 0; 1610fa50a355SKonstantin Belousov en_aslr = (map->flags & MAP_ASLR) != 0; 1611*484e9d03SKonstantin Belousov update_anon = cluster = clustering_anon_allowed(*addr) && 1612fa50a355SKonstantin Belousov (map->flags & MAP_IS_SUB_MAP) == 0 && max_addr == 0 && 1613fa50a355SKonstantin Belousov find_space != VMFS_NO_SPACE && object == NULL && 1614fa50a355SKonstantin Belousov (cow & (MAP_INHERIT_SHARE | MAP_STACK_GROWS_UP | 1615fa50a355SKonstantin Belousov MAP_STACK_GROWS_DOWN)) == 0 && prot != PROT_NONE; 1616fa50a355SKonstantin Belousov curr_min_addr = min_addr = *addr; 1617fa50a355SKonstantin Belousov if (en_aslr && min_addr == 0 && !cluster && 1618fa50a355SKonstantin Belousov find_space != VMFS_NO_SPACE && 1619fa50a355SKonstantin Belousov (map->flags & MAP_ASLR_IGNSTART) != 0) 1620fa50a355SKonstantin Belousov curr_min_addr = min_addr = vm_map_min(map); 1621fa50a355SKonstantin Belousov try = 0; 16224d572bb3SAlan Cox vm_map_lock(map); 1623fa50a355SKonstantin Belousov if (cluster) { 1624fa50a355SKonstantin Belousov curr_min_addr = map->anon_loc; 1625fa50a355SKonstantin Belousov if (curr_min_addr == 0) 1626fa50a355SKonstantin Belousov cluster = false; 1627fa50a355SKonstantin Belousov } 162826c538ffSAlan Cox if (find_space != VMFS_NO_SPACE) { 1629fec29688SAlan Cox KASSERT(find_space == VMFS_ANY_SPACE || 1630fec29688SAlan Cox find_space == VMFS_OPTIMAL_SPACE || 1631fec29688SAlan Cox find_space == VMFS_SUPER_SPACE || 1632fec29688SAlan Cox alignment != 0, ("unexpected VMFS flag")); 1633fec29688SAlan Cox again: 1634fa50a355SKonstantin Belousov /* 1635fa50a355SKonstantin Belousov * When creating an anonymous mapping, try clustering 1636fa50a355SKonstantin Belousov * with an existing anonymous mapping first. 1637fa50a355SKonstantin Belousov * 1638fa50a355SKonstantin Belousov * We make up to two attempts to find address space 1639fa50a355SKonstantin Belousov * for a given find_space value. The first attempt may 1640fa50a355SKonstantin Belousov * apply randomization or may cluster with an existing 1641fa50a355SKonstantin Belousov * anonymous mapping. If this first attempt fails, 1642fa50a355SKonstantin Belousov * perform a first-fit search of the available address 1643fa50a355SKonstantin Belousov * space. 1644fa50a355SKonstantin Belousov * 1645fa50a355SKonstantin Belousov * If all tries failed, and find_space is 1646fa50a355SKonstantin Belousov * VMFS_OPTIMAL_SPACE, fallback to VMFS_ANY_SPACE. 1647fa50a355SKonstantin Belousov * Again enable clustering and randomization. 1648fa50a355SKonstantin Belousov */ 1649fa50a355SKonstantin Belousov try++; 1650fa50a355SKonstantin Belousov MPASS(try <= 2); 1651fa50a355SKonstantin Belousov 1652fa50a355SKonstantin Belousov if (try == 2) { 1653fa50a355SKonstantin Belousov /* 1654fa50a355SKonstantin Belousov * Second try: we failed either to find a 1655fa50a355SKonstantin Belousov * suitable region for randomizing the 1656fa50a355SKonstantin Belousov * allocation, or to cluster with an existing 1657fa50a355SKonstantin Belousov * mapping. Retry with free run. 1658fa50a355SKonstantin Belousov */ 1659fa50a355SKonstantin Belousov curr_min_addr = (map->flags & MAP_ASLR_IGNSTART) != 0 ? 1660fa50a355SKonstantin Belousov vm_map_min(map) : min_addr; 1661fa50a355SKonstantin Belousov atomic_add_long(&aslr_restarts, 1); 1662fa50a355SKonstantin Belousov } 1663fa50a355SKonstantin Belousov 1664fa50a355SKonstantin Belousov if (try == 1 && en_aslr && !cluster) { 1665fa50a355SKonstantin Belousov /* 1666fa50a355SKonstantin Belousov * Find space for allocation, including 1667fa50a355SKonstantin Belousov * gap needed for later randomization. 1668fa50a355SKonstantin Belousov */ 1669fa50a355SKonstantin Belousov pidx = MAXPAGESIZES > 1 && pagesizes[1] != 0 && 1670fa50a355SKonstantin Belousov (find_space == VMFS_SUPER_SPACE || find_space == 1671fa50a355SKonstantin Belousov VMFS_OPTIMAL_SPACE) ? 1 : 0; 1672fa50a355SKonstantin Belousov gap = vm_map_max(map) > MAP_32BIT_MAX_ADDR && 1673fa50a355SKonstantin Belousov (max_addr == 0 || max_addr > MAP_32BIT_MAX_ADDR) ? 1674fa50a355SKonstantin Belousov aslr_pages_rnd_64[pidx] : aslr_pages_rnd_32[pidx]; 1675fa50a355SKonstantin Belousov if (vm_map_findspace(map, curr_min_addr, length + 1676fa50a355SKonstantin Belousov gap * pagesizes[pidx], addr) || 1677fa50a355SKonstantin Belousov (max_addr != 0 && *addr + length > max_addr)) 1678fa50a355SKonstantin Belousov goto again; 1679fa50a355SKonstantin Belousov /* And randomize the start address. */ 1680fa50a355SKonstantin Belousov *addr += (arc4random() % gap) * pagesizes[pidx]; 1681fa50a355SKonstantin Belousov } else if (vm_map_findspace(map, curr_min_addr, length, addr) || 1682edb572a3SJohn Baldwin (max_addr != 0 && *addr + length > max_addr)) { 1683fa50a355SKonstantin Belousov if (cluster) { 1684fa50a355SKonstantin Belousov cluster = false; 1685fa50a355SKonstantin Belousov MPASS(try == 1); 1686fa50a355SKonstantin Belousov goto again; 1687fa50a355SKonstantin Belousov } 1688fec29688SAlan Cox rv = KERN_NO_SPACE; 1689fec29688SAlan Cox goto done; 1690fec29688SAlan Cox } 1691fa50a355SKonstantin Belousov 1692fec29688SAlan Cox if (find_space != VMFS_ANY_SPACE && 1693fec29688SAlan Cox (rv = vm_map_alignspace(map, object, offset, addr, length, 1694fec29688SAlan Cox max_addr, alignment)) != KERN_SUCCESS) { 1695ff74a3faSJohn Baldwin if (find_space == VMFS_OPTIMAL_SPACE) { 1696ff74a3faSJohn Baldwin find_space = VMFS_ANY_SPACE; 1697fa50a355SKonstantin Belousov curr_min_addr = min_addr; 1698fa50a355SKonstantin Belousov cluster = update_anon; 1699fa50a355SKonstantin Belousov try = 0; 1700ff74a3faSJohn Baldwin goto again; 1701ff74a3faSJohn Baldwin } 1702fec29688SAlan Cox goto done; 1703df8bae1dSRodney W. Grimes } 1704ea7e7006SKonstantin Belousov } else if ((cow & MAP_REMAP) != 0) { 1705ea7e7006SKonstantin Belousov if (*addr < vm_map_min(map) || 1706ea7e7006SKonstantin Belousov *addr + length > vm_map_max(map) || 1707ea7e7006SKonstantin Belousov *addr + length <= length) { 1708ea7e7006SKonstantin Belousov rv = KERN_INVALID_ADDRESS; 1709ea7e7006SKonstantin Belousov goto done; 1710ea7e7006SKonstantin Belousov } 1711ea7e7006SKonstantin Belousov vm_map_delete(map, *addr, *addr + length); 1712df8bae1dSRodney W. Grimes } 17134648ba0aSKonstantin Belousov if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) { 1714fec29688SAlan Cox rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot, 1715fec29688SAlan Cox max, cow); 17164648ba0aSKonstantin Belousov } else { 1717fec29688SAlan Cox rv = vm_map_insert(map, object, offset, *addr, *addr + length, 1718fec29688SAlan Cox prot, max, cow); 17194648ba0aSKonstantin Belousov } 1720fa50a355SKonstantin Belousov if (rv == KERN_SUCCESS && update_anon) 1721fa50a355SKonstantin Belousov map->anon_loc = *addr + length; 1722fec29688SAlan Cox done: 1723df8bae1dSRodney W. Grimes vm_map_unlock(map); 1724fec29688SAlan Cox return (rv); 1725df8bae1dSRodney W. Grimes } 1726df8bae1dSRodney W. Grimes 1727e8502826SKonstantin Belousov /* 1728e8502826SKonstantin Belousov * vm_map_find_min() is a variant of vm_map_find() that takes an 1729e8502826SKonstantin Belousov * additional parameter (min_addr) and treats the given address 1730e8502826SKonstantin Belousov * (*addr) differently. Specifically, it treats *addr as a hint 1731e8502826SKonstantin Belousov * and not as the minimum address where the mapping is created. 1732e8502826SKonstantin Belousov * 1733e8502826SKonstantin Belousov * This function works in two phases. First, it tries to 1734e8502826SKonstantin Belousov * allocate above the hint. If that fails and the hint is 1735e8502826SKonstantin Belousov * greater than min_addr, it performs a second pass, replacing 1736e8502826SKonstantin Belousov * the hint with min_addr as the minimum address for the 1737e8502826SKonstantin Belousov * allocation. 1738e8502826SKonstantin Belousov */ 17396a97a3f7SKonstantin Belousov int 17406a97a3f7SKonstantin Belousov vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 17416a97a3f7SKonstantin Belousov vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr, 17426a97a3f7SKonstantin Belousov vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max, 17436a97a3f7SKonstantin Belousov int cow) 17446a97a3f7SKonstantin Belousov { 17456a97a3f7SKonstantin Belousov vm_offset_t hint; 17466a97a3f7SKonstantin Belousov int rv; 17476a97a3f7SKonstantin Belousov 17486a97a3f7SKonstantin Belousov hint = *addr; 17496a97a3f7SKonstantin Belousov for (;;) { 17506a97a3f7SKonstantin Belousov rv = vm_map_find(map, object, offset, addr, length, max_addr, 17516a97a3f7SKonstantin Belousov find_space, prot, max, cow); 17526a97a3f7SKonstantin Belousov if (rv == KERN_SUCCESS || min_addr >= hint) 17536a97a3f7SKonstantin Belousov return (rv); 17547683ad70SKonstantin Belousov *addr = hint = min_addr; 17556a97a3f7SKonstantin Belousov } 17566a97a3f7SKonstantin Belousov } 17576a97a3f7SKonstantin Belousov 175892e78c10SAlan Cox /* 175992e78c10SAlan Cox * A map entry with any of the following flags set must not be merged with 176092e78c10SAlan Cox * another entry. 176192e78c10SAlan Cox */ 176292e78c10SAlan Cox #define MAP_ENTRY_NOMERGE_MASK (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP | \ 176392e78c10SAlan Cox MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP) 176492e78c10SAlan Cox 176507424462SKonstantin Belousov static bool 176607424462SKonstantin Belousov vm_map_mergeable_neighbors(vm_map_entry_t prev, vm_map_entry_t entry) 176707424462SKonstantin Belousov { 176807424462SKonstantin Belousov 176992e78c10SAlan Cox KASSERT((prev->eflags & MAP_ENTRY_NOMERGE_MASK) == 0 || 177092e78c10SAlan Cox (entry->eflags & MAP_ENTRY_NOMERGE_MASK) == 0, 177192e78c10SAlan Cox ("vm_map_mergeable_neighbors: neither %p nor %p are mergeable", 177292e78c10SAlan Cox prev, entry)); 177307424462SKonstantin Belousov return (prev->end == entry->start && 177407424462SKonstantin Belousov prev->object.vm_object == entry->object.vm_object && 177507424462SKonstantin Belousov (prev->object.vm_object == NULL || 177692e78c10SAlan Cox prev->offset + (prev->end - prev->start) == entry->offset) && 177707424462SKonstantin Belousov prev->eflags == entry->eflags && 177807424462SKonstantin Belousov prev->protection == entry->protection && 177907424462SKonstantin Belousov prev->max_protection == entry->max_protection && 178007424462SKonstantin Belousov prev->inheritance == entry->inheritance && 178107424462SKonstantin Belousov prev->wired_count == entry->wired_count && 178207424462SKonstantin Belousov prev->cred == entry->cred); 178307424462SKonstantin Belousov } 178407424462SKonstantin Belousov 178507424462SKonstantin Belousov static void 178607424462SKonstantin Belousov vm_map_merged_neighbor_dispose(vm_map_t map, vm_map_entry_t entry) 178707424462SKonstantin Belousov { 178807424462SKonstantin Belousov 178907424462SKonstantin Belousov /* 179092e78c10SAlan Cox * If the backing object is a vnode object, vm_object_deallocate() 179192e78c10SAlan Cox * calls vrele(). However, vrele() does not lock the vnode because 179292e78c10SAlan Cox * the vnode has additional references. Thus, the map lock can be 179392e78c10SAlan Cox * kept without causing a lock-order reversal with the vnode lock. 179407424462SKonstantin Belousov * 179592e78c10SAlan Cox * Since we count the number of virtual page mappings in 179692e78c10SAlan Cox * object->un_pager.vnp.writemappings, the writemappings value 179792e78c10SAlan Cox * should not be adjusted when the entry is disposed of. 179807424462SKonstantin Belousov */ 179907424462SKonstantin Belousov if (entry->object.vm_object != NULL) 180007424462SKonstantin Belousov vm_object_deallocate(entry->object.vm_object); 180107424462SKonstantin Belousov if (entry->cred != NULL) 180207424462SKonstantin Belousov crfree(entry->cred); 180307424462SKonstantin Belousov vm_map_entry_dispose(map, entry); 180407424462SKonstantin Belousov } 180507424462SKonstantin Belousov 1806df8bae1dSRodney W. Grimes /* 1807b7b2aac2SJohn Dyson * vm_map_simplify_entry: 180867bf6868SJohn Dyson * 18094e71e795SMatthew Dillon * Simplify the given map entry by merging with either neighbor. This 18104e71e795SMatthew Dillon * routine also has the ability to merge with both neighbors. 18114e71e795SMatthew Dillon * 18124e71e795SMatthew Dillon * The map must be locked. 18134e71e795SMatthew Dillon * 1814ba7c64d1SKonstantin Belousov * This routine guarantees that the passed entry remains valid (though 18154e71e795SMatthew Dillon * possibly extended). When merging, this routine may delete one or 18164e71e795SMatthew Dillon * both neighbors. 1817df8bae1dSRodney W. Grimes */ 18180afcd3afSAlan Cox void 18191b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 1820df8bae1dSRodney W. Grimes { 1821308c24baSJohn Dyson vm_map_entry_t next, prev; 1822df8bae1dSRodney W. Grimes 182392e78c10SAlan Cox if ((entry->eflags & MAP_ENTRY_NOMERGE_MASK) != 0) 1824df8bae1dSRodney W. Grimes return; 1825308c24baSJohn Dyson prev = entry->prev; 18262203c46dSMark Johnston if (vm_map_mergeable_neighbors(prev, entry)) { 1827308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 1828308c24baSJohn Dyson entry->start = prev->start; 1829308c24baSJohn Dyson entry->offset = prev->offset; 18300164e057SAlan Cox if (entry->prev != &map->header) 18310164e057SAlan Cox vm_map_entry_resize_free(map, entry->prev); 183207424462SKonstantin Belousov vm_map_merged_neighbor_dispose(map, prev); 1833308c24baSJohn Dyson } 1834de5f6a77SJohn Dyson next = entry->next; 18352203c46dSMark Johnston if (vm_map_mergeable_neighbors(entry, next)) { 1836de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 1837de5f6a77SJohn Dyson entry->end = next->end; 18380164e057SAlan Cox vm_map_entry_resize_free(map, entry); 183907424462SKonstantin Belousov vm_map_merged_neighbor_dispose(map, next); 1840df8bae1dSRodney W. Grimes } 1841df8bae1dSRodney W. Grimes } 184292e78c10SAlan Cox 1843df8bae1dSRodney W. Grimes /* 1844df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 1845df8bae1dSRodney W. Grimes * 1846df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 1847df8bae1dSRodney W. Grimes * the specified address; if necessary, 1848df8bae1dSRodney W. Grimes * it splits the entry into two. 1849df8bae1dSRodney W. Grimes */ 1850df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 1851df8bae1dSRodney W. Grimes { \ 1852df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 1853df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 1854df8bae1dSRodney W. Grimes } 1855df8bae1dSRodney W. Grimes 1856df8bae1dSRodney W. Grimes /* 1857df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1858df8bae1dSRodney W. Grimes * the entry must be split. 1859df8bae1dSRodney W. Grimes */ 18600d94caffSDavid Greenman static void 18611b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1862df8bae1dSRodney W. Grimes { 1863c0877f10SJohn Dyson vm_map_entry_t new_entry; 1864df8bae1dSRodney W. Grimes 18653a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 1866ed11e4d7SMark Johnston KASSERT(entry->end > start && entry->start < start, 1867ed11e4d7SMark Johnston ("_vm_map_clip_start: invalid clip of entry %p", entry)); 18683a0916b8SKonstantin Belousov 1869df8bae1dSRodney W. Grimes /* 18700d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 18710d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 18720d94caffSDavid Greenman * starting address. 1873df8bae1dSRodney W. Grimes */ 1874f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 1875f32dbbeeSJohn Dyson 187611cccda1SJohn Dyson /* 187711cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 187811cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 187911cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 188011cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 188111cccda1SJohn Dyson * put this improvement. 188211cccda1SJohn Dyson */ 188319bd0d9cSKonstantin Belousov if (entry->object.vm_object == NULL && !map->system_map && 188419bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) == 0) { 188511cccda1SJohn Dyson vm_object_t object; 188611cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1887c2e11a03SJohn Dyson atop(entry->end - entry->start)); 188811cccda1SJohn Dyson entry->object.vm_object = object; 188911cccda1SJohn Dyson entry->offset = 0; 1890ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 1891ef694c1aSEdward Tomasz Napierala object->cred = entry->cred; 18923364c323SKonstantin Belousov object->charge = entry->end - entry->start; 1893ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 18943364c323SKonstantin Belousov } 18953364c323SKonstantin Belousov } else if (entry->object.vm_object != NULL && 18963364c323SKonstantin Belousov ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1897ef694c1aSEdward Tomasz Napierala entry->cred != NULL) { 189889f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 1899ef694c1aSEdward Tomasz Napierala KASSERT(entry->object.vm_object->cred == NULL, 1900ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry)); 1901ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 19023364c323SKonstantin Belousov entry->object.vm_object->charge = entry->end - entry->start; 190389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 1904ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 190511cccda1SJohn Dyson } 190611cccda1SJohn Dyson 1907df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1908df8bae1dSRodney W. Grimes *new_entry = *entry; 1909df8bae1dSRodney W. Grimes 1910df8bae1dSRodney W. Grimes new_entry->end = start; 1911df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 1912df8bae1dSRodney W. Grimes entry->start = start; 1913ef694c1aSEdward Tomasz Napierala if (new_entry->cred != NULL) 1914ef694c1aSEdward Tomasz Napierala crhold(entry->cred); 1915df8bae1dSRodney W. Grimes 1916df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 1917df8bae1dSRodney W. Grimes 19189fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1919df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 192084110e7eSKonstantin Belousov /* 192184110e7eSKonstantin Belousov * The object->un_pager.vnp.writemappings for the 192284110e7eSKonstantin Belousov * object of MAP_ENTRY_VN_WRITECNT type entry shall be 192384110e7eSKonstantin Belousov * kept as is here. The virtual pages are 192484110e7eSKonstantin Belousov * re-distributed among the clipped entries, so the sum is 192584110e7eSKonstantin Belousov * left the same. 192684110e7eSKonstantin Belousov */ 1927df8bae1dSRodney W. Grimes } 1928c0877f10SJohn Dyson } 1929df8bae1dSRodney W. Grimes 1930df8bae1dSRodney W. Grimes /* 1931df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 1932df8bae1dSRodney W. Grimes * 1933df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 1934df8bae1dSRodney W. Grimes * the specified address; if necessary, 1935df8bae1dSRodney W. Grimes * it splits the entry into two. 1936df8bae1dSRodney W. Grimes */ 1937df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 1938df8bae1dSRodney W. Grimes { \ 1939af045176SPoul-Henning Kamp if ((endaddr) < (entry->end)) \ 1940af045176SPoul-Henning Kamp _vm_map_clip_end((map), (entry), (endaddr)); \ 1941df8bae1dSRodney W. Grimes } 1942df8bae1dSRodney W. Grimes 1943df8bae1dSRodney W. Grimes /* 1944df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1945df8bae1dSRodney W. Grimes * the entry must be split. 1946df8bae1dSRodney W. Grimes */ 19470d94caffSDavid Greenman static void 19481b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1949df8bae1dSRodney W. Grimes { 1950c0877f10SJohn Dyson vm_map_entry_t new_entry; 1951df8bae1dSRodney W. Grimes 19523a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 1953ed11e4d7SMark Johnston KASSERT(entry->start < end && entry->end > end, 1954ed11e4d7SMark Johnston ("_vm_map_clip_end: invalid clip of entry %p", entry)); 19553a0916b8SKonstantin Belousov 1956df8bae1dSRodney W. Grimes /* 195711cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 195811cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 195911cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 196011cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 196111cccda1SJohn Dyson * put this improvement. 196211cccda1SJohn Dyson */ 196319bd0d9cSKonstantin Belousov if (entry->object.vm_object == NULL && !map->system_map && 196419bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) == 0) { 196511cccda1SJohn Dyson vm_object_t object; 196611cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1967c2e11a03SJohn Dyson atop(entry->end - entry->start)); 196811cccda1SJohn Dyson entry->object.vm_object = object; 196911cccda1SJohn Dyson entry->offset = 0; 1970ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 1971ef694c1aSEdward Tomasz Napierala object->cred = entry->cred; 19723364c323SKonstantin Belousov object->charge = entry->end - entry->start; 1973ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 19743364c323SKonstantin Belousov } 19753364c323SKonstantin Belousov } else if (entry->object.vm_object != NULL && 19763364c323SKonstantin Belousov ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) && 1977ef694c1aSEdward Tomasz Napierala entry->cred != NULL) { 197889f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 1979ef694c1aSEdward Tomasz Napierala KASSERT(entry->object.vm_object->cred == NULL, 1980ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry)); 1981ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 19823364c323SKonstantin Belousov entry->object.vm_object->charge = entry->end - entry->start; 198389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 1984ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 198511cccda1SJohn Dyson } 198611cccda1SJohn Dyson 198711cccda1SJohn Dyson /* 19880d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 1989df8bae1dSRodney W. Grimes */ 1990df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1991df8bae1dSRodney W. Grimes *new_entry = *entry; 1992df8bae1dSRodney W. Grimes 1993df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 1994df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 1995ef694c1aSEdward Tomasz Napierala if (new_entry->cred != NULL) 1996ef694c1aSEdward Tomasz Napierala crhold(entry->cred); 1997df8bae1dSRodney W. Grimes 1998df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 1999df8bae1dSRodney W. Grimes 20009fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 2001df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 2002df8bae1dSRodney W. Grimes } 2003c0877f10SJohn Dyson } 2004df8bae1dSRodney W. Grimes 2005df8bae1dSRodney W. Grimes /* 2006df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 2007df8bae1dSRodney W. Grimes * 2008df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 2009df8bae1dSRodney W. Grimes * 2010df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 2011df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 2012df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 2013df8bae1dSRodney W. Grimes * 2014df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 2015df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 2016df8bae1dSRodney W. Grimes * vm_fault 2017df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 2018df8bae1dSRodney W. Grimes * 2019df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 2020df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 2021df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 2022df8bae1dSRodney W. Grimes */ 2023df8bae1dSRodney W. Grimes int 20241b40f8c0SMatthew Dillon vm_map_submap( 20251b40f8c0SMatthew Dillon vm_map_t map, 20261b40f8c0SMatthew Dillon vm_offset_t start, 20271b40f8c0SMatthew Dillon vm_offset_t end, 20281b40f8c0SMatthew Dillon vm_map_t submap) 2029df8bae1dSRodney W. Grimes { 2030df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2031fa50a355SKonstantin Belousov int result; 2032fa50a355SKonstantin Belousov 2033fa50a355SKonstantin Belousov result = KERN_INVALID_ARGUMENT; 2034fa50a355SKonstantin Belousov 2035fa50a355SKonstantin Belousov vm_map_lock(submap); 2036fa50a355SKonstantin Belousov submap->flags |= MAP_IS_SUB_MAP; 2037fa50a355SKonstantin Belousov vm_map_unlock(submap); 2038df8bae1dSRodney W. Grimes 2039df8bae1dSRodney W. Grimes vm_map_lock(map); 2040df8bae1dSRodney W. Grimes 2041df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2042df8bae1dSRodney W. Grimes 2043df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 2044df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 20450d94caffSDavid Greenman } else 2046df8bae1dSRodney W. Grimes entry = entry->next; 2047df8bae1dSRodney W. Grimes 2048df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 2049df8bae1dSRodney W. Grimes 2050df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 20519fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 2052afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 20532d8acc0fSJohn Dyson entry->object.sub_map = submap; 2054afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 2055df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 2056df8bae1dSRodney W. Grimes } 2057df8bae1dSRodney W. Grimes vm_map_unlock(map); 2058df8bae1dSRodney W. Grimes 2059fa50a355SKonstantin Belousov if (result != KERN_SUCCESS) { 2060fa50a355SKonstantin Belousov vm_map_lock(submap); 2061fa50a355SKonstantin Belousov submap->flags &= ~MAP_IS_SUB_MAP; 2062fa50a355SKonstantin Belousov vm_map_unlock(submap); 2063fa50a355SKonstantin Belousov } 2064df8bae1dSRodney W. Grimes return (result); 2065df8bae1dSRodney W. Grimes } 2066df8bae1dSRodney W. Grimes 2067df8bae1dSRodney W. Grimes /* 2068dd05fa19SAlan Cox * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified 20691f78f902SAlan Cox */ 20701f78f902SAlan Cox #define MAX_INIT_PT 96 20711f78f902SAlan Cox 20721f78f902SAlan Cox /* 20730551c08dSAlan Cox * vm_map_pmap_enter: 20740551c08dSAlan Cox * 2075dd05fa19SAlan Cox * Preload the specified map's pmap with mappings to the specified 2076dd05fa19SAlan Cox * object's memory-resident pages. No further physical pages are 2077dd05fa19SAlan Cox * allocated, and no further virtual pages are retrieved from secondary 2078dd05fa19SAlan Cox * storage. If the specified flags include MAP_PREFAULT_PARTIAL, then a 2079dd05fa19SAlan Cox * limited number of page mappings are created at the low-end of the 2080dd05fa19SAlan Cox * specified address range. (For this purpose, a superpage mapping 2081dd05fa19SAlan Cox * counts as one page mapping.) Otherwise, all resident pages within 20823453bca8SAlan Cox * the specified address range are mapped. 20830551c08dSAlan Cox */ 2084077ec27cSAlan Cox static void 20854da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot, 20860551c08dSAlan Cox vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags) 20870551c08dSAlan Cox { 20888fece8c3SAlan Cox vm_offset_t start; 2089ce142d9eSAlan Cox vm_page_t p, p_start; 2090dd05fa19SAlan Cox vm_pindex_t mask, psize, threshold, tmpidx; 20910551c08dSAlan Cox 2092ba8bca61SAlan Cox if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL) 20931f78f902SAlan Cox return; 20949af6d512SAttilio Rao VM_OBJECT_RLOCK(object); 20959af6d512SAttilio Rao if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 20969af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 209789f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 209801381811SJohn Baldwin if (object->type == OBJT_DEVICE || object->type == OBJT_SG) { 20999af6d512SAttilio Rao pmap_object_init_pt(map->pmap, addr, object, pindex, 21009af6d512SAttilio Rao size); 21019af6d512SAttilio Rao VM_OBJECT_WUNLOCK(object); 21029af6d512SAttilio Rao return; 21039af6d512SAttilio Rao } 21049af6d512SAttilio Rao VM_OBJECT_LOCK_DOWNGRADE(object); 21051f78f902SAlan Cox } 21061f78f902SAlan Cox 21071f78f902SAlan Cox psize = atop(size); 21081f78f902SAlan Cox if (psize + pindex > object->size) { 21099af6d512SAttilio Rao if (object->size < pindex) { 21109af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 21119af6d512SAttilio Rao return; 21129af6d512SAttilio Rao } 21131f78f902SAlan Cox psize = object->size - pindex; 21141f78f902SAlan Cox } 21151f78f902SAlan Cox 2116ce142d9eSAlan Cox start = 0; 2117ce142d9eSAlan Cox p_start = NULL; 2118dd05fa19SAlan Cox threshold = MAX_INIT_PT; 21191f78f902SAlan Cox 2120b382c10aSKonstantin Belousov p = vm_page_find_least(object, pindex); 21211f78f902SAlan Cox /* 21221f78f902SAlan Cox * Assert: the variable p is either (1) the page with the 21231f78f902SAlan Cox * least pindex greater than or equal to the parameter pindex 21241f78f902SAlan Cox * or (2) NULL. 21251f78f902SAlan Cox */ 21261f78f902SAlan Cox for (; 21271f78f902SAlan Cox p != NULL && (tmpidx = p->pindex - pindex) < psize; 21281f78f902SAlan Cox p = TAILQ_NEXT(p, listq)) { 21291f78f902SAlan Cox /* 21301f78f902SAlan Cox * don't allow an madvise to blow away our really 21311f78f902SAlan Cox * free pages allocating pv entries. 21321f78f902SAlan Cox */ 2133dd05fa19SAlan Cox if (((flags & MAP_PREFAULT_MADVISE) != 0 && 2134e2068d0bSJeff Roberson vm_page_count_severe()) || 2135dd05fa19SAlan Cox ((flags & MAP_PREFAULT_PARTIAL) != 0 && 2136dd05fa19SAlan Cox tmpidx >= threshold)) { 2137379fb642SAlan Cox psize = tmpidx; 21381f78f902SAlan Cox break; 21391f78f902SAlan Cox } 21400a2e596aSAlan Cox if (p->valid == VM_PAGE_BITS_ALL) { 2141ce142d9eSAlan Cox if (p_start == NULL) { 2142ce142d9eSAlan Cox start = addr + ptoa(tmpidx); 2143ce142d9eSAlan Cox p_start = p; 2144ce142d9eSAlan Cox } 2145dd05fa19SAlan Cox /* Jump ahead if a superpage mapping is possible. */ 2146dd05fa19SAlan Cox if (p->psind > 0 && ((addr + ptoa(tmpidx)) & 2147dd05fa19SAlan Cox (pagesizes[p->psind] - 1)) == 0) { 2148dd05fa19SAlan Cox mask = atop(pagesizes[p->psind]) - 1; 2149dd05fa19SAlan Cox if (tmpidx + mask < psize && 215088302601SAlan Cox vm_page_ps_test(p, PS_ALL_VALID, NULL)) { 2151dd05fa19SAlan Cox p += mask; 2152dd05fa19SAlan Cox threshold += mask; 2153dd05fa19SAlan Cox } 2154dd05fa19SAlan Cox } 21557bfda801SAlan Cox } else if (p_start != NULL) { 2156cf4682aeSAlan Cox pmap_enter_object(map->pmap, start, addr + 2157cf4682aeSAlan Cox ptoa(tmpidx), p_start, prot); 2158cf4682aeSAlan Cox p_start = NULL; 2159cf4682aeSAlan Cox } 2160cf4682aeSAlan Cox } 2161c46b90e9SAlan Cox if (p_start != NULL) 2162379fb642SAlan Cox pmap_enter_object(map->pmap, start, addr + ptoa(psize), 2163379fb642SAlan Cox p_start, prot); 21649af6d512SAttilio Rao VM_OBJECT_RUNLOCK(object); 21650551c08dSAlan Cox } 21660551c08dSAlan Cox 21670551c08dSAlan Cox /* 2168df8bae1dSRodney W. Grimes * vm_map_protect: 2169df8bae1dSRodney W. Grimes * 2170df8bae1dSRodney W. Grimes * Sets the protection of the specified address 2171df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 2172df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 2173df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 2174df8bae1dSRodney W. Grimes */ 2175df8bae1dSRodney W. Grimes int 2176b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 2177b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 2178df8bae1dSRodney W. Grimes { 2179210a6886SKonstantin Belousov vm_map_entry_t current, entry; 21803364c323SKonstantin Belousov vm_object_t obj; 2181ef694c1aSEdward Tomasz Napierala struct ucred *cred; 2182210a6886SKonstantin Belousov vm_prot_t old_prot; 2183df8bae1dSRodney W. Grimes 218479e9451fSKonstantin Belousov if (start == end) 218579e9451fSKonstantin Belousov return (KERN_SUCCESS); 218679e9451fSKonstantin Belousov 2187df8bae1dSRodney W. Grimes vm_map_lock(map); 2188df8bae1dSRodney W. Grimes 2189e1cb9d37SMark Johnston /* 2190e1cb9d37SMark Johnston * Ensure that we are not concurrently wiring pages. vm_map_wire() may 2191e1cb9d37SMark Johnston * need to fault pages into the map and will drop the map lock while 2192e1cb9d37SMark Johnston * doing so, and the VM object may end up in an inconsistent state if we 2193e1cb9d37SMark Johnston * update the protection on the map entry in between faults. 2194e1cb9d37SMark Johnston */ 2195e1cb9d37SMark Johnston vm_map_wait_busy(map); 2196e1cb9d37SMark Johnston 2197df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2198df8bae1dSRodney W. Grimes 2199df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 2200df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 2201b7b2aac2SJohn Dyson } else { 2202df8bae1dSRodney W. Grimes entry = entry->next; 2203b7b2aac2SJohn Dyson } 2204df8bae1dSRodney W. Grimes 2205df8bae1dSRodney W. Grimes /* 22060d94caffSDavid Greenman * Make a first pass to check for protection violations. 2207df8bae1dSRodney W. Grimes */ 22081c5196c3SKonstantin Belousov for (current = entry; current->start < end; current = current->next) { 22098a89ca94SKonstantin Belousov if ((current->eflags & MAP_ENTRY_GUARD) != 0) 22108a89ca94SKonstantin Belousov continue; 2211afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 2212a1f6d91cSDavid Greenman vm_map_unlock(map); 2213df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2214a1f6d91cSDavid Greenman } 2215df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 2216df8bae1dSRodney W. Grimes vm_map_unlock(map); 2217df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 2218df8bae1dSRodney W. Grimes } 2219df8bae1dSRodney W. Grimes } 2220df8bae1dSRodney W. Grimes 22213364c323SKonstantin Belousov /* 22223364c323SKonstantin Belousov * Do an accounting pass for private read-only mappings that 22233364c323SKonstantin Belousov * now will do cow due to allowed write (e.g. debugger sets 22243364c323SKonstantin Belousov * breakpoint on text segment) 22253364c323SKonstantin Belousov */ 22261c5196c3SKonstantin Belousov for (current = entry; current->start < end; current = current->next) { 22273364c323SKonstantin Belousov 22283364c323SKonstantin Belousov vm_map_clip_end(map, current, end); 22293364c323SKonstantin Belousov 22303364c323SKonstantin Belousov if (set_max || 22313364c323SKonstantin Belousov ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 || 223219bd0d9cSKonstantin Belousov ENTRY_CHARGED(current) || 223319bd0d9cSKonstantin Belousov (current->eflags & MAP_ENTRY_GUARD) != 0) { 22343364c323SKonstantin Belousov continue; 22353364c323SKonstantin Belousov } 22363364c323SKonstantin Belousov 2237ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 22383364c323SKonstantin Belousov obj = current->object.vm_object; 22393364c323SKonstantin Belousov 22403364c323SKonstantin Belousov if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) { 22413364c323SKonstantin Belousov if (!swap_reserve(current->end - current->start)) { 22423364c323SKonstantin Belousov vm_map_unlock(map); 22433364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 22443364c323SKonstantin Belousov } 2245ef694c1aSEdward Tomasz Napierala crhold(cred); 2246ef694c1aSEdward Tomasz Napierala current->cred = cred; 22473364c323SKonstantin Belousov continue; 22483364c323SKonstantin Belousov } 22493364c323SKonstantin Belousov 225089f6b863SAttilio Rao VM_OBJECT_WLOCK(obj); 22513364c323SKonstantin Belousov if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) { 225289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 22533364c323SKonstantin Belousov continue; 22543364c323SKonstantin Belousov } 22553364c323SKonstantin Belousov 22563364c323SKonstantin Belousov /* 22573364c323SKonstantin Belousov * Charge for the whole object allocation now, since 22583364c323SKonstantin Belousov * we cannot distinguish between non-charged and 22593364c323SKonstantin Belousov * charged clipped mapping of the same object later. 22603364c323SKonstantin Belousov */ 22613364c323SKonstantin Belousov KASSERT(obj->charge == 0, 22623d95614fSKonstantin Belousov ("vm_map_protect: object %p overcharged (entry %p)", 22633d95614fSKonstantin Belousov obj, current)); 22643364c323SKonstantin Belousov if (!swap_reserve(ptoa(obj->size))) { 226589f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 22663364c323SKonstantin Belousov vm_map_unlock(map); 22673364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 22683364c323SKonstantin Belousov } 22693364c323SKonstantin Belousov 2270ef694c1aSEdward Tomasz Napierala crhold(cred); 2271ef694c1aSEdward Tomasz Napierala obj->cred = cred; 22723364c323SKonstantin Belousov obj->charge = ptoa(obj->size); 227389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(obj); 22743364c323SKonstantin Belousov } 22753364c323SKonstantin Belousov 2276df8bae1dSRodney W. Grimes /* 22770d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 22780d94caffSDavid Greenman * necessary the second time.] 2279df8bae1dSRodney W. Grimes */ 22801c5196c3SKonstantin Belousov for (current = entry; current->start < end; current = current->next) { 228119bd0d9cSKonstantin Belousov if ((current->eflags & MAP_ENTRY_GUARD) != 0) 228219bd0d9cSKonstantin Belousov continue; 228319bd0d9cSKonstantin Belousov 2284df8bae1dSRodney W. Grimes old_prot = current->protection; 2285210a6886SKonstantin Belousov 2286df8bae1dSRodney W. Grimes if (set_max) 2287df8bae1dSRodney W. Grimes current->protection = 2288df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 2289df8bae1dSRodney W. Grimes old_prot; 2290df8bae1dSRodney W. Grimes else 2291df8bae1dSRodney W. Grimes current->protection = new_prot; 2292df8bae1dSRodney W. Grimes 2293dd006a1bSAlan Cox /* 2294dd006a1bSAlan Cox * For user wired map entries, the normal lazy evaluation of 2295dd006a1bSAlan Cox * write access upgrades through soft page faults is 2296dd006a1bSAlan Cox * undesirable. Instead, immediately copy any pages that are 2297dd006a1bSAlan Cox * copy-on-write and enable write access in the physical map. 2298dd006a1bSAlan Cox */ 2299dd006a1bSAlan Cox if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 && 2300210a6886SKonstantin Belousov (current->protection & VM_PROT_WRITE) != 0 && 23015930251aSKonstantin Belousov (old_prot & VM_PROT_WRITE) == 0) 2302210a6886SKonstantin Belousov vm_fault_copy_entry(map, map, current, current, NULL); 2303210a6886SKonstantin Belousov 2304df8bae1dSRodney W. Grimes /* 23052fafce9eSAlan Cox * When restricting access, update the physical map. Worry 23062fafce9eSAlan Cox * about copy-on-write here. 2307df8bae1dSRodney W. Grimes */ 23082fafce9eSAlan Cox if ((old_prot & ~current->protection) != 0) { 2309afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 2310df8bae1dSRodney W. Grimes VM_PROT_ALL) 2311df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 2312df8bae1dSRodney W. Grimes current->end, 23131c85e3dfSAlan Cox current->protection & MASK(current)); 2314df8bae1dSRodney W. Grimes #undef MASK 2315df8bae1dSRodney W. Grimes } 23167d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 2317df8bae1dSRodney W. Grimes } 2318df8bae1dSRodney W. Grimes vm_map_unlock(map); 2319df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2320df8bae1dSRodney W. Grimes } 2321df8bae1dSRodney W. Grimes 2322df8bae1dSRodney W. Grimes /* 2323867a482dSJohn Dyson * vm_map_madvise: 2324867a482dSJohn Dyson * 2325867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 2326f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 2327f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 2328f7fc307aSAlan Cox * objects. 2329867a482dSJohn Dyson */ 2330b4309055SMatthew Dillon int 23311b40f8c0SMatthew Dillon vm_map_madvise( 23321b40f8c0SMatthew Dillon vm_map_t map, 23331b40f8c0SMatthew Dillon vm_offset_t start, 23341b40f8c0SMatthew Dillon vm_offset_t end, 23351b40f8c0SMatthew Dillon int behav) 2336867a482dSJohn Dyson { 2337f7fc307aSAlan Cox vm_map_entry_t current, entry; 23383e7cb27cSAlan Cox bool modify_map; 2339867a482dSJohn Dyson 2340b4309055SMatthew Dillon /* 2341b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 2342b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 2343b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 2344b4309055SMatthew Dillon * on the map. 2345b4309055SMatthew Dillon */ 2346b4309055SMatthew Dillon switch(behav) { 2347b4309055SMatthew Dillon case MADV_NORMAL: 2348b4309055SMatthew Dillon case MADV_SEQUENTIAL: 2349b4309055SMatthew Dillon case MADV_RANDOM: 23504f79d873SMatthew Dillon case MADV_NOSYNC: 23514f79d873SMatthew Dillon case MADV_AUTOSYNC: 23529730a5daSPaul Saab case MADV_NOCORE: 23539730a5daSPaul Saab case MADV_CORE: 235479e9451fSKonstantin Belousov if (start == end) 23553e7cb27cSAlan Cox return (0); 23563e7cb27cSAlan Cox modify_map = true; 2357867a482dSJohn Dyson vm_map_lock(map); 2358b4309055SMatthew Dillon break; 2359b4309055SMatthew Dillon case MADV_WILLNEED: 2360b4309055SMatthew Dillon case MADV_DONTNEED: 2361b4309055SMatthew Dillon case MADV_FREE: 236279e9451fSKonstantin Belousov if (start == end) 23633e7cb27cSAlan Cox return (0); 23643e7cb27cSAlan Cox modify_map = false; 2365f7fc307aSAlan Cox vm_map_lock_read(map); 2366b4309055SMatthew Dillon break; 2367b4309055SMatthew Dillon default: 23683e7cb27cSAlan Cox return (EINVAL); 2369b4309055SMatthew Dillon } 2370b4309055SMatthew Dillon 2371b4309055SMatthew Dillon /* 2372b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 2373b4309055SMatthew Dillon */ 2374867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 2375867a482dSJohn Dyson 2376867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 2377f7fc307aSAlan Cox if (modify_map) 2378867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 2379b4309055SMatthew Dillon } else { 2380867a482dSJohn Dyson entry = entry->next; 2381b4309055SMatthew Dillon } 2382867a482dSJohn Dyson 2383f7fc307aSAlan Cox if (modify_map) { 2384f7fc307aSAlan Cox /* 2385f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 2386f7fc307aSAlan Cox * 2387f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 2388f7fc307aSAlan Cox * limited to the specified address range. 2389f7fc307aSAlan Cox */ 23901c5196c3SKonstantin Belousov for (current = entry; current->start < end; 23911c5196c3SKonstantin Belousov current = current->next) { 2392f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2393867a482dSJohn Dyson continue; 2394fed9a903SJohn Dyson 239547221757SJohn Dyson vm_map_clip_end(map, current, end); 2396fed9a903SJohn Dyson 2397f7fc307aSAlan Cox switch (behav) { 2398867a482dSJohn Dyson case MADV_NORMAL: 23997f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 2400867a482dSJohn Dyson break; 2401867a482dSJohn Dyson case MADV_SEQUENTIAL: 24027f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 2403867a482dSJohn Dyson break; 2404867a482dSJohn Dyson case MADV_RANDOM: 24057f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 2406867a482dSJohn Dyson break; 24074f79d873SMatthew Dillon case MADV_NOSYNC: 24084f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 24094f79d873SMatthew Dillon break; 24104f79d873SMatthew Dillon case MADV_AUTOSYNC: 24114f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 24124f79d873SMatthew Dillon break; 24139730a5daSPaul Saab case MADV_NOCORE: 24149730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 24159730a5daSPaul Saab break; 24169730a5daSPaul Saab case MADV_CORE: 24179730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 24189730a5daSPaul Saab break; 2419867a482dSJohn Dyson default: 2420867a482dSJohn Dyson break; 2421867a482dSJohn Dyson } 2422f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 2423867a482dSJohn Dyson } 2424867a482dSJohn Dyson vm_map_unlock(map); 2425b4309055SMatthew Dillon } else { 242692a59946SJohn Baldwin vm_pindex_t pstart, pend; 2427f7fc307aSAlan Cox 2428f7fc307aSAlan Cox /* 2429f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 2430f7fc307aSAlan Cox * vm_object. 2431f7fc307aSAlan Cox * 2432f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 2433f7fc307aSAlan Cox * the vm_object pindex and count. 2434f7fc307aSAlan Cox */ 24351c5196c3SKonstantin Belousov for (current = entry; current->start < end; 24361c5196c3SKonstantin Belousov current = current->next) { 243751321f7cSAlan Cox vm_offset_t useEnd, useStart; 24385f99b57cSMatthew Dillon 2439f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 2440f7fc307aSAlan Cox continue; 2441f7fc307aSAlan Cox 244292a59946SJohn Baldwin pstart = OFF_TO_IDX(current->offset); 244392a59946SJohn Baldwin pend = pstart + atop(current->end - current->start); 24445f99b57cSMatthew Dillon useStart = current->start; 244551321f7cSAlan Cox useEnd = current->end; 2446f7fc307aSAlan Cox 2447f7fc307aSAlan Cox if (current->start < start) { 244892a59946SJohn Baldwin pstart += atop(start - current->start); 24495f99b57cSMatthew Dillon useStart = start; 2450f7fc307aSAlan Cox } 245151321f7cSAlan Cox if (current->end > end) { 245292a59946SJohn Baldwin pend -= atop(current->end - end); 245351321f7cSAlan Cox useEnd = end; 245451321f7cSAlan Cox } 2455f7fc307aSAlan Cox 245692a59946SJohn Baldwin if (pstart >= pend) 2457f7fc307aSAlan Cox continue; 2458f7fc307aSAlan Cox 245951321f7cSAlan Cox /* 246051321f7cSAlan Cox * Perform the pmap_advise() before clearing 246151321f7cSAlan Cox * PGA_REFERENCED in vm_page_advise(). Otherwise, a 246251321f7cSAlan Cox * concurrent pmap operation, such as pmap_remove(), 246351321f7cSAlan Cox * could clear a reference in the pmap and set 246451321f7cSAlan Cox * PGA_REFERENCED on the page before the pmap_advise() 246551321f7cSAlan Cox * had completed. Consequently, the page would appear 246651321f7cSAlan Cox * referenced based upon an old reference that 246751321f7cSAlan Cox * occurred before this pmap_advise() ran. 246851321f7cSAlan Cox */ 246951321f7cSAlan Cox if (behav == MADV_DONTNEED || behav == MADV_FREE) 247051321f7cSAlan Cox pmap_advise(map->pmap, useStart, useEnd, 247151321f7cSAlan Cox behav); 247251321f7cSAlan Cox 247392a59946SJohn Baldwin vm_object_madvise(current->object.vm_object, pstart, 247492a59946SJohn Baldwin pend, behav); 247554432196SKonstantin Belousov 247654432196SKonstantin Belousov /* 247754432196SKonstantin Belousov * Pre-populate paging structures in the 247854432196SKonstantin Belousov * WILLNEED case. For wired entries, the 247954432196SKonstantin Belousov * paging structures are already populated. 248054432196SKonstantin Belousov */ 248154432196SKonstantin Belousov if (behav == MADV_WILLNEED && 248254432196SKonstantin Belousov current->wired_count == 0) { 24830551c08dSAlan Cox vm_map_pmap_enter(map, 24845f99b57cSMatthew Dillon useStart, 24854da4d293SAlan Cox current->protection, 2486f7fc307aSAlan Cox current->object.vm_object, 248792a59946SJohn Baldwin pstart, 248892a59946SJohn Baldwin ptoa(pend - pstart), 2489e3026983SMatthew Dillon MAP_PREFAULT_MADVISE 2490b4309055SMatthew Dillon ); 2491f7fc307aSAlan Cox } 2492f7fc307aSAlan Cox } 2493f7fc307aSAlan Cox vm_map_unlock_read(map); 2494f7fc307aSAlan Cox } 2495b4309055SMatthew Dillon return (0); 2496867a482dSJohn Dyson } 2497867a482dSJohn Dyson 2498867a482dSJohn Dyson 2499867a482dSJohn Dyson /* 2500df8bae1dSRodney W. Grimes * vm_map_inherit: 2501df8bae1dSRodney W. Grimes * 2502df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 2503df8bae1dSRodney W. Grimes * range in the target map. Inheritance 2504df8bae1dSRodney W. Grimes * affects how the map will be shared with 2505e2abaaaaSAlan Cox * child maps at the time of vmspace_fork. 2506df8bae1dSRodney W. Grimes */ 2507df8bae1dSRodney W. Grimes int 2508b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 2509b9dcd593SBruce Evans vm_inherit_t new_inheritance) 2510df8bae1dSRodney W. Grimes { 2511c0877f10SJohn Dyson vm_map_entry_t entry; 2512df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 2513df8bae1dSRodney W. Grimes 2514df8bae1dSRodney W. Grimes switch (new_inheritance) { 2515df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2516df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2517df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 251878d7964bSXin LI case VM_INHERIT_ZERO: 2519df8bae1dSRodney W. Grimes break; 2520df8bae1dSRodney W. Grimes default: 2521df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 2522df8bae1dSRodney W. Grimes } 252379e9451fSKonstantin Belousov if (start == end) 252479e9451fSKonstantin Belousov return (KERN_SUCCESS); 2525df8bae1dSRodney W. Grimes vm_map_lock(map); 2526df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2527df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 2528df8bae1dSRodney W. Grimes entry = temp_entry; 2529df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 25300d94caffSDavid Greenman } else 2531df8bae1dSRodney W. Grimes entry = temp_entry->next; 25321c5196c3SKonstantin Belousov while (entry->start < end) { 2533df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 253419bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) == 0 || 253519bd0d9cSKonstantin Belousov new_inheritance != VM_INHERIT_ZERO) 2536df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 253744428f62SAlan Cox vm_map_simplify_entry(map, entry); 2538df8bae1dSRodney W. Grimes entry = entry->next; 2539df8bae1dSRodney W. Grimes } 2540df8bae1dSRodney W. Grimes vm_map_unlock(map); 2541df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2542df8bae1dSRodney W. Grimes } 2543df8bae1dSRodney W. Grimes 2544df8bae1dSRodney W. Grimes /* 2545acd9a301SAlan Cox * vm_map_unwire: 2546acd9a301SAlan Cox * 2547e27e17b7SAlan Cox * Implements both kernel and user unwiring. 2548acd9a301SAlan Cox */ 2549acd9a301SAlan Cox int 2550acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2551abd498aaSBruce M Simpson int flags) 2552acd9a301SAlan Cox { 2553acd9a301SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 2554acd9a301SAlan Cox vm_offset_t saved_start; 2555acd9a301SAlan Cox unsigned int last_timestamp; 2556acd9a301SAlan Cox int rv; 2557abd498aaSBruce M Simpson boolean_t need_wakeup, result, user_unwire; 2558acd9a301SAlan Cox 255979e9451fSKonstantin Belousov if (start == end) 256079e9451fSKonstantin Belousov return (KERN_SUCCESS); 2561abd498aaSBruce M Simpson user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 2562acd9a301SAlan Cox vm_map_lock(map); 2563acd9a301SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 2564acd9a301SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 2565abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 2566cbef13d8SAlan Cox first_entry = first_entry->next; 2567abd498aaSBruce M Simpson else { 2568acd9a301SAlan Cox vm_map_unlock(map); 2569acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 2570acd9a301SAlan Cox } 2571abd498aaSBruce M Simpson } 2572acd9a301SAlan Cox last_timestamp = map->timestamp; 2573acd9a301SAlan Cox entry = first_entry; 25741c5196c3SKonstantin Belousov while (entry->start < end) { 2575acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 2576acd9a301SAlan Cox /* 2577acd9a301SAlan Cox * We have not yet clipped the entry. 2578acd9a301SAlan Cox */ 2579acd9a301SAlan Cox saved_start = (start >= entry->start) ? start : 2580acd9a301SAlan Cox entry->start; 2581acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 25828ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 2583acd9a301SAlan Cox /* 2584acd9a301SAlan Cox * Allow interruption of user unwiring? 2585acd9a301SAlan Cox */ 2586acd9a301SAlan Cox } 2587acd9a301SAlan Cox vm_map_lock(map); 2588acd9a301SAlan Cox if (last_timestamp+1 != map->timestamp) { 2589acd9a301SAlan Cox /* 2590acd9a301SAlan Cox * Look again for the entry because the map was 2591acd9a301SAlan Cox * modified while it was unlocked. 2592acd9a301SAlan Cox * Specifically, the entry may have been 2593acd9a301SAlan Cox * clipped, merged, or deleted. 2594acd9a301SAlan Cox */ 2595acd9a301SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 2596acd9a301SAlan Cox &tmp_entry)) { 2597cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2598cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2599cbef13d8SAlan Cox else { 2600acd9a301SAlan Cox if (saved_start == start) { 2601acd9a301SAlan Cox /* 2602acd9a301SAlan Cox * First_entry has been deleted. 2603acd9a301SAlan Cox */ 2604acd9a301SAlan Cox vm_map_unlock(map); 2605acd9a301SAlan Cox return (KERN_INVALID_ADDRESS); 2606acd9a301SAlan Cox } 2607acd9a301SAlan Cox end = saved_start; 2608acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 2609acd9a301SAlan Cox goto done; 2610acd9a301SAlan Cox } 2611cbef13d8SAlan Cox } 2612acd9a301SAlan Cox if (entry == first_entry) 2613acd9a301SAlan Cox first_entry = tmp_entry; 2614acd9a301SAlan Cox else 2615acd9a301SAlan Cox first_entry = NULL; 2616acd9a301SAlan Cox entry = tmp_entry; 2617acd9a301SAlan Cox } 2618acd9a301SAlan Cox last_timestamp = map->timestamp; 2619acd9a301SAlan Cox continue; 2620acd9a301SAlan Cox } 2621acd9a301SAlan Cox vm_map_clip_start(map, entry, start); 2622acd9a301SAlan Cox vm_map_clip_end(map, entry, end); 2623acd9a301SAlan Cox /* 2624acd9a301SAlan Cox * Mark the entry in case the map lock is released. (See 2625acd9a301SAlan Cox * above.) 2626acd9a301SAlan Cox */ 2627ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2628ff3ae454SKonstantin Belousov entry->wiring_thread == NULL, 2629ff3ae454SKonstantin Belousov ("owned map entry %p", entry)); 2630acd9a301SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 26310acea7dfSKonstantin Belousov entry->wiring_thread = curthread; 2632acd9a301SAlan Cox /* 2633acd9a301SAlan Cox * Check the map for holes in the specified region. 2634abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 2635acd9a301SAlan Cox */ 2636abd498aaSBruce M Simpson if (((flags & VM_MAP_WIRE_HOLESOK) == 0) && 26371c5196c3SKonstantin Belousov (entry->end < end && entry->next->start > entry->end)) { 2638acd9a301SAlan Cox end = entry->end; 2639acd9a301SAlan Cox rv = KERN_INVALID_ADDRESS; 2640acd9a301SAlan Cox goto done; 2641acd9a301SAlan Cox } 2642acd9a301SAlan Cox /* 26433ffbc0cdSAlan Cox * If system unwiring, require that the entry is system wired. 2644acd9a301SAlan Cox */ 26450ada205eSBrian Feldman if (!user_unwire && 26460ada205eSBrian Feldman vm_map_entry_system_wired_count(entry) == 0) { 2647acd9a301SAlan Cox end = entry->end; 2648acd9a301SAlan Cox rv = KERN_INVALID_ARGUMENT; 2649acd9a301SAlan Cox goto done; 2650acd9a301SAlan Cox } 2651acd9a301SAlan Cox entry = entry->next; 2652acd9a301SAlan Cox } 2653acd9a301SAlan Cox rv = KERN_SUCCESS; 2654acd9a301SAlan Cox done: 2655e27e17b7SAlan Cox need_wakeup = FALSE; 2656acd9a301SAlan Cox if (first_entry == NULL) { 2657acd9a301SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2658cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2659cbef13d8SAlan Cox first_entry = first_entry->next; 2660cbef13d8SAlan Cox else 2661acd9a301SAlan Cox KASSERT(result, ("vm_map_unwire: lookup failed")); 2662acd9a301SAlan Cox } 26631c5196c3SKonstantin Belousov for (entry = first_entry; entry->start < end; entry = entry->next) { 26640acea7dfSKonstantin Belousov /* 26650acea7dfSKonstantin Belousov * If VM_MAP_WIRE_HOLESOK was specified, an empty 26660acea7dfSKonstantin Belousov * space in the unwired region could have been mapped 26670acea7dfSKonstantin Belousov * while the map lock was dropped for draining 26680acea7dfSKonstantin Belousov * MAP_ENTRY_IN_TRANSITION. Moreover, another thread 26690acea7dfSKonstantin Belousov * could be simultaneously wiring this new mapping 26700acea7dfSKonstantin Belousov * entry. Detect these cases and skip any entries 26710acea7dfSKonstantin Belousov * marked as in transition by us. 26720acea7dfSKonstantin Belousov */ 26730acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 26740acea7dfSKonstantin Belousov entry->wiring_thread != curthread) { 26750acea7dfSKonstantin Belousov KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 26760acea7dfSKonstantin Belousov ("vm_map_unwire: !HOLESOK and new/changed entry")); 26770acea7dfSKonstantin Belousov continue; 26780acea7dfSKonstantin Belousov } 26790acea7dfSKonstantin Belousov 26803ffbc0cdSAlan Cox if (rv == KERN_SUCCESS && (!user_unwire || 26813ffbc0cdSAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED))) { 2682b2f3846aSAlan Cox if (user_unwire) 2683b2f3846aSAlan Cox entry->eflags &= ~MAP_ENTRY_USER_WIRED; 268403462509SAlan Cox if (entry->wired_count == 1) 268503462509SAlan Cox vm_map_entry_unwire(map, entry); 268603462509SAlan Cox else 2687b2f3846aSAlan Cox entry->wired_count--; 2688b2f3846aSAlan Cox } 26890acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 2690ff3ae454SKonstantin Belousov ("vm_map_unwire: in-transition flag missing %p", entry)); 2691ff3ae454SKonstantin Belousov KASSERT(entry->wiring_thread == curthread, 2692ff3ae454SKonstantin Belousov ("vm_map_unwire: alien wire %p", entry)); 2693acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_IN_TRANSITION; 26940acea7dfSKonstantin Belousov entry->wiring_thread = NULL; 2695acd9a301SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 2696acd9a301SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 2697acd9a301SAlan Cox need_wakeup = TRUE; 2698acd9a301SAlan Cox } 2699acd9a301SAlan Cox vm_map_simplify_entry(map, entry); 2700acd9a301SAlan Cox } 2701acd9a301SAlan Cox vm_map_unlock(map); 2702acd9a301SAlan Cox if (need_wakeup) 2703acd9a301SAlan Cox vm_map_wakeup(map); 2704acd9a301SAlan Cox return (rv); 2705acd9a301SAlan Cox } 2706acd9a301SAlan Cox 2707acd9a301SAlan Cox /* 270866cd575bSAlan Cox * vm_map_wire_entry_failure: 270966cd575bSAlan Cox * 271066cd575bSAlan Cox * Handle a wiring failure on the given entry. 271166cd575bSAlan Cox * 271266cd575bSAlan Cox * The map should be locked. 271366cd575bSAlan Cox */ 271466cd575bSAlan Cox static void 271566cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry, 271666cd575bSAlan Cox vm_offset_t failed_addr) 271766cd575bSAlan Cox { 271866cd575bSAlan Cox 271966cd575bSAlan Cox VM_MAP_ASSERT_LOCKED(map); 272066cd575bSAlan Cox KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 && 272166cd575bSAlan Cox entry->wired_count == 1, 272266cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p isn't being wired", entry)); 272366cd575bSAlan Cox KASSERT(failed_addr < entry->end, 272466cd575bSAlan Cox ("vm_map_wire_entry_failure: entry %p was fully wired", entry)); 272566cd575bSAlan Cox 272666cd575bSAlan Cox /* 272766cd575bSAlan Cox * If any pages at the start of this entry were successfully wired, 272866cd575bSAlan Cox * then unwire them. 272966cd575bSAlan Cox */ 273066cd575bSAlan Cox if (failed_addr > entry->start) { 273166cd575bSAlan Cox pmap_unwire(map->pmap, entry->start, failed_addr); 273266cd575bSAlan Cox vm_object_unwire(entry->object.vm_object, entry->offset, 273366cd575bSAlan Cox failed_addr - entry->start, PQ_ACTIVE); 273466cd575bSAlan Cox } 273566cd575bSAlan Cox 273666cd575bSAlan Cox /* 273766cd575bSAlan Cox * Assign an out-of-range value to represent the failure to wire this 273866cd575bSAlan Cox * entry. 273966cd575bSAlan Cox */ 274066cd575bSAlan Cox entry->wired_count = -1; 274166cd575bSAlan Cox } 274266cd575bSAlan Cox 274366cd575bSAlan Cox /* 2744e27e17b7SAlan Cox * vm_map_wire: 2745e27e17b7SAlan Cox * 2746e27e17b7SAlan Cox * Implements both kernel and user wiring. 2747e27e17b7SAlan Cox */ 2748e27e17b7SAlan Cox int 2749e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end, 2750abd498aaSBruce M Simpson int flags) 2751e27e17b7SAlan Cox { 275212d7cc84SAlan Cox vm_map_entry_t entry, first_entry, tmp_entry; 275366cd575bSAlan Cox vm_offset_t faddr, saved_end, saved_start; 275412d7cc84SAlan Cox unsigned int last_timestamp; 275512d7cc84SAlan Cox int rv; 275666cd575bSAlan Cox boolean_t need_wakeup, result, user_wire; 2757e4cd31ddSJeff Roberson vm_prot_t prot; 2758e27e17b7SAlan Cox 275979e9451fSKonstantin Belousov if (start == end) 276079e9451fSKonstantin Belousov return (KERN_SUCCESS); 2761e4cd31ddSJeff Roberson prot = 0; 2762e4cd31ddSJeff Roberson if (flags & VM_MAP_WIRE_WRITE) 2763e4cd31ddSJeff Roberson prot |= VM_PROT_WRITE; 2764abd498aaSBruce M Simpson user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE; 276512d7cc84SAlan Cox vm_map_lock(map); 276612d7cc84SAlan Cox VM_MAP_RANGE_CHECK(map, start, end); 276712d7cc84SAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) { 2768abd498aaSBruce M Simpson if (flags & VM_MAP_WIRE_HOLESOK) 2769cbef13d8SAlan Cox first_entry = first_entry->next; 2770abd498aaSBruce M Simpson else { 277112d7cc84SAlan Cox vm_map_unlock(map); 277212d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 277312d7cc84SAlan Cox } 2774abd498aaSBruce M Simpson } 277512d7cc84SAlan Cox last_timestamp = map->timestamp; 277612d7cc84SAlan Cox entry = first_entry; 27771c5196c3SKonstantin Belousov while (entry->start < end) { 277812d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_IN_TRANSITION) { 277912d7cc84SAlan Cox /* 278012d7cc84SAlan Cox * We have not yet clipped the entry. 278112d7cc84SAlan Cox */ 278212d7cc84SAlan Cox saved_start = (start >= entry->start) ? start : 278312d7cc84SAlan Cox entry->start; 278412d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 27858ce2d00aSPawel Jakub Dawidek if (vm_map_unlock_and_wait(map, 0)) { 278612d7cc84SAlan Cox /* 278712d7cc84SAlan Cox * Allow interruption of user wiring? 278812d7cc84SAlan Cox */ 278912d7cc84SAlan Cox } 279012d7cc84SAlan Cox vm_map_lock(map); 279112d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 279212d7cc84SAlan Cox /* 279312d7cc84SAlan Cox * Look again for the entry because the map was 279412d7cc84SAlan Cox * modified while it was unlocked. 279512d7cc84SAlan Cox * Specifically, the entry may have been 279612d7cc84SAlan Cox * clipped, merged, or deleted. 279712d7cc84SAlan Cox */ 279812d7cc84SAlan Cox if (!vm_map_lookup_entry(map, saved_start, 279912d7cc84SAlan Cox &tmp_entry)) { 2800cbef13d8SAlan Cox if (flags & VM_MAP_WIRE_HOLESOK) 2801cbef13d8SAlan Cox tmp_entry = tmp_entry->next; 2802cbef13d8SAlan Cox else { 280312d7cc84SAlan Cox if (saved_start == start) { 280412d7cc84SAlan Cox /* 280512d7cc84SAlan Cox * first_entry has been deleted. 280612d7cc84SAlan Cox */ 280712d7cc84SAlan Cox vm_map_unlock(map); 280812d7cc84SAlan Cox return (KERN_INVALID_ADDRESS); 280912d7cc84SAlan Cox } 281012d7cc84SAlan Cox end = saved_start; 281112d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 281212d7cc84SAlan Cox goto done; 281312d7cc84SAlan Cox } 2814cbef13d8SAlan Cox } 281512d7cc84SAlan Cox if (entry == first_entry) 281612d7cc84SAlan Cox first_entry = tmp_entry; 281712d7cc84SAlan Cox else 281812d7cc84SAlan Cox first_entry = NULL; 281912d7cc84SAlan Cox entry = tmp_entry; 282012d7cc84SAlan Cox } 282112d7cc84SAlan Cox last_timestamp = map->timestamp; 282212d7cc84SAlan Cox continue; 282312d7cc84SAlan Cox } 282412d7cc84SAlan Cox vm_map_clip_start(map, entry, start); 282512d7cc84SAlan Cox vm_map_clip_end(map, entry, end); 282612d7cc84SAlan Cox /* 282712d7cc84SAlan Cox * Mark the entry in case the map lock is released. (See 282812d7cc84SAlan Cox * above.) 282912d7cc84SAlan Cox */ 2830ff3ae454SKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 && 2831ff3ae454SKonstantin Belousov entry->wiring_thread == NULL, 2832ff3ae454SKonstantin Belousov ("owned map entry %p", entry)); 283312d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_IN_TRANSITION; 28340acea7dfSKonstantin Belousov entry->wiring_thread = curthread; 2835e4cd31ddSJeff Roberson if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 2836e4cd31ddSJeff Roberson || (entry->protection & prot) != prot) { 2837529ab57bSKonstantin Belousov entry->eflags |= MAP_ENTRY_WIRE_SKIPPED; 28386d7e8091SKonstantin Belousov if ((flags & VM_MAP_WIRE_HOLESOK) == 0) { 28396d7e8091SKonstantin Belousov end = entry->end; 28406d7e8091SKonstantin Belousov rv = KERN_INVALID_ADDRESS; 28416d7e8091SKonstantin Belousov goto done; 28426d7e8091SKonstantin Belousov } 28436d7e8091SKonstantin Belousov goto next_entry; 28446d7e8091SKonstantin Belousov } 2845e4cd31ddSJeff Roberson if (entry->wired_count == 0) { 28460ada205eSBrian Feldman entry->wired_count++; 284712d7cc84SAlan Cox saved_start = entry->start; 284812d7cc84SAlan Cox saved_end = entry->end; 284966cd575bSAlan Cox 285012d7cc84SAlan Cox /* 285112d7cc84SAlan Cox * Release the map lock, relying on the in-transition 2852a5db445dSMax Laier * mark. Mark the map busy for fork. 285312d7cc84SAlan Cox */ 2854a5db445dSMax Laier vm_map_busy(map); 285512d7cc84SAlan Cox vm_map_unlock(map); 285666cd575bSAlan Cox 28570b695684SAlan Cox faddr = saved_start; 28580b695684SAlan Cox do { 285966cd575bSAlan Cox /* 286066cd575bSAlan Cox * Simulate a fault to get the page and enter 286166cd575bSAlan Cox * it into the physical map. 286266cd575bSAlan Cox */ 286366cd575bSAlan Cox if ((rv = vm_fault(map, faddr, VM_PROT_NONE, 28646a875bf9SKonstantin Belousov VM_FAULT_WIRE)) != KERN_SUCCESS) 286566cd575bSAlan Cox break; 28660b695684SAlan Cox } while ((faddr += PAGE_SIZE) < saved_end); 286712d7cc84SAlan Cox vm_map_lock(map); 2868a5db445dSMax Laier vm_map_unbusy(map); 286912d7cc84SAlan Cox if (last_timestamp + 1 != map->timestamp) { 287012d7cc84SAlan Cox /* 287112d7cc84SAlan Cox * Look again for the entry because the map was 287212d7cc84SAlan Cox * modified while it was unlocked. The entry 287312d7cc84SAlan Cox * may have been clipped, but NOT merged or 287412d7cc84SAlan Cox * deleted. 287512d7cc84SAlan Cox */ 287612d7cc84SAlan Cox result = vm_map_lookup_entry(map, saved_start, 287712d7cc84SAlan Cox &tmp_entry); 287812d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 287912d7cc84SAlan Cox if (entry == first_entry) 288012d7cc84SAlan Cox first_entry = tmp_entry; 288112d7cc84SAlan Cox else 288212d7cc84SAlan Cox first_entry = NULL; 288312d7cc84SAlan Cox entry = tmp_entry; 288428c58286SAlan Cox while (entry->end < saved_end) { 288566cd575bSAlan Cox /* 288666cd575bSAlan Cox * In case of failure, handle entries 288766cd575bSAlan Cox * that were not fully wired here; 288866cd575bSAlan Cox * fully wired entries are handled 288966cd575bSAlan Cox * later. 289066cd575bSAlan Cox */ 289166cd575bSAlan Cox if (rv != KERN_SUCCESS && 289266cd575bSAlan Cox faddr < entry->end) 289366cd575bSAlan Cox vm_map_wire_entry_failure(map, 289466cd575bSAlan Cox entry, faddr); 289512d7cc84SAlan Cox entry = entry->next; 289612d7cc84SAlan Cox } 289728c58286SAlan Cox } 289812d7cc84SAlan Cox last_timestamp = map->timestamp; 289912d7cc84SAlan Cox if (rv != KERN_SUCCESS) { 290066cd575bSAlan Cox vm_map_wire_entry_failure(map, entry, faddr); 290112d7cc84SAlan Cox end = entry->end; 290212d7cc84SAlan Cox goto done; 290312d7cc84SAlan Cox } 29040ada205eSBrian Feldman } else if (!user_wire || 29050ada205eSBrian Feldman (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 29060ada205eSBrian Feldman entry->wired_count++; 290712d7cc84SAlan Cox } 290812d7cc84SAlan Cox /* 290912d7cc84SAlan Cox * Check the map for holes in the specified region. 2910abd498aaSBruce M Simpson * If VM_MAP_WIRE_HOLESOK was specified, skip this check. 291112d7cc84SAlan Cox */ 29126d7e8091SKonstantin Belousov next_entry: 2913f141ed73SKonstantin Belousov if ((flags & VM_MAP_WIRE_HOLESOK) == 0 && 29141c5196c3SKonstantin Belousov entry->end < end && entry->next->start > entry->end) { 291512d7cc84SAlan Cox end = entry->end; 291612d7cc84SAlan Cox rv = KERN_INVALID_ADDRESS; 291712d7cc84SAlan Cox goto done; 291812d7cc84SAlan Cox } 291912d7cc84SAlan Cox entry = entry->next; 292012d7cc84SAlan Cox } 292112d7cc84SAlan Cox rv = KERN_SUCCESS; 292212d7cc84SAlan Cox done: 292312d7cc84SAlan Cox need_wakeup = FALSE; 292412d7cc84SAlan Cox if (first_entry == NULL) { 292512d7cc84SAlan Cox result = vm_map_lookup_entry(map, start, &first_entry); 2926cbef13d8SAlan Cox if (!result && (flags & VM_MAP_WIRE_HOLESOK)) 2927cbef13d8SAlan Cox first_entry = first_entry->next; 2928cbef13d8SAlan Cox else 292912d7cc84SAlan Cox KASSERT(result, ("vm_map_wire: lookup failed")); 293012d7cc84SAlan Cox } 29311c5196c3SKonstantin Belousov for (entry = first_entry; entry->start < end; entry = entry->next) { 29320acea7dfSKonstantin Belousov /* 29330acea7dfSKonstantin Belousov * If VM_MAP_WIRE_HOLESOK was specified, an empty 29340acea7dfSKonstantin Belousov * space in the unwired region could have been mapped 29350acea7dfSKonstantin Belousov * while the map lock was dropped for faulting in the 29360acea7dfSKonstantin Belousov * pages or draining MAP_ENTRY_IN_TRANSITION. 29370acea7dfSKonstantin Belousov * Moreover, another thread could be simultaneously 29380acea7dfSKonstantin Belousov * wiring this new mapping entry. Detect these cases 2939546bb2d7SKonstantin Belousov * and skip any entries marked as in transition not by us. 29400acea7dfSKonstantin Belousov */ 29410acea7dfSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 || 29420acea7dfSKonstantin Belousov entry->wiring_thread != curthread) { 29430acea7dfSKonstantin Belousov KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0, 29440acea7dfSKonstantin Belousov ("vm_map_wire: !HOLESOK and new/changed entry")); 29450acea7dfSKonstantin Belousov continue; 29460acea7dfSKonstantin Belousov } 29470acea7dfSKonstantin Belousov 2948546bb2d7SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0) 2949546bb2d7SKonstantin Belousov goto next_entry_done; 2950546bb2d7SKonstantin Belousov 295112d7cc84SAlan Cox if (rv == KERN_SUCCESS) { 295212d7cc84SAlan Cox if (user_wire) 295312d7cc84SAlan Cox entry->eflags |= MAP_ENTRY_USER_WIRED; 295428c58286SAlan Cox } else if (entry->wired_count == -1) { 295528c58286SAlan Cox /* 295628c58286SAlan Cox * Wiring failed on this entry. Thus, unwiring is 295728c58286SAlan Cox * unnecessary. 295828c58286SAlan Cox */ 295928c58286SAlan Cox entry->wired_count = 0; 296003462509SAlan Cox } else if (!user_wire || 296103462509SAlan Cox (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) { 296266cd575bSAlan Cox /* 296366cd575bSAlan Cox * Undo the wiring. Wiring succeeded on this entry 296466cd575bSAlan Cox * but failed on a later entry. 296566cd575bSAlan Cox */ 296603462509SAlan Cox if (entry->wired_count == 1) 296703462509SAlan Cox vm_map_entry_unwire(map, entry); 296803462509SAlan Cox else 296912d7cc84SAlan Cox entry->wired_count--; 297012d7cc84SAlan Cox } 29716d7e8091SKonstantin Belousov next_entry_done: 29720acea7dfSKonstantin Belousov KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0, 29730acea7dfSKonstantin Belousov ("vm_map_wire: in-transition flag missing %p", entry)); 29740acea7dfSKonstantin Belousov KASSERT(entry->wiring_thread == curthread, 29750acea7dfSKonstantin Belousov ("vm_map_wire: alien wire %p", entry)); 29760acea7dfSKonstantin Belousov entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION | 29770acea7dfSKonstantin Belousov MAP_ENTRY_WIRE_SKIPPED); 29780acea7dfSKonstantin Belousov entry->wiring_thread = NULL; 297912d7cc84SAlan Cox if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) { 298012d7cc84SAlan Cox entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP; 298112d7cc84SAlan Cox need_wakeup = TRUE; 298212d7cc84SAlan Cox } 298312d7cc84SAlan Cox vm_map_simplify_entry(map, entry); 298412d7cc84SAlan Cox } 298512d7cc84SAlan Cox vm_map_unlock(map); 298612d7cc84SAlan Cox if (need_wakeup) 298712d7cc84SAlan Cox vm_map_wakeup(map); 298812d7cc84SAlan Cox return (rv); 2989e27e17b7SAlan Cox } 2990e27e17b7SAlan Cox 2991e27e17b7SAlan Cox /* 2992950f8459SAlan Cox * vm_map_sync 2993df8bae1dSRodney W. Grimes * 2994df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 2995df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 2996df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 2997df8bae1dSRodney W. Grimes * 2998637315edSAlan Cox * If the size of the region from start to end is zero, we are 2999637315edSAlan Cox * supposed to flush all modified pages within the region containing 3000637315edSAlan Cox * start. Unfortunately, a region can be split or coalesced with 3001637315edSAlan Cox * neighboring regions, making it difficult to determine what the 3002637315edSAlan Cox * original region was. Therefore, we approximate this requirement by 3003637315edSAlan Cox * flushing the current region containing start. 3004637315edSAlan Cox * 3005df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 3006df8bae1dSRodney W. Grimes */ 3007df8bae1dSRodney W. Grimes int 3008950f8459SAlan Cox vm_map_sync( 30091b40f8c0SMatthew Dillon vm_map_t map, 30101b40f8c0SMatthew Dillon vm_offset_t start, 30111b40f8c0SMatthew Dillon vm_offset_t end, 30121b40f8c0SMatthew Dillon boolean_t syncio, 30131b40f8c0SMatthew Dillon boolean_t invalidate) 3014df8bae1dSRodney W. Grimes { 3015c0877f10SJohn Dyson vm_map_entry_t current; 3016df8bae1dSRodney W. Grimes vm_map_entry_t entry; 3017df8bae1dSRodney W. Grimes vm_size_t size; 3018df8bae1dSRodney W. Grimes vm_object_t object; 3019a316d390SJohn Dyson vm_ooffset_t offset; 3020e53fa61bSKonstantin Belousov unsigned int last_timestamp; 3021126d6082SKonstantin Belousov boolean_t failed; 3022df8bae1dSRodney W. Grimes 3023df8bae1dSRodney W. Grimes vm_map_lock_read(map); 3024df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 3025df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 3026df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3027df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 3028637315edSAlan Cox } else if (start == end) { 3029637315edSAlan Cox start = entry->start; 3030637315edSAlan Cox end = entry->end; 3031df8bae1dSRodney W. Grimes } 3032df8bae1dSRodney W. Grimes /* 3033b7b7cd44SAlan Cox * Make a first pass to check for user-wired memory and holes. 3034df8bae1dSRodney W. Grimes */ 30351c5196c3SKonstantin Belousov for (current = entry; current->start < end; current = current->next) { 3036b7b7cd44SAlan Cox if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) { 3037df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3038df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 3039df8bae1dSRodney W. Grimes } 3040df8bae1dSRodney W. Grimes if (end > current->end && 30411c5196c3SKonstantin Belousov current->end != current->next->start) { 3042df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3043df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 3044df8bae1dSRodney W. Grimes } 3045df8bae1dSRodney W. Grimes } 3046df8bae1dSRodney W. Grimes 30472cf13952SAlan Cox if (invalidate) 3048bc105a67SAlan Cox pmap_remove(map->pmap, start, end); 3049126d6082SKonstantin Belousov failed = FALSE; 30502cf13952SAlan Cox 3051df8bae1dSRodney W. Grimes /* 3052df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 3053df8bae1dSRodney W. Grimes * objects as we go. 3054df8bae1dSRodney W. Grimes */ 30551c5196c3SKonstantin Belousov for (current = entry; current->start < end;) { 3056df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 3057df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 30589fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 3059c0877f10SJohn Dyson vm_map_t smap; 3060df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 3061df8bae1dSRodney W. Grimes vm_size_t tsize; 3062df8bae1dSRodney W. Grimes 30639fdfe602SMatthew Dillon smap = current->object.sub_map; 3064df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 3065df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 3066df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 3067df8bae1dSRodney W. Grimes if (tsize < size) 3068df8bae1dSRodney W. Grimes size = tsize; 3069df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 3070df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 3071df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 3072df8bae1dSRodney W. Grimes } else { 3073df8bae1dSRodney W. Grimes object = current->object.vm_object; 3074df8bae1dSRodney W. Grimes } 3075e53fa61bSKonstantin Belousov vm_object_reference(object); 3076e53fa61bSKonstantin Belousov last_timestamp = map->timestamp; 3077e53fa61bSKonstantin Belousov vm_map_unlock_read(map); 3078126d6082SKonstantin Belousov if (!vm_object_sync(object, offset, size, syncio, invalidate)) 3079126d6082SKonstantin Belousov failed = TRUE; 3080df8bae1dSRodney W. Grimes start += size; 3081e53fa61bSKonstantin Belousov vm_object_deallocate(object); 3082e53fa61bSKonstantin Belousov vm_map_lock_read(map); 3083e53fa61bSKonstantin Belousov if (last_timestamp == map->timestamp || 3084e53fa61bSKonstantin Belousov !vm_map_lookup_entry(map, start, ¤t)) 3085e53fa61bSKonstantin Belousov current = current->next; 3086df8bae1dSRodney W. Grimes } 3087df8bae1dSRodney W. Grimes 3088df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 3089126d6082SKonstantin Belousov return (failed ? KERN_FAILURE : KERN_SUCCESS); 3090df8bae1dSRodney W. Grimes } 3091df8bae1dSRodney W. Grimes 3092df8bae1dSRodney W. Grimes /* 3093df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 3094df8bae1dSRodney W. Grimes * 3095df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 3096df8bae1dSRodney W. Grimes * 3097df8bae1dSRodney W. Grimes * The map in question should be locked. 3098df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 3099df8bae1dSRodney W. Grimes */ 31000362d7d7SJohn Dyson static void 31011b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 3102df8bae1dSRodney W. Grimes { 310303462509SAlan Cox 310403462509SAlan Cox VM_MAP_ASSERT_LOCKED(map); 310503462509SAlan Cox KASSERT(entry->wired_count > 0, 310603462509SAlan Cox ("vm_map_entry_unwire: entry %p isn't wired", entry)); 310703462509SAlan Cox pmap_unwire(map->pmap, entry->start, entry->end); 310803462509SAlan Cox vm_object_unwire(entry->object.vm_object, entry->offset, entry->end - 310903462509SAlan Cox entry->start, PQ_ACTIVE); 3110df8bae1dSRodney W. Grimes entry->wired_count = 0; 3111df8bae1dSRodney W. Grimes } 3112df8bae1dSRodney W. Grimes 31130b367bd8SKonstantin Belousov static void 31140b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map) 31150b367bd8SKonstantin Belousov { 31160b367bd8SKonstantin Belousov 31170b367bd8SKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) 31180b367bd8SKonstantin Belousov vm_object_deallocate(entry->object.vm_object); 31190b367bd8SKonstantin Belousov uma_zfree(system_map ? kmapentzone : mapentzone, entry); 31200b367bd8SKonstantin Belousov } 31210b367bd8SKonstantin Belousov 3122df8bae1dSRodney W. Grimes /* 3123df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 3124df8bae1dSRodney W. Grimes * 3125df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 3126df8bae1dSRodney W. Grimes */ 31270362d7d7SJohn Dyson static void 31281b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 3129df8bae1dSRodney W. Grimes { 313032a89c32SAlan Cox vm_object_t object; 31313364c323SKonstantin Belousov vm_pindex_t offidxstart, offidxend, count, size1; 3132d1780e8dSKonstantin Belousov vm_size_t size; 313332a89c32SAlan Cox 3134df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 31353364c323SKonstantin Belousov object = entry->object.vm_object; 313619bd0d9cSKonstantin Belousov 313719bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) { 313819bd0d9cSKonstantin Belousov MPASS(entry->cred == NULL); 313919bd0d9cSKonstantin Belousov MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0); 314019bd0d9cSKonstantin Belousov MPASS(object == NULL); 314119bd0d9cSKonstantin Belousov vm_map_entry_deallocate(entry, map->system_map); 314219bd0d9cSKonstantin Belousov return; 314319bd0d9cSKonstantin Belousov } 314419bd0d9cSKonstantin Belousov 31453364c323SKonstantin Belousov size = entry->end - entry->start; 31463364c323SKonstantin Belousov map->size -= size; 31473364c323SKonstantin Belousov 3148ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 3149ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred); 3150ef694c1aSEdward Tomasz Napierala crfree(entry->cred); 31513364c323SKonstantin Belousov } 3152df8bae1dSRodney W. Grimes 315332a89c32SAlan Cox if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 31543364c323SKonstantin Belousov (object != NULL)) { 3155ef694c1aSEdward Tomasz Napierala KASSERT(entry->cred == NULL || object->cred == NULL || 31563364c323SKonstantin Belousov (entry->eflags & MAP_ENTRY_NEEDS_COPY), 3157ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry)); 3158d1780e8dSKonstantin Belousov count = atop(size); 315932a89c32SAlan Cox offidxstart = OFF_TO_IDX(entry->offset); 316032a89c32SAlan Cox offidxend = offidxstart + count; 316189f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 31629a4ee196SKonstantin Belousov if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT | 31639a4ee196SKonstantin Belousov OBJ_ONEMAPPING)) == OBJ_ONEMAPPING || 31642e47807cSJeff Roberson object == kernel_object)) { 316532a89c32SAlan Cox vm_object_collapse(object); 31666bbee8e2SAlan Cox 31676bbee8e2SAlan Cox /* 31686bbee8e2SAlan Cox * The option OBJPR_NOTMAPPED can be passed here 31696bbee8e2SAlan Cox * because vm_map_delete() already performed 31706bbee8e2SAlan Cox * pmap_remove() on the only mapping to this range 31716bbee8e2SAlan Cox * of pages. 31726bbee8e2SAlan Cox */ 31736bbee8e2SAlan Cox vm_object_page_remove(object, offidxstart, offidxend, 31746bbee8e2SAlan Cox OBJPR_NOTMAPPED); 317532a89c32SAlan Cox if (object->type == OBJT_SWAP) 31769a4ee196SKonstantin Belousov swap_pager_freespace(object, offidxstart, 31779a4ee196SKonstantin Belousov count); 317832a89c32SAlan Cox if (offidxend >= object->size && 31793364c323SKonstantin Belousov offidxstart < object->size) { 31803364c323SKonstantin Belousov size1 = object->size; 318132a89c32SAlan Cox object->size = offidxstart; 3182ef694c1aSEdward Tomasz Napierala if (object->cred != NULL) { 31833364c323SKonstantin Belousov size1 -= object->size; 31843364c323SKonstantin Belousov KASSERT(object->charge >= ptoa(size1), 31859a4ee196SKonstantin Belousov ("object %p charge < 0", object)); 31869a4ee196SKonstantin Belousov swap_release_by_cred(ptoa(size1), 31879a4ee196SKonstantin Belousov object->cred); 31883364c323SKonstantin Belousov object->charge -= ptoa(size1); 31893364c323SKonstantin Belousov } 31903364c323SKonstantin Belousov } 319132a89c32SAlan Cox } 319289f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3193897d81a0SKonstantin Belousov } else 3194897d81a0SKonstantin Belousov entry->object.vm_object = NULL; 31950b367bd8SKonstantin Belousov if (map->system_map) 31960b367bd8SKonstantin Belousov vm_map_entry_deallocate(entry, TRUE); 31970b367bd8SKonstantin Belousov else { 31980b367bd8SKonstantin Belousov entry->next = curthread->td_map_def_user; 31990b367bd8SKonstantin Belousov curthread->td_map_def_user = entry; 32000b367bd8SKonstantin Belousov } 3201df8bae1dSRodney W. Grimes } 3202df8bae1dSRodney W. Grimes 3203df8bae1dSRodney W. Grimes /* 3204df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 3205df8bae1dSRodney W. Grimes * 3206df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 3207df8bae1dSRodney W. Grimes * map. 3208df8bae1dSRodney W. Grimes */ 3209df8bae1dSRodney W. Grimes int 3210655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 3211df8bae1dSRodney W. Grimes { 3212c0877f10SJohn Dyson vm_map_entry_t entry; 3213df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 3214df8bae1dSRodney W. Grimes 32153a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(map); 321679e9451fSKonstantin Belousov if (start == end) 321779e9451fSKonstantin Belousov return (KERN_SUCCESS); 32183a0916b8SKonstantin Belousov 3219df8bae1dSRodney W. Grimes /* 3220df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 3221df8bae1dSRodney W. Grimes */ 3222876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 3223df8bae1dSRodney W. Grimes entry = first_entry->next; 3224876318ecSAlan Cox else { 3225df8bae1dSRodney W. Grimes entry = first_entry; 3226df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 3227df8bae1dSRodney W. Grimes } 3228df8bae1dSRodney W. Grimes 3229df8bae1dSRodney W. Grimes /* 3230df8bae1dSRodney W. Grimes * Step through all entries in this region 3231df8bae1dSRodney W. Grimes */ 32321c5196c3SKonstantin Belousov while (entry->start < end) { 3233df8bae1dSRodney W. Grimes vm_map_entry_t next; 3234df8bae1dSRodney W. Grimes 323573b2baceSAlan Cox /* 323673b2baceSAlan Cox * Wait for wiring or unwiring of an entry to complete. 32377c938963SBrian Feldman * Also wait for any system wirings to disappear on 32387c938963SBrian Feldman * user maps. 323973b2baceSAlan Cox */ 32407c938963SBrian Feldman if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 || 32417c938963SBrian Feldman (vm_map_pmap(map) != kernel_pmap && 32427c938963SBrian Feldman vm_map_entry_system_wired_count(entry) != 0)) { 324373b2baceSAlan Cox unsigned int last_timestamp; 324473b2baceSAlan Cox vm_offset_t saved_start; 324573b2baceSAlan Cox vm_map_entry_t tmp_entry; 324673b2baceSAlan Cox 324773b2baceSAlan Cox saved_start = entry->start; 324873b2baceSAlan Cox entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP; 324973b2baceSAlan Cox last_timestamp = map->timestamp; 32508ce2d00aSPawel Jakub Dawidek (void) vm_map_unlock_and_wait(map, 0); 325173b2baceSAlan Cox vm_map_lock(map); 325273b2baceSAlan Cox if (last_timestamp + 1 != map->timestamp) { 325373b2baceSAlan Cox /* 325473b2baceSAlan Cox * Look again for the entry because the map was 325573b2baceSAlan Cox * modified while it was unlocked. 325673b2baceSAlan Cox * Specifically, the entry may have been 325773b2baceSAlan Cox * clipped, merged, or deleted. 325873b2baceSAlan Cox */ 325973b2baceSAlan Cox if (!vm_map_lookup_entry(map, saved_start, 326073b2baceSAlan Cox &tmp_entry)) 326173b2baceSAlan Cox entry = tmp_entry->next; 326273b2baceSAlan Cox else { 326373b2baceSAlan Cox entry = tmp_entry; 326473b2baceSAlan Cox vm_map_clip_start(map, entry, 326573b2baceSAlan Cox saved_start); 326673b2baceSAlan Cox } 326773b2baceSAlan Cox } 326873b2baceSAlan Cox continue; 326973b2baceSAlan Cox } 3270df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 3271df8bae1dSRodney W. Grimes 3272c0877f10SJohn Dyson next = entry->next; 3273df8bae1dSRodney W. Grimes 3274df8bae1dSRodney W. Grimes /* 32750d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 32760d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 3277df8bae1dSRodney W. Grimes */ 3278be7be412SKonstantin Belousov if (entry->wired_count != 0) 3279df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 3280df8bae1dSRodney W. Grimes 328132f0fefcSKonstantin Belousov /* 328232f0fefcSKonstantin Belousov * Remove mappings for the pages, but only if the 328332f0fefcSKonstantin Belousov * mappings could exist. For instance, it does not 328432f0fefcSKonstantin Belousov * make sense to call pmap_remove() for guard entries. 328532f0fefcSKonstantin Belousov */ 328632f0fefcSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) != 0 || 328732f0fefcSKonstantin Belousov entry->object.vm_object != NULL) 328832a89c32SAlan Cox pmap_remove(map->pmap, entry->start, entry->end); 3289df8bae1dSRodney W. Grimes 3290fa50a355SKonstantin Belousov if (entry->end == map->anon_loc) 3291fa50a355SKonstantin Belousov map->anon_loc = entry->start; 3292fa50a355SKonstantin Belousov 3293df8bae1dSRodney W. Grimes /* 3294e608cc3cSKonstantin Belousov * Delete the entry only after removing all pmap 3295e608cc3cSKonstantin Belousov * entries pointing to its pages. (Otherwise, its 3296e608cc3cSKonstantin Belousov * page frames may be reallocated, and any modify bits 3297e608cc3cSKonstantin Belousov * will be set in the wrong object!) 3298df8bae1dSRodney W. Grimes */ 3299df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 3300df8bae1dSRodney W. Grimes entry = next; 3301df8bae1dSRodney W. Grimes } 3302df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 3303df8bae1dSRodney W. Grimes } 3304df8bae1dSRodney W. Grimes 3305df8bae1dSRodney W. Grimes /* 3306df8bae1dSRodney W. Grimes * vm_map_remove: 3307df8bae1dSRodney W. Grimes * 3308df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 3309df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 3310df8bae1dSRodney W. Grimes */ 3311df8bae1dSRodney W. Grimes int 33121b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 3313df8bae1dSRodney W. Grimes { 33146eaee3feSAlan Cox int result; 3315df8bae1dSRodney W. Grimes 3316df8bae1dSRodney W. Grimes vm_map_lock(map); 3317df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 3318655c3490SKonstantin Belousov result = vm_map_delete(map, start, end); 3319df8bae1dSRodney W. Grimes vm_map_unlock(map); 3320df8bae1dSRodney W. Grimes return (result); 3321df8bae1dSRodney W. Grimes } 3322df8bae1dSRodney W. Grimes 3323df8bae1dSRodney W. Grimes /* 3324df8bae1dSRodney W. Grimes * vm_map_check_protection: 3325df8bae1dSRodney W. Grimes * 33262d5c7e45SMatthew Dillon * Assert that the target map allows the specified privilege on the 33272d5c7e45SMatthew Dillon * entire address region given. The entire region must be allocated. 33282d5c7e45SMatthew Dillon * 33292d5c7e45SMatthew Dillon * WARNING! This code does not and should not check whether the 33302d5c7e45SMatthew Dillon * contents of the region is accessible. For example a smaller file 33312d5c7e45SMatthew Dillon * might be mapped into a larger address space. 33322d5c7e45SMatthew Dillon * 33332d5c7e45SMatthew Dillon * NOTE! This code is also called by munmap(). 3334d8834602SAlan Cox * 3335d8834602SAlan Cox * The map must be locked. A read lock is sufficient. 3336df8bae1dSRodney W. Grimes */ 33370d94caffSDavid Greenman boolean_t 3338b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 3339b9dcd593SBruce Evans vm_prot_t protection) 3340df8bae1dSRodney W. Grimes { 3341c0877f10SJohn Dyson vm_map_entry_t entry; 3342df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 3343df8bae1dSRodney W. Grimes 3344d8834602SAlan Cox if (!vm_map_lookup_entry(map, start, &tmp_entry)) 3345df8bae1dSRodney W. Grimes return (FALSE); 3346df8bae1dSRodney W. Grimes entry = tmp_entry; 3347df8bae1dSRodney W. Grimes 3348df8bae1dSRodney W. Grimes while (start < end) { 3349df8bae1dSRodney W. Grimes /* 3350df8bae1dSRodney W. Grimes * No holes allowed! 3351df8bae1dSRodney W. Grimes */ 3352d8834602SAlan Cox if (start < entry->start) 3353df8bae1dSRodney W. Grimes return (FALSE); 3354df8bae1dSRodney W. Grimes /* 3355df8bae1dSRodney W. Grimes * Check protection associated with entry. 3356df8bae1dSRodney W. Grimes */ 3357d8834602SAlan Cox if ((entry->protection & protection) != protection) 3358df8bae1dSRodney W. Grimes return (FALSE); 3359df8bae1dSRodney W. Grimes /* go to next entry */ 3360df8bae1dSRodney W. Grimes start = entry->end; 3361df8bae1dSRodney W. Grimes entry = entry->next; 3362df8bae1dSRodney W. Grimes } 3363df8bae1dSRodney W. Grimes return (TRUE); 3364df8bae1dSRodney W. Grimes } 3365df8bae1dSRodney W. Grimes 336686524867SJohn Dyson /* 3367df8bae1dSRodney W. Grimes * vm_map_copy_entry: 3368df8bae1dSRodney W. Grimes * 3369df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 3370df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 3371df8bae1dSRodney W. Grimes */ 3372f708ef1bSPoul-Henning Kamp static void 33731b40f8c0SMatthew Dillon vm_map_copy_entry( 33741b40f8c0SMatthew Dillon vm_map_t src_map, 33751b40f8c0SMatthew Dillon vm_map_t dst_map, 33761b40f8c0SMatthew Dillon vm_map_entry_t src_entry, 33773364c323SKonstantin Belousov vm_map_entry_t dst_entry, 33783364c323SKonstantin Belousov vm_ooffset_t *fork_charge) 3379df8bae1dSRodney W. Grimes { 3380c0877f10SJohn Dyson vm_object_t src_object; 338184110e7eSKonstantin Belousov vm_map_entry_t fake_entry; 33823364c323SKonstantin Belousov vm_offset_t size; 3383ef694c1aSEdward Tomasz Napierala struct ucred *cred; 33843364c323SKonstantin Belousov int charged; 3385c0877f10SJohn Dyson 33863a0916b8SKonstantin Belousov VM_MAP_ASSERT_LOCKED(dst_map); 33873a0916b8SKonstantin Belousov 33889fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 3389df8bae1dSRodney W. Grimes return; 3390df8bae1dSRodney W. Grimes 3391afaa41f6SAlan Cox if (src_entry->wired_count == 0 || 3392afaa41f6SAlan Cox (src_entry->protection & VM_PROT_WRITE) == 0) { 3393df8bae1dSRodney W. Grimes /* 33940d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 33950d94caffSDavid Greenman * write-protected. 3396df8bae1dSRodney W. Grimes */ 3397d9a9209aSAlan Cox if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 && 3398d9a9209aSAlan Cox (src_entry->protection & VM_PROT_WRITE) != 0) { 3399df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 3400df8bae1dSRodney W. Grimes src_entry->start, 3401df8bae1dSRodney W. Grimes src_entry->end, 3402df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 3403df8bae1dSRodney W. Grimes } 3404b18bfc3dSJohn Dyson 3405df8bae1dSRodney W. Grimes /* 3406df8bae1dSRodney W. Grimes * Make a copy of the object. 3407df8bae1dSRodney W. Grimes */ 34083364c323SKonstantin Belousov size = src_entry->end - src_entry->start; 34098aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 341089f6b863SAttilio Rao VM_OBJECT_WLOCK(src_object); 34113364c323SKonstantin Belousov charged = ENTRY_CHARGED(src_entry); 34129a4ee196SKonstantin Belousov if (src_object->handle == NULL && 3413c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 3414c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 3415c0877f10SJohn Dyson vm_object_collapse(src_object); 34169a4ee196SKonstantin Belousov if ((src_object->flags & (OBJ_NOSPLIT | 34179a4ee196SKonstantin Belousov OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 3418c5aaa06dSAlan Cox vm_object_split(src_entry); 34199a4ee196SKonstantin Belousov src_object = 34209a4ee196SKonstantin Belousov src_entry->object.vm_object; 3421a89c6258SAlan Cox } 3422a89c6258SAlan Cox } 3423b921a12bSAlan Cox vm_object_reference_locked(src_object); 3424069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 3425ef694c1aSEdward Tomasz Napierala if (src_entry->cred != NULL && 34263364c323SKonstantin Belousov !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) { 3427ef694c1aSEdward Tomasz Napierala KASSERT(src_object->cred == NULL, 3428ef694c1aSEdward Tomasz Napierala ("OVERCOMMIT: vm_map_copy_entry: cred %p", 34293364c323SKonstantin Belousov src_object)); 3430ef694c1aSEdward Tomasz Napierala src_object->cred = src_entry->cred; 34313364c323SKonstantin Belousov src_object->charge = size; 34323364c323SKonstantin Belousov } 343389f6b863SAttilio Rao VM_OBJECT_WUNLOCK(src_object); 3434c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 34353364c323SKonstantin Belousov if (charged) { 3436ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 3437ef694c1aSEdward Tomasz Napierala crhold(cred); 3438ef694c1aSEdward Tomasz Napierala dst_entry->cred = cred; 34393364c323SKonstantin Belousov *fork_charge += size; 34403364c323SKonstantin Belousov if (!(src_entry->eflags & 34413364c323SKonstantin Belousov MAP_ENTRY_NEEDS_COPY)) { 3442ef694c1aSEdward Tomasz Napierala crhold(cred); 3443ef694c1aSEdward Tomasz Napierala src_entry->cred = cred; 34443364c323SKonstantin Belousov *fork_charge += size; 34453364c323SKonstantin Belousov } 34463364c323SKonstantin Belousov } 34479a4ee196SKonstantin Belousov src_entry->eflags |= MAP_ENTRY_COW | 34489a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY; 34499a4ee196SKonstantin Belousov dst_entry->eflags |= MAP_ENTRY_COW | 34509a4ee196SKonstantin Belousov MAP_ENTRY_NEEDS_COPY; 3451b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 345284110e7eSKonstantin Belousov if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 345384110e7eSKonstantin Belousov /* 345484110e7eSKonstantin Belousov * MAP_ENTRY_VN_WRITECNT cannot 345584110e7eSKonstantin Belousov * indicate write reference from 345684110e7eSKonstantin Belousov * src_entry, since the entry is 345784110e7eSKonstantin Belousov * marked as needs copy. Allocate a 345884110e7eSKonstantin Belousov * fake entry that is used to 345984110e7eSKonstantin Belousov * decrement object->un_pager.vnp.writecount 346084110e7eSKonstantin Belousov * at the appropriate time. Attach 346184110e7eSKonstantin Belousov * fake_entry to the deferred list. 346284110e7eSKonstantin Belousov */ 346384110e7eSKonstantin Belousov fake_entry = vm_map_entry_create(dst_map); 346484110e7eSKonstantin Belousov fake_entry->eflags = MAP_ENTRY_VN_WRITECNT; 346584110e7eSKonstantin Belousov src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT; 346684110e7eSKonstantin Belousov vm_object_reference(src_object); 346784110e7eSKonstantin Belousov fake_entry->object.vm_object = src_object; 346884110e7eSKonstantin Belousov fake_entry->start = src_entry->start; 346984110e7eSKonstantin Belousov fake_entry->end = src_entry->end; 347084110e7eSKonstantin Belousov fake_entry->next = curthread->td_map_def_user; 347184110e7eSKonstantin Belousov curthread->td_map_def_user = fake_entry; 347284110e7eSKonstantin Belousov } 34730ec97ffcSKonstantin Belousov 34740ec97ffcSKonstantin Belousov pmap_copy(dst_map->pmap, src_map->pmap, 34750ec97ffcSKonstantin Belousov dst_entry->start, dst_entry->end - dst_entry->start, 34760ec97ffcSKonstantin Belousov src_entry->start); 3477b18bfc3dSJohn Dyson } else { 3478b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 3479b18bfc3dSJohn Dyson dst_entry->offset = 0; 3480ef694c1aSEdward Tomasz Napierala if (src_entry->cred != NULL) { 3481ef694c1aSEdward Tomasz Napierala dst_entry->cred = curthread->td_ucred; 3482ef694c1aSEdward Tomasz Napierala crhold(dst_entry->cred); 34833364c323SKonstantin Belousov *fork_charge += size; 34843364c323SKonstantin Belousov } 3485b18bfc3dSJohn Dyson } 34860d94caffSDavid Greenman } else { 3487df8bae1dSRodney W. Grimes /* 3488afaa41f6SAlan Cox * We don't want to make writeable wired pages copy-on-write. 3489afaa41f6SAlan Cox * Immediately copy these pages into the new map by simulating 3490afaa41f6SAlan Cox * page faults. The new pages are pageable. 3491df8bae1dSRodney W. Grimes */ 3492121fd461SKonstantin Belousov vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry, 3493121fd461SKonstantin Belousov fork_charge); 3494df8bae1dSRodney W. Grimes } 3495df8bae1dSRodney W. Grimes } 3496df8bae1dSRodney W. Grimes 3497df8bae1dSRodney W. Grimes /* 34982a7be1b6SBrian Feldman * vmspace_map_entry_forked: 34992a7be1b6SBrian Feldman * Update the newly-forked vmspace each time a map entry is inherited 35002a7be1b6SBrian Feldman * or copied. The values for vm_dsize and vm_tsize are approximate 35012a7be1b6SBrian Feldman * (and mostly-obsolete ideas in the face of mmap(2) et al.) 35022a7be1b6SBrian Feldman */ 35032a7be1b6SBrian Feldman static void 35042a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2, 35052a7be1b6SBrian Feldman vm_map_entry_t entry) 35062a7be1b6SBrian Feldman { 35072a7be1b6SBrian Feldman vm_size_t entrysize; 35082a7be1b6SBrian Feldman vm_offset_t newend; 35092a7be1b6SBrian Feldman 351019bd0d9cSKonstantin Belousov if ((entry->eflags & MAP_ENTRY_GUARD) != 0) 351119bd0d9cSKonstantin Belousov return; 35122a7be1b6SBrian Feldman entrysize = entry->end - entry->start; 35132a7be1b6SBrian Feldman vm2->vm_map.size += entrysize; 35142a7be1b6SBrian Feldman if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) { 35152a7be1b6SBrian Feldman vm2->vm_ssize += btoc(entrysize); 35162a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_daddr && 35172a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) { 3518b351299cSAndrew Gallatin newend = MIN(entry->end, 35192a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)); 35202a7be1b6SBrian Feldman vm2->vm_dsize += btoc(newend - entry->start); 35212a7be1b6SBrian Feldman } else if (entry->start >= (vm_offset_t)vm1->vm_taddr && 35222a7be1b6SBrian Feldman entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) { 3523b351299cSAndrew Gallatin newend = MIN(entry->end, 35242a7be1b6SBrian Feldman (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)); 35252a7be1b6SBrian Feldman vm2->vm_tsize += btoc(newend - entry->start); 35262a7be1b6SBrian Feldman } 35272a7be1b6SBrian Feldman } 35282a7be1b6SBrian Feldman 35292a7be1b6SBrian Feldman /* 3530df8bae1dSRodney W. Grimes * vmspace_fork: 3531df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 3532df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 3533df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 3534df8bae1dSRodney W. Grimes * values on the regions in that map. 3535df8bae1dSRodney W. Grimes * 35362a7be1b6SBrian Feldman * XXX It might be worth coalescing the entries added to the new vmspace. 35372a7be1b6SBrian Feldman * 3538df8bae1dSRodney W. Grimes * The source map must not be locked. 3539df8bae1dSRodney W. Grimes */ 3540df8bae1dSRodney W. Grimes struct vmspace * 35413364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge) 3542df8bae1dSRodney W. Grimes { 3543c0877f10SJohn Dyson struct vmspace *vm2; 354479e53838SAlan Cox vm_map_t new_map, old_map; 354579e53838SAlan Cox vm_map_entry_t new_entry, old_entry; 3546de5f6a77SJohn Dyson vm_object_t object; 35471fac7d7fSKonstantin Belousov int locked; 354819bd0d9cSKonstantin Belousov vm_inherit_t inh; 3549df8bae1dSRodney W. Grimes 355079e53838SAlan Cox old_map = &vm1->vm_map; 355179e53838SAlan Cox /* Copy immutable fields of vm1 to vm2. */ 35526e00f3a3SKonstantin Belousov vm2 = vmspace_alloc(vm_map_min(old_map), vm_map_max(old_map), 35536e00f3a3SKonstantin Belousov pmap_pinit); 355489b57fcfSKonstantin Belousov if (vm2 == NULL) 355579e53838SAlan Cox return (NULL); 35562a7be1b6SBrian Feldman vm2->vm_taddr = vm1->vm_taddr; 35572a7be1b6SBrian Feldman vm2->vm_daddr = vm1->vm_daddr; 35582a7be1b6SBrian Feldman vm2->vm_maxsaddr = vm1->vm_maxsaddr; 355979e53838SAlan Cox vm_map_lock(old_map); 356079e53838SAlan Cox if (old_map->busy) 356179e53838SAlan Cox vm_map_wait_busy(old_map); 356279e53838SAlan Cox new_map = &vm2->vm_map; 35631fac7d7fSKonstantin Belousov locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */ 35641fac7d7fSKonstantin Belousov KASSERT(locked, ("vmspace_fork: lock failed")); 3565df8bae1dSRodney W. Grimes 3566fa50a355SKonstantin Belousov new_map->anon_loc = old_map->anon_loc; 3567df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 3568df8bae1dSRodney W. Grimes 3569df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 3570afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 3571df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 3572df8bae1dSRodney W. Grimes 357319bd0d9cSKonstantin Belousov inh = old_entry->inheritance; 357419bd0d9cSKonstantin Belousov if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 && 357519bd0d9cSKonstantin Belousov inh != VM_INHERIT_NONE) 357619bd0d9cSKonstantin Belousov inh = VM_INHERIT_COPY; 357719bd0d9cSKonstantin Belousov 357819bd0d9cSKonstantin Belousov switch (inh) { 3579df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 3580df8bae1dSRodney W. Grimes break; 3581df8bae1dSRodney W. Grimes 3582df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 3583df8bae1dSRodney W. Grimes /* 3584fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 3585fed9a903SJohn Dyson */ 3586fed9a903SJohn Dyson object = old_entry->object.vm_object; 3587fed9a903SJohn Dyson if (object == NULL) { 3588fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 3589c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 3590fed9a903SJohn Dyson old_entry->object.vm_object = object; 359115d2d313SAlan Cox old_entry->offset = 0; 3592ef694c1aSEdward Tomasz Napierala if (old_entry->cred != NULL) { 3593ef694c1aSEdward Tomasz Napierala object->cred = old_entry->cred; 35943364c323SKonstantin Belousov object->charge = old_entry->end - 35953364c323SKonstantin Belousov old_entry->start; 3596ef694c1aSEdward Tomasz Napierala old_entry->cred = NULL; 35973364c323SKonstantin Belousov } 35989a2f6362SAlan Cox } 35999a2f6362SAlan Cox 36009a2f6362SAlan Cox /* 36019a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 36029a2f6362SAlan Cox * to insure that a shadow object is created. 36039a2f6362SAlan Cox */ 36049a2f6362SAlan Cox vm_object_reference(object); 36059a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 36065069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 36075069bf57SJohn Dyson &old_entry->offset, 36080cc74f14SAlan Cox old_entry->end - old_entry->start); 36095069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 3610d30344bdSIan Dowse /* Transfer the second reference too. */ 3611d30344bdSIan Dowse vm_object_reference( 3612d30344bdSIan Dowse old_entry->object.vm_object); 36137fd10fb3SKonstantin Belousov 36147fd10fb3SKonstantin Belousov /* 36157fd10fb3SKonstantin Belousov * As in vm_map_simplify_entry(), the 3616b0994946SKonstantin Belousov * vnode lock will not be acquired in 36177fd10fb3SKonstantin Belousov * this call to vm_object_deallocate(). 36187fd10fb3SKonstantin Belousov */ 3619d30344bdSIan Dowse vm_object_deallocate(object); 36205069bf57SJohn Dyson object = old_entry->object.vm_object; 3621fed9a903SJohn Dyson } 362289f6b863SAttilio Rao VM_OBJECT_WLOCK(object); 3623069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 3624ef694c1aSEdward Tomasz Napierala if (old_entry->cred != NULL) { 3625ef694c1aSEdward Tomasz Napierala KASSERT(object->cred == NULL, ("vmspace_fork both cred")); 3626ef694c1aSEdward Tomasz Napierala object->cred = old_entry->cred; 36273364c323SKonstantin Belousov object->charge = old_entry->end - old_entry->start; 3628ef694c1aSEdward Tomasz Napierala old_entry->cred = NULL; 36293364c323SKonstantin Belousov } 3630b9781cf6SKonstantin Belousov 3631b9781cf6SKonstantin Belousov /* 3632b9781cf6SKonstantin Belousov * Assert the correct state of the vnode 3633b9781cf6SKonstantin Belousov * v_writecount while the object is locked, to 3634b9781cf6SKonstantin Belousov * not relock it later for the assertion 3635b9781cf6SKonstantin Belousov * correctness. 3636b9781cf6SKonstantin Belousov */ 3637b9781cf6SKonstantin Belousov if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT && 3638b9781cf6SKonstantin Belousov object->type == OBJT_VNODE) { 3639b9781cf6SKonstantin Belousov KASSERT(((struct vnode *)object->handle)-> 3640b9781cf6SKonstantin Belousov v_writecount > 0, 3641b9781cf6SKonstantin Belousov ("vmspace_fork: v_writecount %p", object)); 3642b9781cf6SKonstantin Belousov KASSERT(object->un_pager.vnp.writemappings > 0, 3643b9781cf6SKonstantin Belousov ("vmspace_fork: vnp.writecount %p", 3644b9781cf6SKonstantin Belousov object)); 3645b9781cf6SKonstantin Belousov } 364689f6b863SAttilio Rao VM_OBJECT_WUNLOCK(object); 3647fed9a903SJohn Dyson 3648fed9a903SJohn Dyson /* 3649ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 3650df8bae1dSRodney W. Grimes */ 3651df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 3652df8bae1dSRodney W. Grimes *new_entry = *old_entry; 36539f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 36549f6acfd1SKonstantin Belousov MAP_ENTRY_IN_TRANSITION); 36550acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL; 3656df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 365784110e7eSKonstantin Belousov if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) { 365884110e7eSKonstantin Belousov vnode_pager_update_writecount(object, 365984110e7eSKonstantin Belousov new_entry->start, new_entry->end); 366084110e7eSKonstantin Belousov } 3661df8bae1dSRodney W. Grimes 3662df8bae1dSRodney W. Grimes /* 36630d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 36640d94caffSDavid Greenman * inserting at the end of the new map. 3665df8bae1dSRodney W. Grimes */ 3666df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 3667df8bae1dSRodney W. Grimes new_entry); 36682a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 3669df8bae1dSRodney W. Grimes 3670df8bae1dSRodney W. Grimes /* 3671df8bae1dSRodney W. Grimes * Update the physical map 3672df8bae1dSRodney W. Grimes */ 3673df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 3674df8bae1dSRodney W. Grimes new_entry->start, 3675df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 3676df8bae1dSRodney W. Grimes old_entry->start); 3677df8bae1dSRodney W. Grimes break; 3678df8bae1dSRodney W. Grimes 3679df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 3680df8bae1dSRodney W. Grimes /* 3681df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 3682df8bae1dSRodney W. Grimes */ 3683df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 3684df8bae1dSRodney W. Grimes *new_entry = *old_entry; 368584110e7eSKonstantin Belousov /* 368684110e7eSKonstantin Belousov * Copied entry is COW over the old object. 368784110e7eSKonstantin Belousov */ 36889f6acfd1SKonstantin Belousov new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED | 368984110e7eSKonstantin Belousov MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT); 36900acea7dfSKonstantin Belousov new_entry->wiring_thread = NULL; 3691df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 3692df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 3693ef694c1aSEdward Tomasz Napierala new_entry->cred = NULL; 3694df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 3695df8bae1dSRodney W. Grimes new_entry); 36962a7be1b6SBrian Feldman vmspace_map_entry_forked(vm1, vm2, new_entry); 3697bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 36983364c323SKonstantin Belousov new_entry, fork_charge); 3699df8bae1dSRodney W. Grimes break; 370078d7964bSXin LI 370178d7964bSXin LI case VM_INHERIT_ZERO: 370278d7964bSXin LI /* 370378d7964bSXin LI * Create a new anonymous mapping entry modelled from 370478d7964bSXin LI * the old one. 370578d7964bSXin LI */ 370678d7964bSXin LI new_entry = vm_map_entry_create(new_map); 370778d7964bSXin LI memset(new_entry, 0, sizeof(*new_entry)); 370878d7964bSXin LI 370978d7964bSXin LI new_entry->start = old_entry->start; 371078d7964bSXin LI new_entry->end = old_entry->end; 371178d7964bSXin LI new_entry->eflags = old_entry->eflags & 371278d7964bSXin LI ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION | 371378d7964bSXin LI MAP_ENTRY_VN_WRITECNT); 371478d7964bSXin LI new_entry->protection = old_entry->protection; 371578d7964bSXin LI new_entry->max_protection = old_entry->max_protection; 371678d7964bSXin LI new_entry->inheritance = VM_INHERIT_ZERO; 371778d7964bSXin LI 371878d7964bSXin LI vm_map_entry_link(new_map, new_map->header.prev, 371978d7964bSXin LI new_entry); 372078d7964bSXin LI vmspace_map_entry_forked(vm1, vm2, new_entry); 372178d7964bSXin LI 372278d7964bSXin LI new_entry->cred = curthread->td_ucred; 372378d7964bSXin LI crhold(new_entry->cred); 372478d7964bSXin LI *fork_charge += (new_entry->end - new_entry->start); 372578d7964bSXin LI 372678d7964bSXin LI break; 3727df8bae1dSRodney W. Grimes } 3728df8bae1dSRodney W. Grimes old_entry = old_entry->next; 3729df8bae1dSRodney W. Grimes } 373084110e7eSKonstantin Belousov /* 373184110e7eSKonstantin Belousov * Use inlined vm_map_unlock() to postpone handling the deferred 373284110e7eSKonstantin Belousov * map entries, which cannot be done until both old_map and 373384110e7eSKonstantin Belousov * new_map locks are released. 373484110e7eSKonstantin Belousov */ 373584110e7eSKonstantin Belousov sx_xunlock(&old_map->lock); 373684110e7eSKonstantin Belousov sx_xunlock(&new_map->lock); 373784110e7eSKonstantin Belousov vm_map_process_deferred(); 3738df8bae1dSRodney W. Grimes 3739df8bae1dSRodney W. Grimes return (vm2); 3740df8bae1dSRodney W. Grimes } 3741df8bae1dSRodney W. Grimes 37428056df6eSAlan Cox /* 37438056df6eSAlan Cox * Create a process's stack for exec_new_vmspace(). This function is never 37448056df6eSAlan Cox * asked to wire the newly created stack. 37458056df6eSAlan Cox */ 374694f7e29aSAlan Cox int 374794f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 374894f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 374994f7e29aSAlan Cox { 37504648ba0aSKonstantin Belousov vm_size_t growsize, init_ssize; 37518056df6eSAlan Cox rlim_t vmemlim; 37524648ba0aSKonstantin Belousov int rv; 37534648ba0aSKonstantin Belousov 37548056df6eSAlan Cox MPASS((map->flags & MAP_WIREFUTURE) == 0); 37554648ba0aSKonstantin Belousov growsize = sgrowsiz; 37564648ba0aSKonstantin Belousov init_ssize = (max_ssize < growsize) ? max_ssize : growsize; 37574648ba0aSKonstantin Belousov vm_map_lock(map); 3758f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM); 37594648ba0aSKonstantin Belousov /* If we would blow our VMEM resource limit, no go */ 37604648ba0aSKonstantin Belousov if (map->size + init_ssize > vmemlim) { 37614648ba0aSKonstantin Belousov rv = KERN_NO_SPACE; 37624648ba0aSKonstantin Belousov goto out; 37634648ba0aSKonstantin Belousov } 3764e1f92cccSAlan Cox rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot, 37654648ba0aSKonstantin Belousov max, cow); 37664648ba0aSKonstantin Belousov out: 37674648ba0aSKonstantin Belousov vm_map_unlock(map); 37684648ba0aSKonstantin Belousov return (rv); 37694648ba0aSKonstantin Belousov } 37704648ba0aSKonstantin Belousov 377119f49ad3SKonstantin Belousov static int stack_guard_page = 1; 377219f49ad3SKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN, 377319f49ad3SKonstantin Belousov &stack_guard_page, 0, 377419f49ad3SKonstantin Belousov "Specifies the number of guard pages for a stack that grows"); 377519f49ad3SKonstantin Belousov 37764648ba0aSKonstantin Belousov static int 37774648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 37784648ba0aSKonstantin Belousov vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow) 37794648ba0aSKonstantin Belousov { 3780fd75d710SMarcel Moolenaar vm_map_entry_t new_entry, prev_entry; 378119bd0d9cSKonstantin Belousov vm_offset_t bot, gap_bot, gap_top, top; 378219f49ad3SKonstantin Belousov vm_size_t init_ssize, sgp; 3783fd75d710SMarcel Moolenaar int orient, rv; 378494f7e29aSAlan Cox 3785fd75d710SMarcel Moolenaar /* 3786fd75d710SMarcel Moolenaar * The stack orientation is piggybacked with the cow argument. 3787fd75d710SMarcel Moolenaar * Extract it into orient and mask the cow argument so that we 3788fd75d710SMarcel Moolenaar * don't pass it around further. 3789fd75d710SMarcel Moolenaar */ 3790fd75d710SMarcel Moolenaar orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP); 3791fd75d710SMarcel Moolenaar KASSERT(orient != 0, ("No stack grow direction")); 379219bd0d9cSKonstantin Belousov KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP), 379319bd0d9cSKonstantin Belousov ("bi-dir stack")); 3794fd75d710SMarcel Moolenaar 379577bc7900SKonstantin Belousov if (addrbos < vm_map_min(map) || 37969410cd7dSKonstantin Belousov addrbos + max_ssize > vm_map_max(map) || 37979410cd7dSKonstantin Belousov addrbos + max_ssize <= addrbos) 37989410cd7dSKonstantin Belousov return (KERN_INVALID_ADDRESS); 37999410cd7dSKonstantin Belousov sgp = (vm_size_t)stack_guard_page * PAGE_SIZE; 38009410cd7dSKonstantin Belousov if (sgp >= max_ssize) 38019410cd7dSKonstantin Belousov return (KERN_INVALID_ARGUMENT); 3802fd75d710SMarcel Moolenaar 380319f49ad3SKonstantin Belousov init_ssize = growsize; 380419f49ad3SKonstantin Belousov if (max_ssize < init_ssize + sgp) 380519f49ad3SKonstantin Belousov init_ssize = max_ssize - sgp; 380694f7e29aSAlan Cox 380794f7e29aSAlan Cox /* If addr is already mapped, no go */ 38084648ba0aSKonstantin Belousov if (vm_map_lookup_entry(map, addrbos, &prev_entry)) 380994f7e29aSAlan Cox return (KERN_NO_SPACE); 3810a69ac174SMatthew Dillon 3811fd75d710SMarcel Moolenaar /* 3812763df3ecSPedro F. Giffuni * If we can't accommodate max_ssize in the current mapping, no go. 381394f7e29aSAlan Cox */ 38141c5196c3SKonstantin Belousov if (prev_entry->next->start < addrbos + max_ssize) 381594f7e29aSAlan Cox return (KERN_NO_SPACE); 381694f7e29aSAlan Cox 3817fd75d710SMarcel Moolenaar /* 3818fd75d710SMarcel Moolenaar * We initially map a stack of only init_ssize. We will grow as 3819fd75d710SMarcel Moolenaar * needed later. Depending on the orientation of the stack (i.e. 3820fd75d710SMarcel Moolenaar * the grow direction) we either map at the top of the range, the 3821fd75d710SMarcel Moolenaar * bottom of the range or in the middle. 382294f7e29aSAlan Cox * 3823fd75d710SMarcel Moolenaar * Note: we would normally expect prot and max to be VM_PROT_ALL, 3824fd75d710SMarcel Moolenaar * and cow to be 0. Possibly we should eliminate these as input 3825fd75d710SMarcel Moolenaar * parameters, and just pass these values here in the insert call. 382694f7e29aSAlan Cox */ 382719bd0d9cSKonstantin Belousov if (orient == MAP_STACK_GROWS_DOWN) { 3828fd75d710SMarcel Moolenaar bot = addrbos + max_ssize - init_ssize; 3829fd75d710SMarcel Moolenaar top = bot + init_ssize; 383019bd0d9cSKonstantin Belousov gap_bot = addrbos; 383119bd0d9cSKonstantin Belousov gap_top = bot; 383219bd0d9cSKonstantin Belousov } else /* if (orient == MAP_STACK_GROWS_UP) */ { 383319bd0d9cSKonstantin Belousov bot = addrbos; 383419bd0d9cSKonstantin Belousov top = bot + init_ssize; 383519bd0d9cSKonstantin Belousov gap_bot = top; 383619bd0d9cSKonstantin Belousov gap_top = addrbos + max_ssize; 383719bd0d9cSKonstantin Belousov } 3838fd75d710SMarcel Moolenaar rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow); 383919bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) 384019bd0d9cSKonstantin Belousov return (rv); 3841fd75d710SMarcel Moolenaar new_entry = prev_entry->next; 384219bd0d9cSKonstantin Belousov KASSERT(new_entry->end == top || new_entry->start == bot, 384319bd0d9cSKonstantin Belousov ("Bad entry start/end for new stack entry")); 3844712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 || 3845712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0, 3846712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_DOWN")); 3847712efe66SAlan Cox KASSERT((orient & MAP_STACK_GROWS_UP) == 0 || 3848712efe66SAlan Cox (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0, 3849712efe66SAlan Cox ("new entry lacks MAP_ENTRY_GROWS_UP")); 385019bd0d9cSKonstantin Belousov rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE, 385119bd0d9cSKonstantin Belousov VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ? 385219bd0d9cSKonstantin Belousov MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP)); 385319bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) 385419bd0d9cSKonstantin Belousov (void)vm_map_delete(map, bot, top); 385594f7e29aSAlan Cox return (rv); 385694f7e29aSAlan Cox } 385794f7e29aSAlan Cox 385819bd0d9cSKonstantin Belousov /* 385919bd0d9cSKonstantin Belousov * Attempts to grow a vm stack entry. Returns KERN_SUCCESS if we 386019bd0d9cSKonstantin Belousov * successfully grow the stack. 386194f7e29aSAlan Cox */ 386219bd0d9cSKonstantin Belousov static int 386319bd0d9cSKonstantin Belousov vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry) 386494f7e29aSAlan Cox { 386519bd0d9cSKonstantin Belousov vm_map_entry_t stack_entry; 386619bd0d9cSKonstantin Belousov struct proc *p; 386719bd0d9cSKonstantin Belousov struct vmspace *vm; 386819bd0d9cSKonstantin Belousov struct ucred *cred; 386919bd0d9cSKonstantin Belousov vm_offset_t gap_end, gap_start, grow_start; 3870201f03b8SAlan Cox size_t grow_amount, guard, max_grow; 38717e19eda4SAndrey Zonov rlim_t lmemlim, stacklim, vmemlim; 387219bd0d9cSKonstantin Belousov int rv, rv1; 387319bd0d9cSKonstantin Belousov bool gap_deleted, grow_down, is_procstack; 38741ba5ad42SEdward Tomasz Napierala #ifdef notyet 38751ba5ad42SEdward Tomasz Napierala uint64_t limit; 38761ba5ad42SEdward Tomasz Napierala #endif 3877afcc55f3SEdward Tomasz Napierala #ifdef RACCT 38781ba5ad42SEdward Tomasz Napierala int error; 3879afcc55f3SEdward Tomasz Napierala #endif 388023955314SAlfred Perlstein 388119bd0d9cSKonstantin Belousov p = curproc; 388219bd0d9cSKonstantin Belousov vm = p->p_vmspace; 3883eb5ea878SKonstantin Belousov 3884eb5ea878SKonstantin Belousov /* 3885eb5ea878SKonstantin Belousov * Disallow stack growth when the access is performed by a 3886eb5ea878SKonstantin Belousov * debugger or AIO daemon. The reason is that the wrong 3887eb5ea878SKonstantin Belousov * resource limits are applied. 3888eb5ea878SKonstantin Belousov */ 3889eb5ea878SKonstantin Belousov if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL) 3890f758aaddSKonstantin Belousov return (KERN_FAILURE); 3891eb5ea878SKonstantin Belousov 389219bd0d9cSKonstantin Belousov MPASS(!map->system_map); 389319bd0d9cSKonstantin Belousov 3894201f03b8SAlan Cox guard = stack_guard_page * PAGE_SIZE; 3895f6f6d240SMateusz Guzik lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK); 3896f6f6d240SMateusz Guzik stacklim = lim_cur(curthread, RLIMIT_STACK); 3897f6f6d240SMateusz Guzik vmemlim = lim_cur(curthread, RLIMIT_VMEM); 389819bd0d9cSKonstantin Belousov retry: 389919bd0d9cSKonstantin Belousov /* If addr is not in a hole for a stack grow area, no need to grow. */ 390019bd0d9cSKonstantin Belousov if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry)) 390119bd0d9cSKonstantin Belousov return (KERN_FAILURE); 390219bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0) 39030cddd8f0SMatthew Dillon return (KERN_SUCCESS); 390419bd0d9cSKonstantin Belousov if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) { 390519bd0d9cSKonstantin Belousov stack_entry = gap_entry->next; 390619bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 || 390719bd0d9cSKonstantin Belousov stack_entry->start != gap_entry->end) 390819bd0d9cSKonstantin Belousov return (KERN_FAILURE); 390919bd0d9cSKonstantin Belousov grow_amount = round_page(stack_entry->start - addr); 391019bd0d9cSKonstantin Belousov grow_down = true; 391119bd0d9cSKonstantin Belousov } else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) { 391219bd0d9cSKonstantin Belousov stack_entry = gap_entry->prev; 391319bd0d9cSKonstantin Belousov if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 || 391419bd0d9cSKonstantin Belousov stack_entry->end != gap_entry->start) 391519bd0d9cSKonstantin Belousov return (KERN_FAILURE); 391619bd0d9cSKonstantin Belousov grow_amount = round_page(addr + 1 - stack_entry->end); 391719bd0d9cSKonstantin Belousov grow_down = false; 3918b21a0008SMarcel Moolenaar } else { 391919bd0d9cSKonstantin Belousov return (KERN_FAILURE); 3920b21a0008SMarcel Moolenaar } 3921201f03b8SAlan Cox max_grow = gap_entry->end - gap_entry->start; 3922201f03b8SAlan Cox if (guard > max_grow) 3923201f03b8SAlan Cox return (KERN_NO_SPACE); 3924201f03b8SAlan Cox max_grow -= guard; 392519bd0d9cSKonstantin Belousov if (grow_amount > max_grow) 39260cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 392794f7e29aSAlan Cox 3928b21a0008SMarcel Moolenaar /* 3929b21a0008SMarcel Moolenaar * If this is the main process stack, see if we're over the stack 3930b21a0008SMarcel Moolenaar * limit. 393194f7e29aSAlan Cox */ 393219bd0d9cSKonstantin Belousov is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr && 393319bd0d9cSKonstantin Belousov addr < (vm_offset_t)p->p_sysent->sv_usrstack; 393419bd0d9cSKonstantin Belousov if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) 39350cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 393619bd0d9cSKonstantin Belousov 3937afcc55f3SEdward Tomasz Napierala #ifdef RACCT 39384b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 39391ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 39404b5c9cf6SEdward Tomasz Napierala if (is_procstack && racct_set(p, RACCT_STACK, 39414b5c9cf6SEdward Tomasz Napierala ctob(vm->vm_ssize) + grow_amount)) { 39421ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 39431ba5ad42SEdward Tomasz Napierala return (KERN_NO_SPACE); 39441ba5ad42SEdward Tomasz Napierala } 39451ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 39464b5c9cf6SEdward Tomasz Napierala } 3947afcc55f3SEdward Tomasz Napierala #endif 394894f7e29aSAlan Cox 394919bd0d9cSKonstantin Belousov grow_amount = roundup(grow_amount, sgrowsiz); 395019bd0d9cSKonstantin Belousov if (grow_amount > max_grow) 395119bd0d9cSKonstantin Belousov grow_amount = max_grow; 395291d5354aSJohn Baldwin if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) { 3953e4826248SAlan Cox grow_amount = trunc_page((vm_size_t)stacklim) - 3954e4826248SAlan Cox ctob(vm->vm_ssize); 395594f7e29aSAlan Cox } 395619bd0d9cSKonstantin Belousov 39571ba5ad42SEdward Tomasz Napierala #ifdef notyet 39581ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 39591ba5ad42SEdward Tomasz Napierala limit = racct_get_available(p, RACCT_STACK); 39601ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 39611ba5ad42SEdward Tomasz Napierala if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit)) 39621ba5ad42SEdward Tomasz Napierala grow_amount = limit - ctob(vm->vm_ssize); 39631ba5ad42SEdward Tomasz Napierala #endif 396419bd0d9cSKonstantin Belousov 396519bd0d9cSKonstantin Belousov if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) { 39663ac7d297SAndrey Zonov if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) { 39677e19eda4SAndrey Zonov rv = KERN_NO_SPACE; 39687e19eda4SAndrey Zonov goto out; 39697e19eda4SAndrey Zonov } 39707e19eda4SAndrey Zonov #ifdef RACCT 39714b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 39727e19eda4SAndrey Zonov PROC_LOCK(p); 39737e19eda4SAndrey Zonov if (racct_set(p, RACCT_MEMLOCK, 39743ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap)) + grow_amount)) { 39757e19eda4SAndrey Zonov PROC_UNLOCK(p); 39767e19eda4SAndrey Zonov rv = KERN_NO_SPACE; 39777e19eda4SAndrey Zonov goto out; 39787e19eda4SAndrey Zonov } 39797e19eda4SAndrey Zonov PROC_UNLOCK(p); 39804b5c9cf6SEdward Tomasz Napierala } 39817e19eda4SAndrey Zonov #endif 39827e19eda4SAndrey Zonov } 398319bd0d9cSKonstantin Belousov 3984a69ac174SMatthew Dillon /* If we would blow our VMEM resource limit, no go */ 398591d5354aSJohn Baldwin if (map->size + grow_amount > vmemlim) { 39861ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE; 39871ba5ad42SEdward Tomasz Napierala goto out; 3988a69ac174SMatthew Dillon } 3989afcc55f3SEdward Tomasz Napierala #ifdef RACCT 39904b5c9cf6SEdward Tomasz Napierala if (racct_enable) { 39911ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 39921ba5ad42SEdward Tomasz Napierala if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) { 39931ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 39941ba5ad42SEdward Tomasz Napierala rv = KERN_NO_SPACE; 39951ba5ad42SEdward Tomasz Napierala goto out; 39961ba5ad42SEdward Tomasz Napierala } 39971ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 39984b5c9cf6SEdward Tomasz Napierala } 3999afcc55f3SEdward Tomasz Napierala #endif 4000a69ac174SMatthew Dillon 400119bd0d9cSKonstantin Belousov if (vm_map_lock_upgrade(map)) { 400219bd0d9cSKonstantin Belousov gap_entry = NULL; 400319bd0d9cSKonstantin Belousov vm_map_lock_read(map); 400419bd0d9cSKonstantin Belousov goto retry; 400594f7e29aSAlan Cox } 400694f7e29aSAlan Cox 400719bd0d9cSKonstantin Belousov if (grow_down) { 400819bd0d9cSKonstantin Belousov grow_start = gap_entry->end - grow_amount; 400919bd0d9cSKonstantin Belousov if (gap_entry->start + grow_amount == gap_entry->end) { 401019bd0d9cSKonstantin Belousov gap_start = gap_entry->start; 401119bd0d9cSKonstantin Belousov gap_end = gap_entry->end; 401219bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry); 401319bd0d9cSKonstantin Belousov gap_deleted = true; 401419bd0d9cSKonstantin Belousov } else { 401519bd0d9cSKonstantin Belousov MPASS(gap_entry->start < gap_entry->end - grow_amount); 401619bd0d9cSKonstantin Belousov gap_entry->end -= grow_amount; 401719bd0d9cSKonstantin Belousov vm_map_entry_resize_free(map, gap_entry); 401819bd0d9cSKonstantin Belousov gap_deleted = false; 401919bd0d9cSKonstantin Belousov } 402019bd0d9cSKonstantin Belousov rv = vm_map_insert(map, NULL, 0, grow_start, 402119bd0d9cSKonstantin Belousov grow_start + grow_amount, 402219bd0d9cSKonstantin Belousov stack_entry->protection, stack_entry->max_protection, 4023712efe66SAlan Cox MAP_STACK_GROWS_DOWN); 402419bd0d9cSKonstantin Belousov if (rv != KERN_SUCCESS) { 402519bd0d9cSKonstantin Belousov if (gap_deleted) { 402619bd0d9cSKonstantin Belousov rv1 = vm_map_insert(map, NULL, 0, gap_start, 402719bd0d9cSKonstantin Belousov gap_end, VM_PROT_NONE, VM_PROT_NONE, 402819bd0d9cSKonstantin Belousov MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN); 402919bd0d9cSKonstantin Belousov MPASS(rv1 == KERN_SUCCESS); 403019bd0d9cSKonstantin Belousov } else { 403119bd0d9cSKonstantin Belousov gap_entry->end += grow_amount; 403219bd0d9cSKonstantin Belousov vm_map_entry_resize_free(map, gap_entry); 403319bd0d9cSKonstantin Belousov } 403494f7e29aSAlan Cox } 4035b21a0008SMarcel Moolenaar } else { 403619bd0d9cSKonstantin Belousov grow_start = stack_entry->end; 4037ef694c1aSEdward Tomasz Napierala cred = stack_entry->cred; 4038ef694c1aSEdward Tomasz Napierala if (cred == NULL && stack_entry->object.vm_object != NULL) 4039ef694c1aSEdward Tomasz Napierala cred = stack_entry->object.vm_object->cred; 4040ef694c1aSEdward Tomasz Napierala if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred)) 40413364c323SKonstantin Belousov rv = KERN_NO_SPACE; 4042b21a0008SMarcel Moolenaar /* Grow the underlying object if applicable. */ 40433364c323SKonstantin Belousov else if (stack_entry->object.vm_object == NULL || 4044b21a0008SMarcel Moolenaar vm_object_coalesce(stack_entry->object.vm_object, 404557a21abaSAlan Cox stack_entry->offset, 4046b21a0008SMarcel Moolenaar (vm_size_t)(stack_entry->end - stack_entry->start), 4047ef694c1aSEdward Tomasz Napierala (vm_size_t)grow_amount, cred != NULL)) { 404819bd0d9cSKonstantin Belousov if (gap_entry->start + grow_amount == gap_entry->end) 404919bd0d9cSKonstantin Belousov vm_map_entry_delete(map, gap_entry); 405019bd0d9cSKonstantin Belousov else 405119bd0d9cSKonstantin Belousov gap_entry->start += grow_amount; 405219bd0d9cSKonstantin Belousov stack_entry->end += grow_amount; 405319bd0d9cSKonstantin Belousov map->size += grow_amount; 40540164e057SAlan Cox vm_map_entry_resize_free(map, stack_entry); 4055b21a0008SMarcel Moolenaar rv = KERN_SUCCESS; 4056b21a0008SMarcel Moolenaar } else 4057b21a0008SMarcel Moolenaar rv = KERN_FAILURE; 4058b21a0008SMarcel Moolenaar } 4059b21a0008SMarcel Moolenaar if (rv == KERN_SUCCESS && is_procstack) 4060b21a0008SMarcel Moolenaar vm->vm_ssize += btoc(grow_amount); 4061b21a0008SMarcel Moolenaar 4062abd498aaSBruce M Simpson /* 4063abd498aaSBruce M Simpson * Heed the MAP_WIREFUTURE flag if it was set for this process. 4064abd498aaSBruce M Simpson */ 406519bd0d9cSKonstantin Belousov if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) { 406619bd0d9cSKonstantin Belousov vm_map_unlock(map); 406719bd0d9cSKonstantin Belousov vm_map_wire(map, grow_start, grow_start + grow_amount, 4068212e02c8SKonstantin Belousov VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES); 406919bd0d9cSKonstantin Belousov vm_map_lock_read(map); 407019bd0d9cSKonstantin Belousov } else 407119bd0d9cSKonstantin Belousov vm_map_lock_downgrade(map); 4072abd498aaSBruce M Simpson 40731ba5ad42SEdward Tomasz Napierala out: 4074afcc55f3SEdward Tomasz Napierala #ifdef RACCT 40754b5c9cf6SEdward Tomasz Napierala if (racct_enable && rv != KERN_SUCCESS) { 40761ba5ad42SEdward Tomasz Napierala PROC_LOCK(p); 40771ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_VMEM, map->size); 40781ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_VMEM failed")); 40797e19eda4SAndrey Zonov if (!old_mlock) { 40807e19eda4SAndrey Zonov error = racct_set(p, RACCT_MEMLOCK, 40813ac7d297SAndrey Zonov ptoa(pmap_wired_count(map->pmap))); 40827e19eda4SAndrey Zonov KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed")); 40837e19eda4SAndrey Zonov } 40841ba5ad42SEdward Tomasz Napierala error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize)); 40851ba5ad42SEdward Tomasz Napierala KASSERT(error == 0, ("decreasing RACCT_STACK failed")); 40861ba5ad42SEdward Tomasz Napierala PROC_UNLOCK(p); 40871ba5ad42SEdward Tomasz Napierala } 4088afcc55f3SEdward Tomasz Napierala #endif 40891ba5ad42SEdward Tomasz Napierala 40900cddd8f0SMatthew Dillon return (rv); 409194f7e29aSAlan Cox } 409294f7e29aSAlan Cox 4093df8bae1dSRodney W. Grimes /* 40945856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 40955856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 40965856e12eSJohn Dyson */ 409789b57fcfSKonstantin Belousov int 40983ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser) 40991b40f8c0SMatthew Dillon { 41005856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 41015856e12eSJohn Dyson struct vmspace *newvmspace; 41025856e12eSJohn Dyson 41037032434eSKonstantin Belousov KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0, 41047032434eSKonstantin Belousov ("vmspace_exec recursed")); 41056e00f3a3SKonstantin Belousov newvmspace = vmspace_alloc(minuser, maxuser, pmap_pinit); 410689b57fcfSKonstantin Belousov if (newvmspace == NULL) 410789b57fcfSKonstantin Belousov return (ENOMEM); 410851ab6c28SAlan Cox newvmspace->vm_swrss = oldvmspace->vm_swrss; 41095856e12eSJohn Dyson /* 41105856e12eSJohn Dyson * This code is written like this for prototype purposes. The 41115856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 41125856e12eSJohn Dyson * other process's that are still using the vmspace to finally 41135856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 41145856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 41155856e12eSJohn Dyson */ 411657051fdcSTor Egge PROC_VMSPACE_LOCK(p); 41175856e12eSJohn Dyson p->p_vmspace = newvmspace; 411857051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 41196617724cSJeff Roberson if (p == curthread->td_proc) 4120b40ce416SJulian Elischer pmap_activate(curthread); 41217032434eSKonstantin Belousov curthread->td_pflags |= TDP_EXECVMSPC; 412289b57fcfSKonstantin Belousov return (0); 41235856e12eSJohn Dyson } 41245856e12eSJohn Dyson 41255856e12eSJohn Dyson /* 41265856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 41275856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 41285856e12eSJohn Dyson */ 412989b57fcfSKonstantin Belousov int 41301b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p) 41311b40f8c0SMatthew Dillon { 41325856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 41335856e12eSJohn Dyson struct vmspace *newvmspace; 41343364c323SKonstantin Belousov vm_ooffset_t fork_charge; 41355856e12eSJohn Dyson 41365856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 413789b57fcfSKonstantin Belousov return (0); 41383364c323SKonstantin Belousov fork_charge = 0; 41393364c323SKonstantin Belousov newvmspace = vmspace_fork(oldvmspace, &fork_charge); 414089b57fcfSKonstantin Belousov if (newvmspace == NULL) 414189b57fcfSKonstantin Belousov return (ENOMEM); 4142ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) { 41433364c323SKonstantin Belousov vmspace_free(newvmspace); 41443364c323SKonstantin Belousov return (ENOMEM); 41453364c323SKonstantin Belousov } 414657051fdcSTor Egge PROC_VMSPACE_LOCK(p); 41475856e12eSJohn Dyson p->p_vmspace = newvmspace; 414857051fdcSTor Egge PROC_VMSPACE_UNLOCK(p); 41496617724cSJeff Roberson if (p == curthread->td_proc) 4150b40ce416SJulian Elischer pmap_activate(curthread); 4151b56ef1c1SJohn Baldwin vmspace_free(oldvmspace); 415289b57fcfSKonstantin Belousov return (0); 41535856e12eSJohn Dyson } 41545856e12eSJohn Dyson 41555856e12eSJohn Dyson /* 4156df8bae1dSRodney W. Grimes * vm_map_lookup: 4157df8bae1dSRodney W. Grimes * 4158df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 4159df8bae1dSRodney W. Grimes * protection for a given virtual address in the 4160df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 4161df8bae1dSRodney W. Grimes * type specified. 4162df8bae1dSRodney W. Grimes * 4163df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 4164df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 4165df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 4166df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 4167df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 4168df8bae1dSRodney W. Grimes * 4169df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 4170df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 4171df8bae1dSRodney W. Grimes * 4172df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 4173df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 4174df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 4175df8bae1dSRodney W. Grimes * remain the same. 4176df8bae1dSRodney W. Grimes */ 4177df8bae1dSRodney W. Grimes int 4178b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 4179b9dcd593SBruce Evans vm_offset_t vaddr, 418047221757SJohn Dyson vm_prot_t fault_typea, 4181b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 4182b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 4183b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 4184b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 41852d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 4186df8bae1dSRodney W. Grimes { 4187c0877f10SJohn Dyson vm_map_entry_t entry; 4188c0877f10SJohn Dyson vm_map_t map = *var_map; 4189c0877f10SJohn Dyson vm_prot_t prot; 419047221757SJohn Dyson vm_prot_t fault_type = fault_typea; 41913364c323SKonstantin Belousov vm_object_t eobject; 41920cc74f14SAlan Cox vm_size_t size; 4193ef694c1aSEdward Tomasz Napierala struct ucred *cred; 4194df8bae1dSRodney W. Grimes 419519bd0d9cSKonstantin Belousov RetryLookup: 4196df8bae1dSRodney W. Grimes 4197df8bae1dSRodney W. Grimes vm_map_lock_read(map); 4198df8bae1dSRodney W. Grimes 419919bd0d9cSKonstantin Belousov RetryLookupLocked: 4200df8bae1dSRodney W. Grimes /* 42014c3ef59eSAlan Cox * Lookup the faulting address. 4202df8bae1dSRodney W. Grimes */ 4203095104acSAlan Cox if (!vm_map_lookup_entry(map, vaddr, out_entry)) { 4204095104acSAlan Cox vm_map_unlock_read(map); 4205095104acSAlan Cox return (KERN_INVALID_ADDRESS); 4206095104acSAlan Cox } 4207df8bae1dSRodney W. Grimes 42084e94f402SAlan Cox entry = *out_entry; 4209b7b2aac2SJohn Dyson 4210df8bae1dSRodney W. Grimes /* 4211df8bae1dSRodney W. Grimes * Handle submaps. 4212df8bae1dSRodney W. Grimes */ 4213afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 4214df8bae1dSRodney W. Grimes vm_map_t old_map = map; 4215df8bae1dSRodney W. Grimes 4216df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 4217df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 4218df8bae1dSRodney W. Grimes goto RetryLookup; 4219df8bae1dSRodney W. Grimes } 4220a04c970aSJohn Dyson 4221df8bae1dSRodney W. Grimes /* 42220d94caffSDavid Greenman * Check whether this task is allowed to have this page. 4223df8bae1dSRodney W. Grimes */ 4224df8bae1dSRodney W. Grimes prot = entry->protection; 422519bd0d9cSKonstantin Belousov if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) { 422619bd0d9cSKonstantin Belousov fault_typea &= ~VM_PROT_FAULT_LOOKUP; 422719bd0d9cSKonstantin Belousov if (prot == VM_PROT_NONE && map != kernel_map && 422819bd0d9cSKonstantin Belousov (entry->eflags & MAP_ENTRY_GUARD) != 0 && 422919bd0d9cSKonstantin Belousov (entry->eflags & (MAP_ENTRY_STACK_GAP_DN | 423019bd0d9cSKonstantin Belousov MAP_ENTRY_STACK_GAP_UP)) != 0 && 423119bd0d9cSKonstantin Belousov vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS) 423219bd0d9cSKonstantin Belousov goto RetryLookupLocked; 423319bd0d9cSKonstantin Belousov } 423419bd0d9cSKonstantin Belousov fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 42352db65ab4SAlan Cox if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) { 4236095104acSAlan Cox vm_map_unlock_read(map); 4237095104acSAlan Cox return (KERN_PROTECTION_FAILURE); 423847221757SJohn Dyson } 4239b8db9776SKonstantin Belousov KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags & 4240b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) != 4241b8db9776SKonstantin Belousov (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY), 4242b8db9776SKonstantin Belousov ("entry %p flags %x", entry, entry->eflags)); 42435b3e0257SDag-Erling Smørgrav if ((fault_typea & VM_PROT_COPY) != 0 && 42445b3e0257SDag-Erling Smørgrav (entry->max_protection & VM_PROT_WRITE) == 0 && 42455b3e0257SDag-Erling Smørgrav (entry->eflags & MAP_ENTRY_COW) == 0) { 42465b3e0257SDag-Erling Smørgrav vm_map_unlock_read(map); 42475b3e0257SDag-Erling Smørgrav return (KERN_PROTECTION_FAILURE); 42485b3e0257SDag-Erling Smørgrav } 4249df8bae1dSRodney W. Grimes 4250df8bae1dSRodney W. Grimes /* 42510d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 42520d94caffSDavid Greenman * accesses. 4253df8bae1dSRodney W. Grimes */ 425405f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 425505f0fdd2SPoul-Henning Kamp if (*wired) 4256a6d42a0dSAlan Cox fault_type = entry->protection; 42573364c323SKonstantin Belousov size = entry->end - entry->start; 4258df8bae1dSRodney W. Grimes /* 4259df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 4260df8bae1dSRodney W. Grimes */ 4261afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 4262df8bae1dSRodney W. Grimes /* 42630d94caffSDavid Greenman * If we want to write the page, we may as well handle that 4264ad5fca3bSAlan Cox * now since we've got the map locked. 4265df8bae1dSRodney W. Grimes * 42660d94caffSDavid Greenman * If we don't need to write the page, we just demote the 42670d94caffSDavid Greenman * permissions allowed. 4268df8bae1dSRodney W. Grimes */ 4269a6d42a0dSAlan Cox if ((fault_type & VM_PROT_WRITE) != 0 || 4270a6d42a0dSAlan Cox (fault_typea & VM_PROT_COPY) != 0) { 4271df8bae1dSRodney W. Grimes /* 42720d94caffSDavid Greenman * Make a new object, and place it in the object 42730d94caffSDavid Greenman * chain. Note that no new references have appeared 4274ad5fca3bSAlan Cox * -- one just moved from the map to the new 42750d94caffSDavid Greenman * object. 4276df8bae1dSRodney W. Grimes */ 427725adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 4278df8bae1dSRodney W. Grimes goto RetryLookup; 42799917e010SAlan Cox 4280ef694c1aSEdward Tomasz Napierala if (entry->cred == NULL) { 42813364c323SKonstantin Belousov /* 42823364c323SKonstantin Belousov * The debugger owner is charged for 42833364c323SKonstantin Belousov * the memory. 42843364c323SKonstantin Belousov */ 4285ef694c1aSEdward Tomasz Napierala cred = curthread->td_ucred; 4286ef694c1aSEdward Tomasz Napierala crhold(cred); 4287ef694c1aSEdward Tomasz Napierala if (!swap_reserve_by_cred(size, cred)) { 4288ef694c1aSEdward Tomasz Napierala crfree(cred); 42893364c323SKonstantin Belousov vm_map_unlock(map); 42903364c323SKonstantin Belousov return (KERN_RESOURCE_SHORTAGE); 42913364c323SKonstantin Belousov } 4292ef694c1aSEdward Tomasz Napierala entry->cred = cred; 42933364c323SKonstantin Belousov } 42940cc74f14SAlan Cox vm_object_shadow(&entry->object.vm_object, 42950cc74f14SAlan Cox &entry->offset, size); 4296afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 42973364c323SKonstantin Belousov eobject = entry->object.vm_object; 4298ef694c1aSEdward Tomasz Napierala if (eobject->cred != NULL) { 42993364c323SKonstantin Belousov /* 43003364c323SKonstantin Belousov * The object was not shadowed. 43013364c323SKonstantin Belousov */ 4302ef694c1aSEdward Tomasz Napierala swap_release_by_cred(size, entry->cred); 4303ef694c1aSEdward Tomasz Napierala crfree(entry->cred); 4304ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 4305ef694c1aSEdward Tomasz Napierala } else if (entry->cred != NULL) { 430689f6b863SAttilio Rao VM_OBJECT_WLOCK(eobject); 4307ef694c1aSEdward Tomasz Napierala eobject->cred = entry->cred; 43083364c323SKonstantin Belousov eobject->charge = size; 430989f6b863SAttilio Rao VM_OBJECT_WUNLOCK(eobject); 4310ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 43113364c323SKonstantin Belousov } 43129917e010SAlan Cox 43139b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 43140d94caffSDavid Greenman } else { 4315df8bae1dSRodney W. Grimes /* 43160d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 43170d94caffSDavid Greenman * don't allow writes. 4318df8bae1dSRodney W. Grimes */ 43192d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 4320df8bae1dSRodney W. Grimes } 4321df8bae1dSRodney W. Grimes } 43222d8acc0fSJohn Dyson 4323df8bae1dSRodney W. Grimes /* 4324df8bae1dSRodney W. Grimes * Create an object if necessary. 4325df8bae1dSRodney W. Grimes */ 43264e71e795SMatthew Dillon if (entry->object.vm_object == NULL && 43274e71e795SMatthew Dillon !map->system_map) { 432825adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 4329df8bae1dSRodney W. Grimes goto RetryLookup; 433024a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 43313364c323SKonstantin Belousov atop(size)); 4332df8bae1dSRodney W. Grimes entry->offset = 0; 4333ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) { 433489f6b863SAttilio Rao VM_OBJECT_WLOCK(entry->object.vm_object); 4335ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred = entry->cred; 43363364c323SKonstantin Belousov entry->object.vm_object->charge = size; 433789f6b863SAttilio Rao VM_OBJECT_WUNLOCK(entry->object.vm_object); 4338ef694c1aSEdward Tomasz Napierala entry->cred = NULL; 43393364c323SKonstantin Belousov } 43409b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 4341df8bae1dSRodney W. Grimes } 4342b5b40fa6SJohn Dyson 4343df8bae1dSRodney W. Grimes /* 43440d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 43450d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 4346df8bae1dSRodney W. Grimes */ 434710d9120cSKonstantin Belousov *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 4348df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 4349df8bae1dSRodney W. Grimes 4350df8bae1dSRodney W. Grimes *out_prot = prot; 4351df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 4352df8bae1dSRodney W. Grimes } 4353df8bae1dSRodney W. Grimes 4354df8bae1dSRodney W. Grimes /* 435519dc5607STor Egge * vm_map_lookup_locked: 435619dc5607STor Egge * 435719dc5607STor Egge * Lookup the faulting address. A version of vm_map_lookup that returns 435819dc5607STor Egge * KERN_FAILURE instead of blocking on map lock or memory allocation. 435919dc5607STor Egge */ 436019dc5607STor Egge int 436119dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map, /* IN/OUT */ 436219dc5607STor Egge vm_offset_t vaddr, 436319dc5607STor Egge vm_prot_t fault_typea, 436419dc5607STor Egge vm_map_entry_t *out_entry, /* OUT */ 436519dc5607STor Egge vm_object_t *object, /* OUT */ 436619dc5607STor Egge vm_pindex_t *pindex, /* OUT */ 436719dc5607STor Egge vm_prot_t *out_prot, /* OUT */ 436819dc5607STor Egge boolean_t *wired) /* OUT */ 436919dc5607STor Egge { 437019dc5607STor Egge vm_map_entry_t entry; 437119dc5607STor Egge vm_map_t map = *var_map; 437219dc5607STor Egge vm_prot_t prot; 437319dc5607STor Egge vm_prot_t fault_type = fault_typea; 437419dc5607STor Egge 437519dc5607STor Egge /* 43764c3ef59eSAlan Cox * Lookup the faulting address. 437719dc5607STor Egge */ 437819dc5607STor Egge if (!vm_map_lookup_entry(map, vaddr, out_entry)) 437919dc5607STor Egge return (KERN_INVALID_ADDRESS); 438019dc5607STor Egge 438119dc5607STor Egge entry = *out_entry; 438219dc5607STor Egge 438319dc5607STor Egge /* 438419dc5607STor Egge * Fail if the entry refers to a submap. 438519dc5607STor Egge */ 438619dc5607STor Egge if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 438719dc5607STor Egge return (KERN_FAILURE); 438819dc5607STor Egge 438919dc5607STor Egge /* 439019dc5607STor Egge * Check whether this task is allowed to have this page. 439119dc5607STor Egge */ 439219dc5607STor Egge prot = entry->protection; 439319dc5607STor Egge fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE; 439419dc5607STor Egge if ((fault_type & prot) != fault_type) 439519dc5607STor Egge return (KERN_PROTECTION_FAILURE); 439619dc5607STor Egge 439719dc5607STor Egge /* 439819dc5607STor Egge * If this page is not pageable, we have to get it for all possible 439919dc5607STor Egge * accesses. 440019dc5607STor Egge */ 440119dc5607STor Egge *wired = (entry->wired_count != 0); 440219dc5607STor Egge if (*wired) 4403a6d42a0dSAlan Cox fault_type = entry->protection; 440419dc5607STor Egge 440519dc5607STor Egge if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 440619dc5607STor Egge /* 440719dc5607STor Egge * Fail if the entry was copy-on-write for a write fault. 440819dc5607STor Egge */ 440919dc5607STor Egge if (fault_type & VM_PROT_WRITE) 441019dc5607STor Egge return (KERN_FAILURE); 441119dc5607STor Egge /* 441219dc5607STor Egge * We're attempting to read a copy-on-write page -- 441319dc5607STor Egge * don't allow writes. 441419dc5607STor Egge */ 441519dc5607STor Egge prot &= ~VM_PROT_WRITE; 441619dc5607STor Egge } 441719dc5607STor Egge 441819dc5607STor Egge /* 441919dc5607STor Egge * Fail if an object should be created. 442019dc5607STor Egge */ 442119dc5607STor Egge if (entry->object.vm_object == NULL && !map->system_map) 442219dc5607STor Egge return (KERN_FAILURE); 442319dc5607STor Egge 442419dc5607STor Egge /* 442519dc5607STor Egge * Return the object/offset from this entry. If the entry was 442619dc5607STor Egge * copy-on-write or empty, it has been fixed up. 442719dc5607STor Egge */ 442810d9120cSKonstantin Belousov *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 442919dc5607STor Egge *object = entry->object.vm_object; 443019dc5607STor Egge 443119dc5607STor Egge *out_prot = prot; 443219dc5607STor Egge return (KERN_SUCCESS); 443319dc5607STor Egge } 443419dc5607STor Egge 443519dc5607STor Egge /* 4436df8bae1dSRodney W. Grimes * vm_map_lookup_done: 4437df8bae1dSRodney W. Grimes * 4438df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 4439df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 4440df8bae1dSRodney W. Grimes */ 44410d94caffSDavid Greenman void 44421b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 4443df8bae1dSRodney W. Grimes { 4444df8bae1dSRodney W. Grimes /* 4445df8bae1dSRodney W. Grimes * Unlock the main-level map 4446df8bae1dSRodney W. Grimes */ 4447df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 4448df8bae1dSRodney W. Grimes } 4449df8bae1dSRodney W. Grimes 445019ea042eSKonstantin Belousov vm_offset_t 445119ea042eSKonstantin Belousov vm_map_max_KBI(const struct vm_map *map) 445219ea042eSKonstantin Belousov { 445319ea042eSKonstantin Belousov 4454f0165b1cSKonstantin Belousov return (vm_map_max(map)); 445519ea042eSKonstantin Belousov } 445619ea042eSKonstantin Belousov 445719ea042eSKonstantin Belousov vm_offset_t 445819ea042eSKonstantin Belousov vm_map_min_KBI(const struct vm_map *map) 445919ea042eSKonstantin Belousov { 446019ea042eSKonstantin Belousov 4461f0165b1cSKonstantin Belousov return (vm_map_min(map)); 446219ea042eSKonstantin Belousov } 446319ea042eSKonstantin Belousov 446419ea042eSKonstantin Belousov pmap_t 446519ea042eSKonstantin Belousov vm_map_pmap_KBI(vm_map_t map) 446619ea042eSKonstantin Belousov { 446719ea042eSKonstantin Belousov 446819ea042eSKonstantin Belousov return (map->pmap); 446919ea042eSKonstantin Belousov } 447019ea042eSKonstantin Belousov 4471c7c34a24SBruce Evans #include "opt_ddb.h" 4472c3cb3e12SDavid Greenman #ifdef DDB 4473c7c34a24SBruce Evans #include <sys/kernel.h> 4474c7c34a24SBruce Evans 4475c7c34a24SBruce Evans #include <ddb/ddb.h> 4476c7c34a24SBruce Evans 44772ebcd458SAttilio Rao static void 44782ebcd458SAttilio Rao vm_map_print(vm_map_t map) 4479df8bae1dSRodney W. Grimes { 4480c0877f10SJohn Dyson vm_map_entry_t entry; 4481c7c34a24SBruce Evans 4482e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 4483e5f251d2SAlan Cox (void *)map, 4484101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 4485df8bae1dSRodney W. Grimes 4486c7c34a24SBruce Evans db_indent += 2; 4487df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 4488df8bae1dSRodney W. Grimes entry = entry->next) { 448919bd0d9cSKonstantin Belousov db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n", 449019bd0d9cSKonstantin Belousov (void *)entry, (void *)entry->start, (void *)entry->end, 449119bd0d9cSKonstantin Belousov entry->eflags); 4492e5f251d2SAlan Cox { 4493df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 4494df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 44950d94caffSDavid Greenman 449695e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 4497df8bae1dSRodney W. Grimes entry->protection, 4498df8bae1dSRodney W. Grimes entry->max_protection, 44998aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 4500df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 450195e5e988SJohn Dyson db_printf(", wired"); 4502df8bae1dSRodney W. Grimes } 45039fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 4504cd034a5bSMaxime Henrion db_printf(", share=%p, offset=0x%jx\n", 45059fdfe602SMatthew Dillon (void *)entry->object.sub_map, 4506cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 4507df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 45089fdfe602SMatthew Dillon (entry->prev->object.sub_map != 45099fdfe602SMatthew Dillon entry->object.sub_map)) { 4510c7c34a24SBruce Evans db_indent += 2; 45112ebcd458SAttilio Rao vm_map_print((vm_map_t)entry->object.sub_map); 4512c7c34a24SBruce Evans db_indent -= 2; 4513df8bae1dSRodney W. Grimes } 45140d94caffSDavid Greenman } else { 4515ef694c1aSEdward Tomasz Napierala if (entry->cred != NULL) 4516ef694c1aSEdward Tomasz Napierala db_printf(", ruid %d", entry->cred->cr_ruid); 4517cd034a5bSMaxime Henrion db_printf(", object=%p, offset=0x%jx", 4518101eeb7fSBruce Evans (void *)entry->object.vm_object, 4519cd034a5bSMaxime Henrion (uintmax_t)entry->offset); 4520ef694c1aSEdward Tomasz Napierala if (entry->object.vm_object && entry->object.vm_object->cred) 4521ef694c1aSEdward Tomasz Napierala db_printf(", obj ruid %d charge %jx", 4522ef694c1aSEdward Tomasz Napierala entry->object.vm_object->cred->cr_ruid, 45233364c323SKonstantin Belousov (uintmax_t)entry->object.vm_object->charge); 4524afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 4525c7c34a24SBruce Evans db_printf(", copy (%s)", 4526afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 4527c7c34a24SBruce Evans db_printf("\n"); 4528df8bae1dSRodney W. Grimes 4529df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 4530df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 4531df8bae1dSRodney W. Grimes entry->object.vm_object)) { 4532c7c34a24SBruce Evans db_indent += 2; 4533101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 4534101eeb7fSBruce Evans entry->object.vm_object, 453544bbc3b7SKonstantin Belousov 0, 0, (char *)0); 4536c7c34a24SBruce Evans db_indent -= 2; 4537df8bae1dSRodney W. Grimes } 4538df8bae1dSRodney W. Grimes } 4539df8bae1dSRodney W. Grimes } 4540c7c34a24SBruce Evans db_indent -= 2; 4541df8bae1dSRodney W. Grimes } 454295e5e988SJohn Dyson 45432ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map) 45442ebcd458SAttilio Rao { 45452ebcd458SAttilio Rao 45462ebcd458SAttilio Rao if (!have_addr) { 45472ebcd458SAttilio Rao db_printf("usage: show map <addr>\n"); 45482ebcd458SAttilio Rao return; 45492ebcd458SAttilio Rao } 45502ebcd458SAttilio Rao vm_map_print((vm_map_t)addr); 45512ebcd458SAttilio Rao } 455295e5e988SJohn Dyson 455395e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 455495e5e988SJohn Dyson { 455595e5e988SJohn Dyson struct proc *p; 455695e5e988SJohn Dyson 455795e5e988SJohn Dyson if (have_addr) { 4558a9546a6bSJohn Baldwin p = db_lookup_proc(addr); 455995e5e988SJohn Dyson } else { 456095e5e988SJohn Dyson p = curproc; 456195e5e988SJohn Dyson } 456295e5e988SJohn Dyson 4563ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 4564ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 4565b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 456695e5e988SJohn Dyson 45672ebcd458SAttilio Rao vm_map_print((vm_map_t)&p->p_vmspace->vm_map); 456895e5e988SJohn Dyson } 456995e5e988SJohn Dyson 4570c7c34a24SBruce Evans #endif /* DDB */ 4571