xref: /freebsd/sys/vm/vm_map.c (revision 763d9566fe85c422787f87631dfe2a47d3278a4a)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
709a6d144fSKonstantin Belousov #include <sys/kernel.h>
7161d80e90SJohn Baldwin #include <sys/ktr.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73fb919e4dSMark Murray #include <sys/mutex.h>
74b5e8ce9fSBruce Evans #include <sys/proc.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
771efb74fbSJohn Dyson #include <sys/vnode.h>
781ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
792267af78SJulian Elischer #include <sys/resourcevar.h>
8089f6b863SAttilio Rao #include <sys/rwlock.h>
813fde38dfSMike Silbersack #include <sys/file.h>
829a6d144fSKonstantin Belousov #include <sys/sysctl.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>
9584110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
9621cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
97670d17b5SJeff Roberson #include <vm/uma.h>
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
101df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
102df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
103df8bae1dSRodney W. Grimes  *	memory from one map to another.
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
108e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
109e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
110df8bae1dSRodney W. Grimes  *
111956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
112df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
113df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
114df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
115df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
116df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
117df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
120ad5fca3bSAlan Cox  *	by copying VM object references from one map to
121df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
122df8bae1dSRodney W. Grimes  */
123df8bae1dSRodney W. Grimes 
1243a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1258355f576SJeff Roberson static uma_zone_t mapentzone;
1268355f576SJeff Roberson static uma_zone_t kmapentzone;
1278355f576SJeff Roberson static uma_zone_t mapzone;
1288355f576SJeff Roberson static uma_zone_t vmspace_zone;
129b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1308355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
131b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1328355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
13392351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13492351f16SAlan Cox     vm_offset_t max);
1350b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
136655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
1378355f576SJeff Roberson #ifdef INVARIANTS
1388355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1398355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1408355f576SJeff Roberson #endif
141b18bfc3dSJohn Dyson 
142ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
143ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1443364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1453364c323SKonstantin Belousov 
14657051fdcSTor Egge /*
14757051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
14857051fdcSTor Egge  * stable.
14957051fdcSTor Egge  */
15057051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15157051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15257051fdcSTor Egge 
153d239bd3cSKonstantin Belousov /*
154d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
155d239bd3cSKonstantin Belousov  *
156d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
157d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
158d239bd3cSKonstantin Belousov  */
159d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
160d239bd3cSKonstantin Belousov 		{					\
161d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
162d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
163d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
164d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
165d239bd3cSKonstantin Belousov 		if (start > end)			\
166d239bd3cSKonstantin Belousov 			start = end;			\
167d239bd3cSKonstantin Belousov 		}
168d239bd3cSKonstantin Belousov 
1696fecb26bSKonstantin Belousov /*
1706fecb26bSKonstantin Belousov  *	vm_map_startup:
1716fecb26bSKonstantin Belousov  *
1726fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1736fecb26bSKonstantin Belousov  *	any other vm_map routines.
1746fecb26bSKonstantin Belousov  *
1756fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1766fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1776fecb26bSKonstantin Belousov  *
1786fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1796fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1806fecb26bSKonstantin Belousov  *
1816fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1826fecb26bSKonstantin Belousov  *	maps and requires map entries.
1836fecb26bSKonstantin Belousov  */
1846fecb26bSKonstantin Belousov 
1850d94caffSDavid Greenman void
1861b40f8c0SMatthew Dillon vm_map_startup(void)
187df8bae1dSRodney W. Grimes {
1883a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1898355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1908355f576SJeff Roberson #ifdef INVARIANTS
1918355f576SJeff Roberson 	    vm_map_zdtor,
1928355f576SJeff Roberson #else
1938355f576SJeff Roberson 	    NULL,
1948355f576SJeff Roberson #endif
1958355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1968355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
197670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
19818aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
19918aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
200670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
201670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
202670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
203df8bae1dSRodney W. Grimes }
204df8bae1dSRodney W. Grimes 
2058355f576SJeff Roberson static void
2068355f576SJeff Roberson vmspace_zfini(void *mem, int size)
2078355f576SJeff Roberson {
2088355f576SJeff Roberson 	struct vmspace *vm;
2098355f576SJeff Roberson 
2108355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2118355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
2128355f576SJeff Roberson }
2138355f576SJeff Roberson 
214b23f72e9SBrian Feldman static int
215b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2168355f576SJeff Roberson {
2178355f576SJeff Roberson 	struct vmspace *vm;
2188355f576SJeff Roberson 
2198355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2208355f576SJeff Roberson 
22189b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
222b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
223b23f72e9SBrian Feldman 	return (0);
2248355f576SJeff Roberson }
2258355f576SJeff Roberson 
2268355f576SJeff Roberson static void
2278355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2288355f576SJeff Roberson {
2298355f576SJeff Roberson 	vm_map_t map;
2308355f576SJeff Roberson 
2318355f576SJeff Roberson 	map = (vm_map_t)mem;
23236daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
23312c64974SMaxime Henrion 	sx_destroy(&map->lock);
2348355f576SJeff Roberson }
2358355f576SJeff Roberson 
236b23f72e9SBrian Feldman static int
237b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2388355f576SJeff Roberson {
2398355f576SJeff Roberson 	vm_map_t map;
2408355f576SJeff Roberson 
2418355f576SJeff Roberson 	map = (vm_map_t)mem;
242*763d9566STim Kientzle 	memset(map, 0, sizeof(*map));
243e30df26eSAlan Cox 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
244e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
245b23f72e9SBrian Feldman 	return (0);
2468355f576SJeff Roberson }
2478355f576SJeff Roberson 
2488355f576SJeff Roberson #ifdef INVARIANTS
2498355f576SJeff Roberson static void
2508355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2518355f576SJeff Roberson {
2528355f576SJeff Roberson 	struct vmspace *vm;
2538355f576SJeff Roberson 
2548355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2558355f576SJeff Roberson 
2568355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2578355f576SJeff Roberson }
2588355f576SJeff Roberson static void
2598355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2608355f576SJeff Roberson {
2618355f576SJeff Roberson 	vm_map_t map;
2628355f576SJeff Roberson 
2638355f576SJeff Roberson 	map = (vm_map_t)mem;
2648355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2658355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2668355f576SJeff Roberson 	    map, map->nentries));
2678355f576SJeff Roberson 	KASSERT(map->size == 0,
2688355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2699eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2708355f576SJeff Roberson }
2718355f576SJeff Roberson #endif	/* INVARIANTS */
2728355f576SJeff Roberson 
273df8bae1dSRodney W. Grimes /*
274df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
275df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
276df8bae1dSRodney W. Grimes  */
277df8bae1dSRodney W. Grimes struct vmspace *
2782d8acc0fSJohn Dyson vmspace_alloc(min, max)
279df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
280df8bae1dSRodney W. Grimes {
281c0877f10SJohn Dyson 	struct vmspace *vm;
2820d94caffSDavid Greenman 
283a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
28489b57fcfSKonstantin Belousov 	if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
28589b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
28689b57fcfSKonstantin Belousov 		return (NULL);
28789b57fcfSKonstantin Belousov 	}
28821c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
28992351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
290df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2912d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
29251ab6c28SAlan Cox 	vm->vm_swrss = 0;
29351ab6c28SAlan Cox 	vm->vm_tsize = 0;
29451ab6c28SAlan Cox 	vm->vm_dsize = 0;
29551ab6c28SAlan Cox 	vm->vm_ssize = 0;
29651ab6c28SAlan Cox 	vm->vm_taddr = 0;
29751ab6c28SAlan Cox 	vm->vm_daddr = 0;
29851ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
299df8bae1dSRodney W. Grimes 	return (vm);
300df8bae1dSRodney W. Grimes }
301df8bae1dSRodney W. Grimes 
302df8bae1dSRodney W. Grimes void
3031b40f8c0SMatthew Dillon vm_init2(void)
3041b40f8c0SMatthew Dillon {
305a4915c21SAttilio Rao 	uma_zone_reserve_kva(kmapentzone, lmin(cnt.v_page_count,
306c1f02198SAlan Cox 	    (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
3073fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
3088355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
3098355f576SJeff Roberson #ifdef INVARIANTS
3108355f576SJeff Roberson 	    vmspace_zdtor,
3118355f576SJeff Roberson #else
3128355f576SJeff Roberson 	    NULL,
3138355f576SJeff Roberson #endif
3148355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3153075778bSJohn Dyson }
3163075778bSJohn Dyson 
3171ba5ad42SEdward Tomasz Napierala static void
3181ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3191ba5ad42SEdward Tomasz Napierala {
3201ba5ad42SEdward Tomasz Napierala 
321afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3221ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3231ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3241ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3251ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3261ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3271ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3281ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
329afcc55f3SEdward Tomasz Napierala #endif
3301ba5ad42SEdward Tomasz Napierala }
3311ba5ad42SEdward Tomasz Napierala 
33262a59e8fSWarner Losh static inline void
333582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
334df8bae1dSRodney W. Grimes {
3350ef12795SAlan Cox 
33621c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3373db161e0SMatthew Dillon 
3383db161e0SMatthew Dillon 	/*
3393db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3403db161e0SMatthew Dillon 	 * exit1().
3413db161e0SMatthew Dillon 	 */
3423db161e0SMatthew Dillon 	shmexit(vm);
3433db161e0SMatthew Dillon 
34430dcfc09SJohn Dyson 	/*
345df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3460d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3470d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
348df8bae1dSRodney W. Grimes 	 */
349717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
350df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3518355f576SJeff Roberson 
3520ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3530ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3548355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
355df8bae1dSRodney W. Grimes }
356582ec34cSAlfred Perlstein 
357582ec34cSAlfred Perlstein void
358582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
359582ec34cSAlfred Perlstein {
360582ec34cSAlfred Perlstein 
361582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
362582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
363582ec34cSAlfred Perlstein 
3641a587ef2SJohn Baldwin 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
365582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
366582ec34cSAlfred Perlstein }
367582ec34cSAlfred Perlstein 
368582ec34cSAlfred Perlstein void
369582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
370582ec34cSAlfred Perlstein {
371334f7061SPeter Wemm 	struct vmspace *vm;
372582ec34cSAlfred Perlstein 
37357051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
374334f7061SPeter Wemm 	vm = p->p_vmspace;
375334f7061SPeter Wemm 	p->p_vmspace = NULL;
37657051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37757051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
37857051fdcSTor Egge 	vmspace_free(vm);
37957051fdcSTor Egge }
38057051fdcSTor Egge 
38157051fdcSTor Egge void
38257051fdcSTor Egge vmspace_exit(struct thread *td)
38357051fdcSTor Egge {
38457051fdcSTor Egge 	int refcnt;
38557051fdcSTor Egge 	struct vmspace *vm;
38657051fdcSTor Egge 	struct proc *p;
387389d2b6eSMatthew Dillon 
388389d2b6eSMatthew Dillon 	/*
38957051fdcSTor Egge 	 * Release user portion of address space.
39057051fdcSTor Egge 	 * This releases references to vnodes,
39157051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
39257051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
393389d2b6eSMatthew Dillon 	 *
39457051fdcSTor Egge 	 * The last exiting process to reach this point releases as
39557051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
39657051fdcSTor Egge 	 * slower fallback in case another process had a temporary
39757051fdcSTor Egge 	 * reference to the vmspace.
398389d2b6eSMatthew Dillon 	 */
39957051fdcSTor Egge 
40057051fdcSTor Egge 	p = td->td_proc;
40157051fdcSTor Egge 	vm = p->p_vmspace;
40257051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
40357051fdcSTor Egge 	do {
40457051fdcSTor Egge 		refcnt = vm->vm_refcnt;
40557051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
40657051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
40757051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40857051fdcSTor Egge 			p->p_vmspace = &vmspace0;
40957051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
41057051fdcSTor Egge 			pmap_activate(td);
41157051fdcSTor Egge 		}
41257051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
41357051fdcSTor Egge 	if (refcnt == 1) {
41457051fdcSTor Egge 		if (p->p_vmspace != vm) {
41557051fdcSTor Egge 			/* vmspace not yet freed, switch back */
41657051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
41757051fdcSTor Egge 			p->p_vmspace = vm;
41857051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
41957051fdcSTor Egge 			pmap_activate(td);
42057051fdcSTor Egge 		}
42157051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
42257051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
42357051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
42457051fdcSTor Egge 		p->p_vmspace = &vmspace0;
42557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42657051fdcSTor Egge 		pmap_activate(td);
427334f7061SPeter Wemm 		vmspace_dofree(vm);
428334f7061SPeter Wemm 	}
4291ba5ad42SEdward Tomasz Napierala 	vmspace_container_reset(p);
43057051fdcSTor Egge }
43157051fdcSTor Egge 
43257051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
43357051fdcSTor Egge 
43457051fdcSTor Egge struct vmspace *
43557051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
43657051fdcSTor Egge {
43757051fdcSTor Egge 	struct vmspace *vm;
43857051fdcSTor Egge 	int refcnt;
43957051fdcSTor Egge 
44057051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
44157051fdcSTor Egge 	vm = p->p_vmspace;
44257051fdcSTor Egge 	if (vm == NULL) {
44357051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
44457051fdcSTor Egge 		return (NULL);
44557051fdcSTor Egge 	}
44657051fdcSTor Egge 	do {
44757051fdcSTor Egge 		refcnt = vm->vm_refcnt;
44857051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
44957051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
45057051fdcSTor Egge 			return (NULL);
45157051fdcSTor Egge 		}
45257051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
45357051fdcSTor Egge 	if (vm != p->p_vmspace) {
45457051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
45557051fdcSTor Egge 		vmspace_free(vm);
45657051fdcSTor Egge 		return (NULL);
45757051fdcSTor Egge 	}
45857051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
45957051fdcSTor Egge 	return (vm);
46057051fdcSTor Egge }
461df8bae1dSRodney W. Grimes 
4621b40f8c0SMatthew Dillon void
463780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4641b40f8c0SMatthew Dillon {
465bc91c510SAlan Cox 
46693bc4879SAlan Cox 	if (map->system_map)
467ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
46812c64974SMaxime Henrion 	else
4699fde98bbSAttilio Rao 		sx_xlock_(&map->lock, file, line);
4701b40f8c0SMatthew Dillon 	map->timestamp++;
4711b40f8c0SMatthew Dillon }
4721b40f8c0SMatthew Dillon 
4730b367bd8SKonstantin Belousov static void
4740b367bd8SKonstantin Belousov vm_map_process_deferred(void)
4750e0af8ecSBrian Feldman {
4760b367bd8SKonstantin Belousov 	struct thread *td;
4776fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
47884110e7eSKonstantin Belousov 	vm_object_t object;
479655c3490SKonstantin Belousov 
4800b367bd8SKonstantin Belousov 	td = curthread;
4816fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
4826fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
4836fbe60faSJohn Baldwin 	while (entry != NULL) {
4846fbe60faSJohn Baldwin 		next = entry->next;
48584110e7eSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
48684110e7eSKonstantin Belousov 			/*
48784110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
48884110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
48984110e7eSKonstantin Belousov 			 */
49084110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
49184110e7eSKonstantin Belousov 			    ("Submap with writecount"));
49284110e7eSKonstantin Belousov 			object = entry->object.vm_object;
49384110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
49484110e7eSKonstantin Belousov 			vnode_pager_release_writecount(object, entry->start,
49584110e7eSKonstantin Belousov 			    entry->end);
49684110e7eSKonstantin Belousov 		}
4970b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
4986fbe60faSJohn Baldwin 		entry = next;
4990b367bd8SKonstantin Belousov 	}
5000b367bd8SKonstantin Belousov }
5010b367bd8SKonstantin Belousov 
5020b367bd8SKonstantin Belousov void
5030b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
5040b367bd8SKonstantin Belousov {
5050b367bd8SKonstantin Belousov 
5060b367bd8SKonstantin Belousov 	if (map->system_map)
507ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5080b367bd8SKonstantin Belousov 	else {
5099fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
5100b367bd8SKonstantin Belousov 		vm_map_process_deferred();
511655c3490SKonstantin Belousov 	}
5120e0af8ecSBrian Feldman }
5130e0af8ecSBrian Feldman 
5140e0af8ecSBrian Feldman void
515780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
5160e0af8ecSBrian Feldman {
517bc91c510SAlan Cox 
51893bc4879SAlan Cox 	if (map->system_map)
519ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
52012c64974SMaxime Henrion 	else
5219fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
52236daaecdSAlan Cox }
5230e0af8ecSBrian Feldman 
5240e0af8ecSBrian Feldman void
525780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
5260e0af8ecSBrian Feldman {
527bc91c510SAlan Cox 
52836daaecdSAlan Cox 	if (map->system_map)
529ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5300b367bd8SKonstantin Belousov 	else {
5319fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
5320b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5330b367bd8SKonstantin Belousov 	}
53425adb370SBrian Feldman }
53525adb370SBrian Feldman 
536d974f03cSAlan Cox int
537780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
538d974f03cSAlan Cox {
53925adb370SBrian Feldman 	int error;
54025adb370SBrian Feldman 
54136daaecdSAlan Cox 	error = map->system_map ?
542ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5439fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
5443a92e5d5SAlan Cox 	if (error == 0)
5453a92e5d5SAlan Cox 		map->timestamp++;
546bc91c510SAlan Cox 	return (error == 0);
5470e0af8ecSBrian Feldman }
5480e0af8ecSBrian Feldman 
5490e0af8ecSBrian Feldman int
55072d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
55172d97679SDavid Schultz {
55272d97679SDavid Schultz 	int error;
55372d97679SDavid Schultz 
55472d97679SDavid Schultz 	error = map->system_map ?
555ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5569fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
55772d97679SDavid Schultz 	return (error == 0);
55872d97679SDavid Schultz }
55972d97679SDavid Schultz 
56005a8c414SAlan Cox /*
56105a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
56205a8c414SAlan Cox  *
56305a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
56405a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
56505a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
56605a8c414SAlan Cox  *	returned without a read or write lock held.
56705a8c414SAlan Cox  *
56805a8c414SAlan Cox  *	Requires that the map be read locked.
56905a8c414SAlan Cox  */
57072d97679SDavid Schultz int
571780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5720e0af8ecSBrian Feldman {
57305a8c414SAlan Cox 	unsigned int last_timestamp;
574bc91c510SAlan Cox 
57512c64974SMaxime Henrion 	if (map->system_map) {
576ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
57705a8c414SAlan Cox 	} else {
5789fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
57905a8c414SAlan Cox 			last_timestamp = map->timestamp;
5809fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
5810b367bd8SKonstantin Belousov 			vm_map_process_deferred();
58205a8c414SAlan Cox 			/*
58305a8c414SAlan Cox 			 * If the map's timestamp does not change while the
58405a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
58505a8c414SAlan Cox 			 */
5869fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
58705a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
5889fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
58905a8c414SAlan Cox 				return (1);
59005a8c414SAlan Cox 			}
59105a8c414SAlan Cox 		}
59205a8c414SAlan Cox 	}
593bc91c510SAlan Cox 	map->timestamp++;
594bc91c510SAlan Cox 	return (0);
5950e0af8ecSBrian Feldman }
5960e0af8ecSBrian Feldman 
5970e0af8ecSBrian Feldman void
598780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5991b40f8c0SMatthew Dillon {
600bc91c510SAlan Cox 
60112c64974SMaxime Henrion 	if (map->system_map) {
602ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
60305a8c414SAlan Cox 	} else
6049fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
60505a8c414SAlan Cox }
60605a8c414SAlan Cox 
60705a8c414SAlan Cox /*
60805a8c414SAlan Cox  *	vm_map_locked:
60905a8c414SAlan Cox  *
61005a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
61105a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
61205a8c414SAlan Cox  */
61305a8c414SAlan Cox int
61405a8c414SAlan Cox vm_map_locked(vm_map_t map)
61505a8c414SAlan Cox {
61605a8c414SAlan Cox 
61705a8c414SAlan Cox 	if (map->system_map)
61805a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
61905a8c414SAlan Cox 	else
62005a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
62125adb370SBrian Feldman }
62225adb370SBrian Feldman 
6233a0916b8SKonstantin Belousov #ifdef INVARIANTS
6243a0916b8SKonstantin Belousov static void
6253a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6263a0916b8SKonstantin Belousov {
6273a0916b8SKonstantin Belousov 
6283a0916b8SKonstantin Belousov 	if (map->system_map)
629ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
6303a0916b8SKonstantin Belousov 	else
6319fde98bbSAttilio Rao 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
6323a0916b8SKonstantin Belousov }
6333a0916b8SKonstantin Belousov 
6343a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6353a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6363a0916b8SKonstantin Belousov #else
6373a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6383a0916b8SKonstantin Belousov #endif
6393a0916b8SKonstantin Belousov 
640acd9a301SAlan Cox /*
6418304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6428304adaaSAlan Cox  *
6438304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6448304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6458304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6468304adaaSAlan Cox  *	exceeded.
6478304adaaSAlan Cox  *
6488304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6498304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6508304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6518304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6528304adaaSAlan Cox  *	operation on the map.
653acd9a301SAlan Cox  */
6549688f931SAlan Cox int
6558304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
656acd9a301SAlan Cox {
657acd9a301SAlan Cox 
6583a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6598304adaaSAlan Cox 	if (map->system_map)
660ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
6618304adaaSAlan Cox 	else
6629fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
6638304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
6648304adaaSAlan Cox 	    timo));
665acd9a301SAlan Cox }
666acd9a301SAlan Cox 
667acd9a301SAlan Cox /*
668acd9a301SAlan Cox  *	vm_map_wakeup:
6698304adaaSAlan Cox  *
6708304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
6718304adaaSAlan Cox  *	vm_map_unlock_and_wait().
672acd9a301SAlan Cox  */
6739688f931SAlan Cox void
674acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
675acd9a301SAlan Cox {
676acd9a301SAlan Cox 
677b49ecb86SAlan Cox 	/*
6783a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6798304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
6808304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
681b49ecb86SAlan Cox 	 */
6823a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6833a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
684acd9a301SAlan Cox 	wakeup(&map->root);
685acd9a301SAlan Cox }
686acd9a301SAlan Cox 
687a5db445dSMax Laier void
688a5db445dSMax Laier vm_map_busy(vm_map_t map)
689a5db445dSMax Laier {
690a5db445dSMax Laier 
691a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
692a5db445dSMax Laier 	map->busy++;
693a5db445dSMax Laier }
694a5db445dSMax Laier 
695a5db445dSMax Laier void
696a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
697a5db445dSMax Laier {
698a5db445dSMax Laier 
699a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
700a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
701a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
702a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
703a5db445dSMax Laier 		wakeup(&map->busy);
704a5db445dSMax Laier 	}
705a5db445dSMax Laier }
706a5db445dSMax Laier 
707a5db445dSMax Laier void
708a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
709a5db445dSMax Laier {
710a5db445dSMax Laier 
711a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
712a5db445dSMax Laier 	while (map->busy) {
713a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
714a5db445dSMax Laier 		if (map->system_map)
715a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
716a5db445dSMax Laier 		else
717a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
718a5db445dSMax Laier 	}
719a5db445dSMax Laier 	map->timestamp++;
720a5db445dSMax Laier }
721a5db445dSMax Laier 
7221b40f8c0SMatthew Dillon long
7231b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
7241b40f8c0SMatthew Dillon {
7251b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
7261b40f8c0SMatthew Dillon }
7271b40f8c0SMatthew Dillon 
728ff2b5645SMatthew Dillon /*
729df8bae1dSRodney W. Grimes  *	vm_map_create:
730df8bae1dSRodney W. Grimes  *
731df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
732df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
733df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
734df8bae1dSRodney W. Grimes  */
7350d94caffSDavid Greenman vm_map_t
7361b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
737df8bae1dSRodney W. Grimes {
738c0877f10SJohn Dyson 	vm_map_t result;
739df8bae1dSRodney W. Grimes 
740a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
74121c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
74292351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
743df8bae1dSRodney W. Grimes 	return (result);
744df8bae1dSRodney W. Grimes }
745df8bae1dSRodney W. Grimes 
746df8bae1dSRodney W. Grimes /*
747df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
748df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
749df8bae1dSRodney W. Grimes  */
7508355f576SJeff Roberson static void
75192351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
752df8bae1dSRodney W. Grimes {
75321c641b2SJohn Baldwin 
754df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7559688f931SAlan Cox 	map->needs_wakeup = FALSE;
7563075778bSJohn Dyson 	map->system_map = 0;
75792351f16SAlan Cox 	map->pmap = pmap;
758df8bae1dSRodney W. Grimes 	map->min_offset = min;
759df8bae1dSRodney W. Grimes 	map->max_offset = max;
760af7cd0c5SBrian Feldman 	map->flags = 0;
7614e94f402SAlan Cox 	map->root = NULL;
762df8bae1dSRodney W. Grimes 	map->timestamp = 0;
763a5db445dSMax Laier 	map->busy = 0;
764df8bae1dSRodney W. Grimes }
765df8bae1dSRodney W. Grimes 
766a18b1f1dSJason Evans void
76792351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
768a18b1f1dSJason Evans {
76992351f16SAlan Cox 
77092351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
771d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
77212c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
773a18b1f1dSJason Evans }
774a18b1f1dSJason Evans 
775df8bae1dSRodney W. Grimes /*
776b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
777b18bfc3dSJohn Dyson  *
778b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
779b18bfc3dSJohn Dyson  */
78062487bb4SJohn Dyson static void
7811b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
782b18bfc3dSJohn Dyson {
7832b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
784b18bfc3dSJohn Dyson }
785b18bfc3dSJohn Dyson 
786b18bfc3dSJohn Dyson /*
787df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
788df8bae1dSRodney W. Grimes  *
789df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
790b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
791df8bae1dSRodney W. Grimes  */
792f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7931b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
794df8bae1dSRodney W. Grimes {
7951f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7961f6889a1SMatthew Dillon 
7972b4a2c27SAlan Cox 	if (map->system_map)
7982b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
7992b4a2c27SAlan Cox 	else
800a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
8011f6889a1SMatthew Dillon 	if (new_entry == NULL)
8021f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
8031f6889a1SMatthew Dillon 	return (new_entry);
804df8bae1dSRodney W. Grimes }
805df8bae1dSRodney W. Grimes 
806df8bae1dSRodney W. Grimes /*
807794316a8SAlan Cox  *	vm_map_entry_set_behavior:
808794316a8SAlan Cox  *
809794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
810794316a8SAlan Cox  *	sequential.
811794316a8SAlan Cox  */
81262a59e8fSWarner Losh static inline void
813794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
814794316a8SAlan Cox {
815794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
816794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
817794316a8SAlan Cox }
818794316a8SAlan Cox 
819794316a8SAlan Cox /*
8200164e057SAlan Cox  *	vm_map_entry_set_max_free:
8210164e057SAlan Cox  *
8220164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
8230164e057SAlan Cox  */
82462a59e8fSWarner Losh static inline void
8250164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
8260164e057SAlan Cox {
8270164e057SAlan Cox 
8280164e057SAlan Cox 	entry->max_free = entry->adj_free;
8290164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
8300164e057SAlan Cox 		entry->max_free = entry->left->max_free;
8310164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
8320164e057SAlan Cox 		entry->max_free = entry->right->max_free;
8330164e057SAlan Cox }
8340164e057SAlan Cox 
8350164e057SAlan Cox /*
8364e94f402SAlan Cox  *	vm_map_entry_splay:
8374e94f402SAlan Cox  *
8380164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
8390164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
8400164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8410164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8420164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8430164e057SAlan Cox  *	amortized.
8440164e057SAlan Cox  *
8450164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8460164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8470164e057SAlan Cox  *
8480164e057SAlan Cox  *	The map must be locked, and leaves it so.
8490164e057SAlan Cox  *
8500164e057SAlan Cox  *	Returns: the new root.
8514e94f402SAlan Cox  */
8524e94f402SAlan Cox static vm_map_entry_t
8530164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8544e94f402SAlan Cox {
8550164e057SAlan Cox 	vm_map_entry_t llist, rlist;
8560164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
8570164e057SAlan Cox 	vm_map_entry_t y;
8584e94f402SAlan Cox 
8590164e057SAlan Cox 	/* Special case of empty tree. */
8604e94f402SAlan Cox 	if (root == NULL)
8614e94f402SAlan Cox 		return (root);
8620164e057SAlan Cox 
8630164e057SAlan Cox 	/*
8640164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8650164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8660164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8670164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8680164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8690164e057SAlan Cox 	 * the two spines.
8700164e057SAlan Cox 	 */
8710164e057SAlan Cox 	llist = NULL;
8720164e057SAlan Cox 	rlist = NULL;
8730164e057SAlan Cox 	for (;;) {
8740164e057SAlan Cox 		/* root is never NULL in here. */
8750164e057SAlan Cox 		if (addr < root->start) {
8760164e057SAlan Cox 			y = root->left;
8770164e057SAlan Cox 			if (y == NULL)
8784e94f402SAlan Cox 				break;
8790164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8800164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8814e94f402SAlan Cox 				root->left = y->right;
8824e94f402SAlan Cox 				y->right = root;
8830164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8840164e057SAlan Cox 				root = y->left;
8850164e057SAlan Cox 				y->left = rlist;
8860164e057SAlan Cox 				rlist = y;
8870164e057SAlan Cox 			} else {
8880164e057SAlan Cox 				/* Put root on rlist. */
8890164e057SAlan Cox 				root->left = rlist;
8900164e057SAlan Cox 				rlist = root;
8914e94f402SAlan Cox 				root = y;
8924e94f402SAlan Cox 			}
8937438d60bSAlan Cox 		} else if (addr >= root->end) {
8940164e057SAlan Cox 			y = root->right;
8957438d60bSAlan Cox 			if (y == NULL)
8964e94f402SAlan Cox 				break;
8970164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8980164e057SAlan Cox 				/* Rotate left and put y on llist. */
8994e94f402SAlan Cox 				root->right = y->left;
9004e94f402SAlan Cox 				y->left = root;
9010164e057SAlan Cox 				vm_map_entry_set_max_free(root);
9020164e057SAlan Cox 				root = y->right;
9030164e057SAlan Cox 				y->right = llist;
9040164e057SAlan Cox 				llist = y;
9050164e057SAlan Cox 			} else {
9060164e057SAlan Cox 				/* Put root on llist. */
9070164e057SAlan Cox 				root->right = llist;
9080164e057SAlan Cox 				llist = root;
9094e94f402SAlan Cox 				root = y;
9104e94f402SAlan Cox 			}
9117438d60bSAlan Cox 		} else
9127438d60bSAlan Cox 			break;
9130164e057SAlan Cox 	}
9140164e057SAlan Cox 
9150164e057SAlan Cox 	/*
9160164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
9170164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
9180164e057SAlan Cox 	 * bottom of llist and rlist.
9190164e057SAlan Cox 	 */
9200164e057SAlan Cox 	ltree = root->left;
9210164e057SAlan Cox 	while (llist != NULL) {
9220164e057SAlan Cox 		y = llist->right;
9230164e057SAlan Cox 		llist->right = ltree;
9240164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
9250164e057SAlan Cox 		ltree = llist;
9260164e057SAlan Cox 		llist = y;
9270164e057SAlan Cox 	}
9280164e057SAlan Cox 	rtree = root->right;
9290164e057SAlan Cox 	while (rlist != NULL) {
9300164e057SAlan Cox 		y = rlist->left;
9310164e057SAlan Cox 		rlist->left = rtree;
9320164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
9330164e057SAlan Cox 		rtree = rlist;
9340164e057SAlan Cox 		rlist = y;
9350164e057SAlan Cox 	}
9360164e057SAlan Cox 
9370164e057SAlan Cox 	/*
9380164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
9390164e057SAlan Cox 	 */
9400164e057SAlan Cox 	root->left = ltree;
9410164e057SAlan Cox 	root->right = rtree;
9420164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9430164e057SAlan Cox 
9444e94f402SAlan Cox 	return (root);
9454e94f402SAlan Cox }
9464e94f402SAlan Cox 
9474e94f402SAlan Cox /*
948df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
949df8bae1dSRodney W. Grimes  *
950df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
951df8bae1dSRodney W. Grimes  */
9524e94f402SAlan Cox static void
95399c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
95499c81ca9SAlan Cox 		  vm_map_entry_t after_where,
95599c81ca9SAlan Cox 		  vm_map_entry_t entry)
95699c81ca9SAlan Cox {
95721c641b2SJohn Baldwin 
95821c641b2SJohn Baldwin 	CTR4(KTR_VM,
95921c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
96021c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9613a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
96299c81ca9SAlan Cox 	map->nentries++;
96399c81ca9SAlan Cox 	entry->prev = after_where;
96499c81ca9SAlan Cox 	entry->next = after_where->next;
96599c81ca9SAlan Cox 	entry->next->prev = entry;
96699c81ca9SAlan Cox 	after_where->next = entry;
9674e94f402SAlan Cox 
9684e94f402SAlan Cox 	if (after_where != &map->header) {
9694e94f402SAlan Cox 		if (after_where != map->root)
9704e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
9714e94f402SAlan Cox 		entry->right = after_where->right;
9724e94f402SAlan Cox 		entry->left = after_where;
9734e94f402SAlan Cox 		after_where->right = NULL;
9740164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
9750164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
9764e94f402SAlan Cox 	} else {
9774e94f402SAlan Cox 		entry->right = map->root;
9784e94f402SAlan Cox 		entry->left = NULL;
9794e94f402SAlan Cox 	}
9800164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9810164e057SAlan Cox 	    entry->next->start) - entry->end;
9820164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9834e94f402SAlan Cox 	map->root = entry;
984df8bae1dSRodney W. Grimes }
98599c81ca9SAlan Cox 
9864e94f402SAlan Cox static void
98799c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
98899c81ca9SAlan Cox 		    vm_map_entry_t entry)
98999c81ca9SAlan Cox {
9904e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
99199c81ca9SAlan Cox 
9923a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9934e94f402SAlan Cox 	if (entry != map->root)
9944e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
9954e94f402SAlan Cox 	if (entry->left == NULL)
9964e94f402SAlan Cox 		root = entry->right;
9974e94f402SAlan Cox 	else {
9984e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
9994e94f402SAlan Cox 		root->right = entry->right;
10000164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
10010164e057SAlan Cox 		    entry->next->start) - root->end;
10020164e057SAlan Cox 		vm_map_entry_set_max_free(root);
10034e94f402SAlan Cox 	}
10044e94f402SAlan Cox 	map->root = root;
10054e94f402SAlan Cox 
10064e94f402SAlan Cox 	prev = entry->prev;
10074e94f402SAlan Cox 	next = entry->next;
100899c81ca9SAlan Cox 	next->prev = prev;
100999c81ca9SAlan Cox 	prev->next = next;
101099c81ca9SAlan Cox 	map->nentries--;
101121c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
101221c641b2SJohn Baldwin 	    map->nentries, entry);
1013df8bae1dSRodney W. Grimes }
1014df8bae1dSRodney W. Grimes 
1015df8bae1dSRodney W. Grimes /*
10160164e057SAlan Cox  *	vm_map_entry_resize_free:
10170164e057SAlan Cox  *
10180164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
10190164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
10200164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
10210164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
10220164e057SAlan Cox  *
10230164e057SAlan Cox  *	The map must be locked, and leaves it so.
10240164e057SAlan Cox  */
10250164e057SAlan Cox static void
10260164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
10270164e057SAlan Cox {
10280164e057SAlan Cox 
10290164e057SAlan Cox 	/*
10300164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
10310164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
10320164e057SAlan Cox 	 * root and making the change there.
10330164e057SAlan Cox 	 */
10340164e057SAlan Cox 	if (entry != map->root)
10350164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
10360164e057SAlan Cox 
10370164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
10380164e057SAlan Cox 	    entry->next->start) - entry->end;
10390164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
10400164e057SAlan Cox }
10410164e057SAlan Cox 
10420164e057SAlan Cox /*
1043df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
1044df8bae1dSRodney W. Grimes  *
1045df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
1046df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
1047df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
1048df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
1049df8bae1dSRodney W. Grimes  *	result indicates whether the address is
1050df8bae1dSRodney W. Grimes  *	actually contained in the map.
1051df8bae1dSRodney W. Grimes  */
10520d94caffSDavid Greenman boolean_t
10531b40f8c0SMatthew Dillon vm_map_lookup_entry(
10541b40f8c0SMatthew Dillon 	vm_map_t map,
10551b40f8c0SMatthew Dillon 	vm_offset_t address,
10561b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
1057df8bae1dSRodney W. Grimes {
1058c0877f10SJohn Dyson 	vm_map_entry_t cur;
105905a8c414SAlan Cox 	boolean_t locked;
1060df8bae1dSRodney W. Grimes 
10614c3ef59eSAlan Cox 	/*
10624c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
10634c3ef59eSAlan Cox 	 * "address" is the map's header.
10644c3ef59eSAlan Cox 	 */
10654c3ef59eSAlan Cox 	cur = map->root;
10664e94f402SAlan Cox 	if (cur == NULL)
10674e94f402SAlan Cox 		*entry = &map->header;
10684c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
10694c3ef59eSAlan Cox 		*entry = cur;
10704c3ef59eSAlan Cox 		return (TRUE);
107105a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
107205a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
107305a8c414SAlan Cox 		/*
107405a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
107505a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
107605a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
107705a8c414SAlan Cox 		 * on a temporary upgrade.
107805a8c414SAlan Cox 		 */
10794c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
108005a8c414SAlan Cox 		if (!locked)
108105a8c414SAlan Cox 			sx_downgrade(&map->lock);
1082df8bae1dSRodney W. Grimes 
10834c3ef59eSAlan Cox 		/*
10844c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
10854c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
10864c3ef59eSAlan Cox 		 * immediately before or after "address".
10874c3ef59eSAlan Cox 		 */
1088df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1089df8bae1dSRodney W. Grimes 			*entry = cur;
10904e94f402SAlan Cox 			if (cur->end > address)
1091df8bae1dSRodney W. Grimes 				return (TRUE);
10924e94f402SAlan Cox 		} else
1093df8bae1dSRodney W. Grimes 			*entry = cur->prev;
109405a8c414SAlan Cox 	} else
109505a8c414SAlan Cox 		/*
109605a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
109705a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
109805a8c414SAlan Cox 		 */
109905a8c414SAlan Cox 		for (;;) {
110005a8c414SAlan Cox 			if (address < cur->start) {
110105a8c414SAlan Cox 				if (cur->left == NULL) {
110205a8c414SAlan Cox 					*entry = cur->prev;
110305a8c414SAlan Cox 					break;
110405a8c414SAlan Cox 				}
110505a8c414SAlan Cox 				cur = cur->left;
110605a8c414SAlan Cox 			} else if (cur->end > address) {
110705a8c414SAlan Cox 				*entry = cur;
110805a8c414SAlan Cox 				return (TRUE);
110905a8c414SAlan Cox 			} else {
111005a8c414SAlan Cox 				if (cur->right == NULL) {
111105a8c414SAlan Cox 					*entry = cur;
111205a8c414SAlan Cox 					break;
111305a8c414SAlan Cox 				}
111405a8c414SAlan Cox 				cur = cur->right;
111505a8c414SAlan Cox 			}
11164e94f402SAlan Cox 		}
1117df8bae1dSRodney W. Grimes 	return (FALSE);
1118df8bae1dSRodney W. Grimes }
1119df8bae1dSRodney W. Grimes 
1120df8bae1dSRodney W. Grimes /*
112130dcfc09SJohn Dyson  *	vm_map_insert:
112230dcfc09SJohn Dyson  *
112330dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
112430dcfc09SJohn Dyson  *	map at the specified address range.  The object's
112530dcfc09SJohn Dyson  *	size should match that of the address range.
112630dcfc09SJohn Dyson  *
112730dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
11282aaeadf8SMatthew Dillon  *
11292aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11302aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
113130dcfc09SJohn Dyson  */
113230dcfc09SJohn Dyson int
1133b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1134b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1135b9dcd593SBruce Evans 	      int cow)
113630dcfc09SJohn Dyson {
1137c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1138c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
113930dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
11409730a5daSPaul Saab 	vm_eflags_t protoeflags;
1141ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
11428211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
11433364c323SKonstantin Belousov 	boolean_t charge_prev_obj;
114430dcfc09SJohn Dyson 
11453a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
11463a0916b8SKonstantin Belousov 
114730dcfc09SJohn Dyson 	/*
114830dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
114930dcfc09SJohn Dyson 	 */
115030dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
115130dcfc09SJohn Dyson 	    (start >= end))
115230dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
115330dcfc09SJohn Dyson 
115430dcfc09SJohn Dyson 	/*
115530dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
115630dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
115730dcfc09SJohn Dyson 	 */
115830dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
115930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
116030dcfc09SJohn Dyson 
116130dcfc09SJohn Dyson 	prev_entry = temp_entry;
116230dcfc09SJohn Dyson 
116330dcfc09SJohn Dyson 	/*
116430dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
116530dcfc09SJohn Dyson 	 */
116630dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
116730dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
116830dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
116930dcfc09SJohn Dyson 
1170afa07f7eSJohn Dyson 	protoeflags = 0;
11713364c323SKonstantin Belousov 	charge_prev_obj = FALSE;
1172afa07f7eSJohn Dyson 
1173afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1174e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1175afa07f7eSJohn Dyson 
11764e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1177afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1178afa07f7eSJohn Dyson 
11794e045f93SAlan Cox 		KASSERT(object == NULL,
11804e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
11814e045f93SAlan Cox 	}
11824f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11834f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11849730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11859730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
118684110e7eSKonstantin Belousov 	if (cow & MAP_VN_WRITECOUNT)
118784110e7eSKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
11888211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
11898211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
11908211bd45SKonstantin Belousov 	else
11918211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
11924f79d873SMatthew Dillon 
1193ef694c1aSEdward Tomasz Napierala 	cred = NULL;
11943364c323SKonstantin Belousov 	KASSERT((object != kmem_object && object != kernel_object) ||
11953364c323SKonstantin Belousov 	    ((object == kmem_object || object == kernel_object) &&
11963364c323SKonstantin Belousov 		!(protoeflags & MAP_ENTRY_NEEDS_COPY)),
11973364c323SKonstantin Belousov 	    ("kmem or kernel object and cow"));
11983364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
11993364c323SKonstantin Belousov 		goto charged;
12003364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
12013364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
12023364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
12033364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
120441c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1205ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
12063364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
1207ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
1208ef694c1aSEdward Tomasz Napierala 		crhold(cred);
12093364c323SKonstantin Belousov 		if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
12103364c323SKonstantin Belousov 			charge_prev_obj = TRUE;
12113364c323SKonstantin Belousov 	}
12123364c323SKonstantin Belousov 
12133364c323SKonstantin Belousov charged:
1214f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1215f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1216f8616ebfSAlan Cox 		pmap_growkernel(end);
12171d284e00SAlan Cox 	if (object != NULL) {
121830dcfc09SJohn Dyson 		/*
12191d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
12201d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
12211d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
12221d284e00SAlan Cox 		 * reference counting is insufficient to recognize
12231d284e00SAlan Cox 		 * aliases with precision.)
122430dcfc09SJohn Dyson 		 */
122589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
12261d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
12272aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
122889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
12294e045f93SAlan Cox 	}
12304e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
12314e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
12328cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
12334e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
1234ef694c1aSEdward Tomasz Napierala 		 (prev_entry->cred == cred ||
12353364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
1236ef694c1aSEdward Tomasz Napierala 		   (prev_entry->object.vm_object->cred == cred))) &&
12378cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
123857a21abaSAlan Cox 		       prev_entry->offset,
12398cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
12403364c323SKonstantin Belousov 		       (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
124130dcfc09SJohn Dyson 		/*
12422aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
12432aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
12442aaeadf8SMatthew Dillon 		 * new range as well.
124530dcfc09SJohn Dyson 		 */
12468211bd45SKonstantin Belousov 		if ((prev_entry->inheritance == inheritance) &&
12478cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
12488cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
124930dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
125030dcfc09SJohn Dyson 			prev_entry->end = end;
12510164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
12524e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
1253ef694c1aSEdward Tomasz Napierala 			if (cred != NULL)
1254ef694c1aSEdward Tomasz Napierala 				crfree(cred);
125530dcfc09SJohn Dyson 			return (KERN_SUCCESS);
125630dcfc09SJohn Dyson 		}
12578cc7e047SJohn Dyson 
12582aaeadf8SMatthew Dillon 		/*
12592aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
12602aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
12612aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
12624e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
12632aaeadf8SMatthew Dillon 		 */
12642aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
12652aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
12662aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
12678cc7e047SJohn Dyson 		vm_object_reference(object);
1268ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
12693364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
12703364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1271ef694c1aSEdward Tomasz Napierala 			crfree(cred);
1272ef694c1aSEdward Tomasz Napierala 			cred = NULL;
12733364c323SKonstantin Belousov 		}
1274b18bfc3dSJohn Dyson 	}
12752aaeadf8SMatthew Dillon 
12762aaeadf8SMatthew Dillon 	/*
12772aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
12782aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
12792aaeadf8SMatthew Dillon 	 * backing objects.
12802aaeadf8SMatthew Dillon 	 */
12818cc7e047SJohn Dyson 
128230dcfc09SJohn Dyson 	/*
128330dcfc09SJohn Dyson 	 * Create a new entry
128430dcfc09SJohn Dyson 	 */
128530dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
128630dcfc09SJohn Dyson 	new_entry->start = start;
128730dcfc09SJohn Dyson 	new_entry->end = end;
1288ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
128930dcfc09SJohn Dyson 
1290afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
129130dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
129230dcfc09SJohn Dyson 	new_entry->offset = offset;
12932267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12942267af78SJulian Elischer 
12958211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
129630dcfc09SJohn Dyson 	new_entry->protection = prot;
129730dcfc09SJohn Dyson 	new_entry->max_protection = max;
129830dcfc09SJohn Dyson 	new_entry->wired_count = 0;
129913458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
130013458803SAlan Cox 	new_entry->next_read = OFF_TO_IDX(offset);
1301e5f251d2SAlan Cox 
1302ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
13033364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1304ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
13053364c323SKonstantin Belousov 
130630dcfc09SJohn Dyson 	/*
130730dcfc09SJohn Dyson 	 * Insert the new entry into the list
130830dcfc09SJohn Dyson 	 */
130930dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
131030dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
131130dcfc09SJohn Dyson 
13121a484d28SMatthew Dillon 	/*
1313d2a444c0SAlan Cox 	 * It may be possible to merge the new entry with the next and/or
1314d2a444c0SAlan Cox 	 * previous entries.  However, due to MAP_STACK_* being a hack, a
1315d2a444c0SAlan Cox 	 * panic can result from merging such entries.
13161a484d28SMatthew Dillon 	 */
1317d2a444c0SAlan Cox 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0)
13184e71e795SMatthew Dillon 		vm_map_simplify_entry(map, new_entry);
13194e71e795SMatthew Dillon 
13204f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
13214da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1322e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1323e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
13244f79d873SMatthew Dillon 	}
1325e972780aSAlan Cox 
132630dcfc09SJohn Dyson 	return (KERN_SUCCESS);
132730dcfc09SJohn Dyson }
132830dcfc09SJohn Dyson 
132930dcfc09SJohn Dyson /*
13300164e057SAlan Cox  *	vm_map_findspace:
13310164e057SAlan Cox  *
13320164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
13330164e057SAlan Cox  *	beginning at address >= start in the given map.
13340164e057SAlan Cox  *
13350164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
13360164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
13370164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
13380164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
13390164e057SAlan Cox  *	O(log n) amortized with splay trees.
13400164e057SAlan Cox  *
13410164e057SAlan Cox  *	The map must be locked, and leaves it so.
13420164e057SAlan Cox  *
13430164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
13440164e057SAlan Cox  *		 1 if insufficient space.
1345df8bae1dSRodney W. Grimes  */
1346df8bae1dSRodney W. Grimes int
13470164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13480164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1349df8bae1dSRodney W. Grimes {
13500164e057SAlan Cox 	vm_map_entry_t entry;
1351f8616ebfSAlan Cox 	vm_offset_t st;
1352df8bae1dSRodney W. Grimes 
1353986b43f8SAlan Cox 	/*
1354986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1355986b43f8SAlan Cox 	 * address wrap.
1356986b43f8SAlan Cox 	 */
1357df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1358df8bae1dSRodney W. Grimes 		start = map->min_offset;
1359986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1360df8bae1dSRodney W. Grimes 		return (1);
1361df8bae1dSRodney W. Grimes 
13620164e057SAlan Cox 	/* Empty tree means wide open address space. */
13630164e057SAlan Cox 	if (map->root == NULL) {
1364df8bae1dSRodney W. Grimes 		*addr = start;
1365f8616ebfSAlan Cox 		return (0);
136699448ed1SJohn Dyson 	}
13670164e057SAlan Cox 
13680164e057SAlan Cox 	/*
13690164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13700164e057SAlan Cox 	 * must be a gap from start to the root.
13710164e057SAlan Cox 	 */
13720164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13730164e057SAlan Cox 	if (start + length <= map->root->start) {
13740164e057SAlan Cox 		*addr = start;
1375f8616ebfSAlan Cox 		return (0);
13760164e057SAlan Cox 	}
13770164e057SAlan Cox 
13780164e057SAlan Cox 	/*
13790164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1380986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1381986b43f8SAlan Cox 	 * wrap might be a problem.
13820164e057SAlan Cox 	 */
13830164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1384986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13850164e057SAlan Cox 		*addr = st;
1386f8616ebfSAlan Cox 		return (0);
13870164e057SAlan Cox 	}
13880164e057SAlan Cox 
13890164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13900164e057SAlan Cox 	entry = map->root->right;
13910164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13920164e057SAlan Cox 		return (1);
13930164e057SAlan Cox 
13940164e057SAlan Cox 	/*
13950164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13960164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13970164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13980164e057SAlan Cox 	 */
13990164e057SAlan Cox 	while (entry != NULL) {
14000164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
14010164e057SAlan Cox 			entry = entry->left;
14020164e057SAlan Cox 		else if (entry->adj_free >= length) {
14030164e057SAlan Cox 			*addr = entry->end;
1404f8616ebfSAlan Cox 			return (0);
14050164e057SAlan Cox 		} else
14060164e057SAlan Cox 			entry = entry->right;
14070164e057SAlan Cox 	}
14080164e057SAlan Cox 
14090164e057SAlan Cox 	/* Can't get here, so panic if we do. */
14100164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1411df8bae1dSRodney W. Grimes }
1412df8bae1dSRodney W. Grimes 
1413d239bd3cSKonstantin Belousov int
1414d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1415b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1416d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1417d239bd3cSKonstantin Belousov {
1418b8ca4ef2SAlan Cox 	vm_offset_t end;
1419d239bd3cSKonstantin Belousov 	int result;
1420d239bd3cSKonstantin Belousov 
1421d239bd3cSKonstantin Belousov 	end = start + length;
1422897d81a0SKonstantin Belousov 	vm_map_lock(map);
1423d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1424655c3490SKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1425d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1426d239bd3cSKonstantin Belousov 	    max, cow);
1427d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1428d239bd3cSKonstantin Belousov 	return (result);
1429d239bd3cSKonstantin Belousov }
1430d239bd3cSKonstantin Belousov 
1431df8bae1dSRodney W. Grimes /*
1432df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1433df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1434df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1435df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1436df8bae1dSRodney W. Grimes  *
14372aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
14382aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1439df8bae1dSRodney W. Grimes  */
1440df8bae1dSRodney W. Grimes int
1441b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1442b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
144326c538ffSAlan Cox 	    vm_size_t length, int find_space, vm_prot_t prot,
1444b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1445df8bae1dSRodney W. Grimes {
1446ff74a3faSJohn Baldwin 	vm_offset_t start, initial_addr;
14476eaee3feSAlan Cox 	int result;
1448df8bae1dSRodney W. Grimes 
1449ff74a3faSJohn Baldwin 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1450ff74a3faSJohn Baldwin 	    (object->flags & OBJ_COLORED) == 0))
1451ff74a3faSJohn Baldwin 			find_space = VMFS_ANY_SPACE;
1452ff74a3faSJohn Baldwin 	initial_addr = *addr;
1453ff74a3faSJohn Baldwin again:
1454ff74a3faSJohn Baldwin 	start = initial_addr;
1455bea41bcfSDavid Greenman 	vm_map_lock(map);
145626c538ffSAlan Cox 	do {
145726c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1458df8bae1dSRodney W. Grimes 			if (vm_map_findspace(map, start, length, addr)) {
1459df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1460ff74a3faSJohn Baldwin 				if (find_space == VMFS_OPTIMAL_SPACE) {
1461ff74a3faSJohn Baldwin 					find_space = VMFS_ANY_SPACE;
1462ff74a3faSJohn Baldwin 					goto again;
1463ff74a3faSJohn Baldwin 				}
1464df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1465df8bae1dSRodney W. Grimes 			}
1466ca596a25SJuli Mallett 			switch (find_space) {
1467ca596a25SJuli Mallett 			case VMFS_ALIGNED_SPACE:
1468ff74a3faSJohn Baldwin 			case VMFS_OPTIMAL_SPACE:
146926c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
147026c538ffSAlan Cox 				    length);
1471ca596a25SJuli Mallett 				break;
1472ca596a25SJuli Mallett #ifdef VMFS_TLB_ALIGNED_SPACE
1473ca596a25SJuli Mallett 			case VMFS_TLB_ALIGNED_SPACE:
1474ca596a25SJuli Mallett 				pmap_align_tlb(addr);
1475ca596a25SJuli Mallett 				break;
1476ca596a25SJuli Mallett #endif
1477ca596a25SJuli Mallett 			default:
1478ca596a25SJuli Mallett 				break;
1479ca596a25SJuli Mallett 			}
1480ca596a25SJuli Mallett 
1481df8bae1dSRodney W. Grimes 			start = *addr;
1482df8bae1dSRodney W. Grimes 		}
148326c538ffSAlan Cox 		result = vm_map_insert(map, object, offset, start, start +
148426c538ffSAlan Cox 		    length, prot, max, cow);
1485ff74a3faSJohn Baldwin 	} while (result == KERN_NO_SPACE && (find_space == VMFS_ALIGNED_SPACE ||
14869b55fc04SAlan Cox #ifdef VMFS_TLB_ALIGNED_SPACE
1487ff74a3faSJohn Baldwin 	    find_space == VMFS_TLB_ALIGNED_SPACE ||
14889b55fc04SAlan Cox #endif
1489ff74a3faSJohn Baldwin 	    find_space == VMFS_OPTIMAL_SPACE));
1490df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1491df8bae1dSRodney W. Grimes 	return (result);
1492df8bae1dSRodney W. Grimes }
1493df8bae1dSRodney W. Grimes 
1494df8bae1dSRodney W. Grimes /*
1495b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
149667bf6868SJohn Dyson  *
14974e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
14984e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
14994e71e795SMatthew Dillon  *
15004e71e795SMatthew Dillon  *	The map must be locked.
15014e71e795SMatthew Dillon  *
15024e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
15034e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
15044e71e795SMatthew Dillon  *	both neighbors.
1505df8bae1dSRodney W. Grimes  */
1506b7b2aac2SJohn Dyson void
15071b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1508df8bae1dSRodney W. Grimes {
1509308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1510b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1511df8bae1dSRodney W. Grimes 
1512acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1513df8bae1dSRodney W. Grimes 		return;
1514308c24baSJohn Dyson 
1515308c24baSJohn Dyson 	prev = entry->prev;
1516308c24baSJohn Dyson 	if (prev != &map->header) {
151767bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
151867bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
151967bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
152095e5e988SJohn Dyson 		     (!prev->object.vm_object ||
152167bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1522afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
152367bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
152467bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
152567bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
15263364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
1527ef694c1aSEdward Tomasz Napierala 		     (prev->cred == entry->cred)) {
1528308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1529308c24baSJohn Dyson 			entry->start = prev->start;
1530308c24baSJohn Dyson 			entry->offset = prev->offset;
15310164e057SAlan Cox 			if (entry->prev != &map->header)
15320164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
15337fd10fb3SKonstantin Belousov 
15347fd10fb3SKonstantin Belousov 			/*
1535b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1536b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1537b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1538b0994946SKonstantin Belousov 			 * because the vnode has additional
1539b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1540b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1541b0994946SKonstantin Belousov 			 * the vnode lock.
154284110e7eSKonstantin Belousov 			 *
154384110e7eSKonstantin Belousov 			 * Since we count the number of virtual page
154484110e7eSKonstantin Belousov 			 * mappings in object->un_pager.vnp.writemappings,
154584110e7eSKonstantin Belousov 			 * the writemappings value should not be adjusted
154684110e7eSKonstantin Belousov 			 * when the entry is disposed of.
15477fd10fb3SKonstantin Belousov 			 */
1548b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1549308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1550ef694c1aSEdward Tomasz Napierala 			if (prev->cred != NULL)
1551ef694c1aSEdward Tomasz Napierala 				crfree(prev->cred);
1552308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1553308c24baSJohn Dyson 		}
1554308c24baSJohn Dyson 	}
1555de5f6a77SJohn Dyson 
1556de5f6a77SJohn Dyson 	next = entry->next;
1557308c24baSJohn Dyson 	if (next != &map->header) {
155867bf6868SJohn Dyson 		esize = entry->end - entry->start;
155967bf6868SJohn Dyson 		if ((entry->end == next->start) &&
156067bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
156167bf6868SJohn Dyson 		     (!entry->object.vm_object ||
156267bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1563afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
156467bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
156567bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
156667bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
15673364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
1568ef694c1aSEdward Tomasz Napierala 		    (next->cred == entry->cred)) {
1569de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1570de5f6a77SJohn Dyson 			entry->end = next->end;
15710164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
15727fd10fb3SKonstantin Belousov 
15737fd10fb3SKonstantin Belousov 			/*
15747fd10fb3SKonstantin Belousov 			 * See comment above.
15757fd10fb3SKonstantin Belousov 			 */
1576b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1577de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1578ef694c1aSEdward Tomasz Napierala 			if (next->cred != NULL)
1579ef694c1aSEdward Tomasz Napierala 				crfree(next->cred);
1580de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1581df8bae1dSRodney W. Grimes 		}
1582df8bae1dSRodney W. Grimes 	}
1583de5f6a77SJohn Dyson }
1584df8bae1dSRodney W. Grimes /*
1585df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1586df8bae1dSRodney W. Grimes  *
1587df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1588df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1589df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1590df8bae1dSRodney W. Grimes  */
1591df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1592df8bae1dSRodney W. Grimes { \
1593df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1594df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1595df8bae1dSRodney W. Grimes }
1596df8bae1dSRodney W. Grimes 
1597df8bae1dSRodney W. Grimes /*
1598df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1599df8bae1dSRodney W. Grimes  *	the entry must be split.
1600df8bae1dSRodney W. Grimes  */
16010d94caffSDavid Greenman static void
16021b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1603df8bae1dSRodney W. Grimes {
1604c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1605df8bae1dSRodney W. Grimes 
16063a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16073a0916b8SKonstantin Belousov 
1608df8bae1dSRodney W. Grimes 	/*
16090d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
16100d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
16110d94caffSDavid Greenman 	 * starting address.
1612df8bae1dSRodney W. Grimes 	 */
1613f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1614f32dbbeeSJohn Dyson 
161511cccda1SJohn Dyson 	/*
161611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
161711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
161811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
161911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
162011cccda1SJohn Dyson 	 * put this improvement.
162111cccda1SJohn Dyson 	 */
16224e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
162311cccda1SJohn Dyson 		vm_object_t object;
162411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1625c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
162611cccda1SJohn Dyson 		entry->object.vm_object = object;
162711cccda1SJohn Dyson 		entry->offset = 0;
1628ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1629ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
16303364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1631ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
16323364c323SKonstantin Belousov 		}
16333364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16343364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1635ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
163689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1637ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1638ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1639ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
16403364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
164189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1642ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
164311cccda1SJohn Dyson 	}
164411cccda1SJohn Dyson 
1645df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1646df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1647df8bae1dSRodney W. Grimes 
1648df8bae1dSRodney W. Grimes 	new_entry->end = start;
1649df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1650df8bae1dSRodney W. Grimes 	entry->start = start;
1651ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1652ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1653df8bae1dSRodney W. Grimes 
1654df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1655df8bae1dSRodney W. Grimes 
16569fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1657df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
165884110e7eSKonstantin Belousov 		/*
165984110e7eSKonstantin Belousov 		 * The object->un_pager.vnp.writemappings for the
166084110e7eSKonstantin Belousov 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
166184110e7eSKonstantin Belousov 		 * kept as is here.  The virtual pages are
166284110e7eSKonstantin Belousov 		 * re-distributed among the clipped entries, so the sum is
166384110e7eSKonstantin Belousov 		 * left the same.
166484110e7eSKonstantin Belousov 		 */
1665df8bae1dSRodney W. Grimes 	}
1666c0877f10SJohn Dyson }
1667df8bae1dSRodney W. Grimes 
1668df8bae1dSRodney W. Grimes /*
1669df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1670df8bae1dSRodney W. Grimes  *
1671df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1672df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1673df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1674df8bae1dSRodney W. Grimes  */
1675df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1676df8bae1dSRodney W. Grimes { \
1677af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1678af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1679df8bae1dSRodney W. Grimes }
1680df8bae1dSRodney W. Grimes 
1681df8bae1dSRodney W. Grimes /*
1682df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1683df8bae1dSRodney W. Grimes  *	the entry must be split.
1684df8bae1dSRodney W. Grimes  */
16850d94caffSDavid Greenman static void
16861b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1687df8bae1dSRodney W. Grimes {
1688c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1689df8bae1dSRodney W. Grimes 
16903a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16913a0916b8SKonstantin Belousov 
1692df8bae1dSRodney W. Grimes 	/*
169311cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
169411cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
169511cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
169611cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
169711cccda1SJohn Dyson 	 * put this improvement.
169811cccda1SJohn Dyson 	 */
16994e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
170011cccda1SJohn Dyson 		vm_object_t object;
170111cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1702c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
170311cccda1SJohn Dyson 		entry->object.vm_object = object;
170411cccda1SJohn Dyson 		entry->offset = 0;
1705ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1706ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
17073364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1708ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
17093364c323SKonstantin Belousov 		}
17103364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
17113364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1712ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
171389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1714ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1715ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1716ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
17173364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
171889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1719ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
172011cccda1SJohn Dyson 	}
172111cccda1SJohn Dyson 
172211cccda1SJohn Dyson 	/*
17230d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1724df8bae1dSRodney W. Grimes 	 */
1725df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1726df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1727df8bae1dSRodney W. Grimes 
1728df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1729df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1730ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1731ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1732df8bae1dSRodney W. Grimes 
1733df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1734df8bae1dSRodney W. Grimes 
17359fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1736df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1737df8bae1dSRodney W. Grimes 	}
1738c0877f10SJohn Dyson }
1739df8bae1dSRodney W. Grimes 
1740df8bae1dSRodney W. Grimes /*
1741df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1742df8bae1dSRodney W. Grimes  *
1743df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1744df8bae1dSRodney W. Grimes  *
1745df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1746df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1747df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1748df8bae1dSRodney W. Grimes  *
1749df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1750df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1751df8bae1dSRodney W. Grimes  *		vm_fault
1752df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1753df8bae1dSRodney W. Grimes  *
1754df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1755df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1756df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1757df8bae1dSRodney W. Grimes  */
1758df8bae1dSRodney W. Grimes int
17591b40f8c0SMatthew Dillon vm_map_submap(
17601b40f8c0SMatthew Dillon 	vm_map_t map,
17611b40f8c0SMatthew Dillon 	vm_offset_t start,
17621b40f8c0SMatthew Dillon 	vm_offset_t end,
17631b40f8c0SMatthew Dillon 	vm_map_t submap)
1764df8bae1dSRodney W. Grimes {
1765df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1766c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1767df8bae1dSRodney W. Grimes 
1768df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1769df8bae1dSRodney W. Grimes 
1770df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1771df8bae1dSRodney W. Grimes 
1772df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1773df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17740d94caffSDavid Greenman 	} else
1775df8bae1dSRodney W. Grimes 		entry = entry->next;
1776df8bae1dSRodney W. Grimes 
1777df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1778df8bae1dSRodney W. Grimes 
1779df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
17809fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1781afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17822d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1783afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1784df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1785df8bae1dSRodney W. Grimes 	}
1786df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1787df8bae1dSRodney W. Grimes 
1788df8bae1dSRodney W. Grimes 	return (result);
1789df8bae1dSRodney W. Grimes }
1790df8bae1dSRodney W. Grimes 
1791df8bae1dSRodney W. Grimes /*
17921f78f902SAlan Cox  * The maximum number of pages to map
17931f78f902SAlan Cox  */
17941f78f902SAlan Cox #define	MAX_INIT_PT	96
17951f78f902SAlan Cox 
17961f78f902SAlan Cox /*
17970551c08dSAlan Cox  *	vm_map_pmap_enter:
17980551c08dSAlan Cox  *
1799a922d312SAlan Cox  *	Preload read-only mappings for the specified object's resident pages
1800a922d312SAlan Cox  *	into the target map.  If "flags" is MAP_PREFAULT_PARTIAL, then only
1801a922d312SAlan Cox  *	the resident pages within the address range [addr, addr + ulmin(size,
1802a922d312SAlan Cox  *	ptoa(MAX_INIT_PT))) are mapped.  Otherwise, all resident pages within
1803a922d312SAlan Cox  *	the specified address range are mapped.  This eliminates many soft
1804a922d312SAlan Cox  *	faults on process startup and immediately after an mmap(2).  Because
1805a922d312SAlan Cox  *	these are speculative mappings, cached pages are not reactivated and
1806a922d312SAlan Cox  *	mapped.
18070551c08dSAlan Cox  */
18080551c08dSAlan Cox void
18094da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
18100551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
18110551c08dSAlan Cox {
18128fece8c3SAlan Cox 	vm_offset_t start;
1813ce142d9eSAlan Cox 	vm_page_t p, p_start;
18148fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
18150551c08dSAlan Cox 
1816ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
18171f78f902SAlan Cox 		return;
18189af6d512SAttilio Rao 	VM_OBJECT_RLOCK(object);
18199af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18209af6d512SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
182189f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
182201381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18239af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
18249af6d512SAttilio Rao 			    size);
18259af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18269af6d512SAttilio Rao 			return;
18279af6d512SAttilio Rao 		}
18289af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
18291f78f902SAlan Cox 	}
18301f78f902SAlan Cox 
18311f78f902SAlan Cox 	psize = atop(size);
1832a922d312SAlan Cox 	if (psize > MAX_INIT_PT && (flags & MAP_PREFAULT_PARTIAL) != 0)
1833a922d312SAlan Cox 		psize = MAX_INIT_PT;
18341f78f902SAlan Cox 	if (psize + pindex > object->size) {
18359af6d512SAttilio Rao 		if (object->size < pindex) {
18369af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
18379af6d512SAttilio Rao 			return;
18389af6d512SAttilio Rao 		}
18391f78f902SAlan Cox 		psize = object->size - pindex;
18401f78f902SAlan Cox 	}
18411f78f902SAlan Cox 
1842ce142d9eSAlan Cox 	start = 0;
1843ce142d9eSAlan Cox 	p_start = NULL;
18441f78f902SAlan Cox 
1845b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
18461f78f902SAlan Cox 	/*
18471f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
18481f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
18491f78f902SAlan Cox 	 * or (2) NULL.
18501f78f902SAlan Cox 	 */
18511f78f902SAlan Cox 	for (;
18521f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
18531f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
18541f78f902SAlan Cox 		/*
18551f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
18561f78f902SAlan Cox 		 * free pages allocating pv entries.
18571f78f902SAlan Cox 		 */
18581f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
18592feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1860379fb642SAlan Cox 			psize = tmpidx;
18611f78f902SAlan Cox 			break;
18621f78f902SAlan Cox 		}
18630a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1864ce142d9eSAlan Cox 			if (p_start == NULL) {
1865ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1866ce142d9eSAlan Cox 				p_start = p;
1867ce142d9eSAlan Cox 			}
18687bfda801SAlan Cox 		} else if (p_start != NULL) {
1869cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1870cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1871cf4682aeSAlan Cox 			p_start = NULL;
1872cf4682aeSAlan Cox 		}
1873cf4682aeSAlan Cox 	}
1874c46b90e9SAlan Cox 	if (p_start != NULL)
1875379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1876379fb642SAlan Cox 		    p_start, prot);
18779af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
18780551c08dSAlan Cox }
18790551c08dSAlan Cox 
18800551c08dSAlan Cox /*
1881df8bae1dSRodney W. Grimes  *	vm_map_protect:
1882df8bae1dSRodney W. Grimes  *
1883df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1884df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1885df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1886df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1887df8bae1dSRodney W. Grimes  */
1888df8bae1dSRodney W. Grimes int
1889b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1890b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1891df8bae1dSRodney W. Grimes {
1892210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
18933364c323SKonstantin Belousov 	vm_object_t obj;
1894ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
1895210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1896df8bae1dSRodney W. Grimes 
1897df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1898df8bae1dSRodney W. Grimes 
1899df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1902df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1903b7b2aac2SJohn Dyson 	} else {
1904df8bae1dSRodney W. Grimes 		entry = entry->next;
1905b7b2aac2SJohn Dyson 	}
1906df8bae1dSRodney W. Grimes 
1907df8bae1dSRodney W. Grimes 	/*
19080d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1909df8bae1dSRodney W. Grimes 	 */
1910df8bae1dSRodney W. Grimes 	current = entry;
1911df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1912afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1913a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1914df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1915a1f6d91cSDavid Greenman 		}
1916df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1917df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1918df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1919df8bae1dSRodney W. Grimes 		}
1920df8bae1dSRodney W. Grimes 		current = current->next;
1921df8bae1dSRodney W. Grimes 	}
1922df8bae1dSRodney W. Grimes 
19233364c323SKonstantin Belousov 
19243364c323SKonstantin Belousov 	/*
19253364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
19263364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
19273364c323SKonstantin Belousov 	 * breakpoint on text segment)
19283364c323SKonstantin Belousov 	 */
19293364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
19303364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
19313364c323SKonstantin Belousov 
19323364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
19333364c323SKonstantin Belousov 
19343364c323SKonstantin Belousov 		if (set_max ||
19353364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
19363364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
19373364c323SKonstantin Belousov 			continue;
19383364c323SKonstantin Belousov 		}
19393364c323SKonstantin Belousov 
1940ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
19413364c323SKonstantin Belousov 		obj = current->object.vm_object;
19423364c323SKonstantin Belousov 
19433364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
19443364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
19453364c323SKonstantin Belousov 				vm_map_unlock(map);
19463364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
19473364c323SKonstantin Belousov 			}
1948ef694c1aSEdward Tomasz Napierala 			crhold(cred);
1949ef694c1aSEdward Tomasz Napierala 			current->cred = cred;
19503364c323SKonstantin Belousov 			continue;
19513364c323SKonstantin Belousov 		}
19523364c323SKonstantin Belousov 
195389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
19543364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
195589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19563364c323SKonstantin Belousov 			continue;
19573364c323SKonstantin Belousov 		}
19583364c323SKonstantin Belousov 
19593364c323SKonstantin Belousov 		/*
19603364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
19613364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
19623364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
19633364c323SKonstantin Belousov 		 */
19643364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
19653364c323SKonstantin Belousov 		    ("vm_map_protect: object %p overcharged\n", obj));
19663364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
196789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19683364c323SKonstantin Belousov 			vm_map_unlock(map);
19693364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
19703364c323SKonstantin Belousov 		}
19713364c323SKonstantin Belousov 
1972ef694c1aSEdward Tomasz Napierala 		crhold(cred);
1973ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
19743364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
197589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
19763364c323SKonstantin Belousov 	}
19773364c323SKonstantin Belousov 
1978df8bae1dSRodney W. Grimes 	/*
19790d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
19800d94caffSDavid Greenman 	 * necessary the second time.]
1981df8bae1dSRodney W. Grimes 	 */
1982df8bae1dSRodney W. Grimes 	current = entry;
1983df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1984df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1985210a6886SKonstantin Belousov 
1986df8bae1dSRodney W. Grimes 		if (set_max)
1987df8bae1dSRodney W. Grimes 			current->protection =
1988df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1989df8bae1dSRodney W. Grimes 			    old_prot;
1990df8bae1dSRodney W. Grimes 		else
1991df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1992df8bae1dSRodney W. Grimes 
1993210a6886SKonstantin Belousov 		if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1994210a6886SKonstantin Belousov 		     == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1995210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
1996210a6886SKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0) {
1997210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
1998210a6886SKonstantin Belousov 		}
1999210a6886SKonstantin Belousov 
2000df8bae1dSRodney W. Grimes 		/*
20012fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
20022fafce9eSAlan Cox 		 * about copy-on-write here.
2003df8bae1dSRodney W. Grimes 		 */
20042fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
2005afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2006df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
2007df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
2008df8bae1dSRodney W. Grimes 			    current->end,
20091c85e3dfSAlan Cox 			    current->protection & MASK(current));
2010df8bae1dSRodney W. Grimes #undef	MASK
2011df8bae1dSRodney W. Grimes 		}
20127d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
2013df8bae1dSRodney W. Grimes 		current = current->next;
2014df8bae1dSRodney W. Grimes 	}
2015df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2016df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2017df8bae1dSRodney W. Grimes }
2018df8bae1dSRodney W. Grimes 
2019df8bae1dSRodney W. Grimes /*
2020867a482dSJohn Dyson  *	vm_map_madvise:
2021867a482dSJohn Dyson  *
2022867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2023f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2024f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2025f7fc307aSAlan Cox  *	objects.
2026867a482dSJohn Dyson  */
2027b4309055SMatthew Dillon int
20281b40f8c0SMatthew Dillon vm_map_madvise(
20291b40f8c0SMatthew Dillon 	vm_map_t map,
20301b40f8c0SMatthew Dillon 	vm_offset_t start,
20311b40f8c0SMatthew Dillon 	vm_offset_t end,
20321b40f8c0SMatthew Dillon 	int behav)
2033867a482dSJohn Dyson {
2034f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
2035b4309055SMatthew Dillon 	int modify_map = 0;
2036867a482dSJohn Dyson 
2037b4309055SMatthew Dillon 	/*
2038b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2039b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2040b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2041b4309055SMatthew Dillon 	 * on the map.
2042b4309055SMatthew Dillon 	 */
2043b4309055SMatthew Dillon 	switch(behav) {
2044b4309055SMatthew Dillon 	case MADV_NORMAL:
2045b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2046b4309055SMatthew Dillon 	case MADV_RANDOM:
20474f79d873SMatthew Dillon 	case MADV_NOSYNC:
20484f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
20499730a5daSPaul Saab 	case MADV_NOCORE:
20509730a5daSPaul Saab 	case MADV_CORE:
2051b4309055SMatthew Dillon 		modify_map = 1;
2052867a482dSJohn Dyson 		vm_map_lock(map);
2053b4309055SMatthew Dillon 		break;
2054b4309055SMatthew Dillon 	case MADV_WILLNEED:
2055b4309055SMatthew Dillon 	case MADV_DONTNEED:
2056b4309055SMatthew Dillon 	case MADV_FREE:
2057f7fc307aSAlan Cox 		vm_map_lock_read(map);
2058b4309055SMatthew Dillon 		break;
2059b4309055SMatthew Dillon 	default:
2060b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
2061b4309055SMatthew Dillon 	}
2062b4309055SMatthew Dillon 
2063b4309055SMatthew Dillon 	/*
2064b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2065b4309055SMatthew Dillon 	 */
2066867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2067867a482dSJohn Dyson 
2068867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
2069f7fc307aSAlan Cox 		if (modify_map)
2070867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
2071b4309055SMatthew Dillon 	} else {
2072867a482dSJohn Dyson 		entry = entry->next;
2073b4309055SMatthew Dillon 	}
2074867a482dSJohn Dyson 
2075f7fc307aSAlan Cox 	if (modify_map) {
2076f7fc307aSAlan Cox 		/*
2077f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2078f7fc307aSAlan Cox 		 *
2079f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2080f7fc307aSAlan Cox 		 * limited to the specified address range.
2081f7fc307aSAlan Cox 		 */
2082867a482dSJohn Dyson 		for (current = entry;
2083867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2084b4309055SMatthew Dillon 		     current = current->next
2085b4309055SMatthew Dillon 		) {
2086f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2087867a482dSJohn Dyson 				continue;
2088fed9a903SJohn Dyson 
208947221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2090fed9a903SJohn Dyson 
2091f7fc307aSAlan Cox 			switch (behav) {
2092867a482dSJohn Dyson 			case MADV_NORMAL:
20937f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2094867a482dSJohn Dyson 				break;
2095867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
20967f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2097867a482dSJohn Dyson 				break;
2098867a482dSJohn Dyson 			case MADV_RANDOM:
20997f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2100867a482dSJohn Dyson 				break;
21014f79d873SMatthew Dillon 			case MADV_NOSYNC:
21024f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
21034f79d873SMatthew Dillon 				break;
21044f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
21054f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
21064f79d873SMatthew Dillon 				break;
21079730a5daSPaul Saab 			case MADV_NOCORE:
21089730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
21099730a5daSPaul Saab 				break;
21109730a5daSPaul Saab 			case MADV_CORE:
21119730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
21129730a5daSPaul Saab 				break;
2113867a482dSJohn Dyson 			default:
2114867a482dSJohn Dyson 				break;
2115867a482dSJohn Dyson 			}
2116f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2117867a482dSJohn Dyson 		}
2118867a482dSJohn Dyson 		vm_map_unlock(map);
2119b4309055SMatthew Dillon 	} else {
212092a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
2121f7fc307aSAlan Cox 
2122f7fc307aSAlan Cox 		/*
2123f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2124f7fc307aSAlan Cox 		 * vm_object.
2125f7fc307aSAlan Cox 		 *
2126f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2127f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2128f7fc307aSAlan Cox 		 */
2129f7fc307aSAlan Cox 		for (current = entry;
2130f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2131b4309055SMatthew Dillon 		     current = current->next
2132b4309055SMatthew Dillon 		) {
21335f99b57cSMatthew Dillon 			vm_offset_t useStart;
21345f99b57cSMatthew Dillon 
2135f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2136f7fc307aSAlan Cox 				continue;
2137f7fc307aSAlan Cox 
213892a59946SJohn Baldwin 			pstart = OFF_TO_IDX(current->offset);
213992a59946SJohn Baldwin 			pend = pstart + atop(current->end - current->start);
21405f99b57cSMatthew Dillon 			useStart = current->start;
2141f7fc307aSAlan Cox 
2142f7fc307aSAlan Cox 			if (current->start < start) {
214392a59946SJohn Baldwin 				pstart += atop(start - current->start);
21445f99b57cSMatthew Dillon 				useStart = start;
2145f7fc307aSAlan Cox 			}
2146f7fc307aSAlan Cox 			if (current->end > end)
214792a59946SJohn Baldwin 				pend -= atop(current->end - end);
2148f7fc307aSAlan Cox 
214992a59946SJohn Baldwin 			if (pstart >= pend)
2150f7fc307aSAlan Cox 				continue;
2151f7fc307aSAlan Cox 
215292a59946SJohn Baldwin 			vm_object_madvise(current->object.vm_object, pstart,
215392a59946SJohn Baldwin 			    pend, behav);
2154b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
21550551c08dSAlan Cox 				vm_map_pmap_enter(map,
21565f99b57cSMatthew Dillon 				    useStart,
21574da4d293SAlan Cox 				    current->protection,
2158f7fc307aSAlan Cox 				    current->object.vm_object,
215992a59946SJohn Baldwin 				    pstart,
216092a59946SJohn Baldwin 				    ptoa(pend - pstart),
2161e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2162b4309055SMatthew Dillon 				);
2163f7fc307aSAlan Cox 			}
2164f7fc307aSAlan Cox 		}
2165f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2166f7fc307aSAlan Cox 	}
2167b4309055SMatthew Dillon 	return (0);
2168867a482dSJohn Dyson }
2169867a482dSJohn Dyson 
2170867a482dSJohn Dyson 
2171867a482dSJohn Dyson /*
2172df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2173df8bae1dSRodney W. Grimes  *
2174df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2175df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2176df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2177e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2178df8bae1dSRodney W. Grimes  */
2179df8bae1dSRodney W. Grimes int
2180b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2181b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2182df8bae1dSRodney W. Grimes {
2183c0877f10SJohn Dyson 	vm_map_entry_t entry;
2184df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2185df8bae1dSRodney W. Grimes 
2186df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2187df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2188df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2189df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2190df8bae1dSRodney W. Grimes 		break;
2191df8bae1dSRodney W. Grimes 	default:
2192df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2193df8bae1dSRodney W. Grimes 	}
2194df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2195df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2196df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2197df8bae1dSRodney W. Grimes 		entry = temp_entry;
2198df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
21990d94caffSDavid Greenman 	} else
2200df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2201df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2202df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2203df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
220444428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2205df8bae1dSRodney W. Grimes 		entry = entry->next;
2206df8bae1dSRodney W. Grimes 	}
2207df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2208df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2209df8bae1dSRodney W. Grimes }
2210df8bae1dSRodney W. Grimes 
2211df8bae1dSRodney W. Grimes /*
2212acd9a301SAlan Cox  *	vm_map_unwire:
2213acd9a301SAlan Cox  *
2214e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2215acd9a301SAlan Cox  */
2216acd9a301SAlan Cox int
2217acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2218abd498aaSBruce M Simpson     int flags)
2219acd9a301SAlan Cox {
2220acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2221acd9a301SAlan Cox 	vm_offset_t saved_start;
2222acd9a301SAlan Cox 	unsigned int last_timestamp;
2223acd9a301SAlan Cox 	int rv;
2224abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2225acd9a301SAlan Cox 
2226abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2227acd9a301SAlan Cox 	vm_map_lock(map);
2228acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2229acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2230abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2231cbef13d8SAlan Cox 			first_entry = first_entry->next;
2232abd498aaSBruce M Simpson 		else {
2233acd9a301SAlan Cox 			vm_map_unlock(map);
2234acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2235acd9a301SAlan Cox 		}
2236abd498aaSBruce M Simpson 	}
2237acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2238acd9a301SAlan Cox 	entry = first_entry;
2239acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2240acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2241acd9a301SAlan Cox 			/*
2242acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2243acd9a301SAlan Cox 			 */
2244acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2245acd9a301SAlan Cox 			    entry->start;
2246acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
22478ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2248acd9a301SAlan Cox 				/*
2249acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2250acd9a301SAlan Cox 				 */
2251acd9a301SAlan Cox 			}
2252acd9a301SAlan Cox 			vm_map_lock(map);
2253acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2254acd9a301SAlan Cox 				/*
2255acd9a301SAlan Cox 				 * Look again for the entry because the map was
2256acd9a301SAlan Cox 				 * modified while it was unlocked.
2257acd9a301SAlan Cox 				 * Specifically, the entry may have been
2258acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2259acd9a301SAlan Cox 				 */
2260acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2261acd9a301SAlan Cox 				    &tmp_entry)) {
2262cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2263cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2264cbef13d8SAlan Cox 					else {
2265acd9a301SAlan Cox 						if (saved_start == start) {
2266acd9a301SAlan Cox 							/*
2267acd9a301SAlan Cox 							 * First_entry has been deleted.
2268acd9a301SAlan Cox 							 */
2269acd9a301SAlan Cox 							vm_map_unlock(map);
2270acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2271acd9a301SAlan Cox 						}
2272acd9a301SAlan Cox 						end = saved_start;
2273acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2274acd9a301SAlan Cox 						goto done;
2275acd9a301SAlan Cox 					}
2276cbef13d8SAlan Cox 				}
2277acd9a301SAlan Cox 				if (entry == first_entry)
2278acd9a301SAlan Cox 					first_entry = tmp_entry;
2279acd9a301SAlan Cox 				else
2280acd9a301SAlan Cox 					first_entry = NULL;
2281acd9a301SAlan Cox 				entry = tmp_entry;
2282acd9a301SAlan Cox 			}
2283acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2284acd9a301SAlan Cox 			continue;
2285acd9a301SAlan Cox 		}
2286acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2287acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2288acd9a301SAlan Cox 		/*
2289acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2290acd9a301SAlan Cox 		 * above.)
2291acd9a301SAlan Cox 		 */
2292acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
22930acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2294acd9a301SAlan Cox 		/*
2295acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2296abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2297acd9a301SAlan Cox 		 */
2298abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2299abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2300abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2301acd9a301SAlan Cox 			end = entry->end;
2302acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2303acd9a301SAlan Cox 			goto done;
2304acd9a301SAlan Cox 		}
2305acd9a301SAlan Cox 		/*
23063ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2307acd9a301SAlan Cox 		 */
23080ada205eSBrian Feldman 		if (!user_unwire &&
23090ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2310acd9a301SAlan Cox 			end = entry->end;
2311acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2312acd9a301SAlan Cox 			goto done;
2313acd9a301SAlan Cox 		}
2314acd9a301SAlan Cox 		entry = entry->next;
2315acd9a301SAlan Cox 	}
2316acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2317acd9a301SAlan Cox done:
2318e27e17b7SAlan Cox 	need_wakeup = FALSE;
2319acd9a301SAlan Cox 	if (first_entry == NULL) {
2320acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2321cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2322cbef13d8SAlan Cox 			first_entry = first_entry->next;
2323cbef13d8SAlan Cox 		else
2324acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2325acd9a301SAlan Cox 	}
23260acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
23270acea7dfSKonstantin Belousov 	    entry = entry->next) {
23280acea7dfSKonstantin Belousov 		/*
23290acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
23300acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
23310acea7dfSKonstantin Belousov 		 * while the map lock was dropped for draining
23320acea7dfSKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
23330acea7dfSKonstantin Belousov 		 * could be simultaneously wiring this new mapping
23340acea7dfSKonstantin Belousov 		 * entry.  Detect these cases and skip any entries
23350acea7dfSKonstantin Belousov 		 * marked as in transition by us.
23360acea7dfSKonstantin Belousov 		 */
23370acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
23380acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
23390acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
23400acea7dfSKonstantin Belousov 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
23410acea7dfSKonstantin Belousov 			continue;
23420acea7dfSKonstantin Belousov 		}
23430acea7dfSKonstantin Belousov 
23443ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
23453ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2346b2f3846aSAlan Cox 			if (user_unwire)
2347b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2348b2f3846aSAlan Cox 			entry->wired_count--;
23490ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2350b2f3846aSAlan Cox 				/*
2351b2f3846aSAlan Cox 				 * Retain the map lock.
2352b2f3846aSAlan Cox 				 */
23534be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
23544be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
235528634820SAlan Cox 				    (entry->object.vm_object->flags &
235628634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
2357b2f3846aSAlan Cox 			}
2358b2f3846aSAlan Cox 		}
23590acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2360acd9a301SAlan Cox 		    ("vm_map_unwire: in-transition flag missing"));
2361acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
23620acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
2363acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2364acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2365acd9a301SAlan Cox 			need_wakeup = TRUE;
2366acd9a301SAlan Cox 		}
2367acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2368acd9a301SAlan Cox 	}
2369acd9a301SAlan Cox 	vm_map_unlock(map);
2370acd9a301SAlan Cox 	if (need_wakeup)
2371acd9a301SAlan Cox 		vm_map_wakeup(map);
2372acd9a301SAlan Cox 	return (rv);
2373acd9a301SAlan Cox }
2374acd9a301SAlan Cox 
2375acd9a301SAlan Cox /*
2376e27e17b7SAlan Cox  *	vm_map_wire:
2377e27e17b7SAlan Cox  *
2378e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2379e27e17b7SAlan Cox  */
2380e27e17b7SAlan Cox int
2381e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2382abd498aaSBruce M Simpson     int flags)
2383e27e17b7SAlan Cox {
238412d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
238512d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
238612d7cc84SAlan Cox 	unsigned int last_timestamp;
238712d7cc84SAlan Cox 	int rv;
23884be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2389e4cd31ddSJeff Roberson 	vm_prot_t prot;
2390e27e17b7SAlan Cox 
2391e4cd31ddSJeff Roberson 	prot = 0;
2392e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
2393e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
2394abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
239512d7cc84SAlan Cox 	vm_map_lock(map);
239612d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
239712d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2398abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2399cbef13d8SAlan Cox 			first_entry = first_entry->next;
2400abd498aaSBruce M Simpson 		else {
240112d7cc84SAlan Cox 			vm_map_unlock(map);
240212d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
240312d7cc84SAlan Cox 		}
2404abd498aaSBruce M Simpson 	}
240512d7cc84SAlan Cox 	last_timestamp = map->timestamp;
240612d7cc84SAlan Cox 	entry = first_entry;
240712d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
240812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
240912d7cc84SAlan Cox 			/*
241012d7cc84SAlan Cox 			 * We have not yet clipped the entry.
241112d7cc84SAlan Cox 			 */
241212d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
241312d7cc84SAlan Cox 			    entry->start;
241412d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
24158ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
241612d7cc84SAlan Cox 				/*
241712d7cc84SAlan Cox 				 * Allow interruption of user wiring?
241812d7cc84SAlan Cox 				 */
241912d7cc84SAlan Cox 			}
242012d7cc84SAlan Cox 			vm_map_lock(map);
242112d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
242212d7cc84SAlan Cox 				/*
242312d7cc84SAlan Cox 				 * Look again for the entry because the map was
242412d7cc84SAlan Cox 				 * modified while it was unlocked.
242512d7cc84SAlan Cox 				 * Specifically, the entry may have been
242612d7cc84SAlan Cox 				 * clipped, merged, or deleted.
242712d7cc84SAlan Cox 				 */
242812d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
242912d7cc84SAlan Cox 				    &tmp_entry)) {
2430cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2431cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2432cbef13d8SAlan Cox 					else {
243312d7cc84SAlan Cox 						if (saved_start == start) {
243412d7cc84SAlan Cox 							/*
243512d7cc84SAlan Cox 							 * first_entry has been deleted.
243612d7cc84SAlan Cox 							 */
243712d7cc84SAlan Cox 							vm_map_unlock(map);
243812d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
243912d7cc84SAlan Cox 						}
244012d7cc84SAlan Cox 						end = saved_start;
244112d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
244212d7cc84SAlan Cox 						goto done;
244312d7cc84SAlan Cox 					}
2444cbef13d8SAlan Cox 				}
244512d7cc84SAlan Cox 				if (entry == first_entry)
244612d7cc84SAlan Cox 					first_entry = tmp_entry;
244712d7cc84SAlan Cox 				else
244812d7cc84SAlan Cox 					first_entry = NULL;
244912d7cc84SAlan Cox 				entry = tmp_entry;
245012d7cc84SAlan Cox 			}
245112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
245212d7cc84SAlan Cox 			continue;
245312d7cc84SAlan Cox 		}
245412d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
245512d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
245612d7cc84SAlan Cox 		/*
245712d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
245812d7cc84SAlan Cox 		 * above.)
245912d7cc84SAlan Cox 		 */
246012d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
24610acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2462e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2463e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
2464529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
24656d7e8091SKonstantin Belousov 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
24666d7e8091SKonstantin Belousov 				end = entry->end;
24676d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
24686d7e8091SKonstantin Belousov 				goto done;
24696d7e8091SKonstantin Belousov 			}
24706d7e8091SKonstantin Belousov 			goto next_entry;
24716d7e8091SKonstantin Belousov 		}
2472e4cd31ddSJeff Roberson 		if (entry->wired_count == 0) {
24730ada205eSBrian Feldman 			entry->wired_count++;
247412d7cc84SAlan Cox 			saved_start = entry->start;
247512d7cc84SAlan Cox 			saved_end = entry->end;
24764be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
247728634820SAlan Cox 			    (entry->object.vm_object->flags &
247828634820SAlan Cox 			    OBJ_FICTITIOUS) != 0;
247912d7cc84SAlan Cox 			/*
248012d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
2481a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
248212d7cc84SAlan Cox 			 */
2483a5db445dSMax Laier 			vm_map_busy(map);
248412d7cc84SAlan Cox 			vm_map_unlock(map);
2485ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
24862db65ab4SAlan Cox 			    fictitious);
248712d7cc84SAlan Cox 			vm_map_lock(map);
2488a5db445dSMax Laier 			vm_map_unbusy(map);
248912d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
249012d7cc84SAlan Cox 				/*
249112d7cc84SAlan Cox 				 * Look again for the entry because the map was
249212d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
249312d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
249412d7cc84SAlan Cox 				 * deleted.
249512d7cc84SAlan Cox 				 */
249612d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
249712d7cc84SAlan Cox 				    &tmp_entry);
249812d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
249912d7cc84SAlan Cox 				if (entry == first_entry)
250012d7cc84SAlan Cox 					first_entry = tmp_entry;
250112d7cc84SAlan Cox 				else
250212d7cc84SAlan Cox 					first_entry = NULL;
250312d7cc84SAlan Cox 				entry = tmp_entry;
250428c58286SAlan Cox 				while (entry->end < saved_end) {
250528c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
250628c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
250728c58286SAlan Cox 						    ("vm_map_wire: bad count"));
250828c58286SAlan Cox 						entry->wired_count = -1;
250928c58286SAlan Cox 					}
251012d7cc84SAlan Cox 					entry = entry->next;
251112d7cc84SAlan Cox 				}
251228c58286SAlan Cox 			}
251312d7cc84SAlan Cox 			last_timestamp = map->timestamp;
251412d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
251528c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
251628c58286SAlan Cox 				    ("vm_map_wire: bad count"));
251712d7cc84SAlan Cox 				/*
251828c58286SAlan Cox 				 * Assign an out-of-range value to represent
251928c58286SAlan Cox 				 * the failure to wire this entry.
252012d7cc84SAlan Cox 				 */
252128c58286SAlan Cox 				entry->wired_count = -1;
252212d7cc84SAlan Cox 				end = entry->end;
252312d7cc84SAlan Cox 				goto done;
252412d7cc84SAlan Cox 			}
25250ada205eSBrian Feldman 		} else if (!user_wire ||
25260ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
25270ada205eSBrian Feldman 			entry->wired_count++;
252812d7cc84SAlan Cox 		}
252912d7cc84SAlan Cox 		/*
253012d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2531abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
253212d7cc84SAlan Cox 		 */
25336d7e8091SKonstantin Belousov 	next_entry:
2534abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2535abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2536abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
253712d7cc84SAlan Cox 			end = entry->end;
253812d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
253912d7cc84SAlan Cox 			goto done;
254012d7cc84SAlan Cox 		}
254112d7cc84SAlan Cox 		entry = entry->next;
254212d7cc84SAlan Cox 	}
254312d7cc84SAlan Cox 	rv = KERN_SUCCESS;
254412d7cc84SAlan Cox done:
254512d7cc84SAlan Cox 	need_wakeup = FALSE;
254612d7cc84SAlan Cox 	if (first_entry == NULL) {
254712d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2548cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2549cbef13d8SAlan Cox 			first_entry = first_entry->next;
2550cbef13d8SAlan Cox 		else
255112d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
255212d7cc84SAlan Cox 	}
25530acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
25540acea7dfSKonstantin Belousov 	    entry = entry->next) {
25556d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
25566d7e8091SKonstantin Belousov 			goto next_entry_done;
25570acea7dfSKonstantin Belousov 
25580acea7dfSKonstantin Belousov 		/*
25590acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
25600acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
25610acea7dfSKonstantin Belousov 		 * while the map lock was dropped for faulting in the
25620acea7dfSKonstantin Belousov 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
25630acea7dfSKonstantin Belousov 		 * Moreover, another thread could be simultaneously
25640acea7dfSKonstantin Belousov 		 * wiring this new mapping entry.  Detect these cases
25650acea7dfSKonstantin Belousov 		 * and skip any entries marked as in transition by us.
25660acea7dfSKonstantin Belousov 		 */
25670acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
25680acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
25690acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
25700acea7dfSKonstantin Belousov 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
25710acea7dfSKonstantin Belousov 			continue;
25720acea7dfSKonstantin Belousov 		}
25730acea7dfSKonstantin Belousov 
257412d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
257512d7cc84SAlan Cox 			if (user_wire)
257612d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
257728c58286SAlan Cox 		} else if (entry->wired_count == -1) {
257828c58286SAlan Cox 			/*
257928c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
258028c58286SAlan Cox 			 * unnecessary.
258128c58286SAlan Cox 			 */
258228c58286SAlan Cox 			entry->wired_count = 0;
258312d7cc84SAlan Cox 		} else {
25840ada205eSBrian Feldman 			if (!user_wire ||
25850ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
258612d7cc84SAlan Cox 				entry->wired_count--;
25870ada205eSBrian Feldman 			if (entry->wired_count == 0) {
258812d7cc84SAlan Cox 				/*
258912d7cc84SAlan Cox 				 * Retain the map lock.
259012d7cc84SAlan Cox 				 */
25914be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
25924be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
259328634820SAlan Cox 				    (entry->object.vm_object->flags &
259428634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
259512d7cc84SAlan Cox 			}
259612d7cc84SAlan Cox 		}
25976d7e8091SKonstantin Belousov 	next_entry_done:
25980acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
25990acea7dfSKonstantin Belousov 		    ("vm_map_wire: in-transition flag missing %p", entry));
26000acea7dfSKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
26010acea7dfSKonstantin Belousov 		    ("vm_map_wire: alien wire %p", entry));
26020acea7dfSKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
26030acea7dfSKonstantin Belousov 		    MAP_ENTRY_WIRE_SKIPPED);
26040acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
260512d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
260612d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
260712d7cc84SAlan Cox 			need_wakeup = TRUE;
260812d7cc84SAlan Cox 		}
260912d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
261012d7cc84SAlan Cox 	}
261112d7cc84SAlan Cox 	vm_map_unlock(map);
261212d7cc84SAlan Cox 	if (need_wakeup)
261312d7cc84SAlan Cox 		vm_map_wakeup(map);
261412d7cc84SAlan Cox 	return (rv);
2615e27e17b7SAlan Cox }
2616e27e17b7SAlan Cox 
2617e27e17b7SAlan Cox /*
2618950f8459SAlan Cox  * vm_map_sync
2619df8bae1dSRodney W. Grimes  *
2620df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2621df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2622df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2623df8bae1dSRodney W. Grimes  *
2624637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2625637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2626637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2627637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2628637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2629637315edSAlan Cox  * flushing the current region containing start.
2630637315edSAlan Cox  *
2631df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2632df8bae1dSRodney W. Grimes  */
2633df8bae1dSRodney W. Grimes int
2634950f8459SAlan Cox vm_map_sync(
26351b40f8c0SMatthew Dillon 	vm_map_t map,
26361b40f8c0SMatthew Dillon 	vm_offset_t start,
26371b40f8c0SMatthew Dillon 	vm_offset_t end,
26381b40f8c0SMatthew Dillon 	boolean_t syncio,
26391b40f8c0SMatthew Dillon 	boolean_t invalidate)
2640df8bae1dSRodney W. Grimes {
2641c0877f10SJohn Dyson 	vm_map_entry_t current;
2642df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2643df8bae1dSRodney W. Grimes 	vm_size_t size;
2644df8bae1dSRodney W. Grimes 	vm_object_t object;
2645a316d390SJohn Dyson 	vm_ooffset_t offset;
2646e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2647126d6082SKonstantin Belousov 	boolean_t failed;
2648df8bae1dSRodney W. Grimes 
2649df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2650df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2651df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2652df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2653df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2654637315edSAlan Cox 	} else if (start == end) {
2655637315edSAlan Cox 		start = entry->start;
2656637315edSAlan Cox 		end = entry->end;
2657df8bae1dSRodney W. Grimes 	}
2658df8bae1dSRodney W. Grimes 	/*
2659b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2660df8bae1dSRodney W. Grimes 	 */
26617b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
26627b0e72d1SAlan Cox 	    current = current->next) {
2663b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2664df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2665df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2666df8bae1dSRodney W. Grimes 		}
2667df8bae1dSRodney W. Grimes 		if (end > current->end &&
2668df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2669df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2670df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2671df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2672df8bae1dSRodney W. Grimes 		}
2673df8bae1dSRodney W. Grimes 	}
2674df8bae1dSRodney W. Grimes 
26752cf13952SAlan Cox 	if (invalidate)
2676bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2677126d6082SKonstantin Belousov 	failed = FALSE;
26782cf13952SAlan Cox 
2679df8bae1dSRodney W. Grimes 	/*
2680df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2681df8bae1dSRodney W. Grimes 	 * objects as we go.
2682df8bae1dSRodney W. Grimes 	 */
2683e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2684df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2685df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
26869fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2687c0877f10SJohn Dyson 			vm_map_t smap;
2688df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2689df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2690df8bae1dSRodney W. Grimes 
26919fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2692df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2693df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2694df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2695df8bae1dSRodney W. Grimes 			if (tsize < size)
2696df8bae1dSRodney W. Grimes 				size = tsize;
2697df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2698df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2699df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2700df8bae1dSRodney W. Grimes 		} else {
2701df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2702df8bae1dSRodney W. Grimes 		}
2703e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2704e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2705e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2706126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
2707126d6082SKonstantin Belousov 			failed = TRUE;
2708df8bae1dSRodney W. Grimes 		start += size;
2709e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2710e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2711e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2712e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2713e53fa61bSKonstantin Belousov 			current = current->next;
2714df8bae1dSRodney W. Grimes 	}
2715df8bae1dSRodney W. Grimes 
2716df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2717126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
2718df8bae1dSRodney W. Grimes }
2719df8bae1dSRodney W. Grimes 
2720df8bae1dSRodney W. Grimes /*
2721df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2722df8bae1dSRodney W. Grimes  *
2723df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2724df8bae1dSRodney W. Grimes  *
2725df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2726df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2727df8bae1dSRodney W. Grimes  */
27280362d7d7SJohn Dyson static void
27291b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2730df8bae1dSRodney W. Grimes {
27314be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
27324be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
273328634820SAlan Cox 	    (entry->object.vm_object->flags & OBJ_FICTITIOUS) != 0);
2734df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2735df8bae1dSRodney W. Grimes }
2736df8bae1dSRodney W. Grimes 
27370b367bd8SKonstantin Belousov static void
27380b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
27390b367bd8SKonstantin Belousov {
27400b367bd8SKonstantin Belousov 
27410b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
27420b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
27430b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
27440b367bd8SKonstantin Belousov }
27450b367bd8SKonstantin Belousov 
2746df8bae1dSRodney W. Grimes /*
2747df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2748df8bae1dSRodney W. Grimes  *
2749df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2750df8bae1dSRodney W. Grimes  */
27510362d7d7SJohn Dyson static void
27521b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2753df8bae1dSRodney W. Grimes {
275432a89c32SAlan Cox 	vm_object_t object;
27553364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
27563364c323SKonstantin Belousov 	vm_ooffset_t size;
275732a89c32SAlan Cox 
2758df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
27593364c323SKonstantin Belousov 	object = entry->object.vm_object;
27603364c323SKonstantin Belousov 	size = entry->end - entry->start;
27613364c323SKonstantin Belousov 	map->size -= size;
27623364c323SKonstantin Belousov 
2763ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
2764ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
2765ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
27663364c323SKonstantin Belousov 	}
2767df8bae1dSRodney W. Grimes 
276832a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
27693364c323SKonstantin Belousov 	    (object != NULL)) {
2770ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
27713364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2772ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
27733364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
277432a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
277532a89c32SAlan Cox 		offidxend = offidxstart + count;
277689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
277732a89c32SAlan Cox 		if (object->ref_count != 1 &&
277832a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
27799f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
278032a89c32SAlan Cox 			vm_object_collapse(object);
27816bbee8e2SAlan Cox 
27826bbee8e2SAlan Cox 			/*
27836bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
27846bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
27856bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
27866bbee8e2SAlan Cox 			 * of pages.
27876bbee8e2SAlan Cox 			 */
27886bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
27896bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
279032a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
279132a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
279232a89c32SAlan Cox 			if (offidxend >= object->size &&
27933364c323SKonstantin Belousov 			    offidxstart < object->size) {
27943364c323SKonstantin Belousov 				size1 = object->size;
279532a89c32SAlan Cox 				object->size = offidxstart;
2796ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
27973364c323SKonstantin Belousov 					size1 -= object->size;
27983364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
27993364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
2800ef694c1aSEdward Tomasz Napierala 					swap_release_by_cred(ptoa(size1), object->cred);
28013364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
28023364c323SKonstantin Belousov 				}
28033364c323SKonstantin Belousov 			}
280432a89c32SAlan Cox 		}
280589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2806897d81a0SKonstantin Belousov 	} else
2807897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
28080b367bd8SKonstantin Belousov 	if (map->system_map)
28090b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
28100b367bd8SKonstantin Belousov 	else {
28110b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
28120b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
28130b367bd8SKonstantin Belousov 	}
2814df8bae1dSRodney W. Grimes }
2815df8bae1dSRodney W. Grimes 
2816df8bae1dSRodney W. Grimes /*
2817df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2818df8bae1dSRodney W. Grimes  *
2819df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2820df8bae1dSRodney W. Grimes  *	map.
2821df8bae1dSRodney W. Grimes  */
2822df8bae1dSRodney W. Grimes int
2823655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2824df8bae1dSRodney W. Grimes {
2825c0877f10SJohn Dyson 	vm_map_entry_t entry;
2826df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2827df8bae1dSRodney W. Grimes 
28283a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
28293a0916b8SKonstantin Belousov 
2830df8bae1dSRodney W. Grimes 	/*
2831df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2832df8bae1dSRodney W. Grimes 	 */
2833876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2834df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2835876318ecSAlan Cox 	else {
2836df8bae1dSRodney W. Grimes 		entry = first_entry;
2837df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2838df8bae1dSRodney W. Grimes 	}
2839df8bae1dSRodney W. Grimes 
2840df8bae1dSRodney W. Grimes 	/*
2841df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2842df8bae1dSRodney W. Grimes 	 */
2843df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2844df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2845df8bae1dSRodney W. Grimes 
284673b2baceSAlan Cox 		/*
284773b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
28487c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
28497c938963SBrian Feldman 		 * user maps.
285073b2baceSAlan Cox 		 */
28517c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
28527c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
28537c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
285473b2baceSAlan Cox 			unsigned int last_timestamp;
285573b2baceSAlan Cox 			vm_offset_t saved_start;
285673b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
285773b2baceSAlan Cox 
285873b2baceSAlan Cox 			saved_start = entry->start;
285973b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
286073b2baceSAlan Cox 			last_timestamp = map->timestamp;
28618ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
286273b2baceSAlan Cox 			vm_map_lock(map);
286373b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
286473b2baceSAlan Cox 				/*
286573b2baceSAlan Cox 				 * Look again for the entry because the map was
286673b2baceSAlan Cox 				 * modified while it was unlocked.
286773b2baceSAlan Cox 				 * Specifically, the entry may have been
286873b2baceSAlan Cox 				 * clipped, merged, or deleted.
286973b2baceSAlan Cox 				 */
287073b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
287173b2baceSAlan Cox 							 &tmp_entry))
287273b2baceSAlan Cox 					entry = tmp_entry->next;
287373b2baceSAlan Cox 				else {
287473b2baceSAlan Cox 					entry = tmp_entry;
287573b2baceSAlan Cox 					vm_map_clip_start(map, entry,
287673b2baceSAlan Cox 							  saved_start);
287773b2baceSAlan Cox 				}
287873b2baceSAlan Cox 			}
287973b2baceSAlan Cox 			continue;
288073b2baceSAlan Cox 		}
2881df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2882df8bae1dSRodney W. Grimes 
2883c0877f10SJohn Dyson 		next = entry->next;
2884df8bae1dSRodney W. Grimes 
2885df8bae1dSRodney W. Grimes 		/*
28860d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
28870d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2888df8bae1dSRodney W. Grimes 		 */
2889c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2890df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2891c0877f10SJohn Dyson 		}
2892df8bae1dSRodney W. Grimes 
289332a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2894df8bae1dSRodney W. Grimes 
2895df8bae1dSRodney W. Grimes 		/*
2896e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2897e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2898e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2899e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2900df8bae1dSRodney W. Grimes 		 */
2901df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2902df8bae1dSRodney W. Grimes 		entry = next;
2903df8bae1dSRodney W. Grimes 	}
2904df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2905df8bae1dSRodney W. Grimes }
2906df8bae1dSRodney W. Grimes 
2907df8bae1dSRodney W. Grimes /*
2908df8bae1dSRodney W. Grimes  *	vm_map_remove:
2909df8bae1dSRodney W. Grimes  *
2910df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2911df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2912df8bae1dSRodney W. Grimes  */
2913df8bae1dSRodney W. Grimes int
29141b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2915df8bae1dSRodney W. Grimes {
29166eaee3feSAlan Cox 	int result;
2917df8bae1dSRodney W. Grimes 
2918df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2919df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2920655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
2921df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2922df8bae1dSRodney W. Grimes 	return (result);
2923df8bae1dSRodney W. Grimes }
2924df8bae1dSRodney W. Grimes 
2925df8bae1dSRodney W. Grimes /*
2926df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2927df8bae1dSRodney W. Grimes  *
29282d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
29292d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
29302d5c7e45SMatthew Dillon  *
29312d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
29322d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
29332d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
29342d5c7e45SMatthew Dillon  *
29352d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2936d8834602SAlan Cox  *
2937d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2938df8bae1dSRodney W. Grimes  */
29390d94caffSDavid Greenman boolean_t
2940b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2941b9dcd593SBruce Evans 			vm_prot_t protection)
2942df8bae1dSRodney W. Grimes {
2943c0877f10SJohn Dyson 	vm_map_entry_t entry;
2944df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2945df8bae1dSRodney W. Grimes 
2946d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2947df8bae1dSRodney W. Grimes 		return (FALSE);
2948df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2949df8bae1dSRodney W. Grimes 
2950df8bae1dSRodney W. Grimes 	while (start < end) {
2951d8834602SAlan Cox 		if (entry == &map->header)
2952df8bae1dSRodney W. Grimes 			return (FALSE);
2953df8bae1dSRodney W. Grimes 		/*
2954df8bae1dSRodney W. Grimes 		 * No holes allowed!
2955df8bae1dSRodney W. Grimes 		 */
2956d8834602SAlan Cox 		if (start < entry->start)
2957df8bae1dSRodney W. Grimes 			return (FALSE);
2958df8bae1dSRodney W. Grimes 		/*
2959df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2960df8bae1dSRodney W. Grimes 		 */
2961d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2962df8bae1dSRodney W. Grimes 			return (FALSE);
2963df8bae1dSRodney W. Grimes 		/* go to next entry */
2964df8bae1dSRodney W. Grimes 		start = entry->end;
2965df8bae1dSRodney W. Grimes 		entry = entry->next;
2966df8bae1dSRodney W. Grimes 	}
2967df8bae1dSRodney W. Grimes 	return (TRUE);
2968df8bae1dSRodney W. Grimes }
2969df8bae1dSRodney W. Grimes 
297086524867SJohn Dyson /*
2971df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2972df8bae1dSRodney W. Grimes  *
2973df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2974df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2975df8bae1dSRodney W. Grimes  */
2976f708ef1bSPoul-Henning Kamp static void
29771b40f8c0SMatthew Dillon vm_map_copy_entry(
29781b40f8c0SMatthew Dillon 	vm_map_t src_map,
29791b40f8c0SMatthew Dillon 	vm_map_t dst_map,
29801b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
29813364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
29823364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
2983df8bae1dSRodney W. Grimes {
2984c0877f10SJohn Dyson 	vm_object_t src_object;
298584110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
29863364c323SKonstantin Belousov 	vm_offset_t size;
2987ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
29883364c323SKonstantin Belousov 	int charged;
2989c0877f10SJohn Dyson 
29903a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
29913a0916b8SKonstantin Belousov 
29929fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2993df8bae1dSRodney W. Grimes 		return;
2994df8bae1dSRodney W. Grimes 
2995df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2996df8bae1dSRodney W. Grimes 
2997df8bae1dSRodney W. Grimes 		/*
29980d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
29990d94caffSDavid Greenman 		 * write-protected.
3000df8bae1dSRodney W. Grimes 		 */
3001afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
3002df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
3003df8bae1dSRodney W. Grimes 			    src_entry->start,
3004df8bae1dSRodney W. Grimes 			    src_entry->end,
3005df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
3006df8bae1dSRodney W. Grimes 		}
3007b18bfc3dSJohn Dyson 
3008df8bae1dSRodney W. Grimes 		/*
3009df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
3010df8bae1dSRodney W. Grimes 		 */
30113364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
30128aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
301389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(src_object);
30143364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
3015c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
3016c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
3017c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
3018c0877f10SJohn Dyson 				vm_object_collapse(src_object);
301996fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3020c5aaa06dSAlan Cox 					vm_object_split(src_entry);
3021c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
3022a89c6258SAlan Cox 				}
3023a89c6258SAlan Cox 			}
3024b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
3025069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3026ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL &&
30273364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3028ef694c1aSEdward Tomasz Napierala 				KASSERT(src_object->cred == NULL,
3029ef694c1aSEdward Tomasz Napierala 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
30303364c323SKonstantin Belousov 				     src_object));
3031ef694c1aSEdward Tomasz Napierala 				src_object->cred = src_entry->cred;
30323364c323SKonstantin Belousov 				src_object->charge = size;
30333364c323SKonstantin Belousov 			}
303489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(src_object);
3035c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
30363364c323SKonstantin Belousov 			if (charged) {
3037ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3038ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3039ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = cred;
30403364c323SKonstantin Belousov 				*fork_charge += size;
30413364c323SKonstantin Belousov 				if (!(src_entry->eflags &
30423364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
3043ef694c1aSEdward Tomasz Napierala 					crhold(cred);
3044ef694c1aSEdward Tomasz Napierala 					src_entry->cred = cred;
30453364c323SKonstantin Belousov 					*fork_charge += size;
30463364c323SKonstantin Belousov 				}
30473364c323SKonstantin Belousov 			}
3048afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3049afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3050b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
305184110e7eSKonstantin Belousov 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
305284110e7eSKonstantin Belousov 				/*
305384110e7eSKonstantin Belousov 				 * MAP_ENTRY_VN_WRITECNT cannot
305484110e7eSKonstantin Belousov 				 * indicate write reference from
305584110e7eSKonstantin Belousov 				 * src_entry, since the entry is
305684110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
305784110e7eSKonstantin Belousov 				 * fake entry that is used to
305884110e7eSKonstantin Belousov 				 * decrement object->un_pager.vnp.writecount
305984110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
306084110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
306184110e7eSKonstantin Belousov 				 */
306284110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
306384110e7eSKonstantin Belousov 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
306484110e7eSKonstantin Belousov 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
306584110e7eSKonstantin Belousov 				vm_object_reference(src_object);
306684110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
306784110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
306884110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
306984110e7eSKonstantin Belousov 				fake_entry->next = curthread->td_map_def_user;
307084110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
307184110e7eSKonstantin Belousov 			}
3072b18bfc3dSJohn Dyson 		} else {
3073b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
3074b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
3075ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
3076ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
3077ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
30783364c323SKonstantin Belousov 				*fork_charge += size;
30793364c323SKonstantin Belousov 			}
3080b18bfc3dSJohn Dyson 		}
3081df8bae1dSRodney W. Grimes 
3082df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3083df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
30840d94caffSDavid Greenman 	} else {
3085df8bae1dSRodney W. Grimes 		/*
3086df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
30870d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
30880d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
3089df8bae1dSRodney W. Grimes 		 */
3090121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3091121fd461SKonstantin Belousov 		    fork_charge);
3092df8bae1dSRodney W. Grimes 	}
3093df8bae1dSRodney W. Grimes }
3094df8bae1dSRodney W. Grimes 
3095df8bae1dSRodney W. Grimes /*
30962a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
30972a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
30982a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
30992a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
31002a7be1b6SBrian Feldman  */
31012a7be1b6SBrian Feldman static void
31022a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
31032a7be1b6SBrian Feldman     vm_map_entry_t entry)
31042a7be1b6SBrian Feldman {
31052a7be1b6SBrian Feldman 	vm_size_t entrysize;
31062a7be1b6SBrian Feldman 	vm_offset_t newend;
31072a7be1b6SBrian Feldman 
31082a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
31092a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
31102a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
31112a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
31122a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
31132a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3114b351299cSAndrew Gallatin 		newend = MIN(entry->end,
31152a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
31162a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
31172a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
31182a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3119b351299cSAndrew Gallatin 		newend = MIN(entry->end,
31202a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
31212a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
31222a7be1b6SBrian Feldman 	}
31232a7be1b6SBrian Feldman }
31242a7be1b6SBrian Feldman 
31252a7be1b6SBrian Feldman /*
3126df8bae1dSRodney W. Grimes  * vmspace_fork:
3127df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
3128df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
3129df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
3130df8bae1dSRodney W. Grimes  * values on the regions in that map.
3131df8bae1dSRodney W. Grimes  *
31322a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
31332a7be1b6SBrian Feldman  *
3134df8bae1dSRodney W. Grimes  * The source map must not be locked.
3135df8bae1dSRodney W. Grimes  */
3136df8bae1dSRodney W. Grimes struct vmspace *
31373364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3138df8bae1dSRodney W. Grimes {
3139c0877f10SJohn Dyson 	struct vmspace *vm2;
314079e53838SAlan Cox 	vm_map_t new_map, old_map;
314179e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
3142de5f6a77SJohn Dyson 	vm_object_t object;
31431fac7d7fSKonstantin Belousov 	int locked;
3144df8bae1dSRodney W. Grimes 
314579e53838SAlan Cox 	old_map = &vm1->vm_map;
314679e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
31472d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
314889b57fcfSKonstantin Belousov 	if (vm2 == NULL)
314979e53838SAlan Cox 		return (NULL);
31502a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
31512a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
31522a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
315379e53838SAlan Cox 	vm_map_lock(old_map);
315479e53838SAlan Cox 	if (old_map->busy)
315579e53838SAlan Cox 		vm_map_wait_busy(old_map);
315679e53838SAlan Cox 	new_map = &vm2->vm_map;
31571fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
31581fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
3159df8bae1dSRodney W. Grimes 
3160df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3161df8bae1dSRodney W. Grimes 
3162df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3163afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3164df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3165df8bae1dSRodney W. Grimes 
3166df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3167df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3168df8bae1dSRodney W. Grimes 			break;
3169df8bae1dSRodney W. Grimes 
3170df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3171df8bae1dSRodney W. Grimes 			/*
3172fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3173fed9a903SJohn Dyson 			 */
3174fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3175fed9a903SJohn Dyson 			if (object == NULL) {
3176fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3177c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3178fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
317915d2d313SAlan Cox 				old_entry->offset = 0;
3180ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
3181ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
31823364c323SKonstantin Belousov 					object->charge = old_entry->end -
31833364c323SKonstantin Belousov 					    old_entry->start;
3184ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
31853364c323SKonstantin Belousov 				}
31869a2f6362SAlan Cox 			}
31879a2f6362SAlan Cox 
31889a2f6362SAlan Cox 			/*
31899a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
31909a2f6362SAlan Cox 			 * to insure that a shadow object is created.
31919a2f6362SAlan Cox 			 */
31929a2f6362SAlan Cox 			vm_object_reference(object);
31939a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
31945069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
31955069bf57SJohn Dyson 				    &old_entry->offset,
31960cc74f14SAlan Cox 				    old_entry->end - old_entry->start);
31975069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3198d30344bdSIan Dowse 				/* Transfer the second reference too. */
3199d30344bdSIan Dowse 				vm_object_reference(
3200d30344bdSIan Dowse 				    old_entry->object.vm_object);
32017fd10fb3SKonstantin Belousov 
32027fd10fb3SKonstantin Belousov 				/*
32037fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3204b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
32057fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
32067fd10fb3SKonstantin Belousov 				 */
3207d30344bdSIan Dowse 				vm_object_deallocate(object);
32085069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3209fed9a903SJohn Dyson 			}
321089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3211069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3212ef694c1aSEdward Tomasz Napierala 			if (old_entry->cred != NULL) {
3213ef694c1aSEdward Tomasz Napierala 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3214ef694c1aSEdward Tomasz Napierala 				object->cred = old_entry->cred;
32153364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
3216ef694c1aSEdward Tomasz Napierala 				old_entry->cred = NULL;
32173364c323SKonstantin Belousov 			}
3218b9781cf6SKonstantin Belousov 
3219b9781cf6SKonstantin Belousov 			/*
3220b9781cf6SKonstantin Belousov 			 * Assert the correct state of the vnode
3221b9781cf6SKonstantin Belousov 			 * v_writecount while the object is locked, to
3222b9781cf6SKonstantin Belousov 			 * not relock it later for the assertion
3223b9781cf6SKonstantin Belousov 			 * correctness.
3224b9781cf6SKonstantin Belousov 			 */
3225b9781cf6SKonstantin Belousov 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3226b9781cf6SKonstantin Belousov 			    object->type == OBJT_VNODE) {
3227b9781cf6SKonstantin Belousov 				KASSERT(((struct vnode *)object->handle)->
3228b9781cf6SKonstantin Belousov 				    v_writecount > 0,
3229b9781cf6SKonstantin Belousov 				    ("vmspace_fork: v_writecount %p", object));
3230b9781cf6SKonstantin Belousov 				KASSERT(object->un_pager.vnp.writemappings > 0,
3231b9781cf6SKonstantin Belousov 				    ("vmspace_fork: vnp.writecount %p",
3232b9781cf6SKonstantin Belousov 				    object));
3233b9781cf6SKonstantin Belousov 			}
323489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
3235fed9a903SJohn Dyson 
3236fed9a903SJohn Dyson 			/*
3237ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3238df8bae1dSRodney W. Grimes 			 */
3239df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3240df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
32419f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
32429f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
32430acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3244df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
324584110e7eSKonstantin Belousov 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
324684110e7eSKonstantin Belousov 				vnode_pager_update_writecount(object,
324784110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
324884110e7eSKonstantin Belousov 			}
3249df8bae1dSRodney W. Grimes 
3250df8bae1dSRodney W. Grimes 			/*
32510d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
32520d94caffSDavid Greenman 			 * inserting at the end of the new map.
3253df8bae1dSRodney W. Grimes 			 */
3254df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3255df8bae1dSRodney W. Grimes 			    new_entry);
32562a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3257df8bae1dSRodney W. Grimes 
3258df8bae1dSRodney W. Grimes 			/*
3259df8bae1dSRodney W. Grimes 			 * Update the physical map
3260df8bae1dSRodney W. Grimes 			 */
3261df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3262df8bae1dSRodney W. Grimes 			    new_entry->start,
3263df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3264df8bae1dSRodney W. Grimes 			    old_entry->start);
3265df8bae1dSRodney W. Grimes 			break;
3266df8bae1dSRodney W. Grimes 
3267df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3268df8bae1dSRodney W. Grimes 			/*
3269df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3270df8bae1dSRodney W. Grimes 			 */
3271df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3272df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
327384110e7eSKonstantin Belousov 			/*
327484110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
327584110e7eSKonstantin Belousov 			 */
32769f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
327784110e7eSKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
32780acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3279df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3280df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3281ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
3282df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3283df8bae1dSRodney W. Grimes 			    new_entry);
32842a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3285bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
32863364c323SKonstantin Belousov 			    new_entry, fork_charge);
3287df8bae1dSRodney W. Grimes 			break;
3288df8bae1dSRodney W. Grimes 		}
3289df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3290df8bae1dSRodney W. Grimes 	}
329184110e7eSKonstantin Belousov 	/*
329284110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
329384110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
329484110e7eSKonstantin Belousov 	 * new_map locks are released.
329584110e7eSKonstantin Belousov 	 */
329684110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
329784110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
329884110e7eSKonstantin Belousov 	vm_map_process_deferred();
3299df8bae1dSRodney W. Grimes 
3300df8bae1dSRodney W. Grimes 	return (vm2);
3301df8bae1dSRodney W. Grimes }
3302df8bae1dSRodney W. Grimes 
330394f7e29aSAlan Cox int
330494f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
330594f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
330694f7e29aSAlan Cox {
3307fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3308fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
3309cfe52ecfSAndrey Zonov 	vm_size_t growsize, init_ssize;
3310fd75d710SMarcel Moolenaar 	int orient, rv;
33117e19eda4SAndrey Zonov 	rlim_t lmemlim, vmemlim;
331294f7e29aSAlan Cox 
3313fd75d710SMarcel Moolenaar 	/*
3314fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3315fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3316fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3317fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3318fd75d710SMarcel Moolenaar 	 */
3319fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3320fd75d710SMarcel Moolenaar 	cow &= ~orient;
3321fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3322fd75d710SMarcel Moolenaar 
332377bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
332477bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
332577bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
332694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3327fd75d710SMarcel Moolenaar 
3328cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3329cfe52ecfSAndrey Zonov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
333094f7e29aSAlan Cox 
33317e19eda4SAndrey Zonov 	PROC_LOCK(curproc);
33327e19eda4SAndrey Zonov 	lmemlim = lim_cur(curproc, RLIMIT_MEMLOCK);
33337e19eda4SAndrey Zonov 	vmemlim = lim_cur(curproc, RLIMIT_VMEM);
33347e19eda4SAndrey Zonov 	PROC_UNLOCK(curproc);
333591d5354aSJohn Baldwin 
333694f7e29aSAlan Cox 	vm_map_lock(map);
333794f7e29aSAlan Cox 
333894f7e29aSAlan Cox 	/* If addr is already mapped, no go */
333994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
334094f7e29aSAlan Cox 		vm_map_unlock(map);
334194f7e29aSAlan Cox 		return (KERN_NO_SPACE);
334294f7e29aSAlan Cox 	}
334394f7e29aSAlan Cox 
33447e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
33453ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + init_ssize > lmemlim) {
33467e19eda4SAndrey Zonov 			vm_map_unlock(map);
33477e19eda4SAndrey Zonov 			return (KERN_NO_SPACE);
33487e19eda4SAndrey Zonov 		}
33497e19eda4SAndrey Zonov 	}
33507e19eda4SAndrey Zonov 
3351a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
335291d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
3353a69ac174SMatthew Dillon 		vm_map_unlock(map);
3354a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3355a69ac174SMatthew Dillon 	}
3356a69ac174SMatthew Dillon 
3357fd75d710SMarcel Moolenaar 	/*
3358fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3359fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3360fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3361fd75d710SMarcel Moolenaar 	 * space is not protected.
336294f7e29aSAlan Cox 	 *
3363fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3364fd75d710SMarcel Moolenaar 	 * grow the stack.
336594f7e29aSAlan Cox 	 */
336694f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
336794f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
336894f7e29aSAlan Cox 		vm_map_unlock(map);
336994f7e29aSAlan Cox 		return (KERN_NO_SPACE);
337094f7e29aSAlan Cox 	}
337194f7e29aSAlan Cox 
3372fd75d710SMarcel Moolenaar 	/*
3373fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3374fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3375fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3376fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
337794f7e29aSAlan Cox 	 *
3378fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3379fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3380fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
338194f7e29aSAlan Cox 	 */
3382fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3383fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3384fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3385fd75d710SMarcel Moolenaar 		bot = addrbos;
3386fd75d710SMarcel Moolenaar 	else
3387fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3388fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3389fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
339094f7e29aSAlan Cox 
3391fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
339294f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
339329b45e9eSAlan Cox 		if (prev_entry != &map->header)
3394fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
3395fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3396fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
339794f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3398b21a0008SMarcel Moolenaar 
3399fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3400fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
3401fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3402fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
3403fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
340494f7e29aSAlan Cox 	}
340594f7e29aSAlan Cox 
340694f7e29aSAlan Cox 	vm_map_unlock(map);
340794f7e29aSAlan Cox 	return (rv);
340894f7e29aSAlan Cox }
340994f7e29aSAlan Cox 
34109a6d144fSKonstantin Belousov static int stack_guard_page = 0;
34119a6d144fSKonstantin Belousov TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page);
34129a6d144fSKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW,
34139a6d144fSKonstantin Belousov     &stack_guard_page, 0,
34149a6d144fSKonstantin Belousov     "Insert stack guard page ahead of the growable segments.");
34159a6d144fSKonstantin Belousov 
341694f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
341794f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
341894f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
341994f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
342094f7e29aSAlan Cox  * the grow function in vm_machdep.c).
342194f7e29aSAlan Cox  */
342294f7e29aSAlan Cox int
342394f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
342494f7e29aSAlan Cox {
3425b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3426b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
342794f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
342894f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
342994f7e29aSAlan Cox 	vm_offset_t end;
3430cfe52ecfSAndrey Zonov 	vm_size_t growsize;
3431b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
34327e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
3433b21a0008SMarcel Moolenaar 	int is_procstack, rv;
3434ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
34351ba5ad42SEdward Tomasz Napierala #ifdef notyet
34361ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
34371ba5ad42SEdward Tomasz Napierala #endif
3438afcc55f3SEdward Tomasz Napierala #ifdef RACCT
34391ba5ad42SEdward Tomasz Napierala 	int error;
3440afcc55f3SEdward Tomasz Napierala #endif
344123955314SAlfred Perlstein 
344294f7e29aSAlan Cox Retry:
344391d5354aSJohn Baldwin 	PROC_LOCK(p);
34447e19eda4SAndrey Zonov 	lmemlim = lim_cur(p, RLIMIT_MEMLOCK);
344591d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3446bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
344791d5354aSJohn Baldwin 	PROC_UNLOCK(p);
344891d5354aSJohn Baldwin 
344994f7e29aSAlan Cox 	vm_map_lock_read(map);
345094f7e29aSAlan Cox 
345194f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
345294f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
345394f7e29aSAlan Cox 		vm_map_unlock_read(map);
34540cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
345594f7e29aSAlan Cox 	}
345694f7e29aSAlan Cox 
3457b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3458b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3459b21a0008SMarcel Moolenaar 		/*
3460b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3461b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3462b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3463b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
346494f7e29aSAlan Cox 		 */
3465b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
346694f7e29aSAlan Cox 			vm_map_unlock_read(map);
34670cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
346894f7e29aSAlan Cox 		}
3469b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3470b21a0008SMarcel Moolenaar 	} else {
3471b21a0008SMarcel Moolenaar 		/*
3472b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3473b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3474b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3475b21a0008SMarcel Moolenaar 		 * select one.
3476b21a0008SMarcel Moolenaar 		 */
3477b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3478b21a0008SMarcel Moolenaar 			/*
3479b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3480b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3481b21a0008SMarcel Moolenaar 			 */
3482b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3483b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3484b21a0008SMarcel Moolenaar 			else
3485b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3486b21a0008SMarcel Moolenaar 		} else
3487b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3488b21a0008SMarcel Moolenaar 	}
348994f7e29aSAlan Cox 
3490b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3491b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3492b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3493b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3494b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
349594f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3496b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3497b21a0008SMarcel Moolenaar 	} else {
3498b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
349908667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3500b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3501b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3502fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3503b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3504b21a0008SMarcel Moolenaar 	}
3505b21a0008SMarcel Moolenaar 
350694f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
350794f7e29aSAlan Cox 		vm_map_unlock_read(map);
35080cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
350994f7e29aSAlan Cox 	}
351094f7e29aSAlan Cox 
3511b21a0008SMarcel Moolenaar 	/*
3512b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3513b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3514b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3515b21a0008SMarcel Moolenaar 	 * and is probably an error.
351694f7e29aSAlan Cox 	 *
3517b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3518b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
351994f7e29aSAlan Cox 	 */
35209a6d144fSKonstantin Belousov 	if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
352125adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
352294f7e29aSAlan Cox 			goto Retry;
352394f7e29aSAlan Cox 
3524b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
352594f7e29aSAlan Cox 
352694f7e29aSAlan Cox 		vm_map_unlock(map);
35270cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
352894f7e29aSAlan Cox 	}
352994f7e29aSAlan Cox 
3530b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
353194f7e29aSAlan Cox 
3532b21a0008SMarcel Moolenaar 	/*
3533b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3534b21a0008SMarcel Moolenaar 	 * limit.
353594f7e29aSAlan Cox 	 */
353691d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
353794f7e29aSAlan Cox 		vm_map_unlock_read(map);
35380cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
353994f7e29aSAlan Cox 	}
3540afcc55f3SEdward Tomasz Napierala #ifdef RACCT
35411ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
35421ba5ad42SEdward Tomasz Napierala 	if (is_procstack &&
35431ba5ad42SEdward Tomasz Napierala 	    racct_set(p, RACCT_STACK, ctob(vm->vm_ssize) + grow_amount)) {
35441ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
35451ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
35461ba5ad42SEdward Tomasz Napierala 		return (KERN_NO_SPACE);
35471ba5ad42SEdward Tomasz Napierala 	}
35481ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3549afcc55f3SEdward Tomasz Napierala #endif
355094f7e29aSAlan Cox 
3551cfe52ecfSAndrey Zonov 	/* Round up the grow amount modulo sgrowsiz */
3552cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3553cfe52ecfSAndrey Zonov 	grow_amount = roundup(grow_amount, growsize);
3554b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
355594f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
355691d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3557e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
3558e4826248SAlan Cox 		    ctob(vm->vm_ssize);
355994f7e29aSAlan Cox 	}
35601ba5ad42SEdward Tomasz Napierala #ifdef notyet
35611ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
35621ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
35631ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
35641ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
35651ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
35661ba5ad42SEdward Tomasz Napierala #endif
35677e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
35683ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
35697e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
35707e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
35717e19eda4SAndrey Zonov 			goto out;
35727e19eda4SAndrey Zonov 		}
35737e19eda4SAndrey Zonov #ifdef RACCT
35747e19eda4SAndrey Zonov 		PROC_LOCK(p);
35757e19eda4SAndrey Zonov 		if (racct_set(p, RACCT_MEMLOCK,
35763ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
35777e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
35787e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
35797e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
35807e19eda4SAndrey Zonov 			goto out;
35817e19eda4SAndrey Zonov 		}
35827e19eda4SAndrey Zonov 		PROC_UNLOCK(p);
35837e19eda4SAndrey Zonov #endif
35847e19eda4SAndrey Zonov 	}
3585a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
358691d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3587a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
35881ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
35891ba5ad42SEdward Tomasz Napierala 		goto out;
3590a69ac174SMatthew Dillon 	}
3591afcc55f3SEdward Tomasz Napierala #ifdef RACCT
35921ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
35931ba5ad42SEdward Tomasz Napierala 	if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
35941ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
35951ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
35961ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
35971ba5ad42SEdward Tomasz Napierala 		goto out;
35981ba5ad42SEdward Tomasz Napierala 	}
35991ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3600afcc55f3SEdward Tomasz Napierala #endif
3601a69ac174SMatthew Dillon 
360225adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
360394f7e29aSAlan Cox 		goto Retry;
360494f7e29aSAlan Cox 
3605b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3606b21a0008SMarcel Moolenaar 		/*
3607b21a0008SMarcel Moolenaar 		 * Growing downward.
3608b21a0008SMarcel Moolenaar 		 */
360994f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
361094f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
361194f7e29aSAlan Cox 
3612b21a0008SMarcel Moolenaar 		/*
3613b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3614b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
361594f7e29aSAlan Cox 		 */
361694f7e29aSAlan Cox 		if (addr < end) {
3617b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
361894f7e29aSAlan Cox 			addr = end;
36199a6d144fSKonstantin Belousov 			if (stack_guard_page)
36209a6d144fSKonstantin Belousov 				addr += PAGE_SIZE;
362194f7e29aSAlan Cox 		}
362294f7e29aSAlan Cox 
362394f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
362483ce0853SKonstantin Belousov 		    next_entry->protection, next_entry->max_protection, 0);
362594f7e29aSAlan Cox 
362694f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
362794f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
362829b45e9eSAlan Cox 			if (prev_entry != &map->header)
362929b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3630b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3631b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3632b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3633b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3634b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3635b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3636b21a0008SMarcel Moolenaar 			    grow_amount;
3637b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3638b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
363994f7e29aSAlan Cox 		}
3640b21a0008SMarcel Moolenaar 	} else {
3641b21a0008SMarcel Moolenaar 		/*
3642b21a0008SMarcel Moolenaar 		 * Growing upward.
3643b21a0008SMarcel Moolenaar 		 */
3644b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3645b21a0008SMarcel Moolenaar 
3646b21a0008SMarcel Moolenaar 		/*
3647b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3648b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3649b21a0008SMarcel Moolenaar 		 */
3650b21a0008SMarcel Moolenaar 		if (addr > end) {
3651b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3652b21a0008SMarcel Moolenaar 			addr = end;
36539a6d144fSKonstantin Belousov 			if (stack_guard_page)
36549a6d144fSKonstantin Belousov 				addr -= PAGE_SIZE;
365594f7e29aSAlan Cox 		}
365694f7e29aSAlan Cox 
3657b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
3658ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
3659ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
3660ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
3661ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
36623364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3663b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
36643364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3665b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
366657a21abaSAlan Cox 			 stack_entry->offset,
3667b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
3668ef694c1aSEdward Tomasz Napierala 			 (vm_size_t)grow_amount, cred != NULL)) {
366908667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3670b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3671b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3672199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
36730164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3674b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3675b21a0008SMarcel Moolenaar 
3676b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3677b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3678b21a0008SMarcel Moolenaar 		} else
3679b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3680b21a0008SMarcel Moolenaar 	}
3681b21a0008SMarcel Moolenaar 
3682b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3683b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3684b21a0008SMarcel Moolenaar 
368594f7e29aSAlan Cox 	vm_map_unlock(map);
3686b21a0008SMarcel Moolenaar 
3687abd498aaSBruce M Simpson 	/*
3688abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3689abd498aaSBruce M Simpson 	 */
3690b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3691b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3692b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3693b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3694b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3695b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3696b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3697b21a0008SMarcel Moolenaar 	}
3698abd498aaSBruce M Simpson 
36991ba5ad42SEdward Tomasz Napierala out:
3700afcc55f3SEdward Tomasz Napierala #ifdef RACCT
37011ba5ad42SEdward Tomasz Napierala 	if (rv != KERN_SUCCESS) {
37021ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
37031ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
37041ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
37057e19eda4SAndrey Zonov 		if (!old_mlock) {
37067e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
37073ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
37087e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
37097e19eda4SAndrey Zonov 		}
37101ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
37111ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
37121ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
37131ba5ad42SEdward Tomasz Napierala 	}
3714afcc55f3SEdward Tomasz Napierala #endif
37151ba5ad42SEdward Tomasz Napierala 
37160cddd8f0SMatthew Dillon 	return (rv);
371794f7e29aSAlan Cox }
371894f7e29aSAlan Cox 
3719df8bae1dSRodney W. Grimes /*
37205856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
37215856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
37225856e12eSJohn Dyson  */
372389b57fcfSKonstantin Belousov int
37243ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
37251b40f8c0SMatthew Dillon {
37265856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
37275856e12eSJohn Dyson 	struct vmspace *newvmspace;
37285856e12eSJohn Dyson 
37293ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
373089b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
373189b57fcfSKonstantin Belousov 		return (ENOMEM);
373251ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
37335856e12eSJohn Dyson 	/*
37345856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
37355856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
37365856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
37375856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
37385856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
37395856e12eSJohn Dyson 	 */
374057051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
37415856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
374257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37436617724cSJeff Roberson 	if (p == curthread->td_proc)
3744b40ce416SJulian Elischer 		pmap_activate(curthread);
3745b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
374689b57fcfSKonstantin Belousov 	return (0);
37475856e12eSJohn Dyson }
37485856e12eSJohn Dyson 
37495856e12eSJohn Dyson /*
37505856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
37515856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
37525856e12eSJohn Dyson  */
375389b57fcfSKonstantin Belousov int
37541b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
37551b40f8c0SMatthew Dillon {
37565856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
37575856e12eSJohn Dyson 	struct vmspace *newvmspace;
37583364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
37595856e12eSJohn Dyson 
37605856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
376189b57fcfSKonstantin Belousov 		return (0);
37623364c323SKonstantin Belousov 	fork_charge = 0;
37633364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
376489b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
376589b57fcfSKonstantin Belousov 		return (ENOMEM);
3766ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
37673364c323SKonstantin Belousov 		vmspace_free(newvmspace);
37683364c323SKonstantin Belousov 		return (ENOMEM);
37693364c323SKonstantin Belousov 	}
377057051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
37715856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
377257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37736617724cSJeff Roberson 	if (p == curthread->td_proc)
3774b40ce416SJulian Elischer 		pmap_activate(curthread);
3775b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
377689b57fcfSKonstantin Belousov 	return (0);
37775856e12eSJohn Dyson }
37785856e12eSJohn Dyson 
37795856e12eSJohn Dyson /*
3780df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3781df8bae1dSRodney W. Grimes  *
3782df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3783df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3784df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3785df8bae1dSRodney W. Grimes  *	type specified.
3786df8bae1dSRodney W. Grimes  *
3787df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3788df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3789df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3790df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3791df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3792df8bae1dSRodney W. Grimes  *
3793df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3794df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3795df8bae1dSRodney W. Grimes  *
3796df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3797df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3798df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3799df8bae1dSRodney W. Grimes  *	remain the same.
3800df8bae1dSRodney W. Grimes  */
3801df8bae1dSRodney W. Grimes int
3802b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3803b9dcd593SBruce Evans 	      vm_offset_t vaddr,
380447221757SJohn Dyson 	      vm_prot_t fault_typea,
3805b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3806b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3807b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3808b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
38092d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3810df8bae1dSRodney W. Grimes {
3811c0877f10SJohn Dyson 	vm_map_entry_t entry;
3812c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3813c0877f10SJohn Dyson 	vm_prot_t prot;
381447221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
38153364c323SKonstantin Belousov 	vm_object_t eobject;
38160cc74f14SAlan Cox 	vm_size_t size;
3817ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
3818df8bae1dSRodney W. Grimes 
3819df8bae1dSRodney W. Grimes RetryLookup:;
3820df8bae1dSRodney W. Grimes 
3821df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3822df8bae1dSRodney W. Grimes 
3823df8bae1dSRodney W. Grimes 	/*
38244c3ef59eSAlan Cox 	 * Lookup the faulting address.
3825df8bae1dSRodney W. Grimes 	 */
3826095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3827095104acSAlan Cox 		vm_map_unlock_read(map);
3828095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3829095104acSAlan Cox 	}
3830df8bae1dSRodney W. Grimes 
38314e94f402SAlan Cox 	entry = *out_entry;
3832b7b2aac2SJohn Dyson 
3833df8bae1dSRodney W. Grimes 	/*
3834df8bae1dSRodney W. Grimes 	 * Handle submaps.
3835df8bae1dSRodney W. Grimes 	 */
3836afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3837df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3838df8bae1dSRodney W. Grimes 
3839df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3840df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3841df8bae1dSRodney W. Grimes 		goto RetryLookup;
3842df8bae1dSRodney W. Grimes 	}
3843a04c970aSJohn Dyson 
3844df8bae1dSRodney W. Grimes 	/*
38450d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3846df8bae1dSRodney W. Grimes 	 */
3847df8bae1dSRodney W. Grimes 	prot = entry->protection;
384847221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
38492db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3850095104acSAlan Cox 		vm_map_unlock_read(map);
3851095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
385247221757SJohn Dyson 	}
38532ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
385447221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3855a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3856095104acSAlan Cox 		vm_map_unlock_read(map);
3857095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3858a04c970aSJohn Dyson 	}
38595b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
38605b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
38615b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
38625b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
38635b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
38645b3e0257SDag-Erling Smørgrav 	}
3865df8bae1dSRodney W. Grimes 
3866df8bae1dSRodney W. Grimes 	/*
38670d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
38680d94caffSDavid Greenman 	 * accesses.
3869df8bae1dSRodney W. Grimes 	 */
387005f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
387105f0fdd2SPoul-Henning Kamp 	if (*wired)
3872a6d42a0dSAlan Cox 		fault_type = entry->protection;
38733364c323SKonstantin Belousov 	size = entry->end - entry->start;
3874df8bae1dSRodney W. Grimes 	/*
3875df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3876df8bae1dSRodney W. Grimes 	 */
3877afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3878df8bae1dSRodney W. Grimes 		/*
38790d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3880ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3881df8bae1dSRodney W. Grimes 		 *
38820d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
38830d94caffSDavid Greenman 		 * permissions allowed.
3884df8bae1dSRodney W. Grimes 		 */
3885a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3886a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
3887df8bae1dSRodney W. Grimes 			/*
38880d94caffSDavid Greenman 			 * Make a new object, and place it in the object
38890d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3890ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
38910d94caffSDavid Greenman 			 * object.
3892df8bae1dSRodney W. Grimes 			 */
389325adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3894df8bae1dSRodney W. Grimes 				goto RetryLookup;
38959917e010SAlan Cox 
3896ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
38973364c323SKonstantin Belousov 				/*
38983364c323SKonstantin Belousov 				 * The debugger owner is charged for
38993364c323SKonstantin Belousov 				 * the memory.
39003364c323SKonstantin Belousov 				 */
3901ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3902ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3903ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
3904ef694c1aSEdward Tomasz Napierala 					crfree(cred);
39053364c323SKonstantin Belousov 					vm_map_unlock(map);
39063364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
39073364c323SKonstantin Belousov 				}
3908ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
39093364c323SKonstantin Belousov 			}
39100cc74f14SAlan Cox 			vm_object_shadow(&entry->object.vm_object,
39110cc74f14SAlan Cox 			    &entry->offset, size);
3912afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
39133364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
3914ef694c1aSEdward Tomasz Napierala 			if (eobject->cred != NULL) {
39153364c323SKonstantin Belousov 				/*
39163364c323SKonstantin Belousov 				 * The object was not shadowed.
39173364c323SKonstantin Belousov 				 */
3918ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
3919ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
3920ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
3921ef694c1aSEdward Tomasz Napierala 			} else if (entry->cred != NULL) {
392289f6b863SAttilio Rao 				VM_OBJECT_WLOCK(eobject);
3923ef694c1aSEdward Tomasz Napierala 				eobject->cred = entry->cred;
39243364c323SKonstantin Belousov 				eobject->charge = size;
392589f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(eobject);
3926ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
39273364c323SKonstantin Belousov 			}
39289917e010SAlan Cox 
39299b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
39300d94caffSDavid Greenman 		} else {
3931df8bae1dSRodney W. Grimes 			/*
39320d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
39330d94caffSDavid Greenman 			 * don't allow writes.
3934df8bae1dSRodney W. Grimes 			 */
39352d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3936df8bae1dSRodney W. Grimes 		}
3937df8bae1dSRodney W. Grimes 	}
39382d8acc0fSJohn Dyson 
3939df8bae1dSRodney W. Grimes 	/*
3940df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3941df8bae1dSRodney W. Grimes 	 */
39424e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
39434e71e795SMatthew Dillon 	    !map->system_map) {
394425adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3945df8bae1dSRodney W. Grimes 			goto RetryLookup;
394624a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
39473364c323SKonstantin Belousov 		    atop(size));
3948df8bae1dSRodney W. Grimes 		entry->offset = 0;
3949ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
395089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(entry->object.vm_object);
3951ef694c1aSEdward Tomasz Napierala 			entry->object.vm_object->cred = entry->cred;
39523364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
395389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
3954ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
39553364c323SKonstantin Belousov 		}
39569b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3957df8bae1dSRodney W. Grimes 	}
3958b5b40fa6SJohn Dyson 
3959df8bae1dSRodney W. Grimes 	/*
39600d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
39610d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3962df8bae1dSRodney W. Grimes 	 */
39639b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3964df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3965df8bae1dSRodney W. Grimes 
3966df8bae1dSRodney W. Grimes 	*out_prot = prot;
3967df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3968df8bae1dSRodney W. Grimes }
3969df8bae1dSRodney W. Grimes 
3970df8bae1dSRodney W. Grimes /*
397119dc5607STor Egge  *	vm_map_lookup_locked:
397219dc5607STor Egge  *
397319dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
397419dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
397519dc5607STor Egge  */
397619dc5607STor Egge int
397719dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
397819dc5607STor Egge 		     vm_offset_t vaddr,
397919dc5607STor Egge 		     vm_prot_t fault_typea,
398019dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
398119dc5607STor Egge 		     vm_object_t *object,	/* OUT */
398219dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
398319dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
398419dc5607STor Egge 		     boolean_t *wired)		/* OUT */
398519dc5607STor Egge {
398619dc5607STor Egge 	vm_map_entry_t entry;
398719dc5607STor Egge 	vm_map_t map = *var_map;
398819dc5607STor Egge 	vm_prot_t prot;
398919dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
399019dc5607STor Egge 
399119dc5607STor Egge 	/*
39924c3ef59eSAlan Cox 	 * Lookup the faulting address.
399319dc5607STor Egge 	 */
399419dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
399519dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
399619dc5607STor Egge 
399719dc5607STor Egge 	entry = *out_entry;
399819dc5607STor Egge 
399919dc5607STor Egge 	/*
400019dc5607STor Egge 	 * Fail if the entry refers to a submap.
400119dc5607STor Egge 	 */
400219dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
400319dc5607STor Egge 		return (KERN_FAILURE);
400419dc5607STor Egge 
400519dc5607STor Egge 	/*
400619dc5607STor Egge 	 * Check whether this task is allowed to have this page.
400719dc5607STor Egge 	 */
400819dc5607STor Egge 	prot = entry->protection;
400919dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
401019dc5607STor Egge 	if ((fault_type & prot) != fault_type)
401119dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
401219dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
401319dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
4014a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
401519dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
401619dc5607STor Egge 
401719dc5607STor Egge 	/*
401819dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
401919dc5607STor Egge 	 * accesses.
402019dc5607STor Egge 	 */
402119dc5607STor Egge 	*wired = (entry->wired_count != 0);
402219dc5607STor Egge 	if (*wired)
4023a6d42a0dSAlan Cox 		fault_type = entry->protection;
402419dc5607STor Egge 
402519dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
402619dc5607STor Egge 		/*
402719dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
402819dc5607STor Egge 		 */
402919dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
403019dc5607STor Egge 			return (KERN_FAILURE);
403119dc5607STor Egge 		/*
403219dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
403319dc5607STor Egge 		 * don't allow writes.
403419dc5607STor Egge 		 */
403519dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
403619dc5607STor Egge 	}
403719dc5607STor Egge 
403819dc5607STor Egge 	/*
403919dc5607STor Egge 	 * Fail if an object should be created.
404019dc5607STor Egge 	 */
404119dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
404219dc5607STor Egge 		return (KERN_FAILURE);
404319dc5607STor Egge 
404419dc5607STor Egge 	/*
404519dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
404619dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
404719dc5607STor Egge 	 */
404819dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
404919dc5607STor Egge 	*object = entry->object.vm_object;
405019dc5607STor Egge 
405119dc5607STor Egge 	*out_prot = prot;
405219dc5607STor Egge 	return (KERN_SUCCESS);
405319dc5607STor Egge }
405419dc5607STor Egge 
405519dc5607STor Egge /*
4056df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
4057df8bae1dSRodney W. Grimes  *
4058df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
4059df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
4060df8bae1dSRodney W. Grimes  */
40610d94caffSDavid Greenman void
40621b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4063df8bae1dSRodney W. Grimes {
4064df8bae1dSRodney W. Grimes 	/*
4065df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
4066df8bae1dSRodney W. Grimes 	 */
4067df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
4068df8bae1dSRodney W. Grimes }
4069df8bae1dSRodney W. Grimes 
4070c7c34a24SBruce Evans #include "opt_ddb.h"
4071c3cb3e12SDavid Greenman #ifdef DDB
4072c7c34a24SBruce Evans #include <sys/kernel.h>
4073c7c34a24SBruce Evans 
4074c7c34a24SBruce Evans #include <ddb/ddb.h>
4075c7c34a24SBruce Evans 
40762ebcd458SAttilio Rao static void
40772ebcd458SAttilio Rao vm_map_print(vm_map_t map)
4078df8bae1dSRodney W. Grimes {
4079c0877f10SJohn Dyson 	vm_map_entry_t entry;
4080c7c34a24SBruce Evans 
4081e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4082e5f251d2SAlan Cox 	    (void *)map,
4083101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
4084df8bae1dSRodney W. Grimes 
4085c7c34a24SBruce Evans 	db_indent += 2;
4086df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
4087df8bae1dSRodney W. Grimes 	    entry = entry->next) {
4088fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
4089fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
4090e5f251d2SAlan Cox 		{
4091df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
4092df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
40930d94caffSDavid Greenman 
409495e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
4095df8bae1dSRodney W. Grimes 			    entry->protection,
4096df8bae1dSRodney W. Grimes 			    entry->max_protection,
40978aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4098df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
409995e5e988SJohn Dyson 				db_printf(", wired");
4100df8bae1dSRodney W. Grimes 		}
41019fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4102cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
41039fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
4104cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4105df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
41069fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
41079fdfe602SMatthew Dillon 				entry->object.sub_map)) {
4108c7c34a24SBruce Evans 				db_indent += 2;
41092ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
4110c7c34a24SBruce Evans 				db_indent -= 2;
4111df8bae1dSRodney W. Grimes 			}
41120d94caffSDavid Greenman 		} else {
4113ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
4114ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
4115cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
4116101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
4117cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4118ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
4119ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
4120ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
41213364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
4122afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
4123c7c34a24SBruce Evans 				db_printf(", copy (%s)",
4124afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4125c7c34a24SBruce Evans 			db_printf("\n");
4126df8bae1dSRodney W. Grimes 
4127df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
4128df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
4129df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
4130c7c34a24SBruce Evans 				db_indent += 2;
4131101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
4132101eeb7fSBruce Evans 						entry->object.vm_object,
41332ebcd458SAttilio Rao 						1, 0, (char *)0);
4134c7c34a24SBruce Evans 				db_indent -= 2;
4135df8bae1dSRodney W. Grimes 			}
4136df8bae1dSRodney W. Grimes 		}
4137df8bae1dSRodney W. Grimes 	}
4138c7c34a24SBruce Evans 	db_indent -= 2;
4139df8bae1dSRodney W. Grimes }
414095e5e988SJohn Dyson 
41412ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
41422ebcd458SAttilio Rao {
41432ebcd458SAttilio Rao 
41442ebcd458SAttilio Rao 	if (!have_addr) {
41452ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
41462ebcd458SAttilio Rao 		return;
41472ebcd458SAttilio Rao 	}
41482ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
41492ebcd458SAttilio Rao }
415095e5e988SJohn Dyson 
415195e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
415295e5e988SJohn Dyson {
415395e5e988SJohn Dyson 	struct proc *p;
415495e5e988SJohn Dyson 
415595e5e988SJohn Dyson 	if (have_addr) {
415695e5e988SJohn Dyson 		p = (struct proc *) addr;
415795e5e988SJohn Dyson 	} else {
415895e5e988SJohn Dyson 		p = curproc;
415995e5e988SJohn Dyson 	}
416095e5e988SJohn Dyson 
4161ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4162ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4163b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
416495e5e988SJohn Dyson 
41652ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
416695e5e988SJohn Dyson }
416795e5e988SJohn Dyson 
4168c7c34a24SBruce Evans #endif /* DDB */
4169