xref: /freebsd/sys/vm/vm_map.c (revision 4648ba0a0f20a61d9fc0dcd26bbf00283d86e465)
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);
130b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
13192351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13292351f16SAlan Cox     vm_offset_t max);
1330b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
134655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
1358355f576SJeff Roberson #ifdef INVARIANTS
1368355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1378355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1388355f576SJeff Roberson #endif
139*4648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
140*4648ba0aSKonstantin Belousov     vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
141*4648ba0aSKonstantin Belousov     int cow);
142b18bfc3dSJohn Dyson 
143ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
144ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1453364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1463364c323SKonstantin Belousov 
14757051fdcSTor Egge /*
14857051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
14957051fdcSTor Egge  * stable.
15057051fdcSTor Egge  */
15157051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15257051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15357051fdcSTor Egge 
154d239bd3cSKonstantin Belousov /*
155d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
156d239bd3cSKonstantin Belousov  *
157d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
158d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
159d239bd3cSKonstantin Belousov  */
160d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
161d239bd3cSKonstantin Belousov 		{					\
162d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
163d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
164d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
165d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
166d239bd3cSKonstantin Belousov 		if (start > end)			\
167d239bd3cSKonstantin Belousov 			start = end;			\
168d239bd3cSKonstantin Belousov 		}
169d239bd3cSKonstantin Belousov 
1706fecb26bSKonstantin Belousov /*
1716fecb26bSKonstantin Belousov  *	vm_map_startup:
1726fecb26bSKonstantin Belousov  *
1736fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1746fecb26bSKonstantin Belousov  *	any other vm_map routines.
1756fecb26bSKonstantin Belousov  *
1766fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1776fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1786fecb26bSKonstantin Belousov  *
1796fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1806fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1816fecb26bSKonstantin Belousov  *
1826fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1836fecb26bSKonstantin Belousov  *	maps and requires map entries.
1846fecb26bSKonstantin Belousov  */
1856fecb26bSKonstantin Belousov 
1860d94caffSDavid Greenman void
1871b40f8c0SMatthew Dillon vm_map_startup(void)
188df8bae1dSRodney W. Grimes {
1893a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1908355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1918355f576SJeff Roberson #ifdef INVARIANTS
1928355f576SJeff Roberson 	    vm_map_zdtor,
1938355f576SJeff Roberson #else
1948355f576SJeff Roberson 	    NULL,
1958355f576SJeff Roberson #endif
196f872f6eaSAlan Cox 	    vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1978355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
198670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
19918aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
20018aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
201670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
202670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2035df87b21SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2045df87b21SJeff Roberson #ifdef INVARIANTS
2055df87b21SJeff Roberson 	    vmspace_zdtor,
2065df87b21SJeff Roberson #else
2075df87b21SJeff Roberson 	    NULL,
2085df87b21SJeff Roberson #endif
209f872f6eaSAlan Cox 	    vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2108355f576SJeff Roberson }
2118355f576SJeff Roberson 
212b23f72e9SBrian Feldman static int
213b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2148355f576SJeff Roberson {
2158355f576SJeff Roberson 	struct vmspace *vm;
2168355f576SJeff Roberson 
2178355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2188355f576SJeff Roberson 
21989b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
220b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
221e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(vmspace_pmap(vm));
222b23f72e9SBrian Feldman 	return (0);
2238355f576SJeff Roberson }
2248355f576SJeff Roberson 
225b23f72e9SBrian Feldman static int
226b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2278355f576SJeff Roberson {
2288355f576SJeff Roberson 	vm_map_t map;
2298355f576SJeff Roberson 
2308355f576SJeff Roberson 	map = (vm_map_t)mem;
231763d9566STim Kientzle 	memset(map, 0, sizeof(*map));
232e30df26eSAlan Cox 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
233e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
234b23f72e9SBrian Feldman 	return (0);
2358355f576SJeff Roberson }
2368355f576SJeff Roberson 
2378355f576SJeff Roberson #ifdef INVARIANTS
2388355f576SJeff Roberson static void
2398355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2408355f576SJeff Roberson {
2418355f576SJeff Roberson 	struct vmspace *vm;
2428355f576SJeff Roberson 
2438355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2448355f576SJeff Roberson 
2458355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2468355f576SJeff Roberson }
2478355f576SJeff Roberson static void
2488355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2498355f576SJeff Roberson {
2508355f576SJeff Roberson 	vm_map_t map;
2518355f576SJeff Roberson 
2528355f576SJeff Roberson 	map = (vm_map_t)mem;
2538355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2548355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2558355f576SJeff Roberson 	    map, map->nentries));
2568355f576SJeff Roberson 	KASSERT(map->size == 0,
2578355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2589eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2598355f576SJeff Roberson }
2608355f576SJeff Roberson #endif	/* INVARIANTS */
2618355f576SJeff Roberson 
262df8bae1dSRodney W. Grimes /*
263df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
264df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
26574d1d2b7SNeel Natu  *
26674d1d2b7SNeel Natu  * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
267df8bae1dSRodney W. Grimes  */
268df8bae1dSRodney W. Grimes struct vmspace *
26974d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
270df8bae1dSRodney W. Grimes {
271c0877f10SJohn Dyson 	struct vmspace *vm;
2720d94caffSDavid Greenman 
273a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
27474d1d2b7SNeel Natu 
27574d1d2b7SNeel Natu 	KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
27674d1d2b7SNeel Natu 
27774d1d2b7SNeel Natu 	if (pinit == NULL)
27874d1d2b7SNeel Natu 		pinit = &pmap_pinit;
27974d1d2b7SNeel Natu 
28074d1d2b7SNeel Natu 	if (!pinit(vmspace_pmap(vm))) {
28189b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
28289b57fcfSKonstantin Belousov 		return (NULL);
28389b57fcfSKonstantin Belousov 	}
28421c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
28592351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
286df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2872d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
28851ab6c28SAlan Cox 	vm->vm_swrss = 0;
28951ab6c28SAlan Cox 	vm->vm_tsize = 0;
29051ab6c28SAlan Cox 	vm->vm_dsize = 0;
29151ab6c28SAlan Cox 	vm->vm_ssize = 0;
29251ab6c28SAlan Cox 	vm->vm_taddr = 0;
29351ab6c28SAlan Cox 	vm->vm_daddr = 0;
29451ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
295df8bae1dSRodney W. Grimes 	return (vm);
296df8bae1dSRodney W. Grimes }
297df8bae1dSRodney W. Grimes 
2981ba5ad42SEdward Tomasz Napierala static void
2991ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3001ba5ad42SEdward Tomasz Napierala {
3011ba5ad42SEdward Tomasz Napierala 
302afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3031ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3041ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3051ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3061ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3071ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3081ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3091ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
310afcc55f3SEdward Tomasz Napierala #endif
3111ba5ad42SEdward Tomasz Napierala }
3121ba5ad42SEdward Tomasz Napierala 
31362a59e8fSWarner Losh static inline void
314582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
315df8bae1dSRodney W. Grimes {
3160ef12795SAlan Cox 
31721c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3183db161e0SMatthew Dillon 
3193db161e0SMatthew Dillon 	/*
3203db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3213db161e0SMatthew Dillon 	 * exit1().
3223db161e0SMatthew Dillon 	 */
3233db161e0SMatthew Dillon 	shmexit(vm);
3243db161e0SMatthew Dillon 
32530dcfc09SJohn Dyson 	/*
326df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3270d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3280d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
329df8bae1dSRodney W. Grimes 	 */
330717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
331df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3328355f576SJeff Roberson 
3330ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3340ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3358355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
336df8bae1dSRodney W. Grimes }
337582ec34cSAlfred Perlstein 
338582ec34cSAlfred Perlstein void
339582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
340582ec34cSAlfred Perlstein {
341582ec34cSAlfred Perlstein 
342582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
343582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
344582ec34cSAlfred Perlstein 
3451a587ef2SJohn Baldwin 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
346582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
347582ec34cSAlfred Perlstein }
348582ec34cSAlfred Perlstein 
349582ec34cSAlfred Perlstein void
350582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
351582ec34cSAlfred Perlstein {
352334f7061SPeter Wemm 	struct vmspace *vm;
353582ec34cSAlfred Perlstein 
35457051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
355334f7061SPeter Wemm 	vm = p->p_vmspace;
356334f7061SPeter Wemm 	p->p_vmspace = NULL;
35757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
35857051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
35957051fdcSTor Egge 	vmspace_free(vm);
36057051fdcSTor Egge }
36157051fdcSTor Egge 
36257051fdcSTor Egge void
36357051fdcSTor Egge vmspace_exit(struct thread *td)
36457051fdcSTor Egge {
36557051fdcSTor Egge 	int refcnt;
36657051fdcSTor Egge 	struct vmspace *vm;
36757051fdcSTor Egge 	struct proc *p;
368389d2b6eSMatthew Dillon 
369389d2b6eSMatthew Dillon 	/*
37057051fdcSTor Egge 	 * Release user portion of address space.
37157051fdcSTor Egge 	 * This releases references to vnodes,
37257051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
37357051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
374389d2b6eSMatthew Dillon 	 *
37557051fdcSTor Egge 	 * The last exiting process to reach this point releases as
37657051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
37757051fdcSTor Egge 	 * slower fallback in case another process had a temporary
37857051fdcSTor Egge 	 * reference to the vmspace.
379389d2b6eSMatthew Dillon 	 */
38057051fdcSTor Egge 
38157051fdcSTor Egge 	p = td->td_proc;
38257051fdcSTor Egge 	vm = p->p_vmspace;
38357051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
38457051fdcSTor Egge 	do {
38557051fdcSTor Egge 		refcnt = vm->vm_refcnt;
38657051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
38757051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
38857051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
38957051fdcSTor Egge 			p->p_vmspace = &vmspace0;
39057051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39157051fdcSTor Egge 			pmap_activate(td);
39257051fdcSTor Egge 		}
39357051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
39457051fdcSTor Egge 	if (refcnt == 1) {
39557051fdcSTor Egge 		if (p->p_vmspace != vm) {
39657051fdcSTor Egge 			/* vmspace not yet freed, switch back */
39757051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39857051fdcSTor Egge 			p->p_vmspace = vm;
39957051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40057051fdcSTor Egge 			pmap_activate(td);
40157051fdcSTor Egge 		}
40257051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
40357051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
40457051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
40557051fdcSTor Egge 		p->p_vmspace = &vmspace0;
40657051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
40757051fdcSTor Egge 		pmap_activate(td);
408334f7061SPeter Wemm 		vmspace_dofree(vm);
409334f7061SPeter Wemm 	}
4101ba5ad42SEdward Tomasz Napierala 	vmspace_container_reset(p);
41157051fdcSTor Egge }
41257051fdcSTor Egge 
41357051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
41457051fdcSTor Egge 
41557051fdcSTor Egge struct vmspace *
41657051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
41757051fdcSTor Egge {
41857051fdcSTor Egge 	struct vmspace *vm;
41957051fdcSTor Egge 	int refcnt;
42057051fdcSTor Egge 
42157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
42257051fdcSTor Egge 	vm = p->p_vmspace;
42357051fdcSTor Egge 	if (vm == NULL) {
42457051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42557051fdcSTor Egge 		return (NULL);
42657051fdcSTor Egge 	}
42757051fdcSTor Egge 	do {
42857051fdcSTor Egge 		refcnt = vm->vm_refcnt;
42957051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
43057051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
43157051fdcSTor Egge 			return (NULL);
43257051fdcSTor Egge 		}
43357051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
43457051fdcSTor Egge 	if (vm != p->p_vmspace) {
43557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
43657051fdcSTor Egge 		vmspace_free(vm);
43757051fdcSTor Egge 		return (NULL);
43857051fdcSTor Egge 	}
43957051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
44057051fdcSTor Egge 	return (vm);
44157051fdcSTor Egge }
442df8bae1dSRodney W. Grimes 
4431b40f8c0SMatthew Dillon void
444780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4451b40f8c0SMatthew Dillon {
446bc91c510SAlan Cox 
44793bc4879SAlan Cox 	if (map->system_map)
448ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
44912c64974SMaxime Henrion 	else
4509fde98bbSAttilio Rao 		sx_xlock_(&map->lock, file, line);
4511b40f8c0SMatthew Dillon 	map->timestamp++;
4521b40f8c0SMatthew Dillon }
4531b40f8c0SMatthew Dillon 
4540b367bd8SKonstantin Belousov static void
4550b367bd8SKonstantin Belousov vm_map_process_deferred(void)
4560e0af8ecSBrian Feldman {
4570b367bd8SKonstantin Belousov 	struct thread *td;
4586fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
45984110e7eSKonstantin Belousov 	vm_object_t object;
460655c3490SKonstantin Belousov 
4610b367bd8SKonstantin Belousov 	td = curthread;
4626fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
4636fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
4646fbe60faSJohn Baldwin 	while (entry != NULL) {
4656fbe60faSJohn Baldwin 		next = entry->next;
46684110e7eSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
46784110e7eSKonstantin Belousov 			/*
46884110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
46984110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
47084110e7eSKonstantin Belousov 			 */
47184110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
47284110e7eSKonstantin Belousov 			    ("Submap with writecount"));
47384110e7eSKonstantin Belousov 			object = entry->object.vm_object;
47484110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
47584110e7eSKonstantin Belousov 			vnode_pager_release_writecount(object, entry->start,
47684110e7eSKonstantin Belousov 			    entry->end);
47784110e7eSKonstantin Belousov 		}
4780b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
4796fbe60faSJohn Baldwin 		entry = next;
4800b367bd8SKonstantin Belousov 	}
4810b367bd8SKonstantin Belousov }
4820b367bd8SKonstantin Belousov 
4830b367bd8SKonstantin Belousov void
4840b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
4850b367bd8SKonstantin Belousov {
4860b367bd8SKonstantin Belousov 
4870b367bd8SKonstantin Belousov 	if (map->system_map)
488ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
4890b367bd8SKonstantin Belousov 	else {
4909fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
4910b367bd8SKonstantin Belousov 		vm_map_process_deferred();
492655c3490SKonstantin Belousov 	}
4930e0af8ecSBrian Feldman }
4940e0af8ecSBrian Feldman 
4950e0af8ecSBrian Feldman void
496780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
4970e0af8ecSBrian Feldman {
498bc91c510SAlan Cox 
49993bc4879SAlan Cox 	if (map->system_map)
500ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
50112c64974SMaxime Henrion 	else
5029fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
50336daaecdSAlan Cox }
5040e0af8ecSBrian Feldman 
5050e0af8ecSBrian Feldman void
506780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
5070e0af8ecSBrian Feldman {
508bc91c510SAlan Cox 
50936daaecdSAlan Cox 	if (map->system_map)
510ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5110b367bd8SKonstantin Belousov 	else {
5129fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
5130b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5140b367bd8SKonstantin Belousov 	}
51525adb370SBrian Feldman }
51625adb370SBrian Feldman 
517d974f03cSAlan Cox int
518780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
519d974f03cSAlan Cox {
52025adb370SBrian Feldman 	int error;
52125adb370SBrian Feldman 
52236daaecdSAlan Cox 	error = map->system_map ?
523ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5249fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
5253a92e5d5SAlan Cox 	if (error == 0)
5263a92e5d5SAlan Cox 		map->timestamp++;
527bc91c510SAlan Cox 	return (error == 0);
5280e0af8ecSBrian Feldman }
5290e0af8ecSBrian Feldman 
5300e0af8ecSBrian Feldman int
53172d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
53272d97679SDavid Schultz {
53372d97679SDavid Schultz 	int error;
53472d97679SDavid Schultz 
53572d97679SDavid Schultz 	error = map->system_map ?
536ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5379fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
53872d97679SDavid Schultz 	return (error == 0);
53972d97679SDavid Schultz }
54072d97679SDavid Schultz 
54105a8c414SAlan Cox /*
54205a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
54305a8c414SAlan Cox  *
54405a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
54505a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
54605a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
54705a8c414SAlan Cox  *	returned without a read or write lock held.
54805a8c414SAlan Cox  *
54905a8c414SAlan Cox  *	Requires that the map be read locked.
55005a8c414SAlan Cox  */
55172d97679SDavid Schultz int
552780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5530e0af8ecSBrian Feldman {
55405a8c414SAlan Cox 	unsigned int last_timestamp;
555bc91c510SAlan Cox 
55612c64974SMaxime Henrion 	if (map->system_map) {
557ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
55805a8c414SAlan Cox 	} else {
5599fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
56005a8c414SAlan Cox 			last_timestamp = map->timestamp;
5619fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
5620b367bd8SKonstantin Belousov 			vm_map_process_deferred();
56305a8c414SAlan Cox 			/*
56405a8c414SAlan Cox 			 * If the map's timestamp does not change while the
56505a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
56605a8c414SAlan Cox 			 */
5679fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
56805a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
5699fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
57005a8c414SAlan Cox 				return (1);
57105a8c414SAlan Cox 			}
57205a8c414SAlan Cox 		}
57305a8c414SAlan Cox 	}
574bc91c510SAlan Cox 	map->timestamp++;
575bc91c510SAlan Cox 	return (0);
5760e0af8ecSBrian Feldman }
5770e0af8ecSBrian Feldman 
5780e0af8ecSBrian Feldman void
579780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5801b40f8c0SMatthew Dillon {
581bc91c510SAlan Cox 
58212c64974SMaxime Henrion 	if (map->system_map) {
583ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
58405a8c414SAlan Cox 	} else
5859fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
58605a8c414SAlan Cox }
58705a8c414SAlan Cox 
58805a8c414SAlan Cox /*
58905a8c414SAlan Cox  *	vm_map_locked:
59005a8c414SAlan Cox  *
59105a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
59205a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
59305a8c414SAlan Cox  */
59405a8c414SAlan Cox int
59505a8c414SAlan Cox vm_map_locked(vm_map_t map)
59605a8c414SAlan Cox {
59705a8c414SAlan Cox 
59805a8c414SAlan Cox 	if (map->system_map)
59905a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
60005a8c414SAlan Cox 	else
60105a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
60225adb370SBrian Feldman }
60325adb370SBrian Feldman 
6043a0916b8SKonstantin Belousov #ifdef INVARIANTS
6053a0916b8SKonstantin Belousov static void
6063a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6073a0916b8SKonstantin Belousov {
6083a0916b8SKonstantin Belousov 
6093a0916b8SKonstantin Belousov 	if (map->system_map)
610ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
6113a0916b8SKonstantin Belousov 	else
6129fde98bbSAttilio Rao 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
6133a0916b8SKonstantin Belousov }
6143a0916b8SKonstantin Belousov 
6153a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6163a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6173a0916b8SKonstantin Belousov #else
6183a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6193a0916b8SKonstantin Belousov #endif
6203a0916b8SKonstantin Belousov 
621acd9a301SAlan Cox /*
6228304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6238304adaaSAlan Cox  *
6248304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6258304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6268304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6278304adaaSAlan Cox  *	exceeded.
6288304adaaSAlan Cox  *
6298304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6308304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6318304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6328304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6338304adaaSAlan Cox  *	operation on the map.
634acd9a301SAlan Cox  */
6359688f931SAlan Cox int
6368304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
637acd9a301SAlan Cox {
638acd9a301SAlan Cox 
6393a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6408304adaaSAlan Cox 	if (map->system_map)
641ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
6428304adaaSAlan Cox 	else
6439fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
6448304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
6458304adaaSAlan Cox 	    timo));
646acd9a301SAlan Cox }
647acd9a301SAlan Cox 
648acd9a301SAlan Cox /*
649acd9a301SAlan Cox  *	vm_map_wakeup:
6508304adaaSAlan Cox  *
6518304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
6528304adaaSAlan Cox  *	vm_map_unlock_and_wait().
653acd9a301SAlan Cox  */
6549688f931SAlan Cox void
655acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
656acd9a301SAlan Cox {
657acd9a301SAlan Cox 
658b49ecb86SAlan Cox 	/*
6593a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6608304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
6618304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
662b49ecb86SAlan Cox 	 */
6633a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6643a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
665acd9a301SAlan Cox 	wakeup(&map->root);
666acd9a301SAlan Cox }
667acd9a301SAlan Cox 
668a5db445dSMax Laier void
669a5db445dSMax Laier vm_map_busy(vm_map_t map)
670a5db445dSMax Laier {
671a5db445dSMax Laier 
672a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
673a5db445dSMax Laier 	map->busy++;
674a5db445dSMax Laier }
675a5db445dSMax Laier 
676a5db445dSMax Laier void
677a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
678a5db445dSMax Laier {
679a5db445dSMax Laier 
680a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
681a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
682a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
683a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
684a5db445dSMax Laier 		wakeup(&map->busy);
685a5db445dSMax Laier 	}
686a5db445dSMax Laier }
687a5db445dSMax Laier 
688a5db445dSMax Laier void
689a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
690a5db445dSMax Laier {
691a5db445dSMax Laier 
692a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
693a5db445dSMax Laier 	while (map->busy) {
694a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
695a5db445dSMax Laier 		if (map->system_map)
696a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
697a5db445dSMax Laier 		else
698a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
699a5db445dSMax Laier 	}
700a5db445dSMax Laier 	map->timestamp++;
701a5db445dSMax Laier }
702a5db445dSMax Laier 
7031b40f8c0SMatthew Dillon long
7041b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
7051b40f8c0SMatthew Dillon {
7061b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
7071b40f8c0SMatthew Dillon }
7081b40f8c0SMatthew Dillon 
709ff2b5645SMatthew Dillon /*
710df8bae1dSRodney W. Grimes  *	vm_map_create:
711df8bae1dSRodney W. Grimes  *
712df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
713df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
714df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
715df8bae1dSRodney W. Grimes  */
7160d94caffSDavid Greenman vm_map_t
7171b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
718df8bae1dSRodney W. Grimes {
719c0877f10SJohn Dyson 	vm_map_t result;
720df8bae1dSRodney W. Grimes 
721a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
72221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
72392351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
724df8bae1dSRodney W. Grimes 	return (result);
725df8bae1dSRodney W. Grimes }
726df8bae1dSRodney W. Grimes 
727df8bae1dSRodney W. Grimes /*
728df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
729df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
730df8bae1dSRodney W. Grimes  */
7318355f576SJeff Roberson static void
73292351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
733df8bae1dSRodney W. Grimes {
73421c641b2SJohn Baldwin 
735df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7369688f931SAlan Cox 	map->needs_wakeup = FALSE;
7373075778bSJohn Dyson 	map->system_map = 0;
73892351f16SAlan Cox 	map->pmap = pmap;
739df8bae1dSRodney W. Grimes 	map->min_offset = min;
740df8bae1dSRodney W. Grimes 	map->max_offset = max;
741af7cd0c5SBrian Feldman 	map->flags = 0;
7424e94f402SAlan Cox 	map->root = NULL;
743df8bae1dSRodney W. Grimes 	map->timestamp = 0;
744a5db445dSMax Laier 	map->busy = 0;
745df8bae1dSRodney W. Grimes }
746df8bae1dSRodney W. Grimes 
747a18b1f1dSJason Evans void
74892351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
749a18b1f1dSJason Evans {
75092351f16SAlan Cox 
75192351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
752d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
75312c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
754a18b1f1dSJason Evans }
755a18b1f1dSJason Evans 
756df8bae1dSRodney W. Grimes /*
757b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
758b18bfc3dSJohn Dyson  *
759b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
760b18bfc3dSJohn Dyson  */
76162487bb4SJohn Dyson static void
7621b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
763b18bfc3dSJohn Dyson {
7642b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
765b18bfc3dSJohn Dyson }
766b18bfc3dSJohn Dyson 
767b18bfc3dSJohn Dyson /*
768df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
769df8bae1dSRodney W. Grimes  *
770df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
771b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
772df8bae1dSRodney W. Grimes  */
773f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7741b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
775df8bae1dSRodney W. Grimes {
7761f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7771f6889a1SMatthew Dillon 
7782b4a2c27SAlan Cox 	if (map->system_map)
7792b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
7802b4a2c27SAlan Cox 	else
781a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
7821f6889a1SMatthew Dillon 	if (new_entry == NULL)
7831f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
7841f6889a1SMatthew Dillon 	return (new_entry);
785df8bae1dSRodney W. Grimes }
786df8bae1dSRodney W. Grimes 
787df8bae1dSRodney W. Grimes /*
788794316a8SAlan Cox  *	vm_map_entry_set_behavior:
789794316a8SAlan Cox  *
790794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
791794316a8SAlan Cox  *	sequential.
792794316a8SAlan Cox  */
79362a59e8fSWarner Losh static inline void
794794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
795794316a8SAlan Cox {
796794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
797794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
798794316a8SAlan Cox }
799794316a8SAlan Cox 
800794316a8SAlan Cox /*
8010164e057SAlan Cox  *	vm_map_entry_set_max_free:
8020164e057SAlan Cox  *
8030164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
8040164e057SAlan Cox  */
80562a59e8fSWarner Losh static inline void
8060164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
8070164e057SAlan Cox {
8080164e057SAlan Cox 
8090164e057SAlan Cox 	entry->max_free = entry->adj_free;
8100164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
8110164e057SAlan Cox 		entry->max_free = entry->left->max_free;
8120164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
8130164e057SAlan Cox 		entry->max_free = entry->right->max_free;
8140164e057SAlan Cox }
8150164e057SAlan Cox 
8160164e057SAlan Cox /*
8174e94f402SAlan Cox  *	vm_map_entry_splay:
8184e94f402SAlan Cox  *
8190164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
8200164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
8210164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8220164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8230164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8240164e057SAlan Cox  *	amortized.
8250164e057SAlan Cox  *
8260164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8270164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8280164e057SAlan Cox  *
8290164e057SAlan Cox  *	The map must be locked, and leaves it so.
8300164e057SAlan Cox  *
8310164e057SAlan Cox  *	Returns: the new root.
8324e94f402SAlan Cox  */
8334e94f402SAlan Cox static vm_map_entry_t
8340164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8354e94f402SAlan Cox {
8360164e057SAlan Cox 	vm_map_entry_t llist, rlist;
8370164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
8380164e057SAlan Cox 	vm_map_entry_t y;
8394e94f402SAlan Cox 
8400164e057SAlan Cox 	/* Special case of empty tree. */
8414e94f402SAlan Cox 	if (root == NULL)
8424e94f402SAlan Cox 		return (root);
8430164e057SAlan Cox 
8440164e057SAlan Cox 	/*
8450164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8460164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8470164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8480164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8490164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8500164e057SAlan Cox 	 * the two spines.
8510164e057SAlan Cox 	 */
8520164e057SAlan Cox 	llist = NULL;
8530164e057SAlan Cox 	rlist = NULL;
8540164e057SAlan Cox 	for (;;) {
8550164e057SAlan Cox 		/* root is never NULL in here. */
8560164e057SAlan Cox 		if (addr < root->start) {
8570164e057SAlan Cox 			y = root->left;
8580164e057SAlan Cox 			if (y == NULL)
8594e94f402SAlan Cox 				break;
8600164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8610164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8624e94f402SAlan Cox 				root->left = y->right;
8634e94f402SAlan Cox 				y->right = root;
8640164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8650164e057SAlan Cox 				root = y->left;
8660164e057SAlan Cox 				y->left = rlist;
8670164e057SAlan Cox 				rlist = y;
8680164e057SAlan Cox 			} else {
8690164e057SAlan Cox 				/* Put root on rlist. */
8700164e057SAlan Cox 				root->left = rlist;
8710164e057SAlan Cox 				rlist = root;
8724e94f402SAlan Cox 				root = y;
8734e94f402SAlan Cox 			}
8747438d60bSAlan Cox 		} else if (addr >= root->end) {
8750164e057SAlan Cox 			y = root->right;
8767438d60bSAlan Cox 			if (y == NULL)
8774e94f402SAlan Cox 				break;
8780164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8790164e057SAlan Cox 				/* Rotate left and put y on llist. */
8804e94f402SAlan Cox 				root->right = y->left;
8814e94f402SAlan Cox 				y->left = root;
8820164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8830164e057SAlan Cox 				root = y->right;
8840164e057SAlan Cox 				y->right = llist;
8850164e057SAlan Cox 				llist = y;
8860164e057SAlan Cox 			} else {
8870164e057SAlan Cox 				/* Put root on llist. */
8880164e057SAlan Cox 				root->right = llist;
8890164e057SAlan Cox 				llist = root;
8904e94f402SAlan Cox 				root = y;
8914e94f402SAlan Cox 			}
8927438d60bSAlan Cox 		} else
8937438d60bSAlan Cox 			break;
8940164e057SAlan Cox 	}
8950164e057SAlan Cox 
8960164e057SAlan Cox 	/*
8970164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
8980164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
8990164e057SAlan Cox 	 * bottom of llist and rlist.
9000164e057SAlan Cox 	 */
9010164e057SAlan Cox 	ltree = root->left;
9020164e057SAlan Cox 	while (llist != NULL) {
9030164e057SAlan Cox 		y = llist->right;
9040164e057SAlan Cox 		llist->right = ltree;
9050164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
9060164e057SAlan Cox 		ltree = llist;
9070164e057SAlan Cox 		llist = y;
9080164e057SAlan Cox 	}
9090164e057SAlan Cox 	rtree = root->right;
9100164e057SAlan Cox 	while (rlist != NULL) {
9110164e057SAlan Cox 		y = rlist->left;
9120164e057SAlan Cox 		rlist->left = rtree;
9130164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
9140164e057SAlan Cox 		rtree = rlist;
9150164e057SAlan Cox 		rlist = y;
9160164e057SAlan Cox 	}
9170164e057SAlan Cox 
9180164e057SAlan Cox 	/*
9190164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
9200164e057SAlan Cox 	 */
9210164e057SAlan Cox 	root->left = ltree;
9220164e057SAlan Cox 	root->right = rtree;
9230164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9240164e057SAlan Cox 
9254e94f402SAlan Cox 	return (root);
9264e94f402SAlan Cox }
9274e94f402SAlan Cox 
9284e94f402SAlan Cox /*
929df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
930df8bae1dSRodney W. Grimes  *
931df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
932df8bae1dSRodney W. Grimes  */
9334e94f402SAlan Cox static void
93499c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
93599c81ca9SAlan Cox 		  vm_map_entry_t after_where,
93699c81ca9SAlan Cox 		  vm_map_entry_t entry)
93799c81ca9SAlan Cox {
93821c641b2SJohn Baldwin 
93921c641b2SJohn Baldwin 	CTR4(KTR_VM,
94021c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
94121c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9423a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
94399c81ca9SAlan Cox 	map->nentries++;
94499c81ca9SAlan Cox 	entry->prev = after_where;
94599c81ca9SAlan Cox 	entry->next = after_where->next;
94699c81ca9SAlan Cox 	entry->next->prev = entry;
94799c81ca9SAlan Cox 	after_where->next = entry;
9484e94f402SAlan Cox 
9494e94f402SAlan Cox 	if (after_where != &map->header) {
9504e94f402SAlan Cox 		if (after_where != map->root)
9514e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
9524e94f402SAlan Cox 		entry->right = after_where->right;
9534e94f402SAlan Cox 		entry->left = after_where;
9544e94f402SAlan Cox 		after_where->right = NULL;
9550164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
9560164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
9574e94f402SAlan Cox 	} else {
9584e94f402SAlan Cox 		entry->right = map->root;
9594e94f402SAlan Cox 		entry->left = NULL;
9604e94f402SAlan Cox 	}
9610164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9620164e057SAlan Cox 	    entry->next->start) - entry->end;
9630164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9644e94f402SAlan Cox 	map->root = entry;
965df8bae1dSRodney W. Grimes }
96699c81ca9SAlan Cox 
9674e94f402SAlan Cox static void
96899c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
96999c81ca9SAlan Cox 		    vm_map_entry_t entry)
97099c81ca9SAlan Cox {
9714e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
97299c81ca9SAlan Cox 
9733a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9744e94f402SAlan Cox 	if (entry != map->root)
9754e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
9764e94f402SAlan Cox 	if (entry->left == NULL)
9774e94f402SAlan Cox 		root = entry->right;
9784e94f402SAlan Cox 	else {
9794e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
9804e94f402SAlan Cox 		root->right = entry->right;
9810164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
9820164e057SAlan Cox 		    entry->next->start) - root->end;
9830164e057SAlan Cox 		vm_map_entry_set_max_free(root);
9844e94f402SAlan Cox 	}
9854e94f402SAlan Cox 	map->root = root;
9864e94f402SAlan Cox 
9874e94f402SAlan Cox 	prev = entry->prev;
9884e94f402SAlan Cox 	next = entry->next;
98999c81ca9SAlan Cox 	next->prev = prev;
99099c81ca9SAlan Cox 	prev->next = next;
99199c81ca9SAlan Cox 	map->nentries--;
99221c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
99321c641b2SJohn Baldwin 	    map->nentries, entry);
994df8bae1dSRodney W. Grimes }
995df8bae1dSRodney W. Grimes 
996df8bae1dSRodney W. Grimes /*
9970164e057SAlan Cox  *	vm_map_entry_resize_free:
9980164e057SAlan Cox  *
9990164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
10000164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
10010164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
10020164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
10030164e057SAlan Cox  *
10040164e057SAlan Cox  *	The map must be locked, and leaves it so.
10050164e057SAlan Cox  */
10060164e057SAlan Cox static void
10070164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
10080164e057SAlan Cox {
10090164e057SAlan Cox 
10100164e057SAlan Cox 	/*
10110164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
10120164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
10130164e057SAlan Cox 	 * root and making the change there.
10140164e057SAlan Cox 	 */
10150164e057SAlan Cox 	if (entry != map->root)
10160164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
10170164e057SAlan Cox 
10180164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
10190164e057SAlan Cox 	    entry->next->start) - entry->end;
10200164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
10210164e057SAlan Cox }
10220164e057SAlan Cox 
10230164e057SAlan Cox /*
1024df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
1025df8bae1dSRodney W. Grimes  *
1026df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
1027df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
1028df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
1029df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
1030df8bae1dSRodney W. Grimes  *	result indicates whether the address is
1031df8bae1dSRodney W. Grimes  *	actually contained in the map.
1032df8bae1dSRodney W. Grimes  */
10330d94caffSDavid Greenman boolean_t
10341b40f8c0SMatthew Dillon vm_map_lookup_entry(
10351b40f8c0SMatthew Dillon 	vm_map_t map,
10361b40f8c0SMatthew Dillon 	vm_offset_t address,
10371b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
1038df8bae1dSRodney W. Grimes {
1039c0877f10SJohn Dyson 	vm_map_entry_t cur;
104005a8c414SAlan Cox 	boolean_t locked;
1041df8bae1dSRodney W. Grimes 
10424c3ef59eSAlan Cox 	/*
10434c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
10444c3ef59eSAlan Cox 	 * "address" is the map's header.
10454c3ef59eSAlan Cox 	 */
10464c3ef59eSAlan Cox 	cur = map->root;
10474e94f402SAlan Cox 	if (cur == NULL)
10484e94f402SAlan Cox 		*entry = &map->header;
10494c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
10504c3ef59eSAlan Cox 		*entry = cur;
10514c3ef59eSAlan Cox 		return (TRUE);
105205a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
105305a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
105405a8c414SAlan Cox 		/*
105505a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
105605a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
105705a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
105805a8c414SAlan Cox 		 * on a temporary upgrade.
105905a8c414SAlan Cox 		 */
10604c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
106105a8c414SAlan Cox 		if (!locked)
106205a8c414SAlan Cox 			sx_downgrade(&map->lock);
1063df8bae1dSRodney W. Grimes 
10644c3ef59eSAlan Cox 		/*
10654c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
10664c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
10674c3ef59eSAlan Cox 		 * immediately before or after "address".
10684c3ef59eSAlan Cox 		 */
1069df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1070df8bae1dSRodney W. Grimes 			*entry = cur;
10714e94f402SAlan Cox 			if (cur->end > address)
1072df8bae1dSRodney W. Grimes 				return (TRUE);
10734e94f402SAlan Cox 		} else
1074df8bae1dSRodney W. Grimes 			*entry = cur->prev;
107505a8c414SAlan Cox 	} else
107605a8c414SAlan Cox 		/*
107705a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
107805a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
107905a8c414SAlan Cox 		 */
108005a8c414SAlan Cox 		for (;;) {
108105a8c414SAlan Cox 			if (address < cur->start) {
108205a8c414SAlan Cox 				if (cur->left == NULL) {
108305a8c414SAlan Cox 					*entry = cur->prev;
108405a8c414SAlan Cox 					break;
108505a8c414SAlan Cox 				}
108605a8c414SAlan Cox 				cur = cur->left;
108705a8c414SAlan Cox 			} else if (cur->end > address) {
108805a8c414SAlan Cox 				*entry = cur;
108905a8c414SAlan Cox 				return (TRUE);
109005a8c414SAlan Cox 			} else {
109105a8c414SAlan Cox 				if (cur->right == NULL) {
109205a8c414SAlan Cox 					*entry = cur;
109305a8c414SAlan Cox 					break;
109405a8c414SAlan Cox 				}
109505a8c414SAlan Cox 				cur = cur->right;
109605a8c414SAlan Cox 			}
10974e94f402SAlan Cox 		}
1098df8bae1dSRodney W. Grimes 	return (FALSE);
1099df8bae1dSRodney W. Grimes }
1100df8bae1dSRodney W. Grimes 
1101df8bae1dSRodney W. Grimes /*
110230dcfc09SJohn Dyson  *	vm_map_insert:
110330dcfc09SJohn Dyson  *
110430dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
110530dcfc09SJohn Dyson  *	map at the specified address range.  The object's
110630dcfc09SJohn Dyson  *	size should match that of the address range.
110730dcfc09SJohn Dyson  *
110830dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
11092aaeadf8SMatthew Dillon  *
11102aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11112aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
111230dcfc09SJohn Dyson  */
111330dcfc09SJohn Dyson int
1114b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1115b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1116b9dcd593SBruce Evans 	      int cow)
111730dcfc09SJohn Dyson {
1118c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1119c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
112030dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
11219730a5daSPaul Saab 	vm_eflags_t protoeflags;
1122ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
11238211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
11243364c323SKonstantin Belousov 	boolean_t charge_prev_obj;
112530dcfc09SJohn Dyson 
11263a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
11273a0916b8SKonstantin Belousov 
112830dcfc09SJohn Dyson 	/*
112930dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
113030dcfc09SJohn Dyson 	 */
113130dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
113230dcfc09SJohn Dyson 	    (start >= end))
113330dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
113430dcfc09SJohn Dyson 
113530dcfc09SJohn Dyson 	/*
113630dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
113730dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
113830dcfc09SJohn Dyson 	 */
113930dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
114030dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
114130dcfc09SJohn Dyson 
114230dcfc09SJohn Dyson 	prev_entry = temp_entry;
114330dcfc09SJohn Dyson 
114430dcfc09SJohn Dyson 	/*
114530dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
114630dcfc09SJohn Dyson 	 */
114730dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
114830dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
114930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
115030dcfc09SJohn Dyson 
1151afa07f7eSJohn Dyson 	protoeflags = 0;
11523364c323SKonstantin Belousov 	charge_prev_obj = FALSE;
1153afa07f7eSJohn Dyson 
1154afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1155e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1156afa07f7eSJohn Dyson 
11574e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1158afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1159afa07f7eSJohn Dyson 
11604e045f93SAlan Cox 		KASSERT(object == NULL,
11614e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
11624e045f93SAlan Cox 	}
11634f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11644f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11659730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11669730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
116784110e7eSKonstantin Belousov 	if (cow & MAP_VN_WRITECOUNT)
116884110e7eSKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
11698211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
11708211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
11718211bd45SKonstantin Belousov 	else
11728211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
11734f79d873SMatthew Dillon 
1174ef694c1aSEdward Tomasz Napierala 	cred = NULL;
11753364c323SKonstantin Belousov 	KASSERT((object != kmem_object && object != kernel_object) ||
11763364c323SKonstantin Belousov 	    ((object == kmem_object || object == kernel_object) &&
11773364c323SKonstantin Belousov 		!(protoeflags & MAP_ENTRY_NEEDS_COPY)),
11783364c323SKonstantin Belousov 	    ("kmem or kernel object and cow"));
11793364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
11803364c323SKonstantin Belousov 		goto charged;
11813364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
11823364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
11833364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
11843364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
118541c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1186ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
11873364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
1188ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
1189ef694c1aSEdward Tomasz Napierala 		crhold(cred);
11903364c323SKonstantin Belousov 		if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
11913364c323SKonstantin Belousov 			charge_prev_obj = TRUE;
11923364c323SKonstantin Belousov 	}
11933364c323SKonstantin Belousov 
11943364c323SKonstantin Belousov charged:
1195f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1196f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1197f8616ebfSAlan Cox 		pmap_growkernel(end);
11981d284e00SAlan Cox 	if (object != NULL) {
119930dcfc09SJohn Dyson 		/*
12001d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
12011d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
12021d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
12031d284e00SAlan Cox 		 * reference counting is insufficient to recognize
12041d284e00SAlan Cox 		 * aliases with precision.)
120530dcfc09SJohn Dyson 		 */
120689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
12071d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
12082aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
120989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
12104e045f93SAlan Cox 	}
12114e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
12124e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
1213b61a53d4SKonstantin Belousov 		 (cow & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) == 0 &&
12148cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
12154e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
1216ef694c1aSEdward Tomasz Napierala 		 (prev_entry->cred == cred ||
12173364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
1218ef694c1aSEdward Tomasz Napierala 		   (prev_entry->object.vm_object->cred == cred))) &&
12198cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
122057a21abaSAlan Cox 		       prev_entry->offset,
12218cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
12223364c323SKonstantin Belousov 		       (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
122330dcfc09SJohn Dyson 		/*
12242aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
12252aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
12262aaeadf8SMatthew Dillon 		 * new range as well.
122730dcfc09SJohn Dyson 		 */
12288211bd45SKonstantin Belousov 		if ((prev_entry->inheritance == inheritance) &&
12298cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
12308cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
123130dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
123230dcfc09SJohn Dyson 			prev_entry->end = end;
12330164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
12344e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
1235ef694c1aSEdward Tomasz Napierala 			if (cred != NULL)
1236ef694c1aSEdward Tomasz Napierala 				crfree(cred);
123730dcfc09SJohn Dyson 			return (KERN_SUCCESS);
123830dcfc09SJohn Dyson 		}
12398cc7e047SJohn Dyson 
12402aaeadf8SMatthew Dillon 		/*
12412aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
12422aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
12432aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
12444e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
12452aaeadf8SMatthew Dillon 		 */
12462aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
12472aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
12482aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
12498cc7e047SJohn Dyson 		vm_object_reference(object);
1250ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
12513364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
12523364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1253ef694c1aSEdward Tomasz Napierala 			crfree(cred);
1254ef694c1aSEdward Tomasz Napierala 			cred = NULL;
12553364c323SKonstantin Belousov 		}
1256b18bfc3dSJohn Dyson 	}
12572aaeadf8SMatthew Dillon 
12582aaeadf8SMatthew Dillon 	/*
12592aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
12602aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
12612aaeadf8SMatthew Dillon 	 * backing objects.
12622aaeadf8SMatthew Dillon 	 */
12638cc7e047SJohn Dyson 
126430dcfc09SJohn Dyson 	/*
126530dcfc09SJohn Dyson 	 * Create a new entry
126630dcfc09SJohn Dyson 	 */
126730dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
126830dcfc09SJohn Dyson 	new_entry->start = start;
126930dcfc09SJohn Dyson 	new_entry->end = end;
1270ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
127130dcfc09SJohn Dyson 
1272afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
127330dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
127430dcfc09SJohn Dyson 	new_entry->offset = offset;
12752267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12762267af78SJulian Elischer 
12778211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
127830dcfc09SJohn Dyson 	new_entry->protection = prot;
127930dcfc09SJohn Dyson 	new_entry->max_protection = max;
128030dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1281997ac690SKonstantin Belousov 	new_entry->wiring_thread = NULL;
128213458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
128313458803SAlan Cox 	new_entry->next_read = OFF_TO_IDX(offset);
1284e5f251d2SAlan Cox 
1285ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
12863364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1287ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
12883364c323SKonstantin Belousov 
128930dcfc09SJohn Dyson 	/*
129030dcfc09SJohn Dyson 	 * Insert the new entry into the list
129130dcfc09SJohn Dyson 	 */
129230dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
129330dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
129430dcfc09SJohn Dyson 
12951a484d28SMatthew Dillon 	/*
1296d2a444c0SAlan Cox 	 * It may be possible to merge the new entry with the next and/or
1297d2a444c0SAlan Cox 	 * previous entries.  However, due to MAP_STACK_* being a hack, a
1298d2a444c0SAlan Cox 	 * panic can result from merging such entries.
12991a484d28SMatthew Dillon 	 */
1300d2a444c0SAlan Cox 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0)
13014e71e795SMatthew Dillon 		vm_map_simplify_entry(map, new_entry);
13024e71e795SMatthew Dillon 
13034f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
13044da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1305e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1306e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
13074f79d873SMatthew Dillon 	}
1308e972780aSAlan Cox 
130930dcfc09SJohn Dyson 	return (KERN_SUCCESS);
131030dcfc09SJohn Dyson }
131130dcfc09SJohn Dyson 
131230dcfc09SJohn Dyson /*
13130164e057SAlan Cox  *	vm_map_findspace:
13140164e057SAlan Cox  *
13150164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
13160164e057SAlan Cox  *	beginning at address >= start in the given map.
13170164e057SAlan Cox  *
13180164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
13190164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
13200164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
13210164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
13220164e057SAlan Cox  *	O(log n) amortized with splay trees.
13230164e057SAlan Cox  *
13240164e057SAlan Cox  *	The map must be locked, and leaves it so.
13250164e057SAlan Cox  *
13260164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
13270164e057SAlan Cox  *		 1 if insufficient space.
1328df8bae1dSRodney W. Grimes  */
1329df8bae1dSRodney W. Grimes int
13300164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13310164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1332df8bae1dSRodney W. Grimes {
13330164e057SAlan Cox 	vm_map_entry_t entry;
1334f8616ebfSAlan Cox 	vm_offset_t st;
1335df8bae1dSRodney W. Grimes 
1336986b43f8SAlan Cox 	/*
1337986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1338986b43f8SAlan Cox 	 * address wrap.
1339986b43f8SAlan Cox 	 */
1340df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1341df8bae1dSRodney W. Grimes 		start = map->min_offset;
1342986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1343df8bae1dSRodney W. Grimes 		return (1);
1344df8bae1dSRodney W. Grimes 
13450164e057SAlan Cox 	/* Empty tree means wide open address space. */
13460164e057SAlan Cox 	if (map->root == NULL) {
1347df8bae1dSRodney W. Grimes 		*addr = start;
1348f8616ebfSAlan Cox 		return (0);
134999448ed1SJohn Dyson 	}
13500164e057SAlan Cox 
13510164e057SAlan Cox 	/*
13520164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13530164e057SAlan Cox 	 * must be a gap from start to the root.
13540164e057SAlan Cox 	 */
13550164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13560164e057SAlan Cox 	if (start + length <= map->root->start) {
13570164e057SAlan Cox 		*addr = start;
1358f8616ebfSAlan Cox 		return (0);
13590164e057SAlan Cox 	}
13600164e057SAlan Cox 
13610164e057SAlan Cox 	/*
13620164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1363986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1364986b43f8SAlan Cox 	 * wrap might be a problem.
13650164e057SAlan Cox 	 */
13660164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1367986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13680164e057SAlan Cox 		*addr = st;
1369f8616ebfSAlan Cox 		return (0);
13700164e057SAlan Cox 	}
13710164e057SAlan Cox 
13720164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13730164e057SAlan Cox 	entry = map->root->right;
13740164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13750164e057SAlan Cox 		return (1);
13760164e057SAlan Cox 
13770164e057SAlan Cox 	/*
13780164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13790164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13800164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13810164e057SAlan Cox 	 */
13820164e057SAlan Cox 	while (entry != NULL) {
13830164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
13840164e057SAlan Cox 			entry = entry->left;
13850164e057SAlan Cox 		else if (entry->adj_free >= length) {
13860164e057SAlan Cox 			*addr = entry->end;
1387f8616ebfSAlan Cox 			return (0);
13880164e057SAlan Cox 		} else
13890164e057SAlan Cox 			entry = entry->right;
13900164e057SAlan Cox 	}
13910164e057SAlan Cox 
13920164e057SAlan Cox 	/* Can't get here, so panic if we do. */
13930164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1394df8bae1dSRodney W. Grimes }
1395df8bae1dSRodney W. Grimes 
1396d239bd3cSKonstantin Belousov int
1397d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1398b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1399d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1400d239bd3cSKonstantin Belousov {
1401b8ca4ef2SAlan Cox 	vm_offset_t end;
1402d239bd3cSKonstantin Belousov 	int result;
1403d239bd3cSKonstantin Belousov 
1404d239bd3cSKonstantin Belousov 	end = start + length;
1405*4648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1406*4648ba0aSKonstantin Belousov 	    object == NULL,
1407*4648ba0aSKonstantin Belousov 	    ("vm_map_fixed: non-NULL backing object for stack"));
1408897d81a0SKonstantin Belousov 	vm_map_lock(map);
1409d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1410655c3490SKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1411*4648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1412*4648ba0aSKonstantin Belousov 		result = vm_map_stack_locked(map, start, length, sgrowsiz,
1413*4648ba0aSKonstantin Belousov 		    prot, max, cow);
1414*4648ba0aSKonstantin Belousov 	} else {
1415*4648ba0aSKonstantin Belousov 		result = vm_map_insert(map, object, offset, start, end,
1416*4648ba0aSKonstantin Belousov 		    prot, max, cow);
1417*4648ba0aSKonstantin Belousov 	}
1418d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1419d239bd3cSKonstantin Belousov 	return (result);
1420d239bd3cSKonstantin Belousov }
1421d239bd3cSKonstantin Belousov 
1422df8bae1dSRodney W. Grimes /*
1423df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1424df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1425df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1426df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1427df8bae1dSRodney W. Grimes  *
14282aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
14292aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1430df8bae1dSRodney W. Grimes  */
1431df8bae1dSRodney W. Grimes int
1432b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1433b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1434edb572a3SJohn Baldwin 	    vm_size_t length, vm_offset_t max_addr, int find_space,
1435edb572a3SJohn Baldwin 	    vm_prot_t prot, vm_prot_t max, int cow)
1436df8bae1dSRodney W. Grimes {
14375aa60b6fSJohn Baldwin 	vm_offset_t alignment, initial_addr, start;
14386eaee3feSAlan Cox 	int result;
1439df8bae1dSRodney W. Grimes 
1440*4648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
1441*4648ba0aSKonstantin Belousov 	    object == NULL,
1442*4648ba0aSKonstantin Belousov 	    ("vm_map_find: non-NULL backing object for stack"));
1443ff74a3faSJohn Baldwin 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1444ff74a3faSJohn Baldwin 	    (object->flags & OBJ_COLORED) == 0))
1445ff74a3faSJohn Baldwin 		find_space = VMFS_ANY_SPACE;
14465aa60b6fSJohn Baldwin 	if (find_space >> 8 != 0) {
14475aa60b6fSJohn Baldwin 		KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
14485aa60b6fSJohn Baldwin 		alignment = (vm_offset_t)1 << (find_space >> 8);
14495aa60b6fSJohn Baldwin 	} else
14505aa60b6fSJohn Baldwin 		alignment = 0;
1451ff74a3faSJohn Baldwin 	initial_addr = *addr;
1452ff74a3faSJohn Baldwin again:
1453ff74a3faSJohn Baldwin 	start = initial_addr;
1454bea41bcfSDavid Greenman 	vm_map_lock(map);
145526c538ffSAlan Cox 	do {
145626c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1457edb572a3SJohn Baldwin 			if (vm_map_findspace(map, start, length, addr) ||
1458edb572a3SJohn Baldwin 			    (max_addr != 0 && *addr + length > max_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) {
14675aa60b6fSJohn Baldwin 			case VMFS_SUPER_SPACE:
1468ff74a3faSJohn Baldwin 			case VMFS_OPTIMAL_SPACE:
146926c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
147026c538ffSAlan Cox 				    length);
1471ca596a25SJuli Mallett 				break;
14725aa60b6fSJohn Baldwin 			case VMFS_ANY_SPACE:
14735aa60b6fSJohn Baldwin 				break;
1474ca596a25SJuli Mallett 			default:
14755aa60b6fSJohn Baldwin 				if ((*addr & (alignment - 1)) != 0) {
14765aa60b6fSJohn Baldwin 					*addr &= ~(alignment - 1);
14775aa60b6fSJohn Baldwin 					*addr += alignment;
14785aa60b6fSJohn Baldwin 				}
1479ca596a25SJuli Mallett 				break;
1480ca596a25SJuli Mallett 			}
1481ca596a25SJuli Mallett 
1482df8bae1dSRodney W. Grimes 			start = *addr;
1483df8bae1dSRodney W. Grimes 		}
1484*4648ba0aSKonstantin Belousov 		if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1485*4648ba0aSKonstantin Belousov 			result = vm_map_stack_locked(map, start, length,
1486*4648ba0aSKonstantin Belousov 			    sgrowsiz, prot, max, cow);
1487*4648ba0aSKonstantin Belousov 		} else {
1488*4648ba0aSKonstantin Belousov 			result = vm_map_insert(map, object, offset, start,
1489*4648ba0aSKonstantin Belousov 			    start + length, prot, max, cow);
1490*4648ba0aSKonstantin Belousov 		}
14915aa60b6fSJohn Baldwin 	} while (result == KERN_NO_SPACE && find_space != VMFS_NO_SPACE &&
14925aa60b6fSJohn Baldwin 	    find_space != VMFS_ANY_SPACE);
1493df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1494df8bae1dSRodney W. Grimes 	return (result);
1495df8bae1dSRodney W. Grimes }
1496df8bae1dSRodney W. Grimes 
1497df8bae1dSRodney W. Grimes /*
1498b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
149967bf6868SJohn Dyson  *
15004e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
15014e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
15024e71e795SMatthew Dillon  *
15034e71e795SMatthew Dillon  *	The map must be locked.
15044e71e795SMatthew Dillon  *
15054e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
15064e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
15074e71e795SMatthew Dillon  *	both neighbors.
1508df8bae1dSRodney W. Grimes  */
1509b7b2aac2SJohn Dyson void
15101b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1511df8bae1dSRodney W. Grimes {
1512308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1513b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1514df8bae1dSRodney W. Grimes 
1515acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1516df8bae1dSRodney W. Grimes 		return;
1517308c24baSJohn Dyson 
1518308c24baSJohn Dyson 	prev = entry->prev;
1519308c24baSJohn Dyson 	if (prev != &map->header) {
152067bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
152167bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
152267bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
152395e5e988SJohn Dyson 		     (!prev->object.vm_object ||
152467bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1525afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
152667bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
152767bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
152867bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
15293364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
1530ef694c1aSEdward Tomasz Napierala 		     (prev->cred == entry->cred)) {
1531308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1532308c24baSJohn Dyson 			entry->start = prev->start;
1533308c24baSJohn Dyson 			entry->offset = prev->offset;
15340164e057SAlan Cox 			if (entry->prev != &map->header)
15350164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
15367fd10fb3SKonstantin Belousov 
15377fd10fb3SKonstantin Belousov 			/*
1538b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1539b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1540b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1541b0994946SKonstantin Belousov 			 * because the vnode has additional
1542b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1543b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1544b0994946SKonstantin Belousov 			 * the vnode lock.
154584110e7eSKonstantin Belousov 			 *
154684110e7eSKonstantin Belousov 			 * Since we count the number of virtual page
154784110e7eSKonstantin Belousov 			 * mappings in object->un_pager.vnp.writemappings,
154884110e7eSKonstantin Belousov 			 * the writemappings value should not be adjusted
154984110e7eSKonstantin Belousov 			 * when the entry is disposed of.
15507fd10fb3SKonstantin Belousov 			 */
1551b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1552308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1553ef694c1aSEdward Tomasz Napierala 			if (prev->cred != NULL)
1554ef694c1aSEdward Tomasz Napierala 				crfree(prev->cred);
1555308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1556308c24baSJohn Dyson 		}
1557308c24baSJohn Dyson 	}
1558de5f6a77SJohn Dyson 
1559de5f6a77SJohn Dyson 	next = entry->next;
1560308c24baSJohn Dyson 	if (next != &map->header) {
156167bf6868SJohn Dyson 		esize = entry->end - entry->start;
156267bf6868SJohn Dyson 		if ((entry->end == next->start) &&
156367bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
156467bf6868SJohn Dyson 		     (!entry->object.vm_object ||
156567bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1566afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
156767bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
156867bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
156967bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
15703364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
1571ef694c1aSEdward Tomasz Napierala 		    (next->cred == entry->cred)) {
1572de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1573de5f6a77SJohn Dyson 			entry->end = next->end;
15740164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
15757fd10fb3SKonstantin Belousov 
15767fd10fb3SKonstantin Belousov 			/*
15777fd10fb3SKonstantin Belousov 			 * See comment above.
15787fd10fb3SKonstantin Belousov 			 */
1579b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1580de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1581ef694c1aSEdward Tomasz Napierala 			if (next->cred != NULL)
1582ef694c1aSEdward Tomasz Napierala 				crfree(next->cred);
1583de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1584df8bae1dSRodney W. Grimes 		}
1585df8bae1dSRodney W. Grimes 	}
1586de5f6a77SJohn Dyson }
1587df8bae1dSRodney W. Grimes /*
1588df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1589df8bae1dSRodney W. Grimes  *
1590df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1591df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1592df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1593df8bae1dSRodney W. Grimes  */
1594df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1595df8bae1dSRodney W. Grimes { \
1596df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1597df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1598df8bae1dSRodney W. Grimes }
1599df8bae1dSRodney W. Grimes 
1600df8bae1dSRodney W. Grimes /*
1601df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1602df8bae1dSRodney W. Grimes  *	the entry must be split.
1603df8bae1dSRodney W. Grimes  */
16040d94caffSDavid Greenman static void
16051b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1606df8bae1dSRodney W. Grimes {
1607c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1608df8bae1dSRodney W. Grimes 
16093a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16103a0916b8SKonstantin Belousov 
1611df8bae1dSRodney W. Grimes 	/*
16120d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
16130d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
16140d94caffSDavid Greenman 	 * starting address.
1615df8bae1dSRodney W. Grimes 	 */
1616f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1617f32dbbeeSJohn Dyson 
161811cccda1SJohn Dyson 	/*
161911cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
162011cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
162111cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
162211cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
162311cccda1SJohn Dyson 	 * put this improvement.
162411cccda1SJohn Dyson 	 */
16254e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
162611cccda1SJohn Dyson 		vm_object_t object;
162711cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1628c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
162911cccda1SJohn Dyson 		entry->object.vm_object = object;
163011cccda1SJohn Dyson 		entry->offset = 0;
1631ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1632ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
16333364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1634ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
16353364c323SKonstantin Belousov 		}
16363364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16373364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1638ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
163989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1640ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1641ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1642ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
16433364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
164489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1645ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
164611cccda1SJohn Dyson 	}
164711cccda1SJohn Dyson 
1648df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1649df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1650df8bae1dSRodney W. Grimes 
1651df8bae1dSRodney W. Grimes 	new_entry->end = start;
1652df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1653df8bae1dSRodney W. Grimes 	entry->start = start;
1654ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1655ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1656df8bae1dSRodney W. Grimes 
1657df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1658df8bae1dSRodney W. Grimes 
16599fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1660df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
166184110e7eSKonstantin Belousov 		/*
166284110e7eSKonstantin Belousov 		 * The object->un_pager.vnp.writemappings for the
166384110e7eSKonstantin Belousov 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
166484110e7eSKonstantin Belousov 		 * kept as is here.  The virtual pages are
166584110e7eSKonstantin Belousov 		 * re-distributed among the clipped entries, so the sum is
166684110e7eSKonstantin Belousov 		 * left the same.
166784110e7eSKonstantin Belousov 		 */
1668df8bae1dSRodney W. Grimes 	}
1669c0877f10SJohn Dyson }
1670df8bae1dSRodney W. Grimes 
1671df8bae1dSRodney W. Grimes /*
1672df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1673df8bae1dSRodney W. Grimes  *
1674df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1675df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1676df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1677df8bae1dSRodney W. Grimes  */
1678df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1679df8bae1dSRodney W. Grimes { \
1680af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1681af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1682df8bae1dSRodney W. Grimes }
1683df8bae1dSRodney W. Grimes 
1684df8bae1dSRodney W. Grimes /*
1685df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1686df8bae1dSRodney W. Grimes  *	the entry must be split.
1687df8bae1dSRodney W. Grimes  */
16880d94caffSDavid Greenman static void
16891b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1690df8bae1dSRodney W. Grimes {
1691c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1692df8bae1dSRodney W. Grimes 
16933a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16943a0916b8SKonstantin Belousov 
1695df8bae1dSRodney W. Grimes 	/*
169611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
169711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
169811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
169911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
170011cccda1SJohn Dyson 	 * put this improvement.
170111cccda1SJohn Dyson 	 */
17024e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
170311cccda1SJohn Dyson 		vm_object_t object;
170411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1705c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
170611cccda1SJohn Dyson 		entry->object.vm_object = object;
170711cccda1SJohn Dyson 		entry->offset = 0;
1708ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1709ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
17103364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1711ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
17123364c323SKonstantin Belousov 		}
17133364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
17143364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1715ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
171689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1717ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1718ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1719ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
17203364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
172189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1722ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
172311cccda1SJohn Dyson 	}
172411cccda1SJohn Dyson 
172511cccda1SJohn Dyson 	/*
17260d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1727df8bae1dSRodney W. Grimes 	 */
1728df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1729df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1730df8bae1dSRodney W. Grimes 
1731df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1732df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1733ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1734ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1735df8bae1dSRodney W. Grimes 
1736df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1737df8bae1dSRodney W. Grimes 
17389fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1739df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1740df8bae1dSRodney W. Grimes 	}
1741c0877f10SJohn Dyson }
1742df8bae1dSRodney W. Grimes 
1743df8bae1dSRodney W. Grimes /*
1744df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1745df8bae1dSRodney W. Grimes  *
1746df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1747df8bae1dSRodney W. Grimes  *
1748df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1749df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1750df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1751df8bae1dSRodney W. Grimes  *
1752df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1753df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1754df8bae1dSRodney W. Grimes  *		vm_fault
1755df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1756df8bae1dSRodney W. Grimes  *
1757df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1758df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1759df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1760df8bae1dSRodney W. Grimes  */
1761df8bae1dSRodney W. Grimes int
17621b40f8c0SMatthew Dillon vm_map_submap(
17631b40f8c0SMatthew Dillon 	vm_map_t map,
17641b40f8c0SMatthew Dillon 	vm_offset_t start,
17651b40f8c0SMatthew Dillon 	vm_offset_t end,
17661b40f8c0SMatthew Dillon 	vm_map_t submap)
1767df8bae1dSRodney W. Grimes {
1768df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1769c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1770df8bae1dSRodney W. Grimes 
1771df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1772df8bae1dSRodney W. Grimes 
1773df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1774df8bae1dSRodney W. Grimes 
1775df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1776df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17770d94caffSDavid Greenman 	} else
1778df8bae1dSRodney W. Grimes 		entry = entry->next;
1779df8bae1dSRodney W. Grimes 
1780df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
17839fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1784afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17852d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1786afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1787df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1788df8bae1dSRodney W. Grimes 	}
1789df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1790df8bae1dSRodney W. Grimes 
1791df8bae1dSRodney W. Grimes 	return (result);
1792df8bae1dSRodney W. Grimes }
1793df8bae1dSRodney W. Grimes 
1794df8bae1dSRodney W. Grimes /*
1795dd05fa19SAlan Cox  * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
17961f78f902SAlan Cox  */
17971f78f902SAlan Cox #define	MAX_INIT_PT	96
17981f78f902SAlan Cox 
17991f78f902SAlan Cox /*
18000551c08dSAlan Cox  *	vm_map_pmap_enter:
18010551c08dSAlan Cox  *
1802dd05fa19SAlan Cox  *	Preload the specified map's pmap with mappings to the specified
1803dd05fa19SAlan Cox  *	object's memory-resident pages.  No further physical pages are
1804dd05fa19SAlan Cox  *	allocated, and no further virtual pages are retrieved from secondary
1805dd05fa19SAlan Cox  *	storage.  If the specified flags include MAP_PREFAULT_PARTIAL, then a
1806dd05fa19SAlan Cox  *	limited number of page mappings are created at the low-end of the
1807dd05fa19SAlan Cox  *	specified address range.  (For this purpose, a superpage mapping
1808dd05fa19SAlan Cox  *	counts as one page mapping.)  Otherwise, all resident pages within
1809dd05fa19SAlan Cox  *	the specified address range are mapped.  Because these mappings are
1810dd05fa19SAlan Cox  *	being created speculatively, cached pages are not reactivated and
1811a922d312SAlan Cox  *	mapped.
18120551c08dSAlan Cox  */
18130551c08dSAlan Cox void
18144da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
18150551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
18160551c08dSAlan Cox {
18178fece8c3SAlan Cox 	vm_offset_t start;
1818ce142d9eSAlan Cox 	vm_page_t p, p_start;
1819dd05fa19SAlan Cox 	vm_pindex_t mask, psize, threshold, tmpidx;
18200551c08dSAlan Cox 
1821ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
18221f78f902SAlan Cox 		return;
18239af6d512SAttilio Rao 	VM_OBJECT_RLOCK(object);
18249af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18259af6d512SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
182689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
182701381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18289af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
18299af6d512SAttilio Rao 			    size);
18309af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18319af6d512SAttilio Rao 			return;
18329af6d512SAttilio Rao 		}
18339af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
18341f78f902SAlan Cox 	}
18351f78f902SAlan Cox 
18361f78f902SAlan Cox 	psize = atop(size);
18371f78f902SAlan Cox 	if (psize + pindex > object->size) {
18389af6d512SAttilio Rao 		if (object->size < pindex) {
18399af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
18409af6d512SAttilio Rao 			return;
18419af6d512SAttilio Rao 		}
18421f78f902SAlan Cox 		psize = object->size - pindex;
18431f78f902SAlan Cox 	}
18441f78f902SAlan Cox 
1845ce142d9eSAlan Cox 	start = 0;
1846ce142d9eSAlan Cox 	p_start = NULL;
1847dd05fa19SAlan Cox 	threshold = MAX_INIT_PT;
18481f78f902SAlan Cox 
1849b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
18501f78f902SAlan Cox 	/*
18511f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
18521f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
18531f78f902SAlan Cox 	 * or (2) NULL.
18541f78f902SAlan Cox 	 */
18551f78f902SAlan Cox 	for (;
18561f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
18571f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
18581f78f902SAlan Cox 		/*
18591f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
18601f78f902SAlan Cox 		 * free pages allocating pv entries.
18611f78f902SAlan Cox 		 */
1862dd05fa19SAlan Cox 		if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
1863dd05fa19SAlan Cox 		    vm_cnt.v_free_count < vm_cnt.v_free_reserved) ||
1864dd05fa19SAlan Cox 		    ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
1865dd05fa19SAlan Cox 		    tmpidx >= threshold)) {
1866379fb642SAlan Cox 			psize = tmpidx;
18671f78f902SAlan Cox 			break;
18681f78f902SAlan Cox 		}
18690a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1870ce142d9eSAlan Cox 			if (p_start == NULL) {
1871ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1872ce142d9eSAlan Cox 				p_start = p;
1873ce142d9eSAlan Cox 			}
1874dd05fa19SAlan Cox 			/* Jump ahead if a superpage mapping is possible. */
1875dd05fa19SAlan Cox 			if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
1876dd05fa19SAlan Cox 			    (pagesizes[p->psind] - 1)) == 0) {
1877dd05fa19SAlan Cox 				mask = atop(pagesizes[p->psind]) - 1;
1878dd05fa19SAlan Cox 				if (tmpidx + mask < psize &&
1879dd05fa19SAlan Cox 				    vm_page_ps_is_valid(p)) {
1880dd05fa19SAlan Cox 					p += mask;
1881dd05fa19SAlan Cox 					threshold += mask;
1882dd05fa19SAlan Cox 				}
1883dd05fa19SAlan Cox 			}
18847bfda801SAlan Cox 		} else if (p_start != NULL) {
1885cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1886cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1887cf4682aeSAlan Cox 			p_start = NULL;
1888cf4682aeSAlan Cox 		}
1889cf4682aeSAlan Cox 	}
1890c46b90e9SAlan Cox 	if (p_start != NULL)
1891379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1892379fb642SAlan Cox 		    p_start, prot);
18939af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
18940551c08dSAlan Cox }
18950551c08dSAlan Cox 
18960551c08dSAlan Cox /*
1897df8bae1dSRodney W. Grimes  *	vm_map_protect:
1898df8bae1dSRodney W. Grimes  *
1899df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1900df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1901df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1902df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1903df8bae1dSRodney W. Grimes  */
1904df8bae1dSRodney W. Grimes int
1905b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1906b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1907df8bae1dSRodney W. Grimes {
1908210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
19093364c323SKonstantin Belousov 	vm_object_t obj;
1910ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
1911210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1912df8bae1dSRodney W. Grimes 
191379e9451fSKonstantin Belousov 	if (start == end)
191479e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
191579e9451fSKonstantin Belousov 
1916df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1917df8bae1dSRodney W. Grimes 
1918df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1919df8bae1dSRodney W. Grimes 
1920df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1921df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1922b7b2aac2SJohn Dyson 	} else {
1923df8bae1dSRodney W. Grimes 		entry = entry->next;
1924b7b2aac2SJohn Dyson 	}
1925df8bae1dSRodney W. Grimes 
1926df8bae1dSRodney W. Grimes 	/*
19270d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1928df8bae1dSRodney W. Grimes 	 */
1929df8bae1dSRodney W. Grimes 	current = entry;
1930df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1931afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1932a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1933df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1934a1f6d91cSDavid Greenman 		}
1935df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1936df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1937df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1938df8bae1dSRodney W. Grimes 		}
1939df8bae1dSRodney W. Grimes 		current = current->next;
1940df8bae1dSRodney W. Grimes 	}
1941df8bae1dSRodney W. Grimes 
19423364c323SKonstantin Belousov 
19433364c323SKonstantin Belousov 	/*
19443364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
19453364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
19463364c323SKonstantin Belousov 	 * breakpoint on text segment)
19473364c323SKonstantin Belousov 	 */
19483364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
19493364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
19503364c323SKonstantin Belousov 
19513364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
19523364c323SKonstantin Belousov 
19533364c323SKonstantin Belousov 		if (set_max ||
19543364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
19553364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
19563364c323SKonstantin Belousov 			continue;
19573364c323SKonstantin Belousov 		}
19583364c323SKonstantin Belousov 
1959ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
19603364c323SKonstantin Belousov 		obj = current->object.vm_object;
19613364c323SKonstantin Belousov 
19623364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
19633364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
19643364c323SKonstantin Belousov 				vm_map_unlock(map);
19653364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
19663364c323SKonstantin Belousov 			}
1967ef694c1aSEdward Tomasz Napierala 			crhold(cred);
1968ef694c1aSEdward Tomasz Napierala 			current->cred = cred;
19693364c323SKonstantin Belousov 			continue;
19703364c323SKonstantin Belousov 		}
19713364c323SKonstantin Belousov 
197289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
19733364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
197489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19753364c323SKonstantin Belousov 			continue;
19763364c323SKonstantin Belousov 		}
19773364c323SKonstantin Belousov 
19783364c323SKonstantin Belousov 		/*
19793364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
19803364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
19813364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
19823364c323SKonstantin Belousov 		 */
19833364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
19843d95614fSKonstantin Belousov 		    ("vm_map_protect: object %p overcharged (entry %p)",
19853d95614fSKonstantin Belousov 		    obj, current));
19863364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
198789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19883364c323SKonstantin Belousov 			vm_map_unlock(map);
19893364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
19903364c323SKonstantin Belousov 		}
19913364c323SKonstantin Belousov 
1992ef694c1aSEdward Tomasz Napierala 		crhold(cred);
1993ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
19943364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
199589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
19963364c323SKonstantin Belousov 	}
19973364c323SKonstantin Belousov 
1998df8bae1dSRodney W. Grimes 	/*
19990d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
20000d94caffSDavid Greenman 	 * necessary the second time.]
2001df8bae1dSRodney W. Grimes 	 */
2002df8bae1dSRodney W. Grimes 	current = entry;
2003df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
2004df8bae1dSRodney W. Grimes 		old_prot = current->protection;
2005210a6886SKonstantin Belousov 
2006df8bae1dSRodney W. Grimes 		if (set_max)
2007df8bae1dSRodney W. Grimes 			current->protection =
2008df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
2009df8bae1dSRodney W. Grimes 			    old_prot;
2010df8bae1dSRodney W. Grimes 		else
2011df8bae1dSRodney W. Grimes 			current->protection = new_prot;
2012df8bae1dSRodney W. Grimes 
2013dd006a1bSAlan Cox 		/*
2014dd006a1bSAlan Cox 		 * For user wired map entries, the normal lazy evaluation of
2015dd006a1bSAlan Cox 		 * write access upgrades through soft page faults is
2016dd006a1bSAlan Cox 		 * undesirable.  Instead, immediately copy any pages that are
2017dd006a1bSAlan Cox 		 * copy-on-write and enable write access in the physical map.
2018dd006a1bSAlan Cox 		 */
2019dd006a1bSAlan Cox 		if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2020210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
20215930251aSKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0)
2022210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
2023210a6886SKonstantin Belousov 
2024df8bae1dSRodney W. Grimes 		/*
20252fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
20262fafce9eSAlan Cox 		 * about copy-on-write here.
2027df8bae1dSRodney W. Grimes 		 */
20282fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
2029afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2030df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
2031df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
2032df8bae1dSRodney W. Grimes 			    current->end,
20331c85e3dfSAlan Cox 			    current->protection & MASK(current));
2034df8bae1dSRodney W. Grimes #undef	MASK
2035df8bae1dSRodney W. Grimes 		}
20367d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
2037df8bae1dSRodney W. Grimes 		current = current->next;
2038df8bae1dSRodney W. Grimes 	}
2039df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2040df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2041df8bae1dSRodney W. Grimes }
2042df8bae1dSRodney W. Grimes 
2043df8bae1dSRodney W. Grimes /*
2044867a482dSJohn Dyson  *	vm_map_madvise:
2045867a482dSJohn Dyson  *
2046867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2047f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2048f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2049f7fc307aSAlan Cox  *	objects.
2050867a482dSJohn Dyson  */
2051b4309055SMatthew Dillon int
20521b40f8c0SMatthew Dillon vm_map_madvise(
20531b40f8c0SMatthew Dillon 	vm_map_t map,
20541b40f8c0SMatthew Dillon 	vm_offset_t start,
20551b40f8c0SMatthew Dillon 	vm_offset_t end,
20561b40f8c0SMatthew Dillon 	int behav)
2057867a482dSJohn Dyson {
2058f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
2059b4309055SMatthew Dillon 	int modify_map = 0;
2060867a482dSJohn Dyson 
2061b4309055SMatthew Dillon 	/*
2062b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2063b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2064b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2065b4309055SMatthew Dillon 	 * on the map.
2066b4309055SMatthew Dillon 	 */
2067b4309055SMatthew Dillon 	switch(behav) {
2068b4309055SMatthew Dillon 	case MADV_NORMAL:
2069b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2070b4309055SMatthew Dillon 	case MADV_RANDOM:
20714f79d873SMatthew Dillon 	case MADV_NOSYNC:
20724f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
20739730a5daSPaul Saab 	case MADV_NOCORE:
20749730a5daSPaul Saab 	case MADV_CORE:
207579e9451fSKonstantin Belousov 		if (start == end)
207679e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2077b4309055SMatthew Dillon 		modify_map = 1;
2078867a482dSJohn Dyson 		vm_map_lock(map);
2079b4309055SMatthew Dillon 		break;
2080b4309055SMatthew Dillon 	case MADV_WILLNEED:
2081b4309055SMatthew Dillon 	case MADV_DONTNEED:
2082b4309055SMatthew Dillon 	case MADV_FREE:
208379e9451fSKonstantin Belousov 		if (start == end)
208479e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2085f7fc307aSAlan Cox 		vm_map_lock_read(map);
2086b4309055SMatthew Dillon 		break;
2087b4309055SMatthew Dillon 	default:
2088b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
2089b4309055SMatthew Dillon 	}
2090b4309055SMatthew Dillon 
2091b4309055SMatthew Dillon 	/*
2092b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2093b4309055SMatthew Dillon 	 */
2094867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2095867a482dSJohn Dyson 
2096867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
2097f7fc307aSAlan Cox 		if (modify_map)
2098867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
2099b4309055SMatthew Dillon 	} else {
2100867a482dSJohn Dyson 		entry = entry->next;
2101b4309055SMatthew Dillon 	}
2102867a482dSJohn Dyson 
2103f7fc307aSAlan Cox 	if (modify_map) {
2104f7fc307aSAlan Cox 		/*
2105f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2106f7fc307aSAlan Cox 		 *
2107f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2108f7fc307aSAlan Cox 		 * limited to the specified address range.
2109f7fc307aSAlan Cox 		 */
2110867a482dSJohn Dyson 		for (current = entry;
2111867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2112b4309055SMatthew Dillon 		     current = current->next
2113b4309055SMatthew Dillon 		) {
2114f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2115867a482dSJohn Dyson 				continue;
2116fed9a903SJohn Dyson 
211747221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2118fed9a903SJohn Dyson 
2119f7fc307aSAlan Cox 			switch (behav) {
2120867a482dSJohn Dyson 			case MADV_NORMAL:
21217f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2122867a482dSJohn Dyson 				break;
2123867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
21247f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2125867a482dSJohn Dyson 				break;
2126867a482dSJohn Dyson 			case MADV_RANDOM:
21277f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2128867a482dSJohn Dyson 				break;
21294f79d873SMatthew Dillon 			case MADV_NOSYNC:
21304f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
21314f79d873SMatthew Dillon 				break;
21324f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
21334f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
21344f79d873SMatthew Dillon 				break;
21359730a5daSPaul Saab 			case MADV_NOCORE:
21369730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
21379730a5daSPaul Saab 				break;
21389730a5daSPaul Saab 			case MADV_CORE:
21399730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
21409730a5daSPaul Saab 				break;
2141867a482dSJohn Dyson 			default:
2142867a482dSJohn Dyson 				break;
2143867a482dSJohn Dyson 			}
2144f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2145867a482dSJohn Dyson 		}
2146867a482dSJohn Dyson 		vm_map_unlock(map);
2147b4309055SMatthew Dillon 	} else {
214892a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
2149f7fc307aSAlan Cox 
2150f7fc307aSAlan Cox 		/*
2151f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2152f7fc307aSAlan Cox 		 * vm_object.
2153f7fc307aSAlan Cox 		 *
2154f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2155f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2156f7fc307aSAlan Cox 		 */
2157f7fc307aSAlan Cox 		for (current = entry;
2158f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2159b4309055SMatthew Dillon 		     current = current->next
2160b4309055SMatthew Dillon 		) {
216151321f7cSAlan Cox 			vm_offset_t useEnd, useStart;
21625f99b57cSMatthew Dillon 
2163f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2164f7fc307aSAlan Cox 				continue;
2165f7fc307aSAlan Cox 
216692a59946SJohn Baldwin 			pstart = OFF_TO_IDX(current->offset);
216792a59946SJohn Baldwin 			pend = pstart + atop(current->end - current->start);
21685f99b57cSMatthew Dillon 			useStart = current->start;
216951321f7cSAlan Cox 			useEnd = current->end;
2170f7fc307aSAlan Cox 
2171f7fc307aSAlan Cox 			if (current->start < start) {
217292a59946SJohn Baldwin 				pstart += atop(start - current->start);
21735f99b57cSMatthew Dillon 				useStart = start;
2174f7fc307aSAlan Cox 			}
217551321f7cSAlan Cox 			if (current->end > end) {
217692a59946SJohn Baldwin 				pend -= atop(current->end - end);
217751321f7cSAlan Cox 				useEnd = end;
217851321f7cSAlan Cox 			}
2179f7fc307aSAlan Cox 
218092a59946SJohn Baldwin 			if (pstart >= pend)
2181f7fc307aSAlan Cox 				continue;
2182f7fc307aSAlan Cox 
218351321f7cSAlan Cox 			/*
218451321f7cSAlan Cox 			 * Perform the pmap_advise() before clearing
218551321f7cSAlan Cox 			 * PGA_REFERENCED in vm_page_advise().  Otherwise, a
218651321f7cSAlan Cox 			 * concurrent pmap operation, such as pmap_remove(),
218751321f7cSAlan Cox 			 * could clear a reference in the pmap and set
218851321f7cSAlan Cox 			 * PGA_REFERENCED on the page before the pmap_advise()
218951321f7cSAlan Cox 			 * had completed.  Consequently, the page would appear
219051321f7cSAlan Cox 			 * referenced based upon an old reference that
219151321f7cSAlan Cox 			 * occurred before this pmap_advise() ran.
219251321f7cSAlan Cox 			 */
219351321f7cSAlan Cox 			if (behav == MADV_DONTNEED || behav == MADV_FREE)
219451321f7cSAlan Cox 				pmap_advise(map->pmap, useStart, useEnd,
219551321f7cSAlan Cox 				    behav);
219651321f7cSAlan Cox 
219792a59946SJohn Baldwin 			vm_object_madvise(current->object.vm_object, pstart,
219892a59946SJohn Baldwin 			    pend, behav);
2199b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
22000551c08dSAlan Cox 				vm_map_pmap_enter(map,
22015f99b57cSMatthew Dillon 				    useStart,
22024da4d293SAlan Cox 				    current->protection,
2203f7fc307aSAlan Cox 				    current->object.vm_object,
220492a59946SJohn Baldwin 				    pstart,
220592a59946SJohn Baldwin 				    ptoa(pend - pstart),
2206e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2207b4309055SMatthew Dillon 				);
2208f7fc307aSAlan Cox 			}
2209f7fc307aSAlan Cox 		}
2210f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2211f7fc307aSAlan Cox 	}
2212b4309055SMatthew Dillon 	return (0);
2213867a482dSJohn Dyson }
2214867a482dSJohn Dyson 
2215867a482dSJohn Dyson 
2216867a482dSJohn Dyson /*
2217df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2218df8bae1dSRodney W. Grimes  *
2219df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2220df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2221df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2222e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2223df8bae1dSRodney W. Grimes  */
2224df8bae1dSRodney W. Grimes int
2225b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2226b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2227df8bae1dSRodney W. Grimes {
2228c0877f10SJohn Dyson 	vm_map_entry_t entry;
2229df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2230df8bae1dSRodney W. Grimes 
2231df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2232df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2233df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2234df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2235df8bae1dSRodney W. Grimes 		break;
2236df8bae1dSRodney W. Grimes 	default:
2237df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2238df8bae1dSRodney W. Grimes 	}
223979e9451fSKonstantin Belousov 	if (start == end)
224079e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2241df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2242df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2243df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2244df8bae1dSRodney W. Grimes 		entry = temp_entry;
2245df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
22460d94caffSDavid Greenman 	} else
2247df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2248df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2249df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2250df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
225144428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2252df8bae1dSRodney W. Grimes 		entry = entry->next;
2253df8bae1dSRodney W. Grimes 	}
2254df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2255df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2256df8bae1dSRodney W. Grimes }
2257df8bae1dSRodney W. Grimes 
2258df8bae1dSRodney W. Grimes /*
2259acd9a301SAlan Cox  *	vm_map_unwire:
2260acd9a301SAlan Cox  *
2261e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2262acd9a301SAlan Cox  */
2263acd9a301SAlan Cox int
2264acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2265abd498aaSBruce M Simpson     int flags)
2266acd9a301SAlan Cox {
2267acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2268acd9a301SAlan Cox 	vm_offset_t saved_start;
2269acd9a301SAlan Cox 	unsigned int last_timestamp;
2270acd9a301SAlan Cox 	int rv;
2271abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2272acd9a301SAlan Cox 
227379e9451fSKonstantin Belousov 	if (start == end)
227479e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2275abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2276acd9a301SAlan Cox 	vm_map_lock(map);
2277acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2278acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2279abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2280cbef13d8SAlan Cox 			first_entry = first_entry->next;
2281abd498aaSBruce M Simpson 		else {
2282acd9a301SAlan Cox 			vm_map_unlock(map);
2283acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2284acd9a301SAlan Cox 		}
2285abd498aaSBruce M Simpson 	}
2286acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2287acd9a301SAlan Cox 	entry = first_entry;
2288acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2289acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2290acd9a301SAlan Cox 			/*
2291acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2292acd9a301SAlan Cox 			 */
2293acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2294acd9a301SAlan Cox 			    entry->start;
2295acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
22968ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2297acd9a301SAlan Cox 				/*
2298acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2299acd9a301SAlan Cox 				 */
2300acd9a301SAlan Cox 			}
2301acd9a301SAlan Cox 			vm_map_lock(map);
2302acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2303acd9a301SAlan Cox 				/*
2304acd9a301SAlan Cox 				 * Look again for the entry because the map was
2305acd9a301SAlan Cox 				 * modified while it was unlocked.
2306acd9a301SAlan Cox 				 * Specifically, the entry may have been
2307acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2308acd9a301SAlan Cox 				 */
2309acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2310acd9a301SAlan Cox 				    &tmp_entry)) {
2311cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2312cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2313cbef13d8SAlan Cox 					else {
2314acd9a301SAlan Cox 						if (saved_start == start) {
2315acd9a301SAlan Cox 							/*
2316acd9a301SAlan Cox 							 * First_entry has been deleted.
2317acd9a301SAlan Cox 							 */
2318acd9a301SAlan Cox 							vm_map_unlock(map);
2319acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2320acd9a301SAlan Cox 						}
2321acd9a301SAlan Cox 						end = saved_start;
2322acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2323acd9a301SAlan Cox 						goto done;
2324acd9a301SAlan Cox 					}
2325cbef13d8SAlan Cox 				}
2326acd9a301SAlan Cox 				if (entry == first_entry)
2327acd9a301SAlan Cox 					first_entry = tmp_entry;
2328acd9a301SAlan Cox 				else
2329acd9a301SAlan Cox 					first_entry = NULL;
2330acd9a301SAlan Cox 				entry = tmp_entry;
2331acd9a301SAlan Cox 			}
2332acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2333acd9a301SAlan Cox 			continue;
2334acd9a301SAlan Cox 		}
2335acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2336acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2337acd9a301SAlan Cox 		/*
2338acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2339acd9a301SAlan Cox 		 * above.)
2340acd9a301SAlan Cox 		 */
2341ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2342ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2343ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
2344acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
23450acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2346acd9a301SAlan Cox 		/*
2347acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2348abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2349acd9a301SAlan Cox 		 */
2350abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2351abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2352abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2353acd9a301SAlan Cox 			end = entry->end;
2354acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2355acd9a301SAlan Cox 			goto done;
2356acd9a301SAlan Cox 		}
2357acd9a301SAlan Cox 		/*
23583ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2359acd9a301SAlan Cox 		 */
23600ada205eSBrian Feldman 		if (!user_unwire &&
23610ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2362acd9a301SAlan Cox 			end = entry->end;
2363acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2364acd9a301SAlan Cox 			goto done;
2365acd9a301SAlan Cox 		}
2366acd9a301SAlan Cox 		entry = entry->next;
2367acd9a301SAlan Cox 	}
2368acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2369acd9a301SAlan Cox done:
2370e27e17b7SAlan Cox 	need_wakeup = FALSE;
2371acd9a301SAlan Cox 	if (first_entry == NULL) {
2372acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2373cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2374cbef13d8SAlan Cox 			first_entry = first_entry->next;
2375cbef13d8SAlan Cox 		else
2376acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2377acd9a301SAlan Cox 	}
23780acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
23790acea7dfSKonstantin Belousov 	    entry = entry->next) {
23800acea7dfSKonstantin Belousov 		/*
23810acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
23820acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
23830acea7dfSKonstantin Belousov 		 * while the map lock was dropped for draining
23840acea7dfSKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
23850acea7dfSKonstantin Belousov 		 * could be simultaneously wiring this new mapping
23860acea7dfSKonstantin Belousov 		 * entry.  Detect these cases and skip any entries
23870acea7dfSKonstantin Belousov 		 * marked as in transition by us.
23880acea7dfSKonstantin Belousov 		 */
23890acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
23900acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
23910acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
23920acea7dfSKonstantin Belousov 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
23930acea7dfSKonstantin Belousov 			continue;
23940acea7dfSKonstantin Belousov 		}
23950acea7dfSKonstantin Belousov 
23963ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
23973ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2398b2f3846aSAlan Cox 			if (user_unwire)
2399b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2400b2f3846aSAlan Cox 			entry->wired_count--;
24010ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2402b2f3846aSAlan Cox 				/*
2403b2f3846aSAlan Cox 				 * Retain the map lock.
2404b2f3846aSAlan Cox 				 */
24054be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
24064be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
240728634820SAlan Cox 				    (entry->object.vm_object->flags &
240828634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
2409b2f3846aSAlan Cox 			}
2410b2f3846aSAlan Cox 		}
24110acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2412ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: in-transition flag missing %p", entry));
2413ff3ae454SKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
2414ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: alien wire %p", entry));
2415acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
24160acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
2417acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2418acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2419acd9a301SAlan Cox 			need_wakeup = TRUE;
2420acd9a301SAlan Cox 		}
2421acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2422acd9a301SAlan Cox 	}
2423acd9a301SAlan Cox 	vm_map_unlock(map);
2424acd9a301SAlan Cox 	if (need_wakeup)
2425acd9a301SAlan Cox 		vm_map_wakeup(map);
2426acd9a301SAlan Cox 	return (rv);
2427acd9a301SAlan Cox }
2428acd9a301SAlan Cox 
2429acd9a301SAlan Cox /*
2430e27e17b7SAlan Cox  *	vm_map_wire:
2431e27e17b7SAlan Cox  *
2432e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2433e27e17b7SAlan Cox  */
2434e27e17b7SAlan Cox int
2435e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2436abd498aaSBruce M Simpson     int flags)
2437e27e17b7SAlan Cox {
243812d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
243912d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
244012d7cc84SAlan Cox 	unsigned int last_timestamp;
244112d7cc84SAlan Cox 	int rv;
24424be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2443e4cd31ddSJeff Roberson 	vm_prot_t prot;
2444e27e17b7SAlan Cox 
244579e9451fSKonstantin Belousov 	if (start == end)
244679e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2447e4cd31ddSJeff Roberson 	prot = 0;
2448e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
2449e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
2450abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
245112d7cc84SAlan Cox 	vm_map_lock(map);
245212d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
245312d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2454abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2455cbef13d8SAlan Cox 			first_entry = first_entry->next;
2456abd498aaSBruce M Simpson 		else {
245712d7cc84SAlan Cox 			vm_map_unlock(map);
245812d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
245912d7cc84SAlan Cox 		}
2460abd498aaSBruce M Simpson 	}
246112d7cc84SAlan Cox 	last_timestamp = map->timestamp;
246212d7cc84SAlan Cox 	entry = first_entry;
246312d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
246412d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
246512d7cc84SAlan Cox 			/*
246612d7cc84SAlan Cox 			 * We have not yet clipped the entry.
246712d7cc84SAlan Cox 			 */
246812d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
246912d7cc84SAlan Cox 			    entry->start;
247012d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
24718ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
247212d7cc84SAlan Cox 				/*
247312d7cc84SAlan Cox 				 * Allow interruption of user wiring?
247412d7cc84SAlan Cox 				 */
247512d7cc84SAlan Cox 			}
247612d7cc84SAlan Cox 			vm_map_lock(map);
247712d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
247812d7cc84SAlan Cox 				/*
247912d7cc84SAlan Cox 				 * Look again for the entry because the map was
248012d7cc84SAlan Cox 				 * modified while it was unlocked.
248112d7cc84SAlan Cox 				 * Specifically, the entry may have been
248212d7cc84SAlan Cox 				 * clipped, merged, or deleted.
248312d7cc84SAlan Cox 				 */
248412d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
248512d7cc84SAlan Cox 				    &tmp_entry)) {
2486cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2487cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2488cbef13d8SAlan Cox 					else {
248912d7cc84SAlan Cox 						if (saved_start == start) {
249012d7cc84SAlan Cox 							/*
249112d7cc84SAlan Cox 							 * first_entry has been deleted.
249212d7cc84SAlan Cox 							 */
249312d7cc84SAlan Cox 							vm_map_unlock(map);
249412d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
249512d7cc84SAlan Cox 						}
249612d7cc84SAlan Cox 						end = saved_start;
249712d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
249812d7cc84SAlan Cox 						goto done;
249912d7cc84SAlan Cox 					}
2500cbef13d8SAlan Cox 				}
250112d7cc84SAlan Cox 				if (entry == first_entry)
250212d7cc84SAlan Cox 					first_entry = tmp_entry;
250312d7cc84SAlan Cox 				else
250412d7cc84SAlan Cox 					first_entry = NULL;
250512d7cc84SAlan Cox 				entry = tmp_entry;
250612d7cc84SAlan Cox 			}
250712d7cc84SAlan Cox 			last_timestamp = map->timestamp;
250812d7cc84SAlan Cox 			continue;
250912d7cc84SAlan Cox 		}
251012d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
251112d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
251212d7cc84SAlan Cox 		/*
251312d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
251412d7cc84SAlan Cox 		 * above.)
251512d7cc84SAlan Cox 		 */
2516ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2517ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2518ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
251912d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
25200acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2521e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2522e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
2523529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
25246d7e8091SKonstantin Belousov 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
25256d7e8091SKonstantin Belousov 				end = entry->end;
25266d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
25276d7e8091SKonstantin Belousov 				goto done;
25286d7e8091SKonstantin Belousov 			}
25296d7e8091SKonstantin Belousov 			goto next_entry;
25306d7e8091SKonstantin Belousov 		}
2531e4cd31ddSJeff Roberson 		if (entry->wired_count == 0) {
25320ada205eSBrian Feldman 			entry->wired_count++;
253312d7cc84SAlan Cox 			saved_start = entry->start;
253412d7cc84SAlan Cox 			saved_end = entry->end;
25354be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
253628634820SAlan Cox 			    (entry->object.vm_object->flags &
253728634820SAlan Cox 			    OBJ_FICTITIOUS) != 0;
253812d7cc84SAlan Cox 			/*
253912d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
2540a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
254112d7cc84SAlan Cox 			 */
2542a5db445dSMax Laier 			vm_map_busy(map);
254312d7cc84SAlan Cox 			vm_map_unlock(map);
2544ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
25452db65ab4SAlan Cox 			    fictitious);
254612d7cc84SAlan Cox 			vm_map_lock(map);
2547a5db445dSMax Laier 			vm_map_unbusy(map);
254812d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
254912d7cc84SAlan Cox 				/*
255012d7cc84SAlan Cox 				 * Look again for the entry because the map was
255112d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
255212d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
255312d7cc84SAlan Cox 				 * deleted.
255412d7cc84SAlan Cox 				 */
255512d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
255612d7cc84SAlan Cox 				    &tmp_entry);
255712d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
255812d7cc84SAlan Cox 				if (entry == first_entry)
255912d7cc84SAlan Cox 					first_entry = tmp_entry;
256012d7cc84SAlan Cox 				else
256112d7cc84SAlan Cox 					first_entry = NULL;
256212d7cc84SAlan Cox 				entry = tmp_entry;
256328c58286SAlan Cox 				while (entry->end < saved_end) {
256428c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
256528c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
256628c58286SAlan Cox 						    ("vm_map_wire: bad count"));
256728c58286SAlan Cox 						entry->wired_count = -1;
256828c58286SAlan Cox 					}
256912d7cc84SAlan Cox 					entry = entry->next;
257012d7cc84SAlan Cox 				}
257128c58286SAlan Cox 			}
257212d7cc84SAlan Cox 			last_timestamp = map->timestamp;
257312d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
257428c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
257528c58286SAlan Cox 				    ("vm_map_wire: bad count"));
257612d7cc84SAlan Cox 				/*
257728c58286SAlan Cox 				 * Assign an out-of-range value to represent
257828c58286SAlan Cox 				 * the failure to wire this entry.
257912d7cc84SAlan Cox 				 */
258028c58286SAlan Cox 				entry->wired_count = -1;
258112d7cc84SAlan Cox 				end = entry->end;
258212d7cc84SAlan Cox 				goto done;
258312d7cc84SAlan Cox 			}
25840ada205eSBrian Feldman 		} else if (!user_wire ||
25850ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
25860ada205eSBrian Feldman 			entry->wired_count++;
258712d7cc84SAlan Cox 		}
258812d7cc84SAlan Cox 		/*
258912d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2590abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
259112d7cc84SAlan Cox 		 */
25926d7e8091SKonstantin Belousov 	next_entry:
2593abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2594abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2595abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
259612d7cc84SAlan Cox 			end = entry->end;
259712d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
259812d7cc84SAlan Cox 			goto done;
259912d7cc84SAlan Cox 		}
260012d7cc84SAlan Cox 		entry = entry->next;
260112d7cc84SAlan Cox 	}
260212d7cc84SAlan Cox 	rv = KERN_SUCCESS;
260312d7cc84SAlan Cox done:
260412d7cc84SAlan Cox 	need_wakeup = FALSE;
260512d7cc84SAlan Cox 	if (first_entry == NULL) {
260612d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2607cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2608cbef13d8SAlan Cox 			first_entry = first_entry->next;
2609cbef13d8SAlan Cox 		else
261012d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
261112d7cc84SAlan Cox 	}
26120acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
26130acea7dfSKonstantin Belousov 	    entry = entry->next) {
26146d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
26156d7e8091SKonstantin Belousov 			goto next_entry_done;
26160acea7dfSKonstantin Belousov 
26170acea7dfSKonstantin Belousov 		/*
26180acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
26190acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
26200acea7dfSKonstantin Belousov 		 * while the map lock was dropped for faulting in the
26210acea7dfSKonstantin Belousov 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
26220acea7dfSKonstantin Belousov 		 * Moreover, another thread could be simultaneously
26230acea7dfSKonstantin Belousov 		 * wiring this new mapping entry.  Detect these cases
26240acea7dfSKonstantin Belousov 		 * and skip any entries marked as in transition by us.
26250acea7dfSKonstantin Belousov 		 */
26260acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
26270acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
26280acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
26290acea7dfSKonstantin Belousov 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
26300acea7dfSKonstantin Belousov 			continue;
26310acea7dfSKonstantin Belousov 		}
26320acea7dfSKonstantin Belousov 
263312d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
263412d7cc84SAlan Cox 			if (user_wire)
263512d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
263628c58286SAlan Cox 		} else if (entry->wired_count == -1) {
263728c58286SAlan Cox 			/*
263828c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
263928c58286SAlan Cox 			 * unnecessary.
264028c58286SAlan Cox 			 */
264128c58286SAlan Cox 			entry->wired_count = 0;
264212d7cc84SAlan Cox 		} else {
26430ada205eSBrian Feldman 			if (!user_wire ||
26440ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
264512d7cc84SAlan Cox 				entry->wired_count--;
26460ada205eSBrian Feldman 			if (entry->wired_count == 0) {
264712d7cc84SAlan Cox 				/*
264812d7cc84SAlan Cox 				 * Retain the map lock.
264912d7cc84SAlan Cox 				 */
26504be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
26514be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
265228634820SAlan Cox 				    (entry->object.vm_object->flags &
265328634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
265412d7cc84SAlan Cox 			}
265512d7cc84SAlan Cox 		}
26566d7e8091SKonstantin Belousov 	next_entry_done:
26570acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
26580acea7dfSKonstantin Belousov 		    ("vm_map_wire: in-transition flag missing %p", entry));
26590acea7dfSKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
26600acea7dfSKonstantin Belousov 		    ("vm_map_wire: alien wire %p", entry));
26610acea7dfSKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
26620acea7dfSKonstantin Belousov 		    MAP_ENTRY_WIRE_SKIPPED);
26630acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
266412d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
266512d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
266612d7cc84SAlan Cox 			need_wakeup = TRUE;
266712d7cc84SAlan Cox 		}
266812d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
266912d7cc84SAlan Cox 	}
267012d7cc84SAlan Cox 	vm_map_unlock(map);
267112d7cc84SAlan Cox 	if (need_wakeup)
267212d7cc84SAlan Cox 		vm_map_wakeup(map);
267312d7cc84SAlan Cox 	return (rv);
2674e27e17b7SAlan Cox }
2675e27e17b7SAlan Cox 
2676e27e17b7SAlan Cox /*
2677950f8459SAlan Cox  * vm_map_sync
2678df8bae1dSRodney W. Grimes  *
2679df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2680df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2681df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2682df8bae1dSRodney W. Grimes  *
2683637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2684637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2685637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2686637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2687637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2688637315edSAlan Cox  * flushing the current region containing start.
2689637315edSAlan Cox  *
2690df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2691df8bae1dSRodney W. Grimes  */
2692df8bae1dSRodney W. Grimes int
2693950f8459SAlan Cox vm_map_sync(
26941b40f8c0SMatthew Dillon 	vm_map_t map,
26951b40f8c0SMatthew Dillon 	vm_offset_t start,
26961b40f8c0SMatthew Dillon 	vm_offset_t end,
26971b40f8c0SMatthew Dillon 	boolean_t syncio,
26981b40f8c0SMatthew Dillon 	boolean_t invalidate)
2699df8bae1dSRodney W. Grimes {
2700c0877f10SJohn Dyson 	vm_map_entry_t current;
2701df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2702df8bae1dSRodney W. Grimes 	vm_size_t size;
2703df8bae1dSRodney W. Grimes 	vm_object_t object;
2704a316d390SJohn Dyson 	vm_ooffset_t offset;
2705e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2706126d6082SKonstantin Belousov 	boolean_t failed;
2707df8bae1dSRodney W. Grimes 
2708df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2709df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2710df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2711df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2712df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2713637315edSAlan Cox 	} else if (start == end) {
2714637315edSAlan Cox 		start = entry->start;
2715637315edSAlan Cox 		end = entry->end;
2716df8bae1dSRodney W. Grimes 	}
2717df8bae1dSRodney W. Grimes 	/*
2718b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2719df8bae1dSRodney W. Grimes 	 */
27207b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
27217b0e72d1SAlan Cox 	    current = current->next) {
2722b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2723df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2724df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2725df8bae1dSRodney W. Grimes 		}
2726df8bae1dSRodney W. Grimes 		if (end > current->end &&
2727df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2728df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2729df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2730df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2731df8bae1dSRodney W. Grimes 		}
2732df8bae1dSRodney W. Grimes 	}
2733df8bae1dSRodney W. Grimes 
27342cf13952SAlan Cox 	if (invalidate)
2735bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2736126d6082SKonstantin Belousov 	failed = FALSE;
27372cf13952SAlan Cox 
2738df8bae1dSRodney W. Grimes 	/*
2739df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2740df8bae1dSRodney W. Grimes 	 * objects as we go.
2741df8bae1dSRodney W. Grimes 	 */
2742e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2743df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2744df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
27459fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2746c0877f10SJohn Dyson 			vm_map_t smap;
2747df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2748df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2749df8bae1dSRodney W. Grimes 
27509fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2751df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2752df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2753df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2754df8bae1dSRodney W. Grimes 			if (tsize < size)
2755df8bae1dSRodney W. Grimes 				size = tsize;
2756df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2757df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2758df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2759df8bae1dSRodney W. Grimes 		} else {
2760df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2761df8bae1dSRodney W. Grimes 		}
2762e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2763e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2764e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2765126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
2766126d6082SKonstantin Belousov 			failed = TRUE;
2767df8bae1dSRodney W. Grimes 		start += size;
2768e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2769e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2770e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2771e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2772e53fa61bSKonstantin Belousov 			current = current->next;
2773df8bae1dSRodney W. Grimes 	}
2774df8bae1dSRodney W. Grimes 
2775df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2776126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
2777df8bae1dSRodney W. Grimes }
2778df8bae1dSRodney W. Grimes 
2779df8bae1dSRodney W. Grimes /*
2780df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2781df8bae1dSRodney W. Grimes  *
2782df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2783df8bae1dSRodney W. Grimes  *
2784df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2785df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2786df8bae1dSRodney W. Grimes  */
27870362d7d7SJohn Dyson static void
27881b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2789df8bae1dSRodney W. Grimes {
27904be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
27914be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
279228634820SAlan Cox 	    (entry->object.vm_object->flags & OBJ_FICTITIOUS) != 0);
2793df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2794df8bae1dSRodney W. Grimes }
2795df8bae1dSRodney W. Grimes 
27960b367bd8SKonstantin Belousov static void
27970b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
27980b367bd8SKonstantin Belousov {
27990b367bd8SKonstantin Belousov 
28000b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
28010b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
28020b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
28030b367bd8SKonstantin Belousov }
28040b367bd8SKonstantin Belousov 
2805df8bae1dSRodney W. Grimes /*
2806df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2807df8bae1dSRodney W. Grimes  *
2808df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2809df8bae1dSRodney W. Grimes  */
28100362d7d7SJohn Dyson static void
28111b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2812df8bae1dSRodney W. Grimes {
281332a89c32SAlan Cox 	vm_object_t object;
28143364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
28153364c323SKonstantin Belousov 	vm_ooffset_t size;
281632a89c32SAlan Cox 
2817df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
28183364c323SKonstantin Belousov 	object = entry->object.vm_object;
28193364c323SKonstantin Belousov 	size = entry->end - entry->start;
28203364c323SKonstantin Belousov 	map->size -= size;
28213364c323SKonstantin Belousov 
2822ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
2823ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
2824ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
28253364c323SKonstantin Belousov 	}
2826df8bae1dSRodney W. Grimes 
282732a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
28283364c323SKonstantin Belousov 	    (object != NULL)) {
2829ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
28303364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2831ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
28323364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
283332a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
283432a89c32SAlan Cox 		offidxend = offidxstart + count;
283589f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
283632a89c32SAlan Cox 		if (object->ref_count != 1 &&
283732a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
28389f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
283932a89c32SAlan Cox 			vm_object_collapse(object);
28406bbee8e2SAlan Cox 
28416bbee8e2SAlan Cox 			/*
28426bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
28436bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
28446bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
28456bbee8e2SAlan Cox 			 * of pages.
28466bbee8e2SAlan Cox 			 */
28476bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
28486bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
284932a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
285032a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
285132a89c32SAlan Cox 			if (offidxend >= object->size &&
28523364c323SKonstantin Belousov 			    offidxstart < object->size) {
28533364c323SKonstantin Belousov 				size1 = object->size;
285432a89c32SAlan Cox 				object->size = offidxstart;
2855ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
28563364c323SKonstantin Belousov 					size1 -= object->size;
28573364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
28583364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
2859ef694c1aSEdward Tomasz Napierala 					swap_release_by_cred(ptoa(size1), object->cred);
28603364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
28613364c323SKonstantin Belousov 				}
28623364c323SKonstantin Belousov 			}
286332a89c32SAlan Cox 		}
286489f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2865897d81a0SKonstantin Belousov 	} else
2866897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
28670b367bd8SKonstantin Belousov 	if (map->system_map)
28680b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
28690b367bd8SKonstantin Belousov 	else {
28700b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
28710b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
28720b367bd8SKonstantin Belousov 	}
2873df8bae1dSRodney W. Grimes }
2874df8bae1dSRodney W. Grimes 
2875df8bae1dSRodney W. Grimes /*
2876df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2877df8bae1dSRodney W. Grimes  *
2878df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2879df8bae1dSRodney W. Grimes  *	map.
2880df8bae1dSRodney W. Grimes  */
2881df8bae1dSRodney W. Grimes int
2882655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2883df8bae1dSRodney W. Grimes {
2884c0877f10SJohn Dyson 	vm_map_entry_t entry;
2885df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2886df8bae1dSRodney W. Grimes 
28873a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
288879e9451fSKonstantin Belousov 	if (start == end)
288979e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
28903a0916b8SKonstantin Belousov 
2891df8bae1dSRodney W. Grimes 	/*
2892df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2893df8bae1dSRodney W. Grimes 	 */
2894876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2895df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2896876318ecSAlan Cox 	else {
2897df8bae1dSRodney W. Grimes 		entry = first_entry;
2898df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2899df8bae1dSRodney W. Grimes 	}
2900df8bae1dSRodney W. Grimes 
2901df8bae1dSRodney W. Grimes 	/*
2902df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2903df8bae1dSRodney W. Grimes 	 */
2904df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2905df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2906df8bae1dSRodney W. Grimes 
290773b2baceSAlan Cox 		/*
290873b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
29097c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
29107c938963SBrian Feldman 		 * user maps.
291173b2baceSAlan Cox 		 */
29127c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
29137c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
29147c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
291573b2baceSAlan Cox 			unsigned int last_timestamp;
291673b2baceSAlan Cox 			vm_offset_t saved_start;
291773b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
291873b2baceSAlan Cox 
291973b2baceSAlan Cox 			saved_start = entry->start;
292073b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
292173b2baceSAlan Cox 			last_timestamp = map->timestamp;
29228ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
292373b2baceSAlan Cox 			vm_map_lock(map);
292473b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
292573b2baceSAlan Cox 				/*
292673b2baceSAlan Cox 				 * Look again for the entry because the map was
292773b2baceSAlan Cox 				 * modified while it was unlocked.
292873b2baceSAlan Cox 				 * Specifically, the entry may have been
292973b2baceSAlan Cox 				 * clipped, merged, or deleted.
293073b2baceSAlan Cox 				 */
293173b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
293273b2baceSAlan Cox 							 &tmp_entry))
293373b2baceSAlan Cox 					entry = tmp_entry->next;
293473b2baceSAlan Cox 				else {
293573b2baceSAlan Cox 					entry = tmp_entry;
293673b2baceSAlan Cox 					vm_map_clip_start(map, entry,
293773b2baceSAlan Cox 							  saved_start);
293873b2baceSAlan Cox 				}
293973b2baceSAlan Cox 			}
294073b2baceSAlan Cox 			continue;
294173b2baceSAlan Cox 		}
2942df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2943df8bae1dSRodney W. Grimes 
2944c0877f10SJohn Dyson 		next = entry->next;
2945df8bae1dSRodney W. Grimes 
2946df8bae1dSRodney W. Grimes 		/*
29470d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
29480d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2949df8bae1dSRodney W. Grimes 		 */
2950c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2951df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2952c0877f10SJohn Dyson 		}
2953df8bae1dSRodney W. Grimes 
295432a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2955df8bae1dSRodney W. Grimes 
2956df8bae1dSRodney W. Grimes 		/*
2957e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2958e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2959e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2960e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2961df8bae1dSRodney W. Grimes 		 */
2962df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2963df8bae1dSRodney W. Grimes 		entry = next;
2964df8bae1dSRodney W. Grimes 	}
2965df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2966df8bae1dSRodney W. Grimes }
2967df8bae1dSRodney W. Grimes 
2968df8bae1dSRodney W. Grimes /*
2969df8bae1dSRodney W. Grimes  *	vm_map_remove:
2970df8bae1dSRodney W. Grimes  *
2971df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2972df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2973df8bae1dSRodney W. Grimes  */
2974df8bae1dSRodney W. Grimes int
29751b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2976df8bae1dSRodney W. Grimes {
29776eaee3feSAlan Cox 	int result;
2978df8bae1dSRodney W. Grimes 
2979df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2980df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2981655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
2982df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2983df8bae1dSRodney W. Grimes 	return (result);
2984df8bae1dSRodney W. Grimes }
2985df8bae1dSRodney W. Grimes 
2986df8bae1dSRodney W. Grimes /*
2987df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2988df8bae1dSRodney W. Grimes  *
29892d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
29902d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
29912d5c7e45SMatthew Dillon  *
29922d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
29932d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
29942d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
29952d5c7e45SMatthew Dillon  *
29962d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2997d8834602SAlan Cox  *
2998d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2999df8bae1dSRodney W. Grimes  */
30000d94caffSDavid Greenman boolean_t
3001b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3002b9dcd593SBruce Evans 			vm_prot_t protection)
3003df8bae1dSRodney W. Grimes {
3004c0877f10SJohn Dyson 	vm_map_entry_t entry;
3005df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
3006df8bae1dSRodney W. Grimes 
3007d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
3008df8bae1dSRodney W. Grimes 		return (FALSE);
3009df8bae1dSRodney W. Grimes 	entry = tmp_entry;
3010df8bae1dSRodney W. Grimes 
3011df8bae1dSRodney W. Grimes 	while (start < end) {
3012d8834602SAlan Cox 		if (entry == &map->header)
3013df8bae1dSRodney W. Grimes 			return (FALSE);
3014df8bae1dSRodney W. Grimes 		/*
3015df8bae1dSRodney W. Grimes 		 * No holes allowed!
3016df8bae1dSRodney W. Grimes 		 */
3017d8834602SAlan Cox 		if (start < entry->start)
3018df8bae1dSRodney W. Grimes 			return (FALSE);
3019df8bae1dSRodney W. Grimes 		/*
3020df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
3021df8bae1dSRodney W. Grimes 		 */
3022d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
3023df8bae1dSRodney W. Grimes 			return (FALSE);
3024df8bae1dSRodney W. Grimes 		/* go to next entry */
3025df8bae1dSRodney W. Grimes 		start = entry->end;
3026df8bae1dSRodney W. Grimes 		entry = entry->next;
3027df8bae1dSRodney W. Grimes 	}
3028df8bae1dSRodney W. Grimes 	return (TRUE);
3029df8bae1dSRodney W. Grimes }
3030df8bae1dSRodney W. Grimes 
303186524867SJohn Dyson /*
3032df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
3033df8bae1dSRodney W. Grimes  *
3034df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
3035df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
3036df8bae1dSRodney W. Grimes  */
3037f708ef1bSPoul-Henning Kamp static void
30381b40f8c0SMatthew Dillon vm_map_copy_entry(
30391b40f8c0SMatthew Dillon 	vm_map_t src_map,
30401b40f8c0SMatthew Dillon 	vm_map_t dst_map,
30411b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
30423364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
30433364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
3044df8bae1dSRodney W. Grimes {
3045c0877f10SJohn Dyson 	vm_object_t src_object;
304684110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
30473364c323SKonstantin Belousov 	vm_offset_t size;
3048ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
30493364c323SKonstantin Belousov 	int charged;
3050c0877f10SJohn Dyson 
30513a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
30523a0916b8SKonstantin Belousov 
30539fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3054df8bae1dSRodney W. Grimes 		return;
3055df8bae1dSRodney W. Grimes 
3056afaa41f6SAlan Cox 	if (src_entry->wired_count == 0 ||
3057afaa41f6SAlan Cox 	    (src_entry->protection & VM_PROT_WRITE) == 0) {
3058df8bae1dSRodney W. Grimes 		/*
30590d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
30600d94caffSDavid Greenman 		 * write-protected.
3061df8bae1dSRodney W. Grimes 		 */
3062d9a9209aSAlan Cox 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3063d9a9209aSAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) != 0) {
3064df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
3065df8bae1dSRodney W. Grimes 			    src_entry->start,
3066df8bae1dSRodney W. Grimes 			    src_entry->end,
3067df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
3068df8bae1dSRodney W. Grimes 		}
3069b18bfc3dSJohn Dyson 
3070df8bae1dSRodney W. Grimes 		/*
3071df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
3072df8bae1dSRodney W. Grimes 		 */
30733364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
30748aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
307589f6b863SAttilio Rao 			VM_OBJECT_WLOCK(src_object);
30763364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
3077c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
3078c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
3079c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
3080c0877f10SJohn Dyson 				vm_object_collapse(src_object);
308196fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3082c5aaa06dSAlan Cox 					vm_object_split(src_entry);
3083c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
3084a89c6258SAlan Cox 				}
3085a89c6258SAlan Cox 			}
3086b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
3087069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3088ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL &&
30893364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3090ef694c1aSEdward Tomasz Napierala 				KASSERT(src_object->cred == NULL,
3091ef694c1aSEdward Tomasz Napierala 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
30923364c323SKonstantin Belousov 				     src_object));
3093ef694c1aSEdward Tomasz Napierala 				src_object->cred = src_entry->cred;
30943364c323SKonstantin Belousov 				src_object->charge = size;
30953364c323SKonstantin Belousov 			}
309689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(src_object);
3097c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
30983364c323SKonstantin Belousov 			if (charged) {
3099ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3100ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3101ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = cred;
31023364c323SKonstantin Belousov 				*fork_charge += size;
31033364c323SKonstantin Belousov 				if (!(src_entry->eflags &
31043364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
3105ef694c1aSEdward Tomasz Napierala 					crhold(cred);
3106ef694c1aSEdward Tomasz Napierala 					src_entry->cred = cred;
31073364c323SKonstantin Belousov 					*fork_charge += size;
31083364c323SKonstantin Belousov 				}
31093364c323SKonstantin Belousov 			}
3110afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3111afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3112b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
311384110e7eSKonstantin Belousov 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
311484110e7eSKonstantin Belousov 				/*
311584110e7eSKonstantin Belousov 				 * MAP_ENTRY_VN_WRITECNT cannot
311684110e7eSKonstantin Belousov 				 * indicate write reference from
311784110e7eSKonstantin Belousov 				 * src_entry, since the entry is
311884110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
311984110e7eSKonstantin Belousov 				 * fake entry that is used to
312084110e7eSKonstantin Belousov 				 * decrement object->un_pager.vnp.writecount
312184110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
312284110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
312384110e7eSKonstantin Belousov 				 */
312484110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
312584110e7eSKonstantin Belousov 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
312684110e7eSKonstantin Belousov 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
312784110e7eSKonstantin Belousov 				vm_object_reference(src_object);
312884110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
312984110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
313084110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
313184110e7eSKonstantin Belousov 				fake_entry->next = curthread->td_map_def_user;
313284110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
313384110e7eSKonstantin Belousov 			}
3134b18bfc3dSJohn Dyson 		} else {
3135b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
3136b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
3137ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
3138ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
3139ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
31403364c323SKonstantin Belousov 				*fork_charge += size;
31413364c323SKonstantin Belousov 			}
3142b18bfc3dSJohn Dyson 		}
3143df8bae1dSRodney W. Grimes 
3144df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3145df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
31460d94caffSDavid Greenman 	} else {
3147df8bae1dSRodney W. Grimes 		/*
3148afaa41f6SAlan Cox 		 * We don't want to make writeable wired pages copy-on-write.
3149afaa41f6SAlan Cox 		 * Immediately copy these pages into the new map by simulating
3150afaa41f6SAlan Cox 		 * page faults.  The new pages are pageable.
3151df8bae1dSRodney W. Grimes 		 */
3152121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3153121fd461SKonstantin Belousov 		    fork_charge);
3154df8bae1dSRodney W. Grimes 	}
3155df8bae1dSRodney W. Grimes }
3156df8bae1dSRodney W. Grimes 
3157df8bae1dSRodney W. Grimes /*
31582a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
31592a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
31602a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
31612a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
31622a7be1b6SBrian Feldman  */
31632a7be1b6SBrian Feldman static void
31642a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
31652a7be1b6SBrian Feldman     vm_map_entry_t entry)
31662a7be1b6SBrian Feldman {
31672a7be1b6SBrian Feldman 	vm_size_t entrysize;
31682a7be1b6SBrian Feldman 	vm_offset_t newend;
31692a7be1b6SBrian Feldman 
31702a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
31712a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
31722a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
31732a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
31742a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
31752a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3176b351299cSAndrew Gallatin 		newend = MIN(entry->end,
31772a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
31782a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
31792a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
31802a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3181b351299cSAndrew Gallatin 		newend = MIN(entry->end,
31822a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
31832a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
31842a7be1b6SBrian Feldman 	}
31852a7be1b6SBrian Feldman }
31862a7be1b6SBrian Feldman 
31872a7be1b6SBrian Feldman /*
3188df8bae1dSRodney W. Grimes  * vmspace_fork:
3189df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
3190df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
3191df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
3192df8bae1dSRodney W. Grimes  * values on the regions in that map.
3193df8bae1dSRodney W. Grimes  *
31942a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
31952a7be1b6SBrian Feldman  *
3196df8bae1dSRodney W. Grimes  * The source map must not be locked.
3197df8bae1dSRodney W. Grimes  */
3198df8bae1dSRodney W. Grimes struct vmspace *
31993364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3200df8bae1dSRodney W. Grimes {
3201c0877f10SJohn Dyson 	struct vmspace *vm2;
320279e53838SAlan Cox 	vm_map_t new_map, old_map;
320379e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
3204de5f6a77SJohn Dyson 	vm_object_t object;
32051fac7d7fSKonstantin Belousov 	int locked;
3206df8bae1dSRodney W. Grimes 
320779e53838SAlan Cox 	old_map = &vm1->vm_map;
320879e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
320974d1d2b7SNeel Natu 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, NULL);
321089b57fcfSKonstantin Belousov 	if (vm2 == NULL)
321179e53838SAlan Cox 		return (NULL);
32122a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
32132a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
32142a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
321579e53838SAlan Cox 	vm_map_lock(old_map);
321679e53838SAlan Cox 	if (old_map->busy)
321779e53838SAlan Cox 		vm_map_wait_busy(old_map);
321879e53838SAlan Cox 	new_map = &vm2->vm_map;
32191fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
32201fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
3221df8bae1dSRodney W. Grimes 
3222df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3223df8bae1dSRodney W. Grimes 
3224df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3225afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3226df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3227df8bae1dSRodney W. Grimes 
3228df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3229df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3230df8bae1dSRodney W. Grimes 			break;
3231df8bae1dSRodney W. Grimes 
3232df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3233df8bae1dSRodney W. Grimes 			/*
3234fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3235fed9a903SJohn Dyson 			 */
3236fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3237fed9a903SJohn Dyson 			if (object == NULL) {
3238fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3239c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3240fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
324115d2d313SAlan Cox 				old_entry->offset = 0;
3242ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
3243ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
32443364c323SKonstantin Belousov 					object->charge = old_entry->end -
32453364c323SKonstantin Belousov 					    old_entry->start;
3246ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
32473364c323SKonstantin Belousov 				}
32489a2f6362SAlan Cox 			}
32499a2f6362SAlan Cox 
32509a2f6362SAlan Cox 			/*
32519a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
32529a2f6362SAlan Cox 			 * to insure that a shadow object is created.
32539a2f6362SAlan Cox 			 */
32549a2f6362SAlan Cox 			vm_object_reference(object);
32559a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
32565069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
32575069bf57SJohn Dyson 				    &old_entry->offset,
32580cc74f14SAlan Cox 				    old_entry->end - old_entry->start);
32595069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3260d30344bdSIan Dowse 				/* Transfer the second reference too. */
3261d30344bdSIan Dowse 				vm_object_reference(
3262d30344bdSIan Dowse 				    old_entry->object.vm_object);
32637fd10fb3SKonstantin Belousov 
32647fd10fb3SKonstantin Belousov 				/*
32657fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3266b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
32677fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
32687fd10fb3SKonstantin Belousov 				 */
3269d30344bdSIan Dowse 				vm_object_deallocate(object);
32705069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3271fed9a903SJohn Dyson 			}
327289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3273069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3274ef694c1aSEdward Tomasz Napierala 			if (old_entry->cred != NULL) {
3275ef694c1aSEdward Tomasz Napierala 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3276ef694c1aSEdward Tomasz Napierala 				object->cred = old_entry->cred;
32773364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
3278ef694c1aSEdward Tomasz Napierala 				old_entry->cred = NULL;
32793364c323SKonstantin Belousov 			}
3280b9781cf6SKonstantin Belousov 
3281b9781cf6SKonstantin Belousov 			/*
3282b9781cf6SKonstantin Belousov 			 * Assert the correct state of the vnode
3283b9781cf6SKonstantin Belousov 			 * v_writecount while the object is locked, to
3284b9781cf6SKonstantin Belousov 			 * not relock it later for the assertion
3285b9781cf6SKonstantin Belousov 			 * correctness.
3286b9781cf6SKonstantin Belousov 			 */
3287b9781cf6SKonstantin Belousov 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3288b9781cf6SKonstantin Belousov 			    object->type == OBJT_VNODE) {
3289b9781cf6SKonstantin Belousov 				KASSERT(((struct vnode *)object->handle)->
3290b9781cf6SKonstantin Belousov 				    v_writecount > 0,
3291b9781cf6SKonstantin Belousov 				    ("vmspace_fork: v_writecount %p", object));
3292b9781cf6SKonstantin Belousov 				KASSERT(object->un_pager.vnp.writemappings > 0,
3293b9781cf6SKonstantin Belousov 				    ("vmspace_fork: vnp.writecount %p",
3294b9781cf6SKonstantin Belousov 				    object));
3295b9781cf6SKonstantin Belousov 			}
329689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
3297fed9a903SJohn Dyson 
3298fed9a903SJohn Dyson 			/*
3299ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3300df8bae1dSRodney W. Grimes 			 */
3301df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3302df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
33039f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
33049f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
33050acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3306df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
330784110e7eSKonstantin Belousov 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
330884110e7eSKonstantin Belousov 				vnode_pager_update_writecount(object,
330984110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
331084110e7eSKonstantin Belousov 			}
3311df8bae1dSRodney W. Grimes 
3312df8bae1dSRodney W. Grimes 			/*
33130d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
33140d94caffSDavid Greenman 			 * inserting at the end of the new map.
3315df8bae1dSRodney W. Grimes 			 */
3316df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3317df8bae1dSRodney W. Grimes 			    new_entry);
33182a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3319df8bae1dSRodney W. Grimes 
3320df8bae1dSRodney W. Grimes 			/*
3321df8bae1dSRodney W. Grimes 			 * Update the physical map
3322df8bae1dSRodney W. Grimes 			 */
3323df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3324df8bae1dSRodney W. Grimes 			    new_entry->start,
3325df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3326df8bae1dSRodney W. Grimes 			    old_entry->start);
3327df8bae1dSRodney W. Grimes 			break;
3328df8bae1dSRodney W. Grimes 
3329df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3330df8bae1dSRodney W. Grimes 			/*
3331df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3332df8bae1dSRodney W. Grimes 			 */
3333df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3334df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
333584110e7eSKonstantin Belousov 			/*
333684110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
333784110e7eSKonstantin Belousov 			 */
33389f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
333984110e7eSKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
33400acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3341df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3342df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3343ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
3344df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3345df8bae1dSRodney W. Grimes 			    new_entry);
33462a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3347bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
33483364c323SKonstantin Belousov 			    new_entry, fork_charge);
3349df8bae1dSRodney W. Grimes 			break;
3350df8bae1dSRodney W. Grimes 		}
3351df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3352df8bae1dSRodney W. Grimes 	}
335384110e7eSKonstantin Belousov 	/*
335484110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
335584110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
335684110e7eSKonstantin Belousov 	 * new_map locks are released.
335784110e7eSKonstantin Belousov 	 */
335884110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
335984110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
336084110e7eSKonstantin Belousov 	vm_map_process_deferred();
3361df8bae1dSRodney W. Grimes 
3362df8bae1dSRodney W. Grimes 	return (vm2);
3363df8bae1dSRodney W. Grimes }
3364df8bae1dSRodney W. Grimes 
336594f7e29aSAlan Cox int
336694f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
336794f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
336894f7e29aSAlan Cox {
3369*4648ba0aSKonstantin Belousov 	vm_size_t growsize, init_ssize;
3370*4648ba0aSKonstantin Belousov 	rlim_t lmemlim, vmemlim;
3371*4648ba0aSKonstantin Belousov 	int rv;
3372*4648ba0aSKonstantin Belousov 
3373*4648ba0aSKonstantin Belousov 	growsize = sgrowsiz;
3374*4648ba0aSKonstantin Belousov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
3375*4648ba0aSKonstantin Belousov 	vm_map_lock(map);
3376*4648ba0aSKonstantin Belousov 	PROC_LOCK(curproc);
3377*4648ba0aSKonstantin Belousov 	lmemlim = lim_cur(curproc, RLIMIT_MEMLOCK);
3378*4648ba0aSKonstantin Belousov 	vmemlim = lim_cur(curproc, RLIMIT_VMEM);
3379*4648ba0aSKonstantin Belousov 	PROC_UNLOCK(curproc);
3380*4648ba0aSKonstantin Belousov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
3381*4648ba0aSKonstantin Belousov 		if (ptoa(pmap_wired_count(map->pmap)) + init_ssize > lmemlim) {
3382*4648ba0aSKonstantin Belousov 			rv = KERN_NO_SPACE;
3383*4648ba0aSKonstantin Belousov 			goto out;
3384*4648ba0aSKonstantin Belousov 		}
3385*4648ba0aSKonstantin Belousov 	}
3386*4648ba0aSKonstantin Belousov 	/* If we would blow our VMEM resource limit, no go */
3387*4648ba0aSKonstantin Belousov 	if (map->size + init_ssize > vmemlim) {
3388*4648ba0aSKonstantin Belousov 		rv = KERN_NO_SPACE;
3389*4648ba0aSKonstantin Belousov 		goto out;
3390*4648ba0aSKonstantin Belousov 	}
3391*4648ba0aSKonstantin Belousov 	rv = vm_map_stack_locked(map, addrbos, max_ssize, sgrowsiz, prot,
3392*4648ba0aSKonstantin Belousov 	    max, cow);
3393*4648ba0aSKonstantin Belousov out:
3394*4648ba0aSKonstantin Belousov 	vm_map_unlock(map);
3395*4648ba0aSKonstantin Belousov 	return (rv);
3396*4648ba0aSKonstantin Belousov }
3397*4648ba0aSKonstantin Belousov 
3398*4648ba0aSKonstantin Belousov static int
3399*4648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
3400*4648ba0aSKonstantin Belousov     vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
3401*4648ba0aSKonstantin Belousov {
3402fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3403fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
3404*4648ba0aSKonstantin Belousov 	vm_size_t init_ssize;
3405fd75d710SMarcel Moolenaar 	int orient, rv;
340694f7e29aSAlan Cox 
3407fd75d710SMarcel Moolenaar 	/*
3408fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3409fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3410fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3411fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3412fd75d710SMarcel Moolenaar 	 */
3413fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3414fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3415fd75d710SMarcel Moolenaar 
341677bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
341777bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
341877bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
341994f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3420fd75d710SMarcel Moolenaar 
3421cfe52ecfSAndrey Zonov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
342294f7e29aSAlan Cox 
342394f7e29aSAlan Cox 	/* If addr is already mapped, no go */
3424*4648ba0aSKonstantin Belousov 	if (vm_map_lookup_entry(map, addrbos, &prev_entry))
342594f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3426a69ac174SMatthew Dillon 
3427fd75d710SMarcel Moolenaar 	/*
3428fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3429fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3430fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3431fd75d710SMarcel Moolenaar 	 * space is not protected.
343294f7e29aSAlan Cox 	 *
3433fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3434fd75d710SMarcel Moolenaar 	 * grow the stack.
343594f7e29aSAlan Cox 	 */
343694f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
3437*4648ba0aSKonstantin Belousov 	    (prev_entry->next->start < addrbos + max_ssize))
343894f7e29aSAlan Cox 		return (KERN_NO_SPACE);
343994f7e29aSAlan Cox 
3440fd75d710SMarcel Moolenaar 	/*
3441fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3442fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3443fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3444fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
344594f7e29aSAlan Cox 	 *
3446fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3447fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3448fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
344994f7e29aSAlan Cox 	 */
3450fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3451fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3452fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3453fd75d710SMarcel Moolenaar 		bot = addrbos;
3454fd75d710SMarcel Moolenaar 	else
3455fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3456fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3457fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
345894f7e29aSAlan Cox 
3459fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
346094f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
346129b45e9eSAlan Cox 		if (prev_entry != &map->header)
3462fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
3463fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3464fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
346594f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3466b21a0008SMarcel Moolenaar 
3467fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3468fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
3469fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3470fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
3471fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
347294f7e29aSAlan Cox 	}
347394f7e29aSAlan Cox 
347494f7e29aSAlan Cox 	return (rv);
347594f7e29aSAlan Cox }
347694f7e29aSAlan Cox 
34779a6d144fSKonstantin Belousov static int stack_guard_page = 0;
34789a6d144fSKonstantin Belousov TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page);
34799a6d144fSKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW,
34809a6d144fSKonstantin Belousov     &stack_guard_page, 0,
34819a6d144fSKonstantin Belousov     "Insert stack guard page ahead of the growable segments.");
34829a6d144fSKonstantin Belousov 
348394f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
348494f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
348594f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
348694f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
348794f7e29aSAlan Cox  * the grow function in vm_machdep.c).
348894f7e29aSAlan Cox  */
348994f7e29aSAlan Cox int
349094f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
349194f7e29aSAlan Cox {
3492b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3493b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
349494f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
349594f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
349694f7e29aSAlan Cox 	vm_offset_t end;
3497cfe52ecfSAndrey Zonov 	vm_size_t growsize;
3498b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
34997e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
3500b21a0008SMarcel Moolenaar 	int is_procstack, rv;
3501ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
35021ba5ad42SEdward Tomasz Napierala #ifdef notyet
35031ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
35041ba5ad42SEdward Tomasz Napierala #endif
3505afcc55f3SEdward Tomasz Napierala #ifdef RACCT
35061ba5ad42SEdward Tomasz Napierala 	int error;
3507afcc55f3SEdward Tomasz Napierala #endif
350823955314SAlfred Perlstein 
350994f7e29aSAlan Cox Retry:
351091d5354aSJohn Baldwin 	PROC_LOCK(p);
35117e19eda4SAndrey Zonov 	lmemlim = lim_cur(p, RLIMIT_MEMLOCK);
351291d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3513bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
351491d5354aSJohn Baldwin 	PROC_UNLOCK(p);
351591d5354aSJohn Baldwin 
351694f7e29aSAlan Cox 	vm_map_lock_read(map);
351794f7e29aSAlan Cox 
351894f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
351994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
352094f7e29aSAlan Cox 		vm_map_unlock_read(map);
35210cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
352294f7e29aSAlan Cox 	}
352394f7e29aSAlan Cox 
3524b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3525b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3526b21a0008SMarcel Moolenaar 		/*
3527b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3528b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3529b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3530b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
353194f7e29aSAlan Cox 		 */
3532b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
353394f7e29aSAlan Cox 			vm_map_unlock_read(map);
35340cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
353594f7e29aSAlan Cox 		}
3536b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3537b21a0008SMarcel Moolenaar 	} else {
3538b21a0008SMarcel Moolenaar 		/*
3539b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3540b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3541b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3542b21a0008SMarcel Moolenaar 		 * select one.
3543b21a0008SMarcel Moolenaar 		 */
3544b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3545b21a0008SMarcel Moolenaar 			/*
3546b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3547b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3548b21a0008SMarcel Moolenaar 			 */
3549b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3550b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3551b21a0008SMarcel Moolenaar 			else
3552b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3553b21a0008SMarcel Moolenaar 		} else
3554b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3555b21a0008SMarcel Moolenaar 	}
355694f7e29aSAlan Cox 
3557b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3558b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3559b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3560b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3561b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
356294f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3563b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3564b21a0008SMarcel Moolenaar 	} else {
3565b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
356608667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3567b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3568b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3569fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3570b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3571b21a0008SMarcel Moolenaar 	}
3572b21a0008SMarcel Moolenaar 
357394f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
357494f7e29aSAlan Cox 		vm_map_unlock_read(map);
35750cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
357694f7e29aSAlan Cox 	}
357794f7e29aSAlan Cox 
3578b21a0008SMarcel Moolenaar 	/*
3579b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3580b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3581b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3582b21a0008SMarcel Moolenaar 	 * and is probably an error.
358394f7e29aSAlan Cox 	 *
3584b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3585b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
358694f7e29aSAlan Cox 	 */
35879a6d144fSKonstantin Belousov 	if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
358825adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
358994f7e29aSAlan Cox 			goto Retry;
359094f7e29aSAlan Cox 
3591b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
359294f7e29aSAlan Cox 
359394f7e29aSAlan Cox 		vm_map_unlock(map);
35940cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
359594f7e29aSAlan Cox 	}
359694f7e29aSAlan Cox 
3597b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
359894f7e29aSAlan Cox 
3599b21a0008SMarcel Moolenaar 	/*
3600b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3601b21a0008SMarcel Moolenaar 	 * limit.
360294f7e29aSAlan Cox 	 */
360391d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
360494f7e29aSAlan Cox 		vm_map_unlock_read(map);
36050cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
360694f7e29aSAlan Cox 	}
3607afcc55f3SEdward Tomasz Napierala #ifdef RACCT
36081ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
36091ba5ad42SEdward Tomasz Napierala 	if (is_procstack &&
36101ba5ad42SEdward Tomasz Napierala 	    racct_set(p, RACCT_STACK, ctob(vm->vm_ssize) + grow_amount)) {
36111ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
36121ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
36131ba5ad42SEdward Tomasz Napierala 		return (KERN_NO_SPACE);
36141ba5ad42SEdward Tomasz Napierala 	}
36151ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3616afcc55f3SEdward Tomasz Napierala #endif
361794f7e29aSAlan Cox 
3618cfe52ecfSAndrey Zonov 	/* Round up the grow amount modulo sgrowsiz */
3619cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3620cfe52ecfSAndrey Zonov 	grow_amount = roundup(grow_amount, growsize);
3621b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
362294f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
362391d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3624e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
3625e4826248SAlan Cox 		    ctob(vm->vm_ssize);
362694f7e29aSAlan Cox 	}
36271ba5ad42SEdward Tomasz Napierala #ifdef notyet
36281ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
36291ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
36301ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
36311ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
36321ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
36331ba5ad42SEdward Tomasz Napierala #endif
36347e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
36353ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
36367e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
36377e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
36387e19eda4SAndrey Zonov 			goto out;
36397e19eda4SAndrey Zonov 		}
36407e19eda4SAndrey Zonov #ifdef RACCT
36417e19eda4SAndrey Zonov 		PROC_LOCK(p);
36427e19eda4SAndrey Zonov 		if (racct_set(p, RACCT_MEMLOCK,
36433ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
36447e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
36457e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
36467e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
36477e19eda4SAndrey Zonov 			goto out;
36487e19eda4SAndrey Zonov 		}
36497e19eda4SAndrey Zonov 		PROC_UNLOCK(p);
36507e19eda4SAndrey Zonov #endif
36517e19eda4SAndrey Zonov 	}
3652a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
365391d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3654a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
36551ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
36561ba5ad42SEdward Tomasz Napierala 		goto out;
3657a69ac174SMatthew Dillon 	}
3658afcc55f3SEdward Tomasz Napierala #ifdef RACCT
36591ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
36601ba5ad42SEdward Tomasz Napierala 	if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
36611ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
36621ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
36631ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
36641ba5ad42SEdward Tomasz Napierala 		goto out;
36651ba5ad42SEdward Tomasz Napierala 	}
36661ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3667afcc55f3SEdward Tomasz Napierala #endif
3668a69ac174SMatthew Dillon 
366925adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
367094f7e29aSAlan Cox 		goto Retry;
367194f7e29aSAlan Cox 
3672b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3673b21a0008SMarcel Moolenaar 		/*
3674b21a0008SMarcel Moolenaar 		 * Growing downward.
3675b21a0008SMarcel Moolenaar 		 */
367694f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
367794f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
367894f7e29aSAlan Cox 
3679b21a0008SMarcel Moolenaar 		/*
3680b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3681b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
368294f7e29aSAlan Cox 		 */
368394f7e29aSAlan Cox 		if (addr < end) {
3684b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
368594f7e29aSAlan Cox 			addr = end;
36869a6d144fSKonstantin Belousov 			if (stack_guard_page)
36879a6d144fSKonstantin Belousov 				addr += PAGE_SIZE;
368894f7e29aSAlan Cox 		}
368994f7e29aSAlan Cox 
369094f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
369183ce0853SKonstantin Belousov 		    next_entry->protection, next_entry->max_protection, 0);
369294f7e29aSAlan Cox 
369394f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
369494f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
369529b45e9eSAlan Cox 			if (prev_entry != &map->header)
369629b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3697b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3698b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3699b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3700b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3701b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3702b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3703b21a0008SMarcel Moolenaar 			    grow_amount;
3704b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3705b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
370694f7e29aSAlan Cox 		}
3707b21a0008SMarcel Moolenaar 	} else {
3708b21a0008SMarcel Moolenaar 		/*
3709b21a0008SMarcel Moolenaar 		 * Growing upward.
3710b21a0008SMarcel Moolenaar 		 */
3711b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3712b21a0008SMarcel Moolenaar 
3713b21a0008SMarcel Moolenaar 		/*
3714b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3715b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3716b21a0008SMarcel Moolenaar 		 */
3717b21a0008SMarcel Moolenaar 		if (addr > end) {
3718b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3719b21a0008SMarcel Moolenaar 			addr = end;
37209a6d144fSKonstantin Belousov 			if (stack_guard_page)
37219a6d144fSKonstantin Belousov 				addr -= PAGE_SIZE;
372294f7e29aSAlan Cox 		}
372394f7e29aSAlan Cox 
3724b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
3725ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
3726ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
3727ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
3728ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
37293364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3730b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
37313364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3732b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
373357a21abaSAlan Cox 			 stack_entry->offset,
3734b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
3735ef694c1aSEdward Tomasz Napierala 			 (vm_size_t)grow_amount, cred != NULL)) {
373608667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3737b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3738b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3739199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
37400164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3741b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3742b21a0008SMarcel Moolenaar 
3743b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3744b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3745b21a0008SMarcel Moolenaar 		} else
3746b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3747b21a0008SMarcel Moolenaar 	}
3748b21a0008SMarcel Moolenaar 
3749b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3750b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3751b21a0008SMarcel Moolenaar 
375294f7e29aSAlan Cox 	vm_map_unlock(map);
3753b21a0008SMarcel Moolenaar 
3754abd498aaSBruce M Simpson 	/*
3755abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3756abd498aaSBruce M Simpson 	 */
3757b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3758b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3759b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3760b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3761b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3762b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3763b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3764b21a0008SMarcel Moolenaar 	}
3765abd498aaSBruce M Simpson 
37661ba5ad42SEdward Tomasz Napierala out:
3767afcc55f3SEdward Tomasz Napierala #ifdef RACCT
37681ba5ad42SEdward Tomasz Napierala 	if (rv != KERN_SUCCESS) {
37691ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
37701ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
37711ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
37727e19eda4SAndrey Zonov 		if (!old_mlock) {
37737e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
37743ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
37757e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
37767e19eda4SAndrey Zonov 		}
37771ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
37781ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
37791ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
37801ba5ad42SEdward Tomasz Napierala 	}
3781afcc55f3SEdward Tomasz Napierala #endif
37821ba5ad42SEdward Tomasz Napierala 
37830cddd8f0SMatthew Dillon 	return (rv);
378494f7e29aSAlan Cox }
378594f7e29aSAlan Cox 
3786df8bae1dSRodney W. Grimes /*
37875856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
37885856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
37895856e12eSJohn Dyson  */
379089b57fcfSKonstantin Belousov int
37913ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
37921b40f8c0SMatthew Dillon {
37935856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
37945856e12eSJohn Dyson 	struct vmspace *newvmspace;
37955856e12eSJohn Dyson 
37967032434eSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
37977032434eSKonstantin Belousov 	    ("vmspace_exec recursed"));
379874d1d2b7SNeel Natu 	newvmspace = vmspace_alloc(minuser, maxuser, NULL);
379989b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
380089b57fcfSKonstantin Belousov 		return (ENOMEM);
380151ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
38025856e12eSJohn Dyson 	/*
38035856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
38045856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
38055856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
38065856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
38075856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
38085856e12eSJohn Dyson 	 */
380957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
38105856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
381157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
38126617724cSJeff Roberson 	if (p == curthread->td_proc)
3813b40ce416SJulian Elischer 		pmap_activate(curthread);
38147032434eSKonstantin Belousov 	curthread->td_pflags |= TDP_EXECVMSPC;
381589b57fcfSKonstantin Belousov 	return (0);
38165856e12eSJohn Dyson }
38175856e12eSJohn Dyson 
38185856e12eSJohn Dyson /*
38195856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
38205856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
38215856e12eSJohn Dyson  */
382289b57fcfSKonstantin Belousov int
38231b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
38241b40f8c0SMatthew Dillon {
38255856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
38265856e12eSJohn Dyson 	struct vmspace *newvmspace;
38273364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
38285856e12eSJohn Dyson 
38295856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
383089b57fcfSKonstantin Belousov 		return (0);
38313364c323SKonstantin Belousov 	fork_charge = 0;
38323364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
383389b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
383489b57fcfSKonstantin Belousov 		return (ENOMEM);
3835ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
38363364c323SKonstantin Belousov 		vmspace_free(newvmspace);
38373364c323SKonstantin Belousov 		return (ENOMEM);
38383364c323SKonstantin Belousov 	}
383957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
38405856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
384157051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
38426617724cSJeff Roberson 	if (p == curthread->td_proc)
3843b40ce416SJulian Elischer 		pmap_activate(curthread);
3844b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
384589b57fcfSKonstantin Belousov 	return (0);
38465856e12eSJohn Dyson }
38475856e12eSJohn Dyson 
38485856e12eSJohn Dyson /*
3849df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3850df8bae1dSRodney W. Grimes  *
3851df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3852df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3853df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3854df8bae1dSRodney W. Grimes  *	type specified.
3855df8bae1dSRodney W. Grimes  *
3856df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3857df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3858df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3859df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3860df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3861df8bae1dSRodney W. Grimes  *
3862df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3863df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3864df8bae1dSRodney W. Grimes  *
3865df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3866df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3867df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3868df8bae1dSRodney W. Grimes  *	remain the same.
3869df8bae1dSRodney W. Grimes  */
3870df8bae1dSRodney W. Grimes int
3871b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3872b9dcd593SBruce Evans 	      vm_offset_t vaddr,
387347221757SJohn Dyson 	      vm_prot_t fault_typea,
3874b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3875b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3876b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3877b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
38782d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3879df8bae1dSRodney W. Grimes {
3880c0877f10SJohn Dyson 	vm_map_entry_t entry;
3881c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3882c0877f10SJohn Dyson 	vm_prot_t prot;
388347221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
38843364c323SKonstantin Belousov 	vm_object_t eobject;
38850cc74f14SAlan Cox 	vm_size_t size;
3886ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
3887df8bae1dSRodney W. Grimes 
3888df8bae1dSRodney W. Grimes RetryLookup:;
3889df8bae1dSRodney W. Grimes 
3890df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3891df8bae1dSRodney W. Grimes 
3892df8bae1dSRodney W. Grimes 	/*
38934c3ef59eSAlan Cox 	 * Lookup the faulting address.
3894df8bae1dSRodney W. Grimes 	 */
3895095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3896095104acSAlan Cox 		vm_map_unlock_read(map);
3897095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3898095104acSAlan Cox 	}
3899df8bae1dSRodney W. Grimes 
39004e94f402SAlan Cox 	entry = *out_entry;
3901b7b2aac2SJohn Dyson 
3902df8bae1dSRodney W. Grimes 	/*
3903df8bae1dSRodney W. Grimes 	 * Handle submaps.
3904df8bae1dSRodney W. Grimes 	 */
3905afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3906df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3907df8bae1dSRodney W. Grimes 
3908df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3909df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3910df8bae1dSRodney W. Grimes 		goto RetryLookup;
3911df8bae1dSRodney W. Grimes 	}
3912a04c970aSJohn Dyson 
3913df8bae1dSRodney W. Grimes 	/*
39140d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3915df8bae1dSRodney W. Grimes 	 */
3916df8bae1dSRodney W. Grimes 	prot = entry->protection;
391747221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
39182db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3919095104acSAlan Cox 		vm_map_unlock_read(map);
3920095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
392147221757SJohn Dyson 	}
39222ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
392347221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3924a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3925095104acSAlan Cox 		vm_map_unlock_read(map);
3926095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3927a04c970aSJohn Dyson 	}
39285b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
39295b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
39305b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
39315b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
39325b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
39335b3e0257SDag-Erling Smørgrav 	}
3934df8bae1dSRodney W. Grimes 
3935df8bae1dSRodney W. Grimes 	/*
39360d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
39370d94caffSDavid Greenman 	 * accesses.
3938df8bae1dSRodney W. Grimes 	 */
393905f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
394005f0fdd2SPoul-Henning Kamp 	if (*wired)
3941a6d42a0dSAlan Cox 		fault_type = entry->protection;
39423364c323SKonstantin Belousov 	size = entry->end - entry->start;
3943df8bae1dSRodney W. Grimes 	/*
3944df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3945df8bae1dSRodney W. Grimes 	 */
3946afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3947df8bae1dSRodney W. Grimes 		/*
39480d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3949ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3950df8bae1dSRodney W. Grimes 		 *
39510d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
39520d94caffSDavid Greenman 		 * permissions allowed.
3953df8bae1dSRodney W. Grimes 		 */
3954a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3955a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
3956df8bae1dSRodney W. Grimes 			/*
39570d94caffSDavid Greenman 			 * Make a new object, and place it in the object
39580d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3959ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
39600d94caffSDavid Greenman 			 * object.
3961df8bae1dSRodney W. Grimes 			 */
396225adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3963df8bae1dSRodney W. Grimes 				goto RetryLookup;
39649917e010SAlan Cox 
3965ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
39663364c323SKonstantin Belousov 				/*
39673364c323SKonstantin Belousov 				 * The debugger owner is charged for
39683364c323SKonstantin Belousov 				 * the memory.
39693364c323SKonstantin Belousov 				 */
3970ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3971ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3972ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
3973ef694c1aSEdward Tomasz Napierala 					crfree(cred);
39743364c323SKonstantin Belousov 					vm_map_unlock(map);
39753364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
39763364c323SKonstantin Belousov 				}
3977ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
39783364c323SKonstantin Belousov 			}
39790cc74f14SAlan Cox 			vm_object_shadow(&entry->object.vm_object,
39800cc74f14SAlan Cox 			    &entry->offset, size);
3981afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
39823364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
3983ef694c1aSEdward Tomasz Napierala 			if (eobject->cred != NULL) {
39843364c323SKonstantin Belousov 				/*
39853364c323SKonstantin Belousov 				 * The object was not shadowed.
39863364c323SKonstantin Belousov 				 */
3987ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
3988ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
3989ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
3990ef694c1aSEdward Tomasz Napierala 			} else if (entry->cred != NULL) {
399189f6b863SAttilio Rao 				VM_OBJECT_WLOCK(eobject);
3992ef694c1aSEdward Tomasz Napierala 				eobject->cred = entry->cred;
39933364c323SKonstantin Belousov 				eobject->charge = size;
399489f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(eobject);
3995ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
39963364c323SKonstantin Belousov 			}
39979917e010SAlan Cox 
39989b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
39990d94caffSDavid Greenman 		} else {
4000df8bae1dSRodney W. Grimes 			/*
40010d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
40020d94caffSDavid Greenman 			 * don't allow writes.
4003df8bae1dSRodney W. Grimes 			 */
40042d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
4005df8bae1dSRodney W. Grimes 		}
4006df8bae1dSRodney W. Grimes 	}
40072d8acc0fSJohn Dyson 
4008df8bae1dSRodney W. Grimes 	/*
4009df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
4010df8bae1dSRodney W. Grimes 	 */
40114e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
40124e71e795SMatthew Dillon 	    !map->system_map) {
401325adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
4014df8bae1dSRodney W. Grimes 			goto RetryLookup;
401524a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
40163364c323SKonstantin Belousov 		    atop(size));
4017df8bae1dSRodney W. Grimes 		entry->offset = 0;
4018ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
401989f6b863SAttilio Rao 			VM_OBJECT_WLOCK(entry->object.vm_object);
4020ef694c1aSEdward Tomasz Napierala 			entry->object.vm_object->cred = entry->cred;
40213364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
402289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
4023ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
40243364c323SKonstantin Belousov 		}
40259b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
4026df8bae1dSRodney W. Grimes 	}
4027b5b40fa6SJohn Dyson 
4028df8bae1dSRodney W. Grimes 	/*
40290d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
40300d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
4031df8bae1dSRodney W. Grimes 	 */
40329b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4033df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
4034df8bae1dSRodney W. Grimes 
4035df8bae1dSRodney W. Grimes 	*out_prot = prot;
4036df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
4037df8bae1dSRodney W. Grimes }
4038df8bae1dSRodney W. Grimes 
4039df8bae1dSRodney W. Grimes /*
404019dc5607STor Egge  *	vm_map_lookup_locked:
404119dc5607STor Egge  *
404219dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
404319dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
404419dc5607STor Egge  */
404519dc5607STor Egge int
404619dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
404719dc5607STor Egge 		     vm_offset_t vaddr,
404819dc5607STor Egge 		     vm_prot_t fault_typea,
404919dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
405019dc5607STor Egge 		     vm_object_t *object,	/* OUT */
405119dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
405219dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
405319dc5607STor Egge 		     boolean_t *wired)		/* OUT */
405419dc5607STor Egge {
405519dc5607STor Egge 	vm_map_entry_t entry;
405619dc5607STor Egge 	vm_map_t map = *var_map;
405719dc5607STor Egge 	vm_prot_t prot;
405819dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
405919dc5607STor Egge 
406019dc5607STor Egge 	/*
40614c3ef59eSAlan Cox 	 * Lookup the faulting address.
406219dc5607STor Egge 	 */
406319dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
406419dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
406519dc5607STor Egge 
406619dc5607STor Egge 	entry = *out_entry;
406719dc5607STor Egge 
406819dc5607STor Egge 	/*
406919dc5607STor Egge 	 * Fail if the entry refers to a submap.
407019dc5607STor Egge 	 */
407119dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
407219dc5607STor Egge 		return (KERN_FAILURE);
407319dc5607STor Egge 
407419dc5607STor Egge 	/*
407519dc5607STor Egge 	 * Check whether this task is allowed to have this page.
407619dc5607STor Egge 	 */
407719dc5607STor Egge 	prot = entry->protection;
407819dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
407919dc5607STor Egge 	if ((fault_type & prot) != fault_type)
408019dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
408119dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
408219dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
4083a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
408419dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
408519dc5607STor Egge 
408619dc5607STor Egge 	/*
408719dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
408819dc5607STor Egge 	 * accesses.
408919dc5607STor Egge 	 */
409019dc5607STor Egge 	*wired = (entry->wired_count != 0);
409119dc5607STor Egge 	if (*wired)
4092a6d42a0dSAlan Cox 		fault_type = entry->protection;
409319dc5607STor Egge 
409419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
409519dc5607STor Egge 		/*
409619dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
409719dc5607STor Egge 		 */
409819dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
409919dc5607STor Egge 			return (KERN_FAILURE);
410019dc5607STor Egge 		/*
410119dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
410219dc5607STor Egge 		 * don't allow writes.
410319dc5607STor Egge 		 */
410419dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
410519dc5607STor Egge 	}
410619dc5607STor Egge 
410719dc5607STor Egge 	/*
410819dc5607STor Egge 	 * Fail if an object should be created.
410919dc5607STor Egge 	 */
411019dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
411119dc5607STor Egge 		return (KERN_FAILURE);
411219dc5607STor Egge 
411319dc5607STor Egge 	/*
411419dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
411519dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
411619dc5607STor Egge 	 */
411719dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
411819dc5607STor Egge 	*object = entry->object.vm_object;
411919dc5607STor Egge 
412019dc5607STor Egge 	*out_prot = prot;
412119dc5607STor Egge 	return (KERN_SUCCESS);
412219dc5607STor Egge }
412319dc5607STor Egge 
412419dc5607STor Egge /*
4125df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
4126df8bae1dSRodney W. Grimes  *
4127df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
4128df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
4129df8bae1dSRodney W. Grimes  */
41300d94caffSDavid Greenman void
41311b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4132df8bae1dSRodney W. Grimes {
4133df8bae1dSRodney W. Grimes 	/*
4134df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
4135df8bae1dSRodney W. Grimes 	 */
4136df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
4137df8bae1dSRodney W. Grimes }
4138df8bae1dSRodney W. Grimes 
4139c7c34a24SBruce Evans #include "opt_ddb.h"
4140c3cb3e12SDavid Greenman #ifdef DDB
4141c7c34a24SBruce Evans #include <sys/kernel.h>
4142c7c34a24SBruce Evans 
4143c7c34a24SBruce Evans #include <ddb/ddb.h>
4144c7c34a24SBruce Evans 
41452ebcd458SAttilio Rao static void
41462ebcd458SAttilio Rao vm_map_print(vm_map_t map)
4147df8bae1dSRodney W. Grimes {
4148c0877f10SJohn Dyson 	vm_map_entry_t entry;
4149c7c34a24SBruce Evans 
4150e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4151e5f251d2SAlan Cox 	    (void *)map,
4152101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
4153df8bae1dSRodney W. Grimes 
4154c7c34a24SBruce Evans 	db_indent += 2;
4155df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
4156df8bae1dSRodney W. Grimes 	    entry = entry->next) {
4157fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
4158fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
4159e5f251d2SAlan Cox 		{
4160df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
4161df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
41620d94caffSDavid Greenman 
416395e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
4164df8bae1dSRodney W. Grimes 			    entry->protection,
4165df8bae1dSRodney W. Grimes 			    entry->max_protection,
41668aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4167df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
416895e5e988SJohn Dyson 				db_printf(", wired");
4169df8bae1dSRodney W. Grimes 		}
41709fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4171cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
41729fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
4173cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4174df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
41759fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
41769fdfe602SMatthew Dillon 				entry->object.sub_map)) {
4177c7c34a24SBruce Evans 				db_indent += 2;
41782ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
4179c7c34a24SBruce Evans 				db_indent -= 2;
4180df8bae1dSRodney W. Grimes 			}
41810d94caffSDavid Greenman 		} else {
4182ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
4183ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
4184cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
4185101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
4186cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4187ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
4188ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
4189ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
41903364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
4191afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
4192c7c34a24SBruce Evans 				db_printf(", copy (%s)",
4193afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4194c7c34a24SBruce Evans 			db_printf("\n");
4195df8bae1dSRodney W. Grimes 
4196df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
4197df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
4198df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
4199c7c34a24SBruce Evans 				db_indent += 2;
4200101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
4201101eeb7fSBruce Evans 						entry->object.vm_object,
420244bbc3b7SKonstantin Belousov 						0, 0, (char *)0);
4203c7c34a24SBruce Evans 				db_indent -= 2;
4204df8bae1dSRodney W. Grimes 			}
4205df8bae1dSRodney W. Grimes 		}
4206df8bae1dSRodney W. Grimes 	}
4207c7c34a24SBruce Evans 	db_indent -= 2;
4208df8bae1dSRodney W. Grimes }
420995e5e988SJohn Dyson 
42102ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
42112ebcd458SAttilio Rao {
42122ebcd458SAttilio Rao 
42132ebcd458SAttilio Rao 	if (!have_addr) {
42142ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
42152ebcd458SAttilio Rao 		return;
42162ebcd458SAttilio Rao 	}
42172ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
42182ebcd458SAttilio Rao }
421995e5e988SJohn Dyson 
422095e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
422195e5e988SJohn Dyson {
422295e5e988SJohn Dyson 	struct proc *p;
422395e5e988SJohn Dyson 
422495e5e988SJohn Dyson 	if (have_addr) {
422595e5e988SJohn Dyson 		p = (struct proc *) addr;
422695e5e988SJohn Dyson 	} else {
422795e5e988SJohn Dyson 		p = curproc;
422895e5e988SJohn Dyson 	}
422995e5e988SJohn Dyson 
4230ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4231ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4232b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
423395e5e988SJohn Dyson 
42342ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
423595e5e988SJohn Dyson }
423695e5e988SJohn Dyson 
4237c7c34a24SBruce Evans #endif /* DDB */
4238