xref: /freebsd/sys/vm/vm_map.c (revision bfee999d6a4f3607fcb5053668a61e81da0d2f1d)
1df8bae1dSRodney W. Grimes /*
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 3. All advertising materials mentioning features or use of this software
175929bcfaSPhilippe Charnier  *    must display the following acknowledgement:
18df8bae1dSRodney W. Grimes  *	This product includes software developed by the University of
19df8bae1dSRodney W. Grimes  *	California, Berkeley and its contributors.
20df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
21df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
22df8bae1dSRodney W. Grimes  *    without specific prior written permission.
23df8bae1dSRodney W. Grimes  *
24df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
35df8bae1dSRodney W. Grimes  *
363c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  *
39df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
40df8bae1dSRodney W. Grimes  * All rights reserved.
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
43df8bae1dSRodney W. Grimes  *
44df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
45df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
46df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
47df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
48df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
51df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
52df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
55df8bae1dSRodney W. Grimes  *
56df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
57df8bae1dSRodney W. Grimes  *  School of Computer Science
58df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
59df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
60df8bae1dSRodney W. Grimes  *
61df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
62df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65df8bae1dSRodney W. Grimes /*
66df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
67df8bae1dSRodney W. Grimes  */
68df8bae1dSRodney W. Grimes 
69874651b1SDavid E. O'Brien #include <sys/cdefs.h>
70874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
71874651b1SDavid E. O'Brien 
72df8bae1dSRodney W. Grimes #include <sys/param.h>
73df8bae1dSRodney W. Grimes #include <sys/systm.h>
7461d80e90SJohn Baldwin #include <sys/ktr.h>
75fb919e4dSMark Murray #include <sys/lock.h>
76fb919e4dSMark Murray #include <sys/mutex.h>
77b5e8ce9fSBruce Evans #include <sys/proc.h>
78efeaf95aSDavid Greenman #include <sys/vmmeter.h>
79867a482dSJohn Dyson #include <sys/mman.h>
801efb74fbSJohn Dyson #include <sys/vnode.h>
812267af78SJulian Elischer #include <sys/resourcevar.h>
823fde38dfSMike Silbersack #include <sys/file.h>
8305ba50f5SJake Burkholder #include <sys/sysent.h>
843db161e0SMatthew Dillon #include <sys/shm.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9247221757SJohn Dyson #include <vm/vm_pager.h>
9326f9a767SRodney W. Grimes #include <vm/vm_kern.h>
94efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9521cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
96670d17b5SJeff Roberson #include <vm/uma.h>
97df8bae1dSRodney W. Grimes 
98df8bae1dSRodney W. Grimes /*
99df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
100df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
101df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
102df8bae1dSRodney W. Grimes  *	memory from one map to another.
103df8bae1dSRodney W. Grimes  *
104df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
105df8bae1dSRodney W. Grimes  *
106df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
107df8bae1dSRodney W. Grimes  *	entries; a single hint is used to speed up lookups.
108df8bae1dSRodney W. Grimes  *
109956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
110df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
111df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
112df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
113df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
114df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
115df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
116df8bae1dSRodney W. Grimes  *
117df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
118ad5fca3bSAlan Cox  *	by copying VM object references from one map to
119df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
120df8bae1dSRodney W. Grimes  */
121df8bae1dSRodney W. Grimes 
122df8bae1dSRodney W. Grimes /*
123df8bae1dSRodney W. Grimes  *	vm_map_startup:
124df8bae1dSRodney W. Grimes  *
125df8bae1dSRodney W. Grimes  *	Initialize the vm_map module.  Must be called before
126df8bae1dSRodney W. Grimes  *	any other vm_map routines.
127df8bae1dSRodney W. Grimes  *
128df8bae1dSRodney W. Grimes  *	Map and entry structures are allocated from the general
129df8bae1dSRodney W. Grimes  *	purpose memory pool with some exceptions:
130df8bae1dSRodney W. Grimes  *
131df8bae1dSRodney W. Grimes  *	- The kernel map and kmem submap are allocated statically.
132df8bae1dSRodney W. Grimes  *	- Kernel map entries are allocated out of a static pool.
133df8bae1dSRodney W. Grimes  *
134df8bae1dSRodney W. Grimes  *	These restrictions are necessary since malloc() uses the
135df8bae1dSRodney W. Grimes  *	maps and requires map entries.
136df8bae1dSRodney W. Grimes  */
137df8bae1dSRodney W. Grimes 
1383a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1398355f576SJeff Roberson static uma_zone_t mapentzone;
1408355f576SJeff Roberson static uma_zone_t kmapentzone;
1418355f576SJeff Roberson static uma_zone_t mapzone;
1428355f576SJeff Roberson static uma_zone_t vmspace_zone;
1438355f576SJeff Roberson static struct vm_object kmapentobj;
1448355f576SJeff Roberson static void vmspace_zinit(void *mem, int size);
1458355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
1468355f576SJeff Roberson static void vm_map_zinit(void *mem, int size);
1478355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
1488355f576SJeff Roberson static void _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max);
1491fc43fd1SAlan Cox 
1508355f576SJeff Roberson #ifdef INVARIANTS
1518355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1528355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1538355f576SJeff Roberson #endif
154b18bfc3dSJohn Dyson 
1550d94caffSDavid Greenman void
1561b40f8c0SMatthew Dillon vm_map_startup(void)
157df8bae1dSRodney W. Grimes {
1583a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1598355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1608355f576SJeff Roberson #ifdef INVARIANTS
1618355f576SJeff Roberson 	    vm_map_zdtor,
1628355f576SJeff Roberson #else
1638355f576SJeff Roberson 	    NULL,
1648355f576SJeff Roberson #endif
1658355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1668355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
167670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
16818aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
16918aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
170670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
171670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
172670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
1738355f576SJeff Roberson 	uma_prealloc(mapentzone, MAX_MAPENT);
174df8bae1dSRodney W. Grimes }
175df8bae1dSRodney W. Grimes 
1768355f576SJeff Roberson static void
1778355f576SJeff Roberson vmspace_zfini(void *mem, int size)
1788355f576SJeff Roberson {
1798355f576SJeff Roberson 	struct vmspace *vm;
1808355f576SJeff Roberson 
1818355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1828355f576SJeff Roberson 
1838355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
1848355f576SJeff Roberson }
1858355f576SJeff Roberson 
1868355f576SJeff Roberson static void
1878355f576SJeff Roberson vmspace_zinit(void *mem, int size)
1888355f576SJeff Roberson {
1898355f576SJeff Roberson 	struct vmspace *vm;
1908355f576SJeff Roberson 
1918355f576SJeff Roberson 	vm = (struct vmspace *)mem;
1928355f576SJeff Roberson 
1938355f576SJeff Roberson 	vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map));
1948355f576SJeff Roberson }
1958355f576SJeff Roberson 
1968355f576SJeff Roberson static void
1978355f576SJeff Roberson vm_map_zfini(void *mem, int size)
1988355f576SJeff Roberson {
1998355f576SJeff Roberson 	vm_map_t map;
2008355f576SJeff Roberson 
2018355f576SJeff Roberson 	map = (vm_map_t)mem;
20236daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
2038355f576SJeff Roberson 	lockdestroy(&map->lock);
2048355f576SJeff Roberson }
2058355f576SJeff Roberson 
2068355f576SJeff Roberson static void
2078355f576SJeff Roberson vm_map_zinit(void *mem, int size)
2088355f576SJeff Roberson {
2098355f576SJeff Roberson 	vm_map_t map;
2108355f576SJeff Roberson 
2118355f576SJeff Roberson 	map = (vm_map_t)mem;
2128355f576SJeff Roberson 	map->nentries = 0;
2138355f576SJeff Roberson 	map->size = 0;
2148355f576SJeff Roberson 	map->infork = 0;
215d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
216515630b1SAlan Cox 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
2178355f576SJeff Roberson }
2188355f576SJeff Roberson 
2198355f576SJeff Roberson #ifdef INVARIANTS
2208355f576SJeff Roberson static void
2218355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2228355f576SJeff Roberson {
2238355f576SJeff Roberson 	struct vmspace *vm;
2248355f576SJeff Roberson 
2258355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2268355f576SJeff Roberson 
2278355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2288355f576SJeff Roberson }
2298355f576SJeff Roberson static void
2308355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2318355f576SJeff Roberson {
2328355f576SJeff Roberson 	vm_map_t map;
2338355f576SJeff Roberson 
2348355f576SJeff Roberson 	map = (vm_map_t)mem;
2358355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2368355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2378355f576SJeff Roberson 	    map, map->nentries));
2388355f576SJeff Roberson 	KASSERT(map->size == 0,
2398355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2409eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2418355f576SJeff Roberson 	KASSERT(map->infork == 0,
2428355f576SJeff Roberson 	    ("map %p infork == %d on free.",
2438355f576SJeff Roberson 	    map, map->infork));
2448355f576SJeff Roberson }
2458355f576SJeff Roberson #endif	/* INVARIANTS */
2468355f576SJeff Roberson 
247df8bae1dSRodney W. Grimes /*
248df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
249df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
250df8bae1dSRodney W. Grimes  * The remaining fields must be initialized by the caller.
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 	pmap_pinit(vmspace_pmap(vm));
262b1028ad1SLuoqi Chen 	vm->vm_map.pmap = vmspace_pmap(vm);		/* XXX */
263df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2642d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
265389d2b6eSMatthew Dillon 	vm->vm_exitingcnt = 0;
266df8bae1dSRodney W. Grimes 	return (vm);
267df8bae1dSRodney W. Grimes }
268df8bae1dSRodney W. Grimes 
269df8bae1dSRodney W. Grimes void
2701b40f8c0SMatthew Dillon vm_init2(void)
2711b40f8c0SMatthew Dillon {
2729e7c1bceSPeter Wemm 	uma_zone_set_obj(kmapentzone, &kmapentobj, lmin(cnt.v_page_count,
2733fde38dfSMike Silbersack 	    (VM_MAX_KERNEL_ADDRESS - KERNBASE) / PAGE_SIZE) / 8 +
2743fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
2758355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2768355f576SJeff Roberson #ifdef INVARIANTS
2778355f576SJeff Roberson 	    vmspace_zdtor,
2788355f576SJeff Roberson #else
2798355f576SJeff Roberson 	    NULL,
2808355f576SJeff Roberson #endif
2818355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
282ba9be04cSJohn Dyson 	pmap_init2();
2833075778bSJohn Dyson }
2843075778bSJohn Dyson 
285582ec34cSAlfred Perlstein static __inline void
286582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
287df8bae1dSRodney W. Grimes {
28821c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
2893db161e0SMatthew Dillon 
2903db161e0SMatthew Dillon 	/*
2913db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
2923db161e0SMatthew Dillon 	 * exit1().
2933db161e0SMatthew Dillon 	 */
2943db161e0SMatthew Dillon 	shmexit(vm);
2953db161e0SMatthew Dillon 
29630dcfc09SJohn Dyson 	/*
297df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
2980d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
2990d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
300df8bae1dSRodney W. Grimes 	 */
301df8bae1dSRodney W. Grimes 	vm_map_lock(&vm->vm_map);
302df8bae1dSRodney W. Grimes 	(void) vm_map_delete(&vm->vm_map, vm->vm_map.min_offset,
303df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
304a1f6d91cSDavid Greenman 	vm_map_unlock(&vm->vm_map);
3058355f576SJeff Roberson 
306b1028ad1SLuoqi Chen 	pmap_release(vmspace_pmap(vm));
3078355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
308df8bae1dSRodney W. Grimes }
309582ec34cSAlfred Perlstein 
310582ec34cSAlfred Perlstein void
311582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
312582ec34cSAlfred Perlstein {
313582ec34cSAlfred Perlstein 	GIANT_REQUIRED;
314582ec34cSAlfred Perlstein 
315582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
316582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
317582ec34cSAlfred Perlstein 
318389d2b6eSMatthew Dillon 	if (--vm->vm_refcnt == 0 && vm->vm_exitingcnt == 0)
319582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
320582ec34cSAlfred Perlstein }
321582ec34cSAlfred Perlstein 
322582ec34cSAlfred Perlstein void
323582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
324582ec34cSAlfred Perlstein {
325334f7061SPeter Wemm 	struct vmspace *vm;
326582ec34cSAlfred Perlstein 
327334f7061SPeter Wemm 	GIANT_REQUIRED;
328334f7061SPeter Wemm 	vm = p->p_vmspace;
329334f7061SPeter Wemm 	p->p_vmspace = NULL;
330389d2b6eSMatthew Dillon 
331389d2b6eSMatthew Dillon 	/*
332389d2b6eSMatthew Dillon 	 * cleanup by parent process wait()ing on exiting child.  vm_refcnt
333389d2b6eSMatthew Dillon 	 * may not be 0 (e.g. fork() and child exits without exec()ing).
334389d2b6eSMatthew Dillon 	 * exitingcnt may increment above 0 and drop back down to zero
335389d2b6eSMatthew Dillon 	 * several times while vm_refcnt is held non-zero.  vm_refcnt
336389d2b6eSMatthew Dillon 	 * may also increment above 0 and drop back down to zero several
337389d2b6eSMatthew Dillon 	 * times while vm_exitingcnt is held non-zero.
338389d2b6eSMatthew Dillon 	 *
339389d2b6eSMatthew Dillon 	 * The last wait on the exiting child's vmspace will clean up
340389d2b6eSMatthew Dillon 	 * the remainder of the vmspace.
341389d2b6eSMatthew Dillon 	 */
342389d2b6eSMatthew Dillon 	if (--vm->vm_exitingcnt == 0 && vm->vm_refcnt == 0)
343334f7061SPeter Wemm 		vmspace_dofree(vm);
344334f7061SPeter Wemm }
345df8bae1dSRodney W. Grimes 
3461b40f8c0SMatthew Dillon void
347780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
3481b40f8c0SMatthew Dillon {
349bc91c510SAlan Cox 	int error;
350bc91c510SAlan Cox 
35193bc4879SAlan Cox 	if (map->system_map)
35236daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
35336daaecdSAlan Cox 	else {
354bc91c510SAlan Cox 		error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
355bc91c510SAlan Cox 		KASSERT(error == 0, ("%s: failed to get lock", __func__));
35636daaecdSAlan Cox 	}
3571b40f8c0SMatthew Dillon 	map->timestamp++;
3581b40f8c0SMatthew Dillon }
3591b40f8c0SMatthew Dillon 
3601b40f8c0SMatthew Dillon void
361780b1c09SAlan Cox _vm_map_unlock(vm_map_t map, const char *file, int line)
3620e0af8ecSBrian Feldman {
363bc91c510SAlan Cox 
36436daaecdSAlan Cox 	if (map->system_map)
36536daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
36636daaecdSAlan Cox 	else
367bc91c510SAlan Cox 		lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
3680e0af8ecSBrian Feldman }
3690e0af8ecSBrian Feldman 
3700e0af8ecSBrian Feldman void
371780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
3720e0af8ecSBrian Feldman {
373bc91c510SAlan Cox 	int error;
374bc91c510SAlan Cox 
37593bc4879SAlan Cox 	if (map->system_map)
37636daaecdSAlan Cox 		_mtx_lock_flags(&map->system_mtx, 0, file, line);
37736daaecdSAlan Cox 	else {
378bc91c510SAlan Cox 		error = lockmgr(&map->lock, LK_EXCLUSIVE, NULL, curthread);
379bc91c510SAlan Cox 		KASSERT(error == 0, ("%s: failed to get lock", __func__));
3800e0af8ecSBrian Feldman 	}
38136daaecdSAlan Cox }
3820e0af8ecSBrian Feldman 
3830e0af8ecSBrian Feldman void
384780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
3850e0af8ecSBrian Feldman {
386bc91c510SAlan Cox 
38736daaecdSAlan Cox 	if (map->system_map)
38836daaecdSAlan Cox 		_mtx_unlock_flags(&map->system_mtx, 0, file, line);
38936daaecdSAlan Cox 	else
390bc91c510SAlan Cox 		lockmgr(&map->lock, LK_RELEASE, NULL, curthread);
39125adb370SBrian Feldman }
39225adb370SBrian Feldman 
393d974f03cSAlan Cox int
394780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
395d974f03cSAlan Cox {
39625adb370SBrian Feldman 	int error;
39725adb370SBrian Feldman 
39836daaecdSAlan Cox 	error = map->system_map ?
39936daaecdSAlan Cox 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
40036daaecdSAlan Cox 	    lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL, curthread);
4013a92e5d5SAlan Cox 	if (error == 0)
4023a92e5d5SAlan Cox 		map->timestamp++;
403bc91c510SAlan Cox 	return (error == 0);
4040e0af8ecSBrian Feldman }
4050e0af8ecSBrian Feldman 
4060e0af8ecSBrian Feldman int
40772d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
40872d97679SDavid Schultz {
40972d97679SDavid Schultz 	int error;
41072d97679SDavid Schultz 
41172d97679SDavid Schultz 	error = map->system_map ?
41272d97679SDavid Schultz 	    !_mtx_trylock(&map->system_mtx, 0, file, line) :
41372d97679SDavid Schultz 	    lockmgr(&map->lock, LK_EXCLUSIVE | LK_NOWAIT, NULL, curthread);
41472d97679SDavid Schultz 	return (error == 0);
41572d97679SDavid Schultz }
41672d97679SDavid Schultz 
41772d97679SDavid Schultz int
418780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
4190e0af8ecSBrian Feldman {
420bc91c510SAlan Cox 
42136daaecdSAlan Cox 	if (map->system_map) {
42236daaecdSAlan Cox #ifdef INVARIANTS
42336daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
42436daaecdSAlan Cox #endif
42536daaecdSAlan Cox 	} else
426bc91c510SAlan Cox 		KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
427bc91c510SAlan Cox 		    ("%s: lock not held", __func__));
428bc91c510SAlan Cox 	map->timestamp++;
429bc91c510SAlan Cox 	return (0);
4300e0af8ecSBrian Feldman }
4310e0af8ecSBrian Feldman 
4320e0af8ecSBrian Feldman void
433780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
4341b40f8c0SMatthew Dillon {
435bc91c510SAlan Cox 
43636daaecdSAlan Cox 	if (map->system_map) {
43736daaecdSAlan Cox #ifdef INVARIANTS
43836daaecdSAlan Cox 		_mtx_assert(&map->system_mtx, MA_OWNED, file, line);
43936daaecdSAlan Cox #endif
44036daaecdSAlan Cox 	} else
441bc91c510SAlan Cox 		KASSERT(lockstatus(&map->lock, curthread) == LK_EXCLUSIVE,
442bc91c510SAlan Cox 		    ("%s: lock not held", __func__));
44325adb370SBrian Feldman }
44425adb370SBrian Feldman 
445acd9a301SAlan Cox /*
446acd9a301SAlan Cox  *	vm_map_unlock_and_wait:
447acd9a301SAlan Cox  */
4489688f931SAlan Cox int
449acd9a301SAlan Cox vm_map_unlock_and_wait(vm_map_t map, boolean_t user_wait)
450acd9a301SAlan Cox {
451acd9a301SAlan Cox 
4523a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
453acd9a301SAlan Cox 	vm_map_unlock(map);
4543a92e5d5SAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps", 0));
455acd9a301SAlan Cox }
456acd9a301SAlan Cox 
457acd9a301SAlan Cox /*
458acd9a301SAlan Cox  *	vm_map_wakeup:
459acd9a301SAlan Cox  */
4609688f931SAlan Cox void
461acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
462acd9a301SAlan Cox {
463acd9a301SAlan Cox 
464b49ecb86SAlan Cox 	/*
4653a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
4663a92e5d5SAlan Cox 	 * from being performed (and lost) between the vm_map_unlock()
4673a92e5d5SAlan Cox 	 * and the msleep() in vm_map_unlock_and_wait().
468b49ecb86SAlan Cox 	 */
4693a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
4703a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
471acd9a301SAlan Cox 	wakeup(&map->root);
472acd9a301SAlan Cox }
473acd9a301SAlan Cox 
4741b40f8c0SMatthew Dillon long
4751b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
4761b40f8c0SMatthew Dillon {
4771b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
4781b40f8c0SMatthew Dillon }
4791b40f8c0SMatthew Dillon 
4802bc7dd56SBruce M Simpson long
4812bc7dd56SBruce M Simpson vmspace_wired_count(struct vmspace *vmspace)
4822bc7dd56SBruce M Simpson {
4832bc7dd56SBruce M Simpson 	return pmap_wired_count(vmspace_pmap(vmspace));
4842bc7dd56SBruce M Simpson }
4852bc7dd56SBruce M Simpson 
486ff2b5645SMatthew Dillon /*
487df8bae1dSRodney W. Grimes  *	vm_map_create:
488df8bae1dSRodney W. Grimes  *
489df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
490df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
491df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
492df8bae1dSRodney W. Grimes  */
4930d94caffSDavid Greenman vm_map_t
4941b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
495df8bae1dSRodney W. Grimes {
496c0877f10SJohn Dyson 	vm_map_t result;
497df8bae1dSRodney W. Grimes 
498a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
49921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
5008355f576SJeff Roberson 	_vm_map_init(result, min, max);
501df8bae1dSRodney W. Grimes 	result->pmap = pmap;
502df8bae1dSRodney W. Grimes 	return (result);
503df8bae1dSRodney W. Grimes }
504df8bae1dSRodney W. Grimes 
505df8bae1dSRodney W. Grimes /*
506df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
507df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
508df8bae1dSRodney W. Grimes  * The pmap is set elsewhere.
509df8bae1dSRodney W. Grimes  */
5108355f576SJeff Roberson static void
5118355f576SJeff Roberson _vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
512df8bae1dSRodney W. Grimes {
51321c641b2SJohn Baldwin 
514df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
5159688f931SAlan Cox 	map->needs_wakeup = FALSE;
5163075778bSJohn Dyson 	map->system_map = 0;
517df8bae1dSRodney W. Grimes 	map->min_offset = min;
518df8bae1dSRodney W. Grimes 	map->max_offset = max;
519df8bae1dSRodney W. Grimes 	map->first_free = &map->header;
5204e94f402SAlan Cox 	map->root = NULL;
521df8bae1dSRodney W. Grimes 	map->timestamp = 0;
522df8bae1dSRodney W. Grimes }
523df8bae1dSRodney W. Grimes 
524a18b1f1dSJason Evans void
5258355f576SJeff Roberson vm_map_init(vm_map_t map, vm_offset_t min, vm_offset_t max)
526a18b1f1dSJason Evans {
5278355f576SJeff Roberson 	_vm_map_init(map, min, max);
528d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
529515630b1SAlan Cox 	lockinit(&map->lock, PVM, "thrd_sleep", 0, LK_NOPAUSE);
530a18b1f1dSJason Evans }
531a18b1f1dSJason Evans 
532df8bae1dSRodney W. Grimes /*
533b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
534b18bfc3dSJohn Dyson  *
535b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
536b18bfc3dSJohn Dyson  */
53762487bb4SJohn Dyson static void
5381b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
539b18bfc3dSJohn Dyson {
5402b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
541b18bfc3dSJohn Dyson }
542b18bfc3dSJohn Dyson 
543b18bfc3dSJohn Dyson /*
544df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
545df8bae1dSRodney W. Grimes  *
546df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
547b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
548df8bae1dSRodney W. Grimes  */
549f708ef1bSPoul-Henning Kamp static vm_map_entry_t
5501b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
551df8bae1dSRodney W. Grimes {
5521f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
5531f6889a1SMatthew Dillon 
5542b4a2c27SAlan Cox 	if (map->system_map)
5552b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
5562b4a2c27SAlan Cox 	else
557a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
5581f6889a1SMatthew Dillon 	if (new_entry == NULL)
5591f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
5601f6889a1SMatthew Dillon 	return (new_entry);
561df8bae1dSRodney W. Grimes }
562df8bae1dSRodney W. Grimes 
563df8bae1dSRodney W. Grimes /*
564794316a8SAlan Cox  *	vm_map_entry_set_behavior:
565794316a8SAlan Cox  *
566794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
567794316a8SAlan Cox  *	sequential.
568794316a8SAlan Cox  */
569794316a8SAlan Cox static __inline void
570794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
571794316a8SAlan Cox {
572794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
573794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
574794316a8SAlan Cox }
575794316a8SAlan Cox 
576794316a8SAlan Cox /*
5774e94f402SAlan Cox  *	vm_map_entry_splay:
5784e94f402SAlan Cox  *
5794e94f402SAlan Cox  *	Implements Sleator and Tarjan's top-down splay algorithm.  Returns
5804e94f402SAlan Cox  *	the vm_map_entry containing the given address.  If, however, that
5814e94f402SAlan Cox  *	address is not found in the vm_map, returns a vm_map_entry that is
5824e94f402SAlan Cox  *	adjacent to the address, coming before or after it.
5834e94f402SAlan Cox  */
5844e94f402SAlan Cox static vm_map_entry_t
5854e94f402SAlan Cox vm_map_entry_splay(vm_offset_t address, vm_map_entry_t root)
5864e94f402SAlan Cox {
5874e94f402SAlan Cox 	struct vm_map_entry dummy;
5884e94f402SAlan Cox 	vm_map_entry_t lefttreemax, righttreemin, y;
5894e94f402SAlan Cox 
5904e94f402SAlan Cox 	if (root == NULL)
5914e94f402SAlan Cox 		return (root);
5924e94f402SAlan Cox 	lefttreemax = righttreemin = &dummy;
59361c075b6SAlan Cox 	for (;; root = y) {
5944e94f402SAlan Cox 		if (address < root->start) {
59561c075b6SAlan Cox 			if ((y = root->left) == NULL)
5964e94f402SAlan Cox 				break;
59761c075b6SAlan Cox 			if (address < y->start) {
5984e94f402SAlan Cox 				/* Rotate right. */
5994e94f402SAlan Cox 				root->left = y->right;
6004e94f402SAlan Cox 				y->right = root;
6014e94f402SAlan Cox 				root = y;
60261c075b6SAlan Cox 				if ((y = root->left) == NULL)
6034e94f402SAlan Cox 					break;
6044e94f402SAlan Cox 			}
6054e94f402SAlan Cox 			/* Link into the new root's right tree. */
6064e94f402SAlan Cox 			righttreemin->left = root;
6074e94f402SAlan Cox 			righttreemin = root;
6084e94f402SAlan Cox 		} else if (address >= root->end) {
60961c075b6SAlan Cox 			if ((y = root->right) == NULL)
6104e94f402SAlan Cox 				break;
61161c075b6SAlan Cox 			if (address >= y->end) {
6124e94f402SAlan Cox 				/* Rotate left. */
6134e94f402SAlan Cox 				root->right = y->left;
6144e94f402SAlan Cox 				y->left = root;
6154e94f402SAlan Cox 				root = y;
61661c075b6SAlan Cox 				if ((y = root->right) == NULL)
6174e94f402SAlan Cox 					break;
6184e94f402SAlan Cox 			}
6194e94f402SAlan Cox 			/* Link into the new root's left tree. */
6204e94f402SAlan Cox 			lefttreemax->right = root;
6214e94f402SAlan Cox 			lefttreemax = root;
6224e94f402SAlan Cox 		} else
6234e94f402SAlan Cox 			break;
6244e94f402SAlan Cox 	}
6254e94f402SAlan Cox 	/* Assemble the new root. */
6264e94f402SAlan Cox 	lefttreemax->right = root->left;
6274e94f402SAlan Cox 	righttreemin->left = root->right;
6284e94f402SAlan Cox 	root->left = dummy.right;
6294e94f402SAlan Cox 	root->right = dummy.left;
6304e94f402SAlan Cox 	return (root);
6314e94f402SAlan Cox }
6324e94f402SAlan Cox 
6334e94f402SAlan Cox /*
634df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
635df8bae1dSRodney W. Grimes  *
636df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
637df8bae1dSRodney W. Grimes  */
6384e94f402SAlan Cox static void
63999c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
64099c81ca9SAlan Cox 		  vm_map_entry_t after_where,
64199c81ca9SAlan Cox 		  vm_map_entry_t entry)
64299c81ca9SAlan Cox {
64321c641b2SJohn Baldwin 
64421c641b2SJohn Baldwin 	CTR4(KTR_VM,
64521c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
64621c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
64799c81ca9SAlan Cox 	map->nentries++;
64899c81ca9SAlan Cox 	entry->prev = after_where;
64999c81ca9SAlan Cox 	entry->next = after_where->next;
65099c81ca9SAlan Cox 	entry->next->prev = entry;
65199c81ca9SAlan Cox 	after_where->next = entry;
6524e94f402SAlan Cox 
6534e94f402SAlan Cox 	if (after_where != &map->header) {
6544e94f402SAlan Cox 		if (after_where != map->root)
6554e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
6564e94f402SAlan Cox 		entry->right = after_where->right;
6574e94f402SAlan Cox 		entry->left = after_where;
6584e94f402SAlan Cox 		after_where->right = NULL;
6594e94f402SAlan Cox 	} else {
6604e94f402SAlan Cox 		entry->right = map->root;
6614e94f402SAlan Cox 		entry->left = NULL;
6624e94f402SAlan Cox 	}
6634e94f402SAlan Cox 	map->root = entry;
664df8bae1dSRodney W. Grimes }
66599c81ca9SAlan Cox 
6664e94f402SAlan Cox static void
66799c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
66899c81ca9SAlan Cox 		    vm_map_entry_t entry)
66999c81ca9SAlan Cox {
6704e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
67199c81ca9SAlan Cox 
6724e94f402SAlan Cox 	if (entry != map->root)
6734e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
6744e94f402SAlan Cox 	if (entry->left == NULL)
6754e94f402SAlan Cox 		root = entry->right;
6764e94f402SAlan Cox 	else {
6774e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
6784e94f402SAlan Cox 		root->right = entry->right;
6794e94f402SAlan Cox 	}
6804e94f402SAlan Cox 	map->root = root;
6814e94f402SAlan Cox 
6824e94f402SAlan Cox 	prev = entry->prev;
6834e94f402SAlan Cox 	next = entry->next;
68499c81ca9SAlan Cox 	next->prev = prev;
68599c81ca9SAlan Cox 	prev->next = next;
68699c81ca9SAlan Cox 	map->nentries--;
68721c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
68821c641b2SJohn Baldwin 	    map->nentries, entry);
689df8bae1dSRodney W. Grimes }
690df8bae1dSRodney W. Grimes 
691df8bae1dSRodney W. Grimes /*
692df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
693df8bae1dSRodney W. Grimes  *
694df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
695df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
696df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
697df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
698df8bae1dSRodney W. Grimes  *	result indicates whether the address is
699df8bae1dSRodney W. Grimes  *	actually contained in the map.
700df8bae1dSRodney W. Grimes  */
7010d94caffSDavid Greenman boolean_t
7021b40f8c0SMatthew Dillon vm_map_lookup_entry(
7031b40f8c0SMatthew Dillon 	vm_map_t map,
7041b40f8c0SMatthew Dillon 	vm_offset_t address,
7051b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
706df8bae1dSRodney W. Grimes {
707c0877f10SJohn Dyson 	vm_map_entry_t cur;
708df8bae1dSRodney W. Grimes 
7094e94f402SAlan Cox 	cur = vm_map_entry_splay(address, map->root);
7104e94f402SAlan Cox 	if (cur == NULL)
7114e94f402SAlan Cox 		*entry = &map->header;
7124e94f402SAlan Cox 	else {
7134e94f402SAlan Cox 		map->root = cur;
714df8bae1dSRodney W. Grimes 
715df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
716df8bae1dSRodney W. Grimes 			*entry = cur;
7174e94f402SAlan Cox 			if (cur->end > address)
718df8bae1dSRodney W. Grimes 				return (TRUE);
7194e94f402SAlan Cox 		} else
720df8bae1dSRodney W. Grimes 			*entry = cur->prev;
7214e94f402SAlan Cox 	}
722df8bae1dSRodney W. Grimes 	return (FALSE);
723df8bae1dSRodney W. Grimes }
724df8bae1dSRodney W. Grimes 
725df8bae1dSRodney W. Grimes /*
72630dcfc09SJohn Dyson  *	vm_map_insert:
72730dcfc09SJohn Dyson  *
72830dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
72930dcfc09SJohn Dyson  *	map at the specified address range.  The object's
73030dcfc09SJohn Dyson  *	size should match that of the address range.
73130dcfc09SJohn Dyson  *
73230dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
7332aaeadf8SMatthew Dillon  *
7342aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
7352aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
73630dcfc09SJohn Dyson  */
73730dcfc09SJohn Dyson int
738b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
739b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
740b9dcd593SBruce Evans 	      int cow)
74130dcfc09SJohn Dyson {
742c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
743c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
74430dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
7459730a5daSPaul Saab 	vm_eflags_t protoeflags;
74630dcfc09SJohn Dyson 
74730dcfc09SJohn Dyson 	/*
74830dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
74930dcfc09SJohn Dyson 	 */
75030dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
75130dcfc09SJohn Dyson 	    (start >= end))
75230dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
75330dcfc09SJohn Dyson 
75430dcfc09SJohn Dyson 	/*
75530dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
75630dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
75730dcfc09SJohn Dyson 	 */
75830dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
75930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
76030dcfc09SJohn Dyson 
76130dcfc09SJohn Dyson 	prev_entry = temp_entry;
76230dcfc09SJohn Dyson 
76330dcfc09SJohn Dyson 	/*
76430dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
76530dcfc09SJohn Dyson 	 */
76630dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
76730dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
76830dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
76930dcfc09SJohn Dyson 
770afa07f7eSJohn Dyson 	protoeflags = 0;
771afa07f7eSJohn Dyson 
772afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
773e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
774afa07f7eSJohn Dyson 
7754e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
776afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
777afa07f7eSJohn Dyson 
7784e045f93SAlan Cox 		KASSERT(object == NULL,
7794e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
7804e045f93SAlan Cox 	}
7814f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
7824f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
7839730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
7849730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
7854f79d873SMatthew Dillon 
7861d284e00SAlan Cox 	if (object != NULL) {
78730dcfc09SJohn Dyson 		/*
7881d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
7891d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
7901d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
7911d284e00SAlan Cox 		 * reference counting is insufficient to recognize
7921d284e00SAlan Cox 		 * aliases with precision.)
79330dcfc09SJohn Dyson 		 */
7941d284e00SAlan Cox 		VM_OBJECT_LOCK(object);
7951d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
7962aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
7971d284e00SAlan Cox 		VM_OBJECT_UNLOCK(object);
7984e045f93SAlan Cox 	}
7994e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
8004e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
8018cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
8024e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
8034e045f93SAlan Cox 		 ((prev_entry->object.vm_object == NULL) ||
8048cc7e047SJohn Dyson 		  vm_object_coalesce(prev_entry->object.vm_object,
80530dcfc09SJohn Dyson 				     OFF_TO_IDX(prev_entry->offset),
8068cc7e047SJohn Dyson 				     (vm_size_t)(prev_entry->end - prev_entry->start),
807cdc2c291SJohn Dyson 				     (vm_size_t)(end - prev_entry->end)))) {
80830dcfc09SJohn Dyson 		/*
8092aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
8102aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
8112aaeadf8SMatthew Dillon 		 * new range as well.
81230dcfc09SJohn Dyson 		 */
8138cc7e047SJohn Dyson 		if ((prev_entry->inheritance == VM_INHERIT_DEFAULT) &&
8148cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
8158cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
81630dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
81730dcfc09SJohn Dyson 			prev_entry->end = end;
8184e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
81930dcfc09SJohn Dyson 			return (KERN_SUCCESS);
82030dcfc09SJohn Dyson 		}
8218cc7e047SJohn Dyson 
8222aaeadf8SMatthew Dillon 		/*
8232aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
8242aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
8252aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
8264e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
8272aaeadf8SMatthew Dillon 		 */
8282aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
8292aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
8302aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
8318cc7e047SJohn Dyson 		vm_object_reference(object);
832b18bfc3dSJohn Dyson 	}
8332aaeadf8SMatthew Dillon 
8342aaeadf8SMatthew Dillon 	/*
8352aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
8362aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
8372aaeadf8SMatthew Dillon 	 * backing objects.
8382aaeadf8SMatthew Dillon 	 */
8398cc7e047SJohn Dyson 
84030dcfc09SJohn Dyson 	/*
84130dcfc09SJohn Dyson 	 * Create a new entry
84230dcfc09SJohn Dyson 	 */
84330dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
84430dcfc09SJohn Dyson 	new_entry->start = start;
84530dcfc09SJohn Dyson 	new_entry->end = end;
84630dcfc09SJohn Dyson 
847afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
84830dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
84930dcfc09SJohn Dyson 	new_entry->offset = offset;
8502267af78SJulian Elischer 	new_entry->avail_ssize = 0;
8512267af78SJulian Elischer 
85230dcfc09SJohn Dyson 	new_entry->inheritance = VM_INHERIT_DEFAULT;
85330dcfc09SJohn Dyson 	new_entry->protection = prot;
85430dcfc09SJohn Dyson 	new_entry->max_protection = max;
85530dcfc09SJohn Dyson 	new_entry->wired_count = 0;
856e5f251d2SAlan Cox 
85730dcfc09SJohn Dyson 	/*
85830dcfc09SJohn Dyson 	 * Insert the new entry into the list
85930dcfc09SJohn Dyson 	 */
86030dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
86130dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
86230dcfc09SJohn Dyson 
86330dcfc09SJohn Dyson 	/*
86430dcfc09SJohn Dyson 	 * Update the free space hint
86530dcfc09SJohn Dyson 	 */
86667bf6868SJohn Dyson 	if ((map->first_free == prev_entry) &&
8674f79d873SMatthew Dillon 	    (prev_entry->end >= new_entry->start)) {
86830dcfc09SJohn Dyson 		map->first_free = new_entry;
8694f79d873SMatthew Dillon 	}
87030dcfc09SJohn Dyson 
8711a484d28SMatthew Dillon #if 0
8721a484d28SMatthew Dillon 	/*
8731a484d28SMatthew Dillon 	 * Temporarily removed to avoid MAP_STACK panic, due to
8741a484d28SMatthew Dillon 	 * MAP_STACK being a huge hack.  Will be added back in
8751a484d28SMatthew Dillon 	 * when MAP_STACK (and the user stack mapping) is fixed.
8761a484d28SMatthew Dillon 	 */
8774e71e795SMatthew Dillon 	/*
8784e71e795SMatthew Dillon 	 * It may be possible to simplify the entry
8794e71e795SMatthew Dillon 	 */
8804e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
8811a484d28SMatthew Dillon #endif
8824e71e795SMatthew Dillon 
8834f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
8840551c08dSAlan Cox 		vm_map_pmap_enter(map, start,
885e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
886e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
8874f79d873SMatthew Dillon 	}
888e972780aSAlan Cox 
88930dcfc09SJohn Dyson 	return (KERN_SUCCESS);
89030dcfc09SJohn Dyson }
89130dcfc09SJohn Dyson 
89230dcfc09SJohn Dyson /*
893df8bae1dSRodney W. Grimes  * Find sufficient space for `length' bytes in the given map, starting at
894df8bae1dSRodney W. Grimes  * `start'.  The map must be locked.  Returns 0 on success, 1 on no space.
895df8bae1dSRodney W. Grimes  */
896df8bae1dSRodney W. Grimes int
8971b40f8c0SMatthew Dillon vm_map_findspace(
8981b40f8c0SMatthew Dillon 	vm_map_t map,
8991b40f8c0SMatthew Dillon 	vm_offset_t start,
9001b40f8c0SMatthew Dillon 	vm_size_t length,
9011b40f8c0SMatthew Dillon 	vm_offset_t *addr)
902df8bae1dSRodney W. Grimes {
903c0877f10SJohn Dyson 	vm_map_entry_t entry, next;
904c0877f10SJohn Dyson 	vm_offset_t end;
905df8bae1dSRodney W. Grimes 
906df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
907df8bae1dSRodney W. Grimes 		start = map->min_offset;
908df8bae1dSRodney W. Grimes 	if (start > map->max_offset)
909df8bae1dSRodney W. Grimes 		return (1);
910df8bae1dSRodney W. Grimes 
911df8bae1dSRodney W. Grimes 	/*
9120d94caffSDavid Greenman 	 * Look for the first possible address; if there's already something
9130d94caffSDavid Greenman 	 * at this address, we have to start after it.
914df8bae1dSRodney W. Grimes 	 */
915df8bae1dSRodney W. Grimes 	if (start == map->min_offset) {
91667bf6868SJohn Dyson 		if ((entry = map->first_free) != &map->header)
917df8bae1dSRodney W. Grimes 			start = entry->end;
918df8bae1dSRodney W. Grimes 	} else {
919df8bae1dSRodney W. Grimes 		vm_map_entry_t tmp;
9200d94caffSDavid Greenman 
921df8bae1dSRodney W. Grimes 		if (vm_map_lookup_entry(map, start, &tmp))
922df8bae1dSRodney W. Grimes 			start = tmp->end;
923df8bae1dSRodney W. Grimes 		entry = tmp;
924df8bae1dSRodney W. Grimes 	}
925df8bae1dSRodney W. Grimes 
926df8bae1dSRodney W. Grimes 	/*
9270d94caffSDavid Greenman 	 * Look through the rest of the map, trying to fit a new region in the
9280d94caffSDavid Greenman 	 * gap between existing regions, or after the very last region.
929df8bae1dSRodney W. Grimes 	 */
930df8bae1dSRodney W. Grimes 	for (;; start = (entry = next)->end) {
931df8bae1dSRodney W. Grimes 		/*
932df8bae1dSRodney W. Grimes 		 * Find the end of the proposed new region.  Be sure we didn't
933df8bae1dSRodney W. Grimes 		 * go beyond the end of the map, or wrap around the address;
934df8bae1dSRodney W. Grimes 		 * if so, we lose.  Otherwise, if this is the last entry, or
935df8bae1dSRodney W. Grimes 		 * if the proposed new region fits before the next entry, we
936df8bae1dSRodney W. Grimes 		 * win.
937df8bae1dSRodney W. Grimes 		 */
938df8bae1dSRodney W. Grimes 		end = start + length;
939df8bae1dSRodney W. Grimes 		if (end > map->max_offset || end < start)
940df8bae1dSRodney W. Grimes 			return (1);
941df8bae1dSRodney W. Grimes 		next = entry->next;
942df8bae1dSRodney W. Grimes 		if (next == &map->header || next->start >= end)
943df8bae1dSRodney W. Grimes 			break;
944df8bae1dSRodney W. Grimes 	}
945df8bae1dSRodney W. Grimes 	*addr = start;
94699448ed1SJohn Dyson 	if (map == kernel_map) {
94799448ed1SJohn Dyson 		vm_offset_t ksize;
94899448ed1SJohn Dyson 		if ((ksize = round_page(start + length)) > kernel_vm_end) {
94999448ed1SJohn Dyson 			pmap_growkernel(ksize);
95099448ed1SJohn Dyson 		}
95199448ed1SJohn Dyson 	}
952df8bae1dSRodney W. Grimes 	return (0);
953df8bae1dSRodney W. Grimes }
954df8bae1dSRodney W. Grimes 
955df8bae1dSRodney W. Grimes /*
956df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
957df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
958df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
959df8bae1dSRodney W. Grimes  *	returned in the same parameter.
960df8bae1dSRodney W. Grimes  *
9612aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
9622aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
963df8bae1dSRodney W. Grimes  */
964df8bae1dSRodney W. Grimes int
965b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
966b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
967b9dcd593SBruce Evans 	    vm_size_t length, boolean_t find_space, vm_prot_t prot,
968b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
969df8bae1dSRodney W. Grimes {
970c0877f10SJohn Dyson 	vm_offset_t start;
9718d6e8edeSDavid Greenman 	int result, s = 0;
972df8bae1dSRodney W. Grimes 
973df8bae1dSRodney W. Grimes 	start = *addr;
9748d6e8edeSDavid Greenman 
97508442f8aSBosko Milekic 	if (map == kmem_map)
976b18bfc3dSJohn Dyson 		s = splvm();
9778d6e8edeSDavid Greenman 
978bea41bcfSDavid Greenman 	vm_map_lock(map);
979df8bae1dSRodney W. Grimes 	if (find_space) {
980df8bae1dSRodney W. Grimes 		if (vm_map_findspace(map, start, length, addr)) {
981df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
98208442f8aSBosko Milekic 			if (map == kmem_map)
9838d6e8edeSDavid Greenman 				splx(s);
984df8bae1dSRodney W. Grimes 			return (KERN_NO_SPACE);
985df8bae1dSRodney W. Grimes 		}
986df8bae1dSRodney W. Grimes 		start = *addr;
987df8bae1dSRodney W. Grimes 	}
988bd7e5f99SJohn Dyson 	result = vm_map_insert(map, object, offset,
989bd7e5f99SJohn Dyson 		start, start + length, prot, max, cow);
990df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
9918d6e8edeSDavid Greenman 
99208442f8aSBosko Milekic 	if (map == kmem_map)
9938d6e8edeSDavid Greenman 		splx(s);
9948d6e8edeSDavid Greenman 
995df8bae1dSRodney W. Grimes 	return (result);
996df8bae1dSRodney W. Grimes }
997df8bae1dSRodney W. Grimes 
998df8bae1dSRodney W. Grimes /*
999b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
100067bf6868SJohn Dyson  *
10014e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
10024e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
10034e71e795SMatthew Dillon  *
10044e71e795SMatthew Dillon  *	The map must be locked.
10054e71e795SMatthew Dillon  *
10064e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
10074e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
10084e71e795SMatthew Dillon  *	both neighbors.
1009df8bae1dSRodney W. Grimes  */
1010b7b2aac2SJohn Dyson void
10111b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1012df8bae1dSRodney W. Grimes {
1013308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1014b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1015df8bae1dSRodney W. Grimes 
1016acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1017df8bae1dSRodney W. Grimes 		return;
1018308c24baSJohn Dyson 
1019308c24baSJohn Dyson 	prev = entry->prev;
1020308c24baSJohn Dyson 	if (prev != &map->header) {
102167bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
102267bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
102367bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
102495e5e988SJohn Dyson 		     (!prev->object.vm_object ||
102567bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1026afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
102767bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
102867bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
102967bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
1030b7b2aac2SJohn Dyson 		     (prev->wired_count == entry->wired_count)) {
1031308c24baSJohn Dyson 			if (map->first_free == prev)
1032308c24baSJohn Dyson 				map->first_free = entry;
1033308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1034308c24baSJohn Dyson 			entry->start = prev->start;
1035308c24baSJohn Dyson 			entry->offset = prev->offset;
1036b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1037308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1038308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1039308c24baSJohn Dyson 		}
1040308c24baSJohn Dyson 	}
1041de5f6a77SJohn Dyson 
1042de5f6a77SJohn Dyson 	next = entry->next;
1043308c24baSJohn Dyson 	if (next != &map->header) {
104467bf6868SJohn Dyson 		esize = entry->end - entry->start;
104567bf6868SJohn Dyson 		if ((entry->end == next->start) &&
104667bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
104767bf6868SJohn Dyson 		     (!entry->object.vm_object ||
104867bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1049afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
105067bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
105167bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
105267bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
1053b7b2aac2SJohn Dyson 		    (next->wired_count == entry->wired_count)) {
1054308c24baSJohn Dyson 			if (map->first_free == next)
1055308c24baSJohn Dyson 				map->first_free = entry;
1056de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1057de5f6a77SJohn Dyson 			entry->end = next->end;
1058b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1059de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1060de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1061df8bae1dSRodney W. Grimes 		}
1062df8bae1dSRodney W. Grimes 	}
1063de5f6a77SJohn Dyson }
1064df8bae1dSRodney W. Grimes /*
1065df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1066df8bae1dSRodney W. Grimes  *
1067df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1068df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1069df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1070df8bae1dSRodney W. Grimes  */
1071df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1072df8bae1dSRodney W. Grimes { \
1073df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1074df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1075df8bae1dSRodney W. Grimes }
1076df8bae1dSRodney W. Grimes 
1077df8bae1dSRodney W. Grimes /*
1078df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1079df8bae1dSRodney W. Grimes  *	the entry must be split.
1080df8bae1dSRodney W. Grimes  */
10810d94caffSDavid Greenman static void
10821b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1083df8bae1dSRodney W. Grimes {
1084c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1085df8bae1dSRodney W. Grimes 
1086df8bae1dSRodney W. Grimes 	/*
10870d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
10880d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
10890d94caffSDavid Greenman 	 * starting address.
1090df8bae1dSRodney W. Grimes 	 */
1091f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1092f32dbbeeSJohn Dyson 
109311cccda1SJohn Dyson 	/*
109411cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
109511cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
109611cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
109711cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
109811cccda1SJohn Dyson 	 * put this improvement.
109911cccda1SJohn Dyson 	 */
11004e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
110111cccda1SJohn Dyson 		vm_object_t object;
110211cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1103c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
110411cccda1SJohn Dyson 		entry->object.vm_object = object;
110511cccda1SJohn Dyson 		entry->offset = 0;
110611cccda1SJohn Dyson 	}
110711cccda1SJohn Dyson 
1108df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1109df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1110df8bae1dSRodney W. Grimes 
1111df8bae1dSRodney W. Grimes 	new_entry->end = start;
1112df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1113df8bae1dSRodney W. Grimes 	entry->start = start;
1114df8bae1dSRodney W. Grimes 
1115df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1116df8bae1dSRodney W. Grimes 
11179fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1118df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1119df8bae1dSRodney W. Grimes 	}
1120c0877f10SJohn Dyson }
1121df8bae1dSRodney W. Grimes 
1122df8bae1dSRodney W. Grimes /*
1123df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1124df8bae1dSRodney W. Grimes  *
1125df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1126df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1127df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1128df8bae1dSRodney W. Grimes  */
1129df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1130df8bae1dSRodney W. Grimes { \
1131af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1132af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1133df8bae1dSRodney W. Grimes }
1134df8bae1dSRodney W. Grimes 
1135df8bae1dSRodney W. Grimes /*
1136df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1137df8bae1dSRodney W. Grimes  *	the entry must be split.
1138df8bae1dSRodney W. Grimes  */
11390d94caffSDavid Greenman static void
11401b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1141df8bae1dSRodney W. Grimes {
1142c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1143df8bae1dSRodney W. Grimes 
1144df8bae1dSRodney W. Grimes 	/*
114511cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
114611cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
114711cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
114811cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
114911cccda1SJohn Dyson 	 * put this improvement.
115011cccda1SJohn Dyson 	 */
11514e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
115211cccda1SJohn Dyson 		vm_object_t object;
115311cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1154c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
115511cccda1SJohn Dyson 		entry->object.vm_object = object;
115611cccda1SJohn Dyson 		entry->offset = 0;
115711cccda1SJohn Dyson 	}
115811cccda1SJohn Dyson 
115911cccda1SJohn Dyson 	/*
11600d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1161df8bae1dSRodney W. Grimes 	 */
1162df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1163df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1164df8bae1dSRodney W. Grimes 
1165df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1166df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1167df8bae1dSRodney W. Grimes 
1168df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1169df8bae1dSRodney W. Grimes 
11709fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1171df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1172df8bae1dSRodney W. Grimes 	}
1173c0877f10SJohn Dyson }
1174df8bae1dSRodney W. Grimes 
1175df8bae1dSRodney W. Grimes /*
1176df8bae1dSRodney W. Grimes  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
1177df8bae1dSRodney W. Grimes  *
1178df8bae1dSRodney W. Grimes  *	Asserts that the starting and ending region
1179df8bae1dSRodney W. Grimes  *	addresses fall within the valid range of the map.
1180df8bae1dSRodney W. Grimes  */
1181df8bae1dSRodney W. Grimes #define	VM_MAP_RANGE_CHECK(map, start, end)		\
1182df8bae1dSRodney W. Grimes 		{					\
1183df8bae1dSRodney W. Grimes 		if (start < vm_map_min(map))		\
1184df8bae1dSRodney W. Grimes 			start = vm_map_min(map);	\
1185df8bae1dSRodney W. Grimes 		if (end > vm_map_max(map))		\
1186df8bae1dSRodney W. Grimes 			end = vm_map_max(map);		\
1187df8bae1dSRodney W. Grimes 		if (start > end)			\
1188df8bae1dSRodney W. Grimes 			start = end;			\
1189df8bae1dSRodney W. Grimes 		}
1190df8bae1dSRodney W. Grimes 
1191df8bae1dSRodney W. Grimes /*
1192df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1193df8bae1dSRodney W. Grimes  *
1194df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1195df8bae1dSRodney W. Grimes  *
1196df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1197df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1198df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1199df8bae1dSRodney W. Grimes  *
1200df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1201df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1202df8bae1dSRodney W. Grimes  *		vm_fault
1203df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1204df8bae1dSRodney W. Grimes  *
1205df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1206df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1207df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1208df8bae1dSRodney W. Grimes  */
1209df8bae1dSRodney W. Grimes int
12101b40f8c0SMatthew Dillon vm_map_submap(
12111b40f8c0SMatthew Dillon 	vm_map_t map,
12121b40f8c0SMatthew Dillon 	vm_offset_t start,
12131b40f8c0SMatthew Dillon 	vm_offset_t end,
12141b40f8c0SMatthew Dillon 	vm_map_t submap)
1215df8bae1dSRodney W. Grimes {
1216df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1217c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1218df8bae1dSRodney W. Grimes 
1219df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1220df8bae1dSRodney W. Grimes 
1221df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1222df8bae1dSRodney W. Grimes 
1223df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1224df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
12250d94caffSDavid Greenman 	} else
1226df8bae1dSRodney W. Grimes 		entry = entry->next;
1227df8bae1dSRodney W. Grimes 
1228df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1229df8bae1dSRodney W. Grimes 
1230df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
12319fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1232afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
12332d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1234afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1235df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1236df8bae1dSRodney W. Grimes 	}
1237df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1238df8bae1dSRodney W. Grimes 
1239df8bae1dSRodney W. Grimes 	return (result);
1240df8bae1dSRodney W. Grimes }
1241df8bae1dSRodney W. Grimes 
1242df8bae1dSRodney W. Grimes /*
12431f78f902SAlan Cox  * The maximum number of pages to map
12441f78f902SAlan Cox  */
12451f78f902SAlan Cox #define	MAX_INIT_PT	96
12461f78f902SAlan Cox 
12471f78f902SAlan Cox /*
12480551c08dSAlan Cox  *	vm_map_pmap_enter:
12490551c08dSAlan Cox  *
12500551c08dSAlan Cox  *	Preload the mappings for the given object into the specified
12510551c08dSAlan Cox  *	map.  This eliminates the soft faults on process startup and
12520551c08dSAlan Cox  *	immediately after an mmap(2).
12530551c08dSAlan Cox  */
12540551c08dSAlan Cox void
12550551c08dSAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr,
12560551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
12570551c08dSAlan Cox {
12581f78f902SAlan Cox 	vm_offset_t tmpidx;
12591f78f902SAlan Cox 	int psize;
12601f78f902SAlan Cox 	vm_page_t p, mpte;
12610551c08dSAlan Cox 
12621f78f902SAlan Cox 	if (object == NULL)
12631f78f902SAlan Cox 		return;
12640551c08dSAlan Cox 	mtx_lock(&Giant);
12651f78f902SAlan Cox 	VM_OBJECT_LOCK(object);
12661f78f902SAlan Cox 	if (object->type == OBJT_DEVICE) {
12671f78f902SAlan Cox 		pmap_object_init_pt(map->pmap, addr, object, pindex, size);
12681f78f902SAlan Cox 		goto unlock_return;
12691f78f902SAlan Cox 	}
12701f78f902SAlan Cox 
12711f78f902SAlan Cox 	psize = atop(size);
12721f78f902SAlan Cox 
12731f78f902SAlan Cox 	if (object->type != OBJT_VNODE ||
12741f78f902SAlan Cox 	    ((flags & MAP_PREFAULT_PARTIAL) && (psize > MAX_INIT_PT) &&
12751f78f902SAlan Cox 	     (object->resident_page_count > MAX_INIT_PT))) {
12761f78f902SAlan Cox 		goto unlock_return;
12771f78f902SAlan Cox 	}
12781f78f902SAlan Cox 
12791f78f902SAlan Cox 	if (psize + pindex > object->size) {
12801f78f902SAlan Cox 		if (object->size < pindex)
12811f78f902SAlan Cox 			goto unlock_return;
12821f78f902SAlan Cox 		psize = object->size - pindex;
12831f78f902SAlan Cox 	}
12841f78f902SAlan Cox 
12851f78f902SAlan Cox 	mpte = NULL;
12861f78f902SAlan Cox 
12871f78f902SAlan Cox 	if ((p = TAILQ_FIRST(&object->memq)) != NULL) {
12881f78f902SAlan Cox 		if (p->pindex < pindex) {
12891f78f902SAlan Cox 			p = vm_page_splay(pindex, object->root);
12901f78f902SAlan Cox 			if ((object->root = p)->pindex < pindex)
12911f78f902SAlan Cox 				p = TAILQ_NEXT(p, listq);
12921f78f902SAlan Cox 		}
12931f78f902SAlan Cox 	}
12941f78f902SAlan Cox 	/*
12951f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
12961f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
12971f78f902SAlan Cox 	 * or (2) NULL.
12981f78f902SAlan Cox 	 */
12991f78f902SAlan Cox 	for (;
13001f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
13011f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
13021f78f902SAlan Cox 		/*
13031f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
13041f78f902SAlan Cox 		 * free pages allocating pv entries.
13051f78f902SAlan Cox 		 */
13061f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
13071f78f902SAlan Cox 		    cnt.v_free_count < cnt.v_free_reserved) {
13081f78f902SAlan Cox 			break;
13091f78f902SAlan Cox 		}
13101f78f902SAlan Cox 		vm_page_lock_queues();
13111f78f902SAlan Cox 		if ((p->valid & VM_PAGE_BITS_ALL) == VM_PAGE_BITS_ALL &&
13121f78f902SAlan Cox 		    (p->busy == 0) &&
13131f78f902SAlan Cox 		    (p->flags & (PG_BUSY | PG_FICTITIOUS)) == 0) {
13141f78f902SAlan Cox 			if ((p->queue - p->pc) == PQ_CACHE)
13151f78f902SAlan Cox 				vm_page_deactivate(p);
13161f78f902SAlan Cox 			vm_page_busy(p);
13171f78f902SAlan Cox 			vm_page_unlock_queues();
13181f78f902SAlan Cox 			VM_OBJECT_UNLOCK(object);
13191f78f902SAlan Cox 			mpte = pmap_enter_quick(map->pmap,
13201f78f902SAlan Cox 				addr + ptoa(tmpidx), p, mpte);
13211f78f902SAlan Cox 			VM_OBJECT_LOCK(object);
13221f78f902SAlan Cox 			vm_page_lock_queues();
13231f78f902SAlan Cox 			vm_page_wakeup(p);
13241f78f902SAlan Cox 		}
13251f78f902SAlan Cox 		vm_page_unlock_queues();
13261f78f902SAlan Cox 	}
13271f78f902SAlan Cox unlock_return:
13281f78f902SAlan Cox 	VM_OBJECT_UNLOCK(object);
13290551c08dSAlan Cox 	mtx_unlock(&Giant);
13300551c08dSAlan Cox }
13310551c08dSAlan Cox 
13320551c08dSAlan Cox /*
1333df8bae1dSRodney W. Grimes  *	vm_map_protect:
1334df8bae1dSRodney W. Grimes  *
1335df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1336df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1337df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1338df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1339df8bae1dSRodney W. Grimes  */
1340df8bae1dSRodney W. Grimes int
1341b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1342b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1343df8bae1dSRodney W. Grimes {
1344c0877f10SJohn Dyson 	vm_map_entry_t current;
1345df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1346df8bae1dSRodney W. Grimes 
1347df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1348df8bae1dSRodney W. Grimes 
1349df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1350df8bae1dSRodney W. Grimes 
1351df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1352df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1353b7b2aac2SJohn Dyson 	} else {
1354df8bae1dSRodney W. Grimes 		entry = entry->next;
1355b7b2aac2SJohn Dyson 	}
1356df8bae1dSRodney W. Grimes 
1357df8bae1dSRodney W. Grimes 	/*
13580d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1359df8bae1dSRodney W. Grimes 	 */
1360df8bae1dSRodney W. Grimes 	current = entry;
1361df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1362afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1363a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1364df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1365a1f6d91cSDavid Greenman 		}
1366df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1367df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1368df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1369df8bae1dSRodney W. Grimes 		}
1370df8bae1dSRodney W. Grimes 		current = current->next;
1371df8bae1dSRodney W. Grimes 	}
1372df8bae1dSRodney W. Grimes 
1373df8bae1dSRodney W. Grimes 	/*
13740d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
13750d94caffSDavid Greenman 	 * necessary the second time.]
1376df8bae1dSRodney W. Grimes 	 */
1377df8bae1dSRodney W. Grimes 	current = entry;
1378df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1379df8bae1dSRodney W. Grimes 		vm_prot_t old_prot;
1380df8bae1dSRodney W. Grimes 
1381df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, current, end);
1382df8bae1dSRodney W. Grimes 
1383df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1384df8bae1dSRodney W. Grimes 		if (set_max)
1385df8bae1dSRodney W. Grimes 			current->protection =
1386df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1387df8bae1dSRodney W. Grimes 			    old_prot;
1388df8bae1dSRodney W. Grimes 		else
1389df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1390df8bae1dSRodney W. Grimes 
1391df8bae1dSRodney W. Grimes 		/*
13920d94caffSDavid Greenman 		 * Update physical map if necessary. Worry about copy-on-write
13930d94caffSDavid Greenman 		 * here -- CHECK THIS XXX
1394df8bae1dSRodney W. Grimes 		 */
1395df8bae1dSRodney W. Grimes 		if (current->protection != old_prot) {
139647c3ccc4SAlan Cox 			mtx_lock(&Giant);
139785e03a7eSAlan Cox 			vm_page_lock_queues();
1398afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1399df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1400df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1401df8bae1dSRodney W. Grimes 			    current->end,
14021c85e3dfSAlan Cox 			    current->protection & MASK(current));
1403df8bae1dSRodney W. Grimes #undef	MASK
140485e03a7eSAlan Cox 			vm_page_unlock_queues();
140547c3ccc4SAlan Cox 			mtx_unlock(&Giant);
1406df8bae1dSRodney W. Grimes 		}
14077d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
1408df8bae1dSRodney W. Grimes 		current = current->next;
1409df8bae1dSRodney W. Grimes 	}
1410df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1411df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1412df8bae1dSRodney W. Grimes }
1413df8bae1dSRodney W. Grimes 
1414df8bae1dSRodney W. Grimes /*
1415867a482dSJohn Dyson  *	vm_map_madvise:
1416867a482dSJohn Dyson  *
1417867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
1418f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
1419f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
1420f7fc307aSAlan Cox  *	objects.
1421867a482dSJohn Dyson  */
1422b4309055SMatthew Dillon int
14231b40f8c0SMatthew Dillon vm_map_madvise(
14241b40f8c0SMatthew Dillon 	vm_map_t map,
14251b40f8c0SMatthew Dillon 	vm_offset_t start,
14261b40f8c0SMatthew Dillon 	vm_offset_t end,
14271b40f8c0SMatthew Dillon 	int behav)
1428867a482dSJohn Dyson {
1429f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
1430b4309055SMatthew Dillon 	int modify_map = 0;
1431867a482dSJohn Dyson 
1432b4309055SMatthew Dillon 	/*
1433b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
1434b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
1435b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
1436b4309055SMatthew Dillon 	 * on the map.
1437b4309055SMatthew Dillon 	 */
1438b4309055SMatthew Dillon 	switch(behav) {
1439b4309055SMatthew Dillon 	case MADV_NORMAL:
1440b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
1441b4309055SMatthew Dillon 	case MADV_RANDOM:
14424f79d873SMatthew Dillon 	case MADV_NOSYNC:
14434f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
14449730a5daSPaul Saab 	case MADV_NOCORE:
14459730a5daSPaul Saab 	case MADV_CORE:
1446b4309055SMatthew Dillon 		modify_map = 1;
1447867a482dSJohn Dyson 		vm_map_lock(map);
1448b4309055SMatthew Dillon 		break;
1449b4309055SMatthew Dillon 	case MADV_WILLNEED:
1450b4309055SMatthew Dillon 	case MADV_DONTNEED:
1451b4309055SMatthew Dillon 	case MADV_FREE:
1452f7fc307aSAlan Cox 		vm_map_lock_read(map);
1453b4309055SMatthew Dillon 		break;
1454b4309055SMatthew Dillon 	default:
1455b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
1456b4309055SMatthew Dillon 	}
1457b4309055SMatthew Dillon 
1458b4309055SMatthew Dillon 	/*
1459b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
1460b4309055SMatthew Dillon 	 */
1461867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
1462867a482dSJohn Dyson 
1463867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
1464f7fc307aSAlan Cox 		if (modify_map)
1465867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
1466b4309055SMatthew Dillon 	} else {
1467867a482dSJohn Dyson 		entry = entry->next;
1468b4309055SMatthew Dillon 	}
1469867a482dSJohn Dyson 
1470f7fc307aSAlan Cox 	if (modify_map) {
1471f7fc307aSAlan Cox 		/*
1472f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
1473f7fc307aSAlan Cox 		 *
1474f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
1475f7fc307aSAlan Cox 		 * limited to the specified address range.
1476f7fc307aSAlan Cox 		 */
1477867a482dSJohn Dyson 		for (current = entry;
1478867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
1479b4309055SMatthew Dillon 		     current = current->next
1480b4309055SMatthew Dillon 		) {
1481f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1482867a482dSJohn Dyson 				continue;
1483fed9a903SJohn Dyson 
148447221757SJohn Dyson 			vm_map_clip_end(map, current, end);
1485fed9a903SJohn Dyson 
1486f7fc307aSAlan Cox 			switch (behav) {
1487867a482dSJohn Dyson 			case MADV_NORMAL:
14887f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
1489867a482dSJohn Dyson 				break;
1490867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
14917f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
1492867a482dSJohn Dyson 				break;
1493867a482dSJohn Dyson 			case MADV_RANDOM:
14947f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
1495867a482dSJohn Dyson 				break;
14964f79d873SMatthew Dillon 			case MADV_NOSYNC:
14974f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
14984f79d873SMatthew Dillon 				break;
14994f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
15004f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
15014f79d873SMatthew Dillon 				break;
15029730a5daSPaul Saab 			case MADV_NOCORE:
15039730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
15049730a5daSPaul Saab 				break;
15059730a5daSPaul Saab 			case MADV_CORE:
15069730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
15079730a5daSPaul Saab 				break;
1508867a482dSJohn Dyson 			default:
1509867a482dSJohn Dyson 				break;
1510867a482dSJohn Dyson 			}
1511f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
1512867a482dSJohn Dyson 		}
1513867a482dSJohn Dyson 		vm_map_unlock(map);
1514b4309055SMatthew Dillon 	} else {
1515f7fc307aSAlan Cox 		vm_pindex_t pindex;
1516f7fc307aSAlan Cox 		int count;
1517f7fc307aSAlan Cox 
1518f7fc307aSAlan Cox 		/*
1519f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
1520f7fc307aSAlan Cox 		 * vm_object.
1521f7fc307aSAlan Cox 		 *
1522f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
1523f7fc307aSAlan Cox 		 * the vm_object pindex and count.
1524f7fc307aSAlan Cox 		 */
1525f7fc307aSAlan Cox 		for (current = entry;
1526f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
1527b4309055SMatthew Dillon 		     current = current->next
1528b4309055SMatthew Dillon 		) {
15295f99b57cSMatthew Dillon 			vm_offset_t useStart;
15305f99b57cSMatthew Dillon 
1531f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
1532f7fc307aSAlan Cox 				continue;
1533f7fc307aSAlan Cox 
1534f7fc307aSAlan Cox 			pindex = OFF_TO_IDX(current->offset);
1535f7fc307aSAlan Cox 			count = atop(current->end - current->start);
15365f99b57cSMatthew Dillon 			useStart = current->start;
1537f7fc307aSAlan Cox 
1538f7fc307aSAlan Cox 			if (current->start < start) {
1539f7fc307aSAlan Cox 				pindex += atop(start - current->start);
1540f7fc307aSAlan Cox 				count -= atop(start - current->start);
15415f99b57cSMatthew Dillon 				useStart = start;
1542f7fc307aSAlan Cox 			}
1543f7fc307aSAlan Cox 			if (current->end > end)
1544f7fc307aSAlan Cox 				count -= atop(current->end - end);
1545f7fc307aSAlan Cox 
1546f7fc307aSAlan Cox 			if (count <= 0)
1547f7fc307aSAlan Cox 				continue;
1548f7fc307aSAlan Cox 
1549f7fc307aSAlan Cox 			vm_object_madvise(current->object.vm_object,
1550f7fc307aSAlan Cox 					  pindex, count, behav);
1551b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
15520551c08dSAlan Cox 				vm_map_pmap_enter(map,
15535f99b57cSMatthew Dillon 				    useStart,
1554f7fc307aSAlan Cox 				    current->object.vm_object,
1555b4309055SMatthew Dillon 				    pindex,
1556b4309055SMatthew Dillon 				    (count << PAGE_SHIFT),
1557e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
1558b4309055SMatthew Dillon 				);
1559f7fc307aSAlan Cox 			}
1560f7fc307aSAlan Cox 		}
1561f7fc307aSAlan Cox 		vm_map_unlock_read(map);
1562f7fc307aSAlan Cox 	}
1563b4309055SMatthew Dillon 	return (0);
1564867a482dSJohn Dyson }
1565867a482dSJohn Dyson 
1566867a482dSJohn Dyson 
1567867a482dSJohn Dyson /*
1568df8bae1dSRodney W. Grimes  *	vm_map_inherit:
1569df8bae1dSRodney W. Grimes  *
1570df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
1571df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
1572df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
1573df8bae1dSRodney W. Grimes  *	child maps at the time of vm_map_fork.
1574df8bae1dSRodney W. Grimes  */
1575df8bae1dSRodney W. Grimes int
1576b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
1577b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
1578df8bae1dSRodney W. Grimes {
1579c0877f10SJohn Dyson 	vm_map_entry_t entry;
1580df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
1581df8bae1dSRodney W. Grimes 
1582df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
1583df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
1584df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
1585df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
1586df8bae1dSRodney W. Grimes 		break;
1587df8bae1dSRodney W. Grimes 	default:
1588df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
1589df8bae1dSRodney W. Grimes 	}
1590df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1591df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1592df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
1593df8bae1dSRodney W. Grimes 		entry = temp_entry;
1594df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
15950d94caffSDavid Greenman 	} else
1596df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
1597df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
1598df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
1599df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
160044428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
1601df8bae1dSRodney W. Grimes 		entry = entry->next;
1602df8bae1dSRodney W. Grimes 	}
1603df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1604df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
1605df8bae1dSRodney W. Grimes }
1606df8bae1dSRodney W. Grimes 
1607df8bae1dSRodney W. Grimes /*
1608acd9a301SAlan Cox  *	vm_map_unwire:
1609acd9a301SAlan Cox  *
1610e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
1611acd9a301SAlan Cox  */
1612acd9a301SAlan Cox int
1613acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1614abd498aaSBruce M Simpson     int flags)
1615acd9a301SAlan Cox {
1616acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
1617acd9a301SAlan Cox 	vm_offset_t saved_start;
1618acd9a301SAlan Cox 	unsigned int last_timestamp;
1619acd9a301SAlan Cox 	int rv;
1620abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
1621acd9a301SAlan Cox 
1622abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
1623acd9a301SAlan Cox 	vm_map_lock(map);
1624acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
1625acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1626abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1627cbef13d8SAlan Cox 			first_entry = first_entry->next;
1628abd498aaSBruce M Simpson 		else {
1629acd9a301SAlan Cox 			vm_map_unlock(map);
1630acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
1631acd9a301SAlan Cox 		}
1632abd498aaSBruce M Simpson 	}
1633acd9a301SAlan Cox 	last_timestamp = map->timestamp;
1634acd9a301SAlan Cox 	entry = first_entry;
1635acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1636acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
1637acd9a301SAlan Cox 			/*
1638acd9a301SAlan Cox 			 * We have not yet clipped the entry.
1639acd9a301SAlan Cox 			 */
1640acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
1641acd9a301SAlan Cox 			    entry->start;
1642acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
1643acd9a301SAlan Cox 			if (vm_map_unlock_and_wait(map, user_unwire)) {
1644acd9a301SAlan Cox 				/*
1645acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
1646acd9a301SAlan Cox 				 */
1647acd9a301SAlan Cox 			}
1648acd9a301SAlan Cox 			vm_map_lock(map);
1649acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
1650acd9a301SAlan Cox 				/*
1651acd9a301SAlan Cox 				 * Look again for the entry because the map was
1652acd9a301SAlan Cox 				 * modified while it was unlocked.
1653acd9a301SAlan Cox 				 * Specifically, the entry may have been
1654acd9a301SAlan Cox 				 * clipped, merged, or deleted.
1655acd9a301SAlan Cox 				 */
1656acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
1657acd9a301SAlan Cox 				    &tmp_entry)) {
1658cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1659cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1660cbef13d8SAlan Cox 					else {
1661acd9a301SAlan Cox 						if (saved_start == start) {
1662acd9a301SAlan Cox 							/*
1663acd9a301SAlan Cox 							 * First_entry has been deleted.
1664acd9a301SAlan Cox 							 */
1665acd9a301SAlan Cox 							vm_map_unlock(map);
1666acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
1667acd9a301SAlan Cox 						}
1668acd9a301SAlan Cox 						end = saved_start;
1669acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
1670acd9a301SAlan Cox 						goto done;
1671acd9a301SAlan Cox 					}
1672cbef13d8SAlan Cox 				}
1673acd9a301SAlan Cox 				if (entry == first_entry)
1674acd9a301SAlan Cox 					first_entry = tmp_entry;
1675acd9a301SAlan Cox 				else
1676acd9a301SAlan Cox 					first_entry = NULL;
1677acd9a301SAlan Cox 				entry = tmp_entry;
1678acd9a301SAlan Cox 			}
1679acd9a301SAlan Cox 			last_timestamp = map->timestamp;
1680acd9a301SAlan Cox 			continue;
1681acd9a301SAlan Cox 		}
1682acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
1683acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
1684acd9a301SAlan Cox 		/*
1685acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
1686acd9a301SAlan Cox 		 * above.)
1687acd9a301SAlan Cox 		 */
1688acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
1689acd9a301SAlan Cox 		/*
1690acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
1691abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
1692acd9a301SAlan Cox 		 */
1693abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1694abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
1695abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
1696acd9a301SAlan Cox 			end = entry->end;
1697acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
1698acd9a301SAlan Cox 			goto done;
1699acd9a301SAlan Cox 		}
1700acd9a301SAlan Cox 		/*
1701acd9a301SAlan Cox 		 * Require that the entry is wired.
1702acd9a301SAlan Cox 		 */
1703acd9a301SAlan Cox 		if (entry->wired_count == 0 || (user_unwire &&
1704acd9a301SAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)) {
1705acd9a301SAlan Cox 			end = entry->end;
1706acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
1707acd9a301SAlan Cox 			goto done;
1708acd9a301SAlan Cox 		}
1709acd9a301SAlan Cox 		entry = entry->next;
1710acd9a301SAlan Cox 	}
1711acd9a301SAlan Cox 	rv = KERN_SUCCESS;
1712acd9a301SAlan Cox done:
1713e27e17b7SAlan Cox 	need_wakeup = FALSE;
1714acd9a301SAlan Cox 	if (first_entry == NULL) {
1715acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
1716cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1717cbef13d8SAlan Cox 			first_entry = first_entry->next;
1718cbef13d8SAlan Cox 		else
1719acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
1720acd9a301SAlan Cox 	}
1721acd9a301SAlan Cox 	entry = first_entry;
1722acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
1723b2f3846aSAlan Cox 		if (rv == KERN_SUCCESS) {
1724b2f3846aSAlan Cox 			if (user_unwire)
1725b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
1726b2f3846aSAlan Cox 			entry->wired_count--;
1727b2f3846aSAlan Cox 			if (entry->wired_count == 0) {
1728b2f3846aSAlan Cox 				/*
1729b2f3846aSAlan Cox 				 * Retain the map lock.
1730b2f3846aSAlan Cox 				 */
1731b2f3846aSAlan Cox 				vm_fault_unwire(map, entry->start, entry->end);
1732b2f3846aSAlan Cox 			}
1733b2f3846aSAlan Cox 		}
1734acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
1735acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
1736acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
1737acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
1738acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
1739acd9a301SAlan Cox 			need_wakeup = TRUE;
1740acd9a301SAlan Cox 		}
1741acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
1742acd9a301SAlan Cox 		entry = entry->next;
1743acd9a301SAlan Cox 	}
1744acd9a301SAlan Cox 	vm_map_unlock(map);
1745acd9a301SAlan Cox 	if (need_wakeup)
1746acd9a301SAlan Cox 		vm_map_wakeup(map);
1747acd9a301SAlan Cox 	return (rv);
1748acd9a301SAlan Cox }
1749acd9a301SAlan Cox 
1750acd9a301SAlan Cox /*
1751e27e17b7SAlan Cox  *	vm_map_wire:
1752e27e17b7SAlan Cox  *
1753e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
1754e27e17b7SAlan Cox  */
1755e27e17b7SAlan Cox int
1756e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
1757abd498aaSBruce M Simpson     int flags)
1758e27e17b7SAlan Cox {
175912d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
176012d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
176112d7cc84SAlan Cox 	unsigned int last_timestamp;
176212d7cc84SAlan Cox 	int rv;
1763abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_wire;
1764e27e17b7SAlan Cox 
1765abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
176612d7cc84SAlan Cox 	vm_map_lock(map);
176712d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
176812d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
1769abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
1770cbef13d8SAlan Cox 			first_entry = first_entry->next;
1771abd498aaSBruce M Simpson 		else {
177212d7cc84SAlan Cox 			vm_map_unlock(map);
177312d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
177412d7cc84SAlan Cox 		}
1775abd498aaSBruce M Simpson 	}
177612d7cc84SAlan Cox 	last_timestamp = map->timestamp;
177712d7cc84SAlan Cox 	entry = first_entry;
177812d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
177912d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
178012d7cc84SAlan Cox 			/*
178112d7cc84SAlan Cox 			 * We have not yet clipped the entry.
178212d7cc84SAlan Cox 			 */
178312d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
178412d7cc84SAlan Cox 			    entry->start;
178512d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
178612d7cc84SAlan Cox 			if (vm_map_unlock_and_wait(map, user_wire)) {
178712d7cc84SAlan Cox 				/*
178812d7cc84SAlan Cox 				 * Allow interruption of user wiring?
178912d7cc84SAlan Cox 				 */
179012d7cc84SAlan Cox 			}
179112d7cc84SAlan Cox 			vm_map_lock(map);
179212d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
179312d7cc84SAlan Cox 				/*
179412d7cc84SAlan Cox 				 * Look again for the entry because the map was
179512d7cc84SAlan Cox 				 * modified while it was unlocked.
179612d7cc84SAlan Cox 				 * Specifically, the entry may have been
179712d7cc84SAlan Cox 				 * clipped, merged, or deleted.
179812d7cc84SAlan Cox 				 */
179912d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
180012d7cc84SAlan Cox 				    &tmp_entry)) {
1801cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
1802cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
1803cbef13d8SAlan Cox 					else {
180412d7cc84SAlan Cox 						if (saved_start == start) {
180512d7cc84SAlan Cox 							/*
180612d7cc84SAlan Cox 							 * first_entry has been deleted.
180712d7cc84SAlan Cox 							 */
180812d7cc84SAlan Cox 							vm_map_unlock(map);
180912d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
181012d7cc84SAlan Cox 						}
181112d7cc84SAlan Cox 						end = saved_start;
181212d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
181312d7cc84SAlan Cox 						goto done;
181412d7cc84SAlan Cox 					}
1815cbef13d8SAlan Cox 				}
181612d7cc84SAlan Cox 				if (entry == first_entry)
181712d7cc84SAlan Cox 					first_entry = tmp_entry;
181812d7cc84SAlan Cox 				else
181912d7cc84SAlan Cox 					first_entry = NULL;
182012d7cc84SAlan Cox 				entry = tmp_entry;
182112d7cc84SAlan Cox 			}
182212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
182312d7cc84SAlan Cox 			continue;
182412d7cc84SAlan Cox 		}
182512d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
182612d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
182712d7cc84SAlan Cox 		/*
182812d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
182912d7cc84SAlan Cox 		 * above.)
183012d7cc84SAlan Cox 		 */
183112d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
183212d7cc84SAlan Cox 		/*
183312d7cc84SAlan Cox 		 *
183412d7cc84SAlan Cox 		 */
183512d7cc84SAlan Cox 		if (entry->wired_count == 0) {
183612d7cc84SAlan Cox 			entry->wired_count++;
183712d7cc84SAlan Cox 			saved_start = entry->start;
183812d7cc84SAlan Cox 			saved_end = entry->end;
183912d7cc84SAlan Cox 			/*
184012d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
184112d7cc84SAlan Cox 			 * mark.
184212d7cc84SAlan Cox 			 */
184312d7cc84SAlan Cox 			vm_map_unlock(map);
1844ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
1845ef594d31SAlan Cox 			    user_wire);
184612d7cc84SAlan Cox 			vm_map_lock(map);
184712d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
184812d7cc84SAlan Cox 				/*
184912d7cc84SAlan Cox 				 * Look again for the entry because the map was
185012d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
185112d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
185212d7cc84SAlan Cox 				 * deleted.
185312d7cc84SAlan Cox 				 */
185412d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
185512d7cc84SAlan Cox 				    &tmp_entry);
185612d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
185712d7cc84SAlan Cox 				if (entry == first_entry)
185812d7cc84SAlan Cox 					first_entry = tmp_entry;
185912d7cc84SAlan Cox 				else
186012d7cc84SAlan Cox 					first_entry = NULL;
186112d7cc84SAlan Cox 				entry = tmp_entry;
186228c58286SAlan Cox 				while (entry->end < saved_end) {
186328c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
186428c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
186528c58286SAlan Cox 						    ("vm_map_wire: bad count"));
186628c58286SAlan Cox 						entry->wired_count = -1;
186728c58286SAlan Cox 					}
186812d7cc84SAlan Cox 					entry = entry->next;
186912d7cc84SAlan Cox 				}
187028c58286SAlan Cox 			}
187112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
187212d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
187328c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
187428c58286SAlan Cox 				    ("vm_map_wire: bad count"));
187512d7cc84SAlan Cox 				/*
187628c58286SAlan Cox 				 * Assign an out-of-range value to represent
187728c58286SAlan Cox 				 * the failure to wire this entry.
187812d7cc84SAlan Cox 				 */
187928c58286SAlan Cox 				entry->wired_count = -1;
188012d7cc84SAlan Cox 				end = entry->end;
188112d7cc84SAlan Cox 				goto done;
188212d7cc84SAlan Cox 			}
188312d7cc84SAlan Cox 		} else if (!user_wire ||
188412d7cc84SAlan Cox 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
188512d7cc84SAlan Cox 			entry->wired_count++;
188612d7cc84SAlan Cox 		}
188712d7cc84SAlan Cox 		/*
188812d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
1889abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
189012d7cc84SAlan Cox 		 */
1891abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
1892abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
1893abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
189412d7cc84SAlan Cox 			end = entry->end;
189512d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
189612d7cc84SAlan Cox 			goto done;
189712d7cc84SAlan Cox 		}
189812d7cc84SAlan Cox 		entry = entry->next;
189912d7cc84SAlan Cox 	}
190012d7cc84SAlan Cox 	rv = KERN_SUCCESS;
190112d7cc84SAlan Cox done:
190212d7cc84SAlan Cox 	need_wakeup = FALSE;
190312d7cc84SAlan Cox 	if (first_entry == NULL) {
190412d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
1905cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
1906cbef13d8SAlan Cox 			first_entry = first_entry->next;
1907cbef13d8SAlan Cox 		else
190812d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
190912d7cc84SAlan Cox 	}
191012d7cc84SAlan Cox 	entry = first_entry;
191112d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
191212d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
191312d7cc84SAlan Cox 			if (user_wire)
191412d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
191528c58286SAlan Cox 		} else if (entry->wired_count == -1) {
191628c58286SAlan Cox 			/*
191728c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
191828c58286SAlan Cox 			 * unnecessary.
191928c58286SAlan Cox 			 */
192028c58286SAlan Cox 			entry->wired_count = 0;
192112d7cc84SAlan Cox 		} else {
1922f6116791SAlan Cox 			if (!user_wire ||
1923f6116791SAlan Cox 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
192412d7cc84SAlan Cox 				entry->wired_count--;
192512d7cc84SAlan Cox 			if (entry->wired_count == 0) {
192612d7cc84SAlan Cox 				/*
192712d7cc84SAlan Cox 				 * Retain the map lock.
192812d7cc84SAlan Cox 				 */
192912d7cc84SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end);
193012d7cc84SAlan Cox 			}
193112d7cc84SAlan Cox 		}
193212d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
193312d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
193412d7cc84SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
193512d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
193612d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
193712d7cc84SAlan Cox 			need_wakeup = TRUE;
193812d7cc84SAlan Cox 		}
193912d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
194012d7cc84SAlan Cox 		entry = entry->next;
194112d7cc84SAlan Cox 	}
194212d7cc84SAlan Cox 	vm_map_unlock(map);
194312d7cc84SAlan Cox 	if (need_wakeup)
194412d7cc84SAlan Cox 		vm_map_wakeup(map);
194512d7cc84SAlan Cox 	return (rv);
1946e27e17b7SAlan Cox }
1947e27e17b7SAlan Cox 
1948e27e17b7SAlan Cox /*
1949950f8459SAlan Cox  * vm_map_sync
1950df8bae1dSRodney W. Grimes  *
1951df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
1952df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
1953df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
1954df8bae1dSRodney W. Grimes  *
1955637315edSAlan Cox  * If the size of the region from start to end is zero, we are
1956637315edSAlan Cox  * supposed to flush all modified pages within the region containing
1957637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
1958637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
1959637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
1960637315edSAlan Cox  * flushing the current region containing start.
1961637315edSAlan Cox  *
1962df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
1963df8bae1dSRodney W. Grimes  */
1964df8bae1dSRodney W. Grimes int
1965950f8459SAlan Cox vm_map_sync(
19661b40f8c0SMatthew Dillon 	vm_map_t map,
19671b40f8c0SMatthew Dillon 	vm_offset_t start,
19681b40f8c0SMatthew Dillon 	vm_offset_t end,
19691b40f8c0SMatthew Dillon 	boolean_t syncio,
19701b40f8c0SMatthew Dillon 	boolean_t invalidate)
1971df8bae1dSRodney W. Grimes {
1972c0877f10SJohn Dyson 	vm_map_entry_t current;
1973df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1974df8bae1dSRodney W. Grimes 	vm_size_t size;
1975df8bae1dSRodney W. Grimes 	vm_object_t object;
1976a316d390SJohn Dyson 	vm_ooffset_t offset;
1977df8bae1dSRodney W. Grimes 
1978df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
1979df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1980df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
1981df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
1982df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
1983637315edSAlan Cox 	} else if (start == end) {
1984637315edSAlan Cox 		start = entry->start;
1985637315edSAlan Cox 		end = entry->end;
1986df8bae1dSRodney W. Grimes 	}
1987df8bae1dSRodney W. Grimes 	/*
1988b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
1989df8bae1dSRodney W. Grimes 	 */
1990df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
1991b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
1992df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1993df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1994df8bae1dSRodney W. Grimes 		}
1995df8bae1dSRodney W. Grimes 		if (end > current->end &&
1996df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
1997df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
1998df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
1999df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2000df8bae1dSRodney W. Grimes 		}
2001df8bae1dSRodney W. Grimes 	}
2002df8bae1dSRodney W. Grimes 
2003bc105a67SAlan Cox 	if (invalidate) {
2004950f8459SAlan Cox 		mtx_lock(&Giant);
2005bc105a67SAlan Cox 		vm_page_lock_queues();
2006bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2007bc105a67SAlan Cox 		vm_page_unlock_queues();
2008950f8459SAlan Cox 		mtx_unlock(&Giant);
2009bc105a67SAlan Cox 	}
2010df8bae1dSRodney W. Grimes 	/*
2011df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2012df8bae1dSRodney W. Grimes 	 * objects as we go.
2013df8bae1dSRodney W. Grimes 	 */
2014df8bae1dSRodney W. Grimes 	for (current = entry; current->start < end; current = current->next) {
2015df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2016df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
20179fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2018c0877f10SJohn Dyson 			vm_map_t smap;
2019df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2020df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2021df8bae1dSRodney W. Grimes 
20229fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2023df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2024df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2025df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2026df8bae1dSRodney W. Grimes 			if (tsize < size)
2027df8bae1dSRodney W. Grimes 				size = tsize;
2028df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2029df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2030df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2031df8bae1dSRodney W. Grimes 		} else {
2032df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2033df8bae1dSRodney W. Grimes 		}
2034950f8459SAlan Cox 		vm_object_sync(object, offset, size, syncio, invalidate);
2035df8bae1dSRodney W. Grimes 		start += size;
2036df8bae1dSRodney W. Grimes 	}
2037df8bae1dSRodney W. Grimes 
2038df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2039df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2040df8bae1dSRodney W. Grimes }
2041df8bae1dSRodney W. Grimes 
2042df8bae1dSRodney W. Grimes /*
2043df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2044df8bae1dSRodney W. Grimes  *
2045df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2046df8bae1dSRodney W. Grimes  *
2047df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2048df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2049df8bae1dSRodney W. Grimes  */
20500362d7d7SJohn Dyson static void
20511b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2052df8bae1dSRodney W. Grimes {
2053df8bae1dSRodney W. Grimes 	vm_fault_unwire(map, entry->start, entry->end);
2054df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2055df8bae1dSRodney W. Grimes }
2056df8bae1dSRodney W. Grimes 
2057df8bae1dSRodney W. Grimes /*
2058df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2059df8bae1dSRodney W. Grimes  *
2060df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2061df8bae1dSRodney W. Grimes  */
20620362d7d7SJohn Dyson static void
20631b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2064df8bae1dSRodney W. Grimes {
206532a89c32SAlan Cox 	vm_object_t object;
206632a89c32SAlan Cox 	vm_pindex_t offidxstart, offidxend, count;
206732a89c32SAlan Cox 
2068df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
2069df8bae1dSRodney W. Grimes 	map->size -= entry->end - entry->start;
2070df8bae1dSRodney W. Grimes 
207132a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
207232a89c32SAlan Cox 	    (object = entry->object.vm_object) != NULL) {
207332a89c32SAlan Cox 		count = OFF_TO_IDX(entry->end - entry->start);
207432a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
207532a89c32SAlan Cox 		offidxend = offidxstart + count;
207632a89c32SAlan Cox 		VM_OBJECT_LOCK(object);
207732a89c32SAlan Cox 		if (object->ref_count != 1 &&
207832a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
207932a89c32SAlan Cox 		     object == kernel_object || object == kmem_object) &&
208032a89c32SAlan Cox 		    (object->type == OBJT_DEFAULT || object->type == OBJT_SWAP)) {
208132a89c32SAlan Cox 			vm_object_collapse(object);
208232a89c32SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend, FALSE);
208332a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
208432a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
208532a89c32SAlan Cox 			if (offidxend >= object->size &&
208632a89c32SAlan Cox 			    offidxstart < object->size)
208732a89c32SAlan Cox 				object->size = offidxstart;
208832a89c32SAlan Cox 		}
208932a89c32SAlan Cox 		VM_OBJECT_UNLOCK(object);
209032a89c32SAlan Cox 		vm_object_deallocate(object);
2091b5b40fa6SJohn Dyson 	}
2092df8bae1dSRodney W. Grimes 
2093df8bae1dSRodney W. Grimes 	vm_map_entry_dispose(map, entry);
2094df8bae1dSRodney W. Grimes }
2095df8bae1dSRodney W. Grimes 
2096df8bae1dSRodney W. Grimes /*
2097df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2098df8bae1dSRodney W. Grimes  *
2099df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2100df8bae1dSRodney W. Grimes  *	map.
2101df8bae1dSRodney W. Grimes  */
2102df8bae1dSRodney W. Grimes int
21031b40f8c0SMatthew Dillon vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2104df8bae1dSRodney W. Grimes {
2105c0877f10SJohn Dyson 	vm_map_entry_t entry;
2106df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2107df8bae1dSRodney W. Grimes 
2108df8bae1dSRodney W. Grimes 	/*
2109df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2110df8bae1dSRodney W. Grimes 	 */
2111876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2112df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2113876318ecSAlan Cox 	else {
2114df8bae1dSRodney W. Grimes 		entry = first_entry;
2115df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2116df8bae1dSRodney W. Grimes 	}
2117df8bae1dSRodney W. Grimes 
2118df8bae1dSRodney W. Grimes 	/*
2119df8bae1dSRodney W. Grimes 	 * Save the free space hint
2120df8bae1dSRodney W. Grimes 	 */
2121b18bfc3dSJohn Dyson 	if (entry == &map->header) {
2122b18bfc3dSJohn Dyson 		map->first_free = &map->header;
21232dbea5d2SJohn Dyson 	} else if (map->first_free->start >= start) {
2124df8bae1dSRodney W. Grimes 		map->first_free = entry->prev;
21252dbea5d2SJohn Dyson 	}
2126df8bae1dSRodney W. Grimes 
2127df8bae1dSRodney W. Grimes 	/*
2128df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2129df8bae1dSRodney W. Grimes 	 */
2130df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2131df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2132df8bae1dSRodney W. Grimes 
213373b2baceSAlan Cox 		/*
213473b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
213573b2baceSAlan Cox 		 */
213673b2baceSAlan Cox 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0) {
213773b2baceSAlan Cox 			unsigned int last_timestamp;
213873b2baceSAlan Cox 			vm_offset_t saved_start;
213973b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
214073b2baceSAlan Cox 
214173b2baceSAlan Cox 			saved_start = entry->start;
214273b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
214373b2baceSAlan Cox 			last_timestamp = map->timestamp;
214473b2baceSAlan Cox 			(void) vm_map_unlock_and_wait(map, FALSE);
214573b2baceSAlan Cox 			vm_map_lock(map);
214673b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
214773b2baceSAlan Cox 				/*
214873b2baceSAlan Cox 				 * Look again for the entry because the map was
214973b2baceSAlan Cox 				 * modified while it was unlocked.
215073b2baceSAlan Cox 				 * Specifically, the entry may have been
215173b2baceSAlan Cox 				 * clipped, merged, or deleted.
215273b2baceSAlan Cox 				 */
215373b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
215473b2baceSAlan Cox 							 &tmp_entry))
215573b2baceSAlan Cox 					entry = tmp_entry->next;
215673b2baceSAlan Cox 				else {
215773b2baceSAlan Cox 					entry = tmp_entry;
215873b2baceSAlan Cox 					vm_map_clip_start(map, entry,
215973b2baceSAlan Cox 							  saved_start);
216073b2baceSAlan Cox 				}
216173b2baceSAlan Cox 			}
216273b2baceSAlan Cox 			continue;
216373b2baceSAlan Cox 		}
2164df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2165df8bae1dSRodney W. Grimes 
2166c0877f10SJohn Dyson 		next = entry->next;
2167df8bae1dSRodney W. Grimes 
2168df8bae1dSRodney W. Grimes 		/*
21690d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
21700d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2171df8bae1dSRodney W. Grimes 		 */
2172c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2173df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2174c0877f10SJohn Dyson 		}
2175df8bae1dSRodney W. Grimes 
21761cd5fbd8SAlan Cox 		if (map != kmem_map)
21777d040e3cSAlan Cox 			mtx_lock(&Giant);
2178bc105a67SAlan Cox 		vm_page_lock_queues();
217932a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2180bc105a67SAlan Cox 		vm_page_unlock_queues();
21811cd5fbd8SAlan Cox 		if (map != kmem_map)
21827d040e3cSAlan Cox 			mtx_unlock(&Giant);
2183df8bae1dSRodney W. Grimes 
2184df8bae1dSRodney W. Grimes 		/*
21850d94caffSDavid Greenman 		 * Delete the entry (which may delete the object) only after
21860d94caffSDavid Greenman 		 * removing all pmap entries pointing to its pages.
21870d94caffSDavid Greenman 		 * (Otherwise, its page frames may be reallocated, and any
21880d94caffSDavid Greenman 		 * modify bits will be set in the wrong object!)
2189df8bae1dSRodney W. Grimes 		 */
2190df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2191df8bae1dSRodney W. Grimes 		entry = next;
2192df8bae1dSRodney W. Grimes 	}
2193df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2194df8bae1dSRodney W. Grimes }
2195df8bae1dSRodney W. Grimes 
2196df8bae1dSRodney W. Grimes /*
2197df8bae1dSRodney W. Grimes  *	vm_map_remove:
2198df8bae1dSRodney W. Grimes  *
2199df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2200df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2201df8bae1dSRodney W. Grimes  */
2202df8bae1dSRodney W. Grimes int
22031b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2204df8bae1dSRodney W. Grimes {
2205c0877f10SJohn Dyson 	int result, s = 0;
22068d6e8edeSDavid Greenman 
220708442f8aSBosko Milekic 	if (map == kmem_map)
2208b18bfc3dSJohn Dyson 		s = splvm();
2209df8bae1dSRodney W. Grimes 
2210df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2211df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2212df8bae1dSRodney W. Grimes 	result = vm_map_delete(map, start, end);
2213df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2214df8bae1dSRodney W. Grimes 
221508442f8aSBosko Milekic 	if (map == kmem_map)
22168d6e8edeSDavid Greenman 		splx(s);
22178d6e8edeSDavid Greenman 
2218df8bae1dSRodney W. Grimes 	return (result);
2219df8bae1dSRodney W. Grimes }
2220df8bae1dSRodney W. Grimes 
2221df8bae1dSRodney W. Grimes /*
2222df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2223df8bae1dSRodney W. Grimes  *
22242d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
22252d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
22262d5c7e45SMatthew Dillon  *
22272d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
22282d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
22292d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
22302d5c7e45SMatthew Dillon  *
22312d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2232d8834602SAlan Cox  *
2233d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2234df8bae1dSRodney W. Grimes  */
22350d94caffSDavid Greenman boolean_t
2236b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2237b9dcd593SBruce Evans 			vm_prot_t protection)
2238df8bae1dSRodney W. Grimes {
2239c0877f10SJohn Dyson 	vm_map_entry_t entry;
2240df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2241df8bae1dSRodney W. Grimes 
2242d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2243df8bae1dSRodney W. Grimes 		return (FALSE);
2244df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2245df8bae1dSRodney W. Grimes 
2246df8bae1dSRodney W. Grimes 	while (start < end) {
2247d8834602SAlan Cox 		if (entry == &map->header)
2248df8bae1dSRodney W. Grimes 			return (FALSE);
2249df8bae1dSRodney W. Grimes 		/*
2250df8bae1dSRodney W. Grimes 		 * No holes allowed!
2251df8bae1dSRodney W. Grimes 		 */
2252d8834602SAlan Cox 		if (start < entry->start)
2253df8bae1dSRodney W. Grimes 			return (FALSE);
2254df8bae1dSRodney W. Grimes 		/*
2255df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2256df8bae1dSRodney W. Grimes 		 */
2257d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2258df8bae1dSRodney W. Grimes 			return (FALSE);
2259df8bae1dSRodney W. Grimes 		/* go to next entry */
2260df8bae1dSRodney W. Grimes 		start = entry->end;
2261df8bae1dSRodney W. Grimes 		entry = entry->next;
2262df8bae1dSRodney W. Grimes 	}
2263df8bae1dSRodney W. Grimes 	return (TRUE);
2264df8bae1dSRodney W. Grimes }
2265df8bae1dSRodney W. Grimes 
226686524867SJohn Dyson /*
2267df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2268df8bae1dSRodney W. Grimes  *
2269df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2270df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2271df8bae1dSRodney W. Grimes  */
2272f708ef1bSPoul-Henning Kamp static void
22731b40f8c0SMatthew Dillon vm_map_copy_entry(
22741b40f8c0SMatthew Dillon 	vm_map_t src_map,
22751b40f8c0SMatthew Dillon 	vm_map_t dst_map,
22761b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
22771b40f8c0SMatthew Dillon 	vm_map_entry_t dst_entry)
2278df8bae1dSRodney W. Grimes {
2279c0877f10SJohn Dyson 	vm_object_t src_object;
2280c0877f10SJohn Dyson 
22819fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2282df8bae1dSRodney W. Grimes 		return;
2283df8bae1dSRodney W. Grimes 
2284df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2285df8bae1dSRodney W. Grimes 
2286df8bae1dSRodney W. Grimes 		/*
22870d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
22880d94caffSDavid Greenman 		 * write-protected.
2289df8bae1dSRodney W. Grimes 		 */
2290afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
229185e03a7eSAlan Cox 			vm_page_lock_queues();
2292df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2293df8bae1dSRodney W. Grimes 			    src_entry->start,
2294df8bae1dSRodney W. Grimes 			    src_entry->end,
2295df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
229685e03a7eSAlan Cox 			vm_page_unlock_queues();
2297df8bae1dSRodney W. Grimes 		}
2298b18bfc3dSJohn Dyson 
2299df8bae1dSRodney W. Grimes 		/*
2300df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2301df8bae1dSRodney W. Grimes 		 */
23028aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
2303a89c6258SAlan Cox 			VM_OBJECT_LOCK(src_object);
2304c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2305c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2306c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2307c0877f10SJohn Dyson 				vm_object_collapse(src_object);
230896fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2309c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2310c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2311a89c6258SAlan Cox 				}
2312a89c6258SAlan Cox 			}
2313b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2314069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2315e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(src_object);
2316c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
2317afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2318afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
2319b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
2320b18bfc3dSJohn Dyson 		} else {
2321b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
2322b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
2323b18bfc3dSJohn Dyson 		}
2324df8bae1dSRodney W. Grimes 
2325df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
2326df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
23270d94caffSDavid Greenman 	} else {
2328df8bae1dSRodney W. Grimes 		/*
2329df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
23300d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
23310d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
2332df8bae1dSRodney W. Grimes 		 */
2333df8bae1dSRodney W. Grimes 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry);
2334df8bae1dSRodney W. Grimes 	}
2335df8bae1dSRodney W. Grimes }
2336df8bae1dSRodney W. Grimes 
2337df8bae1dSRodney W. Grimes /*
2338df8bae1dSRodney W. Grimes  * vmspace_fork:
2339df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
2340df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
2341df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
2342df8bae1dSRodney W. Grimes  * values on the regions in that map.
2343df8bae1dSRodney W. Grimes  *
2344df8bae1dSRodney W. Grimes  * The source map must not be locked.
2345df8bae1dSRodney W. Grimes  */
2346df8bae1dSRodney W. Grimes struct vmspace *
23471b40f8c0SMatthew Dillon vmspace_fork(struct vmspace *vm1)
2348df8bae1dSRodney W. Grimes {
2349c0877f10SJohn Dyson 	struct vmspace *vm2;
2350df8bae1dSRodney W. Grimes 	vm_map_t old_map = &vm1->vm_map;
2351df8bae1dSRodney W. Grimes 	vm_map_t new_map;
2352df8bae1dSRodney W. Grimes 	vm_map_entry_t old_entry;
2353df8bae1dSRodney W. Grimes 	vm_map_entry_t new_entry;
2354de5f6a77SJohn Dyson 	vm_object_t object;
2355df8bae1dSRodney W. Grimes 
23560cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
23570cddd8f0SMatthew Dillon 
2358df8bae1dSRodney W. Grimes 	vm_map_lock(old_map);
2359b823bbd6SMatthew Dillon 	old_map->infork = 1;
2360df8bae1dSRodney W. Grimes 
23612d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
2362df8bae1dSRodney W. Grimes 	bcopy(&vm1->vm_startcopy, &vm2->vm_startcopy,
2363582ec34cSAlfred Perlstein 	    (caddr_t) &vm1->vm_endcopy - (caddr_t) &vm1->vm_startcopy);
2364df8bae1dSRodney W. Grimes 	new_map = &vm2->vm_map;	/* XXX */
236547221757SJohn Dyson 	new_map->timestamp = 1;
2366df8bae1dSRodney W. Grimes 
2367abd498aaSBruce M Simpson 	/* Do not inherit the MAP_WIREFUTURE property. */
2368abd498aaSBruce M Simpson 	if ((new_map->flags & MAP_WIREFUTURE) == MAP_WIREFUTURE)
2369abd498aaSBruce M Simpson 		new_map->flags &= ~MAP_WIREFUTURE;
2370abd498aaSBruce M Simpson 
2371df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
2372df8bae1dSRodney W. Grimes 
2373df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
2374afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
2375df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
2376df8bae1dSRodney W. Grimes 
2377df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
2378df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
2379df8bae1dSRodney W. Grimes 			break;
2380df8bae1dSRodney W. Grimes 
2381df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
2382df8bae1dSRodney W. Grimes 			/*
2383fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
2384fed9a903SJohn Dyson 			 */
2385fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
2386fed9a903SJohn Dyson 			if (object == NULL) {
2387fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
2388c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
2389fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
2390fed9a903SJohn Dyson 				old_entry->offset = (vm_offset_t) 0;
23919a2f6362SAlan Cox 			}
23929a2f6362SAlan Cox 
23939a2f6362SAlan Cox 			/*
23949a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
23959a2f6362SAlan Cox 			 * to insure that a shadow object is created.
23969a2f6362SAlan Cox 			 */
23979a2f6362SAlan Cox 			vm_object_reference(object);
23989a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
23995069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
24005069bf57SJohn Dyson 					&old_entry->offset,
2401c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
24025069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
2403d30344bdSIan Dowse 				/* Transfer the second reference too. */
2404d30344bdSIan Dowse 				vm_object_reference(
2405d30344bdSIan Dowse 				    old_entry->object.vm_object);
2406d30344bdSIan Dowse 				vm_object_deallocate(object);
24075069bf57SJohn Dyson 				object = old_entry->object.vm_object;
2408fed9a903SJohn Dyson 			}
2409e2479b4fSAlan Cox 			VM_OBJECT_LOCK(object);
2410069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
2411e2479b4fSAlan Cox 			VM_OBJECT_UNLOCK(object);
2412fed9a903SJohn Dyson 
2413fed9a903SJohn Dyson 			/*
2414ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
2415df8bae1dSRodney W. Grimes 			 */
2416df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2417df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2418028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2419df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2420df8bae1dSRodney W. Grimes 
2421df8bae1dSRodney W. Grimes 			/*
24220d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
24230d94caffSDavid Greenman 			 * inserting at the end of the new map.
2424df8bae1dSRodney W. Grimes 			 */
2425df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2426df8bae1dSRodney W. Grimes 			    new_entry);
2427df8bae1dSRodney W. Grimes 
2428df8bae1dSRodney W. Grimes 			/*
2429df8bae1dSRodney W. Grimes 			 * Update the physical map
2430df8bae1dSRodney W. Grimes 			 */
2431df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
2432df8bae1dSRodney W. Grimes 			    new_entry->start,
2433df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
2434df8bae1dSRodney W. Grimes 			    old_entry->start);
2435df8bae1dSRodney W. Grimes 			break;
2436df8bae1dSRodney W. Grimes 
2437df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
2438df8bae1dSRodney W. Grimes 			/*
2439df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
2440df8bae1dSRodney W. Grimes 			 */
2441df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
2442df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
2443028fe6ecSTor Egge 			new_entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2444df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
2445df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
2446df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
2447df8bae1dSRodney W. Grimes 			    new_entry);
2448bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
2449bd7e5f99SJohn Dyson 			    new_entry);
2450df8bae1dSRodney W. Grimes 			break;
2451df8bae1dSRodney W. Grimes 		}
2452df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
2453df8bae1dSRodney W. Grimes 	}
2454df8bae1dSRodney W. Grimes 
2455df8bae1dSRodney W. Grimes 	new_map->size = old_map->size;
2456b823bbd6SMatthew Dillon 	old_map->infork = 0;
2457df8bae1dSRodney W. Grimes 	vm_map_unlock(old_map);
2458df8bae1dSRodney W. Grimes 
2459df8bae1dSRodney W. Grimes 	return (vm2);
2460df8bae1dSRodney W. Grimes }
2461df8bae1dSRodney W. Grimes 
246294f7e29aSAlan Cox int
246394f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
246494f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
246594f7e29aSAlan Cox {
2466fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
2467fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
246894f7e29aSAlan Cox 	vm_size_t init_ssize;
2469fd75d710SMarcel Moolenaar 	int orient, rv;
247091d5354aSJohn Baldwin 	rlim_t vmemlim;
247194f7e29aSAlan Cox 
2472fd75d710SMarcel Moolenaar 	/*
2473fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
2474fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
2475fd75d710SMarcel Moolenaar 	 * don't pass it around further.
2476fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
2477fd75d710SMarcel Moolenaar 	 */
2478fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
2479fd75d710SMarcel Moolenaar 	cow &= ~orient;
2480fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
2481fd75d710SMarcel Moolenaar 
2482fd75d710SMarcel Moolenaar 	if (addrbos < vm_map_min(map) || addrbos > map->max_offset)
248394f7e29aSAlan Cox 		return (KERN_NO_SPACE);
2484fd75d710SMarcel Moolenaar 
2485fd75d710SMarcel Moolenaar 	init_ssize = (max_ssize < sgrowsiz) ? max_ssize : sgrowsiz;
248694f7e29aSAlan Cox 
248791d5354aSJohn Baldwin 	PROC_LOCK(curthread->td_proc);
248891d5354aSJohn Baldwin 	vmemlim = lim_cur(curthread->td_proc, RLIMIT_VMEM);
248991d5354aSJohn Baldwin 	PROC_UNLOCK(curthread->td_proc);
249091d5354aSJohn Baldwin 
249194f7e29aSAlan Cox 	vm_map_lock(map);
249294f7e29aSAlan Cox 
249394f7e29aSAlan Cox 	/* If addr is already mapped, no go */
249494f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
249594f7e29aSAlan Cox 		vm_map_unlock(map);
249694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
249794f7e29aSAlan Cox 	}
249894f7e29aSAlan Cox 
2499a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
250091d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
2501a69ac174SMatthew Dillon 		vm_map_unlock(map);
2502a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2503a69ac174SMatthew Dillon 	}
2504a69ac174SMatthew Dillon 
2505fd75d710SMarcel Moolenaar 	/*
2506fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
2507fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
2508fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
2509fd75d710SMarcel Moolenaar 	 * space is not protected.
251094f7e29aSAlan Cox 	 *
2511fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
2512fd75d710SMarcel Moolenaar 	 * grow the stack.
251394f7e29aSAlan Cox 	 */
251494f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
251594f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
251694f7e29aSAlan Cox 		vm_map_unlock(map);
251794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
251894f7e29aSAlan Cox 	}
251994f7e29aSAlan Cox 
2520fd75d710SMarcel Moolenaar 	/*
2521fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
2522fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
2523fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
2524fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
252594f7e29aSAlan Cox 	 *
2526fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
2527fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
2528fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
252994f7e29aSAlan Cox 	 */
2530fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
2531fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
2532fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
2533fd75d710SMarcel Moolenaar 		bot = addrbos;
2534fd75d710SMarcel Moolenaar 	else
2535fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
2536fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
2537fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
253894f7e29aSAlan Cox 
2539fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
254094f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
254129b45e9eSAlan Cox 		if (prev_entry != &map->header)
2542fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
2543fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
2544fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
254594f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
2546b21a0008SMarcel Moolenaar 
2547fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
2548fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
2549fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
2550fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
2551fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
255294f7e29aSAlan Cox 	}
255394f7e29aSAlan Cox 
255494f7e29aSAlan Cox 	vm_map_unlock(map);
255594f7e29aSAlan Cox 	return (rv);
255694f7e29aSAlan Cox }
255794f7e29aSAlan Cox 
255894f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
255994f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
256094f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
256194f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
256294f7e29aSAlan Cox  * the grow function in vm_machdep.c).
256394f7e29aSAlan Cox  */
256494f7e29aSAlan Cox int
256594f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
256694f7e29aSAlan Cox {
2567b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
2568b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
256994f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
257094f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
257194f7e29aSAlan Cox 	vm_offset_t end;
2572b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
257391d5354aSJohn Baldwin 	rlim_t stacklim, vmemlim;
2574b21a0008SMarcel Moolenaar 	int is_procstack, rv;
257523955314SAlfred Perlstein 
257694f7e29aSAlan Cox Retry:
257791d5354aSJohn Baldwin 	PROC_LOCK(p);
257891d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
2579bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
258091d5354aSJohn Baldwin 	PROC_UNLOCK(p);
258191d5354aSJohn Baldwin 
258294f7e29aSAlan Cox 	vm_map_lock_read(map);
258394f7e29aSAlan Cox 
258494f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
258594f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
258694f7e29aSAlan Cox 		vm_map_unlock_read(map);
25870cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
258894f7e29aSAlan Cox 	}
258994f7e29aSAlan Cox 
2590b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
2591b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
2592b21a0008SMarcel Moolenaar 		/*
2593b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
2594b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
2595b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
2596b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
259794f7e29aSAlan Cox 		 */
2598b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
259994f7e29aSAlan Cox 			vm_map_unlock_read(map);
26000cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
260194f7e29aSAlan Cox 		}
2602b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
2603b21a0008SMarcel Moolenaar 	} else {
2604b21a0008SMarcel Moolenaar 		/*
2605b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
2606b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
2607b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
2608b21a0008SMarcel Moolenaar 		 * select one.
2609b21a0008SMarcel Moolenaar 		 */
2610b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
2611b21a0008SMarcel Moolenaar 			/*
2612b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
2613b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
2614b21a0008SMarcel Moolenaar 			 */
2615b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
2616b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
2617b21a0008SMarcel Moolenaar 			else
2618b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
2619b21a0008SMarcel Moolenaar 		} else
2620b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
2621b21a0008SMarcel Moolenaar 	}
262294f7e29aSAlan Cox 
2623b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2624b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
2625b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
2626b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
2627b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
262894f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
2629b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
2630b21a0008SMarcel Moolenaar 	} else {
2631b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
263208667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
2633b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
2634b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
2635fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
2636b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
2637b21a0008SMarcel Moolenaar 	}
2638b21a0008SMarcel Moolenaar 
263994f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
264094f7e29aSAlan Cox 		vm_map_unlock_read(map);
26410cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
264294f7e29aSAlan Cox 	}
264394f7e29aSAlan Cox 
2644b21a0008SMarcel Moolenaar 	/*
2645b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
2646b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
2647b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
2648b21a0008SMarcel Moolenaar 	 * and is probably an error.
264994f7e29aSAlan Cox 	 *
2650b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
2651b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
265294f7e29aSAlan Cox 	 */
2653b21a0008SMarcel Moolenaar 	if (grow_amount > max_grow) {
265425adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
265594f7e29aSAlan Cox 			goto Retry;
265694f7e29aSAlan Cox 
2657b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
265894f7e29aSAlan Cox 
265994f7e29aSAlan Cox 		vm_map_unlock(map);
26600cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
266194f7e29aSAlan Cox 	}
266294f7e29aSAlan Cox 
2663b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
266494f7e29aSAlan Cox 
2665b21a0008SMarcel Moolenaar 	/*
2666b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
2667b21a0008SMarcel Moolenaar 	 * limit.
266894f7e29aSAlan Cox 	 */
266991d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
267094f7e29aSAlan Cox 		vm_map_unlock_read(map);
26710cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
267294f7e29aSAlan Cox 	}
267394f7e29aSAlan Cox 
267494f7e29aSAlan Cox 	/* Round up the grow amount modulo SGROWSIZ */
2675cbc89bfbSPaul Saab 	grow_amount = roundup (grow_amount, sgrowsiz);
2676b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
267794f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
267891d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
267991d5354aSJohn Baldwin 		grow_amount = stacklim - ctob(vm->vm_ssize);
268094f7e29aSAlan Cox 	}
268194f7e29aSAlan Cox 
2682a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
268391d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
2684a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
2685a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
2686a69ac174SMatthew Dillon 	}
2687a69ac174SMatthew Dillon 
268825adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
268994f7e29aSAlan Cox 		goto Retry;
269094f7e29aSAlan Cox 
2691b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
2692b21a0008SMarcel Moolenaar 		/*
2693b21a0008SMarcel Moolenaar 		 * Growing downward.
2694b21a0008SMarcel Moolenaar 		 */
269594f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
269694f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
269794f7e29aSAlan Cox 
2698b21a0008SMarcel Moolenaar 		/*
2699b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
2700b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
270194f7e29aSAlan Cox 		 */
270294f7e29aSAlan Cox 		if (addr < end) {
2703b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
270494f7e29aSAlan Cox 			addr = end;
270594f7e29aSAlan Cox 		}
270694f7e29aSAlan Cox 
270794f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
270805ba50f5SJake Burkholder 		    p->p_sysent->sv_stackprot, VM_PROT_ALL, 0);
270994f7e29aSAlan Cox 
271094f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
271194f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
271229b45e9eSAlan Cox 			if (prev_entry != &map->header)
271329b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
2714b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
2715b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
2716b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
2717b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
2718b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
2719b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
2720b21a0008SMarcel Moolenaar 			    grow_amount;
2721b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
2722b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
272394f7e29aSAlan Cox 		}
2724b21a0008SMarcel Moolenaar 	} else {
2725b21a0008SMarcel Moolenaar 		/*
2726b21a0008SMarcel Moolenaar 		 * Growing upward.
2727b21a0008SMarcel Moolenaar 		 */
2728b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
2729b21a0008SMarcel Moolenaar 
2730b21a0008SMarcel Moolenaar 		/*
2731b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
2732b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
2733b21a0008SMarcel Moolenaar 		 */
2734b21a0008SMarcel Moolenaar 		if (addr > end) {
2735b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
2736b21a0008SMarcel Moolenaar 			addr = end;
273794f7e29aSAlan Cox 		}
273894f7e29aSAlan Cox 
2739b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
2740b21a0008SMarcel Moolenaar 
2741b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
2742b21a0008SMarcel Moolenaar 		if (stack_entry->object.vm_object == NULL ||
2743b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
2744b21a0008SMarcel Moolenaar 		    OFF_TO_IDX(stack_entry->offset),
2745b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
2746b21a0008SMarcel Moolenaar 		    (vm_size_t)grow_amount)) {
274708667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
2748b21a0008SMarcel Moolenaar 			/* Update the current entry. */
2749b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
2750199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
2751b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
2752b21a0008SMarcel Moolenaar 
2753b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
2754b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
2755b21a0008SMarcel Moolenaar 		} else
2756b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
2757b21a0008SMarcel Moolenaar 	}
2758b21a0008SMarcel Moolenaar 
2759b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
2760b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
2761b21a0008SMarcel Moolenaar 
276294f7e29aSAlan Cox 	vm_map_unlock(map);
2763b21a0008SMarcel Moolenaar 
2764abd498aaSBruce M Simpson 	/*
2765abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
2766abd498aaSBruce M Simpson 	 */
2767b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
2768b21a0008SMarcel Moolenaar 		vm_map_wire(map,
2769b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
2770b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
2771b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
2772b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
2773b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
2774b21a0008SMarcel Moolenaar 	}
2775abd498aaSBruce M Simpson 
27760cddd8f0SMatthew Dillon 	return (rv);
277794f7e29aSAlan Cox }
277894f7e29aSAlan Cox 
2779df8bae1dSRodney W. Grimes /*
27805856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
27815856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
27825856e12eSJohn Dyson  */
27835856e12eSJohn Dyson void
27843ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
27851b40f8c0SMatthew Dillon {
27865856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
27875856e12eSJohn Dyson 	struct vmspace *newvmspace;
27885856e12eSJohn Dyson 
27890cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
27903ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
27915856e12eSJohn Dyson 	bcopy(&oldvmspace->vm_startcopy, &newvmspace->vm_startcopy,
27925856e12eSJohn Dyson 	    (caddr_t) (newvmspace + 1) - (caddr_t) &newvmspace->vm_startcopy);
27935856e12eSJohn Dyson 	/*
27945856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
27955856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
27965856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
27975856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
27985856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
27995856e12eSJohn Dyson 	 */
28005856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2801d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
2802b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2803b40ce416SJulian Elischer 		pmap_activate(curthread);
2804b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
28055856e12eSJohn Dyson }
28065856e12eSJohn Dyson 
28075856e12eSJohn Dyson /*
28085856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
28095856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
28105856e12eSJohn Dyson  */
28115856e12eSJohn Dyson void
28121b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
28131b40f8c0SMatthew Dillon {
28145856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
28155856e12eSJohn Dyson 	struct vmspace *newvmspace;
28165856e12eSJohn Dyson 
28170cddd8f0SMatthew Dillon 	GIANT_REQUIRED;
28185856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
28195856e12eSJohn Dyson 		return;
28205856e12eSJohn Dyson 	newvmspace = vmspace_fork(oldvmspace);
28215856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
2822d4da2dbaSAlan Cox 	pmap_pinit2(vmspace_pmap(newvmspace));
2823b40ce416SJulian Elischer 	if (p == curthread->td_proc)		/* XXXKSE ? */
2824b40ce416SJulian Elischer 		pmap_activate(curthread);
2825b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
28265856e12eSJohn Dyson }
28275856e12eSJohn Dyson 
28285856e12eSJohn Dyson /*
2829df8bae1dSRodney W. Grimes  *	vm_map_lookup:
2830df8bae1dSRodney W. Grimes  *
2831df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
2832df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
2833df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
2834df8bae1dSRodney W. Grimes  *	type specified.
2835df8bae1dSRodney W. Grimes  *
2836df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
2837df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
2838df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
2839df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
2840df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
2841df8bae1dSRodney W. Grimes  *
2842df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
2843df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
2844df8bae1dSRodney W. Grimes  *
2845df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
2846df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
2847df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
2848df8bae1dSRodney W. Grimes  *	remain the same.
2849df8bae1dSRodney W. Grimes  */
2850df8bae1dSRodney W. Grimes int
2851b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
2852b9dcd593SBruce Evans 	      vm_offset_t vaddr,
285347221757SJohn Dyson 	      vm_prot_t fault_typea,
2854b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
2855b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
2856b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
2857b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
28582d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
2859df8bae1dSRodney W. Grimes {
2860c0877f10SJohn Dyson 	vm_map_entry_t entry;
2861c0877f10SJohn Dyson 	vm_map_t map = *var_map;
2862c0877f10SJohn Dyson 	vm_prot_t prot;
286347221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
2864df8bae1dSRodney W. Grimes 
2865df8bae1dSRodney W. Grimes RetryLookup:;
2866df8bae1dSRodney W. Grimes 	/*
2867df8bae1dSRodney W. Grimes 	 * Lookup the faulting address.
2868df8bae1dSRodney W. Grimes 	 */
2869df8bae1dSRodney W. Grimes 
2870df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2871df8bae1dSRodney W. Grimes #define	RETURN(why) \
2872df8bae1dSRodney W. Grimes 		{ \
2873df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map); \
2874df8bae1dSRodney W. Grimes 		return (why); \
2875df8bae1dSRodney W. Grimes 		}
2876df8bae1dSRodney W. Grimes 
2877df8bae1dSRodney W. Grimes 	/*
28780d94caffSDavid Greenman 	 * If the map has an interesting hint, try it before calling full
28790d94caffSDavid Greenman 	 * blown lookup routine.
2880df8bae1dSRodney W. Grimes 	 */
28814e94f402SAlan Cox 	entry = map->root;
2882df8bae1dSRodney W. Grimes 	*out_entry = entry;
28834e94f402SAlan Cox 	if (entry == NULL ||
2884df8bae1dSRodney W. Grimes 	    (vaddr < entry->start) || (vaddr >= entry->end)) {
2885df8bae1dSRodney W. Grimes 		/*
28860d94caffSDavid Greenman 		 * Entry was either not a valid hint, or the vaddr was not
28870d94caffSDavid Greenman 		 * contained in the entry, so do a full lookup.
2888df8bae1dSRodney W. Grimes 		 */
28894e94f402SAlan Cox 		if (!vm_map_lookup_entry(map, vaddr, out_entry))
2890df8bae1dSRodney W. Grimes 			RETURN(KERN_INVALID_ADDRESS);
2891df8bae1dSRodney W. Grimes 
28924e94f402SAlan Cox 		entry = *out_entry;
2893df8bae1dSRodney W. Grimes 	}
2894b7b2aac2SJohn Dyson 
2895df8bae1dSRodney W. Grimes 	/*
2896df8bae1dSRodney W. Grimes 	 * Handle submaps.
2897df8bae1dSRodney W. Grimes 	 */
2898afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
2899df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
2900df8bae1dSRodney W. Grimes 
2901df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
2902df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
2903df8bae1dSRodney W. Grimes 		goto RetryLookup;
2904df8bae1dSRodney W. Grimes 	}
2905a04c970aSJohn Dyson 
2906df8bae1dSRodney W. Grimes 	/*
29070d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
2908a04c970aSJohn Dyson 	 * Note the special case for MAP_ENTRY_COW
2909a04c970aSJohn Dyson 	 * pages with an override.  This is to implement a forced
2910a04c970aSJohn Dyson 	 * COW for debuggers.
2911df8bae1dSRodney W. Grimes 	 */
2912480ba2f5SJohn Dyson 	if (fault_type & VM_PROT_OVERRIDE_WRITE)
2913480ba2f5SJohn Dyson 		prot = entry->max_protection;
2914480ba2f5SJohn Dyson 	else
2915df8bae1dSRodney W. Grimes 		prot = entry->protection;
291647221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
291747221757SJohn Dyson 	if ((fault_type & prot) != fault_type) {
291847221757SJohn Dyson 			RETURN(KERN_PROTECTION_FAILURE);
291947221757SJohn Dyson 	}
29202ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
292147221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
29222ed14a92SAlan Cox 	    (fault_type & VM_PROT_WRITE) &&
292347221757SJohn Dyson 	    (fault_typea & VM_PROT_OVERRIDE_WRITE) == 0) {
2924df8bae1dSRodney W. Grimes 		RETURN(KERN_PROTECTION_FAILURE);
2925a04c970aSJohn Dyson 	}
2926df8bae1dSRodney W. Grimes 
2927df8bae1dSRodney W. Grimes 	/*
29280d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
29290d94caffSDavid Greenman 	 * accesses.
2930df8bae1dSRodney W. Grimes 	 */
293105f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
293205f0fdd2SPoul-Henning Kamp 	if (*wired)
2933df8bae1dSRodney W. Grimes 		prot = fault_type = entry->protection;
2934df8bae1dSRodney W. Grimes 
2935df8bae1dSRodney W. Grimes 	/*
2936df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
2937df8bae1dSRodney W. Grimes 	 */
2938afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
2939df8bae1dSRodney W. Grimes 		/*
29400d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
2941ad5fca3bSAlan Cox 		 * now since we've got the map locked.
2942df8bae1dSRodney W. Grimes 		 *
29430d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
29440d94caffSDavid Greenman 		 * permissions allowed.
2945df8bae1dSRodney W. Grimes 		 */
2946df8bae1dSRodney W. Grimes 		if (fault_type & VM_PROT_WRITE) {
2947df8bae1dSRodney W. Grimes 			/*
29480d94caffSDavid Greenman 			 * Make a new object, and place it in the object
29490d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
2950ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
29510d94caffSDavid Greenman 			 * object.
2952df8bae1dSRodney W. Grimes 			 */
295325adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
2954df8bae1dSRodney W. Grimes 				goto RetryLookup;
29559917e010SAlan Cox 
2956df8bae1dSRodney W. Grimes 			vm_object_shadow(
2957df8bae1dSRodney W. Grimes 			    &entry->object.vm_object,
2958df8bae1dSRodney W. Grimes 			    &entry->offset,
2959c2e11a03SJohn Dyson 			    atop(entry->end - entry->start));
2960afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
29619917e010SAlan Cox 
29629b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
29630d94caffSDavid Greenman 		} else {
2964df8bae1dSRodney W. Grimes 			/*
29650d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
29660d94caffSDavid Greenman 			 * don't allow writes.
2967df8bae1dSRodney W. Grimes 			 */
29682d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
2969df8bae1dSRodney W. Grimes 		}
2970df8bae1dSRodney W. Grimes 	}
29712d8acc0fSJohn Dyson 
2972df8bae1dSRodney W. Grimes 	/*
2973df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
2974df8bae1dSRodney W. Grimes 	 */
29754e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
29764e71e795SMatthew Dillon 	    !map->system_map) {
297725adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
2978df8bae1dSRodney W. Grimes 			goto RetryLookup;
297924a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
2980c2e11a03SJohn Dyson 		    atop(entry->end - entry->start));
2981df8bae1dSRodney W. Grimes 		entry->offset = 0;
29829b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
2983df8bae1dSRodney W. Grimes 	}
2984b5b40fa6SJohn Dyson 
2985df8bae1dSRodney W. Grimes 	/*
29860d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
29870d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
2988df8bae1dSRodney W. Grimes 	 */
29899b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
2990df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
2991df8bae1dSRodney W. Grimes 
2992df8bae1dSRodney W. Grimes 	/*
2993df8bae1dSRodney W. Grimes 	 * Return whether this is the only map sharing this data.
2994df8bae1dSRodney W. Grimes 	 */
2995df8bae1dSRodney W. Grimes 	*out_prot = prot;
2996df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2997df8bae1dSRodney W. Grimes 
2998df8bae1dSRodney W. Grimes #undef	RETURN
2999df8bae1dSRodney W. Grimes }
3000df8bae1dSRodney W. Grimes 
3001df8bae1dSRodney W. Grimes /*
3002df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
3003df8bae1dSRodney W. Grimes  *
3004df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
3005df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
3006df8bae1dSRodney W. Grimes  */
30070d94caffSDavid Greenman void
30081b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
3009df8bae1dSRodney W. Grimes {
3010df8bae1dSRodney W. Grimes 	/*
3011df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
3012df8bae1dSRodney W. Grimes 	 */
3013df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
3014df8bae1dSRodney W. Grimes }
3015df8bae1dSRodney W. Grimes 
3016c7c34a24SBruce Evans #include "opt_ddb.h"
3017c3cb3e12SDavid Greenman #ifdef DDB
3018c7c34a24SBruce Evans #include <sys/kernel.h>
3019c7c34a24SBruce Evans 
3020c7c34a24SBruce Evans #include <ddb/ddb.h>
3021c7c34a24SBruce Evans 
3022df8bae1dSRodney W. Grimes /*
3023df8bae1dSRodney W. Grimes  *	vm_map_print:	[ debug ]
3024df8bae1dSRodney W. Grimes  */
3025c7c34a24SBruce Evans DB_SHOW_COMMAND(map, vm_map_print)
3026df8bae1dSRodney W. Grimes {
302795e5e988SJohn Dyson 	static int nlines;
3028c7c34a24SBruce Evans 	/* XXX convert args. */
3029c0877f10SJohn Dyson 	vm_map_t map = (vm_map_t)addr;
3030c7c34a24SBruce Evans 	boolean_t full = have_addr;
3031df8bae1dSRodney W. Grimes 
3032c0877f10SJohn Dyson 	vm_map_entry_t entry;
3033c7c34a24SBruce Evans 
3034e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
3035e5f251d2SAlan Cox 	    (void *)map,
3036101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
303795e5e988SJohn Dyson 	nlines++;
3038df8bae1dSRodney W. Grimes 
3039c7c34a24SBruce Evans 	if (!full && db_indent)
3040df8bae1dSRodney W. Grimes 		return;
3041df8bae1dSRodney W. Grimes 
3042c7c34a24SBruce Evans 	db_indent += 2;
3043df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
3044df8bae1dSRodney W. Grimes 	    entry = entry->next) {
3045fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
3046fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
304795e5e988SJohn Dyson 		nlines++;
3048e5f251d2SAlan Cox 		{
3049df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
3050df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
30510d94caffSDavid Greenman 
305295e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
3053df8bae1dSRodney W. Grimes 			    entry->protection,
3054df8bae1dSRodney W. Grimes 			    entry->max_protection,
30558aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
3056df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
305795e5e988SJohn Dyson 				db_printf(", wired");
3058df8bae1dSRodney W. Grimes 		}
30599fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3060cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
30619fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
3062cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
306395e5e988SJohn Dyson 			nlines++;
3064df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
30659fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
30669fdfe602SMatthew Dillon 				entry->object.sub_map)) {
3067c7c34a24SBruce Evans 				db_indent += 2;
3068101eeb7fSBruce Evans 				vm_map_print((db_expr_t)(intptr_t)
30699fdfe602SMatthew Dillon 					     entry->object.sub_map,
3070914181e7SBruce Evans 					     full, 0, (char *)0);
3071c7c34a24SBruce Evans 				db_indent -= 2;
3072df8bae1dSRodney W. Grimes 			}
30730d94caffSDavid Greenman 		} else {
3074cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
3075101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
3076cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
3077afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
3078c7c34a24SBruce Evans 				db_printf(", copy (%s)",
3079afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
3080c7c34a24SBruce Evans 			db_printf("\n");
308195e5e988SJohn Dyson 			nlines++;
3082df8bae1dSRodney W. Grimes 
3083df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
3084df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
3085df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
3086c7c34a24SBruce Evans 				db_indent += 2;
3087101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
3088101eeb7fSBruce Evans 						entry->object.vm_object,
3089914181e7SBruce Evans 						full, 0, (char *)0);
309095e5e988SJohn Dyson 				nlines += 4;
3091c7c34a24SBruce Evans 				db_indent -= 2;
3092df8bae1dSRodney W. Grimes 			}
3093df8bae1dSRodney W. Grimes 		}
3094df8bae1dSRodney W. Grimes 	}
3095c7c34a24SBruce Evans 	db_indent -= 2;
309695e5e988SJohn Dyson 	if (db_indent == 0)
309795e5e988SJohn Dyson 		nlines = 0;
3098df8bae1dSRodney W. Grimes }
309995e5e988SJohn Dyson 
310095e5e988SJohn Dyson 
310195e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
310295e5e988SJohn Dyson {
310395e5e988SJohn Dyson 	struct proc *p;
310495e5e988SJohn Dyson 
310595e5e988SJohn Dyson 	if (have_addr) {
310695e5e988SJohn Dyson 		p = (struct proc *) addr;
310795e5e988SJohn Dyson 	} else {
310895e5e988SJohn Dyson 		p = curproc;
310995e5e988SJohn Dyson 	}
311095e5e988SJohn Dyson 
3111ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
3112ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
3113b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
311495e5e988SJohn Dyson 
3115101eeb7fSBruce Evans 	vm_map_print((db_expr_t)(intptr_t)&p->p_vmspace->vm_map, 1, 0, NULL);
311695e5e988SJohn Dyson }
311795e5e988SJohn Dyson 
3118c7c34a24SBruce Evans #endif /* DDB */
3119