xref: /freebsd/sys/vm/vm_map.c (revision d239bd3ccc23ffed1df1ad7114e303b989e46b94)
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 
15157051fdcSTor Egge /*
15257051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15357051fdcSTor Egge  * stable.
15457051fdcSTor Egge  */
15557051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15657051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15757051fdcSTor Egge 
158d239bd3cSKonstantin Belousov /*
159d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
160d239bd3cSKonstantin Belousov  *
161d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
162d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
163d239bd3cSKonstantin Belousov  */
164d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
165d239bd3cSKonstantin Belousov 		{					\
166d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
167d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
168d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
169d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
170d239bd3cSKonstantin Belousov 		if (start > end)			\
171d239bd3cSKonstantin Belousov 			start = end;			\
172d239bd3cSKonstantin Belousov 		}
173d239bd3cSKonstantin Belousov 
1740d94caffSDavid Greenman void
1751b40f8c0SMatthew Dillon vm_map_startup(void)
176df8bae1dSRodney W. Grimes {
1773a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1788355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1798355f576SJeff Roberson #ifdef INVARIANTS
1808355f576SJeff Roberson 	    vm_map_zdtor,
1818355f576SJeff Roberson #else
1828355f576SJeff Roberson 	    NULL,
1838355f576SJeff Roberson #endif
1848355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1858355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
186670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
18718aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
18818aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
189670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
190670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
191670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
192df8bae1dSRodney W. Grimes }
193df8bae1dSRodney W. Grimes 
1948355f576SJeff Roberson static void
1958355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1968355f576SJeff Roberson {
1978355f576SJeff Roberson 	struct vmspace *vm;
1988355f576SJeff Roberson 
1998355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2003f25cbddSPeter Wemm 	pmap_release(vmspace_pmap(vm));
2018355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
2028355f576SJeff Roberson }
2038355f576SJeff Roberson 
204b23f72e9SBrian Feldman static int
205b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2068355f576SJeff Roberson {
2078355f576SJeff Roberson 	struct vmspace *vm;
2088355f576SJeff Roberson 
2098355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2108355f576SJeff Roberson 
211b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
2123f25cbddSPeter Wemm 	pmap_pinit(vmspace_pmap(vm));
213b23f72e9SBrian Feldman 	return (0);
2148355f576SJeff Roberson }
2158355f576SJeff Roberson 
2168355f576SJeff Roberson static void
2178355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2188355f576SJeff Roberson {
2198355f576SJeff Roberson 	vm_map_t map;
2208355f576SJeff Roberson 
2218355f576SJeff Roberson 	map = (vm_map_t)mem;
22236daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
22312c64974SMaxime Henrion 	sx_destroy(&map->lock);
2248355f576SJeff Roberson }
2258355f576SJeff Roberson 
226b23f72e9SBrian Feldman static int
227b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2288355f576SJeff Roberson {
2298355f576SJeff Roberson 	vm_map_t map;
2308355f576SJeff Roberson 
2318355f576SJeff Roberson 	map = (vm_map_t)mem;
2328355f576SJeff Roberson 	map->nentries = 0;
2338355f576SJeff Roberson 	map->size = 0;
234d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
23512c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
236b23f72e9SBrian Feldman 	return (0);
2378355f576SJeff Roberson }
2388355f576SJeff Roberson 
2398355f576SJeff Roberson #ifdef INVARIANTS
2408355f576SJeff Roberson static void
2418355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2428355f576SJeff Roberson {
2438355f576SJeff Roberson 	struct vmspace *vm;
2448355f576SJeff Roberson 
2458355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2468355f576SJeff Roberson 
2478355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2488355f576SJeff Roberson }
2498355f576SJeff Roberson static void
2508355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2518355f576SJeff Roberson {
2528355f576SJeff Roberson 	vm_map_t map;
2538355f576SJeff Roberson 
2548355f576SJeff Roberson 	map = (vm_map_t)mem;
2558355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2568355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2578355f576SJeff Roberson 	    map, map->nentries));
2588355f576SJeff Roberson 	KASSERT(map->size == 0,
2598355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2609eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2618355f576SJeff Roberson }
2628355f576SJeff Roberson #endif	/* INVARIANTS */
2638355f576SJeff Roberson 
264df8bae1dSRodney W. Grimes /*
265df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
266df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes struct vmspace *
2692d8acc0fSJohn Dyson vmspace_alloc(min, max)
270df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
271df8bae1dSRodney W. Grimes {
272c0877f10SJohn Dyson 	struct vmspace *vm;
2730d94caffSDavid Greenman 
274a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
27521c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
2768355f576SJeff Roberson 	_vm_map_init(&vm->vm_map, min, max);
277b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
278df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2792d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
28051ab6c28SAlan Cox 	vm->vm_swrss = 0;
28151ab6c28SAlan Cox 	vm->vm_tsize = 0;
28251ab6c28SAlan Cox 	vm->vm_dsize = 0;
28351ab6c28SAlan Cox 	vm->vm_ssize = 0;
28451ab6c28SAlan Cox 	vm->vm_taddr = 0;
28551ab6c28SAlan Cox 	vm->vm_daddr = 0;
28651ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
287df8bae1dSRodney W. Grimes 	return (vm);
288df8bae1dSRodney W. Grimes }
289df8bae1dSRodney W. Grimes 
290df8bae1dSRodney W. Grimes void
2911b40f8c0SMatthew Dillon vm_init2(void)
2921b40f8c0SMatthew Dillon {
2932feb50bfSAttilio Rao 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
2943fde38dfSMike Silbersack 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
2953fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
2968355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2978355f576SJeff Roberson #ifdef INVARIANTS
2988355f576SJeff Roberson 	    vmspace_zdtor,
2998355f576SJeff Roberson #else
3008355f576SJeff Roberson 	    NULL,
3018355f576SJeff Roberson #endif
3028355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3033075778bSJohn Dyson }
3043075778bSJohn Dyson 
30562a59e8fSWarner Losh static inline void
306582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
307df8bae1dSRodney W. Grimes {
30821c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3093db161e0SMatthew Dillon 
3103db161e0SMatthew Dillon 	/*
3113db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3123db161e0SMatthew Dillon 	 * exit1().
3133db161e0SMatthew Dillon 	 */
3143db161e0SMatthew Dillon 	shmexit(vm);
3153db161e0SMatthew Dillon 
31630dcfc09SJohn Dyson 	/*
317df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3180d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3190d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
320df8bae1dSRodney W. Grimes 	 */
321717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
322df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3238355f576SJeff Roberson 
3248355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
325df8bae1dSRodney W. Grimes }
326582ec34cSAlfred Perlstein 
327582ec34cSAlfred Perlstein void
328582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
329582ec34cSAlfred Perlstein {
3301a276a3fSAlan Cox 	int refcnt;
331582ec34cSAlfred Perlstein 
332582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
333582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
334582ec34cSAlfred Perlstein 
3351a276a3fSAlan Cox 	do
3361a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3371a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
33857051fdcSTor Egge 	if (refcnt == 1)
339582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
340582ec34cSAlfred Perlstein }
341582ec34cSAlfred Perlstein 
342582ec34cSAlfred Perlstein void
343582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
344582ec34cSAlfred Perlstein {
345334f7061SPeter Wemm 	struct vmspace *vm;
346582ec34cSAlfred Perlstein 
34757051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
348334f7061SPeter Wemm 	vm = p->p_vmspace;
349334f7061SPeter Wemm 	p->p_vmspace = NULL;
35057051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
35157051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
35257051fdcSTor Egge 	vmspace_free(vm);
35357051fdcSTor Egge }
35457051fdcSTor Egge 
35557051fdcSTor Egge void
35657051fdcSTor Egge vmspace_exit(struct thread *td)
35757051fdcSTor Egge {
35857051fdcSTor Egge 	int refcnt;
35957051fdcSTor Egge 	struct vmspace *vm;
36057051fdcSTor Egge 	struct proc *p;
361389d2b6eSMatthew Dillon 
362389d2b6eSMatthew Dillon 	/*
36357051fdcSTor Egge 	 * Release user portion of address space.
36457051fdcSTor Egge 	 * This releases references to vnodes,
36557051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
36657051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
367389d2b6eSMatthew Dillon 	 *
36857051fdcSTor Egge 	 * The last exiting process to reach this point releases as
36957051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
37057051fdcSTor Egge 	 * slower fallback in case another process had a temporary
37157051fdcSTor Egge 	 * reference to the vmspace.
372389d2b6eSMatthew Dillon 	 */
37357051fdcSTor Egge 
37457051fdcSTor Egge 	p = td->td_proc;
37557051fdcSTor Egge 	vm = p->p_vmspace;
37657051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
37757051fdcSTor Egge 	do {
37857051fdcSTor Egge 		refcnt = vm->vm_refcnt;
37957051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
38057051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
38157051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
38257051fdcSTor Egge 			p->p_vmspace = &vmspace0;
38357051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
38457051fdcSTor Egge 			pmap_activate(td);
38557051fdcSTor Egge 		}
38657051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
38757051fdcSTor Egge 	if (refcnt == 1) {
38857051fdcSTor Egge 		if (p->p_vmspace != vm) {
38957051fdcSTor Egge 			/* vmspace not yet freed, switch back */
39057051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39157051fdcSTor Egge 			p->p_vmspace = vm;
39257051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39357051fdcSTor Egge 			pmap_activate(td);
39457051fdcSTor Egge 		}
39557051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
39657051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
39757051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
39857051fdcSTor Egge 		p->p_vmspace = &vmspace0;
39957051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
40057051fdcSTor Egge 		pmap_activate(td);
401334f7061SPeter Wemm 		vmspace_dofree(vm);
402334f7061SPeter Wemm 	}
40357051fdcSTor Egge }
40457051fdcSTor Egge 
40557051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
40657051fdcSTor Egge 
40757051fdcSTor Egge struct vmspace *
40857051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
40957051fdcSTor Egge {
41057051fdcSTor Egge 	struct vmspace *vm;
41157051fdcSTor Egge 	int refcnt;
41257051fdcSTor Egge 
41357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
41457051fdcSTor Egge 	vm = p->p_vmspace;
41557051fdcSTor Egge 	if (vm == NULL) {
41657051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41757051fdcSTor Egge 		return (NULL);
41857051fdcSTor Egge 	}
41957051fdcSTor Egge 	do {
42057051fdcSTor Egge 		refcnt = vm->vm_refcnt;
42157051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
42257051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
42357051fdcSTor Egge 			return (NULL);
42457051fdcSTor Egge 		}
42557051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
42657051fdcSTor Egge 	if (vm != p->p_vmspace) {
42757051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42857051fdcSTor Egge 		vmspace_free(vm);
42957051fdcSTor Egge 		return (NULL);
43057051fdcSTor Egge 	}
43157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
43257051fdcSTor Egge 	return (vm);
43357051fdcSTor Egge }
434df8bae1dSRodney W. Grimes 
4351b40f8c0SMatthew Dillon void
436780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4371b40f8c0SMatthew Dillon {
438bc91c510SAlan Cox 
43993bc4879SAlan Cox 	if (map->system_map)
44036daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
44112c64974SMaxime Henrion 	else
442f9819486SAttilio Rao 		(void)_sx_xlock(&map->lock, 0, file, line);
4431b40f8c0SMatthew Dillon 	map->timestamp++;
4441b40f8c0SMatthew Dillon }
4451b40f8c0SMatthew Dillon 
4461b40f8c0SMatthew Dillon void
447780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
4480e0af8ecSBrian Feldman {
449bc91c510SAlan Cox 
45036daaecdSAlan Cox 	if (map->system_map)
45136daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
45236daaecdSAlan Cox 	else
45312c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
4540e0af8ecSBrian Feldman }
4550e0af8ecSBrian Feldman 
4560e0af8ecSBrian Feldman void
457780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
4580e0af8ecSBrian Feldman {
459bc91c510SAlan Cox 
46093bc4879SAlan Cox 	if (map->system_map)
46136daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
46212c64974SMaxime Henrion 	else
463f9819486SAttilio Rao 		(void)_sx_xlock(&map->lock, 0, file, line);
46436daaecdSAlan Cox }
4650e0af8ecSBrian Feldman 
4660e0af8ecSBrian Feldman void
467780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
4680e0af8ecSBrian Feldman {
469bc91c510SAlan Cox 
47036daaecdSAlan Cox 	if (map->system_map)
47136daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
47236daaecdSAlan Cox 	else
47312c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
47425adb370SBrian Feldman }
47525adb370SBrian Feldman 
476d974f03cSAlan Cox int
477780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
478d974f03cSAlan Cox {
47925adb370SBrian Feldman 	int error;
48025adb370SBrian Feldman 
48136daaecdSAlan Cox 	error = map->system_map ?
48236daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
48312c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
4843a92e5d5SAlan Cox 	if (error == 0)
4853a92e5d5SAlan Cox 		map->timestamp++;
486bc91c510SAlan Cox 	return (error == 0);
4870e0af8ecSBrian Feldman }
4880e0af8ecSBrian Feldman 
4890e0af8ecSBrian Feldman int
49072d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
49172d97679SDavid Schultz {
49272d97679SDavid Schultz 	int error;
49372d97679SDavid Schultz 
49472d97679SDavid Schultz 	error = map->system_map ?
49572d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
49612c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
49772d97679SDavid Schultz 	return (error == 0);
49872d97679SDavid Schultz }
49972d97679SDavid Schultz 
50072d97679SDavid Schultz int
501780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5020e0af8ecSBrian Feldman {
503bc91c510SAlan Cox 
50436daaecdSAlan Cox #ifdef INVARIANTS
50512c64974SMaxime Henrion 	if (map->system_map) {
50636daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
50736daaecdSAlan Cox 	} else
50812c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
50912c64974SMaxime Henrion #endif
510bc91c510SAlan Cox 	map->timestamp++;
511bc91c510SAlan Cox 	return (0);
5120e0af8ecSBrian Feldman }
5130e0af8ecSBrian Feldman 
5140e0af8ecSBrian Feldman void
515780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5161b40f8c0SMatthew Dillon {
517bc91c510SAlan Cox 
51836daaecdSAlan Cox #ifdef INVARIANTS
51912c64974SMaxime Henrion 	if (map->system_map) {
52036daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
52136daaecdSAlan Cox 	} else
52212c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
52312c64974SMaxime Henrion #endif
52425adb370SBrian Feldman }
52525adb370SBrian Feldman 
526acd9a301SAlan Cox /*
527acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
528acd9a301SAlan Cox  */
5299688f931SAlan Cox int
530acd9a301SAlan Cox vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
531acd9a301SAlan Cox {
532acd9a301SAlan Cox 
5333a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
534acd9a301SAlan Cox 	vm_map_unlock(map);
5353a92e5d5SAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
536acd9a301SAlan Cox }
537acd9a301SAlan Cox 
538acd9a301SAlan Cox /*
539acd9a301SAlan Cox  *	vm_map_wakeup:
540acd9a301SAlan Cox  */
5419688f931SAlan Cox void
542acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
543acd9a301SAlan Cox {
544acd9a301SAlan Cox 
545b49ecb86SAlan Cox 	/*
5463a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
5473a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
5483a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
549b49ecb86SAlan Cox 	 */
5503a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
5513a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
552acd9a301SAlan Cox 	wakeup(&map->root);
553acd9a301SAlan Cox }
554acd9a301SAlan Cox 
5551b40f8c0SMatthew Dillon long
5561b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
5571b40f8c0SMatthew Dillon {
5581b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
5591b40f8c0SMatthew Dillon }
5601b40f8c0SMatthew Dillon 
5612bc7dd56SBruce M Simpson long
5622bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
5632bc7dd56SBruce M Simpson {
5642bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
5652bc7dd56SBruce M Simpson }
5662bc7dd56SBruce M Simpson 
567ff2b5645SMatthew Dillon /*
568df8bae1dSRodney W. Grimes  *	vm_map_create:
569df8bae1dSRodney W. Grimes  *
570df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
571df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
572df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
573df8bae1dSRodney W. Grimes  */
5740d94caffSDavid Greenman vm_map_t
5751b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
576df8bae1dSRodney W. Grimes {
577c0877f10SJohn Dyson 	vm_map_t result;
578df8bae1dSRodney W. Grimes 
579a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
58021c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
5818355f576SJeff Roberson 	_vm_map_init(result, min, max);
582df8bae1dSRodney W. Grimes 	result->pmap = pmap;
583df8bae1dSRodney W. Grimes 	return (result);
584df8bae1dSRodney W. Grimes }
585df8bae1dSRodney W. Grimes 
586df8bae1dSRodney W. Grimes /*
587df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
588df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
589df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
590df8bae1dSRodney W. Grimes  */
5918355f576SJeff Roberson static void
5928355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
593df8bae1dSRodney W. Grimes {
59421c641b2SJohn Baldwin 
595df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
5969688f931SAlan Cox 	map->needs_wakeup = FALSE;
5973075778bSJohn Dyson 	map->system_map = 0;
598df8bae1dSRodney W. Grimes 	map->min_offset = min;
599df8bae1dSRodney W. Grimes 	map->max_offset = max;
600af7cd0c5SBrian Feldman 	map->flags = 0;
6014e94f402SAlan Cox 	map->root = NULL;
602df8bae1dSRodney W. Grimes 	map->timestamp = 0;
603df8bae1dSRodney W. Grimes }
604df8bae1dSRodney W. Grimes 
605a18b1f1dSJason Evans void
6068355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
607a18b1f1dSJason Evans {
6088355f576SJeff Roberson 	_vm_map_init(map, min, max);
609d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
61012c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
611a18b1f1dSJason Evans }
612a18b1f1dSJason Evans 
613df8bae1dSRodney W. Grimes /*
614b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
615b18bfc3dSJohn Dyson  *
616b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
617b18bfc3dSJohn Dyson  */
61862487bb4SJohn Dyson static void
6191b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
620b18bfc3dSJohn Dyson {
6212b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
622b18bfc3dSJohn Dyson }
623b18bfc3dSJohn Dyson 
624b18bfc3dSJohn Dyson /*
625df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
626df8bae1dSRodney W. Grimes  *
627df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
628b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
629df8bae1dSRodney W. Grimes  */
630f708ef1bSPoul-Henning Kamp static vm_map_entry_t
6311b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
632df8bae1dSRodney W. Grimes {
6331f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
6341f6889a1SMatthew Dillon 
6352b4a2c27SAlan Cox 	if (map->system_map)
6362b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
6372b4a2c27SAlan Cox 	else
638a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
6391f6889a1SMatthew Dillon 	if (new_entry == NULL)
6401f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
6411f6889a1SMatthew Dillon 	return (new_entry);
642df8bae1dSRodney W. Grimes }
643df8bae1dSRodney W. Grimes 
644df8bae1dSRodney W. Grimes /*
645794316a8SAlan Cox  *	vm_map_entry_set_behavior:
646794316a8SAlan Cox  *
647794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
648794316a8SAlan Cox  *	sequential.
649794316a8SAlan Cox  */
65062a59e8fSWarner Losh static inline void
651794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
652794316a8SAlan Cox {
653794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
654794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
655794316a8SAlan Cox }
656794316a8SAlan Cox 
657794316a8SAlan Cox /*
6580164e057SAlan Cox  *	vm_map_entry_set_max_free:
6590164e057SAlan Cox  *
6600164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
6610164e057SAlan Cox  */
66262a59e8fSWarner Losh static inline void
6630164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
6640164e057SAlan Cox {
6650164e057SAlan Cox 
6660164e057SAlan Cox 	entry->max_free = entry->adj_free;
6670164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
6680164e057SAlan Cox 		entry->max_free = entry->left->max_free;
6690164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
6700164e057SAlan Cox 		entry->max_free = entry->right->max_free;
6710164e057SAlan Cox }
6720164e057SAlan Cox 
6730164e057SAlan Cox /*
6744e94f402SAlan Cox  *	vm_map_entry_splay:
6754e94f402SAlan Cox  *
6760164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
6770164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
6780164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
6790164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
6800164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
6810164e057SAlan Cox  *	amortized.
6820164e057SAlan Cox  *
6830164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
6840164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
6850164e057SAlan Cox  *
6860164e057SAlan Cox  *	The map must be locked, and leaves it so.
6870164e057SAlan Cox  *
6880164e057SAlan Cox  *	Returns: the new root.
6894e94f402SAlan Cox  */
6904e94f402SAlan Cox static vm_map_entry_t
6910164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
6924e94f402SAlan Cox {
6930164e057SAlan Cox 	vm_map_entry_t llist, rlist;
6940164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
6950164e057SAlan Cox 	vm_map_entry_t y;
6964e94f402SAlan Cox 
6970164e057SAlan Cox 	/* Special case of empty tree. */
6984e94f402SAlan Cox 	if (root == NULL)
6994e94f402SAlan Cox 		return (root);
7000164e057SAlan Cox 
7010164e057SAlan Cox 	/*
7020164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
7030164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
7040164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
7050164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
7060164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
7070164e057SAlan Cox 	 * the two spines.
7080164e057SAlan Cox 	 */
7090164e057SAlan Cox 	llist = NULL;
7100164e057SAlan Cox 	rlist = NULL;
7110164e057SAlan Cox 	for (;;) {
7120164e057SAlan Cox 		/* root is never NULL in here. */
7130164e057SAlan Cox 		if (addr < root->start) {
7140164e057SAlan Cox 			y = root->left;
7150164e057SAlan Cox 			if (y == NULL)
7164e94f402SAlan Cox 				break;
7170164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
7180164e057SAlan Cox 				/* Rotate right and put y on rlist. */
7194e94f402SAlan Cox 				root->left = y->right;
7204e94f402SAlan Cox 				y->right = root;
7210164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7220164e057SAlan Cox 				root = y->left;
7230164e057SAlan Cox 				y->left = rlist;
7240164e057SAlan Cox 				rlist = y;
7250164e057SAlan Cox 			} else {
7260164e057SAlan Cox 				/* Put root on rlist. */
7270164e057SAlan Cox 				root->left = rlist;
7280164e057SAlan Cox 				rlist = root;
7294e94f402SAlan Cox 				root = y;
7304e94f402SAlan Cox 			}
7310164e057SAlan Cox 		} else {
7320164e057SAlan Cox 			y = root->right;
7330164e057SAlan Cox 			if (addr < root->end || y == NULL)
7344e94f402SAlan Cox 				break;
7350164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
7360164e057SAlan Cox 				/* Rotate left and put y on llist. */
7374e94f402SAlan Cox 				root->right = y->left;
7384e94f402SAlan Cox 				y->left = root;
7390164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7400164e057SAlan Cox 				root = y->right;
7410164e057SAlan Cox 				y->right = llist;
7420164e057SAlan Cox 				llist = y;
7430164e057SAlan Cox 			} else {
7440164e057SAlan Cox 				/* Put root on llist. */
7450164e057SAlan Cox 				root->right = llist;
7460164e057SAlan Cox 				llist = root;
7474e94f402SAlan Cox 				root = y;
7484e94f402SAlan Cox 			}
7494e94f402SAlan Cox 		}
7500164e057SAlan Cox 	}
7510164e057SAlan Cox 
7520164e057SAlan Cox 	/*
7530164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
7540164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
7550164e057SAlan Cox 	 * bottom of llist and rlist.
7560164e057SAlan Cox 	 */
7570164e057SAlan Cox 	ltree = root->left;
7580164e057SAlan Cox 	while (llist != NULL) {
7590164e057SAlan Cox 		y = llist->right;
7600164e057SAlan Cox 		llist->right = ltree;
7610164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
7620164e057SAlan Cox 		ltree = llist;
7630164e057SAlan Cox 		llist = y;
7640164e057SAlan Cox 	}
7650164e057SAlan Cox 	rtree = root->right;
7660164e057SAlan Cox 	while (rlist != NULL) {
7670164e057SAlan Cox 		y = rlist->left;
7680164e057SAlan Cox 		rlist->left = rtree;
7690164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
7700164e057SAlan Cox 		rtree = rlist;
7710164e057SAlan Cox 		rlist = y;
7720164e057SAlan Cox 	}
7730164e057SAlan Cox 
7740164e057SAlan Cox 	/*
7750164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
7760164e057SAlan Cox 	 */
7770164e057SAlan Cox 	root->left = ltree;
7780164e057SAlan Cox 	root->right = rtree;
7790164e057SAlan Cox 	vm_map_entry_set_max_free(root);
7800164e057SAlan Cox 
7814e94f402SAlan Cox 	return (root);
7824e94f402SAlan Cox }
7834e94f402SAlan Cox 
7844e94f402SAlan Cox /*
785df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
786df8bae1dSRodney W. Grimes  *
787df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
788df8bae1dSRodney W. Grimes  */
7894e94f402SAlan Cox static void
79099c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
79199c81ca9SAlan Cox 		  vm_map_entry_t after_where,
79299c81ca9SAlan Cox 		  vm_map_entry_t entry)
79399c81ca9SAlan Cox {
79421c641b2SJohn Baldwin 
79521c641b2SJohn Baldwin 	CTR4(KTR_VM,
79621c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
79721c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
79899c81ca9SAlan Cox 	map->nentries++;
79999c81ca9SAlan Cox 	entry->prev = after_where;
80099c81ca9SAlan Cox 	entry->next = after_where->next;
80199c81ca9SAlan Cox 	entry->next->prev = entry;
80299c81ca9SAlan Cox 	after_where->next = entry;
8034e94f402SAlan Cox 
8044e94f402SAlan Cox 	if (after_where != &map->header) {
8054e94f402SAlan Cox 		if (after_where != map->root)
8064e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
8074e94f402SAlan Cox 		entry->right = after_where->right;
8084e94f402SAlan Cox 		entry->left = after_where;
8094e94f402SAlan Cox 		after_where->right = NULL;
8100164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
8110164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
8124e94f402SAlan Cox 	} else {
8134e94f402SAlan Cox 		entry->right = map->root;
8144e94f402SAlan Cox 		entry->left = NULL;
8154e94f402SAlan Cox 	}
8160164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
8170164e057SAlan Cox 	    entry->next->start) - entry->end;
8180164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
8194e94f402SAlan Cox 	map->root = entry;
820df8bae1dSRodney W. Grimes }
82199c81ca9SAlan Cox 
8224e94f402SAlan Cox static void
82399c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
82499c81ca9SAlan Cox 		    vm_map_entry_t entry)
82599c81ca9SAlan Cox {
8264e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
82799c81ca9SAlan Cox 
8284e94f402SAlan Cox 	if (entry != map->root)
8294e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
8304e94f402SAlan Cox 	if (entry->left == NULL)
8314e94f402SAlan Cox 		root = entry->right;
8324e94f402SAlan Cox 	else {
8334e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
8344e94f402SAlan Cox 		root->right = entry->right;
8350164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
8360164e057SAlan Cox 		    entry->next->start) - root->end;
8370164e057SAlan Cox 		vm_map_entry_set_max_free(root);
8384e94f402SAlan Cox 	}
8394e94f402SAlan Cox 	map->root = root;
8404e94f402SAlan Cox 
8414e94f402SAlan Cox 	prev = entry->prev;
8424e94f402SAlan Cox 	next = entry->next;
84399c81ca9SAlan Cox 	next->prev = prev;
84499c81ca9SAlan Cox 	prev->next = next;
84599c81ca9SAlan Cox 	map->nentries--;
84621c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
84721c641b2SJohn Baldwin 	    map->nentries, entry);
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes /*
8510164e057SAlan Cox  *	vm_map_entry_resize_free:
8520164e057SAlan Cox  *
8530164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
8540164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
8550164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
8560164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
8570164e057SAlan Cox  *
8580164e057SAlan Cox  *	The map must be locked, and leaves it so.
8590164e057SAlan Cox  */
8600164e057SAlan Cox static void
8610164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
8620164e057SAlan Cox {
8630164e057SAlan Cox 
8640164e057SAlan Cox 	/*
8650164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
8660164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
8670164e057SAlan Cox 	 * root and making the change there.
8680164e057SAlan Cox 	 */
8690164e057SAlan Cox 	if (entry != map->root)
8700164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
8710164e057SAlan Cox 
8720164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
8730164e057SAlan Cox 	    entry->next->start) - entry->end;
8740164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
8750164e057SAlan Cox }
8760164e057SAlan Cox 
8770164e057SAlan Cox /*
878df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
879df8bae1dSRodney W. Grimes  *
880df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
881df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
882df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
883df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
884df8bae1dSRodney W. Grimes  *	result indicates whether the address is
885df8bae1dSRodney W. Grimes  *	actually contained in the map.
886df8bae1dSRodney W. Grimes  */
8870d94caffSDavid Greenman boolean_t
8881b40f8c0SMatthew Dillon vm_map_lookup_entry(
8891b40f8c0SMatthew Dillon 	vm_map_t map,
8901b40f8c0SMatthew Dillon 	vm_offset_t address,
8911b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
892df8bae1dSRodney W. Grimes {
893c0877f10SJohn Dyson 	vm_map_entry_t cur;
894df8bae1dSRodney W. Grimes 
8954e94f402SAlan Cox 	cur = vm_map_entry_splay(address, map->root);
8964e94f402SAlan Cox 	if (cur == NULL)
8974e94f402SAlan Cox 		*entry = &map->header;
8984e94f402SAlan Cox 	else {
8994e94f402SAlan Cox 		map->root = cur;
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
902df8bae1dSRodney W. Grimes 			*entry = cur;
9034e94f402SAlan Cox 			if (cur->end > address)
904df8bae1dSRodney W. Grimes 				return (TRUE);
9054e94f402SAlan Cox 		} else
906df8bae1dSRodney W. Grimes 			*entry = cur->prev;
9074e94f402SAlan Cox 	}
908df8bae1dSRodney W. Grimes 	return (FALSE);
909df8bae1dSRodney W. Grimes }
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes /*
91230dcfc09SJohn Dyson  *	vm_map_insert:
91330dcfc09SJohn Dyson  *
91430dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
91530dcfc09SJohn Dyson  *	map at the specified address range.  The object's
91630dcfc09SJohn Dyson  *	size should match that of the address range.
91730dcfc09SJohn Dyson  *
91830dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
9192aaeadf8SMatthew Dillon  *
9202aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
9212aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
92230dcfc09SJohn Dyson  */
92330dcfc09SJohn Dyson int
924b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
925b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
926b9dcd593SBruce Evans 	      int cow)
92730dcfc09SJohn Dyson {
928c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
929c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
93030dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
9319730a5daSPaul Saab 	vm_eflags_t protoeflags;
93230dcfc09SJohn Dyson 
93330dcfc09SJohn Dyson 	/*
93430dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
93530dcfc09SJohn Dyson 	 */
93630dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
93730dcfc09SJohn Dyson 	    (start >= end))
93830dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
93930dcfc09SJohn Dyson 
94030dcfc09SJohn Dyson 	/*
94130dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
94230dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
94330dcfc09SJohn Dyson 	 */
94430dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
94530dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
94630dcfc09SJohn Dyson 
94730dcfc09SJohn Dyson 	prev_entry = temp_entry;
94830dcfc09SJohn Dyson 
94930dcfc09SJohn Dyson 	/*
95030dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
95130dcfc09SJohn Dyson 	 */
95230dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
95330dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
95430dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
95530dcfc09SJohn Dyson 
956afa07f7eSJohn Dyson 	protoeflags = 0;
957afa07f7eSJohn Dyson 
958afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
959e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
960afa07f7eSJohn Dyson 
9614e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
962afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
963afa07f7eSJohn Dyson 
9644e045f93SAlan Cox 		KASSERT(object == NULL,
9654e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
9664e045f93SAlan Cox 	}
9674f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
9684f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
9699730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
9709730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
9714f79d873SMatthew Dillon 
9721d284e00SAlan Cox 	if (object != NULL) {
97330dcfc09SJohn Dyson 		/*
9741d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
9751d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
9761d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
9771d284e00SAlan Cox 		 * reference counting is insufficient to recognize
9781d284e00SAlan Cox 		 * aliases with precision.)
97930dcfc09SJohn Dyson 		 */
9801d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
9811d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
9822aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
9831d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
9844e045f93SAlan Cox 	}
9854e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
9864e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
9878cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
9884e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
9894e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
9908cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
99157a21abaSAlan Cox 				     prev_entry->offset,
9928cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
993cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
99430dcfc09SJohn Dyson 		/*
9952aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
9962aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
9972aaeadf8SMatthew Dillon 		 * new range as well.
99830dcfc09SJohn Dyson 		 */
9998cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
10008cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
10018cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
100230dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
100330dcfc09SJohn Dyson 			prev_entry->end = end;
10040164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
10054e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
100630dcfc09SJohn Dyson 			return (KERN_SUCCESS);
100730dcfc09SJohn Dyson 		}
10088cc7e047SJohn Dyson 
10092aaeadf8SMatthew Dillon 		/*
10102aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
10112aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
10122aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
10134e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
10142aaeadf8SMatthew Dillon 		 */
10152aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
10162aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
10172aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
10188cc7e047SJohn Dyson 		vm_object_reference(object);
1019b18bfc3dSJohn Dyson 	}
10202aaeadf8SMatthew Dillon 
10212aaeadf8SMatthew Dillon 	/*
10222aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
10232aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
10242aaeadf8SMatthew Dillon 	 * backing objects.
10252aaeadf8SMatthew Dillon 	 */
10268cc7e047SJohn Dyson 
102730dcfc09SJohn Dyson 	/*
102830dcfc09SJohn Dyson 	 * Create a new entry
102930dcfc09SJohn Dyson 	 */
103030dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
103130dcfc09SJohn Dyson 	new_entry->start = start;
103230dcfc09SJohn Dyson 	new_entry->end = end;
103330dcfc09SJohn Dyson 
1034afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
103530dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
103630dcfc09SJohn Dyson 	new_entry->offset = offset;
10372267af78SJulian Elischer 	new_entry->avail_ssize = 0;
10382267af78SJulian Elischer 
103930dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
104030dcfc09SJohn Dyson 	new_entry->protection = prot;
104130dcfc09SJohn Dyson 	new_entry->max_protection = max;
104230dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1043e5f251d2SAlan Cox 
104430dcfc09SJohn Dyson 	/*
104530dcfc09SJohn Dyson 	 * Insert the new entry into the list
104630dcfc09SJohn Dyson 	 */
104730dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
104830dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
104930dcfc09SJohn Dyson 
10501a484d28SMatthew Dillon #if 0
10511a484d28SMatthew Dillon 	/*
10521a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
10531a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
10541a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
10551a484d28SMatthew Dillon 	 */
10564e71e795SMatthew Dillon 	/*
10574e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
10584e71e795SMatthew Dillon 	 */
10594e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
10601a484d28SMatthew Dillon #endif
10614e71e795SMatthew Dillon 
10624f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
10634da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1064e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1065e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
10664f79d873SMatthew Dillon 	}
1067e972780aSAlan Cox 
106830dcfc09SJohn Dyson 	return (KERN_SUCCESS);
106930dcfc09SJohn Dyson }
107030dcfc09SJohn Dyson 
107130dcfc09SJohn Dyson /*
10720164e057SAlan Cox  *	vm_map_findspace:
10730164e057SAlan Cox  *
10740164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
10750164e057SAlan Cox  *	beginning at address >= start in the given map.
10760164e057SAlan Cox  *
10770164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
10780164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
10790164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
10800164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
10810164e057SAlan Cox  *	O(log n) amortized with splay trees.
10820164e057SAlan Cox  *
10830164e057SAlan Cox  *	The map must be locked, and leaves it so.
10840164e057SAlan Cox  *
10850164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
10860164e057SAlan Cox  *		 1 if insufficient space.
1087df8bae1dSRodney W. Grimes  */
1088df8bae1dSRodney W. Grimes int
10890164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
10900164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1091df8bae1dSRodney W. Grimes {
10920164e057SAlan Cox 	vm_map_entry_t entry;
10930164e057SAlan Cox 	vm_offset_t end, st;
1094df8bae1dSRodney W. Grimes 
1095986b43f8SAlan Cox 	/*
1096986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1097986b43f8SAlan Cox 	 * address wrap.
1098986b43f8SAlan Cox 	 */
1099df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1100df8bae1dSRodney W. Grimes 		start = map->min_offset;
1101986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1102df8bae1dSRodney W. Grimes 		return (1);
1103df8bae1dSRodney W. Grimes 
11040164e057SAlan Cox 	/* Empty tree means wide open address space. */
11050164e057SAlan Cox 	if (map->root == NULL) {
1106df8bae1dSRodney W. Grimes 		*addr = start;
11070164e057SAlan Cox 		goto found;
110899448ed1SJohn Dyson 	}
11090164e057SAlan Cox 
11100164e057SAlan Cox 	/*
11110164e057SAlan Cox 	 * After splay, if start comes before root node, then there
11120164e057SAlan Cox 	 * must be a gap from start to the root.
11130164e057SAlan Cox 	 */
11140164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
11150164e057SAlan Cox 	if (start + length <= map->root->start) {
11160164e057SAlan Cox 		*addr = start;
11170164e057SAlan Cox 		goto found;
11180164e057SAlan Cox 	}
11190164e057SAlan Cox 
11200164e057SAlan Cox 	/*
11210164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1122986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1123986b43f8SAlan Cox 	 * wrap might be a problem.
11240164e057SAlan Cox 	 */
11250164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1126986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
11270164e057SAlan Cox 		*addr = st;
11280164e057SAlan Cox 		goto found;
11290164e057SAlan Cox 	}
11300164e057SAlan Cox 
11310164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
11320164e057SAlan Cox 	entry = map->root->right;
11330164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
11340164e057SAlan Cox 		return (1);
11350164e057SAlan Cox 
11360164e057SAlan Cox 	/*
11370164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
11380164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
11390164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
11400164e057SAlan Cox 	 */
11410164e057SAlan Cox 	while (entry != NULL) {
11420164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
11430164e057SAlan Cox 			entry = entry->left;
11440164e057SAlan Cox 		else if (entry->adj_free >= length) {
11450164e057SAlan Cox 			*addr = entry->end;
11460164e057SAlan Cox 			goto found;
11470164e057SAlan Cox 		} else
11480164e057SAlan Cox 			entry = entry->right;
11490164e057SAlan Cox 	}
11500164e057SAlan Cox 
11510164e057SAlan Cox 	/* Can't get here, so panic if we do. */
11520164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
11530164e057SAlan Cox 
11540164e057SAlan Cox found:
11550164e057SAlan Cox 	/* Expand the kernel pmap, if necessary. */
11560164e057SAlan Cox 	if (map == kernel_map) {
11570164e057SAlan Cox 		end = round_page(*addr + length);
11580164e057SAlan Cox 		if (end > kernel_vm_end)
11590164e057SAlan Cox 			pmap_growkernel(end);
116099448ed1SJohn Dyson 	}
1161df8bae1dSRodney W. Grimes 	return (0);
1162df8bae1dSRodney W. Grimes }
1163df8bae1dSRodney W. Grimes 
1164d239bd3cSKonstantin Belousov int
1165d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1166d239bd3cSKonstantin Belousov     vm_offset_t *addr /* IN/OUT */, vm_size_t length, vm_prot_t prot,
1167d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1168d239bd3cSKonstantin Belousov {
1169d239bd3cSKonstantin Belousov 	vm_offset_t start, end;
1170d239bd3cSKonstantin Belousov 	int result;
1171d239bd3cSKonstantin Belousov 
1172d239bd3cSKonstantin Belousov 	start = *addr;
1173d239bd3cSKonstantin Belousov 	vm_map_lock(map);
1174d239bd3cSKonstantin Belousov 	end = start + length;
1175d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1176d239bd3cSKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1177d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1178d239bd3cSKonstantin Belousov 	    max, cow);
1179d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1180d239bd3cSKonstantin Belousov 	return (result);
1181d239bd3cSKonstantin Belousov }
1182d239bd3cSKonstantin Belousov 
1183df8bae1dSRodney W. Grimes /*
1184df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1185df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1186df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1187df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1188df8bae1dSRodney W. Grimes  *
11892aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11902aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1191df8bae1dSRodney W. Grimes  */
1192df8bae1dSRodney W. Grimes int
1193b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1194b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1195b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
1196b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1197df8bae1dSRodney W. Grimes {
1198c0877f10SJohn Dyson 	vm_offset_t start;
11996eaee3feSAlan Cox 	int result;
1200df8bae1dSRodney W. Grimes 
1201df8bae1dSRodney W. Grimes 	start = *addr;
1202bea41bcfSDavid Greenman 	vm_map_lock(map);
1203df8bae1dSRodney W. Grimes 	if (find_space) {
1204df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
1205df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1206df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
1207df8bae1dSRodney W. Grimes 		}
1208df8bae1dSRodney W. Grimes 		start = *addr;
1209df8bae1dSRodney W. Grimes 	}
1210bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
1211bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
1212df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1213df8bae1dSRodney W. Grimes 	return (result);
1214df8bae1dSRodney W. Grimes }
1215df8bae1dSRodney W. Grimes 
1216df8bae1dSRodney W. Grimes /*
1217b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
121867bf6868SJohn Dyson  *
12194e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
12204e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
12214e71e795SMatthew Dillon  *
12224e71e795SMatthew Dillon  *	The map must be locked.
12234e71e795SMatthew Dillon  *
12244e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
12254e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
12264e71e795SMatthew Dillon  *	both neighbors.
1227df8bae1dSRodney W. Grimes  */
1228b7b2aac2SJohn Dyson void
12291b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1230df8bae1dSRodney W. Grimes {
1231308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1232b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1233df8bae1dSRodney W. Grimes 
1234acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1235df8bae1dSRodney W. Grimes 		return;
1236308c24baSJohn Dyson 
1237308c24baSJohn Dyson 	prev = entry->prev;
1238308c24baSJohn Dyson 	if (prev != &map->header) {
123967bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
124067bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
124167bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
124295e5e988SJohn Dyson 		     (!prev->object.vm_object ||
124367bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1244afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
124567bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
124667bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
124767bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
1248b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
1249308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1250308c24baSJohn Dyson 			entry->start = prev->start;
1251308c24baSJohn Dyson 			entry->offset = prev->offset;
12520164e057SAlan Cox 			if (entry->prev != &map->header)
12530164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
1254b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1255308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1256308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1257308c24baSJohn Dyson 		}
1258308c24baSJohn Dyson 	}
1259de5f6a77SJohn Dyson 
1260de5f6a77SJohn Dyson 	next = entry->next;
1261308c24baSJohn Dyson 	if (next != &map->header) {
126267bf6868SJohn Dyson 		esize = entry->end - entry->start;
126367bf6868SJohn Dyson 		if ((entry->end == next->start) &&
126467bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
126567bf6868SJohn Dyson 		     (!entry->object.vm_object ||
126667bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1267afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
126867bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
126967bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
127067bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
1271b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
1272de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1273de5f6a77SJohn Dyson 			entry->end = next->end;
12740164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
1275b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1276de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1277de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1278df8bae1dSRodney W. Grimes 		}
1279df8bae1dSRodney W. Grimes 	}
1280de5f6a77SJohn Dyson }
1281df8bae1dSRodney W. Grimes /*
1282df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1283df8bae1dSRodney W. Grimes  *
1284df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1285df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1286df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1287df8bae1dSRodney W. Grimes  */
1288df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1289df8bae1dSRodney W. Grimes { \
1290df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1291df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1292df8bae1dSRodney W. Grimes }
1293df8bae1dSRodney W. Grimes 
1294df8bae1dSRodney W. Grimes /*
1295df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1296df8bae1dSRodney W. Grimes  *	the entry must be split.
1297df8bae1dSRodney W. Grimes  */
12980d94caffSDavid Greenman static void
12991b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1300df8bae1dSRodney W. Grimes {
1301c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1302df8bae1dSRodney W. Grimes 
1303df8bae1dSRodney W. Grimes 	/*
13040d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
13050d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
13060d94caffSDavid Greenman 	 * starting address.
1307df8bae1dSRodney W. Grimes 	 */
1308f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1309f32dbbeeSJohn Dyson 
131011cccda1SJohn Dyson 	/*
131111cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
131211cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
131311cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
131411cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
131511cccda1SJohn Dyson 	 * put this improvement.
131611cccda1SJohn Dyson 	 */
13174e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
131811cccda1SJohn Dyson 		vm_object_t object;
131911cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1320c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
132111cccda1SJohn Dyson 		entry->object.vm_object = object;
132211cccda1SJohn Dyson 		entry->offset = 0;
132311cccda1SJohn Dyson 	}
132411cccda1SJohn Dyson 
1325df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1326df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1327df8bae1dSRodney W. Grimes 
1328df8bae1dSRodney W. Grimes 	new_entry->end = start;
1329df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1330df8bae1dSRodney W. Grimes 	entry->start = start;
1331df8bae1dSRodney W. Grimes 
1332df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1333df8bae1dSRodney W. Grimes 
13349fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1335df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1336df8bae1dSRodney W. Grimes 	}
1337c0877f10SJohn Dyson }
1338df8bae1dSRodney W. Grimes 
1339df8bae1dSRodney W. Grimes /*
1340df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1341df8bae1dSRodney W. Grimes  *
1342df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1343df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1344df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1345df8bae1dSRodney W. Grimes  */
1346df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1347df8bae1dSRodney W. Grimes { \
1348af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1349af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1350df8bae1dSRodney W. Grimes }
1351df8bae1dSRodney W. Grimes 
1352df8bae1dSRodney W. Grimes /*
1353df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1354df8bae1dSRodney W. Grimes  *	the entry must be split.
1355df8bae1dSRodney W. Grimes  */
13560d94caffSDavid Greenman static void
13571b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1358df8bae1dSRodney W. Grimes {
1359c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1360df8bae1dSRodney W. Grimes 
1361df8bae1dSRodney W. Grimes 	/*
136211cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
136311cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
136411cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
136511cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
136611cccda1SJohn Dyson 	 * put this improvement.
136711cccda1SJohn Dyson 	 */
13684e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
136911cccda1SJohn Dyson 		vm_object_t object;
137011cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1371c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
137211cccda1SJohn Dyson 		entry->object.vm_object = object;
137311cccda1SJohn Dyson 		entry->offset = 0;
137411cccda1SJohn Dyson 	}
137511cccda1SJohn Dyson 
137611cccda1SJohn Dyson 	/*
13770d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1378df8bae1dSRodney W. Grimes 	 */
1379df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1380df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1381df8bae1dSRodney W. Grimes 
1382df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1383df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1384df8bae1dSRodney W. Grimes 
1385df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1386df8bae1dSRodney W. Grimes 
13879fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1388df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1389df8bae1dSRodney W. Grimes 	}
1390c0877f10SJohn Dyson }
1391df8bae1dSRodney W. Grimes 
1392df8bae1dSRodney W. Grimes /*
1393df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1394df8bae1dSRodney W. Grimes  *
1395df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1396df8bae1dSRodney W. Grimes  *
1397df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1398df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1399df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1400df8bae1dSRodney W. Grimes  *
1401df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1402df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1403df8bae1dSRodney W. Grimes  *		vm_fault
1404df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1405df8bae1dSRodney W. Grimes  *
1406df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1407df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1408df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1409df8bae1dSRodney W. Grimes  */
1410df8bae1dSRodney W. Grimes int
14111b40f8c0SMatthew Dillon vm_map_submap(
14121b40f8c0SMatthew Dillon 	vm_map_t map,
14131b40f8c0SMatthew Dillon 	vm_offset_t start,
14141b40f8c0SMatthew Dillon 	vm_offset_t end,
14151b40f8c0SMatthew Dillon 	vm_map_t submap)
1416df8bae1dSRodney W. Grimes {
1417df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1418c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1419df8bae1dSRodney W. Grimes 
1420df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1421df8bae1dSRodney W. Grimes 
1422df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1425df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
14260d94caffSDavid Greenman 	} else
1427df8bae1dSRodney W. Grimes 		entry = entry->next;
1428df8bae1dSRodney W. Grimes 
1429df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1430df8bae1dSRodney W. Grimes 
1431df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
14329fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1433afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
14342d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1435afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1436df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1437df8bae1dSRodney W. Grimes 	}
1438df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1439df8bae1dSRodney W. Grimes 
1440df8bae1dSRodney W. Grimes 	return (result);
1441df8bae1dSRodney W. Grimes }
1442df8bae1dSRodney W. Grimes 
1443df8bae1dSRodney W. Grimes /*
14441f78f902SAlan Cox  * The maximum number of pages to map
14451f78f902SAlan Cox  */
14461f78f902SAlan Cox #define	MAX_INIT_PT	96
14471f78f902SAlan Cox 
14481f78f902SAlan Cox /*
14490551c08dSAlan Cox  *	vm_map_pmap_enter:
14500551c08dSAlan Cox  *
1451cf4682aeSAlan Cox  *	Preload read-only mappings for the given object's resident pages into
1452cf4682aeSAlan Cox  *	the given map.  This eliminates the soft faults on process startup and
1453cf4682aeSAlan Cox  *	immediately after an mmap(2).  Unless the given flags include
1454cf4682aeSAlan Cox  *	MAP_PREFAULT_MADVISE, cached pages are not reactivated and mapped.
14550551c08dSAlan Cox  */
14560551c08dSAlan Cox void
14574da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
14580551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
14590551c08dSAlan Cox {
14608fece8c3SAlan Cox 	vm_offset_t start;
1461ce142d9eSAlan Cox 	vm_page_t p, p_start;
14628fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
14631f70d622SAlan Cox 	boolean_t are_queues_locked;
14640551c08dSAlan Cox 
1465ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
14661f78f902SAlan Cox 		return;
14671f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
14681f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
14691f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
14701f78f902SAlan Cox 		goto unlock_return;
14711f78f902SAlan Cox 	}
14721f78f902SAlan Cox 
14731f78f902SAlan Cox 	psize = atop(size);
14741f78f902SAlan Cox 
14751f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
14761f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
14771f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
14781f78f902SAlan Cox 		goto unlock_return;
14791f78f902SAlan Cox 	}
14801f78f902SAlan Cox 
14811f78f902SAlan Cox 	if (psize + pindex > object->size) {
14821f78f902SAlan Cox 		if (object->size < pindex)
14831f78f902SAlan Cox 			goto unlock_return;
14841f78f902SAlan Cox 		psize = object->size - pindex;
14851f78f902SAlan Cox 	}
14861f78f902SAlan Cox 
14871f70d622SAlan Cox 	are_queues_locked = FALSE;
1488ce142d9eSAlan Cox 	start = 0;
1489ce142d9eSAlan Cox 	p_start = NULL;
14901f78f902SAlan Cox 
14911f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
14921f78f902SAlan Cox 		if (p->pindex < pindex) {
14931f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
14941f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
14951f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
14961f78f902SAlan Cox 		}
14971f78f902SAlan Cox 	}
14981f78f902SAlan Cox 	/*
14991f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
15001f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
15011f78f902SAlan Cox 	 * or (2) NULL.
15021f78f902SAlan Cox 	 */
15031f78f902SAlan Cox 	for (;
15041f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
15051f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
15061f78f902SAlan Cox 		/*
15071f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
15081f78f902SAlan Cox 		 * free pages allocating pv entries.
15091f78f902SAlan Cox 		 */
15101f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
15112feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1512379fb642SAlan Cox 			psize = tmpidx;
15131f78f902SAlan Cox 			break;
15141f78f902SAlan Cox 		}
15151f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
15168fece8c3SAlan Cox 		    (p->busy == 0)) {
1517ce142d9eSAlan Cox 			if (p_start == NULL) {
1518ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1519ce142d9eSAlan Cox 				p_start = p;
1520ce142d9eSAlan Cox 			}
15211f70d622SAlan Cox 			if (!are_queues_locked) {
15221f70d622SAlan Cox 				are_queues_locked = TRUE;
152385f5b245SAlan Cox 				vm_page_lock_queues();
152485f5b245SAlan Cox 			}
1525cf4682aeSAlan Cox 			if (VM_PAGE_INQUEUE1(p, PQ_CACHE)) {
1526cf4682aeSAlan Cox 				if ((flags & MAP_PREFAULT_MADVISE) != 0)
15271f70d622SAlan Cox 					vm_page_deactivate(p);
1528cf4682aeSAlan Cox 				else if (p_start != NULL) {
1529cf4682aeSAlan Cox 					pmap_enter_object(map->pmap, start, addr +
1530cf4682aeSAlan Cox 					    ptoa(tmpidx), p_start, prot);
1531cf4682aeSAlan Cox 					p_start = NULL;
1532cf4682aeSAlan Cox 				}
1533cf4682aeSAlan Cox 			}
1534ce142d9eSAlan Cox 		} else if (p_start != NULL) {
1535ce142d9eSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1536ce142d9eSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1537ce142d9eSAlan Cox 			p_start = NULL;
15381f78f902SAlan Cox 		}
15391f78f902SAlan Cox 	}
1540ce142d9eSAlan Cox 	if (p_start != NULL)
1541379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1542379fb642SAlan Cox 		    p_start, prot);
15431f70d622SAlan Cox 	if (are_queues_locked)
15441f70d622SAlan Cox 		vm_page_unlock_queues();
15451f78f902SAlan Cox unlock_return:
15461f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
15470551c08dSAlan Cox }
15480551c08dSAlan Cox 
15490551c08dSAlan Cox /*
1550df8bae1dSRodney W. Grimes  *	vm_map_protect:
1551df8bae1dSRodney W. Grimes  *
1552df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1553df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1554df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1555df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1556df8bae1dSRodney W. Grimes  */
1557df8bae1dSRodney W. Grimes int
1558b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1559b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1560df8bae1dSRodney W. Grimes {
1561c0877f10SJohn Dyson 	vm_map_entry_t current;
1562df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1563df8bae1dSRodney W. Grimes 
1564df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1565df8bae1dSRodney W. Grimes 
1566df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1567df8bae1dSRodney W. Grimes 
1568df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1569df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1570b7b2aac2SJohn Dyson 	} else {
1571df8bae1dSRodney W. Grimes 		entry = entry->next;
1572b7b2aac2SJohn Dyson 	}
1573df8bae1dSRodney W. Grimes 
1574df8bae1dSRodney W. Grimes 	/*
15750d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1576df8bae1dSRodney W. Grimes 	 */
1577df8bae1dSRodney W. Grimes 	current = entry;
1578df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1579afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1580a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1581df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1582a1f6d91cSDavid Greenman 		}
1583df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1584df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1585df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1586df8bae1dSRodney W. Grimes 		}
1587df8bae1dSRodney W. Grimes 		current = current->next;
1588df8bae1dSRodney W. Grimes 	}
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes 	/*
15910d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
15920d94caffSDavid Greenman 	 * necessary the second time.]
1593df8bae1dSRodney W. Grimes 	 */
1594df8bae1dSRodney W. Grimes 	current = entry;
1595df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1596df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1597df8bae1dSRodney W. Grimes 
1598df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1601df8bae1dSRodney W. Grimes 		if (set_max)
1602df8bae1dSRodney W. Grimes 			current->protection =
1603df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1604df8bae1dSRodney W. Grimes 			    old_prot;
1605df8bae1dSRodney W. Grimes 		else
1606df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1607df8bae1dSRodney W. Grimes 
1608df8bae1dSRodney W. Grimes 		/*
16090d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
16100d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1611df8bae1dSRodney W. Grimes 		 */
1612df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1613afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1614df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1615df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1616df8bae1dSRodney W. Grimes 			    current->end,
16171c85e3dfSAlan Cox 			    current->protection & MASK(current));
1618df8bae1dSRodney W. Grimes #undef	MASK
1619df8bae1dSRodney W. Grimes 		}
16207d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1621df8bae1dSRodney W. Grimes 		current = current->next;
1622df8bae1dSRodney W. Grimes 	}
1623df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1624df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1625df8bae1dSRodney W. Grimes }
1626df8bae1dSRodney W. Grimes 
1627df8bae1dSRodney W. Grimes /*
1628867a482dSJohn Dyson  *	vm_map_madvise:
1629867a482dSJohn Dyson  *
1630867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1631f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1632f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1633f7fc307aSAlan Cox  *	objects.
1634867a482dSJohn Dyson  */
1635b4309055SMatthew Dillon int
16361b40f8c0SMatthew Dillon vm_map_madvise(
16371b40f8c0SMatthew Dillon 	vm_map_t map,
16381b40f8c0SMatthew Dillon 	vm_offset_t start,
16391b40f8c0SMatthew Dillon 	vm_offset_t end,
16401b40f8c0SMatthew Dillon 	int behav)
1641867a482dSJohn Dyson {
1642f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1643b4309055SMatthew Dillon 	int modify_map = 0;
1644867a482dSJohn Dyson 
1645b4309055SMatthew Dillon 	/*
1646b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1647b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1648b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1649b4309055SMatthew Dillon 	 * on the map.
1650b4309055SMatthew Dillon 	 */
1651b4309055SMatthew Dillon 	switch(behav) {
1652b4309055SMatthew Dillon 	case MADV_NORMAL:
1653b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1654b4309055SMatthew Dillon 	case MADV_RANDOM:
16554f79d873SMatthew Dillon 	case MADV_NOSYNC:
16564f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
16579730a5daSPaul Saab 	case MADV_NOCORE:
16589730a5daSPaul Saab 	case MADV_CORE:
1659b4309055SMatthew Dillon 		modify_map = 1;
1660867a482dSJohn Dyson 		vm_map_lock(map);
1661b4309055SMatthew Dillon 		break;
1662b4309055SMatthew Dillon 	case MADV_WILLNEED:
1663b4309055SMatthew Dillon 	case MADV_DONTNEED:
1664b4309055SMatthew Dillon 	case MADV_FREE:
1665f7fc307aSAlan Cox 		vm_map_lock_read(map);
1666b4309055SMatthew Dillon 		break;
1667b4309055SMatthew Dillon 	default:
1668b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1669b4309055SMatthew Dillon 	}
1670b4309055SMatthew Dillon 
1671b4309055SMatthew Dillon 	/*
1672b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1673b4309055SMatthew Dillon 	 */
1674867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1675867a482dSJohn Dyson 
1676867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1677f7fc307aSAlan Cox 		if (modify_map)
1678867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1679b4309055SMatthew Dillon 	} else {
1680867a482dSJohn Dyson 		entry = entry->next;
1681b4309055SMatthew Dillon 	}
1682867a482dSJohn Dyson 
1683f7fc307aSAlan Cox 	if (modify_map) {
1684f7fc307aSAlan Cox 		/*
1685f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1686f7fc307aSAlan Cox 		 *
1687f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1688f7fc307aSAlan Cox 		 * limited to the specified address range.
1689f7fc307aSAlan Cox 		 */
1690867a482dSJohn Dyson 		for (current = entry;
1691867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1692b4309055SMatthew Dillon 		     current = current->next
1693b4309055SMatthew Dillon 		) {
1694f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1695867a482dSJohn Dyson 				continue;
1696fed9a903SJohn Dyson 
169747221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1698fed9a903SJohn Dyson 
1699f7fc307aSAlan Cox 			switch (behav) {
1700867a482dSJohn Dyson 			case MADV_NORMAL:
17017f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1702867a482dSJohn Dyson 				break;
1703867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
17047f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1705867a482dSJohn Dyson 				break;
1706867a482dSJohn Dyson 			case MADV_RANDOM:
17077f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1708867a482dSJohn Dyson 				break;
17094f79d873SMatthew Dillon 			case MADV_NOSYNC:
17104f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
17114f79d873SMatthew Dillon 				break;
17124f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
17134f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
17144f79d873SMatthew Dillon 				break;
17159730a5daSPaul Saab 			case MADV_NOCORE:
17169730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
17179730a5daSPaul Saab 				break;
17189730a5daSPaul Saab 			case MADV_CORE:
17199730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
17209730a5daSPaul Saab 				break;
1721867a482dSJohn Dyson 			default:
1722867a482dSJohn Dyson 				break;
1723867a482dSJohn Dyson 			}
1724f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1725867a482dSJohn Dyson 		}
1726867a482dSJohn Dyson 		vm_map_unlock(map);
1727b4309055SMatthew Dillon 	} else {
1728f7fc307aSAlan Cox 		vm_pindex_t pindex;
1729f7fc307aSAlan Cox 		int count;
1730f7fc307aSAlan Cox 
1731f7fc307aSAlan Cox 		/*
1732f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1733f7fc307aSAlan Cox 		 * vm_object.
1734f7fc307aSAlan Cox 		 *
1735f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1736f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1737f7fc307aSAlan Cox 		 */
1738f7fc307aSAlan Cox 		for (current = entry;
1739f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1740b4309055SMatthew Dillon 		     current = current->next
1741b4309055SMatthew Dillon 		) {
17425f99b57cSMatthew Dillon 			vm_offset_t useStart;
17435f99b57cSMatthew Dillon 
1744f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1745f7fc307aSAlan Cox 				continue;
1746f7fc307aSAlan Cox 
1747f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1748f7fc307aSAlan Cox 			count = atop(current->end - current->start);
17495f99b57cSMatthew Dillon 			useStart = current->start;
1750f7fc307aSAlan Cox 
1751f7fc307aSAlan Cox 			if (current->start < start) {
1752f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1753f7fc307aSAlan Cox 				count -= atop(start - current->start);
17545f99b57cSMatthew Dillon 				useStart = start;
1755f7fc307aSAlan Cox 			}
1756f7fc307aSAlan Cox 			if (current->end > end)
1757f7fc307aSAlan Cox 				count -= atop(current->end - end);
1758f7fc307aSAlan Cox 
1759f7fc307aSAlan Cox 			if (count <= 0)
1760f7fc307aSAlan Cox 				continue;
1761f7fc307aSAlan Cox 
1762f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1763f7fc307aSAlan Cox 					  pindex, count, behav);
1764b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
17650551c08dSAlan Cox 				vm_map_pmap_enter(map,
17665f99b57cSMatthew Dillon 				    useStart,
17674da4d293SAlan Cox 				    current->protection,
1768f7fc307aSAlan Cox 				    current->object.vm_object,
1769b4309055SMatthew Dillon 				    pindex,
1770b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1771e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1772b4309055SMatthew Dillon 				);
1773f7fc307aSAlan Cox 			}
1774f7fc307aSAlan Cox 		}
1775f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1776f7fc307aSAlan Cox 	}
1777b4309055SMatthew Dillon 	return (0);
1778867a482dSJohn Dyson }
1779867a482dSJohn Dyson 
1780867a482dSJohn Dyson 
1781867a482dSJohn Dyson /*
1782df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1783df8bae1dSRodney W. Grimes  *
1784df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1785df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1786df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1787df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1788df8bae1dSRodney W. Grimes  */
1789df8bae1dSRodney W. Grimes int
1790b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1791b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1792df8bae1dSRodney W. Grimes {
1793c0877f10SJohn Dyson 	vm_map_entry_t entry;
1794df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1795df8bae1dSRodney W. Grimes 
1796df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1797df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1798df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1799df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1800df8bae1dSRodney W. Grimes 		break;
1801df8bae1dSRodney W. Grimes 	default:
1802df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1803df8bae1dSRodney W. Grimes 	}
1804df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1805df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1806df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1807df8bae1dSRodney W. Grimes 		entry = temp_entry;
1808df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
18090d94caffSDavid Greenman 	} else
1810df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1811df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1812df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1813df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
181444428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
1815df8bae1dSRodney W. Grimes 		entry = entry->next;
1816df8bae1dSRodney W. Grimes 	}
1817df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1818df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1819df8bae1dSRodney W. Grimes }
1820df8bae1dSRodney W. Grimes 
1821df8bae1dSRodney W. Grimes /*
1822acd9a301SAlan Cox  *	vm_map_unwire:
1823acd9a301SAlan Cox  *
1824e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
1825acd9a301SAlan Cox  */
1826acd9a301SAlan Cox int
1827acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1828abd498aaSBruce M Simpson     int flags)
1829acd9a301SAlan Cox {
1830acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
1831acd9a301SAlan Cox 	vm_offset_t saved_start;
1832acd9a301SAlan Cox 	unsigned int last_timestamp;
1833acd9a301SAlan Cox 	int rv;
1834abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
1835acd9a301SAlan Cox 
1836abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1837acd9a301SAlan Cox 	vm_map_lock(map);
1838acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
1839acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1840abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1841cbef13d8SAlan Cox 			first_entry = first_entry->next;
1842abd498aaSBruce M Simpson 		else {
1843acd9a301SAlan Cox 			vm_map_unlock(map);
1844acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
1845acd9a301SAlan Cox 		}
1846abd498aaSBruce M Simpson 	}
1847acd9a301SAlan Cox 	last_timestamp = map->timestamp;
1848acd9a301SAlan Cox 	entry = first_entry;
1849acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1850acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1851acd9a301SAlan Cox 			/*
1852acd9a301SAlan Cox 			 * We have not yet clipped the entry.
1853acd9a301SAlan Cox 			 */
1854acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
1855acd9a301SAlan Cox 			    entry->start;
1856acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1857acd9a301SAlan Cox 			if (vm_map_unlock_and_wait(map, user_unwire)) {
1858acd9a301SAlan Cox 				/*
1859acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
1860acd9a301SAlan Cox 				 */
1861acd9a301SAlan Cox 			}
1862acd9a301SAlan Cox 			vm_map_lock(map);
1863acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
1864acd9a301SAlan Cox 				/*
1865acd9a301SAlan Cox 				 * Look again for the entry because the map was
1866acd9a301SAlan Cox 				 * modified while it was unlocked.
1867acd9a301SAlan Cox 				 * Specifically, the entry may have been
1868acd9a301SAlan Cox 				 * clipped, merged, or deleted.
1869acd9a301SAlan Cox 				 */
1870acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
1871acd9a301SAlan Cox 				    &tmp_entry)) {
1872cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1873cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1874cbef13d8SAlan Cox 					else {
1875acd9a301SAlan Cox 						if (saved_start == start) {
1876acd9a301SAlan Cox 							/*
1877acd9a301SAlan Cox 							 * First_entry has been deleted.
1878acd9a301SAlan Cox 							 */
1879acd9a301SAlan Cox 							vm_map_unlock(map);
1880acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
1881acd9a301SAlan Cox 						}
1882acd9a301SAlan Cox 						end = saved_start;
1883acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
1884acd9a301SAlan Cox 						goto done;
1885acd9a301SAlan Cox 					}
1886cbef13d8SAlan Cox 				}
1887acd9a301SAlan Cox 				if (entry == first_entry)
1888acd9a301SAlan Cox 					first_entry = tmp_entry;
1889acd9a301SAlan Cox 				else
1890acd9a301SAlan Cox 					first_entry = NULL;
1891acd9a301SAlan Cox 				entry = tmp_entry;
1892acd9a301SAlan Cox 			}
1893acd9a301SAlan Cox 			last_timestamp = map->timestamp;
1894acd9a301SAlan Cox 			continue;
1895acd9a301SAlan Cox 		}
1896acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
1897acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
1898acd9a301SAlan Cox 		/*
1899acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
1900acd9a301SAlan Cox 		 * above.)
1901acd9a301SAlan Cox 		 */
1902acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1903acd9a301SAlan Cox 		/*
1904acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
1905abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1906acd9a301SAlan Cox 		 */
1907abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1908abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
1909abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
1910acd9a301SAlan Cox 			end = entry->end;
1911acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
1912acd9a301SAlan Cox 			goto done;
1913acd9a301SAlan Cox 		}
1914acd9a301SAlan Cox 		/*
19153ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
1916acd9a301SAlan Cox 		 */
19170ada205eSBrian Feldman 		if (!user_unwire &&
19180ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
1919acd9a301SAlan Cox 			end = entry->end;
1920acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
1921acd9a301SAlan Cox 			goto done;
1922acd9a301SAlan Cox 		}
1923acd9a301SAlan Cox 		entry = entry->next;
1924acd9a301SAlan Cox 	}
1925acd9a301SAlan Cox 	rv = KERN_SUCCESS;
1926acd9a301SAlan Cox done:
1927e27e17b7SAlan Cox 	need_wakeup = FALSE;
1928acd9a301SAlan Cox 	if (first_entry == NULL) {
1929acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
1930cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1931cbef13d8SAlan Cox 			first_entry = first_entry->next;
1932cbef13d8SAlan Cox 		else
1933acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
1934acd9a301SAlan Cox 	}
1935acd9a301SAlan Cox 	entry = first_entry;
1936acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
19373ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
19383ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1939b2f3846aSAlan Cox 			if (user_unwire)
1940b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1941b2f3846aSAlan Cox 			entry->wired_count--;
19420ada205eSBrian Feldman 			if (entry->wired_count == 0) {
1943b2f3846aSAlan Cox 				/*
1944b2f3846aSAlan Cox 				 * Retain the map lock.
1945b2f3846aSAlan Cox 				 */
19464be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
19474be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
19484be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
1949b2f3846aSAlan Cox 			}
1950b2f3846aSAlan Cox 		}
1951acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1952acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
1953acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1954acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1955acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1956acd9a301SAlan Cox 			need_wakeup = TRUE;
1957acd9a301SAlan Cox 		}
1958acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
1959acd9a301SAlan Cox 		entry = entry->next;
1960acd9a301SAlan Cox 	}
1961acd9a301SAlan Cox 	vm_map_unlock(map);
1962acd9a301SAlan Cox 	if (need_wakeup)
1963acd9a301SAlan Cox 		vm_map_wakeup(map);
1964acd9a301SAlan Cox 	return (rv);
1965acd9a301SAlan Cox }
1966acd9a301SAlan Cox 
1967acd9a301SAlan Cox /*
1968e27e17b7SAlan Cox  *	vm_map_wire:
1969e27e17b7SAlan Cox  *
1970e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
1971e27e17b7SAlan Cox  */
1972e27e17b7SAlan Cox int
1973e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1974abd498aaSBruce M Simpson     int flags)
1975e27e17b7SAlan Cox {
197612d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
197712d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
197812d7cc84SAlan Cox 	unsigned int last_timestamp;
197912d7cc84SAlan Cox 	int rv;
19804be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
1981e27e17b7SAlan Cox 
1982abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
198312d7cc84SAlan Cox 	vm_map_lock(map);
198412d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
198512d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1986abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1987cbef13d8SAlan Cox 			first_entry = first_entry->next;
1988abd498aaSBruce M Simpson 		else {
198912d7cc84SAlan Cox 			vm_map_unlock(map);
199012d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
199112d7cc84SAlan Cox 		}
1992abd498aaSBruce M Simpson 	}
199312d7cc84SAlan Cox 	last_timestamp = map->timestamp;
199412d7cc84SAlan Cox 	entry = first_entry;
199512d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
199612d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
199712d7cc84SAlan Cox 			/*
199812d7cc84SAlan Cox 			 * We have not yet clipped the entry.
199912d7cc84SAlan Cox 			 */
200012d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
200112d7cc84SAlan Cox 			    entry->start;
200212d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
200312d7cc84SAlan Cox 			if (vm_map_unlock_and_wait(map, user_wire)) {
200412d7cc84SAlan Cox 				/*
200512d7cc84SAlan Cox 				 * Allow interruption of user wiring?
200612d7cc84SAlan Cox 				 */
200712d7cc84SAlan Cox 			}
200812d7cc84SAlan Cox 			vm_map_lock(map);
200912d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
201012d7cc84SAlan Cox 				/*
201112d7cc84SAlan Cox 				 * Look again for the entry because the map was
201212d7cc84SAlan Cox 				 * modified while it was unlocked.
201312d7cc84SAlan Cox 				 * Specifically, the entry may have been
201412d7cc84SAlan Cox 				 * clipped, merged, or deleted.
201512d7cc84SAlan Cox 				 */
201612d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
201712d7cc84SAlan Cox 				    &tmp_entry)) {
2018cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2019cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2020cbef13d8SAlan Cox 					else {
202112d7cc84SAlan Cox 						if (saved_start == start) {
202212d7cc84SAlan Cox 							/*
202312d7cc84SAlan Cox 							 * first_entry has been deleted.
202412d7cc84SAlan Cox 							 */
202512d7cc84SAlan Cox 							vm_map_unlock(map);
202612d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
202712d7cc84SAlan Cox 						}
202812d7cc84SAlan Cox 						end = saved_start;
202912d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
203012d7cc84SAlan Cox 						goto done;
203112d7cc84SAlan Cox 					}
2032cbef13d8SAlan Cox 				}
203312d7cc84SAlan Cox 				if (entry == first_entry)
203412d7cc84SAlan Cox 					first_entry = tmp_entry;
203512d7cc84SAlan Cox 				else
203612d7cc84SAlan Cox 					first_entry = NULL;
203712d7cc84SAlan Cox 				entry = tmp_entry;
203812d7cc84SAlan Cox 			}
203912d7cc84SAlan Cox 			last_timestamp = map->timestamp;
204012d7cc84SAlan Cox 			continue;
204112d7cc84SAlan Cox 		}
204212d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
204312d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
204412d7cc84SAlan Cox 		/*
204512d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
204612d7cc84SAlan Cox 		 * above.)
204712d7cc84SAlan Cox 		 */
204812d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
204912d7cc84SAlan Cox 		/*
20500ada205eSBrian Feldman 		 *
205112d7cc84SAlan Cox 		 */
20520ada205eSBrian Feldman 		if (entry->wired_count == 0) {
20530ada205eSBrian Feldman 			entry->wired_count++;
205412d7cc84SAlan Cox 			saved_start = entry->start;
205512d7cc84SAlan Cox 			saved_end = entry->end;
20564be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
20574be14af9SAlan Cox 			    entry->object.vm_object->type == OBJT_DEVICE;
205812d7cc84SAlan Cox 			/*
205912d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
206012d7cc84SAlan Cox 			 * mark.
206112d7cc84SAlan Cox 			 */
206212d7cc84SAlan Cox 			vm_map_unlock(map);
2063ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
20644be14af9SAlan Cox 			    user_wire, fictitious);
206512d7cc84SAlan Cox 			vm_map_lock(map);
206612d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
206712d7cc84SAlan Cox 				/*
206812d7cc84SAlan Cox 				 * Look again for the entry because the map was
206912d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
207012d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
207112d7cc84SAlan Cox 				 * deleted.
207212d7cc84SAlan Cox 				 */
207312d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
207412d7cc84SAlan Cox 				    &tmp_entry);
207512d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
207612d7cc84SAlan Cox 				if (entry == first_entry)
207712d7cc84SAlan Cox 					first_entry = tmp_entry;
207812d7cc84SAlan Cox 				else
207912d7cc84SAlan Cox 					first_entry = NULL;
208012d7cc84SAlan Cox 				entry = tmp_entry;
208128c58286SAlan Cox 				while (entry->end < saved_end) {
208228c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
208328c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
208428c58286SAlan Cox 						    ("vm_map_wire: bad count"));
208528c58286SAlan Cox 						entry->wired_count = -1;
208628c58286SAlan Cox 					}
208712d7cc84SAlan Cox 					entry = entry->next;
208812d7cc84SAlan Cox 				}
208928c58286SAlan Cox 			}
209012d7cc84SAlan Cox 			last_timestamp = map->timestamp;
209112d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
209228c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
209328c58286SAlan Cox 				    ("vm_map_wire: bad count"));
209412d7cc84SAlan Cox 				/*
209528c58286SAlan Cox 				 * Assign an out-of-range value to represent
209628c58286SAlan Cox 				 * the failure to wire this entry.
209712d7cc84SAlan Cox 				 */
209828c58286SAlan Cox 				entry->wired_count = -1;
209912d7cc84SAlan Cox 				end = entry->end;
210012d7cc84SAlan Cox 				goto done;
210112d7cc84SAlan Cox 			}
21020ada205eSBrian Feldman 		} else if (!user_wire ||
21030ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
21040ada205eSBrian Feldman 			entry->wired_count++;
210512d7cc84SAlan Cox 		}
210612d7cc84SAlan Cox 		/*
210712d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2108abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
210912d7cc84SAlan Cox 		 */
2110abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2111abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2112abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
211312d7cc84SAlan Cox 			end = entry->end;
211412d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
211512d7cc84SAlan Cox 			goto done;
211612d7cc84SAlan Cox 		}
211712d7cc84SAlan Cox 		entry = entry->next;
211812d7cc84SAlan Cox 	}
211912d7cc84SAlan Cox 	rv = KERN_SUCCESS;
212012d7cc84SAlan Cox done:
212112d7cc84SAlan Cox 	need_wakeup = FALSE;
212212d7cc84SAlan Cox 	if (first_entry == NULL) {
212312d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2124cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2125cbef13d8SAlan Cox 			first_entry = first_entry->next;
2126cbef13d8SAlan Cox 		else
212712d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
212812d7cc84SAlan Cox 	}
212912d7cc84SAlan Cox 	entry = first_entry;
213012d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
213112d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
213212d7cc84SAlan Cox 			if (user_wire)
213312d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
213428c58286SAlan Cox 		} else if (entry->wired_count == -1) {
213528c58286SAlan Cox 			/*
213628c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
213728c58286SAlan Cox 			 * unnecessary.
213828c58286SAlan Cox 			 */
213928c58286SAlan Cox 			entry->wired_count = 0;
214012d7cc84SAlan Cox 		} else {
21410ada205eSBrian Feldman 			if (!user_wire ||
21420ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
214312d7cc84SAlan Cox 				entry->wired_count--;
21440ada205eSBrian Feldman 			if (entry->wired_count == 0) {
214512d7cc84SAlan Cox 				/*
214612d7cc84SAlan Cox 				 * Retain the map lock.
214712d7cc84SAlan Cox 				 */
21484be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
21494be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
21504be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
215112d7cc84SAlan Cox 			}
215212d7cc84SAlan Cox 		}
215312d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
215412d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
215512d7cc84SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
215612d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
215712d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
215812d7cc84SAlan Cox 			need_wakeup = TRUE;
215912d7cc84SAlan Cox 		}
216012d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
216112d7cc84SAlan Cox 		entry = entry->next;
216212d7cc84SAlan Cox 	}
216312d7cc84SAlan Cox 	vm_map_unlock(map);
216412d7cc84SAlan Cox 	if (need_wakeup)
216512d7cc84SAlan Cox 		vm_map_wakeup(map);
216612d7cc84SAlan Cox 	return (rv);
2167e27e17b7SAlan Cox }
2168e27e17b7SAlan Cox 
2169e27e17b7SAlan Cox /*
2170950f8459SAlan Cox  * vm_map_sync
2171df8bae1dSRodney W. Grimes  *
2172df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2173df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2174df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2175df8bae1dSRodney W. Grimes  *
2176637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2177637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2178637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2179637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2180637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2181637315edSAlan Cox  * flushing the current region containing start.
2182637315edSAlan Cox  *
2183df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2184df8bae1dSRodney W. Grimes  */
2185df8bae1dSRodney W. Grimes int
2186950f8459SAlan Cox vm_map_sync(
21871b40f8c0SMatthew Dillon 	vm_map_t map,
21881b40f8c0SMatthew Dillon 	vm_offset_t start,
21891b40f8c0SMatthew Dillon 	vm_offset_t end,
21901b40f8c0SMatthew Dillon 	boolean_t syncio,
21911b40f8c0SMatthew Dillon 	boolean_t invalidate)
2192df8bae1dSRodney W. Grimes {
2193c0877f10SJohn Dyson 	vm_map_entry_t current;
2194df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2195df8bae1dSRodney W. Grimes 	vm_size_t size;
2196df8bae1dSRodney W. Grimes 	vm_object_t object;
2197a316d390SJohn Dyson 	vm_ooffset_t offset;
2198df8bae1dSRodney W. Grimes 
2199df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2200df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2201df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2202df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2203df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2204637315edSAlan Cox 	} else if (start == end) {
2205637315edSAlan Cox 		start = entry->start;
2206637315edSAlan Cox 		end = entry->end;
2207df8bae1dSRodney W. Grimes 	}
2208df8bae1dSRodney W. Grimes 	/*
2209b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2210df8bae1dSRodney W. Grimes 	 */
2211df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2212b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2213df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2214df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2215df8bae1dSRodney W. Grimes 		}
2216df8bae1dSRodney W. Grimes 		if (end > current->end &&
2217df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2218df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2219df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2220df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2221df8bae1dSRodney W. Grimes 		}
2222df8bae1dSRodney W. Grimes 	}
2223df8bae1dSRodney W. Grimes 
22242cf13952SAlan Cox 	if (invalidate)
2225bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
22262cf13952SAlan Cox 
2227df8bae1dSRodney W. Grimes 	/*
2228df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2229df8bae1dSRodney W. Grimes 	 * objects as we go.
2230df8bae1dSRodney W. Grimes 	 */
2231df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2232df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2233df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
22349fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2235c0877f10SJohn Dyson 			vm_map_t smap;
2236df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2237df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2238df8bae1dSRodney W. Grimes 
22399fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2240df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2241df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2242df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2243df8bae1dSRodney W. Grimes 			if (tsize < size)
2244df8bae1dSRodney W. Grimes 				size = tsize;
2245df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2246df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2247df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2248df8bae1dSRodney W. Grimes 		} else {
2249df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2250df8bae1dSRodney W. Grimes 		}
2251950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2252df8bae1dSRodney W. Grimes 		start += size;
2253df8bae1dSRodney W. Grimes 	}
2254df8bae1dSRodney W. Grimes 
2255df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2256df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2257df8bae1dSRodney W. Grimes }
2258df8bae1dSRodney W. Grimes 
2259df8bae1dSRodney W. Grimes /*
2260df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2261df8bae1dSRodney W. Grimes  *
2262df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2263df8bae1dSRodney W. Grimes  *
2264df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2265df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2266df8bae1dSRodney W. Grimes  */
22670362d7d7SJohn Dyson static void
22681b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2269df8bae1dSRodney W. Grimes {
22704be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
22714be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
22724be14af9SAlan Cox 	    entry->object.vm_object->type == OBJT_DEVICE);
2273df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2274df8bae1dSRodney W. Grimes }
2275df8bae1dSRodney W. Grimes 
2276df8bae1dSRodney W. Grimes /*
2277df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2278df8bae1dSRodney W. Grimes  *
2279df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2280df8bae1dSRodney W. Grimes  */
22810362d7d7SJohn Dyson static void
22821b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2283df8bae1dSRodney W. Grimes {
228432a89c32SAlan Cox 	vm_object_t object;
228532a89c32SAlan Cox 	vm_pindex_t offidxstart, offidxend, count;
228632a89c32SAlan Cox 
2287df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
2288df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
2289df8bae1dSRodney W. Grimes 
229032a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
229132a89c32SAlan Cox 	    (object = entry->object.vm_object) != NULL) {
229232a89c32SAlan Cox 		count = OFF_TO_IDX(entry->end - entry->start);
229332a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
229432a89c32SAlan Cox 		offidxend = offidxstart + count;
229532a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
229632a89c32SAlan Cox 		if (object->ref_count != 1 &&
229732a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
22989f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
229932a89c32SAlan Cox 			vm_object_collapse(object);
2300b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
230132a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
230232a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
230332a89c32SAlan Cox 			if (offidxend >= object->size &&
230432a89c32SAlan Cox 			    offidxstart < object->size)
230532a89c32SAlan Cox 				object->size = offidxstart;
230632a89c32SAlan Cox 		}
230732a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
230832a89c32SAlan Cox 		vm_object_deallocate(object);
2309b5b40fa6SJohn Dyson 	}
2310df8bae1dSRodney W. Grimes 
2311df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
2312df8bae1dSRodney W. Grimes }
2313df8bae1dSRodney W. Grimes 
2314df8bae1dSRodney W. Grimes /*
2315df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2316df8bae1dSRodney W. Grimes  *
2317df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2318df8bae1dSRodney W. Grimes  *	map.
2319df8bae1dSRodney W. Grimes  */
2320df8bae1dSRodney W. Grimes int
23211b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2322df8bae1dSRodney W. Grimes {
2323c0877f10SJohn Dyson 	vm_map_entry_t entry;
2324df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2325df8bae1dSRodney W. Grimes 
2326df8bae1dSRodney W. Grimes 	/*
2327df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2328df8bae1dSRodney W. Grimes 	 */
2329876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2330df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2331876318ecSAlan Cox 	else {
2332df8bae1dSRodney W. Grimes 		entry = first_entry;
2333df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2334df8bae1dSRodney W. Grimes 	}
2335df8bae1dSRodney W. Grimes 
2336df8bae1dSRodney W. Grimes 	/*
2337df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2338df8bae1dSRodney W. Grimes 	 */
2339df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2340df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2341df8bae1dSRodney W. Grimes 
234273b2baceSAlan Cox 		/*
234373b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
23447c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
23457c938963SBrian Feldman 		 * user maps.
234673b2baceSAlan Cox 		 */
23477c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
23487c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
23497c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
235073b2baceSAlan Cox 			unsigned int last_timestamp;
235173b2baceSAlan Cox 			vm_offset_t saved_start;
235273b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
235373b2baceSAlan Cox 
235473b2baceSAlan Cox 			saved_start = entry->start;
235573b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
235673b2baceSAlan Cox 			last_timestamp = map->timestamp;
235773b2baceSAlan Cox 			(void) vm_map_unlock_and_wait(map, FALSE);
235873b2baceSAlan Cox 			vm_map_lock(map);
235973b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
236073b2baceSAlan Cox 				/*
236173b2baceSAlan Cox 				 * Look again for the entry because the map was
236273b2baceSAlan Cox 				 * modified while it was unlocked.
236373b2baceSAlan Cox 				 * Specifically, the entry may have been
236473b2baceSAlan Cox 				 * clipped, merged, or deleted.
236573b2baceSAlan Cox 				 */
236673b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
236773b2baceSAlan Cox 							 &tmp_entry))
236873b2baceSAlan Cox 					entry = tmp_entry->next;
236973b2baceSAlan Cox 				else {
237073b2baceSAlan Cox 					entry = tmp_entry;
237173b2baceSAlan Cox 					vm_map_clip_start(map, entry,
237273b2baceSAlan Cox 							  saved_start);
237373b2baceSAlan Cox 				}
237473b2baceSAlan Cox 			}
237573b2baceSAlan Cox 			continue;
237673b2baceSAlan Cox 		}
2377df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2378df8bae1dSRodney W. Grimes 
2379c0877f10SJohn Dyson 		next = entry->next;
2380df8bae1dSRodney W. Grimes 
2381df8bae1dSRodney W. Grimes 		/*
23820d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
23830d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2384df8bae1dSRodney W. Grimes 		 */
2385c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2386df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2387c0877f10SJohn Dyson 		}
2388df8bae1dSRodney W. Grimes 
238932a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2390df8bae1dSRodney W. Grimes 
2391df8bae1dSRodney W. Grimes 		/*
23920d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
23930d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
23940d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
23950d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2396df8bae1dSRodney W. Grimes 		 */
2397df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2398df8bae1dSRodney W. Grimes 		entry = next;
2399df8bae1dSRodney W. Grimes 	}
2400df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2401df8bae1dSRodney W. Grimes }
2402df8bae1dSRodney W. Grimes 
2403df8bae1dSRodney W. Grimes /*
2404df8bae1dSRodney W. Grimes  *	vm_map_remove:
2405df8bae1dSRodney W. Grimes  *
2406df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2407df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2408df8bae1dSRodney W. Grimes  */
2409df8bae1dSRodney W. Grimes int
24101b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2411df8bae1dSRodney W. Grimes {
24126eaee3feSAlan Cox 	int result;
2413df8bae1dSRodney W. Grimes 
2414df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2415df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2416df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2417df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2418df8bae1dSRodney W. Grimes 	return (result);
2419df8bae1dSRodney W. Grimes }
2420df8bae1dSRodney W. Grimes 
2421df8bae1dSRodney W. Grimes /*
2422df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2423df8bae1dSRodney W. Grimes  *
24242d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
24252d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
24262d5c7e45SMatthew Dillon  *
24272d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
24282d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
24292d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
24302d5c7e45SMatthew Dillon  *
24312d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2432d8834602SAlan Cox  *
2433d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2434df8bae1dSRodney W. Grimes  */
24350d94caffSDavid Greenman boolean_t
2436b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2437b9dcd593SBruce Evans 			vm_prot_t protection)
2438df8bae1dSRodney W. Grimes {
2439c0877f10SJohn Dyson 	vm_map_entry_t entry;
2440df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2441df8bae1dSRodney W. Grimes 
2442d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2443df8bae1dSRodney W. Grimes 		return (FALSE);
2444df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2445df8bae1dSRodney W. Grimes 
2446df8bae1dSRodney W. Grimes 	while (start < end) {
2447d8834602SAlan Cox 		if (entry == &map->header)
2448df8bae1dSRodney W. Grimes 			return (FALSE);
2449df8bae1dSRodney W. Grimes 		/*
2450df8bae1dSRodney W. Grimes 		 * No holes allowed!
2451df8bae1dSRodney W. Grimes 		 */
2452d8834602SAlan Cox 		if (start < entry->start)
2453df8bae1dSRodney W. Grimes 			return (FALSE);
2454df8bae1dSRodney W. Grimes 		/*
2455df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2456df8bae1dSRodney W. Grimes 		 */
2457d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2458df8bae1dSRodney W. Grimes 			return (FALSE);
2459df8bae1dSRodney W. Grimes 		/* go to next entry */
2460df8bae1dSRodney W. Grimes 		start = entry->end;
2461df8bae1dSRodney W. Grimes 		entry = entry->next;
2462df8bae1dSRodney W. Grimes 	}
2463df8bae1dSRodney W. Grimes 	return (TRUE);
2464df8bae1dSRodney W. Grimes }
2465df8bae1dSRodney W. Grimes 
246686524867SJohn Dyson /*
2467df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2468df8bae1dSRodney W. Grimes  *
2469df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2470df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2471df8bae1dSRodney W. Grimes  */
2472f708ef1bSPoul-Henning Kamp static void
24731b40f8c0SMatthew Dillon vm_map_copy_entry(
24741b40f8c0SMatthew Dillon 	vm_map_t src_map,
24751b40f8c0SMatthew Dillon 	vm_map_t dst_map,
24761b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
24771b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2478df8bae1dSRodney W. Grimes {
2479c0877f10SJohn Dyson 	vm_object_t src_object;
2480c0877f10SJohn Dyson 
24819fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2482df8bae1dSRodney W. Grimes 		return;
2483df8bae1dSRodney W. Grimes 
2484df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2485df8bae1dSRodney W. Grimes 
2486df8bae1dSRodney W. Grimes 		/*
24870d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
24880d94caffSDavid Greenman 		 * write-protected.
2489df8bae1dSRodney W. Grimes 		 */
2490afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2491df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2492df8bae1dSRodney W. Grimes 			    src_entry->start,
2493df8bae1dSRodney W. Grimes 			    src_entry->end,
2494df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2495df8bae1dSRodney W. Grimes 		}
2496b18bfc3dSJohn Dyson 
2497df8bae1dSRodney W. Grimes 		/*
2498df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2499df8bae1dSRodney W. Grimes 		 */
25008aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2501a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
2502c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2503c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2504c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2505c0877f10SJohn Dyson 				vm_object_collapse(src_object);
250696fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2507c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2508c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2509a89c6258SAlan Cox 				}
2510a89c6258SAlan Cox 			}
2511b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2512069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2513e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2514c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2515afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2516afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2517b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2518b18bfc3dSJohn Dyson 		} else {
2519b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2520b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2521b18bfc3dSJohn Dyson 		}
2522df8bae1dSRodney W. Grimes 
2523df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2524df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
25250d94caffSDavid Greenman 	} else {
2526df8bae1dSRodney W. Grimes 		/*
2527df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
25280d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
25290d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2530df8bae1dSRodney W. Grimes 		 */
2531df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2532df8bae1dSRodney W. Grimes 	}
2533df8bae1dSRodney W. Grimes }
2534df8bae1dSRodney W. Grimes 
2535df8bae1dSRodney W. Grimes /*
25362a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
25372a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
25382a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
25392a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
25402a7be1b6SBrian Feldman  */
25412a7be1b6SBrian Feldman static void
25422a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
25432a7be1b6SBrian Feldman     vm_map_entry_t entry)
25442a7be1b6SBrian Feldman {
25452a7be1b6SBrian Feldman 	vm_size_t entrysize;
25462a7be1b6SBrian Feldman 	vm_offset_t newend;
25472a7be1b6SBrian Feldman 
25482a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
25492a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
25502a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
25512a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
25522a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
25532a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2554b351299cSAndrew Gallatin 		newend = MIN(entry->end,
25552a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
25562a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
25572a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
25582a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2559b351299cSAndrew Gallatin 		newend = MIN(entry->end,
25602a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
25612a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
25622a7be1b6SBrian Feldman 	}
25632a7be1b6SBrian Feldman }
25642a7be1b6SBrian Feldman 
25652a7be1b6SBrian Feldman /*
2566df8bae1dSRodney W. Grimes  * vmspace_fork:
2567df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2568df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2569df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2570df8bae1dSRodney W. Grimes  * values on the regions in that map.
2571df8bae1dSRodney W. Grimes  *
25722a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
25732a7be1b6SBrian Feldman  *
2574df8bae1dSRodney W. Grimes  * The source map must not be locked.
2575df8bae1dSRodney W. Grimes  */
2576df8bae1dSRodney W. Grimes struct vmspace *
25771b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2578df8bae1dSRodney W. Grimes {
2579c0877f10SJohn Dyson 	struct vmspace *vm2;
2580df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2581df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2582df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2583df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2584de5f6a77SJohn Dyson 	vm_object_t object;
2585df8bae1dSRodney W. Grimes 
2586df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2587df8bae1dSRodney W. Grimes 
25882d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
25892a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
25902a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
25912a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2592df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
259347221757SJohn Dyson 	new_map->timestamp = 1;
2594df8bae1dSRodney W. Grimes 
2595df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2596df8bae1dSRodney W. Grimes 
2597df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2598afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2599df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2600df8bae1dSRodney W. Grimes 
2601df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2602df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2603df8bae1dSRodney W. Grimes 			break;
2604df8bae1dSRodney W. Grimes 
2605df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2606df8bae1dSRodney W. Grimes 			/*
2607fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2608fed9a903SJohn Dyson 			 */
2609fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2610fed9a903SJohn Dyson 			if (object == NULL) {
2611fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2612c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2613fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
261415d2d313SAlan Cox 				old_entry->offset = 0;
26159a2f6362SAlan Cox 			}
26169a2f6362SAlan Cox 
26179a2f6362SAlan Cox 			/*
26189a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
26199a2f6362SAlan Cox 			 * to insure that a shadow object is created.
26209a2f6362SAlan Cox 			 */
26219a2f6362SAlan Cox 			vm_object_reference(object);
26229a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
26235069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
26245069bf57SJohn Dyson 					&old_entry->offset,
2625c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
26265069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2627d30344bdSIan Dowse 				/* Transfer the second reference too. */
2628d30344bdSIan Dowse 				vm_object_reference(
2629d30344bdSIan Dowse 				    old_entry->object.vm_object);
2630d30344bdSIan Dowse 				vm_object_deallocate(object);
26315069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2632fed9a903SJohn Dyson 			}
2633e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2634069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2635e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2636fed9a903SJohn Dyson 
2637fed9a903SJohn Dyson 			/*
2638ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2639df8bae1dSRodney W. Grimes 			 */
2640df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2641df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2642028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2643df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2644df8bae1dSRodney W. Grimes 
2645df8bae1dSRodney W. Grimes 			/*
26460d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
26470d94caffSDavid Greenman 			 * inserting at the end of the new map.
2648df8bae1dSRodney W. Grimes 			 */
2649df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2650df8bae1dSRodney W. Grimes 			    new_entry);
26512a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2652df8bae1dSRodney W. Grimes 
2653df8bae1dSRodney W. Grimes 			/*
2654df8bae1dSRodney W. Grimes 			 * Update the physical map
2655df8bae1dSRodney W. Grimes 			 */
2656df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2657df8bae1dSRodney W. Grimes 			    new_entry->start,
2658df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2659df8bae1dSRodney W. Grimes 			    old_entry->start);
2660df8bae1dSRodney W. Grimes 			break;
2661df8bae1dSRodney W. Grimes 
2662df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2663df8bae1dSRodney W. Grimes 			/*
2664df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2665df8bae1dSRodney W. Grimes 			 */
2666df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2667df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2668028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2669df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2670df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2671df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2672df8bae1dSRodney W. Grimes 			    new_entry);
26732a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2674bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2675bd7e5f99SJohn Dyson 			    new_entry);
2676df8bae1dSRodney W. Grimes 			break;
2677df8bae1dSRodney W. Grimes 		}
2678df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2679df8bae1dSRodney W. Grimes 	}
2680df8bae1dSRodney W. Grimes 
2681df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2682df8bae1dSRodney W. Grimes 
2683df8bae1dSRodney W. Grimes 	return (vm2);
2684df8bae1dSRodney W. Grimes }
2685df8bae1dSRodney W. Grimes 
268694f7e29aSAlan Cox int
268794f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
268894f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
268994f7e29aSAlan Cox {
2690fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2691fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
269294f7e29aSAlan Cox 	vm_size_t init_ssize;
2693fd75d710SMarcel Moolenaar 	int orient, rv;
269491d5354aSJohn Baldwin 	rlim_t vmemlim;
269594f7e29aSAlan Cox 
2696fd75d710SMarcel Moolenaar 	/*
2697fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2698fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2699fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2700fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2701fd75d710SMarcel Moolenaar 	 */
2702fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2703fd75d710SMarcel Moolenaar 	cow &= ~orient;
2704fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2705fd75d710SMarcel Moolenaar 
2706fd75d710SMarcel Moolenaar 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
270794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2708fd75d710SMarcel Moolenaar 
2709fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
271094f7e29aSAlan Cox 
271191d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
271291d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
271391d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
271491d5354aSJohn Baldwin 
271594f7e29aSAlan Cox 	vm_map_lock(map);
271694f7e29aSAlan Cox 
271794f7e29aSAlan Cox 	/* If addr is already mapped, no go */
271894f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
271994f7e29aSAlan Cox 		vm_map_unlock(map);
272094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
272194f7e29aSAlan Cox 	}
272294f7e29aSAlan Cox 
2723a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
272491d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2725a69ac174SMatthew Dillon 		vm_map_unlock(map);
2726a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2727a69ac174SMatthew Dillon 	}
2728a69ac174SMatthew Dillon 
2729fd75d710SMarcel Moolenaar 	/*
2730fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2731fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2732fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2733fd75d710SMarcel Moolenaar 	 * space is not protected.
273494f7e29aSAlan Cox 	 *
2735fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2736fd75d710SMarcel Moolenaar 	 * grow the stack.
273794f7e29aSAlan Cox 	 */
273894f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
273994f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
274094f7e29aSAlan Cox 		vm_map_unlock(map);
274194f7e29aSAlan Cox 		return (KERN_NO_SPACE);
274294f7e29aSAlan Cox 	}
274394f7e29aSAlan Cox 
2744fd75d710SMarcel Moolenaar 	/*
2745fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2746fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2747fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2748fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
274994f7e29aSAlan Cox 	 *
2750fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2751fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2752fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
275394f7e29aSAlan Cox 	 */
2754fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2755fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2756fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2757fd75d710SMarcel Moolenaar 		bot = addrbos;
2758fd75d710SMarcel Moolenaar 	else
2759fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2760fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2761fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
276294f7e29aSAlan Cox 
2763fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
276494f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
276529b45e9eSAlan Cox 		if (prev_entry != &map->header)
2766fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2767fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2768fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
276994f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2770b21a0008SMarcel Moolenaar 
2771fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2772fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2773fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2774fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2775fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
277694f7e29aSAlan Cox 	}
277794f7e29aSAlan Cox 
277894f7e29aSAlan Cox 	vm_map_unlock(map);
277994f7e29aSAlan Cox 	return (rv);
278094f7e29aSAlan Cox }
278194f7e29aSAlan Cox 
278294f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
278394f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
278494f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
278594f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
278694f7e29aSAlan Cox  * the grow function in vm_machdep.c).
278794f7e29aSAlan Cox  */
278894f7e29aSAlan Cox int
278994f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
279094f7e29aSAlan Cox {
2791b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2792b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
279394f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
279494f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
279594f7e29aSAlan Cox 	vm_offset_t end;
2796b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
279791d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2798b21a0008SMarcel Moolenaar 	int is_procstack, rv;
279923955314SAlfred Perlstein 
280094f7e29aSAlan Cox Retry:
280191d5354aSJohn Baldwin 	PROC_LOCK(p);
280291d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2803bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
280491d5354aSJohn Baldwin 	PROC_UNLOCK(p);
280591d5354aSJohn Baldwin 
280694f7e29aSAlan Cox 	vm_map_lock_read(map);
280794f7e29aSAlan Cox 
280894f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
280994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
281094f7e29aSAlan Cox 		vm_map_unlock_read(map);
28110cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
281294f7e29aSAlan Cox 	}
281394f7e29aSAlan Cox 
2814b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2815b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2816b21a0008SMarcel Moolenaar 		/*
2817b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2818b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2819b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2820b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
282194f7e29aSAlan Cox 		 */
2822b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
282394f7e29aSAlan Cox 			vm_map_unlock_read(map);
28240cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
282594f7e29aSAlan Cox 		}
2826b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2827b21a0008SMarcel Moolenaar 	} else {
2828b21a0008SMarcel Moolenaar 		/*
2829b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2830b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2831b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2832b21a0008SMarcel Moolenaar 		 * select one.
2833b21a0008SMarcel Moolenaar 		 */
2834b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2835b21a0008SMarcel Moolenaar 			/*
2836b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2837b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2838b21a0008SMarcel Moolenaar 			 */
2839b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
2840b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
2841b21a0008SMarcel Moolenaar 			else
2842b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
2843b21a0008SMarcel Moolenaar 		} else
2844b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
2845b21a0008SMarcel Moolenaar 	}
284694f7e29aSAlan Cox 
2847b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2848b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2849b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
2850b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
2851b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
285294f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2853b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
2854b21a0008SMarcel Moolenaar 	} else {
2855b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
285608667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
2857b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
2858b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
2859fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2860b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
2861b21a0008SMarcel Moolenaar 	}
2862b21a0008SMarcel Moolenaar 
286394f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
286494f7e29aSAlan Cox 		vm_map_unlock_read(map);
28650cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
286694f7e29aSAlan Cox 	}
286794f7e29aSAlan Cox 
2868b21a0008SMarcel Moolenaar 	/*
2869b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
2870b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
2871b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
2872b21a0008SMarcel Moolenaar 	 * and is probably an error.
287394f7e29aSAlan Cox 	 *
2874b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
2875b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
287694f7e29aSAlan Cox 	 */
2877b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
287825adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
287994f7e29aSAlan Cox 			goto Retry;
288094f7e29aSAlan Cox 
2881b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
288294f7e29aSAlan Cox 
288394f7e29aSAlan Cox 		vm_map_unlock(map);
28840cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
288594f7e29aSAlan Cox 	}
288694f7e29aSAlan Cox 
2887b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
288894f7e29aSAlan Cox 
2889b21a0008SMarcel Moolenaar 	/*
2890b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
2891b21a0008SMarcel Moolenaar 	 * limit.
289294f7e29aSAlan Cox 	 */
289391d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
289494f7e29aSAlan Cox 		vm_map_unlock_read(map);
28950cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
289694f7e29aSAlan Cox 	}
289794f7e29aSAlan Cox 
289894f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2899cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
2900b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
290194f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
290291d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
290391d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
290494f7e29aSAlan Cox 	}
290594f7e29aSAlan Cox 
2906a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
290791d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
2908a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
2909a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2910a69ac174SMatthew Dillon 	}
2911a69ac174SMatthew Dillon 
291225adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
291394f7e29aSAlan Cox 		goto Retry;
291494f7e29aSAlan Cox 
2915b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2916b21a0008SMarcel Moolenaar 		/*
2917b21a0008SMarcel Moolenaar 		 * Growing downward.
2918b21a0008SMarcel Moolenaar 		 */
291994f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
292094f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
292194f7e29aSAlan Cox 
2922b21a0008SMarcel Moolenaar 		/*
2923b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
2924b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
292594f7e29aSAlan Cox 		 */
292694f7e29aSAlan Cox 		if (addr < end) {
2927b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
292894f7e29aSAlan Cox 			addr = end;
292994f7e29aSAlan Cox 		}
293094f7e29aSAlan Cox 
293194f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
293205ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
293394f7e29aSAlan Cox 
293494f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
293594f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
293629b45e9eSAlan Cox 			if (prev_entry != &map->header)
293729b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
2938b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
2939b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2940b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2941b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
2942b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
2943b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
2944b21a0008SMarcel Moolenaar 			    grow_amount;
2945b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2946b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
294794f7e29aSAlan Cox 		}
2948b21a0008SMarcel Moolenaar 	} else {
2949b21a0008SMarcel Moolenaar 		/*
2950b21a0008SMarcel Moolenaar 		 * Growing upward.
2951b21a0008SMarcel Moolenaar 		 */
2952b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
2953b21a0008SMarcel Moolenaar 
2954b21a0008SMarcel Moolenaar 		/*
2955b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
2956b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
2957b21a0008SMarcel Moolenaar 		 */
2958b21a0008SMarcel Moolenaar 		if (addr > end) {
2959b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
2960b21a0008SMarcel Moolenaar 			addr = end;
296194f7e29aSAlan Cox 		}
296294f7e29aSAlan Cox 
2963b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
2964b21a0008SMarcel Moolenaar 
2965b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
2966b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
2967b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
296857a21abaSAlan Cox 		    stack_entry->offset,
2969b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2970b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
297108667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
2972b21a0008SMarcel Moolenaar 			/* Update the current entry. */
2973b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
2974199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
29750164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
2976b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
2977b21a0008SMarcel Moolenaar 
2978b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
2979b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
2980b21a0008SMarcel Moolenaar 		} else
2981b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
2982b21a0008SMarcel Moolenaar 	}
2983b21a0008SMarcel Moolenaar 
2984b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
2985b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
2986b21a0008SMarcel Moolenaar 
298794f7e29aSAlan Cox 	vm_map_unlock(map);
2988b21a0008SMarcel Moolenaar 
2989abd498aaSBruce M Simpson 	/*
2990abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2991abd498aaSBruce M Simpson 	 */
2992b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2993b21a0008SMarcel Moolenaar 		vm_map_wire(map,
2994b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2995b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2996b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
2997b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2998b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2999b21a0008SMarcel Moolenaar 	}
3000abd498aaSBruce M Simpson 
30010cddd8f0SMatthew Dillon 	return (rv);
300294f7e29aSAlan Cox }
300394f7e29aSAlan Cox 
3004df8bae1dSRodney W. Grimes /*
30055856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
30065856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
30075856e12eSJohn Dyson  */
30085856e12eSJohn Dyson void
30093ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
30101b40f8c0SMatthew Dillon {
30115856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
30125856e12eSJohn Dyson 	struct vmspace *newvmspace;
30135856e12eSJohn Dyson 
30143ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
301551ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
30165856e12eSJohn Dyson 	/*
30175856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
30185856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
30195856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
30205856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
30215856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
30225856e12eSJohn Dyson 	 */
302357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
30245856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
302557051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
3026b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
3027b40ce416SJulian Elischer 		pmap_activate(curthread);
3028b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
30295856e12eSJohn Dyson }
30305856e12eSJohn Dyson 
30315856e12eSJohn Dyson /*
30325856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
30335856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
30345856e12eSJohn Dyson  */
30355856e12eSJohn Dyson void
30361b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
30371b40f8c0SMatthew Dillon {
30385856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
30395856e12eSJohn Dyson 	struct vmspace *newvmspace;
30405856e12eSJohn Dyson 
30415856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
30425856e12eSJohn Dyson 		return;
30435856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
304457051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
30455856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
304657051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
3047b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
3048b40ce416SJulian Elischer 		pmap_activate(curthread);
3049b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
30505856e12eSJohn Dyson }
30515856e12eSJohn Dyson 
30525856e12eSJohn Dyson /*
3053df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3054df8bae1dSRodney W. Grimes  *
3055df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3056df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3057df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3058df8bae1dSRodney W. Grimes  *	type specified.
3059df8bae1dSRodney W. Grimes  *
3060df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3061df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3062df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3063df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3064df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3065df8bae1dSRodney W. Grimes  *
3066df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3067df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3068df8bae1dSRodney W. Grimes  *
3069df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3070df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3071df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3072df8bae1dSRodney W. Grimes  *	remain the same.
3073df8bae1dSRodney W. Grimes  */
3074df8bae1dSRodney W. Grimes int
3075b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3076b9dcd593SBruce Evans 	      vm_offset_t vaddr,
307747221757SJohn Dyson 	      vm_prot_t fault_typea,
3078b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3079b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3080b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3081b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
30822d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3083df8bae1dSRodney W. Grimes {
3084c0877f10SJohn Dyson 	vm_map_entry_t entry;
3085c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3086c0877f10SJohn Dyson 	vm_prot_t prot;
308747221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
3088df8bae1dSRodney W. Grimes 
3089df8bae1dSRodney W. Grimes RetryLookup:;
3090df8bae1dSRodney W. Grimes 	/*
3091df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
3092df8bae1dSRodney W. Grimes 	 */
3093df8bae1dSRodney W. Grimes 
3094df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3095df8bae1dSRodney W. Grimes #define	RETURN(why) \
3096df8bae1dSRodney W. Grimes 		{ \
3097df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
3098df8bae1dSRodney W. Grimes 		return (why); \
3099df8bae1dSRodney W. Grimes 		}
3100df8bae1dSRodney W. Grimes 
3101df8bae1dSRodney W. Grimes 	/*
31020d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
31030d94caffSDavid Greenman 	 * blown lookup routine.
3104df8bae1dSRodney W. Grimes 	 */
31054e94f402SAlan Cox 	entry = map->root;
3106df8bae1dSRodney W. Grimes 	*out_entry = entry;
31074e94f402SAlan Cox 	if (entry == NULL ||
3108df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3109df8bae1dSRodney W. Grimes 		/*
31100d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
31110d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
3112df8bae1dSRodney W. Grimes 		 */
31134e94f402SAlan Cox 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3114df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
3115df8bae1dSRodney W. Grimes 
31164e94f402SAlan Cox 		entry = *out_entry;
3117df8bae1dSRodney W. Grimes 	}
3118b7b2aac2SJohn Dyson 
3119df8bae1dSRodney W. Grimes 	/*
3120df8bae1dSRodney W. Grimes 	 * Handle submaps.
3121df8bae1dSRodney W. Grimes 	 */
3122afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3123df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3124df8bae1dSRodney W. Grimes 
3125df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3126df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3127df8bae1dSRodney W. Grimes 		goto RetryLookup;
3128df8bae1dSRodney W. Grimes 	}
3129a04c970aSJohn Dyson 
3130df8bae1dSRodney W. Grimes 	/*
31310d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3132a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
3133a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
3134a04c970aSJohn Dyson 	 * COW for debuggers.
3135df8bae1dSRodney W. Grimes 	 */
3136480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3137480ba2f5SJohn Dyson 		prot = entry->max_protection;
3138480ba2f5SJohn Dyson 	else
3139df8bae1dSRodney W. Grimes 		prot = entry->protection;
314047221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
314147221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
314247221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
314347221757SJohn Dyson 	}
31442ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
314547221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
31462ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
314747221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3148df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
3149a04c970aSJohn Dyson 	}
3150df8bae1dSRodney W. Grimes 
3151df8bae1dSRodney W. Grimes 	/*
31520d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
31530d94caffSDavid Greenman 	 * accesses.
3154df8bae1dSRodney W. Grimes 	 */
315505f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
315605f0fdd2SPoul-Henning Kamp 	if (*wired)
3157df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
3158df8bae1dSRodney W. Grimes 
3159df8bae1dSRodney W. Grimes 	/*
3160df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3161df8bae1dSRodney W. Grimes 	 */
3162afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3163df8bae1dSRodney W. Grimes 		/*
31640d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3165ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3166df8bae1dSRodney W. Grimes 		 *
31670d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
31680d94caffSDavid Greenman 		 * permissions allowed.
3169df8bae1dSRodney W. Grimes 		 */
3170df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
3171df8bae1dSRodney W. Grimes 			/*
31720d94caffSDavid Greenman 			 * Make a new object, and place it in the object
31730d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3174ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
31750d94caffSDavid Greenman 			 * object.
3176df8bae1dSRodney W. Grimes 			 */
317725adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3178df8bae1dSRodney W. Grimes 				goto RetryLookup;
31799917e010SAlan Cox 
3180df8bae1dSRodney W. Grimes 			vm_object_shadow(
3181df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3182df8bae1dSRodney W. Grimes 			    &entry->offset,
3183c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
3184afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
31859917e010SAlan Cox 
31869b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
31870d94caffSDavid Greenman 		} else {
3188df8bae1dSRodney W. Grimes 			/*
31890d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
31900d94caffSDavid Greenman 			 * don't allow writes.
3191df8bae1dSRodney W. Grimes 			 */
31922d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3193df8bae1dSRodney W. Grimes 		}
3194df8bae1dSRodney W. Grimes 	}
31952d8acc0fSJohn Dyson 
3196df8bae1dSRodney W. Grimes 	/*
3197df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3198df8bae1dSRodney W. Grimes 	 */
31994e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
32004e71e795SMatthew Dillon 	    !map->system_map) {
320125adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3202df8bae1dSRodney W. Grimes 			goto RetryLookup;
320324a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3204c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
3205df8bae1dSRodney W. Grimes 		entry->offset = 0;
32069b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3207df8bae1dSRodney W. Grimes 	}
3208b5b40fa6SJohn Dyson 
3209df8bae1dSRodney W. Grimes 	/*
32100d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
32110d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3212df8bae1dSRodney W. Grimes 	 */
32139b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3214df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3215df8bae1dSRodney W. Grimes 
3216df8bae1dSRodney W. Grimes 	*out_prot = prot;
3217df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3218df8bae1dSRodney W. Grimes 
3219df8bae1dSRodney W. Grimes #undef	RETURN
3220df8bae1dSRodney W. Grimes }
3221df8bae1dSRodney W. Grimes 
3222df8bae1dSRodney W. Grimes /*
322319dc5607STor Egge  *	vm_map_lookup_locked:
322419dc5607STor Egge  *
322519dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
322619dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
322719dc5607STor Egge  */
322819dc5607STor Egge int
322919dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
323019dc5607STor Egge 		     vm_offset_t vaddr,
323119dc5607STor Egge 		     vm_prot_t fault_typea,
323219dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
323319dc5607STor Egge 		     vm_object_t *object,	/* OUT */
323419dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
323519dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
323619dc5607STor Egge 		     boolean_t *wired)		/* OUT */
323719dc5607STor Egge {
323819dc5607STor Egge 	vm_map_entry_t entry;
323919dc5607STor Egge 	vm_map_t map = *var_map;
324019dc5607STor Egge 	vm_prot_t prot;
324119dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
324219dc5607STor Egge 
324319dc5607STor Egge 	/*
324419dc5607STor Egge 	 * If the map has an interesting hint, try it before calling full
324519dc5607STor Egge 	 * blown lookup routine.
324619dc5607STor Egge 	 */
324719dc5607STor Egge 	entry = map->root;
324819dc5607STor Egge 	*out_entry = entry;
324919dc5607STor Egge 	if (entry == NULL ||
325019dc5607STor Egge 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
325119dc5607STor Egge 		/*
325219dc5607STor Egge 		 * Entry was either not a valid hint, or the vaddr was not
325319dc5607STor Egge 		 * contained in the entry, so do a full lookup.
325419dc5607STor Egge 		 */
325519dc5607STor Egge 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
325619dc5607STor Egge 			return (KERN_INVALID_ADDRESS);
325719dc5607STor Egge 
325819dc5607STor Egge 		entry = *out_entry;
325919dc5607STor Egge 	}
326019dc5607STor Egge 
326119dc5607STor Egge 	/*
326219dc5607STor Egge 	 * Fail if the entry refers to a submap.
326319dc5607STor Egge 	 */
326419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
326519dc5607STor Egge 		return (KERN_FAILURE);
326619dc5607STor Egge 
326719dc5607STor Egge 	/*
326819dc5607STor Egge 	 * Check whether this task is allowed to have this page.
326919dc5607STor Egge 	 * Note the special case for MAP_ENTRY_COW
327019dc5607STor Egge 	 * pages with an override.  This is to implement a forced
327119dc5607STor Egge 	 * COW for debuggers.
327219dc5607STor Egge 	 */
327319dc5607STor Egge 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
327419dc5607STor Egge 		prot = entry->max_protection;
327519dc5607STor Egge 	else
327619dc5607STor Egge 		prot = entry->protection;
327719dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
327819dc5607STor Egge 	if ((fault_type & prot) != fault_type)
327919dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
328019dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
328119dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
328219dc5607STor Egge 	    (fault_type & VM_PROT_WRITE) &&
328319dc5607STor Egge 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
328419dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
328519dc5607STor Egge 
328619dc5607STor Egge 	/*
328719dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
328819dc5607STor Egge 	 * accesses.
328919dc5607STor Egge 	 */
329019dc5607STor Egge 	*wired = (entry->wired_count != 0);
329119dc5607STor Egge 	if (*wired)
329219dc5607STor Egge 		prot = fault_type = entry->protection;
329319dc5607STor Egge 
329419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
329519dc5607STor Egge 		/*
329619dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
329719dc5607STor Egge 		 */
329819dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
329919dc5607STor Egge 			return (KERN_FAILURE);
330019dc5607STor Egge 		/*
330119dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
330219dc5607STor Egge 		 * don't allow writes.
330319dc5607STor Egge 		 */
330419dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
330519dc5607STor Egge 	}
330619dc5607STor Egge 
330719dc5607STor Egge 	/*
330819dc5607STor Egge 	 * Fail if an object should be created.
330919dc5607STor Egge 	 */
331019dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
331119dc5607STor Egge 		return (KERN_FAILURE);
331219dc5607STor Egge 
331319dc5607STor Egge 	/*
331419dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
331519dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
331619dc5607STor Egge 	 */
331719dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
331819dc5607STor Egge 	*object = entry->object.vm_object;
331919dc5607STor Egge 
332019dc5607STor Egge 	*out_prot = prot;
332119dc5607STor Egge 	return (KERN_SUCCESS);
332219dc5607STor Egge }
332319dc5607STor Egge 
332419dc5607STor Egge /*
3325df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3326df8bae1dSRodney W. Grimes  *
3327df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3328df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3329df8bae1dSRodney W. Grimes  */
33300d94caffSDavid Greenman void
33311b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3332df8bae1dSRodney W. Grimes {
3333df8bae1dSRodney W. Grimes 	/*
3334df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3335df8bae1dSRodney W. Grimes 	 */
3336df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3337df8bae1dSRodney W. Grimes }
3338df8bae1dSRodney W. Grimes 
3339c7c34a24SBruce Evans #include "opt_ddb.h"
3340c3cb3e12SDavid Greenman #ifdef DDB
3341c7c34a24SBruce Evans #include <sys/kernel.h>
3342c7c34a24SBruce Evans 
3343c7c34a24SBruce Evans #include <ddb/ddb.h>
3344c7c34a24SBruce Evans 
3345df8bae1dSRodney W. Grimes /*
3346df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3347df8bae1dSRodney W. Grimes  */
3348c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3349df8bae1dSRodney W. Grimes {
335095e5e988SJohn Dyson 	static int nlines;
3351c7c34a24SBruce Evans 	/* XXX convert args. */
3352c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3353c7c34a24SBruce Evans 	boolean_t full = have_addr;
3354df8bae1dSRodney W. Grimes 
3355c0877f10SJohn Dyson 	vm_map_entry_t entry;
3356c7c34a24SBruce Evans 
3357e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3358e5f251d2SAlan Cox 	    (void *)map,
3359101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
336095e5e988SJohn Dyson 	nlines++;
3361df8bae1dSRodney W. Grimes 
3362c7c34a24SBruce Evans 	if (!full && db_indent)
3363df8bae1dSRodney W. Grimes 		return;
3364df8bae1dSRodney W. Grimes 
3365c7c34a24SBruce Evans 	db_indent += 2;
3366df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3367df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3368fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3369fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
337095e5e988SJohn Dyson 		nlines++;
3371e5f251d2SAlan Cox 		{
3372df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3373df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
33740d94caffSDavid Greenman 
337595e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3376df8bae1dSRodney W. Grimes 			    entry->protection,
3377df8bae1dSRodney W. Grimes 			    entry->max_protection,
33788aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3379df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
338095e5e988SJohn Dyson 				db_printf(", wired");
3381df8bae1dSRodney W. Grimes 		}
33829fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3383cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
33849fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3385cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
338695e5e988SJohn Dyson 			nlines++;
3387df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
33889fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
33899fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3390c7c34a24SBruce Evans 				db_indent += 2;
3391101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
33929fdfe602SMatthew Dillon 					     entry->object.sub_map,
3393914181e7SBruce Evans 					     full, 0, (char *)0);
3394c7c34a24SBruce Evans 				db_indent -= 2;
3395df8bae1dSRodney W. Grimes 			}
33960d94caffSDavid Greenman 		} else {
3397cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3398101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3399cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3400afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3401c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3402afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3403c7c34a24SBruce Evans 			db_printf("\n");
340495e5e988SJohn Dyson 			nlines++;
3405df8bae1dSRodney W. Grimes 
3406df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3407df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3408df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3409c7c34a24SBruce Evans 				db_indent += 2;
3410101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3411101eeb7fSBruce Evans 						entry->object.vm_object,
3412914181e7SBruce Evans 						full, 0, (char *)0);
341395e5e988SJohn Dyson 				nlines += 4;
3414c7c34a24SBruce Evans 				db_indent -= 2;
3415df8bae1dSRodney W. Grimes 			}
3416df8bae1dSRodney W. Grimes 		}
3417df8bae1dSRodney W. Grimes 	}
3418c7c34a24SBruce Evans 	db_indent -= 2;
341995e5e988SJohn Dyson 	if (db_indent == 0)
342095e5e988SJohn Dyson 		nlines = 0;
3421df8bae1dSRodney W. Grimes }
342295e5e988SJohn Dyson 
342395e5e988SJohn Dyson 
342495e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
342595e5e988SJohn Dyson {
342695e5e988SJohn Dyson 	struct proc *p;
342795e5e988SJohn Dyson 
342895e5e988SJohn Dyson 	if (have_addr) {
342995e5e988SJohn Dyson 		p = (struct proc *) addr;
343095e5e988SJohn Dyson 	} else {
343195e5e988SJohn Dyson 		p = curproc;
343295e5e988SJohn Dyson 	}
343395e5e988SJohn Dyson 
3434ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3435ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3436b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
343795e5e988SJohn Dyson 
3438101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
343995e5e988SJohn Dyson }
344095e5e988SJohn Dyson 
3441c7c34a24SBruce Evans #endif /* DDB */
3442