xref: /freebsd/sys/vm/vm_map.c (revision fec296887fd52f2265052ccd263b242ecb2f6a56)
160727d8bSWarner Losh /*-
2796df753SPedro F. Giffuni  * SPDX-License-Identifier: (BSD-3-Clause AND MIT-CMU)
351369649SPedro F. Giffuni  *
4df8bae1dSRodney W. Grimes  * Copyright (c) 1991, 1993
5df8bae1dSRodney W. Grimes  *	The Regents of the University of California.  All rights reserved.
6df8bae1dSRodney W. Grimes  *
7df8bae1dSRodney W. Grimes  * This code is derived from software contributed to Berkeley by
8df8bae1dSRodney W. Grimes  * The Mach Operating System project at Carnegie-Mellon University.
9df8bae1dSRodney W. Grimes  *
10df8bae1dSRodney W. Grimes  * Redistribution and use in source and binary forms, with or without
11df8bae1dSRodney W. Grimes  * modification, are permitted provided that the following conditions
12df8bae1dSRodney W. Grimes  * are met:
13df8bae1dSRodney W. Grimes  * 1. Redistributions of source code must retain the above copyright
14df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer.
15df8bae1dSRodney W. Grimes  * 2. Redistributions in binary form must reproduce the above copyright
16df8bae1dSRodney W. Grimes  *    notice, this list of conditions and the following disclaimer in the
17df8bae1dSRodney W. Grimes  *    documentation and/or other materials provided with the distribution.
18fbbd9655SWarner Losh  * 3. Neither the name of the University nor the names of its contributors
19df8bae1dSRodney W. Grimes  *    may be used to endorse or promote products derived from this software
20df8bae1dSRodney W. Grimes  *    without specific prior written permission.
21df8bae1dSRodney W. Grimes  *
22df8bae1dSRodney W. Grimes  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23df8bae1dSRodney W. Grimes  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24df8bae1dSRodney W. Grimes  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25df8bae1dSRodney W. Grimes  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26df8bae1dSRodney W. Grimes  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27df8bae1dSRodney W. Grimes  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28df8bae1dSRodney W. Grimes  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29df8bae1dSRodney W. Grimes  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30df8bae1dSRodney W. Grimes  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31df8bae1dSRodney W. Grimes  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32df8bae1dSRodney W. Grimes  * SUCH DAMAGE.
33df8bae1dSRodney W. Grimes  *
343c4dd356SDavid Greenman  *	from: @(#)vm_map.c	8.3 (Berkeley) 1/12/94
35df8bae1dSRodney W. Grimes  *
36df8bae1dSRodney W. Grimes  *
37df8bae1dSRodney W. Grimes  * Copyright (c) 1987, 1990 Carnegie-Mellon University.
38df8bae1dSRodney W. Grimes  * All rights reserved.
39df8bae1dSRodney W. Grimes  *
40df8bae1dSRodney W. Grimes  * Authors: Avadis Tevanian, Jr., Michael Wayne Young
41df8bae1dSRodney W. Grimes  *
42df8bae1dSRodney W. Grimes  * Permission to use, copy, modify and distribute this software and
43df8bae1dSRodney W. Grimes  * its documentation is hereby granted, provided that both the copyright
44df8bae1dSRodney W. Grimes  * notice and this permission notice appear in all copies of the
45df8bae1dSRodney W. Grimes  * software, derivative works or modified versions, and any portions
46df8bae1dSRodney W. Grimes  * thereof, and that both notices appear in supporting documentation.
47df8bae1dSRodney W. Grimes  *
48df8bae1dSRodney W. Grimes  * CARNEGIE MELLON ALLOWS FREE USE OF THIS SOFTWARE IN ITS "AS IS"
49df8bae1dSRodney W. Grimes  * CONDITION.  CARNEGIE MELLON DISCLAIMS ANY LIABILITY OF ANY KIND
50df8bae1dSRodney W. Grimes  * FOR ANY DAMAGES WHATSOEVER RESULTING FROM THE USE OF THIS SOFTWARE.
51df8bae1dSRodney W. Grimes  *
52df8bae1dSRodney W. Grimes  * Carnegie Mellon requests users of this software to return to
53df8bae1dSRodney W. Grimes  *
54df8bae1dSRodney W. Grimes  *  Software Distribution Coordinator  or  Software.Distribution@CS.CMU.EDU
55df8bae1dSRodney W. Grimes  *  School of Computer Science
56df8bae1dSRodney W. Grimes  *  Carnegie Mellon University
57df8bae1dSRodney W. Grimes  *  Pittsburgh PA 15213-3890
58df8bae1dSRodney W. Grimes  *
59df8bae1dSRodney W. Grimes  * any improvements or extensions that they make and grant Carnegie the
60df8bae1dSRodney W. Grimes  * rights to redistribute these changes.
61df8bae1dSRodney W. Grimes  */
62df8bae1dSRodney W. Grimes 
63df8bae1dSRodney W. Grimes /*
64df8bae1dSRodney W. Grimes  *	Virtual memory mapping module.
65df8bae1dSRodney W. Grimes  */
66df8bae1dSRodney W. Grimes 
67874651b1SDavid E. O'Brien #include <sys/cdefs.h>
68874651b1SDavid E. O'Brien __FBSDID("$FreeBSD$");
69874651b1SDavid E. O'Brien 
70df8bae1dSRodney W. Grimes #include <sys/param.h>
71df8bae1dSRodney W. Grimes #include <sys/systm.h>
729a6d144fSKonstantin Belousov #include <sys/kernel.h>
7361d80e90SJohn Baldwin #include <sys/ktr.h>
74fb919e4dSMark Murray #include <sys/lock.h>
75fb919e4dSMark Murray #include <sys/mutex.h>
76b5e8ce9fSBruce Evans #include <sys/proc.h>
77efeaf95aSDavid Greenman #include <sys/vmmeter.h>
78867a482dSJohn Dyson #include <sys/mman.h>
791efb74fbSJohn Dyson #include <sys/vnode.h>
801ba5ad42SEdward Tomasz Napierala #include <sys/racct.h>
812267af78SJulian Elischer #include <sys/resourcevar.h>
8289f6b863SAttilio Rao #include <sys/rwlock.h>
833fde38dfSMike Silbersack #include <sys/file.h>
849a6d144fSKonstantin Belousov #include <sys/sysctl.h>
8505ba50f5SJake Burkholder #include <sys/sysent.h>
863db161e0SMatthew Dillon #include <sys/shm.h>
87df8bae1dSRodney W. Grimes 
88df8bae1dSRodney W. Grimes #include <vm/vm.h>
89efeaf95aSDavid Greenman #include <vm/vm_param.h>
90efeaf95aSDavid Greenman #include <vm/pmap.h>
91efeaf95aSDavid Greenman #include <vm/vm_map.h>
92df8bae1dSRodney W. Grimes #include <vm/vm_page.h>
93df8bae1dSRodney W. Grimes #include <vm/vm_object.h>
9447221757SJohn Dyson #include <vm/vm_pager.h>
9526f9a767SRodney W. Grimes #include <vm/vm_kern.h>
96efeaf95aSDavid Greenman #include <vm/vm_extern.h>
9784110e7eSKonstantin Belousov #include <vm/vnode_pager.h>
9821cd6e62SSeigo Tanimura #include <vm/swap_pager.h>
99670d17b5SJeff Roberson #include <vm/uma.h>
100df8bae1dSRodney W. Grimes 
101df8bae1dSRodney W. Grimes /*
102df8bae1dSRodney W. Grimes  *	Virtual memory maps provide for the mapping, protection,
103df8bae1dSRodney W. Grimes  *	and sharing of virtual memory objects.  In addition,
104df8bae1dSRodney W. Grimes  *	this module provides for an efficient virtual copy of
105df8bae1dSRodney W. Grimes  *	memory from one map to another.
106df8bae1dSRodney W. Grimes  *
107df8bae1dSRodney W. Grimes  *	Synchronization is required prior to most operations.
108df8bae1dSRodney W. Grimes  *
109df8bae1dSRodney W. Grimes  *	Maps consist of an ordered doubly-linked list of simple
110e2abaaaaSAlan Cox  *	entries; a self-adjusting binary search tree of these
111e2abaaaaSAlan Cox  *	entries is used to speed up lookups.
112df8bae1dSRodney W. Grimes  *
113956f3135SPhilippe Charnier  *	Since portions of maps are specified by start/end addresses,
114df8bae1dSRodney W. Grimes  *	which may not align with existing map entries, all
115df8bae1dSRodney W. Grimes  *	routines merely "clip" entries to these start/end values.
116df8bae1dSRodney W. Grimes  *	[That is, an entry is split into two, bordering at a
117df8bae1dSRodney W. Grimes  *	start or end value.]  Note that these clippings may not
118df8bae1dSRodney W. Grimes  *	always be necessary (as the two resulting entries are then
119df8bae1dSRodney W. Grimes  *	not changed); however, the clipping is done for convenience.
120df8bae1dSRodney W. Grimes  *
121df8bae1dSRodney W. Grimes  *	As mentioned above, virtual copy operations are performed
122ad5fca3bSAlan Cox  *	by copying VM object references from one map to
123df8bae1dSRodney W. Grimes  *	another, and then marking both regions as copy-on-write.
124df8bae1dSRodney W. Grimes  */
125df8bae1dSRodney W. Grimes 
1263a92e5d5SAlan Cox static struct mtx map_sleep_mtx;
1278355f576SJeff Roberson static uma_zone_t mapentzone;
1288355f576SJeff Roberson static uma_zone_t kmapentzone;
1298355f576SJeff Roberson static uma_zone_t mapzone;
1308355f576SJeff Roberson static uma_zone_t vmspace_zone;
131b23f72e9SBrian Feldman static int vmspace_zinit(void *mem, int size, int flags);
132b23f72e9SBrian Feldman static int vm_map_zinit(void *mem, int ize, int flags);
13392351f16SAlan Cox static void _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min,
13492351f16SAlan Cox     vm_offset_t max);
135*fec29688SAlan Cox static int vm_map_alignspace(vm_map_t map, vm_object_t object,
136*fec29688SAlan Cox     vm_ooffset_t offset, vm_offset_t *addr, vm_size_t length,
137*fec29688SAlan Cox     vm_offset_t max_addr, vm_offset_t alignment);
1380b367bd8SKonstantin Belousov static void vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map);
139655c3490SKonstantin Belousov static void vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry);
14003462509SAlan Cox static void vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry);
14119bd0d9cSKonstantin Belousov static int vm_map_growstack(vm_map_t map, vm_offset_t addr,
14219bd0d9cSKonstantin Belousov     vm_map_entry_t gap_entry);
143077ec27cSAlan Cox static void vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
144077ec27cSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags);
1458355f576SJeff Roberson #ifdef INVARIANTS
1468355f576SJeff Roberson static void vm_map_zdtor(void *mem, int size, void *arg);
1478355f576SJeff Roberson static void vmspace_zdtor(void *mem, int size, void *arg);
1488355f576SJeff Roberson #endif
1494648ba0aSKonstantin Belousov static int vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos,
1504648ba0aSKonstantin Belousov     vm_size_t max_ssize, vm_size_t growsize, vm_prot_t prot, vm_prot_t max,
1514648ba0aSKonstantin Belousov     int cow);
15266cd575bSAlan Cox static void vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
15366cd575bSAlan Cox     vm_offset_t failed_addr);
154b18bfc3dSJohn Dyson 
155ef694c1aSEdward Tomasz Napierala #define	ENTRY_CHARGED(e) ((e)->cred != NULL || \
156ef694c1aSEdward Tomasz Napierala     ((e)->object.vm_object != NULL && (e)->object.vm_object->cred != NULL && \
1573364c323SKonstantin Belousov      !((e)->eflags & MAP_ENTRY_NEEDS_COPY)))
1583364c323SKonstantin Belousov 
15957051fdcSTor Egge /*
16057051fdcSTor Egge  * PROC_VMSPACE_{UN,}LOCK() can be a noop as long as vmspaces are type
16157051fdcSTor Egge  * stable.
16257051fdcSTor Egge  */
16357051fdcSTor Egge #define PROC_VMSPACE_LOCK(p) do { } while (0)
16457051fdcSTor Egge #define PROC_VMSPACE_UNLOCK(p) do { } while (0)
16557051fdcSTor Egge 
166d239bd3cSKonstantin Belousov /*
167d239bd3cSKonstantin Belousov  *	VM_MAP_RANGE_CHECK:	[ internal use only ]
168d239bd3cSKonstantin Belousov  *
169d239bd3cSKonstantin Belousov  *	Asserts that the starting and ending region
170d239bd3cSKonstantin Belousov  *	addresses fall within the valid range of the map.
171d239bd3cSKonstantin Belousov  */
172d239bd3cSKonstantin Belousov #define	VM_MAP_RANGE_CHECK(map, start, end)		\
173d239bd3cSKonstantin Belousov 		{					\
174d239bd3cSKonstantin Belousov 		if (start < vm_map_min(map))		\
175d239bd3cSKonstantin Belousov 			start = vm_map_min(map);	\
176d239bd3cSKonstantin Belousov 		if (end > vm_map_max(map))		\
177d239bd3cSKonstantin Belousov 			end = vm_map_max(map);		\
178d239bd3cSKonstantin Belousov 		if (start > end)			\
179d239bd3cSKonstantin Belousov 			start = end;			\
180d239bd3cSKonstantin Belousov 		}
181d239bd3cSKonstantin Belousov 
1826fecb26bSKonstantin Belousov /*
1836fecb26bSKonstantin Belousov  *	vm_map_startup:
1846fecb26bSKonstantin Belousov  *
1856fecb26bSKonstantin Belousov  *	Initialize the vm_map module.  Must be called before
1866fecb26bSKonstantin Belousov  *	any other vm_map routines.
1876fecb26bSKonstantin Belousov  *
1886fecb26bSKonstantin Belousov  *	Map and entry structures are allocated from the general
1896fecb26bSKonstantin Belousov  *	purpose memory pool with some exceptions:
1906fecb26bSKonstantin Belousov  *
1916fecb26bSKonstantin Belousov  *	- The kernel map and kmem submap are allocated statically.
1926fecb26bSKonstantin Belousov  *	- Kernel map entries are allocated out of a static pool.
1936fecb26bSKonstantin Belousov  *
1946fecb26bSKonstantin Belousov  *	These restrictions are necessary since malloc() uses the
1956fecb26bSKonstantin Belousov  *	maps and requires map entries.
1966fecb26bSKonstantin Belousov  */
1976fecb26bSKonstantin Belousov 
1980d94caffSDavid Greenman void
1991b40f8c0SMatthew Dillon vm_map_startup(void)
200df8bae1dSRodney W. Grimes {
2013a92e5d5SAlan Cox 	mtx_init(&map_sleep_mtx, "vm map sleep mutex", NULL, MTX_DEF);
2028355f576SJeff Roberson 	mapzone = uma_zcreate("MAP", sizeof(struct vm_map), NULL,
2038355f576SJeff Roberson #ifdef INVARIANTS
2048355f576SJeff Roberson 	    vm_map_zdtor,
2058355f576SJeff Roberson #else
2068355f576SJeff Roberson 	    NULL,
2078355f576SJeff Roberson #endif
208f872f6eaSAlan Cox 	    vm_map_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2098355f576SJeff Roberson 	uma_prealloc(mapzone, MAX_KMAP);
210670d17b5SJeff Roberson 	kmapentzone = uma_zcreate("KMAP ENTRY", sizeof(struct vm_map_entry),
21118aa2de5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR,
21218aa2de5SJeff Roberson 	    UMA_ZONE_MTXCLASS | UMA_ZONE_VM);
213670d17b5SJeff Roberson 	mapentzone = uma_zcreate("MAP ENTRY", sizeof(struct vm_map_entry),
214670d17b5SJeff Roberson 	    NULL, NULL, NULL, NULL, UMA_ALIGN_PTR, 0);
2155df87b21SJeff Roberson 	vmspace_zone = uma_zcreate("VMSPACE", sizeof(struct vmspace), NULL,
2165df87b21SJeff Roberson #ifdef INVARIANTS
2175df87b21SJeff Roberson 	    vmspace_zdtor,
2185df87b21SJeff Roberson #else
2195df87b21SJeff Roberson 	    NULL,
2205df87b21SJeff Roberson #endif
221f872f6eaSAlan Cox 	    vmspace_zinit, NULL, UMA_ALIGN_PTR, UMA_ZONE_NOFREE);
2228355f576SJeff Roberson }
2238355f576SJeff Roberson 
224b23f72e9SBrian Feldman static int
225b23f72e9SBrian Feldman vmspace_zinit(void *mem, int size, int flags)
2268355f576SJeff Roberson {
2278355f576SJeff Roberson 	struct vmspace *vm;
2288355f576SJeff Roberson 
2298355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2308355f576SJeff Roberson 
23189b57fcfSKonstantin Belousov 	vm->vm_map.pmap = NULL;
232b23f72e9SBrian Feldman 	(void)vm_map_zinit(&vm->vm_map, sizeof(vm->vm_map), flags);
233e68c64f0SKonstantin Belousov 	PMAP_LOCK_INIT(vmspace_pmap(vm));
234b23f72e9SBrian Feldman 	return (0);
2358355f576SJeff Roberson }
2368355f576SJeff Roberson 
237b23f72e9SBrian Feldman static int
238b23f72e9SBrian Feldman vm_map_zinit(void *mem, int size, int flags)
2398355f576SJeff Roberson {
2408355f576SJeff Roberson 	vm_map_t map;
2418355f576SJeff Roberson 
2428355f576SJeff Roberson 	map = (vm_map_t)mem;
243763d9566STim Kientzle 	memset(map, 0, sizeof(*map));
244e30df26eSAlan Cox 	mtx_init(&map->system_mtx, "vm map (system)", NULL, MTX_DEF | MTX_DUPOK);
245e30df26eSAlan Cox 	sx_init(&map->lock, "vm map (user)");
246b23f72e9SBrian Feldman 	return (0);
2478355f576SJeff Roberson }
2488355f576SJeff Roberson 
2498355f576SJeff Roberson #ifdef INVARIANTS
2508355f576SJeff Roberson static void
2518355f576SJeff Roberson vmspace_zdtor(void *mem, int size, void *arg)
2528355f576SJeff Roberson {
2538355f576SJeff Roberson 	struct vmspace *vm;
2548355f576SJeff Roberson 
2558355f576SJeff Roberson 	vm = (struct vmspace *)mem;
2568355f576SJeff Roberson 
2578355f576SJeff Roberson 	vm_map_zdtor(&vm->vm_map, sizeof(vm->vm_map), arg);
2588355f576SJeff Roberson }
2598355f576SJeff Roberson static void
2608355f576SJeff Roberson vm_map_zdtor(void *mem, int size, void *arg)
2618355f576SJeff Roberson {
2628355f576SJeff Roberson 	vm_map_t map;
2638355f576SJeff Roberson 
2648355f576SJeff Roberson 	map = (vm_map_t)mem;
2658355f576SJeff Roberson 	KASSERT(map->nentries == 0,
2668355f576SJeff Roberson 	    ("map %p nentries == %d on free.",
2678355f576SJeff Roberson 	    map, map->nentries));
2688355f576SJeff Roberson 	KASSERT(map->size == 0,
2698355f576SJeff Roberson 	    ("map %p size == %lu on free.",
2709eb6e519SJeff Roberson 	    map, (unsigned long)map->size));
2718355f576SJeff Roberson }
2728355f576SJeff Roberson #endif	/* INVARIANTS */
2738355f576SJeff Roberson 
274df8bae1dSRodney W. Grimes /*
275df8bae1dSRodney W. Grimes  * Allocate a vmspace structure, including a vm_map and pmap,
276df8bae1dSRodney W. Grimes  * and initialize those structures.  The refcnt is set to 1.
27774d1d2b7SNeel Natu  *
27874d1d2b7SNeel Natu  * If 'pinit' is NULL then the embedded pmap is initialized via pmap_pinit().
279df8bae1dSRodney W. Grimes  */
280df8bae1dSRodney W. Grimes struct vmspace *
28174d1d2b7SNeel Natu vmspace_alloc(vm_offset_t min, vm_offset_t max, pmap_pinit_t pinit)
282df8bae1dSRodney W. Grimes {
283c0877f10SJohn Dyson 	struct vmspace *vm;
2840d94caffSDavid Greenman 
285a163d034SWarner Losh 	vm = uma_zalloc(vmspace_zone, M_WAITOK);
28674d1d2b7SNeel Natu 
28774d1d2b7SNeel Natu 	KASSERT(vm->vm_map.pmap == NULL, ("vm_map.pmap must be NULL"));
28874d1d2b7SNeel Natu 
28974d1d2b7SNeel Natu 	if (pinit == NULL)
29074d1d2b7SNeel Natu 		pinit = &pmap_pinit;
29174d1d2b7SNeel Natu 
29274d1d2b7SNeel Natu 	if (!pinit(vmspace_pmap(vm))) {
29389b57fcfSKonstantin Belousov 		uma_zfree(vmspace_zone, vm);
29489b57fcfSKonstantin Belousov 		return (NULL);
29589b57fcfSKonstantin Belousov 	}
29621c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_alloc: %p", vm);
29792351f16SAlan Cox 	_vm_map_init(&vm->vm_map, vmspace_pmap(vm), min, max);
298df8bae1dSRodney W. Grimes 	vm->vm_refcnt = 1;
2992d8acc0fSJohn Dyson 	vm->vm_shm = NULL;
30051ab6c28SAlan Cox 	vm->vm_swrss = 0;
30151ab6c28SAlan Cox 	vm->vm_tsize = 0;
30251ab6c28SAlan Cox 	vm->vm_dsize = 0;
30351ab6c28SAlan Cox 	vm->vm_ssize = 0;
30451ab6c28SAlan Cox 	vm->vm_taddr = 0;
30551ab6c28SAlan Cox 	vm->vm_daddr = 0;
30651ab6c28SAlan Cox 	vm->vm_maxsaddr = 0;
307df8bae1dSRodney W. Grimes 	return (vm);
308df8bae1dSRodney W. Grimes }
309df8bae1dSRodney W. Grimes 
3104b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
3111ba5ad42SEdward Tomasz Napierala static void
3121ba5ad42SEdward Tomasz Napierala vmspace_container_reset(struct proc *p)
3131ba5ad42SEdward Tomasz Napierala {
3141ba5ad42SEdward Tomasz Napierala 
3151ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
3161ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_DATA, 0);
3171ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_STACK, 0);
3181ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_RSS, 0);
3191ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_MEMLOCK, 0);
3201ba5ad42SEdward Tomasz Napierala 	racct_set(p, RACCT_VMEM, 0);
3211ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
3221ba5ad42SEdward Tomasz Napierala }
3234b5c9cf6SEdward Tomasz Napierala #endif
3241ba5ad42SEdward Tomasz Napierala 
32562a59e8fSWarner Losh static inline void
326582ec34cSAlfred Perlstein vmspace_dofree(struct vmspace *vm)
327df8bae1dSRodney W. Grimes {
3280ef12795SAlan Cox 
32921c641b2SJohn Baldwin 	CTR1(KTR_VM, "vmspace_free: %p", vm);
3303db161e0SMatthew Dillon 
3313db161e0SMatthew Dillon 	/*
3323db161e0SMatthew Dillon 	 * Make sure any SysV shm is freed, it might not have been in
3333db161e0SMatthew Dillon 	 * exit1().
3343db161e0SMatthew Dillon 	 */
3353db161e0SMatthew Dillon 	shmexit(vm);
3363db161e0SMatthew Dillon 
33730dcfc09SJohn Dyson 	/*
338df8bae1dSRodney W. Grimes 	 * Lock the map, to wait out all other references to it.
3390d94caffSDavid Greenman 	 * Delete all of the mappings and pages they hold, then call
3400d94caffSDavid Greenman 	 * the pmap module to reclaim anything left.
341df8bae1dSRodney W. Grimes 	 */
342717f7d59SAlan Cox 	(void)vm_map_remove(&vm->vm_map, vm->vm_map.min_offset,
343df8bae1dSRodney W. Grimes 	    vm->vm_map.max_offset);
3448355f576SJeff Roberson 
3450ef12795SAlan Cox 	pmap_release(vmspace_pmap(vm));
3460ef12795SAlan Cox 	vm->vm_map.pmap = NULL;
3478355f576SJeff Roberson 	uma_zfree(vmspace_zone, vm);
348df8bae1dSRodney W. Grimes }
349582ec34cSAlfred Perlstein 
350582ec34cSAlfred Perlstein void
351582ec34cSAlfred Perlstein vmspace_free(struct vmspace *vm)
352582ec34cSAlfred Perlstein {
353582ec34cSAlfred Perlstein 
354423521aaSRyan Stone 	WITNESS_WARN(WARN_GIANTOK | WARN_SLEEPOK, NULL,
355164a37a5SJohn Baldwin 	    "vmspace_free() called");
356423521aaSRyan Stone 
357582ec34cSAlfred Perlstein 	if (vm->vm_refcnt == 0)
358582ec34cSAlfred Perlstein 		panic("vmspace_free: attempt to free already freed vmspace");
359582ec34cSAlfred Perlstein 
3601a587ef2SJohn Baldwin 	if (atomic_fetchadd_int(&vm->vm_refcnt, -1) == 1)
361582ec34cSAlfred Perlstein 		vmspace_dofree(vm);
362582ec34cSAlfred Perlstein }
363582ec34cSAlfred Perlstein 
364582ec34cSAlfred Perlstein void
365582ec34cSAlfred Perlstein vmspace_exitfree(struct proc *p)
366582ec34cSAlfred Perlstein {
367334f7061SPeter Wemm 	struct vmspace *vm;
368582ec34cSAlfred Perlstein 
36957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
370334f7061SPeter Wemm 	vm = p->p_vmspace;
371334f7061SPeter Wemm 	p->p_vmspace = NULL;
37257051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
37357051fdcSTor Egge 	KASSERT(vm == &vmspace0, ("vmspace_exitfree: wrong vmspace"));
37457051fdcSTor Egge 	vmspace_free(vm);
37557051fdcSTor Egge }
37657051fdcSTor Egge 
37757051fdcSTor Egge void
37857051fdcSTor Egge vmspace_exit(struct thread *td)
37957051fdcSTor Egge {
38057051fdcSTor Egge 	int refcnt;
38157051fdcSTor Egge 	struct vmspace *vm;
38257051fdcSTor Egge 	struct proc *p;
383389d2b6eSMatthew Dillon 
384389d2b6eSMatthew Dillon 	/*
38557051fdcSTor Egge 	 * Release user portion of address space.
38657051fdcSTor Egge 	 * This releases references to vnodes,
38757051fdcSTor Egge 	 * which could cause I/O if the file has been unlinked.
38857051fdcSTor Egge 	 * Need to do this early enough that we can still sleep.
389389d2b6eSMatthew Dillon 	 *
39057051fdcSTor Egge 	 * The last exiting process to reach this point releases as
39157051fdcSTor Egge 	 * much of the environment as it can. vmspace_dofree() is the
39257051fdcSTor Egge 	 * slower fallback in case another process had a temporary
39357051fdcSTor Egge 	 * reference to the vmspace.
394389d2b6eSMatthew Dillon 	 */
39557051fdcSTor Egge 
39657051fdcSTor Egge 	p = td->td_proc;
39757051fdcSTor Egge 	vm = p->p_vmspace;
39857051fdcSTor Egge 	atomic_add_int(&vmspace0.vm_refcnt, 1);
39957051fdcSTor Egge 	do {
40057051fdcSTor Egge 		refcnt = vm->vm_refcnt;
40157051fdcSTor Egge 		if (refcnt > 1 && p->p_vmspace != &vmspace0) {
40257051fdcSTor Egge 			/* Switch now since other proc might free vmspace */
40357051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
40457051fdcSTor Egge 			p->p_vmspace = &vmspace0;
40557051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
40657051fdcSTor Egge 			pmap_activate(td);
40757051fdcSTor Egge 		}
40857051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt - 1));
40957051fdcSTor Egge 	if (refcnt == 1) {
41057051fdcSTor Egge 		if (p->p_vmspace != vm) {
41157051fdcSTor Egge 			/* vmspace not yet freed, switch back */
41257051fdcSTor Egge 			PROC_VMSPACE_LOCK(p);
41357051fdcSTor Egge 			p->p_vmspace = vm;
41457051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
41557051fdcSTor Egge 			pmap_activate(td);
41657051fdcSTor Egge 		}
41757051fdcSTor Egge 		pmap_remove_pages(vmspace_pmap(vm));
41857051fdcSTor Egge 		/* Switch now since this proc will free vmspace */
41957051fdcSTor Egge 		PROC_VMSPACE_LOCK(p);
42057051fdcSTor Egge 		p->p_vmspace = &vmspace0;
42157051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
42257051fdcSTor Egge 		pmap_activate(td);
423334f7061SPeter Wemm 		vmspace_dofree(vm);
424334f7061SPeter Wemm 	}
4254b5c9cf6SEdward Tomasz Napierala #ifdef RACCT
4264b5c9cf6SEdward Tomasz Napierala 	if (racct_enable)
4271ba5ad42SEdward Tomasz Napierala 		vmspace_container_reset(p);
4284b5c9cf6SEdward Tomasz Napierala #endif
42957051fdcSTor Egge }
43057051fdcSTor Egge 
43157051fdcSTor Egge /* Acquire reference to vmspace owned by another process. */
43257051fdcSTor Egge 
43357051fdcSTor Egge struct vmspace *
43457051fdcSTor Egge vmspace_acquire_ref(struct proc *p)
43557051fdcSTor Egge {
43657051fdcSTor Egge 	struct vmspace *vm;
43757051fdcSTor Egge 	int refcnt;
43857051fdcSTor Egge 
43957051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
44057051fdcSTor Egge 	vm = p->p_vmspace;
44157051fdcSTor Egge 	if (vm == NULL) {
44257051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
44357051fdcSTor Egge 		return (NULL);
44457051fdcSTor Egge 	}
44557051fdcSTor Egge 	do {
44657051fdcSTor Egge 		refcnt = vm->vm_refcnt;
44757051fdcSTor Egge 		if (refcnt <= 0) { 	/* Avoid 0->1 transition */
44857051fdcSTor Egge 			PROC_VMSPACE_UNLOCK(p);
44957051fdcSTor Egge 			return (NULL);
45057051fdcSTor Egge 		}
45157051fdcSTor Egge 	} while (!atomic_cmpset_int(&vm->vm_refcnt, refcnt, refcnt + 1));
45257051fdcSTor Egge 	if (vm != p->p_vmspace) {
45357051fdcSTor Egge 		PROC_VMSPACE_UNLOCK(p);
45457051fdcSTor Egge 		vmspace_free(vm);
45557051fdcSTor Egge 		return (NULL);
45657051fdcSTor Egge 	}
45757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
45857051fdcSTor Egge 	return (vm);
45957051fdcSTor Egge }
460df8bae1dSRodney W. Grimes 
4618a4dc40fSJohn Baldwin /*
4628a4dc40fSJohn Baldwin  * Switch between vmspaces in an AIO kernel process.
4638a4dc40fSJohn Baldwin  *
4648a4dc40fSJohn Baldwin  * The AIO kernel processes switch to and from a user process's
4658a4dc40fSJohn Baldwin  * vmspace while performing an I/O operation on behalf of a user
4668a4dc40fSJohn Baldwin  * process.  The new vmspace is either the vmspace of a user process
4678a4dc40fSJohn Baldwin  * obtained from an active AIO request or the initial vmspace of the
4688a4dc40fSJohn Baldwin  * AIO kernel process (when it is idling).  Because user processes
4698a4dc40fSJohn Baldwin  * will block to drain any active AIO requests before proceeding in
4708a4dc40fSJohn Baldwin  * exit() or execve(), the vmspace reference count for these vmspaces
4718a4dc40fSJohn Baldwin  * can never be 0.  This allows for a much simpler implementation than
4728a4dc40fSJohn Baldwin  * the loop in vmspace_acquire_ref() above.  Similarly, AIO kernel
4738a4dc40fSJohn Baldwin  * processes hold an extra reference on their initial vmspace for the
4748a4dc40fSJohn Baldwin  * life of the process so that this guarantee is true for any vmspace
4758a4dc40fSJohn Baldwin  * passed as 'newvm'.
4768a4dc40fSJohn Baldwin  */
4778a4dc40fSJohn Baldwin void
4788a4dc40fSJohn Baldwin vmspace_switch_aio(struct vmspace *newvm)
4798a4dc40fSJohn Baldwin {
4808a4dc40fSJohn Baldwin 	struct vmspace *oldvm;
4818a4dc40fSJohn Baldwin 
4828a4dc40fSJohn Baldwin 	/* XXX: Need some way to assert that this is an aio daemon. */
4838a4dc40fSJohn Baldwin 
4848a4dc40fSJohn Baldwin 	KASSERT(newvm->vm_refcnt > 0,
4858a4dc40fSJohn Baldwin 	    ("vmspace_switch_aio: newvm unreferenced"));
4868a4dc40fSJohn Baldwin 
4878a4dc40fSJohn Baldwin 	oldvm = curproc->p_vmspace;
4888a4dc40fSJohn Baldwin 	if (oldvm == newvm)
4898a4dc40fSJohn Baldwin 		return;
4908a4dc40fSJohn Baldwin 
4918a4dc40fSJohn Baldwin 	/*
4928a4dc40fSJohn Baldwin 	 * Point to the new address space and refer to it.
4938a4dc40fSJohn Baldwin 	 */
4948a4dc40fSJohn Baldwin 	curproc->p_vmspace = newvm;
4958a4dc40fSJohn Baldwin 	atomic_add_int(&newvm->vm_refcnt, 1);
4968a4dc40fSJohn Baldwin 
4978a4dc40fSJohn Baldwin 	/* Activate the new mapping. */
4988a4dc40fSJohn Baldwin 	pmap_activate(curthread);
4998a4dc40fSJohn Baldwin 
5008a4dc40fSJohn Baldwin 	/* Remove the daemon's reference to the old address space. */
5018a4dc40fSJohn Baldwin 	KASSERT(oldvm->vm_refcnt > 1,
5028a4dc40fSJohn Baldwin 	    ("vmspace_switch_aio: oldvm dropping last reference"));
5038a4dc40fSJohn Baldwin 	vmspace_free(oldvm);
5048a4dc40fSJohn Baldwin }
5058a4dc40fSJohn Baldwin 
5061b40f8c0SMatthew Dillon void
507780b1c09SAlan Cox _vm_map_lock(vm_map_t map, const char *file, int line)
5081b40f8c0SMatthew Dillon {
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_xlock_(&map->lock, file, line);
5141b40f8c0SMatthew Dillon 	map->timestamp++;
5151b40f8c0SMatthew Dillon }
5161b40f8c0SMatthew Dillon 
5170b367bd8SKonstantin Belousov static void
5180b367bd8SKonstantin Belousov vm_map_process_deferred(void)
5190e0af8ecSBrian Feldman {
5200b367bd8SKonstantin Belousov 	struct thread *td;
5216fbe60faSJohn Baldwin 	vm_map_entry_t entry, next;
52284110e7eSKonstantin Belousov 	vm_object_t object;
523655c3490SKonstantin Belousov 
5240b367bd8SKonstantin Belousov 	td = curthread;
5256fbe60faSJohn Baldwin 	entry = td->td_map_def_user;
5266fbe60faSJohn Baldwin 	td->td_map_def_user = NULL;
5276fbe60faSJohn Baldwin 	while (entry != NULL) {
5286fbe60faSJohn Baldwin 		next = entry->next;
52984110e7eSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_VN_WRITECNT) != 0) {
53084110e7eSKonstantin Belousov 			/*
53184110e7eSKonstantin Belousov 			 * Decrement the object's writemappings and
53284110e7eSKonstantin Belousov 			 * possibly the vnode's v_writecount.
53384110e7eSKonstantin Belousov 			 */
53484110e7eSKonstantin Belousov 			KASSERT((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0,
53584110e7eSKonstantin Belousov 			    ("Submap with writecount"));
53684110e7eSKonstantin Belousov 			object = entry->object.vm_object;
53784110e7eSKonstantin Belousov 			KASSERT(object != NULL, ("No object for writecount"));
53884110e7eSKonstantin Belousov 			vnode_pager_release_writecount(object, entry->start,
53984110e7eSKonstantin Belousov 			    entry->end);
54084110e7eSKonstantin Belousov 		}
5410b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, FALSE);
5426fbe60faSJohn Baldwin 		entry = next;
5430b367bd8SKonstantin Belousov 	}
5440b367bd8SKonstantin Belousov }
5450b367bd8SKonstantin Belousov 
5460b367bd8SKonstantin Belousov void
5470b367bd8SKonstantin Belousov _vm_map_unlock(vm_map_t map, const char *file, int line)
5480b367bd8SKonstantin Belousov {
5490b367bd8SKonstantin Belousov 
5500b367bd8SKonstantin Belousov 	if (map->system_map)
551ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5520b367bd8SKonstantin Belousov 	else {
5539fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
5540b367bd8SKonstantin Belousov 		vm_map_process_deferred();
555655c3490SKonstantin Belousov 	}
5560e0af8ecSBrian Feldman }
5570e0af8ecSBrian Feldman 
5580e0af8ecSBrian Feldman void
559780b1c09SAlan Cox _vm_map_lock_read(vm_map_t map, const char *file, int line)
5600e0af8ecSBrian Feldman {
561bc91c510SAlan Cox 
56293bc4879SAlan Cox 	if (map->system_map)
563ccdf2333SAttilio Rao 		mtx_lock_flags_(&map->system_mtx, 0, file, line);
56412c64974SMaxime Henrion 	else
5659fde98bbSAttilio Rao 		sx_slock_(&map->lock, file, line);
56636daaecdSAlan Cox }
5670e0af8ecSBrian Feldman 
5680e0af8ecSBrian Feldman void
569780b1c09SAlan Cox _vm_map_unlock_read(vm_map_t map, const char *file, int line)
5700e0af8ecSBrian Feldman {
571bc91c510SAlan Cox 
57236daaecdSAlan Cox 	if (map->system_map)
573ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
5740b367bd8SKonstantin Belousov 	else {
5759fde98bbSAttilio Rao 		sx_sunlock_(&map->lock, file, line);
5760b367bd8SKonstantin Belousov 		vm_map_process_deferred();
5770b367bd8SKonstantin Belousov 	}
57825adb370SBrian Feldman }
57925adb370SBrian Feldman 
580d974f03cSAlan Cox int
581780b1c09SAlan Cox _vm_map_trylock(vm_map_t map, const char *file, int line)
582d974f03cSAlan Cox {
58325adb370SBrian Feldman 	int error;
58425adb370SBrian Feldman 
58536daaecdSAlan Cox 	error = map->system_map ?
586ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
5879fde98bbSAttilio Rao 	    !sx_try_xlock_(&map->lock, file, line);
5883a92e5d5SAlan Cox 	if (error == 0)
5893a92e5d5SAlan Cox 		map->timestamp++;
590bc91c510SAlan Cox 	return (error == 0);
5910e0af8ecSBrian Feldman }
5920e0af8ecSBrian Feldman 
5930e0af8ecSBrian Feldman int
59472d97679SDavid Schultz _vm_map_trylock_read(vm_map_t map, const char *file, int line)
59572d97679SDavid Schultz {
59672d97679SDavid Schultz 	int error;
59772d97679SDavid Schultz 
59872d97679SDavid Schultz 	error = map->system_map ?
599ccdf2333SAttilio Rao 	    !mtx_trylock_flags_(&map->system_mtx, 0, file, line) :
6009fde98bbSAttilio Rao 	    !sx_try_slock_(&map->lock, file, line);
60172d97679SDavid Schultz 	return (error == 0);
60272d97679SDavid Schultz }
60372d97679SDavid Schultz 
60405a8c414SAlan Cox /*
60505a8c414SAlan Cox  *	_vm_map_lock_upgrade:	[ internal use only ]
60605a8c414SAlan Cox  *
60705a8c414SAlan Cox  *	Tries to upgrade a read (shared) lock on the specified map to a write
60805a8c414SAlan Cox  *	(exclusive) lock.  Returns the value "0" if the upgrade succeeds and a
60905a8c414SAlan Cox  *	non-zero value if the upgrade fails.  If the upgrade fails, the map is
61005a8c414SAlan Cox  *	returned without a read or write lock held.
61105a8c414SAlan Cox  *
61205a8c414SAlan Cox  *	Requires that the map be read locked.
61305a8c414SAlan Cox  */
61472d97679SDavid Schultz int
615780b1c09SAlan Cox _vm_map_lock_upgrade(vm_map_t map, const char *file, int line)
6160e0af8ecSBrian Feldman {
61705a8c414SAlan Cox 	unsigned int last_timestamp;
618bc91c510SAlan Cox 
61912c64974SMaxime Henrion 	if (map->system_map) {
620ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
62105a8c414SAlan Cox 	} else {
6229fde98bbSAttilio Rao 		if (!sx_try_upgrade_(&map->lock, file, line)) {
62305a8c414SAlan Cox 			last_timestamp = map->timestamp;
6249fde98bbSAttilio Rao 			sx_sunlock_(&map->lock, file, line);
6250b367bd8SKonstantin Belousov 			vm_map_process_deferred();
62605a8c414SAlan Cox 			/*
62705a8c414SAlan Cox 			 * If the map's timestamp does not change while the
62805a8c414SAlan Cox 			 * map is unlocked, then the upgrade succeeds.
62905a8c414SAlan Cox 			 */
6309fde98bbSAttilio Rao 			sx_xlock_(&map->lock, file, line);
63105a8c414SAlan Cox 			if (last_timestamp != map->timestamp) {
6329fde98bbSAttilio Rao 				sx_xunlock_(&map->lock, file, line);
63305a8c414SAlan Cox 				return (1);
63405a8c414SAlan Cox 			}
63505a8c414SAlan Cox 		}
63605a8c414SAlan Cox 	}
637bc91c510SAlan Cox 	map->timestamp++;
638bc91c510SAlan Cox 	return (0);
6390e0af8ecSBrian Feldman }
6400e0af8ecSBrian Feldman 
6410e0af8ecSBrian Feldman void
642780b1c09SAlan Cox _vm_map_lock_downgrade(vm_map_t map, const char *file, int line)
6431b40f8c0SMatthew Dillon {
644bc91c510SAlan Cox 
64512c64974SMaxime Henrion 	if (map->system_map) {
646ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
64705a8c414SAlan Cox 	} else
6489fde98bbSAttilio Rao 		sx_downgrade_(&map->lock, file, line);
64905a8c414SAlan Cox }
65005a8c414SAlan Cox 
65105a8c414SAlan Cox /*
65205a8c414SAlan Cox  *	vm_map_locked:
65305a8c414SAlan Cox  *
65405a8c414SAlan Cox  *	Returns a non-zero value if the caller holds a write (exclusive) lock
65505a8c414SAlan Cox  *	on the specified map and the value "0" otherwise.
65605a8c414SAlan Cox  */
65705a8c414SAlan Cox int
65805a8c414SAlan Cox vm_map_locked(vm_map_t map)
65905a8c414SAlan Cox {
66005a8c414SAlan Cox 
66105a8c414SAlan Cox 	if (map->system_map)
66205a8c414SAlan Cox 		return (mtx_owned(&map->system_mtx));
66305a8c414SAlan Cox 	else
66405a8c414SAlan Cox 		return (sx_xlocked(&map->lock));
66525adb370SBrian Feldman }
66625adb370SBrian Feldman 
6673a0916b8SKonstantin Belousov #ifdef INVARIANTS
6683a0916b8SKonstantin Belousov static void
6693a0916b8SKonstantin Belousov _vm_map_assert_locked(vm_map_t map, const char *file, int line)
6703a0916b8SKonstantin Belousov {
6713a0916b8SKonstantin Belousov 
6723a0916b8SKonstantin Belousov 	if (map->system_map)
673ccdf2333SAttilio Rao 		mtx_assert_(&map->system_mtx, MA_OWNED, file, line);
6743a0916b8SKonstantin Belousov 	else
6759fde98bbSAttilio Rao 		sx_assert_(&map->lock, SA_XLOCKED, file, line);
6763a0916b8SKonstantin Belousov }
6773a0916b8SKonstantin Belousov 
6783a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map) \
6793a0916b8SKonstantin Belousov     _vm_map_assert_locked(map, LOCK_FILE, LOCK_LINE)
6803a0916b8SKonstantin Belousov #else
6813a0916b8SKonstantin Belousov #define	VM_MAP_ASSERT_LOCKED(map)
6823a0916b8SKonstantin Belousov #endif
6833a0916b8SKonstantin Belousov 
684acd9a301SAlan Cox /*
6858304adaaSAlan Cox  *	_vm_map_unlock_and_wait:
6868304adaaSAlan Cox  *
6878304adaaSAlan Cox  *	Atomically releases the lock on the specified map and puts the calling
6888304adaaSAlan Cox  *	thread to sleep.  The calling thread will remain asleep until either
6898304adaaSAlan Cox  *	vm_map_wakeup() is performed on the map or the specified timeout is
6908304adaaSAlan Cox  *	exceeded.
6918304adaaSAlan Cox  *
6928304adaaSAlan Cox  *	WARNING!  This function does not perform deferred deallocations of
6938304adaaSAlan Cox  *	objects and map	entries.  Therefore, the calling thread is expected to
6948304adaaSAlan Cox  *	reacquire the map lock after reawakening and later perform an ordinary
6958304adaaSAlan Cox  *	unlock operation, such as vm_map_unlock(), before completing its
6968304adaaSAlan Cox  *	operation on the map.
697acd9a301SAlan Cox  */
6989688f931SAlan Cox int
6998304adaaSAlan Cox _vm_map_unlock_and_wait(vm_map_t map, int timo, const char *file, int line)
700acd9a301SAlan Cox {
701acd9a301SAlan Cox 
7023a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
7038304adaaSAlan Cox 	if (map->system_map)
704ccdf2333SAttilio Rao 		mtx_unlock_flags_(&map->system_mtx, 0, file, line);
7058304adaaSAlan Cox 	else
7069fde98bbSAttilio Rao 		sx_xunlock_(&map->lock, file, line);
7078304adaaSAlan Cox 	return (msleep(&map->root, &map_sleep_mtx, PDROP | PVM, "vmmaps",
7088304adaaSAlan Cox 	    timo));
709acd9a301SAlan Cox }
710acd9a301SAlan Cox 
711acd9a301SAlan Cox /*
712acd9a301SAlan Cox  *	vm_map_wakeup:
7138304adaaSAlan Cox  *
7148304adaaSAlan Cox  *	Awaken any threads that have slept on the map using
7158304adaaSAlan Cox  *	vm_map_unlock_and_wait().
716acd9a301SAlan Cox  */
7179688f931SAlan Cox void
718acd9a301SAlan Cox vm_map_wakeup(vm_map_t map)
719acd9a301SAlan Cox {
720acd9a301SAlan Cox 
721b49ecb86SAlan Cox 	/*
7223a92e5d5SAlan Cox 	 * Acquire and release map_sleep_mtx to prevent a wakeup()
7238304adaaSAlan Cox 	 * from being performed (and lost) between the map unlock
7248304adaaSAlan Cox 	 * and the msleep() in _vm_map_unlock_and_wait().
725b49ecb86SAlan Cox 	 */
7263a92e5d5SAlan Cox 	mtx_lock(&map_sleep_mtx);
7273a92e5d5SAlan Cox 	mtx_unlock(&map_sleep_mtx);
728acd9a301SAlan Cox 	wakeup(&map->root);
729acd9a301SAlan Cox }
730acd9a301SAlan Cox 
731a5db445dSMax Laier void
732a5db445dSMax Laier vm_map_busy(vm_map_t map)
733a5db445dSMax Laier {
734a5db445dSMax Laier 
735a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
736a5db445dSMax Laier 	map->busy++;
737a5db445dSMax Laier }
738a5db445dSMax Laier 
739a5db445dSMax Laier void
740a5db445dSMax Laier vm_map_unbusy(vm_map_t map)
741a5db445dSMax Laier {
742a5db445dSMax Laier 
743a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
744a5db445dSMax Laier 	KASSERT(map->busy, ("vm_map_unbusy: not busy"));
745a5db445dSMax Laier 	if (--map->busy == 0 && (map->flags & MAP_BUSY_WAKEUP)) {
746a5db445dSMax Laier 		vm_map_modflags(map, 0, MAP_BUSY_WAKEUP);
747a5db445dSMax Laier 		wakeup(&map->busy);
748a5db445dSMax Laier 	}
749a5db445dSMax Laier }
750a5db445dSMax Laier 
751a5db445dSMax Laier void
752a5db445dSMax Laier vm_map_wait_busy(vm_map_t map)
753a5db445dSMax Laier {
754a5db445dSMax Laier 
755a5db445dSMax Laier 	VM_MAP_ASSERT_LOCKED(map);
756a5db445dSMax Laier 	while (map->busy) {
757a5db445dSMax Laier 		vm_map_modflags(map, MAP_BUSY_WAKEUP, 0);
758a5db445dSMax Laier 		if (map->system_map)
759a5db445dSMax Laier 			msleep(&map->busy, &map->system_mtx, 0, "mbusy", 0);
760a5db445dSMax Laier 		else
761a5db445dSMax Laier 			sx_sleep(&map->busy, &map->lock, 0, "mbusy", 0);
762a5db445dSMax Laier 	}
763a5db445dSMax Laier 	map->timestamp++;
764a5db445dSMax Laier }
765a5db445dSMax Laier 
7661b40f8c0SMatthew Dillon long
7671b40f8c0SMatthew Dillon vmspace_resident_count(struct vmspace *vmspace)
7681b40f8c0SMatthew Dillon {
7691b40f8c0SMatthew Dillon 	return pmap_resident_count(vmspace_pmap(vmspace));
7701b40f8c0SMatthew Dillon }
7711b40f8c0SMatthew Dillon 
772ff2b5645SMatthew Dillon /*
773df8bae1dSRodney W. Grimes  *	vm_map_create:
774df8bae1dSRodney W. Grimes  *
775df8bae1dSRodney W. Grimes  *	Creates and returns a new empty VM map with
776df8bae1dSRodney W. Grimes  *	the given physical map structure, and having
777df8bae1dSRodney W. Grimes  *	the given lower and upper address bounds.
778df8bae1dSRodney W. Grimes  */
7790d94caffSDavid Greenman vm_map_t
7801b40f8c0SMatthew Dillon vm_map_create(pmap_t pmap, vm_offset_t min, vm_offset_t max)
781df8bae1dSRodney W. Grimes {
782c0877f10SJohn Dyson 	vm_map_t result;
783df8bae1dSRodney W. Grimes 
784a163d034SWarner Losh 	result = uma_zalloc(mapzone, M_WAITOK);
78521c641b2SJohn Baldwin 	CTR1(KTR_VM, "vm_map_create: %p", result);
78692351f16SAlan Cox 	_vm_map_init(result, pmap, min, max);
787df8bae1dSRodney W. Grimes 	return (result);
788df8bae1dSRodney W. Grimes }
789df8bae1dSRodney W. Grimes 
790df8bae1dSRodney W. Grimes /*
791df8bae1dSRodney W. Grimes  * Initialize an existing vm_map structure
792df8bae1dSRodney W. Grimes  * such as that in the vmspace structure.
793df8bae1dSRodney W. Grimes  */
7948355f576SJeff Roberson static void
79592351f16SAlan Cox _vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
796df8bae1dSRodney W. Grimes {
79721c641b2SJohn Baldwin 
798df8bae1dSRodney W. Grimes 	map->header.next = map->header.prev = &map->header;
7999688f931SAlan Cox 	map->needs_wakeup = FALSE;
8003075778bSJohn Dyson 	map->system_map = 0;
80192351f16SAlan Cox 	map->pmap = pmap;
802df8bae1dSRodney W. Grimes 	map->min_offset = min;
803df8bae1dSRodney W. Grimes 	map->max_offset = max;
804af7cd0c5SBrian Feldman 	map->flags = 0;
8054e94f402SAlan Cox 	map->root = NULL;
806df8bae1dSRodney W. Grimes 	map->timestamp = 0;
807a5db445dSMax Laier 	map->busy = 0;
808df8bae1dSRodney W. Grimes }
809df8bae1dSRodney W. Grimes 
810a18b1f1dSJason Evans void
81192351f16SAlan Cox vm_map_init(vm_map_t map, pmap_t pmap, vm_offset_t min, vm_offset_t max)
812a18b1f1dSJason Evans {
81392351f16SAlan Cox 
81492351f16SAlan Cox 	_vm_map_init(map, pmap, min, max);
815d923c598SAlan Cox 	mtx_init(&map->system_mtx, "system map", NULL, MTX_DEF | MTX_DUPOK);
81612c64974SMaxime Henrion 	sx_init(&map->lock, "user map");
817a18b1f1dSJason Evans }
818a18b1f1dSJason Evans 
819df8bae1dSRodney W. Grimes /*
820b18bfc3dSJohn Dyson  *	vm_map_entry_dispose:	[ internal use only ]
821b18bfc3dSJohn Dyson  *
822b18bfc3dSJohn Dyson  *	Inverse of vm_map_entry_create.
823b18bfc3dSJohn Dyson  */
82462487bb4SJohn Dyson static void
8251b40f8c0SMatthew Dillon vm_map_entry_dispose(vm_map_t map, vm_map_entry_t entry)
826b18bfc3dSJohn Dyson {
8272b4a2c27SAlan Cox 	uma_zfree(map->system_map ? kmapentzone : mapentzone, entry);
828b18bfc3dSJohn Dyson }
829b18bfc3dSJohn Dyson 
830b18bfc3dSJohn Dyson /*
831df8bae1dSRodney W. Grimes  *	vm_map_entry_create:	[ internal use only ]
832df8bae1dSRodney W. Grimes  *
833df8bae1dSRodney W. Grimes  *	Allocates a VM map entry for insertion.
834b28cb1caSAlfred Perlstein  *	No entry fields are filled in.
835df8bae1dSRodney W. Grimes  */
836f708ef1bSPoul-Henning Kamp static vm_map_entry_t
8371b40f8c0SMatthew Dillon vm_map_entry_create(vm_map_t map)
838df8bae1dSRodney W. Grimes {
8391f6889a1SMatthew Dillon 	vm_map_entry_t new_entry;
8401f6889a1SMatthew Dillon 
8412b4a2c27SAlan Cox 	if (map->system_map)
8422b4a2c27SAlan Cox 		new_entry = uma_zalloc(kmapentzone, M_NOWAIT);
8432b4a2c27SAlan Cox 	else
844a163d034SWarner Losh 		new_entry = uma_zalloc(mapentzone, M_WAITOK);
8451f6889a1SMatthew Dillon 	if (new_entry == NULL)
8461f6889a1SMatthew Dillon 		panic("vm_map_entry_create: kernel resources exhausted");
8471f6889a1SMatthew Dillon 	return (new_entry);
848df8bae1dSRodney W. Grimes }
849df8bae1dSRodney W. Grimes 
850df8bae1dSRodney W. Grimes /*
851794316a8SAlan Cox  *	vm_map_entry_set_behavior:
852794316a8SAlan Cox  *
853794316a8SAlan Cox  *	Set the expected access behavior, either normal, random, or
854794316a8SAlan Cox  *	sequential.
855794316a8SAlan Cox  */
85662a59e8fSWarner Losh static inline void
857794316a8SAlan Cox vm_map_entry_set_behavior(vm_map_entry_t entry, u_char behavior)
858794316a8SAlan Cox {
859794316a8SAlan Cox 	entry->eflags = (entry->eflags & ~MAP_ENTRY_BEHAV_MASK) |
860794316a8SAlan Cox 	    (behavior & MAP_ENTRY_BEHAV_MASK);
861794316a8SAlan Cox }
862794316a8SAlan Cox 
863794316a8SAlan Cox /*
8640164e057SAlan Cox  *	vm_map_entry_set_max_free:
8650164e057SAlan Cox  *
8660164e057SAlan Cox  *	Set the max_free field in a vm_map_entry.
8670164e057SAlan Cox  */
86862a59e8fSWarner Losh static inline void
8690164e057SAlan Cox vm_map_entry_set_max_free(vm_map_entry_t entry)
8700164e057SAlan Cox {
8710164e057SAlan Cox 
8720164e057SAlan Cox 	entry->max_free = entry->adj_free;
8730164e057SAlan Cox 	if (entry->left != NULL && entry->left->max_free > entry->max_free)
8740164e057SAlan Cox 		entry->max_free = entry->left->max_free;
8750164e057SAlan Cox 	if (entry->right != NULL && entry->right->max_free > entry->max_free)
8760164e057SAlan Cox 		entry->max_free = entry->right->max_free;
8770164e057SAlan Cox }
8780164e057SAlan Cox 
8790164e057SAlan Cox /*
8804e94f402SAlan Cox  *	vm_map_entry_splay:
8814e94f402SAlan Cox  *
8820164e057SAlan Cox  *	The Sleator and Tarjan top-down splay algorithm with the
8830164e057SAlan Cox  *	following variation.  Max_free must be computed bottom-up, so
8840164e057SAlan Cox  *	on the downward pass, maintain the left and right spines in
8850164e057SAlan Cox  *	reverse order.  Then, make a second pass up each side to fix
8860164e057SAlan Cox  *	the pointers and compute max_free.  The time bound is O(log n)
8870164e057SAlan Cox  *	amortized.
8880164e057SAlan Cox  *
8890164e057SAlan Cox  *	The new root is the vm_map_entry containing "addr", or else an
8900164e057SAlan Cox  *	adjacent entry (lower or higher) if addr is not in the tree.
8910164e057SAlan Cox  *
8920164e057SAlan Cox  *	The map must be locked, and leaves it so.
8930164e057SAlan Cox  *
8940164e057SAlan Cox  *	Returns: the new root.
8954e94f402SAlan Cox  */
8964e94f402SAlan Cox static vm_map_entry_t
8970164e057SAlan Cox vm_map_entry_splay(vm_offset_t addr, vm_map_entry_t root)
8984e94f402SAlan Cox {
8990164e057SAlan Cox 	vm_map_entry_t llist, rlist;
9000164e057SAlan Cox 	vm_map_entry_t ltree, rtree;
9010164e057SAlan Cox 	vm_map_entry_t y;
9024e94f402SAlan Cox 
9030164e057SAlan Cox 	/* Special case of empty tree. */
9044e94f402SAlan Cox 	if (root == NULL)
9054e94f402SAlan Cox 		return (root);
9060164e057SAlan Cox 
9070164e057SAlan Cox 	/*
9080164e057SAlan Cox 	 * Pass One: Splay down the tree until we find addr or a NULL
9090164e057SAlan Cox 	 * pointer where addr would go.  llist and rlist are the two
9100164e057SAlan Cox 	 * sides in reverse order (bottom-up), with llist linked by
9110164e057SAlan Cox 	 * the right pointer and rlist linked by the left pointer in
9120164e057SAlan Cox 	 * the vm_map_entry.  Wait until Pass Two to set max_free on
9130164e057SAlan Cox 	 * the two spines.
9140164e057SAlan Cox 	 */
9150164e057SAlan Cox 	llist = NULL;
9160164e057SAlan Cox 	rlist = NULL;
9170164e057SAlan Cox 	for (;;) {
9180164e057SAlan Cox 		/* root is never NULL in here. */
9190164e057SAlan Cox 		if (addr < root->start) {
9200164e057SAlan Cox 			y = root->left;
9210164e057SAlan Cox 			if (y == NULL)
9224e94f402SAlan Cox 				break;
9230164e057SAlan Cox 			if (addr < y->start && y->left != NULL) {
9240164e057SAlan Cox 				/* Rotate right and put y on rlist. */
9254e94f402SAlan Cox 				root->left = y->right;
9264e94f402SAlan Cox 				y->right = root;
9270164e057SAlan Cox 				vm_map_entry_set_max_free(root);
9280164e057SAlan Cox 				root = y->left;
9290164e057SAlan Cox 				y->left = rlist;
9300164e057SAlan Cox 				rlist = y;
9310164e057SAlan Cox 			} else {
9320164e057SAlan Cox 				/* Put root on rlist. */
9330164e057SAlan Cox 				root->left = rlist;
9340164e057SAlan Cox 				rlist = root;
9354e94f402SAlan Cox 				root = y;
9364e94f402SAlan Cox 			}
9377438d60bSAlan Cox 		} else if (addr >= root->end) {
9380164e057SAlan Cox 			y = root->right;
9397438d60bSAlan Cox 			if (y == NULL)
9404e94f402SAlan Cox 				break;
9410164e057SAlan Cox 			if (addr >= y->end && y->right != NULL) {
9420164e057SAlan Cox 				/* Rotate left and put y on llist. */
9434e94f402SAlan Cox 				root->right = y->left;
9444e94f402SAlan Cox 				y->left = root;
9450164e057SAlan Cox 				vm_map_entry_set_max_free(root);
9460164e057SAlan Cox 				root = y->right;
9470164e057SAlan Cox 				y->right = llist;
9480164e057SAlan Cox 				llist = y;
9490164e057SAlan Cox 			} else {
9500164e057SAlan Cox 				/* Put root on llist. */
9510164e057SAlan Cox 				root->right = llist;
9520164e057SAlan Cox 				llist = root;
9534e94f402SAlan Cox 				root = y;
9544e94f402SAlan Cox 			}
9557438d60bSAlan Cox 		} else
9567438d60bSAlan Cox 			break;
9570164e057SAlan Cox 	}
9580164e057SAlan Cox 
9590164e057SAlan Cox 	/*
9600164e057SAlan Cox 	 * Pass Two: Walk back up the two spines, flip the pointers
9610164e057SAlan Cox 	 * and set max_free.  The subtrees of the root go at the
9620164e057SAlan Cox 	 * bottom of llist and rlist.
9630164e057SAlan Cox 	 */
9640164e057SAlan Cox 	ltree = root->left;
9650164e057SAlan Cox 	while (llist != NULL) {
9660164e057SAlan Cox 		y = llist->right;
9670164e057SAlan Cox 		llist->right = ltree;
9680164e057SAlan Cox 		vm_map_entry_set_max_free(llist);
9690164e057SAlan Cox 		ltree = llist;
9700164e057SAlan Cox 		llist = y;
9710164e057SAlan Cox 	}
9720164e057SAlan Cox 	rtree = root->right;
9730164e057SAlan Cox 	while (rlist != NULL) {
9740164e057SAlan Cox 		y = rlist->left;
9750164e057SAlan Cox 		rlist->left = rtree;
9760164e057SAlan Cox 		vm_map_entry_set_max_free(rlist);
9770164e057SAlan Cox 		rtree = rlist;
9780164e057SAlan Cox 		rlist = y;
9790164e057SAlan Cox 	}
9800164e057SAlan Cox 
9810164e057SAlan Cox 	/*
9820164e057SAlan Cox 	 * Final assembly: add ltree and rtree as subtrees of root.
9830164e057SAlan Cox 	 */
9840164e057SAlan Cox 	root->left = ltree;
9850164e057SAlan Cox 	root->right = rtree;
9860164e057SAlan Cox 	vm_map_entry_set_max_free(root);
9870164e057SAlan Cox 
9884e94f402SAlan Cox 	return (root);
9894e94f402SAlan Cox }
9904e94f402SAlan Cox 
9914e94f402SAlan Cox /*
992df8bae1dSRodney W. Grimes  *	vm_map_entry_{un,}link:
993df8bae1dSRodney W. Grimes  *
994df8bae1dSRodney W. Grimes  *	Insert/remove entries from maps.
995df8bae1dSRodney W. Grimes  */
9964e94f402SAlan Cox static void
99799c81ca9SAlan Cox vm_map_entry_link(vm_map_t map,
99899c81ca9SAlan Cox 		  vm_map_entry_t after_where,
99999c81ca9SAlan Cox 		  vm_map_entry_t entry)
100099c81ca9SAlan Cox {
100121c641b2SJohn Baldwin 
100221c641b2SJohn Baldwin 	CTR4(KTR_VM,
100321c641b2SJohn Baldwin 	    "vm_map_entry_link: map %p, nentries %d, entry %p, after %p", map,
100421c641b2SJohn Baldwin 	    map->nentries, entry, after_where);
10053a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
10065831f5fcSKonstantin Belousov 	KASSERT(after_where == &map->header ||
10075831f5fcSKonstantin Belousov 	    after_where->end <= entry->start,
10085831f5fcSKonstantin Belousov 	    ("vm_map_entry_link: prev end %jx new start %jx overlap",
10095831f5fcSKonstantin Belousov 	    (uintmax_t)after_where->end, (uintmax_t)entry->start));
10105831f5fcSKonstantin Belousov 	KASSERT(after_where->next == &map->header ||
10115831f5fcSKonstantin Belousov 	    entry->end <= after_where->next->start,
10125831f5fcSKonstantin Belousov 	    ("vm_map_entry_link: new end %jx next start %jx overlap",
10135831f5fcSKonstantin Belousov 	    (uintmax_t)entry->end, (uintmax_t)after_where->next->start));
10145831f5fcSKonstantin Belousov 
101599c81ca9SAlan Cox 	map->nentries++;
101699c81ca9SAlan Cox 	entry->prev = after_where;
101799c81ca9SAlan Cox 	entry->next = after_where->next;
101899c81ca9SAlan Cox 	entry->next->prev = entry;
101999c81ca9SAlan Cox 	after_where->next = entry;
10204e94f402SAlan Cox 
10214e94f402SAlan Cox 	if (after_where != &map->header) {
10224e94f402SAlan Cox 		if (after_where != map->root)
10234e94f402SAlan Cox 			vm_map_entry_splay(after_where->start, map->root);
10244e94f402SAlan Cox 		entry->right = after_where->right;
10254e94f402SAlan Cox 		entry->left = after_where;
10264e94f402SAlan Cox 		after_where->right = NULL;
10270164e057SAlan Cox 		after_where->adj_free = entry->start - after_where->end;
10280164e057SAlan Cox 		vm_map_entry_set_max_free(after_where);
10294e94f402SAlan Cox 	} else {
10304e94f402SAlan Cox 		entry->right = map->root;
10314e94f402SAlan Cox 		entry->left = NULL;
10324e94f402SAlan Cox 	}
10330164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
10340164e057SAlan Cox 	    entry->next->start) - entry->end;
10350164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
10364e94f402SAlan Cox 	map->root = entry;
1037df8bae1dSRodney W. Grimes }
103899c81ca9SAlan Cox 
10394e94f402SAlan Cox static void
104099c81ca9SAlan Cox vm_map_entry_unlink(vm_map_t map,
104199c81ca9SAlan Cox 		    vm_map_entry_t entry)
104299c81ca9SAlan Cox {
10434e94f402SAlan Cox 	vm_map_entry_t next, prev, root;
104499c81ca9SAlan Cox 
10453a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
10464e94f402SAlan Cox 	if (entry != map->root)
10474e94f402SAlan Cox 		vm_map_entry_splay(entry->start, map->root);
10484e94f402SAlan Cox 	if (entry->left == NULL)
10494e94f402SAlan Cox 		root = entry->right;
10504e94f402SAlan Cox 	else {
10514e94f402SAlan Cox 		root = vm_map_entry_splay(entry->start, entry->left);
10524e94f402SAlan Cox 		root->right = entry->right;
10530164e057SAlan Cox 		root->adj_free = (entry->next == &map->header ? map->max_offset :
10540164e057SAlan Cox 		    entry->next->start) - root->end;
10550164e057SAlan Cox 		vm_map_entry_set_max_free(root);
10564e94f402SAlan Cox 	}
10574e94f402SAlan Cox 	map->root = root;
10584e94f402SAlan Cox 
10594e94f402SAlan Cox 	prev = entry->prev;
10604e94f402SAlan Cox 	next = entry->next;
106199c81ca9SAlan Cox 	next->prev = prev;
106299c81ca9SAlan Cox 	prev->next = next;
106399c81ca9SAlan Cox 	map->nentries--;
106421c641b2SJohn Baldwin 	CTR3(KTR_VM, "vm_map_entry_unlink: map %p, nentries %d, entry %p", map,
106521c641b2SJohn Baldwin 	    map->nentries, entry);
1066df8bae1dSRodney W. Grimes }
1067df8bae1dSRodney W. Grimes 
1068df8bae1dSRodney W. Grimes /*
10690164e057SAlan Cox  *	vm_map_entry_resize_free:
10700164e057SAlan Cox  *
10710164e057SAlan Cox  *	Recompute the amount of free space following a vm_map_entry
10720164e057SAlan Cox  *	and propagate that value up the tree.  Call this function after
10730164e057SAlan Cox  *	resizing a map entry in-place, that is, without a call to
10740164e057SAlan Cox  *	vm_map_entry_link() or _unlink().
10750164e057SAlan Cox  *
10760164e057SAlan Cox  *	The map must be locked, and leaves it so.
10770164e057SAlan Cox  */
10780164e057SAlan Cox static void
10790164e057SAlan Cox vm_map_entry_resize_free(vm_map_t map, vm_map_entry_t entry)
10800164e057SAlan Cox {
10810164e057SAlan Cox 
10820164e057SAlan Cox 	/*
10830164e057SAlan Cox 	 * Using splay trees without parent pointers, propagating
10840164e057SAlan Cox 	 * max_free up the tree is done by moving the entry to the
10850164e057SAlan Cox 	 * root and making the change there.
10860164e057SAlan Cox 	 */
10870164e057SAlan Cox 	if (entry != map->root)
10880164e057SAlan Cox 		map->root = vm_map_entry_splay(entry->start, map->root);
10890164e057SAlan Cox 
10900164e057SAlan Cox 	entry->adj_free = (entry->next == &map->header ? map->max_offset :
10910164e057SAlan Cox 	    entry->next->start) - entry->end;
10920164e057SAlan Cox 	vm_map_entry_set_max_free(entry);
10930164e057SAlan Cox }
10940164e057SAlan Cox 
10950164e057SAlan Cox /*
1096df8bae1dSRodney W. Grimes  *	vm_map_lookup_entry:	[ internal use only ]
1097df8bae1dSRodney W. Grimes  *
1098df8bae1dSRodney W. Grimes  *	Finds the map entry containing (or
1099df8bae1dSRodney W. Grimes  *	immediately preceding) the specified address
1100df8bae1dSRodney W. Grimes  *	in the given map; the entry is returned
1101df8bae1dSRodney W. Grimes  *	in the "entry" parameter.  The boolean
1102df8bae1dSRodney W. Grimes  *	result indicates whether the address is
1103df8bae1dSRodney W. Grimes  *	actually contained in the map.
1104df8bae1dSRodney W. Grimes  */
11050d94caffSDavid Greenman boolean_t
11061b40f8c0SMatthew Dillon vm_map_lookup_entry(
11071b40f8c0SMatthew Dillon 	vm_map_t map,
11081b40f8c0SMatthew Dillon 	vm_offset_t address,
11091b40f8c0SMatthew Dillon 	vm_map_entry_t *entry)	/* OUT */
1110df8bae1dSRodney W. Grimes {
1111c0877f10SJohn Dyson 	vm_map_entry_t cur;
111205a8c414SAlan Cox 	boolean_t locked;
1113df8bae1dSRodney W. Grimes 
11144c3ef59eSAlan Cox 	/*
11154c3ef59eSAlan Cox 	 * If the map is empty, then the map entry immediately preceding
11164c3ef59eSAlan Cox 	 * "address" is the map's header.
11174c3ef59eSAlan Cox 	 */
11184c3ef59eSAlan Cox 	cur = map->root;
11194e94f402SAlan Cox 	if (cur == NULL)
11204e94f402SAlan Cox 		*entry = &map->header;
11214c3ef59eSAlan Cox 	else if (address >= cur->start && cur->end > address) {
11224c3ef59eSAlan Cox 		*entry = cur;
11234c3ef59eSAlan Cox 		return (TRUE);
112405a8c414SAlan Cox 	} else if ((locked = vm_map_locked(map)) ||
112505a8c414SAlan Cox 	    sx_try_upgrade(&map->lock)) {
112605a8c414SAlan Cox 		/*
112705a8c414SAlan Cox 		 * Splay requires a write lock on the map.  However, it only
112805a8c414SAlan Cox 		 * restructures the binary search tree; it does not otherwise
112905a8c414SAlan Cox 		 * change the map.  Thus, the map's timestamp need not change
113005a8c414SAlan Cox 		 * on a temporary upgrade.
113105a8c414SAlan Cox 		 */
11324c3ef59eSAlan Cox 		map->root = cur = vm_map_entry_splay(address, cur);
113305a8c414SAlan Cox 		if (!locked)
113405a8c414SAlan Cox 			sx_downgrade(&map->lock);
1135df8bae1dSRodney W. Grimes 
11364c3ef59eSAlan Cox 		/*
11374c3ef59eSAlan Cox 		 * If "address" is contained within a map entry, the new root
11384c3ef59eSAlan Cox 		 * is that map entry.  Otherwise, the new root is a map entry
11394c3ef59eSAlan Cox 		 * immediately before or after "address".
11404c3ef59eSAlan Cox 		 */
1141df8bae1dSRodney W. Grimes 		if (address >= cur->start) {
1142df8bae1dSRodney W. Grimes 			*entry = cur;
11434e94f402SAlan Cox 			if (cur->end > address)
1144df8bae1dSRodney W. Grimes 				return (TRUE);
11454e94f402SAlan Cox 		} else
1146df8bae1dSRodney W. Grimes 			*entry = cur->prev;
114705a8c414SAlan Cox 	} else
114805a8c414SAlan Cox 		/*
114905a8c414SAlan Cox 		 * Since the map is only locked for read access, perform a
115005a8c414SAlan Cox 		 * standard binary search tree lookup for "address".
115105a8c414SAlan Cox 		 */
115205a8c414SAlan Cox 		for (;;) {
115305a8c414SAlan Cox 			if (address < cur->start) {
115405a8c414SAlan Cox 				if (cur->left == NULL) {
115505a8c414SAlan Cox 					*entry = cur->prev;
115605a8c414SAlan Cox 					break;
115705a8c414SAlan Cox 				}
115805a8c414SAlan Cox 				cur = cur->left;
115905a8c414SAlan Cox 			} else if (cur->end > address) {
116005a8c414SAlan Cox 				*entry = cur;
116105a8c414SAlan Cox 				return (TRUE);
116205a8c414SAlan Cox 			} else {
116305a8c414SAlan Cox 				if (cur->right == NULL) {
116405a8c414SAlan Cox 					*entry = cur;
116505a8c414SAlan Cox 					break;
116605a8c414SAlan Cox 				}
116705a8c414SAlan Cox 				cur = cur->right;
116805a8c414SAlan Cox 			}
11694e94f402SAlan Cox 		}
1170df8bae1dSRodney W. Grimes 	return (FALSE);
1171df8bae1dSRodney W. Grimes }
1172df8bae1dSRodney W. Grimes 
1173df8bae1dSRodney W. Grimes /*
117430dcfc09SJohn Dyson  *	vm_map_insert:
117530dcfc09SJohn Dyson  *
117630dcfc09SJohn Dyson  *	Inserts the given whole VM object into the target
117730dcfc09SJohn Dyson  *	map at the specified address range.  The object's
117830dcfc09SJohn Dyson  *	size should match that of the address range.
117930dcfc09SJohn Dyson  *
118030dcfc09SJohn Dyson  *	Requires that the map be locked, and leaves it so.
11812aaeadf8SMatthew Dillon  *
11822aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
11832aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
118430dcfc09SJohn Dyson  */
118530dcfc09SJohn Dyson int
1186b9dcd593SBruce Evans vm_map_insert(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
118733314db0SAlan Cox     vm_offset_t start, vm_offset_t end, vm_prot_t prot, vm_prot_t max, int cow)
118830dcfc09SJohn Dyson {
118933314db0SAlan Cox 	vm_map_entry_t new_entry, prev_entry, temp_entry;
1190ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
11911569205fSKonstantin Belousov 	vm_eflags_t protoeflags;
11928211bd45SKonstantin Belousov 	vm_inherit_t inheritance;
119330dcfc09SJohn Dyson 
11943a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
11952e47807cSJeff Roberson 	KASSERT(object != kernel_object ||
119633314db0SAlan Cox 	    (cow & MAP_COPY_ON_WRITE) == 0,
11972e47807cSJeff Roberson 	    ("vm_map_insert: kernel object and COW"));
119833314db0SAlan Cox 	KASSERT(object == NULL || (cow & MAP_NOFAULT) == 0,
119933314db0SAlan Cox 	    ("vm_map_insert: paradoxical MAP_NOFAULT request"));
120000de6773SKonstantin Belousov 	KASSERT((prot & ~max) == 0,
120100de6773SKonstantin Belousov 	    ("prot %#x is not subset of max_prot %#x", prot, max));
12023a0916b8SKonstantin Belousov 
120330dcfc09SJohn Dyson 	/*
120430dcfc09SJohn Dyson 	 * Check that the start and end points are not bogus.
120530dcfc09SJohn Dyson 	 */
12061569205fSKonstantin Belousov 	if (start < map->min_offset || end > map->max_offset || start >= end)
120730dcfc09SJohn Dyson 		return (KERN_INVALID_ADDRESS);
120830dcfc09SJohn Dyson 
120930dcfc09SJohn Dyson 	/*
121030dcfc09SJohn Dyson 	 * Find the entry prior to the proposed starting address; if it's part
121130dcfc09SJohn Dyson 	 * of an existing entry, this range is bogus.
121230dcfc09SJohn Dyson 	 */
121330dcfc09SJohn Dyson 	if (vm_map_lookup_entry(map, start, &temp_entry))
121430dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
121530dcfc09SJohn Dyson 
121630dcfc09SJohn Dyson 	prev_entry = temp_entry;
121730dcfc09SJohn Dyson 
121830dcfc09SJohn Dyson 	/*
121930dcfc09SJohn Dyson 	 * Assert that the next entry doesn't overlap the end point.
122030dcfc09SJohn Dyson 	 */
12211569205fSKonstantin Belousov 	if (prev_entry->next != &map->header && prev_entry->next->start < end)
122230dcfc09SJohn Dyson 		return (KERN_NO_SPACE);
122330dcfc09SJohn Dyson 
122419bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_GUARD) != 0 && (object != NULL ||
122519bd0d9cSKonstantin Belousov 	    max != VM_PROT_NONE))
122619bd0d9cSKonstantin Belousov 		return (KERN_INVALID_ARGUMENT);
122719bd0d9cSKonstantin Belousov 
1228afa07f7eSJohn Dyson 	protoeflags = 0;
1229afa07f7eSJohn Dyson 	if (cow & MAP_COPY_ON_WRITE)
1230e5f13bddSAlan Cox 		protoeflags |= MAP_ENTRY_COW | MAP_ENTRY_NEEDS_COPY;
123133314db0SAlan Cox 	if (cow & MAP_NOFAULT)
1232afa07f7eSJohn Dyson 		protoeflags |= MAP_ENTRY_NOFAULT;
12334f79d873SMatthew Dillon 	if (cow & MAP_DISABLE_SYNCER)
12344f79d873SMatthew Dillon 		protoeflags |= MAP_ENTRY_NOSYNC;
12359730a5daSPaul Saab 	if (cow & MAP_DISABLE_COREDUMP)
12369730a5daSPaul Saab 		protoeflags |= MAP_ENTRY_NOCOREDUMP;
1237712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_DOWN)
1238712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_DOWN;
1239712efe66SAlan Cox 	if (cow & MAP_STACK_GROWS_UP)
1240712efe66SAlan Cox 		protoeflags |= MAP_ENTRY_GROWS_UP;
124184110e7eSKonstantin Belousov 	if (cow & MAP_VN_WRITECOUNT)
124284110e7eSKonstantin Belousov 		protoeflags |= MAP_ENTRY_VN_WRITECNT;
124319bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_GUARD) != 0)
124419bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_GUARD;
124519bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_STACK_GAP_DN) != 0)
124619bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_STACK_GAP_DN;
124719bd0d9cSKonstantin Belousov 	if ((cow & MAP_CREATE_STACK_GAP_UP) != 0)
124819bd0d9cSKonstantin Belousov 		protoeflags |= MAP_ENTRY_STACK_GAP_UP;
12498211bd45SKonstantin Belousov 	if (cow & MAP_INHERIT_SHARE)
12508211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_SHARE;
12518211bd45SKonstantin Belousov 	else
12528211bd45SKonstantin Belousov 		inheritance = VM_INHERIT_DEFAULT;
12534f79d873SMatthew Dillon 
1254ef694c1aSEdward Tomasz Napierala 	cred = NULL;
125519bd0d9cSKonstantin Belousov 	if ((cow & (MAP_ACC_NO_CHARGE | MAP_NOFAULT | MAP_CREATE_GUARD)) != 0)
12563364c323SKonstantin Belousov 		goto charged;
12573364c323SKonstantin Belousov 	if ((cow & MAP_ACC_CHARGED) || ((prot & VM_PROT_WRITE) &&
12583364c323SKonstantin Belousov 	    ((protoeflags & MAP_ENTRY_NEEDS_COPY) || object == NULL))) {
12593364c323SKonstantin Belousov 		if (!(cow & MAP_ACC_CHARGED) && !swap_reserve(end - start))
12603364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
12611569205fSKonstantin Belousov 		KASSERT(object == NULL ||
12621569205fSKonstantin Belousov 		    (protoeflags & MAP_ENTRY_NEEDS_COPY) != 0 ||
1263ef694c1aSEdward Tomasz Napierala 		    object->cred == NULL,
12641569205fSKonstantin Belousov 		    ("overcommit: vm_map_insert o %p", object));
1265ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
12663364c323SKonstantin Belousov 	}
12673364c323SKonstantin Belousov 
12683364c323SKonstantin Belousov charged:
1269f8616ebfSAlan Cox 	/* Expand the kernel pmap, if necessary. */
1270f8616ebfSAlan Cox 	if (map == kernel_map && end > kernel_vm_end)
1271f8616ebfSAlan Cox 		pmap_growkernel(end);
12721d284e00SAlan Cox 	if (object != NULL) {
127330dcfc09SJohn Dyson 		/*
12741d284e00SAlan Cox 		 * OBJ_ONEMAPPING must be cleared unless this mapping
12751d284e00SAlan Cox 		 * is trivially proven to be the only mapping for any
12761d284e00SAlan Cox 		 * of the object's pages.  (Object granularity
12771d284e00SAlan Cox 		 * reference counting is insufficient to recognize
12781d284e00SAlan Cox 		 * aliases with precision.)
127930dcfc09SJohn Dyson 		 */
128089f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
12811d284e00SAlan Cox 		if (object->ref_count > 1 || object->shadow_count != 0)
12822aaeadf8SMatthew Dillon 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
128389f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
12841569205fSKonstantin Belousov 	} else if (prev_entry != &map->header &&
12851569205fSKonstantin Belousov 	    prev_entry->eflags == protoeflags &&
1286b5f8c226SKonstantin Belousov 	    (cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 &&
12871569205fSKonstantin Belousov 	    prev_entry->end == start && prev_entry->wired_count == 0 &&
1288ef694c1aSEdward Tomasz Napierala 	    (prev_entry->cred == cred ||
12893364c323SKonstantin Belousov 	    (prev_entry->object.vm_object != NULL &&
12901569205fSKonstantin Belousov 	    prev_entry->object.vm_object->cred == cred)) &&
12918cc7e047SJohn Dyson 	    vm_object_coalesce(prev_entry->object.vm_object,
129257a21abaSAlan Cox 	    prev_entry->offset,
12938cc7e047SJohn Dyson 	    (vm_size_t)(prev_entry->end - prev_entry->start),
129460169c88SAlan Cox 	    (vm_size_t)(end - prev_entry->end), cred != NULL &&
129560169c88SAlan Cox 	    (protoeflags & MAP_ENTRY_NEEDS_COPY) == 0)) {
129630dcfc09SJohn Dyson 		/*
12972aaeadf8SMatthew Dillon 		 * We were able to extend the object.  Determine if we
12982aaeadf8SMatthew Dillon 		 * can extend the previous map entry to include the
12992aaeadf8SMatthew Dillon 		 * new range as well.
130030dcfc09SJohn Dyson 		 */
13011569205fSKonstantin Belousov 		if (prev_entry->inheritance == inheritance &&
13021569205fSKonstantin Belousov 		    prev_entry->protection == prot &&
13031569205fSKonstantin Belousov 		    prev_entry->max_protection == max) {
130419bd0d9cSKonstantin Belousov 			if ((prev_entry->eflags & MAP_ENTRY_GUARD) == 0)
13051569205fSKonstantin Belousov 				map->size += end - prev_entry->end;
130630dcfc09SJohn Dyson 			prev_entry->end = end;
13070164e057SAlan Cox 			vm_map_entry_resize_free(map, prev_entry);
13084e71e795SMatthew Dillon 			vm_map_simplify_entry(map, prev_entry);
130930dcfc09SJohn Dyson 			return (KERN_SUCCESS);
131030dcfc09SJohn Dyson 		}
13118cc7e047SJohn Dyson 
13122aaeadf8SMatthew Dillon 		/*
13132aaeadf8SMatthew Dillon 		 * If we can extend the object but cannot extend the
13142aaeadf8SMatthew Dillon 		 * map entry, we have to create a new map entry.  We
13152aaeadf8SMatthew Dillon 		 * must bump the ref count on the extended object to
13164e71e795SMatthew Dillon 		 * account for it.  object may be NULL.
13172aaeadf8SMatthew Dillon 		 */
13182aaeadf8SMatthew Dillon 		object = prev_entry->object.vm_object;
13192aaeadf8SMatthew Dillon 		offset = prev_entry->offset +
13202aaeadf8SMatthew Dillon 		    (prev_entry->end - prev_entry->start);
13218cc7e047SJohn Dyson 		vm_object_reference(object);
1322ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && object != NULL && object->cred != NULL &&
13233364c323SKonstantin Belousov 		    !(prev_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
13243364c323SKonstantin Belousov 			/* Object already accounts for this uid. */
1325ef694c1aSEdward Tomasz Napierala 			cred = NULL;
13263364c323SKonstantin Belousov 		}
1327b18bfc3dSJohn Dyson 	}
132860169c88SAlan Cox 	if (cred != NULL)
132960169c88SAlan Cox 		crhold(cred);
13302aaeadf8SMatthew Dillon 
13312aaeadf8SMatthew Dillon 	/*
133230dcfc09SJohn Dyson 	 * Create a new entry
133330dcfc09SJohn Dyson 	 */
133430dcfc09SJohn Dyson 	new_entry = vm_map_entry_create(map);
133530dcfc09SJohn Dyson 	new_entry->start = start;
133630dcfc09SJohn Dyson 	new_entry->end = end;
1337ef694c1aSEdward Tomasz Napierala 	new_entry->cred = NULL;
133830dcfc09SJohn Dyson 
1339afa07f7eSJohn Dyson 	new_entry->eflags = protoeflags;
134030dcfc09SJohn Dyson 	new_entry->object.vm_object = object;
134130dcfc09SJohn Dyson 	new_entry->offset = offset;
13422267af78SJulian Elischer 
13438211bd45SKonstantin Belousov 	new_entry->inheritance = inheritance;
134430dcfc09SJohn Dyson 	new_entry->protection = prot;
134530dcfc09SJohn Dyson 	new_entry->max_protection = max;
134630dcfc09SJohn Dyson 	new_entry->wired_count = 0;
1347997ac690SKonstantin Belousov 	new_entry->wiring_thread = NULL;
134813458803SAlan Cox 	new_entry->read_ahead = VM_FAULT_READ_AHEAD_INIT;
1349381b7242SAlan Cox 	new_entry->next_read = start;
1350e5f251d2SAlan Cox 
1351ef694c1aSEdward Tomasz Napierala 	KASSERT(cred == NULL || !ENTRY_CHARGED(new_entry),
13521569205fSKonstantin Belousov 	    ("overcommit: vm_map_insert leaks vm_map %p", new_entry));
1353ef694c1aSEdward Tomasz Napierala 	new_entry->cred = cred;
13543364c323SKonstantin Belousov 
135530dcfc09SJohn Dyson 	/*
135630dcfc09SJohn Dyson 	 * Insert the new entry into the list
135730dcfc09SJohn Dyson 	 */
135830dcfc09SJohn Dyson 	vm_map_entry_link(map, prev_entry, new_entry);
135919bd0d9cSKonstantin Belousov 	if ((new_entry->eflags & MAP_ENTRY_GUARD) == 0)
136030dcfc09SJohn Dyson 		map->size += new_entry->end - new_entry->start;
136130dcfc09SJohn Dyson 
13621a484d28SMatthew Dillon 	/*
1363eaaf9f7fSAlan Cox 	 * Try to coalesce the new entry with both the previous and next
1364eaaf9f7fSAlan Cox 	 * entries in the list.  Previously, we only attempted to coalesce
1365eaaf9f7fSAlan Cox 	 * with the previous entry when object is NULL.  Here, we handle the
1366eaaf9f7fSAlan Cox 	 * other cases, which are less common.
13671a484d28SMatthew Dillon 	 */
13684e71e795SMatthew Dillon 	vm_map_simplify_entry(map, new_entry);
13694e71e795SMatthew Dillon 
13701569205fSKonstantin Belousov 	if ((cow & (MAP_PREFAULT | MAP_PREFAULT_PARTIAL)) != 0) {
13711569205fSKonstantin Belousov 		vm_map_pmap_enter(map, start, prot, object, OFF_TO_IDX(offset),
13721569205fSKonstantin Belousov 		    end - start, cow & MAP_PREFAULT_PARTIAL);
13734f79d873SMatthew Dillon 	}
1374e972780aSAlan Cox 
137530dcfc09SJohn Dyson 	return (KERN_SUCCESS);
137630dcfc09SJohn Dyson }
137730dcfc09SJohn Dyson 
137830dcfc09SJohn Dyson /*
13790164e057SAlan Cox  *	vm_map_findspace:
13800164e057SAlan Cox  *
13810164e057SAlan Cox  *	Find the first fit (lowest VM address) for "length" free bytes
13820164e057SAlan Cox  *	beginning at address >= start in the given map.
13830164e057SAlan Cox  *
13840164e057SAlan Cox  *	In a vm_map_entry, "adj_free" is the amount of free space
13850164e057SAlan Cox  *	adjacent (higher address) to this entry, and "max_free" is the
13860164e057SAlan Cox  *	maximum amount of contiguous free space in its subtree.  This
13870164e057SAlan Cox  *	allows finding a free region in one path down the tree, so
13880164e057SAlan Cox  *	O(log n) amortized with splay trees.
13890164e057SAlan Cox  *
13900164e057SAlan Cox  *	The map must be locked, and leaves it so.
13910164e057SAlan Cox  *
13920164e057SAlan Cox  *	Returns: 0 on success, and starting address in *addr,
13930164e057SAlan Cox  *		 1 if insufficient space.
1394df8bae1dSRodney W. Grimes  */
1395df8bae1dSRodney W. Grimes int
13960164e057SAlan Cox vm_map_findspace(vm_map_t map, vm_offset_t start, vm_size_t length,
13970164e057SAlan Cox     vm_offset_t *addr)	/* OUT */
1398df8bae1dSRodney W. Grimes {
13990164e057SAlan Cox 	vm_map_entry_t entry;
1400f8616ebfSAlan Cox 	vm_offset_t st;
1401df8bae1dSRodney W. Grimes 
1402986b43f8SAlan Cox 	/*
1403986b43f8SAlan Cox 	 * Request must fit within min/max VM address and must avoid
1404986b43f8SAlan Cox 	 * address wrap.
1405986b43f8SAlan Cox 	 */
1406df8bae1dSRodney W. Grimes 	if (start < map->min_offset)
1407df8bae1dSRodney W. Grimes 		start = map->min_offset;
1408986b43f8SAlan Cox 	if (start + length > map->max_offset || start + length < start)
1409df8bae1dSRodney W. Grimes 		return (1);
1410df8bae1dSRodney W. Grimes 
14110164e057SAlan Cox 	/* Empty tree means wide open address space. */
14120164e057SAlan Cox 	if (map->root == NULL) {
1413df8bae1dSRodney W. Grimes 		*addr = start;
1414f8616ebfSAlan Cox 		return (0);
141599448ed1SJohn Dyson 	}
14160164e057SAlan Cox 
14170164e057SAlan Cox 	/*
14180164e057SAlan Cox 	 * After splay, if start comes before root node, then there
14190164e057SAlan Cox 	 * must be a gap from start to the root.
14200164e057SAlan Cox 	 */
14210164e057SAlan Cox 	map->root = vm_map_entry_splay(start, map->root);
14220164e057SAlan Cox 	if (start + length <= map->root->start) {
14230164e057SAlan Cox 		*addr = start;
1424f8616ebfSAlan Cox 		return (0);
14250164e057SAlan Cox 	}
14260164e057SAlan Cox 
14270164e057SAlan Cox 	/*
14280164e057SAlan Cox 	 * Root is the last node that might begin its gap before
1429986b43f8SAlan Cox 	 * start, and this is the last comparison where address
1430986b43f8SAlan Cox 	 * wrap might be a problem.
14310164e057SAlan Cox 	 */
14320164e057SAlan Cox 	st = (start > map->root->end) ? start : map->root->end;
1433986b43f8SAlan Cox 	if (length <= map->root->end + map->root->adj_free - st) {
14340164e057SAlan Cox 		*addr = st;
1435f8616ebfSAlan Cox 		return (0);
14360164e057SAlan Cox 	}
14370164e057SAlan Cox 
14380164e057SAlan Cox 	/* With max_free, can immediately tell if no solution. */
14390164e057SAlan Cox 	entry = map->root->right;
14400164e057SAlan Cox 	if (entry == NULL || length > entry->max_free)
14410164e057SAlan Cox 		return (1);
14420164e057SAlan Cox 
14430164e057SAlan Cox 	/*
14440164e057SAlan Cox 	 * Search the right subtree in the order: left subtree, root,
14450164e057SAlan Cox 	 * right subtree (first fit).  The previous splay implies that
14460164e057SAlan Cox 	 * all regions in the right subtree have addresses > start.
14470164e057SAlan Cox 	 */
14480164e057SAlan Cox 	while (entry != NULL) {
14490164e057SAlan Cox 		if (entry->left != NULL && entry->left->max_free >= length)
14500164e057SAlan Cox 			entry = entry->left;
14510164e057SAlan Cox 		else if (entry->adj_free >= length) {
14520164e057SAlan Cox 			*addr = entry->end;
1453f8616ebfSAlan Cox 			return (0);
14540164e057SAlan Cox 		} else
14550164e057SAlan Cox 			entry = entry->right;
14560164e057SAlan Cox 	}
14570164e057SAlan Cox 
14580164e057SAlan Cox 	/* Can't get here, so panic if we do. */
14590164e057SAlan Cox 	panic("vm_map_findspace: max_free corrupt");
1460df8bae1dSRodney W. Grimes }
1461df8bae1dSRodney W. Grimes 
1462d239bd3cSKonstantin Belousov int
1463d239bd3cSKonstantin Belousov vm_map_fixed(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1464b8ca4ef2SAlan Cox     vm_offset_t start, vm_size_t length, vm_prot_t prot,
1465d239bd3cSKonstantin Belousov     vm_prot_t max, int cow)
1466d239bd3cSKonstantin Belousov {
1467b8ca4ef2SAlan Cox 	vm_offset_t end;
1468d239bd3cSKonstantin Belousov 	int result;
1469d239bd3cSKonstantin Belousov 
1470d239bd3cSKonstantin Belousov 	end = start + length;
14714648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
14724648ba0aSKonstantin Belousov 	    object == NULL,
14734648ba0aSKonstantin Belousov 	    ("vm_map_fixed: non-NULL backing object for stack"));
1474897d81a0SKonstantin Belousov 	vm_map_lock(map);
1475d239bd3cSKonstantin Belousov 	VM_MAP_RANGE_CHECK(map, start, end);
147611c42bccSKonstantin Belousov 	if ((cow & MAP_CHECK_EXCL) == 0)
147711c42bccSKonstantin Belousov 		vm_map_delete(map, start, end);
14784648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
14794648ba0aSKonstantin Belousov 		result = vm_map_stack_locked(map, start, length, sgrowsiz,
14804648ba0aSKonstantin Belousov 		    prot, max, cow);
14814648ba0aSKonstantin Belousov 	} else {
14824648ba0aSKonstantin Belousov 		result = vm_map_insert(map, object, offset, start, end,
14834648ba0aSKonstantin Belousov 		    prot, max, cow);
14844648ba0aSKonstantin Belousov 	}
1485d239bd3cSKonstantin Belousov 	vm_map_unlock(map);
1486d239bd3cSKonstantin Belousov 	return (result);
1487d239bd3cSKonstantin Belousov }
1488d239bd3cSKonstantin Belousov 
1489df8bae1dSRodney W. Grimes /*
1490*fec29688SAlan Cox  * Searches for the specified amount of free space in the given map with the
1491*fec29688SAlan Cox  * specified alignment.  Performs an address-ordered, first-fit search from
1492*fec29688SAlan Cox  * the given address "*addr", with an optional upper bound "max_addr".  If the
1493*fec29688SAlan Cox  * parameter "alignment" is zero, then the alignment is computed from the
1494*fec29688SAlan Cox  * given (object, offset) pair so as to enable the greatest possible use of
1495*fec29688SAlan Cox  * superpage mappings.  Returns KERN_SUCCESS and the address of the free space
1496*fec29688SAlan Cox  * in "*addr" if successful.  Otherwise, returns KERN_NO_SPACE.
1497*fec29688SAlan Cox  *
1498*fec29688SAlan Cox  * The map must be locked.  Initially, there must be at least "length" bytes
1499*fec29688SAlan Cox  * of free space at the given address.
1500*fec29688SAlan Cox  */
1501*fec29688SAlan Cox static int
1502*fec29688SAlan Cox vm_map_alignspace(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1503*fec29688SAlan Cox     vm_offset_t *addr, vm_size_t length, vm_offset_t max_addr,
1504*fec29688SAlan Cox     vm_offset_t alignment)
1505*fec29688SAlan Cox {
1506*fec29688SAlan Cox 	vm_offset_t aligned_addr, free_addr;
1507*fec29688SAlan Cox 
1508*fec29688SAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
1509*fec29688SAlan Cox 	free_addr = *addr;
1510*fec29688SAlan Cox 	KASSERT(!vm_map_findspace(map, free_addr, length, addr) &&
1511*fec29688SAlan Cox 	    free_addr == *addr, ("caller provided insufficient free space"));
1512*fec29688SAlan Cox 	for (;;) {
1513*fec29688SAlan Cox 		/*
1514*fec29688SAlan Cox 		 * At the start of every iteration, the free space at address
1515*fec29688SAlan Cox 		 * "*addr" is at least "length" bytes.
1516*fec29688SAlan Cox 		 */
1517*fec29688SAlan Cox 		if (alignment == 0)
1518*fec29688SAlan Cox 			pmap_align_superpage(object, offset, addr, length);
1519*fec29688SAlan Cox 		else if ((*addr & (alignment - 1)) != 0) {
1520*fec29688SAlan Cox 			*addr &= ~(alignment - 1);
1521*fec29688SAlan Cox 			*addr += alignment;
1522*fec29688SAlan Cox 		}
1523*fec29688SAlan Cox 		aligned_addr = *addr;
1524*fec29688SAlan Cox 		if (aligned_addr == free_addr) {
1525*fec29688SAlan Cox 			/*
1526*fec29688SAlan Cox 			 * Alignment did not change "*addr", so "*addr" must
1527*fec29688SAlan Cox 			 * still provide sufficient free space.
1528*fec29688SAlan Cox 			 */
1529*fec29688SAlan Cox 			return (KERN_SUCCESS);
1530*fec29688SAlan Cox 		}
1531*fec29688SAlan Cox 
1532*fec29688SAlan Cox 		/*
1533*fec29688SAlan Cox 		 * Test for address wrap on "*addr".  A wrapped "*addr" could
1534*fec29688SAlan Cox 		 * be a valid address, in which case vm_map_findspace() cannot
1535*fec29688SAlan Cox 		 * be relied upon to fail.
1536*fec29688SAlan Cox 		 */
1537*fec29688SAlan Cox 		if (aligned_addr < free_addr ||
1538*fec29688SAlan Cox 		    vm_map_findspace(map, aligned_addr, length, addr) ||
1539*fec29688SAlan Cox 		    (max_addr != 0 && *addr + length > max_addr))
1540*fec29688SAlan Cox 			return (KERN_NO_SPACE);
1541*fec29688SAlan Cox 		free_addr = *addr;
1542*fec29688SAlan Cox 		if (free_addr == aligned_addr) {
1543*fec29688SAlan Cox 			/*
1544*fec29688SAlan Cox 			 * If a successful call to vm_map_findspace() did not
1545*fec29688SAlan Cox 			 * change "*addr", then "*addr" must still be aligned
1546*fec29688SAlan Cox 			 * and provide sufficient free space.
1547*fec29688SAlan Cox 			 */
1548*fec29688SAlan Cox 			return (KERN_SUCCESS);
1549*fec29688SAlan Cox 		}
1550*fec29688SAlan Cox 	}
1551*fec29688SAlan Cox }
1552*fec29688SAlan Cox 
1553*fec29688SAlan Cox /*
1554df8bae1dSRodney W. Grimes  *	vm_map_find finds an unallocated region in the target address
1555df8bae1dSRodney W. Grimes  *	map with the given length.  The search is defined to be
1556df8bae1dSRodney W. Grimes  *	first-fit from the specified address; the region found is
1557df8bae1dSRodney W. Grimes  *	returned in the same parameter.
1558df8bae1dSRodney W. Grimes  *
15592aaeadf8SMatthew Dillon  *	If object is non-NULL, ref count must be bumped by caller
15602aaeadf8SMatthew Dillon  *	prior to making call to account for the new entry.
1561df8bae1dSRodney W. Grimes  */
1562df8bae1dSRodney W. Grimes int
1563b9dcd593SBruce Evans vm_map_find(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
1564b9dcd593SBruce Evans 	    vm_offset_t *addr,	/* IN/OUT */
1565edb572a3SJohn Baldwin 	    vm_size_t length, vm_offset_t max_addr, int find_space,
1566edb572a3SJohn Baldwin 	    vm_prot_t prot, vm_prot_t max, int cow)
1567df8bae1dSRodney W. Grimes {
1568*fec29688SAlan Cox 	vm_offset_t alignment, min_addr;
1569*fec29688SAlan Cox 	int rv;
1570df8bae1dSRodney W. Grimes 
15714648ba0aSKonstantin Belousov 	KASSERT((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) == 0 ||
15724648ba0aSKonstantin Belousov 	    object == NULL,
15734648ba0aSKonstantin Belousov 	    ("vm_map_find: non-NULL backing object for stack"));
1574ff74a3faSJohn Baldwin 	if (find_space == VMFS_OPTIMAL_SPACE && (object == NULL ||
1575ff74a3faSJohn Baldwin 	    (object->flags & OBJ_COLORED) == 0))
1576ff74a3faSJohn Baldwin 		find_space = VMFS_ANY_SPACE;
15775aa60b6fSJohn Baldwin 	if (find_space >> 8 != 0) {
15785aa60b6fSJohn Baldwin 		KASSERT((find_space & 0xff) == 0, ("bad VMFS flags"));
15795aa60b6fSJohn Baldwin 		alignment = (vm_offset_t)1 << (find_space >> 8);
15805aa60b6fSJohn Baldwin 	} else
15815aa60b6fSJohn Baldwin 		alignment = 0;
15824d572bb3SAlan Cox 	vm_map_lock(map);
158326c538ffSAlan Cox 	if (find_space != VMFS_NO_SPACE) {
1584*fec29688SAlan Cox 		KASSERT(find_space == VMFS_ANY_SPACE ||
1585*fec29688SAlan Cox 		    find_space == VMFS_OPTIMAL_SPACE ||
1586*fec29688SAlan Cox 		    find_space == VMFS_SUPER_SPACE ||
1587*fec29688SAlan Cox 		    alignment != 0, ("unexpected VMFS flag"));
1588*fec29688SAlan Cox 		min_addr = *addr;
1589*fec29688SAlan Cox again:
1590*fec29688SAlan Cox 		if (vm_map_findspace(map, min_addr, length, addr) ||
1591edb572a3SJohn Baldwin 		    (max_addr != 0 && *addr + length > max_addr)) {
1592*fec29688SAlan Cox 			rv = KERN_NO_SPACE;
1593*fec29688SAlan Cox 			goto done;
1594*fec29688SAlan Cox 		}
1595*fec29688SAlan Cox 		if (find_space != VMFS_ANY_SPACE &&
1596*fec29688SAlan Cox 		    (rv = vm_map_alignspace(map, object, offset, addr, length,
1597*fec29688SAlan Cox 		    max_addr, alignment)) != KERN_SUCCESS) {
1598ff74a3faSJohn Baldwin 			if (find_space == VMFS_OPTIMAL_SPACE) {
1599ff74a3faSJohn Baldwin 				find_space = VMFS_ANY_SPACE;
1600ff74a3faSJohn Baldwin 				goto again;
1601ff74a3faSJohn Baldwin 			}
1602*fec29688SAlan Cox 			goto done;
1603df8bae1dSRodney W. Grimes 		}
1604df8bae1dSRodney W. Grimes 	}
16054648ba0aSKonstantin Belousov 	if ((cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP)) != 0) {
1606*fec29688SAlan Cox 		rv = vm_map_stack_locked(map, *addr, length, sgrowsiz, prot,
1607*fec29688SAlan Cox 		    max, cow);
16084648ba0aSKonstantin Belousov 	} else {
1609*fec29688SAlan Cox 		rv = vm_map_insert(map, object, offset, *addr, *addr + length,
1610*fec29688SAlan Cox 		    prot, max, cow);
16114648ba0aSKonstantin Belousov 	}
1612*fec29688SAlan Cox done:
1613df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1614*fec29688SAlan Cox 	return (rv);
1615df8bae1dSRodney W. Grimes }
1616df8bae1dSRodney W. Grimes 
1617e8502826SKonstantin Belousov /*
1618e8502826SKonstantin Belousov  *	vm_map_find_min() is a variant of vm_map_find() that takes an
1619e8502826SKonstantin Belousov  *	additional parameter (min_addr) and treats the given address
1620e8502826SKonstantin Belousov  *	(*addr) differently.  Specifically, it treats *addr as a hint
1621e8502826SKonstantin Belousov  *	and not as the minimum address where the mapping is created.
1622e8502826SKonstantin Belousov  *
1623e8502826SKonstantin Belousov  *	This function works in two phases.  First, it tries to
1624e8502826SKonstantin Belousov  *	allocate above the hint.  If that fails and the hint is
1625e8502826SKonstantin Belousov  *	greater than min_addr, it performs a second pass, replacing
1626e8502826SKonstantin Belousov  *	the hint with min_addr as the minimum address for the
1627e8502826SKonstantin Belousov  *	allocation.
1628e8502826SKonstantin Belousov  */
16296a97a3f7SKonstantin Belousov int
16306a97a3f7SKonstantin Belousov vm_map_find_min(vm_map_t map, vm_object_t object, vm_ooffset_t offset,
16316a97a3f7SKonstantin Belousov     vm_offset_t *addr, vm_size_t length, vm_offset_t min_addr,
16326a97a3f7SKonstantin Belousov     vm_offset_t max_addr, int find_space, vm_prot_t prot, vm_prot_t max,
16336a97a3f7SKonstantin Belousov     int cow)
16346a97a3f7SKonstantin Belousov {
16356a97a3f7SKonstantin Belousov 	vm_offset_t hint;
16366a97a3f7SKonstantin Belousov 	int rv;
16376a97a3f7SKonstantin Belousov 
16386a97a3f7SKonstantin Belousov 	hint = *addr;
16396a97a3f7SKonstantin Belousov 	for (;;) {
16406a97a3f7SKonstantin Belousov 		rv = vm_map_find(map, object, offset, addr, length, max_addr,
16416a97a3f7SKonstantin Belousov 		    find_space, prot, max, cow);
16426a97a3f7SKonstantin Belousov 		if (rv == KERN_SUCCESS || min_addr >= hint)
16436a97a3f7SKonstantin Belousov 			return (rv);
16447683ad70SKonstantin Belousov 		*addr = hint = min_addr;
16456a97a3f7SKonstantin Belousov 	}
16466a97a3f7SKonstantin Belousov }
16476a97a3f7SKonstantin Belousov 
1648df8bae1dSRodney W. Grimes /*
1649b7b2aac2SJohn Dyson  *	vm_map_simplify_entry:
165067bf6868SJohn Dyson  *
16514e71e795SMatthew Dillon  *	Simplify the given map entry by merging with either neighbor.  This
16524e71e795SMatthew Dillon  *	routine also has the ability to merge with both neighbors.
16534e71e795SMatthew Dillon  *
16544e71e795SMatthew Dillon  *	The map must be locked.
16554e71e795SMatthew Dillon  *
1656ba7c64d1SKonstantin Belousov  *	This routine guarantees that the passed entry remains valid (though
16574e71e795SMatthew Dillon  *	possibly extended).  When merging, this routine may delete one or
16584e71e795SMatthew Dillon  *	both neighbors.
1659df8bae1dSRodney W. Grimes  */
16600afcd3afSAlan Cox void
16611b40f8c0SMatthew Dillon vm_map_simplify_entry(vm_map_t map, vm_map_entry_t entry)
1662df8bae1dSRodney W. Grimes {
1663308c24baSJohn Dyson 	vm_map_entry_t next, prev;
1664b7b2aac2SJohn Dyson 	vm_size_t prevsize, esize;
1665df8bae1dSRodney W. Grimes 
1666eaaf9f7fSAlan Cox 	if ((entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP |
1667eaaf9f7fSAlan Cox 	    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_IS_SUB_MAP)) != 0)
1668df8bae1dSRodney W. Grimes 		return;
1669308c24baSJohn Dyson 
1670308c24baSJohn Dyson 	prev = entry->prev;
1671308c24baSJohn Dyson 	if (prev != &map->header) {
167267bf6868SJohn Dyson 		prevsize = prev->end - prev->start;
167367bf6868SJohn Dyson 		if ( (prev->end == entry->start) &&
167467bf6868SJohn Dyson 		     (prev->object.vm_object == entry->object.vm_object) &&
167595e5e988SJohn Dyson 		     (!prev->object.vm_object ||
167667bf6868SJohn Dyson 			(prev->offset + prevsize == entry->offset)) &&
1677afa07f7eSJohn Dyson 		     (prev->eflags == entry->eflags) &&
167867bf6868SJohn Dyson 		     (prev->protection == entry->protection) &&
167967bf6868SJohn Dyson 		     (prev->max_protection == entry->max_protection) &&
168067bf6868SJohn Dyson 		     (prev->inheritance == entry->inheritance) &&
16813364c323SKonstantin Belousov 		     (prev->wired_count == entry->wired_count) &&
1682ef694c1aSEdward Tomasz Napierala 		     (prev->cred == entry->cred)) {
1683308c24baSJohn Dyson 			vm_map_entry_unlink(map, prev);
1684308c24baSJohn Dyson 			entry->start = prev->start;
1685308c24baSJohn Dyson 			entry->offset = prev->offset;
16860164e057SAlan Cox 			if (entry->prev != &map->header)
16870164e057SAlan Cox 				vm_map_entry_resize_free(map, entry->prev);
16887fd10fb3SKonstantin Belousov 
16897fd10fb3SKonstantin Belousov 			/*
1690b0994946SKonstantin Belousov 			 * If the backing object is a vnode object,
1691b0994946SKonstantin Belousov 			 * vm_object_deallocate() calls vrele().
1692b0994946SKonstantin Belousov 			 * However, vrele() does not lock the vnode
1693b0994946SKonstantin Belousov 			 * because the vnode has additional
1694b0994946SKonstantin Belousov 			 * references.  Thus, the map lock can be kept
1695b0994946SKonstantin Belousov 			 * without causing a lock-order reversal with
1696b0994946SKonstantin Belousov 			 * the vnode lock.
169784110e7eSKonstantin Belousov 			 *
169884110e7eSKonstantin Belousov 			 * Since we count the number of virtual page
169984110e7eSKonstantin Belousov 			 * mappings in object->un_pager.vnp.writemappings,
170084110e7eSKonstantin Belousov 			 * the writemappings value should not be adjusted
170184110e7eSKonstantin Belousov 			 * when the entry is disposed of.
17027fd10fb3SKonstantin Belousov 			 */
1703b18bfc3dSJohn Dyson 			if (prev->object.vm_object)
1704308c24baSJohn Dyson 				vm_object_deallocate(prev->object.vm_object);
1705ef694c1aSEdward Tomasz Napierala 			if (prev->cred != NULL)
1706ef694c1aSEdward Tomasz Napierala 				crfree(prev->cred);
1707308c24baSJohn Dyson 			vm_map_entry_dispose(map, prev);
1708308c24baSJohn Dyson 		}
1709308c24baSJohn Dyson 	}
1710de5f6a77SJohn Dyson 
1711de5f6a77SJohn Dyson 	next = entry->next;
1712308c24baSJohn Dyson 	if (next != &map->header) {
171367bf6868SJohn Dyson 		esize = entry->end - entry->start;
171467bf6868SJohn Dyson 		if ((entry->end == next->start) &&
171567bf6868SJohn Dyson 		    (next->object.vm_object == entry->object.vm_object) &&
171667bf6868SJohn Dyson 		     (!entry->object.vm_object ||
171767bf6868SJohn Dyson 			(entry->offset + esize == next->offset)) &&
1718afa07f7eSJohn Dyson 		    (next->eflags == entry->eflags) &&
171967bf6868SJohn Dyson 		    (next->protection == entry->protection) &&
172067bf6868SJohn Dyson 		    (next->max_protection == entry->max_protection) &&
172167bf6868SJohn Dyson 		    (next->inheritance == entry->inheritance) &&
17223364c323SKonstantin Belousov 		    (next->wired_count == entry->wired_count) &&
1723ef694c1aSEdward Tomasz Napierala 		    (next->cred == entry->cred)) {
1724de5f6a77SJohn Dyson 			vm_map_entry_unlink(map, next);
1725de5f6a77SJohn Dyson 			entry->end = next->end;
17260164e057SAlan Cox 			vm_map_entry_resize_free(map, entry);
17277fd10fb3SKonstantin Belousov 
17287fd10fb3SKonstantin Belousov 			/*
17297fd10fb3SKonstantin Belousov 			 * See comment above.
17307fd10fb3SKonstantin Belousov 			 */
1731b18bfc3dSJohn Dyson 			if (next->object.vm_object)
1732de5f6a77SJohn Dyson 				vm_object_deallocate(next->object.vm_object);
1733ef694c1aSEdward Tomasz Napierala 			if (next->cred != NULL)
1734ef694c1aSEdward Tomasz Napierala 				crfree(next->cred);
1735de5f6a77SJohn Dyson 			vm_map_entry_dispose(map, next);
1736df8bae1dSRodney W. Grimes 		}
1737df8bae1dSRodney W. Grimes 	}
1738de5f6a77SJohn Dyson }
1739df8bae1dSRodney W. Grimes /*
1740df8bae1dSRodney W. Grimes  *	vm_map_clip_start:	[ internal use only ]
1741df8bae1dSRodney W. Grimes  *
1742df8bae1dSRodney W. Grimes  *	Asserts that the given entry begins at or after
1743df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1744df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1745df8bae1dSRodney W. Grimes  */
1746df8bae1dSRodney W. Grimes #define vm_map_clip_start(map, entry, startaddr) \
1747df8bae1dSRodney W. Grimes { \
1748df8bae1dSRodney W. Grimes 	if (startaddr > entry->start) \
1749df8bae1dSRodney W. Grimes 		_vm_map_clip_start(map, entry, startaddr); \
1750df8bae1dSRodney W. Grimes }
1751df8bae1dSRodney W. Grimes 
1752df8bae1dSRodney W. Grimes /*
1753df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1754df8bae1dSRodney W. Grimes  *	the entry must be split.
1755df8bae1dSRodney W. Grimes  */
17560d94caffSDavid Greenman static void
17571b40f8c0SMatthew Dillon _vm_map_clip_start(vm_map_t map, vm_map_entry_t entry, vm_offset_t start)
1758df8bae1dSRodney W. Grimes {
1759c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1760df8bae1dSRodney W. Grimes 
17613a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
1762ed11e4d7SMark Johnston 	KASSERT(entry->end > start && entry->start < start,
1763ed11e4d7SMark Johnston 	    ("_vm_map_clip_start: invalid clip of entry %p", entry));
17643a0916b8SKonstantin Belousov 
1765df8bae1dSRodney W. Grimes 	/*
17660d94caffSDavid Greenman 	 * Split off the front portion -- note that we must insert the new
17670d94caffSDavid Greenman 	 * entry BEFORE this one, so that this entry has the specified
17680d94caffSDavid Greenman 	 * starting address.
1769df8bae1dSRodney W. Grimes 	 */
1770f32dbbeeSJohn Dyson 	vm_map_simplify_entry(map, entry);
1771f32dbbeeSJohn Dyson 
177211cccda1SJohn Dyson 	/*
177311cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
177411cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
177511cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
177611cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
177711cccda1SJohn Dyson 	 * put this improvement.
177811cccda1SJohn Dyson 	 */
177919bd0d9cSKonstantin Belousov 	if (entry->object.vm_object == NULL && !map->system_map &&
178019bd0d9cSKonstantin Belousov 	    (entry->eflags & MAP_ENTRY_GUARD) == 0) {
178111cccda1SJohn Dyson 		vm_object_t object;
178211cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1783c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
178411cccda1SJohn Dyson 		entry->object.vm_object = object;
178511cccda1SJohn Dyson 		entry->offset = 0;
1786ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1787ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
17883364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1789ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
17903364c323SKonstantin Belousov 		}
17913364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
17923364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1793ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
179489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1795ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1796ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_start: both cred e %p", entry));
1797ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
17983364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
179989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1800ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
180111cccda1SJohn Dyson 	}
180211cccda1SJohn Dyson 
1803df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1804df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1805df8bae1dSRodney W. Grimes 
1806df8bae1dSRodney W. Grimes 	new_entry->end = start;
1807df8bae1dSRodney W. Grimes 	entry->offset += (start - entry->start);
1808df8bae1dSRodney W. Grimes 	entry->start = start;
1809ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1810ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1811df8bae1dSRodney W. Grimes 
1812df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry->prev, new_entry);
1813df8bae1dSRodney W. Grimes 
18149fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1815df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
181684110e7eSKonstantin Belousov 		/*
181784110e7eSKonstantin Belousov 		 * The object->un_pager.vnp.writemappings for the
181884110e7eSKonstantin Belousov 		 * object of MAP_ENTRY_VN_WRITECNT type entry shall be
181984110e7eSKonstantin Belousov 		 * kept as is here.  The virtual pages are
182084110e7eSKonstantin Belousov 		 * re-distributed among the clipped entries, so the sum is
182184110e7eSKonstantin Belousov 		 * left the same.
182284110e7eSKonstantin Belousov 		 */
1823df8bae1dSRodney W. Grimes 	}
1824c0877f10SJohn Dyson }
1825df8bae1dSRodney W. Grimes 
1826df8bae1dSRodney W. Grimes /*
1827df8bae1dSRodney W. Grimes  *	vm_map_clip_end:	[ internal use only ]
1828df8bae1dSRodney W. Grimes  *
1829df8bae1dSRodney W. Grimes  *	Asserts that the given entry ends at or before
1830df8bae1dSRodney W. Grimes  *	the specified address; if necessary,
1831df8bae1dSRodney W. Grimes  *	it splits the entry into two.
1832df8bae1dSRodney W. Grimes  */
1833df8bae1dSRodney W. Grimes #define vm_map_clip_end(map, entry, endaddr) \
1834df8bae1dSRodney W. Grimes { \
1835af045176SPoul-Henning Kamp 	if ((endaddr) < (entry->end)) \
1836af045176SPoul-Henning Kamp 		_vm_map_clip_end((map), (entry), (endaddr)); \
1837df8bae1dSRodney W. Grimes }
1838df8bae1dSRodney W. Grimes 
1839df8bae1dSRodney W. Grimes /*
1840df8bae1dSRodney W. Grimes  *	This routine is called only when it is known that
1841df8bae1dSRodney W. Grimes  *	the entry must be split.
1842df8bae1dSRodney W. Grimes  */
18430d94caffSDavid Greenman static void
18441b40f8c0SMatthew Dillon _vm_map_clip_end(vm_map_t map, vm_map_entry_t entry, vm_offset_t end)
1845df8bae1dSRodney W. Grimes {
1846c0877f10SJohn Dyson 	vm_map_entry_t new_entry;
1847df8bae1dSRodney W. Grimes 
18483a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
1849ed11e4d7SMark Johnston 	KASSERT(entry->start < end && entry->end > end,
1850ed11e4d7SMark Johnston 	    ("_vm_map_clip_end: invalid clip of entry %p", entry));
18513a0916b8SKonstantin Belousov 
1852df8bae1dSRodney W. Grimes 	/*
185311cccda1SJohn Dyson 	 * If there is no object backing this entry, we might as well create
185411cccda1SJohn Dyson 	 * one now.  If we defer it, an object can get created after the map
185511cccda1SJohn Dyson 	 * is clipped, and individual objects will be created for the split-up
185611cccda1SJohn Dyson 	 * map.  This is a bit of a hack, but is also about the best place to
185711cccda1SJohn Dyson 	 * put this improvement.
185811cccda1SJohn Dyson 	 */
185919bd0d9cSKonstantin Belousov 	if (entry->object.vm_object == NULL && !map->system_map &&
186019bd0d9cSKonstantin Belousov 	    (entry->eflags & MAP_ENTRY_GUARD) == 0) {
186111cccda1SJohn Dyson 		vm_object_t object;
186211cccda1SJohn Dyson 		object = vm_object_allocate(OBJT_DEFAULT,
1863c2e11a03SJohn Dyson 				atop(entry->end - entry->start));
186411cccda1SJohn Dyson 		entry->object.vm_object = object;
186511cccda1SJohn Dyson 		entry->offset = 0;
1866ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
1867ef694c1aSEdward Tomasz Napierala 			object->cred = entry->cred;
18683364c323SKonstantin Belousov 			object->charge = entry->end - entry->start;
1869ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
18703364c323SKonstantin Belousov 		}
18713364c323SKonstantin Belousov 	} else if (entry->object.vm_object != NULL &&
18723364c323SKonstantin Belousov 		   ((entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0) &&
1873ef694c1aSEdward Tomasz Napierala 		   entry->cred != NULL) {
187489f6b863SAttilio Rao 		VM_OBJECT_WLOCK(entry->object.vm_object);
1875ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->object.vm_object->cred == NULL,
1876ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT: vm_entry_clip_end: both cred e %p", entry));
1877ef694c1aSEdward Tomasz Napierala 		entry->object.vm_object->cred = entry->cred;
18783364c323SKonstantin Belousov 		entry->object.vm_object->charge = entry->end - entry->start;
187989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(entry->object.vm_object);
1880ef694c1aSEdward Tomasz Napierala 		entry->cred = NULL;
188111cccda1SJohn Dyson 	}
188211cccda1SJohn Dyson 
188311cccda1SJohn Dyson 	/*
18840d94caffSDavid Greenman 	 * Create a new entry and insert it AFTER the specified entry
1885df8bae1dSRodney W. Grimes 	 */
1886df8bae1dSRodney W. Grimes 	new_entry = vm_map_entry_create(map);
1887df8bae1dSRodney W. Grimes 	*new_entry = *entry;
1888df8bae1dSRodney W. Grimes 
1889df8bae1dSRodney W. Grimes 	new_entry->start = entry->end = end;
1890df8bae1dSRodney W. Grimes 	new_entry->offset += (end - entry->start);
1891ef694c1aSEdward Tomasz Napierala 	if (new_entry->cred != NULL)
1892ef694c1aSEdward Tomasz Napierala 		crhold(entry->cred);
1893df8bae1dSRodney W. Grimes 
1894df8bae1dSRodney W. Grimes 	vm_map_entry_link(map, entry, new_entry);
1895df8bae1dSRodney W. Grimes 
18969fdfe602SMatthew Dillon 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0) {
1897df8bae1dSRodney W. Grimes 		vm_object_reference(new_entry->object.vm_object);
1898df8bae1dSRodney W. Grimes 	}
1899c0877f10SJohn Dyson }
1900df8bae1dSRodney W. Grimes 
1901df8bae1dSRodney W. Grimes /*
1902df8bae1dSRodney W. Grimes  *	vm_map_submap:		[ kernel use only ]
1903df8bae1dSRodney W. Grimes  *
1904df8bae1dSRodney W. Grimes  *	Mark the given range as handled by a subordinate map.
1905df8bae1dSRodney W. Grimes  *
1906df8bae1dSRodney W. Grimes  *	This range must have been created with vm_map_find,
1907df8bae1dSRodney W. Grimes  *	and no other operations may have been performed on this
1908df8bae1dSRodney W. Grimes  *	range prior to calling vm_map_submap.
1909df8bae1dSRodney W. Grimes  *
1910df8bae1dSRodney W. Grimes  *	Only a limited number of operations can be performed
1911df8bae1dSRodney W. Grimes  *	within this rage after calling vm_map_submap:
1912df8bae1dSRodney W. Grimes  *		vm_fault
1913df8bae1dSRodney W. Grimes  *	[Don't try vm_map_copy!]
1914df8bae1dSRodney W. Grimes  *
1915df8bae1dSRodney W. Grimes  *	To remove a submapping, one must first remove the
1916df8bae1dSRodney W. Grimes  *	range from the superior map, and then destroy the
1917df8bae1dSRodney W. Grimes  *	submap (if desired).  [Better yet, don't try it.]
1918df8bae1dSRodney W. Grimes  */
1919df8bae1dSRodney W. Grimes int
19201b40f8c0SMatthew Dillon vm_map_submap(
19211b40f8c0SMatthew Dillon 	vm_map_t map,
19221b40f8c0SMatthew Dillon 	vm_offset_t start,
19231b40f8c0SMatthew Dillon 	vm_offset_t end,
19241b40f8c0SMatthew Dillon 	vm_map_t submap)
1925df8bae1dSRodney W. Grimes {
1926df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
1927c0877f10SJohn Dyson 	int result = KERN_INVALID_ARGUMENT;
1928df8bae1dSRodney W. Grimes 
1929df8bae1dSRodney W. Grimes 	vm_map_lock(map);
1930df8bae1dSRodney W. Grimes 
1931df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
1932df8bae1dSRodney W. Grimes 
1933df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
1934df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
19350d94caffSDavid Greenman 	} else
1936df8bae1dSRodney W. Grimes 		entry = entry->next;
1937df8bae1dSRodney W. Grimes 
1938df8bae1dSRodney W. Grimes 	vm_map_clip_end(map, entry, end);
1939df8bae1dSRodney W. Grimes 
1940df8bae1dSRodney W. Grimes 	if ((entry->start == start) && (entry->end == end) &&
19419fdfe602SMatthew Dillon 	    ((entry->eflags & MAP_ENTRY_COW) == 0) &&
1942afa07f7eSJohn Dyson 	    (entry->object.vm_object == NULL)) {
19432d8acc0fSJohn Dyson 		entry->object.sub_map = submap;
1944afa07f7eSJohn Dyson 		entry->eflags |= MAP_ENTRY_IS_SUB_MAP;
1945df8bae1dSRodney W. Grimes 		result = KERN_SUCCESS;
1946df8bae1dSRodney W. Grimes 	}
1947df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
1948df8bae1dSRodney W. Grimes 
1949df8bae1dSRodney W. Grimes 	return (result);
1950df8bae1dSRodney W. Grimes }
1951df8bae1dSRodney W. Grimes 
1952df8bae1dSRodney W. Grimes /*
1953dd05fa19SAlan Cox  * The maximum number of pages to map if MAP_PREFAULT_PARTIAL is specified
19541f78f902SAlan Cox  */
19551f78f902SAlan Cox #define	MAX_INIT_PT	96
19561f78f902SAlan Cox 
19571f78f902SAlan Cox /*
19580551c08dSAlan Cox  *	vm_map_pmap_enter:
19590551c08dSAlan Cox  *
1960dd05fa19SAlan Cox  *	Preload the specified map's pmap with mappings to the specified
1961dd05fa19SAlan Cox  *	object's memory-resident pages.  No further physical pages are
1962dd05fa19SAlan Cox  *	allocated, and no further virtual pages are retrieved from secondary
1963dd05fa19SAlan Cox  *	storage.  If the specified flags include MAP_PREFAULT_PARTIAL, then a
1964dd05fa19SAlan Cox  *	limited number of page mappings are created at the low-end of the
1965dd05fa19SAlan Cox  *	specified address range.  (For this purpose, a superpage mapping
1966dd05fa19SAlan Cox  *	counts as one page mapping.)  Otherwise, all resident pages within
19673453bca8SAlan Cox  *	the specified address range are mapped.
19680551c08dSAlan Cox  */
1969077ec27cSAlan Cox static void
19704da4d293SAlan Cox vm_map_pmap_enter(vm_map_t map, vm_offset_t addr, vm_prot_t prot,
19710551c08dSAlan Cox     vm_object_t object, vm_pindex_t pindex, vm_size_t size, int flags)
19720551c08dSAlan Cox {
19738fece8c3SAlan Cox 	vm_offset_t start;
1974ce142d9eSAlan Cox 	vm_page_t p, p_start;
1975dd05fa19SAlan Cox 	vm_pindex_t mask, psize, threshold, tmpidx;
19760551c08dSAlan Cox 
1977ba8bca61SAlan Cox 	if ((prot & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0 || object == NULL)
19781f78f902SAlan Cox 		return;
19799af6d512SAttilio Rao 	VM_OBJECT_RLOCK(object);
19809af6d512SAttilio Rao 	if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
19819af6d512SAttilio Rao 		VM_OBJECT_RUNLOCK(object);
198289f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
198301381811SJohn Baldwin 		if (object->type == OBJT_DEVICE || object->type == OBJT_SG) {
19849af6d512SAttilio Rao 			pmap_object_init_pt(map->pmap, addr, object, pindex,
19859af6d512SAttilio Rao 			    size);
19869af6d512SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
19879af6d512SAttilio Rao 			return;
19889af6d512SAttilio Rao 		}
19899af6d512SAttilio Rao 		VM_OBJECT_LOCK_DOWNGRADE(object);
19901f78f902SAlan Cox 	}
19911f78f902SAlan Cox 
19921f78f902SAlan Cox 	psize = atop(size);
19931f78f902SAlan Cox 	if (psize + pindex > object->size) {
19949af6d512SAttilio Rao 		if (object->size < pindex) {
19959af6d512SAttilio Rao 			VM_OBJECT_RUNLOCK(object);
19969af6d512SAttilio Rao 			return;
19979af6d512SAttilio Rao 		}
19981f78f902SAlan Cox 		psize = object->size - pindex;
19991f78f902SAlan Cox 	}
20001f78f902SAlan Cox 
2001ce142d9eSAlan Cox 	start = 0;
2002ce142d9eSAlan Cox 	p_start = NULL;
2003dd05fa19SAlan Cox 	threshold = MAX_INIT_PT;
20041f78f902SAlan Cox 
2005b382c10aSKonstantin Belousov 	p = vm_page_find_least(object, pindex);
20061f78f902SAlan Cox 	/*
20071f78f902SAlan Cox 	 * Assert: the variable p is either (1) the page with the
20081f78f902SAlan Cox 	 * least pindex greater than or equal to the parameter pindex
20091f78f902SAlan Cox 	 * or (2) NULL.
20101f78f902SAlan Cox 	 */
20111f78f902SAlan Cox 	for (;
20121f78f902SAlan Cox 	     p != NULL && (tmpidx = p->pindex - pindex) < psize;
20131f78f902SAlan Cox 	     p = TAILQ_NEXT(p, listq)) {
20141f78f902SAlan Cox 		/*
20151f78f902SAlan Cox 		 * don't allow an madvise to blow away our really
20161f78f902SAlan Cox 		 * free pages allocating pv entries.
20171f78f902SAlan Cox 		 */
2018dd05fa19SAlan Cox 		if (((flags & MAP_PREFAULT_MADVISE) != 0 &&
2019dd05fa19SAlan Cox 		    vm_cnt.v_free_count < vm_cnt.v_free_reserved) ||
2020dd05fa19SAlan Cox 		    ((flags & MAP_PREFAULT_PARTIAL) != 0 &&
2021dd05fa19SAlan Cox 		    tmpidx >= threshold)) {
2022379fb642SAlan Cox 			psize = tmpidx;
20231f78f902SAlan Cox 			break;
20241f78f902SAlan Cox 		}
20250a2e596aSAlan Cox 		if (p->valid == VM_PAGE_BITS_ALL) {
2026ce142d9eSAlan Cox 			if (p_start == NULL) {
2027ce142d9eSAlan Cox 				start = addr + ptoa(tmpidx);
2028ce142d9eSAlan Cox 				p_start = p;
2029ce142d9eSAlan Cox 			}
2030dd05fa19SAlan Cox 			/* Jump ahead if a superpage mapping is possible. */
2031dd05fa19SAlan Cox 			if (p->psind > 0 && ((addr + ptoa(tmpidx)) &
2032dd05fa19SAlan Cox 			    (pagesizes[p->psind] - 1)) == 0) {
2033dd05fa19SAlan Cox 				mask = atop(pagesizes[p->psind]) - 1;
2034dd05fa19SAlan Cox 				if (tmpidx + mask < psize &&
203588302601SAlan Cox 				    vm_page_ps_test(p, PS_ALL_VALID, NULL)) {
2036dd05fa19SAlan Cox 					p += mask;
2037dd05fa19SAlan Cox 					threshold += mask;
2038dd05fa19SAlan Cox 				}
2039dd05fa19SAlan Cox 			}
20407bfda801SAlan Cox 		} else if (p_start != NULL) {
2041cf4682aeSAlan Cox 			pmap_enter_object(map->pmap, start, addr +
2042cf4682aeSAlan Cox 			    ptoa(tmpidx), p_start, prot);
2043cf4682aeSAlan Cox 			p_start = NULL;
2044cf4682aeSAlan Cox 		}
2045cf4682aeSAlan Cox 	}
2046c46b90e9SAlan Cox 	if (p_start != NULL)
2047379fb642SAlan Cox 		pmap_enter_object(map->pmap, start, addr + ptoa(psize),
2048379fb642SAlan Cox 		    p_start, prot);
20499af6d512SAttilio Rao 	VM_OBJECT_RUNLOCK(object);
20500551c08dSAlan Cox }
20510551c08dSAlan Cox 
20520551c08dSAlan Cox /*
2053df8bae1dSRodney W. Grimes  *	vm_map_protect:
2054df8bae1dSRodney W. Grimes  *
2055df8bae1dSRodney W. Grimes  *	Sets the protection of the specified address
2056df8bae1dSRodney W. Grimes  *	region in the target map.  If "set_max" is
2057df8bae1dSRodney W. Grimes  *	specified, the maximum protection is to be set;
2058df8bae1dSRodney W. Grimes  *	otherwise, only the current protection is affected.
2059df8bae1dSRodney W. Grimes  */
2060df8bae1dSRodney W. Grimes int
2061b9dcd593SBruce Evans vm_map_protect(vm_map_t map, vm_offset_t start, vm_offset_t end,
2062b9dcd593SBruce Evans 	       vm_prot_t new_prot, boolean_t set_max)
2063df8bae1dSRodney W. Grimes {
2064210a6886SKonstantin Belousov 	vm_map_entry_t current, entry;
20653364c323SKonstantin Belousov 	vm_object_t obj;
2066ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
2067210a6886SKonstantin Belousov 	vm_prot_t old_prot;
2068df8bae1dSRodney W. Grimes 
206979e9451fSKonstantin Belousov 	if (start == end)
207079e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
207179e9451fSKonstantin Belousov 
2072df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2073df8bae1dSRodney W. Grimes 
2074e1cb9d37SMark Johnston 	/*
2075e1cb9d37SMark Johnston 	 * Ensure that we are not concurrently wiring pages.  vm_map_wire() may
2076e1cb9d37SMark Johnston 	 * need to fault pages into the map and will drop the map lock while
2077e1cb9d37SMark Johnston 	 * doing so, and the VM object may end up in an inconsistent state if we
2078e1cb9d37SMark Johnston 	 * update the protection on the map entry in between faults.
2079e1cb9d37SMark Johnston 	 */
2080e1cb9d37SMark Johnston 	vm_map_wait_busy(map);
2081e1cb9d37SMark Johnston 
2082df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2083df8bae1dSRodney W. Grimes 
2084df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &entry)) {
2085df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
2086b7b2aac2SJohn Dyson 	} else {
2087df8bae1dSRodney W. Grimes 		entry = entry->next;
2088b7b2aac2SJohn Dyson 	}
2089df8bae1dSRodney W. Grimes 
2090df8bae1dSRodney W. Grimes 	/*
20910d94caffSDavid Greenman 	 * Make a first pass to check for protection violations.
2092df8bae1dSRodney W. Grimes 	 */
20931c2d20a1SMark Johnston 	for (current = entry; current != &map->header && current->start < end;
20941c2d20a1SMark Johnston 	    current = current->next) {
20958a89ca94SKonstantin Belousov 		if ((current->eflags & MAP_ENTRY_GUARD) != 0)
20968a89ca94SKonstantin Belousov 			continue;
2097afa07f7eSJohn Dyson 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2098a1f6d91cSDavid Greenman 			vm_map_unlock(map);
2099df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2100a1f6d91cSDavid Greenman 		}
2101df8bae1dSRodney W. Grimes 		if ((new_prot & current->max_protection) != new_prot) {
2102df8bae1dSRodney W. Grimes 			vm_map_unlock(map);
2103df8bae1dSRodney W. Grimes 			return (KERN_PROTECTION_FAILURE);
2104df8bae1dSRodney W. Grimes 		}
2105df8bae1dSRodney W. Grimes 	}
2106df8bae1dSRodney W. Grimes 
21073364c323SKonstantin Belousov 	/*
21083364c323SKonstantin Belousov 	 * Do an accounting pass for private read-only mappings that
21093364c323SKonstantin Belousov 	 * now will do cow due to allowed write (e.g. debugger sets
21103364c323SKonstantin Belousov 	 * breakpoint on text segment)
21113364c323SKonstantin Belousov 	 */
21121c2d20a1SMark Johnston 	for (current = entry; current != &map->header && current->start < end;
21131c2d20a1SMark Johnston 	    current = current->next) {
21143364c323SKonstantin Belousov 
21153364c323SKonstantin Belousov 		vm_map_clip_end(map, current, end);
21163364c323SKonstantin Belousov 
21173364c323SKonstantin Belousov 		if (set_max ||
21183364c323SKonstantin Belousov 		    ((new_prot & ~(current->protection)) & VM_PROT_WRITE) == 0 ||
211919bd0d9cSKonstantin Belousov 		    ENTRY_CHARGED(current) ||
212019bd0d9cSKonstantin Belousov 		    (current->eflags & MAP_ENTRY_GUARD) != 0) {
21213364c323SKonstantin Belousov 			continue;
21223364c323SKonstantin Belousov 		}
21233364c323SKonstantin Belousov 
2124ef694c1aSEdward Tomasz Napierala 		cred = curthread->td_ucred;
21253364c323SKonstantin Belousov 		obj = current->object.vm_object;
21263364c323SKonstantin Belousov 
21273364c323SKonstantin Belousov 		if (obj == NULL || (current->eflags & MAP_ENTRY_NEEDS_COPY)) {
21283364c323SKonstantin Belousov 			if (!swap_reserve(current->end - current->start)) {
21293364c323SKonstantin Belousov 				vm_map_unlock(map);
21303364c323SKonstantin Belousov 				return (KERN_RESOURCE_SHORTAGE);
21313364c323SKonstantin Belousov 			}
2132ef694c1aSEdward Tomasz Napierala 			crhold(cred);
2133ef694c1aSEdward Tomasz Napierala 			current->cred = cred;
21343364c323SKonstantin Belousov 			continue;
21353364c323SKonstantin Belousov 		}
21363364c323SKonstantin Belousov 
213789f6b863SAttilio Rao 		VM_OBJECT_WLOCK(obj);
21383364c323SKonstantin Belousov 		if (obj->type != OBJT_DEFAULT && obj->type != OBJT_SWAP) {
213989f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
21403364c323SKonstantin Belousov 			continue;
21413364c323SKonstantin Belousov 		}
21423364c323SKonstantin Belousov 
21433364c323SKonstantin Belousov 		/*
21443364c323SKonstantin Belousov 		 * Charge for the whole object allocation now, since
21453364c323SKonstantin Belousov 		 * we cannot distinguish between non-charged and
21463364c323SKonstantin Belousov 		 * charged clipped mapping of the same object later.
21473364c323SKonstantin Belousov 		 */
21483364c323SKonstantin Belousov 		KASSERT(obj->charge == 0,
21493d95614fSKonstantin Belousov 		    ("vm_map_protect: object %p overcharged (entry %p)",
21503d95614fSKonstantin Belousov 		    obj, current));
21513364c323SKonstantin Belousov 		if (!swap_reserve(ptoa(obj->size))) {
215289f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(obj);
21533364c323SKonstantin Belousov 			vm_map_unlock(map);
21543364c323SKonstantin Belousov 			return (KERN_RESOURCE_SHORTAGE);
21553364c323SKonstantin Belousov 		}
21563364c323SKonstantin Belousov 
2157ef694c1aSEdward Tomasz Napierala 		crhold(cred);
2158ef694c1aSEdward Tomasz Napierala 		obj->cred = cred;
21593364c323SKonstantin Belousov 		obj->charge = ptoa(obj->size);
216089f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(obj);
21613364c323SKonstantin Belousov 	}
21623364c323SKonstantin Belousov 
2163df8bae1dSRodney W. Grimes 	/*
21640d94caffSDavid Greenman 	 * Go back and fix up protections. [Note that clipping is not
21650d94caffSDavid Greenman 	 * necessary the second time.]
2166df8bae1dSRodney W. Grimes 	 */
21671c2d20a1SMark Johnston 	for (current = entry; current != &map->header && current->start < end;
21681c2d20a1SMark Johnston 	    current = current->next) {
216919bd0d9cSKonstantin Belousov 		if ((current->eflags & MAP_ENTRY_GUARD) != 0)
217019bd0d9cSKonstantin Belousov 			continue;
217119bd0d9cSKonstantin Belousov 
2172df8bae1dSRodney W. Grimes 		old_prot = current->protection;
2173210a6886SKonstantin Belousov 
2174df8bae1dSRodney W. Grimes 		if (set_max)
2175df8bae1dSRodney W. Grimes 			current->protection =
2176df8bae1dSRodney W. Grimes 			    (current->max_protection = new_prot) &
2177df8bae1dSRodney W. Grimes 			    old_prot;
2178df8bae1dSRodney W. Grimes 		else
2179df8bae1dSRodney W. Grimes 			current->protection = new_prot;
2180df8bae1dSRodney W. Grimes 
2181dd006a1bSAlan Cox 		/*
2182dd006a1bSAlan Cox 		 * For user wired map entries, the normal lazy evaluation of
2183dd006a1bSAlan Cox 		 * write access upgrades through soft page faults is
2184dd006a1bSAlan Cox 		 * undesirable.  Instead, immediately copy any pages that are
2185dd006a1bSAlan Cox 		 * copy-on-write and enable write access in the physical map.
2186dd006a1bSAlan Cox 		 */
2187dd006a1bSAlan Cox 		if ((current->eflags & MAP_ENTRY_USER_WIRED) != 0 &&
2188210a6886SKonstantin Belousov 		    (current->protection & VM_PROT_WRITE) != 0 &&
21895930251aSKonstantin Belousov 		    (old_prot & VM_PROT_WRITE) == 0)
2190210a6886SKonstantin Belousov 			vm_fault_copy_entry(map, map, current, current, NULL);
2191210a6886SKonstantin Belousov 
2192df8bae1dSRodney W. Grimes 		/*
21932fafce9eSAlan Cox 		 * When restricting access, update the physical map.  Worry
21942fafce9eSAlan Cox 		 * about copy-on-write here.
2195df8bae1dSRodney W. Grimes 		 */
21962fafce9eSAlan Cox 		if ((old_prot & ~current->protection) != 0) {
2197afa07f7eSJohn Dyson #define MASK(entry)	(((entry)->eflags & MAP_ENTRY_COW) ? ~VM_PROT_WRITE : \
2198df8bae1dSRodney W. Grimes 							VM_PROT_ALL)
2199df8bae1dSRodney W. Grimes 			pmap_protect(map->pmap, current->start,
2200df8bae1dSRodney W. Grimes 			    current->end,
22011c85e3dfSAlan Cox 			    current->protection & MASK(current));
2202df8bae1dSRodney W. Grimes #undef	MASK
2203df8bae1dSRodney W. Grimes 		}
22047d78abc9SJohn Dyson 		vm_map_simplify_entry(map, current);
2205df8bae1dSRodney W. Grimes 	}
2206df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2207df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2208df8bae1dSRodney W. Grimes }
2209df8bae1dSRodney W. Grimes 
2210df8bae1dSRodney W. Grimes /*
2211867a482dSJohn Dyson  *	vm_map_madvise:
2212867a482dSJohn Dyson  *
2213867a482dSJohn Dyson  *	This routine traverses a processes map handling the madvise
2214f7fc307aSAlan Cox  *	system call.  Advisories are classified as either those effecting
2215f7fc307aSAlan Cox  *	the vm_map_entry structure, or those effecting the underlying
2216f7fc307aSAlan Cox  *	objects.
2217867a482dSJohn Dyson  */
2218b4309055SMatthew Dillon int
22191b40f8c0SMatthew Dillon vm_map_madvise(
22201b40f8c0SMatthew Dillon 	vm_map_t map,
22211b40f8c0SMatthew Dillon 	vm_offset_t start,
22221b40f8c0SMatthew Dillon 	vm_offset_t end,
22231b40f8c0SMatthew Dillon 	int behav)
2224867a482dSJohn Dyson {
2225f7fc307aSAlan Cox 	vm_map_entry_t current, entry;
2226b4309055SMatthew Dillon 	int modify_map = 0;
2227867a482dSJohn Dyson 
2228b4309055SMatthew Dillon 	/*
2229b4309055SMatthew Dillon 	 * Some madvise calls directly modify the vm_map_entry, in which case
2230b4309055SMatthew Dillon 	 * we need to use an exclusive lock on the map and we need to perform
2231b4309055SMatthew Dillon 	 * various clipping operations.  Otherwise we only need a read-lock
2232b4309055SMatthew Dillon 	 * on the map.
2233b4309055SMatthew Dillon 	 */
2234b4309055SMatthew Dillon 	switch(behav) {
2235b4309055SMatthew Dillon 	case MADV_NORMAL:
2236b4309055SMatthew Dillon 	case MADV_SEQUENTIAL:
2237b4309055SMatthew Dillon 	case MADV_RANDOM:
22384f79d873SMatthew Dillon 	case MADV_NOSYNC:
22394f79d873SMatthew Dillon 	case MADV_AUTOSYNC:
22409730a5daSPaul Saab 	case MADV_NOCORE:
22419730a5daSPaul Saab 	case MADV_CORE:
224279e9451fSKonstantin Belousov 		if (start == end)
224379e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2244b4309055SMatthew Dillon 		modify_map = 1;
2245867a482dSJohn Dyson 		vm_map_lock(map);
2246b4309055SMatthew Dillon 		break;
2247b4309055SMatthew Dillon 	case MADV_WILLNEED:
2248b4309055SMatthew Dillon 	case MADV_DONTNEED:
2249b4309055SMatthew Dillon 	case MADV_FREE:
225079e9451fSKonstantin Belousov 		if (start == end)
225179e9451fSKonstantin Belousov 			return (KERN_SUCCESS);
2252f7fc307aSAlan Cox 		vm_map_lock_read(map);
2253b4309055SMatthew Dillon 		break;
2254b4309055SMatthew Dillon 	default:
2255b4309055SMatthew Dillon 		return (KERN_INVALID_ARGUMENT);
2256b4309055SMatthew Dillon 	}
2257b4309055SMatthew Dillon 
2258b4309055SMatthew Dillon 	/*
2259b4309055SMatthew Dillon 	 * Locate starting entry and clip if necessary.
2260b4309055SMatthew Dillon 	 */
2261867a482dSJohn Dyson 	VM_MAP_RANGE_CHECK(map, start, end);
2262867a482dSJohn Dyson 
2263867a482dSJohn Dyson 	if (vm_map_lookup_entry(map, start, &entry)) {
2264f7fc307aSAlan Cox 		if (modify_map)
2265867a482dSJohn Dyson 			vm_map_clip_start(map, entry, start);
2266b4309055SMatthew Dillon 	} else {
2267867a482dSJohn Dyson 		entry = entry->next;
2268b4309055SMatthew Dillon 	}
2269867a482dSJohn Dyson 
2270f7fc307aSAlan Cox 	if (modify_map) {
2271f7fc307aSAlan Cox 		/*
2272f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the vm_map_entry.
2273f7fc307aSAlan Cox 		 *
2274f7fc307aSAlan Cox 		 * We clip the vm_map_entry so that behavioral changes are
2275f7fc307aSAlan Cox 		 * limited to the specified address range.
2276f7fc307aSAlan Cox 		 */
2277867a482dSJohn Dyson 		for (current = entry;
2278867a482dSJohn Dyson 		     (current != &map->header) && (current->start < end);
2279b4309055SMatthew Dillon 		     current = current->next
2280b4309055SMatthew Dillon 		) {
2281f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2282867a482dSJohn Dyson 				continue;
2283fed9a903SJohn Dyson 
228447221757SJohn Dyson 			vm_map_clip_end(map, current, end);
2285fed9a903SJohn Dyson 
2286f7fc307aSAlan Cox 			switch (behav) {
2287867a482dSJohn Dyson 			case MADV_NORMAL:
22887f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_NORMAL);
2289867a482dSJohn Dyson 				break;
2290867a482dSJohn Dyson 			case MADV_SEQUENTIAL:
22917f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_SEQUENTIAL);
2292867a482dSJohn Dyson 				break;
2293867a482dSJohn Dyson 			case MADV_RANDOM:
22947f866e4bSAlan Cox 				vm_map_entry_set_behavior(current, MAP_ENTRY_BEHAV_RANDOM);
2295867a482dSJohn Dyson 				break;
22964f79d873SMatthew Dillon 			case MADV_NOSYNC:
22974f79d873SMatthew Dillon 				current->eflags |= MAP_ENTRY_NOSYNC;
22984f79d873SMatthew Dillon 				break;
22994f79d873SMatthew Dillon 			case MADV_AUTOSYNC:
23004f79d873SMatthew Dillon 				current->eflags &= ~MAP_ENTRY_NOSYNC;
23014f79d873SMatthew Dillon 				break;
23029730a5daSPaul Saab 			case MADV_NOCORE:
23039730a5daSPaul Saab 				current->eflags |= MAP_ENTRY_NOCOREDUMP;
23049730a5daSPaul Saab 				break;
23059730a5daSPaul Saab 			case MADV_CORE:
23069730a5daSPaul Saab 				current->eflags &= ~MAP_ENTRY_NOCOREDUMP;
23079730a5daSPaul Saab 				break;
2308867a482dSJohn Dyson 			default:
2309867a482dSJohn Dyson 				break;
2310867a482dSJohn Dyson 			}
2311f7fc307aSAlan Cox 			vm_map_simplify_entry(map, current);
2312867a482dSJohn Dyson 		}
2313867a482dSJohn Dyson 		vm_map_unlock(map);
2314b4309055SMatthew Dillon 	} else {
231592a59946SJohn Baldwin 		vm_pindex_t pstart, pend;
2316f7fc307aSAlan Cox 
2317f7fc307aSAlan Cox 		/*
2318f7fc307aSAlan Cox 		 * madvise behaviors that are implemented in the underlying
2319f7fc307aSAlan Cox 		 * vm_object.
2320f7fc307aSAlan Cox 		 *
2321f7fc307aSAlan Cox 		 * Since we don't clip the vm_map_entry, we have to clip
2322f7fc307aSAlan Cox 		 * the vm_object pindex and count.
2323f7fc307aSAlan Cox 		 */
2324f7fc307aSAlan Cox 		for (current = entry;
2325f7fc307aSAlan Cox 		     (current != &map->header) && (current->start < end);
2326b4309055SMatthew Dillon 		     current = current->next
2327b4309055SMatthew Dillon 		) {
232851321f7cSAlan Cox 			vm_offset_t useEnd, useStart;
23295f99b57cSMatthew Dillon 
2330f7fc307aSAlan Cox 			if (current->eflags & MAP_ENTRY_IS_SUB_MAP)
2331f7fc307aSAlan Cox 				continue;
2332f7fc307aSAlan Cox 
233392a59946SJohn Baldwin 			pstart = OFF_TO_IDX(current->offset);
233492a59946SJohn Baldwin 			pend = pstart + atop(current->end - current->start);
23355f99b57cSMatthew Dillon 			useStart = current->start;
233651321f7cSAlan Cox 			useEnd = current->end;
2337f7fc307aSAlan Cox 
2338f7fc307aSAlan Cox 			if (current->start < start) {
233992a59946SJohn Baldwin 				pstart += atop(start - current->start);
23405f99b57cSMatthew Dillon 				useStart = start;
2341f7fc307aSAlan Cox 			}
234251321f7cSAlan Cox 			if (current->end > end) {
234392a59946SJohn Baldwin 				pend -= atop(current->end - end);
234451321f7cSAlan Cox 				useEnd = end;
234551321f7cSAlan Cox 			}
2346f7fc307aSAlan Cox 
234792a59946SJohn Baldwin 			if (pstart >= pend)
2348f7fc307aSAlan Cox 				continue;
2349f7fc307aSAlan Cox 
235051321f7cSAlan Cox 			/*
235151321f7cSAlan Cox 			 * Perform the pmap_advise() before clearing
235251321f7cSAlan Cox 			 * PGA_REFERENCED in vm_page_advise().  Otherwise, a
235351321f7cSAlan Cox 			 * concurrent pmap operation, such as pmap_remove(),
235451321f7cSAlan Cox 			 * could clear a reference in the pmap and set
235551321f7cSAlan Cox 			 * PGA_REFERENCED on the page before the pmap_advise()
235651321f7cSAlan Cox 			 * had completed.  Consequently, the page would appear
235751321f7cSAlan Cox 			 * referenced based upon an old reference that
235851321f7cSAlan Cox 			 * occurred before this pmap_advise() ran.
235951321f7cSAlan Cox 			 */
236051321f7cSAlan Cox 			if (behav == MADV_DONTNEED || behav == MADV_FREE)
236151321f7cSAlan Cox 				pmap_advise(map->pmap, useStart, useEnd,
236251321f7cSAlan Cox 				    behav);
236351321f7cSAlan Cox 
236492a59946SJohn Baldwin 			vm_object_madvise(current->object.vm_object, pstart,
236592a59946SJohn Baldwin 			    pend, behav);
236654432196SKonstantin Belousov 
236754432196SKonstantin Belousov 			/*
236854432196SKonstantin Belousov 			 * Pre-populate paging structures in the
236954432196SKonstantin Belousov 			 * WILLNEED case.  For wired entries, the
237054432196SKonstantin Belousov 			 * paging structures are already populated.
237154432196SKonstantin Belousov 			 */
237254432196SKonstantin Belousov 			if (behav == MADV_WILLNEED &&
237354432196SKonstantin Belousov 			    current->wired_count == 0) {
23740551c08dSAlan Cox 				vm_map_pmap_enter(map,
23755f99b57cSMatthew Dillon 				    useStart,
23764da4d293SAlan Cox 				    current->protection,
2377f7fc307aSAlan Cox 				    current->object.vm_object,
237892a59946SJohn Baldwin 				    pstart,
237992a59946SJohn Baldwin 				    ptoa(pend - pstart),
2380e3026983SMatthew Dillon 				    MAP_PREFAULT_MADVISE
2381b4309055SMatthew Dillon 				);
2382f7fc307aSAlan Cox 			}
2383f7fc307aSAlan Cox 		}
2384f7fc307aSAlan Cox 		vm_map_unlock_read(map);
2385f7fc307aSAlan Cox 	}
2386b4309055SMatthew Dillon 	return (0);
2387867a482dSJohn Dyson }
2388867a482dSJohn Dyson 
2389867a482dSJohn Dyson 
2390867a482dSJohn Dyson /*
2391df8bae1dSRodney W. Grimes  *	vm_map_inherit:
2392df8bae1dSRodney W. Grimes  *
2393df8bae1dSRodney W. Grimes  *	Sets the inheritance of the specified address
2394df8bae1dSRodney W. Grimes  *	range in the target map.  Inheritance
2395df8bae1dSRodney W. Grimes  *	affects how the map will be shared with
2396e2abaaaaSAlan Cox  *	child maps at the time of vmspace_fork.
2397df8bae1dSRodney W. Grimes  */
2398df8bae1dSRodney W. Grimes int
2399b9dcd593SBruce Evans vm_map_inherit(vm_map_t map, vm_offset_t start, vm_offset_t end,
2400b9dcd593SBruce Evans 	       vm_inherit_t new_inheritance)
2401df8bae1dSRodney W. Grimes {
2402c0877f10SJohn Dyson 	vm_map_entry_t entry;
2403df8bae1dSRodney W. Grimes 	vm_map_entry_t temp_entry;
2404df8bae1dSRodney W. Grimes 
2405df8bae1dSRodney W. Grimes 	switch (new_inheritance) {
2406df8bae1dSRodney W. Grimes 	case VM_INHERIT_NONE:
2407df8bae1dSRodney W. Grimes 	case VM_INHERIT_COPY:
2408df8bae1dSRodney W. Grimes 	case VM_INHERIT_SHARE:
240978d7964bSXin LI 	case VM_INHERIT_ZERO:
2410df8bae1dSRodney W. Grimes 		break;
2411df8bae1dSRodney W. Grimes 	default:
2412df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ARGUMENT);
2413df8bae1dSRodney W. Grimes 	}
241479e9451fSKonstantin Belousov 	if (start == end)
241579e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2416df8bae1dSRodney W. Grimes 	vm_map_lock(map);
2417df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2418df8bae1dSRodney W. Grimes 	if (vm_map_lookup_entry(map, start, &temp_entry)) {
2419df8bae1dSRodney W. Grimes 		entry = temp_entry;
2420df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
24210d94caffSDavid Greenman 	} else
2422df8bae1dSRodney W. Grimes 		entry = temp_entry->next;
2423df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
2424df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
242519bd0d9cSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_GUARD) == 0 ||
242619bd0d9cSKonstantin Belousov 		    new_inheritance != VM_INHERIT_ZERO)
2427df8bae1dSRodney W. Grimes 			entry->inheritance = new_inheritance;
242844428f62SAlan Cox 		vm_map_simplify_entry(map, entry);
2429df8bae1dSRodney W. Grimes 		entry = entry->next;
2430df8bae1dSRodney W. Grimes 	}
2431df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
2432df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
2433df8bae1dSRodney W. Grimes }
2434df8bae1dSRodney W. Grimes 
2435df8bae1dSRodney W. Grimes /*
2436acd9a301SAlan Cox  *	vm_map_unwire:
2437acd9a301SAlan Cox  *
2438e27e17b7SAlan Cox  *	Implements both kernel and user unwiring.
2439acd9a301SAlan Cox  */
2440acd9a301SAlan Cox int
2441acd9a301SAlan Cox vm_map_unwire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2442abd498aaSBruce M Simpson     int flags)
2443acd9a301SAlan Cox {
2444acd9a301SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
2445acd9a301SAlan Cox 	vm_offset_t saved_start;
2446acd9a301SAlan Cox 	unsigned int last_timestamp;
2447acd9a301SAlan Cox 	int rv;
2448abd498aaSBruce M Simpson 	boolean_t need_wakeup, result, user_unwire;
2449acd9a301SAlan Cox 
245079e9451fSKonstantin Belousov 	if (start == end)
245179e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2452abd498aaSBruce M Simpson 	user_unwire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
2453acd9a301SAlan Cox 	vm_map_lock(map);
2454acd9a301SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
2455acd9a301SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2456abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2457cbef13d8SAlan Cox 			first_entry = first_entry->next;
2458abd498aaSBruce M Simpson 		else {
2459acd9a301SAlan Cox 			vm_map_unlock(map);
2460acd9a301SAlan Cox 			return (KERN_INVALID_ADDRESS);
2461acd9a301SAlan Cox 		}
2462abd498aaSBruce M Simpson 	}
2463acd9a301SAlan Cox 	last_timestamp = map->timestamp;
2464acd9a301SAlan Cox 	entry = first_entry;
2465acd9a301SAlan Cox 	while (entry != &map->header && entry->start < end) {
2466acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
2467acd9a301SAlan Cox 			/*
2468acd9a301SAlan Cox 			 * We have not yet clipped the entry.
2469acd9a301SAlan Cox 			 */
2470acd9a301SAlan Cox 			saved_start = (start >= entry->start) ? start :
2471acd9a301SAlan Cox 			    entry->start;
2472acd9a301SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
24738ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
2474acd9a301SAlan Cox 				/*
2475acd9a301SAlan Cox 				 * Allow interruption of user unwiring?
2476acd9a301SAlan Cox 				 */
2477acd9a301SAlan Cox 			}
2478acd9a301SAlan Cox 			vm_map_lock(map);
2479acd9a301SAlan Cox 			if (last_timestamp+1 != map->timestamp) {
2480acd9a301SAlan Cox 				/*
2481acd9a301SAlan Cox 				 * Look again for the entry because the map was
2482acd9a301SAlan Cox 				 * modified while it was unlocked.
2483acd9a301SAlan Cox 				 * Specifically, the entry may have been
2484acd9a301SAlan Cox 				 * clipped, merged, or deleted.
2485acd9a301SAlan Cox 				 */
2486acd9a301SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
2487acd9a301SAlan Cox 				    &tmp_entry)) {
2488cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2489cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2490cbef13d8SAlan Cox 					else {
2491acd9a301SAlan Cox 						if (saved_start == start) {
2492acd9a301SAlan Cox 							/*
2493acd9a301SAlan Cox 							 * First_entry has been deleted.
2494acd9a301SAlan Cox 							 */
2495acd9a301SAlan Cox 							vm_map_unlock(map);
2496acd9a301SAlan Cox 							return (KERN_INVALID_ADDRESS);
2497acd9a301SAlan Cox 						}
2498acd9a301SAlan Cox 						end = saved_start;
2499acd9a301SAlan Cox 						rv = KERN_INVALID_ADDRESS;
2500acd9a301SAlan Cox 						goto done;
2501acd9a301SAlan Cox 					}
2502cbef13d8SAlan Cox 				}
2503acd9a301SAlan Cox 				if (entry == first_entry)
2504acd9a301SAlan Cox 					first_entry = tmp_entry;
2505acd9a301SAlan Cox 				else
2506acd9a301SAlan Cox 					first_entry = NULL;
2507acd9a301SAlan Cox 				entry = tmp_entry;
2508acd9a301SAlan Cox 			}
2509acd9a301SAlan Cox 			last_timestamp = map->timestamp;
2510acd9a301SAlan Cox 			continue;
2511acd9a301SAlan Cox 		}
2512acd9a301SAlan Cox 		vm_map_clip_start(map, entry, start);
2513acd9a301SAlan Cox 		vm_map_clip_end(map, entry, end);
2514acd9a301SAlan Cox 		/*
2515acd9a301SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
2516acd9a301SAlan Cox 		 * above.)
2517acd9a301SAlan Cox 		 */
2518ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2519ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2520ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
2521acd9a301SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
25220acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2523acd9a301SAlan Cox 		/*
2524acd9a301SAlan Cox 		 * Check the map for holes in the specified region.
2525abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
2526acd9a301SAlan Cox 		 */
2527abd498aaSBruce M Simpson 		if (((flags & VM_MAP_WIRE_HOLESOK) == 0) &&
2528abd498aaSBruce M Simpson 		    (entry->end < end && (entry->next == &map->header ||
2529abd498aaSBruce M Simpson 		    entry->next->start > entry->end))) {
2530acd9a301SAlan Cox 			end = entry->end;
2531acd9a301SAlan Cox 			rv = KERN_INVALID_ADDRESS;
2532acd9a301SAlan Cox 			goto done;
2533acd9a301SAlan Cox 		}
2534acd9a301SAlan Cox 		/*
25353ffbc0cdSAlan Cox 		 * If system unwiring, require that the entry is system wired.
2536acd9a301SAlan Cox 		 */
25370ada205eSBrian Feldman 		if (!user_unwire &&
25380ada205eSBrian Feldman 		    vm_map_entry_system_wired_count(entry) == 0) {
2539acd9a301SAlan Cox 			end = entry->end;
2540acd9a301SAlan Cox 			rv = KERN_INVALID_ARGUMENT;
2541acd9a301SAlan Cox 			goto done;
2542acd9a301SAlan Cox 		}
2543acd9a301SAlan Cox 		entry = entry->next;
2544acd9a301SAlan Cox 	}
2545acd9a301SAlan Cox 	rv = KERN_SUCCESS;
2546acd9a301SAlan Cox done:
2547e27e17b7SAlan Cox 	need_wakeup = FALSE;
2548acd9a301SAlan Cox 	if (first_entry == NULL) {
2549acd9a301SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2550cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2551cbef13d8SAlan Cox 			first_entry = first_entry->next;
2552cbef13d8SAlan Cox 		else
2553acd9a301SAlan Cox 			KASSERT(result, ("vm_map_unwire: lookup failed"));
2554acd9a301SAlan Cox 	}
25550acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
25560acea7dfSKonstantin Belousov 	    entry = entry->next) {
25570acea7dfSKonstantin Belousov 		/*
25580acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
25590acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
25600acea7dfSKonstantin Belousov 		 * while the map lock was dropped for draining
25610acea7dfSKonstantin Belousov 		 * MAP_ENTRY_IN_TRANSITION.  Moreover, another thread
25620acea7dfSKonstantin Belousov 		 * could be simultaneously wiring this new mapping
25630acea7dfSKonstantin Belousov 		 * entry.  Detect these cases and skip any entries
25640acea7dfSKonstantin Belousov 		 * marked as in transition by us.
25650acea7dfSKonstantin Belousov 		 */
25660acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
25670acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
25680acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
25690acea7dfSKonstantin Belousov 			    ("vm_map_unwire: !HOLESOK and new/changed entry"));
25700acea7dfSKonstantin Belousov 			continue;
25710acea7dfSKonstantin Belousov 		}
25720acea7dfSKonstantin Belousov 
25733ffbc0cdSAlan Cox 		if (rv == KERN_SUCCESS && (!user_unwire ||
25743ffbc0cdSAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED))) {
2575b2f3846aSAlan Cox 			if (user_unwire)
2576b2f3846aSAlan Cox 				entry->eflags &= ~MAP_ENTRY_USER_WIRED;
257703462509SAlan Cox 			if (entry->wired_count == 1)
257803462509SAlan Cox 				vm_map_entry_unwire(map, entry);
257903462509SAlan Cox 			else
2580b2f3846aSAlan Cox 				entry->wired_count--;
2581b2f3846aSAlan Cox 		}
25820acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
2583ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: in-transition flag missing %p", entry));
2584ff3ae454SKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
2585ff3ae454SKonstantin Belousov 		    ("vm_map_unwire: alien wire %p", entry));
2586acd9a301SAlan Cox 		entry->eflags &= ~MAP_ENTRY_IN_TRANSITION;
25870acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
2588acd9a301SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
2589acd9a301SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
2590acd9a301SAlan Cox 			need_wakeup = TRUE;
2591acd9a301SAlan Cox 		}
2592acd9a301SAlan Cox 		vm_map_simplify_entry(map, entry);
2593acd9a301SAlan Cox 	}
2594acd9a301SAlan Cox 	vm_map_unlock(map);
2595acd9a301SAlan Cox 	if (need_wakeup)
2596acd9a301SAlan Cox 		vm_map_wakeup(map);
2597acd9a301SAlan Cox 	return (rv);
2598acd9a301SAlan Cox }
2599acd9a301SAlan Cox 
2600acd9a301SAlan Cox /*
260166cd575bSAlan Cox  *	vm_map_wire_entry_failure:
260266cd575bSAlan Cox  *
260366cd575bSAlan Cox  *	Handle a wiring failure on the given entry.
260466cd575bSAlan Cox  *
260566cd575bSAlan Cox  *	The map should be locked.
260666cd575bSAlan Cox  */
260766cd575bSAlan Cox static void
260866cd575bSAlan Cox vm_map_wire_entry_failure(vm_map_t map, vm_map_entry_t entry,
260966cd575bSAlan Cox     vm_offset_t failed_addr)
261066cd575bSAlan Cox {
261166cd575bSAlan Cox 
261266cd575bSAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
261366cd575bSAlan Cox 	KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 &&
261466cd575bSAlan Cox 	    entry->wired_count == 1,
261566cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p isn't being wired", entry));
261666cd575bSAlan Cox 	KASSERT(failed_addr < entry->end,
261766cd575bSAlan Cox 	    ("vm_map_wire_entry_failure: entry %p was fully wired", entry));
261866cd575bSAlan Cox 
261966cd575bSAlan Cox 	/*
262066cd575bSAlan Cox 	 * If any pages at the start of this entry were successfully wired,
262166cd575bSAlan Cox 	 * then unwire them.
262266cd575bSAlan Cox 	 */
262366cd575bSAlan Cox 	if (failed_addr > entry->start) {
262466cd575bSAlan Cox 		pmap_unwire(map->pmap, entry->start, failed_addr);
262566cd575bSAlan Cox 		vm_object_unwire(entry->object.vm_object, entry->offset,
262666cd575bSAlan Cox 		    failed_addr - entry->start, PQ_ACTIVE);
262766cd575bSAlan Cox 	}
262866cd575bSAlan Cox 
262966cd575bSAlan Cox 	/*
263066cd575bSAlan Cox 	 * Assign an out-of-range value to represent the failure to wire this
263166cd575bSAlan Cox 	 * entry.
263266cd575bSAlan Cox 	 */
263366cd575bSAlan Cox 	entry->wired_count = -1;
263466cd575bSAlan Cox }
263566cd575bSAlan Cox 
263666cd575bSAlan Cox /*
2637e27e17b7SAlan Cox  *	vm_map_wire:
2638e27e17b7SAlan Cox  *
2639e27e17b7SAlan Cox  *	Implements both kernel and user wiring.
2640e27e17b7SAlan Cox  */
2641e27e17b7SAlan Cox int
2642e27e17b7SAlan Cox vm_map_wire(vm_map_t map, vm_offset_t start, vm_offset_t end,
2643abd498aaSBruce M Simpson     int flags)
2644e27e17b7SAlan Cox {
264512d7cc84SAlan Cox 	vm_map_entry_t entry, first_entry, tmp_entry;
264666cd575bSAlan Cox 	vm_offset_t faddr, saved_end, saved_start;
264712d7cc84SAlan Cox 	unsigned int last_timestamp;
264812d7cc84SAlan Cox 	int rv;
264966cd575bSAlan Cox 	boolean_t need_wakeup, result, user_wire;
2650e4cd31ddSJeff Roberson 	vm_prot_t prot;
2651e27e17b7SAlan Cox 
265279e9451fSKonstantin Belousov 	if (start == end)
265379e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
2654e4cd31ddSJeff Roberson 	prot = 0;
2655e4cd31ddSJeff Roberson 	if (flags & VM_MAP_WIRE_WRITE)
2656e4cd31ddSJeff Roberson 		prot |= VM_PROT_WRITE;
2657abd498aaSBruce M Simpson 	user_wire = (flags & VM_MAP_WIRE_USER) ? TRUE : FALSE;
265812d7cc84SAlan Cox 	vm_map_lock(map);
265912d7cc84SAlan Cox 	VM_MAP_RANGE_CHECK(map, start, end);
266012d7cc84SAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry)) {
2661abd498aaSBruce M Simpson 		if (flags & VM_MAP_WIRE_HOLESOK)
2662cbef13d8SAlan Cox 			first_entry = first_entry->next;
2663abd498aaSBruce M Simpson 		else {
266412d7cc84SAlan Cox 			vm_map_unlock(map);
266512d7cc84SAlan Cox 			return (KERN_INVALID_ADDRESS);
266612d7cc84SAlan Cox 		}
2667abd498aaSBruce M Simpson 	}
266812d7cc84SAlan Cox 	last_timestamp = map->timestamp;
266912d7cc84SAlan Cox 	entry = first_entry;
267012d7cc84SAlan Cox 	while (entry != &map->header && entry->start < end) {
267112d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_IN_TRANSITION) {
267212d7cc84SAlan Cox 			/*
267312d7cc84SAlan Cox 			 * We have not yet clipped the entry.
267412d7cc84SAlan Cox 			 */
267512d7cc84SAlan Cox 			saved_start = (start >= entry->start) ? start :
267612d7cc84SAlan Cox 			    entry->start;
267712d7cc84SAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
26788ce2d00aSPawel Jakub Dawidek 			if (vm_map_unlock_and_wait(map, 0)) {
267912d7cc84SAlan Cox 				/*
268012d7cc84SAlan Cox 				 * Allow interruption of user wiring?
268112d7cc84SAlan Cox 				 */
268212d7cc84SAlan Cox 			}
268312d7cc84SAlan Cox 			vm_map_lock(map);
268412d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
268512d7cc84SAlan Cox 				/*
268612d7cc84SAlan Cox 				 * Look again for the entry because the map was
268712d7cc84SAlan Cox 				 * modified while it was unlocked.
268812d7cc84SAlan Cox 				 * Specifically, the entry may have been
268912d7cc84SAlan Cox 				 * clipped, merged, or deleted.
269012d7cc84SAlan Cox 				 */
269112d7cc84SAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
269212d7cc84SAlan Cox 				    &tmp_entry)) {
2693cbef13d8SAlan Cox 					if (flags & VM_MAP_WIRE_HOLESOK)
2694cbef13d8SAlan Cox 						tmp_entry = tmp_entry->next;
2695cbef13d8SAlan Cox 					else {
269612d7cc84SAlan Cox 						if (saved_start == start) {
269712d7cc84SAlan Cox 							/*
269812d7cc84SAlan Cox 							 * first_entry has been deleted.
269912d7cc84SAlan Cox 							 */
270012d7cc84SAlan Cox 							vm_map_unlock(map);
270112d7cc84SAlan Cox 							return (KERN_INVALID_ADDRESS);
270212d7cc84SAlan Cox 						}
270312d7cc84SAlan Cox 						end = saved_start;
270412d7cc84SAlan Cox 						rv = KERN_INVALID_ADDRESS;
270512d7cc84SAlan Cox 						goto done;
270612d7cc84SAlan Cox 					}
2707cbef13d8SAlan Cox 				}
270812d7cc84SAlan Cox 				if (entry == first_entry)
270912d7cc84SAlan Cox 					first_entry = tmp_entry;
271012d7cc84SAlan Cox 				else
271112d7cc84SAlan Cox 					first_entry = NULL;
271212d7cc84SAlan Cox 				entry = tmp_entry;
271312d7cc84SAlan Cox 			}
271412d7cc84SAlan Cox 			last_timestamp = map->timestamp;
271512d7cc84SAlan Cox 			continue;
271612d7cc84SAlan Cox 		}
271712d7cc84SAlan Cox 		vm_map_clip_start(map, entry, start);
271812d7cc84SAlan Cox 		vm_map_clip_end(map, entry, end);
271912d7cc84SAlan Cox 		/*
272012d7cc84SAlan Cox 		 * Mark the entry in case the map lock is released.  (See
272112d7cc84SAlan Cox 		 * above.)
272212d7cc84SAlan Cox 		 */
2723ff3ae454SKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 &&
2724ff3ae454SKonstantin Belousov 		    entry->wiring_thread == NULL,
2725ff3ae454SKonstantin Belousov 		    ("owned map entry %p", entry));
272612d7cc84SAlan Cox 		entry->eflags |= MAP_ENTRY_IN_TRANSITION;
27270acea7dfSKonstantin Belousov 		entry->wiring_thread = curthread;
2728e4cd31ddSJeff Roberson 		if ((entry->protection & (VM_PROT_READ | VM_PROT_EXECUTE)) == 0
2729e4cd31ddSJeff Roberson 		    || (entry->protection & prot) != prot) {
2730529ab57bSKonstantin Belousov 			entry->eflags |= MAP_ENTRY_WIRE_SKIPPED;
27316d7e8091SKonstantin Belousov 			if ((flags & VM_MAP_WIRE_HOLESOK) == 0) {
27326d7e8091SKonstantin Belousov 				end = entry->end;
27336d7e8091SKonstantin Belousov 				rv = KERN_INVALID_ADDRESS;
27346d7e8091SKonstantin Belousov 				goto done;
27356d7e8091SKonstantin Belousov 			}
27366d7e8091SKonstantin Belousov 			goto next_entry;
27376d7e8091SKonstantin Belousov 		}
2738e4cd31ddSJeff Roberson 		if (entry->wired_count == 0) {
27390ada205eSBrian Feldman 			entry->wired_count++;
274012d7cc84SAlan Cox 			saved_start = entry->start;
274112d7cc84SAlan Cox 			saved_end = entry->end;
274266cd575bSAlan Cox 
274312d7cc84SAlan Cox 			/*
274412d7cc84SAlan Cox 			 * Release the map lock, relying on the in-transition
2745a5db445dSMax Laier 			 * mark.  Mark the map busy for fork.
274612d7cc84SAlan Cox 			 */
2747a5db445dSMax Laier 			vm_map_busy(map);
274812d7cc84SAlan Cox 			vm_map_unlock(map);
274966cd575bSAlan Cox 
27500b695684SAlan Cox 			faddr = saved_start;
27510b695684SAlan Cox 			do {
275266cd575bSAlan Cox 				/*
275366cd575bSAlan Cox 				 * Simulate a fault to get the page and enter
275466cd575bSAlan Cox 				 * it into the physical map.
275566cd575bSAlan Cox 				 */
275666cd575bSAlan Cox 				if ((rv = vm_fault(map, faddr, VM_PROT_NONE,
27576a875bf9SKonstantin Belousov 				    VM_FAULT_WIRE)) != KERN_SUCCESS)
275866cd575bSAlan Cox 					break;
27590b695684SAlan Cox 			} while ((faddr += PAGE_SIZE) < saved_end);
276012d7cc84SAlan Cox 			vm_map_lock(map);
2761a5db445dSMax Laier 			vm_map_unbusy(map);
276212d7cc84SAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
276312d7cc84SAlan Cox 				/*
276412d7cc84SAlan Cox 				 * Look again for the entry because the map was
276512d7cc84SAlan Cox 				 * modified while it was unlocked.  The entry
276612d7cc84SAlan Cox 				 * may have been clipped, but NOT merged or
276712d7cc84SAlan Cox 				 * deleted.
276812d7cc84SAlan Cox 				 */
276912d7cc84SAlan Cox 				result = vm_map_lookup_entry(map, saved_start,
277012d7cc84SAlan Cox 				    &tmp_entry);
277112d7cc84SAlan Cox 				KASSERT(result, ("vm_map_wire: lookup failed"));
277212d7cc84SAlan Cox 				if (entry == first_entry)
277312d7cc84SAlan Cox 					first_entry = tmp_entry;
277412d7cc84SAlan Cox 				else
277512d7cc84SAlan Cox 					first_entry = NULL;
277612d7cc84SAlan Cox 				entry = tmp_entry;
277728c58286SAlan Cox 				while (entry->end < saved_end) {
277866cd575bSAlan Cox 					/*
277966cd575bSAlan Cox 					 * In case of failure, handle entries
278066cd575bSAlan Cox 					 * that were not fully wired here;
278166cd575bSAlan Cox 					 * fully wired entries are handled
278266cd575bSAlan Cox 					 * later.
278366cd575bSAlan Cox 					 */
278466cd575bSAlan Cox 					if (rv != KERN_SUCCESS &&
278566cd575bSAlan Cox 					    faddr < entry->end)
278666cd575bSAlan Cox 						vm_map_wire_entry_failure(map,
278766cd575bSAlan Cox 						    entry, faddr);
278812d7cc84SAlan Cox 					entry = entry->next;
278912d7cc84SAlan Cox 				}
279028c58286SAlan Cox 			}
279112d7cc84SAlan Cox 			last_timestamp = map->timestamp;
279212d7cc84SAlan Cox 			if (rv != KERN_SUCCESS) {
279366cd575bSAlan Cox 				vm_map_wire_entry_failure(map, entry, faddr);
279412d7cc84SAlan Cox 				end = entry->end;
279512d7cc84SAlan Cox 				goto done;
279612d7cc84SAlan Cox 			}
27970ada205eSBrian Feldman 		} else if (!user_wire ||
27980ada205eSBrian Feldman 			   (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
27990ada205eSBrian Feldman 			entry->wired_count++;
280012d7cc84SAlan Cox 		}
280112d7cc84SAlan Cox 		/*
280212d7cc84SAlan Cox 		 * Check the map for holes in the specified region.
2803abd498aaSBruce M Simpson 		 * If VM_MAP_WIRE_HOLESOK was specified, skip this check.
280412d7cc84SAlan Cox 		 */
28056d7e8091SKonstantin Belousov 	next_entry:
2806f141ed73SKonstantin Belousov 		if ((flags & VM_MAP_WIRE_HOLESOK) == 0 &&
2807f141ed73SKonstantin Belousov 		    entry->end < end && (entry->next == &map->header ||
2808f141ed73SKonstantin Belousov 		    entry->next->start > entry->end)) {
280912d7cc84SAlan Cox 			end = entry->end;
281012d7cc84SAlan Cox 			rv = KERN_INVALID_ADDRESS;
281112d7cc84SAlan Cox 			goto done;
281212d7cc84SAlan Cox 		}
281312d7cc84SAlan Cox 		entry = entry->next;
281412d7cc84SAlan Cox 	}
281512d7cc84SAlan Cox 	rv = KERN_SUCCESS;
281612d7cc84SAlan Cox done:
281712d7cc84SAlan Cox 	need_wakeup = FALSE;
281812d7cc84SAlan Cox 	if (first_entry == NULL) {
281912d7cc84SAlan Cox 		result = vm_map_lookup_entry(map, start, &first_entry);
2820cbef13d8SAlan Cox 		if (!result && (flags & VM_MAP_WIRE_HOLESOK))
2821cbef13d8SAlan Cox 			first_entry = first_entry->next;
2822cbef13d8SAlan Cox 		else
282312d7cc84SAlan Cox 			KASSERT(result, ("vm_map_wire: lookup failed"));
282412d7cc84SAlan Cox 	}
28250acea7dfSKonstantin Belousov 	for (entry = first_entry; entry != &map->header && entry->start < end;
28260acea7dfSKonstantin Belousov 	    entry = entry->next) {
28270acea7dfSKonstantin Belousov 		/*
28280acea7dfSKonstantin Belousov 		 * If VM_MAP_WIRE_HOLESOK was specified, an empty
28290acea7dfSKonstantin Belousov 		 * space in the unwired region could have been mapped
28300acea7dfSKonstantin Belousov 		 * while the map lock was dropped for faulting in the
28310acea7dfSKonstantin Belousov 		 * pages or draining MAP_ENTRY_IN_TRANSITION.
28320acea7dfSKonstantin Belousov 		 * Moreover, another thread could be simultaneously
28330acea7dfSKonstantin Belousov 		 * wiring this new mapping entry.  Detect these cases
2834546bb2d7SKonstantin Belousov 		 * and skip any entries marked as in transition not by us.
28350acea7dfSKonstantin Belousov 		 */
28360acea7dfSKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) == 0 ||
28370acea7dfSKonstantin Belousov 		    entry->wiring_thread != curthread) {
28380acea7dfSKonstantin Belousov 			KASSERT((flags & VM_MAP_WIRE_HOLESOK) != 0,
28390acea7dfSKonstantin Belousov 			    ("vm_map_wire: !HOLESOK and new/changed entry"));
28400acea7dfSKonstantin Belousov 			continue;
28410acea7dfSKonstantin Belousov 		}
28420acea7dfSKonstantin Belousov 
2843546bb2d7SKonstantin Belousov 		if ((entry->eflags & MAP_ENTRY_WIRE_SKIPPED) != 0)
2844546bb2d7SKonstantin Belousov 			goto next_entry_done;
2845546bb2d7SKonstantin Belousov 
284612d7cc84SAlan Cox 		if (rv == KERN_SUCCESS) {
284712d7cc84SAlan Cox 			if (user_wire)
284812d7cc84SAlan Cox 				entry->eflags |= MAP_ENTRY_USER_WIRED;
284928c58286SAlan Cox 		} else if (entry->wired_count == -1) {
285028c58286SAlan Cox 			/*
285128c58286SAlan Cox 			 * Wiring failed on this entry.  Thus, unwiring is
285228c58286SAlan Cox 			 * unnecessary.
285328c58286SAlan Cox 			 */
285428c58286SAlan Cox 			entry->wired_count = 0;
285503462509SAlan Cox 		} else if (!user_wire ||
285603462509SAlan Cox 		    (entry->eflags & MAP_ENTRY_USER_WIRED) == 0) {
285766cd575bSAlan Cox 			/*
285866cd575bSAlan Cox 			 * Undo the wiring.  Wiring succeeded on this entry
285966cd575bSAlan Cox 			 * but failed on a later entry.
286066cd575bSAlan Cox 			 */
286103462509SAlan Cox 			if (entry->wired_count == 1)
286203462509SAlan Cox 				vm_map_entry_unwire(map, entry);
286303462509SAlan Cox 			else
286412d7cc84SAlan Cox 				entry->wired_count--;
286512d7cc84SAlan Cox 		}
28666d7e8091SKonstantin Belousov 	next_entry_done:
28670acea7dfSKonstantin Belousov 		KASSERT((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0,
28680acea7dfSKonstantin Belousov 		    ("vm_map_wire: in-transition flag missing %p", entry));
28690acea7dfSKonstantin Belousov 		KASSERT(entry->wiring_thread == curthread,
28700acea7dfSKonstantin Belousov 		    ("vm_map_wire: alien wire %p", entry));
28710acea7dfSKonstantin Belousov 		entry->eflags &= ~(MAP_ENTRY_IN_TRANSITION |
28720acea7dfSKonstantin Belousov 		    MAP_ENTRY_WIRE_SKIPPED);
28730acea7dfSKonstantin Belousov 		entry->wiring_thread = NULL;
287412d7cc84SAlan Cox 		if (entry->eflags & MAP_ENTRY_NEEDS_WAKEUP) {
287512d7cc84SAlan Cox 			entry->eflags &= ~MAP_ENTRY_NEEDS_WAKEUP;
287612d7cc84SAlan Cox 			need_wakeup = TRUE;
287712d7cc84SAlan Cox 		}
287812d7cc84SAlan Cox 		vm_map_simplify_entry(map, entry);
287912d7cc84SAlan Cox 	}
288012d7cc84SAlan Cox 	vm_map_unlock(map);
288112d7cc84SAlan Cox 	if (need_wakeup)
288212d7cc84SAlan Cox 		vm_map_wakeup(map);
288312d7cc84SAlan Cox 	return (rv);
2884e27e17b7SAlan Cox }
2885e27e17b7SAlan Cox 
2886e27e17b7SAlan Cox /*
2887950f8459SAlan Cox  * vm_map_sync
2888df8bae1dSRodney W. Grimes  *
2889df8bae1dSRodney W. Grimes  * Push any dirty cached pages in the address range to their pager.
2890df8bae1dSRodney W. Grimes  * If syncio is TRUE, dirty pages are written synchronously.
2891df8bae1dSRodney W. Grimes  * If invalidate is TRUE, any cached pages are freed as well.
2892df8bae1dSRodney W. Grimes  *
2893637315edSAlan Cox  * If the size of the region from start to end is zero, we are
2894637315edSAlan Cox  * supposed to flush all modified pages within the region containing
2895637315edSAlan Cox  * start.  Unfortunately, a region can be split or coalesced with
2896637315edSAlan Cox  * neighboring regions, making it difficult to determine what the
2897637315edSAlan Cox  * original region was.  Therefore, we approximate this requirement by
2898637315edSAlan Cox  * flushing the current region containing start.
2899637315edSAlan Cox  *
2900df8bae1dSRodney W. Grimes  * Returns an error if any part of the specified range is not mapped.
2901df8bae1dSRodney W. Grimes  */
2902df8bae1dSRodney W. Grimes int
2903950f8459SAlan Cox vm_map_sync(
29041b40f8c0SMatthew Dillon 	vm_map_t map,
29051b40f8c0SMatthew Dillon 	vm_offset_t start,
29061b40f8c0SMatthew Dillon 	vm_offset_t end,
29071b40f8c0SMatthew Dillon 	boolean_t syncio,
29081b40f8c0SMatthew Dillon 	boolean_t invalidate)
2909df8bae1dSRodney W. Grimes {
2910c0877f10SJohn Dyson 	vm_map_entry_t current;
2911df8bae1dSRodney W. Grimes 	vm_map_entry_t entry;
2912df8bae1dSRodney W. Grimes 	vm_size_t size;
2913df8bae1dSRodney W. Grimes 	vm_object_t object;
2914a316d390SJohn Dyson 	vm_ooffset_t offset;
2915e53fa61bSKonstantin Belousov 	unsigned int last_timestamp;
2916126d6082SKonstantin Belousov 	boolean_t failed;
2917df8bae1dSRodney W. Grimes 
2918df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
2919df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
2920df8bae1dSRodney W. Grimes 	if (!vm_map_lookup_entry(map, start, &entry)) {
2921df8bae1dSRodney W. Grimes 		vm_map_unlock_read(map);
2922df8bae1dSRodney W. Grimes 		return (KERN_INVALID_ADDRESS);
2923637315edSAlan Cox 	} else if (start == end) {
2924637315edSAlan Cox 		start = entry->start;
2925637315edSAlan Cox 		end = entry->end;
2926df8bae1dSRodney W. Grimes 	}
2927df8bae1dSRodney W. Grimes 	/*
2928b7b7cd44SAlan Cox 	 * Make a first pass to check for user-wired memory and holes.
2929df8bae1dSRodney W. Grimes 	 */
29307b0e72d1SAlan Cox 	for (current = entry; current != &map->header && current->start < end;
29317b0e72d1SAlan Cox 	    current = current->next) {
2932b7b7cd44SAlan Cox 		if (invalidate && (current->eflags & MAP_ENTRY_USER_WIRED)) {
2933df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2934df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ARGUMENT);
2935df8bae1dSRodney W. Grimes 		}
2936df8bae1dSRodney W. Grimes 		if (end > current->end &&
2937df8bae1dSRodney W. Grimes 		    (current->next == &map->header ||
2938df8bae1dSRodney W. Grimes 			current->end != current->next->start)) {
2939df8bae1dSRodney W. Grimes 			vm_map_unlock_read(map);
2940df8bae1dSRodney W. Grimes 			return (KERN_INVALID_ADDRESS);
2941df8bae1dSRodney W. Grimes 		}
2942df8bae1dSRodney W. Grimes 	}
2943df8bae1dSRodney W. Grimes 
29442cf13952SAlan Cox 	if (invalidate)
2945bc105a67SAlan Cox 		pmap_remove(map->pmap, start, end);
2946126d6082SKonstantin Belousov 	failed = FALSE;
29472cf13952SAlan Cox 
2948df8bae1dSRodney W. Grimes 	/*
2949df8bae1dSRodney W. Grimes 	 * Make a second pass, cleaning/uncaching pages from the indicated
2950df8bae1dSRodney W. Grimes 	 * objects as we go.
2951df8bae1dSRodney W. Grimes 	 */
2952e53fa61bSKonstantin Belousov 	for (current = entry; current != &map->header && current->start < end;) {
2953df8bae1dSRodney W. Grimes 		offset = current->offset + (start - current->start);
2954df8bae1dSRodney W. Grimes 		size = (end <= current->end ? end : current->end) - start;
29559fdfe602SMatthew Dillon 		if (current->eflags & MAP_ENTRY_IS_SUB_MAP) {
2956c0877f10SJohn Dyson 			vm_map_t smap;
2957df8bae1dSRodney W. Grimes 			vm_map_entry_t tentry;
2958df8bae1dSRodney W. Grimes 			vm_size_t tsize;
2959df8bae1dSRodney W. Grimes 
29609fdfe602SMatthew Dillon 			smap = current->object.sub_map;
2961df8bae1dSRodney W. Grimes 			vm_map_lock_read(smap);
2962df8bae1dSRodney W. Grimes 			(void) vm_map_lookup_entry(smap, offset, &tentry);
2963df8bae1dSRodney W. Grimes 			tsize = tentry->end - offset;
2964df8bae1dSRodney W. Grimes 			if (tsize < size)
2965df8bae1dSRodney W. Grimes 				size = tsize;
2966df8bae1dSRodney W. Grimes 			object = tentry->object.vm_object;
2967df8bae1dSRodney W. Grimes 			offset = tentry->offset + (offset - tentry->start);
2968df8bae1dSRodney W. Grimes 			vm_map_unlock_read(smap);
2969df8bae1dSRodney W. Grimes 		} else {
2970df8bae1dSRodney W. Grimes 			object = current->object.vm_object;
2971df8bae1dSRodney W. Grimes 		}
2972e53fa61bSKonstantin Belousov 		vm_object_reference(object);
2973e53fa61bSKonstantin Belousov 		last_timestamp = map->timestamp;
2974e53fa61bSKonstantin Belousov 		vm_map_unlock_read(map);
2975126d6082SKonstantin Belousov 		if (!vm_object_sync(object, offset, size, syncio, invalidate))
2976126d6082SKonstantin Belousov 			failed = TRUE;
2977df8bae1dSRodney W. Grimes 		start += size;
2978e53fa61bSKonstantin Belousov 		vm_object_deallocate(object);
2979e53fa61bSKonstantin Belousov 		vm_map_lock_read(map);
2980e53fa61bSKonstantin Belousov 		if (last_timestamp == map->timestamp ||
2981e53fa61bSKonstantin Belousov 		    !vm_map_lookup_entry(map, start, &current))
2982e53fa61bSKonstantin Belousov 			current = current->next;
2983df8bae1dSRodney W. Grimes 	}
2984df8bae1dSRodney W. Grimes 
2985df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
2986126d6082SKonstantin Belousov 	return (failed ? KERN_FAILURE : KERN_SUCCESS);
2987df8bae1dSRodney W. Grimes }
2988df8bae1dSRodney W. Grimes 
2989df8bae1dSRodney W. Grimes /*
2990df8bae1dSRodney W. Grimes  *	vm_map_entry_unwire:	[ internal use only ]
2991df8bae1dSRodney W. Grimes  *
2992df8bae1dSRodney W. Grimes  *	Make the region specified by this entry pageable.
2993df8bae1dSRodney W. Grimes  *
2994df8bae1dSRodney W. Grimes  *	The map in question should be locked.
2995df8bae1dSRodney W. Grimes  *	[This is the reason for this routine's existence.]
2996df8bae1dSRodney W. Grimes  */
29970362d7d7SJohn Dyson static void
29981b40f8c0SMatthew Dillon vm_map_entry_unwire(vm_map_t map, vm_map_entry_t entry)
2999df8bae1dSRodney W. Grimes {
300003462509SAlan Cox 
300103462509SAlan Cox 	VM_MAP_ASSERT_LOCKED(map);
300203462509SAlan Cox 	KASSERT(entry->wired_count > 0,
300303462509SAlan Cox 	    ("vm_map_entry_unwire: entry %p isn't wired", entry));
300403462509SAlan Cox 	pmap_unwire(map->pmap, entry->start, entry->end);
300503462509SAlan Cox 	vm_object_unwire(entry->object.vm_object, entry->offset, entry->end -
300603462509SAlan Cox 	    entry->start, PQ_ACTIVE);
3007df8bae1dSRodney W. Grimes 	entry->wired_count = 0;
3008df8bae1dSRodney W. Grimes }
3009df8bae1dSRodney W. Grimes 
30100b367bd8SKonstantin Belousov static void
30110b367bd8SKonstantin Belousov vm_map_entry_deallocate(vm_map_entry_t entry, boolean_t system_map)
30120b367bd8SKonstantin Belousov {
30130b367bd8SKonstantin Belousov 
30140b367bd8SKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0)
30150b367bd8SKonstantin Belousov 		vm_object_deallocate(entry->object.vm_object);
30160b367bd8SKonstantin Belousov 	uma_zfree(system_map ? kmapentzone : mapentzone, entry);
30170b367bd8SKonstantin Belousov }
30180b367bd8SKonstantin Belousov 
3019df8bae1dSRodney W. Grimes /*
3020df8bae1dSRodney W. Grimes  *	vm_map_entry_delete:	[ internal use only ]
3021df8bae1dSRodney W. Grimes  *
3022df8bae1dSRodney W. Grimes  *	Deallocate the given entry from the target map.
3023df8bae1dSRodney W. Grimes  */
30240362d7d7SJohn Dyson static void
30251b40f8c0SMatthew Dillon vm_map_entry_delete(vm_map_t map, vm_map_entry_t entry)
3026df8bae1dSRodney W. Grimes {
302732a89c32SAlan Cox 	vm_object_t object;
30283364c323SKonstantin Belousov 	vm_pindex_t offidxstart, offidxend, count, size1;
3029d1780e8dSKonstantin Belousov 	vm_size_t size;
303032a89c32SAlan Cox 
3031df8bae1dSRodney W. Grimes 	vm_map_entry_unlink(map, entry);
30323364c323SKonstantin Belousov 	object = entry->object.vm_object;
303319bd0d9cSKonstantin Belousov 
303419bd0d9cSKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0) {
303519bd0d9cSKonstantin Belousov 		MPASS(entry->cred == NULL);
303619bd0d9cSKonstantin Belousov 		MPASS((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0);
303719bd0d9cSKonstantin Belousov 		MPASS(object == NULL);
303819bd0d9cSKonstantin Belousov 		vm_map_entry_deallocate(entry, map->system_map);
303919bd0d9cSKonstantin Belousov 		return;
304019bd0d9cSKonstantin Belousov 	}
304119bd0d9cSKonstantin Belousov 
30423364c323SKonstantin Belousov 	size = entry->end - entry->start;
30433364c323SKonstantin Belousov 	map->size -= size;
30443364c323SKonstantin Belousov 
3045ef694c1aSEdward Tomasz Napierala 	if (entry->cred != NULL) {
3046ef694c1aSEdward Tomasz Napierala 		swap_release_by_cred(size, entry->cred);
3047ef694c1aSEdward Tomasz Napierala 		crfree(entry->cred);
30483364c323SKonstantin Belousov 	}
3049df8bae1dSRodney W. Grimes 
305032a89c32SAlan Cox 	if ((entry->eflags & MAP_ENTRY_IS_SUB_MAP) == 0 &&
30513364c323SKonstantin Belousov 	    (object != NULL)) {
3052ef694c1aSEdward Tomasz Napierala 		KASSERT(entry->cred == NULL || object->cred == NULL ||
30533364c323SKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_NEEDS_COPY),
3054ef694c1aSEdward Tomasz Napierala 		    ("OVERCOMMIT vm_map_entry_delete: both cred %p", entry));
3055d1780e8dSKonstantin Belousov 		count = atop(size);
305632a89c32SAlan Cox 		offidxstart = OFF_TO_IDX(entry->offset);
305732a89c32SAlan Cox 		offidxend = offidxstart + count;
305889f6b863SAttilio Rao 		VM_OBJECT_WLOCK(object);
30599a4ee196SKonstantin Belousov 		if (object->ref_count != 1 && ((object->flags & (OBJ_NOSPLIT |
30609a4ee196SKonstantin Belousov 		    OBJ_ONEMAPPING)) == OBJ_ONEMAPPING ||
30612e47807cSJeff Roberson 		    object == kernel_object)) {
306232a89c32SAlan Cox 			vm_object_collapse(object);
30636bbee8e2SAlan Cox 
30646bbee8e2SAlan Cox 			/*
30656bbee8e2SAlan Cox 			 * The option OBJPR_NOTMAPPED can be passed here
30666bbee8e2SAlan Cox 			 * because vm_map_delete() already performed
30676bbee8e2SAlan Cox 			 * pmap_remove() on the only mapping to this range
30686bbee8e2SAlan Cox 			 * of pages.
30696bbee8e2SAlan Cox 			 */
30706bbee8e2SAlan Cox 			vm_object_page_remove(object, offidxstart, offidxend,
30716bbee8e2SAlan Cox 			    OBJPR_NOTMAPPED);
307232a89c32SAlan Cox 			if (object->type == OBJT_SWAP)
30739a4ee196SKonstantin Belousov 				swap_pager_freespace(object, offidxstart,
30749a4ee196SKonstantin Belousov 				    count);
307532a89c32SAlan Cox 			if (offidxend >= object->size &&
30763364c323SKonstantin Belousov 			    offidxstart < object->size) {
30773364c323SKonstantin Belousov 				size1 = object->size;
307832a89c32SAlan Cox 				object->size = offidxstart;
3079ef694c1aSEdward Tomasz Napierala 				if (object->cred != NULL) {
30803364c323SKonstantin Belousov 					size1 -= object->size;
30813364c323SKonstantin Belousov 					KASSERT(object->charge >= ptoa(size1),
30829a4ee196SKonstantin Belousov 					    ("object %p charge < 0", object));
30839a4ee196SKonstantin Belousov 					swap_release_by_cred(ptoa(size1),
30849a4ee196SKonstantin Belousov 					    object->cred);
30853364c323SKonstantin Belousov 					object->charge -= ptoa(size1);
30863364c323SKonstantin Belousov 				}
30873364c323SKonstantin Belousov 			}
308832a89c32SAlan Cox 		}
308989f6b863SAttilio Rao 		VM_OBJECT_WUNLOCK(object);
3090897d81a0SKonstantin Belousov 	} else
3091897d81a0SKonstantin Belousov 		entry->object.vm_object = NULL;
30920b367bd8SKonstantin Belousov 	if (map->system_map)
30930b367bd8SKonstantin Belousov 		vm_map_entry_deallocate(entry, TRUE);
30940b367bd8SKonstantin Belousov 	else {
30950b367bd8SKonstantin Belousov 		entry->next = curthread->td_map_def_user;
30960b367bd8SKonstantin Belousov 		curthread->td_map_def_user = entry;
30970b367bd8SKonstantin Belousov 	}
3098df8bae1dSRodney W. Grimes }
3099df8bae1dSRodney W. Grimes 
3100df8bae1dSRodney W. Grimes /*
3101df8bae1dSRodney W. Grimes  *	vm_map_delete:	[ internal use only ]
3102df8bae1dSRodney W. Grimes  *
3103df8bae1dSRodney W. Grimes  *	Deallocates the given address range from the target
3104df8bae1dSRodney W. Grimes  *	map.
3105df8bae1dSRodney W. Grimes  */
3106df8bae1dSRodney W. Grimes int
3107655c3490SKonstantin Belousov vm_map_delete(vm_map_t map, vm_offset_t start, vm_offset_t end)
3108df8bae1dSRodney W. Grimes {
3109c0877f10SJohn Dyson 	vm_map_entry_t entry;
3110df8bae1dSRodney W. Grimes 	vm_map_entry_t first_entry;
3111df8bae1dSRodney W. Grimes 
31123a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(map);
311379e9451fSKonstantin Belousov 	if (start == end)
311479e9451fSKonstantin Belousov 		return (KERN_SUCCESS);
31153a0916b8SKonstantin Belousov 
3116df8bae1dSRodney W. Grimes 	/*
3117df8bae1dSRodney W. Grimes 	 * Find the start of the region, and clip it
3118df8bae1dSRodney W. Grimes 	 */
3119876318ecSAlan Cox 	if (!vm_map_lookup_entry(map, start, &first_entry))
3120df8bae1dSRodney W. Grimes 		entry = first_entry->next;
3121876318ecSAlan Cox 	else {
3122df8bae1dSRodney W. Grimes 		entry = first_entry;
3123df8bae1dSRodney W. Grimes 		vm_map_clip_start(map, entry, start);
3124df8bae1dSRodney W. Grimes 	}
3125df8bae1dSRodney W. Grimes 
3126df8bae1dSRodney W. Grimes 	/*
3127df8bae1dSRodney W. Grimes 	 * Step through all entries in this region
3128df8bae1dSRodney W. Grimes 	 */
3129df8bae1dSRodney W. Grimes 	while ((entry != &map->header) && (entry->start < end)) {
3130df8bae1dSRodney W. Grimes 		vm_map_entry_t next;
3131df8bae1dSRodney W. Grimes 
313273b2baceSAlan Cox 		/*
313373b2baceSAlan Cox 		 * Wait for wiring or unwiring of an entry to complete.
31347c938963SBrian Feldman 		 * Also wait for any system wirings to disappear on
31357c938963SBrian Feldman 		 * user maps.
313673b2baceSAlan Cox 		 */
31377c938963SBrian Feldman 		if ((entry->eflags & MAP_ENTRY_IN_TRANSITION) != 0 ||
31387c938963SBrian Feldman 		    (vm_map_pmap(map) != kernel_pmap &&
31397c938963SBrian Feldman 		    vm_map_entry_system_wired_count(entry) != 0)) {
314073b2baceSAlan Cox 			unsigned int last_timestamp;
314173b2baceSAlan Cox 			vm_offset_t saved_start;
314273b2baceSAlan Cox 			vm_map_entry_t tmp_entry;
314373b2baceSAlan Cox 
314473b2baceSAlan Cox 			saved_start = entry->start;
314573b2baceSAlan Cox 			entry->eflags |= MAP_ENTRY_NEEDS_WAKEUP;
314673b2baceSAlan Cox 			last_timestamp = map->timestamp;
31478ce2d00aSPawel Jakub Dawidek 			(void) vm_map_unlock_and_wait(map, 0);
314873b2baceSAlan Cox 			vm_map_lock(map);
314973b2baceSAlan Cox 			if (last_timestamp + 1 != map->timestamp) {
315073b2baceSAlan Cox 				/*
315173b2baceSAlan Cox 				 * Look again for the entry because the map was
315273b2baceSAlan Cox 				 * modified while it was unlocked.
315373b2baceSAlan Cox 				 * Specifically, the entry may have been
315473b2baceSAlan Cox 				 * clipped, merged, or deleted.
315573b2baceSAlan Cox 				 */
315673b2baceSAlan Cox 				if (!vm_map_lookup_entry(map, saved_start,
315773b2baceSAlan Cox 							 &tmp_entry))
315873b2baceSAlan Cox 					entry = tmp_entry->next;
315973b2baceSAlan Cox 				else {
316073b2baceSAlan Cox 					entry = tmp_entry;
316173b2baceSAlan Cox 					vm_map_clip_start(map, entry,
316273b2baceSAlan Cox 							  saved_start);
316373b2baceSAlan Cox 				}
316473b2baceSAlan Cox 			}
316573b2baceSAlan Cox 			continue;
316673b2baceSAlan Cox 		}
3167df8bae1dSRodney W. Grimes 		vm_map_clip_end(map, entry, end);
3168df8bae1dSRodney W. Grimes 
3169c0877f10SJohn Dyson 		next = entry->next;
3170df8bae1dSRodney W. Grimes 
3171df8bae1dSRodney W. Grimes 		/*
31720d94caffSDavid Greenman 		 * Unwire before removing addresses from the pmap; otherwise,
31730d94caffSDavid Greenman 		 * unwiring will put the entries back in the pmap.
3174df8bae1dSRodney W. Grimes 		 */
3175c0877f10SJohn Dyson 		if (entry->wired_count != 0) {
3176df8bae1dSRodney W. Grimes 			vm_map_entry_unwire(map, entry);
3177c0877f10SJohn Dyson 		}
3178df8bae1dSRodney W. Grimes 
317932a89c32SAlan Cox 		pmap_remove(map->pmap, entry->start, entry->end);
3180df8bae1dSRodney W. Grimes 
3181df8bae1dSRodney W. Grimes 		/*
3182e608cc3cSKonstantin Belousov 		 * Delete the entry only after removing all pmap
3183e608cc3cSKonstantin Belousov 		 * entries pointing to its pages.  (Otherwise, its
3184e608cc3cSKonstantin Belousov 		 * page frames may be reallocated, and any modify bits
3185e608cc3cSKonstantin Belousov 		 * will be set in the wrong object!)
3186df8bae1dSRodney W. Grimes 		 */
3187df8bae1dSRodney W. Grimes 		vm_map_entry_delete(map, entry);
3188df8bae1dSRodney W. Grimes 		entry = next;
3189df8bae1dSRodney W. Grimes 	}
3190df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
3191df8bae1dSRodney W. Grimes }
3192df8bae1dSRodney W. Grimes 
3193df8bae1dSRodney W. Grimes /*
3194df8bae1dSRodney W. Grimes  *	vm_map_remove:
3195df8bae1dSRodney W. Grimes  *
3196df8bae1dSRodney W. Grimes  *	Remove the given address range from the target map.
3197df8bae1dSRodney W. Grimes  *	This is the exported form of vm_map_delete.
3198df8bae1dSRodney W. Grimes  */
3199df8bae1dSRodney W. Grimes int
32001b40f8c0SMatthew Dillon vm_map_remove(vm_map_t map, vm_offset_t start, vm_offset_t end)
3201df8bae1dSRodney W. Grimes {
32026eaee3feSAlan Cox 	int result;
3203df8bae1dSRodney W. Grimes 
3204df8bae1dSRodney W. Grimes 	vm_map_lock(map);
3205df8bae1dSRodney W. Grimes 	VM_MAP_RANGE_CHECK(map, start, end);
3206655c3490SKonstantin Belousov 	result = vm_map_delete(map, start, end);
3207df8bae1dSRodney W. Grimes 	vm_map_unlock(map);
3208df8bae1dSRodney W. Grimes 	return (result);
3209df8bae1dSRodney W. Grimes }
3210df8bae1dSRodney W. Grimes 
3211df8bae1dSRodney W. Grimes /*
3212df8bae1dSRodney W. Grimes  *	vm_map_check_protection:
3213df8bae1dSRodney W. Grimes  *
32142d5c7e45SMatthew Dillon  *	Assert that the target map allows the specified privilege on the
32152d5c7e45SMatthew Dillon  *	entire address region given.  The entire region must be allocated.
32162d5c7e45SMatthew Dillon  *
32172d5c7e45SMatthew Dillon  *	WARNING!  This code does not and should not check whether the
32182d5c7e45SMatthew Dillon  *	contents of the region is accessible.  For example a smaller file
32192d5c7e45SMatthew Dillon  *	might be mapped into a larger address space.
32202d5c7e45SMatthew Dillon  *
32212d5c7e45SMatthew Dillon  *	NOTE!  This code is also called by munmap().
3222d8834602SAlan Cox  *
3223d8834602SAlan Cox  *	The map must be locked.  A read lock is sufficient.
3224df8bae1dSRodney W. Grimes  */
32250d94caffSDavid Greenman boolean_t
3226b9dcd593SBruce Evans vm_map_check_protection(vm_map_t map, vm_offset_t start, vm_offset_t end,
3227b9dcd593SBruce Evans 			vm_prot_t protection)
3228df8bae1dSRodney W. Grimes {
3229c0877f10SJohn Dyson 	vm_map_entry_t entry;
3230df8bae1dSRodney W. Grimes 	vm_map_entry_t tmp_entry;
3231df8bae1dSRodney W. Grimes 
3232d8834602SAlan Cox 	if (!vm_map_lookup_entry(map, start, &tmp_entry))
3233df8bae1dSRodney W. Grimes 		return (FALSE);
3234df8bae1dSRodney W. Grimes 	entry = tmp_entry;
3235df8bae1dSRodney W. Grimes 
3236df8bae1dSRodney W. Grimes 	while (start < end) {
3237d8834602SAlan Cox 		if (entry == &map->header)
3238df8bae1dSRodney W. Grimes 			return (FALSE);
3239df8bae1dSRodney W. Grimes 		/*
3240df8bae1dSRodney W. Grimes 		 * No holes allowed!
3241df8bae1dSRodney W. Grimes 		 */
3242d8834602SAlan Cox 		if (start < entry->start)
3243df8bae1dSRodney W. Grimes 			return (FALSE);
3244df8bae1dSRodney W. Grimes 		/*
3245df8bae1dSRodney W. Grimes 		 * Check protection associated with entry.
3246df8bae1dSRodney W. Grimes 		 */
3247d8834602SAlan Cox 		if ((entry->protection & protection) != protection)
3248df8bae1dSRodney W. Grimes 			return (FALSE);
3249df8bae1dSRodney W. Grimes 		/* go to next entry */
3250df8bae1dSRodney W. Grimes 		start = entry->end;
3251df8bae1dSRodney W. Grimes 		entry = entry->next;
3252df8bae1dSRodney W. Grimes 	}
3253df8bae1dSRodney W. Grimes 	return (TRUE);
3254df8bae1dSRodney W. Grimes }
3255df8bae1dSRodney W. Grimes 
325686524867SJohn Dyson /*
3257df8bae1dSRodney W. Grimes  *	vm_map_copy_entry:
3258df8bae1dSRodney W. Grimes  *
3259df8bae1dSRodney W. Grimes  *	Copies the contents of the source entry to the destination
3260df8bae1dSRodney W. Grimes  *	entry.  The entries *must* be aligned properly.
3261df8bae1dSRodney W. Grimes  */
3262f708ef1bSPoul-Henning Kamp static void
32631b40f8c0SMatthew Dillon vm_map_copy_entry(
32641b40f8c0SMatthew Dillon 	vm_map_t src_map,
32651b40f8c0SMatthew Dillon 	vm_map_t dst_map,
32661b40f8c0SMatthew Dillon 	vm_map_entry_t src_entry,
32673364c323SKonstantin Belousov 	vm_map_entry_t dst_entry,
32683364c323SKonstantin Belousov 	vm_ooffset_t *fork_charge)
3269df8bae1dSRodney W. Grimes {
3270c0877f10SJohn Dyson 	vm_object_t src_object;
327184110e7eSKonstantin Belousov 	vm_map_entry_t fake_entry;
32723364c323SKonstantin Belousov 	vm_offset_t size;
3273ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
32743364c323SKonstantin Belousov 	int charged;
3275c0877f10SJohn Dyson 
32763a0916b8SKonstantin Belousov 	VM_MAP_ASSERT_LOCKED(dst_map);
32773a0916b8SKonstantin Belousov 
32789fdfe602SMatthew Dillon 	if ((dst_entry->eflags|src_entry->eflags) & MAP_ENTRY_IS_SUB_MAP)
3279df8bae1dSRodney W. Grimes 		return;
3280df8bae1dSRodney W. Grimes 
3281afaa41f6SAlan Cox 	if (src_entry->wired_count == 0 ||
3282afaa41f6SAlan Cox 	    (src_entry->protection & VM_PROT_WRITE) == 0) {
3283df8bae1dSRodney W. Grimes 		/*
32840d94caffSDavid Greenman 		 * If the source entry is marked needs_copy, it is already
32850d94caffSDavid Greenman 		 * write-protected.
3286df8bae1dSRodney W. Grimes 		 */
3287d9a9209aSAlan Cox 		if ((src_entry->eflags & MAP_ENTRY_NEEDS_COPY) == 0 &&
3288d9a9209aSAlan Cox 		    (src_entry->protection & VM_PROT_WRITE) != 0) {
3289df8bae1dSRodney W. Grimes 			pmap_protect(src_map->pmap,
3290df8bae1dSRodney W. Grimes 			    src_entry->start,
3291df8bae1dSRodney W. Grimes 			    src_entry->end,
3292df8bae1dSRodney W. Grimes 			    src_entry->protection & ~VM_PROT_WRITE);
3293df8bae1dSRodney W. Grimes 		}
3294b18bfc3dSJohn Dyson 
3295df8bae1dSRodney W. Grimes 		/*
3296df8bae1dSRodney W. Grimes 		 * Make a copy of the object.
3297df8bae1dSRodney W. Grimes 		 */
32983364c323SKonstantin Belousov 		size = src_entry->end - src_entry->start;
32998aef1712SMatthew Dillon 		if ((src_object = src_entry->object.vm_object) != NULL) {
330089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(src_object);
33013364c323SKonstantin Belousov 			charged = ENTRY_CHARGED(src_entry);
33029a4ee196SKonstantin Belousov 			if (src_object->handle == NULL &&
3303c0877f10SJohn Dyson 			    (src_object->type == OBJT_DEFAULT ||
3304c0877f10SJohn Dyson 			    src_object->type == OBJT_SWAP)) {
3305c0877f10SJohn Dyson 				vm_object_collapse(src_object);
33069a4ee196SKonstantin Belousov 				if ((src_object->flags & (OBJ_NOSPLIT |
33079a4ee196SKonstantin Belousov 				    OBJ_ONEMAPPING)) == OBJ_ONEMAPPING) {
3308c5aaa06dSAlan Cox 					vm_object_split(src_entry);
33099a4ee196SKonstantin Belousov 					src_object =
33109a4ee196SKonstantin Belousov 					    src_entry->object.vm_object;
3311a89c6258SAlan Cox 				}
3312a89c6258SAlan Cox 			}
3313b921a12bSAlan Cox 			vm_object_reference_locked(src_object);
3314069e9bc1SDoug Rabson 			vm_object_clear_flag(src_object, OBJ_ONEMAPPING);
3315ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL &&
33163364c323SKonstantin Belousov 			    !(src_entry->eflags & MAP_ENTRY_NEEDS_COPY)) {
3317ef694c1aSEdward Tomasz Napierala 				KASSERT(src_object->cred == NULL,
3318ef694c1aSEdward Tomasz Napierala 				    ("OVERCOMMIT: vm_map_copy_entry: cred %p",
33193364c323SKonstantin Belousov 				     src_object));
3320ef694c1aSEdward Tomasz Napierala 				src_object->cred = src_entry->cred;
33213364c323SKonstantin Belousov 				src_object->charge = size;
33223364c323SKonstantin Belousov 			}
332389f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(src_object);
3324c0877f10SJohn Dyson 			dst_entry->object.vm_object = src_object;
33253364c323SKonstantin Belousov 			if (charged) {
3326ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
3327ef694c1aSEdward Tomasz Napierala 				crhold(cred);
3328ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = cred;
33293364c323SKonstantin Belousov 				*fork_charge += size;
33303364c323SKonstantin Belousov 				if (!(src_entry->eflags &
33313364c323SKonstantin Belousov 				      MAP_ENTRY_NEEDS_COPY)) {
3332ef694c1aSEdward Tomasz Napierala 					crhold(cred);
3333ef694c1aSEdward Tomasz Napierala 					src_entry->cred = cred;
33343364c323SKonstantin Belousov 					*fork_charge += size;
33353364c323SKonstantin Belousov 				}
33363364c323SKonstantin Belousov 			}
33379a4ee196SKonstantin Belousov 			src_entry->eflags |= MAP_ENTRY_COW |
33389a4ee196SKonstantin Belousov 			    MAP_ENTRY_NEEDS_COPY;
33399a4ee196SKonstantin Belousov 			dst_entry->eflags |= MAP_ENTRY_COW |
33409a4ee196SKonstantin Belousov 			    MAP_ENTRY_NEEDS_COPY;
3341b18bfc3dSJohn Dyson 			dst_entry->offset = src_entry->offset;
334284110e7eSKonstantin Belousov 			if (src_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
334384110e7eSKonstantin Belousov 				/*
334484110e7eSKonstantin Belousov 				 * MAP_ENTRY_VN_WRITECNT cannot
334584110e7eSKonstantin Belousov 				 * indicate write reference from
334684110e7eSKonstantin Belousov 				 * src_entry, since the entry is
334784110e7eSKonstantin Belousov 				 * marked as needs copy.  Allocate a
334884110e7eSKonstantin Belousov 				 * fake entry that is used to
334984110e7eSKonstantin Belousov 				 * decrement object->un_pager.vnp.writecount
335084110e7eSKonstantin Belousov 				 * at the appropriate time.  Attach
335184110e7eSKonstantin Belousov 				 * fake_entry to the deferred list.
335284110e7eSKonstantin Belousov 				 */
335384110e7eSKonstantin Belousov 				fake_entry = vm_map_entry_create(dst_map);
335484110e7eSKonstantin Belousov 				fake_entry->eflags = MAP_ENTRY_VN_WRITECNT;
335584110e7eSKonstantin Belousov 				src_entry->eflags &= ~MAP_ENTRY_VN_WRITECNT;
335684110e7eSKonstantin Belousov 				vm_object_reference(src_object);
335784110e7eSKonstantin Belousov 				fake_entry->object.vm_object = src_object;
335884110e7eSKonstantin Belousov 				fake_entry->start = src_entry->start;
335984110e7eSKonstantin Belousov 				fake_entry->end = src_entry->end;
336084110e7eSKonstantin Belousov 				fake_entry->next = curthread->td_map_def_user;
336184110e7eSKonstantin Belousov 				curthread->td_map_def_user = fake_entry;
336284110e7eSKonstantin Belousov 			}
33630ec97ffcSKonstantin Belousov 
33640ec97ffcSKonstantin Belousov 			pmap_copy(dst_map->pmap, src_map->pmap,
33650ec97ffcSKonstantin Belousov 			    dst_entry->start, dst_entry->end - dst_entry->start,
33660ec97ffcSKonstantin Belousov 			    src_entry->start);
3367b18bfc3dSJohn Dyson 		} else {
3368b18bfc3dSJohn Dyson 			dst_entry->object.vm_object = NULL;
3369b18bfc3dSJohn Dyson 			dst_entry->offset = 0;
3370ef694c1aSEdward Tomasz Napierala 			if (src_entry->cred != NULL) {
3371ef694c1aSEdward Tomasz Napierala 				dst_entry->cred = curthread->td_ucred;
3372ef694c1aSEdward Tomasz Napierala 				crhold(dst_entry->cred);
33733364c323SKonstantin Belousov 				*fork_charge += size;
33743364c323SKonstantin Belousov 			}
3375b18bfc3dSJohn Dyson 		}
33760d94caffSDavid Greenman 	} else {
3377df8bae1dSRodney W. Grimes 		/*
3378afaa41f6SAlan Cox 		 * We don't want to make writeable wired pages copy-on-write.
3379afaa41f6SAlan Cox 		 * Immediately copy these pages into the new map by simulating
3380afaa41f6SAlan Cox 		 * page faults.  The new pages are pageable.
3381df8bae1dSRodney W. Grimes 		 */
3382121fd461SKonstantin Belousov 		vm_fault_copy_entry(dst_map, src_map, dst_entry, src_entry,
3383121fd461SKonstantin Belousov 		    fork_charge);
3384df8bae1dSRodney W. Grimes 	}
3385df8bae1dSRodney W. Grimes }
3386df8bae1dSRodney W. Grimes 
3387df8bae1dSRodney W. Grimes /*
33882a7be1b6SBrian Feldman  * vmspace_map_entry_forked:
33892a7be1b6SBrian Feldman  * Update the newly-forked vmspace each time a map entry is inherited
33902a7be1b6SBrian Feldman  * or copied.  The values for vm_dsize and vm_tsize are approximate
33912a7be1b6SBrian Feldman  * (and mostly-obsolete ideas in the face of mmap(2) et al.)
33922a7be1b6SBrian Feldman  */
33932a7be1b6SBrian Feldman static void
33942a7be1b6SBrian Feldman vmspace_map_entry_forked(const struct vmspace *vm1, struct vmspace *vm2,
33952a7be1b6SBrian Feldman     vm_map_entry_t entry)
33962a7be1b6SBrian Feldman {
33972a7be1b6SBrian Feldman 	vm_size_t entrysize;
33982a7be1b6SBrian Feldman 	vm_offset_t newend;
33992a7be1b6SBrian Feldman 
340019bd0d9cSKonstantin Belousov 	if ((entry->eflags & MAP_ENTRY_GUARD) != 0)
340119bd0d9cSKonstantin Belousov 		return;
34022a7be1b6SBrian Feldman 	entrysize = entry->end - entry->start;
34032a7be1b6SBrian Feldman 	vm2->vm_map.size += entrysize;
34042a7be1b6SBrian Feldman 	if (entry->eflags & (MAP_ENTRY_GROWS_DOWN | MAP_ENTRY_GROWS_UP)) {
34052a7be1b6SBrian Feldman 		vm2->vm_ssize += btoc(entrysize);
34062a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_daddr &&
34072a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize)) {
3408b351299cSAndrew Gallatin 		newend = MIN(entry->end,
34092a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_daddr + ctob(vm1->vm_dsize));
34102a7be1b6SBrian Feldman 		vm2->vm_dsize += btoc(newend - entry->start);
34112a7be1b6SBrian Feldman 	} else if (entry->start >= (vm_offset_t)vm1->vm_taddr &&
34122a7be1b6SBrian Feldman 	    entry->start < (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize)) {
3413b351299cSAndrew Gallatin 		newend = MIN(entry->end,
34142a7be1b6SBrian Feldman 		    (vm_offset_t)vm1->vm_taddr + ctob(vm1->vm_tsize));
34152a7be1b6SBrian Feldman 		vm2->vm_tsize += btoc(newend - entry->start);
34162a7be1b6SBrian Feldman 	}
34172a7be1b6SBrian Feldman }
34182a7be1b6SBrian Feldman 
34192a7be1b6SBrian Feldman /*
3420df8bae1dSRodney W. Grimes  * vmspace_fork:
3421df8bae1dSRodney W. Grimes  * Create a new process vmspace structure and vm_map
3422df8bae1dSRodney W. Grimes  * based on those of an existing process.  The new map
3423df8bae1dSRodney W. Grimes  * is based on the old map, according to the inheritance
3424df8bae1dSRodney W. Grimes  * values on the regions in that map.
3425df8bae1dSRodney W. Grimes  *
34262a7be1b6SBrian Feldman  * XXX It might be worth coalescing the entries added to the new vmspace.
34272a7be1b6SBrian Feldman  *
3428df8bae1dSRodney W. Grimes  * The source map must not be locked.
3429df8bae1dSRodney W. Grimes  */
3430df8bae1dSRodney W. Grimes struct vmspace *
34313364c323SKonstantin Belousov vmspace_fork(struct vmspace *vm1, vm_ooffset_t *fork_charge)
3432df8bae1dSRodney W. Grimes {
3433c0877f10SJohn Dyson 	struct vmspace *vm2;
343479e53838SAlan Cox 	vm_map_t new_map, old_map;
343579e53838SAlan Cox 	vm_map_entry_t new_entry, old_entry;
3436de5f6a77SJohn Dyson 	vm_object_t object;
34371fac7d7fSKonstantin Belousov 	int locked;
343819bd0d9cSKonstantin Belousov 	vm_inherit_t inh;
3439df8bae1dSRodney W. Grimes 
344079e53838SAlan Cox 	old_map = &vm1->vm_map;
344179e53838SAlan Cox 	/* Copy immutable fields of vm1 to vm2. */
344274d1d2b7SNeel Natu 	vm2 = vmspace_alloc(old_map->min_offset, old_map->max_offset, NULL);
344389b57fcfSKonstantin Belousov 	if (vm2 == NULL)
344479e53838SAlan Cox 		return (NULL);
34452a7be1b6SBrian Feldman 	vm2->vm_taddr = vm1->vm_taddr;
34462a7be1b6SBrian Feldman 	vm2->vm_daddr = vm1->vm_daddr;
34472a7be1b6SBrian Feldman 	vm2->vm_maxsaddr = vm1->vm_maxsaddr;
344879e53838SAlan Cox 	vm_map_lock(old_map);
344979e53838SAlan Cox 	if (old_map->busy)
345079e53838SAlan Cox 		vm_map_wait_busy(old_map);
345179e53838SAlan Cox 	new_map = &vm2->vm_map;
34521fac7d7fSKonstantin Belousov 	locked = vm_map_trylock(new_map); /* trylock to silence WITNESS */
34531fac7d7fSKonstantin Belousov 	KASSERT(locked, ("vmspace_fork: lock failed"));
3454df8bae1dSRodney W. Grimes 
3455df8bae1dSRodney W. Grimes 	old_entry = old_map->header.next;
3456df8bae1dSRodney W. Grimes 
3457df8bae1dSRodney W. Grimes 	while (old_entry != &old_map->header) {
3458afa07f7eSJohn Dyson 		if (old_entry->eflags & MAP_ENTRY_IS_SUB_MAP)
3459df8bae1dSRodney W. Grimes 			panic("vm_map_fork: encountered a submap");
3460df8bae1dSRodney W. Grimes 
346119bd0d9cSKonstantin Belousov 		inh = old_entry->inheritance;
346219bd0d9cSKonstantin Belousov 		if ((old_entry->eflags & MAP_ENTRY_GUARD) != 0 &&
346319bd0d9cSKonstantin Belousov 		    inh != VM_INHERIT_NONE)
346419bd0d9cSKonstantin Belousov 			inh = VM_INHERIT_COPY;
346519bd0d9cSKonstantin Belousov 
346619bd0d9cSKonstantin Belousov 		switch (inh) {
3467df8bae1dSRodney W. Grimes 		case VM_INHERIT_NONE:
3468df8bae1dSRodney W. Grimes 			break;
3469df8bae1dSRodney W. Grimes 
3470df8bae1dSRodney W. Grimes 		case VM_INHERIT_SHARE:
3471df8bae1dSRodney W. Grimes 			/*
3472fed9a903SJohn Dyson 			 * Clone the entry, creating the shared object if necessary.
3473fed9a903SJohn Dyson 			 */
3474fed9a903SJohn Dyson 			object = old_entry->object.vm_object;
3475fed9a903SJohn Dyson 			if (object == NULL) {
3476fed9a903SJohn Dyson 				object = vm_object_allocate(OBJT_DEFAULT,
3477c2e11a03SJohn Dyson 					atop(old_entry->end - old_entry->start));
3478fed9a903SJohn Dyson 				old_entry->object.vm_object = object;
347915d2d313SAlan Cox 				old_entry->offset = 0;
3480ef694c1aSEdward Tomasz Napierala 				if (old_entry->cred != NULL) {
3481ef694c1aSEdward Tomasz Napierala 					object->cred = old_entry->cred;
34823364c323SKonstantin Belousov 					object->charge = old_entry->end -
34833364c323SKonstantin Belousov 					    old_entry->start;
3484ef694c1aSEdward Tomasz Napierala 					old_entry->cred = NULL;
34853364c323SKonstantin Belousov 				}
34869a2f6362SAlan Cox 			}
34879a2f6362SAlan Cox 
34889a2f6362SAlan Cox 			/*
34899a2f6362SAlan Cox 			 * Add the reference before calling vm_object_shadow
34909a2f6362SAlan Cox 			 * to insure that a shadow object is created.
34919a2f6362SAlan Cox 			 */
34929a2f6362SAlan Cox 			vm_object_reference(object);
34939a2f6362SAlan Cox 			if (old_entry->eflags & MAP_ENTRY_NEEDS_COPY) {
34945069bf57SJohn Dyson 				vm_object_shadow(&old_entry->object.vm_object,
34955069bf57SJohn Dyson 				    &old_entry->offset,
34960cc74f14SAlan Cox 				    old_entry->end - old_entry->start);
34975069bf57SJohn Dyson 				old_entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
3498d30344bdSIan Dowse 				/* Transfer the second reference too. */
3499d30344bdSIan Dowse 				vm_object_reference(
3500d30344bdSIan Dowse 				    old_entry->object.vm_object);
35017fd10fb3SKonstantin Belousov 
35027fd10fb3SKonstantin Belousov 				/*
35037fd10fb3SKonstantin Belousov 				 * As in vm_map_simplify_entry(), the
3504b0994946SKonstantin Belousov 				 * vnode lock will not be acquired in
35057fd10fb3SKonstantin Belousov 				 * this call to vm_object_deallocate().
35067fd10fb3SKonstantin Belousov 				 */
3507d30344bdSIan Dowse 				vm_object_deallocate(object);
35085069bf57SJohn Dyson 				object = old_entry->object.vm_object;
3509fed9a903SJohn Dyson 			}
351089f6b863SAttilio Rao 			VM_OBJECT_WLOCK(object);
3511069e9bc1SDoug Rabson 			vm_object_clear_flag(object, OBJ_ONEMAPPING);
3512ef694c1aSEdward Tomasz Napierala 			if (old_entry->cred != NULL) {
3513ef694c1aSEdward Tomasz Napierala 				KASSERT(object->cred == NULL, ("vmspace_fork both cred"));
3514ef694c1aSEdward Tomasz Napierala 				object->cred = old_entry->cred;
35153364c323SKonstantin Belousov 				object->charge = old_entry->end - old_entry->start;
3516ef694c1aSEdward Tomasz Napierala 				old_entry->cred = NULL;
35173364c323SKonstantin Belousov 			}
3518b9781cf6SKonstantin Belousov 
3519b9781cf6SKonstantin Belousov 			/*
3520b9781cf6SKonstantin Belousov 			 * Assert the correct state of the vnode
3521b9781cf6SKonstantin Belousov 			 * v_writecount while the object is locked, to
3522b9781cf6SKonstantin Belousov 			 * not relock it later for the assertion
3523b9781cf6SKonstantin Belousov 			 * correctness.
3524b9781cf6SKonstantin Belousov 			 */
3525b9781cf6SKonstantin Belousov 			if (old_entry->eflags & MAP_ENTRY_VN_WRITECNT &&
3526b9781cf6SKonstantin Belousov 			    object->type == OBJT_VNODE) {
3527b9781cf6SKonstantin Belousov 				KASSERT(((struct vnode *)object->handle)->
3528b9781cf6SKonstantin Belousov 				    v_writecount > 0,
3529b9781cf6SKonstantin Belousov 				    ("vmspace_fork: v_writecount %p", object));
3530b9781cf6SKonstantin Belousov 				KASSERT(object->un_pager.vnp.writemappings > 0,
3531b9781cf6SKonstantin Belousov 				    ("vmspace_fork: vnp.writecount %p",
3532b9781cf6SKonstantin Belousov 				    object));
3533b9781cf6SKonstantin Belousov 			}
353489f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(object);
3535fed9a903SJohn Dyson 
3536fed9a903SJohn Dyson 			/*
3537ad5fca3bSAlan Cox 			 * Clone the entry, referencing the shared object.
3538df8bae1dSRodney W. Grimes 			 */
3539df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3540df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
35419f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
35429f6acfd1SKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION);
35430acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3544df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
354584110e7eSKonstantin Belousov 			if (new_entry->eflags & MAP_ENTRY_VN_WRITECNT) {
354684110e7eSKonstantin Belousov 				vnode_pager_update_writecount(object,
354784110e7eSKonstantin Belousov 				    new_entry->start, new_entry->end);
354884110e7eSKonstantin Belousov 			}
3549df8bae1dSRodney W. Grimes 
3550df8bae1dSRodney W. Grimes 			/*
35510d94caffSDavid Greenman 			 * Insert the entry into the new map -- we know we're
35520d94caffSDavid Greenman 			 * inserting at the end of the new map.
3553df8bae1dSRodney W. Grimes 			 */
3554df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3555df8bae1dSRodney W. Grimes 			    new_entry);
35562a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3557df8bae1dSRodney W. Grimes 
3558df8bae1dSRodney W. Grimes 			/*
3559df8bae1dSRodney W. Grimes 			 * Update the physical map
3560df8bae1dSRodney W. Grimes 			 */
3561df8bae1dSRodney W. Grimes 			pmap_copy(new_map->pmap, old_map->pmap,
3562df8bae1dSRodney W. Grimes 			    new_entry->start,
3563df8bae1dSRodney W. Grimes 			    (old_entry->end - old_entry->start),
3564df8bae1dSRodney W. Grimes 			    old_entry->start);
3565df8bae1dSRodney W. Grimes 			break;
3566df8bae1dSRodney W. Grimes 
3567df8bae1dSRodney W. Grimes 		case VM_INHERIT_COPY:
3568df8bae1dSRodney W. Grimes 			/*
3569df8bae1dSRodney W. Grimes 			 * Clone the entry and link into the map.
3570df8bae1dSRodney W. Grimes 			 */
3571df8bae1dSRodney W. Grimes 			new_entry = vm_map_entry_create(new_map);
3572df8bae1dSRodney W. Grimes 			*new_entry = *old_entry;
357384110e7eSKonstantin Belousov 			/*
357484110e7eSKonstantin Belousov 			 * Copied entry is COW over the old object.
357584110e7eSKonstantin Belousov 			 */
35769f6acfd1SKonstantin Belousov 			new_entry->eflags &= ~(MAP_ENTRY_USER_WIRED |
357784110e7eSKonstantin Belousov 			    MAP_ENTRY_IN_TRANSITION | MAP_ENTRY_VN_WRITECNT);
35780acea7dfSKonstantin Belousov 			new_entry->wiring_thread = NULL;
3579df8bae1dSRodney W. Grimes 			new_entry->wired_count = 0;
3580df8bae1dSRodney W. Grimes 			new_entry->object.vm_object = NULL;
3581ef694c1aSEdward Tomasz Napierala 			new_entry->cred = NULL;
3582df8bae1dSRodney W. Grimes 			vm_map_entry_link(new_map, new_map->header.prev,
3583df8bae1dSRodney W. Grimes 			    new_entry);
35842a7be1b6SBrian Feldman 			vmspace_map_entry_forked(vm1, vm2, new_entry);
3585bd7e5f99SJohn Dyson 			vm_map_copy_entry(old_map, new_map, old_entry,
35863364c323SKonstantin Belousov 			    new_entry, fork_charge);
3587df8bae1dSRodney W. Grimes 			break;
358878d7964bSXin LI 
358978d7964bSXin LI 		case VM_INHERIT_ZERO:
359078d7964bSXin LI 			/*
359178d7964bSXin LI 			 * Create a new anonymous mapping entry modelled from
359278d7964bSXin LI 			 * the old one.
359378d7964bSXin LI 			 */
359478d7964bSXin LI 			new_entry = vm_map_entry_create(new_map);
359578d7964bSXin LI 			memset(new_entry, 0, sizeof(*new_entry));
359678d7964bSXin LI 
359778d7964bSXin LI 			new_entry->start = old_entry->start;
359878d7964bSXin LI 			new_entry->end = old_entry->end;
359978d7964bSXin LI 			new_entry->eflags = old_entry->eflags &
360078d7964bSXin LI 			    ~(MAP_ENTRY_USER_WIRED | MAP_ENTRY_IN_TRANSITION |
360178d7964bSXin LI 			    MAP_ENTRY_VN_WRITECNT);
360278d7964bSXin LI 			new_entry->protection = old_entry->protection;
360378d7964bSXin LI 			new_entry->max_protection = old_entry->max_protection;
360478d7964bSXin LI 			new_entry->inheritance = VM_INHERIT_ZERO;
360578d7964bSXin LI 
360678d7964bSXin LI 			vm_map_entry_link(new_map, new_map->header.prev,
360778d7964bSXin LI 			    new_entry);
360878d7964bSXin LI 			vmspace_map_entry_forked(vm1, vm2, new_entry);
360978d7964bSXin LI 
361078d7964bSXin LI 			new_entry->cred = curthread->td_ucred;
361178d7964bSXin LI 			crhold(new_entry->cred);
361278d7964bSXin LI 			*fork_charge += (new_entry->end - new_entry->start);
361378d7964bSXin LI 
361478d7964bSXin LI 			break;
3615df8bae1dSRodney W. Grimes 		}
3616df8bae1dSRodney W. Grimes 		old_entry = old_entry->next;
3617df8bae1dSRodney W. Grimes 	}
361884110e7eSKonstantin Belousov 	/*
361984110e7eSKonstantin Belousov 	 * Use inlined vm_map_unlock() to postpone handling the deferred
362084110e7eSKonstantin Belousov 	 * map entries, which cannot be done until both old_map and
362184110e7eSKonstantin Belousov 	 * new_map locks are released.
362284110e7eSKonstantin Belousov 	 */
362384110e7eSKonstantin Belousov 	sx_xunlock(&old_map->lock);
362484110e7eSKonstantin Belousov 	sx_xunlock(&new_map->lock);
362584110e7eSKonstantin Belousov 	vm_map_process_deferred();
3626df8bae1dSRodney W. Grimes 
3627df8bae1dSRodney W. Grimes 	return (vm2);
3628df8bae1dSRodney W. Grimes }
3629df8bae1dSRodney W. Grimes 
36308056df6eSAlan Cox /*
36318056df6eSAlan Cox  * Create a process's stack for exec_new_vmspace().  This function is never
36328056df6eSAlan Cox  * asked to wire the newly created stack.
36338056df6eSAlan Cox  */
363494f7e29aSAlan Cox int
363594f7e29aSAlan Cox vm_map_stack(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
363694f7e29aSAlan Cox     vm_prot_t prot, vm_prot_t max, int cow)
363794f7e29aSAlan Cox {
36384648ba0aSKonstantin Belousov 	vm_size_t growsize, init_ssize;
36398056df6eSAlan Cox 	rlim_t vmemlim;
36404648ba0aSKonstantin Belousov 	int rv;
36414648ba0aSKonstantin Belousov 
36428056df6eSAlan Cox 	MPASS((map->flags & MAP_WIREFUTURE) == 0);
36434648ba0aSKonstantin Belousov 	growsize = sgrowsiz;
36444648ba0aSKonstantin Belousov 	init_ssize = (max_ssize < growsize) ? max_ssize : growsize;
36454648ba0aSKonstantin Belousov 	vm_map_lock(map);
3646f6f6d240SMateusz Guzik 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
36474648ba0aSKonstantin Belousov 	/* If we would blow our VMEM resource limit, no go */
36484648ba0aSKonstantin Belousov 	if (map->size + init_ssize > vmemlim) {
36494648ba0aSKonstantin Belousov 		rv = KERN_NO_SPACE;
36504648ba0aSKonstantin Belousov 		goto out;
36514648ba0aSKonstantin Belousov 	}
3652e1f92cccSAlan Cox 	rv = vm_map_stack_locked(map, addrbos, max_ssize, growsize, prot,
36534648ba0aSKonstantin Belousov 	    max, cow);
36544648ba0aSKonstantin Belousov out:
36554648ba0aSKonstantin Belousov 	vm_map_unlock(map);
36564648ba0aSKonstantin Belousov 	return (rv);
36574648ba0aSKonstantin Belousov }
36584648ba0aSKonstantin Belousov 
365919f49ad3SKonstantin Belousov static int stack_guard_page = 1;
366019f49ad3SKonstantin Belousov SYSCTL_INT(_security_bsd, OID_AUTO, stack_guard_page, CTLFLAG_RWTUN,
366119f49ad3SKonstantin Belousov     &stack_guard_page, 0,
366219f49ad3SKonstantin Belousov     "Specifies the number of guard pages for a stack that grows");
366319f49ad3SKonstantin Belousov 
36644648ba0aSKonstantin Belousov static int
36654648ba0aSKonstantin Belousov vm_map_stack_locked(vm_map_t map, vm_offset_t addrbos, vm_size_t max_ssize,
36664648ba0aSKonstantin Belousov     vm_size_t growsize, vm_prot_t prot, vm_prot_t max, int cow)
36674648ba0aSKonstantin Belousov {
3668fd75d710SMarcel Moolenaar 	vm_map_entry_t new_entry, prev_entry;
366919bd0d9cSKonstantin Belousov 	vm_offset_t bot, gap_bot, gap_top, top;
367019f49ad3SKonstantin Belousov 	vm_size_t init_ssize, sgp;
3671fd75d710SMarcel Moolenaar 	int orient, rv;
367294f7e29aSAlan Cox 
3673fd75d710SMarcel Moolenaar 	/*
3674fd75d710SMarcel Moolenaar 	 * The stack orientation is piggybacked with the cow argument.
3675fd75d710SMarcel Moolenaar 	 * Extract it into orient and mask the cow argument so that we
3676fd75d710SMarcel Moolenaar 	 * don't pass it around further.
3677fd75d710SMarcel Moolenaar 	 */
3678fd75d710SMarcel Moolenaar 	orient = cow & (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP);
3679fd75d710SMarcel Moolenaar 	KASSERT(orient != 0, ("No stack grow direction"));
368019bd0d9cSKonstantin Belousov 	KASSERT(orient != (MAP_STACK_GROWS_DOWN | MAP_STACK_GROWS_UP),
368119bd0d9cSKonstantin Belousov 	    ("bi-dir stack"));
3682fd75d710SMarcel Moolenaar 
368377bc7900SKonstantin Belousov 	if (addrbos < vm_map_min(map) ||
36849410cd7dSKonstantin Belousov 	    addrbos + max_ssize > vm_map_max(map) ||
36859410cd7dSKonstantin Belousov 	    addrbos + max_ssize <= addrbos)
36869410cd7dSKonstantin Belousov 		return (KERN_INVALID_ADDRESS);
36879410cd7dSKonstantin Belousov 	sgp = (vm_size_t)stack_guard_page * PAGE_SIZE;
36889410cd7dSKonstantin Belousov 	if (sgp >= max_ssize)
36899410cd7dSKonstantin Belousov 		return (KERN_INVALID_ARGUMENT);
3690fd75d710SMarcel Moolenaar 
369119f49ad3SKonstantin Belousov 	init_ssize = growsize;
369219f49ad3SKonstantin Belousov 	if (max_ssize < init_ssize + sgp)
369319f49ad3SKonstantin Belousov 		init_ssize = max_ssize - sgp;
369494f7e29aSAlan Cox 
369594f7e29aSAlan Cox 	/* If addr is already mapped, no go */
36964648ba0aSKonstantin Belousov 	if (vm_map_lookup_entry(map, addrbos, &prev_entry))
369794f7e29aSAlan Cox 		return (KERN_NO_SPACE);
3698a69ac174SMatthew Dillon 
3699fd75d710SMarcel Moolenaar 	/*
3700763df3ecSPedro F. Giffuni 	 * If we can't accommodate max_ssize in the current mapping, no go.
370194f7e29aSAlan Cox 	 */
370294f7e29aSAlan Cox 	if ((prev_entry->next != &map->header) &&
37034648ba0aSKonstantin Belousov 	    (prev_entry->next->start < addrbos + max_ssize))
370494f7e29aSAlan Cox 		return (KERN_NO_SPACE);
370594f7e29aSAlan Cox 
3706fd75d710SMarcel Moolenaar 	/*
3707fd75d710SMarcel Moolenaar 	 * We initially map a stack of only init_ssize.  We will grow as
3708fd75d710SMarcel Moolenaar 	 * needed later.  Depending on the orientation of the stack (i.e.
3709fd75d710SMarcel Moolenaar 	 * the grow direction) we either map at the top of the range, the
3710fd75d710SMarcel Moolenaar 	 * bottom of the range or in the middle.
371194f7e29aSAlan Cox 	 *
3712fd75d710SMarcel Moolenaar 	 * Note: we would normally expect prot and max to be VM_PROT_ALL,
3713fd75d710SMarcel Moolenaar 	 * and cow to be 0.  Possibly we should eliminate these as input
3714fd75d710SMarcel Moolenaar 	 * parameters, and just pass these values here in the insert call.
371594f7e29aSAlan Cox 	 */
371619bd0d9cSKonstantin Belousov 	if (orient == MAP_STACK_GROWS_DOWN) {
3717fd75d710SMarcel Moolenaar 		bot = addrbos + max_ssize - init_ssize;
3718fd75d710SMarcel Moolenaar 		top = bot + init_ssize;
371919bd0d9cSKonstantin Belousov 		gap_bot = addrbos;
372019bd0d9cSKonstantin Belousov 		gap_top = bot;
372119bd0d9cSKonstantin Belousov 	} else /* if (orient == MAP_STACK_GROWS_UP) */ {
372219bd0d9cSKonstantin Belousov 		bot = addrbos;
372319bd0d9cSKonstantin Belousov 		top = bot + init_ssize;
372419bd0d9cSKonstantin Belousov 		gap_bot = top;
372519bd0d9cSKonstantin Belousov 		gap_top = addrbos + max_ssize;
372619bd0d9cSKonstantin Belousov 	}
3727fd75d710SMarcel Moolenaar 	rv = vm_map_insert(map, NULL, 0, bot, top, prot, max, cow);
372819bd0d9cSKonstantin Belousov 	if (rv != KERN_SUCCESS)
372919bd0d9cSKonstantin Belousov 		return (rv);
3730fd75d710SMarcel Moolenaar 	new_entry = prev_entry->next;
373119bd0d9cSKonstantin Belousov 	KASSERT(new_entry->end == top || new_entry->start == bot,
373219bd0d9cSKonstantin Belousov 	    ("Bad entry start/end for new stack entry"));
3733712efe66SAlan Cox 	KASSERT((orient & MAP_STACK_GROWS_DOWN) == 0 ||
3734712efe66SAlan Cox 	    (new_entry->eflags & MAP_ENTRY_GROWS_DOWN) != 0,
3735712efe66SAlan Cox 	    ("new entry lacks MAP_ENTRY_GROWS_DOWN"));
3736712efe66SAlan Cox 	KASSERT((orient & MAP_STACK_GROWS_UP) == 0 ||
3737712efe66SAlan Cox 	    (new_entry->eflags & MAP_ENTRY_GROWS_UP) != 0,
3738712efe66SAlan Cox 	    ("new entry lacks MAP_ENTRY_GROWS_UP"));
373919bd0d9cSKonstantin Belousov 	rv = vm_map_insert(map, NULL, 0, gap_bot, gap_top, VM_PROT_NONE,
374019bd0d9cSKonstantin Belousov 	    VM_PROT_NONE, MAP_CREATE_GUARD | (orient == MAP_STACK_GROWS_DOWN ?
374119bd0d9cSKonstantin Belousov 	    MAP_CREATE_STACK_GAP_DN : MAP_CREATE_STACK_GAP_UP));
374219bd0d9cSKonstantin Belousov 	if (rv != KERN_SUCCESS)
374319bd0d9cSKonstantin Belousov 		(void)vm_map_delete(map, bot, top);
374494f7e29aSAlan Cox 	return (rv);
374594f7e29aSAlan Cox }
374694f7e29aSAlan Cox 
374719bd0d9cSKonstantin Belousov /*
374819bd0d9cSKonstantin Belousov  * Attempts to grow a vm stack entry.  Returns KERN_SUCCESS if we
374919bd0d9cSKonstantin Belousov  * successfully grow the stack.
375094f7e29aSAlan Cox  */
375119bd0d9cSKonstantin Belousov static int
375219bd0d9cSKonstantin Belousov vm_map_growstack(vm_map_t map, vm_offset_t addr, vm_map_entry_t gap_entry)
375394f7e29aSAlan Cox {
375419bd0d9cSKonstantin Belousov 	vm_map_entry_t stack_entry;
375519bd0d9cSKonstantin Belousov 	struct proc *p;
375619bd0d9cSKonstantin Belousov 	struct vmspace *vm;
375719bd0d9cSKonstantin Belousov 	struct ucred *cred;
375819bd0d9cSKonstantin Belousov 	vm_offset_t gap_end, gap_start, grow_start;
3759201f03b8SAlan Cox 	size_t grow_amount, guard, max_grow;
37607e19eda4SAndrey Zonov 	rlim_t lmemlim, stacklim, vmemlim;
376119bd0d9cSKonstantin Belousov 	int rv, rv1;
376219bd0d9cSKonstantin Belousov 	bool gap_deleted, grow_down, is_procstack;
37631ba5ad42SEdward Tomasz Napierala #ifdef notyet
37641ba5ad42SEdward Tomasz Napierala 	uint64_t limit;
37651ba5ad42SEdward Tomasz Napierala #endif
3766afcc55f3SEdward Tomasz Napierala #ifdef RACCT
37671ba5ad42SEdward Tomasz Napierala 	int error;
3768afcc55f3SEdward Tomasz Napierala #endif
376923955314SAlfred Perlstein 
377019bd0d9cSKonstantin Belousov 	p = curproc;
377119bd0d9cSKonstantin Belousov 	vm = p->p_vmspace;
3772eb5ea878SKonstantin Belousov 
3773eb5ea878SKonstantin Belousov 	/*
3774eb5ea878SKonstantin Belousov 	 * Disallow stack growth when the access is performed by a
3775eb5ea878SKonstantin Belousov 	 * debugger or AIO daemon.  The reason is that the wrong
3776eb5ea878SKonstantin Belousov 	 * resource limits are applied.
3777eb5ea878SKonstantin Belousov 	 */
3778eb5ea878SKonstantin Belousov 	if (map != &p->p_vmspace->vm_map || p->p_textvp == NULL)
3779f758aaddSKonstantin Belousov 		return (KERN_FAILURE);
3780eb5ea878SKonstantin Belousov 
378119bd0d9cSKonstantin Belousov 	MPASS(!map->system_map);
378219bd0d9cSKonstantin Belousov 
3783201f03b8SAlan Cox 	guard = stack_guard_page * PAGE_SIZE;
3784f6f6d240SMateusz Guzik 	lmemlim = lim_cur(curthread, RLIMIT_MEMLOCK);
3785f6f6d240SMateusz Guzik 	stacklim = lim_cur(curthread, RLIMIT_STACK);
3786f6f6d240SMateusz Guzik 	vmemlim = lim_cur(curthread, RLIMIT_VMEM);
378719bd0d9cSKonstantin Belousov retry:
378819bd0d9cSKonstantin Belousov 	/* If addr is not in a hole for a stack grow area, no need to grow. */
378919bd0d9cSKonstantin Belousov 	if (gap_entry == NULL && !vm_map_lookup_entry(map, addr, &gap_entry))
379019bd0d9cSKonstantin Belousov 		return (KERN_FAILURE);
379119bd0d9cSKonstantin Belousov 	if ((gap_entry->eflags & MAP_ENTRY_GUARD) == 0)
37920cddd8f0SMatthew Dillon 		return (KERN_SUCCESS);
379319bd0d9cSKonstantin Belousov 	if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_DN) != 0) {
379419bd0d9cSKonstantin Belousov 		stack_entry = gap_entry->next;
379519bd0d9cSKonstantin Belousov 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_DOWN) == 0 ||
379619bd0d9cSKonstantin Belousov 		    stack_entry->start != gap_entry->end)
379719bd0d9cSKonstantin Belousov 			return (KERN_FAILURE);
379819bd0d9cSKonstantin Belousov 		grow_amount = round_page(stack_entry->start - addr);
379919bd0d9cSKonstantin Belousov 		grow_down = true;
380019bd0d9cSKonstantin Belousov 	} else if ((gap_entry->eflags & MAP_ENTRY_STACK_GAP_UP) != 0) {
380119bd0d9cSKonstantin Belousov 		stack_entry = gap_entry->prev;
380219bd0d9cSKonstantin Belousov 		if ((stack_entry->eflags & MAP_ENTRY_GROWS_UP) == 0 ||
380319bd0d9cSKonstantin Belousov 		    stack_entry->end != gap_entry->start)
380419bd0d9cSKonstantin Belousov 			return (KERN_FAILURE);
380519bd0d9cSKonstantin Belousov 		grow_amount = round_page(addr + 1 - stack_entry->end);
380619bd0d9cSKonstantin Belousov 		grow_down = false;
3807b21a0008SMarcel Moolenaar 	} else {
380819bd0d9cSKonstantin Belousov 		return (KERN_FAILURE);
3809b21a0008SMarcel Moolenaar 	}
3810201f03b8SAlan Cox 	max_grow = gap_entry->end - gap_entry->start;
3811201f03b8SAlan Cox 	if (guard > max_grow)
3812201f03b8SAlan Cox 		return (KERN_NO_SPACE);
3813201f03b8SAlan Cox 	max_grow -= guard;
381419bd0d9cSKonstantin Belousov 	if (grow_amount > max_grow)
38150cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
381694f7e29aSAlan Cox 
3817b21a0008SMarcel Moolenaar 	/*
3818b21a0008SMarcel Moolenaar 	 * If this is the main process stack, see if we're over the stack
3819b21a0008SMarcel Moolenaar 	 * limit.
382094f7e29aSAlan Cox 	 */
382119bd0d9cSKonstantin Belousov 	is_procstack = addr >= (vm_offset_t)vm->vm_maxsaddr &&
382219bd0d9cSKonstantin Belousov 	    addr < (vm_offset_t)p->p_sysent->sv_usrstack;
382319bd0d9cSKonstantin Belousov 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim))
38240cddd8f0SMatthew Dillon 		return (KERN_NO_SPACE);
382519bd0d9cSKonstantin Belousov 
3826afcc55f3SEdward Tomasz Napierala #ifdef RACCT
38274b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
38281ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
38294b5c9cf6SEdward Tomasz Napierala 		if (is_procstack && racct_set(p, RACCT_STACK,
38304b5c9cf6SEdward Tomasz Napierala 		    ctob(vm->vm_ssize) + grow_amount)) {
38311ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
38321ba5ad42SEdward Tomasz Napierala 			return (KERN_NO_SPACE);
38331ba5ad42SEdward Tomasz Napierala 		}
38341ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
38354b5c9cf6SEdward Tomasz Napierala 	}
3836afcc55f3SEdward Tomasz Napierala #endif
383794f7e29aSAlan Cox 
383819bd0d9cSKonstantin Belousov 	grow_amount = roundup(grow_amount, sgrowsiz);
383919bd0d9cSKonstantin Belousov 	if (grow_amount > max_grow)
384019bd0d9cSKonstantin Belousov 		grow_amount = max_grow;
384191d5354aSJohn Baldwin 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > stacklim)) {
3842e4826248SAlan Cox 		grow_amount = trunc_page((vm_size_t)stacklim) -
3843e4826248SAlan Cox 		    ctob(vm->vm_ssize);
384494f7e29aSAlan Cox 	}
384519bd0d9cSKonstantin Belousov 
38461ba5ad42SEdward Tomasz Napierala #ifdef notyet
38471ba5ad42SEdward Tomasz Napierala 	PROC_LOCK(p);
38481ba5ad42SEdward Tomasz Napierala 	limit = racct_get_available(p, RACCT_STACK);
38491ba5ad42SEdward Tomasz Napierala 	PROC_UNLOCK(p);
38501ba5ad42SEdward Tomasz Napierala 	if (is_procstack && (ctob(vm->vm_ssize) + grow_amount > limit))
38511ba5ad42SEdward Tomasz Napierala 		grow_amount = limit - ctob(vm->vm_ssize);
38521ba5ad42SEdward Tomasz Napierala #endif
385319bd0d9cSKonstantin Belousov 
385419bd0d9cSKonstantin Belousov 	if (!old_mlock && (map->flags & MAP_WIREFUTURE) != 0) {
38553ac7d297SAndrey Zonov 		if (ptoa(pmap_wired_count(map->pmap)) + grow_amount > lmemlim) {
38567e19eda4SAndrey Zonov 			rv = KERN_NO_SPACE;
38577e19eda4SAndrey Zonov 			goto out;
38587e19eda4SAndrey Zonov 		}
38597e19eda4SAndrey Zonov #ifdef RACCT
38604b5c9cf6SEdward Tomasz Napierala 		if (racct_enable) {
38617e19eda4SAndrey Zonov 			PROC_LOCK(p);
38627e19eda4SAndrey Zonov 			if (racct_set(p, RACCT_MEMLOCK,
38633ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)) + grow_amount)) {
38647e19eda4SAndrey Zonov 				PROC_UNLOCK(p);
38657e19eda4SAndrey Zonov 				rv = KERN_NO_SPACE;
38667e19eda4SAndrey Zonov 				goto out;
38677e19eda4SAndrey Zonov 			}
38687e19eda4SAndrey Zonov 			PROC_UNLOCK(p);
38694b5c9cf6SEdward Tomasz Napierala 		}
38707e19eda4SAndrey Zonov #endif
38717e19eda4SAndrey Zonov 	}
387219bd0d9cSKonstantin Belousov 
3873a69ac174SMatthew Dillon 	/* If we would blow our VMEM resource limit, no go */
387491d5354aSJohn Baldwin 	if (map->size + grow_amount > vmemlim) {
38751ba5ad42SEdward Tomasz Napierala 		rv = KERN_NO_SPACE;
38761ba5ad42SEdward Tomasz Napierala 		goto out;
3877a69ac174SMatthew Dillon 	}
3878afcc55f3SEdward Tomasz Napierala #ifdef RACCT
38794b5c9cf6SEdward Tomasz Napierala 	if (racct_enable) {
38801ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
38811ba5ad42SEdward Tomasz Napierala 		if (racct_set(p, RACCT_VMEM, map->size + grow_amount)) {
38821ba5ad42SEdward Tomasz Napierala 			PROC_UNLOCK(p);
38831ba5ad42SEdward Tomasz Napierala 			rv = KERN_NO_SPACE;
38841ba5ad42SEdward Tomasz Napierala 			goto out;
38851ba5ad42SEdward Tomasz Napierala 		}
38861ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
38874b5c9cf6SEdward Tomasz Napierala 	}
3888afcc55f3SEdward Tomasz Napierala #endif
3889a69ac174SMatthew Dillon 
389019bd0d9cSKonstantin Belousov 	if (vm_map_lock_upgrade(map)) {
389119bd0d9cSKonstantin Belousov 		gap_entry = NULL;
389219bd0d9cSKonstantin Belousov 		vm_map_lock_read(map);
389319bd0d9cSKonstantin Belousov 		goto retry;
389494f7e29aSAlan Cox 	}
389594f7e29aSAlan Cox 
389619bd0d9cSKonstantin Belousov 	if (grow_down) {
389719bd0d9cSKonstantin Belousov 		grow_start = gap_entry->end - grow_amount;
389819bd0d9cSKonstantin Belousov 		if (gap_entry->start + grow_amount == gap_entry->end) {
389919bd0d9cSKonstantin Belousov 			gap_start = gap_entry->start;
390019bd0d9cSKonstantin Belousov 			gap_end = gap_entry->end;
390119bd0d9cSKonstantin Belousov 			vm_map_entry_delete(map, gap_entry);
390219bd0d9cSKonstantin Belousov 			gap_deleted = true;
390319bd0d9cSKonstantin Belousov 		} else {
390419bd0d9cSKonstantin Belousov 			MPASS(gap_entry->start < gap_entry->end - grow_amount);
390519bd0d9cSKonstantin Belousov 			gap_entry->end -= grow_amount;
390619bd0d9cSKonstantin Belousov 			vm_map_entry_resize_free(map, gap_entry);
390719bd0d9cSKonstantin Belousov 			gap_deleted = false;
390819bd0d9cSKonstantin Belousov 		}
390919bd0d9cSKonstantin Belousov 		rv = vm_map_insert(map, NULL, 0, grow_start,
391019bd0d9cSKonstantin Belousov 		    grow_start + grow_amount,
391119bd0d9cSKonstantin Belousov 		    stack_entry->protection, stack_entry->max_protection,
3912712efe66SAlan Cox 		    MAP_STACK_GROWS_DOWN);
391319bd0d9cSKonstantin Belousov 		if (rv != KERN_SUCCESS) {
391419bd0d9cSKonstantin Belousov 			if (gap_deleted) {
391519bd0d9cSKonstantin Belousov 				rv1 = vm_map_insert(map, NULL, 0, gap_start,
391619bd0d9cSKonstantin Belousov 				    gap_end, VM_PROT_NONE, VM_PROT_NONE,
391719bd0d9cSKonstantin Belousov 				    MAP_CREATE_GUARD | MAP_CREATE_STACK_GAP_DN);
391819bd0d9cSKonstantin Belousov 				MPASS(rv1 == KERN_SUCCESS);
391919bd0d9cSKonstantin Belousov 			} else {
392019bd0d9cSKonstantin Belousov 				gap_entry->end += grow_amount;
392119bd0d9cSKonstantin Belousov 				vm_map_entry_resize_free(map, gap_entry);
392219bd0d9cSKonstantin Belousov 			}
392394f7e29aSAlan Cox 		}
3924b21a0008SMarcel Moolenaar 	} else {
392519bd0d9cSKonstantin Belousov 		grow_start = stack_entry->end;
3926ef694c1aSEdward Tomasz Napierala 		cred = stack_entry->cred;
3927ef694c1aSEdward Tomasz Napierala 		if (cred == NULL && stack_entry->object.vm_object != NULL)
3928ef694c1aSEdward Tomasz Napierala 			cred = stack_entry->object.vm_object->cred;
3929ef694c1aSEdward Tomasz Napierala 		if (cred != NULL && !swap_reserve_by_cred(grow_amount, cred))
39303364c323SKonstantin Belousov 			rv = KERN_NO_SPACE;
3931b21a0008SMarcel Moolenaar 		/* Grow the underlying object if applicable. */
39323364c323SKonstantin Belousov 		else if (stack_entry->object.vm_object == NULL ||
3933b21a0008SMarcel Moolenaar 		    vm_object_coalesce(stack_entry->object.vm_object,
393457a21abaSAlan Cox 		    stack_entry->offset,
3935b21a0008SMarcel Moolenaar 		    (vm_size_t)(stack_entry->end - stack_entry->start),
3936ef694c1aSEdward Tomasz Napierala 		    (vm_size_t)grow_amount, cred != NULL)) {
393719bd0d9cSKonstantin Belousov 			if (gap_entry->start + grow_amount == gap_entry->end)
393819bd0d9cSKonstantin Belousov 				vm_map_entry_delete(map, gap_entry);
393919bd0d9cSKonstantin Belousov 			else
394019bd0d9cSKonstantin Belousov 				gap_entry->start += grow_amount;
394119bd0d9cSKonstantin Belousov 			stack_entry->end += grow_amount;
394219bd0d9cSKonstantin Belousov 			map->size += grow_amount;
39430164e057SAlan Cox 			vm_map_entry_resize_free(map, stack_entry);
3944b21a0008SMarcel Moolenaar 			rv = KERN_SUCCESS;
3945b21a0008SMarcel Moolenaar 		} else
3946b21a0008SMarcel Moolenaar 			rv = KERN_FAILURE;
3947b21a0008SMarcel Moolenaar 	}
3948b21a0008SMarcel Moolenaar 	if (rv == KERN_SUCCESS && is_procstack)
3949b21a0008SMarcel Moolenaar 		vm->vm_ssize += btoc(grow_amount);
3950b21a0008SMarcel Moolenaar 
3951abd498aaSBruce M Simpson 	/*
3952abd498aaSBruce M Simpson 	 * Heed the MAP_WIREFUTURE flag if it was set for this process.
3953abd498aaSBruce M Simpson 	 */
395419bd0d9cSKonstantin Belousov 	if (rv == KERN_SUCCESS && (map->flags & MAP_WIREFUTURE) != 0) {
395519bd0d9cSKonstantin Belousov 		vm_map_unlock(map);
395619bd0d9cSKonstantin Belousov 		vm_map_wire(map, grow_start, grow_start + grow_amount,
3957212e02c8SKonstantin Belousov 		    VM_MAP_WIRE_USER | VM_MAP_WIRE_NOHOLES);
395819bd0d9cSKonstantin Belousov 		vm_map_lock_read(map);
395919bd0d9cSKonstantin Belousov 	} else
396019bd0d9cSKonstantin Belousov 		vm_map_lock_downgrade(map);
3961abd498aaSBruce M Simpson 
39621ba5ad42SEdward Tomasz Napierala out:
3963afcc55f3SEdward Tomasz Napierala #ifdef RACCT
39644b5c9cf6SEdward Tomasz Napierala 	if (racct_enable && rv != KERN_SUCCESS) {
39651ba5ad42SEdward Tomasz Napierala 		PROC_LOCK(p);
39661ba5ad42SEdward Tomasz Napierala 		error = racct_set(p, RACCT_VMEM, map->size);
39671ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_VMEM failed"));
39687e19eda4SAndrey Zonov 		if (!old_mlock) {
39697e19eda4SAndrey Zonov 			error = racct_set(p, RACCT_MEMLOCK,
39703ac7d297SAndrey Zonov 			    ptoa(pmap_wired_count(map->pmap)));
39717e19eda4SAndrey Zonov 			KASSERT(error == 0, ("decreasing RACCT_MEMLOCK failed"));
39727e19eda4SAndrey Zonov 		}
39731ba5ad42SEdward Tomasz Napierala 	    	error = racct_set(p, RACCT_STACK, ctob(vm->vm_ssize));
39741ba5ad42SEdward Tomasz Napierala 		KASSERT(error == 0, ("decreasing RACCT_STACK failed"));
39751ba5ad42SEdward Tomasz Napierala 		PROC_UNLOCK(p);
39761ba5ad42SEdward Tomasz Napierala 	}
3977afcc55f3SEdward Tomasz Napierala #endif
39781ba5ad42SEdward Tomasz Napierala 
39790cddd8f0SMatthew Dillon 	return (rv);
398094f7e29aSAlan Cox }
398194f7e29aSAlan Cox 
3982df8bae1dSRodney W. Grimes /*
39835856e12eSJohn Dyson  * Unshare the specified VM space for exec.  If other processes are
39845856e12eSJohn Dyson  * mapped to it, then create a new one.  The new vmspace is null.
39855856e12eSJohn Dyson  */
398689b57fcfSKonstantin Belousov int
39873ebc1248SPeter Wemm vmspace_exec(struct proc *p, vm_offset_t minuser, vm_offset_t maxuser)
39881b40f8c0SMatthew Dillon {
39895856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
39905856e12eSJohn Dyson 	struct vmspace *newvmspace;
39915856e12eSJohn Dyson 
39927032434eSKonstantin Belousov 	KASSERT((curthread->td_pflags & TDP_EXECVMSPC) == 0,
39937032434eSKonstantin Belousov 	    ("vmspace_exec recursed"));
399474d1d2b7SNeel Natu 	newvmspace = vmspace_alloc(minuser, maxuser, NULL);
399589b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
399689b57fcfSKonstantin Belousov 		return (ENOMEM);
399751ab6c28SAlan Cox 	newvmspace->vm_swrss = oldvmspace->vm_swrss;
39985856e12eSJohn Dyson 	/*
39995856e12eSJohn Dyson 	 * This code is written like this for prototype purposes.  The
40005856e12eSJohn Dyson 	 * goal is to avoid running down the vmspace here, but let the
40015856e12eSJohn Dyson 	 * other process's that are still using the vmspace to finally
40025856e12eSJohn Dyson 	 * run it down.  Even though there is little or no chance of blocking
40035856e12eSJohn Dyson 	 * here, it is a good idea to keep this form for future mods.
40045856e12eSJohn Dyson 	 */
400557051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
40065856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
400757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
40086617724cSJeff Roberson 	if (p == curthread->td_proc)
4009b40ce416SJulian Elischer 		pmap_activate(curthread);
40107032434eSKonstantin Belousov 	curthread->td_pflags |= TDP_EXECVMSPC;
401189b57fcfSKonstantin Belousov 	return (0);
40125856e12eSJohn Dyson }
40135856e12eSJohn Dyson 
40145856e12eSJohn Dyson /*
40155856e12eSJohn Dyson  * Unshare the specified VM space for forcing COW.  This
40165856e12eSJohn Dyson  * is called by rfork, for the (RFMEM|RFPROC) == 0 case.
40175856e12eSJohn Dyson  */
401889b57fcfSKonstantin Belousov int
40191b40f8c0SMatthew Dillon vmspace_unshare(struct proc *p)
40201b40f8c0SMatthew Dillon {
40215856e12eSJohn Dyson 	struct vmspace *oldvmspace = p->p_vmspace;
40225856e12eSJohn Dyson 	struct vmspace *newvmspace;
40233364c323SKonstantin Belousov 	vm_ooffset_t fork_charge;
40245856e12eSJohn Dyson 
40255856e12eSJohn Dyson 	if (oldvmspace->vm_refcnt == 1)
402689b57fcfSKonstantin Belousov 		return (0);
40273364c323SKonstantin Belousov 	fork_charge = 0;
40283364c323SKonstantin Belousov 	newvmspace = vmspace_fork(oldvmspace, &fork_charge);
402989b57fcfSKonstantin Belousov 	if (newvmspace == NULL)
403089b57fcfSKonstantin Belousov 		return (ENOMEM);
4031ef694c1aSEdward Tomasz Napierala 	if (!swap_reserve_by_cred(fork_charge, p->p_ucred)) {
40323364c323SKonstantin Belousov 		vmspace_free(newvmspace);
40333364c323SKonstantin Belousov 		return (ENOMEM);
40343364c323SKonstantin Belousov 	}
403557051fdcSTor Egge 	PROC_VMSPACE_LOCK(p);
40365856e12eSJohn Dyson 	p->p_vmspace = newvmspace;
403757051fdcSTor Egge 	PROC_VMSPACE_UNLOCK(p);
40386617724cSJeff Roberson 	if (p == curthread->td_proc)
4039b40ce416SJulian Elischer 		pmap_activate(curthread);
4040b56ef1c1SJohn Baldwin 	vmspace_free(oldvmspace);
404189b57fcfSKonstantin Belousov 	return (0);
40425856e12eSJohn Dyson }
40435856e12eSJohn Dyson 
40445856e12eSJohn Dyson /*
4045df8bae1dSRodney W. Grimes  *	vm_map_lookup:
4046df8bae1dSRodney W. Grimes  *
4047df8bae1dSRodney W. Grimes  *	Finds the VM object, offset, and
4048df8bae1dSRodney W. Grimes  *	protection for a given virtual address in the
4049df8bae1dSRodney W. Grimes  *	specified map, assuming a page fault of the
4050df8bae1dSRodney W. Grimes  *	type specified.
4051df8bae1dSRodney W. Grimes  *
4052df8bae1dSRodney W. Grimes  *	Leaves the map in question locked for read; return
4053df8bae1dSRodney W. Grimes  *	values are guaranteed until a vm_map_lookup_done
4054df8bae1dSRodney W. Grimes  *	call is performed.  Note that the map argument
4055df8bae1dSRodney W. Grimes  *	is in/out; the returned map must be used in
4056df8bae1dSRodney W. Grimes  *	the call to vm_map_lookup_done.
4057df8bae1dSRodney W. Grimes  *
4058df8bae1dSRodney W. Grimes  *	A handle (out_entry) is returned for use in
4059df8bae1dSRodney W. Grimes  *	vm_map_lookup_done, to make that fast.
4060df8bae1dSRodney W. Grimes  *
4061df8bae1dSRodney W. Grimes  *	If a lookup is requested with "write protection"
4062df8bae1dSRodney W. Grimes  *	specified, the map may be changed to perform virtual
4063df8bae1dSRodney W. Grimes  *	copying operations, although the data referenced will
4064df8bae1dSRodney W. Grimes  *	remain the same.
4065df8bae1dSRodney W. Grimes  */
4066df8bae1dSRodney W. Grimes int
4067b9dcd593SBruce Evans vm_map_lookup(vm_map_t *var_map,		/* IN/OUT */
4068b9dcd593SBruce Evans 	      vm_offset_t vaddr,
406947221757SJohn Dyson 	      vm_prot_t fault_typea,
4070b9dcd593SBruce Evans 	      vm_map_entry_t *out_entry,	/* OUT */
4071b9dcd593SBruce Evans 	      vm_object_t *object,		/* OUT */
4072b9dcd593SBruce Evans 	      vm_pindex_t *pindex,		/* OUT */
4073b9dcd593SBruce Evans 	      vm_prot_t *out_prot,		/* OUT */
40742d8acc0fSJohn Dyson 	      boolean_t *wired)			/* OUT */
4075df8bae1dSRodney W. Grimes {
4076c0877f10SJohn Dyson 	vm_map_entry_t entry;
4077c0877f10SJohn Dyson 	vm_map_t map = *var_map;
4078c0877f10SJohn Dyson 	vm_prot_t prot;
407947221757SJohn Dyson 	vm_prot_t fault_type = fault_typea;
40803364c323SKonstantin Belousov 	vm_object_t eobject;
40810cc74f14SAlan Cox 	vm_size_t size;
4082ef694c1aSEdward Tomasz Napierala 	struct ucred *cred;
4083df8bae1dSRodney W. Grimes 
408419bd0d9cSKonstantin Belousov RetryLookup:
4085df8bae1dSRodney W. Grimes 
4086df8bae1dSRodney W. Grimes 	vm_map_lock_read(map);
4087df8bae1dSRodney W. Grimes 
408819bd0d9cSKonstantin Belousov RetryLookupLocked:
4089df8bae1dSRodney W. Grimes 	/*
40904c3ef59eSAlan Cox 	 * Lookup the faulting address.
4091df8bae1dSRodney W. Grimes 	 */
4092095104acSAlan Cox 	if (!vm_map_lookup_entry(map, vaddr, out_entry)) {
4093095104acSAlan Cox 		vm_map_unlock_read(map);
4094095104acSAlan Cox 		return (KERN_INVALID_ADDRESS);
4095095104acSAlan Cox 	}
4096df8bae1dSRodney W. Grimes 
40974e94f402SAlan Cox 	entry = *out_entry;
4098b7b2aac2SJohn Dyson 
4099df8bae1dSRodney W. Grimes 	/*
4100df8bae1dSRodney W. Grimes 	 * Handle submaps.
4101df8bae1dSRodney W. Grimes 	 */
4102afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4103df8bae1dSRodney W. Grimes 		vm_map_t old_map = map;
4104df8bae1dSRodney W. Grimes 
4105df8bae1dSRodney W. Grimes 		*var_map = map = entry->object.sub_map;
4106df8bae1dSRodney W. Grimes 		vm_map_unlock_read(old_map);
4107df8bae1dSRodney W. Grimes 		goto RetryLookup;
4108df8bae1dSRodney W. Grimes 	}
4109a04c970aSJohn Dyson 
4110df8bae1dSRodney W. Grimes 	/*
41110d94caffSDavid Greenman 	 * Check whether this task is allowed to have this page.
4112df8bae1dSRodney W. Grimes 	 */
4113df8bae1dSRodney W. Grimes 	prot = entry->protection;
411419bd0d9cSKonstantin Belousov 	if ((fault_typea & VM_PROT_FAULT_LOOKUP) != 0) {
411519bd0d9cSKonstantin Belousov 		fault_typea &= ~VM_PROT_FAULT_LOOKUP;
411619bd0d9cSKonstantin Belousov 		if (prot == VM_PROT_NONE && map != kernel_map &&
411719bd0d9cSKonstantin Belousov 		    (entry->eflags & MAP_ENTRY_GUARD) != 0 &&
411819bd0d9cSKonstantin Belousov 		    (entry->eflags & (MAP_ENTRY_STACK_GAP_DN |
411919bd0d9cSKonstantin Belousov 		    MAP_ENTRY_STACK_GAP_UP)) != 0 &&
412019bd0d9cSKonstantin Belousov 		    vm_map_growstack(map, vaddr, entry) == KERN_SUCCESS)
412119bd0d9cSKonstantin Belousov 			goto RetryLookupLocked;
412219bd0d9cSKonstantin Belousov 	}
412319bd0d9cSKonstantin Belousov 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
41242db65ab4SAlan Cox 	if ((fault_type & prot) != fault_type || prot == VM_PROT_NONE) {
4125095104acSAlan Cox 		vm_map_unlock_read(map);
4126095104acSAlan Cox 		return (KERN_PROTECTION_FAILURE);
412747221757SJohn Dyson 	}
4128b8db9776SKonstantin Belousov 	KASSERT((prot & VM_PROT_WRITE) == 0 || (entry->eflags &
4129b8db9776SKonstantin Belousov 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY)) !=
4130b8db9776SKonstantin Belousov 	    (MAP_ENTRY_USER_WIRED | MAP_ENTRY_NEEDS_COPY),
4131b8db9776SKonstantin Belousov 	    ("entry %p flags %x", entry, entry->eflags));
41325b3e0257SDag-Erling Smørgrav 	if ((fault_typea & VM_PROT_COPY) != 0 &&
41335b3e0257SDag-Erling Smørgrav 	    (entry->max_protection & VM_PROT_WRITE) == 0 &&
41345b3e0257SDag-Erling Smørgrav 	    (entry->eflags & MAP_ENTRY_COW) == 0) {
41355b3e0257SDag-Erling Smørgrav 		vm_map_unlock_read(map);
41365b3e0257SDag-Erling Smørgrav 		return (KERN_PROTECTION_FAILURE);
41375b3e0257SDag-Erling Smørgrav 	}
4138df8bae1dSRodney W. Grimes 
4139df8bae1dSRodney W. Grimes 	/*
41400d94caffSDavid Greenman 	 * If this page is not pageable, we have to get it for all possible
41410d94caffSDavid Greenman 	 * accesses.
4142df8bae1dSRodney W. Grimes 	 */
414305f0fdd2SPoul-Henning Kamp 	*wired = (entry->wired_count != 0);
414405f0fdd2SPoul-Henning Kamp 	if (*wired)
4145a6d42a0dSAlan Cox 		fault_type = entry->protection;
41463364c323SKonstantin Belousov 	size = entry->end - entry->start;
4147df8bae1dSRodney W. Grimes 	/*
4148df8bae1dSRodney W. Grimes 	 * If the entry was copy-on-write, we either ...
4149df8bae1dSRodney W. Grimes 	 */
4150afa07f7eSJohn Dyson 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
4151df8bae1dSRodney W. Grimes 		/*
41520d94caffSDavid Greenman 		 * If we want to write the page, we may as well handle that
4153ad5fca3bSAlan Cox 		 * now since we've got the map locked.
4154df8bae1dSRodney W. Grimes 		 *
41550d94caffSDavid Greenman 		 * If we don't need to write the page, we just demote the
41560d94caffSDavid Greenman 		 * permissions allowed.
4157df8bae1dSRodney W. Grimes 		 */
4158a6d42a0dSAlan Cox 		if ((fault_type & VM_PROT_WRITE) != 0 ||
4159a6d42a0dSAlan Cox 		    (fault_typea & VM_PROT_COPY) != 0) {
4160df8bae1dSRodney W. Grimes 			/*
41610d94caffSDavid Greenman 			 * Make a new object, and place it in the object
41620d94caffSDavid Greenman 			 * chain.  Note that no new references have appeared
4163ad5fca3bSAlan Cox 			 * -- one just moved from the map to the new
41640d94caffSDavid Greenman 			 * object.
4165df8bae1dSRodney W. Grimes 			 */
416625adb370SBrian Feldman 			if (vm_map_lock_upgrade(map))
4167df8bae1dSRodney W. Grimes 				goto RetryLookup;
41689917e010SAlan Cox 
4169ef694c1aSEdward Tomasz Napierala 			if (entry->cred == NULL) {
41703364c323SKonstantin Belousov 				/*
41713364c323SKonstantin Belousov 				 * The debugger owner is charged for
41723364c323SKonstantin Belousov 				 * the memory.
41733364c323SKonstantin Belousov 				 */
4174ef694c1aSEdward Tomasz Napierala 				cred = curthread->td_ucred;
4175ef694c1aSEdward Tomasz Napierala 				crhold(cred);
4176ef694c1aSEdward Tomasz Napierala 				if (!swap_reserve_by_cred(size, cred)) {
4177ef694c1aSEdward Tomasz Napierala 					crfree(cred);
41783364c323SKonstantin Belousov 					vm_map_unlock(map);
41793364c323SKonstantin Belousov 					return (KERN_RESOURCE_SHORTAGE);
41803364c323SKonstantin Belousov 				}
4181ef694c1aSEdward Tomasz Napierala 				entry->cred = cred;
41823364c323SKonstantin Belousov 			}
41830cc74f14SAlan Cox 			vm_object_shadow(&entry->object.vm_object,
41840cc74f14SAlan Cox 			    &entry->offset, size);
4185afa07f7eSJohn Dyson 			entry->eflags &= ~MAP_ENTRY_NEEDS_COPY;
41863364c323SKonstantin Belousov 			eobject = entry->object.vm_object;
4187ef694c1aSEdward Tomasz Napierala 			if (eobject->cred != NULL) {
41883364c323SKonstantin Belousov 				/*
41893364c323SKonstantin Belousov 				 * The object was not shadowed.
41903364c323SKonstantin Belousov 				 */
4191ef694c1aSEdward Tomasz Napierala 				swap_release_by_cred(size, entry->cred);
4192ef694c1aSEdward Tomasz Napierala 				crfree(entry->cred);
4193ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
4194ef694c1aSEdward Tomasz Napierala 			} else if (entry->cred != NULL) {
419589f6b863SAttilio Rao 				VM_OBJECT_WLOCK(eobject);
4196ef694c1aSEdward Tomasz Napierala 				eobject->cred = entry->cred;
41973364c323SKonstantin Belousov 				eobject->charge = size;
419889f6b863SAttilio Rao 				VM_OBJECT_WUNLOCK(eobject);
4199ef694c1aSEdward Tomasz Napierala 				entry->cred = NULL;
42003364c323SKonstantin Belousov 			}
42019917e010SAlan Cox 
42029b09b6c7SMatthew Dillon 			vm_map_lock_downgrade(map);
42030d94caffSDavid Greenman 		} else {
4204df8bae1dSRodney W. Grimes 			/*
42050d94caffSDavid Greenman 			 * We're attempting to read a copy-on-write page --
42060d94caffSDavid Greenman 			 * don't allow writes.
4207df8bae1dSRodney W. Grimes 			 */
42082d8acc0fSJohn Dyson 			prot &= ~VM_PROT_WRITE;
4209df8bae1dSRodney W. Grimes 		}
4210df8bae1dSRodney W. Grimes 	}
42112d8acc0fSJohn Dyson 
4212df8bae1dSRodney W. Grimes 	/*
4213df8bae1dSRodney W. Grimes 	 * Create an object if necessary.
4214df8bae1dSRodney W. Grimes 	 */
42154e71e795SMatthew Dillon 	if (entry->object.vm_object == NULL &&
42164e71e795SMatthew Dillon 	    !map->system_map) {
421725adb370SBrian Feldman 		if (vm_map_lock_upgrade(map))
4218df8bae1dSRodney W. Grimes 			goto RetryLookup;
421924a1cce3SDavid Greenman 		entry->object.vm_object = vm_object_allocate(OBJT_DEFAULT,
42203364c323SKonstantin Belousov 		    atop(size));
4221df8bae1dSRodney W. Grimes 		entry->offset = 0;
4222ef694c1aSEdward Tomasz Napierala 		if (entry->cred != NULL) {
422389f6b863SAttilio Rao 			VM_OBJECT_WLOCK(entry->object.vm_object);
4224ef694c1aSEdward Tomasz Napierala 			entry->object.vm_object->cred = entry->cred;
42253364c323SKonstantin Belousov 			entry->object.vm_object->charge = size;
422689f6b863SAttilio Rao 			VM_OBJECT_WUNLOCK(entry->object.vm_object);
4227ef694c1aSEdward Tomasz Napierala 			entry->cred = NULL;
42283364c323SKonstantin Belousov 		}
42299b09b6c7SMatthew Dillon 		vm_map_lock_downgrade(map);
4230df8bae1dSRodney W. Grimes 	}
4231b5b40fa6SJohn Dyson 
4232df8bae1dSRodney W. Grimes 	/*
42330d94caffSDavid Greenman 	 * Return the object/offset from this entry.  If the entry was
42340d94caffSDavid Greenman 	 * copy-on-write or empty, it has been fixed up.
4235df8bae1dSRodney W. Grimes 	 */
42362b6d1a63SKonstantin Belousov 	*pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset);
4237df8bae1dSRodney W. Grimes 	*object = entry->object.vm_object;
4238df8bae1dSRodney W. Grimes 
4239df8bae1dSRodney W. Grimes 	*out_prot = prot;
4240df8bae1dSRodney W. Grimes 	return (KERN_SUCCESS);
4241df8bae1dSRodney W. Grimes }
4242df8bae1dSRodney W. Grimes 
4243df8bae1dSRodney W. Grimes /*
424419dc5607STor Egge  *	vm_map_lookup_locked:
424519dc5607STor Egge  *
424619dc5607STor Egge  *	Lookup the faulting address.  A version of vm_map_lookup that returns
424719dc5607STor Egge  *      KERN_FAILURE instead of blocking on map lock or memory allocation.
424819dc5607STor Egge  */
424919dc5607STor Egge int
425019dc5607STor Egge vm_map_lookup_locked(vm_map_t *var_map,		/* IN/OUT */
425119dc5607STor Egge 		     vm_offset_t vaddr,
425219dc5607STor Egge 		     vm_prot_t fault_typea,
425319dc5607STor Egge 		     vm_map_entry_t *out_entry,	/* OUT */
425419dc5607STor Egge 		     vm_object_t *object,	/* OUT */
425519dc5607STor Egge 		     vm_pindex_t *pindex,	/* OUT */
425619dc5607STor Egge 		     vm_prot_t *out_prot,	/* OUT */
425719dc5607STor Egge 		     boolean_t *wired)		/* OUT */
425819dc5607STor Egge {
425919dc5607STor Egge 	vm_map_entry_t entry;
426019dc5607STor Egge 	vm_map_t map = *var_map;
426119dc5607STor Egge 	vm_prot_t prot;
426219dc5607STor Egge 	vm_prot_t fault_type = fault_typea;
426319dc5607STor Egge 
426419dc5607STor Egge 	/*
42654c3ef59eSAlan Cox 	 * Lookup the faulting address.
426619dc5607STor Egge 	 */
426719dc5607STor Egge 	if (!vm_map_lookup_entry(map, vaddr, out_entry))
426819dc5607STor Egge 		return (KERN_INVALID_ADDRESS);
426919dc5607STor Egge 
427019dc5607STor Egge 	entry = *out_entry;
427119dc5607STor Egge 
427219dc5607STor Egge 	/*
427319dc5607STor Egge 	 * Fail if the entry refers to a submap.
427419dc5607STor Egge 	 */
427519dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_IS_SUB_MAP)
427619dc5607STor Egge 		return (KERN_FAILURE);
427719dc5607STor Egge 
427819dc5607STor Egge 	/*
427919dc5607STor Egge 	 * Check whether this task is allowed to have this page.
428019dc5607STor Egge 	 */
428119dc5607STor Egge 	prot = entry->protection;
428219dc5607STor Egge 	fault_type &= VM_PROT_READ | VM_PROT_WRITE | VM_PROT_EXECUTE;
428319dc5607STor Egge 	if ((fault_type & prot) != fault_type)
428419dc5607STor Egge 		return (KERN_PROTECTION_FAILURE);
428519dc5607STor Egge 
428619dc5607STor Egge 	/*
428719dc5607STor Egge 	 * If this page is not pageable, we have to get it for all possible
428819dc5607STor Egge 	 * accesses.
428919dc5607STor Egge 	 */
429019dc5607STor Egge 	*wired = (entry->wired_count != 0);
429119dc5607STor Egge 	if (*wired)
4292a6d42a0dSAlan Cox 		fault_type = entry->protection;
429319dc5607STor Egge 
429419dc5607STor Egge 	if (entry->eflags & MAP_ENTRY_NEEDS_COPY) {
429519dc5607STor Egge 		/*
429619dc5607STor Egge 		 * Fail if the entry was copy-on-write for a write fault.
429719dc5607STor Egge 		 */
429819dc5607STor Egge 		if (fault_type & VM_PROT_WRITE)
429919dc5607STor Egge 			return (KERN_FAILURE);
430019dc5607STor Egge 		/*
430119dc5607STor Egge 		 * We're attempting to read a copy-on-write page --
430219dc5607STor Egge 		 * don't allow writes.
430319dc5607STor Egge 		 */
430419dc5607STor Egge 		prot &= ~VM_PROT_WRITE;
430519dc5607STor Egge 	}
430619dc5607STor Egge 
430719dc5607STor Egge 	/*
430819dc5607STor Egge 	 * Fail if an object should be created.
430919dc5607STor Egge 	 */
431019dc5607STor Egge 	if (entry->object.vm_object == NULL && !map->system_map)
431119dc5607STor Egge 		return (KERN_FAILURE);
431219dc5607STor Egge 
431319dc5607STor Egge 	/*
431419dc5607STor Egge 	 * Return the object/offset from this entry.  If the entry was
431519dc5607STor Egge 	 * copy-on-write or empty, it has been fixed up.
431619dc5607STor Egge 	 */
43172b6d1a63SKonstantin Belousov 	*pindex = UOFF_TO_IDX((vaddr - entry->start) + entry->offset);
431819dc5607STor Egge 	*object = entry->object.vm_object;
431919dc5607STor Egge 
432019dc5607STor Egge 	*out_prot = prot;
432119dc5607STor Egge 	return (KERN_SUCCESS);
432219dc5607STor Egge }
432319dc5607STor Egge 
432419dc5607STor Egge /*
4325df8bae1dSRodney W. Grimes  *	vm_map_lookup_done:
4326df8bae1dSRodney W. Grimes  *
4327df8bae1dSRodney W. Grimes  *	Releases locks acquired by a vm_map_lookup
4328df8bae1dSRodney W. Grimes  *	(according to the handle returned by that lookup).
4329df8bae1dSRodney W. Grimes  */
43300d94caffSDavid Greenman void
43311b40f8c0SMatthew Dillon vm_map_lookup_done(vm_map_t map, vm_map_entry_t entry)
4332df8bae1dSRodney W. Grimes {
4333df8bae1dSRodney W. Grimes 	/*
4334df8bae1dSRodney W. Grimes 	 * Unlock the main-level map
4335df8bae1dSRodney W. Grimes 	 */
4336df8bae1dSRodney W. Grimes 	vm_map_unlock_read(map);
4337df8bae1dSRodney W. Grimes }
4338df8bae1dSRodney W. Grimes 
4339c7c34a24SBruce Evans #include "opt_ddb.h"
4340c3cb3e12SDavid Greenman #ifdef DDB
4341c7c34a24SBruce Evans #include <sys/kernel.h>
4342c7c34a24SBruce Evans 
4343c7c34a24SBruce Evans #include <ddb/ddb.h>
4344c7c34a24SBruce Evans 
43452ebcd458SAttilio Rao static void
43462ebcd458SAttilio Rao vm_map_print(vm_map_t map)
4347df8bae1dSRodney W. Grimes {
4348c0877f10SJohn Dyson 	vm_map_entry_t entry;
4349c7c34a24SBruce Evans 
4350e5f251d2SAlan Cox 	db_iprintf("Task map %p: pmap=%p, nentries=%d, version=%u\n",
4351e5f251d2SAlan Cox 	    (void *)map,
4352101eeb7fSBruce Evans 	    (void *)map->pmap, map->nentries, map->timestamp);
4353df8bae1dSRodney W. Grimes 
4354c7c34a24SBruce Evans 	db_indent += 2;
4355df8bae1dSRodney W. Grimes 	for (entry = map->header.next; entry != &map->header;
4356df8bae1dSRodney W. Grimes 	    entry = entry->next) {
435719bd0d9cSKonstantin Belousov 		db_iprintf("map entry %p: start=%p, end=%p, eflags=%#x, \n",
435819bd0d9cSKonstantin Belousov 		    (void *)entry, (void *)entry->start, (void *)entry->end,
435919bd0d9cSKonstantin Belousov 		    entry->eflags);
4360e5f251d2SAlan Cox 		{
4361df8bae1dSRodney W. Grimes 			static char *inheritance_name[4] =
4362df8bae1dSRodney W. Grimes 			{"share", "copy", "none", "donate_copy"};
43630d94caffSDavid Greenman 
436495e5e988SJohn Dyson 			db_iprintf(" prot=%x/%x/%s",
4365df8bae1dSRodney W. Grimes 			    entry->protection,
4366df8bae1dSRodney W. Grimes 			    entry->max_protection,
43678aef1712SMatthew Dillon 			    inheritance_name[(int)(unsigned char)entry->inheritance]);
4368df8bae1dSRodney W. Grimes 			if (entry->wired_count != 0)
436995e5e988SJohn Dyson 				db_printf(", wired");
4370df8bae1dSRodney W. Grimes 		}
43719fdfe602SMatthew Dillon 		if (entry->eflags & MAP_ENTRY_IS_SUB_MAP) {
4372cd034a5bSMaxime Henrion 			db_printf(", share=%p, offset=0x%jx\n",
43739fdfe602SMatthew Dillon 			    (void *)entry->object.sub_map,
4374cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4375df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
43769fdfe602SMatthew Dillon 			    (entry->prev->object.sub_map !=
43779fdfe602SMatthew Dillon 				entry->object.sub_map)) {
4378c7c34a24SBruce Evans 				db_indent += 2;
43792ebcd458SAttilio Rao 				vm_map_print((vm_map_t)entry->object.sub_map);
4380c7c34a24SBruce Evans 				db_indent -= 2;
4381df8bae1dSRodney W. Grimes 			}
43820d94caffSDavid Greenman 		} else {
4383ef694c1aSEdward Tomasz Napierala 			if (entry->cred != NULL)
4384ef694c1aSEdward Tomasz Napierala 				db_printf(", ruid %d", entry->cred->cr_ruid);
4385cd034a5bSMaxime Henrion 			db_printf(", object=%p, offset=0x%jx",
4386101eeb7fSBruce Evans 			    (void *)entry->object.vm_object,
4387cd034a5bSMaxime Henrion 			    (uintmax_t)entry->offset);
4388ef694c1aSEdward Tomasz Napierala 			if (entry->object.vm_object && entry->object.vm_object->cred)
4389ef694c1aSEdward Tomasz Napierala 				db_printf(", obj ruid %d charge %jx",
4390ef694c1aSEdward Tomasz Napierala 				    entry->object.vm_object->cred->cr_ruid,
43913364c323SKonstantin Belousov 				    (uintmax_t)entry->object.vm_object->charge);
4392afa07f7eSJohn Dyson 			if (entry->eflags & MAP_ENTRY_COW)
4393c7c34a24SBruce Evans 				db_printf(", copy (%s)",
4394afa07f7eSJohn Dyson 				    (entry->eflags & MAP_ENTRY_NEEDS_COPY) ? "needed" : "done");
4395c7c34a24SBruce Evans 			db_printf("\n");
4396df8bae1dSRodney W. Grimes 
4397df8bae1dSRodney W. Grimes 			if ((entry->prev == &map->header) ||
4398df8bae1dSRodney W. Grimes 			    (entry->prev->object.vm_object !=
4399df8bae1dSRodney W. Grimes 				entry->object.vm_object)) {
4400c7c34a24SBruce Evans 				db_indent += 2;
4401101eeb7fSBruce Evans 				vm_object_print((db_expr_t)(intptr_t)
4402101eeb7fSBruce Evans 						entry->object.vm_object,
440344bbc3b7SKonstantin Belousov 						0, 0, (char *)0);
4404c7c34a24SBruce Evans 				db_indent -= 2;
4405df8bae1dSRodney W. Grimes 			}
4406df8bae1dSRodney W. Grimes 		}
4407df8bae1dSRodney W. Grimes 	}
4408c7c34a24SBruce Evans 	db_indent -= 2;
4409df8bae1dSRodney W. Grimes }
441095e5e988SJohn Dyson 
44112ebcd458SAttilio Rao DB_SHOW_COMMAND(map, map)
44122ebcd458SAttilio Rao {
44132ebcd458SAttilio Rao 
44142ebcd458SAttilio Rao 	if (!have_addr) {
44152ebcd458SAttilio Rao 		db_printf("usage: show map <addr>\n");
44162ebcd458SAttilio Rao 		return;
44172ebcd458SAttilio Rao 	}
44182ebcd458SAttilio Rao 	vm_map_print((vm_map_t)addr);
44192ebcd458SAttilio Rao }
442095e5e988SJohn Dyson 
442195e5e988SJohn Dyson DB_SHOW_COMMAND(procvm, procvm)
442295e5e988SJohn Dyson {
442395e5e988SJohn Dyson 	struct proc *p;
442495e5e988SJohn Dyson 
442595e5e988SJohn Dyson 	if (have_addr) {
4426a9546a6bSJohn Baldwin 		p = db_lookup_proc(addr);
442795e5e988SJohn Dyson 	} else {
442895e5e988SJohn Dyson 		p = curproc;
442995e5e988SJohn Dyson 	}
443095e5e988SJohn Dyson 
4431ac1e407bSBruce Evans 	db_printf("p = %p, vmspace = %p, map = %p, pmap = %p\n",
4432ac1e407bSBruce Evans 	    (void *)p, (void *)p->p_vmspace, (void *)&p->p_vmspace->vm_map,
4433b1028ad1SLuoqi Chen 	    (void *)vmspace_pmap(p->p_vmspace));
443495e5e988SJohn Dyson 
44352ebcd458SAttilio Rao 	vm_map_print((vm_map_t)&p->p_vmspace->vm_map);
443695e5e988SJohn Dyson }
443795e5e988SJohn Dyson 
4438c7c34a24SBruce Evans #endif /* DDB */
4439