xref: /freebsd/sys/vm/vm_map.c (revision 997e1c252ba74adae8bf82d67dcaf7bb593824db)
160727d8bSWarner Losh /*-
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);
169df8bae1dSRodney W. Grimes }
170df8bae1dSRodney W. Grimes 
1718355f576SJeff Roberson static void
1728355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1738355f576SJeff Roberson {
1748355f576SJeff Roberson 	struct vmspace *vm;
1758355f576SJeff Roberson 
1768355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1773f25cbddSPeter Wemm 	pmap_release(vmspace_pmap(vm));
1788355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
1798355f576SJeff Roberson }
1808355f576SJeff Roberson 
181b23f72e9SBrian Feldman static int
182b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
1838355f576SJeff Roberson {
1848355f576SJeff Roberson 	struct vmspace *vm;
1858355f576SJeff Roberson 
1868355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1878355f576SJeff Roberson 
188b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
1893f25cbddSPeter Wemm 	pmap_pinit(vmspace_pmap(vm));
190b23f72e9SBrian Feldman 	return (0);
1918355f576SJeff Roberson }
1928355f576SJeff Roberson 
1938355f576SJeff Roberson static void
1948355f576SJeff Roberson vm_map_zfini(void *mem, int size)
1958355f576SJeff Roberson {
1968355f576SJeff Roberson 	vm_map_t map;
1978355f576SJeff Roberson 
1988355f576SJeff Roberson 	map = (vm_map_t)mem;
19936daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
20012c64974SMaxime Henrion 	sx_destroy(&map->lock);
2018355f576SJeff Roberson }
2028355f576SJeff Roberson 
203b23f72e9SBrian Feldman static int
204b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2058355f576SJeff Roberson {
2068355f576SJeff Roberson 	vm_map_t map;
2078355f576SJeff Roberson 
2088355f576SJeff Roberson 	map = (vm_map_t)mem;
2098355f576SJeff Roberson 	map->nentries = 0;
2108355f576SJeff Roberson 	map->size = 0;
211d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
21212c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
213b23f72e9SBrian Feldman 	return (0);
2148355f576SJeff Roberson }
2158355f576SJeff Roberson 
2168355f576SJeff Roberson #ifdef INVARIANTS
2178355f576SJeff Roberson static void
2188355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2198355f576SJeff Roberson {
2208355f576SJeff Roberson 	struct vmspace *vm;
2218355f576SJeff Roberson 
2228355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2238355f576SJeff Roberson 
2248355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2258355f576SJeff Roberson }
2268355f576SJeff Roberson static void
2278355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2288355f576SJeff Roberson {
2298355f576SJeff Roberson 	vm_map_t map;
2308355f576SJeff Roberson 
2318355f576SJeff Roberson 	map = (vm_map_t)mem;
2328355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2338355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2348355f576SJeff Roberson 	    map, map->nentries));
2358355f576SJeff Roberson 	KASSERT(map->size == 0,
2368355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2379eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2388355f576SJeff Roberson }
2398355f576SJeff Roberson #endif	/* INVARIANTS */
2408355f576SJeff Roberson 
241df8bae1dSRodney W. Grimes /*
242df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
243df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
244df8bae1dSRodney W. Grimes  */
245df8bae1dSRodney W. Grimes struct vmspace *
2462d8acc0fSJohn Dyson vmspace_alloc(min, max)
247df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
248df8bae1dSRodney W. Grimes {
249c0877f10SJohn Dyson 	struct vmspace *vm;
2500d94caffSDavid Greenman 
251a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
25221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
2538355f576SJeff Roberson 	_vm_map_init(&vm->vm_map, min, max);
254b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
255df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2562d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
25751ab6c28SAlan Cox 	vm->vm_swrss = 0;
25851ab6c28SAlan Cox 	vm->vm_tsize = 0;
25951ab6c28SAlan Cox 	vm->vm_dsize = 0;
26051ab6c28SAlan Cox 	vm->vm_ssize = 0;
26151ab6c28SAlan Cox 	vm->vm_taddr = 0;
26251ab6c28SAlan Cox 	vm->vm_daddr = 0;
26351ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
264389d2b6eSMatthew Dillon 	vm->vm_exitingcnt = 0;
265df8bae1dSRodney W. Grimes 	return (vm);
266df8bae1dSRodney W. Grimes }
267df8bae1dSRodney W. Grimes 
268df8bae1dSRodney W. Grimes void
2691b40f8c0SMatthew Dillon vm_init2(void)
2701b40f8c0SMatthew Dillon {
2719e7c1bceSPeter Wemm 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
2723fde38dfSMike Silbersack 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
2733fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
2748355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2758355f576SJeff Roberson #ifdef INVARIANTS
2768355f576SJeff Roberson 	    vmspace_zdtor,
2778355f576SJeff Roberson #else
2788355f576SJeff Roberson 	    NULL,
2798355f576SJeff Roberson #endif
2808355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2813075778bSJohn Dyson }
2823075778bSJohn Dyson 
283582ec34cSAlfred Perlstein static __inline void
284582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
285df8bae1dSRodney W. Grimes {
28621c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
2873db161e0SMatthew Dillon 
2883db161e0SMatthew Dillon 	/*
2893db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
2903db161e0SMatthew Dillon 	 * exit1().
2913db161e0SMatthew Dillon 	 */
2923db161e0SMatthew Dillon 	shmexit(vm);
2933db161e0SMatthew Dillon 
29430dcfc09SJohn Dyson 	/*
295df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
2960d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
2970d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
298df8bae1dSRodney W. Grimes 	 */
299717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
300df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3018355f576SJeff Roberson 
3028355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
303df8bae1dSRodney W. Grimes }
304582ec34cSAlfred Perlstein 
305582ec34cSAlfred Perlstein void
306582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
307582ec34cSAlfred Perlstein {
3081a276a3fSAlan Cox 	int refcnt;
309582ec34cSAlfred Perlstein 
310582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
311582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
312582ec34cSAlfred Perlstein 
3131a276a3fSAlan Cox 	do
3141a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3151a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
3161a276a3fSAlan Cox 	if (refcnt == 1 && vm->vm_exitingcnt == 0)
317582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
318582ec34cSAlfred Perlstein }
319582ec34cSAlfred Perlstein 
320582ec34cSAlfred Perlstein void
321582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
322582ec34cSAlfred Perlstein {
323334f7061SPeter Wemm 	struct vmspace *vm;
3241a276a3fSAlan Cox 	int exitingcnt;
325582ec34cSAlfred Perlstein 
326334f7061SPeter Wemm 	vm = p->p_vmspace;
327334f7061SPeter Wemm 	p->p_vmspace = NULL;
328389d2b6eSMatthew Dillon 
329389d2b6eSMatthew Dillon 	/*
330389d2b6eSMatthew Dillon 	 * cleanup by parent process wait()ing on exiting child.  vm_refcnt
331389d2b6eSMatthew Dillon 	 * may not be 0 (e.g. fork() and child exits without exec()ing).
332389d2b6eSMatthew Dillon 	 * exitingcnt may increment above 0 and drop back down to zero
333389d2b6eSMatthew Dillon 	 * several times while vm_refcnt is held non-zero.  vm_refcnt
334389d2b6eSMatthew Dillon 	 * may also increment above 0 and drop back down to zero several
335389d2b6eSMatthew Dillon 	 * times while vm_exitingcnt is held non-zero.
336389d2b6eSMatthew Dillon 	 *
337389d2b6eSMatthew Dillon 	 * The last wait on the exiting child's vmspace will clean up
338389d2b6eSMatthew Dillon 	 * the remainder of the vmspace.
339389d2b6eSMatthew Dillon 	 */
3401a276a3fSAlan Cox 	do
3411a276a3fSAlan Cox 		exitingcnt = vm->vm_exitingcnt;
3421a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_exitingcnt, exitingcnt,
3431a276a3fSAlan Cox 	    exitingcnt - 1));
3441a276a3fSAlan Cox 	if (vm->vm_refcnt == 0 && exitingcnt == 1)
345334f7061SPeter Wemm 		vmspace_dofree(vm);
346334f7061SPeter Wemm }
347df8bae1dSRodney W. Grimes 
3481b40f8c0SMatthew Dillon void
349780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
3501b40f8c0SMatthew Dillon {
351bc91c510SAlan Cox 
35293bc4879SAlan Cox 	if (map->system_map)
35336daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
35412c64974SMaxime Henrion 	else
35512c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
3561b40f8c0SMatthew Dillon 	map->timestamp++;
3571b40f8c0SMatthew Dillon }
3581b40f8c0SMatthew Dillon 
3591b40f8c0SMatthew Dillon void
360780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
3610e0af8ecSBrian Feldman {
362bc91c510SAlan Cox 
36336daaecdSAlan Cox 	if (map->system_map)
36436daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
36536daaecdSAlan Cox 	else
36612c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
3670e0af8ecSBrian Feldman }
3680e0af8ecSBrian Feldman 
3690e0af8ecSBrian Feldman void
370780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
3710e0af8ecSBrian Feldman {
372bc91c510SAlan Cox 
37393bc4879SAlan Cox 	if (map->system_map)
37436daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
37512c64974SMaxime Henrion 	else
37612c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
37736daaecdSAlan Cox }
3780e0af8ecSBrian Feldman 
3790e0af8ecSBrian Feldman void
380780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
3810e0af8ecSBrian Feldman {
382bc91c510SAlan Cox 
38336daaecdSAlan Cox 	if (map->system_map)
38436daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
38536daaecdSAlan Cox 	else
38612c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
38725adb370SBrian Feldman }
38825adb370SBrian Feldman 
389d974f03cSAlan Cox int
390780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
391d974f03cSAlan Cox {
39225adb370SBrian Feldman 	int error;
39325adb370SBrian Feldman 
39436daaecdSAlan Cox 	error = map->system_map ?
39536daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
39612c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
3973a92e5d5SAlan Cox 	if (error == 0)
3983a92e5d5SAlan Cox 		map->timestamp++;
399bc91c510SAlan Cox 	return (error == 0);
4000e0af8ecSBrian Feldman }
4010e0af8ecSBrian Feldman 
4020e0af8ecSBrian Feldman int
40372d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
40472d97679SDavid Schultz {
40572d97679SDavid Schultz 	int error;
40672d97679SDavid Schultz 
40772d97679SDavid Schultz 	error = map->system_map ?
40872d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
40912c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
41072d97679SDavid Schultz 	return (error == 0);
41172d97679SDavid Schultz }
41272d97679SDavid Schultz 
41372d97679SDavid Schultz int
414780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
4150e0af8ecSBrian Feldman {
416bc91c510SAlan Cox 
41736daaecdSAlan Cox #ifdef INVARIANTS
41812c64974SMaxime Henrion 	if (map->system_map) {
41936daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
42036daaecdSAlan Cox 	} else
42112c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
42212c64974SMaxime Henrion #endif
423bc91c510SAlan Cox 	map->timestamp++;
424bc91c510SAlan Cox 	return (0);
4250e0af8ecSBrian Feldman }
4260e0af8ecSBrian Feldman 
4270e0af8ecSBrian Feldman void
428780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
4291b40f8c0SMatthew Dillon {
430bc91c510SAlan Cox 
43136daaecdSAlan Cox #ifdef INVARIANTS
43212c64974SMaxime Henrion 	if (map->system_map) {
43336daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
43436daaecdSAlan Cox 	} else
43512c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
43612c64974SMaxime Henrion #endif
43725adb370SBrian Feldman }
43825adb370SBrian Feldman 
439acd9a301SAlan Cox /*
440acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
441acd9a301SAlan Cox  */
4429688f931SAlan Cox int
443acd9a301SAlan Cox vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
444acd9a301SAlan Cox {
445acd9a301SAlan Cox 
4463a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
447acd9a301SAlan Cox 	vm_map_unlock(map);
4483a92e5d5SAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
449acd9a301SAlan Cox }
450acd9a301SAlan Cox 
451acd9a301SAlan Cox /*
452acd9a301SAlan Cox  *	vm_map_wakeup:
453acd9a301SAlan Cox  */
4549688f931SAlan Cox void
455acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
456acd9a301SAlan Cox {
457acd9a301SAlan Cox 
458b49ecb86SAlan Cox 	/*
4593a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
4603a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
4613a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
462b49ecb86SAlan Cox 	 */
4633a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
4643a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
465acd9a301SAlan Cox 	wakeup(&map->root);
466acd9a301SAlan Cox }
467acd9a301SAlan Cox 
4681b40f8c0SMatthew Dillon long
4691b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
4701b40f8c0SMatthew Dillon {
4711b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
4721b40f8c0SMatthew Dillon }
4731b40f8c0SMatthew Dillon 
4742bc7dd56SBruce M Simpson long
4752bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
4762bc7dd56SBruce M Simpson {
4772bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
4782bc7dd56SBruce M Simpson }
4792bc7dd56SBruce M Simpson 
480ff2b5645SMatthew Dillon /*
481df8bae1dSRodney W. Grimes  *	vm_map_create:
482df8bae1dSRodney W. Grimes  *
483df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
484df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
485df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
486df8bae1dSRodney W. Grimes  */
4870d94caffSDavid Greenman vm_map_t
4881b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
489df8bae1dSRodney W. Grimes {
490c0877f10SJohn Dyson 	vm_map_t result;
491df8bae1dSRodney W. Grimes 
492a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
49321c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
4948355f576SJeff Roberson 	_vm_map_init(result, min, max);
495df8bae1dSRodney W. Grimes 	result->pmap = pmap;
496df8bae1dSRodney W. Grimes 	return (result);
497df8bae1dSRodney W. Grimes }
498df8bae1dSRodney W. Grimes 
499df8bae1dSRodney W. Grimes /*
500df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
501df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
502df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
503df8bae1dSRodney W. Grimes  */
5048355f576SJeff Roberson static void
5058355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
506df8bae1dSRodney W. Grimes {
50721c641b2SJohn Baldwin 
508df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
5099688f931SAlan Cox 	map->needs_wakeup = FALSE;
5103075778bSJohn Dyson 	map->system_map = 0;
511df8bae1dSRodney W. Grimes 	map->min_offset = min;
512df8bae1dSRodney W. Grimes 	map->max_offset = max;
513af7cd0c5SBrian Feldman 	map->flags = 0;
5144e94f402SAlan Cox 	map->root = NULL;
515df8bae1dSRodney W. Grimes 	map->timestamp = 0;
516df8bae1dSRodney W. Grimes }
517df8bae1dSRodney W. Grimes 
518a18b1f1dSJason Evans void
5198355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
520a18b1f1dSJason Evans {
5218355f576SJeff Roberson 	_vm_map_init(map, min, max);
522d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
52312c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
524a18b1f1dSJason Evans }
525a18b1f1dSJason Evans 
526df8bae1dSRodney W. Grimes /*
527b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
528b18bfc3dSJohn Dyson  *
529b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
530b18bfc3dSJohn Dyson  */
53162487bb4SJohn Dyson static void
5321b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
533b18bfc3dSJohn Dyson {
5342b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
535b18bfc3dSJohn Dyson }
536b18bfc3dSJohn Dyson 
537b18bfc3dSJohn Dyson /*
538df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
539df8bae1dSRodney W. Grimes  *
540df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
541b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
542df8bae1dSRodney W. Grimes  */
543f708ef1bSPoul-Henning Kamp static vm_map_entry_t
5441b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
545df8bae1dSRodney W. Grimes {
5461f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
5471f6889a1SMatthew Dillon 
5482b4a2c27SAlan Cox 	if (map->system_map)
5492b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
5502b4a2c27SAlan Cox 	else
551a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
5521f6889a1SMatthew Dillon 	if (new_entry == NULL)
5531f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
5541f6889a1SMatthew Dillon 	return (new_entry);
555df8bae1dSRodney W. Grimes }
556df8bae1dSRodney W. Grimes 
557df8bae1dSRodney W. Grimes /*
558794316a8SAlan Cox  *	vm_map_entry_set_behavior:
559794316a8SAlan Cox  *
560794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
561794316a8SAlan Cox  *	sequential.
562794316a8SAlan Cox  */
563794316a8SAlan Cox static __inline void
564794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
565794316a8SAlan Cox {
566794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
567794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
568794316a8SAlan Cox }
569794316a8SAlan Cox 
570794316a8SAlan Cox /*
5710164e057SAlan Cox  *	vm_map_entry_set_max_free:
5720164e057SAlan Cox  *
5730164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
5740164e057SAlan Cox  */
5750164e057SAlan Cox static __inline void
5760164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
5770164e057SAlan Cox {
5780164e057SAlan Cox 
5790164e057SAlan Cox 	entry->max_free = entry->adj_free;
5800164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
5810164e057SAlan Cox 		entry->max_free = entry->left->max_free;
5820164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
5830164e057SAlan Cox 		entry->max_free = entry->right->max_free;
5840164e057SAlan Cox }
5850164e057SAlan Cox 
5860164e057SAlan Cox /*
5874e94f402SAlan Cox  *	vm_map_entry_splay:
5884e94f402SAlan Cox  *
5890164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
5900164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
5910164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
5920164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
5930164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
5940164e057SAlan Cox  *	amortized.
5950164e057SAlan Cox  *
5960164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
5970164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
5980164e057SAlan Cox  *
5990164e057SAlan Cox  *	The map must be locked, and leaves it so.
6000164e057SAlan Cox  *
6010164e057SAlan Cox  *	Returns: the new root.
6024e94f402SAlan Cox  */
6034e94f402SAlan Cox static vm_map_entry_t
6040164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
6054e94f402SAlan Cox {
6060164e057SAlan Cox 	vm_map_entry_t llist, rlist;
6070164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
6080164e057SAlan Cox 	vm_map_entry_t y;
6094e94f402SAlan Cox 
6100164e057SAlan Cox 	/* Special case of empty tree. */
6114e94f402SAlan Cox 	if (root == NULL)
6124e94f402SAlan Cox 		return (root);
6130164e057SAlan Cox 
6140164e057SAlan Cox 	/*
6150164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
6160164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
6170164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
6180164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
6190164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
6200164e057SAlan Cox 	 * the two spines.
6210164e057SAlan Cox 	 */
6220164e057SAlan Cox 	llist = NULL;
6230164e057SAlan Cox 	rlist = NULL;
6240164e057SAlan Cox 	for (;;) {
6250164e057SAlan Cox 		/* root is never NULL in here. */
6260164e057SAlan Cox 		if (addr < root->start) {
6270164e057SAlan Cox 			y = root->left;
6280164e057SAlan Cox 			if (y == NULL)
6294e94f402SAlan Cox 				break;
6300164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
6310164e057SAlan Cox 				/* Rotate right and put y on rlist. */
6324e94f402SAlan Cox 				root->left = y->right;
6334e94f402SAlan Cox 				y->right = root;
6340164e057SAlan Cox 				vm_map_entry_set_max_free(root);
6350164e057SAlan Cox 				root = y->left;
6360164e057SAlan Cox 				y->left = rlist;
6370164e057SAlan Cox 				rlist = y;
6380164e057SAlan Cox 			} else {
6390164e057SAlan Cox 				/* Put root on rlist. */
6400164e057SAlan Cox 				root->left = rlist;
6410164e057SAlan Cox 				rlist = root;
6424e94f402SAlan Cox 				root = y;
6434e94f402SAlan Cox 			}
6440164e057SAlan Cox 		} else {
6450164e057SAlan Cox 			y = root->right;
6460164e057SAlan Cox 			if (addr < root->end || y == NULL)
6474e94f402SAlan Cox 				break;
6480164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
6490164e057SAlan Cox 				/* Rotate left and put y on llist. */
6504e94f402SAlan Cox 				root->right = y->left;
6514e94f402SAlan Cox 				y->left = root;
6520164e057SAlan Cox 				vm_map_entry_set_max_free(root);
6530164e057SAlan Cox 				root = y->right;
6540164e057SAlan Cox 				y->right = llist;
6550164e057SAlan Cox 				llist = y;
6560164e057SAlan Cox 			} else {
6570164e057SAlan Cox 				/* Put root on llist. */
6580164e057SAlan Cox 				root->right = llist;
6590164e057SAlan Cox 				llist = root;
6604e94f402SAlan Cox 				root = y;
6614e94f402SAlan Cox 			}
6624e94f402SAlan Cox 		}
6630164e057SAlan Cox 	}
6640164e057SAlan Cox 
6650164e057SAlan Cox 	/*
6660164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
6670164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
6680164e057SAlan Cox 	 * bottom of llist and rlist.
6690164e057SAlan Cox 	 */
6700164e057SAlan Cox 	ltree = root->left;
6710164e057SAlan Cox 	while (llist != NULL) {
6720164e057SAlan Cox 		y = llist->right;
6730164e057SAlan Cox 		llist->right = ltree;
6740164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
6750164e057SAlan Cox 		ltree = llist;
6760164e057SAlan Cox 		llist = y;
6770164e057SAlan Cox 	}
6780164e057SAlan Cox 	rtree = root->right;
6790164e057SAlan Cox 	while (rlist != NULL) {
6800164e057SAlan Cox 		y = rlist->left;
6810164e057SAlan Cox 		rlist->left = rtree;
6820164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
6830164e057SAlan Cox 		rtree = rlist;
6840164e057SAlan Cox 		rlist = y;
6850164e057SAlan Cox 	}
6860164e057SAlan Cox 
6870164e057SAlan Cox 	/*
6880164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
6890164e057SAlan Cox 	 */
6900164e057SAlan Cox 	root->left = ltree;
6910164e057SAlan Cox 	root->right = rtree;
6920164e057SAlan Cox 	vm_map_entry_set_max_free(root);
6930164e057SAlan Cox 
6944e94f402SAlan Cox 	return (root);
6954e94f402SAlan Cox }
6964e94f402SAlan Cox 
6974e94f402SAlan Cox /*
698df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
699df8bae1dSRodney W. Grimes  *
700df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
701df8bae1dSRodney W. Grimes  */
7024e94f402SAlan Cox static void
70399c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
70499c81ca9SAlan Cox 		  vm_map_entry_t after_where,
70599c81ca9SAlan Cox 		  vm_map_entry_t entry)
70699c81ca9SAlan Cox {
70721c641b2SJohn Baldwin 
70821c641b2SJohn Baldwin 	CTR4(KTR_VM,
70921c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
71021c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
71199c81ca9SAlan Cox 	map->nentries++;
71299c81ca9SAlan Cox 	entry->prev = after_where;
71399c81ca9SAlan Cox 	entry->next = after_where->next;
71499c81ca9SAlan Cox 	entry->next->prev = entry;
71599c81ca9SAlan Cox 	after_where->next = entry;
7164e94f402SAlan Cox 
7174e94f402SAlan Cox 	if (after_where != &map->header) {
7184e94f402SAlan Cox 		if (after_where != map->root)
7194e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
7204e94f402SAlan Cox 		entry->right = after_where->right;
7214e94f402SAlan Cox 		entry->left = after_where;
7224e94f402SAlan Cox 		after_where->right = NULL;
7230164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
7240164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
7254e94f402SAlan Cox 	} else {
7264e94f402SAlan Cox 		entry->right = map->root;
7274e94f402SAlan Cox 		entry->left = NULL;
7284e94f402SAlan Cox 	}
7290164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
7300164e057SAlan Cox 	    entry->next->start) - entry->end;
7310164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
7324e94f402SAlan Cox 	map->root = entry;
733df8bae1dSRodney W. Grimes }
73499c81ca9SAlan Cox 
7354e94f402SAlan Cox static void
73699c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
73799c81ca9SAlan Cox 		    vm_map_entry_t entry)
73899c81ca9SAlan Cox {
7394e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
74099c81ca9SAlan Cox 
7414e94f402SAlan Cox 	if (entry != map->root)
7424e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
7434e94f402SAlan Cox 	if (entry->left == NULL)
7444e94f402SAlan Cox 		root = entry->right;
7454e94f402SAlan Cox 	else {
7464e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
7474e94f402SAlan Cox 		root->right = entry->right;
7480164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
7490164e057SAlan Cox 		    entry->next->start) - root->end;
7500164e057SAlan Cox 		vm_map_entry_set_max_free(root);
7514e94f402SAlan Cox 	}
7524e94f402SAlan Cox 	map->root = root;
7534e94f402SAlan Cox 
7544e94f402SAlan Cox 	prev = entry->prev;
7554e94f402SAlan Cox 	next = entry->next;
75699c81ca9SAlan Cox 	next->prev = prev;
75799c81ca9SAlan Cox 	prev->next = next;
75899c81ca9SAlan Cox 	map->nentries--;
75921c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
76021c641b2SJohn Baldwin 	    map->nentries, entry);
761df8bae1dSRodney W. Grimes }
762df8bae1dSRodney W. Grimes 
763df8bae1dSRodney W. Grimes /*
7640164e057SAlan Cox  *	vm_map_entry_resize_free:
7650164e057SAlan Cox  *
7660164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
7670164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
7680164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
7690164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
7700164e057SAlan Cox  *
7710164e057SAlan Cox  *	The map must be locked, and leaves it so.
7720164e057SAlan Cox  */
7730164e057SAlan Cox static void
7740164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
7750164e057SAlan Cox {
7760164e057SAlan Cox 
7770164e057SAlan Cox 	/*
7780164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
7790164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
7800164e057SAlan Cox 	 * root and making the change there.
7810164e057SAlan Cox 	 */
7820164e057SAlan Cox 	if (entry != map->root)
7830164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
7840164e057SAlan Cox 
7850164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
7860164e057SAlan Cox 	    entry->next->start) - entry->end;
7870164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
7880164e057SAlan Cox }
7890164e057SAlan Cox 
7900164e057SAlan Cox /*
791df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
792df8bae1dSRodney W. Grimes  *
793df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
794df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
795df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
796df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
797df8bae1dSRodney W. Grimes  *	result indicates whether the address is
798df8bae1dSRodney W. Grimes  *	actually contained in the map.
799df8bae1dSRodney W. Grimes  */
8000d94caffSDavid Greenman boolean_t
8011b40f8c0SMatthew Dillon vm_map_lookup_entry(
8021b40f8c0SMatthew Dillon 	vm_map_t map,
8031b40f8c0SMatthew Dillon 	vm_offset_t address,
8041b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
805df8bae1dSRodney W. Grimes {
806c0877f10SJohn Dyson 	vm_map_entry_t cur;
807df8bae1dSRodney W. Grimes 
8084e94f402SAlan Cox 	cur = vm_map_entry_splay(address, map->root);
8094e94f402SAlan Cox 	if (cur == NULL)
8104e94f402SAlan Cox 		*entry = &map->header;
8114e94f402SAlan Cox 	else {
8124e94f402SAlan Cox 		map->root = cur;
813df8bae1dSRodney W. Grimes 
814df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
815df8bae1dSRodney W. Grimes 			*entry = cur;
8164e94f402SAlan Cox 			if (cur->end > address)
817df8bae1dSRodney W. Grimes 				return (TRUE);
8184e94f402SAlan Cox 		} else
819df8bae1dSRodney W. Grimes 			*entry = cur->prev;
8204e94f402SAlan Cox 	}
821df8bae1dSRodney W. Grimes 	return (FALSE);
822df8bae1dSRodney W. Grimes }
823df8bae1dSRodney W. Grimes 
824df8bae1dSRodney W. Grimes /*
82530dcfc09SJohn Dyson  *	vm_map_insert:
82630dcfc09SJohn Dyson  *
82730dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
82830dcfc09SJohn Dyson  *	map at the specified address range.  The object's
82930dcfc09SJohn Dyson  *	size should match that of the address range.
83030dcfc09SJohn Dyson  *
83130dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
8322aaeadf8SMatthew Dillon  *
8332aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
8342aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
83530dcfc09SJohn Dyson  */
83630dcfc09SJohn Dyson int
837b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
838b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
839b9dcd593SBruce Evans 	      int cow)
84030dcfc09SJohn Dyson {
841c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
842c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
84330dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
8449730a5daSPaul Saab 	vm_eflags_t protoeflags;
84530dcfc09SJohn Dyson 
84630dcfc09SJohn Dyson 	/*
84730dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
84830dcfc09SJohn Dyson 	 */
84930dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
85030dcfc09SJohn Dyson 	    (start >= end))
85130dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
85230dcfc09SJohn Dyson 
85330dcfc09SJohn Dyson 	/*
85430dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
85530dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
85630dcfc09SJohn Dyson 	 */
85730dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
85830dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
85930dcfc09SJohn Dyson 
86030dcfc09SJohn Dyson 	prev_entry = temp_entry;
86130dcfc09SJohn Dyson 
86230dcfc09SJohn Dyson 	/*
86330dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
86430dcfc09SJohn Dyson 	 */
86530dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
86630dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
86730dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
86830dcfc09SJohn Dyson 
869afa07f7eSJohn Dyson 	protoeflags = 0;
870afa07f7eSJohn Dyson 
871afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
872e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
873afa07f7eSJohn Dyson 
8744e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
875afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
876afa07f7eSJohn Dyson 
8774e045f93SAlan Cox 		KASSERT(object == NULL,
8784e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
8794e045f93SAlan Cox 	}
8804f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
8814f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
8829730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
8839730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
8844f79d873SMatthew Dillon 
8851d284e00SAlan Cox 	if (object != NULL) {
88630dcfc09SJohn Dyson 		/*
8871d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
8881d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
8891d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
8901d284e00SAlan Cox 		 * reference counting is insufficient to recognize
8911d284e00SAlan Cox 		 * aliases with precision.)
89230dcfc09SJohn Dyson 		 */
8931d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
8941d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
8952aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
8961d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
8974e045f93SAlan Cox 	}
8984e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
8994e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
9008cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
9014e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
9024e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
9038cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
90457a21abaSAlan Cox 				     prev_entry->offset,
9058cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
906cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
90730dcfc09SJohn Dyson 		/*
9082aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
9092aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
9102aaeadf8SMatthew Dillon 		 * new range as well.
91130dcfc09SJohn Dyson 		 */
9128cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
9138cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
9148cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
91530dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
91630dcfc09SJohn Dyson 			prev_entry->end = end;
9170164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
9184e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
91930dcfc09SJohn Dyson 			return (KERN_SUCCESS);
92030dcfc09SJohn Dyson 		}
9218cc7e047SJohn Dyson 
9222aaeadf8SMatthew Dillon 		/*
9232aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
9242aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
9252aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
9264e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
9272aaeadf8SMatthew Dillon 		 */
9282aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
9292aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
9302aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
9318cc7e047SJohn Dyson 		vm_object_reference(object);
932b18bfc3dSJohn Dyson 	}
9332aaeadf8SMatthew Dillon 
9342aaeadf8SMatthew Dillon 	/*
9352aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
9362aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
9372aaeadf8SMatthew Dillon 	 * backing objects.
9382aaeadf8SMatthew Dillon 	 */
9398cc7e047SJohn Dyson 
94030dcfc09SJohn Dyson 	/*
94130dcfc09SJohn Dyson 	 * Create a new entry
94230dcfc09SJohn Dyson 	 */
94330dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
94430dcfc09SJohn Dyson 	new_entry->start = start;
94530dcfc09SJohn Dyson 	new_entry->end = end;
94630dcfc09SJohn Dyson 
947afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
94830dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
94930dcfc09SJohn Dyson 	new_entry->offset = offset;
9502267af78SJulian Elischer 	new_entry->avail_ssize = 0;
9512267af78SJulian Elischer 
95230dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
95330dcfc09SJohn Dyson 	new_entry->protection = prot;
95430dcfc09SJohn Dyson 	new_entry->max_protection = max;
95530dcfc09SJohn Dyson 	new_entry->wired_count = 0;
956e5f251d2SAlan Cox 
95730dcfc09SJohn Dyson 	/*
95830dcfc09SJohn Dyson 	 * Insert the new entry into the list
95930dcfc09SJohn Dyson 	 */
96030dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
96130dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
96230dcfc09SJohn Dyson 
9631a484d28SMatthew Dillon #if 0
9641a484d28SMatthew Dillon 	/*
9651a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
9661a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
9671a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
9681a484d28SMatthew Dillon 	 */
9694e71e795SMatthew Dillon 	/*
9704e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
9714e71e795SMatthew Dillon 	 */
9724e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
9731a484d28SMatthew Dillon #endif
9744e71e795SMatthew Dillon 
9754f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
9764da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
977e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
978e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
9794f79d873SMatthew Dillon 	}
980e972780aSAlan Cox 
98130dcfc09SJohn Dyson 	return (KERN_SUCCESS);
98230dcfc09SJohn Dyson }
98330dcfc09SJohn Dyson 
98430dcfc09SJohn Dyson /*
9850164e057SAlan Cox  *	vm_map_findspace:
9860164e057SAlan Cox  *
9870164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
9880164e057SAlan Cox  *	beginning at address >= start in the given map.
9890164e057SAlan Cox  *
9900164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
9910164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
9920164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
9930164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
9940164e057SAlan Cox  *	O(log n) amortized with splay trees.
9950164e057SAlan Cox  *
9960164e057SAlan Cox  *	The map must be locked, and leaves it so.
9970164e057SAlan Cox  *
9980164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
9990164e057SAlan Cox  *		 1 if insufficient space.
1000df8bae1dSRodney W. Grimes  */
1001df8bae1dSRodney W. Grimes int
10020164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
10030164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1004df8bae1dSRodney W. Grimes {
10050164e057SAlan Cox 	vm_map_entry_t entry;
10060164e057SAlan Cox 	vm_offset_t end, st;
1007df8bae1dSRodney W. Grimes 
1008986b43f8SAlan Cox 	/*
1009986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1010986b43f8SAlan Cox 	 * address wrap.
1011986b43f8SAlan Cox 	 */
1012df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1013df8bae1dSRodney W. Grimes 		start = map->min_offset;
1014986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1015df8bae1dSRodney W. Grimes 		return (1);
1016df8bae1dSRodney W. Grimes 
10170164e057SAlan Cox 	/* Empty tree means wide open address space. */
10180164e057SAlan Cox 	if (map->root == NULL) {
1019df8bae1dSRodney W. Grimes 		*addr = start;
10200164e057SAlan Cox 		goto found;
102199448ed1SJohn Dyson 	}
10220164e057SAlan Cox 
10230164e057SAlan Cox 	/*
10240164e057SAlan Cox 	 * After splay, if start comes before root node, then there
10250164e057SAlan Cox 	 * must be a gap from start to the root.
10260164e057SAlan Cox 	 */
10270164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
10280164e057SAlan Cox 	if (start + length <= map->root->start) {
10290164e057SAlan Cox 		*addr = start;
10300164e057SAlan Cox 		goto found;
10310164e057SAlan Cox 	}
10320164e057SAlan Cox 
10330164e057SAlan Cox 	/*
10340164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1035986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1036986b43f8SAlan Cox 	 * wrap might be a problem.
10370164e057SAlan Cox 	 */
10380164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1039986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
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;
13721f70d622SAlan Cox 	boolean_t are_queues_locked;
13730551c08dSAlan Cox 
1374ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
13751f78f902SAlan Cox 		return;
13761f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
13771f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
13781f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
13791f78f902SAlan Cox 		goto unlock_return;
13801f78f902SAlan Cox 	}
13811f78f902SAlan Cox 
13821f78f902SAlan Cox 	psize = atop(size);
13831f78f902SAlan Cox 
13841f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
13851f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
13861f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
13871f78f902SAlan Cox 		goto unlock_return;
13881f78f902SAlan Cox 	}
13891f78f902SAlan Cox 
13901f78f902SAlan Cox 	if (psize + pindex > object->size) {
13911f78f902SAlan Cox 		if (object->size < pindex)
13921f78f902SAlan Cox 			goto unlock_return;
13931f78f902SAlan Cox 		psize = object->size - pindex;
13941f78f902SAlan Cox 	}
13951f78f902SAlan Cox 
13961f70d622SAlan Cox 	are_queues_locked = FALSE;
13971f78f902SAlan Cox 	mpte = NULL;
13981f78f902SAlan Cox 
13991f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
14001f78f902SAlan Cox 		if (p->pindex < pindex) {
14011f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
14021f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
14031f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
14041f78f902SAlan Cox 		}
14051f78f902SAlan Cox 	}
14061f78f902SAlan Cox 	/*
14071f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
14081f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
14091f78f902SAlan Cox 	 * or (2) NULL.
14101f78f902SAlan Cox 	 */
14111f78f902SAlan Cox 	for (;
14121f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
14131f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
14141f78f902SAlan Cox 		/*
14151f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
14161f78f902SAlan Cox 		 * free pages allocating pv entries.
14171f78f902SAlan Cox 		 */
14181f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
14191f78f902SAlan Cox 		    cnt.v_free_count < cnt.v_free_reserved) {
14201f78f902SAlan Cox 			break;
14211f78f902SAlan Cox 		}
14221f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
14231f78f902SAlan Cox 		    (p->busy == 0) &&
14241f78f902SAlan Cox 		    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
14251f70d622SAlan Cox 			if (!are_queues_locked) {
14261f70d622SAlan Cox 				are_queues_locked = TRUE;
142785f5b245SAlan Cox 				vm_page_lock_queues();
142885f5b245SAlan Cox 			}
1429997e1c25SAlan Cox 			if (VM_PAGE_INQUEUE1(p, PQ_CACHE))
14301f70d622SAlan Cox 				vm_page_deactivate(p);
14311f78f902SAlan Cox 			mpte = pmap_enter_quick(map->pmap,
1432ba8bca61SAlan Cox 			    addr + ptoa(tmpidx), p, prot, mpte);
14331f78f902SAlan Cox 		}
14341f78f902SAlan Cox 	}
14351f70d622SAlan Cox 	if (are_queues_locked)
14361f70d622SAlan Cox 		vm_page_unlock_queues();
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 
2485df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2486df8bae1dSRodney W. Grimes 
24872d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
24882a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
24892a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
24902a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2491df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
249247221757SJohn Dyson 	new_map->timestamp = 1;
2493df8bae1dSRodney W. Grimes 
2494abd498aaSBruce M Simpson 	/* Do not inherit the MAP_WIREFUTURE property. */
2495abd498aaSBruce M Simpson 	if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2496abd498aaSBruce M Simpson 		new_map->flags &= ~MAP_WIREFUTURE;
2497abd498aaSBruce M Simpson 
2498df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2499df8bae1dSRodney W. Grimes 
2500df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2501afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2502df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2503df8bae1dSRodney W. Grimes 
2504df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2505df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2506df8bae1dSRodney W. Grimes 			break;
2507df8bae1dSRodney W. Grimes 
2508df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2509df8bae1dSRodney W. Grimes 			/*
2510fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2511fed9a903SJohn Dyson 			 */
2512fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2513fed9a903SJohn Dyson 			if (object == NULL) {
2514fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2515c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2516fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
251715d2d313SAlan Cox 				old_entry->offset = 0;
25189a2f6362SAlan Cox 			}
25199a2f6362SAlan Cox 
25209a2f6362SAlan Cox 			/*
25219a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
25229a2f6362SAlan Cox 			 * to insure that a shadow object is created.
25239a2f6362SAlan Cox 			 */
25249a2f6362SAlan Cox 			vm_object_reference(object);
25259a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
25265069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
25275069bf57SJohn Dyson 					&old_entry->offset,
2528c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
25295069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2530d30344bdSIan Dowse 				/* Transfer the second reference too. */
2531d30344bdSIan Dowse 				vm_object_reference(
2532d30344bdSIan Dowse 				    old_entry->object.vm_object);
2533d30344bdSIan Dowse 				vm_object_deallocate(object);
25345069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2535fed9a903SJohn Dyson 			}
2536e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2537069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2538e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2539fed9a903SJohn Dyson 
2540fed9a903SJohn Dyson 			/*
2541ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2542df8bae1dSRodney W. Grimes 			 */
2543df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2544df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2545028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2546df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2547df8bae1dSRodney W. Grimes 
2548df8bae1dSRodney W. Grimes 			/*
25490d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
25500d94caffSDavid Greenman 			 * inserting at the end of the new map.
2551df8bae1dSRodney W. Grimes 			 */
2552df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2553df8bae1dSRodney W. Grimes 			    new_entry);
25542a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2555df8bae1dSRodney W. Grimes 
2556df8bae1dSRodney W. Grimes 			/*
2557df8bae1dSRodney W. Grimes 			 * Update the physical map
2558df8bae1dSRodney W. Grimes 			 */
2559df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2560df8bae1dSRodney W. Grimes 			    new_entry->start,
2561df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2562df8bae1dSRodney W. Grimes 			    old_entry->start);
2563df8bae1dSRodney W. Grimes 			break;
2564df8bae1dSRodney W. Grimes 
2565df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2566df8bae1dSRodney W. Grimes 			/*
2567df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2568df8bae1dSRodney W. Grimes 			 */
2569df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2570df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2571028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2572df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2573df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2574df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2575df8bae1dSRodney W. Grimes 			    new_entry);
25762a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2577bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2578bd7e5f99SJohn Dyson 			    new_entry);
2579df8bae1dSRodney W. Grimes 			break;
2580df8bae1dSRodney W. Grimes 		}
2581df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2582df8bae1dSRodney W. Grimes 	}
2583df8bae1dSRodney W. Grimes 
2584df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2585df8bae1dSRodney W. Grimes 
2586df8bae1dSRodney W. Grimes 	return (vm2);
2587df8bae1dSRodney W. Grimes }
2588df8bae1dSRodney W. Grimes 
258994f7e29aSAlan Cox int
259094f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
259194f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
259294f7e29aSAlan Cox {
2593fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2594fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
259594f7e29aSAlan Cox 	vm_size_t init_ssize;
2596fd75d710SMarcel Moolenaar 	int orient, rv;
259791d5354aSJohn Baldwin 	rlim_t vmemlim;
259894f7e29aSAlan Cox 
2599fd75d710SMarcel Moolenaar 	/*
2600fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2601fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2602fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2603fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2604fd75d710SMarcel Moolenaar 	 */
2605fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2606fd75d710SMarcel Moolenaar 	cow &= ~orient;
2607fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2608fd75d710SMarcel Moolenaar 
2609fd75d710SMarcel Moolenaar 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
261094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2611fd75d710SMarcel Moolenaar 
2612fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
261394f7e29aSAlan Cox 
261491d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
261591d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
261691d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
261791d5354aSJohn Baldwin 
261894f7e29aSAlan Cox 	vm_map_lock(map);
261994f7e29aSAlan Cox 
262094f7e29aSAlan Cox 	/* If addr is already mapped, no go */
262194f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
262294f7e29aSAlan Cox 		vm_map_unlock(map);
262394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
262494f7e29aSAlan Cox 	}
262594f7e29aSAlan Cox 
2626a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
262791d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2628a69ac174SMatthew Dillon 		vm_map_unlock(map);
2629a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2630a69ac174SMatthew Dillon 	}
2631a69ac174SMatthew Dillon 
2632fd75d710SMarcel Moolenaar 	/*
2633fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2634fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2635fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2636fd75d710SMarcel Moolenaar 	 * space is not protected.
263794f7e29aSAlan Cox 	 *
2638fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2639fd75d710SMarcel Moolenaar 	 * grow the stack.
264094f7e29aSAlan Cox 	 */
264194f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
264294f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
264394f7e29aSAlan Cox 		vm_map_unlock(map);
264494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
264594f7e29aSAlan Cox 	}
264694f7e29aSAlan Cox 
2647fd75d710SMarcel Moolenaar 	/*
2648fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2649fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2650fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2651fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
265294f7e29aSAlan Cox 	 *
2653fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2654fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2655fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
265694f7e29aSAlan Cox 	 */
2657fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2658fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2659fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2660fd75d710SMarcel Moolenaar 		bot = addrbos;
2661fd75d710SMarcel Moolenaar 	else
2662fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2663fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2664fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
266594f7e29aSAlan Cox 
2666fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
266794f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
266829b45e9eSAlan Cox 		if (prev_entry != &map->header)
2669fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2670fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2671fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
267294f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2673b21a0008SMarcel Moolenaar 
2674fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2675fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2676fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2677fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2678fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
267994f7e29aSAlan Cox 	}
268094f7e29aSAlan Cox 
268194f7e29aSAlan Cox 	vm_map_unlock(map);
268294f7e29aSAlan Cox 	return (rv);
268394f7e29aSAlan Cox }
268494f7e29aSAlan Cox 
268594f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
268694f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
268794f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
268894f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
268994f7e29aSAlan Cox  * the grow function in vm_machdep.c).
269094f7e29aSAlan Cox  */
269194f7e29aSAlan Cox int
269294f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
269394f7e29aSAlan Cox {
2694b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2695b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
269694f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
269794f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
269894f7e29aSAlan Cox 	vm_offset_t end;
2699b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
270091d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2701b21a0008SMarcel Moolenaar 	int is_procstack, rv;
270223955314SAlfred Perlstein 
270394f7e29aSAlan Cox Retry:
270491d5354aSJohn Baldwin 	PROC_LOCK(p);
270591d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2706bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
270791d5354aSJohn Baldwin 	PROC_UNLOCK(p);
270891d5354aSJohn Baldwin 
270994f7e29aSAlan Cox 	vm_map_lock_read(map);
271094f7e29aSAlan Cox 
271194f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
271294f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
271394f7e29aSAlan Cox 		vm_map_unlock_read(map);
27140cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
271594f7e29aSAlan Cox 	}
271694f7e29aSAlan Cox 
2717b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2718b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2719b21a0008SMarcel Moolenaar 		/*
2720b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2721b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2722b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2723b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
272494f7e29aSAlan Cox 		 */
2725b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
272694f7e29aSAlan Cox 			vm_map_unlock_read(map);
27270cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
272894f7e29aSAlan Cox 		}
2729b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2730b21a0008SMarcel Moolenaar 	} else {
2731b21a0008SMarcel Moolenaar 		/*
2732b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2733b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2734b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2735b21a0008SMarcel Moolenaar 		 * select one.
2736b21a0008SMarcel Moolenaar 		 */
2737b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2738b21a0008SMarcel Moolenaar 			/*
2739b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2740b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2741b21a0008SMarcel Moolenaar 			 */
2742b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
2743b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
2744b21a0008SMarcel Moolenaar 			else
2745b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
2746b21a0008SMarcel Moolenaar 		} else
2747b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
2748b21a0008SMarcel Moolenaar 	}
274994f7e29aSAlan Cox 
2750b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2751b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2752b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
2753b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
2754b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
275594f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2756b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
2757b21a0008SMarcel Moolenaar 	} else {
2758b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
275908667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
2760b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
2761b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
2762fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2763b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
2764b21a0008SMarcel Moolenaar 	}
2765b21a0008SMarcel Moolenaar 
276694f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
276794f7e29aSAlan Cox 		vm_map_unlock_read(map);
27680cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
276994f7e29aSAlan Cox 	}
277094f7e29aSAlan Cox 
2771b21a0008SMarcel Moolenaar 	/*
2772b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
2773b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
2774b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
2775b21a0008SMarcel Moolenaar 	 * and is probably an error.
277694f7e29aSAlan Cox 	 *
2777b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
2778b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
277994f7e29aSAlan Cox 	 */
2780b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
278125adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
278294f7e29aSAlan Cox 			goto Retry;
278394f7e29aSAlan Cox 
2784b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
278594f7e29aSAlan Cox 
278694f7e29aSAlan Cox 		vm_map_unlock(map);
27870cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
278894f7e29aSAlan Cox 	}
278994f7e29aSAlan Cox 
2790b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
279194f7e29aSAlan Cox 
2792b21a0008SMarcel Moolenaar 	/*
2793b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
2794b21a0008SMarcel Moolenaar 	 * limit.
279594f7e29aSAlan Cox 	 */
279691d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
279794f7e29aSAlan Cox 		vm_map_unlock_read(map);
27980cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
279994f7e29aSAlan Cox 	}
280094f7e29aSAlan Cox 
280194f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2802cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
2803b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
280494f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
280591d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
280691d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
280794f7e29aSAlan Cox 	}
280894f7e29aSAlan Cox 
2809a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
281091d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
2811a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
2812a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2813a69ac174SMatthew Dillon 	}
2814a69ac174SMatthew Dillon 
281525adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
281694f7e29aSAlan Cox 		goto Retry;
281794f7e29aSAlan Cox 
2818b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2819b21a0008SMarcel Moolenaar 		/*
2820b21a0008SMarcel Moolenaar 		 * Growing downward.
2821b21a0008SMarcel Moolenaar 		 */
282294f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
282394f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
282494f7e29aSAlan Cox 
2825b21a0008SMarcel Moolenaar 		/*
2826b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
2827b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
282894f7e29aSAlan Cox 		 */
282994f7e29aSAlan Cox 		if (addr < end) {
2830b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
283194f7e29aSAlan Cox 			addr = end;
283294f7e29aSAlan Cox 		}
283394f7e29aSAlan Cox 
283494f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
283505ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
283694f7e29aSAlan Cox 
283794f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
283894f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
283929b45e9eSAlan Cox 			if (prev_entry != &map->header)
284029b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
2841b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
2842b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2843b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2844b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
2845b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
2846b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
2847b21a0008SMarcel Moolenaar 			    grow_amount;
2848b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2849b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
285094f7e29aSAlan Cox 		}
2851b21a0008SMarcel Moolenaar 	} else {
2852b21a0008SMarcel Moolenaar 		/*
2853b21a0008SMarcel Moolenaar 		 * Growing upward.
2854b21a0008SMarcel Moolenaar 		 */
2855b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
2856b21a0008SMarcel Moolenaar 
2857b21a0008SMarcel Moolenaar 		/*
2858b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
2859b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
2860b21a0008SMarcel Moolenaar 		 */
2861b21a0008SMarcel Moolenaar 		if (addr > end) {
2862b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
2863b21a0008SMarcel Moolenaar 			addr = end;
286494f7e29aSAlan Cox 		}
286594f7e29aSAlan Cox 
2866b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
2867b21a0008SMarcel Moolenaar 
2868b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
2869b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
2870b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
287157a21abaSAlan Cox 		    stack_entry->offset,
2872b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2873b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
287408667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
2875b21a0008SMarcel Moolenaar 			/* Update the current entry. */
2876b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
2877199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
28780164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
2879b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
2880b21a0008SMarcel Moolenaar 
2881b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
2882b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
2883b21a0008SMarcel Moolenaar 		} else
2884b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
2885b21a0008SMarcel Moolenaar 	}
2886b21a0008SMarcel Moolenaar 
2887b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
2888b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
2889b21a0008SMarcel Moolenaar 
289094f7e29aSAlan Cox 	vm_map_unlock(map);
2891b21a0008SMarcel Moolenaar 
2892abd498aaSBruce M Simpson 	/*
2893abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2894abd498aaSBruce M Simpson 	 */
2895b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2896b21a0008SMarcel Moolenaar 		vm_map_wire(map,
2897b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2898b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2899b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
2900b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2901b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2902b21a0008SMarcel Moolenaar 	}
2903abd498aaSBruce M Simpson 
29040cddd8f0SMatthew Dillon 	return (rv);
290594f7e29aSAlan Cox }
290694f7e29aSAlan Cox 
2907df8bae1dSRodney W. Grimes /*
29085856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
29095856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
29105856e12eSJohn Dyson  */
29115856e12eSJohn Dyson void
29123ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
29131b40f8c0SMatthew Dillon {
29145856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
29155856e12eSJohn Dyson 	struct vmspace *newvmspace;
29165856e12eSJohn Dyson 
29173ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
291851ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
29195856e12eSJohn Dyson 	/*
29205856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
29215856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
29225856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
29235856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
29245856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
29255856e12eSJohn Dyson 	 */
29265856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2927b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2928b40ce416SJulian Elischer 		pmap_activate(curthread);
2929b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
29305856e12eSJohn Dyson }
29315856e12eSJohn Dyson 
29325856e12eSJohn Dyson /*
29335856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
29345856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
29355856e12eSJohn Dyson  */
29365856e12eSJohn Dyson void
29371b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
29381b40f8c0SMatthew Dillon {
29395856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
29405856e12eSJohn Dyson 	struct vmspace *newvmspace;
29415856e12eSJohn Dyson 
29425856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
29435856e12eSJohn Dyson 		return;
29445856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
29455856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2946b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2947b40ce416SJulian Elischer 		pmap_activate(curthread);
2948b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
29495856e12eSJohn Dyson }
29505856e12eSJohn Dyson 
29515856e12eSJohn Dyson /*
2952df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2953df8bae1dSRodney W. Grimes  *
2954df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2955df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2956df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2957df8bae1dSRodney W. Grimes  *	type specified.
2958df8bae1dSRodney W. Grimes  *
2959df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2960df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2961df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2962df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2963df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2964df8bae1dSRodney W. Grimes  *
2965df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2966df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2967df8bae1dSRodney W. Grimes  *
2968df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2969df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2970df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2971df8bae1dSRodney W. Grimes  *	remain the same.
2972df8bae1dSRodney W. Grimes  */
2973df8bae1dSRodney W. Grimes int
2974b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2975b9dcd593SBruce Evans 	      vm_offset_t vaddr,
297647221757SJohn Dyson 	      vm_prot_t fault_typea,
2977b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2978b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2979b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2980b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
29812d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2982df8bae1dSRodney W. Grimes {
2983c0877f10SJohn Dyson 	vm_map_entry_t entry;
2984c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2985c0877f10SJohn Dyson 	vm_prot_t prot;
298647221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2987df8bae1dSRodney W. Grimes 
2988df8bae1dSRodney W. Grimes RetryLookup:;
2989df8bae1dSRodney W. Grimes 	/*
2990df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2991df8bae1dSRodney W. Grimes 	 */
2992df8bae1dSRodney W. Grimes 
2993df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2994df8bae1dSRodney W. Grimes #define	RETURN(why) \
2995df8bae1dSRodney W. Grimes 		{ \
2996df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2997df8bae1dSRodney W. Grimes 		return (why); \
2998df8bae1dSRodney W. Grimes 		}
2999df8bae1dSRodney W. Grimes 
3000df8bae1dSRodney W. Grimes 	/*
30010d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
30020d94caffSDavid Greenman 	 * blown lookup routine.
3003df8bae1dSRodney W. Grimes 	 */
30044e94f402SAlan Cox 	entry = map->root;
3005df8bae1dSRodney W. Grimes 	*out_entry = entry;
30064e94f402SAlan Cox 	if (entry == NULL ||
3007df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3008df8bae1dSRodney W. Grimes 		/*
30090d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
30100d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
3011df8bae1dSRodney W. Grimes 		 */
30124e94f402SAlan Cox 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3013df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
3014df8bae1dSRodney W. Grimes 
30154e94f402SAlan Cox 		entry = *out_entry;
3016df8bae1dSRodney W. Grimes 	}
3017b7b2aac2SJohn Dyson 
3018df8bae1dSRodney W. Grimes 	/*
3019df8bae1dSRodney W. Grimes 	 * Handle submaps.
3020df8bae1dSRodney W. Grimes 	 */
3021afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3022df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3023df8bae1dSRodney W. Grimes 
3024df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3025df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3026df8bae1dSRodney W. Grimes 		goto RetryLookup;
3027df8bae1dSRodney W. Grimes 	}
3028a04c970aSJohn Dyson 
3029df8bae1dSRodney W. Grimes 	/*
30300d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3031a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
3032a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
3033a04c970aSJohn Dyson 	 * COW for debuggers.
3034df8bae1dSRodney W. Grimes 	 */
3035480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3036480ba2f5SJohn Dyson 		prot = entry->max_protection;
3037480ba2f5SJohn Dyson 	else
3038df8bae1dSRodney W. Grimes 		prot = entry->protection;
303947221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
304047221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
304147221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
304247221757SJohn Dyson 	}
30432ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
304447221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
30452ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
304647221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3047df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
3048a04c970aSJohn Dyson 	}
3049df8bae1dSRodney W. Grimes 
3050df8bae1dSRodney W. Grimes 	/*
30510d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
30520d94caffSDavid Greenman 	 * accesses.
3053df8bae1dSRodney W. Grimes 	 */
305405f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
305505f0fdd2SPoul-Henning Kamp 	if (*wired)
3056df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
3057df8bae1dSRodney W. Grimes 
3058df8bae1dSRodney W. Grimes 	/*
3059df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3060df8bae1dSRodney W. Grimes 	 */
3061afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3062df8bae1dSRodney W. Grimes 		/*
30630d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3064ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3065df8bae1dSRodney W. Grimes 		 *
30660d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
30670d94caffSDavid Greenman 		 * permissions allowed.
3068df8bae1dSRodney W. Grimes 		 */
3069df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
3070df8bae1dSRodney W. Grimes 			/*
30710d94caffSDavid Greenman 			 * Make a new object, and place it in the object
30720d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3073ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
30740d94caffSDavid Greenman 			 * object.
3075df8bae1dSRodney W. Grimes 			 */
307625adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3077df8bae1dSRodney W. Grimes 				goto RetryLookup;
30789917e010SAlan Cox 
3079df8bae1dSRodney W. Grimes 			vm_object_shadow(
3080df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3081df8bae1dSRodney W. Grimes 			    &entry->offset,
3082c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
3083afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
30849917e010SAlan Cox 
30859b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
30860d94caffSDavid Greenman 		} else {
3087df8bae1dSRodney W. Grimes 			/*
30880d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
30890d94caffSDavid Greenman 			 * don't allow writes.
3090df8bae1dSRodney W. Grimes 			 */
30912d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3092df8bae1dSRodney W. Grimes 		}
3093df8bae1dSRodney W. Grimes 	}
30942d8acc0fSJohn Dyson 
3095df8bae1dSRodney W. Grimes 	/*
3096df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3097df8bae1dSRodney W. Grimes 	 */
30984e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
30994e71e795SMatthew Dillon 	    !map->system_map) {
310025adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3101df8bae1dSRodney W. Grimes 			goto RetryLookup;
310224a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3103c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
3104df8bae1dSRodney W. Grimes 		entry->offset = 0;
31059b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3106df8bae1dSRodney W. Grimes 	}
3107b5b40fa6SJohn Dyson 
3108df8bae1dSRodney W. Grimes 	/*
31090d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
31100d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3111df8bae1dSRodney W. Grimes 	 */
31129b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3113df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3114df8bae1dSRodney W. Grimes 
3115df8bae1dSRodney W. Grimes 	*out_prot = prot;
3116df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3117df8bae1dSRodney W. Grimes 
3118df8bae1dSRodney W. Grimes #undef	RETURN
3119df8bae1dSRodney W. Grimes }
3120df8bae1dSRodney W. Grimes 
3121df8bae1dSRodney W. Grimes /*
312219dc5607STor Egge  *	vm_map_lookup_locked:
312319dc5607STor Egge  *
312419dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
312519dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
312619dc5607STor Egge  */
312719dc5607STor Egge int
312819dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
312919dc5607STor Egge 		     vm_offset_t vaddr,
313019dc5607STor Egge 		     vm_prot_t fault_typea,
313119dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
313219dc5607STor Egge 		     vm_object_t *object,	/* OUT */
313319dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
313419dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
313519dc5607STor Egge 		     boolean_t *wired)		/* OUT */
313619dc5607STor Egge {
313719dc5607STor Egge 	vm_map_entry_t entry;
313819dc5607STor Egge 	vm_map_t map = *var_map;
313919dc5607STor Egge 	vm_prot_t prot;
314019dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
314119dc5607STor Egge 
314219dc5607STor Egge 	/*
314319dc5607STor Egge 	 * If the map has an interesting hint, try it before calling full
314419dc5607STor Egge 	 * blown lookup routine.
314519dc5607STor Egge 	 */
314619dc5607STor Egge 	entry = map->root;
314719dc5607STor Egge 	*out_entry = entry;
314819dc5607STor Egge 	if (entry == NULL ||
314919dc5607STor Egge 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
315019dc5607STor Egge 		/*
315119dc5607STor Egge 		 * Entry was either not a valid hint, or the vaddr was not
315219dc5607STor Egge 		 * contained in the entry, so do a full lookup.
315319dc5607STor Egge 		 */
315419dc5607STor Egge 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
315519dc5607STor Egge 			return (KERN_INVALID_ADDRESS);
315619dc5607STor Egge 
315719dc5607STor Egge 		entry = *out_entry;
315819dc5607STor Egge 	}
315919dc5607STor Egge 
316019dc5607STor Egge 	/*
316119dc5607STor Egge 	 * Fail if the entry refers to a submap.
316219dc5607STor Egge 	 */
316319dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
316419dc5607STor Egge 		return (KERN_FAILURE);
316519dc5607STor Egge 
316619dc5607STor Egge 	/*
316719dc5607STor Egge 	 * Check whether this task is allowed to have this page.
316819dc5607STor Egge 	 * Note the special case for MAP_ENTRY_COW
316919dc5607STor Egge 	 * pages with an override.  This is to implement a forced
317019dc5607STor Egge 	 * COW for debuggers.
317119dc5607STor Egge 	 */
317219dc5607STor Egge 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
317319dc5607STor Egge 		prot = entry->max_protection;
317419dc5607STor Egge 	else
317519dc5607STor Egge 		prot = entry->protection;
317619dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
317719dc5607STor Egge 	if ((fault_type & prot) != fault_type)
317819dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
317919dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
318019dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
318119dc5607STor Egge 	    (fault_type & VM_PROT_WRITE) &&
318219dc5607STor Egge 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
318319dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
318419dc5607STor Egge 
318519dc5607STor Egge 	/*
318619dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
318719dc5607STor Egge 	 * accesses.
318819dc5607STor Egge 	 */
318919dc5607STor Egge 	*wired = (entry->wired_count != 0);
319019dc5607STor Egge 	if (*wired)
319119dc5607STor Egge 		prot = fault_type = entry->protection;
319219dc5607STor Egge 
319319dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
319419dc5607STor Egge 		/*
319519dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
319619dc5607STor Egge 		 */
319719dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
319819dc5607STor Egge 			return (KERN_FAILURE);
319919dc5607STor Egge 		/*
320019dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
320119dc5607STor Egge 		 * don't allow writes.
320219dc5607STor Egge 		 */
320319dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
320419dc5607STor Egge 	}
320519dc5607STor Egge 
320619dc5607STor Egge 	/*
320719dc5607STor Egge 	 * Fail if an object should be created.
320819dc5607STor Egge 	 */
320919dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
321019dc5607STor Egge 		return (KERN_FAILURE);
321119dc5607STor Egge 
321219dc5607STor Egge 	/*
321319dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
321419dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
321519dc5607STor Egge 	 */
321619dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
321719dc5607STor Egge 	*object = entry->object.vm_object;
321819dc5607STor Egge 
321919dc5607STor Egge 	*out_prot = prot;
322019dc5607STor Egge 	return (KERN_SUCCESS);
322119dc5607STor Egge }
322219dc5607STor Egge 
322319dc5607STor Egge /*
3224df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3225df8bae1dSRodney W. Grimes  *
3226df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3227df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3228df8bae1dSRodney W. Grimes  */
32290d94caffSDavid Greenman void
32301b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3231df8bae1dSRodney W. Grimes {
3232df8bae1dSRodney W. Grimes 	/*
3233df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3234df8bae1dSRodney W. Grimes 	 */
3235df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3236df8bae1dSRodney W. Grimes }
3237df8bae1dSRodney W. Grimes 
3238c7c34a24SBruce Evans #include "opt_ddb.h"
3239c3cb3e12SDavid Greenman #ifdef DDB
3240c7c34a24SBruce Evans #include <sys/kernel.h>
3241c7c34a24SBruce Evans 
3242c7c34a24SBruce Evans #include <ddb/ddb.h>
3243c7c34a24SBruce Evans 
3244df8bae1dSRodney W. Grimes /*
3245df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3246df8bae1dSRodney W. Grimes  */
3247c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3248df8bae1dSRodney W. Grimes {
324995e5e988SJohn Dyson 	static int nlines;
3250c7c34a24SBruce Evans 	/* XXX convert args. */
3251c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3252c7c34a24SBruce Evans 	boolean_t full = have_addr;
3253df8bae1dSRodney W. Grimes 
3254c0877f10SJohn Dyson 	vm_map_entry_t entry;
3255c7c34a24SBruce Evans 
3256e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3257e5f251d2SAlan Cox 	    (void *)map,
3258101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
325995e5e988SJohn Dyson 	nlines++;
3260df8bae1dSRodney W. Grimes 
3261c7c34a24SBruce Evans 	if (!full && db_indent)
3262df8bae1dSRodney W. Grimes 		return;
3263df8bae1dSRodney W. Grimes 
3264c7c34a24SBruce Evans 	db_indent += 2;
3265df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3266df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3267fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3268fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
326995e5e988SJohn Dyson 		nlines++;
3270e5f251d2SAlan Cox 		{
3271df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3272df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
32730d94caffSDavid Greenman 
327495e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3275df8bae1dSRodney W. Grimes 			    entry->protection,
3276df8bae1dSRodney W. Grimes 			    entry->max_protection,
32778aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3278df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
327995e5e988SJohn Dyson 				db_printf(", wired");
3280df8bae1dSRodney W. Grimes 		}
32819fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3282cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
32839fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3284cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
328595e5e988SJohn Dyson 			nlines++;
3286df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
32879fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
32889fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3289c7c34a24SBruce Evans 				db_indent += 2;
3290101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
32919fdfe602SMatthew Dillon 					     entry->object.sub_map,
3292914181e7SBruce Evans 					     full, 0, (char *)0);
3293c7c34a24SBruce Evans 				db_indent -= 2;
3294df8bae1dSRodney W. Grimes 			}
32950d94caffSDavid Greenman 		} else {
3296cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3297101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3298cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3299afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3300c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3301afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3302c7c34a24SBruce Evans 			db_printf("\n");
330395e5e988SJohn Dyson 			nlines++;
3304df8bae1dSRodney W. Grimes 
3305df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3306df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3307df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3308c7c34a24SBruce Evans 				db_indent += 2;
3309101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3310101eeb7fSBruce Evans 						entry->object.vm_object,
3311914181e7SBruce Evans 						full, 0, (char *)0);
331295e5e988SJohn Dyson 				nlines += 4;
3313c7c34a24SBruce Evans 				db_indent -= 2;
3314df8bae1dSRodney W. Grimes 			}
3315df8bae1dSRodney W. Grimes 		}
3316df8bae1dSRodney W. Grimes 	}
3317c7c34a24SBruce Evans 	db_indent -= 2;
331895e5e988SJohn Dyson 	if (db_indent == 0)
331995e5e988SJohn Dyson 		nlines = 0;
3320df8bae1dSRodney W. Grimes }
332195e5e988SJohn Dyson 
332295e5e988SJohn Dyson 
332395e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
332495e5e988SJohn Dyson {
332595e5e988SJohn Dyson 	struct proc *p;
332695e5e988SJohn Dyson 
332795e5e988SJohn Dyson 	if (have_addr) {
332895e5e988SJohn Dyson 		p = (struct proc *) addr;
332995e5e988SJohn Dyson 	} else {
333095e5e988SJohn Dyson 		p = curproc;
333195e5e988SJohn Dyson 	}
333295e5e988SJohn Dyson 
3333ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3334ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3335b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
333695e5e988SJohn Dyson 
3337101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
333895e5e988SJohn Dyson }
333995e5e988SJohn Dyson 
3340c7c34a24SBruce Evans #endif /* DDB */
3341