xref: /freebsd/sys/vm/vm_map.c (revision 4b5c9cf62f33d8359a272dda846930aaa7c635f8)
160727d8bSWarner Losh /*-
2df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
3df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
4df8bae1dSRodney W. Grimes  *
5df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
6df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
7df8bae1dSRodney W. Grimes  *
8df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
9df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
10df8bae1dSRodney W. Grimes  * are met:
11df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
12df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
13df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
15df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
16df8bae1dSRodney W. Grimes  * 4. Neither the name of the University nor the names of its contributors
17df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
18df8bae1dSRodney W. Grimes  *    without specific prior written permission.
19df8bae1dSRodney W. Grimes  *
20df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
31df8bae1dSRodney W. Grimes  *
323c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
33df8bae1dSRodney W. Grimes  *
34df8bae1dSRodney W. Grimes  *
35df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
36df8bae1dSRodney W. Grimes  * All rights reserved.
37df8bae1dSRodney W. Grimes  *
38df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
41df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
42df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
43df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
44df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
45df8bae1dSRodney W. Grimes  *
46df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
47df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
48df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
49df8bae1dSRodney W. Grimes  *
50df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
53df8bae1dSRodney W. Grimes  *  School of Computer Science
54df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
55df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
56df8bae1dSRodney W. Grimes  *
57df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
58df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
59df8bae1dSRodney W. Grimes  */
60df8bae1dSRodney W. Grimes 
61df8bae1dSRodney W. Grimes /*
62df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
63df8bae1dSRodney W. Grimes  */
64df8bae1dSRodney W. Grimes 
65874651b1SDavid E. O'Brien #include <sys/cdefs.h>
66874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
67874651b1SDavid E. O'Brien 
68df8bae1dSRodney W. Grimes #include <sys/param.h>
69df8bae1dSRodney W. Grimes #include <sys/systm.h>
709a6d144fSKonstantin Belousov #include <sys/kernel.h>
7161d80e90SJohn Baldwin #include <sys/ktr.h>
72fb919e4dSMark Murray #include <sys/lock.h>
73fb919e4dSMark Murray #include <sys/mutex.h>
74b5e8ce9fSBruce Evans #include <sys/proc.h>
75efeaf95aSDavid Greenman #include <sys/vmmeter.h>
76867a482dSJohn Dyson #include <sys/mman.h>
771efb74fbSJohn Dyson #include <sys/vnode.h>
781ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
792267af78SJulian Elischer #include <sys/resourcevar.h>
8089f6b863SAttilio Rao #include <sys/rwlock.h>
813fde38dfSMike Silbersack #include <sys/file.h>
829a6d144fSKonstantin Belousov #include <sys/sysctl.h>
8305ba50f5SJake Burkholder #include <sys/sysent.h>
843db161e0SMatthew Dillon #include <sys/shm.h>
85df8bae1dSRodney W. Grimes 
86df8bae1dSRodney W. Grimes #include <vm/vm.h>
87efeaf95aSDavid Greenman #include <vm/vm_param.h>
88efeaf95aSDavid Greenman #include <vm/pmap.h>
89efeaf95aSDavid Greenman #include <vm/vm_map.h>
90df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
91df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9247221757SJohn Dyson #include <vm/vm_pager.h>
9326f9a767SRodney W. Grimes #include <vm/vm_kern.h>
94efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9584110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
9621cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
97670d17b5SJeff Roberson #include <vm/uma.h>
98df8bae1dSRodney W. Grimes 
99df8bae1dSRodney W. Grimes /*
100df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
101df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
102df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
103df8bae1dSRodney W. Grimes  *	memory from one map to another.
104df8bae1dSRodney W. Grimes  *
105df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
108e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
109e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
110df8bae1dSRodney W. Grimes  *
111956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
112df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
113df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
114df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
115df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
116df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
117df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
118df8bae1dSRodney W. Grimes  *
119df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
120ad5fca3bSAlan Cox  *	by copying VM object references from one map to
121df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
122df8bae1dSRodney W. Grimes  */
123df8bae1dSRodney W. Grimes 
1243a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1258355f576SJeff Roberson static uma_zone_t mapentzone;
1268355f576SJeff Roberson static uma_zone_t kmapentzone;
1278355f576SJeff Roberson static uma_zone_t mapzone;
1288355f576SJeff Roberson static uma_zone_t vmspace_zone;
129b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
130b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
13192351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13292351f16SAlan Cox     vm_offset_t max);
1330b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
134655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
13503462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
136077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
137077ec27cSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
1388355f576SJeff Roberson #ifdef INVARIANTS
1398355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1408355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1418355f576SJeff Roberson #endif
1424648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
1434648ba0aSKonstantin Belousov     vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
1444648ba0aSKonstantin Belousov     int cow);
14566cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
14666cd575bSAlan Cox     vm_offset_t failed_addr);
147b18bfc3dSJohn Dyson 
148ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
149ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1503364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1513364c323SKonstantin Belousov 
15257051fdcSTor Egge /*
15357051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
15457051fdcSTor Egge  * stable.
15557051fdcSTor Egge  */
15657051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
15757051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
15857051fdcSTor Egge 
159d239bd3cSKonstantin Belousov /*
160d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
161d239bd3cSKonstantin Belousov  *
162d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
163d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
164d239bd3cSKonstantin Belousov  */
165d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
166d239bd3cSKonstantin Belousov 		{					\
167d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
168d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
169d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
170d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
171d239bd3cSKonstantin Belousov 		if (start > end)			\
172d239bd3cSKonstantin Belousov 			start = end;			\
173d239bd3cSKonstantin Belousov 		}
174d239bd3cSKonstantin Belousov 
1756fecb26bSKonstantin Belousov /*
1766fecb26bSKonstantin Belousov  *	vm_map_startup:
1776fecb26bSKonstantin Belousov  *
1786fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1796fecb26bSKonstantin Belousov  *	any other vm_map routines.
1806fecb26bSKonstantin Belousov  *
1816fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1826fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1836fecb26bSKonstantin Belousov  *
1846fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1856fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1866fecb26bSKonstantin Belousov  *
1876fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1886fecb26bSKonstantin Belousov  *	maps and requires map entries.
1896fecb26bSKonstantin Belousov  */
1906fecb26bSKonstantin Belousov 
1910d94caffSDavid Greenman void
1921b40f8c0SMatthew Dillon vm_map_startup(void)
193df8bae1dSRodney W. Grimes {
1943a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
1958355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
1968355f576SJeff Roberson #ifdef INVARIANTS
1978355f576SJeff Roberson 	    vm_map_zdtor,
1988355f576SJeff Roberson #else
1998355f576SJeff Roberson 	    NULL,
2008355f576SJeff Roberson #endif
201f872f6eaSAlan Cox 	    vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2028355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
203670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
20418aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
20518aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
206670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
207670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2085df87b21SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2095df87b21SJeff Roberson #ifdef INVARIANTS
2105df87b21SJeff Roberson 	    vmspace_zdtor,
2115df87b21SJeff Roberson #else
2125df87b21SJeff Roberson 	    NULL,
2135df87b21SJeff Roberson #endif
214f872f6eaSAlan Cox 	    vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2158355f576SJeff Roberson }
2168355f576SJeff Roberson 
217b23f72e9SBrian Feldman static int
218b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2198355f576SJeff Roberson {
2208355f576SJeff Roberson 	struct vmspace *vm;
2218355f576SJeff Roberson 
2228355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2238355f576SJeff Roberson 
22489b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
225b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
226e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(vmspace_pmap(vm));
227b23f72e9SBrian Feldman 	return (0);
2288355f576SJeff Roberson }
2298355f576SJeff Roberson 
230b23f72e9SBrian Feldman static int
231b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2328355f576SJeff Roberson {
2338355f576SJeff Roberson 	vm_map_t map;
2348355f576SJeff Roberson 
2358355f576SJeff Roberson 	map = (vm_map_t)mem;
236763d9566STim Kientzle 	memset(map, 0, sizeof(*map));
237e30df26eSAlan Cox 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
238e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
239b23f72e9SBrian Feldman 	return (0);
2408355f576SJeff Roberson }
2418355f576SJeff Roberson 
2428355f576SJeff Roberson #ifdef INVARIANTS
2438355f576SJeff Roberson static void
2448355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2458355f576SJeff Roberson {
2468355f576SJeff Roberson 	struct vmspace *vm;
2478355f576SJeff Roberson 
2488355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2498355f576SJeff Roberson 
2508355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2518355f576SJeff Roberson }
2528355f576SJeff Roberson static void
2538355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2548355f576SJeff Roberson {
2558355f576SJeff Roberson 	vm_map_t map;
2568355f576SJeff Roberson 
2578355f576SJeff Roberson 	map = (vm_map_t)mem;
2588355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2598355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2608355f576SJeff Roberson 	    map, map->nentries));
2618355f576SJeff Roberson 	KASSERT(map->size == 0,
2628355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2639eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2648355f576SJeff Roberson }
2658355f576SJeff Roberson #endif	/* INVARIANTS */
2668355f576SJeff Roberson 
267df8bae1dSRodney W. Grimes /*
268df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
269df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
27074d1d2b7SNeel Natu  *
27174d1d2b7SNeel Natu  * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
272df8bae1dSRodney W. Grimes  */
273df8bae1dSRodney W. Grimes struct vmspace *
27474d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
275df8bae1dSRodney W. Grimes {
276c0877f10SJohn Dyson 	struct vmspace *vm;
2770d94caffSDavid Greenman 
278a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
27974d1d2b7SNeel Natu 
28074d1d2b7SNeel Natu 	KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
28174d1d2b7SNeel Natu 
28274d1d2b7SNeel Natu 	if (pinit == NULL)
28374d1d2b7SNeel Natu 		pinit = &pmap_pinit;
28474d1d2b7SNeel Natu 
28574d1d2b7SNeel Natu 	if (!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 
303*4b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3041ba5ad42SEdward Tomasz Napierala static void
3051ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3061ba5ad42SEdward Tomasz Napierala {
3071ba5ad42SEdward Tomasz Napierala 
3081ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3091ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3101ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3111ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3121ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3131ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3141ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3151ba5ad42SEdward Tomasz Napierala }
316*4b5c9cf6SEdward Tomasz Napierala #endif
3171ba5ad42SEdward Tomasz Napierala 
31862a59e8fSWarner Losh static inline void
319582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
320df8bae1dSRodney W. Grimes {
3210ef12795SAlan Cox 
32221c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3233db161e0SMatthew Dillon 
3243db161e0SMatthew Dillon 	/*
3253db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3263db161e0SMatthew Dillon 	 * exit1().
3273db161e0SMatthew Dillon 	 */
3283db161e0SMatthew Dillon 	shmexit(vm);
3293db161e0SMatthew Dillon 
33030dcfc09SJohn Dyson 	/*
331df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3320d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3330d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
334df8bae1dSRodney W. Grimes 	 */
335717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
336df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3378355f576SJeff Roberson 
3380ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3390ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3408355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
341df8bae1dSRodney W. Grimes }
342582ec34cSAlfred Perlstein 
343582ec34cSAlfred Perlstein void
344582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
345582ec34cSAlfred Perlstein {
346582ec34cSAlfred Perlstein 
347423521aaSRyan Stone 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
348423521aaSRyan Stone 	    "vmspace_free() called with non-sleepable lock held");
349423521aaSRyan Stone 
350582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
351582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
352582ec34cSAlfred Perlstein 
3531a587ef2SJohn Baldwin 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
354582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
355582ec34cSAlfred Perlstein }
356582ec34cSAlfred Perlstein 
357582ec34cSAlfred Perlstein void
358582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
359582ec34cSAlfred Perlstein {
360334f7061SPeter Wemm 	struct vmspace *vm;
361582ec34cSAlfred Perlstein 
36257051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
363334f7061SPeter Wemm 	vm = p->p_vmspace;
364334f7061SPeter Wemm 	p->p_vmspace = NULL;
36557051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
36657051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
36757051fdcSTor Egge 	vmspace_free(vm);
36857051fdcSTor Egge }
36957051fdcSTor Egge 
37057051fdcSTor Egge void
37157051fdcSTor Egge vmspace_exit(struct thread *td)
37257051fdcSTor Egge {
37357051fdcSTor Egge 	int refcnt;
37457051fdcSTor Egge 	struct vmspace *vm;
37557051fdcSTor Egge 	struct proc *p;
376389d2b6eSMatthew Dillon 
377389d2b6eSMatthew Dillon 	/*
37857051fdcSTor Egge 	 * Release user portion of address space.
37957051fdcSTor Egge 	 * This releases references to vnodes,
38057051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
38157051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
382389d2b6eSMatthew Dillon 	 *
38357051fdcSTor Egge 	 * The last exiting process to reach this point releases as
38457051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
38557051fdcSTor Egge 	 * slower fallback in case another process had a temporary
38657051fdcSTor Egge 	 * reference to the vmspace.
387389d2b6eSMatthew Dillon 	 */
38857051fdcSTor Egge 
38957051fdcSTor Egge 	p = td->td_proc;
39057051fdcSTor Egge 	vm = p->p_vmspace;
39157051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
39257051fdcSTor Egge 	do {
39357051fdcSTor Egge 		refcnt = vm->vm_refcnt;
39457051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
39557051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
39657051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
39757051fdcSTor Egge 			p->p_vmspace = &vmspace0;
39857051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
39957051fdcSTor Egge 			pmap_activate(td);
40057051fdcSTor Egge 		}
40157051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
40257051fdcSTor Egge 	if (refcnt == 1) {
40357051fdcSTor Egge 		if (p->p_vmspace != vm) {
40457051fdcSTor Egge 			/* vmspace not yet freed, switch back */
40557051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40657051fdcSTor Egge 			p->p_vmspace = vm;
40757051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40857051fdcSTor Egge 			pmap_activate(td);
40957051fdcSTor Egge 		}
41057051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
41157051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
41257051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
41357051fdcSTor Egge 		p->p_vmspace = &vmspace0;
41457051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
41557051fdcSTor Egge 		pmap_activate(td);
416334f7061SPeter Wemm 		vmspace_dofree(vm);
417334f7061SPeter Wemm 	}
418*4b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
419*4b5c9cf6SEdward Tomasz Napierala 	if (racct_enable)
4201ba5ad42SEdward Tomasz Napierala 		vmspace_container_reset(p);
421*4b5c9cf6SEdward Tomasz Napierala #endif
42257051fdcSTor Egge }
42357051fdcSTor Egge 
42457051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
42557051fdcSTor Egge 
42657051fdcSTor Egge struct vmspace *
42757051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
42857051fdcSTor Egge {
42957051fdcSTor Egge 	struct vmspace *vm;
43057051fdcSTor Egge 	int refcnt;
43157051fdcSTor Egge 
43257051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
43357051fdcSTor Egge 	vm = p->p_vmspace;
43457051fdcSTor Egge 	if (vm == NULL) {
43557051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
43657051fdcSTor Egge 		return (NULL);
43757051fdcSTor Egge 	}
43857051fdcSTor Egge 	do {
43957051fdcSTor Egge 		refcnt = vm->vm_refcnt;
44057051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
44157051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
44257051fdcSTor Egge 			return (NULL);
44357051fdcSTor Egge 		}
44457051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
44557051fdcSTor Egge 	if (vm != p->p_vmspace) {
44657051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
44757051fdcSTor Egge 		vmspace_free(vm);
44857051fdcSTor Egge 		return (NULL);
44957051fdcSTor Egge 	}
45057051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
45157051fdcSTor Egge 	return (vm);
45257051fdcSTor Egge }
453df8bae1dSRodney W. Grimes 
4541b40f8c0SMatthew Dillon void
455780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
4561b40f8c0SMatthew Dillon {
457bc91c510SAlan Cox 
45893bc4879SAlan Cox 	if (map->system_map)
459ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
46012c64974SMaxime Henrion 	else
4619fde98bbSAttilio Rao 		sx_xlock_(&map->lock, file, line);
4621b40f8c0SMatthew Dillon 	map->timestamp++;
4631b40f8c0SMatthew Dillon }
4641b40f8c0SMatthew Dillon 
4650b367bd8SKonstantin Belousov static void
4660b367bd8SKonstantin Belousov vm_map_process_deferred(void)
4670e0af8ecSBrian Feldman {
4680b367bd8SKonstantin Belousov 	struct thread *td;
4696fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
47084110e7eSKonstantin Belousov 	vm_object_t object;
471655c3490SKonstantin Belousov 
4720b367bd8SKonstantin Belousov 	td = curthread;
4736fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
4746fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
4756fbe60faSJohn Baldwin 	while (entry != NULL) {
4766fbe60faSJohn Baldwin 		next = entry->next;
47784110e7eSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
47884110e7eSKonstantin Belousov 			/*
47984110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
48084110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
48184110e7eSKonstantin Belousov 			 */
48284110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
48384110e7eSKonstantin Belousov 			    ("Submap with writecount"));
48484110e7eSKonstantin Belousov 			object = entry->object.vm_object;
48584110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
48684110e7eSKonstantin Belousov 			vnode_pager_release_writecount(object, entry->start,
48784110e7eSKonstantin Belousov 			    entry->end);
48884110e7eSKonstantin Belousov 		}
4890b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
4906fbe60faSJohn Baldwin 		entry = next;
4910b367bd8SKonstantin Belousov 	}
4920b367bd8SKonstantin Belousov }
4930b367bd8SKonstantin Belousov 
4940b367bd8SKonstantin Belousov void
4950b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
4960b367bd8SKonstantin Belousov {
4970b367bd8SKonstantin Belousov 
4980b367bd8SKonstantin Belousov 	if (map->system_map)
499ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5000b367bd8SKonstantin Belousov 	else {
5019fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
5020b367bd8SKonstantin Belousov 		vm_map_process_deferred();
503655c3490SKonstantin Belousov 	}
5040e0af8ecSBrian Feldman }
5050e0af8ecSBrian Feldman 
5060e0af8ecSBrian Feldman void
507780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
5080e0af8ecSBrian Feldman {
509bc91c510SAlan Cox 
51093bc4879SAlan Cox 	if (map->system_map)
511ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
51212c64974SMaxime Henrion 	else
5139fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
51436daaecdSAlan Cox }
5150e0af8ecSBrian Feldman 
5160e0af8ecSBrian Feldman void
517780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
5180e0af8ecSBrian Feldman {
519bc91c510SAlan Cox 
52036daaecdSAlan Cox 	if (map->system_map)
521ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5220b367bd8SKonstantin Belousov 	else {
5239fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
5240b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5250b367bd8SKonstantin Belousov 	}
52625adb370SBrian Feldman }
52725adb370SBrian Feldman 
528d974f03cSAlan Cox int
529780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
530d974f03cSAlan Cox {
53125adb370SBrian Feldman 	int error;
53225adb370SBrian Feldman 
53336daaecdSAlan Cox 	error = map->system_map ?
534ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5359fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
5363a92e5d5SAlan Cox 	if (error == 0)
5373a92e5d5SAlan Cox 		map->timestamp++;
538bc91c510SAlan Cox 	return (error == 0);
5390e0af8ecSBrian Feldman }
5400e0af8ecSBrian Feldman 
5410e0af8ecSBrian Feldman int
54272d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
54372d97679SDavid Schultz {
54472d97679SDavid Schultz 	int error;
54572d97679SDavid Schultz 
54672d97679SDavid Schultz 	error = map->system_map ?
547ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5489fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
54972d97679SDavid Schultz 	return (error == 0);
55072d97679SDavid Schultz }
55172d97679SDavid Schultz 
55205a8c414SAlan Cox /*
55305a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
55405a8c414SAlan Cox  *
55505a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
55605a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
55705a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
55805a8c414SAlan Cox  *	returned without a read or write lock held.
55905a8c414SAlan Cox  *
56005a8c414SAlan Cox  *	Requires that the map be read locked.
56105a8c414SAlan Cox  */
56272d97679SDavid Schultz int
563780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
5640e0af8ecSBrian Feldman {
56505a8c414SAlan Cox 	unsigned int last_timestamp;
566bc91c510SAlan Cox 
56712c64974SMaxime Henrion 	if (map->system_map) {
568ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
56905a8c414SAlan Cox 	} else {
5709fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
57105a8c414SAlan Cox 			last_timestamp = map->timestamp;
5729fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
5730b367bd8SKonstantin Belousov 			vm_map_process_deferred();
57405a8c414SAlan Cox 			/*
57505a8c414SAlan Cox 			 * If the map's timestamp does not change while the
57605a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
57705a8c414SAlan Cox 			 */
5789fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
57905a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
5809fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
58105a8c414SAlan Cox 				return (1);
58205a8c414SAlan Cox 			}
58305a8c414SAlan Cox 		}
58405a8c414SAlan Cox 	}
585bc91c510SAlan Cox 	map->timestamp++;
586bc91c510SAlan Cox 	return (0);
5870e0af8ecSBrian Feldman }
5880e0af8ecSBrian Feldman 
5890e0af8ecSBrian Feldman void
590780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
5911b40f8c0SMatthew Dillon {
592bc91c510SAlan Cox 
59312c64974SMaxime Henrion 	if (map->system_map) {
594ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
59505a8c414SAlan Cox 	} else
5969fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
59705a8c414SAlan Cox }
59805a8c414SAlan Cox 
59905a8c414SAlan Cox /*
60005a8c414SAlan Cox  *	vm_map_locked:
60105a8c414SAlan Cox  *
60205a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
60305a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
60405a8c414SAlan Cox  */
60505a8c414SAlan Cox int
60605a8c414SAlan Cox vm_map_locked(vm_map_t map)
60705a8c414SAlan Cox {
60805a8c414SAlan Cox 
60905a8c414SAlan Cox 	if (map->system_map)
61005a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
61105a8c414SAlan Cox 	else
61205a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
61325adb370SBrian Feldman }
61425adb370SBrian Feldman 
6153a0916b8SKonstantin Belousov #ifdef INVARIANTS
6163a0916b8SKonstantin Belousov static void
6173a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6183a0916b8SKonstantin Belousov {
6193a0916b8SKonstantin Belousov 
6203a0916b8SKonstantin Belousov 	if (map->system_map)
621ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
6223a0916b8SKonstantin Belousov 	else
6239fde98bbSAttilio Rao 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
6243a0916b8SKonstantin Belousov }
6253a0916b8SKonstantin Belousov 
6263a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6273a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6283a0916b8SKonstantin Belousov #else
6293a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6303a0916b8SKonstantin Belousov #endif
6313a0916b8SKonstantin Belousov 
632acd9a301SAlan Cox /*
6338304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6348304adaaSAlan Cox  *
6358304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6368304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6378304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6388304adaaSAlan Cox  *	exceeded.
6398304adaaSAlan Cox  *
6408304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6418304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6428304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6438304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6448304adaaSAlan Cox  *	operation on the map.
645acd9a301SAlan Cox  */
6469688f931SAlan Cox int
6478304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
648acd9a301SAlan Cox {
649acd9a301SAlan Cox 
6503a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6518304adaaSAlan Cox 	if (map->system_map)
652ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
6538304adaaSAlan Cox 	else
6549fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
6558304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
6568304adaaSAlan Cox 	    timo));
657acd9a301SAlan Cox }
658acd9a301SAlan Cox 
659acd9a301SAlan Cox /*
660acd9a301SAlan Cox  *	vm_map_wakeup:
6618304adaaSAlan Cox  *
6628304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
6638304adaaSAlan Cox  *	vm_map_unlock_and_wait().
664acd9a301SAlan Cox  */
6659688f931SAlan Cox void
666acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
667acd9a301SAlan Cox {
668acd9a301SAlan Cox 
669b49ecb86SAlan Cox 	/*
6703a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
6718304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
6728304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
673b49ecb86SAlan Cox 	 */
6743a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
6753a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
676acd9a301SAlan Cox 	wakeup(&map->root);
677acd9a301SAlan Cox }
678acd9a301SAlan Cox 
679a5db445dSMax Laier void
680a5db445dSMax Laier vm_map_busy(vm_map_t map)
681a5db445dSMax Laier {
682a5db445dSMax Laier 
683a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
684a5db445dSMax Laier 	map->busy++;
685a5db445dSMax Laier }
686a5db445dSMax Laier 
687a5db445dSMax Laier void
688a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
689a5db445dSMax Laier {
690a5db445dSMax Laier 
691a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
692a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
693a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
694a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
695a5db445dSMax Laier 		wakeup(&map->busy);
696a5db445dSMax Laier 	}
697a5db445dSMax Laier }
698a5db445dSMax Laier 
699a5db445dSMax Laier void
700a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
701a5db445dSMax Laier {
702a5db445dSMax Laier 
703a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
704a5db445dSMax Laier 	while (map->busy) {
705a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
706a5db445dSMax Laier 		if (map->system_map)
707a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
708a5db445dSMax Laier 		else
709a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
710a5db445dSMax Laier 	}
711a5db445dSMax Laier 	map->timestamp++;
712a5db445dSMax Laier }
713a5db445dSMax Laier 
7141b40f8c0SMatthew Dillon long
7151b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
7161b40f8c0SMatthew Dillon {
7171b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
7181b40f8c0SMatthew Dillon }
7191b40f8c0SMatthew Dillon 
720ff2b5645SMatthew Dillon /*
721df8bae1dSRodney W. Grimes  *	vm_map_create:
722df8bae1dSRodney W. Grimes  *
723df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
724df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
725df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
726df8bae1dSRodney W. Grimes  */
7270d94caffSDavid Greenman vm_map_t
7281b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
729df8bae1dSRodney W. Grimes {
730c0877f10SJohn Dyson 	vm_map_t result;
731df8bae1dSRodney W. Grimes 
732a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
73321c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
73492351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
735df8bae1dSRodney W. Grimes 	return (result);
736df8bae1dSRodney W. Grimes }
737df8bae1dSRodney W. Grimes 
738df8bae1dSRodney W. Grimes /*
739df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
740df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
741df8bae1dSRodney W. Grimes  */
7428355f576SJeff Roberson static void
74392351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
744df8bae1dSRodney W. Grimes {
74521c641b2SJohn Baldwin 
746df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7479688f931SAlan Cox 	map->needs_wakeup = FALSE;
7483075778bSJohn Dyson 	map->system_map = 0;
74992351f16SAlan Cox 	map->pmap = pmap;
750df8bae1dSRodney W. Grimes 	map->min_offset = min;
751df8bae1dSRodney W. Grimes 	map->max_offset = max;
752af7cd0c5SBrian Feldman 	map->flags = 0;
7534e94f402SAlan Cox 	map->root = NULL;
754df8bae1dSRodney W. Grimes 	map->timestamp = 0;
755a5db445dSMax Laier 	map->busy = 0;
756df8bae1dSRodney W. Grimes }
757df8bae1dSRodney W. Grimes 
758a18b1f1dSJason Evans void
75992351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
760a18b1f1dSJason Evans {
76192351f16SAlan Cox 
76292351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
763d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
76412c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
765a18b1f1dSJason Evans }
766a18b1f1dSJason Evans 
767df8bae1dSRodney W. Grimes /*
768b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
769b18bfc3dSJohn Dyson  *
770b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
771b18bfc3dSJohn Dyson  */
77262487bb4SJohn Dyson static void
7731b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
774b18bfc3dSJohn Dyson {
7752b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
776b18bfc3dSJohn Dyson }
777b18bfc3dSJohn Dyson 
778b18bfc3dSJohn Dyson /*
779df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
780df8bae1dSRodney W. Grimes  *
781df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
782b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
783df8bae1dSRodney W. Grimes  */
784f708ef1bSPoul-Henning Kamp static vm_map_entry_t
7851b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
786df8bae1dSRodney W. Grimes {
7871f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
7881f6889a1SMatthew Dillon 
7892b4a2c27SAlan Cox 	if (map->system_map)
7902b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
7912b4a2c27SAlan Cox 	else
792a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
7931f6889a1SMatthew Dillon 	if (new_entry == NULL)
7941f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
7951f6889a1SMatthew Dillon 	return (new_entry);
796df8bae1dSRodney W. Grimes }
797df8bae1dSRodney W. Grimes 
798df8bae1dSRodney W. Grimes /*
799794316a8SAlan Cox  *	vm_map_entry_set_behavior:
800794316a8SAlan Cox  *
801794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
802794316a8SAlan Cox  *	sequential.
803794316a8SAlan Cox  */
80462a59e8fSWarner Losh static inline void
805794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
806794316a8SAlan Cox {
807794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
808794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
809794316a8SAlan Cox }
810794316a8SAlan Cox 
811794316a8SAlan Cox /*
8120164e057SAlan Cox  *	vm_map_entry_set_max_free:
8130164e057SAlan Cox  *
8140164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
8150164e057SAlan Cox  */
81662a59e8fSWarner Losh static inline void
8170164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
8180164e057SAlan Cox {
8190164e057SAlan Cox 
8200164e057SAlan Cox 	entry->max_free = entry->adj_free;
8210164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
8220164e057SAlan Cox 		entry->max_free = entry->left->max_free;
8230164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
8240164e057SAlan Cox 		entry->max_free = entry->right->max_free;
8250164e057SAlan Cox }
8260164e057SAlan Cox 
8270164e057SAlan Cox /*
8284e94f402SAlan Cox  *	vm_map_entry_splay:
8294e94f402SAlan Cox  *
8300164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
8310164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
8320164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8330164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8340164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8350164e057SAlan Cox  *	amortized.
8360164e057SAlan Cox  *
8370164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8380164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8390164e057SAlan Cox  *
8400164e057SAlan Cox  *	The map must be locked, and leaves it so.
8410164e057SAlan Cox  *
8420164e057SAlan Cox  *	Returns: the new root.
8434e94f402SAlan Cox  */
8444e94f402SAlan Cox static vm_map_entry_t
8450164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8464e94f402SAlan Cox {
8470164e057SAlan Cox 	vm_map_entry_t llist, rlist;
8480164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
8490164e057SAlan Cox 	vm_map_entry_t y;
8504e94f402SAlan Cox 
8510164e057SAlan Cox 	/* Special case of empty tree. */
8524e94f402SAlan Cox 	if (root == NULL)
8534e94f402SAlan Cox 		return (root);
8540164e057SAlan Cox 
8550164e057SAlan Cox 	/*
8560164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
8570164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
8580164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
8590164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
8600164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
8610164e057SAlan Cox 	 * the two spines.
8620164e057SAlan Cox 	 */
8630164e057SAlan Cox 	llist = NULL;
8640164e057SAlan Cox 	rlist = NULL;
8650164e057SAlan Cox 	for (;;) {
8660164e057SAlan Cox 		/* root is never NULL in here. */
8670164e057SAlan Cox 		if (addr < root->start) {
8680164e057SAlan Cox 			y = root->left;
8690164e057SAlan Cox 			if (y == NULL)
8704e94f402SAlan Cox 				break;
8710164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
8720164e057SAlan Cox 				/* Rotate right and put y on rlist. */
8734e94f402SAlan Cox 				root->left = y->right;
8744e94f402SAlan Cox 				y->right = root;
8750164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8760164e057SAlan Cox 				root = y->left;
8770164e057SAlan Cox 				y->left = rlist;
8780164e057SAlan Cox 				rlist = y;
8790164e057SAlan Cox 			} else {
8800164e057SAlan Cox 				/* Put root on rlist. */
8810164e057SAlan Cox 				root->left = rlist;
8820164e057SAlan Cox 				rlist = root;
8834e94f402SAlan Cox 				root = y;
8844e94f402SAlan Cox 			}
8857438d60bSAlan Cox 		} else if (addr >= root->end) {
8860164e057SAlan Cox 			y = root->right;
8877438d60bSAlan Cox 			if (y == NULL)
8884e94f402SAlan Cox 				break;
8890164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
8900164e057SAlan Cox 				/* Rotate left and put y on llist. */
8914e94f402SAlan Cox 				root->right = y->left;
8924e94f402SAlan Cox 				y->left = root;
8930164e057SAlan Cox 				vm_map_entry_set_max_free(root);
8940164e057SAlan Cox 				root = y->right;
8950164e057SAlan Cox 				y->right = llist;
8960164e057SAlan Cox 				llist = y;
8970164e057SAlan Cox 			} else {
8980164e057SAlan Cox 				/* Put root on llist. */
8990164e057SAlan Cox 				root->right = llist;
9000164e057SAlan Cox 				llist = root;
9014e94f402SAlan Cox 				root = y;
9024e94f402SAlan Cox 			}
9037438d60bSAlan Cox 		} else
9047438d60bSAlan Cox 			break;
9050164e057SAlan Cox 	}
9060164e057SAlan Cox 
9070164e057SAlan Cox 	/*
9080164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
9090164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
9100164e057SAlan Cox 	 * bottom of llist and rlist.
9110164e057SAlan Cox 	 */
9120164e057SAlan Cox 	ltree = root->left;
9130164e057SAlan Cox 	while (llist != NULL) {
9140164e057SAlan Cox 		y = llist->right;
9150164e057SAlan Cox 		llist->right = ltree;
9160164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
9170164e057SAlan Cox 		ltree = llist;
9180164e057SAlan Cox 		llist = y;
9190164e057SAlan Cox 	}
9200164e057SAlan Cox 	rtree = root->right;
9210164e057SAlan Cox 	while (rlist != NULL) {
9220164e057SAlan Cox 		y = rlist->left;
9230164e057SAlan Cox 		rlist->left = rtree;
9240164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
9250164e057SAlan Cox 		rtree = rlist;
9260164e057SAlan Cox 		rlist = y;
9270164e057SAlan Cox 	}
9280164e057SAlan Cox 
9290164e057SAlan Cox 	/*
9300164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
9310164e057SAlan Cox 	 */
9320164e057SAlan Cox 	root->left = ltree;
9330164e057SAlan Cox 	root->right = rtree;
9340164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9350164e057SAlan Cox 
9364e94f402SAlan Cox 	return (root);
9374e94f402SAlan Cox }
9384e94f402SAlan Cox 
9394e94f402SAlan Cox /*
940df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
941df8bae1dSRodney W. Grimes  *
942df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
943df8bae1dSRodney W. Grimes  */
9444e94f402SAlan Cox static void
94599c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
94699c81ca9SAlan Cox 		  vm_map_entry_t after_where,
94799c81ca9SAlan Cox 		  vm_map_entry_t entry)
94899c81ca9SAlan Cox {
94921c641b2SJohn Baldwin 
95021c641b2SJohn Baldwin 	CTR4(KTR_VM,
95121c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
95221c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
9533a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
9545831f5fcSKonstantin Belousov 	KASSERT(after_where == &map->header ||
9555831f5fcSKonstantin Belousov 	    after_where->end <= entry->start,
9565831f5fcSKonstantin Belousov 	    ("vm_map_entry_link: prev end %jx new start %jx overlap",
9575831f5fcSKonstantin Belousov 	    (uintmax_t)after_where->end, (uintmax_t)entry->start));
9585831f5fcSKonstantin Belousov 	KASSERT(after_where->next == &map->header ||
9595831f5fcSKonstantin Belousov 	    entry->end <= after_where->next->start,
9605831f5fcSKonstantin Belousov 	    ("vm_map_entry_link: new end %jx next start %jx overlap",
9615831f5fcSKonstantin Belousov 	    (uintmax_t)entry->end, (uintmax_t)after_where->next->start));
9625831f5fcSKonstantin Belousov 
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,
113533314db0SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
113630dcfc09SJohn Dyson {
113733314db0SAlan Cox 	vm_map_entry_t new_entry, prev_entry, temp_entry;
11389730a5daSPaul Saab 	vm_eflags_t protoeflags;
1139ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
11408211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
114130dcfc09SJohn Dyson 
11423a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
114333314db0SAlan Cox 	KASSERT((object != kmem_object && object != kernel_object) ||
114433314db0SAlan Cox 	    (cow & MAP_COPY_ON_WRITE) == 0,
114533314db0SAlan Cox 	    ("vm_map_insert: kmem or kernel object and COW"));
114633314db0SAlan Cox 	KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
114733314db0SAlan Cox 	    ("vm_map_insert: paradoxical MAP_NOFAULT request"));
11483a0916b8SKonstantin Belousov 
114930dcfc09SJohn Dyson 	/*
115030dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
115130dcfc09SJohn Dyson 	 */
115230dcfc09SJohn Dyson 	if ((start < map->min_offset) || (end > map->max_offset) ||
115330dcfc09SJohn Dyson 	    (start >= end))
115430dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
115530dcfc09SJohn Dyson 
115630dcfc09SJohn Dyson 	/*
115730dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
115830dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
115930dcfc09SJohn Dyson 	 */
116030dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
116130dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
116230dcfc09SJohn Dyson 
116330dcfc09SJohn Dyson 	prev_entry = temp_entry;
116430dcfc09SJohn Dyson 
116530dcfc09SJohn Dyson 	/*
116630dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
116730dcfc09SJohn Dyson 	 */
116830dcfc09SJohn Dyson 	if ((prev_entry->next != &map->header) &&
116930dcfc09SJohn Dyson 	    (prev_entry->next->start < end))
117030dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
117130dcfc09SJohn Dyson 
1172afa07f7eSJohn Dyson 	protoeflags = 0;
1173afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1174e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
117533314db0SAlan Cox 	if (cow & MAP_NOFAULT)
1176afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
11774f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
11784f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
11799730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
11809730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
1181712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_DOWN)
1182712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_DOWN;
1183712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_UP)
1184712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_UP;
118584110e7eSKonstantin Belousov 	if (cow & MAP_VN_WRITECOUNT)
118684110e7eSKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
11878211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
11888211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
11898211bd45SKonstantin Belousov 	else
11908211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
11914f79d873SMatthew Dillon 
1192ef694c1aSEdward Tomasz Napierala 	cred = NULL;
11933364c323SKonstantin Belousov 	if (cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT))
11943364c323SKonstantin Belousov 		goto charged;
11953364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
11963364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
11973364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
11983364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
119941c22744SKonstantin Belousov 		KASSERT(object == NULL || (protoeflags & MAP_ENTRY_NEEDS_COPY) ||
1200ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
12013364c323SKonstantin Belousov 		    ("OVERCOMMIT: vm_map_insert o %p", object));
1202ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
12033364c323SKonstantin Belousov 	}
12043364c323SKonstantin Belousov 
12053364c323SKonstantin Belousov charged:
1206f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1207f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1208f8616ebfSAlan Cox 		pmap_growkernel(end);
12091d284e00SAlan Cox 	if (object != NULL) {
121030dcfc09SJohn Dyson 		/*
12111d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
12121d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
12131d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
12141d284e00SAlan Cox 		 * reference counting is insufficient to recognize
12151d284e00SAlan Cox 		 * aliases with precision.)
121630dcfc09SJohn Dyson 		 */
121789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
12181d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
12192aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
122089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
12214e045f93SAlan Cox 	}
12224e045f93SAlan Cox 	else if ((prev_entry != &map->header) &&
12234e045f93SAlan Cox 		 (prev_entry->eflags == protoeflags) &&
1224b5f8c226SKonstantin Belousov 		 (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 &&
12258cc7e047SJohn Dyson 		 (prev_entry->end == start) &&
12264e045f93SAlan Cox 		 (prev_entry->wired_count == 0) &&
1227ef694c1aSEdward Tomasz Napierala 		 (prev_entry->cred == cred ||
12283364c323SKonstantin Belousov 		  (prev_entry->object.vm_object != NULL &&
1229ef694c1aSEdward Tomasz Napierala 		   (prev_entry->object.vm_object->cred == cred))) &&
12308cc7e047SJohn Dyson 		   vm_object_coalesce(prev_entry->object.vm_object,
123157a21abaSAlan Cox 		       prev_entry->offset,
12328cc7e047SJohn Dyson 		       (vm_size_t)(prev_entry->end - prev_entry->start),
123360169c88SAlan Cox 		       (vm_size_t)(end - prev_entry->end), cred != NULL &&
123460169c88SAlan Cox 		       (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
123530dcfc09SJohn Dyson 		/*
12362aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
12372aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
12382aaeadf8SMatthew Dillon 		 * new range as well.
123930dcfc09SJohn Dyson 		 */
12408211bd45SKonstantin Belousov 		if ((prev_entry->inheritance == inheritance) &&
12418cc7e047SJohn Dyson 		    (prev_entry->protection == prot) &&
12428cc7e047SJohn Dyson 		    (prev_entry->max_protection == max)) {
124330dcfc09SJohn Dyson 			map->size += (end - prev_entry->end);
124430dcfc09SJohn Dyson 			prev_entry->end = end;
12450164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
12464e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
124730dcfc09SJohn Dyson 			return (KERN_SUCCESS);
124830dcfc09SJohn Dyson 		}
12498cc7e047SJohn Dyson 
12502aaeadf8SMatthew Dillon 		/*
12512aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
12522aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
12532aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
12544e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
12552aaeadf8SMatthew Dillon 		 */
12562aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
12572aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
12582aaeadf8SMatthew Dillon 			(prev_entry->end - prev_entry->start);
12598cc7e047SJohn Dyson 		vm_object_reference(object);
1260ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
12613364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
12623364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1263ef694c1aSEdward Tomasz Napierala 			cred = NULL;
12643364c323SKonstantin Belousov 		}
1265b18bfc3dSJohn Dyson 	}
126660169c88SAlan Cox 	if (cred != NULL)
126760169c88SAlan Cox 		crhold(cred);
12682aaeadf8SMatthew Dillon 
12692aaeadf8SMatthew Dillon 	/*
127030dcfc09SJohn Dyson 	 * Create a new entry
127130dcfc09SJohn Dyson 	 */
127230dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
127330dcfc09SJohn Dyson 	new_entry->start = start;
127430dcfc09SJohn Dyson 	new_entry->end = end;
1275ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
127630dcfc09SJohn Dyson 
1277afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
127830dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
127930dcfc09SJohn Dyson 	new_entry->offset = offset;
12802267af78SJulian Elischer 	new_entry->avail_ssize = 0;
12812267af78SJulian Elischer 
12828211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
128330dcfc09SJohn Dyson 	new_entry->protection = prot;
128430dcfc09SJohn Dyson 	new_entry->max_protection = max;
128530dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1286997ac690SKonstantin Belousov 	new_entry->wiring_thread = NULL;
128713458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
128813458803SAlan Cox 	new_entry->next_read = OFF_TO_IDX(offset);
1289e5f251d2SAlan Cox 
1290ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
12913364c323SKonstantin Belousov 	    ("OVERCOMMIT: vm_map_insert leaks vm_map %p", new_entry));
1292ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
12933364c323SKonstantin Belousov 
129430dcfc09SJohn Dyson 	/*
129530dcfc09SJohn Dyson 	 * Insert the new entry into the list
129630dcfc09SJohn Dyson 	 */
129730dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
129830dcfc09SJohn Dyson 	map->size += new_entry->end - new_entry->start;
129930dcfc09SJohn Dyson 
13001a484d28SMatthew Dillon 	/*
1301eaaf9f7fSAlan Cox 	 * Try to coalesce the new entry with both the previous and next
1302eaaf9f7fSAlan Cox 	 * entries in the list.  Previously, we only attempted to coalesce
1303eaaf9f7fSAlan Cox 	 * with the previous entry when object is NULL.  Here, we handle the
1304eaaf9f7fSAlan Cox 	 * other cases, which are less common.
13051a484d28SMatthew Dillon 	 */
13064e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
13074e71e795SMatthew Dillon 
13084f79d873SMatthew Dillon 	if (cow & (MAP_PREFAULT|MAP_PREFAULT_PARTIAL)) {
13094da4d293SAlan Cox 		vm_map_pmap_enter(map, start, prot,
1310e972780aSAlan Cox 				    object, OFF_TO_IDX(offset), end - start,
1311e972780aSAlan Cox 				    cow & MAP_PREFAULT_PARTIAL);
13124f79d873SMatthew Dillon 	}
1313e972780aSAlan Cox 
131430dcfc09SJohn Dyson 	return (KERN_SUCCESS);
131530dcfc09SJohn Dyson }
131630dcfc09SJohn Dyson 
131730dcfc09SJohn Dyson /*
13180164e057SAlan Cox  *	vm_map_findspace:
13190164e057SAlan Cox  *
13200164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
13210164e057SAlan Cox  *	beginning at address >= start in the given map.
13220164e057SAlan Cox  *
13230164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
13240164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
13250164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
13260164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
13270164e057SAlan Cox  *	O(log n) amortized with splay trees.
13280164e057SAlan Cox  *
13290164e057SAlan Cox  *	The map must be locked, and leaves it so.
13300164e057SAlan Cox  *
13310164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
13320164e057SAlan Cox  *		 1 if insufficient space.
1333df8bae1dSRodney W. Grimes  */
1334df8bae1dSRodney W. Grimes int
13350164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13360164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1337df8bae1dSRodney W. Grimes {
13380164e057SAlan Cox 	vm_map_entry_t entry;
1339f8616ebfSAlan Cox 	vm_offset_t st;
1340df8bae1dSRodney W. Grimes 
1341986b43f8SAlan Cox 	/*
1342986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1343986b43f8SAlan Cox 	 * address wrap.
1344986b43f8SAlan Cox 	 */
1345df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1346df8bae1dSRodney W. Grimes 		start = map->min_offset;
1347986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1348df8bae1dSRodney W. Grimes 		return (1);
1349df8bae1dSRodney W. Grimes 
13500164e057SAlan Cox 	/* Empty tree means wide open address space. */
13510164e057SAlan Cox 	if (map->root == NULL) {
1352df8bae1dSRodney W. Grimes 		*addr = start;
1353f8616ebfSAlan Cox 		return (0);
135499448ed1SJohn Dyson 	}
13550164e057SAlan Cox 
13560164e057SAlan Cox 	/*
13570164e057SAlan Cox 	 * After splay, if start comes before root node, then there
13580164e057SAlan Cox 	 * must be a gap from start to the root.
13590164e057SAlan Cox 	 */
13600164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
13610164e057SAlan Cox 	if (start + length <= map->root->start) {
13620164e057SAlan Cox 		*addr = start;
1363f8616ebfSAlan Cox 		return (0);
13640164e057SAlan Cox 	}
13650164e057SAlan Cox 
13660164e057SAlan Cox 	/*
13670164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1368986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1369986b43f8SAlan Cox 	 * wrap might be a problem.
13700164e057SAlan Cox 	 */
13710164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1372986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
13730164e057SAlan Cox 		*addr = st;
1374f8616ebfSAlan Cox 		return (0);
13750164e057SAlan Cox 	}
13760164e057SAlan Cox 
13770164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
13780164e057SAlan Cox 	entry = map->root->right;
13790164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
13800164e057SAlan Cox 		return (1);
13810164e057SAlan Cox 
13820164e057SAlan Cox 	/*
13830164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
13840164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
13850164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
13860164e057SAlan Cox 	 */
13870164e057SAlan Cox 	while (entry != NULL) {
13880164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
13890164e057SAlan Cox 			entry = entry->left;
13900164e057SAlan Cox 		else if (entry->adj_free >= length) {
13910164e057SAlan Cox 			*addr = entry->end;
1392f8616ebfSAlan Cox 			return (0);
13930164e057SAlan Cox 		} else
13940164e057SAlan Cox 			entry = entry->right;
13950164e057SAlan Cox 	}
13960164e057SAlan Cox 
13970164e057SAlan Cox 	/* Can't get here, so panic if we do. */
13980164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1399df8bae1dSRodney W. Grimes }
1400df8bae1dSRodney W. Grimes 
1401d239bd3cSKonstantin Belousov int
1402d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1403b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1404d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1405d239bd3cSKonstantin Belousov {
1406b8ca4ef2SAlan Cox 	vm_offset_t end;
1407d239bd3cSKonstantin Belousov 	int result;
1408d239bd3cSKonstantin Belousov 
1409d239bd3cSKonstantin Belousov 	end = start + length;
14104648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
14114648ba0aSKonstantin Belousov 	    object == NULL,
14124648ba0aSKonstantin Belousov 	    ("vm_map_fixed: non-NULL backing object for stack"));
1413897d81a0SKonstantin Belousov 	vm_map_lock(map);
1414d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
141511c42bccSKonstantin Belousov 	if ((cow & MAP_CHECK_EXCL) == 0)
141611c42bccSKonstantin Belousov 		vm_map_delete(map, start, end);
14174648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
14184648ba0aSKonstantin Belousov 		result = vm_map_stack_locked(map, start, length, sgrowsiz,
14194648ba0aSKonstantin Belousov 		    prot, max, cow);
14204648ba0aSKonstantin Belousov 	} else {
14214648ba0aSKonstantin Belousov 		result = vm_map_insert(map, object, offset, start, end,
14224648ba0aSKonstantin Belousov 		    prot, max, cow);
14234648ba0aSKonstantin Belousov 	}
1424d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1425d239bd3cSKonstantin Belousov 	return (result);
1426d239bd3cSKonstantin Belousov }
1427d239bd3cSKonstantin Belousov 
1428df8bae1dSRodney W. Grimes /*
1429df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1430df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1431df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1432df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1433df8bae1dSRodney W. Grimes  *
14342aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
14352aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1436df8bae1dSRodney W. Grimes  */
1437df8bae1dSRodney W. Grimes int
1438b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1439b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1440edb572a3SJohn Baldwin 	    vm_size_t length, vm_offset_t max_addr, int find_space,
1441edb572a3SJohn Baldwin 	    vm_prot_t prot, vm_prot_t max, int cow)
1442df8bae1dSRodney W. Grimes {
14435aa60b6fSJohn Baldwin 	vm_offset_t alignment, initial_addr, start;
14446eaee3feSAlan Cox 	int result;
1445df8bae1dSRodney W. Grimes 
14464648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
14474648ba0aSKonstantin Belousov 	    object == NULL,
14484648ba0aSKonstantin Belousov 	    ("vm_map_find: non-NULL backing object for stack"));
1449ff74a3faSJohn Baldwin 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1450ff74a3faSJohn Baldwin 	    (object->flags & OBJ_COLORED) == 0))
1451ff74a3faSJohn Baldwin 		find_space = VMFS_ANY_SPACE;
14525aa60b6fSJohn Baldwin 	if (find_space >> 8 != 0) {
14535aa60b6fSJohn Baldwin 		KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
14545aa60b6fSJohn Baldwin 		alignment = (vm_offset_t)1 << (find_space >> 8);
14555aa60b6fSJohn Baldwin 	} else
14565aa60b6fSJohn Baldwin 		alignment = 0;
1457ff74a3faSJohn Baldwin 	initial_addr = *addr;
1458ff74a3faSJohn Baldwin again:
1459ff74a3faSJohn Baldwin 	start = initial_addr;
1460bea41bcfSDavid Greenman 	vm_map_lock(map);
146126c538ffSAlan Cox 	do {
146226c538ffSAlan Cox 		if (find_space != VMFS_NO_SPACE) {
1463edb572a3SJohn Baldwin 			if (vm_map_findspace(map, start, length, addr) ||
1464edb572a3SJohn Baldwin 			    (max_addr != 0 && *addr + length > max_addr)) {
1465df8bae1dSRodney W. Grimes 				vm_map_unlock(map);
1466ff74a3faSJohn Baldwin 				if (find_space == VMFS_OPTIMAL_SPACE) {
1467ff74a3faSJohn Baldwin 					find_space = VMFS_ANY_SPACE;
1468ff74a3faSJohn Baldwin 					goto again;
1469ff74a3faSJohn Baldwin 				}
1470df8bae1dSRodney W. Grimes 				return (KERN_NO_SPACE);
1471df8bae1dSRodney W. Grimes 			}
1472ca596a25SJuli Mallett 			switch (find_space) {
14735aa60b6fSJohn Baldwin 			case VMFS_SUPER_SPACE:
1474ff74a3faSJohn Baldwin 			case VMFS_OPTIMAL_SPACE:
147526c538ffSAlan Cox 				pmap_align_superpage(object, offset, addr,
147626c538ffSAlan Cox 				    length);
1477ca596a25SJuli Mallett 				break;
14785aa60b6fSJohn Baldwin 			case VMFS_ANY_SPACE:
14795aa60b6fSJohn Baldwin 				break;
1480ca596a25SJuli Mallett 			default:
14815aa60b6fSJohn Baldwin 				if ((*addr & (alignment - 1)) != 0) {
14825aa60b6fSJohn Baldwin 					*addr &= ~(alignment - 1);
14835aa60b6fSJohn Baldwin 					*addr += alignment;
14845aa60b6fSJohn Baldwin 				}
1485ca596a25SJuli Mallett 				break;
1486ca596a25SJuli Mallett 			}
1487ca596a25SJuli Mallett 
1488df8bae1dSRodney W. Grimes 			start = *addr;
1489df8bae1dSRodney W. Grimes 		}
14904648ba0aSKonstantin Belousov 		if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
14914648ba0aSKonstantin Belousov 			result = vm_map_stack_locked(map, start, length,
14924648ba0aSKonstantin Belousov 			    sgrowsiz, prot, max, cow);
14934648ba0aSKonstantin Belousov 		} else {
14944648ba0aSKonstantin Belousov 			result = vm_map_insert(map, object, offset, start,
14954648ba0aSKonstantin Belousov 			    start + length, prot, max, cow);
14964648ba0aSKonstantin Belousov 		}
14975aa60b6fSJohn Baldwin 	} while (result == KERN_NO_SPACE && find_space != VMFS_NO_SPACE &&
14985aa60b6fSJohn Baldwin 	    find_space != VMFS_ANY_SPACE);
1499df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1500df8bae1dSRodney W. Grimes 	return (result);
1501df8bae1dSRodney W. Grimes }
1502df8bae1dSRodney W. Grimes 
1503df8bae1dSRodney W. Grimes /*
1504b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
150567bf6868SJohn Dyson  *
15064e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
15074e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
15084e71e795SMatthew Dillon  *
15094e71e795SMatthew Dillon  *	The map must be locked.
15104e71e795SMatthew Dillon  *
15114e71e795SMatthew Dillon  *	This routine guarentees that the passed entry remains valid (though
15124e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
15134e71e795SMatthew Dillon  *	both neighbors.
1514df8bae1dSRodney W. Grimes  */
15150afcd3afSAlan Cox void
15161b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1517df8bae1dSRodney W. Grimes {
1518308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1519b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1520df8bae1dSRodney W. Grimes 
1521eaaf9f7fSAlan Cox 	if ((entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP |
1522eaaf9f7fSAlan Cox 	    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) != 0)
1523df8bae1dSRodney W. Grimes 		return;
1524308c24baSJohn Dyson 
1525308c24baSJohn Dyson 	prev = entry->prev;
1526308c24baSJohn Dyson 	if (prev != &map->header) {
152767bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
152867bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
152967bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
153095e5e988SJohn Dyson 		     (!prev->object.vm_object ||
153167bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1532afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
153367bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
153467bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
153567bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
15363364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
1537ef694c1aSEdward Tomasz Napierala 		     (prev->cred == entry->cred)) {
1538308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1539308c24baSJohn Dyson 			entry->start = prev->start;
1540308c24baSJohn Dyson 			entry->offset = prev->offset;
15410164e057SAlan Cox 			if (entry->prev != &map->header)
15420164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
15437fd10fb3SKonstantin Belousov 
15447fd10fb3SKonstantin Belousov 			/*
1545b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1546b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1547b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1548b0994946SKonstantin Belousov 			 * because the vnode has additional
1549b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1550b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1551b0994946SKonstantin Belousov 			 * the vnode lock.
155284110e7eSKonstantin Belousov 			 *
155384110e7eSKonstantin Belousov 			 * Since we count the number of virtual page
155484110e7eSKonstantin Belousov 			 * mappings in object->un_pager.vnp.writemappings,
155584110e7eSKonstantin Belousov 			 * the writemappings value should not be adjusted
155684110e7eSKonstantin Belousov 			 * when the entry is disposed of.
15577fd10fb3SKonstantin Belousov 			 */
1558b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1559308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1560ef694c1aSEdward Tomasz Napierala 			if (prev->cred != NULL)
1561ef694c1aSEdward Tomasz Napierala 				crfree(prev->cred);
1562308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1563308c24baSJohn Dyson 		}
1564308c24baSJohn Dyson 	}
1565de5f6a77SJohn Dyson 
1566de5f6a77SJohn Dyson 	next = entry->next;
1567308c24baSJohn Dyson 	if (next != &map->header) {
156867bf6868SJohn Dyson 		esize = entry->end - entry->start;
156967bf6868SJohn Dyson 		if ((entry->end == next->start) &&
157067bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
157167bf6868SJohn Dyson 		     (!entry->object.vm_object ||
157267bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1573afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
157467bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
157567bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
157667bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
15773364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
1578ef694c1aSEdward Tomasz Napierala 		    (next->cred == entry->cred)) {
1579de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1580de5f6a77SJohn Dyson 			entry->end = next->end;
15810164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
15827fd10fb3SKonstantin Belousov 
15837fd10fb3SKonstantin Belousov 			/*
15847fd10fb3SKonstantin Belousov 			 * See comment above.
15857fd10fb3SKonstantin Belousov 			 */
1586b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1587de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1588ef694c1aSEdward Tomasz Napierala 			if (next->cred != NULL)
1589ef694c1aSEdward Tomasz Napierala 				crfree(next->cred);
1590de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1591df8bae1dSRodney W. Grimes 		}
1592df8bae1dSRodney W. Grimes 	}
1593de5f6a77SJohn Dyson }
1594df8bae1dSRodney W. Grimes /*
1595df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1596df8bae1dSRodney W. Grimes  *
1597df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1598df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1599df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1600df8bae1dSRodney W. Grimes  */
1601df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1602df8bae1dSRodney W. Grimes { \
1603df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1604df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1605df8bae1dSRodney W. Grimes }
1606df8bae1dSRodney W. Grimes 
1607df8bae1dSRodney W. Grimes /*
1608df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1609df8bae1dSRodney W. Grimes  *	the entry must be split.
1610df8bae1dSRodney W. Grimes  */
16110d94caffSDavid Greenman static void
16121b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1613df8bae1dSRodney W. Grimes {
1614c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1615df8bae1dSRodney W. Grimes 
16163a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
16173a0916b8SKonstantin Belousov 
1618df8bae1dSRodney W. Grimes 	/*
16190d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
16200d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
16210d94caffSDavid Greenman 	 * starting address.
1622df8bae1dSRodney W. Grimes 	 */
1623f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1624f32dbbeeSJohn Dyson 
162511cccda1SJohn Dyson 	/*
162611cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
162711cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
162811cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
162911cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
163011cccda1SJohn Dyson 	 * put this improvement.
163111cccda1SJohn Dyson 	 */
16324e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
163311cccda1SJohn Dyson 		vm_object_t object;
163411cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1635c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
163611cccda1SJohn Dyson 		entry->object.vm_object = object;
163711cccda1SJohn Dyson 		entry->offset = 0;
1638ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1639ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
16403364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1641ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
16423364c323SKonstantin Belousov 		}
16433364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
16443364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1645ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
164689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1647ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1648ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1649ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
16503364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
165189f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1652ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
165311cccda1SJohn Dyson 	}
165411cccda1SJohn Dyson 
1655df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1656df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1657df8bae1dSRodney W. Grimes 
1658df8bae1dSRodney W. Grimes 	new_entry->end = start;
1659df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1660df8bae1dSRodney W. Grimes 	entry->start = start;
1661ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1662ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1663df8bae1dSRodney W. Grimes 
1664df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1665df8bae1dSRodney W. Grimes 
16669fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1667df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
166884110e7eSKonstantin Belousov 		/*
166984110e7eSKonstantin Belousov 		 * The object->un_pager.vnp.writemappings for the
167084110e7eSKonstantin Belousov 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
167184110e7eSKonstantin Belousov 		 * kept as is here.  The virtual pages are
167284110e7eSKonstantin Belousov 		 * re-distributed among the clipped entries, so the sum is
167384110e7eSKonstantin Belousov 		 * left the same.
167484110e7eSKonstantin Belousov 		 */
1675df8bae1dSRodney W. Grimes 	}
1676c0877f10SJohn Dyson }
1677df8bae1dSRodney W. Grimes 
1678df8bae1dSRodney W. Grimes /*
1679df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1680df8bae1dSRodney W. Grimes  *
1681df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1682df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1683df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1684df8bae1dSRodney W. Grimes  */
1685df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1686df8bae1dSRodney W. Grimes { \
1687af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1688af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1689df8bae1dSRodney W. Grimes }
1690df8bae1dSRodney W. Grimes 
1691df8bae1dSRodney W. Grimes /*
1692df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1693df8bae1dSRodney W. Grimes  *	the entry must be split.
1694df8bae1dSRodney W. Grimes  */
16950d94caffSDavid Greenman static void
16961b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1697df8bae1dSRodney W. Grimes {
1698c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1699df8bae1dSRodney W. Grimes 
17003a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
17013a0916b8SKonstantin Belousov 
1702df8bae1dSRodney W. Grimes 	/*
170311cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
170411cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
170511cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
170611cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
170711cccda1SJohn Dyson 	 * put this improvement.
170811cccda1SJohn Dyson 	 */
17094e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL && !map->system_map) {
171011cccda1SJohn Dyson 		vm_object_t object;
171111cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1712c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
171311cccda1SJohn Dyson 		entry->object.vm_object = object;
171411cccda1SJohn Dyson 		entry->offset = 0;
1715ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1716ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
17173364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1718ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
17193364c323SKonstantin Belousov 		}
17203364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
17213364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1722ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
172389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1724ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1725ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1726ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
17273364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
172889f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1729ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
173011cccda1SJohn Dyson 	}
173111cccda1SJohn Dyson 
173211cccda1SJohn Dyson 	/*
17330d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1734df8bae1dSRodney W. Grimes 	 */
1735df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1736df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1737df8bae1dSRodney W. Grimes 
1738df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1739df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1740ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1741ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1742df8bae1dSRodney W. Grimes 
1743df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1744df8bae1dSRodney W. Grimes 
17459fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1746df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1747df8bae1dSRodney W. Grimes 	}
1748c0877f10SJohn Dyson }
1749df8bae1dSRodney W. Grimes 
1750df8bae1dSRodney W. Grimes /*
1751df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1752df8bae1dSRodney W. Grimes  *
1753df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1754df8bae1dSRodney W. Grimes  *
1755df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1756df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1757df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1758df8bae1dSRodney W. Grimes  *
1759df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1760df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1761df8bae1dSRodney W. Grimes  *		vm_fault
1762df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1763df8bae1dSRodney W. Grimes  *
1764df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1765df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1766df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1767df8bae1dSRodney W. Grimes  */
1768df8bae1dSRodney W. Grimes int
17691b40f8c0SMatthew Dillon vm_map_submap(
17701b40f8c0SMatthew Dillon 	vm_map_t map,
17711b40f8c0SMatthew Dillon 	vm_offset_t start,
17721b40f8c0SMatthew Dillon 	vm_offset_t end,
17731b40f8c0SMatthew Dillon 	vm_map_t submap)
1774df8bae1dSRodney W. Grimes {
1775df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1776c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1777df8bae1dSRodney W. Grimes 
1778df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1779df8bae1dSRodney W. Grimes 
1780df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1781df8bae1dSRodney W. Grimes 
1782df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1783df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
17840d94caffSDavid Greenman 	} else
1785df8bae1dSRodney W. Grimes 		entry = entry->next;
1786df8bae1dSRodney W. Grimes 
1787df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1788df8bae1dSRodney W. Grimes 
1789df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
17909fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1791afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
17922d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1793afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1794df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1795df8bae1dSRodney W. Grimes 	}
1796df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1797df8bae1dSRodney W. Grimes 
1798df8bae1dSRodney W. Grimes 	return (result);
1799df8bae1dSRodney W. Grimes }
1800df8bae1dSRodney W. Grimes 
1801df8bae1dSRodney W. Grimes /*
1802dd05fa19SAlan Cox  * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
18031f78f902SAlan Cox  */
18041f78f902SAlan Cox #define	MAX_INIT_PT	96
18051f78f902SAlan Cox 
18061f78f902SAlan Cox /*
18070551c08dSAlan Cox  *	vm_map_pmap_enter:
18080551c08dSAlan Cox  *
1809dd05fa19SAlan Cox  *	Preload the specified map's pmap with mappings to the specified
1810dd05fa19SAlan Cox  *	object's memory-resident pages.  No further physical pages are
1811dd05fa19SAlan Cox  *	allocated, and no further virtual pages are retrieved from secondary
1812dd05fa19SAlan Cox  *	storage.  If the specified flags include MAP_PREFAULT_PARTIAL, then a
1813dd05fa19SAlan Cox  *	limited number of page mappings are created at the low-end of the
1814dd05fa19SAlan Cox  *	specified address range.  (For this purpose, a superpage mapping
1815dd05fa19SAlan Cox  *	counts as one page mapping.)  Otherwise, all resident pages within
1816dd05fa19SAlan Cox  *	the specified address range are mapped.  Because these mappings are
1817dd05fa19SAlan Cox  *	being created speculatively, cached pages are not reactivated and
1818a922d312SAlan Cox  *	mapped.
18190551c08dSAlan Cox  */
1820077ec27cSAlan Cox static void
18214da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
18220551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
18230551c08dSAlan Cox {
18248fece8c3SAlan Cox 	vm_offset_t start;
1825ce142d9eSAlan Cox 	vm_page_t p, p_start;
1826dd05fa19SAlan Cox 	vm_pindex_t mask, psize, threshold, tmpidx;
18270551c08dSAlan Cox 
1828ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
18291f78f902SAlan Cox 		return;
18309af6d512SAttilio Rao 	VM_OBJECT_RLOCK(object);
18319af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18329af6d512SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
183389f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
183401381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
18359af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
18369af6d512SAttilio Rao 			    size);
18379af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
18389af6d512SAttilio Rao 			return;
18399af6d512SAttilio Rao 		}
18409af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
18411f78f902SAlan Cox 	}
18421f78f902SAlan Cox 
18431f78f902SAlan Cox 	psize = atop(size);
18441f78f902SAlan Cox 	if (psize + pindex > object->size) {
18459af6d512SAttilio Rao 		if (object->size < pindex) {
18469af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
18479af6d512SAttilio Rao 			return;
18489af6d512SAttilio Rao 		}
18491f78f902SAlan Cox 		psize = object->size - pindex;
18501f78f902SAlan Cox 	}
18511f78f902SAlan Cox 
1852ce142d9eSAlan Cox 	start = 0;
1853ce142d9eSAlan Cox 	p_start = NULL;
1854dd05fa19SAlan Cox 	threshold = MAX_INIT_PT;
18551f78f902SAlan Cox 
1856b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
18571f78f902SAlan Cox 	/*
18581f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
18591f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
18601f78f902SAlan Cox 	 * or (2) NULL.
18611f78f902SAlan Cox 	 */
18621f78f902SAlan Cox 	for (;
18631f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
18641f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
18651f78f902SAlan Cox 		/*
18661f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
18671f78f902SAlan Cox 		 * free pages allocating pv entries.
18681f78f902SAlan Cox 		 */
1869dd05fa19SAlan Cox 		if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
1870dd05fa19SAlan Cox 		    vm_cnt.v_free_count < vm_cnt.v_free_reserved) ||
1871dd05fa19SAlan Cox 		    ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
1872dd05fa19SAlan Cox 		    tmpidx >= threshold)) {
1873379fb642SAlan Cox 			psize = tmpidx;
18741f78f902SAlan Cox 			break;
18751f78f902SAlan Cox 		}
18760a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
1877ce142d9eSAlan Cox 			if (p_start == NULL) {
1878ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
1879ce142d9eSAlan Cox 				p_start = p;
1880ce142d9eSAlan Cox 			}
1881dd05fa19SAlan Cox 			/* Jump ahead if a superpage mapping is possible. */
1882dd05fa19SAlan Cox 			if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
1883dd05fa19SAlan Cox 			    (pagesizes[p->psind] - 1)) == 0) {
1884dd05fa19SAlan Cox 				mask = atop(pagesizes[p->psind]) - 1;
1885dd05fa19SAlan Cox 				if (tmpidx + mask < psize &&
1886dd05fa19SAlan Cox 				    vm_page_ps_is_valid(p)) {
1887dd05fa19SAlan Cox 					p += mask;
1888dd05fa19SAlan Cox 					threshold += mask;
1889dd05fa19SAlan Cox 				}
1890dd05fa19SAlan Cox 			}
18917bfda801SAlan Cox 		} else if (p_start != NULL) {
1892cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
1893cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
1894cf4682aeSAlan Cox 			p_start = NULL;
1895cf4682aeSAlan Cox 		}
1896cf4682aeSAlan Cox 	}
1897c46b90e9SAlan Cox 	if (p_start != NULL)
1898379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
1899379fb642SAlan Cox 		    p_start, prot);
19009af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
19010551c08dSAlan Cox }
19020551c08dSAlan Cox 
19030551c08dSAlan Cox /*
1904df8bae1dSRodney W. Grimes  *	vm_map_protect:
1905df8bae1dSRodney W. Grimes  *
1906df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
1907df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
1908df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
1909df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
1910df8bae1dSRodney W. Grimes  */
1911df8bae1dSRodney W. Grimes int
1912b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
1913b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
1914df8bae1dSRodney W. Grimes {
1915210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
19163364c323SKonstantin Belousov 	vm_object_t obj;
1917ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
1918210a6886SKonstantin Belousov 	vm_prot_t old_prot;
1919df8bae1dSRodney W. Grimes 
192079e9451fSKonstantin Belousov 	if (start == end)
192179e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
192279e9451fSKonstantin Belousov 
1923df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1924df8bae1dSRodney W. Grimes 
1925df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1926df8bae1dSRodney W. Grimes 
1927df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1928df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
1929b7b2aac2SJohn Dyson 	} else {
1930df8bae1dSRodney W. Grimes 		entry = entry->next;
1931b7b2aac2SJohn Dyson 	}
1932df8bae1dSRodney W. Grimes 
1933df8bae1dSRodney W. Grimes 	/*
19340d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
1935df8bae1dSRodney W. Grimes 	 */
1936df8bae1dSRodney W. Grimes 	current = entry;
1937df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
1938afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
1939a1f6d91cSDavid Greenman 			vm_map_unlock(map);
1940df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
1941a1f6d91cSDavid Greenman 		}
1942df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
1943df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
1944df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
1945df8bae1dSRodney W. Grimes 		}
1946df8bae1dSRodney W. Grimes 		current = current->next;
1947df8bae1dSRodney W. Grimes 	}
1948df8bae1dSRodney W. Grimes 
19493364c323SKonstantin Belousov 
19503364c323SKonstantin Belousov 	/*
19513364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
19523364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
19533364c323SKonstantin Belousov 	 * breakpoint on text segment)
19543364c323SKonstantin Belousov 	 */
19553364c323SKonstantin Belousov 	for (current = entry; (current != &map->header) &&
19563364c323SKonstantin Belousov 	     (current->start < end); current = current->next) {
19573364c323SKonstantin Belousov 
19583364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
19593364c323SKonstantin Belousov 
19603364c323SKonstantin Belousov 		if (set_max ||
19613364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
19623364c323SKonstantin Belousov 		    ENTRY_CHARGED(current)) {
19633364c323SKonstantin Belousov 			continue;
19643364c323SKonstantin Belousov 		}
19653364c323SKonstantin Belousov 
1966ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
19673364c323SKonstantin Belousov 		obj = current->object.vm_object;
19683364c323SKonstantin Belousov 
19693364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
19703364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
19713364c323SKonstantin Belousov 				vm_map_unlock(map);
19723364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
19733364c323SKonstantin Belousov 			}
1974ef694c1aSEdward Tomasz Napierala 			crhold(cred);
1975ef694c1aSEdward Tomasz Napierala 			current->cred = cred;
19763364c323SKonstantin Belousov 			continue;
19773364c323SKonstantin Belousov 		}
19783364c323SKonstantin Belousov 
197989f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
19803364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
198189f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19823364c323SKonstantin Belousov 			continue;
19833364c323SKonstantin Belousov 		}
19843364c323SKonstantin Belousov 
19853364c323SKonstantin Belousov 		/*
19863364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
19873364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
19883364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
19893364c323SKonstantin Belousov 		 */
19903364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
19913d95614fSKonstantin Belousov 		    ("vm_map_protect: object %p overcharged (entry %p)",
19923d95614fSKonstantin Belousov 		    obj, current));
19933364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
199489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
19953364c323SKonstantin Belousov 			vm_map_unlock(map);
19963364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
19973364c323SKonstantin Belousov 		}
19983364c323SKonstantin Belousov 
1999ef694c1aSEdward Tomasz Napierala 		crhold(cred);
2000ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
20013364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
200289f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
20033364c323SKonstantin Belousov 	}
20043364c323SKonstantin Belousov 
2005df8bae1dSRodney W. Grimes 	/*
20060d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
20070d94caffSDavid Greenman 	 * necessary the second time.]
2008df8bae1dSRodney W. Grimes 	 */
2009df8bae1dSRodney W. Grimes 	current = entry;
2010df8bae1dSRodney W. Grimes 	while ((current != &map->header) && (current->start < end)) {
2011df8bae1dSRodney W. Grimes 		old_prot = current->protection;
2012210a6886SKonstantin Belousov 
2013df8bae1dSRodney W. Grimes 		if (set_max)
2014df8bae1dSRodney W. Grimes 			current->protection =
2015df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
2016df8bae1dSRodney W. Grimes 			    old_prot;
2017df8bae1dSRodney W. Grimes 		else
2018df8bae1dSRodney W. Grimes 			current->protection = new_prot;
2019df8bae1dSRodney W. Grimes 
2020dd006a1bSAlan Cox 		/*
2021dd006a1bSAlan Cox 		 * For user wired map entries, the normal lazy evaluation of
2022dd006a1bSAlan Cox 		 * write access upgrades through soft page faults is
2023dd006a1bSAlan Cox 		 * undesirable.  Instead, immediately copy any pages that are
2024dd006a1bSAlan Cox 		 * copy-on-write and enable write access in the physical map.
2025dd006a1bSAlan Cox 		 */
2026dd006a1bSAlan Cox 		if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2027210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
20285930251aSKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0)
2029210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
2030210a6886SKonstantin Belousov 
2031df8bae1dSRodney W. Grimes 		/*
20322fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
20332fafce9eSAlan Cox 		 * about copy-on-write here.
2034df8bae1dSRodney W. Grimes 		 */
20352fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
2036afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2037df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
2038df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
2039df8bae1dSRodney W. Grimes 			    current->end,
20401c85e3dfSAlan Cox 			    current->protection & MASK(current));
2041df8bae1dSRodney W. Grimes #undef	MASK
2042df8bae1dSRodney W. Grimes 		}
20437d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
2044df8bae1dSRodney W. Grimes 		current = current->next;
2045df8bae1dSRodney W. Grimes 	}
2046df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2047df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2048df8bae1dSRodney W. Grimes }
2049df8bae1dSRodney W. Grimes 
2050df8bae1dSRodney W. Grimes /*
2051867a482dSJohn Dyson  *	vm_map_madvise:
2052867a482dSJohn Dyson  *
2053867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2054f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2055f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2056f7fc307aSAlan Cox  *	objects.
2057867a482dSJohn Dyson  */
2058b4309055SMatthew Dillon int
20591b40f8c0SMatthew Dillon vm_map_madvise(
20601b40f8c0SMatthew Dillon 	vm_map_t map,
20611b40f8c0SMatthew Dillon 	vm_offset_t start,
20621b40f8c0SMatthew Dillon 	vm_offset_t end,
20631b40f8c0SMatthew Dillon 	int behav)
2064867a482dSJohn Dyson {
2065f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
2066b4309055SMatthew Dillon 	int modify_map = 0;
2067867a482dSJohn Dyson 
2068b4309055SMatthew Dillon 	/*
2069b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2070b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2071b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2072b4309055SMatthew Dillon 	 * on the map.
2073b4309055SMatthew Dillon 	 */
2074b4309055SMatthew Dillon 	switch(behav) {
2075b4309055SMatthew Dillon 	case MADV_NORMAL:
2076b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2077b4309055SMatthew Dillon 	case MADV_RANDOM:
20784f79d873SMatthew Dillon 	case MADV_NOSYNC:
20794f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
20809730a5daSPaul Saab 	case MADV_NOCORE:
20819730a5daSPaul Saab 	case MADV_CORE:
208279e9451fSKonstantin Belousov 		if (start == end)
208379e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2084b4309055SMatthew Dillon 		modify_map = 1;
2085867a482dSJohn Dyson 		vm_map_lock(map);
2086b4309055SMatthew Dillon 		break;
2087b4309055SMatthew Dillon 	case MADV_WILLNEED:
2088b4309055SMatthew Dillon 	case MADV_DONTNEED:
2089b4309055SMatthew Dillon 	case MADV_FREE:
209079e9451fSKonstantin Belousov 		if (start == end)
209179e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2092f7fc307aSAlan Cox 		vm_map_lock_read(map);
2093b4309055SMatthew Dillon 		break;
2094b4309055SMatthew Dillon 	default:
2095b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
2096b4309055SMatthew Dillon 	}
2097b4309055SMatthew Dillon 
2098b4309055SMatthew Dillon 	/*
2099b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2100b4309055SMatthew Dillon 	 */
2101867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2102867a482dSJohn Dyson 
2103867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
2104f7fc307aSAlan Cox 		if (modify_map)
2105867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
2106b4309055SMatthew Dillon 	} else {
2107867a482dSJohn Dyson 		entry = entry->next;
2108b4309055SMatthew Dillon 	}
2109867a482dSJohn Dyson 
2110f7fc307aSAlan Cox 	if (modify_map) {
2111f7fc307aSAlan Cox 		/*
2112f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2113f7fc307aSAlan Cox 		 *
2114f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2115f7fc307aSAlan Cox 		 * limited to the specified address range.
2116f7fc307aSAlan Cox 		 */
2117867a482dSJohn Dyson 		for (current = entry;
2118867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2119b4309055SMatthew Dillon 		     current = current->next
2120b4309055SMatthew Dillon 		) {
2121f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2122867a482dSJohn Dyson 				continue;
2123fed9a903SJohn Dyson 
212447221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2125fed9a903SJohn Dyson 
2126f7fc307aSAlan Cox 			switch (behav) {
2127867a482dSJohn Dyson 			case MADV_NORMAL:
21287f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2129867a482dSJohn Dyson 				break;
2130867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
21317f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2132867a482dSJohn Dyson 				break;
2133867a482dSJohn Dyson 			case MADV_RANDOM:
21347f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2135867a482dSJohn Dyson 				break;
21364f79d873SMatthew Dillon 			case MADV_NOSYNC:
21374f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
21384f79d873SMatthew Dillon 				break;
21394f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
21404f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
21414f79d873SMatthew Dillon 				break;
21429730a5daSPaul Saab 			case MADV_NOCORE:
21439730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
21449730a5daSPaul Saab 				break;
21459730a5daSPaul Saab 			case MADV_CORE:
21469730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
21479730a5daSPaul Saab 				break;
2148867a482dSJohn Dyson 			default:
2149867a482dSJohn Dyson 				break;
2150867a482dSJohn Dyson 			}
2151f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2152867a482dSJohn Dyson 		}
2153867a482dSJohn Dyson 		vm_map_unlock(map);
2154b4309055SMatthew Dillon 	} else {
215592a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
2156f7fc307aSAlan Cox 
2157f7fc307aSAlan Cox 		/*
2158f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2159f7fc307aSAlan Cox 		 * vm_object.
2160f7fc307aSAlan Cox 		 *
2161f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2162f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2163f7fc307aSAlan Cox 		 */
2164f7fc307aSAlan Cox 		for (current = entry;
2165f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2166b4309055SMatthew Dillon 		     current = current->next
2167b4309055SMatthew Dillon 		) {
216851321f7cSAlan Cox 			vm_offset_t useEnd, useStart;
21695f99b57cSMatthew Dillon 
2170f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2171f7fc307aSAlan Cox 				continue;
2172f7fc307aSAlan Cox 
217392a59946SJohn Baldwin 			pstart = OFF_TO_IDX(current->offset);
217492a59946SJohn Baldwin 			pend = pstart + atop(current->end - current->start);
21755f99b57cSMatthew Dillon 			useStart = current->start;
217651321f7cSAlan Cox 			useEnd = current->end;
2177f7fc307aSAlan Cox 
2178f7fc307aSAlan Cox 			if (current->start < start) {
217992a59946SJohn Baldwin 				pstart += atop(start - current->start);
21805f99b57cSMatthew Dillon 				useStart = start;
2181f7fc307aSAlan Cox 			}
218251321f7cSAlan Cox 			if (current->end > end) {
218392a59946SJohn Baldwin 				pend -= atop(current->end - end);
218451321f7cSAlan Cox 				useEnd = end;
218551321f7cSAlan Cox 			}
2186f7fc307aSAlan Cox 
218792a59946SJohn Baldwin 			if (pstart >= pend)
2188f7fc307aSAlan Cox 				continue;
2189f7fc307aSAlan Cox 
219051321f7cSAlan Cox 			/*
219151321f7cSAlan Cox 			 * Perform the pmap_advise() before clearing
219251321f7cSAlan Cox 			 * PGA_REFERENCED in vm_page_advise().  Otherwise, a
219351321f7cSAlan Cox 			 * concurrent pmap operation, such as pmap_remove(),
219451321f7cSAlan Cox 			 * could clear a reference in the pmap and set
219551321f7cSAlan Cox 			 * PGA_REFERENCED on the page before the pmap_advise()
219651321f7cSAlan Cox 			 * had completed.  Consequently, the page would appear
219751321f7cSAlan Cox 			 * referenced based upon an old reference that
219851321f7cSAlan Cox 			 * occurred before this pmap_advise() ran.
219951321f7cSAlan Cox 			 */
220051321f7cSAlan Cox 			if (behav == MADV_DONTNEED || behav == MADV_FREE)
220151321f7cSAlan Cox 				pmap_advise(map->pmap, useStart, useEnd,
220251321f7cSAlan Cox 				    behav);
220351321f7cSAlan Cox 
220492a59946SJohn Baldwin 			vm_object_madvise(current->object.vm_object, pstart,
220592a59946SJohn Baldwin 			    pend, behav);
220654432196SKonstantin Belousov 
220754432196SKonstantin Belousov 			/*
220854432196SKonstantin Belousov 			 * Pre-populate paging structures in the
220954432196SKonstantin Belousov 			 * WILLNEED case.  For wired entries, the
221054432196SKonstantin Belousov 			 * paging structures are already populated.
221154432196SKonstantin Belousov 			 */
221254432196SKonstantin Belousov 			if (behav == MADV_WILLNEED &&
221354432196SKonstantin Belousov 			    current->wired_count == 0) {
22140551c08dSAlan Cox 				vm_map_pmap_enter(map,
22155f99b57cSMatthew Dillon 				    useStart,
22164da4d293SAlan Cox 				    current->protection,
2217f7fc307aSAlan Cox 				    current->object.vm_object,
221892a59946SJohn Baldwin 				    pstart,
221992a59946SJohn Baldwin 				    ptoa(pend - pstart),
2220e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2221b4309055SMatthew Dillon 				);
2222f7fc307aSAlan Cox 			}
2223f7fc307aSAlan Cox 		}
2224f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2225f7fc307aSAlan Cox 	}
2226b4309055SMatthew Dillon 	return (0);
2227867a482dSJohn Dyson }
2228867a482dSJohn Dyson 
2229867a482dSJohn Dyson 
2230867a482dSJohn Dyson /*
2231df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2232df8bae1dSRodney W. Grimes  *
2233df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2234df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2235df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2236e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2237df8bae1dSRodney W. Grimes  */
2238df8bae1dSRodney W. Grimes int
2239b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2240b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2241df8bae1dSRodney W. Grimes {
2242c0877f10SJohn Dyson 	vm_map_entry_t entry;
2243df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2244df8bae1dSRodney W. Grimes 
2245df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2246df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2247df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2248df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
2249df8bae1dSRodney W. Grimes 		break;
2250df8bae1dSRodney W. Grimes 	default:
2251df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2252df8bae1dSRodney W. Grimes 	}
225379e9451fSKonstantin Belousov 	if (start == end)
225479e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2255df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2256df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2257df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2258df8bae1dSRodney W. Grimes 		entry = temp_entry;
2259df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
22600d94caffSDavid Greenman 	} else
2261df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2262df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2263df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2264df8bae1dSRodney W. Grimes 		entry->inheritance = new_inheritance;
226544428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2266df8bae1dSRodney W. Grimes 		entry = entry->next;
2267df8bae1dSRodney W. Grimes 	}
2268df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2269df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2270df8bae1dSRodney W. Grimes }
2271df8bae1dSRodney W. Grimes 
2272df8bae1dSRodney W. Grimes /*
2273acd9a301SAlan Cox  *	vm_map_unwire:
2274acd9a301SAlan Cox  *
2275e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2276acd9a301SAlan Cox  */
2277acd9a301SAlan Cox int
2278acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2279abd498aaSBruce M Simpson     int flags)
2280acd9a301SAlan Cox {
2281acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2282acd9a301SAlan Cox 	vm_offset_t saved_start;
2283acd9a301SAlan Cox 	unsigned int last_timestamp;
2284acd9a301SAlan Cox 	int rv;
2285abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2286acd9a301SAlan Cox 
228779e9451fSKonstantin Belousov 	if (start == end)
228879e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2289abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2290acd9a301SAlan Cox 	vm_map_lock(map);
2291acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2292acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2293abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2294cbef13d8SAlan Cox 			first_entry = first_entry->next;
2295abd498aaSBruce M Simpson 		else {
2296acd9a301SAlan Cox 			vm_map_unlock(map);
2297acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2298acd9a301SAlan Cox 		}
2299abd498aaSBruce M Simpson 	}
2300acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2301acd9a301SAlan Cox 	entry = first_entry;
2302acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2303acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2304acd9a301SAlan Cox 			/*
2305acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2306acd9a301SAlan Cox 			 */
2307acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2308acd9a301SAlan Cox 			    entry->start;
2309acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
23108ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2311acd9a301SAlan Cox 				/*
2312acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2313acd9a301SAlan Cox 				 */
2314acd9a301SAlan Cox 			}
2315acd9a301SAlan Cox 			vm_map_lock(map);
2316acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2317acd9a301SAlan Cox 				/*
2318acd9a301SAlan Cox 				 * Look again for the entry because the map was
2319acd9a301SAlan Cox 				 * modified while it was unlocked.
2320acd9a301SAlan Cox 				 * Specifically, the entry may have been
2321acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2322acd9a301SAlan Cox 				 */
2323acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2324acd9a301SAlan Cox 				    &tmp_entry)) {
2325cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2326cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2327cbef13d8SAlan Cox 					else {
2328acd9a301SAlan Cox 						if (saved_start == start) {
2329acd9a301SAlan Cox 							/*
2330acd9a301SAlan Cox 							 * First_entry has been deleted.
2331acd9a301SAlan Cox 							 */
2332acd9a301SAlan Cox 							vm_map_unlock(map);
2333acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2334acd9a301SAlan Cox 						}
2335acd9a301SAlan Cox 						end = saved_start;
2336acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2337acd9a301SAlan Cox 						goto done;
2338acd9a301SAlan Cox 					}
2339cbef13d8SAlan Cox 				}
2340acd9a301SAlan Cox 				if (entry == first_entry)
2341acd9a301SAlan Cox 					first_entry = tmp_entry;
2342acd9a301SAlan Cox 				else
2343acd9a301SAlan Cox 					first_entry = NULL;
2344acd9a301SAlan Cox 				entry = tmp_entry;
2345acd9a301SAlan Cox 			}
2346acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2347acd9a301SAlan Cox 			continue;
2348acd9a301SAlan Cox 		}
2349acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2350acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2351acd9a301SAlan Cox 		/*
2352acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2353acd9a301SAlan Cox 		 * above.)
2354acd9a301SAlan Cox 		 */
2355ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2356ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2357ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
2358acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
23590acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2360acd9a301SAlan Cox 		/*
2361acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2362abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2363acd9a301SAlan Cox 		 */
2364abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2365abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2366abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2367acd9a301SAlan Cox 			end = entry->end;
2368acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2369acd9a301SAlan Cox 			goto done;
2370acd9a301SAlan Cox 		}
2371acd9a301SAlan Cox 		/*
23723ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2373acd9a301SAlan Cox 		 */
23740ada205eSBrian Feldman 		if (!user_unwire &&
23750ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2376acd9a301SAlan Cox 			end = entry->end;
2377acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2378acd9a301SAlan Cox 			goto done;
2379acd9a301SAlan Cox 		}
2380acd9a301SAlan Cox 		entry = entry->next;
2381acd9a301SAlan Cox 	}
2382acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2383acd9a301SAlan Cox done:
2384e27e17b7SAlan Cox 	need_wakeup = FALSE;
2385acd9a301SAlan Cox 	if (first_entry == NULL) {
2386acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2387cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2388cbef13d8SAlan Cox 			first_entry = first_entry->next;
2389cbef13d8SAlan Cox 		else
2390acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2391acd9a301SAlan Cox 	}
23920acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
23930acea7dfSKonstantin Belousov 	    entry = entry->next) {
23940acea7dfSKonstantin Belousov 		/*
23950acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
23960acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
23970acea7dfSKonstantin Belousov 		 * while the map lock was dropped for draining
23980acea7dfSKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
23990acea7dfSKonstantin Belousov 		 * could be simultaneously wiring this new mapping
24000acea7dfSKonstantin Belousov 		 * entry.  Detect these cases and skip any entries
24010acea7dfSKonstantin Belousov 		 * marked as in transition by us.
24020acea7dfSKonstantin Belousov 		 */
24030acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
24040acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
24050acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
24060acea7dfSKonstantin Belousov 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
24070acea7dfSKonstantin Belousov 			continue;
24080acea7dfSKonstantin Belousov 		}
24090acea7dfSKonstantin Belousov 
24103ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
24113ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2412b2f3846aSAlan Cox 			if (user_unwire)
2413b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
241403462509SAlan Cox 			if (entry->wired_count == 1)
241503462509SAlan Cox 				vm_map_entry_unwire(map, entry);
241603462509SAlan Cox 			else
2417b2f3846aSAlan Cox 				entry->wired_count--;
2418b2f3846aSAlan Cox 		}
24190acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2420ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: in-transition flag missing %p", entry));
2421ff3ae454SKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
2422ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: alien wire %p", entry));
2423acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
24240acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
2425acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2426acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2427acd9a301SAlan Cox 			need_wakeup = TRUE;
2428acd9a301SAlan Cox 		}
2429acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2430acd9a301SAlan Cox 	}
2431acd9a301SAlan Cox 	vm_map_unlock(map);
2432acd9a301SAlan Cox 	if (need_wakeup)
2433acd9a301SAlan Cox 		vm_map_wakeup(map);
2434acd9a301SAlan Cox 	return (rv);
2435acd9a301SAlan Cox }
2436acd9a301SAlan Cox 
2437acd9a301SAlan Cox /*
243866cd575bSAlan Cox  *	vm_map_wire_entry_failure:
243966cd575bSAlan Cox  *
244066cd575bSAlan Cox  *	Handle a wiring failure on the given entry.
244166cd575bSAlan Cox  *
244266cd575bSAlan Cox  *	The map should be locked.
244366cd575bSAlan Cox  */
244466cd575bSAlan Cox static void
244566cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
244666cd575bSAlan Cox     vm_offset_t failed_addr)
244766cd575bSAlan Cox {
244866cd575bSAlan Cox 
244966cd575bSAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
245066cd575bSAlan Cox 	KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
245166cd575bSAlan Cox 	    entry->wired_count == 1,
245266cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
245366cd575bSAlan Cox 	KASSERT(failed_addr < entry->end,
245466cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
245566cd575bSAlan Cox 
245666cd575bSAlan Cox 	/*
245766cd575bSAlan Cox 	 * If any pages at the start of this entry were successfully wired,
245866cd575bSAlan Cox 	 * then unwire them.
245966cd575bSAlan Cox 	 */
246066cd575bSAlan Cox 	if (failed_addr > entry->start) {
246166cd575bSAlan Cox 		pmap_unwire(map->pmap, entry->start, failed_addr);
246266cd575bSAlan Cox 		vm_object_unwire(entry->object.vm_object, entry->offset,
246366cd575bSAlan Cox 		    failed_addr - entry->start, PQ_ACTIVE);
246466cd575bSAlan Cox 	}
246566cd575bSAlan Cox 
246666cd575bSAlan Cox 	/*
246766cd575bSAlan Cox 	 * Assign an out-of-range value to represent the failure to wire this
246866cd575bSAlan Cox 	 * entry.
246966cd575bSAlan Cox 	 */
247066cd575bSAlan Cox 	entry->wired_count = -1;
247166cd575bSAlan Cox }
247266cd575bSAlan Cox 
247366cd575bSAlan Cox /*
2474e27e17b7SAlan Cox  *	vm_map_wire:
2475e27e17b7SAlan Cox  *
2476e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2477e27e17b7SAlan Cox  */
2478e27e17b7SAlan Cox int
2479e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2480abd498aaSBruce M Simpson     int flags)
2481e27e17b7SAlan Cox {
248212d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
248366cd575bSAlan Cox 	vm_offset_t faddr, saved_end, saved_start;
248412d7cc84SAlan Cox 	unsigned int last_timestamp;
248512d7cc84SAlan Cox 	int rv;
248666cd575bSAlan Cox 	boolean_t need_wakeup, result, user_wire;
2487e4cd31ddSJeff Roberson 	vm_prot_t prot;
2488e27e17b7SAlan Cox 
248979e9451fSKonstantin Belousov 	if (start == end)
249079e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2491e4cd31ddSJeff Roberson 	prot = 0;
2492e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
2493e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
2494abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
249512d7cc84SAlan Cox 	vm_map_lock(map);
249612d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
249712d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2498abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2499cbef13d8SAlan Cox 			first_entry = first_entry->next;
2500abd498aaSBruce M Simpson 		else {
250112d7cc84SAlan Cox 			vm_map_unlock(map);
250212d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
250312d7cc84SAlan Cox 		}
2504abd498aaSBruce M Simpson 	}
250512d7cc84SAlan Cox 	last_timestamp = map->timestamp;
250612d7cc84SAlan Cox 	entry = first_entry;
250712d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
250812d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
250912d7cc84SAlan Cox 			/*
251012d7cc84SAlan Cox 			 * We have not yet clipped the entry.
251112d7cc84SAlan Cox 			 */
251212d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
251312d7cc84SAlan Cox 			    entry->start;
251412d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
25158ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
251612d7cc84SAlan Cox 				/*
251712d7cc84SAlan Cox 				 * Allow interruption of user wiring?
251812d7cc84SAlan Cox 				 */
251912d7cc84SAlan Cox 			}
252012d7cc84SAlan Cox 			vm_map_lock(map);
252112d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
252212d7cc84SAlan Cox 				/*
252312d7cc84SAlan Cox 				 * Look again for the entry because the map was
252412d7cc84SAlan Cox 				 * modified while it was unlocked.
252512d7cc84SAlan Cox 				 * Specifically, the entry may have been
252612d7cc84SAlan Cox 				 * clipped, merged, or deleted.
252712d7cc84SAlan Cox 				 */
252812d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
252912d7cc84SAlan Cox 				    &tmp_entry)) {
2530cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2531cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2532cbef13d8SAlan Cox 					else {
253312d7cc84SAlan Cox 						if (saved_start == start) {
253412d7cc84SAlan Cox 							/*
253512d7cc84SAlan Cox 							 * first_entry has been deleted.
253612d7cc84SAlan Cox 							 */
253712d7cc84SAlan Cox 							vm_map_unlock(map);
253812d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
253912d7cc84SAlan Cox 						}
254012d7cc84SAlan Cox 						end = saved_start;
254112d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
254212d7cc84SAlan Cox 						goto done;
254312d7cc84SAlan Cox 					}
2544cbef13d8SAlan Cox 				}
254512d7cc84SAlan Cox 				if (entry == first_entry)
254612d7cc84SAlan Cox 					first_entry = tmp_entry;
254712d7cc84SAlan Cox 				else
254812d7cc84SAlan Cox 					first_entry = NULL;
254912d7cc84SAlan Cox 				entry = tmp_entry;
255012d7cc84SAlan Cox 			}
255112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
255212d7cc84SAlan Cox 			continue;
255312d7cc84SAlan Cox 		}
255412d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
255512d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
255612d7cc84SAlan Cox 		/*
255712d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
255812d7cc84SAlan Cox 		 * above.)
255912d7cc84SAlan Cox 		 */
2560ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2561ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2562ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
256312d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
25640acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2565e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2566e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
2567529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
25686d7e8091SKonstantin Belousov 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
25696d7e8091SKonstantin Belousov 				end = entry->end;
25706d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
25716d7e8091SKonstantin Belousov 				goto done;
25726d7e8091SKonstantin Belousov 			}
25736d7e8091SKonstantin Belousov 			goto next_entry;
25746d7e8091SKonstantin Belousov 		}
2575e4cd31ddSJeff Roberson 		if (entry->wired_count == 0) {
25760ada205eSBrian Feldman 			entry->wired_count++;
257712d7cc84SAlan Cox 			saved_start = entry->start;
257812d7cc84SAlan Cox 			saved_end = entry->end;
257966cd575bSAlan Cox 
258012d7cc84SAlan Cox 			/*
258112d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
2582a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
258312d7cc84SAlan Cox 			 */
2584a5db445dSMax Laier 			vm_map_busy(map);
258512d7cc84SAlan Cox 			vm_map_unlock(map);
258666cd575bSAlan Cox 
25870b695684SAlan Cox 			faddr = saved_start;
25880b695684SAlan Cox 			do {
258966cd575bSAlan Cox 				/*
259066cd575bSAlan Cox 				 * Simulate a fault to get the page and enter
259166cd575bSAlan Cox 				 * it into the physical map.
259266cd575bSAlan Cox 				 */
259366cd575bSAlan Cox 				if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
259466cd575bSAlan Cox 				    VM_FAULT_CHANGE_WIRING)) != KERN_SUCCESS)
259566cd575bSAlan Cox 					break;
25960b695684SAlan Cox 			} while ((faddr += PAGE_SIZE) < saved_end);
259712d7cc84SAlan Cox 			vm_map_lock(map);
2598a5db445dSMax Laier 			vm_map_unbusy(map);
259912d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
260012d7cc84SAlan Cox 				/*
260112d7cc84SAlan Cox 				 * Look again for the entry because the map was
260212d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
260312d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
260412d7cc84SAlan Cox 				 * deleted.
260512d7cc84SAlan Cox 				 */
260612d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
260712d7cc84SAlan Cox 				    &tmp_entry);
260812d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
260912d7cc84SAlan Cox 				if (entry == first_entry)
261012d7cc84SAlan Cox 					first_entry = tmp_entry;
261112d7cc84SAlan Cox 				else
261212d7cc84SAlan Cox 					first_entry = NULL;
261312d7cc84SAlan Cox 				entry = tmp_entry;
261428c58286SAlan Cox 				while (entry->end < saved_end) {
261566cd575bSAlan Cox 					/*
261666cd575bSAlan Cox 					 * In case of failure, handle entries
261766cd575bSAlan Cox 					 * that were not fully wired here;
261866cd575bSAlan Cox 					 * fully wired entries are handled
261966cd575bSAlan Cox 					 * later.
262066cd575bSAlan Cox 					 */
262166cd575bSAlan Cox 					if (rv != KERN_SUCCESS &&
262266cd575bSAlan Cox 					    faddr < entry->end)
262366cd575bSAlan Cox 						vm_map_wire_entry_failure(map,
262466cd575bSAlan Cox 						    entry, faddr);
262512d7cc84SAlan Cox 					entry = entry->next;
262612d7cc84SAlan Cox 				}
262728c58286SAlan Cox 			}
262812d7cc84SAlan Cox 			last_timestamp = map->timestamp;
262912d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
263066cd575bSAlan Cox 				vm_map_wire_entry_failure(map, entry, faddr);
263112d7cc84SAlan Cox 				end = entry->end;
263212d7cc84SAlan Cox 				goto done;
263312d7cc84SAlan Cox 			}
26340ada205eSBrian Feldman 		} else if (!user_wire ||
26350ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
26360ada205eSBrian Feldman 			entry->wired_count++;
263712d7cc84SAlan Cox 		}
263812d7cc84SAlan Cox 		/*
263912d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2640abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
264112d7cc84SAlan Cox 		 */
26426d7e8091SKonstantin Belousov 	next_entry:
2643abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2644abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2645abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
264612d7cc84SAlan Cox 			end = entry->end;
264712d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
264812d7cc84SAlan Cox 			goto done;
264912d7cc84SAlan Cox 		}
265012d7cc84SAlan Cox 		entry = entry->next;
265112d7cc84SAlan Cox 	}
265212d7cc84SAlan Cox 	rv = KERN_SUCCESS;
265312d7cc84SAlan Cox done:
265412d7cc84SAlan Cox 	need_wakeup = FALSE;
265512d7cc84SAlan Cox 	if (first_entry == NULL) {
265612d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2657cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2658cbef13d8SAlan Cox 			first_entry = first_entry->next;
2659cbef13d8SAlan Cox 		else
266012d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
266112d7cc84SAlan Cox 	}
26620acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
26630acea7dfSKonstantin Belousov 	    entry = entry->next) {
26646d7e8091SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
26656d7e8091SKonstantin Belousov 			goto next_entry_done;
26660acea7dfSKonstantin Belousov 
26670acea7dfSKonstantin Belousov 		/*
26680acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
26690acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
26700acea7dfSKonstantin Belousov 		 * while the map lock was dropped for faulting in the
26710acea7dfSKonstantin Belousov 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
26720acea7dfSKonstantin Belousov 		 * Moreover, another thread could be simultaneously
26730acea7dfSKonstantin Belousov 		 * wiring this new mapping entry.  Detect these cases
26740acea7dfSKonstantin Belousov 		 * and skip any entries marked as in transition by us.
26750acea7dfSKonstantin Belousov 		 */
26760acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
26770acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
26780acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
26790acea7dfSKonstantin Belousov 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
26800acea7dfSKonstantin Belousov 			continue;
26810acea7dfSKonstantin Belousov 		}
26820acea7dfSKonstantin Belousov 
268312d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
268412d7cc84SAlan Cox 			if (user_wire)
268512d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
268628c58286SAlan Cox 		} else if (entry->wired_count == -1) {
268728c58286SAlan Cox 			/*
268828c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
268928c58286SAlan Cox 			 * unnecessary.
269028c58286SAlan Cox 			 */
269128c58286SAlan Cox 			entry->wired_count = 0;
269203462509SAlan Cox 		} else if (!user_wire ||
269303462509SAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
269466cd575bSAlan Cox 			/*
269566cd575bSAlan Cox 			 * Undo the wiring.  Wiring succeeded on this entry
269666cd575bSAlan Cox 			 * but failed on a later entry.
269766cd575bSAlan Cox 			 */
269803462509SAlan Cox 			if (entry->wired_count == 1)
269903462509SAlan Cox 				vm_map_entry_unwire(map, entry);
270003462509SAlan Cox 			else
270112d7cc84SAlan Cox 				entry->wired_count--;
270212d7cc84SAlan Cox 		}
27036d7e8091SKonstantin Belousov 	next_entry_done:
27040acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
27050acea7dfSKonstantin Belousov 		    ("vm_map_wire: in-transition flag missing %p", entry));
27060acea7dfSKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
27070acea7dfSKonstantin Belousov 		    ("vm_map_wire: alien wire %p", entry));
27080acea7dfSKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
27090acea7dfSKonstantin Belousov 		    MAP_ENTRY_WIRE_SKIPPED);
27100acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
271112d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
271212d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
271312d7cc84SAlan Cox 			need_wakeup = TRUE;
271412d7cc84SAlan Cox 		}
271512d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
271612d7cc84SAlan Cox 	}
271712d7cc84SAlan Cox 	vm_map_unlock(map);
271812d7cc84SAlan Cox 	if (need_wakeup)
271912d7cc84SAlan Cox 		vm_map_wakeup(map);
272012d7cc84SAlan Cox 	return (rv);
2721e27e17b7SAlan Cox }
2722e27e17b7SAlan Cox 
2723e27e17b7SAlan Cox /*
2724950f8459SAlan Cox  * vm_map_sync
2725df8bae1dSRodney W. Grimes  *
2726df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2727df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2728df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2729df8bae1dSRodney W. Grimes  *
2730637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2731637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2732637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2733637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2734637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2735637315edSAlan Cox  * flushing the current region containing start.
2736637315edSAlan Cox  *
2737df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2738df8bae1dSRodney W. Grimes  */
2739df8bae1dSRodney W. Grimes int
2740950f8459SAlan Cox vm_map_sync(
27411b40f8c0SMatthew Dillon 	vm_map_t map,
27421b40f8c0SMatthew Dillon 	vm_offset_t start,
27431b40f8c0SMatthew Dillon 	vm_offset_t end,
27441b40f8c0SMatthew Dillon 	boolean_t syncio,
27451b40f8c0SMatthew Dillon 	boolean_t invalidate)
2746df8bae1dSRodney W. Grimes {
2747c0877f10SJohn Dyson 	vm_map_entry_t current;
2748df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2749df8bae1dSRodney W. Grimes 	vm_size_t size;
2750df8bae1dSRodney W. Grimes 	vm_object_t object;
2751a316d390SJohn Dyson 	vm_ooffset_t offset;
2752e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2753126d6082SKonstantin Belousov 	boolean_t failed;
2754df8bae1dSRodney W. Grimes 
2755df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2756df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2757df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2758df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2759df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2760637315edSAlan Cox 	} else if (start == end) {
2761637315edSAlan Cox 		start = entry->start;
2762637315edSAlan Cox 		end = entry->end;
2763df8bae1dSRodney W. Grimes 	}
2764df8bae1dSRodney W. Grimes 	/*
2765b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2766df8bae1dSRodney W. Grimes 	 */
27677b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
27687b0e72d1SAlan Cox 	    current = current->next) {
2769b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2770df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2771df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2772df8bae1dSRodney W. Grimes 		}
2773df8bae1dSRodney W. Grimes 		if (end > current->end &&
2774df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2775df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2776df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2777df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2778df8bae1dSRodney W. Grimes 		}
2779df8bae1dSRodney W. Grimes 	}
2780df8bae1dSRodney W. Grimes 
27812cf13952SAlan Cox 	if (invalidate)
2782bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2783126d6082SKonstantin Belousov 	failed = FALSE;
27842cf13952SAlan Cox 
2785df8bae1dSRodney W. Grimes 	/*
2786df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2787df8bae1dSRodney W. Grimes 	 * objects as we go.
2788df8bae1dSRodney W. Grimes 	 */
2789e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2790df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2791df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
27929fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2793c0877f10SJohn Dyson 			vm_map_t smap;
2794df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2795df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2796df8bae1dSRodney W. Grimes 
27979fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2798df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2799df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2800df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2801df8bae1dSRodney W. Grimes 			if (tsize < size)
2802df8bae1dSRodney W. Grimes 				size = tsize;
2803df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2804df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2805df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2806df8bae1dSRodney W. Grimes 		} else {
2807df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2808df8bae1dSRodney W. Grimes 		}
2809e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2810e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2811e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2812126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
2813126d6082SKonstantin Belousov 			failed = TRUE;
2814df8bae1dSRodney W. Grimes 		start += size;
2815e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2816e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2817e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2818e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2819e53fa61bSKonstantin Belousov 			current = current->next;
2820df8bae1dSRodney W. Grimes 	}
2821df8bae1dSRodney W. Grimes 
2822df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2823126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
2824df8bae1dSRodney W. Grimes }
2825df8bae1dSRodney W. Grimes 
2826df8bae1dSRodney W. Grimes /*
2827df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2828df8bae1dSRodney W. Grimes  *
2829df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2830df8bae1dSRodney W. Grimes  *
2831df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2832df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2833df8bae1dSRodney W. Grimes  */
28340362d7d7SJohn Dyson static void
28351b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2836df8bae1dSRodney W. Grimes {
283703462509SAlan Cox 
283803462509SAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
283903462509SAlan Cox 	KASSERT(entry->wired_count > 0,
284003462509SAlan Cox 	    ("vm_map_entry_unwire: entry %p isn't wired", entry));
284103462509SAlan Cox 	pmap_unwire(map->pmap, entry->start, entry->end);
284203462509SAlan Cox 	vm_object_unwire(entry->object.vm_object, entry->offset, entry->end -
284303462509SAlan Cox 	    entry->start, PQ_ACTIVE);
2844df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
2845df8bae1dSRodney W. Grimes }
2846df8bae1dSRodney W. Grimes 
28470b367bd8SKonstantin Belousov static void
28480b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
28490b367bd8SKonstantin Belousov {
28500b367bd8SKonstantin Belousov 
28510b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
28520b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
28530b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
28540b367bd8SKonstantin Belousov }
28550b367bd8SKonstantin Belousov 
2856df8bae1dSRodney W. Grimes /*
2857df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
2858df8bae1dSRodney W. Grimes  *
2859df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
2860df8bae1dSRodney W. Grimes  */
28610362d7d7SJohn Dyson static void
28621b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
2863df8bae1dSRodney W. Grimes {
286432a89c32SAlan Cox 	vm_object_t object;
28653364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
28663364c323SKonstantin Belousov 	vm_ooffset_t size;
286732a89c32SAlan Cox 
2868df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
28693364c323SKonstantin Belousov 	object = entry->object.vm_object;
28703364c323SKonstantin Belousov 	size = entry->end - entry->start;
28713364c323SKonstantin Belousov 	map->size -= size;
28723364c323SKonstantin Belousov 
2873ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
2874ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
2875ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
28763364c323SKonstantin Belousov 	}
2877df8bae1dSRodney W. Grimes 
287832a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
28793364c323SKonstantin Belousov 	    (object != NULL)) {
2880ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
28813364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
2882ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
28833364c323SKonstantin Belousov 		count = OFF_TO_IDX(size);
288432a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
288532a89c32SAlan Cox 		offidxend = offidxstart + count;
288689f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
288732a89c32SAlan Cox 		if (object->ref_count != 1 &&
288832a89c32SAlan Cox 		    ((object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
28899f5c801bSAlan Cox 		    object == kernel_object || object == kmem_object)) {
289032a89c32SAlan Cox 			vm_object_collapse(object);
28916bbee8e2SAlan Cox 
28926bbee8e2SAlan Cox 			/*
28936bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
28946bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
28956bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
28966bbee8e2SAlan Cox 			 * of pages.
28976bbee8e2SAlan Cox 			 */
28986bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
28996bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
290032a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
290132a89c32SAlan Cox 				swap_pager_freespace(object, offidxstart, count);
290232a89c32SAlan Cox 			if (offidxend >= object->size &&
29033364c323SKonstantin Belousov 			    offidxstart < object->size) {
29043364c323SKonstantin Belousov 				size1 = object->size;
290532a89c32SAlan Cox 				object->size = offidxstart;
2906ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
29073364c323SKonstantin Belousov 					size1 -= object->size;
29083364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
29093364c323SKonstantin Belousov 					    ("vm_map_entry_delete: object->charge < 0"));
2910ef694c1aSEdward Tomasz Napierala 					swap_release_by_cred(ptoa(size1), object->cred);
29113364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
29123364c323SKonstantin Belousov 				}
29133364c323SKonstantin Belousov 			}
291432a89c32SAlan Cox 		}
291589f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
2916897d81a0SKonstantin Belousov 	} else
2917897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
29180b367bd8SKonstantin Belousov 	if (map->system_map)
29190b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
29200b367bd8SKonstantin Belousov 	else {
29210b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
29220b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
29230b367bd8SKonstantin Belousov 	}
2924df8bae1dSRodney W. Grimes }
2925df8bae1dSRodney W. Grimes 
2926df8bae1dSRodney W. Grimes /*
2927df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
2928df8bae1dSRodney W. Grimes  *
2929df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
2930df8bae1dSRodney W. Grimes  *	map.
2931df8bae1dSRodney W. Grimes  */
2932df8bae1dSRodney W. Grimes int
2933655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
2934df8bae1dSRodney W. Grimes {
2935c0877f10SJohn Dyson 	vm_map_entry_t entry;
2936df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
2937df8bae1dSRodney W. Grimes 
29383a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
293979e9451fSKonstantin Belousov 	if (start == end)
294079e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
29413a0916b8SKonstantin Belousov 
2942df8bae1dSRodney W. Grimes 	/*
2943df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
2944df8bae1dSRodney W. Grimes 	 */
2945876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
2946df8bae1dSRodney W. Grimes 		entry = first_entry->next;
2947876318ecSAlan Cox 	else {
2948df8bae1dSRodney W. Grimes 		entry = first_entry;
2949df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2950df8bae1dSRodney W. Grimes 	}
2951df8bae1dSRodney W. Grimes 
2952df8bae1dSRodney W. Grimes 	/*
2953df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
2954df8bae1dSRodney W. Grimes 	 */
2955df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2956df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
2957df8bae1dSRodney W. Grimes 
295873b2baceSAlan Cox 		/*
295973b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
29607c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
29617c938963SBrian Feldman 		 * user maps.
296273b2baceSAlan Cox 		 */
29637c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
29647c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
29657c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
296673b2baceSAlan Cox 			unsigned int last_timestamp;
296773b2baceSAlan Cox 			vm_offset_t saved_start;
296873b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
296973b2baceSAlan Cox 
297073b2baceSAlan Cox 			saved_start = entry->start;
297173b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
297273b2baceSAlan Cox 			last_timestamp = map->timestamp;
29738ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
297473b2baceSAlan Cox 			vm_map_lock(map);
297573b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
297673b2baceSAlan Cox 				/*
297773b2baceSAlan Cox 				 * Look again for the entry because the map was
297873b2baceSAlan Cox 				 * modified while it was unlocked.
297973b2baceSAlan Cox 				 * Specifically, the entry may have been
298073b2baceSAlan Cox 				 * clipped, merged, or deleted.
298173b2baceSAlan Cox 				 */
298273b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
298373b2baceSAlan Cox 							 &tmp_entry))
298473b2baceSAlan Cox 					entry = tmp_entry->next;
298573b2baceSAlan Cox 				else {
298673b2baceSAlan Cox 					entry = tmp_entry;
298773b2baceSAlan Cox 					vm_map_clip_start(map, entry,
298873b2baceSAlan Cox 							  saved_start);
298973b2baceSAlan Cox 				}
299073b2baceSAlan Cox 			}
299173b2baceSAlan Cox 			continue;
299273b2baceSAlan Cox 		}
2993df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
2994df8bae1dSRodney W. Grimes 
2995c0877f10SJohn Dyson 		next = entry->next;
2996df8bae1dSRodney W. Grimes 
2997df8bae1dSRodney W. Grimes 		/*
29980d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
29990d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
3000df8bae1dSRodney W. Grimes 		 */
3001c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
3002df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
3003c0877f10SJohn Dyson 		}
3004df8bae1dSRodney W. Grimes 
300532a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
3006df8bae1dSRodney W. Grimes 
3007df8bae1dSRodney W. Grimes 		/*
3008e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
3009e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
3010e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
3011e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
3012df8bae1dSRodney W. Grimes 		 */
3013df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
3014df8bae1dSRodney W. Grimes 		entry = next;
3015df8bae1dSRodney W. Grimes 	}
3016df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3017df8bae1dSRodney W. Grimes }
3018df8bae1dSRodney W. Grimes 
3019df8bae1dSRodney W. Grimes /*
3020df8bae1dSRodney W. Grimes  *	vm_map_remove:
3021df8bae1dSRodney W. Grimes  *
3022df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
3023df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
3024df8bae1dSRodney W. Grimes  */
3025df8bae1dSRodney W. Grimes int
30261b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3027df8bae1dSRodney W. Grimes {
30286eaee3feSAlan Cox 	int result;
3029df8bae1dSRodney W. Grimes 
3030df8bae1dSRodney W. Grimes 	vm_map_lock(map);
3031df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
3032655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
3033df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
3034df8bae1dSRodney W. Grimes 	return (result);
3035df8bae1dSRodney W. Grimes }
3036df8bae1dSRodney W. Grimes 
3037df8bae1dSRodney W. Grimes /*
3038df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
3039df8bae1dSRodney W. Grimes  *
30402d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
30412d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
30422d5c7e45SMatthew Dillon  *
30432d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
30442d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
30452d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
30462d5c7e45SMatthew Dillon  *
30472d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
3048d8834602SAlan Cox  *
3049d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
3050df8bae1dSRodney W. Grimes  */
30510d94caffSDavid Greenman boolean_t
3052b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3053b9dcd593SBruce Evans 			vm_prot_t protection)
3054df8bae1dSRodney W. Grimes {
3055c0877f10SJohn Dyson 	vm_map_entry_t entry;
3056df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
3057df8bae1dSRodney W. Grimes 
3058d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
3059df8bae1dSRodney W. Grimes 		return (FALSE);
3060df8bae1dSRodney W. Grimes 	entry = tmp_entry;
3061df8bae1dSRodney W. Grimes 
3062df8bae1dSRodney W. Grimes 	while (start < end) {
3063d8834602SAlan Cox 		if (entry == &map->header)
3064df8bae1dSRodney W. Grimes 			return (FALSE);
3065df8bae1dSRodney W. Grimes 		/*
3066df8bae1dSRodney W. Grimes 		 * No holes allowed!
3067df8bae1dSRodney W. Grimes 		 */
3068d8834602SAlan Cox 		if (start < entry->start)
3069df8bae1dSRodney W. Grimes 			return (FALSE);
3070df8bae1dSRodney W. Grimes 		/*
3071df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
3072df8bae1dSRodney W. Grimes 		 */
3073d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
3074df8bae1dSRodney W. Grimes 			return (FALSE);
3075df8bae1dSRodney W. Grimes 		/* go to next entry */
3076df8bae1dSRodney W. Grimes 		start = entry->end;
3077df8bae1dSRodney W. Grimes 		entry = entry->next;
3078df8bae1dSRodney W. Grimes 	}
3079df8bae1dSRodney W. Grimes 	return (TRUE);
3080df8bae1dSRodney W. Grimes }
3081df8bae1dSRodney W. Grimes 
308286524867SJohn Dyson /*
3083df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
3084df8bae1dSRodney W. Grimes  *
3085df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
3086df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
3087df8bae1dSRodney W. Grimes  */
3088f708ef1bSPoul-Henning Kamp static void
30891b40f8c0SMatthew Dillon vm_map_copy_entry(
30901b40f8c0SMatthew Dillon 	vm_map_t src_map,
30911b40f8c0SMatthew Dillon 	vm_map_t dst_map,
30921b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
30933364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
30943364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
3095df8bae1dSRodney W. Grimes {
3096c0877f10SJohn Dyson 	vm_object_t src_object;
309784110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
30983364c323SKonstantin Belousov 	vm_offset_t size;
3099ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
31003364c323SKonstantin Belousov 	int charged;
3101c0877f10SJohn Dyson 
31023a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
31033a0916b8SKonstantin Belousov 
31049fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3105df8bae1dSRodney W. Grimes 		return;
3106df8bae1dSRodney W. Grimes 
3107afaa41f6SAlan Cox 	if (src_entry->wired_count == 0 ||
3108afaa41f6SAlan Cox 	    (src_entry->protection & VM_PROT_WRITE) == 0) {
3109df8bae1dSRodney W. Grimes 		/*
31100d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
31110d94caffSDavid Greenman 		 * write-protected.
3112df8bae1dSRodney W. Grimes 		 */
3113d9a9209aSAlan Cox 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3114d9a9209aSAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) != 0) {
3115df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
3116df8bae1dSRodney W. Grimes 			    src_entry->start,
3117df8bae1dSRodney W. Grimes 			    src_entry->end,
3118df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
3119df8bae1dSRodney W. Grimes 		}
3120b18bfc3dSJohn Dyson 
3121df8bae1dSRodney W. Grimes 		/*
3122df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
3123df8bae1dSRodney W. Grimes 		 */
31243364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
31258aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
312689f6b863SAttilio Rao 			VM_OBJECT_WLOCK(src_object);
31273364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
3128c0877f10SJohn Dyson 			if ((src_object->handle == NULL) &&
3129c0877f10SJohn Dyson 				(src_object->type == OBJT_DEFAULT ||
3130c0877f10SJohn Dyson 				 src_object->type == OBJT_SWAP)) {
3131c0877f10SJohn Dyson 				vm_object_collapse(src_object);
313296fb8cf2SJohn Dyson 				if ((src_object->flags & (OBJ_NOSPLIT|OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3133c5aaa06dSAlan Cox 					vm_object_split(src_entry);
3134c0877f10SJohn Dyson 					src_object = src_entry->object.vm_object;
3135a89c6258SAlan Cox 				}
3136a89c6258SAlan Cox 			}
3137b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
3138069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3139ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL &&
31403364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3141ef694c1aSEdward Tomasz Napierala 				KASSERT(src_object->cred == NULL,
3142ef694c1aSEdward Tomasz Napierala 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
31433364c323SKonstantin Belousov 				     src_object));
3144ef694c1aSEdward Tomasz Napierala 				src_object->cred = src_entry->cred;
31453364c323SKonstantin Belousov 				src_object->charge = size;
31463364c323SKonstantin Belousov 			}
314789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(src_object);
3148c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
31493364c323SKonstantin Belousov 			if (charged) {
3150ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3151ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3152ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = cred;
31533364c323SKonstantin Belousov 				*fork_charge += size;
31543364c323SKonstantin Belousov 				if (!(src_entry->eflags &
31553364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
3156ef694c1aSEdward Tomasz Napierala 					crhold(cred);
3157ef694c1aSEdward Tomasz Napierala 					src_entry->cred = cred;
31583364c323SKonstantin Belousov 					*fork_charge += size;
31593364c323SKonstantin Belousov 				}
31603364c323SKonstantin Belousov 			}
3161afa07f7eSJohn Dyson 			src_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3162afa07f7eSJohn Dyson 			dst_entry->eflags |= (MAP_ENTRY_COW|MAP_ENTRY_NEEDS_COPY);
3163b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
316484110e7eSKonstantin Belousov 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
316584110e7eSKonstantin Belousov 				/*
316684110e7eSKonstantin Belousov 				 * MAP_ENTRY_VN_WRITECNT cannot
316784110e7eSKonstantin Belousov 				 * indicate write reference from
316884110e7eSKonstantin Belousov 				 * src_entry, since the entry is
316984110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
317084110e7eSKonstantin Belousov 				 * fake entry that is used to
317184110e7eSKonstantin Belousov 				 * decrement object->un_pager.vnp.writecount
317284110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
317384110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
317484110e7eSKonstantin Belousov 				 */
317584110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
317684110e7eSKonstantin Belousov 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
317784110e7eSKonstantin Belousov 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
317884110e7eSKonstantin Belousov 				vm_object_reference(src_object);
317984110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
318084110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
318184110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
318284110e7eSKonstantin Belousov 				fake_entry->next = curthread->td_map_def_user;
318384110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
318484110e7eSKonstantin Belousov 			}
3185b18bfc3dSJohn Dyson 		} else {
3186b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
3187b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
3188ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
3189ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
3190ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
31913364c323SKonstantin Belousov 				*fork_charge += size;
31923364c323SKonstantin Belousov 			}
3193b18bfc3dSJohn Dyson 		}
3194df8bae1dSRodney W. Grimes 
3195df8bae1dSRodney W. Grimes 		pmap_copy(dst_map->pmap, src_map->pmap, dst_entry->start,
3196df8bae1dSRodney W. Grimes 		    dst_entry->end - dst_entry->start, src_entry->start);
31970d94caffSDavid Greenman 	} else {
3198df8bae1dSRodney W. Grimes 		/*
3199afaa41f6SAlan Cox 		 * We don't want to make writeable wired pages copy-on-write.
3200afaa41f6SAlan Cox 		 * Immediately copy these pages into the new map by simulating
3201afaa41f6SAlan Cox 		 * page faults.  The new pages are pageable.
3202df8bae1dSRodney W. Grimes 		 */
3203121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3204121fd461SKonstantin Belousov 		    fork_charge);
3205df8bae1dSRodney W. Grimes 	}
3206df8bae1dSRodney W. Grimes }
3207df8bae1dSRodney W. Grimes 
3208df8bae1dSRodney W. Grimes /*
32092a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
32102a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
32112a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
32122a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
32132a7be1b6SBrian Feldman  */
32142a7be1b6SBrian Feldman static void
32152a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
32162a7be1b6SBrian Feldman     vm_map_entry_t entry)
32172a7be1b6SBrian Feldman {
32182a7be1b6SBrian Feldman 	vm_size_t entrysize;
32192a7be1b6SBrian Feldman 	vm_offset_t newend;
32202a7be1b6SBrian Feldman 
32212a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
32222a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
32232a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
32242a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
32252a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
32262a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3227b351299cSAndrew Gallatin 		newend = MIN(entry->end,
32282a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
32292a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
32302a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
32312a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3232b351299cSAndrew Gallatin 		newend = MIN(entry->end,
32332a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
32342a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
32352a7be1b6SBrian Feldman 	}
32362a7be1b6SBrian Feldman }
32372a7be1b6SBrian Feldman 
32382a7be1b6SBrian Feldman /*
3239df8bae1dSRodney W. Grimes  * vmspace_fork:
3240df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
3241df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
3242df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
3243df8bae1dSRodney W. Grimes  * values on the regions in that map.
3244df8bae1dSRodney W. Grimes  *
32452a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
32462a7be1b6SBrian Feldman  *
3247df8bae1dSRodney W. Grimes  * The source map must not be locked.
3248df8bae1dSRodney W. Grimes  */
3249df8bae1dSRodney W. Grimes struct vmspace *
32503364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3251df8bae1dSRodney W. Grimes {
3252c0877f10SJohn Dyson 	struct vmspace *vm2;
325379e53838SAlan Cox 	vm_map_t new_map, old_map;
325479e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
3255de5f6a77SJohn Dyson 	vm_object_t object;
32561fac7d7fSKonstantin Belousov 	int locked;
3257df8bae1dSRodney W. Grimes 
325879e53838SAlan Cox 	old_map = &vm1->vm_map;
325979e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
326074d1d2b7SNeel Natu 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, NULL);
326189b57fcfSKonstantin Belousov 	if (vm2 == NULL)
326279e53838SAlan Cox 		return (NULL);
32632a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
32642a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
32652a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
326679e53838SAlan Cox 	vm_map_lock(old_map);
326779e53838SAlan Cox 	if (old_map->busy)
326879e53838SAlan Cox 		vm_map_wait_busy(old_map);
326979e53838SAlan Cox 	new_map = &vm2->vm_map;
32701fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
32711fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
3272df8bae1dSRodney W. Grimes 
3273df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3274df8bae1dSRodney W. Grimes 
3275df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3276afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3277df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3278df8bae1dSRodney W. Grimes 
3279df8bae1dSRodney W. Grimes 		switch (old_entry->inheritance) {
3280df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3281df8bae1dSRodney W. Grimes 			break;
3282df8bae1dSRodney W. Grimes 
3283df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3284df8bae1dSRodney W. Grimes 			/*
3285fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3286fed9a903SJohn Dyson 			 */
3287fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3288fed9a903SJohn Dyson 			if (object == NULL) {
3289fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3290c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3291fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
329215d2d313SAlan Cox 				old_entry->offset = 0;
3293ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
3294ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
32953364c323SKonstantin Belousov 					object->charge = old_entry->end -
32963364c323SKonstantin Belousov 					    old_entry->start;
3297ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
32983364c323SKonstantin Belousov 				}
32999a2f6362SAlan Cox 			}
33009a2f6362SAlan Cox 
33019a2f6362SAlan Cox 			/*
33029a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
33039a2f6362SAlan Cox 			 * to insure that a shadow object is created.
33049a2f6362SAlan Cox 			 */
33059a2f6362SAlan Cox 			vm_object_reference(object);
33069a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
33075069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
33085069bf57SJohn Dyson 				    &old_entry->offset,
33090cc74f14SAlan Cox 				    old_entry->end - old_entry->start);
33105069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3311d30344bdSIan Dowse 				/* Transfer the second reference too. */
3312d30344bdSIan Dowse 				vm_object_reference(
3313d30344bdSIan Dowse 				    old_entry->object.vm_object);
33147fd10fb3SKonstantin Belousov 
33157fd10fb3SKonstantin Belousov 				/*
33167fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3317b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
33187fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
33197fd10fb3SKonstantin Belousov 				 */
3320d30344bdSIan Dowse 				vm_object_deallocate(object);
33215069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3322fed9a903SJohn Dyson 			}
332389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3324069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3325ef694c1aSEdward Tomasz Napierala 			if (old_entry->cred != NULL) {
3326ef694c1aSEdward Tomasz Napierala 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3327ef694c1aSEdward Tomasz Napierala 				object->cred = old_entry->cred;
33283364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
3329ef694c1aSEdward Tomasz Napierala 				old_entry->cred = NULL;
33303364c323SKonstantin Belousov 			}
3331b9781cf6SKonstantin Belousov 
3332b9781cf6SKonstantin Belousov 			/*
3333b9781cf6SKonstantin Belousov 			 * Assert the correct state of the vnode
3334b9781cf6SKonstantin Belousov 			 * v_writecount while the object is locked, to
3335b9781cf6SKonstantin Belousov 			 * not relock it later for the assertion
3336b9781cf6SKonstantin Belousov 			 * correctness.
3337b9781cf6SKonstantin Belousov 			 */
3338b9781cf6SKonstantin Belousov 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3339b9781cf6SKonstantin Belousov 			    object->type == OBJT_VNODE) {
3340b9781cf6SKonstantin Belousov 				KASSERT(((struct vnode *)object->handle)->
3341b9781cf6SKonstantin Belousov 				    v_writecount > 0,
3342b9781cf6SKonstantin Belousov 				    ("vmspace_fork: v_writecount %p", object));
3343b9781cf6SKonstantin Belousov 				KASSERT(object->un_pager.vnp.writemappings > 0,
3344b9781cf6SKonstantin Belousov 				    ("vmspace_fork: vnp.writecount %p",
3345b9781cf6SKonstantin Belousov 				    object));
3346b9781cf6SKonstantin Belousov 			}
334789f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
3348fed9a903SJohn Dyson 
3349fed9a903SJohn Dyson 			/*
3350ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3351df8bae1dSRodney W. Grimes 			 */
3352df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3353df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
33549f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
33559f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
33560acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3357df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
335884110e7eSKonstantin Belousov 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
335984110e7eSKonstantin Belousov 				vnode_pager_update_writecount(object,
336084110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
336184110e7eSKonstantin Belousov 			}
3362df8bae1dSRodney W. Grimes 
3363df8bae1dSRodney W. Grimes 			/*
33640d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
33650d94caffSDavid Greenman 			 * inserting at the end of the new map.
3366df8bae1dSRodney W. Grimes 			 */
3367df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3368df8bae1dSRodney W. Grimes 			    new_entry);
33692a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3370df8bae1dSRodney W. Grimes 
3371df8bae1dSRodney W. Grimes 			/*
3372df8bae1dSRodney W. Grimes 			 * Update the physical map
3373df8bae1dSRodney W. Grimes 			 */
3374df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3375df8bae1dSRodney W. Grimes 			    new_entry->start,
3376df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3377df8bae1dSRodney W. Grimes 			    old_entry->start);
3378df8bae1dSRodney W. Grimes 			break;
3379df8bae1dSRodney W. Grimes 
3380df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3381df8bae1dSRodney W. Grimes 			/*
3382df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3383df8bae1dSRodney W. Grimes 			 */
3384df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3385df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
338684110e7eSKonstantin Belousov 			/*
338784110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
338884110e7eSKonstantin Belousov 			 */
33899f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
339084110e7eSKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
33910acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3392df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3393df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3394ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
3395df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3396df8bae1dSRodney W. Grimes 			    new_entry);
33972a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3398bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
33993364c323SKonstantin Belousov 			    new_entry, fork_charge);
3400df8bae1dSRodney W. Grimes 			break;
3401df8bae1dSRodney W. Grimes 		}
3402df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3403df8bae1dSRodney W. Grimes 	}
340484110e7eSKonstantin Belousov 	/*
340584110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
340684110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
340784110e7eSKonstantin Belousov 	 * new_map locks are released.
340884110e7eSKonstantin Belousov 	 */
340984110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
341084110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
341184110e7eSKonstantin Belousov 	vm_map_process_deferred();
3412df8bae1dSRodney W. Grimes 
3413df8bae1dSRodney W. Grimes 	return (vm2);
3414df8bae1dSRodney W. Grimes }
3415df8bae1dSRodney W. Grimes 
341694f7e29aSAlan Cox int
341794f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
341894f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
341994f7e29aSAlan Cox {
34204648ba0aSKonstantin Belousov 	vm_size_t growsize, init_ssize;
34214648ba0aSKonstantin Belousov 	rlim_t lmemlim, vmemlim;
34224648ba0aSKonstantin Belousov 	int rv;
34234648ba0aSKonstantin Belousov 
34244648ba0aSKonstantin Belousov 	growsize = sgrowsiz;
34254648ba0aSKonstantin Belousov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
34264648ba0aSKonstantin Belousov 	vm_map_lock(map);
34274648ba0aSKonstantin Belousov 	PROC_LOCK(curproc);
34284648ba0aSKonstantin Belousov 	lmemlim = lim_cur(curproc, RLIMIT_MEMLOCK);
34294648ba0aSKonstantin Belousov 	vmemlim = lim_cur(curproc, RLIMIT_VMEM);
34304648ba0aSKonstantin Belousov 	PROC_UNLOCK(curproc);
34314648ba0aSKonstantin Belousov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
34324648ba0aSKonstantin Belousov 		if (ptoa(pmap_wired_count(map->pmap)) + init_ssize > lmemlim) {
34334648ba0aSKonstantin Belousov 			rv = KERN_NO_SPACE;
34344648ba0aSKonstantin Belousov 			goto out;
34354648ba0aSKonstantin Belousov 		}
34364648ba0aSKonstantin Belousov 	}
34374648ba0aSKonstantin Belousov 	/* If we would blow our VMEM resource limit, no go */
34384648ba0aSKonstantin Belousov 	if (map->size + init_ssize > vmemlim) {
34394648ba0aSKonstantin Belousov 		rv = KERN_NO_SPACE;
34404648ba0aSKonstantin Belousov 		goto out;
34414648ba0aSKonstantin Belousov 	}
3442e1f92cccSAlan Cox 	rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
34434648ba0aSKonstantin Belousov 	    max, cow);
34444648ba0aSKonstantin Belousov out:
34454648ba0aSKonstantin Belousov 	vm_map_unlock(map);
34464648ba0aSKonstantin Belousov 	return (rv);
34474648ba0aSKonstantin Belousov }
34484648ba0aSKonstantin Belousov 
34494648ba0aSKonstantin Belousov static int
34504648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
34514648ba0aSKonstantin Belousov     vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
34524648ba0aSKonstantin Belousov {
3453fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
3454fd75d710SMarcel Moolenaar 	vm_offset_t bot, top;
34554648ba0aSKonstantin Belousov 	vm_size_t init_ssize;
3456fd75d710SMarcel Moolenaar 	int orient, rv;
345794f7e29aSAlan Cox 
3458fd75d710SMarcel Moolenaar 	/*
3459fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3460fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3461fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3462fd75d710SMarcel Moolenaar 	 * NOTE: We explicitly allow bi-directional stacks.
3463fd75d710SMarcel Moolenaar 	 */
3464fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN|MAP_STACK_GROWS_UP);
3465fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
3466fd75d710SMarcel Moolenaar 
346777bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
346877bc7900SKonstantin Belousov 	    addrbos > vm_map_max(map) ||
346977bc7900SKonstantin Belousov 	    addrbos + max_ssize < addrbos)
347094f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3471fd75d710SMarcel Moolenaar 
3472cfe52ecfSAndrey Zonov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
347394f7e29aSAlan Cox 
347494f7e29aSAlan Cox 	/* If addr is already mapped, no go */
34754648ba0aSKonstantin Belousov 	if (vm_map_lookup_entry(map, addrbos, &prev_entry))
347694f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3477a69ac174SMatthew Dillon 
3478fd75d710SMarcel Moolenaar 	/*
3479fd75d710SMarcel Moolenaar 	 * If we can't accomodate max_ssize in the current mapping, no go.
3480fd75d710SMarcel Moolenaar 	 * However, we need to be aware that subsequent user mappings might
3481fd75d710SMarcel Moolenaar 	 * map into the space we have reserved for stack, and currently this
3482fd75d710SMarcel Moolenaar 	 * space is not protected.
348394f7e29aSAlan Cox 	 *
3484fd75d710SMarcel Moolenaar 	 * Hopefully we will at least detect this condition when we try to
3485fd75d710SMarcel Moolenaar 	 * grow the stack.
348694f7e29aSAlan Cox 	 */
348794f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
34884648ba0aSKonstantin Belousov 	    (prev_entry->next->start < addrbos + max_ssize))
348994f7e29aSAlan Cox 		return (KERN_NO_SPACE);
349094f7e29aSAlan Cox 
3491fd75d710SMarcel Moolenaar 	/*
3492fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3493fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3494fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3495fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
349694f7e29aSAlan Cox 	 *
3497fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3498fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3499fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
350094f7e29aSAlan Cox 	 */
3501fd75d710SMarcel Moolenaar 	if (orient == MAP_STACK_GROWS_DOWN)
3502fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3503fd75d710SMarcel Moolenaar 	else if (orient == MAP_STACK_GROWS_UP)
3504fd75d710SMarcel Moolenaar 		bot = addrbos;
3505fd75d710SMarcel Moolenaar 	else
3506fd75d710SMarcel Moolenaar 		bot = round_page(addrbos + max_ssize/2 - init_ssize/2);
3507fd75d710SMarcel Moolenaar 	top = bot + init_ssize;
3508fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
350994f7e29aSAlan Cox 
3510fd75d710SMarcel Moolenaar 	/* Now set the avail_ssize amount. */
351194f7e29aSAlan Cox 	if (rv == KERN_SUCCESS) {
3512fd75d710SMarcel Moolenaar 		new_entry = prev_entry->next;
3513fd75d710SMarcel Moolenaar 		if (new_entry->end != top || new_entry->start != bot)
351494f7e29aSAlan Cox 			panic("Bad entry start/end for new stack entry");
3515b21a0008SMarcel Moolenaar 
3516fd75d710SMarcel Moolenaar 		new_entry->avail_ssize = max_ssize - init_ssize;
3517712efe66SAlan Cox 		KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
3518712efe66SAlan Cox 		    (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
3519712efe66SAlan Cox 		    ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
3520712efe66SAlan Cox 		KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
3521712efe66SAlan Cox 		    (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
3522712efe66SAlan Cox 		    ("new entry lacks MAP_ENTRY_GROWS_UP"));
352394f7e29aSAlan Cox 	}
352494f7e29aSAlan Cox 
352594f7e29aSAlan Cox 	return (rv);
352694f7e29aSAlan Cox }
352794f7e29aSAlan Cox 
35289a6d144fSKonstantin Belousov static int stack_guard_page = 0;
3529af3b2549SHans Petter Selasky SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
35309a6d144fSKonstantin Belousov     &stack_guard_page, 0,
35319a6d144fSKonstantin Belousov     "Insert stack guard page ahead of the growable segments.");
35329a6d144fSKonstantin Belousov 
353394f7e29aSAlan Cox /* Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if the
353494f7e29aSAlan Cox  * desired address is already mapped, or if we successfully grow
353594f7e29aSAlan Cox  * the stack.  Also returns KERN_SUCCESS if addr is outside the
353694f7e29aSAlan Cox  * stack range (this is strange, but preserves compatibility with
353794f7e29aSAlan Cox  * the grow function in vm_machdep.c).
353894f7e29aSAlan Cox  */
353994f7e29aSAlan Cox int
354094f7e29aSAlan Cox vm_map_growstack(struct proc *p, vm_offset_t addr)
354194f7e29aSAlan Cox {
3542b21a0008SMarcel Moolenaar 	vm_map_entry_t next_entry, prev_entry;
3543b21a0008SMarcel Moolenaar 	vm_map_entry_t new_entry, stack_entry;
354494f7e29aSAlan Cox 	struct vmspace *vm = p->p_vmspace;
354594f7e29aSAlan Cox 	vm_map_t map = &vm->vm_map;
354694f7e29aSAlan Cox 	vm_offset_t end;
3547cfe52ecfSAndrey Zonov 	vm_size_t growsize;
3548b21a0008SMarcel Moolenaar 	size_t grow_amount, max_grow;
35497e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
3550b21a0008SMarcel Moolenaar 	int is_procstack, rv;
3551ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
35521ba5ad42SEdward Tomasz Napierala #ifdef notyet
35531ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
35541ba5ad42SEdward Tomasz Napierala #endif
3555afcc55f3SEdward Tomasz Napierala #ifdef RACCT
35561ba5ad42SEdward Tomasz Napierala 	int error;
3557afcc55f3SEdward Tomasz Napierala #endif
355823955314SAlfred Perlstein 
355994f7e29aSAlan Cox Retry:
356091d5354aSJohn Baldwin 	PROC_LOCK(p);
35617e19eda4SAndrey Zonov 	lmemlim = lim_cur(p, RLIMIT_MEMLOCK);
356291d5354aSJohn Baldwin 	stacklim = lim_cur(p, RLIMIT_STACK);
3563bfee999dSAlan Cox 	vmemlim = lim_cur(p, RLIMIT_VMEM);
356491d5354aSJohn Baldwin 	PROC_UNLOCK(p);
356591d5354aSJohn Baldwin 
356694f7e29aSAlan Cox 	vm_map_lock_read(map);
356794f7e29aSAlan Cox 
356894f7e29aSAlan Cox 	/* If addr is already in the entry range, no need to grow.*/
356994f7e29aSAlan Cox 	if (vm_map_lookup_entry(map, addr, &prev_entry)) {
357094f7e29aSAlan Cox 		vm_map_unlock_read(map);
35710cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
357294f7e29aSAlan Cox 	}
357394f7e29aSAlan Cox 
3574b21a0008SMarcel Moolenaar 	next_entry = prev_entry->next;
3575b21a0008SMarcel Moolenaar 	if (!(prev_entry->eflags & MAP_ENTRY_GROWS_UP)) {
3576b21a0008SMarcel Moolenaar 		/*
3577b21a0008SMarcel Moolenaar 		 * This entry does not grow upwards. Since the address lies
3578b21a0008SMarcel Moolenaar 		 * beyond this entry, the next entry (if one exists) has to
3579b21a0008SMarcel Moolenaar 		 * be a downward growable entry. The entry list header is
3580b21a0008SMarcel Moolenaar 		 * never a growable entry, so it suffices to check the flags.
358194f7e29aSAlan Cox 		 */
3582b21a0008SMarcel Moolenaar 		if (!(next_entry->eflags & MAP_ENTRY_GROWS_DOWN)) {
358394f7e29aSAlan Cox 			vm_map_unlock_read(map);
35840cddd8f0SMatthew Dillon 			return (KERN_SUCCESS);
358594f7e29aSAlan Cox 		}
3586b21a0008SMarcel Moolenaar 		stack_entry = next_entry;
3587b21a0008SMarcel Moolenaar 	} else {
3588b21a0008SMarcel Moolenaar 		/*
3589b21a0008SMarcel Moolenaar 		 * This entry grows upward. If the next entry does not at
3590b21a0008SMarcel Moolenaar 		 * least grow downwards, this is the entry we need to grow.
3591b21a0008SMarcel Moolenaar 		 * otherwise we have two possible choices and we have to
3592b21a0008SMarcel Moolenaar 		 * select one.
3593b21a0008SMarcel Moolenaar 		 */
3594b21a0008SMarcel Moolenaar 		if (next_entry->eflags & MAP_ENTRY_GROWS_DOWN) {
3595b21a0008SMarcel Moolenaar 			/*
3596b21a0008SMarcel Moolenaar 			 * We have two choices; grow the entry closest to
3597b21a0008SMarcel Moolenaar 			 * the address to minimize the amount of growth.
3598b21a0008SMarcel Moolenaar 			 */
3599b21a0008SMarcel Moolenaar 			if (addr - prev_entry->end <= next_entry->start - addr)
3600b21a0008SMarcel Moolenaar 				stack_entry = prev_entry;
3601b21a0008SMarcel Moolenaar 			else
3602b21a0008SMarcel Moolenaar 				stack_entry = next_entry;
3603b21a0008SMarcel Moolenaar 		} else
3604b21a0008SMarcel Moolenaar 			stack_entry = prev_entry;
3605b21a0008SMarcel Moolenaar 	}
360694f7e29aSAlan Cox 
3607b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3608b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_DOWN, ("foo"));
3609b21a0008SMarcel Moolenaar 		KASSERT(addr < stack_entry->start, ("foo"));
3610b21a0008SMarcel Moolenaar 		end = (prev_entry != &map->header) ? prev_entry->end :
3611b21a0008SMarcel Moolenaar 		    stack_entry->start - stack_entry->avail_ssize;
361294f7e29aSAlan Cox 		grow_amount = roundup(stack_entry->start - addr, PAGE_SIZE);
3613b21a0008SMarcel Moolenaar 		max_grow = stack_entry->start - end;
3614b21a0008SMarcel Moolenaar 	} else {
3615b21a0008SMarcel Moolenaar 		KASSERT(stack_entry->eflags & MAP_ENTRY_GROWS_UP, ("foo"));
361608667f6dSMarcel Moolenaar 		KASSERT(addr >= stack_entry->end, ("foo"));
3617b21a0008SMarcel Moolenaar 		end = (next_entry != &map->header) ? next_entry->start :
3618b21a0008SMarcel Moolenaar 		    stack_entry->end + stack_entry->avail_ssize;
3619fd75d710SMarcel Moolenaar 		grow_amount = roundup(addr + 1 - stack_entry->end, PAGE_SIZE);
3620b21a0008SMarcel Moolenaar 		max_grow = end - stack_entry->end;
3621b21a0008SMarcel Moolenaar 	}
3622b21a0008SMarcel Moolenaar 
362394f7e29aSAlan Cox 	if (grow_amount > stack_entry->avail_ssize) {
362494f7e29aSAlan Cox 		vm_map_unlock_read(map);
36250cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
362694f7e29aSAlan Cox 	}
362794f7e29aSAlan Cox 
3628b21a0008SMarcel Moolenaar 	/*
3629b21a0008SMarcel Moolenaar 	 * If there is no longer enough space between the entries nogo, and
3630b21a0008SMarcel Moolenaar 	 * adjust the available space.  Note: this  should only happen if the
3631b21a0008SMarcel Moolenaar 	 * user has mapped into the stack area after the stack was created,
3632b21a0008SMarcel Moolenaar 	 * and is probably an error.
363394f7e29aSAlan Cox 	 *
3634b21a0008SMarcel Moolenaar 	 * This also effectively destroys any guard page the user might have
3635b21a0008SMarcel Moolenaar 	 * intended by limiting the stack size.
363694f7e29aSAlan Cox 	 */
36379a6d144fSKonstantin Belousov 	if (grow_amount + (stack_guard_page ? PAGE_SIZE : 0) > max_grow) {
363825adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
363994f7e29aSAlan Cox 			goto Retry;
364094f7e29aSAlan Cox 
3641b21a0008SMarcel Moolenaar 		stack_entry->avail_ssize = max_grow;
364294f7e29aSAlan Cox 
364394f7e29aSAlan Cox 		vm_map_unlock(map);
36440cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
364594f7e29aSAlan Cox 	}
364694f7e29aSAlan Cox 
3647b21a0008SMarcel Moolenaar 	is_procstack = (addr >= (vm_offset_t)vm->vm_maxsaddr) ? 1 : 0;
364894f7e29aSAlan Cox 
3649b21a0008SMarcel Moolenaar 	/*
3650b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3651b21a0008SMarcel Moolenaar 	 * limit.
365294f7e29aSAlan Cox 	 */
365391d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
365494f7e29aSAlan Cox 		vm_map_unlock_read(map);
36550cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
365694f7e29aSAlan Cox 	}
3657afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3658*4b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
36591ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
3660*4b5c9cf6SEdward Tomasz Napierala 		if (is_procstack && racct_set(p, RACCT_STACK,
3661*4b5c9cf6SEdward Tomasz Napierala 		    ctob(vm->vm_ssize) + grow_amount)) {
36621ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
36631ba5ad42SEdward Tomasz Napierala 			vm_map_unlock_read(map);
36641ba5ad42SEdward Tomasz Napierala 			return (KERN_NO_SPACE);
36651ba5ad42SEdward Tomasz Napierala 		}
36661ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
3667*4b5c9cf6SEdward Tomasz Napierala 	}
3668afcc55f3SEdward Tomasz Napierala #endif
366994f7e29aSAlan Cox 
3670cfe52ecfSAndrey Zonov 	/* Round up the grow amount modulo sgrowsiz */
3671cfe52ecfSAndrey Zonov 	growsize = sgrowsiz;
3672cfe52ecfSAndrey Zonov 	grow_amount = roundup(grow_amount, growsize);
3673b21a0008SMarcel Moolenaar 	if (grow_amount > stack_entry->avail_ssize)
367494f7e29aSAlan Cox 		grow_amount = stack_entry->avail_ssize;
367591d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3676e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
3677e4826248SAlan Cox 		    ctob(vm->vm_ssize);
367894f7e29aSAlan Cox 	}
36791ba5ad42SEdward Tomasz Napierala #ifdef notyet
36801ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
36811ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
36821ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
36831ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
36841ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
36851ba5ad42SEdward Tomasz Napierala #endif
36867e19eda4SAndrey Zonov 	if (!old_mlock && map->flags & MAP_WIREFUTURE) {
36873ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
36887e19eda4SAndrey Zonov 			vm_map_unlock_read(map);
36897e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
36907e19eda4SAndrey Zonov 			goto out;
36917e19eda4SAndrey Zonov 		}
36927e19eda4SAndrey Zonov #ifdef RACCT
3693*4b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
36947e19eda4SAndrey Zonov 			PROC_LOCK(p);
36957e19eda4SAndrey Zonov 			if (racct_set(p, RACCT_MEMLOCK,
36963ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
36977e19eda4SAndrey Zonov 				PROC_UNLOCK(p);
36987e19eda4SAndrey Zonov 				vm_map_unlock_read(map);
36997e19eda4SAndrey Zonov 				rv = KERN_NO_SPACE;
37007e19eda4SAndrey Zonov 				goto out;
37017e19eda4SAndrey Zonov 			}
37027e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
3703*4b5c9cf6SEdward Tomasz Napierala 		}
37047e19eda4SAndrey Zonov #endif
37057e19eda4SAndrey Zonov 	}
3706a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
370791d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
3708a69ac174SMatthew Dillon 		vm_map_unlock_read(map);
37091ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
37101ba5ad42SEdward Tomasz Napierala 		goto out;
3711a69ac174SMatthew Dillon 	}
3712afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3713*4b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
37141ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
37151ba5ad42SEdward Tomasz Napierala 		if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
37161ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
37171ba5ad42SEdward Tomasz Napierala 			vm_map_unlock_read(map);
37181ba5ad42SEdward Tomasz Napierala 			rv = KERN_NO_SPACE;
37191ba5ad42SEdward Tomasz Napierala 			goto out;
37201ba5ad42SEdward Tomasz Napierala 		}
37211ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
3722*4b5c9cf6SEdward Tomasz Napierala 	}
3723afcc55f3SEdward Tomasz Napierala #endif
3724a69ac174SMatthew Dillon 
372525adb370SBrian Feldman 	if (vm_map_lock_upgrade(map))
372694f7e29aSAlan Cox 		goto Retry;
372794f7e29aSAlan Cox 
3728b21a0008SMarcel Moolenaar 	if (stack_entry == next_entry) {
3729b21a0008SMarcel Moolenaar 		/*
3730b21a0008SMarcel Moolenaar 		 * Growing downward.
3731b21a0008SMarcel Moolenaar 		 */
373294f7e29aSAlan Cox 		/* Get the preliminary new entry start value */
373394f7e29aSAlan Cox 		addr = stack_entry->start - grow_amount;
373494f7e29aSAlan Cox 
3735b21a0008SMarcel Moolenaar 		/*
3736b21a0008SMarcel Moolenaar 		 * If this puts us into the previous entry, cut back our
3737b21a0008SMarcel Moolenaar 		 * growth to the available space. Also, see the note above.
373894f7e29aSAlan Cox 		 */
373994f7e29aSAlan Cox 		if (addr < end) {
3740b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = max_grow;
374194f7e29aSAlan Cox 			addr = end;
37429a6d144fSKonstantin Belousov 			if (stack_guard_page)
37439a6d144fSKonstantin Belousov 				addr += PAGE_SIZE;
374494f7e29aSAlan Cox 		}
374594f7e29aSAlan Cox 
374694f7e29aSAlan Cox 		rv = vm_map_insert(map, NULL, 0, addr, stack_entry->start,
3747712efe66SAlan Cox 		    next_entry->protection, next_entry->max_protection,
3748712efe66SAlan Cox 		    MAP_STACK_GROWS_DOWN);
374994f7e29aSAlan Cox 
375094f7e29aSAlan Cox 		/* Adjust the available stack space by the amount we grew. */
375194f7e29aSAlan Cox 		if (rv == KERN_SUCCESS) {
3752b21a0008SMarcel Moolenaar 			new_entry = prev_entry->next;
3753b21a0008SMarcel Moolenaar 			KASSERT(new_entry == stack_entry->prev, ("foo"));
3754b21a0008SMarcel Moolenaar 			KASSERT(new_entry->end == stack_entry->start, ("foo"));
3755b21a0008SMarcel Moolenaar 			KASSERT(new_entry->start == addr, ("foo"));
3756712efe66SAlan Cox 			KASSERT((new_entry->eflags & MAP_ENTRY_GROWS_DOWN) !=
3757712efe66SAlan Cox 			    0, ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
3758b21a0008SMarcel Moolenaar 			grow_amount = new_entry->end - new_entry->start;
3759b21a0008SMarcel Moolenaar 			new_entry->avail_ssize = stack_entry->avail_ssize -
3760b21a0008SMarcel Moolenaar 			    grow_amount;
3761b21a0008SMarcel Moolenaar 			stack_entry->eflags &= ~MAP_ENTRY_GROWS_DOWN;
376294f7e29aSAlan Cox 		}
3763b21a0008SMarcel Moolenaar 	} else {
3764b21a0008SMarcel Moolenaar 		/*
3765b21a0008SMarcel Moolenaar 		 * Growing upward.
3766b21a0008SMarcel Moolenaar 		 */
3767b21a0008SMarcel Moolenaar 		addr = stack_entry->end + grow_amount;
3768b21a0008SMarcel Moolenaar 
3769b21a0008SMarcel Moolenaar 		/*
3770b21a0008SMarcel Moolenaar 		 * If this puts us into the next entry, cut back our growth
3771b21a0008SMarcel Moolenaar 		 * to the available space. Also, see the note above.
3772b21a0008SMarcel Moolenaar 		 */
3773b21a0008SMarcel Moolenaar 		if (addr > end) {
3774b21a0008SMarcel Moolenaar 			stack_entry->avail_ssize = end - stack_entry->end;
3775b21a0008SMarcel Moolenaar 			addr = end;
37769a6d144fSKonstantin Belousov 			if (stack_guard_page)
37779a6d144fSKonstantin Belousov 				addr -= PAGE_SIZE;
377894f7e29aSAlan Cox 		}
377994f7e29aSAlan Cox 
3780b21a0008SMarcel Moolenaar 		grow_amount = addr - stack_entry->end;
3781ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
3782ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
3783ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
3784ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
37853364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3786b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
37873364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3788b21a0008SMarcel Moolenaar 			 vm_object_coalesce(stack_entry->object.vm_object,
378957a21abaSAlan Cox 			 stack_entry->offset,
3790b21a0008SMarcel Moolenaar 			 (vm_size_t)(stack_entry->end - stack_entry->start),
3791ef694c1aSEdward Tomasz Napierala 			 (vm_size_t)grow_amount, cred != NULL)) {
379208667f6dSMarcel Moolenaar 			map->size += (addr - stack_entry->end);
3793b21a0008SMarcel Moolenaar 			/* Update the current entry. */
3794b21a0008SMarcel Moolenaar 			stack_entry->end = addr;
3795199c91abSMarcel Moolenaar 			stack_entry->avail_ssize -= grow_amount;
37960164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3797b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3798b21a0008SMarcel Moolenaar 		} else
3799b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3800b21a0008SMarcel Moolenaar 	}
3801b21a0008SMarcel Moolenaar 
3802b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3803b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3804b21a0008SMarcel Moolenaar 
380594f7e29aSAlan Cox 	vm_map_unlock(map);
3806b21a0008SMarcel Moolenaar 
3807abd498aaSBruce M Simpson 	/*
3808abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3809abd498aaSBruce M Simpson 	 */
3810b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE)) {
3811b21a0008SMarcel Moolenaar 		vm_map_wire(map,
3812b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? addr : addr - grow_amount,
3813b21a0008SMarcel Moolenaar 		    (stack_entry == next_entry) ? stack_entry->start : addr,
3814b21a0008SMarcel Moolenaar 		    (p->p_flag & P_SYSTEM)
3815b21a0008SMarcel Moolenaar 		    ? VM_MAP_WIRE_SYSTEM|VM_MAP_WIRE_NOHOLES
3816b21a0008SMarcel Moolenaar 		    : VM_MAP_WIRE_USER|VM_MAP_WIRE_NOHOLES);
3817b21a0008SMarcel Moolenaar 	}
3818abd498aaSBruce M Simpson 
38191ba5ad42SEdward Tomasz Napierala out:
3820afcc55f3SEdward Tomasz Napierala #ifdef RACCT
3821*4b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && rv != KERN_SUCCESS) {
38221ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
38231ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
38241ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
38257e19eda4SAndrey Zonov 		if (!old_mlock) {
38267e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
38273ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
38287e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
38297e19eda4SAndrey Zonov 		}
38301ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
38311ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
38321ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
38331ba5ad42SEdward Tomasz Napierala 	}
3834afcc55f3SEdward Tomasz Napierala #endif
38351ba5ad42SEdward Tomasz Napierala 
38360cddd8f0SMatthew Dillon 	return (rv);
383794f7e29aSAlan Cox }
383894f7e29aSAlan Cox 
3839df8bae1dSRodney W. Grimes /*
38405856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
38415856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
38425856e12eSJohn Dyson  */
384389b57fcfSKonstantin Belousov int
38443ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
38451b40f8c0SMatthew Dillon {
38465856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
38475856e12eSJohn Dyson 	struct vmspace *newvmspace;
38485856e12eSJohn Dyson 
38497032434eSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
38507032434eSKonstantin Belousov 	    ("vmspace_exec recursed"));
385174d1d2b7SNeel Natu 	newvmspace = vmspace_alloc(minuser, maxuser, NULL);
385289b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
385389b57fcfSKonstantin Belousov 		return (ENOMEM);
385451ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
38555856e12eSJohn Dyson 	/*
38565856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
38575856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
38585856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
38595856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
38605856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
38615856e12eSJohn Dyson 	 */
386257051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
38635856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
386457051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
38656617724cSJeff Roberson 	if (p == curthread->td_proc)
3866b40ce416SJulian Elischer 		pmap_activate(curthread);
38677032434eSKonstantin Belousov 	curthread->td_pflags |= TDP_EXECVMSPC;
386889b57fcfSKonstantin Belousov 	return (0);
38695856e12eSJohn Dyson }
38705856e12eSJohn Dyson 
38715856e12eSJohn Dyson /*
38725856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
38735856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
38745856e12eSJohn Dyson  */
387589b57fcfSKonstantin Belousov int
38761b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
38771b40f8c0SMatthew Dillon {
38785856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
38795856e12eSJohn Dyson 	struct vmspace *newvmspace;
38803364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
38815856e12eSJohn Dyson 
38825856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
388389b57fcfSKonstantin Belousov 		return (0);
38843364c323SKonstantin Belousov 	fork_charge = 0;
38853364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
388689b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
388789b57fcfSKonstantin Belousov 		return (ENOMEM);
3888ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
38893364c323SKonstantin Belousov 		vmspace_free(newvmspace);
38903364c323SKonstantin Belousov 		return (ENOMEM);
38913364c323SKonstantin Belousov 	}
389257051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
38935856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
389457051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
38956617724cSJeff Roberson 	if (p == curthread->td_proc)
3896b40ce416SJulian Elischer 		pmap_activate(curthread);
3897b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
389889b57fcfSKonstantin Belousov 	return (0);
38995856e12eSJohn Dyson }
39005856e12eSJohn Dyson 
39015856e12eSJohn Dyson /*
3902df8bae1dSRodney W. Grimes  *	vm_map_lookup:
3903df8bae1dSRodney W. Grimes  *
3904df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
3905df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
3906df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
3907df8bae1dSRodney W. Grimes  *	type specified.
3908df8bae1dSRodney W. Grimes  *
3909df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
3910df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
3911df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
3912df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
3913df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
3914df8bae1dSRodney W. Grimes  *
3915df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
3916df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
3917df8bae1dSRodney W. Grimes  *
3918df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
3919df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
3920df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
3921df8bae1dSRodney W. Grimes  *	remain the same.
3922df8bae1dSRodney W. Grimes  */
3923df8bae1dSRodney W. Grimes int
3924b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
3925b9dcd593SBruce Evans 	      vm_offset_t vaddr,
392647221757SJohn Dyson 	      vm_prot_t fault_typea,
3927b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
3928b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
3929b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
3930b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
39312d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
3932df8bae1dSRodney W. Grimes {
3933c0877f10SJohn Dyson 	vm_map_entry_t entry;
3934c0877f10SJohn Dyson 	vm_map_t map = *var_map;
3935c0877f10SJohn Dyson 	vm_prot_t prot;
393647221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
39373364c323SKonstantin Belousov 	vm_object_t eobject;
39380cc74f14SAlan Cox 	vm_size_t size;
3939ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
3940df8bae1dSRodney W. Grimes 
3941df8bae1dSRodney W. Grimes RetryLookup:;
3942df8bae1dSRodney W. Grimes 
3943df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
3944df8bae1dSRodney W. Grimes 
3945df8bae1dSRodney W. Grimes 	/*
39464c3ef59eSAlan Cox 	 * Lookup the faulting address.
3947df8bae1dSRodney W. Grimes 	 */
3948095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
3949095104acSAlan Cox 		vm_map_unlock_read(map);
3950095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
3951095104acSAlan Cox 	}
3952df8bae1dSRodney W. Grimes 
39534e94f402SAlan Cox 	entry = *out_entry;
3954b7b2aac2SJohn Dyson 
3955df8bae1dSRodney W. Grimes 	/*
3956df8bae1dSRodney W. Grimes 	 * Handle submaps.
3957df8bae1dSRodney W. Grimes 	 */
3958afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
3959df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
3960df8bae1dSRodney W. Grimes 
3961df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
3962df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
3963df8bae1dSRodney W. Grimes 		goto RetryLookup;
3964df8bae1dSRodney W. Grimes 	}
3965a04c970aSJohn Dyson 
3966df8bae1dSRodney W. Grimes 	/*
39670d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
3968df8bae1dSRodney W. Grimes 	 */
3969df8bae1dSRodney W. Grimes 	prot = entry->protection;
397047221757SJohn Dyson 	fault_type &= (VM_PROT_READ|VM_PROT_WRITE|VM_PROT_EXECUTE);
39712db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
3972095104acSAlan Cox 		vm_map_unlock_read(map);
3973095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
397447221757SJohn Dyson 	}
39752ed14a92SAlan Cox 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
397647221757SJohn Dyson 	    (entry->eflags & MAP_ENTRY_COW) &&
3977a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE)) {
3978095104acSAlan Cox 		vm_map_unlock_read(map);
3979095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
3980a04c970aSJohn Dyson 	}
39815b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
39825b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
39835b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
39845b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
39855b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
39865b3e0257SDag-Erling Smørgrav 	}
3987df8bae1dSRodney W. Grimes 
3988df8bae1dSRodney W. Grimes 	/*
39890d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
39900d94caffSDavid Greenman 	 * accesses.
3991df8bae1dSRodney W. Grimes 	 */
399205f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
399305f0fdd2SPoul-Henning Kamp 	if (*wired)
3994a6d42a0dSAlan Cox 		fault_type = entry->protection;
39953364c323SKonstantin Belousov 	size = entry->end - entry->start;
3996df8bae1dSRodney W. Grimes 	/*
3997df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
3998df8bae1dSRodney W. Grimes 	 */
3999afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4000df8bae1dSRodney W. Grimes 		/*
40010d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
4002ad5fca3bSAlan Cox 		 * now since we've got the map locked.
4003df8bae1dSRodney W. Grimes 		 *
40040d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
40050d94caffSDavid Greenman 		 * permissions allowed.
4006df8bae1dSRodney W. Grimes 		 */
4007a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
4008a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
4009df8bae1dSRodney W. Grimes 			/*
40100d94caffSDavid Greenman 			 * Make a new object, and place it in the object
40110d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
4012ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
40130d94caffSDavid Greenman 			 * object.
4014df8bae1dSRodney W. Grimes 			 */
401525adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
4016df8bae1dSRodney W. Grimes 				goto RetryLookup;
40179917e010SAlan Cox 
4018ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
40193364c323SKonstantin Belousov 				/*
40203364c323SKonstantin Belousov 				 * The debugger owner is charged for
40213364c323SKonstantin Belousov 				 * the memory.
40223364c323SKonstantin Belousov 				 */
4023ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
4024ef694c1aSEdward Tomasz Napierala 				crhold(cred);
4025ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
4026ef694c1aSEdward Tomasz Napierala 					crfree(cred);
40273364c323SKonstantin Belousov 					vm_map_unlock(map);
40283364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
40293364c323SKonstantin Belousov 				}
4030ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
40313364c323SKonstantin Belousov 			}
40320cc74f14SAlan Cox 			vm_object_shadow(&entry->object.vm_object,
40330cc74f14SAlan Cox 			    &entry->offset, size);
4034afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
40353364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
4036ef694c1aSEdward Tomasz Napierala 			if (eobject->cred != NULL) {
40373364c323SKonstantin Belousov 				/*
40383364c323SKonstantin Belousov 				 * The object was not shadowed.
40393364c323SKonstantin Belousov 				 */
4040ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
4041ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
4042ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
4043ef694c1aSEdward Tomasz Napierala 			} else if (entry->cred != NULL) {
404489f6b863SAttilio Rao 				VM_OBJECT_WLOCK(eobject);
4045ef694c1aSEdward Tomasz Napierala 				eobject->cred = entry->cred;
40463364c323SKonstantin Belousov 				eobject->charge = size;
404789f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(eobject);
4048ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
40493364c323SKonstantin Belousov 			}
40509917e010SAlan Cox 
40519b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
40520d94caffSDavid Greenman 		} else {
4053df8bae1dSRodney W. Grimes 			/*
40540d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
40550d94caffSDavid Greenman 			 * don't allow writes.
4056df8bae1dSRodney W. Grimes 			 */
40572d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
4058df8bae1dSRodney W. Grimes 		}
4059df8bae1dSRodney W. Grimes 	}
40602d8acc0fSJohn Dyson 
4061df8bae1dSRodney W. Grimes 	/*
4062df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
4063df8bae1dSRodney W. Grimes 	 */
40644e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
40654e71e795SMatthew Dillon 	    !map->system_map) {
406625adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
4067df8bae1dSRodney W. Grimes 			goto RetryLookup;
406824a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
40693364c323SKonstantin Belousov 		    atop(size));
4070df8bae1dSRodney W. Grimes 		entry->offset = 0;
4071ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
407289f6b863SAttilio Rao 			VM_OBJECT_WLOCK(entry->object.vm_object);
4073ef694c1aSEdward Tomasz Napierala 			entry->object.vm_object->cred = entry->cred;
40743364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
407589f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
4076ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
40773364c323SKonstantin Belousov 		}
40789b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
4079df8bae1dSRodney W. Grimes 	}
4080b5b40fa6SJohn Dyson 
4081df8bae1dSRodney W. Grimes 	/*
40820d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
40830d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
4084df8bae1dSRodney W. Grimes 	 */
40859b09b6c7SMatthew Dillon 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
4086df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
4087df8bae1dSRodney W. Grimes 
4088df8bae1dSRodney W. Grimes 	*out_prot = prot;
4089df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
4090df8bae1dSRodney W. Grimes }
4091df8bae1dSRodney W. Grimes 
4092df8bae1dSRodney W. Grimes /*
409319dc5607STor Egge  *	vm_map_lookup_locked:
409419dc5607STor Egge  *
409519dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
409619dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
409719dc5607STor Egge  */
409819dc5607STor Egge int
409919dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
410019dc5607STor Egge 		     vm_offset_t vaddr,
410119dc5607STor Egge 		     vm_prot_t fault_typea,
410219dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
410319dc5607STor Egge 		     vm_object_t *object,	/* OUT */
410419dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
410519dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
410619dc5607STor Egge 		     boolean_t *wired)		/* OUT */
410719dc5607STor Egge {
410819dc5607STor Egge 	vm_map_entry_t entry;
410919dc5607STor Egge 	vm_map_t map = *var_map;
411019dc5607STor Egge 	vm_prot_t prot;
411119dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
411219dc5607STor Egge 
411319dc5607STor Egge 	/*
41144c3ef59eSAlan Cox 	 * Lookup the faulting address.
411519dc5607STor Egge 	 */
411619dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
411719dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
411819dc5607STor Egge 
411919dc5607STor Egge 	entry = *out_entry;
412019dc5607STor Egge 
412119dc5607STor Egge 	/*
412219dc5607STor Egge 	 * Fail if the entry refers to a submap.
412319dc5607STor Egge 	 */
412419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
412519dc5607STor Egge 		return (KERN_FAILURE);
412619dc5607STor Egge 
412719dc5607STor Egge 	/*
412819dc5607STor Egge 	 * Check whether this task is allowed to have this page.
412919dc5607STor Egge 	 */
413019dc5607STor Egge 	prot = entry->protection;
413119dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
413219dc5607STor Egge 	if ((fault_type & prot) != fault_type)
413319dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
413419dc5607STor Egge 	if ((entry->eflags & MAP_ENTRY_USER_WIRED) &&
413519dc5607STor Egge 	    (entry->eflags & MAP_ENTRY_COW) &&
4136a6d42a0dSAlan Cox 	    (fault_type & VM_PROT_WRITE))
413719dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
413819dc5607STor Egge 
413919dc5607STor Egge 	/*
414019dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
414119dc5607STor Egge 	 * accesses.
414219dc5607STor Egge 	 */
414319dc5607STor Egge 	*wired = (entry->wired_count != 0);
414419dc5607STor Egge 	if (*wired)
4145a6d42a0dSAlan Cox 		fault_type = entry->protection;
414619dc5607STor Egge 
414719dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
414819dc5607STor Egge 		/*
414919dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
415019dc5607STor Egge 		 */
415119dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
415219dc5607STor Egge 			return (KERN_FAILURE);
415319dc5607STor Egge 		/*
415419dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
415519dc5607STor Egge 		 * don't allow writes.
415619dc5607STor Egge 		 */
415719dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
415819dc5607STor Egge 	}
415919dc5607STor Egge 
416019dc5607STor Egge 	/*
416119dc5607STor Egge 	 * Fail if an object should be created.
416219dc5607STor Egge 	 */
416319dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
416419dc5607STor Egge 		return (KERN_FAILURE);
416519dc5607STor Egge 
416619dc5607STor Egge 	/*
416719dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
416819dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
416919dc5607STor Egge 	 */
417019dc5607STor Egge 	*pindex = OFF_TO_IDX((vaddr - entry->start) + entry->offset);
417119dc5607STor Egge 	*object = entry->object.vm_object;
417219dc5607STor Egge 
417319dc5607STor Egge 	*out_prot = prot;
417419dc5607STor Egge 	return (KERN_SUCCESS);
417519dc5607STor Egge }
417619dc5607STor Egge 
417719dc5607STor Egge /*
4178df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
4179df8bae1dSRodney W. Grimes  *
4180df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
4181df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
4182df8bae1dSRodney W. Grimes  */
41830d94caffSDavid Greenman void
41841b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4185df8bae1dSRodney W. Grimes {
4186df8bae1dSRodney W. Grimes 	/*
4187df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
4188df8bae1dSRodney W. Grimes 	 */
4189df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
4190df8bae1dSRodney W. Grimes }
4191df8bae1dSRodney W. Grimes 
4192c7c34a24SBruce Evans #include "opt_ddb.h"
4193c3cb3e12SDavid Greenman #ifdef DDB
4194c7c34a24SBruce Evans #include <sys/kernel.h>
4195c7c34a24SBruce Evans 
4196c7c34a24SBruce Evans #include <ddb/ddb.h>
4197c7c34a24SBruce Evans 
41982ebcd458SAttilio Rao static void
41992ebcd458SAttilio Rao vm_map_print(vm_map_t map)
4200df8bae1dSRodney W. Grimes {
4201c0877f10SJohn Dyson 	vm_map_entry_t entry;
4202c7c34a24SBruce Evans 
4203e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4204e5f251d2SAlan Cox 	    (void *)map,
4205101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
4206df8bae1dSRodney W. Grimes 
4207c7c34a24SBruce Evans 	db_indent += 2;
4208df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
4209df8bae1dSRodney W. Grimes 	    entry = entry->next) {
4210fc62ef1fSBruce Evans 		db_iprintf("map entry %p: start=%p, end=%p\n",
4211fc62ef1fSBruce Evans 		    (void *)entry, (void *)entry->start, (void *)entry->end);
4212e5f251d2SAlan Cox 		{
4213df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
4214df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
42150d94caffSDavid Greenman 
421695e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
4217df8bae1dSRodney W. Grimes 			    entry->protection,
4218df8bae1dSRodney W. Grimes 			    entry->max_protection,
42198aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4220df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
422195e5e988SJohn Dyson 				db_printf(", wired");
4222df8bae1dSRodney W. Grimes 		}
42239fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4224cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
42259fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
4226cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4227df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
42289fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
42299fdfe602SMatthew Dillon 				entry->object.sub_map)) {
4230c7c34a24SBruce Evans 				db_indent += 2;
42312ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
4232c7c34a24SBruce Evans 				db_indent -= 2;
4233df8bae1dSRodney W. Grimes 			}
42340d94caffSDavid Greenman 		} else {
4235ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
4236ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
4237cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
4238101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
4239cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4240ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
4241ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
4242ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
42433364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
4244afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
4245c7c34a24SBruce Evans 				db_printf(", copy (%s)",
4246afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4247c7c34a24SBruce Evans 			db_printf("\n");
4248df8bae1dSRodney W. Grimes 
4249df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
4250df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
4251df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
4252c7c34a24SBruce Evans 				db_indent += 2;
4253101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
4254101eeb7fSBruce Evans 						entry->object.vm_object,
425544bbc3b7SKonstantin Belousov 						0, 0, (char *)0);
4256c7c34a24SBruce Evans 				db_indent -= 2;
4257df8bae1dSRodney W. Grimes 			}
4258df8bae1dSRodney W. Grimes 		}
4259df8bae1dSRodney W. Grimes 	}
4260c7c34a24SBruce Evans 	db_indent -= 2;
4261df8bae1dSRodney W. Grimes }
426295e5e988SJohn Dyson 
42632ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
42642ebcd458SAttilio Rao {
42652ebcd458SAttilio Rao 
42662ebcd458SAttilio Rao 	if (!have_addr) {
42672ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
42682ebcd458SAttilio Rao 		return;
42692ebcd458SAttilio Rao 	}
42702ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
42712ebcd458SAttilio Rao }
427295e5e988SJohn Dyson 
427395e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
427495e5e988SJohn Dyson {
427595e5e988SJohn Dyson 	struct proc *p;
427695e5e988SJohn Dyson 
427795e5e988SJohn Dyson 	if (have_addr) {
427895e5e988SJohn Dyson 		p = (struct proc *) addr;
427995e5e988SJohn Dyson 	} else {
428095e5e988SJohn Dyson 		p = curproc;
428195e5e988SJohn Dyson 	}
428295e5e988SJohn Dyson 
4283ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4284ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4285b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
428695e5e988SJohn Dyson 
42872ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
428895e5e988SJohn Dyson }
428995e5e988SJohn Dyson 
4290c7c34a24SBruce Evans #endif /* DDB */
4291