xref: /freebsd/sys/vm/vm_map.c (revision e608cc3c8dc7f678a4ba6bef006c6b9b56ef2559)
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
103e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
104e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
105df8bae1dSRodney W. Grimes  *
106956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
107df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
108df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
109df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
110df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
111df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
112df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
113df8bae1dSRodney W. Grimes  *
114df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
115ad5fca3bSAlan Cox  *	by copying VM object references from one map to
116df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
117df8bae1dSRodney W. Grimes  */
118df8bae1dSRodney W. Grimes 
119df8bae1dSRodney W. Grimes /*
120df8bae1dSRodney W. Grimes  *	vm_map_startup:
121df8bae1dSRodney W. Grimes  *
122df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
123df8bae1dSRodney W. Grimes  *	any other vm_map routines.
124df8bae1dSRodney W. Grimes  *
125df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
126df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
129df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
130df8bae1dSRodney W. Grimes  *
131df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
132df8bae1dSRodney W. Grimes  *	maps and requires map entries.
133df8bae1dSRodney W. Grimes  */
134df8bae1dSRodney W. Grimes 
1353a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1368355f576SJeff Roberson static uma_zone_t mapentzone;
1378355f576SJeff Roberson static uma_zone_t kmapentzone;
1388355f576SJeff Roberson static uma_zone_t mapzone;
1398355f576SJeff Roberson static uma_zone_t vmspace_zone;
1408355f576SJeff Roberson static struct vm_object kmapentobj;
141b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1428355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
143b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1448355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
1458355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
1461fc43fd1SAlan Cox 
1478355f576SJeff Roberson #ifdef INVARIANTS
1488355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1498355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1508355f576SJeff Roberson #endif
151b18bfc3dSJohn Dyson 
15257051fdcSTor Egge /*
15357051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15457051fdcSTor Egge  * stable.
15557051fdcSTor Egge  */
15657051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15757051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15857051fdcSTor Egge 
159d239bd3cSKonstantin Belousov /*
160d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
161d239bd3cSKonstantin Belousov  *
162d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
163d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
164d239bd3cSKonstantin Belousov  */
165d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
166d239bd3cSKonstantin Belousov 		{					\
167d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
168d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
169d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
170d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
171d239bd3cSKonstantin Belousov 		if (start > end)			\
172d239bd3cSKonstantin Belousov 			start = end;			\
173d239bd3cSKonstantin Belousov 		}
174d239bd3cSKonstantin Belousov 
1750d94caffSDavid Greenman void
1761b40f8c0SMatthew Dillon vm_map_startup(void)
177df8bae1dSRodney W. Grimes {
1783a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1798355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1808355f576SJeff Roberson #ifdef INVARIANTS
1818355f576SJeff Roberson 	    vm_map_zdtor,
1828355f576SJeff Roberson #else
1838355f576SJeff Roberson 	    NULL,
1848355f576SJeff Roberson #endif
1858355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1868355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
187670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
18818aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
18918aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
190670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
191670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
192670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
193df8bae1dSRodney W. Grimes }
194df8bae1dSRodney W. Grimes 
1958355f576SJeff Roberson static void
1968355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1978355f576SJeff Roberson {
1988355f576SJeff Roberson 	struct vmspace *vm;
1998355f576SJeff Roberson 
2008355f576SJeff Roberson 	vm = (struct vmspace *)mem;
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 
21189b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
212b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
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);
27589b57fcfSKonstantin Belousov 	if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
27689b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
27789b57fcfSKonstantin Belousov 		return (NULL);
27889b57fcfSKonstantin Belousov 	}
27921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
2808355f576SJeff Roberson 	_vm_map_init(&vm->vm_map, min, max);
281b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
282df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2832d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
28451ab6c28SAlan Cox 	vm->vm_swrss = 0;
28551ab6c28SAlan Cox 	vm->vm_tsize = 0;
28651ab6c28SAlan Cox 	vm->vm_dsize = 0;
28751ab6c28SAlan Cox 	vm->vm_ssize = 0;
28851ab6c28SAlan Cox 	vm->vm_taddr = 0;
28951ab6c28SAlan Cox 	vm->vm_daddr = 0;
29051ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
291df8bae1dSRodney W. Grimes 	return (vm);
292df8bae1dSRodney W. Grimes }
293df8bae1dSRodney W. Grimes 
294df8bae1dSRodney W. Grimes void
2951b40f8c0SMatthew Dillon vm_init2(void)
2961b40f8c0SMatthew Dillon {
2972feb50bfSAttilio Rao 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
298c1f02198SAlan Cox 	    (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
2993fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
3008355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
3018355f576SJeff Roberson #ifdef INVARIANTS
3028355f576SJeff Roberson 	    vmspace_zdtor,
3038355f576SJeff Roberson #else
3048355f576SJeff Roberson 	    NULL,
3058355f576SJeff Roberson #endif
3068355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3073075778bSJohn Dyson }
3083075778bSJohn Dyson 
30962a59e8fSWarner Losh static inline void
310582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
311df8bae1dSRodney W. Grimes {
31221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3133db161e0SMatthew Dillon 
3143db161e0SMatthew Dillon 	/*
3153db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3163db161e0SMatthew Dillon 	 * exit1().
3173db161e0SMatthew Dillon 	 */
3183db161e0SMatthew Dillon 	shmexit(vm);
3193db161e0SMatthew Dillon 
32030dcfc09SJohn Dyson 	/*
321df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3220d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3230d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
324df8bae1dSRodney W. Grimes 	 */
325717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
326df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3278355f576SJeff Roberson 
32889b57fcfSKonstantin Belousov 	/*
32989b57fcfSKonstantin Belousov 	 * XXX Comment out the pmap_release call for now. The
33089b57fcfSKonstantin Belousov 	 * vmspace_zone is marked as UMA_ZONE_NOFREE, and bugs cause
33189b57fcfSKonstantin Belousov 	 * pmap.resident_count to be != 0 on exit sometimes.
33289b57fcfSKonstantin Belousov 	 */
33389b57fcfSKonstantin Belousov /* 	pmap_release(vmspace_pmap(vm)); */
3348355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
335df8bae1dSRodney W. Grimes }
336582ec34cSAlfred Perlstein 
337582ec34cSAlfred Perlstein void
338582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
339582ec34cSAlfred Perlstein {
3401a276a3fSAlan Cox 	int refcnt;
341582ec34cSAlfred Perlstein 
342582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
343582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
344582ec34cSAlfred Perlstein 
3451a276a3fSAlan Cox 	do
3461a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3471a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
34857051fdcSTor Egge 	if (refcnt == 1)
349582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
350582ec34cSAlfred Perlstein }
351582ec34cSAlfred Perlstein 
352582ec34cSAlfred Perlstein void
353582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
354582ec34cSAlfred Perlstein {
355334f7061SPeter Wemm 	struct vmspace *vm;
356582ec34cSAlfred Perlstein 
35757051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
358334f7061SPeter Wemm 	vm = p->p_vmspace;
359334f7061SPeter Wemm 	p->p_vmspace = NULL;
36057051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
36157051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
36257051fdcSTor Egge 	vmspace_free(vm);
36357051fdcSTor Egge }
36457051fdcSTor Egge 
36557051fdcSTor Egge void
36657051fdcSTor Egge vmspace_exit(struct thread *td)
36757051fdcSTor Egge {
36857051fdcSTor Egge 	int refcnt;
36957051fdcSTor Egge 	struct vmspace *vm;
37057051fdcSTor Egge 	struct proc *p;
371389d2b6eSMatthew Dillon 
372389d2b6eSMatthew Dillon 	/*
37357051fdcSTor Egge 	 * Release user portion of address space.
37457051fdcSTor Egge 	 * This releases references to vnodes,
37557051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
37657051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
377389d2b6eSMatthew Dillon 	 *
37857051fdcSTor Egge 	 * The last exiting process to reach this point releases as
37957051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
38057051fdcSTor Egge 	 * slower fallback in case another process had a temporary
38157051fdcSTor Egge 	 * reference to the vmspace.
382389d2b6eSMatthew Dillon 	 */
38357051fdcSTor Egge 
38457051fdcSTor Egge 	p = td->td_proc;
38557051fdcSTor Egge 	vm = p->p_vmspace;
38657051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
38757051fdcSTor Egge 	do {
38857051fdcSTor Egge 		refcnt = vm->vm_refcnt;
38957051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
39057051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
39157051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39257051fdcSTor Egge 			p->p_vmspace = &vmspace0;
39357051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39457051fdcSTor Egge 			pmap_activate(td);
39557051fdcSTor Egge 		}
39657051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
39757051fdcSTor Egge 	if (refcnt == 1) {
39857051fdcSTor Egge 		if (p->p_vmspace != vm) {
39957051fdcSTor Egge 			/* vmspace not yet freed, switch back */
40057051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40157051fdcSTor Egge 			p->p_vmspace = vm;
40257051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40357051fdcSTor Egge 			pmap_activate(td);
40457051fdcSTor Egge 		}
40557051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
40657051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
40757051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
40857051fdcSTor Egge 		p->p_vmspace = &vmspace0;
40957051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41057051fdcSTor Egge 		pmap_activate(td);
411334f7061SPeter Wemm 		vmspace_dofree(vm);
412334f7061SPeter Wemm 	}
41357051fdcSTor Egge }
41457051fdcSTor Egge 
41557051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
41657051fdcSTor Egge 
41757051fdcSTor Egge struct vmspace *
41857051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
41957051fdcSTor Egge {
42057051fdcSTor Egge 	struct vmspace *vm;
42157051fdcSTor Egge 	int refcnt;
42257051fdcSTor Egge 
42357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
42457051fdcSTor Egge 	vm = p->p_vmspace;
42557051fdcSTor Egge 	if (vm == NULL) {
42657051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42757051fdcSTor Egge 		return (NULL);
42857051fdcSTor Egge 	}
42957051fdcSTor Egge 	do {
43057051fdcSTor Egge 		refcnt = vm->vm_refcnt;
43157051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
43257051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
43357051fdcSTor Egge 			return (NULL);
43457051fdcSTor Egge 		}
43557051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
43657051fdcSTor Egge 	if (vm != p->p_vmspace) {
43757051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
43857051fdcSTor Egge 		vmspace_free(vm);
43957051fdcSTor Egge 		return (NULL);
44057051fdcSTor Egge 	}
44157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
44257051fdcSTor Egge 	return (vm);
44357051fdcSTor Egge }
444df8bae1dSRodney W. Grimes 
4451b40f8c0SMatthew Dillon void
446780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4471b40f8c0SMatthew Dillon {
448bc91c510SAlan Cox 
44993bc4879SAlan Cox 	if (map->system_map)
45036daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
45112c64974SMaxime Henrion 	else
452f9819486SAttilio Rao 		(void)_sx_xlock(&map->lock, 0, file, line);
4531b40f8c0SMatthew Dillon 	map->timestamp++;
4541b40f8c0SMatthew Dillon }
4551b40f8c0SMatthew Dillon 
4561b40f8c0SMatthew Dillon void
457780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
4580e0af8ecSBrian Feldman {
459bc91c510SAlan Cox 
46036daaecdSAlan Cox 	if (map->system_map)
46136daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
46236daaecdSAlan Cox 	else
46312c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
4640e0af8ecSBrian Feldman }
4650e0af8ecSBrian Feldman 
4660e0af8ecSBrian Feldman void
467780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
4680e0af8ecSBrian Feldman {
469bc91c510SAlan Cox 
47093bc4879SAlan Cox 	if (map->system_map)
47136daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
47212c64974SMaxime Henrion 	else
47305a8c414SAlan Cox 		(void)_sx_slock(&map->lock, 0, file, line);
47436daaecdSAlan Cox }
4750e0af8ecSBrian Feldman 
4760e0af8ecSBrian Feldman void
477780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
4780e0af8ecSBrian Feldman {
479bc91c510SAlan Cox 
48036daaecdSAlan Cox 	if (map->system_map)
48136daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
48236daaecdSAlan Cox 	else
48305a8c414SAlan Cox 		_sx_sunlock(&map->lock, file, line);
48425adb370SBrian Feldman }
48525adb370SBrian Feldman 
486d974f03cSAlan Cox int
487780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
488d974f03cSAlan Cox {
48925adb370SBrian Feldman 	int error;
49025adb370SBrian Feldman 
49136daaecdSAlan Cox 	error = map->system_map ?
49236daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
49312c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
4943a92e5d5SAlan Cox 	if (error == 0)
4953a92e5d5SAlan Cox 		map->timestamp++;
496bc91c510SAlan Cox 	return (error == 0);
4970e0af8ecSBrian Feldman }
4980e0af8ecSBrian Feldman 
4990e0af8ecSBrian Feldman int
50072d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
50172d97679SDavid Schultz {
50272d97679SDavid Schultz 	int error;
50372d97679SDavid Schultz 
50472d97679SDavid Schultz 	error = map->system_map ?
50572d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
50605a8c414SAlan Cox 	    !_sx_try_slock(&map->lock, file, line);
50772d97679SDavid Schultz 	return (error == 0);
50872d97679SDavid Schultz }
50972d97679SDavid Schultz 
51005a8c414SAlan Cox /*
51105a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
51205a8c414SAlan Cox  *
51305a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
51405a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
51505a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
51605a8c414SAlan Cox  *	returned without a read or write lock held.
51705a8c414SAlan Cox  *
51805a8c414SAlan Cox  *	Requires that the map be read locked.
51905a8c414SAlan Cox  */
52072d97679SDavid Schultz int
521780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5220e0af8ecSBrian Feldman {
52305a8c414SAlan Cox 	unsigned int last_timestamp;
524bc91c510SAlan Cox 
52512c64974SMaxime Henrion 	if (map->system_map) {
52605a8c414SAlan Cox #ifdef INVARIANTS
52736daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
52812c64974SMaxime Henrion #endif
52905a8c414SAlan Cox 	} else {
53005a8c414SAlan Cox 		if (!_sx_try_upgrade(&map->lock, file, line)) {
53105a8c414SAlan Cox 			last_timestamp = map->timestamp;
53205a8c414SAlan Cox 			_sx_sunlock(&map->lock, file, line);
53305a8c414SAlan Cox 			/*
53405a8c414SAlan Cox 			 * If the map's timestamp does not change while the
53505a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
53605a8c414SAlan Cox 			 */
53705a8c414SAlan Cox 			(void)_sx_xlock(&map->lock, 0, file, line);
53805a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
53905a8c414SAlan Cox 				_sx_xunlock(&map->lock, file, line);
54005a8c414SAlan Cox 				return (1);
54105a8c414SAlan Cox 			}
54205a8c414SAlan Cox 		}
54305a8c414SAlan Cox 	}
544bc91c510SAlan Cox 	map->timestamp++;
545bc91c510SAlan Cox 	return (0);
5460e0af8ecSBrian Feldman }
5470e0af8ecSBrian Feldman 
5480e0af8ecSBrian Feldman void
549780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5501b40f8c0SMatthew Dillon {
551bc91c510SAlan Cox 
55212c64974SMaxime Henrion 	if (map->system_map) {
55305a8c414SAlan Cox #ifdef INVARIANTS
55436daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
55512c64974SMaxime Henrion #endif
55605a8c414SAlan Cox 	} else
55705a8c414SAlan Cox 		_sx_downgrade(&map->lock, file, line);
55805a8c414SAlan Cox }
55905a8c414SAlan Cox 
56005a8c414SAlan Cox /*
56105a8c414SAlan Cox  *	vm_map_locked:
56205a8c414SAlan Cox  *
56305a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
56405a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
56505a8c414SAlan Cox  */
56605a8c414SAlan Cox int
56705a8c414SAlan Cox vm_map_locked(vm_map_t map)
56805a8c414SAlan Cox {
56905a8c414SAlan Cox 
57005a8c414SAlan Cox 	if (map->system_map)
57105a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
57205a8c414SAlan Cox 	else
57305a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
57425adb370SBrian Feldman }
57525adb370SBrian Feldman 
576acd9a301SAlan Cox /*
577acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
578acd9a301SAlan Cox  */
5799688f931SAlan Cox int
5808ce2d00aSPawel Jakub Dawidek vm_map_unlock_and_wait(vm_map_t map, int timo)
581acd9a301SAlan Cox {
582acd9a301SAlan Cox 
5833a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
584acd9a301SAlan Cox 	vm_map_unlock(map);
5858ce2d00aSPawel Jakub Dawidek 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", timo));
586acd9a301SAlan Cox }
587acd9a301SAlan Cox 
588acd9a301SAlan Cox /*
589acd9a301SAlan Cox  *	vm_map_wakeup:
590acd9a301SAlan Cox  */
5919688f931SAlan Cox void
592acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
593acd9a301SAlan Cox {
594acd9a301SAlan Cox 
595b49ecb86SAlan Cox 	/*
5963a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
5973a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
5983a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
599b49ecb86SAlan Cox 	 */
6003a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6013a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
602acd9a301SAlan Cox 	wakeup(&map->root);
603acd9a301SAlan Cox }
604acd9a301SAlan Cox 
6051b40f8c0SMatthew Dillon long
6061b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
6071b40f8c0SMatthew Dillon {
6081b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
6091b40f8c0SMatthew Dillon }
6101b40f8c0SMatthew Dillon 
6112bc7dd56SBruce M Simpson long
6122bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
6132bc7dd56SBruce M Simpson {
6142bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
6152bc7dd56SBruce M Simpson }
6162bc7dd56SBruce M Simpson 
617ff2b5645SMatthew Dillon /*
618df8bae1dSRodney W. Grimes  *	vm_map_create:
619df8bae1dSRodney W. Grimes  *
620df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
621df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
622df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
623df8bae1dSRodney W. Grimes  */
6240d94caffSDavid Greenman vm_map_t
6251b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
626df8bae1dSRodney W. Grimes {
627c0877f10SJohn Dyson 	vm_map_t result;
628df8bae1dSRodney W. Grimes 
629a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
63021c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
6318355f576SJeff Roberson 	_vm_map_init(result, min, max);
632df8bae1dSRodney W. Grimes 	result->pmap = pmap;
633df8bae1dSRodney W. Grimes 	return (result);
634df8bae1dSRodney W. Grimes }
635df8bae1dSRodney W. Grimes 
636df8bae1dSRodney W. Grimes /*
637df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
638df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
639df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
640df8bae1dSRodney W. Grimes  */
6418355f576SJeff Roberson static void
6428355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
643df8bae1dSRodney W. Grimes {
64421c641b2SJohn Baldwin 
645df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
6469688f931SAlan Cox 	map->needs_wakeup = FALSE;
6473075778bSJohn Dyson 	map->system_map = 0;
648df8bae1dSRodney W. Grimes 	map->min_offset = min;
649df8bae1dSRodney W. Grimes 	map->max_offset = max;
650af7cd0c5SBrian Feldman 	map->flags = 0;
6514e94f402SAlan Cox 	map->root = NULL;
652df8bae1dSRodney W. Grimes 	map->timestamp = 0;
653df8bae1dSRodney W. Grimes }
654df8bae1dSRodney W. Grimes 
655a18b1f1dSJason Evans void
6568355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
657a18b1f1dSJason Evans {
6588355f576SJeff Roberson 	_vm_map_init(map, min, max);
659d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
66012c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
661a18b1f1dSJason Evans }
662a18b1f1dSJason Evans 
663df8bae1dSRodney W. Grimes /*
664b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
665b18bfc3dSJohn Dyson  *
666b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
667b18bfc3dSJohn Dyson  */
66862487bb4SJohn Dyson static void
6691b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
670b18bfc3dSJohn Dyson {
6712b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
672b18bfc3dSJohn Dyson }
673b18bfc3dSJohn Dyson 
674b18bfc3dSJohn Dyson /*
675df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
676df8bae1dSRodney W. Grimes  *
677df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
678b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
679df8bae1dSRodney W. Grimes  */
680f708ef1bSPoul-Henning Kamp static vm_map_entry_t
6811b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
682df8bae1dSRodney W. Grimes {
6831f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
6841f6889a1SMatthew Dillon 
6852b4a2c27SAlan Cox 	if (map->system_map)
6862b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
6872b4a2c27SAlan Cox 	else
688a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
6891f6889a1SMatthew Dillon 	if (new_entry == NULL)
6901f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
6911f6889a1SMatthew Dillon 	return (new_entry);
692df8bae1dSRodney W. Grimes }
693df8bae1dSRodney W. Grimes 
694df8bae1dSRodney W. Grimes /*
695794316a8SAlan Cox  *	vm_map_entry_set_behavior:
696794316a8SAlan Cox  *
697794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
698794316a8SAlan Cox  *	sequential.
699794316a8SAlan Cox  */
70062a59e8fSWarner Losh static inline void
701794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
702794316a8SAlan Cox {
703794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
704794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
705794316a8SAlan Cox }
706794316a8SAlan Cox 
707794316a8SAlan Cox /*
7080164e057SAlan Cox  *	vm_map_entry_set_max_free:
7090164e057SAlan Cox  *
7100164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
7110164e057SAlan Cox  */
71262a59e8fSWarner Losh static inline void
7130164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
7140164e057SAlan Cox {
7150164e057SAlan Cox 
7160164e057SAlan Cox 	entry->max_free = entry->adj_free;
7170164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
7180164e057SAlan Cox 		entry->max_free = entry->left->max_free;
7190164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
7200164e057SAlan Cox 		entry->max_free = entry->right->max_free;
7210164e057SAlan Cox }
7220164e057SAlan Cox 
7230164e057SAlan Cox /*
7244e94f402SAlan Cox  *	vm_map_entry_splay:
7254e94f402SAlan Cox  *
7260164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
7270164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
7280164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
7290164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
7300164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
7310164e057SAlan Cox  *	amortized.
7320164e057SAlan Cox  *
7330164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
7340164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
7350164e057SAlan Cox  *
7360164e057SAlan Cox  *	The map must be locked, and leaves it so.
7370164e057SAlan Cox  *
7380164e057SAlan Cox  *	Returns: the new root.
7394e94f402SAlan Cox  */
7404e94f402SAlan Cox static vm_map_entry_t
7410164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
7424e94f402SAlan Cox {
7430164e057SAlan Cox 	vm_map_entry_t llist, rlist;
7440164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
7450164e057SAlan Cox 	vm_map_entry_t y;
7464e94f402SAlan Cox 
7470164e057SAlan Cox 	/* Special case of empty tree. */
7484e94f402SAlan Cox 	if (root == NULL)
7494e94f402SAlan Cox 		return (root);
7500164e057SAlan Cox 
7510164e057SAlan Cox 	/*
7520164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
7530164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
7540164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
7550164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
7560164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
7570164e057SAlan Cox 	 * the two spines.
7580164e057SAlan Cox 	 */
7590164e057SAlan Cox 	llist = NULL;
7600164e057SAlan Cox 	rlist = NULL;
7610164e057SAlan Cox 	for (;;) {
7620164e057SAlan Cox 		/* root is never NULL in here. */
7630164e057SAlan Cox 		if (addr < root->start) {
7640164e057SAlan Cox 			y = root->left;
7650164e057SAlan Cox 			if (y == NULL)
7664e94f402SAlan Cox 				break;
7670164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
7680164e057SAlan Cox 				/* Rotate right and put y on rlist. */
7694e94f402SAlan Cox 				root->left = y->right;
7704e94f402SAlan Cox 				y->right = root;
7710164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7720164e057SAlan Cox 				root = y->left;
7730164e057SAlan Cox 				y->left = rlist;
7740164e057SAlan Cox 				rlist = y;
7750164e057SAlan Cox 			} else {
7760164e057SAlan Cox 				/* Put root on rlist. */
7770164e057SAlan Cox 				root->left = rlist;
7780164e057SAlan Cox 				rlist = root;
7794e94f402SAlan Cox 				root = y;
7804e94f402SAlan Cox 			}
7817438d60bSAlan Cox 		} else if (addr >= root->end) {
7820164e057SAlan Cox 			y = root->right;
7837438d60bSAlan Cox 			if (y == NULL)
7844e94f402SAlan Cox 				break;
7850164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
7860164e057SAlan Cox 				/* Rotate left and put y on llist. */
7874e94f402SAlan Cox 				root->right = y->left;
7884e94f402SAlan Cox 				y->left = root;
7890164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7900164e057SAlan Cox 				root = y->right;
7910164e057SAlan Cox 				y->right = llist;
7920164e057SAlan Cox 				llist = y;
7930164e057SAlan Cox 			} else {
7940164e057SAlan Cox 				/* Put root on llist. */
7950164e057SAlan Cox 				root->right = llist;
7960164e057SAlan Cox 				llist = root;
7974e94f402SAlan Cox 				root = y;
7984e94f402SAlan Cox 			}
7997438d60bSAlan Cox 		} else
8007438d60bSAlan Cox 			break;
8010164e057SAlan Cox 	}
8020164e057SAlan Cox 
8030164e057SAlan Cox 	/*
8040164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
8050164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
8060164e057SAlan Cox 	 * bottom of llist and rlist.
8070164e057SAlan Cox 	 */
8080164e057SAlan Cox 	ltree = root->left;
8090164e057SAlan Cox 	while (llist != NULL) {
8100164e057SAlan Cox 		y = llist->right;
8110164e057SAlan Cox 		llist->right = ltree;
8120164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
8130164e057SAlan Cox 		ltree = llist;
8140164e057SAlan Cox 		llist = y;
8150164e057SAlan Cox 	}
8160164e057SAlan Cox 	rtree = root->right;
8170164e057SAlan Cox 	while (rlist != NULL) {
8180164e057SAlan Cox 		y = rlist->left;
8190164e057SAlan Cox 		rlist->left = rtree;
8200164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
8210164e057SAlan Cox 		rtree = rlist;
8220164e057SAlan Cox 		rlist = y;
8230164e057SAlan Cox 	}
8240164e057SAlan Cox 
8250164e057SAlan Cox 	/*
8260164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
8270164e057SAlan Cox 	 */
8280164e057SAlan Cox 	root->left = ltree;
8290164e057SAlan Cox 	root->right = rtree;
8300164e057SAlan Cox 	vm_map_entry_set_max_free(root);
8310164e057SAlan Cox 
8324e94f402SAlan Cox 	return (root);
8334e94f402SAlan Cox }
8344e94f402SAlan Cox 
8354e94f402SAlan Cox /*
836df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
837df8bae1dSRodney W. Grimes  *
838df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
839df8bae1dSRodney W. Grimes  */
8404e94f402SAlan Cox static void
84199c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
84299c81ca9SAlan Cox 		  vm_map_entry_t after_where,
84399c81ca9SAlan Cox 		  vm_map_entry_t entry)
84499c81ca9SAlan Cox {
84521c641b2SJohn Baldwin 
84621c641b2SJohn Baldwin 	CTR4(KTR_VM,
84721c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
84821c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
84999c81ca9SAlan Cox 	map->nentries++;
85099c81ca9SAlan Cox 	entry->prev = after_where;
85199c81ca9SAlan Cox 	entry->next = after_where->next;
85299c81ca9SAlan Cox 	entry->next->prev = entry;
85399c81ca9SAlan Cox 	after_where->next = entry;
8544e94f402SAlan Cox 
8554e94f402SAlan Cox 	if (after_where != &map->header) {
8564e94f402SAlan Cox 		if (after_where != map->root)
8574e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
8584e94f402SAlan Cox 		entry->right = after_where->right;
8594e94f402SAlan Cox 		entry->left = after_where;
8604e94f402SAlan Cox 		after_where->right = NULL;
8610164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
8620164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
8634e94f402SAlan Cox 	} else {
8644e94f402SAlan Cox 		entry->right = map->root;
8654e94f402SAlan Cox 		entry->left = NULL;
8664e94f402SAlan Cox 	}
8670164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
8680164e057SAlan Cox 	    entry->next->start) - entry->end;
8690164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
8704e94f402SAlan Cox 	map->root = entry;
871df8bae1dSRodney W. Grimes }
87299c81ca9SAlan Cox 
8734e94f402SAlan Cox static void
87499c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
87599c81ca9SAlan Cox 		    vm_map_entry_t entry)
87699c81ca9SAlan Cox {
8774e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
87899c81ca9SAlan Cox 
8794e94f402SAlan Cox 	if (entry != map->root)
8804e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
8814e94f402SAlan Cox 	if (entry->left == NULL)
8824e94f402SAlan Cox 		root = entry->right;
8834e94f402SAlan Cox 	else {
8844e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
8854e94f402SAlan Cox 		root->right = entry->right;
8860164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
8870164e057SAlan Cox 		    entry->next->start) - root->end;
8880164e057SAlan Cox 		vm_map_entry_set_max_free(root);
8894e94f402SAlan Cox 	}
8904e94f402SAlan Cox 	map->root = root;
8914e94f402SAlan Cox 
8924e94f402SAlan Cox 	prev = entry->prev;
8934e94f402SAlan Cox 	next = entry->next;
89499c81ca9SAlan Cox 	next->prev = prev;
89599c81ca9SAlan Cox 	prev->next = next;
89699c81ca9SAlan Cox 	map->nentries--;
89721c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
89821c641b2SJohn Baldwin 	    map->nentries, entry);
899df8bae1dSRodney W. Grimes }
900df8bae1dSRodney W. Grimes 
901df8bae1dSRodney W. Grimes /*
9020164e057SAlan Cox  *	vm_map_entry_resize_free:
9030164e057SAlan Cox  *
9040164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
9050164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
9060164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
9070164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
9080164e057SAlan Cox  *
9090164e057SAlan Cox  *	The map must be locked, and leaves it so.
9100164e057SAlan Cox  */
9110164e057SAlan Cox static void
9120164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
9130164e057SAlan Cox {
9140164e057SAlan Cox 
9150164e057SAlan Cox 	/*
9160164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
9170164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
9180164e057SAlan Cox 	 * root and making the change there.
9190164e057SAlan Cox 	 */
9200164e057SAlan Cox 	if (entry != map->root)
9210164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
9220164e057SAlan Cox 
9230164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9240164e057SAlan Cox 	    entry->next->start) - entry->end;
9250164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9260164e057SAlan Cox }
9270164e057SAlan Cox 
9280164e057SAlan Cox /*
929df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
930df8bae1dSRodney W. Grimes  *
931df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
932df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
933df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
934df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
935df8bae1dSRodney W. Grimes  *	result indicates whether the address is
936df8bae1dSRodney W. Grimes  *	actually contained in the map.
937df8bae1dSRodney W. Grimes  */
9380d94caffSDavid Greenman boolean_t
9391b40f8c0SMatthew Dillon vm_map_lookup_entry(
9401b40f8c0SMatthew Dillon 	vm_map_t map,
9411b40f8c0SMatthew Dillon 	vm_offset_t address,
9421b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
943df8bae1dSRodney W. Grimes {
944c0877f10SJohn Dyson 	vm_map_entry_t cur;
94505a8c414SAlan Cox 	boolean_t locked;
946df8bae1dSRodney W. Grimes 
9474c3ef59eSAlan Cox 	/*
9484c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
9494c3ef59eSAlan Cox 	 * "address" is the map's header.
9504c3ef59eSAlan Cox 	 */
9514c3ef59eSAlan Cox 	cur = map->root;
9524e94f402SAlan Cox 	if (cur == NULL)
9534e94f402SAlan Cox 		*entry = &map->header;
9544c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
9554c3ef59eSAlan Cox 		*entry = cur;
9564c3ef59eSAlan Cox 		return (TRUE);
95705a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
95805a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
95905a8c414SAlan Cox 		/*
96005a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
96105a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
96205a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
96305a8c414SAlan Cox 		 * on a temporary upgrade.
96405a8c414SAlan Cox 		 */
9654c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
96605a8c414SAlan Cox 		if (!locked)
96705a8c414SAlan Cox 			sx_downgrade(&map->lock);
968df8bae1dSRodney W. Grimes 
9694c3ef59eSAlan Cox 		/*
9704c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
9714c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
9724c3ef59eSAlan Cox 		 * immediately before or after "address".
9734c3ef59eSAlan Cox 		 */
974df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
975df8bae1dSRodney W. Grimes 			*entry = cur;
9764e94f402SAlan Cox 			if (cur->end > address)
977df8bae1dSRodney W. Grimes 				return (TRUE);
9784e94f402SAlan Cox 		} else
979df8bae1dSRodney W. Grimes 			*entry = cur->prev;
98005a8c414SAlan Cox 	} else
98105a8c414SAlan Cox 		/*
98205a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
98305a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
98405a8c414SAlan Cox 		 */
98505a8c414SAlan Cox 		for (;;) {
98605a8c414SAlan Cox 			if (address < cur->start) {
98705a8c414SAlan Cox 				if (cur->left == NULL) {
98805a8c414SAlan Cox 					*entry = cur->prev;
98905a8c414SAlan Cox 					break;
99005a8c414SAlan Cox 				}
99105a8c414SAlan Cox 				cur = cur->left;
99205a8c414SAlan Cox 			} else if (cur->end > address) {
99305a8c414SAlan Cox 				*entry = cur;
99405a8c414SAlan Cox 				return (TRUE);
99505a8c414SAlan Cox 			} else {
99605a8c414SAlan Cox 				if (cur->right == NULL) {
99705a8c414SAlan Cox 					*entry = cur;
99805a8c414SAlan Cox 					break;
99905a8c414SAlan Cox 				}
100005a8c414SAlan Cox 				cur = cur->right;
100105a8c414SAlan Cox 			}
10024e94f402SAlan Cox 		}
1003df8bae1dSRodney W. Grimes 	return (FALSE);
1004df8bae1dSRodney W. Grimes }
1005df8bae1dSRodney W. Grimes 
1006df8bae1dSRodney W. Grimes /*
100730dcfc09SJohn Dyson  *	vm_map_insert:
100830dcfc09SJohn Dyson  *
100930dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
101030dcfc09SJohn Dyson  *	map at the specified address range.  The object's
101130dcfc09SJohn Dyson  *	size should match that of the address range.
101230dcfc09SJohn Dyson  *
101330dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
10142aaeadf8SMatthew Dillon  *
10152aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
10162aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
101730dcfc09SJohn Dyson  */
101830dcfc09SJohn Dyson int
1019b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1020b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1021b9dcd593SBruce Evans 	      int cow)
102230dcfc09SJohn Dyson {
1023c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1024c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
102530dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
10269730a5daSPaul Saab 	vm_eflags_t protoeflags;
102730dcfc09SJohn Dyson 
102830dcfc09SJohn Dyson 	/*
102930dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
103030dcfc09SJohn Dyson 	 */
103130dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
103230dcfc09SJohn Dyson 	    (start >= end))
103330dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
103430dcfc09SJohn Dyson 
103530dcfc09SJohn Dyson 	/*
103630dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
103730dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
103830dcfc09SJohn Dyson 	 */
103930dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
104030dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
104130dcfc09SJohn Dyson 
104230dcfc09SJohn Dyson 	prev_entry = temp_entry;
104330dcfc09SJohn Dyson 
104430dcfc09SJohn Dyson 	/*
104530dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
104630dcfc09SJohn Dyson 	 */
104730dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
104830dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
104930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
105030dcfc09SJohn Dyson 
1051afa07f7eSJohn Dyson 	protoeflags = 0;
1052afa07f7eSJohn Dyson 
1053afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1054e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1055afa07f7eSJohn Dyson 
10564e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1057afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1058afa07f7eSJohn Dyson 
10594e045f93SAlan Cox 		KASSERT(object == NULL,
10604e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
10614e045f93SAlan Cox 	}
10624f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
10634f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
10649730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
10659730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
10664f79d873SMatthew Dillon 
10671d284e00SAlan Cox 	if (object != NULL) {
106830dcfc09SJohn Dyson 		/*
10691d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
10701d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
10711d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
10721d284e00SAlan Cox 		 * reference counting is insufficient to recognize
10731d284e00SAlan Cox 		 * aliases with precision.)
107430dcfc09SJohn Dyson 		 */
10751d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
10761d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
10772aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
10781d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
10794e045f93SAlan Cox 	}
10804e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
10814e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
10828cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
10834e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
10844e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
10858cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
108657a21abaSAlan Cox 				     prev_entry->offset,
10878cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
1088cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
108930dcfc09SJohn Dyson 		/*
10902aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
10912aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
10922aaeadf8SMatthew Dillon 		 * new range as well.
109330dcfc09SJohn Dyson 		 */
10948cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
10958cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
10968cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
109730dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
109830dcfc09SJohn Dyson 			prev_entry->end = end;
10990164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
11004e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
110130dcfc09SJohn Dyson 			return (KERN_SUCCESS);
110230dcfc09SJohn Dyson 		}
11038cc7e047SJohn Dyson 
11042aaeadf8SMatthew Dillon 		/*
11052aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
11062aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
11072aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
11084e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
11092aaeadf8SMatthew Dillon 		 */
11102aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
11112aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
11122aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
11138cc7e047SJohn Dyson 		vm_object_reference(object);
1114b18bfc3dSJohn Dyson 	}
11152aaeadf8SMatthew Dillon 
11162aaeadf8SMatthew Dillon 	/*
11172aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
11182aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
11192aaeadf8SMatthew Dillon 	 * backing objects.
11202aaeadf8SMatthew Dillon 	 */
11218cc7e047SJohn Dyson 
112230dcfc09SJohn Dyson 	/*
112330dcfc09SJohn Dyson 	 * Create a new entry
112430dcfc09SJohn Dyson 	 */
112530dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
112630dcfc09SJohn Dyson 	new_entry->start = start;
112730dcfc09SJohn Dyson 	new_entry->end = end;
112830dcfc09SJohn Dyson 
1129afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
113030dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
113130dcfc09SJohn Dyson 	new_entry->offset = offset;
11322267af78SJulian Elischer 	new_entry->avail_ssize = 0;
11332267af78SJulian Elischer 
113430dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
113530dcfc09SJohn Dyson 	new_entry->protection = prot;
113630dcfc09SJohn Dyson 	new_entry->max_protection = max;
113730dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1138e5f251d2SAlan Cox 
113930dcfc09SJohn Dyson 	/*
114030dcfc09SJohn Dyson 	 * Insert the new entry into the list
114130dcfc09SJohn Dyson 	 */
114230dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
114330dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
114430dcfc09SJohn Dyson 
11451a484d28SMatthew Dillon #if 0
11461a484d28SMatthew Dillon 	/*
11471a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
11481a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
11491a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
11501a484d28SMatthew Dillon 	 */
11514e71e795SMatthew Dillon 	/*
11524e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
11534e71e795SMatthew Dillon 	 */
11544e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
11551a484d28SMatthew Dillon #endif
11564e71e795SMatthew Dillon 
11574f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
11584da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1159e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1160e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
11614f79d873SMatthew Dillon 	}
1162e972780aSAlan Cox 
116330dcfc09SJohn Dyson 	return (KERN_SUCCESS);
116430dcfc09SJohn Dyson }
116530dcfc09SJohn Dyson 
116630dcfc09SJohn Dyson /*
11670164e057SAlan Cox  *	vm_map_findspace:
11680164e057SAlan Cox  *
11690164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
11700164e057SAlan Cox  *	beginning at address >= start in the given map.
11710164e057SAlan Cox  *
11720164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
11730164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
11740164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
11750164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
11760164e057SAlan Cox  *	O(log n) amortized with splay trees.
11770164e057SAlan Cox  *
11780164e057SAlan Cox  *	The map must be locked, and leaves it so.
11790164e057SAlan Cox  *
11800164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
11810164e057SAlan Cox  *		 1 if insufficient space.
1182df8bae1dSRodney W. Grimes  */
1183df8bae1dSRodney W. Grimes int
11840164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
11850164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1186df8bae1dSRodney W. Grimes {
11870164e057SAlan Cox 	vm_map_entry_t entry;
11880164e057SAlan Cox 	vm_offset_t end, st;
1189df8bae1dSRodney W. Grimes 
1190986b43f8SAlan Cox 	/*
1191986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1192986b43f8SAlan Cox 	 * address wrap.
1193986b43f8SAlan Cox 	 */
1194df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1195df8bae1dSRodney W. Grimes 		start = map->min_offset;
1196986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1197df8bae1dSRodney W. Grimes 		return (1);
1198df8bae1dSRodney W. Grimes 
11990164e057SAlan Cox 	/* Empty tree means wide open address space. */
12000164e057SAlan Cox 	if (map->root == NULL) {
1201df8bae1dSRodney W. Grimes 		*addr = start;
12020164e057SAlan Cox 		goto found;
120399448ed1SJohn Dyson 	}
12040164e057SAlan Cox 
12050164e057SAlan Cox 	/*
12060164e057SAlan Cox 	 * After splay, if start comes before root node, then there
12070164e057SAlan Cox 	 * must be a gap from start to the root.
12080164e057SAlan Cox 	 */
12090164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
12100164e057SAlan Cox 	if (start + length <= map->root->start) {
12110164e057SAlan Cox 		*addr = start;
12120164e057SAlan Cox 		goto found;
12130164e057SAlan Cox 	}
12140164e057SAlan Cox 
12150164e057SAlan Cox 	/*
12160164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1217986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1218986b43f8SAlan Cox 	 * wrap might be a problem.
12190164e057SAlan Cox 	 */
12200164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1221986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
12220164e057SAlan Cox 		*addr = st;
12230164e057SAlan Cox 		goto found;
12240164e057SAlan Cox 	}
12250164e057SAlan Cox 
12260164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
12270164e057SAlan Cox 	entry = map->root->right;
12280164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
12290164e057SAlan Cox 		return (1);
12300164e057SAlan Cox 
12310164e057SAlan Cox 	/*
12320164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
12330164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
12340164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
12350164e057SAlan Cox 	 */
12360164e057SAlan Cox 	while (entry != NULL) {
12370164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
12380164e057SAlan Cox 			entry = entry->left;
12390164e057SAlan Cox 		else if (entry->adj_free >= length) {
12400164e057SAlan Cox 			*addr = entry->end;
12410164e057SAlan Cox 			goto found;
12420164e057SAlan Cox 		} else
12430164e057SAlan Cox 			entry = entry->right;
12440164e057SAlan Cox 	}
12450164e057SAlan Cox 
12460164e057SAlan Cox 	/* Can't get here, so panic if we do. */
12470164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
12480164e057SAlan Cox 
12490164e057SAlan Cox found:
12500164e057SAlan Cox 	/* Expand the kernel pmap, if necessary. */
12510164e057SAlan Cox 	if (map == kernel_map) {
12520164e057SAlan Cox 		end = round_page(*addr + length);
12530164e057SAlan Cox 		if (end > kernel_vm_end)
12540164e057SAlan Cox 			pmap_growkernel(end);
125599448ed1SJohn Dyson 	}
1256df8bae1dSRodney W. Grimes 	return (0);
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259d239bd3cSKonstantin Belousov int
1260d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1261b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1262d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1263d239bd3cSKonstantin Belousov {
1264897d81a0SKonstantin Belousov 	vm_map_entry_t freelist;
1265b8ca4ef2SAlan Cox 	vm_offset_t end;
1266d239bd3cSKonstantin Belousov 	int result;
1267d239bd3cSKonstantin Belousov 
1268d239bd3cSKonstantin Belousov 	end = start + length;
1269897d81a0SKonstantin Belousov 	freelist = NULL;
1270897d81a0SKonstantin Belousov 	vm_map_lock(map);
1271d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1272897d81a0SKonstantin Belousov 	(void) vm_map_delete(map, start, end, &freelist);
1273d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1274d239bd3cSKonstantin Belousov 	    max, cow);
1275d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1276897d81a0SKonstantin Belousov 	vm_map_entry_free_freelist(map, freelist);
1277d239bd3cSKonstantin Belousov 	return (result);
1278d239bd3cSKonstantin Belousov }
1279d239bd3cSKonstantin Belousov 
1280df8bae1dSRodney W. Grimes /*
1281df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1282df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1283df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1284df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1285df8bae1dSRodney W. Grimes  *
12862aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
12872aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1288df8bae1dSRodney W. Grimes  */
1289df8bae1dSRodney W. Grimes int
1290b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1291b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
129226c538ffSAlan Cox 	    vm_size_t length, int find_space, vm_prot_t prot,
1293b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1294df8bae1dSRodney W. Grimes {
1295c0877f10SJohn Dyson 	vm_offset_t start;
12966eaee3feSAlan Cox 	int result;
1297df8bae1dSRodney W. Grimes 
1298df8bae1dSRodney W. Grimes 	start = *addr;
1299bea41bcfSDavid Greenman 	vm_map_lock(map);
130026c538ffSAlan Cox 	do {
130126c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1302df8bae1dSRodney W. Grimes 			if (vm_map_findspace(map, start, length, addr)) {
1303df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1304df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1305df8bae1dSRodney W. Grimes 			}
130626c538ffSAlan Cox 			if (find_space == VMFS_ALIGNED_SPACE)
130726c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
130826c538ffSAlan Cox 				    length);
1309df8bae1dSRodney W. Grimes 			start = *addr;
1310df8bae1dSRodney W. Grimes 		}
131126c538ffSAlan Cox 		result = vm_map_insert(map, object, offset, start, start +
131226c538ffSAlan Cox 		    length, prot, max, cow);
131326c538ffSAlan Cox 	} while (result == KERN_NO_SPACE && find_space == VMFS_ALIGNED_SPACE);
1314df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1315df8bae1dSRodney W. Grimes 	return (result);
1316df8bae1dSRodney W. Grimes }
1317df8bae1dSRodney W. Grimes 
1318df8bae1dSRodney W. Grimes /*
1319b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
132067bf6868SJohn Dyson  *
13214e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
13224e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
13234e71e795SMatthew Dillon  *
13244e71e795SMatthew Dillon  *	The map must be locked.
13254e71e795SMatthew Dillon  *
13264e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
13274e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
13284e71e795SMatthew Dillon  *	both neighbors.
1329df8bae1dSRodney W. Grimes  */
1330b7b2aac2SJohn Dyson void
13311b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1332df8bae1dSRodney W. Grimes {
1333308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1334b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1335df8bae1dSRodney W. Grimes 
1336acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1337df8bae1dSRodney W. Grimes 		return;
1338308c24baSJohn Dyson 
1339308c24baSJohn Dyson 	prev = entry->prev;
1340308c24baSJohn Dyson 	if (prev != &map->header) {
134167bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
134267bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
134367bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
134495e5e988SJohn Dyson 		     (!prev->object.vm_object ||
134567bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1346afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
134767bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
134867bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
134967bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
1350b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
1351308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1352308c24baSJohn Dyson 			entry->start = prev->start;
1353308c24baSJohn Dyson 			entry->offset = prev->offset;
13540164e057SAlan Cox 			if (entry->prev != &map->header)
13550164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
13567fd10fb3SKonstantin Belousov 
13577fd10fb3SKonstantin Belousov 			/*
1358b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1359b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1360b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1361b0994946SKonstantin Belousov 			 * because the vnode has additional
1362b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1363b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1364b0994946SKonstantin Belousov 			 * the vnode lock.
13657fd10fb3SKonstantin Belousov 			 */
1366b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1367308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1368308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1369308c24baSJohn Dyson 		}
1370308c24baSJohn Dyson 	}
1371de5f6a77SJohn Dyson 
1372de5f6a77SJohn Dyson 	next = entry->next;
1373308c24baSJohn Dyson 	if (next != &map->header) {
137467bf6868SJohn Dyson 		esize = entry->end - entry->start;
137567bf6868SJohn Dyson 		if ((entry->end == next->start) &&
137667bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
137767bf6868SJohn Dyson 		     (!entry->object.vm_object ||
137867bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1379afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
138067bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
138167bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
138267bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
1383b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
1384de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1385de5f6a77SJohn Dyson 			entry->end = next->end;
13860164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
13877fd10fb3SKonstantin Belousov 
13887fd10fb3SKonstantin Belousov 			/*
13897fd10fb3SKonstantin Belousov 			 * See comment above.
13907fd10fb3SKonstantin Belousov 			 */
1391b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1392de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1393de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1394df8bae1dSRodney W. Grimes 		}
1395df8bae1dSRodney W. Grimes 	}
1396de5f6a77SJohn Dyson }
1397df8bae1dSRodney W. Grimes /*
1398df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1399df8bae1dSRodney W. Grimes  *
1400df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1401df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1402df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1403df8bae1dSRodney W. Grimes  */
1404df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1405df8bae1dSRodney W. Grimes { \
1406df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1407df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1408df8bae1dSRodney W. Grimes }
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes /*
1411df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1412df8bae1dSRodney W. Grimes  *	the entry must be split.
1413df8bae1dSRodney W. Grimes  */
14140d94caffSDavid Greenman static void
14151b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1416df8bae1dSRodney W. Grimes {
1417c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1418df8bae1dSRodney W. Grimes 
1419df8bae1dSRodney W. Grimes 	/*
14200d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
14210d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
14220d94caffSDavid Greenman 	 * starting address.
1423df8bae1dSRodney W. Grimes 	 */
1424f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1425f32dbbeeSJohn Dyson 
142611cccda1SJohn Dyson 	/*
142711cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
142811cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
142911cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
143011cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
143111cccda1SJohn Dyson 	 * put this improvement.
143211cccda1SJohn Dyson 	 */
14334e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
143411cccda1SJohn Dyson 		vm_object_t object;
143511cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1436c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
143711cccda1SJohn Dyson 		entry->object.vm_object = object;
143811cccda1SJohn Dyson 		entry->offset = 0;
143911cccda1SJohn Dyson 	}
144011cccda1SJohn Dyson 
1441df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1442df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1443df8bae1dSRodney W. Grimes 
1444df8bae1dSRodney W. Grimes 	new_entry->end = start;
1445df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1446df8bae1dSRodney W. Grimes 	entry->start = start;
1447df8bae1dSRodney W. Grimes 
1448df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1449df8bae1dSRodney W. Grimes 
14509fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1451df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1452df8bae1dSRodney W. Grimes 	}
1453c0877f10SJohn Dyson }
1454df8bae1dSRodney W. Grimes 
1455df8bae1dSRodney W. Grimes /*
1456df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1457df8bae1dSRodney W. Grimes  *
1458df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1459df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1460df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1461df8bae1dSRodney W. Grimes  */
1462df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1463df8bae1dSRodney W. Grimes { \
1464af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1465af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1466df8bae1dSRodney W. Grimes }
1467df8bae1dSRodney W. Grimes 
1468df8bae1dSRodney W. Grimes /*
1469df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1470df8bae1dSRodney W. Grimes  *	the entry must be split.
1471df8bae1dSRodney W. Grimes  */
14720d94caffSDavid Greenman static void
14731b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1474df8bae1dSRodney W. Grimes {
1475c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1476df8bae1dSRodney W. Grimes 
1477df8bae1dSRodney W. Grimes 	/*
147811cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
147911cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
148011cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
148111cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
148211cccda1SJohn Dyson 	 * put this improvement.
148311cccda1SJohn Dyson 	 */
14844e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
148511cccda1SJohn Dyson 		vm_object_t object;
148611cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1487c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
148811cccda1SJohn Dyson 		entry->object.vm_object = object;
148911cccda1SJohn Dyson 		entry->offset = 0;
149011cccda1SJohn Dyson 	}
149111cccda1SJohn Dyson 
149211cccda1SJohn Dyson 	/*
14930d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1494df8bae1dSRodney W. Grimes 	 */
1495df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1496df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1497df8bae1dSRodney W. Grimes 
1498df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1499df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1500df8bae1dSRodney W. Grimes 
1501df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1502df8bae1dSRodney W. Grimes 
15039fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1504df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1505df8bae1dSRodney W. Grimes 	}
1506c0877f10SJohn Dyson }
1507df8bae1dSRodney W. Grimes 
1508df8bae1dSRodney W. Grimes /*
1509df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1510df8bae1dSRodney W. Grimes  *
1511df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1512df8bae1dSRodney W. Grimes  *
1513df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1514df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1515df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1516df8bae1dSRodney W. Grimes  *
1517df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1518df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1519df8bae1dSRodney W. Grimes  *		vm_fault
1520df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1521df8bae1dSRodney W. Grimes  *
1522df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1523df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1524df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1525df8bae1dSRodney W. Grimes  */
1526df8bae1dSRodney W. Grimes int
15271b40f8c0SMatthew Dillon vm_map_submap(
15281b40f8c0SMatthew Dillon 	vm_map_t map,
15291b40f8c0SMatthew Dillon 	vm_offset_t start,
15301b40f8c0SMatthew Dillon 	vm_offset_t end,
15311b40f8c0SMatthew Dillon 	vm_map_t submap)
1532df8bae1dSRodney W. Grimes {
1533df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1534c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1535df8bae1dSRodney W. Grimes 
1536df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1537df8bae1dSRodney W. Grimes 
1538df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1539df8bae1dSRodney W. Grimes 
1540df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1541df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
15420d94caffSDavid Greenman 	} else
1543df8bae1dSRodney W. Grimes 		entry = entry->next;
1544df8bae1dSRodney W. Grimes 
1545df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1546df8bae1dSRodney W. Grimes 
1547df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
15489fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1549afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
15502d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1551afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1552df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1553df8bae1dSRodney W. Grimes 	}
1554df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1555df8bae1dSRodney W. Grimes 
1556df8bae1dSRodney W. Grimes 	return (result);
1557df8bae1dSRodney W. Grimes }
1558df8bae1dSRodney W. Grimes 
1559df8bae1dSRodney W. Grimes /*
15601f78f902SAlan Cox  * The maximum number of pages to map
15611f78f902SAlan Cox  */
15621f78f902SAlan Cox #define	MAX_INIT_PT	96
15631f78f902SAlan Cox 
15641f78f902SAlan Cox /*
15650551c08dSAlan Cox  *	vm_map_pmap_enter:
15660551c08dSAlan Cox  *
1567cf4682aeSAlan Cox  *	Preload read-only mappings for the given object's resident pages into
1568cf4682aeSAlan Cox  *	the given map.  This eliminates the soft faults on process startup and
1569c4169725SAlan Cox  *	immediately after an mmap(2).  Because these are speculative mappings,
1570c4169725SAlan Cox  *	cached pages are not reactivated and mapped.
15710551c08dSAlan Cox  */
15720551c08dSAlan Cox void
15734da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
15740551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
15750551c08dSAlan Cox {
15768fece8c3SAlan Cox 	vm_offset_t start;
1577ce142d9eSAlan Cox 	vm_page_t p, p_start;
15788fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
15791f70d622SAlan Cox 	boolean_t are_queues_locked;
15800551c08dSAlan Cox 
1581ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
15821f78f902SAlan Cox 		return;
15831f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
15841f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
15851f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
15861f78f902SAlan Cox 		goto unlock_return;
15871f78f902SAlan Cox 	}
15881f78f902SAlan Cox 
15891f78f902SAlan Cox 	psize = atop(size);
15901f78f902SAlan Cox 
15911f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
15921f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
15931f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
15941f78f902SAlan Cox 		goto unlock_return;
15951f78f902SAlan Cox 	}
15961f78f902SAlan Cox 
15971f78f902SAlan Cox 	if (psize + pindex > object->size) {
15981f78f902SAlan Cox 		if (object->size < pindex)
15991f78f902SAlan Cox 			goto unlock_return;
16001f78f902SAlan Cox 		psize = object->size - pindex;
16011f78f902SAlan Cox 	}
16021f78f902SAlan Cox 
16031f70d622SAlan Cox 	are_queues_locked = FALSE;
1604ce142d9eSAlan Cox 	start = 0;
1605ce142d9eSAlan Cox 	p_start = NULL;
16061f78f902SAlan Cox 
16071f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
16081f78f902SAlan Cox 		if (p->pindex < pindex) {
16091f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
16101f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
16111f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
16121f78f902SAlan Cox 		}
16131f78f902SAlan Cox 	}
16141f78f902SAlan Cox 	/*
16151f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
16161f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
16171f78f902SAlan Cox 	 * or (2) NULL.
16181f78f902SAlan Cox 	 */
16191f78f902SAlan Cox 	for (;
16201f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
16211f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
16221f78f902SAlan Cox 		/*
16231f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
16241f78f902SAlan Cox 		 * free pages allocating pv entries.
16251f78f902SAlan Cox 		 */
16261f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
16272feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1628379fb642SAlan Cox 			psize = tmpidx;
16291f78f902SAlan Cox 			break;
16301f78f902SAlan Cox 		}
16311f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
16328fece8c3SAlan Cox 		    (p->busy == 0)) {
1633ce142d9eSAlan Cox 			if (p_start == NULL) {
1634ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1635ce142d9eSAlan Cox 				p_start = p;
1636ce142d9eSAlan Cox 			}
16377bfda801SAlan Cox 		} else if (p_start != NULL) {
16381f70d622SAlan Cox 			if (!are_queues_locked) {
16391f70d622SAlan Cox 				are_queues_locked = TRUE;
164085f5b245SAlan Cox 				vm_page_lock_queues();
164185f5b245SAlan Cox 			}
1642cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1643cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1644cf4682aeSAlan Cox 			p_start = NULL;
1645cf4682aeSAlan Cox 		}
1646cf4682aeSAlan Cox 	}
16477bfda801SAlan Cox 	if (p_start != NULL) {
16487bfda801SAlan Cox 		if (!are_queues_locked) {
16497bfda801SAlan Cox 			are_queues_locked = TRUE;
16507bfda801SAlan Cox 			vm_page_lock_queues();
16511f78f902SAlan Cox 		}
1652379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1653379fb642SAlan Cox 		    p_start, prot);
16547bfda801SAlan Cox 	}
16551f70d622SAlan Cox 	if (are_queues_locked)
16561f70d622SAlan Cox 		vm_page_unlock_queues();
16571f78f902SAlan Cox unlock_return:
16581f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
16590551c08dSAlan Cox }
16600551c08dSAlan Cox 
16610551c08dSAlan Cox /*
1662df8bae1dSRodney W. Grimes  *	vm_map_protect:
1663df8bae1dSRodney W. Grimes  *
1664df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1665df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1666df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1667df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1668df8bae1dSRodney W. Grimes  */
1669df8bae1dSRodney W. Grimes int
1670b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1671b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1672df8bae1dSRodney W. Grimes {
1673c0877f10SJohn Dyson 	vm_map_entry_t current;
1674df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1675df8bae1dSRodney W. Grimes 
1676df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1677df8bae1dSRodney W. Grimes 
1678df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1679df8bae1dSRodney W. Grimes 
1680df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1681df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1682b7b2aac2SJohn Dyson 	} else {
1683df8bae1dSRodney W. Grimes 		entry = entry->next;
1684b7b2aac2SJohn Dyson 	}
1685df8bae1dSRodney W. Grimes 
1686df8bae1dSRodney W. Grimes 	/*
16870d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1688df8bae1dSRodney W. Grimes 	 */
1689df8bae1dSRodney W. Grimes 	current = entry;
1690df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1691afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1692a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1693df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1694a1f6d91cSDavid Greenman 		}
1695df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1696df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1697df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1698df8bae1dSRodney W. Grimes 		}
1699df8bae1dSRodney W. Grimes 		current = current->next;
1700df8bae1dSRodney W. Grimes 	}
1701df8bae1dSRodney W. Grimes 
1702df8bae1dSRodney W. Grimes 	/*
17030d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
17040d94caffSDavid Greenman 	 * necessary the second time.]
1705df8bae1dSRodney W. Grimes 	 */
1706df8bae1dSRodney W. Grimes 	current = entry;
1707df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1708df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1709df8bae1dSRodney W. Grimes 
1710df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1711df8bae1dSRodney W. Grimes 
1712df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1713df8bae1dSRodney W. Grimes 		if (set_max)
1714df8bae1dSRodney W. Grimes 			current->protection =
1715df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1716df8bae1dSRodney W. Grimes 			    old_prot;
1717df8bae1dSRodney W. Grimes 		else
1718df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1719df8bae1dSRodney W. Grimes 
1720df8bae1dSRodney W. Grimes 		/*
17210d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
1722e2abaaaaSAlan Cox 		 * here.
1723df8bae1dSRodney W. Grimes 		 */
1724df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1725afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1726df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1727df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1728df8bae1dSRodney W. Grimes 			    current->end,
17291c85e3dfSAlan Cox 			    current->protection & MASK(current));
1730df8bae1dSRodney W. Grimes #undef	MASK
1731df8bae1dSRodney W. Grimes 		}
17327d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1733df8bae1dSRodney W. Grimes 		current = current->next;
1734df8bae1dSRodney W. Grimes 	}
1735df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1736df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1737df8bae1dSRodney W. Grimes }
1738df8bae1dSRodney W. Grimes 
1739df8bae1dSRodney W. Grimes /*
1740867a482dSJohn Dyson  *	vm_map_madvise:
1741867a482dSJohn Dyson  *
1742867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1743f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1744f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1745f7fc307aSAlan Cox  *	objects.
1746867a482dSJohn Dyson  */
1747b4309055SMatthew Dillon int
17481b40f8c0SMatthew Dillon vm_map_madvise(
17491b40f8c0SMatthew Dillon 	vm_map_t map,
17501b40f8c0SMatthew Dillon 	vm_offset_t start,
17511b40f8c0SMatthew Dillon 	vm_offset_t end,
17521b40f8c0SMatthew Dillon 	int behav)
1753867a482dSJohn Dyson {
1754f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1755b4309055SMatthew Dillon 	int modify_map = 0;
1756867a482dSJohn Dyson 
1757b4309055SMatthew Dillon 	/*
1758b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1759b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1760b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1761b4309055SMatthew Dillon 	 * on the map.
1762b4309055SMatthew Dillon 	 */
1763b4309055SMatthew Dillon 	switch(behav) {
1764b4309055SMatthew Dillon 	case MADV_NORMAL:
1765b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1766b4309055SMatthew Dillon 	case MADV_RANDOM:
17674f79d873SMatthew Dillon 	case MADV_NOSYNC:
17684f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
17699730a5daSPaul Saab 	case MADV_NOCORE:
17709730a5daSPaul Saab 	case MADV_CORE:
1771b4309055SMatthew Dillon 		modify_map = 1;
1772867a482dSJohn Dyson 		vm_map_lock(map);
1773b4309055SMatthew Dillon 		break;
1774b4309055SMatthew Dillon 	case MADV_WILLNEED:
1775b4309055SMatthew Dillon 	case MADV_DONTNEED:
1776b4309055SMatthew Dillon 	case MADV_FREE:
1777f7fc307aSAlan Cox 		vm_map_lock_read(map);
1778b4309055SMatthew Dillon 		break;
1779b4309055SMatthew Dillon 	default:
1780b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1781b4309055SMatthew Dillon 	}
1782b4309055SMatthew Dillon 
1783b4309055SMatthew Dillon 	/*
1784b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1785b4309055SMatthew Dillon 	 */
1786867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1787867a482dSJohn Dyson 
1788867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1789f7fc307aSAlan Cox 		if (modify_map)
1790867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1791b4309055SMatthew Dillon 	} else {
1792867a482dSJohn Dyson 		entry = entry->next;
1793b4309055SMatthew Dillon 	}
1794867a482dSJohn Dyson 
1795f7fc307aSAlan Cox 	if (modify_map) {
1796f7fc307aSAlan Cox 		/*
1797f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1798f7fc307aSAlan Cox 		 *
1799f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1800f7fc307aSAlan Cox 		 * limited to the specified address range.
1801f7fc307aSAlan Cox 		 */
1802867a482dSJohn Dyson 		for (current = entry;
1803867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1804b4309055SMatthew Dillon 		     current = current->next
1805b4309055SMatthew Dillon 		) {
1806f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1807867a482dSJohn Dyson 				continue;
1808fed9a903SJohn Dyson 
180947221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1810fed9a903SJohn Dyson 
1811f7fc307aSAlan Cox 			switch (behav) {
1812867a482dSJohn Dyson 			case MADV_NORMAL:
18137f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1814867a482dSJohn Dyson 				break;
1815867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
18167f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1817867a482dSJohn Dyson 				break;
1818867a482dSJohn Dyson 			case MADV_RANDOM:
18197f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1820867a482dSJohn Dyson 				break;
18214f79d873SMatthew Dillon 			case MADV_NOSYNC:
18224f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
18234f79d873SMatthew Dillon 				break;
18244f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
18254f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
18264f79d873SMatthew Dillon 				break;
18279730a5daSPaul Saab 			case MADV_NOCORE:
18289730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
18299730a5daSPaul Saab 				break;
18309730a5daSPaul Saab 			case MADV_CORE:
18319730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
18329730a5daSPaul Saab 				break;
1833867a482dSJohn Dyson 			default:
1834867a482dSJohn Dyson 				break;
1835867a482dSJohn Dyson 			}
1836f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1837867a482dSJohn Dyson 		}
1838867a482dSJohn Dyson 		vm_map_unlock(map);
1839b4309055SMatthew Dillon 	} else {
1840f7fc307aSAlan Cox 		vm_pindex_t pindex;
1841f7fc307aSAlan Cox 		int count;
1842f7fc307aSAlan Cox 
1843f7fc307aSAlan Cox 		/*
1844f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1845f7fc307aSAlan Cox 		 * vm_object.
1846f7fc307aSAlan Cox 		 *
1847f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1848f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1849f7fc307aSAlan Cox 		 */
1850f7fc307aSAlan Cox 		for (current = entry;
1851f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1852b4309055SMatthew Dillon 		     current = current->next
1853b4309055SMatthew Dillon 		) {
18545f99b57cSMatthew Dillon 			vm_offset_t useStart;
18555f99b57cSMatthew Dillon 
1856f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1857f7fc307aSAlan Cox 				continue;
1858f7fc307aSAlan Cox 
1859f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1860f7fc307aSAlan Cox 			count = atop(current->end - current->start);
18615f99b57cSMatthew Dillon 			useStart = current->start;
1862f7fc307aSAlan Cox 
1863f7fc307aSAlan Cox 			if (current->start < start) {
1864f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1865f7fc307aSAlan Cox 				count -= atop(start - current->start);
18665f99b57cSMatthew Dillon 				useStart = start;
1867f7fc307aSAlan Cox 			}
1868f7fc307aSAlan Cox 			if (current->end > end)
1869f7fc307aSAlan Cox 				count -= atop(current->end - end);
1870f7fc307aSAlan Cox 
1871f7fc307aSAlan Cox 			if (count <= 0)
1872f7fc307aSAlan Cox 				continue;
1873f7fc307aSAlan Cox 
1874f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1875f7fc307aSAlan Cox 					  pindex, count, behav);
1876b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
18770551c08dSAlan Cox 				vm_map_pmap_enter(map,
18785f99b57cSMatthew Dillon 				    useStart,
18794da4d293SAlan Cox 				    current->protection,
1880f7fc307aSAlan Cox 				    current->object.vm_object,
1881b4309055SMatthew Dillon 				    pindex,
1882b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1883e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1884b4309055SMatthew Dillon 				);
1885f7fc307aSAlan Cox 			}
1886f7fc307aSAlan Cox 		}
1887f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1888f7fc307aSAlan Cox 	}
1889b4309055SMatthew Dillon 	return (0);
1890867a482dSJohn Dyson }
1891867a482dSJohn Dyson 
1892867a482dSJohn Dyson 
1893867a482dSJohn Dyson /*
1894df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1895df8bae1dSRodney W. Grimes  *
1896df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1897df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1898df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1899e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
1900df8bae1dSRodney W. Grimes  */
1901df8bae1dSRodney W. Grimes int
1902b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1903b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1904df8bae1dSRodney W. Grimes {
1905c0877f10SJohn Dyson 	vm_map_entry_t entry;
1906df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1907df8bae1dSRodney W. Grimes 
1908df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1909df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1910df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1911df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1912df8bae1dSRodney W. Grimes 		break;
1913df8bae1dSRodney W. Grimes 	default:
1914df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1915df8bae1dSRodney W. Grimes 	}
1916df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1917df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1918df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1919df8bae1dSRodney W. Grimes 		entry = temp_entry;
1920df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
19210d94caffSDavid Greenman 	} else
1922df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1923df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1924df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1925df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
192644428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
1927df8bae1dSRodney W. Grimes 		entry = entry->next;
1928df8bae1dSRodney W. Grimes 	}
1929df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1930df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1931df8bae1dSRodney W. Grimes }
1932df8bae1dSRodney W. Grimes 
1933df8bae1dSRodney W. Grimes /*
1934acd9a301SAlan Cox  *	vm_map_unwire:
1935acd9a301SAlan Cox  *
1936e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
1937acd9a301SAlan Cox  */
1938acd9a301SAlan Cox int
1939acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1940abd498aaSBruce M Simpson     int flags)
1941acd9a301SAlan Cox {
1942acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
1943acd9a301SAlan Cox 	vm_offset_t saved_start;
1944acd9a301SAlan Cox 	unsigned int last_timestamp;
1945acd9a301SAlan Cox 	int rv;
1946abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
1947acd9a301SAlan Cox 
1948abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1949acd9a301SAlan Cox 	vm_map_lock(map);
1950acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
1951acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1952abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1953cbef13d8SAlan Cox 			first_entry = first_entry->next;
1954abd498aaSBruce M Simpson 		else {
1955acd9a301SAlan Cox 			vm_map_unlock(map);
1956acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
1957acd9a301SAlan Cox 		}
1958abd498aaSBruce M Simpson 	}
1959acd9a301SAlan Cox 	last_timestamp = map->timestamp;
1960acd9a301SAlan Cox 	entry = first_entry;
1961acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1962acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1963acd9a301SAlan Cox 			/*
1964acd9a301SAlan Cox 			 * We have not yet clipped the entry.
1965acd9a301SAlan Cox 			 */
1966acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
1967acd9a301SAlan Cox 			    entry->start;
1968acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
19698ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
1970acd9a301SAlan Cox 				/*
1971acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
1972acd9a301SAlan Cox 				 */
1973acd9a301SAlan Cox 			}
1974acd9a301SAlan Cox 			vm_map_lock(map);
1975acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
1976acd9a301SAlan Cox 				/*
1977acd9a301SAlan Cox 				 * Look again for the entry because the map was
1978acd9a301SAlan Cox 				 * modified while it was unlocked.
1979acd9a301SAlan Cox 				 * Specifically, the entry may have been
1980acd9a301SAlan Cox 				 * clipped, merged, or deleted.
1981acd9a301SAlan Cox 				 */
1982acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
1983acd9a301SAlan Cox 				    &tmp_entry)) {
1984cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1985cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1986cbef13d8SAlan Cox 					else {
1987acd9a301SAlan Cox 						if (saved_start == start) {
1988acd9a301SAlan Cox 							/*
1989acd9a301SAlan Cox 							 * First_entry has been deleted.
1990acd9a301SAlan Cox 							 */
1991acd9a301SAlan Cox 							vm_map_unlock(map);
1992acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
1993acd9a301SAlan Cox 						}
1994acd9a301SAlan Cox 						end = saved_start;
1995acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
1996acd9a301SAlan Cox 						goto done;
1997acd9a301SAlan Cox 					}
1998cbef13d8SAlan Cox 				}
1999acd9a301SAlan Cox 				if (entry == first_entry)
2000acd9a301SAlan Cox 					first_entry = tmp_entry;
2001acd9a301SAlan Cox 				else
2002acd9a301SAlan Cox 					first_entry = NULL;
2003acd9a301SAlan Cox 				entry = tmp_entry;
2004acd9a301SAlan Cox 			}
2005acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2006acd9a301SAlan Cox 			continue;
2007acd9a301SAlan Cox 		}
2008acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2009acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2010acd9a301SAlan Cox 		/*
2011acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2012acd9a301SAlan Cox 		 * above.)
2013acd9a301SAlan Cox 		 */
2014acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2015acd9a301SAlan Cox 		/*
2016acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2017abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2018acd9a301SAlan Cox 		 */
2019abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2020abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2021abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2022acd9a301SAlan Cox 			end = entry->end;
2023acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2024acd9a301SAlan Cox 			goto done;
2025acd9a301SAlan Cox 		}
2026acd9a301SAlan Cox 		/*
20273ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2028acd9a301SAlan Cox 		 */
20290ada205eSBrian Feldman 		if (!user_unwire &&
20300ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2031acd9a301SAlan Cox 			end = entry->end;
2032acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2033acd9a301SAlan Cox 			goto done;
2034acd9a301SAlan Cox 		}
2035acd9a301SAlan Cox 		entry = entry->next;
2036acd9a301SAlan Cox 	}
2037acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2038acd9a301SAlan Cox done:
2039e27e17b7SAlan Cox 	need_wakeup = FALSE;
2040acd9a301SAlan Cox 	if (first_entry == NULL) {
2041acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2042cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2043cbef13d8SAlan Cox 			first_entry = first_entry->next;
2044cbef13d8SAlan Cox 		else
2045acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2046acd9a301SAlan Cox 	}
2047acd9a301SAlan Cox 	entry = first_entry;
2048acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
20493ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
20503ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2051b2f3846aSAlan Cox 			if (user_unwire)
2052b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2053b2f3846aSAlan Cox 			entry->wired_count--;
20540ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2055b2f3846aSAlan Cox 				/*
2056b2f3846aSAlan Cox 				 * Retain the map lock.
2057b2f3846aSAlan Cox 				 */
20584be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
20594be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
20604be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
2061b2f3846aSAlan Cox 			}
2062b2f3846aSAlan Cox 		}
2063acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2064acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
2065acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2066acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2067acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2068acd9a301SAlan Cox 			need_wakeup = TRUE;
2069acd9a301SAlan Cox 		}
2070acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2071acd9a301SAlan Cox 		entry = entry->next;
2072acd9a301SAlan Cox 	}
2073acd9a301SAlan Cox 	vm_map_unlock(map);
2074acd9a301SAlan Cox 	if (need_wakeup)
2075acd9a301SAlan Cox 		vm_map_wakeup(map);
2076acd9a301SAlan Cox 	return (rv);
2077acd9a301SAlan Cox }
2078acd9a301SAlan Cox 
2079acd9a301SAlan Cox /*
2080e27e17b7SAlan Cox  *	vm_map_wire:
2081e27e17b7SAlan Cox  *
2082e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2083e27e17b7SAlan Cox  */
2084e27e17b7SAlan Cox int
2085e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2086abd498aaSBruce M Simpson     int flags)
2087e27e17b7SAlan Cox {
208812d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
208912d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
209012d7cc84SAlan Cox 	unsigned int last_timestamp;
209112d7cc84SAlan Cox 	int rv;
20924be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2093e27e17b7SAlan Cox 
2094abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
209512d7cc84SAlan Cox 	vm_map_lock(map);
209612d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
209712d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2098abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2099cbef13d8SAlan Cox 			first_entry = first_entry->next;
2100abd498aaSBruce M Simpson 		else {
210112d7cc84SAlan Cox 			vm_map_unlock(map);
210212d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
210312d7cc84SAlan Cox 		}
2104abd498aaSBruce M Simpson 	}
210512d7cc84SAlan Cox 	last_timestamp = map->timestamp;
210612d7cc84SAlan Cox 	entry = first_entry;
210712d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
210812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
210912d7cc84SAlan Cox 			/*
211012d7cc84SAlan Cox 			 * We have not yet clipped the entry.
211112d7cc84SAlan Cox 			 */
211212d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
211312d7cc84SAlan Cox 			    entry->start;
211412d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
21158ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
211612d7cc84SAlan Cox 				/*
211712d7cc84SAlan Cox 				 * Allow interruption of user wiring?
211812d7cc84SAlan Cox 				 */
211912d7cc84SAlan Cox 			}
212012d7cc84SAlan Cox 			vm_map_lock(map);
212112d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
212212d7cc84SAlan Cox 				/*
212312d7cc84SAlan Cox 				 * Look again for the entry because the map was
212412d7cc84SAlan Cox 				 * modified while it was unlocked.
212512d7cc84SAlan Cox 				 * Specifically, the entry may have been
212612d7cc84SAlan Cox 				 * clipped, merged, or deleted.
212712d7cc84SAlan Cox 				 */
212812d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
212912d7cc84SAlan Cox 				    &tmp_entry)) {
2130cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2131cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2132cbef13d8SAlan Cox 					else {
213312d7cc84SAlan Cox 						if (saved_start == start) {
213412d7cc84SAlan Cox 							/*
213512d7cc84SAlan Cox 							 * first_entry has been deleted.
213612d7cc84SAlan Cox 							 */
213712d7cc84SAlan Cox 							vm_map_unlock(map);
213812d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
213912d7cc84SAlan Cox 						}
214012d7cc84SAlan Cox 						end = saved_start;
214112d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
214212d7cc84SAlan Cox 						goto done;
214312d7cc84SAlan Cox 					}
2144cbef13d8SAlan Cox 				}
214512d7cc84SAlan Cox 				if (entry == first_entry)
214612d7cc84SAlan Cox 					first_entry = tmp_entry;
214712d7cc84SAlan Cox 				else
214812d7cc84SAlan Cox 					first_entry = NULL;
214912d7cc84SAlan Cox 				entry = tmp_entry;
215012d7cc84SAlan Cox 			}
215112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
215212d7cc84SAlan Cox 			continue;
215312d7cc84SAlan Cox 		}
215412d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
215512d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
215612d7cc84SAlan Cox 		/*
215712d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
215812d7cc84SAlan Cox 		 * above.)
215912d7cc84SAlan Cox 		 */
216012d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
216112d7cc84SAlan Cox 		/*
21620ada205eSBrian Feldman 		 *
216312d7cc84SAlan Cox 		 */
21640ada205eSBrian Feldman 		if (entry->wired_count == 0) {
21650ada205eSBrian Feldman 			entry->wired_count++;
216612d7cc84SAlan Cox 			saved_start = entry->start;
216712d7cc84SAlan Cox 			saved_end = entry->end;
21684be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
21694be14af9SAlan Cox 			    entry->object.vm_object->type == OBJT_DEVICE;
217012d7cc84SAlan Cox 			/*
217112d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
217212d7cc84SAlan Cox 			 * mark.
217312d7cc84SAlan Cox 			 */
217412d7cc84SAlan Cox 			vm_map_unlock(map);
2175ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
21764be14af9SAlan Cox 			    user_wire, fictitious);
217712d7cc84SAlan Cox 			vm_map_lock(map);
217812d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
217912d7cc84SAlan Cox 				/*
218012d7cc84SAlan Cox 				 * Look again for the entry because the map was
218112d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
218212d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
218312d7cc84SAlan Cox 				 * deleted.
218412d7cc84SAlan Cox 				 */
218512d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
218612d7cc84SAlan Cox 				    &tmp_entry);
218712d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
218812d7cc84SAlan Cox 				if (entry == first_entry)
218912d7cc84SAlan Cox 					first_entry = tmp_entry;
219012d7cc84SAlan Cox 				else
219112d7cc84SAlan Cox 					first_entry = NULL;
219212d7cc84SAlan Cox 				entry = tmp_entry;
219328c58286SAlan Cox 				while (entry->end < saved_end) {
219428c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
219528c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
219628c58286SAlan Cox 						    ("vm_map_wire: bad count"));
219728c58286SAlan Cox 						entry->wired_count = -1;
219828c58286SAlan Cox 					}
219912d7cc84SAlan Cox 					entry = entry->next;
220012d7cc84SAlan Cox 				}
220128c58286SAlan Cox 			}
220212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
220312d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
220428c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
220528c58286SAlan Cox 				    ("vm_map_wire: bad count"));
220612d7cc84SAlan Cox 				/*
220728c58286SAlan Cox 				 * Assign an out-of-range value to represent
220828c58286SAlan Cox 				 * the failure to wire this entry.
220912d7cc84SAlan Cox 				 */
221028c58286SAlan Cox 				entry->wired_count = -1;
221112d7cc84SAlan Cox 				end = entry->end;
221212d7cc84SAlan Cox 				goto done;
221312d7cc84SAlan Cox 			}
22140ada205eSBrian Feldman 		} else if (!user_wire ||
22150ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
22160ada205eSBrian Feldman 			entry->wired_count++;
221712d7cc84SAlan Cox 		}
221812d7cc84SAlan Cox 		/*
221912d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2220abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
222112d7cc84SAlan Cox 		 */
2222abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2223abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2224abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
222512d7cc84SAlan Cox 			end = entry->end;
222612d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
222712d7cc84SAlan Cox 			goto done;
222812d7cc84SAlan Cox 		}
222912d7cc84SAlan Cox 		entry = entry->next;
223012d7cc84SAlan Cox 	}
223112d7cc84SAlan Cox 	rv = KERN_SUCCESS;
223212d7cc84SAlan Cox done:
223312d7cc84SAlan Cox 	need_wakeup = FALSE;
223412d7cc84SAlan Cox 	if (first_entry == NULL) {
223512d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2236cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2237cbef13d8SAlan Cox 			first_entry = first_entry->next;
2238cbef13d8SAlan Cox 		else
223912d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
224012d7cc84SAlan Cox 	}
224112d7cc84SAlan Cox 	entry = first_entry;
224212d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
224312d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
224412d7cc84SAlan Cox 			if (user_wire)
224512d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
224628c58286SAlan Cox 		} else if (entry->wired_count == -1) {
224728c58286SAlan Cox 			/*
224828c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
224928c58286SAlan Cox 			 * unnecessary.
225028c58286SAlan Cox 			 */
225128c58286SAlan Cox 			entry->wired_count = 0;
225212d7cc84SAlan Cox 		} else {
22530ada205eSBrian Feldman 			if (!user_wire ||
22540ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
225512d7cc84SAlan Cox 				entry->wired_count--;
22560ada205eSBrian Feldman 			if (entry->wired_count == 0) {
225712d7cc84SAlan Cox 				/*
225812d7cc84SAlan Cox 				 * Retain the map lock.
225912d7cc84SAlan Cox 				 */
22604be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
22614be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
22624be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
226312d7cc84SAlan Cox 			}
226412d7cc84SAlan Cox 		}
226512d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
226612d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
226712d7cc84SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
226812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
226912d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
227012d7cc84SAlan Cox 			need_wakeup = TRUE;
227112d7cc84SAlan Cox 		}
227212d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
227312d7cc84SAlan Cox 		entry = entry->next;
227412d7cc84SAlan Cox 	}
227512d7cc84SAlan Cox 	vm_map_unlock(map);
227612d7cc84SAlan Cox 	if (need_wakeup)
227712d7cc84SAlan Cox 		vm_map_wakeup(map);
227812d7cc84SAlan Cox 	return (rv);
2279e27e17b7SAlan Cox }
2280e27e17b7SAlan Cox 
2281e27e17b7SAlan Cox /*
2282950f8459SAlan Cox  * vm_map_sync
2283df8bae1dSRodney W. Grimes  *
2284df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2285df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2286df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2287df8bae1dSRodney W. Grimes  *
2288637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2289637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2290637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2291637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2292637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2293637315edSAlan Cox  * flushing the current region containing start.
2294637315edSAlan Cox  *
2295df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2296df8bae1dSRodney W. Grimes  */
2297df8bae1dSRodney W. Grimes int
2298950f8459SAlan Cox vm_map_sync(
22991b40f8c0SMatthew Dillon 	vm_map_t map,
23001b40f8c0SMatthew Dillon 	vm_offset_t start,
23011b40f8c0SMatthew Dillon 	vm_offset_t end,
23021b40f8c0SMatthew Dillon 	boolean_t syncio,
23031b40f8c0SMatthew Dillon 	boolean_t invalidate)
2304df8bae1dSRodney W. Grimes {
2305c0877f10SJohn Dyson 	vm_map_entry_t current;
2306df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2307df8bae1dSRodney W. Grimes 	vm_size_t size;
2308df8bae1dSRodney W. Grimes 	vm_object_t object;
2309a316d390SJohn Dyson 	vm_ooffset_t offset;
2310e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2311df8bae1dSRodney W. Grimes 
2312df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2313df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2314df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2315df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2316df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2317637315edSAlan Cox 	} else if (start == end) {
2318637315edSAlan Cox 		start = entry->start;
2319637315edSAlan Cox 		end = entry->end;
2320df8bae1dSRodney W. Grimes 	}
2321df8bae1dSRodney W. Grimes 	/*
2322b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2323df8bae1dSRodney W. Grimes 	 */
23247b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
23257b0e72d1SAlan Cox 	    current = current->next) {
2326b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2327df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2328df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2329df8bae1dSRodney W. Grimes 		}
2330df8bae1dSRodney W. Grimes 		if (end > current->end &&
2331df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2332df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2333df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2334df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2335df8bae1dSRodney W. Grimes 		}
2336df8bae1dSRodney W. Grimes 	}
2337df8bae1dSRodney W. Grimes 
23382cf13952SAlan Cox 	if (invalidate)
2339bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
23402cf13952SAlan Cox 
2341df8bae1dSRodney W. Grimes 	/*
2342df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2343df8bae1dSRodney W. Grimes 	 * objects as we go.
2344df8bae1dSRodney W. Grimes 	 */
2345e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2346df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2347df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
23489fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2349c0877f10SJohn Dyson 			vm_map_t smap;
2350df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2351df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2352df8bae1dSRodney W. Grimes 
23539fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2354df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2355df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2356df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2357df8bae1dSRodney W. Grimes 			if (tsize < size)
2358df8bae1dSRodney W. Grimes 				size = tsize;
2359df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2360df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2361df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2362df8bae1dSRodney W. Grimes 		} else {
2363df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2364df8bae1dSRodney W. Grimes 		}
2365e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2366e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2367e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2368950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2369df8bae1dSRodney W. Grimes 		start += size;
2370e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2371e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2372e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2373e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2374e53fa61bSKonstantin Belousov 			current = current->next;
2375df8bae1dSRodney W. Grimes 	}
2376df8bae1dSRodney W. Grimes 
2377df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2378df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2379df8bae1dSRodney W. Grimes }
2380df8bae1dSRodney W. Grimes 
2381df8bae1dSRodney W. Grimes /*
2382df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2383df8bae1dSRodney W. Grimes  *
2384df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2385df8bae1dSRodney W. Grimes  *
2386df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2387df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2388df8bae1dSRodney W. Grimes  */
23890362d7d7SJohn Dyson static void
23901b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2391df8bae1dSRodney W. Grimes {
23924be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
23934be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
23944be14af9SAlan Cox 	    entry->object.vm_object->type == OBJT_DEVICE);
2395df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2396df8bae1dSRodney W. Grimes }
2397df8bae1dSRodney W. Grimes 
2398897d81a0SKonstantin Belousov void
2399897d81a0SKonstantin Belousov vm_map_entry_free_freelist(vm_map_t map, vm_map_entry_t freelist)
2400897d81a0SKonstantin Belousov {
2401897d81a0SKonstantin Belousov 	vm_map_entry_t e;
2402897d81a0SKonstantin Belousov 	vm_object_t object;
2403897d81a0SKonstantin Belousov 
2404897d81a0SKonstantin Belousov 	while (freelist != NULL) {
2405897d81a0SKonstantin Belousov 		e = freelist;
2406897d81a0SKonstantin Belousov 		freelist = freelist->next;
2407897d81a0SKonstantin Belousov 		if ((e->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
2408897d81a0SKonstantin Belousov 			object = e->object.vm_object;
2409897d81a0SKonstantin Belousov 			vm_object_deallocate(object);
2410897d81a0SKonstantin Belousov 		}
2411897d81a0SKonstantin Belousov 		vm_map_entry_dispose(map, e);
2412897d81a0SKonstantin Belousov 	}
2413897d81a0SKonstantin Belousov }
2414897d81a0SKonstantin Belousov 
2415df8bae1dSRodney W. Grimes /*
2416df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2417df8bae1dSRodney W. Grimes  *
2418df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2419df8bae1dSRodney W. Grimes  */
24200362d7d7SJohn Dyson static void
24211b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2422df8bae1dSRodney W. Grimes {
242332a89c32SAlan Cox 	vm_object_t object;
242432a89c32SAlan Cox 	vm_pindex_t offidxstart, offidxend, count;
242532a89c32SAlan Cox 
2426df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
2427df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
2428df8bae1dSRodney W. Grimes 
242932a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
243032a89c32SAlan Cox 	    (object = entry->object.vm_object) != NULL) {
243132a89c32SAlan Cox 		count = OFF_TO_IDX(entry->end - entry->start);
243232a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
243332a89c32SAlan Cox 		offidxend = offidxstart + count;
243432a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
243532a89c32SAlan Cox 		if (object->ref_count != 1 &&
243632a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
24379f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
243832a89c32SAlan Cox 			vm_object_collapse(object);
2439b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
244032a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
244132a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
244232a89c32SAlan Cox 			if (offidxend >= object->size &&
244332a89c32SAlan Cox 			    offidxstart < object->size)
244432a89c32SAlan Cox 				object->size = offidxstart;
244532a89c32SAlan Cox 		}
244632a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
2447897d81a0SKonstantin Belousov 	} else
2448897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
2449df8bae1dSRodney W. Grimes }
2450df8bae1dSRodney W. Grimes 
2451df8bae1dSRodney W. Grimes /*
2452df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2453df8bae1dSRodney W. Grimes  *
2454df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2455df8bae1dSRodney W. Grimes  *	map.
2456df8bae1dSRodney W. Grimes  */
2457df8bae1dSRodney W. Grimes int
2458897d81a0SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end,
2459897d81a0SKonstantin Belousov     vm_map_entry_t *freelist)
2460df8bae1dSRodney W. Grimes {
2461c0877f10SJohn Dyson 	vm_map_entry_t entry;
2462df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2463df8bae1dSRodney W. Grimes 
2464df8bae1dSRodney W. Grimes 	/*
2465df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2466df8bae1dSRodney W. Grimes 	 */
2467876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2468df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2469876318ecSAlan Cox 	else {
2470df8bae1dSRodney W. Grimes 		entry = first_entry;
2471df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2472df8bae1dSRodney W. Grimes 	}
2473df8bae1dSRodney W. Grimes 
2474df8bae1dSRodney W. Grimes 	/*
2475df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2476df8bae1dSRodney W. Grimes 	 */
2477df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2478df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2479df8bae1dSRodney W. Grimes 
248073b2baceSAlan Cox 		/*
248173b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
24827c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
24837c938963SBrian Feldman 		 * user maps.
248473b2baceSAlan Cox 		 */
24857c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
24867c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
24877c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
248873b2baceSAlan Cox 			unsigned int last_timestamp;
248973b2baceSAlan Cox 			vm_offset_t saved_start;
249073b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
249173b2baceSAlan Cox 
249273b2baceSAlan Cox 			saved_start = entry->start;
249373b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
249473b2baceSAlan Cox 			last_timestamp = map->timestamp;
24958ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
249673b2baceSAlan Cox 			vm_map_lock(map);
249773b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
249873b2baceSAlan Cox 				/*
249973b2baceSAlan Cox 				 * Look again for the entry because the map was
250073b2baceSAlan Cox 				 * modified while it was unlocked.
250173b2baceSAlan Cox 				 * Specifically, the entry may have been
250273b2baceSAlan Cox 				 * clipped, merged, or deleted.
250373b2baceSAlan Cox 				 */
250473b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
250573b2baceSAlan Cox 							 &tmp_entry))
250673b2baceSAlan Cox 					entry = tmp_entry->next;
250773b2baceSAlan Cox 				else {
250873b2baceSAlan Cox 					entry = tmp_entry;
250973b2baceSAlan Cox 					vm_map_clip_start(map, entry,
251073b2baceSAlan Cox 							  saved_start);
251173b2baceSAlan Cox 				}
251273b2baceSAlan Cox 			}
251373b2baceSAlan Cox 			continue;
251473b2baceSAlan Cox 		}
2515df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2516df8bae1dSRodney W. Grimes 
2517c0877f10SJohn Dyson 		next = entry->next;
2518df8bae1dSRodney W. Grimes 
2519df8bae1dSRodney W. Grimes 		/*
25200d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
25210d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2522df8bae1dSRodney W. Grimes 		 */
2523c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2524df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2525c0877f10SJohn Dyson 		}
2526df8bae1dSRodney W. Grimes 
252732a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2528df8bae1dSRodney W. Grimes 
2529df8bae1dSRodney W. Grimes 		/*
2530e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2531e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2532e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2533e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2534df8bae1dSRodney W. Grimes 		 */
2535df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2536897d81a0SKonstantin Belousov 		entry->next = *freelist;
2537897d81a0SKonstantin Belousov 		*freelist = entry;
2538df8bae1dSRodney W. Grimes 		entry = next;
2539df8bae1dSRodney W. Grimes 	}
2540df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2541df8bae1dSRodney W. Grimes }
2542df8bae1dSRodney W. Grimes 
2543df8bae1dSRodney W. Grimes /*
2544df8bae1dSRodney W. Grimes  *	vm_map_remove:
2545df8bae1dSRodney W. Grimes  *
2546df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2547df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2548df8bae1dSRodney W. Grimes  */
2549df8bae1dSRodney W. Grimes int
25501b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2551df8bae1dSRodney W. Grimes {
2552897d81a0SKonstantin Belousov 	vm_map_entry_t freelist;
25536eaee3feSAlan Cox 	int result;
2554df8bae1dSRodney W. Grimes 
2555897d81a0SKonstantin Belousov 	freelist = NULL;
2556df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2557df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2558897d81a0SKonstantin Belousov 	result = vm_map_delete(map, start, end, &freelist);
2559df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2560897d81a0SKonstantin Belousov 	vm_map_entry_free_freelist(map, freelist);
2561df8bae1dSRodney W. Grimes 	return (result);
2562df8bae1dSRodney W. Grimes }
2563df8bae1dSRodney W. Grimes 
2564df8bae1dSRodney W. Grimes /*
2565df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2566df8bae1dSRodney W. Grimes  *
25672d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
25682d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
25692d5c7e45SMatthew Dillon  *
25702d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
25712d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
25722d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
25732d5c7e45SMatthew Dillon  *
25742d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2575d8834602SAlan Cox  *
2576d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2577df8bae1dSRodney W. Grimes  */
25780d94caffSDavid Greenman boolean_t
2579b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2580b9dcd593SBruce Evans 			vm_prot_t protection)
2581df8bae1dSRodney W. Grimes {
2582c0877f10SJohn Dyson 	vm_map_entry_t entry;
2583df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2584df8bae1dSRodney W. Grimes 
2585d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2586df8bae1dSRodney W. Grimes 		return (FALSE);
2587df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2588df8bae1dSRodney W. Grimes 
2589df8bae1dSRodney W. Grimes 	while (start < end) {
2590d8834602SAlan Cox 		if (entry == &map->header)
2591df8bae1dSRodney W. Grimes 			return (FALSE);
2592df8bae1dSRodney W. Grimes 		/*
2593df8bae1dSRodney W. Grimes 		 * No holes allowed!
2594df8bae1dSRodney W. Grimes 		 */
2595d8834602SAlan Cox 		if (start < entry->start)
2596df8bae1dSRodney W. Grimes 			return (FALSE);
2597df8bae1dSRodney W. Grimes 		/*
2598df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2599df8bae1dSRodney W. Grimes 		 */
2600d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2601df8bae1dSRodney W. Grimes 			return (FALSE);
2602df8bae1dSRodney W. Grimes 		/* go to next entry */
2603df8bae1dSRodney W. Grimes 		start = entry->end;
2604df8bae1dSRodney W. Grimes 		entry = entry->next;
2605df8bae1dSRodney W. Grimes 	}
2606df8bae1dSRodney W. Grimes 	return (TRUE);
2607df8bae1dSRodney W. Grimes }
2608df8bae1dSRodney W. Grimes 
260986524867SJohn Dyson /*
2610df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2611df8bae1dSRodney W. Grimes  *
2612df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2613df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2614df8bae1dSRodney W. Grimes  */
2615f708ef1bSPoul-Henning Kamp static void
26161b40f8c0SMatthew Dillon vm_map_copy_entry(
26171b40f8c0SMatthew Dillon 	vm_map_t src_map,
26181b40f8c0SMatthew Dillon 	vm_map_t dst_map,
26191b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
26201b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2621df8bae1dSRodney W. Grimes {
2622c0877f10SJohn Dyson 	vm_object_t src_object;
2623c0877f10SJohn Dyson 
26249fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2625df8bae1dSRodney W. Grimes 		return;
2626df8bae1dSRodney W. Grimes 
2627df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2628df8bae1dSRodney W. Grimes 
2629df8bae1dSRodney W. Grimes 		/*
26300d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
26310d94caffSDavid Greenman 		 * write-protected.
2632df8bae1dSRodney W. Grimes 		 */
2633afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2634df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2635df8bae1dSRodney W. Grimes 			    src_entry->start,
2636df8bae1dSRodney W. Grimes 			    src_entry->end,
2637df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2638df8bae1dSRodney W. Grimes 		}
2639b18bfc3dSJohn Dyson 
2640df8bae1dSRodney W. Grimes 		/*
2641df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2642df8bae1dSRodney W. Grimes 		 */
26438aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2644a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
2645c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2646c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2647c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2648c0877f10SJohn Dyson 				vm_object_collapse(src_object);
264996fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2650c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2651c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2652a89c6258SAlan Cox 				}
2653a89c6258SAlan Cox 			}
2654b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2655069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2656e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2657c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2658afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2659afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2660b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2661b18bfc3dSJohn Dyson 		} else {
2662b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2663b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2664b18bfc3dSJohn Dyson 		}
2665df8bae1dSRodney W. Grimes 
2666df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2667df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
26680d94caffSDavid Greenman 	} else {
2669df8bae1dSRodney W. Grimes 		/*
2670df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
26710d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
26720d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2673df8bae1dSRodney W. Grimes 		 */
2674df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2675df8bae1dSRodney W. Grimes 	}
2676df8bae1dSRodney W. Grimes }
2677df8bae1dSRodney W. Grimes 
2678df8bae1dSRodney W. Grimes /*
26792a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
26802a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
26812a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
26822a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
26832a7be1b6SBrian Feldman  */
26842a7be1b6SBrian Feldman static void
26852a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
26862a7be1b6SBrian Feldman     vm_map_entry_t entry)
26872a7be1b6SBrian Feldman {
26882a7be1b6SBrian Feldman 	vm_size_t entrysize;
26892a7be1b6SBrian Feldman 	vm_offset_t newend;
26902a7be1b6SBrian Feldman 
26912a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
26922a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
26932a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
26942a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
26952a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
26962a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2697b351299cSAndrew Gallatin 		newend = MIN(entry->end,
26982a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
26992a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
27002a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
27012a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2702b351299cSAndrew Gallatin 		newend = MIN(entry->end,
27032a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
27042a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
27052a7be1b6SBrian Feldman 	}
27062a7be1b6SBrian Feldman }
27072a7be1b6SBrian Feldman 
27082a7be1b6SBrian Feldman /*
2709df8bae1dSRodney W. Grimes  * vmspace_fork:
2710df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2711df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2712df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2713df8bae1dSRodney W. Grimes  * values on the regions in that map.
2714df8bae1dSRodney W. Grimes  *
27152a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
27162a7be1b6SBrian Feldman  *
2717df8bae1dSRodney W. Grimes  * The source map must not be locked.
2718df8bae1dSRodney W. Grimes  */
2719df8bae1dSRodney W. Grimes struct vmspace *
27201b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2721df8bae1dSRodney W. Grimes {
2722c0877f10SJohn Dyson 	struct vmspace *vm2;
2723df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2724df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2725df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2726df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2727de5f6a77SJohn Dyson 	vm_object_t object;
27281fac7d7fSKonstantin Belousov 	int locked;
2729df8bae1dSRodney W. Grimes 
2730df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2731df8bae1dSRodney W. Grimes 
27322d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
273389b57fcfSKonstantin Belousov 	if (vm2 == NULL)
273489b57fcfSKonstantin Belousov 		goto unlock_and_return;
27352a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
27362a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
27372a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2738df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
27391fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
27401fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
274147221757SJohn Dyson 	new_map->timestamp = 1;
2742df8bae1dSRodney W. Grimes 
2743df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2744df8bae1dSRodney W. Grimes 
2745df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2746afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2747df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2748df8bae1dSRodney W. Grimes 
2749df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2750df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2751df8bae1dSRodney W. Grimes 			break;
2752df8bae1dSRodney W. Grimes 
2753df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2754df8bae1dSRodney W. Grimes 			/*
2755fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2756fed9a903SJohn Dyson 			 */
2757fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2758fed9a903SJohn Dyson 			if (object == NULL) {
2759fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2760c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2761fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
276215d2d313SAlan Cox 				old_entry->offset = 0;
27639a2f6362SAlan Cox 			}
27649a2f6362SAlan Cox 
27659a2f6362SAlan Cox 			/*
27669a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
27679a2f6362SAlan Cox 			 * to insure that a shadow object is created.
27689a2f6362SAlan Cox 			 */
27699a2f6362SAlan Cox 			vm_object_reference(object);
27709a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
27715069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
27725069bf57SJohn Dyson 					&old_entry->offset,
2773c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
27745069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2775d30344bdSIan Dowse 				/* Transfer the second reference too. */
2776d30344bdSIan Dowse 				vm_object_reference(
2777d30344bdSIan Dowse 				    old_entry->object.vm_object);
27787fd10fb3SKonstantin Belousov 
27797fd10fb3SKonstantin Belousov 				/*
27807fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
2781b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
27827fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
27837fd10fb3SKonstantin Belousov 				 */
2784d30344bdSIan Dowse 				vm_object_deallocate(object);
27855069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2786fed9a903SJohn Dyson 			}
2787e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2788069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2789e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2790fed9a903SJohn Dyson 
2791fed9a903SJohn Dyson 			/*
2792ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2793df8bae1dSRodney W. Grimes 			 */
2794df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2795df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
27969f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
27979f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
2798df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2799df8bae1dSRodney W. Grimes 
2800df8bae1dSRodney W. Grimes 			/*
28010d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
28020d94caffSDavid Greenman 			 * inserting at the end of the new map.
2803df8bae1dSRodney W. Grimes 			 */
2804df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2805df8bae1dSRodney W. Grimes 			    new_entry);
28062a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2807df8bae1dSRodney W. Grimes 
2808df8bae1dSRodney W. Grimes 			/*
2809df8bae1dSRodney W. Grimes 			 * Update the physical map
2810df8bae1dSRodney W. Grimes 			 */
2811df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2812df8bae1dSRodney W. Grimes 			    new_entry->start,
2813df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2814df8bae1dSRodney W. Grimes 			    old_entry->start);
2815df8bae1dSRodney W. Grimes 			break;
2816df8bae1dSRodney W. Grimes 
2817df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2818df8bae1dSRodney W. Grimes 			/*
2819df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2820df8bae1dSRodney W. Grimes 			 */
2821df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2822df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
28239f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
28249f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
2825df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2826df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2827df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2828df8bae1dSRodney W. Grimes 			    new_entry);
28292a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2830bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2831bd7e5f99SJohn Dyson 			    new_entry);
2832df8bae1dSRodney W. Grimes 			break;
2833df8bae1dSRodney W. Grimes 		}
2834df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2835df8bae1dSRodney W. Grimes 	}
283689b57fcfSKonstantin Belousov unlock_and_return:
2837df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
28381fac7d7fSKonstantin Belousov 	if (vm2 != NULL)
28391fac7d7fSKonstantin Belousov 		vm_map_unlock(new_map);
2840df8bae1dSRodney W. Grimes 
2841df8bae1dSRodney W. Grimes 	return (vm2);
2842df8bae1dSRodney W. Grimes }
2843df8bae1dSRodney W. Grimes 
284494f7e29aSAlan Cox int
284594f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
284694f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
284794f7e29aSAlan Cox {
2848fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2849fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
285094f7e29aSAlan Cox 	vm_size_t init_ssize;
2851fd75d710SMarcel Moolenaar 	int orient, rv;
285291d5354aSJohn Baldwin 	rlim_t vmemlim;
285394f7e29aSAlan Cox 
2854fd75d710SMarcel Moolenaar 	/*
2855fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2856fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2857fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2858fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2859fd75d710SMarcel Moolenaar 	 */
2860fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2861fd75d710SMarcel Moolenaar 	cow &= ~orient;
2862fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2863fd75d710SMarcel Moolenaar 
286477bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
286577bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
286677bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
286794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2868fd75d710SMarcel Moolenaar 
2869fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
287094f7e29aSAlan Cox 
287191d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
287291d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
287391d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
287491d5354aSJohn Baldwin 
287594f7e29aSAlan Cox 	vm_map_lock(map);
287694f7e29aSAlan Cox 
287794f7e29aSAlan Cox 	/* If addr is already mapped, no go */
287894f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
287994f7e29aSAlan Cox 		vm_map_unlock(map);
288094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
288194f7e29aSAlan Cox 	}
288294f7e29aSAlan Cox 
2883a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
288491d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2885a69ac174SMatthew Dillon 		vm_map_unlock(map);
2886a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2887a69ac174SMatthew Dillon 	}
2888a69ac174SMatthew Dillon 
2889fd75d710SMarcel Moolenaar 	/*
2890fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2891fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2892fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2893fd75d710SMarcel Moolenaar 	 * space is not protected.
289494f7e29aSAlan Cox 	 *
2895fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2896fd75d710SMarcel Moolenaar 	 * grow the stack.
289794f7e29aSAlan Cox 	 */
289894f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
289994f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
290094f7e29aSAlan Cox 		vm_map_unlock(map);
290194f7e29aSAlan Cox 		return (KERN_NO_SPACE);
290294f7e29aSAlan Cox 	}
290394f7e29aSAlan Cox 
2904fd75d710SMarcel Moolenaar 	/*
2905fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2906fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2907fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2908fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
290994f7e29aSAlan Cox 	 *
2910fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2911fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2912fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
291394f7e29aSAlan Cox 	 */
2914fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2915fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2916fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2917fd75d710SMarcel Moolenaar 		bot = addrbos;
2918fd75d710SMarcel Moolenaar 	else
2919fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2920fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2921fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
292294f7e29aSAlan Cox 
2923fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
292494f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
292529b45e9eSAlan Cox 		if (prev_entry != &map->header)
2926fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2927fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2928fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
292994f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2930b21a0008SMarcel Moolenaar 
2931fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2932fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2933fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2934fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2935fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
293694f7e29aSAlan Cox 	}
293794f7e29aSAlan Cox 
293894f7e29aSAlan Cox 	vm_map_unlock(map);
293994f7e29aSAlan Cox 	return (rv);
294094f7e29aSAlan Cox }
294194f7e29aSAlan Cox 
294294f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
294394f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
294494f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
294594f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
294694f7e29aSAlan Cox  * the grow function in vm_machdep.c).
294794f7e29aSAlan Cox  */
294894f7e29aSAlan Cox int
294994f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
295094f7e29aSAlan Cox {
2951b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2952b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
295394f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
295494f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
295594f7e29aSAlan Cox 	vm_offset_t end;
2956b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
295791d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2958b21a0008SMarcel Moolenaar 	int is_procstack, rv;
295923955314SAlfred Perlstein 
296094f7e29aSAlan Cox Retry:
296191d5354aSJohn Baldwin 	PROC_LOCK(p);
296291d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2963bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
296491d5354aSJohn Baldwin 	PROC_UNLOCK(p);
296591d5354aSJohn Baldwin 
296694f7e29aSAlan Cox 	vm_map_lock_read(map);
296794f7e29aSAlan Cox 
296894f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
296994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
297094f7e29aSAlan Cox 		vm_map_unlock_read(map);
29710cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
297294f7e29aSAlan Cox 	}
297394f7e29aSAlan Cox 
2974b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2975b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2976b21a0008SMarcel Moolenaar 		/*
2977b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2978b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2979b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2980b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
298194f7e29aSAlan Cox 		 */
2982b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
298394f7e29aSAlan Cox 			vm_map_unlock_read(map);
29840cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
298594f7e29aSAlan Cox 		}
2986b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2987b21a0008SMarcel Moolenaar 	} else {
2988b21a0008SMarcel Moolenaar 		/*
2989b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2990b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2991b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2992b21a0008SMarcel Moolenaar 		 * select one.
2993b21a0008SMarcel Moolenaar 		 */
2994b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2995b21a0008SMarcel Moolenaar 			/*
2996b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2997b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2998b21a0008SMarcel Moolenaar 			 */
2999b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3000b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3001b21a0008SMarcel Moolenaar 			else
3002b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3003b21a0008SMarcel Moolenaar 		} else
3004b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3005b21a0008SMarcel Moolenaar 	}
300694f7e29aSAlan Cox 
3007b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3008b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3009b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3010b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3011b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
301294f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3013b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3014b21a0008SMarcel Moolenaar 	} else {
3015b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
301608667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3017b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3018b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3019fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3020b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3021b21a0008SMarcel Moolenaar 	}
3022b21a0008SMarcel Moolenaar 
302394f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
302494f7e29aSAlan Cox 		vm_map_unlock_read(map);
30250cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
302694f7e29aSAlan Cox 	}
302794f7e29aSAlan Cox 
3028b21a0008SMarcel Moolenaar 	/*
3029b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3030b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3031b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3032b21a0008SMarcel Moolenaar 	 * and is probably an error.
303394f7e29aSAlan Cox 	 *
3034b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3035b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
303694f7e29aSAlan Cox 	 */
3037b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
303825adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
303994f7e29aSAlan Cox 			goto Retry;
304094f7e29aSAlan Cox 
3041b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
304294f7e29aSAlan Cox 
304394f7e29aSAlan Cox 		vm_map_unlock(map);
30440cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
304594f7e29aSAlan Cox 	}
304694f7e29aSAlan Cox 
3047b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
304894f7e29aSAlan Cox 
3049b21a0008SMarcel Moolenaar 	/*
3050b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3051b21a0008SMarcel Moolenaar 	 * limit.
305294f7e29aSAlan Cox 	 */
305391d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
305494f7e29aSAlan Cox 		vm_map_unlock_read(map);
30550cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
305694f7e29aSAlan Cox 	}
305794f7e29aSAlan Cox 
305894f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
3059cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
3060b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
306194f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
306291d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
306391d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
306494f7e29aSAlan Cox 	}
306594f7e29aSAlan Cox 
3066a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
306791d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3068a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
3069a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3070a69ac174SMatthew Dillon 	}
3071a69ac174SMatthew Dillon 
307225adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
307394f7e29aSAlan Cox 		goto Retry;
307494f7e29aSAlan Cox 
3075b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3076b21a0008SMarcel Moolenaar 		/*
3077b21a0008SMarcel Moolenaar 		 * Growing downward.
3078b21a0008SMarcel Moolenaar 		 */
307994f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
308094f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
308194f7e29aSAlan Cox 
3082b21a0008SMarcel Moolenaar 		/*
3083b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3084b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
308594f7e29aSAlan Cox 		 */
308694f7e29aSAlan Cox 		if (addr < end) {
3087b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
308894f7e29aSAlan Cox 			addr = end;
308994f7e29aSAlan Cox 		}
309094f7e29aSAlan Cox 
309194f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
309205ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
309394f7e29aSAlan Cox 
309494f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
309594f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
309629b45e9eSAlan Cox 			if (prev_entry != &map->header)
309729b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3098b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3099b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3100b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3101b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3102b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3103b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3104b21a0008SMarcel Moolenaar 			    grow_amount;
3105b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3106b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
310794f7e29aSAlan Cox 		}
3108b21a0008SMarcel Moolenaar 	} else {
3109b21a0008SMarcel Moolenaar 		/*
3110b21a0008SMarcel Moolenaar 		 * Growing upward.
3111b21a0008SMarcel Moolenaar 		 */
3112b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3113b21a0008SMarcel Moolenaar 
3114b21a0008SMarcel Moolenaar 		/*
3115b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3116b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3117b21a0008SMarcel Moolenaar 		 */
3118b21a0008SMarcel Moolenaar 		if (addr > end) {
3119b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3120b21a0008SMarcel Moolenaar 			addr = end;
312194f7e29aSAlan Cox 		}
312294f7e29aSAlan Cox 
3123b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
3124b21a0008SMarcel Moolenaar 
3125b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
3126b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
3127b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
312857a21abaSAlan Cox 		    stack_entry->offset,
3129b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
3130b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
313108667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3132b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3133b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3134199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
31350164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3136b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3137b21a0008SMarcel Moolenaar 
3138b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3139b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3140b21a0008SMarcel Moolenaar 		} else
3141b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3142b21a0008SMarcel Moolenaar 	}
3143b21a0008SMarcel Moolenaar 
3144b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3145b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3146b21a0008SMarcel Moolenaar 
314794f7e29aSAlan Cox 	vm_map_unlock(map);
3148b21a0008SMarcel Moolenaar 
3149abd498aaSBruce M Simpson 	/*
3150abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3151abd498aaSBruce M Simpson 	 */
3152b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3153b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3154b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3155b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3156b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3157b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3158b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3159b21a0008SMarcel Moolenaar 	}
3160abd498aaSBruce M Simpson 
31610cddd8f0SMatthew Dillon 	return (rv);
316294f7e29aSAlan Cox }
316394f7e29aSAlan Cox 
3164df8bae1dSRodney W. Grimes /*
31655856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
31665856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
31675856e12eSJohn Dyson  */
316889b57fcfSKonstantin Belousov int
31693ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
31701b40f8c0SMatthew Dillon {
31715856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
31725856e12eSJohn Dyson 	struct vmspace *newvmspace;
31735856e12eSJohn Dyson 
31743ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
317589b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
317689b57fcfSKonstantin Belousov 		return (ENOMEM);
317751ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
31785856e12eSJohn Dyson 	/*
31795856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
31805856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
31815856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
31825856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
31835856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
31845856e12eSJohn Dyson 	 */
318557051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
31865856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
318757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
31886617724cSJeff Roberson 	if (p == curthread->td_proc)
3189b40ce416SJulian Elischer 		pmap_activate(curthread);
3190b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
319189b57fcfSKonstantin Belousov 	return (0);
31925856e12eSJohn Dyson }
31935856e12eSJohn Dyson 
31945856e12eSJohn Dyson /*
31955856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
31965856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
31975856e12eSJohn Dyson  */
319889b57fcfSKonstantin Belousov int
31991b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
32001b40f8c0SMatthew Dillon {
32015856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
32025856e12eSJohn Dyson 	struct vmspace *newvmspace;
32035856e12eSJohn Dyson 
32045856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
320589b57fcfSKonstantin Belousov 		return (0);
32065856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
320789b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
320889b57fcfSKonstantin Belousov 		return (ENOMEM);
320957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
32105856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
321157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
32126617724cSJeff Roberson 	if (p == curthread->td_proc)
3213b40ce416SJulian Elischer 		pmap_activate(curthread);
3214b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
321589b57fcfSKonstantin Belousov 	return (0);
32165856e12eSJohn Dyson }
32175856e12eSJohn Dyson 
32185856e12eSJohn Dyson /*
3219df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3220df8bae1dSRodney W. Grimes  *
3221df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3222df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3223df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3224df8bae1dSRodney W. Grimes  *	type specified.
3225df8bae1dSRodney W. Grimes  *
3226df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3227df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3228df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3229df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3230df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3231df8bae1dSRodney W. Grimes  *
3232df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3233df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3234df8bae1dSRodney W. Grimes  *
3235df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3236df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3237df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3238df8bae1dSRodney W. Grimes  *	remain the same.
3239df8bae1dSRodney W. Grimes  */
3240df8bae1dSRodney W. Grimes int
3241b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3242b9dcd593SBruce Evans 	      vm_offset_t vaddr,
324347221757SJohn Dyson 	      vm_prot_t fault_typea,
3244b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3245b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3246b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3247b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
32482d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3249df8bae1dSRodney W. Grimes {
3250c0877f10SJohn Dyson 	vm_map_entry_t entry;
3251c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3252c0877f10SJohn Dyson 	vm_prot_t prot;
325347221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
3254df8bae1dSRodney W. Grimes 
3255df8bae1dSRodney W. Grimes RetryLookup:;
3256df8bae1dSRodney W. Grimes 
3257df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3258df8bae1dSRodney W. Grimes 
3259df8bae1dSRodney W. Grimes 	/*
32604c3ef59eSAlan Cox 	 * Lookup the faulting address.
3261df8bae1dSRodney W. Grimes 	 */
3262095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3263095104acSAlan Cox 		vm_map_unlock_read(map);
3264095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3265095104acSAlan Cox 	}
3266df8bae1dSRodney W. Grimes 
32674e94f402SAlan Cox 	entry = *out_entry;
3268b7b2aac2SJohn Dyson 
3269df8bae1dSRodney W. Grimes 	/*
3270df8bae1dSRodney W. Grimes 	 * Handle submaps.
3271df8bae1dSRodney W. Grimes 	 */
3272afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3273df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3274df8bae1dSRodney W. Grimes 
3275df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3276df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3277df8bae1dSRodney W. Grimes 		goto RetryLookup;
3278df8bae1dSRodney W. Grimes 	}
3279a04c970aSJohn Dyson 
3280df8bae1dSRodney W. Grimes 	/*
32810d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3282a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
3283a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
3284a04c970aSJohn Dyson 	 * COW for debuggers.
3285df8bae1dSRodney W. Grimes 	 */
3286480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3287480ba2f5SJohn Dyson 		prot = entry->max_protection;
3288480ba2f5SJohn Dyson 	else
3289df8bae1dSRodney W. Grimes 		prot = entry->protection;
329047221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
329147221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
3292095104acSAlan Cox 		vm_map_unlock_read(map);
3293095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
329447221757SJohn Dyson 	}
32952ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
329647221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
32972ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
329847221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3299095104acSAlan Cox 		vm_map_unlock_read(map);
3300095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3301a04c970aSJohn Dyson 	}
3302df8bae1dSRodney W. Grimes 
3303df8bae1dSRodney W. Grimes 	/*
33040d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
33050d94caffSDavid Greenman 	 * accesses.
3306df8bae1dSRodney W. Grimes 	 */
330705f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
330805f0fdd2SPoul-Henning Kamp 	if (*wired)
3309df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
3310df8bae1dSRodney W. Grimes 
3311df8bae1dSRodney W. Grimes 	/*
3312df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3313df8bae1dSRodney W. Grimes 	 */
3314afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3315df8bae1dSRodney W. Grimes 		/*
33160d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3317ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3318df8bae1dSRodney W. Grimes 		 *
33190d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
33200d94caffSDavid Greenman 		 * permissions allowed.
3321df8bae1dSRodney W. Grimes 		 */
3322df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
3323df8bae1dSRodney W. Grimes 			/*
33240d94caffSDavid Greenman 			 * Make a new object, and place it in the object
33250d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3326ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
33270d94caffSDavid Greenman 			 * object.
3328df8bae1dSRodney W. Grimes 			 */
332925adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3330df8bae1dSRodney W. Grimes 				goto RetryLookup;
33319917e010SAlan Cox 
3332df8bae1dSRodney W. Grimes 			vm_object_shadow(
3333df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3334df8bae1dSRodney W. Grimes 			    &entry->offset,
3335c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
3336afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
33379917e010SAlan Cox 
33389b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
33390d94caffSDavid Greenman 		} else {
3340df8bae1dSRodney W. Grimes 			/*
33410d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
33420d94caffSDavid Greenman 			 * don't allow writes.
3343df8bae1dSRodney W. Grimes 			 */
33442d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3345df8bae1dSRodney W. Grimes 		}
3346df8bae1dSRodney W. Grimes 	}
33472d8acc0fSJohn Dyson 
3348df8bae1dSRodney W. Grimes 	/*
3349df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3350df8bae1dSRodney W. Grimes 	 */
33514e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
33524e71e795SMatthew Dillon 	    !map->system_map) {
335325adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3354df8bae1dSRodney W. Grimes 			goto RetryLookup;
335524a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3356c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
3357df8bae1dSRodney W. Grimes 		entry->offset = 0;
33589b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3359df8bae1dSRodney W. Grimes 	}
3360b5b40fa6SJohn Dyson 
3361df8bae1dSRodney W. Grimes 	/*
33620d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
33630d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3364df8bae1dSRodney W. Grimes 	 */
33659b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3366df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3367df8bae1dSRodney W. Grimes 
3368df8bae1dSRodney W. Grimes 	*out_prot = prot;
3369df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3370df8bae1dSRodney W. Grimes }
3371df8bae1dSRodney W. Grimes 
3372df8bae1dSRodney W. Grimes /*
337319dc5607STor Egge  *	vm_map_lookup_locked:
337419dc5607STor Egge  *
337519dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
337619dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
337719dc5607STor Egge  */
337819dc5607STor Egge int
337919dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
338019dc5607STor Egge 		     vm_offset_t vaddr,
338119dc5607STor Egge 		     vm_prot_t fault_typea,
338219dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
338319dc5607STor Egge 		     vm_object_t *object,	/* OUT */
338419dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
338519dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
338619dc5607STor Egge 		     boolean_t *wired)		/* OUT */
338719dc5607STor Egge {
338819dc5607STor Egge 	vm_map_entry_t entry;
338919dc5607STor Egge 	vm_map_t map = *var_map;
339019dc5607STor Egge 	vm_prot_t prot;
339119dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
339219dc5607STor Egge 
339319dc5607STor Egge 	/*
33944c3ef59eSAlan Cox 	 * Lookup the faulting address.
339519dc5607STor Egge 	 */
339619dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
339719dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
339819dc5607STor Egge 
339919dc5607STor Egge 	entry = *out_entry;
340019dc5607STor Egge 
340119dc5607STor Egge 	/*
340219dc5607STor Egge 	 * Fail if the entry refers to a submap.
340319dc5607STor Egge 	 */
340419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
340519dc5607STor Egge 		return (KERN_FAILURE);
340619dc5607STor Egge 
340719dc5607STor Egge 	/*
340819dc5607STor Egge 	 * Check whether this task is allowed to have this page.
340919dc5607STor Egge 	 * Note the special case for MAP_ENTRY_COW
341019dc5607STor Egge 	 * pages with an override.  This is to implement a forced
341119dc5607STor Egge 	 * COW for debuggers.
341219dc5607STor Egge 	 */
341319dc5607STor Egge 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
341419dc5607STor Egge 		prot = entry->max_protection;
341519dc5607STor Egge 	else
341619dc5607STor Egge 		prot = entry->protection;
341719dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
341819dc5607STor Egge 	if ((fault_type & prot) != fault_type)
341919dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
342019dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
342119dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
342219dc5607STor Egge 	    (fault_type & VM_PROT_WRITE) &&
342319dc5607STor Egge 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
342419dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
342519dc5607STor Egge 
342619dc5607STor Egge 	/*
342719dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
342819dc5607STor Egge 	 * accesses.
342919dc5607STor Egge 	 */
343019dc5607STor Egge 	*wired = (entry->wired_count != 0);
343119dc5607STor Egge 	if (*wired)
343219dc5607STor Egge 		prot = fault_type = entry->protection;
343319dc5607STor Egge 
343419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
343519dc5607STor Egge 		/*
343619dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
343719dc5607STor Egge 		 */
343819dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
343919dc5607STor Egge 			return (KERN_FAILURE);
344019dc5607STor Egge 		/*
344119dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
344219dc5607STor Egge 		 * don't allow writes.
344319dc5607STor Egge 		 */
344419dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
344519dc5607STor Egge 	}
344619dc5607STor Egge 
344719dc5607STor Egge 	/*
344819dc5607STor Egge 	 * Fail if an object should be created.
344919dc5607STor Egge 	 */
345019dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
345119dc5607STor Egge 		return (KERN_FAILURE);
345219dc5607STor Egge 
345319dc5607STor Egge 	/*
345419dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
345519dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
345619dc5607STor Egge 	 */
345719dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
345819dc5607STor Egge 	*object = entry->object.vm_object;
345919dc5607STor Egge 
346019dc5607STor Egge 	*out_prot = prot;
346119dc5607STor Egge 	return (KERN_SUCCESS);
346219dc5607STor Egge }
346319dc5607STor Egge 
346419dc5607STor Egge /*
3465df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3466df8bae1dSRodney W. Grimes  *
3467df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3468df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3469df8bae1dSRodney W. Grimes  */
34700d94caffSDavid Greenman void
34711b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3472df8bae1dSRodney W. Grimes {
3473df8bae1dSRodney W. Grimes 	/*
3474df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3475df8bae1dSRodney W. Grimes 	 */
3476df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3477df8bae1dSRodney W. Grimes }
3478df8bae1dSRodney W. Grimes 
3479c7c34a24SBruce Evans #include "opt_ddb.h"
3480c3cb3e12SDavid Greenman #ifdef DDB
3481c7c34a24SBruce Evans #include <sys/kernel.h>
3482c7c34a24SBruce Evans 
3483c7c34a24SBruce Evans #include <ddb/ddb.h>
3484c7c34a24SBruce Evans 
3485df8bae1dSRodney W. Grimes /*
3486df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3487df8bae1dSRodney W. Grimes  */
3488c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3489df8bae1dSRodney W. Grimes {
349095e5e988SJohn Dyson 	static int nlines;
3491c7c34a24SBruce Evans 	/* XXX convert args. */
3492c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3493c7c34a24SBruce Evans 	boolean_t full = have_addr;
3494df8bae1dSRodney W. Grimes 
3495c0877f10SJohn Dyson 	vm_map_entry_t entry;
3496c7c34a24SBruce Evans 
3497e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3498e5f251d2SAlan Cox 	    (void *)map,
3499101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
350095e5e988SJohn Dyson 	nlines++;
3501df8bae1dSRodney W. Grimes 
3502c7c34a24SBruce Evans 	if (!full && db_indent)
3503df8bae1dSRodney W. Grimes 		return;
3504df8bae1dSRodney W. Grimes 
3505c7c34a24SBruce Evans 	db_indent += 2;
3506df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3507df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3508fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3509fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
351095e5e988SJohn Dyson 		nlines++;
3511e5f251d2SAlan Cox 		{
3512df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3513df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
35140d94caffSDavid Greenman 
351595e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3516df8bae1dSRodney W. Grimes 			    entry->protection,
3517df8bae1dSRodney W. Grimes 			    entry->max_protection,
35188aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3519df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
352095e5e988SJohn Dyson 				db_printf(", wired");
3521df8bae1dSRodney W. Grimes 		}
35229fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3523cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
35249fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3525cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
352695e5e988SJohn Dyson 			nlines++;
3527df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
35289fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
35299fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3530c7c34a24SBruce Evans 				db_indent += 2;
3531101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
35329fdfe602SMatthew Dillon 					     entry->object.sub_map,
3533914181e7SBruce Evans 					     full, 0, (char *)0);
3534c7c34a24SBruce Evans 				db_indent -= 2;
3535df8bae1dSRodney W. Grimes 			}
35360d94caffSDavid Greenman 		} else {
3537cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3538101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3539cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3540afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3541c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3542afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3543c7c34a24SBruce Evans 			db_printf("\n");
354495e5e988SJohn Dyson 			nlines++;
3545df8bae1dSRodney W. Grimes 
3546df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3547df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3548df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3549c7c34a24SBruce Evans 				db_indent += 2;
3550101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3551101eeb7fSBruce Evans 						entry->object.vm_object,
3552914181e7SBruce Evans 						full, 0, (char *)0);
355395e5e988SJohn Dyson 				nlines += 4;
3554c7c34a24SBruce Evans 				db_indent -= 2;
3555df8bae1dSRodney W. Grimes 			}
3556df8bae1dSRodney W. Grimes 		}
3557df8bae1dSRodney W. Grimes 	}
3558c7c34a24SBruce Evans 	db_indent -= 2;
355995e5e988SJohn Dyson 	if (db_indent == 0)
356095e5e988SJohn Dyson 		nlines = 0;
3561df8bae1dSRodney W. Grimes }
356295e5e988SJohn Dyson 
356395e5e988SJohn Dyson 
356495e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
356595e5e988SJohn Dyson {
356695e5e988SJohn Dyson 	struct proc *p;
356795e5e988SJohn Dyson 
356895e5e988SJohn Dyson 	if (have_addr) {
356995e5e988SJohn Dyson 		p = (struct proc *) addr;
357095e5e988SJohn Dyson 	} else {
357195e5e988SJohn Dyson 		p = curproc;
357295e5e988SJohn Dyson 	}
357395e5e988SJohn Dyson 
3574ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3575ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3576b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
357795e5e988SJohn Dyson 
3578101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
357995e5e988SJohn Dyson }
358095e5e988SJohn Dyson 
3581c7c34a24SBruce Evans #endif /* DDB */
3582