xref: /freebsd/sys/vm/vm_map.c (revision c1fbc251cd43f57703976219d84bfb31a5716cf8)
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  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7061d80e90SJohn Baldwin #include <sys/ktr.h>
71fb919e4dSMark Murray #include <sys/lock.h>
72fb919e4dSMark Murray #include <sys/mutex.h>
73b5e8ce9fSBruce Evans #include <sys/proc.h>
74efeaf95aSDavid Greenman #include <sys/vmmeter.h>
75867a482dSJohn Dyson #include <sys/mman.h>
761efb74fbSJohn Dyson #include <sys/vnode.h>
772267af78SJulian Elischer #include <sys/resourcevar.h>
783fde38dfSMike Silbersack #include <sys/file.h>
7905ba50f5SJake Burkholder #include <sys/sysent.h>
803db161e0SMatthew Dillon #include <sys/shm.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 
1343a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1358355f576SJeff Roberson static uma_zone_t mapentzone;
1368355f576SJeff Roberson static uma_zone_t kmapentzone;
1378355f576SJeff Roberson static uma_zone_t mapzone;
1388355f576SJeff Roberson static uma_zone_t vmspace_zone;
1398355f576SJeff Roberson static struct vm_object kmapentobj;
140b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1418355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
142b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1438355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
1448355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
1451fc43fd1SAlan Cox 
1468355f576SJeff Roberson #ifdef INVARIANTS
1478355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1488355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1498355f576SJeff Roberson #endif
150b18bfc3dSJohn Dyson 
1510d94caffSDavid Greenman void
1521b40f8c0SMatthew Dillon vm_map_startup(void)
153df8bae1dSRodney W. Grimes {
1543a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1558355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1568355f576SJeff Roberson #ifdef INVARIANTS
1578355f576SJeff Roberson 	    vm_map_zdtor,
1588355f576SJeff Roberson #else
1598355f576SJeff Roberson 	    NULL,
1608355f576SJeff Roberson #endif
1618355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1628355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
163670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
16418aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
16518aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
166670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
167670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
168670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1698355f576SJeff Roberson 	uma_prealloc(mapentzone, MAX_MAPENT);
170df8bae1dSRodney W. Grimes }
171df8bae1dSRodney W. Grimes 
1728355f576SJeff Roberson static void
1738355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1748355f576SJeff Roberson {
1758355f576SJeff Roberson 	struct vmspace *vm;
1768355f576SJeff Roberson 
1778355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1783f25cbddSPeter Wemm 	pmap_release(vmspace_pmap(vm));
1798355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
1808355f576SJeff Roberson }
1818355f576SJeff Roberson 
182b23f72e9SBrian Feldman static int
183b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
1848355f576SJeff Roberson {
1858355f576SJeff Roberson 	struct vmspace *vm;
1868355f576SJeff Roberson 
1878355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1888355f576SJeff Roberson 
189b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
1903f25cbddSPeter Wemm 	pmap_pinit(vmspace_pmap(vm));
191b23f72e9SBrian Feldman 	return (0);
1928355f576SJeff Roberson }
1938355f576SJeff Roberson 
1948355f576SJeff Roberson static void
1958355f576SJeff Roberson vm_map_zfini(void *mem, int size)
1968355f576SJeff Roberson {
1978355f576SJeff Roberson 	vm_map_t map;
1988355f576SJeff Roberson 
1998355f576SJeff Roberson 	map = (vm_map_t)mem;
20036daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
20112c64974SMaxime Henrion 	sx_destroy(&map->lock);
2028355f576SJeff Roberson }
2038355f576SJeff Roberson 
204b23f72e9SBrian Feldman static int
205b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2068355f576SJeff Roberson {
2078355f576SJeff Roberson 	vm_map_t map;
2088355f576SJeff Roberson 
2098355f576SJeff Roberson 	map = (vm_map_t)mem;
2108355f576SJeff Roberson 	map->nentries = 0;
2118355f576SJeff Roberson 	map->size = 0;
212d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
21312c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
214b23f72e9SBrian Feldman 	return (0);
2158355f576SJeff Roberson }
2168355f576SJeff Roberson 
2178355f576SJeff Roberson #ifdef INVARIANTS
2188355f576SJeff Roberson static void
2198355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2208355f576SJeff Roberson {
2218355f576SJeff Roberson 	struct vmspace *vm;
2228355f576SJeff Roberson 
2238355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2248355f576SJeff Roberson 
2258355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2268355f576SJeff Roberson }
2278355f576SJeff Roberson static void
2288355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2298355f576SJeff Roberson {
2308355f576SJeff Roberson 	vm_map_t map;
2318355f576SJeff Roberson 
2328355f576SJeff Roberson 	map = (vm_map_t)mem;
2338355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2348355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2358355f576SJeff Roberson 	    map, map->nentries));
2368355f576SJeff Roberson 	KASSERT(map->size == 0,
2378355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2389eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
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  */
246df8bae1dSRodney W. Grimes struct vmspace *
2472d8acc0fSJohn Dyson vmspace_alloc(min, max)
248df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
249df8bae1dSRodney W. Grimes {
250c0877f10SJohn Dyson 	struct vmspace *vm;
2510d94caffSDavid Greenman 
252a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
25321c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
2548355f576SJeff Roberson 	_vm_map_init(&vm->vm_map, min, max);
255b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
256df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2572d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
25851ab6c28SAlan Cox 	vm->vm_swrss = 0;
25951ab6c28SAlan Cox 	vm->vm_tsize = 0;
26051ab6c28SAlan Cox 	vm->vm_dsize = 0;
26151ab6c28SAlan Cox 	vm->vm_ssize = 0;
26251ab6c28SAlan Cox 	vm->vm_taddr = 0;
26351ab6c28SAlan Cox 	vm->vm_daddr = 0;
26451ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
265389d2b6eSMatthew Dillon 	vm->vm_exitingcnt = 0;
266df8bae1dSRodney W. Grimes 	return (vm);
267df8bae1dSRodney W. Grimes }
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes void
2701b40f8c0SMatthew Dillon vm_init2(void)
2711b40f8c0SMatthew Dillon {
2729e7c1bceSPeter Wemm 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
2733fde38dfSMike Silbersack 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
2743fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
2758355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2768355f576SJeff Roberson #ifdef INVARIANTS
2778355f576SJeff Roberson 	    vmspace_zdtor,
2788355f576SJeff Roberson #else
2798355f576SJeff Roberson 	    NULL,
2808355f576SJeff Roberson #endif
2818355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
282ba9be04cSJohn Dyson 	pmap_init2();
2833075778bSJohn Dyson }
2843075778bSJohn Dyson 
285582ec34cSAlfred Perlstein static __inline void
286582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
287df8bae1dSRodney W. Grimes {
28821c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
2893db161e0SMatthew Dillon 
2903db161e0SMatthew Dillon 	/*
2913db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
2923db161e0SMatthew Dillon 	 * exit1().
2933db161e0SMatthew Dillon 	 */
2943db161e0SMatthew Dillon 	shmexit(vm);
2953db161e0SMatthew Dillon 
29630dcfc09SJohn Dyson 	/*
297df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
2980d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
2990d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
300df8bae1dSRodney W. Grimes 	 */
301df8bae1dSRodney W. Grimes 	vm_map_lock(&vm->vm_map);
302df8bae1dSRodney W. Grimes 	(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
303df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
304a1f6d91cSDavid Greenman 	vm_map_unlock(&vm->vm_map);
3058355f576SJeff Roberson 
3068355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
307df8bae1dSRodney W. Grimes }
308582ec34cSAlfred Perlstein 
309582ec34cSAlfred Perlstein void
310582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
311582ec34cSAlfred Perlstein {
3121a276a3fSAlan Cox 	int refcnt;
313582ec34cSAlfred Perlstein 
314582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
315582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
316582ec34cSAlfred Perlstein 
3171a276a3fSAlan Cox 	do
3181a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3191a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
3201a276a3fSAlan Cox 	if (refcnt == 1 && vm->vm_exitingcnt == 0)
321582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
322582ec34cSAlfred Perlstein }
323582ec34cSAlfred Perlstein 
324582ec34cSAlfred Perlstein void
325582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
326582ec34cSAlfred Perlstein {
327334f7061SPeter Wemm 	struct vmspace *vm;
3281a276a3fSAlan Cox 	int exitingcnt;
329582ec34cSAlfred Perlstein 
330334f7061SPeter Wemm 	vm = p->p_vmspace;
331334f7061SPeter Wemm 	p->p_vmspace = NULL;
332389d2b6eSMatthew Dillon 
333389d2b6eSMatthew Dillon 	/*
334389d2b6eSMatthew Dillon 	 * cleanup by parent process wait()ing on exiting child.  vm_refcnt
335389d2b6eSMatthew Dillon 	 * may not be 0 (e.g. fork() and child exits without exec()ing).
336389d2b6eSMatthew Dillon 	 * exitingcnt may increment above 0 and drop back down to zero
337389d2b6eSMatthew Dillon 	 * several times while vm_refcnt is held non-zero.  vm_refcnt
338389d2b6eSMatthew Dillon 	 * may also increment above 0 and drop back down to zero several
339389d2b6eSMatthew Dillon 	 * times while vm_exitingcnt is held non-zero.
340389d2b6eSMatthew Dillon 	 *
341389d2b6eSMatthew Dillon 	 * The last wait on the exiting child's vmspace will clean up
342389d2b6eSMatthew Dillon 	 * the remainder of the vmspace.
343389d2b6eSMatthew Dillon 	 */
3441a276a3fSAlan Cox 	do
3451a276a3fSAlan Cox 		exitingcnt = vm->vm_exitingcnt;
3461a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_exitingcnt, exitingcnt,
3471a276a3fSAlan Cox 	    exitingcnt - 1));
3481a276a3fSAlan Cox 	if (vm->vm_refcnt == 0 && exitingcnt == 1)
349334f7061SPeter Wemm 		vmspace_dofree(vm);
350334f7061SPeter Wemm }
351df8bae1dSRodney W. Grimes 
3521b40f8c0SMatthew Dillon void
353780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
3541b40f8c0SMatthew Dillon {
355bc91c510SAlan Cox 
35693bc4879SAlan Cox 	if (map->system_map)
35736daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
35812c64974SMaxime Henrion 	else
35912c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
3601b40f8c0SMatthew Dillon 	map->timestamp++;
3611b40f8c0SMatthew Dillon }
3621b40f8c0SMatthew Dillon 
3631b40f8c0SMatthew Dillon void
364780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
3650e0af8ecSBrian Feldman {
366bc91c510SAlan Cox 
36736daaecdSAlan Cox 	if (map->system_map)
36836daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
36936daaecdSAlan Cox 	else
37012c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
3710e0af8ecSBrian Feldman }
3720e0af8ecSBrian Feldman 
3730e0af8ecSBrian Feldman void
374780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
3750e0af8ecSBrian Feldman {
376bc91c510SAlan Cox 
37793bc4879SAlan Cox 	if (map->system_map)
37836daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
37912c64974SMaxime Henrion 	else
38012c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
38136daaecdSAlan Cox }
3820e0af8ecSBrian Feldman 
3830e0af8ecSBrian Feldman void
384780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
3850e0af8ecSBrian Feldman {
386bc91c510SAlan Cox 
38736daaecdSAlan Cox 	if (map->system_map)
38836daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
38936daaecdSAlan Cox 	else
39012c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
39125adb370SBrian Feldman }
39225adb370SBrian Feldman 
393d974f03cSAlan Cox int
394780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
395d974f03cSAlan Cox {
39625adb370SBrian Feldman 	int error;
39725adb370SBrian Feldman 
39836daaecdSAlan Cox 	error = map->system_map ?
39936daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
40012c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
4013a92e5d5SAlan Cox 	if (error == 0)
4023a92e5d5SAlan Cox 		map->timestamp++;
403bc91c510SAlan Cox 	return (error == 0);
4040e0af8ecSBrian Feldman }
4050e0af8ecSBrian Feldman 
4060e0af8ecSBrian Feldman int
40772d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
40872d97679SDavid Schultz {
40972d97679SDavid Schultz 	int error;
41072d97679SDavid Schultz 
41172d97679SDavid Schultz 	error = map->system_map ?
41272d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
41312c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
41472d97679SDavid Schultz 	return (error == 0);
41572d97679SDavid Schultz }
41672d97679SDavid Schultz 
41772d97679SDavid Schultz int
418780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
4190e0af8ecSBrian Feldman {
420bc91c510SAlan Cox 
42136daaecdSAlan Cox #ifdef INVARIANTS
42212c64974SMaxime Henrion 	if (map->system_map) {
42336daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
42436daaecdSAlan Cox 	} else
42512c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
42612c64974SMaxime Henrion #endif
427bc91c510SAlan Cox 	map->timestamp++;
428bc91c510SAlan Cox 	return (0);
4290e0af8ecSBrian Feldman }
4300e0af8ecSBrian Feldman 
4310e0af8ecSBrian Feldman void
432780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
4331b40f8c0SMatthew Dillon {
434bc91c510SAlan Cox 
43536daaecdSAlan Cox #ifdef INVARIANTS
43612c64974SMaxime Henrion 	if (map->system_map) {
43736daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
43836daaecdSAlan Cox 	} else
43912c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
44012c64974SMaxime Henrion #endif
44125adb370SBrian Feldman }
44225adb370SBrian Feldman 
443acd9a301SAlan Cox /*
444acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
445acd9a301SAlan Cox  */
4469688f931SAlan Cox int
447acd9a301SAlan Cox vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
448acd9a301SAlan Cox {
449acd9a301SAlan Cox 
4503a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
451acd9a301SAlan Cox 	vm_map_unlock(map);
4523a92e5d5SAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
453acd9a301SAlan Cox }
454acd9a301SAlan Cox 
455acd9a301SAlan Cox /*
456acd9a301SAlan Cox  *	vm_map_wakeup:
457acd9a301SAlan Cox  */
4589688f931SAlan Cox void
459acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
460acd9a301SAlan Cox {
461acd9a301SAlan Cox 
462b49ecb86SAlan Cox 	/*
4633a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
4643a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
4653a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
466b49ecb86SAlan Cox 	 */
4673a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
4683a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
469acd9a301SAlan Cox 	wakeup(&map->root);
470acd9a301SAlan Cox }
471acd9a301SAlan Cox 
4721b40f8c0SMatthew Dillon long
4731b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
4741b40f8c0SMatthew Dillon {
4751b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
4761b40f8c0SMatthew Dillon }
4771b40f8c0SMatthew Dillon 
4782bc7dd56SBruce M Simpson long
4792bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
4802bc7dd56SBruce M Simpson {
4812bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
4822bc7dd56SBruce M Simpson }
4832bc7dd56SBruce M Simpson 
484ff2b5645SMatthew Dillon /*
485df8bae1dSRodney W. Grimes  *	vm_map_create:
486df8bae1dSRodney W. Grimes  *
487df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
488df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
489df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
490df8bae1dSRodney W. Grimes  */
4910d94caffSDavid Greenman vm_map_t
4921b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
493df8bae1dSRodney W. Grimes {
494c0877f10SJohn Dyson 	vm_map_t result;
495df8bae1dSRodney W. Grimes 
496a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
49721c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
4988355f576SJeff Roberson 	_vm_map_init(result, min, max);
499df8bae1dSRodney W. Grimes 	result->pmap = pmap;
500df8bae1dSRodney W. Grimes 	return (result);
501df8bae1dSRodney W. Grimes }
502df8bae1dSRodney W. Grimes 
503df8bae1dSRodney W. Grimes /*
504df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
505df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
506df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
507df8bae1dSRodney W. Grimes  */
5088355f576SJeff Roberson static void
5098355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
510df8bae1dSRodney W. Grimes {
51121c641b2SJohn Baldwin 
512df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
5139688f931SAlan Cox 	map->needs_wakeup = FALSE;
5143075778bSJohn Dyson 	map->system_map = 0;
515df8bae1dSRodney W. Grimes 	map->min_offset = min;
516df8bae1dSRodney W. Grimes 	map->max_offset = max;
517af7cd0c5SBrian Feldman 	map->flags = 0;
5184e94f402SAlan Cox 	map->root = NULL;
519df8bae1dSRodney W. Grimes 	map->timestamp = 0;
520df8bae1dSRodney W. Grimes }
521df8bae1dSRodney W. Grimes 
522a18b1f1dSJason Evans void
5238355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
524a18b1f1dSJason Evans {
5258355f576SJeff Roberson 	_vm_map_init(map, min, max);
526d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
52712c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
528a18b1f1dSJason Evans }
529a18b1f1dSJason Evans 
530df8bae1dSRodney W. Grimes /*
531b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
532b18bfc3dSJohn Dyson  *
533b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
534b18bfc3dSJohn Dyson  */
53562487bb4SJohn Dyson static void
5361b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
537b18bfc3dSJohn Dyson {
5382b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
539b18bfc3dSJohn Dyson }
540b18bfc3dSJohn Dyson 
541b18bfc3dSJohn Dyson /*
542df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
543df8bae1dSRodney W. Grimes  *
544df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
545b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
546df8bae1dSRodney W. Grimes  */
547f708ef1bSPoul-Henning Kamp static vm_map_entry_t
5481b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
549df8bae1dSRodney W. Grimes {
5501f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
5511f6889a1SMatthew Dillon 
5522b4a2c27SAlan Cox 	if (map->system_map)
5532b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
5542b4a2c27SAlan Cox 	else
555a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
5561f6889a1SMatthew Dillon 	if (new_entry == NULL)
5571f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
5581f6889a1SMatthew Dillon 	return (new_entry);
559df8bae1dSRodney W. Grimes }
560df8bae1dSRodney W. Grimes 
561df8bae1dSRodney W. Grimes /*
562794316a8SAlan Cox  *	vm_map_entry_set_behavior:
563794316a8SAlan Cox  *
564794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
565794316a8SAlan Cox  *	sequential.
566794316a8SAlan Cox  */
567794316a8SAlan Cox static __inline void
568794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
569794316a8SAlan Cox {
570794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
571794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
572794316a8SAlan Cox }
573794316a8SAlan Cox 
574794316a8SAlan Cox /*
5750164e057SAlan Cox  *	vm_map_entry_set_max_free:
5760164e057SAlan Cox  *
5770164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
5780164e057SAlan Cox  */
5790164e057SAlan Cox static __inline void
5800164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
5810164e057SAlan Cox {
5820164e057SAlan Cox 
5830164e057SAlan Cox 	entry->max_free = entry->adj_free;
5840164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
5850164e057SAlan Cox 		entry->max_free = entry->left->max_free;
5860164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
5870164e057SAlan Cox 		entry->max_free = entry->right->max_free;
5880164e057SAlan Cox }
5890164e057SAlan Cox 
5900164e057SAlan Cox /*
5914e94f402SAlan Cox  *	vm_map_entry_splay:
5924e94f402SAlan Cox  *
5930164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
5940164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
5950164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
5960164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
5970164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
5980164e057SAlan Cox  *	amortized.
5990164e057SAlan Cox  *
6000164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
6010164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
6020164e057SAlan Cox  *
6030164e057SAlan Cox  *	The map must be locked, and leaves it so.
6040164e057SAlan Cox  *
6050164e057SAlan Cox  *	Returns: the new root.
6064e94f402SAlan Cox  */
6074e94f402SAlan Cox static vm_map_entry_t
6080164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
6094e94f402SAlan Cox {
6100164e057SAlan Cox 	vm_map_entry_t llist, rlist;
6110164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
6120164e057SAlan Cox 	vm_map_entry_t y;
6134e94f402SAlan Cox 
6140164e057SAlan Cox 	/* Special case of empty tree. */
6154e94f402SAlan Cox 	if (root == NULL)
6164e94f402SAlan Cox 		return (root);
6170164e057SAlan Cox 
6180164e057SAlan Cox 	/*
6190164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
6200164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
6210164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
6220164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
6230164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
6240164e057SAlan Cox 	 * the two spines.
6250164e057SAlan Cox 	 */
6260164e057SAlan Cox 	llist = NULL;
6270164e057SAlan Cox 	rlist = NULL;
6280164e057SAlan Cox 	for (;;) {
6290164e057SAlan Cox 		/* root is never NULL in here. */
6300164e057SAlan Cox 		if (addr < root->start) {
6310164e057SAlan Cox 			y = root->left;
6320164e057SAlan Cox 			if (y == NULL)
6334e94f402SAlan Cox 				break;
6340164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
6350164e057SAlan Cox 				/* Rotate right and put y on rlist. */
6364e94f402SAlan Cox 				root->left = y->right;
6374e94f402SAlan Cox 				y->right = root;
6380164e057SAlan Cox 				vm_map_entry_set_max_free(root);
6390164e057SAlan Cox 				root = y->left;
6400164e057SAlan Cox 				y->left = rlist;
6410164e057SAlan Cox 				rlist = y;
6420164e057SAlan Cox 			} else {
6430164e057SAlan Cox 				/* Put root on rlist. */
6440164e057SAlan Cox 				root->left = rlist;
6450164e057SAlan Cox 				rlist = root;
6464e94f402SAlan Cox 				root = y;
6474e94f402SAlan Cox 			}
6480164e057SAlan Cox 		} else {
6490164e057SAlan Cox 			y = root->right;
6500164e057SAlan Cox 			if (addr < root->end || y == NULL)
6514e94f402SAlan Cox 				break;
6520164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
6530164e057SAlan Cox 				/* Rotate left and put y on llist. */
6544e94f402SAlan Cox 				root->right = y->left;
6554e94f402SAlan Cox 				y->left = root;
6560164e057SAlan Cox 				vm_map_entry_set_max_free(root);
6570164e057SAlan Cox 				root = y->right;
6580164e057SAlan Cox 				y->right = llist;
6590164e057SAlan Cox 				llist = y;
6600164e057SAlan Cox 			} else {
6610164e057SAlan Cox 				/* Put root on llist. */
6620164e057SAlan Cox 				root->right = llist;
6630164e057SAlan Cox 				llist = root;
6644e94f402SAlan Cox 				root = y;
6654e94f402SAlan Cox 			}
6664e94f402SAlan Cox 		}
6670164e057SAlan Cox 	}
6680164e057SAlan Cox 
6690164e057SAlan Cox 	/*
6700164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
6710164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
6720164e057SAlan Cox 	 * bottom of llist and rlist.
6730164e057SAlan Cox 	 */
6740164e057SAlan Cox 	ltree = root->left;
6750164e057SAlan Cox 	while (llist != NULL) {
6760164e057SAlan Cox 		y = llist->right;
6770164e057SAlan Cox 		llist->right = ltree;
6780164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
6790164e057SAlan Cox 		ltree = llist;
6800164e057SAlan Cox 		llist = y;
6810164e057SAlan Cox 	}
6820164e057SAlan Cox 	rtree = root->right;
6830164e057SAlan Cox 	while (rlist != NULL) {
6840164e057SAlan Cox 		y = rlist->left;
6850164e057SAlan Cox 		rlist->left = rtree;
6860164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
6870164e057SAlan Cox 		rtree = rlist;
6880164e057SAlan Cox 		rlist = y;
6890164e057SAlan Cox 	}
6900164e057SAlan Cox 
6910164e057SAlan Cox 	/*
6920164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
6930164e057SAlan Cox 	 */
6940164e057SAlan Cox 	root->left = ltree;
6950164e057SAlan Cox 	root->right = rtree;
6960164e057SAlan Cox 	vm_map_entry_set_max_free(root);
6970164e057SAlan Cox 
6984e94f402SAlan Cox 	return (root);
6994e94f402SAlan Cox }
7004e94f402SAlan Cox 
7014e94f402SAlan Cox /*
702df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
703df8bae1dSRodney W. Grimes  *
704df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
705df8bae1dSRodney W. Grimes  */
7064e94f402SAlan Cox static void
70799c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
70899c81ca9SAlan Cox 		  vm_map_entry_t after_where,
70999c81ca9SAlan Cox 		  vm_map_entry_t entry)
71099c81ca9SAlan Cox {
71121c641b2SJohn Baldwin 
71221c641b2SJohn Baldwin 	CTR4(KTR_VM,
71321c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
71421c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
71599c81ca9SAlan Cox 	map->nentries++;
71699c81ca9SAlan Cox 	entry->prev = after_where;
71799c81ca9SAlan Cox 	entry->next = after_where->next;
71899c81ca9SAlan Cox 	entry->next->prev = entry;
71999c81ca9SAlan Cox 	after_where->next = entry;
7204e94f402SAlan Cox 
7214e94f402SAlan Cox 	if (after_where != &map->header) {
7224e94f402SAlan Cox 		if (after_where != map->root)
7234e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
7244e94f402SAlan Cox 		entry->right = after_where->right;
7254e94f402SAlan Cox 		entry->left = after_where;
7264e94f402SAlan Cox 		after_where->right = NULL;
7270164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
7280164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
7294e94f402SAlan Cox 	} else {
7304e94f402SAlan Cox 		entry->right = map->root;
7314e94f402SAlan Cox 		entry->left = NULL;
7324e94f402SAlan Cox 	}
7330164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
7340164e057SAlan Cox 	    entry->next->start) - entry->end;
7350164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
7364e94f402SAlan Cox 	map->root = entry;
737df8bae1dSRodney W. Grimes }
73899c81ca9SAlan Cox 
7394e94f402SAlan Cox static void
74099c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
74199c81ca9SAlan Cox 		    vm_map_entry_t entry)
74299c81ca9SAlan Cox {
7434e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
74499c81ca9SAlan Cox 
7454e94f402SAlan Cox 	if (entry != map->root)
7464e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
7474e94f402SAlan Cox 	if (entry->left == NULL)
7484e94f402SAlan Cox 		root = entry->right;
7494e94f402SAlan Cox 	else {
7504e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
7514e94f402SAlan Cox 		root->right = entry->right;
7520164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
7530164e057SAlan Cox 		    entry->next->start) - root->end;
7540164e057SAlan Cox 		vm_map_entry_set_max_free(root);
7554e94f402SAlan Cox 	}
7564e94f402SAlan Cox 	map->root = root;
7574e94f402SAlan Cox 
7584e94f402SAlan Cox 	prev = entry->prev;
7594e94f402SAlan Cox 	next = entry->next;
76099c81ca9SAlan Cox 	next->prev = prev;
76199c81ca9SAlan Cox 	prev->next = next;
76299c81ca9SAlan Cox 	map->nentries--;
76321c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
76421c641b2SJohn Baldwin 	    map->nentries, entry);
765df8bae1dSRodney W. Grimes }
766df8bae1dSRodney W. Grimes 
767df8bae1dSRodney W. Grimes /*
7680164e057SAlan Cox  *	vm_map_entry_resize_free:
7690164e057SAlan Cox  *
7700164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
7710164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
7720164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
7730164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
7740164e057SAlan Cox  *
7750164e057SAlan Cox  *	The map must be locked, and leaves it so.
7760164e057SAlan Cox  */
7770164e057SAlan Cox static void
7780164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
7790164e057SAlan Cox {
7800164e057SAlan Cox 
7810164e057SAlan Cox 	/*
7820164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
7830164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
7840164e057SAlan Cox 	 * root and making the change there.
7850164e057SAlan Cox 	 */
7860164e057SAlan Cox 	if (entry != map->root)
7870164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
7880164e057SAlan Cox 
7890164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
7900164e057SAlan Cox 	    entry->next->start) - entry->end;
7910164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
7920164e057SAlan Cox }
7930164e057SAlan Cox 
7940164e057SAlan Cox /*
795df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
796df8bae1dSRodney W. Grimes  *
797df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
798df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
799df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
800df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
801df8bae1dSRodney W. Grimes  *	result indicates whether the address is
802df8bae1dSRodney W. Grimes  *	actually contained in the map.
803df8bae1dSRodney W. Grimes  */
8040d94caffSDavid Greenman boolean_t
8051b40f8c0SMatthew Dillon vm_map_lookup_entry(
8061b40f8c0SMatthew Dillon 	vm_map_t map,
8071b40f8c0SMatthew Dillon 	vm_offset_t address,
8081b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
809df8bae1dSRodney W. Grimes {
810c0877f10SJohn Dyson 	vm_map_entry_t cur;
811df8bae1dSRodney W. Grimes 
8124e94f402SAlan Cox 	cur = vm_map_entry_splay(address, map->root);
8134e94f402SAlan Cox 	if (cur == NULL)
8144e94f402SAlan Cox 		*entry = &map->header;
8154e94f402SAlan Cox 	else {
8164e94f402SAlan Cox 		map->root = cur;
817df8bae1dSRodney W. Grimes 
818df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
819df8bae1dSRodney W. Grimes 			*entry = cur;
8204e94f402SAlan Cox 			if (cur->end > address)
821df8bae1dSRodney W. Grimes 				return (TRUE);
8224e94f402SAlan Cox 		} else
823df8bae1dSRodney W. Grimes 			*entry = cur->prev;
8244e94f402SAlan Cox 	}
825df8bae1dSRodney W. Grimes 	return (FALSE);
826df8bae1dSRodney W. Grimes }
827df8bae1dSRodney W. Grimes 
828df8bae1dSRodney W. Grimes /*
82930dcfc09SJohn Dyson  *	vm_map_insert:
83030dcfc09SJohn Dyson  *
83130dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
83230dcfc09SJohn Dyson  *	map at the specified address range.  The object's
83330dcfc09SJohn Dyson  *	size should match that of the address range.
83430dcfc09SJohn Dyson  *
83530dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
8362aaeadf8SMatthew Dillon  *
8372aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
8382aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
83930dcfc09SJohn Dyson  */
84030dcfc09SJohn Dyson int
841b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
842b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
843b9dcd593SBruce Evans 	      int cow)
84430dcfc09SJohn Dyson {
845c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
846c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
84730dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
8489730a5daSPaul Saab 	vm_eflags_t protoeflags;
84930dcfc09SJohn Dyson 
85030dcfc09SJohn Dyson 	/*
85130dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
85230dcfc09SJohn Dyson 	 */
85330dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
85430dcfc09SJohn Dyson 	    (start >= end))
85530dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
85630dcfc09SJohn Dyson 
85730dcfc09SJohn Dyson 	/*
85830dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
85930dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
86030dcfc09SJohn Dyson 	 */
86130dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
86230dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
86330dcfc09SJohn Dyson 
86430dcfc09SJohn Dyson 	prev_entry = temp_entry;
86530dcfc09SJohn Dyson 
86630dcfc09SJohn Dyson 	/*
86730dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
86830dcfc09SJohn Dyson 	 */
86930dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
87030dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
87130dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
87230dcfc09SJohn Dyson 
873afa07f7eSJohn Dyson 	protoeflags = 0;
874afa07f7eSJohn Dyson 
875afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
876e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
877afa07f7eSJohn Dyson 
8784e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
879afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
880afa07f7eSJohn Dyson 
8814e045f93SAlan Cox 		KASSERT(object == NULL,
8824e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
8834e045f93SAlan Cox 	}
8844f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
8854f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
8869730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
8879730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
8884f79d873SMatthew Dillon 
8891d284e00SAlan Cox 	if (object != NULL) {
89030dcfc09SJohn Dyson 		/*
8911d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
8921d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
8931d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
8941d284e00SAlan Cox 		 * reference counting is insufficient to recognize
8951d284e00SAlan Cox 		 * aliases with precision.)
89630dcfc09SJohn Dyson 		 */
8971d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
8981d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
8992aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
9001d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
9014e045f93SAlan Cox 	}
9024e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
9034e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
9048cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
9054e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
9064e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
9078cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
90857a21abaSAlan Cox 				     prev_entry->offset,
9098cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
910cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
91130dcfc09SJohn Dyson 		/*
9122aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
9132aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
9142aaeadf8SMatthew Dillon 		 * new range as well.
91530dcfc09SJohn Dyson 		 */
9168cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
9178cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
9188cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
91930dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
92030dcfc09SJohn Dyson 			prev_entry->end = end;
9210164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
9224e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
92330dcfc09SJohn Dyson 			return (KERN_SUCCESS);
92430dcfc09SJohn Dyson 		}
9258cc7e047SJohn Dyson 
9262aaeadf8SMatthew Dillon 		/*
9272aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
9282aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
9292aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
9304e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
9312aaeadf8SMatthew Dillon 		 */
9322aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
9332aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
9342aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
9358cc7e047SJohn Dyson 		vm_object_reference(object);
936b18bfc3dSJohn Dyson 	}
9372aaeadf8SMatthew Dillon 
9382aaeadf8SMatthew Dillon 	/*
9392aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
9402aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
9412aaeadf8SMatthew Dillon 	 * backing objects.
9422aaeadf8SMatthew Dillon 	 */
9438cc7e047SJohn Dyson 
94430dcfc09SJohn Dyson 	/*
94530dcfc09SJohn Dyson 	 * Create a new entry
94630dcfc09SJohn Dyson 	 */
94730dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
94830dcfc09SJohn Dyson 	new_entry->start = start;
94930dcfc09SJohn Dyson 	new_entry->end = end;
95030dcfc09SJohn Dyson 
951afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
95230dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
95330dcfc09SJohn Dyson 	new_entry->offset = offset;
9542267af78SJulian Elischer 	new_entry->avail_ssize = 0;
9552267af78SJulian Elischer 
95630dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
95730dcfc09SJohn Dyson 	new_entry->protection = prot;
95830dcfc09SJohn Dyson 	new_entry->max_protection = max;
95930dcfc09SJohn Dyson 	new_entry->wired_count = 0;
960e5f251d2SAlan Cox 
96130dcfc09SJohn Dyson 	/*
96230dcfc09SJohn Dyson 	 * Insert the new entry into the list
96330dcfc09SJohn Dyson 	 */
96430dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
96530dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
96630dcfc09SJohn Dyson 
9671a484d28SMatthew Dillon #if 0
9681a484d28SMatthew Dillon 	/*
9691a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
9701a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
9711a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
9721a484d28SMatthew Dillon 	 */
9734e71e795SMatthew Dillon 	/*
9744e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
9754e71e795SMatthew Dillon 	 */
9764e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
9771a484d28SMatthew Dillon #endif
9784e71e795SMatthew Dillon 
9794f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
9804da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
981e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
982e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
9834f79d873SMatthew Dillon 	}
984e972780aSAlan Cox 
98530dcfc09SJohn Dyson 	return (KERN_SUCCESS);
98630dcfc09SJohn Dyson }
98730dcfc09SJohn Dyson 
98830dcfc09SJohn Dyson /*
9890164e057SAlan Cox  *	vm_map_findspace:
9900164e057SAlan Cox  *
9910164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
9920164e057SAlan Cox  *	beginning at address >= start in the given map.
9930164e057SAlan Cox  *
9940164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
9950164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
9960164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
9970164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
9980164e057SAlan Cox  *	O(log n) amortized with splay trees.
9990164e057SAlan Cox  *
10000164e057SAlan Cox  *	The map must be locked, and leaves it so.
10010164e057SAlan Cox  *
10020164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
10030164e057SAlan Cox  *		 1 if insufficient space.
1004df8bae1dSRodney W. Grimes  */
1005df8bae1dSRodney W. Grimes int
10060164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
10070164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1008df8bae1dSRodney W. Grimes {
10090164e057SAlan Cox 	vm_map_entry_t entry;
10100164e057SAlan Cox 	vm_offset_t end, st;
1011df8bae1dSRodney W. Grimes 
10120164e057SAlan Cox 	/* Request must fit within min/max VM address. */
1013df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1014df8bae1dSRodney W. Grimes 		start = map->min_offset;
10150164e057SAlan Cox 	if (start + length > map->max_offset)
1016df8bae1dSRodney W. Grimes 		return (1);
1017df8bae1dSRodney W. Grimes 
10180164e057SAlan Cox 	/* Empty tree means wide open address space. */
10190164e057SAlan Cox 	if (map->root == NULL) {
1020df8bae1dSRodney W. Grimes 		*addr = start;
10210164e057SAlan Cox 		goto found;
102299448ed1SJohn Dyson 	}
10230164e057SAlan Cox 
10240164e057SAlan Cox 	/*
10250164e057SAlan Cox 	 * After splay, if start comes before root node, then there
10260164e057SAlan Cox 	 * must be a gap from start to the root.
10270164e057SAlan Cox 	 */
10280164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
10290164e057SAlan Cox 	if (start + length <= map->root->start) {
10300164e057SAlan Cox 		*addr = start;
10310164e057SAlan Cox 		goto found;
10320164e057SAlan Cox 	}
10330164e057SAlan Cox 
10340164e057SAlan Cox 	/*
10350164e057SAlan Cox 	 * Root is the last node that might begin its gap before
10360164e057SAlan Cox 	 * start.
10370164e057SAlan Cox 	 */
10380164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
10390164e057SAlan Cox 	if (st + length <= map->root->end + map->root->adj_free) {
10400164e057SAlan Cox 		*addr = st;
10410164e057SAlan Cox 		goto found;
10420164e057SAlan Cox 	}
10430164e057SAlan Cox 
10440164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
10450164e057SAlan Cox 	entry = map->root->right;
10460164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
10470164e057SAlan Cox 		return (1);
10480164e057SAlan Cox 
10490164e057SAlan Cox 	/*
10500164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
10510164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
10520164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
10530164e057SAlan Cox 	 */
10540164e057SAlan Cox 	while (entry != NULL) {
10550164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
10560164e057SAlan Cox 			entry = entry->left;
10570164e057SAlan Cox 		else if (entry->adj_free >= length) {
10580164e057SAlan Cox 			*addr = entry->end;
10590164e057SAlan Cox 			goto found;
10600164e057SAlan Cox 		} else
10610164e057SAlan Cox 			entry = entry->right;
10620164e057SAlan Cox 	}
10630164e057SAlan Cox 
10640164e057SAlan Cox 	/* Can't get here, so panic if we do. */
10650164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
10660164e057SAlan Cox 
10670164e057SAlan Cox found:
10680164e057SAlan Cox 	/* Expand the kernel pmap, if necessary. */
10690164e057SAlan Cox 	if (map == kernel_map) {
10700164e057SAlan Cox 		end = round_page(*addr + length);
10710164e057SAlan Cox 		if (end > kernel_vm_end)
10720164e057SAlan Cox 			pmap_growkernel(end);
107399448ed1SJohn Dyson 	}
1074df8bae1dSRodney W. Grimes 	return (0);
1075df8bae1dSRodney W. Grimes }
1076df8bae1dSRodney W. Grimes 
1077df8bae1dSRodney W. Grimes /*
1078df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1079df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1080df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1081df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1082df8bae1dSRodney W. Grimes  *
10832aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
10842aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1085df8bae1dSRodney W. Grimes  */
1086df8bae1dSRodney W. Grimes int
1087b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1088b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1089b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
1090b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1091df8bae1dSRodney W. Grimes {
1092c0877f10SJohn Dyson 	vm_offset_t start;
10936eaee3feSAlan Cox 	int result;
1094df8bae1dSRodney W. Grimes 
1095df8bae1dSRodney W. Grimes 	start = *addr;
1096bea41bcfSDavid Greenman 	vm_map_lock(map);
1097df8bae1dSRodney W. Grimes 	if (find_space) {
1098df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
1099df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1100df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
1101df8bae1dSRodney W. Grimes 		}
1102df8bae1dSRodney W. Grimes 		start = *addr;
1103df8bae1dSRodney W. Grimes 	}
1104bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
1105bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
1106df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1107df8bae1dSRodney W. Grimes 	return (result);
1108df8bae1dSRodney W. Grimes }
1109df8bae1dSRodney W. Grimes 
1110df8bae1dSRodney W. Grimes /*
1111b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
111267bf6868SJohn Dyson  *
11134e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
11144e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
11154e71e795SMatthew Dillon  *
11164e71e795SMatthew Dillon  *	The map must be locked.
11174e71e795SMatthew Dillon  *
11184e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
11194e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
11204e71e795SMatthew Dillon  *	both neighbors.
1121df8bae1dSRodney W. Grimes  */
1122b7b2aac2SJohn Dyson void
11231b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1124df8bae1dSRodney W. Grimes {
1125308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1126b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1127df8bae1dSRodney W. Grimes 
1128acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1129df8bae1dSRodney W. Grimes 		return;
1130308c24baSJohn Dyson 
1131308c24baSJohn Dyson 	prev = entry->prev;
1132308c24baSJohn Dyson 	if (prev != &map->header) {
113367bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
113467bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
113567bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
113695e5e988SJohn Dyson 		     (!prev->object.vm_object ||
113767bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1138afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
113967bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
114067bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
114167bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
1142b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
1143308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1144308c24baSJohn Dyson 			entry->start = prev->start;
1145308c24baSJohn Dyson 			entry->offset = prev->offset;
11460164e057SAlan Cox 			if (entry->prev != &map->header)
11470164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
1148b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1149308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1150308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1151308c24baSJohn Dyson 		}
1152308c24baSJohn Dyson 	}
1153de5f6a77SJohn Dyson 
1154de5f6a77SJohn Dyson 	next = entry->next;
1155308c24baSJohn Dyson 	if (next != &map->header) {
115667bf6868SJohn Dyson 		esize = entry->end - entry->start;
115767bf6868SJohn Dyson 		if ((entry->end == next->start) &&
115867bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
115967bf6868SJohn Dyson 		     (!entry->object.vm_object ||
116067bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1161afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
116267bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
116367bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
116467bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
1165b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
1166de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1167de5f6a77SJohn Dyson 			entry->end = next->end;
11680164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
1169b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1170de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1171de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1172df8bae1dSRodney W. Grimes 		}
1173df8bae1dSRodney W. Grimes 	}
1174de5f6a77SJohn Dyson }
1175df8bae1dSRodney W. Grimes /*
1176df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1177df8bae1dSRodney W. Grimes  *
1178df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1179df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1180df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1181df8bae1dSRodney W. Grimes  */
1182df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1183df8bae1dSRodney W. Grimes { \
1184df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1185df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1186df8bae1dSRodney W. Grimes }
1187df8bae1dSRodney W. Grimes 
1188df8bae1dSRodney W. Grimes /*
1189df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1190df8bae1dSRodney W. Grimes  *	the entry must be split.
1191df8bae1dSRodney W. Grimes  */
11920d94caffSDavid Greenman static void
11931b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1194df8bae1dSRodney W. Grimes {
1195c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1196df8bae1dSRodney W. Grimes 
1197df8bae1dSRodney W. Grimes 	/*
11980d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
11990d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
12000d94caffSDavid Greenman 	 * starting address.
1201df8bae1dSRodney W. Grimes 	 */
1202f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1203f32dbbeeSJohn Dyson 
120411cccda1SJohn Dyson 	/*
120511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
120611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
120711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
120811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
120911cccda1SJohn Dyson 	 * put this improvement.
121011cccda1SJohn Dyson 	 */
12114e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
121211cccda1SJohn Dyson 		vm_object_t object;
121311cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1214c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
121511cccda1SJohn Dyson 		entry->object.vm_object = object;
121611cccda1SJohn Dyson 		entry->offset = 0;
121711cccda1SJohn Dyson 	}
121811cccda1SJohn Dyson 
1219df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1220df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1221df8bae1dSRodney W. Grimes 
1222df8bae1dSRodney W. Grimes 	new_entry->end = start;
1223df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1224df8bae1dSRodney W. Grimes 	entry->start = start;
1225df8bae1dSRodney W. Grimes 
1226df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1227df8bae1dSRodney W. Grimes 
12289fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1229df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1230df8bae1dSRodney W. Grimes 	}
1231c0877f10SJohn Dyson }
1232df8bae1dSRodney W. Grimes 
1233df8bae1dSRodney W. Grimes /*
1234df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1235df8bae1dSRodney W. Grimes  *
1236df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1237df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1238df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1239df8bae1dSRodney W. Grimes  */
1240df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1241df8bae1dSRodney W. Grimes { \
1242af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1243af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1244df8bae1dSRodney W. Grimes }
1245df8bae1dSRodney W. Grimes 
1246df8bae1dSRodney W. Grimes /*
1247df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1248df8bae1dSRodney W. Grimes  *	the entry must be split.
1249df8bae1dSRodney W. Grimes  */
12500d94caffSDavid Greenman static void
12511b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1252df8bae1dSRodney W. Grimes {
1253c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1254df8bae1dSRodney W. Grimes 
1255df8bae1dSRodney W. Grimes 	/*
125611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
125711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
125811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
125911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
126011cccda1SJohn Dyson 	 * put this improvement.
126111cccda1SJohn Dyson 	 */
12624e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
126311cccda1SJohn Dyson 		vm_object_t object;
126411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1265c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
126611cccda1SJohn Dyson 		entry->object.vm_object = object;
126711cccda1SJohn Dyson 		entry->offset = 0;
126811cccda1SJohn Dyson 	}
126911cccda1SJohn Dyson 
127011cccda1SJohn Dyson 	/*
12710d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1272df8bae1dSRodney W. Grimes 	 */
1273df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1274df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1275df8bae1dSRodney W. Grimes 
1276df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1277df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1278df8bae1dSRodney W. Grimes 
1279df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1280df8bae1dSRodney W. Grimes 
12819fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1282df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1283df8bae1dSRodney W. Grimes 	}
1284c0877f10SJohn Dyson }
1285df8bae1dSRodney W. Grimes 
1286df8bae1dSRodney W. Grimes /*
1287df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1288df8bae1dSRodney W. Grimes  *
1289df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
1290df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
1291df8bae1dSRodney W. Grimes  */
1292df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1293df8bae1dSRodney W. Grimes 		{					\
1294df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
1295df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
1296df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
1297df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
1298df8bae1dSRodney W. Grimes 		if (start > end)			\
1299df8bae1dSRodney W. Grimes 			start = end;			\
1300df8bae1dSRodney W. Grimes 		}
1301df8bae1dSRodney W. Grimes 
1302df8bae1dSRodney W. Grimes /*
1303df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1304df8bae1dSRodney W. Grimes  *
1305df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1306df8bae1dSRodney W. Grimes  *
1307df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1308df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1309df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1310df8bae1dSRodney W. Grimes  *
1311df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1312df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1313df8bae1dSRodney W. Grimes  *		vm_fault
1314df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1315df8bae1dSRodney W. Grimes  *
1316df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1317df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1318df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1319df8bae1dSRodney W. Grimes  */
1320df8bae1dSRodney W. Grimes int
13211b40f8c0SMatthew Dillon vm_map_submap(
13221b40f8c0SMatthew Dillon 	vm_map_t map,
13231b40f8c0SMatthew Dillon 	vm_offset_t start,
13241b40f8c0SMatthew Dillon 	vm_offset_t end,
13251b40f8c0SMatthew Dillon 	vm_map_t submap)
1326df8bae1dSRodney W. Grimes {
1327df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1328c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1329df8bae1dSRodney W. Grimes 
1330df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1333df8bae1dSRodney W. Grimes 
1334df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1335df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
13360d94caffSDavid Greenman 	} else
1337df8bae1dSRodney W. Grimes 		entry = entry->next;
1338df8bae1dSRodney W. Grimes 
1339df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1340df8bae1dSRodney W. Grimes 
1341df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
13429fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1343afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
13442d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1345afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1346df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1347df8bae1dSRodney W. Grimes 	}
1348df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 	return (result);
1351df8bae1dSRodney W. Grimes }
1352df8bae1dSRodney W. Grimes 
1353df8bae1dSRodney W. Grimes /*
13541f78f902SAlan Cox  * The maximum number of pages to map
13551f78f902SAlan Cox  */
13561f78f902SAlan Cox #define	MAX_INIT_PT	96
13571f78f902SAlan Cox 
13581f78f902SAlan Cox /*
13590551c08dSAlan Cox  *	vm_map_pmap_enter:
13600551c08dSAlan Cox  *
13614da4d293SAlan Cox  *	Preload read-only mappings for the given object into the specified
13620551c08dSAlan Cox  *	map.  This eliminates the soft faults on process startup and
13630551c08dSAlan Cox  *	immediately after an mmap(2).
13640551c08dSAlan Cox  */
13650551c08dSAlan Cox void
13664da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
13670551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
13680551c08dSAlan Cox {
13691f78f902SAlan Cox 	vm_offset_t tmpidx;
13701f78f902SAlan Cox 	int psize;
13711f78f902SAlan Cox 	vm_page_t p, mpte;
13720551c08dSAlan Cox 
13734da4d293SAlan Cox 	if ((prot & VM_PROT_READ) == 0 || object == NULL)
13741f78f902SAlan Cox 		return;
13751f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
13761f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
13771f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
13781f78f902SAlan Cox 		goto unlock_return;
13791f78f902SAlan Cox 	}
13801f78f902SAlan Cox 
13811f78f902SAlan Cox 	psize = atop(size);
13821f78f902SAlan Cox 
13831f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
13841f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
13851f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
13861f78f902SAlan Cox 		goto unlock_return;
13871f78f902SAlan Cox 	}
13881f78f902SAlan Cox 
13891f78f902SAlan Cox 	if (psize + pindex > object->size) {
13901f78f902SAlan Cox 		if (object->size < pindex)
13911f78f902SAlan Cox 			goto unlock_return;
13921f78f902SAlan Cox 		psize = object->size - pindex;
13931f78f902SAlan Cox 	}
13941f78f902SAlan Cox 
13951f78f902SAlan Cox 	mpte = NULL;
13961f78f902SAlan Cox 
13971f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
13981f78f902SAlan Cox 		if (p->pindex < pindex) {
13991f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
14001f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
14011f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
14021f78f902SAlan Cox 		}
14031f78f902SAlan Cox 	}
14041f78f902SAlan Cox 	/*
14051f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
14061f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
14071f78f902SAlan Cox 	 * or (2) NULL.
14081f78f902SAlan Cox 	 */
14091f78f902SAlan Cox 	for (;
14101f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
14111f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
14121f78f902SAlan Cox 		/*
14131f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
14141f78f902SAlan Cox 		 * free pages allocating pv entries.
14151f78f902SAlan Cox 		 */
14161f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
14171f78f902SAlan Cox 		    cnt.v_free_count < cnt.v_free_reserved) {
14181f78f902SAlan Cox 			break;
14191f78f902SAlan Cox 		}
14201f78f902SAlan Cox 		vm_page_lock_queues();
14211f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
14221f78f902SAlan Cox 		    (p->busy == 0) &&
14231f78f902SAlan Cox 		    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
14241f78f902SAlan Cox 			if ((p->queue - p->pc) == PQ_CACHE)
14251f78f902SAlan Cox 				vm_page_deactivate(p);
14261f78f902SAlan Cox 			vm_page_busy(p);
14271f78f902SAlan Cox 			vm_page_unlock_queues();
14281f78f902SAlan Cox 			VM_OBJECT_UNLOCK(object);
14291f78f902SAlan Cox 			mpte = pmap_enter_quick(map->pmap,
14301f78f902SAlan Cox 				addr + ptoa(tmpidx), p, mpte);
14311f78f902SAlan Cox 			VM_OBJECT_LOCK(object);
14321f78f902SAlan Cox 			vm_page_lock_queues();
14331f78f902SAlan Cox 			vm_page_wakeup(p);
14341f78f902SAlan Cox 		}
14351f78f902SAlan Cox 		vm_page_unlock_queues();
14361f78f902SAlan Cox 	}
14371f78f902SAlan Cox unlock_return:
14381f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
14390551c08dSAlan Cox }
14400551c08dSAlan Cox 
14410551c08dSAlan Cox /*
1442df8bae1dSRodney W. Grimes  *	vm_map_protect:
1443df8bae1dSRodney W. Grimes  *
1444df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1445df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1446df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1447df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1448df8bae1dSRodney W. Grimes  */
1449df8bae1dSRodney W. Grimes int
1450b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1451b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1452df8bae1dSRodney W. Grimes {
1453c0877f10SJohn Dyson 	vm_map_entry_t current;
1454df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1455df8bae1dSRodney W. Grimes 
1456df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1457df8bae1dSRodney W. Grimes 
1458df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1459df8bae1dSRodney W. Grimes 
1460df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1461df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1462b7b2aac2SJohn Dyson 	} else {
1463df8bae1dSRodney W. Grimes 		entry = entry->next;
1464b7b2aac2SJohn Dyson 	}
1465df8bae1dSRodney W. Grimes 
1466df8bae1dSRodney W. Grimes 	/*
14670d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1468df8bae1dSRodney W. Grimes 	 */
1469df8bae1dSRodney W. Grimes 	current = entry;
1470df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1471afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1472a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1473df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1474a1f6d91cSDavid Greenman 		}
1475df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1476df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1477df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1478df8bae1dSRodney W. Grimes 		}
1479df8bae1dSRodney W. Grimes 		current = current->next;
1480df8bae1dSRodney W. Grimes 	}
1481df8bae1dSRodney W. Grimes 
1482df8bae1dSRodney W. Grimes 	/*
14830d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
14840d94caffSDavid Greenman 	 * necessary the second time.]
1485df8bae1dSRodney W. Grimes 	 */
1486df8bae1dSRodney W. Grimes 	current = entry;
1487df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1488df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1489df8bae1dSRodney W. Grimes 
1490df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1491df8bae1dSRodney W. Grimes 
1492df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1493df8bae1dSRodney W. Grimes 		if (set_max)
1494df8bae1dSRodney W. Grimes 			current->protection =
1495df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1496df8bae1dSRodney W. Grimes 			    old_prot;
1497df8bae1dSRodney W. Grimes 		else
1498df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1499df8bae1dSRodney W. Grimes 
1500df8bae1dSRodney W. Grimes 		/*
15010d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
15020d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1503df8bae1dSRodney W. Grimes 		 */
1504df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1505afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1506df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1507df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1508df8bae1dSRodney W. Grimes 			    current->end,
15091c85e3dfSAlan Cox 			    current->protection & MASK(current));
1510df8bae1dSRodney W. Grimes #undef	MASK
1511df8bae1dSRodney W. Grimes 		}
15127d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1513df8bae1dSRodney W. Grimes 		current = current->next;
1514df8bae1dSRodney W. Grimes 	}
1515df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1516df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1517df8bae1dSRodney W. Grimes }
1518df8bae1dSRodney W. Grimes 
1519df8bae1dSRodney W. Grimes /*
1520867a482dSJohn Dyson  *	vm_map_madvise:
1521867a482dSJohn Dyson  *
1522867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1523f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1524f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1525f7fc307aSAlan Cox  *	objects.
1526867a482dSJohn Dyson  */
1527b4309055SMatthew Dillon int
15281b40f8c0SMatthew Dillon vm_map_madvise(
15291b40f8c0SMatthew Dillon 	vm_map_t map,
15301b40f8c0SMatthew Dillon 	vm_offset_t start,
15311b40f8c0SMatthew Dillon 	vm_offset_t end,
15321b40f8c0SMatthew Dillon 	int behav)
1533867a482dSJohn Dyson {
1534f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1535b4309055SMatthew Dillon 	int modify_map = 0;
1536867a482dSJohn Dyson 
1537b4309055SMatthew Dillon 	/*
1538b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1539b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1540b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1541b4309055SMatthew Dillon 	 * on the map.
1542b4309055SMatthew Dillon 	 */
1543b4309055SMatthew Dillon 	switch(behav) {
1544b4309055SMatthew Dillon 	case MADV_NORMAL:
1545b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1546b4309055SMatthew Dillon 	case MADV_RANDOM:
15474f79d873SMatthew Dillon 	case MADV_NOSYNC:
15484f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
15499730a5daSPaul Saab 	case MADV_NOCORE:
15509730a5daSPaul Saab 	case MADV_CORE:
1551b4309055SMatthew Dillon 		modify_map = 1;
1552867a482dSJohn Dyson 		vm_map_lock(map);
1553b4309055SMatthew Dillon 		break;
1554b4309055SMatthew Dillon 	case MADV_WILLNEED:
1555b4309055SMatthew Dillon 	case MADV_DONTNEED:
1556b4309055SMatthew Dillon 	case MADV_FREE:
1557f7fc307aSAlan Cox 		vm_map_lock_read(map);
1558b4309055SMatthew Dillon 		break;
1559b4309055SMatthew Dillon 	default:
1560b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1561b4309055SMatthew Dillon 	}
1562b4309055SMatthew Dillon 
1563b4309055SMatthew Dillon 	/*
1564b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1565b4309055SMatthew Dillon 	 */
1566867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1567867a482dSJohn Dyson 
1568867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1569f7fc307aSAlan Cox 		if (modify_map)
1570867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1571b4309055SMatthew Dillon 	} else {
1572867a482dSJohn Dyson 		entry = entry->next;
1573b4309055SMatthew Dillon 	}
1574867a482dSJohn Dyson 
1575f7fc307aSAlan Cox 	if (modify_map) {
1576f7fc307aSAlan Cox 		/*
1577f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1578f7fc307aSAlan Cox 		 *
1579f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1580f7fc307aSAlan Cox 		 * limited to the specified address range.
1581f7fc307aSAlan Cox 		 */
1582867a482dSJohn Dyson 		for (current = entry;
1583867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1584b4309055SMatthew Dillon 		     current = current->next
1585b4309055SMatthew Dillon 		) {
1586f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1587867a482dSJohn Dyson 				continue;
1588fed9a903SJohn Dyson 
158947221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1590fed9a903SJohn Dyson 
1591f7fc307aSAlan Cox 			switch (behav) {
1592867a482dSJohn Dyson 			case MADV_NORMAL:
15937f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1594867a482dSJohn Dyson 				break;
1595867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
15967f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1597867a482dSJohn Dyson 				break;
1598867a482dSJohn Dyson 			case MADV_RANDOM:
15997f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1600867a482dSJohn Dyson 				break;
16014f79d873SMatthew Dillon 			case MADV_NOSYNC:
16024f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
16034f79d873SMatthew Dillon 				break;
16044f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
16054f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
16064f79d873SMatthew Dillon 				break;
16079730a5daSPaul Saab 			case MADV_NOCORE:
16089730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
16099730a5daSPaul Saab 				break;
16109730a5daSPaul Saab 			case MADV_CORE:
16119730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
16129730a5daSPaul Saab 				break;
1613867a482dSJohn Dyson 			default:
1614867a482dSJohn Dyson 				break;
1615867a482dSJohn Dyson 			}
1616f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1617867a482dSJohn Dyson 		}
1618867a482dSJohn Dyson 		vm_map_unlock(map);
1619b4309055SMatthew Dillon 	} else {
1620f7fc307aSAlan Cox 		vm_pindex_t pindex;
1621f7fc307aSAlan Cox 		int count;
1622f7fc307aSAlan Cox 
1623f7fc307aSAlan Cox 		/*
1624f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1625f7fc307aSAlan Cox 		 * vm_object.
1626f7fc307aSAlan Cox 		 *
1627f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1628f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1629f7fc307aSAlan Cox 		 */
1630f7fc307aSAlan Cox 		for (current = entry;
1631f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1632b4309055SMatthew Dillon 		     current = current->next
1633b4309055SMatthew Dillon 		) {
16345f99b57cSMatthew Dillon 			vm_offset_t useStart;
16355f99b57cSMatthew Dillon 
1636f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1637f7fc307aSAlan Cox 				continue;
1638f7fc307aSAlan Cox 
1639f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1640f7fc307aSAlan Cox 			count = atop(current->end - current->start);
16415f99b57cSMatthew Dillon 			useStart = current->start;
1642f7fc307aSAlan Cox 
1643f7fc307aSAlan Cox 			if (current->start < start) {
1644f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1645f7fc307aSAlan Cox 				count -= atop(start - current->start);
16465f99b57cSMatthew Dillon 				useStart = start;
1647f7fc307aSAlan Cox 			}
1648f7fc307aSAlan Cox 			if (current->end > end)
1649f7fc307aSAlan Cox 				count -= atop(current->end - end);
1650f7fc307aSAlan Cox 
1651f7fc307aSAlan Cox 			if (count <= 0)
1652f7fc307aSAlan Cox 				continue;
1653f7fc307aSAlan Cox 
1654f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1655f7fc307aSAlan Cox 					  pindex, count, behav);
1656b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
16570551c08dSAlan Cox 				vm_map_pmap_enter(map,
16585f99b57cSMatthew Dillon 				    useStart,
16594da4d293SAlan Cox 				    current->protection,
1660f7fc307aSAlan Cox 				    current->object.vm_object,
1661b4309055SMatthew Dillon 				    pindex,
1662b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1663e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1664b4309055SMatthew Dillon 				);
1665f7fc307aSAlan Cox 			}
1666f7fc307aSAlan Cox 		}
1667f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1668f7fc307aSAlan Cox 	}
1669b4309055SMatthew Dillon 	return (0);
1670867a482dSJohn Dyson }
1671867a482dSJohn Dyson 
1672867a482dSJohn Dyson 
1673867a482dSJohn Dyson /*
1674df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1675df8bae1dSRodney W. Grimes  *
1676df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1677df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1678df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1679df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1680df8bae1dSRodney W. Grimes  */
1681df8bae1dSRodney W. Grimes int
1682b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1683b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1684df8bae1dSRodney W. Grimes {
1685c0877f10SJohn Dyson 	vm_map_entry_t entry;
1686df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1687df8bae1dSRodney W. Grimes 
1688df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1689df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1690df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1691df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1692df8bae1dSRodney W. Grimes 		break;
1693df8bae1dSRodney W. Grimes 	default:
1694df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1695df8bae1dSRodney W. Grimes 	}
1696df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1697df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1698df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1699df8bae1dSRodney W. Grimes 		entry = temp_entry;
1700df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17010d94caffSDavid Greenman 	} else
1702df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1703df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1704df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1705df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
170644428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
1707df8bae1dSRodney W. Grimes 		entry = entry->next;
1708df8bae1dSRodney W. Grimes 	}
1709df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1710df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1711df8bae1dSRodney W. Grimes }
1712df8bae1dSRodney W. Grimes 
1713df8bae1dSRodney W. Grimes /*
1714acd9a301SAlan Cox  *	vm_map_unwire:
1715acd9a301SAlan Cox  *
1716e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
1717acd9a301SAlan Cox  */
1718acd9a301SAlan Cox int
1719acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1720abd498aaSBruce M Simpson     int flags)
1721acd9a301SAlan Cox {
1722acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
1723acd9a301SAlan Cox 	vm_offset_t saved_start;
1724acd9a301SAlan Cox 	unsigned int last_timestamp;
1725acd9a301SAlan Cox 	int rv;
1726abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
1727acd9a301SAlan Cox 
1728abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1729acd9a301SAlan Cox 	vm_map_lock(map);
1730acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
1731acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1732abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1733cbef13d8SAlan Cox 			first_entry = first_entry->next;
1734abd498aaSBruce M Simpson 		else {
1735acd9a301SAlan Cox 			vm_map_unlock(map);
1736acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
1737acd9a301SAlan Cox 		}
1738abd498aaSBruce M Simpson 	}
1739acd9a301SAlan Cox 	last_timestamp = map->timestamp;
1740acd9a301SAlan Cox 	entry = first_entry;
1741acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1742acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1743acd9a301SAlan Cox 			/*
1744acd9a301SAlan Cox 			 * We have not yet clipped the entry.
1745acd9a301SAlan Cox 			 */
1746acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
1747acd9a301SAlan Cox 			    entry->start;
1748acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1749acd9a301SAlan Cox 			if (vm_map_unlock_and_wait(map, user_unwire)) {
1750acd9a301SAlan Cox 				/*
1751acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
1752acd9a301SAlan Cox 				 */
1753acd9a301SAlan Cox 			}
1754acd9a301SAlan Cox 			vm_map_lock(map);
1755acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
1756acd9a301SAlan Cox 				/*
1757acd9a301SAlan Cox 				 * Look again for the entry because the map was
1758acd9a301SAlan Cox 				 * modified while it was unlocked.
1759acd9a301SAlan Cox 				 * Specifically, the entry may have been
1760acd9a301SAlan Cox 				 * clipped, merged, or deleted.
1761acd9a301SAlan Cox 				 */
1762acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
1763acd9a301SAlan Cox 				    &tmp_entry)) {
1764cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1765cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1766cbef13d8SAlan Cox 					else {
1767acd9a301SAlan Cox 						if (saved_start == start) {
1768acd9a301SAlan Cox 							/*
1769acd9a301SAlan Cox 							 * First_entry has been deleted.
1770acd9a301SAlan Cox 							 */
1771acd9a301SAlan Cox 							vm_map_unlock(map);
1772acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
1773acd9a301SAlan Cox 						}
1774acd9a301SAlan Cox 						end = saved_start;
1775acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
1776acd9a301SAlan Cox 						goto done;
1777acd9a301SAlan Cox 					}
1778cbef13d8SAlan Cox 				}
1779acd9a301SAlan Cox 				if (entry == first_entry)
1780acd9a301SAlan Cox 					first_entry = tmp_entry;
1781acd9a301SAlan Cox 				else
1782acd9a301SAlan Cox 					first_entry = NULL;
1783acd9a301SAlan Cox 				entry = tmp_entry;
1784acd9a301SAlan Cox 			}
1785acd9a301SAlan Cox 			last_timestamp = map->timestamp;
1786acd9a301SAlan Cox 			continue;
1787acd9a301SAlan Cox 		}
1788acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
1789acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
1790acd9a301SAlan Cox 		/*
1791acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
1792acd9a301SAlan Cox 		 * above.)
1793acd9a301SAlan Cox 		 */
1794acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1795acd9a301SAlan Cox 		/*
1796acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
1797abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1798acd9a301SAlan Cox 		 */
1799abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1800abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
1801abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
1802acd9a301SAlan Cox 			end = entry->end;
1803acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
1804acd9a301SAlan Cox 			goto done;
1805acd9a301SAlan Cox 		}
1806acd9a301SAlan Cox 		/*
18073ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
1808acd9a301SAlan Cox 		 */
18090ada205eSBrian Feldman 		if (!user_unwire &&
18100ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
1811acd9a301SAlan Cox 			end = entry->end;
1812acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
1813acd9a301SAlan Cox 			goto done;
1814acd9a301SAlan Cox 		}
1815acd9a301SAlan Cox 		entry = entry->next;
1816acd9a301SAlan Cox 	}
1817acd9a301SAlan Cox 	rv = KERN_SUCCESS;
1818acd9a301SAlan Cox done:
1819e27e17b7SAlan Cox 	need_wakeup = FALSE;
1820acd9a301SAlan Cox 	if (first_entry == NULL) {
1821acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
1822cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1823cbef13d8SAlan Cox 			first_entry = first_entry->next;
1824cbef13d8SAlan Cox 		else
1825acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
1826acd9a301SAlan Cox 	}
1827acd9a301SAlan Cox 	entry = first_entry;
1828acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
18293ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
18303ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1831b2f3846aSAlan Cox 			if (user_unwire)
1832b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1833b2f3846aSAlan Cox 			entry->wired_count--;
18340ada205eSBrian Feldman 			if (entry->wired_count == 0) {
1835b2f3846aSAlan Cox 				/*
1836b2f3846aSAlan Cox 				 * Retain the map lock.
1837b2f3846aSAlan Cox 				 */
18384be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
18394be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
18404be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
1841b2f3846aSAlan Cox 			}
1842b2f3846aSAlan Cox 		}
1843acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1844acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
1845acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1846acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1847acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1848acd9a301SAlan Cox 			need_wakeup = TRUE;
1849acd9a301SAlan Cox 		}
1850acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
1851acd9a301SAlan Cox 		entry = entry->next;
1852acd9a301SAlan Cox 	}
1853acd9a301SAlan Cox 	vm_map_unlock(map);
1854acd9a301SAlan Cox 	if (need_wakeup)
1855acd9a301SAlan Cox 		vm_map_wakeup(map);
1856acd9a301SAlan Cox 	return (rv);
1857acd9a301SAlan Cox }
1858acd9a301SAlan Cox 
1859acd9a301SAlan Cox /*
1860e27e17b7SAlan Cox  *	vm_map_wire:
1861e27e17b7SAlan Cox  *
1862e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
1863e27e17b7SAlan Cox  */
1864e27e17b7SAlan Cox int
1865e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1866abd498aaSBruce M Simpson     int flags)
1867e27e17b7SAlan Cox {
186812d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
186912d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
187012d7cc84SAlan Cox 	unsigned int last_timestamp;
187112d7cc84SAlan Cox 	int rv;
18724be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
1873e27e17b7SAlan Cox 
1874abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
187512d7cc84SAlan Cox 	vm_map_lock(map);
187612d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
187712d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1878abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1879cbef13d8SAlan Cox 			first_entry = first_entry->next;
1880abd498aaSBruce M Simpson 		else {
188112d7cc84SAlan Cox 			vm_map_unlock(map);
188212d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
188312d7cc84SAlan Cox 		}
1884abd498aaSBruce M Simpson 	}
188512d7cc84SAlan Cox 	last_timestamp = map->timestamp;
188612d7cc84SAlan Cox 	entry = first_entry;
188712d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
188812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
188912d7cc84SAlan Cox 			/*
189012d7cc84SAlan Cox 			 * We have not yet clipped the entry.
189112d7cc84SAlan Cox 			 */
189212d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
189312d7cc84SAlan Cox 			    entry->start;
189412d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
189512d7cc84SAlan Cox 			if (vm_map_unlock_and_wait(map, user_wire)) {
189612d7cc84SAlan Cox 				/*
189712d7cc84SAlan Cox 				 * Allow interruption of user wiring?
189812d7cc84SAlan Cox 				 */
189912d7cc84SAlan Cox 			}
190012d7cc84SAlan Cox 			vm_map_lock(map);
190112d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
190212d7cc84SAlan Cox 				/*
190312d7cc84SAlan Cox 				 * Look again for the entry because the map was
190412d7cc84SAlan Cox 				 * modified while it was unlocked.
190512d7cc84SAlan Cox 				 * Specifically, the entry may have been
190612d7cc84SAlan Cox 				 * clipped, merged, or deleted.
190712d7cc84SAlan Cox 				 */
190812d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
190912d7cc84SAlan Cox 				    &tmp_entry)) {
1910cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1911cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1912cbef13d8SAlan Cox 					else {
191312d7cc84SAlan Cox 						if (saved_start == start) {
191412d7cc84SAlan Cox 							/*
191512d7cc84SAlan Cox 							 * first_entry has been deleted.
191612d7cc84SAlan Cox 							 */
191712d7cc84SAlan Cox 							vm_map_unlock(map);
191812d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
191912d7cc84SAlan Cox 						}
192012d7cc84SAlan Cox 						end = saved_start;
192112d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
192212d7cc84SAlan Cox 						goto done;
192312d7cc84SAlan Cox 					}
1924cbef13d8SAlan Cox 				}
192512d7cc84SAlan Cox 				if (entry == first_entry)
192612d7cc84SAlan Cox 					first_entry = tmp_entry;
192712d7cc84SAlan Cox 				else
192812d7cc84SAlan Cox 					first_entry = NULL;
192912d7cc84SAlan Cox 				entry = tmp_entry;
193012d7cc84SAlan Cox 			}
193112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
193212d7cc84SAlan Cox 			continue;
193312d7cc84SAlan Cox 		}
193412d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
193512d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
193612d7cc84SAlan Cox 		/*
193712d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
193812d7cc84SAlan Cox 		 * above.)
193912d7cc84SAlan Cox 		 */
194012d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
194112d7cc84SAlan Cox 		/*
19420ada205eSBrian Feldman 		 *
194312d7cc84SAlan Cox 		 */
19440ada205eSBrian Feldman 		if (entry->wired_count == 0) {
19450ada205eSBrian Feldman 			entry->wired_count++;
194612d7cc84SAlan Cox 			saved_start = entry->start;
194712d7cc84SAlan Cox 			saved_end = entry->end;
19484be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
19494be14af9SAlan Cox 			    entry->object.vm_object->type == OBJT_DEVICE;
195012d7cc84SAlan Cox 			/*
195112d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
195212d7cc84SAlan Cox 			 * mark.
195312d7cc84SAlan Cox 			 */
195412d7cc84SAlan Cox 			vm_map_unlock(map);
1955ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
19564be14af9SAlan Cox 			    user_wire, fictitious);
195712d7cc84SAlan Cox 			vm_map_lock(map);
195812d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
195912d7cc84SAlan Cox 				/*
196012d7cc84SAlan Cox 				 * Look again for the entry because the map was
196112d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
196212d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
196312d7cc84SAlan Cox 				 * deleted.
196412d7cc84SAlan Cox 				 */
196512d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
196612d7cc84SAlan Cox 				    &tmp_entry);
196712d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
196812d7cc84SAlan Cox 				if (entry == first_entry)
196912d7cc84SAlan Cox 					first_entry = tmp_entry;
197012d7cc84SAlan Cox 				else
197112d7cc84SAlan Cox 					first_entry = NULL;
197212d7cc84SAlan Cox 				entry = tmp_entry;
197328c58286SAlan Cox 				while (entry->end < saved_end) {
197428c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
197528c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
197628c58286SAlan Cox 						    ("vm_map_wire: bad count"));
197728c58286SAlan Cox 						entry->wired_count = -1;
197828c58286SAlan Cox 					}
197912d7cc84SAlan Cox 					entry = entry->next;
198012d7cc84SAlan Cox 				}
198128c58286SAlan Cox 			}
198212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
198312d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
198428c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
198528c58286SAlan Cox 				    ("vm_map_wire: bad count"));
198612d7cc84SAlan Cox 				/*
198728c58286SAlan Cox 				 * Assign an out-of-range value to represent
198828c58286SAlan Cox 				 * the failure to wire this entry.
198912d7cc84SAlan Cox 				 */
199028c58286SAlan Cox 				entry->wired_count = -1;
199112d7cc84SAlan Cox 				end = entry->end;
199212d7cc84SAlan Cox 				goto done;
199312d7cc84SAlan Cox 			}
19940ada205eSBrian Feldman 		} else if (!user_wire ||
19950ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
19960ada205eSBrian Feldman 			entry->wired_count++;
199712d7cc84SAlan Cox 		}
199812d7cc84SAlan Cox 		/*
199912d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2000abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
200112d7cc84SAlan Cox 		 */
2002abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2003abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2004abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
200512d7cc84SAlan Cox 			end = entry->end;
200612d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
200712d7cc84SAlan Cox 			goto done;
200812d7cc84SAlan Cox 		}
200912d7cc84SAlan Cox 		entry = entry->next;
201012d7cc84SAlan Cox 	}
201112d7cc84SAlan Cox 	rv = KERN_SUCCESS;
201212d7cc84SAlan Cox done:
201312d7cc84SAlan Cox 	need_wakeup = FALSE;
201412d7cc84SAlan Cox 	if (first_entry == NULL) {
201512d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2016cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2017cbef13d8SAlan Cox 			first_entry = first_entry->next;
2018cbef13d8SAlan Cox 		else
201912d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
202012d7cc84SAlan Cox 	}
202112d7cc84SAlan Cox 	entry = first_entry;
202212d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
202312d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
202412d7cc84SAlan Cox 			if (user_wire)
202512d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
202628c58286SAlan Cox 		} else if (entry->wired_count == -1) {
202728c58286SAlan Cox 			/*
202828c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
202928c58286SAlan Cox 			 * unnecessary.
203028c58286SAlan Cox 			 */
203128c58286SAlan Cox 			entry->wired_count = 0;
203212d7cc84SAlan Cox 		} else {
20330ada205eSBrian Feldman 			if (!user_wire ||
20340ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
203512d7cc84SAlan Cox 				entry->wired_count--;
20360ada205eSBrian Feldman 			if (entry->wired_count == 0) {
203712d7cc84SAlan Cox 				/*
203812d7cc84SAlan Cox 				 * Retain the map lock.
203912d7cc84SAlan Cox 				 */
20404be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
20414be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
20424be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
204312d7cc84SAlan Cox 			}
204412d7cc84SAlan Cox 		}
204512d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
204612d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
204712d7cc84SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
204812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
204912d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
205012d7cc84SAlan Cox 			need_wakeup = TRUE;
205112d7cc84SAlan Cox 		}
205212d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
205312d7cc84SAlan Cox 		entry = entry->next;
205412d7cc84SAlan Cox 	}
205512d7cc84SAlan Cox 	vm_map_unlock(map);
205612d7cc84SAlan Cox 	if (need_wakeup)
205712d7cc84SAlan Cox 		vm_map_wakeup(map);
205812d7cc84SAlan Cox 	return (rv);
2059e27e17b7SAlan Cox }
2060e27e17b7SAlan Cox 
2061e27e17b7SAlan Cox /*
2062950f8459SAlan Cox  * vm_map_sync
2063df8bae1dSRodney W. Grimes  *
2064df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2065df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2066df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2067df8bae1dSRodney W. Grimes  *
2068637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2069637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2070637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2071637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2072637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2073637315edSAlan Cox  * flushing the current region containing start.
2074637315edSAlan Cox  *
2075df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2076df8bae1dSRodney W. Grimes  */
2077df8bae1dSRodney W. Grimes int
2078950f8459SAlan Cox vm_map_sync(
20791b40f8c0SMatthew Dillon 	vm_map_t map,
20801b40f8c0SMatthew Dillon 	vm_offset_t start,
20811b40f8c0SMatthew Dillon 	vm_offset_t end,
20821b40f8c0SMatthew Dillon 	boolean_t syncio,
20831b40f8c0SMatthew Dillon 	boolean_t invalidate)
2084df8bae1dSRodney W. Grimes {
2085c0877f10SJohn Dyson 	vm_map_entry_t current;
2086df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2087df8bae1dSRodney W. Grimes 	vm_size_t size;
2088df8bae1dSRodney W. Grimes 	vm_object_t object;
2089a316d390SJohn Dyson 	vm_ooffset_t offset;
2090df8bae1dSRodney W. Grimes 
2091df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2092df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2093df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2094df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2095df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2096637315edSAlan Cox 	} else if (start == end) {
2097637315edSAlan Cox 		start = entry->start;
2098637315edSAlan Cox 		end = entry->end;
2099df8bae1dSRodney W. Grimes 	}
2100df8bae1dSRodney W. Grimes 	/*
2101b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2102df8bae1dSRodney W. Grimes 	 */
2103df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2104b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2105df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2106df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2107df8bae1dSRodney W. Grimes 		}
2108df8bae1dSRodney W. Grimes 		if (end > current->end &&
2109df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2110df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2111df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2112df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2113df8bae1dSRodney W. Grimes 		}
2114df8bae1dSRodney W. Grimes 	}
2115df8bae1dSRodney W. Grimes 
2116bc105a67SAlan Cox 	if (invalidate) {
2117c1fbc251SAlan Cox 		VM_LOCK_GIANT();
2118bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2119c1fbc251SAlan Cox 		VM_UNLOCK_GIANT();
2120bc105a67SAlan Cox 	}
2121df8bae1dSRodney W. Grimes 	/*
2122df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2123df8bae1dSRodney W. Grimes 	 * objects as we go.
2124df8bae1dSRodney W. Grimes 	 */
2125df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2126df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2127df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
21289fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2129c0877f10SJohn Dyson 			vm_map_t smap;
2130df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2131df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2132df8bae1dSRodney W. Grimes 
21339fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2134df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2135df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2136df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2137df8bae1dSRodney W. Grimes 			if (tsize < size)
2138df8bae1dSRodney W. Grimes 				size = tsize;
2139df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2140df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2141df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2142df8bae1dSRodney W. Grimes 		} else {
2143df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2144df8bae1dSRodney W. Grimes 		}
2145950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2146df8bae1dSRodney W. Grimes 		start += size;
2147df8bae1dSRodney W. Grimes 	}
2148df8bae1dSRodney W. Grimes 
2149df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2150df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2151df8bae1dSRodney W. Grimes }
2152df8bae1dSRodney W. Grimes 
2153df8bae1dSRodney W. Grimes /*
2154df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2155df8bae1dSRodney W. Grimes  *
2156df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2157df8bae1dSRodney W. Grimes  *
2158df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2159df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2160df8bae1dSRodney W. Grimes  */
21610362d7d7SJohn Dyson static void
21621b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2163df8bae1dSRodney W. Grimes {
21644be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
21654be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
21664be14af9SAlan Cox 	    entry->object.vm_object->type == OBJT_DEVICE);
2167df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2168df8bae1dSRodney W. Grimes }
2169df8bae1dSRodney W. Grimes 
2170df8bae1dSRodney W. Grimes /*
2171df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2172df8bae1dSRodney W. Grimes  *
2173df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2174df8bae1dSRodney W. Grimes  */
21750362d7d7SJohn Dyson static void
21761b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2177df8bae1dSRodney W. Grimes {
217832a89c32SAlan Cox 	vm_object_t object;
217932a89c32SAlan Cox 	vm_pindex_t offidxstart, offidxend, count;
218032a89c32SAlan Cox 
2181df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
2182df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
2183df8bae1dSRodney W. Grimes 
218432a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
218532a89c32SAlan Cox 	    (object = entry->object.vm_object) != NULL) {
218632a89c32SAlan Cox 		count = OFF_TO_IDX(entry->end - entry->start);
218732a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
218832a89c32SAlan Cox 		offidxend = offidxstart + count;
218932a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
219032a89c32SAlan Cox 		if (object->ref_count != 1 &&
219132a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
219232a89c32SAlan Cox 		     object == kernel_object || object == kmem_object) &&
219332a89c32SAlan Cox 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
219432a89c32SAlan Cox 			vm_object_collapse(object);
2195b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
219632a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
219732a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
219832a89c32SAlan Cox 			if (offidxend >= object->size &&
219932a89c32SAlan Cox 			    offidxstart < object->size)
220032a89c32SAlan Cox 				object->size = offidxstart;
220132a89c32SAlan Cox 		}
220232a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
220332a89c32SAlan Cox 		vm_object_deallocate(object);
2204b5b40fa6SJohn Dyson 	}
2205df8bae1dSRodney W. Grimes 
2206df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
2207df8bae1dSRodney W. Grimes }
2208df8bae1dSRodney W. Grimes 
2209df8bae1dSRodney W. Grimes /*
2210df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2211df8bae1dSRodney W. Grimes  *
2212df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2213df8bae1dSRodney W. Grimes  *	map.
2214df8bae1dSRodney W. Grimes  */
2215df8bae1dSRodney W. Grimes int
22161b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2217df8bae1dSRodney W. Grimes {
2218c0877f10SJohn Dyson 	vm_map_entry_t entry;
2219df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2220df8bae1dSRodney W. Grimes 
2221df8bae1dSRodney W. Grimes 	/*
2222df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2223df8bae1dSRodney W. Grimes 	 */
2224876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2225df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2226876318ecSAlan Cox 	else {
2227df8bae1dSRodney W. Grimes 		entry = first_entry;
2228df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2229df8bae1dSRodney W. Grimes 	}
2230df8bae1dSRodney W. Grimes 
2231df8bae1dSRodney W. Grimes 	/*
2232df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2233df8bae1dSRodney W. Grimes 	 */
2234df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2235df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2236df8bae1dSRodney W. Grimes 
223773b2baceSAlan Cox 		/*
223873b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
22397c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
22407c938963SBrian Feldman 		 * user maps.
224173b2baceSAlan Cox 		 */
22427c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
22437c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
22447c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
224573b2baceSAlan Cox 			unsigned int last_timestamp;
224673b2baceSAlan Cox 			vm_offset_t saved_start;
224773b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
224873b2baceSAlan Cox 
224973b2baceSAlan Cox 			saved_start = entry->start;
225073b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
225173b2baceSAlan Cox 			last_timestamp = map->timestamp;
225273b2baceSAlan Cox 			(void) vm_map_unlock_and_wait(map, FALSE);
225373b2baceSAlan Cox 			vm_map_lock(map);
225473b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
225573b2baceSAlan Cox 				/*
225673b2baceSAlan Cox 				 * Look again for the entry because the map was
225773b2baceSAlan Cox 				 * modified while it was unlocked.
225873b2baceSAlan Cox 				 * Specifically, the entry may have been
225973b2baceSAlan Cox 				 * clipped, merged, or deleted.
226073b2baceSAlan Cox 				 */
226173b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
226273b2baceSAlan Cox 							 &tmp_entry))
226373b2baceSAlan Cox 					entry = tmp_entry->next;
226473b2baceSAlan Cox 				else {
226573b2baceSAlan Cox 					entry = tmp_entry;
226673b2baceSAlan Cox 					vm_map_clip_start(map, entry,
226773b2baceSAlan Cox 							  saved_start);
226873b2baceSAlan Cox 				}
226973b2baceSAlan Cox 			}
227073b2baceSAlan Cox 			continue;
227173b2baceSAlan Cox 		}
2272df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2273df8bae1dSRodney W. Grimes 
2274c0877f10SJohn Dyson 		next = entry->next;
2275df8bae1dSRodney W. Grimes 
2276df8bae1dSRodney W. Grimes 		/*
22770d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
22780d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2279df8bae1dSRodney W. Grimes 		 */
2280c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2281df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2282c0877f10SJohn Dyson 		}
2283df8bae1dSRodney W. Grimes 
228440448065SAlan Cox 		if (!map->system_map)
2285c1fbc251SAlan Cox 			VM_LOCK_GIANT();
228632a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
228740448065SAlan Cox 		if (!map->system_map)
2288c1fbc251SAlan Cox 			VM_UNLOCK_GIANT();
2289df8bae1dSRodney W. Grimes 
2290df8bae1dSRodney W. Grimes 		/*
22910d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
22920d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
22930d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
22940d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2295df8bae1dSRodney W. Grimes 		 */
2296df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2297df8bae1dSRodney W. Grimes 		entry = next;
2298df8bae1dSRodney W. Grimes 	}
2299df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2300df8bae1dSRodney W. Grimes }
2301df8bae1dSRodney W. Grimes 
2302df8bae1dSRodney W. Grimes /*
2303df8bae1dSRodney W. Grimes  *	vm_map_remove:
2304df8bae1dSRodney W. Grimes  *
2305df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2306df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2307df8bae1dSRodney W. Grimes  */
2308df8bae1dSRodney W. Grimes int
23091b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2310df8bae1dSRodney W. Grimes {
23116eaee3feSAlan Cox 	int result;
2312df8bae1dSRodney W. Grimes 
2313df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2314df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2315df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2316df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2317df8bae1dSRodney W. Grimes 	return (result);
2318df8bae1dSRodney W. Grimes }
2319df8bae1dSRodney W. Grimes 
2320df8bae1dSRodney W. Grimes /*
2321df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2322df8bae1dSRodney W. Grimes  *
23232d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
23242d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
23252d5c7e45SMatthew Dillon  *
23262d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
23272d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
23282d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
23292d5c7e45SMatthew Dillon  *
23302d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2331d8834602SAlan Cox  *
2332d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2333df8bae1dSRodney W. Grimes  */
23340d94caffSDavid Greenman boolean_t
2335b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2336b9dcd593SBruce Evans 			vm_prot_t protection)
2337df8bae1dSRodney W. Grimes {
2338c0877f10SJohn Dyson 	vm_map_entry_t entry;
2339df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2340df8bae1dSRodney W. Grimes 
2341d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2342df8bae1dSRodney W. Grimes 		return (FALSE);
2343df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2344df8bae1dSRodney W. Grimes 
2345df8bae1dSRodney W. Grimes 	while (start < end) {
2346d8834602SAlan Cox 		if (entry == &map->header)
2347df8bae1dSRodney W. Grimes 			return (FALSE);
2348df8bae1dSRodney W. Grimes 		/*
2349df8bae1dSRodney W. Grimes 		 * No holes allowed!
2350df8bae1dSRodney W. Grimes 		 */
2351d8834602SAlan Cox 		if (start < entry->start)
2352df8bae1dSRodney W. Grimes 			return (FALSE);
2353df8bae1dSRodney W. Grimes 		/*
2354df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2355df8bae1dSRodney W. Grimes 		 */
2356d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2357df8bae1dSRodney W. Grimes 			return (FALSE);
2358df8bae1dSRodney W. Grimes 		/* go to next entry */
2359df8bae1dSRodney W. Grimes 		start = entry->end;
2360df8bae1dSRodney W. Grimes 		entry = entry->next;
2361df8bae1dSRodney W. Grimes 	}
2362df8bae1dSRodney W. Grimes 	return (TRUE);
2363df8bae1dSRodney W. Grimes }
2364df8bae1dSRodney W. Grimes 
236586524867SJohn Dyson /*
2366df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2367df8bae1dSRodney W. Grimes  *
2368df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2369df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2370df8bae1dSRodney W. Grimes  */
2371f708ef1bSPoul-Henning Kamp static void
23721b40f8c0SMatthew Dillon vm_map_copy_entry(
23731b40f8c0SMatthew Dillon 	vm_map_t src_map,
23741b40f8c0SMatthew Dillon 	vm_map_t dst_map,
23751b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
23761b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2377df8bae1dSRodney W. Grimes {
2378c0877f10SJohn Dyson 	vm_object_t src_object;
2379c0877f10SJohn Dyson 
23809fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2381df8bae1dSRodney W. Grimes 		return;
2382df8bae1dSRodney W. Grimes 
2383df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2384df8bae1dSRodney W. Grimes 
2385df8bae1dSRodney W. Grimes 		/*
23860d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
23870d94caffSDavid Greenman 		 * write-protected.
2388df8bae1dSRodney W. Grimes 		 */
2389afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2390df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2391df8bae1dSRodney W. Grimes 			    src_entry->start,
2392df8bae1dSRodney W. Grimes 			    src_entry->end,
2393df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2394df8bae1dSRodney W. Grimes 		}
2395b18bfc3dSJohn Dyson 
2396df8bae1dSRodney W. Grimes 		/*
2397df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2398df8bae1dSRodney W. Grimes 		 */
23998aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2400a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
2401c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2402c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2403c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2404c0877f10SJohn Dyson 				vm_object_collapse(src_object);
240596fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2406c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2407c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2408a89c6258SAlan Cox 				}
2409a89c6258SAlan Cox 			}
2410b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2411069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2412e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2413c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2414afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2415afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2416b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2417b18bfc3dSJohn Dyson 		} else {
2418b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2419b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2420b18bfc3dSJohn Dyson 		}
2421df8bae1dSRodney W. Grimes 
2422df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2423df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
24240d94caffSDavid Greenman 	} else {
2425df8bae1dSRodney W. Grimes 		/*
2426df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
24270d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
24280d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2429df8bae1dSRodney W. Grimes 		 */
2430df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2431df8bae1dSRodney W. Grimes 	}
2432df8bae1dSRodney W. Grimes }
2433df8bae1dSRodney W. Grimes 
2434df8bae1dSRodney W. Grimes /*
24352a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
24362a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
24372a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
24382a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
24392a7be1b6SBrian Feldman  */
24402a7be1b6SBrian Feldman static void
24412a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
24422a7be1b6SBrian Feldman     vm_map_entry_t entry)
24432a7be1b6SBrian Feldman {
24442a7be1b6SBrian Feldman 	vm_size_t entrysize;
24452a7be1b6SBrian Feldman 	vm_offset_t newend;
24462a7be1b6SBrian Feldman 
24472a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
24482a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
24492a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
24502a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
24512a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
24522a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2453b351299cSAndrew Gallatin 		newend = MIN(entry->end,
24542a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
24552a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
24562a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
24572a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2458b351299cSAndrew Gallatin 		newend = MIN(entry->end,
24592a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
24602a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
24612a7be1b6SBrian Feldman 	}
24622a7be1b6SBrian Feldman }
24632a7be1b6SBrian Feldman 
24642a7be1b6SBrian Feldman /*
2465df8bae1dSRodney W. Grimes  * vmspace_fork:
2466df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2467df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2468df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2469df8bae1dSRodney W. Grimes  * values on the regions in that map.
2470df8bae1dSRodney W. Grimes  *
24712a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
24722a7be1b6SBrian Feldman  *
2473df8bae1dSRodney W. Grimes  * The source map must not be locked.
2474df8bae1dSRodney W. Grimes  */
2475df8bae1dSRodney W. Grimes struct vmspace *
24761b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2477df8bae1dSRodney W. Grimes {
2478c0877f10SJohn Dyson 	struct vmspace *vm2;
2479df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2480df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2481df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2482df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2483de5f6a77SJohn Dyson 	vm_object_t object;
2484df8bae1dSRodney W. Grimes 
24850cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
24860cddd8f0SMatthew Dillon 
2487df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2488df8bae1dSRodney W. Grimes 
24892d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
24902a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
24912a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
24922a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2493df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
249447221757SJohn Dyson 	new_map->timestamp = 1;
2495df8bae1dSRodney W. Grimes 
2496abd498aaSBruce M Simpson 	/* Do not inherit the MAP_WIREFUTURE property. */
2497abd498aaSBruce M Simpson 	if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2498abd498aaSBruce M Simpson 		new_map->flags &= ~MAP_WIREFUTURE;
2499abd498aaSBruce M Simpson 
2500df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2501df8bae1dSRodney W. Grimes 
2502df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2503afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2504df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2505df8bae1dSRodney W. Grimes 
2506df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2507df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2508df8bae1dSRodney W. Grimes 			break;
2509df8bae1dSRodney W. Grimes 
2510df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2511df8bae1dSRodney W. Grimes 			/*
2512fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2513fed9a903SJohn Dyson 			 */
2514fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2515fed9a903SJohn Dyson 			if (object == NULL) {
2516fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2517c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2518fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2519fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
25209a2f6362SAlan Cox 			}
25219a2f6362SAlan Cox 
25229a2f6362SAlan Cox 			/*
25239a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
25249a2f6362SAlan Cox 			 * to insure that a shadow object is created.
25259a2f6362SAlan Cox 			 */
25269a2f6362SAlan Cox 			vm_object_reference(object);
25279a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
25285069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
25295069bf57SJohn Dyson 					&old_entry->offset,
2530c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
25315069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2532d30344bdSIan Dowse 				/* Transfer the second reference too. */
2533d30344bdSIan Dowse 				vm_object_reference(
2534d30344bdSIan Dowse 				    old_entry->object.vm_object);
2535d30344bdSIan Dowse 				vm_object_deallocate(object);
25365069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2537fed9a903SJohn Dyson 			}
2538e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2539069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2540e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2541fed9a903SJohn Dyson 
2542fed9a903SJohn Dyson 			/*
2543ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2544df8bae1dSRodney W. Grimes 			 */
2545df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2546df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2547028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2548df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2549df8bae1dSRodney W. Grimes 
2550df8bae1dSRodney W. Grimes 			/*
25510d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
25520d94caffSDavid Greenman 			 * inserting at the end of the new map.
2553df8bae1dSRodney W. Grimes 			 */
2554df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2555df8bae1dSRodney W. Grimes 			    new_entry);
25562a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2557df8bae1dSRodney W. Grimes 
2558df8bae1dSRodney W. Grimes 			/*
2559df8bae1dSRodney W. Grimes 			 * Update the physical map
2560df8bae1dSRodney W. Grimes 			 */
2561df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2562df8bae1dSRodney W. Grimes 			    new_entry->start,
2563df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2564df8bae1dSRodney W. Grimes 			    old_entry->start);
2565df8bae1dSRodney W. Grimes 			break;
2566df8bae1dSRodney W. Grimes 
2567df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2568df8bae1dSRodney W. Grimes 			/*
2569df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2570df8bae1dSRodney W. Grimes 			 */
2571df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2572df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2573028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2574df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2575df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2576df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2577df8bae1dSRodney W. Grimes 			    new_entry);
25782a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2579bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2580bd7e5f99SJohn Dyson 			    new_entry);
2581df8bae1dSRodney W. Grimes 			break;
2582df8bae1dSRodney W. Grimes 		}
2583df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2584df8bae1dSRodney W. Grimes 	}
2585df8bae1dSRodney W. Grimes 
2586df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2587df8bae1dSRodney W. Grimes 
2588df8bae1dSRodney W. Grimes 	return (vm2);
2589df8bae1dSRodney W. Grimes }
2590df8bae1dSRodney W. Grimes 
259194f7e29aSAlan Cox int
259294f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
259394f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
259494f7e29aSAlan Cox {
2595fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2596fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
259794f7e29aSAlan Cox 	vm_size_t init_ssize;
2598fd75d710SMarcel Moolenaar 	int orient, rv;
259991d5354aSJohn Baldwin 	rlim_t vmemlim;
260094f7e29aSAlan Cox 
2601fd75d710SMarcel Moolenaar 	/*
2602fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2603fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2604fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2605fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2606fd75d710SMarcel Moolenaar 	 */
2607fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2608fd75d710SMarcel Moolenaar 	cow &= ~orient;
2609fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2610fd75d710SMarcel Moolenaar 
2611fd75d710SMarcel Moolenaar 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
261294f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2613fd75d710SMarcel Moolenaar 
2614fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
261594f7e29aSAlan Cox 
261691d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
261791d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
261891d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
261991d5354aSJohn Baldwin 
262094f7e29aSAlan Cox 	vm_map_lock(map);
262194f7e29aSAlan Cox 
262294f7e29aSAlan Cox 	/* If addr is already mapped, no go */
262394f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
262494f7e29aSAlan Cox 		vm_map_unlock(map);
262594f7e29aSAlan Cox 		return (KERN_NO_SPACE);
262694f7e29aSAlan Cox 	}
262794f7e29aSAlan Cox 
2628a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
262991d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2630a69ac174SMatthew Dillon 		vm_map_unlock(map);
2631a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2632a69ac174SMatthew Dillon 	}
2633a69ac174SMatthew Dillon 
2634fd75d710SMarcel Moolenaar 	/*
2635fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2636fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2637fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2638fd75d710SMarcel Moolenaar 	 * space is not protected.
263994f7e29aSAlan Cox 	 *
2640fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2641fd75d710SMarcel Moolenaar 	 * grow the stack.
264294f7e29aSAlan Cox 	 */
264394f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
264494f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
264594f7e29aSAlan Cox 		vm_map_unlock(map);
264694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
264794f7e29aSAlan Cox 	}
264894f7e29aSAlan Cox 
2649fd75d710SMarcel Moolenaar 	/*
2650fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2651fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2652fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2653fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
265494f7e29aSAlan Cox 	 *
2655fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2656fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2657fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
265894f7e29aSAlan Cox 	 */
2659fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2660fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2661fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2662fd75d710SMarcel Moolenaar 		bot = addrbos;
2663fd75d710SMarcel Moolenaar 	else
2664fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2665fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2666fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
266794f7e29aSAlan Cox 
2668fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
266994f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
267029b45e9eSAlan Cox 		if (prev_entry != &map->header)
2671fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2672fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2673fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
267494f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2675b21a0008SMarcel Moolenaar 
2676fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2677fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2678fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2679fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2680fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
268194f7e29aSAlan Cox 	}
268294f7e29aSAlan Cox 
268394f7e29aSAlan Cox 	vm_map_unlock(map);
268494f7e29aSAlan Cox 	return (rv);
268594f7e29aSAlan Cox }
268694f7e29aSAlan Cox 
268794f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
268894f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
268994f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
269094f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
269194f7e29aSAlan Cox  * the grow function in vm_machdep.c).
269294f7e29aSAlan Cox  */
269394f7e29aSAlan Cox int
269494f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
269594f7e29aSAlan Cox {
2696b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2697b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
269894f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
269994f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
270094f7e29aSAlan Cox 	vm_offset_t end;
2701b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
270291d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2703b21a0008SMarcel Moolenaar 	int is_procstack, rv;
270423955314SAlfred Perlstein 
270594f7e29aSAlan Cox Retry:
270691d5354aSJohn Baldwin 	PROC_LOCK(p);
270791d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2708bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
270991d5354aSJohn Baldwin 	PROC_UNLOCK(p);
271091d5354aSJohn Baldwin 
271194f7e29aSAlan Cox 	vm_map_lock_read(map);
271294f7e29aSAlan Cox 
271394f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
271494f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
271594f7e29aSAlan Cox 		vm_map_unlock_read(map);
27160cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
271794f7e29aSAlan Cox 	}
271894f7e29aSAlan Cox 
2719b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2720b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2721b21a0008SMarcel Moolenaar 		/*
2722b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2723b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2724b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2725b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
272694f7e29aSAlan Cox 		 */
2727b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
272894f7e29aSAlan Cox 			vm_map_unlock_read(map);
27290cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
273094f7e29aSAlan Cox 		}
2731b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2732b21a0008SMarcel Moolenaar 	} else {
2733b21a0008SMarcel Moolenaar 		/*
2734b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2735b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2736b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2737b21a0008SMarcel Moolenaar 		 * select one.
2738b21a0008SMarcel Moolenaar 		 */
2739b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2740b21a0008SMarcel Moolenaar 			/*
2741b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2742b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2743b21a0008SMarcel Moolenaar 			 */
2744b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
2745b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
2746b21a0008SMarcel Moolenaar 			else
2747b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
2748b21a0008SMarcel Moolenaar 		} else
2749b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
2750b21a0008SMarcel Moolenaar 	}
275194f7e29aSAlan Cox 
2752b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2753b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2754b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
2755b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
2756b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
275794f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2758b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
2759b21a0008SMarcel Moolenaar 	} else {
2760b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
276108667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
2762b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
2763b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
2764fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2765b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
2766b21a0008SMarcel Moolenaar 	}
2767b21a0008SMarcel Moolenaar 
276894f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
276994f7e29aSAlan Cox 		vm_map_unlock_read(map);
27700cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
277194f7e29aSAlan Cox 	}
277294f7e29aSAlan Cox 
2773b21a0008SMarcel Moolenaar 	/*
2774b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
2775b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
2776b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
2777b21a0008SMarcel Moolenaar 	 * and is probably an error.
277894f7e29aSAlan Cox 	 *
2779b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
2780b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
278194f7e29aSAlan Cox 	 */
2782b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
278325adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
278494f7e29aSAlan Cox 			goto Retry;
278594f7e29aSAlan Cox 
2786b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
278794f7e29aSAlan Cox 
278894f7e29aSAlan Cox 		vm_map_unlock(map);
27890cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
279094f7e29aSAlan Cox 	}
279194f7e29aSAlan Cox 
2792b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
279394f7e29aSAlan Cox 
2794b21a0008SMarcel Moolenaar 	/*
2795b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
2796b21a0008SMarcel Moolenaar 	 * limit.
279794f7e29aSAlan Cox 	 */
279891d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
279994f7e29aSAlan Cox 		vm_map_unlock_read(map);
28000cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
280194f7e29aSAlan Cox 	}
280294f7e29aSAlan Cox 
280394f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2804cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
2805b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
280694f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
280791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
280891d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
280994f7e29aSAlan Cox 	}
281094f7e29aSAlan Cox 
2811a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
281291d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
2813a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
2814a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2815a69ac174SMatthew Dillon 	}
2816a69ac174SMatthew Dillon 
281725adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
281894f7e29aSAlan Cox 		goto Retry;
281994f7e29aSAlan Cox 
2820b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2821b21a0008SMarcel Moolenaar 		/*
2822b21a0008SMarcel Moolenaar 		 * Growing downward.
2823b21a0008SMarcel Moolenaar 		 */
282494f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
282594f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
282694f7e29aSAlan Cox 
2827b21a0008SMarcel Moolenaar 		/*
2828b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
2829b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
283094f7e29aSAlan Cox 		 */
283194f7e29aSAlan Cox 		if (addr < end) {
2832b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
283394f7e29aSAlan Cox 			addr = end;
283494f7e29aSAlan Cox 		}
283594f7e29aSAlan Cox 
283694f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
283705ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
283894f7e29aSAlan Cox 
283994f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
284094f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
284129b45e9eSAlan Cox 			if (prev_entry != &map->header)
284229b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
2843b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
2844b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2845b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2846b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
2847b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
2848b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
2849b21a0008SMarcel Moolenaar 			    grow_amount;
2850b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2851b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
285294f7e29aSAlan Cox 		}
2853b21a0008SMarcel Moolenaar 	} else {
2854b21a0008SMarcel Moolenaar 		/*
2855b21a0008SMarcel Moolenaar 		 * Growing upward.
2856b21a0008SMarcel Moolenaar 		 */
2857b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
2858b21a0008SMarcel Moolenaar 
2859b21a0008SMarcel Moolenaar 		/*
2860b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
2861b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
2862b21a0008SMarcel Moolenaar 		 */
2863b21a0008SMarcel Moolenaar 		if (addr > end) {
2864b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
2865b21a0008SMarcel Moolenaar 			addr = end;
286694f7e29aSAlan Cox 		}
286794f7e29aSAlan Cox 
2868b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
2869b21a0008SMarcel Moolenaar 
2870b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
2871b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
2872b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
287357a21abaSAlan Cox 		    stack_entry->offset,
2874b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2875b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
287608667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
2877b21a0008SMarcel Moolenaar 			/* Update the current entry. */
2878b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
2879199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
28800164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
2881b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
2882b21a0008SMarcel Moolenaar 
2883b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
2884b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
2885b21a0008SMarcel Moolenaar 		} else
2886b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
2887b21a0008SMarcel Moolenaar 	}
2888b21a0008SMarcel Moolenaar 
2889b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
2890b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
2891b21a0008SMarcel Moolenaar 
289294f7e29aSAlan Cox 	vm_map_unlock(map);
2893b21a0008SMarcel Moolenaar 
2894abd498aaSBruce M Simpson 	/*
2895abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2896abd498aaSBruce M Simpson 	 */
2897b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2898b21a0008SMarcel Moolenaar 		vm_map_wire(map,
2899b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2900b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2901b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
2902b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2903b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2904b21a0008SMarcel Moolenaar 	}
2905abd498aaSBruce M Simpson 
29060cddd8f0SMatthew Dillon 	return (rv);
290794f7e29aSAlan Cox }
290894f7e29aSAlan Cox 
2909df8bae1dSRodney W. Grimes /*
29105856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
29115856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
29125856e12eSJohn Dyson  */
29135856e12eSJohn Dyson void
29143ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
29151b40f8c0SMatthew Dillon {
29165856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
29175856e12eSJohn Dyson 	struct vmspace *newvmspace;
29185856e12eSJohn Dyson 
29190cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
29203ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
292151ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
29225856e12eSJohn Dyson 	/*
29235856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
29245856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
29255856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
29265856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
29275856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
29285856e12eSJohn Dyson 	 */
29295856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2930b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2931b40ce416SJulian Elischer 		pmap_activate(curthread);
2932b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
29335856e12eSJohn Dyson }
29345856e12eSJohn Dyson 
29355856e12eSJohn Dyson /*
29365856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
29375856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
29385856e12eSJohn Dyson  */
29395856e12eSJohn Dyson void
29401b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
29411b40f8c0SMatthew Dillon {
29425856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
29435856e12eSJohn Dyson 	struct vmspace *newvmspace;
29445856e12eSJohn Dyson 
29450cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
29465856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
29475856e12eSJohn Dyson 		return;
29485856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
29495856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2950b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2951b40ce416SJulian Elischer 		pmap_activate(curthread);
2952b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
29535856e12eSJohn Dyson }
29545856e12eSJohn Dyson 
29555856e12eSJohn Dyson /*
2956df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2957df8bae1dSRodney W. Grimes  *
2958df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2959df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2960df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2961df8bae1dSRodney W. Grimes  *	type specified.
2962df8bae1dSRodney W. Grimes  *
2963df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2964df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2965df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2966df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2967df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2968df8bae1dSRodney W. Grimes  *
2969df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2970df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2971df8bae1dSRodney W. Grimes  *
2972df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2973df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2974df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2975df8bae1dSRodney W. Grimes  *	remain the same.
2976df8bae1dSRodney W. Grimes  */
2977df8bae1dSRodney W. Grimes int
2978b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2979b9dcd593SBruce Evans 	      vm_offset_t vaddr,
298047221757SJohn Dyson 	      vm_prot_t fault_typea,
2981b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2982b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2983b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2984b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
29852d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2986df8bae1dSRodney W. Grimes {
2987c0877f10SJohn Dyson 	vm_map_entry_t entry;
2988c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2989c0877f10SJohn Dyson 	vm_prot_t prot;
299047221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2991df8bae1dSRodney W. Grimes 
2992df8bae1dSRodney W. Grimes RetryLookup:;
2993df8bae1dSRodney W. Grimes 	/*
2994df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2995df8bae1dSRodney W. Grimes 	 */
2996df8bae1dSRodney W. Grimes 
2997df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2998df8bae1dSRodney W. Grimes #define	RETURN(why) \
2999df8bae1dSRodney W. Grimes 		{ \
3000df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
3001df8bae1dSRodney W. Grimes 		return (why); \
3002df8bae1dSRodney W. Grimes 		}
3003df8bae1dSRodney W. Grimes 
3004df8bae1dSRodney W. Grimes 	/*
30050d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
30060d94caffSDavid Greenman 	 * blown lookup routine.
3007df8bae1dSRodney W. Grimes 	 */
30084e94f402SAlan Cox 	entry = map->root;
3009df8bae1dSRodney W. Grimes 	*out_entry = entry;
30104e94f402SAlan Cox 	if (entry == NULL ||
3011df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3012df8bae1dSRodney W. Grimes 		/*
30130d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
30140d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
3015df8bae1dSRodney W. Grimes 		 */
30164e94f402SAlan Cox 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3017df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
3018df8bae1dSRodney W. Grimes 
30194e94f402SAlan Cox 		entry = *out_entry;
3020df8bae1dSRodney W. Grimes 	}
3021b7b2aac2SJohn Dyson 
3022df8bae1dSRodney W. Grimes 	/*
3023df8bae1dSRodney W. Grimes 	 * Handle submaps.
3024df8bae1dSRodney W. Grimes 	 */
3025afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3026df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3027df8bae1dSRodney W. Grimes 
3028df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3029df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3030df8bae1dSRodney W. Grimes 		goto RetryLookup;
3031df8bae1dSRodney W. Grimes 	}
3032a04c970aSJohn Dyson 
3033df8bae1dSRodney W. Grimes 	/*
30340d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3035a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
3036a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
3037a04c970aSJohn Dyson 	 * COW for debuggers.
3038df8bae1dSRodney W. Grimes 	 */
3039480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3040480ba2f5SJohn Dyson 		prot = entry->max_protection;
3041480ba2f5SJohn Dyson 	else
3042df8bae1dSRodney W. Grimes 		prot = entry->protection;
304347221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
304447221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
304547221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
304647221757SJohn Dyson 	}
30472ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
304847221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
30492ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
305047221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3051df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
3052a04c970aSJohn Dyson 	}
3053df8bae1dSRodney W. Grimes 
3054df8bae1dSRodney W. Grimes 	/*
30550d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
30560d94caffSDavid Greenman 	 * accesses.
3057df8bae1dSRodney W. Grimes 	 */
305805f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
305905f0fdd2SPoul-Henning Kamp 	if (*wired)
3060df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
3061df8bae1dSRodney W. Grimes 
3062df8bae1dSRodney W. Grimes 	/*
3063df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3064df8bae1dSRodney W. Grimes 	 */
3065afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3066df8bae1dSRodney W. Grimes 		/*
30670d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3068ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3069df8bae1dSRodney W. Grimes 		 *
30700d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
30710d94caffSDavid Greenman 		 * permissions allowed.
3072df8bae1dSRodney W. Grimes 		 */
3073df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
3074df8bae1dSRodney W. Grimes 			/*
30750d94caffSDavid Greenman 			 * Make a new object, and place it in the object
30760d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3077ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
30780d94caffSDavid Greenman 			 * object.
3079df8bae1dSRodney W. Grimes 			 */
308025adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3081df8bae1dSRodney W. Grimes 				goto RetryLookup;
30829917e010SAlan Cox 
3083df8bae1dSRodney W. Grimes 			vm_object_shadow(
3084df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3085df8bae1dSRodney W. Grimes 			    &entry->offset,
3086c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
3087afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
30889917e010SAlan Cox 
30899b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
30900d94caffSDavid Greenman 		} else {
3091df8bae1dSRodney W. Grimes 			/*
30920d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
30930d94caffSDavid Greenman 			 * don't allow writes.
3094df8bae1dSRodney W. Grimes 			 */
30952d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3096df8bae1dSRodney W. Grimes 		}
3097df8bae1dSRodney W. Grimes 	}
30982d8acc0fSJohn Dyson 
3099df8bae1dSRodney W. Grimes 	/*
3100df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3101df8bae1dSRodney W. Grimes 	 */
31024e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
31034e71e795SMatthew Dillon 	    !map->system_map) {
310425adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3105df8bae1dSRodney W. Grimes 			goto RetryLookup;
310624a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3107c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
3108df8bae1dSRodney W. Grimes 		entry->offset = 0;
31099b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3110df8bae1dSRodney W. Grimes 	}
3111b5b40fa6SJohn Dyson 
3112df8bae1dSRodney W. Grimes 	/*
31130d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
31140d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3115df8bae1dSRodney W. Grimes 	 */
31169b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3117df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3118df8bae1dSRodney W. Grimes 
3119df8bae1dSRodney W. Grimes 	*out_prot = prot;
3120df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3121df8bae1dSRodney W. Grimes 
3122df8bae1dSRodney W. Grimes #undef	RETURN
3123df8bae1dSRodney W. Grimes }
3124df8bae1dSRodney W. Grimes 
3125df8bae1dSRodney W. Grimes /*
312619dc5607STor Egge  *	vm_map_lookup_locked:
312719dc5607STor Egge  *
312819dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
312919dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
313019dc5607STor Egge  */
313119dc5607STor Egge int
313219dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
313319dc5607STor Egge 		     vm_offset_t vaddr,
313419dc5607STor Egge 		     vm_prot_t fault_typea,
313519dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
313619dc5607STor Egge 		     vm_object_t *object,	/* OUT */
313719dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
313819dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
313919dc5607STor Egge 		     boolean_t *wired)		/* OUT */
314019dc5607STor Egge {
314119dc5607STor Egge 	vm_map_entry_t entry;
314219dc5607STor Egge 	vm_map_t map = *var_map;
314319dc5607STor Egge 	vm_prot_t prot;
314419dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
314519dc5607STor Egge 
314619dc5607STor Egge 	/*
314719dc5607STor Egge 	 * If the map has an interesting hint, try it before calling full
314819dc5607STor Egge 	 * blown lookup routine.
314919dc5607STor Egge 	 */
315019dc5607STor Egge 	entry = map->root;
315119dc5607STor Egge 	*out_entry = entry;
315219dc5607STor Egge 	if (entry == NULL ||
315319dc5607STor Egge 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
315419dc5607STor Egge 		/*
315519dc5607STor Egge 		 * Entry was either not a valid hint, or the vaddr was not
315619dc5607STor Egge 		 * contained in the entry, so do a full lookup.
315719dc5607STor Egge 		 */
315819dc5607STor Egge 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
315919dc5607STor Egge 			return (KERN_INVALID_ADDRESS);
316019dc5607STor Egge 
316119dc5607STor Egge 		entry = *out_entry;
316219dc5607STor Egge 	}
316319dc5607STor Egge 
316419dc5607STor Egge 	/*
316519dc5607STor Egge 	 * Fail if the entry refers to a submap.
316619dc5607STor Egge 	 */
316719dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
316819dc5607STor Egge 		return (KERN_FAILURE);
316919dc5607STor Egge 
317019dc5607STor Egge 	/*
317119dc5607STor Egge 	 * Check whether this task is allowed to have this page.
317219dc5607STor Egge 	 * Note the special case for MAP_ENTRY_COW
317319dc5607STor Egge 	 * pages with an override.  This is to implement a forced
317419dc5607STor Egge 	 * COW for debuggers.
317519dc5607STor Egge 	 */
317619dc5607STor Egge 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
317719dc5607STor Egge 		prot = entry->max_protection;
317819dc5607STor Egge 	else
317919dc5607STor Egge 		prot = entry->protection;
318019dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
318119dc5607STor Egge 	if ((fault_type & prot) != fault_type)
318219dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
318319dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
318419dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
318519dc5607STor Egge 	    (fault_type & VM_PROT_WRITE) &&
318619dc5607STor Egge 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
318719dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
318819dc5607STor Egge 
318919dc5607STor Egge 	/*
319019dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
319119dc5607STor Egge 	 * accesses.
319219dc5607STor Egge 	 */
319319dc5607STor Egge 	*wired = (entry->wired_count != 0);
319419dc5607STor Egge 	if (*wired)
319519dc5607STor Egge 		prot = fault_type = entry->protection;
319619dc5607STor Egge 
319719dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
319819dc5607STor Egge 		/*
319919dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
320019dc5607STor Egge 		 */
320119dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
320219dc5607STor Egge 			return (KERN_FAILURE);
320319dc5607STor Egge 		/*
320419dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
320519dc5607STor Egge 		 * don't allow writes.
320619dc5607STor Egge 		 */
320719dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
320819dc5607STor Egge 	}
320919dc5607STor Egge 
321019dc5607STor Egge 	/*
321119dc5607STor Egge 	 * Fail if an object should be created.
321219dc5607STor Egge 	 */
321319dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
321419dc5607STor Egge 		return (KERN_FAILURE);
321519dc5607STor Egge 
321619dc5607STor Egge 	/*
321719dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
321819dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
321919dc5607STor Egge 	 */
322019dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
322119dc5607STor Egge 	*object = entry->object.vm_object;
322219dc5607STor Egge 
322319dc5607STor Egge 	*out_prot = prot;
322419dc5607STor Egge 	return (KERN_SUCCESS);
322519dc5607STor Egge }
322619dc5607STor Egge 
322719dc5607STor Egge /*
3228df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3229df8bae1dSRodney W. Grimes  *
3230df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3231df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3232df8bae1dSRodney W. Grimes  */
32330d94caffSDavid Greenman void
32341b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3235df8bae1dSRodney W. Grimes {
3236df8bae1dSRodney W. Grimes 	/*
3237df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3238df8bae1dSRodney W. Grimes 	 */
3239df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3240df8bae1dSRodney W. Grimes }
3241df8bae1dSRodney W. Grimes 
3242c7c34a24SBruce Evans #include "opt_ddb.h"
3243c3cb3e12SDavid Greenman #ifdef DDB
3244c7c34a24SBruce Evans #include <sys/kernel.h>
3245c7c34a24SBruce Evans 
3246c7c34a24SBruce Evans #include <ddb/ddb.h>
3247c7c34a24SBruce Evans 
3248df8bae1dSRodney W. Grimes /*
3249df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3250df8bae1dSRodney W. Grimes  */
3251c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3252df8bae1dSRodney W. Grimes {
325395e5e988SJohn Dyson 	static int nlines;
3254c7c34a24SBruce Evans 	/* XXX convert args. */
3255c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3256c7c34a24SBruce Evans 	boolean_t full = have_addr;
3257df8bae1dSRodney W. Grimes 
3258c0877f10SJohn Dyson 	vm_map_entry_t entry;
3259c7c34a24SBruce Evans 
3260e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3261e5f251d2SAlan Cox 	    (void *)map,
3262101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
326395e5e988SJohn Dyson 	nlines++;
3264df8bae1dSRodney W. Grimes 
3265c7c34a24SBruce Evans 	if (!full && db_indent)
3266df8bae1dSRodney W. Grimes 		return;
3267df8bae1dSRodney W. Grimes 
3268c7c34a24SBruce Evans 	db_indent += 2;
3269df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3270df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3271fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3272fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
327395e5e988SJohn Dyson 		nlines++;
3274e5f251d2SAlan Cox 		{
3275df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3276df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
32770d94caffSDavid Greenman 
327895e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3279df8bae1dSRodney W. Grimes 			    entry->protection,
3280df8bae1dSRodney W. Grimes 			    entry->max_protection,
32818aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3282df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
328395e5e988SJohn Dyson 				db_printf(", wired");
3284df8bae1dSRodney W. Grimes 		}
32859fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3286cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
32879fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3288cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
328995e5e988SJohn Dyson 			nlines++;
3290df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
32919fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
32929fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3293c7c34a24SBruce Evans 				db_indent += 2;
3294101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
32959fdfe602SMatthew Dillon 					     entry->object.sub_map,
3296914181e7SBruce Evans 					     full, 0, (char *)0);
3297c7c34a24SBruce Evans 				db_indent -= 2;
3298df8bae1dSRodney W. Grimes 			}
32990d94caffSDavid Greenman 		} else {
3300cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3301101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3302cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3303afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3304c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3305afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3306c7c34a24SBruce Evans 			db_printf("\n");
330795e5e988SJohn Dyson 			nlines++;
3308df8bae1dSRodney W. Grimes 
3309df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3310df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3311df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3312c7c34a24SBruce Evans 				db_indent += 2;
3313101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3314101eeb7fSBruce Evans 						entry->object.vm_object,
3315914181e7SBruce Evans 						full, 0, (char *)0);
331695e5e988SJohn Dyson 				nlines += 4;
3317c7c34a24SBruce Evans 				db_indent -= 2;
3318df8bae1dSRodney W. Grimes 			}
3319df8bae1dSRodney W. Grimes 		}
3320df8bae1dSRodney W. Grimes 	}
3321c7c34a24SBruce Evans 	db_indent -= 2;
332295e5e988SJohn Dyson 	if (db_indent == 0)
332395e5e988SJohn Dyson 		nlines = 0;
3324df8bae1dSRodney W. Grimes }
332595e5e988SJohn Dyson 
332695e5e988SJohn Dyson 
332795e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
332895e5e988SJohn Dyson {
332995e5e988SJohn Dyson 	struct proc *p;
333095e5e988SJohn Dyson 
333195e5e988SJohn Dyson 	if (have_addr) {
333295e5e988SJohn Dyson 		p = (struct proc *) addr;
333395e5e988SJohn Dyson 	} else {
333495e5e988SJohn Dyson 		p = curproc;
333595e5e988SJohn Dyson 	}
333695e5e988SJohn Dyson 
3337ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3338ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3339b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
334095e5e988SJohn Dyson 
3341101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
334295e5e988SJohn Dyson }
334395e5e988SJohn Dyson 
3344c7c34a24SBruce Evans #endif /* DDB */
3345