xref: /freebsd/sys/vm/vm_map.c (revision 57051fdc4bed0ddad7caeaddfe44829678aae0e4)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
7061d80e90SJohn Baldwin #include <sys/ktr.h>
71fb919e4dSMark Murray #include <sys/lock.h>
72fb919e4dSMark Murray #include <sys/mutex.h>
73b5e8ce9fSBruce Evans #include <sys/proc.h>
74efeaf95aSDavid Greenman #include <sys/vmmeter.h>
75867a482dSJohn Dyson #include <sys/mman.h>
761efb74fbSJohn Dyson #include <sys/vnode.h>
772267af78SJulian Elischer #include <sys/resourcevar.h>
783fde38dfSMike Silbersack #include <sys/file.h>
7905ba50f5SJake Burkholder #include <sys/sysent.h>
803db161e0SMatthew Dillon #include <sys/shm.h>
81df8bae1dSRodney W. Grimes 
82df8bae1dSRodney W. Grimes #include <vm/vm.h>
83efeaf95aSDavid Greenman #include <vm/vm_param.h>
84efeaf95aSDavid Greenman #include <vm/pmap.h>
85efeaf95aSDavid Greenman #include <vm/vm_map.h>
86df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
87df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
8847221757SJohn Dyson #include <vm/vm_pager.h>
8926f9a767SRodney W. Grimes #include <vm/vm_kern.h>
90efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9121cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
92670d17b5SJeff Roberson #include <vm/uma.h>
93df8bae1dSRodney W. Grimes 
94df8bae1dSRodney W. Grimes /*
95df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
96df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
97df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
98df8bae1dSRodney W. Grimes  *	memory from one map to another.
99df8bae1dSRodney W. Grimes  *
100df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
101df8bae1dSRodney W. Grimes  *
102df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
103df8bae1dSRodney W. Grimes  *	entries; a single hint is used to speed up lookups.
104df8bae1dSRodney W. Grimes  *
105956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
106df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
107df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
108df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
109df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
110df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
111df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
112df8bae1dSRodney W. Grimes  *
113df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
114ad5fca3bSAlan Cox  *	by copying VM object references from one map to
115df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
116df8bae1dSRodney W. Grimes  */
117df8bae1dSRodney W. Grimes 
118df8bae1dSRodney W. Grimes /*
119df8bae1dSRodney W. Grimes  *	vm_map_startup:
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
122df8bae1dSRodney W. Grimes  *	any other vm_map routines.
123df8bae1dSRodney W. Grimes  *
124df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
125df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
126df8bae1dSRodney W. Grimes  *
127df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
128df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
129df8bae1dSRodney W. Grimes  *
130df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
131df8bae1dSRodney W. Grimes  *	maps and requires map entries.
132df8bae1dSRodney W. Grimes  */
133df8bae1dSRodney W. Grimes 
1343a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1358355f576SJeff Roberson static uma_zone_t mapentzone;
1368355f576SJeff Roberson static uma_zone_t kmapentzone;
1378355f576SJeff Roberson static uma_zone_t mapzone;
1388355f576SJeff Roberson static uma_zone_t vmspace_zone;
1398355f576SJeff Roberson static struct vm_object kmapentobj;
140b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1418355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
142b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1438355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
1448355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
1451fc43fd1SAlan Cox 
1468355f576SJeff Roberson #ifdef INVARIANTS
1478355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1488355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1498355f576SJeff Roberson #endif
150b18bfc3dSJohn Dyson 
15157051fdcSTor Egge /*
15257051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15357051fdcSTor Egge  * stable.
15457051fdcSTor Egge  */
15557051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15657051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15757051fdcSTor Egge 
1580d94caffSDavid Greenman void
1591b40f8c0SMatthew Dillon vm_map_startup(void)
160df8bae1dSRodney W. Grimes {
1613a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1628355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1638355f576SJeff Roberson #ifdef INVARIANTS
1648355f576SJeff Roberson 	    vm_map_zdtor,
1658355f576SJeff Roberson #else
1668355f576SJeff Roberson 	    NULL,
1678355f576SJeff Roberson #endif
1688355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1698355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
170670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
17118aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
17218aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
173670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
174670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
175670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
176df8bae1dSRodney W. Grimes }
177df8bae1dSRodney W. Grimes 
1788355f576SJeff Roberson static void
1798355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1808355f576SJeff Roberson {
1818355f576SJeff Roberson 	struct vmspace *vm;
1828355f576SJeff Roberson 
1838355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1843f25cbddSPeter Wemm 	pmap_release(vmspace_pmap(vm));
1858355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
1868355f576SJeff Roberson }
1878355f576SJeff Roberson 
188b23f72e9SBrian Feldman static int
189b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
1908355f576SJeff Roberson {
1918355f576SJeff Roberson 	struct vmspace *vm;
1928355f576SJeff Roberson 
1938355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1948355f576SJeff Roberson 
195b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
1963f25cbddSPeter Wemm 	pmap_pinit(vmspace_pmap(vm));
197b23f72e9SBrian Feldman 	return (0);
1988355f576SJeff Roberson }
1998355f576SJeff Roberson 
2008355f576SJeff Roberson static void
2018355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2028355f576SJeff Roberson {
2038355f576SJeff Roberson 	vm_map_t map;
2048355f576SJeff Roberson 
2058355f576SJeff Roberson 	map = (vm_map_t)mem;
20636daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
20712c64974SMaxime Henrion 	sx_destroy(&map->lock);
2088355f576SJeff Roberson }
2098355f576SJeff Roberson 
210b23f72e9SBrian Feldman static int
211b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2128355f576SJeff Roberson {
2138355f576SJeff Roberson 	vm_map_t map;
2148355f576SJeff Roberson 
2158355f576SJeff Roberson 	map = (vm_map_t)mem;
2168355f576SJeff Roberson 	map->nentries = 0;
2178355f576SJeff Roberson 	map->size = 0;
218d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
21912c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
220b23f72e9SBrian Feldman 	return (0);
2218355f576SJeff Roberson }
2228355f576SJeff Roberson 
2238355f576SJeff Roberson #ifdef INVARIANTS
2248355f576SJeff Roberson static void
2258355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2268355f576SJeff Roberson {
2278355f576SJeff Roberson 	struct vmspace *vm;
2288355f576SJeff Roberson 
2298355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2308355f576SJeff Roberson 
2318355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2328355f576SJeff Roberson }
2338355f576SJeff Roberson static void
2348355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2358355f576SJeff Roberson {
2368355f576SJeff Roberson 	vm_map_t map;
2378355f576SJeff Roberson 
2388355f576SJeff Roberson 	map = (vm_map_t)mem;
2398355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2408355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2418355f576SJeff Roberson 	    map, map->nentries));
2428355f576SJeff Roberson 	KASSERT(map->size == 0,
2438355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2449eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2458355f576SJeff Roberson }
2468355f576SJeff Roberson #endif	/* INVARIANTS */
2478355f576SJeff Roberson 
248df8bae1dSRodney W. Grimes /*
249df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
250df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
251df8bae1dSRodney W. Grimes  */
252df8bae1dSRodney W. Grimes struct vmspace *
2532d8acc0fSJohn Dyson vmspace_alloc(min, max)
254df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
255df8bae1dSRodney W. Grimes {
256c0877f10SJohn Dyson 	struct vmspace *vm;
2570d94caffSDavid Greenman 
258a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
25921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
2608355f576SJeff Roberson 	_vm_map_init(&vm->vm_map, min, max);
261b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
262df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2632d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
26451ab6c28SAlan Cox 	vm->vm_swrss = 0;
26551ab6c28SAlan Cox 	vm->vm_tsize = 0;
26651ab6c28SAlan Cox 	vm->vm_dsize = 0;
26751ab6c28SAlan Cox 	vm->vm_ssize = 0;
26851ab6c28SAlan Cox 	vm->vm_taddr = 0;
26951ab6c28SAlan Cox 	vm->vm_daddr = 0;
27051ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
271df8bae1dSRodney W. Grimes 	return (vm);
272df8bae1dSRodney W. Grimes }
273df8bae1dSRodney W. Grimes 
274df8bae1dSRodney W. Grimes void
2751b40f8c0SMatthew Dillon vm_init2(void)
2761b40f8c0SMatthew Dillon {
2779e7c1bceSPeter Wemm 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
2783fde38dfSMike Silbersack 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
2793fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
2808355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2818355f576SJeff Roberson #ifdef INVARIANTS
2828355f576SJeff Roberson 	    vmspace_zdtor,
2838355f576SJeff Roberson #else
2848355f576SJeff Roberson 	    NULL,
2858355f576SJeff Roberson #endif
2868355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2873075778bSJohn Dyson }
2883075778bSJohn Dyson 
28962a59e8fSWarner Losh static inline void
290582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
291df8bae1dSRodney W. Grimes {
29221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
2933db161e0SMatthew Dillon 
2943db161e0SMatthew Dillon 	/*
2953db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
2963db161e0SMatthew Dillon 	 * exit1().
2973db161e0SMatthew Dillon 	 */
2983db161e0SMatthew Dillon 	shmexit(vm);
2993db161e0SMatthew Dillon 
30030dcfc09SJohn Dyson 	/*
301df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3020d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3030d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
304df8bae1dSRodney W. Grimes 	 */
305717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
306df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3078355f576SJeff Roberson 
3088355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
309df8bae1dSRodney W. Grimes }
310582ec34cSAlfred Perlstein 
311582ec34cSAlfred Perlstein void
312582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
313582ec34cSAlfred Perlstein {
3141a276a3fSAlan Cox 	int refcnt;
315582ec34cSAlfred Perlstein 
316582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
317582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
318582ec34cSAlfred Perlstein 
3191a276a3fSAlan Cox 	do
3201a276a3fSAlan Cox 		refcnt = vm->vm_refcnt;
3211a276a3fSAlan Cox 	while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
32257051fdcSTor Egge 	if (refcnt == 1)
323582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
324582ec34cSAlfred Perlstein }
325582ec34cSAlfred Perlstein 
326582ec34cSAlfred Perlstein void
327582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
328582ec34cSAlfred Perlstein {
329334f7061SPeter Wemm 	struct vmspace *vm;
330582ec34cSAlfred Perlstein 
33157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
332334f7061SPeter Wemm 	vm = p->p_vmspace;
333334f7061SPeter Wemm 	p->p_vmspace = NULL;
33457051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
33557051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
33657051fdcSTor Egge 	vmspace_free(vm);
33757051fdcSTor Egge }
33857051fdcSTor Egge 
33957051fdcSTor Egge void
34057051fdcSTor Egge vmspace_exit(struct thread *td)
34157051fdcSTor Egge {
34257051fdcSTor Egge 	int refcnt;
34357051fdcSTor Egge 	struct vmspace *vm;
34457051fdcSTor Egge 	struct proc *p;
345389d2b6eSMatthew Dillon 
346389d2b6eSMatthew Dillon 	/*
34757051fdcSTor Egge 	 * Release user portion of address space.
34857051fdcSTor Egge 	 * This releases references to vnodes,
34957051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
35057051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
351389d2b6eSMatthew Dillon 	 *
35257051fdcSTor Egge 	 * The last exiting process to reach this point releases as
35357051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
35457051fdcSTor Egge 	 * slower fallback in case another process had a temporary
35557051fdcSTor Egge 	 * reference to the vmspace.
356389d2b6eSMatthew Dillon 	 */
35757051fdcSTor Egge 
35857051fdcSTor Egge 	p = td->td_proc;
35957051fdcSTor Egge 	vm = p->p_vmspace;
36057051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
36157051fdcSTor Egge 	do {
36257051fdcSTor Egge 		refcnt = vm->vm_refcnt;
36357051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
36457051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
36557051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
36657051fdcSTor Egge 			p->p_vmspace = &vmspace0;
36757051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
36857051fdcSTor Egge 			pmap_activate(td);
36957051fdcSTor Egge 		}
37057051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
37157051fdcSTor Egge 	if (refcnt == 1) {
37257051fdcSTor Egge 		if (p->p_vmspace != vm) {
37357051fdcSTor Egge 			/* vmspace not yet freed, switch back */
37457051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
37557051fdcSTor Egge 			p->p_vmspace = vm;
37657051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
37757051fdcSTor Egge 			pmap_activate(td);
37857051fdcSTor Egge 		}
37957051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
38057051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
38157051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
38257051fdcSTor Egge 		p->p_vmspace = &vmspace0;
38357051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
38457051fdcSTor Egge 		pmap_activate(td);
385334f7061SPeter Wemm 		vmspace_dofree(vm);
386334f7061SPeter Wemm 	}
38757051fdcSTor Egge }
38857051fdcSTor Egge 
38957051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
39057051fdcSTor Egge 
39157051fdcSTor Egge struct vmspace *
39257051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
39357051fdcSTor Egge {
39457051fdcSTor Egge 	struct vmspace *vm;
39557051fdcSTor Egge 	int refcnt;
39657051fdcSTor Egge 
39757051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
39857051fdcSTor Egge 	vm = p->p_vmspace;
39957051fdcSTor Egge 	if (vm == NULL) {
40057051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
40157051fdcSTor Egge 		return (NULL);
40257051fdcSTor Egge 	}
40357051fdcSTor Egge 	do {
40457051fdcSTor Egge 		refcnt = vm->vm_refcnt;
40557051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
40657051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40757051fdcSTor Egge 			return (NULL);
40857051fdcSTor Egge 		}
40957051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
41057051fdcSTor Egge 	if (vm != p->p_vmspace) {
41157051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41257051fdcSTor Egge 		vmspace_free(vm);
41357051fdcSTor Egge 		return (NULL);
41457051fdcSTor Egge 	}
41557051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
41657051fdcSTor Egge 	return (vm);
41757051fdcSTor Egge }
418df8bae1dSRodney W. Grimes 
4191b40f8c0SMatthew Dillon void
420780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4211b40f8c0SMatthew Dillon {
422bc91c510SAlan Cox 
42393bc4879SAlan Cox 	if (map->system_map)
42436daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
42512c64974SMaxime Henrion 	else
42612c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
4271b40f8c0SMatthew Dillon 	map->timestamp++;
4281b40f8c0SMatthew Dillon }
4291b40f8c0SMatthew Dillon 
4301b40f8c0SMatthew Dillon void
431780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
4320e0af8ecSBrian Feldman {
433bc91c510SAlan Cox 
43436daaecdSAlan Cox 	if (map->system_map)
43536daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
43636daaecdSAlan Cox 	else
43712c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
4380e0af8ecSBrian Feldman }
4390e0af8ecSBrian Feldman 
4400e0af8ecSBrian Feldman void
441780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
4420e0af8ecSBrian Feldman {
443bc91c510SAlan Cox 
44493bc4879SAlan Cox 	if (map->system_map)
44536daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
44612c64974SMaxime Henrion 	else
44712c64974SMaxime Henrion 		_sx_xlock(&map->lock, file, line);
44836daaecdSAlan Cox }
4490e0af8ecSBrian Feldman 
4500e0af8ecSBrian Feldman void
451780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
4520e0af8ecSBrian Feldman {
453bc91c510SAlan Cox 
45436daaecdSAlan Cox 	if (map->system_map)
45536daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
45636daaecdSAlan Cox 	else
45712c64974SMaxime Henrion 		_sx_xunlock(&map->lock, file, line);
45825adb370SBrian Feldman }
45925adb370SBrian Feldman 
460d974f03cSAlan Cox int
461780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
462d974f03cSAlan Cox {
46325adb370SBrian Feldman 	int error;
46425adb370SBrian Feldman 
46536daaecdSAlan Cox 	error = map->system_map ?
46636daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
46712c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
4683a92e5d5SAlan Cox 	if (error == 0)
4693a92e5d5SAlan Cox 		map->timestamp++;
470bc91c510SAlan Cox 	return (error == 0);
4710e0af8ecSBrian Feldman }
4720e0af8ecSBrian Feldman 
4730e0af8ecSBrian Feldman int
47472d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
47572d97679SDavid Schultz {
47672d97679SDavid Schultz 	int error;
47772d97679SDavid Schultz 
47872d97679SDavid Schultz 	error = map->system_map ?
47972d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
48012c64974SMaxime Henrion 	    !_sx_try_xlock(&map->lock, file, line);
48172d97679SDavid Schultz 	return (error == 0);
48272d97679SDavid Schultz }
48372d97679SDavid Schultz 
48472d97679SDavid Schultz int
485780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
4860e0af8ecSBrian Feldman {
487bc91c510SAlan Cox 
48836daaecdSAlan Cox #ifdef INVARIANTS
48912c64974SMaxime Henrion 	if (map->system_map) {
49036daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
49136daaecdSAlan Cox 	} else
49212c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
49312c64974SMaxime Henrion #endif
494bc91c510SAlan Cox 	map->timestamp++;
495bc91c510SAlan Cox 	return (0);
4960e0af8ecSBrian Feldman }
4970e0af8ecSBrian Feldman 
4980e0af8ecSBrian Feldman void
499780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5001b40f8c0SMatthew Dillon {
501bc91c510SAlan Cox 
50236daaecdSAlan Cox #ifdef INVARIANTS
50312c64974SMaxime Henrion 	if (map->system_map) {
50436daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
50536daaecdSAlan Cox 	} else
50612c64974SMaxime Henrion 		_sx_assert(&map->lock, SX_XLOCKED, file, line);
50712c64974SMaxime Henrion #endif
50825adb370SBrian Feldman }
50925adb370SBrian Feldman 
510acd9a301SAlan Cox /*
511acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
512acd9a301SAlan Cox  */
5139688f931SAlan Cox int
514acd9a301SAlan Cox vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
515acd9a301SAlan Cox {
516acd9a301SAlan Cox 
5173a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
518acd9a301SAlan Cox 	vm_map_unlock(map);
5193a92e5d5SAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
520acd9a301SAlan Cox }
521acd9a301SAlan Cox 
522acd9a301SAlan Cox /*
523acd9a301SAlan Cox  *	vm_map_wakeup:
524acd9a301SAlan Cox  */
5259688f931SAlan Cox void
526acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
527acd9a301SAlan Cox {
528acd9a301SAlan Cox 
529b49ecb86SAlan Cox 	/*
5303a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
5313a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
5323a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
533b49ecb86SAlan Cox 	 */
5343a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
5353a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
536acd9a301SAlan Cox 	wakeup(&map->root);
537acd9a301SAlan Cox }
538acd9a301SAlan Cox 
5391b40f8c0SMatthew Dillon long
5401b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
5411b40f8c0SMatthew Dillon {
5421b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
5431b40f8c0SMatthew Dillon }
5441b40f8c0SMatthew Dillon 
5452bc7dd56SBruce M Simpson long
5462bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
5472bc7dd56SBruce M Simpson {
5482bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
5492bc7dd56SBruce M Simpson }
5502bc7dd56SBruce M Simpson 
551ff2b5645SMatthew Dillon /*
552df8bae1dSRodney W. Grimes  *	vm_map_create:
553df8bae1dSRodney W. Grimes  *
554df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
555df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
556df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
557df8bae1dSRodney W. Grimes  */
5580d94caffSDavid Greenman vm_map_t
5591b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
560df8bae1dSRodney W. Grimes {
561c0877f10SJohn Dyson 	vm_map_t result;
562df8bae1dSRodney W. Grimes 
563a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
56421c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
5658355f576SJeff Roberson 	_vm_map_init(result, min, max);
566df8bae1dSRodney W. Grimes 	result->pmap = pmap;
567df8bae1dSRodney W. Grimes 	return (result);
568df8bae1dSRodney W. Grimes }
569df8bae1dSRodney W. Grimes 
570df8bae1dSRodney W. Grimes /*
571df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
572df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
573df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
574df8bae1dSRodney W. Grimes  */
5758355f576SJeff Roberson static void
5768355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
577df8bae1dSRodney W. Grimes {
57821c641b2SJohn Baldwin 
579df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
5809688f931SAlan Cox 	map->needs_wakeup = FALSE;
5813075778bSJohn Dyson 	map->system_map = 0;
582df8bae1dSRodney W. Grimes 	map->min_offset = min;
583df8bae1dSRodney W. Grimes 	map->max_offset = max;
584af7cd0c5SBrian Feldman 	map->flags = 0;
5854e94f402SAlan Cox 	map->root = NULL;
586df8bae1dSRodney W. Grimes 	map->timestamp = 0;
587df8bae1dSRodney W. Grimes }
588df8bae1dSRodney W. Grimes 
589a18b1f1dSJason Evans void
5908355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
591a18b1f1dSJason Evans {
5928355f576SJeff Roberson 	_vm_map_init(map, min, max);
593d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
59412c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
595a18b1f1dSJason Evans }
596a18b1f1dSJason Evans 
597df8bae1dSRodney W. Grimes /*
598b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
599b18bfc3dSJohn Dyson  *
600b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
601b18bfc3dSJohn Dyson  */
60262487bb4SJohn Dyson static void
6031b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
604b18bfc3dSJohn Dyson {
6052b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
606b18bfc3dSJohn Dyson }
607b18bfc3dSJohn Dyson 
608b18bfc3dSJohn Dyson /*
609df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
610df8bae1dSRodney W. Grimes  *
611df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
612b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
613df8bae1dSRodney W. Grimes  */
614f708ef1bSPoul-Henning Kamp static vm_map_entry_t
6151b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
616df8bae1dSRodney W. Grimes {
6171f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
6181f6889a1SMatthew Dillon 
6192b4a2c27SAlan Cox 	if (map->system_map)
6202b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
6212b4a2c27SAlan Cox 	else
622a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
6231f6889a1SMatthew Dillon 	if (new_entry == NULL)
6241f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
6251f6889a1SMatthew Dillon 	return (new_entry);
626df8bae1dSRodney W. Grimes }
627df8bae1dSRodney W. Grimes 
628df8bae1dSRodney W. Grimes /*
629794316a8SAlan Cox  *	vm_map_entry_set_behavior:
630794316a8SAlan Cox  *
631794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
632794316a8SAlan Cox  *	sequential.
633794316a8SAlan Cox  */
63462a59e8fSWarner Losh static inline void
635794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
636794316a8SAlan Cox {
637794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
638794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
639794316a8SAlan Cox }
640794316a8SAlan Cox 
641794316a8SAlan Cox /*
6420164e057SAlan Cox  *	vm_map_entry_set_max_free:
6430164e057SAlan Cox  *
6440164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
6450164e057SAlan Cox  */
64662a59e8fSWarner Losh static inline void
6470164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
6480164e057SAlan Cox {
6490164e057SAlan Cox 
6500164e057SAlan Cox 	entry->max_free = entry->adj_free;
6510164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
6520164e057SAlan Cox 		entry->max_free = entry->left->max_free;
6530164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
6540164e057SAlan Cox 		entry->max_free = entry->right->max_free;
6550164e057SAlan Cox }
6560164e057SAlan Cox 
6570164e057SAlan Cox /*
6584e94f402SAlan Cox  *	vm_map_entry_splay:
6594e94f402SAlan Cox  *
6600164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
6610164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
6620164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
6630164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
6640164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
6650164e057SAlan Cox  *	amortized.
6660164e057SAlan Cox  *
6670164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
6680164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
6690164e057SAlan Cox  *
6700164e057SAlan Cox  *	The map must be locked, and leaves it so.
6710164e057SAlan Cox  *
6720164e057SAlan Cox  *	Returns: the new root.
6734e94f402SAlan Cox  */
6744e94f402SAlan Cox static vm_map_entry_t
6750164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
6764e94f402SAlan Cox {
6770164e057SAlan Cox 	vm_map_entry_t llist, rlist;
6780164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
6790164e057SAlan Cox 	vm_map_entry_t y;
6804e94f402SAlan Cox 
6810164e057SAlan Cox 	/* Special case of empty tree. */
6824e94f402SAlan Cox 	if (root == NULL)
6834e94f402SAlan Cox 		return (root);
6840164e057SAlan Cox 
6850164e057SAlan Cox 	/*
6860164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
6870164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
6880164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
6890164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
6900164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
6910164e057SAlan Cox 	 * the two spines.
6920164e057SAlan Cox 	 */
6930164e057SAlan Cox 	llist = NULL;
6940164e057SAlan Cox 	rlist = NULL;
6950164e057SAlan Cox 	for (;;) {
6960164e057SAlan Cox 		/* root is never NULL in here. */
6970164e057SAlan Cox 		if (addr < root->start) {
6980164e057SAlan Cox 			y = root->left;
6990164e057SAlan Cox 			if (y == NULL)
7004e94f402SAlan Cox 				break;
7010164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
7020164e057SAlan Cox 				/* Rotate right and put y on rlist. */
7034e94f402SAlan Cox 				root->left = y->right;
7044e94f402SAlan Cox 				y->right = root;
7050164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7060164e057SAlan Cox 				root = y->left;
7070164e057SAlan Cox 				y->left = rlist;
7080164e057SAlan Cox 				rlist = y;
7090164e057SAlan Cox 			} else {
7100164e057SAlan Cox 				/* Put root on rlist. */
7110164e057SAlan Cox 				root->left = rlist;
7120164e057SAlan Cox 				rlist = root;
7134e94f402SAlan Cox 				root = y;
7144e94f402SAlan Cox 			}
7150164e057SAlan Cox 		} else {
7160164e057SAlan Cox 			y = root->right;
7170164e057SAlan Cox 			if (addr < root->end || y == NULL)
7184e94f402SAlan Cox 				break;
7190164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
7200164e057SAlan Cox 				/* Rotate left and put y on llist. */
7214e94f402SAlan Cox 				root->right = y->left;
7224e94f402SAlan Cox 				y->left = root;
7230164e057SAlan Cox 				vm_map_entry_set_max_free(root);
7240164e057SAlan Cox 				root = y->right;
7250164e057SAlan Cox 				y->right = llist;
7260164e057SAlan Cox 				llist = y;
7270164e057SAlan Cox 			} else {
7280164e057SAlan Cox 				/* Put root on llist. */
7290164e057SAlan Cox 				root->right = llist;
7300164e057SAlan Cox 				llist = root;
7314e94f402SAlan Cox 				root = y;
7324e94f402SAlan Cox 			}
7334e94f402SAlan Cox 		}
7340164e057SAlan Cox 	}
7350164e057SAlan Cox 
7360164e057SAlan Cox 	/*
7370164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
7380164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
7390164e057SAlan Cox 	 * bottom of llist and rlist.
7400164e057SAlan Cox 	 */
7410164e057SAlan Cox 	ltree = root->left;
7420164e057SAlan Cox 	while (llist != NULL) {
7430164e057SAlan Cox 		y = llist->right;
7440164e057SAlan Cox 		llist->right = ltree;
7450164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
7460164e057SAlan Cox 		ltree = llist;
7470164e057SAlan Cox 		llist = y;
7480164e057SAlan Cox 	}
7490164e057SAlan Cox 	rtree = root->right;
7500164e057SAlan Cox 	while (rlist != NULL) {
7510164e057SAlan Cox 		y = rlist->left;
7520164e057SAlan Cox 		rlist->left = rtree;
7530164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
7540164e057SAlan Cox 		rtree = rlist;
7550164e057SAlan Cox 		rlist = y;
7560164e057SAlan Cox 	}
7570164e057SAlan Cox 
7580164e057SAlan Cox 	/*
7590164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
7600164e057SAlan Cox 	 */
7610164e057SAlan Cox 	root->left = ltree;
7620164e057SAlan Cox 	root->right = rtree;
7630164e057SAlan Cox 	vm_map_entry_set_max_free(root);
7640164e057SAlan Cox 
7654e94f402SAlan Cox 	return (root);
7664e94f402SAlan Cox }
7674e94f402SAlan Cox 
7684e94f402SAlan Cox /*
769df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
770df8bae1dSRodney W. Grimes  *
771df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
772df8bae1dSRodney W. Grimes  */
7734e94f402SAlan Cox static void
77499c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
77599c81ca9SAlan Cox 		  vm_map_entry_t after_where,
77699c81ca9SAlan Cox 		  vm_map_entry_t entry)
77799c81ca9SAlan Cox {
77821c641b2SJohn Baldwin 
77921c641b2SJohn Baldwin 	CTR4(KTR_VM,
78021c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
78121c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
78299c81ca9SAlan Cox 	map->nentries++;
78399c81ca9SAlan Cox 	entry->prev = after_where;
78499c81ca9SAlan Cox 	entry->next = after_where->next;
78599c81ca9SAlan Cox 	entry->next->prev = entry;
78699c81ca9SAlan Cox 	after_where->next = entry;
7874e94f402SAlan Cox 
7884e94f402SAlan Cox 	if (after_where != &map->header) {
7894e94f402SAlan Cox 		if (after_where != map->root)
7904e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
7914e94f402SAlan Cox 		entry->right = after_where->right;
7924e94f402SAlan Cox 		entry->left = after_where;
7934e94f402SAlan Cox 		after_where->right = NULL;
7940164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
7950164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
7964e94f402SAlan Cox 	} else {
7974e94f402SAlan Cox 		entry->right = map->root;
7984e94f402SAlan Cox 		entry->left = NULL;
7994e94f402SAlan Cox 	}
8000164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
8010164e057SAlan Cox 	    entry->next->start) - entry->end;
8020164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
8034e94f402SAlan Cox 	map->root = entry;
804df8bae1dSRodney W. Grimes }
80599c81ca9SAlan Cox 
8064e94f402SAlan Cox static void
80799c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
80899c81ca9SAlan Cox 		    vm_map_entry_t entry)
80999c81ca9SAlan Cox {
8104e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
81199c81ca9SAlan Cox 
8124e94f402SAlan Cox 	if (entry != map->root)
8134e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
8144e94f402SAlan Cox 	if (entry->left == NULL)
8154e94f402SAlan Cox 		root = entry->right;
8164e94f402SAlan Cox 	else {
8174e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
8184e94f402SAlan Cox 		root->right = entry->right;
8190164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
8200164e057SAlan Cox 		    entry->next->start) - root->end;
8210164e057SAlan Cox 		vm_map_entry_set_max_free(root);
8224e94f402SAlan Cox 	}
8234e94f402SAlan Cox 	map->root = root;
8244e94f402SAlan Cox 
8254e94f402SAlan Cox 	prev = entry->prev;
8264e94f402SAlan Cox 	next = entry->next;
82799c81ca9SAlan Cox 	next->prev = prev;
82899c81ca9SAlan Cox 	prev->next = next;
82999c81ca9SAlan Cox 	map->nentries--;
83021c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
83121c641b2SJohn Baldwin 	    map->nentries, entry);
832df8bae1dSRodney W. Grimes }
833df8bae1dSRodney W. Grimes 
834df8bae1dSRodney W. Grimes /*
8350164e057SAlan Cox  *	vm_map_entry_resize_free:
8360164e057SAlan Cox  *
8370164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
8380164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
8390164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
8400164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
8410164e057SAlan Cox  *
8420164e057SAlan Cox  *	The map must be locked, and leaves it so.
8430164e057SAlan Cox  */
8440164e057SAlan Cox static void
8450164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
8460164e057SAlan Cox {
8470164e057SAlan Cox 
8480164e057SAlan Cox 	/*
8490164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
8500164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
8510164e057SAlan Cox 	 * root and making the change there.
8520164e057SAlan Cox 	 */
8530164e057SAlan Cox 	if (entry != map->root)
8540164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
8550164e057SAlan Cox 
8560164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
8570164e057SAlan Cox 	    entry->next->start) - entry->end;
8580164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
8590164e057SAlan Cox }
8600164e057SAlan Cox 
8610164e057SAlan Cox /*
862df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
863df8bae1dSRodney W. Grimes  *
864df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
865df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
866df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
867df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
868df8bae1dSRodney W. Grimes  *	result indicates whether the address is
869df8bae1dSRodney W. Grimes  *	actually contained in the map.
870df8bae1dSRodney W. Grimes  */
8710d94caffSDavid Greenman boolean_t
8721b40f8c0SMatthew Dillon vm_map_lookup_entry(
8731b40f8c0SMatthew Dillon 	vm_map_t map,
8741b40f8c0SMatthew Dillon 	vm_offset_t address,
8751b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
876df8bae1dSRodney W. Grimes {
877c0877f10SJohn Dyson 	vm_map_entry_t cur;
878df8bae1dSRodney W. Grimes 
8794e94f402SAlan Cox 	cur = vm_map_entry_splay(address, map->root);
8804e94f402SAlan Cox 	if (cur == NULL)
8814e94f402SAlan Cox 		*entry = &map->header;
8824e94f402SAlan Cox 	else {
8834e94f402SAlan Cox 		map->root = cur;
884df8bae1dSRodney W. Grimes 
885df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
886df8bae1dSRodney W. Grimes 			*entry = cur;
8874e94f402SAlan Cox 			if (cur->end > address)
888df8bae1dSRodney W. Grimes 				return (TRUE);
8894e94f402SAlan Cox 		} else
890df8bae1dSRodney W. Grimes 			*entry = cur->prev;
8914e94f402SAlan Cox 	}
892df8bae1dSRodney W. Grimes 	return (FALSE);
893df8bae1dSRodney W. Grimes }
894df8bae1dSRodney W. Grimes 
895df8bae1dSRodney W. Grimes /*
89630dcfc09SJohn Dyson  *	vm_map_insert:
89730dcfc09SJohn Dyson  *
89830dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
89930dcfc09SJohn Dyson  *	map at the specified address range.  The object's
90030dcfc09SJohn Dyson  *	size should match that of the address range.
90130dcfc09SJohn Dyson  *
90230dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
9032aaeadf8SMatthew Dillon  *
9042aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
9052aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
90630dcfc09SJohn Dyson  */
90730dcfc09SJohn Dyson int
908b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
909b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
910b9dcd593SBruce Evans 	      int cow)
91130dcfc09SJohn Dyson {
912c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
913c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
91430dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
9159730a5daSPaul Saab 	vm_eflags_t protoeflags;
91630dcfc09SJohn Dyson 
91730dcfc09SJohn Dyson 	/*
91830dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
91930dcfc09SJohn Dyson 	 */
92030dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
92130dcfc09SJohn Dyson 	    (start >= end))
92230dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
92330dcfc09SJohn Dyson 
92430dcfc09SJohn Dyson 	/*
92530dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
92630dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
92730dcfc09SJohn Dyson 	 */
92830dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
92930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
93030dcfc09SJohn Dyson 
93130dcfc09SJohn Dyson 	prev_entry = temp_entry;
93230dcfc09SJohn Dyson 
93330dcfc09SJohn Dyson 	/*
93430dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
93530dcfc09SJohn Dyson 	 */
93630dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
93730dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
93830dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
93930dcfc09SJohn Dyson 
940afa07f7eSJohn Dyson 	protoeflags = 0;
941afa07f7eSJohn Dyson 
942afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
943e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
944afa07f7eSJohn Dyson 
9454e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
946afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
947afa07f7eSJohn Dyson 
9484e045f93SAlan Cox 		KASSERT(object == NULL,
9494e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
9504e045f93SAlan Cox 	}
9514f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
9524f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
9539730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
9549730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
9554f79d873SMatthew Dillon 
9561d284e00SAlan Cox 	if (object != NULL) {
95730dcfc09SJohn Dyson 		/*
9581d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
9591d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
9601d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
9611d284e00SAlan Cox 		 * reference counting is insufficient to recognize
9621d284e00SAlan Cox 		 * aliases with precision.)
96330dcfc09SJohn Dyson 		 */
9641d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
9651d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
9662aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
9671d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
9684e045f93SAlan Cox 	}
9694e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
9704e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
9718cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
9724e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
9734e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
9748cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
97557a21abaSAlan Cox 				     prev_entry->offset,
9768cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
977cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
97830dcfc09SJohn Dyson 		/*
9792aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
9802aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
9812aaeadf8SMatthew Dillon 		 * new range as well.
98230dcfc09SJohn Dyson 		 */
9838cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
9848cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
9858cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
98630dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
98730dcfc09SJohn Dyson 			prev_entry->end = end;
9880164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
9894e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
99030dcfc09SJohn Dyson 			return (KERN_SUCCESS);
99130dcfc09SJohn Dyson 		}
9928cc7e047SJohn Dyson 
9932aaeadf8SMatthew Dillon 		/*
9942aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
9952aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
9962aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
9974e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
9982aaeadf8SMatthew Dillon 		 */
9992aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
10002aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
10012aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
10028cc7e047SJohn Dyson 		vm_object_reference(object);
1003b18bfc3dSJohn Dyson 	}
10042aaeadf8SMatthew Dillon 
10052aaeadf8SMatthew Dillon 	/*
10062aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
10072aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
10082aaeadf8SMatthew Dillon 	 * backing objects.
10092aaeadf8SMatthew Dillon 	 */
10108cc7e047SJohn Dyson 
101130dcfc09SJohn Dyson 	/*
101230dcfc09SJohn Dyson 	 * Create a new entry
101330dcfc09SJohn Dyson 	 */
101430dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
101530dcfc09SJohn Dyson 	new_entry->start = start;
101630dcfc09SJohn Dyson 	new_entry->end = end;
101730dcfc09SJohn Dyson 
1018afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
101930dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
102030dcfc09SJohn Dyson 	new_entry->offset = offset;
10212267af78SJulian Elischer 	new_entry->avail_ssize = 0;
10222267af78SJulian Elischer 
102330dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
102430dcfc09SJohn Dyson 	new_entry->protection = prot;
102530dcfc09SJohn Dyson 	new_entry->max_protection = max;
102630dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1027e5f251d2SAlan Cox 
102830dcfc09SJohn Dyson 	/*
102930dcfc09SJohn Dyson 	 * Insert the new entry into the list
103030dcfc09SJohn Dyson 	 */
103130dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
103230dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
103330dcfc09SJohn Dyson 
10341a484d28SMatthew Dillon #if 0
10351a484d28SMatthew Dillon 	/*
10361a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
10371a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
10381a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
10391a484d28SMatthew Dillon 	 */
10404e71e795SMatthew Dillon 	/*
10414e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
10424e71e795SMatthew Dillon 	 */
10434e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
10441a484d28SMatthew Dillon #endif
10454e71e795SMatthew Dillon 
10464f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
10474da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1048e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1049e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
10504f79d873SMatthew Dillon 	}
1051e972780aSAlan Cox 
105230dcfc09SJohn Dyson 	return (KERN_SUCCESS);
105330dcfc09SJohn Dyson }
105430dcfc09SJohn Dyson 
105530dcfc09SJohn Dyson /*
10560164e057SAlan Cox  *	vm_map_findspace:
10570164e057SAlan Cox  *
10580164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
10590164e057SAlan Cox  *	beginning at address >= start in the given map.
10600164e057SAlan Cox  *
10610164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
10620164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
10630164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
10640164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
10650164e057SAlan Cox  *	O(log n) amortized with splay trees.
10660164e057SAlan Cox  *
10670164e057SAlan Cox  *	The map must be locked, and leaves it so.
10680164e057SAlan Cox  *
10690164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
10700164e057SAlan Cox  *		 1 if insufficient space.
1071df8bae1dSRodney W. Grimes  */
1072df8bae1dSRodney W. Grimes int
10730164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
10740164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1075df8bae1dSRodney W. Grimes {
10760164e057SAlan Cox 	vm_map_entry_t entry;
10770164e057SAlan Cox 	vm_offset_t end, st;
1078df8bae1dSRodney W. Grimes 
1079986b43f8SAlan Cox 	/*
1080986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1081986b43f8SAlan Cox 	 * address wrap.
1082986b43f8SAlan Cox 	 */
1083df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1084df8bae1dSRodney W. Grimes 		start = map->min_offset;
1085986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1086df8bae1dSRodney W. Grimes 		return (1);
1087df8bae1dSRodney W. Grimes 
10880164e057SAlan Cox 	/* Empty tree means wide open address space. */
10890164e057SAlan Cox 	if (map->root == NULL) {
1090df8bae1dSRodney W. Grimes 		*addr = start;
10910164e057SAlan Cox 		goto found;
109299448ed1SJohn Dyson 	}
10930164e057SAlan Cox 
10940164e057SAlan Cox 	/*
10950164e057SAlan Cox 	 * After splay, if start comes before root node, then there
10960164e057SAlan Cox 	 * must be a gap from start to the root.
10970164e057SAlan Cox 	 */
10980164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
10990164e057SAlan Cox 	if (start + length <= map->root->start) {
11000164e057SAlan Cox 		*addr = start;
11010164e057SAlan Cox 		goto found;
11020164e057SAlan Cox 	}
11030164e057SAlan Cox 
11040164e057SAlan Cox 	/*
11050164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1106986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1107986b43f8SAlan Cox 	 * wrap might be a problem.
11080164e057SAlan Cox 	 */
11090164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1110986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
11110164e057SAlan Cox 		*addr = st;
11120164e057SAlan Cox 		goto found;
11130164e057SAlan Cox 	}
11140164e057SAlan Cox 
11150164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
11160164e057SAlan Cox 	entry = map->root->right;
11170164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
11180164e057SAlan Cox 		return (1);
11190164e057SAlan Cox 
11200164e057SAlan Cox 	/*
11210164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
11220164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
11230164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
11240164e057SAlan Cox 	 */
11250164e057SAlan Cox 	while (entry != NULL) {
11260164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
11270164e057SAlan Cox 			entry = entry->left;
11280164e057SAlan Cox 		else if (entry->adj_free >= length) {
11290164e057SAlan Cox 			*addr = entry->end;
11300164e057SAlan Cox 			goto found;
11310164e057SAlan Cox 		} else
11320164e057SAlan Cox 			entry = entry->right;
11330164e057SAlan Cox 	}
11340164e057SAlan Cox 
11350164e057SAlan Cox 	/* Can't get here, so panic if we do. */
11360164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
11370164e057SAlan Cox 
11380164e057SAlan Cox found:
11390164e057SAlan Cox 	/* Expand the kernel pmap, if necessary. */
11400164e057SAlan Cox 	if (map == kernel_map) {
11410164e057SAlan Cox 		end = round_page(*addr + length);
11420164e057SAlan Cox 		if (end > kernel_vm_end)
11430164e057SAlan Cox 			pmap_growkernel(end);
114499448ed1SJohn Dyson 	}
1145df8bae1dSRodney W. Grimes 	return (0);
1146df8bae1dSRodney W. Grimes }
1147df8bae1dSRodney W. Grimes 
1148df8bae1dSRodney W. Grimes /*
1149df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1150df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1151df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1152df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1153df8bae1dSRodney W. Grimes  *
11542aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11552aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1156df8bae1dSRodney W. Grimes  */
1157df8bae1dSRodney W. Grimes int
1158b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1159b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1160b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
1161b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1162df8bae1dSRodney W. Grimes {
1163c0877f10SJohn Dyson 	vm_offset_t start;
11646eaee3feSAlan Cox 	int result;
1165df8bae1dSRodney W. Grimes 
1166df8bae1dSRodney W. Grimes 	start = *addr;
1167bea41bcfSDavid Greenman 	vm_map_lock(map);
1168df8bae1dSRodney W. Grimes 	if (find_space) {
1169df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
1170df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1171df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
1172df8bae1dSRodney W. Grimes 		}
1173df8bae1dSRodney W. Grimes 		start = *addr;
1174df8bae1dSRodney W. Grimes 	}
1175bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
1176bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
1177df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1178df8bae1dSRodney W. Grimes 	return (result);
1179df8bae1dSRodney W. Grimes }
1180df8bae1dSRodney W. Grimes 
1181df8bae1dSRodney W. Grimes /*
1182b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
118367bf6868SJohn Dyson  *
11844e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
11854e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
11864e71e795SMatthew Dillon  *
11874e71e795SMatthew Dillon  *	The map must be locked.
11884e71e795SMatthew Dillon  *
11894e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
11904e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
11914e71e795SMatthew Dillon  *	both neighbors.
1192df8bae1dSRodney W. Grimes  */
1193b7b2aac2SJohn Dyson void
11941b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1195df8bae1dSRodney W. Grimes {
1196308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1197b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1198df8bae1dSRodney W. Grimes 
1199acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1200df8bae1dSRodney W. Grimes 		return;
1201308c24baSJohn Dyson 
1202308c24baSJohn Dyson 	prev = entry->prev;
1203308c24baSJohn Dyson 	if (prev != &map->header) {
120467bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
120567bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
120667bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
120795e5e988SJohn Dyson 		     (!prev->object.vm_object ||
120867bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1209afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
121067bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
121167bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
121267bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
1213b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
1214308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1215308c24baSJohn Dyson 			entry->start = prev->start;
1216308c24baSJohn Dyson 			entry->offset = prev->offset;
12170164e057SAlan Cox 			if (entry->prev != &map->header)
12180164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
1219b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1220308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1221308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1222308c24baSJohn Dyson 		}
1223308c24baSJohn Dyson 	}
1224de5f6a77SJohn Dyson 
1225de5f6a77SJohn Dyson 	next = entry->next;
1226308c24baSJohn Dyson 	if (next != &map->header) {
122767bf6868SJohn Dyson 		esize = entry->end - entry->start;
122867bf6868SJohn Dyson 		if ((entry->end == next->start) &&
122967bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
123067bf6868SJohn Dyson 		     (!entry->object.vm_object ||
123167bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1232afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
123367bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
123467bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
123567bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
1236b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
1237de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1238de5f6a77SJohn Dyson 			entry->end = next->end;
12390164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
1240b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1241de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1242de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1243df8bae1dSRodney W. Grimes 		}
1244df8bae1dSRodney W. Grimes 	}
1245de5f6a77SJohn Dyson }
1246df8bae1dSRodney W. Grimes /*
1247df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1248df8bae1dSRodney W. Grimes  *
1249df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1250df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1251df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1252df8bae1dSRodney W. Grimes  */
1253df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1254df8bae1dSRodney W. Grimes { \
1255df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1256df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1257df8bae1dSRodney W. Grimes }
1258df8bae1dSRodney W. Grimes 
1259df8bae1dSRodney W. Grimes /*
1260df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1261df8bae1dSRodney W. Grimes  *	the entry must be split.
1262df8bae1dSRodney W. Grimes  */
12630d94caffSDavid Greenman static void
12641b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1265df8bae1dSRodney W. Grimes {
1266c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1267df8bae1dSRodney W. Grimes 
1268df8bae1dSRodney W. Grimes 	/*
12690d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
12700d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
12710d94caffSDavid Greenman 	 * starting address.
1272df8bae1dSRodney W. Grimes 	 */
1273f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1274f32dbbeeSJohn Dyson 
127511cccda1SJohn Dyson 	/*
127611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
127711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
127811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
127911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
128011cccda1SJohn Dyson 	 * put this improvement.
128111cccda1SJohn Dyson 	 */
12824e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
128311cccda1SJohn Dyson 		vm_object_t object;
128411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1285c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
128611cccda1SJohn Dyson 		entry->object.vm_object = object;
128711cccda1SJohn Dyson 		entry->offset = 0;
128811cccda1SJohn Dyson 	}
128911cccda1SJohn Dyson 
1290df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1291df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1292df8bae1dSRodney W. Grimes 
1293df8bae1dSRodney W. Grimes 	new_entry->end = start;
1294df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1295df8bae1dSRodney W. Grimes 	entry->start = start;
1296df8bae1dSRodney W. Grimes 
1297df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1298df8bae1dSRodney W. Grimes 
12999fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1300df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1301df8bae1dSRodney W. Grimes 	}
1302c0877f10SJohn Dyson }
1303df8bae1dSRodney W. Grimes 
1304df8bae1dSRodney W. Grimes /*
1305df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1306df8bae1dSRodney W. Grimes  *
1307df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1308df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1309df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1310df8bae1dSRodney W. Grimes  */
1311df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1312df8bae1dSRodney W. Grimes { \
1313af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1314af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1315df8bae1dSRodney W. Grimes }
1316df8bae1dSRodney W. Grimes 
1317df8bae1dSRodney W. Grimes /*
1318df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1319df8bae1dSRodney W. Grimes  *	the entry must be split.
1320df8bae1dSRodney W. Grimes  */
13210d94caffSDavid Greenman static void
13221b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1323df8bae1dSRodney W. Grimes {
1324c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1325df8bae1dSRodney W. Grimes 
1326df8bae1dSRodney W. Grimes 	/*
132711cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
132811cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
132911cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
133011cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
133111cccda1SJohn Dyson 	 * put this improvement.
133211cccda1SJohn Dyson 	 */
13334e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
133411cccda1SJohn Dyson 		vm_object_t object;
133511cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1336c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
133711cccda1SJohn Dyson 		entry->object.vm_object = object;
133811cccda1SJohn Dyson 		entry->offset = 0;
133911cccda1SJohn Dyson 	}
134011cccda1SJohn Dyson 
134111cccda1SJohn Dyson 	/*
13420d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1343df8bae1dSRodney W. Grimes 	 */
1344df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1345df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1348df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1349df8bae1dSRodney W. Grimes 
1350df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1351df8bae1dSRodney W. Grimes 
13529fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1353df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1354df8bae1dSRodney W. Grimes 	}
1355c0877f10SJohn Dyson }
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes /*
1358df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1359df8bae1dSRodney W. Grimes  *
1360df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
1361df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
1362df8bae1dSRodney W. Grimes  */
1363df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1364df8bae1dSRodney W. Grimes 		{					\
1365df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
1366df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
1367df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
1368df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
1369df8bae1dSRodney W. Grimes 		if (start > end)			\
1370df8bae1dSRodney W. Grimes 			start = end;			\
1371df8bae1dSRodney W. Grimes 		}
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes /*
1374df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1375df8bae1dSRodney W. Grimes  *
1376df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1377df8bae1dSRodney W. Grimes  *
1378df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1379df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1380df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1381df8bae1dSRodney W. Grimes  *
1382df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1383df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1384df8bae1dSRodney W. Grimes  *		vm_fault
1385df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1386df8bae1dSRodney W. Grimes  *
1387df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1388df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1389df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1390df8bae1dSRodney W. Grimes  */
1391df8bae1dSRodney W. Grimes int
13921b40f8c0SMatthew Dillon vm_map_submap(
13931b40f8c0SMatthew Dillon 	vm_map_t map,
13941b40f8c0SMatthew Dillon 	vm_offset_t start,
13951b40f8c0SMatthew Dillon 	vm_offset_t end,
13961b40f8c0SMatthew Dillon 	vm_map_t submap)
1397df8bae1dSRodney W. Grimes {
1398df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1399c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1400df8bae1dSRodney W. Grimes 
1401df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1402df8bae1dSRodney W. Grimes 
1403df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1404df8bae1dSRodney W. Grimes 
1405df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1406df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
14070d94caffSDavid Greenman 	} else
1408df8bae1dSRodney W. Grimes 		entry = entry->next;
1409df8bae1dSRodney W. Grimes 
1410df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1411df8bae1dSRodney W. Grimes 
1412df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
14139fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1414afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
14152d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1416afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1417df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1418df8bae1dSRodney W. Grimes 	}
1419df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1420df8bae1dSRodney W. Grimes 
1421df8bae1dSRodney W. Grimes 	return (result);
1422df8bae1dSRodney W. Grimes }
1423df8bae1dSRodney W. Grimes 
1424df8bae1dSRodney W. Grimes /*
14251f78f902SAlan Cox  * The maximum number of pages to map
14261f78f902SAlan Cox  */
14271f78f902SAlan Cox #define	MAX_INIT_PT	96
14281f78f902SAlan Cox 
14291f78f902SAlan Cox /*
14300551c08dSAlan Cox  *	vm_map_pmap_enter:
14310551c08dSAlan Cox  *
14324da4d293SAlan Cox  *	Preload read-only mappings for the given object into the specified
14330551c08dSAlan Cox  *	map.  This eliminates the soft faults on process startup and
14340551c08dSAlan Cox  *	immediately after an mmap(2).
14350551c08dSAlan Cox  */
14360551c08dSAlan Cox void
14374da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
14380551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
14390551c08dSAlan Cox {
14401f78f902SAlan Cox 	vm_offset_t tmpidx;
14411f78f902SAlan Cox 	int psize;
14421f78f902SAlan Cox 	vm_page_t p, mpte;
14431f70d622SAlan Cox 	boolean_t are_queues_locked;
14440551c08dSAlan Cox 
1445ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
14461f78f902SAlan Cox 		return;
14471f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
14481f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
14491f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
14501f78f902SAlan Cox 		goto unlock_return;
14511f78f902SAlan Cox 	}
14521f78f902SAlan Cox 
14531f78f902SAlan Cox 	psize = atop(size);
14541f78f902SAlan Cox 
14551f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
14561f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
14571f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
14581f78f902SAlan Cox 		goto unlock_return;
14591f78f902SAlan Cox 	}
14601f78f902SAlan Cox 
14611f78f902SAlan Cox 	if (psize + pindex > object->size) {
14621f78f902SAlan Cox 		if (object->size < pindex)
14631f78f902SAlan Cox 			goto unlock_return;
14641f78f902SAlan Cox 		psize = object->size - pindex;
14651f78f902SAlan Cox 	}
14661f78f902SAlan Cox 
14671f70d622SAlan Cox 	are_queues_locked = FALSE;
14681f78f902SAlan Cox 	mpte = NULL;
14691f78f902SAlan Cox 
14701f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
14711f78f902SAlan Cox 		if (p->pindex < pindex) {
14721f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
14731f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
14741f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
14751f78f902SAlan Cox 		}
14761f78f902SAlan Cox 	}
14771f78f902SAlan Cox 	/*
14781f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
14791f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
14801f78f902SAlan Cox 	 * or (2) NULL.
14811f78f902SAlan Cox 	 */
14821f78f902SAlan Cox 	for (;
14831f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
14841f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
14851f78f902SAlan Cox 		/*
14861f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
14871f78f902SAlan Cox 		 * free pages allocating pv entries.
14881f78f902SAlan Cox 		 */
14891f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
14901f78f902SAlan Cox 		    cnt.v_free_count < cnt.v_free_reserved) {
14911f78f902SAlan Cox 			break;
14921f78f902SAlan Cox 		}
14931f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
14941f78f902SAlan Cox 		    (p->busy == 0) &&
14951f78f902SAlan Cox 		    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
14961f70d622SAlan Cox 			if (!are_queues_locked) {
14971f70d622SAlan Cox 				are_queues_locked = TRUE;
149885f5b245SAlan Cox 				vm_page_lock_queues();
149985f5b245SAlan Cox 			}
1500997e1c25SAlan Cox 			if (VM_PAGE_INQUEUE1(p, PQ_CACHE))
15011f70d622SAlan Cox 				vm_page_deactivate(p);
15021f78f902SAlan Cox 			mpte = pmap_enter_quick(map->pmap,
1503ba8bca61SAlan Cox 			    addr + ptoa(tmpidx), p, prot, mpte);
15041f78f902SAlan Cox 		}
15051f78f902SAlan Cox 	}
15061f70d622SAlan Cox 	if (are_queues_locked)
15071f70d622SAlan Cox 		vm_page_unlock_queues();
15081f78f902SAlan Cox unlock_return:
15091f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
15100551c08dSAlan Cox }
15110551c08dSAlan Cox 
15120551c08dSAlan Cox /*
1513df8bae1dSRodney W. Grimes  *	vm_map_protect:
1514df8bae1dSRodney W. Grimes  *
1515df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1516df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1517df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1518df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1519df8bae1dSRodney W. Grimes  */
1520df8bae1dSRodney W. Grimes int
1521b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1522b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1523df8bae1dSRodney W. Grimes {
1524c0877f10SJohn Dyson 	vm_map_entry_t current;
1525df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1526df8bae1dSRodney W. Grimes 
1527df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1528df8bae1dSRodney W. Grimes 
1529df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1530df8bae1dSRodney W. Grimes 
1531df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1532df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1533b7b2aac2SJohn Dyson 	} else {
1534df8bae1dSRodney W. Grimes 		entry = entry->next;
1535b7b2aac2SJohn Dyson 	}
1536df8bae1dSRodney W. Grimes 
1537df8bae1dSRodney W. Grimes 	/*
15380d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1539df8bae1dSRodney W. Grimes 	 */
1540df8bae1dSRodney W. Grimes 	current = entry;
1541df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1542afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1543a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1544df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1545a1f6d91cSDavid Greenman 		}
1546df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1547df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1548df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1549df8bae1dSRodney W. Grimes 		}
1550df8bae1dSRodney W. Grimes 		current = current->next;
1551df8bae1dSRodney W. Grimes 	}
1552df8bae1dSRodney W. Grimes 
1553df8bae1dSRodney W. Grimes 	/*
15540d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
15550d94caffSDavid Greenman 	 * necessary the second time.]
1556df8bae1dSRodney W. Grimes 	 */
1557df8bae1dSRodney W. Grimes 	current = entry;
1558df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1559df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1560df8bae1dSRodney W. Grimes 
1561df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1562df8bae1dSRodney W. Grimes 
1563df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1564df8bae1dSRodney W. Grimes 		if (set_max)
1565df8bae1dSRodney W. Grimes 			current->protection =
1566df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1567df8bae1dSRodney W. Grimes 			    old_prot;
1568df8bae1dSRodney W. Grimes 		else
1569df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1570df8bae1dSRodney W. Grimes 
1571df8bae1dSRodney W. Grimes 		/*
15720d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
15730d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1574df8bae1dSRodney W. Grimes 		 */
1575df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
1576afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1577df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1578df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1579df8bae1dSRodney W. Grimes 			    current->end,
15801c85e3dfSAlan Cox 			    current->protection & MASK(current));
1581df8bae1dSRodney W. Grimes #undef	MASK
1582df8bae1dSRodney W. Grimes 		}
15837d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1584df8bae1dSRodney W. Grimes 		current = current->next;
1585df8bae1dSRodney W. Grimes 	}
1586df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1587df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1588df8bae1dSRodney W. Grimes }
1589df8bae1dSRodney W. Grimes 
1590df8bae1dSRodney W. Grimes /*
1591867a482dSJohn Dyson  *	vm_map_madvise:
1592867a482dSJohn Dyson  *
1593867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1594f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1595f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1596f7fc307aSAlan Cox  *	objects.
1597867a482dSJohn Dyson  */
1598b4309055SMatthew Dillon int
15991b40f8c0SMatthew Dillon vm_map_madvise(
16001b40f8c0SMatthew Dillon 	vm_map_t map,
16011b40f8c0SMatthew Dillon 	vm_offset_t start,
16021b40f8c0SMatthew Dillon 	vm_offset_t end,
16031b40f8c0SMatthew Dillon 	int behav)
1604867a482dSJohn Dyson {
1605f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1606b4309055SMatthew Dillon 	int modify_map = 0;
1607867a482dSJohn Dyson 
1608b4309055SMatthew Dillon 	/*
1609b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1610b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1611b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1612b4309055SMatthew Dillon 	 * on the map.
1613b4309055SMatthew Dillon 	 */
1614b4309055SMatthew Dillon 	switch(behav) {
1615b4309055SMatthew Dillon 	case MADV_NORMAL:
1616b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1617b4309055SMatthew Dillon 	case MADV_RANDOM:
16184f79d873SMatthew Dillon 	case MADV_NOSYNC:
16194f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
16209730a5daSPaul Saab 	case MADV_NOCORE:
16219730a5daSPaul Saab 	case MADV_CORE:
1622b4309055SMatthew Dillon 		modify_map = 1;
1623867a482dSJohn Dyson 		vm_map_lock(map);
1624b4309055SMatthew Dillon 		break;
1625b4309055SMatthew Dillon 	case MADV_WILLNEED:
1626b4309055SMatthew Dillon 	case MADV_DONTNEED:
1627b4309055SMatthew Dillon 	case MADV_FREE:
1628f7fc307aSAlan Cox 		vm_map_lock_read(map);
1629b4309055SMatthew Dillon 		break;
1630b4309055SMatthew Dillon 	default:
1631b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1632b4309055SMatthew Dillon 	}
1633b4309055SMatthew Dillon 
1634b4309055SMatthew Dillon 	/*
1635b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1636b4309055SMatthew Dillon 	 */
1637867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1638867a482dSJohn Dyson 
1639867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1640f7fc307aSAlan Cox 		if (modify_map)
1641867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1642b4309055SMatthew Dillon 	} else {
1643867a482dSJohn Dyson 		entry = entry->next;
1644b4309055SMatthew Dillon 	}
1645867a482dSJohn Dyson 
1646f7fc307aSAlan Cox 	if (modify_map) {
1647f7fc307aSAlan Cox 		/*
1648f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1649f7fc307aSAlan Cox 		 *
1650f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1651f7fc307aSAlan Cox 		 * limited to the specified address range.
1652f7fc307aSAlan Cox 		 */
1653867a482dSJohn Dyson 		for (current = entry;
1654867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1655b4309055SMatthew Dillon 		     current = current->next
1656b4309055SMatthew Dillon 		) {
1657f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1658867a482dSJohn Dyson 				continue;
1659fed9a903SJohn Dyson 
166047221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1661fed9a903SJohn Dyson 
1662f7fc307aSAlan Cox 			switch (behav) {
1663867a482dSJohn Dyson 			case MADV_NORMAL:
16647f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1665867a482dSJohn Dyson 				break;
1666867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
16677f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1668867a482dSJohn Dyson 				break;
1669867a482dSJohn Dyson 			case MADV_RANDOM:
16707f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1671867a482dSJohn Dyson 				break;
16724f79d873SMatthew Dillon 			case MADV_NOSYNC:
16734f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
16744f79d873SMatthew Dillon 				break;
16754f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
16764f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
16774f79d873SMatthew Dillon 				break;
16789730a5daSPaul Saab 			case MADV_NOCORE:
16799730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
16809730a5daSPaul Saab 				break;
16819730a5daSPaul Saab 			case MADV_CORE:
16829730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
16839730a5daSPaul Saab 				break;
1684867a482dSJohn Dyson 			default:
1685867a482dSJohn Dyson 				break;
1686867a482dSJohn Dyson 			}
1687f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1688867a482dSJohn Dyson 		}
1689867a482dSJohn Dyson 		vm_map_unlock(map);
1690b4309055SMatthew Dillon 	} else {
1691f7fc307aSAlan Cox 		vm_pindex_t pindex;
1692f7fc307aSAlan Cox 		int count;
1693f7fc307aSAlan Cox 
1694f7fc307aSAlan Cox 		/*
1695f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1696f7fc307aSAlan Cox 		 * vm_object.
1697f7fc307aSAlan Cox 		 *
1698f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1699f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1700f7fc307aSAlan Cox 		 */
1701f7fc307aSAlan Cox 		for (current = entry;
1702f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1703b4309055SMatthew Dillon 		     current = current->next
1704b4309055SMatthew Dillon 		) {
17055f99b57cSMatthew Dillon 			vm_offset_t useStart;
17065f99b57cSMatthew Dillon 
1707f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1708f7fc307aSAlan Cox 				continue;
1709f7fc307aSAlan Cox 
1710f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1711f7fc307aSAlan Cox 			count = atop(current->end - current->start);
17125f99b57cSMatthew Dillon 			useStart = current->start;
1713f7fc307aSAlan Cox 
1714f7fc307aSAlan Cox 			if (current->start < start) {
1715f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1716f7fc307aSAlan Cox 				count -= atop(start - current->start);
17175f99b57cSMatthew Dillon 				useStart = start;
1718f7fc307aSAlan Cox 			}
1719f7fc307aSAlan Cox 			if (current->end > end)
1720f7fc307aSAlan Cox 				count -= atop(current->end - end);
1721f7fc307aSAlan Cox 
1722f7fc307aSAlan Cox 			if (count <= 0)
1723f7fc307aSAlan Cox 				continue;
1724f7fc307aSAlan Cox 
1725f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1726f7fc307aSAlan Cox 					  pindex, count, behav);
1727b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
17280551c08dSAlan Cox 				vm_map_pmap_enter(map,
17295f99b57cSMatthew Dillon 				    useStart,
17304da4d293SAlan Cox 				    current->protection,
1731f7fc307aSAlan Cox 				    current->object.vm_object,
1732b4309055SMatthew Dillon 				    pindex,
1733b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1734e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1735b4309055SMatthew Dillon 				);
1736f7fc307aSAlan Cox 			}
1737f7fc307aSAlan Cox 		}
1738f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1739f7fc307aSAlan Cox 	}
1740b4309055SMatthew Dillon 	return (0);
1741867a482dSJohn Dyson }
1742867a482dSJohn Dyson 
1743867a482dSJohn Dyson 
1744867a482dSJohn Dyson /*
1745df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1746df8bae1dSRodney W. Grimes  *
1747df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1748df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1749df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1750df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1751df8bae1dSRodney W. Grimes  */
1752df8bae1dSRodney W. Grimes int
1753b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1754b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1755df8bae1dSRodney W. Grimes {
1756c0877f10SJohn Dyson 	vm_map_entry_t entry;
1757df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1760df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1761df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1762df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1763df8bae1dSRodney W. Grimes 		break;
1764df8bae1dSRodney W. Grimes 	default:
1765df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1766df8bae1dSRodney W. Grimes 	}
1767df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1768df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1769df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1770df8bae1dSRodney W. Grimes 		entry = temp_entry;
1771df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17720d94caffSDavid Greenman 	} else
1773df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1774df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1775df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1776df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
177744428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
1778df8bae1dSRodney W. Grimes 		entry = entry->next;
1779df8bae1dSRodney W. Grimes 	}
1780df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1781df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1782df8bae1dSRodney W. Grimes }
1783df8bae1dSRodney W. Grimes 
1784df8bae1dSRodney W. Grimes /*
1785acd9a301SAlan Cox  *	vm_map_unwire:
1786acd9a301SAlan Cox  *
1787e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
1788acd9a301SAlan Cox  */
1789acd9a301SAlan Cox int
1790acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1791abd498aaSBruce M Simpson     int flags)
1792acd9a301SAlan Cox {
1793acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
1794acd9a301SAlan Cox 	vm_offset_t saved_start;
1795acd9a301SAlan Cox 	unsigned int last_timestamp;
1796acd9a301SAlan Cox 	int rv;
1797abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
1798acd9a301SAlan Cox 
1799abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1800acd9a301SAlan Cox 	vm_map_lock(map);
1801acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
1802acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1803abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1804cbef13d8SAlan Cox 			first_entry = first_entry->next;
1805abd498aaSBruce M Simpson 		else {
1806acd9a301SAlan Cox 			vm_map_unlock(map);
1807acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
1808acd9a301SAlan Cox 		}
1809abd498aaSBruce M Simpson 	}
1810acd9a301SAlan Cox 	last_timestamp = map->timestamp;
1811acd9a301SAlan Cox 	entry = first_entry;
1812acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1813acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1814acd9a301SAlan Cox 			/*
1815acd9a301SAlan Cox 			 * We have not yet clipped the entry.
1816acd9a301SAlan Cox 			 */
1817acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
1818acd9a301SAlan Cox 			    entry->start;
1819acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1820acd9a301SAlan Cox 			if (vm_map_unlock_and_wait(map, user_unwire)) {
1821acd9a301SAlan Cox 				/*
1822acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
1823acd9a301SAlan Cox 				 */
1824acd9a301SAlan Cox 			}
1825acd9a301SAlan Cox 			vm_map_lock(map);
1826acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
1827acd9a301SAlan Cox 				/*
1828acd9a301SAlan Cox 				 * Look again for the entry because the map was
1829acd9a301SAlan Cox 				 * modified while it was unlocked.
1830acd9a301SAlan Cox 				 * Specifically, the entry may have been
1831acd9a301SAlan Cox 				 * clipped, merged, or deleted.
1832acd9a301SAlan Cox 				 */
1833acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
1834acd9a301SAlan Cox 				    &tmp_entry)) {
1835cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1836cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1837cbef13d8SAlan Cox 					else {
1838acd9a301SAlan Cox 						if (saved_start == start) {
1839acd9a301SAlan Cox 							/*
1840acd9a301SAlan Cox 							 * First_entry has been deleted.
1841acd9a301SAlan Cox 							 */
1842acd9a301SAlan Cox 							vm_map_unlock(map);
1843acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
1844acd9a301SAlan Cox 						}
1845acd9a301SAlan Cox 						end = saved_start;
1846acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
1847acd9a301SAlan Cox 						goto done;
1848acd9a301SAlan Cox 					}
1849cbef13d8SAlan Cox 				}
1850acd9a301SAlan Cox 				if (entry == first_entry)
1851acd9a301SAlan Cox 					first_entry = tmp_entry;
1852acd9a301SAlan Cox 				else
1853acd9a301SAlan Cox 					first_entry = NULL;
1854acd9a301SAlan Cox 				entry = tmp_entry;
1855acd9a301SAlan Cox 			}
1856acd9a301SAlan Cox 			last_timestamp = map->timestamp;
1857acd9a301SAlan Cox 			continue;
1858acd9a301SAlan Cox 		}
1859acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
1860acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
1861acd9a301SAlan Cox 		/*
1862acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
1863acd9a301SAlan Cox 		 * above.)
1864acd9a301SAlan Cox 		 */
1865acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1866acd9a301SAlan Cox 		/*
1867acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
1868abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1869acd9a301SAlan Cox 		 */
1870abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1871abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
1872abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
1873acd9a301SAlan Cox 			end = entry->end;
1874acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
1875acd9a301SAlan Cox 			goto done;
1876acd9a301SAlan Cox 		}
1877acd9a301SAlan Cox 		/*
18783ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
1879acd9a301SAlan Cox 		 */
18800ada205eSBrian Feldman 		if (!user_unwire &&
18810ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
1882acd9a301SAlan Cox 			end = entry->end;
1883acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
1884acd9a301SAlan Cox 			goto done;
1885acd9a301SAlan Cox 		}
1886acd9a301SAlan Cox 		entry = entry->next;
1887acd9a301SAlan Cox 	}
1888acd9a301SAlan Cox 	rv = KERN_SUCCESS;
1889acd9a301SAlan Cox done:
1890e27e17b7SAlan Cox 	need_wakeup = FALSE;
1891acd9a301SAlan Cox 	if (first_entry == NULL) {
1892acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
1893cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1894cbef13d8SAlan Cox 			first_entry = first_entry->next;
1895cbef13d8SAlan Cox 		else
1896acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
1897acd9a301SAlan Cox 	}
1898acd9a301SAlan Cox 	entry = first_entry;
1899acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
19003ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
19013ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
1902b2f3846aSAlan Cox 			if (user_unwire)
1903b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1904b2f3846aSAlan Cox 			entry->wired_count--;
19050ada205eSBrian Feldman 			if (entry->wired_count == 0) {
1906b2f3846aSAlan Cox 				/*
1907b2f3846aSAlan Cox 				 * Retain the map lock.
1908b2f3846aSAlan Cox 				 */
19094be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
19104be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
19114be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
1912b2f3846aSAlan Cox 			}
1913b2f3846aSAlan Cox 		}
1914acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1915acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
1916acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1917acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1918acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1919acd9a301SAlan Cox 			need_wakeup = TRUE;
1920acd9a301SAlan Cox 		}
1921acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
1922acd9a301SAlan Cox 		entry = entry->next;
1923acd9a301SAlan Cox 	}
1924acd9a301SAlan Cox 	vm_map_unlock(map);
1925acd9a301SAlan Cox 	if (need_wakeup)
1926acd9a301SAlan Cox 		vm_map_wakeup(map);
1927acd9a301SAlan Cox 	return (rv);
1928acd9a301SAlan Cox }
1929acd9a301SAlan Cox 
1930acd9a301SAlan Cox /*
1931e27e17b7SAlan Cox  *	vm_map_wire:
1932e27e17b7SAlan Cox  *
1933e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
1934e27e17b7SAlan Cox  */
1935e27e17b7SAlan Cox int
1936e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1937abd498aaSBruce M Simpson     int flags)
1938e27e17b7SAlan Cox {
193912d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
194012d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
194112d7cc84SAlan Cox 	unsigned int last_timestamp;
194212d7cc84SAlan Cox 	int rv;
19434be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
1944e27e17b7SAlan Cox 
1945abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
194612d7cc84SAlan Cox 	vm_map_lock(map);
194712d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
194812d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1949abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1950cbef13d8SAlan Cox 			first_entry = first_entry->next;
1951abd498aaSBruce M Simpson 		else {
195212d7cc84SAlan Cox 			vm_map_unlock(map);
195312d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
195412d7cc84SAlan Cox 		}
1955abd498aaSBruce M Simpson 	}
195612d7cc84SAlan Cox 	last_timestamp = map->timestamp;
195712d7cc84SAlan Cox 	entry = first_entry;
195812d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
195912d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
196012d7cc84SAlan Cox 			/*
196112d7cc84SAlan Cox 			 * We have not yet clipped the entry.
196212d7cc84SAlan Cox 			 */
196312d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
196412d7cc84SAlan Cox 			    entry->start;
196512d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
196612d7cc84SAlan Cox 			if (vm_map_unlock_and_wait(map, user_wire)) {
196712d7cc84SAlan Cox 				/*
196812d7cc84SAlan Cox 				 * Allow interruption of user wiring?
196912d7cc84SAlan Cox 				 */
197012d7cc84SAlan Cox 			}
197112d7cc84SAlan Cox 			vm_map_lock(map);
197212d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
197312d7cc84SAlan Cox 				/*
197412d7cc84SAlan Cox 				 * Look again for the entry because the map was
197512d7cc84SAlan Cox 				 * modified while it was unlocked.
197612d7cc84SAlan Cox 				 * Specifically, the entry may have been
197712d7cc84SAlan Cox 				 * clipped, merged, or deleted.
197812d7cc84SAlan Cox 				 */
197912d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
198012d7cc84SAlan Cox 				    &tmp_entry)) {
1981cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1982cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1983cbef13d8SAlan Cox 					else {
198412d7cc84SAlan Cox 						if (saved_start == start) {
198512d7cc84SAlan Cox 							/*
198612d7cc84SAlan Cox 							 * first_entry has been deleted.
198712d7cc84SAlan Cox 							 */
198812d7cc84SAlan Cox 							vm_map_unlock(map);
198912d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
199012d7cc84SAlan Cox 						}
199112d7cc84SAlan Cox 						end = saved_start;
199212d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
199312d7cc84SAlan Cox 						goto done;
199412d7cc84SAlan Cox 					}
1995cbef13d8SAlan Cox 				}
199612d7cc84SAlan Cox 				if (entry == first_entry)
199712d7cc84SAlan Cox 					first_entry = tmp_entry;
199812d7cc84SAlan Cox 				else
199912d7cc84SAlan Cox 					first_entry = NULL;
200012d7cc84SAlan Cox 				entry = tmp_entry;
200112d7cc84SAlan Cox 			}
200212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
200312d7cc84SAlan Cox 			continue;
200412d7cc84SAlan Cox 		}
200512d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
200612d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
200712d7cc84SAlan Cox 		/*
200812d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
200912d7cc84SAlan Cox 		 * above.)
201012d7cc84SAlan Cox 		 */
201112d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
201212d7cc84SAlan Cox 		/*
20130ada205eSBrian Feldman 		 *
201412d7cc84SAlan Cox 		 */
20150ada205eSBrian Feldman 		if (entry->wired_count == 0) {
20160ada205eSBrian Feldman 			entry->wired_count++;
201712d7cc84SAlan Cox 			saved_start = entry->start;
201812d7cc84SAlan Cox 			saved_end = entry->end;
20194be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
20204be14af9SAlan Cox 			    entry->object.vm_object->type == OBJT_DEVICE;
202112d7cc84SAlan Cox 			/*
202212d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
202312d7cc84SAlan Cox 			 * mark.
202412d7cc84SAlan Cox 			 */
202512d7cc84SAlan Cox 			vm_map_unlock(map);
2026ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
20274be14af9SAlan Cox 			    user_wire, fictitious);
202812d7cc84SAlan Cox 			vm_map_lock(map);
202912d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
203012d7cc84SAlan Cox 				/*
203112d7cc84SAlan Cox 				 * Look again for the entry because the map was
203212d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
203312d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
203412d7cc84SAlan Cox 				 * deleted.
203512d7cc84SAlan Cox 				 */
203612d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
203712d7cc84SAlan Cox 				    &tmp_entry);
203812d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
203912d7cc84SAlan Cox 				if (entry == first_entry)
204012d7cc84SAlan Cox 					first_entry = tmp_entry;
204112d7cc84SAlan Cox 				else
204212d7cc84SAlan Cox 					first_entry = NULL;
204312d7cc84SAlan Cox 				entry = tmp_entry;
204428c58286SAlan Cox 				while (entry->end < saved_end) {
204528c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
204628c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
204728c58286SAlan Cox 						    ("vm_map_wire: bad count"));
204828c58286SAlan Cox 						entry->wired_count = -1;
204928c58286SAlan Cox 					}
205012d7cc84SAlan Cox 					entry = entry->next;
205112d7cc84SAlan Cox 				}
205228c58286SAlan Cox 			}
205312d7cc84SAlan Cox 			last_timestamp = map->timestamp;
205412d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
205528c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
205628c58286SAlan Cox 				    ("vm_map_wire: bad count"));
205712d7cc84SAlan Cox 				/*
205828c58286SAlan Cox 				 * Assign an out-of-range value to represent
205928c58286SAlan Cox 				 * the failure to wire this entry.
206012d7cc84SAlan Cox 				 */
206128c58286SAlan Cox 				entry->wired_count = -1;
206212d7cc84SAlan Cox 				end = entry->end;
206312d7cc84SAlan Cox 				goto done;
206412d7cc84SAlan Cox 			}
20650ada205eSBrian Feldman 		} else if (!user_wire ||
20660ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
20670ada205eSBrian Feldman 			entry->wired_count++;
206812d7cc84SAlan Cox 		}
206912d7cc84SAlan Cox 		/*
207012d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2071abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
207212d7cc84SAlan Cox 		 */
2073abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2074abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2075abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
207612d7cc84SAlan Cox 			end = entry->end;
207712d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
207812d7cc84SAlan Cox 			goto done;
207912d7cc84SAlan Cox 		}
208012d7cc84SAlan Cox 		entry = entry->next;
208112d7cc84SAlan Cox 	}
208212d7cc84SAlan Cox 	rv = KERN_SUCCESS;
208312d7cc84SAlan Cox done:
208412d7cc84SAlan Cox 	need_wakeup = FALSE;
208512d7cc84SAlan Cox 	if (first_entry == NULL) {
208612d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2087cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2088cbef13d8SAlan Cox 			first_entry = first_entry->next;
2089cbef13d8SAlan Cox 		else
209012d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
209112d7cc84SAlan Cox 	}
209212d7cc84SAlan Cox 	entry = first_entry;
209312d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
209412d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
209512d7cc84SAlan Cox 			if (user_wire)
209612d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
209728c58286SAlan Cox 		} else if (entry->wired_count == -1) {
209828c58286SAlan Cox 			/*
209928c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
210028c58286SAlan Cox 			 * unnecessary.
210128c58286SAlan Cox 			 */
210228c58286SAlan Cox 			entry->wired_count = 0;
210312d7cc84SAlan Cox 		} else {
21040ada205eSBrian Feldman 			if (!user_wire ||
21050ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
210612d7cc84SAlan Cox 				entry->wired_count--;
21070ada205eSBrian Feldman 			if (entry->wired_count == 0) {
210812d7cc84SAlan Cox 				/*
210912d7cc84SAlan Cox 				 * Retain the map lock.
211012d7cc84SAlan Cox 				 */
21114be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
21124be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
21134be14af9SAlan Cox 				    entry->object.vm_object->type == OBJT_DEVICE);
211412d7cc84SAlan Cox 			}
211512d7cc84SAlan Cox 		}
211612d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
211712d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
211812d7cc84SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
211912d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
212012d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
212112d7cc84SAlan Cox 			need_wakeup = TRUE;
212212d7cc84SAlan Cox 		}
212312d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
212412d7cc84SAlan Cox 		entry = entry->next;
212512d7cc84SAlan Cox 	}
212612d7cc84SAlan Cox 	vm_map_unlock(map);
212712d7cc84SAlan Cox 	if (need_wakeup)
212812d7cc84SAlan Cox 		vm_map_wakeup(map);
212912d7cc84SAlan Cox 	return (rv);
2130e27e17b7SAlan Cox }
2131e27e17b7SAlan Cox 
2132e27e17b7SAlan Cox /*
2133950f8459SAlan Cox  * vm_map_sync
2134df8bae1dSRodney W. Grimes  *
2135df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2136df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2137df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2138df8bae1dSRodney W. Grimes  *
2139637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2140637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2141637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2142637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2143637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2144637315edSAlan Cox  * flushing the current region containing start.
2145637315edSAlan Cox  *
2146df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2147df8bae1dSRodney W. Grimes  */
2148df8bae1dSRodney W. Grimes int
2149950f8459SAlan Cox vm_map_sync(
21501b40f8c0SMatthew Dillon 	vm_map_t map,
21511b40f8c0SMatthew Dillon 	vm_offset_t start,
21521b40f8c0SMatthew Dillon 	vm_offset_t end,
21531b40f8c0SMatthew Dillon 	boolean_t syncio,
21541b40f8c0SMatthew Dillon 	boolean_t invalidate)
2155df8bae1dSRodney W. Grimes {
2156c0877f10SJohn Dyson 	vm_map_entry_t current;
2157df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2158df8bae1dSRodney W. Grimes 	vm_size_t size;
2159df8bae1dSRodney W. Grimes 	vm_object_t object;
2160a316d390SJohn Dyson 	vm_ooffset_t offset;
2161df8bae1dSRodney W. Grimes 
2162df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2163df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2164df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2165df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2166df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2167637315edSAlan Cox 	} else if (start == end) {
2168637315edSAlan Cox 		start = entry->start;
2169637315edSAlan Cox 		end = entry->end;
2170df8bae1dSRodney W. Grimes 	}
2171df8bae1dSRodney W. Grimes 	/*
2172b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2173df8bae1dSRodney W. Grimes 	 */
2174df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2175b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2176df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2177df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2178df8bae1dSRodney W. Grimes 		}
2179df8bae1dSRodney W. Grimes 		if (end > current->end &&
2180df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2181df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2182df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2183df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2184df8bae1dSRodney W. Grimes 		}
2185df8bae1dSRodney W. Grimes 	}
2186df8bae1dSRodney W. Grimes 
2187bc105a67SAlan Cox 	if (invalidate) {
2188c1fbc251SAlan Cox 		VM_LOCK_GIANT();
2189bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2190c1fbc251SAlan Cox 		VM_UNLOCK_GIANT();
2191bc105a67SAlan Cox 	}
2192df8bae1dSRodney W. Grimes 	/*
2193df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2194df8bae1dSRodney W. Grimes 	 * objects as we go.
2195df8bae1dSRodney W. Grimes 	 */
2196df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2197df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2198df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
21999fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2200c0877f10SJohn Dyson 			vm_map_t smap;
2201df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2202df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2203df8bae1dSRodney W. Grimes 
22049fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2205df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2206df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2207df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2208df8bae1dSRodney W. Grimes 			if (tsize < size)
2209df8bae1dSRodney W. Grimes 				size = tsize;
2210df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2211df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2212df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2213df8bae1dSRodney W. Grimes 		} else {
2214df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2215df8bae1dSRodney W. Grimes 		}
2216950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2217df8bae1dSRodney W. Grimes 		start += size;
2218df8bae1dSRodney W. Grimes 	}
2219df8bae1dSRodney W. Grimes 
2220df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2221df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2222df8bae1dSRodney W. Grimes }
2223df8bae1dSRodney W. Grimes 
2224df8bae1dSRodney W. Grimes /*
2225df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2226df8bae1dSRodney W. Grimes  *
2227df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2228df8bae1dSRodney W. Grimes  *
2229df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2230df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2231df8bae1dSRodney W. Grimes  */
22320362d7d7SJohn Dyson static void
22331b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2234df8bae1dSRodney W. Grimes {
22354be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
22364be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
22374be14af9SAlan Cox 	    entry->object.vm_object->type == OBJT_DEVICE);
2238df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2239df8bae1dSRodney W. Grimes }
2240df8bae1dSRodney W. Grimes 
2241df8bae1dSRodney W. Grimes /*
2242df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2243df8bae1dSRodney W. Grimes  *
2244df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2245df8bae1dSRodney W. Grimes  */
22460362d7d7SJohn Dyson static void
22471b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2248df8bae1dSRodney W. Grimes {
224932a89c32SAlan Cox 	vm_object_t object;
225032a89c32SAlan Cox 	vm_pindex_t offidxstart, offidxend, count;
225132a89c32SAlan Cox 
2252df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
2253df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
2254df8bae1dSRodney W. Grimes 
225532a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
225632a89c32SAlan Cox 	    (object = entry->object.vm_object) != NULL) {
225732a89c32SAlan Cox 		count = OFF_TO_IDX(entry->end - entry->start);
225832a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
225932a89c32SAlan Cox 		offidxend = offidxstart + count;
226032a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
226132a89c32SAlan Cox 		if (object->ref_count != 1 &&
226232a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
226332a89c32SAlan Cox 		     object == kernel_object || object == kmem_object) &&
226432a89c32SAlan Cox 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
226532a89c32SAlan Cox 			vm_object_collapse(object);
2266b103b948SDag-Erling Smørgrav 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
226732a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
226832a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
226932a89c32SAlan Cox 			if (offidxend >= object->size &&
227032a89c32SAlan Cox 			    offidxstart < object->size)
227132a89c32SAlan Cox 				object->size = offidxstart;
227232a89c32SAlan Cox 		}
227332a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
227432a89c32SAlan Cox 		vm_object_deallocate(object);
2275b5b40fa6SJohn Dyson 	}
2276df8bae1dSRodney W. Grimes 
2277df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
2278df8bae1dSRodney W. Grimes }
2279df8bae1dSRodney W. Grimes 
2280df8bae1dSRodney W. Grimes /*
2281df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2282df8bae1dSRodney W. Grimes  *
2283df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2284df8bae1dSRodney W. Grimes  *	map.
2285df8bae1dSRodney W. Grimes  */
2286df8bae1dSRodney W. Grimes int
22871b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2288df8bae1dSRodney W. Grimes {
2289c0877f10SJohn Dyson 	vm_map_entry_t entry;
2290df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2291df8bae1dSRodney W. Grimes 
2292df8bae1dSRodney W. Grimes 	/*
2293df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2294df8bae1dSRodney W. Grimes 	 */
2295876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2296df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2297876318ecSAlan Cox 	else {
2298df8bae1dSRodney W. Grimes 		entry = first_entry;
2299df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2300df8bae1dSRodney W. Grimes 	}
2301df8bae1dSRodney W. Grimes 
2302df8bae1dSRodney W. Grimes 	/*
2303df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2304df8bae1dSRodney W. Grimes 	 */
2305df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2306df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2307df8bae1dSRodney W. Grimes 
230873b2baceSAlan Cox 		/*
230973b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
23107c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
23117c938963SBrian Feldman 		 * user maps.
231273b2baceSAlan Cox 		 */
23137c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
23147c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
23157c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
231673b2baceSAlan Cox 			unsigned int last_timestamp;
231773b2baceSAlan Cox 			vm_offset_t saved_start;
231873b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
231973b2baceSAlan Cox 
232073b2baceSAlan Cox 			saved_start = entry->start;
232173b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
232273b2baceSAlan Cox 			last_timestamp = map->timestamp;
232373b2baceSAlan Cox 			(void) vm_map_unlock_and_wait(map, FALSE);
232473b2baceSAlan Cox 			vm_map_lock(map);
232573b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
232673b2baceSAlan Cox 				/*
232773b2baceSAlan Cox 				 * Look again for the entry because the map was
232873b2baceSAlan Cox 				 * modified while it was unlocked.
232973b2baceSAlan Cox 				 * Specifically, the entry may have been
233073b2baceSAlan Cox 				 * clipped, merged, or deleted.
233173b2baceSAlan Cox 				 */
233273b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
233373b2baceSAlan Cox 							 &tmp_entry))
233473b2baceSAlan Cox 					entry = tmp_entry->next;
233573b2baceSAlan Cox 				else {
233673b2baceSAlan Cox 					entry = tmp_entry;
233773b2baceSAlan Cox 					vm_map_clip_start(map, entry,
233873b2baceSAlan Cox 							  saved_start);
233973b2baceSAlan Cox 				}
234073b2baceSAlan Cox 			}
234173b2baceSAlan Cox 			continue;
234273b2baceSAlan Cox 		}
2343df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2344df8bae1dSRodney W. Grimes 
2345c0877f10SJohn Dyson 		next = entry->next;
2346df8bae1dSRodney W. Grimes 
2347df8bae1dSRodney W. Grimes 		/*
23480d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
23490d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2350df8bae1dSRodney W. Grimes 		 */
2351c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2352df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2353c0877f10SJohn Dyson 		}
2354df8bae1dSRodney W. Grimes 
235540448065SAlan Cox 		if (!map->system_map)
2356c1fbc251SAlan Cox 			VM_LOCK_GIANT();
235732a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
235840448065SAlan Cox 		if (!map->system_map)
2359c1fbc251SAlan Cox 			VM_UNLOCK_GIANT();
2360df8bae1dSRodney W. Grimes 
2361df8bae1dSRodney W. Grimes 		/*
23620d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
23630d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
23640d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
23650d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2366df8bae1dSRodney W. Grimes 		 */
2367df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2368df8bae1dSRodney W. Grimes 		entry = next;
2369df8bae1dSRodney W. Grimes 	}
2370df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2371df8bae1dSRodney W. Grimes }
2372df8bae1dSRodney W. Grimes 
2373df8bae1dSRodney W. Grimes /*
2374df8bae1dSRodney W. Grimes  *	vm_map_remove:
2375df8bae1dSRodney W. Grimes  *
2376df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2377df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2378df8bae1dSRodney W. Grimes  */
2379df8bae1dSRodney W. Grimes int
23801b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2381df8bae1dSRodney W. Grimes {
23826eaee3feSAlan Cox 	int result;
2383df8bae1dSRodney W. Grimes 
2384df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2385df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2386df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2387df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2388df8bae1dSRodney W. Grimes 	return (result);
2389df8bae1dSRodney W. Grimes }
2390df8bae1dSRodney W. Grimes 
2391df8bae1dSRodney W. Grimes /*
2392df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2393df8bae1dSRodney W. Grimes  *
23942d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
23952d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
23962d5c7e45SMatthew Dillon  *
23972d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
23982d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
23992d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
24002d5c7e45SMatthew Dillon  *
24012d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2402d8834602SAlan Cox  *
2403d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2404df8bae1dSRodney W. Grimes  */
24050d94caffSDavid Greenman boolean_t
2406b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2407b9dcd593SBruce Evans 			vm_prot_t protection)
2408df8bae1dSRodney W. Grimes {
2409c0877f10SJohn Dyson 	vm_map_entry_t entry;
2410df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2411df8bae1dSRodney W. Grimes 
2412d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2413df8bae1dSRodney W. Grimes 		return (FALSE);
2414df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2415df8bae1dSRodney W. Grimes 
2416df8bae1dSRodney W. Grimes 	while (start < end) {
2417d8834602SAlan Cox 		if (entry == &map->header)
2418df8bae1dSRodney W. Grimes 			return (FALSE);
2419df8bae1dSRodney W. Grimes 		/*
2420df8bae1dSRodney W. Grimes 		 * No holes allowed!
2421df8bae1dSRodney W. Grimes 		 */
2422d8834602SAlan Cox 		if (start < entry->start)
2423df8bae1dSRodney W. Grimes 			return (FALSE);
2424df8bae1dSRodney W. Grimes 		/*
2425df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2426df8bae1dSRodney W. Grimes 		 */
2427d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2428df8bae1dSRodney W. Grimes 			return (FALSE);
2429df8bae1dSRodney W. Grimes 		/* go to next entry */
2430df8bae1dSRodney W. Grimes 		start = entry->end;
2431df8bae1dSRodney W. Grimes 		entry = entry->next;
2432df8bae1dSRodney W. Grimes 	}
2433df8bae1dSRodney W. Grimes 	return (TRUE);
2434df8bae1dSRodney W. Grimes }
2435df8bae1dSRodney W. Grimes 
243686524867SJohn Dyson /*
2437df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2438df8bae1dSRodney W. Grimes  *
2439df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2440df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2441df8bae1dSRodney W. Grimes  */
2442f708ef1bSPoul-Henning Kamp static void
24431b40f8c0SMatthew Dillon vm_map_copy_entry(
24441b40f8c0SMatthew Dillon 	vm_map_t src_map,
24451b40f8c0SMatthew Dillon 	vm_map_t dst_map,
24461b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
24471b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2448df8bae1dSRodney W. Grimes {
2449c0877f10SJohn Dyson 	vm_object_t src_object;
2450c0877f10SJohn Dyson 
24519fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2452df8bae1dSRodney W. Grimes 		return;
2453df8bae1dSRodney W. Grimes 
2454df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2455df8bae1dSRodney W. Grimes 
2456df8bae1dSRodney W. Grimes 		/*
24570d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
24580d94caffSDavid Greenman 		 * write-protected.
2459df8bae1dSRodney W. Grimes 		 */
2460afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2461df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2462df8bae1dSRodney W. Grimes 			    src_entry->start,
2463df8bae1dSRodney W. Grimes 			    src_entry->end,
2464df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2465df8bae1dSRodney W. Grimes 		}
2466b18bfc3dSJohn Dyson 
2467df8bae1dSRodney W. Grimes 		/*
2468df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2469df8bae1dSRodney W. Grimes 		 */
24708aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2471a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
2472c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2473c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2474c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2475c0877f10SJohn Dyson 				vm_object_collapse(src_object);
247696fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2477c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2478c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2479a89c6258SAlan Cox 				}
2480a89c6258SAlan Cox 			}
2481b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2482069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2483e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2484c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2485afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2486afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2487b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2488b18bfc3dSJohn Dyson 		} else {
2489b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2490b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2491b18bfc3dSJohn Dyson 		}
2492df8bae1dSRodney W. Grimes 
2493df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2494df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
24950d94caffSDavid Greenman 	} else {
2496df8bae1dSRodney W. Grimes 		/*
2497df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
24980d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
24990d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2500df8bae1dSRodney W. Grimes 		 */
2501df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2502df8bae1dSRodney W. Grimes 	}
2503df8bae1dSRodney W. Grimes }
2504df8bae1dSRodney W. Grimes 
2505df8bae1dSRodney W. Grimes /*
25062a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
25072a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
25082a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
25092a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
25102a7be1b6SBrian Feldman  */
25112a7be1b6SBrian Feldman static void
25122a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
25132a7be1b6SBrian Feldman     vm_map_entry_t entry)
25142a7be1b6SBrian Feldman {
25152a7be1b6SBrian Feldman 	vm_size_t entrysize;
25162a7be1b6SBrian Feldman 	vm_offset_t newend;
25172a7be1b6SBrian Feldman 
25182a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
25192a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
25202a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
25212a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
25222a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
25232a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
2524b351299cSAndrew Gallatin 		newend = MIN(entry->end,
25252a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
25262a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
25272a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
25282a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
2529b351299cSAndrew Gallatin 		newend = MIN(entry->end,
25302a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
25312a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
25322a7be1b6SBrian Feldman 	}
25332a7be1b6SBrian Feldman }
25342a7be1b6SBrian Feldman 
25352a7be1b6SBrian Feldman /*
2536df8bae1dSRodney W. Grimes  * vmspace_fork:
2537df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2538df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2539df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2540df8bae1dSRodney W. Grimes  * values on the regions in that map.
2541df8bae1dSRodney W. Grimes  *
25422a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
25432a7be1b6SBrian Feldman  *
2544df8bae1dSRodney W. Grimes  * The source map must not be locked.
2545df8bae1dSRodney W. Grimes  */
2546df8bae1dSRodney W. Grimes struct vmspace *
25471b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2548df8bae1dSRodney W. Grimes {
2549c0877f10SJohn Dyson 	struct vmspace *vm2;
2550df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2551df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2552df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2553df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2554de5f6a77SJohn Dyson 	vm_object_t object;
2555df8bae1dSRodney W. Grimes 
2556df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2557df8bae1dSRodney W. Grimes 
25582d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
25592a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
25602a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
25612a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
2562df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
256347221757SJohn Dyson 	new_map->timestamp = 1;
2564df8bae1dSRodney W. Grimes 
2565abd498aaSBruce M Simpson 	/* Do not inherit the MAP_WIREFUTURE property. */
2566abd498aaSBruce M Simpson 	if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2567abd498aaSBruce M Simpson 		new_map->flags &= ~MAP_WIREFUTURE;
2568abd498aaSBruce M Simpson 
2569df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2570df8bae1dSRodney W. Grimes 
2571df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2572afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2573df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2574df8bae1dSRodney W. Grimes 
2575df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2576df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2577df8bae1dSRodney W. Grimes 			break;
2578df8bae1dSRodney W. Grimes 
2579df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2580df8bae1dSRodney W. Grimes 			/*
2581fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2582fed9a903SJohn Dyson 			 */
2583fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2584fed9a903SJohn Dyson 			if (object == NULL) {
2585fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2586c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2587fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
258815d2d313SAlan Cox 				old_entry->offset = 0;
25899a2f6362SAlan Cox 			}
25909a2f6362SAlan Cox 
25919a2f6362SAlan Cox 			/*
25929a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
25939a2f6362SAlan Cox 			 * to insure that a shadow object is created.
25949a2f6362SAlan Cox 			 */
25959a2f6362SAlan Cox 			vm_object_reference(object);
25969a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
25975069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
25985069bf57SJohn Dyson 					&old_entry->offset,
2599c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
26005069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2601d30344bdSIan Dowse 				/* Transfer the second reference too. */
2602d30344bdSIan Dowse 				vm_object_reference(
2603d30344bdSIan Dowse 				    old_entry->object.vm_object);
2604d30344bdSIan Dowse 				vm_object_deallocate(object);
26055069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2606fed9a903SJohn Dyson 			}
2607e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2608069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2609e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2610fed9a903SJohn Dyson 
2611fed9a903SJohn Dyson 			/*
2612ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2613df8bae1dSRodney W. Grimes 			 */
2614df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2615df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2616028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2617df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2618df8bae1dSRodney W. Grimes 
2619df8bae1dSRodney W. Grimes 			/*
26200d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
26210d94caffSDavid Greenman 			 * inserting at the end of the new map.
2622df8bae1dSRodney W. Grimes 			 */
2623df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2624df8bae1dSRodney W. Grimes 			    new_entry);
26252a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2626df8bae1dSRodney W. Grimes 
2627df8bae1dSRodney W. Grimes 			/*
2628df8bae1dSRodney W. Grimes 			 * Update the physical map
2629df8bae1dSRodney W. Grimes 			 */
2630df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2631df8bae1dSRodney W. Grimes 			    new_entry->start,
2632df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2633df8bae1dSRodney W. Grimes 			    old_entry->start);
2634df8bae1dSRodney W. Grimes 			break;
2635df8bae1dSRodney W. Grimes 
2636df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2637df8bae1dSRodney W. Grimes 			/*
2638df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2639df8bae1dSRodney W. Grimes 			 */
2640df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2641df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2642028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2643df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2644df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2645df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2646df8bae1dSRodney W. Grimes 			    new_entry);
26472a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
2648bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2649bd7e5f99SJohn Dyson 			    new_entry);
2650df8bae1dSRodney W. Grimes 			break;
2651df8bae1dSRodney W. Grimes 		}
2652df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2653df8bae1dSRodney W. Grimes 	}
2654df8bae1dSRodney W. Grimes 
2655df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2656df8bae1dSRodney W. Grimes 
2657df8bae1dSRodney W. Grimes 	return (vm2);
2658df8bae1dSRodney W. Grimes }
2659df8bae1dSRodney W. Grimes 
266094f7e29aSAlan Cox int
266194f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
266294f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
266394f7e29aSAlan Cox {
2664fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2665fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
266694f7e29aSAlan Cox 	vm_size_t init_ssize;
2667fd75d710SMarcel Moolenaar 	int orient, rv;
266891d5354aSJohn Baldwin 	rlim_t vmemlim;
266994f7e29aSAlan Cox 
2670fd75d710SMarcel Moolenaar 	/*
2671fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2672fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2673fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2674fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2675fd75d710SMarcel Moolenaar 	 */
2676fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2677fd75d710SMarcel Moolenaar 	cow &= ~orient;
2678fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2679fd75d710SMarcel Moolenaar 
2680fd75d710SMarcel Moolenaar 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
268194f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2682fd75d710SMarcel Moolenaar 
2683fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
268494f7e29aSAlan Cox 
268591d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
268691d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
268791d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
268891d5354aSJohn Baldwin 
268994f7e29aSAlan Cox 	vm_map_lock(map);
269094f7e29aSAlan Cox 
269194f7e29aSAlan Cox 	/* If addr is already mapped, no go */
269294f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
269394f7e29aSAlan Cox 		vm_map_unlock(map);
269494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
269594f7e29aSAlan Cox 	}
269694f7e29aSAlan Cox 
2697a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
269891d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2699a69ac174SMatthew Dillon 		vm_map_unlock(map);
2700a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2701a69ac174SMatthew Dillon 	}
2702a69ac174SMatthew Dillon 
2703fd75d710SMarcel Moolenaar 	/*
2704fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2705fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2706fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2707fd75d710SMarcel Moolenaar 	 * space is not protected.
270894f7e29aSAlan Cox 	 *
2709fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2710fd75d710SMarcel Moolenaar 	 * grow the stack.
271194f7e29aSAlan Cox 	 */
271294f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
271394f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
271494f7e29aSAlan Cox 		vm_map_unlock(map);
271594f7e29aSAlan Cox 		return (KERN_NO_SPACE);
271694f7e29aSAlan Cox 	}
271794f7e29aSAlan Cox 
2718fd75d710SMarcel Moolenaar 	/*
2719fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2720fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2721fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2722fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
272394f7e29aSAlan Cox 	 *
2724fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2725fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2726fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
272794f7e29aSAlan Cox 	 */
2728fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2729fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2730fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2731fd75d710SMarcel Moolenaar 		bot = addrbos;
2732fd75d710SMarcel Moolenaar 	else
2733fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2734fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2735fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
273694f7e29aSAlan Cox 
2737fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
273894f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
273929b45e9eSAlan Cox 		if (prev_entry != &map->header)
2740fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2741fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2742fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
274394f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2744b21a0008SMarcel Moolenaar 
2745fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2746fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2747fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2748fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2749fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
275094f7e29aSAlan Cox 	}
275194f7e29aSAlan Cox 
275294f7e29aSAlan Cox 	vm_map_unlock(map);
275394f7e29aSAlan Cox 	return (rv);
275494f7e29aSAlan Cox }
275594f7e29aSAlan Cox 
275694f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
275794f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
275894f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
275994f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
276094f7e29aSAlan Cox  * the grow function in vm_machdep.c).
276194f7e29aSAlan Cox  */
276294f7e29aSAlan Cox int
276394f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
276494f7e29aSAlan Cox {
2765b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2766b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
276794f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
276894f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
276994f7e29aSAlan Cox 	vm_offset_t end;
2770b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
277191d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2772b21a0008SMarcel Moolenaar 	int is_procstack, rv;
277323955314SAlfred Perlstein 
277494f7e29aSAlan Cox Retry:
277591d5354aSJohn Baldwin 	PROC_LOCK(p);
277691d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2777bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
277891d5354aSJohn Baldwin 	PROC_UNLOCK(p);
277991d5354aSJohn Baldwin 
278094f7e29aSAlan Cox 	vm_map_lock_read(map);
278194f7e29aSAlan Cox 
278294f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
278394f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
278494f7e29aSAlan Cox 		vm_map_unlock_read(map);
27850cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
278694f7e29aSAlan Cox 	}
278794f7e29aSAlan Cox 
2788b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2789b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2790b21a0008SMarcel Moolenaar 		/*
2791b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2792b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2793b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2794b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
279594f7e29aSAlan Cox 		 */
2796b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
279794f7e29aSAlan Cox 			vm_map_unlock_read(map);
27980cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
279994f7e29aSAlan Cox 		}
2800b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2801b21a0008SMarcel Moolenaar 	} else {
2802b21a0008SMarcel Moolenaar 		/*
2803b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2804b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2805b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2806b21a0008SMarcel Moolenaar 		 * select one.
2807b21a0008SMarcel Moolenaar 		 */
2808b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2809b21a0008SMarcel Moolenaar 			/*
2810b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2811b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2812b21a0008SMarcel Moolenaar 			 */
2813b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
2814b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
2815b21a0008SMarcel Moolenaar 			else
2816b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
2817b21a0008SMarcel Moolenaar 		} else
2818b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
2819b21a0008SMarcel Moolenaar 	}
282094f7e29aSAlan Cox 
2821b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2822b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2823b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
2824b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
2825b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
282694f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2827b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
2828b21a0008SMarcel Moolenaar 	} else {
2829b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
283008667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
2831b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
2832b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
2833fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2834b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
2835b21a0008SMarcel Moolenaar 	}
2836b21a0008SMarcel Moolenaar 
283794f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
283894f7e29aSAlan Cox 		vm_map_unlock_read(map);
28390cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
284094f7e29aSAlan Cox 	}
284194f7e29aSAlan Cox 
2842b21a0008SMarcel Moolenaar 	/*
2843b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
2844b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
2845b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
2846b21a0008SMarcel Moolenaar 	 * and is probably an error.
284794f7e29aSAlan Cox 	 *
2848b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
2849b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
285094f7e29aSAlan Cox 	 */
2851b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
285225adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
285394f7e29aSAlan Cox 			goto Retry;
285494f7e29aSAlan Cox 
2855b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
285694f7e29aSAlan Cox 
285794f7e29aSAlan Cox 		vm_map_unlock(map);
28580cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
285994f7e29aSAlan Cox 	}
286094f7e29aSAlan Cox 
2861b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
286294f7e29aSAlan Cox 
2863b21a0008SMarcel Moolenaar 	/*
2864b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
2865b21a0008SMarcel Moolenaar 	 * limit.
286694f7e29aSAlan Cox 	 */
286791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
286894f7e29aSAlan Cox 		vm_map_unlock_read(map);
28690cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
287094f7e29aSAlan Cox 	}
287194f7e29aSAlan Cox 
287294f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2873cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
2874b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
287594f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
287691d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
287791d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
287894f7e29aSAlan Cox 	}
287994f7e29aSAlan Cox 
2880a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
288191d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
2882a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
2883a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2884a69ac174SMatthew Dillon 	}
2885a69ac174SMatthew Dillon 
288625adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
288794f7e29aSAlan Cox 		goto Retry;
288894f7e29aSAlan Cox 
2889b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2890b21a0008SMarcel Moolenaar 		/*
2891b21a0008SMarcel Moolenaar 		 * Growing downward.
2892b21a0008SMarcel Moolenaar 		 */
289394f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
289494f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
289594f7e29aSAlan Cox 
2896b21a0008SMarcel Moolenaar 		/*
2897b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
2898b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
289994f7e29aSAlan Cox 		 */
290094f7e29aSAlan Cox 		if (addr < end) {
2901b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
290294f7e29aSAlan Cox 			addr = end;
290394f7e29aSAlan Cox 		}
290494f7e29aSAlan Cox 
290594f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
290605ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
290794f7e29aSAlan Cox 
290894f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
290994f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
291029b45e9eSAlan Cox 			if (prev_entry != &map->header)
291129b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
2912b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
2913b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2914b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2915b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
2916b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
2917b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
2918b21a0008SMarcel Moolenaar 			    grow_amount;
2919b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2920b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
292194f7e29aSAlan Cox 		}
2922b21a0008SMarcel Moolenaar 	} else {
2923b21a0008SMarcel Moolenaar 		/*
2924b21a0008SMarcel Moolenaar 		 * Growing upward.
2925b21a0008SMarcel Moolenaar 		 */
2926b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
2927b21a0008SMarcel Moolenaar 
2928b21a0008SMarcel Moolenaar 		/*
2929b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
2930b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
2931b21a0008SMarcel Moolenaar 		 */
2932b21a0008SMarcel Moolenaar 		if (addr > end) {
2933b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
2934b21a0008SMarcel Moolenaar 			addr = end;
293594f7e29aSAlan Cox 		}
293694f7e29aSAlan Cox 
2937b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
2938b21a0008SMarcel Moolenaar 
2939b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
2940b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
2941b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
294257a21abaSAlan Cox 		    stack_entry->offset,
2943b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2944b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
294508667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
2946b21a0008SMarcel Moolenaar 			/* Update the current entry. */
2947b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
2948199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
29490164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
2950b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
2951b21a0008SMarcel Moolenaar 
2952b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
2953b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
2954b21a0008SMarcel Moolenaar 		} else
2955b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
2956b21a0008SMarcel Moolenaar 	}
2957b21a0008SMarcel Moolenaar 
2958b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
2959b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
2960b21a0008SMarcel Moolenaar 
296194f7e29aSAlan Cox 	vm_map_unlock(map);
2962b21a0008SMarcel Moolenaar 
2963abd498aaSBruce M Simpson 	/*
2964abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2965abd498aaSBruce M Simpson 	 */
2966b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2967b21a0008SMarcel Moolenaar 		vm_map_wire(map,
2968b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2969b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2970b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
2971b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2972b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2973b21a0008SMarcel Moolenaar 	}
2974abd498aaSBruce M Simpson 
29750cddd8f0SMatthew Dillon 	return (rv);
297694f7e29aSAlan Cox }
297794f7e29aSAlan Cox 
2978df8bae1dSRodney W. Grimes /*
29795856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
29805856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
29815856e12eSJohn Dyson  */
29825856e12eSJohn Dyson void
29833ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
29841b40f8c0SMatthew Dillon {
29855856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
29865856e12eSJohn Dyson 	struct vmspace *newvmspace;
29875856e12eSJohn Dyson 
29883ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
298951ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
29905856e12eSJohn Dyson 	/*
29915856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
29925856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
29935856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
29945856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
29955856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
29965856e12eSJohn Dyson 	 */
299757051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
29985856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
299957051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
3000b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
3001b40ce416SJulian Elischer 		pmap_activate(curthread);
3002b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
30035856e12eSJohn Dyson }
30045856e12eSJohn Dyson 
30055856e12eSJohn Dyson /*
30065856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
30075856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
30085856e12eSJohn Dyson  */
30095856e12eSJohn Dyson void
30101b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
30111b40f8c0SMatthew Dillon {
30125856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
30135856e12eSJohn Dyson 	struct vmspace *newvmspace;
30145856e12eSJohn Dyson 
30155856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
30165856e12eSJohn Dyson 		return;
30175856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
301857051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
30195856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
302057051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
3021b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
3022b40ce416SJulian Elischer 		pmap_activate(curthread);
3023b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
30245856e12eSJohn Dyson }
30255856e12eSJohn Dyson 
30265856e12eSJohn Dyson /*
3027df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3028df8bae1dSRodney W. Grimes  *
3029df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3030df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3031df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3032df8bae1dSRodney W. Grimes  *	type specified.
3033df8bae1dSRodney W. Grimes  *
3034df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3035df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3036df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3037df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3038df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3039df8bae1dSRodney W. Grimes  *
3040df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3041df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3042df8bae1dSRodney W. Grimes  *
3043df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3044df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3045df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3046df8bae1dSRodney W. Grimes  *	remain the same.
3047df8bae1dSRodney W. Grimes  */
3048df8bae1dSRodney W. Grimes int
3049b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3050b9dcd593SBruce Evans 	      vm_offset_t vaddr,
305147221757SJohn Dyson 	      vm_prot_t fault_typea,
3052b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3053b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3054b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3055b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
30562d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3057df8bae1dSRodney W. Grimes {
3058c0877f10SJohn Dyson 	vm_map_entry_t entry;
3059c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3060c0877f10SJohn Dyson 	vm_prot_t prot;
306147221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
3062df8bae1dSRodney W. Grimes 
3063df8bae1dSRodney W. Grimes RetryLookup:;
3064df8bae1dSRodney W. Grimes 	/*
3065df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
3066df8bae1dSRodney W. Grimes 	 */
3067df8bae1dSRodney W. Grimes 
3068df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3069df8bae1dSRodney W. Grimes #define	RETURN(why) \
3070df8bae1dSRodney W. Grimes 		{ \
3071df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
3072df8bae1dSRodney W. Grimes 		return (why); \
3073df8bae1dSRodney W. Grimes 		}
3074df8bae1dSRodney W. Grimes 
3075df8bae1dSRodney W. Grimes 	/*
30760d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
30770d94caffSDavid Greenman 	 * blown lookup routine.
3078df8bae1dSRodney W. Grimes 	 */
30794e94f402SAlan Cox 	entry = map->root;
3080df8bae1dSRodney W. Grimes 	*out_entry = entry;
30814e94f402SAlan Cox 	if (entry == NULL ||
3082df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
3083df8bae1dSRodney W. Grimes 		/*
30840d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
30850d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
3086df8bae1dSRodney W. Grimes 		 */
30874e94f402SAlan Cox 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
3088df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
3089df8bae1dSRodney W. Grimes 
30904e94f402SAlan Cox 		entry = *out_entry;
3091df8bae1dSRodney W. Grimes 	}
3092b7b2aac2SJohn Dyson 
3093df8bae1dSRodney W. Grimes 	/*
3094df8bae1dSRodney W. Grimes 	 * Handle submaps.
3095df8bae1dSRodney W. Grimes 	 */
3096afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3097df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3098df8bae1dSRodney W. Grimes 
3099df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3100df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3101df8bae1dSRodney W. Grimes 		goto RetryLookup;
3102df8bae1dSRodney W. Grimes 	}
3103a04c970aSJohn Dyson 
3104df8bae1dSRodney W. Grimes 	/*
31050d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3106a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
3107a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
3108a04c970aSJohn Dyson 	 * COW for debuggers.
3109df8bae1dSRodney W. Grimes 	 */
3110480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
3111480ba2f5SJohn Dyson 		prot = entry->max_protection;
3112480ba2f5SJohn Dyson 	else
3113df8bae1dSRodney W. Grimes 		prot = entry->protection;
311447221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
311547221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
311647221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
311747221757SJohn Dyson 	}
31182ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
311947221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
31202ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
312147221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
3122df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
3123a04c970aSJohn Dyson 	}
3124df8bae1dSRodney W. Grimes 
3125df8bae1dSRodney W. Grimes 	/*
31260d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
31270d94caffSDavid Greenman 	 * accesses.
3128df8bae1dSRodney W. Grimes 	 */
312905f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
313005f0fdd2SPoul-Henning Kamp 	if (*wired)
3131df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
3132df8bae1dSRodney W. Grimes 
3133df8bae1dSRodney W. Grimes 	/*
3134df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3135df8bae1dSRodney W. Grimes 	 */
3136afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3137df8bae1dSRodney W. Grimes 		/*
31380d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3139ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3140df8bae1dSRodney W. Grimes 		 *
31410d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
31420d94caffSDavid Greenman 		 * permissions allowed.
3143df8bae1dSRodney W. Grimes 		 */
3144df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
3145df8bae1dSRodney W. Grimes 			/*
31460d94caffSDavid Greenman 			 * Make a new object, and place it in the object
31470d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3148ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
31490d94caffSDavid Greenman 			 * object.
3150df8bae1dSRodney W. Grimes 			 */
315125adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3152df8bae1dSRodney W. Grimes 				goto RetryLookup;
31539917e010SAlan Cox 
3154df8bae1dSRodney W. Grimes 			vm_object_shadow(
3155df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
3156df8bae1dSRodney W. Grimes 			    &entry->offset,
3157c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
3158afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
31599917e010SAlan Cox 
31609b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
31610d94caffSDavid Greenman 		} else {
3162df8bae1dSRodney W. Grimes 			/*
31630d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
31640d94caffSDavid Greenman 			 * don't allow writes.
3165df8bae1dSRodney W. Grimes 			 */
31662d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3167df8bae1dSRodney W. Grimes 		}
3168df8bae1dSRodney W. Grimes 	}
31692d8acc0fSJohn Dyson 
3170df8bae1dSRodney W. Grimes 	/*
3171df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3172df8bae1dSRodney W. Grimes 	 */
31734e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
31744e71e795SMatthew Dillon 	    !map->system_map) {
317525adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3176df8bae1dSRodney W. Grimes 			goto RetryLookup;
317724a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
3178c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
3179df8bae1dSRodney W. Grimes 		entry->offset = 0;
31809b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3181df8bae1dSRodney W. Grimes 	}
3182b5b40fa6SJohn Dyson 
3183df8bae1dSRodney W. Grimes 	/*
31840d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
31850d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3186df8bae1dSRodney W. Grimes 	 */
31879b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3188df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3189df8bae1dSRodney W. Grimes 
3190df8bae1dSRodney W. Grimes 	*out_prot = prot;
3191df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3192df8bae1dSRodney W. Grimes 
3193df8bae1dSRodney W. Grimes #undef	RETURN
3194df8bae1dSRodney W. Grimes }
3195df8bae1dSRodney W. Grimes 
3196df8bae1dSRodney W. Grimes /*
319719dc5607STor Egge  *	vm_map_lookup_locked:
319819dc5607STor Egge  *
319919dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
320019dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
320119dc5607STor Egge  */
320219dc5607STor Egge int
320319dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
320419dc5607STor Egge 		     vm_offset_t vaddr,
320519dc5607STor Egge 		     vm_prot_t fault_typea,
320619dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
320719dc5607STor Egge 		     vm_object_t *object,	/* OUT */
320819dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
320919dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
321019dc5607STor Egge 		     boolean_t *wired)		/* OUT */
321119dc5607STor Egge {
321219dc5607STor Egge 	vm_map_entry_t entry;
321319dc5607STor Egge 	vm_map_t map = *var_map;
321419dc5607STor Egge 	vm_prot_t prot;
321519dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
321619dc5607STor Egge 
321719dc5607STor Egge 	/*
321819dc5607STor Egge 	 * If the map has an interesting hint, try it before calling full
321919dc5607STor Egge 	 * blown lookup routine.
322019dc5607STor Egge 	 */
322119dc5607STor Egge 	entry = map->root;
322219dc5607STor Egge 	*out_entry = entry;
322319dc5607STor Egge 	if (entry == NULL ||
322419dc5607STor Egge 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
322519dc5607STor Egge 		/*
322619dc5607STor Egge 		 * Entry was either not a valid hint, or the vaddr was not
322719dc5607STor Egge 		 * contained in the entry, so do a full lookup.
322819dc5607STor Egge 		 */
322919dc5607STor Egge 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
323019dc5607STor Egge 			return (KERN_INVALID_ADDRESS);
323119dc5607STor Egge 
323219dc5607STor Egge 		entry = *out_entry;
323319dc5607STor Egge 	}
323419dc5607STor Egge 
323519dc5607STor Egge 	/*
323619dc5607STor Egge 	 * Fail if the entry refers to a submap.
323719dc5607STor Egge 	 */
323819dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
323919dc5607STor Egge 		return (KERN_FAILURE);
324019dc5607STor Egge 
324119dc5607STor Egge 	/*
324219dc5607STor Egge 	 * Check whether this task is allowed to have this page.
324319dc5607STor Egge 	 * Note the special case for MAP_ENTRY_COW
324419dc5607STor Egge 	 * pages with an override.  This is to implement a forced
324519dc5607STor Egge 	 * COW for debuggers.
324619dc5607STor Egge 	 */
324719dc5607STor Egge 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
324819dc5607STor Egge 		prot = entry->max_protection;
324919dc5607STor Egge 	else
325019dc5607STor Egge 		prot = entry->protection;
325119dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
325219dc5607STor Egge 	if ((fault_type & prot) != fault_type)
325319dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
325419dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
325519dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
325619dc5607STor Egge 	    (fault_type & VM_PROT_WRITE) &&
325719dc5607STor Egge 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0)
325819dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
325919dc5607STor Egge 
326019dc5607STor Egge 	/*
326119dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
326219dc5607STor Egge 	 * accesses.
326319dc5607STor Egge 	 */
326419dc5607STor Egge 	*wired = (entry->wired_count != 0);
326519dc5607STor Egge 	if (*wired)
326619dc5607STor Egge 		prot = fault_type = entry->protection;
326719dc5607STor Egge 
326819dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
326919dc5607STor Egge 		/*
327019dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
327119dc5607STor Egge 		 */
327219dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
327319dc5607STor Egge 			return (KERN_FAILURE);
327419dc5607STor Egge 		/*
327519dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
327619dc5607STor Egge 		 * don't allow writes.
327719dc5607STor Egge 		 */
327819dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
327919dc5607STor Egge 	}
328019dc5607STor Egge 
328119dc5607STor Egge 	/*
328219dc5607STor Egge 	 * Fail if an object should be created.
328319dc5607STor Egge 	 */
328419dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
328519dc5607STor Egge 		return (KERN_FAILURE);
328619dc5607STor Egge 
328719dc5607STor Egge 	/*
328819dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
328919dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
329019dc5607STor Egge 	 */
329119dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
329219dc5607STor Egge 	*object = entry->object.vm_object;
329319dc5607STor Egge 
329419dc5607STor Egge 	*out_prot = prot;
329519dc5607STor Egge 	return (KERN_SUCCESS);
329619dc5607STor Egge }
329719dc5607STor Egge 
329819dc5607STor Egge /*
3299df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3300df8bae1dSRodney W. Grimes  *
3301df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3302df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3303df8bae1dSRodney W. Grimes  */
33040d94caffSDavid Greenman void
33051b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3306df8bae1dSRodney W. Grimes {
3307df8bae1dSRodney W. Grimes 	/*
3308df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3309df8bae1dSRodney W. Grimes 	 */
3310df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3311df8bae1dSRodney W. Grimes }
3312df8bae1dSRodney W. Grimes 
3313c7c34a24SBruce Evans #include "opt_ddb.h"
3314c3cb3e12SDavid Greenman #ifdef DDB
3315c7c34a24SBruce Evans #include <sys/kernel.h>
3316c7c34a24SBruce Evans 
3317c7c34a24SBruce Evans #include <ddb/ddb.h>
3318c7c34a24SBruce Evans 
3319df8bae1dSRodney W. Grimes /*
3320df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3321df8bae1dSRodney W. Grimes  */
3322c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3323df8bae1dSRodney W. Grimes {
332495e5e988SJohn Dyson 	static int nlines;
3325c7c34a24SBruce Evans 	/* XXX convert args. */
3326c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3327c7c34a24SBruce Evans 	boolean_t full = have_addr;
3328df8bae1dSRodney W. Grimes 
3329c0877f10SJohn Dyson 	vm_map_entry_t entry;
3330c7c34a24SBruce Evans 
3331e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3332e5f251d2SAlan Cox 	    (void *)map,
3333101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
333495e5e988SJohn Dyson 	nlines++;
3335df8bae1dSRodney W. Grimes 
3336c7c34a24SBruce Evans 	if (!full && db_indent)
3337df8bae1dSRodney W. Grimes 		return;
3338df8bae1dSRodney W. Grimes 
3339c7c34a24SBruce Evans 	db_indent += 2;
3340df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3341df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3342fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3343fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
334495e5e988SJohn Dyson 		nlines++;
3345e5f251d2SAlan Cox 		{
3346df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3347df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
33480d94caffSDavid Greenman 
334995e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3350df8bae1dSRodney W. Grimes 			    entry->protection,
3351df8bae1dSRodney W. Grimes 			    entry->max_protection,
33528aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3353df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
335495e5e988SJohn Dyson 				db_printf(", wired");
3355df8bae1dSRodney W. Grimes 		}
33569fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3357cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
33589fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3359cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
336095e5e988SJohn Dyson 			nlines++;
3361df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
33629fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
33639fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3364c7c34a24SBruce Evans 				db_indent += 2;
3365101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
33669fdfe602SMatthew Dillon 					     entry->object.sub_map,
3367914181e7SBruce Evans 					     full, 0, (char *)0);
3368c7c34a24SBruce Evans 				db_indent -= 2;
3369df8bae1dSRodney W. Grimes 			}
33700d94caffSDavid Greenman 		} else {
3371cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3372101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3373cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3374afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3375c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3376afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3377c7c34a24SBruce Evans 			db_printf("\n");
337895e5e988SJohn Dyson 			nlines++;
3379df8bae1dSRodney W. Grimes 
3380df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3381df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3382df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3383c7c34a24SBruce Evans 				db_indent += 2;
3384101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3385101eeb7fSBruce Evans 						entry->object.vm_object,
3386914181e7SBruce Evans 						full, 0, (char *)0);
338795e5e988SJohn Dyson 				nlines += 4;
3388c7c34a24SBruce Evans 				db_indent -= 2;
3389df8bae1dSRodney W. Grimes 			}
3390df8bae1dSRodney W. Grimes 		}
3391df8bae1dSRodney W. Grimes 	}
3392c7c34a24SBruce Evans 	db_indent -= 2;
339395e5e988SJohn Dyson 	if (db_indent == 0)
339495e5e988SJohn Dyson 		nlines = 0;
3395df8bae1dSRodney W. Grimes }
339695e5e988SJohn Dyson 
339795e5e988SJohn Dyson 
339895e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
339995e5e988SJohn Dyson {
340095e5e988SJohn Dyson 	struct proc *p;
340195e5e988SJohn Dyson 
340295e5e988SJohn Dyson 	if (have_addr) {
340395e5e988SJohn Dyson 		p = (struct proc *) addr;
340495e5e988SJohn Dyson 	} else {
340595e5e988SJohn Dyson 		p = curproc;
340695e5e988SJohn Dyson 	}
340795e5e988SJohn Dyson 
3408ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3409ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3410b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
341195e5e988SJohn Dyson 
3412101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
341395e5e988SJohn Dyson }
341495e5e988SJohn Dyson 
3415c7c34a24SBruce Evans #endif /* DDB */
3416