xref: /freebsd/sys/vm/vm_map.c (revision 5b3e02570aad765ee54aa9ba564aa97f52101227)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
709a6d144fSKonstantin Belousov #include <sys/kernel.h>
7161d80e90SJohn Baldwin #include <sys/ktr.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73fb919e4dSMark Murray #include <sys/mutex.h>
74b5e8ce9fSBruce Evans #include <sys/proc.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
771efb74fbSJohn Dyson #include <sys/vnode.h>
781ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
792267af78SJulian Elischer #include <sys/resourcevar.h>
8089f6b863SAttilio Rao #include <sys/rwlock.h>
813fde38dfSMike Silbersack #include <sys/file.h>
829a6d144fSKonstantin Belousov #include <sys/sysctl.h>
8305ba50f5SJake Burkholder #include <sys/sysent.h>
843db161e0SMatthew Dillon #include <sys/shm.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9247221757SJohn Dyson #include <vm/vm_pager.h>
9326f9a767SRodney W. Grimes #include <vm/vm_kern.h>
94efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9584110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
9621cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
97670d17b5SJeff Roberson #include <vm/uma.h>
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
101df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
102df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
103df8bae1dSRodney W. Grimes  *	memory from one map to another.
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
108e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
109e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
110df8bae1dSRodney W. Grimes  *
111956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
112df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
113df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
114df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
115df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
116df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
117df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
120ad5fca3bSAlan Cox  *	by copying VM object references from one map to
121df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
122df8bae1dSRodney W. Grimes  */
123df8bae1dSRodney W. Grimes 
1243a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1258355f576SJeff Roberson static uma_zone_t mapentzone;
1268355f576SJeff Roberson static uma_zone_t kmapentzone;
1278355f576SJeff Roberson static uma_zone_t mapzone;
1288355f576SJeff Roberson static uma_zone_t vmspace_zone;
129b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
1308355f576SJeff Roberson static void vmspace_zfini(void *mem, int size);
131b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
1328355f576SJeff Roberson static void vm_map_zfini(void *mem, int size);
13392351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13492351f16SAlan Cox     vm_offset_t max);
1350b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
136655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
1378355f576SJeff Roberson #ifdef INVARIANTS
1388355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1398355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1408355f576SJeff Roberson #endif
141b18bfc3dSJohn Dyson 
142ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
143ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1443364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1453364c323SKonstantin Belousov 
14657051fdcSTor Egge /*
14757051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
14857051fdcSTor Egge  * stable.
14957051fdcSTor Egge  */
15057051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15157051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15257051fdcSTor Egge 
153d239bd3cSKonstantin Belousov /*
154d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
155d239bd3cSKonstantin Belousov  *
156d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
157d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
158d239bd3cSKonstantin Belousov  */
159d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
160d239bd3cSKonstantin Belousov 		{					\
161d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
162d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
163d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
164d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
165d239bd3cSKonstantin Belousov 		if (start > end)			\
166d239bd3cSKonstantin Belousov 			start = end;			\
167d239bd3cSKonstantin Belousov 		}
168d239bd3cSKonstantin Belousov 
1696fecb26bSKonstantin Belousov /*
1706fecb26bSKonstantin Belousov  *	vm_map_startup:
1716fecb26bSKonstantin Belousov  *
1726fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1736fecb26bSKonstantin Belousov  *	any other vm_map routines.
1746fecb26bSKonstantin Belousov  *
1756fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1766fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1776fecb26bSKonstantin Belousov  *
1786fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1796fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1806fecb26bSKonstantin Belousov  *
1816fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1826fecb26bSKonstantin Belousov  *	maps and requires map entries.
1836fecb26bSKonstantin Belousov  */
1846fecb26bSKonstantin Belousov 
1850d94caffSDavid Greenman void
1861b40f8c0SMatthew Dillon vm_map_startup(void)
187df8bae1dSRodney W. Grimes {
1883a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1898355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1908355f576SJeff Roberson #ifdef INVARIANTS
1918355f576SJeff Roberson 	    vm_map_zdtor,
1928355f576SJeff Roberson #else
1938355f576SJeff Roberson 	    NULL,
1948355f576SJeff Roberson #endif
1958355f576SJeff Roberson 	    vm_map_zinit, vm_map_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
1968355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
197670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
19818aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
19918aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
200670d17b5SJeff Roberson 	uma_prealloc(kmapentzone, MAX_KMAPENT);
201670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
202670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
203df8bae1dSRodney W. Grimes }
204df8bae1dSRodney W. Grimes 
2058355f576SJeff Roberson static void
2068355f576SJeff Roberson vmspace_zfini(void *mem, int size)
2078355f576SJeff Roberson {
2088355f576SJeff Roberson 	struct vmspace *vm;
2098355f576SJeff Roberson 
2108355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2118355f576SJeff Roberson 	vm_map_zfini(&vm->vm_map, sizeof(vm->vm_map));
2128355f576SJeff Roberson }
2138355f576SJeff Roberson 
214b23f72e9SBrian Feldman static int
215b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2168355f576SJeff Roberson {
2178355f576SJeff Roberson 	struct vmspace *vm;
2188355f576SJeff Roberson 
2198355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2208355f576SJeff Roberson 
22189b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
222b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
223b23f72e9SBrian Feldman 	return (0);
2248355f576SJeff Roberson }
2258355f576SJeff Roberson 
2268355f576SJeff Roberson static void
2278355f576SJeff Roberson vm_map_zfini(void *mem, int size)
2288355f576SJeff Roberson {
2298355f576SJeff Roberson 	vm_map_t map;
2308355f576SJeff Roberson 
2318355f576SJeff Roberson 	map = (vm_map_t)mem;
23236daaecdSAlan Cox 	mtx_destroy(&map->system_mtx);
23312c64974SMaxime Henrion 	sx_destroy(&map->lock);
2348355f576SJeff Roberson }
2358355f576SJeff Roberson 
236b23f72e9SBrian Feldman static int
237b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2388355f576SJeff Roberson {
2398355f576SJeff Roberson 	vm_map_t map;
2408355f576SJeff Roberson 
2418355f576SJeff Roberson 	map = (vm_map_t)mem;
2428355f576SJeff Roberson 	map->nentries = 0;
2438355f576SJeff Roberson 	map->size = 0;
244e30df26eSAlan Cox 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
245e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
246b23f72e9SBrian Feldman 	return (0);
2478355f576SJeff Roberson }
2488355f576SJeff Roberson 
2498355f576SJeff Roberson #ifdef INVARIANTS
2508355f576SJeff Roberson static void
2518355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2528355f576SJeff Roberson {
2538355f576SJeff Roberson 	struct vmspace *vm;
2548355f576SJeff Roberson 
2558355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2568355f576SJeff Roberson 
2578355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2588355f576SJeff Roberson }
2598355f576SJeff Roberson static void
2608355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2618355f576SJeff Roberson {
2628355f576SJeff Roberson 	vm_map_t map;
2638355f576SJeff Roberson 
2648355f576SJeff Roberson 	map = (vm_map_t)mem;
2658355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2668355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2678355f576SJeff Roberson 	    map, map->nentries));
2688355f576SJeff Roberson 	KASSERT(map->size == 0,
2698355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2709eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2718355f576SJeff Roberson }
2728355f576SJeff Roberson #endif	/* INVARIANTS */
2738355f576SJeff Roberson 
274df8bae1dSRodney W. Grimes /*
275df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
276df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
277df8bae1dSRodney W. Grimes  */
278df8bae1dSRodney W. Grimes struct vmspace *
2792d8acc0fSJohn Dyson vmspace_alloc(min, max)
280df8bae1dSRodney W. Grimes 	vm_offset_t min, max;
281df8bae1dSRodney W. Grimes {
282c0877f10SJohn Dyson 	struct vmspace *vm;
2830d94caffSDavid Greenman 
284a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
28589b57fcfSKonstantin Belousov 	if (vm->vm_map.pmap == NULL && !pmap_pinit(vmspace_pmap(vm))) {
28689b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
28789b57fcfSKonstantin Belousov 		return (NULL);
28889b57fcfSKonstantin Belousov 	}
28921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
29092351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
291df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2922d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
29351ab6c28SAlan Cox 	vm->vm_swrss = 0;
29451ab6c28SAlan Cox 	vm->vm_tsize = 0;
29551ab6c28SAlan Cox 	vm->vm_dsize = 0;
29651ab6c28SAlan Cox 	vm->vm_ssize = 0;
29751ab6c28SAlan Cox 	vm->vm_taddr = 0;
29851ab6c28SAlan Cox 	vm->vm_daddr = 0;
29951ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
300df8bae1dSRodney W. Grimes 	return (vm);
301df8bae1dSRodney W. Grimes }
302df8bae1dSRodney W. Grimes 
303df8bae1dSRodney W. Grimes void
3041b40f8c0SMatthew Dillon vm_init2(void)
3051b40f8c0SMatthew Dillon {
306a4915c21SAttilio Rao 	uma_zone_reserve_kva(kmapentzone, lmin(cnt.v_page_count,
307c1f02198SAlan Cox 	    (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) / PAGE_SIZE) / 8 +
3083fde38dfSMike Silbersack 	     maxproc * 2 + maxfiles);
3098355f576SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
3108355f576SJeff Roberson #ifdef INVARIANTS
3118355f576SJeff Roberson 	    vmspace_zdtor,
3128355f576SJeff Roberson #else
3138355f576SJeff Roberson 	    NULL,
3148355f576SJeff Roberson #endif
3158355f576SJeff Roberson 	    vmspace_zinit, vmspace_zfini, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
3163075778bSJohn Dyson }
3173075778bSJohn Dyson 
3181ba5ad42SEdward Tomasz Napierala static void
3191ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3201ba5ad42SEdward Tomasz Napierala {
3211ba5ad42SEdward Tomasz Napierala 
322afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3231ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3241ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3251ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3261ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3271ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3281ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3291ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
330afcc55f3SEdward Tomasz Napierala #endif
3311ba5ad42SEdward Tomasz Napierala }
3321ba5ad42SEdward Tomasz Napierala 
33362a59e8fSWarner Losh static inline void
334582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
335df8bae1dSRodney W. Grimes {
3360ef12795SAlan Cox 
33721c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3383db161e0SMatthew Dillon 
3393db161e0SMatthew Dillon 	/*
3403db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3413db161e0SMatthew Dillon 	 * exit1().
3423db161e0SMatthew Dillon 	 */
3433db161e0SMatthew Dillon 	shmexit(vm);
3443db161e0SMatthew Dillon 
34530dcfc09SJohn Dyson 	/*
346df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3470d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3480d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
349df8bae1dSRodney W. Grimes 	 */
350717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
351df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3528355f576SJeff Roberson 
3530ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3540ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3558355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
356df8bae1dSRodney W. Grimes }
357582ec34cSAlfred Perlstein 
358582ec34cSAlfred Perlstein void
359582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
360582ec34cSAlfred Perlstein {
361582ec34cSAlfred Perlstein 
362582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
363582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
364582ec34cSAlfred Perlstein 
3651a587ef2SJohn Baldwin 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
366582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
367582ec34cSAlfred Perlstein }
368582ec34cSAlfred Perlstein 
369582ec34cSAlfred Perlstein void
370582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
371582ec34cSAlfred Perlstein {
372334f7061SPeter Wemm 	struct vmspace *vm;
373582ec34cSAlfred Perlstein 
37457051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
375334f7061SPeter Wemm 	vm = p->p_vmspace;
376334f7061SPeter Wemm 	p->p_vmspace = NULL;
37757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37857051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
37957051fdcSTor Egge 	vmspace_free(vm);
38057051fdcSTor Egge }
38157051fdcSTor Egge 
38257051fdcSTor Egge void
38357051fdcSTor Egge vmspace_exit(struct thread *td)
38457051fdcSTor Egge {
38557051fdcSTor Egge 	int refcnt;
38657051fdcSTor Egge 	struct vmspace *vm;
38757051fdcSTor Egge 	struct proc *p;
388389d2b6eSMatthew Dillon 
389389d2b6eSMatthew Dillon 	/*
39057051fdcSTor Egge 	 * Release user portion of address space.
39157051fdcSTor Egge 	 * This releases references to vnodes,
39257051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
39357051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
394389d2b6eSMatthew Dillon 	 *
39557051fdcSTor Egge 	 * The last exiting process to reach this point releases as
39657051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
39757051fdcSTor Egge 	 * slower fallback in case another process had a temporary
39857051fdcSTor Egge 	 * reference to the vmspace.
399389d2b6eSMatthew Dillon 	 */
40057051fdcSTor Egge 
40157051fdcSTor Egge 	p = td->td_proc;
40257051fdcSTor Egge 	vm = p->p_vmspace;
40357051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
40457051fdcSTor Egge 	do {
40557051fdcSTor Egge 		refcnt = vm->vm_refcnt;
40657051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
40757051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
40857051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40957051fdcSTor Egge 			p->p_vmspace = &vmspace0;
41057051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
41157051fdcSTor Egge 			pmap_activate(td);
41257051fdcSTor Egge 		}
41357051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
41457051fdcSTor Egge 	if (refcnt == 1) {
41557051fdcSTor Egge 		if (p->p_vmspace != vm) {
41657051fdcSTor Egge 			/* vmspace not yet freed, switch back */
41757051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
41857051fdcSTor Egge 			p->p_vmspace = vm;
41957051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
42057051fdcSTor Egge 			pmap_activate(td);
42157051fdcSTor Egge 		}
42257051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
42357051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
42457051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
42557051fdcSTor Egge 		p->p_vmspace = &vmspace0;
42657051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42757051fdcSTor Egge 		pmap_activate(td);
428334f7061SPeter Wemm 		vmspace_dofree(vm);
429334f7061SPeter Wemm 	}
4301ba5ad42SEdward Tomasz Napierala 	vmspace_container_reset(p);
43157051fdcSTor Egge }
43257051fdcSTor Egge 
43357051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
43457051fdcSTor Egge 
43557051fdcSTor Egge struct vmspace *
43657051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
43757051fdcSTor Egge {
43857051fdcSTor Egge 	struct vmspace *vm;
43957051fdcSTor Egge 	int refcnt;
44057051fdcSTor Egge 
44157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
44257051fdcSTor Egge 	vm = p->p_vmspace;
44357051fdcSTor Egge 	if (vm == NULL) {
44457051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
44557051fdcSTor Egge 		return (NULL);
44657051fdcSTor Egge 	}
44757051fdcSTor Egge 	do {
44857051fdcSTor Egge 		refcnt = vm->vm_refcnt;
44957051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
45057051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
45157051fdcSTor Egge 			return (NULL);
45257051fdcSTor Egge 		}
45357051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
45457051fdcSTor Egge 	if (vm != p->p_vmspace) {
45557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
45657051fdcSTor Egge 		vmspace_free(vm);
45757051fdcSTor Egge 		return (NULL);
45857051fdcSTor Egge 	}
45957051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
46057051fdcSTor Egge 	return (vm);
46157051fdcSTor Egge }
462df8bae1dSRodney W. Grimes 
4631b40f8c0SMatthew Dillon void
464780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4651b40f8c0SMatthew Dillon {
466bc91c510SAlan Cox 
46793bc4879SAlan Cox 	if (map->system_map)
468ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
46912c64974SMaxime Henrion 	else
4709fde98bbSAttilio Rao 		sx_xlock_(&map->lock, file, line);
4711b40f8c0SMatthew Dillon 	map->timestamp++;
4721b40f8c0SMatthew Dillon }
4731b40f8c0SMatthew Dillon 
4740b367bd8SKonstantin Belousov static void
4750b367bd8SKonstantin Belousov vm_map_process_deferred(void)
4760e0af8ecSBrian Feldman {
4770b367bd8SKonstantin Belousov 	struct thread *td;
4786fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
47984110e7eSKonstantin Belousov 	vm_object_t object;
480655c3490SKonstantin Belousov 
4810b367bd8SKonstantin Belousov 	td = curthread;
4826fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
4836fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
4846fbe60faSJohn Baldwin 	while (entry != NULL) {
4856fbe60faSJohn Baldwin 		next = entry->next;
48684110e7eSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
48784110e7eSKonstantin Belousov 			/*
48884110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
48984110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
49084110e7eSKonstantin Belousov 			 */
49184110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
49284110e7eSKonstantin Belousov 			    ("Submap with writecount"));
49384110e7eSKonstantin Belousov 			object = entry->object.vm_object;
49484110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
49584110e7eSKonstantin Belousov 			vnode_pager_release_writecount(object, entry->start,
49684110e7eSKonstantin Belousov 			    entry->end);
49784110e7eSKonstantin Belousov 		}
4980b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
4996fbe60faSJohn Baldwin 		entry = next;
5000b367bd8SKonstantin Belousov 	}
5010b367bd8SKonstantin Belousov }
5020b367bd8SKonstantin Belousov 
5030b367bd8SKonstantin Belousov void
5040b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
5050b367bd8SKonstantin Belousov {
5060b367bd8SKonstantin Belousov 
5070b367bd8SKonstantin Belousov 	if (map->system_map)
508ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5090b367bd8SKonstantin Belousov 	else {
5109fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
5110b367bd8SKonstantin Belousov 		vm_map_process_deferred();
512655c3490SKonstantin Belousov 	}
5130e0af8ecSBrian Feldman }
5140e0af8ecSBrian Feldman 
5150e0af8ecSBrian Feldman void
516780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
5170e0af8ecSBrian Feldman {
518bc91c510SAlan Cox 
51993bc4879SAlan Cox 	if (map->system_map)
520ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
52112c64974SMaxime Henrion 	else
5229fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
52336daaecdSAlan Cox }
5240e0af8ecSBrian Feldman 
5250e0af8ecSBrian Feldman void
526780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
5270e0af8ecSBrian Feldman {
528bc91c510SAlan Cox 
52936daaecdSAlan Cox 	if (map->system_map)
530ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5310b367bd8SKonstantin Belousov 	else {
5329fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
5330b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5340b367bd8SKonstantin Belousov 	}
53525adb370SBrian Feldman }
53625adb370SBrian Feldman 
537d974f03cSAlan Cox int
538780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
539d974f03cSAlan Cox {
54025adb370SBrian Feldman 	int error;
54125adb370SBrian Feldman 
54236daaecdSAlan Cox 	error = map->system_map ?
543ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5449fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
5453a92e5d5SAlan Cox 	if (error == 0)
5463a92e5d5SAlan Cox 		map->timestamp++;
547bc91c510SAlan Cox 	return (error == 0);
5480e0af8ecSBrian Feldman }
5490e0af8ecSBrian Feldman 
5500e0af8ecSBrian Feldman int
55172d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
55272d97679SDavid Schultz {
55372d97679SDavid Schultz 	int error;
55472d97679SDavid Schultz 
55572d97679SDavid Schultz 	error = map->system_map ?
556ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5579fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
55872d97679SDavid Schultz 	return (error == 0);
55972d97679SDavid Schultz }
56072d97679SDavid Schultz 
56105a8c414SAlan Cox /*
56205a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
56305a8c414SAlan Cox  *
56405a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
56505a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
56605a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
56705a8c414SAlan Cox  *	returned without a read or write lock held.
56805a8c414SAlan Cox  *
56905a8c414SAlan Cox  *	Requires that the map be read locked.
57005a8c414SAlan Cox  */
57172d97679SDavid Schultz int
572780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5730e0af8ecSBrian Feldman {
57405a8c414SAlan Cox 	unsigned int last_timestamp;
575bc91c510SAlan Cox 
57612c64974SMaxime Henrion 	if (map->system_map) {
577ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
57805a8c414SAlan Cox 	} else {
5799fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
58005a8c414SAlan Cox 			last_timestamp = map->timestamp;
5819fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
5820b367bd8SKonstantin Belousov 			vm_map_process_deferred();
58305a8c414SAlan Cox 			/*
58405a8c414SAlan Cox 			 * If the map's timestamp does not change while the
58505a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
58605a8c414SAlan Cox 			 */
5879fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
58805a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
5899fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
59005a8c414SAlan Cox 				return (1);
59105a8c414SAlan Cox 			}
59205a8c414SAlan Cox 		}
59305a8c414SAlan Cox 	}
594bc91c510SAlan Cox 	map->timestamp++;
595bc91c510SAlan Cox 	return (0);
5960e0af8ecSBrian Feldman }
5970e0af8ecSBrian Feldman 
5980e0af8ecSBrian Feldman void
599780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
6001b40f8c0SMatthew Dillon {
601bc91c510SAlan Cox 
60212c64974SMaxime Henrion 	if (map->system_map) {
603ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
60405a8c414SAlan Cox 	} else
6059fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
60605a8c414SAlan Cox }
60705a8c414SAlan Cox 
60805a8c414SAlan Cox /*
60905a8c414SAlan Cox  *	vm_map_locked:
61005a8c414SAlan Cox  *
61105a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
61205a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
61305a8c414SAlan Cox  */
61405a8c414SAlan Cox int
61505a8c414SAlan Cox vm_map_locked(vm_map_t map)
61605a8c414SAlan Cox {
61705a8c414SAlan Cox 
61805a8c414SAlan Cox 	if (map->system_map)
61905a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
62005a8c414SAlan Cox 	else
62105a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
62225adb370SBrian Feldman }
62325adb370SBrian Feldman 
6243a0916b8SKonstantin Belousov #ifdef INVARIANTS
6253a0916b8SKonstantin Belousov static void
6263a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6273a0916b8SKonstantin Belousov {
6283a0916b8SKonstantin Belousov 
6293a0916b8SKonstantin Belousov 	if (map->system_map)
630ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
6313a0916b8SKonstantin Belousov 	else
6329fde98bbSAttilio Rao 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
6333a0916b8SKonstantin Belousov }
6343a0916b8SKonstantin Belousov 
6353a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6363a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6373a0916b8SKonstantin Belousov #else
6383a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6393a0916b8SKonstantin Belousov #endif
6403a0916b8SKonstantin Belousov 
641acd9a301SAlan Cox /*
6428304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6438304adaaSAlan Cox  *
6448304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6458304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6468304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6478304adaaSAlan Cox  *	exceeded.
6488304adaaSAlan Cox  *
6498304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6508304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6518304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6528304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6538304adaaSAlan Cox  *	operation on the map.
654acd9a301SAlan Cox  */
6559688f931SAlan Cox int
6568304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
657acd9a301SAlan Cox {
658acd9a301SAlan Cox 
6593a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6608304adaaSAlan Cox 	if (map->system_map)
661ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
6628304adaaSAlan Cox 	else
6639fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
6648304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
6658304adaaSAlan Cox 	    timo));
666acd9a301SAlan Cox }
667acd9a301SAlan Cox 
668acd9a301SAlan Cox /*
669acd9a301SAlan Cox  *	vm_map_wakeup:
6708304adaaSAlan Cox  *
6718304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
6728304adaaSAlan Cox  *	vm_map_unlock_and_wait().
673acd9a301SAlan Cox  */
6749688f931SAlan Cox void
675acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
676acd9a301SAlan Cox {
677acd9a301SAlan Cox 
678b49ecb86SAlan Cox 	/*
6793a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6808304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
6818304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
682b49ecb86SAlan Cox 	 */
6833a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6843a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
685acd9a301SAlan Cox 	wakeup(&map->root);
686acd9a301SAlan Cox }
687acd9a301SAlan Cox 
688a5db445dSMax Laier void
689a5db445dSMax Laier vm_map_busy(vm_map_t map)
690a5db445dSMax Laier {
691a5db445dSMax Laier 
692a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
693a5db445dSMax Laier 	map->busy++;
694a5db445dSMax Laier }
695a5db445dSMax Laier 
696a5db445dSMax Laier void
697a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
698a5db445dSMax Laier {
699a5db445dSMax Laier 
700a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
701a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
702a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
703a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
704a5db445dSMax Laier 		wakeup(&map->busy);
705a5db445dSMax Laier 	}
706a5db445dSMax Laier }
707a5db445dSMax Laier 
708a5db445dSMax Laier void
709a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
710a5db445dSMax Laier {
711a5db445dSMax Laier 
712a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
713a5db445dSMax Laier 	while (map->busy) {
714a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
715a5db445dSMax Laier 		if (map->system_map)
716a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
717a5db445dSMax Laier 		else
718a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
719a5db445dSMax Laier 	}
720a5db445dSMax Laier 	map->timestamp++;
721a5db445dSMax Laier }
722a5db445dSMax Laier 
7231b40f8c0SMatthew Dillon long
7241b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
7251b40f8c0SMatthew Dillon {
7261b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
7271b40f8c0SMatthew Dillon }
7281b40f8c0SMatthew Dillon 
729ff2b5645SMatthew Dillon /*
730df8bae1dSRodney W. Grimes  *	vm_map_create:
731df8bae1dSRodney W. Grimes  *
732df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
733df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
734df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
735df8bae1dSRodney W. Grimes  */
7360d94caffSDavid Greenman vm_map_t
7371b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
738df8bae1dSRodney W. Grimes {
739c0877f10SJohn Dyson 	vm_map_t result;
740df8bae1dSRodney W. Grimes 
741a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
74221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
74392351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
744df8bae1dSRodney W. Grimes 	return (result);
745df8bae1dSRodney W. Grimes }
746df8bae1dSRodney W. Grimes 
747df8bae1dSRodney W. Grimes /*
748df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
749df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
750df8bae1dSRodney W. Grimes  */
7518355f576SJeff Roberson static void
75292351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
753df8bae1dSRodney W. Grimes {
75421c641b2SJohn Baldwin 
755df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7569688f931SAlan Cox 	map->needs_wakeup = FALSE;
7573075778bSJohn Dyson 	map->system_map = 0;
75892351f16SAlan Cox 	map->pmap = pmap;
759df8bae1dSRodney W. Grimes 	map->min_offset = min;
760df8bae1dSRodney W. Grimes 	map->max_offset = max;
761af7cd0c5SBrian Feldman 	map->flags = 0;
7624e94f402SAlan Cox 	map->root = NULL;
763df8bae1dSRodney W. Grimes 	map->timestamp = 0;
764a5db445dSMax Laier 	map->busy = 0;
765df8bae1dSRodney W. Grimes }
766df8bae1dSRodney W. Grimes 
767a18b1f1dSJason Evans void
76892351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
769a18b1f1dSJason Evans {
77092351f16SAlan Cox 
77192351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
772d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
77312c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
774a18b1f1dSJason Evans }
775a18b1f1dSJason Evans 
776df8bae1dSRodney W. Grimes /*
777b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
778b18bfc3dSJohn Dyson  *
779b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
780b18bfc3dSJohn Dyson  */
78162487bb4SJohn Dyson static void
7821b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
783b18bfc3dSJohn Dyson {
7842b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
785b18bfc3dSJohn Dyson }
786b18bfc3dSJohn Dyson 
787b18bfc3dSJohn Dyson /*
788df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
789df8bae1dSRodney W. Grimes  *
790df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
791b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
792df8bae1dSRodney W. Grimes  */
793f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7941b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
795df8bae1dSRodney W. Grimes {
7961f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7971f6889a1SMatthew Dillon 
7982b4a2c27SAlan Cox 	if (map->system_map)
7992b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
8002b4a2c27SAlan Cox 	else
801a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
8021f6889a1SMatthew Dillon 	if (new_entry == NULL)
8031f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
8041f6889a1SMatthew Dillon 	return (new_entry);
805df8bae1dSRodney W. Grimes }
806df8bae1dSRodney W. Grimes 
807df8bae1dSRodney W. Grimes /*
808794316a8SAlan Cox  *	vm_map_entry_set_behavior:
809794316a8SAlan Cox  *
810794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
811794316a8SAlan Cox  *	sequential.
812794316a8SAlan Cox  */
81362a59e8fSWarner Losh static inline void
814794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
815794316a8SAlan Cox {
816794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
817794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
818794316a8SAlan Cox }
819794316a8SAlan Cox 
820794316a8SAlan Cox /*
8210164e057SAlan Cox  *	vm_map_entry_set_max_free:
8220164e057SAlan Cox  *
8230164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
8240164e057SAlan Cox  */
82562a59e8fSWarner Losh static inline void
8260164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
8270164e057SAlan Cox {
8280164e057SAlan Cox 
8290164e057SAlan Cox 	entry->max_free = entry->adj_free;
8300164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
8310164e057SAlan Cox 		entry->max_free = entry->left->max_free;
8320164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
8330164e057SAlan Cox 		entry->max_free = entry->right->max_free;
8340164e057SAlan Cox }
8350164e057SAlan Cox 
8360164e057SAlan Cox /*
8374e94f402SAlan Cox  *	vm_map_entry_splay:
8384e94f402SAlan Cox  *
8390164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
8400164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
8410164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8420164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8430164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8440164e057SAlan Cox  *	amortized.
8450164e057SAlan Cox  *
8460164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8470164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8480164e057SAlan Cox  *
8490164e057SAlan Cox  *	The map must be locked, and leaves it so.
8500164e057SAlan Cox  *
8510164e057SAlan Cox  *	Returns: the new root.
8524e94f402SAlan Cox  */
8534e94f402SAlan Cox static vm_map_entry_t
8540164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8554e94f402SAlan Cox {
8560164e057SAlan Cox 	vm_map_entry_t llist, rlist;
8570164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
8580164e057SAlan Cox 	vm_map_entry_t y;
8594e94f402SAlan Cox 
8600164e057SAlan Cox 	/* Special case of empty tree. */
8614e94f402SAlan Cox 	if (root == NULL)
8624e94f402SAlan Cox 		return (root);
8630164e057SAlan Cox 
8640164e057SAlan Cox 	/*
8650164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8660164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8670164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8680164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8690164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8700164e057SAlan Cox 	 * the two spines.
8710164e057SAlan Cox 	 */
8720164e057SAlan Cox 	llist = NULL;
8730164e057SAlan Cox 	rlist = NULL;
8740164e057SAlan Cox 	for (;;) {
8750164e057SAlan Cox 		/* root is never NULL in here. */
8760164e057SAlan Cox 		if (addr < root->start) {
8770164e057SAlan Cox 			y = root->left;
8780164e057SAlan Cox 			if (y == NULL)
8794e94f402SAlan Cox 				break;
8800164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8810164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8824e94f402SAlan Cox 				root->left = y->right;
8834e94f402SAlan Cox 				y->right = root;
8840164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8850164e057SAlan Cox 				root = y->left;
8860164e057SAlan Cox 				y->left = rlist;
8870164e057SAlan Cox 				rlist = y;
8880164e057SAlan Cox 			} else {
8890164e057SAlan Cox 				/* Put root on rlist. */
8900164e057SAlan Cox 				root->left = rlist;
8910164e057SAlan Cox 				rlist = root;
8924e94f402SAlan Cox 				root = y;
8934e94f402SAlan Cox 			}
8947438d60bSAlan Cox 		} else if (addr >= root->end) {
8950164e057SAlan Cox 			y = root->right;
8967438d60bSAlan Cox 			if (y == NULL)
8974e94f402SAlan Cox 				break;
8980164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8990164e057SAlan Cox 				/* Rotate left and put y on llist. */
9004e94f402SAlan Cox 				root->right = y->left;
9014e94f402SAlan Cox 				y->left = root;
9020164e057SAlan Cox 				vm_map_entry_set_max_free(root);
9030164e057SAlan Cox 				root = y->right;
9040164e057SAlan Cox 				y->right = llist;
9050164e057SAlan Cox 				llist = y;
9060164e057SAlan Cox 			} else {
9070164e057SAlan Cox 				/* Put root on llist. */
9080164e057SAlan Cox 				root->right = llist;
9090164e057SAlan Cox 				llist = root;
9104e94f402SAlan Cox 				root = y;
9114e94f402SAlan Cox 			}
9127438d60bSAlan Cox 		} else
9137438d60bSAlan Cox 			break;
9140164e057SAlan Cox 	}
9150164e057SAlan Cox 
9160164e057SAlan Cox 	/*
9170164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
9180164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
9190164e057SAlan Cox 	 * bottom of llist and rlist.
9200164e057SAlan Cox 	 */
9210164e057SAlan Cox 	ltree = root->left;
9220164e057SAlan Cox 	while (llist != NULL) {
9230164e057SAlan Cox 		y = llist->right;
9240164e057SAlan Cox 		llist->right = ltree;
9250164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
9260164e057SAlan Cox 		ltree = llist;
9270164e057SAlan Cox 		llist = y;
9280164e057SAlan Cox 	}
9290164e057SAlan Cox 	rtree = root->right;
9300164e057SAlan Cox 	while (rlist != NULL) {
9310164e057SAlan Cox 		y = rlist->left;
9320164e057SAlan Cox 		rlist->left = rtree;
9330164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
9340164e057SAlan Cox 		rtree = rlist;
9350164e057SAlan Cox 		rlist = y;
9360164e057SAlan Cox 	}
9370164e057SAlan Cox 
9380164e057SAlan Cox 	/*
9390164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
9400164e057SAlan Cox 	 */
9410164e057SAlan Cox 	root->left = ltree;
9420164e057SAlan Cox 	root->right = rtree;
9430164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9440164e057SAlan Cox 
9454e94f402SAlan Cox 	return (root);
9464e94f402SAlan Cox }
9474e94f402SAlan Cox 
9484e94f402SAlan Cox /*
949df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
950df8bae1dSRodney W. Grimes  *
951df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
952df8bae1dSRodney W. Grimes  */
9534e94f402SAlan Cox static void
95499c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
95599c81ca9SAlan Cox 		  vm_map_entry_t after_where,
95699c81ca9SAlan Cox 		  vm_map_entry_t entry)
95799c81ca9SAlan Cox {
95821c641b2SJohn Baldwin 
95921c641b2SJohn Baldwin 	CTR4(KTR_VM,
96021c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
96121c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9623a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
96399c81ca9SAlan Cox 	map->nentries++;
96499c81ca9SAlan Cox 	entry->prev = after_where;
96599c81ca9SAlan Cox 	entry->next = after_where->next;
96699c81ca9SAlan Cox 	entry->next->prev = entry;
96799c81ca9SAlan Cox 	after_where->next = entry;
9684e94f402SAlan Cox 
9694e94f402SAlan Cox 	if (after_where != &map->header) {
9704e94f402SAlan Cox 		if (after_where != map->root)
9714e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
9724e94f402SAlan Cox 		entry->right = after_where->right;
9734e94f402SAlan Cox 		entry->left = after_where;
9744e94f402SAlan Cox 		after_where->right = NULL;
9750164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
9760164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
9774e94f402SAlan Cox 	} else {
9784e94f402SAlan Cox 		entry->right = map->root;
9794e94f402SAlan Cox 		entry->left = NULL;
9804e94f402SAlan Cox 	}
9810164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
9820164e057SAlan Cox 	    entry->next->start) - entry->end;
9830164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
9844e94f402SAlan Cox 	map->root = entry;
985df8bae1dSRodney W. Grimes }
98699c81ca9SAlan Cox 
9874e94f402SAlan Cox static void
98899c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
98999c81ca9SAlan Cox 		    vm_map_entry_t entry)
99099c81ca9SAlan Cox {
9914e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
99299c81ca9SAlan Cox 
9933a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9944e94f402SAlan Cox 	if (entry != map->root)
9954e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
9964e94f402SAlan Cox 	if (entry->left == NULL)
9974e94f402SAlan Cox 		root = entry->right;
9984e94f402SAlan Cox 	else {
9994e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
10004e94f402SAlan Cox 		root->right = entry->right;
10010164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
10020164e057SAlan Cox 		    entry->next->start) - root->end;
10030164e057SAlan Cox 		vm_map_entry_set_max_free(root);
10044e94f402SAlan Cox 	}
10054e94f402SAlan Cox 	map->root = root;
10064e94f402SAlan Cox 
10074e94f402SAlan Cox 	prev = entry->prev;
10084e94f402SAlan Cox 	next = entry->next;
100999c81ca9SAlan Cox 	next->prev = prev;
101099c81ca9SAlan Cox 	prev->next = next;
101199c81ca9SAlan Cox 	map->nentries--;
101221c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
101321c641b2SJohn Baldwin 	    map->nentries, entry);
1014df8bae1dSRodney W. Grimes }
1015df8bae1dSRodney W. Grimes 
1016df8bae1dSRodney W. Grimes /*
10170164e057SAlan Cox  *	vm_map_entry_resize_free:
10180164e057SAlan Cox  *
10190164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
10200164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
10210164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
10220164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
10230164e057SAlan Cox  *
10240164e057SAlan Cox  *	The map must be locked, and leaves it so.
10250164e057SAlan Cox  */
10260164e057SAlan Cox static void
10270164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
10280164e057SAlan Cox {
10290164e057SAlan Cox 
10300164e057SAlan Cox 	/*
10310164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
10320164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
10330164e057SAlan Cox 	 * root and making the change there.
10340164e057SAlan Cox 	 */
10350164e057SAlan Cox 	if (entry != map->root)
10360164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
10370164e057SAlan Cox 
10380164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
10390164e057SAlan Cox 	    entry->next->start) - entry->end;
10400164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
10410164e057SAlan Cox }
10420164e057SAlan Cox 
10430164e057SAlan Cox /*
1044df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
1045df8bae1dSRodney W. Grimes  *
1046df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
1047df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
1048df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
1049df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
1050df8bae1dSRodney W. Grimes  *	result indicates whether the address is
1051df8bae1dSRodney W. Grimes  *	actually contained in the map.
1052df8bae1dSRodney W. Grimes  */
10530d94caffSDavid Greenman boolean_t
10541b40f8c0SMatthew Dillon vm_map_lookup_entry(
10551b40f8c0SMatthew Dillon 	vm_map_t map,
10561b40f8c0SMatthew Dillon 	vm_offset_t address,
10571b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
1058df8bae1dSRodney W. Grimes {
1059c0877f10SJohn Dyson 	vm_map_entry_t cur;
106005a8c414SAlan Cox 	boolean_t locked;
1061df8bae1dSRodney W. Grimes 
10624c3ef59eSAlan Cox 	/*
10634c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
10644c3ef59eSAlan Cox 	 * "address" is the map's header.
10654c3ef59eSAlan Cox 	 */
10664c3ef59eSAlan Cox 	cur = map->root;
10674e94f402SAlan Cox 	if (cur == NULL)
10684e94f402SAlan Cox 		*entry = &map->header;
10694c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
10704c3ef59eSAlan Cox 		*entry = cur;
10714c3ef59eSAlan Cox 		return (TRUE);
107205a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
107305a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
107405a8c414SAlan Cox 		/*
107505a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
107605a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
107705a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
107805a8c414SAlan Cox 		 * on a temporary upgrade.
107905a8c414SAlan Cox 		 */
10804c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
108105a8c414SAlan Cox 		if (!locked)
108205a8c414SAlan Cox 			sx_downgrade(&map->lock);
1083df8bae1dSRodney W. Grimes 
10844c3ef59eSAlan Cox 		/*
10854c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
10864c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
10874c3ef59eSAlan Cox 		 * immediately before or after "address".
10884c3ef59eSAlan Cox 		 */
1089df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1090df8bae1dSRodney W. Grimes 			*entry = cur;
10914e94f402SAlan Cox 			if (cur->end > address)
1092df8bae1dSRodney W. Grimes 				return (TRUE);
10934e94f402SAlan Cox 		} else
1094df8bae1dSRodney W. Grimes 			*entry = cur->prev;
109505a8c414SAlan Cox 	} else
109605a8c414SAlan Cox 		/*
109705a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
109805a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
109905a8c414SAlan Cox 		 */
110005a8c414SAlan Cox 		for (;;) {
110105a8c414SAlan Cox 			if (address < cur->start) {
110205a8c414SAlan Cox 				if (cur->left == NULL) {
110305a8c414SAlan Cox 					*entry = cur->prev;
110405a8c414SAlan Cox 					break;
110505a8c414SAlan Cox 				}
110605a8c414SAlan Cox 				cur = cur->left;
110705a8c414SAlan Cox 			} else if (cur->end > address) {
110805a8c414SAlan Cox 				*entry = cur;
110905a8c414SAlan Cox 				return (TRUE);
111005a8c414SAlan Cox 			} else {
111105a8c414SAlan Cox 				if (cur->right == NULL) {
111205a8c414SAlan Cox 					*entry = cur;
111305a8c414SAlan Cox 					break;
111405a8c414SAlan Cox 				}
111505a8c414SAlan Cox 				cur = cur->right;
111605a8c414SAlan Cox 			}
11174e94f402SAlan Cox 		}
1118df8bae1dSRodney W. Grimes 	return (FALSE);
1119df8bae1dSRodney W. Grimes }
1120df8bae1dSRodney W. Grimes 
1121df8bae1dSRodney W. Grimes /*
112230dcfc09SJohn Dyson  *	vm_map_insert:
112330dcfc09SJohn Dyson  *
112430dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
112530dcfc09SJohn Dyson  *	map at the specified address range.  The object's
112630dcfc09SJohn Dyson  *	size should match that of the address range.
112730dcfc09SJohn Dyson  *
112830dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
11292aaeadf8SMatthew Dillon  *
11302aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11312aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
113230dcfc09SJohn Dyson  */
113330dcfc09SJohn Dyson int
1134b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1135b9dcd593SBruce Evans 	      vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max,
1136b9dcd593SBruce Evans 	      int cow)
113730dcfc09SJohn Dyson {
1138c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1139c0877f10SJohn Dyson 	vm_map_entry_t prev_entry;
114030dcfc09SJohn Dyson 	vm_map_entry_t temp_entry;
11419730a5daSPaul Saab 	vm_eflags_t protoeflags;
1142ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
11438211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
11443364c323SKonstantin Belousov 	boolean_t charge_prev_obj;
114530dcfc09SJohn Dyson 
11463a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
11473a0916b8SKonstantin Belousov 
114830dcfc09SJohn Dyson 	/*
114930dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
115030dcfc09SJohn Dyson 	 */
115130dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
115230dcfc09SJohn Dyson 	    (start >= end))
115330dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
115430dcfc09SJohn Dyson 
115530dcfc09SJohn Dyson 	/*
115630dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
115730dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
115830dcfc09SJohn Dyson 	 */
115930dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
116030dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
116130dcfc09SJohn Dyson 
116230dcfc09SJohn Dyson 	prev_entry = temp_entry;
116330dcfc09SJohn Dyson 
116430dcfc09SJohn Dyson 	/*
116530dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
116630dcfc09SJohn Dyson 	 */
116730dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
116830dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
116930dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
117030dcfc09SJohn Dyson 
1171afa07f7eSJohn Dyson 	protoeflags = 0;
11723364c323SKonstantin Belousov 	charge_prev_obj = FALSE;
1173afa07f7eSJohn Dyson 
1174afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1175e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY;
1176afa07f7eSJohn Dyson 
11774e045f93SAlan Cox 	if (cow & MAP_NOFAULT) {
1178afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
1179afa07f7eSJohn Dyson 
11804e045f93SAlan Cox 		KASSERT(object == NULL,
11814e045f93SAlan Cox 			("vm_map_insert: paradoxical MAP_NOFAULT request"));
11824e045f93SAlan Cox 	}
11834f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11844f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11859730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11869730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
118784110e7eSKonstantin Belousov 	if (cow & MAP_VN_WRITECOUNT)
118884110e7eSKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
11898211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
11908211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
11918211bd45SKonstantin Belousov 	else
11928211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
11934f79d873SMatthew Dillon 
1194ef694c1aSEdward Tomasz Napierala 	cred = NULL;
11953364c323SKonstantin Belousov 	KASSERT((object != kmem_object && object != kernel_object) ||
11963364c323SKonstantin Belousov 	    ((object == kmem_object || object == kernel_object) &&
11973364c323SKonstantin Belousov 		!(protoeflags & MAP_ENTRY_NEEDS_COPY)),
11983364c323SKonstantin Belousov 	    ("kmem or kernel object and cow"));
11993364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
12003364c323SKonstantin Belousov 		goto charged;
12013364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
12023364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
12033364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
12043364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
120541c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1206ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
12073364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
1208ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
1209ef694c1aSEdward Tomasz Napierala 		crhold(cred);
12103364c323SKonstantin Belousov 		if (object == NULL && !(protoeflags & MAP_ENTRY_NEEDS_COPY))
12113364c323SKonstantin Belousov 			charge_prev_obj = TRUE;
12123364c323SKonstantin Belousov 	}
12133364c323SKonstantin Belousov 
12143364c323SKonstantin Belousov charged:
1215f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1216f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1217f8616ebfSAlan Cox 		pmap_growkernel(end);
12181d284e00SAlan Cox 	if (object != NULL) {
121930dcfc09SJohn Dyson 		/*
12201d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
12211d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
12221d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
12231d284e00SAlan Cox 		 * reference counting is insufficient to recognize
12241d284e00SAlan Cox 		 * aliases with precision.)
122530dcfc09SJohn Dyson 		 */
122689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
12271d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
12282aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
122989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
12304e045f93SAlan Cox 	}
12314e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
12324e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
12338cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
12344e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
1235ef694c1aSEdward Tomasz Napierala 		 (prev_entry->cred == cred ||
12363364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
1237ef694c1aSEdward Tomasz Napierala 		   (prev_entry->object.vm_object->cred == cred))) &&
12388cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
123957a21abaSAlan Cox 		       prev_entry->offset,
12408cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
12413364c323SKonstantin Belousov 		       (vm_size_t)(end - prev_entry->end), charge_prev_obj)) {
124230dcfc09SJohn Dyson 		/*
12432aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
12442aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
12452aaeadf8SMatthew Dillon 		 * new range as well.
124630dcfc09SJohn Dyson 		 */
12478211bd45SKonstantin Belousov 		if ((prev_entry->inheritance == inheritance) &&
12488cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
12498cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
125030dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
125130dcfc09SJohn Dyson 			prev_entry->end = end;
12520164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
12534e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
1254ef694c1aSEdward Tomasz Napierala 			if (cred != NULL)
1255ef694c1aSEdward Tomasz Napierala 				crfree(cred);
125630dcfc09SJohn Dyson 			return (KERN_SUCCESS);
125730dcfc09SJohn Dyson 		}
12588cc7e047SJohn Dyson 
12592aaeadf8SMatthew Dillon 		/*
12602aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
12612aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
12622aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
12634e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
12642aaeadf8SMatthew Dillon 		 */
12652aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
12662aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
12672aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
12688cc7e047SJohn Dyson 		vm_object_reference(object);
1269ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
12703364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
12713364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1272ef694c1aSEdward Tomasz Napierala 			crfree(cred);
1273ef694c1aSEdward Tomasz Napierala 			cred = NULL;
12743364c323SKonstantin Belousov 		}
1275b18bfc3dSJohn Dyson 	}
12762aaeadf8SMatthew Dillon 
12772aaeadf8SMatthew Dillon 	/*
12782aaeadf8SMatthew Dillon 	 * NOTE: if conditionals fail, object can be NULL here.  This occurs
12792aaeadf8SMatthew Dillon 	 * in things like the buffer map where we manage kva but do not manage
12802aaeadf8SMatthew Dillon 	 * backing objects.
12812aaeadf8SMatthew Dillon 	 */
12828cc7e047SJohn Dyson 
128330dcfc09SJohn Dyson 	/*
128430dcfc09SJohn Dyson 	 * Create a new entry
128530dcfc09SJohn Dyson 	 */
128630dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
128730dcfc09SJohn Dyson 	new_entry->start = start;
128830dcfc09SJohn Dyson 	new_entry->end = end;
1289ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
129030dcfc09SJohn Dyson 
1291afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
129230dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
129330dcfc09SJohn Dyson 	new_entry->offset = offset;
12942267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12952267af78SJulian Elischer 
12968211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
129730dcfc09SJohn Dyson 	new_entry->protection = prot;
129830dcfc09SJohn Dyson 	new_entry->max_protection = max;
129930dcfc09SJohn Dyson 	new_entry->wired_count = 0;
130013458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
130113458803SAlan Cox 	new_entry->next_read = OFF_TO_IDX(offset);
1302e5f251d2SAlan Cox 
1303ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
13043364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1305ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
13063364c323SKonstantin Belousov 
130730dcfc09SJohn Dyson 	/*
130830dcfc09SJohn Dyson 	 * Insert the new entry into the list
130930dcfc09SJohn Dyson 	 */
131030dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
131130dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
131230dcfc09SJohn Dyson 
13131a484d28SMatthew Dillon 	/*
1314d2a444c0SAlan Cox 	 * It may be possible to merge the new entry with the next and/or
1315d2a444c0SAlan Cox 	 * previous entries.  However, due to MAP_STACK_* being a hack, a
1316d2a444c0SAlan Cox 	 * panic can result from merging such entries.
13171a484d28SMatthew Dillon 	 */
1318d2a444c0SAlan Cox 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0)
13194e71e795SMatthew Dillon 		vm_map_simplify_entry(map, new_entry);
13204e71e795SMatthew Dillon 
13214f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
13224da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1323e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1324e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
13254f79d873SMatthew Dillon 	}
1326e972780aSAlan Cox 
132730dcfc09SJohn Dyson 	return (KERN_SUCCESS);
132830dcfc09SJohn Dyson }
132930dcfc09SJohn Dyson 
133030dcfc09SJohn Dyson /*
13310164e057SAlan Cox  *	vm_map_findspace:
13320164e057SAlan Cox  *
13330164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
13340164e057SAlan Cox  *	beginning at address >= start in the given map.
13350164e057SAlan Cox  *
13360164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
13370164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
13380164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
13390164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
13400164e057SAlan Cox  *	O(log n) amortized with splay trees.
13410164e057SAlan Cox  *
13420164e057SAlan Cox  *	The map must be locked, and leaves it so.
13430164e057SAlan Cox  *
13440164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
13450164e057SAlan Cox  *		 1 if insufficient space.
1346df8bae1dSRodney W. Grimes  */
1347df8bae1dSRodney W. Grimes int
13480164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13490164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1350df8bae1dSRodney W. Grimes {
13510164e057SAlan Cox 	vm_map_entry_t entry;
1352f8616ebfSAlan Cox 	vm_offset_t st;
1353df8bae1dSRodney W. Grimes 
1354986b43f8SAlan Cox 	/*
1355986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1356986b43f8SAlan Cox 	 * address wrap.
1357986b43f8SAlan Cox 	 */
1358df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1359df8bae1dSRodney W. Grimes 		start = map->min_offset;
1360986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1361df8bae1dSRodney W. Grimes 		return (1);
1362df8bae1dSRodney W. Grimes 
13630164e057SAlan Cox 	/* Empty tree means wide open address space. */
13640164e057SAlan Cox 	if (map->root == NULL) {
1365df8bae1dSRodney W. Grimes 		*addr = start;
1366f8616ebfSAlan Cox 		return (0);
136799448ed1SJohn Dyson 	}
13680164e057SAlan Cox 
13690164e057SAlan Cox 	/*
13700164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13710164e057SAlan Cox 	 * must be a gap from start to the root.
13720164e057SAlan Cox 	 */
13730164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13740164e057SAlan Cox 	if (start + length <= map->root->start) {
13750164e057SAlan Cox 		*addr = start;
1376f8616ebfSAlan Cox 		return (0);
13770164e057SAlan Cox 	}
13780164e057SAlan Cox 
13790164e057SAlan Cox 	/*
13800164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1381986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1382986b43f8SAlan Cox 	 * wrap might be a problem.
13830164e057SAlan Cox 	 */
13840164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1385986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13860164e057SAlan Cox 		*addr = st;
1387f8616ebfSAlan Cox 		return (0);
13880164e057SAlan Cox 	}
13890164e057SAlan Cox 
13900164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13910164e057SAlan Cox 	entry = map->root->right;
13920164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13930164e057SAlan Cox 		return (1);
13940164e057SAlan Cox 
13950164e057SAlan Cox 	/*
13960164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13970164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13980164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13990164e057SAlan Cox 	 */
14000164e057SAlan Cox 	while (entry != NULL) {
14010164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
14020164e057SAlan Cox 			entry = entry->left;
14030164e057SAlan Cox 		else if (entry->adj_free >= length) {
14040164e057SAlan Cox 			*addr = entry->end;
1405f8616ebfSAlan Cox 			return (0);
14060164e057SAlan Cox 		} else
14070164e057SAlan Cox 			entry = entry->right;
14080164e057SAlan Cox 	}
14090164e057SAlan Cox 
14100164e057SAlan Cox 	/* Can't get here, so panic if we do. */
14110164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1412df8bae1dSRodney W. Grimes }
1413df8bae1dSRodney W. Grimes 
1414d239bd3cSKonstantin Belousov int
1415d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1416b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1417d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1418d239bd3cSKonstantin Belousov {
1419b8ca4ef2SAlan Cox 	vm_offset_t end;
1420d239bd3cSKonstantin Belousov 	int result;
1421d239bd3cSKonstantin Belousov 
1422d239bd3cSKonstantin Belousov 	end = start + length;
1423897d81a0SKonstantin Belousov 	vm_map_lock(map);
1424d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
1425655c3490SKonstantin Belousov 	(void) vm_map_delete(map, start, end);
1426d239bd3cSKonstantin Belousov 	result = vm_map_insert(map, object, offset, start, end, prot,
1427d239bd3cSKonstantin Belousov 	    max, cow);
1428d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1429d239bd3cSKonstantin Belousov 	return (result);
1430d239bd3cSKonstantin Belousov }
1431d239bd3cSKonstantin Belousov 
1432df8bae1dSRodney W. Grimes /*
1433df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1434df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1435df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1436df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1437df8bae1dSRodney W. Grimes  *
14382aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
14392aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1440df8bae1dSRodney W. Grimes  */
1441df8bae1dSRodney W. Grimes int
1442b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1443b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
144426c538ffSAlan Cox 	    vm_size_t length, int find_space, vm_prot_t prot,
1445b9dcd593SBruce Evans 	    vm_prot_t max, int cow)
1446df8bae1dSRodney W. Grimes {
1447c0877f10SJohn Dyson 	vm_offset_t start;
14486eaee3feSAlan Cox 	int result;
1449df8bae1dSRodney W. Grimes 
1450df8bae1dSRodney W. Grimes 	start = *addr;
1451bea41bcfSDavid Greenman 	vm_map_lock(map);
145226c538ffSAlan Cox 	do {
145326c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1454df8bae1dSRodney W. Grimes 			if (vm_map_findspace(map, start, length, addr)) {
1455df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1456df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1457df8bae1dSRodney W. Grimes 			}
1458ca596a25SJuli Mallett 			switch (find_space) {
1459ca596a25SJuli Mallett 			case VMFS_ALIGNED_SPACE:
146026c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
146126c538ffSAlan Cox 				    length);
1462ca596a25SJuli Mallett 				break;
1463ca596a25SJuli Mallett #ifdef VMFS_TLB_ALIGNED_SPACE
1464ca596a25SJuli Mallett 			case VMFS_TLB_ALIGNED_SPACE:
1465ca596a25SJuli Mallett 				pmap_align_tlb(addr);
1466ca596a25SJuli Mallett 				break;
1467ca596a25SJuli Mallett #endif
1468ca596a25SJuli Mallett 			default:
1469ca596a25SJuli Mallett 				break;
1470ca596a25SJuli Mallett 			}
1471ca596a25SJuli Mallett 
1472df8bae1dSRodney W. Grimes 			start = *addr;
1473df8bae1dSRodney W. Grimes 		}
147426c538ffSAlan Cox 		result = vm_map_insert(map, object, offset, start, start +
147526c538ffSAlan Cox 		    length, prot, max, cow);
14769b55fc04SAlan Cox 	} while (result == KERN_NO_SPACE && (find_space == VMFS_ALIGNED_SPACE
14779b55fc04SAlan Cox #ifdef VMFS_TLB_ALIGNED_SPACE
14789b55fc04SAlan Cox 	    || find_space == VMFS_TLB_ALIGNED_SPACE
14799b55fc04SAlan Cox #endif
14809b55fc04SAlan Cox 	    ));
1481df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1482df8bae1dSRodney W. Grimes 	return (result);
1483df8bae1dSRodney W. Grimes }
1484df8bae1dSRodney W. Grimes 
1485df8bae1dSRodney W. Grimes /*
1486b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
148767bf6868SJohn Dyson  *
14884e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
14894e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
14904e71e795SMatthew Dillon  *
14914e71e795SMatthew Dillon  *	The map must be locked.
14924e71e795SMatthew Dillon  *
14934e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
14944e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
14954e71e795SMatthew Dillon  *	both neighbors.
1496df8bae1dSRodney W. Grimes  */
1497b7b2aac2SJohn Dyson void
14981b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1499df8bae1dSRodney W. Grimes {
1500308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1501b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1502df8bae1dSRodney W. Grimes 
1503acd9a301SAlan Cox 	if (entry->eflags & (MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP))
1504df8bae1dSRodney W. Grimes 		return;
1505308c24baSJohn Dyson 
1506308c24baSJohn Dyson 	prev = entry->prev;
1507308c24baSJohn Dyson 	if (prev != &map->header) {
150867bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
150967bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
151067bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
151195e5e988SJohn Dyson 		     (!prev->object.vm_object ||
151267bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1513afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
151467bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
151567bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
151667bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
15173364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
1518ef694c1aSEdward Tomasz Napierala 		     (prev->cred == entry->cred)) {
1519308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1520308c24baSJohn Dyson 			entry->start = prev->start;
1521308c24baSJohn Dyson 			entry->offset = prev->offset;
15220164e057SAlan Cox 			if (entry->prev != &map->header)
15230164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
15247fd10fb3SKonstantin Belousov 
15257fd10fb3SKonstantin Belousov 			/*
1526b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1527b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1528b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1529b0994946SKonstantin Belousov 			 * because the vnode has additional
1530b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1531b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1532b0994946SKonstantin Belousov 			 * the vnode lock.
153384110e7eSKonstantin Belousov 			 *
153484110e7eSKonstantin Belousov 			 * Since we count the number of virtual page
153584110e7eSKonstantin Belousov 			 * mappings in object->un_pager.vnp.writemappings,
153684110e7eSKonstantin Belousov 			 * the writemappings value should not be adjusted
153784110e7eSKonstantin Belousov 			 * when the entry is disposed of.
15387fd10fb3SKonstantin Belousov 			 */
1539b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1540308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1541ef694c1aSEdward Tomasz Napierala 			if (prev->cred != NULL)
1542ef694c1aSEdward Tomasz Napierala 				crfree(prev->cred);
1543308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1544308c24baSJohn Dyson 		}
1545308c24baSJohn Dyson 	}
1546de5f6a77SJohn Dyson 
1547de5f6a77SJohn Dyson 	next = entry->next;
1548308c24baSJohn Dyson 	if (next != &map->header) {
154967bf6868SJohn Dyson 		esize = entry->end - entry->start;
155067bf6868SJohn Dyson 		if ((entry->end == next->start) &&
155167bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
155267bf6868SJohn Dyson 		     (!entry->object.vm_object ||
155367bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1554afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
155567bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
155667bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
155767bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
15583364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
1559ef694c1aSEdward Tomasz Napierala 		    (next->cred == entry->cred)) {
1560de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1561de5f6a77SJohn Dyson 			entry->end = next->end;
15620164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
15637fd10fb3SKonstantin Belousov 
15647fd10fb3SKonstantin Belousov 			/*
15657fd10fb3SKonstantin Belousov 			 * See comment above.
15667fd10fb3SKonstantin Belousov 			 */
1567b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1568de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1569ef694c1aSEdward Tomasz Napierala 			if (next->cred != NULL)
1570ef694c1aSEdward Tomasz Napierala 				crfree(next->cred);
1571de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1572df8bae1dSRodney W. Grimes 		}
1573df8bae1dSRodney W. Grimes 	}
1574de5f6a77SJohn Dyson }
1575df8bae1dSRodney W. Grimes /*
1576df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1577df8bae1dSRodney W. Grimes  *
1578df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1579df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1580df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1581df8bae1dSRodney W. Grimes  */
1582df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1583df8bae1dSRodney W. Grimes { \
1584df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1585df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1586df8bae1dSRodney W. Grimes }
1587df8bae1dSRodney W. Grimes 
1588df8bae1dSRodney W. Grimes /*
1589df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1590df8bae1dSRodney W. Grimes  *	the entry must be split.
1591df8bae1dSRodney W. Grimes  */
15920d94caffSDavid Greenman static void
15931b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1594df8bae1dSRodney W. Grimes {
1595c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1596df8bae1dSRodney W. Grimes 
15973a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
15983a0916b8SKonstantin Belousov 
1599df8bae1dSRodney W. Grimes 	/*
16000d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
16010d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
16020d94caffSDavid Greenman 	 * starting address.
1603df8bae1dSRodney W. Grimes 	 */
1604f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1605f32dbbeeSJohn Dyson 
160611cccda1SJohn Dyson 	/*
160711cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
160811cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
160911cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
161011cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
161111cccda1SJohn Dyson 	 * put this improvement.
161211cccda1SJohn Dyson 	 */
16134e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
161411cccda1SJohn Dyson 		vm_object_t object;
161511cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1616c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
161711cccda1SJohn Dyson 		entry->object.vm_object = object;
161811cccda1SJohn Dyson 		entry->offset = 0;
1619ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1620ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
16213364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1622ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
16233364c323SKonstantin Belousov 		}
16243364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16253364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1626ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
162789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1628ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1629ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1630ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
16313364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
163289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1633ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
163411cccda1SJohn Dyson 	}
163511cccda1SJohn Dyson 
1636df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1637df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1638df8bae1dSRodney W. Grimes 
1639df8bae1dSRodney W. Grimes 	new_entry->end = start;
1640df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1641df8bae1dSRodney W. Grimes 	entry->start = start;
1642ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1643ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1644df8bae1dSRodney W. Grimes 
1645df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1646df8bae1dSRodney W. Grimes 
16479fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1648df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
164984110e7eSKonstantin Belousov 		/*
165084110e7eSKonstantin Belousov 		 * The object->un_pager.vnp.writemappings for the
165184110e7eSKonstantin Belousov 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
165284110e7eSKonstantin Belousov 		 * kept as is here.  The virtual pages are
165384110e7eSKonstantin Belousov 		 * re-distributed among the clipped entries, so the sum is
165484110e7eSKonstantin Belousov 		 * left the same.
165584110e7eSKonstantin Belousov 		 */
1656df8bae1dSRodney W. Grimes 	}
1657c0877f10SJohn Dyson }
1658df8bae1dSRodney W. Grimes 
1659df8bae1dSRodney W. Grimes /*
1660df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1661df8bae1dSRodney W. Grimes  *
1662df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1663df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1664df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1665df8bae1dSRodney W. Grimes  */
1666df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1667df8bae1dSRodney W. Grimes { \
1668af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1669af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1670df8bae1dSRodney W. Grimes }
1671df8bae1dSRodney W. Grimes 
1672df8bae1dSRodney W. Grimes /*
1673df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1674df8bae1dSRodney W. Grimes  *	the entry must be split.
1675df8bae1dSRodney W. Grimes  */
16760d94caffSDavid Greenman static void
16771b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1678df8bae1dSRodney W. Grimes {
1679c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1680df8bae1dSRodney W. Grimes 
16813a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16823a0916b8SKonstantin Belousov 
1683df8bae1dSRodney W. Grimes 	/*
168411cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
168511cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
168611cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
168711cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
168811cccda1SJohn Dyson 	 * put this improvement.
168911cccda1SJohn Dyson 	 */
16904e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
169111cccda1SJohn Dyson 		vm_object_t object;
169211cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1693c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
169411cccda1SJohn Dyson 		entry->object.vm_object = object;
169511cccda1SJohn Dyson 		entry->offset = 0;
1696ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1697ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
16983364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1699ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
17003364c323SKonstantin Belousov 		}
17013364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
17023364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1703ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
170489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1705ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1706ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1707ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
17083364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
170989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1710ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
171111cccda1SJohn Dyson 	}
171211cccda1SJohn Dyson 
171311cccda1SJohn Dyson 	/*
17140d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1715df8bae1dSRodney W. Grimes 	 */
1716df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1717df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1718df8bae1dSRodney W. Grimes 
1719df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1720df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1721ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1722ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1723df8bae1dSRodney W. Grimes 
1724df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1725df8bae1dSRodney W. Grimes 
17269fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1727df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1728df8bae1dSRodney W. Grimes 	}
1729c0877f10SJohn Dyson }
1730df8bae1dSRodney W. Grimes 
1731df8bae1dSRodney W. Grimes /*
1732df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1733df8bae1dSRodney W. Grimes  *
1734df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1735df8bae1dSRodney W. Grimes  *
1736df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1737df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1738df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1739df8bae1dSRodney W. Grimes  *
1740df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1741df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1742df8bae1dSRodney W. Grimes  *		vm_fault
1743df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1744df8bae1dSRodney W. Grimes  *
1745df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1746df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1747df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1748df8bae1dSRodney W. Grimes  */
1749df8bae1dSRodney W. Grimes int
17501b40f8c0SMatthew Dillon vm_map_submap(
17511b40f8c0SMatthew Dillon 	vm_map_t map,
17521b40f8c0SMatthew Dillon 	vm_offset_t start,
17531b40f8c0SMatthew Dillon 	vm_offset_t end,
17541b40f8c0SMatthew Dillon 	vm_map_t submap)
1755df8bae1dSRodney W. Grimes {
1756df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1757c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1758df8bae1dSRodney W. Grimes 
1759df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1760df8bae1dSRodney W. Grimes 
1761df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1762df8bae1dSRodney W. Grimes 
1763df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1764df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17650d94caffSDavid Greenman 	} else
1766df8bae1dSRodney W. Grimes 		entry = entry->next;
1767df8bae1dSRodney W. Grimes 
1768df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1769df8bae1dSRodney W. Grimes 
1770df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
17719fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1772afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17732d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1774afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1775df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1776df8bae1dSRodney W. Grimes 	}
1777df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1778df8bae1dSRodney W. Grimes 
1779df8bae1dSRodney W. Grimes 	return (result);
1780df8bae1dSRodney W. Grimes }
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes /*
17831f78f902SAlan Cox  * The maximum number of pages to map
17841f78f902SAlan Cox  */
17851f78f902SAlan Cox #define	MAX_INIT_PT	96
17861f78f902SAlan Cox 
17871f78f902SAlan Cox /*
17880551c08dSAlan Cox  *	vm_map_pmap_enter:
17890551c08dSAlan Cox  *
1790a922d312SAlan Cox  *	Preload read-only mappings for the specified object's resident pages
1791a922d312SAlan Cox  *	into the target map.  If "flags" is MAP_PREFAULT_PARTIAL, then only
1792a922d312SAlan Cox  *	the resident pages within the address range [addr, addr + ulmin(size,
1793a922d312SAlan Cox  *	ptoa(MAX_INIT_PT))) are mapped.  Otherwise, all resident pages within
1794a922d312SAlan Cox  *	the specified address range are mapped.  This eliminates many soft
1795a922d312SAlan Cox  *	faults on process startup and immediately after an mmap(2).  Because
1796a922d312SAlan Cox  *	these are speculative mappings, cached pages are not reactivated and
1797a922d312SAlan Cox  *	mapped.
17980551c08dSAlan Cox  */
17990551c08dSAlan Cox void
18004da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
18010551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
18020551c08dSAlan Cox {
18038fece8c3SAlan Cox 	vm_offset_t start;
1804ce142d9eSAlan Cox 	vm_page_t p, p_start;
18058fece8c3SAlan Cox 	vm_pindex_t psize, tmpidx;
18060551c08dSAlan Cox 
1807ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
18081f78f902SAlan Cox 		return;
18099af6d512SAttilio Rao 	VM_OBJECT_RLOCK(object);
18109af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18119af6d512SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
181289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
181301381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18149af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
18159af6d512SAttilio Rao 			    size);
18169af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18179af6d512SAttilio Rao 			return;
18189af6d512SAttilio Rao 		}
18199af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
18201f78f902SAlan Cox 	}
18211f78f902SAlan Cox 
18221f78f902SAlan Cox 	psize = atop(size);
1823a922d312SAlan Cox 	if (psize > MAX_INIT_PT && (flags & MAP_PREFAULT_PARTIAL) != 0)
1824a922d312SAlan Cox 		psize = MAX_INIT_PT;
18251f78f902SAlan Cox 	if (psize + pindex > object->size) {
18269af6d512SAttilio Rao 		if (object->size < pindex) {
18279af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
18289af6d512SAttilio Rao 			return;
18299af6d512SAttilio Rao 		}
18301f78f902SAlan Cox 		psize = object->size - pindex;
18311f78f902SAlan Cox 	}
18321f78f902SAlan Cox 
1833ce142d9eSAlan Cox 	start = 0;
1834ce142d9eSAlan Cox 	p_start = NULL;
18351f78f902SAlan Cox 
1836b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
18371f78f902SAlan Cox 	/*
18381f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
18391f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
18401f78f902SAlan Cox 	 * or (2) NULL.
18411f78f902SAlan Cox 	 */
18421f78f902SAlan Cox 	for (;
18431f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
18441f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
18451f78f902SAlan Cox 		/*
18461f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
18471f78f902SAlan Cox 		 * free pages allocating pv entries.
18481f78f902SAlan Cox 		 */
18491f78f902SAlan Cox 		if ((flags & MAP_PREFAULT_MADVISE) &&
18502feb50bfSAttilio Rao 		    cnt.v_free_count < cnt.v_free_reserved) {
1851379fb642SAlan Cox 			psize = tmpidx;
18521f78f902SAlan Cox 			break;
18531f78f902SAlan Cox 		}
18540a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1855ce142d9eSAlan Cox 			if (p_start == NULL) {
1856ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1857ce142d9eSAlan Cox 				p_start = p;
1858ce142d9eSAlan Cox 			}
18597bfda801SAlan Cox 		} else if (p_start != NULL) {
1860cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1861cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1862cf4682aeSAlan Cox 			p_start = NULL;
1863cf4682aeSAlan Cox 		}
1864cf4682aeSAlan Cox 	}
1865c46b90e9SAlan Cox 	if (p_start != NULL)
1866379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1867379fb642SAlan Cox 		    p_start, prot);
18689af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
18690551c08dSAlan Cox }
18700551c08dSAlan Cox 
18710551c08dSAlan Cox /*
1872df8bae1dSRodney W. Grimes  *	vm_map_protect:
1873df8bae1dSRodney W. Grimes  *
1874df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1875df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1876df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1877df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1878df8bae1dSRodney W. Grimes  */
1879df8bae1dSRodney W. Grimes int
1880b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1881b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1882df8bae1dSRodney W. Grimes {
1883210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
18843364c323SKonstantin Belousov 	vm_object_t obj;
1885ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
1886210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1887df8bae1dSRodney W. Grimes 
1888df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1889df8bae1dSRodney W. Grimes 
1890df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1891df8bae1dSRodney W. Grimes 
1892df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1893df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1894b7b2aac2SJohn Dyson 	} else {
1895df8bae1dSRodney W. Grimes 		entry = entry->next;
1896b7b2aac2SJohn Dyson 	}
1897df8bae1dSRodney W. Grimes 
1898df8bae1dSRodney W. Grimes 	/*
18990d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1900df8bae1dSRodney W. Grimes 	 */
1901df8bae1dSRodney W. Grimes 	current = entry;
1902df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1903afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1904a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1905df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1906a1f6d91cSDavid Greenman 		}
1907df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1908df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1909df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1910df8bae1dSRodney W. Grimes 		}
1911df8bae1dSRodney W. Grimes 		current = current->next;
1912df8bae1dSRodney W. Grimes 	}
1913df8bae1dSRodney W. Grimes 
19143364c323SKonstantin Belousov 
19153364c323SKonstantin Belousov 	/*
19163364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
19173364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
19183364c323SKonstantin Belousov 	 * breakpoint on text segment)
19193364c323SKonstantin Belousov 	 */
19203364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
19213364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
19223364c323SKonstantin Belousov 
19233364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
19243364c323SKonstantin Belousov 
19253364c323SKonstantin Belousov 		if (set_max ||
19263364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
19273364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
19283364c323SKonstantin Belousov 			continue;
19293364c323SKonstantin Belousov 		}
19303364c323SKonstantin Belousov 
1931ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
19323364c323SKonstantin Belousov 		obj = current->object.vm_object;
19333364c323SKonstantin Belousov 
19343364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
19353364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
19363364c323SKonstantin Belousov 				vm_map_unlock(map);
19373364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
19383364c323SKonstantin Belousov 			}
1939ef694c1aSEdward Tomasz Napierala 			crhold(cred);
1940ef694c1aSEdward Tomasz Napierala 			current->cred = cred;
19413364c323SKonstantin Belousov 			continue;
19423364c323SKonstantin Belousov 		}
19433364c323SKonstantin Belousov 
194489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
19453364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
194689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19473364c323SKonstantin Belousov 			continue;
19483364c323SKonstantin Belousov 		}
19493364c323SKonstantin Belousov 
19503364c323SKonstantin Belousov 		/*
19513364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
19523364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
19533364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
19543364c323SKonstantin Belousov 		 */
19553364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
19563364c323SKonstantin Belousov 		    ("vm_map_protect: object %p overcharged\n", obj));
19573364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
195889f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19593364c323SKonstantin Belousov 			vm_map_unlock(map);
19603364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
19613364c323SKonstantin Belousov 		}
19623364c323SKonstantin Belousov 
1963ef694c1aSEdward Tomasz Napierala 		crhold(cred);
1964ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
19653364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
196689f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
19673364c323SKonstantin Belousov 	}
19683364c323SKonstantin Belousov 
1969df8bae1dSRodney W. Grimes 	/*
19700d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
19710d94caffSDavid Greenman 	 * necessary the second time.]
1972df8bae1dSRodney W. Grimes 	 */
1973df8bae1dSRodney W. Grimes 	current = entry;
1974df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1975df8bae1dSRodney W. Grimes 		old_prot = current->protection;
1976210a6886SKonstantin Belousov 
1977df8bae1dSRodney W. Grimes 		if (set_max)
1978df8bae1dSRodney W. Grimes 			current->protection =
1979df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
1980df8bae1dSRodney W. Grimes 			    old_prot;
1981df8bae1dSRodney W. Grimes 		else
1982df8bae1dSRodney W. Grimes 			current->protection = new_prot;
1983df8bae1dSRodney W. Grimes 
1984210a6886SKonstantin Belousov 		if ((current->eflags & (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED))
1985210a6886SKonstantin Belousov 		     == (MAP_ENTRY_COW | MAP_ENTRY_USER_WIRED) &&
1986210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
1987210a6886SKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0) {
1988210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
1989210a6886SKonstantin Belousov 		}
1990210a6886SKonstantin Belousov 
1991df8bae1dSRodney W. Grimes 		/*
19922fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
19932fafce9eSAlan Cox 		 * about copy-on-write here.
1994df8bae1dSRodney W. Grimes 		 */
19952fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
1996afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
1997df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
1998df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
1999df8bae1dSRodney W. Grimes 			    current->end,
20001c85e3dfSAlan Cox 			    current->protection & MASK(current));
2001df8bae1dSRodney W. Grimes #undef	MASK
2002df8bae1dSRodney W. Grimes 		}
20037d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
2004df8bae1dSRodney W. Grimes 		current = current->next;
2005df8bae1dSRodney W. Grimes 	}
2006df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2007df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2008df8bae1dSRodney W. Grimes }
2009df8bae1dSRodney W. Grimes 
2010df8bae1dSRodney W. Grimes /*
2011867a482dSJohn Dyson  *	vm_map_madvise:
2012867a482dSJohn Dyson  *
2013867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2014f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2015f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2016f7fc307aSAlan Cox  *	objects.
2017867a482dSJohn Dyson  */
2018b4309055SMatthew Dillon int
20191b40f8c0SMatthew Dillon vm_map_madvise(
20201b40f8c0SMatthew Dillon 	vm_map_t map,
20211b40f8c0SMatthew Dillon 	vm_offset_t start,
20221b40f8c0SMatthew Dillon 	vm_offset_t end,
20231b40f8c0SMatthew Dillon 	int behav)
2024867a482dSJohn Dyson {
2025f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
2026b4309055SMatthew Dillon 	int modify_map = 0;
2027867a482dSJohn Dyson 
2028b4309055SMatthew Dillon 	/*
2029b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2030b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2031b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2032b4309055SMatthew Dillon 	 * on the map.
2033b4309055SMatthew Dillon 	 */
2034b4309055SMatthew Dillon 	switch(behav) {
2035b4309055SMatthew Dillon 	case MADV_NORMAL:
2036b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2037b4309055SMatthew Dillon 	case MADV_RANDOM:
20384f79d873SMatthew Dillon 	case MADV_NOSYNC:
20394f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
20409730a5daSPaul Saab 	case MADV_NOCORE:
20419730a5daSPaul Saab 	case MADV_CORE:
2042b4309055SMatthew Dillon 		modify_map = 1;
2043867a482dSJohn Dyson 		vm_map_lock(map);
2044b4309055SMatthew Dillon 		break;
2045b4309055SMatthew Dillon 	case MADV_WILLNEED:
2046b4309055SMatthew Dillon 	case MADV_DONTNEED:
2047b4309055SMatthew Dillon 	case MADV_FREE:
2048f7fc307aSAlan Cox 		vm_map_lock_read(map);
2049b4309055SMatthew Dillon 		break;
2050b4309055SMatthew Dillon 	default:
2051b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
2052b4309055SMatthew Dillon 	}
2053b4309055SMatthew Dillon 
2054b4309055SMatthew Dillon 	/*
2055b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2056b4309055SMatthew Dillon 	 */
2057867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2058867a482dSJohn Dyson 
2059867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
2060f7fc307aSAlan Cox 		if (modify_map)
2061867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
2062b4309055SMatthew Dillon 	} else {
2063867a482dSJohn Dyson 		entry = entry->next;
2064b4309055SMatthew Dillon 	}
2065867a482dSJohn Dyson 
2066f7fc307aSAlan Cox 	if (modify_map) {
2067f7fc307aSAlan Cox 		/*
2068f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2069f7fc307aSAlan Cox 		 *
2070f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2071f7fc307aSAlan Cox 		 * limited to the specified address range.
2072f7fc307aSAlan Cox 		 */
2073867a482dSJohn Dyson 		for (current = entry;
2074867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2075b4309055SMatthew Dillon 		     current = current->next
2076b4309055SMatthew Dillon 		) {
2077f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2078867a482dSJohn Dyson 				continue;
2079fed9a903SJohn Dyson 
208047221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2081fed9a903SJohn Dyson 
2082f7fc307aSAlan Cox 			switch (behav) {
2083867a482dSJohn Dyson 			case MADV_NORMAL:
20847f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2085867a482dSJohn Dyson 				break;
2086867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
20877f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2088867a482dSJohn Dyson 				break;
2089867a482dSJohn Dyson 			case MADV_RANDOM:
20907f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2091867a482dSJohn Dyson 				break;
20924f79d873SMatthew Dillon 			case MADV_NOSYNC:
20934f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
20944f79d873SMatthew Dillon 				break;
20954f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
20964f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
20974f79d873SMatthew Dillon 				break;
20989730a5daSPaul Saab 			case MADV_NOCORE:
20999730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
21009730a5daSPaul Saab 				break;
21019730a5daSPaul Saab 			case MADV_CORE:
21029730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
21039730a5daSPaul Saab 				break;
2104867a482dSJohn Dyson 			default:
2105867a482dSJohn Dyson 				break;
2106867a482dSJohn Dyson 			}
2107f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2108867a482dSJohn Dyson 		}
2109867a482dSJohn Dyson 		vm_map_unlock(map);
2110b4309055SMatthew Dillon 	} else {
211192a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
2112f7fc307aSAlan Cox 
2113f7fc307aSAlan Cox 		/*
2114f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2115f7fc307aSAlan Cox 		 * vm_object.
2116f7fc307aSAlan Cox 		 *
2117f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2118f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2119f7fc307aSAlan Cox 		 */
2120f7fc307aSAlan Cox 		for (current = entry;
2121f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2122b4309055SMatthew Dillon 		     current = current->next
2123b4309055SMatthew Dillon 		) {
21245f99b57cSMatthew Dillon 			vm_offset_t useStart;
21255f99b57cSMatthew Dillon 
2126f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2127f7fc307aSAlan Cox 				continue;
2128f7fc307aSAlan Cox 
212992a59946SJohn Baldwin 			pstart = OFF_TO_IDX(current->offset);
213092a59946SJohn Baldwin 			pend = pstart + atop(current->end - current->start);
21315f99b57cSMatthew Dillon 			useStart = current->start;
2132f7fc307aSAlan Cox 
2133f7fc307aSAlan Cox 			if (current->start < start) {
213492a59946SJohn Baldwin 				pstart += atop(start - current->start);
21355f99b57cSMatthew Dillon 				useStart = start;
2136f7fc307aSAlan Cox 			}
2137f7fc307aSAlan Cox 			if (current->end > end)
213892a59946SJohn Baldwin 				pend -= atop(current->end - end);
2139f7fc307aSAlan Cox 
214092a59946SJohn Baldwin 			if (pstart >= pend)
2141f7fc307aSAlan Cox 				continue;
2142f7fc307aSAlan Cox 
214392a59946SJohn Baldwin 			vm_object_madvise(current->object.vm_object, pstart,
214492a59946SJohn Baldwin 			    pend, behav);
2145b4309055SMatthew Dillon 			if (behav == MADV_WILLNEED) {
21460551c08dSAlan Cox 				vm_map_pmap_enter(map,
21475f99b57cSMatthew Dillon 				    useStart,
21484da4d293SAlan Cox 				    current->protection,
2149f7fc307aSAlan Cox 				    current->object.vm_object,
215092a59946SJohn Baldwin 				    pstart,
215192a59946SJohn Baldwin 				    ptoa(pend - pstart),
2152e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2153b4309055SMatthew Dillon 				);
2154f7fc307aSAlan Cox 			}
2155f7fc307aSAlan Cox 		}
2156f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2157f7fc307aSAlan Cox 	}
2158b4309055SMatthew Dillon 	return (0);
2159867a482dSJohn Dyson }
2160867a482dSJohn Dyson 
2161867a482dSJohn Dyson 
2162867a482dSJohn Dyson /*
2163df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2164df8bae1dSRodney W. Grimes  *
2165df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2166df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2167df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2168e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2169df8bae1dSRodney W. Grimes  */
2170df8bae1dSRodney W. Grimes int
2171b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2172b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2173df8bae1dSRodney W. Grimes {
2174c0877f10SJohn Dyson 	vm_map_entry_t entry;
2175df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2176df8bae1dSRodney W. Grimes 
2177df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2178df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2179df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2180df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2181df8bae1dSRodney W. Grimes 		break;
2182df8bae1dSRodney W. Grimes 	default:
2183df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2184df8bae1dSRodney W. Grimes 	}
2185df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2186df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2187df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2188df8bae1dSRodney W. Grimes 		entry = temp_entry;
2189df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
21900d94caffSDavid Greenman 	} else
2191df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2192df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2193df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2194df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
219544428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2196df8bae1dSRodney W. Grimes 		entry = entry->next;
2197df8bae1dSRodney W. Grimes 	}
2198df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2199df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2200df8bae1dSRodney W. Grimes }
2201df8bae1dSRodney W. Grimes 
2202df8bae1dSRodney W. Grimes /*
2203acd9a301SAlan Cox  *	vm_map_unwire:
2204acd9a301SAlan Cox  *
2205e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2206acd9a301SAlan Cox  */
2207acd9a301SAlan Cox int
2208acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2209abd498aaSBruce M Simpson     int flags)
2210acd9a301SAlan Cox {
2211acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2212acd9a301SAlan Cox 	vm_offset_t saved_start;
2213acd9a301SAlan Cox 	unsigned int last_timestamp;
2214acd9a301SAlan Cox 	int rv;
2215abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2216acd9a301SAlan Cox 
2217abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2218acd9a301SAlan Cox 	vm_map_lock(map);
2219acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2220acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2221abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2222cbef13d8SAlan Cox 			first_entry = first_entry->next;
2223abd498aaSBruce M Simpson 		else {
2224acd9a301SAlan Cox 			vm_map_unlock(map);
2225acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2226acd9a301SAlan Cox 		}
2227abd498aaSBruce M Simpson 	}
2228acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2229acd9a301SAlan Cox 	entry = first_entry;
2230acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2231acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2232acd9a301SAlan Cox 			/*
2233acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2234acd9a301SAlan Cox 			 */
2235acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2236acd9a301SAlan Cox 			    entry->start;
2237acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
22388ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2239acd9a301SAlan Cox 				/*
2240acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2241acd9a301SAlan Cox 				 */
2242acd9a301SAlan Cox 			}
2243acd9a301SAlan Cox 			vm_map_lock(map);
2244acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2245acd9a301SAlan Cox 				/*
2246acd9a301SAlan Cox 				 * Look again for the entry because the map was
2247acd9a301SAlan Cox 				 * modified while it was unlocked.
2248acd9a301SAlan Cox 				 * Specifically, the entry may have been
2249acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2250acd9a301SAlan Cox 				 */
2251acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2252acd9a301SAlan Cox 				    &tmp_entry)) {
2253cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2254cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2255cbef13d8SAlan Cox 					else {
2256acd9a301SAlan Cox 						if (saved_start == start) {
2257acd9a301SAlan Cox 							/*
2258acd9a301SAlan Cox 							 * First_entry has been deleted.
2259acd9a301SAlan Cox 							 */
2260acd9a301SAlan Cox 							vm_map_unlock(map);
2261acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2262acd9a301SAlan Cox 						}
2263acd9a301SAlan Cox 						end = saved_start;
2264acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2265acd9a301SAlan Cox 						goto done;
2266acd9a301SAlan Cox 					}
2267cbef13d8SAlan Cox 				}
2268acd9a301SAlan Cox 				if (entry == first_entry)
2269acd9a301SAlan Cox 					first_entry = tmp_entry;
2270acd9a301SAlan Cox 				else
2271acd9a301SAlan Cox 					first_entry = NULL;
2272acd9a301SAlan Cox 				entry = tmp_entry;
2273acd9a301SAlan Cox 			}
2274acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2275acd9a301SAlan Cox 			continue;
2276acd9a301SAlan Cox 		}
2277acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2278acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2279acd9a301SAlan Cox 		/*
2280acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2281acd9a301SAlan Cox 		 * above.)
2282acd9a301SAlan Cox 		 */
2283acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2284acd9a301SAlan Cox 		/*
2285acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2286abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2287acd9a301SAlan Cox 		 */
2288abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2289abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2290abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2291acd9a301SAlan Cox 			end = entry->end;
2292acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2293acd9a301SAlan Cox 			goto done;
2294acd9a301SAlan Cox 		}
2295acd9a301SAlan Cox 		/*
22963ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2297acd9a301SAlan Cox 		 */
22980ada205eSBrian Feldman 		if (!user_unwire &&
22990ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2300acd9a301SAlan Cox 			end = entry->end;
2301acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2302acd9a301SAlan Cox 			goto done;
2303acd9a301SAlan Cox 		}
2304acd9a301SAlan Cox 		entry = entry->next;
2305acd9a301SAlan Cox 	}
2306acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2307acd9a301SAlan Cox done:
2308e27e17b7SAlan Cox 	need_wakeup = FALSE;
2309acd9a301SAlan Cox 	if (first_entry == NULL) {
2310acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2311cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2312cbef13d8SAlan Cox 			first_entry = first_entry->next;
2313cbef13d8SAlan Cox 		else
2314acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2315acd9a301SAlan Cox 	}
2316acd9a301SAlan Cox 	entry = first_entry;
2317acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
23183ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
23193ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2320b2f3846aSAlan Cox 			if (user_unwire)
2321b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
2322b2f3846aSAlan Cox 			entry->wired_count--;
23230ada205eSBrian Feldman 			if (entry->wired_count == 0) {
2324b2f3846aSAlan Cox 				/*
2325b2f3846aSAlan Cox 				 * Retain the map lock.
2326b2f3846aSAlan Cox 				 */
23274be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
23284be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
232928634820SAlan Cox 				    (entry->object.vm_object->flags &
233028634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
2331b2f3846aSAlan Cox 			}
2332b2f3846aSAlan Cox 		}
2333acd9a301SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
2334acd9a301SAlan Cox 			("vm_map_unwire: in-transition flag missing"));
2335acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
2336acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2337acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2338acd9a301SAlan Cox 			need_wakeup = TRUE;
2339acd9a301SAlan Cox 		}
2340acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2341acd9a301SAlan Cox 		entry = entry->next;
2342acd9a301SAlan Cox 	}
2343acd9a301SAlan Cox 	vm_map_unlock(map);
2344acd9a301SAlan Cox 	if (need_wakeup)
2345acd9a301SAlan Cox 		vm_map_wakeup(map);
2346acd9a301SAlan Cox 	return (rv);
2347acd9a301SAlan Cox }
2348acd9a301SAlan Cox 
2349acd9a301SAlan Cox /*
2350e27e17b7SAlan Cox  *	vm_map_wire:
2351e27e17b7SAlan Cox  *
2352e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2353e27e17b7SAlan Cox  */
2354e27e17b7SAlan Cox int
2355e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2356abd498aaSBruce M Simpson     int flags)
2357e27e17b7SAlan Cox {
235812d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
235912d7cc84SAlan Cox 	vm_offset_t saved_end, saved_start;
236012d7cc84SAlan Cox 	unsigned int last_timestamp;
236112d7cc84SAlan Cox 	int rv;
23624be14af9SAlan Cox 	boolean_t fictitious, need_wakeup, result, user_wire;
2363e4cd31ddSJeff Roberson 	vm_prot_t prot;
2364e27e17b7SAlan Cox 
2365e4cd31ddSJeff Roberson 	prot = 0;
2366e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
2367e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
2368abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
236912d7cc84SAlan Cox 	vm_map_lock(map);
237012d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
237112d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2372abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2373cbef13d8SAlan Cox 			first_entry = first_entry->next;
2374abd498aaSBruce M Simpson 		else {
237512d7cc84SAlan Cox 			vm_map_unlock(map);
237612d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
237712d7cc84SAlan Cox 		}
2378abd498aaSBruce M Simpson 	}
237912d7cc84SAlan Cox 	last_timestamp = map->timestamp;
238012d7cc84SAlan Cox 	entry = first_entry;
238112d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
238212d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
238312d7cc84SAlan Cox 			/*
238412d7cc84SAlan Cox 			 * We have not yet clipped the entry.
238512d7cc84SAlan Cox 			 */
238612d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
238712d7cc84SAlan Cox 			    entry->start;
238812d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
23898ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
239012d7cc84SAlan Cox 				/*
239112d7cc84SAlan Cox 				 * Allow interruption of user wiring?
239212d7cc84SAlan Cox 				 */
239312d7cc84SAlan Cox 			}
239412d7cc84SAlan Cox 			vm_map_lock(map);
239512d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
239612d7cc84SAlan Cox 				/*
239712d7cc84SAlan Cox 				 * Look again for the entry because the map was
239812d7cc84SAlan Cox 				 * modified while it was unlocked.
239912d7cc84SAlan Cox 				 * Specifically, the entry may have been
240012d7cc84SAlan Cox 				 * clipped, merged, or deleted.
240112d7cc84SAlan Cox 				 */
240212d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
240312d7cc84SAlan Cox 				    &tmp_entry)) {
2404cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2405cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2406cbef13d8SAlan Cox 					else {
240712d7cc84SAlan Cox 						if (saved_start == start) {
240812d7cc84SAlan Cox 							/*
240912d7cc84SAlan Cox 							 * first_entry has been deleted.
241012d7cc84SAlan Cox 							 */
241112d7cc84SAlan Cox 							vm_map_unlock(map);
241212d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
241312d7cc84SAlan Cox 						}
241412d7cc84SAlan Cox 						end = saved_start;
241512d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
241612d7cc84SAlan Cox 						goto done;
241712d7cc84SAlan Cox 					}
2418cbef13d8SAlan Cox 				}
241912d7cc84SAlan Cox 				if (entry == first_entry)
242012d7cc84SAlan Cox 					first_entry = tmp_entry;
242112d7cc84SAlan Cox 				else
242212d7cc84SAlan Cox 					first_entry = NULL;
242312d7cc84SAlan Cox 				entry = tmp_entry;
242412d7cc84SAlan Cox 			}
242512d7cc84SAlan Cox 			last_timestamp = map->timestamp;
242612d7cc84SAlan Cox 			continue;
242712d7cc84SAlan Cox 		}
242812d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
242912d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
243012d7cc84SAlan Cox 		/*
243112d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
243212d7cc84SAlan Cox 		 * above.)
243312d7cc84SAlan Cox 		 */
243412d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
2435e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2436e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
2437529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
24386d7e8091SKonstantin Belousov 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
24396d7e8091SKonstantin Belousov 				end = entry->end;
24406d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
24416d7e8091SKonstantin Belousov 				goto done;
24426d7e8091SKonstantin Belousov 			}
24436d7e8091SKonstantin Belousov 			goto next_entry;
24446d7e8091SKonstantin Belousov 		}
2445e4cd31ddSJeff Roberson 		if (entry->wired_count == 0) {
24460ada205eSBrian Feldman 			entry->wired_count++;
244712d7cc84SAlan Cox 			saved_start = entry->start;
244812d7cc84SAlan Cox 			saved_end = entry->end;
24494be14af9SAlan Cox 			fictitious = entry->object.vm_object != NULL &&
245028634820SAlan Cox 			    (entry->object.vm_object->flags &
245128634820SAlan Cox 			    OBJ_FICTITIOUS) != 0;
245212d7cc84SAlan Cox 			/*
245312d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
2454a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
245512d7cc84SAlan Cox 			 */
2456a5db445dSMax Laier 			vm_map_busy(map);
245712d7cc84SAlan Cox 			vm_map_unlock(map);
2458ef594d31SAlan Cox 			rv = vm_fault_wire(map, saved_start, saved_end,
24592db65ab4SAlan Cox 			    fictitious);
246012d7cc84SAlan Cox 			vm_map_lock(map);
2461a5db445dSMax Laier 			vm_map_unbusy(map);
246212d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
246312d7cc84SAlan Cox 				/*
246412d7cc84SAlan Cox 				 * Look again for the entry because the map was
246512d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
246612d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
246712d7cc84SAlan Cox 				 * deleted.
246812d7cc84SAlan Cox 				 */
246912d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
247012d7cc84SAlan Cox 				    &tmp_entry);
247112d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
247212d7cc84SAlan Cox 				if (entry == first_entry)
247312d7cc84SAlan Cox 					first_entry = tmp_entry;
247412d7cc84SAlan Cox 				else
247512d7cc84SAlan Cox 					first_entry = NULL;
247612d7cc84SAlan Cox 				entry = tmp_entry;
247728c58286SAlan Cox 				while (entry->end < saved_end) {
247828c58286SAlan Cox 					if (rv != KERN_SUCCESS) {
247928c58286SAlan Cox 						KASSERT(entry->wired_count == 1,
248028c58286SAlan Cox 						    ("vm_map_wire: bad count"));
248128c58286SAlan Cox 						entry->wired_count = -1;
248228c58286SAlan Cox 					}
248312d7cc84SAlan Cox 					entry = entry->next;
248412d7cc84SAlan Cox 				}
248528c58286SAlan Cox 			}
248612d7cc84SAlan Cox 			last_timestamp = map->timestamp;
248712d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
248828c58286SAlan Cox 				KASSERT(entry->wired_count == 1,
248928c58286SAlan Cox 				    ("vm_map_wire: bad count"));
249012d7cc84SAlan Cox 				/*
249128c58286SAlan Cox 				 * Assign an out-of-range value to represent
249228c58286SAlan Cox 				 * the failure to wire this entry.
249312d7cc84SAlan Cox 				 */
249428c58286SAlan Cox 				entry->wired_count = -1;
249512d7cc84SAlan Cox 				end = entry->end;
249612d7cc84SAlan Cox 				goto done;
249712d7cc84SAlan Cox 			}
24980ada205eSBrian Feldman 		} else if (!user_wire ||
24990ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
25000ada205eSBrian Feldman 			entry->wired_count++;
250112d7cc84SAlan Cox 		}
250212d7cc84SAlan Cox 		/*
250312d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2504abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
250512d7cc84SAlan Cox 		 */
25066d7e8091SKonstantin Belousov 	next_entry:
2507abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2508abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2509abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
251012d7cc84SAlan Cox 			end = entry->end;
251112d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
251212d7cc84SAlan Cox 			goto done;
251312d7cc84SAlan Cox 		}
251412d7cc84SAlan Cox 		entry = entry->next;
251512d7cc84SAlan Cox 	}
251612d7cc84SAlan Cox 	rv = KERN_SUCCESS;
251712d7cc84SAlan Cox done:
251812d7cc84SAlan Cox 	need_wakeup = FALSE;
251912d7cc84SAlan Cox 	if (first_entry == NULL) {
252012d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2521cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2522cbef13d8SAlan Cox 			first_entry = first_entry->next;
2523cbef13d8SAlan Cox 		else
252412d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
252512d7cc84SAlan Cox 	}
252612d7cc84SAlan Cox 	entry = first_entry;
252712d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
25286d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
25296d7e8091SKonstantin Belousov 			goto next_entry_done;
253012d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
253112d7cc84SAlan Cox 			if (user_wire)
253212d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
253328c58286SAlan Cox 		} else if (entry->wired_count == -1) {
253428c58286SAlan Cox 			/*
253528c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
253628c58286SAlan Cox 			 * unnecessary.
253728c58286SAlan Cox 			 */
253828c58286SAlan Cox 			entry->wired_count = 0;
253912d7cc84SAlan Cox 		} else {
25400ada205eSBrian Feldman 			if (!user_wire ||
25410ada205eSBrian Feldman 			    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0)
254212d7cc84SAlan Cox 				entry->wired_count--;
25430ada205eSBrian Feldman 			if (entry->wired_count == 0) {
254412d7cc84SAlan Cox 				/*
254512d7cc84SAlan Cox 				 * Retain the map lock.
254612d7cc84SAlan Cox 				 */
25474be14af9SAlan Cox 				vm_fault_unwire(map, entry->start, entry->end,
25484be14af9SAlan Cox 				    entry->object.vm_object != NULL &&
254928634820SAlan Cox 				    (entry->object.vm_object->flags &
255028634820SAlan Cox 				    OBJ_FICTITIOUS) != 0);
255112d7cc84SAlan Cox 			}
255212d7cc84SAlan Cox 		}
25536d7e8091SKonstantin Belousov 	next_entry_done:
255412d7cc84SAlan Cox 		KASSERT(entry->eflags & MAP_ENTRY_IN_TRANSITION,
255512d7cc84SAlan Cox 			("vm_map_wire: in-transition flag missing"));
25566d7e8091SKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION|MAP_ENTRY_WIRE_SKIPPED);
255712d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
255812d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
255912d7cc84SAlan Cox 			need_wakeup = TRUE;
256012d7cc84SAlan Cox 		}
256112d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
256212d7cc84SAlan Cox 		entry = entry->next;
256312d7cc84SAlan Cox 	}
256412d7cc84SAlan Cox 	vm_map_unlock(map);
256512d7cc84SAlan Cox 	if (need_wakeup)
256612d7cc84SAlan Cox 		vm_map_wakeup(map);
256712d7cc84SAlan Cox 	return (rv);
2568e27e17b7SAlan Cox }
2569e27e17b7SAlan Cox 
2570e27e17b7SAlan Cox /*
2571950f8459SAlan Cox  * vm_map_sync
2572df8bae1dSRodney W. Grimes  *
2573df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2574df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2575df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2576df8bae1dSRodney W. Grimes  *
2577637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2578637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2579637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2580637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2581637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2582637315edSAlan Cox  * flushing the current region containing start.
2583637315edSAlan Cox  *
2584df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2585df8bae1dSRodney W. Grimes  */
2586df8bae1dSRodney W. Grimes int
2587950f8459SAlan Cox vm_map_sync(
25881b40f8c0SMatthew Dillon 	vm_map_t map,
25891b40f8c0SMatthew Dillon 	vm_offset_t start,
25901b40f8c0SMatthew Dillon 	vm_offset_t end,
25911b40f8c0SMatthew Dillon 	boolean_t syncio,
25921b40f8c0SMatthew Dillon 	boolean_t invalidate)
2593df8bae1dSRodney W. Grimes {
2594c0877f10SJohn Dyson 	vm_map_entry_t current;
2595df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2596df8bae1dSRodney W. Grimes 	vm_size_t size;
2597df8bae1dSRodney W. Grimes 	vm_object_t object;
2598a316d390SJohn Dyson 	vm_ooffset_t offset;
2599e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2600126d6082SKonstantin Belousov 	boolean_t failed;
2601df8bae1dSRodney W. Grimes 
2602df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2603df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2604df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2605df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2606df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2607637315edSAlan Cox 	} else if (start == end) {
2608637315edSAlan Cox 		start = entry->start;
2609637315edSAlan Cox 		end = entry->end;
2610df8bae1dSRodney W. Grimes 	}
2611df8bae1dSRodney W. Grimes 	/*
2612b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2613df8bae1dSRodney W. Grimes 	 */
26147b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
26157b0e72d1SAlan Cox 	    current = current->next) {
2616b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2617df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2618df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2619df8bae1dSRodney W. Grimes 		}
2620df8bae1dSRodney W. Grimes 		if (end > current->end &&
2621df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2622df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2623df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2624df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2625df8bae1dSRodney W. Grimes 		}
2626df8bae1dSRodney W. Grimes 	}
2627df8bae1dSRodney W. Grimes 
26282cf13952SAlan Cox 	if (invalidate)
2629bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2630126d6082SKonstantin Belousov 	failed = FALSE;
26312cf13952SAlan Cox 
2632df8bae1dSRodney W. Grimes 	/*
2633df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2634df8bae1dSRodney W. Grimes 	 * objects as we go.
2635df8bae1dSRodney W. Grimes 	 */
2636e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2637df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2638df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
26399fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2640c0877f10SJohn Dyson 			vm_map_t smap;
2641df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2642df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2643df8bae1dSRodney W. Grimes 
26449fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2645df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2646df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2647df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2648df8bae1dSRodney W. Grimes 			if (tsize < size)
2649df8bae1dSRodney W. Grimes 				size = tsize;
2650df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2651df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2652df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2653df8bae1dSRodney W. Grimes 		} else {
2654df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2655df8bae1dSRodney W. Grimes 		}
2656e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2657e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2658e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2659126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
2660126d6082SKonstantin Belousov 			failed = TRUE;
2661df8bae1dSRodney W. Grimes 		start += size;
2662e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2663e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2664e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2665e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2666e53fa61bSKonstantin Belousov 			current = current->next;
2667df8bae1dSRodney W. Grimes 	}
2668df8bae1dSRodney W. Grimes 
2669df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2670126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
2671df8bae1dSRodney W. Grimes }
2672df8bae1dSRodney W. Grimes 
2673df8bae1dSRodney W. Grimes /*
2674df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2675df8bae1dSRodney W. Grimes  *
2676df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2677df8bae1dSRodney W. Grimes  *
2678df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2679df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2680df8bae1dSRodney W. Grimes  */
26810362d7d7SJohn Dyson static void
26821b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2683df8bae1dSRodney W. Grimes {
26844be14af9SAlan Cox 	vm_fault_unwire(map, entry->start, entry->end,
26854be14af9SAlan Cox 	    entry->object.vm_object != NULL &&
268628634820SAlan Cox 	    (entry->object.vm_object->flags & OBJ_FICTITIOUS) != 0);
2687df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2688df8bae1dSRodney W. Grimes }
2689df8bae1dSRodney W. Grimes 
26900b367bd8SKonstantin Belousov static void
26910b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
26920b367bd8SKonstantin Belousov {
26930b367bd8SKonstantin Belousov 
26940b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
26950b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
26960b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
26970b367bd8SKonstantin Belousov }
26980b367bd8SKonstantin Belousov 
2699df8bae1dSRodney W. Grimes /*
2700df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2701df8bae1dSRodney W. Grimes  *
2702df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2703df8bae1dSRodney W. Grimes  */
27040362d7d7SJohn Dyson static void
27051b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2706df8bae1dSRodney W. Grimes {
270732a89c32SAlan Cox 	vm_object_t object;
27083364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
27093364c323SKonstantin Belousov 	vm_ooffset_t size;
271032a89c32SAlan Cox 
2711df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
27123364c323SKonstantin Belousov 	object = entry->object.vm_object;
27133364c323SKonstantin Belousov 	size = entry->end - entry->start;
27143364c323SKonstantin Belousov 	map->size -= size;
27153364c323SKonstantin Belousov 
2716ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
2717ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
2718ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
27193364c323SKonstantin Belousov 	}
2720df8bae1dSRodney W. Grimes 
272132a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
27223364c323SKonstantin Belousov 	    (object != NULL)) {
2723ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
27243364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2725ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
27263364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
272732a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
272832a89c32SAlan Cox 		offidxend = offidxstart + count;
272989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
273032a89c32SAlan Cox 		if (object->ref_count != 1 &&
273132a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
27329f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
273332a89c32SAlan Cox 			vm_object_collapse(object);
27346bbee8e2SAlan Cox 
27356bbee8e2SAlan Cox 			/*
27366bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
27376bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
27386bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
27396bbee8e2SAlan Cox 			 * of pages.
27406bbee8e2SAlan Cox 			 */
27416bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
27426bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
274332a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
274432a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
274532a89c32SAlan Cox 			if (offidxend >= object->size &&
27463364c323SKonstantin Belousov 			    offidxstart < object->size) {
27473364c323SKonstantin Belousov 				size1 = object->size;
274832a89c32SAlan Cox 				object->size = offidxstart;
2749ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
27503364c323SKonstantin Belousov 					size1 -= object->size;
27513364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
27523364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
2753ef694c1aSEdward Tomasz Napierala 					swap_release_by_cred(ptoa(size1), object->cred);
27543364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
27553364c323SKonstantin Belousov 				}
27563364c323SKonstantin Belousov 			}
275732a89c32SAlan Cox 		}
275889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2759897d81a0SKonstantin Belousov 	} else
2760897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
27610b367bd8SKonstantin Belousov 	if (map->system_map)
27620b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
27630b367bd8SKonstantin Belousov 	else {
27640b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
27650b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
27660b367bd8SKonstantin Belousov 	}
2767df8bae1dSRodney W. Grimes }
2768df8bae1dSRodney W. Grimes 
2769df8bae1dSRodney W. Grimes /*
2770df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2771df8bae1dSRodney W. Grimes  *
2772df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2773df8bae1dSRodney W. Grimes  *	map.
2774df8bae1dSRodney W. Grimes  */
2775df8bae1dSRodney W. Grimes int
2776655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2777df8bae1dSRodney W. Grimes {
2778c0877f10SJohn Dyson 	vm_map_entry_t entry;
2779df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2780df8bae1dSRodney W. Grimes 
27813a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
27823a0916b8SKonstantin Belousov 
2783df8bae1dSRodney W. Grimes 	/*
2784df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2785df8bae1dSRodney W. Grimes 	 */
2786876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2787df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2788876318ecSAlan Cox 	else {
2789df8bae1dSRodney W. Grimes 		entry = first_entry;
2790df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2791df8bae1dSRodney W. Grimes 	}
2792df8bae1dSRodney W. Grimes 
2793df8bae1dSRodney W. Grimes 	/*
2794df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2795df8bae1dSRodney W. Grimes 	 */
2796df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2797df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2798df8bae1dSRodney W. Grimes 
279973b2baceSAlan Cox 		/*
280073b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
28017c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
28027c938963SBrian Feldman 		 * user maps.
280373b2baceSAlan Cox 		 */
28047c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
28057c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
28067c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
280773b2baceSAlan Cox 			unsigned int last_timestamp;
280873b2baceSAlan Cox 			vm_offset_t saved_start;
280973b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
281073b2baceSAlan Cox 
281173b2baceSAlan Cox 			saved_start = entry->start;
281273b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
281373b2baceSAlan Cox 			last_timestamp = map->timestamp;
28148ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
281573b2baceSAlan Cox 			vm_map_lock(map);
281673b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
281773b2baceSAlan Cox 				/*
281873b2baceSAlan Cox 				 * Look again for the entry because the map was
281973b2baceSAlan Cox 				 * modified while it was unlocked.
282073b2baceSAlan Cox 				 * Specifically, the entry may have been
282173b2baceSAlan Cox 				 * clipped, merged, or deleted.
282273b2baceSAlan Cox 				 */
282373b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
282473b2baceSAlan Cox 							 &tmp_entry))
282573b2baceSAlan Cox 					entry = tmp_entry->next;
282673b2baceSAlan Cox 				else {
282773b2baceSAlan Cox 					entry = tmp_entry;
282873b2baceSAlan Cox 					vm_map_clip_start(map, entry,
282973b2baceSAlan Cox 							  saved_start);
283073b2baceSAlan Cox 				}
283173b2baceSAlan Cox 			}
283273b2baceSAlan Cox 			continue;
283373b2baceSAlan Cox 		}
2834df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2835df8bae1dSRodney W. Grimes 
2836c0877f10SJohn Dyson 		next = entry->next;
2837df8bae1dSRodney W. Grimes 
2838df8bae1dSRodney W. Grimes 		/*
28390d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
28400d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
2841df8bae1dSRodney W. Grimes 		 */
2842c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
2843df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
2844c0877f10SJohn Dyson 		}
2845df8bae1dSRodney W. Grimes 
284632a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
2847df8bae1dSRodney W. Grimes 
2848df8bae1dSRodney W. Grimes 		/*
2849e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
2850e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
2851e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
2852e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
2853df8bae1dSRodney W. Grimes 		 */
2854df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
2855df8bae1dSRodney W. Grimes 		entry = next;
2856df8bae1dSRodney W. Grimes 	}
2857df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2858df8bae1dSRodney W. Grimes }
2859df8bae1dSRodney W. Grimes 
2860df8bae1dSRodney W. Grimes /*
2861df8bae1dSRodney W. Grimes  *	vm_map_remove:
2862df8bae1dSRodney W. Grimes  *
2863df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
2864df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
2865df8bae1dSRodney W. Grimes  */
2866df8bae1dSRodney W. Grimes int
28671b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
2868df8bae1dSRodney W. Grimes {
28696eaee3feSAlan Cox 	int result;
2870df8bae1dSRodney W. Grimes 
2871df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2872df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2873655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
2874df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2875df8bae1dSRodney W. Grimes 	return (result);
2876df8bae1dSRodney W. Grimes }
2877df8bae1dSRodney W. Grimes 
2878df8bae1dSRodney W. Grimes /*
2879df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
2880df8bae1dSRodney W. Grimes  *
28812d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
28822d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
28832d5c7e45SMatthew Dillon  *
28842d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
28852d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
28862d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
28872d5c7e45SMatthew Dillon  *
28882d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
2889d8834602SAlan Cox  *
2890d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
2891df8bae1dSRodney W. Grimes  */
28920d94caffSDavid Greenman boolean_t
2893b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
2894b9dcd593SBruce Evans 			vm_prot_t protection)
2895df8bae1dSRodney W. Grimes {
2896c0877f10SJohn Dyson 	vm_map_entry_t entry;
2897df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
2898df8bae1dSRodney W. Grimes 
2899d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
2900df8bae1dSRodney W. Grimes 		return (FALSE);
2901df8bae1dSRodney W. Grimes 	entry = tmp_entry;
2902df8bae1dSRodney W. Grimes 
2903df8bae1dSRodney W. Grimes 	while (start < end) {
2904d8834602SAlan Cox 		if (entry == &map->header)
2905df8bae1dSRodney W. Grimes 			return (FALSE);
2906df8bae1dSRodney W. Grimes 		/*
2907df8bae1dSRodney W. Grimes 		 * No holes allowed!
2908df8bae1dSRodney W. Grimes 		 */
2909d8834602SAlan Cox 		if (start < entry->start)
2910df8bae1dSRodney W. Grimes 			return (FALSE);
2911df8bae1dSRodney W. Grimes 		/*
2912df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
2913df8bae1dSRodney W. Grimes 		 */
2914d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
2915df8bae1dSRodney W. Grimes 			return (FALSE);
2916df8bae1dSRodney W. Grimes 		/* go to next entry */
2917df8bae1dSRodney W. Grimes 		start = entry->end;
2918df8bae1dSRodney W. Grimes 		entry = entry->next;
2919df8bae1dSRodney W. Grimes 	}
2920df8bae1dSRodney W. Grimes 	return (TRUE);
2921df8bae1dSRodney W. Grimes }
2922df8bae1dSRodney W. Grimes 
292386524867SJohn Dyson /*
2924df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
2925df8bae1dSRodney W. Grimes  *
2926df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
2927df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
2928df8bae1dSRodney W. Grimes  */
2929f708ef1bSPoul-Henning Kamp static void
29301b40f8c0SMatthew Dillon vm_map_copy_entry(
29311b40f8c0SMatthew Dillon 	vm_map_t src_map,
29321b40f8c0SMatthew Dillon 	vm_map_t dst_map,
29331b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
29343364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
29353364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
2936df8bae1dSRodney W. Grimes {
2937c0877f10SJohn Dyson 	vm_object_t src_object;
293884110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
29393364c323SKonstantin Belousov 	vm_offset_t size;
2940ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
29413364c323SKonstantin Belousov 	int charged;
2942c0877f10SJohn Dyson 
29433a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
29443a0916b8SKonstantin Belousov 
29459fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
2946df8bae1dSRodney W. Grimes 		return;
2947df8bae1dSRodney W. Grimes 
2948df8bae1dSRodney W. Grimes 	if (src_entry->wired_count == 0) {
2949df8bae1dSRodney W. Grimes 
2950df8bae1dSRodney W. Grimes 		/*
29510d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
29520d94caffSDavid Greenman 		 * write-protected.
2953df8bae1dSRodney W. Grimes 		 */
2954afa07f7eSJohn Dyson 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) {
2955df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
2956df8bae1dSRodney W. Grimes 			    src_entry->start,
2957df8bae1dSRodney W. Grimes 			    src_entry->end,
2958df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
2959df8bae1dSRodney W. Grimes 		}
2960b18bfc3dSJohn Dyson 
2961df8bae1dSRodney W. Grimes 		/*
2962df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
2963df8bae1dSRodney W. Grimes 		 */
29643364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
29658aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
296689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(src_object);
29673364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
2968c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
2969c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
2970c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
2971c0877f10SJohn Dyson 				vm_object_collapse(src_object);
297296fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
2973c5aaa06dSAlan Cox 					vm_object_split(src_entry);
2974c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
2975a89c6258SAlan Cox 				}
2976a89c6258SAlan Cox 			}
2977b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
2978069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
2979ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL &&
29803364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
2981ef694c1aSEdward Tomasz Napierala 				KASSERT(src_object->cred == NULL,
2982ef694c1aSEdward Tomasz Napierala 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
29833364c323SKonstantin Belousov 				     src_object));
2984ef694c1aSEdward Tomasz Napierala 				src_object->cred = src_entry->cred;
29853364c323SKonstantin Belousov 				src_object->charge = size;
29863364c323SKonstantin Belousov 			}
298789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(src_object);
2988c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
29893364c323SKonstantin Belousov 			if (charged) {
2990ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
2991ef694c1aSEdward Tomasz Napierala 				crhold(cred);
2992ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = cred;
29933364c323SKonstantin Belousov 				*fork_charge += size;
29943364c323SKonstantin Belousov 				if (!(src_entry->eflags &
29953364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
2996ef694c1aSEdward Tomasz Napierala 					crhold(cred);
2997ef694c1aSEdward Tomasz Napierala 					src_entry->cred = cred;
29983364c323SKonstantin Belousov 					*fork_charge += size;
29993364c323SKonstantin Belousov 				}
30003364c323SKonstantin Belousov 			}
3001afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3002afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3003b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
300484110e7eSKonstantin Belousov 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
300584110e7eSKonstantin Belousov 				/*
300684110e7eSKonstantin Belousov 				 * MAP_ENTRY_VN_WRITECNT cannot
300784110e7eSKonstantin Belousov 				 * indicate write reference from
300884110e7eSKonstantin Belousov 				 * src_entry, since the entry is
300984110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
301084110e7eSKonstantin Belousov 				 * fake entry that is used to
301184110e7eSKonstantin Belousov 				 * decrement object->un_pager.vnp.writecount
301284110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
301384110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
301484110e7eSKonstantin Belousov 				 */
301584110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
301684110e7eSKonstantin Belousov 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
301784110e7eSKonstantin Belousov 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
301884110e7eSKonstantin Belousov 				vm_object_reference(src_object);
301984110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
302084110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
302184110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
302284110e7eSKonstantin Belousov 				fake_entry->next = curthread->td_map_def_user;
302384110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
302484110e7eSKonstantin Belousov 			}
3025b18bfc3dSJohn Dyson 		} else {
3026b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
3027b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
3028ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
3029ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
3030ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
30313364c323SKonstantin Belousov 				*fork_charge += size;
30323364c323SKonstantin Belousov 			}
3033b18bfc3dSJohn Dyson 		}
3034df8bae1dSRodney W. Grimes 
3035df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3036df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
30370d94caffSDavid Greenman 	} else {
3038df8bae1dSRodney W. Grimes 		/*
3039df8bae1dSRodney W. Grimes 		 * Of course, wired down pages can't be set copy-on-write.
30400d94caffSDavid Greenman 		 * Cause wired pages to be copied into the new map by
30410d94caffSDavid Greenman 		 * simulating faults (the new pages are pageable)
3042df8bae1dSRodney W. Grimes 		 */
3043121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3044121fd461SKonstantin Belousov 		    fork_charge);
3045df8bae1dSRodney W. Grimes 	}
3046df8bae1dSRodney W. Grimes }
3047df8bae1dSRodney W. Grimes 
3048df8bae1dSRodney W. Grimes /*
30492a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
30502a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
30512a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
30522a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
30532a7be1b6SBrian Feldman  */
30542a7be1b6SBrian Feldman static void
30552a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
30562a7be1b6SBrian Feldman     vm_map_entry_t entry)
30572a7be1b6SBrian Feldman {
30582a7be1b6SBrian Feldman 	vm_size_t entrysize;
30592a7be1b6SBrian Feldman 	vm_offset_t newend;
30602a7be1b6SBrian Feldman 
30612a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
30622a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
30632a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
30642a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
30652a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
30662a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3067b351299cSAndrew Gallatin 		newend = MIN(entry->end,
30682a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
30692a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
30702a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
30712a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3072b351299cSAndrew Gallatin 		newend = MIN(entry->end,
30732a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
30742a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
30752a7be1b6SBrian Feldman 	}
30762a7be1b6SBrian Feldman }
30772a7be1b6SBrian Feldman 
30782a7be1b6SBrian Feldman /*
3079df8bae1dSRodney W. Grimes  * vmspace_fork:
3080df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
3081df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
3082df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
3083df8bae1dSRodney W. Grimes  * values on the regions in that map.
3084df8bae1dSRodney W. Grimes  *
30852a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
30862a7be1b6SBrian Feldman  *
3087df8bae1dSRodney W. Grimes  * The source map must not be locked.
3088df8bae1dSRodney W. Grimes  */
3089df8bae1dSRodney W. Grimes struct vmspace *
30903364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3091df8bae1dSRodney W. Grimes {
3092c0877f10SJohn Dyson 	struct vmspace *vm2;
309379e53838SAlan Cox 	vm_map_t new_map, old_map;
309479e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
3095de5f6a77SJohn Dyson 	vm_object_t object;
30961fac7d7fSKonstantin Belousov 	int locked;
3097df8bae1dSRodney W. Grimes 
309879e53838SAlan Cox 	old_map = &vm1->vm_map;
309979e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
31002d8acc0fSJohn Dyson 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset);
310189b57fcfSKonstantin Belousov 	if (vm2 == NULL)
310279e53838SAlan Cox 		return (NULL);
31032a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
31042a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
31052a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
310679e53838SAlan Cox 	vm_map_lock(old_map);
310779e53838SAlan Cox 	if (old_map->busy)
310879e53838SAlan Cox 		vm_map_wait_busy(old_map);
310979e53838SAlan Cox 	new_map = &vm2->vm_map;
31101fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
31111fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
3112df8bae1dSRodney W. Grimes 
3113df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3114df8bae1dSRodney W. Grimes 
3115df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3116afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3117df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3118df8bae1dSRodney W. Grimes 
3119df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3120df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3121df8bae1dSRodney W. Grimes 			break;
3122df8bae1dSRodney W. Grimes 
3123df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3124df8bae1dSRodney W. Grimes 			/*
3125fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3126fed9a903SJohn Dyson 			 */
3127fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3128fed9a903SJohn Dyson 			if (object == NULL) {
3129fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3130c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3131fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
313215d2d313SAlan Cox 				old_entry->offset = 0;
3133ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
3134ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
31353364c323SKonstantin Belousov 					object->charge = old_entry->end -
31363364c323SKonstantin Belousov 					    old_entry->start;
3137ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
31383364c323SKonstantin Belousov 				}
31399a2f6362SAlan Cox 			}
31409a2f6362SAlan Cox 
31419a2f6362SAlan Cox 			/*
31429a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
31439a2f6362SAlan Cox 			 * to insure that a shadow object is created.
31449a2f6362SAlan Cox 			 */
31459a2f6362SAlan Cox 			vm_object_reference(object);
31469a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
31475069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
31485069bf57SJohn Dyson 				    &old_entry->offset,
31490cc74f14SAlan Cox 				    old_entry->end - old_entry->start);
31505069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3151d30344bdSIan Dowse 				/* Transfer the second reference too. */
3152d30344bdSIan Dowse 				vm_object_reference(
3153d30344bdSIan Dowse 				    old_entry->object.vm_object);
31547fd10fb3SKonstantin Belousov 
31557fd10fb3SKonstantin Belousov 				/*
31567fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3157b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
31587fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
31597fd10fb3SKonstantin Belousov 				 */
3160d30344bdSIan Dowse 				vm_object_deallocate(object);
31615069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3162fed9a903SJohn Dyson 			}
316389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3164069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3165ef694c1aSEdward Tomasz Napierala 			if (old_entry->cred != NULL) {
3166ef694c1aSEdward Tomasz Napierala 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3167ef694c1aSEdward Tomasz Napierala 				object->cred = old_entry->cred;
31683364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
3169ef694c1aSEdward Tomasz Napierala 				old_entry->cred = NULL;
31703364c323SKonstantin Belousov 			}
3171b9781cf6SKonstantin Belousov 
3172b9781cf6SKonstantin Belousov 			/*
3173b9781cf6SKonstantin Belousov 			 * Assert the correct state of the vnode
3174b9781cf6SKonstantin Belousov 			 * v_writecount while the object is locked, to
3175b9781cf6SKonstantin Belousov 			 * not relock it later for the assertion
3176b9781cf6SKonstantin Belousov 			 * correctness.
3177b9781cf6SKonstantin Belousov 			 */
3178b9781cf6SKonstantin Belousov 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3179b9781cf6SKonstantin Belousov 			    object->type == OBJT_VNODE) {
3180b9781cf6SKonstantin Belousov 				KASSERT(((struct vnode *)object->handle)->
3181b9781cf6SKonstantin Belousov 				    v_writecount > 0,
3182b9781cf6SKonstantin Belousov 				    ("vmspace_fork: v_writecount %p", object));
3183b9781cf6SKonstantin Belousov 				KASSERT(object->un_pager.vnp.writemappings > 0,
3184b9781cf6SKonstantin Belousov 				    ("vmspace_fork: vnp.writecount %p",
3185b9781cf6SKonstantin Belousov 				    object));
3186b9781cf6SKonstantin Belousov 			}
318789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
3188fed9a903SJohn Dyson 
3189fed9a903SJohn Dyson 			/*
3190ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3191df8bae1dSRodney W. Grimes 			 */
3192df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3193df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
31949f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
31959f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
3196df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
319784110e7eSKonstantin Belousov 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
319884110e7eSKonstantin Belousov 				vnode_pager_update_writecount(object,
319984110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
320084110e7eSKonstantin Belousov 			}
3201df8bae1dSRodney W. Grimes 
3202df8bae1dSRodney W. Grimes 			/*
32030d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
32040d94caffSDavid Greenman 			 * inserting at the end of the new map.
3205df8bae1dSRodney W. Grimes 			 */
3206df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3207df8bae1dSRodney W. Grimes 			    new_entry);
32082a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3209df8bae1dSRodney W. Grimes 
3210df8bae1dSRodney W. Grimes 			/*
3211df8bae1dSRodney W. Grimes 			 * Update the physical map
3212df8bae1dSRodney W. Grimes 			 */
3213df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3214df8bae1dSRodney W. Grimes 			    new_entry->start,
3215df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3216df8bae1dSRodney W. Grimes 			    old_entry->start);
3217df8bae1dSRodney W. Grimes 			break;
3218df8bae1dSRodney W. Grimes 
3219df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3220df8bae1dSRodney W. Grimes 			/*
3221df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3222df8bae1dSRodney W. Grimes 			 */
3223df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3224df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
322584110e7eSKonstantin Belousov 			/*
322684110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
322784110e7eSKonstantin Belousov 			 */
32289f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
322984110e7eSKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
3230df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3231df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3232ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
3233df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3234df8bae1dSRodney W. Grimes 			    new_entry);
32352a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3236bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
32373364c323SKonstantin Belousov 			    new_entry, fork_charge);
3238df8bae1dSRodney W. Grimes 			break;
3239df8bae1dSRodney W. Grimes 		}
3240df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3241df8bae1dSRodney W. Grimes 	}
324284110e7eSKonstantin Belousov 	/*
324384110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
324484110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
324584110e7eSKonstantin Belousov 	 * new_map locks are released.
324684110e7eSKonstantin Belousov 	 */
324784110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
324884110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
324984110e7eSKonstantin Belousov 	vm_map_process_deferred();
3250df8bae1dSRodney W. Grimes 
3251df8bae1dSRodney W. Grimes 	return (vm2);
3252df8bae1dSRodney W. Grimes }
3253df8bae1dSRodney W. Grimes 
325494f7e29aSAlan Cox int
325594f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
325694f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
325794f7e29aSAlan Cox {
3258fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3259fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
3260cfe52ecfSAndrey Zonov 	vm_size_t growsize, init_ssize;
3261fd75d710SMarcel Moolenaar 	int orient, rv;
32627e19eda4SAndrey Zonov 	rlim_t lmemlim, vmemlim;
326394f7e29aSAlan Cox 
3264fd75d710SMarcel Moolenaar 	/*
3265fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3266fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3267fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3268fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3269fd75d710SMarcel Moolenaar 	 */
3270fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3271fd75d710SMarcel Moolenaar 	cow &= ~orient;
3272fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3273fd75d710SMarcel Moolenaar 
327477bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
327577bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
327677bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
327794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3278fd75d710SMarcel Moolenaar 
3279cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3280cfe52ecfSAndrey Zonov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
328194f7e29aSAlan Cox 
32827e19eda4SAndrey Zonov 	PROC_LOCK(curproc);
32837e19eda4SAndrey Zonov 	lmemlim = lim_cur(curproc, RLIMIT_MEMLOCK);
32847e19eda4SAndrey Zonov 	vmemlim = lim_cur(curproc, RLIMIT_VMEM);
32857e19eda4SAndrey Zonov 	PROC_UNLOCK(curproc);
328691d5354aSJohn Baldwin 
328794f7e29aSAlan Cox 	vm_map_lock(map);
328894f7e29aSAlan Cox 
328994f7e29aSAlan Cox 	/* If addr is already mapped, no go */
329094f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addrbos, &prev_entry)) {
329194f7e29aSAlan Cox 		vm_map_unlock(map);
329294f7e29aSAlan Cox 		return (KERN_NO_SPACE);
329394f7e29aSAlan Cox 	}
329494f7e29aSAlan Cox 
32957e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
32963ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + init_ssize > lmemlim) {
32977e19eda4SAndrey Zonov 			vm_map_unlock(map);
32987e19eda4SAndrey Zonov 			return (KERN_NO_SPACE);
32997e19eda4SAndrey Zonov 		}
33007e19eda4SAndrey Zonov 	}
33017e19eda4SAndrey Zonov 
3302a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
330391d5354aSJohn Baldwin 	if (map->size + init_ssize > vmemlim) {
3304a69ac174SMatthew Dillon 		vm_map_unlock(map);
3305a69ac174SMatthew Dillon 		return (KERN_NO_SPACE);
3306a69ac174SMatthew Dillon 	}
3307a69ac174SMatthew Dillon 
3308fd75d710SMarcel Moolenaar 	/*
3309fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3310fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3311fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3312fd75d710SMarcel Moolenaar 	 * space is not protected.
331394f7e29aSAlan Cox 	 *
3314fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3315fd75d710SMarcel Moolenaar 	 * grow the stack.
331694f7e29aSAlan Cox 	 */
331794f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
331894f7e29aSAlan Cox 	    (prev_entry->next->start < addrbos + max_ssize)) {
331994f7e29aSAlan Cox 		vm_map_unlock(map);
332094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
332194f7e29aSAlan Cox 	}
332294f7e29aSAlan Cox 
3323fd75d710SMarcel Moolenaar 	/*
3324fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3325fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3326fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3327fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
332894f7e29aSAlan Cox 	 *
3329fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3330fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3331fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
333294f7e29aSAlan Cox 	 */
3333fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3334fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3335fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3336fd75d710SMarcel Moolenaar 		bot = addrbos;
3337fd75d710SMarcel Moolenaar 	else
3338fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3339fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3340fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
334194f7e29aSAlan Cox 
3342fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
334394f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
334429b45e9eSAlan Cox 		if (prev_entry != &map->header)
3345fd75d710SMarcel Moolenaar 			vm_map_clip_end(map, prev_entry, bot);
3346fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3347fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
334894f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3349b21a0008SMarcel Moolenaar 
3350fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3351fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_DOWN)
3352fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
3353fd75d710SMarcel Moolenaar 		if (orient & MAP_STACK_GROWS_UP)
3354fd75d710SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_UP;
335594f7e29aSAlan Cox 	}
335694f7e29aSAlan Cox 
335794f7e29aSAlan Cox 	vm_map_unlock(map);
335894f7e29aSAlan Cox 	return (rv);
335994f7e29aSAlan Cox }
336094f7e29aSAlan Cox 
33619a6d144fSKonstantin Belousov static int stack_guard_page = 0;
33629a6d144fSKonstantin Belousov TUNABLE_INT("security.bsd.stack_guard_page", &stack_guard_page);
33639a6d144fSKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RW,
33649a6d144fSKonstantin Belousov     &stack_guard_page, 0,
33659a6d144fSKonstantin Belousov     "Insert stack guard page ahead of the growable segments.");
33669a6d144fSKonstantin Belousov 
336794f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
336894f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
336994f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
337094f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
337194f7e29aSAlan Cox  * the grow function in vm_machdep.c).
337294f7e29aSAlan Cox  */
337394f7e29aSAlan Cox int
337494f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
337594f7e29aSAlan Cox {
3376b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3377b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
337894f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
337994f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
338094f7e29aSAlan Cox 	vm_offset_t end;
3381cfe52ecfSAndrey Zonov 	vm_size_t growsize;
3382b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
33837e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
3384b21a0008SMarcel Moolenaar 	int is_procstack, rv;
3385ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
33861ba5ad42SEdward Tomasz Napierala #ifdef notyet
33871ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
33881ba5ad42SEdward Tomasz Napierala #endif
3389afcc55f3SEdward Tomasz Napierala #ifdef RACCT
33901ba5ad42SEdward Tomasz Napierala 	int error;
3391afcc55f3SEdward Tomasz Napierala #endif
339223955314SAlfred Perlstein 
339394f7e29aSAlan Cox Retry:
339491d5354aSJohn Baldwin 	PROC_LOCK(p);
33957e19eda4SAndrey Zonov 	lmemlim = lim_cur(p, RLIMIT_MEMLOCK);
339691d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3397bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
339891d5354aSJohn Baldwin 	PROC_UNLOCK(p);
339991d5354aSJohn Baldwin 
340094f7e29aSAlan Cox 	vm_map_lock_read(map);
340194f7e29aSAlan Cox 
340294f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
340394f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
340494f7e29aSAlan Cox 		vm_map_unlock_read(map);
34050cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
340694f7e29aSAlan Cox 	}
340794f7e29aSAlan Cox 
3408b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3409b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3410b21a0008SMarcel Moolenaar 		/*
3411b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3412b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3413b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3414b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
341594f7e29aSAlan Cox 		 */
3416b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
341794f7e29aSAlan Cox 			vm_map_unlock_read(map);
34180cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
341994f7e29aSAlan Cox 		}
3420b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3421b21a0008SMarcel Moolenaar 	} else {
3422b21a0008SMarcel Moolenaar 		/*
3423b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3424b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3425b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3426b21a0008SMarcel Moolenaar 		 * select one.
3427b21a0008SMarcel Moolenaar 		 */
3428b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3429b21a0008SMarcel Moolenaar 			/*
3430b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3431b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3432b21a0008SMarcel Moolenaar 			 */
3433b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3434b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3435b21a0008SMarcel Moolenaar 			else
3436b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3437b21a0008SMarcel Moolenaar 		} else
3438b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3439b21a0008SMarcel Moolenaar 	}
344094f7e29aSAlan Cox 
3441b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3442b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3443b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3444b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3445b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
344694f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3447b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3448b21a0008SMarcel Moolenaar 	} else {
3449b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
345008667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3451b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3452b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3453fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3454b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3455b21a0008SMarcel Moolenaar 	}
3456b21a0008SMarcel Moolenaar 
345794f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
345894f7e29aSAlan Cox 		vm_map_unlock_read(map);
34590cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
346094f7e29aSAlan Cox 	}
346194f7e29aSAlan Cox 
3462b21a0008SMarcel Moolenaar 	/*
3463b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3464b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3465b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3466b21a0008SMarcel Moolenaar 	 * and is probably an error.
346794f7e29aSAlan Cox 	 *
3468b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3469b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
347094f7e29aSAlan Cox 	 */
34719a6d144fSKonstantin Belousov 	if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
347225adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
347394f7e29aSAlan Cox 			goto Retry;
347494f7e29aSAlan Cox 
3475b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
347694f7e29aSAlan Cox 
347794f7e29aSAlan Cox 		vm_map_unlock(map);
34780cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
347994f7e29aSAlan Cox 	}
348094f7e29aSAlan Cox 
3481b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
348294f7e29aSAlan Cox 
3483b21a0008SMarcel Moolenaar 	/*
3484b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3485b21a0008SMarcel Moolenaar 	 * limit.
348694f7e29aSAlan Cox 	 */
348791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
348894f7e29aSAlan Cox 		vm_map_unlock_read(map);
34890cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
349094f7e29aSAlan Cox 	}
3491afcc55f3SEdward Tomasz Napierala #ifdef RACCT
34921ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
34931ba5ad42SEdward Tomasz Napierala 	if (is_procstack &&
34941ba5ad42SEdward Tomasz Napierala 	    racct_set(p, RACCT_STACK, ctob(vm->vm_ssize) + grow_amount)) {
34951ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
34961ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
34971ba5ad42SEdward Tomasz Napierala 		return (KERN_NO_SPACE);
34981ba5ad42SEdward Tomasz Napierala 	}
34991ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3500afcc55f3SEdward Tomasz Napierala #endif
350194f7e29aSAlan Cox 
3502cfe52ecfSAndrey Zonov 	/* Round up the grow amount modulo sgrowsiz */
3503cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3504cfe52ecfSAndrey Zonov 	grow_amount = roundup(grow_amount, growsize);
3505b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
350694f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
350791d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3508e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
3509e4826248SAlan Cox 		    ctob(vm->vm_ssize);
351094f7e29aSAlan Cox 	}
35111ba5ad42SEdward Tomasz Napierala #ifdef notyet
35121ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
35131ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
35141ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
35151ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
35161ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
35171ba5ad42SEdward Tomasz Napierala #endif
35187e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
35193ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
35207e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
35217e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
35227e19eda4SAndrey Zonov 			goto out;
35237e19eda4SAndrey Zonov 		}
35247e19eda4SAndrey Zonov #ifdef RACCT
35257e19eda4SAndrey Zonov 		PROC_LOCK(p);
35267e19eda4SAndrey Zonov 		if (racct_set(p, RACCT_MEMLOCK,
35273ac7d297SAndrey Zonov 		    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
35287e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
35297e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
35307e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
35317e19eda4SAndrey Zonov 			goto out;
35327e19eda4SAndrey Zonov 		}
35337e19eda4SAndrey Zonov 		PROC_UNLOCK(p);
35347e19eda4SAndrey Zonov #endif
35357e19eda4SAndrey Zonov 	}
3536a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
353791d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3538a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
35391ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
35401ba5ad42SEdward Tomasz Napierala 		goto out;
3541a69ac174SMatthew Dillon 	}
3542afcc55f3SEdward Tomasz Napierala #ifdef RACCT
35431ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
35441ba5ad42SEdward Tomasz Napierala 	if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
35451ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
35461ba5ad42SEdward Tomasz Napierala 		vm_map_unlock_read(map);
35471ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
35481ba5ad42SEdward Tomasz Napierala 		goto out;
35491ba5ad42SEdward Tomasz Napierala 	}
35501ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3551afcc55f3SEdward Tomasz Napierala #endif
3552a69ac174SMatthew Dillon 
355325adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
355494f7e29aSAlan Cox 		goto Retry;
355594f7e29aSAlan Cox 
3556b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3557b21a0008SMarcel Moolenaar 		/*
3558b21a0008SMarcel Moolenaar 		 * Growing downward.
3559b21a0008SMarcel Moolenaar 		 */
356094f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
356194f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
356294f7e29aSAlan Cox 
3563b21a0008SMarcel Moolenaar 		/*
3564b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3565b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
356694f7e29aSAlan Cox 		 */
356794f7e29aSAlan Cox 		if (addr < end) {
3568b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
356994f7e29aSAlan Cox 			addr = end;
35709a6d144fSKonstantin Belousov 			if (stack_guard_page)
35719a6d144fSKonstantin Belousov 				addr += PAGE_SIZE;
357294f7e29aSAlan Cox 		}
357394f7e29aSAlan Cox 
357494f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
357583ce0853SKonstantin Belousov 		    next_entry->protection, next_entry->max_protection, 0);
357694f7e29aSAlan Cox 
357794f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
357894f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
357929b45e9eSAlan Cox 			if (prev_entry != &map->header)
358029b45e9eSAlan Cox 				vm_map_clip_end(map, prev_entry, addr);
3581b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3582b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3583b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3584b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3585b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3586b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3587b21a0008SMarcel Moolenaar 			    grow_amount;
3588b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
3589b21a0008SMarcel Moolenaar 			new_entry->eflags |= MAP_ENTRY_GROWS_DOWN;
359094f7e29aSAlan Cox 		}
3591b21a0008SMarcel Moolenaar 	} else {
3592b21a0008SMarcel Moolenaar 		/*
3593b21a0008SMarcel Moolenaar 		 * Growing upward.
3594b21a0008SMarcel Moolenaar 		 */
3595b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3596b21a0008SMarcel Moolenaar 
3597b21a0008SMarcel Moolenaar 		/*
3598b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3599b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3600b21a0008SMarcel Moolenaar 		 */
3601b21a0008SMarcel Moolenaar 		if (addr > end) {
3602b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3603b21a0008SMarcel Moolenaar 			addr = end;
36049a6d144fSKonstantin Belousov 			if (stack_guard_page)
36059a6d144fSKonstantin Belousov 				addr -= PAGE_SIZE;
360694f7e29aSAlan Cox 		}
360794f7e29aSAlan Cox 
3608b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
3609ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
3610ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
3611ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
3612ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
36133364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3614b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
36153364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3616b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
361757a21abaSAlan Cox 			 stack_entry->offset,
3618b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
3619ef694c1aSEdward Tomasz Napierala 			 (vm_size_t)grow_amount, cred != NULL)) {
362008667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3621b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3622b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3623199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
36240164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3625b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3626b21a0008SMarcel Moolenaar 
3627b21a0008SMarcel Moolenaar 			if (next_entry != &map->header)
3628b21a0008SMarcel Moolenaar 				vm_map_clip_start(map, next_entry, addr);
3629b21a0008SMarcel Moolenaar 		} else
3630b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3631b21a0008SMarcel Moolenaar 	}
3632b21a0008SMarcel Moolenaar 
3633b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3634b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3635b21a0008SMarcel Moolenaar 
363694f7e29aSAlan Cox 	vm_map_unlock(map);
3637b21a0008SMarcel Moolenaar 
3638abd498aaSBruce M Simpson 	/*
3639abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3640abd498aaSBruce M Simpson 	 */
3641b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3642b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3643b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3644b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3645b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3646b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3647b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3648b21a0008SMarcel Moolenaar 	}
3649abd498aaSBruce M Simpson 
36501ba5ad42SEdward Tomasz Napierala out:
3651afcc55f3SEdward Tomasz Napierala #ifdef RACCT
36521ba5ad42SEdward Tomasz Napierala 	if (rv != KERN_SUCCESS) {
36531ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
36541ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
36551ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
36567e19eda4SAndrey Zonov 		if (!old_mlock) {
36577e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
36583ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
36597e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
36607e19eda4SAndrey Zonov 		}
36611ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
36621ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
36631ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
36641ba5ad42SEdward Tomasz Napierala 	}
3665afcc55f3SEdward Tomasz Napierala #endif
36661ba5ad42SEdward Tomasz Napierala 
36670cddd8f0SMatthew Dillon 	return (rv);
366894f7e29aSAlan Cox }
366994f7e29aSAlan Cox 
3670df8bae1dSRodney W. Grimes /*
36715856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
36725856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
36735856e12eSJohn Dyson  */
367489b57fcfSKonstantin Belousov int
36753ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
36761b40f8c0SMatthew Dillon {
36775856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
36785856e12eSJohn Dyson 	struct vmspace *newvmspace;
36795856e12eSJohn Dyson 
36803ebc1248SPeter Wemm 	newvmspace = vmspace_alloc(minuser, maxuser);
368189b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
368289b57fcfSKonstantin Belousov 		return (ENOMEM);
368351ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
36845856e12eSJohn Dyson 	/*
36855856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
36865856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
36875856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
36885856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
36895856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
36905856e12eSJohn Dyson 	 */
369157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
36925856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
369357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
36946617724cSJeff Roberson 	if (p == curthread->td_proc)
3695b40ce416SJulian Elischer 		pmap_activate(curthread);
3696b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
369789b57fcfSKonstantin Belousov 	return (0);
36985856e12eSJohn Dyson }
36995856e12eSJohn Dyson 
37005856e12eSJohn Dyson /*
37015856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
37025856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
37035856e12eSJohn Dyson  */
370489b57fcfSKonstantin Belousov int
37051b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
37061b40f8c0SMatthew Dillon {
37075856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
37085856e12eSJohn Dyson 	struct vmspace *newvmspace;
37093364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
37105856e12eSJohn Dyson 
37115856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
371289b57fcfSKonstantin Belousov 		return (0);
37133364c323SKonstantin Belousov 	fork_charge = 0;
37143364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
371589b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
371689b57fcfSKonstantin Belousov 		return (ENOMEM);
3717ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
37183364c323SKonstantin Belousov 		vmspace_free(newvmspace);
37193364c323SKonstantin Belousov 		return (ENOMEM);
37203364c323SKonstantin Belousov 	}
372157051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
37225856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
372357051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37246617724cSJeff Roberson 	if (p == curthread->td_proc)
3725b40ce416SJulian Elischer 		pmap_activate(curthread);
3726b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
372789b57fcfSKonstantin Belousov 	return (0);
37285856e12eSJohn Dyson }
37295856e12eSJohn Dyson 
37305856e12eSJohn Dyson /*
3731df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3732df8bae1dSRodney W. Grimes  *
3733df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3734df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3735df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3736df8bae1dSRodney W. Grimes  *	type specified.
3737df8bae1dSRodney W. Grimes  *
3738df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3739df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3740df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3741df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3742df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3743df8bae1dSRodney W. Grimes  *
3744df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3745df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3746df8bae1dSRodney W. Grimes  *
3747df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3748df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3749df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3750df8bae1dSRodney W. Grimes  *	remain the same.
3751df8bae1dSRodney W. Grimes  */
3752df8bae1dSRodney W. Grimes int
3753b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3754b9dcd593SBruce Evans 	      vm_offset_t vaddr,
375547221757SJohn Dyson 	      vm_prot_t fault_typea,
3756b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3757b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3758b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3759b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
37602d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3761df8bae1dSRodney W. Grimes {
3762c0877f10SJohn Dyson 	vm_map_entry_t entry;
3763c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3764c0877f10SJohn Dyson 	vm_prot_t prot;
376547221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
37663364c323SKonstantin Belousov 	vm_object_t eobject;
37670cc74f14SAlan Cox 	vm_size_t size;
3768ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
3769df8bae1dSRodney W. Grimes 
3770df8bae1dSRodney W. Grimes RetryLookup:;
3771df8bae1dSRodney W. Grimes 
3772df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3773df8bae1dSRodney W. Grimes 
3774df8bae1dSRodney W. Grimes 	/*
37754c3ef59eSAlan Cox 	 * Lookup the faulting address.
3776df8bae1dSRodney W. Grimes 	 */
3777095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3778095104acSAlan Cox 		vm_map_unlock_read(map);
3779095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3780095104acSAlan Cox 	}
3781df8bae1dSRodney W. Grimes 
37824e94f402SAlan Cox 	entry = *out_entry;
3783b7b2aac2SJohn Dyson 
3784df8bae1dSRodney W. Grimes 	/*
3785df8bae1dSRodney W. Grimes 	 * Handle submaps.
3786df8bae1dSRodney W. Grimes 	 */
3787afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3788df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3789df8bae1dSRodney W. Grimes 
3790df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3791df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3792df8bae1dSRodney W. Grimes 		goto RetryLookup;
3793df8bae1dSRodney W. Grimes 	}
3794a04c970aSJohn Dyson 
3795df8bae1dSRodney W. Grimes 	/*
37960d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3797df8bae1dSRodney W. Grimes 	 */
3798df8bae1dSRodney W. Grimes 	prot = entry->protection;
379947221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
38002db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3801095104acSAlan Cox 		vm_map_unlock_read(map);
3802095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
380347221757SJohn Dyson 	}
38042ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
380547221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3806a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3807095104acSAlan Cox 		vm_map_unlock_read(map);
3808095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3809a04c970aSJohn Dyson 	}
3810*5b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
3811*5b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
3812*5b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
3813*5b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
3814*5b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
3815*5b3e0257SDag-Erling Smørgrav 	}
3816df8bae1dSRodney W. Grimes 
3817df8bae1dSRodney W. Grimes 	/*
38180d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
38190d94caffSDavid Greenman 	 * accesses.
3820df8bae1dSRodney W. Grimes 	 */
382105f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
382205f0fdd2SPoul-Henning Kamp 	if (*wired)
3823a6d42a0dSAlan Cox 		fault_type = entry->protection;
38243364c323SKonstantin Belousov 	size = entry->end - entry->start;
3825df8bae1dSRodney W. Grimes 	/*
3826df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3827df8bae1dSRodney W. Grimes 	 */
3828afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
3829df8bae1dSRodney W. Grimes 		/*
38300d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
3831ad5fca3bSAlan Cox 		 * now since we've got the map locked.
3832df8bae1dSRodney W. Grimes 		 *
38330d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
38340d94caffSDavid Greenman 		 * permissions allowed.
3835df8bae1dSRodney W. Grimes 		 */
3836a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
3837a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
3838df8bae1dSRodney W. Grimes 			/*
38390d94caffSDavid Greenman 			 * Make a new object, and place it in the object
38400d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
3841ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
38420d94caffSDavid Greenman 			 * object.
3843df8bae1dSRodney W. Grimes 			 */
384425adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
3845df8bae1dSRodney W. Grimes 				goto RetryLookup;
38469917e010SAlan Cox 
3847ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
38483364c323SKonstantin Belousov 				/*
38493364c323SKonstantin Belousov 				 * The debugger owner is charged for
38503364c323SKonstantin Belousov 				 * the memory.
38513364c323SKonstantin Belousov 				 */
3852ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3853ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3854ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
3855ef694c1aSEdward Tomasz Napierala 					crfree(cred);
38563364c323SKonstantin Belousov 					vm_map_unlock(map);
38573364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
38583364c323SKonstantin Belousov 				}
3859ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
38603364c323SKonstantin Belousov 			}
38610cc74f14SAlan Cox 			vm_object_shadow(&entry->object.vm_object,
38620cc74f14SAlan Cox 			    &entry->offset, size);
3863afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
38643364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
3865ef694c1aSEdward Tomasz Napierala 			if (eobject->cred != NULL) {
38663364c323SKonstantin Belousov 				/*
38673364c323SKonstantin Belousov 				 * The object was not shadowed.
38683364c323SKonstantin Belousov 				 */
3869ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
3870ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
3871ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
3872ef694c1aSEdward Tomasz Napierala 			} else if (entry->cred != NULL) {
387389f6b863SAttilio Rao 				VM_OBJECT_WLOCK(eobject);
3874ef694c1aSEdward Tomasz Napierala 				eobject->cred = entry->cred;
38753364c323SKonstantin Belousov 				eobject->charge = size;
387689f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(eobject);
3877ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
38783364c323SKonstantin Belousov 			}
38799917e010SAlan Cox 
38809b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
38810d94caffSDavid Greenman 		} else {
3882df8bae1dSRodney W. Grimes 			/*
38830d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
38840d94caffSDavid Greenman 			 * don't allow writes.
3885df8bae1dSRodney W. Grimes 			 */
38862d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
3887df8bae1dSRodney W. Grimes 		}
3888df8bae1dSRodney W. Grimes 	}
38892d8acc0fSJohn Dyson 
3890df8bae1dSRodney W. Grimes 	/*
3891df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
3892df8bae1dSRodney W. Grimes 	 */
38934e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
38944e71e795SMatthew Dillon 	    !map->system_map) {
389525adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
3896df8bae1dSRodney W. Grimes 			goto RetryLookup;
389724a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
38983364c323SKonstantin Belousov 		    atop(size));
3899df8bae1dSRodney W. Grimes 		entry->offset = 0;
3900ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
390189f6b863SAttilio Rao 			VM_OBJECT_WLOCK(entry->object.vm_object);
3902ef694c1aSEdward Tomasz Napierala 			entry->object.vm_object->cred = entry->cred;
39033364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
390489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
3905ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
39063364c323SKonstantin Belousov 		}
39079b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
3908df8bae1dSRodney W. Grimes 	}
3909b5b40fa6SJohn Dyson 
3910df8bae1dSRodney W. Grimes 	/*
39110d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
39120d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
3913df8bae1dSRodney W. Grimes 	 */
39149b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
3915df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
3916df8bae1dSRodney W. Grimes 
3917df8bae1dSRodney W. Grimes 	*out_prot = prot;
3918df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3919df8bae1dSRodney W. Grimes }
3920df8bae1dSRodney W. Grimes 
3921df8bae1dSRodney W. Grimes /*
392219dc5607STor Egge  *	vm_map_lookup_locked:
392319dc5607STor Egge  *
392419dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
392519dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
392619dc5607STor Egge  */
392719dc5607STor Egge int
392819dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
392919dc5607STor Egge 		     vm_offset_t vaddr,
393019dc5607STor Egge 		     vm_prot_t fault_typea,
393119dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
393219dc5607STor Egge 		     vm_object_t *object,	/* OUT */
393319dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
393419dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
393519dc5607STor Egge 		     boolean_t *wired)		/* OUT */
393619dc5607STor Egge {
393719dc5607STor Egge 	vm_map_entry_t entry;
393819dc5607STor Egge 	vm_map_t map = *var_map;
393919dc5607STor Egge 	vm_prot_t prot;
394019dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
394119dc5607STor Egge 
394219dc5607STor Egge 	/*
39434c3ef59eSAlan Cox 	 * Lookup the faulting address.
394419dc5607STor Egge 	 */
394519dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
394619dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
394719dc5607STor Egge 
394819dc5607STor Egge 	entry = *out_entry;
394919dc5607STor Egge 
395019dc5607STor Egge 	/*
395119dc5607STor Egge 	 * Fail if the entry refers to a submap.
395219dc5607STor Egge 	 */
395319dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
395419dc5607STor Egge 		return (KERN_FAILURE);
395519dc5607STor Egge 
395619dc5607STor Egge 	/*
395719dc5607STor Egge 	 * Check whether this task is allowed to have this page.
395819dc5607STor Egge 	 */
395919dc5607STor Egge 	prot = entry->protection;
396019dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
396119dc5607STor Egge 	if ((fault_type & prot) != fault_type)
396219dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
396319dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
396419dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
3965a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
396619dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
396719dc5607STor Egge 
396819dc5607STor Egge 	/*
396919dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
397019dc5607STor Egge 	 * accesses.
397119dc5607STor Egge 	 */
397219dc5607STor Egge 	*wired = (entry->wired_count != 0);
397319dc5607STor Egge 	if (*wired)
3974a6d42a0dSAlan Cox 		fault_type = entry->protection;
397519dc5607STor Egge 
397619dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
397719dc5607STor Egge 		/*
397819dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
397919dc5607STor Egge 		 */
398019dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
398119dc5607STor Egge 			return (KERN_FAILURE);
398219dc5607STor Egge 		/*
398319dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
398419dc5607STor Egge 		 * don't allow writes.
398519dc5607STor Egge 		 */
398619dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
398719dc5607STor Egge 	}
398819dc5607STor Egge 
398919dc5607STor Egge 	/*
399019dc5607STor Egge 	 * Fail if an object should be created.
399119dc5607STor Egge 	 */
399219dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
399319dc5607STor Egge 		return (KERN_FAILURE);
399419dc5607STor Egge 
399519dc5607STor Egge 	/*
399619dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
399719dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
399819dc5607STor Egge 	 */
399919dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
400019dc5607STor Egge 	*object = entry->object.vm_object;
400119dc5607STor Egge 
400219dc5607STor Egge 	*out_prot = prot;
400319dc5607STor Egge 	return (KERN_SUCCESS);
400419dc5607STor Egge }
400519dc5607STor Egge 
400619dc5607STor Egge /*
4007df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
4008df8bae1dSRodney W. Grimes  *
4009df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
4010df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
4011df8bae1dSRodney W. Grimes  */
40120d94caffSDavid Greenman void
40131b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4014df8bae1dSRodney W. Grimes {
4015df8bae1dSRodney W. Grimes 	/*
4016df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
4017df8bae1dSRodney W. Grimes 	 */
4018df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
4019df8bae1dSRodney W. Grimes }
4020df8bae1dSRodney W. Grimes 
4021c7c34a24SBruce Evans #include "opt_ddb.h"
4022c3cb3e12SDavid Greenman #ifdef DDB
4023c7c34a24SBruce Evans #include <sys/kernel.h>
4024c7c34a24SBruce Evans 
4025c7c34a24SBruce Evans #include <ddb/ddb.h>
4026c7c34a24SBruce Evans 
40272ebcd458SAttilio Rao static void
40282ebcd458SAttilio Rao vm_map_print(vm_map_t map)
4029df8bae1dSRodney W. Grimes {
4030c0877f10SJohn Dyson 	vm_map_entry_t entry;
4031c7c34a24SBruce Evans 
4032e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4033e5f251d2SAlan Cox 	    (void *)map,
4034101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
4035df8bae1dSRodney W. Grimes 
4036c7c34a24SBruce Evans 	db_indent += 2;
4037df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
4038df8bae1dSRodney W. Grimes 	    entry = entry->next) {
4039fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
4040fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
4041e5f251d2SAlan Cox 		{
4042df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
4043df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
40440d94caffSDavid Greenman 
404595e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
4046df8bae1dSRodney W. Grimes 			    entry->protection,
4047df8bae1dSRodney W. Grimes 			    entry->max_protection,
40488aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4049df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
405095e5e988SJohn Dyson 				db_printf(", wired");
4051df8bae1dSRodney W. Grimes 		}
40529fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4053cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
40549fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
4055cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4056df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
40579fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
40589fdfe602SMatthew Dillon 				entry->object.sub_map)) {
4059c7c34a24SBruce Evans 				db_indent += 2;
40602ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
4061c7c34a24SBruce Evans 				db_indent -= 2;
4062df8bae1dSRodney W. Grimes 			}
40630d94caffSDavid Greenman 		} else {
4064ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
4065ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
4066cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
4067101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
4068cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4069ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
4070ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
4071ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
40723364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
4073afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
4074c7c34a24SBruce Evans 				db_printf(", copy (%s)",
4075afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4076c7c34a24SBruce Evans 			db_printf("\n");
4077df8bae1dSRodney W. Grimes 
4078df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
4079df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
4080df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
4081c7c34a24SBruce Evans 				db_indent += 2;
4082101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
4083101eeb7fSBruce Evans 						entry->object.vm_object,
40842ebcd458SAttilio Rao 						1, 0, (char *)0);
4085c7c34a24SBruce Evans 				db_indent -= 2;
4086df8bae1dSRodney W. Grimes 			}
4087df8bae1dSRodney W. Grimes 		}
4088df8bae1dSRodney W. Grimes 	}
4089c7c34a24SBruce Evans 	db_indent -= 2;
4090df8bae1dSRodney W. Grimes }
409195e5e988SJohn Dyson 
40922ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
40932ebcd458SAttilio Rao {
40942ebcd458SAttilio Rao 
40952ebcd458SAttilio Rao 	if (!have_addr) {
40962ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
40972ebcd458SAttilio Rao 		return;
40982ebcd458SAttilio Rao 	}
40992ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
41002ebcd458SAttilio Rao }
410195e5e988SJohn Dyson 
410295e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
410395e5e988SJohn Dyson {
410495e5e988SJohn Dyson 	struct proc *p;
410595e5e988SJohn Dyson 
410695e5e988SJohn Dyson 	if (have_addr) {
410795e5e988SJohn Dyson 		p = (struct proc *) addr;
410895e5e988SJohn Dyson 	} else {
410995e5e988SJohn Dyson 		p = curproc;
411095e5e988SJohn Dyson 	}
411195e5e988SJohn Dyson 
4112ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4113ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4114b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
411595e5e988SJohn Dyson 
41162ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
411795e5e988SJohn Dyson }
411895e5e988SJohn Dyson 
4119c7c34a24SBruce Evans #endif /* DDB */
4120