1df8bae1dSRodney W. Grimes /* 2df8bae1dSRodney W. Grimes * Copyright (c) 1991, 1993 3df8bae1dSRodney W. Grimes * The Regents of the University of California. All rights reserved. 4df8bae1dSRodney W. Grimes * 5df8bae1dSRodney W. Grimes * This code is derived from software contributed to Berkeley by 6df8bae1dSRodney W. Grimes * The Mach Operating System project at Carnegie-Mellon University. 7df8bae1dSRodney W. Grimes * 8df8bae1dSRodney W. Grimes * Redistribution and use in source and binary forms, with or without 9df8bae1dSRodney W. Grimes * modification, are permitted provided that the following conditions 10df8bae1dSRodney W. Grimes * are met: 11df8bae1dSRodney W. Grimes * 1. Redistributions of source code must retain the above copyright 12df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer. 13df8bae1dSRodney W. Grimes * 2. Redistributions in binary form must reproduce the above copyright 14df8bae1dSRodney W. Grimes * notice, this list of conditions and the following disclaimer in the 15df8bae1dSRodney W. Grimes * documentation and/or other materials provided with the distribution. 16df8bae1dSRodney W. Grimes * 3. All advertising materials mentioning features or use of this software 17df8bae1dSRodney W. Grimes * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 363c4dd356SDavid Greenman * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * 39df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40df8bae1dSRodney W. Grimes * All rights reserved. 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 45df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 46df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 47df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 48df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57df8bae1dSRodney W. Grimes * School of Computer Science 58df8bae1dSRodney W. Grimes * Carnegie Mellon University 59df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 62df8bae1dSRodney W. Grimes * rights to redistribute these changes. 633c4dd356SDavid Greenman * 6486524867SJohn Dyson * $Id: vm_map.c,v 1.122 1998/05/01 02:25:29 dyson Exp $ 65df8bae1dSRodney W. Grimes */ 66df8bae1dSRodney W. Grimes 67df8bae1dSRodney W. Grimes /* 68df8bae1dSRodney W. Grimes * Virtual memory mapping module. 69df8bae1dSRodney W. Grimes */ 70df8bae1dSRodney W. Grimes 71df8bae1dSRodney W. Grimes #include <sys/param.h> 72df8bae1dSRodney W. Grimes #include <sys/systm.h> 73df8bae1dSRodney W. Grimes #include <sys/malloc.h> 74b5e8ce9fSBruce Evans #include <sys/proc.h> 75efeaf95aSDavid Greenman #include <sys/vmmeter.h> 76867a482dSJohn Dyson #include <sys/mman.h> 771efb74fbSJohn Dyson #include <sys/buf.h> 781efb74fbSJohn Dyson #include <sys/vnode.h> 79df8bae1dSRodney W. Grimes 80df8bae1dSRodney W. Grimes #include <vm/vm.h> 81efeaf95aSDavid Greenman #include <vm/vm_param.h> 82efeaf95aSDavid Greenman #include <vm/vm_prot.h> 83efeaf95aSDavid Greenman #include <vm/vm_inherit.h> 84996c772fSJohn Dyson #include <sys/lock.h> 85efeaf95aSDavid Greenman #include <vm/pmap.h> 86efeaf95aSDavid Greenman #include <vm/vm_map.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 88df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 8947221757SJohn Dyson #include <vm/vm_pager.h> 9026f9a767SRodney W. Grimes #include <vm/vm_kern.h> 91efeaf95aSDavid Greenman #include <vm/vm_extern.h> 92f35329acSJohn Dyson #include <vm/default_pager.h> 9347221757SJohn Dyson #include <vm/swap_pager.h> 943075778bSJohn Dyson #include <vm/vm_zone.h> 95df8bae1dSRodney W. Grimes 96a1c995b6SPoul-Henning Kamp static MALLOC_DEFINE(M_VMMAP, "VM map", "VM map structures"); 9755166637SPoul-Henning Kamp 98df8bae1dSRodney W. Grimes /* 99df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 100df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 101df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 102df8bae1dSRodney W. Grimes * memory from one map to another. 103df8bae1dSRodney W. Grimes * 104df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 105df8bae1dSRodney W. Grimes * 106df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 107df8bae1dSRodney W. Grimes * entries; a single hint is used to speed up lookups. 108df8bae1dSRodney W. Grimes * 109df8bae1dSRodney W. Grimes * In order to properly represent the sharing of virtual 110df8bae1dSRodney W. Grimes * memory regions among maps, the map structure is bi-level. 111df8bae1dSRodney W. Grimes * Top-level ("address") maps refer to regions of sharable 112df8bae1dSRodney W. Grimes * virtual memory. These regions are implemented as 113df8bae1dSRodney W. Grimes * ("sharing") maps, which then refer to the actual virtual 114df8bae1dSRodney W. Grimes * memory objects. When two address maps "share" memory, 115df8bae1dSRodney W. Grimes * their top-level maps both have references to the same 116df8bae1dSRodney W. Grimes * sharing map. When memory is virtual-copied from one 117df8bae1dSRodney W. Grimes * address map to another, the references in the sharing 118df8bae1dSRodney W. Grimes * maps are actually copied -- no copying occurs at the 119df8bae1dSRodney W. Grimes * virtual memory object level. 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * Since portions of maps are specified by start/end addreses, 122df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 123df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 124df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 125df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 126df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 127df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 128df8bae1dSRodney W. Grimes * No attempt is currently made to "glue back together" two 129df8bae1dSRodney W. Grimes * abutting entries. 130df8bae1dSRodney W. Grimes * 131df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 132df8bae1dSRodney W. Grimes * by copying VM object references from one sharing map to 133df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 134df8bae1dSRodney W. Grimes * It is important to note that only one writeable reference 135df8bae1dSRodney W. Grimes * to a VM object region exists in any map -- this means that 136df8bae1dSRodney W. Grimes * shadow object creation can be delayed until a write operation 137df8bae1dSRodney W. Grimes * occurs. 138df8bae1dSRodney W. Grimes */ 139df8bae1dSRodney W. Grimes 140df8bae1dSRodney W. Grimes /* 141df8bae1dSRodney W. Grimes * vm_map_startup: 142df8bae1dSRodney W. Grimes * 143df8bae1dSRodney W. Grimes * Initialize the vm_map module. Must be called before 144df8bae1dSRodney W. Grimes * any other vm_map routines. 145df8bae1dSRodney W. Grimes * 146df8bae1dSRodney W. Grimes * Map and entry structures are allocated from the general 147df8bae1dSRodney W. Grimes * purpose memory pool with some exceptions: 148df8bae1dSRodney W. Grimes * 149df8bae1dSRodney W. Grimes * - The kernel map and kmem submap are allocated statically. 150df8bae1dSRodney W. Grimes * - Kernel map entries are allocated out of a static pool. 151df8bae1dSRodney W. Grimes * 152df8bae1dSRodney W. Grimes * These restrictions are necessary since malloc() uses the 153df8bae1dSRodney W. Grimes * maps and requires map entries. 154df8bae1dSRodney W. Grimes */ 155df8bae1dSRodney W. Grimes 156bd7e5f99SJohn Dyson extern char kstack[]; 157b7b2aac2SJohn Dyson extern int inmprotect; 158df8bae1dSRodney W. Grimes 1593075778bSJohn Dyson static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store; 1602d8acc0fSJohn Dyson static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone; 1613075778bSJohn Dyson static struct vm_object kmapentobj, mapentobj, mapobj; 1623075778bSJohn Dyson #define MAP_ENTRY_INIT 128 163303b270bSEivind Eklund static struct vm_map_entry map_entry_init[MAX_MAPENT]; 164303b270bSEivind Eklund static struct vm_map_entry kmap_entry_init[MAX_KMAPENT]; 165303b270bSEivind Eklund static struct vm_map map_init[MAX_KMAP]; 166b18bfc3dSJohn Dyson 167df8bae1dSRodney W. Grimes static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 168df8bae1dSRodney W. Grimes static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 169f708ef1bSPoul-Henning Kamp static vm_map_entry_t vm_map_entry_create __P((vm_map_t)); 170f708ef1bSPoul-Henning Kamp static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t)); 1710362d7d7SJohn Dyson static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t)); 172f708ef1bSPoul-Henning Kamp static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t)); 173f708ef1bSPoul-Henning Kamp static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t, 174f708ef1bSPoul-Henning Kamp vm_map_entry_t)); 175dda6b171SJohn Dyson static void vm_map_split __P((vm_map_entry_t)); 176df8bae1dSRodney W. Grimes 1770d94caffSDavid Greenman void 1780d94caffSDavid Greenman vm_map_startup() 179df8bae1dSRodney W. Grimes { 1803075778bSJohn Dyson mapzone = &mapzone_store; 1810d65e566SJohn Dyson zbootinit(mapzone, "MAP", sizeof (struct vm_map), 1823075778bSJohn Dyson map_init, MAX_KMAP); 1833075778bSJohn Dyson kmapentzone = &kmapentzone_store; 1840d65e566SJohn Dyson zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry), 1853075778bSJohn Dyson kmap_entry_init, MAX_KMAPENT); 1863075778bSJohn Dyson mapentzone = &mapentzone_store; 1870d65e566SJohn Dyson zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry), 1883075778bSJohn Dyson map_entry_init, MAX_MAPENT); 189df8bae1dSRodney W. Grimes } 190df8bae1dSRodney W. Grimes 191df8bae1dSRodney W. Grimes /* 192df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 193df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 194df8bae1dSRodney W. Grimes * The remaining fields must be initialized by the caller. 195df8bae1dSRodney W. Grimes */ 196df8bae1dSRodney W. Grimes struct vmspace * 1972d8acc0fSJohn Dyson vmspace_alloc(min, max) 198df8bae1dSRodney W. Grimes vm_offset_t min, max; 199df8bae1dSRodney W. Grimes { 200c0877f10SJohn Dyson struct vmspace *vm; 2010d94caffSDavid Greenman 2022d8acc0fSJohn Dyson vm = zalloc(vmspace_zone); 2032d8acc0fSJohn Dyson bzero(&vm->vm_map, sizeof vm->vm_map); 2042d8acc0fSJohn Dyson vm_map_init(&vm->vm_map, min, max); 205df8bae1dSRodney W. Grimes pmap_pinit(&vm->vm_pmap); 206df8bae1dSRodney W. Grimes vm->vm_map.pmap = &vm->vm_pmap; /* XXX */ 207df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 2082d8acc0fSJohn Dyson vm->vm_shm = NULL; 209df8bae1dSRodney W. Grimes return (vm); 210df8bae1dSRodney W. Grimes } 211df8bae1dSRodney W. Grimes 212df8bae1dSRodney W. Grimes void 2133075778bSJohn Dyson vm_init2(void) { 2140d65e566SJohn Dyson zinitna(kmapentzone, &kmapentobj, 2150a80f406SJohn Dyson NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1); 2160d65e566SJohn Dyson zinitna(mapentzone, &mapentobj, 2170a80f406SJohn Dyson NULL, 0, 0, 0, 1); 2180d65e566SJohn Dyson zinitna(mapzone, &mapobj, 2190a80f406SJohn Dyson NULL, 0, 0, 0, 1); 2202d8acc0fSJohn Dyson vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3); 221ba9be04cSJohn Dyson pmap_init2(); 22299448ed1SJohn Dyson vm_object_init2(); 2233075778bSJohn Dyson } 2243075778bSJohn Dyson 2253075778bSJohn Dyson void 226df8bae1dSRodney W. Grimes vmspace_free(vm) 227c0877f10SJohn Dyson struct vmspace *vm; 228df8bae1dSRodney W. Grimes { 229df8bae1dSRodney W. Grimes 230a1f6d91cSDavid Greenman if (vm->vm_refcnt == 0) 231a1f6d91cSDavid Greenman panic("vmspace_free: attempt to free already freed vmspace"); 232a1f6d91cSDavid Greenman 233df8bae1dSRodney W. Grimes if (--vm->vm_refcnt == 0) { 234bd7e5f99SJohn Dyson 23530dcfc09SJohn Dyson /* 236df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 2370d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 2380d94caffSDavid Greenman * the pmap module to reclaim anything left. 239df8bae1dSRodney W. Grimes */ 240df8bae1dSRodney W. Grimes vm_map_lock(&vm->vm_map); 241df8bae1dSRodney W. Grimes (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 242df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 243a1f6d91cSDavid Greenman vm_map_unlock(&vm->vm_map); 244b18bfc3dSJohn Dyson 245df8bae1dSRodney W. Grimes pmap_release(&vm->vm_pmap); 2462d8acc0fSJohn Dyson zfree(vmspace_zone, vm); 247df8bae1dSRodney W. Grimes } 248df8bae1dSRodney W. Grimes } 249df8bae1dSRodney W. Grimes 250df8bae1dSRodney W. Grimes /* 251df8bae1dSRodney W. Grimes * vm_map_create: 252df8bae1dSRodney W. Grimes * 253df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 254df8bae1dSRodney W. Grimes * the given physical map structure, and having 255df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 256df8bae1dSRodney W. Grimes */ 2570d94caffSDavid Greenman vm_map_t 2582d8acc0fSJohn Dyson vm_map_create(pmap, min, max) 259df8bae1dSRodney W. Grimes pmap_t pmap; 260df8bae1dSRodney W. Grimes vm_offset_t min, max; 261df8bae1dSRodney W. Grimes { 262c0877f10SJohn Dyson vm_map_t result; 263df8bae1dSRodney W. Grimes 2643075778bSJohn Dyson result = zalloc(mapzone); 2652d8acc0fSJohn Dyson vm_map_init(result, min, max); 266df8bae1dSRodney W. Grimes result->pmap = pmap; 267df8bae1dSRodney W. Grimes return (result); 268df8bae1dSRodney W. Grimes } 269df8bae1dSRodney W. Grimes 270df8bae1dSRodney W. Grimes /* 271df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 272df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 273df8bae1dSRodney W. Grimes * The pmap is set elsewhere. 274df8bae1dSRodney W. Grimes */ 275df8bae1dSRodney W. Grimes void 2762d8acc0fSJohn Dyson vm_map_init(map, min, max) 277c0877f10SJohn Dyson struct vm_map *map; 278df8bae1dSRodney W. Grimes vm_offset_t min, max; 279df8bae1dSRodney W. Grimes { 280df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 281df8bae1dSRodney W. Grimes map->nentries = 0; 282df8bae1dSRodney W. Grimes map->size = 0; 283df8bae1dSRodney W. Grimes map->is_main_map = TRUE; 2843075778bSJohn Dyson map->system_map = 0; 285df8bae1dSRodney W. Grimes map->min_offset = min; 286df8bae1dSRodney W. Grimes map->max_offset = max; 287df8bae1dSRodney W. Grimes map->first_free = &map->header; 288df8bae1dSRodney W. Grimes map->hint = &map->header; 289df8bae1dSRodney W. Grimes map->timestamp = 0; 2908f9110f6SJohn Dyson lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE); 291df8bae1dSRodney W. Grimes } 292df8bae1dSRodney W. Grimes 293df8bae1dSRodney W. Grimes /* 294b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 295b18bfc3dSJohn Dyson * 296b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 297b18bfc3dSJohn Dyson */ 29862487bb4SJohn Dyson static void 299b18bfc3dSJohn Dyson vm_map_entry_dispose(map, entry) 300b18bfc3dSJohn Dyson vm_map_t map; 301b18bfc3dSJohn Dyson vm_map_entry_t entry; 302b18bfc3dSJohn Dyson { 303b79933ebSJohn Dyson zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry); 304b18bfc3dSJohn Dyson } 305b18bfc3dSJohn Dyson 306b18bfc3dSJohn Dyson /* 307df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 308df8bae1dSRodney W. Grimes * 309df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 310df8bae1dSRodney W. Grimes * No entry fields are filled in. This routine is 311df8bae1dSRodney W. Grimes */ 312f708ef1bSPoul-Henning Kamp static vm_map_entry_t 31326f9a767SRodney W. Grimes vm_map_entry_create(map) 314df8bae1dSRodney W. Grimes vm_map_t map; 315df8bae1dSRodney W. Grimes { 316b79933ebSJohn Dyson return zalloc((map->system_map || !mapentzone) ? kmapentzone : mapentzone); 317df8bae1dSRodney W. Grimes } 318df8bae1dSRodney W. Grimes 319df8bae1dSRodney W. Grimes /* 320df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 321df8bae1dSRodney W. Grimes * 322df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 323df8bae1dSRodney W. Grimes */ 324df8bae1dSRodney W. Grimes #define vm_map_entry_link(map, after_where, entry) \ 325df8bae1dSRodney W. Grimes { \ 326df8bae1dSRodney W. Grimes (map)->nentries++; \ 32747221757SJohn Dyson (map)->timestamp++; \ 328df8bae1dSRodney W. Grimes (entry)->prev = (after_where); \ 329df8bae1dSRodney W. Grimes (entry)->next = (after_where)->next; \ 330df8bae1dSRodney W. Grimes (entry)->prev->next = (entry); \ 331df8bae1dSRodney W. Grimes (entry)->next->prev = (entry); \ 332df8bae1dSRodney W. Grimes } 333df8bae1dSRodney W. Grimes #define vm_map_entry_unlink(map, entry) \ 334df8bae1dSRodney W. Grimes { \ 335df8bae1dSRodney W. Grimes (map)->nentries--; \ 33647221757SJohn Dyson (map)->timestamp++; \ 337df8bae1dSRodney W. Grimes (entry)->next->prev = (entry)->prev; \ 338df8bae1dSRodney W. Grimes (entry)->prev->next = (entry)->next; \ 339df8bae1dSRodney W. Grimes } 340df8bae1dSRodney W. Grimes 341df8bae1dSRodney W. Grimes /* 342df8bae1dSRodney W. Grimes * SAVE_HINT: 343df8bae1dSRodney W. Grimes * 344df8bae1dSRodney W. Grimes * Saves the specified entry as the hint for 34524a1cce3SDavid Greenman * future lookups. 346df8bae1dSRodney W. Grimes */ 347df8bae1dSRodney W. Grimes #define SAVE_HINT(map,value) \ 34824a1cce3SDavid Greenman (map)->hint = (value); 349df8bae1dSRodney W. Grimes 350df8bae1dSRodney W. Grimes /* 351df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 352df8bae1dSRodney W. Grimes * 353df8bae1dSRodney W. Grimes * Finds the map entry containing (or 354df8bae1dSRodney W. Grimes * immediately preceding) the specified address 355df8bae1dSRodney W. Grimes * in the given map; the entry is returned 356df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 357df8bae1dSRodney W. Grimes * result indicates whether the address is 358df8bae1dSRodney W. Grimes * actually contained in the map. 359df8bae1dSRodney W. Grimes */ 3600d94caffSDavid Greenman boolean_t 3610d94caffSDavid Greenman vm_map_lookup_entry(map, address, entry) 362c0877f10SJohn Dyson vm_map_t map; 363c0877f10SJohn Dyson vm_offset_t address; 364df8bae1dSRodney W. Grimes vm_map_entry_t *entry; /* OUT */ 365df8bae1dSRodney W. Grimes { 366c0877f10SJohn Dyson vm_map_entry_t cur; 367c0877f10SJohn Dyson vm_map_entry_t last; 368df8bae1dSRodney W. Grimes 369df8bae1dSRodney W. Grimes /* 3700d94caffSDavid Greenman * Start looking either from the head of the list, or from the hint. 371df8bae1dSRodney W. Grimes */ 372df8bae1dSRodney W. Grimes 373df8bae1dSRodney W. Grimes cur = map->hint; 374df8bae1dSRodney W. Grimes 375df8bae1dSRodney W. Grimes if (cur == &map->header) 376df8bae1dSRodney W. Grimes cur = cur->next; 377df8bae1dSRodney W. Grimes 378df8bae1dSRodney W. Grimes if (address >= cur->start) { 379df8bae1dSRodney W. Grimes /* 380df8bae1dSRodney W. Grimes * Go from hint to end of list. 381df8bae1dSRodney W. Grimes * 3820d94caffSDavid Greenman * But first, make a quick check to see if we are already looking 3830d94caffSDavid Greenman * at the entry we want (which is usually the case). Note also 3840d94caffSDavid Greenman * that we don't need to save the hint here... it is the same 3850d94caffSDavid Greenman * hint (unless we are at the header, in which case the hint 3860d94caffSDavid Greenman * didn't buy us anything anyway). 387df8bae1dSRodney W. Grimes */ 388df8bae1dSRodney W. Grimes last = &map->header; 389df8bae1dSRodney W. Grimes if ((cur != last) && (cur->end > address)) { 390df8bae1dSRodney W. Grimes *entry = cur; 391df8bae1dSRodney W. Grimes return (TRUE); 392df8bae1dSRodney W. Grimes } 3930d94caffSDavid Greenman } else { 394df8bae1dSRodney W. Grimes /* 395df8bae1dSRodney W. Grimes * Go from start to hint, *inclusively* 396df8bae1dSRodney W. Grimes */ 397df8bae1dSRodney W. Grimes last = cur->next; 398df8bae1dSRodney W. Grimes cur = map->header.next; 399df8bae1dSRodney W. Grimes } 400df8bae1dSRodney W. Grimes 401df8bae1dSRodney W. Grimes /* 402df8bae1dSRodney W. Grimes * Search linearly 403df8bae1dSRodney W. Grimes */ 404df8bae1dSRodney W. Grimes 405df8bae1dSRodney W. Grimes while (cur != last) { 406df8bae1dSRodney W. Grimes if (cur->end > address) { 407df8bae1dSRodney W. Grimes if (address >= cur->start) { 408df8bae1dSRodney W. Grimes /* 4090d94caffSDavid Greenman * Save this lookup for future hints, and 4100d94caffSDavid Greenman * return 411df8bae1dSRodney W. Grimes */ 412df8bae1dSRodney W. Grimes 413df8bae1dSRodney W. Grimes *entry = cur; 414df8bae1dSRodney W. Grimes SAVE_HINT(map, cur); 415df8bae1dSRodney W. Grimes return (TRUE); 416df8bae1dSRodney W. Grimes } 417df8bae1dSRodney W. Grimes break; 418df8bae1dSRodney W. Grimes } 419df8bae1dSRodney W. Grimes cur = cur->next; 420df8bae1dSRodney W. Grimes } 421df8bae1dSRodney W. Grimes *entry = cur->prev; 422df8bae1dSRodney W. Grimes SAVE_HINT(map, *entry); 423df8bae1dSRodney W. Grimes return (FALSE); 424df8bae1dSRodney W. Grimes } 425df8bae1dSRodney W. Grimes 426df8bae1dSRodney W. Grimes /* 42730dcfc09SJohn Dyson * vm_map_insert: 42830dcfc09SJohn Dyson * 42930dcfc09SJohn Dyson * Inserts the given whole VM object into the target 43030dcfc09SJohn Dyson * map at the specified address range. The object's 43130dcfc09SJohn Dyson * size should match that of the address range. 43230dcfc09SJohn Dyson * 43330dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 43430dcfc09SJohn Dyson */ 43530dcfc09SJohn Dyson int 436b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 437b9dcd593SBruce Evans vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 438b9dcd593SBruce Evans int cow) 43930dcfc09SJohn Dyson { 440c0877f10SJohn Dyson vm_map_entry_t new_entry; 441c0877f10SJohn Dyson vm_map_entry_t prev_entry; 44230dcfc09SJohn Dyson vm_map_entry_t temp_entry; 443a5b6fd29SJohn Dyson vm_object_t prev_object; 444afa07f7eSJohn Dyson u_char protoeflags; 44530dcfc09SJohn Dyson 446cdc2c291SJohn Dyson if ((object != NULL) && (cow & MAP_NOFAULT)) { 447cdc2c291SJohn Dyson panic("vm_map_insert: paradoxical MAP_NOFAULT request"); 448cdc2c291SJohn Dyson } 449cdc2c291SJohn Dyson 45030dcfc09SJohn Dyson /* 45130dcfc09SJohn Dyson * Check that the start and end points are not bogus. 45230dcfc09SJohn Dyson */ 45330dcfc09SJohn Dyson 45430dcfc09SJohn Dyson if ((start < map->min_offset) || (end > map->max_offset) || 45530dcfc09SJohn Dyson (start >= end)) 45630dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 45730dcfc09SJohn Dyson 45830dcfc09SJohn Dyson /* 45930dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 46030dcfc09SJohn Dyson * of an existing entry, this range is bogus. 46130dcfc09SJohn Dyson */ 46230dcfc09SJohn Dyson 46330dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 46430dcfc09SJohn Dyson return (KERN_NO_SPACE); 46530dcfc09SJohn Dyson 46630dcfc09SJohn Dyson prev_entry = temp_entry; 46730dcfc09SJohn Dyson 46830dcfc09SJohn Dyson /* 46930dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 47030dcfc09SJohn Dyson */ 47130dcfc09SJohn Dyson 47230dcfc09SJohn Dyson if ((prev_entry->next != &map->header) && 47330dcfc09SJohn Dyson (prev_entry->next->start < end)) 47430dcfc09SJohn Dyson return (KERN_NO_SPACE); 47530dcfc09SJohn Dyson 476afa07f7eSJohn Dyson protoeflags = 0; 477afa07f7eSJohn Dyson if (cow & MAP_COPY_NEEDED) 478afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NEEDS_COPY; 479afa07f7eSJohn Dyson 480afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 481afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_COW; 482afa07f7eSJohn Dyson 483afa07f7eSJohn Dyson if (cow & MAP_NOFAULT) 484afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 485afa07f7eSJohn Dyson 48630dcfc09SJohn Dyson /* 48730dcfc09SJohn Dyson * See if we can avoid creating a new entry by extending one of our 4888cc7e047SJohn Dyson * neighbors. Or at least extend the object. 48930dcfc09SJohn Dyson */ 4908cc7e047SJohn Dyson 4918cc7e047SJohn Dyson if ((object == NULL) && 4928cc7e047SJohn Dyson (prev_entry != &map->header) && 493afa07f7eSJohn Dyson (( prev_entry->eflags & (MAP_ENTRY_IS_A_MAP | MAP_ENTRY_IS_SUB_MAP)) == 0) && 49495e5e988SJohn Dyson ((prev_entry->object.vm_object == NULL) || 49595e5e988SJohn Dyson (prev_entry->object.vm_object->type == OBJT_DEFAULT)) && 4968cc7e047SJohn Dyson (prev_entry->end == start) && 4978cc7e047SJohn Dyson (prev_entry->wired_count == 0)) { 4988cc7e047SJohn Dyson 499cdc2c291SJohn Dyson 500afa07f7eSJohn Dyson if ((protoeflags == prev_entry->eflags) && 501afa07f7eSJohn Dyson ((cow & MAP_NOFAULT) || 5028cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 50330dcfc09SJohn Dyson OFF_TO_IDX(prev_entry->offset), 5048cc7e047SJohn Dyson (vm_size_t) (prev_entry->end - prev_entry->start), 505cdc2c291SJohn Dyson (vm_size_t) (end - prev_entry->end)))) { 506a5b6fd29SJohn Dyson 50730dcfc09SJohn Dyson /* 5088cc7e047SJohn Dyson * Coalesced the two objects. Can we extend the 5098cc7e047SJohn Dyson * previous map entry to include the new range? 51030dcfc09SJohn Dyson */ 5118cc7e047SJohn Dyson if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 5128cc7e047SJohn Dyson (prev_entry->protection == prot) && 5138cc7e047SJohn Dyson (prev_entry->max_protection == max)) { 5148cc7e047SJohn Dyson 51530dcfc09SJohn Dyson map->size += (end - prev_entry->end); 51630dcfc09SJohn Dyson prev_entry->end = end; 517afa07f7eSJohn Dyson if ((cow & MAP_NOFAULT) == 0) { 518a5b6fd29SJohn Dyson prev_object = prev_entry->object.vm_object; 519b5b40fa6SJohn Dyson default_pager_convert_to_swapq(prev_object); 520cdc2c291SJohn Dyson } 52130dcfc09SJohn Dyson return (KERN_SUCCESS); 52230dcfc09SJohn Dyson } 5238cc7e047SJohn Dyson else { 5248cc7e047SJohn Dyson object = prev_entry->object.vm_object; 5258cc7e047SJohn Dyson offset = prev_entry->offset + (prev_entry->end - 5268cc7e047SJohn Dyson prev_entry->start); 5278cc7e047SJohn Dyson 5288cc7e047SJohn Dyson vm_object_reference(object); 529b18bfc3dSJohn Dyson } 53067bf6868SJohn Dyson } 5318cc7e047SJohn Dyson } 5328cc7e047SJohn Dyson 53330dcfc09SJohn Dyson /* 53430dcfc09SJohn Dyson * Create a new entry 53530dcfc09SJohn Dyson */ 53630dcfc09SJohn Dyson 53730dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 53830dcfc09SJohn Dyson new_entry->start = start; 53930dcfc09SJohn Dyson new_entry->end = end; 54030dcfc09SJohn Dyson 541afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 54230dcfc09SJohn Dyson new_entry->object.vm_object = object; 54330dcfc09SJohn Dyson new_entry->offset = offset; 54430dcfc09SJohn Dyson 54530dcfc09SJohn Dyson if (map->is_main_map) { 54630dcfc09SJohn Dyson new_entry->inheritance = VM_INHERIT_DEFAULT; 54730dcfc09SJohn Dyson new_entry->protection = prot; 54830dcfc09SJohn Dyson new_entry->max_protection = max; 54930dcfc09SJohn Dyson new_entry->wired_count = 0; 55030dcfc09SJohn Dyson } 55130dcfc09SJohn Dyson /* 55230dcfc09SJohn Dyson * Insert the new entry into the list 55330dcfc09SJohn Dyson */ 55430dcfc09SJohn Dyson 55530dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 55630dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 55730dcfc09SJohn Dyson 55830dcfc09SJohn Dyson /* 55930dcfc09SJohn Dyson * Update the free space hint 56030dcfc09SJohn Dyson */ 56167bf6868SJohn Dyson if ((map->first_free == prev_entry) && 56267bf6868SJohn Dyson (prev_entry->end >= new_entry->start)) 56330dcfc09SJohn Dyson map->first_free = new_entry; 56430dcfc09SJohn Dyson 565b5b40fa6SJohn Dyson default_pager_convert_to_swapq(object); 56630dcfc09SJohn Dyson return (KERN_SUCCESS); 56730dcfc09SJohn Dyson } 56830dcfc09SJohn Dyson 56930dcfc09SJohn Dyson /* 570df8bae1dSRodney W. Grimes * Find sufficient space for `length' bytes in the given map, starting at 571df8bae1dSRodney W. Grimes * `start'. The map must be locked. Returns 0 on success, 1 on no space. 572df8bae1dSRodney W. Grimes */ 573df8bae1dSRodney W. Grimes int 574df8bae1dSRodney W. Grimes vm_map_findspace(map, start, length, addr) 575c0877f10SJohn Dyson vm_map_t map; 576c0877f10SJohn Dyson vm_offset_t start; 577df8bae1dSRodney W. Grimes vm_size_t length; 578df8bae1dSRodney W. Grimes vm_offset_t *addr; 579df8bae1dSRodney W. Grimes { 580c0877f10SJohn Dyson vm_map_entry_t entry, next; 581c0877f10SJohn Dyson vm_offset_t end; 582df8bae1dSRodney W. Grimes 583df8bae1dSRodney W. Grimes if (start < map->min_offset) 584df8bae1dSRodney W. Grimes start = map->min_offset; 585df8bae1dSRodney W. Grimes if (start > map->max_offset) 586df8bae1dSRodney W. Grimes return (1); 587df8bae1dSRodney W. Grimes 588df8bae1dSRodney W. Grimes /* 5890d94caffSDavid Greenman * Look for the first possible address; if there's already something 5900d94caffSDavid Greenman * at this address, we have to start after it. 591df8bae1dSRodney W. Grimes */ 592df8bae1dSRodney W. Grimes if (start == map->min_offset) { 59367bf6868SJohn Dyson if ((entry = map->first_free) != &map->header) 594df8bae1dSRodney W. Grimes start = entry->end; 595df8bae1dSRodney W. Grimes } else { 596df8bae1dSRodney W. Grimes vm_map_entry_t tmp; 5970d94caffSDavid Greenman 598df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &tmp)) 599df8bae1dSRodney W. Grimes start = tmp->end; 600df8bae1dSRodney W. Grimes entry = tmp; 601df8bae1dSRodney W. Grimes } 602df8bae1dSRodney W. Grimes 603df8bae1dSRodney W. Grimes /* 6040d94caffSDavid Greenman * Look through the rest of the map, trying to fit a new region in the 6050d94caffSDavid Greenman * gap between existing regions, or after the very last region. 606df8bae1dSRodney W. Grimes */ 607df8bae1dSRodney W. Grimes for (;; start = (entry = next)->end) { 608df8bae1dSRodney W. Grimes /* 609df8bae1dSRodney W. Grimes * Find the end of the proposed new region. Be sure we didn't 610df8bae1dSRodney W. Grimes * go beyond the end of the map, or wrap around the address; 611df8bae1dSRodney W. Grimes * if so, we lose. Otherwise, if this is the last entry, or 612df8bae1dSRodney W. Grimes * if the proposed new region fits before the next entry, we 613df8bae1dSRodney W. Grimes * win. 614df8bae1dSRodney W. Grimes */ 615df8bae1dSRodney W. Grimes end = start + length; 616df8bae1dSRodney W. Grimes if (end > map->max_offset || end < start) 617df8bae1dSRodney W. Grimes return (1); 618df8bae1dSRodney W. Grimes next = entry->next; 619df8bae1dSRodney W. Grimes if (next == &map->header || next->start >= end) 620df8bae1dSRodney W. Grimes break; 621df8bae1dSRodney W. Grimes } 622df8bae1dSRodney W. Grimes SAVE_HINT(map, entry); 623df8bae1dSRodney W. Grimes *addr = start; 62499448ed1SJohn Dyson if (map == kernel_map) { 62599448ed1SJohn Dyson vm_offset_t ksize; 62699448ed1SJohn Dyson if ((ksize = round_page(start + length)) > kernel_vm_end) { 62799448ed1SJohn Dyson pmap_growkernel(ksize); 62899448ed1SJohn Dyson } 62999448ed1SJohn Dyson } 630df8bae1dSRodney W. Grimes return (0); 631df8bae1dSRodney W. Grimes } 632df8bae1dSRodney W. Grimes 633df8bae1dSRodney W. Grimes /* 634df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 635df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 636df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 637df8bae1dSRodney W. Grimes * returned in the same parameter. 638df8bae1dSRodney W. Grimes * 639df8bae1dSRodney W. Grimes */ 640df8bae1dSRodney W. Grimes int 641b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 642b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 643b9dcd593SBruce Evans vm_size_t length, boolean_t find_space, vm_prot_t prot, 644b9dcd593SBruce Evans vm_prot_t max, int cow) 645df8bae1dSRodney W. Grimes { 646c0877f10SJohn Dyson vm_offset_t start; 6478d6e8edeSDavid Greenman int result, s = 0; 648df8bae1dSRodney W. Grimes 649df8bae1dSRodney W. Grimes start = *addr; 6508d6e8edeSDavid Greenman 6519579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 652b18bfc3dSJohn Dyson s = splvm(); 6538d6e8edeSDavid Greenman 654bea41bcfSDavid Greenman vm_map_lock(map); 655df8bae1dSRodney W. Grimes if (find_space) { 656df8bae1dSRodney W. Grimes if (vm_map_findspace(map, start, length, addr)) { 657df8bae1dSRodney W. Grimes vm_map_unlock(map); 6589579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 6598d6e8edeSDavid Greenman splx(s); 660df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 661df8bae1dSRodney W. Grimes } 662df8bae1dSRodney W. Grimes start = *addr; 663df8bae1dSRodney W. Grimes } 664bd7e5f99SJohn Dyson result = vm_map_insert(map, object, offset, 665bd7e5f99SJohn Dyson start, start + length, prot, max, cow); 666df8bae1dSRodney W. Grimes vm_map_unlock(map); 6678d6e8edeSDavid Greenman 6689579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 6698d6e8edeSDavid Greenman splx(s); 6708d6e8edeSDavid Greenman 671df8bae1dSRodney W. Grimes return (result); 672df8bae1dSRodney W. Grimes } 673df8bae1dSRodney W. Grimes 674df8bae1dSRodney W. Grimes /* 675b7b2aac2SJohn Dyson * vm_map_simplify_entry: 67667bf6868SJohn Dyson * 677b7b2aac2SJohn Dyson * Simplify the given map entry by merging with either neighbor. 678df8bae1dSRodney W. Grimes */ 679b7b2aac2SJohn Dyson void 6800d94caffSDavid Greenman vm_map_simplify_entry(map, entry) 681df8bae1dSRodney W. Grimes vm_map_t map; 682df8bae1dSRodney W. Grimes vm_map_entry_t entry; 683df8bae1dSRodney W. Grimes { 684308c24baSJohn Dyson vm_map_entry_t next, prev; 685b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 686df8bae1dSRodney W. Grimes 687afa07f7eSJohn Dyson if (entry->eflags & (MAP_ENTRY_IS_SUB_MAP|MAP_ENTRY_IS_A_MAP)) 688df8bae1dSRodney W. Grimes return; 689308c24baSJohn Dyson 690308c24baSJohn Dyson prev = entry->prev; 691308c24baSJohn Dyson if (prev != &map->header) { 69267bf6868SJohn Dyson prevsize = prev->end - prev->start; 69367bf6868SJohn Dyson if ( (prev->end == entry->start) && 69467bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 69595e5e988SJohn Dyson (!prev->object.vm_object || 69695e5e988SJohn Dyson (prev->object.vm_object->behavior == entry->object.vm_object->behavior)) && 69767bf6868SJohn Dyson (!prev->object.vm_object || 69867bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 699afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 70067bf6868SJohn Dyson (prev->protection == entry->protection) && 70167bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 70267bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 703b7b2aac2SJohn Dyson (prev->wired_count == entry->wired_count)) { 704308c24baSJohn Dyson if (map->first_free == prev) 705308c24baSJohn Dyson map->first_free = entry; 706b18bfc3dSJohn Dyson if (map->hint == prev) 707b18bfc3dSJohn Dyson map->hint = entry; 708308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 709308c24baSJohn Dyson entry->start = prev->start; 710308c24baSJohn Dyson entry->offset = prev->offset; 711b18bfc3dSJohn Dyson if (prev->object.vm_object) 712308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 713308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 714308c24baSJohn Dyson } 715308c24baSJohn Dyson } 716de5f6a77SJohn Dyson 717de5f6a77SJohn Dyson next = entry->next; 718308c24baSJohn Dyson if (next != &map->header) { 71967bf6868SJohn Dyson esize = entry->end - entry->start; 72067bf6868SJohn Dyson if ((entry->end == next->start) && 72167bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 72295e5e988SJohn Dyson (!next->object.vm_object || 72395e5e988SJohn Dyson (next->object.vm_object->behavior == entry->object.vm_object->behavior)) && 72467bf6868SJohn Dyson (!entry->object.vm_object || 72567bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 726afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 72767bf6868SJohn Dyson (next->protection == entry->protection) && 72867bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 72967bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 730b7b2aac2SJohn Dyson (next->wired_count == entry->wired_count)) { 731308c24baSJohn Dyson if (map->first_free == next) 732308c24baSJohn Dyson map->first_free = entry; 733b18bfc3dSJohn Dyson if (map->hint == next) 734b18bfc3dSJohn Dyson map->hint = entry; 735de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 736de5f6a77SJohn Dyson entry->end = next->end; 737b18bfc3dSJohn Dyson if (next->object.vm_object) 738de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 739de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 740df8bae1dSRodney W. Grimes } 741df8bae1dSRodney W. Grimes } 742de5f6a77SJohn Dyson } 743df8bae1dSRodney W. Grimes /* 744df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 745df8bae1dSRodney W. Grimes * 746df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 747df8bae1dSRodney W. Grimes * the specified address; if necessary, 748df8bae1dSRodney W. Grimes * it splits the entry into two. 749df8bae1dSRodney W. Grimes */ 750df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 751df8bae1dSRodney W. Grimes { \ 752df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 753df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 754c0877f10SJohn Dyson else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \ 755c0877f10SJohn Dyson entry->object.vm_object->flags |= OBJ_ONEMAPPING; \ 756df8bae1dSRodney W. Grimes } 757df8bae1dSRodney W. Grimes 758df8bae1dSRodney W. Grimes /* 759df8bae1dSRodney W. Grimes * This routine is called only when it is known that 760df8bae1dSRodney W. Grimes * the entry must be split. 761df8bae1dSRodney W. Grimes */ 7620d94caffSDavid Greenman static void 7630d94caffSDavid Greenman _vm_map_clip_start(map, entry, start) 764c0877f10SJohn Dyson vm_map_t map; 765c0877f10SJohn Dyson vm_map_entry_t entry; 766c0877f10SJohn Dyson vm_offset_t start; 767df8bae1dSRodney W. Grimes { 768c0877f10SJohn Dyson vm_map_entry_t new_entry; 769df8bae1dSRodney W. Grimes 770df8bae1dSRodney W. Grimes /* 7710d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 7720d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 7730d94caffSDavid Greenman * starting address. 774df8bae1dSRodney W. Grimes */ 775df8bae1dSRodney W. Grimes 776f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 777f32dbbeeSJohn Dyson 77811cccda1SJohn Dyson /* 77911cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 78011cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 78111cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 78211cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 78311cccda1SJohn Dyson * put this improvement. 78411cccda1SJohn Dyson */ 78511cccda1SJohn Dyson 78611cccda1SJohn Dyson if (entry->object.vm_object == NULL) { 78711cccda1SJohn Dyson vm_object_t object; 78811cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 789c2e11a03SJohn Dyson atop(entry->end - entry->start)); 79011cccda1SJohn Dyson entry->object.vm_object = object; 79111cccda1SJohn Dyson entry->offset = 0; 79211cccda1SJohn Dyson } 79311cccda1SJohn Dyson 794df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 795df8bae1dSRodney W. Grimes *new_entry = *entry; 796df8bae1dSRodney W. Grimes 797df8bae1dSRodney W. Grimes new_entry->end = start; 798df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 799df8bae1dSRodney W. Grimes entry->start = start; 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 802df8bae1dSRodney W. Grimes 803c0877f10SJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 804c0877f10SJohn Dyson if (new_entry->object.vm_object->ref_count == 1) 805c0877f10SJohn Dyson new_entry->object.vm_object->flags |= OBJ_ONEMAPPING; 806df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 807df8bae1dSRodney W. Grimes } 808c0877f10SJohn Dyson } 809df8bae1dSRodney W. Grimes 810df8bae1dSRodney W. Grimes /* 811df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 812df8bae1dSRodney W. Grimes * 813df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 814df8bae1dSRodney W. Grimes * the specified address; if necessary, 815df8bae1dSRodney W. Grimes * it splits the entry into two. 816df8bae1dSRodney W. Grimes */ 817df8bae1dSRodney W. Grimes 818df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 819df8bae1dSRodney W. Grimes { \ 820df8bae1dSRodney W. Grimes if (endaddr < entry->end) \ 821df8bae1dSRodney W. Grimes _vm_map_clip_end(map, entry, endaddr); \ 822c0877f10SJohn Dyson else if (entry->object.vm_object && (entry->object.vm_object->ref_count == 1)) \ 823c0877f10SJohn Dyson entry->object.vm_object->flags |= OBJ_ONEMAPPING; \ 824df8bae1dSRodney W. Grimes } 825df8bae1dSRodney W. Grimes 826df8bae1dSRodney W. Grimes /* 827df8bae1dSRodney W. Grimes * This routine is called only when it is known that 828df8bae1dSRodney W. Grimes * the entry must be split. 829df8bae1dSRodney W. Grimes */ 8300d94caffSDavid Greenman static void 8310d94caffSDavid Greenman _vm_map_clip_end(map, entry, end) 832c0877f10SJohn Dyson vm_map_t map; 833c0877f10SJohn Dyson vm_map_entry_t entry; 834c0877f10SJohn Dyson vm_offset_t end; 835df8bae1dSRodney W. Grimes { 836c0877f10SJohn Dyson vm_map_entry_t new_entry; 837df8bae1dSRodney W. Grimes 838df8bae1dSRodney W. Grimes /* 83911cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 84011cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 84111cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 84211cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 84311cccda1SJohn Dyson * put this improvement. 84411cccda1SJohn Dyson */ 84511cccda1SJohn Dyson 84611cccda1SJohn Dyson if (entry->object.vm_object == NULL) { 84711cccda1SJohn Dyson vm_object_t object; 84811cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 849c2e11a03SJohn Dyson atop(entry->end - entry->start)); 85011cccda1SJohn Dyson entry->object.vm_object = object; 85111cccda1SJohn Dyson entry->offset = 0; 85211cccda1SJohn Dyson } 85311cccda1SJohn Dyson 85411cccda1SJohn Dyson /* 8550d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 856df8bae1dSRodney W. Grimes */ 857df8bae1dSRodney W. Grimes 858df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 859df8bae1dSRodney W. Grimes *new_entry = *entry; 860df8bae1dSRodney W. Grimes 861df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 862df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 863df8bae1dSRodney W. Grimes 864df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 865df8bae1dSRodney W. Grimes 866c0877f10SJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 867c0877f10SJohn Dyson if (new_entry->object.vm_object->ref_count == 1) 868c0877f10SJohn Dyson new_entry->object.vm_object->flags |= OBJ_ONEMAPPING; 869df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 870df8bae1dSRodney W. Grimes } 871c0877f10SJohn Dyson } 872df8bae1dSRodney W. Grimes 873df8bae1dSRodney W. Grimes /* 874df8bae1dSRodney W. Grimes * VM_MAP_RANGE_CHECK: [ internal use only ] 875df8bae1dSRodney W. Grimes * 876df8bae1dSRodney W. Grimes * Asserts that the starting and ending region 877df8bae1dSRodney W. Grimes * addresses fall within the valid range of the map. 878df8bae1dSRodney W. Grimes */ 879df8bae1dSRodney W. Grimes #define VM_MAP_RANGE_CHECK(map, start, end) \ 880df8bae1dSRodney W. Grimes { \ 881df8bae1dSRodney W. Grimes if (start < vm_map_min(map)) \ 882df8bae1dSRodney W. Grimes start = vm_map_min(map); \ 883df8bae1dSRodney W. Grimes if (end > vm_map_max(map)) \ 884df8bae1dSRodney W. Grimes end = vm_map_max(map); \ 885df8bae1dSRodney W. Grimes if (start > end) \ 886df8bae1dSRodney W. Grimes start = end; \ 887df8bae1dSRodney W. Grimes } 888df8bae1dSRodney W. Grimes 889df8bae1dSRodney W. Grimes /* 890df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 891df8bae1dSRodney W. Grimes * 892df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 893df8bae1dSRodney W. Grimes * 894df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 895df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 896df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 897df8bae1dSRodney W. Grimes * 898df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 899df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 900df8bae1dSRodney W. Grimes * vm_fault 901df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 902df8bae1dSRodney W. Grimes * 903df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 904df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 905df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 906df8bae1dSRodney W. Grimes */ 907df8bae1dSRodney W. Grimes int 908df8bae1dSRodney W. Grimes vm_map_submap(map, start, end, submap) 909c0877f10SJohn Dyson vm_map_t map; 910c0877f10SJohn Dyson vm_offset_t start; 911c0877f10SJohn Dyson vm_offset_t end; 912df8bae1dSRodney W. Grimes vm_map_t submap; 913df8bae1dSRodney W. Grimes { 914df8bae1dSRodney W. Grimes vm_map_entry_t entry; 915c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 916df8bae1dSRodney W. Grimes 917df8bae1dSRodney W. Grimes vm_map_lock(map); 918df8bae1dSRodney W. Grimes 919df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 920df8bae1dSRodney W. Grimes 921df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 922df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 9230d94caffSDavid Greenman } else 924df8bae1dSRodney W. Grimes entry = entry->next; 925df8bae1dSRodney W. Grimes 926df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 927df8bae1dSRodney W. Grimes 928df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 929afa07f7eSJohn Dyson ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_COW)) == 0) && 930afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 9312d8acc0fSJohn Dyson entry->object.sub_map = submap; 932afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 933df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 934df8bae1dSRodney W. Grimes } 935df8bae1dSRodney W. Grimes vm_map_unlock(map); 936df8bae1dSRodney W. Grimes 937df8bae1dSRodney W. Grimes return (result); 938df8bae1dSRodney W. Grimes } 939df8bae1dSRodney W. Grimes 940df8bae1dSRodney W. Grimes /* 941df8bae1dSRodney W. Grimes * vm_map_protect: 942df8bae1dSRodney W. Grimes * 943df8bae1dSRodney W. Grimes * Sets the protection of the specified address 944df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 945df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 946df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 947df8bae1dSRodney W. Grimes */ 948df8bae1dSRodney W. Grimes int 949b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 950b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 951df8bae1dSRodney W. Grimes { 952c0877f10SJohn Dyson vm_map_entry_t current; 953df8bae1dSRodney W. Grimes vm_map_entry_t entry; 954df8bae1dSRodney W. Grimes 955df8bae1dSRodney W. Grimes vm_map_lock(map); 956df8bae1dSRodney W. Grimes 957df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 958df8bae1dSRodney W. Grimes 959df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 960df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 961b7b2aac2SJohn Dyson } else { 962df8bae1dSRodney W. Grimes entry = entry->next; 963b7b2aac2SJohn Dyson } 964df8bae1dSRodney W. Grimes 965df8bae1dSRodney W. Grimes /* 9660d94caffSDavid Greenman * Make a first pass to check for protection violations. 967df8bae1dSRodney W. Grimes */ 968df8bae1dSRodney W. Grimes 969df8bae1dSRodney W. Grimes current = entry; 970df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 971afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 972a1f6d91cSDavid Greenman vm_map_unlock(map); 973df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 974a1f6d91cSDavid Greenman } 975df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 976df8bae1dSRodney W. Grimes vm_map_unlock(map); 977df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 978df8bae1dSRodney W. Grimes } 979df8bae1dSRodney W. Grimes current = current->next; 980df8bae1dSRodney W. Grimes } 981df8bae1dSRodney W. Grimes 982df8bae1dSRodney W. Grimes /* 9830d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 9840d94caffSDavid Greenman * necessary the second time.] 985df8bae1dSRodney W. Grimes */ 986df8bae1dSRodney W. Grimes 987df8bae1dSRodney W. Grimes current = entry; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 990df8bae1dSRodney W. Grimes vm_prot_t old_prot; 991df8bae1dSRodney W. Grimes 992df8bae1dSRodney W. Grimes vm_map_clip_end(map, current, end); 993df8bae1dSRodney W. Grimes 994df8bae1dSRodney W. Grimes old_prot = current->protection; 995df8bae1dSRodney W. Grimes if (set_max) 996df8bae1dSRodney W. Grimes current->protection = 997df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 998df8bae1dSRodney W. Grimes old_prot; 999df8bae1dSRodney W. Grimes else 1000df8bae1dSRodney W. Grimes current->protection = new_prot; 1001df8bae1dSRodney W. Grimes 1002df8bae1dSRodney W. Grimes /* 10030d94caffSDavid Greenman * Update physical map if necessary. Worry about copy-on-write 10040d94caffSDavid Greenman * here -- CHECK THIS XXX 1005df8bae1dSRodney W. Grimes */ 1006df8bae1dSRodney W. Grimes 1007df8bae1dSRodney W. Grimes if (current->protection != old_prot) { 1008afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1009df8bae1dSRodney W. Grimes VM_PROT_ALL) 1010df8bae1dSRodney W. Grimes 1011afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_A_MAP) { 1012df8bae1dSRodney W. Grimes vm_map_entry_t share_entry; 1013df8bae1dSRodney W. Grimes vm_offset_t share_end; 1014df8bae1dSRodney W. Grimes 1015df8bae1dSRodney W. Grimes vm_map_lock(current->object.share_map); 1016df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry( 1017df8bae1dSRodney W. Grimes current->object.share_map, 1018df8bae1dSRodney W. Grimes current->offset, 1019df8bae1dSRodney W. Grimes &share_entry); 1020df8bae1dSRodney W. Grimes share_end = current->offset + 1021df8bae1dSRodney W. Grimes (current->end - current->start); 1022df8bae1dSRodney W. Grimes while ((share_entry != 1023df8bae1dSRodney W. Grimes ¤t->object.share_map->header) && 1024df8bae1dSRodney W. Grimes (share_entry->start < share_end)) { 1025df8bae1dSRodney W. Grimes 1026df8bae1dSRodney W. Grimes pmap_protect(map->pmap, 10275270eceaSBruce Evans (qmax(share_entry->start, 1028df8bae1dSRodney W. Grimes current->offset) - 1029df8bae1dSRodney W. Grimes current->offset + 1030df8bae1dSRodney W. Grimes current->start), 1031df8bae1dSRodney W. Grimes min(share_entry->end, 1032df8bae1dSRodney W. Grimes share_end) - 1033df8bae1dSRodney W. Grimes current->offset + 1034df8bae1dSRodney W. Grimes current->start, 1035df8bae1dSRodney W. Grimes current->protection & 1036df8bae1dSRodney W. Grimes MASK(share_entry)); 1037df8bae1dSRodney W. Grimes 1038df8bae1dSRodney W. Grimes share_entry = share_entry->next; 1039df8bae1dSRodney W. Grimes } 1040df8bae1dSRodney W. Grimes vm_map_unlock(current->object.share_map); 10410d94caffSDavid Greenman } else 1042df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 1043df8bae1dSRodney W. Grimes current->end, 1044df8bae1dSRodney W. Grimes current->protection & MASK(entry)); 1045df8bae1dSRodney W. Grimes #undef MASK 1046df8bae1dSRodney W. Grimes } 10477d78abc9SJohn Dyson 10487d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 10497d78abc9SJohn Dyson 1050df8bae1dSRodney W. Grimes current = current->next; 1051df8bae1dSRodney W. Grimes } 1052df8bae1dSRodney W. Grimes 10532d8acc0fSJohn Dyson map->timestamp++; 1054df8bae1dSRodney W. Grimes vm_map_unlock(map); 1055df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1056df8bae1dSRodney W. Grimes } 1057df8bae1dSRodney W. Grimes 1058df8bae1dSRodney W. Grimes /* 1059867a482dSJohn Dyson * vm_map_madvise: 1060867a482dSJohn Dyson * 1061867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 1062867a482dSJohn Dyson * system call. 1063867a482dSJohn Dyson */ 1064867a482dSJohn Dyson void 1065867a482dSJohn Dyson vm_map_madvise(map, pmap, start, end, advise) 1066867a482dSJohn Dyson vm_map_t map; 1067867a482dSJohn Dyson pmap_t pmap; 1068867a482dSJohn Dyson vm_offset_t start, end; 1069867a482dSJohn Dyson int advise; 1070867a482dSJohn Dyson { 1071c0877f10SJohn Dyson vm_map_entry_t current; 1072867a482dSJohn Dyson vm_map_entry_t entry; 1073867a482dSJohn Dyson 1074867a482dSJohn Dyson vm_map_lock(map); 1075867a482dSJohn Dyson 1076867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 1077867a482dSJohn Dyson 1078867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 1079867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 1080867a482dSJohn Dyson } else 1081867a482dSJohn Dyson entry = entry->next; 1082867a482dSJohn Dyson 1083867a482dSJohn Dyson for(current = entry; 1084867a482dSJohn Dyson (current != &map->header) && (current->start < end); 1085867a482dSJohn Dyson current = current->next) { 108647221757SJohn Dyson vm_size_t size; 1087fed9a903SJohn Dyson 1088afa07f7eSJohn Dyson if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) { 1089867a482dSJohn Dyson continue; 1090867a482dSJohn Dyson } 1091fed9a903SJohn Dyson 109247221757SJohn Dyson vm_map_clip_end(map, current, end); 109347221757SJohn Dyson size = current->end - current->start; 109447221757SJohn Dyson 1095fed9a903SJohn Dyson /* 1096fed9a903SJohn Dyson * Create an object if needed 1097fed9a903SJohn Dyson */ 1098fed9a903SJohn Dyson if (current->object.vm_object == NULL) { 1099fed9a903SJohn Dyson vm_object_t object; 110047221757SJohn Dyson if ((advise == MADV_FREE) || (advise == MADV_DONTNEED)) 110147221757SJohn Dyson continue; 1102fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, OFF_TO_IDX(size)); 1103fed9a903SJohn Dyson current->object.vm_object = object; 1104fed9a903SJohn Dyson current->offset = 0; 1105fed9a903SJohn Dyson } 1106fed9a903SJohn Dyson 1107867a482dSJohn Dyson switch (advise) { 1108867a482dSJohn Dyson case MADV_NORMAL: 1109867a482dSJohn Dyson current->object.vm_object->behavior = OBJ_NORMAL; 1110867a482dSJohn Dyson break; 1111867a482dSJohn Dyson case MADV_SEQUENTIAL: 1112867a482dSJohn Dyson current->object.vm_object->behavior = OBJ_SEQUENTIAL; 1113867a482dSJohn Dyson break; 1114867a482dSJohn Dyson case MADV_RANDOM: 1115867a482dSJohn Dyson current->object.vm_object->behavior = OBJ_RANDOM; 1116867a482dSJohn Dyson break; 1117867a482dSJohn Dyson /* 1118867a482dSJohn Dyson * Right now, we could handle DONTNEED and WILLNEED with common code. 1119867a482dSJohn Dyson * They are mostly the same, except for the potential async reads (NYI). 1120867a482dSJohn Dyson */ 11210a47b48bSJohn Dyson case MADV_FREE: 1122867a482dSJohn Dyson case MADV_DONTNEED: 1123867a482dSJohn Dyson { 1124867a482dSJohn Dyson vm_pindex_t pindex; 1125867a482dSJohn Dyson int count; 112647221757SJohn Dyson pindex = OFF_TO_IDX(current->offset); 1127867a482dSJohn Dyson count = OFF_TO_IDX(size); 1128867a482dSJohn Dyson /* 1129867a482dSJohn Dyson * MADV_DONTNEED removes the page from all 1130867a482dSJohn Dyson * pmaps, so pmap_remove is not necessary. 1131867a482dSJohn Dyson */ 1132867a482dSJohn Dyson vm_object_madvise(current->object.vm_object, 1133867a482dSJohn Dyson pindex, count, advise); 1134867a482dSJohn Dyson } 1135867a482dSJohn Dyson break; 1136867a482dSJohn Dyson 1137867a482dSJohn Dyson case MADV_WILLNEED: 1138867a482dSJohn Dyson { 1139867a482dSJohn Dyson vm_pindex_t pindex; 1140867a482dSJohn Dyson int count; 11414334b0d8SJohn Dyson pindex = OFF_TO_IDX(current->offset); 1142867a482dSJohn Dyson count = OFF_TO_IDX(size); 1143867a482dSJohn Dyson vm_object_madvise(current->object.vm_object, 1144867a482dSJohn Dyson pindex, count, advise); 1145867a482dSJohn Dyson pmap_object_init_pt(pmap, current->start, 1146867a482dSJohn Dyson current->object.vm_object, pindex, 1147867a482dSJohn Dyson (count << PAGE_SHIFT), 0); 1148867a482dSJohn Dyson } 1149867a482dSJohn Dyson break; 1150867a482dSJohn Dyson 1151867a482dSJohn Dyson default: 1152867a482dSJohn Dyson break; 1153867a482dSJohn Dyson } 1154867a482dSJohn Dyson } 1155867a482dSJohn Dyson 115647221757SJohn Dyson map->timestamp++; 1157867a482dSJohn Dyson vm_map_simplify_entry(map, entry); 1158867a482dSJohn Dyson vm_map_unlock(map); 1159867a482dSJohn Dyson return; 1160867a482dSJohn Dyson } 1161867a482dSJohn Dyson 1162867a482dSJohn Dyson 1163867a482dSJohn Dyson /* 1164df8bae1dSRodney W. Grimes * vm_map_inherit: 1165df8bae1dSRodney W. Grimes * 1166df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 1167df8bae1dSRodney W. Grimes * range in the target map. Inheritance 1168df8bae1dSRodney W. Grimes * affects how the map will be shared with 1169df8bae1dSRodney W. Grimes * child maps at the time of vm_map_fork. 1170df8bae1dSRodney W. Grimes */ 1171df8bae1dSRodney W. Grimes int 1172b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1173b9dcd593SBruce Evans vm_inherit_t new_inheritance) 1174df8bae1dSRodney W. Grimes { 1175c0877f10SJohn Dyson vm_map_entry_t entry; 1176df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 1177df8bae1dSRodney W. Grimes 1178df8bae1dSRodney W. Grimes switch (new_inheritance) { 1179df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 1180df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 1181df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 1182df8bae1dSRodney W. Grimes break; 1183df8bae1dSRodney W. Grimes default: 1184df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1185df8bae1dSRodney W. Grimes } 1186df8bae1dSRodney W. Grimes 1187df8bae1dSRodney W. Grimes vm_map_lock(map); 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1190df8bae1dSRodney W. Grimes 1191df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 1192df8bae1dSRodney W. Grimes entry = temp_entry; 1193df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 11940d94caffSDavid Greenman } else 1195df8bae1dSRodney W. Grimes entry = temp_entry->next; 1196df8bae1dSRodney W. Grimes 1197df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1198df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1199df8bae1dSRodney W. Grimes 1200df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 1201df8bae1dSRodney W. Grimes 1202df8bae1dSRodney W. Grimes entry = entry->next; 1203df8bae1dSRodney W. Grimes } 1204df8bae1dSRodney W. Grimes 1205f32dbbeeSJohn Dyson vm_map_simplify_entry(map, temp_entry); 120647221757SJohn Dyson map->timestamp++; 1207df8bae1dSRodney W. Grimes vm_map_unlock(map); 1208df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1209df8bae1dSRodney W. Grimes } 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes /* 12127aaaa4fdSJohn Dyson * Implement the semantics of mlock 12137aaaa4fdSJohn Dyson */ 12147aaaa4fdSJohn Dyson int 12157aaaa4fdSJohn Dyson vm_map_user_pageable(map, start, end, new_pageable) 1216c0877f10SJohn Dyson vm_map_t map; 1217c0877f10SJohn Dyson vm_offset_t start; 1218c0877f10SJohn Dyson vm_offset_t end; 1219c0877f10SJohn Dyson boolean_t new_pageable; 12207aaaa4fdSJohn Dyson { 1221b44959ceSTor Egge vm_map_entry_t entry; 12227aaaa4fdSJohn Dyson vm_map_entry_t start_entry; 1223b44959ceSTor Egge vm_offset_t estart; 12247aaaa4fdSJohn Dyson int rv; 12257aaaa4fdSJohn Dyson 12267aaaa4fdSJohn Dyson vm_map_lock(map); 12277aaaa4fdSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 12287aaaa4fdSJohn Dyson 12297aaaa4fdSJohn Dyson if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 12307aaaa4fdSJohn Dyson vm_map_unlock(map); 12317aaaa4fdSJohn Dyson return (KERN_INVALID_ADDRESS); 12327aaaa4fdSJohn Dyson } 12337aaaa4fdSJohn Dyson 12347aaaa4fdSJohn Dyson if (new_pageable) { 12357aaaa4fdSJohn Dyson 12367aaaa4fdSJohn Dyson entry = start_entry; 12377aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 12387aaaa4fdSJohn Dyson 12397aaaa4fdSJohn Dyson /* 12407aaaa4fdSJohn Dyson * Now decrement the wiring count for each region. If a region 12417aaaa4fdSJohn Dyson * becomes completely unwired, unwire its physical pages and 12427aaaa4fdSJohn Dyson * mappings. 12437aaaa4fdSJohn Dyson */ 1244996c772fSJohn Dyson vm_map_set_recursive(map); 12457aaaa4fdSJohn Dyson 12467aaaa4fdSJohn Dyson entry = start_entry; 12477aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 1248afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 12497aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 1250afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 12517aaaa4fdSJohn Dyson entry->wired_count--; 12527aaaa4fdSJohn Dyson if (entry->wired_count == 0) 12537aaaa4fdSJohn Dyson vm_fault_unwire(map, entry->start, entry->end); 12547aaaa4fdSJohn Dyson } 1255b44959ceSTor Egge vm_map_simplify_entry(map,entry); 12567aaaa4fdSJohn Dyson entry = entry->next; 12577aaaa4fdSJohn Dyson } 1258996c772fSJohn Dyson vm_map_clear_recursive(map); 12597aaaa4fdSJohn Dyson } else { 12607aaaa4fdSJohn Dyson 12617aaaa4fdSJohn Dyson entry = start_entry; 12627aaaa4fdSJohn Dyson 12637aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 12647aaaa4fdSJohn Dyson 1265afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 12667aaaa4fdSJohn Dyson entry = entry->next; 12677aaaa4fdSJohn Dyson continue; 12687aaaa4fdSJohn Dyson } 12697aaaa4fdSJohn Dyson 12707aaaa4fdSJohn Dyson if (entry->wired_count != 0) { 12717aaaa4fdSJohn Dyson entry->wired_count++; 1272afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 12737aaaa4fdSJohn Dyson entry = entry->next; 12747aaaa4fdSJohn Dyson continue; 12757aaaa4fdSJohn Dyson } 12767aaaa4fdSJohn Dyson 12777aaaa4fdSJohn Dyson /* Here on entry being newly wired */ 12787aaaa4fdSJohn Dyson 1279afa07f7eSJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 1280afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 12817aaaa4fdSJohn Dyson if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 12827aaaa4fdSJohn Dyson 12837aaaa4fdSJohn Dyson vm_object_shadow(&entry->object.vm_object, 12847aaaa4fdSJohn Dyson &entry->offset, 1285c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1286afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 12877aaaa4fdSJohn Dyson 12887aaaa4fdSJohn Dyson } else if (entry->object.vm_object == NULL) { 12897aaaa4fdSJohn Dyson 12907aaaa4fdSJohn Dyson entry->object.vm_object = 12917aaaa4fdSJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1292c2e11a03SJohn Dyson atop(entry->end - entry->start)); 12937aaaa4fdSJohn Dyson entry->offset = (vm_offset_t) 0; 12947aaaa4fdSJohn Dyson 12957aaaa4fdSJohn Dyson } 12967aaaa4fdSJohn Dyson default_pager_convert_to_swapq(entry->object.vm_object); 12977aaaa4fdSJohn Dyson } 12987aaaa4fdSJohn Dyson 12997aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 13007aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 13017aaaa4fdSJohn Dyson 13027aaaa4fdSJohn Dyson entry->wired_count++; 1303afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 1304b44959ceSTor Egge estart = entry->start; 13057aaaa4fdSJohn Dyson 13067aaaa4fdSJohn Dyson /* First we need to allow map modifications */ 1307996c772fSJohn Dyson vm_map_set_recursive(map); 130803e9c6c1SJohn Dyson vm_map_lock_downgrade(map); 130947221757SJohn Dyson map->timestamp++; 13107aaaa4fdSJohn Dyson 13117aaaa4fdSJohn Dyson rv = vm_fault_user_wire(map, entry->start, entry->end); 13127aaaa4fdSJohn Dyson if (rv) { 13137aaaa4fdSJohn Dyson 13147aaaa4fdSJohn Dyson entry->wired_count--; 1315afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 13167aaaa4fdSJohn Dyson 1317996c772fSJohn Dyson vm_map_clear_recursive(map); 13187aaaa4fdSJohn Dyson vm_map_unlock(map); 13197aaaa4fdSJohn Dyson 13207aaaa4fdSJohn Dyson (void) vm_map_user_pageable(map, start, entry->start, TRUE); 13217aaaa4fdSJohn Dyson return rv; 13227aaaa4fdSJohn Dyson } 13237aaaa4fdSJohn Dyson 1324996c772fSJohn Dyson vm_map_clear_recursive(map); 1325b44959ceSTor Egge if (vm_map_lock_upgrade(map)) { 1326b44959ceSTor Egge vm_map_lock(map); 1327b44959ceSTor Egge if (vm_map_lookup_entry(map, estart, &entry) 1328b44959ceSTor Egge == FALSE) { 1329b44959ceSTor Egge vm_map_unlock(map); 1330b44959ceSTor Egge (void) vm_map_user_pageable(map, 1331b44959ceSTor Egge start, 1332b44959ceSTor Egge estart, 1333b44959ceSTor Egge TRUE); 1334b44959ceSTor Egge return (KERN_INVALID_ADDRESS); 1335b44959ceSTor Egge } 1336b44959ceSTor Egge } 1337b44959ceSTor Egge vm_map_simplify_entry(map,entry); 13387aaaa4fdSJohn Dyson } 13397aaaa4fdSJohn Dyson } 134047221757SJohn Dyson map->timestamp++; 13417aaaa4fdSJohn Dyson vm_map_unlock(map); 13427aaaa4fdSJohn Dyson return KERN_SUCCESS; 13437aaaa4fdSJohn Dyson } 13447aaaa4fdSJohn Dyson 13457aaaa4fdSJohn Dyson /* 1346df8bae1dSRodney W. Grimes * vm_map_pageable: 1347df8bae1dSRodney W. Grimes * 1348df8bae1dSRodney W. Grimes * Sets the pageability of the specified address 1349df8bae1dSRodney W. Grimes * range in the target map. Regions specified 1350df8bae1dSRodney W. Grimes * as not pageable require locked-down physical 1351df8bae1dSRodney W. Grimes * memory and physical page maps. 1352df8bae1dSRodney W. Grimes * 1353df8bae1dSRodney W. Grimes * The map must not be locked, but a reference 1354df8bae1dSRodney W. Grimes * must remain to the map throughout the call. 1355df8bae1dSRodney W. Grimes */ 1356df8bae1dSRodney W. Grimes int 1357df8bae1dSRodney W. Grimes vm_map_pageable(map, start, end, new_pageable) 1358c0877f10SJohn Dyson vm_map_t map; 1359c0877f10SJohn Dyson vm_offset_t start; 1360c0877f10SJohn Dyson vm_offset_t end; 1361c0877f10SJohn Dyson boolean_t new_pageable; 1362df8bae1dSRodney W. Grimes { 1363c0877f10SJohn Dyson vm_map_entry_t entry; 1364df8bae1dSRodney W. Grimes vm_map_entry_t start_entry; 1365c0877f10SJohn Dyson vm_offset_t failed = 0; 1366df8bae1dSRodney W. Grimes int rv; 1367df8bae1dSRodney W. Grimes 1368df8bae1dSRodney W. Grimes vm_map_lock(map); 1369df8bae1dSRodney W. Grimes 1370df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1371df8bae1dSRodney W. Grimes 1372df8bae1dSRodney W. Grimes /* 13730d94caffSDavid Greenman * Only one pageability change may take place at one time, since 13740d94caffSDavid Greenman * vm_fault assumes it will be called only once for each 13750d94caffSDavid Greenman * wiring/unwiring. Therefore, we have to make sure we're actually 13760d94caffSDavid Greenman * changing the pageability for the entire region. We do so before 13770d94caffSDavid Greenman * making any changes. 1378df8bae1dSRodney W. Grimes */ 1379df8bae1dSRodney W. Grimes 1380df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1381df8bae1dSRodney W. Grimes vm_map_unlock(map); 1382df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1383df8bae1dSRodney W. Grimes } 1384df8bae1dSRodney W. Grimes entry = start_entry; 1385df8bae1dSRodney W. Grimes 1386df8bae1dSRodney W. Grimes /* 13870d94caffSDavid Greenman * Actions are rather different for wiring and unwiring, so we have 13880d94caffSDavid Greenman * two separate cases. 1389df8bae1dSRodney W. Grimes */ 1390df8bae1dSRodney W. Grimes 1391df8bae1dSRodney W. Grimes if (new_pageable) { 1392df8bae1dSRodney W. Grimes 1393df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1394df8bae1dSRodney W. Grimes 1395df8bae1dSRodney W. Grimes /* 13960d94caffSDavid Greenman * Unwiring. First ensure that the range to be unwired is 13970d94caffSDavid Greenman * really wired down and that there are no holes. 1398df8bae1dSRodney W. Grimes */ 1399df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1400df8bae1dSRodney W. Grimes 1401df8bae1dSRodney W. Grimes if (entry->wired_count == 0 || 1402df8bae1dSRodney W. Grimes (entry->end < end && 1403df8bae1dSRodney W. Grimes (entry->next == &map->header || 1404df8bae1dSRodney W. Grimes entry->next->start > entry->end))) { 1405df8bae1dSRodney W. Grimes vm_map_unlock(map); 1406df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1407df8bae1dSRodney W. Grimes } 1408df8bae1dSRodney W. Grimes entry = entry->next; 1409df8bae1dSRodney W. Grimes } 1410df8bae1dSRodney W. Grimes 1411df8bae1dSRodney W. Grimes /* 14120d94caffSDavid Greenman * Now decrement the wiring count for each region. If a region 14130d94caffSDavid Greenman * becomes completely unwired, unwire its physical pages and 14140d94caffSDavid Greenman * mappings. 1415df8bae1dSRodney W. Grimes */ 1416996c772fSJohn Dyson vm_map_set_recursive(map); 1417df8bae1dSRodney W. Grimes 1418df8bae1dSRodney W. Grimes entry = start_entry; 1419df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1420df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1421df8bae1dSRodney W. Grimes 1422df8bae1dSRodney W. Grimes entry->wired_count--; 1423df8bae1dSRodney W. Grimes if (entry->wired_count == 0) 1424df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 1425df8bae1dSRodney W. Grimes 1426df8bae1dSRodney W. Grimes entry = entry->next; 1427df8bae1dSRodney W. Grimes } 1428f32dbbeeSJohn Dyson vm_map_simplify_entry(map, start_entry); 1429996c772fSJohn Dyson vm_map_clear_recursive(map); 14300d94caffSDavid Greenman } else { 1431df8bae1dSRodney W. Grimes /* 1432df8bae1dSRodney W. Grimes * Wiring. We must do this in two passes: 1433df8bae1dSRodney W. Grimes * 14340d94caffSDavid Greenman * 1. Holding the write lock, we create any shadow or zero-fill 14350d94caffSDavid Greenman * objects that need to be created. Then we clip each map 14360d94caffSDavid Greenman * entry to the region to be wired and increment its wiring 14370d94caffSDavid Greenman * count. We create objects before clipping the map entries 1438df8bae1dSRodney W. Grimes * to avoid object proliferation. 1439df8bae1dSRodney W. Grimes * 14400d94caffSDavid Greenman * 2. We downgrade to a read lock, and call vm_fault_wire to 14410d94caffSDavid Greenman * fault in the pages for any newly wired area (wired_count is 14420d94caffSDavid Greenman * 1). 1443df8bae1dSRodney W. Grimes * 14440d94caffSDavid Greenman * Downgrading to a read lock for vm_fault_wire avoids a possible 144524a1cce3SDavid Greenman * deadlock with another process that may have faulted on one 14460d94caffSDavid Greenman * of the pages to be wired (it would mark the page busy, 14470d94caffSDavid Greenman * blocking us, then in turn block on the map lock that we 14480d94caffSDavid Greenman * hold). Because of problems in the recursive lock package, 14490d94caffSDavid Greenman * we cannot upgrade to a write lock in vm_map_lookup. Thus, 14500d94caffSDavid Greenman * any actions that require the write lock must be done 14510d94caffSDavid Greenman * beforehand. Because we keep the read lock on the map, the 14520d94caffSDavid Greenman * copy-on-write status of the entries we modify here cannot 14530d94caffSDavid Greenman * change. 1454df8bae1dSRodney W. Grimes */ 1455df8bae1dSRodney W. Grimes 1456df8bae1dSRodney W. Grimes /* 1457df8bae1dSRodney W. Grimes * Pass 1. 1458df8bae1dSRodney W. Grimes */ 1459df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1460df8bae1dSRodney W. Grimes if (entry->wired_count == 0) { 1461df8bae1dSRodney W. Grimes 1462df8bae1dSRodney W. Grimes /* 1463df8bae1dSRodney W. Grimes * Perform actions of vm_map_lookup that need 1464df8bae1dSRodney W. Grimes * the write lock on the map: create a shadow 1465df8bae1dSRodney W. Grimes * object for a copy-on-write region, or an 1466df8bae1dSRodney W. Grimes * object for a zero-fill region. 1467df8bae1dSRodney W. Grimes * 1468df8bae1dSRodney W. Grimes * We don't have to do this for entries that 14690d94caffSDavid Greenman * point to sharing maps, because we won't 14700d94caffSDavid Greenman * hold the lock on the sharing map. 1471df8bae1dSRodney W. Grimes */ 1472afa07f7eSJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 1473afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1474b5b40fa6SJohn Dyson if (copyflag && 1475df8bae1dSRodney W. Grimes ((entry->protection & VM_PROT_WRITE) != 0)) { 1476df8bae1dSRodney W. Grimes 1477df8bae1dSRodney W. Grimes vm_object_shadow(&entry->object.vm_object, 1478df8bae1dSRodney W. Grimes &entry->offset, 1479c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1480afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 14810d94caffSDavid Greenman } else if (entry->object.vm_object == NULL) { 1482df8bae1dSRodney W. Grimes entry->object.vm_object = 1483a316d390SJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1484c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1485df8bae1dSRodney W. Grimes entry->offset = (vm_offset_t) 0; 1486df8bae1dSRodney W. Grimes } 1487b5b40fa6SJohn Dyson default_pager_convert_to_swapq(entry->object.vm_object); 1488df8bae1dSRodney W. Grimes } 1489df8bae1dSRodney W. Grimes } 1490df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1491df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1492df8bae1dSRodney W. Grimes entry->wired_count++; 1493df8bae1dSRodney W. Grimes 1494df8bae1dSRodney W. Grimes /* 1495df8bae1dSRodney W. Grimes * Check for holes 1496df8bae1dSRodney W. Grimes */ 1497df8bae1dSRodney W. Grimes if (entry->end < end && 1498df8bae1dSRodney W. Grimes (entry->next == &map->header || 1499df8bae1dSRodney W. Grimes entry->next->start > entry->end)) { 1500df8bae1dSRodney W. Grimes /* 15010d94caffSDavid Greenman * Found one. Object creation actions do not 15020d94caffSDavid Greenman * need to be undone, but the wired counts 15030d94caffSDavid Greenman * need to be restored. 1504df8bae1dSRodney W. Grimes */ 1505df8bae1dSRodney W. Grimes while (entry != &map->header && entry->end > start) { 1506df8bae1dSRodney W. Grimes entry->wired_count--; 1507df8bae1dSRodney W. Grimes entry = entry->prev; 1508df8bae1dSRodney W. Grimes } 150947221757SJohn Dyson map->timestamp++; 1510df8bae1dSRodney W. Grimes vm_map_unlock(map); 1511df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1512df8bae1dSRodney W. Grimes } 1513df8bae1dSRodney W. Grimes entry = entry->next; 1514df8bae1dSRodney W. Grimes } 1515df8bae1dSRodney W. Grimes 1516df8bae1dSRodney W. Grimes /* 1517df8bae1dSRodney W. Grimes * Pass 2. 1518df8bae1dSRodney W. Grimes */ 1519df8bae1dSRodney W. Grimes 1520df8bae1dSRodney W. Grimes /* 1521df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1522df8bae1dSRodney W. Grimes * 152324a1cce3SDavid Greenman * If we are wiring in the kernel map or a submap of it, 152424a1cce3SDavid Greenman * unlock the map to avoid deadlocks. We trust that the 152524a1cce3SDavid Greenman * kernel is well-behaved, and therefore will not do 152624a1cce3SDavid Greenman * anything destructive to this region of the map while 152724a1cce3SDavid Greenman * we have it unlocked. We cannot trust user processes 152824a1cce3SDavid Greenman * to do the same. 1529df8bae1dSRodney W. Grimes * 1530df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1531df8bae1dSRodney W. Grimes */ 1532df8bae1dSRodney W. Grimes if (vm_map_pmap(map) == kernel_pmap) { 1533df8bae1dSRodney W. Grimes vm_map_unlock(map); /* trust me ... */ 15340d94caffSDavid Greenman } else { 1535996c772fSJohn Dyson vm_map_set_recursive(map); 153603e9c6c1SJohn Dyson vm_map_lock_downgrade(map); 1537df8bae1dSRodney W. Grimes } 1538df8bae1dSRodney W. Grimes 1539df8bae1dSRodney W. Grimes rv = 0; 1540df8bae1dSRodney W. Grimes entry = start_entry; 1541df8bae1dSRodney W. Grimes while (entry != &map->header && entry->start < end) { 1542df8bae1dSRodney W. Grimes /* 15430d94caffSDavid Greenman * If vm_fault_wire fails for any page we need to undo 15440d94caffSDavid Greenman * what has been done. We decrement the wiring count 15450d94caffSDavid Greenman * for those pages which have not yet been wired (now) 15460d94caffSDavid Greenman * and unwire those that have (later). 1547df8bae1dSRodney W. Grimes * 1548df8bae1dSRodney W. Grimes * XXX this violates the locking protocol on the map, 1549df8bae1dSRodney W. Grimes * needs to be fixed. 1550df8bae1dSRodney W. Grimes */ 1551df8bae1dSRodney W. Grimes if (rv) 1552df8bae1dSRodney W. Grimes entry->wired_count--; 1553df8bae1dSRodney W. Grimes else if (entry->wired_count == 1) { 1554df8bae1dSRodney W. Grimes rv = vm_fault_wire(map, entry->start, entry->end); 1555df8bae1dSRodney W. Grimes if (rv) { 1556df8bae1dSRodney W. Grimes failed = entry->start; 1557df8bae1dSRodney W. Grimes entry->wired_count--; 1558df8bae1dSRodney W. Grimes } 1559df8bae1dSRodney W. Grimes } 1560df8bae1dSRodney W. Grimes entry = entry->next; 1561df8bae1dSRodney W. Grimes } 1562df8bae1dSRodney W. Grimes 1563df8bae1dSRodney W. Grimes if (vm_map_pmap(map) == kernel_pmap) { 1564df8bae1dSRodney W. Grimes vm_map_lock(map); 15650d94caffSDavid Greenman } else { 1566996c772fSJohn Dyson vm_map_clear_recursive(map); 1567df8bae1dSRodney W. Grimes } 1568df8bae1dSRodney W. Grimes if (rv) { 1569df8bae1dSRodney W. Grimes vm_map_unlock(map); 1570df8bae1dSRodney W. Grimes (void) vm_map_pageable(map, start, failed, TRUE); 1571df8bae1dSRodney W. Grimes return (rv); 1572df8bae1dSRodney W. Grimes } 1573b7b2aac2SJohn Dyson vm_map_simplify_entry(map, start_entry); 1574df8bae1dSRodney W. Grimes } 1575df8bae1dSRodney W. Grimes 1576df8bae1dSRodney W. Grimes vm_map_unlock(map); 1577df8bae1dSRodney W. Grimes 157847221757SJohn Dyson map->timestamp++; 1579df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1580df8bae1dSRodney W. Grimes } 1581df8bae1dSRodney W. Grimes 1582df8bae1dSRodney W. Grimes /* 1583df8bae1dSRodney W. Grimes * vm_map_clean 1584df8bae1dSRodney W. Grimes * 1585df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 1586df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 1587df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 1588df8bae1dSRodney W. Grimes * 1589df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 1590df8bae1dSRodney W. Grimes */ 1591df8bae1dSRodney W. Grimes int 1592df8bae1dSRodney W. Grimes vm_map_clean(map, start, end, syncio, invalidate) 1593df8bae1dSRodney W. Grimes vm_map_t map; 1594df8bae1dSRodney W. Grimes vm_offset_t start; 1595df8bae1dSRodney W. Grimes vm_offset_t end; 1596df8bae1dSRodney W. Grimes boolean_t syncio; 1597df8bae1dSRodney W. Grimes boolean_t invalidate; 1598df8bae1dSRodney W. Grimes { 1599c0877f10SJohn Dyson vm_map_entry_t current; 1600df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1601df8bae1dSRodney W. Grimes vm_size_t size; 1602df8bae1dSRodney W. Grimes vm_object_t object; 1603a316d390SJohn Dyson vm_ooffset_t offset; 1604df8bae1dSRodney W. Grimes 1605df8bae1dSRodney W. Grimes vm_map_lock_read(map); 1606df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1607df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 1608df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1609df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1610df8bae1dSRodney W. Grimes } 1611df8bae1dSRodney W. Grimes /* 1612df8bae1dSRodney W. Grimes * Make a first pass to check for holes. 1613df8bae1dSRodney W. Grimes */ 1614df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1615afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1616df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1617df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1618df8bae1dSRodney W. Grimes } 1619df8bae1dSRodney W. Grimes if (end > current->end && 1620df8bae1dSRodney W. Grimes (current->next == &map->header || 1621df8bae1dSRodney W. Grimes current->end != current->next->start)) { 1622df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1623df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1624df8bae1dSRodney W. Grimes } 1625df8bae1dSRodney W. Grimes } 1626df8bae1dSRodney W. Grimes 1627df8bae1dSRodney W. Grimes /* 1628df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 1629df8bae1dSRodney W. Grimes * objects as we go. 1630df8bae1dSRodney W. Grimes */ 1631df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1632df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 1633df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 1634afa07f7eSJohn Dyson if (current->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) { 1635c0877f10SJohn Dyson vm_map_t smap; 1636df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 1637df8bae1dSRodney W. Grimes vm_size_t tsize; 1638df8bae1dSRodney W. Grimes 1639df8bae1dSRodney W. Grimes smap = current->object.share_map; 1640df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 1641df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 1642df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 1643df8bae1dSRodney W. Grimes if (tsize < size) 1644df8bae1dSRodney W. Grimes size = tsize; 1645df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 1646df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 1647df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 1648df8bae1dSRodney W. Grimes } else { 1649df8bae1dSRodney W. Grimes object = current->object.vm_object; 1650df8bae1dSRodney W. Grimes } 16518a02c104SJohn Dyson /* 16528a02c104SJohn Dyson * Note that there is absolutely no sense in writing out 16538a02c104SJohn Dyson * anonymous objects, so we track down the vnode object 16548a02c104SJohn Dyson * to write out. 16558a02c104SJohn Dyson * We invalidate (remove) all pages from the address space 16568a02c104SJohn Dyson * anyway, for semantic correctness. 16578a02c104SJohn Dyson */ 16588a02c104SJohn Dyson while (object->backing_object) { 16598a02c104SJohn Dyson object = object->backing_object; 16608a02c104SJohn Dyson offset += object->backing_object_offset; 16618a02c104SJohn Dyson if (object->size < OFF_TO_IDX( offset + size)) 16628a02c104SJohn Dyson size = IDX_TO_OFF(object->size) - offset; 16638a02c104SJohn Dyson } 16648a02c104SJohn Dyson if (invalidate) 16658a02c104SJohn Dyson pmap_remove(vm_map_pmap(map), current->start, 166667cc64f4SJohn Dyson current->start + size); 166724a1cce3SDavid Greenman if (object && (object->type == OBJT_VNODE)) { 1668df8bae1dSRodney W. Grimes /* 16690d94caffSDavid Greenman * Flush pages if writing is allowed. XXX should we continue 16700d94caffSDavid Greenman * on an error? 1671f5cf85d4SDavid Greenman * 1672f5cf85d4SDavid Greenman * XXX Doing async I/O and then removing all the pages from 1673f5cf85d4SDavid Greenman * the object before it completes is probably a very bad 1674f5cf85d4SDavid Greenman * idea. 1675df8bae1dSRodney W. Grimes */ 1676a02051c3SJohn Dyson if (current->protection & VM_PROT_WRITE) { 16778f9110f6SJohn Dyson int flags; 16782be70f79SJohn Dyson if (object->type == OBJT_VNODE) 1679157ac55fSJohn Dyson vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc); 16808f9110f6SJohn Dyson flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 16818f9110f6SJohn Dyson flags |= invalidate ? OBJPC_INVAL : 0; 1682a316d390SJohn Dyson vm_object_page_clean(object, 1683a316d390SJohn Dyson OFF_TO_IDX(offset), 16842be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 16858f9110f6SJohn Dyson flags); 1686df8bae1dSRodney W. Grimes if (invalidate) 1687a316d390SJohn Dyson vm_object_page_remove(object, 1688a316d390SJohn Dyson OFF_TO_IDX(offset), 16892be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 1690a316d390SJohn Dyson FALSE); 16912be70f79SJohn Dyson if (object->type == OBJT_VNODE) 16922be70f79SJohn Dyson VOP_UNLOCK(object->handle, 0, curproc); 1693bf4bd9bdSDavid Greenman } 1694a02051c3SJohn Dyson } 1695df8bae1dSRodney W. Grimes start += size; 1696df8bae1dSRodney W. Grimes } 1697df8bae1dSRodney W. Grimes 1698df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1699df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1700df8bae1dSRodney W. Grimes } 1701df8bae1dSRodney W. Grimes 1702df8bae1dSRodney W. Grimes /* 1703df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 1704df8bae1dSRodney W. Grimes * 1705df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 1706df8bae1dSRodney W. Grimes * 1707df8bae1dSRodney W. Grimes * The map in question should be locked. 1708df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 1709df8bae1dSRodney W. Grimes */ 17100362d7d7SJohn Dyson static void 17110d94caffSDavid Greenman vm_map_entry_unwire(map, entry) 1712df8bae1dSRodney W. Grimes vm_map_t map; 1713c0877f10SJohn Dyson vm_map_entry_t entry; 1714df8bae1dSRodney W. Grimes { 1715df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 1716df8bae1dSRodney W. Grimes entry->wired_count = 0; 1717df8bae1dSRodney W. Grimes } 1718df8bae1dSRodney W. Grimes 1719df8bae1dSRodney W. Grimes /* 1720df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 1721df8bae1dSRodney W. Grimes * 1722df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 1723df8bae1dSRodney W. Grimes */ 17240362d7d7SJohn Dyson static void 17250d94caffSDavid Greenman vm_map_entry_delete(map, entry) 1726c0877f10SJohn Dyson vm_map_t map; 1727c0877f10SJohn Dyson vm_map_entry_t entry; 1728df8bae1dSRodney W. Grimes { 1729df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 1730df8bae1dSRodney W. Grimes map->size -= entry->end - entry->start; 1731df8bae1dSRodney W. Grimes 17322d8acc0fSJohn Dyson if ((entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) == 0) { 1733df8bae1dSRodney W. Grimes vm_object_deallocate(entry->object.vm_object); 1734b5b40fa6SJohn Dyson } 1735df8bae1dSRodney W. Grimes 1736df8bae1dSRodney W. Grimes vm_map_entry_dispose(map, entry); 1737df8bae1dSRodney W. Grimes } 1738df8bae1dSRodney W. Grimes 1739df8bae1dSRodney W. Grimes /* 1740df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 1741df8bae1dSRodney W. Grimes * 1742df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 1743df8bae1dSRodney W. Grimes * map. 1744df8bae1dSRodney W. Grimes * 1745df8bae1dSRodney W. Grimes * When called with a sharing map, removes pages from 1746df8bae1dSRodney W. Grimes * that region from all physical maps. 1747df8bae1dSRodney W. Grimes */ 1748df8bae1dSRodney W. Grimes int 1749df8bae1dSRodney W. Grimes vm_map_delete(map, start, end) 1750c0877f10SJohn Dyson vm_map_t map; 1751df8bae1dSRodney W. Grimes vm_offset_t start; 1752c0877f10SJohn Dyson vm_offset_t end; 1753df8bae1dSRodney W. Grimes { 1754c0877f10SJohn Dyson vm_map_entry_t entry; 1755df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 1756df8bae1dSRodney W. Grimes 1757df8bae1dSRodney W. Grimes /* 1758df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 1759df8bae1dSRodney W. Grimes */ 1760df8bae1dSRodney W. Grimes 17612dbea5d2SJohn Dyson if (!vm_map_lookup_entry(map, start, &first_entry)) { 1762df8bae1dSRodney W. Grimes entry = first_entry->next; 1763c0877f10SJohn Dyson if (entry->object.vm_object && 1764c0877f10SJohn Dyson (entry->object.vm_object->ref_count == 1)) 1765c0877f10SJohn Dyson entry->object.vm_object->flags |= OBJ_ONEMAPPING; 17662dbea5d2SJohn Dyson } else { 1767df8bae1dSRodney W. Grimes entry = first_entry; 1768df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1769df8bae1dSRodney W. Grimes /* 17700d94caffSDavid Greenman * Fix the lookup hint now, rather than each time though the 17710d94caffSDavid Greenman * loop. 1772df8bae1dSRodney W. Grimes */ 1773df8bae1dSRodney W. Grimes SAVE_HINT(map, entry->prev); 1774df8bae1dSRodney W. Grimes } 1775df8bae1dSRodney W. Grimes 1776df8bae1dSRodney W. Grimes /* 1777df8bae1dSRodney W. Grimes * Save the free space hint 1778df8bae1dSRodney W. Grimes */ 1779df8bae1dSRodney W. Grimes 1780b18bfc3dSJohn Dyson if (entry == &map->header) { 1781b18bfc3dSJohn Dyson map->first_free = &map->header; 17822dbea5d2SJohn Dyson } else if (map->first_free->start >= start) { 1783df8bae1dSRodney W. Grimes map->first_free = entry->prev; 17842dbea5d2SJohn Dyson } 1785df8bae1dSRodney W. Grimes 1786df8bae1dSRodney W. Grimes /* 1787df8bae1dSRodney W. Grimes * Step through all entries in this region 1788df8bae1dSRodney W. Grimes */ 1789df8bae1dSRodney W. Grimes 1790df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1791df8bae1dSRodney W. Grimes vm_map_entry_t next; 1792b18bfc3dSJohn Dyson vm_offset_t s, e; 1793c0877f10SJohn Dyson vm_object_t object; 17942dbea5d2SJohn Dyson vm_pindex_t offidxstart, offidxend; 1795b18bfc3dSJohn Dyson vm_ooffset_t offset; 1796df8bae1dSRodney W. Grimes 1797df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1798df8bae1dSRodney W. Grimes 1799c0877f10SJohn Dyson offset = entry->offset; 1800df8bae1dSRodney W. Grimes s = entry->start; 1801df8bae1dSRodney W. Grimes e = entry->end; 1802c0877f10SJohn Dyson next = entry->next; 1803c0877f10SJohn Dyson object = entry->object.vm_object; 1804df8bae1dSRodney W. Grimes 18052dbea5d2SJohn Dyson offidxstart = OFF_TO_IDX(offset); 1806c0877f10SJohn Dyson offidxend = offidxstart + OFF_TO_IDX(e - s); 18072dbea5d2SJohn Dyson 1808df8bae1dSRodney W. Grimes /* 18090d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 18100d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 1811df8bae1dSRodney W. Grimes */ 1812c0877f10SJohn Dyson if (entry->wired_count != 0) { 1813df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 1814c0877f10SJohn Dyson } 1815df8bae1dSRodney W. Grimes 1816df8bae1dSRodney W. Grimes /* 18170d94caffSDavid Greenman * If this is a sharing map, we must remove *all* references 18180d94caffSDavid Greenman * to this data, since we can't find all of the physical maps 18190d94caffSDavid Greenman * which are sharing it. 1820df8bae1dSRodney W. Grimes */ 1821df8bae1dSRodney W. Grimes 1822c0877f10SJohn Dyson if ((object == kernel_object) || (object == kmem_object)) { 18232dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1824b18bfc3dSJohn Dyson } else if (!map->is_main_map) { 18252dbea5d2SJohn Dyson vm_object_pmap_remove(object, offidxstart, offidxend); 1826b18bfc3dSJohn Dyson } else { 1827df8bae1dSRodney W. Grimes pmap_remove(map->pmap, s, e); 1828c0877f10SJohn Dyson if (object && 1829c0877f10SJohn Dyson (object->flags & OBJ_ONEMAPPING) && 1830c0877f10SJohn Dyson ((object->type == OBJT_SWAP) || (object->type == OBJT_DEFAULT))) { 18312dbea5d2SJohn Dyson vm_object_collapse(object); 18322dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 18332dbea5d2SJohn Dyson if (object->type == OBJT_SWAP) { 18342dbea5d2SJohn Dyson swap_pager_freespace(object, offidxstart, offidxend); 18352dbea5d2SJohn Dyson } 1836c0877f10SJohn Dyson if (object->size <= offidxend) { 1837c0877f10SJohn Dyson object->size = offidxstart; 1838c0877f10SJohn Dyson } 18392dbea5d2SJohn Dyson } 1840b18bfc3dSJohn Dyson } 1841df8bae1dSRodney W. Grimes 1842df8bae1dSRodney W. Grimes /* 18430d94caffSDavid Greenman * Delete the entry (which may delete the object) only after 18440d94caffSDavid Greenman * removing all pmap entries pointing to its pages. 18450d94caffSDavid Greenman * (Otherwise, its page frames may be reallocated, and any 18460d94caffSDavid Greenman * modify bits will be set in the wrong object!) 1847df8bae1dSRodney W. Grimes */ 1848df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 1849df8bae1dSRodney W. Grimes entry = next; 1850df8bae1dSRodney W. Grimes } 1851df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1852df8bae1dSRodney W. Grimes } 1853df8bae1dSRodney W. Grimes 1854df8bae1dSRodney W. Grimes /* 1855df8bae1dSRodney W. Grimes * vm_map_remove: 1856df8bae1dSRodney W. Grimes * 1857df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 1858df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 1859df8bae1dSRodney W. Grimes */ 1860df8bae1dSRodney W. Grimes int 1861df8bae1dSRodney W. Grimes vm_map_remove(map, start, end) 1862c0877f10SJohn Dyson vm_map_t map; 1863c0877f10SJohn Dyson vm_offset_t start; 1864c0877f10SJohn Dyson vm_offset_t end; 1865df8bae1dSRodney W. Grimes { 1866c0877f10SJohn Dyson int result, s = 0; 18678d6e8edeSDavid Greenman 18689579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 1869b18bfc3dSJohn Dyson s = splvm(); 1870df8bae1dSRodney W. Grimes 1871df8bae1dSRodney W. Grimes vm_map_lock(map); 1872df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1873df8bae1dSRodney W. Grimes result = vm_map_delete(map, start, end); 1874df8bae1dSRodney W. Grimes vm_map_unlock(map); 1875df8bae1dSRodney W. Grimes 18769579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 18778d6e8edeSDavid Greenman splx(s); 18788d6e8edeSDavid Greenman 1879df8bae1dSRodney W. Grimes return (result); 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes 1882df8bae1dSRodney W. Grimes /* 1883df8bae1dSRodney W. Grimes * vm_map_check_protection: 1884df8bae1dSRodney W. Grimes * 1885df8bae1dSRodney W. Grimes * Assert that the target map allows the specified 1886df8bae1dSRodney W. Grimes * privilege on the entire address region given. 1887df8bae1dSRodney W. Grimes * The entire region must be allocated. 1888df8bae1dSRodney W. Grimes */ 18890d94caffSDavid Greenman boolean_t 1890b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 1891b9dcd593SBruce Evans vm_prot_t protection) 1892df8bae1dSRodney W. Grimes { 1893c0877f10SJohn Dyson vm_map_entry_t entry; 1894df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 1895df8bae1dSRodney W. Grimes 1896df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 1897df8bae1dSRodney W. Grimes return (FALSE); 1898df8bae1dSRodney W. Grimes } 1899df8bae1dSRodney W. Grimes entry = tmp_entry; 1900df8bae1dSRodney W. Grimes 1901df8bae1dSRodney W. Grimes while (start < end) { 1902df8bae1dSRodney W. Grimes if (entry == &map->header) { 1903df8bae1dSRodney W. Grimes return (FALSE); 1904df8bae1dSRodney W. Grimes } 1905df8bae1dSRodney W. Grimes /* 1906df8bae1dSRodney W. Grimes * No holes allowed! 1907df8bae1dSRodney W. Grimes */ 1908df8bae1dSRodney W. Grimes 1909df8bae1dSRodney W. Grimes if (start < entry->start) { 1910df8bae1dSRodney W. Grimes return (FALSE); 1911df8bae1dSRodney W. Grimes } 1912df8bae1dSRodney W. Grimes /* 1913df8bae1dSRodney W. Grimes * Check protection associated with entry. 1914df8bae1dSRodney W. Grimes */ 1915df8bae1dSRodney W. Grimes 1916df8bae1dSRodney W. Grimes if ((entry->protection & protection) != protection) { 1917df8bae1dSRodney W. Grimes return (FALSE); 1918df8bae1dSRodney W. Grimes } 1919df8bae1dSRodney W. Grimes /* go to next entry */ 1920df8bae1dSRodney W. Grimes 1921df8bae1dSRodney W. Grimes start = entry->end; 1922df8bae1dSRodney W. Grimes entry = entry->next; 1923df8bae1dSRodney W. Grimes } 1924df8bae1dSRodney W. Grimes return (TRUE); 1925df8bae1dSRodney W. Grimes } 1926df8bae1dSRodney W. Grimes 192786524867SJohn Dyson /* 192886524867SJohn Dyson * Split the pages in a map entry into a new object. This affords 192986524867SJohn Dyson * easier removal of unused pages, and keeps object inheritance from 193086524867SJohn Dyson * being a negative impact on memory usage. 193186524867SJohn Dyson */ 1932c0877f10SJohn Dyson static void 1933c0877f10SJohn Dyson vm_map_split(entry) 1934c0877f10SJohn Dyson vm_map_entry_t entry; 1935c0877f10SJohn Dyson { 193686524867SJohn Dyson vm_page_t m; 1937c0877f10SJohn Dyson vm_object_t orig_object, new_object; 1938c0877f10SJohn Dyson vm_offset_t s, e; 1939c0877f10SJohn Dyson vm_pindex_t offidxstart, offidxend, idx; 1940c0877f10SJohn Dyson vm_size_t size; 1941c0877f10SJohn Dyson vm_ooffset_t offset; 1942c0877f10SJohn Dyson 1943c0877f10SJohn Dyson orig_object = entry->object.vm_object; 1944c0877f10SJohn Dyson if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1945c0877f10SJohn Dyson return; 1946c0877f10SJohn Dyson if (orig_object->ref_count <= 1) 1947c0877f10SJohn Dyson return; 1948c0877f10SJohn Dyson 1949c0877f10SJohn Dyson offset = entry->offset; 1950c0877f10SJohn Dyson s = entry->start; 1951c0877f10SJohn Dyson e = entry->end; 1952c0877f10SJohn Dyson 1953c0877f10SJohn Dyson offidxstart = OFF_TO_IDX(offset); 1954c0877f10SJohn Dyson offidxend = offidxstart + OFF_TO_IDX(e - s); 1955c0877f10SJohn Dyson size = offidxend - offidxstart; 1956c0877f10SJohn Dyson 1957c0877f10SJohn Dyson new_object = vm_pager_allocate(orig_object->type, 1958c0877f10SJohn Dyson NULL, size, VM_PROT_ALL, 0LL); 1959c0877f10SJohn Dyson if (new_object == NULL) 1960c0877f10SJohn Dyson return; 1961c0877f10SJohn Dyson 1962c0877f10SJohn Dyson for (idx = 0; idx < size; idx++) { 1963c0877f10SJohn Dyson vm_page_t m; 1964c0877f10SJohn Dyson 1965c0877f10SJohn Dyson retry: 1966c0877f10SJohn Dyson m = vm_page_lookup(orig_object, offidxstart + idx); 1967c0877f10SJohn Dyson if (m == NULL) 1968c0877f10SJohn Dyson continue; 1969c0877f10SJohn Dyson if (m->flags & PG_BUSY) { 1970c0877f10SJohn Dyson m->flags |= PG_WANTED; 1971c0877f10SJohn Dyson tsleep(m, PVM, "spltwt", 0); 1972c0877f10SJohn Dyson goto retry; 1973c0877f10SJohn Dyson } 1974c0877f10SJohn Dyson 1975dda6b171SJohn Dyson m->flags |= PG_BUSY; 1976c0877f10SJohn Dyson vm_page_protect(m, VM_PROT_NONE); 1977c0877f10SJohn Dyson vm_page_rename(m, new_object, idx); 1978e493d28aSJohn Dyson m->dirty = VM_PAGE_BITS_ALL; 197986524867SJohn Dyson m->flags |= PG_BUSY; 1980c0877f10SJohn Dyson } 1981c0877f10SJohn Dyson 1982c0877f10SJohn Dyson if (orig_object->type == OBJT_SWAP) { 1983c0877f10SJohn Dyson orig_object->paging_in_progress++; 1984c0877f10SJohn Dyson /* 1985c0877f10SJohn Dyson * copy orig_object pages into new_object 1986c0877f10SJohn Dyson * and destroy unneeded pages in 1987c0877f10SJohn Dyson * shadow object. 1988c0877f10SJohn Dyson */ 1989c0877f10SJohn Dyson swap_pager_copy(orig_object, OFF_TO_IDX(orig_object->paging_offset), 1990c0877f10SJohn Dyson new_object, OFF_TO_IDX(new_object->paging_offset), 1991c0877f10SJohn Dyson offidxstart, 0); 1992c0877f10SJohn Dyson vm_object_pip_wakeup(orig_object); 1993c0877f10SJohn Dyson } 1994c0877f10SJohn Dyson 199586524867SJohn Dyson for (idx = 0; idx < size; idx++) { 199686524867SJohn Dyson m = vm_page_lookup(new_object, idx); 199786524867SJohn Dyson if (m) { 199886524867SJohn Dyson PAGE_WAKEUP(m); 199986524867SJohn Dyson } 200086524867SJohn Dyson } 200186524867SJohn Dyson 2002c0877f10SJohn Dyson entry->object.vm_object = new_object; 2003c0877f10SJohn Dyson entry->offset = 0LL; 2004c0877f10SJohn Dyson vm_object_deallocate(orig_object); 2005c0877f10SJohn Dyson } 2006c0877f10SJohn Dyson 2007df8bae1dSRodney W. Grimes /* 2008df8bae1dSRodney W. Grimes * vm_map_copy_entry: 2009df8bae1dSRodney W. Grimes * 2010df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 2011df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 2012df8bae1dSRodney W. Grimes */ 2013f708ef1bSPoul-Henning Kamp static void 20140d94caffSDavid Greenman vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry) 2015df8bae1dSRodney W. Grimes vm_map_t src_map, dst_map; 2016c0877f10SJohn Dyson vm_map_entry_t src_entry, dst_entry; 2017df8bae1dSRodney W. Grimes { 2018c0877f10SJohn Dyson vm_object_t src_object; 2019c0877f10SJohn Dyson 2020afa07f7eSJohn Dyson if ((dst_entry->eflags|src_entry->eflags) & 2021afa07f7eSJohn Dyson (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) 2022df8bae1dSRodney W. Grimes return; 2023df8bae1dSRodney W. Grimes 2024df8bae1dSRodney W. Grimes if (src_entry->wired_count == 0) { 2025df8bae1dSRodney W. Grimes 2026df8bae1dSRodney W. Grimes /* 20270d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 20280d94caffSDavid Greenman * write-protected. 2029df8bae1dSRodney W. Grimes */ 2030afa07f7eSJohn Dyson if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2031df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 2032df8bae1dSRodney W. Grimes src_entry->start, 2033df8bae1dSRodney W. Grimes src_entry->end, 2034df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 2035df8bae1dSRodney W. Grimes } 2036b18bfc3dSJohn Dyson 2037df8bae1dSRodney W. Grimes /* 2038df8bae1dSRodney W. Grimes * Make a copy of the object. 2039df8bae1dSRodney W. Grimes */ 2040c0877f10SJohn Dyson if (src_object = src_entry->object.vm_object) { 2041c0877f10SJohn Dyson 2042c0877f10SJohn Dyson if ((src_object->handle == NULL) && 2043c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 2044c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 2045c0877f10SJohn Dyson vm_object_collapse(src_object); 2046c0877f10SJohn Dyson if (src_object->flags & OBJ_ONEMAPPING) { 2047c0877f10SJohn Dyson vm_map_split(src_entry); 2048c0877f10SJohn Dyson src_object = src_entry->object.vm_object; 2049c0877f10SJohn Dyson } 2050c0877f10SJohn Dyson } 2051c0877f10SJohn Dyson 2052c0877f10SJohn Dyson vm_object_reference(src_object); 2053c0877f10SJohn Dyson src_object->flags &= ~OBJ_ONEMAPPING; 2054c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 2055afa07f7eSJohn Dyson src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2056afa07f7eSJohn Dyson dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2057b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 2058b18bfc3dSJohn Dyson } else { 2059b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 2060b18bfc3dSJohn Dyson dst_entry->offset = 0; 2061b18bfc3dSJohn Dyson } 2062df8bae1dSRodney W. Grimes 2063df8bae1dSRodney W. Grimes pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2064df8bae1dSRodney W. Grimes dst_entry->end - dst_entry->start, src_entry->start); 20650d94caffSDavid Greenman } else { 2066df8bae1dSRodney W. Grimes /* 2067df8bae1dSRodney W. Grimes * Of course, wired down pages can't be set copy-on-write. 20680d94caffSDavid Greenman * Cause wired pages to be copied into the new map by 20690d94caffSDavid Greenman * simulating faults (the new pages are pageable) 2070df8bae1dSRodney W. Grimes */ 2071df8bae1dSRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2072df8bae1dSRodney W. Grimes } 2073df8bae1dSRodney W. Grimes } 2074df8bae1dSRodney W. Grimes 2075df8bae1dSRodney W. Grimes /* 2076df8bae1dSRodney W. Grimes * vmspace_fork: 2077df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 2078df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 2079df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 2080df8bae1dSRodney W. Grimes * values on the regions in that map. 2081df8bae1dSRodney W. Grimes * 2082df8bae1dSRodney W. Grimes * The source map must not be locked. 2083df8bae1dSRodney W. Grimes */ 2084df8bae1dSRodney W. Grimes struct vmspace * 2085df8bae1dSRodney W. Grimes vmspace_fork(vm1) 2086c0877f10SJohn Dyson struct vmspace *vm1; 2087df8bae1dSRodney W. Grimes { 2088c0877f10SJohn Dyson struct vmspace *vm2; 2089df8bae1dSRodney W. Grimes vm_map_t old_map = &vm1->vm_map; 2090df8bae1dSRodney W. Grimes vm_map_t new_map; 2091df8bae1dSRodney W. Grimes vm_map_entry_t old_entry; 2092df8bae1dSRodney W. Grimes vm_map_entry_t new_entry; 2093df8bae1dSRodney W. Grimes pmap_t new_pmap; 2094de5f6a77SJohn Dyson vm_object_t object; 2095df8bae1dSRodney W. Grimes 2096df8bae1dSRodney W. Grimes vm_map_lock(old_map); 2097df8bae1dSRodney W. Grimes 20982d8acc0fSJohn Dyson vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2099df8bae1dSRodney W. Grimes bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2100df8bae1dSRodney W. Grimes (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2101df8bae1dSRodney W. Grimes new_pmap = &vm2->vm_pmap; /* XXX */ 2102df8bae1dSRodney W. Grimes new_map = &vm2->vm_map; /* XXX */ 210347221757SJohn Dyson new_map->timestamp = 1; 2104df8bae1dSRodney W. Grimes 2105df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 2106df8bae1dSRodney W. Grimes 2107df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 2108afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2109df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 2110df8bae1dSRodney W. Grimes 2111df8bae1dSRodney W. Grimes switch (old_entry->inheritance) { 2112df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2113df8bae1dSRodney W. Grimes break; 2114df8bae1dSRodney W. Grimes 2115df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 2116df8bae1dSRodney W. Grimes /* 2117fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 2118fed9a903SJohn Dyson */ 2119fed9a903SJohn Dyson object = old_entry->object.vm_object; 2120fed9a903SJohn Dyson if (object == NULL) { 2121fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 2122c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 2123c0877f10SJohn Dyson object->flags &= ~OBJ_ONEMAPPING; 2124fed9a903SJohn Dyson old_entry->object.vm_object = object; 2125fed9a903SJohn Dyson old_entry->offset = (vm_offset_t) 0; 21265069bf57SJohn Dyson } else if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 21275069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 21285069bf57SJohn Dyson &old_entry->offset, 2129c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 21305069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 21315069bf57SJohn Dyson object = old_entry->object.vm_object; 2132fed9a903SJohn Dyson } 2133fed9a903SJohn Dyson 2134fed9a903SJohn Dyson /* 2135df8bae1dSRodney W. Grimes * Clone the entry, referencing the sharing map. 2136df8bae1dSRodney W. Grimes */ 2137df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2138df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2139df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 214095e5e988SJohn Dyson vm_object_reference(object); 2141df8bae1dSRodney W. Grimes 2142df8bae1dSRodney W. Grimes /* 21430d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 21440d94caffSDavid Greenman * inserting at the end of the new map. 2145df8bae1dSRodney W. Grimes */ 2146df8bae1dSRodney W. Grimes 2147df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2148df8bae1dSRodney W. Grimes new_entry); 2149df8bae1dSRodney W. Grimes 2150df8bae1dSRodney W. Grimes /* 2151df8bae1dSRodney W. Grimes * Update the physical map 2152df8bae1dSRodney W. Grimes */ 2153df8bae1dSRodney W. Grimes 2154df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 2155df8bae1dSRodney W. Grimes new_entry->start, 2156df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 2157df8bae1dSRodney W. Grimes old_entry->start); 2158df8bae1dSRodney W. Grimes break; 2159df8bae1dSRodney W. Grimes 2160df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2161df8bae1dSRodney W. Grimes /* 2162df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 2163df8bae1dSRodney W. Grimes */ 2164df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2165df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2166df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2167df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 2168afa07f7eSJohn Dyson new_entry->eflags &= ~MAP_ENTRY_IS_A_MAP; 2169df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2170df8bae1dSRodney W. Grimes new_entry); 2171bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 2172bd7e5f99SJohn Dyson new_entry); 2173df8bae1dSRodney W. Grimes break; 2174df8bae1dSRodney W. Grimes } 2175df8bae1dSRodney W. Grimes old_entry = old_entry->next; 2176df8bae1dSRodney W. Grimes } 2177df8bae1dSRodney W. Grimes 2178df8bae1dSRodney W. Grimes new_map->size = old_map->size; 2179df8bae1dSRodney W. Grimes vm_map_unlock(old_map); 2180df8bae1dSRodney W. Grimes 2181df8bae1dSRodney W. Grimes return (vm2); 2182df8bae1dSRodney W. Grimes } 2183df8bae1dSRodney W. Grimes 2184df8bae1dSRodney W. Grimes /* 21855856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 21865856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 21875856e12eSJohn Dyson */ 21885856e12eSJohn Dyson 21895856e12eSJohn Dyson void 21905856e12eSJohn Dyson vmspace_exec(struct proc *p) { 21915856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 21925856e12eSJohn Dyson struct vmspace *newvmspace; 21935856e12eSJohn Dyson vm_map_t map = &p->p_vmspace->vm_map; 21945856e12eSJohn Dyson 21952d8acc0fSJohn Dyson newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 21965856e12eSJohn Dyson bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 21975856e12eSJohn Dyson (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 21985856e12eSJohn Dyson /* 21995856e12eSJohn Dyson * This code is written like this for prototype purposes. The 22005856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 22015856e12eSJohn Dyson * other process's that are still using the vmspace to finally 22025856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 22035856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 22045856e12eSJohn Dyson */ 22055856e12eSJohn Dyson vmspace_free(oldvmspace); 22065856e12eSJohn Dyson p->p_vmspace = newvmspace; 22075856e12eSJohn Dyson if (p == curproc) 22085856e12eSJohn Dyson pmap_activate(p); 22095856e12eSJohn Dyson } 22105856e12eSJohn Dyson 22115856e12eSJohn Dyson /* 22125856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 22135856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 22145856e12eSJohn Dyson */ 22155856e12eSJohn Dyson 22165856e12eSJohn Dyson void 22175856e12eSJohn Dyson vmspace_unshare(struct proc *p) { 22185856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 22195856e12eSJohn Dyson struct vmspace *newvmspace; 22205856e12eSJohn Dyson 22215856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 22225856e12eSJohn Dyson return; 22235856e12eSJohn Dyson newvmspace = vmspace_fork(oldvmspace); 22245856e12eSJohn Dyson vmspace_free(oldvmspace); 22255856e12eSJohn Dyson p->p_vmspace = newvmspace; 22265856e12eSJohn Dyson if (p == curproc) 22275856e12eSJohn Dyson pmap_activate(p); 22285856e12eSJohn Dyson } 22295856e12eSJohn Dyson 22305856e12eSJohn Dyson 22315856e12eSJohn Dyson /* 2232df8bae1dSRodney W. Grimes * vm_map_lookup: 2233df8bae1dSRodney W. Grimes * 2234df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 2235df8bae1dSRodney W. Grimes * protection for a given virtual address in the 2236df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 2237df8bae1dSRodney W. Grimes * type specified. 2238df8bae1dSRodney W. Grimes * 2239df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 2240df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 2241df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 2242df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 2243df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 2244df8bae1dSRodney W. Grimes * 2245df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 2246df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 2247df8bae1dSRodney W. Grimes * 2248df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 2249df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 2250df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 2251df8bae1dSRodney W. Grimes * remain the same. 2252df8bae1dSRodney W. Grimes */ 2253df8bae1dSRodney W. Grimes int 2254b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2255b9dcd593SBruce Evans vm_offset_t vaddr, 225647221757SJohn Dyson vm_prot_t fault_typea, 2257b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 2258b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 2259b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 2260b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 22612d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 2262df8bae1dSRodney W. Grimes { 2263df8bae1dSRodney W. Grimes vm_map_t share_map; 2264df8bae1dSRodney W. Grimes vm_offset_t share_offset; 2265c0877f10SJohn Dyson vm_map_entry_t entry; 2266c0877f10SJohn Dyson vm_map_t map = *var_map; 2267c0877f10SJohn Dyson vm_prot_t prot; 2268c0877f10SJohn Dyson boolean_t su; 226947221757SJohn Dyson vm_prot_t fault_type = fault_typea; 2270df8bae1dSRodney W. Grimes 2271df8bae1dSRodney W. Grimes RetryLookup:; 2272df8bae1dSRodney W. Grimes 2273df8bae1dSRodney W. Grimes /* 2274df8bae1dSRodney W. Grimes * Lookup the faulting address. 2275df8bae1dSRodney W. Grimes */ 2276df8bae1dSRodney W. Grimes 2277df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2278df8bae1dSRodney W. Grimes 2279df8bae1dSRodney W. Grimes #define RETURN(why) \ 2280df8bae1dSRodney W. Grimes { \ 2281df8bae1dSRodney W. Grimes vm_map_unlock_read(map); \ 2282df8bae1dSRodney W. Grimes return(why); \ 2283df8bae1dSRodney W. Grimes } 2284df8bae1dSRodney W. Grimes 2285df8bae1dSRodney W. Grimes /* 22860d94caffSDavid Greenman * If the map has an interesting hint, try it before calling full 22870d94caffSDavid Greenman * blown lookup routine. 2288df8bae1dSRodney W. Grimes */ 2289df8bae1dSRodney W. Grimes 2290df8bae1dSRodney W. Grimes entry = map->hint; 2291df8bae1dSRodney W. Grimes 2292df8bae1dSRodney W. Grimes *out_entry = entry; 2293df8bae1dSRodney W. Grimes 2294df8bae1dSRodney W. Grimes if ((entry == &map->header) || 2295df8bae1dSRodney W. Grimes (vaddr < entry->start) || (vaddr >= entry->end)) { 2296df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2297df8bae1dSRodney W. Grimes 2298df8bae1dSRodney W. Grimes /* 22990d94caffSDavid Greenman * Entry was either not a valid hint, or the vaddr was not 23000d94caffSDavid Greenman * contained in the entry, so do a full lookup. 2301df8bae1dSRodney W. Grimes */ 2302df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2303df8bae1dSRodney W. Grimes RETURN(KERN_INVALID_ADDRESS); 2304df8bae1dSRodney W. Grimes 2305df8bae1dSRodney W. Grimes entry = tmp_entry; 2306df8bae1dSRodney W. Grimes *out_entry = entry; 2307df8bae1dSRodney W. Grimes } 2308b7b2aac2SJohn Dyson 2309df8bae1dSRodney W. Grimes /* 2310df8bae1dSRodney W. Grimes * Handle submaps. 2311df8bae1dSRodney W. Grimes */ 2312df8bae1dSRodney W. Grimes 2313afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2314df8bae1dSRodney W. Grimes vm_map_t old_map = map; 2315df8bae1dSRodney W. Grimes 2316df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 2317df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 2318df8bae1dSRodney W. Grimes goto RetryLookup; 2319df8bae1dSRodney W. Grimes } 2320a04c970aSJohn Dyson 2321df8bae1dSRodney W. Grimes /* 23220d94caffSDavid Greenman * Check whether this task is allowed to have this page. 2323a04c970aSJohn Dyson * Note the special case for MAP_ENTRY_COW 2324a04c970aSJohn Dyson * pages with an override. This is to implement a forced 2325a04c970aSJohn Dyson * COW for debuggers. 2326df8bae1dSRodney W. Grimes */ 2327df8bae1dSRodney W. Grimes 2328480ba2f5SJohn Dyson if (fault_type & VM_PROT_OVERRIDE_WRITE) 2329480ba2f5SJohn Dyson prot = entry->max_protection; 2330480ba2f5SJohn Dyson else 2331df8bae1dSRodney W. Grimes prot = entry->protection; 233247221757SJohn Dyson 233347221757SJohn Dyson fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 233447221757SJohn Dyson if ((fault_type & prot) != fault_type) { 233547221757SJohn Dyson RETURN(KERN_PROTECTION_FAILURE); 233647221757SJohn Dyson } 233747221757SJohn Dyson 2338480ba2f5SJohn Dyson if (entry->wired_count && (fault_type & VM_PROT_WRITE) && 233947221757SJohn Dyson (entry->eflags & MAP_ENTRY_COW) && 234047221757SJohn Dyson (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2341df8bae1dSRodney W. Grimes RETURN(KERN_PROTECTION_FAILURE); 2342a04c970aSJohn Dyson } 2343df8bae1dSRodney W. Grimes 2344df8bae1dSRodney W. Grimes /* 23450d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 23460d94caffSDavid Greenman * accesses. 2347df8bae1dSRodney W. Grimes */ 2348df8bae1dSRodney W. Grimes 234905f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 235005f0fdd2SPoul-Henning Kamp if (*wired) 2351df8bae1dSRodney W. Grimes prot = fault_type = entry->protection; 2352df8bae1dSRodney W. Grimes 2353df8bae1dSRodney W. Grimes /* 23540d94caffSDavid Greenman * If we don't already have a VM object, track it down. 2355df8bae1dSRodney W. Grimes */ 2356df8bae1dSRodney W. Grimes 2357afa07f7eSJohn Dyson su = (entry->eflags & MAP_ENTRY_IS_A_MAP) == 0; 235805f0fdd2SPoul-Henning Kamp if (su) { 2359df8bae1dSRodney W. Grimes share_map = map; 2360df8bae1dSRodney W. Grimes share_offset = vaddr; 23610d94caffSDavid Greenman } else { 2362df8bae1dSRodney W. Grimes vm_map_entry_t share_entry; 2363df8bae1dSRodney W. Grimes 2364df8bae1dSRodney W. Grimes /* 2365df8bae1dSRodney W. Grimes * Compute the sharing map, and offset into it. 2366df8bae1dSRodney W. Grimes */ 2367df8bae1dSRodney W. Grimes 2368df8bae1dSRodney W. Grimes share_map = entry->object.share_map; 2369df8bae1dSRodney W. Grimes share_offset = (vaddr - entry->start) + entry->offset; 2370df8bae1dSRodney W. Grimes 2371df8bae1dSRodney W. Grimes /* 2372df8bae1dSRodney W. Grimes * Look for the backing store object and offset 2373df8bae1dSRodney W. Grimes */ 2374df8bae1dSRodney W. Grimes 2375df8bae1dSRodney W. Grimes vm_map_lock_read(share_map); 2376df8bae1dSRodney W. Grimes 2377df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(share_map, share_offset, 2378df8bae1dSRodney W. Grimes &share_entry)) { 2379df8bae1dSRodney W. Grimes vm_map_unlock_read(share_map); 2380df8bae1dSRodney W. Grimes RETURN(KERN_INVALID_ADDRESS); 2381df8bae1dSRodney W. Grimes } 2382df8bae1dSRodney W. Grimes entry = share_entry; 2383df8bae1dSRodney W. Grimes } 2384df8bae1dSRodney W. Grimes 2385df8bae1dSRodney W. Grimes /* 2386df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 2387df8bae1dSRodney W. Grimes */ 2388df8bae1dSRodney W. Grimes 2389afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2390df8bae1dSRodney W. Grimes /* 23910d94caffSDavid Greenman * If we want to write the page, we may as well handle that 23920d94caffSDavid Greenman * now since we've got the sharing map locked. 2393df8bae1dSRodney W. Grimes * 23940d94caffSDavid Greenman * If we don't need to write the page, we just demote the 23950d94caffSDavid Greenman * permissions allowed. 2396df8bae1dSRodney W. Grimes */ 2397df8bae1dSRodney W. Grimes 2398df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 2399df8bae1dSRodney W. Grimes /* 24000d94caffSDavid Greenman * Make a new object, and place it in the object 24010d94caffSDavid Greenman * chain. Note that no new references have appeared 24020d94caffSDavid Greenman * -- one just moved from the share map to the new 24030d94caffSDavid Greenman * object. 2404df8bae1dSRodney W. Grimes */ 2405df8bae1dSRodney W. Grimes 240603e9c6c1SJohn Dyson if (vm_map_lock_upgrade(share_map)) { 2407df8bae1dSRodney W. Grimes if (share_map != map) 2408df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2409dbc806e7SJohn Dyson 2410df8bae1dSRodney W. Grimes goto RetryLookup; 2411df8bae1dSRodney W. Grimes } 2412df8bae1dSRodney W. Grimes vm_object_shadow( 2413df8bae1dSRodney W. Grimes &entry->object.vm_object, 2414df8bae1dSRodney W. Grimes &entry->offset, 2415c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2416df8bae1dSRodney W. Grimes 2417afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 241803e9c6c1SJohn Dyson vm_map_lock_downgrade(share_map); 24190d94caffSDavid Greenman } else { 2420df8bae1dSRodney W. Grimes /* 24210d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 24220d94caffSDavid Greenman * don't allow writes. 2423df8bae1dSRodney W. Grimes */ 2424df8bae1dSRodney W. Grimes 24252d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 2426df8bae1dSRodney W. Grimes } 2427df8bae1dSRodney W. Grimes } 24282d8acc0fSJohn Dyson 2429df8bae1dSRodney W. Grimes /* 2430df8bae1dSRodney W. Grimes * Create an object if necessary. 2431df8bae1dSRodney W. Grimes */ 2432df8bae1dSRodney W. Grimes if (entry->object.vm_object == NULL) { 2433df8bae1dSRodney W. Grimes 243403e9c6c1SJohn Dyson if (vm_map_lock_upgrade(share_map)) { 2435df8bae1dSRodney W. Grimes if (share_map != map) 2436df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2437df8bae1dSRodney W. Grimes goto RetryLookup; 2438df8bae1dSRodney W. Grimes } 243924a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2440c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2441df8bae1dSRodney W. Grimes entry->offset = 0; 244203e9c6c1SJohn Dyson vm_map_lock_downgrade(share_map); 2443df8bae1dSRodney W. Grimes } 2444b5b40fa6SJohn Dyson 2445925a3a41SJohn Dyson if (entry->object.vm_object->type == OBJT_DEFAULT) 2446b5b40fa6SJohn Dyson default_pager_convert_to_swapq(entry->object.vm_object); 2447df8bae1dSRodney W. Grimes /* 24480d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 24490d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 2450df8bae1dSRodney W. Grimes */ 2451df8bae1dSRodney W. Grimes 2452a316d390SJohn Dyson *pindex = OFF_TO_IDX((share_offset - entry->start) + entry->offset); 2453df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 2454df8bae1dSRodney W. Grimes 2455df8bae1dSRodney W. Grimes /* 2456df8bae1dSRodney W. Grimes * Return whether this is the only map sharing this data. 2457df8bae1dSRodney W. Grimes */ 2458df8bae1dSRodney W. Grimes 2459df8bae1dSRodney W. Grimes *out_prot = prot; 2460df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2461df8bae1dSRodney W. Grimes 2462df8bae1dSRodney W. Grimes #undef RETURN 2463df8bae1dSRodney W. Grimes } 2464df8bae1dSRodney W. Grimes 2465df8bae1dSRodney W. Grimes /* 2466df8bae1dSRodney W. Grimes * vm_map_lookup_done: 2467df8bae1dSRodney W. Grimes * 2468df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 2469df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 2470df8bae1dSRodney W. Grimes */ 2471df8bae1dSRodney W. Grimes 24720d94caffSDavid Greenman void 24730d94caffSDavid Greenman vm_map_lookup_done(map, entry) 2474c0877f10SJohn Dyson vm_map_t map; 2475df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2476df8bae1dSRodney W. Grimes { 2477df8bae1dSRodney W. Grimes /* 2478df8bae1dSRodney W. Grimes * If this entry references a map, unlock it first. 2479df8bae1dSRodney W. Grimes */ 2480df8bae1dSRodney W. Grimes 2481afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_A_MAP) 2482df8bae1dSRodney W. Grimes vm_map_unlock_read(entry->object.share_map); 2483df8bae1dSRodney W. Grimes 2484df8bae1dSRodney W. Grimes /* 2485df8bae1dSRodney W. Grimes * Unlock the main-level map 2486df8bae1dSRodney W. Grimes */ 2487df8bae1dSRodney W. Grimes 2488df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2489df8bae1dSRodney W. Grimes } 2490df8bae1dSRodney W. Grimes 24911efb74fbSJohn Dyson /* 24921efb74fbSJohn Dyson * Implement uiomove with VM operations. This handles (and collateral changes) 24931efb74fbSJohn Dyson * support every combination of source object modification, and COW type 24941efb74fbSJohn Dyson * operations. 24951efb74fbSJohn Dyson */ 24961efb74fbSJohn Dyson int 249747221757SJohn Dyson vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages) 24981efb74fbSJohn Dyson vm_map_t mapa; 24991efb74fbSJohn Dyson vm_object_t srcobject; 25001efb74fbSJohn Dyson off_t cp; 250147221757SJohn Dyson int cnta; 25021efb74fbSJohn Dyson vm_offset_t uaddra; 250395e5e988SJohn Dyson int *npages; 25041efb74fbSJohn Dyson { 25051efb74fbSJohn Dyson vm_map_t map; 250647221757SJohn Dyson vm_object_t first_object, oldobject, object; 25072d8acc0fSJohn Dyson vm_map_entry_t entry; 25081efb74fbSJohn Dyson vm_prot_t prot; 25092d8acc0fSJohn Dyson boolean_t wired; 25101efb74fbSJohn Dyson int tcnt, rv; 25112d8acc0fSJohn Dyson vm_offset_t uaddr, start, end, tend; 25121efb74fbSJohn Dyson vm_pindex_t first_pindex, osize, oindex; 25131efb74fbSJohn Dyson off_t ooffset; 251447221757SJohn Dyson int cnt; 25151efb74fbSJohn Dyson 251695e5e988SJohn Dyson if (npages) 251795e5e988SJohn Dyson *npages = 0; 251895e5e988SJohn Dyson 251947221757SJohn Dyson cnt = cnta; 25202d8acc0fSJohn Dyson uaddr = uaddra; 25212d8acc0fSJohn Dyson 25221efb74fbSJohn Dyson while (cnt > 0) { 25231efb74fbSJohn Dyson map = mapa; 25241efb74fbSJohn Dyson 25251efb74fbSJohn Dyson if ((vm_map_lookup(&map, uaddr, 25262d8acc0fSJohn Dyson VM_PROT_READ, &entry, &first_object, 25272d8acc0fSJohn Dyson &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 25281efb74fbSJohn Dyson return EFAULT; 25291efb74fbSJohn Dyson } 25301efb74fbSJohn Dyson 25312d8acc0fSJohn Dyson vm_map_clip_start(map, entry, uaddr); 25321efb74fbSJohn Dyson 25331efb74fbSJohn Dyson tcnt = cnt; 25342d8acc0fSJohn Dyson tend = uaddr + tcnt; 25352d8acc0fSJohn Dyson if (tend > entry->end) { 25362d8acc0fSJohn Dyson tcnt = entry->end - uaddr; 25372d8acc0fSJohn Dyson tend = entry->end; 25382d8acc0fSJohn Dyson } 25391efb74fbSJohn Dyson 25402d8acc0fSJohn Dyson vm_map_clip_end(map, entry, tend); 25411efb74fbSJohn Dyson 25422d8acc0fSJohn Dyson start = entry->start; 25432d8acc0fSJohn Dyson end = entry->end; 25441efb74fbSJohn Dyson 2545c2e11a03SJohn Dyson osize = atop(tcnt); 254695e5e988SJohn Dyson 2547925a3a41SJohn Dyson oindex = OFF_TO_IDX(cp); 254895e5e988SJohn Dyson if (npages) { 2549925a3a41SJohn Dyson vm_pindex_t idx; 255095e5e988SJohn Dyson for (idx = 0; idx < osize; idx++) { 255195e5e988SJohn Dyson vm_page_t m; 2552925a3a41SJohn Dyson if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 25532d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 255495e5e988SJohn Dyson return 0; 255595e5e988SJohn Dyson } 2556925a3a41SJohn Dyson if ((m->flags & PG_BUSY) || 255795e5e988SJohn Dyson ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 25582d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 255995e5e988SJohn Dyson return 0; 256095e5e988SJohn Dyson } 256195e5e988SJohn Dyson } 256295e5e988SJohn Dyson } 256395e5e988SJohn Dyson 25641efb74fbSJohn Dyson /* 25651efb74fbSJohn Dyson * If we are changing an existing map entry, just redirect 25661efb74fbSJohn Dyson * the object, and change mappings. 25671efb74fbSJohn Dyson */ 25682d8acc0fSJohn Dyson if ((first_object->type == OBJT_VNODE) && 25692d8acc0fSJohn Dyson ((oldobject = entry->object.vm_object) == first_object)) { 25702d8acc0fSJohn Dyson 25712d8acc0fSJohn Dyson if ((entry->offset != cp) || (oldobject != srcobject)) { 25722d8acc0fSJohn Dyson /* 25732d8acc0fSJohn Dyson * Remove old window into the file 25742d8acc0fSJohn Dyson */ 25752d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 25762d8acc0fSJohn Dyson 25772d8acc0fSJohn Dyson /* 25782d8acc0fSJohn Dyson * Force copy on write for mmaped regions 25792d8acc0fSJohn Dyson */ 25802d8acc0fSJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 25812d8acc0fSJohn Dyson 25822d8acc0fSJohn Dyson /* 25832d8acc0fSJohn Dyson * Point the object appropriately 25842d8acc0fSJohn Dyson */ 25852d8acc0fSJohn Dyson if (oldobject != srcobject) { 25862d8acc0fSJohn Dyson 25872d8acc0fSJohn Dyson /* 25882d8acc0fSJohn Dyson * Set the object optimization hint flag 25892d8acc0fSJohn Dyson */ 25902d8acc0fSJohn Dyson srcobject->flags |= OBJ_OPT; 25912d8acc0fSJohn Dyson vm_object_reference(srcobject); 25922d8acc0fSJohn Dyson entry->object.vm_object = srcobject; 25932d8acc0fSJohn Dyson 25942d8acc0fSJohn Dyson if (oldobject) { 25952d8acc0fSJohn Dyson vm_object_deallocate(oldobject); 25962d8acc0fSJohn Dyson } 25972d8acc0fSJohn Dyson } 25982d8acc0fSJohn Dyson 25992d8acc0fSJohn Dyson entry->offset = cp; 26002d8acc0fSJohn Dyson map->timestamp++; 26012d8acc0fSJohn Dyson } else { 26022d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 26032d8acc0fSJohn Dyson } 26042d8acc0fSJohn Dyson 26052d8acc0fSJohn Dyson } else if ((first_object->ref_count == 1) && 2606925a3a41SJohn Dyson (first_object->size == osize) && 260747221757SJohn Dyson ((first_object->type == OBJT_DEFAULT) || 260847221757SJohn Dyson (first_object->type == OBJT_SWAP)) ) { 2609925a3a41SJohn Dyson 2610925a3a41SJohn Dyson oldobject = first_object->backing_object; 2611925a3a41SJohn Dyson 2612925a3a41SJohn Dyson if ((first_object->backing_object_offset != cp) || 2613925a3a41SJohn Dyson (oldobject != srcobject)) { 2614925a3a41SJohn Dyson /* 2615925a3a41SJohn Dyson * Remove old window into the file 2616925a3a41SJohn Dyson */ 26172d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 2618925a3a41SJohn Dyson 2619925a3a41SJohn Dyson /* 262047221757SJohn Dyson * Remove unneeded old pages 262147221757SJohn Dyson */ 262247221757SJohn Dyson if (first_object->resident_page_count) { 262347221757SJohn Dyson vm_object_page_remove (first_object, 0, 0, 0); 262447221757SJohn Dyson } 262547221757SJohn Dyson 262647221757SJohn Dyson /* 262747221757SJohn Dyson * Invalidate swap space 262847221757SJohn Dyson */ 262947221757SJohn Dyson if (first_object->type == OBJT_SWAP) { 263047221757SJohn Dyson swap_pager_freespace(first_object, 263147221757SJohn Dyson OFF_TO_IDX(first_object->paging_offset), 263247221757SJohn Dyson first_object->size); 263347221757SJohn Dyson } 263447221757SJohn Dyson 263547221757SJohn Dyson /* 2636925a3a41SJohn Dyson * Force copy on write for mmaped regions 2637925a3a41SJohn Dyson */ 263847221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 26391efb74fbSJohn Dyson 26401efb74fbSJohn Dyson /* 26411efb74fbSJohn Dyson * Point the object appropriately 26421efb74fbSJohn Dyson */ 2643925a3a41SJohn Dyson if (oldobject != srcobject) { 264447221757SJohn Dyson 2645925a3a41SJohn Dyson /* 2646925a3a41SJohn Dyson * Set the object optimization hint flag 2647925a3a41SJohn Dyson */ 2648925a3a41SJohn Dyson srcobject->flags |= OBJ_OPT; 2649925a3a41SJohn Dyson vm_object_reference(srcobject); 2650925a3a41SJohn Dyson 2651925a3a41SJohn Dyson if (oldobject) { 2652925a3a41SJohn Dyson TAILQ_REMOVE(&oldobject->shadow_head, 2653925a3a41SJohn Dyson first_object, shadow_list); 2654925a3a41SJohn Dyson oldobject->shadow_count--; 2655925a3a41SJohn Dyson vm_object_deallocate(oldobject); 2656925a3a41SJohn Dyson } 2657925a3a41SJohn Dyson 2658925a3a41SJohn Dyson TAILQ_INSERT_TAIL(&srcobject->shadow_head, 2659925a3a41SJohn Dyson first_object, shadow_list); 2660925a3a41SJohn Dyson srcobject->shadow_count++; 2661925a3a41SJohn Dyson 2662925a3a41SJohn Dyson first_object->backing_object = srcobject; 2663925a3a41SJohn Dyson } 26641efb74fbSJohn Dyson first_object->backing_object_offset = cp; 26652d8acc0fSJohn Dyson map->timestamp++; 2666925a3a41SJohn Dyson } else { 26672d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 2668925a3a41SJohn Dyson } 26691efb74fbSJohn Dyson /* 26701efb74fbSJohn Dyson * Otherwise, we have to do a logical mmap. 26711efb74fbSJohn Dyson */ 26721efb74fbSJohn Dyson } else { 26731efb74fbSJohn Dyson 2674925a3a41SJohn Dyson srcobject->flags |= OBJ_OPT; 2675925a3a41SJohn Dyson vm_object_reference(srcobject); 26761efb74fbSJohn Dyson 26772d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 26781efb74fbSJohn Dyson 267947221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 268047221757SJohn Dyson vm_map_lock_upgrade(map); 26811efb74fbSJohn Dyson 26822d8acc0fSJohn Dyson if (entry == &map->header) { 26831efb74fbSJohn Dyson map->first_free = &map->header; 26841efb74fbSJohn Dyson } else if (map->first_free->start >= start) { 26852d8acc0fSJohn Dyson map->first_free = entry->prev; 26861efb74fbSJohn Dyson } 26871efb74fbSJohn Dyson 26882d8acc0fSJohn Dyson SAVE_HINT(map, entry->prev); 26892d8acc0fSJohn Dyson vm_map_entry_delete(map, entry); 26901efb74fbSJohn Dyson 26912d8acc0fSJohn Dyson object = srcobject; 26922d8acc0fSJohn Dyson ooffset = cp; 26932d8acc0fSJohn Dyson #if 0 26942d8acc0fSJohn Dyson vm_object_shadow(&object, &ooffset, osize); 26952d8acc0fSJohn Dyson #endif 26962d8acc0fSJohn Dyson 26972d8acc0fSJohn Dyson rv = vm_map_insert(map, object, ooffset, start, tend, 26982d8acc0fSJohn Dyson VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE|MAP_COPY_NEEDED); 26991efb74fbSJohn Dyson 27001efb74fbSJohn Dyson if (rv != KERN_SUCCESS) 27011efb74fbSJohn Dyson panic("vm_uiomove: could not insert new entry: %d", rv); 27021efb74fbSJohn Dyson } 27031efb74fbSJohn Dyson 27041efb74fbSJohn Dyson /* 27051efb74fbSJohn Dyson * Map the window directly, if it is already in memory 27061efb74fbSJohn Dyson */ 27072d8acc0fSJohn Dyson pmap_object_init_pt(map->pmap, uaddr, 27082d8acc0fSJohn Dyson srcobject, oindex, tcnt, 0); 27091efb74fbSJohn Dyson 271047221757SJohn Dyson map->timestamp++; 27111efb74fbSJohn Dyson vm_map_unlock(map); 27121efb74fbSJohn Dyson 27131efb74fbSJohn Dyson cnt -= tcnt; 27142d8acc0fSJohn Dyson uaddr += tcnt; 27151efb74fbSJohn Dyson cp += tcnt; 271695e5e988SJohn Dyson if (npages) 271795e5e988SJohn Dyson *npages += osize; 27181efb74fbSJohn Dyson } 27191efb74fbSJohn Dyson return 0; 27201efb74fbSJohn Dyson } 27211efb74fbSJohn Dyson 27221efb74fbSJohn Dyson /* 27231efb74fbSJohn Dyson * Performs the copy_on_write operations necessary to allow the virtual copies 27241efb74fbSJohn Dyson * into user space to work. This has to be called for write(2) system calls 27251efb74fbSJohn Dyson * from other processes, file unlinking, and file size shrinkage. 27261efb74fbSJohn Dyson */ 27271efb74fbSJohn Dyson void 27281efb74fbSJohn Dyson vm_freeze_copyopts(object, froma, toa) 27291efb74fbSJohn Dyson vm_object_t object; 27301efb74fbSJohn Dyson vm_pindex_t froma, toa; 27311efb74fbSJohn Dyson { 273295461b45SJohn Dyson int s, rv; 27331efb74fbSJohn Dyson vm_object_t robject, robjectn; 27341efb74fbSJohn Dyson vm_pindex_t idx, from, to; 27351efb74fbSJohn Dyson 27362d8acc0fSJohn Dyson if ((object == NULL) || 273795e5e988SJohn Dyson ((object->flags & OBJ_OPT) == 0)) 273895e5e988SJohn Dyson return; 27391efb74fbSJohn Dyson 27401efb74fbSJohn Dyson if (object->shadow_count > object->ref_count) 27411efb74fbSJohn Dyson panic("vm_freeze_copyopts: sc > rc"); 27421efb74fbSJohn Dyson 274347221757SJohn Dyson while( robject = TAILQ_FIRST(&object->shadow_head)) { 27441efb74fbSJohn Dyson vm_pindex_t bo_pindex; 27451efb74fbSJohn Dyson vm_page_t m_in, m_out; 27461efb74fbSJohn Dyson 27471efb74fbSJohn Dyson bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 27481efb74fbSJohn Dyson 274995e5e988SJohn Dyson vm_object_reference(robject); 2750925a3a41SJohn Dyson 275166095752SJohn Dyson vm_object_pip_wait(robject, "objfrz"); 2752925a3a41SJohn Dyson 27531efb74fbSJohn Dyson if (robject->ref_count == 1) { 27541efb74fbSJohn Dyson vm_object_deallocate(robject); 27551efb74fbSJohn Dyson continue; 27561efb74fbSJohn Dyson } 27571efb74fbSJohn Dyson 27581efb74fbSJohn Dyson robject->paging_in_progress++; 27591efb74fbSJohn Dyson 276047221757SJohn Dyson for (idx = 0; idx < robject->size; idx++) { 27611efb74fbSJohn Dyson 276247221757SJohn Dyson m_outretry: 276395461b45SJohn Dyson m_out = vm_page_grab(robject, idx, 276495461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 27651efb74fbSJohn Dyson 27661efb74fbSJohn Dyson if (m_out->valid == 0) { 276747221757SJohn Dyson m_inretry: 276895461b45SJohn Dyson m_in = vm_page_grab(object, bo_pindex + idx, 276995461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 277095461b45SJohn Dyson if (m_in->valid == 0) { 277147221757SJohn Dyson rv = vm_pager_get_pages(object, &m_in, 1, 0); 277247221757SJohn Dyson if (rv != VM_PAGER_OK) { 277347221757SJohn Dyson printf("vm_freeze_copyopts: cannot read page from file: %x\n", m_in->pindex); 277447221757SJohn Dyson continue; 27751efb74fbSJohn Dyson } 277695461b45SJohn Dyson vm_page_deactivate(m_in); 277747221757SJohn Dyson } 277847221757SJohn Dyson 277947221757SJohn Dyson vm_page_protect(m_in, VM_PROT_NONE); 278047221757SJohn Dyson pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out)); 278195461b45SJohn Dyson m_out->valid = m_in->valid; 278247221757SJohn Dyson m_out->dirty = VM_PAGE_BITS_ALL; 278347221757SJohn Dyson 278495461b45SJohn Dyson vm_page_activate(m_out); 278547221757SJohn Dyson 27861efb74fbSJohn Dyson PAGE_WAKEUP(m_in); 27871efb74fbSJohn Dyson } 278895461b45SJohn Dyson PAGE_WAKEUP(m_out); 278947221757SJohn Dyson } 2790925a3a41SJohn Dyson 27911efb74fbSJohn Dyson object->shadow_count--; 279247221757SJohn Dyson object->ref_count--; 27931efb74fbSJohn Dyson TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 27941efb74fbSJohn Dyson robject->backing_object = NULL; 27951efb74fbSJohn Dyson robject->backing_object_offset = 0; 27961efb74fbSJohn Dyson 279747221757SJohn Dyson vm_object_pip_wakeup(robject); 27981efb74fbSJohn Dyson vm_object_deallocate(robject); 27991efb74fbSJohn Dyson } 280047221757SJohn Dyson 28011efb74fbSJohn Dyson object->flags &= ~OBJ_OPT; 28021efb74fbSJohn Dyson } 28031efb74fbSJohn Dyson 2804c7c34a24SBruce Evans #include "opt_ddb.h" 2805c3cb3e12SDavid Greenman #ifdef DDB 2806c7c34a24SBruce Evans #include <sys/kernel.h> 2807c7c34a24SBruce Evans 2808c7c34a24SBruce Evans #include <ddb/ddb.h> 2809c7c34a24SBruce Evans 2810df8bae1dSRodney W. Grimes /* 2811df8bae1dSRodney W. Grimes * vm_map_print: [ debug ] 2812df8bae1dSRodney W. Grimes */ 2813c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print) 2814df8bae1dSRodney W. Grimes { 281595e5e988SJohn Dyson static int nlines; 2816c7c34a24SBruce Evans /* XXX convert args. */ 2817c0877f10SJohn Dyson vm_map_t map = (vm_map_t)addr; 2818c7c34a24SBruce Evans boolean_t full = have_addr; 2819df8bae1dSRodney W. Grimes 2820c0877f10SJohn Dyson vm_map_entry_t entry; 2821c7c34a24SBruce Evans 28222d8acc0fSJohn Dyson db_iprintf("%s map 0x%x: pmap=0x%x, nentries=%d, version=%d\n", 2823df8bae1dSRodney W. Grimes (map->is_main_map ? "Task" : "Share"), 28242d8acc0fSJohn Dyson (int) map, (int) (map->pmap), map->nentries, 2825df8bae1dSRodney W. Grimes map->timestamp); 282695e5e988SJohn Dyson nlines++; 2827df8bae1dSRodney W. Grimes 2828c7c34a24SBruce Evans if (!full && db_indent) 2829df8bae1dSRodney W. Grimes return; 2830df8bae1dSRodney W. Grimes 2831c7c34a24SBruce Evans db_indent += 2; 2832df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 2833df8bae1dSRodney W. Grimes entry = entry->next) { 283495e5e988SJohn Dyson #if 0 283595e5e988SJohn Dyson if (nlines > 18) { 283695e5e988SJohn Dyson db_printf("--More--"); 283795e5e988SJohn Dyson cngetc(); 283895e5e988SJohn Dyson db_printf("\r"); 283995e5e988SJohn Dyson nlines = 0; 284095e5e988SJohn Dyson } 284195e5e988SJohn Dyson #endif 284295e5e988SJohn Dyson 284395e5e988SJohn Dyson db_iprintf("map entry 0x%x: start=0x%x, end=0x%x\n", 2844df8bae1dSRodney W. Grimes (int) entry, (int) entry->start, (int) entry->end); 284595e5e988SJohn Dyson nlines++; 2846df8bae1dSRodney W. Grimes if (map->is_main_map) { 2847df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 2848df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 28490d94caffSDavid Greenman 285095e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 2851df8bae1dSRodney W. Grimes entry->protection, 2852df8bae1dSRodney W. Grimes entry->max_protection, 2853df8bae1dSRodney W. Grimes inheritance_name[entry->inheritance]); 2854df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 285595e5e988SJohn Dyson db_printf(", wired"); 2856df8bae1dSRodney W. Grimes } 2857afa07f7eSJohn Dyson if (entry->eflags & (MAP_ENTRY_IS_A_MAP|MAP_ENTRY_IS_SUB_MAP)) { 285895e5e988SJohn Dyson db_printf(", share=0x%x, offset=0x%x\n", 2859df8bae1dSRodney W. Grimes (int) entry->object.share_map, 2860df8bae1dSRodney W. Grimes (int) entry->offset); 286195e5e988SJohn Dyson nlines++; 2862df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 2863afa07f7eSJohn Dyson ((entry->prev->eflags & MAP_ENTRY_IS_A_MAP) == 0) || 2864df8bae1dSRodney W. Grimes (entry->prev->object.share_map != 2865df8bae1dSRodney W. Grimes entry->object.share_map)) { 2866c7c34a24SBruce Evans db_indent += 2; 2867914181e7SBruce Evans vm_map_print((int)entry->object.share_map, 2868914181e7SBruce Evans full, 0, (char *)0); 2869c7c34a24SBruce Evans db_indent -= 2; 2870df8bae1dSRodney W. Grimes } 28710d94caffSDavid Greenman } else { 287295e5e988SJohn Dyson db_printf(", object=0x%x, offset=0x%x", 2873df8bae1dSRodney W. Grimes (int) entry->object.vm_object, 2874df8bae1dSRodney W. Grimes (int) entry->offset); 2875afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 2876c7c34a24SBruce Evans db_printf(", copy (%s)", 2877afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 2878c7c34a24SBruce Evans db_printf("\n"); 287995e5e988SJohn Dyson nlines++; 2880df8bae1dSRodney W. Grimes 2881df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 2882afa07f7eSJohn Dyson (entry->prev->eflags & MAP_ENTRY_IS_A_MAP) || 2883df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 2884df8bae1dSRodney W. Grimes entry->object.vm_object)) { 2885c7c34a24SBruce Evans db_indent += 2; 2886914181e7SBruce Evans vm_object_print((int)entry->object.vm_object, 2887914181e7SBruce Evans full, 0, (char *)0); 288895e5e988SJohn Dyson nlines += 4; 2889c7c34a24SBruce Evans db_indent -= 2; 2890df8bae1dSRodney W. Grimes } 2891df8bae1dSRodney W. Grimes } 2892df8bae1dSRodney W. Grimes } 2893c7c34a24SBruce Evans db_indent -= 2; 289495e5e988SJohn Dyson if (db_indent == 0) 289595e5e988SJohn Dyson nlines = 0; 2896df8bae1dSRodney W. Grimes } 289795e5e988SJohn Dyson 289895e5e988SJohn Dyson 289995e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 290095e5e988SJohn Dyson { 290195e5e988SJohn Dyson struct proc *p; 290295e5e988SJohn Dyson 290395e5e988SJohn Dyson if (have_addr) { 290495e5e988SJohn Dyson p = (struct proc *) addr; 290595e5e988SJohn Dyson } else { 290695e5e988SJohn Dyson p = curproc; 290795e5e988SJohn Dyson } 290895e5e988SJohn Dyson 290995e5e988SJohn Dyson printf("p = 0x%x, vmspace = 0x%x, map = 0x%x, pmap = 0x%x\n", 291095e5e988SJohn Dyson p, p->p_vmspace, &p->p_vmspace->vm_map, &p->p_vmspace->vm_pmap); 291195e5e988SJohn Dyson 291295e5e988SJohn Dyson vm_map_print ((int) &p->p_vmspace->vm_map, 1, 0, NULL); 291395e5e988SJohn Dyson } 291495e5e988SJohn Dyson 2915c7c34a24SBruce Evans #endif /* DDB */ 2916