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 17956f3135SPhilippe Charnier * must display the following acknowledgment: 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 * 64c3aac50fSPeter Wemm * $FreeBSD$ 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> 73b5e8ce9fSBruce Evans #include <sys/proc.h> 74efeaf95aSDavid Greenman #include <sys/vmmeter.h> 75867a482dSJohn Dyson #include <sys/mman.h> 761efb74fbSJohn Dyson #include <sys/vnode.h> 772267af78SJulian Elischer #include <sys/resourcevar.h> 78df8bae1dSRodney W. Grimes 79df8bae1dSRodney W. Grimes #include <vm/vm.h> 80efeaf95aSDavid Greenman #include <vm/vm_param.h> 81996c772fSJohn Dyson #include <sys/lock.h> 82efeaf95aSDavid Greenman #include <vm/pmap.h> 83efeaf95aSDavid Greenman #include <vm/vm_map.h> 84df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 85df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 8647221757SJohn Dyson #include <vm/vm_pager.h> 8726f9a767SRodney W. Grimes #include <vm/vm_kern.h> 88efeaf95aSDavid Greenman #include <vm/vm_extern.h> 8947221757SJohn Dyson #include <vm/swap_pager.h> 903075778bSJohn Dyson #include <vm/vm_zone.h> 91df8bae1dSRodney W. Grimes 92df8bae1dSRodney W. Grimes /* 93df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 94df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 95df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 96df8bae1dSRodney W. Grimes * memory from one map to another. 97df8bae1dSRodney W. Grimes * 98df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 99df8bae1dSRodney W. Grimes * 100df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 101df8bae1dSRodney W. Grimes * entries; a single hint is used to speed up lookups. 102df8bae1dSRodney W. Grimes * 103956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 104df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 105df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 106df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 107df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 108df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 109df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 110df8bae1dSRodney W. Grimes * 111df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 112ad5fca3bSAlan Cox * by copying VM object references from one map to 113df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 114df8bae1dSRodney W. Grimes */ 115df8bae1dSRodney W. Grimes 116df8bae1dSRodney W. Grimes /* 117df8bae1dSRodney W. Grimes * vm_map_startup: 118df8bae1dSRodney W. Grimes * 119df8bae1dSRodney W. Grimes * Initialize the vm_map module. Must be called before 120df8bae1dSRodney W. Grimes * any other vm_map routines. 121df8bae1dSRodney W. Grimes * 122df8bae1dSRodney W. Grimes * Map and entry structures are allocated from the general 123df8bae1dSRodney W. Grimes * purpose memory pool with some exceptions: 124df8bae1dSRodney W. Grimes * 125df8bae1dSRodney W. Grimes * - The kernel map and kmem submap are allocated statically. 126df8bae1dSRodney W. Grimes * - Kernel map entries are allocated out of a static pool. 127df8bae1dSRodney W. Grimes * 128df8bae1dSRodney W. Grimes * These restrictions are necessary since malloc() uses the 129df8bae1dSRodney W. Grimes * maps and requires map entries. 130df8bae1dSRodney W. Grimes */ 131df8bae1dSRodney W. Grimes 1323075778bSJohn Dyson static struct vm_zone kmapentzone_store, mapentzone_store, mapzone_store; 1332d8acc0fSJohn Dyson static vm_zone_t mapentzone, kmapentzone, mapzone, vmspace_zone; 1343075778bSJohn Dyson static struct vm_object kmapentobj, mapentobj, mapobj; 1351fc43fd1SAlan Cox 136303b270bSEivind Eklund static struct vm_map_entry map_entry_init[MAX_MAPENT]; 137303b270bSEivind Eklund static struct vm_map_entry kmap_entry_init[MAX_KMAPENT]; 138303b270bSEivind Eklund static struct vm_map map_init[MAX_KMAP]; 139b18bfc3dSJohn Dyson 140df8bae1dSRodney W. Grimes static void _vm_map_clip_end __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 141df8bae1dSRodney W. Grimes static void _vm_map_clip_start __P((vm_map_t, vm_map_entry_t, vm_offset_t)); 142f708ef1bSPoul-Henning Kamp static vm_map_entry_t vm_map_entry_create __P((vm_map_t)); 143f708ef1bSPoul-Henning Kamp static void vm_map_entry_delete __P((vm_map_t, vm_map_entry_t)); 1440362d7d7SJohn Dyson static void vm_map_entry_dispose __P((vm_map_t, vm_map_entry_t)); 145f708ef1bSPoul-Henning Kamp static void vm_map_entry_unwire __P((vm_map_t, vm_map_entry_t)); 146f708ef1bSPoul-Henning Kamp static void vm_map_copy_entry __P((vm_map_t, vm_map_t, vm_map_entry_t, 147f708ef1bSPoul-Henning Kamp vm_map_entry_t)); 148dda6b171SJohn Dyson static void vm_map_split __P((vm_map_entry_t)); 149df8bae1dSRodney W. Grimes 1500d94caffSDavid Greenman void 1510d94caffSDavid Greenman vm_map_startup() 152df8bae1dSRodney W. Grimes { 1533075778bSJohn Dyson mapzone = &mapzone_store; 1540d65e566SJohn Dyson zbootinit(mapzone, "MAP", sizeof (struct vm_map), 1553075778bSJohn Dyson map_init, MAX_KMAP); 1563075778bSJohn Dyson kmapentzone = &kmapentzone_store; 1570d65e566SJohn Dyson zbootinit(kmapentzone, "KMAP ENTRY", sizeof (struct vm_map_entry), 1583075778bSJohn Dyson kmap_entry_init, MAX_KMAPENT); 1593075778bSJohn Dyson mapentzone = &mapentzone_store; 1600d65e566SJohn Dyson zbootinit(mapentzone, "MAP ENTRY", sizeof (struct vm_map_entry), 1613075778bSJohn Dyson map_entry_init, MAX_MAPENT); 162df8bae1dSRodney W. Grimes } 163df8bae1dSRodney W. Grimes 164df8bae1dSRodney W. Grimes /* 165df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 166df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 167df8bae1dSRodney W. Grimes * The remaining fields must be initialized by the caller. 168df8bae1dSRodney W. Grimes */ 169df8bae1dSRodney W. Grimes struct vmspace * 1702d8acc0fSJohn Dyson vmspace_alloc(min, max) 171df8bae1dSRodney W. Grimes vm_offset_t min, max; 172df8bae1dSRodney W. Grimes { 173c0877f10SJohn Dyson struct vmspace *vm; 1740d94caffSDavid Greenman 1752d8acc0fSJohn Dyson vm = zalloc(vmspace_zone); 1762d8acc0fSJohn Dyson vm_map_init(&vm->vm_map, min, max); 177b1028ad1SLuoqi Chen pmap_pinit(vmspace_pmap(vm)); 178b1028ad1SLuoqi Chen vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 179df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 1802d8acc0fSJohn Dyson vm->vm_shm = NULL; 181df8bae1dSRodney W. Grimes return (vm); 182df8bae1dSRodney W. Grimes } 183df8bae1dSRodney W. Grimes 184df8bae1dSRodney W. Grimes void 1853075778bSJohn Dyson vm_init2(void) { 1860d65e566SJohn Dyson zinitna(kmapentzone, &kmapentobj, 1870a80f406SJohn Dyson NULL, 0, cnt.v_page_count / 4, ZONE_INTERRUPT, 1); 1880d65e566SJohn Dyson zinitna(mapentzone, &mapentobj, 1890a80f406SJohn Dyson NULL, 0, 0, 0, 1); 1900d65e566SJohn Dyson zinitna(mapzone, &mapobj, 1910a80f406SJohn Dyson NULL, 0, 0, 0, 1); 1922d8acc0fSJohn Dyson vmspace_zone = zinit("VMSPACE", sizeof (struct vmspace), 0, 0, 3); 193ba9be04cSJohn Dyson pmap_init2(); 19499448ed1SJohn Dyson vm_object_init2(); 1953075778bSJohn Dyson } 1963075778bSJohn Dyson 1973075778bSJohn Dyson void 198df8bae1dSRodney W. Grimes vmspace_free(vm) 199c0877f10SJohn Dyson struct vmspace *vm; 200df8bae1dSRodney W. Grimes { 201df8bae1dSRodney W. Grimes 202a1f6d91cSDavid Greenman if (vm->vm_refcnt == 0) 203a1f6d91cSDavid Greenman panic("vmspace_free: attempt to free already freed vmspace"); 204a1f6d91cSDavid Greenman 205df8bae1dSRodney W. Grimes if (--vm->vm_refcnt == 0) { 206bd7e5f99SJohn Dyson 20730dcfc09SJohn Dyson /* 208df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 2090d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 2100d94caffSDavid Greenman * the pmap module to reclaim anything left. 211df8bae1dSRodney W. Grimes */ 212df8bae1dSRodney W. Grimes vm_map_lock(&vm->vm_map); 213df8bae1dSRodney W. Grimes (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 214df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 215a1f6d91cSDavid Greenman vm_map_unlock(&vm->vm_map); 216b18bfc3dSJohn Dyson 217b1028ad1SLuoqi Chen pmap_release(vmspace_pmap(vm)); 2182d8acc0fSJohn Dyson zfree(vmspace_zone, vm); 219df8bae1dSRodney W. Grimes } 220df8bae1dSRodney W. Grimes } 221df8bae1dSRodney W. Grimes 222df8bae1dSRodney W. Grimes /* 223df8bae1dSRodney W. Grimes * vm_map_create: 224df8bae1dSRodney W. Grimes * 225df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 226df8bae1dSRodney W. Grimes * the given physical map structure, and having 227df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 228df8bae1dSRodney W. Grimes */ 2290d94caffSDavid Greenman vm_map_t 2302d8acc0fSJohn Dyson vm_map_create(pmap, min, max) 231df8bae1dSRodney W. Grimes pmap_t pmap; 232df8bae1dSRodney W. Grimes vm_offset_t min, max; 233df8bae1dSRodney W. Grimes { 234c0877f10SJohn Dyson vm_map_t result; 235df8bae1dSRodney W. Grimes 2363075778bSJohn Dyson result = zalloc(mapzone); 2372d8acc0fSJohn Dyson vm_map_init(result, min, max); 238df8bae1dSRodney W. Grimes result->pmap = pmap; 239df8bae1dSRodney W. Grimes return (result); 240df8bae1dSRodney W. Grimes } 241df8bae1dSRodney W. Grimes 242df8bae1dSRodney W. Grimes /* 243df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 244df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 245df8bae1dSRodney W. Grimes * The pmap is set elsewhere. 246df8bae1dSRodney W. Grimes */ 247df8bae1dSRodney W. Grimes void 2482d8acc0fSJohn Dyson vm_map_init(map, min, max) 249c0877f10SJohn Dyson struct vm_map *map; 250df8bae1dSRodney W. Grimes vm_offset_t min, max; 251df8bae1dSRodney W. Grimes { 252df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 253df8bae1dSRodney W. Grimes map->nentries = 0; 254df8bae1dSRodney W. Grimes map->size = 0; 2553075778bSJohn Dyson map->system_map = 0; 256df8bae1dSRodney W. Grimes map->min_offset = min; 257df8bae1dSRodney W. Grimes map->max_offset = max; 258df8bae1dSRodney W. Grimes map->first_free = &map->header; 259df8bae1dSRodney W. Grimes map->hint = &map->header; 260df8bae1dSRodney W. Grimes map->timestamp = 0; 2618f9110f6SJohn Dyson lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE); 262df8bae1dSRodney W. Grimes } 263df8bae1dSRodney W. Grimes 264df8bae1dSRodney W. Grimes /* 265b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 266b18bfc3dSJohn Dyson * 267b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 268b18bfc3dSJohn Dyson */ 26962487bb4SJohn Dyson static void 270b18bfc3dSJohn Dyson vm_map_entry_dispose(map, entry) 271b18bfc3dSJohn Dyson vm_map_t map; 272b18bfc3dSJohn Dyson vm_map_entry_t entry; 273b18bfc3dSJohn Dyson { 274b79933ebSJohn Dyson zfree((map->system_map || !mapentzone) ? kmapentzone : mapentzone, entry); 275b18bfc3dSJohn Dyson } 276b18bfc3dSJohn Dyson 277b18bfc3dSJohn Dyson /* 278df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 279df8bae1dSRodney W. Grimes * 280df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 281df8bae1dSRodney W. Grimes * No entry fields are filled in. This routine is 282df8bae1dSRodney W. Grimes */ 283f708ef1bSPoul-Henning Kamp static vm_map_entry_t 28426f9a767SRodney W. Grimes vm_map_entry_create(map) 285df8bae1dSRodney W. Grimes vm_map_t map; 286df8bae1dSRodney W. Grimes { 2871f6889a1SMatthew Dillon vm_map_entry_t new_entry; 2881f6889a1SMatthew Dillon 2891f6889a1SMatthew Dillon new_entry = zalloc((map->system_map || !mapentzone) ? 2901f6889a1SMatthew Dillon kmapentzone : mapentzone); 2911f6889a1SMatthew Dillon if (new_entry == NULL) 2921f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 2931f6889a1SMatthew Dillon return(new_entry); 294df8bae1dSRodney W. Grimes } 295df8bae1dSRodney W. Grimes 296df8bae1dSRodney W. Grimes /* 297df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 298df8bae1dSRodney W. Grimes * 299df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 300df8bae1dSRodney W. Grimes */ 30199c81ca9SAlan Cox static __inline void 30299c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 30399c81ca9SAlan Cox vm_map_entry_t after_where, 30499c81ca9SAlan Cox vm_map_entry_t entry) 30599c81ca9SAlan Cox { 30699c81ca9SAlan Cox map->nentries++; 30799c81ca9SAlan Cox entry->prev = after_where; 30899c81ca9SAlan Cox entry->next = after_where->next; 30999c81ca9SAlan Cox entry->next->prev = entry; 31099c81ca9SAlan Cox after_where->next = entry; 311df8bae1dSRodney W. Grimes } 31299c81ca9SAlan Cox 31399c81ca9SAlan Cox static __inline void 31499c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 31599c81ca9SAlan Cox vm_map_entry_t entry) 31699c81ca9SAlan Cox { 31799c81ca9SAlan Cox vm_map_entry_t prev = entry->prev; 31899c81ca9SAlan Cox vm_map_entry_t next = entry->next; 31999c81ca9SAlan Cox 32099c81ca9SAlan Cox next->prev = prev; 32199c81ca9SAlan Cox prev->next = next; 32299c81ca9SAlan Cox map->nentries--; 323df8bae1dSRodney W. Grimes } 324df8bae1dSRodney W. Grimes 325df8bae1dSRodney W. Grimes /* 326df8bae1dSRodney W. Grimes * SAVE_HINT: 327df8bae1dSRodney W. Grimes * 328df8bae1dSRodney W. Grimes * Saves the specified entry as the hint for 32924a1cce3SDavid Greenman * future lookups. 330df8bae1dSRodney W. Grimes */ 331df8bae1dSRodney W. Grimes #define SAVE_HINT(map,value) \ 33224a1cce3SDavid Greenman (map)->hint = (value); 333df8bae1dSRodney W. Grimes 334df8bae1dSRodney W. Grimes /* 335df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 336df8bae1dSRodney W. Grimes * 337df8bae1dSRodney W. Grimes * Finds the map entry containing (or 338df8bae1dSRodney W. Grimes * immediately preceding) the specified address 339df8bae1dSRodney W. Grimes * in the given map; the entry is returned 340df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 341df8bae1dSRodney W. Grimes * result indicates whether the address is 342df8bae1dSRodney W. Grimes * actually contained in the map. 343df8bae1dSRodney W. Grimes */ 3440d94caffSDavid Greenman boolean_t 3450d94caffSDavid Greenman vm_map_lookup_entry(map, address, entry) 346c0877f10SJohn Dyson vm_map_t map; 347c0877f10SJohn Dyson vm_offset_t address; 348df8bae1dSRodney W. Grimes vm_map_entry_t *entry; /* OUT */ 349df8bae1dSRodney W. Grimes { 350c0877f10SJohn Dyson vm_map_entry_t cur; 351c0877f10SJohn Dyson vm_map_entry_t last; 352df8bae1dSRodney W. Grimes 353df8bae1dSRodney W. Grimes /* 3540d94caffSDavid Greenman * Start looking either from the head of the list, or from the hint. 355df8bae1dSRodney W. Grimes */ 356df8bae1dSRodney W. Grimes 357df8bae1dSRodney W. Grimes cur = map->hint; 358df8bae1dSRodney W. Grimes 359df8bae1dSRodney W. Grimes if (cur == &map->header) 360df8bae1dSRodney W. Grimes cur = cur->next; 361df8bae1dSRodney W. Grimes 362df8bae1dSRodney W. Grimes if (address >= cur->start) { 363df8bae1dSRodney W. Grimes /* 364df8bae1dSRodney W. Grimes * Go from hint to end of list. 365df8bae1dSRodney W. Grimes * 3660d94caffSDavid Greenman * But first, make a quick check to see if we are already looking 3670d94caffSDavid Greenman * at the entry we want (which is usually the case). Note also 3680d94caffSDavid Greenman * that we don't need to save the hint here... it is the same 3690d94caffSDavid Greenman * hint (unless we are at the header, in which case the hint 3700d94caffSDavid Greenman * didn't buy us anything anyway). 371df8bae1dSRodney W. Grimes */ 372df8bae1dSRodney W. Grimes last = &map->header; 373df8bae1dSRodney W. Grimes if ((cur != last) && (cur->end > address)) { 374df8bae1dSRodney W. Grimes *entry = cur; 375df8bae1dSRodney W. Grimes return (TRUE); 376df8bae1dSRodney W. Grimes } 3770d94caffSDavid Greenman } else { 378df8bae1dSRodney W. Grimes /* 379df8bae1dSRodney W. Grimes * Go from start to hint, *inclusively* 380df8bae1dSRodney W. Grimes */ 381df8bae1dSRodney W. Grimes last = cur->next; 382df8bae1dSRodney W. Grimes cur = map->header.next; 383df8bae1dSRodney W. Grimes } 384df8bae1dSRodney W. Grimes 385df8bae1dSRodney W. Grimes /* 386df8bae1dSRodney W. Grimes * Search linearly 387df8bae1dSRodney W. Grimes */ 388df8bae1dSRodney W. Grimes 389df8bae1dSRodney W. Grimes while (cur != last) { 390df8bae1dSRodney W. Grimes if (cur->end > address) { 391df8bae1dSRodney W. Grimes if (address >= cur->start) { 392df8bae1dSRodney W. Grimes /* 3930d94caffSDavid Greenman * Save this lookup for future hints, and 3940d94caffSDavid Greenman * return 395df8bae1dSRodney W. Grimes */ 396df8bae1dSRodney W. Grimes 397df8bae1dSRodney W. Grimes *entry = cur; 398df8bae1dSRodney W. Grimes SAVE_HINT(map, cur); 399df8bae1dSRodney W. Grimes return (TRUE); 400df8bae1dSRodney W. Grimes } 401df8bae1dSRodney W. Grimes break; 402df8bae1dSRodney W. Grimes } 403df8bae1dSRodney W. Grimes cur = cur->next; 404df8bae1dSRodney W. Grimes } 405df8bae1dSRodney W. Grimes *entry = cur->prev; 406df8bae1dSRodney W. Grimes SAVE_HINT(map, *entry); 407df8bae1dSRodney W. Grimes return (FALSE); 408df8bae1dSRodney W. Grimes } 409df8bae1dSRodney W. Grimes 410df8bae1dSRodney W. Grimes /* 41130dcfc09SJohn Dyson * vm_map_insert: 41230dcfc09SJohn Dyson * 41330dcfc09SJohn Dyson * Inserts the given whole VM object into the target 41430dcfc09SJohn Dyson * map at the specified address range. The object's 41530dcfc09SJohn Dyson * size should match that of the address range. 41630dcfc09SJohn Dyson * 41730dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 4182aaeadf8SMatthew Dillon * 4192aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 4202aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 42130dcfc09SJohn Dyson */ 42230dcfc09SJohn Dyson int 423b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 424b9dcd593SBruce Evans vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 425b9dcd593SBruce Evans int cow) 42630dcfc09SJohn Dyson { 427c0877f10SJohn Dyson vm_map_entry_t new_entry; 428c0877f10SJohn Dyson vm_map_entry_t prev_entry; 42930dcfc09SJohn Dyson vm_map_entry_t temp_entry; 4309730a5daSPaul Saab vm_eflags_t protoeflags; 43130dcfc09SJohn Dyson 43230dcfc09SJohn Dyson /* 43330dcfc09SJohn Dyson * Check that the start and end points are not bogus. 43430dcfc09SJohn Dyson */ 43530dcfc09SJohn Dyson 43630dcfc09SJohn Dyson if ((start < map->min_offset) || (end > map->max_offset) || 43730dcfc09SJohn Dyson (start >= end)) 43830dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 43930dcfc09SJohn Dyson 44030dcfc09SJohn Dyson /* 44130dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 44230dcfc09SJohn Dyson * of an existing entry, this range is bogus. 44330dcfc09SJohn Dyson */ 44430dcfc09SJohn Dyson 44530dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 44630dcfc09SJohn Dyson return (KERN_NO_SPACE); 44730dcfc09SJohn Dyson 44830dcfc09SJohn Dyson prev_entry = temp_entry; 44930dcfc09SJohn Dyson 45030dcfc09SJohn Dyson /* 45130dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 45230dcfc09SJohn Dyson */ 45330dcfc09SJohn Dyson 45430dcfc09SJohn Dyson if ((prev_entry->next != &map->header) && 45530dcfc09SJohn Dyson (prev_entry->next->start < end)) 45630dcfc09SJohn Dyson return (KERN_NO_SPACE); 45730dcfc09SJohn Dyson 458afa07f7eSJohn Dyson protoeflags = 0; 459afa07f7eSJohn Dyson 460afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 461e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 462afa07f7eSJohn Dyson 4634e045f93SAlan Cox if (cow & MAP_NOFAULT) { 464afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 465afa07f7eSJohn Dyson 4664e045f93SAlan Cox KASSERT(object == NULL, 4674e045f93SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 4684e045f93SAlan Cox } 4694f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 4704f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 4719730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 4729730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 4734f79d873SMatthew Dillon 4742aaeadf8SMatthew Dillon if (object) { 47530dcfc09SJohn Dyson /* 4762aaeadf8SMatthew Dillon * When object is non-NULL, it could be shared with another 4772aaeadf8SMatthew Dillon * process. We have to set or clear OBJ_ONEMAPPING 4782aaeadf8SMatthew Dillon * appropriately. 47930dcfc09SJohn Dyson */ 4802aaeadf8SMatthew Dillon if ((object->ref_count > 1) || (object->shadow_count != 0)) { 4812aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 4822aaeadf8SMatthew Dillon } 4834e045f93SAlan Cox } 4844e045f93SAlan Cox else if ((prev_entry != &map->header) && 4854e045f93SAlan Cox (prev_entry->eflags == protoeflags) && 4868cc7e047SJohn Dyson (prev_entry->end == start) && 4874e045f93SAlan Cox (prev_entry->wired_count == 0) && 4884e045f93SAlan Cox ((prev_entry->object.vm_object == NULL) || 4898cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 49030dcfc09SJohn Dyson OFF_TO_IDX(prev_entry->offset), 4918cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 492cdc2c291SJohn Dyson (vm_size_t)(end - prev_entry->end)))) { 49330dcfc09SJohn Dyson /* 4942aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 4952aaeadf8SMatthew Dillon * can extend the previous map entry to include the 4962aaeadf8SMatthew Dillon * new range as well. 49730dcfc09SJohn Dyson */ 4988cc7e047SJohn Dyson if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 4998cc7e047SJohn Dyson (prev_entry->protection == prot) && 5008cc7e047SJohn Dyson (prev_entry->max_protection == max)) { 50130dcfc09SJohn Dyson map->size += (end - prev_entry->end); 50230dcfc09SJohn Dyson prev_entry->end = end; 50330dcfc09SJohn Dyson return (KERN_SUCCESS); 50430dcfc09SJohn Dyson } 5058cc7e047SJohn Dyson 5062aaeadf8SMatthew Dillon /* 5072aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 5082aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 5092aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 5102aaeadf8SMatthew Dillon * account for it. 5112aaeadf8SMatthew Dillon */ 5122aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 5132aaeadf8SMatthew Dillon offset = prev_entry->offset + 5142aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 5158cc7e047SJohn Dyson vm_object_reference(object); 516b18bfc3dSJohn Dyson } 5172aaeadf8SMatthew Dillon 5182aaeadf8SMatthew Dillon /* 5192aaeadf8SMatthew Dillon * NOTE: if conditionals fail, object can be NULL here. This occurs 5202aaeadf8SMatthew Dillon * in things like the buffer map where we manage kva but do not manage 5212aaeadf8SMatthew Dillon * backing objects. 5222aaeadf8SMatthew Dillon */ 5238cc7e047SJohn Dyson 52430dcfc09SJohn Dyson /* 52530dcfc09SJohn Dyson * Create a new entry 52630dcfc09SJohn Dyson */ 52730dcfc09SJohn Dyson 52830dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 52930dcfc09SJohn Dyson new_entry->start = start; 53030dcfc09SJohn Dyson new_entry->end = end; 53130dcfc09SJohn Dyson 532afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 53330dcfc09SJohn Dyson new_entry->object.vm_object = object; 53430dcfc09SJohn Dyson new_entry->offset = offset; 5352267af78SJulian Elischer new_entry->avail_ssize = 0; 5362267af78SJulian Elischer 53730dcfc09SJohn Dyson new_entry->inheritance = VM_INHERIT_DEFAULT; 53830dcfc09SJohn Dyson new_entry->protection = prot; 53930dcfc09SJohn Dyson new_entry->max_protection = max; 54030dcfc09SJohn Dyson new_entry->wired_count = 0; 541e5f251d2SAlan Cox 54230dcfc09SJohn Dyson /* 54330dcfc09SJohn Dyson * Insert the new entry into the list 54430dcfc09SJohn Dyson */ 54530dcfc09SJohn Dyson 54630dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 54730dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 54830dcfc09SJohn Dyson 54930dcfc09SJohn Dyson /* 55030dcfc09SJohn Dyson * Update the free space hint 55130dcfc09SJohn Dyson */ 55267bf6868SJohn Dyson if ((map->first_free == prev_entry) && 5534f79d873SMatthew Dillon (prev_entry->end >= new_entry->start)) { 55430dcfc09SJohn Dyson map->first_free = new_entry; 5554f79d873SMatthew Dillon } 55630dcfc09SJohn Dyson 5574f79d873SMatthew Dillon if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 558e972780aSAlan Cox pmap_object_init_pt(map->pmap, start, 559e972780aSAlan Cox object, OFF_TO_IDX(offset), end - start, 560e972780aSAlan Cox cow & MAP_PREFAULT_PARTIAL); 5614f79d873SMatthew Dillon } 562e972780aSAlan Cox 56330dcfc09SJohn Dyson return (KERN_SUCCESS); 56430dcfc09SJohn Dyson } 56530dcfc09SJohn Dyson 56630dcfc09SJohn Dyson /* 567df8bae1dSRodney W. Grimes * Find sufficient space for `length' bytes in the given map, starting at 568df8bae1dSRodney W. Grimes * `start'. The map must be locked. Returns 0 on success, 1 on no space. 569df8bae1dSRodney W. Grimes */ 570df8bae1dSRodney W. Grimes int 571df8bae1dSRodney W. Grimes vm_map_findspace(map, start, length, addr) 572c0877f10SJohn Dyson vm_map_t map; 573c0877f10SJohn Dyson vm_offset_t start; 574df8bae1dSRodney W. Grimes vm_size_t length; 575df8bae1dSRodney W. Grimes vm_offset_t *addr; 576df8bae1dSRodney W. Grimes { 577c0877f10SJohn Dyson vm_map_entry_t entry, next; 578c0877f10SJohn Dyson vm_offset_t end; 579df8bae1dSRodney W. Grimes 580df8bae1dSRodney W. Grimes if (start < map->min_offset) 581df8bae1dSRodney W. Grimes start = map->min_offset; 582df8bae1dSRodney W. Grimes if (start > map->max_offset) 583df8bae1dSRodney W. Grimes return (1); 584df8bae1dSRodney W. Grimes 585df8bae1dSRodney W. Grimes /* 5860d94caffSDavid Greenman * Look for the first possible address; if there's already something 5870d94caffSDavid Greenman * at this address, we have to start after it. 588df8bae1dSRodney W. Grimes */ 589df8bae1dSRodney W. Grimes if (start == map->min_offset) { 59067bf6868SJohn Dyson if ((entry = map->first_free) != &map->header) 591df8bae1dSRodney W. Grimes start = entry->end; 592df8bae1dSRodney W. Grimes } else { 593df8bae1dSRodney W. Grimes vm_map_entry_t tmp; 5940d94caffSDavid Greenman 595df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &tmp)) 596df8bae1dSRodney W. Grimes start = tmp->end; 597df8bae1dSRodney W. Grimes entry = tmp; 598df8bae1dSRodney W. Grimes } 599df8bae1dSRodney W. Grimes 600df8bae1dSRodney W. Grimes /* 6010d94caffSDavid Greenman * Look through the rest of the map, trying to fit a new region in the 6020d94caffSDavid Greenman * gap between existing regions, or after the very last region. 603df8bae1dSRodney W. Grimes */ 604df8bae1dSRodney W. Grimes for (;; start = (entry = next)->end) { 605df8bae1dSRodney W. Grimes /* 606df8bae1dSRodney W. Grimes * Find the end of the proposed new region. Be sure we didn't 607df8bae1dSRodney W. Grimes * go beyond the end of the map, or wrap around the address; 608df8bae1dSRodney W. Grimes * if so, we lose. Otherwise, if this is the last entry, or 609df8bae1dSRodney W. Grimes * if the proposed new region fits before the next entry, we 610df8bae1dSRodney W. Grimes * win. 611df8bae1dSRodney W. Grimes */ 612df8bae1dSRodney W. Grimes end = start + length; 613df8bae1dSRodney W. Grimes if (end > map->max_offset || end < start) 614df8bae1dSRodney W. Grimes return (1); 615df8bae1dSRodney W. Grimes next = entry->next; 616df8bae1dSRodney W. Grimes if (next == &map->header || next->start >= end) 617df8bae1dSRodney W. Grimes break; 618df8bae1dSRodney W. Grimes } 619df8bae1dSRodney W. Grimes SAVE_HINT(map, entry); 620df8bae1dSRodney W. Grimes *addr = start; 62199448ed1SJohn Dyson if (map == kernel_map) { 62299448ed1SJohn Dyson vm_offset_t ksize; 62399448ed1SJohn Dyson if ((ksize = round_page(start + length)) > kernel_vm_end) { 62499448ed1SJohn Dyson pmap_growkernel(ksize); 62599448ed1SJohn Dyson } 62699448ed1SJohn Dyson } 627df8bae1dSRodney W. Grimes return (0); 628df8bae1dSRodney W. Grimes } 629df8bae1dSRodney W. Grimes 630df8bae1dSRodney W. Grimes /* 631df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 632df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 633df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 634df8bae1dSRodney W. Grimes * returned in the same parameter. 635df8bae1dSRodney W. Grimes * 6362aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 6372aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 638df8bae1dSRodney W. Grimes */ 639df8bae1dSRodney W. Grimes int 640b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 641b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 642b9dcd593SBruce Evans vm_size_t length, boolean_t find_space, vm_prot_t prot, 643b9dcd593SBruce Evans vm_prot_t max, int cow) 644df8bae1dSRodney W. Grimes { 645c0877f10SJohn Dyson vm_offset_t start; 6468d6e8edeSDavid Greenman int result, s = 0; 647df8bae1dSRodney W. Grimes 648df8bae1dSRodney W. Grimes start = *addr; 6498d6e8edeSDavid Greenman 6509579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 651b18bfc3dSJohn Dyson s = splvm(); 6528d6e8edeSDavid Greenman 653bea41bcfSDavid Greenman vm_map_lock(map); 654df8bae1dSRodney W. Grimes if (find_space) { 655df8bae1dSRodney W. Grimes if (vm_map_findspace(map, start, length, addr)) { 656df8bae1dSRodney W. Grimes vm_map_unlock(map); 6579579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 6588d6e8edeSDavid Greenman splx(s); 659df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 660df8bae1dSRodney W. Grimes } 661df8bae1dSRodney W. Grimes start = *addr; 662df8bae1dSRodney W. Grimes } 663bd7e5f99SJohn Dyson result = vm_map_insert(map, object, offset, 664bd7e5f99SJohn Dyson start, start + length, prot, max, cow); 665df8bae1dSRodney W. Grimes vm_map_unlock(map); 6668d6e8edeSDavid Greenman 6679579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 6688d6e8edeSDavid Greenman splx(s); 6698d6e8edeSDavid Greenman 670df8bae1dSRodney W. Grimes return (result); 671df8bae1dSRodney W. Grimes } 672df8bae1dSRodney W. Grimes 673df8bae1dSRodney W. Grimes /* 674b7b2aac2SJohn Dyson * vm_map_simplify_entry: 67567bf6868SJohn Dyson * 676b7b2aac2SJohn Dyson * Simplify the given map entry by merging with either neighbor. 677df8bae1dSRodney W. Grimes */ 678b7b2aac2SJohn Dyson void 6790d94caffSDavid Greenman vm_map_simplify_entry(map, entry) 680df8bae1dSRodney W. Grimes vm_map_t map; 681df8bae1dSRodney W. Grimes vm_map_entry_t entry; 682df8bae1dSRodney W. Grimes { 683308c24baSJohn Dyson vm_map_entry_t next, prev; 684b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 685df8bae1dSRodney W. Grimes 6869fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 687df8bae1dSRodney W. Grimes return; 688308c24baSJohn Dyson 689308c24baSJohn Dyson prev = entry->prev; 690308c24baSJohn Dyson if (prev != &map->header) { 69167bf6868SJohn Dyson prevsize = prev->end - prev->start; 69267bf6868SJohn Dyson if ( (prev->end == entry->start) && 69367bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 69495e5e988SJohn Dyson (!prev->object.vm_object || 69567bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 696afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 69767bf6868SJohn Dyson (prev->protection == entry->protection) && 69867bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 69967bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 700b7b2aac2SJohn Dyson (prev->wired_count == entry->wired_count)) { 701308c24baSJohn Dyson if (map->first_free == prev) 702308c24baSJohn Dyson map->first_free = entry; 703b18bfc3dSJohn Dyson if (map->hint == prev) 704b18bfc3dSJohn Dyson map->hint = entry; 705308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 706308c24baSJohn Dyson entry->start = prev->start; 707308c24baSJohn Dyson entry->offset = prev->offset; 708b18bfc3dSJohn Dyson if (prev->object.vm_object) 709308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 710308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 711308c24baSJohn Dyson } 712308c24baSJohn Dyson } 713de5f6a77SJohn Dyson 714de5f6a77SJohn Dyson next = entry->next; 715308c24baSJohn Dyson if (next != &map->header) { 71667bf6868SJohn Dyson esize = entry->end - entry->start; 71767bf6868SJohn Dyson if ((entry->end == next->start) && 71867bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 71967bf6868SJohn Dyson (!entry->object.vm_object || 72067bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 721afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 72267bf6868SJohn Dyson (next->protection == entry->protection) && 72367bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 72467bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 725b7b2aac2SJohn Dyson (next->wired_count == entry->wired_count)) { 726308c24baSJohn Dyson if (map->first_free == next) 727308c24baSJohn Dyson map->first_free = entry; 728b18bfc3dSJohn Dyson if (map->hint == next) 729b18bfc3dSJohn Dyson map->hint = entry; 730de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 731de5f6a77SJohn Dyson entry->end = next->end; 732b18bfc3dSJohn Dyson if (next->object.vm_object) 733de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 734de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 735df8bae1dSRodney W. Grimes } 736df8bae1dSRodney W. Grimes } 737de5f6a77SJohn Dyson } 738df8bae1dSRodney W. Grimes /* 739df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 740df8bae1dSRodney W. Grimes * 741df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 742df8bae1dSRodney W. Grimes * the specified address; if necessary, 743df8bae1dSRodney W. Grimes * it splits the entry into two. 744df8bae1dSRodney W. Grimes */ 745df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 746df8bae1dSRodney W. Grimes { \ 747df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 748df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 749df8bae1dSRodney W. Grimes } 750df8bae1dSRodney W. Grimes 751df8bae1dSRodney W. Grimes /* 752df8bae1dSRodney W. Grimes * This routine is called only when it is known that 753df8bae1dSRodney W. Grimes * the entry must be split. 754df8bae1dSRodney W. Grimes */ 7550d94caffSDavid Greenman static void 7560d94caffSDavid Greenman _vm_map_clip_start(map, entry, start) 757c0877f10SJohn Dyson vm_map_t map; 758c0877f10SJohn Dyson vm_map_entry_t entry; 759c0877f10SJohn Dyson vm_offset_t start; 760df8bae1dSRodney W. Grimes { 761c0877f10SJohn Dyson vm_map_entry_t new_entry; 762df8bae1dSRodney W. Grimes 763df8bae1dSRodney W. Grimes /* 7640d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 7650d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 7660d94caffSDavid Greenman * starting address. 767df8bae1dSRodney W. Grimes */ 768df8bae1dSRodney W. Grimes 769f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 770f32dbbeeSJohn Dyson 77111cccda1SJohn Dyson /* 77211cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 77311cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 77411cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 77511cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 77611cccda1SJohn Dyson * put this improvement. 77711cccda1SJohn Dyson */ 77811cccda1SJohn Dyson 77911cccda1SJohn Dyson if (entry->object.vm_object == NULL) { 78011cccda1SJohn Dyson vm_object_t object; 78111cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 782c2e11a03SJohn Dyson atop(entry->end - entry->start)); 78311cccda1SJohn Dyson entry->object.vm_object = object; 78411cccda1SJohn Dyson entry->offset = 0; 78511cccda1SJohn Dyson } 78611cccda1SJohn Dyson 787df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 788df8bae1dSRodney W. Grimes *new_entry = *entry; 789df8bae1dSRodney W. Grimes 790df8bae1dSRodney W. Grimes new_entry->end = start; 791df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 792df8bae1dSRodney W. Grimes entry->start = start; 793df8bae1dSRodney W. Grimes 794df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 795df8bae1dSRodney W. Grimes 7969fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 797df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 798df8bae1dSRodney W. Grimes } 799c0877f10SJohn Dyson } 800df8bae1dSRodney W. Grimes 801df8bae1dSRodney W. Grimes /* 802df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 803df8bae1dSRodney W. Grimes * 804df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 805df8bae1dSRodney W. Grimes * the specified address; if necessary, 806df8bae1dSRodney W. Grimes * it splits the entry into two. 807df8bae1dSRodney W. Grimes */ 808df8bae1dSRodney W. Grimes 809df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 810df8bae1dSRodney W. Grimes { \ 811df8bae1dSRodney W. Grimes if (endaddr < entry->end) \ 812df8bae1dSRodney W. Grimes _vm_map_clip_end(map, entry, endaddr); \ 813df8bae1dSRodney W. Grimes } 814df8bae1dSRodney W. Grimes 815df8bae1dSRodney W. Grimes /* 816df8bae1dSRodney W. Grimes * This routine is called only when it is known that 817df8bae1dSRodney W. Grimes * the entry must be split. 818df8bae1dSRodney W. Grimes */ 8190d94caffSDavid Greenman static void 8200d94caffSDavid Greenman _vm_map_clip_end(map, entry, end) 821c0877f10SJohn Dyson vm_map_t map; 822c0877f10SJohn Dyson vm_map_entry_t entry; 823c0877f10SJohn Dyson vm_offset_t end; 824df8bae1dSRodney W. Grimes { 825c0877f10SJohn Dyson vm_map_entry_t new_entry; 826df8bae1dSRodney W. Grimes 827df8bae1dSRodney W. Grimes /* 82811cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 82911cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 83011cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 83111cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 83211cccda1SJohn Dyson * put this improvement. 83311cccda1SJohn Dyson */ 83411cccda1SJohn Dyson 83511cccda1SJohn Dyson if (entry->object.vm_object == NULL) { 83611cccda1SJohn Dyson vm_object_t object; 83711cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 838c2e11a03SJohn Dyson atop(entry->end - entry->start)); 83911cccda1SJohn Dyson entry->object.vm_object = object; 84011cccda1SJohn Dyson entry->offset = 0; 84111cccda1SJohn Dyson } 84211cccda1SJohn Dyson 84311cccda1SJohn Dyson /* 8440d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 845df8bae1dSRodney W. Grimes */ 846df8bae1dSRodney W. Grimes 847df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 848df8bae1dSRodney W. Grimes *new_entry = *entry; 849df8bae1dSRodney W. Grimes 850df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 851df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 852df8bae1dSRodney W. Grimes 853df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 854df8bae1dSRodney W. Grimes 8559fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 856df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 857df8bae1dSRodney W. Grimes } 858c0877f10SJohn Dyson } 859df8bae1dSRodney W. Grimes 860df8bae1dSRodney W. Grimes /* 861df8bae1dSRodney W. Grimes * VM_MAP_RANGE_CHECK: [ internal use only ] 862df8bae1dSRodney W. Grimes * 863df8bae1dSRodney W. Grimes * Asserts that the starting and ending region 864df8bae1dSRodney W. Grimes * addresses fall within the valid range of the map. 865df8bae1dSRodney W. Grimes */ 866df8bae1dSRodney W. Grimes #define VM_MAP_RANGE_CHECK(map, start, end) \ 867df8bae1dSRodney W. Grimes { \ 868df8bae1dSRodney W. Grimes if (start < vm_map_min(map)) \ 869df8bae1dSRodney W. Grimes start = vm_map_min(map); \ 870df8bae1dSRodney W. Grimes if (end > vm_map_max(map)) \ 871df8bae1dSRodney W. Grimes end = vm_map_max(map); \ 872df8bae1dSRodney W. Grimes if (start > end) \ 873df8bae1dSRodney W. Grimes start = end; \ 874df8bae1dSRodney W. Grimes } 875df8bae1dSRodney W. Grimes 876df8bae1dSRodney W. Grimes /* 877df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 878df8bae1dSRodney W. Grimes * 879df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 880df8bae1dSRodney W. Grimes * 881df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 882df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 883df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 884df8bae1dSRodney W. Grimes * 885df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 886df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 887df8bae1dSRodney W. Grimes * vm_fault 888df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 889df8bae1dSRodney W. Grimes * 890df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 891df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 892df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 893df8bae1dSRodney W. Grimes */ 894df8bae1dSRodney W. Grimes int 895df8bae1dSRodney W. Grimes vm_map_submap(map, start, end, submap) 896c0877f10SJohn Dyson vm_map_t map; 897c0877f10SJohn Dyson vm_offset_t start; 898c0877f10SJohn Dyson vm_offset_t end; 899df8bae1dSRodney W. Grimes vm_map_t submap; 900df8bae1dSRodney W. Grimes { 901df8bae1dSRodney W. Grimes vm_map_entry_t entry; 902c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 903df8bae1dSRodney W. Grimes 904df8bae1dSRodney W. Grimes vm_map_lock(map); 905df8bae1dSRodney W. Grimes 906df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 907df8bae1dSRodney W. Grimes 908df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 909df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 9100d94caffSDavid Greenman } else 911df8bae1dSRodney W. Grimes entry = entry->next; 912df8bae1dSRodney W. Grimes 913df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 914df8bae1dSRodney W. Grimes 915df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 9169fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 917afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 9182d8acc0fSJohn Dyson entry->object.sub_map = submap; 919afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 920df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 921df8bae1dSRodney W. Grimes } 922df8bae1dSRodney W. Grimes vm_map_unlock(map); 923df8bae1dSRodney W. Grimes 924df8bae1dSRodney W. Grimes return (result); 925df8bae1dSRodney W. Grimes } 926df8bae1dSRodney W. Grimes 927df8bae1dSRodney W. Grimes /* 928df8bae1dSRodney W. Grimes * vm_map_protect: 929df8bae1dSRodney W. Grimes * 930df8bae1dSRodney W. Grimes * Sets the protection of the specified address 931df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 932df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 933df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 934df8bae1dSRodney W. Grimes */ 935df8bae1dSRodney W. Grimes int 936b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 937b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 938df8bae1dSRodney W. Grimes { 939c0877f10SJohn Dyson vm_map_entry_t current; 940df8bae1dSRodney W. Grimes vm_map_entry_t entry; 941df8bae1dSRodney W. Grimes 942df8bae1dSRodney W. Grimes vm_map_lock(map); 943df8bae1dSRodney W. Grimes 944df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 945df8bae1dSRodney W. Grimes 946df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 947df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 948b7b2aac2SJohn Dyson } else { 949df8bae1dSRodney W. Grimes entry = entry->next; 950b7b2aac2SJohn Dyson } 951df8bae1dSRodney W. Grimes 952df8bae1dSRodney W. Grimes /* 9530d94caffSDavid Greenman * Make a first pass to check for protection violations. 954df8bae1dSRodney W. Grimes */ 955df8bae1dSRodney W. Grimes 956df8bae1dSRodney W. Grimes current = entry; 957df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 958afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 959a1f6d91cSDavid Greenman vm_map_unlock(map); 960df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 961a1f6d91cSDavid Greenman } 962df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 963df8bae1dSRodney W. Grimes vm_map_unlock(map); 964df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 965df8bae1dSRodney W. Grimes } 966df8bae1dSRodney W. Grimes current = current->next; 967df8bae1dSRodney W. Grimes } 968df8bae1dSRodney W. Grimes 969df8bae1dSRodney W. Grimes /* 9700d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 9710d94caffSDavid Greenman * necessary the second time.] 972df8bae1dSRodney W. Grimes */ 973df8bae1dSRodney W. Grimes 974df8bae1dSRodney W. Grimes current = entry; 975df8bae1dSRodney W. Grimes 976df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 977df8bae1dSRodney W. Grimes vm_prot_t old_prot; 978df8bae1dSRodney W. Grimes 979df8bae1dSRodney W. Grimes vm_map_clip_end(map, current, end); 980df8bae1dSRodney W. Grimes 981df8bae1dSRodney W. Grimes old_prot = current->protection; 982df8bae1dSRodney W. Grimes if (set_max) 983df8bae1dSRodney W. Grimes current->protection = 984df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 985df8bae1dSRodney W. Grimes old_prot; 986df8bae1dSRodney W. Grimes else 987df8bae1dSRodney W. Grimes current->protection = new_prot; 988df8bae1dSRodney W. Grimes 989df8bae1dSRodney W. Grimes /* 9900d94caffSDavid Greenman * Update physical map if necessary. Worry about copy-on-write 9910d94caffSDavid Greenman * here -- CHECK THIS XXX 992df8bae1dSRodney W. Grimes */ 993df8bae1dSRodney W. Grimes 994df8bae1dSRodney W. Grimes if (current->protection != old_prot) { 995afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 996df8bae1dSRodney W. Grimes VM_PROT_ALL) 997df8bae1dSRodney W. Grimes 998df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 999df8bae1dSRodney W. Grimes current->end, 10001c85e3dfSAlan Cox current->protection & MASK(current)); 1001df8bae1dSRodney W. Grimes #undef MASK 1002df8bae1dSRodney W. Grimes } 10037d78abc9SJohn Dyson 10047d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 10057d78abc9SJohn Dyson 1006df8bae1dSRodney W. Grimes current = current->next; 1007df8bae1dSRodney W. Grimes } 1008df8bae1dSRodney W. Grimes 1009df8bae1dSRodney W. Grimes vm_map_unlock(map); 1010df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1011df8bae1dSRodney W. Grimes } 1012df8bae1dSRodney W. Grimes 1013df8bae1dSRodney W. Grimes /* 1014867a482dSJohn Dyson * vm_map_madvise: 1015867a482dSJohn Dyson * 1016867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 1017f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 1018f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 1019f7fc307aSAlan Cox * objects. 1020867a482dSJohn Dyson */ 1021b4309055SMatthew Dillon 1022b4309055SMatthew Dillon int 1023f7fc307aSAlan Cox vm_map_madvise(map, start, end, behav) 1024867a482dSJohn Dyson vm_map_t map; 1025867a482dSJohn Dyson vm_offset_t start, end; 1026f7fc307aSAlan Cox int behav; 1027867a482dSJohn Dyson { 1028f7fc307aSAlan Cox vm_map_entry_t current, entry; 1029b4309055SMatthew Dillon int modify_map = 0; 1030867a482dSJohn Dyson 1031b4309055SMatthew Dillon /* 1032b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 1033b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 1034b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 1035b4309055SMatthew Dillon * on the map. 1036b4309055SMatthew Dillon */ 1037f7fc307aSAlan Cox 1038b4309055SMatthew Dillon switch(behav) { 1039b4309055SMatthew Dillon case MADV_NORMAL: 1040b4309055SMatthew Dillon case MADV_SEQUENTIAL: 1041b4309055SMatthew Dillon case MADV_RANDOM: 10424f79d873SMatthew Dillon case MADV_NOSYNC: 10434f79d873SMatthew Dillon case MADV_AUTOSYNC: 10449730a5daSPaul Saab case MADV_NOCORE: 10459730a5daSPaul Saab case MADV_CORE: 1046b4309055SMatthew Dillon modify_map = 1; 1047867a482dSJohn Dyson vm_map_lock(map); 1048b4309055SMatthew Dillon break; 1049b4309055SMatthew Dillon case MADV_WILLNEED: 1050b4309055SMatthew Dillon case MADV_DONTNEED: 1051b4309055SMatthew Dillon case MADV_FREE: 1052f7fc307aSAlan Cox vm_map_lock_read(map); 1053b4309055SMatthew Dillon break; 1054b4309055SMatthew Dillon default: 1055b4309055SMatthew Dillon return (KERN_INVALID_ARGUMENT); 1056b4309055SMatthew Dillon } 1057b4309055SMatthew Dillon 1058b4309055SMatthew Dillon /* 1059b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 1060b4309055SMatthew Dillon */ 1061867a482dSJohn Dyson 1062867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 1063867a482dSJohn Dyson 1064867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 1065f7fc307aSAlan Cox if (modify_map) 1066867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 1067b4309055SMatthew Dillon } else { 1068867a482dSJohn Dyson entry = entry->next; 1069b4309055SMatthew Dillon } 1070867a482dSJohn Dyson 1071f7fc307aSAlan Cox if (modify_map) { 1072f7fc307aSAlan Cox /* 1073f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 1074f7fc307aSAlan Cox * 1075f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 1076f7fc307aSAlan Cox * limited to the specified address range. 1077f7fc307aSAlan Cox */ 1078867a482dSJohn Dyson for (current = entry; 1079867a482dSJohn Dyson (current != &map->header) && (current->start < end); 1080b4309055SMatthew Dillon current = current->next 1081b4309055SMatthew Dillon ) { 1082f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1083867a482dSJohn Dyson continue; 1084fed9a903SJohn Dyson 108547221757SJohn Dyson vm_map_clip_end(map, current, end); 1086fed9a903SJohn Dyson 1087f7fc307aSAlan Cox switch (behav) { 1088867a482dSJohn Dyson case MADV_NORMAL: 10897f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1090867a482dSJohn Dyson break; 1091867a482dSJohn Dyson case MADV_SEQUENTIAL: 10927f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1093867a482dSJohn Dyson break; 1094867a482dSJohn Dyson case MADV_RANDOM: 10957f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1096867a482dSJohn Dyson break; 10974f79d873SMatthew Dillon case MADV_NOSYNC: 10984f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 10994f79d873SMatthew Dillon break; 11004f79d873SMatthew Dillon case MADV_AUTOSYNC: 11014f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 11024f79d873SMatthew Dillon break; 11039730a5daSPaul Saab case MADV_NOCORE: 11049730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 11059730a5daSPaul Saab break; 11069730a5daSPaul Saab case MADV_CORE: 11079730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 11089730a5daSPaul Saab break; 1109867a482dSJohn Dyson default: 1110867a482dSJohn Dyson break; 1111867a482dSJohn Dyson } 1112f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 1113867a482dSJohn Dyson } 1114867a482dSJohn Dyson vm_map_unlock(map); 1115b4309055SMatthew Dillon } else { 1116f7fc307aSAlan Cox vm_pindex_t pindex; 1117f7fc307aSAlan Cox int count; 1118f7fc307aSAlan Cox 1119f7fc307aSAlan Cox /* 1120f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 1121f7fc307aSAlan Cox * vm_object. 1122f7fc307aSAlan Cox * 1123f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 1124f7fc307aSAlan Cox * the vm_object pindex and count. 1125f7fc307aSAlan Cox */ 1126f7fc307aSAlan Cox for (current = entry; 1127f7fc307aSAlan Cox (current != &map->header) && (current->start < end); 1128b4309055SMatthew Dillon current = current->next 1129b4309055SMatthew Dillon ) { 1130f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1131f7fc307aSAlan Cox continue; 1132f7fc307aSAlan Cox 1133f7fc307aSAlan Cox pindex = OFF_TO_IDX(current->offset); 1134f7fc307aSAlan Cox count = atop(current->end - current->start); 1135f7fc307aSAlan Cox 1136f7fc307aSAlan Cox if (current->start < start) { 1137f7fc307aSAlan Cox pindex += atop(start - current->start); 1138f7fc307aSAlan Cox count -= atop(start - current->start); 1139f7fc307aSAlan Cox } 1140f7fc307aSAlan Cox if (current->end > end) 1141f7fc307aSAlan Cox count -= atop(current->end - end); 1142f7fc307aSAlan Cox 1143f7fc307aSAlan Cox if (count <= 0) 1144f7fc307aSAlan Cox continue; 1145f7fc307aSAlan Cox 1146f7fc307aSAlan Cox vm_object_madvise(current->object.vm_object, 1147f7fc307aSAlan Cox pindex, count, behav); 1148b4309055SMatthew Dillon if (behav == MADV_WILLNEED) { 1149b4309055SMatthew Dillon pmap_object_init_pt( 1150b4309055SMatthew Dillon map->pmap, 1151b4309055SMatthew Dillon current->start, 1152f7fc307aSAlan Cox current->object.vm_object, 1153b4309055SMatthew Dillon pindex, 1154b4309055SMatthew Dillon (count << PAGE_SHIFT), 1155b4309055SMatthew Dillon 0 1156b4309055SMatthew Dillon ); 1157f7fc307aSAlan Cox } 1158f7fc307aSAlan Cox } 1159f7fc307aSAlan Cox vm_map_unlock_read(map); 1160f7fc307aSAlan Cox } 1161b4309055SMatthew Dillon return(0); 1162867a482dSJohn Dyson } 1163867a482dSJohn Dyson 1164867a482dSJohn Dyson 1165867a482dSJohn Dyson /* 1166df8bae1dSRodney W. Grimes * vm_map_inherit: 1167df8bae1dSRodney W. Grimes * 1168df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 1169df8bae1dSRodney W. Grimes * range in the target map. Inheritance 1170df8bae1dSRodney W. Grimes * affects how the map will be shared with 1171df8bae1dSRodney W. Grimes * child maps at the time of vm_map_fork. 1172df8bae1dSRodney W. Grimes */ 1173df8bae1dSRodney W. Grimes int 1174b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1175b9dcd593SBruce Evans vm_inherit_t new_inheritance) 1176df8bae1dSRodney W. Grimes { 1177c0877f10SJohn Dyson vm_map_entry_t entry; 1178df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 1179df8bae1dSRodney W. Grimes 1180df8bae1dSRodney W. Grimes switch (new_inheritance) { 1181df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 1182df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 1183df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 1184df8bae1dSRodney W. Grimes break; 1185df8bae1dSRodney W. Grimes default: 1186df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes 1189df8bae1dSRodney W. Grimes vm_map_lock(map); 1190df8bae1dSRodney W. Grimes 1191df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1192df8bae1dSRodney W. Grimes 1193df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 1194df8bae1dSRodney W. Grimes entry = temp_entry; 1195df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 11960d94caffSDavid Greenman } else 1197df8bae1dSRodney W. Grimes entry = temp_entry->next; 1198df8bae1dSRodney W. Grimes 1199df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1200df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1201df8bae1dSRodney W. Grimes 1202df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 1203df8bae1dSRodney W. Grimes 120444428f62SAlan Cox vm_map_simplify_entry(map, entry); 120544428f62SAlan Cox 1206df8bae1dSRodney W. Grimes entry = entry->next; 1207df8bae1dSRodney W. Grimes } 1208df8bae1dSRodney W. Grimes 1209df8bae1dSRodney W. Grimes vm_map_unlock(map); 1210df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1211df8bae1dSRodney W. Grimes } 1212df8bae1dSRodney W. Grimes 1213df8bae1dSRodney W. Grimes /* 12147aaaa4fdSJohn Dyson * Implement the semantics of mlock 12157aaaa4fdSJohn Dyson */ 12167aaaa4fdSJohn Dyson int 12177aaaa4fdSJohn Dyson vm_map_user_pageable(map, start, end, new_pageable) 1218c0877f10SJohn Dyson vm_map_t map; 1219c0877f10SJohn Dyson vm_offset_t start; 1220c0877f10SJohn Dyson vm_offset_t end; 1221c0877f10SJohn Dyson boolean_t new_pageable; 12227aaaa4fdSJohn Dyson { 1223b44959ceSTor Egge vm_map_entry_t entry; 12247aaaa4fdSJohn Dyson vm_map_entry_t start_entry; 1225b44959ceSTor Egge vm_offset_t estart; 12267aaaa4fdSJohn Dyson int rv; 12277aaaa4fdSJohn Dyson 12287aaaa4fdSJohn Dyson vm_map_lock(map); 12297aaaa4fdSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 12307aaaa4fdSJohn Dyson 12317aaaa4fdSJohn Dyson if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 12327aaaa4fdSJohn Dyson vm_map_unlock(map); 12337aaaa4fdSJohn Dyson return (KERN_INVALID_ADDRESS); 12347aaaa4fdSJohn Dyson } 12357aaaa4fdSJohn Dyson 12367aaaa4fdSJohn Dyson if (new_pageable) { 12377aaaa4fdSJohn Dyson 12387aaaa4fdSJohn Dyson entry = start_entry; 12397aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 12407aaaa4fdSJohn Dyson 12417aaaa4fdSJohn Dyson /* 12427aaaa4fdSJohn Dyson * Now decrement the wiring count for each region. If a region 12437aaaa4fdSJohn Dyson * becomes completely unwired, unwire its physical pages and 12447aaaa4fdSJohn Dyson * mappings. 12457aaaa4fdSJohn Dyson */ 12467aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 1247afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 12487aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 1249afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 12507aaaa4fdSJohn Dyson entry->wired_count--; 12517aaaa4fdSJohn Dyson if (entry->wired_count == 0) 12527aaaa4fdSJohn Dyson vm_fault_unwire(map, entry->start, entry->end); 12537aaaa4fdSJohn Dyson } 1254b44959ceSTor Egge vm_map_simplify_entry(map,entry); 12557aaaa4fdSJohn Dyson entry = entry->next; 12567aaaa4fdSJohn Dyson } 12577aaaa4fdSJohn Dyson } else { 12587aaaa4fdSJohn Dyson 12597aaaa4fdSJohn Dyson entry = start_entry; 12607aaaa4fdSJohn Dyson 12617aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 12627aaaa4fdSJohn Dyson 1263afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 12647aaaa4fdSJohn Dyson entry = entry->next; 12657aaaa4fdSJohn Dyson continue; 12667aaaa4fdSJohn Dyson } 12677aaaa4fdSJohn Dyson 12687aaaa4fdSJohn Dyson if (entry->wired_count != 0) { 12697aaaa4fdSJohn Dyson entry->wired_count++; 1270afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 12717aaaa4fdSJohn Dyson entry = entry->next; 12727aaaa4fdSJohn Dyson continue; 12737aaaa4fdSJohn Dyson } 12747aaaa4fdSJohn Dyson 12757aaaa4fdSJohn Dyson /* Here on entry being newly wired */ 12767aaaa4fdSJohn Dyson 12779fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1278afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 12797aaaa4fdSJohn Dyson if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 12807aaaa4fdSJohn Dyson 12817aaaa4fdSJohn Dyson vm_object_shadow(&entry->object.vm_object, 12827aaaa4fdSJohn Dyson &entry->offset, 1283c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1284afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 12857aaaa4fdSJohn Dyson 12867aaaa4fdSJohn Dyson } else if (entry->object.vm_object == NULL) { 12877aaaa4fdSJohn Dyson 12887aaaa4fdSJohn Dyson entry->object.vm_object = 12897aaaa4fdSJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1290c2e11a03SJohn Dyson atop(entry->end - entry->start)); 12917aaaa4fdSJohn Dyson entry->offset = (vm_offset_t) 0; 12927aaaa4fdSJohn Dyson 12937aaaa4fdSJohn Dyson } 12947aaaa4fdSJohn Dyson } 12957aaaa4fdSJohn Dyson 12967aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 12977aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 12987aaaa4fdSJohn Dyson 12997aaaa4fdSJohn Dyson entry->wired_count++; 1300afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 1301b44959ceSTor Egge estart = entry->start; 13027aaaa4fdSJohn Dyson 13037aaaa4fdSJohn Dyson /* First we need to allow map modifications */ 1304996c772fSJohn Dyson vm_map_set_recursive(map); 130503e9c6c1SJohn Dyson vm_map_lock_downgrade(map); 130647221757SJohn Dyson map->timestamp++; 13077aaaa4fdSJohn Dyson 13087aaaa4fdSJohn Dyson rv = vm_fault_user_wire(map, entry->start, entry->end); 13097aaaa4fdSJohn Dyson if (rv) { 13107aaaa4fdSJohn Dyson 13117aaaa4fdSJohn Dyson entry->wired_count--; 1312afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 13137aaaa4fdSJohn Dyson 1314996c772fSJohn Dyson vm_map_clear_recursive(map); 13157aaaa4fdSJohn Dyson vm_map_unlock(map); 13167aaaa4fdSJohn Dyson 13177aaaa4fdSJohn Dyson (void) vm_map_user_pageable(map, start, entry->start, TRUE); 13187aaaa4fdSJohn Dyson return rv; 13197aaaa4fdSJohn Dyson } 13207aaaa4fdSJohn Dyson 1321996c772fSJohn Dyson vm_map_clear_recursive(map); 1322b44959ceSTor Egge if (vm_map_lock_upgrade(map)) { 1323b44959ceSTor Egge vm_map_lock(map); 1324b44959ceSTor Egge if (vm_map_lookup_entry(map, estart, &entry) 1325b44959ceSTor Egge == FALSE) { 1326b44959ceSTor Egge vm_map_unlock(map); 1327b44959ceSTor Egge (void) vm_map_user_pageable(map, 1328b44959ceSTor Egge start, 1329b44959ceSTor Egge estart, 1330b44959ceSTor Egge TRUE); 1331b44959ceSTor Egge return (KERN_INVALID_ADDRESS); 1332b44959ceSTor Egge } 1333b44959ceSTor Egge } 1334b44959ceSTor Egge vm_map_simplify_entry(map,entry); 13357aaaa4fdSJohn Dyson } 13367aaaa4fdSJohn Dyson } 133747221757SJohn Dyson map->timestamp++; 13387aaaa4fdSJohn Dyson vm_map_unlock(map); 13397aaaa4fdSJohn Dyson return KERN_SUCCESS; 13407aaaa4fdSJohn Dyson } 13417aaaa4fdSJohn Dyson 13427aaaa4fdSJohn Dyson /* 1343df8bae1dSRodney W. Grimes * vm_map_pageable: 1344df8bae1dSRodney W. Grimes * 1345df8bae1dSRodney W. Grimes * Sets the pageability of the specified address 1346df8bae1dSRodney W. Grimes * range in the target map. Regions specified 1347df8bae1dSRodney W. Grimes * as not pageable require locked-down physical 1348df8bae1dSRodney W. Grimes * memory and physical page maps. 1349df8bae1dSRodney W. Grimes * 1350df8bae1dSRodney W. Grimes * The map must not be locked, but a reference 1351df8bae1dSRodney W. Grimes * must remain to the map throughout the call. 1352df8bae1dSRodney W. Grimes */ 1353df8bae1dSRodney W. Grimes int 1354df8bae1dSRodney W. Grimes vm_map_pageable(map, start, end, new_pageable) 1355c0877f10SJohn Dyson vm_map_t map; 1356c0877f10SJohn Dyson vm_offset_t start; 1357c0877f10SJohn Dyson vm_offset_t end; 1358c0877f10SJohn Dyson boolean_t new_pageable; 1359df8bae1dSRodney W. Grimes { 1360c0877f10SJohn Dyson vm_map_entry_t entry; 1361df8bae1dSRodney W. Grimes vm_map_entry_t start_entry; 1362c0877f10SJohn Dyson vm_offset_t failed = 0; 1363df8bae1dSRodney W. Grimes int rv; 1364df8bae1dSRodney W. Grimes 1365df8bae1dSRodney W. Grimes vm_map_lock(map); 1366df8bae1dSRodney W. Grimes 1367df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1368df8bae1dSRodney W. Grimes 1369df8bae1dSRodney W. Grimes /* 13700d94caffSDavid Greenman * Only one pageability change may take place at one time, since 13710d94caffSDavid Greenman * vm_fault assumes it will be called only once for each 13720d94caffSDavid Greenman * wiring/unwiring. Therefore, we have to make sure we're actually 13730d94caffSDavid Greenman * changing the pageability for the entire region. We do so before 13740d94caffSDavid Greenman * making any changes. 1375df8bae1dSRodney W. Grimes */ 1376df8bae1dSRodney W. Grimes 1377df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1378df8bae1dSRodney W. Grimes vm_map_unlock(map); 1379df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1380df8bae1dSRodney W. Grimes } 1381df8bae1dSRodney W. Grimes entry = start_entry; 1382df8bae1dSRodney W. Grimes 1383df8bae1dSRodney W. Grimes /* 13840d94caffSDavid Greenman * Actions are rather different for wiring and unwiring, so we have 13850d94caffSDavid Greenman * two separate cases. 1386df8bae1dSRodney W. Grimes */ 1387df8bae1dSRodney W. Grimes 1388df8bae1dSRodney W. Grimes if (new_pageable) { 1389df8bae1dSRodney W. Grimes 1390df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1391df8bae1dSRodney W. Grimes 1392df8bae1dSRodney W. Grimes /* 13930d94caffSDavid Greenman * Unwiring. First ensure that the range to be unwired is 13940d94caffSDavid Greenman * really wired down and that there are no holes. 1395df8bae1dSRodney W. Grimes */ 1396df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1397df8bae1dSRodney W. Grimes 1398df8bae1dSRodney W. Grimes if (entry->wired_count == 0 || 1399df8bae1dSRodney W. Grimes (entry->end < end && 1400df8bae1dSRodney W. Grimes (entry->next == &map->header || 1401df8bae1dSRodney W. Grimes entry->next->start > entry->end))) { 1402df8bae1dSRodney W. Grimes vm_map_unlock(map); 1403df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1404df8bae1dSRodney W. Grimes } 1405df8bae1dSRodney W. Grimes entry = entry->next; 1406df8bae1dSRodney W. Grimes } 1407df8bae1dSRodney W. Grimes 1408df8bae1dSRodney W. Grimes /* 14090d94caffSDavid Greenman * Now decrement the wiring count for each region. If a region 14100d94caffSDavid Greenman * becomes completely unwired, unwire its physical pages and 14110d94caffSDavid Greenman * mappings. 1412df8bae1dSRodney W. Grimes */ 1413df8bae1dSRodney W. Grimes entry = start_entry; 1414df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1415df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1416df8bae1dSRodney W. Grimes 1417df8bae1dSRodney W. Grimes entry->wired_count--; 1418df8bae1dSRodney W. Grimes if (entry->wired_count == 0) 1419df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 1420df8bae1dSRodney W. Grimes 142144428f62SAlan Cox vm_map_simplify_entry(map, entry); 142244428f62SAlan Cox 1423df8bae1dSRodney W. Grimes entry = entry->next; 1424df8bae1dSRodney W. Grimes } 14250d94caffSDavid Greenman } else { 1426df8bae1dSRodney W. Grimes /* 1427df8bae1dSRodney W. Grimes * Wiring. We must do this in two passes: 1428df8bae1dSRodney W. Grimes * 14290d94caffSDavid Greenman * 1. Holding the write lock, we create any shadow or zero-fill 14300d94caffSDavid Greenman * objects that need to be created. Then we clip each map 14310d94caffSDavid Greenman * entry to the region to be wired and increment its wiring 14320d94caffSDavid Greenman * count. We create objects before clipping the map entries 1433df8bae1dSRodney W. Grimes * to avoid object proliferation. 1434df8bae1dSRodney W. Grimes * 14350d94caffSDavid Greenman * 2. We downgrade to a read lock, and call vm_fault_wire to 14360d94caffSDavid Greenman * fault in the pages for any newly wired area (wired_count is 14370d94caffSDavid Greenman * 1). 1438df8bae1dSRodney W. Grimes * 14390d94caffSDavid Greenman * Downgrading to a read lock for vm_fault_wire avoids a possible 144024a1cce3SDavid Greenman * deadlock with another process that may have faulted on one 14410d94caffSDavid Greenman * of the pages to be wired (it would mark the page busy, 14420d94caffSDavid Greenman * blocking us, then in turn block on the map lock that we 14430d94caffSDavid Greenman * hold). Because of problems in the recursive lock package, 14440d94caffSDavid Greenman * we cannot upgrade to a write lock in vm_map_lookup. Thus, 14450d94caffSDavid Greenman * any actions that require the write lock must be done 14460d94caffSDavid Greenman * beforehand. Because we keep the read lock on the map, the 14470d94caffSDavid Greenman * copy-on-write status of the entries we modify here cannot 14480d94caffSDavid Greenman * change. 1449df8bae1dSRodney W. Grimes */ 1450df8bae1dSRodney W. Grimes 1451df8bae1dSRodney W. Grimes /* 1452df8bae1dSRodney W. Grimes * Pass 1. 1453df8bae1dSRodney W. Grimes */ 1454df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1455df8bae1dSRodney W. Grimes if (entry->wired_count == 0) { 1456df8bae1dSRodney W. Grimes 1457df8bae1dSRodney W. Grimes /* 1458df8bae1dSRodney W. Grimes * Perform actions of vm_map_lookup that need 1459df8bae1dSRodney W. Grimes * the write lock on the map: create a shadow 1460df8bae1dSRodney W. Grimes * object for a copy-on-write region, or an 1461df8bae1dSRodney W. Grimes * object for a zero-fill region. 1462df8bae1dSRodney W. Grimes * 1463df8bae1dSRodney W. Grimes * We don't have to do this for entries that 1464ad5fca3bSAlan Cox * point to sub maps, because we won't 1465ad5fca3bSAlan Cox * hold the lock on the sub map. 1466df8bae1dSRodney W. Grimes */ 14679fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1468afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1469b5b40fa6SJohn Dyson if (copyflag && 1470df8bae1dSRodney W. Grimes ((entry->protection & VM_PROT_WRITE) != 0)) { 1471df8bae1dSRodney W. Grimes 1472df8bae1dSRodney W. Grimes vm_object_shadow(&entry->object.vm_object, 1473df8bae1dSRodney W. Grimes &entry->offset, 1474c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1475afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 14760d94caffSDavid Greenman } else if (entry->object.vm_object == NULL) { 1477df8bae1dSRodney W. Grimes entry->object.vm_object = 1478a316d390SJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1479c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1480df8bae1dSRodney W. Grimes entry->offset = (vm_offset_t) 0; 1481df8bae1dSRodney W. Grimes } 1482df8bae1dSRodney W. Grimes } 1483df8bae1dSRodney W. Grimes } 1484df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1485df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1486df8bae1dSRodney W. Grimes entry->wired_count++; 1487df8bae1dSRodney W. Grimes 1488df8bae1dSRodney W. Grimes /* 1489df8bae1dSRodney W. Grimes * Check for holes 1490df8bae1dSRodney W. Grimes */ 1491df8bae1dSRodney W. Grimes if (entry->end < end && 1492df8bae1dSRodney W. Grimes (entry->next == &map->header || 1493df8bae1dSRodney W. Grimes entry->next->start > entry->end)) { 1494df8bae1dSRodney W. Grimes /* 14950d94caffSDavid Greenman * Found one. Object creation actions do not 14960d94caffSDavid Greenman * need to be undone, but the wired counts 14970d94caffSDavid Greenman * need to be restored. 1498df8bae1dSRodney W. Grimes */ 1499df8bae1dSRodney W. Grimes while (entry != &map->header && entry->end > start) { 1500df8bae1dSRodney W. Grimes entry->wired_count--; 1501df8bae1dSRodney W. Grimes entry = entry->prev; 1502df8bae1dSRodney W. Grimes } 1503df8bae1dSRodney W. Grimes vm_map_unlock(map); 1504df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1505df8bae1dSRodney W. Grimes } 1506df8bae1dSRodney W. Grimes entry = entry->next; 1507df8bae1dSRodney W. Grimes } 1508df8bae1dSRodney W. Grimes 1509df8bae1dSRodney W. Grimes /* 1510df8bae1dSRodney W. Grimes * Pass 2. 1511df8bae1dSRodney W. Grimes */ 1512df8bae1dSRodney W. Grimes 1513df8bae1dSRodney W. Grimes /* 1514df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1515df8bae1dSRodney W. Grimes * 151624a1cce3SDavid Greenman * If we are wiring in the kernel map or a submap of it, 151724a1cce3SDavid Greenman * unlock the map to avoid deadlocks. We trust that the 151824a1cce3SDavid Greenman * kernel is well-behaved, and therefore will not do 151924a1cce3SDavid Greenman * anything destructive to this region of the map while 152024a1cce3SDavid Greenman * we have it unlocked. We cannot trust user processes 152124a1cce3SDavid Greenman * to do the same. 1522df8bae1dSRodney W. Grimes * 1523df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1524df8bae1dSRodney W. Grimes */ 1525df8bae1dSRodney W. Grimes if (vm_map_pmap(map) == kernel_pmap) { 1526df8bae1dSRodney W. Grimes vm_map_unlock(map); /* trust me ... */ 15270d94caffSDavid Greenman } else { 152803e9c6c1SJohn Dyson vm_map_lock_downgrade(map); 1529df8bae1dSRodney W. Grimes } 1530df8bae1dSRodney W. Grimes 1531df8bae1dSRodney W. Grimes rv = 0; 1532df8bae1dSRodney W. Grimes entry = start_entry; 1533df8bae1dSRodney W. Grimes while (entry != &map->header && entry->start < end) { 1534df8bae1dSRodney W. Grimes /* 15350d94caffSDavid Greenman * If vm_fault_wire fails for any page we need to undo 15360d94caffSDavid Greenman * what has been done. We decrement the wiring count 15370d94caffSDavid Greenman * for those pages which have not yet been wired (now) 15380d94caffSDavid Greenman * and unwire those that have (later). 1539df8bae1dSRodney W. Grimes * 1540df8bae1dSRodney W. Grimes * XXX this violates the locking protocol on the map, 1541df8bae1dSRodney W. Grimes * needs to be fixed. 1542df8bae1dSRodney W. Grimes */ 1543df8bae1dSRodney W. Grimes if (rv) 1544df8bae1dSRodney W. Grimes entry->wired_count--; 1545df8bae1dSRodney W. Grimes else if (entry->wired_count == 1) { 1546df8bae1dSRodney W. Grimes rv = vm_fault_wire(map, entry->start, entry->end); 1547df8bae1dSRodney W. Grimes if (rv) { 1548df8bae1dSRodney W. Grimes failed = entry->start; 1549df8bae1dSRodney W. Grimes entry->wired_count--; 1550df8bae1dSRodney W. Grimes } 1551df8bae1dSRodney W. Grimes } 1552df8bae1dSRodney W. Grimes entry = entry->next; 1553df8bae1dSRodney W. Grimes } 1554df8bae1dSRodney W. Grimes 1555df8bae1dSRodney W. Grimes if (vm_map_pmap(map) == kernel_pmap) { 1556df8bae1dSRodney W. Grimes vm_map_lock(map); 1557df8bae1dSRodney W. Grimes } 1558df8bae1dSRodney W. Grimes if (rv) { 1559df8bae1dSRodney W. Grimes vm_map_unlock(map); 1560df8bae1dSRodney W. Grimes (void) vm_map_pageable(map, start, failed, TRUE); 1561df8bae1dSRodney W. Grimes return (rv); 1562df8bae1dSRodney W. Grimes } 1563b7b2aac2SJohn Dyson vm_map_simplify_entry(map, start_entry); 1564df8bae1dSRodney W. Grimes } 1565df8bae1dSRodney W. Grimes 1566df8bae1dSRodney W. Grimes vm_map_unlock(map); 1567df8bae1dSRodney W. Grimes 1568df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1569df8bae1dSRodney W. Grimes } 1570df8bae1dSRodney W. Grimes 1571df8bae1dSRodney W. Grimes /* 1572df8bae1dSRodney W. Grimes * vm_map_clean 1573df8bae1dSRodney W. Grimes * 1574df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 1575df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 1576df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 1577df8bae1dSRodney W. Grimes * 1578df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 1579df8bae1dSRodney W. Grimes */ 1580df8bae1dSRodney W. Grimes int 1581df8bae1dSRodney W. Grimes vm_map_clean(map, start, end, syncio, invalidate) 1582df8bae1dSRodney W. Grimes vm_map_t map; 1583df8bae1dSRodney W. Grimes vm_offset_t start; 1584df8bae1dSRodney W. Grimes vm_offset_t end; 1585df8bae1dSRodney W. Grimes boolean_t syncio; 1586df8bae1dSRodney W. Grimes boolean_t invalidate; 1587df8bae1dSRodney W. Grimes { 1588c0877f10SJohn Dyson vm_map_entry_t current; 1589df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1590df8bae1dSRodney W. Grimes vm_size_t size; 1591df8bae1dSRodney W. Grimes vm_object_t object; 1592a316d390SJohn Dyson vm_ooffset_t offset; 1593df8bae1dSRodney W. Grimes 1594df8bae1dSRodney W. Grimes vm_map_lock_read(map); 1595df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1596df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 1597df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1598df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1599df8bae1dSRodney W. Grimes } 1600df8bae1dSRodney W. Grimes /* 1601df8bae1dSRodney W. Grimes * Make a first pass to check for holes. 1602df8bae1dSRodney W. Grimes */ 1603df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1604afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1605df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1606df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1607df8bae1dSRodney W. Grimes } 1608df8bae1dSRodney W. Grimes if (end > current->end && 1609df8bae1dSRodney W. Grimes (current->next == &map->header || 1610df8bae1dSRodney W. Grimes current->end != current->next->start)) { 1611df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1612df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1613df8bae1dSRodney W. Grimes } 1614df8bae1dSRodney W. Grimes } 1615df8bae1dSRodney W. Grimes 1616cf2819ccSJohn Dyson if (invalidate) 1617cf2819ccSJohn Dyson pmap_remove(vm_map_pmap(map), start, end); 1618df8bae1dSRodney W. Grimes /* 1619df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 1620df8bae1dSRodney W. Grimes * objects as we go. 1621df8bae1dSRodney W. Grimes */ 1622df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1623df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 1624df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 16259fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1626c0877f10SJohn Dyson vm_map_t smap; 1627df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 1628df8bae1dSRodney W. Grimes vm_size_t tsize; 1629df8bae1dSRodney W. Grimes 16309fdfe602SMatthew Dillon smap = current->object.sub_map; 1631df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 1632df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 1633df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 1634df8bae1dSRodney W. Grimes if (tsize < size) 1635df8bae1dSRodney W. Grimes size = tsize; 1636df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 1637df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 1638df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 1639df8bae1dSRodney W. Grimes } else { 1640df8bae1dSRodney W. Grimes object = current->object.vm_object; 1641df8bae1dSRodney W. Grimes } 16428a02c104SJohn Dyson /* 16438a02c104SJohn Dyson * Note that there is absolutely no sense in writing out 16448a02c104SJohn Dyson * anonymous objects, so we track down the vnode object 16458a02c104SJohn Dyson * to write out. 16468a02c104SJohn Dyson * We invalidate (remove) all pages from the address space 16478a02c104SJohn Dyson * anyway, for semantic correctness. 16488a02c104SJohn Dyson */ 16498a02c104SJohn Dyson while (object->backing_object) { 16508a02c104SJohn Dyson object = object->backing_object; 16518a02c104SJohn Dyson offset += object->backing_object_offset; 16528a02c104SJohn Dyson if (object->size < OFF_TO_IDX( offset + size)) 16538a02c104SJohn Dyson size = IDX_TO_OFF(object->size) - offset; 16548a02c104SJohn Dyson } 1655ff359f84SMatthew Dillon if (object && (object->type == OBJT_VNODE) && 1656ff359f84SMatthew Dillon (current->protection & VM_PROT_WRITE)) { 1657df8bae1dSRodney W. Grimes /* 1658ff359f84SMatthew Dillon * Flush pages if writing is allowed, invalidate them 1659ff359f84SMatthew Dillon * if invalidation requested. Pages undergoing I/O 1660ff359f84SMatthew Dillon * will be ignored by vm_object_page_remove(). 1661f5cf85d4SDavid Greenman * 1662ff359f84SMatthew Dillon * We cannot lock the vnode and then wait for paging 1663ff359f84SMatthew Dillon * to complete without deadlocking against vm_fault. 1664ff359f84SMatthew Dillon * Instead we simply call vm_object_page_remove() and 1665ff359f84SMatthew Dillon * allow it to block internally on a page-by-page 1666ff359f84SMatthew Dillon * basis when it encounters pages undergoing async 1667ff359f84SMatthew Dillon * I/O. 1668df8bae1dSRodney W. Grimes */ 16698f9110f6SJohn Dyson int flags; 1670ff359f84SMatthew Dillon 1671ff359f84SMatthew Dillon vm_object_reference(object); 1672157ac55fSJohn Dyson vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curproc); 16738f9110f6SJohn Dyson flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 16748f9110f6SJohn Dyson flags |= invalidate ? OBJPC_INVAL : 0; 1675a316d390SJohn Dyson vm_object_page_clean(object, 1676a316d390SJohn Dyson OFF_TO_IDX(offset), 16772be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 16788f9110f6SJohn Dyson flags); 1679cf2819ccSJohn Dyson if (invalidate) { 1680ff359f84SMatthew Dillon /*vm_object_pip_wait(object, "objmcl");*/ 1681a316d390SJohn Dyson vm_object_page_remove(object, 1682a316d390SJohn Dyson OFF_TO_IDX(offset), 16832be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 1684a316d390SJohn Dyson FALSE); 1685cf2819ccSJohn Dyson } 16862be70f79SJohn Dyson VOP_UNLOCK(object->handle, 0, curproc); 1687ff359f84SMatthew Dillon vm_object_deallocate(object); 1688a02051c3SJohn Dyson } 1689df8bae1dSRodney W. Grimes start += size; 1690df8bae1dSRodney W. Grimes } 1691df8bae1dSRodney W. Grimes 1692df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1693df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1694df8bae1dSRodney W. Grimes } 1695df8bae1dSRodney W. Grimes 1696df8bae1dSRodney W. Grimes /* 1697df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 1698df8bae1dSRodney W. Grimes * 1699df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 1700df8bae1dSRodney W. Grimes * 1701df8bae1dSRodney W. Grimes * The map in question should be locked. 1702df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 1703df8bae1dSRodney W. Grimes */ 17040362d7d7SJohn Dyson static void 17050d94caffSDavid Greenman vm_map_entry_unwire(map, entry) 1706df8bae1dSRodney W. Grimes vm_map_t map; 1707c0877f10SJohn Dyson vm_map_entry_t entry; 1708df8bae1dSRodney W. Grimes { 1709df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 1710df8bae1dSRodney W. Grimes entry->wired_count = 0; 1711df8bae1dSRodney W. Grimes } 1712df8bae1dSRodney W. Grimes 1713df8bae1dSRodney W. Grimes /* 1714df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 1715df8bae1dSRodney W. Grimes * 1716df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 1717df8bae1dSRodney W. Grimes */ 17180362d7d7SJohn Dyson static void 17190d94caffSDavid Greenman vm_map_entry_delete(map, entry) 1720c0877f10SJohn Dyson vm_map_t map; 1721c0877f10SJohn Dyson vm_map_entry_t entry; 1722df8bae1dSRodney W. Grimes { 1723df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 1724df8bae1dSRodney W. Grimes map->size -= entry->end - entry->start; 1725df8bae1dSRodney W. Grimes 17269fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1727df8bae1dSRodney W. Grimes vm_object_deallocate(entry->object.vm_object); 1728b5b40fa6SJohn Dyson } 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes vm_map_entry_dispose(map, entry); 1731df8bae1dSRodney W. Grimes } 1732df8bae1dSRodney W. Grimes 1733df8bae1dSRodney W. Grimes /* 1734df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 1735df8bae1dSRodney W. Grimes * 1736df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 1737df8bae1dSRodney W. Grimes * map. 1738df8bae1dSRodney W. Grimes */ 1739df8bae1dSRodney W. Grimes int 1740df8bae1dSRodney W. Grimes vm_map_delete(map, start, end) 1741c0877f10SJohn Dyson vm_map_t map; 1742df8bae1dSRodney W. Grimes vm_offset_t start; 1743c0877f10SJohn Dyson vm_offset_t end; 1744df8bae1dSRodney W. Grimes { 1745cbd8ec09SJohn Dyson vm_object_t object; 1746c0877f10SJohn Dyson vm_map_entry_t entry; 1747df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 1748df8bae1dSRodney W. Grimes 1749df8bae1dSRodney W. Grimes /* 1750df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 1751df8bae1dSRodney W. Grimes */ 1752df8bae1dSRodney W. Grimes 1753876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 1754df8bae1dSRodney W. Grimes entry = first_entry->next; 1755876318ecSAlan Cox else { 1756df8bae1dSRodney W. Grimes entry = first_entry; 1757df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1758df8bae1dSRodney W. Grimes /* 17590d94caffSDavid Greenman * Fix the lookup hint now, rather than each time though the 17600d94caffSDavid Greenman * loop. 1761df8bae1dSRodney W. Grimes */ 1762df8bae1dSRodney W. Grimes SAVE_HINT(map, entry->prev); 1763df8bae1dSRodney W. Grimes } 1764df8bae1dSRodney W. Grimes 1765df8bae1dSRodney W. Grimes /* 1766df8bae1dSRodney W. Grimes * Save the free space hint 1767df8bae1dSRodney W. Grimes */ 1768df8bae1dSRodney W. Grimes 1769b18bfc3dSJohn Dyson if (entry == &map->header) { 1770b18bfc3dSJohn Dyson map->first_free = &map->header; 17712dbea5d2SJohn Dyson } else if (map->first_free->start >= start) { 1772df8bae1dSRodney W. Grimes map->first_free = entry->prev; 17732dbea5d2SJohn Dyson } 1774df8bae1dSRodney W. Grimes 1775df8bae1dSRodney W. Grimes /* 1776df8bae1dSRodney W. Grimes * Step through all entries in this region 1777df8bae1dSRodney W. Grimes */ 1778df8bae1dSRodney W. Grimes 1779df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1780df8bae1dSRodney W. Grimes vm_map_entry_t next; 1781b18bfc3dSJohn Dyson vm_offset_t s, e; 1782cbd8ec09SJohn Dyson vm_pindex_t offidxstart, offidxend, count; 1783df8bae1dSRodney W. Grimes 1784df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1785df8bae1dSRodney W. Grimes 1786df8bae1dSRodney W. Grimes s = entry->start; 1787df8bae1dSRodney W. Grimes e = entry->end; 1788c0877f10SJohn Dyson next = entry->next; 1789df8bae1dSRodney W. Grimes 1790cbd8ec09SJohn Dyson offidxstart = OFF_TO_IDX(entry->offset); 1791cbd8ec09SJohn Dyson count = OFF_TO_IDX(e - s); 1792cbd8ec09SJohn Dyson object = entry->object.vm_object; 17932dbea5d2SJohn Dyson 1794df8bae1dSRodney W. Grimes /* 17950d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 17960d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 1797df8bae1dSRodney W. Grimes */ 1798c0877f10SJohn Dyson if (entry->wired_count != 0) { 1799df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 1800c0877f10SJohn Dyson } 1801df8bae1dSRodney W. Grimes 1802cbd8ec09SJohn Dyson offidxend = offidxstart + count; 1803df8bae1dSRodney W. Grimes 1804c0877f10SJohn Dyson if ((object == kernel_object) || (object == kmem_object)) { 18052dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 1806b18bfc3dSJohn Dyson } else { 1807df8bae1dSRodney W. Grimes pmap_remove(map->pmap, s, e); 1808876318ecSAlan Cox if (object != NULL && 1809876318ecSAlan Cox object->ref_count != 1 && 1810876318ecSAlan Cox (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING && 1811876318ecSAlan Cox (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 18122dbea5d2SJohn Dyson vm_object_collapse(object); 18132dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 18142dbea5d2SJohn Dyson if (object->type == OBJT_SWAP) { 1815cbd8ec09SJohn Dyson swap_pager_freespace(object, offidxstart, count); 18162dbea5d2SJohn Dyson } 1817876318ecSAlan Cox if (offidxend >= object->size && 1818876318ecSAlan Cox offidxstart < object->size) { 1819c0877f10SJohn Dyson object->size = offidxstart; 1820c0877f10SJohn Dyson } 18212dbea5d2SJohn Dyson } 1822b18bfc3dSJohn Dyson } 1823df8bae1dSRodney W. Grimes 1824df8bae1dSRodney W. Grimes /* 18250d94caffSDavid Greenman * Delete the entry (which may delete the object) only after 18260d94caffSDavid Greenman * removing all pmap entries pointing to its pages. 18270d94caffSDavid Greenman * (Otherwise, its page frames may be reallocated, and any 18280d94caffSDavid Greenman * modify bits will be set in the wrong object!) 1829df8bae1dSRodney W. Grimes */ 1830df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 1831df8bae1dSRodney W. Grimes entry = next; 1832df8bae1dSRodney W. Grimes } 1833df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1834df8bae1dSRodney W. Grimes } 1835df8bae1dSRodney W. Grimes 1836df8bae1dSRodney W. Grimes /* 1837df8bae1dSRodney W. Grimes * vm_map_remove: 1838df8bae1dSRodney W. Grimes * 1839df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 1840df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 1841df8bae1dSRodney W. Grimes */ 1842df8bae1dSRodney W. Grimes int 1843df8bae1dSRodney W. Grimes vm_map_remove(map, start, end) 1844c0877f10SJohn Dyson vm_map_t map; 1845c0877f10SJohn Dyson vm_offset_t start; 1846c0877f10SJohn Dyson vm_offset_t end; 1847df8bae1dSRodney W. Grimes { 1848c0877f10SJohn Dyson int result, s = 0; 18498d6e8edeSDavid Greenman 18509579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 1851b18bfc3dSJohn Dyson s = splvm(); 1852df8bae1dSRodney W. Grimes 1853df8bae1dSRodney W. Grimes vm_map_lock(map); 1854df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1855df8bae1dSRodney W. Grimes result = vm_map_delete(map, start, end); 1856df8bae1dSRodney W. Grimes vm_map_unlock(map); 1857df8bae1dSRodney W. Grimes 18589579ee64SDavid Greenman if (map == kmem_map || map == mb_map) 18598d6e8edeSDavid Greenman splx(s); 18608d6e8edeSDavid Greenman 1861df8bae1dSRodney W. Grimes return (result); 1862df8bae1dSRodney W. Grimes } 1863df8bae1dSRodney W. Grimes 1864df8bae1dSRodney W. Grimes /* 1865df8bae1dSRodney W. Grimes * vm_map_check_protection: 1866df8bae1dSRodney W. Grimes * 1867df8bae1dSRodney W. Grimes * Assert that the target map allows the specified 1868df8bae1dSRodney W. Grimes * privilege on the entire address region given. 1869df8bae1dSRodney W. Grimes * The entire region must be allocated. 1870df8bae1dSRodney W. Grimes */ 18710d94caffSDavid Greenman boolean_t 1872b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 1873b9dcd593SBruce Evans vm_prot_t protection) 1874df8bae1dSRodney W. Grimes { 1875c0877f10SJohn Dyson vm_map_entry_t entry; 1876df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 1877df8bae1dSRodney W. Grimes 1878df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 1879df8bae1dSRodney W. Grimes return (FALSE); 1880df8bae1dSRodney W. Grimes } 1881df8bae1dSRodney W. Grimes entry = tmp_entry; 1882df8bae1dSRodney W. Grimes 1883df8bae1dSRodney W. Grimes while (start < end) { 1884df8bae1dSRodney W. Grimes if (entry == &map->header) { 1885df8bae1dSRodney W. Grimes return (FALSE); 1886df8bae1dSRodney W. Grimes } 1887df8bae1dSRodney W. Grimes /* 1888df8bae1dSRodney W. Grimes * No holes allowed! 1889df8bae1dSRodney W. Grimes */ 1890df8bae1dSRodney W. Grimes 1891df8bae1dSRodney W. Grimes if (start < entry->start) { 1892df8bae1dSRodney W. Grimes return (FALSE); 1893df8bae1dSRodney W. Grimes } 1894df8bae1dSRodney W. Grimes /* 1895df8bae1dSRodney W. Grimes * Check protection associated with entry. 1896df8bae1dSRodney W. Grimes */ 1897df8bae1dSRodney W. Grimes 1898df8bae1dSRodney W. Grimes if ((entry->protection & protection) != protection) { 1899df8bae1dSRodney W. Grimes return (FALSE); 1900df8bae1dSRodney W. Grimes } 1901df8bae1dSRodney W. Grimes /* go to next entry */ 1902df8bae1dSRodney W. Grimes 1903df8bae1dSRodney W. Grimes start = entry->end; 1904df8bae1dSRodney W. Grimes entry = entry->next; 1905df8bae1dSRodney W. Grimes } 1906df8bae1dSRodney W. Grimes return (TRUE); 1907df8bae1dSRodney W. Grimes } 1908df8bae1dSRodney W. Grimes 190986524867SJohn Dyson /* 191086524867SJohn Dyson * Split the pages in a map entry into a new object. This affords 191186524867SJohn Dyson * easier removal of unused pages, and keeps object inheritance from 191286524867SJohn Dyson * being a negative impact on memory usage. 191386524867SJohn Dyson */ 1914c0877f10SJohn Dyson static void 1915c0877f10SJohn Dyson vm_map_split(entry) 1916c0877f10SJohn Dyson vm_map_entry_t entry; 1917c0877f10SJohn Dyson { 191886524867SJohn Dyson vm_page_t m; 1919bd6be915SJohn Dyson vm_object_t orig_object, new_object, source; 1920c0877f10SJohn Dyson vm_offset_t s, e; 1921c0877f10SJohn Dyson vm_pindex_t offidxstart, offidxend, idx; 1922c0877f10SJohn Dyson vm_size_t size; 1923c0877f10SJohn Dyson vm_ooffset_t offset; 1924c0877f10SJohn Dyson 1925c0877f10SJohn Dyson orig_object = entry->object.vm_object; 1926c0877f10SJohn Dyson if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 1927c0877f10SJohn Dyson return; 1928c0877f10SJohn Dyson if (orig_object->ref_count <= 1) 1929c0877f10SJohn Dyson return; 1930c0877f10SJohn Dyson 1931c0877f10SJohn Dyson offset = entry->offset; 1932c0877f10SJohn Dyson s = entry->start; 1933c0877f10SJohn Dyson e = entry->end; 1934c0877f10SJohn Dyson 1935c0877f10SJohn Dyson offidxstart = OFF_TO_IDX(offset); 1936c0877f10SJohn Dyson offidxend = offidxstart + OFF_TO_IDX(e - s); 1937c0877f10SJohn Dyson size = offidxend - offidxstart; 1938c0877f10SJohn Dyson 1939c0877f10SJohn Dyson new_object = vm_pager_allocate(orig_object->type, 19406cde7a16SDavid Greenman NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 1941c0877f10SJohn Dyson if (new_object == NULL) 1942c0877f10SJohn Dyson return; 1943c0877f10SJohn Dyson 1944bd6be915SJohn Dyson source = orig_object->backing_object; 1945bd6be915SJohn Dyson if (source != NULL) { 1946bd6be915SJohn Dyson vm_object_reference(source); /* Referenced by new_object */ 1947bd6be915SJohn Dyson TAILQ_INSERT_TAIL(&source->shadow_head, 1948bd6be915SJohn Dyson new_object, shadow_list); 1949069e9bc1SDoug Rabson vm_object_clear_flag(source, OBJ_ONEMAPPING); 1950bd6be915SJohn Dyson new_object->backing_object_offset = 1951a0fce827SJohn Polstra orig_object->backing_object_offset + IDX_TO_OFF(offidxstart); 1952bd6be915SJohn Dyson new_object->backing_object = source; 1953bd6be915SJohn Dyson source->shadow_count++; 1954bd6be915SJohn Dyson source->generation++; 1955bd6be915SJohn Dyson } 1956bd6be915SJohn Dyson 1957c0877f10SJohn Dyson for (idx = 0; idx < size; idx++) { 1958c0877f10SJohn Dyson vm_page_t m; 1959c0877f10SJohn Dyson 1960c0877f10SJohn Dyson retry: 1961c0877f10SJohn Dyson m = vm_page_lookup(orig_object, offidxstart + idx); 1962c0877f10SJohn Dyson if (m == NULL) 1963c0877f10SJohn Dyson continue; 19641c7c3c6aSMatthew Dillon 19651c7c3c6aSMatthew Dillon /* 19661c7c3c6aSMatthew Dillon * We must wait for pending I/O to complete before we can 19671c7c3c6aSMatthew Dillon * rename the page. 1968d1bf5d56SMatthew Dillon * 1969d1bf5d56SMatthew Dillon * We do not have to VM_PROT_NONE the page as mappings should 1970d1bf5d56SMatthew Dillon * not be changed by this operation. 19711c7c3c6aSMatthew Dillon */ 19721c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(m, TRUE, "spltwt")) 1973c0877f10SJohn Dyson goto retry; 1974c0877f10SJohn Dyson 1975e69763a3SDoug Rabson vm_page_busy(m); 1976c0877f10SJohn Dyson vm_page_rename(m, new_object, idx); 19777dbf82dcSMatthew Dillon /* page automatically made dirty by rename and cache handled */ 1978e69763a3SDoug Rabson vm_page_busy(m); 1979c0877f10SJohn Dyson } 1980c0877f10SJohn Dyson 1981c0877f10SJohn Dyson if (orig_object->type == OBJT_SWAP) { 1982d474eaaaSDoug Rabson vm_object_pip_add(orig_object, 1); 1983c0877f10SJohn Dyson /* 1984c0877f10SJohn Dyson * copy orig_object pages into new_object 1985c0877f10SJohn Dyson * and destroy unneeded pages in 1986c0877f10SJohn Dyson * shadow object. 1987c0877f10SJohn Dyson */ 19881c7c3c6aSMatthew Dillon swap_pager_copy(orig_object, new_object, offidxstart, 0); 1989c0877f10SJohn Dyson vm_object_pip_wakeup(orig_object); 1990c0877f10SJohn Dyson } 1991c0877f10SJohn Dyson 199286524867SJohn Dyson for (idx = 0; idx < size; idx++) { 199386524867SJohn Dyson m = vm_page_lookup(new_object, idx); 199486524867SJohn Dyson if (m) { 1995e69763a3SDoug Rabson vm_page_wakeup(m); 199686524867SJohn Dyson } 199786524867SJohn Dyson } 199886524867SJohn Dyson 1999c0877f10SJohn Dyson entry->object.vm_object = new_object; 2000c0877f10SJohn Dyson entry->offset = 0LL; 2001c0877f10SJohn Dyson vm_object_deallocate(orig_object); 2002c0877f10SJohn Dyson } 2003c0877f10SJohn Dyson 2004df8bae1dSRodney W. Grimes /* 2005df8bae1dSRodney W. Grimes * vm_map_copy_entry: 2006df8bae1dSRodney W. Grimes * 2007df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 2008df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 2009df8bae1dSRodney W. Grimes */ 2010f708ef1bSPoul-Henning Kamp static void 20110d94caffSDavid Greenman vm_map_copy_entry(src_map, dst_map, src_entry, dst_entry) 2012df8bae1dSRodney W. Grimes vm_map_t src_map, dst_map; 2013c0877f10SJohn Dyson vm_map_entry_t src_entry, dst_entry; 2014df8bae1dSRodney W. Grimes { 2015c0877f10SJohn Dyson vm_object_t src_object; 2016c0877f10SJohn Dyson 20179fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2018df8bae1dSRodney W. Grimes return; 2019df8bae1dSRodney W. Grimes 2020df8bae1dSRodney W. Grimes if (src_entry->wired_count == 0) { 2021df8bae1dSRodney W. Grimes 2022df8bae1dSRodney W. Grimes /* 20230d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 20240d94caffSDavid Greenman * write-protected. 2025df8bae1dSRodney W. Grimes */ 2026afa07f7eSJohn Dyson if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2027df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 2028df8bae1dSRodney W. Grimes src_entry->start, 2029df8bae1dSRodney W. Grimes src_entry->end, 2030df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 2031df8bae1dSRodney W. Grimes } 2032b18bfc3dSJohn Dyson 2033df8bae1dSRodney W. Grimes /* 2034df8bae1dSRodney W. Grimes * Make a copy of the object. 2035df8bae1dSRodney W. Grimes */ 20368aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 2037c0877f10SJohn Dyson 2038c0877f10SJohn Dyson if ((src_object->handle == NULL) && 2039c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 2040c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 2041c0877f10SJohn Dyson vm_object_collapse(src_object); 204296fb8cf2SJohn Dyson if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2043c0877f10SJohn Dyson vm_map_split(src_entry); 2044c0877f10SJohn Dyson src_object = src_entry->object.vm_object; 2045c0877f10SJohn Dyson } 2046c0877f10SJohn Dyson } 2047c0877f10SJohn Dyson 2048c0877f10SJohn Dyson vm_object_reference(src_object); 2049069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2050c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 2051afa07f7eSJohn Dyson src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2052afa07f7eSJohn Dyson dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2053b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 2054b18bfc3dSJohn Dyson } else { 2055b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 2056b18bfc3dSJohn Dyson dst_entry->offset = 0; 2057b18bfc3dSJohn Dyson } 2058df8bae1dSRodney W. Grimes 2059df8bae1dSRodney W. Grimes pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2060df8bae1dSRodney W. Grimes dst_entry->end - dst_entry->start, src_entry->start); 20610d94caffSDavid Greenman } else { 2062df8bae1dSRodney W. Grimes /* 2063df8bae1dSRodney W. Grimes * Of course, wired down pages can't be set copy-on-write. 20640d94caffSDavid Greenman * Cause wired pages to be copied into the new map by 20650d94caffSDavid Greenman * simulating faults (the new pages are pageable) 2066df8bae1dSRodney W. Grimes */ 2067df8bae1dSRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2068df8bae1dSRodney W. Grimes } 2069df8bae1dSRodney W. Grimes } 2070df8bae1dSRodney W. Grimes 2071df8bae1dSRodney W. Grimes /* 2072df8bae1dSRodney W. Grimes * vmspace_fork: 2073df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 2074df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 2075df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 2076df8bae1dSRodney W. Grimes * values on the regions in that map. 2077df8bae1dSRodney W. Grimes * 2078df8bae1dSRodney W. Grimes * The source map must not be locked. 2079df8bae1dSRodney W. Grimes */ 2080df8bae1dSRodney W. Grimes struct vmspace * 2081df8bae1dSRodney W. Grimes vmspace_fork(vm1) 2082c0877f10SJohn Dyson struct vmspace *vm1; 2083df8bae1dSRodney W. Grimes { 2084c0877f10SJohn Dyson struct vmspace *vm2; 2085df8bae1dSRodney W. Grimes vm_map_t old_map = &vm1->vm_map; 2086df8bae1dSRodney W. Grimes vm_map_t new_map; 2087df8bae1dSRodney W. Grimes vm_map_entry_t old_entry; 2088df8bae1dSRodney W. Grimes vm_map_entry_t new_entry; 2089de5f6a77SJohn Dyson vm_object_t object; 2090df8bae1dSRodney W. Grimes 2091df8bae1dSRodney W. Grimes vm_map_lock(old_map); 2092df8bae1dSRodney W. Grimes 20932d8acc0fSJohn Dyson vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2094df8bae1dSRodney W. Grimes bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2095df8bae1dSRodney W. Grimes (caddr_t) (vm1 + 1) - (caddr_t) &vm1->vm_startcopy); 2096df8bae1dSRodney W. Grimes new_map = &vm2->vm_map; /* XXX */ 209747221757SJohn Dyson new_map->timestamp = 1; 2098df8bae1dSRodney W. Grimes 2099df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 2100df8bae1dSRodney W. Grimes 2101df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 2102afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2103df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 2104df8bae1dSRodney W. Grimes 2105df8bae1dSRodney W. Grimes switch (old_entry->inheritance) { 2106df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2107df8bae1dSRodney W. Grimes break; 2108df8bae1dSRodney W. Grimes 2109df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 2110df8bae1dSRodney W. Grimes /* 2111fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 2112fed9a903SJohn Dyson */ 2113fed9a903SJohn Dyson object = old_entry->object.vm_object; 2114fed9a903SJohn Dyson if (object == NULL) { 2115fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 2116c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 2117fed9a903SJohn Dyson old_entry->object.vm_object = object; 2118fed9a903SJohn Dyson old_entry->offset = (vm_offset_t) 0; 21199a2f6362SAlan Cox } 21209a2f6362SAlan Cox 21219a2f6362SAlan Cox /* 21229a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 21239a2f6362SAlan Cox * to insure that a shadow object is created. 21249a2f6362SAlan Cox */ 21259a2f6362SAlan Cox vm_object_reference(object); 21269a2f6362SAlan Cox 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 } 2133069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 2134fed9a903SJohn Dyson 2135fed9a903SJohn Dyson /* 2136ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 2137df8bae1dSRodney W. Grimes */ 2138df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2139df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2140df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 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; 2168df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2169df8bae1dSRodney W. Grimes new_entry); 2170bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 2171bd7e5f99SJohn Dyson new_entry); 2172df8bae1dSRodney W. Grimes break; 2173df8bae1dSRodney W. Grimes } 2174df8bae1dSRodney W. Grimes old_entry = old_entry->next; 2175df8bae1dSRodney W. Grimes } 2176df8bae1dSRodney W. Grimes 2177df8bae1dSRodney W. Grimes new_map->size = old_map->size; 2178df8bae1dSRodney W. Grimes vm_map_unlock(old_map); 2179df8bae1dSRodney W. Grimes 2180df8bae1dSRodney W. Grimes return (vm2); 2181df8bae1dSRodney W. Grimes } 2182df8bae1dSRodney W. Grimes 218394f7e29aSAlan Cox int 218494f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 218594f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 218694f7e29aSAlan Cox { 218794f7e29aSAlan Cox vm_map_entry_t prev_entry; 218894f7e29aSAlan Cox vm_map_entry_t new_stack_entry; 218994f7e29aSAlan Cox vm_size_t init_ssize; 219094f7e29aSAlan Cox int rv; 219194f7e29aSAlan Cox 219294f7e29aSAlan Cox if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS) 219394f7e29aSAlan Cox return (KERN_NO_SPACE); 219494f7e29aSAlan Cox 219594f7e29aSAlan Cox if (max_ssize < SGROWSIZ) 219694f7e29aSAlan Cox init_ssize = max_ssize; 219794f7e29aSAlan Cox else 219894f7e29aSAlan Cox init_ssize = SGROWSIZ; 219994f7e29aSAlan Cox 220094f7e29aSAlan Cox vm_map_lock(map); 220194f7e29aSAlan Cox 220294f7e29aSAlan Cox /* If addr is already mapped, no go */ 220394f7e29aSAlan Cox if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 220494f7e29aSAlan Cox vm_map_unlock(map); 220594f7e29aSAlan Cox return (KERN_NO_SPACE); 220694f7e29aSAlan Cox } 220794f7e29aSAlan Cox 220894f7e29aSAlan Cox /* If we can't accomodate max_ssize in the current mapping, 220994f7e29aSAlan Cox * no go. However, we need to be aware that subsequent user 221094f7e29aSAlan Cox * mappings might map into the space we have reserved for 221194f7e29aSAlan Cox * stack, and currently this space is not protected. 221294f7e29aSAlan Cox * 221394f7e29aSAlan Cox * Hopefully we will at least detect this condition 221494f7e29aSAlan Cox * when we try to grow the stack. 221594f7e29aSAlan Cox */ 221694f7e29aSAlan Cox if ((prev_entry->next != &map->header) && 221794f7e29aSAlan Cox (prev_entry->next->start < addrbos + max_ssize)) { 221894f7e29aSAlan Cox vm_map_unlock(map); 221994f7e29aSAlan Cox return (KERN_NO_SPACE); 222094f7e29aSAlan Cox } 222194f7e29aSAlan Cox 222294f7e29aSAlan Cox /* We initially map a stack of only init_ssize. We will 222394f7e29aSAlan Cox * grow as needed later. Since this is to be a grow 222494f7e29aSAlan Cox * down stack, we map at the top of the range. 222594f7e29aSAlan Cox * 222694f7e29aSAlan Cox * Note: we would normally expect prot and max to be 222794f7e29aSAlan Cox * VM_PROT_ALL, and cow to be 0. Possibly we should 222894f7e29aSAlan Cox * eliminate these as input parameters, and just 222994f7e29aSAlan Cox * pass these values here in the insert call. 223094f7e29aSAlan Cox */ 223194f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize, 223294f7e29aSAlan Cox addrbos + max_ssize, prot, max, cow); 223394f7e29aSAlan Cox 223494f7e29aSAlan Cox /* Now set the avail_ssize amount */ 223594f7e29aSAlan Cox if (rv == KERN_SUCCESS){ 223629b45e9eSAlan Cox if (prev_entry != &map->header) 223729b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize); 223894f7e29aSAlan Cox new_stack_entry = prev_entry->next; 223994f7e29aSAlan Cox if (new_stack_entry->end != addrbos + max_ssize || 224094f7e29aSAlan Cox new_stack_entry->start != addrbos + max_ssize - init_ssize) 224194f7e29aSAlan Cox panic ("Bad entry start/end for new stack entry"); 224294f7e29aSAlan Cox else 224394f7e29aSAlan Cox new_stack_entry->avail_ssize = max_ssize - init_ssize; 224494f7e29aSAlan Cox } 224594f7e29aSAlan Cox 224694f7e29aSAlan Cox vm_map_unlock(map); 224794f7e29aSAlan Cox return (rv); 224894f7e29aSAlan Cox } 224994f7e29aSAlan Cox 225094f7e29aSAlan Cox /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 225194f7e29aSAlan Cox * desired address is already mapped, or if we successfully grow 225294f7e29aSAlan Cox * the stack. Also returns KERN_SUCCESS if addr is outside the 225394f7e29aSAlan Cox * stack range (this is strange, but preserves compatibility with 225494f7e29aSAlan Cox * the grow function in vm_machdep.c). 225594f7e29aSAlan Cox */ 225694f7e29aSAlan Cox int 225794f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr) 225894f7e29aSAlan Cox { 225994f7e29aSAlan Cox vm_map_entry_t prev_entry; 226094f7e29aSAlan Cox vm_map_entry_t stack_entry; 226194f7e29aSAlan Cox vm_map_entry_t new_stack_entry; 226294f7e29aSAlan Cox struct vmspace *vm = p->p_vmspace; 226394f7e29aSAlan Cox vm_map_t map = &vm->vm_map; 226494f7e29aSAlan Cox vm_offset_t end; 226594f7e29aSAlan Cox int grow_amount; 226694f7e29aSAlan Cox int rv; 226794f7e29aSAlan Cox int is_procstack; 226894f7e29aSAlan Cox Retry: 226994f7e29aSAlan Cox vm_map_lock_read(map); 227094f7e29aSAlan Cox 227194f7e29aSAlan Cox /* If addr is already in the entry range, no need to grow.*/ 227294f7e29aSAlan Cox if (vm_map_lookup_entry(map, addr, &prev_entry)) { 227394f7e29aSAlan Cox vm_map_unlock_read(map); 227494f7e29aSAlan Cox return (KERN_SUCCESS); 227594f7e29aSAlan Cox } 227694f7e29aSAlan Cox 227794f7e29aSAlan Cox if ((stack_entry = prev_entry->next) == &map->header) { 227894f7e29aSAlan Cox vm_map_unlock_read(map); 227994f7e29aSAlan Cox return (KERN_SUCCESS); 228094f7e29aSAlan Cox } 228194f7e29aSAlan Cox if (prev_entry == &map->header) 228294f7e29aSAlan Cox end = stack_entry->start - stack_entry->avail_ssize; 228394f7e29aSAlan Cox else 228494f7e29aSAlan Cox end = prev_entry->end; 228594f7e29aSAlan Cox 228694f7e29aSAlan Cox /* This next test mimics the old grow function in vm_machdep.c. 228794f7e29aSAlan Cox * It really doesn't quite make sense, but we do it anyway 228894f7e29aSAlan Cox * for compatibility. 228994f7e29aSAlan Cox * 229094f7e29aSAlan Cox * If not growable stack, return success. This signals the 229194f7e29aSAlan Cox * caller to proceed as he would normally with normal vm. 229294f7e29aSAlan Cox */ 229394f7e29aSAlan Cox if (stack_entry->avail_ssize < 1 || 229494f7e29aSAlan Cox addr >= stack_entry->start || 229594f7e29aSAlan Cox addr < stack_entry->start - stack_entry->avail_ssize) { 229694f7e29aSAlan Cox vm_map_unlock_read(map); 229794f7e29aSAlan Cox return (KERN_SUCCESS); 229894f7e29aSAlan Cox } 229994f7e29aSAlan Cox 230094f7e29aSAlan Cox /* Find the minimum grow amount */ 230194f7e29aSAlan Cox grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 230294f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 230394f7e29aSAlan Cox vm_map_unlock_read(map); 230494f7e29aSAlan Cox return (KERN_NO_SPACE); 230594f7e29aSAlan Cox } 230694f7e29aSAlan Cox 230794f7e29aSAlan Cox /* If there is no longer enough space between the entries 230894f7e29aSAlan Cox * nogo, and adjust the available space. Note: this 230994f7e29aSAlan Cox * should only happen if the user has mapped into the 231094f7e29aSAlan Cox * stack area after the stack was created, and is 231194f7e29aSAlan Cox * probably an error. 231294f7e29aSAlan Cox * 231394f7e29aSAlan Cox * This also effectively destroys any guard page the user 231494f7e29aSAlan Cox * might have intended by limiting the stack size. 231594f7e29aSAlan Cox */ 231694f7e29aSAlan Cox if (grow_amount > stack_entry->start - end) { 231794f7e29aSAlan Cox if (vm_map_lock_upgrade(map)) 231894f7e29aSAlan Cox goto Retry; 231994f7e29aSAlan Cox 232094f7e29aSAlan Cox stack_entry->avail_ssize = stack_entry->start - end; 232194f7e29aSAlan Cox 232294f7e29aSAlan Cox vm_map_unlock(map); 232394f7e29aSAlan Cox return (KERN_NO_SPACE); 232494f7e29aSAlan Cox } 232594f7e29aSAlan Cox 232694f7e29aSAlan Cox is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 232794f7e29aSAlan Cox 232894f7e29aSAlan Cox /* If this is the main process stack, see if we're over the 232994f7e29aSAlan Cox * stack limit. 233094f7e29aSAlan Cox */ 23316389da78SAlan Cox if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 233294f7e29aSAlan Cox p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 233394f7e29aSAlan Cox vm_map_unlock_read(map); 233494f7e29aSAlan Cox return (KERN_NO_SPACE); 233594f7e29aSAlan Cox } 233694f7e29aSAlan Cox 233794f7e29aSAlan Cox /* Round up the grow amount modulo SGROWSIZ */ 233894f7e29aSAlan Cox grow_amount = roundup (grow_amount, SGROWSIZ); 233994f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 234094f7e29aSAlan Cox grow_amount = stack_entry->avail_ssize; 234194f7e29aSAlan Cox } 23426389da78SAlan Cox if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 234394f7e29aSAlan Cox p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 234494f7e29aSAlan Cox grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 23456389da78SAlan Cox ctob(vm->vm_ssize); 234694f7e29aSAlan Cox } 234794f7e29aSAlan Cox 234894f7e29aSAlan Cox if (vm_map_lock_upgrade(map)) 234994f7e29aSAlan Cox goto Retry; 235094f7e29aSAlan Cox 235194f7e29aSAlan Cox /* Get the preliminary new entry start value */ 235294f7e29aSAlan Cox addr = stack_entry->start - grow_amount; 235394f7e29aSAlan Cox 235494f7e29aSAlan Cox /* If this puts us into the previous entry, cut back our growth 235594f7e29aSAlan Cox * to the available space. Also, see the note above. 235694f7e29aSAlan Cox */ 235794f7e29aSAlan Cox if (addr < end) { 235894f7e29aSAlan Cox stack_entry->avail_ssize = stack_entry->start - end; 235994f7e29aSAlan Cox addr = end; 236094f7e29aSAlan Cox } 236194f7e29aSAlan Cox 236294f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 236329b45e9eSAlan Cox VM_PROT_ALL, 236429b45e9eSAlan Cox VM_PROT_ALL, 236594f7e29aSAlan Cox 0); 236694f7e29aSAlan Cox 236794f7e29aSAlan Cox /* Adjust the available stack space by the amount we grew. */ 236894f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 236929b45e9eSAlan Cox if (prev_entry != &map->header) 237029b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addr); 237194f7e29aSAlan Cox new_stack_entry = prev_entry->next; 237294f7e29aSAlan Cox if (new_stack_entry->end != stack_entry->start || 237394f7e29aSAlan Cox new_stack_entry->start != addr) 237494f7e29aSAlan Cox panic ("Bad stack grow start/end in new stack entry"); 237594f7e29aSAlan Cox else { 237694f7e29aSAlan Cox new_stack_entry->avail_ssize = stack_entry->avail_ssize - 237794f7e29aSAlan Cox (new_stack_entry->end - 237894f7e29aSAlan Cox new_stack_entry->start); 237994f7e29aSAlan Cox if (is_procstack) 23806389da78SAlan Cox vm->vm_ssize += btoc(new_stack_entry->end - 23816389da78SAlan Cox new_stack_entry->start); 238294f7e29aSAlan Cox } 238394f7e29aSAlan Cox } 238494f7e29aSAlan Cox 238594f7e29aSAlan Cox vm_map_unlock(map); 238694f7e29aSAlan Cox return (rv); 238794f7e29aSAlan Cox 238894f7e29aSAlan Cox } 238994f7e29aSAlan Cox 2390df8bae1dSRodney W. Grimes /* 23915856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 23925856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 23935856e12eSJohn Dyson */ 23945856e12eSJohn Dyson 23955856e12eSJohn Dyson void 23965856e12eSJohn Dyson vmspace_exec(struct proc *p) { 23975856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 23985856e12eSJohn Dyson struct vmspace *newvmspace; 23995856e12eSJohn Dyson vm_map_t map = &p->p_vmspace->vm_map; 24005856e12eSJohn Dyson 24012d8acc0fSJohn Dyson newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 24025856e12eSJohn Dyson bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 24035856e12eSJohn Dyson (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 24045856e12eSJohn Dyson /* 24055856e12eSJohn Dyson * This code is written like this for prototype purposes. The 24065856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 24075856e12eSJohn Dyson * other process's that are still using the vmspace to finally 24085856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 24095856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 24105856e12eSJohn Dyson */ 24115856e12eSJohn Dyson vmspace_free(oldvmspace); 24125856e12eSJohn Dyson p->p_vmspace = newvmspace; 2413d4da2dbaSAlan Cox pmap_pinit2(vmspace_pmap(newvmspace)); 24145856e12eSJohn Dyson if (p == curproc) 24155856e12eSJohn Dyson pmap_activate(p); 24165856e12eSJohn Dyson } 24175856e12eSJohn Dyson 24185856e12eSJohn Dyson /* 24195856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 24205856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 24215856e12eSJohn Dyson */ 24225856e12eSJohn Dyson 24235856e12eSJohn Dyson void 24245856e12eSJohn Dyson vmspace_unshare(struct proc *p) { 24255856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 24265856e12eSJohn Dyson struct vmspace *newvmspace; 24275856e12eSJohn Dyson 24285856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 24295856e12eSJohn Dyson return; 24305856e12eSJohn Dyson newvmspace = vmspace_fork(oldvmspace); 24315856e12eSJohn Dyson vmspace_free(oldvmspace); 24325856e12eSJohn Dyson p->p_vmspace = newvmspace; 2433d4da2dbaSAlan Cox pmap_pinit2(vmspace_pmap(newvmspace)); 24345856e12eSJohn Dyson if (p == curproc) 24355856e12eSJohn Dyson pmap_activate(p); 24365856e12eSJohn Dyson } 24375856e12eSJohn Dyson 24385856e12eSJohn Dyson 24395856e12eSJohn Dyson /* 2440df8bae1dSRodney W. Grimes * vm_map_lookup: 2441df8bae1dSRodney W. Grimes * 2442df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 2443df8bae1dSRodney W. Grimes * protection for a given virtual address in the 2444df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 2445df8bae1dSRodney W. Grimes * type specified. 2446df8bae1dSRodney W. Grimes * 2447df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 2448df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 2449df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 2450df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 2451df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 2452df8bae1dSRodney W. Grimes * 2453df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 2454df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 2455df8bae1dSRodney W. Grimes * 2456df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 2457df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 2458df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 2459df8bae1dSRodney W. Grimes * remain the same. 2460df8bae1dSRodney W. Grimes */ 2461df8bae1dSRodney W. Grimes int 2462b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2463b9dcd593SBruce Evans vm_offset_t vaddr, 246447221757SJohn Dyson vm_prot_t fault_typea, 2465b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 2466b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 2467b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 2468b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 24692d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 2470df8bae1dSRodney W. Grimes { 2471c0877f10SJohn Dyson vm_map_entry_t entry; 2472c0877f10SJohn Dyson vm_map_t map = *var_map; 2473c0877f10SJohn Dyson vm_prot_t prot; 247447221757SJohn Dyson vm_prot_t fault_type = fault_typea; 2475df8bae1dSRodney W. Grimes 2476df8bae1dSRodney W. Grimes RetryLookup:; 2477df8bae1dSRodney W. Grimes 2478df8bae1dSRodney W. Grimes /* 2479df8bae1dSRodney W. Grimes * Lookup the faulting address. 2480df8bae1dSRodney W. Grimes */ 2481df8bae1dSRodney W. Grimes 2482df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2483df8bae1dSRodney W. Grimes 2484df8bae1dSRodney W. Grimes #define RETURN(why) \ 2485df8bae1dSRodney W. Grimes { \ 2486df8bae1dSRodney W. Grimes vm_map_unlock_read(map); \ 2487df8bae1dSRodney W. Grimes return(why); \ 2488df8bae1dSRodney W. Grimes } 2489df8bae1dSRodney W. Grimes 2490df8bae1dSRodney W. Grimes /* 24910d94caffSDavid Greenman * If the map has an interesting hint, try it before calling full 24920d94caffSDavid Greenman * blown lookup routine. 2493df8bae1dSRodney W. Grimes */ 2494df8bae1dSRodney W. Grimes 2495df8bae1dSRodney W. Grimes entry = map->hint; 2496df8bae1dSRodney W. Grimes 2497df8bae1dSRodney W. Grimes *out_entry = entry; 2498df8bae1dSRodney W. Grimes 2499df8bae1dSRodney W. Grimes if ((entry == &map->header) || 2500df8bae1dSRodney W. Grimes (vaddr < entry->start) || (vaddr >= entry->end)) { 2501df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2502df8bae1dSRodney W. Grimes 2503df8bae1dSRodney W. Grimes /* 25040d94caffSDavid Greenman * Entry was either not a valid hint, or the vaddr was not 25050d94caffSDavid Greenman * contained in the entry, so do a full lookup. 2506df8bae1dSRodney W. Grimes */ 2507df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2508df8bae1dSRodney W. Grimes RETURN(KERN_INVALID_ADDRESS); 2509df8bae1dSRodney W. Grimes 2510df8bae1dSRodney W. Grimes entry = tmp_entry; 2511df8bae1dSRodney W. Grimes *out_entry = entry; 2512df8bae1dSRodney W. Grimes } 2513b7b2aac2SJohn Dyson 2514df8bae1dSRodney W. Grimes /* 2515df8bae1dSRodney W. Grimes * Handle submaps. 2516df8bae1dSRodney W. Grimes */ 2517df8bae1dSRodney W. Grimes 2518afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2519df8bae1dSRodney W. Grimes vm_map_t old_map = map; 2520df8bae1dSRodney W. Grimes 2521df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 2522df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 2523df8bae1dSRodney W. Grimes goto RetryLookup; 2524df8bae1dSRodney W. Grimes } 2525a04c970aSJohn Dyson 2526df8bae1dSRodney W. Grimes /* 25270d94caffSDavid Greenman * Check whether this task is allowed to have this page. 2528a04c970aSJohn Dyson * Note the special case for MAP_ENTRY_COW 2529a04c970aSJohn Dyson * pages with an override. This is to implement a forced 2530a04c970aSJohn Dyson * COW for debuggers. 2531df8bae1dSRodney W. Grimes */ 2532df8bae1dSRodney W. Grimes 2533480ba2f5SJohn Dyson if (fault_type & VM_PROT_OVERRIDE_WRITE) 2534480ba2f5SJohn Dyson prot = entry->max_protection; 2535480ba2f5SJohn Dyson else 2536df8bae1dSRodney W. Grimes prot = entry->protection; 253747221757SJohn Dyson 253847221757SJohn Dyson fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 253947221757SJohn Dyson if ((fault_type & prot) != fault_type) { 254047221757SJohn Dyson RETURN(KERN_PROTECTION_FAILURE); 254147221757SJohn Dyson } 254247221757SJohn Dyson 25432ed14a92SAlan Cox if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 254447221757SJohn Dyson (entry->eflags & MAP_ENTRY_COW) && 25452ed14a92SAlan Cox (fault_type & VM_PROT_WRITE) && 254647221757SJohn Dyson (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2547df8bae1dSRodney W. Grimes RETURN(KERN_PROTECTION_FAILURE); 2548a04c970aSJohn Dyson } 2549df8bae1dSRodney W. Grimes 2550df8bae1dSRodney W. Grimes /* 25510d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 25520d94caffSDavid Greenman * accesses. 2553df8bae1dSRodney W. Grimes */ 2554df8bae1dSRodney W. Grimes 255505f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 255605f0fdd2SPoul-Henning Kamp if (*wired) 2557df8bae1dSRodney W. Grimes prot = fault_type = entry->protection; 2558df8bae1dSRodney W. Grimes 2559df8bae1dSRodney W. Grimes /* 2560df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 2561df8bae1dSRodney W. Grimes */ 2562df8bae1dSRodney W. Grimes 2563afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2564df8bae1dSRodney W. Grimes /* 25650d94caffSDavid Greenman * If we want to write the page, we may as well handle that 2566ad5fca3bSAlan Cox * now since we've got the map locked. 2567df8bae1dSRodney W. Grimes * 25680d94caffSDavid Greenman * If we don't need to write the page, we just demote the 25690d94caffSDavid Greenman * permissions allowed. 2570df8bae1dSRodney W. Grimes */ 2571df8bae1dSRodney W. Grimes 2572df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 2573df8bae1dSRodney W. Grimes /* 25740d94caffSDavid Greenman * Make a new object, and place it in the object 25750d94caffSDavid Greenman * chain. Note that no new references have appeared 2576ad5fca3bSAlan Cox * -- one just moved from the map to the new 25770d94caffSDavid Greenman * object. 2578df8bae1dSRodney W. Grimes */ 2579df8bae1dSRodney W. Grimes 25809b09b6c7SMatthew Dillon if (vm_map_lock_upgrade(map)) 2581df8bae1dSRodney W. Grimes goto RetryLookup; 25829b09b6c7SMatthew Dillon 2583df8bae1dSRodney W. Grimes vm_object_shadow( 2584df8bae1dSRodney W. Grimes &entry->object.vm_object, 2585df8bae1dSRodney W. Grimes &entry->offset, 2586c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2587df8bae1dSRodney W. Grimes 2588afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 25899b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 25900d94caffSDavid Greenman } else { 2591df8bae1dSRodney W. Grimes /* 25920d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 25930d94caffSDavid Greenman * don't allow writes. 2594df8bae1dSRodney W. Grimes */ 2595df8bae1dSRodney W. Grimes 25962d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 2597df8bae1dSRodney W. Grimes } 2598df8bae1dSRodney W. Grimes } 25992d8acc0fSJohn Dyson 2600df8bae1dSRodney W. Grimes /* 2601df8bae1dSRodney W. Grimes * Create an object if necessary. 2602df8bae1dSRodney W. Grimes */ 2603df8bae1dSRodney W. Grimes if (entry->object.vm_object == NULL) { 26049b09b6c7SMatthew Dillon if (vm_map_lock_upgrade(map)) 2605df8bae1dSRodney W. Grimes goto RetryLookup; 26069b09b6c7SMatthew Dillon 260724a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2608c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2609df8bae1dSRodney W. Grimes entry->offset = 0; 26109b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 2611df8bae1dSRodney W. Grimes } 2612b5b40fa6SJohn Dyson 2613df8bae1dSRodney W. Grimes /* 26140d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 26150d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 2616df8bae1dSRodney W. Grimes */ 2617df8bae1dSRodney W. Grimes 26189b09b6c7SMatthew Dillon *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2619df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 2620df8bae1dSRodney W. Grimes 2621df8bae1dSRodney W. Grimes /* 2622df8bae1dSRodney W. Grimes * Return whether this is the only map sharing this data. 2623df8bae1dSRodney W. Grimes */ 2624df8bae1dSRodney W. Grimes 2625df8bae1dSRodney W. Grimes *out_prot = prot; 2626df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2627df8bae1dSRodney W. Grimes 2628df8bae1dSRodney W. Grimes #undef RETURN 2629df8bae1dSRodney W. Grimes } 2630df8bae1dSRodney W. Grimes 2631df8bae1dSRodney W. Grimes /* 2632df8bae1dSRodney W. Grimes * vm_map_lookup_done: 2633df8bae1dSRodney W. Grimes * 2634df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 2635df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 2636df8bae1dSRodney W. Grimes */ 2637df8bae1dSRodney W. Grimes 26380d94caffSDavid Greenman void 26390d94caffSDavid Greenman vm_map_lookup_done(map, entry) 2640c0877f10SJohn Dyson vm_map_t map; 2641df8bae1dSRodney W. Grimes vm_map_entry_t entry; 2642df8bae1dSRodney W. Grimes { 2643df8bae1dSRodney W. Grimes /* 2644df8bae1dSRodney W. Grimes * Unlock the main-level map 2645df8bae1dSRodney W. Grimes */ 2646df8bae1dSRodney W. Grimes 2647df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2648df8bae1dSRodney W. Grimes } 2649df8bae1dSRodney W. Grimes 26501efb74fbSJohn Dyson /* 26511efb74fbSJohn Dyson * Implement uiomove with VM operations. This handles (and collateral changes) 26521efb74fbSJohn Dyson * support every combination of source object modification, and COW type 26531efb74fbSJohn Dyson * operations. 26541efb74fbSJohn Dyson */ 26551efb74fbSJohn Dyson int 265647221757SJohn Dyson vm_uiomove(mapa, srcobject, cp, cnta, uaddra, npages) 26571efb74fbSJohn Dyson vm_map_t mapa; 26581efb74fbSJohn Dyson vm_object_t srcobject; 26591efb74fbSJohn Dyson off_t cp; 266047221757SJohn Dyson int cnta; 26611efb74fbSJohn Dyson vm_offset_t uaddra; 266295e5e988SJohn Dyson int *npages; 26631efb74fbSJohn Dyson { 26641efb74fbSJohn Dyson vm_map_t map; 266547221757SJohn Dyson vm_object_t first_object, oldobject, object; 26662d8acc0fSJohn Dyson vm_map_entry_t entry; 26671efb74fbSJohn Dyson vm_prot_t prot; 26682d8acc0fSJohn Dyson boolean_t wired; 26691efb74fbSJohn Dyson int tcnt, rv; 26702d8acc0fSJohn Dyson vm_offset_t uaddr, start, end, tend; 26711efb74fbSJohn Dyson vm_pindex_t first_pindex, osize, oindex; 26721efb74fbSJohn Dyson off_t ooffset; 267347221757SJohn Dyson int cnt; 26741efb74fbSJohn Dyson 267595e5e988SJohn Dyson if (npages) 267695e5e988SJohn Dyson *npages = 0; 267795e5e988SJohn Dyson 267847221757SJohn Dyson cnt = cnta; 26792d8acc0fSJohn Dyson uaddr = uaddra; 26802d8acc0fSJohn Dyson 26811efb74fbSJohn Dyson while (cnt > 0) { 26821efb74fbSJohn Dyson map = mapa; 26831efb74fbSJohn Dyson 26841efb74fbSJohn Dyson if ((vm_map_lookup(&map, uaddr, 26852d8acc0fSJohn Dyson VM_PROT_READ, &entry, &first_object, 26862d8acc0fSJohn Dyson &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 26871efb74fbSJohn Dyson return EFAULT; 26881efb74fbSJohn Dyson } 26891efb74fbSJohn Dyson 26902d8acc0fSJohn Dyson vm_map_clip_start(map, entry, uaddr); 26911efb74fbSJohn Dyson 26921efb74fbSJohn Dyson tcnt = cnt; 26932d8acc0fSJohn Dyson tend = uaddr + tcnt; 26942d8acc0fSJohn Dyson if (tend > entry->end) { 26952d8acc0fSJohn Dyson tcnt = entry->end - uaddr; 26962d8acc0fSJohn Dyson tend = entry->end; 26972d8acc0fSJohn Dyson } 26981efb74fbSJohn Dyson 26992d8acc0fSJohn Dyson vm_map_clip_end(map, entry, tend); 27001efb74fbSJohn Dyson 27012d8acc0fSJohn Dyson start = entry->start; 27022d8acc0fSJohn Dyson end = entry->end; 27031efb74fbSJohn Dyson 2704c2e11a03SJohn Dyson osize = atop(tcnt); 270595e5e988SJohn Dyson 2706925a3a41SJohn Dyson oindex = OFF_TO_IDX(cp); 270795e5e988SJohn Dyson if (npages) { 2708925a3a41SJohn Dyson vm_pindex_t idx; 270995e5e988SJohn Dyson for (idx = 0; idx < osize; idx++) { 271095e5e988SJohn Dyson vm_page_t m; 2711925a3a41SJohn Dyson if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 27122d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 271395e5e988SJohn Dyson return 0; 271495e5e988SJohn Dyson } 27151c7c3c6aSMatthew Dillon /* 27161c7c3c6aSMatthew Dillon * disallow busy or invalid pages, but allow 27171c7c3c6aSMatthew Dillon * m->busy pages if they are entirely valid. 27181c7c3c6aSMatthew Dillon */ 2719925a3a41SJohn Dyson if ((m->flags & PG_BUSY) || 272095e5e988SJohn Dyson ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 27212d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 272295e5e988SJohn Dyson return 0; 272395e5e988SJohn Dyson } 272495e5e988SJohn Dyson } 272595e5e988SJohn Dyson } 272695e5e988SJohn Dyson 27271efb74fbSJohn Dyson /* 27281efb74fbSJohn Dyson * If we are changing an existing map entry, just redirect 27291efb74fbSJohn Dyson * the object, and change mappings. 27301efb74fbSJohn Dyson */ 27312d8acc0fSJohn Dyson if ((first_object->type == OBJT_VNODE) && 27322d8acc0fSJohn Dyson ((oldobject = entry->object.vm_object) == first_object)) { 27332d8acc0fSJohn Dyson 27342d8acc0fSJohn Dyson if ((entry->offset != cp) || (oldobject != srcobject)) { 27352d8acc0fSJohn Dyson /* 27362d8acc0fSJohn Dyson * Remove old window into the file 27372d8acc0fSJohn Dyson */ 27382d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 27392d8acc0fSJohn Dyson 27402d8acc0fSJohn Dyson /* 27412d8acc0fSJohn Dyson * Force copy on write for mmaped regions 27422d8acc0fSJohn Dyson */ 27432d8acc0fSJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 27442d8acc0fSJohn Dyson 27452d8acc0fSJohn Dyson /* 27462d8acc0fSJohn Dyson * Point the object appropriately 27472d8acc0fSJohn Dyson */ 27482d8acc0fSJohn Dyson if (oldobject != srcobject) { 27492d8acc0fSJohn Dyson 27502d8acc0fSJohn Dyson /* 27512d8acc0fSJohn Dyson * Set the object optimization hint flag 27522d8acc0fSJohn Dyson */ 2753069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 27542d8acc0fSJohn Dyson vm_object_reference(srcobject); 27552d8acc0fSJohn Dyson entry->object.vm_object = srcobject; 27562d8acc0fSJohn Dyson 27572d8acc0fSJohn Dyson if (oldobject) { 27582d8acc0fSJohn Dyson vm_object_deallocate(oldobject); 27592d8acc0fSJohn Dyson } 27602d8acc0fSJohn Dyson } 27612d8acc0fSJohn Dyson 27622d8acc0fSJohn Dyson entry->offset = cp; 27632d8acc0fSJohn Dyson map->timestamp++; 27642d8acc0fSJohn Dyson } else { 27652d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 27662d8acc0fSJohn Dyson } 27672d8acc0fSJohn Dyson 27682d8acc0fSJohn Dyson } else if ((first_object->ref_count == 1) && 2769925a3a41SJohn Dyson (first_object->size == osize) && 277047221757SJohn Dyson ((first_object->type == OBJT_DEFAULT) || 277147221757SJohn Dyson (first_object->type == OBJT_SWAP)) ) { 2772925a3a41SJohn Dyson 2773925a3a41SJohn Dyson oldobject = first_object->backing_object; 2774925a3a41SJohn Dyson 2775925a3a41SJohn Dyson if ((first_object->backing_object_offset != cp) || 2776925a3a41SJohn Dyson (oldobject != srcobject)) { 2777925a3a41SJohn Dyson /* 2778925a3a41SJohn Dyson * Remove old window into the file 2779925a3a41SJohn Dyson */ 27802d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 2781925a3a41SJohn Dyson 2782925a3a41SJohn Dyson /* 278347221757SJohn Dyson * Remove unneeded old pages 278447221757SJohn Dyson */ 278547221757SJohn Dyson vm_object_page_remove(first_object, 0, 0, 0); 278647221757SJohn Dyson 278747221757SJohn Dyson /* 278847221757SJohn Dyson * Invalidate swap space 278947221757SJohn Dyson */ 279047221757SJohn Dyson if (first_object->type == OBJT_SWAP) { 279147221757SJohn Dyson swap_pager_freespace(first_object, 27921c7c3c6aSMatthew Dillon 0, 279347221757SJohn Dyson first_object->size); 279447221757SJohn Dyson } 279547221757SJohn Dyson 279647221757SJohn Dyson /* 2797925a3a41SJohn Dyson * Force copy on write for mmaped regions 2798925a3a41SJohn Dyson */ 279947221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 28001efb74fbSJohn Dyson 28011efb74fbSJohn Dyson /* 28021efb74fbSJohn Dyson * Point the object appropriately 28031efb74fbSJohn Dyson */ 2804925a3a41SJohn Dyson if (oldobject != srcobject) { 280547221757SJohn Dyson 2806925a3a41SJohn Dyson /* 2807925a3a41SJohn Dyson * Set the object optimization hint flag 2808925a3a41SJohn Dyson */ 2809069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 2810925a3a41SJohn Dyson vm_object_reference(srcobject); 2811925a3a41SJohn Dyson 2812925a3a41SJohn Dyson if (oldobject) { 2813925a3a41SJohn Dyson TAILQ_REMOVE(&oldobject->shadow_head, 2814925a3a41SJohn Dyson first_object, shadow_list); 2815925a3a41SJohn Dyson oldobject->shadow_count--; 2816b4309055SMatthew Dillon /* XXX bump generation? */ 2817925a3a41SJohn Dyson vm_object_deallocate(oldobject); 2818925a3a41SJohn Dyson } 2819925a3a41SJohn Dyson 2820925a3a41SJohn Dyson TAILQ_INSERT_TAIL(&srcobject->shadow_head, 2821925a3a41SJohn Dyson first_object, shadow_list); 2822925a3a41SJohn Dyson srcobject->shadow_count++; 2823b4309055SMatthew Dillon /* XXX bump generation? */ 2824925a3a41SJohn Dyson 2825925a3a41SJohn Dyson first_object->backing_object = srcobject; 2826925a3a41SJohn Dyson } 28271efb74fbSJohn Dyson first_object->backing_object_offset = cp; 28282d8acc0fSJohn Dyson map->timestamp++; 2829925a3a41SJohn Dyson } else { 28302d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 2831925a3a41SJohn Dyson } 28321efb74fbSJohn Dyson /* 28331efb74fbSJohn Dyson * Otherwise, we have to do a logical mmap. 28341efb74fbSJohn Dyson */ 28351efb74fbSJohn Dyson } else { 28361efb74fbSJohn Dyson 2837069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 2838925a3a41SJohn Dyson vm_object_reference(srcobject); 28391efb74fbSJohn Dyson 28402d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 28411efb74fbSJohn Dyson 284247221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 284347221757SJohn Dyson vm_map_lock_upgrade(map); 28441efb74fbSJohn Dyson 28452d8acc0fSJohn Dyson if (entry == &map->header) { 28461efb74fbSJohn Dyson map->first_free = &map->header; 28471efb74fbSJohn Dyson } else if (map->first_free->start >= start) { 28482d8acc0fSJohn Dyson map->first_free = entry->prev; 28491efb74fbSJohn Dyson } 28501efb74fbSJohn Dyson 28512d8acc0fSJohn Dyson SAVE_HINT(map, entry->prev); 28522d8acc0fSJohn Dyson vm_map_entry_delete(map, entry); 28531efb74fbSJohn Dyson 28542d8acc0fSJohn Dyson object = srcobject; 28552d8acc0fSJohn Dyson ooffset = cp; 28562d8acc0fSJohn Dyson 28572d8acc0fSJohn Dyson rv = vm_map_insert(map, object, ooffset, start, tend, 2858e5f13bddSAlan Cox VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE); 28591efb74fbSJohn Dyson 28601efb74fbSJohn Dyson if (rv != KERN_SUCCESS) 28611efb74fbSJohn Dyson panic("vm_uiomove: could not insert new entry: %d", rv); 28621efb74fbSJohn Dyson } 28631efb74fbSJohn Dyson 28641efb74fbSJohn Dyson /* 28651efb74fbSJohn Dyson * Map the window directly, if it is already in memory 28661efb74fbSJohn Dyson */ 28672d8acc0fSJohn Dyson pmap_object_init_pt(map->pmap, uaddr, 28682d8acc0fSJohn Dyson srcobject, oindex, tcnt, 0); 28691efb74fbSJohn Dyson 287047221757SJohn Dyson map->timestamp++; 28711efb74fbSJohn Dyson vm_map_unlock(map); 28721efb74fbSJohn Dyson 28731efb74fbSJohn Dyson cnt -= tcnt; 28742d8acc0fSJohn Dyson uaddr += tcnt; 28751efb74fbSJohn Dyson cp += tcnt; 287695e5e988SJohn Dyson if (npages) 287795e5e988SJohn Dyson *npages += osize; 28781efb74fbSJohn Dyson } 28791efb74fbSJohn Dyson return 0; 28801efb74fbSJohn Dyson } 28811efb74fbSJohn Dyson 28821efb74fbSJohn Dyson /* 28831efb74fbSJohn Dyson * Performs the copy_on_write operations necessary to allow the virtual copies 28841efb74fbSJohn Dyson * into user space to work. This has to be called for write(2) system calls 28851efb74fbSJohn Dyson * from other processes, file unlinking, and file size shrinkage. 28861efb74fbSJohn Dyson */ 28871efb74fbSJohn Dyson void 28881efb74fbSJohn Dyson vm_freeze_copyopts(object, froma, toa) 28891efb74fbSJohn Dyson vm_object_t object; 28901efb74fbSJohn Dyson vm_pindex_t froma, toa; 28911efb74fbSJohn Dyson { 2892f5ef029eSPoul-Henning Kamp int rv; 2893f5ef029eSPoul-Henning Kamp vm_object_t robject; 2894f5ef029eSPoul-Henning Kamp vm_pindex_t idx; 28951efb74fbSJohn Dyson 28962d8acc0fSJohn Dyson if ((object == NULL) || 289795e5e988SJohn Dyson ((object->flags & OBJ_OPT) == 0)) 289895e5e988SJohn Dyson return; 28991efb74fbSJohn Dyson 29001efb74fbSJohn Dyson if (object->shadow_count > object->ref_count) 29011efb74fbSJohn Dyson panic("vm_freeze_copyopts: sc > rc"); 29021efb74fbSJohn Dyson 29038aef1712SMatthew Dillon while((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 29041efb74fbSJohn Dyson vm_pindex_t bo_pindex; 29051efb74fbSJohn Dyson vm_page_t m_in, m_out; 29061efb74fbSJohn Dyson 29071efb74fbSJohn Dyson bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 29081efb74fbSJohn Dyson 290995e5e988SJohn Dyson vm_object_reference(robject); 2910925a3a41SJohn Dyson 291166095752SJohn Dyson vm_object_pip_wait(robject, "objfrz"); 2912925a3a41SJohn Dyson 29131efb74fbSJohn Dyson if (robject->ref_count == 1) { 29141efb74fbSJohn Dyson vm_object_deallocate(robject); 29151efb74fbSJohn Dyson continue; 29161efb74fbSJohn Dyson } 29171efb74fbSJohn Dyson 2918d474eaaaSDoug Rabson vm_object_pip_add(robject, 1); 29191efb74fbSJohn Dyson 292047221757SJohn Dyson for (idx = 0; idx < robject->size; idx++) { 29211efb74fbSJohn Dyson 292295461b45SJohn Dyson m_out = vm_page_grab(robject, idx, 292395461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 29241efb74fbSJohn Dyson 29251efb74fbSJohn Dyson if (m_out->valid == 0) { 292695461b45SJohn Dyson m_in = vm_page_grab(object, bo_pindex + idx, 292795461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 292895461b45SJohn Dyson if (m_in->valid == 0) { 292947221757SJohn Dyson rv = vm_pager_get_pages(object, &m_in, 1, 0); 293047221757SJohn Dyson if (rv != VM_PAGER_OK) { 29313efc015bSPeter Wemm printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 293247221757SJohn Dyson continue; 29331efb74fbSJohn Dyson } 293495461b45SJohn Dyson vm_page_deactivate(m_in); 293547221757SJohn Dyson } 293647221757SJohn Dyson 293747221757SJohn Dyson vm_page_protect(m_in, VM_PROT_NONE); 293847221757SJohn Dyson pmap_copy_page(VM_PAGE_TO_PHYS(m_in), VM_PAGE_TO_PHYS(m_out)); 293995461b45SJohn Dyson m_out->valid = m_in->valid; 29407dbf82dcSMatthew Dillon vm_page_dirty(m_out); 294195461b45SJohn Dyson vm_page_activate(m_out); 2942e69763a3SDoug Rabson vm_page_wakeup(m_in); 29431efb74fbSJohn Dyson } 2944e69763a3SDoug Rabson vm_page_wakeup(m_out); 294547221757SJohn Dyson } 2946925a3a41SJohn Dyson 29471efb74fbSJohn Dyson object->shadow_count--; 294847221757SJohn Dyson object->ref_count--; 29491efb74fbSJohn Dyson TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 29501efb74fbSJohn Dyson robject->backing_object = NULL; 29511efb74fbSJohn Dyson robject->backing_object_offset = 0; 29521efb74fbSJohn Dyson 295347221757SJohn Dyson vm_object_pip_wakeup(robject); 29541efb74fbSJohn Dyson vm_object_deallocate(robject); 29551efb74fbSJohn Dyson } 295647221757SJohn Dyson 2957069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_OPT); 29581efb74fbSJohn Dyson } 29591efb74fbSJohn Dyson 2960c7c34a24SBruce Evans #include "opt_ddb.h" 2961c3cb3e12SDavid Greenman #ifdef DDB 2962c7c34a24SBruce Evans #include <sys/kernel.h> 2963c7c34a24SBruce Evans 2964c7c34a24SBruce Evans #include <ddb/ddb.h> 2965c7c34a24SBruce Evans 2966df8bae1dSRodney W. Grimes /* 2967df8bae1dSRodney W. Grimes * vm_map_print: [ debug ] 2968df8bae1dSRodney W. Grimes */ 2969c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print) 2970df8bae1dSRodney W. Grimes { 297195e5e988SJohn Dyson static int nlines; 2972c7c34a24SBruce Evans /* XXX convert args. */ 2973c0877f10SJohn Dyson vm_map_t map = (vm_map_t)addr; 2974c7c34a24SBruce Evans boolean_t full = have_addr; 2975df8bae1dSRodney W. Grimes 2976c0877f10SJohn Dyson vm_map_entry_t entry; 2977c7c34a24SBruce Evans 2978e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 2979e5f251d2SAlan Cox (void *)map, 2980101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 298195e5e988SJohn Dyson nlines++; 2982df8bae1dSRodney W. Grimes 2983c7c34a24SBruce Evans if (!full && db_indent) 2984df8bae1dSRodney W. Grimes return; 2985df8bae1dSRodney W. Grimes 2986c7c34a24SBruce Evans db_indent += 2; 2987df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 2988df8bae1dSRodney W. Grimes entry = entry->next) { 2989fc62ef1fSBruce Evans db_iprintf("map entry %p: start=%p, end=%p\n", 2990fc62ef1fSBruce Evans (void *)entry, (void *)entry->start, (void *)entry->end); 299195e5e988SJohn Dyson nlines++; 2992e5f251d2SAlan Cox { 2993df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 2994df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 29950d94caffSDavid Greenman 299695e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 2997df8bae1dSRodney W. Grimes entry->protection, 2998df8bae1dSRodney W. Grimes entry->max_protection, 29998aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 3000df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 300195e5e988SJohn Dyson db_printf(", wired"); 3002df8bae1dSRodney W. Grimes } 30039fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3004101eeb7fSBruce Evans /* XXX no %qd in kernel. Truncate entry->offset. */ 3005101eeb7fSBruce Evans db_printf(", share=%p, offset=0x%lx\n", 30069fdfe602SMatthew Dillon (void *)entry->object.sub_map, 3007ecbb00a2SDoug Rabson (long)entry->offset); 300895e5e988SJohn Dyson nlines++; 3009df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 30109fdfe602SMatthew Dillon (entry->prev->object.sub_map != 30119fdfe602SMatthew Dillon entry->object.sub_map)) { 3012c7c34a24SBruce Evans db_indent += 2; 3013101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t) 30149fdfe602SMatthew Dillon entry->object.sub_map, 3015914181e7SBruce Evans full, 0, (char *)0); 3016c7c34a24SBruce Evans db_indent -= 2; 3017df8bae1dSRodney W. Grimes } 30180d94caffSDavid Greenman } else { 3019101eeb7fSBruce Evans /* XXX no %qd in kernel. Truncate entry->offset. */ 3020101eeb7fSBruce Evans db_printf(", object=%p, offset=0x%lx", 3021101eeb7fSBruce Evans (void *)entry->object.vm_object, 3022ecbb00a2SDoug Rabson (long)entry->offset); 3023afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 3024c7c34a24SBruce Evans db_printf(", copy (%s)", 3025afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3026c7c34a24SBruce Evans db_printf("\n"); 302795e5e988SJohn Dyson nlines++; 3028df8bae1dSRodney W. Grimes 3029df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 3030df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 3031df8bae1dSRodney W. Grimes entry->object.vm_object)) { 3032c7c34a24SBruce Evans db_indent += 2; 3033101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 3034101eeb7fSBruce Evans entry->object.vm_object, 3035914181e7SBruce Evans full, 0, (char *)0); 303695e5e988SJohn Dyson nlines += 4; 3037c7c34a24SBruce Evans db_indent -= 2; 3038df8bae1dSRodney W. Grimes } 3039df8bae1dSRodney W. Grimes } 3040df8bae1dSRodney W. Grimes } 3041c7c34a24SBruce Evans db_indent -= 2; 304295e5e988SJohn Dyson if (db_indent == 0) 304395e5e988SJohn Dyson nlines = 0; 3044df8bae1dSRodney W. Grimes } 304595e5e988SJohn Dyson 304695e5e988SJohn Dyson 304795e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 304895e5e988SJohn Dyson { 304995e5e988SJohn Dyson struct proc *p; 305095e5e988SJohn Dyson 305195e5e988SJohn Dyson if (have_addr) { 305295e5e988SJohn Dyson p = (struct proc *) addr; 305395e5e988SJohn Dyson } else { 305495e5e988SJohn Dyson p = curproc; 305595e5e988SJohn Dyson } 305695e5e988SJohn Dyson 3057ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3058ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3059b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 306095e5e988SJohn Dyson 3061101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 306295e5e988SJohn Dyson } 306395e5e988SJohn Dyson 3064c7c34a24SBruce Evans #endif /* DDB */ 3065