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 175929bcfaSPhilippe Charnier * must display the following acknowledgement: 18df8bae1dSRodney W. Grimes * This product includes software developed by the University of 19df8bae1dSRodney W. Grimes * California, Berkeley and its contributors. 20df8bae1dSRodney W. Grimes * 4. Neither the name of the University nor the names of its contributors 21df8bae1dSRodney W. Grimes * may be used to endorse or promote products derived from this software 22df8bae1dSRodney W. Grimes * without specific prior written permission. 23df8bae1dSRodney W. Grimes * 24df8bae1dSRodney W. Grimes * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 25df8bae1dSRodney W. Grimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26df8bae1dSRodney W. Grimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27df8bae1dSRodney W. Grimes * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 28df8bae1dSRodney W. Grimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29df8bae1dSRodney W. Grimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30df8bae1dSRodney W. Grimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31df8bae1dSRodney W. Grimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32df8bae1dSRodney W. Grimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33df8bae1dSRodney W. Grimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34df8bae1dSRodney W. Grimes * SUCH DAMAGE. 35df8bae1dSRodney W. Grimes * 363c4dd356SDavid Greenman * from: @(#)vm_map.c 8.3 (Berkeley) 1/12/94 37df8bae1dSRodney W. Grimes * 38df8bae1dSRodney W. Grimes * 39df8bae1dSRodney W. Grimes * Copyright (c) 1987, 1990 Carnegie-Mellon University. 40df8bae1dSRodney W. Grimes * All rights reserved. 41df8bae1dSRodney W. Grimes * 42df8bae1dSRodney W. Grimes * Authors: Avadis Tevanian, Jr., Michael Wayne Young 43df8bae1dSRodney W. Grimes * 44df8bae1dSRodney W. Grimes * Permission to use, copy, modify and distribute this software and 45df8bae1dSRodney W. Grimes * its documentation is hereby granted, provided that both the copyright 46df8bae1dSRodney W. Grimes * notice and this permission notice appear in all copies of the 47df8bae1dSRodney W. Grimes * software, derivative works or modified versions, and any portions 48df8bae1dSRodney W. Grimes * thereof, and that both notices appear in supporting documentation. 49df8bae1dSRodney W. Grimes * 50df8bae1dSRodney W. Grimes * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS" 51df8bae1dSRodney W. Grimes * CONDITION. CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND 52df8bae1dSRodney W. Grimes * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE. 53df8bae1dSRodney W. Grimes * 54df8bae1dSRodney W. Grimes * Carnegie Mellon requests users of this software to return to 55df8bae1dSRodney W. Grimes * 56df8bae1dSRodney W. Grimes * Software Distribution Coordinator or Software.Distribution@CS.CMU.EDU 57df8bae1dSRodney W. Grimes * School of Computer Science 58df8bae1dSRodney W. Grimes * Carnegie Mellon University 59df8bae1dSRodney W. Grimes * Pittsburgh PA 15213-3890 60df8bae1dSRodney W. Grimes * 61df8bae1dSRodney W. Grimes * any improvements or extensions that they make and grant Carnegie the 62df8bae1dSRodney W. Grimes * rights to redistribute these changes. 633c4dd356SDavid Greenman * 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> 7361d80e90SJohn Baldwin #include <sys/ktr.h> 74fb919e4dSMark Murray #include <sys/lock.h> 75fb919e4dSMark Murray #include <sys/mutex.h> 76b5e8ce9fSBruce Evans #include <sys/proc.h> 77efeaf95aSDavid Greenman #include <sys/vmmeter.h> 78867a482dSJohn Dyson #include <sys/mman.h> 791efb74fbSJohn Dyson #include <sys/vnode.h> 802267af78SJulian Elischer #include <sys/resourcevar.h> 81df8bae1dSRodney W. Grimes 82df8bae1dSRodney W. Grimes #include <vm/vm.h> 83efeaf95aSDavid Greenman #include <vm/vm_param.h> 84efeaf95aSDavid Greenman #include <vm/pmap.h> 85efeaf95aSDavid Greenman #include <vm/vm_map.h> 86df8bae1dSRodney W. Grimes #include <vm/vm_page.h> 87df8bae1dSRodney W. Grimes #include <vm/vm_object.h> 8847221757SJohn Dyson #include <vm/vm_pager.h> 8926f9a767SRodney W. Grimes #include <vm/vm_kern.h> 90efeaf95aSDavid Greenman #include <vm/vm_extern.h> 9121cd6e62SSeigo Tanimura #include <vm/swap_pager.h> 92670d17b5SJeff Roberson #include <vm/uma.h> 93df8bae1dSRodney W. Grimes 94df8bae1dSRodney W. Grimes /* 95df8bae1dSRodney W. Grimes * Virtual memory maps provide for the mapping, protection, 96df8bae1dSRodney W. Grimes * and sharing of virtual memory objects. In addition, 97df8bae1dSRodney W. Grimes * this module provides for an efficient virtual copy of 98df8bae1dSRodney W. Grimes * memory from one map to another. 99df8bae1dSRodney W. Grimes * 100df8bae1dSRodney W. Grimes * Synchronization is required prior to most operations. 101df8bae1dSRodney W. Grimes * 102df8bae1dSRodney W. Grimes * Maps consist of an ordered doubly-linked list of simple 103df8bae1dSRodney W. Grimes * entries; a single hint is used to speed up lookups. 104df8bae1dSRodney W. Grimes * 105956f3135SPhilippe Charnier * Since portions of maps are specified by start/end addresses, 106df8bae1dSRodney W. Grimes * which may not align with existing map entries, all 107df8bae1dSRodney W. Grimes * routines merely "clip" entries to these start/end values. 108df8bae1dSRodney W. Grimes * [That is, an entry is split into two, bordering at a 109df8bae1dSRodney W. Grimes * start or end value.] Note that these clippings may not 110df8bae1dSRodney W. Grimes * always be necessary (as the two resulting entries are then 111df8bae1dSRodney W. Grimes * not changed); however, the clipping is done for convenience. 112df8bae1dSRodney W. Grimes * 113df8bae1dSRodney W. Grimes * As mentioned above, virtual copy operations are performed 114ad5fca3bSAlan Cox * by copying VM object references from one map to 115df8bae1dSRodney W. Grimes * another, and then marking both regions as copy-on-write. 116df8bae1dSRodney W. Grimes */ 117df8bae1dSRodney W. Grimes 118df8bae1dSRodney W. Grimes /* 119df8bae1dSRodney W. Grimes * vm_map_startup: 120df8bae1dSRodney W. Grimes * 121df8bae1dSRodney W. Grimes * Initialize the vm_map module. Must be called before 122df8bae1dSRodney W. Grimes * any other vm_map routines. 123df8bae1dSRodney W. Grimes * 124df8bae1dSRodney W. Grimes * Map and entry structures are allocated from the general 125df8bae1dSRodney W. Grimes * purpose memory pool with some exceptions: 126df8bae1dSRodney W. Grimes * 127df8bae1dSRodney W. Grimes * - The kernel map and kmem submap are allocated statically. 128df8bae1dSRodney W. Grimes * - Kernel map entries are allocated out of a static pool. 129df8bae1dSRodney W. Grimes * 130df8bae1dSRodney W. Grimes * These restrictions are necessary since malloc() uses the 131df8bae1dSRodney W. Grimes * maps and requires map entries. 132df8bae1dSRodney W. Grimes */ 133df8bae1dSRodney W. Grimes 1348355f576SJeff Roberson static uma_zone_t mapentzone; 1358355f576SJeff Roberson static uma_zone_t kmapentzone; 1368355f576SJeff Roberson static uma_zone_t mapzone; 1378355f576SJeff Roberson static uma_zone_t vmspace_zone; 1388355f576SJeff Roberson static struct vm_object kmapentobj; 1398355f576SJeff Roberson static void vmspace_zinit(void *mem, int size); 1408355f576SJeff Roberson static void vmspace_zfini(void *mem, int size); 1418355f576SJeff Roberson static void vm_map_zinit(void *mem, int size); 1428355f576SJeff Roberson static void vm_map_zfini(void *mem, int size); 1438355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max); 1441fc43fd1SAlan Cox 1458355f576SJeff Roberson #ifdef INVARIANTS 1468355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg); 1478355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg); 1488355f576SJeff Roberson #endif 149b18bfc3dSJohn Dyson 1500d94caffSDavid Greenman void 1511b40f8c0SMatthew Dillon vm_map_startup(void) 152df8bae1dSRodney W. Grimes { 1538355f576SJeff Roberson mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL, 1548355f576SJeff Roberson #ifdef INVARIANTS 1558355f576SJeff Roberson vm_map_zdtor, 1568355f576SJeff Roberson #else 1578355f576SJeff Roberson NULL, 1588355f576SJeff Roberson #endif 1598355f576SJeff Roberson vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1608355f576SJeff Roberson uma_prealloc(mapzone, MAX_KMAP); 161670d17b5SJeff Roberson kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry), 162670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 163670d17b5SJeff Roberson uma_prealloc(kmapentzone, MAX_KMAPENT); 164670d17b5SJeff Roberson mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry), 165670d17b5SJeff Roberson NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0); 1668355f576SJeff Roberson uma_prealloc(mapentzone, MAX_MAPENT); 167df8bae1dSRodney W. Grimes } 168df8bae1dSRodney W. Grimes 1698355f576SJeff Roberson static void 1708355f576SJeff Roberson vmspace_zfini(void *mem, int size) 1718355f576SJeff Roberson { 1728355f576SJeff Roberson struct vmspace *vm; 1738355f576SJeff Roberson 1748355f576SJeff Roberson vm = (struct vmspace *)mem; 1758355f576SJeff Roberson 1768355f576SJeff Roberson vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map)); 1778355f576SJeff Roberson } 1788355f576SJeff Roberson 1798355f576SJeff Roberson static void 1808355f576SJeff Roberson vmspace_zinit(void *mem, int size) 1818355f576SJeff Roberson { 1828355f576SJeff Roberson struct vmspace *vm; 1838355f576SJeff Roberson 1848355f576SJeff Roberson vm = (struct vmspace *)mem; 1858355f576SJeff Roberson 1868355f576SJeff Roberson vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map)); 1878355f576SJeff Roberson } 1888355f576SJeff Roberson 1898355f576SJeff Roberson static void 1908355f576SJeff Roberson vm_map_zfini(void *mem, int size) 1918355f576SJeff Roberson { 1928355f576SJeff Roberson vm_map_t map; 1938355f576SJeff Roberson 1948355f576SJeff Roberson GIANT_REQUIRED; 1958355f576SJeff Roberson map = (vm_map_t)mem; 1968355f576SJeff Roberson 1978355f576SJeff Roberson lockdestroy(&map->lock); 1988355f576SJeff Roberson } 1998355f576SJeff Roberson 2008355f576SJeff Roberson static void 2018355f576SJeff Roberson vm_map_zinit(void *mem, int size) 2028355f576SJeff Roberson { 2038355f576SJeff Roberson vm_map_t map; 2048355f576SJeff Roberson 2058355f576SJeff Roberson GIANT_REQUIRED; 2068355f576SJeff Roberson 2078355f576SJeff Roberson map = (vm_map_t)mem; 2088355f576SJeff Roberson map->nentries = 0; 2098355f576SJeff Roberson map->size = 0; 2108355f576SJeff Roberson map->infork = 0; 2118355f576SJeff Roberson lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE); 2128355f576SJeff Roberson } 2138355f576SJeff Roberson 2148355f576SJeff Roberson #ifdef INVARIANTS 2158355f576SJeff Roberson static void 2168355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg) 2178355f576SJeff Roberson { 2188355f576SJeff Roberson struct vmspace *vm; 2198355f576SJeff Roberson 2208355f576SJeff Roberson vm = (struct vmspace *)mem; 2218355f576SJeff Roberson 2228355f576SJeff Roberson vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg); 2238355f576SJeff Roberson } 2248355f576SJeff Roberson static void 2258355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg) 2268355f576SJeff Roberson { 2278355f576SJeff Roberson vm_map_t map; 2288355f576SJeff Roberson 2298355f576SJeff Roberson map = (vm_map_t)mem; 2308355f576SJeff Roberson KASSERT(map->nentries == 0, 2318355f576SJeff Roberson ("map %p nentries == %d on free.", 2328355f576SJeff Roberson map, map->nentries)); 2338355f576SJeff Roberson KASSERT(map->size == 0, 2348355f576SJeff Roberson ("map %p size == %lu on free.", 2359eb6e519SJeff Roberson map, (unsigned long)map->size)); 2368355f576SJeff Roberson KASSERT(map->infork == 0, 2378355f576SJeff Roberson ("map %p infork == %d on free.", 2388355f576SJeff Roberson map, map->infork)); 2398355f576SJeff Roberson } 2408355f576SJeff Roberson #endif /* INVARIANTS */ 2418355f576SJeff Roberson 242df8bae1dSRodney W. Grimes /* 243df8bae1dSRodney W. Grimes * Allocate a vmspace structure, including a vm_map and pmap, 244df8bae1dSRodney W. Grimes * and initialize those structures. The refcnt is set to 1. 245df8bae1dSRodney W. Grimes * The remaining fields must be initialized by the caller. 246df8bae1dSRodney W. Grimes */ 247df8bae1dSRodney W. Grimes struct vmspace * 2482d8acc0fSJohn Dyson vmspace_alloc(min, max) 249df8bae1dSRodney W. Grimes vm_offset_t min, max; 250df8bae1dSRodney W. Grimes { 251c0877f10SJohn Dyson struct vmspace *vm; 2520d94caffSDavid Greenman 2530cddd8f0SMatthew Dillon GIANT_REQUIRED; 2548355f576SJeff Roberson vm = uma_zalloc(vmspace_zone, M_WAITOK); 25521c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_alloc: %p", vm); 2568355f576SJeff Roberson _vm_map_init(&vm->vm_map, min, max); 257b1028ad1SLuoqi Chen pmap_pinit(vmspace_pmap(vm)); 258b1028ad1SLuoqi Chen vm->vm_map.pmap = vmspace_pmap(vm); /* XXX */ 259df8bae1dSRodney W. Grimes vm->vm_refcnt = 1; 2602d8acc0fSJohn Dyson vm->vm_shm = NULL; 261582ec34cSAlfred Perlstein vm->vm_freer = NULL; 262df8bae1dSRodney W. Grimes return (vm); 263df8bae1dSRodney W. Grimes } 264df8bae1dSRodney W. Grimes 265df8bae1dSRodney W. Grimes void 2661b40f8c0SMatthew Dillon vm_init2(void) 2671b40f8c0SMatthew Dillon { 2688355f576SJeff Roberson uma_zone_set_obj(kmapentzone, &kmapentobj, cnt.v_page_count / 4); 2698355f576SJeff Roberson vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL, 2708355f576SJeff Roberson #ifdef INVARIANTS 2718355f576SJeff Roberson vmspace_zdtor, 2728355f576SJeff Roberson #else 2738355f576SJeff Roberson NULL, 2748355f576SJeff Roberson #endif 2758355f576SJeff Roberson vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 276ba9be04cSJohn Dyson pmap_init2(); 27799448ed1SJohn Dyson vm_object_init2(); 2783075778bSJohn Dyson } 2793075778bSJohn Dyson 280582ec34cSAlfred Perlstein static __inline void 281582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm) 282df8bae1dSRodney W. Grimes { 28321c641b2SJohn Baldwin CTR1(KTR_VM, "vmspace_free: %p", vm); 28430dcfc09SJohn Dyson /* 285df8bae1dSRodney W. Grimes * Lock the map, to wait out all other references to it. 2860d94caffSDavid Greenman * Delete all of the mappings and pages they hold, then call 2870d94caffSDavid Greenman * the pmap module to reclaim anything left. 288df8bae1dSRodney W. Grimes */ 289df8bae1dSRodney W. Grimes vm_map_lock(&vm->vm_map); 290df8bae1dSRodney W. Grimes (void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset, 291df8bae1dSRodney W. Grimes vm->vm_map.max_offset); 292a1f6d91cSDavid Greenman vm_map_unlock(&vm->vm_map); 2938355f576SJeff Roberson 294b1028ad1SLuoqi Chen pmap_release(vmspace_pmap(vm)); 2958355f576SJeff Roberson uma_zfree(vmspace_zone, vm); 296df8bae1dSRodney W. Grimes } 297582ec34cSAlfred Perlstein 298582ec34cSAlfred Perlstein void 299582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm) 300582ec34cSAlfred Perlstein { 301582ec34cSAlfred Perlstein GIANT_REQUIRED; 302582ec34cSAlfred Perlstein 303582ec34cSAlfred Perlstein if (vm->vm_refcnt == 0) 304582ec34cSAlfred Perlstein panic("vmspace_free: attempt to free already freed vmspace"); 305582ec34cSAlfred Perlstein 306582ec34cSAlfred Perlstein if (--vm->vm_refcnt == 0) 307582ec34cSAlfred Perlstein vmspace_dofree(vm); 308582ec34cSAlfred Perlstein } 309582ec34cSAlfred Perlstein 310582ec34cSAlfred Perlstein void 311582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p) 312582ec34cSAlfred Perlstein { 313334f7061SPeter Wemm struct vmspace *vm; 314582ec34cSAlfred Perlstein 315334f7061SPeter Wemm GIANT_REQUIRED; 316334f7061SPeter Wemm if (p == p->p_vmspace->vm_freer) { 317334f7061SPeter Wemm vm = p->p_vmspace; 318334f7061SPeter Wemm p->p_vmspace = NULL; 319334f7061SPeter Wemm vmspace_dofree(vm); 320334f7061SPeter Wemm } 321df8bae1dSRodney W. Grimes } 322df8bae1dSRodney W. Grimes 323df8bae1dSRodney W. Grimes /* 324ff2b5645SMatthew Dillon * vmspace_swap_count() - count the approximate swap useage in pages for a 325ff2b5645SMatthew Dillon * vmspace. 326ff2b5645SMatthew Dillon * 327ff2b5645SMatthew Dillon * Swap useage is determined by taking the proportional swap used by 328ff2b5645SMatthew Dillon * VM objects backing the VM map. To make up for fractional losses, 329ff2b5645SMatthew Dillon * if the VM object has any swap use at all the associated map entries 330ff2b5645SMatthew Dillon * count for at least 1 swap page. 331ff2b5645SMatthew Dillon */ 332ff2b5645SMatthew Dillon int 333ff2b5645SMatthew Dillon vmspace_swap_count(struct vmspace *vmspace) 334ff2b5645SMatthew Dillon { 335ff2b5645SMatthew Dillon vm_map_t map = &vmspace->vm_map; 336ff2b5645SMatthew Dillon vm_map_entry_t cur; 337ff2b5645SMatthew Dillon int count = 0; 338ff2b5645SMatthew Dillon 339ff2b5645SMatthew Dillon for (cur = map->header.next; cur != &map->header; cur = cur->next) { 340ff2b5645SMatthew Dillon vm_object_t object; 341ff2b5645SMatthew Dillon 342ff2b5645SMatthew Dillon if ((cur->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 && 343ff2b5645SMatthew Dillon (object = cur->object.vm_object) != NULL && 344ff2b5645SMatthew Dillon object->type == OBJT_SWAP 345ff2b5645SMatthew Dillon ) { 346ff2b5645SMatthew Dillon int n = (cur->end - cur->start) / PAGE_SIZE; 347ff2b5645SMatthew Dillon 348ff2b5645SMatthew Dillon if (object->un_pager.swp.swp_bcount) { 349ef6a93efSMatthew Dillon count += object->un_pager.swp.swp_bcount * 350ef6a93efSMatthew Dillon SWAP_META_PAGES * n / object->size + 1; 351ff2b5645SMatthew Dillon } 352ff2b5645SMatthew Dillon } 353ff2b5645SMatthew Dillon } 354ff2b5645SMatthew Dillon return (count); 355ff2b5645SMatthew Dillon } 356ff2b5645SMatthew Dillon 3571b40f8c0SMatthew Dillon u_char 3581b40f8c0SMatthew Dillon vm_map_entry_behavior(struct vm_map_entry *entry) 3591b40f8c0SMatthew Dillon { 3601b40f8c0SMatthew Dillon return entry->eflags & MAP_ENTRY_BEHAV_MASK; 3611b40f8c0SMatthew Dillon } 3621b40f8c0SMatthew Dillon 3631b40f8c0SMatthew Dillon void 3641b40f8c0SMatthew Dillon vm_map_entry_set_behavior(struct vm_map_entry *entry, u_char behavior) 3651b40f8c0SMatthew Dillon { 3661b40f8c0SMatthew Dillon entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) | 3671b40f8c0SMatthew Dillon (behavior & MAP_ENTRY_BEHAV_MASK); 3681b40f8c0SMatthew Dillon } 3691b40f8c0SMatthew Dillon 3701b40f8c0SMatthew Dillon void 37125adb370SBrian Feldman vm_map_lock(vm_map_t map) 3721b40f8c0SMatthew Dillon { 3731b40f8c0SMatthew Dillon vm_map_printf("locking map LK_EXCLUSIVE: %p\n", map); 37425adb370SBrian Feldman if (lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread) != 0) 37525adb370SBrian Feldman panic("vm_map_lock: failed to get lock"); 3761b40f8c0SMatthew Dillon map->timestamp++; 3771b40f8c0SMatthew Dillon } 3781b40f8c0SMatthew Dillon 3791b40f8c0SMatthew Dillon void 38025adb370SBrian Feldman vm_map_unlock(vm_map_t map) 3810e0af8ecSBrian Feldman { 3820e0af8ecSBrian Feldman vm_map_printf("locking map LK_RELEASE: %p\n", map); 38325adb370SBrian Feldman lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread); 3840e0af8ecSBrian Feldman } 3850e0af8ecSBrian Feldman 3860e0af8ecSBrian Feldman void 38725adb370SBrian Feldman vm_map_lock_read(vm_map_t map) 3880e0af8ecSBrian Feldman { 3890e0af8ecSBrian Feldman vm_map_printf("locking map LK_SHARED: %p\n", map); 39025adb370SBrian Feldman lockmgr(&(map)->lock, LK_SHARED, NULL, curthread); 3910e0af8ecSBrian Feldman } 3920e0af8ecSBrian Feldman 3930e0af8ecSBrian Feldman void 39425adb370SBrian Feldman vm_map_unlock_read(vm_map_t map) 3950e0af8ecSBrian Feldman { 3960e0af8ecSBrian Feldman vm_map_printf("locking map LK_RELEASE: %p\n", map); 39725adb370SBrian Feldman lockmgr(&(map)->lock, LK_RELEASE, NULL, curthread); 39825adb370SBrian Feldman } 39925adb370SBrian Feldman 40025adb370SBrian Feldman static __inline__ int 40125adb370SBrian Feldman _vm_map_lock_upgrade(vm_map_t map, struct thread *td) { 40225adb370SBrian Feldman int error; 40325adb370SBrian Feldman 40425adb370SBrian Feldman vm_map_printf("locking map LK_EXCLUPGRADE: %p\n", map); 40525adb370SBrian Feldman error = lockmgr(&map->lock, LK_EXCLUPGRADE, NULL, td); 40625adb370SBrian Feldman if (error == 0) 40725adb370SBrian Feldman map->timestamp++; 40825adb370SBrian Feldman return error; 4090e0af8ecSBrian Feldman } 4100e0af8ecSBrian Feldman 4110e0af8ecSBrian Feldman int 41225adb370SBrian Feldman vm_map_lock_upgrade(vm_map_t map) 4130e0af8ecSBrian Feldman { 41425adb370SBrian Feldman return (_vm_map_lock_upgrade(map, curthread)); 4150e0af8ecSBrian Feldman } 4160e0af8ecSBrian Feldman 4170e0af8ecSBrian Feldman void 41825adb370SBrian Feldman vm_map_lock_downgrade(vm_map_t map) 4191b40f8c0SMatthew Dillon { 4201b40f8c0SMatthew Dillon vm_map_printf("locking map LK_DOWNGRADE: %p\n", map); 42125adb370SBrian Feldman lockmgr(&map->lock, LK_DOWNGRADE, NULL, curthread); 42225adb370SBrian Feldman } 42325adb370SBrian Feldman 42425adb370SBrian Feldman void 42525adb370SBrian Feldman vm_map_set_recursive(vm_map_t map) 42625adb370SBrian Feldman { 42725adb370SBrian Feldman mtx_lock((map)->lock.lk_interlock); 42825adb370SBrian Feldman map->lock.lk_flags |= LK_CANRECURSE; 42925adb370SBrian Feldman mtx_unlock((map)->lock.lk_interlock); 43025adb370SBrian Feldman } 43125adb370SBrian Feldman 43225adb370SBrian Feldman void 43325adb370SBrian Feldman vm_map_clear_recursive(vm_map_t map) 43425adb370SBrian Feldman { 43525adb370SBrian Feldman mtx_lock((map)->lock.lk_interlock); 43625adb370SBrian Feldman map->lock.lk_flags &= ~LK_CANRECURSE; 43725adb370SBrian Feldman mtx_unlock((map)->lock.lk_interlock); 4381b40f8c0SMatthew Dillon } 4391b40f8c0SMatthew Dillon 4401b40f8c0SMatthew Dillon vm_offset_t 4411b40f8c0SMatthew Dillon vm_map_min(vm_map_t map) 4421b40f8c0SMatthew Dillon { 4431b40f8c0SMatthew Dillon return (map->min_offset); 4441b40f8c0SMatthew Dillon } 4451b40f8c0SMatthew Dillon 4461b40f8c0SMatthew Dillon vm_offset_t 4471b40f8c0SMatthew Dillon vm_map_max(vm_map_t map) 4481b40f8c0SMatthew Dillon { 4491b40f8c0SMatthew Dillon return (map->max_offset); 4501b40f8c0SMatthew Dillon } 4511b40f8c0SMatthew Dillon 4521b40f8c0SMatthew Dillon struct pmap * 4531b40f8c0SMatthew Dillon vm_map_pmap(vm_map_t map) 4541b40f8c0SMatthew Dillon { 4551b40f8c0SMatthew Dillon return (map->pmap); 4561b40f8c0SMatthew Dillon } 4571b40f8c0SMatthew Dillon 4581b40f8c0SMatthew Dillon struct pmap * 4591b40f8c0SMatthew Dillon vmspace_pmap(struct vmspace *vmspace) 4601b40f8c0SMatthew Dillon { 4611b40f8c0SMatthew Dillon return &vmspace->vm_pmap; 4621b40f8c0SMatthew Dillon } 4631b40f8c0SMatthew Dillon 4641b40f8c0SMatthew Dillon long 4651b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace) 4661b40f8c0SMatthew Dillon { 4671b40f8c0SMatthew Dillon return pmap_resident_count(vmspace_pmap(vmspace)); 4681b40f8c0SMatthew Dillon } 4691b40f8c0SMatthew Dillon 470ff2b5645SMatthew Dillon /* 471df8bae1dSRodney W. Grimes * vm_map_create: 472df8bae1dSRodney W. Grimes * 473df8bae1dSRodney W. Grimes * Creates and returns a new empty VM map with 474df8bae1dSRodney W. Grimes * the given physical map structure, and having 475df8bae1dSRodney W. Grimes * the given lower and upper address bounds. 476df8bae1dSRodney W. Grimes */ 4770d94caffSDavid Greenman vm_map_t 4781b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max) 479df8bae1dSRodney W. Grimes { 480c0877f10SJohn Dyson vm_map_t result; 481df8bae1dSRodney W. Grimes 4820cddd8f0SMatthew Dillon GIANT_REQUIRED; 4830cddd8f0SMatthew Dillon 4848355f576SJeff Roberson result = uma_zalloc(mapzone, M_WAITOK); 48521c641b2SJohn Baldwin CTR1(KTR_VM, "vm_map_create: %p", result); 4868355f576SJeff Roberson _vm_map_init(result, min, max); 487df8bae1dSRodney W. Grimes result->pmap = pmap; 488df8bae1dSRodney W. Grimes return (result); 489df8bae1dSRodney W. Grimes } 490df8bae1dSRodney W. Grimes 491df8bae1dSRodney W. Grimes /* 492df8bae1dSRodney W. Grimes * Initialize an existing vm_map structure 493df8bae1dSRodney W. Grimes * such as that in the vmspace structure. 494df8bae1dSRodney W. Grimes * The pmap is set elsewhere. 495df8bae1dSRodney W. Grimes */ 4968355f576SJeff Roberson static void 4978355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 498df8bae1dSRodney W. Grimes { 4990cddd8f0SMatthew Dillon GIANT_REQUIRED; 50021c641b2SJohn Baldwin 501df8bae1dSRodney W. Grimes map->header.next = map->header.prev = &map->header; 5023075778bSJohn Dyson map->system_map = 0; 503df8bae1dSRodney W. Grimes map->min_offset = min; 504df8bae1dSRodney W. Grimes map->max_offset = max; 505df8bae1dSRodney W. Grimes map->first_free = &map->header; 506df8bae1dSRodney W. Grimes map->hint = &map->header; 507df8bae1dSRodney W. Grimes map->timestamp = 0; 508df8bae1dSRodney W. Grimes } 509df8bae1dSRodney W. Grimes 510a18b1f1dSJason Evans void 5118355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max) 512a18b1f1dSJason Evans { 5138355f576SJeff Roberson _vm_map_init(map, min, max); 5148355f576SJeff Roberson lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE); 515a18b1f1dSJason Evans } 516a18b1f1dSJason Evans 517df8bae1dSRodney W. Grimes /* 518b18bfc3dSJohn Dyson * vm_map_entry_dispose: [ internal use only ] 519b18bfc3dSJohn Dyson * 520b18bfc3dSJohn Dyson * Inverse of vm_map_entry_create. 521b18bfc3dSJohn Dyson */ 52262487bb4SJohn Dyson static void 5231b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry) 524b18bfc3dSJohn Dyson { 5258355f576SJeff Roberson uma_zfree((map->system_map || !mapentzone) 5268355f576SJeff Roberson ? kmapentzone : mapentzone, entry); 527b18bfc3dSJohn Dyson } 528b18bfc3dSJohn Dyson 529b18bfc3dSJohn Dyson /* 530df8bae1dSRodney W. Grimes * vm_map_entry_create: [ internal use only ] 531df8bae1dSRodney W. Grimes * 532df8bae1dSRodney W. Grimes * Allocates a VM map entry for insertion. 533b28cb1caSAlfred Perlstein * No entry fields are filled in. 534df8bae1dSRodney W. Grimes */ 535f708ef1bSPoul-Henning Kamp static vm_map_entry_t 5361b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map) 537df8bae1dSRodney W. Grimes { 5381f6889a1SMatthew Dillon vm_map_entry_t new_entry; 5391f6889a1SMatthew Dillon 5408355f576SJeff Roberson new_entry = uma_zalloc((map->system_map || !mapentzone) ? 5418355f576SJeff Roberson kmapentzone : mapentzone, M_WAITOK); 5421f6889a1SMatthew Dillon if (new_entry == NULL) 5431f6889a1SMatthew Dillon panic("vm_map_entry_create: kernel resources exhausted"); 5441f6889a1SMatthew Dillon return (new_entry); 545df8bae1dSRodney W. Grimes } 546df8bae1dSRodney W. Grimes 547df8bae1dSRodney W. Grimes /* 548df8bae1dSRodney W. Grimes * vm_map_entry_{un,}link: 549df8bae1dSRodney W. Grimes * 550df8bae1dSRodney W. Grimes * Insert/remove entries from maps. 551df8bae1dSRodney W. Grimes */ 55299c81ca9SAlan Cox static __inline void 55399c81ca9SAlan Cox vm_map_entry_link(vm_map_t map, 55499c81ca9SAlan Cox vm_map_entry_t after_where, 55599c81ca9SAlan Cox vm_map_entry_t entry) 55699c81ca9SAlan Cox { 55721c641b2SJohn Baldwin 55821c641b2SJohn Baldwin CTR4(KTR_VM, 55921c641b2SJohn Baldwin "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map, 56021c641b2SJohn Baldwin map->nentries, entry, after_where); 56199c81ca9SAlan Cox map->nentries++; 56299c81ca9SAlan Cox entry->prev = after_where; 56399c81ca9SAlan Cox entry->next = after_where->next; 56499c81ca9SAlan Cox entry->next->prev = entry; 56599c81ca9SAlan Cox after_where->next = entry; 566df8bae1dSRodney W. Grimes } 56799c81ca9SAlan Cox 56899c81ca9SAlan Cox static __inline void 56999c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map, 57099c81ca9SAlan Cox vm_map_entry_t entry) 57199c81ca9SAlan Cox { 57299c81ca9SAlan Cox vm_map_entry_t prev = entry->prev; 57399c81ca9SAlan Cox vm_map_entry_t next = entry->next; 57499c81ca9SAlan Cox 57599c81ca9SAlan Cox next->prev = prev; 57699c81ca9SAlan Cox prev->next = next; 57799c81ca9SAlan Cox map->nentries--; 57821c641b2SJohn Baldwin CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map, 57921c641b2SJohn Baldwin map->nentries, entry); 580df8bae1dSRodney W. Grimes } 581df8bae1dSRodney W. Grimes 582df8bae1dSRodney W. Grimes /* 583df8bae1dSRodney W. Grimes * SAVE_HINT: 584df8bae1dSRodney W. Grimes * 585df8bae1dSRodney W. Grimes * Saves the specified entry as the hint for 58624a1cce3SDavid Greenman * future lookups. 587df8bae1dSRodney W. Grimes */ 588df8bae1dSRodney W. Grimes #define SAVE_HINT(map,value) \ 58924a1cce3SDavid Greenman (map)->hint = (value); 590df8bae1dSRodney W. Grimes 591df8bae1dSRodney W. Grimes /* 592df8bae1dSRodney W. Grimes * vm_map_lookup_entry: [ internal use only ] 593df8bae1dSRodney W. Grimes * 594df8bae1dSRodney W. Grimes * Finds the map entry containing (or 595df8bae1dSRodney W. Grimes * immediately preceding) the specified address 596df8bae1dSRodney W. Grimes * in the given map; the entry is returned 597df8bae1dSRodney W. Grimes * in the "entry" parameter. The boolean 598df8bae1dSRodney W. Grimes * result indicates whether the address is 599df8bae1dSRodney W. Grimes * actually contained in the map. 600df8bae1dSRodney W. Grimes */ 6010d94caffSDavid Greenman boolean_t 6021b40f8c0SMatthew Dillon vm_map_lookup_entry( 6031b40f8c0SMatthew Dillon vm_map_t map, 6041b40f8c0SMatthew Dillon vm_offset_t address, 6051b40f8c0SMatthew Dillon vm_map_entry_t *entry) /* OUT */ 606df8bae1dSRodney W. Grimes { 607c0877f10SJohn Dyson vm_map_entry_t cur; 608c0877f10SJohn Dyson vm_map_entry_t last; 609df8bae1dSRodney W. Grimes 6100cddd8f0SMatthew Dillon GIANT_REQUIRED; 611df8bae1dSRodney W. Grimes /* 6120d94caffSDavid Greenman * Start looking either from the head of the list, or from the hint. 613df8bae1dSRodney W. Grimes */ 614df8bae1dSRodney W. Grimes cur = map->hint; 615df8bae1dSRodney W. Grimes 616df8bae1dSRodney W. Grimes if (cur == &map->header) 617df8bae1dSRodney W. Grimes cur = cur->next; 618df8bae1dSRodney W. Grimes 619df8bae1dSRodney W. Grimes if (address >= cur->start) { 620df8bae1dSRodney W. Grimes /* 621df8bae1dSRodney W. Grimes * Go from hint to end of list. 622df8bae1dSRodney W. Grimes * 6230d94caffSDavid Greenman * But first, make a quick check to see if we are already looking 6240d94caffSDavid Greenman * at the entry we want (which is usually the case). Note also 6250d94caffSDavid Greenman * that we don't need to save the hint here... it is the same 6260d94caffSDavid Greenman * hint (unless we are at the header, in which case the hint 6270d94caffSDavid Greenman * didn't buy us anything anyway). 628df8bae1dSRodney W. Grimes */ 629df8bae1dSRodney W. Grimes last = &map->header; 630df8bae1dSRodney W. Grimes if ((cur != last) && (cur->end > address)) { 631df8bae1dSRodney W. Grimes *entry = cur; 632df8bae1dSRodney W. Grimes return (TRUE); 633df8bae1dSRodney W. Grimes } 6340d94caffSDavid Greenman } else { 635df8bae1dSRodney W. Grimes /* 636df8bae1dSRodney W. Grimes * Go from start to hint, *inclusively* 637df8bae1dSRodney W. Grimes */ 638df8bae1dSRodney W. Grimes last = cur->next; 639df8bae1dSRodney W. Grimes cur = map->header.next; 640df8bae1dSRodney W. Grimes } 641df8bae1dSRodney W. Grimes 642df8bae1dSRodney W. Grimes /* 643df8bae1dSRodney W. Grimes * Search linearly 644df8bae1dSRodney W. Grimes */ 645df8bae1dSRodney W. Grimes while (cur != last) { 646df8bae1dSRodney W. Grimes if (cur->end > address) { 647df8bae1dSRodney W. Grimes if (address >= cur->start) { 648df8bae1dSRodney W. Grimes /* 6490d94caffSDavid Greenman * Save this lookup for future hints, and 6500d94caffSDavid Greenman * return 651df8bae1dSRodney W. Grimes */ 652df8bae1dSRodney W. Grimes *entry = cur; 653df8bae1dSRodney W. Grimes SAVE_HINT(map, cur); 654df8bae1dSRodney W. Grimes return (TRUE); 655df8bae1dSRodney W. Grimes } 656df8bae1dSRodney W. Grimes break; 657df8bae1dSRodney W. Grimes } 658df8bae1dSRodney W. Grimes cur = cur->next; 659df8bae1dSRodney W. Grimes } 660df8bae1dSRodney W. Grimes *entry = cur->prev; 661df8bae1dSRodney W. Grimes SAVE_HINT(map, *entry); 662df8bae1dSRodney W. Grimes return (FALSE); 663df8bae1dSRodney W. Grimes } 664df8bae1dSRodney W. Grimes 665df8bae1dSRodney W. Grimes /* 66630dcfc09SJohn Dyson * vm_map_insert: 66730dcfc09SJohn Dyson * 66830dcfc09SJohn Dyson * Inserts the given whole VM object into the target 66930dcfc09SJohn Dyson * map at the specified address range. The object's 67030dcfc09SJohn Dyson * size should match that of the address range. 67130dcfc09SJohn Dyson * 67230dcfc09SJohn Dyson * Requires that the map be locked, and leaves it so. 6732aaeadf8SMatthew Dillon * 6742aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 6752aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 67630dcfc09SJohn Dyson */ 67730dcfc09SJohn Dyson int 678b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 679b9dcd593SBruce Evans vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, 680b9dcd593SBruce Evans int cow) 68130dcfc09SJohn Dyson { 682c0877f10SJohn Dyson vm_map_entry_t new_entry; 683c0877f10SJohn Dyson vm_map_entry_t prev_entry; 68430dcfc09SJohn Dyson vm_map_entry_t temp_entry; 6859730a5daSPaul Saab vm_eflags_t protoeflags; 68630dcfc09SJohn Dyson 6870cddd8f0SMatthew Dillon GIANT_REQUIRED; 6880cddd8f0SMatthew Dillon 68930dcfc09SJohn Dyson /* 69030dcfc09SJohn Dyson * Check that the start and end points are not bogus. 69130dcfc09SJohn Dyson */ 69230dcfc09SJohn Dyson if ((start < map->min_offset) || (end > map->max_offset) || 69330dcfc09SJohn Dyson (start >= end)) 69430dcfc09SJohn Dyson return (KERN_INVALID_ADDRESS); 69530dcfc09SJohn Dyson 69630dcfc09SJohn Dyson /* 69730dcfc09SJohn Dyson * Find the entry prior to the proposed starting address; if it's part 69830dcfc09SJohn Dyson * of an existing entry, this range is bogus. 69930dcfc09SJohn Dyson */ 70030dcfc09SJohn Dyson if (vm_map_lookup_entry(map, start, &temp_entry)) 70130dcfc09SJohn Dyson return (KERN_NO_SPACE); 70230dcfc09SJohn Dyson 70330dcfc09SJohn Dyson prev_entry = temp_entry; 70430dcfc09SJohn Dyson 70530dcfc09SJohn Dyson /* 70630dcfc09SJohn Dyson * Assert that the next entry doesn't overlap the end point. 70730dcfc09SJohn Dyson */ 70830dcfc09SJohn Dyson if ((prev_entry->next != &map->header) && 70930dcfc09SJohn Dyson (prev_entry->next->start < end)) 71030dcfc09SJohn Dyson return (KERN_NO_SPACE); 71130dcfc09SJohn Dyson 712afa07f7eSJohn Dyson protoeflags = 0; 713afa07f7eSJohn Dyson 714afa07f7eSJohn Dyson if (cow & MAP_COPY_ON_WRITE) 715e5f13bddSAlan Cox protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY; 716afa07f7eSJohn Dyson 7174e045f93SAlan Cox if (cow & MAP_NOFAULT) { 718afa07f7eSJohn Dyson protoeflags |= MAP_ENTRY_NOFAULT; 719afa07f7eSJohn Dyson 7204e045f93SAlan Cox KASSERT(object == NULL, 7214e045f93SAlan Cox ("vm_map_insert: paradoxical MAP_NOFAULT request")); 7224e045f93SAlan Cox } 7234f79d873SMatthew Dillon if (cow & MAP_DISABLE_SYNCER) 7244f79d873SMatthew Dillon protoeflags |= MAP_ENTRY_NOSYNC; 7259730a5daSPaul Saab if (cow & MAP_DISABLE_COREDUMP) 7269730a5daSPaul Saab protoeflags |= MAP_ENTRY_NOCOREDUMP; 7274f79d873SMatthew Dillon 7282aaeadf8SMatthew Dillon if (object) { 72930dcfc09SJohn Dyson /* 7302aaeadf8SMatthew Dillon * When object is non-NULL, it could be shared with another 7312aaeadf8SMatthew Dillon * process. We have to set or clear OBJ_ONEMAPPING 7322aaeadf8SMatthew Dillon * appropriately. 73330dcfc09SJohn Dyson */ 7342aaeadf8SMatthew Dillon if ((object->ref_count > 1) || (object->shadow_count != 0)) { 7352aaeadf8SMatthew Dillon vm_object_clear_flag(object, OBJ_ONEMAPPING); 7362aaeadf8SMatthew Dillon } 7374e045f93SAlan Cox } 7384e045f93SAlan Cox else if ((prev_entry != &map->header) && 7394e045f93SAlan Cox (prev_entry->eflags == protoeflags) && 7408cc7e047SJohn Dyson (prev_entry->end == start) && 7414e045f93SAlan Cox (prev_entry->wired_count == 0) && 7424e045f93SAlan Cox ((prev_entry->object.vm_object == NULL) || 7438cc7e047SJohn Dyson vm_object_coalesce(prev_entry->object.vm_object, 74430dcfc09SJohn Dyson OFF_TO_IDX(prev_entry->offset), 7458cc7e047SJohn Dyson (vm_size_t)(prev_entry->end - prev_entry->start), 746cdc2c291SJohn Dyson (vm_size_t)(end - prev_entry->end)))) { 74730dcfc09SJohn Dyson /* 7482aaeadf8SMatthew Dillon * We were able to extend the object. Determine if we 7492aaeadf8SMatthew Dillon * can extend the previous map entry to include the 7502aaeadf8SMatthew Dillon * new range as well. 75130dcfc09SJohn Dyson */ 7528cc7e047SJohn Dyson if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) && 7538cc7e047SJohn Dyson (prev_entry->protection == prot) && 7548cc7e047SJohn Dyson (prev_entry->max_protection == max)) { 75530dcfc09SJohn Dyson map->size += (end - prev_entry->end); 75630dcfc09SJohn Dyson prev_entry->end = end; 7574e71e795SMatthew Dillon vm_map_simplify_entry(map, prev_entry); 75830dcfc09SJohn Dyson return (KERN_SUCCESS); 75930dcfc09SJohn Dyson } 7608cc7e047SJohn Dyson 7612aaeadf8SMatthew Dillon /* 7622aaeadf8SMatthew Dillon * If we can extend the object but cannot extend the 7632aaeadf8SMatthew Dillon * map entry, we have to create a new map entry. We 7642aaeadf8SMatthew Dillon * must bump the ref count on the extended object to 7654e71e795SMatthew Dillon * account for it. object may be NULL. 7662aaeadf8SMatthew Dillon */ 7672aaeadf8SMatthew Dillon object = prev_entry->object.vm_object; 7682aaeadf8SMatthew Dillon offset = prev_entry->offset + 7692aaeadf8SMatthew Dillon (prev_entry->end - prev_entry->start); 7708cc7e047SJohn Dyson vm_object_reference(object); 771b18bfc3dSJohn Dyson } 7722aaeadf8SMatthew Dillon 7732aaeadf8SMatthew Dillon /* 7742aaeadf8SMatthew Dillon * NOTE: if conditionals fail, object can be NULL here. This occurs 7752aaeadf8SMatthew Dillon * in things like the buffer map where we manage kva but do not manage 7762aaeadf8SMatthew Dillon * backing objects. 7772aaeadf8SMatthew Dillon */ 7788cc7e047SJohn Dyson 77930dcfc09SJohn Dyson /* 78030dcfc09SJohn Dyson * Create a new entry 78130dcfc09SJohn Dyson */ 78230dcfc09SJohn Dyson new_entry = vm_map_entry_create(map); 78330dcfc09SJohn Dyson new_entry->start = start; 78430dcfc09SJohn Dyson new_entry->end = end; 78530dcfc09SJohn Dyson 786afa07f7eSJohn Dyson new_entry->eflags = protoeflags; 78730dcfc09SJohn Dyson new_entry->object.vm_object = object; 78830dcfc09SJohn Dyson new_entry->offset = offset; 7892267af78SJulian Elischer new_entry->avail_ssize = 0; 7902267af78SJulian Elischer 79130dcfc09SJohn Dyson new_entry->inheritance = VM_INHERIT_DEFAULT; 79230dcfc09SJohn Dyson new_entry->protection = prot; 79330dcfc09SJohn Dyson new_entry->max_protection = max; 79430dcfc09SJohn Dyson new_entry->wired_count = 0; 795e5f251d2SAlan Cox 79630dcfc09SJohn Dyson /* 79730dcfc09SJohn Dyson * Insert the new entry into the list 79830dcfc09SJohn Dyson */ 79930dcfc09SJohn Dyson vm_map_entry_link(map, prev_entry, new_entry); 80030dcfc09SJohn Dyson map->size += new_entry->end - new_entry->start; 80130dcfc09SJohn Dyson 80230dcfc09SJohn Dyson /* 80330dcfc09SJohn Dyson * Update the free space hint 80430dcfc09SJohn Dyson */ 80567bf6868SJohn Dyson if ((map->first_free == prev_entry) && 8064f79d873SMatthew Dillon (prev_entry->end >= new_entry->start)) { 80730dcfc09SJohn Dyson map->first_free = new_entry; 8084f79d873SMatthew Dillon } 80930dcfc09SJohn Dyson 8101a484d28SMatthew Dillon #if 0 8111a484d28SMatthew Dillon /* 8121a484d28SMatthew Dillon * Temporarily removed to avoid MAP_STACK panic, due to 8131a484d28SMatthew Dillon * MAP_STACK being a huge hack. Will be added back in 8141a484d28SMatthew Dillon * when MAP_STACK (and the user stack mapping) is fixed. 8151a484d28SMatthew Dillon */ 8164e71e795SMatthew Dillon /* 8174e71e795SMatthew Dillon * It may be possible to simplify the entry 8184e71e795SMatthew Dillon */ 8194e71e795SMatthew Dillon vm_map_simplify_entry(map, new_entry); 8201a484d28SMatthew Dillon #endif 8214e71e795SMatthew Dillon 8224f79d873SMatthew Dillon if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) { 823e972780aSAlan Cox pmap_object_init_pt(map->pmap, start, 824e972780aSAlan Cox object, OFF_TO_IDX(offset), end - start, 825e972780aSAlan Cox cow & MAP_PREFAULT_PARTIAL); 8264f79d873SMatthew Dillon } 827e972780aSAlan Cox 82830dcfc09SJohn Dyson return (KERN_SUCCESS); 82930dcfc09SJohn Dyson } 83030dcfc09SJohn Dyson 83130dcfc09SJohn Dyson /* 832df8bae1dSRodney W. Grimes * Find sufficient space for `length' bytes in the given map, starting at 833df8bae1dSRodney W. Grimes * `start'. The map must be locked. Returns 0 on success, 1 on no space. 834df8bae1dSRodney W. Grimes */ 835df8bae1dSRodney W. Grimes int 8361b40f8c0SMatthew Dillon vm_map_findspace( 8371b40f8c0SMatthew Dillon vm_map_t map, 8381b40f8c0SMatthew Dillon vm_offset_t start, 8391b40f8c0SMatthew Dillon vm_size_t length, 8401b40f8c0SMatthew Dillon vm_offset_t *addr) 841df8bae1dSRodney W. Grimes { 842c0877f10SJohn Dyson vm_map_entry_t entry, next; 843c0877f10SJohn Dyson vm_offset_t end; 844df8bae1dSRodney W. Grimes 8450cddd8f0SMatthew Dillon GIANT_REQUIRED; 846df8bae1dSRodney W. Grimes if (start < map->min_offset) 847df8bae1dSRodney W. Grimes start = map->min_offset; 848df8bae1dSRodney W. Grimes if (start > map->max_offset) 849df8bae1dSRodney W. Grimes return (1); 850df8bae1dSRodney W. Grimes 851df8bae1dSRodney W. Grimes /* 8520d94caffSDavid Greenman * Look for the first possible address; if there's already something 8530d94caffSDavid Greenman * at this address, we have to start after it. 854df8bae1dSRodney W. Grimes */ 855df8bae1dSRodney W. Grimes if (start == map->min_offset) { 85667bf6868SJohn Dyson if ((entry = map->first_free) != &map->header) 857df8bae1dSRodney W. Grimes start = entry->end; 858df8bae1dSRodney W. Grimes } else { 859df8bae1dSRodney W. Grimes vm_map_entry_t tmp; 8600d94caffSDavid Greenman 861df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &tmp)) 862df8bae1dSRodney W. Grimes start = tmp->end; 863df8bae1dSRodney W. Grimes entry = tmp; 864df8bae1dSRodney W. Grimes } 865df8bae1dSRodney W. Grimes 866df8bae1dSRodney W. Grimes /* 8670d94caffSDavid Greenman * Look through the rest of the map, trying to fit a new region in the 8680d94caffSDavid Greenman * gap between existing regions, or after the very last region. 869df8bae1dSRodney W. Grimes */ 870df8bae1dSRodney W. Grimes for (;; start = (entry = next)->end) { 871df8bae1dSRodney W. Grimes /* 872df8bae1dSRodney W. Grimes * Find the end of the proposed new region. Be sure we didn't 873df8bae1dSRodney W. Grimes * go beyond the end of the map, or wrap around the address; 874df8bae1dSRodney W. Grimes * if so, we lose. Otherwise, if this is the last entry, or 875df8bae1dSRodney W. Grimes * if the proposed new region fits before the next entry, we 876df8bae1dSRodney W. Grimes * win. 877df8bae1dSRodney W. Grimes */ 878df8bae1dSRodney W. Grimes end = start + length; 879df8bae1dSRodney W. Grimes if (end > map->max_offset || end < start) 880df8bae1dSRodney W. Grimes return (1); 881df8bae1dSRodney W. Grimes next = entry->next; 882df8bae1dSRodney W. Grimes if (next == &map->header || next->start >= end) 883df8bae1dSRodney W. Grimes break; 884df8bae1dSRodney W. Grimes } 885df8bae1dSRodney W. Grimes SAVE_HINT(map, entry); 886df8bae1dSRodney W. Grimes *addr = start; 88799448ed1SJohn Dyson if (map == kernel_map) { 88899448ed1SJohn Dyson vm_offset_t ksize; 88999448ed1SJohn Dyson if ((ksize = round_page(start + length)) > kernel_vm_end) { 89099448ed1SJohn Dyson pmap_growkernel(ksize); 89199448ed1SJohn Dyson } 89299448ed1SJohn Dyson } 893df8bae1dSRodney W. Grimes return (0); 894df8bae1dSRodney W. Grimes } 895df8bae1dSRodney W. Grimes 896df8bae1dSRodney W. Grimes /* 897df8bae1dSRodney W. Grimes * vm_map_find finds an unallocated region in the target address 898df8bae1dSRodney W. Grimes * map with the given length. The search is defined to be 899df8bae1dSRodney W. Grimes * first-fit from the specified address; the region found is 900df8bae1dSRodney W. Grimes * returned in the same parameter. 901df8bae1dSRodney W. Grimes * 9022aaeadf8SMatthew Dillon * If object is non-NULL, ref count must be bumped by caller 9032aaeadf8SMatthew Dillon * prior to making call to account for the new entry. 904df8bae1dSRodney W. Grimes */ 905df8bae1dSRodney W. Grimes int 906b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset, 907b9dcd593SBruce Evans vm_offset_t *addr, /* IN/OUT */ 908b9dcd593SBruce Evans vm_size_t length, boolean_t find_space, vm_prot_t prot, 909b9dcd593SBruce Evans vm_prot_t max, int cow) 910df8bae1dSRodney W. Grimes { 911c0877f10SJohn Dyson vm_offset_t start; 9128d6e8edeSDavid Greenman int result, s = 0; 913df8bae1dSRodney W. Grimes 9140cddd8f0SMatthew Dillon GIANT_REQUIRED; 9150cddd8f0SMatthew Dillon 916df8bae1dSRodney W. Grimes start = *addr; 9178d6e8edeSDavid Greenman 91808442f8aSBosko Milekic if (map == kmem_map) 919b18bfc3dSJohn Dyson s = splvm(); 9208d6e8edeSDavid Greenman 921bea41bcfSDavid Greenman vm_map_lock(map); 922df8bae1dSRodney W. Grimes if (find_space) { 923df8bae1dSRodney W. Grimes if (vm_map_findspace(map, start, length, addr)) { 924df8bae1dSRodney W. Grimes vm_map_unlock(map); 92508442f8aSBosko Milekic if (map == kmem_map) 9268d6e8edeSDavid Greenman splx(s); 927df8bae1dSRodney W. Grimes return (KERN_NO_SPACE); 928df8bae1dSRodney W. Grimes } 929df8bae1dSRodney W. Grimes start = *addr; 930df8bae1dSRodney W. Grimes } 931bd7e5f99SJohn Dyson result = vm_map_insert(map, object, offset, 932bd7e5f99SJohn Dyson start, start + length, prot, max, cow); 933df8bae1dSRodney W. Grimes vm_map_unlock(map); 9348d6e8edeSDavid Greenman 93508442f8aSBosko Milekic if (map == kmem_map) 9368d6e8edeSDavid Greenman splx(s); 9378d6e8edeSDavid Greenman 938df8bae1dSRodney W. Grimes return (result); 939df8bae1dSRodney W. Grimes } 940df8bae1dSRodney W. Grimes 941df8bae1dSRodney W. Grimes /* 942b7b2aac2SJohn Dyson * vm_map_simplify_entry: 94367bf6868SJohn Dyson * 9444e71e795SMatthew Dillon * Simplify the given map entry by merging with either neighbor. This 9454e71e795SMatthew Dillon * routine also has the ability to merge with both neighbors. 9464e71e795SMatthew Dillon * 9474e71e795SMatthew Dillon * The map must be locked. 9484e71e795SMatthew Dillon * 9494e71e795SMatthew Dillon * This routine guarentees that the passed entry remains valid (though 9504e71e795SMatthew Dillon * possibly extended). When merging, this routine may delete one or 9514e71e795SMatthew Dillon * both neighbors. 952df8bae1dSRodney W. Grimes */ 953b7b2aac2SJohn Dyson void 9541b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry) 955df8bae1dSRodney W. Grimes { 956308c24baSJohn Dyson vm_map_entry_t next, prev; 957b7b2aac2SJohn Dyson vm_size_t prevsize, esize; 958df8bae1dSRodney W. Grimes 9590cddd8f0SMatthew Dillon GIANT_REQUIRED; 9600cddd8f0SMatthew Dillon 9619fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) 962df8bae1dSRodney W. Grimes return; 963308c24baSJohn Dyson 964308c24baSJohn Dyson prev = entry->prev; 965308c24baSJohn Dyson if (prev != &map->header) { 96667bf6868SJohn Dyson prevsize = prev->end - prev->start; 96767bf6868SJohn Dyson if ( (prev->end == entry->start) && 96867bf6868SJohn Dyson (prev->object.vm_object == entry->object.vm_object) && 96995e5e988SJohn Dyson (!prev->object.vm_object || 97067bf6868SJohn Dyson (prev->offset + prevsize == entry->offset)) && 971afa07f7eSJohn Dyson (prev->eflags == entry->eflags) && 97267bf6868SJohn Dyson (prev->protection == entry->protection) && 97367bf6868SJohn Dyson (prev->max_protection == entry->max_protection) && 97467bf6868SJohn Dyson (prev->inheritance == entry->inheritance) && 975b7b2aac2SJohn Dyson (prev->wired_count == entry->wired_count)) { 976308c24baSJohn Dyson if (map->first_free == prev) 977308c24baSJohn Dyson map->first_free = entry; 978b18bfc3dSJohn Dyson if (map->hint == prev) 979b18bfc3dSJohn Dyson map->hint = entry; 980308c24baSJohn Dyson vm_map_entry_unlink(map, prev); 981308c24baSJohn Dyson entry->start = prev->start; 982308c24baSJohn Dyson entry->offset = prev->offset; 983b18bfc3dSJohn Dyson if (prev->object.vm_object) 984308c24baSJohn Dyson vm_object_deallocate(prev->object.vm_object); 985308c24baSJohn Dyson vm_map_entry_dispose(map, prev); 986308c24baSJohn Dyson } 987308c24baSJohn Dyson } 988de5f6a77SJohn Dyson 989de5f6a77SJohn Dyson next = entry->next; 990308c24baSJohn Dyson if (next != &map->header) { 99167bf6868SJohn Dyson esize = entry->end - entry->start; 99267bf6868SJohn Dyson if ((entry->end == next->start) && 99367bf6868SJohn Dyson (next->object.vm_object == entry->object.vm_object) && 99467bf6868SJohn Dyson (!entry->object.vm_object || 99567bf6868SJohn Dyson (entry->offset + esize == next->offset)) && 996afa07f7eSJohn Dyson (next->eflags == entry->eflags) && 99767bf6868SJohn Dyson (next->protection == entry->protection) && 99867bf6868SJohn Dyson (next->max_protection == entry->max_protection) && 99967bf6868SJohn Dyson (next->inheritance == entry->inheritance) && 1000b7b2aac2SJohn Dyson (next->wired_count == entry->wired_count)) { 1001308c24baSJohn Dyson if (map->first_free == next) 1002308c24baSJohn Dyson map->first_free = entry; 1003b18bfc3dSJohn Dyson if (map->hint == next) 1004b18bfc3dSJohn Dyson map->hint = entry; 1005de5f6a77SJohn Dyson vm_map_entry_unlink(map, next); 1006de5f6a77SJohn Dyson entry->end = next->end; 1007b18bfc3dSJohn Dyson if (next->object.vm_object) 1008de5f6a77SJohn Dyson vm_object_deallocate(next->object.vm_object); 1009de5f6a77SJohn Dyson vm_map_entry_dispose(map, next); 1010df8bae1dSRodney W. Grimes } 1011df8bae1dSRodney W. Grimes } 1012de5f6a77SJohn Dyson } 1013df8bae1dSRodney W. Grimes /* 1014df8bae1dSRodney W. Grimes * vm_map_clip_start: [ internal use only ] 1015df8bae1dSRodney W. Grimes * 1016df8bae1dSRodney W. Grimes * Asserts that the given entry begins at or after 1017df8bae1dSRodney W. Grimes * the specified address; if necessary, 1018df8bae1dSRodney W. Grimes * it splits the entry into two. 1019df8bae1dSRodney W. Grimes */ 1020df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \ 1021df8bae1dSRodney W. Grimes { \ 1022df8bae1dSRodney W. Grimes if (startaddr > entry->start) \ 1023df8bae1dSRodney W. Grimes _vm_map_clip_start(map, entry, startaddr); \ 1024df8bae1dSRodney W. Grimes } 1025df8bae1dSRodney W. Grimes 1026df8bae1dSRodney W. Grimes /* 1027df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1028df8bae1dSRodney W. Grimes * the entry must be split. 1029df8bae1dSRodney W. Grimes */ 10300d94caffSDavid Greenman static void 10311b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start) 1032df8bae1dSRodney W. Grimes { 1033c0877f10SJohn Dyson vm_map_entry_t new_entry; 1034df8bae1dSRodney W. Grimes 1035df8bae1dSRodney W. Grimes /* 10360d94caffSDavid Greenman * Split off the front portion -- note that we must insert the new 10370d94caffSDavid Greenman * entry BEFORE this one, so that this entry has the specified 10380d94caffSDavid Greenman * starting address. 1039df8bae1dSRodney W. Grimes */ 1040f32dbbeeSJohn Dyson vm_map_simplify_entry(map, entry); 1041f32dbbeeSJohn Dyson 104211cccda1SJohn Dyson /* 104311cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 104411cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 104511cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 104611cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 104711cccda1SJohn Dyson * put this improvement. 104811cccda1SJohn Dyson */ 10494e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 105011cccda1SJohn Dyson vm_object_t object; 105111cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1052c2e11a03SJohn Dyson atop(entry->end - entry->start)); 105311cccda1SJohn Dyson entry->object.vm_object = object; 105411cccda1SJohn Dyson entry->offset = 0; 105511cccda1SJohn Dyson } 105611cccda1SJohn Dyson 1057df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1058df8bae1dSRodney W. Grimes *new_entry = *entry; 1059df8bae1dSRodney W. Grimes 1060df8bae1dSRodney W. Grimes new_entry->end = start; 1061df8bae1dSRodney W. Grimes entry->offset += (start - entry->start); 1062df8bae1dSRodney W. Grimes entry->start = start; 1063df8bae1dSRodney W. Grimes 1064df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry->prev, new_entry); 1065df8bae1dSRodney W. Grimes 10669fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1067df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1068df8bae1dSRodney W. Grimes } 1069c0877f10SJohn Dyson } 1070df8bae1dSRodney W. Grimes 1071df8bae1dSRodney W. Grimes /* 1072df8bae1dSRodney W. Grimes * vm_map_clip_end: [ internal use only ] 1073df8bae1dSRodney W. Grimes * 1074df8bae1dSRodney W. Grimes * Asserts that the given entry ends at or before 1075df8bae1dSRodney W. Grimes * the specified address; if necessary, 1076df8bae1dSRodney W. Grimes * it splits the entry into two. 1077df8bae1dSRodney W. Grimes */ 1078df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \ 1079df8bae1dSRodney W. Grimes { \ 1080df8bae1dSRodney W. Grimes if (endaddr < entry->end) \ 1081df8bae1dSRodney W. Grimes _vm_map_clip_end(map, entry, endaddr); \ 1082df8bae1dSRodney W. Grimes } 1083df8bae1dSRodney W. Grimes 1084df8bae1dSRodney W. Grimes /* 1085df8bae1dSRodney W. Grimes * This routine is called only when it is known that 1086df8bae1dSRodney W. Grimes * the entry must be split. 1087df8bae1dSRodney W. Grimes */ 10880d94caffSDavid Greenman static void 10891b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end) 1090df8bae1dSRodney W. Grimes { 1091c0877f10SJohn Dyson vm_map_entry_t new_entry; 1092df8bae1dSRodney W. Grimes 1093df8bae1dSRodney W. Grimes /* 109411cccda1SJohn Dyson * If there is no object backing this entry, we might as well create 109511cccda1SJohn Dyson * one now. If we defer it, an object can get created after the map 109611cccda1SJohn Dyson * is clipped, and individual objects will be created for the split-up 109711cccda1SJohn Dyson * map. This is a bit of a hack, but is also about the best place to 109811cccda1SJohn Dyson * put this improvement. 109911cccda1SJohn Dyson */ 11004e71e795SMatthew Dillon if (entry->object.vm_object == NULL && !map->system_map) { 110111cccda1SJohn Dyson vm_object_t object; 110211cccda1SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 1103c2e11a03SJohn Dyson atop(entry->end - entry->start)); 110411cccda1SJohn Dyson entry->object.vm_object = object; 110511cccda1SJohn Dyson entry->offset = 0; 110611cccda1SJohn Dyson } 110711cccda1SJohn Dyson 110811cccda1SJohn Dyson /* 11090d94caffSDavid Greenman * Create a new entry and insert it AFTER the specified entry 1110df8bae1dSRodney W. Grimes */ 1111df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(map); 1112df8bae1dSRodney W. Grimes *new_entry = *entry; 1113df8bae1dSRodney W. Grimes 1114df8bae1dSRodney W. Grimes new_entry->start = entry->end = end; 1115df8bae1dSRodney W. Grimes new_entry->offset += (end - entry->start); 1116df8bae1dSRodney W. Grimes 1117df8bae1dSRodney W. Grimes vm_map_entry_link(map, entry, new_entry); 1118df8bae1dSRodney W. Grimes 11199fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1120df8bae1dSRodney W. Grimes vm_object_reference(new_entry->object.vm_object); 1121df8bae1dSRodney W. Grimes } 1122c0877f10SJohn Dyson } 1123df8bae1dSRodney W. Grimes 1124df8bae1dSRodney W. Grimes /* 1125df8bae1dSRodney W. Grimes * VM_MAP_RANGE_CHECK: [ internal use only ] 1126df8bae1dSRodney W. Grimes * 1127df8bae1dSRodney W. Grimes * Asserts that the starting and ending region 1128df8bae1dSRodney W. Grimes * addresses fall within the valid range of the map. 1129df8bae1dSRodney W. Grimes */ 1130df8bae1dSRodney W. Grimes #define VM_MAP_RANGE_CHECK(map, start, end) \ 1131df8bae1dSRodney W. Grimes { \ 1132df8bae1dSRodney W. Grimes if (start < vm_map_min(map)) \ 1133df8bae1dSRodney W. Grimes start = vm_map_min(map); \ 1134df8bae1dSRodney W. Grimes if (end > vm_map_max(map)) \ 1135df8bae1dSRodney W. Grimes end = vm_map_max(map); \ 1136df8bae1dSRodney W. Grimes if (start > end) \ 1137df8bae1dSRodney W. Grimes start = end; \ 1138df8bae1dSRodney W. Grimes } 1139df8bae1dSRodney W. Grimes 1140df8bae1dSRodney W. Grimes /* 1141df8bae1dSRodney W. Grimes * vm_map_submap: [ kernel use only ] 1142df8bae1dSRodney W. Grimes * 1143df8bae1dSRodney W. Grimes * Mark the given range as handled by a subordinate map. 1144df8bae1dSRodney W. Grimes * 1145df8bae1dSRodney W. Grimes * This range must have been created with vm_map_find, 1146df8bae1dSRodney W. Grimes * and no other operations may have been performed on this 1147df8bae1dSRodney W. Grimes * range prior to calling vm_map_submap. 1148df8bae1dSRodney W. Grimes * 1149df8bae1dSRodney W. Grimes * Only a limited number of operations can be performed 1150df8bae1dSRodney W. Grimes * within this rage after calling vm_map_submap: 1151df8bae1dSRodney W. Grimes * vm_fault 1152df8bae1dSRodney W. Grimes * [Don't try vm_map_copy!] 1153df8bae1dSRodney W. Grimes * 1154df8bae1dSRodney W. Grimes * To remove a submapping, one must first remove the 1155df8bae1dSRodney W. Grimes * range from the superior map, and then destroy the 1156df8bae1dSRodney W. Grimes * submap (if desired). [Better yet, don't try it.] 1157df8bae1dSRodney W. Grimes */ 1158df8bae1dSRodney W. Grimes int 11591b40f8c0SMatthew Dillon vm_map_submap( 11601b40f8c0SMatthew Dillon vm_map_t map, 11611b40f8c0SMatthew Dillon vm_offset_t start, 11621b40f8c0SMatthew Dillon vm_offset_t end, 11631b40f8c0SMatthew Dillon vm_map_t submap) 1164df8bae1dSRodney W. Grimes { 1165df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1166c0877f10SJohn Dyson int result = KERN_INVALID_ARGUMENT; 1167df8bae1dSRodney W. Grimes 11680cddd8f0SMatthew Dillon GIANT_REQUIRED; 11690cddd8f0SMatthew Dillon 1170df8bae1dSRodney W. Grimes vm_map_lock(map); 1171df8bae1dSRodney W. Grimes 1172df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1173df8bae1dSRodney W. Grimes 1174df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1175df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 11760d94caffSDavid Greenman } else 1177df8bae1dSRodney W. Grimes entry = entry->next; 1178df8bae1dSRodney W. Grimes 1179df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1180df8bae1dSRodney W. Grimes 1181df8bae1dSRodney W. Grimes if ((entry->start == start) && (entry->end == end) && 11829fdfe602SMatthew Dillon ((entry->eflags & MAP_ENTRY_COW) == 0) && 1183afa07f7eSJohn Dyson (entry->object.vm_object == NULL)) { 11842d8acc0fSJohn Dyson entry->object.sub_map = submap; 1185afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_IS_SUB_MAP; 1186df8bae1dSRodney W. Grimes result = KERN_SUCCESS; 1187df8bae1dSRodney W. Grimes } 1188df8bae1dSRodney W. Grimes vm_map_unlock(map); 1189df8bae1dSRodney W. Grimes 1190df8bae1dSRodney W. Grimes return (result); 1191df8bae1dSRodney W. Grimes } 1192df8bae1dSRodney W. Grimes 1193df8bae1dSRodney W. Grimes /* 1194df8bae1dSRodney W. Grimes * vm_map_protect: 1195df8bae1dSRodney W. Grimes * 1196df8bae1dSRodney W. Grimes * Sets the protection of the specified address 1197df8bae1dSRodney W. Grimes * region in the target map. If "set_max" is 1198df8bae1dSRodney W. Grimes * specified, the maximum protection is to be set; 1199df8bae1dSRodney W. Grimes * otherwise, only the current protection is affected. 1200df8bae1dSRodney W. Grimes */ 1201df8bae1dSRodney W. Grimes int 1202b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end, 1203b9dcd593SBruce Evans vm_prot_t new_prot, boolean_t set_max) 1204df8bae1dSRodney W. Grimes { 1205c0877f10SJohn Dyson vm_map_entry_t current; 1206df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1207df8bae1dSRodney W. Grimes 12080cddd8f0SMatthew Dillon GIANT_REQUIRED; 1209df8bae1dSRodney W. Grimes vm_map_lock(map); 1210df8bae1dSRodney W. Grimes 1211df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1212df8bae1dSRodney W. Grimes 1213df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &entry)) { 1214df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1215b7b2aac2SJohn Dyson } else { 1216df8bae1dSRodney W. Grimes entry = entry->next; 1217b7b2aac2SJohn Dyson } 1218df8bae1dSRodney W. Grimes 1219df8bae1dSRodney W. Grimes /* 12200d94caffSDavid Greenman * Make a first pass to check for protection violations. 1221df8bae1dSRodney W. Grimes */ 1222df8bae1dSRodney W. Grimes current = entry; 1223df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1224afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1225a1f6d91cSDavid Greenman vm_map_unlock(map); 1226df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1227a1f6d91cSDavid Greenman } 1228df8bae1dSRodney W. Grimes if ((new_prot & current->max_protection) != new_prot) { 1229df8bae1dSRodney W. Grimes vm_map_unlock(map); 1230df8bae1dSRodney W. Grimes return (KERN_PROTECTION_FAILURE); 1231df8bae1dSRodney W. Grimes } 1232df8bae1dSRodney W. Grimes current = current->next; 1233df8bae1dSRodney W. Grimes } 1234df8bae1dSRodney W. Grimes 1235df8bae1dSRodney W. Grimes /* 12360d94caffSDavid Greenman * Go back and fix up protections. [Note that clipping is not 12370d94caffSDavid Greenman * necessary the second time.] 1238df8bae1dSRodney W. Grimes */ 1239df8bae1dSRodney W. Grimes current = entry; 1240df8bae1dSRodney W. Grimes while ((current != &map->header) && (current->start < end)) { 1241df8bae1dSRodney W. Grimes vm_prot_t old_prot; 1242df8bae1dSRodney W. Grimes 1243df8bae1dSRodney W. Grimes vm_map_clip_end(map, current, end); 1244df8bae1dSRodney W. Grimes 1245df8bae1dSRodney W. Grimes old_prot = current->protection; 1246df8bae1dSRodney W. Grimes if (set_max) 1247df8bae1dSRodney W. Grimes current->protection = 1248df8bae1dSRodney W. Grimes (current->max_protection = new_prot) & 1249df8bae1dSRodney W. Grimes old_prot; 1250df8bae1dSRodney W. Grimes else 1251df8bae1dSRodney W. Grimes current->protection = new_prot; 1252df8bae1dSRodney W. Grimes 1253df8bae1dSRodney W. Grimes /* 12540d94caffSDavid Greenman * Update physical map if necessary. Worry about copy-on-write 12550d94caffSDavid Greenman * here -- CHECK THIS XXX 1256df8bae1dSRodney W. Grimes */ 1257df8bae1dSRodney W. Grimes if (current->protection != old_prot) { 1258afa07f7eSJohn Dyson #define MASK(entry) (((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \ 1259df8bae1dSRodney W. Grimes VM_PROT_ALL) 1260df8bae1dSRodney W. Grimes pmap_protect(map->pmap, current->start, 1261df8bae1dSRodney W. Grimes current->end, 12621c85e3dfSAlan Cox current->protection & MASK(current)); 1263df8bae1dSRodney W. Grimes #undef MASK 1264df8bae1dSRodney W. Grimes } 12657d78abc9SJohn Dyson vm_map_simplify_entry(map, current); 1266df8bae1dSRodney W. Grimes current = current->next; 1267df8bae1dSRodney W. Grimes } 1268df8bae1dSRodney W. Grimes vm_map_unlock(map); 1269df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1270df8bae1dSRodney W. Grimes } 1271df8bae1dSRodney W. Grimes 1272df8bae1dSRodney W. Grimes /* 1273867a482dSJohn Dyson * vm_map_madvise: 1274867a482dSJohn Dyson * 1275867a482dSJohn Dyson * This routine traverses a processes map handling the madvise 1276f7fc307aSAlan Cox * system call. Advisories are classified as either those effecting 1277f7fc307aSAlan Cox * the vm_map_entry structure, or those effecting the underlying 1278f7fc307aSAlan Cox * objects. 1279867a482dSJohn Dyson */ 1280b4309055SMatthew Dillon int 12811b40f8c0SMatthew Dillon vm_map_madvise( 12821b40f8c0SMatthew Dillon vm_map_t map, 12831b40f8c0SMatthew Dillon vm_offset_t start, 12841b40f8c0SMatthew Dillon vm_offset_t end, 12851b40f8c0SMatthew Dillon int behav) 1286867a482dSJohn Dyson { 1287f7fc307aSAlan Cox vm_map_entry_t current, entry; 1288b4309055SMatthew Dillon int modify_map = 0; 1289867a482dSJohn Dyson 12900cddd8f0SMatthew Dillon GIANT_REQUIRED; 12910cddd8f0SMatthew Dillon 1292b4309055SMatthew Dillon /* 1293b4309055SMatthew Dillon * Some madvise calls directly modify the vm_map_entry, in which case 1294b4309055SMatthew Dillon * we need to use an exclusive lock on the map and we need to perform 1295b4309055SMatthew Dillon * various clipping operations. Otherwise we only need a read-lock 1296b4309055SMatthew Dillon * on the map. 1297b4309055SMatthew Dillon */ 1298b4309055SMatthew Dillon switch(behav) { 1299b4309055SMatthew Dillon case MADV_NORMAL: 1300b4309055SMatthew Dillon case MADV_SEQUENTIAL: 1301b4309055SMatthew Dillon case MADV_RANDOM: 13024f79d873SMatthew Dillon case MADV_NOSYNC: 13034f79d873SMatthew Dillon case MADV_AUTOSYNC: 13049730a5daSPaul Saab case MADV_NOCORE: 13059730a5daSPaul Saab case MADV_CORE: 1306b4309055SMatthew Dillon modify_map = 1; 1307867a482dSJohn Dyson vm_map_lock(map); 1308b4309055SMatthew Dillon break; 1309b4309055SMatthew Dillon case MADV_WILLNEED: 1310b4309055SMatthew Dillon case MADV_DONTNEED: 1311b4309055SMatthew Dillon case MADV_FREE: 1312f7fc307aSAlan Cox vm_map_lock_read(map); 1313b4309055SMatthew Dillon break; 1314b4309055SMatthew Dillon default: 1315b4309055SMatthew Dillon return (KERN_INVALID_ARGUMENT); 1316b4309055SMatthew Dillon } 1317b4309055SMatthew Dillon 1318b4309055SMatthew Dillon /* 1319b4309055SMatthew Dillon * Locate starting entry and clip if necessary. 1320b4309055SMatthew Dillon */ 1321867a482dSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 1322867a482dSJohn Dyson 1323867a482dSJohn Dyson if (vm_map_lookup_entry(map, start, &entry)) { 1324f7fc307aSAlan Cox if (modify_map) 1325867a482dSJohn Dyson vm_map_clip_start(map, entry, start); 1326b4309055SMatthew Dillon } else { 1327867a482dSJohn Dyson entry = entry->next; 1328b4309055SMatthew Dillon } 1329867a482dSJohn Dyson 1330f7fc307aSAlan Cox if (modify_map) { 1331f7fc307aSAlan Cox /* 1332f7fc307aSAlan Cox * madvise behaviors that are implemented in the vm_map_entry. 1333f7fc307aSAlan Cox * 1334f7fc307aSAlan Cox * We clip the vm_map_entry so that behavioral changes are 1335f7fc307aSAlan Cox * limited to the specified address range. 1336f7fc307aSAlan Cox */ 1337867a482dSJohn Dyson for (current = entry; 1338867a482dSJohn Dyson (current != &map->header) && (current->start < end); 1339b4309055SMatthew Dillon current = current->next 1340b4309055SMatthew Dillon ) { 1341f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1342867a482dSJohn Dyson continue; 1343fed9a903SJohn Dyson 134447221757SJohn Dyson vm_map_clip_end(map, current, end); 1345fed9a903SJohn Dyson 1346f7fc307aSAlan Cox switch (behav) { 1347867a482dSJohn Dyson case MADV_NORMAL: 13487f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL); 1349867a482dSJohn Dyson break; 1350867a482dSJohn Dyson case MADV_SEQUENTIAL: 13517f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL); 1352867a482dSJohn Dyson break; 1353867a482dSJohn Dyson case MADV_RANDOM: 13547f866e4bSAlan Cox vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM); 1355867a482dSJohn Dyson break; 13564f79d873SMatthew Dillon case MADV_NOSYNC: 13574f79d873SMatthew Dillon current->eflags |= MAP_ENTRY_NOSYNC; 13584f79d873SMatthew Dillon break; 13594f79d873SMatthew Dillon case MADV_AUTOSYNC: 13604f79d873SMatthew Dillon current->eflags &= ~MAP_ENTRY_NOSYNC; 13614f79d873SMatthew Dillon break; 13629730a5daSPaul Saab case MADV_NOCORE: 13639730a5daSPaul Saab current->eflags |= MAP_ENTRY_NOCOREDUMP; 13649730a5daSPaul Saab break; 13659730a5daSPaul Saab case MADV_CORE: 13669730a5daSPaul Saab current->eflags &= ~MAP_ENTRY_NOCOREDUMP; 13679730a5daSPaul Saab break; 1368867a482dSJohn Dyson default: 1369867a482dSJohn Dyson break; 1370867a482dSJohn Dyson } 1371f7fc307aSAlan Cox vm_map_simplify_entry(map, current); 1372867a482dSJohn Dyson } 1373867a482dSJohn Dyson vm_map_unlock(map); 1374b4309055SMatthew Dillon } else { 1375f7fc307aSAlan Cox vm_pindex_t pindex; 1376f7fc307aSAlan Cox int count; 1377f7fc307aSAlan Cox 1378f7fc307aSAlan Cox /* 1379f7fc307aSAlan Cox * madvise behaviors that are implemented in the underlying 1380f7fc307aSAlan Cox * vm_object. 1381f7fc307aSAlan Cox * 1382f7fc307aSAlan Cox * Since we don't clip the vm_map_entry, we have to clip 1383f7fc307aSAlan Cox * the vm_object pindex and count. 1384f7fc307aSAlan Cox */ 1385f7fc307aSAlan Cox for (current = entry; 1386f7fc307aSAlan Cox (current != &map->header) && (current->start < end); 1387b4309055SMatthew Dillon current = current->next 1388b4309055SMatthew Dillon ) { 13895f99b57cSMatthew Dillon vm_offset_t useStart; 13905f99b57cSMatthew Dillon 1391f7fc307aSAlan Cox if (current->eflags & MAP_ENTRY_IS_SUB_MAP) 1392f7fc307aSAlan Cox continue; 1393f7fc307aSAlan Cox 1394f7fc307aSAlan Cox pindex = OFF_TO_IDX(current->offset); 1395f7fc307aSAlan Cox count = atop(current->end - current->start); 13965f99b57cSMatthew Dillon useStart = current->start; 1397f7fc307aSAlan Cox 1398f7fc307aSAlan Cox if (current->start < start) { 1399f7fc307aSAlan Cox pindex += atop(start - current->start); 1400f7fc307aSAlan Cox count -= atop(start - current->start); 14015f99b57cSMatthew Dillon useStart = start; 1402f7fc307aSAlan Cox } 1403f7fc307aSAlan Cox if (current->end > end) 1404f7fc307aSAlan Cox count -= atop(current->end - end); 1405f7fc307aSAlan Cox 1406f7fc307aSAlan Cox if (count <= 0) 1407f7fc307aSAlan Cox continue; 1408f7fc307aSAlan Cox 1409f7fc307aSAlan Cox vm_object_madvise(current->object.vm_object, 1410f7fc307aSAlan Cox pindex, count, behav); 1411b4309055SMatthew Dillon if (behav == MADV_WILLNEED) { 1412b4309055SMatthew Dillon pmap_object_init_pt( 1413b4309055SMatthew Dillon map->pmap, 14145f99b57cSMatthew Dillon useStart, 1415f7fc307aSAlan Cox current->object.vm_object, 1416b4309055SMatthew Dillon pindex, 1417b4309055SMatthew Dillon (count << PAGE_SHIFT), 1418e3026983SMatthew Dillon MAP_PREFAULT_MADVISE 1419b4309055SMatthew Dillon ); 1420f7fc307aSAlan Cox } 1421f7fc307aSAlan Cox } 1422f7fc307aSAlan Cox vm_map_unlock_read(map); 1423f7fc307aSAlan Cox } 1424b4309055SMatthew Dillon return (0); 1425867a482dSJohn Dyson } 1426867a482dSJohn Dyson 1427867a482dSJohn Dyson 1428867a482dSJohn Dyson /* 1429df8bae1dSRodney W. Grimes * vm_map_inherit: 1430df8bae1dSRodney W. Grimes * 1431df8bae1dSRodney W. Grimes * Sets the inheritance of the specified address 1432df8bae1dSRodney W. Grimes * range in the target map. Inheritance 1433df8bae1dSRodney W. Grimes * affects how the map will be shared with 1434df8bae1dSRodney W. Grimes * child maps at the time of vm_map_fork. 1435df8bae1dSRodney W. Grimes */ 1436df8bae1dSRodney W. Grimes int 1437b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end, 1438b9dcd593SBruce Evans vm_inherit_t new_inheritance) 1439df8bae1dSRodney W. Grimes { 1440c0877f10SJohn Dyson vm_map_entry_t entry; 1441df8bae1dSRodney W. Grimes vm_map_entry_t temp_entry; 1442df8bae1dSRodney W. Grimes 14430cddd8f0SMatthew Dillon GIANT_REQUIRED; 14440cddd8f0SMatthew Dillon 1445df8bae1dSRodney W. Grimes switch (new_inheritance) { 1446df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 1447df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 1448df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 1449df8bae1dSRodney W. Grimes break; 1450df8bae1dSRodney W. Grimes default: 1451df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1452df8bae1dSRodney W. Grimes } 1453df8bae1dSRodney W. Grimes 1454df8bae1dSRodney W. Grimes vm_map_lock(map); 1455df8bae1dSRodney W. Grimes 1456df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1457df8bae1dSRodney W. Grimes 1458df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &temp_entry)) { 1459df8bae1dSRodney W. Grimes entry = temp_entry; 1460df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 14610d94caffSDavid Greenman } else 1462df8bae1dSRodney W. Grimes entry = temp_entry->next; 1463df8bae1dSRodney W. Grimes 1464df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1465df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1466df8bae1dSRodney W. Grimes 1467df8bae1dSRodney W. Grimes entry->inheritance = new_inheritance; 1468df8bae1dSRodney W. Grimes 146944428f62SAlan Cox vm_map_simplify_entry(map, entry); 147044428f62SAlan Cox 1471df8bae1dSRodney W. Grimes entry = entry->next; 1472df8bae1dSRodney W. Grimes } 1473df8bae1dSRodney W. Grimes 1474df8bae1dSRodney W. Grimes vm_map_unlock(map); 1475df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1476df8bae1dSRodney W. Grimes } 1477df8bae1dSRodney W. Grimes 1478df8bae1dSRodney W. Grimes /* 14797aaaa4fdSJohn Dyson * Implement the semantics of mlock 14807aaaa4fdSJohn Dyson */ 14817aaaa4fdSJohn Dyson int 14821b40f8c0SMatthew Dillon vm_map_user_pageable( 14831b40f8c0SMatthew Dillon vm_map_t map, 14841b40f8c0SMatthew Dillon vm_offset_t start, 14851b40f8c0SMatthew Dillon vm_offset_t end, 14861b40f8c0SMatthew Dillon boolean_t new_pageable) 14877aaaa4fdSJohn Dyson { 1488b44959ceSTor Egge vm_map_entry_t entry; 14897aaaa4fdSJohn Dyson vm_map_entry_t start_entry; 1490b44959ceSTor Egge vm_offset_t estart; 1491e7673b84STor Egge vm_offset_t eend; 14927aaaa4fdSJohn Dyson int rv; 14937aaaa4fdSJohn Dyson 14947aaaa4fdSJohn Dyson vm_map_lock(map); 14957aaaa4fdSJohn Dyson VM_MAP_RANGE_CHECK(map, start, end); 14967aaaa4fdSJohn Dyson 14977aaaa4fdSJohn Dyson if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 14987aaaa4fdSJohn Dyson vm_map_unlock(map); 14997aaaa4fdSJohn Dyson return (KERN_INVALID_ADDRESS); 15007aaaa4fdSJohn Dyson } 15017aaaa4fdSJohn Dyson 15027aaaa4fdSJohn Dyson if (new_pageable) { 15037aaaa4fdSJohn Dyson 15047aaaa4fdSJohn Dyson entry = start_entry; 15057aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 15067aaaa4fdSJohn Dyson 15077aaaa4fdSJohn Dyson /* 15087aaaa4fdSJohn Dyson * Now decrement the wiring count for each region. If a region 15097aaaa4fdSJohn Dyson * becomes completely unwired, unwire its physical pages and 15107aaaa4fdSJohn Dyson * mappings. 15117aaaa4fdSJohn Dyson */ 15127aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 1513afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 15147aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 1515afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 15167aaaa4fdSJohn Dyson entry->wired_count--; 15177aaaa4fdSJohn Dyson if (entry->wired_count == 0) 15187aaaa4fdSJohn Dyson vm_fault_unwire(map, entry->start, entry->end); 15197aaaa4fdSJohn Dyson } 1520b44959ceSTor Egge vm_map_simplify_entry(map,entry); 15217aaaa4fdSJohn Dyson entry = entry->next; 15227aaaa4fdSJohn Dyson } 15237aaaa4fdSJohn Dyson } else { 15247aaaa4fdSJohn Dyson 15257aaaa4fdSJohn Dyson entry = start_entry; 15267aaaa4fdSJohn Dyson 15277aaaa4fdSJohn Dyson while ((entry != &map->header) && (entry->start < end)) { 15287aaaa4fdSJohn Dyson 1529afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_USER_WIRED) { 15307aaaa4fdSJohn Dyson entry = entry->next; 15317aaaa4fdSJohn Dyson continue; 15327aaaa4fdSJohn Dyson } 15337aaaa4fdSJohn Dyson 15347aaaa4fdSJohn Dyson if (entry->wired_count != 0) { 15357aaaa4fdSJohn Dyson entry->wired_count++; 1536afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 15377aaaa4fdSJohn Dyson entry = entry->next; 15387aaaa4fdSJohn Dyson continue; 15397aaaa4fdSJohn Dyson } 15407aaaa4fdSJohn Dyson 15417aaaa4fdSJohn Dyson /* Here on entry being newly wired */ 15427aaaa4fdSJohn Dyson 15439fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1544afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 15457aaaa4fdSJohn Dyson if (copyflag && ((entry->protection & VM_PROT_WRITE) != 0)) { 15467aaaa4fdSJohn Dyson 15477aaaa4fdSJohn Dyson vm_object_shadow(&entry->object.vm_object, 15487aaaa4fdSJohn Dyson &entry->offset, 1549c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1550afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 15517aaaa4fdSJohn Dyson 15524e71e795SMatthew Dillon } else if (entry->object.vm_object == NULL && 15534e71e795SMatthew Dillon !map->system_map) { 15547aaaa4fdSJohn Dyson 15557aaaa4fdSJohn Dyson entry->object.vm_object = 15567aaaa4fdSJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1557c2e11a03SJohn Dyson atop(entry->end - entry->start)); 15587aaaa4fdSJohn Dyson entry->offset = (vm_offset_t) 0; 15597aaaa4fdSJohn Dyson 15607aaaa4fdSJohn Dyson } 15617aaaa4fdSJohn Dyson } 15627aaaa4fdSJohn Dyson 15637aaaa4fdSJohn Dyson vm_map_clip_start(map, entry, start); 15647aaaa4fdSJohn Dyson vm_map_clip_end(map, entry, end); 15657aaaa4fdSJohn Dyson 15667aaaa4fdSJohn Dyson entry->wired_count++; 1567afa07f7eSJohn Dyson entry->eflags |= MAP_ENTRY_USER_WIRED; 1568b44959ceSTor Egge estart = entry->start; 1569e7673b84STor Egge eend = entry->end; 15707aaaa4fdSJohn Dyson 15717aaaa4fdSJohn Dyson /* First we need to allow map modifications */ 157225adb370SBrian Feldman vm_map_set_recursive(map); 157325adb370SBrian Feldman vm_map_lock_downgrade(map); 157447221757SJohn Dyson map->timestamp++; 15757aaaa4fdSJohn Dyson 15767aaaa4fdSJohn Dyson rv = vm_fault_user_wire(map, entry->start, entry->end); 15777aaaa4fdSJohn Dyson if (rv) { 157825adb370SBrian Feldman 15797aaaa4fdSJohn Dyson entry->wired_count--; 1580afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_USER_WIRED; 158125adb370SBrian Feldman 158225adb370SBrian Feldman vm_map_clear_recursive(map); 15837aaaa4fdSJohn Dyson vm_map_unlock(map); 15847aaaa4fdSJohn Dyson 1585e7673b84STor Egge /* 1586e7673b84STor Egge * At this point, the map is unlocked, and 1587e7673b84STor Egge * entry might no longer be valid. Use copy 1588e7673b84STor Egge * of entry start value obtained while entry 1589e7673b84STor Egge * was valid. 1590e7673b84STor Egge */ 1591e7673b84STor Egge (void) vm_map_user_pageable(map, start, estart, 1592e7673b84STor Egge TRUE); 15937aaaa4fdSJohn Dyson return rv; 15947aaaa4fdSJohn Dyson } 15957aaaa4fdSJohn Dyson 159625adb370SBrian Feldman vm_map_clear_recursive(map); 159725adb370SBrian Feldman if (vm_map_lock_upgrade(map)) { 1598b44959ceSTor Egge vm_map_lock(map); 1599b44959ceSTor Egge if (vm_map_lookup_entry(map, estart, &entry) 1600b44959ceSTor Egge == FALSE) { 1601b44959ceSTor Egge vm_map_unlock(map); 1602e7673b84STor Egge /* 1603e7673b84STor Egge * vm_fault_user_wire succeded, thus 1604e7673b84STor Egge * the area between start and eend 1605e7673b84STor Egge * is wired and has to be unwired 1606e7673b84STor Egge * here as part of the cleanup. 1607e7673b84STor Egge */ 1608b44959ceSTor Egge (void) vm_map_user_pageable(map, 1609b44959ceSTor Egge start, 1610e7673b84STor Egge eend, 1611b44959ceSTor Egge TRUE); 1612b44959ceSTor Egge return (KERN_INVALID_ADDRESS); 1613b44959ceSTor Egge } 1614b44959ceSTor Egge } 1615b44959ceSTor Egge vm_map_simplify_entry(map,entry); 16167aaaa4fdSJohn Dyson } 16177aaaa4fdSJohn Dyson } 161847221757SJohn Dyson map->timestamp++; 16197aaaa4fdSJohn Dyson vm_map_unlock(map); 16207aaaa4fdSJohn Dyson return KERN_SUCCESS; 16217aaaa4fdSJohn Dyson } 16227aaaa4fdSJohn Dyson 16237aaaa4fdSJohn Dyson /* 1624df8bae1dSRodney W. Grimes * vm_map_pageable: 1625df8bae1dSRodney W. Grimes * 1626df8bae1dSRodney W. Grimes * Sets the pageability of the specified address 1627df8bae1dSRodney W. Grimes * range in the target map. Regions specified 1628df8bae1dSRodney W. Grimes * as not pageable require locked-down physical 1629df8bae1dSRodney W. Grimes * memory and physical page maps. 1630df8bae1dSRodney W. Grimes * 1631df8bae1dSRodney W. Grimes * The map must not be locked, but a reference 1632df8bae1dSRodney W. Grimes * must remain to the map throughout the call. 1633df8bae1dSRodney W. Grimes */ 1634df8bae1dSRodney W. Grimes int 16351b40f8c0SMatthew Dillon vm_map_pageable( 16361b40f8c0SMatthew Dillon vm_map_t map, 16371b40f8c0SMatthew Dillon vm_offset_t start, 16381b40f8c0SMatthew Dillon vm_offset_t end, 16391b40f8c0SMatthew Dillon boolean_t new_pageable) 1640df8bae1dSRodney W. Grimes { 1641c0877f10SJohn Dyson vm_map_entry_t entry; 1642df8bae1dSRodney W. Grimes vm_map_entry_t start_entry; 1643c0877f10SJohn Dyson vm_offset_t failed = 0; 1644df8bae1dSRodney W. Grimes int rv; 1645df8bae1dSRodney W. Grimes 16460cddd8f0SMatthew Dillon GIANT_REQUIRED; 16470cddd8f0SMatthew Dillon 1648df8bae1dSRodney W. Grimes vm_map_lock(map); 1649df8bae1dSRodney W. Grimes 1650df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1651df8bae1dSRodney W. Grimes 1652df8bae1dSRodney W. Grimes /* 16530d94caffSDavid Greenman * Only one pageability change may take place at one time, since 16540d94caffSDavid Greenman * vm_fault assumes it will be called only once for each 16550d94caffSDavid Greenman * wiring/unwiring. Therefore, we have to make sure we're actually 16560d94caffSDavid Greenman * changing the pageability for the entire region. We do so before 16570d94caffSDavid Greenman * making any changes. 1658df8bae1dSRodney W. Grimes */ 1659df8bae1dSRodney W. Grimes if (vm_map_lookup_entry(map, start, &start_entry) == FALSE) { 1660df8bae1dSRodney W. Grimes vm_map_unlock(map); 1661df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1662df8bae1dSRodney W. Grimes } 1663df8bae1dSRodney W. Grimes entry = start_entry; 1664df8bae1dSRodney W. Grimes 1665df8bae1dSRodney W. Grimes /* 16660d94caffSDavid Greenman * Actions are rather different for wiring and unwiring, so we have 16670d94caffSDavid Greenman * two separate cases. 1668df8bae1dSRodney W. Grimes */ 1669df8bae1dSRodney W. Grimes if (new_pageable) { 1670df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1671df8bae1dSRodney W. Grimes 1672df8bae1dSRodney W. Grimes /* 16730d94caffSDavid Greenman * Unwiring. First ensure that the range to be unwired is 16740d94caffSDavid Greenman * really wired down and that there are no holes. 1675df8bae1dSRodney W. Grimes */ 1676df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1677df8bae1dSRodney W. Grimes if (entry->wired_count == 0 || 1678df8bae1dSRodney W. Grimes (entry->end < end && 1679df8bae1dSRodney W. Grimes (entry->next == &map->header || 1680df8bae1dSRodney W. Grimes entry->next->start > entry->end))) { 1681df8bae1dSRodney W. Grimes vm_map_unlock(map); 1682df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1683df8bae1dSRodney W. Grimes } 1684df8bae1dSRodney W. Grimes entry = entry->next; 1685df8bae1dSRodney W. Grimes } 1686df8bae1dSRodney W. Grimes 1687df8bae1dSRodney W. Grimes /* 16880d94caffSDavid Greenman * Now decrement the wiring count for each region. If a region 16890d94caffSDavid Greenman * becomes completely unwired, unwire its physical pages and 16900d94caffSDavid Greenman * mappings. 1691df8bae1dSRodney W. Grimes */ 1692df8bae1dSRodney W. Grimes entry = start_entry; 1693df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1694df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1695df8bae1dSRodney W. Grimes 1696df8bae1dSRodney W. Grimes entry->wired_count--; 1697df8bae1dSRodney W. Grimes if (entry->wired_count == 0) 1698df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 1699df8bae1dSRodney W. Grimes 170044428f62SAlan Cox vm_map_simplify_entry(map, entry); 170144428f62SAlan Cox 1702df8bae1dSRodney W. Grimes entry = entry->next; 1703df8bae1dSRodney W. Grimes } 17040d94caffSDavid Greenman } else { 1705df8bae1dSRodney W. Grimes /* 1706df8bae1dSRodney W. Grimes * Wiring. We must do this in two passes: 1707df8bae1dSRodney W. Grimes * 17080d94caffSDavid Greenman * 1. Holding the write lock, we create any shadow or zero-fill 17090d94caffSDavid Greenman * objects that need to be created. Then we clip each map 17100d94caffSDavid Greenman * entry to the region to be wired and increment its wiring 17110d94caffSDavid Greenman * count. We create objects before clipping the map entries 1712df8bae1dSRodney W. Grimes * to avoid object proliferation. 1713df8bae1dSRodney W. Grimes * 17140d94caffSDavid Greenman * 2. We downgrade to a read lock, and call vm_fault_wire to 17150d94caffSDavid Greenman * fault in the pages for any newly wired area (wired_count is 17160d94caffSDavid Greenman * 1). 1717df8bae1dSRodney W. Grimes * 17180d94caffSDavid Greenman * Downgrading to a read lock for vm_fault_wire avoids a possible 171924a1cce3SDavid Greenman * deadlock with another process that may have faulted on one 17200d94caffSDavid Greenman * of the pages to be wired (it would mark the page busy, 17210d94caffSDavid Greenman * blocking us, then in turn block on the map lock that we 17220d94caffSDavid Greenman * hold). Because of problems in the recursive lock package, 17230d94caffSDavid Greenman * we cannot upgrade to a write lock in vm_map_lookup. Thus, 17240d94caffSDavid Greenman * any actions that require the write lock must be done 17250d94caffSDavid Greenman * beforehand. Because we keep the read lock on the map, the 17260d94caffSDavid Greenman * copy-on-write status of the entries we modify here cannot 17270d94caffSDavid Greenman * change. 1728df8bae1dSRodney W. Grimes */ 1729df8bae1dSRodney W. Grimes 1730df8bae1dSRodney W. Grimes /* 1731df8bae1dSRodney W. Grimes * Pass 1. 1732df8bae1dSRodney W. Grimes */ 1733df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 1734df8bae1dSRodney W. Grimes if (entry->wired_count == 0) { 1735df8bae1dSRodney W. Grimes 1736df8bae1dSRodney W. Grimes /* 1737df8bae1dSRodney W. Grimes * Perform actions of vm_map_lookup that need 1738df8bae1dSRodney W. Grimes * the write lock on the map: create a shadow 1739df8bae1dSRodney W. Grimes * object for a copy-on-write region, or an 1740df8bae1dSRodney W. Grimes * object for a zero-fill region. 1741df8bae1dSRodney W. Grimes * 1742df8bae1dSRodney W. Grimes * We don't have to do this for entries that 1743ad5fca3bSAlan Cox * point to sub maps, because we won't 1744ad5fca3bSAlan Cox * hold the lock on the sub map. 1745df8bae1dSRodney W. Grimes */ 17469fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 1747afa07f7eSJohn Dyson int copyflag = entry->eflags & MAP_ENTRY_NEEDS_COPY; 1748b5b40fa6SJohn Dyson if (copyflag && 1749df8bae1dSRodney W. Grimes ((entry->protection & VM_PROT_WRITE) != 0)) { 1750df8bae1dSRodney W. Grimes 1751df8bae1dSRodney W. Grimes vm_object_shadow(&entry->object.vm_object, 1752df8bae1dSRodney W. Grimes &entry->offset, 1753c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1754afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 17554e71e795SMatthew Dillon } else if (entry->object.vm_object == NULL && 17564e71e795SMatthew Dillon !map->system_map) { 1757df8bae1dSRodney W. Grimes entry->object.vm_object = 1758a316d390SJohn Dyson vm_object_allocate(OBJT_DEFAULT, 1759c2e11a03SJohn Dyson atop(entry->end - entry->start)); 1760df8bae1dSRodney W. Grimes entry->offset = (vm_offset_t) 0; 1761df8bae1dSRodney W. Grimes } 1762df8bae1dSRodney W. Grimes } 1763df8bae1dSRodney W. Grimes } 1764df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 1765df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 1766df8bae1dSRodney W. Grimes entry->wired_count++; 1767df8bae1dSRodney W. Grimes 1768df8bae1dSRodney W. Grimes /* 1769df8bae1dSRodney W. Grimes * Check for holes 1770df8bae1dSRodney W. Grimes */ 1771df8bae1dSRodney W. Grimes if (entry->end < end && 1772df8bae1dSRodney W. Grimes (entry->next == &map->header || 1773df8bae1dSRodney W. Grimes entry->next->start > entry->end)) { 1774df8bae1dSRodney W. Grimes /* 17750d94caffSDavid Greenman * Found one. Object creation actions do not 17760d94caffSDavid Greenman * need to be undone, but the wired counts 17770d94caffSDavid Greenman * need to be restored. 1778df8bae1dSRodney W. Grimes */ 1779df8bae1dSRodney W. Grimes while (entry != &map->header && entry->end > start) { 1780df8bae1dSRodney W. Grimes entry->wired_count--; 1781df8bae1dSRodney W. Grimes entry = entry->prev; 1782df8bae1dSRodney W. Grimes } 1783df8bae1dSRodney W. Grimes vm_map_unlock(map); 1784df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1785df8bae1dSRodney W. Grimes } 1786df8bae1dSRodney W. Grimes entry = entry->next; 1787df8bae1dSRodney W. Grimes } 1788df8bae1dSRodney W. Grimes 1789df8bae1dSRodney W. Grimes /* 1790df8bae1dSRodney W. Grimes * Pass 2. 1791df8bae1dSRodney W. Grimes */ 1792df8bae1dSRodney W. Grimes 1793df8bae1dSRodney W. Grimes /* 1794df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1795df8bae1dSRodney W. Grimes * 179624a1cce3SDavid Greenman * If we are wiring in the kernel map or a submap of it, 179724a1cce3SDavid Greenman * unlock the map to avoid deadlocks. We trust that the 179824a1cce3SDavid Greenman * kernel is well-behaved, and therefore will not do 179924a1cce3SDavid Greenman * anything destructive to this region of the map while 180024a1cce3SDavid Greenman * we have it unlocked. We cannot trust user processes 180124a1cce3SDavid Greenman * to do the same. 1802df8bae1dSRodney W. Grimes * 1803df8bae1dSRodney W. Grimes * HACK HACK HACK HACK 1804df8bae1dSRodney W. Grimes */ 1805df8bae1dSRodney W. Grimes if (vm_map_pmap(map) == kernel_pmap) { 1806df8bae1dSRodney W. Grimes vm_map_unlock(map); /* trust me ... */ 18070d94caffSDavid Greenman } else { 180803e9c6c1SJohn Dyson vm_map_lock_downgrade(map); 1809df8bae1dSRodney W. Grimes } 1810df8bae1dSRodney W. Grimes 1811df8bae1dSRodney W. Grimes rv = 0; 1812df8bae1dSRodney W. Grimes entry = start_entry; 1813df8bae1dSRodney W. Grimes while (entry != &map->header && entry->start < end) { 1814df8bae1dSRodney W. Grimes /* 18150d94caffSDavid Greenman * If vm_fault_wire fails for any page we need to undo 18160d94caffSDavid Greenman * what has been done. We decrement the wiring count 18170d94caffSDavid Greenman * for those pages which have not yet been wired (now) 18180d94caffSDavid Greenman * and unwire those that have (later). 1819df8bae1dSRodney W. Grimes * 1820df8bae1dSRodney W. Grimes * XXX this violates the locking protocol on the map, 1821df8bae1dSRodney W. Grimes * needs to be fixed. 1822df8bae1dSRodney W. Grimes */ 1823df8bae1dSRodney W. Grimes if (rv) 1824df8bae1dSRodney W. Grimes entry->wired_count--; 1825df8bae1dSRodney W. Grimes else if (entry->wired_count == 1) { 1826df8bae1dSRodney W. Grimes rv = vm_fault_wire(map, entry->start, entry->end); 1827df8bae1dSRodney W. Grimes if (rv) { 1828df8bae1dSRodney W. Grimes failed = entry->start; 1829df8bae1dSRodney W. Grimes entry->wired_count--; 1830df8bae1dSRodney W. Grimes } 1831df8bae1dSRodney W. Grimes } 1832df8bae1dSRodney W. Grimes entry = entry->next; 1833df8bae1dSRodney W. Grimes } 1834df8bae1dSRodney W. Grimes 183525adb370SBrian Feldman if (vm_map_pmap(map) == kernel_pmap) { 183625adb370SBrian Feldman vm_map_lock(map); 183725adb370SBrian Feldman } 1838df8bae1dSRodney W. Grimes if (rv) { 183925adb370SBrian Feldman vm_map_unlock(map); 1840df8bae1dSRodney W. Grimes (void) vm_map_pageable(map, start, failed, TRUE); 1841df8bae1dSRodney W. Grimes return (rv); 1842df8bae1dSRodney W. Grimes } 1843e7673b84STor Egge /* 1844e7673b84STor Egge * An exclusive lock on the map is needed in order to call 1845e7673b84STor Egge * vm_map_simplify_entry(). If the current lock on the map 1846e7673b84STor Egge * is only a shared lock, an upgrade is needed. 1847e7673b84STor Egge */ 1848e7673b84STor Egge if (vm_map_pmap(map) != kernel_pmap && 1849e7673b84STor Egge vm_map_lock_upgrade(map)) { 1850e7673b84STor Egge vm_map_lock(map); 1851e7673b84STor Egge if (vm_map_lookup_entry(map, start, &start_entry) == 1852e7673b84STor Egge FALSE) { 1853e7673b84STor Egge vm_map_unlock(map); 1854e7673b84STor Egge return KERN_SUCCESS; 1855e7673b84STor Egge } 1856e7673b84STor Egge } 1857b7b2aac2SJohn Dyson vm_map_simplify_entry(map, start_entry); 1858df8bae1dSRodney W. Grimes } 1859df8bae1dSRodney W. Grimes 1860df8bae1dSRodney W. Grimes vm_map_unlock(map); 1861df8bae1dSRodney W. Grimes 1862df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1863df8bae1dSRodney W. Grimes } 1864df8bae1dSRodney W. Grimes 1865df8bae1dSRodney W. Grimes /* 1866df8bae1dSRodney W. Grimes * vm_map_clean 1867df8bae1dSRodney W. Grimes * 1868df8bae1dSRodney W. Grimes * Push any dirty cached pages in the address range to their pager. 1869df8bae1dSRodney W. Grimes * If syncio is TRUE, dirty pages are written synchronously. 1870df8bae1dSRodney W. Grimes * If invalidate is TRUE, any cached pages are freed as well. 1871df8bae1dSRodney W. Grimes * 1872df8bae1dSRodney W. Grimes * Returns an error if any part of the specified range is not mapped. 1873df8bae1dSRodney W. Grimes */ 1874df8bae1dSRodney W. Grimes int 18751b40f8c0SMatthew Dillon vm_map_clean( 18761b40f8c0SMatthew Dillon vm_map_t map, 18771b40f8c0SMatthew Dillon vm_offset_t start, 18781b40f8c0SMatthew Dillon vm_offset_t end, 18791b40f8c0SMatthew Dillon boolean_t syncio, 18801b40f8c0SMatthew Dillon boolean_t invalidate) 1881df8bae1dSRodney W. Grimes { 1882c0877f10SJohn Dyson vm_map_entry_t current; 1883df8bae1dSRodney W. Grimes vm_map_entry_t entry; 1884df8bae1dSRodney W. Grimes vm_size_t size; 1885df8bae1dSRodney W. Grimes vm_object_t object; 1886a316d390SJohn Dyson vm_ooffset_t offset; 1887df8bae1dSRodney W. Grimes 18880cddd8f0SMatthew Dillon GIANT_REQUIRED; 18890cddd8f0SMatthew Dillon 1890df8bae1dSRodney W. Grimes vm_map_lock_read(map); 1891df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 1892df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &entry)) { 1893df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1894df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1895df8bae1dSRodney W. Grimes } 1896df8bae1dSRodney W. Grimes /* 1897df8bae1dSRodney W. Grimes * Make a first pass to check for holes. 1898df8bae1dSRodney W. Grimes */ 1899df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1900afa07f7eSJohn Dyson if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1901df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1902df8bae1dSRodney W. Grimes return (KERN_INVALID_ARGUMENT); 1903df8bae1dSRodney W. Grimes } 1904df8bae1dSRodney W. Grimes if (end > current->end && 1905df8bae1dSRodney W. Grimes (current->next == &map->header || 1906df8bae1dSRodney W. Grimes current->end != current->next->start)) { 1907df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1908df8bae1dSRodney W. Grimes return (KERN_INVALID_ADDRESS); 1909df8bae1dSRodney W. Grimes } 1910df8bae1dSRodney W. Grimes } 1911df8bae1dSRodney W. Grimes 1912cf2819ccSJohn Dyson if (invalidate) 1913cf2819ccSJohn Dyson pmap_remove(vm_map_pmap(map), start, end); 1914df8bae1dSRodney W. Grimes /* 1915df8bae1dSRodney W. Grimes * Make a second pass, cleaning/uncaching pages from the indicated 1916df8bae1dSRodney W. Grimes * objects as we go. 1917df8bae1dSRodney W. Grimes */ 1918df8bae1dSRodney W. Grimes for (current = entry; current->start < end; current = current->next) { 1919df8bae1dSRodney W. Grimes offset = current->offset + (start - current->start); 1920df8bae1dSRodney W. Grimes size = (end <= current->end ? end : current->end) - start; 19219fdfe602SMatthew Dillon if (current->eflags & MAP_ENTRY_IS_SUB_MAP) { 1922c0877f10SJohn Dyson vm_map_t smap; 1923df8bae1dSRodney W. Grimes vm_map_entry_t tentry; 1924df8bae1dSRodney W. Grimes vm_size_t tsize; 1925df8bae1dSRodney W. Grimes 19269fdfe602SMatthew Dillon smap = current->object.sub_map; 1927df8bae1dSRodney W. Grimes vm_map_lock_read(smap); 1928df8bae1dSRodney W. Grimes (void) vm_map_lookup_entry(smap, offset, &tentry); 1929df8bae1dSRodney W. Grimes tsize = tentry->end - offset; 1930df8bae1dSRodney W. Grimes if (tsize < size) 1931df8bae1dSRodney W. Grimes size = tsize; 1932df8bae1dSRodney W. Grimes object = tentry->object.vm_object; 1933df8bae1dSRodney W. Grimes offset = tentry->offset + (offset - tentry->start); 1934df8bae1dSRodney W. Grimes vm_map_unlock_read(smap); 1935df8bae1dSRodney W. Grimes } else { 1936df8bae1dSRodney W. Grimes object = current->object.vm_object; 1937df8bae1dSRodney W. Grimes } 19388a02c104SJohn Dyson /* 19398a02c104SJohn Dyson * Note that there is absolutely no sense in writing out 19408a02c104SJohn Dyson * anonymous objects, so we track down the vnode object 19418a02c104SJohn Dyson * to write out. 19428a02c104SJohn Dyson * We invalidate (remove) all pages from the address space 19438a02c104SJohn Dyson * anyway, for semantic correctness. 19448c5dffe8SMatthew Dillon * 19458c5dffe8SMatthew Dillon * note: certain anonymous maps, such as MAP_NOSYNC maps, 19468c5dffe8SMatthew Dillon * may start out with a NULL object. 19478a02c104SJohn Dyson */ 19488c5dffe8SMatthew Dillon while (object && object->backing_object) { 19498a02c104SJohn Dyson object = object->backing_object; 19508a02c104SJohn Dyson offset += object->backing_object_offset; 19518a02c104SJohn Dyson if (object->size < OFF_TO_IDX(offset + size)) 19528a02c104SJohn Dyson size = IDX_TO_OFF(object->size) - offset; 19538a02c104SJohn Dyson } 1954ff359f84SMatthew Dillon if (object && (object->type == OBJT_VNODE) && 1955ff359f84SMatthew Dillon (current->protection & VM_PROT_WRITE)) { 1956df8bae1dSRodney W. Grimes /* 1957ff359f84SMatthew Dillon * Flush pages if writing is allowed, invalidate them 1958ff359f84SMatthew Dillon * if invalidation requested. Pages undergoing I/O 1959ff359f84SMatthew Dillon * will be ignored by vm_object_page_remove(). 1960f5cf85d4SDavid Greenman * 1961ff359f84SMatthew Dillon * We cannot lock the vnode and then wait for paging 1962ff359f84SMatthew Dillon * to complete without deadlocking against vm_fault. 1963ff359f84SMatthew Dillon * Instead we simply call vm_object_page_remove() and 1964ff359f84SMatthew Dillon * allow it to block internally on a page-by-page 1965ff359f84SMatthew Dillon * basis when it encounters pages undergoing async 1966ff359f84SMatthew Dillon * I/O. 1967df8bae1dSRodney W. Grimes */ 19688f9110f6SJohn Dyson int flags; 1969ff359f84SMatthew Dillon 1970ff359f84SMatthew Dillon vm_object_reference(object); 1971b40ce416SJulian Elischer vn_lock(object->handle, LK_EXCLUSIVE | LK_RETRY, curthread); 19728f9110f6SJohn Dyson flags = (syncio || invalidate) ? OBJPC_SYNC : 0; 19738f9110f6SJohn Dyson flags |= invalidate ? OBJPC_INVAL : 0; 1974a316d390SJohn Dyson vm_object_page_clean(object, 1975a316d390SJohn Dyson OFF_TO_IDX(offset), 19762be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 19778f9110f6SJohn Dyson flags); 1978cf2819ccSJohn Dyson if (invalidate) { 1979ff359f84SMatthew Dillon /*vm_object_pip_wait(object, "objmcl");*/ 1980a316d390SJohn Dyson vm_object_page_remove(object, 1981a316d390SJohn Dyson OFF_TO_IDX(offset), 19822be70f79SJohn Dyson OFF_TO_IDX(offset + size + PAGE_MASK), 1983a316d390SJohn Dyson FALSE); 1984cf2819ccSJohn Dyson } 1985b40ce416SJulian Elischer VOP_UNLOCK(object->handle, 0, curthread); 1986ff359f84SMatthew Dillon vm_object_deallocate(object); 1987a02051c3SJohn Dyson } 1988df8bae1dSRodney W. Grimes start += size; 1989df8bae1dSRodney W. Grimes } 1990df8bae1dSRodney W. Grimes 1991df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 1992df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 1993df8bae1dSRodney W. Grimes } 1994df8bae1dSRodney W. Grimes 1995df8bae1dSRodney W. Grimes /* 1996df8bae1dSRodney W. Grimes * vm_map_entry_unwire: [ internal use only ] 1997df8bae1dSRodney W. Grimes * 1998df8bae1dSRodney W. Grimes * Make the region specified by this entry pageable. 1999df8bae1dSRodney W. Grimes * 2000df8bae1dSRodney W. Grimes * The map in question should be locked. 2001df8bae1dSRodney W. Grimes * [This is the reason for this routine's existence.] 2002df8bae1dSRodney W. Grimes */ 20030362d7d7SJohn Dyson static void 20041b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry) 2005df8bae1dSRodney W. Grimes { 2006df8bae1dSRodney W. Grimes vm_fault_unwire(map, entry->start, entry->end); 2007df8bae1dSRodney W. Grimes entry->wired_count = 0; 2008df8bae1dSRodney W. Grimes } 2009df8bae1dSRodney W. Grimes 2010df8bae1dSRodney W. Grimes /* 2011df8bae1dSRodney W. Grimes * vm_map_entry_delete: [ internal use only ] 2012df8bae1dSRodney W. Grimes * 2013df8bae1dSRodney W. Grimes * Deallocate the given entry from the target map. 2014df8bae1dSRodney W. Grimes */ 20150362d7d7SJohn Dyson static void 20161b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry) 2017df8bae1dSRodney W. Grimes { 2018df8bae1dSRodney W. Grimes vm_map_entry_unlink(map, entry); 2019df8bae1dSRodney W. Grimes map->size -= entry->end - entry->start; 2020df8bae1dSRodney W. Grimes 20219fdfe602SMatthew Dillon if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) { 2022df8bae1dSRodney W. Grimes vm_object_deallocate(entry->object.vm_object); 2023b5b40fa6SJohn Dyson } 2024df8bae1dSRodney W. Grimes 2025df8bae1dSRodney W. Grimes vm_map_entry_dispose(map, entry); 2026df8bae1dSRodney W. Grimes } 2027df8bae1dSRodney W. Grimes 2028df8bae1dSRodney W. Grimes /* 2029df8bae1dSRodney W. Grimes * vm_map_delete: [ internal use only ] 2030df8bae1dSRodney W. Grimes * 2031df8bae1dSRodney W. Grimes * Deallocates the given address range from the target 2032df8bae1dSRodney W. Grimes * map. 2033df8bae1dSRodney W. Grimes */ 2034df8bae1dSRodney W. Grimes int 20351b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end) 2036df8bae1dSRodney W. Grimes { 2037cbd8ec09SJohn Dyson vm_object_t object; 2038c0877f10SJohn Dyson vm_map_entry_t entry; 2039df8bae1dSRodney W. Grimes vm_map_entry_t first_entry; 2040df8bae1dSRodney W. Grimes 20410cddd8f0SMatthew Dillon GIANT_REQUIRED; 20420cddd8f0SMatthew Dillon 2043df8bae1dSRodney W. Grimes /* 2044df8bae1dSRodney W. Grimes * Find the start of the region, and clip it 2045df8bae1dSRodney W. Grimes */ 2046876318ecSAlan Cox if (!vm_map_lookup_entry(map, start, &first_entry)) 2047df8bae1dSRodney W. Grimes entry = first_entry->next; 2048876318ecSAlan Cox else { 2049df8bae1dSRodney W. Grimes entry = first_entry; 2050df8bae1dSRodney W. Grimes vm_map_clip_start(map, entry, start); 2051df8bae1dSRodney W. Grimes /* 20520d94caffSDavid Greenman * Fix the lookup hint now, rather than each time though the 20530d94caffSDavid Greenman * loop. 2054df8bae1dSRodney W. Grimes */ 2055df8bae1dSRodney W. Grimes SAVE_HINT(map, entry->prev); 2056df8bae1dSRodney W. Grimes } 2057df8bae1dSRodney W. Grimes 2058df8bae1dSRodney W. Grimes /* 2059df8bae1dSRodney W. Grimes * Save the free space hint 2060df8bae1dSRodney W. Grimes */ 2061b18bfc3dSJohn Dyson if (entry == &map->header) { 2062b18bfc3dSJohn Dyson map->first_free = &map->header; 20632dbea5d2SJohn Dyson } else if (map->first_free->start >= start) { 2064df8bae1dSRodney W. Grimes map->first_free = entry->prev; 20652dbea5d2SJohn Dyson } 2066df8bae1dSRodney W. Grimes 2067df8bae1dSRodney W. Grimes /* 2068df8bae1dSRodney W. Grimes * Step through all entries in this region 2069df8bae1dSRodney W. Grimes */ 2070df8bae1dSRodney W. Grimes while ((entry != &map->header) && (entry->start < end)) { 2071df8bae1dSRodney W. Grimes vm_map_entry_t next; 2072b18bfc3dSJohn Dyson vm_offset_t s, e; 2073cbd8ec09SJohn Dyson vm_pindex_t offidxstart, offidxend, count; 2074df8bae1dSRodney W. Grimes 2075df8bae1dSRodney W. Grimes vm_map_clip_end(map, entry, end); 2076df8bae1dSRodney W. Grimes 2077df8bae1dSRodney W. Grimes s = entry->start; 2078df8bae1dSRodney W. Grimes e = entry->end; 2079c0877f10SJohn Dyson next = entry->next; 2080df8bae1dSRodney W. Grimes 2081cbd8ec09SJohn Dyson offidxstart = OFF_TO_IDX(entry->offset); 2082cbd8ec09SJohn Dyson count = OFF_TO_IDX(e - s); 2083cbd8ec09SJohn Dyson object = entry->object.vm_object; 20842dbea5d2SJohn Dyson 2085df8bae1dSRodney W. Grimes /* 20860d94caffSDavid Greenman * Unwire before removing addresses from the pmap; otherwise, 20870d94caffSDavid Greenman * unwiring will put the entries back in the pmap. 2088df8bae1dSRodney W. Grimes */ 2089c0877f10SJohn Dyson if (entry->wired_count != 0) { 2090df8bae1dSRodney W. Grimes vm_map_entry_unwire(map, entry); 2091c0877f10SJohn Dyson } 2092df8bae1dSRodney W. Grimes 2093cbd8ec09SJohn Dyson offidxend = offidxstart + count; 2094df8bae1dSRodney W. Grimes 2095c0877f10SJohn Dyson if ((object == kernel_object) || (object == kmem_object)) { 20962dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 2097b18bfc3dSJohn Dyson } else { 2098df8bae1dSRodney W. Grimes pmap_remove(map->pmap, s, e); 2099876318ecSAlan Cox if (object != NULL && 2100876318ecSAlan Cox object->ref_count != 1 && 2101876318ecSAlan Cox (object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING && 2102876318ecSAlan Cox (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) { 21032dbea5d2SJohn Dyson vm_object_collapse(object); 21042dbea5d2SJohn Dyson vm_object_page_remove(object, offidxstart, offidxend, FALSE); 21052dbea5d2SJohn Dyson if (object->type == OBJT_SWAP) { 2106cbd8ec09SJohn Dyson swap_pager_freespace(object, offidxstart, count); 21072dbea5d2SJohn Dyson } 2108876318ecSAlan Cox if (offidxend >= object->size && 2109876318ecSAlan Cox offidxstart < object->size) { 2110c0877f10SJohn Dyson object->size = offidxstart; 2111c0877f10SJohn Dyson } 21122dbea5d2SJohn Dyson } 2113b18bfc3dSJohn Dyson } 2114df8bae1dSRodney W. Grimes 2115df8bae1dSRodney W. Grimes /* 21160d94caffSDavid Greenman * Delete the entry (which may delete the object) only after 21170d94caffSDavid Greenman * removing all pmap entries pointing to its pages. 21180d94caffSDavid Greenman * (Otherwise, its page frames may be reallocated, and any 21190d94caffSDavid Greenman * modify bits will be set in the wrong object!) 2120df8bae1dSRodney W. Grimes */ 2121df8bae1dSRodney W. Grimes vm_map_entry_delete(map, entry); 2122df8bae1dSRodney W. Grimes entry = next; 2123df8bae1dSRodney W. Grimes } 2124df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2125df8bae1dSRodney W. Grimes } 2126df8bae1dSRodney W. Grimes 2127df8bae1dSRodney W. Grimes /* 2128df8bae1dSRodney W. Grimes * vm_map_remove: 2129df8bae1dSRodney W. Grimes * 2130df8bae1dSRodney W. Grimes * Remove the given address range from the target map. 2131df8bae1dSRodney W. Grimes * This is the exported form of vm_map_delete. 2132df8bae1dSRodney W. Grimes */ 2133df8bae1dSRodney W. Grimes int 21341b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end) 2135df8bae1dSRodney W. Grimes { 2136c0877f10SJohn Dyson int result, s = 0; 21378d6e8edeSDavid Greenman 21380cddd8f0SMatthew Dillon GIANT_REQUIRED; 21390cddd8f0SMatthew Dillon 214008442f8aSBosko Milekic if (map == kmem_map) 2141b18bfc3dSJohn Dyson s = splvm(); 2142df8bae1dSRodney W. Grimes 2143df8bae1dSRodney W. Grimes vm_map_lock(map); 2144df8bae1dSRodney W. Grimes VM_MAP_RANGE_CHECK(map, start, end); 2145df8bae1dSRodney W. Grimes result = vm_map_delete(map, start, end); 2146df8bae1dSRodney W. Grimes vm_map_unlock(map); 2147df8bae1dSRodney W. Grimes 214808442f8aSBosko Milekic if (map == kmem_map) 21498d6e8edeSDavid Greenman splx(s); 21508d6e8edeSDavid Greenman 2151df8bae1dSRodney W. Grimes return (result); 2152df8bae1dSRodney W. Grimes } 2153df8bae1dSRodney W. Grimes 2154df8bae1dSRodney W. Grimes /* 2155df8bae1dSRodney W. Grimes * vm_map_check_protection: 2156df8bae1dSRodney W. Grimes * 2157df8bae1dSRodney W. Grimes * Assert that the target map allows the specified 2158df8bae1dSRodney W. Grimes * privilege on the entire address region given. 2159df8bae1dSRodney W. Grimes * The entire region must be allocated. 2160df8bae1dSRodney W. Grimes */ 21610d94caffSDavid Greenman boolean_t 2162b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end, 2163b9dcd593SBruce Evans vm_prot_t protection) 2164df8bae1dSRodney W. Grimes { 2165c0877f10SJohn Dyson vm_map_entry_t entry; 2166df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2167df8bae1dSRodney W. Grimes 21680cddd8f0SMatthew Dillon GIANT_REQUIRED; 21690cddd8f0SMatthew Dillon 21702f6c16e1SAlan Cox vm_map_lock_read(map); 2171df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, start, &tmp_entry)) { 21722f6c16e1SAlan Cox vm_map_unlock_read(map); 2173df8bae1dSRodney W. Grimes return (FALSE); 2174df8bae1dSRodney W. Grimes } 2175df8bae1dSRodney W. Grimes entry = tmp_entry; 2176df8bae1dSRodney W. Grimes 2177df8bae1dSRodney W. Grimes while (start < end) { 2178df8bae1dSRodney W. Grimes if (entry == &map->header) { 21792f6c16e1SAlan Cox vm_map_unlock_read(map); 2180df8bae1dSRodney W. Grimes return (FALSE); 2181df8bae1dSRodney W. Grimes } 2182df8bae1dSRodney W. Grimes /* 2183df8bae1dSRodney W. Grimes * No holes allowed! 2184df8bae1dSRodney W. Grimes */ 2185df8bae1dSRodney W. Grimes if (start < entry->start) { 21862f6c16e1SAlan Cox vm_map_unlock_read(map); 2187df8bae1dSRodney W. Grimes return (FALSE); 2188df8bae1dSRodney W. Grimes } 2189df8bae1dSRodney W. Grimes /* 2190df8bae1dSRodney W. Grimes * Check protection associated with entry. 2191df8bae1dSRodney W. Grimes */ 2192df8bae1dSRodney W. Grimes if ((entry->protection & protection) != protection) { 21932f6c16e1SAlan Cox vm_map_unlock_read(map); 2194df8bae1dSRodney W. Grimes return (FALSE); 2195df8bae1dSRodney W. Grimes } 2196df8bae1dSRodney W. Grimes /* go to next entry */ 2197df8bae1dSRodney W. Grimes start = entry->end; 2198df8bae1dSRodney W. Grimes entry = entry->next; 2199df8bae1dSRodney W. Grimes } 22002f6c16e1SAlan Cox vm_map_unlock_read(map); 2201df8bae1dSRodney W. Grimes return (TRUE); 2202df8bae1dSRodney W. Grimes } 2203df8bae1dSRodney W. Grimes 220486524867SJohn Dyson /* 220586524867SJohn Dyson * Split the pages in a map entry into a new object. This affords 220686524867SJohn Dyson * easier removal of unused pages, and keeps object inheritance from 220786524867SJohn Dyson * being a negative impact on memory usage. 220886524867SJohn Dyson */ 2209c0877f10SJohn Dyson static void 22101b40f8c0SMatthew Dillon vm_map_split(vm_map_entry_t entry) 2211c0877f10SJohn Dyson { 221286524867SJohn Dyson vm_page_t m; 2213bd6be915SJohn Dyson vm_object_t orig_object, new_object, source; 2214c0877f10SJohn Dyson vm_offset_t s, e; 2215c0877f10SJohn Dyson vm_pindex_t offidxstart, offidxend, idx; 2216c0877f10SJohn Dyson vm_size_t size; 2217c0877f10SJohn Dyson vm_ooffset_t offset; 2218c0877f10SJohn Dyson 22190cddd8f0SMatthew Dillon GIANT_REQUIRED; 22200cddd8f0SMatthew Dillon 2221c0877f10SJohn Dyson orig_object = entry->object.vm_object; 2222c0877f10SJohn Dyson if (orig_object->type != OBJT_DEFAULT && orig_object->type != OBJT_SWAP) 2223c0877f10SJohn Dyson return; 2224c0877f10SJohn Dyson if (orig_object->ref_count <= 1) 2225c0877f10SJohn Dyson return; 2226c0877f10SJohn Dyson 2227c0877f10SJohn Dyson offset = entry->offset; 2228c0877f10SJohn Dyson s = entry->start; 2229c0877f10SJohn Dyson e = entry->end; 2230c0877f10SJohn Dyson 2231c0877f10SJohn Dyson offidxstart = OFF_TO_IDX(offset); 2232c0877f10SJohn Dyson offidxend = offidxstart + OFF_TO_IDX(e - s); 2233c0877f10SJohn Dyson size = offidxend - offidxstart; 2234c0877f10SJohn Dyson 2235c0877f10SJohn Dyson new_object = vm_pager_allocate(orig_object->type, 22366cde7a16SDavid Greenman NULL, IDX_TO_OFF(size), VM_PROT_ALL, 0LL); 2237c0877f10SJohn Dyson if (new_object == NULL) 2238c0877f10SJohn Dyson return; 2239c0877f10SJohn Dyson 2240bd6be915SJohn Dyson source = orig_object->backing_object; 2241bd6be915SJohn Dyson if (source != NULL) { 2242bd6be915SJohn Dyson vm_object_reference(source); /* Referenced by new_object */ 2243bd6be915SJohn Dyson TAILQ_INSERT_TAIL(&source->shadow_head, 2244bd6be915SJohn Dyson new_object, shadow_list); 2245069e9bc1SDoug Rabson vm_object_clear_flag(source, OBJ_ONEMAPPING); 2246bd6be915SJohn Dyson new_object->backing_object_offset = 2247a0fce827SJohn Polstra orig_object->backing_object_offset + IDX_TO_OFF(offidxstart); 2248bd6be915SJohn Dyson new_object->backing_object = source; 2249bd6be915SJohn Dyson source->shadow_count++; 2250bd6be915SJohn Dyson source->generation++; 2251bd6be915SJohn Dyson } 2252bd6be915SJohn Dyson 2253c0877f10SJohn Dyson for (idx = 0; idx < size; idx++) { 2254c0877f10SJohn Dyson vm_page_t m; 2255c0877f10SJohn Dyson 2256c0877f10SJohn Dyson retry: 2257c0877f10SJohn Dyson m = vm_page_lookup(orig_object, offidxstart + idx); 2258c0877f10SJohn Dyson if (m == NULL) 2259c0877f10SJohn Dyson continue; 22601c7c3c6aSMatthew Dillon 22611c7c3c6aSMatthew Dillon /* 22621c7c3c6aSMatthew Dillon * We must wait for pending I/O to complete before we can 22631c7c3c6aSMatthew Dillon * rename the page. 2264d1bf5d56SMatthew Dillon * 2265d1bf5d56SMatthew Dillon * We do not have to VM_PROT_NONE the page as mappings should 2266d1bf5d56SMatthew Dillon * not be changed by this operation. 22671c7c3c6aSMatthew Dillon */ 22681c7c3c6aSMatthew Dillon if (vm_page_sleep_busy(m, TRUE, "spltwt")) 2269c0877f10SJohn Dyson goto retry; 2270c0877f10SJohn Dyson 2271e69763a3SDoug Rabson vm_page_busy(m); 2272c0877f10SJohn Dyson vm_page_rename(m, new_object, idx); 22737dbf82dcSMatthew Dillon /* page automatically made dirty by rename and cache handled */ 2274e69763a3SDoug Rabson vm_page_busy(m); 2275c0877f10SJohn Dyson } 2276c0877f10SJohn Dyson 2277c0877f10SJohn Dyson if (orig_object->type == OBJT_SWAP) { 2278d474eaaaSDoug Rabson vm_object_pip_add(orig_object, 1); 2279c0877f10SJohn Dyson /* 2280c0877f10SJohn Dyson * copy orig_object pages into new_object 2281c0877f10SJohn Dyson * and destroy unneeded pages in 2282c0877f10SJohn Dyson * shadow object. 2283c0877f10SJohn Dyson */ 22841c7c3c6aSMatthew Dillon swap_pager_copy(orig_object, new_object, offidxstart, 0); 2285c0877f10SJohn Dyson vm_object_pip_wakeup(orig_object); 2286c0877f10SJohn Dyson } 2287c0877f10SJohn Dyson 228886524867SJohn Dyson for (idx = 0; idx < size; idx++) { 228986524867SJohn Dyson m = vm_page_lookup(new_object, idx); 229086524867SJohn Dyson if (m) { 2291e69763a3SDoug Rabson vm_page_wakeup(m); 229286524867SJohn Dyson } 229386524867SJohn Dyson } 229486524867SJohn Dyson 2295c0877f10SJohn Dyson entry->object.vm_object = new_object; 2296c0877f10SJohn Dyson entry->offset = 0LL; 2297c0877f10SJohn Dyson vm_object_deallocate(orig_object); 2298c0877f10SJohn Dyson } 2299c0877f10SJohn Dyson 2300df8bae1dSRodney W. Grimes /* 2301df8bae1dSRodney W. Grimes * vm_map_copy_entry: 2302df8bae1dSRodney W. Grimes * 2303df8bae1dSRodney W. Grimes * Copies the contents of the source entry to the destination 2304df8bae1dSRodney W. Grimes * entry. The entries *must* be aligned properly. 2305df8bae1dSRodney W. Grimes */ 2306f708ef1bSPoul-Henning Kamp static void 23071b40f8c0SMatthew Dillon vm_map_copy_entry( 23081b40f8c0SMatthew Dillon vm_map_t src_map, 23091b40f8c0SMatthew Dillon vm_map_t dst_map, 23101b40f8c0SMatthew Dillon vm_map_entry_t src_entry, 23111b40f8c0SMatthew Dillon vm_map_entry_t dst_entry) 2312df8bae1dSRodney W. Grimes { 2313c0877f10SJohn Dyson vm_object_t src_object; 2314c0877f10SJohn Dyson 23159fdfe602SMatthew Dillon if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP) 2316df8bae1dSRodney W. Grimes return; 2317df8bae1dSRodney W. Grimes 2318df8bae1dSRodney W. Grimes if (src_entry->wired_count == 0) { 2319df8bae1dSRodney W. Grimes 2320df8bae1dSRodney W. Grimes /* 23210d94caffSDavid Greenman * If the source entry is marked needs_copy, it is already 23220d94caffSDavid Greenman * write-protected. 2323df8bae1dSRodney W. Grimes */ 2324afa07f7eSJohn Dyson if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) { 2325df8bae1dSRodney W. Grimes pmap_protect(src_map->pmap, 2326df8bae1dSRodney W. Grimes src_entry->start, 2327df8bae1dSRodney W. Grimes src_entry->end, 2328df8bae1dSRodney W. Grimes src_entry->protection & ~VM_PROT_WRITE); 2329df8bae1dSRodney W. Grimes } 2330b18bfc3dSJohn Dyson 2331df8bae1dSRodney W. Grimes /* 2332df8bae1dSRodney W. Grimes * Make a copy of the object. 2333df8bae1dSRodney W. Grimes */ 23348aef1712SMatthew Dillon if ((src_object = src_entry->object.vm_object) != NULL) { 2335c0877f10SJohn Dyson 2336c0877f10SJohn Dyson if ((src_object->handle == NULL) && 2337c0877f10SJohn Dyson (src_object->type == OBJT_DEFAULT || 2338c0877f10SJohn Dyson src_object->type == OBJT_SWAP)) { 2339c0877f10SJohn Dyson vm_object_collapse(src_object); 234096fb8cf2SJohn Dyson if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) { 2341c0877f10SJohn Dyson vm_map_split(src_entry); 2342c0877f10SJohn Dyson src_object = src_entry->object.vm_object; 2343c0877f10SJohn Dyson } 2344c0877f10SJohn Dyson } 2345c0877f10SJohn Dyson 2346c0877f10SJohn Dyson vm_object_reference(src_object); 2347069e9bc1SDoug Rabson vm_object_clear_flag(src_object, OBJ_ONEMAPPING); 2348c0877f10SJohn Dyson dst_entry->object.vm_object = src_object; 2349afa07f7eSJohn Dyson src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2350afa07f7eSJohn Dyson dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY); 2351b18bfc3dSJohn Dyson dst_entry->offset = src_entry->offset; 2352b18bfc3dSJohn Dyson } else { 2353b18bfc3dSJohn Dyson dst_entry->object.vm_object = NULL; 2354b18bfc3dSJohn Dyson dst_entry->offset = 0; 2355b18bfc3dSJohn Dyson } 2356df8bae1dSRodney W. Grimes 2357df8bae1dSRodney W. Grimes pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start, 2358df8bae1dSRodney W. Grimes dst_entry->end - dst_entry->start, src_entry->start); 23590d94caffSDavid Greenman } else { 2360df8bae1dSRodney W. Grimes /* 2361df8bae1dSRodney W. Grimes * Of course, wired down pages can't be set copy-on-write. 23620d94caffSDavid Greenman * Cause wired pages to be copied into the new map by 23630d94caffSDavid Greenman * simulating faults (the new pages are pageable) 2364df8bae1dSRodney W. Grimes */ 2365df8bae1dSRodney W. Grimes vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry); 2366df8bae1dSRodney W. Grimes } 2367df8bae1dSRodney W. Grimes } 2368df8bae1dSRodney W. Grimes 2369df8bae1dSRodney W. Grimes /* 2370df8bae1dSRodney W. Grimes * vmspace_fork: 2371df8bae1dSRodney W. Grimes * Create a new process vmspace structure and vm_map 2372df8bae1dSRodney W. Grimes * based on those of an existing process. The new map 2373df8bae1dSRodney W. Grimes * is based on the old map, according to the inheritance 2374df8bae1dSRodney W. Grimes * values on the regions in that map. 2375df8bae1dSRodney W. Grimes * 2376df8bae1dSRodney W. Grimes * The source map must not be locked. 2377df8bae1dSRodney W. Grimes */ 2378df8bae1dSRodney W. Grimes struct vmspace * 23791b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1) 2380df8bae1dSRodney W. Grimes { 2381c0877f10SJohn Dyson struct vmspace *vm2; 2382df8bae1dSRodney W. Grimes vm_map_t old_map = &vm1->vm_map; 2383df8bae1dSRodney W. Grimes vm_map_t new_map; 2384df8bae1dSRodney W. Grimes vm_map_entry_t old_entry; 2385df8bae1dSRodney W. Grimes vm_map_entry_t new_entry; 2386de5f6a77SJohn Dyson vm_object_t object; 2387df8bae1dSRodney W. Grimes 23880cddd8f0SMatthew Dillon GIANT_REQUIRED; 23890cddd8f0SMatthew Dillon 2390df8bae1dSRodney W. Grimes vm_map_lock(old_map); 2391b823bbd6SMatthew Dillon old_map->infork = 1; 2392df8bae1dSRodney W. Grimes 23932d8acc0fSJohn Dyson vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset); 2394df8bae1dSRodney W. Grimes bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy, 2395582ec34cSAlfred Perlstein (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy); 2396df8bae1dSRodney W. Grimes new_map = &vm2->vm_map; /* XXX */ 239747221757SJohn Dyson new_map->timestamp = 1; 2398df8bae1dSRodney W. Grimes 2399df8bae1dSRodney W. Grimes old_entry = old_map->header.next; 2400df8bae1dSRodney W. Grimes 2401df8bae1dSRodney W. Grimes while (old_entry != &old_map->header) { 2402afa07f7eSJohn Dyson if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP) 2403df8bae1dSRodney W. Grimes panic("vm_map_fork: encountered a submap"); 2404df8bae1dSRodney W. Grimes 2405df8bae1dSRodney W. Grimes switch (old_entry->inheritance) { 2406df8bae1dSRodney W. Grimes case VM_INHERIT_NONE: 2407df8bae1dSRodney W. Grimes break; 2408df8bae1dSRodney W. Grimes 2409df8bae1dSRodney W. Grimes case VM_INHERIT_SHARE: 2410df8bae1dSRodney W. Grimes /* 2411fed9a903SJohn Dyson * Clone the entry, creating the shared object if necessary. 2412fed9a903SJohn Dyson */ 2413fed9a903SJohn Dyson object = old_entry->object.vm_object; 2414fed9a903SJohn Dyson if (object == NULL) { 2415fed9a903SJohn Dyson object = vm_object_allocate(OBJT_DEFAULT, 2416c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 2417fed9a903SJohn Dyson old_entry->object.vm_object = object; 2418fed9a903SJohn Dyson old_entry->offset = (vm_offset_t) 0; 24199a2f6362SAlan Cox } 24209a2f6362SAlan Cox 24219a2f6362SAlan Cox /* 24229a2f6362SAlan Cox * Add the reference before calling vm_object_shadow 24239a2f6362SAlan Cox * to insure that a shadow object is created. 24249a2f6362SAlan Cox */ 24259a2f6362SAlan Cox vm_object_reference(object); 24269a2f6362SAlan Cox if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) { 24275069bf57SJohn Dyson vm_object_shadow(&old_entry->object.vm_object, 24285069bf57SJohn Dyson &old_entry->offset, 2429c2e11a03SJohn Dyson atop(old_entry->end - old_entry->start)); 24305069bf57SJohn Dyson old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 2431d30344bdSIan Dowse /* Transfer the second reference too. */ 2432d30344bdSIan Dowse vm_object_reference( 2433d30344bdSIan Dowse old_entry->object.vm_object); 2434d30344bdSIan Dowse vm_object_deallocate(object); 24355069bf57SJohn Dyson object = old_entry->object.vm_object; 2436fed9a903SJohn Dyson } 2437069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_ONEMAPPING); 2438fed9a903SJohn Dyson 2439fed9a903SJohn Dyson /* 2440ad5fca3bSAlan Cox * Clone the entry, referencing the shared object. 2441df8bae1dSRodney W. Grimes */ 2442df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2443df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2444028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2445df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2446df8bae1dSRodney W. Grimes 2447df8bae1dSRodney W. Grimes /* 24480d94caffSDavid Greenman * Insert the entry into the new map -- we know we're 24490d94caffSDavid Greenman * inserting at the end of the new map. 2450df8bae1dSRodney W. Grimes */ 2451df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2452df8bae1dSRodney W. Grimes new_entry); 2453df8bae1dSRodney W. Grimes 2454df8bae1dSRodney W. Grimes /* 2455df8bae1dSRodney W. Grimes * Update the physical map 2456df8bae1dSRodney W. Grimes */ 2457df8bae1dSRodney W. Grimes pmap_copy(new_map->pmap, old_map->pmap, 2458df8bae1dSRodney W. Grimes new_entry->start, 2459df8bae1dSRodney W. Grimes (old_entry->end - old_entry->start), 2460df8bae1dSRodney W. Grimes old_entry->start); 2461df8bae1dSRodney W. Grimes break; 2462df8bae1dSRodney W. Grimes 2463df8bae1dSRodney W. Grimes case VM_INHERIT_COPY: 2464df8bae1dSRodney W. Grimes /* 2465df8bae1dSRodney W. Grimes * Clone the entry and link into the map. 2466df8bae1dSRodney W. Grimes */ 2467df8bae1dSRodney W. Grimes new_entry = vm_map_entry_create(new_map); 2468df8bae1dSRodney W. Grimes *new_entry = *old_entry; 2469028fe6ecSTor Egge new_entry->eflags &= ~MAP_ENTRY_USER_WIRED; 2470df8bae1dSRodney W. Grimes new_entry->wired_count = 0; 2471df8bae1dSRodney W. Grimes new_entry->object.vm_object = NULL; 2472df8bae1dSRodney W. Grimes vm_map_entry_link(new_map, new_map->header.prev, 2473df8bae1dSRodney W. Grimes new_entry); 2474bd7e5f99SJohn Dyson vm_map_copy_entry(old_map, new_map, old_entry, 2475bd7e5f99SJohn Dyson new_entry); 2476df8bae1dSRodney W. Grimes break; 2477df8bae1dSRodney W. Grimes } 2478df8bae1dSRodney W. Grimes old_entry = old_entry->next; 2479df8bae1dSRodney W. Grimes } 2480df8bae1dSRodney W. Grimes 2481df8bae1dSRodney W. Grimes new_map->size = old_map->size; 2482b823bbd6SMatthew Dillon old_map->infork = 0; 2483df8bae1dSRodney W. Grimes vm_map_unlock(old_map); 2484df8bae1dSRodney W. Grimes 2485df8bae1dSRodney W. Grimes return (vm2); 2486df8bae1dSRodney W. Grimes } 2487df8bae1dSRodney W. Grimes 248894f7e29aSAlan Cox int 248994f7e29aSAlan Cox vm_map_stack (vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize, 249094f7e29aSAlan Cox vm_prot_t prot, vm_prot_t max, int cow) 249194f7e29aSAlan Cox { 249294f7e29aSAlan Cox vm_map_entry_t prev_entry; 249394f7e29aSAlan Cox vm_map_entry_t new_stack_entry; 249494f7e29aSAlan Cox vm_size_t init_ssize; 249594f7e29aSAlan Cox int rv; 249694f7e29aSAlan Cox 24970cddd8f0SMatthew Dillon GIANT_REQUIRED; 24980cddd8f0SMatthew Dillon 249994f7e29aSAlan Cox if (VM_MIN_ADDRESS > 0 && addrbos < VM_MIN_ADDRESS) 250094f7e29aSAlan Cox return (KERN_NO_SPACE); 250194f7e29aSAlan Cox 2502cbc89bfbSPaul Saab if (max_ssize < sgrowsiz) 250394f7e29aSAlan Cox init_ssize = max_ssize; 250494f7e29aSAlan Cox else 2505cbc89bfbSPaul Saab init_ssize = sgrowsiz; 250694f7e29aSAlan Cox 250794f7e29aSAlan Cox vm_map_lock(map); 250894f7e29aSAlan Cox 250994f7e29aSAlan Cox /* If addr is already mapped, no go */ 251094f7e29aSAlan Cox if (vm_map_lookup_entry(map, addrbos, &prev_entry)) { 251194f7e29aSAlan Cox vm_map_unlock(map); 251294f7e29aSAlan Cox return (KERN_NO_SPACE); 251394f7e29aSAlan Cox } 251494f7e29aSAlan Cox 251594f7e29aSAlan Cox /* If we can't accomodate max_ssize in the current mapping, 251694f7e29aSAlan Cox * no go. However, we need to be aware that subsequent user 251794f7e29aSAlan Cox * mappings might map into the space we have reserved for 251894f7e29aSAlan Cox * stack, and currently this space is not protected. 251994f7e29aSAlan Cox * 252094f7e29aSAlan Cox * Hopefully we will at least detect this condition 252194f7e29aSAlan Cox * when we try to grow the stack. 252294f7e29aSAlan Cox */ 252394f7e29aSAlan Cox if ((prev_entry->next != &map->header) && 252494f7e29aSAlan Cox (prev_entry->next->start < addrbos + max_ssize)) { 252594f7e29aSAlan Cox vm_map_unlock(map); 252694f7e29aSAlan Cox return (KERN_NO_SPACE); 252794f7e29aSAlan Cox } 252894f7e29aSAlan Cox 252994f7e29aSAlan Cox /* We initially map a stack of only init_ssize. We will 253094f7e29aSAlan Cox * grow as needed later. Since this is to be a grow 253194f7e29aSAlan Cox * down stack, we map at the top of the range. 253294f7e29aSAlan Cox * 253394f7e29aSAlan Cox * Note: we would normally expect prot and max to be 253494f7e29aSAlan Cox * VM_PROT_ALL, and cow to be 0. Possibly we should 253594f7e29aSAlan Cox * eliminate these as input parameters, and just 253694f7e29aSAlan Cox * pass these values here in the insert call. 253794f7e29aSAlan Cox */ 253894f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addrbos + max_ssize - init_ssize, 253994f7e29aSAlan Cox addrbos + max_ssize, prot, max, cow); 254094f7e29aSAlan Cox 254194f7e29aSAlan Cox /* Now set the avail_ssize amount */ 254294f7e29aSAlan Cox if (rv == KERN_SUCCESS){ 254329b45e9eSAlan Cox if (prev_entry != &map->header) 254429b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addrbos + max_ssize - init_ssize); 254594f7e29aSAlan Cox new_stack_entry = prev_entry->next; 254694f7e29aSAlan Cox if (new_stack_entry->end != addrbos + max_ssize || 254794f7e29aSAlan Cox new_stack_entry->start != addrbos + max_ssize - init_ssize) 254894f7e29aSAlan Cox panic ("Bad entry start/end for new stack entry"); 254994f7e29aSAlan Cox else 255094f7e29aSAlan Cox new_stack_entry->avail_ssize = max_ssize - init_ssize; 255194f7e29aSAlan Cox } 255294f7e29aSAlan Cox 255394f7e29aSAlan Cox vm_map_unlock(map); 255494f7e29aSAlan Cox return (rv); 255594f7e29aSAlan Cox } 255694f7e29aSAlan Cox 255794f7e29aSAlan Cox /* Attempts to grow a vm stack entry. Returns KERN_SUCCESS if the 255894f7e29aSAlan Cox * desired address is already mapped, or if we successfully grow 255994f7e29aSAlan Cox * the stack. Also returns KERN_SUCCESS if addr is outside the 256094f7e29aSAlan Cox * stack range (this is strange, but preserves compatibility with 256194f7e29aSAlan Cox * the grow function in vm_machdep.c). 256294f7e29aSAlan Cox */ 256394f7e29aSAlan Cox int 256494f7e29aSAlan Cox vm_map_growstack (struct proc *p, vm_offset_t addr) 256594f7e29aSAlan Cox { 256694f7e29aSAlan Cox vm_map_entry_t prev_entry; 256794f7e29aSAlan Cox vm_map_entry_t stack_entry; 256894f7e29aSAlan Cox vm_map_entry_t new_stack_entry; 256994f7e29aSAlan Cox struct vmspace *vm = p->p_vmspace; 257094f7e29aSAlan Cox vm_map_t map = &vm->vm_map; 257194f7e29aSAlan Cox vm_offset_t end; 257294f7e29aSAlan Cox int grow_amount; 257394f7e29aSAlan Cox int rv; 257494f7e29aSAlan Cox int is_procstack; 257523955314SAlfred Perlstein 25760cddd8f0SMatthew Dillon GIANT_REQUIRED; 257723955314SAlfred Perlstein 257894f7e29aSAlan Cox Retry: 257994f7e29aSAlan Cox vm_map_lock_read(map); 258094f7e29aSAlan Cox 258194f7e29aSAlan Cox /* If addr is already in the entry range, no need to grow.*/ 258294f7e29aSAlan Cox if (vm_map_lookup_entry(map, addr, &prev_entry)) { 258394f7e29aSAlan Cox vm_map_unlock_read(map); 25840cddd8f0SMatthew Dillon return (KERN_SUCCESS); 258594f7e29aSAlan Cox } 258694f7e29aSAlan Cox 258794f7e29aSAlan Cox if ((stack_entry = prev_entry->next) == &map->header) { 258894f7e29aSAlan Cox vm_map_unlock_read(map); 25890cddd8f0SMatthew Dillon return (KERN_SUCCESS); 259094f7e29aSAlan Cox } 259194f7e29aSAlan Cox if (prev_entry == &map->header) 259294f7e29aSAlan Cox end = stack_entry->start - stack_entry->avail_ssize; 259394f7e29aSAlan Cox else 259494f7e29aSAlan Cox end = prev_entry->end; 259594f7e29aSAlan Cox 259694f7e29aSAlan Cox /* This next test mimics the old grow function in vm_machdep.c. 259794f7e29aSAlan Cox * It really doesn't quite make sense, but we do it anyway 259894f7e29aSAlan Cox * for compatibility. 259994f7e29aSAlan Cox * 260094f7e29aSAlan Cox * If not growable stack, return success. This signals the 260194f7e29aSAlan Cox * caller to proceed as he would normally with normal vm. 260294f7e29aSAlan Cox */ 260394f7e29aSAlan Cox if (stack_entry->avail_ssize < 1 || 260494f7e29aSAlan Cox addr >= stack_entry->start || 260594f7e29aSAlan Cox addr < stack_entry->start - stack_entry->avail_ssize) { 260694f7e29aSAlan Cox vm_map_unlock_read(map); 26070cddd8f0SMatthew Dillon return (KERN_SUCCESS); 260894f7e29aSAlan Cox } 260994f7e29aSAlan Cox 261094f7e29aSAlan Cox /* Find the minimum grow amount */ 261194f7e29aSAlan Cox grow_amount = roundup (stack_entry->start - addr, PAGE_SIZE); 261294f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 261394f7e29aSAlan Cox vm_map_unlock_read(map); 26140cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 261594f7e29aSAlan Cox } 261694f7e29aSAlan Cox 261794f7e29aSAlan Cox /* If there is no longer enough space between the entries 261894f7e29aSAlan Cox * nogo, and adjust the available space. Note: this 261994f7e29aSAlan Cox * should only happen if the user has mapped into the 262094f7e29aSAlan Cox * stack area after the stack was created, and is 262194f7e29aSAlan Cox * probably an error. 262294f7e29aSAlan Cox * 262394f7e29aSAlan Cox * This also effectively destroys any guard page the user 262494f7e29aSAlan Cox * might have intended by limiting the stack size. 262594f7e29aSAlan Cox */ 262694f7e29aSAlan Cox if (grow_amount > stack_entry->start - end) { 262725adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 262894f7e29aSAlan Cox goto Retry; 262994f7e29aSAlan Cox 263094f7e29aSAlan Cox stack_entry->avail_ssize = stack_entry->start - end; 263194f7e29aSAlan Cox 263294f7e29aSAlan Cox vm_map_unlock(map); 26330cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 263494f7e29aSAlan Cox } 263594f7e29aSAlan Cox 263694f7e29aSAlan Cox is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr; 263794f7e29aSAlan Cox 263894f7e29aSAlan Cox /* If this is the main process stack, see if we're over the 263994f7e29aSAlan Cox * stack limit. 264094f7e29aSAlan Cox */ 26416389da78SAlan Cox if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 264294f7e29aSAlan Cox p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 264394f7e29aSAlan Cox vm_map_unlock_read(map); 26440cddd8f0SMatthew Dillon return (KERN_NO_SPACE); 264594f7e29aSAlan Cox } 264694f7e29aSAlan Cox 264794f7e29aSAlan Cox /* Round up the grow amount modulo SGROWSIZ */ 2648cbc89bfbSPaul Saab grow_amount = roundup (grow_amount, sgrowsiz); 264994f7e29aSAlan Cox if (grow_amount > stack_entry->avail_ssize) { 265094f7e29aSAlan Cox grow_amount = stack_entry->avail_ssize; 265194f7e29aSAlan Cox } 26526389da78SAlan Cox if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > 265394f7e29aSAlan Cox p->p_rlimit[RLIMIT_STACK].rlim_cur)) { 265494f7e29aSAlan Cox grow_amount = p->p_rlimit[RLIMIT_STACK].rlim_cur - 26556389da78SAlan Cox ctob(vm->vm_ssize); 265694f7e29aSAlan Cox } 265794f7e29aSAlan Cox 265825adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 265994f7e29aSAlan Cox goto Retry; 266094f7e29aSAlan Cox 266194f7e29aSAlan Cox /* Get the preliminary new entry start value */ 266294f7e29aSAlan Cox addr = stack_entry->start - grow_amount; 266394f7e29aSAlan Cox 266494f7e29aSAlan Cox /* If this puts us into the previous entry, cut back our growth 266594f7e29aSAlan Cox * to the available space. Also, see the note above. 266694f7e29aSAlan Cox */ 266794f7e29aSAlan Cox if (addr < end) { 266894f7e29aSAlan Cox stack_entry->avail_ssize = stack_entry->start - end; 266994f7e29aSAlan Cox addr = end; 267094f7e29aSAlan Cox } 267194f7e29aSAlan Cox 267294f7e29aSAlan Cox rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start, 267329b45e9eSAlan Cox VM_PROT_ALL, 267429b45e9eSAlan Cox VM_PROT_ALL, 267594f7e29aSAlan Cox 0); 267694f7e29aSAlan Cox 267794f7e29aSAlan Cox /* Adjust the available stack space by the amount we grew. */ 267894f7e29aSAlan Cox if (rv == KERN_SUCCESS) { 267929b45e9eSAlan Cox if (prev_entry != &map->header) 268029b45e9eSAlan Cox vm_map_clip_end(map, prev_entry, addr); 268194f7e29aSAlan Cox new_stack_entry = prev_entry->next; 268294f7e29aSAlan Cox if (new_stack_entry->end != stack_entry->start || 268394f7e29aSAlan Cox new_stack_entry->start != addr) 268494f7e29aSAlan Cox panic ("Bad stack grow start/end in new stack entry"); 268594f7e29aSAlan Cox else { 268694f7e29aSAlan Cox new_stack_entry->avail_ssize = stack_entry->avail_ssize - 268794f7e29aSAlan Cox (new_stack_entry->end - 268894f7e29aSAlan Cox new_stack_entry->start); 268994f7e29aSAlan Cox if (is_procstack) 26906389da78SAlan Cox vm->vm_ssize += btoc(new_stack_entry->end - 26916389da78SAlan Cox new_stack_entry->start); 269294f7e29aSAlan Cox } 269394f7e29aSAlan Cox } 269494f7e29aSAlan Cox 269594f7e29aSAlan Cox vm_map_unlock(map); 26960cddd8f0SMatthew Dillon return (rv); 269794f7e29aSAlan Cox } 269894f7e29aSAlan Cox 2699df8bae1dSRodney W. Grimes /* 27005856e12eSJohn Dyson * Unshare the specified VM space for exec. If other processes are 27015856e12eSJohn Dyson * mapped to it, then create a new one. The new vmspace is null. 27025856e12eSJohn Dyson */ 27035856e12eSJohn Dyson void 27041b40f8c0SMatthew Dillon vmspace_exec(struct proc *p) 27051b40f8c0SMatthew Dillon { 27065856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 27075856e12eSJohn Dyson struct vmspace *newvmspace; 27085856e12eSJohn Dyson vm_map_t map = &p->p_vmspace->vm_map; 27095856e12eSJohn Dyson 27100cddd8f0SMatthew Dillon GIANT_REQUIRED; 27112d8acc0fSJohn Dyson newvmspace = vmspace_alloc(map->min_offset, map->max_offset); 27125856e12eSJohn Dyson bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy, 27135856e12eSJohn Dyson (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy); 27145856e12eSJohn Dyson /* 27155856e12eSJohn Dyson * This code is written like this for prototype purposes. The 27165856e12eSJohn Dyson * goal is to avoid running down the vmspace here, but let the 27175856e12eSJohn Dyson * other process's that are still using the vmspace to finally 27185856e12eSJohn Dyson * run it down. Even though there is little or no chance of blocking 27195856e12eSJohn Dyson * here, it is a good idea to keep this form for future mods. 27205856e12eSJohn Dyson */ 27215856e12eSJohn Dyson p->p_vmspace = newvmspace; 2722d4da2dbaSAlan Cox pmap_pinit2(vmspace_pmap(newvmspace)); 272321c641b2SJohn Baldwin vmspace_free(oldvmspace); 2724b40ce416SJulian Elischer if (p == curthread->td_proc) /* XXXKSE ? */ 2725b40ce416SJulian Elischer pmap_activate(curthread); 27265856e12eSJohn Dyson } 27275856e12eSJohn Dyson 27285856e12eSJohn Dyson /* 27295856e12eSJohn Dyson * Unshare the specified VM space for forcing COW. This 27305856e12eSJohn Dyson * is called by rfork, for the (RFMEM|RFPROC) == 0 case. 27315856e12eSJohn Dyson */ 27325856e12eSJohn Dyson void 27331b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p) 27341b40f8c0SMatthew Dillon { 27355856e12eSJohn Dyson struct vmspace *oldvmspace = p->p_vmspace; 27365856e12eSJohn Dyson struct vmspace *newvmspace; 27375856e12eSJohn Dyson 27380cddd8f0SMatthew Dillon GIANT_REQUIRED; 27395856e12eSJohn Dyson if (oldvmspace->vm_refcnt == 1) 27405856e12eSJohn Dyson return; 27415856e12eSJohn Dyson newvmspace = vmspace_fork(oldvmspace); 27425856e12eSJohn Dyson p->p_vmspace = newvmspace; 2743d4da2dbaSAlan Cox pmap_pinit2(vmspace_pmap(newvmspace)); 274421c641b2SJohn Baldwin vmspace_free(oldvmspace); 2745b40ce416SJulian Elischer if (p == curthread->td_proc) /* XXXKSE ? */ 2746b40ce416SJulian Elischer pmap_activate(curthread); 27475856e12eSJohn Dyson } 27485856e12eSJohn Dyson 27495856e12eSJohn Dyson /* 2750df8bae1dSRodney W. Grimes * vm_map_lookup: 2751df8bae1dSRodney W. Grimes * 2752df8bae1dSRodney W. Grimes * Finds the VM object, offset, and 2753df8bae1dSRodney W. Grimes * protection for a given virtual address in the 2754df8bae1dSRodney W. Grimes * specified map, assuming a page fault of the 2755df8bae1dSRodney W. Grimes * type specified. 2756df8bae1dSRodney W. Grimes * 2757df8bae1dSRodney W. Grimes * Leaves the map in question locked for read; return 2758df8bae1dSRodney W. Grimes * values are guaranteed until a vm_map_lookup_done 2759df8bae1dSRodney W. Grimes * call is performed. Note that the map argument 2760df8bae1dSRodney W. Grimes * is in/out; the returned map must be used in 2761df8bae1dSRodney W. Grimes * the call to vm_map_lookup_done. 2762df8bae1dSRodney W. Grimes * 2763df8bae1dSRodney W. Grimes * A handle (out_entry) is returned for use in 2764df8bae1dSRodney W. Grimes * vm_map_lookup_done, to make that fast. 2765df8bae1dSRodney W. Grimes * 2766df8bae1dSRodney W. Grimes * If a lookup is requested with "write protection" 2767df8bae1dSRodney W. Grimes * specified, the map may be changed to perform virtual 2768df8bae1dSRodney W. Grimes * copying operations, although the data referenced will 2769df8bae1dSRodney W. Grimes * remain the same. 2770df8bae1dSRodney W. Grimes */ 2771df8bae1dSRodney W. Grimes int 2772b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map, /* IN/OUT */ 2773b9dcd593SBruce Evans vm_offset_t vaddr, 277447221757SJohn Dyson vm_prot_t fault_typea, 2775b9dcd593SBruce Evans vm_map_entry_t *out_entry, /* OUT */ 2776b9dcd593SBruce Evans vm_object_t *object, /* OUT */ 2777b9dcd593SBruce Evans vm_pindex_t *pindex, /* OUT */ 2778b9dcd593SBruce Evans vm_prot_t *out_prot, /* OUT */ 27792d8acc0fSJohn Dyson boolean_t *wired) /* OUT */ 2780df8bae1dSRodney W. Grimes { 2781c0877f10SJohn Dyson vm_map_entry_t entry; 2782c0877f10SJohn Dyson vm_map_t map = *var_map; 2783c0877f10SJohn Dyson vm_prot_t prot; 278447221757SJohn Dyson vm_prot_t fault_type = fault_typea; 2785df8bae1dSRodney W. Grimes 27860cddd8f0SMatthew Dillon GIANT_REQUIRED; 2787df8bae1dSRodney W. Grimes RetryLookup:; 2788df8bae1dSRodney W. Grimes /* 2789df8bae1dSRodney W. Grimes * Lookup the faulting address. 2790df8bae1dSRodney W. Grimes */ 2791df8bae1dSRodney W. Grimes 2792df8bae1dSRodney W. Grimes vm_map_lock_read(map); 2793df8bae1dSRodney W. Grimes #define RETURN(why) \ 2794df8bae1dSRodney W. Grimes { \ 2795df8bae1dSRodney W. Grimes vm_map_unlock_read(map); \ 2796df8bae1dSRodney W. Grimes return (why); \ 2797df8bae1dSRodney W. Grimes } 2798df8bae1dSRodney W. Grimes 2799df8bae1dSRodney W. Grimes /* 28000d94caffSDavid Greenman * If the map has an interesting hint, try it before calling full 28010d94caffSDavid Greenman * blown lookup routine. 2802df8bae1dSRodney W. Grimes */ 2803df8bae1dSRodney W. Grimes entry = map->hint; 2804df8bae1dSRodney W. Grimes *out_entry = entry; 2805df8bae1dSRodney W. Grimes if ((entry == &map->header) || 2806df8bae1dSRodney W. Grimes (vaddr < entry->start) || (vaddr >= entry->end)) { 2807df8bae1dSRodney W. Grimes vm_map_entry_t tmp_entry; 2808df8bae1dSRodney W. Grimes 2809df8bae1dSRodney W. Grimes /* 28100d94caffSDavid Greenman * Entry was either not a valid hint, or the vaddr was not 28110d94caffSDavid Greenman * contained in the entry, so do a full lookup. 2812df8bae1dSRodney W. Grimes */ 2813df8bae1dSRodney W. Grimes if (!vm_map_lookup_entry(map, vaddr, &tmp_entry)) 2814df8bae1dSRodney W. Grimes RETURN(KERN_INVALID_ADDRESS); 2815df8bae1dSRodney W. Grimes 2816df8bae1dSRodney W. Grimes entry = tmp_entry; 2817df8bae1dSRodney W. Grimes *out_entry = entry; 2818df8bae1dSRodney W. Grimes } 2819b7b2aac2SJohn Dyson 2820df8bae1dSRodney W. Grimes /* 2821df8bae1dSRodney W. Grimes * Handle submaps. 2822df8bae1dSRodney W. Grimes */ 2823afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 2824df8bae1dSRodney W. Grimes vm_map_t old_map = map; 2825df8bae1dSRodney W. Grimes 2826df8bae1dSRodney W. Grimes *var_map = map = entry->object.sub_map; 2827df8bae1dSRodney W. Grimes vm_map_unlock_read(old_map); 2828df8bae1dSRodney W. Grimes goto RetryLookup; 2829df8bae1dSRodney W. Grimes } 2830a04c970aSJohn Dyson 2831df8bae1dSRodney W. Grimes /* 28320d94caffSDavid Greenman * Check whether this task is allowed to have this page. 2833a04c970aSJohn Dyson * Note the special case for MAP_ENTRY_COW 2834a04c970aSJohn Dyson * pages with an override. This is to implement a forced 2835a04c970aSJohn Dyson * COW for debuggers. 2836df8bae1dSRodney W. Grimes */ 2837480ba2f5SJohn Dyson if (fault_type & VM_PROT_OVERRIDE_WRITE) 2838480ba2f5SJohn Dyson prot = entry->max_protection; 2839480ba2f5SJohn Dyson else 2840df8bae1dSRodney W. Grimes prot = entry->protection; 284147221757SJohn Dyson fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE); 284247221757SJohn Dyson if ((fault_type & prot) != fault_type) { 284347221757SJohn Dyson RETURN(KERN_PROTECTION_FAILURE); 284447221757SJohn Dyson } 28452ed14a92SAlan Cox if ((entry->eflags & MAP_ENTRY_USER_WIRED) && 284647221757SJohn Dyson (entry->eflags & MAP_ENTRY_COW) && 28472ed14a92SAlan Cox (fault_type & VM_PROT_WRITE) && 284847221757SJohn Dyson (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) { 2849df8bae1dSRodney W. Grimes RETURN(KERN_PROTECTION_FAILURE); 2850a04c970aSJohn Dyson } 2851df8bae1dSRodney W. Grimes 2852df8bae1dSRodney W. Grimes /* 28530d94caffSDavid Greenman * If this page is not pageable, we have to get it for all possible 28540d94caffSDavid Greenman * accesses. 2855df8bae1dSRodney W. Grimes */ 285605f0fdd2SPoul-Henning Kamp *wired = (entry->wired_count != 0); 285705f0fdd2SPoul-Henning Kamp if (*wired) 2858df8bae1dSRodney W. Grimes prot = fault_type = entry->protection; 2859df8bae1dSRodney W. Grimes 2860df8bae1dSRodney W. Grimes /* 2861df8bae1dSRodney W. Grimes * If the entry was copy-on-write, we either ... 2862df8bae1dSRodney W. Grimes */ 2863afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_NEEDS_COPY) { 2864df8bae1dSRodney W. Grimes /* 28650d94caffSDavid Greenman * If we want to write the page, we may as well handle that 2866ad5fca3bSAlan Cox * now since we've got the map locked. 2867df8bae1dSRodney W. Grimes * 28680d94caffSDavid Greenman * If we don't need to write the page, we just demote the 28690d94caffSDavid Greenman * permissions allowed. 2870df8bae1dSRodney W. Grimes */ 2871df8bae1dSRodney W. Grimes if (fault_type & VM_PROT_WRITE) { 2872df8bae1dSRodney W. Grimes /* 28730d94caffSDavid Greenman * Make a new object, and place it in the object 28740d94caffSDavid Greenman * chain. Note that no new references have appeared 2875ad5fca3bSAlan Cox * -- one just moved from the map to the new 28760d94caffSDavid Greenman * object. 2877df8bae1dSRodney W. Grimes */ 287825adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 2879df8bae1dSRodney W. Grimes goto RetryLookup; 2880df8bae1dSRodney W. Grimes vm_object_shadow( 2881df8bae1dSRodney W. Grimes &entry->object.vm_object, 2882df8bae1dSRodney W. Grimes &entry->offset, 2883c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2884afa07f7eSJohn Dyson entry->eflags &= ~MAP_ENTRY_NEEDS_COPY; 28859b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 28860d94caffSDavid Greenman } else { 2887df8bae1dSRodney W. Grimes /* 28880d94caffSDavid Greenman * We're attempting to read a copy-on-write page -- 28890d94caffSDavid Greenman * don't allow writes. 2890df8bae1dSRodney W. Grimes */ 28912d8acc0fSJohn Dyson prot &= ~VM_PROT_WRITE; 2892df8bae1dSRodney W. Grimes } 2893df8bae1dSRodney W. Grimes } 28942d8acc0fSJohn Dyson 2895df8bae1dSRodney W. Grimes /* 2896df8bae1dSRodney W. Grimes * Create an object if necessary. 2897df8bae1dSRodney W. Grimes */ 28984e71e795SMatthew Dillon if (entry->object.vm_object == NULL && 28994e71e795SMatthew Dillon !map->system_map) { 290025adb370SBrian Feldman if (vm_map_lock_upgrade(map)) 2901df8bae1dSRodney W. Grimes goto RetryLookup; 290224a1cce3SDavid Greenman entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT, 2903c2e11a03SJohn Dyson atop(entry->end - entry->start)); 2904df8bae1dSRodney W. Grimes entry->offset = 0; 29059b09b6c7SMatthew Dillon vm_map_lock_downgrade(map); 2906df8bae1dSRodney W. Grimes } 2907b5b40fa6SJohn Dyson 2908df8bae1dSRodney W. Grimes /* 29090d94caffSDavid Greenman * Return the object/offset from this entry. If the entry was 29100d94caffSDavid Greenman * copy-on-write or empty, it has been fixed up. 2911df8bae1dSRodney W. Grimes */ 29129b09b6c7SMatthew Dillon *pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset); 2913df8bae1dSRodney W. Grimes *object = entry->object.vm_object; 2914df8bae1dSRodney W. Grimes 2915df8bae1dSRodney W. Grimes /* 2916df8bae1dSRodney W. Grimes * Return whether this is the only map sharing this data. 2917df8bae1dSRodney W. Grimes */ 2918df8bae1dSRodney W. Grimes *out_prot = prot; 2919df8bae1dSRodney W. Grimes return (KERN_SUCCESS); 2920df8bae1dSRodney W. Grimes 2921df8bae1dSRodney W. Grimes #undef RETURN 2922df8bae1dSRodney W. Grimes } 2923df8bae1dSRodney W. Grimes 2924df8bae1dSRodney W. Grimes /* 2925df8bae1dSRodney W. Grimes * vm_map_lookup_done: 2926df8bae1dSRodney W. Grimes * 2927df8bae1dSRodney W. Grimes * Releases locks acquired by a vm_map_lookup 2928df8bae1dSRodney W. Grimes * (according to the handle returned by that lookup). 2929df8bae1dSRodney W. Grimes */ 29300d94caffSDavid Greenman void 29311b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry) 2932df8bae1dSRodney W. Grimes { 2933df8bae1dSRodney W. Grimes /* 2934df8bae1dSRodney W. Grimes * Unlock the main-level map 2935df8bae1dSRodney W. Grimes */ 29360cddd8f0SMatthew Dillon GIANT_REQUIRED; 2937df8bae1dSRodney W. Grimes vm_map_unlock_read(map); 2938df8bae1dSRodney W. Grimes } 2939df8bae1dSRodney W. Grimes 29401efb74fbSJohn Dyson /* 29411efb74fbSJohn Dyson * Implement uiomove with VM operations. This handles (and collateral changes) 29421efb74fbSJohn Dyson * support every combination of source object modification, and COW type 29431efb74fbSJohn Dyson * operations. 29441efb74fbSJohn Dyson */ 29451efb74fbSJohn Dyson int 29461b40f8c0SMatthew Dillon vm_uiomove( 29471b40f8c0SMatthew Dillon vm_map_t mapa, 29481b40f8c0SMatthew Dillon vm_object_t srcobject, 29491b40f8c0SMatthew Dillon off_t cp, 29501b40f8c0SMatthew Dillon int cnta, 29511b40f8c0SMatthew Dillon vm_offset_t uaddra, 29521b40f8c0SMatthew Dillon int *npages) 29531efb74fbSJohn Dyson { 29541efb74fbSJohn Dyson vm_map_t map; 295547221757SJohn Dyson vm_object_t first_object, oldobject, object; 29562d8acc0fSJohn Dyson vm_map_entry_t entry; 29571efb74fbSJohn Dyson vm_prot_t prot; 29582d8acc0fSJohn Dyson boolean_t wired; 29591efb74fbSJohn Dyson int tcnt, rv; 29602d8acc0fSJohn Dyson vm_offset_t uaddr, start, end, tend; 29611efb74fbSJohn Dyson vm_pindex_t first_pindex, osize, oindex; 29621efb74fbSJohn Dyson off_t ooffset; 296347221757SJohn Dyson int cnt; 29641efb74fbSJohn Dyson 29650cddd8f0SMatthew Dillon GIANT_REQUIRED; 29660cddd8f0SMatthew Dillon 296795e5e988SJohn Dyson if (npages) 296895e5e988SJohn Dyson *npages = 0; 296995e5e988SJohn Dyson 297047221757SJohn Dyson cnt = cnta; 29712d8acc0fSJohn Dyson uaddr = uaddra; 29722d8acc0fSJohn Dyson 29731efb74fbSJohn Dyson while (cnt > 0) { 29741efb74fbSJohn Dyson map = mapa; 29751efb74fbSJohn Dyson 29761efb74fbSJohn Dyson if ((vm_map_lookup(&map, uaddr, 29772d8acc0fSJohn Dyson VM_PROT_READ, &entry, &first_object, 29782d8acc0fSJohn Dyson &first_pindex, &prot, &wired)) != KERN_SUCCESS) { 29791efb74fbSJohn Dyson return EFAULT; 29801efb74fbSJohn Dyson } 29811efb74fbSJohn Dyson 29822d8acc0fSJohn Dyson vm_map_clip_start(map, entry, uaddr); 29831efb74fbSJohn Dyson 29841efb74fbSJohn Dyson tcnt = cnt; 29852d8acc0fSJohn Dyson tend = uaddr + tcnt; 29862d8acc0fSJohn Dyson if (tend > entry->end) { 29872d8acc0fSJohn Dyson tcnt = entry->end - uaddr; 29882d8acc0fSJohn Dyson tend = entry->end; 29892d8acc0fSJohn Dyson } 29901efb74fbSJohn Dyson 29912d8acc0fSJohn Dyson vm_map_clip_end(map, entry, tend); 29921efb74fbSJohn Dyson 29932d8acc0fSJohn Dyson start = entry->start; 29942d8acc0fSJohn Dyson end = entry->end; 29951efb74fbSJohn Dyson 2996c2e11a03SJohn Dyson osize = atop(tcnt); 299795e5e988SJohn Dyson 2998925a3a41SJohn Dyson oindex = OFF_TO_IDX(cp); 299995e5e988SJohn Dyson if (npages) { 3000925a3a41SJohn Dyson vm_pindex_t idx; 300195e5e988SJohn Dyson for (idx = 0; idx < osize; idx++) { 300295e5e988SJohn Dyson vm_page_t m; 3003925a3a41SJohn Dyson if ((m = vm_page_lookup(srcobject, oindex + idx)) == NULL) { 30042d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 300595e5e988SJohn Dyson return 0; 300695e5e988SJohn Dyson } 30071c7c3c6aSMatthew Dillon /* 30081c7c3c6aSMatthew Dillon * disallow busy or invalid pages, but allow 30091c7c3c6aSMatthew Dillon * m->busy pages if they are entirely valid. 30101c7c3c6aSMatthew Dillon */ 3011925a3a41SJohn Dyson if ((m->flags & PG_BUSY) || 301295e5e988SJohn Dyson ((m->valid & VM_PAGE_BITS_ALL) != VM_PAGE_BITS_ALL)) { 30132d8acc0fSJohn Dyson vm_map_lookup_done(map, entry); 301495e5e988SJohn Dyson return 0; 301595e5e988SJohn Dyson } 301695e5e988SJohn Dyson } 301795e5e988SJohn Dyson } 301895e5e988SJohn Dyson 30191efb74fbSJohn Dyson /* 30201efb74fbSJohn Dyson * If we are changing an existing map entry, just redirect 30211efb74fbSJohn Dyson * the object, and change mappings. 30221efb74fbSJohn Dyson */ 30232d8acc0fSJohn Dyson if ((first_object->type == OBJT_VNODE) && 30242d8acc0fSJohn Dyson ((oldobject = entry->object.vm_object) == first_object)) { 30252d8acc0fSJohn Dyson 30262d8acc0fSJohn Dyson if ((entry->offset != cp) || (oldobject != srcobject)) { 30272d8acc0fSJohn Dyson /* 30282d8acc0fSJohn Dyson * Remove old window into the file 30292d8acc0fSJohn Dyson */ 30302d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 30312d8acc0fSJohn Dyson 30322d8acc0fSJohn Dyson /* 30332d8acc0fSJohn Dyson * Force copy on write for mmaped regions 30342d8acc0fSJohn Dyson */ 30352d8acc0fSJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 30362d8acc0fSJohn Dyson 30372d8acc0fSJohn Dyson /* 30382d8acc0fSJohn Dyson * Point the object appropriately 30392d8acc0fSJohn Dyson */ 30402d8acc0fSJohn Dyson if (oldobject != srcobject) { 30412d8acc0fSJohn Dyson 30422d8acc0fSJohn Dyson /* 30432d8acc0fSJohn Dyson * Set the object optimization hint flag 30442d8acc0fSJohn Dyson */ 3045069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 30462d8acc0fSJohn Dyson vm_object_reference(srcobject); 30472d8acc0fSJohn Dyson entry->object.vm_object = srcobject; 30482d8acc0fSJohn Dyson 30492d8acc0fSJohn Dyson if (oldobject) { 30502d8acc0fSJohn Dyson vm_object_deallocate(oldobject); 30512d8acc0fSJohn Dyson } 30522d8acc0fSJohn Dyson } 30532d8acc0fSJohn Dyson 30542d8acc0fSJohn Dyson entry->offset = cp; 30552d8acc0fSJohn Dyson map->timestamp++; 30562d8acc0fSJohn Dyson } else { 30572d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 30582d8acc0fSJohn Dyson } 30592d8acc0fSJohn Dyson 30602d8acc0fSJohn Dyson } else if ((first_object->ref_count == 1) && 3061925a3a41SJohn Dyson (first_object->size == osize) && 306247221757SJohn Dyson ((first_object->type == OBJT_DEFAULT) || 306347221757SJohn Dyson (first_object->type == OBJT_SWAP)) ) { 3064925a3a41SJohn Dyson 3065925a3a41SJohn Dyson oldobject = first_object->backing_object; 3066925a3a41SJohn Dyson 3067925a3a41SJohn Dyson if ((first_object->backing_object_offset != cp) || 3068925a3a41SJohn Dyson (oldobject != srcobject)) { 3069925a3a41SJohn Dyson /* 3070925a3a41SJohn Dyson * Remove old window into the file 3071925a3a41SJohn Dyson */ 30722d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 3073925a3a41SJohn Dyson 3074925a3a41SJohn Dyson /* 307547221757SJohn Dyson * Remove unneeded old pages 307647221757SJohn Dyson */ 307747221757SJohn Dyson vm_object_page_remove(first_object, 0, 0, 0); 307847221757SJohn Dyson 307947221757SJohn Dyson /* 308047221757SJohn Dyson * Invalidate swap space 308147221757SJohn Dyson */ 308247221757SJohn Dyson if (first_object->type == OBJT_SWAP) { 308347221757SJohn Dyson swap_pager_freespace(first_object, 30841c7c3c6aSMatthew Dillon 0, 308547221757SJohn Dyson first_object->size); 308647221757SJohn Dyson } 308747221757SJohn Dyson 308847221757SJohn Dyson /* 3089925a3a41SJohn Dyson * Force copy on write for mmaped regions 3090925a3a41SJohn Dyson */ 309147221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 30921efb74fbSJohn Dyson 30931efb74fbSJohn Dyson /* 30941efb74fbSJohn Dyson * Point the object appropriately 30951efb74fbSJohn Dyson */ 3096925a3a41SJohn Dyson if (oldobject != srcobject) { 3097925a3a41SJohn Dyson /* 3098925a3a41SJohn Dyson * Set the object optimization hint flag 3099925a3a41SJohn Dyson */ 3100069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 3101925a3a41SJohn Dyson vm_object_reference(srcobject); 3102925a3a41SJohn Dyson 3103925a3a41SJohn Dyson if (oldobject) { 3104925a3a41SJohn Dyson TAILQ_REMOVE(&oldobject->shadow_head, 3105925a3a41SJohn Dyson first_object, shadow_list); 3106925a3a41SJohn Dyson oldobject->shadow_count--; 3107b4309055SMatthew Dillon /* XXX bump generation? */ 3108925a3a41SJohn Dyson vm_object_deallocate(oldobject); 3109925a3a41SJohn Dyson } 3110925a3a41SJohn Dyson 3111925a3a41SJohn Dyson TAILQ_INSERT_TAIL(&srcobject->shadow_head, 3112925a3a41SJohn Dyson first_object, shadow_list); 3113925a3a41SJohn Dyson srcobject->shadow_count++; 3114b4309055SMatthew Dillon /* XXX bump generation? */ 3115925a3a41SJohn Dyson 3116925a3a41SJohn Dyson first_object->backing_object = srcobject; 3117925a3a41SJohn Dyson } 31181efb74fbSJohn Dyson first_object->backing_object_offset = cp; 31192d8acc0fSJohn Dyson map->timestamp++; 3120925a3a41SJohn Dyson } else { 31212d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 3122925a3a41SJohn Dyson } 31231efb74fbSJohn Dyson /* 31241efb74fbSJohn Dyson * Otherwise, we have to do a logical mmap. 31251efb74fbSJohn Dyson */ 31261efb74fbSJohn Dyson } else { 31271efb74fbSJohn Dyson 3128069e9bc1SDoug Rabson vm_object_set_flag(srcobject, OBJ_OPT); 3129925a3a41SJohn Dyson vm_object_reference(srcobject); 31301efb74fbSJohn Dyson 31312d8acc0fSJohn Dyson pmap_remove (map->pmap, uaddr, tend); 31321efb74fbSJohn Dyson 313347221757SJohn Dyson vm_object_pmap_copy_1 (srcobject, oindex, oindex + osize); 313425adb370SBrian Feldman vm_map_lock_upgrade(map); 31351efb74fbSJohn Dyson 31362d8acc0fSJohn Dyson if (entry == &map->header) { 31371efb74fbSJohn Dyson map->first_free = &map->header; 31381efb74fbSJohn Dyson } else if (map->first_free->start >= start) { 31392d8acc0fSJohn Dyson map->first_free = entry->prev; 31401efb74fbSJohn Dyson } 31411efb74fbSJohn Dyson 31422d8acc0fSJohn Dyson SAVE_HINT(map, entry->prev); 31432d8acc0fSJohn Dyson vm_map_entry_delete(map, entry); 31441efb74fbSJohn Dyson 31452d8acc0fSJohn Dyson object = srcobject; 31462d8acc0fSJohn Dyson ooffset = cp; 31472d8acc0fSJohn Dyson 31482d8acc0fSJohn Dyson rv = vm_map_insert(map, object, ooffset, start, tend, 3149e5f13bddSAlan Cox VM_PROT_ALL, VM_PROT_ALL, MAP_COPY_ON_WRITE); 31501efb74fbSJohn Dyson 31511efb74fbSJohn Dyson if (rv != KERN_SUCCESS) 31521efb74fbSJohn Dyson panic("vm_uiomove: could not insert new entry: %d", rv); 31531efb74fbSJohn Dyson } 31541efb74fbSJohn Dyson 31551efb74fbSJohn Dyson /* 31561efb74fbSJohn Dyson * Map the window directly, if it is already in memory 31571efb74fbSJohn Dyson */ 31582d8acc0fSJohn Dyson pmap_object_init_pt(map->pmap, uaddr, 31592d8acc0fSJohn Dyson srcobject, oindex, tcnt, 0); 31601efb74fbSJohn Dyson 316147221757SJohn Dyson map->timestamp++; 31621efb74fbSJohn Dyson vm_map_unlock(map); 31631efb74fbSJohn Dyson 31641efb74fbSJohn Dyson cnt -= tcnt; 31652d8acc0fSJohn Dyson uaddr += tcnt; 31661efb74fbSJohn Dyson cp += tcnt; 316795e5e988SJohn Dyson if (npages) 316895e5e988SJohn Dyson *npages += osize; 31691efb74fbSJohn Dyson } 31701efb74fbSJohn Dyson return 0; 31711efb74fbSJohn Dyson } 31721efb74fbSJohn Dyson 31731efb74fbSJohn Dyson /* 31741efb74fbSJohn Dyson * Performs the copy_on_write operations necessary to allow the virtual copies 31751efb74fbSJohn Dyson * into user space to work. This has to be called for write(2) system calls 31761efb74fbSJohn Dyson * from other processes, file unlinking, and file size shrinkage. 31771efb74fbSJohn Dyson */ 31781efb74fbSJohn Dyson void 31791b40f8c0SMatthew Dillon vm_freeze_copyopts(vm_object_t object, vm_pindex_t froma, vm_pindex_t toa) 31801efb74fbSJohn Dyson { 3181f5ef029eSPoul-Henning Kamp int rv; 3182f5ef029eSPoul-Henning Kamp vm_object_t robject; 3183f5ef029eSPoul-Henning Kamp vm_pindex_t idx; 31841efb74fbSJohn Dyson 31850cddd8f0SMatthew Dillon GIANT_REQUIRED; 31862d8acc0fSJohn Dyson if ((object == NULL) || 318795e5e988SJohn Dyson ((object->flags & OBJ_OPT) == 0)) 318895e5e988SJohn Dyson return; 31891efb74fbSJohn Dyson 31901efb74fbSJohn Dyson if (object->shadow_count > object->ref_count) 31911efb74fbSJohn Dyson panic("vm_freeze_copyopts: sc > rc"); 31921efb74fbSJohn Dyson 31938aef1712SMatthew Dillon while ((robject = TAILQ_FIRST(&object->shadow_head)) != NULL) { 31941efb74fbSJohn Dyson vm_pindex_t bo_pindex; 31951efb74fbSJohn Dyson vm_page_t m_in, m_out; 31961efb74fbSJohn Dyson 31971efb74fbSJohn Dyson bo_pindex = OFF_TO_IDX(robject->backing_object_offset); 31981efb74fbSJohn Dyson 319995e5e988SJohn Dyson vm_object_reference(robject); 3200925a3a41SJohn Dyson 320166095752SJohn Dyson vm_object_pip_wait(robject, "objfrz"); 3202925a3a41SJohn Dyson 32031efb74fbSJohn Dyson if (robject->ref_count == 1) { 32041efb74fbSJohn Dyson vm_object_deallocate(robject); 32051efb74fbSJohn Dyson continue; 32061efb74fbSJohn Dyson } 32071efb74fbSJohn Dyson 3208d474eaaaSDoug Rabson vm_object_pip_add(robject, 1); 32091efb74fbSJohn Dyson 321047221757SJohn Dyson for (idx = 0; idx < robject->size; idx++) { 32111efb74fbSJohn Dyson 321295461b45SJohn Dyson m_out = vm_page_grab(robject, idx, 321395461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 32141efb74fbSJohn Dyson 32151efb74fbSJohn Dyson if (m_out->valid == 0) { 321695461b45SJohn Dyson m_in = vm_page_grab(object, bo_pindex + idx, 321795461b45SJohn Dyson VM_ALLOC_NORMAL | VM_ALLOC_RETRY); 321895461b45SJohn Dyson if (m_in->valid == 0) { 321947221757SJohn Dyson rv = vm_pager_get_pages(object, &m_in, 1, 0); 322047221757SJohn Dyson if (rv != VM_PAGER_OK) { 32213efc015bSPeter Wemm printf("vm_freeze_copyopts: cannot read page from file: %lx\n", (long)m_in->pindex); 322247221757SJohn Dyson continue; 32231efb74fbSJohn Dyson } 322495461b45SJohn Dyson vm_page_deactivate(m_in); 322547221757SJohn Dyson } 322647221757SJohn Dyson 322747221757SJohn Dyson vm_page_protect(m_in, VM_PROT_NONE); 32281a87a0daSPeter Wemm pmap_copy_page(m_in, m_out); 322995461b45SJohn Dyson m_out->valid = m_in->valid; 32307dbf82dcSMatthew Dillon vm_page_dirty(m_out); 323195461b45SJohn Dyson vm_page_activate(m_out); 3232e69763a3SDoug Rabson vm_page_wakeup(m_in); 32331efb74fbSJohn Dyson } 3234e69763a3SDoug Rabson vm_page_wakeup(m_out); 323547221757SJohn Dyson } 3236925a3a41SJohn Dyson 32371efb74fbSJohn Dyson object->shadow_count--; 323847221757SJohn Dyson object->ref_count--; 32391efb74fbSJohn Dyson TAILQ_REMOVE(&object->shadow_head, robject, shadow_list); 32401efb74fbSJohn Dyson robject->backing_object = NULL; 32411efb74fbSJohn Dyson robject->backing_object_offset = 0; 32421efb74fbSJohn Dyson 324347221757SJohn Dyson vm_object_pip_wakeup(robject); 32441efb74fbSJohn Dyson vm_object_deallocate(robject); 32451efb74fbSJohn Dyson } 324647221757SJohn Dyson 3247069e9bc1SDoug Rabson vm_object_clear_flag(object, OBJ_OPT); 32481efb74fbSJohn Dyson } 32491efb74fbSJohn Dyson 3250c7c34a24SBruce Evans #include "opt_ddb.h" 3251c3cb3e12SDavid Greenman #ifdef DDB 3252c7c34a24SBruce Evans #include <sys/kernel.h> 3253c7c34a24SBruce Evans 3254c7c34a24SBruce Evans #include <ddb/ddb.h> 3255c7c34a24SBruce Evans 3256df8bae1dSRodney W. Grimes /* 3257df8bae1dSRodney W. Grimes * vm_map_print: [ debug ] 3258df8bae1dSRodney W. Grimes */ 3259c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print) 3260df8bae1dSRodney W. Grimes { 326195e5e988SJohn Dyson static int nlines; 3262c7c34a24SBruce Evans /* XXX convert args. */ 3263c0877f10SJohn Dyson vm_map_t map = (vm_map_t)addr; 3264c7c34a24SBruce Evans boolean_t full = have_addr; 3265df8bae1dSRodney W. Grimes 3266c0877f10SJohn Dyson vm_map_entry_t entry; 3267c7c34a24SBruce Evans 3268e5f251d2SAlan Cox db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n", 3269e5f251d2SAlan Cox (void *)map, 3270101eeb7fSBruce Evans (void *)map->pmap, map->nentries, map->timestamp); 327195e5e988SJohn Dyson nlines++; 3272df8bae1dSRodney W. Grimes 3273c7c34a24SBruce Evans if (!full && db_indent) 3274df8bae1dSRodney W. Grimes return; 3275df8bae1dSRodney W. Grimes 3276c7c34a24SBruce Evans db_indent += 2; 3277df8bae1dSRodney W. Grimes for (entry = map->header.next; entry != &map->header; 3278df8bae1dSRodney W. Grimes entry = entry->next) { 3279fc62ef1fSBruce Evans db_iprintf("map entry %p: start=%p, end=%p\n", 3280fc62ef1fSBruce Evans (void *)entry, (void *)entry->start, (void *)entry->end); 328195e5e988SJohn Dyson nlines++; 3282e5f251d2SAlan Cox { 3283df8bae1dSRodney W. Grimes static char *inheritance_name[4] = 3284df8bae1dSRodney W. Grimes {"share", "copy", "none", "donate_copy"}; 32850d94caffSDavid Greenman 328695e5e988SJohn Dyson db_iprintf(" prot=%x/%x/%s", 3287df8bae1dSRodney W. Grimes entry->protection, 3288df8bae1dSRodney W. Grimes entry->max_protection, 32898aef1712SMatthew Dillon inheritance_name[(int)(unsigned char)entry->inheritance]); 3290df8bae1dSRodney W. Grimes if (entry->wired_count != 0) 329195e5e988SJohn Dyson db_printf(", wired"); 3292df8bae1dSRodney W. Grimes } 32939fdfe602SMatthew Dillon if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) { 3294101eeb7fSBruce Evans /* XXX no %qd in kernel. Truncate entry->offset. */ 3295101eeb7fSBruce Evans db_printf(", share=%p, offset=0x%lx\n", 32969fdfe602SMatthew Dillon (void *)entry->object.sub_map, 3297ecbb00a2SDoug Rabson (long)entry->offset); 329895e5e988SJohn Dyson nlines++; 3299df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 33009fdfe602SMatthew Dillon (entry->prev->object.sub_map != 33019fdfe602SMatthew Dillon entry->object.sub_map)) { 3302c7c34a24SBruce Evans db_indent += 2; 3303101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t) 33049fdfe602SMatthew Dillon entry->object.sub_map, 3305914181e7SBruce Evans full, 0, (char *)0); 3306c7c34a24SBruce Evans db_indent -= 2; 3307df8bae1dSRodney W. Grimes } 33080d94caffSDavid Greenman } else { 3309101eeb7fSBruce Evans /* XXX no %qd in kernel. Truncate entry->offset. */ 3310101eeb7fSBruce Evans db_printf(", object=%p, offset=0x%lx", 3311101eeb7fSBruce Evans (void *)entry->object.vm_object, 3312ecbb00a2SDoug Rabson (long)entry->offset); 3313afa07f7eSJohn Dyson if (entry->eflags & MAP_ENTRY_COW) 3314c7c34a24SBruce Evans db_printf(", copy (%s)", 3315afa07f7eSJohn Dyson (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done"); 3316c7c34a24SBruce Evans db_printf("\n"); 331795e5e988SJohn Dyson nlines++; 3318df8bae1dSRodney W. Grimes 3319df8bae1dSRodney W. Grimes if ((entry->prev == &map->header) || 3320df8bae1dSRodney W. Grimes (entry->prev->object.vm_object != 3321df8bae1dSRodney W. Grimes entry->object.vm_object)) { 3322c7c34a24SBruce Evans db_indent += 2; 3323101eeb7fSBruce Evans vm_object_print((db_expr_t)(intptr_t) 3324101eeb7fSBruce Evans entry->object.vm_object, 3325914181e7SBruce Evans full, 0, (char *)0); 332695e5e988SJohn Dyson nlines += 4; 3327c7c34a24SBruce Evans db_indent -= 2; 3328df8bae1dSRodney W. Grimes } 3329df8bae1dSRodney W. Grimes } 3330df8bae1dSRodney W. Grimes } 3331c7c34a24SBruce Evans db_indent -= 2; 333295e5e988SJohn Dyson if (db_indent == 0) 333395e5e988SJohn Dyson nlines = 0; 3334df8bae1dSRodney W. Grimes } 333595e5e988SJohn Dyson 333695e5e988SJohn Dyson 333795e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm) 333895e5e988SJohn Dyson { 333995e5e988SJohn Dyson struct proc *p; 334095e5e988SJohn Dyson 334195e5e988SJohn Dyson if (have_addr) { 334295e5e988SJohn Dyson p = (struct proc *) addr; 334395e5e988SJohn Dyson } else { 334495e5e988SJohn Dyson p = curproc; 334595e5e988SJohn Dyson } 334695e5e988SJohn Dyson 3347ac1e407bSBruce Evans db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n", 3348ac1e407bSBruce Evans (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map, 3349b1028ad1SLuoqi Chen (void *)vmspace_pmap(p->p_vmspace)); 335095e5e988SJohn Dyson 3351101eeb7fSBruce Evans vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL); 335295e5e988SJohn Dyson } 335395e5e988SJohn Dyson 3354c7c34a24SBruce Evans #endif /* DDB */ 3355